Ruby Technical Test
Dependencies
- Ruby 4.0.6
- Minitest 6.0.6
Instructions
Checkout class is in checkout.rb
Run tests with ruby -Ilib:test tests.rb
main.rb is a scratchpad
Initial Thoughts
- Cart can be stored in an array of strings
- Cart items should be validated against a known list of products
- We need a hash of products and their prices
Implementation Details
Cart items are added via symbol, and their prices are simply stored in a hash constant on the Checkout class iteself.
Item validation is a simple lookup in the AVAILABLE_ITEMS hash, and a custom exception is raised if the item is not found.
Discounts are calculated separately from the running total of the checkout and applied afterwards.
Discounts are simple calculations done in-line, again for simplicity
Future Work
Some basic stuff is missing.
- Checkout items are not grouped when listed
- Subtotals are not listed anywhere
- Discount totals are not assoiated with the items they apply to (or available to the user at all, they're just applied to the total silently)
The first thing I'd want to do is make the items real objects. You'd instantiate them from templates and apply discounts directly to them (e.g. -0.50 per Pico when there are enough in the cart)
We'd need a new discount method to go along with that. Each discount type would be its own class that you'd pass the checkout items to and it would mark the discounts on them as appropriate. Maybe even leave a reference back to the discount used.
Then we could do a lot more with displaying the checkout to the user, including the features listed in the bullets above.
Beyond that there are a lot of directions we could go. Happy to explore this later.
Thanks for reading!