地址簿计划

时间:2017-03-31 21:25:35

标签: 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 AddressBook(String firstName) {
    this.firstName = firstName;
}

public AddressBook(String firstName, String middleName) {
    this.firstName = firstName;
    this.middleName = middleName;
}

public AddressBook(String firstName, String middleName, String lastName) {
    this.firstName = firstName;
    this.middleName = middleName;
    this.lastName = lastName;
}

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) {
    String nameEquals;
    if ( name1.equals(name2) ) {
        nameEquals = (name1 + " and " + name2 + " are equal names.");
        return nameEquals;
    }
    else {
        nameEquals = (name1 + " and " + name2 + " are not equal names.");
        return nameEquals;
    }
}

}

测试班......

public class TestAddressBook {

public static void main(String[] args) {

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

    AddressBook entry1 = 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:        " + entry1.getFirstName() );
    System.out.println( "Middle Name:       " + entry1.getMiddleName() );
    System.out.println( "Last  Name:        " + entry1.getLastName() );
    System.out.println( "Address:           " + entry1.getHomeAddress() );
    System.out.println( "Home Phone:        " + entry1.getHomePhone() );
    System.out.println( "Cell Phone:        " + entry1.getCellPhone() );
    System.out.println( "Business Phone:    " + entry1.getBusinessPhone() );
    System.out.println( "Skype ID:          " + entry1.getSkypeId() );
    System.out.println( "Facebook ID:       " + entry1.getFacebookId() );
    System.out.println( "Personal Website:  " + entry1.getPersonalWebSite() );

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

    AddressBook entry2 = 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:        " + entry2.getFirstName() );
    System.out.println( "Middle Name:       " + entry2.getMiddleName() );
    System.out.println( "Last  Name:        " + entry2.getLastName() );
    System.out.println( "Address:           " + entry2.getHomeAddress() );
    System.out.println( "Home Phone:        " + entry2.getHomePhone() );
    System.out.println( "Cell Phone:        " + entry2.getCellPhone() );
    System.out.println( "Business Phone:    " + entry2.getBusinessPhone() );
    System.out.println( "Skype ID:          " + entry2.getSkypeId() );
    System.out.println( "Facebook ID:       " + entry2.getFacebookId() );
    System.out.println( "Personal Website:  " + entry2.getPersonalWebSite() );

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

    AddressBook entry3 = 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:        " + entry3.getFirstName() );
    System.out.println( "Middle Name:       " + entry3.getMiddleName() );
    System.out.println( "Last  Name:        " + entry3.getLastName() );
    System.out.println( "Address:           " + entry3.getHomeAddress() );
    System.out.println( "Home Phone:        " + entry3.getHomePhone() );
    System.out.println( "Cell Phone:        " + entry3.getCellPhone() );
    System.out.println( "Business Phone:    " + entry3.getBusinessPhone() );
    System.out.println( "Skype ID:          " + entry3.getSkypeId() );
    System.out.println( "Facebook ID:       " + entry3.getFacebookId() );
    System.out.println( "Personal Website:  " + entry3.getPersonalWebSite() );



    AddressBook.compareNames(entry1.getFirstName() + " " + 
                             entry1.getMiddleName().charAt(0) + " " + 
                             entry1.getLastName(), 
                             entry2.getFirstName() + " " + 
                             entry2.getMiddleName().charAt(0) + " " + 
                             entry2.getLastName() ); 







}

}

1 个答案:

答案 0 :(得分:0)

你好好看看这个

package teteo;

public class TestAddressBook {

    public static void main(String[] args) {

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

        AddressBook entry1 = 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:        " + entry1.getFirstName() );
        System.out.println( "Middle Name:       " + entry1.getMiddleName() );
        System.out.println( "Last  Name:        " + entry1.getLastName() );
        System.out.println( "Address:           " + entry1.getHomeAddress() );
        System.out.println( "Home Phone:        " + entry1.getHomePhone() );
        System.out.println( "Cell Phone:        " + entry1.getCellPhone() );
        System.out.println( "Business Phone:    " + entry1.getBusinessPhone() );
        System.out.println( "Skype ID:          " + entry1.getSkypeId() );
        System.out.println( "Facebook ID:       " + entry1.getFacebookId() );
        System.out.println( "Personal Website:  " + entry1.getPersonalWebSite() );

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

        AddressBook entry2 = new AddressBook("Robert", "James", "Smith", "56 Outtamy Way", 
                                             "564-342-8372", "564-283-9832", 
                                             "564-293-3489", "brucie392", "brucieface28",
                                             "http://www.freewebhost.com/carteristheman");

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

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

        AddressBook entry3 = 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:        " + entry3.getFirstName() );
        System.out.println( "Middle Name:       " + entry3.getMiddleName() );
        System.out.println( "Last  Name:        " + entry3.getLastName() );
        System.out.println( "Address:           " + entry3.getHomeAddress() );
        System.out.println( "Home Phone:        " + entry3.getHomePhone() );
        System.out.println( "Cell Phone:        " + entry3.getCellPhone() );
        System.out.println( "Business Phone:    " + entry3.getBusinessPhone() );
        System.out.println( "Skype ID:          " + entry3.getSkypeId() );
        System.out.println( "Facebook ID:       " + entry3.getFacebookId() );
        System.out.println( "Personal Website:  " + entry3.getPersonalWebSite() );



      String equal =   AddressBook.compareNames(entry1.getFirstName() + " " + 
                                 entry1.getMiddleName().charAt(0) + " " + 
                                 entry1.getLastName(), 
                                 entry2.getFirstName() + " " + 
                                 entry2.getMiddleName().charAt(0) + " " + 
                                 entry2.getLastName() ); 

System.out.println(equal);


}
}

现在它是平等的,但你必须知道为了做到这一点,你应该使用

compareTo()或类比看看

https://www.javacodegeeks.com/2013/03/difference-between-comparator-and-comparable-in-java.html

如果使用地图会发生什么?它取决于一个键,你会得到重复的值,我的意思是如果你不会使用它你的代码是好的,但如果你将使用它,那么看看那里:)