-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask12.js
120 lines (101 loc) · 4.44 KB
/
task12.js
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
const menu = [
{name: 'SIZE_SMALL' ,price: 50, calories: 20},
{name: 'SIZE_LARGE', price: 100, calories: 40 },
{name: 'STUFFING_CHEESE', price: 10, calories: 20},
{name: 'STUFFING_SALAD', price: 20, calories: 5},
{name: 'STUFFING_POTATO', price: 15, calories: 10},
{name: 'TOPPING_MAYO', price: 15, calories: 0},
{name: 'TOPPING_SPICE', price: 20, calories: 5}];
}
function Hamburger(size, stuffing) {
if(size === undefined) throw new HamburgerException("Неуказан бургер");
else if(!stuffing) throw new HamburgerException("Неуказана начинка");
if(size !== Hamburger.SIZE_SMALL && size !== Hamburger.SIZE_LARGE){
throw new HamburgerException("Некоректные параметры бургера");
}
else if(stuffing !== Hamburger.STUFFING_CHEESE && stuffing !== Hamburger.STUFFING_SALAD && stuffing !== Hamburger.STUFFING_POTATO){
throw new HamburgerException("Некоректные параметры начинки");
}
this.size = size;
this.stuffing = stuffing;
this.topping = [];
this.price = menu[size].price + menu[stuffing].price;
this.calories = menu[size].calories + menu[stuffing].calories
}
/* Размеры, виды начинок и добавок */
Hamburger.SIZE_SMALL = 0;
Hamburger.SIZE_LARGE = 1;
Hamburger.STUFFING_CHEESE = 2;
Hamburger.STUFFING_SALAD = 3;
Hamburger.STUFFING_POTATO = 4;
Hamburger.TOPPING_MAYO = 5;
Hamburger.TOPPING_SPICE = 6;
Hamburger.prototype.addTopping = function (topping){
if(topping !== Hamburger.TOPPING_MAYO && topping !== Hamburger.TOPPING_SPICE){
throw new HamburgerException("Некоректные параметры начинки")
}
if(this.topping.length === 0){
this.topping[this.topping.length] = topping;
this.price += menu[topping].price;
this.calories += menu[topping].calories;
}
else if(this.topping.length === 1 && this.topping[0] !== topping) {
this.topping[this.topping.length] = topping;
this.price += menu[topping].price;
this.calories += menu[topping].calories;
}
else throw new HamburgerException("Невозможно добавить начинку");
};
Hamburger.prototype.removeTopping = function (topping){
if(this.topping[0] === topping){
topping.splice(0,1);
this.price -= menu[topping].price;
this.calories -= menu[topping].calories;
}
else if(this.topping[1] === topping){
this.topping.length = 1;
this.price -= menu[topping].price;
this.calories -= menu[topping].calories;
}
else throw new HamburgerException("Невозможно удалить несуществующую начинку");
};
Hamburger.prototype.getToppings = function (){
let result = [];
for(let i = 0; i < this.topping.length; i++){
result[i] = menu[this.topping[i]].name
}
return result;
};
Hamburger.prototype.getSize = function (){
return menu[this.size].name;
};
Hamburger.prototype.getStuffing = function (){
return menu[this.stuffing].name;
};
Hamburger.prototype.calculatePrice = function (){
return this.price;
};
Hamburger.prototype.calculateCalories = function (){
return this.calories;
};
function HamburgerException (property) {
Error.call(this, property) ;
this.name = "PropertyError";
this.property = property;
this.message = "Ошибка в свойстве " + property;
this.stack = (new Error()).stack;
}
HamburgerException.prototype = Object.create(Error.prototype);
// маленький гамбургер с начинкой из сыра
let hamburger = new Hamburger(Hamburger.SIZE_SMALL, Hamburger.STUFFING_CHEESE);
hamburger.addTopping(Hamburger.TOPPING_MAYO); //Добавили начинку
console.log("Calories:", hamburger.calculateCalories()); // 40
console.log("Price:", hamburger.calculatePrice()); // 75
hamburger.addTopping(Hamburger.TOPPING_SPICE); //Добавили еще начинку
console.log("Calories:", hamburger.calculateCalories()); // 45
console.log("Price with sauce:", hamburger.calculatePrice()); // 95
console.log("Is hamburger large:", hamburger.getSize() === Hamburger.SIZE_LARGE); // -> false
hamburger.removeTopping(Hamburger.TOPPING_SPICE); //Удалили начинку
console.log("Calories:", hamburger.calculateCalories()); // 40
console.log("Price with sauce:", hamburger.calculatePrice()); // 75
console.log("Have toppings:", hamburger.getToppings().length); // 1