java小练习

学生选课,只可以选择开放的课
学生类:学号 姓名 选择多门课程,只能选择开放的课
课程类: 课程号 课程名称 是否开放
管理员类:设置课程是否开放
测试类: 一个学生可以选多门课

public class Course {
    /*学生选课,只可以选择开放的课
   学生类:学号  姓名   选择多门课程,只能选择开放的课
   课程类: 课程号  课程名称  是否开放
   管理员类:设置课程是否开放
   测试类: 一个学生可以选多门课
*/
    private int  cid;
    private String cname;
    private boolean judge;

    public Course(int cid, String cname) {
        this.cid = cid;
        this.cname = cname;
    }

    public String getCname() {
        return cname;
    }

    public Course() {
    }

    public int getCid() {
        return cid;
    }

    public void setJudge(boolean judge) {
        this.judge = judge;
    }

    public boolean getJudge(){
        return this.judge;
    }
    @Override
    public String toString() {
        return "cid=" + cid +
                ", cname='" + cname + '\'';
    }
}

public class Manager {
    public void setcourse(Course course,boolean judge) {
        course.setJudge(judge);
    }
}


public class Student {
    private String ID;
    private String name;
    ArrayList<Course> course=new ArrayList<>();

    public Student(String ID, String name) {
        this.ID = ID;
        this.name = name;
    }

    public void select(Course... courses){
        for (Course course:courses) {
            if (course.getJudge()==true){
                this.course.add(course);
            }else System.out.println(course.getCid()+"\t"+course.getCname()+"课程不可选");;
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "ID='" + ID + '\'' +
                ", name='" + name + '\'' +
                ", course=" + course +
                '}';
    }
}



public class Demo {
    public static void main(String[] args) {
        Course course=new Course(1,"java");
        Course course1=new Course(2,"c++");
        Course course2=new Course(3,"python");
        Manager manager=new Manager();
        manager.setcourse(course,true);
        manager.setcourse(course1,false);
        manager.setcourse(course2,true);
        Student student = new Student("123098","Tom");
        student.select(course,course1,course2);
        System.out.println(student.toString());
    }
}

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两年半的JAVA练习生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值