0

I am new to OOP and programming in general. I am having trouble with how to put things in the parent class and call them from the other classes and main.

I have the following arraylist creators in main, but feel to be really OOP these should be in the parent and subclasses and just called from main. Is this is correct can someone help me with how this would work.

How do I get the arraylist in the parent class and then call it correctly from main?

This is what I have for main:

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 != 5){
        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 Personal Contacts: Enter 3");
        System.out.println("Display Business Contacts: Enter 4");
        System.out.println("5 to quit");

        type = input1.nextInt();

        if(type == 5){
            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);
        }

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

        }
        }


        }  

This is what I have for the parent class:

package ooo1;

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

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 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 displayContacts(){
        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);
    }

}

One of my subclasses: other same just adds a few more variables: Display Contact(): doesn't work not sure what to do with it either.

/* * 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 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 void displayContacts(){
    System.out.print("Personal Contacts: ");
    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);
    System.out.println("Birthday:" + dateofBirth);
}

}

2
  • 1
    I think it's right that Contact represents just 1 contact. If you want to separate the List's out then you could look at creating an AddressBook class which contains a collection of Contacts. Commented Feb 10, 2014 at 15:45
  • If I leave this way confused on how I print out the contact lists. I need them separated by Personal and Business and need to be able to call a certain contact by contactID. If I could get the print feature to work I would call this good for a first attempt at this. If a created an addressbook class how would that work. Commented Feb 10, 2014 at 16:03

3 Answers 3

1

You probably want something like this.

public class AddressBook<T extends Contact>
{

  private List<T> contacts = new ArrayList<T>();

  public void addContact(T contact)
  {
    contacts.add(contact);
  }

}

You could instantiate and use this class like this.

AddressBook<Contact> book = new AddressBook<Contact>();
book.add(new PersonalContact(...));
book.add(new BusinessContact(...));

Then over time you have the flexibility to add methods to AddressBook that work with the underlying collection. For instance you might want to search for contacts with a particular name. Or return an iterator of Contacts ordered by a particular attribute.

Sign up to request clarification or add additional context in comments.

2 Comments

I am sure this is a stupid question: I see where I need this to be but can not seem to get it there. What is the <T> referring too. Sorry am I getting more confused.
T is the generic type. You can instantiate AddressBook of type Contact or you can be more specific and say this address book can only hold personal contacts.
0

You can add a method in class Contact :

public void getData(){
// take in all the inputs here, so that you can directly store them in class member variables instead of passing them from main.
}

Assuming that PersonalContact & BusinessContact are inherited classes from Contact. You can add a method in them:

class PersonalContact extends Contact{  
String dateofBirth;
public void getData(){
super.getData();  //calls getData() method from base class
// take DOB as input & store it
}

similarly for BusinessContact class.

I suggest you take a look at abstract classes & interfaces for future use.

5 Comments

getData is a bad name for that method, since it's not a getter.
The method does get data from the user.I've written in comments.
So you want your model class to be tied to the UI and prompt for user input? I hope I've misunderstood.
I'm not getting what you are trying to say.You can add a new Scanner object as a class member in Contact,& the getData method will prompt for user input using this Scanner object.
That design will highly couple your model (The contact class) to the UI (the scanner). This is poor OO design.
0

The Contact class seems okay. But ContactList not that much. It's supposed to be a data structure for contacts, so there's not reason for main method there.

public class ContactList {

     private ArrayList<Contact> contacts;       

     public ContactList(){
          this.contacts = new ArrayList<Contact>();
     }

     public void addContact(Contact contact){
          this.contacts.add(contact);
     }

     public Contact getContact(int index){
          return contacts.get(index);
     }

     // other methods that work with the data structure
     // f.e. searching, deleting, ...

}

and then you could have some ContactUtil class that would take care of reading contact info from user (what you had in you main method).

public final class ContactUtil {

    private ContactUtil(){} // we don't want to create instances of this class

    public static Contact readNewContact(){

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


         return contact;
    }   

}

And finally you will have some class just for main():

public class Main {

      public static void main(String[] args){

           ContactList myContacs = new ContactList();
           myContacts.add(ContactUtil.readNewContact());

           Contact contact = ContactUtil.readNewContact();
           myContacts.add(contact); 

      }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.