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] [T12-4] Jeremy Yiren Low #171

Open
wants to merge 6 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
Binary file added lib/hamcrest-core-1.3.jar
Binary file not shown.
Binary file added lib/junit-4.12.jar
Binary file not shown.
Binary file not shown.
11 changes: 10 additions & 1 deletion src/seedu/addressbook/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import seedu.addressbook.commands.Command;
import seedu.addressbook.commands.CommandResult;
import seedu.addressbook.commands.ExitCommand;
import seedu.addressbook.common.Messages;
import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.parser.Parser;
Expand All @@ -32,6 +33,8 @@ public class Main {
/** The list of person shown to the user most recently. */
private List<? extends ReadOnlyPerson> lastShownList = Collections.emptyList();

/** The previous command entered by user. */
private String PreviousUserCommand = Messages.HISTORY_EMPTY;

Choose a reason for hiding this comment

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

Don't forget to follow camelCase!


public static void main(String... launchArgs) {
new Main().run(launchArgs);
Expand All @@ -50,6 +53,7 @@ public void run(String[] launchArgs) {
* @param launchArgs arguments supplied by the user at program launch
*
*/

private void start(String[] launchArgs) {
try {
this.ui = new TextUi();
Expand Down Expand Up @@ -83,9 +87,14 @@ private void runCommandLoopUntilExitCommand() {
Command command;
do {
String userCommandText = ui.getUserCommand();
command = new Parser().parseCommand(userCommandText);
command = new Parser().parseCommand(userCommandText, PreviousUserCommand);
CommandResult result = executeCommand(command);
recordResult(result);
if(result.isExecuted){
PreviousUserCommand = userCommandText;
} else {
PreviousUserCommand = Messages.HISTORY_INVALID;
}
ui.showResultToUser(result);

} while (!ExitCommand.isExit(command));
Expand Down
4 changes: 2 additions & 2 deletions src/seedu/addressbook/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public ReadOnlyPerson getPerson() {
public CommandResult execute() {
try {
addressBook.addPerson(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd), true);
} catch (UniquePersonList.DuplicatePersonException dpe) {
return new CommandResult(MESSAGE_DUPLICATE_PERSON);
return new CommandResult(MESSAGE_DUPLICATE_PERSON, false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/seedu/addressbook/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class ClearCommand extends Command {
@Override
public CommandResult execute() {
addressBook.clear();
return new CommandResult(MESSAGE_SUCCESS);
return new CommandResult(MESSAGE_SUCCESS, true);
}
}
9 changes: 7 additions & 2 deletions src/seedu/addressbook/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ public class CommandResult {
/** The feedback message to be shown to the user. Contains a description of the execution result */
public final String feedbackToUser;

/** The feedback message to be shown to the user. Contains a description of the execution result */
public final boolean isExecuted;

/** The list of persons that was produced by the command */
private final List<? extends ReadOnlyPerson> relevantPersons;

public CommandResult(String feedbackToUser) {
public CommandResult(String feedbackToUser, boolean tag) {
this.feedbackToUser = feedbackToUser;
isExecuted = tag;
relevantPersons = null;
}

public CommandResult(String feedbackToUser, List<? extends ReadOnlyPerson> relevantPersons) {
public CommandResult(String feedbackToUser, boolean tag, List<? extends ReadOnlyPerson> relevantPersons) {
this.feedbackToUser = feedbackToUser;
isExecuted = tag;
this.relevantPersons = relevantPersons;
}

Expand Down
8 changes: 5 additions & 3 deletions src/seedu/addressbook/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.addressbook.commands;

import seedu.addressbook.common.Messages;
import seedu.addressbook.data.person.Person;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException;

Expand Down Expand Up @@ -29,13 +30,14 @@ public DeleteCommand(int targetVisibleIndex) {
public CommandResult execute() {
try {
final ReadOnlyPerson target = getTargetPerson();
addressBook.sendToBin((Person)target);
addressBook.removePerson(target);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, target));
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, target), true);

} catch (IndexOutOfBoundsException ie) {
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX, false);
} catch (PersonNotFoundException pnfe) {
return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK);
return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK, false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/seedu/addressbook/commands/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ExitCommand extends Command {

@Override
public CommandResult execute() {
return new CommandResult(MESSAGE_EXIT_ACKNOWEDGEMENT);
return new CommandResult(MESSAGE_EXIT_ACKNOWEDGEMENT, true);
}

public static boolean isExit(Command command) {
Expand Down
2 changes: 1 addition & 1 deletion src/seedu/addressbook/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Set<String> getKeywords() {
@Override
public CommandResult execute() {
final List<ReadOnlyPerson> personsFound = getPersonsWithNameContainingAnyKeyword(keywords);
return new CommandResult(getMessageForPersonListShownSummary(personsFound), personsFound);
return new CommandResult(getMessageForPersonListShownSummary(personsFound), true, personsFound);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ public CommandResult execute() {
+ "\n" + ClearCommand.MESSAGE_USAGE
+ "\n" + FindCommand.MESSAGE_USAGE
+ "\n" + ListCommand.MESSAGE_USAGE
+ "\n" + UndoCommand.MESSAGE_USAGE
+ "\n" + ViewCommand.MESSAGE_USAGE
+ "\n" + ViewAllCommand.MESSAGE_USAGE
+ "\n" + HelpCommand.MESSAGE_USAGE
+ "\n" + ExitCommand.MESSAGE_USAGE
);
, true);

Choose a reason for hiding this comment

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

Note that you should break after a comma and that this line should be indented as well.

}
}
2 changes: 1 addition & 1 deletion src/seedu/addressbook/commands/IncorrectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public IncorrectCommand(String feedbackToUser) {

@Override
public CommandResult execute() {
return new CommandResult(feedbackToUser);
return new CommandResult(feedbackToUser, false);
}

}
2 changes: 1 addition & 1 deletion src/seedu/addressbook/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public class ListCommand extends Command {
@Override
public CommandResult execute() {
List<ReadOnlyPerson> allPersons = addressBook.getAllPersons().immutableListView();
return new CommandResult(getMessageForPersonListShownSummary(allPersons), allPersons);
return new CommandResult(getMessageForPersonListShownSummary(allPersons), true, allPersons);
}
}
47 changes: 47 additions & 0 deletions src/seedu/addressbook/commands/UndoCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package seedu.addressbook.commands;

import seedu.addressbook.common.Messages;
import seedu.addressbook.data.person.UniquePersonList;

/**
* Clears the address book.
*/
public class UndoCommand extends Command {

public static final String COMMAND_WORD = "undo";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Reverts effect of previous command. "
+ "Only applicable to add, delete commands.\n"
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "Undo successful. Command reverted: ";
public static final String MESSAGE_INVALID = "Undo cannot be performed on this command: ";

private final String LAST_COMMAND;

public UndoCommand(String cmd){
this.LAST_COMMAND = cmd;
}

@Override
public CommandResult execute(){
String[] command = LAST_COMMAND.split(" ");
if(command[0].equals(AddCommand.COMMAND_WORD)){
try {
addressBook.removeLastPerson();
}
catch (UniquePersonList.PersonNotFoundException pnfe) {

Choose a reason for hiding this comment

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

Catch statements should be on the same line as the closed brace of the previous try/catch closed brace.

return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK, true);
}
return new CommandResult(MESSAGE_SUCCESS + LAST_COMMAND, true);
} else if(command[0].equals(DeleteCommand.COMMAND_WORD)) {
try {
addressBook.addBackDeletedPerson();
} catch (UniquePersonList.DuplicatePersonException dpe){
return new CommandResult(AddCommand.MESSAGE_DUPLICATE_PERSON, false);
}
return new CommandResult(MESSAGE_SUCCESS + LAST_COMMAND, true);
} else {
return new CommandResult(MESSAGE_INVALID + LAST_COMMAND, true);
}
}
}
6 changes: 3 additions & 3 deletions src/seedu/addressbook/commands/ViewAllCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public CommandResult execute() {
try {
final ReadOnlyPerson target = getTargetPerson();
if (!addressBook.containsPerson(target)) {
return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK);
return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK, true);
}
return new CommandResult(String.format(MESSAGE_VIEW_PERSON_DETAILS, target.getAsTextShowAll()));
return new CommandResult(String.format(MESSAGE_VIEW_PERSON_DETAILS, target.getAsTextShowAll()), true);
} catch (IndexOutOfBoundsException ie) {
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX, true);

Choose a reason for hiding this comment

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

Shouldn't this return false as the second parameter?

}
}
}
6 changes: 3 additions & 3 deletions src/seedu/addressbook/commands/ViewCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public CommandResult execute() {
try {
final ReadOnlyPerson target = getTargetPerson();
if (!addressBook.containsPerson(target)) {
return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK);
return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK, true);
}
return new CommandResult(String.format(MESSAGE_VIEW_PERSON_DETAILS, target.getAsTextHidePrivate()));
return new CommandResult(String.format(MESSAGE_VIEW_PERSON_DETAILS, target.getAsTextHidePrivate()), true);
} catch (IndexOutOfBoundsException ie) {
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX, true);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/seedu/addressbook/common/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public class Messages {
"java seedu.addressbook.Main [STORAGE_FILE_PATH]";
public static final String MESSAGE_WELCOME = "Welcome to your Address Book!";
public static final String MESSAGE_USING_STORAGE_FILE = "Using storage file : %1$s";
public static final String HISTORY_EMPTY = "NO COMMAND HISTORY.";
public static final String HISTORY_INVALID = "PREVIOUS COMMAND IS INVALID. PLEASE ENTER NEW COMMAND.";
}
26 changes: 26 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import seedu.addressbook.data.person.UniquePersonList.DuplicatePersonException;
import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException;

import java.util.Iterator;
import java.util.Optional;

/**
* Represents the entire address book. Contains the data of the address book.
*/
public class AddressBook {

private final UniquePersonList allPersons;
private Optional<Person> deletedPerson = Optional.empty();

/**
* Creates an empty address book.
Expand Down Expand Up @@ -61,13 +65,35 @@ public void clear() {
allPersons.clear();
}

/**
* Clears the most recently added person from addressbook.
*/
public void removeLastPerson() throws PersonNotFoundException {
Iterator<Person> itr = allPersons.iterator();
assert itr.hasNext();

Choose a reason for hiding this comment

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

Great use of assertions here to verify the program's state!

Person p = itr.next();
while(itr.hasNext()){
p = itr.next();
}
this.removePerson(p);
}

/**
* Returns a new UniquePersonList of all persons in the address book at the time of the call.
*/
public UniquePersonList getAllPersons() {
return new UniquePersonList(allPersons);
}

public void sendToBin(Person target) {
deletedPerson = Optional.of(target);
}

public void addBackDeletedPerson() throws DuplicatePersonException{
assert deletedPerson.isPresent();
allPersons.add(deletedPerson.get());
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
25 changes: 7 additions & 18 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,11 @@
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.*;

Choose a reason for hiding this comment

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

Imported classes should always be listed explicitly.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import seedu.addressbook.commands.AddCommand;
import seedu.addressbook.commands.ClearCommand;
import seedu.addressbook.commands.Command;
import seedu.addressbook.commands.DeleteCommand;
import seedu.addressbook.commands.ExitCommand;
import seedu.addressbook.commands.FindCommand;
import seedu.addressbook.commands.HelpCommand;
import seedu.addressbook.commands.IncorrectCommand;
import seedu.addressbook.commands.ListCommand;
import seedu.addressbook.commands.ViewAllCommand;
import seedu.addressbook.commands.ViewCommand;
import seedu.addressbook.commands.*;

Choose a reason for hiding this comment

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

Imported classes should always be listed explicitly.

import seedu.addressbook.data.exception.IllegalValueException;

/**
Expand Down Expand Up @@ -62,12 +48,11 @@ public static class ParseException extends Exception {
* @param userInput full user input string
* @return the command based on the user input
*/
public Command parseCommand(String userInput) {
public Command parseCommand(String userInput, String previousInput) {
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim());
if (!matcher.matches()) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE));
}

final String commandWord = matcher.group("commandWord");
final String arguments = matcher.group("arguments");

Expand All @@ -88,6 +73,9 @@ public Command parseCommand(String userInput) {
case ListCommand.COMMAND_WORD:
return new ListCommand();

case UndoCommand.COMMAND_WORD:
return new UndoCommand(previousInput.trim());

case ViewCommand.COMMAND_WORD:
return prepareView(arguments);

Expand All @@ -101,6 +89,7 @@ public Command parseCommand(String userInput) {
default:
return new HelpCommand();
}

}

/**
Expand Down
Loading