From d954c9f1379691df9d8839130e3304bb159a7eef Mon Sep 17 00:00:00 2001 From: Chen Lei Yu <128605764+ChenLeiyu@users.noreply.github.com> Date: Fri, 1 Nov 2024 18:07:15 +0800 Subject: [PATCH 1/7] Fix undo bug for "add" command --- src/main/java/seedu/address/logic/commands/AddCommand.java | 4 ++++ .../seedu/address/logic/commands/FavouriteGameCommand.java | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/logic/commands/AddCommand.java b/src/main/java/seedu/address/logic/commands/AddCommand.java index 4ee4be17a5c..75c0818a551 100644 --- a/src/main/java/seedu/address/logic/commands/AddCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddCommand.java @@ -10,6 +10,7 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; import java.util.List; +import java.util.function.Predicate; import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; @@ -47,6 +48,7 @@ public class AddCommand extends Command { public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book"; private final Person toAdd; + private Predicate previousPredicate; /** * Creates an AddCommand to add the specified {@code Person} @@ -64,6 +66,7 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_DUPLICATE_PERSON); } + previousPredicate = model.getCurrentPredicate(); model.addPerson(toAdd); model.addCommandToLog(this); return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd))); @@ -76,6 +79,7 @@ public void undo(Model model) { Person personToDelete = lastShownList.get(lastShownList.size() - 1); model.deletePerson(personToDelete); + model.updateFilteredPersonList(previousPredicate); } @Override diff --git a/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java b/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java index 4157fa7a8e0..9b9a351a6d1 100644 --- a/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java +++ b/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java @@ -67,7 +67,7 @@ public CommandResult execute(Model model) throws CommandException { targetGame.setAsFavourite(); model.setPerson(targetPerson, targetPerson); model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); - + model.addCommandToLog(this); return new CommandResult(String.format(MESSAGE_FAVOURITE_GAME_SUCCESS, gameName)); } From e901b45f99a986e0cbdad57ba80910e913175b9d Mon Sep 17 00:00:00 2001 From: Chen Lei Yu <128605764+ChenLeiyu@users.noreply.github.com> Date: Fri, 1 Nov 2024 18:09:55 +0800 Subject: [PATCH 2/7] Fix fav and unfav resetting filtered list --- .../java/seedu/address/logic/commands/FavouriteGameCommand.java | 1 - .../seedu/address/logic/commands/UnfavouriteGameCommand.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java b/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java index 9b9a351a6d1..91dbe51fc10 100644 --- a/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java +++ b/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java @@ -66,7 +66,6 @@ public CommandResult execute(Model model) throws CommandException { targetGame.setAsFavourite(); model.setPerson(targetPerson, targetPerson); - model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); model.addCommandToLog(this); return new CommandResult(String.format(MESSAGE_FAVOURITE_GAME_SUCCESS, gameName)); } diff --git a/src/main/java/seedu/address/logic/commands/UnfavouriteGameCommand.java b/src/main/java/seedu/address/logic/commands/UnfavouriteGameCommand.java index 8694a2e74ac..cae462d486e 100644 --- a/src/main/java/seedu/address/logic/commands/UnfavouriteGameCommand.java +++ b/src/main/java/seedu/address/logic/commands/UnfavouriteGameCommand.java @@ -66,7 +66,6 @@ public CommandResult execute(Model model) throws CommandException { targetGame.removeFavourite(); model.setPerson(targetPerson, targetPerson); - model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); return new CommandResult(String.format(MESSAGE_UNFAVOURITE_GAME_SUCCESS, gameName)); } From 5370d5b16861814a8ac3b24da433b36b1d3fe136 Mon Sep 17 00:00:00 2001 From: Chen Lei Yu <128605764+ChenLeiyu@users.noreply.github.com> Date: Fri, 1 Nov 2024 18:28:23 +0800 Subject: [PATCH 3/7] Add undo functionality to "favgame" --- .../logic/commands/FavouriteGameCommand.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java b/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java index 91dbe51fc10..60ce4d2535c 100644 --- a/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java +++ b/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java @@ -34,6 +34,7 @@ public class FavouriteGameCommand extends Command { private Index index; private String gameName; + private boolean prevGameIsFavourite; /** * @param index of the person in the filtered person list to edit @@ -63,7 +64,7 @@ public CommandResult execute(Model model) throws CommandException { if (targetGame == null) { throw new CommandException(MESSAGE_GAME_NOT_FOUND); } - + prevGameIsFavourite = targetGame.getFavouriteStatus(); targetGame.setAsFavourite(); model.setPerson(targetPerson, targetPerson); model.addCommandToLog(this); @@ -71,7 +72,22 @@ public CommandResult execute(Model model) throws CommandException { } public void undo(Model model) { + requireNonNull(model); + List lastShownList = model.getFilteredPersonList(); + assert index.getZeroBased() < lastShownList.size() : "Index should still be within bounds when undoing"; + assert !gameName.isEmpty() : "Game name should not be empty when undoing"; + + Person targetPerson = lastShownList.get(index.getZeroBased()); + Game targetGame = targetPerson.getGames().get(gameName); + + assert targetGame != null : "Game should be present when undoing"; + + if (!prevGameIsFavourite) { + targetGame.removeFavourite(); + } + + model.setPerson(targetPerson, targetPerson); } @Override From f5e46bb10bbd550b438d7d5673e4114527fe908c Mon Sep 17 00:00:00 2001 From: Chen Lei Yu <128605764+ChenLeiyu@users.noreply.github.com> Date: Fri, 1 Nov 2024 18:33:03 +0800 Subject: [PATCH 4/7] Add undo functionality for "unfavgame" --- .../commands/UnfavouriteGameCommand.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/UnfavouriteGameCommand.java b/src/main/java/seedu/address/logic/commands/UnfavouriteGameCommand.java index cae462d486e..08a32fb31c2 100644 --- a/src/main/java/seedu/address/logic/commands/UnfavouriteGameCommand.java +++ b/src/main/java/seedu/address/logic/commands/UnfavouriteGameCommand.java @@ -35,6 +35,7 @@ public class UnfavouriteGameCommand extends Command { private Index index; private String gameName; + private boolean prevGameIsFavourite; /** * @param index of the person in the filtered person list to edit @@ -44,6 +45,7 @@ public UnfavouriteGameCommand(Index index, String gameName) { this.index = index; this.gameName = gameName; } + @Override public CommandResult execute(Model model) throws CommandException { requireNonNull(model); @@ -63,15 +65,44 @@ public CommandResult execute(Model model) throws CommandException { if (targetGame == null) { throw new CommandException(MESSAGE_GAME_NOT_FOUND); } - + prevGameIsFavourite = targetGame.getFavouriteStatus(); targetGame.removeFavourite(); model.setPerson(targetPerson, targetPerson); - + model.addCommandToLog(this); return new CommandResult(String.format(MESSAGE_UNFAVOURITE_GAME_SUCCESS, gameName)); } @Override public void undo(Model model) { + requireNonNull(model); + List lastShownList = model.getFilteredPersonList(); + + assert index.getZeroBased() < lastShownList.size() : "Index should still be within bounds when undoing"; + assert !gameName.isEmpty() : "Game name should not be empty when undoing"; + + Person targetPerson = lastShownList.get(index.getZeroBased()); + Game targetGame = targetPerson.getGames().get(gameName); + + assert targetGame != null : "Game should be present when undoing"; + + if (prevGameIsFavourite) { + targetGame.setAsFavourite(); + } + + model.setPerson(targetPerson, targetPerson); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + if (!(other instanceof UnfavouriteGameCommand)) { + return false; + } + UnfavouriteGameCommand e = (UnfavouriteGameCommand) other; + return index.equals(e.index) && gameName.equals(e.gameName); } } From 0f179389b8bee92bc2fe80bf6ac63d4d26628eec Mon Sep 17 00:00:00 2001 From: Chen Lei Yu <128605764+ChenLeiyu@users.noreply.github.com> Date: Fri, 1 Nov 2024 19:06:46 +0800 Subject: [PATCH 5/7] Add undo functionality for "findtime" --- .../address/logic/commands/FindTimeCommand.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/FindTimeCommand.java b/src/main/java/seedu/address/logic/commands/FindTimeCommand.java index 294dc0ec499..28c09a3ef6a 100644 --- a/src/main/java/seedu/address/logic/commands/FindTimeCommand.java +++ b/src/main/java/seedu/address/logic/commands/FindTimeCommand.java @@ -2,9 +2,12 @@ import static java.util.Objects.requireNonNull; +import java.util.function.Predicate; + import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; import seedu.address.model.Model; +import seedu.address.model.person.Person; import seedu.address.model.preferredtime.PreferredTimeOverlapsRangesPredicate; @@ -22,6 +25,7 @@ public class FindTimeCommand extends Command { + "Example: " + COMMAND_WORD + " 1100-1230 2130-2245"; private final PreferredTimeOverlapsRangesPredicate predicate; + private Predicate previousPredicate; public FindTimeCommand(PreferredTimeOverlapsRangesPredicate predicate) { this.predicate = predicate; @@ -30,11 +34,19 @@ public FindTimeCommand(PreferredTimeOverlapsRangesPredicate predicate) { @Override 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())); } + @Override + public void undo(Model model) { + requireNonNull(model); + model.updateFilteredPersonList(previousPredicate); + } + @Override public boolean equals(Object other) { if (other == this) { @@ -50,11 +62,6 @@ public boolean equals(Object other) { return predicate.equals(otherFindTimeCommand.predicate); } - @Override - public void undo(Model model) { - - } - @Override public String toString() { return new ToStringBuilder(this) From 5e6ed39cf4bbb2bea3edcdf8ddfd0aba844676d6 Mon Sep 17 00:00:00 2001 From: Chen Lei Yu <128605764+ChenLeiyu@users.noreply.github.com> Date: Fri, 1 Nov 2024 19:12:45 +0800 Subject: [PATCH 6/7] Fix code style problems --- .../java/seedu/address/logic/commands/FavouriteGameCommand.java | 2 +- .../seedu/address/logic/commands/UnfavouriteGameCommand.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java b/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java index 60ce4d2535c..e440e9a21f5 100644 --- a/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java +++ b/src/main/java/seedu/address/logic/commands/FavouriteGameCommand.java @@ -2,7 +2,6 @@ import static java.util.Objects.requireNonNull; import static seedu.address.logic.parser.CliSyntax.PREFIX_GAME; -import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; import java.util.List; @@ -71,6 +70,7 @@ public CommandResult execute(Model model) throws CommandException { return new CommandResult(String.format(MESSAGE_FAVOURITE_GAME_SUCCESS, gameName)); } + @Override public void undo(Model model) { requireNonNull(model); List lastShownList = model.getFilteredPersonList(); diff --git a/src/main/java/seedu/address/logic/commands/UnfavouriteGameCommand.java b/src/main/java/seedu/address/logic/commands/UnfavouriteGameCommand.java index 08a32fb31c2..f5baa5859c0 100644 --- a/src/main/java/seedu/address/logic/commands/UnfavouriteGameCommand.java +++ b/src/main/java/seedu/address/logic/commands/UnfavouriteGameCommand.java @@ -2,7 +2,6 @@ import static java.util.Objects.requireNonNull; import static seedu.address.logic.parser.CliSyntax.PREFIX_GAME; -import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; import java.util.List; From c22e5ada5f04f2a2984025ab074b878d533e0f12 Mon Sep 17 00:00:00 2001 From: Chen Lei Yu <128605764+ChenLeiyu@users.noreply.github.com> Date: Fri, 1 Nov 2024 19:18:24 +0800 Subject: [PATCH 7/7] Fix addCommand test using new method --- .../java/seedu/address/logic/commands/AddCommandTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 06ff18c3618..8dd7722ab75 100644 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -225,6 +225,11 @@ public void addCommandToLog(Command command) { } + @Override + public Predicate getCurrentPredicate() { + return null; + } + @Override public ReadOnlyAddressBook getAddressBook() { return new AddressBook();