Skip to main content

Command Palette

Search for a command to run...

Questions

Updated
4 min readView as Markdown

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:

  1. Create a common method for making payments.

  2. Every payment method should have its own implementation.

  3. The customer should be able to change the payment method at runtime.

  4. CheckoutService should execute the selected payment method.

  5. Do not use if-else or switch to identify the payment type.

  6. Validate that the payment amount is greater than zero.

  7. A new payment method such as WalletPayment should be addable without modifying CheckoutService.

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

  1. Create a common fare-calculation strategy for all cab types.

  2. Every cab type must have its own fare-calculation implementation.

  3. The customer must be able to change the selected cab type at runtime.

  4. RideBookingService should calculate the fare using the currently selected strategy.

  5. Do not use if-else or switch to identify the cab type.

  6. Validate that:

    • Distance must be greater than zero.

    • Duration must be greater than zero.

    • The selected fare strategy must not be null.

  7. Display:

    • Selected cab type

    • Distance

    • Duration

    • Final fare

  8. A new cab type, such as LuxuryCab, should be addable without modifying RideBookingService.

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

  1. Create a common Document interface containing:
void generate(String content);
  1. Create the following implementations:
  • PDFDocument

  • WordDocument

  • ExcelDocument

  1. Create a DocumentFactory class containing:
Document createDocument(String documentType);
  1. DocumentFactory should create and return the appropriate document object.

  2. The client should not directly create objects such as:

new PDFDocument();
new WordDocument();
  1. If an invalid document type is provided, throw an appropriate exception.

  2. The content must not be null or 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.

More from this blog

Amit singh's blog

235 posts