Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[W5][W13-3]Ruhani Suri #159

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ Deletes the 2nd person in the address book.
`delete 1` +
Deletes the 1st person in the results of the `find` command.

== Listing all previous commands: `history`

Displays all user entered commands in reverse chronological order. +
Format: `history`

****
Shows all previous commands, with the latest ones first.
****

Examples:

* `history` +
`delete 2` +
`list`


== View non-private details of a person : `view`

Displays the non-private details of the specified person. +
Expand Down
8 changes: 6 additions & 2 deletions src/seedu/addressbook/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Stack;

import seedu.addressbook.commands.Command;
import seedu.addressbook.commands.CommandResult;
Expand All @@ -24,7 +25,7 @@ public class Main {

/** Version info of the program. */
public static final String VERSION = "AddressBook Level 2 - Version 1.0";

public static Stack<String> CommandHistory = new Stack<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be public?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed it to private in Main.java now, however it is public in Parser.java and HistoryCommand.java. I think that is needed because I will be passing the CommandHistory Stack from main to parser to the the command handling class. Please let me know if this can be done in a better way and why this approach is not preferred.

Copy link

@ewaldhew ewaldhew Feb 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't that its not preferred, as long as you have a reason to make things publicly accessible then that's fine :)

Remember to close this PR!

private TextUi ui;
private StorageFile storage;
private AddressBook addressBook;
Expand Down Expand Up @@ -83,7 +84,10 @@ private void runCommandLoopUntilExitCommand() {
Command command;
do {
String userCommandText = ui.getUserCommand();
command = new Parser().parseCommand(userCommandText);
CommandHistory.push(userCommandText);
Parser parser = new Parser();
parser.CHStack = CommandHistory;
command = parser.parseCommand(userCommandText);
CommandResult result = executeCommand(command);
recordResult(result);
ui.showResultToUser(result);
Expand Down
1 change: 1 addition & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public CommandResult execute() {
+ "\n" + ViewAllCommand.MESSAGE_USAGE
+ "\n" + HelpCommand.MESSAGE_USAGE
+ "\n" + ExitCommand.MESSAGE_USAGE
+ "\n" + HistoryCommand.MESSAGE_USAGE
);
}
}
29 changes: 29 additions & 0 deletions src/seedu/addressbook/commands/HistoryCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.addressbook.commands;


import java.util.Stack;

//import static seedu.addressbook.Main.CommandHistory;

/**
* Displays all the past commands inputted by the user.
*/
public class HistoryCommand extends Command {

public static final String COMMAND_WORD = "history";
public static final String MESSAGE_USAGE = "Shows all the commands history in reverse chronological order.\n"
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "Displaying all past commands!";

public static Stack<String> CommandHistory;

@Override
public CommandResult execute() {
Stack<String> CHCopy = (Stack<String>) CommandHistory.clone();
while (!CHCopy.empty()) {
System.out.println("|| " + CHCopy.pop());
}
return new CommandResult(MESSAGE_SUCCESS);
}
}
13 changes: 8 additions & 5 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import static seedu.addressbook.common.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.addressbook.common.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -22,6 +18,7 @@
import seedu.addressbook.commands.ListCommand;
import seedu.addressbook.commands.ViewAllCommand;
import seedu.addressbook.commands.ViewCommand;
import seedu.addressbook.commands.HistoryCommand;
import seedu.addressbook.data.exception.IllegalValueException;

/**
Expand All @@ -41,6 +38,7 @@ public class Parser {
+ " (?<isAddressPrivate>p?)a/(?<address>[^/]+)"
+ "(?<tagArguments>(?: t/[^/]+)*)"); // variable number of tags

public static Stack<String> CHStack;

/**
* Signals that the user input could not be parsed.
Expand Down Expand Up @@ -73,6 +71,11 @@ public Command parseCommand(String userInput) {

switch (commandWord) {

case HistoryCommand.COMMAND_WORD:
HistoryCommand hcexecute = new HistoryCommand();
hcexecute.CommandHistory = CHStack;
return hcexecute;

case AddCommand.COMMAND_WORD:
return prepareAdd(arguments);

Expand Down
7 changes: 7 additions & 0 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
|| Example: help
|| exit: Exits the program.
|| Example: exit
|| Shows all the commands history in reverse chronological order.
|| Example: history
|| ===================================================
|| Enter command: || [Command entered: history]
|| history
|| sfdfd
|| Displaying all past commands!
|| ===================================================
|| Enter command: || [Command entered: delete 1]
|| The person index provided is invalid
Expand Down
1 change: 1 addition & 0 deletions test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# should recognise invalid command
sfdfd
history

##########################################################
# commands using list index before any listing created
Expand Down