This project is an e-commerce system designed to support basic functionality like adding products to a cart, checking out, and calculating total costs, including shipping. It demonstrates an inventory management system where products may have specific characteristics, such as expiring, being shippable, or requiring special handling.
- Product Management: Define products with attributes like name, price, and quantity, with support for expirable and shippable products.
- Shopping Cart: Add products to a cart with specific quantities, ensuring requested quantities are in stock.
- Checkout: Calculate order subtotal, shipping fees, and total paid amount. Display checkout details in the console.
- Error Handling:
- Error messages if the cart is empty, the customer's balance is insufficient, a product is out of stock, or expired.
- Collect all items that require shipping and send them to
ShippingServicefor processing.
- Product: Base class for all products, holding common attributes (
name,price,quantity). - ShippableProduct: Extends
Productand implements theShippableinterface, adding aweightattribute. For example, TV and Headphones are shippable. - ExpirableProduct: Extends
ShippableProductwith anexpiryDateattribute to represent perishable products. Examples include Cheese and Biscuits. - NonShippableProduct: Extends
Product, representing items that don’t require shipping, like Mobile Scratch Cards.
- CartItem: Represents a product added to the cart with a specific quantity. It checks stock availability and calculates the total price.
- Cart: Holds a list of
CartIteminstances, manages subtotal calculation, and validates product stock and expiration status.
- Checkout: Handles the checkout process by validating the cart, calculating totals, updating customer balance, and reducing product quantities. If items are shippable, it calculates shipping fees based on weight.
- Shipping: Processes all shippable items in the cart, calculating the total weight and displaying shipping information.
productsPackage:Product: Base class with attributes likename,price, andquantity.ShippableProduct: ExtendsProduct, addsweight, and implements theShippableinterface.ExpirableProduct: ExtendsShippableProduct, adding anexpiryDateandisExpired()method.NonShippableProduct: ExtendsProductfor items that don’t require shipping.
cartsPackage:CartItem: Represents a product in the cart with a specified quantity, providing methods to check stock and calculate total price.Cart: Manages a collection ofCartItems, providing subtotal calculation, stock validation, and expiration checks.
servicesPackage:Checkout: Contains the maincheckoutmethod, handling order processing and validation.Shipping: Processes all shippable items, calculating shipping costs based on item weight.
customersPackage:Customer: Represents a customer with attributes likename,address, andbalance. Provides methods to update balance.
// Initialize a customer and a cart
Customer customer = new Customer("Mohamed Eid", "Sadat City", 1500.00);
Cart cart = new Cart();
// Initialize inventory
// Non-expired product
LocalDate cheeseExpiryDate = LocalDate.of(2024, 12, 8);
ExpirableProduct cheese = new ExpirableProduct("Cheese", 30, 9, 0.5, cheeseExpiryDate);
// Expired product
LocalDate orangeExpiryDate = LocalDate.of(2024, 10, 29);
ExpirableProduct orange = new ExpirableProduct("Orange", 25, 12, 0.5, orangeExpiryDate);
// Shippable products
ShippableProduct headPhone = new ShippableProduct("HeadPhone", 350, 5, 0.15);
ShippableProduct tv = new ShippableProduct("TV", 2500, 5, 0.15);
// Non-shippable product
NonShippableProduct scratchCard = new NonShippableProduct("ScratchCard", 100, 20);
// Add items to cart
cart.add(headPhone, 1);
cart.add(headPhone, 1);
cart.add(cheese, 2);
cart.add(scratchCard, 1);
Checkout.checkout(customer, cart);Output:
** Shipment notice **
2x HeadPhone 300.0g
2x Cheese 1000.0g
Total package weight is 1.3Kg
** Checkout receipt **
2X HeadPhone 700.0
2X Cheese 60.0
1X ScratchCard 100.0
----------------------
Subtotal 860.0
Shipping 19.5
Amount 879.5
Example 2: Insufficient balance
// Add items to cart
cart.add(headPhone, 1);
cart.add(headPhone, 1);
cart.add(tv, 1);
cart.add(cheese, 2);
cart.add(scratchCard, 1);
Checkout.checkout(customer, cart);Output:
Exception in thread "main" java.lang.IllegalArgumentException:
Customer balance is insufficient.`
Example 3: Expired product
// Add items to cart
cart.add(headPhone, 1);
cart.add(headPhone, 1);
cart.add(orange, 2);
cart.add(cheese, 2);
cart.add(scratchCard, 1);
Checkout.checkout(customer, cart);Output:
Exception in thread "main" java.lang.IllegalArgumentException:
Some ordered items are Expired.
Example 4: Quantity exceeds stock
// Add items to cart
cart.add(headPhone, 1);
cart.add(headPhone, 1);
cart.add(cheese, 20);
cart.add(scratchCard, 1);
Checkout.checkout(customer, cart);` Output:
Exception in thread "main" java.lang.IllegalArgumentException:
Requested quantity is greater than stock.
Example 5: Empty cart checkout
cart.add(headPhone, 1);
cart.add(headPhone, 1);
cart.add(scratchCard, 1);
cart.clearCart();
Checkout.checkout(customer, cart);` Output:
Exception in thread "main" java.lang.IllegalArgumentException:
Cart Is Empty!