1 package com.atguigu.bean;
2
3 ///文档注释 alt+shift+j
4 /**
5 * @description Customer为实体对象,用来封装客户信息
6 * @author 吴彦祖
7 * @version
8 * @date 2020年8月14日下午4:58:29
9 */
10 public class Customer {
11 private String name;// 客户姓名
12 private char gender;// 性别
13 private int age; // 年龄
14 private String phone;// 电话号码
15 private String email;// 电子邮箱
16 public String getName() {
17 return name;
18 }
19 public void setName(String name) {
20 this.name = name;
21 }
22 public char getGender() {
23 return gender;
24 }
25 public void setGender(char gender) {
26 this.gender = gender;
27 }
28 public int getAge() {
29 return age;
30 }
31 public void setAge(int age) {
32 this.age = age;
33 }
34 public String getPhone() {
35 return phone;
36 }
37 public void setPhone(String phone) {
38 this.phone = phone;
39 }
40 public String getEmail() {
41 return email;
42 }
43 public void setEmail(String email) {
44 this.email = email;
45 }
46
47 public Customer() {
48
49 }
50 public Customer(String name, char gender, int age, String phone, String email) {
51
52 this.name = name;
53 this.gender = gender;
54 this.age = age;
55 this.phone = phone;
56 this.email = email;
57 }
58
59 }
1 package com.atguigu.p2.util;
2
3
4 import java.util.*;
5 /**
6 CMUtility工具类:
7 将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
8 */
9 public class CMUtility {
10 private static Scanner scanner = new Scanner(System.in);
11 /**
12 用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
13 */
14 public static char readMenuSelection() {
15 char c;
16 for (; ; ) {
17 String str = readKeyBoard(1, false);
18 c = str.charAt(0);
19 if (c != '1' && c != '2' &&
20 c != '3' && c != '4' && c != '5') {
21 System.out.print("选择错误,请重新输入:");
22 } else break;
23 }
24 return c;
25 }
26 /**
27 从键盘读取一个字符,并将其作为方法的返回值。
28 */
29 public static char readChar() {
30 String str = readKeyBoard(1, false);
31 return str.charAt(0);
32 }
33 /**
34 从键盘读取一个字符,并将其作为方法的返回值。
35 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
36 */
37 public static char readChar(char defaultValue) {
38 String str = readKeyBoard(1, true);
39 return (str.length() == 0) ? defaultValue : str.charAt(0);
40 }
41 /**
42 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
43 */
44 public static int readInt() {
45 int n;
46 for (; ; ) {
47 String str = readKeyBoard(2, false);
48 try {
49 n = Integer.parseInt(str);
50 break;
51 } catch (NumberFormatException e) {
52 System.out.print("数字输入错误,请重新输入:");
53 }
54 }
55 return n;
56 }
57 /**
58 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
59 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
60 */
61 public static int readInt(int defaultValue) {
62 int n;
63 for (; ; ) {
64 String str = readKeyBoard(2, true);
65 if (str.equals("")) {
66 return defaultValue;
67 }
68
69 try {
70 n = Integer.parseInt(str);
71 break;
72 } catch (NumberFormatException e) {
73 System.out.print("数字输入错误,请重新输入:");
74 }
75 }
76 return n;
77 }
78 /**
79 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
80 */
81 public static String readString(int limit) {
82 return readKeyBoard(limit, false);
83 }
84 /**
85 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
86 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
87 */
88 public static String readString(int limit, String defaultValue) {
89 String str = readKeyBoard(limit, true);
90 return str.equals("")? defaultValue : str;
91 }
92 /**
93 用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
94 */
95 public static char readConfirmSelection() {
96 char c;
97 for (; ; ) {
98 String str = readKeyBoard(1, false).toUpperCase();
99 c = str.charAt(0);
100 if (c == 'Y' || c == 'N') {
101 break;
102 } else {
103 System.out.print("选择错误,请重新输入:");
104 }
105 }
106 return c;
107 }
108
109 private static String readKeyBoard(int limit, boolean blankReturn) {
110 String line = "";
111
112 while (scanner.hasNextLine()) {
113 line = scanner.nextLine();
114 if (line.length() == 0) {
115 if (blankReturn) return line;
116 else continue;
117 }
118
119 if (line.length() < 1 || line.length() > limit) {
120 System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
121 continue;
122 }
123 break;
124 }
125
126 return line;
127 }
128 }
1 package com.atguigu.p2.service;
2
3 import com.atguigu.p2.bean.Customer;
4
5 /**
6 * @description CustomerList为Customer对象的管理模块,
7 * 内部用数组管理一组Customer对象,并提供相应的添加、修改、删除和遍历方法, 供CustomerView调用
8 *
9 * @author 吴彦祖
10 * @version
11 * @date 2020年8月14日下午5:02:05
12 */
13
14 /**
15 * @description
16 * @author 吴彦祖
17 * @version
18 * @date 2020年8月14日下午5:26:27
19 */
20 public class CustomerList {
21 private Customer[] customers;// 用来保存客户对象的数组
22 private int total = 0;// 记录已保存客户对象的数量
23 /**
24 * 用途:构造器,用来初始化customers数组
25 * @param totalCustomer 指定customers数组的最大空间
26 */
27 public CustomerList(int totalCustomer) {
28 customers = new Customer[totalCustomer];
29 }
30
31 /**
32 * @description 用途:将参数customer添加到数组中最后一个客户对象记录之后
33 * @author 吴彦祖
34 * @date 2020年8月14日下午5:22:07
35 * @return true :添加成功 false:添加失败
36 */
37 public boolean addCustomer(Customer customer) {
38 if(total >= customers.length) {
39 return false;
40 }
41 customers[total] = customer;
42 total++;
43 //customers[total++] = customer;
44 return true;
45 }
46
47
48 /**
49 * @Description 用途:用参数customer替换数组中由index指定的对象
50 * @author 吴彦祖
51 * @date 2020年8月14日下午5:27:59
52 * @param index index指定所替换对象在数组中的位置,从0开始
53 * @param cust customer指定替换的新客户对象
54 * @return true :修改成功 false :修改失败
55 */
56 public boolean replaceCustomer(int index, Customer cust) {
57 if(index < 0 || index >= total) {
58 return false;
59 }
60 customers[index] = cust ;
61 return true;
62 }
63
64 /**
65 * @Description 从数组中删除参数index指定索引位置的客户对象记录
66 * @author 吴彦祖
67 * @date 2020年8月14日下午5:33:31
68 * @param index
69 * @return true :删除成功 false :删除失败
70 */
71 public boolean deleteCustomer(int index) {
72 if(index < 0 || index >= total) {
73 return false;
74 }
75 for(int i = index ; i < total - 1; i++) {
76 customers[i] = customers [i+1];
77 }
78 //最后有数据的元素需要置空
79 customers[total - 1] = null;
80 total --;
81 //customers[--total] = null;
82 return true;
83
84 }
85 /**
86 * @Description 获取所有的客户信息
87 * @author 吴彦祖
88 * @date 2020年8月14日下午5:37:18
89 * @return
90 */
91 public Customer[] getAllCustomers() {
92 Customer[ ] custs = new Customer [total] ;
93 for(int i = 0 ;i< total ;i++) {
94 custs[i] =customers[i];
95 }
96 return custs;
97 }
98 /**
99 * @Description 获取制定索引位置上的客户
100 * @author 吴彦祖
101 * @date 2020年8月14日下午5:39:18
102 * @param index
103 * @return 找到了元素则返回 没有找到则返回null
104 */
105 public Customer getCustomer(int index) {
106 if(index < 0 || index >= total) {
107 return null;
108 }
109 return customers[index];
110 }
111 /**
112 * @Description 获取存储的客户的数量
113 * @author 吴彦祖
114 * @date 2020年8月14日下午5:40:02
115 * @return
116 */
117 public int getTotal() {
118 return total;
119 //return customers.length;//错误
120 }
121
122
123
124
125 }
1 package com.atguigu.p2.ui;
2
3
4 import com.atguigu.p2.bean.Customer;
5 import com.atguigu.p2.service.CustomerList;
6 import com.atguigu.p2.util.CMUtility;
7
8 /**
9 * @description CustomerView为主模块,负责菜单的显示和处理用户操作
10 *
11 * @author 吴彦祖
12 * @version
13 * @date 2020年8月14日下午5:05:19
14 */
15
16 public class CustomerView {
17 private CustomerList customerList = new CustomerList(10);
18
19 public CustomerView() {
20 Customer customer = new Customer("王涛", '男', 23, "132434343434", "wt@gmail.com");
21 customerList.addCustomer(customer);
22 }
23 // 创建最大包含10个客户对象的CustomerList 对象,供以下各成员方法使用。
24
25 /**
26 * @Description 显示 客户信息管理软件 界面的方法
27 * @author 吴彦祖
28 * @date 2020年8月14日下午6:12:30
29 */
30 public void enterMainMenu() {
31 boolean isFlag = true;
32 while (isFlag) {
33 System.out.println("\n-----------------客户信息管理软件-----------------\n");
34 System.out.println(" 1 添 加 客 户");
35 System.out.println(" 2 修 改 客 户");
36 System.out.println(" 3 删 除 客 户");
37 System.out.println(" 4 客 户 列 表");
38 System.out.println(" 5 退 出\n");
39 System.out.print(" 请选择(1-5):");
40 char menu = CMUtility.readMenuSelection();
41 switch (menu) {
42 case '1':
43 addNewCustomer();
44 break;
45 case '2':
46 modifyCustomer();
47 break;
48 case '3':
49 deleteCustomer();
50 break;
51 case '4':
52 listAllCustomers();
53 break;
54 case '5':
55 System.out.println("退出!");
56 System.out.println("确认是否退出(Y/N):");
57 char isExit = CMUtility.readConfirmSelection();
58 if (isExit == 'Y') {
59 isFlag = false;
60
61 }
62 }
63 }
64 }
65
66 /**
67 * @Description 添加客户的操作
68 * @author 吴彦祖
69 * @date 2020年8月14日下午6:11:09
70 */
71 private void addNewCustomer() {
72 // System.out.println("添加客户的操作");
73 System.out.println("---------------------------添加客户---------------------------");
74 System.out.println("姓名:");
75 String name = CMUtility.readString(10);
76 System.out.println("性别:");
77 char gender = CMUtility.readChar();
78 System.out.println("年龄:");
79 int age = CMUtility.readInt();
80 System.out.println("电话:");
81 String phone = CMUtility.readString(13);
82 System.out.println("邮箱:");
83 String email = CMUtility.readString(30);
84
85 // 将上述数据封装到对象中
86 Customer customer = new Customer(name, gender, age, phone, email);
87 boolean isSuccess = customerList.addCustomer(customer);
88 if (isSuccess) {
89 System.out.println("---------------------------添加完成---------------------------");
90 } else {
91 System.out.println("---------------------客户目录已满,添加失败-----------------------");
92 }
93 }
94
95 /**
96 * @Description 修改客户的操作
97 * @author 吴彦祖
98 * @date 2020年8月14日下午6:11:20
99 */
100 private void modifyCustomer() {
101 // System.out.println("修改客户的操作");
102 System.out.println("---------------------修改客户---------------------");
103 Customer cust;//先声明
104 int number;
105 for(;;) {
106 System.out.println("请选择待修改客户编号(-1退出):");
107 number = CMUtility.readInt();
108
109 if(number == -1) {
110 return;
111 }
112 cust = customerList.getCustomer(number - 1);
113 if(cust == null) {
114 System.out.println("无法找到指定客户!");
115 }else {//找到了相应编号的客户
116 break;
117
118 }
119 }
120 //修改客户信息
121 System.out.println("姓名("+cust.getName()+"):");
122 String name = CMUtility.readString(10,cust.getName());
123 System.out.println("性别("+cust.getGender()+"):");
124 char gender = CMUtility.readChar(cust.getGender());
125 System.out.println("年龄("+cust.getAge()+"):");
126 int age = CMUtility.readInt(cust.getAge());
127 System.out.println("电话("+cust.getPhone()+"):");
128 String phone = CMUtility.readString(13,cust.getPhone());
129 System.out.println("邮箱("+cust.getEmail()+"):");
130 String email = CMUtility.readString(30,cust.getEmail());
131
132 Customer newCust = new Customer(name,gender,age,phone,email);
133
134 boolean isRepalaced = customerList.replaceCustomer(number - 1, newCust);
135 if(isRepalaced) {
136 System.out.println("---------------------------修改完成---------------------------");
137 } else {
138 System.out.println("---------------------------修改失败---------------------------");
139 }
140 }
141
142 /**
143 * @Description 删除客户的操作
144 * @author 吴彦祖
145 * @date 2020年8月14日下午6:11:59
146 */
147 private void deleteCustomer() {
148 System.out.println("删除客户的操作");
149 System.out.println("---------------------删除客户---------------------");
150
151 int number ;
152 for(;;) {
153 System.out.print("请选择待删除客户编号(-1退出):");
154 number= CMUtility.readInt();
155
156 if(number == -1) {
157 return ;
158 }
159
160 Customer customer = customerList.getCustomer(number - 1);
161 if(customer == null) {
162 System.out.println("无法找到指定客户!");
163 }else {
164 break;
165 }
166 }
167 //找到了指定的客户
168 System.out.print("确认是否删除(Y/N):");
169 char isDelete = CMUtility.readConfirmSelection();
170 if(isDelete == 'Y') {
171 boolean deleteSuccess = customerList.deleteCustomer(number - 1);
172 if(deleteSuccess) {
173 System.out.println("---------------------删除完成---------------------");
174 }else{
175 System.out.println("---------------------删除失败---------------------");
176 }
177 }else {
178 return;
179 }
180 }
181
182 /**
183 * @Description 显示客户列表的操作
184 * @author 吴彦祖
185 * @date 2020年8月14日下午6:12:13
186 */
187 private void listAllCustomers() {
188 System.out.println("显示客户列表的操作");
189 System.out.println("---------------------------客户列表---------------------------");
190 int total = customerList.getTotal();
191 if (total == 0) {
192 System.out.println("没有客户记录!");
193
194 } else {
195 System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱\t");
196 Customer[] custs = customerList.getAllCustomers();
197 for (int i = 0; i < custs.length; i++) {
198 Customer cust = custs[i];
199 System.out.println(i + 1 + "\t" + cust.getName() + "\t" + cust.getGender() + "\t" + cust.getAge() + "\t"
200 + cust.getPhone() + "\t" + cust.getEmail() + "\t");
201 }
202 }
203 System.out.println("-------------------------客户列表完成-------------------------");
204 }
205
206 public static void main(String[] args) {
207 CustomerView v = new CustomerView();
208 v.enterMainMenu();
209 }
210
211 }