Home >Java >javaTutorial >Detailed explanation of implementation of Java writing console student status management system
Key technologies: Looping, switch condition judgment, object creation, ArrayList() dynamic array or collection, private
System functions:
1. Add students (student ID, name, age, place of residence) and traverse based on student ID. If the student ID already exists, re-enter it
2. Modify students based on student ID. Enter the required To modify the student's student ID, re-enter the name, age, and residence in sequence. If the student ID is entered incorrectly, you will be prompted to re-enter
3. Deleting students will be judged based on the student ID. If the student ID does not exist, a prompt will be prompted; If the number exists, delete all the information contained in the changed student number
4. View all student information and use loop traversal
5. Exit the program. If you do not choose, the program will return to the main page
Create student class
package student_status_management_system; public class Student { private String student_number; private String name; private String age; private String address; public Student() { } public Student(String student_number, String name, String age, String address) { this.student_number = student_number; this.name = name; this.age = age; this.address = address; } public String getStudent_number() { return student_number; } public void setStudent_number(String student_number) { this.student_number = student_number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Student ID duplicate problem
public static boolean repetition(ArrayList<Student>array,String studentnumber){ boolean flag = false; for (int i = 0;i<array.size();i++){ Student s = array.get(i); if (s.getStudent_number().equals(studentnumber)){ flag = true; break; } } return flag; }
Add student
public static void addStudent(ArrayList<Student> array) { String studentnumber; Scanner sc = new Scanner(System.in); while (true) { System.out.println("请输入学生学号:"); studentnumber = sc.nextLine(); boolean flag = repetition(array, studentnumber); if (flag) { System.out.println("该学号已存在!请重新输入!"); } else { break; } } System.out.println("请输入学生姓名:"); String name = sc.nextLine(); System.out.println("请输入学生年龄:"); String age = sc.nextLine(); System.out.println("请输入学生居住地:"); String address = sc.nextLine(); //创建学生对象,把键盘录入的学生信息赋值给学生对象的成员变量 Student s = new Student(); s.setStudent_number(studentnumber); s.setName(name); s.setAge(age); s.setAddress(address); //将信息添加到集合 array.add(s); System.out.println("添加成功!"); }
Modify student information
public static void updateStudent(ArrayList<Student> array) { int index = -1; Scanner sc = new Scanner(System.in); System.out.println("请输入你要修改的学生的学号:"); String id = sc.nextLine(); for (int i = 0; i < array.size(); i++) { Student student = array.get(i); if (student.getStudent_number().equals(id)) { index = i; break; } } if (index == -1) { System.out.println("该学号不存在!"); } else { System.out.println("请输入新的学生姓名:"); String name = sc.nextLine(); System.out.println("请输入新的学生年龄:"); String age = sc.nextLine(); System.out.println("请输入新的学生居住地:"); String address = sc.nextLine(); Student s = new Student(); s.setStudent_number(id); s.setName(name); s.setAge(age); s.setAddress(address); array.set(index, s); System.out.println("修改成功!"); } }
Delete students
public static void deleteStudent(ArrayList<Student> array) { int index = -1; //定义索引 Scanner sc = new Scanner(System.in); System.out.println("请输入你要删除的学生的学号:"); String id = sc.nextLine(); for (int i = 0; i < array.size(); i++) { Student s = array.get(i); if (s.getStudent_number().equals(id)) { index = i; break; } } if (index == -1) { System.out.println("该学号不存在!"); } else { array.remove(index); System.out.println("删除成功!"); } }
View all students
public static void findAllStudent(ArrayList<Student> array) { //显示表头 if (array.size() == 0) { System.out.println("你还没有输入信息!"); return;//阻止程序往下执行 } System.out.println("学号\t\t\t\t姓名\t\t年龄\t\t\t居住地"); for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getStudent_number() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "岁" + "\t\t" + s.getAddress()); } }
main method
public static void main(String[] args) { //创建集合,储存学生信息 ArrayList<Student> array = new ArrayList<>(); 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 退出系统"); //主界面 Scanner sc = new Scanner(System.in); System.out.println("请输入你要进行的操作(数字序号):"); String line = sc.nextLine(); switch (line) { case "1": addStudent(array); break; case "2": updateStudent(array); break; case "3": deleteStudent(array); break; case "4": findAllStudent(array); break; case "5": System.out.println("再见!"); System.exit(0);//JVM退出 } } }
Effect demonstration
Add students
## Modify students##Delete students
Exit the program
View students without adding student information
The above is the detailed content of Detailed explanation of implementation of Java writing console student status management system. For more information, please follow other related articles on the PHP Chinese website!