Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PizzaCalories;
- import java.util.ArrayList;
- import java.util.List;
- public class Pizza {
- private String name;
- private Dough dough;
- private List<Topping> toppings;
- public Pizza(String name, int toppings) {
- setName(name);
- setToppings(toppings);
- }
- private void setName(String name) {
- if (name.isEmpty() || name.length() > 15) {
- throw new IllegalArgumentException("Pizza name should be between 1 and 15 symbols.");
- }
- this.name = name;
- }
- public void setDough(Dough dough) {
- this.dough = dough;
- }
- public void addTopping(Topping topping) {
- this.toppings.add(topping);
- }
- private void setToppings(int toppings) {
- if (toppings > 10) {
- throw new IllegalArgumentException("Number of toppings should be in range [1..10].");
- }
- this.toppings = new ArrayList<>(toppings);
- }
- public double getOverallCalories() {
- double calories = 0;
- calories += this.dough.calculateCalories();
- for (Topping topping : this.toppings) {
- calories += topping.calculateCalories();
- }
- return calories;
- }
- }
Add Comment
Please, Sign In to add comment