Untitled
class Component: def __init__(self, name, brand, price): self.name = name self.brand = brand self.price = price def __repr__(self): return f"{self.name} ({self.brand}): ${self.price}" class CPU(Component): def __init__(self, name, brand, price, cores, clock_speed): super().__init__(name, brand, price) self.cores = cores self.clock_speed = clock_speed class Memory(Component): def __init__(self, name, brand, price, size, type_): super().__init__(name, brand, price) self.size = size self.type_ = type_ class HDD(Component): def __init__(self, name, brand, price, capacity, rpm): super().__init__(name, brand, price) self.capacity = capacity self.rpm = rpm class Catalogue: def __init__(self): self.components = [] def add_component(self, component): self.components.append(component) def remove_component(self, component): self.components.remove(component) def search_by_name(self, name): return [comp for comp in self.components if name.lower() in comp.name.lower()] def list_all(self): return self.components class Client: def __init__(self, name, email, address): self.name = name self.email = email self.address = address self.history = ClientHistory() self.cart = ClientCart() def __repr__(self): return f"Client: {self.name}, Email: {self.email}, Address: {self.address}" class ClientCart: def __init__(self): self.items = [] def add_to_cart(self, component, quantity=1): self.items.append((component, quantity)) def remove_from_cart(self, component): self.items = [item for item in self.items if item[0] != component] def view_cart(self): return self.items def clear_cart(self): self.items = [] def calculate_total(self): return sum(item[0].price * item[1] for item in self.items) class ClientHistory: def __init__(self): self.purchases = [] def add_purchase(self, cart): self.purchases.append(cart.items) def view_history(self): return self.purchases class WebshopService: def __init__(self, catalogue): self.catalogue = catalogue def place_order(self, client): if not client.cart.items: raise ValueError("Cart is empty") total = client.cart.calculate_total() client.history.add_purchase(client.cart) client.cart.clear_cart() return f"Order placed successfully! Total amount: ${total:.2f}" def add_to_catalogue(self, component): self.catalogue.add_component(component) # Example Usage: if __name__ == "__main__": # Create some components cpu = CPU(name="Ryzen 5 3600", brand="AMD", price=200, cores=6, clock_speed="3.6 GHz") memory = Memory(name="Corsair Vengeance", brand="Corsair", price=80, size="16GB", type_="DDR4") hdd = HDD(name="Seagate Barracuda", brand="Seagate", price=50, capacity="1TB", rpm=7200) # Create a catalogue and add components catalogue = Catalogue() catalogue.add_component(cpu) catalogue.add_component(memory) catalogue.add_component(hdd) # Create a client client = Client(name="John Doe", email="john@example.com", address="123 Elm Street") # Add components to client's cart client.cart.add_to_cart(cpu) client.cart.add_to_cart(memory, quantity=2) # Place order service = WebshopService(catalogue) print(service.place_order(client)) # Order placed successfully! Total amount: $360.00 # View client's purchase history print(client.history.view_history())
Leave a Comment