-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductCatalog.java
103 lines (89 loc) · 3.47 KB
/
ProductCatalog.java
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
101
102
103
import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class ProductCatalog {
private List<Product> products;
// Constructor
public ProductCatalog() {
products = new ArrayList<>();
loadFromFile("products.dat");
}
// Add a product
public void addProduct(Product product) throws Exception {
if (isDuplicate(product)) {
throw new Exception("Duplicate product found! Name and category must be unique.");
}
products.add(product);
saveToFile("products.dat");
}
// Check for duplicate product by name and category
private boolean isDuplicate(Product product) {
return products.stream().anyMatch(p -> p.getName().equalsIgnoreCase(product.getName()) && p.getCategory().equalsIgnoreCase(product.getCategory()));
}
// View all products
public List<Product> viewProducts() {
return products;
}
// Search for a product by name or subsequence
public List<Product> searchProduct(String subsequence) {
List<Product> result = new ArrayList<>();
for (Product product : products) {
if (product.getName().toLowerCase().contains(subsequence.toLowerCase())) {
result.add(product);
}
}
return result;
}
// Update a product
public void updateProduct(Product updatedProduct) {
for (Product product : products) {
if (product.getName().equalsIgnoreCase(updatedProduct.getName())) {
product.setCategory(updatedProduct.getCategory());
product.setPrice(updatedProduct.getPrice());
product.setQuantity(updatedProduct.getQuantity());
saveToFile("products.dat");
break;
}
}
}
// Delete a product by name
public void deleteProduct(String name) {
products.removeIf(product -> product.getName().equalsIgnoreCase(name));
saveToFile("products.dat");
}
// Sort products by name in ascending order
public void sortProductsByNameAsc() {
products.sort(Comparator.comparing(Product::getName));
}
// Sort products by name in descending order
public void sortProductsByNameDesc() {
products.sort((p1, p2) -> p2.getName().compareTo(p1.getName()));
}
// Sort products by price in ascending order
public void sortProductsByPriceAsc() {
products.sort(Comparator.comparingDouble(Product::getPrice));
}
// Sort products by price in descending order
public void sortProductsByPriceDesc() {
products.sort((p1, p2) -> Double.compare(p2.getPrice(), p1.getPrice()));
}
// Save products to a file
private void saveToFile(String fileName) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName))) {
oos.writeObject(products);
} catch (IOException e) {
System.err.println("Error saving products to file: " + e.getMessage());
}
}
// Load products from a file
@SuppressWarnings("unchecked")
private void loadFromFile(String fileName) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName))) {
products = (List<Product>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
products = new ArrayList<>();
System.err.println("Error loading products from file: " + e.getMessage());
}
}
}