/state-monad

Example usage of state monad from cats and comparison with implementation of logic in actors

Primary LanguageScalaApache License 2.0Apache-2.0

state-monad

Example usage of state monad from cats and comparison with implementation of logic in actors

Model

This application is modeling vending machine. Program interacts with user by simple command in terminal. User can:

  • insert coin
  • select product
  • withdraw money

Vending machine can give you a product, change or display message. Additionally vending machine is sending reports to owner like:

  • there is a lot of money
  • issues with expiry date
  • vending machine run out of product

Architecture


               ╭────────────╮     ╭────────────────╮
 User Input -> │  Actor     │  -> │ User Outputs   │
               │            │     ╰────────────────╯
               │  Logic is  │     ╭────────────────╮
               │  here      │  -> │ System reports │
               ╰────────────╯     ╰────────────────╯



Implementation

There are two implementations of vending machine:

Run app

Running application:

sbt "runMain vending.VendingMachineDemo"

You will be asked to choose implementation.

Interaction with vending machine:

+digit -> insert coins, for example: +3
digit  -> select number, for example: 2
-      -> resign and take your money
q      -> quit program

Tests

Tests are written for:

One test is ignored (should do not report if money box is almost full for a second time). This test is currently failing because feature is not implemented. You can enable this test and implement logic using Actor and state monad.

Where you should look

Take a look at code and think about following topics:

  • can logic be divided into smaller parts?
  • can logic be composed from smaller parts?
  • can logic can be reused?
  • is it easy to test code?
  • is it easy to add additional features?
  • how fast are test