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
39 changes: 39 additions & 0 deletions solution-lab-2.02-maven/.gitignore
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions solution-lab-2.02-maven/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions solution-lab-2.02-maven/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions solution-lab-2.02-maven/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions solution-lab-2.02-maven/order-data/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- Parent reference: Inherits configurations from the parent POM. -->
<parent>
<groupId>com.example</groupId>
<artifactId>solution-lab-2.02-maven</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<!-- Module metadata: Unique artifactId for this child module. -->
<artifactId>order-data</artifactId>

<!-- Dependencies: Inherits versions from parent, adds module-specific ones. -->
<dependencies>
<!-- Gson for JSON handling. -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<!-- SLF4J for logging. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
<!-- JUnit for testing (bonus). -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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<OrderItem> items;
private double total;

// Default constructor for Gson
public Order() {
}

public Order(String orderId, String customer, List<OrderItem> 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<OrderItem> getItems() {
return items;
}

public void setItems(List<OrderItem> 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 +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
58 changes: 58 additions & 0 deletions solution-lab-2.02-maven/order-logic/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- Parent reference: Inherits from root POM. -->
<parent>
<groupId>com.example</groupId>
<artifactId>solution-lab-2.02-maven</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<!-- Module metadata. -->
<artifactId>order-logic</artifactId>

<!-- Dependencies: Includes inter-module dependency on order-data. -->
<dependencies>
<!-- Dependency on order-data module for model classes. -->
<dependency>
<groupId>com.example</groupId>
<artifactId>order-data</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- SLF4J for logging. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
<!-- JUnit for testing (bonus). -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading