Advertisement
svephoto

PizzaCalories: class Pizza [Java]

Apr 10th, 2021
1,395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 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.     private int numberOfToppings;
  11.  
  12.     public Pizza(String name, int numberOfToppings) {
  13.         this.setName(name);
  14.         this.dough = null;
  15.         this.setToppings(numberOfToppings);
  16.         this.toppings = new ArrayList<>(this.numberOfToppings);
  17.     }
  18.  
  19.     private void setName(String name) {
  20.         ValidatorUtil.validatePizzaName(name);
  21.         this.name = name;
  22.     }
  23.  
  24.     public String getName() {
  25.         return this.name;
  26.     }
  27.  
  28.     private void setToppings(int numberOfToppings) {
  29.         ValidatorUtil.validateNumberOfToppings(numberOfToppings);
  30.         this.numberOfToppings = numberOfToppings;
  31.     }
  32.  
  33.     public void setDough(Dough dough) {
  34.         this.dough = dough;
  35.     }
  36.  
  37.     public void addTopping(Topping topping) {
  38.         if (this.toppings.size() < this.numberOfToppings) {
  39.             this.toppings.add(topping);
  40.         }
  41.     }
  42.  
  43.     public double getOverallCalories() {
  44.         return this.dough.calculateCalories()
  45.                 + this.toppings.stream()
  46.                 .mapToDouble(Topping::calculateCalories)
  47.                 .sum();
  48.     }
  49.  
  50.     @Override
  51.     public String toString() {
  52.         return String.format("%s - %.2f", this.getName(), this.getOverallCalories());
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement