svephoto

PizzaCalories: class Topping [Java]

Apr 10th, 2021
783
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×