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 _find_task(task_params, library) -> int: try: tasks = library.find_task(task_params) except AppError as e: sys.stderr.write(str(e)) return ERROR_CODE print('Search:') Printer.print_tasks(tasks, 'Result:') return 0
def _show_messages(messages) -> int: if messages: reminders = [] for message in messages[:]: # type: str if message.lower().startswith('reminder'): reminders.append(message) messages.remove(message) Printer.print_reminders(reversed(reminders)) print() Printer.print_notifications(reversed(messages)) return 0 return ERROR_CODE
def _show_queue_tasks(key, library, opened, archive, failed, long, sortby): def load_tasks(task_keys): _tasks = [] for _key in task_keys: task = library.get_task(key=_key) _tasks.append(task) _tasks.sort(key=lambda x: x.__dict__[sortby], reverse=True) return _tasks if not opened and not archive and not failed: opened = True try: queue = library.get_queue(key) except AppError as e: sys.stderr.write(str(e)) return ERROR_CODE if sortby is None: sortby = ParserArgs.TASK_PRIORITY.dest.lower() print('Queue: "{}", key {}\nTasks:'.format(queue.name, queue.key)) if opened: tasks = load_tasks(queue.opened_tasks) Printer.print_tasks(tasks, TaskStatus.OPENED, long, Printer.CL_YELLOW) if archive: tasks = load_tasks(queue.solved_tasks) Printer.print_tasks(tasks, TaskStatus.SOLVED, long, Printer.CL_BLUE) if failed: tasks = load_tasks(queue.failed_tasks) Printer.print_tasks(tasks, TaskStatus.FAILED, long, Printer.CL_RED) return 0
def _show_task(key, library, long) -> int: try: task = library.get_task(key) except AppError as e: sys.stderr.write(str(e)) return ERROR_CODE print('Main task:') if long: Printer.print_task_fully(task) else: Printer.print_task_briefly(task) sub_tasks = library.task_controller.get_sub_tasks(task) if sub_tasks: Printer.print_tasks(sub_tasks, "Sub tasks:") return 0
def _show_user_tasks(library, long, sortby) -> int: try: print('User: "******".'.format(library.get_online_user().nick)) queues = library.get_user_queues() Printer.print_queues(queues) except AppError as e: sys.stderr.write(str(e)) return ERROR_CODE if sortby is None: sortby = ParserArgs.TASK_PRIORITY.dest.lower() author_tasks, responsible_tasks = library.get_user_tasks() print('Tasks:') author_tasks.sort(key=lambda x: x.__dict__[sortby], reverse=True) responsible_tasks.sort(key=lambda x: x.__dict__[sortby], reverse=True) Printer.print_tasks(author_tasks, "Author", long) Printer.print_tasks(responsible_tasks, "Responsible", long) return 0
def _find_queues(name, library: Interface) -> int: queues = library.find_queues(name) print('Search:') Printer.print_queues(queues, 'Results for "{}"'.format(name)) return 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