使用多态来显示arraylist的输出

时间:2014-02-10 02:43:15

标签: java oop arraylist

我需要显示当用户从菜单中选择显示联系人时输入的所有联系人的名字和姓氏。当用户输入在ArrayList中输入的联系人ID时,我还需要显示联系人的所有详细信息。 我可以显示的是输入的最后一个联系人。一旦我经历过一次然后返回菜单并输入另一个联系人,我是不是保留了ArrayList?或者我只是没有正确调用打印功能来打印arraylist中的所有联系人。

如果我现在输入3,它只会回到数组的第一行,并希望我输入一个联系人ID,然后再次创建联系人,而不是显示已存在的联系人。

package ooo1;

import java.util.ArrayList;
import java.util.Scanner;

public class ContactList {

  public static void main(String[] args) {

    Scanner input1 = new Scanner(System.in);
    int type = 0;

    while(type != 4){
      System.out.println("Please select an option:");
      System.out.println("Personal Contact: Enter 1");
      System.out.println("Business Contact: Enter 2");
      System.out.println("Display Contacts List: Enter 3");
      System.out.println("4 to quit");

      type = input1.nextInt();

      if(type == 4){
        System.out.println("Goodbye");
        break;
      }

      ArrayList<Contact> contacts = new ArrayList<Contact>();
      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();

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

        for (Contact showcontact: contacts){
          System.out.println(showcontact.displayContact());
        }
      }

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

        for (Contact showcontact: contacts){
           System.out.println(showcontact.displayContact());
        }
      }

      else if(type ==3){
        for (Contact listcontacts: contacts){
            System.out.println(listcontacts.displayFullName());
        }
      }          
    }  
  }
}

父类:

package ooo1;

public abstract class Contact {

  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 this.contactId; }
  public void setFirstName(String input){ this.firstName = input; }
  public String getFirstName(){ return this.firstName; }
  public void setLastName(String input){ this.lastName = input; }
  public String getLastName(){ return this.lastName; }
  public void setAddress(String input){ this.address = input; }
  public String getAddress(){ return this.address; }
  public void setPhoneNumber(String input){ this.phoneNumber = input; }
  public String getPhoneNumber(){ return this.phoneNumber; }
  public void setEmailAddress(String input){ this.emailAddress = input; }
  public String getEmailAddress(){ return this.emailAddress; }

  public String displayFullName(){
      System.out.println("Contact List:");
      return ("First Name:" + this.getFirstName() + "Last Name:" + this.getLastName());
  }

  public String displayContact(){
      return ("ContactID:" + this.getContactId() + "First Name:" + this.getFirstName() + "Last Name:" + this.getLastName() + "Address:" + this.getAddress() + "Phone Number:" + this.getPhoneNumber() + "Email Address" + this.getEmailAddress());
  }
}

其中一个子类:其他同样只添加更多变量:

package ooo1;

public class PersonalContact extends Contact {

  private String dateofBirth;

  public PersonalContact(String contactId, String firstName, String lastName, String address, String phoneNumber, String emailAddress, String dateofBirth){      
      super(contactId, firstName, lastName, address, phoneNumber, emailAddress);    
      this.dateofBirth = dateofBirth;
  }

  public void setDateofBirth(String input){ this.dateofBirth=input; }

  public String getDateofBirth(){ return this.dateofBirth; }

  @Override
  public String displayContact(){
      System.out.println("Personal Contacts:");
      return super.displayContact() + "Date of Birth: " + this.getDateofBirth();   
  }
}

1 个答案:

答案 0 :(得分:1)

所以这里有一个关于如何使用多态的小例子,基本上你的类被剥离到最小值(这就是SSCCE的意思 - 剥离不相关的东西,比如注释和行为相同的多个属性等) 。

当您调用存储在ArrayList多态中的Contact对象上的displayContact()方法时,请确保使用正确的displayContact方法。

PersonalContact.displayContacts();不起作用的原因是你需要在on对象上调用方法,因为它不是静态方法。

我希望这个例子有所帮助:

// Contact.java

public abstract class Contact {

    private final String name;

    public Contact(String name) {
        this.name = name;
    }

    public String displayContact() {
         return "Contact name: " 
                + name + ". Contact type: " 
                + this.getClass().getName() + ". ";
    }
}

// PersonalContact.java

public class PersonalContact extends Contact {

    private final String dateofBirth;

    public PersonalContact(String name, String dateOfBirth) {
        super(name);
        this.dateofBirth = dateOfBirth;
    }

    @Override
    public String displayContact() {
        return super.displayContact() + "Date of birth: " + dateofBirth + ".";
    }
}

// BusinessContact.java

public class BusinessContact extends Contact {

    private final String organization;

    public BusinessContact(String name, String org) {
        super(name);
        this.organization = org;
    }

    @Override
    public String displayContact() {
        return super.displayContact() + "Organization: " + organization + ".";
    }
}

// ContactList.java

import java.util.ArrayList;

public class ContactList {

    public static void main(String[] args) {

        ArrayList<Contact> contacts = new ArrayList<>();

        PersonalContact personalContact1 = new PersonalContact("John", "1980-01-01");
        BusinessContact businessContact1 = new BusinessContact("theCompany", "The Company");

        contacts.add(personalContact1);
        contacts.add(businessContact1);

        for (Contact contact : contacts) {
            System.out.println(contact.displayContact());
        }
    }
}

EDIT在评论中添加了一些更多引用问题。

您无法打印任何内容的原因是请求输入的代码块始终执行,因为它不包含在type 1 or 2块中。为了解决这个问题,我建议进行以下更改:

首先,将ArrayList和Scanner移动到main方法的开头:

ArrayList<Contact> contacts = new ArrayList<Contact>();
Scanner input = new Scanner(System.in);

然后在if块中包含处理输入的整个代码块,看起来像这样:

if(type==1 || type==2){

        Contact contact = null;

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

        if(type == 1){
          System.out.println("Please enter Birthday: ");
          String dateofBirth = input.nextLine();
          contact = new PersonalContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, dateofBirth);
        }

        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 = new BusinessContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, jobTitle, organization);
        }

        // add the new contact
        contacts.add(contact);
        // print out the newly created contact here
        System.out.println(contact.displayContact());
    }

此外,您实际上不需要两个Scanner对象,只需重用其中一个:)

你应该考虑将if(type==4)移到其他区块之后,以便它按顺序排列(这只是风格问题)。