jm2dev / shopping-cart

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shopping cart

Overview

You are building a checkout system for a shop which only sells apples and oranges.

Step 1

Apples cost 60p and oranges cost 25p.

Build a checkout system which takes a list of items scanned at the till and outputs the total cost

For example: [ Apple, Apple, Orange, Apple ] => £2.05

Make reasonable assumptions about the inputs to your solution; for example, many candidates take a list of strings as input.

Step 2

The shop decides to introduce two new offers

  • buy one, get one free on Apples
  • 3 for the price of 2 on Oranges

Update your checkout functions accordingly

Assumptions

Input should a comma separated list of oranges (‘Orange’) and apples (‘Apple). Anything else will be reported as an error to appear instead of the cost.

Requirements

  • java 8 sdk
  • sbt

Usage

sbt clean test

Playing with the REPL

$ sbt console
[info] Loading project definition from /home/jm/ws-github/shopping-cart/project
[info] Set current project to Shopping Cart (in build file:/home/jm/ws-github/shopping-cart/)
[info] Starting scala interpreter...
[info]
Welcome to Scala 2.11.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_72).
Type in expressions for evaluation. Or try :help.

scala> val input = "Orange,Apple,Orange,Orange"
input: String = Orange,Apple,Orange,Orange

scala> val cart = new com.jm2dev.hmrc.ShoppingCart(input)
cart: com.jm2dev.hmrc.ShoppingCart = com.jm2dev.hmrc.ShoppingCart@2c20410e

scala> cart.total
res0: String = £1.10

About


Languages

Language:Scala 100.0%