import java.util.ArrayList;import java.util.List;import java.util.Scanner;/** * * @author young * */class User{ private String no; private String name; private int age; private String add; public String getNo() { return no; } public void setNo(String no) { this.no = no; } 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 getAdd() { return add; } public void setAdd(String add) { this.add = add; }}public class Test { // 增加 public static ArrayList addUser(){ List list = new ArrayList(); User u = new User(); Scanner sc = new Scanner(System.in); System.out.print("输入 no: "带罩戚); u.setNo(sc.nextLine()); System.out.print("输入name: "); u.setName(sc.nextLine()); System.out.print("输入add: "); u.setAdd(sc.nextLine()); System.out.print("输入age: "); u.setAge(sc.nextInt()); list.add(u); return (ArrayList) list; } // 修改 public static void updateUser(String name, List list){ Scanner sc = new Scanner(System.in); boolean isExist = false; for(int i = 0; i < list.size(); i++){ if(name.equals(list.get(i).getName())){ isExist = true; System.out.print("输入新 no: "); list.get(i).setNo(sc.nextLine()); System.out.print("闷碰输入新name: "); list.get(i).setName(sc.nextLine()); System.out.print("输入新add: "); list.get(i).setAdd(sc.nextLine()); System.out.print("输入新age: "); list.get(i).setAge(sc.nextInt()); } } if(isExist == false){ System.out.println("不存在该用户"); } } // 查找 public static void findUser(String name, List list){ boolean isExist = false; for(int i = 0; i < list.size(); i++){ if(name.equals(list.get(i).getName())){ isExist = true; System.out.println("用户编蠢陵号: " + list.get(i).getNo() + ". 姓名: " + list.get(i).getName() + ". 地址: " + list.get(i).getAdd() + ". 年龄: " + list.get(i).getAge()); } } if(isExist == false){ System.out.println("不存在该用户"); } } // 删除 public static void delUser(String name, List list) { boolean isExist = false; for (int i = 0; i < list.size(); i++) { if (name.equals(list.get(i).getName())) { isExist = true; list.remove(i); System.out.println("该用户已删除"); } } if (isExist == false) { System.out.println("不存在该用户"); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); List list = new ArrayList(); int i = 0; while(true){ System.out.println("输入你的选择: 1.增加\t2.查找\t3.修改\t4.删除\t0.退出"); i = sc.nextInt(); switch (i) { case 1: list = addUser(); break; case 2: System.out.print("输入要查找的用户名: "); String name = sc.next(); findUser(name, list); break; case 3: System.out.print("输入要修改的用户名: "); String n = sc.next(); updateUser(n, list); break; case 4: System.out.print("输入要删除的用户名: "); String na = sc.next(); delUser(na, list); break; case 0: System.out.println("程序结束!"); System.exit(0); break; } } }}
相关文章
-
java 没学过但有兴趣请问它能编译出EXE吗?
2023-06-13 11:26 阅读(608) -
Java 中 drawImage的各个参数代表啥意思
2023-06-12 19:47 阅读(599) -
Java中的compareTo()函数是怎么用的?
2023-06-12 11:00 阅读(617)
1 java中如何辨别11110000是表示正数还是负数?
626 阅读
2 javascript什么意思
565 阅读
3 Java认证考试指南(第2版)
619 阅读
4 Java语言程序设计(2016年清华大学出版社出版的图书)
614 阅读
5 Java7从入门到精通
540 阅读