Advertisement
Guest User

Pizza

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