Nub Nub - 1 month ago 33
Java Question

How to transfer funds from one bank account to another in Java?

Assignment:
Change the Account class so that funds can be moved form one account to another. Think of this as withdrawing money from one account and depositing it into another. Change the main method of Banking class to show this new service.

I am working on a bank account class that can deposit and withdraw money from bank account balances. I am working on the class part of the the assignment where you declare all the methods for the driver. My assignment wants me to make a method that will withdraw money from one account and deposit that money into another account. I already know how to withdraw and deposit, I just don't know how to transfer money from one account to another account. Here is my code for the transfer method so far:

import java.text.NumberFormat;
public class Account
{
private NumberFormat fmt = NumberFormat.getCurrencyInstance();
private final double RATE = 0.035; // interest rate of 3.5%
private long acctNumber;
private double balance;
private String name;
//-----------------------------------------------------------------
// Sets up the account by defining its owner, account number,
// and initial balance.
//-----------------------------------------------------------------
public Account (String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
//-----------------------------------------------------------------
// Validates the transaction, then deposits the specified amount
// into the account. Returns the new balance.
//-----------------------------------------------------------------
public double deposit (double amount)
{
if (amount < 0) // deposit value is negative
{
System.out.println ();
System.out.println ("Error: Deposit amount is invalid.");
System.out.println (acctNumber + " " + fmt.format(amount));
}
else
balance = balance + amount;
return balance;
}
//-----------------------------------------------------------------
// Validates the transaction, then withdraws the specified amount
// from the account. Returns the new balance.
//-----------------------------------------------------------------
public double withdraw (double amount, double fee)
{
amount += fee;
if (amount < 0) // withdraw value is negative
{
System.out.println ();
System.out.println ("Error: Withdraw amount is invalid.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
}
else
if (amount > balance) // withdraw value exceeds balance
{
System.out.println ();
System.out.println ("Error: Insufficient funds.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
System.out.println ("Available: " + fmt.format(balance));
}
else
balance = balance - amount;
return balance;
}

public double transfer (double amount, double fee)
{
amount += fee;
if (amount < 0) // withdraw value is negative
{
System.out.println ();
System.out.println ("Error: Withdraw amount is invalid.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
}
else
if (amount > balance) // withdraw value exceeds balance
{
System.out.println ();
System.out.println ("Error: Insufficient funds.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
System.out.println ("Available: " + fmt.format(balance));
}
else
balance = balance - amount;

//What should I put here to deposit the amount into another account?

if (amount < 0) // deposit value is negative
{
System.out.println ();
System.out.println ("Error: Deposit amount is invalid.");
System.out.println (acctNumber + " " + fmt.format(amount));
}
else
balance = balance + amount;
}

//-----------------------------------------------------------------
// Adds interest to the account and returns the new balance.
//-----------------------------------------------------------------
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}
//-----------------------------------------------------------------
// Returns the current balance of the account.
//-----------------------------------------------------------------
public double getBalance ()
{
return balance;
}
//-----------------------------------------------------------------
// Returns the account number.
//-----------------------------------------------------------------
public long getAccountNumber ()
{
return acctNumber;
}
//-----------------------------------------------------------------
// Returns a one-line description of the account as a string.
//-----------------------------------------------------------------
public String toString ()
{
return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
}
}

Answer

You need to variables for balance since they are different accounts otherwise you're just returning the money to the same place you just took it out from. In this case, assuming that you take out $100 and you put $100 into the other account then. This is the change i would make here, however i would need to see the other classes fully. It's hard when you ask a question about an assignment and don't provide the full code, just a snippet. We don't know how the classes work :)

public double transfer(double amount, double fee)
{
    boolean withdraw = false;
    // Withdraw:
    amount += fee;
    if (amount < 0) // withdraw value is negative
    {
        System.out.println();
        System.out.println("Error: Withdraw amount is invalid.");
        System.out.println("Account A: " + acctNumber);
        System.out.println("Requested: " + fmt.format(amount));
    }
    else if (amount > balanceAccountA) // withdraw value exceeds balance
    {
        System.out.println();
        System.out.println("Error: Insufficient funds.");
        System.out.println("Account A: " + acctNumber);
        System.out.println("Requested: " + fmt.format(amount));
        System.out.println("Available: " + fmt.format(balanceA));
    }
    else {
        balanceA = balanceA - amount;
        withdraw = true;
    }

    if (withdraw) {
        balanceB = balanceB + amount;           
    }
}

EDIT Now that you've update your code to include the full Account class, there is no purpose to your transfer method. Tranfer should be a method in your main class which instantiates the accounts. Example of a transfer would be.

public static void main(String[] args)
{
    Account a = new Account("userA", 123, 200);
    Account b = new Account("userB", 234, 500);

    // Tranfer
    a.withdraw(100, 5);
    System.out.println(a.getBalance());
    b.deposit(100);
    System.out.println(b.getBalance());
}

and turning this into a method would be

public static void transfer (Account from, Account to, double amount, double fee)
{
    from.withdraw(amount, fee);
    to.deposit(amount);
}