Skip to content

Commit

Permalink
✨ 【第14章 学習用テストと回帰テスト】 文字列をユニオン型で持たせるようにし取り回しやすいようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
dodonki1223 committed Dec 30, 2021
1 parent 5ce5130 commit de1ed37
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/bank.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Expression } from './expression';
import { Money } from './money';
import { Currency, Money } from './money';

export class Bank {
reduce(source: Expression, to: string):Money {
reduce(source: Expression, to: Currency):Money {
return source.reduce(this, to);
}

addRate(from: string, to: string, rate: number) {
}

rate(from: string, to: string): number {
rate(from: Currency, to: Currency): number {
return from === 'CHF' && to === 'USD' ? 2 : 1
}
}
4 changes: 2 additions & 2 deletions src/expression.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bank } from "./bank";
import { Money } from "./money";
import { Currency, Money } from "./money";

export interface Expression{
reduce(bank: Bank, to: string): Money;
reduce(bank: Bank, to: Currency): Money;
}
6 changes: 4 additions & 2 deletions src/money.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Bank } from "./bank";
import { Expression } from "./expression";
import { Sum } from "./sum";

export type Currency = 'CHF' | 'USD';

export class Money implements Expression {
constructor(public readonly amount: number, public readonly currency: string) {
constructor(public readonly amount: number, public readonly currency: Currency) {
}

static dollar(amount: number):Money {
Expand Down Expand Up @@ -34,7 +36,7 @@ export class Money implements Expression {
return new Sum(this, addend);
}

reduce(bank: Bank,to: string): Money {
reduce(bank: Bank,to: Currency): Money {
const rate = bank.rate(this.currency, to);
return new Money(this.amount / rate, to);
}
Expand Down
4 changes: 2 additions & 2 deletions src/sum.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Bank } from "./bank";
import { Expression } from "./expression";
import { Money } from "./money";
import { Currency, Money } from "./money";

export class Sum implements Expression {
constructor(public augend: Money, public addend: Money) {
}

reduce(bank: Bank, to: string):Money {
reduce(bank: Bank, to: Currency):Money {
const amount :number = this.augend.amount + this.addend.amount;
return new Money(amount, to);
}
Expand Down

0 comments on commit de1ed37

Please sign in to comment.