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/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;
+}
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