Advertisement
svephoto

PizzaCalories: class Topping [Java]

Apr 10th, 2021
1,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. package pizzacalories;
  2.  
  3. public class Topping {
  4.     private String toppingType;
  5.     private double weight;
  6.  
  7.     public Topping(String toppingType, double weight) {
  8.         this.setToppingType(toppingType);
  9.         this.setWeight(weight);
  10.     }
  11.  
  12.     private void setToppingType(String toppingType) {
  13.         ValidatorUtil.validateToppingType(toppingType);
  14.         this.toppingType = toppingType;
  15.     }
  16.  
  17.     private void setWeight(double weight) {
  18.         ValidatorUtil.validateToppingWeight(this.toppingType, weight);
  19.         this.weight = weight;
  20.     }
  21.  
  22.     public double calculateCalories() {
  23.         double calories = this.weight * 2;
  24.  
  25.         switch (this.toppingType) {
  26.             case "Meat":
  27.                 calories *= 1.2;
  28.                 break;
  29.             case "Veggies":
  30.                 calories *= 0.8;
  31.                 break;
  32.             case "Cheese":
  33.                 calories *= 1.1;
  34.                 break;
  35.             case "Sauce":
  36.                 calories *= 0.9;
  37.                 break;
  38.         }
  39.  
  40.         return calories;
  41.     }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement