Address class being called from Bank Class

2.7k views Asked by At

I have 2 classes that extend for each other. I have a mutator method that is calling to the class Address.Java to get the city and state from the class. However the Bank.java class is giving me problems. the specific method giving me problems is

public void setBankAddress(String aCity, String aState)(code is below) I dont know if this makes sense {

}

Bank.Java

public class Bank
{
     private String bankName;
     private int bankID;
     private Address bankAddress;

     public Bank()
     {
         bankName = "?";
         bankID = 0;
         bankAddress = new Address();
     }   

     public String getBankName()
     {
         return bankName;
     }

     public int getBankID()
     {
         return bankID;
     }

     public Address getBankAddress()
     {
         return bankAddress;
     }

     public void setBankName(String aBankName)
     {
         bankName = aBankName;
     }

     public void setBankID(int aBankID)
     {
         bankID = aBankID;
     }

     public void setBankAddress(String aCity, String aState)
     {

     }

     public String toString()
     {
         return "\nBank name:\t\t" + bankName + "\nBank ID:\t\t" + bankID + "\nBank    Address:\t\t" + bankAddress + "\n\n";
      }

}

Address. Java

public class Address
{
    private String city;
    private String state;

    public Address()
    {
    city = "?";
    state = "?";
    }

    public String getCity()
    {
    return city;
    }

    public String getState()
    {
    return state;
    }

    public void setCity(String aCity)
    {
    city = aCity;
    }

    public void setState(String aState)
    {
    state = aState;
    }

    public String toString()
    {
    return city + "," + state;
    }

}
1

There are 1 answers

0
Sotirios Delimanolis On

Do you just not know how to call methods on objects?

public void setBankAddress(String aCity, String aState)
{
    bankAddress.setCity(aCity);
    bankAddress.setState(aState);
}

bankAddress is already initialized in the constructor so you can safely call these methods.

However, it's usually good practice to do that outside such methods and pass the whole object itself

Address someAddress = new Address();
someAddress.setCity(aCity);
someAddress.setState(aState);

Bank bank = new Bank();
bank.setBankAddress(someAddress);
...
public void setBankAddress(Address aBankAddress) {
    bankAddress = aBankAddress;
}