Пример #1
-47
  // This method adds a chosen course to the existing student's schedule if there is an empty space.
  // It takes in a Course object for the corresponding course and returns no value.
  // It outputs an error message if there is no empty spaces left in the student's schedule.
  public void addChosenCourse(Course newCourse) {
    // This finds the student's current schedule size.
    int scheduleSize = schedule.getCourseChosen().size();

    // This checks if the course is already selected by the student.
    boolean chosen = false;

    for (int i = 0; i < scheduleSize && !chosen; i++) {
      if (schedule.getCourseChosen().get(i).compareToCourseCode(newCourse) == 0) {
        chosen = true;
      }
    }

    // This checks the capacity of the student's schedule.
    // It adds the course if the schedule has 3 or fewer courses and outputs an error message if the
    // schedule has 4 courses.
    if (scheduleSize < MAX_SCHEDULE && !chosen) {
      schedule.getCourseChosen().add(scheduleSize, newCourse);
      System.out.println("The course is add from the student's schedule.");
    } else {
      // This outputs an error message if the entire schedule if full.
      System.out.println(
          "Sorry. The student's schedule is full or the course is already in the student's time table.");
      System.out.println("No new courses can be added.");
    }
  }
Пример #2
-48
  // This method deletes a chosen course from the existing student's schedule if such a course can
  // be found.
  // It takes in a Course object for the corresponding course and returns no value.
  // It outputs an error message if there is no such course found in the student's existing
  // schedule.
  public void deleteChosenCourse(Course deletedCourse) {
    // This initializes a boolean to be false to assume that there is such a course yet to be found
    // in the student's
    // existing schedule.
    boolean found = false;

    // This loops through the student's existing schedule to find the course.
    for (int i = 0; i < schedule.getCourseChosen().size() && !found; i++) {
      Course temCourse = schedule.getCourseChosen().get(i);
      if (temCourse.getCourseCode() == deletedCourse.getCourseCode()) {
        found = true;
        // This deletes the selected course by issuing the value of the course to be null.
        schedule.getCourseChosen().remove(i);
        System.out.println("The course is removed from the student's schedule.");
      }
    }

    // This outputs an error message if there is no such a course in the student's existing
    // schedule.
    if (!found) {
      System.out.println("Sorry. The student did not select this course.");
      System.out.println("No courses can be deleted.");
    }
  }