diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0f30089 --- /dev/null +++ b/.gitignore @@ -0,0 +1,96 @@ +# Created by .ignore support plugin (hsz.mobi) +### Example user template template +### Example user template + +# IntelliJ project files +.idea +*.iml +out +gen +### Android template +# Built application files +*.apk +*.ap_ +*.aab + +# Files for the ART/Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ +out/ +release/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ +.idea/workspace.xml +.idea/tasks.xml +.idea/gradle.xml +.idea/assetWizardSettings.xml +.idea/dictionaries +.idea/libraries +# Android Studio 3 in .gitignore file. +.idea/caches +.idea/modules.xml +# Comment next line if keeping position of elements in Navigation Editor is relevant for you +.idea/navEditor.xml + +# Keystore files +# Uncomment the following lines if you do not want to check your keystore files in. +#*.jks +#*.keystore + +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild + +# Google Services (e.g. APIs or Firebase) +# google-services.json + +# Freeline +freeline.py +freeline/ +freeline_project_description.json + +# fastlane +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots +fastlane/test_output +fastlane/readme.md + +# Version control +vcs.xml + +# lint +lint/intermediates/ +lint/generated/ +lint/outputs/ +lint/tmp/ +# lint/reports/ + +### AppEngine template +# Google App Engine generated folder +appengine-generated/ + diff --git a/pizza-order-training.iml b/pizza-order-training.iml new file mode 100644 index 0000000..54764a2 --- /dev/null +++ b/pizza-order-training.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3e9851b..c23b3d2 100644 --- a/pom.xml +++ b/pom.xml @@ -7,14 +7,43 @@ com.simbirsoft pizza-order-training 1.0-SNAPSHOT + + + junit + junit + 4.12 + test + + + org.junit.jupiter + junit-jupiter-api + 5.3.2 + compile + + + org.junit.jupiter + junit-jupiter-api + 5.3.2 + + + org.junit.jupiter + junit-jupiter-api + 5.3.2 + + + org.junit.jupiter + junit-jupiter-api + 5.3.2 + + org.apache.maven.plugins maven-compiler-plugin - 1.7 - 1.7 + 8 + 8 diff --git a/src/main/java/PizzaOrdertests.java b/src/main/java/PizzaOrdertests.java new file mode 100644 index 0000000..03f37bf --- /dev/null +++ b/src/main/java/PizzaOrdertests.java @@ -0,0 +1,45 @@ +import model.Ingredient; +import model.Pizza; +import model.PizzaException; +import org.junit.*; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; + +public class PizzaOrdertests { + Pizza pizza; + Ingredient ingredient; + @BeforeEach + public void prepare(){ + pizza = new Pizza(50,new HashMap()); + ingredient = new Ingredient("cheeeeess"); + } + @Test() + public void addIngredient_ShouldCreatePizzs_Sucksessfull() throws PizzaException { + pizza.addIngredient(ingredient,3); + int k = pizza.getIngredients().get(ingredient); + Assertions.assertEquals(3,k); + } + + @Test() + public void addIngredient_ShouldGenerateException() throws PizzaException{ + Assertions.assertThrows(PizzaException.class,()->pizza.addIngredient(ingredient, -5)); + } + @Test() + public void removeIngredient_ShouldGenerateException() throws PizzaException{ + Assertions.assertThrows(PizzaException.class,()->pizza.removeIngredient(ingredient, -5)); + } + @AfterEach + public void after(){ + + } + /*@Test() + public void removeIngredient_ShouldSu throws PizzaException{ + Pizza pizza = new Pizza(50,new HashMap()); + Ingredient ingredient = new Ingredient("cheeeeess"); + Assertions.assertThrows(PizzaException.class,()->pizza.removeIngredient(ingredient, -5)); + }*/ +} diff --git a/src/main/java/model/Pizza.java b/src/main/java/model/Pizza.java index 8096217..a93e558 100644 --- a/src/main/java/model/Pizza.java +++ b/src/main/java/model/Pizza.java @@ -16,16 +16,13 @@ public Pizza() { } public Pizza( - int size, HashMap ingredients + int size, HashMap ingredients ) { this.size = size; this.ingredients = ingredients; } - public void addIngredient( - Ingredient ingredient, - int count - ) throws PizzaException { + public void addIngredient(Ingredient ingredient, int count) throws PizzaException { if (ingredient != null && count > 0) { // Проверка на существование коллекции if (!ingredientsCollectionNotNull()) @@ -33,44 +30,46 @@ public void addIngredient( // Если элемент уже существует if (ingredients.containsKey(ingredient)) { int alreadyExistIngredientCount = ingredients.get( - ingredient); + ingredient); ingredients.put( - ingredient, alreadyExistIngredientCount + count); - // Если не существует + ingredient, alreadyExistIngredientCount + count); + // Если не существует } else { ingredients.put(ingredient, count); } } else throw new PizzaException( - "Указан некорректный ингредиент или его количество"); + "Указан некорректный ингредиент или его количество"); } - public void removeIngredient( - Ingredient ingredient, - int count - ) throws PizzaException { + public void removeIngredient(Ingredient ingredient, int count) throws PizzaException { if (ingredient != null && count > 0) { - // Проверка на существование коллекции - if (!ingredientsCollectionNotNull()) - initIngredientsCollection(); + checkIngredientsCollection(); // Если элемент уже существует - if (ingredients.containsKey(ingredient)) { - int alreadyExistIngredientCount = ingredients.get( - ingredient); - // Если количество уже существующих элементов больше либо - // равно количеству удаляемых - if (Math.abs(count) >= alreadyExistIngredientCount) - ingredients.remove(ingredient); - else - ingredients.put( + } else + throw new PizzaException( + "Указан некорректный ингредиент или его количество"); + if (ingredients.containsKey(ingredient)) { + int alreadyExistIngredientCount = ingredients.get(ingredient); + int countMinusExist = count - alreadyExistIngredientCount; + if (countMinusExist == 0) { + ingredients.remove(ingredient); + } else if (countMinusExist < 0) { + ingredients.put( ingredient, alreadyExistIngredientCount - count); - // Если элемента не сущетсвует - } else - throw new PizzaException( - "Вы пытаетесь удалить ингредиент, который не существует в пицце"); + } else { + throw new PizzaException( + "Вы пытаетесь удалить большее количество ингредиента, чем содержится в пицце"); + } + // Если элемента не сущетсвует } else throw new PizzaException( - "Указан некорректный ингредиент или его количество"); + "Вы пытаетесь удалить ингредиент, который не существует в пицце"); + } + + private void checkIngredientsCollection() { + if (!ingredientsCollectionNotNull()) + initIngredientsCollection(); } private boolean ingredientsCollectionNotNull() { @@ -81,7 +80,7 @@ private void initIngredientsCollection() { ingredients = new HashMap(); } - // -- Getters & Setters -- +// -- Getters & Setters -- public int getSize() { return size; @@ -96,7 +95,7 @@ public HashMap getIngredients() { } public void setIngredients( - HashMap ingredients + HashMap ingredients ) { this.ingredients = ingredients; }