-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bank.java
276 lines (176 loc) · 6.34 KB
/
Bank.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package lab3;
import java.util.ArrayList;
public class Bank {
private ArrayList<Account> theAccounts;
private ArrayList<Customer> theCustomers;
public Bank() {
theAccounts = new ArrayList<Account>();
theCustomers = new ArrayList<Customer>();
}
public boolean hasCustomer(String arg) {
boolean dummy = false;
for (int i = 0; i < theCustomers.size(); i++) {
if (theCustomers.get(i).getName().equals(arg)) {
dummy = true;
}
}
return dummy;
}
public void addCustomer(String arg) {
if (hasCustomer(arg)) {
System.out.println("The customer already exists.");
}
else {
theCustomers.add(new Customer(arg));
System.out.println("Customer added.");
}
}
public void addCurrentAccount(String arg1, double arg2) {
if (hasCustomer(arg1)) {
Customer dummy = getCustomer(arg1);
if (dummy.hasCurrentAccount()) {
System.out.println("The customer already has a current account.");
}
else {
CurrentAccount newAccount = new CurrentAccount(dummy, this, arg2);
// this, dvs den banken när är i
// skickar med bankobjektet
theAccounts.add(newAccount);
dummy.addCurrentAccount(newAccount);
System.out.println("Added a account for " + dummy.getName());
}
}
else {
System.out.println("There is no customer with that name.");
}
}
// kollar om ett namn matchar med ett annat
public Customer getCustomer(String arg) {
if (!hasCustomer(arg)) return null;
else {
Customer dummy;
int irun = 0;
do {
dummy = theCustomers.get(irun);
irun++;
} while (!dummy.getName().equals(arg));
return dummy;
}
}
public Account getAccount(int arg) {
Account number = null;
for(int j =0; j < theAccounts.size(); j++){
// hämtar kontonummer, jämför med arg
if(theAccounts.get(j).getAccountNumber()== arg) {
number = theAccounts.get(j);
return number;
}
}
System.out.println("No such account exists");
// no such account found
return number;
}
// addSavingsAccount(String arg) should add a savings account to the current account of the
// customer with the name arg and attach it to theAccounts
public void addSavingsAccount(String nameOfCustomer) {
Customer aCustomer = getCustomer(nameOfCustomer);
if(aCustomer == null) {
System.out.println("There is no customer with that name.");
}
else
{
SavingsAccount nyttSparKonto = aCustomer.getCurrentAccount().createSavingsAccount();
// en kund, hämta lönekonto, skapa sparkonto
// returerna sparkonto
// man har e kund (objekt), frågar den om lönekonto(getCurrentAccount)
// säga åt lönekontot att skapa sparkonto som hör til den kunden
// aCustomer.getCurrentAccount() = ett kundensLönekonto
// kundensLönekonto.createSavingsAccount()
// från kundensLönekonto skapar man sparkonto
theAccounts.add(nyttSparKonto);
}
}
public void computeAnnualChange() {
// kopplar till metoden annualchange som:
// sätta in årets ränta på konto
// eller ta ut avgiften
for(int j = 0; j < theAccounts.size(); j++) {
theAccounts.get(j).annualChange();
}
}
public void transfer(String CustomersName, String command, double belopp) {
Customer enCustomer = this.getCustomer(CustomersName);
if (enCustomer == null) {
return; //avsluta
}
// frågar om kundens CurrentAccount
// enCustomer = kunden
CurrentAccount getCurrentAccountAvCustomer = enCustomer.getCurrentAccount();
// kollaro m kund har ett konto
if ( getCurrentAccountAvCustomer == null) {
return; //avsluta
}
// input is save, do this:
// check if customer has Saving Account
boolean CustomerHaveSavingAccount;
if(command.equals("save")) {
CustomerHaveSavingAccount = getCurrentAccountAvCustomer.hasSavingsAccount();
if(CustomerHaveSavingAccount==false) {
this.addSavingsAccount(CustomersName);
// anropar metoden addSavingsAccount
// skapar nytt sparkonto
}
// för över pengar till saving account
getCurrentAccountAvCustomer.send(belopp);
}
if(command.equals("withdraw")) {
CustomerHaveSavingAccount = getCurrentAccountAvCustomer.hasSavingsAccount();
if(CustomerHaveSavingAccount==true) {
getCurrentAccountAvCustomer.receive(belopp);
// pengar från saving account till lönekontot
}
}
// pengar från console
// inga pengar som förs över mellan konton
// nya utomstående pengar sätts in via input av den som kör programmet
if(command.equals("external")) {
getCurrentAccountAvCustomer.receive(0,belopp);
}
Customer customerFromCommand = getCustomer(command);
// om en kund med namnet från input command
if(customerFromCommand != null) {
CurrentAccount Account1 = enCustomer.getCurrentAccount();
CurrentAccount Account2 = customerFromCommand.getCurrentAccount();
//int number1 = Account1.getAccountNumber();
//number1 behövs inte
int number2 = Account2.getAccountNumber();
//int number2 = customerFromCommand.getCurrentAccount().getAccountNumber();
// samma sak
// You call the method send(number2, arg3) of
//the current account number1.
Account1.send(number2, belopp);
}
}
public void transactions(String CustomerName) {
Customer enCustomerName = this.getCustomer( CustomerName);
if (enCustomerName == null) {
return; //avsluta
}
CurrentAccount getCurrentAccountAvCustomer = enCustomerName.getCurrentAccount();
if(getCurrentAccountAvCustomer!=null) {
// skriver ut strängen
System.out.println(getCurrentAccountAvCustomer.listTransactions());
}
}
public String toString() {
String result = "Bank information : ";
double totalValue = 0.0;
for (int i = 0; i < theAccounts.size(); i++) {
totalValue = totalValue + theAccounts.get(i).getBalance();
}
result = result + "\nNumber of customers : " + theCustomers.size();
result = result + "\nNumber of accounts : " + theAccounts.size();
result = result + "\nIt controls a total of " + totalValue;
return result;
}
}// måsvinge till Bank