-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFood.java
More file actions
54 lines (46 loc) · 1.87 KB
/
Food.java
File metadata and controls
54 lines (46 loc) · 1.87 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
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class Food extends Product {
private String expiryDate;
public Food(int productID, String productName, String category, double price, int quantityInStock, String expiryDate) {
super(productID, productName, category, price, quantityInStock);
this.expiryDate = expiryDate;
}
// Getter & Setter for expiryDate
public String getExpiryDate() {
return this.expiryDate;
}
public void setExpiryDate(String newExpiryDate) {
this.expiryDate = newExpiryDate;
}
@Override
public void addStock(int quantity) {
if (isProductNotExpired() == true) {
super.addStock(quantity);
} else {
System.out.println("You can't add this food item to the stock, it is expired!!!");
}
}
// Method to check if the product is expired
private boolean isProductNotExpired() {
try {
LocalDate expiry = LocalDate.parse(expiryDate, DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate today = LocalDate.now();
// Returns true if today is before or on the expiry date
return !today.isAfter(expiry);
} catch (DateTimeParseException e) {
System.out.println("Error: Invalid expiry date format for product: " + getProductName());
return false;
}
}
@Override
public void displayProductInfo() {
System.out.println("Product info for "+super.getProductName());
System.out.println("Product ID: "+super.getProductID());
System.out.println("Category: "+super.getCategory());
System.out.println("Price: "+super.getPrice());
System.out.println("Qty in Stock: "+super.getQuantityInStock());
System.out.println("Exp Date: "+this.expiryDate);
}
}