Skip to content

Instantly share code, notes, and snippets.

@beaucarnes
Created May 1, 2020 01:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beaucarnes/25979a12750b98c570606fb30f636933 to your computer and use it in GitHub Desktop.
Save beaucarnes/25979a12750b98c570606fb30f636933 to your computer and use it in GitHub Desktop.
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def withdraw(self, amount, description = ""):
self.ledger.append({"amount": -amount, "description": description})
def deposit(self, amount, description = ""):
self.ledger.append({"amount": amount, "description": description})
def get_balance(self):
total = 0
for item in self.ledger:
total += item['amount']
return total
def transfer(self, amount, budget_category):
self.withdraw(amount, f"Transfer to {budget_category.name}")
budget_category.deposit(amount, f"Transfer from {self.name}")
def __str__(self):
output = self.name.center(30, "*") + "\n"
for item in self.ledger:
output += f"{item['description'][:23].ljust(23)}{format(item['amount'], '.2f').rjust(7)}\n"
output += f"Total: {format(self.get_balance(), '.2f')}"
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment