Skip to content

Commit

Permalink
✨ 【第10章 テストに聞いてみる】 times の一般化のためにまずは currency を追加する
Browse files Browse the repository at this point in the history
・Dollar, Francクラスを Money クラスと合わせるために currency のフィールドを追加する
  • Loading branch information
dodonki1223 committed Dec 19, 2021
1 parent 2bbaf15 commit 3d8d590
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/dollar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { Money, Dollar } from '../money';
test('times', () => {
const five: Money = Money.dollar(5);

expect(five.times(2)).toEqual(new Dollar(10));
expect(five.times(3)).toEqual(new Dollar(15));
expect(five.times(2)).toEqual(Money.dollar(10));
expect(five.times(3)).toEqual(Money.dollar(15));
});
4 changes: 2 additions & 2 deletions src/__tests__/franc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Money, Franc } from '../money';
test('times', () => {
const five: Money = Money.franc(5);

expect(five.times(2)).toEqual(new Franc(10));
expect(five.times(3)).toEqual(new Franc(15));
expect(five.times(2)).toEqual(Money.franc(10));
expect(five.times(3)).toEqual(Money.franc(15));
});

16 changes: 8 additions & 8 deletions src/money.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export abstract class Money {
}

static dollar(amount: number):Dollar {
return new Dollar(amount)
return new Dollar(amount, 'USD')
}

static franc(amount: number):Franc {
return new Franc(amount)
return new Franc(amount, 'CHF')
}

abstract times(multiplier: number): Money
Expand All @@ -25,21 +25,21 @@ export abstract class Money {
}

export class Dollar extends Money {
constructor(amount: number) {
super(amount, 'USD');
constructor(amount: number, currency: string) {
super(amount, currency);
}

times(multiplier: number): Money {
return new Dollar(this.amount * multiplier)
return new Dollar(this.amount * multiplier, this.currency)
}
}

export class Franc extends Money {
constructor(amount: number) {
super(amount, 'CHF');
constructor(amount: number, currency: string) {
super(amount, currency);
}

times(multiplier: number): Money {
return new Franc(this.amount * multiplier)
return new Franc(this.amount * multiplier, this.currency)
}
}

0 comments on commit 3d8d590

Please sign in to comment.