Project introduction : use java Realize the management of student information , The entered data includes : Student ID , full name , Age , Residence, etc , And can realize the addition of student information , modify , delete , View function .

one , Create project

1, entry name :myStudentManager

two , create package

1, Package name :study

2, Names can also be named by themselves

three , Create two classes

1, Students (Studnet)
package Study; /* Students Alt + Insert Choose according to your needs */ public class Student { // Student ID
private String sid; // full name private String name; // Age private String age; // Place of residence
private String address; public Student() { } public Student(String sid, String
name, String age, String address) { this.sid = sid; this.name = name; this.age
= age; this.address = address; } 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; } }
2, Student tests (StudentManager)
package Study; import jdk.swing.interop.SwingInterOpUtils; import
java.util.ArrayList; import java.util.Scanner; /* Student management system */ public class
StudentManager { /* 1: Writing the main interface with output statements 2: use Scanner Realize keyboard input data 3: use switch Statement completion operation selection
4: Complete the cycle and return to the main interface again */ public static void main(String[] args) { // Create a collection object to store student data
ArrayList<Student> array = new ArrayList<>(); // Complete the cycle and return to the main interface again while (true) {
// Writing the main interface with output statements System.out.println("-------- Welcome to the student management system --------");
System.out.println("1 Add student "); System.out.println("2 Delete student ");
System.out.println("3 Modify student "); System.out.println("4 View all students ");
System.out.println("5 sign out "); System.out.println(" Please enter your choice :"); // use Scanner Realize keyboard input data
Scanner sc = new Scanner(System.in); String line = sc.nextLine();
// use switch Statement completion operation selection switch (line) { case "1": // System.out.println(" Add student ");
addStudent(array); break; case "2": // System.out.println(" Delete student ");
deleteStudent(array); break; case "3": // System.out.println(" Modify student ");
updateStudent(array); break; case "4": // System.out.println(" View all students ");
findAllStudent(array); break; case "5": System.out.println(" Thanks for using "); // break;
System.exit(0); //JVM(java virtual machine ) sign out } } } // Define a method , Used to add student information public static void
addStudent(ArrayList<Student> array) { // Enter the data required by the student object with the keyboard , Display prompt information , Prompt for what information to enter
Scanner sc = new Scanner(System.in); // In order to make sid Can in while Accessed outside the loop , We defined him outside the loop
String sid; while (true) { System.out.println(" Please enter student ID :"); sid = sc.nextLine();
boolean flag = isUsed(array, sid); if (flag) {
System.out.println(" The student ID you entered has been used , Please re-enter "); } else { break; } }
System.out.println(" Please enter student name :"); String name = sc.nextLine();
System.out.println(" Please enter student age :"); String age = sc.nextLine();
System.out.println(" Please enter the student residence :"); String address = sc.nextLine();
// Create student object , Assign the data entered by the keyboard to the student object member variable Student s = new Student(); s.setSid(sid);
s.setName(name); s.setAge(age); s.setAddress(address); // Add student object to collection
array.add(s); // Prompt for adding successfully System.out.println(" Student added successfully "); } // Define a method , Judge whether the student ID is used
public static boolean isUsed(ArrayList<Student> array, String sid){
// If it is the same as a student ID in the set , return ture; If all are different , return false boolean flag = false; for (int i = 0; i
< array.size(); i++){ Student s = array.get(i); if (s.getSid().equals(sid)){
flag = true; break; } } return flag; } // Define a method , Used to view student information public static void
findAllStudent(ArrayList<Student> array) { // Judge whether there is data in the set , If no prompt is displayed if
(array.size() == 0) { System.out.println(" no message , Please add information before querying "); } else { // Display header information
//\t Actually tab Key position System.out.println(" Student ID \t\t\t\t full name \t\t Age \t\t Place of residence "); // Display the output of elements in the collection
for (int i = 0; i < array.size(); i++) { Student s = array.get(i);
System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + " year \t"
+ s.getAddress()); } } } // Define a method , Used to delete student information public static void
deleteStudent(ArrayList<Student> array) { // Key in the student ID to be deleted , Display prompt information Scanner sc =
new Scanner(System.in); System.out.println(" Please enter the student ID you want to delete :"); String sid =
sc.nextLine(); // Before deleting a student action , Judge whether the student ID exists // If not , Display prompt information // Traverse the collection to delete the corresponding student object from the collection int
index = -1; for (int i = 0; i < array.size(); i++) { Student s = array.get(i);
if (s.getSid().equals(sid)) { // array.remove(i); index = i; break; } } if
(index == -1){ System.out.println(" The information does not exist , Please re-enter "); } else{ array.remove(index);
// Prompt to delete student successfully System.out.println(" Delete student succeeded "); } } // Define a method , Used to modify student information public static
void updateStudent(ArrayList<Student> array) { // Key in the student ID to be modified , Display prompt information Scanner sc
= new Scanner(System.in); System.out.println(" Please enter the student ID you want to modify :"); String sid =
sc.nextLine(); int index = -1; // Traverse and modify the corresponding information of students for(int i = 0; i < array.size();
i++){ Student student = array.get(i); if (student.getSid().equals(sid)){ index
= i; // array.set(i,s); break; } } if (index == -1) {
System.out.println(" There is no student ID to modify , Please re-enter "); } else { // Key in the student information to be modified
System.out.println(" Please enter the student's new name :"); String name = sc.nextLine();
System.out.println(" Please enter the new age of the student :"); String age = sc.nextLine();
System.out.println(" Please enter the student's new residence :"); String address = sc.nextLine(); // Create a student object
Student s = new Student(); s.setSid(sid); s.setName(name); s.setAge(age);
s.setAddress(address); array.set(index, s); // Prompt for successful modification
System.out.println(" Student information modified successfully "); } } }
four , Main code analysis

1, Preparation of main interface

// Complete the cycle and return to the main interface again while (true) { // Writing the main interface with output statements
System.out.println("-------- Welcome to the student management system --------"); System.out.println("1 Add student ");
System.out.println("2 Delete student "); System.out.println("3 Modify student ");
System.out.println("4 View all students "); System.out.println("5 sign out ");
System.out.println(" Please enter your choice :"); // use Scanner Realize keyboard input data Scanner sc = new
Scanner(System.in); String line = sc.nextLine(); // use switch Statement completion operation selection switch
(line) { case "1": // System.out.println(" Add student "); addStudent(array); break;
case "2": // System.out.println(" Delete student "); deleteStudent(array); break; case "3":
// System.out.println(" Modify student "); updateStudent(array); break; case "4": //
System.out.println(" View all students "); findAllStudent(array); break; case "5":
System.out.println(" Thanks for using "); // break; System.exit(0); //JVM(java virtual machine ) sign out } } }
 2, Add student information ( Resolve duplicate additions )
public static void addStudent(ArrayList<Student> array) {
// Enter the data required by the student object with the keyboard , Display prompt information , Prompt for what information to enter Scanner sc = new Scanner(System.in);
// In order to make sid Can in while Accessed outside the loop , We defined him outside the loop String sid; while (true) {
System.out.println(" Please enter student ID :"); sid = sc.nextLine(); boolean flag =
isUsed(array, sid); if (flag) { System.out.println(" The student ID you entered has been used , Please re-enter "); } else
{ break; } } System.out.println(" Please enter student name :"); String name = sc.nextLine();
System.out.println(" Please enter student age :"); String age = sc.nextLine();
System.out.println(" Please enter the student residence :"); String address = sc.nextLine();
// Create student object , Assign the data entered by the keyboard to the student object member variable Student s = new Student(); s.setSid(sid);
s.setName(name); s.setAge(age); s.setAddress(address); // Add student object to collection
array.add(s); // Prompt for adding successfully System.out.println(" Student added successfully "); }
2.1, Define a method to determine whether the student ID is used
public static boolean isUsed(ArrayList<Student> array, String sid){
// If it is the same as a student ID in the set , return ture; If all are different , return false boolean flag = false; for (int i = 0; i
< array.size(); i++){ Student s = array.get(i); if (s.getSid().equals(sid)){
flag = true; break; } } return flag; }

3, Modify student information ( Solve the problem of modifying students that do not exist )
public static void updateStudent(ArrayList<Student> array) {
// Key in the student ID to be modified , Display prompt information Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the student ID you want to modify :"); String sid = sc.nextLine(); int index =
-1; // Traverse and modify the corresponding information of students for(int i = 0; i < array.size(); i++){ Student student =
array.get(i); if (student.getSid().equals(sid)){ index = i; // array.set(i,s);
break; } } if (index == -1) { System.out.println(" There is no student ID to modify , Please re-enter "); } else {
// Key in the student information to be modified System.out.println(" Please enter the student's new name :"); String name = sc.nextLine();
System.out.println(" Please enter the new age of the student :"); String age = sc.nextLine();
System.out.println(" Please enter the student's new residence :"); String address = sc.nextLine(); // Create a student object
Student s = new Student(); s.setSid(sid); s.setName(name); s.setAge(age);
s.setAddress(address); array.set(index, s); // Prompt for successful modification
System.out.println(" Student information modified successfully "); } }
4, Delete student information ( Solve the problem that deleting students does not exist )
public static void deleteStudent(ArrayList<Student> array) {
// Key in the student ID to be deleted , Display prompt information Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the student ID you want to delete :"); String sid = sc.nextLine();
// Before deleting a student action , Judge whether the student ID exists // If not , Display prompt information // Traverse the collection to delete the corresponding student object from the collection int index = -1; for
(int i = 0; i < array.size(); i++) { Student s = array.get(i); if
(s.getSid().equals(sid)) { // array.remove(i); index = i; break; } } if (index
== -1){ System.out.println(" The information does not exist , Please re-enter "); } else{ array.remove(index);
// Prompt to delete student successfully System.out.println(" Delete student succeeded "); } }
5, View all students
public static void findAllStudent(ArrayList<Student> array) {
// Judge whether there is data in the set , If no prompt is displayed if (array.size() == 0) {
System.out.println(" no message , Please add information before querying "); } else { // Display header information //\t Actually tab Key position
System.out.println(" Student ID \t\t\t\t full name \t\t Age \t\t Place of residence "); // Display the output of elements in the collection for (int i = 0;
i < array.size(); i++) { Student s = array.get(i);
System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + " year \t"
+ s.getAddress()); } } }

Technology
©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?