package day5;
import java.util.Arrays;
import java.util.Scanner;
public class phoneNote {
//菜单
public static void menu(){
System.out.println("========================================================================");
System.out.println("1、添加 2、删除 3、修改 4、查询所有 5、根据姓名查询 0、退出");
System.out.println("========================================================================");
System.out.println("请选择业务");
}
//判断是否存在数据
public static boolean haveData(String [][]note){
if(note[0][0]==null){
System.out.println("还没有数据请先输入数据");
return false;
}else{
return true;
}
}
//添加
public static void add(String note[][]){
Scanner sc = new Scanner(System.in);
System.out.println("===========================添加联系人==================================");
int length = 0;
//判断是否到达存储的上限
if(note[0]==null){
//还没有存储数据
length = 0;
}else{
for (String []k:note){
for (String i :k){
if(i!=null){
length++;
break;
}
}
}
}
if(length==note.length){
System.out.println("已经到达存储上限");
// return new String[][]{};
}else{
String name,sex,age,phone,qq,adress;
System.out.print("姓名:");
name = sc.next();
System.out.print("性别:");
sex = sc.next();
System.out.print("年龄:");
age = sc.next();
System.out.print("电话:");
phone = sc.next();
System.out.print("QQ:");
qq = sc.next();
System.out.print("地址:");
adress = sc.next();
System.out.println("===========================输入的数据为==================================");
System.out.println("姓名:"+name+" ,性别:"+sex+" ,年龄:"+age+" ,电话:"+phone+" ,qq:"+qq+" ,地址:"+adress);
System.out.println("");
note[length][0] = "姓名"; note[length][1] = name;
note[length][2] = " 性别:"; note[length][3] = sex;
note[length][4] = " 年龄:"; note[length][5] = age;
note[length][6] = " 电话:"; note[length][7] = phone;
note[length][8] = " QQ:"; note[length][9] = qq;
note[length][10] =" 地址:"; note[length][11] = adress;
// return new String[][] {{"姓名:",name," 性别:",sex," 年龄:",age," 电话:",phone," QQ:",qq," 地址:",adress}};
}
}
//删除
public static String[][] del(String note[][]){
Scanner sc = new Scanner(System.in);
System.out.println("===========================删除数据==================================");
if(haveData(note)){
System.out.print("请输入姓名:");
String name = sc.next();
System.out.println("");
//显示查询到的数据
for (String []k:note){
if(k[1] ==null){
System.out.println("查无此人");
break;
}
if(k[1].equals(name)){
System.out.println("===========================查询到的你需要删除的数据==================================");
for (String i :k){
System.out.print(i);
}
System.out.println("");
}
}
//将需要删除的数据赋值为null
String [][] newNote = new String[10][12];
int m = 0 ;
for (int i =0;i
if(note[i][1] != null && note[i][1].equals(name)){
continue;
}
for (int j =0;j
newNote[m][j] =note[i][j];
}
//新数组的行
m++;
}
return newNote;
}
return new String [][] {};
}
//修改
public static void change(String note[][]){
Scanner sc = new Scanner(System.in);
System.out.println("===========================修改数据==================================");
if(haveData(note)){
System.out.print("请输入姓名:");
String changeName = sc.next();
System.out.println("");
//显示查询到的数据
for (String []k:note){
if(k[1] ==null){
System.out.println("查无此人");
break;
}
if(k[1].equals(changeName)){
System.out.println("===========================查询到的你需要修改的数据==================================");
for (String i :k){
System.out.print(i);
}
System.out.println("");
}
}
//输入修改的数据
String name,sex,age,phone,qq,adress;
System.out.print("姓名:");
name = sc.next();
System.out.print("性别:");
sex = sc.next();
System.out.print("年龄:");
age = sc.next();
System.out.print("电话:");
phone = sc.next();
System.out.print("QQ:");
qq = sc.next();
System.out.print("地址:");
adress = sc.next();
System.out.println("===========================输入的数据为==================================");
System.out.println("姓名:"+name+" ,性别:"+sex+" ,年龄:"+age+" ,电话:"+phone+" ,qq:"+qq+" ,地址:"+adress);
System.out.println("");
String [][] newNote = {{"姓名:",name," 性别:",sex," 年龄:",age," 电话:",phone," QQ:",qq," 地址:",adress}};;
//将修改后的数据赋值给原来的数组
for (int i =0;i
if(note[i][1] != null && note[i][1].equals(changeName)){
for (int j =0;j
if(!newNote[0][j].equals("0")){
note[i][j] = newNote[0][j];
}
}
}
}
}
}
//查询所有
public static void findAll(String note[][]){
System.out.println("===========================所有的数据==================================");
//判断是否存在数据
if(haveData(note)){
//有数据
for (String[] k:note){
//遇到空退出循环
if(k[0] == null){
break;
}
for (String i:k){
System.out.print(i);
}
System.out.println("");
}
}
}
//通过姓名查询
public static boolean findByName(String note[][]){
Scanner sc = new Scanner(System.in);
System.out.println("===========================通过姓名查询数据==================================");
//判断是否存在数据
if(haveData(note)){
System.out.print("请输入姓名:");
String name = sc.next();
System.out.println("");
for (String []k:note){
if(k[1] == null){
System.out.println("查无此人");
break;
}
if(k[1].equals(name)){
System.out.println("===========================查询到的数据==================================");
for (String i :k){
System.out.print(i);
}
System.out.println("");
return true;
}
}
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//选择的功能
int chose;
//是否退出
boolean quite = false;
//存储数据的数组
String [][] note = new String[10][12];
while (!quite){
menu();
chose = sc.nextInt();
switch (chose){
case 1:
add(note);
break;
case 2:
note =del(note);
break;
case 3:
change(note);
break;
case 4:
findAll(note);
break;
case 5:
findByName(note);
break;
case 0:
quite = true;
break;
default:
System.out.println("没有该功能:");
break;
}
}
}
}