-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBudgetManager.java
314 lines (264 loc) · 11.6 KB
/
BudgetManager.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
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
package budget;
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
/*
* This class store various purchase list and show statistics about them.
*/
public class BudgetManager implements Serializable {
public static final DecimalFormat df = new DecimalFormat("0.00");
final private LinkedHashSet<String> foodList;
final private LinkedHashSet<String> clothesList;
final private LinkedHashSet<String> entertainmentList;
final private LinkedHashSet<String> othersList;
final private Map<Categories, Double> balanceRecords;
private double totalBalance;
final private String pathToFile;
public static final String separator = "############################";
BudgetManager() {
this.pathToFile = "./purchases.txt";
this.othersList = new LinkedHashSet<>();
this.entertainmentList = new LinkedHashSet<>();
this.foodList = new LinkedHashSet<>();
this.clothesList = new LinkedHashSet<>();
this.totalBalance = 0.0;
this.balanceRecords = new LinkedHashMap<>();
for (Categories category : Categories.values()) {
this.balanceRecords.put(category, 0.0);
}
}
protected void addOthers(String details, double price) {
this.othersList.add(details + " $" + df.format(price));
this.balanceRecords.put(Categories.Other, this.balanceRecords.get(Categories.Other) + price);
updateBalance(price);
System.out.println("Purchase was added!");
}
protected void addEntertainment(String details, double price) {
this.entertainmentList.add(details + " $" + df.format(price));
this.balanceRecords.put(Categories.Entertainment, this.balanceRecords.get(Categories.Entertainment) + price);
updateBalance(price);
System.out.println("Purchase was added!");
}
protected void addClothes(String details, double price) {
this.clothesList.add(details + " $" + df.format(price));
this.balanceRecords.put(Categories.Clothes, this.balanceRecords.get(Categories.Clothes) + price);
updateBalance(price);
System.out.println("Purchase was added!");
}
protected void addFood(String details, double price) {
this.foodList.add(details + " $" + df.format(price));
this.balanceRecords.put(Categories.Food, this.balanceRecords.get(Categories.Food) + price);
updateBalance(price);
System.out.println("Purchase was added!");
}
private void updateBalance(double price) {
if (this.totalBalance >= price) {
this.totalBalance -= price;
} else {
this.totalBalance = 0.0;
}
}
protected void showAllPurchases() {
LinkedHashSet<String> allPurchases = getAllPurchases();
System.out.println("All:");
for (String purchase : allPurchases) {
System.out.println(purchase);
}
double totalPurchase = 0.0;
for (Categories category : this.balanceRecords.keySet()) {
totalPurchase += this.balanceRecords.get(category);
}
System.out.printf("Total sum: $%.2f\n", totalPurchase);
}
private LinkedHashSet<String> getAllPurchases() {
LinkedHashSet<String> allPurchases = new LinkedHashSet<>();
allPurchases.addAll(foodList);
allPurchases.addAll(clothesList);
allPurchases.addAll(entertainmentList);
allPurchases.addAll(othersList);
return allPurchases;
}
protected void showClothesPurchases() {
System.out.println("Clothes:");
if (this.clothesList.isEmpty()) {
System.out.println("The purchase list is empty!");
} else {
for (String purchase : this.clothesList) {
System.out.println(purchase);
}
System.out.printf("Total sum: $%.2f\n", this.balanceRecords.get(Categories.Clothes));
}
}
protected void showOthersPurchases() {
System.out.println("Other:");
if (this.othersList.isEmpty()) {
System.out.println("The purchase list is empty!");
} else {
for (String purchase : this.othersList) {
System.out.println(purchase);
}
System.out.printf("Total sum: $%.2f\n", this.balanceRecords.get(Categories.Other));
}
}
protected void showEntertainmentPurchases() {
System.out.println("Entertainment:");
if (this.entertainmentList.isEmpty()) {
System.out.println("The purchase list is empty!");
} else {
for (String purchase : this.entertainmentList) {
System.out.println(purchase);
}
System.out.printf("Total sum: $%.2f\n", this.balanceRecords.get(Categories.Entertainment));
}
}
protected void showFoodPurchases() {
System.out.println("Food:");
if (this.foodList.isEmpty()) {
System.out.println("The purchase list is empty!");
} else {
for (String purchase : this.foodList) {
System.out.println(purchase);
}
System.out.printf("Total sum: $%.2f\n", this.balanceRecords.get(Categories.Food));
}
}
protected void showBalance() {
System.out.printf("Balance: $%.2f", this.totalBalance);
}
protected void addIncome(double amount) {
this.totalBalance += amount;
System.out.println("Income was added!");
}
protected boolean isShoppingListEmpty() {
return this.clothesList.isEmpty() && this.foodList.isEmpty() && this.entertainmentList.isEmpty() && this.othersList.isEmpty();
}
protected void savePurchases() {
StringBuilder purchaseHistory = new StringBuilder();
purchaseHistory.append("Food: ".concat(foodList.toString()));
purchaseHistory.append(separator);
purchaseHistory.append("Clothes: ".concat(clothesList.toString()));
purchaseHistory.append(separator);
purchaseHistory.append("Entertainment: ".concat(entertainmentList.toString()));
purchaseHistory.append(separator);
purchaseHistory.append("Other: ".concat(othersList.toString()));
purchaseHistory.append(separator);
purchaseHistory.append("Balance: ".concat(String.valueOf(totalBalance)));
try {
FileOutputStream fileOutputStream
= new FileOutputStream(this.pathToFile);
ObjectOutputStream objectOutputStream
= new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(purchaseHistory);
objectOutputStream.flush();
objectOutputStream.close();
System.out.println("Purchases were saved!");
} catch (IOException e) {
e.getMessage();
}
}
protected void loadPurchases() {
Object data = "";
try {
FileInputStream fileInputStream
= new FileInputStream(this.pathToFile);
ObjectInputStream objectInputStream
= new ObjectInputStream(fileInputStream);
data = objectInputStream.readObject();
objectInputStream.close();
} catch (IOException | ClassNotFoundException ex1) {
ex1.getMessage();
}
updatePurchaseDetails(data.toString());
System.out.println("Purchases were loaded!");
}
private void updatePurchaseDetails(String data) {
String[] purchaseHistory = data.split(separator);
for (String input : purchaseHistory) {
String[] list = input.split(":");
String category = list[0].trim();
switch (category) {
case "Food" -> {
String[] items = list[1].substring(2, list[1].length() - 1).split(",");
for (String item : items) {
if (foodList.add(item.trim())) {
int priceIndex = item.lastIndexOf("$");
double price = Double.parseDouble(item.substring(priceIndex + 1));
this.balanceRecords.put(Categories.Food, this.balanceRecords.get(Categories.Food) + price);
}
}
}
case "Clothes" -> {
String[] items = list[1].substring(2, list[1].length() - 1).split(",");
for (String item : items) {
if (clothesList.add(item.trim())) {
int priceIndex = item.lastIndexOf("$");
double price = Double.parseDouble(item.substring(priceIndex + 1));
this.balanceRecords.put(Categories.Clothes, this.balanceRecords.get(Categories.Clothes) + price);
}
}
}
case "Entertainment" -> {
String[] items = list[1].substring(2, list[1].length() - 1).split(",");
for (String item : items) {
if (entertainmentList.add(item.trim())) {
int priceIndex = item.lastIndexOf("$");
double price = Double.parseDouble(item.substring(priceIndex + 1));
this.balanceRecords.put(Categories.Entertainment, this.balanceRecords.get(Categories.Entertainment) + price);
}
}
}
case "Other" -> {
String[] items = list[1].substring(2, list[1].length() - 1).split(",");
for (String item : items) {
if (othersList.add(item.trim())) {
int priceIndex = item.lastIndexOf("$");
double price = Double.parseDouble(item.substring(priceIndex + 1));
this.balanceRecords.put(Categories.Other, this.balanceRecords.get(Categories.Other) + price);
}
}
}
case "Balance" -> {
double balance = Double.parseDouble(list[1]);
this.totalBalance += balance;
}
}
}
}
protected void sortCategories() {
String[] category = new String[Categories.values().length];
int index = 0;
for (Categories cat : this.balanceRecords.keySet()) {
category[index] = cat.name().concat(" - $").concat(df.format(this.balanceRecords.get(cat)));
index++;
}
Sort.mergeSort(category, category.length);
for (String cat : category) {
System.out.println(cat);
}
System.out.println();
}
protected void sortByCategory(String category) {
String[] purchaseList = null;
switch (category) {
case "Food" -> purchaseList = this.foodList.toArray(new String[0]);
case "Clothes" -> purchaseList = this.clothesList.toArray(new String[0]);
case "Entertainment" -> purchaseList = this.entertainmentList.toArray(new String[0]);
case "Other" -> purchaseList = this.othersList.toArray(new String[0]);
case "All" -> {
LinkedHashSet<String> allPurchases = getAllPurchases();
purchaseList = allPurchases.toArray(new String[0]);
}
}
if (purchaseList.length == 0) {
System.out.println("The purchase list is empty!");
System.out.println();
} else {
Sort.mergeSort(purchaseList, purchaseList.length);
System.out.println(category + ":");
for (String purchase : purchaseList) {
System.out.println(purchase);
}
System.out.println();
}
}
}