diff --git a/pom.xml b/pom.xml index 3e9851b..411e522 100644 --- a/pom.xml +++ b/pom.xml @@ -20,5 +20,22 @@ + + + + junit + junit + 4.12 + test + + + junit + junit + 4.12 + compile + + + + \ No newline at end of file diff --git a/src/main/java/model/Pizza.java b/src/main/java/model/Pizza.java index 8096217..5606f7b 100644 --- a/src/main/java/model/Pizza.java +++ b/src/main/java/model/Pizza.java @@ -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; } diff --git a/src/main/java/test/PizzaTest.java b/src/main/java/test/PizzaTest.java new file mode 100644 index 0000000..1f79d4d --- /dev/null +++ b/src/main/java/test/PizzaTest.java @@ -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); + + } +}