Java实现的简单电话号码储存

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



测试驱动程序:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值