-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbudget.py
124 lines (101 loc) · 4.15 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def truncate(n):
multiplier = 10
return int(n * multiplier) / multiplier
def getTotals(categories):
total = 0
breakdown = []
for Category in categories:
total += Category.get_withdrawls()
breakdown.append(Category.get_withdrawls())
rounded = list(map(lambda x: truncate(x/total), breakdown))
return rounded
def create_spend_chart(categories):
"""
create a function (outside of the class) called create_spend_chart that takes a list of categories as an argument. It should return a string that is a bar chart.
"""
res = "Percentage spent by category\n"
i= 100
totals = getTotals(categories)
while i >=0:
cat_spaces = " "
for total in totals:
if total * 100>= i:
cat_spaces +="o "
else:
cat_spaces += " "
res += str(i).rjust(3) + "|" + cat_spaces + ("\n")
i -= 10
dashes = "-" + "---"*len(categories)
names = []
x_axis = ""
for Category in categories:
names.append(Category.name)
maxi = max(names, key= len)
for x in range(len(maxi)):
nameStr = ' '
for name in names:
if x >= len(name):
nameStr += " "
else:
nameStr += name[x] + " "
if (x != len(maxi) -1):
nameStr += '\n'
x_axis += nameStr
res += dashes.rjust(len(dashes)+4) + "\n" + x_axis
return res
class Category:
def __init__(self,name):
self.name= name
self.ledger = list()
def __str__(self):
title = f"{self.name:*^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"]
output = title + items + "Total: " + str(total)
return output
def deposit(self, amount, description=""):
"""
A deposit method that accepts an amount and description. If no description is given, it should default to an empty string. The method should append an object to the ledger list in the form of {"amount":amount, "description":description}.
"""
self.ledger.append({"amount":amount,"description":description})
def withdraw(self,amount,description=""):
"""
A withdraw method that is similar to the deposit method, but the amount passed in should be stored in the ledger as a negative number. If there are not enough funds, nothing should be added to the ledger. This method should return True if the withdrawal took place, and False otherwise.
"""
if (self.check_funds(amount)):
self.ledger.append({"amount":-amount,"description":description})
return True;
return False
def get_balance(self):
"""
A get_balance method that returns the current balance of the budget category based on the deposits and withdrawals that have occurred.
"""
total_cash= 0
for item in self.ledger:
total_cash += item["amount"]
return total_cash
def transfer(self,amount,Category):
"""
A transfer method that accepts an amount and another budget category as arguments. The method should add a withdrawal with the amount and the description "Transfer to [Destination Budget Category]". The method should then add a deposit to the other budget category with the amount and the description "Transfer from [Source Budget Category]". If there are not enough funds, nothing should be added to either ledgers. This method should return True if the transfer took place, and False otherwise.
"""
if (self.check_funds(amount)):
self.withdraw(amount,"Transfer to " + Category.name)
Category.deposit(amount, "Transfer from "+ self.name)
return True
return False
def check_funds(self, amount):
"""
A check_funds method that accepts an amount as an argument. It returns False if the amount is greater than the balance of the budget category and returns True otherwise. This method should be used by both the withdraw method and transfer method.
"""
if (self.get_balance() >= amount):
return True
return False
def get_withdrawls(self):
total = 0
for item in self.ledger:
if item['amount'] < 0:
total += item['amount']
return total