-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpenseTracker.java
More file actions
507 lines (469 loc) · 22.8 KB
/
expenseTracker.java
File metadata and controls
507 lines (469 loc) · 22.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
class expense {
int exp_id;
int amount;
String category;
String description;
String date;
expense() {
}
expense(int exp_id, int amount, String category, String description, String date) {
this.exp_id = exp_id;
this.amount = amount;
this.category = category;
this.description = description;
this.date = date;
}
public int getExp_id() {
return exp_id;
}
public int getAmount() {
return amount;
}
public String getCategory() {
return category;
}
public String getDescription() {
return description;
}
public String getDate() {
return date;
}
public String getString() {
return String.format("%d,%d,%s,%s,%s", exp_id, amount, category, description, date);
}
public String getFormattedString() {
return String.format("ID : %d , Amount : %d , Category : %s , Description : %s , Date : %s", exp_id, amount,
category, description, date);
}
}
// storing expense
class expenseStorage {
static void storeExpense(ArrayList<expense> expObjects) {
File myFile = new File("expense.txt");
// checking if file exist or not
if (myFile.exists()) {
} else {
try {
myFile.createNewFile();
} catch (Exception e) {
System.out.println("Cannot create File! " + e.getMessage());
}
}
try {
FileWriter myFileWriter = new FileWriter("expense.txt");
for (expense expense : expObjects) {
myFileWriter.write(expense.getString() + "\n");
}
myFileWriter.close();
} catch (Exception e) {
System.out.println("Cannot store the data");
e.printStackTrace();
}
}
static ArrayList<expense> readExpense() {
File myFile = new File("expense.txt");
ArrayList<expense> expObjects = new ArrayList<expense>();
if (myFile.exists()) {
try {
Scanner sc = new Scanner(myFile);
while (sc.hasNextLine()) {
String str = sc.nextLine();
String[] str_arr = str.split("\n");
try {
for (String string : str_arr) {
String[] expInfo = string.split(",");
expense exp = new expense(Integer.valueOf(expInfo[0]), Integer.valueOf(expInfo[1]),
expInfo[2], expInfo[3], expInfo[4]);
expObjects.add(exp);
}
} catch (Exception e) {
System.out.println("Cannot return expense");
}
}
sc.close();
} catch (Exception e) {
e.getStackTrace();
}
}
return expObjects;
}
}
class categories {
static void storeCategories(ArrayList<String> categories) {
File categoriesFile = new File("categories.txt");
if (categoriesFile.exists()) {
} else {
try {
categoriesFile.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
FileWriter categoriesFileWriter = new FileWriter("categories.txt");
for (String category : categories) {
categoriesFileWriter.write(category.toLowerCase() + "\n");
}
categoriesFileWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
static ArrayList<String> readCategories() {
ArrayList<String> categories = new ArrayList<>();
File categoriesFile = new File("categories.txt");
if (categoriesFile.exists()) {
try {
Scanner sc = new Scanner(categoriesFile);
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] categoryArr = line.split("\n");
for (String string : categoryArr) {
categories.add(string);
}
}
sc.close();
} catch (Exception e) {
System.out.println("Cannot retrieve categories from file.");
}
}
return categories;
}
}
public class ExpenseTracker {
static void waitForEnter() {
System.out.println("\nPress enter to continue..");
try {
System.in.read();
} catch (Exception e) {
}
}
static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
public static void main(String[] args) {
// arrayList to store categories
ArrayList<String> categoriesList = categories.readCategories();
// arraylist to store object of class expense
ArrayList<expense> expObjects = expenseStorage.readExpense();
Scanner sc = new Scanner(System.in);
int option = 0;
try {
while (option != 7) {
// code to clear screen
clearScreen();
// code to display name of project
System.out.println("\t********************************");
System.out.println(" \t\tExpense Tracker");
System.out.println("\t********************************\n");
// options displayed to user
System.out.println("1)Add expense");
System.out.println("2)Delete expense");
System.out.println("3)Manage categories");
System.out.println("4)View expense");
System.out.println("5)Modify expense");
System.out.println("6)Generate Report");
System.out.println("7)Exit\n");
// taking input
System.out.print("Enter your option :");
option = sc.nextInt();
switch (option) {
case 1:
clearScreen();
System.out.println("\n***************");
System.out.println("Add Expense");
System.out.println("***************\n");
int exp_id = 0;
int amount = 0;
String category = new String();
String description = new String();
String date = new String();
try {
System.out.print("\nEnter expense ID :");
exp_id = sc.nextInt();
System.out.print("Enter Amount :");
amount = sc.nextInt();
sc.nextLine();
System.out.print("Enter Category :");
category = sc.nextLine();
if (categoriesList.contains(category.toLowerCase())) {
System.out.print("Enter Description :");
description = sc.nextLine();
System.out.print("Enter Date(dd/mm/yyyy) :");
date = sc.nextLine();
expense exp = new expense(exp_id, amount, category, description, date);
try {
expObjects.add(exp);
expenseStorage.storeExpense(expObjects);
System.out.println("\n\nInformation Added successfully");
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Category does not exist. First create category and try again.");
}
} catch (Exception e) {
System.out.println("Invalid input in one or more field. Try Again!");
}
waitForEnter();
break;
case 2:
clearScreen();
System.out.println("\n\n***************");
System.out.println("Delete Expense");
System.out.println("***************\n");
System.out.print("Enter Expense ID to delete expense :");
int deleteExpId = sc.nextInt();
boolean expenseDeleted = false;
try {
for (expense seperateExpense : expObjects) {
if (seperateExpense.getExp_id() == deleteExpId) {
expObjects.remove(expObjects.indexOf(seperateExpense));
System.out.println("Expense Deleted successfully");
expenseDeleted = true;
}
}
} catch (Exception e) {
// System.out.println("Error while deleting expense.");
}
if (expenseDeleted == false)
System.out.println("Element not found");
expenseStorage.storeExpense(expObjects);
waitForEnter();
break;
case 3:
clearScreen();
System.out.println("\n*****************");
System.out.println("Manage Categories");
System.out.println("*****************\n");
System.out.println("1) Add category");
System.out.println("2) View categories");
System.out.println("3) Delete category");
System.out.println("4) Main Menu");
int categoryChoice = 0;
System.out.print("Enter your choice : ");
categoryChoice = sc.nextInt();
sc.nextLine();
switch (categoryChoice) {
case 1:
clearScreen();
System.out.println("\n**************");
System.out.println("Add Category");
System.out.println("**************\n");
// try catch block to manage incorrect input
System.out.print("\nEnter the name of the category :");
String categoryName = new String();
try {
categoryName = sc.nextLine();
} catch (Exception e) {
System.out.println("Incorrect Input. Please try again");
}
categoriesList.add(categoryName);
try {
categories.storeCategories(categoriesList);
System.out.println("\nCategory added successfully");
} catch (Exception e) {
System.out.println("Error occured while storing categories");
}
waitForEnter();
break;
case 2:
clearScreen();
System.out.println("\n****************");
System.out.println("View Categories");
System.out.println("****************\n");
int i = 1;
System.out.println(""); // for gap between line to avoid confusion
for (String seperateCategory : categoriesList) {
System.out.println(i + ")" + seperateCategory);
i++;
}
waitForEnter();
break;
case 3:
clearScreen();
System.out.println("\n**************");
System.out.println("Delete Category");
System.out.println("**************\n");
System.out.println("Disclaimer :- Make sure the category name must be correct.");
System.out.print("\nEnter the category name to be deleted:");
String deleteCategory = new String();
try {
deleteCategory = sc.nextLine();
} catch (Exception e) {
System.out.println("Incorrect Input. Please try again");
}
boolean categoryDeleted = false;
try {
for (String seperateCategory : categoriesList) {
if (deleteCategory.toLowerCase().equalsIgnoreCase(seperateCategory)) {
categoriesList.remove(categoriesList.indexOf(deleteCategory));
categoryDeleted = true;
}
}
} catch (Exception e) {
// System.out.println("Cannot delete category due to exception..");
}
if (categoryDeleted) {
System.out.println("\nCategory deleted successfully");
} else {
System.out.println("\nCategory not found");
}
categories.storeCategories(categoriesList);
waitForEnter();
break;
case 4 :
break;
default:
System.out.println("Invalid Choice!");
}
waitForEnter();
break;
case 4:
clearScreen();
System.out.println("\n\n***************");
System.out.println("View Expense");
System.out.println("***************\n");
int i = 1;
for (expense expense : expObjects) {
System.out.println(i + ") " + expense.getFormattedString());
i++;
}
System.out.println("\n");
waitForEnter();
break;
case 5:
clearScreen();
System.out.println("\n\n******************");
System.out.println("Modify Expense");
System.out.println("******************\n");
System.out.print("Enter Expense ID to modify expense :");
int modifyExpId = 0;
try {
modifyExpId = sc.nextInt();
} catch (Exception e) {
System.out.println("Incorrect input. Try again!");
}
expense modifyExpense = new expense();
expense tempExpense = new expense();
boolean expenseFound = false;
try {
for (expense seperateExpense : expObjects) {
if (seperateExpense.getExp_id() == modifyExpId) {
modifyExpense = seperateExpense;
expenseFound = true;
}
}
} catch (Exception e) {
// System.out.println("Cannot retrive expense for modification");
}
if (expenseFound) {
tempExpense = modifyExpense;
System.out.println("\nExpense Found. Enter new details:- \n");
System.out.print("\nEnter amount :");
int newAmount = sc.nextInt();
sc.nextLine();
System.out.print("Enter category :");
String newCategory = sc.nextLine();
if (categoriesList.contains(newCategory.toLowerCase())) {
System.out.print("Enter description :");
String newDescription = sc.nextLine();
System.out.print("Enter date (dd/mm/yyyy) :");
String newDate = sc.nextLine();
modifyExpense.amount = newAmount;
modifyExpense.category = newCategory;
modifyExpense.description = newDescription;
modifyExpense.date = newDate;
try {
expObjects.remove(expObjects.indexOf(tempExpense));
expObjects.add(modifyExpense);
System.out.println("\nExpense modified successfully....");
} catch (Exception e) {
System.out.println("\nCannot modify expense...");
}
} else {
System.out.println("Category does not exist. First add category and try again.");
}
} else {
System.out.println("\nExpense not found. Try again...");
}
expenseStorage.storeExpense(expObjects);
waitForEnter();
break;
case 6:
clearScreen();
System.out.println("\t********************************");
System.out.println(" \tReport Generation");
System.out.println("\t********************************\n");
System.out.println("1) Generate report (category wise)");
System.out.println("2) Generate report (Date wise)");
int reportOption = 0;
System.out.print("Enter your option : ");
try {
reportOption = sc.nextInt();
sc.nextLine();
} catch (Exception e) {
System.out.println("Invalid input. Try again!");
}
switch (reportOption) {
case 1:
String categoryReport = new String();
int expCount = 0;
System.out.print("Enter category name to generate report :");
try {
categoryReport = sc.nextLine();
} catch (Exception e) {
System.out.println("Invalid input. Try again!");
}
if(categoriesList.contains(categoryReport.toLowerCase())){
System.out.println("Report Generated.\n");
System.out.println("Category Name : "+ categoryReport + "\n");
System.out.println("************************************************************************************************************************");
System.out.println(" \t\t\t\t\t\tCategory Report");
System.out.println("************************************************************************************************************************\n");
for (expense reportExpense : expObjects) {
if((reportExpense.getCategory()).equals(categoryReport.toLowerCase())){
System.out.println(reportExpense.getFormattedString());
expCount++;
}
}
System.out.println("\n************************************************************************************************************************");
System.out.println("\nTotal no. of expense with category("+categoryReport+") :" + expCount);
}
else{
System.out.println("Category does not exist. Report generation not possible!");
}
break;
case 2 :
System.out.println("Currently under development....");
break;
default:
System.out.println("Invalid option. Try Again!");
break;
}
waitForEnter();
break;
case 7:
clearScreen();
System.out.println("\t********************************");
System.out.println(" \t\tExpense Tracker");
System.out.println("\t********************************\n");
System.out.println("Thank you for using our expense tracker, Goodbye...\n");
break;
default:
System.out.println("Invalid Choice. Try Again");
waitForEnter();
break;
}
}
} catch (Exception e) {
System.out.println("Internal Error occured. Program terminated!");
}
sc.close();
}
}