本文實例為大家分享了java實現客戶信息管理系統的具體代碼,供大家參考,具體內容如下
一、CMUtility工具類
講不同的功能封裝為方法,就是可以直接通過調用方法使用它的功能,而無需考慮具體的功能實現細節。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
package com.p2.util; import java.util.*; /** * 工具類 * 講不同的功能封裝為方法,就是可以直接通過調用方法使用它的功能,而無需考慮具體的功 * 能實現細節。 * **/ public class CMUtility { private static Scanner scanner = new Scanner(System.in); /** * 用于界面菜單的選擇,該方法讀取鍵盤,如果用戶鍵入"1"-"5"中的任意字符,則方法返回。 * 返回值為用戶選擇的數字。 **/ public static char readMenuSelection(){ char c; for (; ; ){ String str = readaKeyBoard( 1 , false ); c = str.charAt( 0 ); if (c != '1' && c!= '2' && c!= '3' && c!= '4' && c!= '5' ){ System.out.println( "選擇錯誤,請重新輸入:" ); } else break ; } return c; } /** * 從鍵盤讀取一個字符,并將其作為方法的返回值。 **/ public static char readChar(){ String str = readaKeyBoard( 1 , false ); return str.charAt( 0 ); } /** * 從鍵盤讀取一個字符,并將其作為方法的返回值。 * 如果用戶不輸入字符而直接回車,方法將以defaultValue 作為返回值。 **/ public static char readChar( char defaultValue){ String str = readaKeyBoard( 1 , true ); return (str.length() == 0 ) ? defaultValue :str.charAt( 0 ); } /** * 從鍵盤讀取一個長度不超過2位的整數,并將其作為方法的返回值。 **/ public static int readInt(){ int n; for (; ; ){ String str = readaKeyBoard( 2 , false ); try { n = Integer.parseInt(str); break ; } catch (NumberFormatException e){ System.out.println( "數字輸入錯誤,請重新輸入:" ); } } return n; } /** * 從鍵盤讀取一個長度不超過2位的整數,并將其為方法的返回值。 * 如果用戶不輸入字符而直接回車,方法將以defaultValue 作為返回值。 **/ public static int readInt( int defaultValue){ int n; for (; ; ){ String str = readaKeyBoard( 2 , true ); if (str.equals( "" )){ return defaultValue; } try { n = Integer.parseInt(str); break ; } catch (NumberFormatException e){ System.out.println( "數字輸入錯誤,請重新輸入:" ); } } return n; } /** * 從鍵盤 讀取一個長讀不超過limit的字符串,并將其作為方法的返回值。 **/ public static String readString ( int limit){ return readaKeyBoard(limit, false ); } public static String readString ( int limit, String defaultValue){ return readaKeyBoard(limit, false ); } /** * 從鍵盤讀取一個瘡毒不超過limit的字符串,將其作為方法的返回值。 * 如果用戶不輸入字符而直接回車,方法以defaultValue作為返回值。 **/ public static char readComfirmSelection(){ char c; for (; ; ){ String str = readaKeyBoard( 1 , false ).toUpperCase(); c = str.charAt( 0 ); if (c == 'Y' || c == 'N' ){ break ; } else { System.out.println( "選擇錯誤,請重新輸入:" ); } } return c; } private static String readaKeyBoard( 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.println( "輸入長度(不大于" + limit + ")錯誤,請重新輸入:" ); continue ; } break ; } return line; } } |
二、Customer
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package com.p2.bean; /** * 把對象封裝到方法里面 * * **/ public class Customer { private String name; //客戶姓名 private char gender; //性別 private int age; //年齡 private String phone; //電話號碼 private String email; //電子郵箱 //構造器 //方法(封裝) public Customer(String name, char gender, int age, String phone, String email) { this .name = name; this .gender = gender; this .age = age; this .phone = phone; this .email = email; } public Customer() { } public String getName(){ return name; } public void setName(String name) { this .name = name; } public char getGender() { return gender; } public void setGender( char gender) { this .gender = gender; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this .phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this .email = email; } } |
三、CustomerList
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
package com.p2.service; /** * 最主要的 * **/ import com.p2.bean.Customer; public class CustomerList { private Customer[] customers; private int total = 0 ; //構造器 /** * 用來初始化customers數組的構造器 * @param totalCustomer: 指定數組的長度 **/ public CustomerList ( int totalCustomer){ //給正在創建的對象進行初始化 customers = new Customer[totalCustomer]; } //方法 /** * 將指定的客戶添加到數組中 * @param customer * @return成功true,失敗:false **/ public boolean addCustomer(Customer customer){ //增 if (total < 0 || total >= customers.length){ return false ; } customers[total++] = customer; return true ; } /** * 修改指定位置的客戶信息 * @param index * @param cust * @return **/ public boolean replaceCustomer( int index, Customer cust){ //改 if (index < 0 || index >= total){ return false ; } customers[index] = cust; return true ; } /** * 刪除指定位置上的客戶 * @param index * @return **/ public boolean deleteCustsomer( int index){ //刪除 if (index < 0 || index >= total){ return false ; } for ( int i = index; i < total - 1 ; i++){ customers[i] = customers[i + 1 ]; } //最后有數據的元素需要置空 // customers[total - 1] =null; // total --; customers[-- total] = null ; return true ; } /** * 獲得所有的客戶信息 * @return **/ public Customer[] getAllCustomers() { //查詢所有 Customer[] custs = new Customer[total]; for ( int i = 0 ; i < total; i++){ custs[i] = customers[i]; } return custs; } /** * 獲取指定位置上的客戶信息 * @param index * @return **/ public Customer getCustomer( int index) { //查詢一個 if (index < 0 || index >= total){ return null ; } return customers[index]; } //封裝 /** * 獲取存儲客戶的的數量 * @return **/ public int getTotal() { //統計數據 return total; } } |
四、CustomerView
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
package com.p2.ui; import com.p2.bean.Customer; import com.p2.service.CustomerList; import com.p2.util.CMUtility; /** * * 用戶交互 * * * **/ public class CustomerView { private CustomerList customerList = new CustomerList( 100 ); public CustomerView(){ customerList.addCustomer(customer); } /** * 顯示《客戶信息管理軟件》 **/ public void enterMainMenu(){ boolean isFlag = true ; while (isFlag){ System.out.println( "\n----------客戶信息管理軟件--------" ); System.out.println( " 1.添加客戶" ); System.out.println( " 2.修改客戶" ); System.out.println( " 3.刪除客戶" ); System.out.println( " 4.客戶列表" ); System.out.println( " 5.退出\n" ); System.out.print( " 請選擇(1-5):" ); char menu = CMUtility.readMenuSelection(); switch (menu){ case '1' : addNewCustomer(); break ; case '2' : modifyCustomer(); break ; case '3' : deleteCustomer(); break ; case '4' : listAllCustomers(); break ; case '5' : // System.out.println("退出"); System.out.println( "確認是否退出(Y/N):" ); char isExit = CMUtility.readComfirmSelection(); if (isExit == 'Y' ){ isFlag = false ; } // break; } // isFlag = false; } } /** * 添加客戶 * **/ private void addNewCustomer(){ System.out.println( "------添加客戶------" ); System.out.print( "姓名:" ); String name = CMUtility.readString( 10 ); System.out.print( "性別:" ); char gender = CMUtility.readChar(); System.out.print( "年齡:" ); int age = CMUtility.readInt(); System.out.print( "電話:" ); String phone = CMUtility.readString( 13 ); System.out.print( "郵箱:" ); String email = CMUtility.readString( 30 ); //將上述數據封裝到對象中 Customer customer = new Customer(name, gender, age, phone, email); boolean isSucccess = customerList.addCustomer(customer); if (isSucccess){ System.out.println( "------添加完成------" ); } else { System.out.println( "------客戶已滿------" ); } } /** * 修改客戶 **/ private void modifyCustomer(){ System.out.println( "---------修改客戶-------" ); Customer cust; int number; for (;;){ System.out.print( "選擇要修改的客戶編號(-1退出):" ); number = CMUtility.readInt(); if (number == - 1 ){ return ; } else { cust = customerList.getCustomer(number - 1 ); if (cust == null ){ System.out.println( "無法找到指定客戶!!" ); } else { //找到相應的客戶 break ; } } } //修改客戶信息 System.out.print( "姓名:(" + cust.getName() + "):" ); String name = CMUtility.readString( 10 , cust.getName()); System.out.print( "性別:(" + cust.getGender() + "):" ); char gender = CMUtility.readChar(cust.getGender()); System.out.print( "年齡:(" + cust.getAge() + "):" ); int age = CMUtility.readInt(cust.getAge()); System.out.print( "電話:(" + cust.getPhone() + "):" ); String phone = CMUtility.readString( 13 , cust.getPhone()); System.out.print( "郵箱:(" + cust.getEmail() + "):" ); String email = CMUtility.readString( 30 ,cust.getEmail()); Customer newcust = new Customer(name, gender, age, phone, email); boolean isRepalaced = customerList.replaceCustomer(number - 1 , newcust); if (isRepalaced){ System.out.println( "修改成功!!" ); } else { System.out.println( "修改失敗!!" ); } } /** * 刪除客戶 **/ private void deleteCustomer(){ System.out.println( "-------刪除客戶-------" ); int number; for (;;){ System.out.print( "請選擇呆刪除客戶的編號(-1退出):" ); number = CMUtility.readInt(); if (number ==- 1 ){ return ; } Customer customer =customerList.getCustomer(number - 1 ); if (customer == null ){ System.out.println( "無法找到指定客戶!!" ); } else { break ; } } //找到客戶了 System.out.println( "是否確認刪除(Y/N):" ); char isDelete = CMUtility.readComfirmSelection(); if (isDelete == 'Y' ){ boolean deleteSuccess = customerList.deleteCustsomer(number - 1 ); if (deleteSuccess ){ System.out.println( "-----------刪除成功---------" ); } else { System.out.println( "-----------刪除失敗---------" ); } } else { return ; } } /** * 顯示客戶列表 **/ private void listAllCustomers(){ // System.out.println("顯示客戶"); System.out.println( "---------客戶列表-------" ); int total = customerList.getTotal(); if (total == 0 ){ System.out.println( "沒有客戶記錄!" ); } else { System.out.println( "編號\t姓名\t性別\t年齡\t電話\t\t郵箱" ); Customer[] custs = customerList.getAllCustomers(); for ( int i = 0 ;i<custs.length; i++){ Customer cust = custs[i]; System.out.println((i + 1 ) + "\t" + cust.getName() + "\t" +cust.getGender() + "\t" +cust.getAge() + "\t" + cust.getPhone() + "\t" + cust.getEmail()); } } System.out.println( "---------客戶列表結束-------" ); } public static void main(String[] args) { CustomerView view = new CustomerView(); view.enterMainMenu(); } } |
五、運行結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/xue3236522091/article/details/113799144