欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现学生选课系统

程序员文章站 2024-02-28 22:06:34
本文为大家分享了java实现学生选课系统的具体代码,供大家参考,具体内容如下 案例要求: 学生(学号,姓名,专业,所选课程{<3}) 老师(工号,姓名,所教课...

本文为大家分享了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();
 }

}

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

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