用Java编写通讯录

时间:2022-08-11 01:09:00

package shizi;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Scanner;

public class Tongxunlu {

static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static String id="",name="", address="", phone="", sex="", qq="", yn="";

static Read[] c = new Read[10];

static int num = 0;

public static void main(String[] args) {

while(true){

System.out.println("*************通讯录***************"); System.out.println("1.添加联系人");

System.out.println("2.修改联系人");

System.out.println("3.删除联系人");

System.out.println("4.查询联系人");

System.out.println("5.退出");

try{

System.out.println("请你按键选择!");

int s = new Scanner(System.in).nextInt();

switch(s){

case 1:{

caseone();

break;

}

case 2:{

casetwo();

break;

}

case 3:

{

casethree();

break;

}

case 4:

{

casefour();

break;

case 5: System.exit(0); default: System.out.println("输入错误,请从新输入"); } } catch(Exception e){ System.out.println("你必须输入数字类型!"); } } } public static void caseone() throws IOException { System.out.println("请输入ID"); id = in.readLine(); System.out.println("请输入名字"); name = in.readLine(); System.out.println("请输入电话"); phone = in.readLine(); System.out.println("请输入QQ"); qq = in.readLine(); while(true) { System.out.println("请输入性别"); sex = in.readLine(); if(sex.equals("男")||sex.equals("女")) break; System.out.println("性别输入错误,重新输入"); } System.out.println("请输入地址"); address = in.readLine(); System.out.println("输入完成,是否添加(Y/N)"); yn = in.readLine(); if(yn.equals("Y") || yn.equals("y")) { c[num] = new Read(id,name,phone,qq,sex,address); num++; System.out.println("添加成功!"); }


System.out.println("添加失败!"); } public static void casetwo() throws IOException { System.out.println("请输入要删除的名字:"); name = in.readLine(); for(int i=0;i<num;i++) { if(name.equals(c[i].Id())) { System.out.println("删除成功!"); for(;i<num-1;i++) c[i]=c[i+1]; c[num] =null; break; } if(i==num) System.out.println("删除失败!"); } } public static void casethree() throws IOException { System.out.println("请输入要修改的名字:"); name = in.readLine(); for(int i=0;i<num;i++) { if(name.equals(c[i].Id())) { System.out.println("请从新输入信息:"); System.out.println("请输入ID"); id = in.readLine(); System.out.println("请输入名字"); name = in.readLine(); System.out.println("请输入电话"); phone = in.readLine(); System.out.println("请输入QQ"); qq = in.readLine(); System.out.println("请输入性别"); sex = in.readLine(); System.out.println("请输入地址"); address = in.readLine();


c[i] = new Read(id,name,phone,qq,sex,address);

break;

}

if(i==num)

System.out.println("修改失败!");

}

}

public static void casefour() throws IOException

{

System.out.println("请输入要查询的联系人名字:");

System.out.println(num);

name = in.readLine();

for(int i=0;i<num;i++)

{

if(name.equals(c[i].Name()))

{

System.out.println("查询成功!");

System.out.println("你要查找的联系人信息为:");

System.out.println("ID:"+c[i].Id()+"\n名字:"+c[i].Name()+"\n话:"+c[i].Phone()+"\nQQ:"+c[i].QQ()+"\n性别:"+c[i].Sex()+"\n地址:"+c[i].Address()); break;

}

if(i==num)

System.out.println("查找失败!");

}

}

}

class Read{

private String id;

private String name;

private String phone;

private String qq;

private String sex;

private String address;

public String Name(){

return name;

}


} public String Id(){ return id; } public String Phone(){ return phone; } public String QQ(){ return qq; } public String Sex(){ return sex; } public String Address(){ return address; } public Read(String id,String name,String phone,String qq,String sex,String address){ this.id = id; this.name = name; this.phone = phone; this.qq = qq; this.sex =sex; this.address = address; }

Java免费学习资料直播公开课群:175161984