Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
kostaskougios committed Apr 7, 2024
1 parent e16ca0e commit e1dcb0a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions example-scripts/budget.sc
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,26 @@ 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] =
controller.render(initialForm).run()

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)

Expand Down

0 comments on commit e1dcb0a

Please sign in to comment.