Skip to content

Commit

Permalink
Abstraction to check lastViewedPerson
Browse files Browse the repository at this point in the history
  • Loading branch information
potaotototo committed Oct 30, 2024
1 parent cf129f4 commit 1238ac3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
12 changes: 5 additions & 7 deletions src/main/java/tuteez/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ public CommandResult execute(Model model) throws CommandException {
personToDelete = getPersonToDeleteByName(model, targetName);
}

if (model.getLastViewedPerson().get().isPresent()) {
if (personToDelete.equals(model.getLastViewedPerson().get().get())) {
model.removeLastViewedPerson();
String logMessageForPerson =
String.format("Student on display is deleted, Student: %s", personToDelete);
logger.info(logMessageForPerson);
}
if (model.isSamePersonAsPersonOnDisplay(personToDelete)) {
model.removeLastViewedPerson();
String logMessageForPerson =
String.format("Student on display is deleted, Student: %s", personToDelete);
logger.info(logMessageForPerson);
}

logger.info("Student deleted - " + personToDelete);
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/tuteez/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,11 @@ public CommandResult execute(Model model) throws CommandException {

model.setPerson(personToEdit, editedPerson);

if (model.getLastViewedPerson().get().isPresent()) {
if (personToEdit.equals(model.getLastViewedPerson().get().get())) {
model.updateLastViewedPerson(editedPerson);
String logMessageForPerson =
String.format("Student on display is edited, After Edit - Student: %s", editedPerson);
logger.info(logMessageForPerson);
}
if (model.isSamePersonAsPersonOnDisplay(personToEdit)) {
model.updateLastViewedPerson(editedPerson);
String logMessageForPerson =
String.format("Student on display is edited, After Edit - Student: %s", editedPerson);
logger.info(logMessageForPerson);
}

model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/tuteez/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ public interface Model {
*/
void removeLastViewedPerson();

/**
* Returns true if the lastViewedPerson is present.
*/
boolean isLastViewPersonAvailable();

/**
* Returns true if the provided person matches the person currently shown on display.
*/
boolean isSamePersonAsPersonOnDisplay(Person person);

/**
* Returns the {@code person} in the address book with the given name.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/tuteez/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ public void removeLastViewedPerson() {
logger.info("Last viewed person removed");
}

@Override
public boolean isLastViewPersonAvailable() {
return lastViewedPerson.get().isPresent();
}

@Override
public boolean isSamePersonAsPersonOnDisplay(Person person) {
if (isLastViewPersonAvailable()){
return lastViewedPerson.get().get().equals(person);
}

return false;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/tuteez/logic/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,17 @@ public void updateLastViewedPerson(Person personOnDisplay) {

@Override
public void removeLastViewedPerson() {
throw new AssertionError("This method should not be called.");
throw new AssertionError("This method should not be called");
}

@Override
public boolean isLastViewPersonAvailable() {
throw new AssertionError("This method should not be called");
}

@Override
public boolean isSamePersonAsPersonOnDisplay(Person person) {
throw new AssertionError("This method should not be called");
}

@Override
Expand Down

0 comments on commit 1238ac3

Please sign in to comment.