通过主菜单对各级子菜单进行控制,并实现添加记录,查找记录,删除记录,修改记录,排序记录,以及退出系统功能的实现。一共六部分的功能模块。
上面的图就是每个模块具有的功能,而且还用到了正则表达式判断输入的是否符合规则,这个系统体现了mvc的设计模式,分工调用很清晰。
主要包含了下面几个类:
app类:程序的主函数类,程序开始的入口
menu类:里面都是简单页面显示的效果,没有功能的实现。
telnoteregex类:正则表达式,判断是否输入合格,都在这个方法中。
operate类:业务逻辑类,全部功能的实现基本都集中在这个类中。
person类:封装数据。
具体的实现代码:
app类的实现代码:
java" id="highlighter_542587">
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
|
package com.bjsxt.iphone; public class app { public static void main(string[] args) { new app().start(); } public void start(){ menu m= new menu(); operate o= new operate(); telnoteregex reg= new telnoteregex(); while ( true ){ m.mainmenu(); int key=reg.menuregex( 1 , 6 ); switch (key){ case 1 : o.addlogic(); break ; case 2 : o.searchlogic(); break ; case 3 : o.modifylogiclogic(); break ; case 4 : o.deletelogic(); break ; case 5 : o.orderlogic(); break ; case 6 : system.exit( 0 ); break ; } } } } |
person类具体的实现代码:
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
|
package com.bjsxt.iphone; public class person { private int id; //用户id属性 private string name; //用户姓名属性 private string age; //用户年龄属性 private string sex; //用户性别属性 private string telnum; //用户电话号码属性 private string address; //用户地址属性 public person(){} public person( int id,string name,string age,string sex,string telnum,string address){ this .id=id; this .name=name; this .age=age; this .sex=sex; this .telnum=telnum; this .address=address; } public int getid() { return id; } public void setid( int id) { this .id=id; } public string getname() { return name; } public void setname(string name) { this .name = name; } public string getage() { return age; } public void setage(string age) { this .age = age; } public string getsex() { return sex; } public void setsex(string sex) { this .sex = sex; } public string gettelnum() { return telnum; } public void settelnum(string telnum) { this .telnum = telnum; } public string getaddress() { return address; } public void setaddress(string address) { this .address = address; } } |
telnoteregex类具体代码:
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
|
package com.bjsxt.iphone; import java.util.scanner; public class telnoteregex { //对菜单项校验 @suppresswarnings ( "resource" ) public int menuregex( int min, int max){ string regex= "[0-9]{1}" ; scanner sc= new scanner(system.in); while ( true ){ string input=sc.nextline(); //根据合法性的校验 if (input.matches(regex)){ int key=integer.parseint(input); if (key>=min && key<=max){ return key; } else { system.out.println( "输入的菜单项不符合要求!" ); } } else { system.out.println( "输入内容不正确!" ); } } } //用户输入姓名的验证 @suppresswarnings ( "resource" ) public string nameregex(string name){ string pattern = "[\\u4e00-\\u9fa5]+" ; string str=name; scanner sc= new scanner(system.in); while ( true ){ if (str.matches(pattern)){ return str; } else { system.out.println( "输入不合法字符,请重新输入中文汉字" ); str=sc.nextline(); continue ; } } } //用户输入年龄的验证 @suppresswarnings ( "resource" ) public string ageregex(string age){ string pattern = "^([0-9]|[0-9]{2}|100)$" ; string str=age; scanner sc= new scanner(system.in); while ( true ){ if (str.matches(pattern)){ return str; } else { system.out.println( "输入不合法字符,请重新输入数字" ); str=sc.nextline(); continue ; } } } //用户性别的验证 @suppresswarnings ( "resource" ) public string sexregex(string sex){ string pattern = "[男女mmff]" ; string str=sex; scanner sc= new scanner(system.in); while ( true ){ if (str.matches(pattern)){ return str; } else { system.out.println( "输入不合法字符,请重新输入男女mmff" ); str=sc.nextline(); continue ; } } } //用户输入电话号码的验证 @suppresswarnings ( "resource" ) public string telnumregex(string telnum){ string pattern = "[0-9]{11}" ; string str=telnum; scanner sc= new scanner(system.in); while ( true ){ if (str.matches(pattern)){ return str; } else { system.out.println( "输入不合法字符,请重新输入11位的手机号码" ); str=sc.nextline(); continue ; } } } //用户输入地址的验证 @suppresswarnings ( "resource" ) public string addressregex(string address){ string pattern = "[a-za-z[0-9]]{1,15}" ; string str=address; scanner sc= new scanner(system.in); while ( true ){ if (str.matches(pattern)){ return str; } else { system.out.println( "输入不合法字符,请重新输入1到15个英文大小写或是数字" ); str=sc.nextline(); continue ; } } } } |
menu类代码的具体实现:
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
|
package com.bjsxt.iphone; public class menu { //主界面 public void mainmenu() { system.out.println( "*************************" ); system.out.println( "** 1 添加记录 **" ); system.out.println( "** 2 查找记录 **" ); system.out.println( "** 3 修改记录 **" ); system.out.println( "** 4 删除记录 **" ); system.out.println( "** 5 排序记录 **" ); system.out.println( "** 6 退出系统 **" ); system.out.println( "*************************" ); } //添加界面 public void addmenu () { system.out.println( "*************************" ); system.out.println( "** 1 添加新记录 **" ); system.out.println( "** 2 查看全记录 **" ); system.out.println( "** 3 返回上一级 **" ); system.out.println( "*************************" ); } //查找界面 public void searchmenu () { system.out.println( "*************************" ); system.out.println( "** 1 按姓名查找 **" ); system.out.println( "** 2 按年龄查找 **" ); system.out.println( "** 3 按性别查找 **" ); system.out.println( "** 4 按号码查找 **" ); system.out.println( "** 5 按住址查找 **" ); system.out.println( "** 6 查看全记录 **" ); system.out.println( "** 7 返回上一级 **" ); system.out.println( "*************************" ); } //修改界面 public void modifymenu () { system.out.println( "*************************" ); system.out.println( "** 1 查看全记录 **" ); system.out.println( "** 2 修改指定记录 **" ); system.out.println( "** 3 返回上一级 **" ); system.out.println( "*************************" ); } //修改子界面 public void submodifymenu () { system.out.println( "*************************" ); system.out.println( "** 1 修改姓名 **" ); system.out.println( "** 2 修改年龄 **" ); system.out.println( "** 3 修改性别 **" ); system.out.println( "** 4 修改号码 **" ); system.out.println( "** 5 修改住址 **" ); system.out.println( "** 6 返回上一级 **" ); system.out.println( "*************************" ); } //删除界面 public void deletemenu () { system.out.println( "*************************" ); system.out.println( "** 1 查看全记录 **" ); system.out.println( "** 2 删除指定记录 **" ); system.out.println( "** 3 删除全部记录 **" ); system.out.println( "** 4 返回上一级 **" ); system.out.println( "*************************" ); } //排序界面 public void ordermenu () { system.out.println( "*************************" ); system.out.println( "** 1 按姓名排序 **" ); system.out.println( "** 2 按年龄排序 **" ); system.out.println( "** 3 按性别排序 **" ); system.out.println( "** 4 查看全部记录 **" ); system.out.println( "** 5 返回上一级 **" ); system.out.println( "*************************" ); } } |
operate类的具体实现:
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
package com.bjsxt.iphone; import java.util.arraylist; import java.util.collections; import java.util.comparator; import java.util.iterator; import java.util.list; import java.util.scanner; public class operate{ private list<person> list= new arraylist<person>(); //用户添加信息业务逻辑控制 public void addlogic(){ menu m= new menu(); telnoteregex reg= new telnoteregex(); while ( true ){ m.addmenu(); int key=reg.menuregex( 1 , 3 ); switch (key){ case 1 : addoperation (); break ; case 2 : showall(); break ; case 3 : return ; } } } //用户查询信息业务逻辑控 public void searchlogic(){ menu m= new menu(); telnoteregex reg= new telnoteregex(); while ( true ){ m.searchmenu(); int key=reg.menuregex( 1 , 7 ); switch (key){ case 1 : searchbyname(); break ; case 2 : searchbyage(); break ; case 3 : searchbysex(); break ; case 4 : searchbytelnum(); break ; case 5 : searchbyadd(); break ; case 6 : showall(); break ; case 7 : return ; } } } //修改信息业务逻辑控制 public void modifylogiclogic(){ menu m= new menu(); telnoteregex reg= new telnoteregex(); while ( true ){ m.modifymenu(); int key=reg.menuregex( 1 , 3 ); switch (key){ case 1 : showall(); break ; case 2 : modifylogiclogic1(); break ; case 3 : return ; } } } //修改有三层跳转 public void modifylogiclogic1(){ menu m= new menu(); telnoteregex reg= new telnoteregex(); while ( true ){ m.submodifymenu(); int key=reg.menuregex( 1 , 6 ); switch (key){ case 1 : namemodify(); break ; case 2 : agemodify(); break ; case 3 : sexmodify(); break ; case 4 : telnummodify(); break ; case 5 : addressmodify(); break ; case 6 : return ; } } } //删除信息业务逻辑控制 public void deletelogic(){ menu m= new menu(); telnoteregex reg= new telnoteregex(); while ( true ){ m.deletemenu(); int key=reg.menuregex( 1 , 4 ); switch (key){ case 1 : showall(); break ; case 2 : delete(); break ; case 3 : deleteall(); break ; case 4 : return ; } } } //排序信息业务逻辑控制 public void orderlogic(){ menu m= new menu(); telnoteregex reg= new telnoteregex(); while ( true ){ m.ordermenu(); int key=reg.menuregex( 1 , 5 ); switch (key){ case 1 : ordername(); break ; case 2 : orderage(); break ; case 3 : ordersex(); break ; case 4 : showall(); break ; case 5 : return ; } } } //添加新用户信息 @suppresswarnings ( "resource" ) public void addoperation (){ telnoteregex reg= new telnoteregex(); scanner sc= new scanner(system.in); system.out.println( "请输入联系人id:" ); string id=sc.nextline(); system.out.println( "请输入要添加的姓名:" ); string name=sc.nextline(); name=reg.nameregex(name); system.out.println( "请输入要添加的年龄:" ); string age=sc.nextline(); age=reg.ageregex(age); system.out.println( "请输入要添加的性别:" ); string sex=sc.nextline(); sex=reg.sexregex(sex); system.out.println( "请输入要添加的手机号码:" ); string telnum=sc.nextline(); telnum=reg.telnumregex(telnum); system.out.println( "请输入要添加的地址:" ); string address=sc.nextline(); address=reg.addressregex(address); int personid=integer.parseint(id); person p= new person(personid,name,age,sex,telnum,address); list.add(p); } // //判断序号是否已经存在插入成功失败 // @suppresswarnings("resource") // public string judgeid(string id){ // scanner sc=new scanner(system.in); // iterator<person> it=list.iterator(); // string newid=id; // while(it.hasnext()){ // person p=(person)it.next(); // if(newid.equals(p.getid())){ // system.out.println("此序号已经存在,请重新输入序号"); // newid=sc.nextline(); // continue; // }else{ // return newid; // } // } // return id; // } //查询全部用户信息 public void showall(){ system.out.println( "序号\t\t" + "姓名\t\t" + "年龄\t\t" + "性别\t\t" + "手机号\t\t" + "地址" ); iterator<person> it=list.iterator(); while (it.hasnext()){ person p=(person)it.next(); system.out.println(p.getid()+ "#" + "\t\t" +p.getname()+ "\t\t" +p.getage()+ "\t\t" +p.getsex()+ "\t\t" +p.gettelnum()+ "\t\t" +p.getaddress()); } } //按姓名查询用户信息 @suppresswarnings ( "resource" ) public void searchbyname(){ telnoteregex reg= new telnoteregex(); system.out.println( "输入你要查询的姓名:" ); scanner sc= new scanner(system.in); string name=sc.nextline(); name=reg.nameregex(name); iterator<person> it=list.iterator(); while (it.hasnext()){ person p=(person)it.next(); if (name.equals(p.getname())){ system.out.println(p.getid()+ "#" + "\t\t" +p.getname()+ "\t\t" +p.getage()+ "\t\t" +p.getsex()+ "\t\t" +p.gettelnum()+ "\t\t" +p.getaddress()); } } } //按年龄查询用户信息 @suppresswarnings ( "resource" ) public void searchbyage(){ telnoteregex reg= new telnoteregex(); system.out.println( "输入你要查询的年龄:" ); scanner sc= new scanner(system.in); string age=sc.nextline(); age=reg.ageregex(age); iterator<person> it=list.iterator(); while (it.hasnext()){ person p=(person)it.next(); if (age.equals(p.getage())){ system.out.println(p.getid()+ "#" + "\t\t" +p.getname()+ "\t\t" +p.getage()+ "\t\t" +p.getsex()+ "\t\t" +p.gettelnum()+ "\t\t" +p.getaddress()); } } } //按性别查询用户信息 @suppresswarnings ( "resource" ) public void searchbysex(){ telnoteregex reg= new telnoteregex(); system.out.println( "输入你要查询的性别:" ); scanner sc= new scanner(system.in); string sex=sc.nextline(); sex=reg.sexregex(sex); iterator<person> it=list.iterator(); while (it.hasnext()){ person p=(person)it.next(); if (sex.equals(p.getsex())){ system.out.println(p.getid()+ "#" + "\t\t" +p.getname()+ "\t\t" +p.getage()+ "\t\t" +p.getsex()+ "\t\t" +p.gettelnum()+ "\t\t" +p.getaddress()); } } } //按电话号码查询用户信息 @suppresswarnings ( "resource" ) public void searchbytelnum(){ telnoteregex reg= new telnoteregex(); system.out.println( "输入你要查询的电话号码:" ); scanner sc= new scanner(system.in); string telnum=sc.nextline(); telnum=reg.telnumregex(telnum); iterator<person> it=list.iterator(); while (it.hasnext()){ person p=(person)it.next(); if (telnum.equals(p.gettelnum())){ system.out.println(p.getid()+ "#" + "\t\t" +p.getname()+ "\t\t" +p.getage()+ "\t\t" +p.getsex()+ "\t\t" +p.gettelnum()+ "\t\t" +p.getaddress()); } } } //按地址查询用户信息 @suppresswarnings ( "resource" ) public void searchbyadd(){ telnoteregex reg= new telnoteregex(); system.out.println( "输入你要查询的地址:" ); scanner sc= new scanner(system.in); string address=sc.nextline(); address=reg.addressregex(address); iterator<person> it=list.iterator(); while (it.hasnext()){ person p=(person)it.next(); if (address.equals(p.getaddress())){ system.out.println(p.getid()+ "#" + "\t\t" +p.getname()+ "\t\t" +p.getage()+ "\t\t" +p.getsex()+ "\t\t" +p.gettelnum()+ "\t\t" +p.getaddress()); } } } // //修改指定记录信息 // public void modify(){ // system.out.println("请输入你要修改的信息:"); // // } //修改姓名信息 @suppresswarnings ( "resource" ) public void namemodify(){ telnoteregex reg= new telnoteregex(); system.out.println( "请输入你要修改的序号:" ); scanner sc= new scanner(system.in); string num=sc.nextline(); int id=integer.parseint(num); system.out.println( "请输入你要修改的姓名:" ); string name=sc.nextline(); name=reg.nameregex(name); iterator<person> it=list.iterator(); while (it.hasnext()){ person p=(person)it.next(); if (id==p.getid()){ p.setname(name); system.out.println(p.getid()+ "#" + "\t\t" +p.getname()+ "\t\t" +p.getage()+ "\t\t" +p.getsex()+ "\t\t" +p.gettelnum()+ "\t\t" +p.getaddress()); } else { system.out.println( "修改失败,请重试!" ); } } } //修改年龄信息 @suppresswarnings ( "resource" ) public void agemodify(){ telnoteregex reg= new telnoteregex(); system.out.println( "请输入你要修改的序号:" ); scanner sc= new scanner(system.in); string num=sc.nextline(); int id=integer.parseint(num); system.out.println( "请输入你要修改的年龄:" ); string age=sc.nextline(); age=reg.ageregex(age); iterator<person> it=list.iterator(); while (it.hasnext()){ person p=(person)it.next(); if (id==p.getid()){ p.setage(age); system.out.println(p.getid()+ "#" + "\t\t" +p.getname()+ "\t\t" +p.getage()+ "\t\t" +p.getsex()+ "\t\t" +p.gettelnum()+ "\t\t" +p.getaddress()); } else { system.out.println( "修改失败,请重试!" ); } } } //修改性别信息 @suppresswarnings ( "resource" ) public void sexmodify(){ telnoteregex reg= new telnoteregex(); system.out.println( "请输入你要修改的序号:" ); scanner sc= new scanner(system.in); string num=sc.nextline(); int id=integer.parseint(num); system.out.println( "请输入你要修改的性别:" ); string sex=sc.nextline(); sex=reg.sexregex(sex); iterator<person> it=list.iterator(); while (it.hasnext()){ person p=(person)it.next(); if (id==p.getid()){ p.setsex(sex); system.out.println(p.getid()+ "#" + "\t\t" +p.getname()+ "\t\t" +p.getage()+ "\t\t" +p.getsex()+ "\t\t" +p.gettelnum()+ "\t\t" +p.getaddress()); } else { system.out.println( "修改失败,请重试!" ); } } } //修改号码信息 @suppresswarnings ( "resource" ) public void telnummodify(){ system.out.println( "请输入你要修改的序号:" ); scanner sc= new scanner(system.in); string num=sc.nextline(); int id=integer.parseint(num); system.out.println( "请输入你要修改的电话号码:" ); string telnum=sc.nextline(); iterator<person> it=list.iterator(); while (it.hasnext()){ person p=(person)it.next(); if (id==p.getid()){ p.settelnum(telnum); system.out.println(p.getid()+ "#" + "\t\t" +p.getname()+ "\t\t" +p.getage()+ "\t\t" +p.getsex()+ "\t\t" +p.gettelnum()+ "\t\t" +p.getaddress()); } else { system.out.println( "修改失败,请重试!" ); } } } //修改地址信息 @suppresswarnings ( "resource" ) public void addressmodify(){ telnoteregex reg= new telnoteregex(); system.out.println( "请输入你要修改的序号:" ); scanner sc= new scanner(system.in); string num=sc.nextline(); int id=integer.parseint(num); system.out.println( "请输入你要修改的地址:" ); string address=sc.nextline(); address=reg.addressregex(address); iterator<person> it=list.iterator(); while (it.hasnext()){ person p=(person)it.next(); if (id==p.getid()){ p.setaddress(address); system.out.println(p.getid()+ "#" + "\t\t" +p.getname()+ "\t\t" +p.getage()+ "\t\t" +p.getsex()+ "\t\t" +p.gettelnum()+ "\t\t" +p.getaddress()); } else { system.out.println( "修改失败,请重试!" ); } } } //删除指定用户信息 @suppresswarnings ( "resource" ) public void delete(){ system.out.println( "请输入你要删除的id:" ); scanner sc= new scanner(system.in); string num=sc.nextline(); int id=integer.parseint(num); iterator<person> it=list.iterator(); while (it.hasnext()){ person p=(person)it.next(); if (id==p.getid()){ list.remove(id); system.out.println( "删除成功" ); } else { system.out.println( "删除失败" ); } } } //删除全部用户信息 public void deleteall(){ list.clear(); system.out.println( "删除成功" ); } //按用户姓名排序信息 public void ordername(){ comparator<person> comparator= new comparator<person>(){ @override public int compare(person o1, person o2) { return o1.getname().compareto(o2.getname()); } }; collections.sort(list,comparator); showall(); } //按用户年龄排序信息 public void orderage(){ comparator<person> comparator= new comparator<person>(){ @override public int compare(person o1, person o2) { return o1.getage().compareto(o2.getage()); } }; collections.sort(list,comparator); showall(); } //按用户性别排序信息 public void ordersex(){ comparator<person> comparator= new comparator<person>(){ @override public int compare(person o1, person o2) { return o1.getsex().compareto(o2.getsex()); } }; collections.sort(list,comparator); showall(); } } |
总结:这个项目让我很清晰可以明白什么是面向对象,为什么说是java是面向对象的编程语言,程序的代码都是对象在调用,也明白一个项目应该如何分工明确进行书写,功能和视图要分开,每个类都有自己特定的功能,各个类的功能不要相互卸载一起,这样的代码基本是复用不高的,如果分开明确,以后再添加功能模块的时候,只需要增加功能,无需修改之前的代码,很好的体现了开闭原则,对功能开发,对修改内容关闭,这个项目也体现了mvc的设计模式,对我而言,这个项目让我受益匪浅。
原文链接:http://blog.csdn.net/chai_ge_ge/article/details/77110632