Guest User

Topping

a guest
Mar 9th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. package PizzaCalories2;
  2.  
  3. public class Topping {
  4.     private String toppingType; // meat, veggies, cheese or sauce
  5.     private double weight;
  6.  
  7.     public Topping(String toppingType, double weight) {
  8.         setToppingType(toppingType);
  9.         setWeight(weight);
  10.     }
  11.  
  12.     private void setToppingType(String toppingType) {
  13.         if (toppingType.isEmpty()) {
  14.             throw new IllegalArgumentException("Invalid topping input.");
  15.         }
  16.  
  17.         if (!toppingType.equals("Meat") && !toppingType.equals("Veggies")
  18.                 && !toppingType.equals("Cheese") && !toppingType.equals("Sauce")) {
  19.             throw new IllegalArgumentException("Cannot place " + toppingType + " on top of your pizza.");
  20.         }
  21.  
  22.  
  23.         this.toppingType = toppingType;
  24.     }
  25.  
  26.     private void setWeight(double weight) {
  27.         if (weight < 1.0 || weight > 50.0) {
  28.             throw new IllegalArgumentException(this.toppingType + " weight should be in the range [1..50].");
  29.         }
  30.         this.weight = weight;
  31.     }
  32.  
  33.     public double calculateCalories() {
  34.         double calories = 0;
  35.  
  36.         switch (this.toppingType) {
  37.             case "Meat":
  38.                 calories = (this.weight * 2) * 1.2;
  39.                 break;
  40.             case "Veggies":
  41.                 calories = (this.weight * 2) * 0.8;
  42.                 break;
  43.             case "Cheese":
  44.                 calories = (this.weight * 2) * 1.1;
  45.                 break;
  46.             case "Sauce":
  47.                 calories = (this.weight * 2) * 0.9;
  48.                 break;
  49.             default:
  50.                 break;
  51.         }
  52.  
  53.         return calories;
  54.     }
  55.  
  56. }
Add Comment
Please, Sign In to add comment