diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..7bc07ec --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Environment-dependent path to Maven home directory +/mavenHomeManager.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..b238dc7 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..fa69e43 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/order-data/pom.xml b/order-data/pom.xml new file mode 100644 index 0000000..d2490ff --- /dev/null +++ b/order-data/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + com.ironhack + 02-maven + 1.0-SNAPSHOT + + + order-data + + + 21 + 21 + UTF-8 + + + + + com.google.code.gson + gson + compile + + + \ No newline at end of file diff --git a/order-data/src/main/java/com/ironhack/Order.java b/order-data/src/main/java/com/ironhack/Order.java new file mode 100644 index 0000000..d9c542e --- /dev/null +++ b/order-data/src/main/java/com/ironhack/Order.java @@ -0,0 +1,60 @@ +package com.ironhack; + +import java.math.BigDecimal; +import java.util.List; + +public class Order { + private final String id; + private final String customerId; + private final BigDecimal total; + private final List items; + + public Order(String id, String customerId, BigDecimal total, List items) { + validateOrder(id, customerId, total, items); + this.id = id; + this.customerId = customerId; + this.total = total; + this.items = items; + } + + private void validateOrder(String id, String customerId, BigDecimal total, List items) { + if (id == null) { + throw new IllegalArgumentException("ID cannot be null"); + } + if (customerId == null) { + throw new IllegalArgumentException("Customer ID cannot be null"); + } + if (total == null) { + throw new IllegalArgumentException("Total cannot be null"); + } + if (items == null) { + throw new IllegalArgumentException("Order items must be specified"); + } + } + + public String getId() { + return id; + } + + public String getCustomerId() { + return customerId; + } + + public BigDecimal getTotal() { + return total; + } + + public List getItems() { + return items; + } + + @Override + public String toString() { + return "Order{" + + "id='" + id + '\'' + + ", customerId='" + customerId + '\'' + + ", total=" + total + + ", items=" + items + + '}'; + } +} diff --git a/order-data/src/main/java/com/ironhack/OrderItem.java b/order-data/src/main/java/com/ironhack/OrderItem.java new file mode 100644 index 0000000..ecce20f --- /dev/null +++ b/order-data/src/main/java/com/ironhack/OrderItem.java @@ -0,0 +1,69 @@ +package com.ironhack; + +import java.math.BigDecimal; + +public class OrderItem { + private final String id; + private final String orderId; + private final String productId; + private final int quantity; + private final BigDecimal unitPrice; + + public OrderItem(String id, String orderId, String productId, int quantity, BigDecimal unitPrice) { + validateOrderItem(id, orderId, productId, quantity, unitPrice); + this.id = id; + this.orderId = orderId; + this.productId = productId; + this.quantity = quantity; + this.unitPrice = unitPrice; + } + + private void validateOrderItem(String id, String orderId, String productId, int quantity, BigDecimal unitPrice) { + if (id == null) { + throw new IllegalArgumentException("ID cannot be null"); + } + if (orderId == null) { + throw new IllegalArgumentException("Order ID cannot be null"); + } + if (productId == null) { + throw new IllegalArgumentException("Product ID cannot be null"); + } + if (quantity < 0) { + throw new IllegalArgumentException("Quantity cannot be negative"); + } + if (unitPrice == null) { + throw new IllegalArgumentException("Unit Price cannot be null"); + } + } + + public String getId() { + return id; + } + + public String getOrderId() { + return orderId; + } + + public String getProductId() { + return productId; + } + + public int getQuantity() { + return quantity; + } + + public BigDecimal getUnitPrice() { + return unitPrice; + } + + @Override + public String toString() { + return "OrderItem{" + + "id='" + id + '\'' + + ", orderId='" + orderId + '\'' + + ", productId='" + productId + '\'' + + ", quantity=" + quantity + + ", unitPrice=" + unitPrice + + '}'; + } +} diff --git a/order-data/src/main/java/com/ironhack/OrderProcessor.java b/order-data/src/main/java/com/ironhack/OrderProcessor.java new file mode 100644 index 0000000..96cfd30 --- /dev/null +++ b/order-data/src/main/java/com/ironhack/OrderProcessor.java @@ -0,0 +1,67 @@ +package com.ironhack; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +import java.lang.reflect.Type; +import java.util.List; + +public class OrderProcessor { + private static final Gson GSON = new Gson(); + + public static void main(String[] args) { + String ordersJson = """ + [ + { + "id": "order-1001", + "customerId": "customer-501", + "total": 259.98, + "items": [ + { + "id": "item-1", + "orderId": "order-1001", + "productId": "product-2001", + "quantity": 2, + "unitPrice": 49.99 + }, + { + "id": "item-2", + "orderId": "order-1001", + "productId": "product-2002", + "quantity": 1, + "unitPrice": 159.99 + } + ] + }, + { + "id": "order-1002", + "customerId": "customer-777", + "total": 89.97, + "items": [ + { + "id": "item-3", + "orderId": "order-1002", + "productId": "product-3001", + "quantity": 3, + "unitPrice": 29.99 + } + ] + } + ] + """; + List orders = processData(ordersJson); + logOrders(orders); + } + + public static List processData(String ordersJson) { + Type listType = new TypeToken>() {}.getType(); + + return GSON.fromJson(ordersJson, listType); + } + + private static void logOrders(List orders) { + for (Order order : orders) { + System.out.println(order); + } + } +} diff --git a/order-logic/pom.xml b/order-logic/pom.xml new file mode 100644 index 0000000..62bf334 --- /dev/null +++ b/order-logic/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + com.ironhack + 02-maven + 1.0-SNAPSHOT + + + order-logic + + + 21 + 21 + UTF-8 + + + + + com.google.code.gson + gson + compile + + + com.ironhack + order-data + 1.0-SNAPSHOT + compile + + + \ No newline at end of file diff --git a/order-logic/src/main/java/com/ironhack/OrderCalculator.java b/order-logic/src/main/java/com/ironhack/OrderCalculator.java new file mode 100644 index 0000000..0652f63 --- /dev/null +++ b/order-logic/src/main/java/com/ironhack/OrderCalculator.java @@ -0,0 +1,96 @@ +package com.ironhack; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +public class OrderCalculator { + public static void main(String[] args) { + String ordersJson = """ + [ + { + "id": "order-1001", + "customerId": "customer-501", + "total": 250.00, + "items": [ + { + "id": "item-1", + "orderId": "order-1001", + "productId": "product-2001", + "quantity": 2, + "unitPrice": 50.00 + }, + { + "id": "item-2", + "orderId": "order-1001", + "productId": "product-2002", + "quantity": 1, + "unitPrice": 150.00 + } + ] + }, + { + "id": "order-1002", + "customerId": "customer-777", + "total": 89.97, + "items": [ + { + "id": "item-3", + "orderId": "order-1002", + "productId": "product-3001", + "quantity": 3, + "unitPrice": 29.99 + } + ] + } + ] + """; + + List orders = OrderProcessor.processData(ordersJson); + validateOrders(orders); + + // Should return 0 elements + List filteredOrders = filterByPrice(orders, BigDecimal.valueOf(100)); + System.out.println("Filtered orders: " + filteredOrders); + + // Should return element with specified id + Order orderByCustomerId = filterByCustomerId(orders, "customer-501"); + System.out.println("Order by customer ID=customer-501 : " + orderByCustomerId); + } + + private static void validateOrders(List orders) { + // Validate order total equals actual total + for (Order order : orders) { + BigDecimal actualTotal = BigDecimal.ZERO; + + for (OrderItem item : order.getItems()) { + BigDecimal quantity = BigDecimal.valueOf(item.getQuantity()); + BigDecimal orderItemPrice = item.getUnitPrice().multiply(quantity); + + actualTotal = actualTotal.add(orderItemPrice); + } + if (order.getTotal().compareTo(actualTotal) != 0) { + throw new IllegalArgumentException("INVALID_TOTAL_PRICE. Order items total price does not match given total."); + } + } + } + + public static List filterByPrice(List orders, BigDecimal price) { + List filteredOrders = new ArrayList<>(); + for (Order order : orders) { + if (order.getTotal().compareTo(price) == 0) { + filteredOrders.add(order); + } + } + return filteredOrders; + } + + public static Order filterByCustomerId(List orders, String customerId) { + for (Order order : orders) { + if (order.getCustomerId().equals(customerId)) { + return order; + } + } + return null; + } +} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..3b1c86c --- /dev/null +++ b/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + com.ironhack + 02-maven + 1.0-SNAPSHOT + pom + + + order-data + order-logic + + + + 21 + 21 + UTF-8 + + + + + central + https://repo1.maven.org/maven2 + + + + + central + https://repo1.maven.org/maven2 + + + + + + + com.google.code.gson + gson + 2.13.2 + compile + + + + + \ No newline at end of file