Skip to content

Commit

Permalink
✨ 【第14章 学習用テストと回帰テスト】 為替レートを管理するオブジェクトを作成する
Browse files Browse the repository at this point in the history
・ただし、USD → USD の変換がまだうまくいっていないので修正する必要あり
  • Loading branch information
dodonki1223 committed Dec 30, 2021
1 parent de1ed37 commit 1d52102
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/bank.ts
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]
}
}
18 changes: 18 additions & 0 deletions src/pair.ts
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;
}
}

0 comments on commit 1d52102

Please sign in to comment.