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

Introduce factory methods for franc and dollar #8

Merged
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
21 changes: 0 additions & 21 deletions src/dollar/index.spec.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/dollar/index.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/franc/index.spec.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/franc/index.ts

This file was deleted.

47 changes: 42 additions & 5 deletions src/money/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
import Dollar from '../dollar';
import Franc from '../franc';
import Money, { Dollar, Franc } from '../money';

describe('Money', () => {
describe('equality', () => {
it('should consider an object representing $5 equals to another object representing $5', () => {
expect(new Franc(5).equals(new Franc(5))).toBe(true);
expect(new Franc(5).equals(new Franc(6))).toBe(false);
expect(Money.franc(5).equals(Money.franc(5))).toBe(true);
expect(Money.franc(5).equals(Money.franc(6))).toBe(false);
expect(new Dollar(5).equals(new Dollar(5))).toBe(true);
expect(new Dollar(5).equals(new Dollar(6))).toBe(false);
expect(new Franc(5).equals(new Dollar(5))).toBe(false);
expect(Money.franc(5).equals(new Dollar(5))).toBe(false);
});
});
});

describe('Dollar', () => {
it.todo('should sum $5 + 10CHF as being $10 if exchange rate is 2:1');

describe('multiplication', () => {
it('should multiply $5 by 2 and get $10', () => {
const five: Money = Money.dollar(5);
const product = five.times(2);
expect(product.equals(Money.dollar(10))).toBe(true);
});

it('should preserve the initial dollar amount when multiplying multiple times', () => {
const five = Money.dollar(5);
let product: Dollar = five.times(2);
expect(product.equals(Money.dollar(10))).toBe(true);
product = five.times(3);
expect(product.equals(Money.dollar(15))).toBe(true);
});
});
});

describe('Franc', () => {
describe('multiplication', () => {
it('should multiply $5 by 2 and get $10', () => {
const five = Money.franc(5);
const product = five.times(2);
expect(product.equals(Money.franc(10))).toBe(true);
});

it('should preserve the initial dollar amount when multiplying multiple times', () => {
const five = Money.franc(5);
let product: Franc = five.times(2);
expect(product.equals(Money.franc(10))).toBe(true);
product = five.times(3);
expect(product.equals(Money.franc(15))).toBe(true);
});
});
});
34 changes: 33 additions & 1 deletion src/money/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default class Money {
abstract class Money {
private _amount: number;

constructor(amount: number) {
Expand All @@ -13,4 +13,36 @@ export default class Money {
const money: Money = obj as Money;
return this.amount == money.amount && obj.constructor.name == this.constructor.name;
}

static dollar(amount: number): Money {
return new Dollar(amount);
}

static franc(amount: number): Money {
return new Franc(amount);
}

abstract times(multiplier: number): Money;
}

export class Dollar extends Money {
constructor(amount: number) {
super(amount);
}

times(multiplier: number): Dollar {
return new Dollar(this.amount * multiplier);
}
}

export class Franc extends Money {
constructor(amount: number) {
super(amount);
}

times(multiplier: number): Franc {
return new Franc(this.amount * multiplier);
}
}

export default Money;