Question

I’m working on a registry for persons and their belongings. This program is command based and therefore has some different functions. My problem here is that when the command ”show richest person" is performed, incorrect belongings of the calculated person are printed. The person and the total value of this owner’s items are correct; only the list of gadgets seem to be mixed up with someone else’s. This is how the code looks like:

public void getRichest() {
    double maxValue = -1;
    ArrayList<Person> richestPersons = new ArrayList<Person>();

    for (int i=0; i<personRegistry.size(); i++) {
        double totalValue = personRegistry.get(i).getTotalValue();
        if (totalValue > maxValue) {
            maxValue = totalValue;
            richestPersons = new ArrayList<Person>();
            richestPersons.add(personRegistry.get(i));
        } else if (totalValue == maxValue)
            richestPersons.add(personRegistry.get(i));
    }
    System.out.print("The richest person is ");
    for (int i=0; i<richestPersons.size();i++) {
        System.out.print(richestPersons.get(i).getName() + " ");
    }
    System.out.println("with the total value " + maxValue);
    for (int i=0; i<richestPersons.size();i++) {
        String name = richestPersons.get(i).getName();
        ArrayList<Item> items = personRegistry.get(i).getItems();

    // I GUESS THE PROBLEM STARTS HERE...
    System.out.println(name + " owns the following items:");
        for (int j=0; j<items.size(); j++) {
            String itemName = items.get(j).getName();
            double itemValue = items.get(j).getValue();
            System.out.println(itemName + " " + itemValue + " ");
        }
    }
}

The described problem only occurs in the execution of this command and no other. For example, If I perform a command called ”show information about specific person” and select the same person (the richest) the belongings of this person will now be correct. The code is almost identical with the earlier shown:

}

public void search(String name) {
    for (int i=0; i<personRegistry.size(); i++) {
        if (name.equalsIgnoreCase(personRegistry.get(i).getName())) {
            String foundName = personRegistry.get(i).getName();
            double totalValue = personRegistry.get(i).getTotalValue();

            System.out.println(foundName + " has a total value of " + totalValue + " ");
            System.out.println(foundName + " owns the following items:");

            ArrayList<Item> items = personRegistry.get(i).getItems();
            for (int j=0; j<items.size(); j++) {
                String itemName = items.get(j).getName();
                double itemValue = items.get(j).getValue();
                System.out.println(itemName + " " + itemValue + " ");
            }
            return;
        }
    }
    System.out.println("Error: Could not find the person you were searching for."); 
}

I’ve tried to figure this out (for example, I've tried copy-paste the latter code into the "getRichest"), but I would really appriciate some help!

Was it helpful?

Solution

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());
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top