已经完成了,程序运行结果如下:
1. 查询
2. 新增
3. 删除
4. 退出
请输入数字1-4:1
张三 13333333333
李四 15555555555
1. 查询
2. 新增
3. 删除
4. 退出
请输入数字1-4:2
请输入联系人姓名和电话,使用空格分隔:王五 18888888888
联系人信息添加成功!
1. 查询
2. 新增
3. 删除
4. 退出
请输入数字1-4:1
张三 13333333333
李四 15555555555
王五 18888888888
1. 查询
2. 新增
3. 删除
4. 退出
请输入数字1-4:3
请输入要删除的联系人姓名:李四
联系人信息删除成功!
1. 查询
2. 新增
3. 删除
4. 退出
请输入数字1-4:1
张三 13333333333
王五 18888888888
1. 查询
2. 新增
3. 删除
4. 退出
请输入数字1-4:4
您已退出,程序运行结束!
如有帮助,请采纳,十分感谢!
代码如下,如有帮助,请采纳,谢谢!
记得自己修带文本路径。
public class App {
private static final Scanner SC = new Scanner(System.in);
private static List<Person> personList = new ArrayList<>();
private void menu() {
System.out.println("<----------------------这里是菜单---------------------->");
System.out.println("请选择:");
System.out.println("1.查询");
System.out.println("2.新增");
System.out.println("3.删除");
System.out.println("4.退出");
}
private void run() throws IOException {
while (true) {
menu();
String opStr = SC.next();
if (opStr.length() > 1 || !Character.isDigit(opStr.charAt(0)) || Integer.parseInt(opStr) > 4 || Integer.parseInt(opStr) <= 0) {
System.out.println("请输入正确的选项");
continue;
}
int op = Integer.parseInt(opStr);
switch (op) {
case 1:
select(true);
break;
case 2:
insert();
break;
case 3:
delete();
break;
case 4:
closeAndSave();
break;
default: throw new IllegalArgumentException("参数错误");
}
}
}
private void select(boolean isOutput) throws IOException {
if (personList.size() == 0) {
BufferedReader br = new BufferedReader(new FileReader("E:\\workspace-idea\\test\\src\\main\\resources\\content.text"));
for (String line = br.readLine(); line != null; line = br.readLine()) {
String[] splitLine = line.split(" ");
personList.add(new Person(splitLine[0], splitLine[1]));
}
br.close();
}
if (isOutput) {
System.out.println("<----------------------查询---------------------->");
personList.forEach(person -> System.out.println("姓名:" + person.getName() + " 电话号码:" + person.getPhone()));
}
}
private void insert() throws IOException {
System.out.println("<----------------------新增---------------------->");
if (personList.size() == 0) {
select(false);
}
System.out.println("请输入新增联系人姓名:");
String name = SC.next();
System.out.println("请输入新增联系人电话:");
String phone = SC.next();
personList.add(new Person(name, phone));
}
private void delete() throws IOException {
System.out.println("<----------------------删除---------------------->");
if (personList.size() == 0) {
select(false);
}
System.out.println("请输入需要删除的联系人姓名:");
String name = SC.next();
personList = personList.stream()
.filter(person -> !name.equals(person.getName()))
.collect(Collectors.toList());
}
private void closeAndSave() throws IOException {
SC.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\workspace-idea\\test\\src\\main\\resources\\content.text"));
for (Person person : personList) {
bw.write(person.toString() + "\n");
}
bw.close();
System.exit(0);
}
public static void main( String[] args ) throws IOException {
new App().run();
}
}
class Person {
private final String name;
private final String phone;
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public Person(String name, String phone) {
this.name = name;
this.phone = phone;
}
@Override
public String toString() {
return name + " " + phone;
}
}
文本示例
李四 121212
王五 131313
赵六 101213
运行截图