Skip to content

Commit

Permalink
✨ 【第14章 学習用テストと回帰テスト】 為替レートを Money が知っていることになってしまったので Bank に移動させる
Browse files Browse the repository at this point in the history
  • Loading branch information
dodonki1223 committed Dec 30, 2021
1 parent 0b95524 commit 5ce5130
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { Money } from './money';

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

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

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

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

Expand Down Expand Up @@ -33,8 +34,8 @@ export class Money implements Expression {
return new Sum(this, addend);
}

reduce(to: string): Money {
const rate = this.currency === 'CHF' && to === 'USD' ? 2 : 1;
return new Money(this.amount / rate, to)
reduce(bank: Bank,to: string): Money {
const rate = bank.rate(this.currency, to);
return new Money(this.amount / rate, to);
}
}
3 changes: 2 additions & 1 deletion src/sum.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Bank } from "./bank";
import { Expression } from "./expression";
import { Money } from "./money";

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

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

0 comments on commit 5ce5130

Please sign in to comment.