Skip to content

Commit

Permalink
✨ 【第9章 歩幅の調整】 Dollar と Franc のクラスを消すために Currency フィールドを追加する
Browse files Browse the repository at this point in the history
・Money クラスに currency フィールドを作成し、Dollar, Franc ごとに currency を設定するような書きぶりにする
・本来なら Currency オブジェクトを作りたいがまずは文字列だけで十分だと思われるので簡単に実装する
  • Loading branch information
dodonki1223 committed Dec 12, 2021
1 parent 76f6989 commit 0ce7c6f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/__tests__/money.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ test('null equals', () => {
});

test('equals Franc = Dollar', () => {
expect(new Franc(5).equals(new Dollar(5))).toBeTruthy();
expect(new Franc(5).equals(new Dollar(6))).toBeFalsy();

test('currency', () => {
expect(Money.dollar(1).currency).toBe('USD');
expect(Money.franc(1).currency).toBe('CHF');
});
6 changes: 3 additions & 3 deletions src/money.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// import { Franc } from "./franc";

export abstract class Money {
constructor(protected readonly amount: number) {
constructor(protected readonly amount: number, public readonly currency: string) {
}

static dollar(amount: number):Dollar {
Expand All @@ -26,7 +26,7 @@ export abstract class Money {

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

times(multiplier: number): Money {
Expand All @@ -36,7 +36,7 @@ export class Dollar extends Money {

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

times(multiplier: number): Money {
Expand Down

0 comments on commit 0ce7c6f

Please sign in to comment.