You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.0 KiB
Java

package src;
public class AnimalHelper {
private final Animal[] animals;
private double averagePrice;
public AnimalHelper(Animal[] animals) {
this.animals = animals;
}
public void displayInformation() {
/* Display the animal information */
int total = 0;
double sum = 0.0;
for (Animal animal : animals) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\n\nName: " + animal.animalName());
stringBuilder.append("\nSound: " + animal.animalSound());
stringBuilder.append("\nPrice: £" + animal.animalPrice());
System.out.println(stringBuilder.toString());
total++;
sum += animal.animalPrice();
}
averagePrice = sum / total;
}
public void displayAveragePrice() {
/* Display the average price of all the animals */
System.out.printf("%nThe Average Price For All The Animals Are: £%.2f", averagePrice);
}
}