Search code examples
javamethodstransfer

Bank Account TransferTo Method


Ok I am having problems creating a transfer to method to transfer "money" from one account to the next. After the transfer the amounts of each account would be printed out. I created the code but when I run it the amount transferred is set to the bank account amount. It should just deduct from one account and add to the next account. What am I doing wrong?

This is my class with contructors and methods:

public class Account {
// Instance variables
private double balance;

// Constructors
public Account(double initialBalance) {
    balance = initialBalance;
}

public Account(int account, double balance) {
    this.balance = balance;
}

public Account() {
    balance = 0.0;
}

// Instance methods

public void setBalance() {
    balance = Math.random() * 1000;
    balance = Math.round((balance * 100.0)+.5) / 100.0;
}

public void deposit(double amount) {
    balance = balance + amount;
}

public void withdraw(double amount) {
    balance = balance - amount;
}

public double getBalance() {
    balance = Math.round((balance * 100.0)+.5) / 100.0;
    return balance;
}

public void close() {
    balance = 0.0;
}

public void transferTo(Account bank, double x) {
    if (x <= balance) {
        withdraw(x);
        bank.deposit(x);
        System.out.println("\nTransfer succesful. Tansfered: $" + bank.getBalance());
    } else if (x > balance) {
        System.out.println("\nTransfer failed, not enough balance!");
    }
}
}

This is my class with the main method

public class MyBank {

public static void main(String[] args) {

    Account[] bank = { new Account(), new Account(), new Account(),
            new Account() };

    bank[0].setBalance();
    bank[1].setBalance();
    bank[2].setBalance();
    bank[3].setBalance();

    System.out.println("Accounts 1 balance is: $" + bank[0].getBalance());
    System.out.println("Accounts 2 balance is: $" + bank[1].getBalance());
    System.out.println("Accounts 3 balance is: $" + bank[2].getBalance());
    System.out.println("Accounts 4 balance is: $" + bank[3].getBalance());

    double x = Math.random()*100;
    bank[0].transferTo(bank[1], x);
    System.out.println("Remaining balance of Account 1: $" + bank[0].getBalance());
    System.out.println("Remaining balance of Account 2: $" + bank[1].getBalance());

    double y = (Math.random()*300);
    bank[2].transferTo(bank[3], y);
    System.out.println("Remaining balance of Account 3: $" + bank[2].getBalance());
    System.out.println("Remaining balance of Account 4: $" + bank[3].getBalance());
}
}

Solution

  • One issue you did have, is that you were accidentally printing the BALANCE rather than the amount actually transferred, in your transferTo() method. I fixed this. On another note, if you're going to be printing these numbers out, look into printf() to make them appear as actual dollar amounts. I did it to one of the lines to show you an example.

    PS - Try NOT using Math.random() for stuff when you're trying to debug.

    public void transferTo(Account bank, double x) {
        if (x <= this.balance) {
            withdraw(x);
            bank.deposit(x);
            System.out.println("\nTransfer succesful. Tansfered: $" + x);
        } else { //does not need to be else if, because if not <=, it MUST be >.
            System.out.println("\nTransfer failed, not enough balance!");
        }
    }
    

    I can verify that the method works perfectly fine, after these edits, on my machine. This is what I used to test:

    public class MyBank {
    
    public static void main(String[] args) {
    
        Account[] bank = { new Account(), new Account(), new Account(),
                new Account() };
    
        bank[0].setBalance();
        bank[1].setBalance();
        bank[2].setBalance();
        bank[3].setBalance();
    
        double x = 100.00;
        System.out.printf("Transferring $%.2f from Acc 1 to Acc 2.\n", x); //printf example
        System.out.println("Acc 1 previous balance: " + bank[0].getBalance());
        System.out.println("Acc 2 previous balance: " + bank[1].getBalance());
        bank[0].transferTo(bank[1], x);
        
        
        System.out.println("Acc 1 new balance: " + bank[0].getBalance());
        System.out.println("Acc 2 new balance: " + bank[1].getBalance());
            
            
    }
    }
    

    And lastly, my output:

    Transferring $100.00 from Acc 1 to Acc 2.

    Acc 1 previous balance: 106.76

    Acc 2 previous balance: 266.18

    Transfer succesful. Transfered: $100.0

    Acc 1 new balance: 6.77

    Acc 2 new balance: 366.19