def __init__(self, user: User): self.user = user self.display = Display(input_handler=ConsoleInput(), output_handler=ConsoleOutput()) self.strings = Strings self.utils = Utils() self.burger_manager = BurgerManager() self.customisation_manager = CustomisationManager() self.main_menu()
class TestBurgersManager(unittest.TestCase): def setUp(self): self.burgers_manager = BurgerManager() def tearDown(self): self.burgers_manager._instance = None def test_register(self): # A burger should only register once burger = Kids(1.2, 3, 'Cheese Burger', []) identical_burger = Kids(1.2, 3, 'Cheese Burger', []) self.burgers_manager.register_burger(burger) self.assertEqual(self.burgers_manager.all_burgers(), [burger]) self.burgers_manager.register_burger(identical_burger) self.assertEqual(self.burgers_manager.all_burgers(), [burger]) def test_get_type(self): burgers_manager_type = self.burgers_manager.get_type(BurgerTypes.KIDS) self.assertEqual(burgers_manager_type, Kids) def test_singleton(self): first_manager = BurgerManager() second_manager = BurgerManager() self.assertEqual(first_manager, second_manager)
def test_get(self): # type = 1 is ignored because we mock below burger_data = { "name": "Cheese Burger", "cost": 1.2, "id_number": 1, "type": 1 } burger_manager = BurgerManager() burger_manager.register_burger = MagicMock(return_value=None) burger_manager.get_type = MagicMock(side_effect=[Kids, Adults]) burger_factory = BurgerFactory(burger_manager) kids_burger = burger_factory.get(burger_data) self.assertEqual(kids_burger, Kids(**burger_data)) adults_burger = burger_factory.get(burger_data) self.assertEqual(adults_burger, Adults(**burger_data))
class Utils: burger_manager: BurgerManager customisation_manager: CustomisationManager display: Display def __init__(self): self.display = Display() self.customisation_manager = CustomisationManager() self.burger_manager = BurgerManager() @property def all_customisations(self) -> list[Customisation]: return self.customisation_manager.all_customisations() @property def all_burgers(self) -> list[Burger]: return self.burger_manager.all_burgers() def output_indexed_list(self, items): for index, item in enumerate(items): self.display.output.print(f"{index}. {item.name} -- £{item.cost}") def allowed_values_from_list(self, items) -> list[int]: return list(range(0, len(items))) def get_at_index( self, index: int, burgers: list[Burger] = None, customisations: list[Customisation] = None ) -> Union[Burger, Customisation]: if burgers: return burgers[index] elif customisations: return customisations[index] def get_index(self, items: list[Any], text: str) -> int: return self.display.input.validate( self.allowed_values_from_list(items), self.display.input.get_integer, text)
def setUp(self): self.burgers_manager = BurgerManager()
def test_singleton(self): first_manager = BurgerManager() second_manager = BurgerManager() self.assertEqual(first_manager, second_manager)
class Menu: def __init__(self, user: User): self.user = user self.display = Display(input_handler=ConsoleInput(), output_handler=ConsoleOutput()) self.strings = Strings self.utils = Utils() self.burger_manager = BurgerManager() self.customisation_manager = CustomisationManager() self.main_menu() def main_menu(self): running = True while running: self.display.output.print(self.strings.commands) command = self.display.input.get_string('Enter a command: ') self.display.output.new_line() if command == 'a': self.list_burgers() self.add_burger_to_basket() elif command == 'u': self.display_basket() self.update_burger_in_basket() elif command == 'r': self.remove_burger_from_basket() elif command == 'b': self.display_basket() elif command == 'p': self.user.pay() running = False elif command == 'q': running = False self.display.output.print("Quitting...") def list_burgers(self): self.display.output.print(self.strings.current_burgers) self.utils.output_indexed_list(self.burger_manager.all_burgers()) def add_burger_to_basket(self): burger = self.burger_by_input() burger = self.add_customisation_to_burger(burger) burger = self.add_size_to_burger(burger) self.user.basket.add_burger(burger) def burger_by_input(self, burgers: list[Burger] = None, text: str = None): if burgers is None: burgers = self.utils.all_burgers if text is None: text = self.strings.add_burger_to_basket() burger_index = self.utils.get_index(burgers, text) burger = self.utils.get_at_index(burger_index, burgers=burgers) return burger def add_size_to_burger(self, burger: Burger) -> Burger: self.list_sizes(burger) sizes = burger.available_sizes size_index = self.utils.get_index( sizes, self.strings.add_size_to_burger(burger, sizes)) burger.size = sizes[size_index] return burger def list_sizes(self, burger: Burger): for index, size in enumerate(burger.available_sizes): self.display.output.print( self.strings.list_size(index, size, burger)) def list_customisations(self): self.display.output.print(self.strings.current_customisations) all_customisations = self.customisation_manager.all_customisations() self.utils.output_indexed_list(all_customisations) def add_customisation_to_burger(self, burger: Burger) -> Burger: def input_customisation() -> Union[Literal['y'], Literal['n']]: return self.display.input.validate(['y', 'n'], self.display.input.get_string, self.strings.add_customisations) add_customisation = input_customisation() while add_customisation == 'y': self.utils.output_indexed_list(self.utils.all_customisations) customisation_index = self.utils.get_index( self.utils.all_customisations, self.strings.add_customisation_to_burger(burger), ) customisation = self.utils.get_at_index( customisation_index, customisations=self.utils.all_customisations) burger.add_customisation(customisation) add_customisation = input_customisation() return burger def display_basket(self): basket = self.user.basket self.display.output.print(self.strings.items_in_basket_cost(basket)) for index, item in enumerate(basket.items): self.display.output.print(self.strings.basket_item(item, index)) for customisation in item.customisations: self.display.output.print( self.strings.basket_customisation(customisation)) def remove_burger_from_basket(self): self.display_basket() burger_index = int( self.display.input.get_string( self.strings.remove_burger_from_basket(self.user))) self.user.basket.remove_burger(self.user.basket.items[burger_index].id) def update_burger_in_basket(self): def yes_no_choice(text: str) -> Union[Literal['y'], Literal['n']]: choice = self.display.input.validate(['y', 'n'], self.display.input.get_string, text) return choice == 'y' def change_size(): return yes_no_choice(self.strings.change_size) def change_customisation(): return yes_no_choice(self.strings.change_customisation) def remove_customisation(): return yes_no_choice(self.strings.remove_customisation) def update_burger(): self.user.basket.update_burger(burger.id, burger) burger = self.burger_by_input( self.user.basket.items, self.strings.update_burger_in_basket(self.user)) if change_size(): burger = self.add_size_to_burger(burger) if not change_customisation(): update_burger() return while remove_customisation(): for index, customisation in enumerate(burger.customisations): self.display.output.print( f"{index}. {customisation.name}: {customisation.cost}") customisation_text = self.strings.customisation_to_remove(burger) customisation_index = self.utils.get_index(burger.customisations, customisation_text) burger.remove_customisation( burger.customisations[customisation_index]) burger = self.add_customisation_to_burger(burger) update_burger()
def __init__(self): self.display = Display() self.customisation_manager = CustomisationManager() self.burger_manager = BurgerManager()
def add_burger_to_basket(): burger_manager = BurgerManager() return f"Which burger would you like to add to your basket? 0-{len(burger_manager.all_burgers()) - 1} "