2

can someone help me how to shorten this code by using only ONE loop through the list which both deleteFromList() and increaseAge() can use??

private void deleteFromList() {
    System.out.println("Type the name of the person you want to delete: ");
    String nameOfPerson = keyboard.nextLine();
    for (int i = 0; i < allPersons.size(); i++) {
        if (allPersons.get(i).getName().equalsIgnoreCase(nameOfPerson)) {
            allPersons.remove(i);
            System.out.println("The person has been deleted!");
        }
    }
}


private void increaseAge() {
    System.out.println("Type in the persons name: ");
    String nameOfPerson = keyboard.nextLine();
    for (int i = 0; i < allPersons.size(); i++) {
        if (allPersons.get(i).getName().equalsIgnoreCase(nameOfPerson)) {
            Person person = allPersons.get(i);
            person.setAge();
            System.out.println("Persons age have been changed");
        }
    }
}
3
  • What should happen if the name entered is the same for both deletion and incrementing the age? Commented Dec 6, 2016 at 8:40
  • 1
    @TimBiegeleisen I think the question is simply how to reduce code duplication. Commented Dec 6, 2016 at 8:45
  • codereview.stackexchange.com Commented Dec 6, 2016 at 8:49

2 Answers 2

1

If I understand you correctly, you're looking to avoid the duplicate iteration and comparison logic between the two methods. The good news is, if you're using Java 8 you don't have to loop at all:

private void deleteFromList() {
    System.out.println("Type the name of the person you want to delete: ");
    String nameOfPerson = keyboard.nextLine();
    allPersons.removeIf(p -> p.getName().equalsIgnoreCase(nameOfPerson));
}

private void increaseAge() {
    System.out.println("Type in the persons name: ");
    String nameOfPerson = keyboard.nextLine();
    allPersons.stream()
            .filter(p -> p.getName().equalsIgnoreCase(nameOfPerson))
            .forEach(Person::setAge);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe something like the following could help? Names should probably be optimized ;-) Using a general method handlePerson so that you can put in specific consumers, if you need them.

public void handlePerson(String personName, Consumer<Person> personConsumer) {
    allPersons.stream()
        .filter(p -> p.getName().equalsIgnoreCase(personName))
        .forEach(personConsumer);
}


public void increaseAge() {
    System.out.println("Type in the persons name: ");
    handlePerson(keyboard.nextLine(), Person::setAge));
    // or if you really want to do it all in the consumer:
    handlePerson(keyboard.nextLine(), p -> { 
        p.setAge(); 
        System.out.println("Persons age have been changed");
    });
}

public void deletePerson() {
    System.out.println("Type in the persons name: ");
    String personName = keyboard.nextLine();
    // of course no way to handle deletion via consumer ;-)
    if (allPersons.removeIf(p -> p.getName().equalsIgnoreCase(personName))) {
        System.out.println("The person has been deleted!");
    }

}

You may also want to introduce a separate Predicate if you are using p.getName().equalsIgnoreCase(personName) more often.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.