From 31db0a5c7b752330c193327a66db81cb243cd6ec Mon Sep 17 00:00:00 2001 From: kenanqafarov Date: Tue, 24 Feb 2026 18:29:07 +0400 Subject: [PATCH] All done --- solution-lab-2.02-maven/.gitignore | 39 ++++++ solution-lab-2.02-maven/.idea/.gitignore | 10 ++ solution-lab-2.02-maven/.idea/encodings.xml | 11 ++ solution-lab-2.02-maven/.idea/misc.xml | 14 +++ solution-lab-2.02-maven/order-data/pom.xml | 46 +++++++ .../java/com/ironhack/orderdata/Order.java | 66 ++++++++++ .../com/ironhack/orderdata/OrderItem.java | 52 ++++++++ .../ironhack/orderdata/OrderProcessor.java | 62 ++++++++++ .../src/main/resources/sample-order.json | 17 +++ solution-lab-2.02-maven/order-logic/pom.xml | 58 +++++++++ .../com/ironhack/logic/OrderCalculator.java | 66 ++++++++++ solution-lab-2.02-maven/pom.xml | 113 ++++++++++++++++++ 12 files changed, 554 insertions(+) create mode 100644 solution-lab-2.02-maven/.gitignore create mode 100644 solution-lab-2.02-maven/.idea/.gitignore create mode 100644 solution-lab-2.02-maven/.idea/encodings.xml create mode 100644 solution-lab-2.02-maven/.idea/misc.xml create mode 100644 solution-lab-2.02-maven/order-data/pom.xml create mode 100644 solution-lab-2.02-maven/order-data/src/main/java/com/ironhack/orderdata/Order.java create mode 100644 solution-lab-2.02-maven/order-data/src/main/java/com/ironhack/orderdata/OrderItem.java create mode 100644 solution-lab-2.02-maven/order-data/src/main/java/com/ironhack/orderdata/OrderProcessor.java create mode 100644 solution-lab-2.02-maven/order-data/src/main/resources/sample-order.json create mode 100644 solution-lab-2.02-maven/order-logic/pom.xml create mode 100644 solution-lab-2.02-maven/order-logic/src/main/java/com/ironhack/logic/OrderCalculator.java create mode 100644 solution-lab-2.02-maven/pom.xml diff --git a/solution-lab-2.02-maven/.gitignore b/solution-lab-2.02-maven/.gitignore new file mode 100644 index 0000000..480bdf5 --- /dev/null +++ b/solution-lab-2.02-maven/.gitignore @@ -0,0 +1,39 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ +.kotlin + +### 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/solution-lab-2.02-maven/.idea/.gitignore b/solution-lab-2.02-maven/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/solution-lab-2.02-maven/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/solution-lab-2.02-maven/.idea/encodings.xml b/solution-lab-2.02-maven/.idea/encodings.xml new file mode 100644 index 0000000..e05fe48 --- /dev/null +++ b/solution-lab-2.02-maven/.idea/encodings.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/solution-lab-2.02-maven/.idea/misc.xml b/solution-lab-2.02-maven/.idea/misc.xml new file mode 100644 index 0000000..d2b5d0f --- /dev/null +++ b/solution-lab-2.02-maven/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/solution-lab-2.02-maven/order-data/pom.xml b/solution-lab-2.02-maven/order-data/pom.xml new file mode 100644 index 0000000..e7160b4 --- /dev/null +++ b/solution-lab-2.02-maven/order-data/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + + + + com.example + solution-lab-2.02-maven + 1.0-SNAPSHOT + + + + order-data + + + + + + com.google.code.gson + gson + 2.10.1 + + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-simple + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + \ No newline at end of file diff --git a/solution-lab-2.02-maven/order-data/src/main/java/com/ironhack/orderdata/Order.java b/solution-lab-2.02-maven/order-data/src/main/java/com/ironhack/orderdata/Order.java new file mode 100644 index 0000000..115d5a2 --- /dev/null +++ b/solution-lab-2.02-maven/order-data/src/main/java/com/ironhack/orderdata/Order.java @@ -0,0 +1,66 @@ +package com.ironhack.orderdata; + +import java.util.List; + +/** + * Represents an Order entity mapped from JSON data. + */ +public class Order { + private String orderId; + private String customer; + private List items; + private double total; + + // Default constructor for Gson + public Order() { + } + + public Order(String orderId, String customer, List items, double total) { + this.orderId = orderId; + this.customer = customer; + this.items = items; + this.total = total; + } + + public String getOrderId() { + return orderId; + } + + public void setOrderId(String orderId) { + this.orderId = orderId; + } + + public String getCustomer() { + return customer; + } + + public void setCustomer(String customer) { + this.customer = customer; + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } + + public double getTotal() { + return total; + } + + public void setTotal(double total) { + this.total = total; + } + + @Override + public String toString() { + return "Order{" + + "orderId='" + orderId + '\'' + + ", customer='" + customer + '\'' + + ", items=" + items + + ", total=" + total + + '}'; + } +} \ No newline at end of file diff --git a/solution-lab-2.02-maven/order-data/src/main/java/com/ironhack/orderdata/OrderItem.java b/solution-lab-2.02-maven/order-data/src/main/java/com/ironhack/orderdata/OrderItem.java new file mode 100644 index 0000000..d6cf084 --- /dev/null +++ b/solution-lab-2.02-maven/order-data/src/main/java/com/ironhack/orderdata/OrderItem.java @@ -0,0 +1,52 @@ +package com.ironhack.orderdata; + +/** + * Represents an individual item within an Order. + */ +public class OrderItem { + private String product; + private int quantity; + private double price; + + public OrderItem() { + } + + public OrderItem(String product, int quantity, double price) { + this.product = product; + this.quantity = quantity; + this.price = price; + } + + public String getProduct() { + return product; + } + + public void setProduct(String product) { + this.product = product; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + @Override + public String toString() { + return "OrderItem{" + + "product='" + product + '\'' + + ", quantity=" + quantity + + ", price=" + price + + '}'; + } +} \ No newline at end of file diff --git a/solution-lab-2.02-maven/order-data/src/main/java/com/ironhack/orderdata/OrderProcessor.java b/solution-lab-2.02-maven/order-data/src/main/java/com/ironhack/orderdata/OrderProcessor.java new file mode 100644 index 0000000..0f66ef6 --- /dev/null +++ b/solution-lab-2.02-maven/order-data/src/main/java/com/ironhack/orderdata/OrderProcessor.java @@ -0,0 +1,62 @@ +package com.ironhack.orderdata; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; + +/** + * JSON sifariş məlumatlarını classpath-dən oxuyur və Order obyektinə çevirir. + */ +public class OrderProcessor { + + private static final Logger logger = LoggerFactory.getLogger(OrderProcessor.class); + private static final Gson gson = new Gson(); + + public static void main(String[] args) { + String resourceName = "sample-order.json"; + + logger.info("Sifariş JSON-u yüklənir: {}", resourceName); + + try { + Order order = parseOrderFromClasspath(resourceName); + logger.info("Sifariş uğurla oxundu: {}", order); + + // Əlavə olaraq hesablanmış totalı da yoxlaya bilərik + double calculated = order.getItems().stream() + .mapToDouble(item -> item.getQuantity() * item.getPrice()) + .sum(); + logger.info("JSON-dakı total: {}, hesablanmış: {}", order.getTotal(), calculated); + + } catch (IOException e) { + logger.error("Fayl oxunmadı: {}", resourceName, e); + } catch (JsonSyntaxException e) { + logger.error("JSON formatı səhvdir", e); + } catch (Exception e) { + logger.error("Gözlənilməz xəta", e); + } + } + + public static Order parseOrderFromClasspath(String resourceName) throws IOException { + ClassLoader classLoader = OrderProcessor.class.getClassLoader(); + InputStream inputStream = classLoader.getResourceAsStream(resourceName); + + if (inputStream == null) { + throw new IOException("Fayl tapılmadı: " + resourceName + + "\nYer: src/main/resources/" + resourceName); + } + + try (InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) { + Order order = gson.fromJson(reader, Order.class); + if (order == null) { + throw new IOException("JSON boşdur və ya parse olunmadı"); + } + return order; + } + } +} \ No newline at end of file diff --git a/solution-lab-2.02-maven/order-data/src/main/resources/sample-order.json b/solution-lab-2.02-maven/order-data/src/main/resources/sample-order.json new file mode 100644 index 0000000..c489c3c --- /dev/null +++ b/solution-lab-2.02-maven/order-data/src/main/resources/sample-order.json @@ -0,0 +1,17 @@ +{ + "orderId": "123", + "customer": "John Doe", + "items": [ + { + "product": "Book", + "quantity": 2, + "price": 10.0 + }, + { + "product": "Pen", + "quantity": 5, + "price": 1.0 + } + ], + "total": 25.0 +} \ No newline at end of file diff --git a/solution-lab-2.02-maven/order-logic/pom.xml b/solution-lab-2.02-maven/order-logic/pom.xml new file mode 100644 index 0000000..51b8f10 --- /dev/null +++ b/solution-lab-2.02-maven/order-logic/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + + + com.example + solution-lab-2.02-maven + 1.0-SNAPSHOT + + + + order-logic + + + + + + com.example + order-data + 1.0-SNAPSHOT + + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-simple + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 16 + 16 + + + + + \ No newline at end of file diff --git a/solution-lab-2.02-maven/order-logic/src/main/java/com/ironhack/logic/OrderCalculator.java b/solution-lab-2.02-maven/order-logic/src/main/java/com/ironhack/logic/OrderCalculator.java new file mode 100644 index 0000000..0e66f1f --- /dev/null +++ b/solution-lab-2.02-maven/order-logic/src/main/java/com/ironhack/logic/OrderCalculator.java @@ -0,0 +1,66 @@ +package com.ironhack.logic; + +import com.ironhack.orderdata.Order; +import com.ironhack.orderdata.OrderItem; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; + +/** + * Order obyektləri üzərində biznes məntiqini tətbiq edir + */ +public class OrderCalculator { + + private static final Logger logger = LoggerFactory.getLogger(OrderCalculator.class); + + public static void main(String[] args) { + // Test üçün nümunə order (hard-coded) + List items = new ArrayList<>(); + items.add(new OrderItem("Book", 2, 10.0)); + items.add(new OrderItem("Pen", 5, 1.0)); + + Order order = new Order("123", "John Doe", items, 25.0); + + double calculatedTotal = calculateOrderTotal(order); + logger.info("Hesablanmış məbləğ : {}", calculatedTotal); + + List expensive = filterExpensiveItems(order, 5.0); + logger.info("Bahalı məhsullar (> 5.0): {}", expensive); + + // Yoxlama + if (Math.abs(calculatedTotal - order.getTotal()) > 0.001) { + logger.warn("Total uyğunsuzluğu! Hesab: {}, Verilmiş: {}", calculatedTotal, order.getTotal()); + } else { + logger.info("Total düzgündür ✓"); + } + } + + /** + * Sifarişin ümumi məbləğini hesablayır (item-lərdən) + */ + public static double calculateOrderTotal(Order order) { + if (order == null || order.getItems() == null || order.getItems().isEmpty()) { + logger.warn("Boş və ya null sifariş"); + return 0.0; + } + + return order.getItems().stream() + .mapToDouble(item -> item.getQuantity() * item.getPrice()) + .sum(); + } + + /** + * Müəyyən qiymətdən bahalı məhsulları qaytarır + */ + public static List filterExpensiveItems(Order order, double minPrice) { + if (order == null || order.getItems() == null) { + return new ArrayList<>(); + } + + return order.getItems().stream() + .filter(item -> item.getPrice() > minPrice) + .toList(); + } +} \ No newline at end of file diff --git a/solution-lab-2.02-maven/pom.xml b/solution-lab-2.02-maven/pom.xml new file mode 100644 index 0000000..4399b85 --- /dev/null +++ b/solution-lab-2.02-maven/pom.xml @@ -0,0 +1,113 @@ + + + 4.0.0 + + com.ironhack + solution-lab-2.02-maven + 1.0-SNAPSHOT + pom + + + 11 + UTF-8 + ${java.version} + ${java.version} + + + 2.13.2 + 2.0.17 + 5.10.0 + + + + + + + com.google.code.gson + gson + ${gson.version} + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + slf4j-simple + ${slf4j.version} + + + + + org.junit.jupiter + junit-jupiter-api + ${junit.jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-params + ${junit.jupiter.version} + test + + + + + + + central + https://repo.maven.apache.org/maven2 + true + false + + + + + + central + https://repo.maven.apache.org/maven2 + true + false + + + + + order-data + order-logic + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + ${java.version} + ${java.version} + ${project.build.sourceEncoding} + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.5.0 + + + + + \ No newline at end of file