package day11; import java.util.ArrayList; import java.util.Scanner; public class ManagementSystem { /** * 学生管理系统分析: * 1.拥有增删改查功能,不允许学生学号重复 * 2.通过方法的重载来优化代码 * 3.通过一个方法来遍历集合并返回学生对象 * 4. * * @param args */ // 定义一个集合来存放学生对象 private static ArrayList<Student> students = new ArrayList<>(); public static void main(String[] args) { System.out.println("欢迎进入每特教育学生管理系统"); for (; ; ) { System.out.println("请输入对应序号1-5进行操作"); System.out.println("1.新增学生"); System.out.println("2.修改学生"); System.out.println("3.删除学生"); System.out.println("4.查询学生"); System.out.println("5.退出系统"); Scanner scanner = new Scanner(System.in); Integer number = scanner.nextInt(); switch (number) { case 1: System.out.println("输入新增学生信息"); addstudent(); break; case 2: System.out.println("开始修改学生信息"); modify(); break; case 3: System.out.println("开始删除学生信息"); delete(); break; case 4: System.out.println("欢迎查询学生信息"); check(); break; case 5: System.out.println("操作完毕,退出系统"); return; } } } public static void addstudent() { // 这是新增学生的方法 注意 学号不能重复 Scanner scanner = new Scanner(System.in); System.out.println("请输入学生的学号"); String sId = scanner.nextLine(); Student student = repeat(sId, false); // 如果返回的学生对象不为空,则表示集合中已经存在相同学号的对象,则提示用户 if (student != null) { System.out.println("学号" + sId + "已经被占用,无法继续添加"); return; } System.out.println("请输入学生的名字"); String name = scanner.nextLine(); System.out.println("请输入学生的地址"); String local = scanner.nextLine(); System.out.println("请输入学生的年龄"); Integer age = scanner.nextInt(); // 在集合中新增一个学生,传递学号 名字 地址 年龄 students.add(new Student(sId, name, local, age)); System.out.println("新增学生完成"); } // 这是删除学生的方法 public static void delete() { System.out.println("请输入想要删除的学生学号"); Scanner sca = new Scanner(System.in); String id = sca.nextLine(); Student student = repeat(id, true); if (student == null) { System.out.println("没有找到该学号对应的学生"); return; } System.out.println("学号" + id + "学生删除完毕"); } // 这个是查询学生的方法 public static void check() { System.out.println("学号\t姓名\t住址\t年龄"); for (Student student : students) { System.out.println(student.getStuid() + "\t" + student.getName() + "\t" + student.getLocal() + "\t" + student.getAge()); } } /** * repeat这个方法主要作用是循环遍历集合,把用户输入的学号与集合中的学号做对比 * 如果该学号对象已经在集合中存在,则返回该学号对应的学生对象 * @param sId * @param isdelete * @return */ public static Student repeat(String sId, boolean isdelete) { // 这是判断学号是否重复的方法,如果重复则删除该学生对象 for (int i = 0; i < students.size(); i++) { // 取出集合中每一个对象的学号和新增的学号比较 Student student = students.get(i); // 如果返回true就代表集合中已经存在该学号 if (student.getStuid().equals(sId)) { if (isdelete) { // 走到这里说明集合中已经有录入的学号对象,直接删除该对象 return students.remove(i); } return student; } } return null; } public static void modify() { // 这是修改学生信息的方法 System.out.println("请输入要修改学生的学号"); Scanner sc = new Scanner(System.in); String stid = sc.nextLine(); // 调用repeat方法查询集合中是否存在该对象,如果该学号对象已经在集合中存在,则返回该学号对应的学生对象 Student student = repeat(stid, false); // 如果student等于空,则代表没有在集合中没有找到该学号学生对象 if (student==null){ System.out.println("没有根据该学号查找到对应学生"); return; } System.out.println("请输入学生的名字"); String newname = sc.nextLine(); System.out.println("请输入学生的地址"); String newlocal = sc.nextLine(); System.out.println("请输入学生的年龄"); Integer newage = sc.nextInt(); // 通过对象.set 方法修改学生信息 student.setName(newname); student.setLocal(newlocal); student.setAge(newage); System.out.println("学生信息修改完成"); } }
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交