package studentsystem; public class student { private String id; private String name; private int age; private double score; public student() { } public student (String id , String name , int age , double score) { this.id = id; this.name = name; this.age = age; this.score = score; } public void setId(String id) { this.id = id; } public String getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } }
package studentsystem; import java.util.ArrayList; import java.util.Scanner; public class studentsystem { public static void main(String [] args) { //学生类集合 ArrayList <student> list = new ArrayList<>(); while(true) { System.out.println("************欢迎来到学生管理系统*************"); System.out.println("1.添加学生信息"); System.out.println("2.删除学生信息"); System.out.println("3.修改学生信息"); System.out.println("4.查询学生信息"); System.out.println("5.退出"); System.out.println("请输输入您的选择: "); Scanner sc = new Scanner(System.in); String str = sc.next(); switch(str) { case "1" -> Add(list); case "2" -> Delete(list); case "3" -> Updata(list); case "4" -> Find(list); case "5" -> { System.out.println("退出系统"); return ; } default -> System.out.println("输入错误"); } } } //添加 public static void Add(ArrayList <student> list) { student s = new student(); Scanner sc = new Scanner(System.in); String id = null; while(true) { System.out.println("请输入学号"); id = sc.next(); boolean flag = only(list , id); if(flag) { System.out.println("id已存在,请重新输入"); } else { s.setId(id); break; } } System.out.println("请输入姓名"); String name = sc.next(); s.setName(name); System.out.println("请输入年龄"); int age = sc.nextInt(); s.setAge(age); System.out.println("请输入分数"); double score = sc.nextDouble(); s.setScore(score); //加入集合 list.add(s); System.out.println("添加成功"); } //删除 public static void Delete(ArrayList <student> list) { Scanner sc = new Scanner(System.in); System.out.println("输入要删除的id"); String id = sc.next(); int index = getID(list , id) ; if(index >= 0) { list.remove(index); System.out.println("删除成功"); } else { System.out.println("删除失败"); } } //修改 public static void Updata(ArrayList <student> list) { Scanner sc = new Scanner(System.in); System.out.println("输入修改的id"); String id = sc.next(); int index = getID(list , id); if(index >= 0) { student stu = list.get(index); System.out.println("请输入姓名"); String name = sc.next(); stu.setName(name); System.out.println("请输入年龄"); int age = sc.nextInt(); stu.setAge(age); System.out.println("请输入分数"); double score = sc.nextDouble(); stu.setScore(score); System.out.println("修改成功"); } else { System.out.println("修改失败"); } } //查询 public static void Find(ArrayList <student> list) { if(list.size() == 0) { System.out.println("无学生信息"); return ; } //输出 System.out.println("id\t姓名\t年龄\t分数\t"); for(int i = 0 ;i < list.size() ; i++) { student stu = list.get(i); System.out.println( stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getScore() ); } } //判断id唯一 public static boolean only(ArrayList<student> list , String id) { for(int i = 0 ; i < list.size(); i++) { student stu = list.get(i); String sid = stu.getId(); if(sid.equals(id)) { return true; } } return false; } //获取索引 public static int getID(ArrayList<student> list , String id) { for(int i = 0 ; i < list.size(); i++) { student stu = list.get(i); String sid = stu.getId(); if(sid.equals(id)) { return i; } } return -1; } }