From 0ce7c6fff2f3338ec7ce0fbcc6f7d4bf5b2dd844 Mon Sep 17 00:00:00 2001 From: dodonki1223 Date: Sun, 12 Dec 2021 20:41:27 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E3=80=90=E7=AC=AC9=E7=AB=A0=20?= =?UTF-8?q?=E6=AD=A9=E5=B9=85=E3=81=AE=E8=AA=BF=E6=95=B4=E3=80=91=20Dollar?= =?UTF-8?q?=20=E3=81=A8=20Franc=20=E3=81=AE=E3=82=AF=E3=83=A9=E3=82=B9?= =?UTF-8?q?=E3=82=92=E6=B6=88=E3=81=99=E3=81=9F=E3=82=81=E3=81=AB=20Curren?= =?UTF-8?q?cy=20=E3=83=95=E3=82=A3=E3=83=BC=E3=83=AB=E3=83=89=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・Money クラスに currency フィールドを作成し、Dollar, Franc ごとに currency を設定するような書きぶりにする ・本来なら Currency オブジェクトを作りたいがまずは文字列だけで十分だと思われるので簡単に実装する --- src/__tests__/money.test.ts | 6 ++++-- src/money.ts | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/__tests__/money.test.ts b/src/__tests__/money.test.ts index 41560a0..7c4131b 100644 --- a/src/__tests__/money.test.ts +++ b/src/__tests__/money.test.ts @@ -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'); }); diff --git a/src/money.ts b/src/money.ts index df038f5..9ab937a 100644 --- a/src/money.ts +++ b/src/money.ts @@ -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 { @@ -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 { @@ -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 {