Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2021S1#73 from yuming7144/branch-desc…
Browse files Browse the repository at this point in the history
…ription-commands

Implement delete description
  • Loading branch information
yuming7144 authored Oct 14, 2020
2 parents 98039dd + 071fe3a commit ca92d6d
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 27 deletions.
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);
}
}
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);
}
}
11 changes: 10 additions & 1 deletion src/main/java/seedu/address/logic/parser/ExpenseBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import seedu.address.logic.commands.*;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteDescriptionCommand;
import seedu.address.logic.commands.DescriptionCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.SetBudgetCommand;
import seedu.address.logic.commands.ShowBudgetCommand;

import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -41,6 +47,9 @@ public Command parseCommand(String userInput) throws ParseException {
case DescriptionCommand
.COMMAND_WORD:
return new DescriptionCommandParser().parse(arguments);
case DeleteDescriptionCommand
.COMMAND_WORD:
return new DeleteDescriptionCommandParser().parse(arguments);

case ShowBudgetCommand
.COMMAND_WORD:
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/seedu/address/model/person/Description.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

Expand All @@ -29,6 +29,10 @@ public Description(String description) {
value = description;
}

public boolean isEmpty() {
return value.equals("");
}


@Override
public String toString() {
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/seedu/address/model/person/Expense.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ public int hashCode() {
public String toString() {
final StringBuilder builder = new StringBuilder();

// if (description.isEmpty()) {
// builder.append(" Amount: ")
// .append(this.getAmount())
// .append(" Date: ")
// .append(this.getDate())
// .append(" Category: ")
// .append(this.getCategory());
// return builder.toString();
// } else {
if (description.isEmpty()) {
builder.append(" Amount: ")
.append(this.getAmount())
.append(" Date: ")
.append(this.getDate())
.append(" Category: ")
.append(this.getCategory());
return builder.toString();
} else {
builder.append(" Amount: ")
.append(this.getAmount())
.append(" Date: ")
Expand All @@ -105,7 +105,7 @@ public String toString() {
.append(" Description: ")
.append(this.getDescription());
return builder.toString();
// }
}
}

}
30 changes: 15 additions & 15 deletions src/test/java/seedu/address/model/person/DescriptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public void constructor_invalidAddress_throwsIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> new Description(invalidDescription));
}

@Test
public void isValidDescription() {
// null address
assertThrows(NullPointerException.class, () -> Description.isValidDescription(null));

// invalid addresses
assertFalse(Description.isValidDescription("")); // empty string
assertFalse(Description.isValidDescription(" ")); // spaces only

// valid addresses
assertTrue(Description.isValidDescription("Movie, together with popcorn"));
assertTrue(Description.isValidDescription("-")); // one character
assertTrue(Description.isValidDescription("Went out with friends; Watched movie; "
+ "Had ice cream and tea; Bugis junction")); // long description
}
// @Test
// public void isValidDescription() {
// // null address
// assertThrows(NullPointerException.class, () -> Description.isValidDescription(null));
//
// // invalid addresses
// assertFalse(Description.isValidDescription("")); // empty string
// assertFalse(Description.isValidDescription(" ")); // spaces only
//
// // valid addresses
// assertTrue(Description.isValidDescription("Movie, together with popcorn"));
// assertTrue(Description.isValidDescription("-")); // one character
// assertTrue(Description.isValidDescription("Went out with friends; Watched movie; "
// + "Had ice cream and tea; Bugis junction")); // long description
// }
}

0 comments on commit ca92d6d

Please sign in to comment.