diff --git a/src/main/java/tuteez/logic/commands/DeleteCommand.java b/src/main/java/tuteez/logic/commands/DeleteCommand.java index 7e158fa4887..d68342fae11 100644 --- a/src/main/java/tuteez/logic/commands/DeleteCommand.java +++ b/src/main/java/tuteez/logic/commands/DeleteCommand.java @@ -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); diff --git a/src/main/java/tuteez/logic/commands/EditCommand.java b/src/main/java/tuteez/logic/commands/EditCommand.java index c0435493918..1993f40ba45 100644 --- a/src/main/java/tuteez/logic/commands/EditCommand.java +++ b/src/main/java/tuteez/logic/commands/EditCommand.java @@ -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); diff --git a/src/main/java/tuteez/model/Model.java b/src/main/java/tuteez/model/Model.java index b8d5de449f8..47676a1fd58 100644 --- a/src/main/java/tuteez/model/Model.java +++ b/src/main/java/tuteez/model/Model.java @@ -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. */ diff --git a/src/main/java/tuteez/model/ModelManager.java b/src/main/java/tuteez/model/ModelManager.java index 7c1b7a452f1..78549a3091b 100644 --- a/src/main/java/tuteez/model/ModelManager.java +++ b/src/main/java/tuteez/model/ModelManager.java @@ -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) { diff --git a/src/test/java/tuteez/logic/commands/AddCommandTest.java b/src/test/java/tuteez/logic/commands/AddCommandTest.java index 8124b5eeb79..06e053ca68b 100644 --- a/src/test/java/tuteez/logic/commands/AddCommandTest.java +++ b/src/test/java/tuteez/logic/commands/AddCommandTest.java @@ -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