@@ -9,12 +9,15 @@ import io.udash.wrappers.jquery.{jQ => $, _}
9
9
10
10
import java .text .DecimalFormat
11
11
12
+ import framework .JsRead ._
13
+ import api .Mortgage
14
+
12
15
@ JSExportTopLevel (" mortgage_calculator" )
13
16
object MortgageCalculator extends framework.Page (" mortgage_calculator" ) {
14
17
override def renderBody = body(
15
18
div(`class` := " container card w-25 mt-5 p-3" )(
16
19
h3(" Mortgage Calculator" ),
17
- t. form(t.id := " calculator" )(
20
+ form(t.id := " calculator" )(
18
21
input(label = " Loan Amount ($)" , id = " loan" , default = 1e6 .toInt),
19
22
input(label = " APR (%)" , id = " apr" , default = 5 ),
20
23
input(label = " Mortgage Period (years)" , id = " years" , default = 30 ),
@@ -33,15 +36,14 @@ object MortgageCalculator extends framework.Page("mortgage_calculator") {
33
36
)
34
37
35
38
override def init () = {
36
- $(" #calc_payments" ).on(" click" , calc)
39
+ $(" #calc_payments" ).on(" click" , calculate_payments)
40
+ $(" #refinance" ).on(" click" , calculate_refinance)
37
41
}
38
42
39
- @ nowarn
40
- def calc (element : Element , event : JQueryEvent ) = {
41
- import framework .JsRead ._
43
+ val moneyFormat = new DecimalFormat (" $ #.00" )
42
44
43
- val format = new DecimalFormat ( " $ #.00 " );
44
- import api . Mortgage
45
+ @ nowarn
46
+ def calculate_payments ( element : Element , event : JQueryEvent ) = {
45
47
$(" #output" ).html(
46
48
table(`class` := " table table-striped font-monospace" )(
47
49
tr(th(" #" ), th(" Balance" ), th(" Payment" ), th(" Principal" ), th(" Interest" )),
@@ -57,15 +59,26 @@ object MortgageCalculator extends framework.Page("mortgage_calculator") {
57
59
} $(" #output tr:last" ).after(
58
60
tr(
59
61
td(row + 1 ),
60
- td(format .format(payment.balance)),
61
- td(format .format(payment.payment)),
62
- td(format .format(payment.principal)),
63
- td(format .format(payment.interest)),
62
+ td(moneyFormat .format(payment.balance)),
63
+ td(moneyFormat .format(payment.payment)),
64
+ td(moneyFormat .format(payment.principal)),
65
+ td(moneyFormat .format(payment.interest)),
64
66
).render,
65
67
)
66
68
}
69
+
70
+ @ nowarn
71
+ def calculate_refinance (element : Element , event : JQueryEvent ) = {
72
+ for {
73
+ amount <- $(" #loan" ).value().as[Int ]
74
+ apr <- $(" #apr" ).value().as[Double ]
75
+ years <- $(" #years" ).value().as[Int ]
76
+ newApr <- $(" #new_apr" ).value().as[Double ]
77
+ mortgage = Mortgage (amount = amount, apr = apr, years = years)
78
+ penalty <- Mortgage .API .refinancePenalty(mortgage, newApr)
79
+ } $(" #output" ).html(
80
+ div(`class` := " container" )(s " You will save ${moneyFormat.format(penalty)} over $years years in interest " ).render
81
+ )
82
+ }
67
83
}
68
84
69
- /*
70
- * Thu, Sep 1: code review + 2nd API
71
- */
0 commit comments