-
Notifications
You must be signed in to change notification settings - Fork 113
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
base: master
Are you sure you want to change the base?
Changes from all commits
9de36bc
ddcb542
3519257
d78354f
54ea2c0
6f8884e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this return false as the second parameter? |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
/** | ||
|
@@ -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"); | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -101,6 +89,7 @@ public Command parseCommand(String userInput) { | |
default: | ||
return new HelpCommand(); | ||
} | ||
|
||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
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!