温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java实现学生选课系统

发布时间:2020-08-30 19:14:33 来源:脚本之家 阅读:182 作者:跌娣 栏目:编程语言

本文为大家分享了java实现学生选课系统的具体代码,供大家参考,具体内容如下

案例要求:

学生(学号,姓名,专业,所选课程{<3})
老师(工号,姓名,所教课程{<3})
课程(课程号,课程名,学分,教师,已选课学生{<30})

选课系统代码如下:

//teacher

public class Teacher {
 private int id;
 private String teacherName;
 private Course[] courses;
 //构造函数
 public Teacher() {
  super();
  courses= new Course[3];
 }
 public Teacher(int id,String teacherName){
  this.id=id;
  this.teacherName=teacherName;
  courses = new Course[3];
 }
 //修改或是添加属性
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getTeacherName() {
  return teacherName;
 }
 public void setTeacherName(String teacherName) {
  this.teacherName = teacherName;
 }


}
/**
  * 课程
*/

public class Course {
 private String courseName;
 private int courseId;
 private Teacher teacher;
 private float credit;
 private Student[] students;
 //构造函数
 public Course(int courseId,String courseName,float credit,Teacher teacher) {
  super();
  this.courseId=courseId;
  this.courseName=courseName;
  this.credit=credit;
  this.setTeacher(teacher);
  students = new Student[30];
 }
 public Course(int courseId,String courseName,float credit) {
  super();
  this.courseId=courseId;
  this.courseName=courseName;
  this.credit=credit;
  students = new Student[30];
 }

 public Course(int courseId,String courseName) {
  super();
  this.courseId=courseId;
  this.courseName=courseName;
  students = new Student[30];
 }

 public Course() {//默认形式,要有以防万一
  super();
  students = new Student[30];
 }

 //修改或获取属性值id,name,credit,
 public void setId(int id){
  this.courseId=id;
 }
 public int getId(){
  return this.courseId;
 }
 public void setName(String name){
  this.courseName=name;
 }
 public String getName(){
  return this.courseName;
 }
 public void setCredit(float credit ){
  this.credit=credit;
 }
 public float getCredit(){
  return this.credit;
 }
 public Teacher getTeacher() {
  return teacher;
 }
 public void setTeacher(Teacher teacher) {
  this.teacher = teacher;
 }

 //课加入学生
 public boolean addStudent(Student stu){
  boolean flag = false;//标志值:是否加入成功
  //如果学生没有选过这门课,同时课的学生还没满则执行
  if(!isSelectedStudent(stu)&&isNullStudent(stu)){
   for(int i=0;i<students.length;i++){
    if(students[i]==null){
     students[i]=stu;
     flag=true;
     break;
    }
   }
  }
  return flag;
 }
 //课移除学生
 public boolean removeStudent(Student stu){
  boolean flag=false;
  if(isSelectedStudent(stu)){//选过这门课
   for(int i=0;i<students.length;i++){
    if(students[i]==stu){
     students[i]=null;
     flag=true;
     break;
    }
   }
  }
  return flag;
 }
 //显示选择课程的学生:
 public void displayStudent(){
  System.out.println("选择的课程:"+this.courseName+"的学生有:");
  for(Student s:students){
   if(s!=null){
    System.out.print(s.getStuName()+" ");
   }
  }
  System.out.println();
 }
 //子方法1:学生是否选过这门课
 public boolean isSelectedStudent(Student stu){
  boolean flag=false;
  for(Student s:students){//只能用于检查,不能修改
   if(s==stu){
    flag=true;
    break;
   }
  }
  return flag;
 }
 //子方法2:学科学生未达到限定人数吗
 public boolean isNullStudent(Student stu){
  boolean flag=false;
  for(Student s:students){
   if(s==null){//还有空位
    flag=true;
    break;
   }
  }
  return flag;
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub

 }


}
/**
 * 学生代码
 * @author Floris0811
 */
public class Student {
 private String stuName;
 private int stuId;
 private String major;
 private Course[] courses;
 //构造函数
 public Student() {//不要忘
  super();
  courses = new Course[3];
 }
 public Student(int stuId,String stuName) {
  super();
  this.stuId=stuId;
  this.stuName=stuName;
  courses = new Course[3];
 }
 public Student(int stuId,String stuName,String major) {
  super();
  this.stuId=stuId;
  this.stuName=stuName;
  this.major = major;
  courses = new Course[3];
 }
 //修改获取属性name,id,major
 public String getStuName() {
  return stuName;
 }

 public void setStuName(String stuName) {
  this.stuName = stuName;
 }
 public int getStuId() {
  return stuId;
 }
 public void setStuId(int stuId) {
  this.stuId = stuId;
 }
 public String getMajor() {
  return major;
 }
 public void setMajor(String major) {
  this.major = major;
 }
 //学生选课;
 public boolean addCourse(Course course){
  boolean flag=false;
  if(!isSelectedCourse(course)&&isNullCourse(course)){
   for(int i=0;i<this.courses.length;i++){
    if(courses[i]==null){
     courses[i]=course;
     course.addStudent(this);//课程也要添加学生
     flag=true;
     break;
    }
   }
  }
  return flag;
 }
 //学生移除课程
 public boolean removeCourse(Course course){
  boolean flag=false;
  if(isSelectedCourse(course)){
   for(int i=0;i<this.courses.length;i++){
    if(courses[i]==course){
     courses[i]=null;
     course.removeStudent(this);//在课程中移除学生
     flag=true;
     break;
    }
   }

  }
  return flag;
 }
 //显示学生所选的课程
 public void displayCourse(){
  System.out.println("学生"+this.stuName+"所选课程有:");
  for(Course c:courses){
   if(c!=null){
    System.out.print(c.getName()+" ");
   }
  }
  System.out.println();
 }

 //子方法1:课是否被选过
 public boolean isSelectedCourse(Course course){
  boolean flag=false;
  for(Course c:courses){
   if(c==course){
    flag=true;
    break;
   }
  }
  return flag;
 }
 //子方法2:学生是否还有选修课位置
 public boolean isNullCourse(Course course){
  boolean flag=false;
  for(Course c:courses){
   if(c==null){
    flag=true;
    break;
   }
  }
  return flag;
 }


}
package test;

public class ChooseCourseByStu {

 /**
  * 选课管理系统
  */
 public static void main(String[] args) {
  Student stu0 = new Student(1001,"Lily");
  Student stu1 = new Student(1002,"Eilly");
  Student stu2 = new Student(1003,"Floris");
  Student stu3 = new Student(1004,"HaHa");
  Course cour0 = new Course(001,"高数");
  Course cour1 = new Course(002,"线代");
  Course cour2 = new Course(003,"概率论");
  stu0.addCourse(cour0);
  stu0.addCourse(cour2);
  stu0.addCourse(cour1);
  stu1.addCourse(cour2);
  stu1.addCourse(cour0);
  stu2.addCourse(cour1);
  stu3.addCourse(cour0);
  stu3.addCourse(cour1);
  stu1.removeCourse(cour2);
  stu0.displayCourse();
  cour0.removeStudent(stu1);
  cour1.displayStudent();
 }

}

关于管理系统的更多内容请点击《管理系统专题》进行学习

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI