[POO] aula08 ex2 added

This commit is contained in:
TiagoRG 2023-04-08 23:33:30 +01:00
parent eeb2cba5bd
commit 2e45082e24
16 changed files with 652 additions and 0 deletions

View File

@ -0,0 +1,66 @@
package aula08.ex2.Aliments;
import aula08.ex2.Enums.AlimentOrigin;
import java.util.Objects;
public abstract class Aliment {
double proteins;
double calories;
double weight;
final AlimentOrigin alimentOrigin;
public Aliment(double proteins, double calories, double weight, AlimentOrigin alimentOrigin) {
setProteins(proteins);
setCalories(calories);
setCalories(weight);
this.alimentOrigin = alimentOrigin;
}
public double getProteins() {
return this.proteins;
};
public void setProteins(double proteins) {
if (proteins <= 0)
throw new IllegalArgumentException("Proteins must be positive");
this.proteins = proteins;
}
public double getCalories() {
return this.calories;
}
public void setCalories(double calories) {
if (calories <= 0)
throw new IllegalArgumentException("Calories must be positive");
this.calories = calories;
}
public double getWeight() {
return this.weight;
}
public void setWeight(double weight) {
if (weight <= 0)
throw new IllegalArgumentException("Weight must be positive");
this.weight = weight;
}
public AlimentOrigin getAlimentOrigin() {
return this.alimentOrigin;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Aliment aliment = (Aliment) o;
return Double.compare(aliment.getProteins(), getProteins()) == 0 && Double.compare(aliment.getCalories(), getCalories()) == 0 && Double.compare(aliment.getWeight(), getWeight()) == 0 && getAlimentOrigin() == aliment.getAlimentOrigin();
}
@Override
public int hashCode() {
return Objects.hash(getProteins(), getCalories(), getWeight(), getAlimentOrigin());
}
}

View File

@ -0,0 +1,37 @@
package aula08.ex2.Aliments;
import aula08.ex2.Enums.AlimentOrigin;
import java.util.Objects;
public class Cereal extends Aliment {
private final String name;
public Cereal(String name, double proteins, double calories, double weight) {
super(proteins, calories, weight, AlimentOrigin.VEGAN);
this.name = name;
}
public String getName() {
return this.name;
}
@Override
public String toString() {
return String.format("Cereal %s, Proteins: %.1f, Calories: %.1f, Weight: %.1f", this.getName(), this.getProteins(), this.getCalories(), this.getWeight());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Cereal cereal = (Cereal) o;
return getName().equals(cereal.getName());
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getName());
}
}

View File

@ -0,0 +1,42 @@
package aula08.ex2.Aliments;
import aula08.ex2.Enums.AlimentOrigin;
import aula08.ex2.Enums.FishState;
import java.util.Objects;
public class Fish extends Aliment {
private FishState fishState;
public Fish(FishState fishState, double proteins, double calories, double weight) {
super(proteins, calories, weight, AlimentOrigin.ANIMAL);
this.fishState = fishState;
}
public FishState getFishState() {
return this.fishState;
}
public void setFishState(FishState fishState) {
this.fishState = fishState;
}
@Override
public String toString() {
return String.format("Fish %s, Proteins: %.1f, Calories: %.1f, Weight: %.1f", this.getFishState().toString(), this.getProteins(), this.getCalories(), this.getWeight());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Fish fish = (Fish) o;
return getFishState() == fish.getFishState();
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getFishState());
}
}

View File

@ -0,0 +1,42 @@
package aula08.ex2.Aliments;
import aula08.ex2.Enums.AlimentOrigin;
import aula08.ex2.Enums.MeatType;
import java.util.Objects;
public class Meat extends Aliment {
private MeatType meatType;
public Meat(MeatType meatType, double proteins, double calories, double weight) {
super(proteins, calories, weight, AlimentOrigin.ANIMAL);
setMeatType(meatType);
}
public MeatType getMeatType() {
return this.meatType;
}
public void setMeatType(MeatType meatType) {
this.meatType = meatType;
}
@Override
public String toString() {
return String.format("Meat %s, Proteins: %.1f, Calories: %.1f, Weight: %.1f", this.getMeatType().toString(), this.getProteins(), this.getCalories(), this.getWeight());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Meat meat = (Meat) o;
return getMeatType() == meat.getMeatType();
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getMeatType());
}
}

View File

@ -0,0 +1,37 @@
package aula08.ex2.Aliments;
import aula08.ex2.Enums.AlimentOrigin;
import java.util.Objects;
public class Vegetable extends Aliment {
private final String name;
public Vegetable(String name, double proteins, double calories, double weight) {
super(proteins, calories, weight, AlimentOrigin.VEGAN);
this.name = name;
}
public String getName() {
return this.name;
}
@Override
public String toString() {
return String.format("Vegetable %s, Proteins: %.1f, Calories: %.1f, Weight: %.1f", this.getName(), this.getProteins(), this.getCalories(), this.getWeight());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Vegetable vegetable = (Vegetable) o;
return getName().equals(vegetable.getName());
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getName());
}
}

View File

@ -0,0 +1,40 @@
package aula08.ex2.Dishes;
import aula08.ex2.Aliments.Aliment;
import java.util.Objects;
public class DietDish extends Dish {
private final double maxCalories;
public DietDish(String name, double maxCalories) {
super(name);
this.maxCalories = maxCalories;
}
@Override
public void addAliment(Aliment aliment) {
if (aliment.getCalories() > (maxCalories - this.calculateCalories()))
throw new IllegalArgumentException("Aliment is over calories limit.");
super.addAliment(aliment);
}
@Override
public String toString() {
return String.format("Dish '%s', using %d aliments - Diet (%.1f calories)", this.getName(), this.getAlimentCount(), this.calculateCalories());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
DietDish dietDish = (DietDish) o;
return Double.compare(dietDish.maxCalories, maxCalories) == 0;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), maxCalories);
}
}

View File

@ -0,0 +1,70 @@
package aula08.ex2.Dishes;
import aula08.ex2.Aliments.Aliment;
import java.util.LinkedList;
import java.util.Objects;
public class Dish {
private final String name;
private final LinkedList<Aliment> aliments;
public Dish(String name) {
this.name = name;
this.aliments = new LinkedList<>();
}
public String getName() {
return name;
}
public LinkedList<Aliment> getAliments() {
return this.aliments;
}
public void addAliment(Aliment aliment) {
this.aliments.add(aliment);
}
public int getAlimentCount() {
return this.aliments.size();
}
public double calculateCalories() {
double total = 0;
for (Aliment aliment : this.aliments)
total += aliment.getCalories();
return total;
}
public double calculateProteins() {
double total = 0;
for (Aliment aliment : this.aliments)
total += aliment.getProteins();
return total;
}
public double calculateWeight() {
double total = 0;
for (Aliment aliment : this.aliments)
total += aliment.getWeight();
return total;
}
@Override
public String toString() {
return String.format("Dish '%s', using %d aliments", this.getName(), this.getAlimentCount());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dish dish = (Dish) o;
return getName().equals(dish.getName()) && getAliments().equals(dish.getAliments());
}
@Override
public int hashCode() {
return Objects.hash(getName(), getAliments());
}
}

View File

@ -0,0 +1,22 @@
package aula08.ex2.Dishes;
import aula08.ex2.Aliments.Aliment;
import aula08.ex2.Enums.AlimentOrigin;
public class VeganDish extends Dish {
public VeganDish(String name) {
super(name);
}
@Override
public void addAliment(Aliment aliment) {
if (aliment.getAlimentOrigin() == AlimentOrigin.ANIMAL)
throw new IllegalArgumentException("Aliment must be vegan.");
super.addAliment(aliment);
}
@Override
public String toString() {
return String.format("Dish '%s', using %d aliments - Vegan dish", this.getName(), this.getAlimentCount());
}
}

View File

@ -0,0 +1,13 @@
package aula08.ex2.Enums;
public enum AlimentOrigin {
ANIMAL, VEGAN;
@Override
public String toString() {
return switch (this) {
case ANIMAL -> "ANIMAL";
case VEGAN -> "VEGAN";
};
}
}

View File

@ -0,0 +1,25 @@
package aula08.ex2.Enums;
public enum AlimentType {
MEAT, FISH, CEREAL, VEGETABLE;
public static AlimentType fromString(String string) {
return switch (string) {
case "MEAT", "Meat", "meat" -> MEAT;
case "FISH", "Fish", "fish" -> FISH;
case "CEREAL", "Cereal", "cereal" -> CEREAL;
case "VEGETABLE", "Vegetable", "vegetable" -> VEGETABLE;
default -> null;
};
}
@Override
public String toString() {
return switch (this) {
case MEAT -> "Meat";
case FISH -> "Fish";
case CEREAL -> "Cereal";
case VEGETABLE -> "Vegetable";
};
}
}

View File

@ -0,0 +1,23 @@
package aula08.ex2.Enums;
public enum DishType {
NORMAL, VEGAN, DIET;
public static DishType fromString(String string) {
return switch (string) {
case "NORMAL", "Normal", "normal" -> NORMAL;
case "VEGAN", "Vegan", "vegan" -> VEGAN;
case "DIET", "Diet", "diet" -> DIET;
default -> null;
};
}
@Override
public String toString() {
return switch (this) {
case NORMAL -> "NORMAL";
case VEGAN -> "VEGAN";
case DIET -> "DIET";
};
}
}

View File

@ -0,0 +1,21 @@
package aula08.ex2.Enums;
public enum FishState {
FRESH, FROZEN;
public static FishState fromString(String string) {
return switch (string) {
case "FRESH", "Fresh", "fresh" -> FRESH;
case "FROZEN", "Frozen", "frozen" -> FROZEN;
default -> null;
};
}
@Override
public String toString() {
return switch (this) {
case FRESH -> "FRESH";
case FROZEN -> "FROZEN";
};
}
}

View File

@ -0,0 +1,25 @@
package aula08.ex2.Enums;
public enum MeatType {
COW, PORK, TURKEY, CHICKEN;
public static MeatType fromString(String string) {
return switch (string) {
case "COW", "Cow", "cow" -> COW;
case "PORK", "Pork", "pork" -> PORK;
case "TURKEY", "Turkey", "turkey" -> TURKEY;
case "CHICKEN", "Chicken", "chicken" -> CHICKEN;
default -> null;
};
}
@Override
public String toString() {
return switch (this) {
case COW -> "COW";
case PORK -> "PORK";
case TURKEY -> "TURKEY";
case CHICKEN -> "CHICKEN";
};
}
}

View File

@ -0,0 +1,27 @@
package aula08.ex2;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
private static final Scanner sin = new Scanner(System.in);
private static LinkedList<Menu> Menus;
public static void main(String[] args) {
while (true) {
System.out.print("Choose an option:\n1 - Create menu\n2 - List menus\n0 - Exit\n\n>> ");
int option = Integer.parseInt(sin.nextLine());
switch (option) {
case 0 -> {
sin.close();
System.exit(0);
}
case 1 -> Menus.add(new Menu(sin));
case 2 -> {
for (Menu menu : Menus)
System.out.println(menu);
}
}
}
}
}

View File

@ -0,0 +1,131 @@
package aula08.ex2;
import aula08.ex2.Aliments.*;
import aula08.ex2.Dishes.*;
import aula08.ex2.Enums.*;
import utils.Enums.Weekday;
import java.util.LinkedHashMap;
import java.util.Scanner;
public class Menu {
private final Scanner sin;
private final LinkedHashMap<Weekday, Dish> menu;
public Menu(Scanner sin, LinkedHashMap<Weekday, Dish> menu) {
this.sin = sin;
this.menu = menu;
while (true) {
System.out.print("Choose an option:\n1 - Add dish\n2 - Remove dish\n3 - Print menu\n0 - Exit\n\n>> ");
int option = Integer.parseInt(sin.nextLine());
switch (option) {
case 0 -> {
return;
}
case 1 -> {
System.out.print("What weekday do you want to add a dish to: ");
Weekday weekday = Weekday.fromString(sin.nextLine());
if (this.menu.containsKey(weekday))
System.out.print("That weekday already has a dish assigned to it.");
else
this.menu.put(weekday, this.dishBuilder());
}
case 2 -> {
System.out.print("What weekday dish do you want to remove: ");
Weekday weekday = Weekday.fromString(sin.nextLine());
try {
this.menu.remove(weekday);
} catch (Exception ignored) {
System.out.println("That weekday has no dish assigned to.");
}
}
case 3 -> System.out.println(this);
default -> System.out.println("Invalid option.");
}
}
}
public Menu(Scanner sin) {
this(sin, new LinkedHashMap<>());
}
public Menu() {
this(new Scanner(System.in), new LinkedHashMap<>());
}
public Dish dishBuilder() {
System.out.print("Dish name: ");
String name = sin.nextLine();
Dish dish;
while (true) {
System.out.print("Dish type? (NORMAL/VEGAN/DIET): ");
DishType dishType = DishType.fromString(sin.nextLine());
switch (dishType) {
case NORMAL -> dish = new Dish(name);
case VEGAN -> dish = new VeganDish(name);
case DIET -> {
System.out.print("Calories limit: ");
double caloriesLimit = Double.parseDouble(sin.nextLine());
dish = new DietDish(name, caloriesLimit);
}
default -> {
System.out.print("Invalid option.");
continue;
}
}
break;
}
while (true) {
System.out.print("Choose an option:\n1 - Add aliment\n2 - Print dish\n0 - Finish dish\n\n>> ");
int option = Integer.parseInt(sin.nextLine());
switch (option) {
case 0 -> {
return dish;
}
case 1 -> {
Aliment aliment = null;
System.out.print("Aliment calories: ");
double calories = Double.parseDouble(sin.nextLine());
System.out.print("Aliment proteins: ");
double proteins = Double.parseDouble(sin.nextLine());
System.out.print("Aliment weight: ");
double weight = Double.parseDouble(sin.nextLine());
System.out.print("What type of aliment are u adding? (MEAT/FISH/CEREAL/VEGETABLE): ");
AlimentType alimentType = AlimentType.fromString(sin.nextLine());
switch (alimentType) {
case MEAT -> {
System.out.print(alimentType + " type? (COW/PORK/TURKEY/CHICKEN): ");
MeatType meatType = MeatType.fromString(sin.nextLine());
aliment = new Meat(meatType, proteins, calories, weight);
}
case FISH -> {
System.out.print(alimentType + " state? (FRESH/FROZEN): ");
FishState fishState = FishState.fromString(sin.nextLine());
aliment = new Fish(fishState, proteins, calories, weight);
}
case CEREAL, VEGETABLE -> {
System.out.print(alimentType + " name: ");
String alimentName = sin.nextLine();
aliment = alimentType == AlimentType.CEREAL ? new Cereal(alimentName, proteins, calories, weight) : new Vegetable(alimentName, proteins, calories, weight);
}
}
dish.addAliment(aliment);
}
case 2 -> {
System.out.println(dish);
for (Aliment aliment : dish.getAliments())
System.out.println(aliment);
}
default -> System.out.println("Invalid option.");
}
}
}
@Override
public String toString() {
StringBuilder string = new StringBuilder("Menu");
for (Weekday weekDay : this.menu.keySet())
string.append(String.format("%s : %s", weekDay, menu.get(weekDay)));
return string.toString();
}
}

View File

@ -0,0 +1,31 @@
package utils.Enums;
public enum Weekday {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
public static Weekday fromString(String string) {
return switch (string) {
case "MONDAY", "Monday", "monday" -> MONDAY;
case "TUESDAY", "Tuesday", "tuesday" -> TUESDAY;
case "WEDNESDAY", "Wednesday", "wednesday" -> WEDNESDAY;
case "THURSDAY", "Thursday", "thursday" -> THURSDAY;
case "FRIDAY", "Friday", "friday" -> FRIDAY;
case "SATURDAY", "Saturday", "saturday" -> SATURDAY;
case "SUNDAY", "Sunday", "sunday" -> SUNDAY;
default -> null;
};
}
@Override
public String toString() {
return switch (this) {
case MONDAY -> "MONDAY";
case TUESDAY -> "TUESDAY";
case WEDNESDAY -> "WEDNESDAY";
case THURSDAY -> "THURSDAY";
case FRIDAY -> "FRIDAY";
case SATURDAY -> "SATURDAY";
case SUNDAY -> "SUNDAY";
};
}
}