模拟实现基于文本界面的客户信息管理软件,该软件能够实现对客户对象的插入、
修改和删除(用数组实现),并能够打印客户明细表。
项目采用分级菜单方式。主菜单如下:
“添加客户”的界面及操作过程如下所示:
“修改客户”的界面及操作过程如下所示:
“删除客户”的界面及操作过程如下所示:
“客户列表”的界面及操作过程如下所示:
第1步 — Customer类的设计
Customer为实体类,用来封装客户信息
该类封装客户的以下信息:
String name :客户姓名
char gender :性别
int age :年龄
String phone:电话号码
String email :电子邮箱
提供各属性的get/set方法
提供所需的构造器
按照设计要求编写Customer类,并编译
在Customer 类中临时添加一个main方法中,作为单元测试方法。
在方法中创建Customer对象,并调用对象的各个方法,以测试该类是否编写正确。
1 public class Customer { 2 3 private String name; 4 private char gender; 5 private int age; 6 private String phone; 7 private String email; 8 9 public Customer(String name, char gender, int age) { 10 this(name, gender, age, "", ""); 11 } 12 13 public Customer(String name, char gender, int age, String phone, 14 String email) { 15 this.name = name; 16 this.gender = gender; 17 this.age = age; 18 this.phone = phone; 19 this.email = email; 20 } 21 22 public String getName() { 23 return name; 24 } 25 26 public void setName(String name) { 27 this.name = name; 28 } 29 30 public char getGender() { 31 return gender; 32 } 33 34 public void setGender(char gender) { 35 this.gender = gender; 36 } 37 38 public int getAge() { 39 return age; 40 } 41 42 public void setAge(int age) { 43 this.age = age; 44 } 45 46 public String getPhone() { 47 return phone; 48 } 49 50 public void setPhone(String phone) { 51 this.phone = phone; 52 } 53 54 public String getEmail() { 55 return email; 56 } 57 58 public void setEmail(String email) { 59 this.email = email; 60 } 61 62 public String getDetails() { 63 return name + "\t" + gender + "\t" + age + "\t\t" + phone + "\t" + email; 64 } 65 }
第2步 — CustomerList类的设计
CustomerList为Customer对象的管理模块,内部使用数组管理一组Customer对象
本类封装以下信息:
Customer[] customers:用来保存客户对象的数组
int total = 0 :记录已保存客户对象的数量
该类至少提供以下构造器和方法:
public CustomerList(int totalCustomer)
public boolean addCustomer(Customer customer)
public boolean replaceCustomer(int index, Customer cust)
public boolean deleteCustomer(int index)
public Customer[] getAllCustomers()
public Customer getCustomer(int index)
public int getTotal()
public CustomerList(int totalCustomer)
用途:构造器,用来初始化customers数组
参数:totalCustomer:指定customers数组的最大空间
public boolean addCustomer(Customer customer)
用途:将参数customer添加到数组中最后一个客户对象记录之后
参数:customer指定要添加的客户对象
返回:添加成功返回true;false表示数组已满,无法添加
public boolean replaceCustomer(int index, Customer cust)
用途:用参数customer替换数组中由index指定的对象
参数:customer指定替换的新客户对象
index指定所替换对象在数组中的位置,从0开始
返回:替换成功返回true;false表示索引无效,无法替换
public boolean deleteCustomer(int index)
用途:从数组中删除参数index指定索引位置的客户对象记录
参数: index指定所删除对象在数组中的索引位置,从0开始
返回:删除成功返回true;false表示索引无效,无法删除
public Customer[] getAllCustomers()
用途:返回数组中记录的所有客户对象
返回: Customer[] 数组中包含了当前所有客户对象,该数组长度与对象个数相同。
public Customer getCustomer(int index)
用途:返回参数index指定索引位置的客户对象记录
参数: index指定所要获取的客户在数组中的索引位置,从0开始
返回:封装了客户信息的Customer对象
1 public class CustomerList { 2 3 private Customer[] customers; 4 private int total = 0; 5 6 public CustomerList(int totalCustomer) { 7 customers = new Customer[totalCustomer]; 8 } 9 10 public boolean addCustomer(Customer customer) { 11 if (total >= customers.length) return false; 12 13 customers[total++] = customer; 14 return true; 15 } 16 17 public boolean replaceCustomer(int index, Customer cust) { 18 if (index < 0 || index >= total) return false; 19 20 customers[index] = cust; 21 return true; 22 } 23 24 public boolean deleteCustomer(int index) { 25 if (index < 0 || index >= total) return false; 26 27 for (int i = index; i < total - 1; i++) { 28 customers[i] = customers[i + 1]; 29 } 30 31 customers[--total] = null; 32 33 return true; 34 } 35 36 public Customer[] getAllCustomers() { 37 Customer[] custs = new Customer[total]; 38 for (int i = 0; i < total; i++) { 39 custs[i] = customers[i]; 40 } 41 return custs; 42 } 43 44 public int getTotal() { 45 return total; 46 } 47 48 public Customer getCustomer(int index) { 49 if (index < 0 || index >= total) return null; 50 51 return customers[index]; 52 } 53 }
第3步 — CustomerView类的设计
CustomerView为主模块,负责菜单的显示和处理用户操作
本类封装以下信息:
CustomerList customerList = new CustomerList(10);
创建最大包含10个客户对象的CustomerList 对象,供以下各成员方法使用。
该类至少提供以下方法:
public void enterMainMenu()
private void addNewCustomer()
private void modifyCustomer()
private void deleteCustomer()
private void listAllCustomers()
public static void main(String[] args)
public void enterMainMenu()
用途:显示主菜单,响应用户输入,根据用户操作分别调用其他相应的成员方法,
(如addNewCustomer),以完成客户信息处理。
private void addNewCustomer()
private void modifyCustomer()
private void deleteCustomer()
private void listAllCustomers()
用途:这四个方法分别完成“添加客户”、“修改客户”、“删除客户”和“客户列表”等各菜单功能。
这四个方法仅供enterMainMenu()方法调用。
public static void main(String[] args)
用途:创建CustomerView实例,并调用 enterMainMenu()方法以执行程序。
public void enterMainMenu()
用途:显示主菜单,响应用户输入,根据用户操作分别调用其他相应的成员方法,
(如addNewCustomer)以完成客户信息处理。
private void addNewCustomer()
private void modifyCustomer()
private void deleteCustomer()
private void listAllCustomers()
用途:这四个方法分别完成“添加客户”、“修改客户”、“删除客户”和“客户列表”等各菜单功能。
这四个方法仅供enterMainMenu()方法调用。
public static void main(String[] args)
用途:创建CustomerView实例,并调用 enterMainMenu()方法以执行程序。
按照设计要求编写CustomerView类,逐一实现各个方法,并编译
执行main方法中,测试以下功能:
主菜单显示及操作是否正确
“添加客户”操作是否正确,给用户的提示是否明确合理;
测试当添加的客户总数超过10时,运行是否正确。
“修改客户”操作是否正确,给用户的提示是否明确合理;
“删除客户”操作是否正确,给用户的提示是否明确合理;
“客户列表”操作是否正确,表格是否规整;
1 public class CustomerView { 2 3 private CustomerList customers = new CustomerList(10); 4 5 public CustomerView() { 6 Customer cust = new Customer("张三", '男', 30, "010-56253825", 7 "[email protected]"); 8 customers.addCustomer(cust); 9 } 10 11 public void enterMainMenu() { 12 boolean loopFlag = true; 13 do { 14 System.out 15 .println("\n-----------------客户信息管理软件-----------------\n"); 16 System.out.println(" 1 添 加 客 户"); 17 System.out.println(" 2 修 改 客 户"); 18 System.out.println(" 3 删 除 客 户"); 19 System.out.println(" 4 客 户 列 表"); 20 System.out.println(" 5 退 出\n"); 21 System.out.print(" 请选择(1-5):"); 22 23 char key = CMUtility.readMenuSelection(); 24 System.out.println(); 25 switch (key) { 26 case '1': 27 addNewCustomer(); 28 break; 29 case '2': 30 modifyCustomer(); 31 break; 32 case '3': 33 deleteCustomer(); 34 break; 35 case '4': 36 listAllCustomers(); 37 break; 38 case '5': 39 System.out.print("确认是否退出(Y/N):"); 40 char yn = CMUtility.readConfirmSelection(); 41 if (yn == 'Y') 42 loopFlag = false; 43 break; 44 } 45 } while (loopFlag); 46 } 47 48 private void addNewCustomer() { 49 System.out.println("---------------------添加客户---------------------"); 50 System.out.print("姓名:"); 51 String name = CMUtility.readString(4); 52 System.out.print("性别:"); 53 char gender = CMUtility.readChar(); 54 System.out.print("年龄:"); 55 int age = CMUtility.readInt(); 56 System.out.print("电话:"); 57 String phone = CMUtility.readString(15); 58 System.out.print("邮箱:"); 59 String email = CMUtility.readString(15); 60 61 Customer cust = new Customer(name, gender, age, phone, email); 62 boolean flag = customers.addCustomer(cust); 63 if (flag) { 64 System.out 65 .println("---------------------添加完成---------------------"); 66 } else { 67 System.out.println("----------------记录已满,无法添加-----------------"); 68 } 69 } 70 71 private void modifyCustomer() { 72 System.out.println("---------------------修改客户---------------------"); 73 74 int index = 0; 75 Customer cust = null; 76 for (;;) { 77 System.out.print("请选择待修改客户编号(-1退出):"); 78 index = CMUtility.readInt(); 79 if (index == -1) { 80 return; 81 } 82 83 cust = customers.getCustomer(index - 1); 84 if (cust == null) { 85 System.out.println("无法找到指定客户!"); 86 } else 87 break; 88 } 89 90 System.out.print("姓名(" + cust.getName() + "):"); 91 String name = CMUtility.readString(4, cust.getName()); 92 93 System.out.print("性别(" + cust.getGender() + "):"); 94 char gender = CMUtility.readChar(cust.getGender()); 95 96 System.out.print("年龄(" + cust.getAge() + "):"); 97 int age = CMUtility.readInt(cust.getAge()); 98 99 System.out.print("电话(" + cust.getPhone() + "):"); 100 String phone = CMUtility.readString(15, cust.getPhone()); 101 102 System.out.print("邮箱(" + cust.getEmail() + "):"); 103 String email = CMUtility.readString(15, cust.getEmail()); 104 105 cust = new Customer(name, gender, age, phone, email); 106 107 boolean flag = customers.replaceCustomer(index - 1, cust); 108 if (flag) { 109 System.out 110 .println("---------------------修改完成---------------------"); 111 } else { 112 System.out.println("----------无法找到指定客户,修改失败--------------"); 113 } 114 } 115 116 private void deleteCustomer() { 117 System.out.println("---------------------删除客户---------------------"); 118 119 int index = 0; 120 Customer cust = null; 121 for (;;) { 122 System.out.print("请选择待删除客户编号(-1退出):"); 123 index = CMUtility.readInt(); 124 if (index == -1) { 125 return; 126 } 127 128 cust = customers.getCustomer(index - 1); 129 if (cust == null) { 130 System.out.println("无法找到指定客户!"); 131 } else 132 break; 133 } 134 135 System.out.print("确认是否删除(Y/N):"); 136 char yn = CMUtility.readConfirmSelection(); 137 if (yn == 'N') 138 return; 139 140 boolean flag = customers.deleteCustomer(index - 1); 141 if (flag) { 142 System.out 143 .println("---------------------删除完成---------------------"); 144 } else { 145 System.out.println("----------无法找到指定客户,删除失败--------------"); 146 } 147 } 148 149 private void listAllCustomers() { 150 System.out.println("---------------------------客户列表---------------------------"); 151 Customer[] custs = customers.getAllCustomers(); 152 if (custs.length == 0) { 153 System.out.println("没有客户记录!"); 154 } else { 155 System.out.println("编号\t姓名\t性别\t年龄\t\t电话\t\t邮箱"); 156 for (int i = 0; i < custs.length; i++) { 157 System.out.println((i+1) + "\t" + custs[i].getDetails()); 158 } 159 } 160 161 System.out.println("-------------------------客户列表完成-------------------------"); 162 } 163 164 public static void main(String[] args) { 165 CustomerView cView = new CustomerView(); 166 cView.enterMainMenu(); 167 } 168 }
1 import java.util.Scanner; 2 3 public class CMUtility { 4 private static Scanner scanner = new Scanner(System.in); 5 /** 6 用于界面菜单的选择。如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。 7 */ 8 public static char readMenuSelection() { 9 char c; 10 for (; ; ) { 11 String str = readKeyBoard(1, false); 12 c = str.charAt(0); 13 if (c != '1' && c != '2' && 14 c != '3' && c != '4' && c != '5') { 15 System.out.print("选择错误,请重新输入:"); 16 } else break; 17 } 18 return c; 19 } 20 /** 21 从键盘读取一个字符,并将其作为方法的返回值。 22 */ 23 public static char readChar() { 24 String str = readKeyBoard(1, false); 25 return str.charAt(0); 26 } 27 /** 28 从键盘读取一个字符,并将其作为方法的返回值。 29 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。 30 */ 31 public static char readChar(char defaultValue) { 32 String str = readKeyBoard(1, true); 33 return (str.length() == 0) ? defaultValue : str.charAt(0); 34 } 35 /** 36 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。 37 */ 38 public static int readInt() { 39 int n; 40 for (; ; ) { 41 String str = readKeyBoard(2, false); 42 try { 43 n = Integer.parseInt(str); 44 break; 45 } catch (NumberFormatException e) { 46 System.out.print("数字输入错误,请重新输入:"); 47 } 48 } 49 return n; 50 } 51 /** 52 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。 53 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。 54 */ 55 public static int readInt(int defaultValue) { 56 int n; 57 for (; ; ) { 58 String str = readKeyBoard(2, true); 59 if (str.equals("")) { 60 return defaultValue; 61 } 62 63 try { 64 n = Integer.parseInt(str); 65 break; 66 } catch (NumberFormatException e) { 67 System.out.print("数字输入错误,请重新输入:"); 68 } 69 } 70 return n; 71 } 72 /** 73 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。 74 */ 75 public static String readString(int limit) { 76 return readKeyBoard(limit, false); 77 } 78 /** 79 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。 80 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。 81 */ 82 public static String readString(int limit, String defaultValue) { 83 String str = readKeyBoard(limit, true); 84 return str.equals("")? defaultValue : str; 85 } 86 /** 87 用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。 88 */ 89 public static char readConfirmSelection() { 90 char c; 91 for (; ; ) { 92 String str = readKeyBoard(1, false).toUpperCase(); 93 c = str.charAt(0); 94 if (c == 'Y' || c == 'N') { 95 break; 96 } else { 97 System.out.print("选择错误,请重新输入:"); 98 } 99 } 100 return c; 101 } 102 103 private static String readKeyBoard(int limit, boolean blankReturn) { 104 String line = ""; 105 106 while (scanner.hasNextLine()) { 107 line = scanner.nextLine(); 108 if (line.length() == 0) { 109 if (blankReturn) return line; 110 else continue; 111 } 112 113 if (line.length() < 1 || line.length() > limit) { 114 System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:"); 115 continue; 116 } 117 break; 118 } 119 120 return line; 121 } 122 }