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

Remove duplication between Dollar and Franc #6

Merged
merged 9 commits into from
Sep 28, 2022
10 changes: 3 additions & 7 deletions src/dollar/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
export default class Dollar {
private amount: number;
import Money from '../money';

export default class Dollar extends Money {
constructor(amount: number) {
this.amount = amount;
super(amount);
}

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

equals(dollar: Object): boolean {
return this.amount == (dollar as Dollar).amount;
}
}
10 changes: 10 additions & 0 deletions src/franc/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Franc from '.';
import Dollar from '../dollar';

describe('Franc', () => {
describe('multiplication', () => {
Expand All @@ -16,4 +17,13 @@ describe('Franc', () => {
expect(product.equals(new Franc(15))).toBe(true);
});
});

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(new Dollar(5).equals(new Dollar(5))).toBe(true);
expect(new Dollar(5).equals(new Dollar(6))).toBe(false);
});
});
});
10 changes: 3 additions & 7 deletions src/franc/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
export default class Franc {
private amount: number;
import Money from '../money';

export default class Franc extends Money {
constructor(amount: number) {
this.amount = amount;
super(amount);
}

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

equals(dollar: Object): boolean {
return this.amount == (dollar as Franc).amount;
}
}
16 changes: 16 additions & 0 deletions src/money/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default class Money {
private _amount: number;

constructor(amount: number) {
this._amount = amount;
}

get amount() {
return this._amount;
}

equals(obj: Object): boolean {
const money: Money = obj as Money;
return this.amount == money.amount;
}
}