Java實(shí)現(xiàn)簡(jiǎn)單通訊錄管理系統(tǒng)
本文實(shí)例為大家分享了Java實(shí)現(xiàn)通訊錄管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
題目:1、完成一個(gè)通訊錄,需求:
(1)添加聯(lián)系人(聯(lián)系人:編號(hào),姓名,手機(jī)號(hào),QQ,郵箱地址)添加時(shí)需要檢查手機(jī)號(hào)和郵箱地址格式是否正確,若不正確,不允許添加
(2)聯(lián)系人查詢(xún)(輸入姓名或電話(huà)查詢(xún))
(3)顯示聯(lián)系人列表
(4)根據(jù)編號(hào)刪除指定編號(hào)的聯(lián)系人
代碼分析:之前寫(xiě)過(guò)類(lèi)似的管理系統(tǒng),不過(guò)是使用數(shù)組進(jìn)行數(shù)據(jù)存儲(chǔ),這次的通訊錄管理系統(tǒng)通過(guò)動(dòng)態(tài)數(shù)組
ArrayList進(jìn)行數(shù)據(jù)存儲(chǔ)。其中代碼實(shí)現(xiàn)的原理和之前所寫(xiě)相似。在此不再贅述。
判斷手機(jī)號(hào)郵箱地址格式是否格式正確使用了正則表達(dá)式進(jìn)行判斷,如果輸入錯(cuò)誤則輸出提示語(yǔ)句,并重新輸入正確格式,遞歸實(shí)現(xiàn)。
其中修改手機(jī)號(hào)的方法和刪除用戶(hù)類(lèi)似,順帶寫(xiě)了一下,沒(méi)有進(jìn)行實(shí)現(xiàn),感興趣的朋友可以自己進(jìn)行實(shí)現(xiàn)測(cè)試一下。
代碼實(shí)現(xiàn):用戶(hù)類(lèi):
package com.softeem.j2106.work; /** * @author admin * 2021/7/26 */public class User { private int no; private String name; private String phone; private String QQ; private String email; public User() { } public User(int no, String name, String phone, String QQ, String email) {this.no = no;this.name = name;this.phone = phone;this.QQ = QQ;this.email = email; } public int getNo() {return no; } public void setNo(int no) {this.no = no; } public String getName() {return name; } public void setName(String name) {this.name = name; } public String getPhone() {return phone; } public void setPhone(String phone) {this.phone = phone; } public String getQQ() {return QQ; } public void setQQ(String QQ) {this.QQ = QQ; } public String getEmail() {return email; } public void setEmail(String email) {this.email = email; } @Override public String toString() {return 'User{' +'no=' + no +', name=’' + name + ’’’ +', phone=’' + phone + ’’’ +', QQ=’' + QQ + ’’’ +', email=’' + email + ’’’ +’}’; }}
用戶(hù)管理類(lèi):
public class UserMange { static ArrayList<User> s = new ArrayList<>(); public boolean addUser(User user){return s.add(user); } public ArrayList showInfo(){return s; } public User searchByName(String name){for (User user : s) { if (Objects.equals(name,user.getName()) ||Objects.equals(name,user.getPhone())){return user; }}return null; } public boolean updatePhone(int no,String phone){User user = null;for(User u:s) { if(no == u.getNo()) {u.setPhone(phone);break; }}if(user == null) { System.out.println('該用戶(hù)不存在'); return false;}System.out.println('修改成功!');return true; } public boolean delUser(int no){User user = null;for(User u:s) { if(no == u.getNo()) {user = u;break; }}if(user == null) { System.out.println('該用戶(hù)不存在'); return false;}return s.remove(user); }}
測(cè)試類(lèi):
public class Test2 { static UserMange user = new UserMange(); static Scanner sc = new Scanner(System.in); public static void start(){System.out.println('=======SOFTEEM通訊錄管理系統(tǒng)=====');System.out.println('【1】添加聯(lián)系人');System.out.println('【2】聯(lián)系人查詢(xún)');System.out.println('【3】顯示聯(lián)系人列表');System.out.println('【4】根據(jù)編號(hào)刪除指定編號(hào)的聯(lián)系人');System.out.println('=============================');int i = sc.nextInt();switch (i){ case 1:add();start();break; case 2:System.out.println('【1】通過(guò)聯(lián)系人姓名查詢(xún)/【2】通過(guò)聯(lián)系人電話(huà)查詢(xún)');int a = sc.nextInt();findbyName(a);start();break; case 3:show();start();break; case 4:del();start();break; case 0:System.out.println('謝謝使用,再見(jiàn)!');System.exit(0);break; default:System.out.println('請(qǐng)輸入正確的指令!');start();break;} } public static void add(){System.out.println('請(qǐng)輸入聯(lián)系人編號(hào):');int a = sc.nextInt();System.out.println('請(qǐng)輸入聯(lián)系人姓名:');String b = sc.next();System.out.println('請(qǐng)輸入聯(lián)系人手機(jī)號(hào):');String c = sc.next();judgePhone(c);System.out.println('請(qǐng)輸入聯(lián)系人QQ:');String d = sc.next();System.out.println('請(qǐng)輸入聯(lián)系人郵箱地址:');String e = sc.next();judgeEmail(e);User x = new User(a,b,c,d,e);if(user.addUser(x)){ System.out.println('添加成功!');} } public static void judgePhone(String phone){ if (phone.matches('1[34589][0-9]{9}')){ }else { System.out.println('手機(jī)號(hào)輸入有誤,請(qǐng)重新輸入'); String v = sc.next(); judgePhone(v);} } public static void judgeEmail(String email){ if (email.matches('[A-Za-z0-9]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)')){ }else { System.out.println('郵箱格式輸入有誤,請(qǐng)重新輸入'); String v = sc.next(); judgeEmail(v);} } public static void findbyName(int a){if (a==1){ System.out.println('請(qǐng)輸入聯(lián)系人姓名');}else { System.out.println('請(qǐng)輸入聯(lián)系人電話(huà)');}String name = sc.next();User user = Test2.user.searchByName(name);System.out.println(user); } public static void show(){ArrayList list = user.showInfo();for (Object o : list) { System.out.println(o);} } public static void del(){System.out.println('請(qǐng)輸入編號(hào)');int no = sc.nextInt();if(user.delUser(no)){ System.out.println('刪除成功');} } public static void main(String[] args) {start(); }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車(chē)輛管理系統(tǒng)2. ASP.Net MVC利用NPOI導(dǎo)入導(dǎo)出Excel的示例代碼3. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算4. jstl 字符串處理函數(shù)5. JSP動(dòng)態(tài)網(wǎng)頁(yè)開(kāi)發(fā)原理詳解6. XHTML 1.0:標(biāo)記新的開(kāi)端7. asp與php中定時(shí)生成頁(yè)面的思路與代碼8. 帶你了解CSS基礎(chǔ)知識(shí),樣式9. React中使用TS完成父組件調(diào)用子組件的操作方法10. React實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)hook組件實(shí)戰(zhàn)示例
