Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6778247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:14:40+00:00 2026-05-26T16:14:40+00:00

Hi I was tasked with building an app with the following brief. Implement an

  • 0

Hi I was tasked with building an app with the following brief.

Implement an interface, which calculates prices based on promotional rules.

co = Checkout.new(promotional_rules)
co.scan(item)
co.scan(item)
price = co.total

Essentially, depending on the promotional rules set up, certain items are discounted accordingly.

I had some feedback on my code stating that I have encapsulated the group of promotional_rules and then exposed the rules as an array anyway – bad OO

I initially created a promotional_rules object, which contains an array of rules.

  def initialize
    @rules = []
  end

 def addrule(rule)
   @rules.push(rule)
 end

Then in my checkout object I have the promotional_rules object that has been setup and passed into the initializer. I loop through the array of rules contained in the promotional_rules object and apply them to the items scanned by the checkout object.

def initialize(promotionalrules=Promotionalrules.new)
  @promotionalrules = promotionalrules
end

....Other code

for rule in @promotionalrules.getrules
  for item in @items
    ##Execute rule on current item.
  end
end

Im not overly happy with my code…the loop with the loop etc. But am just looking for some help with encapsulation as Im not sure where I have gone wrong.

Any suggestions on good design patterns to apply to the brief would be beneficial also, as not too confident about the approach I took. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:14:40+00:00Added an answer on May 26, 2026 at 4:14 pm

    I’d guess that they’re complaining about this “exposure”:

    for rule in @promotionalrules.getrules
    

    that bleeds the internal rules from your Promotionalrules (which probably should be called PromotionalRules) out to the caller. The fix is to reverse your logic a little bit:

    class Promotionalrules
      #...
      # and possibly remove the getrules method completely
      def apply_to_item(item)
        # Apply @rules to item
      end
      #...
    end
    

    and then later:

    # I'm not sure how the rules and item interact so this "each" might
    # be a different iterator in reality
    @items.each { |i| @promotional_rules.apply_to_item(i) }
    

    The essential change is that you apply the ruleset as a whole to each item. This hides the ruleset implementation details and, as an extra bonus, allows you to easily support rules that depend on each other (“you can get discount X unless you’re using coupon Y” and things like that).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been tasked with building a new web project from scratch, with the
I am tasked with building a process which will compensate for replication delays on
I have been tasked with building a new system that models a virtual filesystem.
I have been tasked with building a new workflow system to handle our service
I've been tasked with building a very simple app that that has a series
I'm a web developer tasked with building a basic iOS app for internal use.
I am tasked with building an HTML/JavaScript application which is not networked (it runs
I'm building an iPhone app and I am tasked with automating the build to
I've been tasked with implementing a blacklist-based profanity filter for a Rails app. I
I have been tasked with building a simple web based system for managing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.