比较地址簿程序中的名称

时间:2017-03-09 01:58:19

标签: java addressbook

我正在做一本地址簿作为学校作业的一部分而且我已经把它弄清楚了,但我只是坚持了一部分。这是我的代码。我有一个AddressBook类,它包含变量和适当的getter和setter方法,用于更改/返回书中每个人的信息。然后我有一个带有方法main的TestAddressBook类,它演示了如何处理书籍。我唯一坚持的是最终要求,我被要求比较两个名字,看看它们是否相等。这是类的代码。

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

}

}

然后是我的考试班......

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






}

}

作业要求这个......

“使用get和set方法,创建比较方法compareNames(name1,name2),比较字符串name1和name2的名字,中间名和姓字。假设name1和name2遵循以下格式:”FirstName M .LastName“。”

这项任务要求对这两个字符串进行比较,然后返回“罗伯特·J·史密斯和布鲁斯·A·卡特不等名”的字样。任何人都可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

我刚刚意识到这里没有排序请求......所以最简单的答案是,考虑空值

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
}

您的规范未考虑Susan Anne PetersonSusan Alice Peterson是否应该是同一个人(如果他们应该对中间名进行子串,则通过缩短它来暗示这一点)。使用Objects.equals,因为它是无效的。

可以说,如果这确实是您想要的域等效,那么您应该在equals上实现hashCodeAddressBook,以正确的方式查看this article on equality。如果您决定实施等于,则可以使用EqualsVerifier

验证其正确性

如果由于某种原因你想要比较/排序,用不区分大小写的null安全方式

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

答案 1 :(得分:0)

首先,让我们将名称格式化为&#34; Firstname MiddleInitial。姓氏&#34;所以他们可以轻松比较。 这可以使用以下代码完成:

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

如果你想让它更紧凑,你可以组合这些线。

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

对第二个条目重复此步骤,只需更改变量名称。

现在,让我们比较两个字符串。这可以使用简单的if语句完成,例如:

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

现在你已经拥有了代码,让我们把它变成一个方法。这可以通过以下方式完成:

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

答案 2 :(得分:-1)

要比较两个字符串的内容,只需使用string1.equals(string2);,它将返回一个布尔值。除此之外,您可能需要先清理字符串。您可以通过调用字符串上的.CASE_INSENSITIVE_ORDER常量来获取这两个名称并比较它们的小写形式。希望这有帮助,而且我没有给出太多