使用java和面向对象创建联系人列表

时间:2014-02-09 20:35:15

标签: java oop arraylist

我是编程和面向对象设计的新手。这是我完成学士学位的最后要求(不是编程)。我对如何使面向对象的工作感到困惑,我看到的任何东西似乎都没有帮助。 分配是创建使用继承,多态和集合的联系人列表。我需要一个存储两种联系人的联系人列表:商业和个人。 1.提示选择要添加或显示的联系人。 2.提示允许用户输入联系信息。 3.提示将显示所选联系人的输出。

我有以下类和子类。我被困在如何阅读特定arraylists的输入中。我甚至都不知道这些课程是否正确。 任何帮助都会很棒,我只需要通过这个,然后我很乐意将编程留给那些知道他们在做什么的人。

这就是我的父母班级所拥有的:

package ooo1;

public abstract class Contact {

    private String contactId;
    private String firstName;
    private String lastName;
    private String address;
    private String phoneNumber;
    private 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;
    }

    void displayContact(){
        System.out.println("Contact ID:" + contactId + " First Name:" + firstName + " Last Name:" + lastName);
        System.out.println("Address:" + address);
        System.out.println("Phone Number:" + phoneNumber);
        System.out.println("Email Address:" + emailAddress);
    }

}

这是我的一个子类:

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

}

这是我的另一个子类:

package ooo1;

public class BusinessContact extends Contact {

    private String jobTitle;
    private String organization;

    public BusinessContact(String contactId, String firstName, String lastName, String address, String phoneNumber, String emailAddress, String jobTitle, String organization){

        super(contactId, firstName, lastName, address, phoneNumber, emailAddress);

        this.jobTitle = jobTitle;
        this.organization = organization;
    }
    public void setJobTitle(String input){
        this.jobTitle = input;
    }
    public String getJobTitle(){
        return this.jobTitle;
    }

    public void setOrganization(String input){
        this.organization = input;
    }
    public String getOrganization(){
        return this.organization;
    }

}

这就是我对Main的看法,我认为这是错误的:

package ooo1;

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

public class ContactList {

    public static void main(String[] args) {

        ArrayList<PersonalContact> personalList = new ArrayList<PersonalContact>();

        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();
        System.out.println("Please enter Birthday: ");
        String dateofBirth = input.nextLine(); 

    }

}

2 个答案:

答案 0 :(得分:0)

你需要将它们组合在一起。首先,考虑使用List。它应该包含两种类型的联系人,但是它的类型参数使用派生类型PersonalContact。而是使用基类Contact

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

接下来,您似乎开始通过控制台从最终用户收集有关联系人的信息。在此之前,您可能想询问输入的联系类型,可以选择为个人联系人输入1或为业务联系人输入2。然后收集特定于他们选择的联系类型的信息。

Scanner input = new Scanner(System.in);
System.out.println("Please enter Specify the contact type (1 Personal, 2 Business) : ");
int contactType = input.nextInt();

//collect data common to both types
if(contactType == 1){
   //collect information specific to personal
} else if(contactType ==2){
   //collect information specific to business
}

接下来,您需要使用构造函数将收集的数据转换为实际对象。这需要有条件地完成,如果做得好,实际上可能是最后一部分的一部分。

   Contact contact;
   if(contactType == 1){
       contact = new PersonalContact(/*arguments here*/);
   } else{
       contact = new BusinessContact(/*arguments here*/);
   }

然后通过将联系人添加到列表中来完成:

   contacts.add(contact);

答案 1 :(得分:0)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaprac;

/**
 *
 * @author Arijit
 */
public class Contact {
    private String name;
    private int number;

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

    public String getName() {
        return name;
    }

    public int getNumber() {
        return number;
    }

    public static Contact createContact(int number, String name){
        return new Contact(number,name);
    }



}

这是Contact类,由联系人姓名和联系电话号码管理。接下来,我们将设计MobilePhone类,它将包含一个数字(可选)和一个联系人的arraylist。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaprac;

import java.util.ArrayList;

/**
 *
 * @author Arijit
 */
class MobilePhone1 {
 public int mynumber;
 public ArrayList < Contact > contacts;

 public MobilePhone1(int mynumber) {
  this.mynumber = mynumber;
  this.contacts = new ArrayList < Contact > ();
 }

 public Boolean addContact(Contact contact) {
  if (findPosition(contact) >= 0) {
   System.out.println("Contact already is phone");
   return false;
  } else {
   contacts.add(contact);
  }

  return true;
 }

 public ArrayList < Contact > getContacts() {
  return contacts;
 }

 public void updateContact(Contact oldContact, Contact newContact) {
  if (findPosition(oldContact) >= 0) {
   contacts.set(findPosition(oldContact), newContact);
  } else {
   System.out.println("Contact does not exist");
  }
 }

 public void removeContact(Contact contact) {
  if (findPosition(contact) >= 0) {
   contacts.remove(findPosition(contact));
  } else {
   System.out.println("No contact");
  }
 }

 public int searchContact(Contact contact) {
  int position = findPosition(contact);
  if (contacts.contains(contact)) {
   System.out.println("Item found at position");
   return position;
  }
  System.out.println("Not found");
  return -1;
 }



 private int findPosition(Contact contact) {
  return this.contacts.indexOf(contact);

 }

 private int findPosition(String name) {
  for (int i = 0; i < contacts.size(); i++) {
   Contact contact = this.contacts.get(i);
   if (contact.getName().equals(name)) {
    return i;
   }
  }
  return -1;
 }


}

public class MobilePhone {
 public static void main(String args[]) {
  Boolean quit = false;
  int choice = 0;
  java.util.Scanner sc = new java.util.Scanner(System.in);
  MobilePhone1 phone = new MobilePhone1(98312);

  //Contact newcontact = Contact.createContact(12345,"Arijt");

  while (!quit) {
   System.out.println("Enter your choice");
   choice = sc.nextInt();
   sc.nextLine();


   switch (choice) {
    case 0:
     System.out.println("Enter the number of Contacts");
     int count = sc.nextInt();
     for (int i = 0; i < count; i++) {
      System.out.println("Enter number");
      int phoneNumber = sc.nextInt();
      System.out.println("Enter Name");
      sc.nextLine();
      String name = sc.nextLine();
      Contact newcontact = Contact.createContact(phoneNumber, name);
      phone.addContact(newcontact);
     }
     break;

    case 1:
     int size = phone.getContacts().size();
     System.out.println(size);
     for (int i = size - 1; i >= 0; i--) {
      phone.removeContact(phone.getContacts().get(i));
     }
     System.out.println(phone.getContacts().isEmpty());
     break;

    case 2:

     for (int i = 0; i < phone.getContacts().size(); i++) {
      System.out.println(phone.searchContact(phone.getContacts().get(i)));
     }
     break;

    case 3:

     //Contact newcontact1 = Contact.createContact(12345,"Buzz");
     System.out.println("Enter the Contact name you want to update");
     String oldContactName = sc.nextLine();

     for (int j = 0; j < phone.getContacts().size(); j++) {
      if (phone.getContacts().get(j).getName().equals(oldContactName)) {
       System.out.println("Enter the new Contact name");
       String newName = sc.nextLine();
       System.out.println("Enter the new Contact number");
       int newNumber = sc.nextInt();
       phone.updateContact(phone.getContacts().get(j), Contact.createContact(newNumber, newName));
      } else {
       System.out.println("You are looking for the wrong contact");
      }

     }


     for (int i = 0; i < phone.getContacts().size(); i++) {
      System.out.println(phone.getContacts().get(i).getName() + "," + phone.getContacts().get(i).getNumber());
     }
     break;

    case 4:
     if(phone.getContacts().isEmpty()){
         System.out.println("Emtpty contact list");

     }
     else {
         System.out.println("Contact list");
     for (int i = 0; i < phone.getContacts().size(); i++) {
      System.out.println("Name: "+phone.getContacts().get(i).getName() + ",Phone Number: " + phone.getContacts().get(i).getNumber());
     }
     }
     break;

    case 6:
        System.out.println("Enter 0 for adding contact\n");
        System.out.println("Enter 1 for removing every contact\n");
        System.out.println("Enter 2 for searching contact\n");
        System.out.println("Enter 3 for updating contact\n");
        System.out.println("Enter 4 for viewing the contact list\n");
        System.out.println("Enter 6 for exiting\n");
        System.out.println("Enter 5 to see the instrusctions again\n");
        break;
    case 5:
     quit = true;
     break;



   }
  }
 }
}