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