-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinventory.js
73 lines (63 loc) · 2.11 KB
/
inventory.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
import * as data from "../data.json";
/**
* The class that contains information about the inventory.
*/
export default class Inventory {
/**
* Create an Inventory
* @param {Object = {}} inventory - Can be initialized with an existing inventory object, otherwise it creates an empty inventory object.
*/
constructor(inventory = {}) {
this._inventory = inventory;
}
get inventory() {
return this._inventory;
}
/**
* Placeholder for a deep clone.
* @returns The instance's attributes
*/
clone = () => this._inventory;
/**
* Placeholder for a API call to the inventory. Copies the data.json inventory in place of the API call and sets the instance's inventory to the response.
*/
fetchInventory = () => {
this._inventory = { ...data.inventory };
};
/**
* Subtracts an item's ingredients from the inventory
* @param {Object} item - An Item object defined by ./item.js
*/
subtractItemFromInventory = (item) => {
let updatedInventory = this._inventory;
for (let ingredient in item.ingredients) {
updatedInventory[ingredient] -= item.ingredients[ingredient];
}
this._inventory = updatedInventory;
};
/**
* Adds an item's ingredients to the inventory
* @param {Object} item - An Item object defined by ./item.js
*/
addItemToInventory = (item) => {
let updatedInventory = this._inventory;
for (let ingredient in item.ingredients) {
updatedInventory[ingredient] += item.ingredients[ingredient];
}
this._inventory = updatedInventory;
};
/**
* Adds all ingredients from an order to the inventory
* @param {Object} order - An Order object defined by ./order.js
*/
addOrderToInventory = (order) => {
order.itemList.forEach((item) => this.addItemToInventory(item));
};
/**
* Subtracts all ingredients from an order from the inventory
* @param {Object} order - An Order object defined by ./order.js
*/
subtractOrderFromInventory = (order) => {
order.itemList.forEach((item) => this.subtractItemFromInventory(item));
};
}