If you have tried to call simulatePlan
from the Scala Console, you probably tried different values for nbOfMonths
and observed the resulting capital at retirement and after death. It would be useful to have a function that finds the optimal nbOfMonths
so that you have enough capital to never run out of money during your retirement.
As usual, let's start with a new unit test to clarify what we expect from this function:
"RetCalc.nbOfMonthsSaving" should { "calculate how long I need to save before I can retire" in { val actual = RetCalc.nbOfMonthsSaving( interestRate = 0.04 / 12, nbOfMonthsInRetirement = 40 * 12, netIncome = 3000, currentExpenses = 2000, initialCapital = 10000) val expected = 23 * 12 + 1 actual should ===(expected) } }
In this test, the expected value can be a bit difficult to figure out. One way would be to use the NPM
function in Excel. Alternatively, you could call simulatePlan
many times in...