使用ArrayList实现简易学生管理系统

1 功能描述:
实现学生信息的增删改查
学生信息项(学号sid,姓名sname,年龄age)

2 代码实现:
Student类


public class Student {
    private String sid;
    private String sname;
    private String sage;

    public Student() {
    }

    public Student(String sid, String sname, String sage) {
        this.sid = sid;
        this.sname = sname;
        this.sage = sage;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSage() {
        return sage;
    }

    public void setSage(String sage) {
        this.sage = sage;
    }
}

StudentManager类


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

public class StudentManager {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<Student>();
        while (true) {
            System.out.println("---Welcome to the Student Manage System---");
            System.out.println("1 Add Student Information");
            System.out.println("2 Delete Student Information");
            System.out.println("3 Update Student Information");
            System.out.println("4 Search Student Information");
            System.out.println("5 Display All Student Information");
            System.out.println("6 Exit");
            System.out.println("Please enter your choice:");

            Scanner sc = new Scanner(System.in);
            String choice = sc.nextLine();

            switch (choice) {
                case "1":
                    addStudent(students);
                    break;
                case "2":
                    deleteStudent(students);
                    break;
                case "3":
                    updateStudent(students);
                    break;
                case "4":
                    searchStudent(students);
                    break;
                case "5":
                    displayAllStudent(students);
                    break;
                case "6":
                    System.out.println("Goodbye");
                    System.exit(0);
                default:
                    System.out.println("Invalid choice");
                    break;
            }
        }
    }

    public static boolean isStudentExists(ArrayList<Student> arrayList, String sid) {
        int index = -1;
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            if (student.getSid().equals(sid)) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            return false;
        }
        return true;
    }

    public static void addStudent(ArrayList<Student> arrayList) {
        Scanner sc = new Scanner(System.in);
        String studentID = new String();
        boolean flag = true;
        while (flag) {
            System.out.println("Enter Student ID: ");
            studentID = sc.nextLine();

            flag = isStudentExists(arrayList, studentID);
            if (flag) {
                System.out.println("Student ID already exists");
            }
        }
        System.out.println("Enter Student Name: ");
        String studentName = sc.nextLine();
        System.out.println("Enter Student Age: ");
        String studentAge = sc.nextLine();

        Student student = new Student(studentID, studentName, studentAge);
        arrayList.add(student);

        System.out.println("Added Student Information Successfully");
    }

    public static void deleteStudent(ArrayList<Student> arrayList) {
        int index = -1;
        while (index < 0) {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter Student ID: ");
            String studentID = sc.nextLine();
            for (int i = 0; i < arrayList.size(); i++) {
                Student student = arrayList.get(i);
                if (student.getSid().equals(studentID)) {
                    arrayList.remove(i);
                    index = i;
                    System.out.println("Deleted Student Information Successfully");
                    break;
                }
            }
            if (index == -1) {
                System.out.println("Student ID does not exist");
            }
        }
    }

    public static void updateStudent(ArrayList<Student> arrayList) {
        int index = -1;
        while (index < 0) {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter Student ID: ");
            String studentID = sc.nextLine();

            for (int i = 0; i < arrayList.size(); i++) {
                Student student = arrayList.get(i);
                if (student.getSid().equals(studentID)) {
                    System.out.println("Enter Student Name: ");
                    String studentName = sc.nextLine();
                    System.out.println("Enter Student Age: ");
                    String studentAge = sc.nextLine();
                    Student newStudent = new Student(studentID, studentName, studentAge);
                    arrayList.set(i, newStudent);
                    index = i;
                    System.out.println("Updated Student Information Successfully");
                    break;
                }
            }
            if (index == -1) {
                System.out.println("Student ID does not exist");
            }
        }
    }

    public static void searchStudent(ArrayList<Student> arrayList) {
        int index = -1;
        while (index < 0) {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter Student ID: ");
            String studentID = sc.nextLine();
            for (int i = 0; i < arrayList.size(); i++) {
                Student student = arrayList.get(i);

                if (student.getSid().equals(studentID)) {
                    System.out.println("StudentID\tStudentName\tStudentAge");
                    System.out.println(student.getSid() + "\t" + student.getSname() + "\t" + student.getSage());
                    index = i;
                    break;
                }
            }
            if (index == -1) {
                System.out.println("Student ID does not exist");
            }
        }
    }

    public static void displayAllStudent(ArrayList<Student> arrayList) {
        System.out.println("StudentID\tStudentName\tStudentAge");
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            System.out.println(student.getSid() + "\t" + student.getSname() + "\t" + student.getSage());
        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容