Please add this to my code, will upvote quickly!! My code so far is:
print("Welcome to the Calculon 5000")
path = input("What is the path to the bill?")
class Calculon5000:
def __init__(self, mypath):
try:
with open(mypath, 'r') as file:
self.raw_data = file.read().splitlines()
except FileNotFoundError:
print(f"Was unable to find {mypath}")
exit()
def calculate_bill(self):
total_cost = 0
for item, price in self.prices_dict.items():
quantity = self.quantities_dict.get(item, 0)
total_cost += price * quantity
return total_cost
path_to_data = "data.txt"
obj = Calculon5000(path_to_data)
class Calculon5000:
def __init__(self, raw_data):
self.prices_dict = {}
self.quantities_dict = {}
for item in raw_data:
name = item['name']
price = float(item['price'])
quantity = int(item['quantity'])
self.prices_dict[name] = price
self.quantities_dict[name] = quantity
def calculate_bill(self):
total_cost = 0
for item, price in self.prices_dict.items():
quantity = self.quantities_dict.get(item, 0)
total_cost += price * quantity
return total_cost
the_bill = Calculon5000(obj.raw_data)
print(f"The total cost is {the_bill.calculate_bill()}")
# Extend the class Calculon5000 with a child class Calculon5001
# with the following changes:
class Calculon5001(Calculon5000):
def __init__(self, raw_data):
super().__init__(raw_data)
self.discount_dict = {}
def add_discount(self, product, amount):
self.discount_dict[product] = amount
def calculate_bill(self):
total_cost = 0
for item, price in self.prices_dict.items():
quantity = self.quantities_dict.get(item, 0)
if item in self.discount_dict:
discount = self.discount_dict[item]
price = price - (price * discount)
total_cost += price * quantity
return total_cost
new_bill = Calculon5001(path)
new_bill.add_discount('grapes', 0.1)
new_bill.add_discount('coke', 0.75)
print(f"The total cost of the discounted bill is {new_bill.calculate_bill()}")