Example usage of state monad from cats and comparison with implementation of logic in actors
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
╭────────────╮ ╭────────────────╮
User Input -> │ Actor │ -> │ User Outputs │
│ │ ╰────────────────╯
│ Logic is │ ╭────────────────╮
│ here │ -> │ System reports │
╰────────────╯ ╰────────────────╯
There are two implementations of vending machine:
- actor based, all logic is in actor
- all logic in state monad (from cats), actor is for communication and keeping state
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 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.
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