-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProduct.java
More file actions
213 lines (176 loc) · 4.76 KB
/
Product.java
File metadata and controls
213 lines (176 loc) · 4.76 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Class: Product.java
Purpose:
- Represents a product available in the online shopping system
- Stores and updates product details
- Implements Discountable interface to handle product discounts
*/
import java.time.LocalDate;
public class Product implements Discountable {
private static final int MAX_INSTANCES = 100;
private static int instanceCount = 0;
private String productId;
private String name;
private String category;
private int stock;
private double price;
private double discountPercent;
private boolean isAvailable;
private boolean discountAvailable;
private String lastUpdated;
// Constructor
public Product(String productId, String name, String category, int stock, double price)
throws InvalidPriceException, InvalidStockException {
if (instanceCount >= MAX_INSTANCES) {
throw new IllegalStateException("Reached max Product limit " + MAX_INSTANCES);
}
if (price < 0) {
throw new InvalidPriceException("Price cannot be negative.");
}
if (stock < 0) {
throw new InvalidStockException("Stock cannot be negative.");
}
this.productId = productId;
this.name = name;
this.category = category;
this.stock = stock;
this.price = price;
this.discountPercent = 0;
this.discountAvailable = false;
this.isAvailable = stock > 0;
this.lastUpdated = LocalDate.now().toString();
instanceCount++;
}
// Getters and setters
public static int getMaxInstances() {
return MAX_INSTANCES;
}
public static int getInstanceCount() {
return instanceCount;
}
public String getProductId() {
return productId;
}
public String getName() {
return name;
}
public String getCategory() {
return category;
}
public int getStock() {
return stock;
}
public double getPrice() {
return price;
}
public double getCurrentPrice() {
return price * (1 - discountPercent / 100);
}
public boolean isAvailable() {
return isAvailable;
}
public boolean isDiscountAvailable() {
return discountAvailable;
}
public String getLastUpdated() {
return lastUpdated;
}
public void setProductId(String productId) {
this.productId = productId;
}
public void setName(String name) {
this.name = name;
}
public void setCategory(String category) {
this.category = category;
}
public void setDiscountPercent(double discountPercent) {
this.discountPercent = discountPercent;
}
public void setAvailable(boolean isAvailable) {
this.isAvailable = isAvailable;
}
public void setDiscountAvailable(boolean isDiscountAvailable) {
this.discountAvailable = isDiscountAvailable;
}
public void setLastUpdated(String lastUpdated) {
this.lastUpdated = lastUpdated;
}
public void setPrice(double price) throws InvalidPriceException {
if (price < 0) {
throw new InvalidPriceException("Price cannot be negative.");
}
this.price = price;
updateLastUpdated();
}
public void setStock(int stock) throws InvalidStockException {
if (stock < 0) {
throw new InvalidStockException("Stock cannot be negative.");
}
this.stock = stock;
isAvailable = stock > 0;
updateLastUpdated();
}
// Discount-related methods
@Override
public void applyDiscount(double percent) {
if (percent < 0 || percent > 100) {
throw new IllegalArgumentException("Discount must be between 0 and 100.");
}
discountPercent = percent;
discountAvailable = true;
updateLastUpdated();
}
@Override
public void clearDiscount() {
discountPercent = 0;
discountAvailable = false;
updateLastUpdated();
}
@Override
public double getDiscountPercent() {
return discountPercent;
}
// Product-related methods
public void restock(int amount) throws InvalidStockException {
if (amount <= 0) {
throw new InvalidStockException("Restock amount must be positive.");
}
stock += amount;
isAvailable = true;
updateLastUpdated();
}
public void reduceStock(int amount) throws InvalidStockException {
if (amount <= 0) {
throw new InvalidStockException("Amount must be positive.");
}
if (amount > stock) {
throw new InvalidStockException("Not enough stock available.");
}
stock -= amount;
isAvailable = stock > 0;
updateLastUpdated();
}
public void deleteProduct() {
stock = 0;
isAvailable = false;
updateLastUpdated();
}
private void updateLastUpdated() {
lastUpdated = LocalDate.now().toString();
}
// toString()
@Override
public String toString() {
return name
+ " [ID: "
+ productId
+ ", $"
+ String.format("%.2f", getCurrentPrice())
+ ", Stock: "
+ stock
+ ", Discount: "
+ discountPercent
+ "%]";
}
}