Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
.idea
.gradle
.classpath
.project
.settings
bin/
!**/src/main/**/build/
!**/src/test/**/build/
build/
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/task_2/OlivandersShop.java
Original file line number Diff line number Diff line change
@@ -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<String, Integer> woodTypeCount = new HashMap<>();
private final Map<String, Integer> coreMaterialCount = new HashMap<>();
private final List<WandOrder> 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<Wand> findMostPowerfulWand() {

if (orders.isEmpty()) {
throw new OrdersListIsEmptyException("Order list is empty");
}

return orders.stream().map(WandOrder::getWand).max(Comparator.comparingInt(Wand::getPowerLevel));
}

}
79 changes: 79 additions & 0 deletions src/main/java/task_2/Task2.java
Original file line number Diff line number Diff line change
@@ -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<Wand> mostPowerfulWand = shop.findMostPowerfulWand();
log.info("Most powerful wand is: {}", mostPowerfulWand.get());
} catch (OrdersListIsEmptyException e) {
log.error("Try to find most powerful wand: {}", e.getMessage());
}

}
}
44 changes: 44 additions & 0 deletions src/main/java/task_2/Wand.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
38 changes: 38 additions & 0 deletions src/main/java/task_2/WandOrder.java
Original file line number Diff line number Diff line change
@@ -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)";
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package task_2.exceptions;

public class NotEnoughMaterialException extends RuntimeException {

public NotEnoughMaterialException(String message) {
super(message);
}

}
10 changes: 10 additions & 0 deletions src/main/java/task_2/exceptions/NotEnoughWoodException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package task_2.exceptions;

public class NotEnoughWoodException extends RuntimeException {

public NotEnoughWoodException(String message) {
super(message);
}


}
11 changes: 11 additions & 0 deletions src/main/java/task_2/exceptions/OrdersListIsEmptyException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package task_2.exceptions;

public class OrdersListIsEmptyException extends RuntimeException {

public OrdersListIsEmptyException(String message) {
super(message);
}



}
60 changes: 60 additions & 0 deletions src/test/java/task2/OlivandersShopTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
34 changes: 34 additions & 0 deletions src/test/java/task2/WandOrderTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading