Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akimboyko/72f8e2c8be4a3131defe to your computer and use it in GitHub Desktop.
Save akimboyko/72f8e2c8be4a3131defe to your computer and use it in GitHub Desktop.
Terminal Kata
Implement a solution to the following problem. We are looking for clean, well-factored, OO code.
You do not need to provide any form of persistence in this program. Your project should contain some way of running automated tests to prove it works.
The program should be an API. You can opt to put a user interface on it or not, but we will only be looking at the API portion.
Here are the requirements:
Consider a grocery market where items have prices per unit but also volume prices. For example, doughnuts may be $1.25 each or 3 for $3 dollars. There could only be a single volume discount per product.
Implement a point-of-sale scanning API that accepts an arbitrary ordering of products (similar to what would happen when actually at a checkout line) then returns the correct total price for an entire shopping cart based on the per unit prices or the volume prices as applicable.
Here are the products listed by code and the prices to use (there is no sales tax):
Product Code Price
--------------- ---------------
A $1.25 each or 3 for $3.00
B $4.25
C $1.00 or $5 for a six pack
D $0.75
The interface at the top level PointOfSaleTerminal service object should look something like this. You are free to design/implement the rest of the code however you wish, including how you specify the prices in the system:
PointOfSaleTerminal terminal = new PointOfSaleTerminal();
terminal.SetPricing(...);
terminal.Scan("A");
terminal.Scan("C");
... etc.
double result = terminal.CalculateTotal();
Here are the minimal inputs you should use for your test cases. These test cases must be shown to work in your program:
Scan these items in this order: ABCDABA; Verify the total price is $13.25.
Scan these items in this order: CCCCCCC; Verify the total price is $6.00.
Scan these items in this order: ABCD; Verify the total price is $7.25
Update:
Add total discount for units, not volume
namespace Terminal
open System
open System.Collections.Immutable
[<Measure>] type USD
[<Measure>] type Volume
[<Measure>] type Percent
type Price =
| PricePerUnit of decimal<USD>
| PricePerUnitAndVolume of decimal<USD> * decimal<USD> * int<Volume>
[<CustomEquality; NoComparison>]
type Total =
{
unitPrice : decimal<USD>
volumePrice : decimal<USD>
}
member this.WithDiscount(percent: decimal<Percent>) =
if (0.00M<Percent> <= percent && percent <= 1.00M<Percent>) then
invalidArg "percent" (sprintf "Value passed in was %A." percent)
{
unitPrice =
Decimal.Round(
decimal(this.unitPrice * (1.00M - decimal percent / 100.00M)), 2)
* 1.00M<USD> // convert to unit of measure
volumePrice = this.volumePrice
}
override this.Equals(otherObj) =
match otherObj with
| :? Total as other ->
(this.unitPrice = other.unitPrice && this.volumePrice = other.volumePrice)
| :? decimal<USD> as other ->
other = (this.unitPrice + this.volumePrice)
| _ -> false
override this.GetHashCode() =
hash (this.unitPrice + this.volumePrice)
override this.ToString() =
(this.unitPrice + this.volumePrice).ToString()
static member (+) (a, b) =
{
unitPrice = a.unitPrice + b.unitPrice
volumePrice = a.volumePrice + b.volumePrice
}
type PointOfSaleTerminal
private (productWithPrices, productSet) =
new() = PointOfSaleTerminal(ImmutableDictionary.Empty, ImmutableDictionary.Empty)
member this.SetPricing(name, price) =
PointOfSaleTerminal(productWithPrices.Add(name, price), productSet)
member this.Scan(products: string) =
let foldFunction (productSet: ImmutableDictionary<obj, int<Volume>>) name =
let (sucess, value) = productSet.TryGetValue(name)
match sucess with
| false -> productSet.Add(name, 1<Volume>)
| true -> productSet.Remove(name).Add(name, value + 1<Volume>)
let updatedProductSet =
products.ToCharArray()
|> Seq.fold foldFunction productSet
PointOfSaleTerminal(productWithPrices, updatedProductSet)
member this.CalculateTotal =
let foldFunction total name =
let (priceSucess, price) = productWithPrices.TryGetValue(name)
let (volumeSucess, totalVolume) = productSet.TryGetValue(name)
if (not(priceSucess) || not(volumeSucess)) then
failwithf "Can't find price for product %A" name
total +
match price with
| PricePerUnit pricePerUnit ->
{
unitPrice = pricePerUnit * decimal totalVolume
volumePrice = 0.00M<USD>
}
| PricePerUnitAndVolume(pricePerUnit, pricePerVolume, volume) ->
{
unitPrice = pricePerUnit * decimal (totalVolume % volume)
volumePrice = pricePerVolume * decimal (totalVolume / volume)
}
productSet.Keys
|> Seq.fold foldFunction { unitPrice = 0.00M<USD>; volumePrice = 0.00M<USD> }
module Terminal.Tests
open System
open Xunit
open FsUnit.Xunit
open Terminal
let terminalAOnly =
(new PointOfSaleTerminal())
.SetPricing('A', PricePerUnitAndVolume (1.25M<USD>, 3.00M<USD>, 3<Volume>))
let terminalBOnly =
(new PointOfSaleTerminal())
.SetPricing('B', PricePerUnit 4.25M<USD>)
let terminalPredefined =
(new PointOfSaleTerminal())
.SetPricing('A', PricePerUnitAndVolume (1.25M<USD>, 3.00M<USD>, 3<Volume>))
.SetPricing('B', PricePerUnit 4.25M<USD>)
.SetPricing('C', PricePerUnitAndVolume (1.00M<USD>, 5.00M<USD>, 6<Volume>))
.SetPricing('D', PricePerUnit 0.75M<USD>)
[<Fact>]
let ``Initially Terminal without producs should return USD0.00 Total`` () =
let terminal = new PointOfSaleTerminal()
terminal.CalculateTotal |> should equal 0.00M<USD>
[<Fact>]
let ``Lets add product "B" with price $4.25 and get total for one unit`` () =
let terminal = terminalBOnly.Scan("B")
terminal.CalculateTotal |> should equal 4.25M<USD>
[<Fact>]
let ``Lets add product "B" with price $4.25 and get total for two units`` () =
let terminal = terminalBOnly.Scan("B").Scan("B")
terminal.CalculateTotal |> should equal 8.50M<USD>
[<Fact>]
let ``Lets add product "A" with price $1.25 each or 3 for $3.00 and get total for one unit`` () =
let terminal = terminalAOnly.Scan("A")
terminal.CalculateTotal |> should equal 1.25M<USD>
[<Fact>]
let ``Lets add product "A" with price $1.25 each or 3 for $3.00 and get total for two units`` () =
let terminal = terminalAOnly.Scan("A").Scan("A")
terminal.CalculateTotal |> should equal 2.50M<USD>
[<Fact>]
let ``Lets add product "A" with price $1.25 each or 3 for $3.00 and get discount for three units`` () =
let terminal = terminalAOnly.Scan("A").Scan("A").Scan("A")
terminal.CalculateTotal |> should equal 3.00M<USD>
[<Fact>]
let ``Lets add product "A" with price $1.25 each or 3 for $3.00 and get discount for four units`` () =
let terminal = terminalAOnly.Scan("A").Scan("A").Scan("A").Scan("A")
terminal.CalculateTotal |> should equal 4.25M<USD>
[<Fact>]
let ``Lets add product "A", "B", "C" and "D" with prices and get total price for "ABCDABA"`` () =
let terminal = terminalPredefined.Scan("ABCDABA")
terminal.CalculateTotal |> should equal 13.25M<USD>
[<Fact>]
let ``Lets add product "A", "B", "C" and "D" with prices and get total price for "CCCCCCC"`` () =
let terminal = terminalPredefined.Scan("CCCCCCC")
terminal.CalculateTotal |> should equal 6.00M<USD>
[<Fact>]
let ``Lets add product "A", "B", "C" and "D" with prices and get total price for "ABCD"`` () =
let terminal = terminalPredefined.Scan("ABCD")
terminal.CalculateTotal |> should equal 7.25M<USD>
[<Fact>]
let ``Lets skip rpoduct confiuration, add only product "A" and fail with unknow product "A"`` () =
let terminal = (new PointOfSaleTerminal()).Scan("A")
(fun () -> terminal.CalculateTotal |> ignore) |> should throw typeof<Exception>
[<Fact>]
let ``Lets add product "A", "B", "C" and "D" with prices and fail with unknow product "Z"`` () =
let terminal = terminalPredefined.Scan("Z")
(fun () -> terminal.CalculateTotal |> ignore)
|> should throw typeof<Exception>
[<Fact>]
let ``Lets sum two prices together using operator '+'`` () =
let price1 = { unitPrice = 1.00M<USD>; volumePrice = 2.00M<USD> }
let price2 = { unitPrice = 0.25M<USD>; volumePrice = 0.34M<USD> }
price1 + price2 |> should equal { unitPrice = 1.25M<USD>; volumePrice = 2.34M<USD> }
[<Fact>]
let ``Discount in percent could be only in range [0.00%; 1.00%]`` () =
let price1 = { unitPrice = 1.00M<USD>; volumePrice = 2.00M<USD> }
(fun () -> price1.WithDiscount(+2.00M<Percent>) |> ignore)
|> should throw typeof<ArgumentException>
[<Fact>]
let ``Lets add product "B" with price $4.25 and get total with discount 10% for one unit`` () =
let terminal = terminalBOnly.Scan("B")
terminal.CalculateTotal.WithDiscount(10.00M<Percent>) |> should equal 3.82M<USD>
[<Fact>]
let ``Lets add product "B" with price $4.25 and get total with discount 10% for two units`` () =
let terminal = terminalBOnly.Scan("B").Scan("B")
terminal.CalculateTotal.WithDiscount(10.00M<Percent>) |> should equal 7.65M<USD>
[<Fact>]
let ``Lets add product "A" with price $1.25 each or 3 for $3.00 and get total with discount 10% for one unit`` () =
let terminal = terminalAOnly.Scan("A")
terminal.CalculateTotal.WithDiscount(10.00M<Percent>) |> should equal 1.12M<USD>
[<Fact>]
let ``Lets add product "A" with price $1.25 each or 3 for $3.00 and get discount for volume of three units w/o total discount 10%`` () =
let terminal = terminalAOnly.Scan("AAA")
terminal.CalculateTotal.WithDiscount(10.00M<Percent>) |> should equal 3.00M<USD>
[<Fact>]
let ``Lets add product "A" with price $1.25 each or 3 for $3.00 and get discount for volume of three units and one unit with total discount 10%`` () =
let terminal = terminalAOnly.Scan("AAAA")
terminal.CalculateTotal.WithDiscount(10.00M<Percent>) |> should equal (3.00M<USD> + 1.12M<USD>)
[<Fact>]
let ``Lets add product "A", "B", "C" and "D" with prices and get total price with discount 10% for "ABCDABA"`` () =
let terminal = terminalPredefined.Scan("ABCDABA")
// 3xA = 3.00M<USD>
// 2xB = 7.65M<USD>
// 1xC = 0.90M<USD>
// 1xD = 0.67M<USD>
terminal.CalculateTotal.WithDiscount(10.00M<Percent>) |> should equal 12.22M<USD>
[<Fact>]
let ``Lets add product "A", "B", "C" and "D" with prices and get total price for with discount 10% "CCCCCCC"`` () =
let terminal = terminalPredefined.Scan("CCCCCCC")
// only one 'C' has discount 10%, others had volumn discount
terminal.CalculateTotal.WithDiscount(10.00M<Percent>) |> should equal 5.90M<USD>
[<Fact>]
let ``Lets add product "A", "B", "C" and "D" with prices and get total price for with discount 10% "ABCD"`` () =
let terminal = terminalPredefined.Scan("ABCD")
terminal.CalculateTotal.WithDiscount(10.00M<Percent>) |> should equal 6.52M<USD>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment