Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .plus and .times to Expression interface #16

Merged
merged 6 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/expression/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Expression, { Sum } from '.';
import Bank from '../bank';
import Money from '../money';

describe('Sum', () => {
describe('plus', () => {
it('should sum two expressions', () => {
const fiveBucks: Expression = Money.dollar(5);
const tenFrancs: Expression = Money.franc(10);
const bank: Bank = new Bank();
bank.addRate('CHF', 'USD', 2);

const sum = new Sum(fiveBucks, tenFrancs).plus(fiveBucks);
const result = sum.reduce(bank, 'USD');

expect(result).toEqual(Money.dollar(15));
});
});

describe('times', () => {
it('should multiply a sum', () => {
const fiveBucks: Expression = Money.dollar(5);
const tenFrancs: Expression = Money.franc(10);
const bank: Bank = new Bank();
bank.addRate('CHF', 'USD', 2);

const sum = new Sum(fiveBucks, tenFrancs).times(2);
const result = sum.reduce(bank, 'USD');

expect(result).toEqual(Money.dollar(20));
});
});
});
7 changes: 6 additions & 1 deletion src/expression/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Money from '../money';
export default interface Expression {
reduce(bank: Bank, to: string): Money;
plus(addend: Expression): Expression;
times(multiplier: number): Expression;
}

export class Sum implements Expression {
Expand All @@ -30,6 +31,10 @@ export class Sum implements Expression {
}

plus(addend: Expression): Expression {
throw new Error('Method not implemented.');
return new Sum(this, addend);
}

times(multiplier: number): Expression {
return new Sum(this.augend.times(multiplier), this.addend.times(multiplier));
}
}
16 changes: 11 additions & 5 deletions src/money/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ describe('Money', () => {
});

describe('addition', () => {
it('should return a Sum expression', () => {
const five: Money = Money.dollar(5);
it('should return a Sum expression if currencies are not equal', () => {
const fiveBucks: Money = Money.dollar(5);
const fiveFrancs: Money = Money.franc(5);

const sumExp: Expression = five.plus(five);
const sumExp: Expression = fiveBucks.plus(fiveFrancs);
const sum = sumExp as Sum;

expect(sum.augend).toEqual(five);
expect(sum.addend).toEqual(five);
expect(sum.augend).toEqual(fiveBucks);
expect(sum.addend).toEqual(fiveFrancs);
});

it('should sum $5 + $5 and get $10', () => {
Expand All @@ -34,6 +35,11 @@ describe('Money', () => {

expect(reduced).toEqual(Money.dollar(10));
});

it('should return a Money object if the two currencies are the same', () => {
const result = Money.dollar(1).plus(Money.dollar(1));
expect(result).toBeInstanceOf(Money);
});
});

describe('multiplication', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/money/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export default class Money implements Expression {
}

plus(addend: Expression): Expression {
if (addend instanceof Money && (addend as Money).currency == this.currency)
return new Money(this.amount + addend.amount, this.currency);
return new Sum(this, addend);
}

Expand Down