Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Savings account problem
import java.util.{Calendar, Date}
class SavingsAccount(amount: Int, percent: Double, openDate: Date) {
def annualInterest() : Int = {
(amount * percent).round.toInt
}
def monthlyInterest() : Int = {
annualInterest() / 12
}
def reminderInterest() : Int = {
annualInterest() - 12 * monthlyInterest()
}
def firstMonthInterest() : Int = {
monthlyInterest() - lastMonthInterest() + reminderInterest()
}
def lastMonthInterest() : Int = {
val calendar = Calendar.getInstance()
calendar.setTime(openDate)
monthlyInterest() * calendar.get(Calendar.DAY_OF_MONTH) / calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
}
}
val amount = 100000
val annualInterestRate = 0.12
val openDate = new Date()
val account = new SavingsAccount(amount, annualInterestRate, openDate)
val annually = account.annualInterest()
val monthly = account.monthlyInterest()
val reminder = account.reminderInterest()
val first = account.firstMonthInterest()
val last = account.lastMonthInterest()
val isOk = 11 * monthly + first + last == annually
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment