This project is a basic order processing system for an e-commerce platform. It handles customer orders containing various item types, such as physical products, digital products, and gift cards. The solution focuses on object-oriented principles such as abstraction, inheritance, and polymorphism, offering a clean and extendable design.
- Abstraction: The
OrderItem
class is an abstract class representing common attributes and methods shared among all types of order items. - Inheritance:
PhysicalProduct
,DigitalProduct
, andGiftCard
classes extendOrderItem
to inherit common behaviors while implementing specific functionalities. - Polymorphism: Each subclass (
PhysicalProduct
,DigitalProduct
,GiftCard
) overrides methods likegetTotalPrice()
as needed to support different item-specific behaviors.
OrderItem
(abstract class):- Attributes:
productName
,price
,quantity
. - Methods:
double getTotalPrice()
.
- Attributes:
PhysicalProduct
:- Additional Attribute:
shippingWeight
. - Method:
double calculateShippingCost()
.
- Additional Attribute:
DigitalProduct
:- Method:
String generateDownloadLink()
.
- Method:
GiftCard
:- Additional Attribute:
recipientEmail
. - Method:
void sendGift()
.
- Additional Attribute:
Order
:- Attributes:
orderId
,customerId
,List<OrderItem> items
. - Method:
void addItem(OrderItem item)
,double calculateTotalCost()
,double calculateTotalShippingCost()
.
- Attributes:
SummaryDTO
:- Attributes:
orderId
,totalCost
,shippingCost
,items
. - Represents the summary of an order with all calculated costs.
- Attributes:
src/main/java/com/bella/ecommerce/
├── EcommerceApplication.java
├── models/
│ ├── OrderItem.java
│ ├── PhysicalProduct.java
│ ├── DigitalProduct.java
│ ├── GiftCard.java
│ └── Order.java
├── controllers/
│ └── OrderController.java
│ └── MainController.java
├── repositories/
│ └── OrderRepository.java
│ └── OrderItemRepository.java
├── services/
│ └── OrderService.java
│ └── impl/
│ └── OrderServiceImpl.java
├── utils/
│ └── ApiResponse.java
├── exceptions/
│ └── BadRequestException.java
│ └── GlobalExceptionHandler.java
├── validators/
│ └── EmailValidator.java
│ └── TypeValidator.java
│ └── ValidEmail.java
│ └── ValidType.java
└── dtos/
├── CreateOrderDTO.java
└── CreateOrderItemDTO.java
└── OrderItemDTO.java
└── SummaryDTO.java
- HTTP Method:
POST
- URL:
/orders
- Request Body:
{ "customerId": "12345" }
- Response:
{ "message": "Order created successfully", "status": "CREATED", "success": true, "timestamp": "2024-10-17T02:28:01.240286" }
- Response:
- HTTP Method:
POST
- URL:
/orders/{orderId}/items
- Request Body:
{ "type": "physical", "productName": "Laptop", "price": 100000, "quantity": 2, "shippingWeight": 1.5 }
- URL:
- Response:
{ "message": "Item added to order successfully", "status": "CREATED", "success": true, "timestamp": "2024-10-17T02:28:01.240286" }
- HTTP Method:
GET
- URL:
/orders/{orderId}
- Response:
{ "data": { "customerId": "12345", "totalCost": 200000.0, "totalShippingCost": 2250.0, "grandTotal": 202250.0, "items": [ { "type": "physical", "id": 1, "productName": "Laptop", "price": 100000.0, "quantity": 2, "shippingWeight": 1.5, "totalPrice": 200000.0 }, { "type": "digital", "id": 2, "productName": "E-book: Mastering Java", "price": 15000.0, "quantity": 1, "totalPrice": 15000.0, "downloadLink": "http://example.com/downloads/12345" } ] }, "message": "Order summary retrieved successfully", "status": "OK", "success": true, "timestamp": "2024-10-17T02:29:09.414690" }
- Response:
- Abstract
OrderItem
Class: TheOrderItem
class is abstract to ensure that no generic order item can be created. This ensures that every item added to an order is either aPhysicalProduct
,DigitalProduct
, orGiftCard
, each with its own specific behavior. - Use of Polymorphism: The design enables flexibility when calculating the total price for each item, as each type can override the
getTotalPrice()
method if necessary. - Separation of Concerns: The
OrderServiceImpl
handles business logic related to order creation, item addition, and order summary retrieval, while theOrderController
manages HTTP requests and responses. - Scalability: The use of interfaces and abstract classes ensures that new item types can be added with minimal changes to the existing code. For instance, adding a new item type like
SubscriptionProduct
would involve creating a new class extendingOrderItem
and implementing specific methods.
-
Install PostgreSQL if you haven't already, and create a database:
CREATE DATABASE ordersystem;
-
Configure database connection in
src/main/resources/application.properties
:spring.datasource.url=jdbc:postgresql://localhost:5432/ordersystem spring.datasource.username=your_db_username spring.datasource.password=your_db_password spring.jpa.hibernate.ddl-auto=create spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect server.port=8090
-
Run database migrations (if using tools like Flyway or Liquibase) or allow Hibernate to create tables automatically based on your entity classes.
-
Clone the repository:
git clone https://github.com/Bellamy01/ecommerce.git cd ecommerce
-
Build the project using Maven:
mvn clean install
-
Run the application:
mvn spring-boot:run
-
The application will run on http://localhost:8090.
-
Using Postman:
- Method:
POST
- URL:
http://localhost:8090/orders
- Body (JSON):
{ "customerId": "12345" }
- Click "Send" and verify the response.
- Method:
-
Using curl:
curl -X POST http://localhost:8090/orders -H "Content-Type: application/json" -d '{"customerId": "12345"}'
-
Using Postman:
- Method:
POST
- URL:
http://localhost:8090/orders/1/items
- Body (JSON):
{ "type": "PHYSICAL", "productName": "Laptop", "price": 1000.0, "quantity": 1, "shippingWeight": 2.5 }
- Click "Send" and verify the response.
- Method:
-
Using curl:
curl -X POST http://localhost:8090/orders/1/items -H "Content-Type: application/json" -d '{"type": "PHYSICAL", "productName": "Laptop", "price": 1000.0, "quantity": 1, "shippingWeight": 2.5}'
-
Using Postman:
- Method:
GET
- URL:
http://localhost:8090/orders/1
- Click "Send" and verify the response.
- Method:
-
Using curl:
curl -X GET http://localhost:8090/orders/1
Feel free to add any additional information or context about the project here. You can discuss the challenges you faced, the lessons you learned, or the next steps you plan to take.