forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 3
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 #140 from Cary-Xx/final
Add history command and minor fix
- Loading branch information
Showing
29 changed files
with
235 additions
and
59 deletions.
There are no files selected for viewing
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,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(); | ||
} | ||
} |
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
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
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
32 changes: 32 additions & 0 deletions
32
src/main/java/seedu/address/logic/commands/HistoryCommand.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,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))); | ||
} | ||
} |
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
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
Oops, something went wrong.