Project: ShopLite order flow baseline design
55 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)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.
=== 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
Cartcreates its ownCartItemobjects and rejects a quantity below 1. - 2An
Ordercopies the cart items, rejects an empty list withcart is empty, and gives its items total and units. - 3
PriceCalculatoralone knows the delivery rule: free from Rs 499, otherwise Rs 40. - 4
PaymentGatewayandNotificationServiceare interfaces;OrderServicereceives them, and the pricing class, through its constructor. - 5A successful payment marks the order
PAIDand sends a confirmation; a declined one marks itPAYMENT_FAILEDand sends a retry message. - 6Notifications go out on every channel through a
MultiChannelNotifierthat holds a list ofNotificationServiceobjects. - 7Order ids run from
ORD-1001, and a rejected empty cart does not use up a number. - 8
mainis the composition root and the only place that names concrete classes.
Milestones
0/5 done · commit and push after each oneModel 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.
Put pricing in its own class
payablereturns 1799.0 for Asha's order and 400.0 for three notebooks at Rs 120.Add the payment and notification seams
A
FakeCardGateway(5000)approves Rs 1799 and declines Rs 5998, and onesendcall prints an email line and an SMS line.Coordinate in OrderService and wire it in main
The program prints the full sample run, including the declined order and the empty cart.
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.
Add a
WhatsAppNotificationchannel and turn it on for every order.· easyAdd 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
Add a coupon:
Cartcan hold an optional coupon code, andPriceCalculatortakes Rs 100 off forFEST100when 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