TimD
TimD

Reputation: 25

Not getting expected output

When I run my program and choose option 2 to get contact details I am expecting it to display as follows (First Name, Last Name):

Contacts how have been entered: 
0) John Doe 
1) George Smith 
2) Nancy Davis 
Please enter the number corresponding to the contact you would like to view: 

Instead for a personal contact it is displaying as follows:

Contacts who have been entered: 
0) Doe 1 F St. (last name, address) 
Please enter the number corresponding to the contact you would like to view: 

Instead for a business contact it is displaying as follows:

Contacts who have been entered: 
0) 1 F St. [email protected] (address, email) 
Please enter the number corresponding to the contact you would like to view: 

Then when I enter the number to display the contact for personal it is returning me only first name and business is only returning me first and last name. It should be returning the full contact info that was put in during the add contact step. I thought I programmed everything properly but it isn't displaying what I want to see. Any help would be greatly appreciated. My code is listed below.

Main:

package contactlist;

import java.util.ArrayList;
import java.util.Scanner;


public class ContactList {

    public static void main(String[] args) {

    int swValue;
    Scanner keyIn = new Scanner(System.in);

    ArrayList<Contact> ContactRecords = new ArrayList<Contact>();
    while (true) {
        // Display menu graphics
        System.out.println("========================================");
        System.out.println("|            Address List              |");
        System.out.println("========================================");
        System.out.println("| Options:                             |");
        System.out.println("|   1. Add Contact                     |");
        System.out.println("|   2. Get Contact Details             |");
        System.out.println("|   3. Exit                            |");
        System.out.println("========================================");
        System.out.println(" Select option: ");
        swValue = keyIn.nextInt();


        switch (swValue) {
            case 1:
                addContact(ContactRecords);
                break;
            case 2:
                getRecords(ContactRecords);
                break;
            case 3:
                System.exit(0);
                break;
            default:
                System.out.println("Invalid selection");
                break;
        }
    }
}

public static void addContact(ArrayList<Contact> ContactRecords) {
    Scanner textIn = new Scanner(System.in);
    Scanner keyIn = new Scanner(System.in);
    System.out.println("First Name: ");
    String firstName = textIn.nextLine();
    System.out.println("Last Name: ");
    String lastName = textIn.nextLine();
    System.out.println("Address:  ");
    String address = textIn.nextLine();
    System.out.println("Email Address: ");
    String email = textIn.nextLine();
    System.out.println("Phone: ");
    String phone = textIn.nextLine();
    System.out.println("Is this a 1) Personal or 2) Business?");
    int choice = keyIn.nextInt();
    if (choice == 1) {
        System.out.println("Date of Birth:  ");
        String dateOfBirth = textIn.nextLine();
        Personal aPersonal = new Personal(firstName, lastName, address,
        email, phone, dateOfBirth);
        ContactRecords.add(aPersonal);
    }
    if (choice == 2) {
        System.out.println("Job Title:  ");
        String jobTitle = textIn.nextLine();
        System.out.println("Organization: ");
        String organization = textIn.nextLine();
        Business aBusiness = new Business(firstName, lastName, address,
        email, phone, jobTitle, organization);
        ContactRecords.add(aBusiness);
    }

}

public static void getRecords(ArrayList<Contact> ContactRecords)
{
    Scanner keyIn = new Scanner(System.in);
    System.out.println("Contacts who have been entered:");
    for (int i = 0; i < ContactRecords.size(); i++) {
        System.out.println(i + ") "+ ContactRecords.get(i).getFirstName() +
        " " + ContactRecords.get(i).getLastName();
    }
    System.out.println("Please enter the number corresponding to the contact 
    you would like to view: ");
    int choice = keyIn.nextInt();

    System.out.println(ContactRecords.get(choice).toString());
}

}

Contact

package contactlist;

public abstract class Contact {
private String firstName;
private String lastName;
private String address;
private String email;
private String phone;

public Contact(String firstName, String lastName, String address, String 
email, String phone) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.email = email;
    this.phone = phone;
}

public Contact() {
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

@Override
public String toString() {
    return toString() ;
}
}

Personal

package contactlist;

public class Personal extends Contact {
private String dateOfBirth;

public Personal(String dateOfBirth, String firstName, String lastName, 
String address, String email, String phone) {
    super(firstName, lastName, address, email, phone);
    this.dateOfBirth = dateOfBirth;
}

public Personal() {
    super();
}

public String getDateOfBirth() {
    return dateOfBirth;
}

public void setDateOfBirth(String dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}

@Override
public String toString() {
    return dateOfBirth;
}

}

Business

package contactlist;

public class Business extends Contact {
private String jobTitle;
private String organization;

public Business(String jobTitle, String organization, String firstName, 
String lastName, String address, String email, String phone) {
    super(firstName, lastName, address, email, phone);
    this.jobTitle = jobTitle;
    this.organization = organization;
}

public Business() {
    super();
}

public String getJobTitle() {
    return jobTitle;
}

public void setJobTitle(String jobTitle) {
    this.jobTitle = jobTitle;
}

public String getOrganization() {
    return organization;
}

public void setOrganization(String organization) {
    this.organization = organization;
}

@Override
public String toString() {
    return jobTitle + " " + organization;
}

}

Upvotes: 0

Views: 102

Answers (2)

RealSkeptic
RealSkeptic

Reputation: 34608

This is the line you are using to print the contact:

System.out.println(ContactRecords.get(choice).toString());

This means you are using the toString() method to print the contact. Now, what do you have in a personal contact?

@Override
public String toString() {
    return dateOfBirth;
}

This means it is only returning the date of birth, not any of the other fields in the contact.

For the business contact you have:

@Override
public String toString() {
    return jobTitle + " " + organization;
}

This means it will only display the content of the fields jobTitle and organization, not any of the other fields.

This, in combination to the answer @femtoRgon gave you, which means that you are also not assigning the fields correctly, gives you the result you have.

You have to:

  1. Change the toString() in Contact to return the common fields. Right now it is a dangerous, infinitely recursive method:

    @Override
    public String toString() {
         return toString() ;
    }
    
  2. Rewrite the toString methods in the personal and business card so that they return a combination of super.toString() - the method you changed in step 1 - along with the additional fields that are specific to Personal or Business.

  3. Change the new calls you make so that you pass the parameters correctly to the constructors.

Upvotes: 1

femtoRgon
femtoRgon

Reputation: 33341

Here are the Personal constructor call and the Personal constructor definition.

Personal aPersonal = new Personal(firstName, lastName, address,
    email, phone, dateOfBirth);

public Personal(String dateOfBirth, String firstName, String lastName, 
String address, String email, String phone) {

They order of your arguments doesn't match. So you are passing in a variable called "firstName" as the argument specified as the "dateOfBirth", and "lastName" as the "firstName", "address" as the "lastName", etc. So your call to the ctor should be:

Personal aPersonal = new Personal(dateOfBirth, firstName, lastName, address,
    email, phone);

The problem is very much the same with your call to the Business constructor, you pass in "jobTitle" and "organization" as the last arguments, while the class expects them to be the first two arguments.

Upvotes: 0

Related Questions