Created
June 23, 2012 04:58
-
-
Save branflake2267/2976943 to your computer and use it in GitHub Desktop.
Dart 2 Decimal Money
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
#import('unittest/unittest.dart'); | |
class Money { | |
bool _isNegative = false; | |
int _dollars = 0; | |
int _cents = 0; | |
Money(String money) { | |
if (money.contains("-")) { | |
_isNegative = true; | |
money = money.replaceFirst("-", ""); | |
} | |
if (!money.contains(".")) { | |
_setDollars(money); | |
_cents = 0; | |
} else { | |
List<String> split = money.split("."); | |
_setDollars(split[0]); | |
_setCents(split[1]); | |
} | |
} | |
Money.create(bool isNegative, int dollars, int cents) : | |
_isNegative = isNegative, _dollars = dollars, _cents = cents; | |
_setDollars(String dollars) { | |
_dollars = Math.parseInt(dollars); | |
} | |
_setCents(String cents) { | |
_cents = Math.parseInt(cents); | |
} | |
bool get isNegative() => _isNegative; | |
int get dollars() { | |
int dollars = _dollars; | |
if (_isNegative == true && _dollars != 0) { | |
dollars = -_dollars; | |
} | |
return dollars; | |
} | |
int get cents() { | |
int cents = _cents; | |
if (_isNegative == true && _cents != 0) { | |
cents = -_cents; | |
} | |
return cents; | |
} | |
Money operator +(Money money) { | |
int dollarsTotal = this.dollars + money.dollars; | |
int centsTotal = this.cents + money.cents; | |
if (centsTotal <= -100) { | |
dollarsTotal--; | |
centsTotal += 100; | |
centsTotal *= -1; | |
} else if (centsTotal >= 100) { | |
dollarsTotal++; | |
centsTotal -= 100; | |
} | |
bool isNegative = false; | |
if (dollarsTotal < 0) { | |
isNegative = true; | |
dollarsTotal *= -1; | |
if (centsTotal < 0) { | |
centsTotal *= -1; | |
} | |
} | |
return new Money.create(isNegative, dollarsTotal, centsTotal); | |
} | |
bool operator ==(Money money) { | |
if (this !== null && money !== null && | |
this.dollars == money.dollars && | |
this.cents == money.cents) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
String toString() { | |
String sign = ""; | |
if (_isNegative) { | |
sign = "-"; | |
} | |
String s = "\$$sign$_dollars.$_cents"; | |
return s; | |
} | |
} | |
main() { | |
test('get dollars -', () { | |
var money = new Money("-1.99"); | |
expect(-1, money.dollars, "dollars compare"); | |
}); | |
test('get cents -', () { | |
var money = new Money("-1.99"); | |
expect(-99, money.cents, "cents compare"); | |
}); | |
test('get dollars +', () { | |
var money = new Money("1.99"); | |
expect(1, money.dollars, "dollars compare"); | |
}); | |
test('get cents +', () { | |
var money = new Money("1.99"); | |
expect(99, money.cents, "cents compare"); | |
}); | |
test('adding negatives', () { | |
var left = new Money("-1.99"); | |
var right = new Money("-1.99"); | |
var total = left + right; | |
print("$left + $right = $total"); | |
var expected = new Money("-3.98"); | |
expect(expected, total); | |
}); | |
test('adding positives', () { | |
var left = new Money("1.99"); | |
var right = new Money("1.99"); | |
var total = left + right; | |
print("$left + $right = $total"); | |
var expected = new Money("3.98"); | |
expect(expected, total); | |
}); | |
test('adding positives', () { | |
var left = new Money("1.99"); | |
var right = new Money("-1.99"); | |
var total = left + right; | |
print("$left + $right = $total"); | |
var expected = new Money("0.0"); | |
expect(expected, total); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment