Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
kostaskougios committed Apr 10, 2024
1 parent e1dcb0a commit 0afa5fe
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions example-scripts/budget.sc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ case class BudgetForm(
startDate: LocalDate = LocalDate.of(2024, 4, 1),
initialBudget: Int = 1000,
percentIncreasePerYear: Float = 4f / 100f,
available: Float = 300000
available: Float = 100000
)

class BudgetPage(initialForm: BudgetForm)(using ConnectedSession):
Expand All @@ -29,11 +29,17 @@ class BudgetPage(initialForm: BudgetForm)(using ConnectedSession):
def components(form: BudgetForm, events: Events): MV[BudgetForm] =

case class Row(date: LocalDate, budget: Float, total: Float, available: Float)
val rows = (1 to 30 * 12).foldLeft(Seq.empty[Row]): (rows, month) =>
val date = form.startDate.plusMonths(month)
val budget = form.initialBudget + form.initialBudget * ((month / 12) * form.percentIncreasePerYear)
val total = rows.map(_.budget).sum + budget
rows :+ Row(date, budget, total, Math.max(0, form.available - total))
val rows = (1 to 30 * 12)
.foldLeft((Seq.empty[Row], form.initialBudget)):
case ((rows, lastBudget), month) =>
val date = form.startDate.plusMonths(month)
val budget = if month % 12 == 0 then (lastBudget + lastBudget * form.percentIncreasePerYear).toInt else lastBudget
val total = rows.map(_.budget).sum + budget
(
rows :+ Row(date, budget, total, Math.max(0, form.available - total)),
budget
)
._1
val table = QuickTable().withHeaders("Date", "Budget", "Total", "Available").withRows(rows.map(r => Seq(r.date, r.budget, r.total, r.available)))

MV(form, table)
Expand Down

0 comments on commit 0afa5fe

Please sign in to comment.