学生类
1. package timberman666; 2. 3. public class Student { 4. private String id; 5. private String name; 6. private int age; 7. private String address; 8. 9. public Student() { 10. } 11. 12. public Student(String id, String name, int age, String address) { 13. this.id = id; 14. this.name = name; 15. this.age = age; 16. this.address = address; 17. } 18. 19. /** 20. * 获取 21. * @return id 22. */ 23. public String getId() { 24. return id; 25. } 26. 27. /** 28. * 设置 29. * @param id 30. */ 31. public void setId(String id) { 32. this.id = id; 33. } 34. 35. /** 36. * 获取 37. * @return name 38. */ 39. public String getName() { 40. return name; 41. } 42. 43. /** 44. * 设置 45. * @param name 46. */ 47. public void setName(String name) { 48. this.name = name; 49. } 50. 51. /** 52. * 获取 53. * @return age 54. */ 55. public int getAge() { 56. return age; 57. } 58. 59. /** 60. * 设置 61. * @param age 62. */ 63. public void setAge(int age) { 64. this.age = age; 65. } 66. 67. /** 68. * 获取 69. * @return address 70. */ 71. public String getAddress() { 72. return address; 73. } 74. 75. /** 76. * 设置 77. * @param address 78. */ 79. public void setAddress(String address) { 80. this.address = address; 81. } 82. 83. public String toString() { 84. return "Student{id = " + id + ", name = " + name + ", age = " + age + ", address = " + address + "}"; 85. } 86. }
菜单
1. loop:while (true) { 2. System.out.println("Welcome to the timberman's Student Management System"); 3. System.out.println("1:Add"); 4. System.out.println("2:Delete"); 5. System.out.println("3:Modify"); 6. System.out.println("4:Query"); 7. System.out.println("5:Quit"); 8. System.out.println("Please enter your choice"); 9. Scanner sc=new Scanner(System.in); 10. String choose= sc.next(); 11. switch (choose) 12. { 13. case"1"-> addStudent(list); 14. case"2"-> deleteStudent(list); 15. case"3"-> modifyStudent(list); 16. case"4"-> queryStudent(list); 17. case"5"-> { 18. System.out.println("Quit"); 19. break loop; 20. } 21. default -> System.out.println("There is no such option"); 22. } 23. }
添加学生信息功能的实现
1. //add student 2. public static void addStudent(ArrayList<Student> list) { 3. System.out.println("Add a student"); 4. Student s= new Student(); 5. Scanner sc=new Scanner(System.in); 6. 7. String id=null; 8. while (true) { 9. System.out.println("Please enter the student's id"); 10. id=sc.next(); 11. boolean flag=contains(list,id); 12. if(flag){ 13. System.out.println("The ID already exists, please re-enter it"); 14. } 15. else{ 16. s.setId(id); 17. break; 18. } 19. 20. } 21. 22. System.out.println("Please enter the student's name"); 23. String name=sc.next(); 24. s.setName(name); 25. 26. System.out.println("Please enter the student's age"); 27. int age= sc.nextInt(); 28. s.setAge(age); 29. 30. System.out.println("Please enter the student's address"); 31. String address = sc.next(); 32. s.setAddress(address); 33. 34. list.add(s); 35. 36. System.out.println("Student information added successfully"); 37. }
删除学生信息功能的实现
1. //delete student 2. public static void deleteStudent(ArrayList<Student> list) { 3. System.out.println("Delete a student"); 4. Scanner sc=new Scanner(System.in); 5. String id=sc.next(); 6. int index=getIndex(list,id); 7. if(index>=0){ 8. list.remove(index); 9. } 10. else{ 11. System.out.println("id does not exist, deletion failed"); 12. } 13. 14. }
修改学生信息功能的实现
1. //modify student 2. public static void modifyStudent(ArrayList<Student> list) { 3. System.out.println("Modify a student"); 4. Scanner sc=new Scanner(System.in); 5. String id=sc.next(); 6. int index=getIndex(list,id); 7. if(index==-1){ 8. System.out.println("The ID to be modified does not exist " +id+ " please re-enter it"); 9. return; 10. } 11. Student stu=list.get(index); 12. System.out.println("Please enter the name of the student you want to modify"); 13. String newName= sc.next(); 14. stu.setName(newName); 15. 16. System.out.println("Please enter the age of the student you want to modify"); 17. int newAge= sc.nextInt(); 18. stu.setAge(newAge); 19. 20. System.out.println("Please enter the address of the student you want to modify"); 21. String newAddress= sc.next(); 22. stu.setAddress(newAddress); 23. 24. System.out.println("The student's information was modified successfully"); 25. }
查询学生信息的实现
1. //query student 2. public static void queryStudent(ArrayList<Student> list) { 3. System.out.println("Query a student"); 4. if(list.size()==0) 5. { 6. System.out.println("There is currently no student information, please add it and inquire"); 7. return; 8. } 9. System.out.println("id\t\tname\tage\taddress"); 10. for (int i = 0; i < list.size() ; i++) { 11. Student stu=list.get(i); 12. System.out.println(stu.getId()+"\t"+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress()); 13. 14. } 15. 16. }
通过ID获得下标功能的实现
1. //Get the index by id 2. public static int getIndex(ArrayList<Student> list,String id) { 3. for (int i = 0; i < list.size(); i++) { 4. Student stu=list.get(i); 5. String sid= stu.getId(); 6. if(sid.equals(id)) { 7. return i; 8. } 9. } 10. return -1; 11. }
判断ID是否唯一功能的实现
1. //Determine whether the id exists in the collection 2. public static boolean contains(ArrayList<Student> list,String id) { 3. for (int i = 0; i < list.size(); i++) { 4. Student s=list.get(i); 5. String sid=s.getId(); 6. if(sid.equals(id)){ 7. return true; 8. } 9. } 10. return false; 11. }