Skip to content

Comments

Refactor discount and receipt systems using Strategy and Factory patterns#14

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-7
Draft

Refactor discount and receipt systems using Strategy and Factory patterns#14
Copilot wants to merge 4 commits intomainfrom
copilot/fix-7

Conversation

Copy link

Copilot AI commented Sep 3, 2025

This PR addresses the code organization and flexibility issues in the supermarket receipt system by implementing well-established design patterns across two key areas of the codebase.

Problem

The original codebase suffered from several code smells:

  1. Long Method: ShoppingCart.handleOffers() contained 40+ lines of complex conditional logic
  2. Feature Envy: Extensive access to external objects within discount calculations
  3. Open/Closed Principle Violations: Adding new discount types or receipt formats required modifying existing code
  4. Lack of HTML Receipt Support: Only plain text receipts were available

Solution

1. Strategy Pattern for Discount Calculations

Extracted the complex discount calculation logic from ShoppingCart.handleOffers() into focused strategy classes:

// Before: Complex nested conditionals
if (offer.offerType == SpecialOfferType.THREE_FOR_TWO) {
    // 8+ lines of calculation logic
} else if (offer.offerType == SpecialOfferType.TWO_FOR_AMOUNT) {
    // 6+ lines of calculation logic
} // ... more conditionals

// After: Clean strategy pattern
DiscountStrategy strategy = DiscountStrategyFactory.createStrategy(offer.offerType);
Discount discount = strategy.calculateDiscount(p, quantity, unitPrice, offer.argument);

Concrete Strategies Implemented:

  • ThreeForTwoDiscountStrategy
  • TenPercentDiscountStrategy
  • TwoForAmountDiscountStrategy
  • FiveForAmountDiscountStrategy

2. Factory Pattern for Strategy Creation

DiscountStrategyFactory centralizes the creation of discount strategies, eliminating conditional logic and making it easy to add new discount types.

3. Strategy Pattern for Receipt Formatting

Implemented a flexible receipt formatting system that supports multiple output formats:

// Text receipts (default)
ReceiptPrinter textPrinter = new ReceiptPrinter(new TextReceiptFormatter());

// HTML receipts (new feature)
ReceiptPrinter htmlPrinter = new ReceiptPrinter(new HtmlReceiptFormatter());

The HtmlReceiptFormatter includes proper HTML escaping and structured table output, addressing the HTML receipt feature request from the README.

Results

Code Quality Improvements:

  • Reduced ShoppingCart.handleOffers() from 40+ lines to 16 lines
  • Eliminated complex nested conditionals
  • Improved separation of concerns
  • Enhanced testability with focused, isolated classes

Functionality Enhancements:

  • Added HTML receipt generation capability
  • Maintained 100% backward compatibility
  • All existing functionality preserved

Testing:

  • 17 comprehensive tests covering all strategies and integration scenarios
  • Individual strategy testing for better isolation
  • End-to-end verification of discount calculations and receipt formatting

The refactoring demonstrates how multiple design patterns can work together to create a more maintainable, extensible, and robust codebase while preserving all existing functionality.

Fixes #7.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits September 3, 2025 11:28
Co-authored-by: nstubbe <20206435+nstubbe@users.noreply.github.com>
Co-authored-by: nstubbe <20206435+nstubbe@users.noreply.github.com>
Co-authored-by: nstubbe <20206435+nstubbe@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor to apply suitable design patterns Refactor discount and receipt systems using Strategy and Factory patterns Sep 3, 2025
Copilot AI requested a review from nstubbe September 3, 2025 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor to apply suitable design patterns

2 participants