forked from Mrinank-Bhowmick/python-beginner-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
123 lines (97 loc) · 3.32 KB
/
main.py
File metadata and controls
123 lines (97 loc) · 3.32 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
import sqlite3
# Connect to the database
conn = sqlite3.connect("expenses.db")
cursor = conn.cursor()
# Define SQL statements
create_table_sql = """CREATE TABLE IF NOT EXISTS expenses (id INTEGER PRIMARY KEY AUTOINCREMENT, date TEXT, description TEXT, amount REAL)"""
insert_expense_sql = "INSERT INTO expenses (date, description, amount) VALUES (?, ?, ?)"
select_expenses_sql = "SELECT * FROM expenses ORDER BY date;"
delete_expense_by_id_sql = "DELETE FROM expenses WHERE id=?"
update_expense_by_id_sql = "UPDATE expenses SET description=? WHERE id=?"
# Check if the database exists, create it if not
if not conn.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='expenses';"
).fetchone():
cursor.execute(create_table_sql)
conn.commit()
def add_expense():
global cursor
# Get input from user
print("Enter date (YYYY-MM-DD): ")
date = input().split()[0] + "-01"
print("Enter description: ")
description = input()
print("Enter amount: ")
amount = float(input())
try:
cursor.execute(insert_expense_sql, (date, description, amount))
conn.commit()
print("Expense added successfully.")
except Exception as e:
print("Error adding expense: {}".format(e))
def delete_expense():
global cursor
# Get ID from user
print("Enter ID to delete: ")
id = int(input())
try:
cursor.execute(delete_expense_by_id_sql, (id,))
conn.commit()
print("Expense deleted successfully.")
except Exception as e:
print("Error deleting expense: {}".format(e))
def update_expense():
global cursor
# Get ID and new description from user
print("Enter ID to update: ")
id = int(input())
print("Enter new description: ")
description = input()
try:
cursor.execute(update_expense_by_id_sql, (description, id))
conn.commit()
print("Expense updated successfully.")
except Exception as e:
print("Error updating expense: {}".format(e))
def view_expenses():
# Fetch and display expenses
expenses = conn.execute(select_expenses_sql).fetchall()
if not expenses:
print("No expenses recorded yet.")
else:
print("Expenses:")
for expense in expenses:
print(
f"ID: {expense[0]}, Date: {expense[1]}, Description: {expense[2]}, Amount: Rs: {expense[3]}"
)
def total_expenses():
# Calculate and display total expenses
total = conn.execute("SELECT SUM(amount) FROM expenses;").fetchone()[0]
print(f"Total expenses: {total}$")
def main_menu():
while True:
print("\nExpense Tracker Menu:")
print("1. Add Expense")
print("2. View Expenses")
print("3. Total Expenses")
print("4. Delete Expense")
print("5. Update Expense Description")
print("6. Quit")
choice = input("Enter your choice (1-6): ")
if choice == "1":
add_expense()
elif choice == "2":
view_expenses()
elif choice == "3":
total_expenses()
elif choice == "4":
delete_expense()
elif choice == "5":
update_expense()
elif choice == "6":
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main_menu()
conn.close()