包链接:https://pan.baidu.com/s/1nzzv6DrSkX8ecSRou4hiEA 提取码: 2m7u
public static void addPerson(){
System.out.println("请输入姓名:");
String name = sc.next();
System.out.println("请输入年龄:");
int age = sc.nextInt();
System.out.println("请输入地址:");
String address=sc.next();
System.out.println("请输入电话:");
int tel=sc.nextInt();
Person p1=new Person(name, age, address, tel);
person.add(p1);
System.out.println("添加成功!");
}
public static void delPerson(){
System.out.println("请输入姓名:");
String name=sc.next();
int index=getIndexByName(name);
if(index!=-1){
person.remove(index);
System.out.println("删除成功!");
}else{
System.out.println("没有此人!");
}
}
public static void updatePerson(){
System.out.println("请输入你要更新的名字:");
String name=sc.next();
int index=getIndexByName(name);
if(index!=-1){
System.out.println("请输入姓名:");
String name1=sc.next();
System.out.println("请输入年龄:");
int age=sc.nextInt();
System.out.println("请输入地址:");
String address=sc.next();
System.out.println("请输入电话:");
int tel=sc.nextInt();
Person p1=new Person(name1, age, address, tel);
person.set(index, p1);
}
else{
System.out.println("没有此人!");
}
}
public static void selectAll(){
for (int i = 0; i < person.size(); i++) {
Object o=person.get(i);
Person persons=(Person) o;
persons.display();
}
}
public static void selectByName(){
System.out.println("请输入你要查询的姓名:");
String name=sc.next();
int index=getIndexByName(name);
if(index!=-1){
Object o=person.get(index);
Person person=(Person) o;
person.display();
}
else{
System.out.println("没有此人!");
}
public static int getIndexByName(String name){
for (int i = 0; i < person.size(); i++) {
if(person.get(i).getName().equals(name)){
return i;
}
}
return -1;
}
public static void input(){
System.out.println("请输入姓名:");
String name1=sc.next();
System.out.println("请输入年龄:");
int age=sc.nextInt();
System.out.println("请输入地址:");
String address=sc.next();
System.out.println("请输入电话:");
int tel=sc.nextInt();
}
public class Person {
private String name;
private int age;
private String address;
private int tel;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getTel() {
return tel;
}
public void setTel(int tel) {
this.tel = tel;
}
public Person(String name, int age, String address, int tel) {
super();
this.name = name;
this.age = age;
this.address = address;
this.tel = tel;
}
public Person() {
super();
}
public void display(){
System.out.println("姓名:"+name
+" "+" 年龄:"+age+
" "+"地址:"+address+
" "+"电话:"+tel
);
}
}
public class TelTest {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
// 添加一条数据
Person p2=new Person("李四", 18, "贵阳", 1786327);
Method.person.add(p2);
while(true){
System.out.println("---------------电话本管理系统--------------");
System.out.println("1.添加 2.删除 3.修改 4.查询所有 5.根据姓名查询 0.退出");
System.out.println("---------------电话本管理系统---------------");
System.out.println("请选择业务:");
int x=sc.nextInt();
switch (x) {
case 1:
Method.addPerson();
break;
case 2:
Method.delPerson();
break;
case 3:
Method.updatePerson();
break;
case 4:
Method.selectAll();
break;
case 5:
Method.selectByName();
break;
case 0:
System.out.println("你已退出程序!");
System.exit(0);
break;
default:
break;
}
}
}
}