Skip to content

Commit 36af124

Browse files
vitlindaGiacomo Cavalieri
authored and
Giacomo Cavalieri
committed
feat: add restocking bc
1 parent 790c06b commit 36af124

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package dev.atedeg.mdm.restocking
2+
3+
import cats.data.NonEmptyList
4+
import cats.syntax.all.*
5+
6+
import dev.atedeg.mdm.utils.given
7+
8+
/**
9+
* Given a list of ingredients needed to [[IncomingEvent.ProductionStarted start a production]],
10+
* it removes that [[WeightInQuintals quantity]] from the [[Stock stock]].
11+
*/
12+
def consumeIngredients(stock: Stock)(ingredients: NonEmptyList[QuintalsOfIngredient]): Stock =
13+
ingredients.foldLeft(stock) { case (newStock, QuintalsOfIngredient(q, i)) =>
14+
val toSubtract = StockedQuantity(q.n)
15+
newStock.updatedWith(i)(oldQuantity => oldQuantity.map(_ - toSubtract))
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dev.atedeg.mdm.restocking
2+
3+
import cats.data.NonEmptyList
4+
5+
enum IncomingEvent:
6+
case OrderMilk(quintals: QuintalsOfMilk)
7+
case ProductionStarted(ingredients: NonEmptyList[QuintalsOfIngredient])
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package dev.atedeg.mdm.restocking
2+
3+
import dev.atedeg.mdm.products.Ingredient
4+
import dev.atedeg.mdm.utils.*
5+
import dev.atedeg.mdm.utils.given
6+
7+
/**
8+
* A quantity of milk expressed in quintals.
9+
* @note it must be a [[PositiveNumber positive number]].
10+
* @example `QuintalsOfMilk(1)` is a valid weight of 110 kg.
11+
* @example `QuintalsOfMilk(0)` is not a valid weight.
12+
*/
13+
final case class QuintalsOfMilk(quintals: PositiveNumber)
14+
15+
/**
16+
* An [[Ingredient ingredient]] and a [[WeightInQuintals weight in quintals]].
17+
*/
18+
final case class QuintalsOfIngredient(quintals: WeightInQuintals, ingredient: Ingredient)
19+
20+
/**
21+
* A weight expressed in quintals.
22+
*/
23+
final case class WeightInQuintals(n: PositiveDecimal)
24+
25+
/**
26+
* Quintals of stocked milk.
27+
*/
28+
final case class StockedMilk(quintals: NonNegativeDecimal)
29+
30+
/**
31+
* The quantity of ingredients in stock.
32+
*/
33+
type Stock = Map[Ingredient, StockedQuantity]
34+
35+
/**
36+
* A stocked quantity.
37+
*/
38+
final case class StockedQuantity(quintals: NonNegativeDecimal) derives Minus

0 commit comments

Comments
 (0)