Skip to content

Instantly share code, notes, and snippets.

@DoRightt
Created June 12, 2020 13:37
Show Gist options
  • Save DoRightt/d5b123ca458da3522be52ee04dc62921 to your computer and use it in GitHub Desktop.
Save DoRightt/d5b123ca458da3522be52ee04dc62921 to your computer and use it in GitHub Desktop.
interface Iterator {
next(): Object;
hasNext(): boolean;
}
class DinnerMenuIterator implements Iterator {
items: MenuItem[];
position: number = 0;
constructor(items: MenuItem[]) {
this.items = items;
}
next(): MenuItem {
const menuItem = this.items[this.position];
this.position++;
return menuItem;
}
hasNext(): boolean {
if (this.position >= this.items.length || this.items[this.position] === undefined) {
return false;
} else {
return true;
}
}
}
class BreakfastMenuIterator implements Iterator {
items: Map;
position: number = 0;
constructor(items: Set<MenuItem>) {
this.items = items;
}
next(): MenuItem {
const menuItem = this.items.get(this.position);
this.position++;
return menuItem;
}
hasNext(): boolean {
if (this.position >= this.items.size || this.items.get(this.position) === undefined) {
return false;
} else {
return true;
}
}
}
class MenuItem {
name: string;
description: string;
vegeterian: boolean;
price: number;
constructor(name: string, description: string, vegeterian: boolean, price: number) {
this.name = name;
this.description = description;
this.vegeterian = vegeterian;
this.price = price;
}
getName(): string {
return this.name;
}
getPrice(): number {
return this.price;
}
getDescription(): string {
return this.description;
}
isVegeterian(): boolean {
return this.vegeterian;
}
}
class DinnerMenu {
static MAX_ITEMS = 6;
numberOfItems: number = 0;
menuItems: MenuItem[];
constructor() {
this.menuItems = [];
this.addItem("Rice", "Our classical rice with soy sauce", true, 2.45);
this.addItem("Soup", "Noodle soup", true, 2.99);
this.addItem("Porrage", "Porrage with beef", false, 5.19);
this.addItem("Potatoe", "Fried potatoe with ketchup and burger", false, 4.99);
}
addItem(name: string, description: string, vegeterian: boolean, price: number) {
const menuItem = new MenuItem(name, description, vegeterian, price);
if (this.numberOfItems >= DinnerMenu.MAX_ITEMS) {
console.log("Sorry, but menu is full. Can't add new item in menu");
} else {
this.menuItems.push(menuItem);
this.numberOfItems++;
}
}
createIterator(): Iterator {
return new DinnerMenuIterator(this.menuItems);
}
}
class PancakeHouseMenu {
menuItems: Map;
constructor() {
this.menuItems = new Map();
this.addItem("Regular pancake", "Our classical pancake with sourcream", true, 2.99);
this.addItem("Sweet pancake", "Sweet pancake with berry jam", true, 1.99);
this.addItem("Double breakfast", "Our classical pancake with fried potatoe and eggs", false, 4.99);
this.addItem("Pancake fest", "Three types of our pancakes", true, 3.99);
}
addItem(
name: string,
description: string,
vegeterian: boolean,
price: number
) {
const menuItem = new MenuItem(name, description, vegeterian, price);
this.menuItems.set(this.menuItems.size, menuItem);
}
createIterator(): Iterator {
return new BreakfastMenuIterator(this.menuItems);
}
}
class Waitress {
pancakeHouseMenu: PancakeHouseMenu;
dinnerMenu: DinnerMenu;
constructor(pancakeHouseMenu: PancakeHouseMenu, dinnerMenu: DinnerMenu) {
this.pancakeHouseMenu = pancakeHouseMenu;
this.dinnerMenu = dinnerMenu;
}
public printMenu() {
const pancakeIterator = this.pancakeHouseMenu.createIterator();
const dinnerIterator = this.dinnerMenu.createIterator();
console.log('Breakfast menu:');
this.getMenuItems(pancakeIterator);
console.log('Dinner menu:');
this.getMenuItems(dinnerIterator);
}
private getMenuItems(iterator: Iterator) {
while(iterator.hasNext()) {
const menuItem = iterator.next();
console.log(`${menuItem.name}: ${menuItem.description}, ${menuItem.price}`);
}
}
}
const pancakes = new PancakeHouseMenu();
const dinners = new DinnerMenu();
const waitress = new Waitress(pancakes, dinners);
waitress.printMenu();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment