title: Bank Account TransferTo Method
body: <p>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?</p>
  
  <p><strong>This is my class with contructors and methods:</strong></p>
  
  <pre><code>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 &lt;= balance) {
          withdraw(x);
          bank.deposit(x);
          System.out.println("\nTransfer succesful. Tansfered: $" + bank.getBalance());
      } else if (x &gt; balance) {
          System.out.println("\nTransfer failed, not enough balance!");
      }
  }
  }
  </code></pre>
  
  <p><strong>This is my class with the main method</strong></p>
  
  <pre><code>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());
  }
  }
  </code></pre>