在java控制台中编写电话本,利用hashmap存储数据

这是java中的控制台的电话本小程序,使用了hashmap存储联系人信息
在java控制台中编写电话本,利用hashmap存储数据_第1张图片
在java控制台中编写电话本,利用hashmap存储数据_第2张图片
在java控制台中编写电话本,利用hashmap存储数据_第3张图片

package com.dsr.java;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Scanner;

public class phonebook {
    boolean startsingle = true;
    Scanner sc = new Scanner(System.in);
    // LinkedHashMap是HashMap的一个子类,他会维护一个链表,记录你的插入循序,让你能够按顺序输出。
    LinkedHashMap book = new LinkedHashMap();

    private String name;
    private String sex;
    private String age;
    private String num;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new phonebook().run();
    }

    private void run() {
        // TODO Auto-generated method stub
        while (startsingle) {

            System.out.println("———————————————————电话本———————————————————");
            System.out.println("1.添加\t2.删除\t3.修改\t4.查询\t5.打印全部\t0.退出");
            System.out.println("———————————————————电话本———————————————————");
            System.out.println("请输入数字选择功能");
            String select = sc.next();
            switch (select) {
            case "1":
                add();
                break;
            case "2":
                delete();
                break;
            case "3":
                update();
                break;
            case "4":
                search();
                break;
            case "5":
                printall();
                break;
            case "0":
                sc.close();
                startsingle = false;
                break;

            default:
                System.out.println("输错了,重来!!!!");
                break;
            }
        }
    }

    private void printall() {
        // TODO Auto-generated method stub
        // 1.常见的打印方式
        int i = 1;
        for (String key : book.keySet()) {
            System.out.print("第" + i + "位联系人\t");
            System.out.println(book.get(key).printone());
            i++;
        }

        //Iterator iter=book.entrySet().iterator();
        //while(iter.hasNext()){
        //  System.out.println(iter.next());
        //}

    }

    private void search() {
        // TODO Auto-generated method stub
        System.out.println("输入查询的联系人姓名");
        name = sc.next();
        if (find(name) == 0) {
            System.out.println("联系人不存在!!!!");
            return;
        }
        System.out.println(book.get(name).printone());

    }

    private void update() {
        // TODO Auto-generated method stub
        System.out.println("输入要修改的联系人信息!!!");
        name = sc.next();
        int update = find(name);
        if (update == 0) {
            System.out.println("你要修改的联系人不存在!!!!");
            return;
        }
        book.remove(name);
        books b = new books();
        System.out.println("输入姓名!!!");
        name = sc.next();
        // 联系人信息修改之后会附带着修改hashmap中的键
        b.setName(name);
        System.out.println("输入性别!!!");
        b.setSex(sc.next());
        System.out.println("输入年龄!!!");
        b.setAge(sc.next());
        System.out.println("输入电话!!!");
        b.setNum(sc.next());
        book.put(name, b);
        System.out.println(b.printone());
        b = null;
        System.gc();

    }

    private void delete() {
        // TODO Auto-generated method stub
        System.out.println("请输入要删除联系人的姓名!!!!!");
        String name = sc.next();

        int i = find(name);
        if (i == 1) {
            book.remove(name);
            System.out.println("删除成功!!!!");
        } else {

            System.out.println("你要删除的联系人不存在!!!!");

        }
    }

    private void add() {
        // TODO Auto-generated method stub
        name = null;
        boolean addsingle = true;
        while (addsingle) {
            System.out.println("输入姓名!!!!!");
            name = sc.next();
            if (book.get(name) != null) {
                System.out.println("联系人已存在,确认覆盖联系人?????");
                System.out.println("1.覆盖\t2.重新输入\t3.取消");
                String covervalue = sc.next();
                switch (covervalue) {
                case "1":
                    System.out.println("输入姓名!!!!!");
                    name = sc.next();
                    addsingle = false;
                    break;
                case "2":

                    break;
                case "3":
                    addsingle = false;
                    return;

                default:
                    System.out.println("输错了!!!!");
                    break;
                }
            } else {
                addsingle = false;
            }
        }
        System.out.println("输入性别!!!!!");
        sex = sc.next();
        System.out.println("输入姓年龄!!!!!");
        age = sc.next();
        System.out.println("输入电话!!!!!");
        num = sc.next();
        books b = new books();
        b.setName(name);
        b.setAge(age);
        b.setNum(num);
        b.setSex(sex);
        book.put(name, b);
        System.out.println("添加成功");
        System.out.println(b.printone());
        b = null;
        System.gc();

    }

    private int find(String name) {

        if (book.get(name) == null) {
            return 0;// 表示不存在
        }
        return 1;
    }
}

class books {
    private String name;
    private String sex;
    private String age;
    private String num;

    public String printone() {
        return "name:" + name + "\tsex:" + sex + "\tage:" + age + "\tnum:"
                + num;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

}

你可能感兴趣的:(java)