前言:小刘之前用JAVA写了一个通讯录管理系统具体描述如下;
系统功能说明
系统功能说明:
初始化功能后,根据命令指令提示操作相应的功能。
1.导入初始数据
根据模板文件,将模板文件的数据导入到系统中
2.显示信息
展示系统中通讯录列表数据
3.输入记录
根据提示将输入的姓名,手机号,邮件,年龄,性别,地址等信息添加到系统中
4.删除记录
根据通讯录列表记录的编号将系统中该记录删除
5.查询记录
根据提示的搜索条件进行按照姓名或者手机号的维度进行搜索查询
6.编辑记录
根据提示按照通讯录的编号查询此记录并且按照提示进行编辑操作
7.去重
根据提示的去重条件进行按照姓名或者手机号的维度进行去重
8.批量导出数据
将系统中的数据批量全部导出到模板导出文件中
9.退出
退出系统程序
二、 开发环境
1.操作系统:Windows 10
2.开发工具:IntelliJ IDEA 2021.3
3.编程语言:java
4.开发运行:jdk1.8
三、系统主要功能运行截图
1.导入模板文件
2.导入初始数据并显示数据
3.输入记录并显示数据
4.编辑记录并显示数据
5.删除数据并显示数据
6.查询数据并显示数据
按姓名查询
按手机查询
7.去重数据并显示数据
按姓名去重
按手机号去重
8.导出数据到模板文件
源码如下
1./**
2. * 通讯录管理系统实体
3. */
4.public class AddressList {
5. /**
6. * 编号 自增
7. */
8. private Integer id;
9. /**
10. * 姓名
11. */
12. private String name;
13. /**
14. * 手机号
15. */
16. private String phone;
17. /**
18. * 年龄
19. */
20. private Integer age;
21. /**
22. * 性别 男/女
23. */
24. private String sex;
25. /**
26. * 邮箱
27. */
28. private String email;
29. /**
30. * 住址
31. */
32. private String address;
33.
34.
35. public AddressList() {
36. }
37.
38. public AddressList(Integer id, String name, String phone, Integer age, String sex, String email, String address) {
39. this.id = id;
40. this.name = name;
41. this.phone = phone;
42. this.age = age;
43. this.sex = sex;
44. this.email = email;
45. this.address = address;
46. }
47.
48. public Integer getId() {
49. return id;
50. }
51.
52. public void setId(Integer id) {
53. this.id = id;
54. }
55.
56. public String getName() {
57. return name;
58. }
59.
60. public void setName(String name) {
61. this.name = name;
62. }
63.
64. public String getPhone() {
65. return phone;
66. }
67.
68. public void setPhone(String phone) {
69. this.phone = phone;
70. }
71.
72. public Integer getAge() {
73. return age;
74. }
75.
76. public void setAge(Integer age) {
77. this.age = age;
78. }
79.
80. public String getSex() {
81. return sex;
82. }
83.
84. public void setSex(String sex) {
85. this.sex = sex;
86. }
87.
88. public String getEmail() {
89. return email;
90. }
91.
92. public void setEmail(String email) {
93. this.email = email;
94. }
95.
96. public String getAddress() {
97. return address;
98. }
99.
100. public void setAddress(String address) {
101. this.address = address;
102. }
103.
104. @Override
105. public String toString() {
106. return "AddressList{" +
107. "id=" + id +
108. ", name='" + name + '\'' +
109. ", phone='" + phone + '\'' +
110. ", age=" + age +
111. ", sex='" + sex + '\'' +
112. ", email='" + email + '\'' +
113. ", address='" + address + '\'' +
114. '}';
115. }
116.}
1.import java.io.*;
2.import java.util.*;
3.import java.util.stream.Collectors;
4.
5.import static java.util.stream.Collectors.collectingAndThen;
6.import static java.util.stream.Collectors.toCollection;
7.
8./**
9. * 通讯录管理系统主类
10. */
11.public class AddressListMain {
12. private static Integer index=0; //自增的编号
13. private static String header=""; //表格表头
14. private static List<AddressList> list = new ArrayList<>(); //存储数据列表
15. private static String importPath= "src\\import.txt";//导入模板的路径
16. private static String exportPath="src\\export.txt";//导出模板的路径
17.
18. public static void main(String[] args) {
19. initMenu();//初始化菜单
20. int a;// 定义switch语句变量
21. Scanner in = new Scanner(System.in); // 实例化输入流对象
22. while (in.hasNext()) {
23. a = in.nextInt();
24. while (a < 0 || a > 8) {
25. System.out.print("输入超出范围,请重新输入:");
26. a = in.nextInt();
27. }
28. switch (a) {
29. case 0:
30. System.exit(0); // 退出
31. break;
32. case 1:
33. importInfo(); //导入初始数据
34. break;
35. case 2:
36. showTable(); //显示信息
37. break;
38. case 3:
39. add(); //输入记录
40. break;
41. case 4:
42. del(); //删除记录
43. break;
44. case 5:
45. select(); //查询记录
46. break;
47. case 6:
48. edit(); //编辑记录
49. break;
50. case 7:
51. deWeight(); //去重
52. break;
53. case 8:
54. exportInfo(); //批量导出数据
55. break;
56. }
57. System.out.println("请按任意键继续......");
58. Scanner atWill = new Scanner(System.in);
59. atWill.nextLine();
60. initMenu();
61. }
62.
63.
64.
65. }
66.
67. /**
68. * 删除记录
69. */
70. public static void del(){
71. System.out.println("请输入要删除的记录编号:");
72. Scanner in =new Scanner(System.in);
73. String next = in.next();
74. delById(Integer.valueOf(next));
75. System.out.println("======================删除成功========================");
76. }
77.
78. /**
79. * 按照记录编号进行删除数据
80. * @param id
81. */
82. public static void delById(Integer id){
83. Iterator<AddressList> iterator = list.iterator();
84. while (iterator.hasNext()){
85. AddressList list = iterator.next();
86. if (list.getId()==Integer.valueOf(id)){
87. iterator.remove();
88. }
89. }
90. }
91. /**
92. * 查询记录
93. */
94. public static void select(){
95. System.out.println("请输入要查看搜索的维度:a:姓名;b:手机号");
96. System.out.print("请输入a或者b:");
97. Scanner in =new Scanner(System.in);
98. String next = in.next();
99. if (next.equals("a")){
100. System.out.print("请输入姓名:");
101. String s = in.next();
102. getListByName(s);
103. }else if (next.equals("b")){
104. System.out.print("请输入手机号:");
105. String s = in.next();
106. getListByPhone(s);
107. }else {
108. System.out.println("请按流程输入相应的关键字;");
109. }
110. }
111.
112. /**去重
113. * 按照提示的字段去重
114. */
115. public static void deWeight(){
116. System.out.println("请输入要查要去重的维度:a:姓名;b:手机号");
117. Scanner in =new Scanner(System.in);
118. String next = in.next();
119. if (next.equals("a")){
120. ArrayList<AddressList> collect = list.stream().collect(
121. collectingAndThen(
122. toCollection(() -> new TreeSet<>(Comparator.comparing(AddressList::get Name))), ArrayList::new)
123. );
124. showTableListBySelect(collect);
125. }else if (next.equals("b")){
126. ArrayList<AddressList> collect = list.stream().collect(
127. collectingAndThen(
128. toCollection(() -> new TreeSet<>(Comparator.comparing(AddressList::getPhone))), ArrayList::new)
129. );
130. showTableListBySelect(collect);
131. }
132. }
133.
134. /**
135. * 编辑记录
136. */
137. public static void edit(){
138. System.out.println("请输入要编辑的记录编号:");
139. Scanner in =new Scanner(System.in);
140. String next = in.next();
141. boolean b = getOneById(Integer.valueOf(next));
142. if (!b){
143. return;
144. }
145. System.out.println("请根据流程进行修改该记录");
146. String s = "";
147. String msg = "";
148.
149. AddressList c = new AddressList();
150.
151. msg = "请输入姓名:";
152. System.out.print(msg);
153. s = in.next();
154. c.setName(s);
155.
156. msg = "请输入手机号:";
157. System.out.print(msg);
158. s = in.next();
159. c.setPhone(s);
160.
161.
162.
163. msg = "请输入年龄:";
164. System.out.print(msg);
165. s = in.next();
166. c.setAge(Integer.valueOf(s));
167.
168. msg = "请输入性别:";
169. System.out.print(msg);
170. s = in.next();
171. c.setSex(s);
172.
173. msg = "请输入邮箱:";
174. System.out.print(msg);
175. s = in.next();
176. c.setEmail(s);
177.
178. msg = "请输入地址:";
179. System.out.print(msg);
180. s = in.next();
181. c.setAddress(s);
182. c.setId(Integer.valueOf(next));
183.
184.
185. delById(c.getId());
186. list.add(c);
187. showTable();
188. }
189.
190. /**
191. * 按编号id搜索获取该记录
192. * @param id
193. */
194. public static boolean getOneById(Integer id){
195. AddressList e=null;
196. for (int i=0;i<list.size();i++){
197. AddressList list = AddressListMain.list.get(i);
198. if (list.getId().equals(id)){
199. e=list;
200. }
201. }
202. if (e==null){
203. System.out.println("请输入有效的通讯录记录编号");
204. return false;
205. }
206. List<AddressList>tem=new ArrayList<>();
207. tem.add(e);
208. showTableListBySelect(tem);
209. return true;
210. }
211.
212. /**
213. * 按名称搜索获取记录列表
214. */
215. public static void getListByName(String name){
216. List<AddressList> collect = list.stream().filter(t -> t.getName().contains(name)).collect(Collectors.toList());
217. showTableListBySelect(collect);
218. }
219.
220. /**
221. * 按手机号搜索获取记录列表
222. */
223. public static void getListByPhone(String phone){
224. List<AddressList> collect = list.stream().filter(t -> t.getPhone().contains(phone)).collect(Collectors.toList());
225. showTableListBySelect(collect);
226. }
227.
228. /**
229. * 批量导出记录
230. */
231. public static void exportInfo(){
232. try {
233. BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(exportPath,true));
234. bufferedWriter.write(header+"\n");
235. list.stream().forEach(one->{
236.
237. String tr =one.getId()+"\t\t"+one.getName() + one.getPhone() + "\t\t" + one.getAge() + "\t\t" + one.getSex() + "\t\t" + one.getEmail() + one.getAddress();
238. tr+="\n";
239. try {
240. bufferedWriter.write(tr);
241. } catch (IOException e) {
242. e.printStackTrace();
243. }
244. });
245.
246. bufferedWriter.flush();//刷新缓冲区
247. bufferedWriter.close();
248.
249. } catch (IOException e) {
250. e.printStackTrace();
251. }
252.
253. System.out.println("================批量数据导出成功===================");
254. }
255.
256.
257. /**
258. * 按模板导入数据
259. */
260. public static void importInfo(){
261. BufferedReader bufferedReader = null;//字符读入缓冲流
262. try {
263. bufferedReader = new BufferedReader(new FileReader(importPath));
264. String input = "";
265. int indexTem=0;
266. while ((input = bufferedReader.readLine()) != null) {
267. if (indexTem!=0){
268. String [] arr = input.split("\\s+");
269. AddressList one=new AddressList(++index,arr[0],arr[1],Integer.valueOf(arr[2]),arr[3],arr[4],arr[5]);
270. list.add(one);
271. }
272. indexTem++;
273. }
274. bufferedReader.close();
275. } catch (FileNotFoundException e) {
276. e.printStackTrace();
277. } catch (IOException e) {
278. e.printStackTrace();
279. }
280.
281. System.out.println("=====================数据导入成功===============");
282.
283. }
284.
285. /**
286. * 输入记录
287. */
288. public static void add() {
289. String s = "";
290. String msg = "";
291. Scanner in = new Scanner(System.in);
292. AddressList c = new AddressList();
293.
294. msg = "请输入手机号:";
295. System.out.print(msg);
296. s = in.next();
297. c.setPhone(s);
298.
299. msg = "请输入姓名:";
300. System.out.print(msg);
301. s = in.next();
302. c.setName(s);
303.
304. msg = "请输入年龄:";
305. System.out.print(msg);
306. s = in.next();
307. c.setAge(Integer.valueOf(s));
308.
309. msg = "请输入性别:";
310. System.out.print(msg);
311. s = in.next();
312. c.setSex(s);
313.
314. msg = "请输入邮箱:";
315. System.out.print(msg);
316. s = in.next();
317. c.setEmail(s);
318.
319. msg = "请输入地址:";
320. System.out.print(msg);
321. s = in.next();
322. c.setAddress(s);
323.
324. c.setId(++index);
325. list.add(c);
326. System.out.println("======================记录新增成功======================");
327. }
328.
329. /***
330. * 初始化菜单
331. */
332. public static void initMenu() {
333. System.out.println("*************通讯录管理系统功能表**************");
334. System.out.println("***** 1.导入初始数据 *****");
335. System.out.println("***** 2.显示信息 *****");
336. System.out.println("***** 3.输入记录 *****");
337. System.out.println("***** 4.删除记录 *****");
338. System.out.println("***** 5.查询记录 *****");
339. System.out.println("***** 6.编辑记录 *****");
340. System.out.println("***** 7.去重 *****");
341. System.out.println("***** 8.批量导出数据 *****");
342. System.out.println("***** 0.退出 *****");
343. System.out.println("*******************************************");
344. System.out.print("请选择(0~8):");
345. }
346.
347.
348.
349. /**
350. * 显示表格内容
351. */
352. public static void showTable(){
353. initHeader();
354. initTable(list);
355. sortList();
356. for (int i=0;i<list.size();i++){
357. System.out.println(getTr(list.get(i)));
358. }
359. }
360.
361. /**
362. * 获取表格tr数据
363. * @param one
364. * @return
365. */
366. public static String getTr(AddressList one){
367. String s = one.getId() + "\t\t" + one.getName() + one.getPhone() + "\t\t" + one.getAge() + "\t\t" + one.getSex() + "\t\t" + one.getEmail() + one.getAddress();
368. return s;
369. }
370.
371. /**
372. * 按照搜索条件获取展示数据
373. */
374. public static void showTableListBySelect(List<AddressList>list){
375. initHeader();
376. initTable(list);
377. for (int i=0;i<list.size();i++){
378. System.out.println(list.get(i).getId()+"\t\t"+list.get(i).getName()+list.get(i).getPhone() +"\t\t"+list.get(i).getAge()+"\t\t"+list.get(i).getSex()+"\t\t"+list.get(i).getEmail()+list.get(i).getAddress() );
379. }
380. }
381.
382. /**
383. * 排序
384. */
385. public static void sortList(){
386. List<AddressList> collect = list.stream().sorted(Comparator.comparing(AddressList::getId)).collect(Collectors.toList());
387. list=collect;
388. }
389.
390. /**
391. * 初始化表头
392. */
393. public static void initHeader(){
394. if (header==""){
395.
396. List<String> asList = Arrays.asList("编号\t\t","姓名", "手机号", "\t年龄", "\t\t性别", "\t\t邮箱", "地址");
397.
398. asList.stream().forEach(t->{
399. if (t.contains("姓名")){
400. if (t.length()<10){
401. String s="";
402. for (int j=t.length();j<10;j++){
403. s+=" ";
404. }
405. t=t+s;
406. }
407. }
408. if (t.contains("手机号")){
409. if (t.length()<11){
410. String s="";
411. for (int j=t.length();j<11;j++){
412. s+=" ";
413. }
414. t=t+s;
415. }
416. }
417. if (t.contains("邮箱")){
418. if (t.length()<20){
419. String s="";
420. for (int j=t.length();j<20;j++){
421. s+=" ";
422. }
423. t=t+s;
424. }
425. }
426. header+=t;
427. });
428. }
429. System.out.println(header);
430. }
431.
432. /**
433. * 初始化表格内容
434. * @param lis
435. */
436. public static void initTable(List<AddressList>lis){
437. for (int i=0;i<lis.size();i++){
438. AddressList list = lis.get(i);
439. String name = list.getName();
440. String phone = list.getPhone();
441. String email = list.getEmail();
442. String age=String.valueOf(list.getAge());
443. String sex=list.getSex();
444.
445. if (name.length()<10){
446. String s="";
447. for (int j=name.length();j<10;j++){
448. s+=" ";
449. }
450. list.setName(list.getName()+s);
451. }
452.
453. if (phone.length()<11){
454. String s="";
455. for (int j=phone.length();j<11;j++){
456. s+=" ";
457. }
458. list.setPhone(list.getPhone()+s);
459. }
460.
461. if (email.length()<20){
462. String s="";
463. for (int j=email.length();j<20;j++){
464. s+=" ";
465. }
466. list.setEmail(list.getEmail()+s);
467. }
468.
469. }
470. }
471.}