电话本用户增删改查

package T4_employee_list;


import java.util.Scanner;


public class Test {


public static void main(String[] args) {



Scanner sc = new Scanner(System.in);
PhoneManager pm = new PhoneManager();


boolean isExit = false;
while (!isExit) {
System.out.println("-----------------电话本----------------------");


System.out.println("1:添加\t   2:删除\t   3:修改\t  4:按姓名查询\t  5:查询所有\t   0:退出");


System.out.println("------------------电话本--------------------");


System.out.println("请选择业务");
String input = sc.next();
switch (input) {
case "1":
pm.add(sc);
break;
case "2":
pm.delete(sc);
break;
case "3":
pm.modify(sc);
break;
case "4":
pm.lookup(sc);
break;
case "5":
pm.show();
break;
case "0":
isExit = true;
break;


default:
System.out.println("您的选择不正确");
break;
}
}
sc.close();
}


}




package T4_employee_list;


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class PhoneManager {
// 创建一个list集合用于存放用户信息
List<Phone> phones = new ArrayList<>();


public Phone input(Scanner sc) {
Phone p = new Phone();
System.out.println("请输入姓名");
String name = sc.next();
System.out.println("请输入性别");
String sex = sc.next();
System.out.println("请输入年龄");
int age = sc.nextInt();
System.out.println("请输入qq");
int qq = sc.nextInt();
System.out.println("请输入电话号码");
int number = sc.nextInt();
p.setName(name);
p.setAge(age);
p.setSex(sex);
p.setQq(qq);
p.setNumber(number);
return p;
}


public boolean add(Scanner sc) {
try {
phones.add(input(sc));
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
return true;


}


public void show() {
for (Phone ph : phones) {
System.out.println(ph.toString());
}


}


public boolean delete(Scanner sc) {
System.out.println("请输入要删除的用户姓名");
String name = sc.next();
Phone deletephone = new Phone();
for (Phone ph : phones) {
if (ph.getName().equals(name)) {
deletephone = ph;
break;
}
}
if (deletephone.getName() == null) {
System.out.println("");


} else {


phones.remove(deletephone);
show();


}


return false;


}


public boolean modify(Scanner sc) {


System.out.println("请输入要修改的用户姓名");
String name = sc.next();
Phone modifyphone = new Phone();
for (Phone ph : phones) {
if (ph.getName().equals(name)) {
// ph的引用内存地址赋给了modifyphone此时modifyphone就指向list集合里的ph那一块内存
modifyphone = ph;
break;
}
}


if (modifyphone.getName() == null) {
System.out.println("");


} else {
Phone p = input(sc);
modifyphone.setName(p.getName());
modifyphone.setAge(p.getAge());
modifyphone.setNumber(p.getNumber());
modifyphone.setSex(p.getSex());
modifyphone.setQq(p.getQq());
System.out.println(modifyphone);
}


return true;
}


public boolean lookup(Scanner sc) {
System.out.println("请输入要查询用户的姓名");
String name = sc.next();
Phone lookphone = new Phone();
for (Phone p : phones) {
if (p.getName().equals(name)) {
lookphone = p;
System.out.println(lookphone.toString());
}
}
return true;
}
}





package T4_employee_list;


public class Phone {
private String name;
private int number;
private int age;
private String sex;
private int qq;


//返回用户的具体信息
public String toString(){


return "姓名"+this.name+"\t性别"+this.sex+"\t年龄"+this.age+"\tqq"+this.qq+"\t电话"+this.number;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getQq() {
return qq;
}
public void setQq(int qq) {
this.qq = qq;
}




public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值