学习JAVA.day01

学习JAVA.day01
简单通讯录实现
功能描述:
1.查询通讯录
2.添加新用户
3.删除旧用户
4.编辑旧用户
5.退出通讯录
通过对通讯录的编辑,练习JAVA基础语法及流程结构
附上效果图:
figure 1.通讯录效果图
附源码:

import java.util.Scanner;

public class ContactList {
    static int perNum = 0;
    public static void main(String[] args) {
        boolean flagOnline = true;
        boolean isFirstInList = true;
        Person firstPer = null;
        while (flagOnline) {
            //1:显示通讯录选项  功能:查看通讯录、添加新用户、删除旧用户、编辑旧用户、退出通讯录
            showOpt();
            //2.初始化用户列表
            if (isFirstInList) {
                firstPer = initList();
                isFirstInList = false;
            }
            //3.等待输入
            boolean isCinOk = true;
            int index = 0;
            while (isCinOk) {
                index = new Scanner(System.in).nextInt();
                if (index > 5 || index < 1) {
                    System.out.println("请求错误!请输入1-5选项");
                } else {
                    isCinOk = false;
                }
            }
            switch (index) {
                case 1:
                    System.out.println("查看通讯录");
                    showList(firstPer);
                    break;
                case 2:
                    System.out.println("添加新用户");
                    addNewPer(firstPer);
                    break;
                case 3:
                    System.out.println("删除旧用户");
                    deletePer(firstPer);
                    break;
                case 4:
                    System.out.println("编辑旧用户");
                    remPer(firstPer);
                    break;
                case 5:
                    System.out.println("退出通讯录");
                    flagOnline = false;
                    break;
                default:
                    System.out.println("输入错误!");
                    break;
            }
            isCinOk = true;
        }
    }
    public static void showOpt() {
        System.out.println("========欢迎进入通讯录========");
        System.out.println("1.查看通讯录");
        System.out.println("2.添加新用户");
        System.out.println("3.删除旧用户");
        System.out.println("4.编辑旧用户");
        System.out.println("5.退出通讯录");
        System.out.println("============================");
    }
    //初始化通讯录
    public static Person initList() {
        Person per = new Person();
        per.num = perNum++;
        per.setName("移动客服");
        per.setAge(120);
        per.setPhoneNum("10086");
        per.pNext = null;
        return per;
    }
    //查看通讯录
    public static void showList(Person firstPer) {
        while (firstPer != null) {
            System.out.println(firstPer.num + "  " + firstPer.getName() + "  " + firstPer.getAge() + "  " + firstPer.getPhoneNum());
            firstPer = firstPer.pNext;
        }
    }
    //添加新用户
    public static void addNewPer(Person firstPer) {
        Person newPer = new Person();
        newPer.num = perNum++;
        System.out.println("请输入新用户姓名:");
        newPer.setName(new Scanner(System.in).nextLine());
        System.out.println("请输入新用户年龄:");
        newPer.setAge(new Scanner(System.in).nextInt());
        System.out.println("请输入新用户电话:");
        newPer.setPhoneNum(new Scanner(System.in).nextLine());
        newPer.pNext = null;
        addPer(firstPer, newPer);
    }
    public static void addPer(Person firstPer, Person newPer) {
        while (firstPer.pNext != null) {
            firstPer = firstPer.pNext;
        }
        firstPer.pNext = newPer;
    }
    //删除旧用户
    public static void deletePer(Person firstPer) {
        int delNum;
        Person delPer = null;
        boolean isFindPer = false;
        System.out.println("请输入待删除用户编号:");
        delNum = new Scanner(System.in).nextInt();
        if (delNum > 0) {
            while (firstPer.pNext != null) {
                if (firstPer.pNext.num == delNum) {
                    delPer = firstPer.pNext;
                    isFindPer = true;
                    firstPer.pNext = delPer.pNext;
                }
                if (isFindPer) {
                    firstPer = firstPer.pNext;
                    firstPer.num--;
                } else {
                    firstPer = firstPer.pNext;
                }
            }
            perNum--;
        }
    }
    //编辑旧用户
    public static void remPer(Person firstPer) {
        int remNum;
        Person remPer = null;
        System.out.println("请输入待编辑用户编号:");
        remNum = new Scanner(System.in).nextInt();
        if (remNum > 0) {
            while (firstPer != null) {
                if (firstPer.num == remNum) {
                    System.out.println("请输入新姓名:");
                    firstPer.setName(new Scanner(System.in).nextLine());
                    System.out.println("请输入新年龄:");
                    firstPer.setAge(new Scanner(System.in).nextInt());
                    System.out.println("请输入新电话:");
                    firstPer.setPhoneNum(new Scanner(System.in).nextLine());
                    break;
                }
                firstPer = firstPer.pNext;
            }
        }
    }
}

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值