-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankBalance.java
More file actions
339 lines (294 loc) · 11.8 KB
/
BankBalance.java
File metadata and controls
339 lines (294 loc) · 11.8 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package FST;
import javax.annotation.processing.FilerException;
import java.io.*;
import java.util.*;
import java.math.BigDecimal;
import java.text.DecimalFormat;
// Program name: BankBalance.java
// Purpose:
// Created by Evan Rimer on Saturday April 11 2019.
// Copyright © 2019 Evan Rimer. All rights reserved.
public class BankBalance implements Serializable {//Start of Class BankBalance
private double accountBalance;
private double annualInterestRate;
private ArrayList<Transaction> allTransactions= new ArrayList<>();
private DecimalFormat df = new DecimalFormat("'$'0.00");//Decimal format that rounds to two decimal places
public ArrayList<Transaction> transactionList = new ArrayList<>();//Creating array list of transactions
public ArrayList<DepTransaction> depositList = new ArrayList<>();
public ArrayList<WithTransaction> withdrawList = new ArrayList<>();
//Constructor method to initialize object accountBalance and annualInterestRate
BankBalance (double initialBalance) {
accountBalance = initialBalance;
setAccountBalance(initialBalance);
annualInterestRate = 0;
}
//Set the value of the initial balance of accountBalance
private void setAccountBalance (double initialBalance) {
//Error for account to not have negative balance
if (initialBalance < 0) {
throw new IllegalArgumentException("The bank account can not have a negative balance.");
}
//Calls method to insure initial balance has two decimal places
twoDecimalPlaces(initialBalance);
this.accountBalance = initialBalance;
}
//Method to deposit money in the account
public void deposit (double amountToDeposit) {
//Insure deposit is a positive value
if (amountToDeposit < 0) {
throw new IllegalArgumentException("You can not deposit a negative value.");
}
//Calls method to insure initial deposit has two decimal places
twoDecimalPlaces(amountToDeposit);
//Adds deposit to accountBalance
accountBalance = accountBalance + amountToDeposit;
writingBalance(Account.returnName());
//Adds the transaction to transaction list
var transaction = new Transaction(Transaction.Type.deposit, amountToDeposit, accountBalance);
transactionList.add(transaction);
var transaction1 = new DepTransaction(DepTransaction.Type.deposit, amountToDeposit, accountBalance);
depositList.add(transaction1);
}
//Method to withdraw money from the account
public void withdraw (double amountToWithdraw) {
//Insure withdraw is a positive value
if (amountToWithdraw < 0) {
throw new IllegalArgumentException("You can not withdraw a negative value.");
}
//Insure that you can not withdraw more money that what is in the account
if (amountToWithdraw > accountBalance) {
throw new IllegalArgumentException("You do not have enough money in your account." + "Balance: " + accountBalance + "." + " Amount to Withdraw: " + amountToWithdraw);
}
//Calls method to insure initial withdraw has two decimal places
twoDecimalPlaces(amountToWithdraw);
//Subtracts withdraw from account
accountBalance -= amountToWithdraw;
writingBalance(Account.returnName());
var transaction = new Transaction(Transaction.Type.withdrawal, amountToWithdraw, accountBalance);
transactionList.add(transaction);
var transaction1 = new WithTransaction(WithTransaction.Type.withdraw, amountToWithdraw, accountBalance);
withdrawList.add(transaction1);
}
//Method to put old and new transactions into a single ArrayList called allTransactions
public void readingArray() throws IOException{
ArrayList<Transaction> transactions = new ArrayList<>();
try {
//reads all old transactions already stored and adds to array called transactions
FileInputStream fileIn = new FileInputStream(Account.returnName()+ "transactionList.txt");
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
Object obj = objectIn.readObject();
int i=0;
while (i < 1000000000) {
Transaction transaction = (Transaction) obj;
transactions.add(transaction);
obj = objectIn.readObject();
i++;
}
objectIn.close();
} catch (Exception ex) { }
try {
//if refresh button is pressed without adding new transactions, allTransactions isn't clear
//if it isn't clear that means that the transactions are already being displayed
//if it is clear it will send to catch and add the transactions to allTransactions
allTransactions.get(0);
}catch (Exception eeeee) {
boolean add = true;
try {
//Checks if any new transactions are the same as old saved ones
for (int i = 0; i < transactionList.size(); i++) {
if (transactionList.get(i).compareTo(transactions.get(0)) == 0) {
add = false;
}
}
}catch (Exception f){}
//If none are the same it means transactionList is all new transactions
//Therefore must add back transactions
if(add) {
allTransactions.addAll(transactions);
}
//Add new transactions
allTransactions.addAll(transactionList);
transactionList.clear();
//add allTransactions to transactionList because transactionList is used in other classes
transactionList.addAll(allTransactions);
//Adds deposits to deposit list
//Adds withdrawals to withdrawal list
for (int i = 0; i < transactionList.size(); i++) {
if(transactionList.get(i).type == Transaction.Type.deposit){
var transaction = new DepTransaction(DepTransaction.Type.deposit, transactionList.get(i).amount, transactionList.get(i).balanceAfterTransaction);
transaction.date = transactionList.get(i).date;
depositList.add(transaction);
}else if(transactionList.get(i).type == Transaction.Type.withdrawal){
var transaction = new WithTransaction(WithTransaction.Type.withdraw, transactionList.get(i).amount, transactionList.get(i).balanceAfterTransaction);
transaction.date = transactionList.get(i).date;
withdrawList.add(transaction);
}
}
}
}
//Method to write transactions as Transactions (Objects) to file for current user
public void writingArray (String user) throws IOException{
readingArray();
try {
//Initializes file with name of user followed by transactionList.txt
FileOutputStream fileOut = new FileOutputStream(user + "transactionList.txt");
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
//Writes all transactions stored in allTransactions ArrayList to the file
for (int i = 0; i < allTransactions.size(); i++) {
objectOut.writeObject(allTransactions.get(i));
}
objectOut.close();
} catch (Exception ex) {
System.out.println("Uh oh");
ex.printStackTrace();
}
}
//Writes balance to file called the user's name followed by balance.txt
public void writingBalance (String user) {
try {
FileWriter fr = new FileWriter(user + "balance.txt");
BufferedWriter br = new BufferedWriter(fr);
PrintWriter pw = new PrintWriter(br);
pw.write(getAccountBalance());
pw.close();
} catch (IOException e) { }
}
//reads stored balance of user
public static String readingBalance (String user) throws IOException {
String balance;
try {
FileReader fr = new FileReader(user + "balance.txt");
BufferedReader br = new BufferedReader(fr);
balance = br.readLine();
} catch (Exception e) {
balance = "$100.00";
}
return balance;
}
//Method to set annual interest rate
public void setAnnualInterestRate (double interestRate) {
//Insure interest rate is positive value
if (interestRate < 0) {
throw new IllegalArgumentException("You can not have a negative interest rate.");
}
annualInterestRate = interestRate;
}
//Method to add monthly interest to account balance
public void monthlyInterest () {
double monthlyRate = ((annualInterestRate / 12) * accountBalance);
accountBalance += monthlyRate;
var transaction = new Transaction(Transaction.Type.interest, monthlyRate, accountBalance);
transactionList.add(transaction);
}
public String getAccountBalance () {
return df.format(accountBalance);
}
//To string method to display object accountBalance as String
@Override
public String toString () {
return "" + df.format(accountBalance);
}
//Method to make sure the double value only has two decimal points
private void twoDecimalPlaces (double num) {
if (BigDecimal.valueOf(num).scale() > 2) {
throw new IllegalArgumentException("The Input can not have more than two decimal places.");
}
}
}//End of Class BankBalance
class Transaction implements Comparable<Transaction>, Serializable {
private final DecimalFormat df = new DecimalFormat("'$'0.00");//Decimal format that rounds to two decimal places
public Date date;
public Type type;
public final double amount;
public final double balanceAfterTransaction;
public static Comparator<Transaction> inverseComparator = (t1, t2) -> -t1.compareTo(t2);
public static Comparator<Transaction> timeComparator = (t1, t2) -> -t1.date.compareTo(t2.date);
public Transaction (Type type, double amount, double balanceAfterTransaction) {
this.type = type;
this.amount = amount;
this.balanceAfterTransaction = balanceAfterTransaction;
this.date = new Date();
}
@Override
public int compareTo (Transaction o) {
return Double.compare(this.amount, o.amount);
}
enum Type {
deposit, withdrawal, interest
}
@Override
public String toString () {
switch (type) {
case deposit:
return "Deposit: " + df.format(amount) + "," + " Balance: " + df.format(balanceAfterTransaction) + ", Time: " + date;
case withdrawal:
return "Withdraw: " + df.format(amount) + "," + " Balance: " + df.format(balanceAfterTransaction) + ", Time: " + date;
case interest:
return "Interest: " + df.format(amount) + "," + " Balance: " + df.format(balanceAfterTransaction) + ", Time: " + date;
default:
throw new IllegalArgumentException("Impossible");
}
}
}
class DepTransaction implements Comparable<DepTransaction>, Serializable {
private final DecimalFormat df = new DecimalFormat("'$'0.00");//Decimal format that rounds to two decimal places
public Date date;
public Type type;
public double amount;
public double balanceAfterTransaction;
public static final Comparator<DepTransaction> inverseComparator = (t1, t2) -> -t1.compareTo(t2);
public static final Comparator<DepTransaction> timeComparator = (t1, t2) -> -t1.date.compareTo(t2.date);
public DepTransaction (Type type, double amount, double balanceAfterTransaction) {
this.type = type;
this.amount = amount;
this.balanceAfterTransaction = balanceAfterTransaction;
this.date = new Date();
}
@Override
public int compareTo (DepTransaction o) {
return Double.compare(this.amount, o.amount);
}
enum Type {
deposit
}
@Override
public String toString () {
switch (type) {
case deposit:
return "Deposit: " + df.format(amount) + "," + " Balance: " + df.format(balanceAfterTransaction) + ", Time: " + date;
default:
throw new IllegalArgumentException("Impossible");
}
}
}
class WithTransaction implements Comparable<WithTransaction>, Serializable {
private final DecimalFormat df = new DecimalFormat("'$'0.00");//Decimal format that rounds to two decimal places
public Date date;
public Type type;
public double amount;
public double balanceAfterTransaction;
public static final Comparator<WithTransaction> inverseComparator = (t1, t2) -> -t1.compareTo(t2);
public static final Comparator<WithTransaction> timeComparator = (t1, t2) -> -t1.date.compareTo(t2.date);
public WithTransaction (Type type, double amount, double balanceAfterTransaction) {
this.type = type;
this.amount = amount;
this.balanceAfterTransaction = balanceAfterTransaction;
this.date = new Date();
}
@Override
public int compareTo (WithTransaction o) {
return Double.compare(this.amount, o.amount);
}
enum Type {
withdraw
}
@Override
public String toString () {
switch (type) {
case withdraw:
return "Withdraw: " + df.format(amount) + "," + " Balance: " + df.format(balanceAfterTransaction) + ", Time: " + date;
default:
throw new IllegalArgumentException("Impossible");
}
}
}