java Realization of Students' Course Selection System

  • 2021-07-01 07:22:36
  • OfStack

This paper shares the specific code of java to realize the student course selection system for your reference, the specific contents are as follows

Case requirements:

Students (student number, name, major, selected course { < 3})
Teacher (job number, name, course taught { < 3})
Courses (course number, course name, credits, teacher, selected students { < 30})

The code of the course selection system is as follows:


//teacher

public class Teacher {
 private int id;
 private String teacherName;
 private Course[] courses;
 // Constructor 
 public Teacher() {
  super();
  courses= new Course[3];
 }
 public Teacher(int id,String teacherName){
  this.id=id;
  this.teacherName=teacherName;
  courses = new Course[3];
 }
 // Modify or add attributes 
 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;
 }


}

/**
  *  Curriculum 
*/

public class Course {
 private String courseName;
 private int courseId;
 private Teacher teacher;
 private float credit;
 private Student[] students;
 // Constructor 
 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() {// The default form should be in case of ten thousand 1
  super();
  students = new Student[30];
 }

 // Modify or get property values 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;
 }

 // Enroll students in the class 
 public boolean addStudent(Student stu){
  boolean flag = false;// Flag value: whether the addition was successful 
  // If the student has not taken this course and the students in the same course are not full, it will be implemented 
  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;
 }
 // Class removes students 
 public boolean removeStudent(Student stu){
  boolean flag=false;
  if(isSelectedStudent(stu)){// Have taken this course 
   for(int i=0;i<students.length;i++){
    if(students[i]==stu){
     students[i]=null;
     flag=true;
     break;
    }
   }
  }
  return flag;
 }
 // Displays the students of the selected course: 
 public void displayStudent(){
  System.out.println(" Selected courses: "+this.courseName+" Of the students are :");
  for(Student s:students){
   if(s!=null){
    System.out.print(s.getStuName()+" ");
   }
  }
  System.out.println();
 }
 // Sub-method 1 Have students ever taken this course 
 public boolean isSelectedStudent(Student stu){
  boolean flag=false;
  for(Student s:students){// Can only be used for inspection, cannot be modified 
   if(s==stu){
    flag=true;
    break;
   }
  }
  return flag;
 }
 // Sub-method 2 Does the number of students in the subject not reach the limit 
 public boolean isNullStudent(Student stu){
  boolean flag=false;
  for(Student s:students){
   if(s==null){// There are still vacancies 
    flag=true;
    break;
   }
  }
  return flag;
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub

 }


}

/**
 *  Student code 
 * @author Floris0811
 */
public class Student {
 private String stuName;
 private int stuId;
 private String major;
 private Course[] courses;
 // Constructor 
 public Student() {// Don't forget 
  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];
 }
 // Modify the get property 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;
 }
 // Students choose courses; 
 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);// Students should also be added to the course 
     flag=true;
     break;
    }
   }
  }
  return flag;
 }
 // Students remove courses 
 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);// Remove students from the course 
     flag=true;
     break;
    }
   }

  }
  return flag;
 }
 // Displays the course selected by the student 
 public void displayCourse(){
  System.out.println(" Students "+this.stuName+" The selected courses are: ");
  for(Course c:courses){
   if(c!=null){
    System.out.print(c.getName()+" ");
   }
  }
  System.out.println();
 }

 // Sub-method 1 Have you ever chosen a class 
 public boolean isSelectedCourse(Course course){
  boolean flag=false;
  for(Course c:courses){
   if(c==course){
    flag=true;
    break;
   }
  }
  return flag;
 }
 // Sub-method 2 Are there any elective courses available for students 
 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 {

 /**
  *  Course selection management system 
  */
 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," High number ");
  Course cour1 = new Course(002," Line generation ");
  Course cour2 = new Course(003," Probability theory ");
  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();
 }

}

For more information about management system, please click on "Management System Topics" to learn


Related articles: