import java.util.Scanner;
public class MyTel {
public static Scanner sc = new Scanner(System.in);
//电话本存储结构
public static String[][] myTels = new String[500][6];
public static int count = 0; //记录当前电话本中的联系人个数
//显示主界面
public static void showMain(){
System.out.println("-----------------电话本管理系统----------------");
System.out.println("1.添加 2.删除 3.修改 4.查询所有 5.根据姓名查询 0.退出");
System.out.println("-----------------电话本管理系统----------------");
System.out.println("请选择业务:");
}
//根据索引值修改内容
public static void updateByIndex(int index){
System.out.println("姓名:");
myTels[index][0] = sc.next();
System.out.println("性别:");
myTels[index][1] = sc.next();
System.out.println("年龄:");
myTels[index][2] = sc.next();
System.out.println("电话:");
myTels[index][3] = sc.next();
System.out.println("QQ:");
myTels[index][4] = sc.next();
System.out.println("地址:");
myTels[index][5] = sc.next();
System.out.println("姓名:"+myTels[count][0]+",性别:"+myTels[index][1]+",年龄:"+myTels[index][2]+",电话:"+myTels[index][3]+",QQ:"+myTels[index][4]+",地址:"+myTels[index][5]);
}
//添加电话本
public static void myAdd(){
System.out.println("------------------添加电话本-----------------");
updateByIndex(count);
count++;
System.out.println("添加成功!");
}
//根据姓名查找联系人,返回查找到的索引
public static int findByName(String name){
int index = -1;
//循环遍历电话本
for(int i=0;i<count;i++){
if(myTels[i][0].equals(name)){
index = i;
}
}
return index;
}
//删除电话本
public static void myDelete(){
System.out.println("-----------------删除电话本----------------");
System.out.println("请输入要删除的联系人姓名:");
String name = sc.next();
//首先找到该联系人
int tempIndex = findByName(name);
for(int i=0;i<myTels.length;i++){
if(myTels[i][0].equals(name)){
tempIndex = i;
}
}
//如果不存在该联系人则直接返回
if(tempIndex == -1){
System.out.println("该联系人不存在!");
return;
}
//确认是否删除
System.out.println("确认要删除吗?1(是)0(否)");
int input = sc.nextInt();
if(input == 1){
//如果存在则直接删除(将后面的联系人替换前面的联系人)
for(int i=tempIndex+1;i<count;i++){
//前移
myTels[i-1] = myTels[i];
}
//联系人个数减1
count--;
System.out.println("删除成功!");
}
}
//修改
public static void myUpdate(){
System.out.println("-----------------修改电话本----------------");
System.out.println("请输入要修改的联系人姓名:");
String tempname = sc.next();
//根据姓名查找联系人
int index = findByName(tempname);
if(index == -1){
System.out.println("无信息!");
return;
}
//正常的修改逻辑
System.out.println("姓名:"+myTels[index][0]+",性别:"+myTels[index][1]+",年龄:"+myTels[index][2]+",电话:"+myTels[index][3]+",QQ:"+myTels[index][4]+",地址:"+myTels[index][5]);
System.out.println("求重新输入信息:");
updateByIndex(index);
System.out.println("修改成功!");
}
//打印所有电话本
public static void myshowAll(){
for(String[] infos:myTels){
for(String info:infos){
System.out.println(info+",");
}
System.out.println();
}
}
//根据姓名查询
public static void myFinfByName(){
System.out.println("------------------根据姓名查询-----------------");
System.out.println("请输入联系人姓名:");
String name = sc.next();
int index = findByName(name);
// TODO 20190112添加“联系人是否存在”
for(String info:myTels[index]){
System.out.println(info+",");
}
}
public static void main(String[] args) {
while(true){
//显示主界面
showMain();
//获取控制台输入
int input = sc.nextInt();
//根据输入进行相关业务处理
switch(input){
case 1:
//添加
myAdd();
break;
case 2:
//删除
myDelete();
break;
case 3:
//修改
myUpdate();
break;
case 4:
//查询所有
myshowAll();
break;
case 5:
//根据姓名查询
myFinfByName();
break;
case 0:
//退出系统
sc.close();
System.exit(1);
break;
default:
System.out.println("输入有误!");
showMain();
break;
}
}
}
}