-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
177 lines (147 loc) · 5.22 KB
/
Main.java
File metadata and controls
177 lines (147 loc) · 5.22 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
// Custom Exceptions
class InsufficientStockException extends Exception {
public InsufficientStockException(String message) {
super(message);
}
}
class InvalidQuantityException extends Exception {
public InvalidQuantityException(String message) {
super(message);
}
}
// Encapsulation
class Product {
private String productId;
private String productName;
private int quantity;
private double price;
public Product(String productId, String productName, int quantity, double price) {
this.productId = productId;
this.productName = productName;
this.quantity = quantity;
this.price = price;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
// Inheritance
class PerishableProduct extends Product {
private String expirationDate;
private static final int MAX_QUANTITY = 100;
public PerishableProduct(String productId, String productName, int quantity, double price, String expirationDate) {
super(productId, productName, quantity, price);
this.expirationDate = expirationDate;
}
public String getExpirationDate() {
return expirationDate;
}
public void setExpirationDate(String expirationDate) {
this.expirationDate = expirationDate;
}
public void updateStock(int amount) {
try {
if (amount < 0) {
if (getQuantity() + amount < 0) {
System.out.println("Insufficient stock for product " + getProductName() + ".");
}
} else if (amount < 0) {
System.out.println("Quantity cannot be negative.");
}
int newQuantity = getQuantity() + amount;
if (newQuantity > MAX_QUANTITY) {
System.out.println("Cannot exceed maximum quantity of " + MAX_QUANTITY + " for perishable products.");
} else {
setQuantity(newQuantity);
System.out.println("Stock updated: " + getProductName() + " new quantity is " + getQuantity());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class NonPerishableProduct extends Product {
private int shelfLife;
public NonPerishableProduct(String productId, String productName, int quantity, double price, int shelfLife) {
super(productId, productName, quantity, price);
this.shelfLife = shelfLife;
}
public int getShelfLife() {
return shelfLife;
}
public void setShelfLife(int shelfLife) {
this.shelfLife = shelfLife;
}
public void updateStock(int amount) {
try {
if (amount < 0) {
if (getQuantity() + amount < 0) {
System.out.println("Insufficient stock for product " + getProductName() + ".");
}
} else if (amount < 0) {
System.out.println("Quantity cannot be negative.");
}
int newQuantity = getQuantity() + amount;
setQuantity(newQuantity);
System.out.println("Stock updated: " + getProductName() + " new quantity is " + getQuantity());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
// Abstraction
abstract class InventoryOperation {
public abstract void addProduct(Product product);
public abstract void removeProduct(String productId);
}
class Inventory extends InventoryOperation {
private java.util.List<Product> products = new java.util.ArrayList<>();
@Override
public void addProduct(Product product) {
products.add(product);
System.out.println("Product added: " + product.getProductName() + " with quantity " + product.getQuantity());
}
@Override
public void removeProduct(String productId) {
for (Product product : products) {
if (product.getProductId().equals(productId)) {
products.remove(product);
System.out.println("Product " + productId + " removed successfully.");
return;
}
}
System.out.println("Product not found.");
}
public void showProducts() {
System.out.println("Current inventory:");
for (Product product : products) {
System.out.println(product.getProductName() + " - Quantity: " + product.getQuantity());
}
}
}
// Main Class
public class Main {
public static void main(String[] args) {
Inventory inventory = new Inventory();
PerishableProduct apple =