diff --git a/example-scripts/budget.sc b/example-scripts/budget.sc index 2609cdb..02878b2 100755 --- a/example-scripts/budget.sc +++ b/example-scripts/budget.sc @@ -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): @@ -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)