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
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. 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
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
- package pojo;
-
- public class Student {
- //学生姓名
- private String name;
- //学生年龄
- private int age;
- //学生学号
- private int id;
- //班级信息
- private String team;
- }
Encapsulate student information fields
At this point, the Student class encapsulation of student information is completed
- package pojo;
-
- public class Student {
- //学生姓名
- private String name;
- //学生年龄
- private int age;
- //学生学号
- private int id;
- //班级信息
- private String team;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getTeam() {
- return team;
- }
-
- public void setTeam(String team) {
- this.team = team;
- }
-
- public Student() {
- }
-
- public Student(String name, int age, int id, String team) {
- this.name = name;
- this.age = age;
- this.id = id;
- this.team = team;
- }
-
-
-
- }
- Scanner sc = new Scanner(System.in);
- public void addStudent(ArrayList<Student> list) {
- //判断集合对象的长度,如果list.size()==0则集合中没有对象,所以创建第一个对象
- if (list.size()==0){
- System.out.println("请输入学号");
- int id = sc.nextInt();
- System.out.println("请输入姓名");
- String name = sc.next();
- System.out.println("请输入年龄");
- int age = sc.nextInt();
- System.out.println("请输入班级");
- String team = sc.next();
- Student stu = new Student(name, age, id, team);
- list.add(stu);
- System.out.println("添加学生" + stu.getName() + "成功");
- }else {
- //否则集合中已存在对象
- lo:while (true) {
- System.out.println("请输入学号");
- int id = sc.nextInt();
- //通过遍历集合查找各个对象的id值
- for (int i = 0; i < list.size(); i++) {
- Student student = list.get(i);
- //如果我们通过键盘录入的id值与已有对象的id值相匹配则重新添加信息
- if (student.getId() == id) {
- System.out.println("该学号已存在请重新添加:");
- break lo;
- }
- //否则添加信息
- else {
- System.out.println("请输入姓名");
- String name = sc.next();
- System.out.println("请输入年龄");
- int age = sc.nextInt();
- System.out.println("请输入班级");
- String team = sc.next();
- Student stu = new Student(name, age, id, team);
- list.add(stu);
- System.out.println("添加学生" + stu.getName() + "成功");
- break lo;
- }
- }
- }
- }
-
- }
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.
-
- //查询学生信息功能
- public void printAllStudent(ArrayList<Student> list) {
- //通过Main启动类中studentDao.addStudent(list)传入学生对象list集合本身
- // 而ArrayList<Student> list接受
- // list.isEmpty()判断列表是否包含元素,不包含元素则返回 true,否则返回false
- if (list.isEmpty()) {
- System.out.println("列表中没有学生信息。");
- return;//弹栈退出
- }else {
- System.out.println("学号\t姓名\t年龄\t班级");
- for (int i = 0; i < list.size(); i++) {
- //遍历集合
-
- Student student = list.get(i);
- System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getTeam());
- }
- }
-
- }
- //根据学生学号删除学生信息功能
- public void delStudent(ArrayList<Student> list, int id) {
- //通过遍历集合得到各个对象的id值
- for (int i = 0; i < list.size(); i++) {
- Student student = list.get(i);
- //若某对象的id值与传入id值相匹配,从而根据id删除信息
- if (student.getId() == id) {
- list.remove(student);
- System.out.println("删除"+student.getName()+"成功");
- break;
- } else {
- System.out.println("没有找到学号为:"+student.getId()+"的学号删除失败");
- break;
- }
-
- }
- }
- //修改学生信息功能
- public void updateStudent(ArrayList<Student> list, int id1) {
- //通过遍历集合得到集合某索引的对象中的id值与传入id1值相匹配,从而根据id修改信息
- for (int i = 0; i < list.size(); i++) {
- Student student = list.get(i);
- if (student.getId() == id1) {
- System.out.println("请输入新的名字:");
- String name = sc.next();
- System.out.println("请输入新的年龄:");
- int age = sc.nextInt();
- System.out.println("请输入新的班级:");
- String team = sc.next();
- //其中student.getId()是获取原对象的id(做到学号不作修改)
- Student student1 = new Student(name, age,student.getId(), team);
- /*如果你传入的学号(num)与集合对象的某个索引匹配,
- 则将新得到的student1插入该索引的对象当中*/
- list.set(i, student1);
- System.out.println("修改成功");
- break;
- }
- }
- System.out.println("没有"+id1+"该学号");
-
-
- }
- package service;
-
- import pojo.Student;
-
- import java.util.ArrayList;
- import java.util.Scanner;
-
- public class StudentDao {
-
- Scanner sc = new Scanner(System.in);
- //查询学生信息功能
- public void printAllStudent(ArrayList<Student> list) {
- //通过Main启动类中studentDao.addStudent(list)传入学生对象list集合本身
- // 而ArrayList<Student> list接受
- // list.isEmpty()判断列表是否包含元素,不包含元素则返回 true,否则返回false
- if (list.isEmpty()) {
- System.out.println("列表中没有学生信息。");
- return;//弹栈退出
- }else {
- System.out.println("学号\t姓名\t年龄\t班级");
- for (int i = 0; i < list.size(); i++) {
- //遍历集合
-
- Student student = list.get(i);
- System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getTeam());
- }
- }
-
- }
- //添加学生信息功能(学生学号是唯一的)
- public void addStudent(ArrayList<Student> list) {
- //判断集合对象的长度,如果list.size()==0则集合中没有对象,所以创建第一个对象
- if (list.size()==0){
- System.out.println("请输入学号");
- int id = sc.nextInt();
- System.out.println("请输入姓名");
- String name = sc.next();
- System.out.println("请输入年龄");
- int age = sc.nextInt();
- System.out.println("请输入班级");
- String team = sc.next();
- Student stu = new Student(name, age, id, team);
- list.add(stu);
- System.out.println("添加学生" + stu.getName() + "成功");
- }else {
- //否则集合中已存在对象
- lo:while (true) {
- System.out.println("请输入学号");
- int id = sc.nextInt();
- //通过遍历集合查找各个对象的id值
- for (int i = 0; i < list.size(); i++) {
- Student student = list.get(i);
- //如果我们通过键盘录入的id值与已有对象的id值相匹配则重新添加信息
- if (student.getId() == id) {
- System.out.println("该学号已存在请重新添加:");
- break lo;
- }
- //否则添加信息
- else {
- System.out.println("请输入姓名");
- String name = sc.next();
- System.out.println("请输入年龄");
- int age = sc.nextInt();
- System.out.println("请输入班级");
- String team = sc.next();
- Student stu = new Student(name, age, id, team);
- list.add(stu);
- System.out.println("添加学生" + stu.getName() + "成功");
- break lo;
- }
- }
- }
- }
-
- }
-
- //根据学生学号删除学生信息功能
- public void delStudent(ArrayList<Student> list, int id) {
- //通过遍历集合得到各个对象的id值
- for (int i = 0; i < list.size(); i++) {
- Student student = list.get(i);
- //若某对象的id值与传入id值相匹配,从而根据id删除信息
- if (student.getId() == id) {
- list.remove(student);
- System.out.println("删除"+student.getName()+"成功");
- break;
- } else {
- System.out.println("没有找到学号为:"+student.getId()+"的学号删除失败");
- break;
- }
-
- }
- }
- //修改学生信息功能
- public void updateStudent(ArrayList<Student> list, int id1) {
- //通过遍历集合得到集合某索引的对象中的id值与传入id1值相匹配,从而根据id修改信息
- for (int i = 0; i < list.size(); i++) {
- Student student = list.get(i);
- if (student.getId() == id1) {
- System.out.println("请输入新的名字:");
- String name = sc.next();
- System.out.println("请输入新的年龄:");
- int age = sc.nextInt();
- System.out.println("请输入新的班级:");
- String team = sc.next();
- //其中student.getId()是获取原对象的id(做到学号不作修改)
- Student student1 = new Student(name, age,student.getId(), team);
- /*如果你传入的学号(num)与集合对象的某个索引匹配,
- 则将新得到的student1插入该索引的对象当中*/
- list.set(i, student1);
- System.out.println("修改成功");
- break;
- }
- }
- System.out.println("没有"+id1+"该学号");
-
-
- }
- }
Define the ArrayList<Student> list collection on the main page to store student objects
- import pojo.Student;
- import service.StudentDao;
-
- import java.util.ArrayList;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- //调用service层中StudentDao中的方法(实例化对象studentDao)(增删改查)
- StudentDao studentDao = new StudentDao();
- //定义学生对象的一个数组
- ArrayList<Student> list = new ArrayList<>();
- Scanner sc = new Scanner(System.in);
- lo:
- //通过while循环无限进入我们的系统,直到选5退出结束
- while (true) {
- System.out.println("------欢迎来到学生信息管理系统------");
- System.out.println("1 添加学生信息");
- System.out.println("2 删除学生信息");
- System.out.println("3 修改学生信息");
- System.out.println("4 查看学生信息");
- System.out.println("5 退出");
- System.out.println("请输入你的选择:");
- //通过switch-case语句实现我们要选择的功能
- int choice = sc.nextInt();
- switch (choice) {
- //通过调用studentDao实例化对象中的方法实现(增删改查)
- case 1:
- /*向方法中传入定义的集合本身list
- 以便我们在实现方法时更好调用和处理集合中的每一个学生对象*/
- studentDao.addStudent(list);
- break;
- case 2:
- System.out.print("请输入你要删除的学生学号:");
- int id = sc.nextInt();
- //向delStudent()函数传入list集合,以及键盘录入的学号
- studentDao.delStudent(list, id);
- break;
- case 3:
- System.out.print("请输入你要修改的学生学号:");
- int id1 = sc.nextInt();
- studentDao.updateStudent(list, id1);
- case 4:
- studentDao.printAllStudent(list);
- break;
- case 5:
- System.out.println("退出成功");
- break lo;
-
- }
-
-
- }
-
- }
- }
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.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!