Implement a simple student management system from 0 to 1 with a console
Needs Analysis:
You need to use the console to implement a student management system, run the program into the management system, and input the corresponding operands to achieve the corresponding functions. Functions include adding student information, viewing student information, deleting student information for a given number, modifying student information for a given number, and exiting the system.
Operation flow:
- Create a new project with IDEA, add new modules, create packages, student management classes.
-
Next, create a student class with member variables such as school number, name, age, and address, member methods get/set, and constructor parameters and reference constructors.
package StudentMannager; /* Student Class */ public class Student { //Member variables private String sid; private String name; private String age; private String address; //non-parameter constructor public Student() { } //Parameterized constructor public Student(String sid, String name, String age, String address) { this.sid = sid; this.name = name; this.age = age; this.address = address; } //get/set methods for individual member variables public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } 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; } }
-
Write the main interface first, requiring the display to enter the management system, prompting for the operands to be entered and the corresponding functions.
package StudentMannager; /* Student Management System */ public class StudentManager { public static void main(String[] args) { //Enter the main interface of the student management system System.out.println("--------Welcome to the Student Management System--------"); System.out.println("1.Add Students"); System.out.println("2.View all students"); System.out.println("3.Modify Student Information"); System.out.println("4.Delete Student Information"); System.out.println("5.Exit System"); //Number of operations prompted for input System.out.println("Enter the appropriate action:"); } }
-
Enter the operands from the keyboard, enter the corresponding function modules, and loop through the main interface until you exit the system.
package StudentMannager; import java.util.Scanner; /* Student Management System */ public class StudentManager { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Loop into student management system //Enter the main interface of the student management system while (true) { System.out.println("--------Welcome to the Student Management System--------"); System.out.println("1.Add Students"); System.out.println("2.View all students"); System.out.println("3.Modify Student Information"); System.out.println("4.Delete Student Information"); System.out.println("5.Exit System"); //Number of operations prompted for input System.out.println("Enter the appropriate action:"); String num = sc.nextLine(); //Judging Operands Enter Corresponding Function Modules switch (num) { case "1": System.out.println("Add Students"); break; case "2": System.out.println("View all students"); break; case "3": System.out.println("Modify Student Information"); break; case "4": System.out.println("Delete Students"); break; case "5": System.out.println("Exit System"); System.exit(0); default: System.out.println("The operand you entered is incorrect. Please re-enter it"); break; } } } }
-
Write the code to add the student function module, which requires that duplicate student numbers cannot be added, that the addition gives prompts successfully, and is called in the main method.
public static void addStudent(ArrayList<Student> array) { Scanner sc = new Scanner(System.in); //Define sid variable outside loop String sid; //Give hints //Determine if the number is repeated System.out.println("Please enter a number:"); while (true) { //Define Identity Variables int index = -1; sid = sc.nextLine(); for (int i = 0; i < array.size(); i++) { if (array.get(i).getSid().equals(sid)) { index = i; } } //Determine the value of the identifying variable if (index == -1) { break; } else { System.out.println("The number you entered already exists. Please re-enter the number:"); } } System.out.println("Please enter your name:"); String name = sc.nextLine(); System.out.println("Please enter age:"); String age = sc.nextLine(); System.out.println("Please enter an address:"); String address = sc.nextLine(); //Create Student Object Receive Student s = new Student(); s.setSid(sid); s.setName(name); s.setAge(age); s.setAddress(address); //Add Student Objects to Collection array.add(s); //Give hints System.out.println("Added Successfully"); }
-
Write a function code to check whether there are any students before checking, no students give prompts and go back to the menu interface. If there is a student, first output the header information, then output the student information in the corresponding place, pay attention to the layout of the console.
public static void findStudent(ArrayList<Student> array) { //First judge if there are any students, no students prompt to add students first if (array.size() == 0) { System.out.println("There is no student information at this time, Please add students first"); return; } //Create Student Object Student s = new Student(); //Give the header System.out.println("School Number\t\t\t Full name\t\t Age\t address"); //Output content in its format for (int i = 0; i < array.size(); i++) { s = array.get(i); System.out.println(s.getSid() + "\t" + s.getName() + "\t\t" + s.getAge() + "year\t" + s.getAddress()); } }
-
Write code to modify student information function. Require to modify student information according to the number. First, judge whether the student number exists, if it does not exist, give a prompt, if it exists, prompt to enter the specified information, make the modification, and finally give a prompt for the success of the modification.
public static void updateStudent(ArrayList<Student> array) { Scanner sc = new Scanner(System.in); //Give hints //Determine if the number you want to modify exists //Define identity variable index int index = -1; System.out.println("Please enter a number:"); String sid = sc.nextLine(); for (int i = 0; i < array.size(); i++) { if (array.get(i).getSid().equals(sid)) { index = i; } } //Identifying variables if (index == -1) { System.out.println("The student number you entered does not exist. Please add it before modifying it"); return; } System.out.println("Please enter your name:"); String name = sc.nextLine(); System.out.println("Please enter age:"); String age = sc.nextLine(); System.out.println("Please enter an address:"); String address = sc.nextLine(); //Modify Student Information array.get(index).setName(name); array.get(index).setAge(age); array.get(index).setAddress(address); //Give Successful Modification Tips System.out.println("Successful modification of student information"); }
-
Write delete student function code, ask to delete student information according to the school number, first determine if the student to delete exists, there is no prompt information, and delete by the school number if there is.
public static void deleteStudent(ArrayList<Student> array) { Scanner sc = new Scanner(System.in); //Prompt message //Define Identity Variables int index = -1; System.out.println("Enter the number of the student you want to delete:"); String sid = sc.nextLine(); //Determine if a school number exists for (int i = 0;i < array.size(); i++) { if (array.get(i).getSid().equals(sid)) { index = i; } } //Identifying variables if (index == -1) { System.out.println("The student number you entered does not exist"); return; } //Perform deletion array.remove(index); //Success hint for deletion System.out.println("Delete succeeded"); }
Such a simple student management system will do ~