当前位置:网站首页>Student management system

Student management system

2022-06-13 03:48:00 iFulling

use Java Do a student management system

package Study;

import jdk.swing.interop.SwingInterOpUtils;

import java.util.ArrayList;
import java.util.Scanner;

/*  Student management system  */
public class StudentManager {
    
    /* 1: Write the main interface with output statements  2: use Scanner Realize keyboard input data  3: use switch Statement to complete the selection of operations  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) {
    
            // Write 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 students ");
            System.out.println("3  Revise students ");
            System.out.println("4  Check out all the 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 to complete the selection of operations 
            switch (line) {
    
                case "1":
// System.out.println(" Add student ");
                    addStudent(array);
                    break;
                case "2":
// System.out.println(" Delete students ");
                    deleteStudent(array);
                    break;
                case "3":
// System.out.println(" Revise students ");
                    updateStudent(array);
                    break;
                case "4":
// System.out.println(" Check out all the students ");
                    findAllStudent(array);
                    break;
                case "5":
                    System.out.println(" Thank you 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 message , Prompt what information to enter 
        Scanner sc = new Scanner(System.in);

        // In order to make sid Can be in while Outside the loop is accessed , We define it outside the loop 
        String sid;

        while (true) {
    
        System.out.println(" Please enter the student id number :");
            sid = sc.nextLine();

            boolean flag = isUsed(array, sid);
            if (flag) {
    
                System.out.println(" The student number you entered has been used , Please re-enter ");
            } else {
    
                break;
            }
        }

        System.out.println(" Please enter the student's name :");
        String name = sc.nextLine();
        System.out.println(" Please enter the age of the student :");
        String age = sc.nextLine();
        System.out.println(" Please enter the student's residence :");
        String address = sc.nextLine();

        // Create student objects , 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 objects to the collection 
        array.add(s);

        // Give a prompt for adding successfully 
        System.out.println(" Add student successfully ");
    }

    // Define a method , Judge whether the student number is used 
    public static boolean isUsed(ArrayList<Student> array, String sid){
    
        // If it is the same as a student number in the collection , return ture; If it's not the same , 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) {
    
        // Determine if there is data in the set , If no prompt is displayed 
        if (array.size() == 0) {
    
            System.out.println(" No information , Please add information before querying ");
        } else {
    
            // Display header information 
            //\t  In fact, that is tab Key position 
            System.out.println(" Student number \t\t\t\t full name \t\t Age \t\t To live ");

            // Display the output of the 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) {
    
        // Enter the student number to be deleted with the keyboard , Display prompt message 
        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 number exists 
        // If it doesn't exist , Display prompt message 
        // Traverse the set and delete the corresponding student object from the set 
        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);
            // Give a prompt to delete the student successfully 
            System.out.println(" Delete student success ");
        }
    }

    // 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 message 
        Scanner sc = new Scanner(System.in);

        System.out.println(" Please enter the student number 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 be modified , Please re-enter ");
        } else {
    
            // Enter the student information to be modified with the keyboard 
            System.out.println(" Please enter the student's new name :");
            String name = sc.nextLine();
            System.out.println(" Please enter the student's new age :");
            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);
            // Give a prompt of successful modification 
            System.out.println(" Modify student information successfully ");
        }
    }
}

package Study;
/*  Students  Alt + Insert  Choose according to your own needs  */
public class Student {
    
    // Student number 
    private String sid;
    // full name 
    private String name;
    // Age 
    private String age;
    // To live 
    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;
    }
}

原网站

版权声明
本文为[iFulling]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130336107340.html