java客户信息管理软件

需求说明:

模拟实现基于文本界面的《客户信息管理软件》
该软件能够实现对客户对象的插入、修改和删除(用数组实现),并能够打印客户明细表。

封装类:

public class Customer {
    private String name;//姓名
    private char gender;//性别
    private int age;//年龄
    private String phone;//电话
    private String email;//email

    public Customer() {
    }

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

    public String getName() {
        return name;
    }

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

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

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

    public String getPhone() {
        return phone;
    }

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

    public String getEmail() {
        return email;
    }

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

控制类

public class CustomerList {

    private Customer[] customers;//用来保存客户对象的数组
    private int total = 0;//记录已保存的用户的数量

    /**
     * 初始化数组
     *
     * @param totalCustomer 指定数组的长度
     */
    public CustomerList(int totalCustomer) {
        customers = new Customer[totalCustomer];
    }

    /**
     * 增加客户
     *
     * @param customer
     * @return
     */
    public boolean addCustomer(Customer customer) {
        if (total < customers.length && customer != null) {

            customers[total] = customer;
            total++;
            return true;
        }

        return false;
    }

    /**
     * 修改客户
     *
     * @param index
     * @param cust
     * @return
     */
    public boolean replaceCustomer(int index, Customer cust) {
        if (index < total && index >= 0) {
            customers[index] = cust;
            return true;
        }

        return false;
    }

    /**
     * 删除客户
     *
     * @param index
     * @return
     */
    public boolean deleterCustomer(int index) {
        if (index < 0 && index > total) {
            return false;
        }
        for (int i = index; i < customers.length - 1; i++) {
            customers[i] = customers[i + 1];
        }
        customers[total] = null;
        total--;
        return true;
    }

    /**
     * 获取全部客户
     *
     * @return
     */
    public Customer[] getCustomers() {
        Customer[] customers1 = new Customer[total];
        for (int i = 0; i < total; i++) {
            customers1[i] = customers[i];
        }
        return customers1;
    }

    /**
     * 获取指定客户
     *
     * @param index
     * @return
     */
    public Customer getCustomer(int index) {
        if (index < 0 && index > total) {
            return null;
        }
        return customers[index];
    }

    /**
     * 获取客户总人数
     *
     * @return
     */
    public int getTotal() {
        return total;
    }

}

系统主界面

public class CustomerView {
    private CustomerList customerList = new CustomerList(10);

    public CustomerView() {
        Customer customer = new Customer("佟刚",'男',45,"010-56253825","tong@abc.com");
        customerList.addCustomer(customer);
    }

    /**
     * 功能主界面
     */
    public void enterMainMenu() {
        boolean isFlag = true;
        while (isFlag) {


            System.out.println(" -----------------客户信息管理软件-----------------\n" +
                    "\n" +
                    "                 1 添 加 客 户\n" +
                    "                 2 修 改 客 户\n" +
                    "                 3 删 除 客 户\n" +
                    "                 4 客 户 列 表\n" +
                    "                 5 退      出\n" +
                    "\n" +
                    "                请选择(1-5):_\n");
            char c = CMUtility.readMenuSelection();
            switch (c) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomer();
                    break;
                case '5':
                    System.out.println("确认是否删除(Y/N)");
                    char c1 = CMUtility.readConfirmSelection();
                    if (c1 == 'Y') {
                        isFlag = false;
                    }
            }
        }
    }


    /**
     * 添加客户
     */
    public void addNewCustomer() {
        System.out.println("---------------------添加客户---------------------\n");
        System.out.print("姓名:");
        String s = CMUtility.readString(10);

        System.out.print("性别:");
        char c = CMUtility.readChar();
        System.out.print("年龄:");
        int i = CMUtility.readInt();
        System.out.print("电话:");
        String s1 = CMUtility.readString(13);
        System.out.print("邮箱:");
        String s2 = CMUtility.readString(32);

        Customer customer = new Customer(s, c, i, s1, s2);
        boolean b = customerList.addCustomer(customer);
        if (b){
            System.out.println("---------------------添加完成---------------------\n");
        }else {
            System.out.println("---------------------客户目录已满,添加失败---------------------\n");

        }

    }

    /**
     * 修改客户
     */
    public void modifyCustomer() {
        Customer customer = null;
        int i;
        System.out.println("---------------------修改用户---------------------\n");
        while (true) {
            System.out.println("请输入要修改的用户编号(输入-1退出)");
            i = CMUtility.readInt();
            if (i == -1) {
                return;
            }
            customer = customerList.getCustomer(i - 1);
            if (customer == null) {
                System.out.println("无法找到指定用户");
            }else {
               break;
            }
        }
        System.out.print("客户姓名("+customer.getName()+"):");
        String name = CMUtility.readString(10, customer.getName());
        System.out.println("性别("+customer.getGender()+"):");
        char gender = CMUtility.readChar(customer.getGender());
        System.out.println("年龄("+customer.getAge()+"):");
        int age = CMUtility.readInt(customer.getAge());
        System.out.println("电话("+customer.getPhone()+"):");
        String phone = CMUtility.readString(13, customer.getPhone());
        System.out.println("邮箱("+customer.getEmail()+"):");
        String email = CMUtility.readString(30, customer.getEmail());

        Customer newcustomer = new Customer(name, gender, age, phone, email);
        boolean b = customerList.replaceCustomer(i - 1, newcustomer);
        if (b) {
            System.out.println("---------------------修改完成---------------------\n");
        }else {
            System.out.println("---------------------修改失败---------------------\n");

        }


    }

    /**
     * 删除客户
     */
    public void deleteCustomer() {
        int i ;
        System.out.println("---------------------删除用户---------------------\n");

        while (true){
            System.out.print("请输入要删除的用户编号:");
            i = CMUtility.readInt();
            if (i == -1) {
                return;
            }

            Customer customer = customerList.getCustomer(i - 1);
            if (customer == null) {
                System.out.println("无法找到指定用户");
            }else {
                break;
            }
        }
        System.out.println("确认是否删除?(Y/N)");
        char isdelete = CMUtility.readChar();
        if (isdelete == 'Y') {
            boolean b = customerList.deleterCustomer(i - 1);
            if (b) {
                System.out.println("---------------------删除成功---------------------\n");
            }else {
                System.out.println("---------------------删除失败---------------------\n");
            }
        }
    }

    /**
     * 显示客户列表客户
     */
    public void listAllCustomer() {
        System.out.println("---------------------------客户列表---------------------------\n");

        int total = customerList.getTotal();
        if (total == 0) {
            System.out.println("没有客户记录!");
        }else {
            System.out.println("编号  姓名  性别  年龄   电话          邮箱\n");
            Customer[] customers = customerList.getCustomers();
            for (int i = 0; i < customers.length; i++) {
                System.out.println((i+1)+"\t"+customers[i].getName()+"\t"
                        +customers[i].getGender()+"\t"+customers[i].getAge()+"\t"+customers[i].getPhone()+"\t"
                        +customers[i].getEmail());
            }
        }

        System.out.println("-------------------------客户列表完成-------------------------");
    }

    public static void main(String[] args) {
        CustomerView customerView = new CustomerView();
        customerView.enterMainMenu();
    }

}

工具类(读取键盘输入的值并且判断)

public class CustomerView {
    private CustomerList customerList = new CustomerList(10);

    public CustomerView() {
        Customer customer = new Customer("佟刚",'男',45,"010-56253825","tong@abc.com");
        customerList.addCustomer(customer);
    }

    /**
     * 功能主界面
     */
    public void enterMainMenu() {
        boolean isFlag = true;
        while (isFlag) {


            System.out.println(" -----------------客户信息管理软件-----------------\n" +
                    "\n" +
                    "                 1 添 加 客 户\n" +
                    "                 2 修 改 客 户\n" +
                    "                 3 删 除 客 户\n" +
                    "                 4 客 户 列 表\n" +
                    "                 5 退      出\n" +
                    "\n" +
                    "                请选择(1-5):_\n");
            char c = CMUtility.readMenuSelection();
            switch (c) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomer();
                    break;
                case '5':
                    System.out.println("确认是否删除(Y/N)");
                    char c1 = CMUtility.readConfirmSelection();
                    if (c1 == 'Y') {
                        isFlag = false;
                    }
            }
        }
    }


    /**
     * 添加客户
     */
    public void addNewCustomer() {
        System.out.println("---------------------添加客户---------------------\n");
        System.out.print("姓名:");
        String s = CMUtility.readString(10);

        System.out.print("性别:");
        char c = CMUtility.readChar();
        System.out.print("年龄:");
        int i = CMUtility.readInt();
        System.out.print("电话:");
        String s1 = CMUtility.readString(13);
        System.out.print("邮箱:");
        String s2 = CMUtility.readString(32);

        Customer customer = new Customer(s, c, i, s1, s2);
        boolean b = customerList.addCustomer(customer);
        if (b){
            System.out.println("---------------------添加完成---------------------\n");
        }else {
            System.out.println("---------------------客户目录已满,添加失败---------------------\n");

        }

    }

    /**
     * 修改客户
     */
    public void modifyCustomer() {
        Customer customer = null;
        int i;
        System.out.println("---------------------修改用户---------------------\n");
        while (true) {
            System.out.println("请输入要修改的用户编号(输入-1退出)");
            i = CMUtility.readInt();
            if (i == -1) {
                return;
            }
            customer = customerList.getCustomer(i - 1);
            if (customer == null) {
                System.out.println("无法找到指定用户");
            }else {
               break;
            }
        }
        System.out.print("客户姓名("+customer.getName()+"):");
        String name = CMUtility.readString(10, customer.getName());
        System.out.println("性别("+customer.getGender()+"):");
        char gender = CMUtility.readChar(customer.getGender());
        System.out.println("年龄("+customer.getAge()+"):");
        int age = CMUtility.readInt(customer.getAge());
        System.out.println("电话("+customer.getPhone()+"):");
        String phone = CMUtility.readString(13, customer.getPhone());
        System.out.println("邮箱("+customer.getEmail()+"):");
        String email = CMUtility.readString(30, customer.getEmail());

        Customer newcustomer = new Customer(name, gender, age, phone, email);
        boolean b = customerList.replaceCustomer(i - 1, newcustomer);
        if (b) {
            System.out.println("---------------------修改完成---------------------\n");
        }else {
            System.out.println("---------------------修改失败---------------------\n");

        }


    }

    /**
     * 删除客户
     */
    public void deleteCustomer() {
        int i ;
        System.out.println("---------------------删除用户---------------------\n");

        while (true){
            System.out.print("请输入要删除的用户编号:");
            i = CMUtility.readInt();
            if (i == -1) {
                return;
            }

            Customer customer = customerList.getCustomer(i - 1);
            if (customer == null) {
                System.out.println("无法找到指定用户");
            }else {
                break;
            }
        }
        System.out.println("确认是否删除?(Y/N)");
        char isdelete = CMUtility.readChar();
        if (isdelete == 'Y') {
            boolean b = customerList.deleterCustomer(i - 1);
            if (b) {
                System.out.println("---------------------删除成功---------------------\n");
            }else {
                System.out.println("---------------------删除失败---------------------\n");
            }
        }
    }

    /**
     * 显示客户列表客户
     */
    public void listAllCustomer() {
        System.out.println("---------------------------客户列表---------------------------\n");

        int total = customerList.getTotal();
        if (total == 0) {
            System.out.println("没有客户记录!");
        }else {
            System.out.println("编号  姓名  性别  年龄   电话          邮箱\n");
            Customer[] customers = customerList.getCustomers();
            for (int i = 0; i < customers.length; i++) {
                System.out.println((i+1)+"\t"+customers[i].getName()+"\t"
                        +customers[i].getGender()+"\t"+customers[i].getAge()+"\t"+customers[i].getPhone()+"\t"
                        +customers[i].getEmail());
            }
        }

        System.out.println("-------------------------客户列表完成-------------------------");
    }

    public static void main(String[] args) {
        CustomerView customerView = new CustomerView();
        customerView.enterMainMenu();
    }

}

posted @ 2020-10-24 10:09  MrFugui  阅读(20)  评论(0编辑  收藏  举报