Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,22 @@
</plugins>
</build>

<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>

</dependencies>


</project>
50 changes: 23 additions & 27 deletions src/main/java/model/Pizza.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,31 @@ public void addIngredient(
"Указан некорректный ингредиент или его количество");
}

public void removeIngredient(
Ingredient ingredient,
int count
) throws PizzaException {
if (ingredient != null && count > 0) {
// Проверка на существование коллекции
if (!ingredientsCollectionNotNull())
initIngredientsCollection();
// Если элемент уже существует
if (ingredients.containsKey(ingredient)) {
int alreadyExistIngredientCount = ingredients.get(
ingredient);
// Если количество уже существующих элементов больше либо
// равно количеству удаляемых
if (Math.abs(count) >= alreadyExistIngredientCount)
ingredients.remove(ingredient);
else
ingredients.put(
ingredient, alreadyExistIngredientCount - count);
// Если элемента не сущетсвует
} else
throw new PizzaException(
"Вы пытаетесь удалить ингредиент, который не существует в пицце");
} else
throw new PizzaException(
"Указан некорректный ингредиент или его количество");
public void removeIngredient(Ingredient ingredient, int count) throws PizzaException {
if(ingredient == null || count <=0){
throw new PizzaException("Указан некорректный ингредиент или его количество");
}

if (!ingredientsCollectionNotNull()) {
initIngredientsCollection();
}
int alreadyExistIngredientCount;

try {
alreadyExistIngredientCount = ingredients.get(ingredient);
} catch (NullPointerException e) {
throw new PizzaException("Вы пытаетесь удалить ингредиент, который не существует в пицце");
}

if(count < alreadyExistIngredientCount){
ingredients.put(ingredient, alreadyExistIngredientCount - count);
}
else {
ingredients.remove(ingredient);
}
}

private boolean ingredientsCollectionNotNull() {
private boolean ingredientsCollectionNotNull(){
return ingredients != null;
}

Expand Down
36 changes: 36 additions & 0 deletions src/main/java/test/PizzaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package test;


import model.Ingredient;
import model.PizzaException;
import org.junit.*;

import model.Pizza;

public class PizzaTest {

@Test(expected = PizzaException.class)
public void removeIngredientWhenItNull() throws PizzaException {
Pizza pizza = new Pizza();
pizza.removeIngredient(new Ingredient(""), 1);
}


@Test(expected = PizzaException.class)
public void addIngredientWhenItExists() throws PizzaException{
Ingredient ingredient = null;
Pizza pizza = new Pizza();

pizza.addIngredient(ingredient, 10);

}

@Test
public void addIngredientShouldIncreaseIngredientCount() throws PizzaException{
Pizza pizza = new Pizza();

pizza.addIngredient(new Ingredient(""), 10);
Assert.assertEquals(pizza.getIngredients().size(), 1);

}
}