Our client is an online marketplace, here is a sample of some of the products available on our site:
Product code | Name | Price |
---|---|---|
001 | Lavender heart | £9.25 |
002 | Personalised cufflinks | £45.00 |
003 | Kids T-shirt | £19.95 |
004 | Coffee | £11.23 |
Our marketing team want to offer promotions as an incentive for our customers to purchase these items.
- When people spend over £60, they get 10% of the whole purchase.
- When people buy 2 or more lavender hearts then the price drops to £8.50.
- When people buy 2 coffees, they get one for free.
Our check-out can scan items in any order, and because our promotions will change, it needs to be flexible regarding our promotional rules.
The interface to our checkout looks like this (shown in Ruby):
co = Checkout.new(promotional_rules)
co.scan(item)
co.scan(item)
price = co.total
Using plain Ruby, implement a checkout system that fulfills these requirements.
Basket: 001,002,003
Total price expected: £66.78
Basket: 001,003,001
Total price expected: £36.95
Basket: 001,002,001,003
Total price expected: £73.76
Basket: 004,004
Total price expected: £11.23
Basket: 001,002,001,004,003,004
Total price expected: £83.86
$ irb -r ./lib/checkout.rb
rules = [
Rules::BulkDiscount.new(item: ItemStorage.find(001), minimum_quantity: 2, price_reduction: 0.75),
Rules::Discount.new(percentage: 0.10, minimum_spent: 60)
]
checkout = Checkout.new(rules)
checkout.scan(001)
checkout.scan(002)
checkout.scan(003)
checkout.total
=> 66.78
rules = [
Rules::BulkDiscount.new(item: ItemStorage.find(001), minimum_quantity: 2, price_reduction: 0.75),
Rules::Discount.new(percentage: 0.10, minimum_spent: 60)
]
checkout = Checkout.new(rules)
checkout.scan(001)
checkout.scan(003)
checkout.scan(001)
checkout.total
=> 36.95
rules = [
Rules::BulkDiscount.new(item: ItemStorage.find(001), minimum_quantity: 2, price_reduction: 0.75),
Rules::Discount.new(percentage: 0.10, minimum_spent: 60)
]
checkout = Checkout.new(rules)
checkout.scan(001)
checkout.scan(002)
checkout.scan(001)
checkout.scan(003)
checkout.total
=> 73.76
rules = [
Rules::BulkDiscount.new(item: ItemStorage.find(001), minimum_quantity: 2, price_reduction: 0.75),
Rules::Discount.new(percentage: 0.10, minimum_spent: 60),
Rules::BuyOneGetOneFree.new(item: ItemStorage.find(004), minimum_quantity: 2)
]
checkout = Checkout.new(rules)
subject.scan(001)
subject.scan(002)
subject.scan(001)
subject.scan(004)
subject.scan(003)
subject.scan(004)
checkout.total
=> 83.86
This project was created using ruby 2.7.2 (see .ruby-version).
I use rbenv to install different ruby versions, you may need to install homebrew.
$ brew install ruby-build rbenv
$ rbenv install
$ gem install bundler
$ bundle install
$ rspec
$ bundle exec rubocop
If you want to correct all cops
$ bundle exec rubocop -A
After running the rspec command, a new directory is created to show test coverage
$ open coverage/index.html