【发布时间】:2015-02-21 04:38:48
【问题描述】:
当我运行我的程序并选择选项 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;
}
}
【问题讨论】:
-
如果你给我们一个更小的例子来重现你的问题,这将更方便每个人阅读。
-
谢谢大家,我稍后会查看并相应更新。感谢您的意见。
标签: java