Last active
August 27, 2021 15:08
-
-
Save pathikrit/b5a2630b050912614333f0cac2580b7f to your computer and use it in GitHub Desktop.
Incur Credit Card Fees or Not?
This file contains hidden or 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
import squants.Dimensionless | |
import squants.DimensionlessConversions._ | |
import squants.market._, MoneyConversions._ | |
case class CreditCard(name: String, points: Dimensionless, checkingAPR: Dimensionless) { | |
def charge(amount: Money, fee: Dimensionless = 0 percent, flatFee: Money = 0 dollars): String = { | |
val fees = amount*fee + flatFee | |
val interestEarned = amount * checkingAPR/12 | |
val cashBack = (amount + fees)*points | |
val saved = cashBack - interestEarned - fees | |
val (verdict, result) = if (saved < 0.dollars) { | |
s"Too expensive to pay ${amount.toFormattedString} by $name card" -> "Pay in fees" | |
} else { | |
s"Use your $name card to pay ${amount.toFormattedString}" -> "Earn in points" | |
} | |
f""" | |
|$verdict | |
|Fees Charged = ${fees.toFormattedString}%8s | |
|Checking interest earned = ${interestEarned.toFormattedString}%8s | |
|Cash back ($points) = ${cashBack.toFormattedString}%8s | |
|------------------------------------------------------- | |
|${result.padTo(24, ' ')} = ${saved.abs.toFormattedString}%8s | |
""".stripMargin | |
} | |
} | |
/***********************************************************************************************/ | |
val myCard = CreditCard(name = "Bank of America Premium Rewards", points = 1.5*1.75 percent, checkingAPR = 0.02 percent) | |
println(myCard.charge(amount = 5000 dollars, fee = 2.65 percent, flatFee = 2.25 dollars)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment