Java實現簡單通訊錄管理系統(tǒng)
本文實例為大家分享了Java實現通訊錄管理系統(tǒng)的具體代碼,供大家參考,具體內容如下
題目:1、完成一個通訊錄,需求:
(1)添加聯(lián)系人(聯(lián)系人:編號,姓名,手機號,QQ,郵箱地址)添加時需要檢查手機號和郵箱地址格式是否正確,若不正確,不允許添加
(2)聯(lián)系人查詢(輸入姓名或電話查詢)
(3)顯示聯(lián)系人列表
(4)根據編號刪除指定編號的聯(lián)系人
代碼分析:之前寫過類似的管理系統(tǒng),不過是使用數組進行數據存儲,這次的通訊錄管理系統(tǒng)通過動態(tài)數組
ArrayList進行數據存儲。其中代碼實現的原理和之前所寫相似。在此不再贅述。
判斷手機號郵箱地址格式是否格式正確使用了正則表達式進行判斷,如果輸入錯誤則輸出提示語句,并重新輸入正確格式,遞歸實現。
其中修改手機號的方法和刪除用戶類似,順帶寫了一下,沒有進行實現,感興趣的朋友可以自己進行實現測試一下。
代碼實現:用戶類:
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 + ’’’ +’}’; }}
用戶管理類:
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('該用戶不存在'); 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('該用戶不存在'); return false;}return s.remove(user); }}
測試類:
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)系人查詢');System.out.println('【3】顯示聯(lián)系人列表');System.out.println('【4】根據編號刪除指定編號的聯(lián)系人');System.out.println('=============================');int i = sc.nextInt();switch (i){ case 1:add();start();break; case 2:System.out.println('【1】通過聯(lián)系人姓名查詢/【2】通過聯(lián)系人電話查詢');int a = sc.nextInt();findbyName(a);start();break; case 3:show();start();break; case 4:del();start();break; case 0:System.out.println('謝謝使用,再見!');System.exit(0);break; default:System.out.println('請輸入正確的指令!');start();break;} } public static void add(){System.out.println('請輸入聯(lián)系人編號:');int a = sc.nextInt();System.out.println('請輸入聯(lián)系人姓名:');String b = sc.next();System.out.println('請輸入聯(lián)系人手機號:');String c = sc.next();judgePhone(c);System.out.println('請輸入聯(lián)系人QQ:');String d = sc.next();System.out.println('請輸入聯(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('手機號輸入有誤,請重新輸入'); 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('郵箱格式輸入有誤,請重新輸入'); String v = sc.next(); judgeEmail(v);} } public static void findbyName(int a){if (a==1){ System.out.println('請輸入聯(lián)系人姓名');}else { System.out.println('請輸入聯(lián)系人電話');}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('請輸入編號');int no = sc.nextInt();if(user.delUser(no)){ System.out.println('刪除成功');} } public static void main(String[] args) {start(); }}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: