Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

16장까지 예제 추가 #3

Open
wants to merge 2 commits into
base: feat/chapter9
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/chapters/chapter12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class Money implements Expression {
return `${this.amount} ${this._currency}`;
}

public plus(added: Money): Expression {
return new Money(this.amount + added.amount, this._currency);
public plus(addend: Money): Expression {
return new Money(this.amount + addend.amount, this._currency);
}
}

Expand Down
111 changes: 111 additions & 0 deletions src/chapters/chapter13.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { Bank, Money } from "./chapter13";
import type { Expression } from "./chapter13";
import { Sum } from "./chapter13";

/*
// attempt 1
describe("chapter 13", () => {
test("multiplication", () => {
const five: Money = Money.dollar(5);
expect(Money.dollar(10)).toEqual(five.times(2));
expect(Money.dollar(15)).toEqual(five.times(3));
});

test("francMultiplication", () => {
const five = Money.franc(5);
expect(Money.franc(10)).toEqual(five.times(2));
expect(Money.franc(15)).toEqual(five.times(3));
});

test("equality", () => {
expect(Money.dollar(5).equals(Money.dollar(5))).toBe(true);
expect(Money.dollar(5).equals(Money.dollar(6))).toBe(false);
expect(Money.franc(5).equals(Money.dollar(5))).toBe(false);
});

test("currency", () => {
expect(Money.dollar(1).currency()).toBe("USD");
expect(Money.franc(1).currency()).toBe("CHF");
});

test("simpleAddition", () => {
const five = Money.dollar(5);
const sum: Expression = five.plus(five);
const bank = new Bank();
const reduced = bank.reduce(sum, "USD");
expect(Money.dollar(10)).toEqual(reduced);
});

test("plusReturnsSum", () => {
const five = Money.dollar(5);
const result = five.plus(five);
const sum: Sum = result;

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

test("reduceSum", () => {
const sum = new Sum(Money.dollar(3), Money.dollar(4));
const bank = new Bank();
const result = bank.reduce(sum, "USD");
expect(Money.dollar(7)).toEqual(result);
});
});
*/

// attempt 2
describe("chapter 13", () => {
test("multiplication", () => {
const five: Money = Money.dollar(5);
expect(Money.dollar(10)).toEqual(five.times(2));
expect(Money.dollar(15)).toEqual(five.times(3));
});

test("francMultiplication", () => {
const five = Money.franc(5);
expect(Money.franc(10)).toEqual(five.times(2));
expect(Money.franc(15)).toEqual(five.times(3));
});

test("equality", () => {
expect(Money.dollar(5).equals(Money.dollar(5))).toBe(true);
expect(Money.dollar(5).equals(Money.dollar(6))).toBe(false);
expect(Money.franc(5).equals(Money.dollar(5))).toBe(false);
});

test("currency", () => {
expect(Money.dollar(1).currency()).toBe("USD");
expect(Money.franc(1).currency()).toBe("CHF");
});

test("simpleAddition", () => {
const five = Money.dollar(5);
const sum: Expression = five.plus(five);
const bank = new Bank();
const reduced = bank.reduce(sum, "USD");
expect(Money.dollar(10)).toEqual(reduced);
});

test("plusReturnsSum", () => {
const five = Money.dollar(5);
const result = five.plus(five);
const sum: Sum = result;

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

test("reduceSum", () => {
const sum = new Sum(Money.dollar(3), Money.dollar(4));
const bank = new Bank();
const result = bank.reduce(sum, "USD");
expect(Money.dollar(7)).toEqual(result);
});

test("reduceMoney", () => {
const bank = new Bank();
const result = bank.reduce(Money.dollar(1), "USD");
expect(Money.dollar(1)).toEqual(result);
});
});
216 changes: 216 additions & 0 deletions src/chapters/chapter13.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
// attempt 1
export class Money implements Expression {
protected _amount: number;
protected _currency: string;

constructor(amount: number, currency: string) {
this._amount = amount;
this._currency = currency;
}

public get amount(): number {
return this._amount;
};

public static dollar(amount: number): Money {
return new Money(amount, "USD");
}

public static franc(amount: number): Money {
return new Money(amount, "CHF");
}

public equals(object: Object) {
const money = object as Money;
return this._amount === money._amount && this._currency === money.currency();
}

public times(multiplier: number): Money {
return new Money(this._amount * multiplier, this._currency);
}

public currency(): string {
return this._currency;
}

public toString() {
return `${this._amount} ${this._currency}`;
}

public plus(addend: Money): Sum {
return new Sum(this, addend);
}
}

export interface Expression {}

export class Bank {
public reduce(source: Expression, to: string): Money {
const sum = source as Sum;
return sum.reduce(to);
}
}

export class Sum implements Expression {
public augend: Money;
public addend: Money;

constructor(augend: Money, addend: Money) {
this.augend = augend;
this.addend = addend;
}

public reduce(to: string) {
const amount = this.augend.amount + this.addend.amount;
return new Money(amount, to);
}
}
*/

/*
// attempt 2
export class Money implements Expression {
protected _amount: number;
protected _currency: string;

constructor(amount: number, currency: string) {
this._amount = amount;
this._currency = currency;
}

public get amount(): number {
return this._amount;
};

public static dollar(amount: number): Money {
return new Money(amount, "USD");
}

public static franc(amount: number): Money {
return new Money(amount, "CHF");
}

public equals(object: Object) {
const money = object as Money;
return this._amount === money._amount && this._currency === money.currency();
}

public times(multiplier: number): Money {
return new Money(this._amount * multiplier, this._currency);
}

public currency(): string {
return this._currency;
}

public toString() {
return `${this._amount} ${this._currency}`;
}

public plus(addend: Money): Sum {
return new Sum(this, addend);
}
}

export interface Expression {}

export class Bank {
public reduce(source: Expression, to: string): Money {
if (source instanceof Money) {
return source;
}

const sum = source as Sum;
return sum.reduce(to);
}
}

export class Sum implements Expression {
public augend: Money;
public addend: Money;

constructor(augend: Money, addend: Money) {
this.augend = augend;
this.addend = addend;
}

public reduce(to: string) {
const amount = this.augend.amount + this.addend.amount;
return new Money(amount, to);
}
}
*/

// attempt 3
export class Money implements Expression {
protected _amount: number;
protected _currency: string;

constructor(amount: number, currency: string) {
this._amount = amount;
this._currency = currency;
}

public get amount(): number {
return this._amount;
};

public static dollar(amount: number): Money {
return new Money(amount, "USD");
}

public static franc(amount: number): Money {
return new Money(amount, "CHF");
}

public equals(object: Object) {
const money = object as Money;
return this._amount === money._amount && this._currency === money.currency();
}

public times(multiplier: number): Money {
return new Money(this._amount * multiplier, this._currency);
}

public currency(): string {
return this._currency;
}

public toString() {
return `${this._amount} ${this._currency}`;
}

public plus(addend: Money): Sum {
return new Sum(this, addend);
}

public reduce(to: string) {
return this;
}
}

export interface Expression {
reduce(to: string): Money;
}

export class Bank {
public reduce(source: Expression, to: string): Money {
return source.reduce(to);
}
}

export class Sum implements Expression {
public augend: Money;
public addend: Money;

constructor(augend: Money, addend: Money) {
this.augend = augend;
this.addend = addend;
}

public reduce(to: string) {
const amount = this.augend.amount + this.addend.amount;
return new Money(amount, to);
}
}
Loading