-
Notifications
You must be signed in to change notification settings - Fork 0
/
menus.py
106 lines (74 loc) · 2.61 KB
/
menus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Abstract Menu class and 2 implementations."""
from abc import ABC, abstractmethod
from .iterators import MenuIterator
class Menu(ABC):
"""Abstract Menu class."""
_description: str
@abstractmethod
def add_item(self, item) -> None:
"""Add item to menu (abstract method).
All child classes must implement this method.
"""
...
@abstractmethod
def set_items(self, items) -> None:
"""Set all menu items (abstract method).
All child classes must implement this method.
"""
...
@abstractmethod
def create_iterator(self) -> MenuIterator:
"""Create iterator for menu items (abstract method).
All child classes must implement this method.
"""
...
@property
def description(self) -> str:
"""Return menu description."""
return self._description
@abstractmethod
def __repr__(self) -> str:
"""Return menu representation.
All child classes must implement this method.
"""
...
class DinerMenu(Menu):
"""Diner menu class.
Uses tuples to store menu items.
"""
_description: str = 'Diner menu'
def __init__(self) -> None:
"""Initialize menu."""
self._menu_items: list[tuple[str, float]] = []
def add_item(self, item: tuple[str, float]) -> None:
"""Add item to menu."""
self._menu_items.append(item)
def set_items(self, items: list[tuple[str, float]]) -> None:
"""Set all menu items."""
self._menu_items = items
def create_iterator(self) -> MenuIterator:
"""Create iterator for menu items."""
return MenuIterator(self._menu_items)
def __repr__(self) -> str:
"""Return menu representation."""
return f'{self._description} with {len(self._menu_items)} items'
class PancackeMenu(Menu):
"""Pancake menu class.
Uses dict to store menu items.
"""
_description = 'Pancake menu'
def __init__(self) -> None:
"""Initialize menu."""
self._menu_items: dict[str, float] = {}
def add_item(self, item: dict[str, float]) -> None:
"""Add item to menu."""
self._menu_items.update(item)
def set_items(self, items: dict[str, float]) -> None:
"""Set all menu items."""
self._menu_items = items
def create_iterator(self) -> MenuIterator:
"""Create iterator for menu items."""
return MenuIterator(list(self._menu_items.items()))
def __repr__(self) -> str:
"""Return menu representation."""
return f'{self._description} with {len(self._menu_items)} items'