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
3 changes: 2 additions & 1 deletion src/main/java/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public record Order(
String id,
List<Product> products
List<Product> products,
Orderstatus orderstatus
) {
}
5 changes: 5 additions & 0 deletions src/main/java/Orderstatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public enum Orderstatus {
PROCESSING,
IN_DELIVERY,
COMPLETED
}
5 changes: 5 additions & 0 deletions src/main/java/ProductNotFound.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class ProductNotFound extends Exception {
public ProductNotFound(String message) {
super(message);
}
}
7 changes: 4 additions & 3 deletions src/main/java/ProductRepo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class ProductRepo {
private List<Product> products;
Expand All @@ -13,13 +14,13 @@ public List<Product> getProducts() {
return products;
}

public Product getProductById(String id) {
public Optional<Product> getProductById(String id) {
for (Product product : products) {
if (product.id().equals(id)) {
return product;
return Optional.of(product);
}
}
return null;
return Optional.empty();
}

public Product addProduct(Product newProduct) {
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/ShopService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ public class ShopService {
private ProductRepo productRepo = new ProductRepo();
private OrderRepo orderRepo = new OrderMapRepo();

public Order addOrder(List<String> productIds) {
public Order addOrder(List<String> productIds) throws ProductNotFound {
List<Product> products = new ArrayList<>();
for (String productId : productIds) {
Product productToOrder = productRepo.getProductById(productId);
Product productToOrder = productRepo.getProductById(productId).orElse(null);
if (productToOrder == null) {
System.out.println("Product mit der Id: " + productId + " konnte nicht bestellt werden!");
return null;
throw new ProductNotFound("Product mit der Id: \"" + productId + "\" konnte nicht bestellt werden!");
//System.out.println("Product mit der Id: " + productId + " konnte nicht bestellt werden!");
//return null;
}
products.add(productToOrder);
}

Order newOrder = new Order(UUID.randomUUID().toString(), products);
Order newOrder = new Order(UUID.randomUUID().toString(), products,Orderstatus.PROCESSING);

return orderRepo.addOrder(newOrder);
}
public List<Order> getByStatus(Orderstatus orderstatus) {
List<Order> orders = orderRepo.getOrders();
List<Order> matched = orders.stream()
.filter(s->s.orderstatus()==orderstatus)
.toList();
return matched;
}
}
31 changes: 31 additions & 0 deletions src/main/java/main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.ArrayList;
import java.util.List;

public class main {
public static void main(String[] args) {
ShopService shopService = new ShopService();
ProductRepo productRepo = new ProductRepo();

List<String> order1 = new ArrayList<>();
order1.add("1");

try{
shopService.addOrder(order1);
}catch(ProductNotFound e){
System.out.println(e);
}

List<String> order2 = new ArrayList<>();
order2.add("2");
try{
shopService.addOrder(order2);
}catch(ProductNotFound e){
System.out.println(e);
}


shopService.getByStatus(Orderstatus.PROCESSING);
System.out.println(productRepo.getProductById("1"));
System.out.println(productRepo.getProductById("1").get());
}
}
2 changes: 1 addition & 1 deletion src/test/java/ProductRepoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void getProductById() {
ProductRepo repo = new ProductRepo();

//WHEN
Product actual = repo.getProductById("1");
Product actual = repo.getProductById("1").orElse(null);

//THEN
Product expected = new Product("1", "Apfel");
Expand Down