Loading...
Olegati avatar Olegati 7 Точки

04. Pizza Calories 91/100

Здравейте, 

На задача 4ри от Encapsulation - Exercises 6тият тест отказва да мине. Предполагам, че проблемът е в някой метод, но не виждам грешката.

Линк към класовете в GitHub -> https://github.com/OKuzmanov/Software-University-SoftUni/tree/main/Java-OOP/src/encapsulation/exercises/pizzaCalories

Линк към Judge -> https://judge.softuni.org/Contests/Compete/Index/1536#3;


Условието: 

Problem 4. Pizza Calories

A Pizza is made of a dough and different toppings. You should model a class Pizza which should have a name, dough and toppings as fields. Every type of ingredient should have its own class.

Pizza

-

name: String

-

dought:  Dough

-

toppings: List<Topping>

+

Piza (String, int numberOfToppings)

-

setToppings(int) : void

-

setName(String) : void

+

setDough(Dough) : void

+

getName(): String

+

addTopping (Topping) : void

+

getOverallCalories () : double

 

Every ingredient has different fields: the dough can be white or wholegrain and in addition it can be crispy, chewy or homemade. The toppings can be of type meat, veggies, cheese or sauce. Every ingredient should have a weight in grams and a method for calculating its calories according its type. Calories per gram are calculated through modifiers. Every ingredient has 2 calories per gram as a base and a modifier that gives the exact calories.

 

 

Dough

-

flourType: String

-

bakingTechnique: String

-

weight: double

+

Dought (String, String, double)

-

setWeight(double): void

-

setFlourType(String): void

-

setBakingTechnique(String): void

+

calculateCalories (): double

Topping

-

toppingType: String

-

weight: double

+

Topping (String, double)

-

setToppingType (String): void

-

setWeight (double): void

+

calculateCalories (): double

 

 

Your job is to model the classes in such a way that they are properly encapsulated and to provide a public method for every pizza that calculates its calories according to the ingredients it has.

Dough Modifiers

Toppings Modifiers

  • White – 1.5;
  • Wholegrain – 1.0;
  • Crispy – 0.9;
  • Chewy – 1.1;
  • Homemade – 1.0;
  • Meat – 1.2;
  • Veggies – 0.8;
  • Cheese – 1.1;
  • Sauce – 0.9;

 

For example, white dough has a modifier of 1.5, a chewy dough has a modifier of 1.1, which means that a white chewy dough weighting 100 grams will have (2 * 100) * 1.5 * 1.1 = 330.00 total calories.

For example, meat has a modifier of 1.2, which means that a meat weighting 50 grams will have (2 * 50) * 1.2 = 120.00 total calories.

Data Validation

Data Validation must be in the order of the Input Data.

  • If invalid flour type or an invalid baking technique is given an exception is thrown with the message "Invalid type of dough.".
  • If dough weight is outside of range [1..200] throw an exception with the message "Dough weight should be in the range [1..200]."
  • If topping is not one of the provided types throw an exception with the message "Cannot place {name of invalid argument} on top of your pizza."
  • If topping weight is outside of range [1..50] throw an exception with the message "{Topping type name} weight should be in the range [1..50].".
  • If name of the pizza is empty, only whitespace or longer than 15 symbols throw an exception with the message "Pizza name should be between 1 and 15 symbols.".
  • If number of topping is outside of range [0..10] throw an exception with the message "Number of toppings should be in range [0..10].".

The input for a pizza consists of several lines:

  • On the first line is the pizza name and the number of toppings it has in format:
    • Pizza {pizzaName} {numberOfToppings}
  • On the second line you will get input for the dough in format:
    • Dough {flourType} {bakingTechnique} {weightInGrams}
  • On the next lines, you will receive every topping the pizza has, until an"END" command is given:
    • Topping {toppingType} {weightInGrams}

If creation of the pizza was successful print on a single line the name of the pizza and the total calories it has, rounded to the second digit after the decimal point.

Examples

Input

Output

Pizza Meatless 2

Dough Wholegrain Crispy 100

Topping Veggies 50

Topping Cheese 50

END

Meatless - 370.00

Pizza Bulgarian 20

Dough Type500 Bulgarian 100

Topping Cheese 50

Topping Cheese 50

Topping Salami 20

Topping Meat 10

END

Number of toppings should be in range [0..10].

Pizza Bulgarian 2

Dough Type500 Bulgarian 100

Topping Cheese 50

Topping Cheese 50

Topping Salami 20

Topping Meat 10

END

Invalid type of dough.

Pizza Bulgarian 2

Dough White Chewy 100

Topping Parmesan 50

Topping Cheese 50

Topping Salami 20

Topping Meat 10

END

Cannot place Parmesan on top of your pizza.

Тагове:
0
Java OOP Advanced
Olegati avatar Olegati 7 Точки

Открих си грешката, била е в методите setWeight() и setToppingType(). Накрая на стринг форматърите съм сложил допълнително %n, а принтирам съобщението от грешката в Main с println, следователно се получават два нови реда вместо един и Judge дава грешка.

Грешно:

private void setWeight(double weight) {
        if (weight > 0 && weight <= 50) {
            this.weight = weight;
        } else {
            throw new IllegalArgumentException(String.format("%s weight should be in the range [1..50].%n", this.toppingType));
        }
    }

    private void setToppingType(String toppingType) {
        if (!toppingType.equals("Meat") && !toppingType.equals("Veggies") &&
                !toppingType.equals("Cheese") && !toppingType.equals("Sauce")) {
            throw new IllegalArgumentException(String.format("Cannot place %s on top of your pizza.%n", toppingType));
        } else {
            this.toppingType = toppingType;
        }
    }

Правилно: 

private void setWeight(double weight) {
        if (weight > 0 && weight <= 50) {
            this.weight = weight;
        } else {
            throw new IllegalArgumentException(String.format("%s weight should be in the range [1..50].", this.toppingType));
        }
    }

    private void setToppingType(String toppingType) {
        if (!toppingType.equals("Meat") && !toppingType.equals("Veggies") &&
                !toppingType.equals("Cheese") && !toppingType.equals("Sauce")) {
            throw new IllegalArgumentException(String.format("Cannot place %s on top of your pizza.", toppingType));
        } else {
            this.toppingType = toppingType;
        }
    }

 

2
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.