-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.py
More file actions
81 lines (59 loc) · 2.3 KB
/
code.py
File metadata and controls
81 lines (59 loc) · 2.3 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
class Budget:
def __init__(self, name, balance):
self.name = name
self.balance = balance
self.transactions = []
def deposit(self, amount, note=""):
if amount > 0:
self.balance += amount
self.transactions.append((amount, note))
return True
return False
def withdraw(self, amount, note=""):
if amount > 0 and self.check_funds(amount):
self.balance -= amount
self.transactions.append((amount, note))
return True
return False
def get_balance(self):
return self.balance
def check_funds(self, amount):
return amount <= self.balance
def get_transactions(self):
return self.transactions
class Expense(Budget):
def __init__(self, name, budget):
super().__init__(name, 0) # Initialize balance as 0
self.budget = budget
def get_budget(self):
return self.budget
def get_balance_left(self):
return self.budget - self.balance
def main():
try:
# Create an expense budget
expense_budget = Expense("Expense", int(input("Enter your budget:")))
# Deposit money into the expense budget
if expense_budget.deposit(int(input("Enter amount deposited:")), "Money deposited"):
print("Deposit successful")
else:
print("Invalid deposit amount")
# Withdraw money from the expense budget
if expense_budget.withdraw(int(input("Enter amount withdrawn:")), "Money spent"):
print("Withdrawal successful")
else:
print("Insufficient funds or invalid withdrawal amount")
# Get the balance of the expense budget
print("Current balance:", expense_budget.get_balance())
# Get the budget of the expense budget
print("Budget:", expense_budget.get_budget())
# Get the balance left in the expense budget
print("Balance left:", expense_budget.get_balance_left())
# Get the transactions of the expense budget
print("Transactions:", expense_budget.get_transactions())
except:
print("wrong input , try again")
main()
if __name__ == '__main__':
main()
# added try catch....