JLS
JLS

Reputation: 71

Loop an ArrayList with ONE method

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");
        }
    }
}

Upvotes: 2

Views: 49

Answers (2)

Roland
Roland

Reputation: 23232

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.

Upvotes: 1

shmosel
shmosel

Reputation: 50716

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);
}

Upvotes: 1

Related Questions