JAVA学习笔记6,客户管理系统,实现对客户增删改查,对之前学习的内容做一个小结

JAVA学习笔记6,客户管理系统,对之前学习的内容做一个小结


模拟实现一个基于文本界面的《客户信息管理软件》


效果图:
主界面
添加客户
客户列表

控制台输入工具类
import java.util.Scanner;

public class ScanUtil {
    private static Scanner scan=new Scanner(System.in);
    public static int getMenuNum() {
        Integer num;
        while (true) {
            Object obj=getMustInput("(0-4)",1);
            num=(Integer)obj;
            if (num>=0 && num<=4) {
                return num;
            }else{
                System.out.print("请输入0-4:");
            }
        }      
    }
    public static Long getPhoneNum() {
        Object obj=getMustInput("11位数的手机号",2);
        return (Long)obj;
    }

    public static Object getPhone() {
        Object phone = getInput(2);
        return phone;
    }

    public static Object getMustInput(String desc,int...arr){
        String line;
        System.out.print("请输入"+desc+":");
        while (scan.hasNextLine()){
            line=scan.nextLine();
            if (line.length()==0) {
                System.out.print("你还没有输入,请重新输入"+desc+":");
                continue;
            }
            if (arr.length==1) {//arr.length=1表示获取数字,
                if(arr[0]==1){//arr[0]==1表示获取int数字
                    try {
                        int result=Integer.valueOf(line);
                        return result;
                    } catch (Exception e) {
                        System.out.print("输入的不是数字,请重新输入:");
                        continue;
                    }
                }else{//arr[0]==1表示获取Long数字
                    if (line.startsWith("1") && line.length()==11) {
                        try {
                            Long result=Long.valueOf(line);
                            return result;
                        } catch (Exception e) {
                            System.out.print("输入的不是手机号,请重新输入:");
                            continue;
                        }
                    }else{
                        System.out.print("输入的不是手机号,请重新输入:");
                        continue;
                    }
                }
            }else{//arr.length!=1表示获取数字,获取字符串
                return line;
            }
        }
        return null;
    }
    public static String getSexOrEmail(boolean b,int num,String desc){
        if (b) {
            while (true) {
                String info=(String)getMustInput(desc);
                if (num==1) {//num==1代表获取sex
                    if (info.equals("man") || info.equals("woman")) {
                        return info;
                    }else{
                        continue;
                    }                  
                }
                if (num==2) {
                    if (!info.contains("@")) {
                        System.out.print("邮箱格式不正确,请重新输入");
                        continue;
                    }
                    return info;
                }
            }
        } else {
            while (true) {
                Object infos=getInput();
                if (infos==null) {
                    return null;    
                }
                String info=(String)infos;
                if (num==1) {//num==1代表获取sex
                    if (info.equals("man") || info.equals("woman")) {
                        return info;
                    }  else{
                        System.out.print("请输入性别(man/woman):");
                        continue;
                    }                
                }
                if (num==2) {
                    if (!info.contains("@")) {
                        System.out.print("邮箱格式不正确,");
                        continue;
                    }else{
                        return info;
                    }
                }
            }
        }
    }

    public static Object getInput(int...arr) {
        String line;
        while (true) {
            line=scan.nextLine();
            if (line.length()==0) {
                return null;
            } else {
                if (arr.length==1) {
                    if (arr[0]==1) {
                        try {
                            int result=Integer.valueOf(line);
                            return result;
                        } catch (Exception e) {
                            System.out.print("输入的不是一个数字,请重新输入:");
                            continue;
                        }
                    }else{
                        if (line.startsWith("1") && line.length()==11) {
                            try {
                                Long result=Long.valueOf(line);
                                return result;
                            } catch (Exception e) {
                                System.out.print("输入的不是手机号,请重新输入:");
                                continue;
                            }
                        }else{
                            System.out.print("输入的不是手机号,请重新输入:");
                            continue;
                        }
                    }
                }else{
                    return line;
                }
            }
        }
    }
}

客户类
public class Customer {
    private String name;
    private String sex;
    private int age;
    private long phone;
    private String email;

    public Customer(String name, String sex, int age, long phone, String email) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.phone = phone;
        this.email = email;
    }

    @Override
    public String toString() {
        return name + "\t" + sex + "\t" + age + "\t\t" + phone + "\t" + email;
    }

    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 int getAge() {
        return age;
    }

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

    public long getPhone() {
        return phone;
    }

    public void setPhone(long phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    
}
客户列表类,实现对客户的增删改查
public class CustomerList { 
    public Customer[] cust=new Customer[20];
    private int total=0;
    private final static CustomerList custList=new CustomerList();
    private CustomerList(){
        cust[total++]=new Customer("wang", "man", 20, 13888888888L, "11@qq.com");
    }

    public static CustomerList getList() {
        return custList;
    }
    public boolean addCustomer(Customer c) {
        if (total>20 || c==null) {
            return false;
        }
        cust[total++]=c;
        return true;
    }

    public boolean delCustomer(int index) {
        if (index >= total || index<0) {
            return false;
        }
        for (int i = index; i < total; i++) {
            cust[i]=cust[i+1];
        }
        total--;
        return true;
    }

    public Customer[] getCustomers() {
        if (total==0) {
            return null;
        }
        Customer[] custs=new Customer[total];
        for (int i = 0; i < total; i++) {
            custs[i]=cust[i];
        }
        return custs;
    }

    public boolean modifyCustomer(int index,Customer c) {
        if (index >= total || index<0 || c == null) {
            return false;
        }
        cust[index]=c;
        return true;
    }

    public Customer getCustomer(int index) {
        if (index >= total || index<0) {
            return null;
        }
        return cust[index];
    }
}

显示类,实现终端界面的显示
public class CustomerView {
    private final CustomerList list=CustomerList.getList();
    
    private void showMenu(){
        while (true) {
            System.out.println("\n-----------------客户信息管理软件-----------------\n");
            System.out.println("                   1 添 加 客 户");
            System.out.println("                   2 修 改 客 户");
            System.out.println("                   3 删 除 客 户");
            System.out.println("                   4 客 户 列 表");
            System.out.println("                   0 退       出\n");
            // System.out.print("                   请选择(0-4):");
            int num=ScanUtil.getMenuNum();
            switch (num) {
                case 0:
                    return;
                case 1:
                    add();
                    break;
                case 2:
                    modify();
                    break;
                case 3:
                    del();
                    break;
                case 4:
                    showCustomers();
                    break;
                default:
                    break;
            }
        }
    }

    public CustomerView() {
        showMenu();
    }

    public void showCustomers() {
        System.out.println("---------------------------客户列表---------------------------");
        Customer[] custs=list.getCustomers();
        if (custs==null) {
            System.out.println("客户列表是空的!");
            return;
        }
        System.out.println("编号\t姓名\t性别\t年龄\t\t电话\t\t邮箱");
        for (int i = 0; i < custs.length; i++) {
            System.out.println(i+1+"\t"+custs[i]);
        }
    }

    public void del() {
        System.out.println("---------------------删除客户---------------------");
        int index=(Integer)ScanUtil.getMustInput("编号",1);
        if (list.delCustomer(index-1)) {
            System.out.println("删除成功!");
        }else{
            System.out.println("指定的编号不存在,删除错误!");
        }
    }

    public void add() {
        System.out.println("---------------------添加客户---------------------");
        String sex = ScanUtil.getSexOrEmail(true,1,"性别(man/woman)");
        Object name = ScanUtil.getMustInput("客户姓名");
        Object age = ScanUtil.getMustInput("年龄",1);
        long phone = ScanUtil.getPhoneNum();
        String email = ScanUtil.getSexOrEmail(true, 2,"邮箱");
        Customer c=new Customer((String)name, sex, (Integer)age, phone, email);
        if (list.addCustomer(c)){
            System.out.println("添加成功!");
        }else{
            System.out.println("数据库已满,无法添加!");
        }
    }

    public void modify() {
        System.out.println("---------------------修改客户---------------------");
        // System.out.print("请输入编号:");
        Object index;
        Customer cust;
        while (true) {
            index = ScanUtil.getMustInput("编号",1);
            if ((cust=list.getCustomer((Integer)index-1))==null){
                System.out.print("输入的编号找不到,请重新输入:");
            }else{
                break;
            }
        }
        int count=0;
        System.out.print("原年龄"+cust.getAge()+",");
        Object age = ScanUtil.getInput(1);
        if (age!=null) {
            cust.setAge((Integer)age);
        }else{
            count++;
        }
        System.out.print("原姓名"+cust.getName()+",");
        Object name = ScanUtil.getInput();
        if (name!=null) {
            cust.setName((String)name);
        } else {
            count++;
        }
        System.out.print("原性别"+cust.getSex()+",");
        String sex = ScanUtil.getSexOrEmail(false, 1,"性别");
        if (sex!=null) {
            cust.setSex(sex);
        } else {
            count++;
        }
        System.out.print("原邮箱"+cust.getEmail()+",");
        String email = ScanUtil.getSexOrEmail(false, 2,"邮箱");
        if (email!=null) {
            cust.setEmail(email);
        } else {
            count++;
        }
        System.out.print("原手机号"+cust.getPhone()+",");
        Object phoneNum = ScanUtil.getPhone();
        if (phoneNum!=null) {
            cust.setPhone((Long)phoneNum);
        }else{
            count++;
        }
        if (count>4) {
            System.out.println("你没有修改任何属性,修改不成功!");
        } else {
            list.modifyCustomer((Integer)index, cust);
            System.out.println("修改成功!");
        }        
    }
}
程序入口
public class App {
    public static void main(String[] args) {
        new CustomerView();
    }
}

基本能跑起来的,但还是有些小bug,例如手机号输入获取等可以优化一下的,想着第二天有时间改一下。第二天,第三天过去了……想着不改也能用,不浪费时间了,然后就放下了。这样的习惯,能学好一门语言吗???
2021.2.4 今天debug了一下,但还能继续优化,看时间吧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值