-
Notifications
You must be signed in to change notification settings - Fork 0
/
budget.py
86 lines (71 loc) · 2.8 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
class Category:
def __init__(self, name):
self.name = name # It will allow the budget.Category("Food").
self.ledger = []
def deposit(self, amount, description=""):
self.ledger.append({"amount": amount, "description": description}) # Add the deposit to the list.
def get_balance(self, amount=0):
balance = 0
for diposit in self.ledger: # Get every diposit on ledger list.
balance += diposit["amount"]
return balance
def checkFunds(self, amount):
return amount <= self.get_balance(amount) # Check if the amount is less than the balance got.
def withdraw(self, amount, description=""):
if self.checkFunds(amount): # If checkFunds function returns true, it will store the withdraw.
self.ledger.append({"amount": -amount, "description": description})
return True
else:
return False
def transfer(self, amount, category):
if (self.checkFunds(amount)):
self.withdraw(amount, f"Transfer to {Category}")
category.deposit(amount, f"Transfer from {self.name}")
return True
else:
return False
def __str__(self):
title = f"{self.name.center(30, '*')}\n" # Specify the string to center, the width, and the characters that will fill out the string.
itemList = ""
total = 0
amount = 0
for item in self.ledger:
amount = f"{item['amount']:>7.2f}"
itemList += f"{item['description'][0:23]}" + f"{amount}\n"
total += item['amount']
str = title + itemList + f"Total: {total}"
return str
def create_spend_chart(categories):
cateogryNames = []
withdraws = []
for category in categories:
total = 0
for item in category.ledger:
if item['amount'] < 0:
total -= item['amount']
withdraws.append(round(total, 2))
cateogryNames.append(category.name)
percentages = []
for amount in withdraws:
percentages.append(round(amount/sum(withdraws) * 100))
graphBar = "Percentage spent by category\n"
for i in range(100, 0, -10):
graphBar += str(i).rjust(3) + "| "
for perc in percentages:
if perc >= i:
graphBar += "o "
else:
graphBar += " "
graphBar += "\n"
graphBar += " " + "-" * (len(categories) * 3 + 1) + "\n"
longestNameLen = max([len(name) for name in cateogryNames])
for i in range(longestNameLen):
graphBar += " "
for name in cateogryNames:
if i < len(name):
graphBar += name[i] + " "
else:
graphBar += " "
if i != longestNameLen - 1:
graphBar += "\n"
return graphBar