Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull up "times" operation #10

Merged
merged 9 commits into from
Sep 29, 2022
6 changes: 5 additions & 1 deletion src/money/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Money from '../money';
import Money, { Franc } from '../money';

describe('Money', () => {
describe('equality', () => {
Expand All @@ -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', () => {
Expand Down
20 changes: 7 additions & 13 deletions src/money/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
abstract class Money {
class Money {
private _amount: number;
private _currency: string;

Expand All @@ -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 {
Expand All @@ -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;