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 phoneNoteBookMap;

  public PhoneNoteBook(String fileName) throws IOException { phoneNoteBookMap = new TreeMap();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, "\t");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:\n");String phone = JOptionPane.showInputDialog("Please enter the phone:\n");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:\n");String phone = JOptionPane.showInputDialog("Please enter his/her new phone name:\n");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:\n");if (!phoneNoteBookMap.containsKey(name)) { JOptionPane.showConfirmDialog(null, "This name does not exist. So you don't 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:\n" + "Enter \"1\" to add a new record;\n" + "Enter \"2\" to update a existing record;\n" + "Enter \"3\" to find a phone number;\n" + "Enter \"4\" to remove a existing record.\n";

  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();}