MiniMi2022

Pizza Calories

Mar 11th, 2022
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1. package PizzaCalories;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Dough {
  6.     private String flourType;
  7.     private String bakingTechnique;
  8.     private double weight;
  9.  
  10.     public Dough(String flourType, String bakingTechnique, double weight) {
  11.         setFlourType(flourType);
  12.         setBakingTechnique(bakingTechnique);
  13.         setWeight(weight);
  14.     }
  15.  
  16.     private void setFlourType(String flourType) {
  17.         check(flourType);
  18.         this.flourType = flourType;
  19.     }
  20.  
  21.     private void setBakingTechnique(String bakingTechnique) {
  22.         check(bakingTechnique);
  23.         this.bakingTechnique = bakingTechnique;
  24.     }
  25.  
  26.     private void check(String flourTypeOrBakingTechnique) {
  27.         if (Arrays.stream(DoughModifiers.values()).noneMatch(m->m.name().equals(flourTypeOrBakingTechnique))) {
  28.             throw new IllegalArgumentException("Invalid type of dough.");
  29.         }
  30.     }
  31.  
  32.     private void setWeight(double weight) {
  33.         if (weight < 1 || weight > 200) {
  34.             throw new IllegalArgumentException("Dough weight should be in the range [1..200].");
  35.         }
  36.         this.weight=weight;
  37.     }
  38.  
  39.     public double calculateCalories() {
  40.         return (2*weight)*(DoughModifiers.valueOf(flourType).getModifier())*(DoughModifiers.valueOf(bakingTechnique).getModifier());
  41.     }
  42.  
  43. }
  44. ---------------------------------------------------------------------------------------------------------------------------------------
  45. public enum DoughModifiers {
  46.     White(1.5),
  47.     Wholegrain(1.0),
  48.     Crispy(0.9),
  49.     Chewy(1.1),
  50.     Homemade(1.0);
  51.     double modifier;
  52.  
  53.     DoughModifiers(double modifier) {
  54.         this.modifier = modifier;
  55.     }
  56.  
  57.     public double getModifier() {
  58.         return modifier;
  59.     }
  60. }
  61. ---------------------------------------------------------------------------------------------------------------------------------------
  62. import java.util.ArrayList;
  63. import java.util.List;
  64.  
  65. public class Pizza {
  66.     private String name;
  67.     private Dough dough;
  68.     private List<Topping> toppings;
  69.  
  70.     public Pizza(String name, int numberOfToppings) {
  71.         setName(name);
  72.         setToppings(numberOfToppings);
  73.     }
  74.  
  75.     private void setName(String name) {
  76.         if (name == null || name.trim().isEmpty() || name.length() > 15) {
  77.             throw new IllegalArgumentException("Pizza name should be between 1 and 15 symbols.");
  78.  
  79.         }
  80.         this.name = name;
  81.     }
  82.  
  83.     private void setToppings(int numberOfToppings) {
  84.         if (numberOfToppings < 0 || numberOfToppings > 10) {
  85.             throw new IllegalArgumentException("Number of toppings should be in range [0..10].");
  86.         }
  87.         toppings = new ArrayList<>();
  88.     }
  89.  
  90.     public void setDough(Dough dough) {
  91.         this.dough = dough;
  92.     }
  93.  
  94.     public String getName() {
  95.         return name;
  96.     }
  97.  
  98.     public void addTopping(Topping topping) {
  99.         toppings.add(topping);
  100.     }
  101.  
  102.     public double getOverallCalories() {
  103.         double caloriesAllToppings= toppings.stream().mapToDouble(Topping::calculateCalories).sum();
  104.         return dough.calculateCalories()+caloriesAllToppings;
  105.     }
  106. }
  107. ---------------------------------------------------------------------------------------------------------------------------------------
  108. import java.util.Arrays;
  109.  
  110. public class Topping {
  111.     private String toppingType;
  112.     private double weight;
  113.  
  114.     public Topping(String toppingType, double weight){
  115.         setToppingType(toppingType);
  116.         setWeight(weight);
  117.     }
  118.  
  119.     private void setToppingType(String toppingType){
  120.         if (Arrays.stream(ToppingModifiers.values()).noneMatch(m->m.name().equals(toppingType))) {
  121.             throw new IllegalArgumentException(String.format
  122.                     ("Cannot place %s on top of your pizza.",toppingType));
  123.         }
  124.         this.toppingType=toppingType;
  125.     }
  126.  
  127.     private void setWeight(double weight){
  128.  
  129.         if (weight < 1 || weight > 50) {
  130.             throw new IllegalArgumentException(String.format("%s weight should be in the range [1..50].",toppingType));
  131.         }
  132.         this.weight=weight;
  133.     }
  134.  
  135.     public double calculateCalories(){
  136.         return 2*weight*ToppingModifiers.valueOf(toppingType).getModifier();
  137.     }
  138. }
  139. ---------------------------------------------------------------------------------------------------------------------------------------
  140. public enum ToppingModifiers {
  141.     Meat (1.2),
  142.     Veggies (0.8),
  143.     Cheese (1.1),
  144.     Sauce (0.9);
  145.     double modifier;
  146.  
  147.     ToppingModifiers(double modifier) {
  148.         this.modifier = modifier;
  149.     }
  150.  
  151.     public double getModifier() {
  152.         return modifier;
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment