Created
March 29, 2015 11:58
-
-
Save mordka/23a94c1fd0c4a96db0a5 to your computer and use it in GitHub Desktop.
Calculates regular saver effective interest rate, assuming you pay in 250 each first day of month over 1 year period.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rounding { | |
public BigDecimal round(int n) { | |
return setScale(n, BigDecimal.ROUND_HALF_UP); | |
} | |
} | |
class Taxable { | |
def defaultTaxRate = 0.2 | |
public afterTax(taxRate=defaultTaxRate) { | |
return this * (1-taxRate); | |
} | |
} | |
BigDecimal.mixin Rounding | |
BigDecimal.mixin Taxable | |
def months = ["January" : 31, | |
"February" : 28, | |
"March" : 31 , | |
"April" : 30 , | |
"May" : 31 , | |
"June" : 30 , | |
"July" : 31 , | |
"August" : 31 , | |
"September" : 30 , | |
"October" : 31 , | |
"November" : 30 , | |
"December" : 31 ] | |
def grossRate = 0.06 | |
def dailyRate = grossRate/365 | |
def payInAmount = 250 | |
def payInCount = 0 | |
def interestsTotal =0 | |
BigDecimal accountValue = 0 | |
println 'DATA IN ------------------' | |
println 'Calculating interest rate...' | |
println 'Interest GROSS AER rate = ' << grossRate | |
println 'Daily rate = ' << dailyRate | |
println '--------------------------' | |
def dailyInterest=0 | |
months.each{String month, int days -> | |
//pay in first day of month | |
++payInCount | |
accountValue = accountValue + payInAmount | |
for (dayOfMonth in (1..days)) { | |
dailyInterest = accountValue*dailyRate | |
accountValue = dailyInterest + accountValue | |
interestsTotal += dailyInterest | |
} | |
//println 'account value in the end of ' << month << ' is ' << accountValue.round(2) | |
} | |
println 'RESULT --------------------' | |
println 'Number of pay ins ' << payInCount | |
println 'Total interests ' << interestsTotal.round(2) | |
println 'Total interests after tax ' << (interestsTotal.afterTax()).round(2) | |
println 'Your account value after 12 months and after tax is ' << (payInCount*payInAmount+interestsTotal.afterTax()).round(2) | |
def grossInterest = (interestsTotal/accountValue*100) | |
println 'Effective GROSS interest rate is ' << grossInterest.round(2) | |
println 'Effective NET interest rate is ' << grossInterest.afterTax().round(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment