Guest User

Pizza

a guest
Mar 7th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. package PizzaCalories;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class Pizza {
  7.     private String name;
  8.     private Dough dough;
  9.     private List<Topping> toppings;
  10.  
  11.     public Pizza(String name, int toppings) {
  12.         setName(name);
  13.         setToppings(toppings);
  14.     }
  15.  
  16.     private void setName(String name) {
  17.         if (name.isEmpty() || name.length() > 15) {
  18.             throw new IllegalArgumentException("Pizza name should be between 1 and 15 symbols.");
  19.         }
  20.         this.name = name;
  21.     }
  22.  
  23.     public void setDough(Dough dough) {
  24.         this.dough = dough;
  25.     }
  26.  
  27.     public void addTopping(Topping topping) {
  28.         this.toppings.add(topping);
  29.     }
  30.  
  31.  
  32.     private void setToppings(int toppings) {
  33.         if (toppings > 10) {
  34.             throw new IllegalArgumentException("Number of toppings should be in range [1..10].");
  35.         }
  36.         this.toppings = new ArrayList<>(toppings);
  37.     }
  38.  
  39.     public double getOverallCalories() {
  40.         double calories = 0;
  41.         calories += this.dough.calculateCalories();
  42.         for (Topping topping : this.toppings) {
  43.             calories += topping.calculateCalories();
  44.         }
  45.  
  46.         return calories;
  47.     }
  48. }
Add Comment
Please, Sign In to add comment