Skip to lesson

Project: ShopLite order flow baseline design

A good baseline design keeps each job in its own class and puts an interface wherever change is coming, so later patterns slot in without a rewrite. This ShopLite order flow is that baseline for the whole section.

What you'll build

Your program models ShopLite's order flow in one Java file. A customer fills a Cart, OrderService turns it into an Order, PriceCalculator adds the delivery fee, a PaymentGateway charges the amount and a NotificationService sends the confirmation by email and SMS. A declined payment and an empty cart are handled cleanly.

The design is the point: each class has one job (SRP), OrderService receives interfaces through its constructor (DIP), notifications are composed from channels, and only main names concrete classes. The creational, structural and behavioural module projects grow this same file in the same java-shoplite-patterns repository; the later projects start their own repositories. Amounts use double to keep the code short; real payment code uses BigDecimal.

Sample run
=== ShopLite order flow ===
[card] charged Rs 1799.00 for ORD-1001
[email to asha@mail.com] ORD-1001 confirmed, paid Rs 1799.00
[sms to 9876543210] ORD-1001 confirmed, paid Rs 1799.00
ORD-1001 | Asha | 3 units | PAID
---
[card] charged Rs 400.00 for ORD-1002
[email to ravi@mail.com] ORD-1002 confirmed, paid Rs 400.00
[sms to 9123456780] ORD-1002 confirmed, paid Rs 400.00
ORD-1002 | Ravi | 3 units | PAID
---
[card] declined Rs 5998.00 for ORD-1003
[email to meena@mail.com] ORD-1003 payment of Rs 5998.00 failed, please retry
[sms to 9988776655] ORD-1003 payment of Rs 5998.00 failed, please retry
ORD-1003 | Meena | 2 units | PAYMENT_FAILED
---
Not placed for Kiran: cart is empty
---

Requirements

  • 1A Cart creates its own CartItem objects and rejects a quantity below 1.
  • 2An Order copies the cart items, rejects an empty list with cart is empty, and gives its items total and units.
  • 3PriceCalculator alone knows the delivery rule: free from Rs 499, otherwise Rs 40.
  • 4PaymentGateway and NotificationService are interfaces; OrderService receives them, and the pricing class, through its constructor.
  • 5A successful payment marks the order PAID and sends a confirmation; a declined one marks it PAYMENT_FAILED and sends a retry message.
  • 6Notifications go out on every channel through a MultiChannelNotifier that holds a list of NotificationService objects.
  • 7Order ids run from ORD-1001, and a rejected empty cart does not use up a number.
  • 8main is the composition root and the only place that names concrete classes.

Milestones

0/5 done · commit and push after each one
  1. Model products, the cart and the order

    A cart with Earbuds and two pen sets gives an order whose items total is 1799.0 and units are 3.

  2. Put pricing in its own class

    payable returns 1799.0 for Asha's order and 400.0 for three notebooks at Rs 120.

  3. Add the payment and notification seams

    A FakeCardGateway(5000) approves Rs 1799 and declines Rs 5998, and one send call prints an email line and an SMS line.

  4. Coordinate in OrderService and wire it in main

    The program prints the full sample run, including the declined order and the empty cart.

  5. Sketch the UML and run the self-check

    Your README has a class diagram and a list of what will change and where its seam is.

Build it in your own GitHub repository and push after each milestone. When everything works, mark the project as complete.

Extra challenges (optional)

Do any you like, each on its own branch, so your main project keeps working.

  1. Add a WhatsAppNotification channel and turn it on for every order.· easy

  2. Add a main-method test with fakes: a gateway that always declines and a notifier that records messages in a list. Check the status and the exact message.· medium

  3. Add a coupon: Cart can hold an optional coupon code, and PriceCalculator takes Rs 100 off for FEST100 when the items total is at least Rs 999.· medium

Solution

Opens when you add your repository link.

Finish up

Mark complete & continue

Next: Singleton: one instance on purpose