-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main_OOPS.java
400 lines (357 loc) · 12.9 KB
/
Main_OOPS.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
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
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.io.File;
// Parent Expense class
class Expense {
double amount;
String category;
public Expense(double amount, String category) {
this.amount = amount;
this.category = category;
}
@Override
public String toString() {
return "Amount: " + amount + ", Category: " + category;
}
}
// Subclasses for different expense categories
class HomeExpense extends Expense {
public HomeExpense(double amount) {
super(amount, "Home");
}
}
class BillsExpense extends Expense {
public BillsExpense(double amount) {
super(amount, "Bills");
}
}
class HealthExpense extends Expense {
public HealthExpense(double amount) {
super(amount, "Health");
}
}
class EducationExpense extends Expense {
public EducationExpense(double amount) {
super(amount, "Education");
}
}
class TransportationExpense extends Expense {
public TransportationExpense(double amount) {
super(amount, "Transportation");
}
}
class FoodExpense extends Expense {
public FoodExpense(double amount) {
super(amount, "Food");
}
}
// Parent Income class
class Income {
double amount;
String category;
public Income(double amount, String category) {
this.amount = amount;
this.category = category;
}
@Override
public String toString() {
return "Amount: " + amount + ", Category: " + category;
}
}
// Subclasses for different Income categories
class Refund extends Income {
public Refund(double amount) {
super(amount, "Refund");
}
}
class Grant extends Income {
public Grant(double amount) {
super(amount, "Grant");
}
}
class Salary extends Income {
public Salary(double amount) {
super(amount, "Salary");
}
}
class Sale extends Income {
public Sale(double amount) {
super(amount, "Sale");
}
}
class Award extends Income {
public Award(double amount) {
super(amount, "Awards");
}
}
class Coupon extends Income {
public Coupon(double amount) {
super(amount, "Coupon");
}
}
public class Main {
private static final String EXPENSES_DIRECTORY = "Expenses/";
private static final String EXPENSES_FILENAME = EXPENSES_DIRECTORY + "Shrijan_expenses.json";
private static final String INCOMES_FILENAME = "Shrijan_incomes.json";
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private static final String[] FRIENDS = {"Keshav", "Vaibhav", "Praveen", "Ayush", "Harsh"};
private static final String[] FRIENDS_EXPENSE_FILES = {"Keshav_expenses.json", "Vaibhav_expenses.json",
"Praveen_expenses.json", "Ayush_expenses.json", "Harsh_expenses.json"};
private static final String[] FRIENDS_INCOME_FILES = {"Keshav_incomes.json", "Vaibhav_incomes.json",
"Praveen_incomes.json", "Ayush_incomes.json", "Harsh_incomes.json"};
private static final Gson[] FRIENDS_EXPENSE_GSON = new Gson[FRIENDS.length];
private static final Gson[] FRIENDS_INCOME_GSON = new Gson[FRIENDS.length];
public static void main(String[] args) {
// Create Expenses directory if it doesn't exist
File expensesDir = new File(EXPENSES_DIRECTORY);
if (!expensesDir.exists()) {
expensesDir.mkdir();
}
for (int i = 0; i < FRIENDS.length; i++) {
FRIENDS_EXPENSE_GSON[i] = new GsonBuilder().setPrettyPrinting().create();
FRIENDS_INCOME_GSON[i] = new GsonBuilder().setPrettyPrinting().create();
}
List<Expense> expenses = loadExpenses();
List<Income> incomes = loadIncomes();
Scanner scanner = new Scanner(System.in);
System.out.println("1. Add an Expense");
System.out.println("2. Add an Income");
System.out.println("3. Split Bill");
System.out.println("4. Display Expenses");
System.out.println("5. Display Incomes");
System.out.println("6. Exit");
while (true) {
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addExpense(scanner, expenses);
break;
case 2:
addIncome(scanner, incomes);
break;
case 3:
splitBill(scanner, expenses);
break;
case 4:
displayExpenses(expenses);
break;
case 5:
displayIncomes(incomes);
break;
case 6:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please enter again.");
}
}
}
private static void addExpense(Scanner scanner, List<Expense> expenses) {
System.out.print("Enter the amount: ");
double amount = scanner.nextDouble();
scanner.nextLine(); // Consume newline
System.out.println("Select a category:");
System.out.println("1. Home");
System.out.println("2. Bills");
System.out.println("3. Health");
System.out.println("4. Education");
System.out.println("5. Transportation");
System.out.println("6. Food");
System.out.print("Enter the category number: ");
int categoryChoice = scanner.nextInt();
scanner.nextLine(); // Consume newline
String category;
switch (categoryChoice) {
case 1:
category = "Home";
break;
case 2:
category = "Bills";
break;
case 3:
category = "Health";
break;
case 4:
category = "Education";
break;
case 5:
category = "Transportation";
break;
case 6:
category = "Food";
break;
default:
System.out.println("Invalid category choice");
return;
}
Expense newExpense = new Expense(amount, category);
expenses.add(newExpense);
saveExpenses(expenses);
System.out.println("Expense added successfully.");
}
private static void addIncome(Scanner scanner, List<Income> incomes) {
System.out.print("Enter the amount: ");
double amount = scanner.nextDouble();
scanner.nextLine(); // Consume newline
System.out.println("Select an income category:");
System.out.println("1. Refund");
System.out.println("2. Grant");
System.out.println("3. Salary");
System.out.println("4. Sale");
System.out.println("5. Award");
System.out.println("6. Coupon");
System.out.print("Enter the category number: ");
int categoryChoice = scanner.nextInt();
scanner.nextLine(); // Consume newline
String category;
switch (categoryChoice) {
case 1:
category = "Refund";
break;
case 2:
category = "Grant";
break;
case 3:
category = "Salary";
break;
case 4:
category = "Sale";
break;
case 5:
category = "Award";
break;
case 6:
category = "Coupon";
break;
default:
System.out.println("Invalid category choice");
return;
}
Income newIncome = new Income(amount, category);
incomes.add(newIncome);
saveIncomes(incomes);
System.out.println("Income added successfully.");
}
private static void splitBill(Scanner scanner, List<Expense> expenses) {
System.out.print("Enter the amount to split: ");
double amount = scanner.nextDouble();
scanner.nextLine(); // Consume newline
List<String> selectedFriends = selectFriends(scanner);
double splitAmount = amount / (selectedFriends.size() + 1); // Including yourself
for (String friend : selectedFriends) {
int index = Arrays.asList(FRIENDS).indexOf(friend);
if (index != -1) {
List<Expense> friendExpenses = loadFriendExpenses(index);
friendExpenses.add(new Expense(splitAmount, "Shared"));
saveFriendExpenses(index, friendExpenses);
System.out.println("Split amount added to " + friend + "'s expenses.");
}
}
expenses.add(new Expense(splitAmount, "Shared"));
saveExpenses(expenses);
System.out.println("Split amount added to your expenses.");
}
private static List<String> selectFriends(Scanner scanner) {
List<String> selectedFriends = new ArrayList<>();
System.out.println("Select friends to split bill with:");
for (int i = 0; i < FRIENDS.length; i++) {
System.out.println((i + 1) + ". " + FRIENDS[i]);
}
System.out.println("Enter the numbers separated by spaces (e.g., '1 3 4'), or '0' to split with everyone:");
String input = scanner.nextLine();
String[] numbers = input.split(" ");
for (String number : numbers) {
int index = Integer.parseInt(number) - 1;
if (index >= 0 && index < FRIENDS.length) {
selectedFriends.add(FRIENDS[index]);
}
}
return selectedFriends;
}
private static void saveExpenses(List<Expense> expenses) {
try (FileWriter writer = new FileWriter(EXPENSES_FILENAME)) {
gson.toJson(expenses, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
private static List<Expense> loadExpenses() {
try (FileReader reader = new FileReader(EXPENSES_FILENAME)) {
Expense[] expensesArray = gson.fromJson(reader, Expense[].class);
if (expensesArray != null) {
return new ArrayList<>(List.of(expensesArray));
}
} catch (IOException e) {
// If file does not exist or cannot be read, return an empty list
}
return new ArrayList<>();
}
private static void saveFriendExpenses(int index, List<Expense> expenses) {
String friendExpensesFilename = EXPENSES_DIRECTORY + FRIENDS_EXPENSE_FILES[index];
try (FileWriter writer = new FileWriter(friendExpensesFilename)) {
FRIENDS_EXPENSE_GSON[index].toJson(expenses, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
private static List<Expense> loadFriendExpenses(int index) {
String friendExpensesFilename = EXPENSES_DIRECTORY + FRIENDS_EXPENSE_FILES[index];
try (FileReader reader = new FileReader(friendExpensesFilename)) {
Expense[] expensesArray = FRIENDS_EXPENSE_GSON[index].fromJson(reader, Expense[].class);
if (expensesArray != null) {
return new ArrayList<>(List.of(expensesArray));
}
} catch (IOException e) {
// If file does not exist or cannot be read, return an empty list
}
return new ArrayList<>();
}
private static void saveIncomes(List<Income> incomes) {
try (FileWriter writer = new FileWriter(INCOMES_FILENAME)) {
gson.toJson(incomes, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
private static List<Income> loadIncomes() {
try (FileReader reader = new FileReader(INCOMES_FILENAME)) {
Income[] incomesArray = gson.fromJson(reader, Income[].class);
if (incomesArray != null) {
return new ArrayList<>(List.of(incomesArray));
}
} catch (IOException e) {
// If file does not exist or cannot be read, return an empty list
}
return new ArrayList<>();
}
private static void displayExpenses(List<Expense> expenses) {
if (expenses.isEmpty()) {
System.out.println("No expenses recorded.");
return;
}
System.out.println("Expenses:");
for (Expense expense : expenses) {
System.out.println(expense);
}
}
private static void displayIncomes(List<Income> incomes) {
if (incomes.isEmpty()) {
System.out.println("No incomes recorded.");
return;
}
System.out.println("Incomes:");
for (Income income : incomes) {
System.out.println(income);
}
}
}