Lesson contents

Scala — Inventory project with boundary tests

This Scala inventory program uses foldLeft to pass the updated Result from one event to the next. An invalid event increments the rejection count while preserving the stock value.

A simple example

Trace one stock change

  1. Start with 3 in stock
  2. Do this Take 1 away
  3. Result 2
One subtraction updates the starting count. The project below adds checks for larger changes.
Lesson.scala
object Lesson extends App {
    var stock = 3
    stock -= 1
    println(stock)
}
Output
2
Advertisements

Build an inventory event processor

foldLeft threads a new Result through each event. Returning a copy leaves earlier states intact.

Receive 5, ship 2: 3 in stock. Invalid events: 2 rejected. Receive 4: 7 in stock.
Reconcile a stock ledger

The first delivery brings five units and a shipment takes two. A request to ship nine and an unknown event are both rejected, so stock stays at three. The final delivery adds four and closes the ledger at seven.

Run the example

Scala 2.13 with JDK 11 or later. Run these commands from the directory containing the example.

scalac Lesson.scala
scala Lesson
Lesson.scala
object Lesson {
  case class Event(kind: String, units: Int)
  case class Result(stock: Int, rejected: Int)

  def process(events: List[Event]): Result =
    events.foldLeft(Result(0, 0)) { (state, event) =>
      event match {
        case Event("in", n) if n > 0 => state.copy(stock = state.stock + n)
        case Event("out", n) if n > 0 && n <= state.stock =>
          state.copy(stock = state.stock - n)
        case _ => state.copy(rejected = state.rejected + 1)
      }
    }

  def main(args: Array[String]): Unit = {
    val events = List(Event("in", 5), Event("out", 2), Event("out", 9),
                      Event("?", 1), Event("in", 4))
    assert(process(Nil) == Result(0, 0))
    assert(process(List(Event("in", 2), Event("out", 2))) == Result(0, 0))
    val result = process(events)
    assert(result == Result(7, 2))
    println(result.stock.toString + " " + result.rejected)
  }
}
Output
7 2

Start with zero units. Receive 5 and ship 2 to leave 3. Reject the shipment of 9 and the unknown event. Finally receive 4. The two output numbers are the ending stock of 7 and the two rejected events.

Buy supplies for the next shift

A breakfast kiosk has 28 cups left, expects 64 orders tomorrow and keeps four spare cups for spills. The supervisor subtracts usable stock from the target and buys sealed sleeves of ten, rounding the order up to a whole sleeve.

Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val orders = 64
        val spare = 4
        val onHand = 28
        val shortage = math.max(0, orders + spare - onHand)
        val sleeves = (shortage + 9) / 10
        println((sleeves).toString() + " sleeves")
    }
}
Output
4 sleeves

The target is 68 cups. After counting the 28 on hand, the kiosk needs 40 more, exactly four sleeves of ten.

Try it yourself

Append a shipment of 7, followed by another shipment of 1.

Check your answer

The first shipment exactly empties stock. The next one is rejected. Update the expected result to 0 units and 3 rejections. Keep the existing checks for the empty event list and exact depletion.

Tour of Scala

Related tags

  • Scala
  • inventory
  • testing
  • state transitions