News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

2024-11(5)

Java——Student Information Management System (Simple + Super Detailed)

posted on 2024-11-02 14:18     read(532)     comment(0)     like(29)     collect(1)


  Preface : This system uses basic Java technology, including the following technology: ArrayList collection stores student object information, and implements a very simple student information management system, which implements the functions of adding student information, deleting student information, modifying student information, and viewing student information through keyboard input on the console. There are complete codes and comments.

Note: This system was written by the blogger according to his own ideas. The logic may be problematic or imperfect, so please forgive me. The code logic of adding student functions and the memory map used for storage are introduced in detail. The technologies used in other functions are similar to those of adding functions, so I will not introduce them in detail.

Table of contents

1. Main page display

2. Project Structure

3. Encapsulate the Student class

1. Encapsulate the student information field code part

2. Generate Getter and Setter methods

3. Generate parameterized and non-parameterized constructors

4. Student complete code

4. Service business logic layer StudentDao class encapsulation implementation method

1. Add student information code

2. Query student information function code

3. Delete the learning information function code

4. Modify student information function code

5. Complete code of StudentDao class

5. Main startup class writes the main page of the system

1. Complete code of Main startup class


1. Main page display

2. Project Structure

First create the pojo package and the Student class under the pojo package

The service layer is similar. After it is created, it looks like this

The pojo layer is used to encapsulate some entity classes, among which we encapsulate the Student class

The service layer is used to encapsulate business logic code (add, delete, modify, and query)

The Main startup class is used to implement the main interface

3. Encapsulate the Student class

1. Encapsulate the student information field code part

  1. package pojo;
  2. public class Student {
  3. //学生姓名
  4. private String name;
  5. //学生年龄
  6. private int age;
  7. //学生学号
  8. private int id;
  9. //班级信息
  10. private String team;
  11. }

Encapsulate student information fields

  • name Student name
  • age Student age
  • id Student ID
  • Team Class

2. Generate Getter and Setter methods

3. Generate parameterized and non-parameterized constructors

At this point, the Student class encapsulation of student information is completed

4. Student complete code

  1. package pojo;
  2. public class Student {
  3. //学生姓名
  4. private String name;
  5. //学生年龄
  6. private int age;
  7. //学生学号
  8. private int id;
  9. //班级信息
  10. private String team;
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. public int getId() {
  24. return id;
  25. }
  26. public void setId(int id) {
  27. this.id = id;
  28. }
  29. public String getTeam() {
  30. return team;
  31. }
  32. public void setTeam(String team) {
  33. this.team = team;
  34. }
  35. public Student() {
  36. }
  37. public Student(String name, int age, int id, String team) {
  38. this.name = name;
  39. this.age = age;
  40. this.id = id;
  41. this.team = team;
  42. }
  43. }

4. Service business logic layer StudentDao class encapsulation implementation method

1. Add student information code

  1. Scanner sc = new Scanner(System.in);
  2. public void addStudent(ArrayList<Student> list) {
  3. //判断集合对象的长度,如果list.size()==0则集合中没有对象,所以创建第一个对象
  4. if (list.size()==0){
  5. System.out.println("请输入学号");
  6. int id = sc.nextInt();
  7. System.out.println("请输入姓名");
  8. String name = sc.next();
  9. System.out.println("请输入年龄");
  10. int age = sc.nextInt();
  11. System.out.println("请输入班级");
  12. String team = sc.next();
  13. Student stu = new Student(name, age, id, team);
  14. list.add(stu);
  15. System.out.println("添加学生" + stu.getName() + "成功");
  16. }else {
  17. //否则集合中已存在对象
  18. lo:while (true) {
  19. System.out.println("请输入学号");
  20. int id = sc.nextInt();
  21. //通过遍历集合查找各个对象的id值
  22. for (int i = 0; i < list.size(); i++) {
  23. Student student = list.get(i);
  24. //如果我们通过键盘录入的id值与已有对象的id值相匹配则重新添加信息
  25. if (student.getId() == id) {
  26. System.out.println("该学号已存在请重新添加:");
  27. break lo;
  28. }
  29. //否则添加信息
  30. else {
  31. System.out.println("请输入姓名");
  32. String name = sc.next();
  33. System.out.println("请输入年龄");
  34. int age = sc.nextInt();
  35. System.out.println("请输入班级");
  36. String team = sc.next();
  37. Student stu = new Student(name, age, id, team);
  38. list.add(stu);
  39. System.out.println("添加学生" + stu.getName() + "成功");
  40. break lo;
  41. }
  42. }
  43. }
  44. }
  45. }

Code logic explanation 

The addStudent() method is called and processed by accepting the list collection passed in by the Main startup class

  First, we use list.size()==0 to determine whether we are storing the first student object, because our student ID is usually unique, but the name, age, and class are not unique and can have the same name. The first student object we store does not have a duplicate student ID. Therefore, the first student object uses the keyboard to enter information into the stu object, and then adds the stu object to our list collection.

For example:

Through the memory diagram after list.add(stu);, I also roughly abstracted it, and I don’t quite understand it (don’t criticize me)

When we enter the second student object information, there is already a stu object with index 0 in the list (memory address 001)

Then the condition list.size()==0 is not true, at this time list.size()==1

If (list.size()==0) the judgment fails, execute else

We input the information of the second student through the keyboard. At this time, we traverse the for loop to get the address of the object in the collection and assign it to student. At this time, the memory address of student is 001

At this time, if the student ID we enter on the keyboard is the same as the student ID of object 001 in the collection, a prompt will be given

That is, when student.getId() == id

This student number already exists, please add it again:

Otherwise, if student.getId() != id, the code in the following else statement will be executed to add the second student information and store it in the list collection. The index of the second object in the collection is 1.

2. Query student information function code

  1. //查询学生信息功能
  2. public void printAllStudent(ArrayList<Student> list) {
  3. //通过Main启动类中studentDao.addStudent(list)传入学生对象list集合本身
  4. // 而ArrayList<Student> list接受
  5. // list.isEmpty()判断列表是否包含元素,不包含元素则返回 true,否则返回false
  6. if (list.isEmpty()) {
  7. System.out.println("列表中没有学生信息。");
  8. return;//弹栈退出
  9. }else {
  10. System.out.println("学号\t姓名\t年龄\t班级");
  11. for (int i = 0; i < list.size(); i++) {
  12. //遍历集合
  13. Student student = list.get(i);
  14. System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getTeam());
  15. }
  16. }
  17. }

3. Delete the learning information function code

  1. //根据学生学号删除学生信息功能
  2. public void delStudent(ArrayList<Student> list, int id) {
  3. //通过遍历集合得到各个对象的id值
  4. for (int i = 0; i < list.size(); i++) {
  5. Student student = list.get(i);
  6. //若某对象的id值与传入id值相匹配,从而根据id删除信息
  7. if (student.getId() == id) {
  8. list.remove(student);
  9. System.out.println("删除"+student.getName()+"成功");
  10. break;
  11. } else {
  12. System.out.println("没有找到学号为:"+student.getId()+"的学号删除失败");
  13. break;
  14. }
  15. }
  16. }

4. Modify student information function code

  1. //修改学生信息功能
  2. public void updateStudent(ArrayList<Student> list, int id1) {
  3. //通过遍历集合得到集合某索引的对象中的id值与传入id1值相匹配,从而根据id修改信息
  4. for (int i = 0; i < list.size(); i++) {
  5. Student student = list.get(i);
  6. if (student.getId() == id1) {
  7. System.out.println("请输入新的名字:");
  8. String name = sc.next();
  9. System.out.println("请输入新的年龄:");
  10. int age = sc.nextInt();
  11. System.out.println("请输入新的班级:");
  12. String team = sc.next();
  13. //其中student.getId()是获取原对象的id(做到学号不作修改)
  14. Student student1 = new Student(name, age,student.getId(), team);
  15. /*如果你传入的学号(num)与集合对象的某个索引匹配,
  16. 则将新得到的student1插入该索引的对象当中*/
  17. list.set(i, student1);
  18. System.out.println("修改成功");
  19. break;
  20. }
  21. }
  22. System.out.println("没有"+id1+"该学号");
  23. }

5. Complete code of StudentDao class

  1. package service;
  2. import pojo.Student;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5. public class StudentDao {
  6. Scanner sc = new Scanner(System.in);
  7. //查询学生信息功能
  8. public void printAllStudent(ArrayList<Student> list) {
  9. //通过Main启动类中studentDao.addStudent(list)传入学生对象list集合本身
  10. // 而ArrayList<Student> list接受
  11. // list.isEmpty()判断列表是否包含元素,不包含元素则返回 true,否则返回false
  12. if (list.isEmpty()) {
  13. System.out.println("列表中没有学生信息。");
  14. return;//弹栈退出
  15. }else {
  16. System.out.println("学号\t姓名\t年龄\t班级");
  17. for (int i = 0; i < list.size(); i++) {
  18. //遍历集合
  19. Student student = list.get(i);
  20. System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getTeam());
  21. }
  22. }
  23. }
  24. //添加学生信息功能(学生学号是唯一的)
  25. public void addStudent(ArrayList<Student> list) {
  26. //判断集合对象的长度,如果list.size()==0则集合中没有对象,所以创建第一个对象
  27. if (list.size()==0){
  28. System.out.println("请输入学号");
  29. int id = sc.nextInt();
  30. System.out.println("请输入姓名");
  31. String name = sc.next();
  32. System.out.println("请输入年龄");
  33. int age = sc.nextInt();
  34. System.out.println("请输入班级");
  35. String team = sc.next();
  36. Student stu = new Student(name, age, id, team);
  37. list.add(stu);
  38. System.out.println("添加学生" + stu.getName() + "成功");
  39. }else {
  40. //否则集合中已存在对象
  41. lo:while (true) {
  42. System.out.println("请输入学号");
  43. int id = sc.nextInt();
  44. //通过遍历集合查找各个对象的id值
  45. for (int i = 0; i < list.size(); i++) {
  46. Student student = list.get(i);
  47. //如果我们通过键盘录入的id值与已有对象的id值相匹配则重新添加信息
  48. if (student.getId() == id) {
  49. System.out.println("该学号已存在请重新添加:");
  50. break lo;
  51. }
  52. //否则添加信息
  53. else {
  54. System.out.println("请输入姓名");
  55. String name = sc.next();
  56. System.out.println("请输入年龄");
  57. int age = sc.nextInt();
  58. System.out.println("请输入班级");
  59. String team = sc.next();
  60. Student stu = new Student(name, age, id, team);
  61. list.add(stu);
  62. System.out.println("添加学生" + stu.getName() + "成功");
  63. break lo;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. //根据学生学号删除学生信息功能
  70. public void delStudent(ArrayList<Student> list, int id) {
  71. //通过遍历集合得到各个对象的id值
  72. for (int i = 0; i < list.size(); i++) {
  73. Student student = list.get(i);
  74. //若某对象的id值与传入id值相匹配,从而根据id删除信息
  75. if (student.getId() == id) {
  76. list.remove(student);
  77. System.out.println("删除"+student.getName()+"成功");
  78. break;
  79. } else {
  80. System.out.println("没有找到学号为:"+student.getId()+"的学号删除失败");
  81. break;
  82. }
  83. }
  84. }
  85. //修改学生信息功能
  86. public void updateStudent(ArrayList<Student> list, int id1) {
  87. //通过遍历集合得到集合某索引的对象中的id值与传入id1值相匹配,从而根据id修改信息
  88. for (int i = 0; i < list.size(); i++) {
  89. Student student = list.get(i);
  90. if (student.getId() == id1) {
  91. System.out.println("请输入新的名字:");
  92. String name = sc.next();
  93. System.out.println("请输入新的年龄:");
  94. int age = sc.nextInt();
  95. System.out.println("请输入新的班级:");
  96. String team = sc.next();
  97. //其中student.getId()是获取原对象的id(做到学号不作修改)
  98. Student student1 = new Student(name, age,student.getId(), team);
  99. /*如果你传入的学号(num)与集合对象的某个索引匹配,
  100. 则将新得到的student1插入该索引的对象当中*/
  101. list.set(i, student1);
  102. System.out.println("修改成功");
  103. break;
  104. }
  105. }
  106. System.out.println("没有"+id1+"该学号");
  107. }
  108. }

5. Main startup class writes the main page of the system

1. Complete code of Main startup class

Define the ArrayList<Student> list collection on the main page to store student objects

  1. import pojo.Student;
  2. import service.StudentDao;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5. public class Main {
  6. public static void main(String[] args) {
  7. //调用service层中StudentDao中的方法(实例化对象studentDao)(增删改查)
  8. StudentDao studentDao = new StudentDao();
  9. //定义学生对象的一个数组
  10. ArrayList<Student> list = new ArrayList<>();
  11. Scanner sc = new Scanner(System.in);
  12. lo:
  13. //通过while循环无限进入我们的系统,直到选5退出结束
  14. while (true) {
  15. System.out.println("------欢迎来到学生信息管理系统------");
  16. System.out.println("1 添加学生信息");
  17. System.out.println("2 删除学生信息");
  18. System.out.println("3 修改学生信息");
  19. System.out.println("4 查看学生信息");
  20. System.out.println("5 退出");
  21. System.out.println("请输入你的选择:");
  22. //通过switch-case语句实现我们要选择的功能
  23. int choice = sc.nextInt();
  24. switch (choice) {
  25. //通过调用studentDao实例化对象中的方法实现(增删改查)
  26. case 1:
  27. /*向方法中传入定义的集合本身list
  28. 以便我们在实现方法时更好调用和处理集合中的每一个学生对象*/
  29. studentDao.addStudent(list);
  30. break;
  31. case 2:
  32. System.out.print("请输入你要删除的学生学号:");
  33. int id = sc.nextInt();
  34. //向delStudent()函数传入list集合,以及键盘录入的学号
  35. studentDao.delStudent(list, id);
  36. break;
  37. case 3:
  38. System.out.print("请输入你要修改的学生学号:");
  39. int id1 = sc.nextInt();
  40. studentDao.updateStudent(list, id1);
  41. case 4:
  42. studentDao.printAllStudent(list);
  43. break;
  44. case 5:
  45. System.out.println("退出成功");
  46. break lo;
  47. }
  48. }
  49. }
  50. }



Category of website: technical article > Blog

Author:Fiee

link:http://www.pythonblackhole.com/blog/article/245808/92fd2917d6298b485e90/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

29 0
collect article
collected

Comment content: (supports up to 255 characters)