Skip to content

Commit

Permalink
Merge 58c755e into f4e1b12
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoarmanda committed Oct 15, 2019
2 parents f4e1b12 + 58c755e commit 78d86c8
Show file tree
Hide file tree
Showing 25 changed files with 127 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public CommandResult execute(String commandText) throws CommandException, ParseE

CommandResult commandResult;
Command command = addressBookParser.parseCommand(commandText, model.getUserPrefs());
commandResult = command.execute(model);
commandResult = command.run(model);

try {
storage.saveAddressBook(model.getAddressBook());
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ public AddCommand(Expense expense) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
protected void validate(Model model) throws CommandException {
requireNonNull(model);

if (model.hasExpense(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_EXPENSE);
}
}

@Override
protected CommandResult execute(Model model) {
requireNonNull(model);

model.addExpense(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/seedu/address/logic/commands/AliasCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public AliasCommand(Alias alias) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
protected void validate(Model model) throws CommandException {
requireNonNull(model);

// if command_word is reserved
Expand All @@ -53,9 +53,13 @@ public CommandResult execute(Model model) throws CommandException {
|| model.getUserPrefs().aliasCommandWordIsAlias(commandWord)) {
throw new CommandException(MESSAGE_RECURSIVE_WARNING);
}
}

model.addUserAlias(toAdd);
@Override
protected CommandResult execute(Model model) {
requireNonNull(model);

model.addUserAlias(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd.getAliasName()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@ public BudgetCommand(Budget budget) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
protected void validate(Model model) throws CommandException {
requireNonNull(model);

if (model.hasBudget(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_BUDGET);
}
}

@Override
protected CommandResult execute(Model model) {
requireNonNull(model);

model.addBudget(toAdd);
model.setPrimary(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ public class ClearCommand extends Command {
public static final String COMMAND_WORD = "clear";
public static final String MESSAGE_SUCCESS = "Address book has been cleared!";

@Override
protected void validate(Model model) {
// No validation necessary.
}

@Override
public CommandResult execute(Model model) {
protected CommandResult execute(Model model) {
requireNonNull(model);
model.setAddressBook(new AddressBook());
return new CommandResult(MESSAGE_SUCCESS);
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/seedu/address/logic/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,29 @@
public abstract class Command {

/**
* Executes the command and returns the result message.
* Validates the command parameters.
* @throws CommandException If parameters are invalid.
*/
protected abstract void validate(Model model) throws CommandException;

/**
* Executes the command, without validation, and returns the result message.
*
* @param model {@code Model} which the command should operate on.
* @return feedback message of the operation result for display
* @throws CommandException If an error occurs during command execution.
*/
public abstract CommandResult execute(Model model) throws CommandException;
protected abstract CommandResult execute(Model model) throws CommandException;

/**
* Executes the command with validation and returns the result message.
*
* @param model {@code Model} which the command should operate on.
* @return feedback message of the operation result for display
* @throws CommandException If an error occurs during command execution.
*/
public CommandResult run(Model model) throws CommandException {
validate(model);
return execute(model);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ public DeleteCommand(Index targetIndex) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
protected void validate(Model model) throws CommandException {
requireNonNull(model);
List<Expense> lastShownList = model.getFilteredExpenseList();

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_EXPENSE_DISPLAYED_INDEX);
}
}

@Override
protected CommandResult execute(Model model) {
requireNonNull(model);
List<Expense> lastShownList = model.getFilteredExpenseList();

Expense expenseToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deleteExpense(expenseToDelete);
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,28 @@ public EditCommand(Index index, EditExpenseDescriptor editExpenseDescriptor) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
protected void validate(Model model) throws CommandException {
requireNonNull(model);
List<Expense> lastShownList = model.getFilteredExpenseList();

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 = createEditedExpense(expenseToEdit, editExpenseDescriptor);

if (!expenseToEdit.isSameExpense(editedExpense) && model.hasExpense(editedExpense)) {
throw new CommandException(MESSAGE_DUPLICATE_EXPENSE);
}
}

@Override
protected CommandResult execute(Model model) {
requireNonNull(model);

List<Expense> lastShownList = model.getFilteredExpenseList();
Expense expenseToEdit = lastShownList.get(index.getZeroBased());
Expense editedExpense = createEditedExpense(expenseToEdit, editExpenseDescriptor);

model.setExpense(expenseToEdit, editedExpense);
model.updateFilteredExpenseList(PREDICATE_SHOW_ALL_EXPENSES);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/logic/commands/EventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ public EventCommand(Event event) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
protected void validate(Model model) throws CommandException {
requireNonNull(model);

if (model.hasEvent(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_EVENT);
}
}

@Override
protected CommandResult execute(Model model) {
requireNonNull(model);

model.addEvent(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/logic/commands/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ public class ExitCommand extends Command {
public static final String MESSAGE_EXIT_ACKNOWLEDGEMENT = "Exiting Address Book as requested ...";

@Override
public CommandResult execute(Model model) {
protected void validate(Model model) {
// No validation necessary.
}

@Override
protected CommandResult execute(Model model) {
return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true);
}

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ public FindCommand(DescriptionContainsKeywordsPredicate predicate) {
}

@Override
public CommandResult execute(Model model) {
protected void validate(Model model) {
// No validation necessary.
}

@Override
protected CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredExpenseList(predicate);
return new CommandResult(
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/logic/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public class HelpCommand extends Command {
public static final String SHOWING_HELP_MESSAGE = "Opened help window.";

@Override
public CommandResult execute(Model model) {
protected void validate(Model model) {
// No validation necessary.
}

@Override
protected CommandResult execute(Model model) {
return new CommandResult(SHOWING_HELP_MESSAGE, true, false);
}
}
6 changes: 5 additions & 1 deletion src/main/java/seedu/address/logic/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ public class ListCommand extends Command {

public static final String MESSAGE_SUCCESS = "Listed all expenses";

@Override
protected void validate(Model model) {
// No validation necessary.
}

@Override
public CommandResult execute(Model model) {
protected CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredExpenseList(PREDICATE_SHOW_ALL_EXPENSES);
return new CommandResult(MESSAGE_SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public class RemindersCommand extends Command {
public static final String MESSAGE_SUCCESS = "Listed all reminders";

@Override
public CommandResult execute(Model model) {
protected void validate(Model model) {
// No validation necessary.
}

@Override
protected CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredExpenseList(PREDICATE_SHOW_ALL_EXPENSES);
return new CommandResult(MESSAGE_SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void setUp() {
// No addCommand should not be the same as another addCommand, odds are unlikely because of use of UUID

@Test
public void execute_duplicateExpense_throwsCommandException() {
public void run_duplicateExpense_throwsCommandException() {
Expense expenseInList = model.getAddressBook().getExpenseList().get(0);
assertCommandFailure(new AddCommand(expenseInList), model, AddCommand.MESSAGE_DUPLICATE_EXPENSE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ public void constructor_nullExpense_throwsNullPointerException() {
}

@Test
public void execute_expenseAcceptedByModel_addSuccessful() throws Exception {
public void run_expenseAcceptedByModel_addSuccessful() throws Exception {
ModelStubAcceptingExpenseAdded modelStub = new ModelStubAcceptingExpenseAdded();
Expense validExpense = new ExpenseBuilder().build();

CommandResult commandResult = new AddCommand(validExpense).execute(modelStub);
CommandResult commandResult = new AddCommand(validExpense).run(modelStub);

assertEquals(String.format(AddCommand.MESSAGE_SUCCESS, validExpense), commandResult.getFeedbackToUser());
assertEquals(Arrays.asList(validExpense), modelStub.expensesAdded);
}

@Test
public void execute_duplicateExpense_throwsCommandException() {
public void run_duplicateExpense_throwsCommandException() {
Expense validExpense = new ExpenseBuilder().build();
AddCommand addCommand = new AddCommand(validExpense);
ModelStub modelStub = new ModelStubWithExpense(validExpense);

assertThrows(CommandException.class, AddCommand.MESSAGE_DUPLICATE_EXPENSE, () -> addCommand.execute(modelStub));
assertThrows(CommandException.class, AddCommand.MESSAGE_DUPLICATE_EXPENSE, () -> addCommand.run(modelStub));
}

@Test
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/seedu/address/logic/commands/AliasCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ public void equals() {
}

@Test
public void execute_aliasNameIsReservedCommandWord_throwsCommandException() {
public void run_aliasNameIsReservedCommandWord_throwsCommandException() {
AliasCommand command = new AliasCommand(AliasTestUtil.ALIAS_NAME_ADD);
assertThrows(CommandException.class, () -> command.execute(model));
assertThrows(CommandException.class, () -> command.run(model));
}

@Test
public void execute_aliasCommandWordIsAlias_throwsCommandException() {
public void run_aliasCommandWordIsAlias_throwsCommandException() {
AliasCommand command = new AliasCommand(AliasTestUtil.ALIAS_TO_ALIAS);
assertThrows(CommandException.class, () -> command.execute(model));
assertThrows(CommandException.class, () -> command.run(model));
}

@Test
public void execute_aliasCommandIsValid_success() {
public void run_aliasCommandIsValid_success() {
expectedModel.addUserAlias(ALIAS_A_TO_B);
assertCommandSuccess(
new AliasCommand(ALIAS_A_TO_B), model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
public class ClearCommandTest {

@Test
public void execute_emptyAddressBook_success() {
public void run_emptyAddressBook_success() {
Model model = new ModelManager();
Model expectedModel = new ModelManager();

assertCommandSuccess(new ClearCommand(), model, ClearCommand.MESSAGE_SUCCESS, expectedModel);
}

@Test
public void execute_nonEmptyAddressBook_success() {
public void run_nonEmptyAddressBook_success() {
Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());
expectedModel.setAddressBook(new AddressBook());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class CommandTestUtil {
public static void assertCommandSuccess(Command command, Model actualModel, CommandResult expectedCommandResult,
Model expectedModel) {
try {
CommandResult result = command.execute(actualModel);
CommandResult result = command.run(actualModel);
assertEquals(expectedCommandResult, result);
assertEquals(expectedModel, actualModel);
} catch (CommandException ce) {
Expand Down Expand Up @@ -99,7 +99,7 @@ public static void assertCommandFailure(Command command, Model actualModel, Stri
AddressBook expectedAddressBook = new AddressBook(actualModel.getAddressBook());
List<Expense> expectedFilteredList = new ArrayList<>(actualModel.getFilteredExpenseList());

assertThrows(CommandException.class, expectedMessage, () -> command.execute(actualModel));
assertThrows(CommandException.class, expectedMessage, () -> command.run(actualModel));
assertEquals(expectedAddressBook, actualModel.getAddressBook());
assertEquals(expectedFilteredList, actualModel.getFilteredExpenseList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class DeleteCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void execute_validIndexUnfilteredList_success() {
public void run_validIndexUnfilteredList_success() {
Expense expenseToDelete = model.getFilteredExpenseList().get(INDEX_FIRST_EXPENSE.getZeroBased());
DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_EXPENSE);

Expand All @@ -40,15 +40,15 @@ public void execute_validIndexUnfilteredList_success() {
}

@Test
public void execute_invalidIndexUnfilteredList_throwsCommandException() {
public void run_invalidIndexUnfilteredList_throwsCommandException() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredExpenseList().size() + 1);
DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex);

assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_EXPENSE_DISPLAYED_INDEX);
}

@Test
public void execute_validIndexFilteredList_success() {
public void run_validIndexFilteredList_success() {
showExpenseAtIndex(model, INDEX_FIRST_EXPENSE);

Expense expenseToDelete = model.getFilteredExpenseList().get(INDEX_FIRST_EXPENSE.getZeroBased());
Expand All @@ -64,7 +64,7 @@ public void execute_validIndexFilteredList_success() {
}

@Test
public void execute_invalidIndexFilteredList_throwsCommandException() {
public void run_invalidIndexFilteredList_throwsCommandException() {
showExpenseAtIndex(model, INDEX_FIRST_EXPENSE);

Index outOfBoundIndex = INDEX_SECOND_EXPENSE;
Expand Down

0 comments on commit 78d86c8

Please sign in to comment.