電話本管理系統 ArrayList版見:https://blog.csdn.net/Amber_technology/article/details/103977752
一、電話本管理系統
1.主界面
2.添加
2.修改
1)無信息
2)正常修改
3 . 打印所有電話本
4.刪除
再次選擇4,查詢所有,張三的信息已刪除
0.退出系統
import java.util.Scanner;
public class Test {
static Scanner sc = new Scanner(System.in);
static Telephone[] telephones = new Telephone[100];
public static void main(String[] args) {
//主界面
while (true) {
System.out.println("----------------電話本管理系統-------------");
System.out.println("1.添加 2.刪除 3.修改 4.查詢所有 5.根據姓名查詢 0.退出 ");
System.out.println("----------------電話本管理系統-------------");
System.out.println("請選擇業務");
int num = sc.nextInt();
if (num == 0) {
break;
}
switch (num) {
case 1:
add();
break;
case 2:
del();
break;
case 3:
modify();
break;
case 4:
selctAll();
break;
case 5:
search();
break;
default:
System.out.println("請輸入1~5之間的數字");
break;
}
}
}
private static int search(String name) {
for (int i = 0; i < telephones.length; i++) {
if (telephones[i] == null) {
break;
}
if (telephones[i].getName().equals(name)) {
return i;
}
}
return -1;
}
private static void search() {
System.out.println("請輸入要查找的姓名");
String name = sc.next();
int i = search(name);
if (i == -1) {
System.out.println("未找到相應記錄");
} else {
print(telephones[i]);
}
}
private static void selctAll() {
System.out.println("---------------打印所有電話本--------------");
if (telephones[0]==null) {
System.out.println("無記錄");
}else {
for (int i = 0; i < telephones.length;i++) {
if (telephones[i]==null) {
break;
}else {
print(telephones[i]);
}
}
}
}
private static void modify() {
System.out.println("請輸入要修改的姓名");
boolean flag =false;
String name = sc.next();
int i = search(name);
if (i == -1) {
System.out.println("無信息");
}else{
print(telephones[i]);
System.out.println("請重新輸入信息");
input(telephones[i]);
print(telephones[i]);
flag = true;
}
}
private static void del() {
System.out.println("請輸入要刪除的姓名");
String name = sc.next();
int i = search(name);
if (i == -1) {
System.out.println("未找到相應信息");
return;
}else{
print(telephones[i]);
System.out.println("確定要刪除嗎?1(是)0(否)");
int num = sc.nextInt();
if (num == 1) {
for (int j = i; j < telephones.length -1; j++) {
telephones[i]=telephones[i+1];
}
}
}
}
private static void add() {
for (int i = 0; i < telephones.length; i++) {
if (telephones[i]!=null) {
continue;
}else {
telephones[i] = new Telephone();
System.out.println("----------------添加電話本---------------");
input(telephones[i]);
print(telephones[i]);
System.out.println("添加成功");
break;
}
}
}
//打印telephone[i]
private static void print(Telephone telephone) {
System.out.print("姓名:");
System.out.print(telephone.getName());
System.out.print("性別:");
System.out.print(telephone.getSex());
System.out.print("年齡:");
System.out.print(telephone.getAge());
System.out.print("電話:");
System.out.print(telephone.getTel());
System.out.print("QQ:");
System.out.print(telephone.getQQ());
System.out.print("地址:");
System.out.print(telephone.getAddr());
System.out.println();
}
//輸入telephone[i]
private static void input(Telephone telephone) {
System.out.println("姓名:");
telephone.setName(sc.next());
System.out.println("性別:");
telephone.setSex(sc.next());
System.out.println("年齡:");
telephone.setAge(sc.nextInt());
System.out.println("電話:");
telephone.setTel(sc.next());
System.out.println("QQ:");
telephone.setQQ(sc.next());
System.out.println("地址:");
telephone.setAddr(sc.next());
}
}
public class Telephone {
//變量
private String name;
private String sex;
private int age;
private String tel;
private String QQ;
private String addr;
//方法
//添加
public void add() {
}
//修改
public void modify() {
}
//刪除
public void del() {
}
//set & get方法
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 getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getQQ() {
return QQ;
}
public void setQQ(String QQ) {
this.QQ = QQ;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}