0

I'm very new to programming and this is my first course. I'm having a very hard time understanding the whole concept and hope I could get some tips on how to proceed.

Essentially I need to write a simple Address Book program which lets the user input the information of 2 (or more?) users. I was given the following advice from my course aid:

In the method main of my application (demo or test) class - create two or more instances/objects of class AddressBook - use the instances (objects) to call instance methods of class AddressBook - use 2 instances/objects to compare their names

I currently have the following:

AddressBook.java

import java.util.Scanner;

public class AddressBook {

    Scanner sc = new Scanner(System.in);

    //Declare method variables and initial value (empty)
    String firstName = "";
    String middleName = "";
    String lastName = "";
    String homeAddress = "";
    String businessPhone = "";
    String homePhone = "";
    String cellPhone = "";
    String skypeId = "";
    String facebookId = "";
    String personalWebSite = "";

    public AddressBook(String firstName, String middleName, 
            String lastName, String homeAddress, String businessPhone,
            String homePhone, String cellPhone, String skypeId,
            String facebookId, String personalWebSite) {
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
        this.homeAddress = homeAddress;
        this.businessPhone = businessPhone;
        this.homePhone = homePhone;
        this.cellPhone= cellPhone;
        this.skypeId = skypeId;
        this.facebookId = facebookId;
        this.personalWebSite = personalWebSite;
    }

    //Setters
    void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    void setLastName(String lastName){
        this.lastName = lastName;
    }

    void setHomeAddress(String homeAddress) {
        this.homeAddress = homeAddress;
    }

    void setBusinessPhone(String businessPhone) {
        this.businessPhone = businessPhone;
    }

    void setHomePhone(String homePhone) {
        this.homePhone = homePhone;
    }

    void setCellPhone(String cellPhone) {
        this.cellPhone = cellPhone;
    }

    void setSkypeId(String skypeId) {
        this.skypeId = skypeId;
    }

    void setFacebookId(String facebookId) {
        this.facebookId = facebookId;
    }

    void setPersonalWebSite(String personalWebSite) {
        this.personalWebSite = personalWebSite;
    }


    //Getters
    public String getFirstName() {
        System.out.print("First name: ");
        String firstName = sc.nextLine();
        return firstName;
    }

    public String getMiddleName() {
        System.out.print("Middle name: ");
        String middleName = sc.nextLine();
        return middleName;
    }

    public String getLastName() {
        System.out.print("Last name: ");
        String lastName = sc.nextLine();
        return lastName;
    }

    public String getHomeAddress() {
        System.out.print("Home address: ");
        String homeAddress = sc.nextLine();
        return homeAddress;
    }

    public String getBusinessPhone() {
        System.out.print("Business phone number: ");
        String businessPhone = sc.nextLine();
        return businessPhone;
    }

    public String getHomePhone() {
        System.out.print("Home phone number: ");
        String homePhone = sc.nextLine();
        return homePhone;
    }

    public String getCellPhone() {
        System.out.print("Cell phone number: ");
        String cellPhone = sc.nextLine();
        return cellPhone;
    }

    public String getSkypeId() {
        System.out.print("Skype ID: ");
        String skypeId = sc.nextLine();
        return skypeId;
    }

    public String getFacebookId() {
        System.out.print("Facebook ID: ");
        String facebookId = sc.nextLine();
        return facebookId;
    }

    public String getPersonalWebSite() {
        System.out.print("Personal Website: ");
        String personalWebSite = sc.nextLine();
        return personalWebSite;
    }

    public String compareNames() {
            String comp1 = name1.getFirstName;
            String comp2 = name2.getFirstName;

            if (comp1.equalsIgnoreCase(comp2)) {
                System.out.println("The names match!");
            } else {
                System.out.println("The names don't match!");
            }
    }


} // end of class AddressBook

AddressBookDemo.java

public class AddressBookDemo {

    public static void main(String[] args) {

        // Create two AddreeBook objects
        AddressBook name1 = new AddressBook();
        AddressBook name2 = new AddressBook();

        // Invoke methods on those objects
        System.out.println("Enter the first person's info");
        name1.getFirstName();
            //Testing input/output
            System.out.println(name1.firstName);
//      name1.getMiddleName();
//      name1.getLastName();
//      name1.getHomeAddress();
//      name1.getBusinessPhone();
//      name1.getHomePhone();
//      name1.getCellPhone();
//      name1.getSkypeId();
//      name1.getFacebookId();
//      name1.getPersonalWebSite();

        System.out.println("Enter the second person's info");
        name2.getFirstName();
            //Testing input/output
            System.out.println(name2.firstName);
//      name2.getMiddleName();
//      name2.getLastName();
//      name2.getHomeAddress();
//      name2.getBusinessPhone();
//      name2.getHomePhone();
//      name2.getCellPhone();
//      name2.getSkypeId();
//      name2.getFacebookId();
//      name2.getPersonalWebSite();


    } // end of Main

} // End of class AddressBookDemo

As you can see I'm getting constructor errors when trying to initialize the two instances of class AddressBook(). If I try to add an arg of 'null' I simply get a return of 'null' when I test the output after invoking the methods. I also created the compareName() method in the main however haven't called it yet since I can't even get the first part to work. Additionally I'm a bit confused how I'm supposed to get the values of name1.firstName and name2.firstName sent back to main (or should I simply store the value into another variable and create an incremental loop each time getFirstName() is called?).

hjavaher
  • 2,589
  • 3
  • 30
  • 52
Chris
  • 3
  • 2
  • 2
    Constructors are used to construct objects. It doesn't seem like you're passing anything meaningful to construct the address book with. – Reut Sharabani Feb 18 '16 at 23:14
  • Getters aren't supposed to get any user input -- they should just return what's already in the object. Your main method should be doing everything with the scanner and then calling the setters to set the fields appropriately, or even better, passing the arguments into the `AddressBook` constructor. – Louis Wasserman Feb 18 '16 at 23:15
  • Finally, `String middleName = sc.nextLine();` does _not_ set the `middleName` field in the `AddressBook` object; it creates a new variable that happens to have the same name. Even for what you're trying to do, you should just write `middleName = sc.nextLine()` without the `String`. – Louis Wasserman Feb 18 '16 at 23:17
  • I think I understand what you guys mean. I'm revising my code to address the improper use of set/get. Also thanks for letting me know that I was only creating a new instance of each String; it's so obvious now looking at it ... sigh – Chris Feb 19 '16 at 00:05

2 Answers2

0

in your main class, you need to pass in the values you want stored in your address book, i.e.

AddressBook name1 = new AddressBook("firstName", "middleName", 
        "lastName", "homeAddress", "businessPhone",
        "homePhone", "cellPhone", "skypeId",
        "facebookId", "personalWebSite");

This will set a new name1 of type AddressBook with the above information. You may also want to consider creating an ArrayList of type AddressBook, which will store many instances of the AddressBook class.

ArrayList<AddressBook> myAddressBook = new ArrayList();

Then you need to add an object to the ArrayList:

myAddressBook.add(new AddressBook(("firstName", "middleName", 
        "lastName", "homeAddress", "businessPhone",
        "homePhone", "cellPhone", "skypeId",
        "facebookId", "personalWebSite"));
tmaxxcar
  • 309
  • 2
  • 15
  • thanks but I was told to avoid using arrays for the assignment since it's reviewed in later chapters. To be honest I had to send an email to my aid to find out how they expected me to do this without the use of an array ... – Chris Feb 19 '16 at 00:07
  • then you would have to create multiple AddressBook objects. You could use the scanner to get a number, which would be the number of contacts to create, and then use a loop to create the necessary number of contacts – tmaxxcar Feb 19 '16 at 00:26
0

This is what I ended up with using some of the advice given here. I will stick with just 2 contacts since that is what the assignment asked for;

Addressbook.java

import java.util.Scanner;

public class AddressBook {

    Scanner sc = new Scanner(System.in);

    //Declare method variables and initial value (empty)
    String firstName = "";
    String middleName = "";
    String lastName = "";
    String homeAddress = "";
    String businessPhone = "";
    String homePhone = "";
    String cellPhone = "";
    String skypeId = "";
    String facebookId = "";
    String personalWebSite = "";

    public String getFirstName() {
        System.out.print("First name: ");
        firstName = sc.nextLine();
        return firstName;
    }

    public String getMiddleName() {
        System.out.print("Middle name: ");
        middleName = sc.nextLine();
        return middleName;
    }

    public String getLastName() {
        System.out.print("Last name: ");
        lastName = sc.nextLine();
        return lastName;
    }

    public String getHomeAddress() {
        System.out.print("Home address: ");
        homeAddress = sc.nextLine();
        return homeAddress;
    }

    public String getBusinessPhone() {
        System.out.print("Business phone number: ");
        businessPhone = sc.nextLine();
        return businessPhone;
    }

    public String getHomePhone() {
        System.out.print("Home phone number: ");
        homePhone = sc.nextLine();
        return homePhone;
    }

    public String getCellPhone() {
        System.out.print("Cell phone number: ");
        cellPhone = sc.nextLine();
        return cellPhone;
    }

    public String getSkypeId() {
        System.out.print("Skype ID: ");
        skypeId = sc.nextLine();
        return skypeId;
    }

    public String getFacebookId() {
        System.out.print("Facebook ID: ");
        facebookId = sc.nextLine();
        return facebookId;
    }

    public String getPersonalWebSite() {
        System.out.print("Personal Website: ");
        personalWebSite = sc.nextLine();
        return personalWebSite;
    }

} // end of class AddressBook

AddressBookDemo.java

public class AddressBookDemo {

    public static void main(String[] args) {

        // Create two AddreeBook objects
        AddressBook name1 = new AddressBook();
        AddressBook name2 = new AddressBook();

        // Invoke methods on those objects
        System.out.println("Enter the first person's info");
        name1.getFirstName();
        name1.getMiddleName();
        name1.getLastName();
        name1.getHomeAddress();
        name1.getBusinessPhone();
        name1.getHomePhone();
        name1.getCellPhone();
        name1.getSkypeId();
        name1.getFacebookId();
        name1.getPersonalWebSite();

        System.out.println("Enter the second person's info");
        name2.getFirstName();
        name2.getMiddleName();
        name2.getLastName();
        name2.getHomeAddress();
        name2.getBusinessPhone();
        name2.getHomePhone();
        name2.getCellPhone();
        name2.getSkypeId();
        name2.getFacebookId();
        name2.getPersonalWebSite();

        // Compare names 
        String comp1 = name1.firstName + " " + name1.middleName.toUpperCase().charAt(0) + ". " + name1.lastName;
        String comp2 = name2.firstName + " " + name2.middleName.toUpperCase().charAt(0) + ". " + name2.lastName;
        if (comp1.equalsIgnoreCase(comp2)) {
            System.out.println("The names match!");
        } else {
            System.out.println("The names don't match!");
        } // end if/else statement

    } // end of Main

} // End of class AddressBookDemo

Thanks for everyone's help! Greatly appreciated.

Chris
  • 3
  • 2
  • in your AddressBook class you might want to split your getters and setters. `public String getFirstName() { return firstName; } public void setFirstName(){ this.firstName = sc.nextLine(); }` – tmaxxcar Feb 19 '16 at 00:35