Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PizzaCalories;
- import java.util.Arrays;
- public class Dough {
- private String flourType;
- private String bakingTechnique;
- private double weight;
- public Dough(String flourType, String bakingTechnique, double weight) {
- setFlourType(flourType);
- setBakingTechnique(bakingTechnique);
- setWeight(weight);
- }
- private void setFlourType(String flourType) {
- check(flourType);
- this.flourType = flourType;
- }
- private void setBakingTechnique(String bakingTechnique) {
- check(bakingTechnique);
- this.bakingTechnique = bakingTechnique;
- }
- private void check(String flourTypeOrBakingTechnique) {
- if (Arrays.stream(DoughModifiers.values()).noneMatch(m->m.name().equals(flourTypeOrBakingTechnique))) {
- throw new IllegalArgumentException("Invalid type of dough.");
- }
- }
- private void setWeight(double weight) {
- if (weight < 1 || weight > 200) {
- throw new IllegalArgumentException("Dough weight should be in the range [1..200].");
- }
- this.weight=weight;
- }
- public double calculateCalories() {
- return (2*weight)*(DoughModifiers.valueOf(flourType).getModifier())*(DoughModifiers.valueOf(bakingTechnique).getModifier());
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- public enum DoughModifiers {
- White(1.5),
- Wholegrain(1.0),
- Crispy(0.9),
- Chewy(1.1),
- Homemade(1.0);
- double modifier;
- DoughModifiers(double modifier) {
- this.modifier = modifier;
- }
- public double getModifier() {
- return modifier;
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- 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 numberOfToppings) {
- setName(name);
- setToppings(numberOfToppings);
- }
- private void setName(String name) {
- if (name == null || name.trim().isEmpty() || name.length() > 15) {
- throw new IllegalArgumentException("Pizza name should be between 1 and 15 symbols.");
- }
- this.name = name;
- }
- private void setToppings(int numberOfToppings) {
- if (numberOfToppings < 0 || numberOfToppings > 10) {
- throw new IllegalArgumentException("Number of toppings should be in range [0..10].");
- }
- toppings = new ArrayList<>();
- }
- public void setDough(Dough dough) {
- this.dough = dough;
- }
- public String getName() {
- return name;
- }
- public void addTopping(Topping topping) {
- toppings.add(topping);
- }
- public double getOverallCalories() {
- double caloriesAllToppings= toppings.stream().mapToDouble(Topping::calculateCalories).sum();
- return dough.calculateCalories()+caloriesAllToppings;
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- import java.util.Arrays;
- public class Topping {
- private String toppingType;
- private double weight;
- public Topping(String toppingType, double weight){
- setToppingType(toppingType);
- setWeight(weight);
- }
- private void setToppingType(String toppingType){
- if (Arrays.stream(ToppingModifiers.values()).noneMatch(m->m.name().equals(toppingType))) {
- throw new IllegalArgumentException(String.format
- ("Cannot place %s on top of your pizza.",toppingType));
- }
- this.toppingType=toppingType;
- }
- private void setWeight(double weight){
- if (weight < 1 || weight > 50) {
- throw new IllegalArgumentException(String.format("%s weight should be in the range [1..50].",toppingType));
- }
- this.weight=weight;
- }
- public double calculateCalories(){
- return 2*weight*ToppingModifiers.valueOf(toppingType).getModifier();
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- public enum ToppingModifiers {
- Meat (1.2),
- Veggies (0.8),
- Cheese (1.1),
- Sauce (0.9);
- double modifier;
- ToppingModifiers(double modifier) {
- this.modifier = modifier;
- }
- public double getModifier() {
- return modifier;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment