II am having so much trouble getting this method: A method in SmallBank named printBankDetailsthat prints out the details of the stored BankAccounts. This method should work in the case that there is only one BankAccount as well as in the case that there are two BankAccounts. You can check whether there is a value in the account2 instance variable as follows. I have no idea how to get the value of the primitive types I created, account1 and account2 then print that value. Any suggestions?
public class SmallBank
{
private BankAccount account1;
private BankAccount account2;
public SmallBank(String name, int balance)
{
account1 = new BankAccount(name, balance);
account2 = null;
}
public void addSecondAccount(BankAccount newAccount)
{
account2 = newAccount;
}
public void printBlankDetails()
{
account1.getDeclaredFields();
String name = field.getName();
Int balance = field.getBalance();
System.out.println(b);
}
}
An alternate design is to have say
printDetails()in theBankAccountclass itself. Something like:and then use it in your
SmallBank::printBlankDetails()method as follows:IMO, this approach provides better encapsulation, hides the details of
BankAccountfromSmallBankclass.