DEV Community

loading...

Discussion on: Daily Challenge #56 - Coffee Shop

Collapse
choroba profile image
E. Choroba

Erlang solution with tests.

-module(coffee).
-include_lib("eunit/include/eunit.hrl").
-export([coffee/1]).

coffee(X) ->
    P = coffee_price(X),
    case P of
        false -> "Sorry, exact change only, try again tomorrow!";
        _     -> "Here is your " ++ P ++ ", have a nice day!"
    end.

coffee_price(2.20) -> "Americano";
coffee_price(2.30) -> "Latte";
coffee_price(2.40) -> "Flat white";
coffee_price(3.50) -> "Filter";
coffee_price(_)    -> false.

americano_test() ->
    ?assert(coffee(2.20) == "Here is your Americano, have a nice day!").
latte_test() ->
    ?assert(coffee(2.30) == "Here is your Latte, have a nice day!").
flat_white_test() ->
    ?assert(coffee(2.40) == "Here is your Flat white, have a nice day!").
filter_test() ->
    ?assert(coffee(3.50) == "Here is your Filter, have a nice day!").
invalid_test() ->
    ?assert(coffee(4.90) == "Sorry, exact change only, try again tomorrow!").