Skip to content

Commit

Permalink
Merge pull request #26 from PureUsagi/feature-DeleteCommand
Browse files Browse the repository at this point in the history
Command and DeleteCommand Feature
  • Loading branch information
YHWong20 authored Mar 12, 2024
2 parents 3f467dc + 292f6be commit 8224a7e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/seedu/binbash/ItemList.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public void addItem(Item item) {
itemList.add(item);
}

public String deleteItem(int index) {
Item tempItem = itemList.remove(index - 1);

String output = "Got it! I've removed the following item:"
+ String.format("\t%s", tempItem);
return output;
}

@Override
public String toString() {
return itemList.toString();
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/seedu/binbash/command/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package seedu.binbash.command;

import java.util.regex.Pattern;
import seedu.binbash.ItemList;

public abstract class Command {
public static final String COMMAND_STRING = "command";
public static final Pattern COMMAND_FORMAT =
Pattern.compile("(?<command>\\S+)(?<arguments>.*)");
protected ItemList itemList;

protected Command(ItemList itemList) {
this.itemList = itemList;
}

public abstract String execute();
}
21 changes: 21 additions & 0 deletions src/main/java/seedu/binbash/command/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package seedu.binbash.command;

import java.util.regex.Pattern;
import seedu.binbash.ItemList;

public class DeleteCommand extends Command {
public static final String COMMAND_STRING = "delete";
public static final Pattern COMMAND_FORMAT =
Pattern.compile("delete\\s(?<index>\\d+)");
protected int index;

public DeleteCommand(ItemList itemList, int index) {
super(itemList);

this.index = index;
}

public String execute() {
return itemList.deleteItem(index);
}
}

0 comments on commit 8224a7e

Please sign in to comment.