BankAccount

01: /**
02: A bank account has a balance that can be changed by
03: deposits and withdrawals.
04: */
05: publicclassBankAccount
06: {
07: /**
08: Constructs a bank account with a zero balance.
09: */
10: publicBankAccount()
11: {
12: balance=0;
13: }
14:
15: /**
16: Constructs a bank account with a given balance.
17: @param initialBalance the initial balance
18: */
19: publicBankAccount(doubleinitialBalance)
20: {
21: balance=initialBalance;
22: }
23:
24: /**
25: Deposits money into the bank account.
26: @param amount the amount to deposit
27: */
28: publicvoiddeposit(doubleamount)
29: {
30: balance=balance+amount;
31: }
32:
33: /**
34: Withdraws money from the bank account.
35: @param amount the amount to withdraw
36: */
37: publicvoidwithdraw(doubleamount)
38: {
39: balance=balance-amount;
40: }
41:
42: /**
43: Gets the current balance of the bank account.
44: @return the current balance
45: */
46: publicdoublegetBalance()
47: {
48: returnbalance;
49: }
50:
51: /**
52: Transfers money from the bank account to another account
53: @param amount the amount to transfer
54: @param other the other account
55: */
56: publicvoidtransfer(doubleamount,BankAccountother)
57: {
58: withdraw(amount);
59: other.deposit(amount);
60: }
61:
62: privatedoublebalance;
63: }

Post a Comment

0 Comments