-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreInventoryArray.java
More file actions
97 lines (86 loc) · 3.17 KB
/
StoreInventoryArray.java
File metadata and controls
97 lines (86 loc) · 3.17 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
public class StoreInventoryArray {
private final Product[] products;
private int productCount;
// Constructor
public StoreInventoryArray(int capacity) {
this.products = new Product[capacity];
this.productCount = 0;
}
// Add a product to the inventory
public void addProduct(Product product) {
int index = findProductIndexByID(product.getProductID());
if (index == -1) {
if (this.productCount < products.length) {
this.products[this.productCount] = product;
this.productCount++;
System.out.println("Your product has been added successfully.");
} else {
System.out.println("Sorry, The inventory is full.");
}
} else {
System.out.println("You can't add a product that is already in the inventory.");
}
}
// Remove a product by ID
public void removeProduct(int productID) {
int index = findProductIndexByID(productID);
if (index != -1) {
for (int i = index; i < this.productCount - 1; i++) {
this.products[i] = this.products[i + 1];
}
this.products[this.productCount - 1] = null;
this.productCount--;
System.out.println("The product has been removed successfully.");
} else {
System.out.println("The Product with ID " + productID + " has not been found.");
}
}
// Find product index by ID
private int findProductIndexByID(int productID) {
for (int i = 0; i < this.productCount; i++) {
if (this.products[i].getProductID() == productID) {
return i;
}
}
return -1;
}
// Add stock to a product
public void addStockToProduct(int productID, int quantity) {
int index = findProductIndexByID(productID);
if (index != -1) {
this.products[index].addStock(quantity);
System.out.println("Stock added successfully.");
} else {
System.out.println("The product with ID " + productID + " not found.");
}
}
// Remove stock from a product
public void removeStockFromProduct(int productID, int quantity) {
int index = findProductIndexByID(productID);
if (index != -1) {
this.products[index].removeStock(quantity);
System.out.println("The stock has been removed successfully.");
} else {
System.out.println("The Product with ID " + productID + " not found. ");
}
}
// Display all products
public void displayAllProducts() {
if (this.productCount == 0) {
System.out.println("The inventory is empty.");
} else {
for (int i = 0; i < this.productCount; i++) {
this.products[i].displayProductInfo();
System.out.println("___________________________");
}
}
}
// Generate a detailed inventory report
public void generateReport() {
System.out.println("Full inventory Report:");
displayAllProducts();
}
public int getProductCount() {
return this.productCount;
}
}