Java implements a simple student information management system, using List set class
source code
Student object class
package JuyThirteenthJStuMangeSys;
/**
* @date 2018/7/13
* @author HCQ
*@ see student information management system
*/
//Implement the compatible interface, rewrite the CompareTo method, which is used for "1. Query all student information" in the function menu, and sort according to the student number
public class StudentH implements Comparable{
//Properties and construction methods can be changed according to the needs of the topic
private int id; / / student ID
private String name; / / name
private String classNum; / / class
public StudentH() {
} public StudentH(int id, String name, String classNum) { this.id = id; this.name = name; this.classNum = classNum; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getClassNum() { return classNum; } public void setClassNum(String classNum) { this.classNum = classNum; } @Override public String toString() { //Rewrite toString method to print out return "Student"+id+" [Student ID=" + id + ", Full name=" + name + ", class=" + classNum + "]"; } @Override public int compareTo(StudentH o) { //Rewrite the compareTo method to sort by student number // TODO Auto-generated method stub return this.id - o.getId(); }
}
Student information management system
package JuyThirteenthJStuMangeSys;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/**
* @date 2018/7/13
* @author HCQ
*@ see student information management system
*/
public class StudentManageSystem {
private static Scanner sc;
public static void main(String[] args) { //Simple welcome interface System.out.println("---------------------------------"); System.out.println("\t" + "Welcome to the student management system "); System.out.println("---------------------------------"); List<StudentH> list = new LinkedList<StudentH>(); sc = new Scanner(System.in); int id = 0; try { while (true) { //In the function menu interface, each time a function is used, the function menu will be printed once to facilitate the user to query the corresponding numbers of the function System.out.println("Function menu:"); System.out.println("----------------------------------"); System.out.println("1,Query all student information"); System.out.println("2,Query information according to student ID"); System.out.println("3,Add student information"); System.out.println("4,Delete student information"); System.out.println("5,Modify student information"); System.out.println("6,Exit system"); System.out.println("(Tip: for your convenience, please maximize the console before using the system.)"); System.out.println("----------------------------------"); System.out.print("Please input the corresponding number for operation:"); int input = sc.nextInt(); //Input digital selection function if(input>0 && input<7) { //When the input number is 1-6, the function can be executed, otherwise the system exits System.out.println("---------------------------------"); // 3. Add student information if (input == 3) { System.out.print("Please enter several student information you want to add:"); int num = sc.nextInt(); System.out.println("----------------------------------"); for (int w = 0; w < num; w++) { System.out.println("Please add student information"); System.out.println("----------------------------------"); id++; System.out.print("Please enter the student's name:"); String name = sc.next(); System.out.print("Please enter the class:"); String classNum = sc.next(); list.add(new StudentH(id, name, classNum)); System.out.println("Successfully added."); System.out.println("---------------------------------"); } } // 1. Query all student information if (input == 1) { if (list.isEmpty() == true) { //Judge whether there is information of Student object in the set System.out.println("You have not added student information."); } else { System.out.println("Student information form"); System.out.println("---------------------------------"); for (int j = 0; j < list.size(); j++) { //This method can be called only when the CompareTo method is rewritten and the Comparable interface is implemented Collections.sort(list); //To compare and sort, you must define another custom comparison class. Here, the class name of the comparison class is MyComparator
// Collections.sort(list, new MyComparator());
System.out.println(list.get(j).toString());
}
}
System.out.println("-----—");
}
// 2. Query information according to student ID if (input == 2) { if (list.isEmpty() == true) { System.out.println("You have not added student information."); } else { System.out.print("Please enter the student ID you want to find:"); int index = sc.nextInt(); List<Integer> listID = new ArrayList<Integer>(); for (int c = 0; c < list.size(); c++) { listID.add(list.get(c).getId()); } for (int q = 0; q < list.size(); q++) { if (listID.contains(index)) { if (index == list.get(q).getId()) { System.out.println(list.get(q).toString()); break; } else { continue; } } else { System.out.println("No information about the student."); break; } } } System.out.println("---------------------------------"); } // 4. Delete student information if (input == 4) { if (list.isEmpty() == true) { System.out.println("You have not added student information."); } else { System.out.print("Please enter the student ID you want to delete:"); int index = sc.nextInt(); List<Integer> listID = new ArrayList<Integer>(); for (int c = 0; c < list.size(); c++) { listID.add(list.get(c).getId()); } for (int q = 0; q < list.size(); q++) { if (listID.contains(index)) { if (index == list.get(q).getId()) { list.remove(q); System.out.println("Delete succeeded."); break; } else { continue; } } else { System.out.println("No information about the student."); break; } } } System.out.println("---------------------------------"); } // 5. Modify student information if (input == 5) { if (list.isEmpty() == true) { System.out.println("You have not added student information."); } else { System.out.print("Please enter the student ID you want to modify:"); int index = sc.nextInt(); List<Integer> listID = new ArrayList<Integer>(); for (int c = 0; c < list.size(); c++) { listID.add(list.get(c).getId()); } for (int q = 0; q < list.size(); q++) { if (listID.contains(index)) { if (index == list.get(q).getId()) { System.out.print("Original information of this student:"); //Print the original information of the student information to be modified System.out.println(list.get(q).toString()); System.out.println("Please modify this student information:"); System.out.println("----------------------------------"); System.out.print("Please re-enter Student Name:"); String name = sc.next(); System.out.print("Please re-enter the student class:"); String classNum = sc.next(); list.remove(q); //Delete all information of the student first id = index; //Add the student information again list.add(new StudentH(id, name, classNum)); System.out.println("Modification succeeded."); break; } else { continue; } } else { System.out.println("No information about the student."); break; } } } System.out.println("----------------------------------"); } // 6. Exit the system if (input == 6) { System.out.println("The system has exited. Welcome to use next time..."); System.exit(0); } } else { System.out.println("----------------------------------"); System.out.println("Input error, system exit."); System.exit(0); } } //When inputting, if the input is not a number, it will catch the inputmistakexception exception and prompt "input error, system exit" } catch(InputMismatchException e) { System.out.println("----------------------------------"); System.out.println("Input error, system exit."); } catch(Exception e) { System.out.println("----------------------------------"); System.out.println("Input error, system exit."); } finally { System.exit(0); //Exit system } }
}
Custom comparison class, MyComparator
package JuyThirteenthJStuMangeSys;
import java.util.Comparator;
/**
* @date 2018/7/13
* @author HCQ
*@ see student information management system
*/
//If you want to compare and sort, you need to customize a comparison class. See the specific code of "1. Query all student information" in the class of student information management system
public class MyComparator implements Comparator{
public int compare(StudentH o1, StudentH o2) {
return o1.getId() - o2.getId();
}
}
The code is for reference only. If you have any questions, please leave a message. Thank you.
2018/8/5