电话本管理系统 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;
}
}