实际运行效果
添加客户:
显示客户列表:
修改用户:
删除用户:
实际代码
编程思路:
文件分层
代码实现
Customer.java
package crm.domain; /** * 数据层/domain,javabean,pojo */ public class Customer { // 编号 姓名 性别 年龄 电话 邮箱 private int id; private String name; private char sex; private int age; private String phone; private String email; public Customer(int id, String name, char sex, int age, String phone, String email) { super(); this.id = id; this.name = name; this.sex = sex; this.age = age; this.phone = phone; this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Customer() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return id + "\t" + name + "\t" + sex + "\t" + age + "\t" + phone + "\t" + email ; } }
CustomerService.java
package crm.service; import crm.domain.Customer; import crm.utils.CMUtility; /** * 业务处理层 */ public class CustomerService { //将所有对象保存在一个数组中,customers[] Customer[] customers; int customerNum = 1; //用于记录,目前有几个人在数组中 int customerId = 1 ; //用于标记添加用户的编号。 public CustomerService(int len) { //构造器 传入默认数组长度,即人数 customers = new Customer[len]; //先默认生成一个人,用于测试 customers[0] = new Customer(1, "小范", '男', 20, "1112", "xiaofan@qq.com"); } //返回数组列表 public Customer[] list() { return customers; } //添加用户,加入数组扩容的功能 public boolean addCustomer(Customer customer) { //添加进customers数组中 if(customerNum >= customers.length) { //已存在人数小于数组长度,可以添加 System.out.println("数组已满.."); //这里数组扩容 Customer[] temp = new Customer[customers.length + 1]; for (int i = 0; i < customers.length; i++) { temp[i] = customers[i]; } customer.setId(++customerId); //此用户的id 为 以前的id+1 temp[customers.length] = customer; customers = temp; return true; } customer.setId(++customerId); //此用户的id 为 以前的id+1 customers[customerNum++] = customer; // 每添加一个用户,让数组内人数++ return true; } //删除用户,指定id public boolean delCustomer(int id) { int index = -1 ; //保存要删除id的下标 for (int i = 0; i < customerNum; i++) { if(customers[i].getId() == id) { index = i ; } } if(index == -1) { return false; }else { for(int i = index;i < customerNum;i++) { //将要删除客户后面的客户依次前移 customers[i] = customers[i + 1]; } //将customerNum所在的客户置空,因为已经前移了 customers[customerNum--] = null; //同时要减少数组中的人数 return true; } } //修改用户信息,将新对象赋值给原来的位置 public boolean changeCustomer(int id,Customer customer) { int index = -1 ; //保存要赋值id的下标 for (int i = 0; i < customerNum; i++) { if(customers[i].getId() == id) { index = i ; } } if(index == -1) { return false; }else { customers[index] = customer; return true; } } //判断用户是否存在,返回查找到的用户 public Customer customerIsIn(int id) { int index = -1 ; for (int i = 0; i < customerNum; i++) { if(customers[i].getId() == id) { index = i ; } } if(index == -1) { return null; }else { return customers[index]; } } }
CustomerView.java
package crm.view; /** * 界面层 */ import crm.domain.Customer; import crm.service.CustomerService; import crm.utils.CMUtility; public class CustomerView { //创建对象,调用Service功能 private CustomerService customerService = new CustomerService(2); public static void main(String[] args) { new CustomerView().showMenu(); } //打印菜单 public void showMenu(){ boolean flag = true; do { /* * -----------------客户信息管理软件----------------- 1 添 加 客 户 2 修 改 客 户 3 删 除 客 户 4 客 户 列 表 5 退 出 请选择(1-5):_ */ System.out.println("\n===================客户信息管理软件==================="); System.out.println("\t\t1 添 加 客 户"); System.out.println("\t\t2 修 改 客 户"); System.out.println("\t\t3 删 除 客 户"); System.out.println("\t\t4 客 户 列 表"); System.out.println("\t\t5 退 出"); System.out.print("请选择(1-5):"); char choice = CMUtility.readMenuSelection(); switch(choice) { case '1': add(); break; case '2': change(); break; case '3': del(); break; case '4': showList(); break; case '5': System.out.println("退 出"); flag = false; break; } } while (flag); } //显示客户列表,即输出所有客户信息 public void showList() { Customer[] customers = customerService.list(); /* * ---------------------------客户列表--------------------------- 编号 姓名 性别 年龄 电话 邮箱 1 张三 男 30 010-56253825 abc@email.com 2 李四 女 23 010-56253825 lisi@ibm.com 3 王芳 女 26 010-56253825 wang@163.com -------------------------客户列表完成------------------------- */ System.out.println("=======================客户列表======================="); System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱"); for (int i = 0; i < customers.length; i++) { if (customers[i] == null) { break; } System.out.println(customers[i]); } System.out.println("======================客户列表完成====================="); } //添加客户 public void add() { /* * ---------------------添加客户--------------------- 姓名:张三 性别:男 年龄:30 电话:010-56253825 邮箱:zhang@abc.com ---------------------添加完成--------------------- */ System.out.println("=======================添加客户======================="); System.out.print("姓名:"); String name = CMUtility.readString(10); System.out.print("性别:"); char sex = CMUtility.readChar(); System.out.print("年龄:"); int age = CMUtility.readInt(); System.out.print("电话:"); String phone = CMUtility.readString(12); System.out.print("邮箱:"); String email = CMUtility.readString(20); Customer customer = new Customer(0, name, sex, age, phone, email); if(customerService.addCustomer(customer)) { System.out.println("=======================添加成功======================="); }else { System.out.println("=======================添加失败======================="); } } //删除用户 public void del() { /* * ---------------------删除客户--------------------- 请选择待删除客户编号(-1退出):1 确认是否删除(Y/N):y ---------------------删除完成--------------------- */ System.out.println("\n=======================删除客户======================="); System.out.print("请选择待删除客户编号(-1退出):"); int id = CMUtility.readInt(); System.out.print("确认是否删除(Y/N):"); char flag = CMUtility.readConfirmSelection(); if(flag == 'Y') { if(customerService.delCustomer(id)) { System.out.println("=======================删除成功======================="); }else { System.out.println("=======================删除失败======================="); } }else { System.out.println("=======================取消删除======================="); } } //修改客户 public void change() { /* * ---------------------修改客户--------------------- 请选择待修改客户编号(-1退出):1 姓名(张三):<直接回车表示不修改> 性别(男): 年龄(30): 电话(010-56253825): 邮箱(zhang@abc.com):zsan@abc.com ---------------------修改完成--------------------- */ System.out.println("\n=======================修改客户======================="); System.out.print("请选择待修改客户编号(-1退出):"); int id = CMUtility.readInt(); Customer findCustomer = customerService.customerIsIn(id);//保存返回的客户对象 if(findCustomer == null) { //如果id不存在直接提示输入错误,修改失败 System.out.println("=================修改失败,输入id错误================="); }else { //id存在 System.out.print("姓名("+ findCustomer.getName() +"):<直接回车表示不修改>"); String name = CMUtility.readString(10, findCustomer.getName()); System.out.print("性别("+ findCustomer.getSex() +"):"); char sex = CMUtility.readChar(findCustomer.getSex()); System.out.print("年龄("+ findCustomer.getAge() +"):"); int age = CMUtility.readInt(findCustomer.getAge()); System.out.print("电话("+ findCustomer.getPhone() +"):"); String phone = CMUtility.readString(12,findCustomer.getPhone()); System.out.print("邮箱("+ findCustomer.getEmail() +"):"); String email = CMUtility.readString(20,findCustomer.getEmail()); Customer customer = new Customer(id, name, sex, age, phone, email); if(customerService.changeCustomer(id, customer)) { System.out.println("=======================修改成功======================="); }else { System.out.println("=======================修改失败======================="); } } } }
CMUility.java(工具包)
package crm.utils; /** 工具类的作用: 处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。 */ import java.util.*; /** */ public class CMUtility { //静态属性。。。 private static Scanner scanner = new Scanner(System.in); /** * 功能:读取键盘输入的一个菜单选项,值:1——5的范围 * @return 1——5 */ public static char readMenuSelection() { char c; for (; ; ) { String str = readKeyBoard(1, false);//包含一个字符的字符串 c = str.charAt(0);//将字符串转换成字符char类型 if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5') { System.out.print("选择错误,请重新输入:"); } else break; } return c; } /** * 功能:读取键盘输入的一个字符 * @return 一个字符 */ public static char readChar() { String str = readKeyBoard(1, false);//就是一个字符 return str.charAt(0); } /** * 功能:读取键盘输入的一个字符,如果直接按回车,则返回指定的默认值;否则返回输入的那个字符 * @param defaultValue 指定的默认值 * @return 默认值或输入的字符 */ public static char readChar(char defaultValue) { String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符 return (str.length() == 0) ? defaultValue : str.charAt(0); } /** * 功能:读取键盘输入的整型,长度小于2位 * @return 整数 */ public static int readInt() { int n; for (; ; ) { String str = readKeyBoard(2, false);//一个整数,长度<=2位 try { n = Integer.parseInt(str);//将字符串转换成整数 break; } catch (NumberFormatException e) { System.out.print("数字输入错误,请重新输入:"); } } return n; } /** * 功能:读取键盘输入的 整数或默认值,如果直接回车,则返回默认值,否则返回输入的整数 * @param defaultValue 指定的默认值 * @return 整数或默认值 */ 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; } /** * 功能:读取键盘输入的指定长度的字符串 * @param limit 限制的长度 * @return 指定长度的字符串 */ public static String readString(int limit) { return readKeyBoard(limit, false); } /** * 功能:读取键盘输入的指定长度的字符串或默认值,如果直接回车,返回默认值,否则返回字符串 * @param limit 限制的长度 * @param defaultValue 指定的默认值 * @return 指定长度的字符串 */ public static String readString(int limit, String defaultValue) { String str = readKeyBoard(limit, true); return str.equals("")? defaultValue : str; } /** * 功能:读取键盘输入的确认选项,Y或N * * @return Y或N */ 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; } /** * 功能: 读取一个字符串 * @param limit 读取的长度 * @param blankReturn 如果为true ,表示 可以读空字符串。 * 如果为false表示 不能读空字符串。 * * 如果输入为空,或者输入大于limit的长度,就会提示重新输入。 * @return */ private static String readKeyBoard(int limit, boolean blankReturn) { //定义了字符串 String line = ""; //scanner.hasNextLine() 判断有没有下一行 while (scanner.hasNextLine()) { line = scanner.nextLine();//读取这一行 //如果line.length=0, 即用户没有输入任何内容,直接回车 if (line.length() == 0) { if (blankReturn) return line;//如果blankReturn=true,可以返回空串 else continue; //如果blankReturn=false,不接受空串,必须输入内容 } //如果用户输入的内容大于了 limit,就提示重写输入 //如果用户如的内容 >0 <= limit ,我就接受 if (line.length() < 1 || line.length() > limit) { System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:"); continue; } break; } return line; } }