Created
April 18, 2016 19:54
-
-
Save scottashipp/d0d64cfcdcda7de7e0750b3918340af0 to your computer and use it in GitHub Desktop.
Budgeting with discretionary income can be difficult because its hard when faced with daily decisions to think of that rather larger monthly figure in daily terms. This program helps you take your monthly discretionary money and turn it into a number you can target daily.
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
object DailyAllowance extends App { | |
val DaysInMonth = 30 | |
val MonthlyNet = USD("$5000.00") | |
val MonthlyFixedExpenses = USD("$3000.00") | |
case class Money(rate: Int) | |
class USD(val dollars: Int, val cents: Int) extends Money(100) { | |
val amountInCents = (dollars * rate) + cents | |
def convertAmount(amount: Int): USD = { | |
val dollars: Int = amount / rate | |
val cents: Int = amount - (dollars * rate) | |
new USD(dollars, cents) | |
} | |
def +(other: USD) = { | |
convertAmount(this.amountInCents + other.amountInCents) | |
} | |
def -(other: USD) = { | |
convertAmount(Math.abs(this.amountInCents - other.amountInCents)) | |
} | |
def /(other: USD) = { | |
convertAmount(this.amountInCents / other.amountInCents) | |
} | |
def /(x: Int) = { | |
convertAmount(this.amountInCents / x) | |
} | |
override def toString(): String = { | |
s"$$${dollars}.${if(cents < 10) "0" else ""}${cents}" | |
} | |
} | |
object USD { | |
def apply(s: String) = { | |
val withoutDollarSign = if(s.charAt(0) == '$') s.substring(1) else s | |
val Array(dollars,cents) = withoutDollarSign.split('.') | |
if(cents.length > 2) { | |
throw new IllegalArgumentException("Unrecognized format. Please use a format like $XXXX.XX or XXXX.XX.") | |
} | |
new USD(dollars.toInt, cents.toInt) | |
} | |
} | |
println(s"Monthly net income: ${MonthlyNet}") | |
println(s"Monthly fixed expenses: ${MonthlyFixedExpenses}") | |
val remainingAfterExpenses = MonthlyNet - MonthlyFixedExpenses | |
println(s"Discretionary income: ${remainingAfterExpenses}") | |
println(s"Discretionary per day: ${remainingAfterExpenses / DaysInMonth}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment