技术中心

这里象征着我们的态度和能力

>Java实现的简单电话号码储存
发布者:中国IT实验室    信息来源:中国IT实验室    发布时间:2012-01-28      浏览次数:5638
分享到:

新浪微博

腾讯微博

QQ空间

豆瓣网

QQ好友

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

  view plainprint?

  package com.sinosuperman.example;

  import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.StringTokenizer;import java.util.TreeMap;

  import javax.swing.JOptionPane;

  public class PhoneNoteBook {

  private final File phoneNoteBookFile;

  private TreeMap<String, String> phoneNoteBookMap;

  public PhoneNoteBook(String fileName) throws IOException { phoneNoteBookMap = new TreeMap<String, String>();phoneNoteBookFile = new File(fileName);loadAllRecords();}

  private void loadAllRecords() throws IOException { BufferedReader reader = new BufferedReader(new FileReader(phoneNoteBookFile));String line = reader.readLine();while (line != null) { StringTokenizer str = new StringTokenizer(line, " ");String name = str.nextToken();String phone = str.nextToken();phoneNoteBookMap.put(name, phone);line = reader.readLine();} reader.close();}

  private void addRecord() { String name = JOptionPane.showInputDialog("Please enter the name: ");String phone = JOptionPane.showInputDialog("Please enter the phone: ");if (JOptionPane.showConfirmDialog(null, "Are you sure?") == JOptionPane.YES_OPTION) { phoneNoteBookMap.put(name, phone);} else { JOptionPane.showMessageDialog(null, "Operation has been canceled");}

  private void updateRecord() { String name = JOptionPane.showInputDialog("Please enter the name: ");String phone = JOptionPane.showInputDialog("Please enter his/her new phone name: ");if (!phoneNoteBookMap.containsKey(name)) { if (JOptionPane.showConfirmDialog(null, "This name does not exist. Do you want to create a new one?") == JOptionPane.YES_OPTION) { phoneNoteBookMap.put(name, phone);} else { JOptionPane.showMessageDialog(null, "Operation has been canceled");} } if (JOptionPane.showConfirmDialog(null, "Are you sure to modify his/her phone number?") == JOptionPane.YES_OPTION) { phoneNoteBookMap.put(name, phone);} else { JOptionPane.showMessageDialog(null, "Operation has been canceled");}

  private void searchRecord() { String name = JOptionPane.showInputDialog("Please the name for searching");if (phoneNoteBookMap.containsKey(name)) { JOptionPane.showMessageDialog(null, phoneNoteBookMap.get(name));} else { JOptionPane.showMessageDialog(null, "The name you are searching does not exists.");}

  private void removeRecord() { String name = JOptionPane.showInputDialog("Please enter the name: ");if (!phoneNoteBookMap.containsKey(name)) { JOptionPane.showConfirmDialog(null, "This name does not exist. So you dont need to remove it.");} else if (JOptionPane.showConfirmDialog(null, "Are you sure to remove his/her record?") == JOptionPane.YES_OPTION) { phoneNoteBookMap.remove(name);} else { JOptionPane.showMessageDialog(null, "Operation has been canceled");}

  public void display() { String message = "Please select an operation: " + "Enter "1" to add a new record; " + "Enter "2" to update a existing record; " + "Enter "3" to find a phone number; " + "Enter "4" to remove a existing record. ";

  int choice = 0;try { choice = Integer.parseInt(JOptionPane.showInputDialog(message));switch (choice) { case 1:addRecord();break;case 2:updateRecord();break;case 3:searchRecord();break;case 4:removeRecord();break;default:} } catch (NumberFormatException e) { }

  if (JOptionPane.showConfirmDialog(null, "Would you want to continue?") != JOptionPane.YES_OPTION) { JOptionPane.showMessageDialog(null, "Thank you.");} else { display();}

  测试驱动程序:view plainprint?

  package com.sinosuperman.driver;

  import java.io.IOException;

  import com.sinosuperman.example.PhoneNoteBook;

  public class Driver { public static void main(String[] args) throws IOException { PhoneNoteBook phoneNoteBook = new PhoneNoteBook("PhoneNoteBook.txt");phoneNoteBook.display();}

4000-880-989
(24小时热线)
联系客服
微信公众号

官方公众号

小程序

©2008-2022 CORPORATION ALL Rights Reserved. 昆明奥远科技有限公司版权所有 滇ICP备09003328号-1 滇公网安备 53011102000818号
昆明那家网络公司好,新媒体运营,网站优化,网络推广,网站建设,网页设计,网站设计,网站推广,云南网站公司,昆明新媒体公司,云南网红主播,昆明SEO公司,昆明网站建设,昆明网络推广,昆明网站优化,昆明网站推广,红河网站建设,大理网络公司,曲靖网络公司,丽江网站设计,昭通网络公司,保山大数据服务,智慧高速建设,智慧校园服务,云南IDC服务商,网络安全测评,等保测评,网站关键词排名优化服务,服务客户尽超2000余家,一切尽在奥远科技,服务电话:13888956730