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)系人查詢(輸入姓名或電話查詢)
(3)顯示聯(lián)系人列表
(4)根據(jù)編號(hào)刪除指定編號(hào)的聯(lián)系人
代碼分析:之前寫過(guò)類似的管理系統(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)的原理和之前所寫相似。在此不再贅述。
判斷手機(jī)號(hào)郵箱地址格式是否格式正確使用了正則表達(dá)式進(jìn)行判斷,如果輸入錯(cuò)誤則輸出提示語(yǔ)句,并重新輸入正確格式,遞歸實(shí)現(xiàn)。
其中修改手機(jī)號(hào)的方法和刪除用戶類似,順帶寫了一下,沒有進(jìn)行實(shí)現(xiàn),感興趣的朋友可以自己進(jìn)行實(shí)現(xiàn)測(cè)試一下。
代碼實(shí)現(xiàn):用戶類:
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); }}
測(cè)試類:
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】根據(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)系人姓名查詢/【2】通過(guò)聯(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('請(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)系人電話');}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. CSS清除浮動(dòng)方法匯總2. js開發(fā)中的頁(yè)面、屏幕、瀏覽器的位置原理(高度寬度)說(shuō)明講解(附圖)3. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)4. CSS百分比padding制作圖片自適應(yīng)布局5. vue跳轉(zhuǎn)頁(yè)面常用的幾種方法匯總6. 不要在HTML中濫用div7. XML入門的常見問(wèn)題(三)8. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)9. 深入了解React中的合成事件10. TypeScript實(shí)現(xiàn)十大排序算法之歸并排序示例詳解
