Skip to content

Commit

Permalink
now if the user tries to add an exam to a student that clashes with t…
Browse files Browse the repository at this point in the history
…he student's lessons, the exam is still added but a warning message is shown
  • Loading branch information
Niu BoQian committed Apr 8, 2023
1 parent a758c72 commit ce49492
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public CreateExamCommand(List<String> names, NamePredicate predicate, String exa

@Override
public CommandResult execute(Model model) throws CommandException {
StringBuilder lessonClashMessage = new StringBuilder("\n");
requireNonNull(model);
model.updateFilteredStudentList(PREDICATE_SHOW_ALL_STUDENTS);

Expand Down Expand Up @@ -105,6 +106,10 @@ public CommandResult execute(Model model) throws CommandException {

try {
for (Student student : studentList) {
if (student.hasLessonAtSameTime(exam)) {
lessonClashMessage.append("*******WARNING*******\n").append(student.getName().fullName)
.append(" has a lesson at the same time as the exam\n Consider rescheduling clashing lesson");
}
student.addExam(exam);
}
} catch (Exception e) {
Expand All @@ -118,7 +123,7 @@ public CommandResult execute(Model model) throws CommandException {
}

return new CommandResult(
String.format(Messages.MESSAGE_EXAM_ADDED_SUCCESS, exam, sb));
String.format(Messages.MESSAGE_EXAM_ADDED_SUCCESS, exam, sb) + lessonClashMessage);
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/seedu/address/model/student/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,20 @@ public void addExam(Exam exam) throws DuplicateEntryException, ConflictingExamsE
this.examList.add(exam);
}

/**
* Returns true if the exam's time clashes with any lesson's time.
* @param exam Exam to be checked
* @return boolean
*/
public boolean hasLessonAtSameTime(Exam exam) {
for (Lesson lesson : lessonsList) {
if (exam.getStartTime().isBefore(lesson.getEndTime()) && exam.getEndTime().isAfter(lesson.getStartTime())) {
return true;
}
}
return false;
}

/**
* Returns an exam from the exam list.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
*/
public class ConflictingExamsException extends RuntimeException {
public ConflictingExamsException() {
super("The exam clashes with another exam");
super("The exam clashes with another exam/lesson");
}
}

0 comments on commit ce49492

Please sign in to comment.