Created
February 7, 2019 17:47
-
-
Save andrewgeorgemitchell/f25b8146dd3dce38a04b49de7334a437 to your computer and use it in GitHub Desktop.
A cash class which uses integer math to maintain precision in javascript.
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 CashAmount{ | |
constructor(cashDecimalValue) { | |
this.cashDecimalValue = cashDecimalValue; | |
this.totalInPennies = this.totalInPennies.bind(this); | |
this.addDoubleAmount = this.addDoubleAmount.bind(this); | |
this.toDouble = this.toDouble.bind(this); | |
this.toDoubleString = this.toDoubleString.bind(this); | |
this.quantityOfEachDenomination = this.quantityOfEachDenomination.bind(this); | |
this.denominations = { | |
'hundreds': 100, | |
'fifties': 50, | |
'twenties': 20, | |
'tens': 10, | |
'fives': 5, | |
'ones': 1, | |
'quarters': .25, | |
'dimes': .10, | |
'nickels': .05, | |
'pennies': .01 | |
} | |
} | |
totalInPennies(cashAmount) { | |
if (cashAmount) { | |
return cashAmount * 100 | |
} | |
return this.cashDecimalValue * 100; | |
} | |
addDoubleAmount(cashValue) { | |
this.cashDecimalValue = (this.totalInPennies(this.cashDecimalValue) + this.totalInPennies(cashValue)) / 100; | |
} | |
quantityOfEachDenomination() { | |
const denominationsResults = {} | |
let cashAmountInPenniesLeft = this.totalInPennies(this.cashDecimalValue); | |
for (const key in this.denominations) { | |
if (this.denominations.hasOwnProperty(key)) { | |
const currentDenominationInPennies = this.totalInPennies(this.denominations[key]); | |
const amountOfCurrentDenomination = Math.floor(cashAmountInPenniesLeft/currentDenominationInPennies) | |
cashAmountInPenniesLeft = cashAmountInPenniesLeft - (amountOfCurrentDenomination * currentDenominationInPennies); | |
denominationsResults[key] = amountOfCurrentDenomination; | |
} | |
} | |
return denominationsResults; | |
} | |
toDouble() { | |
return this.cashDecimalValue; | |
} | |
toDoubleString() { | |
const totalInString = `${this.cashDecimalValue}`; | |
return totalInString; | |
} | |
} | |
// Tests | |
const totalInPenniesTest = () => { | |
const cash = new CashAmount(10.50); | |
console.log('cash.totalInPennies() should equal 1050. Result:', cash.totalInPennies()) | |
} | |
const addDoubleAmountTest = () => { | |
const cash = new CashAmount(10.50); | |
cash.addDoubleAmount(29.33); | |
console.log('should equal 3983. Result:', cash.totalInPennies()) | |
} | |
const checkDenominationTest = () => { | |
const cash = new CashAmount(967.93); | |
console.log(`should equal | |
{ | |
'hundreds': 9, | |
'fifties': 1, | |
'twenties': 0, | |
'tens': 1, | |
'fives': 1, | |
'ones': 2, | |
'quarters': 3, | |
'dimes': 1, | |
'nickels': 1, | |
'pennies': 3 | |
}. | |
Result: | |
`, cash.quantityOfEachDenomination()) | |
} | |
const toDoubleTest = () => { | |
const cash = new CashAmount(10.50); | |
cash.addDoubleAmount(29.33); | |
console.log('should equal 39.83. Result:', cash.toDouble()); | |
} | |
const toDoubleStringTest = () => { | |
const cash = new CashAmount(10.50); | |
cash.addDoubleAmount(29.33); | |
console.log('should equal "39.83". Result:', cash.toDoubleString(), 'Type of Response:', typeof cash.toDoubleString()); | |
} | |
const tests = [ | |
totalInPenniesTest, | |
addDoubleAmountTest, | |
checkDenominationTest, | |
toDoubleTest, | |
toDoubleStringTest, | |
] | |
for (let i = 0; i < tests.length; i++) { | |
tests[i](); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment