Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2103T-T12-4#208
Browse files Browse the repository at this point in the history
Add undo functionality for 'FavouriteGame', 'UnfavouriteGame', and 'FindTime' commands
  • Loading branch information
JJtan2002 authored Nov 1, 2024
2 parents 84179d5 + c22e5ad commit a22d2d3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Person> previousPredicate;

/**
* Creates an AddCommand to add the specified {@code Person}
Expand All @@ -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)));
Expand All @@ -76,6 +79,7 @@ public void undo(Model model) {

Person personToDelete = lastShownList.get(lastShownList.size() - 1);
model.deletePerson(personToDelete);
model.updateFilteredPersonList(previousPredicate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -34,6 +33,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
Expand Down Expand Up @@ -63,16 +63,31 @@ 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.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

model.addCommandToLog(this);
return new CommandResult(String.format(MESSAGE_FAVOURITE_GAME_SUCCESS, gameName));
}

@Override
public void undo(Model model) {
requireNonNull(model);
List<Person> 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
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/seedu/address/logic/commands/FindTimeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -22,6 +25,7 @@ public class FindTimeCommand extends Command {
+ "Example: " + COMMAND_WORD + " 1100-1230 2130-2245";

private final PreferredTimeOverlapsRangesPredicate predicate;
private Predicate<Person> previousPredicate;

public FindTimeCommand(PreferredTimeOverlapsRangesPredicate predicate) {
this.predicate = predicate;
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -35,6 +34,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
Expand All @@ -44,6 +44,7 @@ public UnfavouriteGameCommand(Index index, String gameName) {
this.index = index;
this.gameName = gameName;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
Expand All @@ -63,16 +64,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.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

model.addCommandToLog(this);
return new CommandResult(String.format(MESSAGE_UNFAVOURITE_GAME_SUCCESS, gameName));
}

@Override
public void undo(Model model) {
requireNonNull(model);
List<Person> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ public void addCommandToLog(Command command) {

}

@Override
public Predicate<Person> getCurrentPredicate() {
return null;
}

@Override
public ReadOnlyAddressBook getAddressBook() {
return new AddressBook();
Expand Down

0 comments on commit a22d2d3

Please sign in to comment.