From e1dcb0a5dd9a7e8ae154eb30ba2291ea9ee1243f Mon Sep 17 00:00:00 2001 From: Kostas Kougios Date: Sun, 7 Apr 2024 19:46:20 +0100 Subject: [PATCH] - --- example-scripts/budget.sc | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/example-scripts/budget.sc b/example-scripts/budget.sc index 82dc636..2609cdb 100755 --- a/example-scripts/budget.sc +++ b/example-scripts/budget.sc @@ -15,7 +15,12 @@ Sessions println(new BudgetPage(BudgetForm()).run) -case class BudgetForm(startDate: LocalDate = LocalDate.of(2024, 4, 1), initialBudget: Int = 1000, percentIncreasePerYear: Float = 4f / 100f) +case class BudgetForm( + startDate: LocalDate = LocalDate.of(2024, 4, 1), + initialBudget: Int = 1000, + percentIncreasePerYear: Float = 4f / 100f, + available: Float = 300000 +) class BudgetPage(initialForm: BudgetForm)(using ConnectedSession): def run: Option[BudgetForm] = @@ -23,13 +28,13 @@ class BudgetPage(initialForm: BudgetForm)(using ConnectedSession): def components(form: BudgetForm, events: Events): MV[BudgetForm] = - case class Row(date: LocalDate, budget: Float, total: Float) + 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) - val table = QuickTable().withHeaders("Date", "Budget", "Total").withRows(rows.map(r => Seq(r.date, r.budget, r.total))) + rows :+ Row(date, budget, total, Math.max(0, form.available - total)) + val table = QuickTable().withHeaders("Date", "Budget", "Total", "Available").withRows(rows.map(r => Seq(r.date, r.budget, r.total, r.available))) MV(form, table)