1 /**
2 A bank account has a balance that can be changed by
3 deposits and withdrawals.
4 */
5 public class BankAccount
6 {
7 private double balance;
8
9 /**
10 Constructs a bank account with a zero balance.
11 */
12 public BankAccount()
13 {
14 balance = 0;
15 }
16
17 /**
18 Constructs a bank account with a given balance.
19 @param initialBalance the initial balance
20 */
21 public BankAccount(double initialBalance)
22 {
23 balance = initialBalance;
24 }
25
26 /**
27 Deposits money into the bank account.
28 @param amount the amount to deposit
29 */
30 public void deposit(double amount)
31 {
32 balance = balance + amount;
33 }
34
35 /**
36 Withdraws money from the bank account.
37 @param amount the amount to withdraw
38 */
39 public void withdraw(double amount)
40 {
41 balance = balance - amount;
42 }
43
44 /**
45 Gets the current balance of the bank account.
46 @return the current balance
47 */
48 public double getBalance()
49 {
50 return balance;
51 }
52
53 /**
54 Transfers money from this account and tries to add it
55 @param amount the amount of money to transfer
56 @param otherBalance balance to add the amount to
57 */
58 void transfer(double amount, double otherBalance)
59 {
60 balance = balance - amount;
61 otherBalance = otherBalance + amount;
62 // Won’t update the argument
63 }
64
65 /**
66 Transfers money from this account to another.
67 @param amount the amount of money to transfer
68 @param otherAccount account to add the amount to
69 */
70 public void transfer(double amount, BankAccount otherAccount)
71 {
72 balance = balance - amount;
73 otherAccount.deposit(amount);
74 }
75
76 public void transfer2(double amount, BankAccount otherAccount)
77 {
78 balance = balance - amount;
79 double newBalance = otherAccount.balance + amount;
80 otherAccount = new BankAccount(newBalance); // Won’t work
81 }
82 }