Sha256: 4e268edc8876302950eaa4a5f6ee6a98e87e1ae41d462a08252bc2499e4392ab

Contents?: true

Size: 1.44 KB

Versions: 131

Compression:

Stored size: 1.44 KB

Contents

defmodule BankAccount do
  @moduledoc """
  A bank account that supports access from multiple processes.
  """

  use GenServer

  ## Callbacks

  def init(_args) do
    {:ok, 0}
  end

  def handle_call(:balance, _from, balance) do
    {:reply, balance, balance}
  end

  def handle_call({:update, amount}, _from, balance) do
    {:reply, :ok, balance + amount}
  end

  def handle_call(:close, _from, balance) do
    # We stop normally and return :stopped to the caller.
    {:stop, :normal, :stopped, balance}
  end

  ## Interface for tests

  @typedoc """
  An account handle.
  """
  @opaque account :: pid

  @doc """
  Open the bank. Makes the account available.
  """
  @spec open_bank() :: account
  def open_bank() do
    {:ok, pid} = GenServer.start_link(__MODULE__, [], [])
    pid
  end

  @doc """
  Close the bank. Makes the account unavailable.
  """
  @spec close_bank(account) :: any
  def close_bank(account) do
    GenServer.stop(account)
  end

  @doc """
  Get the account's balance.
  """
  @spec balance(account) :: integer
  def balance(account) do
    if Process.alive?(account) do
      GenServer.call(account, :balance)
    else
      {:error, :account_closed}
    end
  end

  @doc """
  Update the account's balance by adding the given amount which may be negative.
  """
  def update(account, amount) do
    if Process.alive?(account) do
      GenServer.call(account, {:update, amount})
    else
      {:error, :account_closed}
    end
  end
end

Version data entries

131 entries across 131 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/bank-account/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/bank-account/example.exs