Skip to content

Commit

Permalink
Merge pull request #140 from Cary-Xx/final
Browse files Browse the repository at this point in the history
Add history command and minor fix
  • Loading branch information
Cary-Xx committed Nov 10, 2019
2 parents b550c8d + 767342f commit 11a499b
Show file tree
Hide file tree
Showing 29 changed files with 235 additions and 59 deletions.
58 changes: 58 additions & 0 deletions src/main/java/seedu/address/logic/CommandHistory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package seedu.address.logic;

import static java.util.Objects.requireNonNull;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
* Stores the history of commands executed.
*/
public class CommandHistory {
private final ObservableList<String> userInputHistory = FXCollections.observableArrayList();
private final ObservableList<String> unmodifiableUserInputHistory =
FXCollections.unmodifiableObservableList(userInputHistory);

public CommandHistory() {}

public CommandHistory(CommandHistory commandHistory) {
userInputHistory.addAll(commandHistory.userInputHistory);
}

/**
* Appends {@code userInput} to the list of user input entered.
*/
public void add(String userInput) {
requireNonNull(userInput);
userInputHistory.add(userInput);
}

/**
* Returns an unmodifiable view of {@code userInputHistory}.
*/
public ObservableList<String> getHistory() {
return unmodifiableUserInputHistory;
}

@Override
public boolean equals(Object obj) {
// short circuit if same object
if (obj == this) {
return true;
}

// instanceof handles nulls
if (!(obj instanceof CommandHistory)) {
return false;
}

// state check
CommandHistory other = (CommandHistory) obj;
return userInputHistory.equals(other.userInputHistory);
}

@Override
public int hashCode() {
return userInputHistory.hashCode();
}
}
10 changes: 8 additions & 2 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public class LogicManager implements Logic {

private final Model model;
private final Storage storage;
private final CommandHistory history;
private final MymParser mymParser;

public LogicManager(Model model, Storage storage) {
this.model = model;
this.storage = storage;
history = new CommandHistory();
mymParser = new MymParser();
}

Expand All @@ -42,8 +44,12 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
logger.info("----------------[USER COMMAND][" + commandText + "]");

CommandResult commandResult;
Command command = mymParser.parseCommand(commandText);
commandResult = command.execute(model);
try {
Command command = mymParser.parseCommand(commandText);
commandResult = command.execute(model, history);
} finally {
history.add(commandText);
}

try {
storage.saveExpenseList(model.getExpenseList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_END_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ViewState;
Expand Down Expand Up @@ -49,7 +50,7 @@ public AddBudgetCommand(Budget budget) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

if (toAdd.getEndDate().localDate.isBefore(toAdd.getStartDate().localDate)) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ViewState;
Expand Down Expand Up @@ -49,7 +50,7 @@ public AddCommand(Expense expense) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

if (model.hasExpense(toAdd)) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.ExpenseList;
import seedu.address.model.Model;
Expand All @@ -22,7 +23,7 @@ public class ClearCommand extends Command {
public static final String MESSAGE_CLEAR_ERROR = "An error occurred while trying to clear list";

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
ViewState viewState = model.getViewState();

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/seedu/address/logic/commands/Command.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.logic.commands;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;

Expand All @@ -12,8 +13,9 @@ public abstract class Command {
* Executes the command and returns the result message.
*
* @param model {@code Model} which the command should operate on.
* @param history history of user inputs.
* @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;
public abstract CommandResult execute(Model model, CommandHistory history) throws CommandException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ViewState;
Expand Down Expand Up @@ -35,7 +36,7 @@ public DeleteCommand(Index targetIndex) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

ViewState viewState = model.getViewState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ViewState;
Expand Down Expand Up @@ -77,7 +78,7 @@ private static Budget createEditedBudget(Budget budgetToEdit, EditBudgetDescript
}

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

List<Budget> lastShownList = model.getFilteredBudgetList();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ViewState;
Expand Down Expand Up @@ -87,7 +88,7 @@ private static Expense createEditedExpense(Expense expenseToEdit, EditExpenseDes
}

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
ViewState viewState = model.getViewState();
List<Expense> lastShownList;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/ExitCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.logic.commands;

import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;

/**
Expand All @@ -12,7 +13,7 @@ public class ExitCommand extends Command {
public static final String MESSAGE_EXIT_ACKNOWLEDGEMENT = "Exiting MYMorise as requested ...";

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

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;

import seedu.address.commons.core.Messages;
import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;
import seedu.address.model.expense.NameContainsKeywordsPredicate;

Expand All @@ -26,7 +27,7 @@ public FindCommand(NameContainsKeywordsPredicate predicate) {
}

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model, CommandHistory history) {
requireNonNull(model);
model.updateFilteredExpenseList(predicate);
int size = model.getFilteredExpenseList().size();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/HelpCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.logic.commands;

import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;

/**
Expand All @@ -15,7 +16,7 @@ public class HelpCommand extends Command {
public static final String SHOWING_HELP_MESSAGE = "Opened help window.";

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model, CommandHistory history) {
return new CommandResult(SHOWING_HELP_MESSAGE, true, false);
}
}
32 changes: 32 additions & 0 deletions src/main/java/seedu/address/logic/commands/HistoryCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.ArrayList;
import java.util.Collections;

import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;

/**
* Lists all the commands entered by user from the start of app launch.
*/
public class HistoryCommand extends Command {

public static final String COMMAND_WORD = "history";
public static final String MESSAGE_SUCCESS = "Entered commands (from most recent to earliest):\n%1$s";
public static final String MESSAGE_NO_HISTORY = "You have not yet entered any commands.";

@Override
public CommandResult execute(Model model, CommandHistory history) {
requireNonNull(history);
ArrayList<String> previousCommands = new ArrayList<>(history.getHistory());

if (previousCommands.isEmpty()) {
return new CommandResult(MESSAGE_NO_HISTORY);
}

Collections.reverse(previousCommands);
return new CommandResult(String.format(MESSAGE_SUCCESS, String.join("\n", previousCommands)));
}
}
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_EXPENSES;

import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;
import seedu.address.model.ViewState;

Expand All @@ -16,7 +17,7 @@ public class ListCommand extends Command {
public static final String MESSAGE_SUCCESS = "Listed all expenses";

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model, CommandHistory history) {
requireNonNull(model);
model.updateFilteredExpenseList(PREDICATE_SHOW_ALL_EXPENSES);
model.setViewState(ViewState.DEFAULT_EXPENSELIST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ViewState;
Expand Down Expand Up @@ -35,7 +36,7 @@ public ViewBudgetCommand(Index targetIndex) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

List<Budget> lastShownList = model.getFilteredBudgetList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_BUDGETS;

import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;
import seedu.address.model.ViewState;

Expand All @@ -16,7 +17,7 @@ public class ViewBudgetListCommand extends Command {
public static final String MESSAGE_SUCCESS = "Listed all budgets";

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model, CommandHistory history) {
requireNonNull(model);
model.updateFilteredBudgetList(PREDICATE_SHOW_ALL_BUDGETS);
model.setViewState(ViewState.BUDGETLIST);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/parser/MymParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.HistoryCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.ViewBudgetCommand;
import seedu.address.logic.commands.ViewBudgetListCommand;
Expand Down Expand Up @@ -84,6 +85,9 @@ public Command parseCommand(String userInput) throws ParseException {
case AddBudgetCommand.COMMAND_WORD:
return new AddBudgetCommandParser().parse(arguments);

case HistoryCommand.COMMAND_WORD:
return new HistoryCommand();

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/address/model/budget/Budget.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public void addExpenseIntoBudget(Expense expense) {
recalculateAmountLeft();
}


/**
* @param expense target expense in the budget
*/
Expand Down
Loading

0 comments on commit 11a499b

Please sign in to comment.