非常简约学生管理系统——HashSet进行编写
很小的一个练习,可以参考一下啊~~~~~~
1:注意;学生类中进行多个重要方法的重写
package com.xt.homework; public class Student { private String id; private String name; public Student(String id, String name) { this.id = id; this.name = name; } public Student(String id) { this.id=id; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int hashCode() { return this.id.hashCode(); } @Override public boolean equals(Object obj) { if(obj instanceof Student&& this instanceof Student) return (this.id.equals(((Student)obj).id)); return false; } @Override public String toString() { return "id=" + id + ", name=" + name ; } }
主要的类
package com.xt.homework; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Set<Student> student = new HashSet<>(); while(true){ System.out.println("欢迎登陆学生管理系统:\n1:添加学生信息;\n2:删除学生信息;\n3:修改学生信息;\n4:查询学生信息;"); int option = scanner.nextInt(); switch (option) { case 1:{ System.out.println("------------添加学生-------------"); System.out.println("请输入学生的学号:"); String id=scanner.next(); System.out.println("请输入学生的姓名:"); String name=scanner.next(); student.add(new Student(id,name)); System.out.println("添加成功"); break; } case 2:{ System.out.println("************删除学生****************"); System.out.println("请输入删除学生的学号:"); String deleteID=scanner.next(); boolean result = student.remove(new Student(deleteID)); if (result) { System.out.println("删除成功"); } else { System.out.println("没有id为"+deleteID+"的学生!"); } break; } case 3:{ System.out.println("################修改学生信息####################"); System.out.println("请输入需要修改的id号:"); String updateID = scanner.next(); System.out.println("请输入新的姓名:"); String name=scanner.next(); boolean tag = false; for (Student student2 : student) { if(student2.getId().equals(updateID)) { student.remove(student2); student.add(new Student(updateID,name)); System.out.println("修改成功"); tag=true; break; } } if(!tag) { System.out.println("修改失败!!"); } break; } case 4:{ System.out.println("@@@@@@@@@@@@@@@@@@查询学生信息@@@@@@@@@@@@@@@@@@@@@@@"); System.out.println("请输入需要查询学生的id:"); String selectID = scanner.next(); boolean tag = false; for (Student stu : student) { if(stu.getId().equals(selectID)) { System.out.println(stu); tag=true; System.out.println("查询成功!!!"); break; } } if(!tag) { System.out.println("没有id为"+selectID+"的学生!"); } break; } default: System.out.println("没有该操作");; } } } }