StudentManagerSystem.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package J20250715.studentManager;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4. /**
  5. * @author WanJl
  6. * @version 1.0
  7. * @title StudentManager
  8. * @description
  9. * @create 2025/7/15
  10. */
  11. public class StudentManagerSystem {
  12. //添加学生到系统
  13. /**
  14. * 向list集合中添加学生对象
  15. * @param list
  16. */
  17. public static void addStudent(ArrayList<Student> list){
  18. System.out.println("添加学生信息...");
  19. Scanner sc=new Scanner(System.in);
  20. //判断学号是否重复,如果不重复才可以输入
  21. System.out.println("请输入学号:");
  22. String sid=sc.nextLine();
  23. System.out.println("请输入姓名:");
  24. String name=sc.nextLine();
  25. System.out.println("请输入年龄:");
  26. int age=sc.nextInt();
  27. System.out.println("请输入生日:");
  28. String birthday=sc.next();
  29. //创建学生对象
  30. Student student=new Student(sid,name,age,birthday);
  31. //把学生对象存入ArrayList集合对象
  32. list.add(student);
  33. System.out.println("添加学生成功");
  34. }
  35. //根据学号修改学生信息
  36. public static void updateStudent(ArrayList<Student> list){
  37. //获取键盘输入
  38. Scanner sc=new Scanner(System.in);
  39. System.out.println("请输入要修改学生的学号:");
  40. //先根据学号,找到这个学生对象元素在集合的位置索引值下标。
  41. String sid = sc.next();
  42. int index = getIndex(sid, list);//找到索引值
  43. //判断 index如果是-1,说明没有这个学生
  44. if (index==-1){
  45. System.out.println("查无此人,请重新输入学号");
  46. }else {
  47. System.out.println("请输入新的学生姓名:");
  48. String name = sc.next();
  49. System.out.println("请输入新的学生年龄:");
  50. int age = sc.nextInt();
  51. System.out.println("请输入新的学生生日:");
  52. String birthday = sc.next();
  53. Student s=new Student(sid,name,age,birthday);
  54. //调用list的修改元素的方法
  55. //list.set(集合的索引值,学生对象);
  56. list.set(index,s); //找到集合中index位置,将该元素替换为s
  57. System.out.println("修改学生成功");
  58. }
  59. }
  60. //根据学号删除学生信息
  61. //查询学生列表信息
  62. public static void queryStudentList(ArrayList<Student> list){
  63. //判空
  64. if(list.size()==0){
  65. System.out.println("没有学生信息,请先添加,后查询");
  66. return;
  67. }
  68. System.out.println("学号\t\t姓名\t年龄\t生日");
  69. //遍历集合
  70. for (int i = 0; i < list.size(); i++) {
  71. Student student = list.get(i);
  72. System.out.println(student);
  73. }
  74. }
  75. //根据id获取学生对象在集合中的索引值的方法
  76. public static int getIndex(String sid,ArrayList<Student> list){
  77. //索引值初始化为-1
  78. int index=-1;
  79. //遍历集合
  80. for (int i = 0; i < list.size(); i++) {
  81. Student student = list.get(i);
  82. String studentSid = student.getSid();
  83. if (sid.equals(studentSid)){ //id比较相等
  84. //说明存在这个学生,把索引值赋值给index
  85. index=i;
  86. }
  87. }
  88. return index;
  89. }
  90. }