Skip to content

Latest commit

 

History

History
256 lines (198 loc) · 6.74 KB

inventory_management.livemd

File metadata and controls

256 lines (198 loc) · 6.74 KB

ETS Inventory Management

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

ETS Inventory

You're going to build an in-memory inventory management application using :ets.

Example Solution
defmodule Inventory do
  def new(opts \\ []) do
    :ets.new(:inventory, opts)
  end

  def get_quantity(ref, item) do
    case :ets.lookup(ref, item) do
      [{_item, quantity}] -> quantity
      _ -> 0
    end
  end

  def store(ref, item, quantity) do
    existing_quantity = get_quantity(ref, item)
    :ets.insert(ref, {item, quantity + existing_quantity})
  end
end

Implement the Inventory module as documented.

defmodule Inventory do
  @moduledoc """
  Documentation for `Inventory`

  ## Examples

      Single quantity.

      iex> ref = Inventory.new()
      iex> Inventory.store(ref, :apples, 5)
      iex> Inventory.get_quantity(ref, :apples)
      5

      Multiple stores.

      iex> ref = Inventory.new()
      iex> Inventory.store(ref, :apples, 5)
      iex> Inventory.store(ref, :apples, 2)
      iex> Inventory.get_quantity(ref, :apples)
      7
  """
  @doc """
  Create a new Inventory :ets table.

  ## Examples

      iex> ref = Inventory.new()
      iex> is_reference(ref)
      true
  """
  def new() do
  end

  @doc """
  Retrieve the current quantity of an item in inventory.

  ## Examples
        
      iex> ref = Inventory.new()
      iex> Inventory.get_quantity(ref, :apples)
      0
  """
  def get_quantity(ref, item) do
  end

  @doc """
  Store an item in inventory.
  If item exists, add amount to existing total.

  ## Examples

      iex> ref = Inventory.new()
      iex> Inventory.store(ref, :apples, 5)
      true
  """
  def store(ref, item, quantity) do
  end
end

Bonus: Warehouse

You're going to build a warehouse management application using :ets. Now, instead of having a single quantity for each item, every item can be stored in multiple boxes, each with their own quantity of that item.

Hint

Consider using the :duplicate_bag Table Type.

Example Solution
defmodule Warehouse do
  def new() do
    :ets.new(:warehouse, [:duplicate_bag])
  end

  def get_quantity(ref, item) do
    :ets.lookup(ref, item)
  end

  def store(ref, item, quantity) do
    :ets.insert(ref, {item, quantity})
  end
end
defmodule Warehouse do
  @moduledoc """
  Documentation for `Warehouse`

  ## Examples

      Existing quantity.

      iex> ref = Warehouse.new()
      iex> Warehouse.store(ref, :apples, 5)
      iex> Warehouse.get_boxes(ref, :apples)
      [apples: 5]

      Multiple Boxes.

      iex> ref = Warehouse.new()
      iex> Warehouse.store(ref, :apples, 1)
      iex> Warehouse.store(ref, :apples, 3)
      iex> Warehouse.get_boxes(ref, :apples)
      [apples: 1, apples: 3]
  """

  @doc """
  Create a new Warehouse :ets table.

  ## Examples

      iex> ref = Warehouse.new()
      iex> is_reference(ref)
      true
  """
  def new(opts \\ []) do
  end

  @doc """
  Retrieve all boxes of an item and the associated quantity in each box.

  ## Examples

      iex> ref = Warehouse.new()
      iex> Warehouse.get_boxes(ref, :apples)
      []
  """
  def get_boxes(ref, item) do
  end

  @doc """
  Store an item in the warehouse.
  If item exists, add amount to existing total.

    ## Examples

      iex> ref = Warehouse.new()
      iex> Warehouse.store(ref, :apples, 5)
      true
  """
  def store(ref, item, quantity) do
  end
end

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish ETS Inventory Management exercise"
$ git push

We're proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation