-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.java
More file actions
120 lines (107 loc) · 3.18 KB
/
BankAccount.java
File metadata and controls
120 lines (107 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
public class BankAccount{
private double balance;
private String accountNumber;
private Customer accountHolder;
/**
* gets this account's balance
*/
public double getBalance(){
return balance;
}
/**
* gets this account's number
*/
public String getAccountNumber(){
return accountNumber;
}
/**
* gets the customer acting as account holder
*
* potential privacy leak; returns account holder Customer object's direct address
*/
public Customer getAccountHolder(){
return accountHolder;
}
/**
* returns formatted string of this account holder's name, ID and this bank account's balance and number
*/
public String toString(){
return "("+accountHolder+") " + accountNumber + ": " + balance;
}
/**
* adds positive amount to this account's balance
*/
public void deposit(double cash){
if (cash > 0.0)balance += cash;
}
/**
* removes positive amount from this account's balance
* prevents removal if it results in a negative balance
*/
public void withdraw(double cash){
if ((cash > 0.0) && (cash <= balance) && (balance > 0.0))balance -= cash;
}
/**
* withdraws positive amount from this account into a given account
* prevents transaction if it results in a negative balance
*
* potential privacy leak;
*/
public void transfer(double amount, BankAccount otherBankAccount){
if (amount > 0.0 && amount <= this.balance){
withdraw(amount);
otherBankAccount.deposit(amount);
}
}
/**
* sets this account's account holder
*
* potential privacy leak; account holder Customer object must be declared outside of this method
* thus reference to Customer object may exist outside of this account
*/
public void setAccountHolder(Customer newAccountHolder){
this.accountHolder = newAccountHolder;
}
/**
* default constructor
* sets balance to 0.0 and account number to "8888"
*/
public BankAccount(){
this(0.0);
}
/**
* constructor given starting balance
* sets account number to "8888"
*/
public BankAccount(double balance){
this(balance, "8888");
}
/**
* constructor given starting balance and accountNumber
*/
public BankAccount(double balance, String accountNumber){
this.balance = balance;
this.accountNumber = accountNumber;
}
/**
* constructor given starting balance and the account holder
*
* potential privacy leak; account holder Customer object must be declared outside of constructor
* thus reference to Customer object may exist outside of this account
*/
public BankAccount(Customer accountHolder, double balance){
setAccountHolder(accountHolder);
this.balance = balance;
}
/**
* copy constructor
* copies balance, account number and account holder
*
* potential privacy leak; this copy account's account holder references same Customer object as the account to copy from
*/
public BankAccount(BankAccount copy){
this.balance = copy.balance;
this.accountNumber = copy.accountNumber;
this.accountHolder = copy.accountHolder;
}
}