From 6f3de9f62f47ebccbc7e18665214ee5db751f829 Mon Sep 17 00:00:00 2001 From: Whelan <111336032+whelan-low@users.noreply.github.com> Date: Wed, 20 Mar 2024 14:21:39 +0800 Subject: [PATCH] Delete student from tutorial (#89) * Add code to delete student from tutorial class. Returns true if successful. Also modified the tutorialClass deletion method from ModuleCode to do the same for tutorialClasses * Hook delete students from tutorial classes command to deleteStudent classes * Add test cases for email and id * Refactor code into the abstract DeleteStudent class --- .../DeleteStudentByEmailCommand.java | 3 ++ .../DeleteStudentByIdCommand.java | 3 ++ .../DeleteStudentByIndexCommand.java | 3 ++ .../DeleteStudentCommand.java | 23 +++++++++ .../address/model/module/ModuleCode.java | 6 ++- .../address/model/module/TutorialClass.java | 9 ++++ .../commands/DeleteStudentCommandTest.java | 48 +++++++++++++++++++ .../address/model/module/ModuleCodeTest.java | 27 +++++++++++ .../model/module/TutorialClassTest.java | 42 ++++++++++++++++ 9 files changed, 162 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentByEmailCommand.java b/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentByEmailCommand.java index acec878d757..4b02fc93008 100644 --- a/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentByEmailCommand.java +++ b/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentByEmailCommand.java @@ -11,6 +11,8 @@ import seedu.address.model.person.Email; import seedu.address.model.person.Person; + + /** * Deletes a student identified using email from the address book. */ @@ -36,6 +38,7 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(String.format(PersonMessages.MESSAGE_PERSON_EMAIL_NOT_FOUND, email)); } model.deletePerson(personToDelete); + deleteStudentFromTutorialClasses(model, personToDelete); return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete))); } diff --git a/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentByIdCommand.java b/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentByIdCommand.java index 6dd745a2d33..711c2e2406c 100644 --- a/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentByIdCommand.java +++ b/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentByIdCommand.java @@ -11,6 +11,8 @@ import seedu.address.model.person.Person; import seedu.address.model.person.StudentId; + + /** * Deletes a student identified using student ID from the address book. */ @@ -36,6 +38,7 @@ public CommandResult execute(Model model) throws CommandException { } model.deletePerson(personToDelete); + deleteStudentFromTutorialClasses(model, personToDelete); return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete))); } diff --git a/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentByIndexCommand.java b/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentByIndexCommand.java index 1b68d7d84f7..f4f45337b91 100644 --- a/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentByIndexCommand.java +++ b/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentByIndexCommand.java @@ -11,6 +11,8 @@ import seedu.address.model.Model; import seedu.address.model.person.Person; + + /** * Deletes a student identified using index from the address book. */ @@ -37,6 +39,7 @@ public CommandResult execute(Model model) throws CommandException { targetIndex.getOneBased())); } model.deletePerson(personToDelete); + deleteStudentFromTutorialClasses(model, personToDelete); return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete))); } diff --git a/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentCommand.java b/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentCommand.java index 959723709e4..64b3e2d23b9 100644 --- a/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentCommand.java +++ b/src/main/java/seedu/address/logic/commands/deletestudentcommands/DeleteStudentCommand.java @@ -1,9 +1,16 @@ package seedu.address.logic.commands.deletestudentcommands; +import java.util.ArrayList; + +import javafx.collections.ObservableList; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; +import seedu.address.model.module.ModuleCode; +import seedu.address.model.module.TutorialClass; +import seedu.address.model.person.Person; + /** * Abstract class for DeleteStudentCommand @@ -31,4 +38,20 @@ public DeleteStudentCommand() { public abstract boolean equals(Object other); public abstract String toString(); + + /** + * Deletes a specified student from all tutorial classes from all modules in a model. + * + * @param model that contains the modules and tutorial classes + * @param student to be deleted + */ + public void deleteStudentFromTutorialClasses(Model model, Person student) { + ObservableList list = model.getAddressBook().getModuleList(); + for (ModuleCode module : list) { + ArrayList tutorialClassesOfModule = module.getTutorialClasses(); + for (TutorialClass tutorialClass : tutorialClassesOfModule) { + tutorialClass.deleteStudent(student); + } + } + } } diff --git a/src/main/java/seedu/address/model/module/ModuleCode.java b/src/main/java/seedu/address/model/module/ModuleCode.java index ac834ea9e7e..bca0ece9ed3 100644 --- a/src/main/java/seedu/address/model/module/ModuleCode.java +++ b/src/main/java/seedu/address/model/module/ModuleCode.java @@ -156,8 +156,10 @@ public void addTutorialClass(TutorialClass tutorialClass) { * The tutorial has to exist to be used in this function. * * @param tutorialClass name of tutorial class to be deleted. + * + * @return true if tutorial class was successfully deleted */ - public void deleteTutorialClass(TutorialClass tutorialClass) { - tutorialClasses.remove(tutorialClass); + public boolean deleteTutorialClass(TutorialClass tutorialClass) { + return tutorialClasses.remove(tutorialClass); } } diff --git a/src/main/java/seedu/address/model/module/TutorialClass.java b/src/main/java/seedu/address/model/module/TutorialClass.java index 26bd331005a..4d24e3b6f44 100644 --- a/src/main/java/seedu/address/model/module/TutorialClass.java +++ b/src/main/java/seedu/address/model/module/TutorialClass.java @@ -98,6 +98,15 @@ public void addStudent(Person student) { students.add(student); } + /** + * Removes a student from the tutorial class if it exists. + * + * @return true if the student was removed + */ + public boolean deleteStudent(Person student) { + return students.remove(student); + } + /** * Checks if the student is in the tutorial class. * @param student diff --git a/src/test/java/seedu/address/logic/commands/DeleteStudentCommandTest.java b/src/test/java/seedu/address/logic/commands/DeleteStudentCommandTest.java index 59f320a8cb3..324685e4dc5 100644 --- a/src/test/java/seedu/address/logic/commands/DeleteStudentCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/DeleteStudentCommandTest.java @@ -1,5 +1,6 @@ package seedu.address.logic.commands; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_AMY; @@ -22,6 +23,8 @@ import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; +import seedu.address.model.module.ModuleCode; +import seedu.address.model.module.TutorialClass; import seedu.address.model.person.Email; import seedu.address.model.person.Person; import seedu.address.model.person.StudentId; @@ -121,4 +124,49 @@ private void showNoPerson(Model model) { assertTrue(model.getFilteredPersonList().isEmpty()); } + + @Test + public void deleteStudentFromTutorialClasses_success() { + //Create modules, tutorial classes and student objects + ModuleCode cs2103T = new ModuleCode("CS2103T"); + ModuleCode cs2101 = new ModuleCode("CS2101"); + TutorialClass t01 = new TutorialClass("T01"); + TutorialClass t02 = new TutorialClass("T02"); + TutorialClass t03 = new TutorialClass("T03"); + TutorialClass t04 = new TutorialClass("T04"); + + Person studentToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + Person student2 = model.getFilteredPersonList().get(INDEX_SECOND_PERSON.getZeroBased()); + + //assign students to tutorials and tutorials to modules + t01.addStudent(studentToDelete); + t02.addStudent(student2); + t03.addStudent(studentToDelete); + t03.addStudent(student2); + + cs2103T.addTutorialClass(t01); //should have 0 people upon deletion + cs2103T.addTutorialClass(t02); //should have 1 person upon deletion + cs2101.addTutorialClass(t03); //should have 1 person upon deletion + cs2101.addTutorialClass(t04); //should have 0 people upon deletion + + model.addModule(cs2103T); + model.addModule(cs2101); + + DeleteStudentCommand deleteCommand = new DeleteStudentByIndexCommand(INDEX_FIRST_PERSON); + + String expectedMessage = String.format(DeleteStudentCommand.MESSAGE_DELETE_PERSON_SUCCESS, + Messages.format(studentToDelete)); + + ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + expectedModel.deletePerson(studentToDelete); + + assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel); + assertEquals(0, t01.getStudents().size()); + assertEquals(1, t02.getStudents().size()); + assertEquals(1, t03.getStudents().size()); + assertEquals(0, t04.getStudents().size()); + assertEquals(student2, t02.getStudents().get(0)); + assertEquals(student2, t03.getStudents().get(0)); + + } } diff --git a/src/test/java/seedu/address/model/module/ModuleCodeTest.java b/src/test/java/seedu/address/model/module/ModuleCodeTest.java index 3f996d24bf0..babcaa6fc3b 100644 --- a/src/test/java/seedu/address/model/module/ModuleCodeTest.java +++ b/src/test/java/seedu/address/model/module/ModuleCodeTest.java @@ -88,4 +88,31 @@ void listTutorialClasses_empty_success() { String expectedString = "Tutorials in CS2103T: None!"; assertEquals(moduleCode.listTutorialClasses(), expectedString); } + + @Test + void deleteTutorialClass_notInList_failure() { + + // Creating TutorialClass objects + TutorialClass tutorialClass1 = new TutorialClass("T01"); + TutorialClass tutorialClass2 = new TutorialClass("T02"); + + ModuleCode moduleCode = new ModuleCode("CS2103T"); + moduleCode.addTutorialClass(tutorialClass1); + + assertFalse(moduleCode.deleteTutorialClass(tutorialClass2)); + assertEquals(1, moduleCode.getTutorialClasses().size()); + } + + @Test + void deleteTutorialClass_inList_success() { + + // Creating TutorialClass objects + TutorialClass tutorialClass1 = new TutorialClass("T01"); + + ModuleCode moduleCode = new ModuleCode("CS2103T"); + moduleCode.addTutorialClass(tutorialClass1); + + assertTrue(moduleCode.deleteTutorialClass(tutorialClass1)); + assertEquals(0, moduleCode.getTutorialClasses().size()); + } } diff --git a/src/test/java/seedu/address/model/module/TutorialClassTest.java b/src/test/java/seedu/address/model/module/TutorialClassTest.java index 0e291ef1e66..c79fbd6b952 100644 --- a/src/test/java/seedu/address/model/module/TutorialClassTest.java +++ b/src/test/java/seedu/address/model/module/TutorialClassTest.java @@ -145,4 +145,46 @@ void testConstructorWithStudents() { assertTrue(tutorialClass.getStudents().contains(student1)); assertTrue(tutorialClass.getStudents().contains(student2)); } + + @Test + void deleteStudent_notInList_failure() { + ArrayList students = new ArrayList<>(); + + // Creating Person objects + Name name1 = new Name("John"); + Email email1 = new Email("john@example.com"); + StudentId stuId1 = new StudentId("A0213333J"); + Set tags1 = new HashSet<>(); + Person student1 = new Person(name1, email1, stuId1, tags1); + + Name name2 = new Name("Alice"); + Email email2 = new Email("alice@example.com"); + StudentId stuId2 = new StudentId("A0145678A"); + Set tags2 = new HashSet<>(); + Person student2 = new Person(name2, email2, stuId2, tags2); + + students.add(student1); + + TutorialClass tutorialClass = new TutorialClass(VALID_TUTORIAL, students); + assertFalse(tutorialClass.deleteStudent(student2)); + assertEquals(1, tutorialClass.getStudents().size()); + } + + @Test + void deleteStudent_inList_success() { + ArrayList students = new ArrayList<>(); + + // Creating Person object + Name name1 = new Name("John"); + Email email1 = new Email("john@example.com"); + StudentId stuId1 = new StudentId("A0213333J"); + Set tags1 = new HashSet<>(); + Person student1 = new Person(name1, email1, stuId1, tags1); + + students.add(student1); + + TutorialClass tutorialClass = new TutorialClass(VALID_TUTORIAL, students); + assertTrue(tutorialClass.deleteStudent(student1)); + assertEquals(0, tutorialClass.getStudents().size()); + } }