Simulation Implementation of student achievement management system (java basic practice)

✅ About the author: Hello, I'm little fish. You can call me fish

📒 Blog home page: It's a little fish

🔥 Series column: JavaSE learning notes

🌻 Although we are becoming more and more realistic, please don't forget our original dream

💖 Bloggers are also in the learning stage. Please let us know if you find any problems. Thank you very much 💖

catalogue

1, Effect display

II. Code implementation

🌻 Main function (program entry)

🌻 Student information storage bag

🌻 Student object operation package

We have talked about many details of the system in our previous blog, so we won't go into details here

Portal: Implementation of library management system (super detailed)

Let's see the effect of this system!

1, Effect display

  🌰 Add student information

  🌰 Search for student information by name

  🌰 Modify the information of a student by student number

  🌰 Delete all information of a student by student number

🌰 Sort students by total score

🌰 Output failed Student object information

  🌰 Exit the system

II. Code implementation

🌻 Main function (program entry)

import Operation.*;
import Operation.studentChange;
import student.StudentList;

import java.util.Scanner;
import Operation.ioOPerate;
public class Main {
    public static void main(String[] args) {
        System.out.println("Welcome to the student achievement management system");
        System.out.println("========================");
        System.out.println("0.Add student information");
        System.out.println("1.Print all student information");
        System.out.println("2.Find information about the student by name");
        System.out.println("3.Modify the score information of a student according to the student number");
        System.out.println("4.Delete all information of a student by student number");
        System.out.println("5.In ascending order according to the total score of students");
        System.out.println("6.Output failed student information by subject");
        System.out.println("7.Exit the system");
        System.out.println("=========================");
        StudentList studentList = new StudentList();
        // This is equivalent to that the interface reference refers to different subclass objects, and the upward transformation has taken place
        ioOPerate[] ioOperation = new ioOPerate[] {  // Assign values to the interface array and point to different instantiated operation objects respectively
                new AddStudent(),                   // In this way, we can perform different operations on the student object according to the subscript
                new printAll(),
                new studentFind(),
                new studentChange(),
                new studentDelete(),
                new studentSort(),
                new flunkPrint(),
                new exitOperation(),
        };
        while (true) {
            System.out.print("Please enter your action:");
            Scanner sc  = new Scanner(System.in);
            int choice = sc.nextInt();
            // It embodies polymorphism
            ioOperation[choice].work(studentList);  // work is a method overridden by subclasses. It will perform different operations according to different references of the current interface object ioOperation
            // The interface is equal to the parent class, and the subclass implementing the interface is equivalent to the subclass inheriting the parent class
            // Conditions of polymorphism: 1 There is inheritance (Implementation) relationship 2 Parent class reference (interface reference) refers to subclass object (upward transformation) 3 The subclass object overrides the parent class (the method in the interface)
        }
    }
}

🌻 Student information storage bag

This package has two classes, one is a student class that contains all the attributes of a single student, and the other is a collection class of student objects

Student class

/**
 * Student class, which contains all the attributes of a student
 */
package student;
// Because you want to compare the reference variables of Student type, you need to implement the Comparable interface
public class Student implements Comparable<Student> {
    private long studentId;
    private String name;
    private double cGrade;
    private double dataStructureGrade;
    // The construction method initializes each attribute of the student
    public Student(long studentId, String name, double cGrade, double dataStructureGrade) {
        this.studentId = studentId;
        this.name = name;
        this.cGrade = cGrade;
        this.dataStructureGrade = dataStructureGrade;
    }
    // Override the toString method of the parent class Object so that you can use arrays ToString (students) can directly print out the values of each reference Object in the array
    @Override
    public String toString() {
        return "Student{" +
                "studentId=" + studentId +
                ", name='" + name + '\'' +
                ", cGrade=" + cGrade +
                ", dataStructureGrade=" + dataStructureGrade +
                '}';
    }
    // Rewrite the compareTo method in the Comparable interface and compare it according to the total score
    @Override
    public int compareTo(Student o) {
        return (int)((this.cGrade + this.dataStructureGrade) - (o.cGrade + o.dataStructureGrade));
    }

    // Modify the information of the designated student
    public void studentChange(long studentId, String name, double cGrade, double dataStructureGrade) {
        System.out.println("Will students" + this.name + "Change the name of to:" + name);
        this.name = name;
        System.out.println("Student number from" + this.studentId + "Amend to read:" + studentId);
        this.studentId = studentId;
        System.out.println("c Language achievement from" + this.cGrade + "Amend to read:" + cGrade);
        this.cGrade = cGrade;
        System.out.println("Data structure score from" + this.dataStructureGrade + "Amend to read:" + dataStructureGrade);
        this.dataStructureGrade = dataStructureGrade;
        System.out.println("Congratulations, modification succeeded!");
    }

    public long getStudentId() {
        return studentId;
    }

    public void setStudentId(long studentId) {
        this.studentId = studentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getcGrade() {
        return cGrade;
    }

    public void setcGrade(double cGrade) {
        this.cGrade = cGrade;
    }

    public double getDataStructureGrade() {
        return dataStructureGrade;
    }

    public void setDataStructureGrade(double dataStructureGrade) {
        this.dataStructureGrade = dataStructureGrade;
    }
}



StudentList class

/**
 * It stores the information of a group of students and is a collection of student objects. Our operations on the student system mainly focus on this StudentList class
 */
package student;
// Conduct package guiding operation
import java.util.Scanner;

public class StudentList {
    private int usedSize;

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    public Student[] getStudents() {
        return students;
    }
    // Print the student object with the specified subscript
    public void printStudent(int pos) {
        System.out.println(students[pos]);
    }
    // Gets the book with the specified subscript
    public Student getStudent(int pos) {
        return students[pos];
    }
    // Place the book in the specified position of the array
    public void setStudent(int pos, Student student) {
        students[pos] = student;
    }

    // Initialize student array
    private Student[] students = new Student[10];
    public StudentList() {
        students[0] = new Student(12345, "Zhang San", 76, 92);
        students[1] = new Student(12346, "Li Si", 86, 67);
        students[2] = new Student(12347, "Wang Wu", 64, 99);
        usedSize = 3;
    }

    // Delete an element in the array
    public void delete() {
        // Delete student information
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the student number of the student you want to delete:");
        long id = sc.nextLong();
        if (students[usedSize - 1].getStudentId() == id) {
            --usedSize;  // Special treatment is required when the value at the end of the array is deleted
            System.out.println("Congratulations, deleted successfully!!!");
            return;
        }
        for (int i = 0; i < usedSize; i++) {
            if (students[i].getStudentId() == id) {
                for (int j = i + 1; j < usedSize; j++) {
                    students[j - 1] = students[j];
                }
                --usedSize;
                System.out.println("Congratulations, deleted successfully!!!");
                return;
            }
        }
        System.out.println("Failed to delete, the student was not found!!!");
    }

}

🌻 Student object operation package


This package is mainly for some specific operations

ioOperation interface

package Operation;

import student.Student;
import student.StudentList;
// This is an interface, in which the work abstract method is rewritten by different subclasses that implement the interface to realize polymorphism
// The interface is equal to the parent class, and the subclass implementing the interface is equivalent to the subclass inheriting the parent class
// Conditions of polymorphism: 1 There is inheritance (Implementation) relationship 2 Parent class reference (interface reference) refers to subclass object (upward transformation) 3 The subclass object overrides the parent class (the method in the interface)
public abstract interface ioOPerate {
    public abstract void work(StudentList studentList);
}

Add student information

package Operation;

import student.Student;
import student.StudentList;

import java.util.Scanner;

public class AddStudent implements ioOPerate{ // Implement ioOperation interface
    @Override // Rewrite the abstract method work in the interface
    public void work(StudentList studentList) {
        if (studentList.getUsedSize() > 10) {
            System.out.println("Failed to add. No more students can be added in the system. It needs to be expanded!!!");
            return;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the student number of the student you want to add");
        long studentId = sc.nextLong();
        System.out.println("Please enter the name of the student you want to add");
        String name = sc.next();
        System.out.println("Please enter the number of students you want to add c language achievement");
        double cGrade = sc.nextDouble();
        System.out.println("Please enter the grade of the data structure of the student you want to add");
        double dataStructureGrade = sc.nextDouble();

        Student student = new Student(studentId, name, cGrade, dataStructureGrade);
        studentList.setStudent(studentList.getUsedSize(), student); // Add a book at the end of the array by default
        studentList.setUsedSize(studentList.getUsedSize() + 1);
        System.out.println("Congratulations, added successfully!!!");
    }
}

Display information for all students

package Operation;

import student.Student;
import student.StudentList;

import java.util.Arrays;

// Print out the information of all students
public class printAll implements ioOPerate{

    @Override
    public void work(StudentList studentList) {
        // Print all student information
        System.out.println("The student information at this time is:");
        for (int i = 0; i < studentList.getUsedSize(); i++) {
            studentList.printStudent(i);
        }
    }
}

Find information about the specified student by name

package Operation;

import student.Student;
import student.StudentList;

import java.util.Scanner;

public class studentFind implements ioOPerate{
    @Override
    public void work(StudentList studentList) {
        // Print the information of the designated student

        // Print by student name
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the name of the student you want to find:");
        String nameFind = sc.nextLine();

        for (int i = 0; i < studentList.getUsedSize(); i++) {
            if (studentList.getStudents()[i].getName().equals(nameFind)) {
                System.out.println("Student number:" + studentList.getStudents()[i].getStudentId() + "  full name:" + studentList.getStudents()[i].getName());
                System.out.println("c Language achievement:" + studentList.getStudents()[i].getcGrade() + "  Data structure score:" + studentList.getStudents()[i].getDataStructureGrade());
                return;
            }
        }
        System.out.println("The student was not found!!!");
    }
}

 

Modify the specified student information by student number

package Operation;

import student.Student;
import student.StudentList;

import java.util.Scanner;

// Modify student information
public class studentChange implements ioOPerate{
    @Override
    public void work(StudentList studentList) {
        // Modify student information by student number
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the student number of the student you want to modify:");
        long id = sc.nextLong();
        for (int i = 0; i < studentList.getUsedSize(); i++) {
            if (studentList.getStudents()[i].getStudentId() == id) {
                System.out.println("Please enter the modified Name:");
                String name = sc.next();
                System.out.println("Please enter the modified student number:");
                long studentId = sc.nextLong();
                System.out.println("Please enter the modified c Language score:");
                double cGrade = sc.nextDouble();
                System.out.println("Please enter the score of the modified data structure:");
                double dateStructureGrade = sc.nextDouble();
                studentList.getStudents()[i].studentChange(studentId, name, cGrade, dateStructureGrade);
                return;
            }
        }
        System.out.println("Modification failed, there is no student in the system!!!");
    }
}

Delete the information of designated students by student number

package Operation;

import student.Student;
import student.StudentList;

import java.util.Scanner;

public class studentDelete implements ioOPerate{
    @Override
    public void work(StudentList studentList) {
        // Delete student information
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the student number of the student you want to delete:");
        long id = sc.nextLong();
        if (studentList.getStudents()[studentList.getUsedSize() - 1].getStudentId() == id) {
            studentList.setStudent(studentList.getUsedSize(), null);  // Special treatment is required when the value at the end of the array is deleted
            studentList.setUsedSize(studentList.getUsedSize() - 1);
            System.out.println("Congratulations, deleted successfully!!!");
            return;
        }
        for (int i = 0; i < studentList.getUsedSize(); i++) {
            if (studentList.getStudents()[i].getStudentId() == id) {
                for (int j = i + 1; j < studentList.getUsedSize(); j++) {
                    Student student = studentList.getStudent(j);
                    studentList.setStudent(j - 1, student);
                }
                studentList.setUsedSize(studentList.getUsedSize() - 1);
                System.out.println("Congratulations, deleted successfully!!!");
                return;
            }
        }
        System.out.println("Failed to delete, the student was not found!!!");
    }
}

Sort students in ascending order according to their total scores

package Operation;

import student.Student;
import student.StudentList;

import java.util.Arrays;

public class studentSort implements ioOPerate{
    @Override
    public void work(StudentList studentList) {
        System.out.println("Sorting students by total score~~~");
        Arrays.sort(studentList.getStudents(), 0, studentList.getUsedSize());
        System.out.println("Congratulations, sorting succeeded!!!");
    }
}

Output failed student information

package Operation;

import student.Student;
import student.StudentList;

import java.util.Arrays;

// Printing of failed students
public class flunkPrint implements ioOPerate{
    @Override
    public void work(StudentList studentList) {
        for (int i = 0; i < studentList.getUsedSize(); i++) {
            if (studentList.getStudents()[i].getcGrade() < 60 || studentList.getStudents()[i].getDataStructureGrade() < 60) {
                studentList.printStudent(i);
            }
        }
    }
}

Exit the system

package Operation;

import student.Student;
import student.StudentList;

public class exitOperation implements ioOPerate{
    @Override
    public void work(StudentList studentList) {
        System.out.println("Exiting system");
        for (int i = 0; i < studentList.getUsedSize(); i++) {
            studentList.setStudent(i, null);
        }
        System.out.println("Exit succeeded!");
        System.exit(0);
    }
}

gitee code address: Student management system practicehttps://gitee.com/shen-xiao-yu/java104/tree/master/20220503/src Closing remarks:

In practice, I found that I only understood a lot of knowledge superficially, not really.

We must practice more and try to use our knowledge!!!

 

Tags: Java JavaSE

Posted by sdi126 on Wed, 04 May 2022 12:31:09 +0300