Advertisement
Guest User

Topping

a guest
Mar 7th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. package PizzaCalories;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class Topping {
  7.     private String toppingType;
  8.     private double weight;
  9.     private final Map toppingsModifiers = new HashMap<String, Double>(){{
  10.         put("Meat", 1.2);
  11.         put("Veggies", 0.8);
  12.         put("Cheese", 1.1);
  13.         put("Sauce", 0.9);
  14.     }};
  15.  
  16.     public Topping(String toppingType, double weight) {
  17.         setToppingType(toppingType);
  18.         setWeight(weight);
  19.     }
  20.  
  21.     private void setToppingType(String toppingType) {
  22.         if (!toppingsModifiers.containsKey(toppingType)) {
  23.             throw new IllegalArgumentException("Cannot place " + toppingType +" on top of your pizza.");
  24.         }
  25.  
  26.  
  27.         this.toppingType = toppingType;
  28.     }
  29.  
  30.     private void setWeight(double weight) {
  31.         if (weight < 1 && weight > 50) {
  32.             throw new IllegalArgumentException(toppingType + " weight should be in the range [1..50].");
  33.         }
  34.         this.weight = weight;
  35.     }
  36.  
  37.     public double calculateCalories() {
  38.         return (double)toppingsModifiers.get(this.toppingType) * (2 * this.weight);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement