发布网友 发布时间:2022-04-23 10:07
共2个回答
热心网友 时间:2022-04-26 16:25
package x.y.nio;package x.y.nio;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class PersonUtils {
// 模拟数据库
private static Map<String, Person> persons = new HashMap<String, Person>();
public static void add(String name, String mobile, String email,
String address) {
Person person = new Person();
person.setName(name);
person.setMobile(mobile);
person.setEmail(email);
person.setAddress(address);
persons.put(person.getName(), person);
}
public static void update(Person person) {
persons.put(person.getName(), person);
}
public static void del(String name) {
persons.remove(name);
}
public static void get(String name) {
Person person = persons.get(name);
if (person != null) {
System.out.println(person.toString());
}
}
public static void getAll() {
for (String name : persons.keySet()) {
Person person = persons.get(name);
System.out.println(person.toString());
}
}
public static void main(String[] args) {
System.out.println("通讯录 \n 输入功能序号:1:添加 2:修改 3:删除 4:查询 5:列表0:退出");
Scanner scanner = new Scanner(System.in);
while(true){
int i = scanner.nextInt();
switch (i) {
case 1:
System.out.print("姓名:");
String name = scanner.next();
System.out.print("手机:");
String mobile = scanner.next();
System.out.print("邮箱:");
String email = scanner.next();
System.out.print("地址:");
String address = scanner.next();
PersonUtils.add(name, mobile, email, address);
System.out.println("添加成功!");
PersonUtils.get(name);
break;
case 2:
System.out.print("输入要修改的人的姓名:");
String updateName = scanner.next();
Person person = persons.get(updateName);
if(person != null){
System.out.print("手机:");
person.setMobile(scanner.next());
System.out.print("邮箱:");
person.setEmail(scanner.next());
System.out.print("地址:");
person.setAddress(scanner.next());
PersonUtils.update(person);
System.out.println("修改成功!");
PersonUtils.get(updateName);
}else{
System.out.println("要修改的人不存在!");
}
break;
case 3:
System.out.print("输入要删除的姓名:");
PersonUtils.del(scanner.next());
System.out.println("删除成功!");
PersonUtils.getAll();
break;
case 4:
System.out.print("输入要查询的姓名:");
PersonUtils.get(scanner.next());
break;
case 5:
PersonUtils.getAll();
break;
case 0:
System.exit(0);
break;
default :
System.out.println("选项错误!");
break;
}
}
}
}
热心网友 时间:2022-04-26 17:43
我给你看看追问刚看完JAVA基础部分,其他的还不会,只用这些然后写出来这个,谢谢追答
实体类
package com.ocaenlive.connactor.operation;