def test_create(actor_factory: DefaultActorFactory) -> None: cashier = actor_factory.create_cashier() assert cashier is not None assert isinstance(cashier, Cashier) manager = actor_factory.create_manager() assert manager is not None assert isinstance(manager, StoreManager)
def __init__(self) -> None: self.factory = DefaultActorFactory() manager = self.factory.create_manager() cashiers = [self.factory.create_cashier()] database = self.__init_database() self._store = Store(manager=manager, cashiers=cashiers, database=database) self._printer = Printer()
def test_make_receipt(actor_factory: DefaultActorFactory) -> None: cashier = actor_factory.create_cashier() cashier.open_receipt(receipt_builder=ReceiptBuilder()) items: List[IItem] = [ Item("Milk"), Item("Lemonade"), OrdinaryPack(7, "Beer") ] for item in items: cashier.add_item_to_receipt(item) receipt = cashier.give_receipt() for item in receipt: items.remove(item) assert len(items) == 0
def test_make_x_report(actor_factory: DefaultActorFactory) -> None: manager = actor_factory.create_manager() sold_items = { Item("Milk"): 4, Item("Lemonade"): 3, OrdinaryPack(2, "Beer"): 2, } x_report = manager.make_X_report(sold_items.copy(), 15.0) for item in x_report: assert item[0] in sold_items assert item[1] == sold_items[item[0]] sold_items.pop(item[0]) assert len(sold_items) == 0 assert x_report.calculate_price() == 15.0
def test_receive_receipt(actor_factory: DefaultActorFactory) -> None: cashier = actor_factory.create_cashier() cashier.open_receipt(receipt_builder=ReceiptBuilder()) items: List[IItem] = [ Item("Milk"), Item("Lemonade"), OrdinaryPack(7, "Beer") ] customer = Customer(items.copy(), cashier, [""]) for item in customer.show_items(): cashier.add_item_to_receipt(item) customer.receive_receipt() receipt = customer.show_receipt() for item in receipt: items.remove(item) assert len(items) == 0
class StoreSimulator: factory: IActorFactory _store: IStore _printer: IPrinter def __init__(self) -> None: self.factory = DefaultActorFactory() manager = self.factory.create_manager() cashiers = [self.factory.create_cashier()] database = self.__init_database() self._store = Store(manager=manager, cashiers=cashiers, database=database) self._printer = Printer() def simulate_shift(self) -> None: customers_served = 0 while True: self.__simulate_serve_customer() customers_served += 1 shift_ended = self.__check_report(customers_served) if shift_ended: break def __simulate_serve_customer(self) -> None: cashier = self._store.get_available_cashier() customer = self.factory.create_customer( cashier=cashier, store_items=self._store.get_items()) cashier.open_receipt(receipt_builder=ReceiptBuilder()) customer_items = customer.show_items() for item in customer_items: cashier.add_item_to_receipt(item) receipt = cashier.give_receipt() self._printer.print_receipt( receipt=receipt, catalogue=self._store.get_catalogue(), discounts=self._store.get_discounts(), ) customer.pay_for_items() cashier.close_receipt() self._store.add_sold_items(receipt) def __check_report(self, customers_served: int) -> bool: if customers_served % PER_ZREPORT_CUSTOMER_COUNT == 0: if self._store.get_manager().answer_y_n_question(): self._store.close_shift() return True elif customers_served % PER_XREPORT_CUSTOMER_COUNT == 0: if self._store.get_manager().answer_y_n_question(): x_report = self._store.get_manager().make_X_report( self._store.get_sold_items(), self._store.get_revenue()) self._printer.print_X_report(x_report) return False def __init_database(self) -> IStoreDatabase: database = StoreDatabase() items = { Item("Milk"): 4.99, Item("Mineral Water"): 3.00, Item("Bread"): 0.80, Item("Diapers"): 1.39, OrdinaryPack(6, "Beer"): 1.00, Item("Cheese"): 4.00, OrdinaryPack(10, "Tissues"): 1.50, } discounts = { (OrdinaryPack(10, "Tissues"), ): 0.1, (Item("Bread"), Item("Cheese")): 0.05, (Item("Mineral Water"), ): 0.5, } for item in items.items(): database.add_item(item) for discount in discounts.items(): database.add_discount(discount) return database
def test_make_x_report_empty(actor_factory: DefaultActorFactory) -> None: manager = actor_factory.create_manager() x_report = manager.make_X_report({}, 0.0) assert x_report.calculate_price() == 0.0
def test_show_items(actor_factory: DefaultActorFactory) -> None: cashier = actor_factory.create_cashier() items = [Item("Milk"), Item("Lemonade"), OrdinaryPack(7, "Beer")] customer = actor_factory.create_customer(cashier, items) assert set(customer.show_items()).issubset(items)
def test_create_customer(actor_factory: DefaultActorFactory) -> None: cashier = actor_factory.create_cashier() items = [Item("Milk"), Item("Lemonade"), OrdinaryPack(5, "Beer")] customer = actor_factory.create_customer(cashier, items) assert customer is not None assert isinstance(cashier, Cashier)
def actor_factory() -> IActorFactory: return DefaultActorFactory()