Skip to content

Commit 8f89d61

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#78 from BILLXYR/branch-Add-Functionality-addDeleteCommandFunction
Branch add functionality add delete command function
2 parents 1223267 + a73b9b7 commit 8f89d61

File tree

5 files changed

+92
-4
lines changed

5 files changed

+92
-4
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import java.util.List;
6+
7+
import seedu.address.commons.core.Messages;
8+
import seedu.address.commons.core.index.Index;
9+
import seedu.address.logic.commands.exceptions.CommandException;
10+
import seedu.address.model.Model;
11+
import seedu.address.model.person.Expense;
12+
13+
/**
14+
* Deletes a person identified using it's displayed index from the address book.
15+
*/
16+
public class DeleteExpenseCommand extends Command {
17+
18+
public static final String COMMAND_WORD = "delete";
19+
20+
public static final String MESSAGE_USAGE = COMMAND_WORD
21+
+ ": Deletes the expense identified by the index number used in the displayed expense list.\n"
22+
+ "Parameters: INDEX (must be a positive integer)\n"
23+
+ "Example: " + COMMAND_WORD + " 1";
24+
25+
public static final String MESSAGE_DELETE_EXPENSE_SUCCESS = "Deleted expense: %1$s";
26+
27+
private final Index targetIndex;
28+
29+
public DeleteExpenseCommand(Index targetIndex) {
30+
this.targetIndex = targetIndex;
31+
}
32+
33+
@Override
34+
public CommandResult execute(Model model) throws CommandException {
35+
requireNonNull(model);
36+
List<Expense> lastShownList = model.getFilteredExpenseList();
37+
38+
if (targetIndex.getZeroBased() >= lastShownList.size()) {
39+
throw new CommandException(Messages.MESSAGE_INVALID_EXPENSE_DISPLAYED_INDEX);
40+
}
41+
42+
Expense expenseToDelete = lastShownList.get(targetIndex.getZeroBased());
43+
model.deleteExpense(expenseToDelete);
44+
return new CommandResult(String.format(MESSAGE_DELETE_EXPENSE_SUCCESS, expenseToDelete));
45+
}
46+
47+
@Override
48+
public boolean equals(Object other) {
49+
return other == this // short circuit if same object
50+
|| (other instanceof DeleteExpenseCommand // instanceof handles nulls
51+
&& targetIndex.equals(((DeleteExpenseCommand) other).targetIndex)); // state check
52+
}
53+
}

src/main/java/seedu/address/logic/commands/exceptions/CommandException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package seedu.address.logic.commands.exceptions;
22

33
/**
4-
* Represents an error which occurs during execution of a {@link Command}.
4+
* Represents an error which occurs during execution of a command.
55
*/
66
public class CommandException extends Exception {
77
public CommandException(String message) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package seedu.address.logic.parser;
2+
3+
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
4+
5+
import seedu.address.commons.core.index.Index;
6+
import seedu.address.logic.commands.DeleteCommand;
7+
import seedu.address.logic.commands.DeleteExpenseCommand;
8+
import seedu.address.logic.parser.exceptions.ParseException;
9+
10+
/**
11+
* Parses input arguments and creates a new DeleteCommand object
12+
*/
13+
public class DeleteExpenseCommandParser implements Parser<DeleteExpenseCommand> {
14+
15+
/**
16+
* Parses the given {@code String} of arguments in the context of the DeleteCommand
17+
* and returns a DeleteCommand object for execution.
18+
* @throws ParseException if the user input does not conform the expected format
19+
*/
20+
public DeleteExpenseCommand parse(String args) throws ParseException {
21+
try {
22+
Index index = ParserUtil.parseIndex(args);
23+
return new DeleteExpenseCommand(index);
24+
} catch (ParseException pe) {
25+
throw new ParseException(
26+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
27+
}
28+
}
29+
30+
}

src/main/java/seedu/address/logic/parser/ExpenseBookParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import seedu.address.logic.commands.AddExpenseCommand;
1010
import seedu.address.logic.commands.Command;
11+
import seedu.address.logic.commands.DeleteExpenseCommand;
1112
import seedu.address.logic.commands.DeleteDescriptionCommand;
1213
import seedu.address.logic.commands.DescriptionCommand;
1314
import seedu.address.logic.commands.ListExpenseCommand;
@@ -46,6 +47,8 @@ public Command parseCommand(String userInput) throws ParseException {
4647
return new DescriptionCommandParser().parse(arguments);
4748
case AddExpenseCommand.COMMAND_WORD:
4849
return new AddExpenseCommandParser().parse(arguments);
50+
case DeleteExpenseCommand.COMMAND_WORD:
51+
return new DeleteExpenseCommandParser().parse(arguments);
4952

5053
case ListExpenseCommand.COMMAND_WORD:
5154
return new ListExpenseCommand();

src/main/java/seedu/address/model/ExpenseModelManager.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,18 @@ public ReadOnlyExpenseBook getExpenseBook() {
9292

9393
@Override
9494
public boolean hasExpense(Expense expense) {
95-
return false;
95+
requireNonNull(expense);
96+
return expenseBook.hasExpense(expense);
9697
}
9798

9899
@Override
99100
public void deleteExpense(Expense targetExpense) {
100-
101+
expenseBook.removeExpense(targetExpense);
101102
}
102103
@Override
103104
public void addExpense(Expense expense) {
104-
105+
expenseBook.addExpense(expense);
106+
updateFilteredExpenseList(PREDICATE_SHOW_ALL_EXPENSES);
105107
}
106108

107109
@Override

0 commit comments

Comments
 (0)