From 42fdb57e9315d0749a1990940ebb8cb72713a1cb Mon Sep 17 00:00:00 2001 From: YunisSadig Date: Wed, 25 Feb 2026 17:40:48 +0400 Subject: [PATCH] finsihed --- .gitignore | 39 ++++ .idea/.gitignore | 10 + .idea/encodings.xml | 11 + .idea/misc.xml | 19 ++ .idea/vcs.xml | 6 + order-data/README.md | 190 ++++++++++++++++++ order-data/pom.xml | 29 +++ .../src/main/java/Ironhack/com/Order.java | 60 ++++++ .../src/main/java/Ironhack/com/OrderItem.java | 41 ++++ order-data/src/main/java/OrderProcessor.java | 28 +++ order-logic/pom.xml | 28 +++ .../src/main/java/com/OrderCalculator.java | 75 +++++++ pom.xml | 55 +++++ src/main/java/Ironhack/com/Order.java | 60 ++++++ src/main/java/Ironhack/com/OrderItem.java | 41 ++++ src/main/java/OrderProcessor.java | 28 +++ src/main/java/jsonmaven/org/Main.java | 13 ++ 17 files changed, 733 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 order-data/README.md create mode 100644 order-data/pom.xml create mode 100644 order-data/src/main/java/Ironhack/com/Order.java create mode 100644 order-data/src/main/java/Ironhack/com/OrderItem.java create mode 100644 order-data/src/main/java/OrderProcessor.java create mode 100644 order-logic/pom.xml create mode 100644 order-logic/src/main/java/com/OrderCalculator.java create mode 100644 pom.xml create mode 100644 src/main/java/Ironhack/com/Order.java create mode 100644 src/main/java/Ironhack/com/OrderItem.java create mode 100644 src/main/java/OrderProcessor.java create mode 100644 src/main/java/jsonmaven/org/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..480bdf5 --- /dev/null +++ b/.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/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.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/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e05fe48 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e3d7b17 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/order-data/README.md b/order-data/README.md new file mode 100644 index 0000000..d1e6d68 --- /dev/null +++ b/order-data/README.md @@ -0,0 +1,190 @@ +![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) + +# LAB Java | Maven + +## Introduction + +In this lab, you will build a multi-module Maven project that simulates processing order data stored in JSON format. You will use the Gson library to convert JSON data into Java objects, then apply business logic to process these orders. The goal is to practice advanced Maven configurations—including creating a parent POM and setting up child modules—while enhancing your skills in JSON manipulation and modular project design. + +
+ +## Requirements + +1. **Repository Setup:** + + - Fork this repo. + - Clone this repo. + - Add your instructor and the class graders as collaborators to your repository. If you are unsure who your class graders are, ask your instructor or refer to the day 1 slide deck. + - In the repository, create a Java project and add the code for the following prompts. + +2. **Project Structure:** + - Create a Maven project with a **parent POM**. + - Create at least **two child modules**: + - **Order Data Module:** This module is responsible for handling JSON order data. + - **Order Logic Module:** This module implements business logic (e.g., calculating order totals or filtering orders). + +## Submission + +Once you finish the assignment, submit a URL link to your repository or your pull request in the field below. + +
+ +## Instructions + +### 1. Set Up the Parent POM + +- **Create a New Parent Project:** + - Start by creating a new directory for your project. The main project should be named **solution-lab-2.02-maven**. +- **Configure the Parent POM:** + - Create a `pom.xml` in your parent directory. + - Include project metadata (groupId, artifactId, version) and set the packaging to `pom`. + - Define a **properties section** (e.g., `java.version` and `project.build.sourceEncoding`). + - Set up a **dependency management section** with at least one common dependency (for example, SLF4J). + - Configure **repository** and **pluginRepository** sections to point to Maven Central. + - List your child modules using a `` section (e.g., `order-data` and `order-logic`). + +### 2. Create the Order Data Module + +- **Module Setup:** + + - Within the parent project directory, create a new subdirectory named `order-data`. + - Create a `pom.xml` for the module. Make sure this POM references the parent POM by including a `` section. + +- **Implement JSON Processing:** + - Develop a Java class (for example, `OrderProcessor.java`) that will: + - Read a sample JSON string or file containing order details. Think about the fields you need, such as orderId, customer, items, and total. + - Use the Gson library to convert the JSON data into Java objects. You should define classes like `Order` and `OrderItem` that map to the JSON structure. + - Print the parsed order data to the console so you can verify that it’s being read correctly. + +### 3. Create the Order Logic Module + +- **Module Setup:** + + - In the parent project directory, create another subdirectory called `order-logic`. + - Create a `pom.xml` for this module and reference the parent POM in its configuration. + +- **Implement Business Logic:** + - Develop a Java class (for example, `OrderCalculator.java`) that will: + - Import the order model classes from the `order-data` module (remember to set up inter-module dependencies so that the classes are available). + - Process the order data by applying business logic—such as calculating aggregate values (summing order totals, filtering orders based on certain criteria, etc.). + - Print the results of the processing to the console. + +### 4. Build the Multi-Module Project + +- **Compile and Package:** + - From your parent project directory, run the command: + ```bash + mvn clean install + ``` + - This command should build the parent project and all child modules. Verify that the build completes successfully. + +### 5. Testing and Verification + +- **Run and Verify:** + - Run the main method in your **Order Data Module** (e.g., in `OrderProcessor.java`) to check that the JSON data is being parsed correctly. + - Run the main method in your **Order Logic Module** (e.g., in `OrderCalculator.java`) to ensure that your business logic processes the order data correctly. + - Make sure the console outputs are clear and provide informative results. + +
+ +## FAQs + +
+ +
+ I am stuck and don't know how to solve the problem or where to start. What should I do? + +
+ +If you are stuck in your code and don't know how to solve the problem or where to start, you should take a step back and try to form a clear, straight forward question about the specific issue you are facing. The process you will go through while trying to define this question, will help you narrow down the problem and come up with potential solutions. + +For example, are you facing a problem because you don't understand the concept or are you receiving an error message that you don't know how to fix? It is usually helpful to try to state the problem as clearly as possible, including any error messages you are receiving. This can help you communicate the issue to others and potentially get help from classmates or online resources. + +Once you have a clear understanding of the problem, you should be able to start working toward the solution. + +
+ +
+ +
+ How do I create a Maven project in IntelliJ? + +
+ +To create a Maven project in IntelliJ, you can follow these steps: + +1. Open IntelliJ IDEA and click the "Create New Project" button. +2. In the "New Project" dialog, select "Maven" as the build system. +3. Specify the name of the project. +4. In the "Project Location" section, specify a location where you want to save your project. +5. Select the "Create Git repository" checkbox in order to initialize the git repository upon creation of the project. +6. Click the "Create" button to create the Maven project. + +
+ +
+ +
+ +
+ What is a parent POM and why is it important? +
+ A parent POM centralizes configurations (dependencies, plugins, properties) so that all child modules inherit them. This ensures consistency and simplifies maintenance across multi-module projects. + +
+ +
+ +
+ +
+ How do I build a multi-module project using Maven? +
+ Navigate to the parent project directory in your terminal and run `mvn clean install` to build the entire project. Maven will compile and package all modules as defined in the "modules" section of the parent POM. + +
+ +
+ +
+ +
+ How do I set up inter-module dependencies in a multi-module project? +
+ If one module needs to use classes from another module, you must add an inter-module dependency. In the dependent module's `pom.xml`, add a dependency with the groupId, artifactId, and version that match the module it depends on. For example, if your `order-logic` module needs to access classes from the `order-data` module, include the `order-data` module as a dependency. Make sure that the parent POM lists both modules and that you build the project using `mvn clean install` so that all modules are correctly compiled and packaged. + +
+ +
+ +
+ +
+ I am unable to push changes to my repository. What should I do? + +
+ +If you are unable to push changes to your repository, here are a few steps that you can follow: + +1. Check your internet connection: Ensure that your internet connection is stable and working. +1. Verify your repository URL: Make sure that you are using the correct repository URL to push your changes. +1. Check Git credentials: Ensure that your Git credentials are up-to-date and correct. You can check your credentials using the following command: + +```bash +git config --list +``` + +4. Update your local repository: Before pushing changes, make sure that your local repository is up-to-date with the remote repository. You can update your local repository using the following command: + +```bash +git fetch origin +``` + +5. Check for conflicts: If there are any conflicts between your local repository and the remote repository, resolve them before pushing changes. +6. Push changes: Once you have resolved any conflicts and updated your local repository, you can try pushing changes again using the following command: + +```bash +git push origin +``` + +
diff --git a/order-data/pom.xml b/order-data/pom.xml new file mode 100644 index 0000000..8c4b50e --- /dev/null +++ b/order-data/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + org.example + project6 + 1.0-SNAPSHOT + + + order-data + + + 25 + 25 + UTF-8 + + + + + + com.google.code.gson + gson + 2.13.2 + + + + \ No newline at end of file diff --git a/order-data/src/main/java/Ironhack/com/Order.java b/order-data/src/main/java/Ironhack/com/Order.java new file mode 100644 index 0000000..b7591b7 --- /dev/null +++ b/order-data/src/main/java/Ironhack/com/Order.java @@ -0,0 +1,60 @@ +package Ironhack.com; + +import java.math.BigDecimal; +import java.util.List; + +public class Order { + private String orderId; + private String customer; + private List items; + private BigDecimal 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 BigDecimal getTotal() { + return total; + } + + public void setTotal(BigDecimal total) { + this.total = total; + } + + public Order(String orderId, String customer, List items, BigDecimal total) { + this.orderId = orderId; + this.customer = customer; + this.items = items; + this.total = total; + } + + @Override + public String toString() { + return "Order{" + + "orderId='" + orderId + '\'' + + ", customer='" + customer + '\'' + + ", items=" + items + + ", total=" + total + + '}'; + } +} diff --git a/order-data/src/main/java/Ironhack/com/OrderItem.java b/order-data/src/main/java/Ironhack/com/OrderItem.java new file mode 100644 index 0000000..ca06335 --- /dev/null +++ b/order-data/src/main/java/Ironhack/com/OrderItem.java @@ -0,0 +1,41 @@ +package Ironhack.com; + +import java.math.BigDecimal; + +public class OrderItem { + private String name; + + private BigDecimal price; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public OrderItem(String name, BigDecimal price) { + this.name = name; + + this.price = price; + } + + @Override + public String toString() { + return "OrderItem{" + + "name='" + name + '\'' + + ", price=" + price + + '}'; + } +} diff --git a/order-data/src/main/java/OrderProcessor.java b/order-data/src/main/java/OrderProcessor.java new file mode 100644 index 0000000..b02b4ac --- /dev/null +++ b/order-data/src/main/java/OrderProcessor.java @@ -0,0 +1,28 @@ +import Ironhack.com.Order; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class OrderProcessor { + public void main(){ + processor(); + } + public static void processor(){ + String json = """ + { + "orderId": "1001", + "customer": "Alice Smith", + "items": [ + {"name": "Laptop", "quantity": 1, "price": 1200.50}, + {"name": "Mouse", "quantity": 2, "price": 25.75} + ], + "total": 1252.0 + } + """; + Gson gson=new GsonBuilder().setPrettyPrinting().create(); + Order orderJson=gson.fromJson(json,Order.class); + + System.out.println("Your order"); + System.out.println(orderJson); + } +} diff --git a/order-logic/pom.xml b/order-logic/pom.xml new file mode 100644 index 0000000..2660700 --- /dev/null +++ b/order-logic/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + org.example + project6 + 1.0-SNAPSHOT + + + order-logic + + + 25 + 25 + UTF-8 + + + + org.example + order-data + 1.0-SNAPSHOT + compile + + + + \ No newline at end of file diff --git a/order-logic/src/main/java/com/OrderCalculator.java b/order-logic/src/main/java/com/OrderCalculator.java new file mode 100644 index 0000000..b56da01 --- /dev/null +++ b/order-logic/src/main/java/com/OrderCalculator.java @@ -0,0 +1,75 @@ +package com; + +import Ironhack.com.Order; +import Ironhack.com.OrderItem; + +import java.math.BigDecimal; +import java.util.List; + +public class OrderCalculator { + public void main(String[] args){ + ListorderItems1=List.of(new OrderItem("meat",new BigDecimal("12.5")),new OrderItem("knife",new BigDecimal("13.7"))); + ListorderItems2=List.of(new OrderItem("paper",new BigDecimal("12.6")),new OrderItem("Scissors",new BigDecimal("13.5"))); + + Order order1 = new Order("1001", "Yunis Sadiq", orderItems1, new BigDecimal("27.2")); + Order order2 = new Order("1002", "Ayhan Agayev", orderItems2, new BigDecimal("26.1")); + + BigDecimal threshold=new BigDecimal("26.0"); + Listorders=List.of(order1,order2); + printOrders(orders); + finalTotalCalculator(orders); + filterByPrice(orders,threshold); + findMin(orders); + findMax(orders); + } + + public static void printOrders(List orders){ + System.out.println("Get each order"); + for(Order order:orders){ + System.out.println(order); + } + } + public static void finalTotalCalculator(Listorders){ + System.out.println("Total final"); + BigDecimal finalTotal= BigDecimal.ZERO; + for(Order order:orders){ + finalTotal=finalTotal.add(order.getTotal()); + } + System.out.println("\nFinal total of all orders: $" + finalTotal); + } + public static void filterByPrice(List orders, BigDecimal threshold){ + System.out.println("Orders that are above " + threshold); + for(Order order:orders){ + if(order.getTotal().compareTo(threshold)>0) { + System.out.println(order); + } + } + System.out.println("---------------------"); + } + public static void findMin(List orders){ + if(orders.isEmpty()){ + return; + } + Order minOrder=orders.get(0); + for(Order order:orders){ + if(order.getTotal().compareTo(minOrder.getTotal())<0){ + minOrder=order; + } + } + System.out.println("The order is with minimum total is "+minOrder); + + } + public static void findMax(List orders){ + if(orders.isEmpty()){ + return; + } + Order maxOrder=orders.get(0); + for(Order order:orders){ + if(order.getTotal().compareTo(maxOrder.getTotal())>0){ + maxOrder=order; + } + } + System.out.println("The order is with minimum total is "+maxOrder); + + } +} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..bec08e9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + + ironhack + solution-lab-2.02-maven + 1.0-SNAPSHOT + pom + + order-data + order-logic + + + + 21 + 21 + UTF-8 + 21 + + + + + + com.google.code.gson + gson + 2.13.2 + compile + + + + + + + com.google.code.gson + gson + + + + + + central + https://repo.maven.apache.org/maven2 + + + + + + central + https://repo.maven.apache.org/maven2 + + + + \ No newline at end of file diff --git a/src/main/java/Ironhack/com/Order.java b/src/main/java/Ironhack/com/Order.java new file mode 100644 index 0000000..b7591b7 --- /dev/null +++ b/src/main/java/Ironhack/com/Order.java @@ -0,0 +1,60 @@ +package Ironhack.com; + +import java.math.BigDecimal; +import java.util.List; + +public class Order { + private String orderId; + private String customer; + private List items; + private BigDecimal 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 BigDecimal getTotal() { + return total; + } + + public void setTotal(BigDecimal total) { + this.total = total; + } + + public Order(String orderId, String customer, List items, BigDecimal total) { + this.orderId = orderId; + this.customer = customer; + this.items = items; + this.total = total; + } + + @Override + public String toString() { + return "Order{" + + "orderId='" + orderId + '\'' + + ", customer='" + customer + '\'' + + ", items=" + items + + ", total=" + total + + '}'; + } +} diff --git a/src/main/java/Ironhack/com/OrderItem.java b/src/main/java/Ironhack/com/OrderItem.java new file mode 100644 index 0000000..ca06335 --- /dev/null +++ b/src/main/java/Ironhack/com/OrderItem.java @@ -0,0 +1,41 @@ +package Ironhack.com; + +import java.math.BigDecimal; + +public class OrderItem { + private String name; + + private BigDecimal price; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public OrderItem(String name, BigDecimal price) { + this.name = name; + + this.price = price; + } + + @Override + public String toString() { + return "OrderItem{" + + "name='" + name + '\'' + + ", price=" + price + + '}'; + } +} diff --git a/src/main/java/OrderProcessor.java b/src/main/java/OrderProcessor.java new file mode 100644 index 0000000..b02b4ac --- /dev/null +++ b/src/main/java/OrderProcessor.java @@ -0,0 +1,28 @@ +import Ironhack.com.Order; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class OrderProcessor { + public void main(){ + processor(); + } + public static void processor(){ + String json = """ + { + "orderId": "1001", + "customer": "Alice Smith", + "items": [ + {"name": "Laptop", "quantity": 1, "price": 1200.50}, + {"name": "Mouse", "quantity": 2, "price": 25.75} + ], + "total": 1252.0 + } + """; + Gson gson=new GsonBuilder().setPrettyPrinting().create(); + Order orderJson=gson.fromJson(json,Order.class); + + System.out.println("Your order"); + System.out.println(orderJson); + } +} diff --git a/src/main/java/jsonmaven/org/Main.java b/src/main/java/jsonmaven/org/Main.java new file mode 100644 index 0000000..8d1f617 --- /dev/null +++ b/src/main/java/jsonmaven/org/Main.java @@ -0,0 +1,13 @@ +package jsonmaven.org; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.util.ArrayList; +import java.util.List; + +public class Main { + public void main(String[] args){ + + } +}