当前位置:   article > 正文

Java学习--学生管理系统(残破版)

Java学习--学生管理系统(残破版)

代码

Main.java

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void main(String[] args) {
  5. ArrayList<Student> list = new ArrayList<>();
  6. loop:
  7. while (true) {
  8. System.out.println("-----欢迎来到阿宝院校学生管理系统-----");
  9. System.out.println("1.添加学生");
  10. System.out.println("2.删除学生");
  11. System.out.println("3.修改学生");
  12. System.out.println("4.查询学生");
  13. System.out.println("5.退出系统");
  14. System.out.println("请输入您的选择:");
  15. Scanner sc = new Scanner(System.in);
  16. String choose = sc.next();
  17. switch (choose) {
  18. case "1" -> AddStudent(list);
  19. case "2" -> DeleteStudent(list);
  20. case "3" -> UpdateStudent(list);
  21. case "4" -> LookStudent(list);
  22. case "5" -> {
  23. System.out.println("退出!");
  24. break loop;
  25. }
  26. default -> System.out.println("没有这个选项!");
  27. }
  28. }
  29. }
  30. // 添加学生
  31. public static void AddStudent(ArrayList<Student> list) {
  32. System.out.println("添加学生!");
  33. Student s = new Student();
  34. Scanner sc = new Scanner(System.in);
  35. System.out.println("请录入id:");
  36. while (true) {
  37. String id = sc.next();
  38. boolean soleIds = soleId(list, id);
  39. if (soleIds) {
  40. System.out.println("该id已经存在!");
  41. } else {
  42. s.setId(id);
  43. break;
  44. }
  45. }
  46. System.out.println("请输入姓名:");
  47. String name = sc.next();
  48. s.setName(name);
  49. System.out.println("请输入年龄:");
  50. int age = sc.nextInt();
  51. s.setAge(age);
  52. System.out.println("请输入家庭住址:");
  53. String address = sc.next();
  54. s.setAddress(address);
  55. list.add(s);
  56. }
  57. //删除学生的信息
  58. public static void DeleteStudent(ArrayList<Student> list) {
  59. System.out.println("请输入要删除的学生id:");
  60. Scanner sc = new Scanner(System.in);
  61. String id = sc.next();
  62. // 查询有没有这个学生
  63. int indexValue = getIndex(list, id);
  64. if (indexValue >= 0) {
  65. list.remove(indexValue);
  66. System.out.println("删除成功!");
  67. } else {
  68. System.out.println("该id不存在!");
  69. }
  70. }
  71. //修改学生的信息
  72. public static void UpdateStudent(ArrayList<Student> list) {
  73. System.out.println("请输入要修改的学生id:");
  74. Scanner sc = new Scanner(System.in);
  75. String id = sc.next();
  76. // 查询有没有这个学生
  77. Boolean values = soleId(list, id);
  78. int indexValue = getIndex(list, id);
  79. if (values) {
  80. Student stu = list.get(indexValue);
  81. System.out.println("请输入要修改的姓名:");
  82. String name = sc.next();
  83. stu.setName(name);
  84. System.out.println("请输入要修改的年龄:");
  85. int age = sc.nextInt();
  86. stu.setAge(age);
  87. System.out.println("请输入要修改的家庭住址:");
  88. String address = sc.next();
  89. stu.setAddress(address);
  90. System.out.println("修改成功!");
  91. } else {
  92. System.out.println("该id不存在!");
  93. }
  94. }
  95. //查询学生的信息
  96. public static void LookStudent(ArrayList<Student> list) {
  97. //如果没有学生
  98. if (list.isEmpty()) {
  99. System.out.println("当前暂无学生,请先添加后再查询!");
  100. return;
  101. }
  102. System.out.println("id\t姓名\t年龄\t家庭住址");
  103. for (int i = 0; i < list.size(); i++) {
  104. Student stu = list.get(i);
  105. System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
  106. }
  107. }
  108. //判断id是否唯一
  109. public static Boolean soleId(ArrayList<Student> list, String id) {
  110. for (int i = 0; i < list.size(); i++) {
  111. Student stu = list.get(i);
  112. String ids = stu.getId();
  113. if (ids.equals(id)) {
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. //获取集合中的索引
  120. public static int getIndex(ArrayList<Student> list, String id) {
  121. for (int i = 0; i < list.size(); i++) {
  122. Student stu = list.get(i);
  123. String ids = stu.getId();
  124. if (ids.equals(id)) {
  125. return i;
  126. }
  127. }
  128. return -1;
  129. }
  130. }

Student.java

  1. public class Student {
  2. private String id;
  3. private String name;
  4. private int age;
  5. private String address;
  6. public Student() {
  7. }
  8. public Student(String id, String name, int age, String address) {
  9. this.id = id;
  10. this.name = name;
  11. this.age = age;
  12. this.address = address;
  13. }
  14. /**
  15. * 获取
  16. * @return id
  17. */
  18. public String getId() {
  19. return id;
  20. }
  21. /**
  22. * 设置
  23. * @param id
  24. */
  25. public void setId(String id) {
  26. this.id = id;
  27. }
  28. /**
  29. * 获取
  30. * @return name
  31. */
  32. public String getName() {
  33. return name;
  34. }
  35. /**
  36. * 设置
  37. * @param name
  38. */
  39. public void setName(String name) {
  40. this.name = name;
  41. }
  42. /**
  43. * 获取
  44. * @return age
  45. */
  46. public int getAge() {
  47. return age;
  48. }
  49. /**
  50. * 设置
  51. * @param age
  52. */
  53. public void setAge(int age) {
  54. this.age = age;
  55. }
  56. /**
  57. * 获取
  58. * @return address
  59. */
  60. public String getAddress() {
  61. return address;
  62. }
  63. /**
  64. * 设置
  65. * @param address
  66. */
  67. public void setAddress(String address) {
  68. this.address = address;
  69. }
  70. }

效果图

主界面

增加和查询

删除

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/183015
推荐阅读
相关标签
  

闽ICP备14008679号