-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbudget_app.py
90 lines (77 loc) · 3.18 KB
/
budget_app.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
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
class Category:
def __init__(self, budget):
self.budget = budget
self.ledger = []
def deposit(self, amount, description = None):
if description is None:
self.description = []
self.ledger.append({'amount': amount, 'description': ''})
else:
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description = None):
if self.check_funds(amount):
if description is None:
self.description = []
self.ledger.append({'amount': -amount, 'description': ''})
else:
self.ledger.append({'amount': -amount, 'description': description})
return True
else:
return False
def get_balance(self):
current_balance = 0
for item in self.ledger:
current_balance += item['amount']
return current_balance
def transfer(self, amount, another_budget):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {another_budget.budget}")
another_budget.deposit(amount, f"Transfer from {self.budget}")
return True
return False
def check_funds(self, amount):
if (amount > self.get_balance()):
return False
return True
def __str__(self):
title = f"{self.budget:*^30}\n"
items = ""
total = 0
for item in self.ledger:
items += f"{item['description'][0:23]:23}" + f"{item['amount']:>7.2f}" + "\n"
total += item['amount']
return title + items + 'Total: ' + str(total)
def create_spend_chart(categories):
def withdrawals(category): #A function to get total withdrawals for each budget category
individual = 0
for item in category.ledger:
if item['amount'] < 0:
individual += item['amount']
return individual
total_spent = sum(withdrawals(i) for i in categories)
# Calculate spending percentages for each category
percentages = []
for i in categories:
one = withdrawals(i)
percentage = round(((one / total_spent)*10 // 1)*10) #need for rounding to 10s
percentages.append(percentage)
max_length = max(len(category.budget) for category in categories) # Determine the longest category name for proper spacing
chart = "Percentage spent by category\n" # Create the chart
for i in range(100, -1, -10): #The percentages, vertical |, and 'o's
chart += str(i).rjust(3) + "| "
for percentage in percentages:
if percentage >= i:
chart += "o "
else:
chart += " "
chart += "\n"
chart += " " + "-" * (3 * len(categories) + 1) # Add the horizontal line
for x in range(max_length): #For the vertically written budget category names underneath the horizontal line
category_names = ' '.join(category.budget[x] if x < len(category.budget) else ' ' for category in categories)
chart += "\n " + category_names + " "
return chart