客户信息管理软件

发布时间 2023-03-26 15:52:38作者: 忆梦寻尘

客户信息管理软件


CMUtility

        import java.util.Scanner;

        public class CMUtility {
            private static Scanner scanner = new Scanner(System.in);

            public static char readMenuSelection() {
                char c;
                for (; ; ) {
                    String str = readKeyBoard(1, false);
                    c = str.charAt(0);
                    if (c != '1' && c != '2' && 
                        c != '3' && c != '4' && c != '5') {
                        System.out.print("选择错误,请重新输入:");
                    } else break;
                }
                return c;
            }

            public static char readChar() {
                String str = readKeyBoard(1, false);
                return str.charAt(0);
            }

            public static char readChar(char defaultValue) {
                String str = readKeyBoard(1, true);
                return (str.length() == 0) ? defaultValue : str.charAt(0);
            }

            public static int readInt() {
                int n;
                for (; ; ) {
                    String str = readKeyBoard(2, false);
                    try {
                        n = Integer.parseInt(str);
                        break;
                    } catch (NumberFormatException e) {
                        System.out.print("数字输入错误,请重新输入:");
                    }
                }
                return n;
            }

            public static int readInt(int defaultValue) {
                int n;
                for (; ; ) {
                    String str = readKeyBoard(2, true);
                    if (str.equals("")) {
                        return defaultValue;
                    }

                    try {
                        n = Integer.parseInt(str);
                        break;
                    } catch (NumberFormatException e) {
                        System.out.print("数字输入错误,请重新输入:");
                    }
                }
                return n;
            }

            public static String readString(int limit) {
                return readKeyBoard(limit, false);
            }

            public static String readString(int limit, String defaultValue) {
                String str = readKeyBoard(limit, true);
                return str.equals("")? defaultValue : str;
            }

            public static char readConfirmSelection() {
                char c;
                for (; ; ) {
                    String str = readKeyBoard(1, false).toUpperCase();
                    c = str.charAt(0);
                    if (c == 'Y' || c == 'N') {
                        break;
                    } else {
                        System.out.print("选择错误,请重新输入:");
                    }
                }
                return c;
            }

            private static String readKeyBoard(int limit, boolean blankReturn) {
                String line = "";

                while (scanner.hasNextLine()) {
                    line = scanner.nextLine();
                    if (line.length() == 0) {
                        if (blankReturn) return line;
                        else continue;
                    }

                    if (line.length() < 1 || line.length() > limit) {
                        System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
                        continue;
                    }
                    break;
                }

                return line;
            }
        }

Customer

        /**
         * 客户类
         */
        public class Customer {
            private String name;
            private char gender;
            private int age;
            private String phone;
            private String 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 String getDetails() {
                return name + "\t\t" + gender + "\t\t" + age + "\t\t" + phone + "\t\t" + email;
            }
        }

CustomerList

        /**
         * Customer的管理模块
         */
        public class CustomerList {
            private Customer[] customers;//保存Customer对象
            private int total = 0;//有效客户的数量

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

            /**
             * 添加客户
             * @param customer
             * @return
             */
            public boolean addCustomer(Customer customer) {
                if (total >= customers.length) {
                    return false;//数组已满
                }
                customers[total] = customer;
                total++;
                return true;//添加成功
            }

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

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

            /**
             * 返回有效元素的客户
             * @return
             */
            public Customer[] getAllCustomer() {
                Customer[] cuts = new Customer[total];
                for (int i = 0; i < cuts.length; i++) {
                    cuts[i] = customers[i];
                }
                return cuts;
            }

            /**
             * 返回客户对象的索引位置
             * @param index
             * @return
             */
            public Customer getCustomer(int index) {
                return customers[index];
            }

            public static void main(String[] args) {
                CustomerList cl = new CustomerList(5);
                Customer customer = new Customer("张三", '男', 18, "010-1234567", "zhangsan@abc.com");
                boolean flag = cl.addCustomer(customer);
                if (flag) {
                    System.out.println("添加成功!");
                } else {
                    System.out.println("添加失败!");
                }

                Customer[] allCustomer = cl.getAllCustomer();
                for (Customer cust : allCustomer) {
                    System.out.println(cust.getDetails());
                }

            }
        }

CustomerView

        /**
         * 与用户进行交互
         */
        public class CustomerView {
            private CustomerList customerList = new CustomerList(10);

            public void enterMainMenu() {
                boolean loopFlag = true;
                do {
                    System.out.println("\n-----------------客户信息管理软件-----------------\n");
                    System.out.println("\t\t\t\t 1 添加客户");
                    System.out.println("\t\t\t\t 2 修改客户");
                    System.out.println("\t\t\t\t 3 删除客户");
                    System.out.println("\t\t\t\t 4 客户列表");
                    System.out.println("\t\t\t\t 5 退出");
                    System.out.println();
                    System.out.print("\t\t\t\t 请选择(1-5):");

                    char ch = CMUtility.readMenuSelection();
                    switch (ch) {
                        case '1':
                            //添加客户
                            addNewCustomer();
                            break;
                        case '2':
                            //修改客户
                            modifyCustomer();
                            break;
                        case '3':
                            //删除客户
                            deleteCustomer();
                            break;
                        case '4':
                            //客户列表
                            listAllCustomer();
                            break;
                        case '5':
                            //退出
                            System.out.print("是否退出(y/n):");
                            char c = CMUtility.readConfirmSelection();
                            if (c == 'Y') {
                                loopFlag = false;
                            }
                            break;
                    }
                } while (loopFlag);
            }

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

                System.out.print("性别:");
                char gender = CMUtility.readChar();

                System.out.print("年龄:");
                int age = CMUtility.readInt();

                System.out.print("电话:");
                String phone = CMUtility.readString(20);

                System.out.print("邮箱:");
                String emile = CMUtility.readString(20);

                //将散列的数据装进对象
                Customer customer = new Customer(name, gender, age, phone, emile);
                //将对象添加到数组中
                boolean flag = customerList.addCustomer(customer);
                if (flag) {
                    System.out.println("---------------------添加完成---------------------");
                } else {
                    System.out.println("---------------------添加失败---------------------");
                }
            }

            /**
             * 修改
             */
            private void modifyCustomer() {
                System.out.println("\n---------------------修改客户---------------------\n");
                Customer customer = null;
                int num = 0;

                while (true) {
                    System.out.print("请选择待修改客户编号(-1退出):");
                    num = CMUtility.readInt();
                    if (num == -1) {
                        return;//结束当前方法
                    }
                    customer = customerList.getCustomer(num - 1);
                    if (customer == null) {
                        System.out.println("无法找到指定客户!");
                    } else {
                        break;//结束这个死循环
                    }
                }
                //修改操作
                System.out.print("姓名(" + customer.getName() + "):");
                String name = CMUtility.readString(20, customer.getName());

                System.out.print("性别(" + customer.getGender() + "):");
                char gender = CMUtility.readChar(customer.getGender());

                System.out.print("年龄(" + customer.getAge() + "):");
                int age = CMUtility.readInt(customer.getAge());

                System.out.print("电话(" + customer.getPhone() + "):");
                String phone = CMUtility.readString(20, customer.getPhone());

                System.out.print("邮箱(" + customer.getEmail() + "):");
                String emile = CMUtility.readString(20, customer.getEmail());

                Customer cust = new Customer(name, gender, age, phone, emile);
                boolean flag = customerList.replaceCustomer(num - 1, cust);
                if (flag) {
                    System.out.println("---------------------修改完成---------------------");
                } else {
                    System.out.println("---------------------修改失败---------------------");
                }
            }

            /**
             * 删除
             */
            private void deleteCustomer() {
                System.out.println("\n---------------------删除客户---------------------\n");
                int num = 0;
                while (true) {
                    System.out.print("请选择待删除客户编号(-1退出):");
                    num = CMUtility.readInt();
                    if (num == -1) {
                        return;//结束当前方法
                    }
                    Customer cust = customerList.getCustomer(num - 1);
                    if (cust == null) {
                        System.out.println("无法找到客户信息!");
                    } else {
                        break;//结束这个死循环
                    }
                }
                //删除操作
                System.out.print("确认是否删除(Y/N):");
                char ch = CMUtility.readConfirmSelection();
                if (ch == 'N') {
                    return;//结束方法别删了
                }
                boolean flag = customerList.deleteCustomer(num - 1);
                if (flag) {
                    System.out.println("---------------------删除成功---------------------");
                } else {
                    System.out.println("---------------------删除失败---------------------");
                }
            }

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

                Customer[] allCustomer = customerList.getAllCustomer();
                if (allCustomer.length == 0) {
                    System.out.println("无法找到信息!");
                } else {
                    System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
                }
                for (int i = 0; i < allCustomer.length; i++) {
                    System.out.println((i + 1) + "\t" + allCustomer[i].getDetails());
                }
            }

            /**
             * 程序的入口
             *
             * @param args
             */
            public static void main(String[] args) {
                CustomerView cv = new CustomerView();
                cv.enterMainMenu();
            }
        }