呼叫&比较多个实例方法

时间:2016-02-18 23:11:14

标签: java class methods encapsulation instances

我是编程新手,这是我的第一门课程。我很难理解整个概念,希望能得到一些关于如何继续的提示。

基本上我需要编写一个简单的地址簿程序,让用户输入2个(或更多?)用户的信息。我的课程辅助给了我以下建议:

在我的应用程序(演示或测试)类的主要方法中 - 创建类AddressBook的两个或多个实例/对象 - 使用实例(对象)来调用AddressBook类的实例方法 - 使用2个实例/对象来比较他们的名字

我目前有以下内容:

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

正如您所看到的,我在尝试初始化类AddressBook()的两个实例时遇到了构造函数错误。如果我尝试添加一个'null'的arg,我在调用方法后测试输出时只返回'null'。我还在main中创建了compareName()方法但是还没有调用它,因为我甚至无法使第一部分工作。另外我有点困惑我应该如何将name1.firstName和name2.firstName的值发送回main(或者我应该只是将值存储到另一个变量中并在每次getFirstName()时创建一个增量循环叫什么?)。

2 个答案:

答案 0 :(得分:0)

在您的主要课程中,您需要传入您希望存储在地址簿中的值,即

    double numberOfMinutes       = 0.00;
    double additionalMinutesCost = 0.60;
    double totalCost             = 0.00;
    double cost                  = 0.00;
    double extra                 = 0.00;
    double teleCost              = 2.50;

    //Display banner
    System.out.print("Welcome to Kenneth's Telephone Company\nBill Calculator\n\n");

    //Promt user for minutes
    System.out.print("Please enter the Amount of minutes: ");
    numberOfMinutes = keyboard.nextDouble();

    if (numberOfMinutes ==0) {

        System.out.print("Amount due: $0\n\n");
        System.out.print("Thank you for working with us\n");

    } else {

        //Check number of minutes
        if(numberOfMinutes >=1 && numberOfMinutes <=3) {

            System.out.println("Amount due: $2.50\n"); 
            System.out.print("Thank you for working with us\n");

        } else  {

            //Calculate additonal cost
            extra = numberOfMinutes -3;
            cost = additionalMinutesCost * extra;
            totalCost =  cost + teleCost;

            System.out.printf("Amount due: $%.2f\n\n", totalCost); 
            System.out.println("Thank you for working with us");
        }
    }

这将使用上述信息设置AddressBook name1 = new AddressBook("firstName", "middleName", "lastName", "homeAddress", "businessPhone", "homePhone", "cellPhone", "skypeId", "facebookId", "personalWebSite"); 类型name1的新AddressBook。您可能还需要考虑创建类型为AddressBook的ArrayList,它将存储AddressBook类的许多实例。

ArrayList<AddressBook> myAddressBook = new ArrayList();

然后你需要将一个对象添加到ArrayList:

myAddressBook.add(new AddressBook(("firstName", "middleName", 
        "lastName", "homeAddress", "businessPhone",
        "homePhone", "cellPhone", "skypeId",
        "facebookId", "personalWebSite"));

答案 1 :(得分:0)

这是我最终使用这里给出的一些建议。我将坚持只有2个联系人,因为这是作业所要求的;

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

感谢大家的帮助!非常感谢。