csep-2025/commons/src/main/java/commons/Ingredient.java

122 lines
3.1 KiB
Java

package commons;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.util.Objects;
@Entity
public class Ingredient {
//I don't like magic numbers
// online it says nutrition are this much kcal per gram
public static final double KCAL_PER_GRAM_PROTEIN = 4.0;
public static final double KCAL_PER_GRAM_CARBS = 4.0;
public static final double KCAL_PER_GRAM_FAT= 9.0;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long id;
// FIXME Dec 22 2025::temporarily made this not a unique constraint because of weird JPA behaviour
@Column(name = "name", nullable = false, unique = false)
public String name;
@Column(name = "protein", nullable = false)
public double proteinPer100g;
@Column(name = "fat", nullable = false)
public double fatPer100g;
@Column(name = "carbs", nullable = false)
public double carbsPer100g;
public Ingredient() {}
public Ingredient(
Long id,
String name,
double proteinPer100g,
double fatPer100g,
double carbsPer100g) {
this.id = id;
this.name = name;
this.proteinPer100g = proteinPer100g;
this.fatPer100g = fatPer100g;
this.carbsPer100g = carbsPer100g;
}
public Ingredient(
String name,
double proteinPer100g,
double fatPer100g,
double carbsPer100g) {
this.name = name;
this.proteinPer100g = proteinPer100g;
this.fatPer100g = fatPer100g;
this.carbsPer100g = carbsPer100g;
}
public double kcalPer100g() {
return proteinPer100g * KCAL_PER_GRAM_PROTEIN
+ carbsPer100g * KCAL_PER_GRAM_CARBS
+ fatPer100g * KCAL_PER_GRAM_FAT;
}
public void setId(long id) {
this.id = id;
}
public double getProteinPer100g() {
return proteinPer100g;
}
public double getFatPer100g() {
return fatPer100g;
}
public double getCarbsPer100g() {
return carbsPer100g;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name){
this.name = name;
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Ingredient that = (Ingredient) o;
return id == that.id && Double.compare(proteinPer100g, that.proteinPer100g) == 0 && Double.compare(fatPer100g, that.fatPer100g) == 0 && Double.compare(carbsPer100g, that.carbsPer100g) == 0 && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name, proteinPer100g, fatPer100g, carbsPer100g);
}
@Override
public String toString() {
return "Ingredient " + id + " - " + name +
"= P:" + proteinPer100g +
"/F:" + fatPer100g +
"/C:" + carbsPer100g + " per 100g";
}
}