Skip to content

Commit

Permalink
Merge branch 'AY2425S1-CS2103T-T12-4:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
JJtan2002 authored Nov 7, 2024
2 parents abd1949 + 287a054 commit 92457e1
Show file tree
Hide file tree
Showing 27 changed files with 199 additions and 30 deletions.
28 changes: 24 additions & 4 deletions src/main/java/seedu/address/logic/CommandLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,40 @@
import seedu.address.logic.commands.Command;

/**
* Keeps track of Commands given by user
* Keeps track of Commands given by user using a stack
*/
public class CommandLog {
private Stack<Command> log = new Stack<>();
private Stack<String> inputLog = new Stack<>();

/**
* Adds a command to the stack
*/
public void add(Command command) {
log.push(command);
}

public Command undo() {
/**
* Pops most recent command at the top of the stack
* @return null if stack is empty
*/
public Command getPreviousCommand() {
return log.isEmpty() ? null : log.pop();
}

public boolean hasLog() {
return !log.isEmpty();
/**
* Adds a user input to the stack
*/
public void addinput(String input) {
inputLog.push(input);
}

/**
* Returns most recent user input at the top of the stack
* @return empty string if stack is empty
*/
public String getPreviousInput() {
return inputLog.isEmpty() ? "" : inputLog.pop();
}

}
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
CommandResult commandResult;
Command command = addressBookParser.parseCommand(commandText);
commandResult = command.execute(model);
if (command.canBeUndone()) {
model.addInputToLog(commandText);
model.addCommandToLog(command);
}

if (commandResult.isLoad()) {
try {
Optional<ReadOnlyAddressBook> loadedAddressBookOpt = storage.loadAddressBookManually();
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class AddCommand extends Command {

public static final String MESSAGE_SUCCESS = "New person added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";
private static final boolean IS_UNDOABLE = true;

private final Person toAdd;
private Predicate<Person> previousPredicate;
Expand All @@ -68,7 +69,6 @@ public CommandResult execute(Model model) throws CommandException {

previousPredicate = model.getCurrentPredicate();
model.addPerson(toAdd);
model.addCommandToLog(this);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)));
}

Expand All @@ -82,6 +82,11 @@ public void undo(Model model) {
model.updateFilteredPersonList(previousPredicate);
}

@Override
public boolean canBeUndone() {
return IS_UNDOABLE;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class AddGameCommand extends Command {

public static final String MESSAGE_ADD_GAME_SUCCESS = "Added Game to Person: %1$s";
public static final String MESSAGE_GAME_EXISTS = "The game provided already exists for that person.";
private static final boolean IS_UNDOABLE = true;

private final Index index;
private final GameDescriptor addGameDescriptor;
Expand Down Expand Up @@ -90,7 +91,6 @@ public CommandResult execute(Model model) throws CommandException {
}
gameMap.put(gameName, editedGame);
model.setPerson(personToEdit, personToEdit);
model.addCommandToLog(this);
return new CommandResult(String.format(MESSAGE_ADD_GAME_SUCCESS, Messages.format(editedGame)));
}

Expand All @@ -104,6 +104,11 @@ public void undo(Model model) {
model.setPerson(personToEdit, personToEdit);
}

@Override
public boolean canBeUndone() {
return IS_UNDOABLE;
}

/**
* Creates and returns a {@code Game} with the details of {@code addGameDescriptor}
*/
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class ClearCommand extends Command {

public static final String COMMAND_WORD = "clear";
public static final String MESSAGE_SUCCESS = "Address book has been cleared!";
private static final boolean IS_UNDOABLE = true;

private ReadOnlyAddressBook previousAddressBook;

Expand All @@ -22,12 +23,16 @@ public CommandResult execute(Model model) {
requireNonNull(model);
previousAddressBook = new AddressBook(model.getAddressBook());
model.setAddressBook(new AddressBook());
model.addCommandToLog(this);
return new CommandResult(MESSAGE_SUCCESS);
}

@Override
public void undo(Model model) {
model.setAddressBook(previousAddressBook);
}

@Override
public boolean canBeUndone() {
return IS_UNDOABLE;
}
}
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public abstract class Command {
*/
public abstract void undo(Model model);

/**
* Returns whether the command can be undone.
*/
public abstract boolean canBeUndone();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class DeleteCommand extends Command {
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";
private static final boolean IS_UNDOABLE = true;

private final Index targetIndex;

Expand All @@ -48,7 +49,6 @@ public CommandResult execute(Model model) throws CommandException {
model.getAddressBookIndex(targetIndex.getZeroBased()));
model.deletePerson(personToDelete);
deletedPerson = personToDelete;
model.addCommandToLog(this);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)));
}

Expand All @@ -60,6 +60,11 @@ public void undo(Model model) {
model.insertPerson(deletedPerson, deletedPersonIndex.getZeroBased());
}

@Override
public boolean canBeUndone() {
return IS_UNDOABLE;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class DeleteGameCommand extends Command {

public static final String MESSAGE_DELETE_GAME_SUCCESS = "Deleted Game from Person: %1$s";
public static final String MESSAGE_NOT_DELETED = "The game provided does not exist for that person.";
private static final boolean IS_UNDOABLE = true;

private final Index index;
private final String gameName;
Expand Down Expand Up @@ -69,7 +70,6 @@ public CommandResult execute(Model model) throws CommandException {

deletedGame = gameMap.remove(gameName);
model.setPerson(personToEdit, personToEdit);
model.addCommandToLog(this);
return new CommandResult(String.format(MESSAGE_DELETE_GAME_SUCCESS, Messages.format(deletedGame)));
}

Expand All @@ -83,6 +83,10 @@ public void undo(Model model) {
model.setPerson(personToEdit, personToEdit);
}

@Override
public boolean canBeUndone() {
return IS_UNDOABLE;
}

@Override
public boolean equals(Object other) {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PREFERREDTIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -59,6 +58,7 @@ public class EditCommand extends Command {
public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";
private static final boolean IS_UNDOABLE = true;

private final Index index;
private final EditPersonDescriptor editPersonDescriptor;
Expand Down Expand Up @@ -94,8 +94,6 @@ public CommandResult execute(Model model) throws CommandException {
}

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
model.addCommandToLog(this);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
}

Expand All @@ -104,7 +102,11 @@ public void undo(Model model) {
requireNonNull(model);

model.setPerson(editedPerson, personToEdit);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
}

@Override
public boolean canBeUndone() {
return IS_UNDOABLE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class EditGameCommand extends Command {

public static final String MESSAGE_EDIT_GAME_SUCCESS = "Edited Game: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
private static final boolean IS_UNDOABLE = true;

private final Index index;
private final String gameName;
Expand Down Expand Up @@ -86,7 +87,6 @@ public CommandResult execute(Model model) throws CommandException {
Game editedGame = createEditedGame(gameToEdit, editGameDescriptor);
gameMap.put(gameName, editedGame);
model.setPerson(personToEdit, personToEdit);
model.addCommandToLog(this);
return new CommandResult(String.format(MESSAGE_EDIT_GAME_SUCCESS, Messages.format(editedGame)));
}

Expand All @@ -98,6 +98,11 @@ public void undo(Model model) {
model.setPerson(personToEdit, personToEdit);
}

@Override
public boolean canBeUndone() {
return IS_UNDOABLE;
}

/**
* Creates and returns a {@code Game} with the details of {@code gameToEdit}
* edited with {@code editGameDescriptor}.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/commands/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ExitCommand extends Command {
public static final String COMMAND_WORD = "exit";

public static final String MESSAGE_EXIT_ACKNOWLEDGEMENT = "Exiting Address Book as requested ...";
private static final boolean IS_UNDOABLE = false;

@Override
public CommandResult execute(Model model) {
Expand All @@ -21,4 +22,8 @@ public void undo(Model model) {

}

@Override
public boolean canBeUndone() {
return IS_UNDOABLE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class FavouriteGameCommand extends Command {
public static final String MESSAGE_FAVOURITE_GAME_SUCCESS = "Favourited Game: %1$s";
public static final String MESSAGE_GAME_NOT_SPECIFIED = "Please specify a game!";
public static final String MESSAGE_GAME_NOT_FOUND = "Game not found!";
private static final boolean IS_UNDOABLE = true;

private Index index;
private String gameName;
Expand Down Expand Up @@ -66,7 +67,6 @@ public CommandResult execute(Model model) throws CommandException {
prevGameIsFavourite = targetGame.getFavouriteStatus();
targetGame.setAsFavourite();
model.setPerson(targetPerson, targetPerson);
model.addCommandToLog(this);
return new CommandResult(String.format(MESSAGE_FAVOURITE_GAME_SUCCESS, gameName));
}

Expand All @@ -90,6 +90,11 @@ public void undo(Model model) {
model.setPerson(targetPerson, targetPerson);
}

@Override
public boolean canBeUndone() {
return IS_UNDOABLE;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class FindCommand extends Command {
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n"
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " alice bob charlie";
private static final boolean IS_UNDOABLE = true;

private final NameContainsKeywordsPredicate predicate;
private Predicate<Person> previousPredicate;
Expand All @@ -35,7 +36,6 @@ public CommandResult execute(Model model) {
requireNonNull(model);
previousPredicate = model.getCurrentPredicate();
model.updateFilteredPersonList(predicate);
model.addCommandToLog(this);
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}
Expand All @@ -46,6 +46,11 @@ public void undo(Model model) {
model.updateFilteredPersonList(previousPredicate);
}

@Override
public boolean canBeUndone() {
return IS_UNDOABLE;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class FindTimeCommand extends Command {
+ "Tips: RANGE has format HHmm, same ending time as starting time is allowed.\n"
+ "Parameters: RANGE [MORE_RANGES]...\n"
+ "Example: " + COMMAND_WORD + " 1100-1230 2130-2245";
private static final boolean IS_UNDOABLE = true;

private final PreferredTimeOverlapsRangesPredicate predicate;
private Predicate<Person> previousPredicate;
Expand All @@ -37,7 +38,6 @@ public CommandResult execute(Model model) {
requireNonNull(model);
previousPredicate = model.getCurrentPredicate();
model.updateFilteredPersonList(predicate);
model.addCommandToLog(this);
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}
Expand All @@ -48,6 +48,11 @@ public void undo(Model model) {
model.updateFilteredPersonList(previousPredicate);
}

@Override
public boolean canBeUndone() {
return IS_UNDOABLE;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/logic/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class HelpCommand extends Command {
+ "Example: " + COMMAND_WORD;

public static final String SHOWING_HELP_MESSAGE = "Opened help window.";
private static final boolean IS_UNDOABLE = false;

@Override
public CommandResult execute(Model model) {
Expand All @@ -23,4 +24,9 @@ public CommandResult execute(Model model) {
public void undo(Model model) {

}

@Override
public boolean canBeUndone() {
return IS_UNDOABLE;
}
}
Loading

0 comments on commit 92457e1

Please sign in to comment.