-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomer.java
159 lines (137 loc) · 4.55 KB
/
Customer.java
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.util.*;
public class Customer extends BankUser {
ArrayList<Account> accounts = new ArrayList<Account>();
AccountFactory accountFactory = new AccountFactory();
public Customer(String username, String password, Currency startingValue) {
super(username, password);
CheckingAccount firstAccount = new CheckingAccount("dollar");
openAccount(firstAccount);
}
// method for opening an account
public void openAccount(Account account) {
accounts.add(account);
}
// get all accounts
public ArrayList<Account> getAllAccounts() {
return accounts;
}
// gets an account by index
public Account getAccountByIndex(int i) {
return accounts.get(i);
}
// close an account
public boolean closeAccount(int i){
Account toClose = accounts.get(i);
if (toClose.getAmount().getValue() + toClose.getClosingFee().getValue() < 0) {
return false;
} else {
toClose.setAmount(new Dollar(toClose.getAmount().getValue() + toClose.getClosingFee().getValue()).convertTo(toClose.getCurrencyType()));
accounts.remove(i);
return true;
}
}
// delete account
public boolean deleteAccount(String accountNum){
int index = findAccount(accountNum);
accounts.remove(index);
return true;
}
// return account index
public int findAccount(String accountNum){
int length = accounts.size();
for(int i=0;i<length;i++){
if(accounts.get(i).getID().toString().equals(accountNum)){
return i;
}
}
return -1;
}
// a method that requests a loan.
public boolean requestLoan(Currency amount, String collateral){
int index = -1;
for (int i = 0; i < accounts.size(); i++) {
if (accounts.get(i) instanceof LoanAccount) {
index = i;
}
}
if (index != -1) {
LoanAccount loanAccount = (LoanAccount) accounts.get(index);
loanAccount.addLoan(new Loan(amount, this, new Date(), collateral));
return true;
} else {
return false;
}
}
// a method that shows transactions of the user.
public ArrayList<Transaction> showTransactionForAccountAtIndex(int i){
return accounts.get(i).getTransactions();
}
// gets all the transactions associated with a user
public ArrayList<Transaction> getAllTransactions() {
ArrayList<Transaction> allTransactions = new ArrayList<Transaction>();
for (Account acc : accounts) {
allTransactions.addAll(acc.getTransactions());
}
return allTransactions;
}
// a method that shows current balances of the user.
public Currency showCurrentBalances(String currencyType){
double value = 0;
for (Account account : accounts) {
Currency balance = account.getAmount();
value += balance.convertTo(currencyType).getValue();
}
switch (currencyType) {
case "dollar":
return new Dollar(value);
case "yen":
return new Yen(value);
case "euro":
return new Euro(value);
}
return new Dollar(value);
}
public ArrayList<Account> getAccounts() {
return accounts;
}
public int getCheckingNum(){
ArrayList<Account> accounts = this.getAccounts();
int count = 0;
for(int i=0;i<accounts.size();i++){
if(accounts.get(i).getAccountType().equals("checking")){
count++;
}
}
return count;
}
public int getSavingNum() {
ArrayList<Account> accounts = this.getAccounts();
int count = 0;
for(int i=0;i<accounts.size();i++){
if(accounts.get(i).getAccountType().equals("savings")){
count++;
}
}
return count;
}
public int getSecurititesNum() {
ArrayList<Account> accounts = this.getAccounts();
int count = 0;
for(int i=0;i<accounts.size();i++){
if(accounts.get(i).getAccountType().equals("securities")){
count++;
}
}
return count;
}
public int getLoanNum() {
ArrayList<Account> accounts = this.getAccounts();
int count = 0;
for(int i=0;i<accounts.size();i++){
if(accounts.get(i).getAccountType().equals("loan")){
count++;
}
}
return count;
}
}