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

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

9 changes: 9 additions & 0 deletions .idea/encodings.xml

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

15 changes: 15 additions & 0 deletions .idea/misc.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

27 changes: 27 additions & 0 deletions order-data/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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>
<groupId>com.ironhack</groupId>
<artifactId>02-maven</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>order-data</artifactId>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
60 changes: 60 additions & 0 deletions order-data/src/main/java/com/ironhack/Order.java
Original file line number Diff line number Diff line change
@@ -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<OrderItem> items;

public Order(String id, String customerId, BigDecimal total, List<OrderItem> 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<OrderItem> 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<OrderItem> getItems() {
return items;
}

@Override
public String toString() {
return "Order{" +
"id='" + id + '\'' +
", customerId='" + customerId + '\'' +
", total=" + total +
", items=" + items +
'}';
}
}
69 changes: 69 additions & 0 deletions order-data/src/main/java/com/ironhack/OrderItem.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
67 changes: 67 additions & 0 deletions order-data/src/main/java/com/ironhack/OrderProcessor.java
Original file line number Diff line number Diff line change
@@ -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<Order> orders = processData(ordersJson);
logOrders(orders);
}

public static List<Order> processData(String ordersJson) {
Type listType = new TypeToken<List<Order>>() {}.getType();

return GSON.fromJson(ordersJson, listType);
}

private static void logOrders(List<Order> orders) {
for (Order order : orders) {
System.out.println(order);
}
}
}
33 changes: 33 additions & 0 deletions order-logic/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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>
<groupId>com.ironhack</groupId>
<artifactId>02-maven</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>order-logic</artifactId>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.ironhack</groupId>
<artifactId>order-data</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Loading