java - 没有得到预期的输出

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

}

最佳答案

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

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

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

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

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

对于您的业务联系方式:

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

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

这与@femtoRgon给你的答案相结合,这意味着你也没有正确分配字段,给出了你所得到的结果。

你必须:

  1. 更改 Contact 中的 toString() 以返回公共(public)字段。现在这是一种危险的、无限递归的方法:

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

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

关于java - 没有得到预期的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28637474/

相关文章:

java - Axis 2 客户端未收到 Axis 2 Web 服务抛出的异常

java - 如何解决无法加载身份验证插件 'caching_sha2_password'问题

java - 为什么这个 map.put() 会覆盖现有的值事件,尽管 map.containskey() 返回 false?

java - Eclipse:在多个项目之间共享代码

java - 局部内部类安全访问包含方法的局部变量

java - 将二进制数据转换为字符串

java - 如何知道哪些文件已被修改以修复从 git 提交开始的错误?

java - 从 127.0.0.1 到 2130706433,然后再返回

java - 为什么在jsp/servlet 的web.xml(Deployment Descriptor) 中使用<servlet-mapping>?

java - 线程 "timer-0"java.lang.NoClassDefFoundError 中的异常