My code so far :
import math
class Category:
def __init__(self,category):
self.category=category
self.ledger=[]
def get_balance(self):
am=0
for i in self.ledger :
am+=i['amount']
return am
def check_funds(self,amt):
if amt>self.get_balance() :
return False
return True
def __str__(self) :
length=len(self.category)
for i in range(15-length//2) :
print('*', end='')
print(self.category,end='')
if length%2==0 :
print('*',end='')
for i in range(14-length//2) :
print('*',end='')
print(" ")
for l in self.ledger :
des=' '
if l['description']=='deposit' :
des='initial deposit'
else :
des=l['description']
amt=0
a=0
if l['amount']<0:
amt=math.ceil(math.log(-l['amount'],10))+1
a=-l['amount']
else :
amt=math.ceil(math.log(l['amount'],10))
a=l['amount']
if 10**math.ceil(math.log(a,10))-a==0 :
amt+=1
if len(des)>26-amt :
des=des[:26-amt]+""
print(des,end='')
for i in range(27-len(des)-amt) :
print(' ',end='')
print('{:.2f}'.format(l['amount']))
print("Total:",'{:.2f}'.format(self.get_balance()),'\n\n')
return ""
def deposit(self,amt,description=''):
self.ledger.append({'amount': amt,'description':description})
def withdraw(self,amt,description=''):
if self.check_funds(amt):
self.ledger.append({'amount':-amt,'description':description})
return True
return False
def transfer(self,amt,bud):
if self.check_funds(amt) :
self.withdraw(amt,f'Transfer to {bud.category}')
bud.deposit(amt,f'Transfer from {self.category}')
return True
return False
def expendeture(obj) :
lst=obj.ledger
amt=0
for l in lst :
if l['amount']<0 :
amt-=l['amount']
return amt
def create_spend_chart(categories):
cat=[]
l=0
for category in categories:
cat.append(expendeture(category))
if l<len(category.category) :
l=len(category.category)
cat = [round(100*i/sum(cat)) for i in cat]
print('Percentage spent by category')
for i in range(100,-1,-10) :
if i!=100 :
print(' ',end='')
if i==0 :
print(' ',end='')
print(i,'| ',sep='',end='')
for index,category in enumerate(categories) :
if cat[index]>=i :
print('o ',end='')
else :
print(' ',end='')
print(' ')
print(' -',end='')
for i in range(len(cat)) :
print('---',end='')
print('')
for i in range(l) :
print(' ',end='')
for j in range(len(categories)) :
if len(categories[j].category)>i :
print(categories[j].category[i],end=' ')
else :
print(' ',end='')
print('')
food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
auto = Category('Auto')
auto.deposit(400, 'deposit')
auto.withdraw(60, 'groceries')
auto.withdraw(80, 'restaurant and more food for dessert')
clothing.deposit(5000, 'deposit')
clothing.withdraw(800, 'tshirt')
category=[food,auto,clothing]
print(food,auto,clothing)
create_spend_chart(category)