拟实现一个基于文本界面的《客户信息管理软件》
进一步掌握编程技巧和调试技巧,熟悉面向对象编程
主要涉及以下知识点:
▶类结构的使用:
▶对象的创建与使用
▶类的封装性
▶声明和使用数组
▶数组的插入、删除和替换
▶关键字的使用:this
import java.util.*;
/**
* CMUtility工具类:
* 将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
*/
public class CMUtility {private static Scanner scanner = new Scanner(System.in);/**用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。*/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(2, false);return str.charAt(0);}/**从键盘读取一个字符,并将其作为方法的返回值。如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。*/public static char readChar(char defaultValue) {String str = readKeyBoard(2, true);return (str.length() == 0) ? defaultValue : str.charAt(0);}/**从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。*/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;}/**从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。*/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;}/**从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。*/public static String readString(int limit) {return readKeyBoard(limit, false);}/**从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。*/public static String readString(int limit, String defaultValue) {String str = readKeyBoard(limit, true);return str.equals("")? defaultValue : str;}/**用于确认选择的输入。该方法从键盘读取‘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;}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;}
}
public class Customer {private String name;private char gender = '男';private int age;private String phone;private String email;public void setName(String name) {this.name = name;}public String getName() {return name;}public void setGender(char gender) {this.gender = gender;}public char getGender() {return gender;}public void setAge(int age) {this.age = age;}public int getAge() {return age;}public void setPhone(String phone) {this.phone = phone;}public String getPhone() {return phone;}public void setEmail(String email) {this.email = email;}public String getEmail() {return email;}//测试public static void main(String[] args) {Customer a = new Customer();a.setAge(28);a.setEmail("3466@qq.com");a.setGender('女');a.setName("张三");a.setPhone("10086");System.out.println(a.age+" "+a.email+" "+a.gender+" "+a.name+" "+a.phone);}}
public class CustomerList {private int total = 0;private Customer[] customers;private int totalCustomer;public int getTotalCustomer() {return totalCustomer;}/*** 用于初始化用户信息数组* @param totalCustomer */public CustomerList(int totalCustomer) {this.totalCustomer = totalCustomer;customers = new Customer[totalCustomer];}/*** 用于增加新的客户* @param customer 新增客户的信息* @return 提示增加客户是否成功*/public boolean addCustomer(Customer customer) {if(total < customers.length) {customers[total] = customer;this.total++;return true;}return false;}/*** 用途修改指定客户信息* @param index 客户索引* @param cust 用于替换的客户信息* @return 提示替换是否成功*/public boolean replaceCustomer(int index, Customer cust) {if(index < 0||index >= total) return false;if(customers[index] == null) return false;else{customers[index].setAge(cust.getAge() == -1?customers[index].getAge():cust.getAge());customers[index].setEmail(cust.getEmail() == null?customers[index].getEmail():cust.getEmail());customers[index].setGender(cust.getGender() == ' '?customers[index].getGender():cust.getGender());customers[index].setName(cust.getName() == null?customers[index].getName():cust.getName());customers[index].setPhone(cust.getPhone() == null?customers[index].getPhone():cust.getPhone());}return true;}/*** 用于删除指定客户* @param index 客户索引* @return 提示删除是否成功*/public boolean deleteCustomer(int index) {if(index < 0||index >= total) return false;if(customers[index] == null) return false;else {for(int i = index; i < total; i++) {customers[i] = customers[i+1];}}total--;return true;}/*** 用于获取全部客户* @return 返回所有客户列表*/public Customer[] getAllCustomer() {Customer[] allCustomer = new Customer[total];for(int i = 0; i < total; i++) { allCustomer[i] = customers[i];}return allCustomer;}/*** 获取指定客户* @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 static void main(String[] args) {CustomerList customerList = new CustomerList(5);Customer a = new Customer();Customer b = new Customer();Customer c = new Customer();Customer d = new Customer();//Customer e = new Customer();//Customer f = new Customer();a.setAge(28);a.setEmail("3466@qq.com");a.setGender('F');a.setName("张三");a.setPhone("10086");b.setAge(22);b.setEmail("12345@qq.com");b.setGender('M');b.setName("李四");b.setPhone("10001");boolean test1 = customerList.replaceCustomer(0, a);boolean test2 = customerList.deleteCustomer(0);boolean test3 = customerList.replaceCustomer(-1, a);boolean test4 = customerList.deleteCustomer(-1);Customer test5 = customerList.getCustomer(-1);boolean test6 = customerList.addCustomer(b);System.out.println(customerList.total);boolean test7 = customerList.deleteCustomer(3);boolean test8 = customerList.deleteCustomer(0);boolean t1 = customerList.addCustomer(a);boolean t2 = customerList.addCustomer(b);boolean t3 = customerList.addCustomer(c);boolean t4 = customerList.addCustomer(d);boolean t5 = customerList.deleteCustomer(2);boolean t6 = customerList.deleteCustomer(0);System.out.println(test1+" "+ test2+" "+ test3+" "+ test4+" "+ test5+" "+test6+" "+ test7+" "+test8);System.out.println(t1+" "+t2+" "+t3+" "+t4+" "+t5+" "+t6+" ");Customer[] allCustomer = new Customer[0];allCustomer = customerList.getAllCustomer();System.out.println(allCustomer.length);System.out.println(allCustomer[0].getName());}}
public class CustomerView {CustomerList customerList = new CustomerList(10);//public void enterMainMenu() {CustomerView customerView = new CustomerView();abc: while (true) {try {Thread.sleep(300);System.out.println("--------------客户信息管理软件---------------");System.out.println("\n\t\t 1 添 加 用 户");System.out.println("\t\t 2 修 改 用 户");System.out.println("\t\t 3 删 除 客 户");System.out.println("\t\t 4 客 户 列 表");System.out.println("\t\t 5 退 出");System.out.println("\n\t\t 请选择 (1-5):__");char num = CMUtility.readMenuSelection();switch (num) {case '1':customerView.addNewCustomer();break;case '2':customerView.modifyCustomer();break;case '3':customerView.deleteCustomer();break;case '4':customerView.listAllCustomers();break;case '5':System.out.println("正在退出...");break abc;default:break;}} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("已退出,祝你生活愉快");}/*** 添加客户*/private void addNewCustomer() {if (customerList.getTotal() >= customerList.getTotalCustomer()) {System.out.println("客户数量已满,无法添加,即将返回主菜单...");return;}System.out.println("-----------------添加客户------------------");Customer newCust = new Customer();System.out.print("姓名:");newCust.setName(CMUtility.readString(10));// System.out.println();System.out.print("性别:");newCust.setGender(CMUtility.readChar());// System.out.println();System.out.print("年龄:");newCust.setAge(CMUtility.readInt());// System.out.println();System.out.print("电话:");newCust.setPhone(CMUtility.readString(15));// System.out.println();System.out.print("邮箱:");while (true) {String str = CMUtility.readString(20);String emailMatcher = "[0-9A-Za-z]+@[0-9A-Za-z]+\\.[0-9A-Za-z]+";if(!str.matches(emailMatcher)) {System.out.println("邮箱不合法,请重新输入");continue;}newCust.setEmail(str);break;}// System.out.println();if (customerList.addCustomer(newCust)) {System.out.println("-----------------添加成功------------------");System.out.println("正在返回主菜单...");};}/*** 修改客户*/private void modifyCustomer() {if (customerList.getTotal() == 0) {System.out.println("当前无可修改的客户信息,即将返回主菜单...");return;}System.out.println("-----------------修改客户------------------");Customer cust = new Customer();int index = 0;for (;;) {System.out.println("请选择待修改客户编号(-1退出):");index = CMUtility.readInt();if (index == -1) {System.out.println("正在返回...");return;}if (index >= customerList.getTotal()) {System.out.println("无该编号的客户");continue;}break;}Customer oldcust = customerList.getCustomer(index);System.out.println("请在后方修改,直接回车则保持原状。");System.out.print("姓名(" + oldcust.getName() + "):");cust.setName(CMUtility.readString(20, null));System.out.print("性别(" + oldcust.getGender() + "):");cust.setGender(CMUtility.readChar(' '));System.out.print("年龄(" + oldcust.getAge() + "):");cust.setAge(CMUtility.readInt(-1));System.out.print("电话(" + oldcust.getPhone() + "):");cust.setPhone(CMUtility.readString(15, null));System.out.print("邮箱(" + oldcust.getEmail() + "):");while (true) {String str = CMUtility.readString(20,null);if(str == null) {cust.setEmail(null);break;}String emailMatcher = "[0-9A-Za-z]+@[0-9A-Za-z]+\\.[0-9A-Za-z]+";if(!str.matches(emailMatcher)) {System.out.println("邮箱不合法,请重新输入:");continue;}cust.setEmail(str);}if (customerList.replaceCustomer(index, cust)) {System.out.println("-----------修改结束-------------");System.out.println("正在返回主菜单...");};}/*** 删除客户*/private void deleteCustomer() {if(customerList.getTotal() == 0) {System.out.println("当前无可操作的客户,即将返回主菜单...");return;}System.out.println("-----------------删除客户------------------");System.out.println("请选择待删除客户编号(-1退出):");int index = 0;while (true) {index = CMUtility.readInt();if (index == -1) {System.out.println("正在返回...");return;} else if (index < 0 || index >= customerList.getTotal()) {System.out.println("该用户不存在,请重新输入...");continue;}break;}System.out.println("确认是否删除客户“"+customerList.getCustomer(index).getName()+"”(Y/N):");switch (CMUtility.readConfirmSelection()) {case 'Y':customerList.deleteCustomer(index);System.out.println("-----------------删除成功------------------");break;case 'N':System.out.println("-----------------取消操作------------------");break;}System.out.println("正在返回主菜单...");}/*** 展示所有客户信息*/private void listAllCustomers() {System.out.println("-----------------客户列表------------------");Customer[] allCustomer;allCustomer = customerList.getAllCustomer();System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");for (int i = 0; i < customerList.getTotal(); i++) {System.out.println(i + "\t" + allCustomer[i].getName() + "\t" + allCustomer[i].getGender() + "\t"+ allCustomer[i].getAge() + "\t" + allCustomer[i].getPhone() + "\t" + allCustomer[i].getEmail());}CMUtility.readChar(' ');System.out.println("----------------客户列表完成-----------------");System.out.println("正在返回主菜单...");}//程序入口public static void main(String[] args) {CustomerView customerView = new CustomerView();customerView.enterMainMenu();}
}