没有得到预期的产出

时间:2015-02-20 20:17:20

标签: java

当我运行我的程序并选择选项2以获取联系人详细信息时,我希望它显示如下(名字,姓氏):

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: 

而不是个人联系,它显示如下:

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: 

而不是商业联系,它显示如下:

Contacts who have been entered: 
0) 1 F St. jd@gmail.com (address, email) 
Please enter the number corresponding to the contact you would like to view: 

然后,当我输入显示个人联系人的号码时,它只返回我的名字和业务,只返回我的名字和姓氏。它应该返回添加联系步骤中放入的完整联系信息。我以为我正确地编程了一切,但它并没有显示我想看的东西。任何帮助将不胜感激。我的代码如下所示。

主:

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());
}

}

联系

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() ;
}
}

个人

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;
}

}

商业

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;
}

}

2 个答案:

答案 0 :(得分:1)

这是您用来打印联系人的行:

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

这意味着您使用toString()方法打印联系人。现在,你在个人联系方面有什么?

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

这意味着它只返回出生日期,而不是联系人中的任何其他字段。

对于您的业务联系人:

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

这意味着它只会显示字段jobTitleorganization的内容,而不会显示任何其他字段。

这与@femtoRgon给出的答案相结合,这意味着您也没有正确分配字段,为您提供结果。

你必须:

  1. 更改联系人中的toString()以返回公共字段。现在它是一种危险的,无限递归的方法:

    @Override
    public String toString() {
         return toString() ;
    }
    
  2. 重写个人和名片中的toString方法,以便它们返回super.toString()的组合 - 您在步骤1中更改的方法 - 以及特定于PersonalBusiness

  3. 更改您进行的new调用,以便将参数正确传递给构造函数。

答案 1 :(得分:0)

以下是Personal构造函数调用和Personal构造函数定义。

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

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

他们的论点顺序并不匹配。所以你要传入一个名为&#34; firstName&#34;的变量。作为参数指定为&#34; dateOfBirth&#34;和&#34; lastName&#34;作为&#34; firstName&#34;,&#34;地址&#34;作为&#34; lastName&#34;等等。所以你对ctor的调用应该是:

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

问题与您对Business构造函数的调用非常相似,您传入&#34; jobTitle&#34;和&#34;组织&#34;作为最后一个参数,而类期望它们是前两个参数。