-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventoryManagement.js
62 lines (54 loc) · 1.71 KB
/
InventoryManagement.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
import { Product } from './Product.js';
export class InventoryManagement {
constructor() {
this.products = []; // Array zur Speicherung von Produkten
this.stock = new Map(); // Map zur Verwaltung der Bestände
}
// Produkt hinzufügen
addProduct(product, quantity = 0) {
if (!(product instanceof Product)) {
throw new Error('Das Objekt muss eine Instanz der Product-Klasse sein.');
}
this.products.push(product);
this.stock.set(product.id, quantity);
}
// Produkt entfernen
removeProduct(productId) {
this.products = this.products.filter(product => product.id !== productId);
this.stock.delete(productId);
}
updateStock(productId, increment) {
if (!this.stock.has(productId)) {
throw new Error('Produkt nicht gefunden.');
}
if (increment < 0) {
throw new Error('Bestand kann nicht negativ sein.');
}
this.stock.set(productId, this.getStock(productId) + increment);
}
getStock(productId) {
if (!this.stock.has(productId)) {
throw new Error('Produkt nicht gefunden.');
}
return this.stock.get(productId);
}
// Produkte basierend auf einer Kategorie abrufen
getProductsByCategory(categoryId) {
return this.products.filter(product =>
product.categoryId === categoryId && this.getStock(product.id) > 0
);
}
// Alle Produkte auflisten
listAllProducts() {
return this.products.map(product => ({
...product,
stock: this.getStock(product.id)
}));
}
// Produkte basierend auf einem Ablaufdatum filtern
getExpiredProducts(currentDate) {
return this.products.filter(product =>
new Date(product.expiryDate) < new Date(currentDate) && this.getStock(product.id) > 0
);
}
}