工具类 CMUtility
1 package day1_14;
2
3 import java.util.Scanner;
4
5 public class CMUtility {
6
7 private static Scanner scanner = new Scanner(System.in);
8
9 /**
10 * 用于界面菜单的选择,该方法读取键盘,
11 * 如果用户键入'1'-'5'中的任意字符,则方法返回用户键入的字符
12 */
13 public static char readMenuSelection(){
14 char c;
15 for (; ; ) {
16 String str = readKeyBoard(1, false);
17 c = str.charAt(0);
18 if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5') {
19 System.out.print("选择错误,请重新输入:");
20 }else{
21 break;
22 }
23 }
24 return c;
25 }
26
27
28 /**
29 * 从键盘读取一个字符,并将其作为方法的返回值
30 */
31 public static char readChar() {
32 String str = readKeyBoard(1, false);
33 return str.charAt(0);
34 }
35
36 /**
37 * 从键盘读取一个字符,并将其作为方法的返回值
38 * 如果用户不输入字符而直接回车,方法将以defaultValue作为返回值
39 */
40 public static char readChar(char defaultValue) {
41 String str = readKeyBoard(1, true);
42 return (str.length() == 0) ? defaultValue :str.charAt(0);
43 }
44
45 /**
46 * 从键盘读取一个长度不超过2的整数,并将其作为方法的返回值
47 */
48 public static int readInt() {
49 int n;
50 for (; ; ) {
51 String str = readKeyBoard(2, false);
52 try {
53 n = Integer.parseInt(str);
54 break;
55 } catch (NumberFormatException e) {
56 System.out.print("数字输入错误,请重新输入:");
57 }
58 }
59 return n;
60 }
61
62 /**
63 * 从键盘读取一个长度不超过2的整数,并将其作为方法的返回值
64 * 如果用户不输入字符而直接回车,方法将以defaultValue作为返回值
65 */
66 public static int readInt(int defaultValue) {
67 int n ;
68 for (; ; ) {
69 String str = readKeyBoard(2, true);
70 if(str.length() == 0){
71 return defaultValue;
72 }
73 try{
74 n = Integer.parseInt(str);
75 break;
76 } catch (NumberFormatException e) {
77 System.out.print("数字输入错误,请重新输入:");
78 }
79 }
80 return n;
81 }
82
83 /**
84 * 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值
85 */
86 public static String readString(int limit) {
87 return readKeyBoard(limit, false);
88 }
89
90
91 /**
92 * 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值
93 * 如果用户不输入字符而直接回车,方法将以defaultValue作为返回值
94 */
95 public static String readString(int limit, String defaultValue) {
96 String str = readKeyBoard(limit, true);
97 return str.equals("") ? defaultValue :str;
98 }
99
100 /**
101 * 用于确认选择的输入,该方法从键盘读取'Y'或'N',并将其作为方法的返回值
102 */
103 public static char readConfirmSelection() {
104 char c;
105 for (; ; ) {
106 String str = readKeyBoard(1,false).toUpperCase();
107 c = str.charAt(0);
108 if (c == 'Y' || c == 'N') {
109 break;
110 }else{
111 System.out.print("选择错误,请重新输入:");
112 continue;
113 }
114 }
115 return c;
116 }
117
118
119 //封装私有的方法
120 private static String readKeyBoard(int limit, boolean blankReturn) {
121 String line = "";
122
123 while (scanner.hasNextLine()) {
124 line = scanner.nextLine();
125 if (line.length() == 0) {
126 if (blankReturn){
127 return line;
128 }else {
129 System.out.print("请重新输入:");
130 continue;
131 }
132 }
133
134 if (line.length() < 1 ||
135 line.length() > limit) {
136 System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
137 continue;
138 }
139 break;
140 }
141 return line;
142 }
143 }
Customer类
1 package day1_14;
2
3 public class Customer {
4 private String name; //姓名
5 private char gender;//性别
6 private int age; //年龄
7 private String phone;//电话号码
8 private String email;//电子邮箱
9
10 public Customer() {
11 }
12
13 public Customer(String name, char gender, int age, String phone, String email) {
14 this.name = name;
15 this.gender = gender;
16 this.age = age;
17 this.phone = phone;
18 this.email = email;
19 }
20
21 public String getName() {
22 return name;
23 }
24
25 public void setName(String name) {
26 this.name = name;
27 }
28
29 public char getGender() {
30 return gender;
31 }
32
33 public void setGender(char gender) {
34 this.gender = gender;
35 }
36
37 public int getAge() {
38 return age;
39 }
40
41 public void setAge(int age) {
42 this.age = age;
43 }
44
45 public String getPhone() {
46 return phone;
47 }
48
49 public void setPhone(String phone) {
50 this.phone = phone;
51 }
52
53 public String getEmail() {
54 return email;
55 }
56
57 public void setEmail(String email) {
58 this.email = email;
59 }
60 }
CustomerList类
1 package day1_14;
2
3 public class CustomerList {
4 private Customer[] customers;//用来保存客户对象的数组
5 private int total; //记录以保存客户对象的数量
6
7 /**
8 * 用来初始化customers数组的构造器
9 * @param totalCustomer 指定数组的长度
10 */
11 public CustomerList(int totalCustomer) {
12 customers = new Customer[totalCustomer];
13 }
14
15 /**
16 * 将指定的客户添加到数组中
17 * @param cst
18 * @return true:添加成功 false:添加失败
19 */
20 public boolean addCustomer(Customer cst) {
21 if (total >= customers.length) {
22 return false;
23 }
24 //第一种方式
25 //customers[total] = cst;
26 //total++;
27 //第二种方式
28 customers[total++] = cst;
29 return true;
30 }
31
32 /**
33 * 修改指定索引位置的客户信息
34 * @param index
35 * @param cst
36 * @return true:添加成功 false:添加失败
37 */
38 public boolean replaceCustomer(int index,Customer cst) {
39 if (index < 0 || index >= total) {
40 return false;
41 }
42 customers[index] = cst;
43 return true;
44 }
45
46
47 /**
48 * 删除指定索引位置的客户
49 * @param index
50 * @return true:添加成功 false:添加失败
51 */
52 public boolean deleteCustomer(int index) {
53 if (index < 0 || index >= total) {
54 return false;
55 }
56
57 for (int i = index; i < total-1; i++) {
58 customers[i] = customers[i + 1];
59 }
60 //最后有数据的元素需要置空
61 //第一种方式
62 //customers[total - 1] = null;
63 //total--;
64
65 //第二种方式
66 customers[--total] = null;
67 return true;
68 }
69
70 /**
71 * 获取所有的客户信息
72 * @return
73 */
74 public Customer[] getAllCustomer() {
75 Customer[] csts = new Customer[total];
76 for (int i = 0; i < total; i++) {
77 csts[i] = customers[i];
78 }
79 return csts;
80 }
81
82 /**
83 * 获取指定索引位置的客户信息
84 *
85 * @return 如果找到元素,则返回;如果没有找到,则返回null
86 * @paam index
87 */
88 public Customer getCustomer(int index) {
89 if (index < 0 || index >= total) {
90 return null;
91 }
92 return customers[index];
93 }
94
95 /**
96 * 获取存储的客户的数量
97 * @return
98 */
99 public int getTotal(){
100 return total;
101 }
102
103 }
CustomerView
1 package day1_14;
2
3 import com.sun.org.apache.xerces.internal.impl.xs.SchemaNamespaceSupport;
4
5 import java.util.Scanner;
6
7 public class CustomerView {
8 //最大包含10个客户对象
9 private static CustomerList customerList = new CustomerList(10);
10
11 public CustomerView() {
12 //Customer customer = new Customer("张三", '男', 29, "13212344321", "123@gmail");
13 //customerList.addCustomer(customer);
14 }
15
16 public static void main(String[] args) {
17 CustomerView view = new CustomerView();
18 view.enterMainMenu();
19 }
20
21 /**
22 * 显示客户管理软件主菜单的方法
23 */
24 public void enterMainMenu() {
25 boolean isFlag = true;
26 while(isFlag){
27 System.out.println("\n-----------------客户信息管理软件-----------------\n");
28 System.out.println(" 1 添 加 客 户");
29 System.out.println(" 2 修 改 客 户");
30 System.out.println(" 3 删 除 客 户");
31 System.out.println(" 4 客 户 列 表");
32 System.out.println(" 5 退 出\n");
33 System.out.print(" 请选择(1-5):");
34 char menu = CMUtility.readMenuSelection();
35 switch (menu) {
36 case '1':
37 addNewCustomer();
38 break;
39 case '2':
40 modifyCustomer();
41 break;
42 case '3':
43 removeCustomer();
44 break;
45 case '4':
46 listAllCustomers();
47 break;
48 case '5':
49 //System.out.println("退出");
50 System.out.print("确认是否退出(Y/N):");
51 char isExit = CMUtility.readConfirmSelection();
52 if(isExit == 'Y'){
53 isFlag = false;
54 }
55 }
56 }
57 }
58
59 /**
60 * 添加客户的操作
61 */
62 public void addNewCustomer() {
63 //System.out.println("添加客户的操作");
64 System.out.print("姓名:");
65 String name = CMUtility.readString(10);
66 System.out.print("性别:");
67 char gendel = CMUtility.readChar();
68 System.out.print("年龄:");
69 int age = CMUtility.readInt();
70 System.out.print("电话号码:");
71 String phone = CMUtility.readString(13);
72 System.out.print("电子邮箱:");
73 String email = CMUtility.readString(30);
74 //将上述数据封装到对象中
75 Customer customer = new Customer(name, gendel, age, phone, email);
76 boolean isSuccess = customerList.addCustomer(customer);
77 if (isSuccess) {
78 System.out.println("---------------添加客户成功---------------");
79 }else {
80 System.out.println("----------客户目录已满,添加失败----------");
81 }
82
83 }
84
85 /**
86 * 修改客户的操作
87 */
88 public void modifyCustomer() {
89 //System.out.println("修改客户的操作");
90 System.out.println("-------------修改客户-------------");
91 Customer customer;
92 int number;
93 for(;;){
94 System.out.print("请选择待修改客户编号(-1退出):");
95 number = CMUtility.readInt();
96 if (number == -1) {
97 return;
98 }
99 customer = customerList.getCustomer(number-1);
100 if (customer == null) {
101 System.out.println("无法找到指定客户");
102 } else {//找到了相应编号的客户
103 break;
104 }
105 }
106
107 //程序走到这里,意味着找到了指定的客户
108 System.out.print("姓名(" + customer.getName() + "):");
109 String name = CMUtility.readString(10,customer.getName());
110 System.out.print("性别(" + customer.getGender() + "):");
111 char gendel = CMUtility.readChar(customer.getGender());
112 System.out.print("年龄(" + customer.getAge() + "):");
113 int age = CMUtility.readInt(customer.getAge());
114 System.out.print("电话号码(" + customer.getPhone() + "):");
115 String phone = CMUtility.readString(13,customer.getPhone());
116 System.out.print("电子邮箱(" + customer.getEmail() + "):");
117 String email = CMUtility.readString(30,customer.getEmail());
118 Customer newCustomer = new Customer(name, gendel, age, phone, email);
119 boolean isReplaced = customerList.replaceCustomer(number - 1, newCustomer);
120 if (isReplaced) {
121 System.out.println("-----------修改完成-----------");
122 } else {
123 System.out.println("-----------修改失败-----------");
124 }
125
126 }
127
128 /**
129 * 删除客户的操作
130 */
131 public void removeCustomer() {
132 //System.out.println("删除客户的操作");
133 System.out.println("-------------删除客户-------------");
134 int number;
135 for(;;) {
136 System.out.print("请选择待删除客户编号(-1退出):");
137 number = CMUtility.readInt();
138 if (number == -1) {
139 return;
140 }
141 Customer customer = customerList.getCustomer(number-1);
142 if (customer == null) {
143 System.out.println("无法找到指定的客户");
144 } else {
145 System.out.print("确认是否删除(Y/N):");
146 char isConfirmed = CMUtility.readConfirmSelection();
147 if (isConfirmed == 'Y') {
148 break;
149 }
150 }
151 }
152 boolean isDeleted = customerList.deleteCustomer(number-1);
153 if (isDeleted) {
154 System.out.println("------------删除成功------------");
155 } else {
156 System.out.println("------------删除失败------------");
157 }
158 }
159
160 /**
161 * 显示客户列表的操作
162 */
163 public void listAllCustomers() {
164 //System.out.println("显示客户列表的操作");
165 System.out.println("---------------客户列表---------------");
166 int total = customerList.getTotal();
167 if (total == 0) {
168 System.out.println("没有客户记录!");
169 }else{
170 System.out.println("编号\t姓名\t性别\t年龄\t电话号码\t电子邮箱");
171 Customer[] allCustomer = customerList.getAllCustomer();
172 for (int i = 0; i < allCustomer.length; i++) {
173 Customer customer = allCustomer[i];
174 System.out.println((i + 1) + "\t" + customer.getName() +
175 "\t" + customer.getGender() +
176 "\t" + customer.getAge() +
177 "\t" + customer.getPhone() +
178 "\t" + customer.getEmail());
179 }
180 }
181 System.out.println("---------------客户列表完成---------------");
182 }
183 }