加入收藏 | 设为首页 | 会员中心 | 我要投稿 我爱故事小小网_铜陵站长网 (http://www.0562zz.com/)- 视频终端、云渲染、应用安全、数据安全、安全管理!
当前位置: 首页 > 教程 > 正文

Java达成的简单电话号码储存

发布时间:2021-12-18 15:27:45 所属栏目:教程 来源:互联网
导读: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 ja

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

(编辑:我爱故事小小网_铜陵站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读