diff --git a/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/.formatter.exs b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/.gitignore b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/.gitignore new file mode 100644 index 0000000..98f70cd --- /dev/null +++ b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/.gitignore @@ -0,0 +1,24 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +cart-*.tar + diff --git a/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/README.md b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/README.md new file mode 100644 index 0000000..7c8ab3a --- /dev/null +++ b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/README.md @@ -0,0 +1,21 @@ +# Cart + +**TODO: Add description** + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed +by adding `cart` to your list of dependencies in `mix.exs`: + +```elixir +def deps do + [ + {:cart, "~> 0.1.0"} + ] +end +``` + +Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) +and published on [HexDocs](https://hexdocs.pm). Once published, the docs can +be found at [https://hexdocs.pm/cart](https://hexdocs.pm/cart). + diff --git a/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/lib/cart.ex b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/lib/cart.ex new file mode 100644 index 0000000..a699b0f --- /dev/null +++ b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/lib/cart.ex @@ -0,0 +1,57 @@ +defmodule Cart do + @moduledoc """ + Documentation for Cart. + """ + + def show_cart(events) when is_list(events) do + Enum.reverse(events) + |> Enum.reduce( + [], + fn {event, {product, qty, price}}, cart -> + adjustment = calculate_adjustment(event, qty) + [{_product, {qty, _price}} | rest] = move_to_head(cart, product) + + [{product, {qty + adjustment, price}} | rest] + |> remove_item_with_zero_qty() + end + ) + end + + def calculate_adjustment(:item_added, qty) do + qty + end + + def calculate_adjustment(:item_removed, qty) do + -1 * qty + end + + def move_to_head(cart, product) do + item_to_move = Enum.find(cart, {product, {0, 0.00}}, fn {item, _} -> item == product end) + rest_of_items = Enum.filter(cart, fn {item, _} -> item != product end) + [item_to_move | rest_of_items] + end + + def remove_item_with_zero_qty(cart) do + Enum.filter(cart, fn {_product, {qty, _price}} -> qty > 0 end) + end + + def get_total(events) do + show_cart(events) |> Enum.reduce(0, fn {_product, {qty, price}}, sum -> sum + qty * price end) + end + + def get_count(events) do + show_cart(events) |> Enum.reduce(0, fn {_product, {qty, _price}}, sum -> sum + qty end) + end + + def get_added_items(events) do + events + |> Enum.filter(fn {event_type, _} -> event_type == :item_added end) + |> Enum.reduce([], fn {_event, {product, _, _}}, products -> [product | products] end) + end + + def get_removed_items(events) do + events + |> Enum.filter(fn {event_type, _} -> event_type == :item_removed end) + |> Enum.reduce([], fn {_event, {product, _, _}}, products -> [product | products] end) + end +end diff --git a/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/mix.exs b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/mix.exs new file mode 100644 index 0000000..1c7b315 --- /dev/null +++ b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/mix.exs @@ -0,0 +1,28 @@ +defmodule Cart.MixProject do + use Mix.Project + + def project do + [ + app: :cart, + version: "0.1.0", + elixir: "~> 1.10", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/test/cart_test.exs b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/test/cart_test.exs new file mode 100644 index 0000000..176ac11 --- /dev/null +++ b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/test/cart_test.exs @@ -0,0 +1,123 @@ +defmodule CartTest do + use ExUnit.Case + + test "show_cart(events) returns an empty list for an empty cart" do + events = [] + assert Cart.show_cart(events) == [] + end + + test "show_card(events) returns an empty list for a cart that has item added and then removed" do + random_qty = Enum.random(1..100) + + events = [ + {:item_removed, {:dinosaur_egg, random_qty, 125.00}}, + {:item_added, {:dinosaur_egg, random_qty, 125.00}} + ] + + assert Cart.show_cart(events) == [] + end + + test "show_cart(events) returns a list of current items in the right form" do + items = [ + {:iguana_leash, {4, 12.00}}, + {:zombie_repelent, {12, 3.00}} + ] + + events = [ + {:item_added, {:iguana_leash, 3, 12.00}}, + {:item_removed, {:iguana_leash, 1, 12.00}}, + {:item_added, {:iguana_leash, 2, 12.00}}, + {:item_added, {:zombie_repelent, 12, 3.00}} + ] + + assert Cart.show_cart(events) == items + end + + test "get_total(events) returns a zero for an empty cart" do + events = [] + assert Cart.get_total(events) == 0.00 + end + + test "get_total(events) returns a floating point number for the total cost of all products in the cart" do + events = [ + {:item_added, {:iguana_leash, 3, 12.00}}, + {:item_added, {:zombie_repelent, 12, 3.00}} + ] + + assert Cart.get_total(events) == 72.00 + end + + test "get_count(events) returns a zero for an empty cart" do + events = [] + assert Cart.get_count(events) == 0 + end + + test "get_count(events) returns a number of items in a cart" do + events = [ + {:item_added, {:iguana_leash, 3, 12.00}}, + {:item_added, {:zombie_repelent, 12, 3.00}} + ] + + assert Cart.get_count(events) == 15 + end + + test "get_added_items(events) returns an empty list for an empty cart" do + events = [] + assert Cart.get_added_items(events) == [] + end + + test "get_added_items(events) returns an empty list for a cart with only removed items" do + events = [ + {:item_removed, {:iguana_leash, 3, 12.00}}, + {:item_removed, {:zombie_repelent, 12, 3.00}} + ] + + assert Cart.get_added_items(events) == [] + end + + test "get_added_items(events) returns a list of added items for a cart" do + events = [ + {:item_added, {:iguana_leash, 3, 12.00}}, + {:item_removed, {:iguana_leash, 3, 12.00}}, + {:item_added, {:iguana_leash, 3, 12.00}}, + {:item_added, {:zombie_repelent, 12, 3.00}}, + {:item_removed, {:zombie_repelent, 5, 3.00}}, + {:item_added, {:zombie_repelent, 12, 3.00}}, + {:item_added, {:iguana_leash, 3, 12.00}} + ] + + items = [:iguana_leash, :zombie_repelent, :zombie_repelent, :iguana_leash, :iguana_leash] + + assert Cart.get_added_items(events) == items + end + + test "get_removed_items(events) returns an empty list for an empty cart" do + events = [] + assert Cart.get_removed_items(events) == [] + end + + test "get_removed_items(events) returns an empty list for a cart with only added items" do + events = [ + {:item_added, {:iguana_leash, 3, 12.00}}, + {:item_added, {:zombie_repelent, 12, 3.00}} + ] + + assert Cart.get_removed_items(events) == [] + end + + test "get_removed_items(events) returns a list of items removed form a cart" do + events = [ + {:item_added, {:iguana_leash, 3, 12.00}}, + {:item_removed, {:iguana_leash, 3, 12.00}}, + {:item_added, {:iguana_leash, 3, 12.00}}, + {:item_added, {:zombie_repelent, 12, 3.00}}, + {:item_removed, {:zombie_repelent, 5, 3.00}}, + {:item_added, {:zombie_repelent, 12, 3.00}}, + {:item_added, {:iguana_leash, 3, 12.00}} + ] + + items = [:zombie_repelent, :iguana_leash] + + assert Cart.get_removed_items(events) == items + end +end diff --git a/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/test/test_helper.exs b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()