So here is my code doing so far:
defmodule Shopping do
def shop(list) do
products = %{orange: 5,
newspaper: 8,
apple: 2,
pear: 9,
milk: 7}
shop(products, list, 0)
end
def shop(_products, [], total) do
total
end
def shop(products, [{product,qty} | list], total) do
{:ok, price} = Map.fetch(products, product)
count = shop(products, list, qty*price)
totalall = shop(products, list, total + qty*price)
IO.puts product ++ count ++ qty ++ totalall
end
end
IO.puts Shopping.shop([{:orange,4},{:newspaper,1}])
and i try to get a result with
[{qty1, itemname1,price1,qty1*price1},{qty2, itemname2,price2,qty2*price2},{:total, pricetotal}]
like this:
iex(1) Shopping.shop([{:orange,4},{:newspaper,1}])
[{4, :orange, 5, 20},{1, :newspaper, 8, 8},{:total, 28}]
thanks for any help