Skip to content
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
11 changes: 11 additions & 0 deletions src/domain/entities/juice.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { JuiceType } from '../objects/juiceType';
import { Juice } from './juice';

describe('juice', () => {
let ju!: Juice;

beforeEach(() => {
// テスト前にJuiceのインスタンスを作成する
ju = new Juice(JuiceType.COKE, 120);
});
});
44 changes: 14 additions & 30 deletions src/domain/entities/juice.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,29 @@
import { JuiceData, JuiceType } from '../objects/juiceType';

export class Juice {
constructor(
private internalName: string,
private internalPrice: number // private internalQuantity: number
private internalType: JuiceType,
private internalPrice: number
) {}

get name(): string {
return this.internalName;
return JuiceData[this.type].name;
}
get type(): JuiceType {
return this.internalType;
}

get price(): number {
return this.internalPrice;
}

// get quantity(): number {
// return this.internalQuantity;
// }

set name(name: string) {
this.internalName = name;
}

set price(price: number) {
this.internalPrice = price;
}

// set quantity(quantity: number) {
// this.internalQuantity = quantity;
// }

juiceInfo(): string {
return (
'name:' +
this.name +
' price:' +
new Intl.NumberFormat('ja-JP', {
style: 'currency',
currency: 'JPY',
}).format(this.price)
// ' stock:'
// this.internalQuantity +
// '本'
);
}
}

export type JuiceInfo = {
name: string;
price: number;
quantity: number;
};
2 changes: 1 addition & 1 deletion src/domain/objects/juiceType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export enum JuiceType {
WATER = 2,
}

const data = {
export const JuiceData = {
0: { name: 'コーラ' },
1: { name: 'レッドブル' },
2: { name: '水' },
Expand Down
15 changes: 0 additions & 15 deletions src/usecases/__tests__/juice.spec.ts

This file was deleted.

20 changes: 20 additions & 0 deletions src/usecases/cash/cash.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Cash } from './cash';

describe('cash', () => {
let c!: Cash;

beforeEach(() => {
// テスト前にcashのインスタンスを作成する
c = new Cash();
});

it('投入金額を取得', () => {
c.balance = 150;
expect(c.balance).toBe(150);
});

it('売上金額を取得', () => {
c.earning = 150;
expect(c.earning).toBe(150);
});
});
38 changes: 31 additions & 7 deletions src/usecases/cash/cash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,43 @@ export class Cash implements ICash {
return this.internalBalance;
}

set balance(balance: number) {
this.internalBalance = balance;
get earning(): number {
return this.internalEarning;
}

get earning(): number {
private isNaturalNumber(n: number): boolean {
if (n < 1 || n % 1 !== 0) {
throw new Error('nは自然数のみ指定できます');
}
return true;
}

addBalance(n: number): number {
this.isNaturalNumber(n);
this.internalBalance += n;
return this.internalBalance;
}

subBalance(n: number): number {
this.isNaturalNumber(n);
this.internalBalance -= n;
return this.internalBalance;
}

addEarning(n: number): number {
this.isNaturalNumber(n);
this.internalEarning += n;
return this.internalEarning;
}

set earning(earning: number) {
this.internalEarning = earning;
subEarning(n: number): number {
this.isNaturalNumber(n);
this.internalEarning -= n;
return this.internalEarning;
}

checkMoneyCondition(price: number): boolean {
return this.balance >= price;
resetBalance(): number {
this.internalBalance = 0;
return this.internalBalance;
}
}
2 changes: 0 additions & 2 deletions src/usecases/cash/iCash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ export interface ICash {
get earning(): number;

set earning(earning: number);

checkMoneyCondition(price: number): boolean;
}
45 changes: 45 additions & 0 deletions src/usecases/display/display.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Juice, JuiceInfo } from '../../domain/entities/juice';
import { JuiceData, JuiceType } from '../../domain/objects/juiceType';
import { Stock } from '../stock/stock';
import { Display } from './display';

describe('display', () => {
let d!: Display;
let s!: Stock;

beforeEach(() => {
// テスト前にstockのインスタンスを作成する
d = new Display();
s = new Stock();
});
it('ジュースの情報を取得する', () => {
expect(d.makeJuiceInfo(new Juice(JuiceType.COKE, 120), 5)).toEqual({
name: 'コーラ',
price: 120,
stock: 5,
});
});

it('自販機内に格納される飲み物の情報を取得する', () => {
s.add(new Juice(JuiceType.COKE, 120), 5);
s.add(new Juice(JuiceType.REDBULL, 200), 5);
s.add(new Juice(JuiceType.WATER, 100), 5);
expect(d.stocksInfo(s.stocks)).toEqual([
{
name: JuiceData[JuiceType.COKE].name,
price: 120,
stock: 5,
},
{
name: JuiceData[JuiceType.REDBULL].name,
price: 200,
stock: 5,
},
{
name: JuiceData[JuiceType.WATER].name,
price: 100,
stock: 5,
},
]);
});
});
42 changes: 14 additions & 28 deletions src/usecases/display/display.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
import { IDisplay } from './iDisplay';
import { Stock } from '../stock/stock';
import { Cash } from '../cash/cash';
import { JuiceType } from '../../domain/objects/juiceType';
import { Juice } from '../../domain/entities/juice';
import { JuiceData, JuiceType } from '../../domain/objects/juiceType';
import { Juice, JuiceInfo } from '../../domain/entities/juice';

export class Display implements IDisplay {
Cash = new Cash();
Stock = new Stock();

makeStockInfoRow(juice: Juice, quantity: number) {
return juice.juiceInfo() + ` stock:${quantity}本`;
makeJuiceInfo(juice: Juice, quantity: number) {
const juiceInfo: JuiceInfo = {
name: JuiceData[juice.type].name,
price: juice.price,
stock: quantity,
};
return juiceInfo;
}

stocksInfo(stocks: Map<JuiceType, { juice: Juice; quantity: number }>) {
const stocksInfo: string[] = [];
stocksInfo(
stocks: Map<JuiceType, { juice: Juice; quantity: number }>
): JuiceInfo[] {
const stocksInfo: JuiceInfo[] = [];
for (const [_, stock] of stocks) {
stocksInfo.push(this.makeStockInfoRow(stock.juice, stock.quantity));
stocksInfo.push(this.makeJuiceInfo(stock.juice, stock.quantity));
}
return stocksInfo;
}

acquireBuyableList(
stocks: Map<JuiceType, { juice: Juice; quantity: number }>
) {
const buyableList: string[] = [];
for (const [juiceType, stock] of stocks) {
if (
this.Stock.checkStockCondition(juiceType) &&
this.Cash.checkMoneyCondition(stock.juice.price)
) {
buyableList.push(this.makeStockInfoRow(stock.juice, stock.quantity));
}
}

return buyableList;
}
}
10 changes: 3 additions & 7 deletions src/usecases/display/iDisplay.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { Juice } from '../../domain/entities/juice';
import { Juice, JuiceInfo } from '../../domain/entities/juice';
import { JuiceType } from '../../domain/objects/juiceType';

export interface IDisplay {
makeStockInfoRow(juice: Juice, quantity: number): string;
makeJuiceInfo(juice: Juice, quantity: number): JuiceInfo;

stocksInfo(
stocks: Map<JuiceType, { juice: Juice; quantity: number }>
): string[];

acquireBuyableList(
stocks: Map<JuiceType, { juice: Juice; quantity: number }>
): string[];
): JuiceInfo[];
}
3 changes: 1 addition & 2 deletions src/usecases/stock/iStock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ import { JuiceType } from '../../domain/objects/juiceType';

export interface IStock {
get stocks(): Map<JuiceType, { juice: Juice; quantity: number }>;
addStock(juiceType: JuiceType, juice: Juice, quantity: number): void;
checkStockCondition(juiceType: JuiceType): boolean;
add(juice: Juice, quantity: number): void;
}
27 changes: 27 additions & 0 deletions src/usecases/stock/stock.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Juice } from '../../domain/entities/juice';
import { JuiceData, JuiceType } from '../../domain/objects/juiceType';
import { Stock } from './stock';

describe('stock', () => {
let s!: Stock;

beforeEach(() => {
// テスト前にstockのインスタンスを作成する
s = new Stock();
});

it('在庫を追加', () => {
const juice = new Juice(JuiceType.COKE, 120);

expect(s.add(juice, 5)).toBe(true);
});

it('在庫を取得する', () => {
const juice = new Juice(JuiceType.COKE, 120);

s.add(juice, 5);
expect(s.stocks).toEqual(
new Map([[JuiceType.COKE, { juice, quantity: 5 }]])
);
});
});
Loading