这是我需要完成的最后一项要求,我无法弄清楚我做错了什么。我试图只显示ContactID,输入到数组中的联系人的名字和姓氏。 我的父类中有一个函数,它设置为显示ContactID,名字和姓氏。 我的问题是输出这些细节。但它会在打印联系人之前输出上次输入的联系人的完整详细信息。
这是我的主要课程:选项3是问题所在。
import java.util.ArrayList; import java.util.Scanner;
public class ContactList {
public static void main(String[] args) {
ArrayList<Contact> contacts = new ArrayList<>();
Scanner input1 = new Scanner(System.in);
int type = 0;
while(type != 5){
System.out.println("Please select an option: ");
System.out.println("Add a Personal Contact: Enter 1 ");
System.out.println("Add a Business Contact: Enter 2 ");
System.out.println("Display Contacts List: Enter 3 ");
System.out.println("Display Contact Details: Enter 4 ");
System.out.println("To Quit: Enter 5 ");
type = input1.nextInt();
if(type == 5){
System.out.println("Goodbye ");
break;
}
if(type == 1 || type == 2){
Contact contact = null;
Scanner input = new Scanner(System.in);
System.out.println("Please enter ContactId : ");
String contactId = input.nextLine();
System.out.println("Please enter First Name : ");
String firstName = input.nextLine();
System.out.println("Please enter Last Name : ");
String lastName = input.nextLine();
System.out.println("Please enter Address : ");
String address = input.nextLine();
System.out.println("Please enter Phone Number : ");
String phoneNumber = input.nextLine();
System.out.println("Please enter Email Address : ");
String emailAddress = input.nextLine();
//Create a personal contact.
if(type == 1){
System.out.println("Please enter Birthday: ");
String dateofBirth = input.nextLine();
Contact pcontact = new PersonalContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, dateofBirth);
contacts.add(pcontact);
}
//Create a business contact.
else if(type == 2){
System.out.println("Please enter Job Title: ");
String jobTitle = input.nextLine();
System.out.println("Please enter Organization: ");
String organization = input.nextLine();
Contact bcontact = new BusinessContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, jobTitle, organization);
contacts.add(bcontact);
}
}
//Print full name of each Contact.
if(type == 3){
System.out.println(contacts);
for (Contact namecontact: contacts)
{
System.out.println(namecontact.displayFullName());}
}
//Print contact details for selected contact.
else if(type == 4){
System.out.println("Enter a Contact ID to display Contact Details: ");
Scanner input2 = new Scanner(System.in);
String soughtID;
soughtID = input2.nextLine();
for (Contact showcontact1: contacts)
{
if (showcontact1.displayId().equals(soughtID))
System.out.println(showcontact1.displayContact());
}
}
}
}
}
这是保存方法的Parent类:displayFullName是我正在调用的。
包ooo1;
公共抽象类联系{
String contactId;
String firstName;
String lastName;
String address;
String phoneNumber;
String emailAddress;
public Contact(String contactId,String firstName,String lastName, String address, String phoneNumber, String emailAddress)
{
this.contactId = contactId;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phoneNumber = phoneNumber;
this.emailAddress = emailAddress;
}
public void setContactId(String input){
this.contactId = input;
}
public String getContactId(){
return contactId;
}
public void setFirstName(String input){
this.firstName = input;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String input){
this.lastName = input;
}
public String getLastName(){
return lastName;
}
public void setAddress(String input){
this.address = input;
}
public String getAddress(){
return address;
}
public void setPhoneNumber(String input){
this.phoneNumber = input;
}
public String getPhoneNumber(){
return phoneNumber;
}
public void setEmailAddress(String input){
this.emailAddress = input;
}
public String getEmailAddress(){
return emailAddress;
}
@Override
public String toString(){
return ("ContactID: " + this.getContactId() + "\nFirst Name: " + this.getFirstName() + "\nLast Name: " + this.getLastName() + "\nAddress: " + this.getAddress() + "\nPhone Number: " + this.getPhoneNumber() + "\nEmail Address " + this.getEmailAddress());
}
public String displayFullName(){
System.out.println("Contact: ");
return ("ContactID: " + this.getContactId() + "\nFirst Name: " + this.getFirstName() + "\nLast Name: " + this.getLastName());
}
public String displayContact(){
return ("ContactID: " + this.getContactId() + "\nFirst Name: " + this.getFirstName() + "\nLast Name: " + this.getLastName() + "\nAddress :" + this.getAddress() + "\nPhone Number :" + this.getPhoneNumber() + "\nEmail Address " + this.getEmailAddress());
}
public String displayId(){
return (this.getContactId());
}
}
这是输出的样子:
3 [ContactID:3 名字:汤姆 姓氏:琼斯 地址:西街123号 电话号码:345-235-2345 电子邮件地址:tjones@yahoo.com 出生日期:12-12-1978] 的联系人: ContactID:3 名字:汤姆 姓氏:琼斯
当我打电话给我时,我只想要来自联系人:(以粗体显示)。
如何让它停止显示上次输入的联系人的完整详细信息。
答案 0 :(得分:0)
删除下面的行,becoz它正在打印联系对象。
if(type == 3){
System.out.println(contacts); // Remove this line
for (Contact namecontact: contacts)
{
System.out.println(namecontact.displayFullName());
}