Java学习之~ArrayList实现学生管理系统

公子初心
2023-09-30 / 0 评论 / 46 阅读 / 正在检测是否收录...

1.Student类文件 Student.java

package cc.itnan.study;

public class Student {
    private String stuNum;
    private String stuName;
    private int age;
    private String brithDay;


    public Student() {
    }

    public Student(String stuNum, String stuName, int age, String brithDay) {
        this.stuNum = stuNum;
        this.stuName = stuName;
        this.age = age;
        this.brithDay = brithDay;
    }

    /**
     * 获取
     * @return stuNum
     */
    public String getStuNum() {
        return stuNum;
    }

    /**
     * 设置
     * @param stuNum
     */
    public void setStuNum(String stuNum) {
        this.stuNum = stuNum;
    }

    /**
     * 获取
     * @return stuName
     */
    public String getStuName() {
        return stuName;
    }

    /**
     * 设置
     * @param stuName
     */
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return brithDay
     */
    public String getBrithDay() {
        return brithDay;
    }

    /**
     * 设置
     * @param brithDay
     */
    public void setBrithDay(String brithDay) {
        this.brithDay = brithDay;
    }


}

2.StudentTest类文件 StudentTest.java

package cc.itnan.study;

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

public class StudentTest {

    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
//        list.add(new Student("1001","小明",18,"2000"));
//        list.add(new Student("1002","大明",19,"2001"));
//        list.add(new Student("1003","老明",20,"2002"));
        while (true) {
            System.out.println("---------------------欢迎来到学生管理系统---------------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            Scanner sc = new Scanner(System.in);
            int chioce = sc.nextInt();
            switch (chioce){
                case 1:
                    System.out.println("添加学生");
                    addStudent(list);
                    break;
                case 2:
                    System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case 3:
                    System.out.println("修改学生");
                    updateStudent(list);
                    break;
                case 4:
                    System.out.println("查看学生");
                    queryStudentInfo(list);
                    break;
                case 5:
                    System.out.println("再见!");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误,请检查!");

            }
        }
    }

    private static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String stuNum;
        while (true){
            System.out.println("请输入学生学号:");
            stuNum = sc.next();
            if(getIndexById(stuNum,list) == -1){
                break;
            }
        }

        System.out.println("请输入学生姓名:");
        String stuName = sc.next();

        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();

        System.out.println("请输入学生生日:");
        String birthDay = sc.next();

        list.add(new Student(stuNum,stuName,age,birthDay));
        System.out.println("添加成功!");
    }

    private static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的学生id:");
        String stuNum = sc.next();
        int indexById = getIndexById(stuNum, list);
        System.out.println(indexById);
        if(indexById>=0){
            // 修改
            System.out.println("请输入学生姓名:");
            String stuName = sc.next();

            System.out.println("请输入学生年龄:");
            int age = sc.nextInt();

            System.out.println("请输入学生生日:");
            String birthDay = sc.next();

            list.set(indexById,new Student(stuNum,stuName,age,birthDay));
            System.out.println("修改成功");
        }else{
            // 查无此人
            System.out.println("查无此人,无法修改");
        }

    }

    private static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除到底学生学号:");
        String stuNum = sc.next();
        int indexById = getIndexById(stuNum, list);
        System.out.println(indexById);
        if(indexById>=0){
            // 删除
            list.remove(indexById);
            System.out.println("删除成功");
        }else{
            // 查无此人
            System.out.println("查无此人,无法删除");
        }


    }

    private static int getIndexById(String id, ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(id.equals(stu.getStuNum())){
                return i;
            }

        }
        return -1;
    }


    private static void queryStudentInfo(ArrayList<Student> list) {
        if(list.size()==0){
            System.out.println("没有学生信息,请添加!");
        }else{
            System.out.println("学号:\t姓名\t年龄\t出生日期\t");
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                System.out.println(stu.getStuNum()+"\t"+stu.getStuName()+"\t"+stu.getAge()+"\t"+stu.getBrithDay());
            }
        }
    }
}
0

评论 (0)

取消