From 256c0ce32fa20fb762ec77d9c823706b282455f8 Mon Sep 17 00:00:00 2001 From: Clarenceeey Date: Sun, 10 Nov 2024 16:09:02 +0800 Subject: [PATCH] addtolessoncommand now adds when multiple of same students added in same command --- .../commands/lesson/AddToLessonCommand.java | 43 +++++++++++----- .../lesson/AddToLessonCommandTest.java | 49 +++++++++++++++---- 2 files changed, 72 insertions(+), 20 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/lesson/AddToLessonCommand.java b/src/main/java/seedu/address/logic/commands/lesson/AddToLessonCommand.java index d9c44e9e569..6d9b5dc994c 100644 --- a/src/main/java/seedu/address/logic/commands/lesson/AddToLessonCommand.java +++ b/src/main/java/seedu/address/logic/commands/lesson/AddToLessonCommand.java @@ -5,6 +5,7 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_INDEX; import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -22,7 +23,6 @@ import seedu.address.model.lesson.Lesson; import seedu.address.model.student.Name; import seedu.address.model.student.Student; -import seedu.address.model.student.exceptions.DuplicateStudentException; /** * Adds students to a specific lesson. @@ -46,6 +46,7 @@ public class AddToLessonCommand extends Command { public static final String MESSAGE_DUPLICATE_STUDENT_IN_LESSON_BY_INDEX = "%s at index %d is already added to the lesson!"; + private final Index index; private final List studentNames; private final List indices; @@ -83,19 +84,29 @@ public CommandResult execute(Model model) throws CommandException { logger.info("Lesson to edit: " + targetLesson.toString()); + Set studentsToAddSet = new HashSet<>(); + ArrayList studentsToAddArr = new ArrayList<>(); + for (Name studentName : studentNames) { Student student = model.findStudentByName(studentName) .orElseThrow(() -> new CommandException(String.format(MESSAGE_STUDENT_NOT_FOUND, studentName))); - try { - editedLesson.addStudent(student); - } catch (DuplicateStudentException e) { + + // if student was already in the lesson before this command, throw error + if (editedLesson.hasStudent(student)) { logger.warning("Students were not added to lesson " + targetLesson.toString() - + " because there were duplicate names"); + + " because the student " + studentName + " was already in lesson"); throw new CommandException( String.format(MESSAGE_DUPLICATE_STUDENT_IN_LESSON_BY_NAME, studentName)); } + + //if student was not previously in the lesson before this command, add it to the set + if (!studentsToAddSet.contains(student)) { + studentsToAddArr.add(student); + } + studentsToAddSet.add(student); } + // checking if the indices are out of bounds boolean throwException = false; Set outOfBounds = new HashSet<>(); @@ -107,26 +118,36 @@ public CommandResult execute(Model model) throws CommandException { } if (throwException) { - logger.warning("Students were not added to lesson " + targetLesson.toString() - + " because there were indices of students that were out of bounds"); String formattedOutOfBoundIndices = outOfBounds.stream() .map(index -> String.valueOf(index.getOneBased())) .collect(Collectors.joining(", ")); + logger.warning("Students were not added to lesson " + targetLesson.toString() + + " because indices " + formattedOutOfBoundIndices + " were out of bounds"); throw new CommandException(String.format(Messages.MESSAGE_INVALID_INDEX_SHOWN, formattedOutOfBoundIndices)); } + // checking if the students at these indices were already in the lesson for (Index studentIndex : indices) { Student student = lastShownStudentList.get(studentIndex.getZeroBased()); - try { - editedLesson.addStudent(student); - } catch (DuplicateStudentException e) { + + if (editedLesson.hasStudent(student)) { logger.warning("Students were not added to lesson " + targetLesson.toString() - + " because there were duplicate names"); + + " because student " + student.getName() + " was already in the lesson"); throw new CommandException( String.format(MESSAGE_DUPLICATE_STUDENT_IN_LESSON_BY_INDEX, student.getName(), studentIndex.getOneBased())); } + + if (!studentsToAddSet.contains(student)) { + studentsToAddArr.add(student); + } + studentsToAddSet.add(student); + } + + // actually adding the students into the lesson + for (Student student: studentsToAddArr) { + editedLesson.addStudent(student); } model.setLesson(targetLesson, editedLesson); diff --git a/src/test/java/seedu/address/logic/commands/lesson/AddToLessonCommandTest.java b/src/test/java/seedu/address/logic/commands/lesson/AddToLessonCommandTest.java index 2b9bc652564..bb85e028784 100644 --- a/src/test/java/seedu/address/logic/commands/lesson/AddToLessonCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/lesson/AddToLessonCommandTest.java @@ -202,27 +202,58 @@ public void execute_someInvalidStudentNames_throwsCommandException() { } @Test - public void execute_duplicateStudentByIndexInCommand_throwsCommandException() { + public void execute_duplicateStudentByIndexInCommandSuccess() throws Exception { AddToLessonCommand command = new AddToLessonCommand(validLessonIndex5, validStudentNames, duplicateIndices); - String error = String.format(AddToLessonCommand.MESSAGE_DUPLICATE_STUDENT_IN_LESSON_BY_INDEX, - ALICE.getName().fullName, - INDEX_FIRST_STUDENT.getOneBased()); + Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); - assertCommandFailure(command, model, error); + CommandResult result = command.execute(model); + + Lesson targetLesson = expectedModel.getFilteredLessonList() + .get(validLessonIndex5.getZeroBased()); + Lesson editedLesson = new Lesson(targetLesson); + + editedLesson.addStudent(CARL); + editedLesson.addStudent(DANIEL); + editedLesson.addStudent(ALICE); + expectedModel.setLesson(targetLesson, editedLesson); + + String expectedMessage = String.format(AddToLessonCommand.MESSAGE_ADD_TO_LESSON_SUCCESS, + Messages.format(editedLesson)); + + assertEquals(result.getFeedbackToUser(), expectedMessage); + assertEquals(expectedModel.getFilteredLessonList().get(validLessonIndex5.getZeroBased()), + model.getFilteredLessonList().get(validLessonIndex5.getZeroBased())); } @Test - public void execute_duplicateStudentByNameInCommand_throwsCommandException() { + public void execute_duplicateStudentByNameInCommandSuccess() throws Exception { AddToLessonCommand command = new AddToLessonCommand(validLessonIndex5, duplicateStudentNames, validStudentIndicesContainsAlice); - String error = String.format(AddToLessonCommand.MESSAGE_DUPLICATE_STUDENT_IN_LESSON_BY_NAME, - CARL.getName().fullName); + Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); + + CommandResult result = command.execute(model); + + Lesson targetLesson = expectedModel.getFilteredLessonList() + .get(validLessonIndex5.getZeroBased()); + Lesson editedLesson = new Lesson(targetLesson); + + editedLesson.addStudent(CARL); + editedLesson.addStudent(ALICE); + editedLesson.addStudent(BENSON); + expectedModel.setLesson(targetLesson, editedLesson); + + String expectedMessage = String.format(AddToLessonCommand.MESSAGE_ADD_TO_LESSON_SUCCESS, + Messages.format(editedLesson)); + + assertEquals(result.getFeedbackToUser(), expectedMessage); + assertEquals(expectedModel.getFilteredLessonList().get(validLessonIndex5.getZeroBased()), + model.getFilteredLessonList().get(validLessonIndex5.getZeroBased())); + - assertCommandFailure(command, model, error); } @Test