diff --git a/.gitignore b/.gitignore index 23841a6..3fad757 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,9 @@ .idea .gradle +.classpath +.project +.settings +bin/ +!**/src/main/**/build/ +!**/src/test/**/build/ +build/ \ No newline at end of file diff --git a/build.gradle b/build.gradle index e9a013a..03aa0b7 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,16 @@ repositories { dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + + compileOnly 'org.projectlombok:lombok:1.18.30' + annotationProcessor 'org.projectlombok:lombok:1.18.30' + + testCompileOnly 'org.projectlombok:lombok:1.18.30' + testAnnotationProcessor 'org.projectlombok:lombok:1.18.30' + + implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.9' + implementation group: 'org.slf4j', name: 'slf4j-simple', version: '2.0.9' } test { diff --git a/src/main/java/task_2/OlivandersShop.java b/src/main/java/task_2/OlivandersShop.java new file mode 100644 index 0000000..efc2ea0 --- /dev/null +++ b/src/main/java/task_2/OlivandersShop.java @@ -0,0 +1,74 @@ +package task_2; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import task_2.exceptions.NotEnoughMaterialException; +import task_2.exceptions.NotEnoughWoodException; +import task_2.exceptions.OrdersListIsEmptyException; + +@Slf4j +@Getter +public class OlivandersShop { + + private final Map woodTypeCount = new HashMap<>(); + private final Map coreMaterialCount = new HashMap<>(); + private final List orders = new ArrayList<>(); + + { + woodTypeCount.put("Acacia", 5); + woodTypeCount.put("English Oak", 10); + woodTypeCount.put("Hawthorn", 2); + woodTypeCount.put("Elder", 4); + woodTypeCount.put("Beech", 19); + woodTypeCount.put("Vine", 17); + + coreMaterialCount.put("Phoenix feather", 5); + coreMaterialCount.put("Unicorn hair", 22); + coreMaterialCount.put("Dragon heartstring", 15); + + } + + public void placeOrder(WandOrder order) { + + if (order == null) { + throw new IllegalArgumentException("please check you order"); + } + + String woodType = order.getWand().getWoodType(); + String coreMaterial = order.getWand().getCoreMaterial(); + Integer wtc = woodTypeCount.get(woodType); + Integer cmc = coreMaterialCount.get(coreMaterial); + Integer quantity = order.getQuantity(); + + if (wtc == null || wtc < quantity) { + log.warn("Order {} wasn't placed", order); + throw new NotEnoughWoodException("Not enough " + woodType + " in the store"); + } else if (cmc == null || cmc < quantity) { + log.warn("Order {} wasn't placed", order); + throw new NotEnoughMaterialException("Not enough " + coreMaterial + " in the store"); + } else { + orders.add(order); + woodTypeCount.replace(woodType, wtc - quantity); + coreMaterialCount.replace(coreMaterial, cmc - quantity); + log.info("Order {} was placed", order); + } + + } + + public Optional findMostPowerfulWand() { + + if (orders.isEmpty()) { + throw new OrdersListIsEmptyException("Order list is empty"); + } + + return orders.stream().map(WandOrder::getWand).max(Comparator.comparingInt(Wand::getPowerLevel)); + } + +} diff --git a/src/main/java/task_2/Task2.java b/src/main/java/task_2/Task2.java new file mode 100644 index 0000000..8f0605f --- /dev/null +++ b/src/main/java/task_2/Task2.java @@ -0,0 +1,79 @@ +package task_2; + +import java.util.Optional; + +import lombok.extern.slf4j.Slf4j; +import task_2.exceptions.OrdersListIsEmptyException; + +@Slf4j +public class Task2 { + + public static void main(String[] args) { + + OlivandersShop shop = new OlivandersShop(); + + try { + shop.findMostPowerfulWand(); + } catch (OrdersListIsEmptyException e) { + log.error("Try to find most powerful wand: {}", e.getMessage()); + } + + try { + shop.findMostPowerfulWand(); + } catch (OrdersListIsEmptyException e) { + log.error("Try to find wand: {}", e.getMessage()); + } + + try { + new Wand(null, 0, null, 0); + } catch (IllegalArgumentException e) { + log.error("Wrong arguments wand creation: {}", e.getMessage()); + } + + Wand wand1 = new Wand("Acacia", 8, "Phoenix feather", 4); + Wand wand2 = new Wand("English Oak", 9, "Unicorn hair", 5); + Wand wand3 = new Wand("Hawthorn", 10, "Dragon heartstring", 9); + Wand wand4 = new Wand("Elder", 12, "Phoenix feather", 8); + Wand wand5 = new Wand("Beech", 15, "Phoenix feather", 10); + + try { + new WandOrder(null, wand5, 0); + } catch (IllegalArgumentException e) { + log.error("Wrong arguments WandOrder creation: {}", e.getMessage()); + } + + WandOrder wandOrder1 = new WandOrder("Lord Voldemort", wand1, 1); + WandOrder wandOrder2 = new WandOrder("Harry Potter", wand2, 2); + WandOrder wandOrder3 = new WandOrder("Ron Weasley", wand3, 3); + WandOrder wandOrder4 = new WandOrder("Hermione Jean Granger", wand4, 5); + WandOrder wandOrder5 = new WandOrder("Harry Potter", wand5, 5); + + shop.placeOrder(wandOrder1); + shop.placeOrder(wandOrder2); + try { + shop.placeOrder(wandOrder3); + } catch (Exception e) { + log.error("Order placing exception: {}", e.getMessage()); + } + + try { + shop.placeOrder(wandOrder4); + } catch (Exception e) { + log.error("Order placing exception : {}", e.getMessage()); + } + + try { + shop.placeOrder(wandOrder5); + } catch (Exception e) { + log.error("Order placing exception : {}", e.getMessage()); + } + + try { + Optional mostPowerfulWand = shop.findMostPowerfulWand(); + log.info("Most powerful wand is: {}", mostPowerfulWand.get()); + } catch (OrdersListIsEmptyException e) { + log.error("Try to find most powerful wand: {}", e.getMessage()); + } + + } +} diff --git a/src/main/java/task_2/Wand.java b/src/main/java/task_2/Wand.java new file mode 100644 index 0000000..16a8bd5 --- /dev/null +++ b/src/main/java/task_2/Wand.java @@ -0,0 +1,44 @@ +package task_2; + +public class Wand { + + private String woodType; + private int length; + private String coreMaterial; + private int powerLevel; + + public Wand(String woodType, int length, String coreMaterial, int powerLevel) { + + if (woodType == null || woodType.isBlank() || length <= 0 || length > 20 || coreMaterial == null + || coreMaterial.isBlank() || powerLevel <= 0 || powerLevel > 10) { + throw new IllegalArgumentException("please check wand arguments"); + } + + this.woodType = woodType; + this.length = length; + this.coreMaterial = coreMaterial; + this.powerLevel = powerLevel; + } + + public String getWoodType() { + return woodType; + } + + public int getLength() { + return length; + } + + public String getCoreMaterial() { + return coreMaterial; + } + + public int getPowerLevel() { + return powerLevel; + } + + @Override + public String toString() { + return "Wood: " + woodType + ", " + length + " in. , core: " + coreMaterial + ", power: " + powerLevel; + } + +} diff --git a/src/main/java/task_2/WandOrder.java b/src/main/java/task_2/WandOrder.java new file mode 100644 index 0000000..26d8a71 --- /dev/null +++ b/src/main/java/task_2/WandOrder.java @@ -0,0 +1,38 @@ +package task_2; + +public class WandOrder { + + private String customerName; + private Wand wand; + private int quantity; + + public WandOrder(String customerName, Wand wand, int quantity) { + if(customerName == null || customerName.isBlank() || wand == null || quantity <= 0) { + throw new IllegalArgumentException("Please check order arguments"); + } + + this.customerName = customerName; + this.wand = wand; + this.quantity = quantity; + } + + public String getCustomerName() { + return customerName; + } + + public Wand getWand() { + return wand; + } + + public int getQuantity() { + return quantity; + } + + @Override + public String toString() { + return "From: " + customerName + " for " + quantity + "pc(s)"; + } + + + +} diff --git a/src/main/java/task_2/exceptions/NotEnoughMaterialException.java b/src/main/java/task_2/exceptions/NotEnoughMaterialException.java new file mode 100644 index 0000000..10a8a0b --- /dev/null +++ b/src/main/java/task_2/exceptions/NotEnoughMaterialException.java @@ -0,0 +1,9 @@ +package task_2.exceptions; + +public class NotEnoughMaterialException extends RuntimeException { + + public NotEnoughMaterialException(String message) { + super(message); + } + +} diff --git a/src/main/java/task_2/exceptions/NotEnoughWoodException.java b/src/main/java/task_2/exceptions/NotEnoughWoodException.java new file mode 100644 index 0000000..d8e2c14 --- /dev/null +++ b/src/main/java/task_2/exceptions/NotEnoughWoodException.java @@ -0,0 +1,10 @@ +package task_2.exceptions; + +public class NotEnoughWoodException extends RuntimeException { + + public NotEnoughWoodException(String message) { + super(message); + } + + +} diff --git a/src/main/java/task_2/exceptions/OrdersListIsEmptyException.java b/src/main/java/task_2/exceptions/OrdersListIsEmptyException.java new file mode 100644 index 0000000..797c76a --- /dev/null +++ b/src/main/java/task_2/exceptions/OrdersListIsEmptyException.java @@ -0,0 +1,11 @@ +package task_2.exceptions; + +public class OrdersListIsEmptyException extends RuntimeException { + + public OrdersListIsEmptyException(String message) { + super(message); + } + + + +} diff --git a/src/test/java/task2/OlivandersShopTest.java b/src/test/java/task2/OlivandersShopTest.java new file mode 100644 index 0000000..c9e3e4b --- /dev/null +++ b/src/test/java/task2/OlivandersShopTest.java @@ -0,0 +1,60 @@ +package task2; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import task_2.OlivandersShop; +import task_2.Wand; +import task_2.WandOrder; +import task_2.exceptions.NotEnoughMaterialException; +import task_2.exceptions.NotEnoughWoodException; +import task_2.exceptions.OrdersListIsEmptyException; + +public class OlivandersShopTest { + + private final OlivandersShop shop = new OlivandersShop(); + private final OlivandersShop shop2 = new OlivandersShop(); + private final Wand wand1 = new Wand("Acacia", 8, "Phoenix feather", 4); + private final Wand wand2 = new Wand("English Oak", 9, "Unicorn hair", 5); + private final Wand wand3 = new Wand("Hawthorn", 10, "Dragon heartstring", 9); + private final Wand wand4 = new Wand("Elder", 12, "Phoenix feather", 8); + private final Wand wand5 = new Wand("Beech", 15, "Phoenix feather", 10); + private final WandOrder wandOrder1 = new WandOrder("Lord Voldemort", wand1, 1); + private final WandOrder wandOrder2 = new WandOrder("Harry Potter", wand2, 2); + private final WandOrder wandOrder3 = new WandOrder("Ron Weasley", wand3, 3); + private final WandOrder wandOrder4 = new WandOrder("Hermione Jean Granger", wand4, 5); + private final WandOrder wandOrder5 = new WandOrder("Harry Potter", wand5, 5); + + @Test + void placeOrder() { + Exception ex = Assertions.assertThrows(IllegalArgumentException.class, () -> shop.placeOrder(null)); + + assertEquals("please check you order", ex.getMessage()); + + shop.placeOrder(wandOrder1); + assertEquals(wandOrder1.getCustomerName(), shop.getOrders().get(0).getCustomerName()); + + shop.placeOrder(wandOrder2); + assertEquals(wandOrder2.getWand().getWoodType(), shop.getOrders().get(1).getWand().getWoodType()); + + Exception ex2 = Assertions.assertThrows(NotEnoughWoodException.class, () -> shop.placeOrder(wandOrder3)); + assertEquals("Not enough " + wandOrder3.getWand().getWoodType() + " in the store", ex2.getMessage()); + + Exception ex3 = Assertions.assertThrows(NotEnoughMaterialException.class, () -> shop.placeOrder(wandOrder5)); + assertEquals("Not enough " + wandOrder5.getWand().getCoreMaterial() + " in the store", ex3.getMessage()); + + } + + void findMostPowerfulWand() { + Exception ex = Assertions.assertThrows(OrdersListIsEmptyException.class, () -> shop2.findMostPowerfulWand()); + assertEquals("Order list is empty", ex.getMessage()); + + shop2.placeOrder(wandOrder1); + shop2.placeOrder(wandOrder2); + + assertEquals(shop2.findMostPowerfulWand().get().getPowerLevel(), 5); + } + +} diff --git a/src/test/java/task2/WandOrderTest.java b/src/test/java/task2/WandOrderTest.java new file mode 100644 index 0000000..2c731f3 --- /dev/null +++ b/src/test/java/task2/WandOrderTest.java @@ -0,0 +1,34 @@ +package task2; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import task_2.Wand; +import task_2.WandOrder; + +public class WandOrderTest { + + private final Wand wand = new Wand("Acacia", 8, "Phoenix feather", 4); + + @Test + void creation() { + Exception ex = Assertions.assertThrows(IllegalArgumentException.class, + () -> new WandOrder(null, wand, 1)); + + Exception ex2 = Assertions.assertThrows(IllegalArgumentException.class, + () -> new WandOrder(" ", wand, 1)); + + Exception ex3 = Assertions.assertThrows(IllegalArgumentException.class, + () -> new WandOrder("Lord Voldemort", wand, 0)); + + Exception ex4 = Assertions.assertThrows(IllegalArgumentException.class, + () -> new WandOrder("Lord Voldemort", null, 5)); + + assertEquals("Please check order arguments", ex.getMessage()); + assertEquals("Please check order arguments", ex2.getMessage()); + assertEquals("Please check order arguments", ex3.getMessage()); + assertEquals("Please check order arguments", ex4.getMessage()); + } +} diff --git a/src/test/java/task2/WandTest.java b/src/test/java/task2/WandTest.java new file mode 100644 index 0000000..737ede1 --- /dev/null +++ b/src/test/java/task2/WandTest.java @@ -0,0 +1,28 @@ +package task2; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import task_2.Wand; + +public class WandTest { + + @Test + void creation() { + Exception ex = Assertions.assertThrows(IllegalArgumentException.class, + () -> new Wand(null, 8, "Phoenix feather", 4)); + + Exception ex2 = Assertions.assertThrows(IllegalArgumentException.class, + () -> new Wand(" ", 8, "Phoenix feather", 4)); + + Exception ex3 = Assertions.assertThrows(IllegalArgumentException.class, + () -> new Wand("English Oak", 9, "", 5)); + + assertEquals("please check wand arguments", ex.getMessage()); + assertEquals("please check wand arguments", ex2.getMessage()); + assertEquals("please check wand arguments", ex3.getMessage()); + } + +}