Java實(shí)現(xià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)系人
代碼分析:
之前寫過類似的管理系統(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("請輸入正確的指令!"); start(); break; } } public static void add(){ System.out.println("請輸入聯(lián)系人編號(hào):"); int a = sc.nextInt(); System.out.println("請輸入聯(lián)系人姓名:"); String b = sc.next(); System.out.println("請輸入聯(lián)系人手機(jī)號(hào):"); 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("手機(jī)號(hào)輸入有誤,請重新輸入"); 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("請輸入編號(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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中有界隊(duì)列的飽和策略(reject policy)原理解析
這篇文章主要介紹了Java中有界隊(duì)列的飽和策略(reject policy)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04restTemplate未設(shè)置連接數(shù)導(dǎo)致服務(wù)雪崩問題以及解決
面對(duì)線上問題,仔細(xì)分析原因,及時(shí)調(diào)整配置,能有效解決問題,本文詳細(xì)描述了線上遇到流量突增引發(fā)的問題,通過查看代碼和連接池信息,分析出問題的原因是連接池滿了,連接池大小配置不足以應(yīng)對(duì)大并發(fā)流量,通過調(diào)整連接池大小配置2024-10-10Java?Jar包項(xiàng)目內(nèi)存設(shè)置方法舉例
這篇文章主要給大家介紹了關(guān)于Java?Jar包項(xiàng)目內(nèi)存設(shè)置方法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01java中Collections.sort排序函數(shù)用法詳解
本篇文章主要介紹了java中Collections.sort排序函數(shù)用法詳解,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12