java實(shí)現(xiàn)電話本系統(tǒng)
本文實(shí)例為大家分享了java模擬實(shí)現(xiàn)電話本系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
java 模擬 電話本系統(tǒng) (基礎(chǔ)版)
需求:注意的是,聯(lián)系人的id是自增長的
實(shí)現(xiàn)的功能:
java綜合編程練習(xí):基本的增刪改查,對于邏輯數(shù)據(jù)的過濾,這塊做了簡單的處理,暫不優(yōu)化
ContactPerson.java
package com.demo4; /**1、新增一個聯(lián)系人,聯(lián)系人的編號從1開始自動增長,姓名,性別,年齡,手機(jī)號, ? 身份證號由用戶從控制臺錄入*/ public class ContactPerson { ? ? /*這里用靜態(tài)標(biāo)識,di為自增長*/ ? ? private ?static int ? count=0; ? ? private ?int id; ? ? private ?String name; ? ? private ?String sex; ? ? private ?int age; ? ? private ?String phoneNo; ? ? private ?String IdCard; ? ? public ContactPerson(){} ? ? public ContactPerson(String name, String sex, int age, String phoneNo, String idCard) { ? ? ? ? ? ? this.name = name; ? ? ? ? ? ? this.sex = sex; ? ? ? ? ? ? this.age = age; ? ? ? ? ? ? this.phoneNo = phoneNo; ? ? ? ? ? ? IdCard = idCard; ? ? ? ? ? ? /*靜態(tài)id為自增長*/ ? ? ? ? ? ? this.id = ++count; ? ? } ? ? public int getId() { ? ? ? ? return id; ? ? } ? ? public String getName() { ? ? ? ? return name; ? ? } ? ? public void setName(String name) { ? ? ? ? this.name = name; ? ? } ? ? public String getSex() { ? ? ? ? return sex; ? ? } ? ? public void setSex(String sex) { ? ? ? ? this.sex = sex; ? ? } ? ? public int getAge() { ? ? ? ? return age; ? ? } ? ? public void setAge(int age) { ? ? ? ? this.age = age; ? ? } ? ? public String getPhoneNo() { ? ? ? ? return phoneNo; ? ? } ? ? public void setPhoneNo(String phoneNo) { ? ? ? ? this.phoneNo = phoneNo; ? ? } ? ? public String getIdCard() { ? ? ? ? return IdCard; ? ? } ? ? public void setIdCard(String idCard) { ? ? ? ? IdCard = idCard; ? ? } ? ? public static int getCount() { ? ? ? ? return count; ? ? } ? ? public void ?show() { ? ? ? ? System.out.println( ? ? ? ? ? ? ? ?"【 編號 =" + id + " ?姓名=" + name + ", 性別=" + sex + ", 年齡=" + age + ", 電話號碼=" + phoneNo + ", 證件號=" + IdCard +" 】" ); ? ? ? } }
PhoneBook.java
package com.demo4; import java.util.Arrays; public class PhoneBook { ? ? private ?ContactPerson[] contactPersons ?; ? ? private ?int size; ? ? public PhoneBook(){ ? ? ? ? contactPersons= new ContactPerson[20]; ? ? ? ? size=0; ? ? } ? ? ? ? //1. 新增一個聯(lián)系人 ? ? ? ? public void ?addContPerson(String name,String sex,int age, String phoneNo, String IdCard){ ? ? ? ? ? ContactPerson person = new ContactPerson(name,sex,age,phoneNo,IdCard); ? ? ? ? ? contactPersons[size++]= person; ? ? ? ? ? ? System.out.println("添加成功"); ? ? ? ? } ? ? ? ? // 2.查找指定的聯(lián)系人 ? ? ?public ContactPerson[] findByName(String name){ ? ? ? ? ContactPerson[] newContPersons = new ContactPerson[size]; ? ? ? ? int length=0; ? ? ? ? int i; ? ? ? ? for(i=0; i<size; i++){ ? ? ? ? ? ? if(contactPersons[i].getName().equals(name)){ ? ? ? ? ? ? ? ? newContPersons[length++] =contactPersons[i]; ? ? ? ? ? ? } ? ? ? ? ? ? i++; ? ? ? ? } ? ? ? ? newContPersons = Arrays.copyOf(newContPersons,length); ? ? ? ? return newContPersons; ? ? ?} ? ? ? ? //3.查找所有的聯(lián)系人 ? ? ? public void ? findAll(){ ? ? ? ? for(int i=0; i<size;i++){ ? ? ? ? ? ? contactPersons[i].show(); ? ? ? ? } ? ? ? } ? ? ? ?//4. 修改聯(lián)系人信息 ? ? ? ? public void modfyMesById(int id,String phoneNo){ ? ? ? ? int i; ? ? ? ? for(i=0; i<size; i++){ ? ? ? ? ? ? if(contactPersons[i].getId()==id){ ? ? ? ? ? ? ? ? ?contactPersons[i].setPhoneNo(phoneNo); ? ? ? ? ? ? ? ? System.out.println("修改成功"); ? ? ? ? ? ? ? ? ?break; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? if(i>=size){ ? ? ? ? ? ? System.out.println("修改有誤"); ? ? ? ? } ? ?} ? ? ? ? //5.刪除聯(lián)系人信息 ? ? ? ? ?public void delContPerson(int id) { ? ? ? ? ? ? ?int i; ? ? ? ? ? ? ?for(i=0; i<size; i++) { ? ? ? ? ? ? ? ? ?if (contactPersons[i].getId() == id) { ? ? ? ? ? ? ? ? ? ? ?break; ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ?} ? ? ? ? ? ? ?if(i>=size){ ? ? ? ? ? ? ? ? ?System.out.println("刪除有誤"); ? ? ? ? ? ? ?}else{ ? ? ? ? ? ? ? ? ?for(int j=size-2; j>=i; j++){ ? ? ? ? ? ? ? ? ? ? ?contactPersons[j]=contactPersons[j+1]; ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ?size--; ? ? ? ? ? ? ? ? ?System.out.println("刪除成功"); ? ? ? ? ? ? ?} ? ? ? ? } }
test.java
package com.demo4; import java.util.Scanner; public class test { ? ? public static void main(String[] args) { ? ? ? /* ?1. 新增一個聯(lián)系人 ? ? ? ? 2.查找指定的聯(lián)系人 ? ? ? ? 3.查找所有的聯(lián)系人 ? ? ? ? 4. 修改聯(lián)系人信息 ? ? ? ? 5.刪除聯(lián)系人信息 ? ? ? ? 6.退出系統(tǒng)*/ ? ? ? ?PhoneBook phoneBook = new PhoneBook(); ? ? ? ? System.out.println("==================電話本系統(tǒng)==================="); ? ? ? ? Scanner sc =new Scanner(System.in); ? ? ? ?prof: while(true) { ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? System.out.println("---------------------------------------------------------"); ? ? ? ? ? ? ? ? System.out.println("1,【添加聯(lián)系人】 ?2,【查找聯(lián)系人】 ?3,【瀏覽所有】"); ? ? ? ? ? ? ? ? System.out.println("4,【修改聯(lián)系人】 ?5,【刪除聯(lián)系人】 ?6,【退出系統(tǒng)】"); ? ? ? ? ? ? ? ? System.out.println("----------------------------------------------------------"); ? ? ? ? ? ? ? ? System.out.print("選擇操作【標(biāo)號】:"); ? ? ? ? ? ? ? ? if(!sc.hasNextInt()){ ? ? ? ? ? ? ? ? ? ? sc.next(); ? ? ? ? ? ? ? ? ? ? System.out.println("輸入有序,【重新】輸入"); ? ? ? ? ? ? ? ? ? ? continue ; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? int num = sc.nextInt(); ? ? ? ? ? ? ? ? switch (num) { ? ? ? ? ? ? ? ? ? ? case 1: ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("進(jìn)入【添加】操作:"); ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("添加操作"); ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("輸入姓名:");String name =sc.next(); ? ? ? ? ? ? ? ? ? ? ? ? /*這里對性別做了簡單限制,男或者女,其他輸入暫且不做判斷,可以自行優(yōu)化*/ ? ? ? ? ? ? ? ? ? ? ? ? while(true) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("輸入性別:"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? String sex = sc.next(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (!("男".equals(sex) || "女".equals(sex))) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("輸入有誤,【重新】輸入"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? continue; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("輸入年齡: ");Integer age = sc.nextInt(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("輸入電話號碼:");String phoneNo = sc.next(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("輸入證件號:");String idCard = sc.next(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? phoneBook.addContPerson(name,sex,age,phoneNo,idCard); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? case 2: ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("進(jìn)入【查詢】操作:"); ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("輸入聯(lián)系人【姓名】");String fname = sc.next(); ? ? ? ? ? ? ? ? ? ? ? ? ContactPerson[] contactPeoples=phoneBook.findByName(fname); ? ? ? ? ? ? ? ? ? ? ? ? if(contactPeoples!=null){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? for(ContactPerson contactPeople : contactPeoples){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?contactPeople.show(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("查詢成功"); ? ? ? ? ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("暫無信息"); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case 3: ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("進(jìn)入【瀏覽】操作:"); ? ? ? ? ? ? ? ? ? ? ? ? phoneBook.findAll(); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case 4: ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("進(jìn)入【修改】操作:"); ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("輸入修改的【標(biāo)號】: "); ? ? ? ? ? ? ? ? ? ? ? ? int id =sc.nextInt(); ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("將【手機(jī)號】修改成: "); ? ? ? ? ? ? ? ? ? ? ? ? String newphoneNo = sc.next(); ? ? ? ? ? ? ? ? ? ? ? ? phoneBook.modfyMesById(id,newphoneNo); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case 5: ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("進(jìn)入【刪除】操作:"); ? ? ? ? ? ? ? ? ? ? ? ? ? while(true) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("輸入要刪除標(biāo)【標(biāo)號】:"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? /*這里做一下簡單判斷,輸入整數(shù)才可以*/ ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (!sc.hasNextInt()) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("輸入有誤,【重新】輸入"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sc.next(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? continue; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? int delid = sc.nextInt(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? phoneBook.delContPerson(delid); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? }break; ? ? ? ? ? ? ? ? ? ? case 6: ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("退出成功"); ? ? ? ? ? ? ? ? ? ? ? ? break prof; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ?} ? ? ?} }
運(yùn)行截圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaSE學(xué)習(xí)之內(nèi)部類及常用API
這篇文章主要介紹了JavaSE中的內(nèi)部類和幾個常用的API,文中的示例代碼介紹詳細(xì),對我們學(xué)習(xí)JavaSEI有一定的幫助,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12Java JVM運(yùn)行時數(shù)據(jù)區(qū)(Run-Time Data Areas)
運(yùn)行時數(shù)據(jù)區(qū),是java虛擬機(jī)定義的在程序執(zhí)行期間使用的各種運(yùn)行時的數(shù)據(jù)區(qū),通過JVM運(yùn)行時數(shù)據(jù)區(qū)圖例給大家展示的很詳細(xì),對JVM 運(yùn)行時數(shù)據(jù)區(qū)相關(guān)知識感興趣的朋友跟隨小編一起看看吧2021-06-06Java超詳細(xì)講解如何生成隨機(jī)整數(shù)
在?Java?中,生成隨機(jī)數(shù)的場景有很多,所以本文我們就來盤點(diǎn)一下?幾種生成隨機(jī)數(shù)的方式,以及它們之間的區(qū)別和每種生成方式所對應(yīng)的場景2022-05-05mybatis(mybatis-plus)映射文件(XML文件)中特殊字符轉(zhuǎn)義的實(shí)現(xiàn)
XML 文件在解析時會將五種特殊字符進(jìn)行轉(zhuǎn)義,本文主要介紹了mybatis(mybatis-plus)映射文件(XML文件)中特殊字符轉(zhuǎn)義的實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-12-12如何使用spring ResponseEntity處理http響應(yīng)
這篇文章主要介紹了如何使用spring ResponseEntity處理http響應(yīng)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07解決Java編譯時錯誤:A JNI error has occurred,ple
這篇文章主要介紹了解決Java編譯時錯誤:A JNI error has occurred,please check your installation and try again,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02SpringBoot 整合Tess4J庫實(shí)現(xiàn)圖片文字識別案例詳解
Tess4J是一個基于Tesseract OCR引擎的Java接口,可以用來識別圖像中的文本,說白了,就是封裝了它的API,讓Java可以直接調(diào)用,今天給大家分享一個SpringBoot整合Tess4j庫實(shí)現(xiàn)圖片文字識別的小案例2023-10-10