Untitled

 avatar
unknown
python
2 years ago
2.3 kB
3
Indexable
#budget.py
class Category:
  
  def __init__(self, category):
    self.category = category
    self.ledger = []
    self.balance = 0.0

  def deposit(self, amount, description = ""):
    obj = {"amount": amount, "description": description}
    self.ledger.append(obj)
    self.balance+=amount

  def withdraw(self, amount, description = ""):
    negativeAmount = -1 * amount
    
    if(self.check_funds(amount) == False):
      return False
    else:
      obj = {"amount": negativeAmount, "description":description}
      self.ledger.append(obj)
      self.balance-=amount
      return True
  
  def get_balance(self):
    return self.balance

  def transfer(self, amount, name):
    if (amount > self.balance):
      return False
    self.withdraw(amount, "Transfer to " + name.category)
    name.deposit(amount, "Transfer from " + self.category)
    return True
    
  def check_funds(self, amount):
    if(amount > self.get_balance()):
      return False
    return True

  def __str__(self):

    text = str(self.category)
    
    top = text.center(30, "*") # error: 'str' object cannot be interpreted as an integer

    strList = []
    strList.append(top)
    for obj in self.ledger:
      strList.append("\n")
      values = list(obj.values())
      
      amount = str(values[0])[:7]
      adjustedAmount = "{:,.2f}".format(float(amount))
      description = str(values[1][:23])
      strList.append(description + amount.rjust(30 - len(description)))

    strList.append( "\n" + "Total: " + "{:,.2f}".format(self.balance))
    return ('').join(strList)

    
    
def create_spend_chart(*categories):
  chartArray = []
  arr =[]
  arr.append(categories)
  for i in range (0, 100):
    chartArray.append('\n')
    chartArray.append(str(i).rjust(3) + "| ")
    i+=10

  chartArray = list(reversed(chartArray))
  
  return "hello"   

  
#main.py

import budget

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)

create_spend_chart(food) #error create_spend_chart does not exist
Editor is loading...