Reputation: 59
I have 3 options in this code and my other classes are set up correctly as far as I can tell. Option 1 and 3 seem to work fine but with option 2 I want it to print the number of contacts (which it does already), print the number of each type of contact, i.e. Personal or Business, and then print the average age of all the contacts. I can't figure out how to get the average age or how to print each type.
import java.util.Scanner;
public class PlannerMain { static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Contact[] contacts = new Contact[6];
contacts[0] = new PersonalContact("Joe Smith, ", 33, ", 100 Evergreen Ave, ", "Seattle,", "WA ", 98999); // Personal
contacts[1] = new PersonalContact("Lawrence Williams, ", 45, ", 2000 1st St, ", "Tacoma,", "WA ", 98100); // Personal
contacts[2] = new PersonalContact("Rachel Garcia, ", 12, ", 12 Forest Drive, ", "Los Angelos,", "CA ", 99301); // Personal
contacts[3] = new BusinessContact("Gregory Smith, ", 67, ", 360-888-7777", " 360-555-6666"); // Business
contacts[4] = new BusinessContact("Jerome Bradley, ", 18, ", 216-111-2222", " 253-444-7777"); // Business
contacts[5] = new BusinessContact("Susie Adams, ", 23, ", 253-333-7777", " 425-666-9999"); // Business
while (true) {
System.out.println("1.Print planner contacts. ");
System.out.println("2.Print planner statistics. ");
System.out.println("3.Exit.");
int option = scanner.nextInt();
if (option == 1) { // Displays every contact in the list until the length of the list has been reached
for (int i = 0; i < contacts.length; i++) {
System.out.println(contacts[i]);
} // End for
} // End option 1
else if (option == 2) {
int sum = 0;
System.out.println("Number of contacts: " + contacts.length);
for (int i = 0; i < contacts.length; i++){
sum += contacts[i];
}
System.out.println("Average age: " + (sum / contacts.length));
if(contacts[] instanceof PersonalContact){
for (int i = 0; i < PersonalContact.getLength(); i++){
System.out.println("Personal Contacts: " + personalContacts.length);
}// End for PC
}//End if PC
if(contacts[] instanceof BusinessContact){
for (int i = 0; i < BusinessContact.getLength(); i++){
System.out.println("BusinessContacts: " + businessContacts.length);
}// End for BC
}//End if BC
} // End option 2
else if (option == 3) { /** Terminates the program */
System.exit(0);
} // End option 3
} // End while
}// End void main
}// End
Upvotes: 0
Views: 129
Reputation: 174
To get the average age currently you are using a sum variable and attempting to add contacts[i] to it for every Contact in contacts. What you actually want is to add the age of each Contact to sum, using something like
sum += contacts[i].getAge()
or
sum += contacts[i].age
depending on how the Contact class is implemented.
To get the number of PersonalContacts and BusinessContacts you can do something like:
int personalContactsCount = 0;
for (int i = 0; i < contacts.length; i++) {
if (contacts[i] instanceof PersonalContact) {
personalContactsCount++;
}
}
int businessContactsCount = contacts.length - personalContactsCount;
Upvotes: 1