Skip to content

Commit

Permalink
✨ 【TODO】 丸め処理用の足し算メソッドを使用した書きぶりに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
dodonki1223 committed Jan 10, 2022
1 parent fa0b23d commit 9e8e57e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- [ ] $5 + $5がMoneyを返す
- [ ] Money の丸め処理どうする?

- [x] Money の丸め処理どうする?
- [x] 他のオブジェクトとの等価性比較
- [x] 5CHF * 2 = 10CHF
- [x] Sum.plus
Expand Down
6 changes: 5 additions & 1 deletion src/__tests__/money.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ test('test reduce sum', () => {
const sum = Money.dollar(3).plus(Money.dollar(4));
const bank = new Bank();
const reduced = bank.reduce(sum, 'USD');
expect(Money.dollar(7)).toEqual(reduced);
expect(reduced).toEqual(Money.dollar(7));

const sumDecimal = Money.dollar(0.7715).plus(Money.dollar(0.177));
const reducedDecimal = bank.reduce(sumDecimal, 'USD');
expect(reducedDecimal).toEqual(Money.dollar(0.9485));
});

test('test plus returns sum', () => {
Expand Down
7 changes: 3 additions & 4 deletions src/sum.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Bank } from "./bank";
import { Expression } from "./expression";
import { Expression, roundCalculate } from "./expression";
import { Currency, Money } from "./money";

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

plus(addend: Expression): Expression {
return new Sum(this, addend);
}
Expand All @@ -14,9 +15,7 @@ export class Sum implements Expression {
}

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

0 comments on commit 9e8e57e

Please sign in to comment.