-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbudget.py
More file actions
107 lines (89 loc) · 2.72 KB
/
Copy pathbudget.py
File metadata and controls
107 lines (89 loc) · 2.72 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
def truncate(n):
multiplier = 10
return int(n * multiplier) / multiplier #tentar usar o //
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):
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=""):
self.ledger.append({"amount": amount, "description": description})
# ============================================
def withdraw (self, amount, description=""):
if (self.check_funds(amount)):
self.ledger.append({"amount": -amount, "description": description})
return True;
return False
# ============================================
def get_balance(self):
total_cash = 0
for item in self.ledger:
total_cash += item["amount"]
return total_cash
# ============================================
def transfer (self, amount, category):
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):
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