Skip to content

Commit 31222a1

Browse files
author
Mohaned Atef
committed
fixed checkstyle comments
1 parent 2649ee8 commit 31222a1

File tree

13 files changed

+213
-165
lines changed

13 files changed

+213
-165
lines changed

Monolithic-Ecommerce/src/main/java/com/iluwatar/monolithic/EcommerceApp.java

Lines changed: 86 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -20,95 +20,105 @@
2020
public class EcommerceApp implements CommandLineRunner {
2121

2222
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-
23+
private final UserCon userService;
24+
private final ProductCon productService;
25+
private final OrderCon orderService;
26+
/**
27+
* Initilizing controllers as services.
28+
* */
2729
public EcommerceApp(UserCon userService, ProductCon productService, OrderCon orderService) {
2830
this.userService = userService;
2931
this.productService = productService;
3032
this.orderService = orderService;
3133
}
32-
34+
/**
35+
* The main entry point for the Monolithic E-commerce application.
36+
* Initializes the Spring Boot application and starts the embedded server.
37+
*/
3338
public static void main(String... args) {
34-
SpringApplication.run(EcommerceApp.class, args);
35-
}
39+
SpringApplication.run(EcommerceApp.class, args);
40+
}
3641

37-
@Override
38-
public void run(String... args) {
39-
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
42+
@Override
43+
public void run(String... args) {
44+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
4045

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: ");
46+
log.info("Welcome to the Monolithic E-commerce CLI!");
47+
while (true) {
48+
log.info("\nChoose an option:");
49+
log.info("1. Register User");
50+
log.info("2. Add Product");
51+
log.info("3. Place Order");
52+
log.info("4. Exit");
53+
log.info("Enter your choice: ");
4954

50-
int userInput = scanner.nextInt();
51-
scanner.nextLine();
55+
int userInput = scanner.nextInt();
56+
scanner.nextLine();
5257

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-
}
58+
switch (userInput) {
59+
case 1 -> registerUser(scanner);
60+
case 2 -> addProduct(scanner);
61+
case 3 -> placeOrder(scanner);
62+
case 4 -> {
63+
log.info("Exiting the application. Goodbye!");
64+
return;
6365
}
66+
default -> log.info("Invalid choice! Please try again.");
67+
}
6468
}
69+
}
70+
/**
71+
* Handles User Registration through user CLI inputs.
72+
* */
73+
protected void registerUser(Scanner scanner) {
74+
log.info("Enter user details:");
75+
log.info("Name: ");
76+
String name = scanner.nextLine();
77+
log.info("Email: ");
78+
String email = scanner.nextLine();
79+
log.info("Password: ");
80+
String password = scanner.nextLine();
6581

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();
82+
User user = new User(null, name, email, password);
83+
userService.registerUser(user);
84+
log.info("User registered successfully!");
85+
}
86+
/**
87+
* Handles the addition of products.
88+
* */
89+
protected void addProduct(Scanner scanner) {
90+
log.info("Enter product details:");
91+
log.info("Name: ");
92+
String name = scanner.nextLine();
93+
log.info("Description: ");
94+
String description = scanner.nextLine();
95+
log.info("Price: ");
96+
double price = scanner.nextDouble();
97+
log.info("Stock: ");
98+
int stock = scanner.nextInt();
99+
Products product = new Products(null, name, description, price, stock);
100+
scanner.nextLine();
101+
productService.addProduct(product);
102+
log.info("Product added successfully!");
103+
}
104+
/**
105+
* Handles Order Placement through user CLI inputs.
106+
*/
107+
protected void placeOrder(Scanner scanner) {
108+
log.info("Enter order details:");
109+
log.info("User ID: ");
110+
long userId = scanner.nextLong();
111+
log.info("Product ID: ");
112+
long productId = scanner.nextLong();
113+
log.info("Quantity: ");
114+
int quantity = scanner.nextInt();
115+
scanner.nextLine();
106116

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-
}
117+
try {
118+
orderService.placeOrder(userId, productId, quantity);
119+
log.info("Order placed successfully!");
120+
} catch (Exception e) {
121+
log.info("Error placing order: {}", e.getMessage());
113122
}
123+
}
114124
}
Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,52 @@
11
package com.iluwatar.monolithic.controller;
2-
3-
import com.iluwatar.monolithic.exceptions.NonExistentUserException;
4-
import com.iluwatar.monolithic.exceptions.NonExistentProductException;
52
import com.iluwatar.monolithic.exceptions.InsufficientStockException;
6-
import com.iluwatar.monolithic.model.User;
3+
import com.iluwatar.monolithic.exceptions.NonExistentProductException;
4+
import com.iluwatar.monolithic.exceptions.NonExistentUserException;
75
import com.iluwatar.monolithic.model.Orders;
86
import com.iluwatar.monolithic.model.Products;
7+
import com.iluwatar.monolithic.model.User;
98
import com.iluwatar.monolithic.repository.OrderRepo;
109
import com.iluwatar.monolithic.repository.ProductRepo;
1110
import com.iluwatar.monolithic.repository.UserRepo;
1211
import org.springframework.stereotype.Service;
13-
12+
/**
13+
* OrderCon is a controller class for managing Order operations.
14+
* */
1415
@Service
1516
public class OrderCon {
16-
private final OrderRepo orderRepository;
17-
private final UserRepo userRepository;
18-
private final ProductRepo productRepository;
19-
17+
private final OrderRepo orderRepository;
18+
private final UserRepo userRepository;
19+
private final ProductRepo productRepository;
20+
/**
21+
* This function handles the initializing of the controller.
22+
* */
2023
public OrderCon(OrderRepo orderRepository, UserRepo userRepository, ProductRepo productRepository) {
2124
this.orderRepository = orderRepository;
2225
this.userRepository = userRepository;
2326
this.productRepository = productRepository;
2427
}
25-
28+
/**
29+
* This function handles placing orders with all of its cases.
30+
* */
2631
public Orders placeOrder(Long userId, Long productId, Integer quantity) {
27-
User user = userRepository.findById(userId).orElse(null);
28-
if (user == null) {
29-
throw new NonExistentUserException("User with ID " + userId + " not found");
30-
}
32+
User user = userRepository.findById(userId).orElse(null);
33+
if (user == null) {
34+
throw new NonExistentUserException("User with ID " + userId + " not found");
35+
}
3136

32-
Products product = productRepository.findById(productId).orElse(null);
33-
if (product == null) {
34-
throw new NonExistentProductException("Product with ID " + productId + " not found");
35-
}
37+
Products product = productRepository.findById(productId).orElse(null);
38+
if (product == null) {
39+
throw new NonExistentProductException("Product with ID " + productId + " not found");
40+
}
3641

37-
if (product.getStock() < quantity) {
38-
throw new InsufficientStockException("Not enough stock for product " + productId);
39-
}
42+
if (product.getStock() < quantity) {
43+
throw new InsufficientStockException("Not enough stock for product " + productId);
44+
}
4045

41-
product.setStock(product.getStock() - quantity);
42-
productRepository.save(product);
46+
product.setStock(product.getStock() - quantity);
47+
productRepository.save(product);
4348

44-
Orders order = new Orders(null, user, product, quantity, product.getPrice() * quantity);
45-
return orderRepository.save(order);
46-
}
49+
Orders order = new Orders(null, user, product, quantity, product.getPrice() * quantity);
50+
return orderRepository.save(order);
51+
}
4752
}

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,33 @@
22

33
import com.iluwatar.monolithic.model.Products;
44
import com.iluwatar.monolithic.repository.ProductRepo;
5+
import java.util.List;
56
import org.springframework.stereotype.Service;
67

7-
import java.util.List;
8+
/**
9+
* ProductCon is a controller class for managing Product operations.
10+
* */
11+
812

913
@Service
1014
public class ProductCon {
11-
private final ProductRepo productRepository;
12-
15+
private final ProductRepo productRepository;
16+
/**
17+
* Linking Controller to DB.
18+
* */
1319
public ProductCon(ProductRepo productRepository) {
1420
this.productRepository = productRepository;
1521
}
16-
22+
/**
23+
* Adds a product to the DB.
24+
* */
1725
public Products addProduct(Products product) {
18-
return productRepository.save(product);
19-
}
20-
21-
public List<Products> getAllProducts() {
22-
return productRepository.findAll();
23-
}
26+
return productRepository.save(product);
27+
}
28+
/**
29+
* Returns all relevant Products.
30+
* */
31+
public List<Products> getAllProducts() {
32+
return productRepository.findAll();
33+
}
2434
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@
33
import com.iluwatar.monolithic.model.User;
44
import com.iluwatar.monolithic.repository.UserRepo;
55
import org.springframework.stereotype.Service;
6-
6+
/**
7+
* UserCon is a controller class for managing user operations.
8+
*/
79
@Service
810
public class UserCon {
9-
private final UserRepo userRepository;
10-
11+
private final UserRepo userRepository;
12+
/**
13+
* Linking Controller to DB.
14+
*/
1115
public UserCon(UserRepo userRepository) {
1216
this.userRepository = userRepository;
1317
}
14-
18+
/**
19+
* Adds a user to the DB.
20+
*/
1521
public User registerUser(User user) {
16-
return userRepository.save(user);
17-
}
22+
return userRepository.save(user);
23+
}
1824
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.iluwatar.monolithic.exceptions;
22

33
import java.io.Serial;
4-
5-
public class InsufficientStockException extends RuntimeException{
4+
/**
5+
* Custom Exception class for enhanced readability.
6+
* */
7+
public class InsufficientStockException extends RuntimeException {
68
@Serial
79
private static final long serialVersionUID = 1005208208127745099L;
8-
10+
/**
11+
* Exception Constructor that is readable through code and provides the message inputted into it.
12+
* */
913
public InsufficientStockException(String message) {
10-
super(message);
11-
}
14+
super(message);
15+
}
1216
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.iluwatar.monolithic.exceptions;
22

33
import java.io.Serial;
4-
5-
public class NonExistentProductException extends RuntimeException{
4+
/**
5+
* Custom Exception class for enhanced readability.
6+
* */
7+
public class NonExistentProductException extends RuntimeException {
68
@Serial
79
private static final long serialVersionUID = -593425162052345565L;
8-
9-
public NonExistentProductException(String msg){
10+
/**
11+
* Exception Constructor that is readable through code and provides the message inputted into it.
12+
* */
13+
public NonExistentProductException(String msg) {
1014
super(msg);
1115
}
1216
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.iluwatar.monolithic.exceptions;
22

33
import java.io.Serial;
4-
4+
/**
5+
* Custom Exception class for enhanced readability.
6+
* */
57
public class NonExistentUserException extends RuntimeException {
68
@Serial
79
private static final long serialVersionUID = -7660909426227843633L;
8-
10+
/**
11+
* Exception Constructor that is readable through code and provides the message inputted into it.
12+
* */
913
public NonExistentUserException(String msg) {
10-
super(msg);
11-
}
14+
super(msg);
15+
}
1216
}

0 commit comments

Comments
 (0)