Bank Account Transfer Project Java

user1691618 picture user1691618 · Oct 6, 2012 · Viewed 9.3k times · Source

I'm doing a project in Java, and I'm stuck on part of it. I have the deposit function working in SavingsAccount class, but I can't seem to figure out how to call it in the engine class. For our project, we have to allow the user to create multiple bank accounts and transfer funds between them, using the BlueJ virtual machine. I'll post the related code for my engine class and savings account class... thanks, any help would be appreciated!

Problem: I can't get the money to transfer from one account to another, I get an error message on the engine class. I think I'm doing something wrong with the account I'm sending money to...

Savings Account Code

public class SavingsAccount extends BankAccount
public void transfer (BankAccount that, double amount) 
 {
   if 
   (balance-amount < -80)
   balance = balance ;
   else
   {
       if 
       (amount <= balance)
            {
                this.balance = this.balance - amount;
                that.balance = that.balance + amount;
            }
       else
            {
               this.balance = this.balance - amount-20;
               that.balance = that.balance + amount;
            }
    }
 }

Engine Class

public class engine
{
 SavingsAccount savings1 = new SavingsAccount();
 savings1.balance = 0;

 //code for other choices, such as deposit and withdraw... 

    if (selection2 == 3)
       {
          System.out.println ("How much would you like to transfer?");
          int transferAmount = in.nextInt ( );
          System.out.println ("Which account would you like to transfer the money to?");
          String thatAccount = in.next();
          savings1.withdraw (transferAmount);
          thatAccount.deposit (transferAmount);
          System.out.println ("You account balance is " + savings1.getBalance () + "!");

      }

Answer

Yogendra Singh picture Yogendra Singh · Oct 6, 2012

I have some obervation/suggestions as below:

Your transferAccount thatAccount is a String String thatAccount = in.next();. How can you call deposit () method on that?

I don't see deposit() and withdraw() methods in SavingsAccount class, hope there are present in BankAccount class.

Now sure how you are initializing the balance as saving1.balance=0;. It should be done through some class method e.g. setBalance as saving1.setBalance(0);.

When you are invoking savings1.withdraw() method, the balance is 0.

Hope these will help you in identifying your issue and correcting the program.