Based on my current code, and code and values I have written under (Test), I was able to show that for the respective net_balance of sender and receiver, the correct adjustment were made, yet I still have failed to meet condition 9 and 10, and am confused as to what mistake i have made. Would appreciate any guidance, thanks.
Your code so far
from os import truncate
# Remember Category is an object type, where multiple variables have the given class type
# Each class variable has two dynamic values: ledger(List of Tuples); balance_net(Net Sum)
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance_net = 0
# Deposit Method: + Dict -> + amount
def deposit(self, amount, description = ""):
object = {
"amount": amount,
"description": description
}
self.ledger.append(object)
# Withdraw Method: + Dict -> - amount
def withdraw(self, amount, description = ""):
object = {
"amount": - amount,
"description": description
}
self.ledger.append(object)
# If append() is executed, object should be in self.ledger, i.e. list
if object in self.ledger:
return True
else:
return False
# Transfer Method: (self = Sender)
def transfer(self, amount, reciever):
# Ledger Code Chunk:
object_send = {
"amount": - amount,
"description": f"Transfer to {reciever.name}"
}
self.ledger.append(object_send)
object_recieve = {
"amount": amount,
"description": f"Transfer from {self.name}"
}
reciever.ledger.append(object_recieve)
# Balance_net Code Chunk:
self.balance_net -= amount
reciever.balance_net += amount
# Transfer Sucessful Code Chunk:
# Determine if the respective transaction tuples are found in the respect ledger
if object_send in self.ledger and object_recieve in reciever.ledger:
return True
else:
return False
# get_balance Method:
def get_balance(self, category):
# For every (Amount-Description) Tuple in list, amount is extracted to form a balance_net
for transcation in self.ledger:
self.balance_net += transcation["amount"]
return self.balance_net
# check_funds Method:
def check_funds(self, amount):
if amount > self.balance_net:
return False
else:
True
# create_spend_chart Method:
def create_spend_chart(categories):
pass
####################### (Test)
# Cash Category:
cash = Category("Asset")
cash.deposit(100000, "Inherentence")
print(cash.ledger)
print(cash.get_balance("Asset"))
# Food Category:
food = Category('food')
food.deposit(900, 'deposit')
print(food.ledger)
food.withdraw(45.67, 'milk, cereal, eggs, bacon, bread')
print(food.ledger)
print(food.get_balance(food))
# Transfer Method:
cash.transfer(10000, food)
print(cash.balance_net)
print(cash.ledger)
print(food.balance_net)
print(food.ledger)
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
This is what I have done so far, I have been able to solve the previous conditions. What I have left now is condition 15, before starting the last part.
I would appreciate any help.
from typing import Text
from os import truncate
# Remember Category is an object type, where multiple variables have the given class type
# Each class variable has two dynamic values: ledger(List of Tuples); balance_net(Net Sum)
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance_net = 0
# Deposit Method: + Dict -> + amount
def deposit(self, amount, description = ""):
object = {
"amount": amount,
"description": description
}
# Ledger Code Chunk:
self.ledger.append(object)
# Balance_net Code Chunk:
self.balance_net += object['amount']
# Withdraw Method: + Dict -> - amount
def withdraw(self, amount, description = ""):
object = {
"amount": - amount,
"description": description
}
# Condition for code execution: (i.e whether sufficient balance_net)
if self.check_funds(amount) == False:
return False
elif self.check_funds(amount) == True:
self.ledger.append(object)
self.balance_net += object['amount']
return True
# Transfer Method: (self = Sender)
def transfer(self, amount, reciever):
# Ledger Code Chunk:
object_send = {
"amount": - amount,
"description": f"Transfer to {reciever.name}"
}
self.ledger.append(object_send)
object_recieve = {
"amount": amount,
"description": f"Transfer from {self.name}"
}
reciever.ledger.append(object_recieve)
# Balance_net Code Chunk:
self.balance_net -= amount
reciever.balance_net += amount
# Transfer Sucessful Code Chunk:
# Determine if the respective transaction tuples are found in the respect ledger
if object_send in self.ledger and object_recieve in reciever.ledger:
return True
# (Problem 15)
elif object_send in self.ledger == False and object_recieve not in reciever.ledger == False:
return False
# get_balance Method:
def get_balance(self):
return self.balance_net
# check_funds Method:
def check_funds(self, amount):
if amount > self.balance_net:
return False
elif amount <= self.balance_net:
return True
# (Print) __str__ Method:
def __str__(self):
# f-str allows for variable setting of the middle title word
# :*^30 -> : (syntex) * (what is used to fill) ->
# ^ (middle; fill rest) 30 totoal length, including middle title word
display_line = f"{self.name:*^30}\n"
for transaction in self.ledger:
# text is the description of any given dictionary,
# up to the 23rd character
text = f"{transaction['description'][:23]}"
# :7:.2f -> skip first parameter, i.e. earlist possible start,
# second parameter 7, i.e. until 7, 'third parameter' .2f, i.e. float 2 decimal place
number = f"{transaction['amount']:7.2f}"
# :< Left align ; :> Right align
display_line +=f"{text:<23}{number:>7}\n"
display_line += f'Total: {self.get_balance()}'
return display_line
# create_spend_chart Method:
def create_spend_chart(categories):
pass
####################### (Test)
# Food Category:
food = Category('food')
food.deposit(900, 'deposit')
print(food.ledger)
print(food.balance_net)
food.withdraw(45.67, 'milk, cereal, eggs, bacon, bread')
print(food.ledger)
print(food.balance_net)
print(food)
for test 15, is your method checking if there are enough funds for the transfer before doing it? and if there are not enough funds, is it returning False?
As of now, I’m stuck on the final part of the code, which is the visual display. Honestly, quite lost, would once again appreciate any support.
from typing import Text
from os import truncate
# Remember Category is an object type, where multiple variables have the given class type
# Each class variable has two dynamic values: ledger(List of Tuples); balance_net(Net Sum)
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance_net = 0
# Deposit Method: + Dict -> + amount
def deposit(self, amount, description = ""):
object = {
"amount": amount,
"description": description
}
# Ledger Code Chunk:
self.ledger.append(object)
# Balance_net Code Chunk:
self.balance_net += object['amount']
# Withdraw Method: + Dict -> - amount
def withdraw(self, amount, description = ""):
object = {
"amount": - amount,
"description": description
}
# Condition for code execution: (i.e whether sufficient balance_net)
if self.check_funds(amount) == False:
return False
elif self.check_funds(amount) == True:
self.ledger.append(object)
self.balance_net += object['amount']
return True
# Transfer Method: (self = Sender)
def transfer(self, amount, reciever):
# Like Withdrawl, transfer should depend on the
# True/False condition of check_funds
if self.check_funds(amount) == False:
return False
elif self.check_funds(amount) == True:
# Ledger Code Chunk:
object_send = {
"amount": - amount,
"description": f"Transfer to {reciever.name}"
}
self.ledger.append(object_send)
object_recieve = {
"amount": amount,
"description": f"Transfer from {self.name}"
}
reciever.ledger.append(object_recieve)
# Balance_net Code Chunk:
self.balance_net -= amount
reciever.balance_net += amount
return True
# get_balance Method:
def get_balance(self):
return self.balance_net
# check_funds Method:
def check_funds(self, amount):
if amount > self.balance_net:
return False
elif amount <= self.balance_net:
return True
# (Print) __str__ Method:
def __str__(self):
# f-str allows for variable setting of the middle title word
# :*^30 -> : (syntex) * (what is used to fill) ->
# ^ (middle; fill rest) 30 totoal length, including middle title word
display_line = f"{self.name:*^30}\n"
for transaction in self.ledger:
# text is the description of any given dictionary,
# up to the 23rd character
text = f"{transaction['description'][:23]}"
# :7:.2f -> skip first parameter, i.e. earlist possible start,
# second parameter 7, i.e. until 7, 'third parameter' .2f, i.e. float 2 decimal place
number = f"{transaction['amount']:7.2f}"
# :< Left align ; :> Right align
display_line +=f"{text:<23}{number:>7}\n"
display_line += f'Total: {self.get_balance()}'
return display_line
# create_spend_chart Method:
def create_spend_chart(categories):
balance_net_all = []
category_list = []
spent_amounts = []
percentage_spent = []
text = "Percentaghe spent by category" + "\n"
for category in categories:
category_balance_net = 0
for transaction in category.ledger:
if transaction['amount'] < 0:
category_balance_net += -transaction['amount']
spent_amounts.append(category_balance_net)
category_list.append(category.name)
balance_net_all.append(category_balance_net)
# Percentage rounding adjustment
total_spent_overall = sum(balance_net_all)
for amount in spent_amounts:
percentage = (amount / total_spent_overall) * 100
rounded_percentage = (percentage // 10) * 10 # Round down to nearest 10
percentage_spent.append(rounded_percentage)
# Build the chart rows
for percentage in range(100, -1, -10):
text += f'{percentage:>3} | '
for spent in percentage_spent:
text += "o " if spent >= percentage else " "
text += '\n'
# Horizontal line
text += " -" + "---" * len(category_list) + '\n'
# Add category names vertically
max_length = max(len(name) for name in category_list)
for index in range(max_length):
text += ' '
for name in category_list:
if index < len(name):
text += name[index] + ' '
else:
text += " "
text += '\n'
return text.rstrip("\n") # Remove any trailing newline
####################### (Test)
cash = Category('Asset')
food = Category('Food')
cash.deposit(100, 'Deposit')
food.deposit(200, 'Deposit')
food.transfer(100,cash)
categories = [food, cash]
create_spend_chart(categories)