Skip to content

Commit 2649ee8

Browse files
author
Mohaned Atef
committed
added application.properties file for springboot, added controller classes and used the CLI main class from the previous submission and enhanced upon it
1 parent 1c6c98b commit 2649ee8

File tree

8 files changed

+188
-21
lines changed

8 files changed

+188
-21
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.iluwatar.monolithic;
2+
3+
import com.iluwatar.monolithic.controller.OrderCon;
4+
import com.iluwatar.monolithic.controller.ProductCon;
5+
import com.iluwatar.monolithic.controller.UserCon;
6+
import com.iluwatar.monolithic.model.Products;
7+
import com.iluwatar.monolithic.model.User;
8+
import java.nio.charset.StandardCharsets;
9+
import java.util.Scanner;
10+
import org.apache.logging.log4j.LogManager;
11+
import org.apache.logging.log4j.Logger;
12+
import org.springframework.boot.CommandLineRunner;
13+
import org.springframework.boot.SpringApplication;
14+
import org.springframework.boot.autoconfigure.SpringBootApplication;
15+
16+
/**
17+
* Main entry point for the Monolithic E-commerce application.
18+
*/
19+
@SpringBootApplication
20+
public class EcommerceApp implements CommandLineRunner {
21+
22+
private static final Logger log = LogManager.getLogger(EcommerceApp.class);
23+
private final UserCon userService;
24+
private final ProductCon productService;
25+
private final OrderCon orderService;
26+
27+
public EcommerceApp(UserCon userService, ProductCon productService, OrderCon orderService) {
28+
this.userService = userService;
29+
this.productService = productService;
30+
this.orderService = orderService;
31+
}
32+
33+
public static void main(String... args) {
34+
SpringApplication.run(EcommerceApp.class, args);
35+
}
36+
37+
@Override
38+
public void run(String... args) {
39+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
40+
41+
log.info("Welcome to the Monolithic E-commerce CLI!");
42+
while (true) {
43+
log.info("\nChoose an option:");
44+
log.info("1. Register User");
45+
log.info("2. Add Product");
46+
log.info("3. Place Order");
47+
log.info("4. Exit");
48+
log.info("Enter your choice: ");
49+
50+
int userInput = scanner.nextInt();
51+
scanner.nextLine();
52+
53+
switch (userInput) {
54+
case 1 -> registerUser(scanner);
55+
case 2 -> addProduct(scanner);
56+
case 3 -> placeOrder(scanner);
57+
case 4 -> {
58+
log.info("Exiting the application. Goodbye!");
59+
return;
60+
}
61+
default -> log.info("Invalid choice! Please try again.");
62+
}
63+
}
64+
}
65+
66+
private void registerUser(Scanner scanner) {
67+
log.info("Enter user details:");
68+
log.info("Name: ");
69+
String name = scanner.nextLine();
70+
log.info("Email: ");
71+
String email = scanner.nextLine();
72+
log.info("Password: ");
73+
String password = scanner.nextLine();
74+
75+
User user = new User(null, name, email, password);
76+
userService.registerUser(user);
77+
log.info("User registered successfully!");
78+
}
79+
80+
private void addProduct(Scanner scanner) {
81+
log.info("Enter product details:");
82+
log.info("Name: ");
83+
String name = scanner.nextLine();
84+
log.info("Description: ");
85+
String description = scanner.nextLine();
86+
log.info("Price: ");
87+
double price = scanner.nextDouble();
88+
log.info("Stock: ");
89+
int stock = scanner.nextInt();
90+
scanner.nextLine();
91+
92+
Products product = new Products(null, name, description, price, stock);
93+
productService.addProduct(product);
94+
log.info("Product added successfully!");
95+
}
96+
97+
private void placeOrder(Scanner scanner) {
98+
log.info("Enter order details:");
99+
log.info("User ID: ");
100+
long userId = scanner.nextLong();
101+
log.info("Product ID: ");
102+
long productId = scanner.nextLong();
103+
log.info("Quantity: ");
104+
int quantity = scanner.nextInt();
105+
scanner.nextLine();
106+
107+
try {
108+
orderService.placeOrder(userId, productId, quantity);
109+
log.info("Order placed successfully!");
110+
} catch (Exception e) {
111+
log.info("Error placing order: {}", e.getMessage());
112+
}
113+
}
114+
}

Monolithic-Ecommerce/src/main/java/com/iluwatar/monolithic/controller/OrderCon.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11
package com.iluwatar.monolithic.controller;
22

3+
import com.iluwatar.monolithic.exceptions.NonExistentUserException;
4+
import com.iluwatar.monolithic.exceptions.NonExistentProductException;
5+
import com.iluwatar.monolithic.exceptions.InsufficientStockException;
36
import com.iluwatar.monolithic.model.User;
47
import com.iluwatar.monolithic.model.Orders;
58
import com.iluwatar.monolithic.model.Products;
69
import com.iluwatar.monolithic.repository.OrderRepo;
710
import com.iluwatar.monolithic.repository.ProductRepo;
811
import com.iluwatar.monolithic.repository.UserRepo;
9-
import org.springframework.beans.factory.annotation.Autowired;
1012
import org.springframework.stereotype.Service;
1113

1214
@Service
1315
public class OrderCon {
14-
@Autowired
15-
private OrderRepo orderRepository;
16+
private final OrderRepo orderRepository;
17+
private final UserRepo userRepository;
18+
private final ProductRepo productRepository;
1619

17-
@Autowired
18-
private UserRepo userRepository;
20+
public OrderCon(OrderRepo orderRepository, UserRepo userRepository, ProductRepo productRepository) {
21+
this.orderRepository = orderRepository;
22+
this.userRepository = userRepository;
23+
this.productRepository = productRepository;
24+
}
1925

20-
@Autowired
21-
private ProductRepo productRepository;
22-
23-
public Orders placeOrder(Long userId, Long productId, Integer quantity) {
26+
public Orders placeOrder(Long userId, Long productId, Integer quantity) {
2427
User user = userRepository.findById(userId).orElse(null);
2528
if (user == null) {
26-
throw new RuntimeException("User not found");
29+
throw new NonExistentUserException("User with ID " + userId + " not found");
2730
}
2831

2932
Products product = productRepository.findById(productId).orElse(null);
3033
if (product == null) {
31-
throw new RuntimeException("Product not found");
32-
}
34+
throw new NonExistentProductException("Product with ID " + productId + " not found");
35+
}
3336

3437
if (product.getStock() < quantity) {
35-
throw new RuntimeException("Not enough stock");
38+
throw new InsufficientStockException("Not enough stock for product " + productId);
3639
}
3740

3841
product.setStock(product.getStock() - quantity);

Monolithic-Ecommerce/src/main/java/com/iluwatar/monolithic/controller/ProductCon.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
import com.iluwatar.monolithic.model.Products;
44
import com.iluwatar.monolithic.repository.ProductRepo;
5-
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.stereotype.Service;
76

87
import java.util.List;
98

109
@Service
1110
public class ProductCon {
12-
@Autowired
13-
private ProductRepo productRepository;
11+
private final ProductRepo productRepository;
1412

15-
public Products addProduct(Products product) {
13+
public ProductCon(ProductRepo productRepository) {
14+
this.productRepository = productRepository;
15+
}
16+
17+
public Products addProduct(Products product) {
1618
return productRepository.save(product);
1719
}
1820

Monolithic-Ecommerce/src/main/java/com/iluwatar/monolithic/controller/UserCon.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
import com.iluwatar.monolithic.model.User;
44
import com.iluwatar.monolithic.repository.UserRepo;
5-
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.stereotype.Service;
76

87
@Service
98
public class UserCon {
10-
@Autowired
11-
private UserRepo userRepository;
9+
private final UserRepo userRepository;
1210

13-
public User registerUser(User user) {
11+
public UserCon(UserRepo userRepository) {
12+
this.userRepository = userRepository;
13+
}
14+
15+
public User registerUser(User user) {
1416
return userRepository.save(user);
1517
}
1618
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iluwatar.monolithic.exceptions;
2+
3+
import java.io.Serial;
4+
5+
public class InsufficientStockException extends RuntimeException{
6+
@Serial
7+
private static final long serialVersionUID = 1005208208127745099L;
8+
9+
public InsufficientStockException(String message) {
10+
super(message);
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iluwatar.monolithic.exceptions;
2+
3+
import java.io.Serial;
4+
5+
public class NonExistentProductException extends RuntimeException{
6+
@Serial
7+
private static final long serialVersionUID = -593425162052345565L;
8+
9+
public NonExistentProductException(String msg){
10+
super(msg);
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iluwatar.monolithic.exceptions;
2+
3+
import java.io.Serial;
4+
5+
public class NonExistentUserException extends RuntimeException {
6+
@Serial
7+
private static final long serialVersionUID = -7660909426227843633L;
8+
9+
public NonExistentUserException(String msg) {
10+
super(msg);
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
spring.datasource.driver-class-name=org.h2.Driver
2+
spring.datasource.password=password
3+
# Dynamic H2 Database configuration
4+
spring.datasource.url=jdbc:h2:mem:testdb
5+
spring.datasource.username=sa
6+
# H2 Console
7+
spring.h2.console.enabled=true
8+
# Hibernate Configuration
9+
spring.jpa.hibernate.ddl-auto=update
10+
spring.jpa.show-sql=true

0 commit comments

Comments
 (0)