diff --git a/src/money/index.spec.ts b/src/money/index.spec.ts index 6c3c555..9e79438 100644 --- a/src/money/index.spec.ts +++ b/src/money/index.spec.ts @@ -1,4 +1,4 @@ -import Money from '../money'; +import Money, { Franc } from '../money'; describe('Money', () => { describe('equality', () => { @@ -9,6 +9,10 @@ describe('Money', () => { expect(Money.dollar(5).equals(Money.dollar(6))).toBe(false); expect(Money.franc(5).equals(Money.dollar(5))).toBe(false); }); + + it('should return true if comparing two different classes with the same amount and currency', () => { + expect(new Money(5, 'CHF').equals(new Franc(5, 'CHF'))).toBe(true); + }); }); describe('currency', () => { diff --git a/src/money/index.ts b/src/money/index.ts index ac226df..33fb152 100644 --- a/src/money/index.ts +++ b/src/money/index.ts @@ -1,4 +1,4 @@ -abstract class Money { +class Money { private _amount: number; private _currency: string; @@ -13,7 +13,7 @@ abstract class Money { equals(obj: Object): boolean { const money: Money = obj as Money; - return this.amount == money.amount && obj.constructor.name == this.constructor.name; + return this.amount == money.amount && money.currency == this.currency; } static dollar(amount: number): Money { @@ -24,23 +24,17 @@ abstract class Money { return new Franc(amount, 'CHF'); } - abstract times(multiplier: number): Money; + times(multiplier: number): Money { + return new Money(this.amount * multiplier, this.currency); + } get currency(): string { return this._currency; } } -export class Dollar extends Money { - times(multiplier: number): Money { - return Money.dollar(this.amount * multiplier); - } -} +export class Dollar extends Money {} -export class Franc extends Money { - times(multiplier: number): Money { - return Money.franc(this.amount * multiplier); - } -} +export class Franc extends Money {} export default Money;