Skip to content

Commit

Permalink
Merge pull request #97 from TyrusLye/undo-command
Browse files Browse the repository at this point in the history
Undo command
  • Loading branch information
butteredyakiimo authored Nov 2, 2023
2 parents 566d12a + c63bc3c commit fe0efc1
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 15 deletions.
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
command = addressBookParser.parseCommand(commandText);
//checking for undo command
if (command instanceof UndoCommand) {
model.setAddressBook(backupModel.getAddressBook());
if (backupModel.getAddressBook().equals(model.getAddressBook())) {
commandResult = command.execute(null);
return commandResult;
} else {
model.setAddressBook(backupModel.getAddressBook());
}
} else {
backupModel.setAddressBook(model.getAddressBook());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class ClearCommand extends Command {

public static final String COMMAND_WORD = "reset";
public static final String MESSAGE_SUCCESS = "Address book has been cleared!";
public static final String MESSAGE_USAGE = "This command will erase the entire Address Book\n"
public static final String MESSAGE_USAGE = "This command will erase the entire Address Book.\n"
+ "Are you sure you want to do this? "
+ "If you are sure, Enter 'reset confirm'";
+ "If you are sure, Enter 'reset confirm'.";

private final String confirmation;

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/seedu/address/logic/commands/UndoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
*/
public class UndoCommand extends Command {
public static final String COMMAND_WORD = "undo";
public static final String MESSAGE_SUCCESS = "Undo Completed";
public static final String MESSAGE_USAGE = "This command will undo the last command";
public static final String MESSAGE_SUCCESS = "Undo completed";
public static final String MESSAGE_USAGE = "This command will undo the last command.";
public static final String MESSAGE_UNDO_DONE = "No more undo history found!";


@Override
public CommandResult execute(Model model) {
if (model == null) {
return new CommandResult(MESSAGE_UNDO_DONE);
}
return new CommandResult(MESSAGE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand All @@ -19,18 +17,18 @@ public class ClearCommandParser {
public ClearCommand parse(String args) throws ParseException {
if (args.trim().isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ClearCommand.MESSAGE_USAGE));
String.format(ClearCommand.MESSAGE_USAGE));
}
try {
String confirmation = ParserUtil.parseSimpleString(args);
if (!confirmation.equals("confirm")) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ClearCommand.MESSAGE_USAGE));
String.format(ClearCommand.MESSAGE_USAGE));
}
return new ClearCommand(confirmation);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ClearCommand.MESSAGE_USAGE), pe);
String.format(ClearCommand.MESSAGE_USAGE), pe);
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,20 @@ void handleViewExit() {
commandBox.setInConfirmationDialog(true);
}
}
/*
void handleConfirm() {
if (commandBox.getInConfirmationDialog()) {
commandBox.setInConfirmationDialog(false);
personListPanelPlaceholder.setVisible(true);
personProfilePlaceholder.getChildren().remove(personProfile.getRoot());
personProfilePlaceholder.setVisible(false);
sendFeedback("Exiting view as requested.");
} else {
commandBox.setInConfirmationDialog(true);
}
}
*/

void handleCancelViewExit() {
sendFeedback("Cancelled exit.");
Expand All @@ -249,6 +263,12 @@ private CommandResult executeCommand(String commandText) throws CommandException
if (commandResult.getCommandType() == CommandType.HELP) {
handleHelp();
}
/*
if (commandResult.getCommandType() == CommandType.CLEAR) {
handleConfirm();
}
*/

if (commandResult.getCommandType() == CommandType.EXIT) {
handleExit();
Expand Down Expand Up @@ -317,6 +337,8 @@ private CommandResult executeCommand(String commandText) throws CommandException
}
}



protected void sendFeedback(String feedback) {
resultDisplay.setFeedbackToUser(feedback);
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.exceptions.DataLoadingException;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.CommandResult;
Expand Down Expand Up @@ -163,6 +164,29 @@ public void execute_nonUndoCommand_success() throws CommandException, ParseExcep
assertEquals(model.getAddressBook(), expectedModel.getAddressBook());
}

@Test
public void execute_undoCommandWithNullModel_success() throws CommandException, ParseException,
DataLoadingException, IOException {
String undoCommand = "undo";
CommandResult undoResult = logic.execute(undoCommand);

assertEquals(UndoCommand.MESSAGE_UNDO_DONE, undoResult.getFeedbackToUser());
assertEquals(backupModel, model);
}

@Test
public void setGuiSettings_validGuiSettings_success() {
GuiSettings guiSettings = new GuiSettings(800, 600, 2, 2);

logic.setGuiSettings(guiSettings);

GuiSettings updatedGuiSettings = model.getGuiSettings();

assertEquals(guiSettings, updatedGuiSettings);
}






Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ public void execute_confirmClear_success() {
Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());
expectedModel.setAddressBook(new AddressBook());

CommandResult expectedCommandResult = new CommandResult(ClearCommand.MESSAGE_SUCCESS);

assertCommandSuccess(new ClearCommand("confirm"), model, expectedCommandResult, expectedModel);
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/java/seedu/address/logic/commands/UndoCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ public void execute_undoCommand_success() {

assertEquals(expectedModel, model);
}

@Test
public void execute_undoCommandNullModel_success() {
UndoCommand undoCommand = new UndoCommand();

// Set the model to null
model = null;

CommandResult commandResult = undoCommand.execute(model);

assertEquals(UndoCommand.MESSAGE_UNDO_DONE, commandResult.getFeedbackToUser());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import org.junit.jupiter.api.Test;

Expand All @@ -23,15 +22,15 @@ public void parse_validArgs_returnsClearCommand() throws ParseException {
@Test
public void parse_emptyArgs_throwsParseException() {
String emptyArgs = "";
String expectedErrorMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, ClearCommand.MESSAGE_USAGE);
String expectedErrorMessage = String.format(ClearCommand.MESSAGE_USAGE);
ParseException exception = assertThrows(ParseException.class, () -> parser.parse(emptyArgs));
assertEquals(expectedErrorMessage, exception.getMessage());
}

@Test
public void parse_invalidArgs_throwsParseException() {
String invalidArgs = "invalid confirmation";
String expectedErrorMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, ClearCommand.MESSAGE_USAGE);
String expectedErrorMessage = String.format(ClearCommand.MESSAGE_USAGE);
ParseException exception = assertThrows(ParseException.class, () -> parser.parse(invalidArgs));
assertEquals(expectedErrorMessage, exception.getMessage());
}
Expand Down

0 comments on commit fe0efc1

Please sign in to comment.