当我运行程序并选择选项 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;
}
这意味着它将仅显示 jobTitle
和 organization
字段的内容,而不显示任何其他字段。
这与@femtoRgon给你的答案相结合,这意味着你也没有正确分配字段,给出了你所得到的结果。
你必须:
更改 Contact 中的
toString()
以返回公共(public)字段。现在这是一种危险的、无限递归的方法:@Override public String toString() { return toString() ; }
重写个人名片和名片中的
toString
方法,以便它们返回super.toString()
的组合 - 您在步骤 1 中更改的方法- 以及特定于个人
或商业
的其他字段。更改您进行的
new
调用,以便将参数正确传递给构造函数。
关于java - 没有得到预期的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28637474/