Questions
Question 1: Online Payment System
Design an online payment system using the Strategy Design Pattern.
The system should support:
UPI Payment
Credit Card Payment
Cash on Delivery
Requirements:
Create a common method for making payments.
Every payment method should have its own implementation.
The customer should be able to change the payment method at runtime.
CheckoutServiceshould execute the selected payment method.Do not use
if-elseorswitchto identify the payment type.Validate that the payment amount is greater than zero.
A new payment method such as
WalletPaymentshould be addable without modifyingCheckoutService.
Expected usage:
CheckoutService checkout =
new CheckoutService(new UPIPayment());
checkout.makePayment(2000);
checkout.setPaymentStrategy(
new CreditCardPayment());
checkout.makePayment(5000);
Expected output:
Rs. 2000 paid using UPI
Rs. 5000 paid using Credit Card
Question: Cab Fare Calculation System — Strategy Design Pattern
Design a cab fare calculation system using the Strategy Design Pattern.
A ride-booking application provides the following cab types:
Fare rules
| Cab Type | Base Fare | Distance Charge | Time Charge |
|---|---|---|---|
| Mini Cab | ₹40 | ₹12 per km | ₹1 per minute |
| Sedan Cab | ₹60 | ₹16 per km | ₹2 per minute |
| Shared Cab | ₹20 | ₹8 per km | ₹0.50 per minute |
The fare should be calculated using:
FinalFare=BaseFare+(Distance×DistanceRate)+(Duration×TimeRate)FinalFare = BaseFare + (Distance \times DistanceRate) + (Duration \times TimeRate)FinalFare=BaseFare+(Distance×DistanceRate)+(Duration×TimeRate)
Requirements
Create a common fare-calculation strategy for all cab types.
Every cab type must have its own fare-calculation implementation.
The customer must be able to change the selected cab type at runtime.
RideBookingServiceshould calculate the fare using the currently selected strategy.Do not use
if-elseorswitchto identify the cab type.Validate that:
Distance must be greater than zero.
Duration must be greater than zero.
The selected fare strategy must not be
null.
Display:
Selected cab type
Distance
Duration
Final fare
A new cab type, such as
LuxuryCab, should be addable without modifyingRideBookingService.
Expected usage
RideBookingService bookingService =
new RideBookingService(new MiniFareStrategy());
bookingService.calculateFare(10, 20);
bookingService.setFareStrategy(
new SedanFareStrategy());
bookingService.calculateFare(10, 20);
bookingService.setFareStrategy(
new SharedFareStrategy());
bookingService.calculateFare(10, 20);
Expected output
Selected cab: Mini Cab
Distance: 10.0 km
Duration: 20 minutes
Final fare: Rs. 180.0
Selected cab: Sedan Cab
Distance: 10.0 km
Duration: 20 minutes
Final fare: Rs. 260.0
Selected cab: Shared Cab
Distance: 10.0 km
Duration: 20 minutes
Final fare: Rs. 110.0
Constraint: Solve this problem using only the Strategy Design Pattern.
Question 3: Document Export System — Simple Factory
Design a document export system using the Simple Factory Pattern.
The system should support:
PDF Document
Word Document
Excel Document
Requirements
- Create a common
Documentinterface containing:
void generate(String content);
- Create the following implementations:
PDFDocumentWordDocumentExcelDocument
- Create a
DocumentFactoryclass containing:
Document createDocument(String documentType);
DocumentFactoryshould create and return the appropriate document object.The client should not directly create objects such as:
new PDFDocument();
new WordDocument();
If an invalid document type is provided, throw an appropriate exception.
The content must not be
nullor empty.
Expected usage
Document document =
DocumentFactory.createDocument("PDF");
document.generate("Factory Pattern Notes");
Document document2 =
DocumentFactory.createDocument("WORD");
document2.generate("Java Assignment");
Expected output
Generating PDF document: Factory Pattern Notes
Generating Word document: Java Assignment
Student task
Design the complete system and draw its UML diagram.