简易版的通讯录

通讯录描述:

主要功能:添加,查找,删除和更改

1.添加
由用户从键盘输入用户名,手机号和办公室号码,如果用户名已经存在了,那么会提示用户添加失败,该用户名已经存在。反之提示用户添加成功。
2.寻找
根据用户从键盘输入用户名,首先判断通讯录是否存在这个用户,如果在通讯录列表中,那么就找到对应的手机号码和办公室号码返回给用户。否则就通知用户,该用户不存在。
3.删除
根据用户从键盘输入用户名,首先判断通讯录是否存在这个用户,如果在通讯录列表中,那么就删除该用户,否则就提示用户,该用户不在。
4.更新
根据用户从键盘输入用户名,手机号,以及办公室号,首先判断通讯录是否存在这个用户,如果在通讯录列表中,那么就找到这个用户,修改他们的手机号以及办公室号并且告诉用户更新成功。否则就通知用户,该用户不存在,无法更新。
import java.util.Scanner;
/**
 * @author : panjing
 * @data : 2019/10/24 21:26
 * @describe :
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Contect contect = new Contect();
       while(true){
           //1.先打印菜单
           meun();
           //2.根据选择做事
           int a = sc.nextInt();
           sc.nextLine();
           switch (a){
               case 1:
                   add(sc,contect);break;
               case 2:
                   select(sc,contect);break;
               case 3:
                   remove(sc,contect);break;
               case 4:
                   update(sc,contect);break;
                default:
                    System.out.println("输入格式不正确!");
           }

       }
    }

    private static void update(Scanner sc, Contect contect) {  // 更新

        System.out.println("请输入要更新的用户名:");
        String name = sc.nextLine();
        System.out.println("请输入要更新的手机号:");
        String mobilePhone = sc.nextLine();
        System.out.println("请输入要更新的办公室号:");
        String officePhone = sc.nextLine();

        if(contect.update(name,mobilePhone,officePhone)){
            System.out.println("更新成功!");
        }else {
            System.out.println("该用户不存在,无法更新!");
        }

    }

    private static void remove(Scanner sc, Contect contect) { //删除
        System.out.println("请输入需要删除的用户名:");
        String name = sc.nextLine();
        if(contect.remove(name)){
            System.out.println("删除成功!");
        }else{
            System.out.println("该用户不存在!");
        }
    }

    private static void select(Scanner sc,Contect contect) { //查找

        System.out.println("请输入要查找的姓名:");
        String name = sc.nextLine();
        if(contect.select(name) == null){
            System.out.println("用户不存在!");
        }else{
            System.out.println("用户名称:"+ name +"\n " + "查找的电话:" + contect.select(name));
        }
    }


    private static void add(Scanner sc,Contect contect) { //添加
        System.out.println("请输入姓名:");
        String name = sc.nextLine();
        System.out.println("请输入手机号:");
        String mobilePhone = sc.nextLine();
        System.out.println("请输入办公室电话:");
        String officePhone = sc.nextLine();

        try {
            contect.add(name,mobilePhone,officePhone);
            System.out.println("用户添加成功!");
        } catch (Exception e) {
            System.out.println("添加失败,用户已存在!");
        }finally{
            System.out.println();
        }

    }

    private static void meun() {
        System.out.println("------------------");
        System.out.println("|     通讯录     |");
        System.out.println("|     1.添加     |");
        System.out.println("|     2.查找     |");
        System.out.println("|     3.删除     |");
        System.out.println("|     4.更新     |");
        System.out.println("------------------");

        System.out.println(" 请输入你的选择: ");
    }

}

 

/**
 * @author : panjing
 * @data : 2019/10/24 21:24
 * @describe :
 */
public class User {
    String name;
    String mobilePhone;
    String officePhone;

    public User(String name, String mobilePhone, String officePhone) {
        this.name = name;
        this.mobilePhone = mobilePhone;
        this.officePhone = officePhone;
    }

    @Override
    public String toString() {
        return "用户名为" + name  +
                ", mobilePhone= '" + mobilePhone +" \'"+
                ", officePhone= '" + officePhone +" \'";
    }
}

 


import java.util.Map;
import java.util.TreeMap;
/**
 * @author : panjing
 * @data : 2019/10/24 21:24
 * @describe :
 */
public class Contect {
    Map<String,User> map = new TreeMap<>();

    public void add(String name, String mobilePhone, String officePhone) throws Exception{
        User user = new User(name,mobilePhone,officePhone);
        if(map.containsKey(name)){
            throw new Exception("用户已存在!");
        }
        map.put(name,user);
    }

    public User select(String name) {
        if(map.containsKey(name)){
            return map.get(name);
        }
        return null;
    }

    public boolean remove(String name) {
        if(map.containsKey(name)){
            map.remove(name);
            return true;
        }

        return false;
    }

    public boolean update(String name, String mobilePhone, String officePhone) {
        if(map.containsKey(name)){
            remove(name);
            User  user = new User(name,mobilePhone,officePhone);
            map.put(name,user);
            return true;
        }
            return false;

    }
}

结果预览:

1.添加

2.查找

3.删除

4.更新

如果用户名不存在,是不会更新成功的。如图:

只有当通讯录中有这个名字,才能更新成功。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值