class Category:
def __init__(self,category):
self.category = category
self.ledger = []
self.balance = 0
def deposit(self, amount, description=''):
self.ledger.append({'amount': amount, 'description': description})
self.balance += amount
def withdraw(self, amount, description=''):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': description})
self.balance -= amount
return True
return False
def get_balance(self):
return self.balance
def transfer(self, amount, categ):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {categ.category}")
categ.deposit(amount, f"Transfer from {self.category}")
return True
else:
return False
def check_funds(self, amount): #check what is passed as amount against balance.
if amount > self.balance:
return False
else:
return True
def __str__(self):
display =''
title = self.category.center(30, '*')
display += title + '\n'
for item in self.ledger:
#description = item["description"]
description = item["description"][:23].ljust(23)#descript. 23 chars only
amount = "{:.2f}".format(item["amount"]).rjust(7)#amount 2 decimal places
display += description + amount + '\n'
display += f"Total: {self.balance:.2f}"#Display total balance 2 decimal places
return display
def create_spend_chart(categories):
# Calculate total spent
total_spent = sum(-category.ledger[i]['amount'] for category in categories for i in range(len(category.ledger)) if category.ledger[i]['amount'] < 0)
# Calculate percentage spent for each category
percentages = [(sum(-item['amount'] for item in category.ledger if item['amount'] < 0) / total_spent) * 100 for category in categories]
percentages = [int(p // 10) * 10 for p in percentages] # Round down to nearest 10
# Create chart
chart = "Percentage spent by category\n"
for i in range(100, -1, -10):
chart += str(i).rjust(3) + "| "
for p in percentages:
if p >= i:
chart += "o "
else:
chart += " "
chart += "\n"
# Print the horizontal line/dashes at the bottom of the chart
chart += " " + "-" * (len(categories) * 3 + 1) + "\n"
# Find maximum category name length
max_len = max(len(category.category) for category in categories)
# Add category names vertically
for i in range(max_len):
chart += " "
for j, category in enumerate(categories):
if i < len(category.category):
chart += category.category[i] + " " # Changed to consistent spacing
else:
chart += " "
if j == len(categories) - 1: # Check if it's the last category
chart += " " # Add two extra spaces
if i != max_len - 1:
chart += " \n"
chart = chart.rstrip('\n')
return chart
###############################################
food = Category("Food")
clothing = Category("Clothing")
auto = Category("Auto")
food.deposit(1000, "initial deposit")
clothing.deposit(1000, "initial deposit")
auto.deposit(1000, "initial deposit")
food.withdraw(500, "groceries")
clothing.withdraw(300, "shirt")
auto.withdraw(200, "gas")
print(create_spend_chart([food, clothing, auto]))
#food.transfer(500, clothing)
#print(food.get_balance()) # Should print 500
#print(clothing.get_balance()) # Should print 500
#print(food)
`class Category:
def \__init_\_(self,category):
self.category = category
self.ledger = []
self.balance = 0
def deposit(self, amount, description=‘’):
self.ledger.append({‘amount’: amount, ‘description’: description})
self.balance += amount
def withdraw(self, amount, description=‘’):
if self.check_funds(amount):
self.ledger.append({‘amount’: -amount, ‘description’: description})
self.balance -= amount
return True
return False
def get_balance(self):
return self.balance
def transfer(self, amount, categ):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {categ.category}")
categ.deposit(amount, f"Transfer from {self.category}")
return True
else:
return False
def check_funds(self, amount): #check what is passed as amount against balance.
if amount > self.balance:
return False
else:
return True
def _str_(self):
display =''
title = self.category.center(30, '\*')
display += title + '\\n'
for item in self.ledger:
#description = item[“description”]
description = item\["description"\]\[:23\].ljust(23)#descript. 23 chars only
amount = "{:.2f}".format(item\["amount"\]).rjust(7)#amount 2 decimal places
display += description + amount + '\\n'
display += f"Total: {self.balance:.2f}"#Display total balance 2 decimal places
return display
def create_spend_chart(categories):
# Calculate total spent
total_spent = sum(-category.ledger\[i\]\['amount'\] for category in categories for i in range(len(category.ledger)) if category.ledger\[i\]\['amount'\] < 0)
# Calculate percentage spent for each category
percentages = \[(sum(-item\['amount'\] for item in category.ledger if item\['amount'\] < 0) / total_spent) \* 100 for category in categories\]
percentages = \[int(p // 10) \* 10 for p in percentages\] # Round down to nearest 10
# Create chart
chart = "Percentage spent by category\\n"
for i in range(100, -1, -10):
chart += str(i).rjust(3) + "| "
for p in percentages:
if p >= i:
chart += "o "
else:
chart += " "
chart += "\\n"
# Print the horizontal line/dashes at the bottom of the chart
chart += " " + "-" \* (len(categories) \* 3 + 1) + "\\n"
# Find maximum category name length
max_len = max(len(category.category) for category in categories)
# Add category names vertically
for i in range(max_len):
chart += " "
for j, category in enumerate(categories):
if i < len(category.category):
chart += category.category\[i\] + " " # Changed to consistent spacing
else:
chart += " "
if j == len(categories) - 1: # Check if it’s the last category
chart += " " # Add two extra spaces
if i != max_len - 1:
chart += " \\n"
chart = chart.rstrip('\\n')
return chart
###############################################
food = Category(“Food”)
clothing = Category(“Clothing”)
auto = Category(“Auto”)
food.deposit(1000, “initial deposit”)
clothing.deposit(1000, “initial deposit”)
auto.deposit(1000, “initial deposit”)
food.withdraw(500, “groceries”)
clothing.withdraw(300, “shirt”)
auto.withdraw(200, “gas”)
print(create_spend_chart([food, clothing, auto]))
#food.transfer(500, clothing)
#print(food.get_balance()) # Should print 500
#print(clothing.get_balance()) # Should print 500
#print(food)
`