提示信息

Java学生信息管理系统:简单与详细的实现

阿三 已关注

Java学生信息管理系统可以用于管理学生的信息,如学生的姓名、学号、课程等。下面我将提供一个简单版本以及一个详细版本的实现方案。

简单实现

功能: 添加学生、查看所有学生信息、根据学号删除学生。

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. class Student {
  4. private String name;
  5. private String id;
  6. public Student(String name, String id) {
  7. this.name = name;
  8. this.id = id;
  9. }
  10. public String getName() {
  11. return name;
  12. }
  13. public String getId() {
  14. return id;
  15. }
  16. @Override
  17. public String toString() {
  18. return "Student ID: " + id + ", Name: " + name;
  19. }
  20. }
  21. public class StudentManagementSystem {
  22. private ArrayList<Student> students = new ArrayList<>();
  23. private Scanner scanner = new Scanner(System.in);
  24. public static void main(String[] args) {
  25. new StudentManagementSystem().run();
  26. }
  27. private void run() {
  28. while (true) {
  29. System.out.println("\n1. Add Student");
  30. System.out.println("2. View Students");
  31. System.out.println("3. Delete Student");
  32. System.out.println("4. Exit");
  33. System.out.print("Choose an option: ");
  34. int choice = scanner.nextInt();
  35. scanner.nextLine(); // Consume newline
  36. switch (choice) {
  37. case 1:
  38. addStudent();
  39. break;
  40. case 2:
  41. viewStudents();
  42. break;
  43. case 3:
  44. deleteStudent();
  45. break;
  46. case 4:
  47. System.out.println("Exiting...");
  48. return;
  49. default:
  50. System.out.println("Invalid choice. Try again.");
  51. }
  52. }
  53. }
  54. private void addStudent() {
  55. System.out.print("Enter Student Name: ");
  56. String name = scanner.nextLine();
  57. System.out.print("Enter Student ID: ");
  58. String id = scanner.nextLine();
  59. students.add(new Student(name, id));
  60. System.out.println("Student added successfully.");
  61. }
  62. private void viewStudents() {
  63. if (students.isEmpty()) {
  64. System.out.println("No students found.");
  65. return;
  66. }
  67. for (Student student : students) {
  68. System.out.println(student);
  69. }
  70. }
  71. private void deleteStudent() {
  72. System.out.print("Enter Student ID to delete: ");
  73. String id = scanner.nextLine();
  74. students.removeIf(student -> student.getId().equals(id));
  75. System.out.println("Student deleted if existed.");
  76. }
  77. }

详细实现

功能: 在简单版本基础上增加更新学生信息、查找学生,以及持久化数据到文件中。

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.io.*;
  4. class Student {
  5. private String name;
  6. private String id;
  7. private String course;
  8. public Student(String name, String id, String course) {
  9. this.name = name;
  10. this.id = id;
  11. this.course = course;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public String getId() {
  17. return id;
  18. }
  19. public String getCourse() {
  20. return course;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public void setCourse(String course) {
  26. this.course = course;
  27. }
  28. @Override
  29. public String toString() {
  30. return "Student ID: " + id + ", Name: " + name + ", Course: " + course;
  31. }
  32. }
  33. public class StudentManagementSystem {
  34. private ArrayList<Student> students = new ArrayList<>();
  35. private Scanner scanner = new Scanner(System.in);
  36. private static final String FILE_PATH = "students.txt";
  37. public static void main(String[] args) {
  38. new StudentManagementSystem().run();
  39. }
  40. private void run() {
  41. loadStudents();
  42. while (true) {
  43. System.out.println("\n1. Add Student");
  44. System.out.println("2. View Students");
  45. System.out.println("3. Delete Student");
  46. System.out.println("4. Update Student");
  47. System.out.println("5. Find Student");
  48. System.out.println("6. Exit");
  49. System.out.print("Choose an option: ");
  50. int choice = scanner.nextInt();
  51. scanner.nextLine(); // Consume newline
  52. switch (choice) {
  53. case 1:
  54. addStudent();
  55. break;
  56. case 2:
  57. viewStudents();
  58. break;
  59. case 3:
  60. deleteStudent();
  61. break;
  62. case 4:
  63. updateStudent();
  64. break;
  65. case 5:
  66. findStudent();
  67. break;
  68. case 6:
  69. saveStudents();
  70. System.out.println("Exiting...");
  71. return;
  72. default:
  73. System.out.println("Invalid choice. Try again.");
  74. }
  75. }
  76. }
  77. private void addStudent() {
  78. System.out.print("Enter Student Name: ");
  79. String name = scanner.nextLine();
  80. System.out.print("Enter Student ID: ");
  81. String id = scanner.nextLine();
  82. System.out.print("Enter Course: ");
  83. String course = scanner.nextLine();
  84. students.add(new Student(name, id, course));
  85. System.out.println("Student added successfully.");
  86. }
  87. private void viewStudents() {
  88. if (students.isEmpty()) {
  89. System.out.println("No students found.");
  90. return;
  91. }
  92. for (Student student : students) {
  93. System.out.println(student);
  94. }
  95. }
  96. private void deleteStudent() {
  97. System.out.print("Enter Student ID to delete: ");
  98. String id = scanner.nextLine();
  99. students.removeIf(student -> student.getId().equals(id));
  100. System.out.println("Student deleted if existed.");
  101. }
  102. private void updateStudent() {
  103. System.out.print("Enter Student ID to update: ");
  104. String id = scanner.nextLine();
  105. for (Student student : students) {
  106. if (student.getId().equals(id)) {
  107. System.out.print("Enter new name: ");
  108. String newName = scanner.nextLine();
  109. System.out.print("Enter new course: ");
  110. String newCourse = scanner.nextLine();
  111. student.setName(newName);
  112. student.setCourse(newCourse);
  113. System.out.println("Student updated.");
  114. return;
  115. }
  116. }
  117. System.out.println("Student not found.");
  118. }
  119. private void findStudent() {
  120. System.out.print("Enter Student ID to find: ");
  121. String id = scanner.nextLine();
  122. for (Student student : students) {
  123. if (student.getId().equals(id)) {
  124. System.out.println(student);
  125. return;
  126. }
  127. }
  128. System.out.println("Student not found.");
  129. }
  130. private void loadStudents() {
  131. try (BufferedReader br = new BufferedReader(new FileReader(FILE_PATH))) {
  132. String line;
  133. while ((line = br.readLine()) != null) {
  134. String[] parts = line.split(",");
  135. if (parts.length == 3) {
  136. students.add(new Student(parts[0], parts[1], parts[2]));
  137. }
  138. }
  139. } catch (IOException e) {
  140. System.out.println("Error loading students: " + e.getMessage());
  141. }
  142. }
  143. private void saveStudents() {
  144. try (PrintWriter pw = new PrintWriter(new FileWriter(FILE_PATH))) {
  145. for (Student student : students) {
  146. pw.println(student.getName() + "," + student.getId() + "," + student.getCourse());
  147. }
  148. } catch (IOException e) {
  149. System.out.println("Error saving students: " + e.getMessage());
  150. }
  151. }
  152. }

说明

  • 简单实现适合初学者和缺少复杂功能的管理系统,提供最基本的CRUD操作。
  • 详细实现增加了学生信息更新、查找功能,并使用文件存储学生信息,以便程序关闭后数据不丢失。
  • 可以根据需求扩展功能,例如实现图形界面、加入异常处理、使用数据库来代替文件存储等。
    遇到难题? "AI大模型GPT4.0、GPT" 是你的私人解答专家! 点击按钮去提问......
阿三 关注 已关注

最近一次登录:2024-11-20 03:37:51   

暂时还没有签名,请关注我或评论我的文章
×
免费图表工具,画流程图、架构图