Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2425S1#153 from JYL27/branch-CommandBugs
Browse files Browse the repository at this point in the history
Fix bugs related to Find Command and Delete Command
  • Loading branch information
btbrandon authored Nov 7, 2024
2 parents 2cb0729 + c347945 commit 6dd1783
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,20 @@ public CommandResult execute(Model model) throws CommandException {
}

if (module != null) {
if (!toDelete.hasModule(module)) {
Module moduleToDelete = null;
for (Module currModule : toDelete.getModules()) {
if (currModule.equals(module)) {
moduleToDelete = currModule;
break;
}
}

if (moduleToDelete == null) {
throw new CommandException(String.format(MESSAGE_MODULE_NOT_FOUND, toDelete.getStudentId()));
}
model.deleteModule(toDelete, module);
return new CommandResult(String.format(MESSAGE_DELETE_MODULE_SUCCESS, module.toString()));

model.deleteModule(toDelete, moduleToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_MODULE_SUCCESS, moduleToDelete));
}

model.deletePerson(toDelete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class FindCommandParser implements Parser<FindCommand> {
*/
public FindCommand parse(String args) throws ParseException {
String trimmedArg = args.trim();
if (trimmedArg.contains(" ")) {
if (trimmedArg.isEmpty() || trimmedArg.contains(" ")) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/seedu/address/logic/commands/DeleteCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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_GRADE_AMY;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex;
Expand All @@ -16,6 +17,7 @@
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Grade;
import seedu.address.model.person.Module;
import seedu.address.model.person.Person;
import seedu.address.model.person.StudentId;
Expand Down Expand Up @@ -185,4 +187,20 @@ public void execute_invalidStudentIdWithModule_throwsCommandException() {
deleteCommand, model, String.format(DeleteCommand.MESSAGE_PERSON_NOT_FOUND, invalidStudentId));
}

@Test
public void execute_deleteGradedModule_success() {
Person personWithModule = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
StudentId studentIdToDelete = personWithModule.getStudentId();
Module moduleToDelete = personWithModule.getModules().get(0);
moduleToDelete.setGrade(new Grade(VALID_GRADE_AMY));

DeleteCommand deleteCommand = new DeleteCommand(studentIdToDelete, moduleToDelete);
String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_MODULE_SUCCESS, moduleToDelete);

ModelManager expectedModel = new ModelManager(model.getEduContacts(), new UserPrefs());
expectedModel.deleteModule(personWithModule, moduleToDelete);

assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ public class FindCommandParserTest {
private FindCommandParser parser = new FindCommandParser();

@Test
public void parse_validArgs_returnsDeleteCommand() {
public void parse_validArgs_returnsFindCommand() {
StudentId validStudentId = new StudentId(CommandTestUtil.VALID_STUDENTID_BOB);
assertParseSuccess(parser, validStudentId.toString(), new FindCommand(validStudentId));
}

@Test
public void parse_invalidArgs_throwsParseException() {
// empty input
assertParseFailure(parser, "",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));

// multiple arguments
assertParseFailure(parser, "11111111 abc",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
Expand Down

0 comments on commit 6dd1783

Please sign in to comment.