项目1

项目2

项目3

项目4

项目5

项目6

项目7

项目8

项目9

项目10

学生类的创建

/**
 * @document: 学生类,实体类用来封装学生信息
 * @Author:SmallG
 * @CreateTime:2023/8/1+18:30
 */

public class Student {
    private String name; //姓名
    private int age;     //年龄
    private String gender; //性别
    private String birth;//生日

    public Student() {
    }

    public Student(String name, int age, String gender, String birth) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.birth = birth;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }
}

StudentList类创建

/**
 * @Author:SmallG
 * @CreateTime:2023/8/1+18:34
 */

public class StudentList {
    //声明Student数组,用来保存学生对象
    private Student[] students = new Student[5];
    private int total = 0;
    private int totalStudent;

    public Student[] getStudents() {
        return students;
    }

    public void setStudents(Student[] students) {
        this.students = students;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    //初始化students数组


    public StudentList() {
    }

    public StudentList(int totalStudent) {
        this.totalStudent = totalStudent;
    }

    //添加学生方法
    public boolean addStudent(Student student) {

        //基于total个student.length判断数组是否已满
        if (total == students.length) {
            return false; //添加失败
        } else {
            //将student添加到数组中,并调整total的值
            students[total] = student;
            total++;
            return true; //添加成功
        }
    }

    //判断数组是否已满
    public boolean isFull() {
        if (total == students.length) {
            return true; //数组已满
        } else {
            return false; //数组未满
        }
    }

    //替换学生,用参数stud替换数组中由index指定的对象
    public boolean replaceStudent(int index, Student stud) {
        if (index >= students.length || index < 0) {
            return false; //替换失败
        } else {
            //students[index] = stud;
            return true; //替换成功
        }
    }

    //删除学生,从数组中删除参数为index指定索引位置的学生对象记录
    public boolean deleteStudent(int index) {
        if (index < 0 || index >= students.length) {
            return false; //索引无效,无法删除
        } else {
            for (int i =index;i<students.length-1;i++){
                students[i]=students[i+1];
            }
            total--;
            return true;
        }
    }

    //返回数组中记录的所有学生对象
    public void getAllStudent(){
        for (int i=0;i<total;i++){
            if (!students[i].getName().equals(null)){
                System.out.println((i+1)+"\t\t\t"+students[i].getName()+"\t\t"+students[i].getGender()+"\t\t\t"+students[i].getAge()+"\t\t\t"+students[i].getBirth());
            }
        }
    }

    //返回参数index指定索引位置的学生对象记录
    public Student getStudent(int index){
        if (index<0){
            return null;
        }else if(!students[index].equals("null")){

                return students[index];
        }else
        {
            return null;
        }
    }
}

StudentView类创建

/**
 * @document: 主模块,负责菜单的显示和处理用户操作
 * @Author:SmallG
 * @CreateTime:2023/8/1+19:08
 */

public class StudentView {
    //创建最大包含5个学生对象的StudentList对象,供以下各成员方法使用
    StudentList studentList = new StudentList(5);
    //用于读取用户键盘输入
    Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        StudentView studentView = new StudentView();
        studentView.enterMainMenu();
    }

    //显示主菜单,相应用户输入,根据用户操作分别调用其他相应成员的方法
    public void enterMainMenu() {
        boolean flag1 = true;
        while (flag1) {
            System.out.println("------------------学生信息管理软件------------------");
            System.out.println();
            System.out.println("                 1 添 加 学 生                   ");
            System.out.println("                 2 修 改 学 生                   ");
            System.out.println("                 3 删 除 学 生                   ");
            System.out.println("                 4 学 生 列 表                   ");
            System.out.println("                 5 退      出                   ");
            System.out.println();
            System.out.print("                 请选择(1-5):");
            int num = scanner.nextInt();
            switch (num) {
                case 1:
                    addNewStudent();
                    break;
                case 2:
                    replaceNewStudent();
                    break;
                case 3:
                    deleteStudent();
                    break;
                case 4:
                    getStudent();
                    break;
                case 5:
                    System.out.println("退出成功!");
                    flag1 = false;
                    break;
                default:
                    System.out.println("序号输入有误!");
                    break;
            }
        }

    }

    //查看学生列表
    public void getStudent() {
        System.out.println("---------------------学生列表---------------------");
        System.out.println("编号\t\t姓名\t\t性别\t\t年龄\t\t生日\t\t");
        studentList.getAllStudent();
        System.out.println("-------------------学生列表完成-------------------");
    }

    //删除学生
    public void deleteStudent() {
        System.out.println("---------------------删除学生---------------------");
        System.out.println("请选择待删除学生编号(-1退出):");
        int num = scanner.nextInt();
        if (num == -1) {
            System.out.println("退出成功!");
        } else {
            System.out.println("确认是否删除(Y/N)");
            String s = scanner.next();
            if ((s.equals("y") || s.equals("Y")) && studentList.deleteStudent(num - 1)) {
                System.out.println("---------------------删除完成---------------------");
            } else if (s.equals("n") || s.equals("N")) {
                System.out.println("---------------------删除取消---------------------");
            } else {
                System.out.println("输入有误!");
            }
        }

    }

    //修改学生
    public void replaceNewStudent() {
        System.out.println("---------------------修改学生---------------------");
        System.out.println("请选择待修改学生编号(-1退出):");
        int num = scanner.nextInt();
        if (num > 0) {
            System.out.println("姓名(" + studentList.getStudent(num - 1).getName() + "):");
            String name = scanner.next();
            if (!name.equals("-2")) {
                studentList.getStudent(num - 1).setName(name);
            }

            System.out.println("性别(" + studentList.getStudent(num - 1).getGender() + "):");
            String gender = scanner.next();
            if (!gender.equals("-2")) {
                studentList.getStudent(num - 1).setGender(gender);
            }

            System.out.println("年龄(" + studentList.getStudent(num - 1).getAge() + "):");
            int age = scanner.nextInt();
            if (age != -2) {
                studentList.getStudent(num - 1).setAge(age);
            }

            System.out.println("生日(" + studentList.getStudent(num - 1).getBirth() + "):");
            String birth = scanner.next();
            if (!birth.equals("-2")) {
                studentList.getStudent(num - 1).setGender(birth);
            }

        } else {
            System.out.println("退出成功!");
        }

        boolean flag = studentList.replaceStudent(num - 1, studentList.getStudent(num - 1));
        if (flag) {
            System.out.println("---------------------修改完成---------------------");
        } else {
            System.out.println("---------------------修改失败---------------------");
        }
    }

    //添加学生
    public void addNewStudent() {
        System.out.println("---------------------添加学生---------------------");
        //判断是否已满
        if (studentList.isFull()) {
            System.out.println("-----------------记录已满,无法添加-----------------");
        } else {
            //未满
            System.out.print("姓名:");
            String studentName = scanner.next();
            System.out.print("性别:");
            String studentGender = scanner.next();
            System.out.print("年龄:");
            int studentAge = scanner.nextInt();
            System.out.print("生日:");
            String studentBirth = scanner.next();

            //创建Student对象stud,封装输入的学生信息
            Student stud = new Student(studentName, studentAge, studentGender, studentBirth);
            boolean flag = studentList.addStudent(stud);
            if (flag) { //添加成功
                System.out.println("---------------------添加成功---------------------");
            } else {
                System.out.println("-----------------记录已满,无法添加-----------------");
            }
        }
    }
}
最后修改:2023 年 08 月 03 日
如果觉得我的文章对你有用,请随意赞赏