-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.js
100 lines (87 loc) · 2.38 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
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
export default class Inventory {
constructor() {
this._products = new Array();
}
getArray() {
return this._products;
}
add(product) {
let pos = this._findProducts(product);
if(pos) {
return false;
}
this._products.push(product);
return true;
}
delete(code) {
let pos = this._verifyCode(code);
if(pos >= 0) {
for(let i = pos; i < (this._products.length - 1); i++) {
this._products[i] = this._products[i+1];
}
this._products.pop();
return true;
} else {
return null;
}
}
search(product) {
let verify = this._verifyCode(product);
if(verify < 0) {
return null;
} else {
return this._products[verify];
}
}
list() {
if(this._getLength() <= 0) {
return false;
} else {
return true;
}
}
reverseList() {
if(this._getLength() <= 0) {
return false;
} else {
return true;
}
}
insert(product, inPos) {
let nPos;
let find = this._findProducts(product);
if(find) {
return false;
} else {
for(inPos; inPos < (this._products.length + 1); inPos++) {
if(inPos === this._getLength()) {
this._products.push(product);
return;
}
nPos = this._products[inPos];
this._products[inPos] = product;
product = nPos;
}
return true;
}
}
_getLength() {
return this._products.length;
}
_verifyCode(code) {
for(let i= 0; i < this._getLength(); i++) {
if(this._products[i].getCode() === code){
return i;
}
}
return -1;
}
_findProducts(product) {
for(let i= 0; i < this._getLength(); i++) {
if(this._products[i].getCode() === product.getCode()){
return true;
}
}
return false;
}
}