java实现电话本系统
本文实例为大家分享了java模拟实现电话本系统的具体代码,供大家参考,具体内容如下
java 模拟 电话本系统 (基础版)
需求:注意的是,联系人的id是自增长的
实现的功能:
java综合编程练习:基本的增删改查,对于逻辑数据的过滤,这块做了简单的处理,暂不优化
ContactPerson.java
package com.demo4;
public class ContactPerson {
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;
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. 新增一个联系人
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.查找指定的联系人
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.查找所有的联系人
public void findAll(){
for(int i=0; i<size;i++){
contactPersons[i].show();
}
}
//4. 修改联系人信息
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.删除联系人信息
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) {
PhoneBook phoneBook = new PhoneBook();
System.out.println("==================电话本系统===================");
Scanner sc =new Scanner(System.in);
prof: while(true) {
while (true) {
System.out.println("---------------------------------------------------------");
System.out.println("1,【添加联系人】 2,【查找联系人】 3,【浏览所有】");
System.out.println("4,【修改联系人】 5,【删除联系人】 6,【退出系统】");
System.out.println("----------------------------------------------------------");
System.out.print("选择操作【标号】:");
if(!sc.hasNextInt()){
sc.next();
System.out.println("输入有序,【重新】输入");
continue ;
}
int num = sc.nextInt();
switch (num) {
case 1:
System.out.println("进入【添加】操作:");
System.out.println("添加操作");
System.out.print("输入姓名:");String name =sc.next();
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("进入【查询】操作:");
System.out.print("输入联系人【姓名】");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("进入【浏览】操作:");
phoneBook.findAll();
break;
case 4:
System.out.println("进入【修改】操作:");
System.out.print("输入修改的【标号】: ");
int id =sc.nextInt();
System.out.print("将【手机号】修改成: ");
String newphoneNo = sc.next();
phoneBook.modfyMesById(id,newphoneNo);
break;
case 5:
System.out.println("进入【删除】操作:");
while(true) {
System.out.println("输入要删除标【标号】:");
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;
}
}
}
}
}
运行截图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关文章