Skip to content

Commit

Permalink
Delete student from tutorial (#89)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
whelan-low authored Mar 20, 2024
1 parent 0cf4239 commit 6f3de9f
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<ModuleCode> list = model.getAddressBook().getModuleList();
for (ModuleCode module : list) {
ArrayList<TutorialClass> tutorialClassesOfModule = module.getTutorialClasses();
for (TutorialClass tutorialClass : tutorialClassesOfModule) {
tutorialClass.deleteStudent(student);
}
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/seedu/address/model/module/ModuleCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/model/module/TutorialClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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));

}
}
27 changes: 27 additions & 0 deletions src/test/java/seedu/address/model/module/ModuleCodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
42 changes: 42 additions & 0 deletions src/test/java/seedu/address/model/module/TutorialClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,46 @@ void testConstructorWithStudents() {
assertTrue(tutorialClass.getStudents().contains(student1));
assertTrue(tutorialClass.getStudents().contains(student2));
}

@Test
void deleteStudent_notInList_failure() {
ArrayList<Person> students = new ArrayList<>();

// Creating Person objects
Name name1 = new Name("John");
Email email1 = new Email("john@example.com");
StudentId stuId1 = new StudentId("A0213333J");
Set<Tag> 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<Tag> 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<Person> students = new ArrayList<>();

// Creating Person object
Name name1 = new Name("John");
Email email1 = new Email("john@example.com");
StudentId stuId1 = new StudentId("A0213333J");
Set<Tag> 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());
}
}

0 comments on commit 6f3de9f

Please sign in to comment.