#JavaFood project
1 messages · Page 1 of 1 (latest)
The correct data type depends on your needs. I'm unsure why you'd use a HashMap as a first choice though. What would be your key and value respectively?
ok so this is the script
package edu.ntnu.iir.bidata;
import java.time.LocalDate;
public class Grocery {
private final String name; // Name of the item.
private double quantity; // The quantity of the items.
private double weight; // The weight of the item.
private double price; // The price of the item.
private String unit; // The measuring unit of the weight (kg, ml, gm).
private LocalDate bestBeforeDate; // The best-before date of the item.
// Constructor
public Grocery(String name, double quantity, double weight, double price, String unit, LocalDate bestBeforeDate) {
this.name = name;
this.quantity = quantity;
this.weight = weight;
this.price = price;
this.unit = unit;
this.bestBeforeDate = bestBeforeDate;
}
// Getters and setters
public String getName() {
return name;
}
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity) {
this.quantity = quantity;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public LocalDate getBestBeforeDate() {
return bestBeforeDate;
}
public void setBestBeforeDate(LocalDate bestBeforeDate) {
this.bestBeforeDate = bestBeforeDate;
}
// Utility methods
@Override
public String toString() {
return "Grocery{" +
"name='" + name + '\'' +
", quantity=" + quantity +
", weight=" + weight +
", price=" + price +
", unit='" + unit + '\'' +
", bestBeforeDate=" + bestBeforeDate +
'}';
}
// Example of checking if the item is past its best-before date
public boolean isPastBestBeforeDate() {
return LocalDate.now().isAfter(bestBeforeDate);
}
// Example of calculating total cost based on quantity
public double calculateTotalCost() {
return quantity * price;
}
// Example of displaying formatted weight
public String getFormattedWeight() {
return weight + " " + unit;
}
}
the is the grocery class
this is the gui
That doesn't really answer my question, and what if I buy the same good two times with a different expiration date?
I am also unsure what you mean with will be saved only for 1 session. Regardless of the data type the data will be gone if you do not persist it in some manner.
Do you have any tips how i can do it better? I am beginner in java som i still don't have good control yet