-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ 【第14章 学習用テストと回帰テスト】 為替レートを管理するオブジェクトを作成する
・ただし、USD → USD の変換がまだうまくいっていないので修正する必要あり
- Loading branch information
1 parent
de1ed37
commit 1d52102
Showing
2 changed files
with
26 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
import { Expression } from './expression'; | ||
import { Currency, Money } from './money'; | ||
import { Pair } from './pair'; | ||
|
||
export class Bank { | ||
private rates: { [key in string]: number } = {} | ||
|
||
reduce(source: Expression, to: Currency):Money { | ||
return source.reduce(this, to); | ||
} | ||
|
||
addRate(from: string, to: string, rate: number) { | ||
addRate(from: Currency, to: Currency, rate: number) { | ||
const pair = new Pair(from, to); | ||
this.rates[pair.key] = rate; | ||
} | ||
|
||
rate(from: Currency, to: Currency): number { | ||
return from === 'CHF' && to === 'USD' ? 2 : 1 | ||
const pair = new Pair(from, to); | ||
return this.rates[pair.key] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Currency } from "./money"; | ||
|
||
export class Pair{ | ||
constructor(private from: Currency, private to: Currency){ | ||
} | ||
|
||
get key(): string { | ||
return `${this.from}_${this.to}`; | ||
} | ||
|
||
equals(object: Object): boolean { | ||
const pair = object as Pair; | ||
if (!pair) | ||
return false; | ||
|
||
return this.from === pair.from && this.to === pair.to; | ||
} | ||
} |