· 7 years ago · Feb 21, 2019, 01:58 PM
1'use strict';
2
3class Ingredient {
4 name: string;
5 amount: number;
6 constructor(newName: string, newAmount: number) {
7 this.name = newName;
8 this.amount = newAmount;
9 }
10}
11
12class Dish {
13 name: string;
14 //I think this ingredient part is very poorly explained in the task so I went with
15 //my interpretation like below
16 ingredients: Ingredient[] = [];
17 constructor(newName: string, newIngredients: Ingredient[]) {
18 this.name = newName;
19 this.ingredients = newIngredients;
20 }
21 getIngredients() {
22 for (let i: number = 0; i < this.ingredients.length; i++) {
23 return this.ingredients[i].name;
24 }
25 }
26}
27
28class Order {
29 private id: number = generateRandomNumber(1001, 9999);
30 private status: string;
31 dishes: Dish[] = [];
32 constructor(newDishes: Dish[]) {
33 this.dishes = newDishes;
34 }
35 delay() {
36 this.status = "waiting";
37 }
38 deliver() {
39 this.status = "delivered";
40 }
41 reject() {
42 this.status = "rejected";
43 }
44 getDishes() {
45 for (let i: number = 0; i < this.dishes.length; i++) {
46 return this.dishes[i].name;
47 }
48 }
49}
50
51class OnlineOrder extends Order {
52 address: string;
53 constructor(newDishes: Dish[]) {
54 super(newDishes);
55 }
56}
57
58class OfflineOrder extends Order {
59 private table: number;
60 constructor(newDishes: Dish[], newTable: number) {
61 super(newDishes)
62 this.table = newTable;
63 }
64}
65
66class Restaurant {
67 private inventory: Ingredient[];
68 constructor(newInventory: Ingredient[]) {
69 this.inventory = newInventory;
70 }
71 receiveOrder(newOrder: Order) {
72 //Typescript doesn't let me access the ingredients list on the orders, even though they exist
73 if (newOrder.dishes.ingredients.length < this.inventory.length) {
74 newOrder.deliver();
75
76 }
77 //Here I wanted to check whether the new order's ingredient list is longer and if the
78 //address property exists on it, because this way I can check if it's online or not
79 else if (newOrder.dishes.ingredients.length > this.inventory.length && newOrder.address) {
80 newOrder.delay();
81 }
82 else if (newOrder.dishes.ingredients.length > this.inventory.length && !newOrder.address) {
83 newOrder.reject();
84 }
85 return newOrder;
86 }
87}
88
89//This function will create a random number between the given minimum and maximum values
90function generateRandomNumber(min_value: number, max_value: number) {
91 let newNumber: number = 0;
92 newNumber = Math.random() * (max_value - min_value) + min_value;
93 return Math.floor(newNumber);
94}
95
96Exercise 3
97In this exercise you will create an order manager application for a restaurant. We need to track the status of an order and check the availability of the ingredients. You can read the specification below
98
99Dish
100Create a Dish class, this will represent a dish what can be ordered from the restaurant. It has 2 private fields (these fields must be set through the constructor)
101
102name - the name of the dish
103ingredients - where we want to store the ingredients' name and amount
104For the sake of simplicity in our fictive world there are only two ingredients: ingredient1 and ingredient2
105and one public method
106
107getIngredients() - which returns the ingredients field
108Orders
109Create an Order class with 3 private fields
110
111id - a randomly generated number (1000 < x <= 9999) for each order it cannot be set as constructor argument
112status - a string, its default value is created, cannot be set as constructor argument
113dishes - a Dish array which is set through the constructor as argument
114and 4 methods
115
116delay() - it sets the status field to waiting
117deliver() - it sets the status field to delivered
118reject() - it sets the status field to rejected
119getDishes() - it returns the Dishes associated with the Order
120Create an OnlineOrder class which is a special Order, it has one more private field
121
122address - a string, which can be set through the constructor just like the dishes
123Finally create an OfflineOrder which is a special Order, it has one more private field
124
125table - a number, it is set from the constructor just like the dishes
126Restaurant
127Create a Restaurant class. This class will process the orders. It has 1 private field which must be set through the constructor.
128
129inventory - it will hold the available ingredients (it will have only two keys, ingredient1 and ingredient2)
130and 1 public method
131
132receiveOrder(order) - it takes an Order as argument.
133if the dishes in the order can be made (there is enough ingredients in the inventory) then it must reduce the resources in the inventory and call the deliver method on the order
134if there is not enough ingredients and the parameter is an online order then it must call the delay method of the order
135otherwise it must reject the order
136finally it must return the order