If you're simply trying to print out only the richest person and their belongings, you should use:
Person richest = new Person(/*Any default initializers here*/);
// This will set 'richest' to the richest person
for (Person p : personRegistry) {
if (p.getValue() > richest.getValue())
richest = p;
}
System.out.println("Richest person is: " + richest.getName());
System.out.println("Total value: " + richest.getTotalValue());
System.out.println(richest.getName() + " owns: ");
for (Item i : richest.getItems()) {
System.out.println(i.getName() + " " + i.getValue());
}
If you wish to hold duplicates (that is, Person objects with the same totalValue),
ArrayList<Person> richestPeople = new ArrayList<Person>();
Person richest = new Person(/*Any default initializers here*/);
for (Person p : personRegistry) {
if (p.getValue() > richest.getValue())
richest = p;
}
// now check to see if there are people with same value
for (Person p : personRegistry) {
if (p.getValue() == richest.getValue())
// add them to the list
richestPeople.add(p);
}
// now we loop through the list of richest people and print them out
for (Person p : richestPeople) {
System.out.println("Richest person is: " + p.getName());
System.out.println("Total value: " + p.getTotalValue());
System.out.println(p.getName() + " owns: ");
for (Item i : p.getItems()) {
System.out.println(i.getName() + " " + i.getValue());
}
}