-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
131 lines (113 loc) · 2.8 KB
/
index.ts
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* The Iterator Pattern
*
* The Iterator Pattern provides a way to access the elements of an aggregate
* object sequentially without exposing its underlying representation.
*/
// Following along with the text, notice I'm naming the interface IIterator.
// 1) Stylistically, this is what I've been doing BUT
// 2) There already is an Iterator! So we need a unique name.
interface IIterator<T> {
hasNext: () => boolean;
next: () => T;
}
class MenuItem {
name: string;
description: string;
vegetarian: boolean;
price: number;
constructor(
name: string,
description: string,
vegetarian: boolean,
price: number
) {
this.name = name;
this.description = description;
this.vegetarian = vegetarian;
this.price = price;
}
getName() {
return this.name;
}
getDescription() {
return this.description;
}
getPrice() {
return this.price;
}
isVegetarian() {
return this.isVegetarian;
}
}
class DinnerMenuIterator implements IIterator<MenuItem> {
items: MenuItem[];
position = 0;
constructor(items: MenuItem[]) {
this.items = items;
}
next() {
const menuItem = this.items[this.position];
this.position = this.position + 1;
return menuItem;
}
// No need for the if/else here
hasNext() {
return !(
this.position >= this.items.length || this.items[this.position] === null
);
}
}
class DinnerMenu {
menuItems: MenuItem[];
numberOfItems = 0;
constructor() {
this.menuItems = [];
this.addItem('Tasty Burger', 'It is a tasty burger!', false, 23.99);
this.addItem('Veggie Burger', 'It is a veggie burger!', true, 33.99);
this.addItem("General Tso's Chicken", 'From the good', false, 45.0);
this.addItem(
'Beef Pot Pie',
'A flavor sensation that will send you to the moon!',
false,
3.99
);
}
addItem(
name: string,
description: string,
vegetarian: boolean,
price: number
) {
const menuItem = new MenuItem(name, description, vegetarian, price);
this.menuItems.push(menuItem);
}
createIterator(): DinnerMenuIterator {
return new DinnerMenuIterator(this.menuItems);
}
}
class Waitress {
dinnerMenu: DinnerMenu;
constructor(dinnerMenu: DinnerMenu) {
this.dinnerMenu = dinnerMenu;
}
printMenu() {
const dinnerIterator = this.dinnerMenu.createIterator();
this.printMenuIter(dinnerIterator);
}
// No method overloading here
private printMenuIter(iter: DinnerMenuIterator) {
while (iter.hasNext()) {
const menuItem = iter.next();
console.log(
`${menuItem.getName()}, ${menuItem.getPrice()} -- ${menuItem.getDescription()}`
);
}
}
}
function exampleOne() {
const dinnerMenu = new DinnerMenu();
const waitress = new Waitress(dinnerMenu);
waitress.printMenu();
}
export default {exampleOne};