-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path00. Product Inventary Project.js
66 lines (52 loc) · 1.79 KB
/
00. Product Inventary Project.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
/*
Classes 00. Product Inventory Project - Create an application which manages an inventory of products. Create a product class which has a price, id, and quantity on hand.
Then create an inventory class which keeps track of various products and can sum up the inventory value.
*/
(function () {
"use strict";
function Product(id, name, price, quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
Product.prototype = {
add : function (count) {
this.quantity += count === 0 || count ? count : 1;
},
remove : function (count) {
this.quantity -= count === 0 || count ? count : 1;
},
setPrice : function (value) {
this.price = value;
},
toString : function () {
return this.name;
}
};
function Inventary() {
this.items = Array.prototype.slice.call(arguments);
}
Inventary.prototype = {
add : function (item) {
this.items.push(item);
},
remove : function (item) {
var index = this.items.indexOf(item);
if (index > -1) {
this.items.splice(index, 1);
}
},
getTotalPrice : function () {
var sum = this.items.reduce(function (previousValue, currentValue, index, array) {
return previousValue + currentValue.price * currentValue.quantity;
}, 0);
return sum;
}
};
var peaches = new Product(0, "peaches", 5, 5000),
carrots = new Product(1, "carrots", 2, 10000),
bananas = new Product(2, "bananas", 6, 3000),
inventary = new Inventary(peaches, carrots, bananas);
return inventary.getTotalPrice();
}());