Update / load inventory
Customer purchase item(s)
Accept payment (card, cash and coin)
Check low stock / functionality and call technician
from typing import Dict
class InventoryManager:
def __init__(self):
self.inventory_dict: Dict[str, 'Item'] = {}
def add_item(self, item_name: str, quantity: int, price: float) -> bool:
"""Add or update an item in the inventory."""
if item_name in self.inventory_dict:
self.inventory_dict[item_name].quantity += quantity
else:
self.inventory_dict[item_name] = Item(item_name, price, quantity)
return True
def get_items(self) -> Dict[str, 'Item']:
"""Return all items in the inventory."""
return self.inventory_dict
def get_item(self, item_name: str) -> int:
"""Get the quantity of a specific item."""
item = self.inventory_dict.get(item_name)
return item.quantity if item else 0
def update_item(self, item_name: str, quantity_delta: int) -> bool:
"""Update the quantity of an item."""
if item_name in self.inventory_dict:
new_quantity = self.inventory_dict[item_name].quantity + quantity_delta
if new_quantity < 0:
raise ValueError("Quantity cannot be negative.")
self.inventory_dict[item_name].quantity = new_quantity
return True
return False
class Payment:
def accept_payment(self, payment_method: str) -> bool:
if payment_method == "coin":
return CoinProcessor().accept_payment()
elif payment_method == "cash":
return CashProcessor().accept_payment()
elif payment_method == "card":
return CardProcessor().accept_payment()
else:
print("Unsupported payment method.")
return False
class CoinProcessor(Payment):
def accept_payment(self) -> bool:
print("Coin payment accepted.")
return True
class CashProcessor(Payment):
def accept_payment(self) -> bool:
print("Cash payment accepted.")
return True
class CardProcessor(Payment):
def accept_payment(self) -> bool:
print("Card payment accepted.")
return True
class Alerts:
def notify(self, status: str):
pass
class PaymentAlert(Alerts):
def notify(self, status: str):
if status == "payment failed":
print("Payment Alert: A payment failure has occurred.")
class RestockAlert(Alerts):
def notify(self, status: str):
if status == "item out of stock":
print("Restock Alert: Item out of stock, please refill.")
class MaintenanceAlert(Alerts):
def notify(self, status: str):
if status == "needs maintenance":
print("Maintenance Alert: The vending machine needs maintenance.")
Determine how these objects will interact with each other to fulfill the use cases...
Payment interfaced is implemented by all of cashProcessor,cardProcesoor and coinProcessor.
Observer pattern
Strategy pattern
public class Car { ... } // Example
Single relationship principle:
Flexble to add new items, new paymenthods and new alerts
Try creating a class, flow, state and/or sequence diagram using the diagramming tool. Mermaid flow diagrams can be used to represent system use cases. You can ask the interviewer bot to create a starter diagram if unfamiliar with the tool. Briefly explain your diagrams if necessary...
I donno