通讯录管理系统JAVA版本
前言:小劉之前用JAVA寫了一個通訊錄管理系統(tǒng)具體描述如下;
系統(tǒng)功能說明
系統(tǒng)功能說明:
初始化功能后,根據(jù)命令指令提示操作相應的功能。
1.導入初始數(shù)據(jù)
根據(jù)模板文件,將模板文件的數(shù)據(jù)導入到系統(tǒng)中
2.顯示信息
展示系統(tǒng)中通訊錄列表數(shù)據(jù)
3.輸入記錄
根據(jù)提示將輸入的姓名,手機號,郵件,年齡,性別,地址等信息添加到系統(tǒng)中
4.刪除記錄
根據(jù)通訊錄列表記錄的編號將系統(tǒng)中該記錄刪除
5.查詢記錄
根據(jù)提示的搜索條件進行按照姓名或者手機號的維度進行搜索查詢
6.編輯記錄
根據(jù)提示按照通訊錄的編號查詢此記錄并且按照提示進行編輯操作
7.去重
根據(jù)提示的去重條件進行按照姓名或者手機號的維度進行去重
8.批量導出數(shù)據(jù)
將系統(tǒng)中的數(shù)據(jù)批量全部導出到模板導出文件中
9.退出
退出系統(tǒng)程序
二、 開發(fā)環(huán)境
1.操作系統(tǒng):Windows 10
2.開發(fā)工具:IntelliJ IDEA 2021.3
3.編程語言:java
4.開發(fā)運行:jdk1.8
三、系統(tǒng)主要功能運行截圖
1.導入模板文件
2.導入初始數(shù)據(jù)并顯示數(shù)據(jù)
3.輸入記錄并顯示數(shù)據(jù)
4.編輯記錄并顯示數(shù)據(jù)
5.刪除數(shù)據(jù)并顯示數(shù)據(jù)
6.查詢數(shù)據(jù)并顯示數(shù)據(jù)
按姓名查詢
按手機查詢
7.去重數(shù)據(jù)并顯示數(shù)據(jù)
按姓名去重
按手機號去重
8.導出數(shù)據(jù)到模板文件
源碼如下
1./** 2. * 通訊錄管理系統(tǒng)實體 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. * 通訊錄管理系統(tǒng)主類 10. */ 11.public class AddressListMain { 12. private static Integer index=0; //自增的編號 13. private static String header=""; //表格表頭 14. private static List<AddressList> list = new ArrayList<>(); //存儲數(shù)據(jù)列表 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(); //導入初始數(shù)據(jù) 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(); //批量導出數(shù)據(jù) 55. break; 56. } 57. System.out.println("請按任意鍵繼續(xù)......"); 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. * 按照記錄編號進行刪除數(shù)據(jù) 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("請根據(jù)流程進行修改該記錄"); 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();//刷新緩沖區(qū) 247. bufferedWriter.close(); 248. 249. } catch (IOException e) { 250. e.printStackTrace(); 251. } 252. 253. System.out.println("================批量數(shù)據(jù)導出成功==================="); 254. } 255. 256. 257. /** 258. * 按模板導入數(shù)據(jù) 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("=====================數(shù)據(jù)導入成功==============="); 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("*************通訊錄管理系統(tǒng)功能表**************"); 334. System.out.println("***** 1.導入初始數(shù)據(jù) *****"); 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.批量導出數(shù)據(jù) *****"); 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數(shù)據(jù) 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. * 按照搜索條件獲取展示數(shù)據(jù) 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.}有啥不懂得小伙伴們加群交流啦:852665736
無償免費分享源碼以及技術和面試文檔,更多優(yōu)秀精致的源碼技術棧分享請關注微信公眾號:gh_817962068649
總結
以上是生活随笔為你收集整理的通讯录管理系统JAVA版本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html国庆节代码,QQ空间国庆节留言代
- 下一篇: java 仿百度文库源码_Java模拟实