-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBudget.py
More file actions
138 lines (105 loc) · 4.61 KB
/
Copy pathBudget.py
File metadata and controls
138 lines (105 loc) · 4.61 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#Complete the Category class in budget.py.
#different budget categories like food, clothing,
#objects are created, passed in category name.
#The class have an instance variable called ledger
#that is a list.
class Category:
def __init__(self, category_name):
self.category_name = category_name
self.ledger = []
self.budget = 0
#A deposit method that accepts an amount and description.
#If no description is given, it should default to an empty string.
#The method append an object to the ledger
#the form {"amount": amount, "description": description}.
def deposit(self, amount: float, description=""):
self.ledger.append({"amount": amount, "description":description})
self.budget += amount
#A check_funds method that accepts an amount as an argument. It returns False if the amount is greater than the budget of the budget category and returns True otherwise. This method should be used by both the withdraw method and transfer method.
def check_funds(self, amount):
if self.get_balance() >= amount:
return True
else:
return False
#A withdraw method that is similar to the deposit method,
#but the amount stored in the ledger as a negative number.
#¡¡ If there are not enough funds, nothing should be added to the ledger.
#¡¡ This method return True if the withdrawal took place, False otherwise.
def withdraw(self, amount: float, description=""):
if self.check_funds(amount):
self.ledger.append({"amount": - amount, "description":description})
self.budget -= amount
return True
else:
return False
#A get_budget method that returns the current budget
#of the budget category based on the deposits and withdrawals
def set_balance(self, amount):
self.budget = amount
def get_balance(self):
return self.budget
#A transfer method that accepts an amount
#and another budget category as arguments.
#add a withdrawal with the amount and the description "Transfer to [Destination Budget Category]".
#add a deposit to the other budget category with the amount
#and the description "Transfer from [Source Budget Category]".
#This method should return True if the transfer took place,
#and False otherwise.
def transfer(self, amount, category_budget):
if self.check_funds(amount) == True:
self.withdraw(amount, f"Transfer to {category_budget.category_name}")
category_budget.deposit(amount, f"Transfer from {self.category_name}")
return True
else:
return False
# second part - how the print should look:
def __str__(self):
title_line = self.category_name.center(30, "*") + "\n"
layout = title_line
for i in self.ledger:
all_items = f"{i['description'][:23].ljust(23)}{format(i['amount'], '.2f').rjust(7)}\n"
layout += all_items
category_total = f"Total: {format(self.get_balance(), '.2f')}"
layout += category_total
return layout
#third part - graph
def create_spend_chart(categories):
category_names = []
spent = []
total_expenditure = []
for category in categories:
total = 0
for i in category.ledger:
if i['amount'] < 0:
total -= i['amount']
spent.append(round(total, 2))
category_names.append(category.category_name)
for percents in spent:
total_expenditure.append(round(percents/sum(spent), 2)*100)
graph_layout = "Percentage spent by category\n"
lines = range(100,-10,-10)
# from internet, similar to first project
# go through it again later!!
for line_names in lines:
graph_layout += str(line_names).rjust(3) + "| "
for percents in total_expenditure:
if percents >= line_names:
graph_layout += "o "
else:
graph_layout += " "
graph_layout += "\n"
graph_layout += " ----" + ("---" * (len(category_names) - 1))
graph_layout += "\n "
len_longest = 0
for names in category_names:
if len_longest < len(names):
len_longest = len(names)
for i in range(len_longest):
for names in category_names:
if len(names) > i:
graph_layout += names[i] + " "
else:
graph_layout += " "
if i < len_longest - 1:
graph_layout += "\n "
return graph_layout