溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java實現(xiàn)學生選課系統(tǒng)

發(fā)布時間:2020-08-30 19:14:33 來源:腳本之家 閱讀:179 作者:跌娣 欄目:編程語言

本文為大家分享了java實現(xiàn)學生選課系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

案例要求:

學生(學號,姓名,專業(yè),所選課程{<3})
老師(工號,姓名,所教課程{<3})
課程(課程號,課程名,學分,教師,已選課學生{<30})

選課系統(tǒng)代碼如下:

//teacher

public class Teacher {
 private int id;
 private String teacherName;
 private Course[] courses;
 //構(gòu)造函數(shù)
 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;
 //構(gòu)造函數(shù)
 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;//標志值:是否加入成功
  //如果學生沒有選過這門課,同時課的學生還沒滿則執(zhí)行
  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:學科學生未達到限定人數(shù)嗎
 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;
 //構(gòu)造函數(shù)
 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 {

 /**
  * 選課管理系統(tǒng)
  */
 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,"高數(shù)");
  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();
 }

}

關(guān)于管理系統(tǒng)的更多內(nèi)容請點擊《管理系統(tǒng)專題》進行學習

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI