Skip to content

Commit 8bdcfb4

Browse files
committed
Added new features 1.Monthly Total 2.Category wise stats 3.Search expense by date range
1 parent 881319e commit 8bdcfb4

File tree

3 files changed

+147
-39
lines changed

3 files changed

+147
-39
lines changed

ExpenseTracker/App.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,14 @@ public static void main(String args[]){
4646
em.viewAllExpense(sc);
4747
break;
4848
case 5:
49-
// em.monthlyTotal(sc);
50-
continue;
51-
// break;
49+
em.monthlyTotal(sc);
50+
break;
5251
case 6:
53-
// em.categoryWiseStats(sc);
54-
continue;
55-
// break;
52+
em.categoryWiseStats(sc);
53+
break;
5654
case 7:
57-
// em.searchExpenseByDateRange(sc);
58-
continue;
59-
// break;
55+
em.searchExpenseByDateRange(sc);
56+
break;
6057
case 8:
6158
// em.exportToCSV(sc);
6259
continue;

ExpenseTracker/modal/Expense.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ public void editExpense(double amount,Category category,LocalDate date,String no
3434
this.note = note;
3535
}
3636

37-
public void deleteExpense(){
38-
39-
}
40-
4137
// Getters
4238

4339
public int getId(){
@@ -59,25 +55,4 @@ public Category getCategory(){
5955
public LocalDate getDate(){
6056
return this.date;
6157
}
62-
63-
// public double getMonthlyTotal(){
64-
65-
// }
66-
67-
// public String getCategoryStats(){
68-
69-
// }
70-
71-
// public String searchByDateRange(){
72-
73-
// }
74-
75-
// public void saveToFile(){
76-
77-
// }
78-
79-
// public void loadFromFile(){
80-
81-
// }
82-
8358
}

ExpenseTracker/service/ExpenseManager.java

Lines changed: 141 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
import java.util.Scanner;
66
import java.time.LocalDate;
77
import java.util.ArrayList;
8+
import java.time.temporal.ChronoField;
9+
import java.time.format.DateTimeFormatter;
10+
import java.time.format.DateTimeFormatterBuilder;
811

912
public class ExpenseManager{
1013

11-
private ArrayList<Expense> allExpenses = new ArrayList<>();
14+
private ArrayList<Expense> allExpenses = new ArrayList<>();
1215

1316
private int getExpenseId(Scanner sc){
1417
int id;
@@ -30,6 +33,49 @@ private int getExpenseId(Scanner sc){
3033
return id;
3134
}
3235

36+
private void showMonthlyTotal(Scanner sc,int month,int year){
37+
LocalDate firstDate = LocalDate.of(year, month, 1);;
38+
LocalDate lastDate = LocalDate.of(year,month,firstDate.lengthOfMonth());
39+
40+
double food = 0,travel = 0,shopping = 0,bills = 0,others = 0;
41+
double totalAmount = 0;
42+
for(Expense e : allExpenses){
43+
LocalDate date = e.getDate();
44+
if(!date.isBefore(firstDate) && !date.isAfter(lastDate)){
45+
Category category = e.getCategory();
46+
double amount = e.getAmount();
47+
switch(category){
48+
case FOOD:
49+
food += amount;
50+
break;
51+
case TRAVEL:
52+
travel += amount;
53+
break;
54+
case SHOPPING:
55+
shopping += amount;
56+
break;
57+
case BILLS:
58+
bills += amount;
59+
break;
60+
case OTHERS:
61+
others += amount;
62+
break;
63+
default: continue;
64+
}
65+
totalAmount += amount;
66+
}
67+
}
68+
69+
System.out.println("\n------- Monthly Total (" + firstDate.getMonth().toString().toLowerCase() + " " + year + ") -------");
70+
System.out.println("Food : " + food);
71+
System.out.println("Shopping : " + shopping);
72+
System.out.println("Bills : " + bills);
73+
System.out.println("Travel : " + travel);
74+
System.out.println("Others : " + others);
75+
System.out.println("---------------------------------------------");
76+
System.out.println("TOTAL : " + totalAmount);
77+
}
78+
3379
public void addExpense(Scanner sc){
3480
double amount;
3581
while(true){
@@ -138,6 +184,7 @@ public void editExpense(Scanner sc){
138184
continue;
139185
}
140186
}
187+
break;
141188
}
142189
// Take categroy
143190
Category category;
@@ -177,7 +224,7 @@ public void editExpense(Scanner sc){
177224
break;
178225
}
179226
// Take note
180-
System.out.print("Enter new note (or press Enter to keep same): Sneakers on discount");
227+
System.out.print("Enter new note (or press Enter to keep same): ");
181228
String note = sc.nextLine();
182229
if(note.isEmpty()) note = expense.getNote();
183230
expense.editExpense(amount , category , date , note);
@@ -213,6 +260,7 @@ public void viewAllExpense(Scanner sc){
213260
}
214261

215262
public void monthlyTotal(Scanner sc){
263+
216264
int month;
217265
while(true){
218266
System.out.print("Enter month (1-12): ");
@@ -231,7 +279,7 @@ public void monthlyTotal(Scanner sc){
231279

232280
int year;
233281
while(true){
234-
System.out.println("Enter year: ");
282+
System.out.print("Enter year: ");
235283
if(!sc.hasNextInt()){
236284
System.out.println("Enter a number for year!!!");
237285
sc.nextLine();
@@ -244,11 +292,99 @@ public void monthlyTotal(Scanner sc){
244292
}
245293
break;
246294
}
295+
showMonthlyTotal(sc,month,year);
296+
}
297+
298+
public void categoryWiseStats(Scanner sc){
299+
double food = 0,travel = 0,shopping = 0,bills = 0,others = 0;
300+
int foodItems = 0,travelItems = 0,shoppingItems=0,billItems=0,otherItems=0;
301+
for(Expense e : allExpenses){
302+
LocalDate date = e.getDate();
303+
Category category = e.getCategory();
304+
double amount = e.getAmount();
305+
switch(category){
306+
case FOOD:
307+
food += amount;
308+
foodItems++;
309+
break;
310+
case TRAVEL:
311+
travel += amount;
312+
travelItems++;
313+
break;
314+
case SHOPPING:
315+
shopping += amount;
316+
shoppingItems++;
317+
break;
318+
case BILLS:
319+
bills += amount;
320+
billItems++;
321+
break;
322+
case OTHERS:
323+
others += amount;
324+
otherItems++;
325+
break;
326+
default: continue;
327+
}
328+
}
329+
330+
System.out.println("\n------- Category-wise Statistics -------");
331+
System.out.println("Food : " + food + " (" + foodItems+ " items)");
332+
System.out.println("Shopping : " + shopping + " (" + shoppingItems+ " items)");
333+
System.out.println("Bills : " + bills + " (" + billItems+ " items)");
334+
System.out.println("Travel : " + travel + " (" + travelItems+ " items)");
335+
System.out.println("Others : " + others + " (" + otherItems+ " items)");
336+
System.out.println("-----------------------------------------");
247337
}
248338

249-
public void categoryWiseStats(Scanner sc){}
339+
public void searchExpenseByDateRange(Scanner sc){
340+
LocalDate start=null,end=null;
341+
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
342+
.appendValue(ChronoField.YEAR,4)
343+
.appendLiteral('-')
344+
.appendValue(ChronoField.MONTH_OF_YEAR)
345+
.appendLiteral('-')
346+
.appendValue(ChronoField.DAY_OF_MONTH)
347+
.toFormatter();
348+
while(true){
349+
System.out.print("Enter start Date (YYYY-MM-DD): ");
350+
String startDate = sc.nextLine();
351+
try{
352+
start = LocalDate.parse(startDate,formatter);
353+
break;
354+
}catch(Exception e){
355+
System.out.println(e.toString());
356+
System.out.println("Enter valid date!");
357+
}
358+
}
250359

251-
public void searchExpenseByDateRange(Scanner sc){}
360+
while(true){
361+
System.out.print("Enter end Date (YYYY-MM-DD): ");
362+
String endDate = sc.nextLine();
363+
try{
364+
end = LocalDate.parse(endDate,formatter);
365+
if(end.isBefore(start)){
366+
throw new IllegalArgumentException("End date should be after the start date.");
367+
}
368+
break;
369+
}catch(Exception e){
370+
System.out.println("Enter valied date!");
371+
}
372+
}
373+
double totalAmount=0;
374+
System.out.println("-------------------- ALL EXPENSES -------------------");
375+
System.out.printf("%-4s | %-10s | %-12s | %-10s | %-8s%n",
376+
"ID", "Amount", "Category", "Date", "Note");
377+
for(Expense expense : allExpenses){
378+
LocalDate date = expense.getDate();
379+
if(!date.isBefore(start) && !date.isAfter(end)){
380+
double amount = expense.getAmount();
381+
totalAmount += amount;
382+
System.out.printf("%-4d | %-10.2f | %-12s | %-10s | %-8s%n",expense.getId(),amount,expense.getCategory(),expense.getDate(),expense.getNote());
383+
}
384+
}
385+
System.out.println("-----------------------------------------------------");
386+
System.out.println("Total in range: " + totalAmount);
387+
}
252388

253389
public void exportToCSV(){}
254390

0 commit comments

Comments
 (0)