Skip to content

Commit eb8ece9

Browse files
author
Potecaru Tudor
committed
Updated product inventary
1 parent 5b5eebc commit eb8ece9

File tree

1 file changed

+46
-50
lines changed

1 file changed

+46
-50
lines changed

Diff for: Classes/00. Product Inventary Project.js

+46-50
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,62 @@
55

66
(function () {
77
"use strict";
8-
}());
98

10-
function Product(id, name, price, quantity) {
11-
this.id = id;
12-
this.name = name;
13-
this.price = price;
14-
this.quantity = quantity;
15-
}
16-
17-
Product.prototype = {
18-
add : function (count) {
19-
this.quantity = !count ? this.quantity++ : this.quantity + count;
20-
},
9+
function Product(id, name, price, quantity) {
10+
this.id = id;
11+
this.name = name;
12+
this.price = price;
13+
this.quantity = quantity;
14+
}
2115

22-
remove : function (count) {
23-
this.quantity = !count ? this.quantity-- : this.quantity - count >= 0 ? this.quantity - count : 0;
24-
},
16+
Product.prototype = {
17+
add : function (count) {
18+
this.quantity += count === 0 || count ? count : 1;
19+
},
2520

26-
setPrice : function (value) {
27-
this.price = value;
28-
},
21+
remove : function (count) {
22+
this.quantity -= count === 0 || count ? count : 1;
23+
},
2924

30-
toString : function () {
31-
return this.name;
32-
}
33-
};
25+
setPrice : function (value) {
26+
this.price = value;
27+
},
3428

35-
function Inventary() {
36-
this.items = arguments !== undefined ? Array.prototype.slice.call(arguments) : [];
37-
}
29+
toString : function () {
30+
return this.name;
31+
}
32+
};
3833

39-
Inventary.prototype = {
40-
add : function (item) {
41-
this.items.push(item);
42-
},
34+
function Inventary() {
35+
this.items = Array.prototype.slice.call(arguments);
36+
}
4337

44-
remove : function (item) {
45-
var index = this.items.indexOf(item);
38+
Inventary.prototype = {
39+
add : function (item) {
40+
this.items.push(item);
41+
},
4642

47-
if (index > -1) {
48-
this.items.splice(index, 1);
49-
}
50-
},
43+
remove : function (item) {
44+
var index = this.items.indexOf(item);
5145

52-
getTotalPrice : function () {
53-
var totalPrice = 0;
46+
if (index > -1) {
47+
this.items.splice(index, 1);
48+
}
49+
},
5450

55-
return (function (items) {
56-
items.map(function (item) {
57-
totalPrice += item.price;
58-
});
51+
getTotalPrice : function () {
52+
var sum = this.items.reduce(function (previousValue, currentValue, index, array) {
53+
return previousValue + currentValue.price;
54+
}, 0);
5955

60-
return totalPrice;
61-
}(this.items));
62-
}
63-
};
56+
return sum;
57+
}
58+
};
6459

65-
var peaches = new Product(0, "peaches", 5, 5000);
66-
var carrots = new Product(1, "carrots", 2, 10000);
67-
var bananas = new Product(2, "bananas", 6, 3000);
60+
var peaches = new Product(0, "peaches", 5, 5000),
61+
carrots = new Product(1, "carrots", 2, 10000),
62+
bananas = new Product(2, "bananas", 6, 3000),
63+
inventary = new Inventary(peaches, carrots, bananas);
6864

69-
var inventary = new Inventary(peaches, carrots, bananas);
70-
inventary.getTotalPrice();
65+
inventary.getTotalPrice();
66+
}());

0 commit comments

Comments
 (0)