- Scala Programming Projects
- Mikael Valot Nicolas Jorand
- 279字
- 2021-07-23 16:25:19
Writing a test for the decumulation phase
Now you know how much capital you can expect at your retirement date. It turns out you can reuse the same futureCapital function, to work out how much capital will be left for your heirs.
Add the following test in RetCalcSpec, underneath the previous unit test, and run it. It should pass:
"RetCalc.futureCapital" should {
"calculate how much savings will be left after having taken a pension
for n months" in {
val actual = RetCalc.futureCapital(
interestRate = 0.04/12, nbOfMonths = 40 * 12,
netIncome = 0, currentExpenses = 2000, initialCapital =
541267.1990)
val expected = 309867.53176
actual should ===(expected)
}
}
So, if you live for 40 years after your retirement date, spend the same amount every month, and don't have any other income, you will still have a significant capital left for your heirs. If the remaining capital was negative, that would have meant that you would have run out of money at some point during your retirement and it is an outcome we want to avoid.
Feel free to call the function from the Scala Console and try different values that would match more closely to your personal situation. Try different values for the interest rate and observe how you can end up with a negative capital if the rate is low.
Note that, in a production system, you would certainly add more unit tests to cover some other edge cases and make sure that the function will not crash. As we will cover error handling in Chapter 3, Handling Errors, we can assume that the test coverage of futureCapital is good enough for now.
- 連接未來:從古登堡到谷歌的網絡革命
- Building E-commerce Sites with VirtueMart Cookbook
- 光網絡評估及案例分析
- INSTANT PhpStorm Starter
- 萬物互聯:蜂窩物聯網組網技術詳解
- 無人機通信
- Mastering JavaFX 10
- Building Web Applications with ArcGIS
- React Cookbook
- Getting Started with Memcached
- Building RESTful Web Services with .NET Core
- 物聯網M2M開發技術:基于無線CPU-Q26XX
- 通信系統實戰筆記:無處不在的信號處理
- 互聯網安全的40個智慧洞見(2018)
- 無線傳感器網絡定位方法及應用