von 'class angebot'
habe ich mir eine weitere Version gebastelt.
'Übung macht den Meister' und ist besser lesbar.
OSWALD
Code: Alles auswählen
KIOSK VERKSUFSANGEBOT
class Angebot:
def __init__(self, product_id, name, price, quantity, **kwargs):
self.product_id = product_id
self.name = name
self.price = price
self.quantity = quantity
## Add any additional attributes provided
for key, value in kwargs.items():
setattr(self, key, value)
def total_value(self):
return self.price * self.quantity
def display_info(self):
result = [f"Angebot: {self.name} (ID: {self.product_id})",
f"Price: €{self.price:.2f}",
f"Quantity: {self.quantity}",
f"Total Value: €{self.total_value():.2f}"]
## Display additional attributes
standard_attrs = {'product_id', 'name', 'price', 'quantity'}
for attr in dir(self):
if not attr.startswith('__') and not callable(getattr(self, attr)) and attr not in standard_attrs:
value = getattr(self, attr)
result.append(f"{attr.replace('_', ' ').title()}: {value}")
return '\n'.join(result)
class Verkauf:
def __init__(self):
self.products = {}
def add_product(self, product):
self.products[product.product_id] = product
print(f"Added: {product.name}")
def update_attribute(self, product_id, attribute, value):
if product_id in self.products:
product = self.products[product_id]
old_value = getattr(product, attribute, None)
setattr(product, attribute, value)
print(f"Updated {attribute} of {product.name} from {old_value} to {value}")
return True
print(f"Product with ID {product_id} not found.")
return False
def display_verkauf(self):
if not self.products:
print("Verkauf")
return
print("\n===== Current Inventory =====")
total_value = 0
for product in self.products.values():
print(f"\n{product.display_info()}")
total_value += product.total_value()
print(f"\nTotal Verkauf Value: €{total_value:.2f}")
print("============================")
print("#IMPLENTIEREN #################################")
verkauf = Verkauf()
## Create products with additional attributes
kaffee = Angebot(1, "Kaffee", 3.50, 5)
wiener = Angebot(2, "Wiener", 4, 10)
eiskaffee =Angebot(3, "Eiskaffee ",6 ,25)
bratwurst = Angebot(4, "Bratwurst", 9, 5)
kuchen = Angebot(5,"Kuchen",3.50,7)
## Verkaufte Produkte
verkauf.add_product(kaffee)
verkauf.add_product(wiener)
verkauf.add_product(eiskaffee)
verkauf.add_product(bratwurst)
verkauf.add_product(kuchen)
## Display the inventory
verkauf.display_verkauf()
## Update a standard attribute
verkauf.update_attribute(5, "price", 5)
verkauf.update_attribute(4, "price" ,3)
## Update a custom attribute
## Add a new attribute to an existing product
verkauf.update_attribute(2, "price", 13)
## Display the updated inventory
verkauf.display_verkauf()