1. package com.sinosuperman.example; 
  2.  
  3. import java.io.BufferedReader; 
  4. import java.io.File; 
  5. import java.io.FileReader; 
  6. import java.io.IOException; 
  7. import java.util.StringTokenizer; 
  8. import java.util.TreeMap; 
  9.  
  10. import javax.swing.JOptionPane; 
  11.  
  12. public class PhoneNoteBook { 
  13.  
  14. private final File phoneNoteBookFile; 
  15.  
  16. private TreeMap<String, String> phoneNoteBookMap; 
  17.  
  18. public PhoneNoteBook(String fileName) throws IOException { 
  19.         phoneNoteBookMap = new TreeMap<String, String>(); 
  20.         phoneNoteBookFile = new File(fileName); 
  21.         loadAllRecords(); 
  22.     } 
  23.  
  24. private void loadAllRecords() throws IOException { 
  25.         BufferedReader reader = new BufferedReader(new FileReader(phoneNoteBookFile)); 
  26.         String line = reader.readLine(); 
  27. while (line != null) { 
  28.             StringTokenizer str = new StringTokenizer(line, "\t"); 
  29.             String name = str.nextToken(); 
  30.             String phone = str.nextToken(); 
  31.             phoneNoteBookMap.put(name, phone); 
  32.             line = reader.readLine(); 
  33.         } 
  34.         reader.close(); 
  35.     } 
  36.  
  37. private void addRecord() { 
  38.         String name = JOptionPane.showInputDialog("Please enter the name:\n"); 
  39.         String phone = JOptionPane.showInputDialog("Please enter the phone:\n"); 
  40. if (JOptionPane.showConfirmDialog(null, "Are you sure?") == JOptionPane.YES_OPTION) { 
  41.             phoneNoteBookMap.put(name, phone); 
  42.         } else { 
  43.             JOptionPane.showMessageDialog(null, "Operation has been canceled"); 
  44.         } 
  45.     } 
  46.  
  47. private void updateRecord() { 
  48.         String name = JOptionPane.showInputDialog("Please enter the name:\n"); 
  49.         String phone = JOptionPane.showInputDialog("Please enter his/her new phone name:\n"); 
  50. if (!phoneNoteBookMap.containsKey(name)) { 
  51. if (JOptionPane.showConfirmDialog(null, "This name does not exist. Do you want to create a new one?") == JOptionPane.YES_OPTION) { 
  52.                 phoneNoteBookMap.put(name, phone); 
  53.             } else { 
  54.                 JOptionPane.showMessageDialog(null, "Operation has been canceled"); 
  55.             } 
  56.         } if (JOptionPane.showConfirmDialog(null, "Are you sure to modify his/her phone number?") == JOptionPane.YES_OPTION) { 
  57.             phoneNoteBookMap.put(name, phone); 
  58.         } else { 
  59.             JOptionPane.showMessageDialog(null, "Operation has been canceled"); 
  60.         } 
  61.     } 
  62.  
  63. private void searchRecord() { 
  64.         String name = JOptionPane.showInputDialog("Please the name for searching"); 
  65. if (phoneNoteBookMap.containsKey(name)) { 
  66.             JOptionPane.showMessageDialog(null, phoneNoteBookMap.get(name)); 
  67.         } else { 
  68.             JOptionPane.showMessageDialog(null, "The name you are searching does not exists."); 
  69.         } 
  70.     } 
  71.  
  72. private void removeRecord() { 
  73.         String name = JOptionPane.showInputDialog("Please enter the name:\n"); 
  74. if (!phoneNoteBookMap.containsKey(name)) { 
  75.             JOptionPane.showConfirmDialog(null, "This name does not exist. So you don't need to remove it."); 
  76.         } else if (JOptionPane.showConfirmDialog(null, "Are you sure to remove his/her record?") == JOptionPane.YES_OPTION) { 
  77.             phoneNoteBookMap.remove(name); 
  78.         } else { 
  79.             JOptionPane.showMessageDialog(null, "Operation has been canceled"); 
  80.         }  
  81.     } 
  82.  
  83. public void display() { 
  84.         String message = "Please select an operation:\n" + 
  85. "Enter \"1\" to add a new record;\n" +  
  86. "Enter \"2\" to update a existing record;\n" + 
  87. "Enter \"3\" to find a phone number;\n" + 
  88. "Enter \"4\" to remove a existing record.\n"; 
  89.  
  90. int choice = 0; 
  91. try { 
  92.             choice = Integer.parseInt(JOptionPane.showInputDialog(message)); 
  93. switch (choice) { 
  94. case 1: 
  95.                 addRecord(); 
  96. break; 
  97. case 2: 
  98.                 updateRecord(); 
  99. break; 
  100. case 3: 
  101.                 searchRecord(); 
  102. break; 
  103. case 4: 
  104.                 removeRecord(); 
  105. break; 
  106. default: 
  107.             } 
  108.         } catch (NumberFormatException e) { 
  109.         } 
  110.  
  111. if (JOptionPane.showConfirmDialog(null, "Would you want to continue?") != JOptionPane.YES_OPTION) { 
  112.             JOptionPane.showMessageDialog(null, "Thank you."); 
  113.         } else { 
  114.             display(); 
  115.         } 
  116.     } 

测试驱动程序:

   package com.sinosuperman.driver; 

  1.  
  2. import java.io.IOException; 
  3.  
  4. import com.sinosuperman.example.PhoneNoteBook; 
  5.  
  6. public class Driver { 
  7. public static void main(String[] args) throws IOException { 
  8.         PhoneNoteBook phoneNoteBook = new PhoneNoteBook("PhoneNoteBook.txt"); 
  9.         phoneNoteBook.display(); 
  10.     }