package tripvisModel { import mx.formatters.NumberFormatter; public class MoneyAmount { private var _amount : Number; private var _currency : Currency; //Constructors: public function MoneyAmount(amount: Number = 0, currency: Currency = null) { _amount = amount; // als er een currency is ingevuld: if (currency != null) { _currency = currency; } else { _currency = Currency.DEFAULT_CURRENCY; } } //Getters & Setters: public function set amount(value:Number):void { this._amount = value; } public function get amount():Number { return this._amount; } public function set currency(value: Currency):void { this._currency = value; } public function get currency(): Currency { return this._currency; } private function convertToDefaultCurrency(moneyAmount: MoneyAmount): MoneyAmount { if (moneyAmount.currency != Currency.DEFAULT_CURRENCY) { var newAmount: Number = moneyAmount.amount * moneyAmount.currency.rate; return new MoneyAmount(newAmount, Currency.DEFAULT_CURRENCY); } else { return moneyAmount; } } public function add(m: MoneyAmount): MoneyAmount { var amount1: Number = convertToDefaultCurrency(this).amount; var amount2: Number = convertToDefaultCurrency(m).amount; return new MoneyAmount(amount1 + amount2, Currency.DEFAULT_CURRENCY); } public function clear(): void { this._amount = 0; this._currency = Currency.DEFAULT_CURRENCY; } public function equals(m: MoneyAmount): Boolean { if (this.amount != m.amount) return false; if (this.currency != m.currency) return false; return true; } public function toString(): String { var nrFormatter: NumberFormatter = new NumberFormatter(); nrFormatter.precision = 2; nrFormatter.useThousandsSeparator = false; if (_amount % 1 != 0) return this._currency.sign + ' ' + nrFormatter.format( this._amount.toString() ); else return this._currency.sign + ' ' + this._amount.toString(); } } }