-
Array List
I am working on a bank account program. There is Bank.java, BankAccount.java, and BankTester.java. I am trying to finish up a deposit method in the Bank class but am running into problems when trying to change the current balance to the new one. Here is my code for the method:
//PART B: Deposit funds
public void deposit(int accountNumber, double amount)
{
BankAccount a = accounts.get(accountNumber);
double bal = a.getBalance();
bal = (bal + amount);
//How do I make bal the current balance for the account??
}:confused:
-
BankAccount a = accounts.get(accountNumber);
does get(int) return an instance of BankAccount? And what is "accounts"?... Actually why are you making a new instance of BankAccount at all? Just use the BankAccount that calls the method.
//How do I make bal the current balance for the account??
does the Account have a field for its balance? Then you could just do (this.)balanceField = amount + (this.)getBalance;
where amount is your parameter in the method. I just put (this.) so you know it belongs to the calling Object. Hope that helped.
-MK12
-
First of all, when you post next time please use an appropriate title in your question. Here your title and the question not make sense.
If you want to call a method from a class of another what you have to do basically? Create an instance of it. Then call the method. If that methods returns anything you have to store it in a correct data type variable. Do you have a clear idea about that?