Skip to content

Commit

Permalink
Merge pull request #180 from NBQian/LWE-conflict-bug
Browse files Browse the repository at this point in the history
Lwe conflict bug
  • Loading branch information
Yufannnn authored Apr 6, 2023
2 parents 62f7037 + 5808897 commit 230c0ca
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ public class Messages {
+ "whose name contains \"%s\".";
public static final String MESSAGE_EXTENDED_STUDENT_NAME = "There is at least one existing student "
+ "whose name is contained in \"%s\".";
public static final String MESSAGE_CONFLICTING_LESSON_TIME = "You already have a lesson during this time!";
public static final String MESSAGE_CONFLICTING_EXAM_TIME = "This student has an exam during this time!";
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(String.format(Messages.MESSAGE_HAS_DUPLICATE_NAMES, dupNames));
}

Lesson lesson = new Lesson(lessonName, startTime, endTime);

if (model.hasConflictingLessonTime(lesson)) {
throw new CommandException(Messages.MESSAGE_CONFLICTING_LESSON_TIME);
}

// if (model.hasConflictingExamTime(lesson)) {
// throw new CommandException(Messages.MESSAGE_CONFLICTING_EXAM_TIME);
// }

model.updateFilteredStudentList(predicate);

List<Student> studentList = model.getFilteredStudentList();
Expand All @@ -108,8 +118,6 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(Messages.MESSAGE_INVALID_LESSON_DURATION);
}

Lesson lesson = new Lesson(lessonName, startTime, endTime);

try {
for (Student student : studentList) {
student.addLesson(lesson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ public CommandResult execute(Model model) throws CommandException {
}

Lesson newLesson = new Lesson(newLessonName, newStartTime, newEndTime);
model.updateFilteredStudentList(s -> s != student);

if (model.hasConflictingLessonTime(newLesson)) {
throw new CommandException(Messages.MESSAGE_CONFLICTING_LESSON_TIME);
}

if (model.hasConflictingExamTime(newLesson)) {
throw new CommandException(Messages.MESSAGE_CONFLICTING_EXAM_TIME);
}

try {
student.setLesson(index.getZeroBased(), newLesson);
} catch (Exception e) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.model.student.Lesson;
import seedu.address.model.student.Student;

/**
Expand Down Expand Up @@ -91,4 +92,7 @@ public interface Model {
boolean hasExtendedNameEdit(String name, Integer index);
boolean noSuchStudent(String name);
boolean hasDuplicateNameAdd(String toString);
boolean hasConflictingLessonTime(Lesson lesson);

boolean hasConflictingExamTime(Lesson lesson);
}
21 changes: 21 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.collections.transformation.FilteredList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.student.Lesson;
import seedu.address.model.student.Student;

/**
Expand Down Expand Up @@ -217,4 +218,24 @@ public boolean noSuchStudent(String name) {
}
return true;
}

@Override
public boolean hasConflictingLessonTime(Lesson lesson) {
for (Student s : filteredPersons) {
if (s.hasConflictingLessonTime(lesson)) {
return true;
}
}
return false;
}

@Override
public boolean hasConflictingExamTime(Lesson lesson) {
for (Student s : filteredPersons) {
if (s.hasConflictingExamTime(lesson)) {
return true;
}
}
return false;
}
}
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/student/Exam.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,14 @@ public String toString() {
.append(getStringGrade());
return builder.toString();
}

/**
* Returns true if the given lesson is the same as this one.
* @param lesson The other lesson to compare with.
* @return True if the given lesson has the same time slot as this one.
*/
public boolean isSameTimeLesson(Lesson lesson) {
return lesson != null
&& lesson.getStartTime().isBefore(getEndTime()) && lesson.getEndTime().isAfter(getStartTime());
}
}
21 changes: 21 additions & 0 deletions src/main/java/seedu/address/model/student/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import javafx.collections.ObservableList;
import javafx.scene.chart.PieChart;
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.model.student.exceptions.ConflictingExamsException;
import seedu.address.model.student.exceptions.ConflictingLessonsException;
Expand Down Expand Up @@ -266,13 +267,25 @@ public void deleteLesson(Index index) {
* @param lesson the lesson to be added
*/
public void addLesson(Lesson lesson) throws ConflictingLessonsException {
if (hasExamAtSameTime(lesson)) {
throw new ConflictingLessonsException(Messages.MESSAGE_CONFLICTING_EXAM_TIME);
}
try {
this.lessonsList.add(lesson);
} catch (Exception e) {
throw new ConflictingLessonsException(e.getMessage());
}
}

private boolean hasExamAtSameTime(Lesson lesson) {
for (Exam exam : examList) {
if (lesson.getStartTime().isBefore(exam.getEndTime()) && lesson.getEndTime().isAfter(exam.getStartTime())) {
return true;
}
}
return false;
}

/**
* Returns an immutable assignment list, which throws {@code UnsupportedOperationException}
* if modification is attempted.
Expand Down Expand Up @@ -522,4 +535,12 @@ public ObservableList<Exam> getPastExamsList() {
public ObservableList<Exam> getUpcomingExamsList() {
return examList.getUpcomingExams();
}

public boolean hasConflictingLessonTime(Lesson lesson) {
return this.lessonsList.hasConflictingLessonTime(lesson);
}

public boolean hasConflictingExamTime(Lesson lesson) {
return this.examList.hasConflictingExamTime(lesson);
}
}
14 changes: 14 additions & 0 deletions src/main/java/seedu/address/model/student/UniqueExamList.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,18 @@ public ObservableList<Exam> getUpcomingExams() {
}
return FXCollections.observableArrayList(upcomingExams);
}

/**
* Returns true if the list contains an exam with the same time as the lesson.
* @param lesson the lesson to be checked
* @return true if the list contains an exam with the same time as the lesson.
*/
public boolean hasConflictingExamTime(Lesson lesson) {
for (Exam exam : internalList) {
if (exam.isSameTimeLesson(lesson)) {
return true;
}
}
return false;
}
}
14 changes: 14 additions & 0 deletions src/main/java/seedu/address/model/student/UniqueLessonsList.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,18 @@ public ObservableList<Lesson> getUpcomingLessons() {
public boolean hasLesson() {
return this.internalList.size() != 0;
}

/**
* Returns true if the list contains a lesson that has time conflict as the given argument.
* @param lesson the lesson to be checked
* @return true if the list contains a lesson with conflicting timeslot
*/
public boolean hasConflictingLessonTime(Lesson lesson) {
for (Lesson l : internalList) {
if (l.isSameTimeLesson(lesson)) {
return true;
}
}
return false;
}
}
20 changes: 20 additions & 0 deletions src/test/java/seedu/address/logic/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.student.Lesson;
import seedu.address.model.student.Student;
import seedu.address.testutil.StudentBuilder;

Expand Down Expand Up @@ -171,6 +172,15 @@ public boolean hasExtendedNameEdit(String name, Integer index) {
public boolean hasDuplicateNameAdd(String name) {
throw new AssertionError("this method should not be called.");
}
@Override
public boolean hasConflictingLessonTime(Lesson lesson) {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean hasConflictingExamTime(Lesson lesson) {
throw new AssertionError("This method should not be called.");
}
}

/**
Expand Down Expand Up @@ -213,6 +223,16 @@ public void addPerson(Student person) {
public ReadOnlyAddressBook getAddressBook() {
return new AddressBook();
}

@Override
public boolean hasConflictingLessonTime(Lesson lesson) {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean hasConflictingExamTime(Lesson lesson) {
throw new AssertionError("This method should not be called.");
}
}

}

0 comments on commit 230c0ca

Please sign in to comment.