-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbudget-app.py
145 lines (140 loc) · 4.13 KB
/
budget-app.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class Category:
funds = 0
total_spent = 0
def check_funds(self, amount):
if self.funds < amount:
return False
else:
return True
def __str__(self):
mystr = self.category.center(30, '*') + "\n"
for i in self.ledger:
mystr = mystr + "{:<23}".format(i["description"][0: 23]) + format(i["amount"], '.2f').rjust(7) + "\n"
mystr = mystr + "Total: " + str(self.funds)
return mystr
def __init__(self, category):
self.ledger = []
self.category = category
self.category_spent = 0
def deposit(self, amount, description=''):
self.funds = self.funds + amount
self.ledger.append({"amount": amount, "description": description})
def withdraw (self, amount, description=''):
if self.check_funds(amount):
self.category_spent = self.category_spent + amount
Category.total_spent = Category.total_spent + amount
self.funds = self.funds - amount
self.ledger.append({"amount": -amount, "description": description})
return True
else:
return False
def get_balance (self):
return self.funds
def transfer(self, amount, budget_category):
transfer_to = "Transfer to " + budget_category.category
transfer_from = "Transfer from " + self.category
if self.check_funds(amount):
self.withdraw(amount, transfer_to)
budget_category.deposit(amount, transfer_from)
return True
else:
return False
def create_spend_chart(categories):
mystr = "Percentage spent by category" + "\n"
mylist = []
horizontal_len = len(categories)
myobjects = []
for i in categories:
myobjects.append(i)
# print(myobjects)
mypercentages = []
for i in range(0, horizontal_len):
per = (categories[i].category_spent/Category.total_spent)*100
per = int(per)
val = per - (per%10)
mypercentages.append(val)
# print(mypercentages)
for i in range(0, (horizontal_len+1)*11):
mylist.append([])
m = 100
for i in range(0, (horizontal_len+1)*11, 4):
mylist[i].append(m)
m = m-10
for _ in range(0, horizontal_len):
k=0
for i in range(_+1, (horizontal_len+1)*11, 4):
if(mylist[k][0]<=mypercentages[_]):
mylist[i] = "o"
else:
mylist[i]=None
k = k + 4
# for i in range(0, 4):
# print(" ", end = "")
# print("-", end = "")
# for i in range(0, horizontal_len):
# print("---", end = "")
# print (mylist)
#biggest length
maxl = 0
for i in range(0, horizontal_len):
if(len(categories[i].category) > maxl):
maxl = len(categories[i].category)
# print(maxl)
lmatrix = []
for i in range(0, maxl*horizontal_len):
lmatrix.append([])
# print(lmatrix)
# for i in range(0, horizontal_len):
# for j in range(0, len(categories[i].category)):
# # print(j, end="")
# print(categories[i].category[j], end="")
# pass
# print()
for kr in range(0, len(categories)):
k = kr
m = kr
n = 0
for i in range(0, len(categories[kr].category)):
if(m < len(categories[m].category)):
lmatrix [k] = categories[m].category[n:n+1]
n+=1
k+=len(categories)
for i in range(0, len(lmatrix)):
if isinstance(lmatrix[i], list):
lmatrix[i]=None
# print(lmatrix)
temp = 0
for i in mylist:
if i != 'o' and i!=None:
if i[0] == 100:
mystr = mystr + str(i[0]) + "| "
elif i[0] == 0:
mystr = mystr + " " + str(i[0]) + "| "
else:
mystr = mystr + " " + str(i[0]) + "| "
if i == None:
mystr = mystr + " "
if i == 'o':
mystr = mystr + 'o '
temp = temp + 1
if (temp % (horizontal_len+1))==0:
mystr = mystr + "\n"
mystr = mystr + " "
mystr = mystr + "-"
temp = 0
for i in range(0, horizontal_len):
# print("---", end = "")
mystr = mystr + "---"
mystr = mystr + "\n"
mystr = mystr + " "
for i in lmatrix:
if i == None:
mystr = mystr + " "
if i!= None:
mystr = mystr + i + " "
temp = temp +1
if ((temp % horizontal_len) == 0 and i!=(maxl*horizontal_len)):
mystr = mystr + "\n" + " "
# print(mystr)
mystr = mystr[0:len(mystr)-6]
return mystr