国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Java實(shí)現(xiàn)簡單通訊錄管理系統(tǒng)

瀏覽:42日期:2023-02-08 17:42:31

本文實(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)系人

代碼分析:

之前寫過類似的管理系統(tǒng),不過是使用數(shù)組進(jìn)行數(shù)據(jù)存儲(chǔ),這次的通訊錄管理系統(tǒng)通過動(dòng)態(tài)數(shù)組

ArrayList進(jìn)行數(shù)據(jù)存儲(chǔ)。其中代碼實(shí)現(xiàn)的原理和之前所寫相似。在此不再贅述。

判斷手機(jī)號(hào)郵箱地址格式是否格式正確使用了正則表達(dá)式進(jìn)行判斷,如果輸入錯(cuò)誤則輸出提示語句,并重新輸入正確格式,遞歸實(shí)現(xiàn)。

其中修改手機(jī)號(hào)的方法和刪除用戶類似,順帶寫了一下,沒有進(jìn)行實(shí)現(xiàn),感興趣的朋友可以自己進(jìn)行實(shí)現(xiàn)測試一下。

代碼實(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); }}

測試類:

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】通過聯(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('請(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(); }}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 沿河| 宿州市| 铁岭县| 老河口市| 南城县| 景德镇市| 鸡东县| 十堰市| 盐边县| 壶关县| 广昌县| 丰城市| 惠来县| 东安县| 中宁县| 福海县| 瓮安县| 依兰县| 嘉荫县| 宁夏| 灵武市| 上林县| 绍兴市| 清水河县| 崇左市| 湘西| 温州市| 宜春市| 安塞县| 林西县| 霍邱县| 郎溪县| 新余市| 惠来县| 湘阴县| 石门县| 开封市| 博白县| 昭平县| 文水县| 吴旗县|