Skip to content

Commit

Permalink
Merge pull request #101 from yyyaohhh/Delete-Feature
Browse files Browse the repository at this point in the history
Add Delete feature
  • Loading branch information
rocketninja7 authored Oct 13, 2023
2 parents 8c9b7be + d421837 commit 37ec484
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 45 deletions.
1 change: 0 additions & 1 deletion src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,4 @@ public static String format(Module module) {
.append(module.getGrade());
return builder.toString();
}

}
41 changes: 19 additions & 22 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CODE;

import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.module.Module;
import seedu.address.model.module.ModuleCode;


/**
* Deletes a person identified using it's displayed index from the address book.
Expand All @@ -19,33 +19,30 @@ public class DeleteCommand extends Command {
public static final String COMMAND_WORD = "delete";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the person identified by the index number used in the displayed person list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";
+ ": Deletes the module identified by the module code. "
+ "Parameters: " + PREFIX_CODE + "code "
+ "Example: " + COMMAND_WORD + " " + PREFIX_CODE + "CS2103T ";

public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";
public static final String MESSAGE_DELETE_MODULE_SUCCESS = "Deleted Module: %1$s";
public static final String MESSAGE_NOT_FOUND_MODULE = "This module is not found and has not been added.";

private final Index targetIndex;
private final ModuleCode toDelete;

public DeleteCommand(Index targetIndex) {
this.targetIndex = targetIndex;
public DeleteCommand(ModuleCode toDelete) {
this.toDelete = toDelete;
}

@Override
public CommandResult execute(Model model) throws CommandException {
throw new CommandException("DeleteCommand not implemented yet");
/*
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();
Module targetMod = model.findModuleUsingCode(toDelete);

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
if (!model.hasModule(targetMod)) {
throw new CommandException(MESSAGE_NOT_FOUND_MODULE);
}

Person personToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)));
*/
model.deleteModule(targetMod);
return new CommandResult(String.format(MESSAGE_DELETE_MODULE_SUCCESS, Messages.format(targetMod)));
}

@Override
Expand All @@ -60,13 +57,13 @@ public boolean equals(Object other) {
}

DeleteCommand otherDeleteCommand = (DeleteCommand) other;
return targetIndex.equals(otherDeleteCommand.targetIndex);
return toDelete.equals(otherDeleteCommand.toDelete);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetIndex", targetIndex)
.add("toDelete", toDelete)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.module.ModuleCode;

/**
* Parses input arguments and creates a new DeleteCommand object
Expand All @@ -18,8 +18,9 @@ public class DeleteCommandParser implements Parser<DeleteCommand> {
*/
public DeleteCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteCommand(index);
ModuleCode code = ParserUtil.parseCode(args);
return new DeleteCommand(code);

} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javafx.collections.ObservableList;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.module.Module;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.module.UniqueModuleList;

/**
Expand Down Expand Up @@ -76,8 +77,6 @@ public void addModule(Module m) {
modules.add(m);
}



/**
* Removes {@code key} from this {@code AddressBook}.
* {@code key} must exist in the address book.
Expand All @@ -97,6 +96,17 @@ public void setModule(Module target, Module editedModule) {
modules.setModules(target, editedModule);
}

/**
* Finds and returns a module using its module code from the internal list of modules.
*
* @param code The module code to search for.
* @return The module with the specified module code, or null if not found.
* @throws NullPointerException If the provided module code is null.
*/
public Module findUsingCode(ModuleCode code) {
requireNonNull(code);
return modules.find(code);
}


//// util methods
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.model.module.Module;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.person.Person;

/**
* The API of the Model component.
Expand Down Expand Up @@ -76,7 +78,15 @@ public interface Model {
*/
void setModule(Module target, Module editedPerson);

/** Returns an unmodifiable view of the filtered module list */
/**
* Finds and returns a module using its module code.
*
* @param code The module code used to search for the module.
* @return The module with the specified module code, or null if not found.
*/
Module findModuleUsingCode(ModuleCode code);

/** Returns an unmodifiable view of the filtered person list */
ObservableList<Module> getFilteredModuleList();

/**
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.module.Module;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.person.Person;

/**
* Represents the in-memory model of the address book data.
Expand Down Expand Up @@ -94,6 +96,7 @@ public boolean hasModule(Module module) {
return addressBook.hasModule(module);
}

@Override
public void deleteModule(Module module) {
requireNonNull(module);
addressBook.removeModule(module);
Expand All @@ -112,7 +115,13 @@ public void setModule(Module target, Module editedModule) {
addressBook.setModule(target, editedModule);
}

//=========== Filtered Module List Accessors =============================================================
@Override
public Module findModuleUsingCode(ModuleCode code) {
requireAllNonNull(code);
return addressBook.findUsingCode(code);
}

//=========== Filtered Person List Accessors =============================================================

/**
* Returns an unmodifiable view of the list of {@code Module} backed by the internal list of
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/seedu/address/model/module/UniqueModuleList.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,35 @@ public void remove(Module toRemove) {
}
}

/**
* Replaces the modules in the internal list with the modules from the provided `UniqueModuleList`.
*
* @param replacement The `UniqueModuleList` containing the modules to replace the internal list.
* @throws NullPointerException If the provided replacement is null.
*/
public void setModules(UniqueModuleList replacement) {
requireNonNull(replacement);
internalList.setAll(replacement.internalList);
}

/**
* Finds and returns the module with the specified module code.
*
* @param code The module code to search for.
* @return The module with the specified code.
* @throws ModuleNotFoundException If no module with the given code is found.
*/
public Module find(ModuleCode code) {
Module[] mods = new Module[internalList.size()];
mods = internalList.toArray(mods);
for (int i = 0; i < internalList.size(); i++) {
if (mods[i].getModuleCode().equals(code)) {
return mods[i];
}
}
throw new ModuleNotFoundException();
}

/**
* Replaces the contents of this list with {@code modules}.
* {@code modules} must not contain duplicate modules.
Expand Down
35 changes: 22 additions & 13 deletions src/test/java/seedu/address/logic/commands/DeleteCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.module.Module;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.person.Person;
import seedu.address.testutil.ModuleBuilder;

/**
* Contains integration tests (interaction with the Model) and unit tests for
Expand All @@ -29,14 +32,15 @@ public class DeleteCommandTest {

@Test
public void execute_validIndexUnfilteredList_success() {
Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_PERSON);
Module moduleToDelete = new ModuleBuilder().withCode("CS2030S").build();
ModuleCode code = moduleToDelete.getModuleCode();
DeleteCommand deleteCommand = new DeleteCommand(code);

String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS,
Messages.format(personToDelete));
String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_MODULE_SUCCESS,
Messages.format(moduleToDelete));

ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
expectedModel.deletePerson(personToDelete);
expectedModel.deleteModule(moduleToDelete);

assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel);
}
Expand Down Expand Up @@ -81,14 +85,18 @@ public void execute_invalidIndexFilteredList_throwsCommandException() {

@Test
public void equals() {
DeleteCommand deleteFirstCommand = new DeleteCommand(INDEX_FIRST_PERSON);
DeleteCommand deleteSecondCommand = new DeleteCommand(INDEX_SECOND_PERSON);
Module cs2030s = new ModuleBuilder().withCode("CS2030S").build();
Module cs2040s = new ModuleBuilder().withCode("CS2040S").build();
ModuleCode firstCode = cs2030s.getModuleCode();
ModuleCode secondCode = cs2040s.getModuleCode();
DeleteCommand deleteFirstCommand = new DeleteCommand(firstCode);
DeleteCommand deleteSecondCommand = new DeleteCommand(secondCode);

// same object -> returns true
assertTrue(deleteFirstCommand.equals(deleteFirstCommand));

// same values -> returns true
DeleteCommand deleteFirstCommandCopy = new DeleteCommand(INDEX_FIRST_PERSON);
DeleteCommand deleteFirstCommandCopy = new DeleteCommand(firstCode);
assertTrue(deleteFirstCommand.equals(deleteFirstCommandCopy));

// different types -> returns false
Expand All @@ -103,18 +111,19 @@ public void equals() {

@Test
public void toStringMethod() {
Index targetIndex = Index.fromOneBased(1);
DeleteCommand deleteCommand = new DeleteCommand(targetIndex);
String expected = DeleteCommand.class.getCanonicalName() + "{targetIndex=" + targetIndex + "}";
Module cs2030s = new ModuleBuilder().withCode("CS2030S").build();
ModuleCode code = cs2030s.getModuleCode();
DeleteCommand deleteCommand = new DeleteCommand(code);
String expected = DeleteCommand.class.getCanonicalName() + "{targetModule=" + code + "}";
assertEquals(expected, deleteCommand.toString());
}

/**
* Updates {@code model}'s filtered list to show no one.
*/
private void showNoPerson(Model model) {
model.updateFilteredPersonList(p -> false);
model.updateFilteredModuleList(p -> false);

assertTrue(model.getFilteredPersonList().isEmpty());
assertTrue(model.getFilteredModuleList().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalModules.CS2030S;

import org.junit.jupiter.api.Test;

Expand All @@ -22,11 +22,12 @@ public class DeleteCommandParserTest {

@Test
public void parse_validArgs_returnsDeleteCommand() {
assertParseSuccess(parser, "1", new DeleteCommand(INDEX_FIRST_PERSON));
assertParseSuccess(parser, "CS2030", new DeleteCommand(CS2030S.getModuleCode()));
}

@Test
public void parse_invalidArgs_throwsParseException() {
assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
assertParseFailure(parser, "2030", String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}
}

0 comments on commit 37ec484

Please sign in to comment.