1010select_expenses_sql = "SELECT * FROM expenses ORDER BY date;"
1111delete_expense_by_id_sql = "DELETE FROM expenses WHERE id=?"
1212update_expense_by_id_sql = "UPDATE expenses SET description=? WHERE id=?"
13+ maximum_amount_spent_sql = "SELECT MAX(amount) FROM expenses"
14+ minimum_amount_spent_sql = "SELECT MIN(amount) FROM expenses"
15+ average_amount_spent_sql = "SELECT AVG(amount) FROM expenses"
16+ total_amount_spent_sql = "SELECT SUM(amount) FROM expenses"
17+ total_number_of_expenses_sql = "SELECT COUNT(amount) FROM expenses"
1318
1419# Check if the database exists, create it if not
1520if not conn .execute (
@@ -93,7 +98,7 @@ def view_expenses():
9398def total_expenses ():
9499 # Calculate and display total expenses
95100 total = conn .execute ("SELECT SUM(amount) FROM expenses;" ).fetchone ()[0 ]
96- print (f"Total expenses: { total } $ " )
101+ print (f"Total expenses: Rs: { total } " )
97102
98103
99104# defining function for exporting to csv
@@ -247,10 +252,34 @@ def data_filter():
247252# defined this funtion to check is database is empty or not
248253def is_empty ():
249254 if conn .execute (select_expenses_sql ).fetchall () == []:
250- return True
255+ print ( "The Database is empty" )
251256 else :
252- return False
253-
257+ print ("The Database is not empty" )
258+
259+ # expense analysis function
260+ def analyse_expense ():
261+ """
262+ This function analyses the expense and gives the:
263+ 1. Maximum amount spent
264+ 2. Minimum amount spent
265+ 3. Average amount spent
266+ 4. Total amount spent
267+ 5. Total number of expenses
268+ """
269+ print ("\n ***EXPENSE ANALYSIS***\n " )
270+
271+ maximum_amount_spent = conn .execute (maximum_amount_spent_sql ).fetchall ()
272+ print (f"a. Maximum amount spent = { maximum_amount_spent [0 ][0 ] } " )
273+ minimum_amount_spent = conn .execute (minimum_amount_spent_sql ).fetchall ()
274+ print (f"b. Minimum amount spent = { minimum_amount_spent [0 ][0 ] } " )
275+ average_amount_spent = conn .execute (average_amount_spent_sql ).fetchall ()
276+ print ("c. Average amount spent = %.2f" % average_amount_spent [0 ][0 ])
277+ total_amount_spent = conn .execute (total_amount_spent_sql ).fetchall ()
278+ print (f"d. Total amount spent = { total_amount_spent [0 ][0 ] } " )
279+ total_number_of_expenses = conn .execute (total_number_of_expenses_sql ).fetchall ()
280+ print (f"e. Total number of expenses = { total_number_of_expenses [0 ][0 ] } " )
281+
282+ print ("\n ***END OF EXPENSE ANALYSIS***" )
254283
255284def main_menu ():
256285 # conn=db_init()
@@ -263,7 +292,9 @@ def main_menu():
263292 print ("5. Update Expense Description" )
264293 print ("6. Export Expense" )
265294 print ("7. Data Filter" )
266- print ("8. Quit" )
295+ print ("8. Check if the database is Empty" )
296+ print ("9. Analyze Expense" )
297+ print ("10. Quit" )
267298
268299 choice = input ("Enter your choice (1-8): " )
269300
@@ -283,6 +314,10 @@ def main_menu():
283314 data_filter ()
284315 elif choice == "8" :
285316 is_empty ()
317+ elif choice == "9" :
318+ analyse_expense ()
319+ elif choice == "10" :
320+ exit (0 )
286321 else :
287322 print ("Invalid choice. Please try again." )
288323
0 commit comments