-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.js
77 lines (63 loc) · 1.72 KB
/
models.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
function Product(id, name, price, description, image, available) {
this.id = id;
this.name = name;
this.price = price;
this.description = description;
this.image = image;
this.available = available;
}
function Category(id, name) {
this.id = id;
this.name = name;
this.subCategories = [];
this.promotions = [];
}
Category.prototype.addSubCategory = function (subCategory) {
this.subCategories.push(subCategory);
}
Category.prototype.addPromotion = function (promotion) {
this.promotions.push(promotion);
}
Category.prototype.hasPromotions = function () {
return this.availablePromotions().length > 0;
}
Category.prototype.availablePromotions = function () {
return this.promotions.filter(function (promotion) {
return promotion.isAvailable();
});
}
function SubCategory(id, name) {
this.id = id;
this.name = name;
this.products = [];
}
SubCategory.prototype.addProduct = function (product) {
this.products.push(product);
}
SubCategory.prototype.availableProducts = function () {
return this.products.filter(function (product) {
return product.available;
});
}
function Promotion(products, price) {
this.products = products;
this.price = price;
}
Promotion.prototype.name = function () {
return this.products.map(function (product) {
return product.name;
}).join(" + ");
}
Promotion.prototype.regularTotalPrice = function () {
var total = 0;
this.products.forEach(function (product) {
total += product.price;
})
return total;
}
Promotion.prototype.isAvailable = function () {
var res = this.products.filter(function (product) {
return !product.available;
});
return res.length == 0;
}