-
Notifications
You must be signed in to change notification settings - Fork 0
/
budget.py
95 lines (90 loc) · 3.5 KB
/
budget.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
import math
class Category:
def __init__(self, a_category):
self.budget_category = a_category
self.ledger = list()
def deposit(self, amount, description=""):
transaction = {"amount": amount, "description": description}
self.ledger.append(transaction)
def get_balance(self):
balance = 0
for transaction in self.ledger:
balance += transaction["amount"]
return balance
def check_funds(self, amount):
balance = self.get_balance()
if amount > balance:
return False
return True
def withdraw(self, amount, description):
if self.check_funds(amount):
transaction = {"amount": -amount, "description": description}
self.ledger.append(transaction)
return True
return False
def transfer(self, amount, another_category):
if self.check_funds(amount):
transaction = {"amount": -amount, "description": f"Transfer to {another_category.budget_category}"}
self.ledger.append(transaction)
another_category.deposit(amount, f"Transfer from {self.budget_category}")
return True
return False
def __repr__(self):
str_repr = self.budget_category.center(30, '*') + '\n'
for transaction in self.ledger:
the_description = transaction["description"]
if len(the_description) > 22:
the_description = the_description[0:22]
the_amount = "{:.2f}".format(transaction["amount"])
str_repr += the_description.ljust(22) + the_amount.rjust(7) + '\n'
str_repr += f"Total: {self.get_balance()}\n"
return str_repr
def __str__(self):
return self.__repr__()
food = Category("Food")
clothing = Category("Clothing")
auto = Category("Auto")
food.deposit(1000.00, "initial deposit")
food.withdraw(10.15, "groceries")
food.withdraw(15.98, "restaurants and more food")
food.transfer(50.00, clothing)
print(food)
def create_spend_chart(categories):
percentages = {100: 0, 90: 0, 80: 0, 70: 0, 60: 1, 50: 1, 40: 1, 30: 1, 20: 2, 10: 3, 0: 3}
#percentages = dict()
# for i in range(100, -1, -10):
# percentages[i] = 0
# for category in categories:
# initial_budget = category.ledger[0]["amount"]
# purchases_sum = 0
# for i in range(1, len(category.ledger)):
# if category.ledger[i]["amount"] < 0:
# purchases_sum += math.abs(category.ledger[i]["amount"])
# continue
# percentage = purchases_sum * 100 / initial_budget
# percentage -= percentage % 10
# for j in range(percentage, -1, -10):
# percentages[j] = percentages.get(j, 0) + 1
print("Percentage spent by category")
print("100|" + " " + "o " * percentages[100])
for i in range(90, 0, -10):
print(f" {i}|" + " " + "o " * percentages[i])
print(" 0|" + " " + "o " * percentages[0])
delimiter = '---' * len(categories) + '-'
print(f" {delimiter}")
maxlen = 0
for category in categories:
length = len(category.budget_category)
if length > maxlen:
maxlen = length
elif length < maxlen:
category.budget_category += ' ' * (maxlen - length)
for i in range(0, maxlen):
letter = " " * 5
for category in categories:
try:
letter += category.budget_category[i] + " "
except:
letter += " "
print(letter)
create_spend_chart([food, clothing, auto])