forked from nus-cs2103-AY2021S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nus-cs2103-AY2021S1#73 from yuming7144/branch-desc…
…ription-commands Implement delete description
- Loading branch information
Showing
6 changed files
with
164 additions
and
27 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/main/java/seedu/address/logic/commands/DeleteDescriptionCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package seedu.address.logic.commands; | ||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_EXPENSES; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Expense; | ||
import seedu.address.model.person.Description; | ||
|
||
|
||
/** | ||
* Changes the description of an existing expense in the address book. | ||
*/ | ||
public class DeleteDescriptionCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "deleteDes"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes the description of the expense identified " | ||
+ "by the index number used in the last expense listing. " | ||
+ "Existing expense will have empty description.\n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ "d/ [DESCRIPTION]\n" | ||
+ "Example: " + COMMAND_WORD + " 1 "; | ||
|
||
public static final String MESSAGE_NOT_IMPLEMENTED_YET = "Description command not implemented yet"; | ||
public static final String MESSAGE_ARGUMENTS = "Index: %1$d, Description: %2$s"; | ||
public static final String MESSAGE_ADD_DESCRIPTION_SUCCESS = "Added description to Expense: %1$s \n"; | ||
public static final String MESSAGE_DELETE_DESCRIPTION_SUCCESS = "Removed description from Expense: %1$s \n"; | ||
private final Index index; | ||
private static final Description EMPTY_DESCRIPTION = new Description(""); | ||
|
||
/** | ||
* @param index of the expense in the filtered expense list to edit the description | ||
*/ | ||
public DeleteDescriptionCommand(Index index) { | ||
requireAllNonNull(index); | ||
|
||
this.index = index; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
List<Expense> lastShownList = model.getFilteredExpenseList(); | ||
|
||
if (index.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_EXPENSE_DISPLAYED_INDEX); | ||
} | ||
|
||
Expense expenseToEdit = lastShownList.get(index.getZeroBased()); | ||
Expense editedExpense = new Expense(expenseToEdit.getAmount(), expenseToEdit.getDate(), expenseToEdit.getCategory(), | ||
EMPTY_DESCRIPTION); | ||
|
||
model.setExpense(expenseToEdit, editedExpense); | ||
model.updateFilteredExpenseList(PREDICATE_SHOW_ALL_EXPENSES); | ||
|
||
return new CommandResult(generateSuccessMessage(editedExpense)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof DeleteDescriptionCommand)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
DeleteDescriptionCommand e = (DeleteDescriptionCommand) other; | ||
return index.equals(e.index); | ||
} | ||
|
||
/** | ||
* Generates a command execution success message based on whether the description is added to or removed from | ||
* {@code expenseToEdit}. | ||
*/ | ||
private String generateSuccessMessage(Expense expenseToEdit) { | ||
String message = MESSAGE_DELETE_DESCRIPTION_SUCCESS; | ||
return String.format(message, expenseToEdit); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/seedu/address/logic/parser/DeleteDescriptionCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.exceptions.IllegalValueException; | ||
import seedu.address.logic.commands.DeleteDescriptionCommand; | ||
import seedu.address.logic.commands.DescriptionCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
|
||
/** | ||
* Parses input arguments and creates a new {@code DescriptionCommand} object | ||
*/ | ||
public class DeleteDescriptionCommandParser implements Parser<DeleteDescriptionCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the {@code DescriptionCommand} | ||
* and returns a {@code DescriptionCommand} object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public DeleteDescriptionCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_DESCRIPTION); | ||
|
||
Index index; | ||
try { | ||
index = ParserUtil.parseIndex(argMultimap.getPreamble()); | ||
} catch (IllegalValueException ive) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DescriptionCommand.MESSAGE_USAGE), ive); | ||
} | ||
|
||
return new DeleteDescriptionCommand(index); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters