-4

I'm doing an address book as part of a school assignment and I have it mostly figured out, but I'm stuck on one part. Here's my code first. I have an AddressBook class that contains variables and appropriate getter and setter methods to change/return the information for each person in the book. Then I have a TestAddressBook class with a method main that demonstrates how to address book works. The only thing I'm stuck on is the final requirement, where I'm asked to compare two names and see if they're equal. Here's the code for the classes.

public class AddressBook {

private String firstName;
private String middleName;
private String lastName;
private String homeAddress;
private String homePhone;
private String cellPhone;
private String businessPhone;
private String skypeId;
private String facebookId;
private String personalWebSite;

public AddressBook(String firstName, String middleName, String lastName,
                   String homeAddress, String homePhone, String cellPhone,
                   String businessPhone, String skypeID, String facebookID,
                   String personalWebSite) {

                        this.firstName = firstName;
                        this.middleName = middleName;
                        this.lastName  = lastName;
                        this.homeAddress = homeAddress;
                        this.homePhone = homePhone;
                        this.cellPhone = cellPhone;
                        this.businessPhone = businessPhone;
                        this.skypeId = skypeID;
                        this.facebookId = facebookID;
                        this.personalWebSite = personalWebSite;
                        }

public String getFirstName() {
    return firstName;
}

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

public String getMiddleName() {
    return middleName;
}

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

public String getLastName() {
    return lastName;
}

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

public String getHomeAddress() {
    return homeAddress;
}

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

public String getHomePhone() {
    return homePhone;
}

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

public String getCellPhone() {
    return cellPhone;
}

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

public String getBusinessPhone() {
    return businessPhone;
}

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

public String getSkypeId() {
    return skypeId;
}

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

public String getFacebookId() {
    return facebookId;
}

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

public String getPersonalWebSite() {
    return personalWebSite;
}

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

public static String compareNames(String name1, String name2) {

}

}

Then my test class...

public class TestAddressBook {

public static void main(String[] args) {

    System.out.println("ENTRY 1");

    AddressBook name1 = new AddressBook("Robert", "James", "Smith", "3 Fake St", 
                                        "222-321-8371", "222-423-2382", 
                                        "222-438-2918", "bob483", "bobfb493",
                                        "http://www.freewebhost.com/bob848");

    System.out.println( "First Name:        " + name1.getFirstName() );
    System.out.println( "Middle Name:       " + name1.getMiddleName() );
    System.out.println( "Last  Name:        " + name1.getLastName() );
    System.out.println( "Address:           " + name1.getHomeAddress() );
    System.out.println( "Home Phone:        " + name1.getHomePhone() );
    System.out.println( "Cell Phone:        " + name1.getCellPhone() );
    System.out.println( "Business Phone:    " + name1.getBusinessPhone() );
    System.out.println( "Skype ID:          " + name1.getSkypeId() );
    System.out.println( "Facebook ID:       " + name1.getFacebookId() );
    System.out.println( "Personal Website:  " + name1.getPersonalWebSite() );

    System.out.println("\nENTRY 2");

    AddressBook name2 = new AddressBook("Bruce", "Allan", "Carter", "56 Outtamy Way", 
                                        "564-342-8372", "564-283-9832", 
                                        "564-293-3489", "brucie392", "brucieface28",
                                        "http://www.freewebhost.com/carteristheman");

    System.out.println( "First Name:        " + name2.getFirstName() );
    System.out.println( "Middle Name:       " + name2.getMiddleName() );
    System.out.println( "Last  Name:        " + name2.getLastName() );
    System.out.println( "Address:           " + name2.getHomeAddress() );
    System.out.println( "Home Phone:        " + name2.getHomePhone() );
    System.out.println( "Cell Phone:        " + name2.getCellPhone() );
    System.out.println( "Business Phone:    " + name2.getBusinessPhone() );
    System.out.println( "Skype ID:          " + name2.getSkypeId() );
    System.out.println( "Facebook ID:       " + name2.getFacebookId() );
    System.out.println( "Personal Website:  " + name2.getPersonalWebSite() );

    System.out.println("\nENTRY 3");

    AddressBook name3 = new AddressBook("Susan", "Anne", "Peters", "6 Madeup Blvd", 
                                        "736-453-1238", "736-392-2385", 
                                        "736-926-2439", "anniep", "susananne",
                                        "http://www.freewebhost.com/Susanspage");

    System.out.println( "First Name:        " + name3.getFirstName() );
    System.out.println( "Middle Name:       " + name3.getMiddleName() );
    System.out.println( "Last  Name:        " + name3.getLastName() );
    System.out.println( "Address:           " + name3.getHomeAddress() );
    System.out.println( "Home Phone:        " + name3.getHomePhone() );
    System.out.println( "Cell Phone:        " + name3.getCellPhone() );
    System.out.println( "Business Phone:    " + name3.getBusinessPhone() );
    System.out.println( "Skype ID:          " + name3.getSkypeId() );
    System.out.println( "Facebook ID:       " + name3.getFacebookId() );
    System.out.println( "Personal Website:  " + name3.getPersonalWebSite() );






}

}

The assignment is asking for this...

"Using the get and set methods, create a comparison method compareNames(name1, name2) that compares the first, middle, and last names of strings name1 and name2. Assume that name1 and name2 follow the following format: “FirstName M. LastName”."

The assignment requires that the two strings be compared and return something along the lines of "Robert J. Smith and Bruce A. Carter are not equal names." Can anyone help with this?

ripp2k
  • 1
  • 4
  • Added it. Java. Sorry! – ripp2k Mar 09 '17 at 02:08
  • Why school is crap *create a comparison method compareNames(name1, name2) * there's an interface for this `Comparator` stop teaching people how to do things the wrong way... with method references and Comparator.comparing... https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html – xenoterracide Mar 09 '17 at 02:31
  • See 9 http://www.baeldung.com/java-8-sort-lambda for a way to combine, or you could concatenate the strings first and then just compare that... comparators will return 0 for equal – xenoterracide Mar 09 '17 at 02:41

3 Answers3

0

I've just realized there's not a request for sorting here... so the simplest answer is, accounting for nulls

public  boolean compareNames( AddressBook a, AddressBook b) {
    if ( a == null || b == null ) {
       return false;
    }

    return Objects.equals( a.getFirstname(), b.getFirstname() )
           && Objects.equals( ... ); // fill in the rest
}

your spec does not account for whether Susan Anne Peterson and Susan Alice Peterson should be the same person (if they should substring the middle name, it implies this by shortening it). Use Objects.equals because it's null safe.

Arguably if this is truly the domain equivalence you want, you should actually implement equals and hashCode on AddressBook, for the correct way to do that see this article on equality. If you decide to implement equals, then you can verify its correctness with EqualsVerifier

if for some reason you wanted comparison/ordering, in a case insensitive null safe way

Comparator<String> nsci =Comparator.nullsFirst(String.CASE_INSENSTIVE_ORDER)
return Comparator.comparing(AddressBook::getFirstName, nsci )
   .thenComparing(AddressBook::getMiddleName, nsci )
   .thenComparing(AddressBook::getLastName, nsci ).compareTo( address1, address2 ); // if 0 they are equal
xenoterracide
  • 16,274
  • 24
  • 118
  • 243
0

First of all, let's format the names into "Firstname MiddleInitial. Lastname" so they can be compared easily. This can be done with the following code:

//Get each part of the name:
String firstname1 = entry1.getFirstName();
String middleInitial1 = entry1.getMiddleName().charAt(0);
String lastname1 = entry1.getLastName();
//Combine them
String fullname1 = firstname + " " + middleInitial + ". " + lastname;

If you wanted this to be more compact, you could combine the lines.

String fullname1 = entry1.getFirstName() + " " + entry1.getMiddleName().chatAt(0) + ". " + entry1.getLastName();

Repeat this step for the second entry, just changing the variable names.

Now, let's compare the two strings. This can be done with a simple if statement, such as the following:

if (fullname1.equalsIgnoreCase(fullname2) {
    System.out.println(fullname1 + " is equal to " + fullname2);
} else {
    System.out.println(fullname1 + " is not equal to " + fullname2);
}

Now that you have the code, let's make it into a method. This could be done with the following:

public static String (AddressBook entry1 AddressBook entry2) {
  String fullname1 = entry1.getFirstName() + " " + entry1.getMiddleName().charAt(0) + ". " + entry1.getLastName();
  String fullname2 = entry1.getFirstName() + " " + entry1.getMiddleName().charAt(0) + ". " + entry1.getLastName();
  if (fullname1.equalsIgnoreCase(fullname2) {
        return fullname1 + " is equal to " + fullname2;
    } else {
        return fullname1 + " is not equal to " + fullname2;
    }
}
  • On your second code snippet, use '.charAt(0)' not '.chatAt(0)' I know it's just a typo, but just to help the OP – Peake Mar 10 '17 at 02:59
  • That makes a lot of sense! How exactly would I make that method a part of my AddressBook class and then call it from my TestAddressBook class. The assignment asks for a method in the form of: public static String compareNames(String name1, String name2) – ripp2k Mar 10 '17 at 03:02
  • @Peake Thanks! I've fixed it. – Jake O'Brien Mar 11 '17 at 02:17
  • @LucasO'Connor To answer your question, I've edited my answer to show how to make that into a method. – Jake O'Brien Mar 11 '17 at 02:28
-1

To compare the contents of two strings just use string1.equals(string2); Which will return a boolean. Adding to that you may want to clean the string first. You could take both names and compare their lowercase forms by calling the .CASE_INSENSITIVE_ORDER constant on the strings. Hopefully this helps and I didn't give too much

Peake
  • 85
  • 1
  • 6
  • don't use toLowerCase, use [String.CASE_INSENSTIVE_ORDER](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#CASE_INSENSITIVE_ORDER) – xenoterracide Mar 09 '17 at 02:33
  • @xenoterracide What do that do? – Peake Mar 09 '17 at 02:36
  • it's a comparator that allows you to compare strings without case sensitivity, and presumbably doesn't behave in a surprising manner when you add unicode to the mix, which just doing toLowerCase might do, because when dealing with unicode, case is not a boolean. – xenoterracide Mar 09 '17 at 02:38
  • @xenoterracide Thanks for the input, I edited my answer – Peake Mar 09 '17 at 02:43
  • I get what you're saying, but I'm still having trouble wrapping my mind around this one. I feel like I'm missing something obvious, but I can't figure out how to implement this in my program. – ripp2k Mar 09 '17 at 04:16
  • @LucasO'Connor you could use something like 'if(string1.CASE_INSENSITIVE_ORDER.equals(string2.CASE_INSENSITIVE_ORDER)){ return string1 + " is the same as " + string2; }' – Peake Mar 10 '17 at 02:57