Repository navigation
Expand file tree
/
Copy pathbudget_app.py
More file actions
86 lines (67 loc) · 2.89 KB
/
Copy pathbudget_app.py
File metadata and controls
86 lines (67 loc) · 2.89 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
class Category:
def __init__(self, category):
self.category = category
self.ledger = []
def deposit(self, amount, description=""):
self.ledger.append({"amount": amount, "description": description})
def withdraw(self, amount, description=""):
if self.check_funds(amount):
self.ledger.append({"amount": -amount, "description": description})
return True
return False
def get_balance(self):
return sum(item["amount"] for item in self.ledger)
def transfer(self, amount, budget_category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {budget_category.category}")
budget_category.deposit(amount, f"Transfer from {self.category}")
return True
return False
def check_funds(self, amount):
return amount <= self.get_balance()
def __str__(self):
output = f"{'*' * 30}\n{self.category.center(30, '*')}\n"
for item in self.ledger:
output += f"{item['description'][:23]:23}" + f"{item['amount']:>7.2f}\n"
output += f"Total: {self.get_balance():.2f}"
return output
def create_spend_chart(categories):
spendings = [sum(item['amount'] for item in category.ledger if item['amount'] < 0) for category in categories]
total_spent = sum(spendings)
percentages = [(spending / total_spent) * 100 if total_spent > 0 else 0 for spending in spendings]
chart = "Percentage spent by category\n"
for i in range(100, -1, -10):
chart += str(i).rjust(3) + "| "
for percentage in percentages:
if percentage >= i:
chart += "o "
else:
chart += " "
chart += "\n"
chart += " -" + "---" * len(categories) + "\n"
max_category_name_length = max(len(category.category) for category in categories)
category_names = [category.category.ljust(max_category_name_length) for category in categories]
for i in range(max_category_name_length):
chart += " "
for name in category_names:
chart += name[i] + " "
chart += "\n"
return chart.rstrip("\n")
# Testing the Category class
if __name__ == "__main__":
food_category = Category("Food")
clothing_category = Category("Clothing")
auto_category = Category("Auto")
food_category.deposit(1000, "initial deposit")
food_category.withdraw(10.15, "groceries")
food_category.withdraw(15.89, "restaurant and more food")
food_category.transfer(50, clothing_category)
clothing_category.deposit(500, "initial deposit")
clothing_category.transfer(50, food_category)
print(food_category)
print(clothing_category)
print(auto_category)
# Testing the create_spend_chart function
categories = [food_category, clothing_category, auto_category]
chart = create_spend_chart(categories)
print(chart)