Skip to content

Commit

Permalink
✨ 【第10章 テストに聞いてみる】 サブクラスたちの times の実装の差異をなくすために times の一般化
Browse files Browse the repository at this point in the history
・差異をなくすためにベタ書きの値を変数に置き換え
・サブラクラスではそれぞれサブクラスのインスタンスを返すようにしていたが、今回は Money クラスを返すように変更した
  • Loading branch information
dodonki1223 committed Dec 19, 2021
1 parent 3d8d590 commit 87cfc55
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
- [ ] 5CHF * 2 = 10CHF
- [ ] Dollar と Franc の重複
- [x] equalsの一般化
- [ ] timesの一般化
- [x] timesの一般化
- [x] FrancとDollarの比較
- [x] 通貨の概念
14 changes: 4 additions & 10 deletions src/money.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// import { Dollar } from "./dollar";
// import { Franc } from "./franc";

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

Expand All @@ -13,7 +13,9 @@ export 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)
}

equals(money: Money | null) {
if (money === null) {
Expand All @@ -28,18 +30,10 @@ export class Dollar extends Money {
constructor(amount: number, currency: string) {
super(amount, currency);
}

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

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

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

0 comments on commit 87cfc55

Please sign in to comment.