-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.java
More file actions
57 lines (50 loc) · 2.09 KB
/
Copy pathInventory.java
File metadata and controls
57 lines (50 loc) · 2.09 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
import java.util.ArrayList;
import javax.swing.JOptionPane;
class Inventory {
private ArrayList<InventoryItem> items;
public Inventory() {
this.items = new ArrayList<>();
}
public void addItem(InventoryItem item) {
items.add(item);
}
public void removeItem(String model) {
for (InventoryItem item : items) {
if (item.getModel().equals(model)) {
items.remove(item);
JOptionPane.showMessageDialog(null, "Item with model " + model + " removed successfully.");
return;
}
}
JOptionPane.showMessageDialog(null, "Item with model " + model + " not found.");
}
public InventoryItem getItemByModel(String model) {
for (InventoryItem item : items) {
if (item.getModel().equals(model)) {
return item;
}
}
return null;
}
public void displayInventory() {
StringBuilder inventoryList = new StringBuilder("Inventory:\n");
for (InventoryItem item : items) {
inventoryList.append("Model: ").append(item.getModel()).append(", Brand: ").append(item.getBrand()).append(", Price: $").append(item.getPrice()).append(", Quantity: ").append(item.getQuantity()).append("\n");
}
JOptionPane.showMessageDialog(null, inventoryList.toString());
}
public void purchaseItem(String model, int quantity) {
InventoryItem item = getItemByModel(model);
if (item != null) {
if (item.getQuantity() >= quantity) {
item.setQuantity(item.getQuantity() - quantity);
double totalPrice = item.getPrice() * quantity;
JOptionPane.showMessageDialog(null, "Purchase successful!\nTotal Price: $" + totalPrice);
} else {
JOptionPane.showMessageDialog(null, "Insufficient quantity of item " + model + " in stock.");
}
} else {
JOptionPane.showMessageDialog(null, "Item with model " + model + " not found.");
}
}
}