如何用java实现幼儿园管理系统的名额问题?

实现 Schoolsystem类

  1. Schoolsystem( Integer big, Integer medium, Integer small)l初始化Schoolsystem类,三个参数分别对应每种班级的剩余名额。
    (2) bool addstudert( Integer stu ype)检查是否有 stutype对应的班级。 stutypet有三种类型:大,中,小,分别用数字1,2和3表示。一个学生只能在 stutype对应的班级中。如果没有剩余多的班级人数,请返回 false,否则将该学生录入班级井返回true
    (3) Stringl parse( String input)解析输入字符串(格式见示列)返回字符串数组
    (4) void printi()输出结果(格式见示例)
    r'scholsystem, addstuden "adcstudert" addstudent addstudent']
    [1,1,0],[1],[2],[3],[1]
    输出
    [null true, true, false, false]
    解释
    School System school System= rew Schoolsystem(1,1,0);/初始化有1个大班名额,1个中班名额,0个小班名额
    schoolsystem. addstudent(1)∥返回true,因为有1个剩余的大班名额
    school System. addstudent(2);/返回tue,因为有1个剩余的中班名额
    schodsyster. addstudent(3)返回 false,因为没有剩余的小班名额
    schod System. addstudent(1)返回 false,因为没有剩余的大班名额,唯一1个大班名额经被占据了
    需要实现的接口及代码主体参考
    /报名入学接口
    interface Isignupf
    打印輸出结果
    public vold printo;
    ∥检查是否有 stuType对应的班级名额
    ∥如果没有剩余名额,请返回 false,否则学生将报名进入该班级并返回true
    public boolean adcstudert (int stutype),
    解析命令行输入的参数(格式),如文摧述
    public static Iparams parse throws Exception
    interface Params
    ∥获取大班名额
    public int geteigo)
    ∥获取中班名额
    public int getmedium
    获取小班名额
    public int getsma
    ∥获取入学序列,例如[1223]表示依次报名入学一名大班学生,中班学生,中班学生,小班学生

img

望采纳

public class Schoolsystem {
    private int big;
    private int medium;
    private int small;
    
    public Schoolsystem(int big, int medium, int small) {
        this.big = big;
        this.medium = medium;
        this.small = small;
    }
    
    public boolean addstudent(int stutype) {
        if (stutype == 1 && big > 0) {
            big--;
            return true;
        } else if (stutype == 2 && medium > 0) {
            medium--;
            return true;
        } else if (stutype == 3 && small > 0) {
            small--;
            return true;
        }
        return false;
    }
    
    public static Params parse(String input) throws Exception {
        String[] arr = input.split(",");
        int big = Integer.parseInt(arr[0]);
        int medium = Integer.parseInt(arr[1]);
        int small = Integer.parseInt(arr[2]);
        int[] stutype = new int[arr.length - 3];
        for (int i = 3; i < arr.length; i++) {
            stutype[i - 3] = Integer.parseInt(arr[i]);
        }
        return new Params(big, medium, small, stutype);
    }
    
    public void print() {
        System.out.print("[");
        for (int i = 0; i < params.getStutype().length; i++) {
            System.out.print(addstudent(params.getStutype()[i]) + " ");
        }
        System.out.println("]");
    }
    
    public static class Params {
        private int big;
        private int medium;
        private int small;
        private int[] stutype;
        
        public Params(int big, int medium, int small, int[] stutype) {
            this.big = big;
            this.medium = medium;
            this.small = small;
            this.stutype = stutype;
        }
        
        public int getBig() {
            return big;
        }
        
        public int getMedium() {
            return medium;
        }
        
        public int getSmall() {
            return small;
        }
        
        public int[] getStutype() {
            return stutype;
        }
    }
}