Java-implemented simple phone number Storage

Source: Internet
Author: User
package com.sinosuperman.example;import;import;import;import;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();}}}

Test driver:

package com.sinosuperman.driver;import;import com.sinosuperman.example.PhoneNoteBook;public class Driver {public static void main(String[] args) throws IOException {PhoneNoteBook phoneNoteBook = new PhoneNoteBook("PhoneNoteBook.txt");phoneNoteBook.display();}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.