From bafd78cf97940747c6e07c94263ac3b50e3bef18 Mon Sep 17 00:00:00 2001 From: matveymaletskov Date: Fri, 21 Jun 2019 19:05:40 +0300 Subject: [PATCH 1/2] refactor --- src/main/java/PizzaOrder.java | 4 + src/main/java/controller/CliController.java | 2 +- .../{model => exception}/PizzaException.java | 2 +- .../StorageException.java | 2 +- src/main/java/model/DefaultStorage.java | 75 ------------------- src/main/java/model/Pizza.java | 2 + src/main/java/model/Storage.java | 15 ---- src/main/java/storage/DefaultStorage.java | 45 +++++++++++ src/main/java/storage/Storage.java | 19 +++++ 9 files changed, 73 insertions(+), 93 deletions(-) rename src/main/java/{model => exception}/PizzaException.java (94%) rename src/main/java/{model => exception}/StorageException.java (94%) delete mode 100644 src/main/java/model/DefaultStorage.java delete mode 100644 src/main/java/model/Storage.java create mode 100644 src/main/java/storage/DefaultStorage.java create mode 100644 src/main/java/storage/Storage.java diff --git a/src/main/java/PizzaOrder.java b/src/main/java/PizzaOrder.java index b24274d..615f736 100644 --- a/src/main/java/PizzaOrder.java +++ b/src/main/java/PizzaOrder.java @@ -1,5 +1,9 @@ import controller.CliController; +import exception.PizzaException; +import exception.StorageException; +import storage.DefaultStorage; import model.*; +import storage.Storage; import java.util.Scanner; diff --git a/src/main/java/controller/CliController.java b/src/main/java/controller/CliController.java index 2414ab0..a4f6724 100644 --- a/src/main/java/controller/CliController.java +++ b/src/main/java/controller/CliController.java @@ -42,7 +42,7 @@ public static void printHelpMessage() { "заказанной пиццы)."); } - public static void printStoreMessage(HashMap ingredients) { + public static void printStoreMessage(Map ingredients) { if (ingredients != null && ingredients.size() > 0) { StringBuilder message = new StringBuilder( "На данный момент в хранилище имеются следующие ингриденты:\n"); diff --git a/src/main/java/model/PizzaException.java b/src/main/java/exception/PizzaException.java similarity index 94% rename from src/main/java/model/PizzaException.java rename to src/main/java/exception/PizzaException.java index 70d1fa6..0ceef3e 100644 --- a/src/main/java/model/PizzaException.java +++ b/src/main/java/exception/PizzaException.java @@ -1,4 +1,4 @@ -package model; +package exception; public class PizzaException extends Exception { diff --git a/src/main/java/model/StorageException.java b/src/main/java/exception/StorageException.java similarity index 94% rename from src/main/java/model/StorageException.java rename to src/main/java/exception/StorageException.java index 072c3f1..547ad8d 100644 --- a/src/main/java/model/StorageException.java +++ b/src/main/java/exception/StorageException.java @@ -1,4 +1,4 @@ -package model; +package exception; public class StorageException extends Exception { diff --git a/src/main/java/model/DefaultStorage.java b/src/main/java/model/DefaultStorage.java deleted file mode 100644 index cc60d79..0000000 --- a/src/main/java/model/DefaultStorage.java +++ /dev/null @@ -1,75 +0,0 @@ -package model; - -import java.util.HashMap; - -public class DefaultStorage extends Storage { - - private HashMap ingredients; - - public DefaultStorage() { - initIngredients(); - } - - @Override - public void addIngredient( - Ingredient ingredient, - int count - ) throws StorageException { - if (!ingredientsNotNull()) - initIngredients(); - if (ingredient != null && count > 0) { - HashMap ingredients = getStorageIngredients(); - // Если ингредиент уже существует - if (ingredients.containsKey(ingredient)) { - int alreadyExistIngredientCount = ingredients.get( - ingredient); - ingredients.put( - ingredient, alreadyExistIngredientCount + count); - // Если не существует - } else - ingredients.put(ingredient, count); - } else - throw new StorageException( - "Введён некорректный ингредиент или его количество"); - } - - @Override - public void removeIngredient(Ingredient ingredient, int count) - throws StorageException { - if (!ingredientsNotNull()) - initIngredients(); - if (ingredient != null && count > 0) { - HashMap ingredients = getStorageIngredients(); - // Если ингридиент уже существует - 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 StorageException( - "Вы пытаетесь удалить несуществующий ингридиент в хранилище"); - } else - throw new StorageException( - "Введён некорректный ингридиент или его количество"); - } - - @Override - public HashMap getStorageIngredients() { - return ingredients; - } - - private boolean ingredientsNotNull() { - return ingredients != null; - } - - private void initIngredients() { - ingredients = new HashMap<>(); - } -} diff --git a/src/main/java/model/Pizza.java b/src/main/java/model/Pizza.java index 8096217..46b1cc4 100644 --- a/src/main/java/model/Pizza.java +++ b/src/main/java/model/Pizza.java @@ -1,5 +1,7 @@ package model; +import exception.PizzaException; + import java.util.HashMap; /** diff --git a/src/main/java/model/Storage.java b/src/main/java/model/Storage.java deleted file mode 100644 index 2e3c8a9..0000000 --- a/src/main/java/model/Storage.java +++ /dev/null @@ -1,15 +0,0 @@ -package model; - -import java.util.HashMap; - -public abstract class Storage { - - public abstract void addIngredient(Ingredient ingredient, int count) - throws StorageException; - - public abstract HashMap getStorageIngredients() - throws StorageException; - - public abstract void removeIngredient(Ingredient ingredient, int count) - throws StorageException; -} diff --git a/src/main/java/storage/DefaultStorage.java b/src/main/java/storage/DefaultStorage.java new file mode 100644 index 0000000..dcf93dc --- /dev/null +++ b/src/main/java/storage/DefaultStorage.java @@ -0,0 +1,45 @@ +package storage; + +import model.Ingredient; +import exception.StorageException; + +import java.util.HashMap; +import java.util.Map; + +public class DefaultStorage extends Storage { + private Map ingredients = new HashMap<>(); + + @Override + public void addIngredient(Ingredient ingredient, int count) throws StorageException { + if (ingredient == null || count <= 0) { + throw new StorageException("Введён некорректный ингредиент или его количество"); + } + if (ingredients.containsKey(ingredient)) { + ingredients.put(ingredient, ingredients.get(ingredient) + count); + } else { + ingredients.put(ingredient, count); + } + } + + @Override + public void removeIngredient(Ingredient ingredient, int count) throws StorageException { + if (ingredient == null || count <= 0) { + throw new StorageException("Введён некорректный ингридиент или его количество"); + } + if (ingredients.containsKey(ingredient)) { + int alreadyExistIngredientCount = ingredients.get(ingredient); + if (count >= alreadyExistIngredientCount) { + ingredients.remove(ingredient); + } else { + ingredients.put(ingredient, alreadyExistIngredientCount - count); + } + } else { + throw new StorageException("Вы пытаетесь удалить несуществующий ингридиент в хранилище"); + } + } + + @Override + public Map getStorageIngredients() { + return ingredients; + } +} diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java new file mode 100644 index 0000000..749a3c7 --- /dev/null +++ b/src/main/java/storage/Storage.java @@ -0,0 +1,19 @@ +package storage; + +import exception.StorageException; +import model.Ingredient; + +import java.util.HashMap; +import java.util.Map; + +public abstract class Storage { + + public abstract void addIngredient(Ingredient ingredient, int count) + throws StorageException; + + public abstract Map getStorageIngredients() + throws StorageException; + + public abstract void removeIngredient(Ingredient ingredient, int count) + throws StorageException; +} From 114f7749f66d40400a5762ff3d5a05e8313c0aa8 Mon Sep 17 00:00:00 2001 From: matveymaletskov Date: Mon, 24 Jun 2019 19:34:27 +0300 Subject: [PATCH 2/2] added unit tests for DafaultStorage --- pom.xml | 8 ++++ src/test/java/storage/DefaultStorageTest.java | 41 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/test/java/storage/DefaultStorageTest.java diff --git a/pom.xml b/pom.xml index 3e9851b..62a37ae 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,14 @@ + + + junit + junit + 4.12 + test + + \ No newline at end of file diff --git a/src/test/java/storage/DefaultStorageTest.java b/src/test/java/storage/DefaultStorageTest.java new file mode 100644 index 0000000..82c08e0 --- /dev/null +++ b/src/test/java/storage/DefaultStorageTest.java @@ -0,0 +1,41 @@ +package storage; + +import exception.StorageException; +import model.Ingredient; +import org.junit.*; + + +public class DefaultStorageTest { + + private Storage storage; + private Ingredient testIngredient; + + @Before + public void setUp() { + storage = new DefaultStorage(); + testIngredient = new Ingredient("Cucumber"); + } + + @Test + public void addNotExistIngredient() throws StorageException { + storage.addIngredient(testIngredient, 2); + + Assert.assertEquals((Integer)2, storage.getStorageIngredients().get(testIngredient)); + } + + @Test + public void removeIngredientNotNull() throws StorageException { + storage.addIngredient(testIngredient, 2); + storage.removeIngredient(testIngredient, 1); + + Assert.assertEquals((Integer)1, storage.getStorageIngredients().get(testIngredient)); + } + + @Test + public void removeIngredientNull() throws StorageException { + storage.addIngredient(testIngredient, 2); + storage.removeIngredient(testIngredient, 2); + + Assert.assertNull(storage.getStorageIngredients().get(testIngredient)); + } +} \ No newline at end of file