forked from omymble/oop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw_OOP.js
149 lines (135 loc) · 4.53 KB
/
hw_OOP.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//PARENT
function Position(item) {
this.price = item.price;
this.calorie = item.calorie;
this.type = item.type;
}
Position.prototype = {
calculatePrice: function () {
return this.price;
},
calculateCalories: function () {
return this.calorie;
},
getType: function () {
return this.type;
},
};
//CHILDS
function Drink(drink) {
Position.call(this, drink);
}
Drink.prototype = Object.create(Position.prototype);
Drink.COLA = { price: 50, calorie: 40, type: "Cola" };
Drink.COFFEE = { price: 80, calorie: 20, type: "Coffee" };
function Salad(salad, weight = 100) {
const temp = {
price: weight === 100 ? salad.price : (salad.price * weight) / 100,
calorie: weight === 100 ? salad.calorie : (salad.calorie * weight) / 100,
type: salad.type,
};
Position.call(this, temp);
this.weight = weight;
}
Salad.CAESAR = { price: 100, calorie: 20, type: "Caesar" };
Salad.OLIVIE = { price: 50, calorie: 80, type: "Olivie" };
Salad.prototype = Object.create(Position.prototype);
function HamburgerSize(size) {
Position.call(this, size);
}
HamburgerSize.prototype = Object.create(Position.prototype);
function HamburgerStuffing(stuffing) {
Position.call(this, stuffing);
}
HamburgerStuffing.prototype = Object.create(Position.prototype);
function Hamburger(size, stuffing) {
this.size = new HamburgerSize(size);
this.stuffing = new HamburgerStuffing(stuffing);
const temp = {
price: this.size.calculatePrice() + this.stuffing.calculatePrice(),
calorie: this.size.calculateCalories() + this.stuffing.calculateCalories(),
type: this.size.getType().concat(" ", this.stuffing.getType()),
};
Position.call(this, temp);
}
Hamburger.prototype = Object.create(Position.prototype);
Hamburger.prototype.getSize = function () {
return Position.prototype.getType.call(this.size);
};
Hamburger.prototype.getStuffing = function () {
return Position.prototype.getType.call(this.stuffing);
};
Hamburger.SIZE_SMALL = { price: 50, calorie: 20, type: "Small" };
Hamburger.SIZE_LARGE = { price: 100, calorie: 40, type: "Large" };
Hamburger.STUFFING_CHEESE = { price: 10, calorie: 20, type: "Cheese" };
Hamburger.STUFFING_SALAD = { price: 20, calorie: 5, type: "Salad" };
Hamburger.STUFFING_POTATO = { price: 15, calorie: 10, type: "Potato" };
//ORDER
function Order() {
this.paid = false;
this.items = [];
}
Order.prototype = {
constructor: Order,
addItem: function (item, amount = 1) {
if (!this.paid) {
while (amount > 0) {
this.items.push(item);
amount--;
}
return "Order updated";
}
return "Order is already paid";
},
removeItem: function (item) {
if (!this.paid) {
let index = this.items.indexOf(item);
if (index < 0) {
return `Item wasn't in order`;
}
this.items.splice(index, 1);
return `Item has been removed from order`;
}
return "Order is already paid. No changes allowed";
},
pay: function () {
this.paid = true;
return "Order is paid";
},
calculatePrice: function () {
return `Total price: ${this.items.reduce(
(sums, el) => sums + el.calculatePrice(),
0
)}`;
},
calculateCalories: function () {
return `Total calories: ${this.items.reduce(
(sums, el) => sums + el.calculateCalories(),
0
)}`;
},
};
//TEST
const hamburgerLargePotato = new Hamburger(
Hamburger.SIZE_LARGE,
Hamburger.STUFFING_POTATO
); //{ price:115, calorie: 50, type: "Large"/"Potato" };
const saladOlivie = new Salad(Salad.OLIVIE, 50); //{ price:25, calorie: 40, type: "Olivie" };
const drinkCola = new Drink(Drink.COLA); //{ price:50, calorie: 40, type: "Cola" };
const order = new Order();
order.addItem(hamburgerLargePotato, 2);
order.addItem(drinkCola, 0);
order.addItem(saladOlivie);
console.log(
hamburgerLargePotato.calculatePrice(),
drinkCola.calculatePrice(),
saladOlivie.calculatePrice()
); //115 50 25
console.log(order.removeItem(drinkCola)); //Item wasn't in order
order.addItem(drinkCola); //drinkCola*1
order.addItem(drinkCola); //drinkCola*2
console.log(order.removeItem(drinkCola)); //Item has been removed from order
console.log(order.calculatePrice()); //hamburgerLargePotato(115)*2+drinkCola(50)+saladOlivie(25)=305
console.log(order.pay()); //Order is paid
console.log(order.removeItem(drinkCola)); //Order is already paid. No changes allowed
console.log(order.calculateCalories()); //hamburgerLargePotato(50)*2+drinkCola(40)+saladOlivie(40)=180