简体   繁体   中英

Having trouble call a method from another class

My assignment is to create an address book and there are multiple classes. I'm having trouble figuring out how to call a method from a class that does not contain the main method. So that you know what I'm asking, here is a list of the classes I need to have in my program:

  • BaseContact (generic info, name, number, etc)

  • Person (info specific to a person)

  • Business (info specific to a business)

  • Location (address: building number, street, city, state, zip)

  • Photo (photo ID number, file name, date of photo, and description)

  • AddressBook (Contains list of all Contacts. Methods neede: add, remove, display one, sort, and search for contacts by any of the properties of the contacts.

  • DataAccessService – This class will have methods to readAllContacts and saveAllContacts. Implement this class as an interface.

  • FileAccessService – This will implement the DataAccessService interface. The contacts will be saved to a text file and read from a text file.

  • DatabaseAccessService – This will also implement the DataAccessService interface. The contacts will be read and written to a table in a SQL database.

  • ConsoleApp – Has a main() method. In main(), you can create instances of the other classes in the application.

Below is the code, so far, for the classes I'm inquiring about

BaseContact class:

Public class BaseContact {
        private String phoneNumber;
        private String name;

    public BaseContact(String phoneNumber, String Name) {
        this.phoneNumber = phoneNumber;
        this.name = name;
    }

    public String getPhoneNumber() {return phoneNumber;}

    public void setPhoneNumber(String phoneNumber) {this.phoneNumber = 
                               phoneNumber;}

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    }

PersonContact Class:

public class PersonContact extends BaseContact {
private String dob;
private String description;

public PersonContact(String dob, String description, String phoneNumber, String name) {
    super(phoneNumber, name);
    this.dob = dob;
    this.description = description;
}

public String getDob() {return dob;}

public void setDob(String dob) {this.dob = dob;}

public String getDescription() {return description;}

public void setDescription(String description) {this.description = description;}

}

BusinessContact Class:

public class BusinessContact extends BaseContact {
private String busHours;
private String website;

public BusinessContact(String busHours, String website, String phoneNumber, String name) {
    super(phoneNumber, name);
    this.busHours = busHours;
    this.website = website;
}

public String getBusHours() {return busHours;}

public void setBusHours(String busHours) {this.busHours = busHours;}

public String getWebsite() {return website;}

public void setWebsite(String website) {this.website = website;}

}

Location Class:

public class Location {
private String buildingNum;
private String additionalAddr;
private String street;
private String city;
private String state;
private int zip;

public Location(String buidingNum, String additionalAddr, String street, String city, String state, int zip) {
    this.buildingNum = buildingNum;
    this.additionalAddr = additionalAddr;
    this.street = street;
    this.city = city;
    this.state = state;
    this.zip = zip;
}

}

Photo Class:

public class Photo {
private String idNumber;
private String fileName;
private String photoDate;
private String description;

}

I understand that I will just extend the BaseContact class in the PersonContact and BusinessContact classes, but I'm not sure how to link the Photo and Location classes to the contacts. Will I be calling the Location and Photo classes from the BaseContact class or from the PersonContact and BusinessContact classes? Once I do that, how do I pair locations with the correct contact?

It sounds like your instincts are good: it makes sense that a Person would be a type of BaseContact , as well as a Business . Try thinking about each of the other classes in turn: Is a Location a type of contact? Or something that belongs to a contact? Does a Person have a Photo ? Does a Business ?

Something that could be considered part of or "owned by" another class could perhaps be member data for that class. Another aspect to think through is whether a class can contain only one of something, or any number of them. (I hope your instructor is introducing the basics of Java's generic collections!)

I hope this gives you a start on figuring out how to handle this assignment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM