###### budget.py #####class Category:
def __init__(self, category_name):
self.name = category_name
self.ledger = []
self.ledger.append(self.name.center(30, '*'))
def amount_adjust(self, amount):
self.amount = amount
amount = format(amount, '.2f')
amount = str(amount)
if len(amount) > 7:
amount = amount[0:7]
amount = amount.rjust(7)
return amount
def description_adjust(self, description=None):
self.description = description
if description == None:
description = ''.ljust(23)
else:
description = description[0:23].ljust(23)
return description
def deposit(self, amount, description=None):
deposited = self.description_adjust(description) + self.amount_adjust(amount)
self.ledger.append(deposited)
def withdraw(self, amount, description=None):
if self.check_funds(amount) == True:
amount = (-1) * amount
withdrawn = self.description_adjust(description) + self.amount_adjust(amount)
self.ledger.append(withdrawn)
return True
else:
return False
def transfer(self, amount, sent_to):
if self.check_funds(amount) == True:
description = "Transfer to " + sent_to.name
self.withdraw(amount, description)
description2= "Transfer from " + self.name
sent_to.deposit(amount, description2)
return True
else:
return False
def get_balance(self):
Total = 0.00
for i in range(1, len(self.ledger)):
amount = (self.ledger[i])[23:30]
amount = amount.split()
amount =''.join(amount)
if len(amount) > 0:
Total = Total + float(amount)
return float(format(Total, '.2f'))
def check_funds(self, amount):
if float(self.get_balance()) < amount:
return False
else:
return True
def __str__(self):
self.ledger.append("Total: " + str(self.get_balance()))
d = '\n'.join(self.ledger)
return d
def create_spend_chart(categories):
print('Percentage spent by category')
sumlist = []
for category in categories:
sums = 0.00
for i in range(1, len(category.ledger)):
amount = (category.ledger[i])[23:30]
amount = amount.split()
amount =''.join(amount)
if len(amount) > 0 and float(amount) < 0:
withdrawal = (-1) * float(amount)
sums = sums + withdrawal
sums = (sums // 10) * 10
sumlist.append(sums)
for j in range(100, -10, -10):
percentage = str(j).rjust(3,' ') + '|'
c = 0
for i in range(0, len(sumlist)):
if sumlist[i] >= j:
c = c + 1
print(percentage + ' o' * min(1, c) + ' o' * (c - 1))
l = len(percentage + ' o'*c)
print(' ' + ''.rjust(l-3, '-'))
length = 0
clist = []
returner = []
for category in categories:
clist.append(category.name)
length = max(length, len(category.name))
for j in range(0, length):
letter = []
for i in range(0,len(clist)):
entry = clist[i]
if j < len(entry):
letter.append(entry[j])
else:
letter.append(' ')
returner.append(5 * ' ' + ' '.join(letter) + 2 * ' ')
return '\n'.join(returner)
# This entrypoint file to be used in development. Start by reading README.md
import budget
from budget import create_spend_chart
food = budget.Category("Food")
food.deposit(1000, "initial deposit")
food.withdraw(10.15, "groceries")
food.withdraw(15.89, "restaurant and more food for dessert")
print(food.get_balance())
clothing = budget.Category("Clothing")
food.transfer(50, clothing)
clothing.withdraw(25.55)
clothing.withdraw(100)
auto = budget.Category("Auto")
auto.deposit(1000, "initial deposit")
auto.withdraw(15)
print(food)
print(clothing)
print(create_spend_chart([food, clothing, auto]))
973.96
*************Food*************
initial deposit 1000.00
groceries -10.15
restaurant and more foo -15.89
Transfer to Clothing -50.00
Total: 923.96
***********Clothing***********
Transfer from Food 50.00
-25.55
Total: 24.45
Percentage spent by category
100|
90|
80|
70| o
60| o
50| o
40| o
30| o
20| o o
10| o o o
0| o o o
----------
F C A
o l u
o o t
d t o
h
i
n
g
food = budget.Category("Food")
entertainment = budget.Category("Entertainment")
#food.deposit(900, "deposit")
#food.withdraw(45.67)
food.transfer(20, entertainment)
food.deposit(900, "deposit")
food.withdraw(45.67, "milk, cereal, eggs, bacon, bread")
print(food)
#print(food.get_balance())
#print(food.withdraw(100.10))
#print(food.check_funds(30))
#print(food.transfer(200, entertainment))
print(entertainment)
*************Food************* deposit 900.00 milk, cereal, eggs, bac -45.67 Total: 854.33 ********Entertainment********* Total: 0.0
food.deposit(900, "deposit")
entertainment.deposit(900, "deposit")
auto.deposit(900, "deposit")
food.withdraw(105.55)
entertainment.withdraw(33.40)
auto.withdraw(10.99)
print(create_spend_chart([auto, food, entertainment]))
Percentage spent by category
100| o
90| o
80| o
70| o
60| o
50| o
40| o
30| o o
20| o o o
10| o o o
0| o o o
----------
A F E
u o n
t o t
o d e
r
t
a
i
n
m
e
n
t