-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmini-inventory.js
More file actions
116 lines (95 loc) · 3.11 KB
/
mini-inventory.js
File metadata and controls
116 lines (95 loc) · 3.11 KB
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
/* eslint-disable max-len */
let ItemManager = {
items: [],
create (itemName, category, quantity) {
if (itemName.length >= 5 &&
category.length >= 5 &&
!category.includes(' ') &&
quantity !== undefined) {
this.items.push(new Item(itemName, category, quantity));
return true;
} else {
return false;
}
},
update (skuCode, ...args) {
let foundItem = this.items.find(item => item.skuCode === skuCode);
Object.assign(foundItem, ...args);
},
delete (skuCode) {
let foundIndex = this.items.findIndex(item => item.skuCode === skuCode);
this.items.splice(foundIndex,1);
},
inStock() {
let inStockItems = this.items.filter (item => item.quantity > 0);
inStockItems.forEach (item => console.log(item));
},
itemsInCategory(category) {
let filteredItems = this.items.filter (item => item.category === category);
filteredItems.forEach(item => console.log(item));
}
};
function Item (itemName, category, quantity) {
this.skuCode = (itemName.slice(0,3) + category.slice(0,2)).toUpperCase();
this.itemName = itemName;
this.category = category;
this.quantity = quantity;
}
let ReportManager = {
items: [],
init (source) {
this.items = source.items;
},
createReporter(skuCode) {
let foundItem = this.items.find(item => item.skuCode === skuCode);
return {
itemInfo() {
for (let property in foundItem) {
console.log(`${property}: ${foundItem[property]}`);
}
}
};
},
reportInStock() {
let inStockItems = this.items.filter(item => item.quantity > 0);
let inStockItemNames = [];
inStockItems.forEach(item => inStockItemNames.push(item.itemName));
console.log(inStockItemNames.toString());
}
};
ItemManager.create('basket ball', 'sports', 0); // valid item
ItemManager.create('asd', 'sports', 0);
ItemManager.create('soccer ball', 'sports', 5); // valid item
ItemManager.create('football', 'sports');
ItemManager.create('football', 'sports', 3); // valid item
ItemManager.create('kitchen pot', 'cooking items', 0);
ItemManager.create('kitchen pot', 'cooking', 3); // valid item
// returns list with the 4 valid items
console.log(ItemManager.items);
ReportManager.init(ItemManager);
// logs soccer ball,football,kitchen pot
ReportManager.reportInStock();
ItemManager.update('SOCSP', { quantity: 0 });
// returns list with the item objects for football and kitchen pot
ItemManager.inStock();
// football,kitchen pot
ReportManager.reportInStock();
// returns list with the item objects for basket ball, soccer ball, and football
ItemManager.itemsInCategory('sports');
ItemManager.delete('SOCSP');
// returns list the remaining 3 valid items (soccer ball is removed from the list)
console.log(ItemManager.items);
let kitchenPotReporter = ReportManager.createReporter('KITCO');
kitchenPotReporter.itemInfo();
// logs
// skuCode: KITCO
// itemName: kitchen pot
// category: cooking
// quantity: 3
ItemManager.update('KITCO', { quantity: 10 });
kitchenPotReporter.itemInfo();
// logs
// skuCode: KITCO
// itemName: kitchen pot
// category: cooking
// quantity: 10