Skip to content

Commit ca92d6d

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#73 from yuming7144/branch-description-commands
Implement delete description
2 parents 98039dd + 071fe3a commit ca92d6d

File tree

6 files changed

+164
-27
lines changed

6 files changed

+164
-27
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package seedu.address.logic.commands;
2+
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
3+
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_EXPENSES;
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+
import seedu.address.model.person.Description;
13+
14+
15+
/**
16+
* Changes the description of an existing expense in the address book.
17+
*/
18+
public class DeleteDescriptionCommand extends Command {
19+
20+
public static final String COMMAND_WORD = "deleteDes";
21+
22+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes the description of the expense identified "
23+
+ "by the index number used in the last expense listing. "
24+
+ "Existing expense will have empty description.\n"
25+
+ "Parameters: INDEX (must be a positive integer) "
26+
+ "d/ [DESCRIPTION]\n"
27+
+ "Example: " + COMMAND_WORD + " 1 ";
28+
29+
public static final String MESSAGE_NOT_IMPLEMENTED_YET = "Description command not implemented yet";
30+
public static final String MESSAGE_ARGUMENTS = "Index: %1$d, Description: %2$s";
31+
public static final String MESSAGE_ADD_DESCRIPTION_SUCCESS = "Added description to Expense: %1$s \n";
32+
public static final String MESSAGE_DELETE_DESCRIPTION_SUCCESS = "Removed description from Expense: %1$s \n";
33+
private final Index index;
34+
private static final Description EMPTY_DESCRIPTION = new Description("");
35+
36+
/**
37+
* @param index of the expense in the filtered expense list to edit the description
38+
*/
39+
public DeleteDescriptionCommand(Index index) {
40+
requireAllNonNull(index);
41+
42+
this.index = index;
43+
}
44+
45+
@Override
46+
public CommandResult execute(Model model) throws CommandException {
47+
List<Expense> lastShownList = model.getFilteredExpenseList();
48+
49+
if (index.getZeroBased() >= lastShownList.size()) {
50+
throw new CommandException(Messages.MESSAGE_INVALID_EXPENSE_DISPLAYED_INDEX);
51+
}
52+
53+
Expense expenseToEdit = lastShownList.get(index.getZeroBased());
54+
Expense editedExpense = new Expense(expenseToEdit.getAmount(), expenseToEdit.getDate(), expenseToEdit.getCategory(),
55+
EMPTY_DESCRIPTION);
56+
57+
model.setExpense(expenseToEdit, editedExpense);
58+
model.updateFilteredExpenseList(PREDICATE_SHOW_ALL_EXPENSES);
59+
60+
return new CommandResult(generateSuccessMessage(editedExpense));
61+
}
62+
63+
@Override
64+
public boolean equals(Object other) {
65+
// short circuit if same object
66+
if (other == this) {
67+
return true;
68+
}
69+
70+
// instanceof handles nulls
71+
if (!(other instanceof DeleteDescriptionCommand)) {
72+
return false;
73+
}
74+
75+
// state check
76+
DeleteDescriptionCommand e = (DeleteDescriptionCommand) other;
77+
return index.equals(e.index);
78+
}
79+
80+
/**
81+
* Generates a command execution success message based on whether the description is added to or removed from
82+
* {@code expenseToEdit}.
83+
*/
84+
private String generateSuccessMessage(Expense expenseToEdit) {
85+
String message = MESSAGE_DELETE_DESCRIPTION_SUCCESS;
86+
return String.format(message, expenseToEdit);
87+
}
88+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package seedu.address.logic.parser;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION;
6+
7+
import seedu.address.commons.core.index.Index;
8+
import seedu.address.commons.exceptions.IllegalValueException;
9+
import seedu.address.logic.commands.DeleteDescriptionCommand;
10+
import seedu.address.logic.commands.DescriptionCommand;
11+
import seedu.address.logic.parser.exceptions.ParseException;
12+
13+
14+
/**
15+
* Parses input arguments and creates a new {@code DescriptionCommand} object
16+
*/
17+
public class DeleteDescriptionCommandParser implements Parser<DeleteDescriptionCommand> {
18+
/**
19+
* Parses the given {@code String} of arguments in the context of the {@code DescriptionCommand}
20+
* and returns a {@code DescriptionCommand} object for execution.
21+
* @throws ParseException if the user input does not conform the expected format
22+
*/
23+
public DeleteDescriptionCommand parse(String args) throws ParseException {
24+
requireNonNull(args);
25+
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_DESCRIPTION);
26+
27+
Index index;
28+
try {
29+
index = ParserUtil.parseIndex(argMultimap.getPreamble());
30+
} catch (IllegalValueException ive) {
31+
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DescriptionCommand.MESSAGE_USAGE), ive);
32+
}
33+
34+
return new DeleteDescriptionCommand(index);
35+
}
36+
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
import java.util.regex.Matcher;
77
import java.util.regex.Pattern;
88

9-
import seedu.address.logic.commands.*;
9+
import seedu.address.logic.commands.Command;
10+
import seedu.address.logic.commands.DeleteDescriptionCommand;
11+
import seedu.address.logic.commands.DescriptionCommand;
12+
import seedu.address.logic.commands.HelpCommand;
13+
import seedu.address.logic.commands.SetBudgetCommand;
14+
import seedu.address.logic.commands.ShowBudgetCommand;
15+
1016
import seedu.address.logic.parser.exceptions.ParseException;
1117

1218
/**
@@ -41,6 +47,9 @@ public Command parseCommand(String userInput) throws ParseException {
4147
case DescriptionCommand
4248
.COMMAND_WORD:
4349
return new DescriptionCommandParser().parse(arguments);
50+
case DeleteDescriptionCommand
51+
.COMMAND_WORD:
52+
return new DeleteDescriptionCommandParser().parse(arguments);
4453

4554
case ShowBudgetCommand
4655
.COMMAND_WORD:

src/main/java/seedu/address/model/person/Description.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/**
77
* Represents an expense's description in the finance manager.
8-
* Guarantees: immutable; is valid as declared in {@link #isValidDescription(String)}
8+
* Guarantees: immutable;
99
*/
1010
public class Description {
1111

@@ -29,6 +29,10 @@ public Description(String description) {
2929
value = description;
3030
}
3131

32+
public boolean isEmpty() {
33+
return value.equals("");
34+
}
35+
3236

3337
@Override
3438
public String toString() {

src/main/java/seedu/address/model/person/Expense.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ public int hashCode() {
8787
public String toString() {
8888
final StringBuilder builder = new StringBuilder();
8989

90-
// if (description.isEmpty()) {
91-
// builder.append(" Amount: ")
92-
// .append(this.getAmount())
93-
// .append(" Date: ")
94-
// .append(this.getDate())
95-
// .append(" Category: ")
96-
// .append(this.getCategory());
97-
// return builder.toString();
98-
// } else {
90+
if (description.isEmpty()) {
91+
builder.append(" Amount: ")
92+
.append(this.getAmount())
93+
.append(" Date: ")
94+
.append(this.getDate())
95+
.append(" Category: ")
96+
.append(this.getCategory());
97+
return builder.toString();
98+
} else {
9999
builder.append(" Amount: ")
100100
.append(this.getAmount())
101101
.append(" Date: ")
@@ -105,7 +105,7 @@ public String toString() {
105105
.append(" Description: ")
106106
.append(this.getDescription());
107107
return builder.toString();
108-
// }
108+
}
109109
}
110110

111111
}

src/test/java/seedu/address/model/person/DescriptionTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ public void constructor_invalidAddress_throwsIllegalArgumentException() {
1919
assertThrows(IllegalArgumentException.class, () -> new Description(invalidDescription));
2020
}
2121

22-
@Test
23-
public void isValidDescription() {
24-
// null address
25-
assertThrows(NullPointerException.class, () -> Description.isValidDescription(null));
26-
27-
// invalid addresses
28-
assertFalse(Description.isValidDescription("")); // empty string
29-
assertFalse(Description.isValidDescription(" ")); // spaces only
30-
31-
// valid addresses
32-
assertTrue(Description.isValidDescription("Movie, together with popcorn"));
33-
assertTrue(Description.isValidDescription("-")); // one character
34-
assertTrue(Description.isValidDescription("Went out with friends; Watched movie; "
35-
+ "Had ice cream and tea; Bugis junction")); // long description
36-
}
22+
// @Test
23+
// public void isValidDescription() {
24+
// // null address
25+
// assertThrows(NullPointerException.class, () -> Description.isValidDescription(null));
26+
//
27+
// // invalid addresses
28+
// assertFalse(Description.isValidDescription("")); // empty string
29+
// assertFalse(Description.isValidDescription(" ")); // spaces only
30+
//
31+
// // valid addresses
32+
// assertTrue(Description.isValidDescription("Movie, together with popcorn"));
33+
// assertTrue(Description.isValidDescription("-")); // one character
34+
// assertTrue(Description.isValidDescription("Went out with friends; Watched movie; "
35+
// + "Had ice cream and tea; Bugis junction")); // long description
36+
// }
3737
}

0 commit comments

Comments
 (0)