-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
182 lines (135 loc) · 5.84 KB
/
main.py
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
import locale
import datetime
import sqlite3
import json
import random
# Set the locale for formatting
locale.setlocale(locale.LC_ALL, '')
class Expense:
def __init__(self, title, amount, category, date=None):
self.title = title
self.date = date or datetime.datetime.now().strftime("%d/%m/%Y")
self.amount = amount
self.category = category
def __str__(self):
formatted_amount = locale.format_string("%.2f", self.amount, grouping=True)
return f"New expense added on {self.date} of ₹{formatted_amount} in {self.category}"
# Rest of your script remains unchanged
def get_random_greeting():
with open("greetings.json", "r") as file:
greetings_data = json.load(file)
greetings_list = greetings_data.get("greetings", [])
return random.choice(greetings_list)
def insert_expense(cursor, expense):
cursor.execute(
"INSERT INTO expenses (title, date, amount, category) VALUES (?, ?, ?, ?)",
(expense.title, expense.date, expense.amount, expense.category),
)
connection.commit()
def print_history(cursor):
rows = cursor.execute("SELECT * FROM expenses").fetchall()
if not rows:
print("Empty! Add an expense to get started.")
else:
print("\nExpense History:\n")
for row in rows:
formatted_amount = locale.format_string("%.2f", row[3], grouping=True)
print(
f"ID: {row[0]}, Title: {row[1]}, Date: {row[2]}, Amount: ₹{formatted_amount}, Category: {row[4]}"
)
total = cursor.execute("SELECT SUM(amount) FROM expenses").fetchone()[0]
formatted_total = locale.format_string("%.2f", total, grouping=True)
print(f"\nTotal: ₹{formatted_total}")
def delete_expense(cursor, id):
cursor.execute("DELETE FROM expenses WHERE ID=?", (id,))
cursor.execute("UPDATE expenses SET ID = ID - 1 WHERE ID > ?", (id,))
cursor.execute("UPDATE SQLITE_SEQUENCE SET SEQ=SEQ-1 WHERE NAME='expenses'")
connection.commit()
def get_expense(cursor, id):
cursor.execute("SELECT * FROM expenses WHERE ID=?", (id,))
row = cursor.fetchone()
formatted_amount = locale.format_string("%.2f", row[3], grouping=True)
print(f"\nID: {row[0]}, Title: {row[1]}, Date: {row[2]}, Amount: ₹{formatted_amount}, Category: {row[4]}")
def edit_expense(cursor, id):
get_expense(cursor, id)
field_to_edit = input("\nWhat do you want to edit? (Enter the number) \n1. Title \n2. Date \n3. Amount \n4. Category \nEnter your choice:")
if field_to_edit == "1":
title = input("\nEnter the new title: ")
cursor.execute("UPDATE expenses SET title = ? WHERE id = ?", (title, id) )
connection.commit()
print("\nTitle updated successfully!")
print ("\nUpdated Expense: ")
get_expense(cursor, id)
elif field_to_edit == "2":
date = input("\nEnter the new date (dd/mm/yyyy): ")
cursor.execute("UPDATE expenses SET date = ? WHERE id = ?", (date, id) )
connection.commit()
print("\nDate updated successfully!")
print ("\nUpdated Expense: ")
get_expense(cursor, id)
elif field_to_edit == "3":
amount = input("\nEnter the new amount: ")
cursor.execute("UPDATE expenses SET amount = ? WHERE id = ?", (amount, id) )
connection.commit()
print("\nAmount updated successfully!")
print ("\nUpdated Expense: ")
get_expense(cursor, id)
elif field_to_edit == "4":
category = input("\nEnter the new category: ")
cursor.execute("UPDATE expenses SET category = ? WHERE id = ?", (category, id) )
connection.commit()
print("\nCategory updated successfully!")
print ("\nUpdated Expense: ")
get_expense(cursor, id)
else:
print("Invalid choice. Please try again.")
try:
connection = sqlite3.connect("expenses.db")
cursor = connection.cursor()
cursor.execute(
"CREATE TABLE IF NOT EXISTS expenses (ID INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, date TEXT, amount REAL, category TEXT)"
)
print(get_random_greeting())
print("-------------------------------")
print("Note: You can press Ctrl+C to exit/stop operation at any time.")
while True:
print("\nWhat do you want to do? (Enter the number)")
print("1. Add Expense")
print("2. Print Expense History")
print("3. Delete Expense")
print("4. Edit Expense")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == "1":
title = input("\nEnter the title of the expense: ")
while True:
amount = input("Enter the amount of the expense: ")
try:
amount = float(amount)
break
except ValueError:
print("Invalid amount. Please enter a number.")
category = input("Enter the category of the expense: ")
new_expense = Expense(title, amount, category)
insert_expense(cursor, new_expense)
print("\n" + str(new_expense))
elif choice == "2":
print_history(cursor)
elif choice == "3":
print_history(cursor)
id_to_delete = input("\nEnter the ID of the expense to delete: ")
delete_expense(cursor, id_to_delete)
print("\nExpense deleted successfully!")
elif choice == "4":
print_history(cursor)
id_to_edit = input("\nEnter the ID of the expense to edit: ")
edit_expense(cursor, id_to_edit)
elif choice == "5" or choice.lower() == "exit":
print("\nExiting...")
break
else:
print("Invalid choice. Please try again.")
except KeyboardInterrupt:
print("\nExiting...")
finally:
connection.close()