Skip to content

Commit

Permalink
Merge pull request #241 from AngPengXuan/235-redundant-functions
Browse files Browse the repository at this point in the history
Remove redundant functions
  • Loading branch information
Ponharish authored Nov 7, 2024
2 parents 67102da + c97c1fb commit d215b44
Showing 1 changed file with 3 additions and 38 deletions.
41 changes: 3 additions & 38 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
Expand Down Expand Up @@ -34,13 +32,12 @@ public DeleteCommand(Nric targetNric) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (!personExistsByNric(targetNric, lastShownList)) {
Person personToDelete = model.getPerson(targetNric);

if (personToDelete == null) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_NRIC);
}

Person personToDelete = findPersonByNric(targetNric, lastShownList);
model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)));
}
Expand All @@ -66,36 +63,4 @@ public String toString() {
.add("targetNric", targetNric)
.toString();
}

/**
* Finds a person by their Nric in the given list.
*
* @param nric The Nric to search for.
* @param personList The list of persons to search in.
* @return The person with the given Nric, or null if not found.
*/
private Person findPersonByNric(Nric nric, List<Person> personList) {
for (Person person : personList) {
if (person.getNric().equals(nric)) {
return person;
}
}
return null;
}

/**
* Checks if a person with the given Nric exists in the given list.
*
* @param nric The Nric to search for.
* @param personList The list of persons to search in.
* @return True if a person with the given Nric exists, false otherwise.
*/
private boolean personExistsByNric(Nric nric, List<Person> personList) {
for (Person person : personList) {
if (person.getNric().equals(nric)) {
return true;
}
}
return false;
}
}

0 comments on commit d215b44

Please sign in to comment.