code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,132 @@
+import { FORMAT_MESSAGE, OUTPUT_MESSAGE } from './module';
+
+describe('๋ฉ์์ง ํฌ๋งท ํ
์คํธ', () => {
+ describe('orderMenus ํ
์คํธ', () => {
+ test.each([
+ {
+ description: '์ถ๋ ฅ ๊ฒฐ๊ณผ๋ "ํผ์ 2๊ฐ - ์ฝ๋ผ 1๊ฐ" ์ด๋ค.',
+ input: [
+ ['ํผ์', 2],
+ ['์ฝ๋ผ', 1],
+ ],
+ output: 'ํผ์ 2๊ฐ\n์ฝ๋ผ 1๊ฐ\n',
+ },
+ ])('$description', ({ input, output }) => {
+ // given - when - then
+ expect(FORMAT_MESSAGE.orderMenus(input)).toBe(output);
+ });
+ });
+
+ describe('amount ํ
์คํธ', () => {
+ test.each([
+ { input: 5000, output: '5,000์' },
+ { input: 0, output: '0์' },
+ ])('amount๊ฐ $input์ผ ๋, ์ถ๋ ฅ ๋ฉ์์ง๋ "$output"์ด์ด์ผ ํ๋ค', ({ input, output }) => {
+ // given - when - then
+ expect(FORMAT_MESSAGE.amount(input)).toBe(output);
+ });
+ });
+
+ describe('benefitHistory ํ
์คํธ', () => {
+ test.each([
+ {
+ description:
+ '์ถ๋ ฅ ๊ฒฐ๊ณผ๋ "ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: -1,200์ - ํ์ผ ํ ์ธ: -4,046์ - ํน๋ณ ํ ์ธ: -1,000์ - ์ฆ์ ์ด๋ฒคํธ: -25,000์"์ด๋ค.',
+ input: {
+ xmasBenefitAmount: 1200,
+ weekDayBenefitAmount: 4046,
+ specialBenefitAmount: 1000,
+ giftAmount: 25000,
+ },
+ output:
+ 'ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: -1,200์\nํ์ผ ํ ์ธ: -4,046์\nํน๋ณ ํ ์ธ: -1,000์\n์ฆ์ ์ด๋ฒคํธ: -25,000์',
+ },
+ {
+ description: `ํ๋ก๋ชจ์
ํํ์ด ๋ชจ๋ 0์์ธ ๊ฒฝ์ฐ ๊ฒฐ๊ณผ๋ "${OUTPUT_MESSAGE.nothing}"์ด๋ค.`,
+ input: {
+ xmasBenefitAmount: 0,
+ weekendBenefitAmount: 0,
+ weekDayBenefitAmount: 0,
+ specialBenefitAmount: 0,
+ giftAmount: 0,
+ },
+ output: OUTPUT_MESSAGE.nothing,
+ },
+ ])('$description', ({ input, output }) => {
+ // given - when - then
+ expect(FORMAT_MESSAGE.benefitHistory(input)).toBe(output);
+ });
+ });
+
+ describe('title ํ
์คํธ', () => {
+ test.each([
+ {
+ description: 'newLine ์ต์
์ด false์ผ ๊ฒฝ์ฐ, ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ "<์ ๋ชฉ>"์ด๋ค.',
+ input: { config: { newLine: false }, title: '์ ๋ชฉ' },
+ output: '<์ ๋ชฉ>',
+ },
+ {
+ description: 'newLine ์ต์
์ด true์ผ ๊ฒฝ์ฐ, ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ "\\n<์ ๋ชฉ>"์ด๋ค.',
+ input: { config: { newLine: true }, title: '์ ๋ชฉ' },
+ output: '\n<์ ๋ชฉ>',
+ },
+ ])('$description', ({ input, output }) => {
+ // given - when - then
+ expect(FORMAT_MESSAGE.title(input.config, input.title)).toBe(output);
+ });
+ });
+
+ describe('gift ํ
์คํธ', () => {
+ test.each([
+ {
+ description: '์ฆ์ ์ด๋ฒคํธ ๊ธ์ก์ด ์๋ ๊ฒฝ์ฐ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ "์ดํ์ธ 1๊ฐ" ์ด๋ค.',
+ input: 25000,
+ output: '์ดํ์ธ 1๊ฐ',
+ },
+ {
+ description: `์ฆ์ ์ด๋ฒคํธ ๊ธ์ก์ด ์๋ ๊ฒฝ์ฐ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ "${OUTPUT_MESSAGE.nothing}" ์ด๋ค.`,
+ input: 0,
+ output: OUTPUT_MESSAGE.nothing,
+ },
+ ])('$description', ({ input: giftAmount, output }) => {
+ // given - when - then
+ expect(FORMAT_MESSAGE.gift(giftAmount)).toBe(output);
+ });
+ });
+
+ describe('totalBenefitAmount ํ
์คํธ', () => {
+ test.each([
+ {
+ description: '์
๋ ฅ์ด 25000์ธ ๊ฒฝ์ฐ "-25,000์"์ ๋ฐํํ๋ค.',
+ input: 25000,
+ output: '-25,000์',
+ },
+ {
+ description: '์
๋ ฅ์ด 0์ธ ๊ฒฝ์ฐ "0์"์ ๋ฐํํ๋ค.',
+ input: 0,
+ output: '0์',
+ },
+ ])('$description', ({ input: totalBenefitAmount, output }) => {
+ // given - when - then
+ expect(FORMAT_MESSAGE.totalBenefitAmount(totalBenefitAmount)).toBe(output);
+ });
+ });
+
+ describe('eventBadge ํ
์คํธ', () => {
+ test.each([
+ {
+ description: '์
๋ ฅ์ด "์ฐํ"์ธ ๊ฒฝ์ฐ ๊ทธ๋๋ก "์ฐํ"๋ฅผ ๋ฐํํ๋ค.',
+ input: '์ฐํ',
+ output: '์ฐํ',
+ },
+ {
+ description: `์
๋ ฅ ๊ฐ์ด null์ธ ๊ฒฝ์ฐ "${OUTPUT_MESSAGE.nothing}"์ ๋ฐํํ๋ค.`,
+ input: null,
+ output: OUTPUT_MESSAGE.nothing,
+ },
+ ])('$description', ({ input: eventBadge, output }) => {
+ // given - when - then
+ expect(FORMAT_MESSAGE.eventBadge(eventBadge)).toBe(output);
+ });
+ });
+}); | JavaScript | ํ
์คํธ ์ผ์ด์ค๊ฐ ์ถฉ๋ถํ ๋ ์๊ธธ ์ ์๋ค๊ณ ์๊ฐํด์ each๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ ์๊ณ , ํ
์คํธ ์ผ์ด์ค๋ฅผ ๋ณ์๋ช
๊ณผ ํจ๊ป ์ฌ์ฉํ๊ธฐ ํธํด์ ์ฌ์ฉํ๋๊ฒ๋ ์์ต๋๋ค.
์ ๊ฐ description์ ๊ตณ์ด ๋ ์ด์ ๋ ๋ฐ์ดํฐ๊ฐ ์ค๋ฐ๊ฟ์ด ์์ด์ ํ๋ฉด์์ ๋ง์ด ๊นจ์ ธ์ ๊ทธ๊ฑธ ์ข ๋ ๊ฐ์ ํ๊ณ ์ ์ถ๊ฐํ์ต๋๋ค! |
@@ -0,0 +1,53 @@
+import { PROMOTION_DATE_INFO } from '../../constants/promotionSystem.js';
+
+// constant.js๋ ํ
์คํธ ์ฝ๋ ๋ฐ module.js์์๋ง ์ฌ์ฉ
+export const INITIAL_PROMOTION_BENEFIT_RESULT = Object.freeze({
+ xmasBenefitAmount: 0,
+ weekDayBenefitAmount: 0,
+ weekendBenefitAmount: 0,
+ specialBenefitAmount: 0,
+ giftAmount: 0,
+});
+
+export const MINIMUM_TOTAL_ORDER_AMOUNT = 10000;
+
+export const BENEFIT_DATE_INFO = Object.freeze({
+ startDate: new Date(`${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-01`),
+ endDate: new Date(`${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-31`),
+ christmas: new Date(`${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-25`),
+});
+
+export const DAY_OF_BENEFIT_CONDITION = Object.freeze({
+ weekday: Object.freeze({
+ menuCategory: '๋์ ํธ',
+ days: [0, 1, 2, 3, 4],
+ }),
+
+ weekend: Object.freeze({
+ menuCategory: '๋ฉ์ธ',
+ days: [5, 6],
+ }),
+});
+
+export const BENEFIT_AMOUNT_INFO = Object.freeze({
+ everyDay: 100,
+ christmas: 1000,
+ dayOfWeek: 2023,
+ special: 1000,
+});
+
+export const MINIMUM_ORDER_AMOUNT_FOR_GIFT = 120000;
+
+export const GIFT_INFO = {
+ menuCategory: '์๋ฃ',
+ menuName: '์ดํ์ธ',
+};
+
+export const SPECIAL_DATES = new Set([
+ `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-03`,
+ `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-10`,
+ `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-17`,
+ `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-24`,
+ `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-25`,
+ `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-31`,
+]); | JavaScript | ์๋ก์ด ๊ฐ๋ฐ์๊ฐ ๋ค์ด์ค๋ฉด ๊ทธ ๋ถ์ด ์ค์ค๋ก ํ๋จํ ๊ฒ ์๋๋ผ ํ๋ก์ ํธ์ ๋ํ ์ค๋ช
์ ์ถฉ๋ถํ ์ธ์์ธ๊ณ ๋ฐ๋๊ฒ ๋จผ์ ๋ผ๊ณ ์๊ฐํฉ๋๋ค.
์ ๊ฐ ๋ฐ๋ก ๊ด๋ฆฌํ ์ด์ ๋ ์์๋ฅผ ๊ธฐ์กด์๋ ํ
์คํธ์ฝ๋์ ์์ค ์ฝ๋์์ ์ฌ ์ฌ์ฉ๋๋ ๊ฐ๋ค์ ๊ด๋ฆฌํ๊ณ ์ ์์์ ๊ด๋ จ๋ ๋๋ฉ์ธ์์ ๊ด๋ฆฌ๋ฅผ ํ์ฌ ์ฝ๋ ์์ง์ฑ์ ๊ฐํ ํ์์ต๋๋ค.
์ด๋ฒ ๋ฏธ์
์ ๊ฒฝ์ฐ ์์ ๊ฐ์ด ๋๋ฌด ๋ง์ด ๋ฐ์ํด์ ๋ก์ง ๋ด๋ถ๋ฅผ ํ์
ํ๊ธฐ ์ด๋ ค์ ์๊ณ (๋ฌผ๋ก ์ด ๋ถ๋ถ์ ๋ ๋ถ๋ฆฌ๋ฅผ ํ๋ค๋ฉด ๊ฐ์ ๊ฐ๋ฅํ์๊ฑฐ๋ผ๊ณ ์๊ฐํด์.) ์ธ์คํด์ค์ ์๋ช
์ฃผ๊ธฐ๊ฐ ๋์ฒด๋ก ์งง์ ํ์ ํจ์ํ ๋ชจ๋๋ก ๊ฐ์ ์ ํ์ต๋๋ค.
๊ทธ ๊ณผ์ ์์ ๋๋ฉ์ธ static ์์๋ฅผ ์ด๋ป๊ฒ ๊ด๋ฆฌํ ๊น ๊ณ ๋ฏผํ๊ณ , constants์ ์๋ ์์๋ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉ๋๋ ์์๋ก์จ ๊ด๋ฆฌ ๋๊ณ ์์๊ธฐ ๋๋ฌธ์, ์ฌ๋ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฐพ์๋ณด๋ค toss/slash์ three.js์์ ์์๋ฅผ ๋๋ ํ ๋ฆฌ ๋ด๋ถ์ constant.js๋ก ๊ด๋ฆฌํด์ ์ ๋ ๋๋ ํ ๋ฆฌ ๋ด๋ถ์ ์๋ constant.js๋ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉํ๋ ๊ฒ์ด ์๋ ์์ค์ฝ๋์ ๋๋ฉ์ธ์ ํ
์คํธ ์ฝ๋์์ ์ฌ ์ฌ์ฉํ๊ธฐ ์ํด ์ถ๊ฐํ์ต๋๋ค!
๊ทธ๋ ๊ฒ ํจ์ผ๋ก์จ ๋ชจ๋ ๋ด๋ถ์ ๋ก์ง์ ์ง์คํ ์ ์๊ณ , ๊ด์ฌ์ฌ๊ฐ ๋ถ๋ฆฌ๋๋ค๊ณ ์๊ฐํ์ด์. |
@@ -0,0 +1,49 @@
+import InputView from '../views/InputView.js';
+import OutputView from '../views/OutputView.js';
+import PromotionResultService from '../service/PromotionResultService.js';
+import ErrorHandler from '../errors/ErrorHandler/module.js';
+
+/**
+ * @module ChristmasPromotionController
+ * ํฌ๋ฆฌ์ค๋ง์ค ํ๋ก๋ชจ์
์ด๋ฒคํธ ๊ด๋ จ ์ ํ๋ฆฌ์ผ์ด์
์ ํ๋ฆ ์ ์ด๋ฅผ ๋ด๋น
+ */
+const ChristmasPromotionController = {
+ /**
+ * @returns {Promise<void>}
+ */
+ async play() {
+ const { visitDate, orderMenuInfo } = await processUserInput();
+
+ processPromotionResult({ visitDate, orderMenuInfo });
+ },
+};
+
+/**
+ * @returns {Promise<{visitDate : number, orderMenuInfo : [string, number][]}>} ์ ์ ๊ฐ ์
๋ ฅํ ๋ฐฉ๋ฌธ ์ผ์ ๋ฐ ๋ฉ๋ด ์ ๋ณด๋ฅผ ๋ฐํํ Promise ๊ฐ์ฒด
+ */
+async function processUserInput() {
+ OutputView.printStartGuideComments();
+
+ const visitDate = await ErrorHandler.retryOnErrors(InputView.readVisitDate.bind(InputView));
+ const orderMenuInfo = await ErrorHandler.retryOnErrors(
+ InputView.readOrderMenuInfo.bind(InputView),
+ );
+
+ return { visitDate, orderMenuInfo };
+}
+
+/**
+ * @param {{visitDate : number, orderMenuInfo : [string, number][]}} params - ์ ์ ๊ฐ ์
๋ ฅํ ๋ฐฉ๋ฌธ ์ผ์ ๋ฐ ๋ฉ๋ด ์ ๋ณด
+ * @returns {void}
+ */
+function processPromotionResult({ visitDate, orderMenuInfo }) {
+ const promotionResult = PromotionResultService.createPromotionResult({
+ visitDate,
+ orderMenuInfo,
+ });
+
+ OutputView.printEndGuideComments(visitDate);
+ OutputView.printPromotionResult({ orderMenuInfo, promotionResult });
+}
+
+export default ChristmasPromotionController; | JavaScript | mvc๋ฅผ ์์๋ ๋ถ๋ค์ ์ฝ๊ฒ ์ดํดํ์๊ฒ ์ง๋ง controller์ ์ญํ ์ ์ดํดํ์์ง ๋ชปํ๋ ๋ถ๋ค๋ ์์๊ฑฐ๋ผ ์๊ฐํด์ ์ฃผ์์ ๋จ๊ฒผ์ต๋๋ค.
๋ํ, ์ฝ๋์ ์ผ๊ด์ฑ์ ์ด์ ๋ ์์๋๊ฑฐ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,277 @@
+import { PROMOTION_DATE_INFO } from '../../constants/promotionSystem';
+import {
+ BENEFIT_AMOUNT_INFO,
+ MINIMUM_ORDER_AMOUNT_FOR_GIFT,
+ INITIAL_PROMOTION_BENEFIT_RESULT,
+ MINIMUM_TOTAL_ORDER_AMOUNT,
+} from './constant';
+import DecemberPromotionPlan from './module';
+
+describe('12์ ์ด๋ฒคํธ ๊ณํ์ ๋ฐ๋ฅธ ํํ ๊ณ์ฐ ํ
์คํธ', () => {
+ const createBenefitResult = (ordererInfo) => DecemberPromotionPlan.execute(ordererInfo);
+
+ const createOrdererInfoTestCase = ({
+ visitDate = 3,
+ orderMenuInfo = [
+ ['ํฐ๋ณธ์คํ
์ดํฌ', 1],
+ ['๋ฐ๋นํ๋ฆฝ', 1],
+ ['์ด์ฝ์ผ์ดํฌ', 2],
+ ['์ ๋ก์ฝ๋ผ', 1],
+ ],
+ totalOrderAmount = 142000,
+ } = {}) => ({
+ visitDate,
+ orderMenuInfo,
+ totalOrderAmount,
+ });
+
+ describe('ํ๋ก๋ชจ์
์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: `5000์์ ${MINIMUM_TOTAL_ORDER_AMOUNT}์ ๋ฏธ๋ง ์ด๊ธฐ ๋๋ฌธ์ ํ๋ก๋ชจ์
์ ์ฉ์ด ๋์ง ์๋๋ค.`,
+ ordererInfo: {
+ visitDate: 3,
+ orderMenuInfo: ['์์ด์คํฌ๋ฆผ', 1],
+ totalOrderAmount: 5000,
+ },
+ expectedBenefitInfo: {
+ ...INITIAL_PROMOTION_BENEFIT_RESULT,
+ },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+
+ // then
+ expect(benefitResult).toStrictEqual(expectedBenefitInfo);
+ });
+ });
+
+ describe('ํ์ผ ํ ์ธ ์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 3์ผ์ ํ์ผ์ด๊ธฐ ๋๋ฌธ์ ํ ์ธ์ด ์ ์ฉ๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase(),
+ },
+ expectedBenefitInfo: {
+ weekDayBenefitAmount: 2 * BENEFIT_AMOUNT_INFO.dayOfWeek,
+ },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 2์ผ์ ์ฃผ๋ง์ด๊ธฐ ๋๋ฌธ์ ํ ์ธ์ด ์ ์ฉ๋์ง ์๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase({
+ visitDate: 2,
+ }),
+ },
+ expectedBenefitInfo: {
+ weekDayBenefitAmount: 0,
+ },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+
+ // then
+ expect(benefitResult.weekDayBenefitAmount).toBe(expectedBenefitInfo.weekDayBenefitAmount);
+ });
+ });
+
+ describe('์ฃผ๋ง ํ ์ธ ์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 2์ผ์ ์ฃผ๋ง์ด๊ธฐ ๋๋ฌธ์ ํ ์ธ์ด ์ ์ฉ๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase({
+ visitDate: 2,
+ }),
+ },
+ expectedBenefitInfo: {
+ weekendBenefitAmount: BENEFIT_AMOUNT_INFO.dayOfWeek * 2,
+ },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 3์ผ์ ํ์ผ์ด๊ธฐ ๋๋ฌธ์ ํ ์ธ์ด ์ ์ฉ๋์ง ์๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase(),
+ },
+ expectedBenefitInfo: {
+ weekendBenefitAmount: 0,
+ },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+
+ // then
+ expect(benefitResult.weekendBenefitAmount).toBe(expectedBenefitInfo.weekendBenefitAmount);
+ });
+ });
+
+ describe('12์ ์ด๋ฒคํธ ์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 1์ผ์ ${PROMOTION_DATE_INFO.month}์ ํ ์ธ์ด ์ ์ฉ๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase({ visitDate: 1 }),
+ },
+ expectedBenefitInfo: {
+ xmasBenefitAmount: BENEFIT_AMOUNT_INFO.christmas,
+ giftAmount: 25000,
+ specialBenefitAmount: 0,
+ weekDayBenefitAmount: 0,
+ weekendBenefitAmount: BENEFIT_AMOUNT_INFO.dayOfWeek * 2,
+ },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 31์ผ์ 12์ ํ ์ธ์ด ์ ์ฉ๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase({
+ visitDate: 31,
+ }),
+ },
+ expectedBenefitInfo: {
+ xmasBenefitAmount: 0,
+ weekDayBenefitAmount: BENEFIT_AMOUNT_INFO.dayOfWeek * 2,
+ weekendBenefitAmount: 0,
+ specialBenefitAmount: BENEFIT_AMOUNT_INFO.special,
+ giftAmount: 25000,
+ },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+
+ // then
+ expect(benefitResult).toStrictEqual(expectedBenefitInfo);
+ });
+ });
+
+ describe('์ฆ์ ์ด๋ฒคํธ ์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: `88000์์ ${MINIMUM_ORDER_AMOUNT_FOR_GIFT}๋ง์ ๋ฏธ๋ง์ด๋ฏ๋ก ์ดํ์ธ์ด ์ฆ์ ๋์ง ์๋๋ค.`,
+ ordererInfo: {
+ visitDate: 2,
+ orderMenuInfo: [
+ ['ํฐ๋ณธ์คํ
์ดํฌ', 1],
+ ['์ด์ฝ์ผ์ดํฌ', 2],
+ ['์ ๋ก์ฝ๋ผ', 1],
+ ],
+ totalOrderAmount: 88000,
+ },
+ expectedBenefitInfo: {
+ giftAmount: 0,
+ },
+ },
+ {
+ description: `142000์์ ${MINIMUM_ORDER_AMOUNT_FOR_GIFT}๋ง์ ์ด๊ณผ์ด๋ฏ๋ก ์ดํ์ธ์ด ์ฆ์ ๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase(),
+ },
+ expectedBenefitInfo: {
+ giftAmount: 25000,
+ },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+ // then
+ expect(benefitResult.giftAmount).toBe(expectedBenefitInfo.giftAmount);
+ });
+ });
+
+ describe('ํฌ๋ฆฌ์ค๋ง์ค ์ด๋ฒคํธ ์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: 'ํฌ๋ฆฌ์ค๋ง์ค๊ฐ ์ง๋ ์ดํ๋ ํ ์ธ์ด ์ ์ฉ๋์ง ์๋๋ค.',
+ ordererInfo: {
+ ...createOrdererInfoTestCase({
+ visitDate: 26,
+ }),
+ },
+ expectedBenefitInfo: {
+ xmasBenefitAmount: 0,
+ },
+ },
+ {
+ description: 'ํฌ๋ฆฌ์ค๋ง์ค ์ ์๋ ํ ์ธ์ด ์ ์ฉ ๋๋ค.',
+ ordererInfo: {
+ ...createOrdererInfoTestCase({
+ visitDate: 24,
+ }),
+ },
+ expectedBenefitInfo: {
+ xmasBenefitAmount: 3300,
+ },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+
+ // then
+ expect(benefitResult.xmasBenefitAmount).toBe(expectedBenefitInfo.xmasBenefitAmount);
+ });
+ });
+
+ describe('ํน๋ณ ํ ์ธ ์ด๋ฒคํธ ์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 3์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 3,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 10์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 10,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 17์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 17,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 24์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 24,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 25์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 25,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 31์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 31,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 30์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋์ง ์๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 30,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: 0 },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+
+ // then
+ expect(benefitResult.specialBenefitAmount).toBe(expectedBenefitInfo.specialBenefitAmount);
+ });
+ });
+}); | JavaScript | ์ ํ
์คํธ์ ๋ชฉ์ ์ 25์ผ์ด ์ด๋ฒคํธ ์ข
๋ฃ ์์ ์ด๋ผ ๊ทธ ๊ฒฝ๊ณ ๊ฐ๋ค์ ์ด๋ฒคํธ ์ ์ฉ ์ฌ๋ถ๋ฅผ ํ
์คํธํ๋๊ฒ ๊ฐ์ฅ ์ค์ํ๋ค๊ณ ์๊ฐํด์ ์ ๋ ๊ฒ ํ
์คํธ๋ฅผ ์งํํ์ต๋๋ค.
์์ฑ์๋์ ์๊ฒฌ๋ ์ ๋ ๋ง๋ค๊ณ ์๊ฐํด์. ๋ค๋ง, ์ ๋ ํ์ฌ ํ
์คํธ ์ฝ๋๊ฐ ๊ธด ์ํฉ์์ 1~25์ผ์ ๋ชจ๋ ํ
์คํธํ๋๊ฒ ๋น์ฉ์ด ๋๋ฌด ํฌ๋ค๊ณ ์๊ฐํด์ ๋ค์๊ณผ ๊ฐ์ด ํ
์คํธ๋ฅผ ์งํํ์ต๋๋ค! |
@@ -0,0 +1,277 @@
+import { PROMOTION_DATE_INFO } from '../../constants/promotionSystem';
+import {
+ BENEFIT_AMOUNT_INFO,
+ MINIMUM_ORDER_AMOUNT_FOR_GIFT,
+ INITIAL_PROMOTION_BENEFIT_RESULT,
+ MINIMUM_TOTAL_ORDER_AMOUNT,
+} from './constant';
+import DecemberPromotionPlan from './module';
+
+describe('12์ ์ด๋ฒคํธ ๊ณํ์ ๋ฐ๋ฅธ ํํ ๊ณ์ฐ ํ
์คํธ', () => {
+ const createBenefitResult = (ordererInfo) => DecemberPromotionPlan.execute(ordererInfo);
+
+ const createOrdererInfoTestCase = ({
+ visitDate = 3,
+ orderMenuInfo = [
+ ['ํฐ๋ณธ์คํ
์ดํฌ', 1],
+ ['๋ฐ๋นํ๋ฆฝ', 1],
+ ['์ด์ฝ์ผ์ดํฌ', 2],
+ ['์ ๋ก์ฝ๋ผ', 1],
+ ],
+ totalOrderAmount = 142000,
+ } = {}) => ({
+ visitDate,
+ orderMenuInfo,
+ totalOrderAmount,
+ });
+
+ describe('ํ๋ก๋ชจ์
์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: `5000์์ ${MINIMUM_TOTAL_ORDER_AMOUNT}์ ๋ฏธ๋ง ์ด๊ธฐ ๋๋ฌธ์ ํ๋ก๋ชจ์
์ ์ฉ์ด ๋์ง ์๋๋ค.`,
+ ordererInfo: {
+ visitDate: 3,
+ orderMenuInfo: ['์์ด์คํฌ๋ฆผ', 1],
+ totalOrderAmount: 5000,
+ },
+ expectedBenefitInfo: {
+ ...INITIAL_PROMOTION_BENEFIT_RESULT,
+ },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+
+ // then
+ expect(benefitResult).toStrictEqual(expectedBenefitInfo);
+ });
+ });
+
+ describe('ํ์ผ ํ ์ธ ์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 3์ผ์ ํ์ผ์ด๊ธฐ ๋๋ฌธ์ ํ ์ธ์ด ์ ์ฉ๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase(),
+ },
+ expectedBenefitInfo: {
+ weekDayBenefitAmount: 2 * BENEFIT_AMOUNT_INFO.dayOfWeek,
+ },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 2์ผ์ ์ฃผ๋ง์ด๊ธฐ ๋๋ฌธ์ ํ ์ธ์ด ์ ์ฉ๋์ง ์๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase({
+ visitDate: 2,
+ }),
+ },
+ expectedBenefitInfo: {
+ weekDayBenefitAmount: 0,
+ },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+
+ // then
+ expect(benefitResult.weekDayBenefitAmount).toBe(expectedBenefitInfo.weekDayBenefitAmount);
+ });
+ });
+
+ describe('์ฃผ๋ง ํ ์ธ ์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 2์ผ์ ์ฃผ๋ง์ด๊ธฐ ๋๋ฌธ์ ํ ์ธ์ด ์ ์ฉ๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase({
+ visitDate: 2,
+ }),
+ },
+ expectedBenefitInfo: {
+ weekendBenefitAmount: BENEFIT_AMOUNT_INFO.dayOfWeek * 2,
+ },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 3์ผ์ ํ์ผ์ด๊ธฐ ๋๋ฌธ์ ํ ์ธ์ด ์ ์ฉ๋์ง ์๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase(),
+ },
+ expectedBenefitInfo: {
+ weekendBenefitAmount: 0,
+ },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+
+ // then
+ expect(benefitResult.weekendBenefitAmount).toBe(expectedBenefitInfo.weekendBenefitAmount);
+ });
+ });
+
+ describe('12์ ์ด๋ฒคํธ ์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 1์ผ์ ${PROMOTION_DATE_INFO.month}์ ํ ์ธ์ด ์ ์ฉ๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase({ visitDate: 1 }),
+ },
+ expectedBenefitInfo: {
+ xmasBenefitAmount: BENEFIT_AMOUNT_INFO.christmas,
+ giftAmount: 25000,
+ specialBenefitAmount: 0,
+ weekDayBenefitAmount: 0,
+ weekendBenefitAmount: BENEFIT_AMOUNT_INFO.dayOfWeek * 2,
+ },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 31์ผ์ 12์ ํ ์ธ์ด ์ ์ฉ๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase({
+ visitDate: 31,
+ }),
+ },
+ expectedBenefitInfo: {
+ xmasBenefitAmount: 0,
+ weekDayBenefitAmount: BENEFIT_AMOUNT_INFO.dayOfWeek * 2,
+ weekendBenefitAmount: 0,
+ specialBenefitAmount: BENEFIT_AMOUNT_INFO.special,
+ giftAmount: 25000,
+ },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+
+ // then
+ expect(benefitResult).toStrictEqual(expectedBenefitInfo);
+ });
+ });
+
+ describe('์ฆ์ ์ด๋ฒคํธ ์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: `88000์์ ${MINIMUM_ORDER_AMOUNT_FOR_GIFT}๋ง์ ๋ฏธ๋ง์ด๋ฏ๋ก ์ดํ์ธ์ด ์ฆ์ ๋์ง ์๋๋ค.`,
+ ordererInfo: {
+ visitDate: 2,
+ orderMenuInfo: [
+ ['ํฐ๋ณธ์คํ
์ดํฌ', 1],
+ ['์ด์ฝ์ผ์ดํฌ', 2],
+ ['์ ๋ก์ฝ๋ผ', 1],
+ ],
+ totalOrderAmount: 88000,
+ },
+ expectedBenefitInfo: {
+ giftAmount: 0,
+ },
+ },
+ {
+ description: `142000์์ ${MINIMUM_ORDER_AMOUNT_FOR_GIFT}๋ง์ ์ด๊ณผ์ด๋ฏ๋ก ์ดํ์ธ์ด ์ฆ์ ๋๋ค.`,
+ ordererInfo: {
+ ...createOrdererInfoTestCase(),
+ },
+ expectedBenefitInfo: {
+ giftAmount: 25000,
+ },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+ // then
+ expect(benefitResult.giftAmount).toBe(expectedBenefitInfo.giftAmount);
+ });
+ });
+
+ describe('ํฌ๋ฆฌ์ค๋ง์ค ์ด๋ฒคํธ ์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: 'ํฌ๋ฆฌ์ค๋ง์ค๊ฐ ์ง๋ ์ดํ๋ ํ ์ธ์ด ์ ์ฉ๋์ง ์๋๋ค.',
+ ordererInfo: {
+ ...createOrdererInfoTestCase({
+ visitDate: 26,
+ }),
+ },
+ expectedBenefitInfo: {
+ xmasBenefitAmount: 0,
+ },
+ },
+ {
+ description: 'ํฌ๋ฆฌ์ค๋ง์ค ์ ์๋ ํ ์ธ์ด ์ ์ฉ ๋๋ค.',
+ ordererInfo: {
+ ...createOrdererInfoTestCase({
+ visitDate: 24,
+ }),
+ },
+ expectedBenefitInfo: {
+ xmasBenefitAmount: 3300,
+ },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+
+ // then
+ expect(benefitResult.xmasBenefitAmount).toBe(expectedBenefitInfo.xmasBenefitAmount);
+ });
+ });
+
+ describe('ํน๋ณ ํ ์ธ ์ด๋ฒคํธ ์ ์ฉ ์ฌ๋ถ ํ
์คํธ', () => {
+ test.each([
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 3์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 3,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 10์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 10,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 17์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 17,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 24์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 24,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 25์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 25,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 31์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 31,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special },
+ },
+ {
+ description: `${PROMOTION_DATE_INFO.month}์ 30์ผ์ ํน๋ณ ํ ์ธ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋์ง ์๋๋ค.`,
+ ordererInfo: createOrdererInfoTestCase({
+ visitDate: 30,
+ }),
+ expectedBenefitInfo: { specialBenefitAmount: 0 },
+ },
+ ])('$description', ({ ordererInfo, expectedBenefitInfo }) => {
+ // given - when
+ const benefitResult = createBenefitResult(ordererInfo);
+
+ // then
+ expect(benefitResult.specialBenefitAmount).toBe(expectedBenefitInfo.specialBenefitAmount);
+ });
+ });
+}); | JavaScript | ์ด๊ฑด ์์ฑ์๋์ด api ์คํ์ ์๋ชป ์ดํดํ๊ณ ๊ณ์ ๊ฑฐ ๊ฐ์์..! ์ ๋ ๊ฐ์ํ ํ ์ ์์์ผ๋ฉด ์ข์๊ฒ ์ง๋ง, ํ์ฌ ํ์ํ ์ ๋ณด๊ฐ visitDate, orderMenuInfo, totalOrderAmount ์ด 3๊ฐ์ง ์ ๋ณด๊ฐ ํ์ํด์ ์ด์ฉ์ ์์ด ์ ๋ ๊ฒ ์ฝ๋๋ฅผ ์ง ๊ฒ ์
๋๋ค..!
๋ํ ์ ๋ ์คํ๋ ค ๊ฐ์ฒด๋ก ๋ํ๋ด๋๊ฒ ์ฝ๊ฐ์ ๋ณต์ก๋๊ฐ ์ฌ๋ผ๊ฐ์ง์ธ์ ๋ ๋ช
์์ ์ผ๋ก ํ
์คํธ ์คํ์ ์ ๊ณตํด์ ๊ฐ๋
์ฑ์ด ๋ ๋๋ค๊ณ ์๊ฐํ๋ ์
์ฅ์ด๋ผ ์ฐธ๊ณ ํด์ฃผ์๋ฉด ์ข์๊ฑฐ ๊ฐ์์ !! |
@@ -0,0 +1,143 @@
+import MenuSearcher from '../MenuSearcher/module.js';
+import { PROMOTION_DATE_INFO } from '../../constants/promotionSystem.js';
+import {
+ DAY_OF_BENEFIT_CONDITION,
+ BENEFIT_DATE_INFO,
+ INITIAL_PROMOTION_BENEFIT_RESULT,
+ MINIMUM_TOTAL_ORDER_AMOUNT,
+ BENEFIT_AMOUNT_INFO,
+ MINIMUM_ORDER_AMOUNT_FOR_GIFT,
+ GIFT_INFO,
+ SPECIAL_DATES,
+} from './constant.js';
+
+/**
+ * @module DecemberPromotionPlan
+ * 12์์ ํ๋ก๋ชจ์
๊ณํ ๋ค์(ํฌ๋ฆฌ์ค๋ง์ค, ์ฃผ๋ง/ํ์ผ ํ ์ธ, ํน๋ณ ์ด๋ฒคํธ ๋ฐ ์ฆ์ ํ์ฌ)ํตํด ํ ์ธ ํํ ๋ค์ ์ ๊ณต ํ๋ ๋ชจ๋
+ */
+const DecemberPromotionPlan = Object.freeze({
+ /**
+ * @param {import('../../utils/jsDoc.js').OrdererInfo} ordererInfo - ์ฃผ๋ฌธ์ ์ ๋ณด(๋ฐฉ๋ฌธ ์ผ์, ์ด ์ฃผ๋ฌธ ๊ธ์ก, ์ฃผ๋ฌธํ ๋ฉ๋ด ์ ๋ณด)
+ * @returns {import('../../utils/jsDoc.js').PromotionBenefitResult} ๊ฐ ํํ ๊ธ์ก ์ ๋ณด
+ */
+ execute({ visitDate, totalOrderAmount, orderMenuInfo }) {
+ const { startDate, endDate } = BENEFIT_DATE_INFO;
+ const formatVisitDate = formatVisitDateForPromotion(visitDate);
+ const params = { dateInfo: { formatVisitDate, startDate, endDate }, totalOrderAmount };
+
+ return isAvailablePromotion(params)
+ ? createPromotionBenefitResult({
+ visitDate: formatVisitDate,
+ totalOrderAmount,
+ orderMenuInfo,
+ })
+ : INITIAL_PROMOTION_BENEFIT_RESULT;
+ },
+});
+
+export default DecemberPromotionPlan;
+
+/**
+ * @param {number} visitDate - ๋ฐฉ๋ฌธ ์ผ์
+ * @returns {Date} ๋ฐฉ๋ฌธ ์ผ์์ ๋ํ Date ๊ฐ์ฒด
+ */
+function formatVisitDateForPromotion(visitDate) {
+ const { year, month } = PROMOTION_DATE_INFO;
+
+ return visitDate < 10
+ ? new Date(`${year}-${month}-0${visitDate}`)
+ : new Date(`${year}-${month}-${visitDate}`);
+}
+
+/**
+ * @param {{dateInfo: {formatVisitDate: Date, startDate: Date, endDate: Date}, totalOrderAmount: number}} params - ํ๋ก๋ชจ์
์ด ์ ํจํ์ง ํ์ธํ ์ ๋ณด
+ * @returns {boolean} ์ด๋ฒคํธ ๊ธฐ๊ฐ ๋ฐ ์ต์ ์ฃผ๋ฌธ ๊ธ์ก ์๊ฑด์ ์ถฉ์กฑํ๋์ง ์ฌ๋ถ
+ */
+function isAvailablePromotion({
+ dateInfo: { formatVisitDate, startDate, endDate },
+ totalOrderAmount,
+}) {
+ return (
+ formatVisitDate >= startDate &&
+ formatVisitDate <= endDate &&
+ totalOrderAmount >= MINIMUM_TOTAL_ORDER_AMOUNT
+ );
+}
+
+/**
+ * @param {import('../../utils/jsDoc.js').OrdererInfo} ordererInfo - ์ฃผ๋ฌธ์ ์ ๋ณด(๋ฐฉ๋ฌธ ์ผ์, ์ด ์ฃผ๋ฌธ ๊ธ์ก, ์ฃผ๋ฌธํ ๋ฉ๋ด ์ ๋ณด)
+ * @returns {import('../../utils/jsDoc.js').PromotionBenefitResult} ๊ฐ ํํ ๊ธ์ก ์ ๋ณด
+ */
+function createPromotionBenefitResult(ordererInfo) {
+ const { weekday, weekend } = DAY_OF_BENEFIT_CONDITION;
+
+ return {
+ xmasBenefitAmount: calculateChristmasBenefit(ordererInfo),
+ weekDayBenefitAmount: calculateBenefitForDayType({ ordererInfo, ...weekday }),
+ weekendBenefitAmount: calculateBenefitForDayType({ ordererInfo, ...weekend }),
+ specialBenefitAmount: calculateSpecialBenefit(ordererInfo),
+ giftAmount: calculateGiftEvent(ordererInfo),
+ };
+}
+
+/**
+ * @param {import('../../utils/jsDoc.js').OrdererInfo} ordererInfo - ์ฃผ๋ฌธ์ ์ ๋ณด(๋ฐฉ๋ฌธ ์ผ์, ์ด ์ฃผ๋ฌธ ๊ธ์ก, ์ฃผ๋ฌธํ ๋ฉ๋ด ์ ๋ณด)
+ * @returns {number | 0} ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ์ด ์ ์ฉ๋๊ฑฐ๋ ์ ์ฉ๋์ง ์์ ๊ธ์ก
+ */
+function calculateChristmasBenefit({ visitDate }) {
+ const { startDate, christmas: endDate } = BENEFIT_DATE_INFO;
+
+ if (!(visitDate >= startDate && visitDate <= endDate)) return 0;
+
+ const millisecondPerDay = 1000 * 60 * 60 * 24;
+ const daysUntilChristmas = Math.floor((endDate - visitDate) / millisecondPerDay);
+
+ const { christmas, everyDay } = BENEFIT_AMOUNT_INFO;
+
+ const totalEventDay = 24;
+ return christmas + everyDay * (totalEventDay - daysUntilChristmas);
+}
+
+/**
+ *
+ * @param {import('../../utils/jsDoc.js').CalculateBenefitForDayTypeParams} params - ์ฃผ๋ฌธ์ ์ ๋ณด + ์์ผ ํ ์ธ ์กฐ๊ฑด์ด ๋ค์ด์๋ ๋งค๊ฐ ๋ณ์
+ * @returns {number | 0} ์์ผ(์ฃผ๋ง/ํ์ผ) ํ ์ธ์ด ์ ์ฉ๋๊ฑฐ๋ ์ ์ฉ ๋์ง ์์ ๊ธ์ก
+ */
+function calculateBenefitForDayType({
+ ordererInfo: { orderMenuInfo, visitDate },
+ menuCategory,
+ days,
+}) {
+ return !days.includes(visitDate.getDay())
+ ? 0
+ : orderMenuInfo
+ .filter(([menuName]) => MenuSearcher.isMenuInCategory(menuName, menuCategory))
+ .reduce(
+ (totalBenefit, [, quantity]) => totalBenefit + BENEFIT_AMOUNT_INFO.dayOfWeek * quantity,
+ 0,
+ );
+}
+
+/**
+ * @param {{visitDate : Date}} params - ๋ฐฉ๋ฌธ ์ผ์(Date ๊ฐ์ฒด)๊ฐ ํฌํจ๋ ๊ฐ์ฒด
+ * @returns {number | 0} ํน๋ณ ํ ์ธ์ด ์ ์ฉ๋๊ฑฐ๋ ์ ์ฉ๋์ง ์์ ๊ธ์ก
+ */
+function calculateSpecialBenefit({ visitDate }) {
+ const formattedVisitDate = visitDate.toISOString().substring(0, 10);
+ const specialDates = SPECIAL_DATES;
+
+ return specialDates.has(formattedVisitDate) ? BENEFIT_AMOUNT_INFO.special : 0;
+}
+
+/**
+ * @param {{totalOrderAmount : number}} params - ์ด ์ฃผ๋ฌธ ๊ธ์ก์ด ํฌํจ๋ ๊ฐ์ฒด
+ * @returns {number | 0} ์ฆ์ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๊ฑฐ๋ ์ ์ฉ๋์ง ์์ ๊ธ์ก
+ */
+function calculateGiftEvent({ totalOrderAmount }) {
+ if (totalOrderAmount < MINIMUM_ORDER_AMOUNT_FOR_GIFT) return 0;
+
+ const { menuCategory, menuName } = GIFT_INFO;
+ const champagne = MenuSearcher.findByMenuName(menuName, menuCategory);
+
+ return champagne.price;
+} | JavaScript | if๋ฌธ์ ์ฌ์ฉํ๋ฉด early return์ผ๋ก 2๋ฒ return๋ฌธ์ ์ฌ์ฉํด์ผํ์ง๋ง 3ํญ ์ฐ์ฐ์๋ ๊ฐ์ด๊ธฐ ๋๋ฌธ์ return๋ฌธ์ ๊ฐ์ํ ํ ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค! |
@@ -0,0 +1,136 @@
+import Event from "../src/domain/Event.js";
+
+describe('์ด๋ฒคํธ ๊ด๋ฆฌ์ Event ํด๋์ค ํ
์คํธ', () => {
+ test.each([['0'], ['-25'], ['1.1'], ['32']])('1๋ถํฐ 31 ์ฌ์ด์ ์์ฐ์๊ฐ ์๋์ ์์ธ์ฒ๋ฆฌ', (input) => {
+ expect(() => {
+ const event = new Event(input);
+ event.date()
+ }).toThrow('[ERROR]');
+ });
+
+ test.each([['a'], ['!'], [' '], ['3a'], ['๊ฐ'], ['1 '], ['1.0']])('์ซ์ ์ด์ธ์ ๊ณต๋ฐฑ์ ํฌํจํ ๋ฌธ์ ์์ธ์ฒ๋ฆฌ', (input) => {
+ expect(() => {
+ const event = new Event(input);
+ event.date();
+ }).toThrow('[ERROR]');
+ });
+
+ test('๋ ์ง๋ฅผ ์ซ์ํ์ผ๋ก ๋ถ๋ฌ์ค๋ ๊ธฐ๋ฅ ํ
์คํธ', () => {
+ const event = new Event('25');
+
+ expect(event.date()).toEqual(25)
+ });
+
+ test.each([['26'], ['27'], ['28'], ['29'], ['30'], ['31']])('ํฌ๋ฆฌ์ค๋ง์คD-Day ํ ์ธ๊ธ์ก์ 25์ผ ์ดํ 0์', (input) => {
+ const event = new Event(input);
+
+ expect(event.christmasDDayDiscount()).toEqual(0);
+ });
+
+ test('[ํฌ๋ฆฌ์ค๋ง์คDDAYํ ์ธ๋ฉ์๋] 25์ผ๊น์ง ์ ์ฉ๋๋ ํฌ๋ฆฌ์ค๋ง์คD-Day ํ ์ธ๊ธ์ก์ 1000 + (date-1)*100 ์', () => {
+ const DATE = '25';
+ const event = new Event(DATE);
+ const RESULT = 1000 + (25 - 1) * 100;
+
+ expect(event.christmasDDayDiscount()).toEqual(RESULT);
+ });
+
+ test.each([['1'],['2'],['22'],['23'],['29'],['30']])('[ํ์ผํ ์ธ๋ฉ์๋]ํ์ผ ํ ์ธ ๊ธ์ก์ ์ฃผ๋ง์ธ ๊ธ,ํ ์์ผ์ 0์', (input) => {
+ const event = new Event(input)
+ const DESERT_AMOUNT = 10;
+
+ expect(event.weekdayDiscount(DESERT_AMOUNT)).toEqual(0);
+ });
+
+ test('[ํ์ผํ ์ธ๋ฉ์๋]ํ์ผ์๋ ๋์ ํธ ๊ฐ์ * 2023์ ํ์ผํ ์ธ ๊ธ์ก ํ
์คํธ', () => {
+ const DESERT_AMOUNT = 3;
+ const event = new Event('25');
+ const RESULT = 2023 * 3;
+
+ expect(event.weekdayDiscount(DESERT_AMOUNT)).toEqual(RESULT);
+ });
+
+ test('[ํ์ผํ ์ธ๋ฉ์๋] ํ์ผ์ด๋ผ๋ ๋์ ํธ ์๋์ด 0 ์ด๋ฉด ํ์ผํ ์ธ ๊ธ์ก์ 0์', () => {
+ const DESERT_AMOUNT = 0;
+ const event = new Event('5');
+
+ expect(event.weekdayDiscount(DESERT_AMOUNT)).toEqual(0);
+ });
+
+ test.each([['4'], ['12'], ['25'], ['28']])('[์ฃผ๋งํ ์ธ๋ฉ์๋] ํ์ผ์๋ ์ฃผ๋ง ํ ์ธ ๊ธ์ก 0์', (input) => {
+ const MAIN_AMOUNT = 10;
+ const event = new Event(input);
+
+ expect(event.weekendDiscount(MAIN_AMOUNT)).toEqual(0);
+ });
+
+ test.each([['1'], ['2'], ['22']])('[์ฃผ๋งํ ์ธ๋ฉ์๋] ์ฃผ๋ง ์ผ์ง๋ผ๋ ๋ฉ์ธ์๋ฆฌ ์๋์ด 0์ผ์ ์ฃผ๋ง ํ ์ธ ๊ธ์ก์ 0์', (input) => {
+ const MAIN_AMOUNT = 0;
+ const event = new Event(input)
+
+ expect(event.weekendDiscount(MAIN_AMOUNT)).toEqual(0);
+ });
+
+ test('[์ฃผ๋งํ ์ธ๋ฉ์๋] ์ฃผ๋ง์๋ ๋ฉ์ธ์๋ฆฌ์๋ * 2023์ ํ ์ธ์ ์ฉ', () => {
+ const MAIN_AMOUNT = 3;
+ const RESULT = 3 * 2023;
+ const event = new Event('22');
+
+ expect(event.weekendDiscount(MAIN_AMOUNT)).toEqual(RESULT);
+ });
+
+ test.each([['3'], ['10'], ['17'], ['24'], ['31'], ['25']])('[์คํ์
ํ ์ธ๋ฉ์๋] ํน๋ณํ ๋ ์ง์ ๋ํด์ 1000์ ํ ์ธ์ ์ฉ ํ
์คํธ', (input) => {
+ const RESULT = 1000;
+ const event = new Event(input);
+
+ expect(event.specialDiscount()).toEqual(RESULT);
+ });
+
+ test.each([['1'], ['23'], ['11'], ['27']])('[์คํจ์
ํ ์ธ๋ฉ์๋] ํน๋ณํ ๋ ์ด ์๋ ๋ ์ ๋ํด์ ํ ์ธ๊ธ์ก 0์', (input) => {
+ const RESULT = 0;
+ const event = new Event(input);
+
+ expect(event.specialDiscount()).toEqual(RESULT);
+ });
+
+ test('[์ฌ์ํ๊ธ์ก๋ฉ์๋]์ด ์ฃผ๋ฌธ์ก์ด 12๋ง์ ์ด์์ผ์ 2๋ง5์ฒ์ ํ ์ธ์ ์ฉ ๋ณด์ฌ์ฃผ๊ธฐ', () => {
+ const TOTAL_FOODS_PRICE_BEFORE_DISCOUNT = 120000;
+ const RESULT = 25000;
+ const event = new Event('1');
+
+ expect(event.freebieDiscount(TOTAL_FOODS_PRICE_BEFORE_DISCOUNT)).toEqual(RESULT);
+ });
+
+ test('[์ฌ์ํ๊ธ์ก๋ฉ์๋]์ด ์ฃผ๋ฌธ์ก์ด 12๋ง์ ๋ฏธ๋ง์ด๋ฉด 0์ ์ฒ๋ฆฌ', () => {
+ const TOTAL_FOODS_PRICE_BEFORE_DISCOUNT = 119999;
+ const RESULT = 0;
+ const event = new Event('25');
+
+ expect(event.freebieDiscount(TOTAL_FOODS_PRICE_BEFORE_DISCOUNT)).toEqual(RESULT);
+ });
+
+ test('[์ด๋ฒคํธ์ฐธ์ฌ๊ฐ๋ฅ์ฌ๋ถ๋ฉ์๋] ์ด ์ฃผ๋ฌธ์ก ๋ง์ ์ด์๋ถํฐ true ๋ฐํ', () => {
+ const TOTAL_FOODS_PRICE_BEFORE_DISCOUNT = 10002;
+ const RESULT = true;
+ const event = new Event('24');
+
+ expect(event.condition(TOTAL_FOODS_PRICE_BEFORE_DISCOUNT)).toEqual(RESULT);
+ });
+
+ test('[์ด๋ฒคํธ์ฐธ์ฌ๊ฐ๋ฅ์ฌ๋ถ๋ฉ์๋] ์ด ์ฃผ๋ฌธ์ก ๋ง์ ๋ฏธ๋ง๋ถํฐ false ๋ฐํ', () => {
+ const TOTAL_FOODS_PRICE_BEFORE_DISCOUNT = 9999;
+ const RESULT = false;
+ const event = new Event('31');
+
+ expect(event.condition(TOTAL_FOODS_PRICE_BEFORE_DISCOUNT)).toEqual(RESULT);
+ });
+
+ let badgeIndex = 0;
+ test.each([[4999], [5000], [12000], [21000]])('[๋ฒณ์ง์์ฌ๋ฉ์๋] ํดํ๋ฐ์ ๊ธ์ก์ ๋ฐ๋ผ ๋ฑ์ง ์์ฌ', (totalBenefitAmount) => {
+ const BADGE_RESULT = ['์์', '๋ณ', 'ํธ๋ฆฌ', '์ฐํ'];
+ const event = new Event('22');
+
+ expect(event.getBadge(totalBenefitAmount)).toEqual(BADGE_RESULT[badgeIndex]);
+ badgeIndex += 1;
+ });
+}); | JavaScript | ๊ฐ ํ
์คํธ์ ๋ํด ๋จ์ผ ๊ฐ์ ์ฃผ๋ ๊ฒฝ์ฐ์ ๋ํด์๋ []๋ฅผ ํฌํจํ์ง ์์๋ ๋๋ ๊ฑธ๋ก ์๊ณ ์์ด์!! ํ ๋ฒ ํ์ธํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,110 @@
+import {
+ priceFilter,
+ PRICE_REFINER,
+ merageFoodInfo,
+ BENEFITS_REFINER,
+ isNotApplicableBenefits
+} from "../src/constants/ViewRefiner.js";
+
+test('์ซ์๋ฅผ ์ฒ๋จ์๋ก ์๋ผ์ ๋ฌธ์๋ก ๋ณํ๋๋์ง ํ
์คํธ', () => {
+ const NUMBER = 1234567890;
+ const PRICE_STRING = '1,234,567,890';
+
+ expect(priceFilter(NUMBER)).toEqual(PRICE_STRING);
+});
+
+test('์ซ์๊ฐ 0์ผ๊ฒฝ์ฐ ๋ฌธ์๋ก 0์์ ๋ฐํํ๋ค', () => {
+ const PRICE = 0;
+ const RESULT = '0์';
+
+ expect(PRICE_REFINER.negative(PRICE)).toEqual(RESULT);
+});
+
+test('์ซ์๊ฐ 0์ด ์๋๋ผ๋ฉด ์ฒ๋จ์๋ก ์๋ผ ๋ง์ด๋์ค์ ์์ ๋ถ์ฌ ๋ฌธ์๋ก ๋ฐํํ๋ค', () => {
+ const PRICE = 1500000;
+ const RESULT = '-1,500,000์';
+
+ expect(PRICE_REFINER.negative(PRICE)).toEqual(RESULT);
+});
+
+test('์์ ์ด๋ฆ๊ณผ ์๋์ ์ ์ง์ง์ด ์ฃผ๋์ง ํ
์คํธ', () => {
+ const FOOD_NAME = '์๊ณจ๋์ฅ์ฐ๊ฐ';
+ const FOOD_AMOUNT = '7';
+ const FOOD_NAME_WITH_AMOUNT = '์๊ณจ๋์ฅ์ฐ๊ฐ 7๊ฐ';
+
+ expect(merageFoodInfo(FOOD_NAME, FOOD_AMOUNT)).toEqual(FOOD_NAME_WITH_AMOUNT);
+});
+
+test('ํํ ๋ด์ญ์ด ํ๋๋ ์์๋(ํค condition์ value๊ฐ false) ์์์ด๋ผ๋ ๋ฌธ์๋ง ๋ด๊ธด ๋ฐฐ์ด์ ๋ฐํํ๋ค', () => {
+ const map = new Map();
+ map
+ .set('christmas', 1000)
+ .set('weekday', 2023 * 1)
+ .set('weekend', 0)
+ .set('special', 0)
+ .set('freebie', 0)
+ .set('condition', false);
+
+ const BENEFITS_LIST = ['์์'];
+
+ expect(BENEFITS_REFINER.benfitList(map)).toEqual(BENEFITS_LIST);
+});
+
+test('ํค condition์ value๊ฐ true๋ผ๋ฉด map์ ๋ด๊ธด ์ ๋ณด๋ฅผ ์ต์ข
์ถ๋ ฅํ์์ ๋ง์ถฐ ๋ฌธ์ ๋ฐฐ์ด๋ก ๋ฐํํ๋ค', () => {
+ const map = new Map();
+ map
+ .set('christmas', 1000 + (24 -1) * 100)
+ .set('weekday', 2023 * 10)
+ .set('weekend', 0)
+ .set('special', 1000)
+ .set('freebie', 25000)
+ .set('condition', true);
+
+ const BENEFITS_ARRAY = [
+ 'ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: -3,300์',
+ 'ํ์ผ ํ ์ธ: -20,230์',
+ 'ํน๋ณ ํ ์ธ: -1,000์',
+ '์ฆ์ ์ด๋ฒคํธ: -25,000์'
+ ];
+
+ expect(BENEFITS_REFINER.benfitList(map)).toEqual(BENEFITS_ARRAY);
+});
+
+test('๋ชจ๋ ํํ ํ ์ธ๊ธ์ก์ด 0์์ด๋ฉด true๋ฅผ ๋ฐํํ๋ค', () => {
+ const map = new Map();
+ map
+ .set('christmas', 0)
+ .set('weekday', 0)
+ .set('weekend', 0)
+ .set('special', 0)
+ .set('freebie', 0)
+ .set('condition', true);
+
+ expect(isNotApplicableBenefits(map)).toEqual(true);
+});
+
+test('map ์์ conditon ์ value ๊ฐ false๋ผ๋ฉด true๋ฅผ ๋ฐํํ๋ค', () => {
+ const map = new Map();
+ map
+ .set('christmas', 2000)
+ .set('weekday', 2023)
+ .set('weekend', 0)
+ .set('special', 1000)
+ .set('freebie', 25000)
+ .set('condition', false);
+
+ expect(isNotApplicableBenefits(map)).toEqual(true);
+});
+
+test('ํํ ๊ธ์ก์ด ๋ชจ๋ 0์์ด ์๋๋ฉด์ conditon์ value๊ฐ true๋ผ๋ฉด false๋ฅผ ๋ฐํํ๋ค', () => {
+ const map = new Map();
+ map
+ .set('christmas', 0)
+ .set('weekday', 0)
+ .set('weekend', 0)
+ .set('special', 0)
+ .set('freebie', 25000)
+ .set('condition', true);
+
+ expect(isNotApplicableBenefits(map)).toEqual(false);
+}); | JavaScript | ๋๋ฉ์ธ ๋ก์ง์ด๋ ํ์ค ๋ฌธ์ ์ ๋ํ ์์ฌ๊ฒฐ์ ์ ํ๋ ์ฝ๋๋ก ์๊ณ ์๋๋ฐ, ํน์ ํด๋น ํ
์คํธ๊ฐ ๋๋ฉ์ธ ๋ก์ง์ ๋ํ ํ
์คํธ์ธ์ง ์ฌ์ญ๊ณ ์ถ์ต๋๋น! |
@@ -1,7 +1,67 @@
-export default OutputView = {
- printMenu() {
- Console.print("<์ฃผ๋ฌธ ๋ฉ๋ด>");
- // ...
- }
- // ...
-}
+import { Console } from "@woowacourse/mission-utils";
+import { ANSWER } from "./constants/PlannerMesseage.js";
+import { PRICE_REFINER, BENEFITS_REFINER } from "./constants/ViewRefiner.js";
+
+const OutputView = {
+ printIntroduce() {
+ Console.print(ANSWER.introduce);
+ },
+
+ printProlog(date) {
+ Console.print(`${ANSWER.prolog(date)}\n`);
+ },
+
+ printOrderMenuResult(foodsArray) {
+ Console.print(ANSWER.orderMenu);
+
+ foodsArray.forEach((food) => Console.print(food));
+ Console.print('');
+ },
+
+ printTotalOrderAmountResult(amount) {
+ Console.print(ANSWER.totalOrderAmount);
+
+ Console.print(PRICE_REFINER.positive(amount));
+ Console.print('');
+ },
+
+ printFreebieMenuResult(freebieMenu) {
+ Console.print(ANSWER.freebieMenu);
+
+ Console.print(freebieMenu);
+ Console.print('');
+ },
+
+ printBenefitListResult(benefitArray) {
+ Console.print(ANSWER.benefitList);
+
+ BENEFITS_REFINER.benfitList(benefitArray).forEach((benefit) => Console.print(benefit));
+ Console.print('');
+ },
+
+ printTotalBenefitAmountResult(totalBenefitAmount) {
+ Console.print(ANSWER.totalBenefitAmount);
+
+ Console.print(PRICE_REFINER.negative(totalBenefitAmount));
+ Console.print('');
+ },
+
+ printFinalPaymentAmountResult(finalPaymentAmount) {
+ Console.print(ANSWER.finalPaymentAmount);
+
+ Console.print(PRICE_REFINER.positive(finalPaymentAmount));
+ Console.print('');
+ },
+
+ printBadgeResult(badge) {
+ Console.print(ANSWER.DecemberEventBadge);
+
+ Console.print(badge);
+ },
+
+ printErrorMesseage(messeage) {
+ Console.print(`${messeage}`);
+ }
+};
+
+export default OutputView; | JavaScript | **OutputView**๊ฐ ์ ๋ง ๊น๋ํ๊ฒ ์ ์ ๋ฆฌ๋์ด ์๋ ๊ฒ ๊ฐ์ต๋๋ค ! ๐๐ |
@@ -0,0 +1,81 @@
+import Supervisor from "../domain/Supervisor.js";
+import InputView from "../InputView.js";
+import OutputView from "../OutputView.js";
+
+class EventPlanner {
+ #supervisor;
+
+ constructor() {
+ this.#supervisor = new Supervisor();
+ }
+
+ async start() {
+ OutputView.printIntroduce();
+
+ await this.getDate();
+ await this.getMenu();
+
+ this.plannerResult();
+ }
+
+ async getDate() {
+ try {
+ this.#supervisor.dateUpload(await InputView.readDate());
+
+ } catch (error) {
+ OutputView.printErrorMesseage(error);
+ await this.getDate();
+ }
+ }
+
+ async getMenu() {
+ try {
+ this.#supervisor.menuUpload(await InputView.readMenu());
+
+ } catch (error) {
+ OutputView.printErrorMesseage(error);
+ await this.getMenu();
+ }
+ }
+
+ plannerResult() {
+ OutputView.printProlog(this.#supervisor.event().date());
+ this.orderMenu();
+ this.totalOrderAmount();
+ this.freebieMenu();
+ this.benefitList();
+ this.totalBenefitAmount();
+ this.finalPaymentAmount();
+ this.badge();
+ }
+
+ orderMenu() {
+ OutputView.printOrderMenuResult(this.#supervisor.cashier().foodsListWithQuantity());
+ }
+
+ totalOrderAmount() {
+ OutputView.printTotalOrderAmountResult(this.#supervisor.cashier().totalFoodsPrice());
+ }
+
+ freebieMenu() {
+ OutputView.printFreebieMenuResult(this.#supervisor.cashier().freebieMenu());
+ }
+
+ benefitList() {
+ OutputView.printBenefitListResult(this.#supervisor.benefitList());
+ }
+
+ totalBenefitAmount() {
+ OutputView.printTotalBenefitAmountResult(this.#supervisor.totalBenefitAmount());
+ }
+
+ finalPaymentAmount() {
+ OutputView.printFinalPaymentAmountResult(this.#supervisor.finalPaymentAmount());
+ }
+
+ badge() {
+ OutputView.printBadgeResult(this.#supervisor.giveBadge());
+ }
+}
+
+export default EventPlanner; | JavaScript | `getDate`์ `getMenu` ๋ฉ์๋์์ ์ฌ์
๋ ฅ ์์ฒญ์ ๋ํ `try catch` ๊ตฌ๋ฌธ์ด ์ค๋ณต๋์ด์ ์ฌ์ฉ๋๋ ๊ฒ ๊ฐ์์! ์ฌ์
๋ ฅ ์์ฒญ์ ๋ํ `try catch` ๊ตฌ๋ฌธ์ ํจ์๋ก ๋ถ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ํด ๊ณ ๋ คํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,84 @@
+import { dateValidator } from "../util/Validator.js";
+import {
+ DISCOUNT,
+ BADGE,
+ BENEFIT_MESSEAGE
+} from "../constants/BenefitStorage.js";
+
+class Event {
+ #date;
+
+ constructor(date) {
+ dateValidator(date);
+ this.#date = Number(date);
+ }
+
+ #week() {
+ const WEEK = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];
+
+ return WEEK[new Date(2023, 11, this.#date).getDay()];
+ }
+
+ date() {
+ return this.#date;
+ }
+
+ christmasDDayDiscount() {
+ if (this.#date <= DISCOUNT.christmasDate) {
+ return DISCOUNT.christmasBasic + (this.#date - 1) * DISCOUNT.christmasAccumulation;
+ }
+ return DISCOUNT.zero;
+ }
+
+ weekdayDiscount(desertQuantity) {
+ if (this.#week() !== 'FR' && this.#week() !== 'SA') {
+ return desertQuantity * DISCOUNT.weekday;
+ }
+ return DISCOUNT.zero;
+ }
+
+ weekendDiscount(mainQuantity) {
+ if (this.#week() === 'FR' || this.#week() === 'SA') {
+ return mainQuantity * DISCOUNT.weekend;
+ }
+ return DISCOUNT.zero;
+ }
+
+ specialDiscount() {
+ if (this.#week() === 'SU' || this.#date === DISCOUNT.christmasDate) {
+ return DISCOUNT.special;
+ }
+ return DISCOUNT.zero;
+ }
+
+ freebieDiscount(totalFoodsPriceBeforeDiscount) {
+ if (totalFoodsPriceBeforeDiscount >= DISCOUNT.freebieStandard) {
+ return DISCOUNT.freebiePrice;
+ }
+ return DISCOUNT.zero;
+ }
+
+ condition(totalFoodsPriceBeforeDiscount) {
+ if (totalFoodsPriceBeforeDiscount >= DISCOUNT.eventStandardAmount) {
+ return true;
+ }
+ return false;
+ }
+
+ getBadge(totalBenefitAmount) {
+ if (totalBenefitAmount < BADGE.star) {
+ return BENEFIT_MESSEAGE.non;
+ }
+ if (totalBenefitAmount >= BADGE.santa) {
+ return BADGE.santaName;
+ }
+ if (totalBenefitAmount >= BADGE.tree) {
+ return BADGE.treeName;
+ }
+ if (totalBenefitAmount >= BADGE.star) {
+ return BADGE.starName;
+ }
+ }
+}
+
+export default Event; | JavaScript | ์์ผ์ ๋ํ ๊ฐ๋ค์ ์์๋ก ์ ์ธํ๊ณ , ์์๋ฅผ ํ์ฉํ๋ฉด ๊ฐ๋
์ฑ ํฅ์์ ๋์๋ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,49 @@
+import { foodValidator } from "../util/Validator.js";
+import {
+ MENU,
+ NAME,
+ KNIFE
+} from "../constants/FoodStorage.js";
+
+class Food {
+ #name;
+
+ #quantity;
+
+ constructor(food) {
+ foodValidator(this.#enrollFood(food));
+ }
+
+ #enrollFood(food) {
+ const foodInfoArray = food.split(KNIFE.bladePosition);
+ this.#name = foodInfoArray[KNIFE.namePosition];
+ this.#quantity = Number(foodInfoArray[KNIFE.quantityPosition]);
+
+ return foodInfoArray;
+ }
+
+ #category() {
+ const [foodTypeInfo] = MENU
+ .filter((foodType) => Object.hasOwn(foodType, this.#name));
+
+ return foodTypeInfo;
+ }
+
+ type() {
+ return this.#category()[NAME.type];
+ }
+
+ name() {
+ return this.#name;
+ }
+
+ quantity() {
+ return this.#quantity;
+ }
+
+ totalPrice() {
+ return this.#category()[this.#name] * this.quantity();
+ }
+}
+
+export default Food; | JavaScript | ํ์
, ์ด๋ฆ, ํํฐํฐ ๋ฑ๋ฑ ๋ฐํํด์ฃผ๋ ํจ์๋ช
์ ๋ํด `get`์ ๋ถ์ฌ์ฃผ๋๊ฒ ๊ฐ๋
์ฑ ํฅ์์ ๋ ๋์์ด ๋์ง ์์๊น์?? |
@@ -0,0 +1,46 @@
+import { ERROR_DATE, ERROR_MENU } from "../constants/ErrorMesseage.js";
+import { MENU, KNIFE } from "../constants/FoodStorage.js";
+
+export const dateValidator = (date) => {
+ if (
+ date.includes(' ') ||
+ date.includes('.') ||
+ Number.isNaN(Number(date)) ||
+ !Number.isInteger(Number(date)) ||
+ Number(date) < ERROR_DATE.minDate ||
+ Number(date) > ERROR_DATE.maxDate
+ ) {
+ throw new Error(ERROR_DATE.basic);
+ }
+};
+
+export const nonFoodInMenu = (foodName) => {
+ const foodType = MENU
+ .filter((type) => Object.hasOwn(type, foodName));
+
+ if (foodType.length === 0) {
+ return true;
+ }
+ return false;
+};
+
+export const nonFoodQuantity = (foodQuantity) => {
+ if (foodQuantity < ERROR_MENU.minOrderNumber) {
+ return true;
+ }
+ return false;
+};
+
+export const foodValidator = (foodInfo) => {
+ if (
+ foodInfo.length !== 2 ||
+ foodInfo[KNIFE.quantityPosition].includes('.') ||
+ foodInfo[KNIFE.quantityPosition].includes(' ') ||
+ Number.isNaN(Number(foodInfo[KNIFE.quantityPosition])) ||
+ !Number.isInteger(Number(foodInfo[KNIFE.quantityPosition])) ||
+ nonFoodInMenu(foodInfo[KNIFE.namePosition]) ||
+ nonFoodQuantity(Number(foodInfo[KNIFE.quantityPosition]))
+ ) {
+ throw new Error(ERROR_MENU.basic);
+ }
+}; | JavaScript | `if` ๋ฌธ ๋ด๋ถ์์ ๊ฒ์ฆํ๋ ๊ฒ๋ค์ด ๋๋ฌด ๋ง์์ ์คํ๋ ค ๊ฐ๋
์ฑ์ด ๋จ์ด์ ธ ๋ณด์ผ ์๋ ์์ ๊ฒ ๊ฐ์์,,!
์ด ๋ถ๋ถ์ ์ ๊ทํํ์์ ํ์ฉํ๋ค๋ฉด ๊ฐ๋
์ฑ ํฅ์์ ๋์์ด ๋ ์ ์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,110 @@
+import {
+ priceFilter,
+ PRICE_REFINER,
+ merageFoodInfo,
+ BENEFITS_REFINER,
+ isNotApplicableBenefits
+} from "../src/constants/ViewRefiner.js";
+
+test('์ซ์๋ฅผ ์ฒ๋จ์๋ก ์๋ผ์ ๋ฌธ์๋ก ๋ณํ๋๋์ง ํ
์คํธ', () => {
+ const NUMBER = 1234567890;
+ const PRICE_STRING = '1,234,567,890';
+
+ expect(priceFilter(NUMBER)).toEqual(PRICE_STRING);
+});
+
+test('์ซ์๊ฐ 0์ผ๊ฒฝ์ฐ ๋ฌธ์๋ก 0์์ ๋ฐํํ๋ค', () => {
+ const PRICE = 0;
+ const RESULT = '0์';
+
+ expect(PRICE_REFINER.negative(PRICE)).toEqual(RESULT);
+});
+
+test('์ซ์๊ฐ 0์ด ์๋๋ผ๋ฉด ์ฒ๋จ์๋ก ์๋ผ ๋ง์ด๋์ค์ ์์ ๋ถ์ฌ ๋ฌธ์๋ก ๋ฐํํ๋ค', () => {
+ const PRICE = 1500000;
+ const RESULT = '-1,500,000์';
+
+ expect(PRICE_REFINER.negative(PRICE)).toEqual(RESULT);
+});
+
+test('์์ ์ด๋ฆ๊ณผ ์๋์ ์ ์ง์ง์ด ์ฃผ๋์ง ํ
์คํธ', () => {
+ const FOOD_NAME = '์๊ณจ๋์ฅ์ฐ๊ฐ';
+ const FOOD_AMOUNT = '7';
+ const FOOD_NAME_WITH_AMOUNT = '์๊ณจ๋์ฅ์ฐ๊ฐ 7๊ฐ';
+
+ expect(merageFoodInfo(FOOD_NAME, FOOD_AMOUNT)).toEqual(FOOD_NAME_WITH_AMOUNT);
+});
+
+test('ํํ ๋ด์ญ์ด ํ๋๋ ์์๋(ํค condition์ value๊ฐ false) ์์์ด๋ผ๋ ๋ฌธ์๋ง ๋ด๊ธด ๋ฐฐ์ด์ ๋ฐํํ๋ค', () => {
+ const map = new Map();
+ map
+ .set('christmas', 1000)
+ .set('weekday', 2023 * 1)
+ .set('weekend', 0)
+ .set('special', 0)
+ .set('freebie', 0)
+ .set('condition', false);
+
+ const BENEFITS_LIST = ['์์'];
+
+ expect(BENEFITS_REFINER.benfitList(map)).toEqual(BENEFITS_LIST);
+});
+
+test('ํค condition์ value๊ฐ true๋ผ๋ฉด map์ ๋ด๊ธด ์ ๋ณด๋ฅผ ์ต์ข
์ถ๋ ฅํ์์ ๋ง์ถฐ ๋ฌธ์ ๋ฐฐ์ด๋ก ๋ฐํํ๋ค', () => {
+ const map = new Map();
+ map
+ .set('christmas', 1000 + (24 -1) * 100)
+ .set('weekday', 2023 * 10)
+ .set('weekend', 0)
+ .set('special', 1000)
+ .set('freebie', 25000)
+ .set('condition', true);
+
+ const BENEFITS_ARRAY = [
+ 'ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: -3,300์',
+ 'ํ์ผ ํ ์ธ: -20,230์',
+ 'ํน๋ณ ํ ์ธ: -1,000์',
+ '์ฆ์ ์ด๋ฒคํธ: -25,000์'
+ ];
+
+ expect(BENEFITS_REFINER.benfitList(map)).toEqual(BENEFITS_ARRAY);
+});
+
+test('๋ชจ๋ ํํ ํ ์ธ๊ธ์ก์ด 0์์ด๋ฉด true๋ฅผ ๋ฐํํ๋ค', () => {
+ const map = new Map();
+ map
+ .set('christmas', 0)
+ .set('weekday', 0)
+ .set('weekend', 0)
+ .set('special', 0)
+ .set('freebie', 0)
+ .set('condition', true);
+
+ expect(isNotApplicableBenefits(map)).toEqual(true);
+});
+
+test('map ์์ conditon ์ value ๊ฐ false๋ผ๋ฉด true๋ฅผ ๋ฐํํ๋ค', () => {
+ const map = new Map();
+ map
+ .set('christmas', 2000)
+ .set('weekday', 2023)
+ .set('weekend', 0)
+ .set('special', 1000)
+ .set('freebie', 25000)
+ .set('condition', false);
+
+ expect(isNotApplicableBenefits(map)).toEqual(true);
+});
+
+test('ํํ ๊ธ์ก์ด ๋ชจ๋ 0์์ด ์๋๋ฉด์ conditon์ value๊ฐ true๋ผ๋ฉด false๋ฅผ ๋ฐํํ๋ค', () => {
+ const map = new Map();
+ map
+ .set('christmas', 0)
+ .set('weekday', 0)
+ .set('weekend', 0)
+ .set('special', 0)
+ .set('freebie', 25000)
+ .set('condition', true);
+
+ expect(isNotApplicableBenefits(map)).toEqual(false);
+}); | JavaScript | ํํ ๋ด์ญ์ map์์ ๊ด๋ฆฌ ํ๋ค๋ณด๋ ์ถ๋ ฅ ํํ๋ก ๋ณํํ๋ ๋ก์ง์ด ๋ค์ ๋ณต์กํด์ก์ต๋๋ค. ํด๋น ๋ก์ง์ ๊ทธ ์ญํ ์ ํด์ฃผ๋๋ฐ ์๊ฐ๋ณด๋ค ๋ณต์กํ๊ณ ์ค์๋ฅผ ๋ง์ด ํ ๋ถ๋ถ์ด๋ผ ํ
์คํธ๋ฅผ ์งํํ์ต๋๋ค! |
@@ -0,0 +1,81 @@
+import Supervisor from "../domain/Supervisor.js";
+import InputView from "../InputView.js";
+import OutputView from "../OutputView.js";
+
+class EventPlanner {
+ #supervisor;
+
+ constructor() {
+ this.#supervisor = new Supervisor();
+ }
+
+ async start() {
+ OutputView.printIntroduce();
+
+ await this.getDate();
+ await this.getMenu();
+
+ this.plannerResult();
+ }
+
+ async getDate() {
+ try {
+ this.#supervisor.dateUpload(await InputView.readDate());
+
+ } catch (error) {
+ OutputView.printErrorMesseage(error);
+ await this.getDate();
+ }
+ }
+
+ async getMenu() {
+ try {
+ this.#supervisor.menuUpload(await InputView.readMenu());
+
+ } catch (error) {
+ OutputView.printErrorMesseage(error);
+ await this.getMenu();
+ }
+ }
+
+ plannerResult() {
+ OutputView.printProlog(this.#supervisor.event().date());
+ this.orderMenu();
+ this.totalOrderAmount();
+ this.freebieMenu();
+ this.benefitList();
+ this.totalBenefitAmount();
+ this.finalPaymentAmount();
+ this.badge();
+ }
+
+ orderMenu() {
+ OutputView.printOrderMenuResult(this.#supervisor.cashier().foodsListWithQuantity());
+ }
+
+ totalOrderAmount() {
+ OutputView.printTotalOrderAmountResult(this.#supervisor.cashier().totalFoodsPrice());
+ }
+
+ freebieMenu() {
+ OutputView.printFreebieMenuResult(this.#supervisor.cashier().freebieMenu());
+ }
+
+ benefitList() {
+ OutputView.printBenefitListResult(this.#supervisor.benefitList());
+ }
+
+ totalBenefitAmount() {
+ OutputView.printTotalBenefitAmountResult(this.#supervisor.totalBenefitAmount());
+ }
+
+ finalPaymentAmount() {
+ OutputView.printFinalPaymentAmountResult(this.#supervisor.finalPaymentAmount());
+ }
+
+ badge() {
+ OutputView.printBadgeResult(this.#supervisor.giveBadge());
+ }
+}
+
+export default EventPlanner; | JavaScript | ๋ฆฌํฉํ ๋ง๋ ์ ์ฉํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค! ๐ |
@@ -0,0 +1,84 @@
+import { dateValidator } from "../util/Validator.js";
+import {
+ DISCOUNT,
+ BADGE,
+ BENEFIT_MESSEAGE
+} from "../constants/BenefitStorage.js";
+
+class Event {
+ #date;
+
+ constructor(date) {
+ dateValidator(date);
+ this.#date = Number(date);
+ }
+
+ #week() {
+ const WEEK = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];
+
+ return WEEK[new Date(2023, 11, this.#date).getDay()];
+ }
+
+ date() {
+ return this.#date;
+ }
+
+ christmasDDayDiscount() {
+ if (this.#date <= DISCOUNT.christmasDate) {
+ return DISCOUNT.christmasBasic + (this.#date - 1) * DISCOUNT.christmasAccumulation;
+ }
+ return DISCOUNT.zero;
+ }
+
+ weekdayDiscount(desertQuantity) {
+ if (this.#week() !== 'FR' && this.#week() !== 'SA') {
+ return desertQuantity * DISCOUNT.weekday;
+ }
+ return DISCOUNT.zero;
+ }
+
+ weekendDiscount(mainQuantity) {
+ if (this.#week() === 'FR' || this.#week() === 'SA') {
+ return mainQuantity * DISCOUNT.weekend;
+ }
+ return DISCOUNT.zero;
+ }
+
+ specialDiscount() {
+ if (this.#week() === 'SU' || this.#date === DISCOUNT.christmasDate) {
+ return DISCOUNT.special;
+ }
+ return DISCOUNT.zero;
+ }
+
+ freebieDiscount(totalFoodsPriceBeforeDiscount) {
+ if (totalFoodsPriceBeforeDiscount >= DISCOUNT.freebieStandard) {
+ return DISCOUNT.freebiePrice;
+ }
+ return DISCOUNT.zero;
+ }
+
+ condition(totalFoodsPriceBeforeDiscount) {
+ if (totalFoodsPriceBeforeDiscount >= DISCOUNT.eventStandardAmount) {
+ return true;
+ }
+ return false;
+ }
+
+ getBadge(totalBenefitAmount) {
+ if (totalBenefitAmount < BADGE.star) {
+ return BENEFIT_MESSEAGE.non;
+ }
+ if (totalBenefitAmount >= BADGE.santa) {
+ return BADGE.santaName;
+ }
+ if (totalBenefitAmount >= BADGE.tree) {
+ return BADGE.treeName;
+ }
+ if (totalBenefitAmount >= BADGE.star) {
+ return BADGE.starName;
+ }
+ }
+}
+
+export default Event; | JavaScript | ์์ ์ฒ๋ฆฌ๋ ํ์์ง๋ง ์ง๊ธ ๋ค์ ๋ด๋ ํด๋์ค ๋ด์์ ์ ์ธํ ๋ถ๋ถ์ ๋ง์๋๋ก ๋ง์ด ์์ฌ์ด ๊ฒ ๊ฐ์ต๋๋คใ
์์ผ์ ์ด๋ฆ์ ํ๋ค์์ ํ ์ง๋ ๊ณ ๋ฏผ์ด์๋๋ฐ ๋ ๊ธ์๋ก๋ ์ถฉ๋ถํ ๊ฒ ๊ฐ์ ์ฝ์ด๋ฅผ ์ฌ์ฉํ์์ต๋๋ค. |
@@ -0,0 +1,49 @@
+import { foodValidator } from "../util/Validator.js";
+import {
+ MENU,
+ NAME,
+ KNIFE
+} from "../constants/FoodStorage.js";
+
+class Food {
+ #name;
+
+ #quantity;
+
+ constructor(food) {
+ foodValidator(this.#enrollFood(food));
+ }
+
+ #enrollFood(food) {
+ const foodInfoArray = food.split(KNIFE.bladePosition);
+ this.#name = foodInfoArray[KNIFE.namePosition];
+ this.#quantity = Number(foodInfoArray[KNIFE.quantityPosition]);
+
+ return foodInfoArray;
+ }
+
+ #category() {
+ const [foodTypeInfo] = MENU
+ .filter((foodType) => Object.hasOwn(foodType, this.#name));
+
+ return foodTypeInfo;
+ }
+
+ type() {
+ return this.#category()[NAME.type];
+ }
+
+ name() {
+ return this.#name;
+ }
+
+ quantity() {
+ return this.#quantity;
+ }
+
+ totalPrice() {
+ return this.#category()[this.#name] * this.quantity();
+ }
+}
+
+export default Food; | JavaScript | get์ ์ ํํ ์ธ์ ๋ถ์ฌ์ผ ํ ์ง ๊ณ ๋ฏผ์ด ๋ฉ๋๋ค. type ๋ฉ์๋์ ๊ฒฝ์ฐ type์ ๊ฐ์ ธ์ค๋ ๊ฒ์ด๋ get์ ๋ถ์ฌ์ผ ํ ์ง,
๋จ์ํ ํ๋ ๊ฐ์ ๊ฐ์ ธ์ค๋๊ฒ ์๋๋ผ ์์ฐํ ๋ก์ง์ด ๊ตฌํ ๋์ด ์์ผ๋ food.type() ์ฐ๋ก๋ ์๋ฏธ ์ ๋ฌํ ์ถฉ๋ถํ์ง ์์ง ํ๊ฐ๋ฆฌ๋ ๋ถ๋ถ์ธ ๊ฒ ๊ฐ์ต๋๋ค.ใ
ใ
|
@@ -0,0 +1,106 @@
+import Cashier from "../src/domain/Cashier.js";
+
+describe('Cashier ํด๋์ค ์ ํจ์ฑ ํ
์คํธ', () => {
+ test.each([
+ ['์ ๋ก์ฝ๋ผ-10'],
+ ['์ ๋ก์ฝ๋ผ-2,์ดํ์ธ-4'],
+ ['์ดํ์ธ-3,๋ ๋์์ธ-1,์ ๋ก์ฝ๋ผ-2']
+ ])('์๋ฃ๋ง ์ฃผ๋ฌธ์ ์์ธ์ฒ๋ฆฌ', (input) => {
+ expect(() => new Cashier(input)).toThrow('[ERROR');
+ });
+
+ test.each([
+ ['์์ด์คํฌ๋ฆผ-3,์ด์ฝ์ผ์ดํฌ-4,์์ด์คํฌ๋ฆผ-1'],
+ ['ํฐ๋ณธ์คํ
์ดํฌ-3,๋ฐ๋นํ๋ฆฝ-4,์์ก์ด์ํ-3,์์ก์ด์ํ-1'],
+ ['์์ ์๋ฌ๋-1,์ ๋ก์ฝ๋ผ-1,์ ๋ก์ฝ๋ผ-1,์์ ์๋ฌ๋-4,ํฌ๋ฆฌ์ค๋ง์คํ์คํ-1']
+ ])('์ฃผ๋ฌธํ ์์ ์ด๋ฆ์ด ์ค๋ณต๋๋ฉด ์์ธ์ฒ๋ฆฌ', (input) => {
+ expect(() => new Cashier(input)).toThrow('[ERROR]');
+ });
+
+ test.each([
+ ['์ ๋ก์ฝ๋ผ-21'],
+ ['์ดํ์ธ-3,์์ก์ด์ํ-10,์์ ์
๋ฌ๋-8,์์ด์คํฌ๋ฆผ-10'],
+ ['ํฐ๋ณธ์คํ
์ดํฌ-22,๋ ๋์์ธ-1']
+ ])('์ฃผ๋ฌธ ์๋์ด 20 ์ด๊ณผ์ ์์ธ์ฒ๋ฆฌ', (input) => {
+ expect(() => new Cashier(input)).toThrow('[ERROR]');
+ });
+
+ test.each([
+ ['์์ด์คํฌ๋ฆผ-3.1'],
+ ['์ด์ฝ์ผ์ดํฌ-3.a'],
+ ['ํฐ๋ณธ์คํ
์ดํฌ-a'],
+ ['ํฌ๋ฆฌ์ค๋ง์คํ์คํ-0.99']
+ ])('์ฃผ๋ฌธ ์๋์ด ์ฌ๋ฐ๋ฅธ ํ์์ด ์๋๋ฉด ์์ธ์ฒ๋ฆฌ', (input) => {
+ expect(() => new Cashier(input)).toThrow('[ERROR]');
+ });
+
+ test.each([
+ ['์ ๋ก์ฝ๋ผ-1,์์ด์คํฌ๋ฆผ-19'],
+ ['ํฐ๋ณธ์คํ
์ดํฌ-20'],
+ ['์์ ์๋ฌ๋-3,์์ก์ด์ํ-15,๋ ๋์์ธ-2']
+ ])('์ฌ๋ฐ๋ฅด๊ฒ ์
๋ ฅ์ ์์ธ๋ฅผ ๋ฐ์์ํค์ง ์๋๋ค', (input) => {
+ expect(() => new Cashier(input)).not.toThrow();
+ });
+});
+
+describe('Cashier ํด๋์ค ๋ฉ์๋ ๋จ์ํ
์คํธ', () => {
+ test('์์ ์ด๋ฆ๊ณผ ์๋์ ์ ์ง์ง์ด์ ๋ณด์ฌ์ฃผ๋์ง ํ
์คํธ', () => {
+ const ORDER_MENU = 'ํฐ๋ณธ์คํ
์ดํฌ-2,์ดํ์ธ-3,์ด์ฝ์ผ์ดํฌ-4,์์ ์๋ฌ๋-5,๋ฐ๋นํ๋ฆฝ-3,ํํ์ค-3';
+ const RESULT = [
+ 'ํฐ๋ณธ์คํ
์ดํฌ 2๊ฐ',
+ '์ดํ์ธ 3๊ฐ',
+ '์ด์ฝ์ผ์ดํฌ 4๊ฐ',
+ '์์ ์๋ฌ๋ 5๊ฐ',
+ '๋ฐ๋นํ๋ฆฝ 3๊ฐ',
+ 'ํํ์ค 3๊ฐ'
+ ];
+
+ const cashier = new Cashier(ORDER_MENU);
+
+ expect(cashier.foodsListWithQuantity()).toEqual(RESULT);
+ });
+
+ test('์ฃผ๋ฌธํ ์์๋ค์ ์ด ๊ฐ๊ฒฉ์ ์ ๊ณ์ฐํ๋์ง ํ
์คํธ', () => {
+ const ORDER_MENU = '์์ด์คํฌ๋ฆผ-4,ํฐ๋ณธ์คํ
์ดํฌ-3,์ดํ์ธ-2,์์ ์๋ฌ๋-1';
+ const RESULT = (5000 * 4) + (55000 * 3) + (25000 * 2) + (8000 * 1);
+ const cashier = new Cashier(ORDER_MENU);
+
+ expect(cashier.totalFoodsPrice()).toEqual(RESULT);
+ });
+
+ let typeIndex = 0
+ test.each([
+ ['appetizer'],
+ ['main'],
+ ['desert'],
+ ['drink']
+ ])('ํด๋น ์ฃผ๋ฌธ์ ์ง์ ํ ํ์
์์์ ์ด์๋์ ์ ๊ณ์ฐํ๋์ง ํ
์คํธ', (input) => {
+ const ORDER_MENU =
+ '์์ก์ด์ํ-3,ํํ์ค-3,' +
+ 'ํฐ๋ณธ์คํ
์ดํฌ-1,ํด์ฐ๋ฌผํ์คํ-2,๋ฐ๋นํ๋ฆฝ-2,' +
+ '์ด์ฝ์ผ์ดํฌ-3,์์ด์คํฌ๋ฆผ-1,' +
+ '์ ๋ก์ฝ๋ผ-2,์ดํ์ธ-1';
+
+ const RESULT = [6,5,4,3];
+ const cashier = new Cashier(ORDER_MENU);
+
+ expect(cashier.totalTypeQuantity(input)).toEqual(RESULT[typeIndex]);
+ typeIndex += 1;
+ });
+
+ test('์ฃผ๋ฌธ ๊ธ์ก์ด 12๋ง์ ์ด์์ด๋ฉด ์ดํ์ธ ์ฆ์ ํด์ฃผ๋์ง ํ
์คํธ', () => {
+ const ORDER_MENU = 'ํฐ๋ณธ์คํ
์ดํฌ-2,์์ก์ด์ํ-1,ํํ์ค-1';
+ const RESULT = '์ดํ์ธ 1๊ฐ';
+ const cashier = new Cashier(ORDER_MENU);
+
+ expect(cashier.freebieMenu()).toEqual(RESULT);
+ });
+
+ test('์ฃผ๋ฌธ ๊ธ์ก์ด 12๋ง์ ๋ฏธ๋ง์ด๋ฉด ์์์ ํ์ํ๋์ง ํ
์คํธ', () => {
+ const ORDER_MENU = 'ํฐ๋ณธ์คํ
์ดํฌ-2,์์ก์ด์ํ-1';
+ const RESULT = '์์';
+ const cashier = new Cashier(ORDER_MENU);
+
+ expect(cashier.freebieMenu()).toEqual(RESULT);
+ });
+}); | JavaScript | ํ
์คํธ ๊ผผ๊ผผํ๊ฑฐ ๋๋ฌด ์ข์ต๋๋ค! ๐ ๊ทธ๋ฐ๋ฐ ํ
์คํธ๋ฌธ๊ตฌ ์์์ ํ๋๋ก ํต์ผํ๋ฉด ์ข์ ๊ฒ ๊ฐ์์!
ํด๋น ๋ฌธ๊ตฌ์ฒ๋ผ `์์ธ๋ฅผ ๋ฐ์์ํค์ง ์๋๋ค`. ์ ๋ง์ถฐ์ `์์ธ์ฒ๋ฆฌ` -> `์์ธ๋ฅผ ๋ฐ์์ํจ๋ค` ๋ก ํต์ผํ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,70 @@
+import Food from "./Food.js";
+import { ERROR_MENU } from "../constants/ErrorMesseage.js";
+import { merageFoodInfo } from "../constants/ViewRefiner.js";
+import { DISCOUNT, BENEFIT_MESSEAGE } from "../constants/BenefitStorage.js";
+import { NAME } from "../constants/FoodStorage.js";
+
+class Cashier {
+ #foods = [];
+
+ constructor(orderHistory) {
+ this.#enrollFoods(orderHistory);
+ this.#orderDuplicationValidator();
+ this.#onlyDrinkValidator();
+ this.#orderMaxValidator();
+ }
+
+ #enrollFoods(orderHistory) {
+ orderHistory
+ .split(',')
+ .forEach((food) => this.#foods.push(new Food(food)));
+ }
+
+ #orderDuplicationValidator() {
+ const foodNameList = this.#foods.map((food) => food.name());
+ if (foodNameList.length !== new Set(foodNameList).size) {
+ throw new Error(ERROR_MENU.basic);
+ }
+ }
+
+ #onlyDrinkValidator() {
+ const drinkTypeFoodsList = this.#foods.filter((food) => food.type() === NAME.drink);
+
+ if (drinkTypeFoodsList.length === this.#foods.length) {
+ throw new Error(ERROR_MENU.onlyDrink);
+ }
+ }
+
+ #orderMaxValidator() {
+ if (this.#totalFoodsQuantity() > ERROR_MENU.maxOrderNumber) {
+ throw new Error(`${ERROR_MENU.maxOrder}\n${ERROR_MENU.orderAmountExample}`);
+ }
+ }
+
+ #totalFoodsQuantity() {
+ return this.#foods.reduce((acc,cur) => acc + cur.quantity(), 0);
+ }
+
+ foodsListWithQuantity() {
+ return this.#foods.map((foodInfo) => merageFoodInfo(foodInfo.name(), foodInfo.quantity()));
+ }
+
+ totalFoodsPrice() {
+ return this.#foods.reduce((acc, cur) => acc + cur.totalPrice(), 0);
+ }
+
+ totalTypeQuantity(foodType) {
+ return this.#foods
+ .filter((food) => food.type() === foodType)
+ .reduce((acc, cur) => acc + cur.quantity(), 0);
+ }
+
+ freebieMenu() {
+ if (this.totalFoodsPrice() >= DISCOUNT.freebieStandard) {
+ return DISCOUNT.freebieItem;
+ }
+ return BENEFIT_MESSEAGE.non;
+ }
+}
+
+export default Cashier; | JavaScript | ํน์ ์ถ๋ ฅ๊ณผ ๊ด๋ จ๋ ํฌ๋งทํ
๋ถ๋ถ์ ๋๋ฉ์ธ์์ ์ฒ๋ฆฌํ ์ด์ ๊ฐ ์์ผ์ค๊น์? ๊ถ๊ธํฉ๋๋ค! ํฌ๋งทํ
ํด์ฃผ๋ ๊ฒ๋ ๋๋ฉ์ธ์ ์ญํ ์ด๋ผ๊ณ ์๊ฐํ์
จ๊ธฐ ๋๋ฌธ์ผ๊น์? |
@@ -1,7 +1,67 @@
-export default OutputView = {
- printMenu() {
- Console.print("<์ฃผ๋ฌธ ๋ฉ๋ด>");
- // ...
- }
- // ...
-}
+import { Console } from "@woowacourse/mission-utils";
+import { ANSWER } from "./constants/PlannerMesseage.js";
+import { PRICE_REFINER, BENEFITS_REFINER } from "./constants/ViewRefiner.js";
+
+const OutputView = {
+ printIntroduce() {
+ Console.print(ANSWER.introduce);
+ },
+
+ printProlog(date) {
+ Console.print(`${ANSWER.prolog(date)}\n`);
+ },
+
+ printOrderMenuResult(foodsArray) {
+ Console.print(ANSWER.orderMenu);
+
+ foodsArray.forEach((food) => Console.print(food));
+ Console.print('');
+ },
+
+ printTotalOrderAmountResult(amount) {
+ Console.print(ANSWER.totalOrderAmount);
+
+ Console.print(PRICE_REFINER.positive(amount));
+ Console.print('');
+ },
+
+ printFreebieMenuResult(freebieMenu) {
+ Console.print(ANSWER.freebieMenu);
+
+ Console.print(freebieMenu);
+ Console.print('');
+ },
+
+ printBenefitListResult(benefitArray) {
+ Console.print(ANSWER.benefitList);
+
+ BENEFITS_REFINER.benfitList(benefitArray).forEach((benefit) => Console.print(benefit));
+ Console.print('');
+ },
+
+ printTotalBenefitAmountResult(totalBenefitAmount) {
+ Console.print(ANSWER.totalBenefitAmount);
+
+ Console.print(PRICE_REFINER.negative(totalBenefitAmount));
+ Console.print('');
+ },
+
+ printFinalPaymentAmountResult(finalPaymentAmount) {
+ Console.print(ANSWER.finalPaymentAmount);
+
+ Console.print(PRICE_REFINER.positive(finalPaymentAmount));
+ Console.print('');
+ },
+
+ printBadgeResult(badge) {
+ Console.print(ANSWER.DecemberEventBadge);
+
+ Console.print(badge);
+ },
+
+ printErrorMesseage(messeage) {
+ Console.print(`${messeage}`);
+ }
+};
+
+export default OutputView; | JavaScript | ํด๋น๋ถ๋ถ messeage ๊ทธ๋ฅ ๋ฐ๋ก ์ถ๋ ฅํด๋ ๋ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -1,7 +1,67 @@
-export default OutputView = {
- printMenu() {
- Console.print("<์ฃผ๋ฌธ ๋ฉ๋ด>");
- // ...
- }
- // ...
-}
+import { Console } from "@woowacourse/mission-utils";
+import { ANSWER } from "./constants/PlannerMesseage.js";
+import { PRICE_REFINER, BENEFITS_REFINER } from "./constants/ViewRefiner.js";
+
+const OutputView = {
+ printIntroduce() {
+ Console.print(ANSWER.introduce);
+ },
+
+ printProlog(date) {
+ Console.print(`${ANSWER.prolog(date)}\n`);
+ },
+
+ printOrderMenuResult(foodsArray) {
+ Console.print(ANSWER.orderMenu);
+
+ foodsArray.forEach((food) => Console.print(food));
+ Console.print('');
+ },
+
+ printTotalOrderAmountResult(amount) {
+ Console.print(ANSWER.totalOrderAmount);
+
+ Console.print(PRICE_REFINER.positive(amount));
+ Console.print('');
+ },
+
+ printFreebieMenuResult(freebieMenu) {
+ Console.print(ANSWER.freebieMenu);
+
+ Console.print(freebieMenu);
+ Console.print('');
+ },
+
+ printBenefitListResult(benefitArray) {
+ Console.print(ANSWER.benefitList);
+
+ BENEFITS_REFINER.benfitList(benefitArray).forEach((benefit) => Console.print(benefit));
+ Console.print('');
+ },
+
+ printTotalBenefitAmountResult(totalBenefitAmount) {
+ Console.print(ANSWER.totalBenefitAmount);
+
+ Console.print(PRICE_REFINER.negative(totalBenefitAmount));
+ Console.print('');
+ },
+
+ printFinalPaymentAmountResult(finalPaymentAmount) {
+ Console.print(ANSWER.finalPaymentAmount);
+
+ Console.print(PRICE_REFINER.positive(finalPaymentAmount));
+ Console.print('');
+ },
+
+ printBadgeResult(badge) {
+ Console.print(ANSWER.DecemberEventBadge);
+
+ Console.print(badge);
+ },
+
+ printErrorMesseage(messeage) {
+ Console.print(`${messeage}`);
+ }
+};
+
+export default OutputView; | JavaScript | ์ ๋ชฉ ๋ถ๋ถ์ด๋ผ์ ๋ค ๋์ด์ฐ๊ธฐ๋ฅผ ํ์ ๊ฑธ๊น์? ๊ผผ๊ผผํฉ๋๋ค! ๐ |
@@ -0,0 +1,40 @@
+export const DISCOUNT = Object.freeze({
+ christmasDate : 25,
+ christmasBasic : 1000,
+ christmasAccumulation : 100,
+ weekday : 2023,
+ weekend : 2023,
+ special : 1000,
+ freebieItem : '์ดํ์ธ 1๊ฐ',
+ freebieStandard : 120000,
+ freebiePrice : 25000,
+ eventStandardAmount : 10000,
+ zero : 0,
+});
+
+export const EVENT_NAME = Object.freeze({
+ christmas : 'christmas',
+ weekday : 'weekday',
+ weekend : 'weekend',
+ special : 'special',
+ freebie : 'freebie',
+ condition : 'condition',
+});
+
+export const BADGE = Object.freeze({
+ star : 5000,
+ tree : 10000,
+ santa : 20000,
+ starName : '๋ณ',
+ treeName : 'ํธ๋ฆฌ',
+ santaName : '์ฐํ',
+});
+
+export const BENEFIT_MESSEAGE = Object.freeze({
+ christmas : 'ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: ',
+ weekday : 'ํ์ผ ํ ์ธ: ',
+ weekend : '์ฃผ๋ง ํ ์ธ: ',
+ special : 'ํน๋ณ ํ ์ธ: ',
+ freebie : '์ฆ์ ์ด๋ฒคํธ: ',
+ non : '์์',
+}); | JavaScript | ํด๋น ์ดํ์ธ์ string์ผ๋ก ๋ฃ๊ธฐ๋ณด๋ค ์ ์ธํด๋์ผ์ ์์ ์จ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,40 @@
+export const DISCOUNT = Object.freeze({
+ christmasDate : 25,
+ christmasBasic : 1000,
+ christmasAccumulation : 100,
+ weekday : 2023,
+ weekend : 2023,
+ special : 1000,
+ freebieItem : '์ดํ์ธ 1๊ฐ',
+ freebieStandard : 120000,
+ freebiePrice : 25000,
+ eventStandardAmount : 10000,
+ zero : 0,
+});
+
+export const EVENT_NAME = Object.freeze({
+ christmas : 'christmas',
+ weekday : 'weekday',
+ weekend : 'weekend',
+ special : 'special',
+ freebie : 'freebie',
+ condition : 'condition',
+});
+
+export const BADGE = Object.freeze({
+ star : 5000,
+ tree : 10000,
+ santa : 20000,
+ starName : '๋ณ',
+ treeName : 'ํธ๋ฆฌ',
+ santaName : '์ฐํ',
+});
+
+export const BENEFIT_MESSEAGE = Object.freeze({
+ christmas : 'ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: ',
+ weekday : 'ํ์ผ ํ ์ธ: ',
+ weekend : '์ฃผ๋ง ํ ์ธ: ',
+ special : 'ํน๋ณ ํ ์ธ: ',
+ freebie : '์ฆ์ ์ด๋ฒคํธ: ',
+ non : '์์',
+}); | JavaScript | ๋ณ์๋ช
์ ๊ฐ๊ฒฉ์ด๋ผ๋๊ฒ ๋ ๋๋ฌ๋๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,40 @@
+export const DISCOUNT = Object.freeze({
+ christmasDate : 25,
+ christmasBasic : 1000,
+ christmasAccumulation : 100,
+ weekday : 2023,
+ weekend : 2023,
+ special : 1000,
+ freebieItem : '์ดํ์ธ 1๊ฐ',
+ freebieStandard : 120000,
+ freebiePrice : 25000,
+ eventStandardAmount : 10000,
+ zero : 0,
+});
+
+export const EVENT_NAME = Object.freeze({
+ christmas : 'christmas',
+ weekday : 'weekday',
+ weekend : 'weekend',
+ special : 'special',
+ freebie : 'freebie',
+ condition : 'condition',
+});
+
+export const BADGE = Object.freeze({
+ star : 5000,
+ tree : 10000,
+ santa : 20000,
+ starName : '๋ณ',
+ treeName : 'ํธ๋ฆฌ',
+ santaName : '์ฐํ',
+});
+
+export const BENEFIT_MESSEAGE = Object.freeze({
+ christmas : 'ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: ',
+ weekday : 'ํ์ผ ํ ์ธ: ',
+ weekend : '์ฃผ๋ง ํ ์ธ: ',
+ special : 'ํน๋ณ ํ ์ธ: ',
+ freebie : '์ฆ์ ์ด๋ฒคํธ: ',
+ non : '์์',
+}); | JavaScript | ๊ผผ๊ผผํ์ญ๋๋น!! ๐๐๐ |
@@ -0,0 +1,40 @@
+export const DISCOUNT = Object.freeze({
+ christmasDate : 25,
+ christmasBasic : 1000,
+ christmasAccumulation : 100,
+ weekday : 2023,
+ weekend : 2023,
+ special : 1000,
+ freebieItem : '์ดํ์ธ 1๊ฐ',
+ freebieStandard : 120000,
+ freebiePrice : 25000,
+ eventStandardAmount : 10000,
+ zero : 0,
+});
+
+export const EVENT_NAME = Object.freeze({
+ christmas : 'christmas',
+ weekday : 'weekday',
+ weekend : 'weekend',
+ special : 'special',
+ freebie : 'freebie',
+ condition : 'condition',
+});
+
+export const BADGE = Object.freeze({
+ star : 5000,
+ tree : 10000,
+ santa : 20000,
+ starName : '๋ณ',
+ treeName : 'ํธ๋ฆฌ',
+ santaName : '์ฐํ',
+});
+
+export const BENEFIT_MESSEAGE = Object.freeze({
+ christmas : 'ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: ',
+ weekday : 'ํ์ผ ํ ์ธ: ',
+ weekend : '์ฃผ๋ง ํ ์ธ: ',
+ special : 'ํน๋ณ ํ ์ธ: ',
+ freebie : '์ฆ์ ์ด๋ฒคํธ: ',
+ non : '์์',
+}); | JavaScript | ๊ทธ๋ฆฌ๊ณ ํด๋น `DISCOUNT` ์์์ ์ด๋ฒคํธ์ ๊ด๋ จ๋ ๋ชจ๋ ์ ๋ณด๊ฐ ๋ค์ด๊ฐ์๋๋ฐ, ์์ ํฌ๋ฆฌ์ค๋ง์ค ์ด๋ฒคํธ, ํ์ผ ์ด๋ฒคํธ, ๋ฑ๋ฑ์ผ๋ก ์์๋ฅผ ์ชผ๊ฐ๊ฑฐ๋, ํน์ ์ค๊ฐ์ ๊ณต๋ฐฑ๋ผ์ธ์ ๋ฃ์ผ๋ฉด ์ข์ ๊ฒ ๊ฐ์์! ๊ตฌ๋ถ๋์ด์์ง ์๋ค๋ณด๋๊น ํ์
ํ๊ธฐ ์กฐ๊ธ ์ด๋ ค์ธ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,40 @@
+export const DISCOUNT = Object.freeze({
+ christmasDate : 25,
+ christmasBasic : 1000,
+ christmasAccumulation : 100,
+ weekday : 2023,
+ weekend : 2023,
+ special : 1000,
+ freebieItem : '์ดํ์ธ 1๊ฐ',
+ freebieStandard : 120000,
+ freebiePrice : 25000,
+ eventStandardAmount : 10000,
+ zero : 0,
+});
+
+export const EVENT_NAME = Object.freeze({
+ christmas : 'christmas',
+ weekday : 'weekday',
+ weekend : 'weekend',
+ special : 'special',
+ freebie : 'freebie',
+ condition : 'condition',
+});
+
+export const BADGE = Object.freeze({
+ star : 5000,
+ tree : 10000,
+ santa : 20000,
+ starName : '๋ณ',
+ treeName : 'ํธ๋ฆฌ',
+ santaName : '์ฐํ',
+});
+
+export const BENEFIT_MESSEAGE = Object.freeze({
+ christmas : 'ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: ',
+ weekday : 'ํ์ผ ํ ์ธ: ',
+ weekend : '์ฃผ๋ง ํ ์ธ: ',
+ special : 'ํน๋ณ ํ ์ธ: ',
+ freebie : '์ฆ์ ์ด๋ฒคํธ: ',
+ non : '์์',
+}); | JavaScript | `star : { price : 5000, name : '๋ณ'}` ํด๋น ํ์์ ์ด๋จ์ง ์ถ์ฒํ๊ณ ๊ฐ๋๋ค! |
@@ -0,0 +1,14 @@
+export const ERROR_DATE = Object.freeze({
+ basic : '[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ minDate : 1,
+ maxDate : 31,
+});
+
+export const ERROR_MENU = Object.freeze({
+ basic : '[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ onlyDrink : '[ERROR] ์๋ฃ๋ง ์ฃผ๋ฌธ ์, ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.',
+ maxOrder : '[ERROR] ๋ฉ๋ด๋ ํ ๋ฒ์ ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.',
+ orderAmountExample : '(e.g. ์์ ์๋ฌ๋-1, ํฐ๋ณธ์คํ
์ดํฌ-1, ํฌ๋ฆฌ์ค๋ง์คํ์คํ-1, ์ ๋ก์ฝ๋ผ-3, ์์ด์คํฌ๋ฆผ-1์ ์ด๊ฐ์๋ 7๊ฐ)',
+ minOrderNumber : 1,
+ maxOrderNumber : 20,
+}); | JavaScript | ํด๋น 20๋ ์์๋ก ๋นผ๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,43 @@
+export const APPETIZER = Object.freeze({
+ '์์ก์ด์ํ' : 6000,
+ 'ํํ์ค' : 5500,
+ '์์ ์๋ฌ๋' : 8000,
+ 'type' : 'appetizer',
+});
+
+export const MAIN = Object.freeze({
+ 'ํฐ๋ณธ์คํ
์ดํฌ' : 55000,
+ '๋ฐ๋นํ๋ฆฝ' : 54000,
+ 'ํด์ฐ๋ฌผํ์คํ' : 35000,
+ 'ํฌ๋ฆฌ์ค๋ง์คํ์คํ' : 25000,
+ 'type' : 'main',
+});
+
+export const DESERT = Object.freeze({
+ '์ด์ฝ์ผ์ดํฌ' : 15000,
+ '์์ด์คํฌ๋ฆผ' : 5000,
+ 'type' : 'desert',
+});
+
+export const DRINK = Object.freeze({
+ '์ ๋ก์ฝ๋ผ' : 3000,
+ '๋ ๋์์ธ' : 60000,
+ '์ดํ์ธ' : 25000,
+ 'type' : 'drink',
+});
+
+export const MENU = [APPETIZER, MAIN, DESERT, DRINK];
+
+export const NAME = Object.freeze({
+ type : 'type',
+ appetizer : 'appetizer',
+ main : 'main',
+ desert : 'desert',
+ drink : 'drink',
+});
+
+export const KNIFE = Object.freeze({
+ namePosition : 0,
+ quantityPosition : 1,
+ bladePosition : '-'
+}); | JavaScript | ํ์
๋ถ๋ถ์ ํด๋น ๊ฐ์ฒด์ ๋ง์ง๋ง์ ๋ฃ๊ธฐ๋ณด๋จ, ๋ฐ๋ก ๋นผ์ ํ์ธํ๋๊ฒ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค!
๊ฐ์ฒด๊ฐ ์ค๊ฐ์ ์์ฑ์ด ๋ค๋ฅผ ๊ฒฝ์ฐ, ์ ์ง๋ณด์๋ ํ๋ค๋ฟ๋ง ์๋๋ผ ๋งค๋ฒ `APPETIZER`์ ๋ง์ง๋ง ์์ฑ์ type์ด๋ผ๋๊ฑธ ๊ธฐ์ตํด๋๊ณ ์ฝ๋๋ฅผ ์ง์ผํด์, type์ ํด๋น ๊ฐ์ฒด์์ ๋ฐ๋ก ๋นผ๋๊ฒ ์ข์๋ณด์
๋๋ค!
```js
export const APPETIZER = Object.freeze({
'์์ก์ด์ํ': 6000,
'ํํ์ค': 5500,
'์์ ์๋ฌ๋': 8000,
});
``` |
@@ -0,0 +1,43 @@
+export const APPETIZER = Object.freeze({
+ '์์ก์ด์ํ' : 6000,
+ 'ํํ์ค' : 5500,
+ '์์ ์๋ฌ๋' : 8000,
+ 'type' : 'appetizer',
+});
+
+export const MAIN = Object.freeze({
+ 'ํฐ๋ณธ์คํ
์ดํฌ' : 55000,
+ '๋ฐ๋นํ๋ฆฝ' : 54000,
+ 'ํด์ฐ๋ฌผํ์คํ' : 35000,
+ 'ํฌ๋ฆฌ์ค๋ง์คํ์คํ' : 25000,
+ 'type' : 'main',
+});
+
+export const DESERT = Object.freeze({
+ '์ด์ฝ์ผ์ดํฌ' : 15000,
+ '์์ด์คํฌ๋ฆผ' : 5000,
+ 'type' : 'desert',
+});
+
+export const DRINK = Object.freeze({
+ '์ ๋ก์ฝ๋ผ' : 3000,
+ '๋ ๋์์ธ' : 60000,
+ '์ดํ์ธ' : 25000,
+ 'type' : 'drink',
+});
+
+export const MENU = [APPETIZER, MAIN, DESERT, DRINK];
+
+export const NAME = Object.freeze({
+ type : 'type',
+ appetizer : 'appetizer',
+ main : 'main',
+ desert : 'desert',
+ drink : 'drink',
+});
+
+export const KNIFE = Object.freeze({
+ namePosition : 0,
+ quantityPosition : 1,
+ bladePosition : '-'
+}); | JavaScript | ์ด ๋ถ๋ถ์ `-` ๋ก ์ชผ๊ฐฐ์๋ ๋์ค๋ ๋ฐฐ์ด์ index๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํ ์์ ๋ง์๊น์? ๊ทธ๋ ๋ค๋ฉด ๊ตฌ์กฐ๋ถํดํ ๋น ์ฐ๋ฉด ๋ ๊น๋ํ ๊ฒ ๊ฐ์์! ๊ทธ๋ ๊ฒ ๋๋ค๋ฉด `KNIFE `์์๋ช
๋ณด๋ค๋ `delimiter`๋ `seperator`๋ก ๋ฐ๊พธ๋๊ฒ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,14 @@
+export const ERROR_DATE = Object.freeze({
+ basic : '[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ minDate : 1,
+ maxDate : 31,
+});
+
+export const ERROR_MENU = Object.freeze({
+ basic : '[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ onlyDrink : '[ERROR] ์๋ฃ๋ง ์ฃผ๋ฌธ ์, ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.',
+ maxOrder : '[ERROR] ๋ฉ๋ด๋ ํ ๋ฒ์ ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.',
+ orderAmountExample : '(e.g. ์์ ์๋ฌ๋-1, ํฐ๋ณธ์คํ
์ดํฌ-1, ํฌ๋ฆฌ์ค๋ง์คํ์คํ-1, ์ ๋ก์ฝ๋ผ-3, ์์ด์คํฌ๋ฆผ-1์ ์ด๊ฐ์๋ 7๊ฐ)',
+ minOrderNumber : 1,
+ maxOrderNumber : 20,
+}); | JavaScript | `ERROR_MENU`์๋ ์๋ฌ์ ๊ด๋ จ๋๊ฒ๋ง ์์ผ๋ฉด ์ข์ ๊ฒ ๊ฐ์๋ฐ, 11,12,13 line์ ์กฐ๊ธ ์ ๋งคํ ๊ฒ ๊ฐ์ต๋๋ค!
`ERROR_MENU`-> `MENU`๋ก ๋ฐ๊พธ๊ณ , ๊ทธ ์์ `ERROR`๋ก ํ๋ฒ ๋ ๊ฐ์ธ๋๊ฑด ์ด๋จ์ง ์ถ์ฒํ๊ณ ๊ฐ๋๋น! |
@@ -0,0 +1,41 @@
+import { BENEFIT_MESSEAGE } from "./BenefitStorage.js";
+import { EVENT_NAME } from "./BenefitStorage.js";
+
+export const priceFilter = (priceNumber) => new Intl.NumberFormat('ko-KR').format(priceNumber);
+
+export const merageFoodInfo = (foodName, foodAmount) => `${foodName} ${foodAmount}๊ฐ`;
+
+export const PRICE_REFINER = Object.freeze({
+ positive : (priceNumber) => `${priceFilter(priceNumber)}์`,
+ negative : (priceNumber) => {
+ if (priceNumber === 0) return `${priceNumber}์`;
+
+ return `-${priceFilter(priceNumber)}์`;
+ },
+});
+
+export const isNotApplicableBenefits = (benefitList) => {
+ const benefitsResultArray = [];
+ for (let benefitResult of benefitList.values()) {
+ if (benefitResult !== 0) benefitsResultArray.push(benefitResult);
+ }
+ if (benefitsResultArray.length === 1) return true;
+
+ if (!benefitList.get(EVENT_NAME.condition)) return true;
+
+ return false;
+};
+
+export const BENEFITS_REFINER = Object.freeze({
+ benfitList : (benefitList) => {
+ if (isNotApplicableBenefits(benefitList)) return [BENEFIT_MESSEAGE.non];
+
+ const refinedBenefits = [];
+ benefitList.forEach((value, key) => {
+ if (value !==0 && typeof(value) !== 'boolean') {
+ refinedBenefits.push(`${BENEFIT_MESSEAGE[key]}${PRICE_REFINER.negative(value)}`);
+ }
+ });
+ return refinedBenefits;
+ },
+}); | JavaScript | ํด๋น ๊ฐ์ฒด์ ๊ฒฝ์ฐ positive, negative์์ฑ์ ๋๋์ง๋ง๊ณ ์ฒ์๋ถํฐ 3๊ฐ์ง ์กฐ๊ฑด๋ฌธ์ผ๋ก ๋ฐ๋ก ๊ฒ์ฌํ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,41 @@
+import { BENEFIT_MESSEAGE } from "./BenefitStorage.js";
+import { EVENT_NAME } from "./BenefitStorage.js";
+
+export const priceFilter = (priceNumber) => new Intl.NumberFormat('ko-KR').format(priceNumber);
+
+export const merageFoodInfo = (foodName, foodAmount) => `${foodName} ${foodAmount}๊ฐ`;
+
+export const PRICE_REFINER = Object.freeze({
+ positive : (priceNumber) => `${priceFilter(priceNumber)}์`,
+ negative : (priceNumber) => {
+ if (priceNumber === 0) return `${priceNumber}์`;
+
+ return `-${priceFilter(priceNumber)}์`;
+ },
+});
+
+export const isNotApplicableBenefits = (benefitList) => {
+ const benefitsResultArray = [];
+ for (let benefitResult of benefitList.values()) {
+ if (benefitResult !== 0) benefitsResultArray.push(benefitResult);
+ }
+ if (benefitsResultArray.length === 1) return true;
+
+ if (!benefitList.get(EVENT_NAME.condition)) return true;
+
+ return false;
+};
+
+export const BENEFITS_REFINER = Object.freeze({
+ benfitList : (benefitList) => {
+ if (isNotApplicableBenefits(benefitList)) return [BENEFIT_MESSEAGE.non];
+
+ const refinedBenefits = [];
+ benefitList.forEach((value, key) => {
+ if (value !==0 && typeof(value) !== 'boolean') {
+ refinedBenefits.push(`${BENEFIT_MESSEAGE[key]}${PRICE_REFINER.negative(value)}`);
+ }
+ });
+ return refinedBenefits;
+ },
+}); | JavaScript | ํน์ ์๋ค๋ ์์๋ผ๊ณ ๋ณผ ์ ์๋๋ฐ ์นด๋ฉ์ผ์ด์ค๋ก ์์ฑํ์ ์ด์ ๊ฐ ์์๊น์? |
@@ -0,0 +1,41 @@
+import { BENEFIT_MESSEAGE } from "./BenefitStorage.js";
+import { EVENT_NAME } from "./BenefitStorage.js";
+
+export const priceFilter = (priceNumber) => new Intl.NumberFormat('ko-KR').format(priceNumber);
+
+export const merageFoodInfo = (foodName, foodAmount) => `${foodName} ${foodAmount}๊ฐ`;
+
+export const PRICE_REFINER = Object.freeze({
+ positive : (priceNumber) => `${priceFilter(priceNumber)}์`,
+ negative : (priceNumber) => {
+ if (priceNumber === 0) return `${priceNumber}์`;
+
+ return `-${priceFilter(priceNumber)}์`;
+ },
+});
+
+export const isNotApplicableBenefits = (benefitList) => {
+ const benefitsResultArray = [];
+ for (let benefitResult of benefitList.values()) {
+ if (benefitResult !== 0) benefitsResultArray.push(benefitResult);
+ }
+ if (benefitsResultArray.length === 1) return true;
+
+ if (!benefitList.get(EVENT_NAME.condition)) return true;
+
+ return false;
+};
+
+export const BENEFITS_REFINER = Object.freeze({
+ benfitList : (benefitList) => {
+ if (isNotApplicableBenefits(benefitList)) return [BENEFIT_MESSEAGE.non];
+
+ const refinedBenefits = [];
+ benefitList.forEach((value, key) => {
+ if (value !==0 && typeof(value) !== 'boolean') {
+ refinedBenefits.push(`${BENEFIT_MESSEAGE[key]}${PRICE_REFINER.negative(value)}`);
+ }
+ });
+ return refinedBenefits;
+ },
+}); | JavaScript | ์ฌ๊ธฐ !==๋ค์ ๋์ด์ฐ๊ธฐ 1๋ฒ ํด์ผํ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,81 @@
+import Supervisor from "../domain/Supervisor.js";
+import InputView from "../InputView.js";
+import OutputView from "../OutputView.js";
+
+class EventPlanner {
+ #supervisor;
+
+ constructor() {
+ this.#supervisor = new Supervisor();
+ }
+
+ async start() {
+ OutputView.printIntroduce();
+
+ await this.getDate();
+ await this.getMenu();
+
+ this.plannerResult();
+ }
+
+ async getDate() {
+ try {
+ this.#supervisor.dateUpload(await InputView.readDate());
+
+ } catch (error) {
+ OutputView.printErrorMesseage(error);
+ await this.getDate();
+ }
+ }
+
+ async getMenu() {
+ try {
+ this.#supervisor.menuUpload(await InputView.readMenu());
+
+ } catch (error) {
+ OutputView.printErrorMesseage(error);
+ await this.getMenu();
+ }
+ }
+
+ plannerResult() {
+ OutputView.printProlog(this.#supervisor.event().date());
+ this.orderMenu();
+ this.totalOrderAmount();
+ this.freebieMenu();
+ this.benefitList();
+ this.totalBenefitAmount();
+ this.finalPaymentAmount();
+ this.badge();
+ }
+
+ orderMenu() {
+ OutputView.printOrderMenuResult(this.#supervisor.cashier().foodsListWithQuantity());
+ }
+
+ totalOrderAmount() {
+ OutputView.printTotalOrderAmountResult(this.#supervisor.cashier().totalFoodsPrice());
+ }
+
+ freebieMenu() {
+ OutputView.printFreebieMenuResult(this.#supervisor.cashier().freebieMenu());
+ }
+
+ benefitList() {
+ OutputView.printBenefitListResult(this.#supervisor.benefitList());
+ }
+
+ totalBenefitAmount() {
+ OutputView.printTotalBenefitAmountResult(this.#supervisor.totalBenefitAmount());
+ }
+
+ finalPaymentAmount() {
+ OutputView.printFinalPaymentAmountResult(this.#supervisor.finalPaymentAmount());
+ }
+
+ badge() {
+ OutputView.printBadgeResult(this.#supervisor.giveBadge());
+ }
+}
+
+export default EventPlanner; | JavaScript | ํด๋น ๋ถ๋ถ์ ๋ฐ๋ก ํจ์๋ก ์๋นผ๊ณ `OutputView.print~`๋ก ๋ฐ๋ก ๋ฃ์ด๋ ๊ด์ฐฎ์ง ์์๊น ํฉ๋๋ค!
๊ฐ๊ฐ์ ํจ์์์ ๋ฐ๋ก ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ด ์๋๊ฒ ์๋๋ผ์, OutputView๋ก ๋ฐ๋ก ๋ค์ด๊ฐ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,70 @@
+import Food from "./Food.js";
+import { ERROR_MENU } from "../constants/ErrorMesseage.js";
+import { merageFoodInfo } from "../constants/ViewRefiner.js";
+import { DISCOUNT, BENEFIT_MESSEAGE } from "../constants/BenefitStorage.js";
+import { NAME } from "../constants/FoodStorage.js";
+
+class Cashier {
+ #foods = [];
+
+ constructor(orderHistory) {
+ this.#enrollFoods(orderHistory);
+ this.#orderDuplicationValidator();
+ this.#onlyDrinkValidator();
+ this.#orderMaxValidator();
+ }
+
+ #enrollFoods(orderHistory) {
+ orderHistory
+ .split(',')
+ .forEach((food) => this.#foods.push(new Food(food)));
+ }
+
+ #orderDuplicationValidator() {
+ const foodNameList = this.#foods.map((food) => food.name());
+ if (foodNameList.length !== new Set(foodNameList).size) {
+ throw new Error(ERROR_MENU.basic);
+ }
+ }
+
+ #onlyDrinkValidator() {
+ const drinkTypeFoodsList = this.#foods.filter((food) => food.type() === NAME.drink);
+
+ if (drinkTypeFoodsList.length === this.#foods.length) {
+ throw new Error(ERROR_MENU.onlyDrink);
+ }
+ }
+
+ #orderMaxValidator() {
+ if (this.#totalFoodsQuantity() > ERROR_MENU.maxOrderNumber) {
+ throw new Error(`${ERROR_MENU.maxOrder}\n${ERROR_MENU.orderAmountExample}`);
+ }
+ }
+
+ #totalFoodsQuantity() {
+ return this.#foods.reduce((acc,cur) => acc + cur.quantity(), 0);
+ }
+
+ foodsListWithQuantity() {
+ return this.#foods.map((foodInfo) => merageFoodInfo(foodInfo.name(), foodInfo.quantity()));
+ }
+
+ totalFoodsPrice() {
+ return this.#foods.reduce((acc, cur) => acc + cur.totalPrice(), 0);
+ }
+
+ totalTypeQuantity(foodType) {
+ return this.#foods
+ .filter((food) => food.type() === foodType)
+ .reduce((acc, cur) => acc + cur.quantity(), 0);
+ }
+
+ freebieMenu() {
+ if (this.totalFoodsPrice() >= DISCOUNT.freebieStandard) {
+ return DISCOUNT.freebieItem;
+ }
+ return BENEFIT_MESSEAGE.non;
+ }
+}
+
+export default Cashier; | JavaScript | import ์์๋ ๋ง์ถฐ์ฃผ๋ฉด ์ข์ ๊ฒ ๊ฐ์์! ์๋ฅผ ๋ค์ด constants๋ถ๋ถ๋ ์ํ๋ฒณ ์์ผ๋ก importํด์ค๋ค๊ฑฐ๋,,! |
@@ -0,0 +1,70 @@
+import Food from "./Food.js";
+import { ERROR_MENU } from "../constants/ErrorMesseage.js";
+import { merageFoodInfo } from "../constants/ViewRefiner.js";
+import { DISCOUNT, BENEFIT_MESSEAGE } from "../constants/BenefitStorage.js";
+import { NAME } from "../constants/FoodStorage.js";
+
+class Cashier {
+ #foods = [];
+
+ constructor(orderHistory) {
+ this.#enrollFoods(orderHistory);
+ this.#orderDuplicationValidator();
+ this.#onlyDrinkValidator();
+ this.#orderMaxValidator();
+ }
+
+ #enrollFoods(orderHistory) {
+ orderHistory
+ .split(',')
+ .forEach((food) => this.#foods.push(new Food(food)));
+ }
+
+ #orderDuplicationValidator() {
+ const foodNameList = this.#foods.map((food) => food.name());
+ if (foodNameList.length !== new Set(foodNameList).size) {
+ throw new Error(ERROR_MENU.basic);
+ }
+ }
+
+ #onlyDrinkValidator() {
+ const drinkTypeFoodsList = this.#foods.filter((food) => food.type() === NAME.drink);
+
+ if (drinkTypeFoodsList.length === this.#foods.length) {
+ throw new Error(ERROR_MENU.onlyDrink);
+ }
+ }
+
+ #orderMaxValidator() {
+ if (this.#totalFoodsQuantity() > ERROR_MENU.maxOrderNumber) {
+ throw new Error(`${ERROR_MENU.maxOrder}\n${ERROR_MENU.orderAmountExample}`);
+ }
+ }
+
+ #totalFoodsQuantity() {
+ return this.#foods.reduce((acc,cur) => acc + cur.quantity(), 0);
+ }
+
+ foodsListWithQuantity() {
+ return this.#foods.map((foodInfo) => merageFoodInfo(foodInfo.name(), foodInfo.quantity()));
+ }
+
+ totalFoodsPrice() {
+ return this.#foods.reduce((acc, cur) => acc + cur.totalPrice(), 0);
+ }
+
+ totalTypeQuantity(foodType) {
+ return this.#foods
+ .filter((food) => food.type() === foodType)
+ .reduce((acc, cur) => acc + cur.quantity(), 0);
+ }
+
+ freebieMenu() {
+ if (this.totalFoodsPrice() >= DISCOUNT.freebieStandard) {
+ return DISCOUNT.freebieItem;
+ }
+ return BENEFIT_MESSEAGE.non;
+ }
+}
+
+export default Cashier; | JavaScript | ํด๋น ๋ถ๋ถ๋ split๋ถ๋ถ์ด๋๊น ๋ฐ๋ก ์์๋ก ๋ง๋ค์ด์ค๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,62 @@
+package baseball;
+
+
+import java.util.List;
+
+public class GameUtil {
+
+ public void runGame() {
+
+ if (runOnlyOnceGame()) {
+ showMenu();
+ }
+ }
+
+ public boolean runOnlyOnceGame() {
+
+ Computer computer = new Computer();
+ List<Integer> computers = computer.makeNumbers();
+
+ return gameResult(computers);
+ }
+
+ private boolean gameResult(List<Integer> computers) {
+ boolean threeStrike = true;
+ while (threeStrike) {
+
+ Player player = new Player();
+ List<Integer> playerNumbers = player.makeNumbers();
+ Referee referee = new Referee();
+
+ String result = referee.informStrikeBall(computers, playerNumbers);
+ System.out.println(result);
+ threeStrike = isGame(threeStrike, result);
+ }
+
+ return true;
+ }
+
+ private boolean isGame(boolean game, String result) {
+ if (result.equals("3์คํธ๋ผ์ดํฌ")) {
+ System.out.println("3๊ฐ์ ์ซ์๋ฅผ ๋ชจ๋ ๋งํ์
จ์ต๋๋ค! ๊ฒ์ ์ข
๋ฃ");
+ game = false;
+ }
+ return game;
+ }
+
+ private void showMenu() {
+ ScannerUtil scannerUtil = new ScannerUtil();
+ System.out.println("๊ฒ์์ ์๋ก ์์ํ๋ ค๋ฉด 1, ์ข
๋ฃํ๋ ค๋ฉด 2๋ฅผ ์
๋ ฅํ์ธ์.");
+
+ int choice = scannerUtil.insertInt();
+ if (choice == 1) {
+ runGame();
+ }
+
+ while (choice != 1 && choice != 2) {
+ System.out.println("์๋ชป ์
๋ ฅํ์
จ์ต๋๋ค. ๋ค์์
๋ ฅํด์ฃผ์ธ์.");
+ choice = scannerUtil.insertInt();
+ }
+ System.out.println("์๊ณ ํ์
จ์ต๋๋ค.");
+ }
+} | Java | Util ์ด๋ผ๋ ์ด๋ฆ์ ํด๋์ค๋ ์ ํฉํ์ง ์์ ๊ฒ ๊ฐ์์ :)
`์ ํธ ํด๋์ค`๋ ๋ณดํต ์ค์ฉ์ฑ์ด๋ ์ ์ฉ์ฑ์ ์ค์ ์ ๋ ์ ์ ๋ฉ์๋๋ค์ ๋ชจ์๋๊ณ ์ฌ๋ฌ ๊ณณ์์ ํธ์ถํ๋ ํ์์ผ๋ก ์ฌ์ฉํ๋ฉฐ ์ฌ๋ฌ ๊ณณ์์ ํธ์ถํ๊ธฐ ๋๋ฌธ์ ์ ํธ ํด๋์ค๋ ๊ณตํต์ ์ธ ๋ก์ง์ ๋ค๋ฃจ๋๋ก ๋ง๋ค์ด์ผํ๋ฉฐ ํฉ๋๋ค.
๊ฐ์ธ์ ์ผ๋ก๋ ์ค์ ๋ก๋ ์ด๋ฌํ ์ ํธ ํด๋์ค์ ๋ฉ์๋๋ฅผ ๋ณ๊ฒฝํ์ ๋ ์ํฅ์ด ๋๋ฌด ํฌ๊ธฐ ๋๋ฌธ์ ์ต๋ํ ์ฌ์ฉ์ ์ง์ํ๋ ๊ฒ์ ์ ํธํฉ๋๋ค :) |
@@ -0,0 +1,62 @@
+package baseball;
+
+
+import java.util.List;
+
+public class GameUtil {
+
+ public void runGame() {
+
+ if (runOnlyOnceGame()) {
+ showMenu();
+ }
+ }
+
+ public boolean runOnlyOnceGame() {
+
+ Computer computer = new Computer();
+ List<Integer> computers = computer.makeNumbers();
+
+ return gameResult(computers);
+ }
+
+ private boolean gameResult(List<Integer> computers) {
+ boolean threeStrike = true;
+ while (threeStrike) {
+
+ Player player = new Player();
+ List<Integer> playerNumbers = player.makeNumbers();
+ Referee referee = new Referee();
+
+ String result = referee.informStrikeBall(computers, playerNumbers);
+ System.out.println(result);
+ threeStrike = isGame(threeStrike, result);
+ }
+
+ return true;
+ }
+
+ private boolean isGame(boolean game, String result) {
+ if (result.equals("3์คํธ๋ผ์ดํฌ")) {
+ System.out.println("3๊ฐ์ ์ซ์๋ฅผ ๋ชจ๋ ๋งํ์
จ์ต๋๋ค! ๊ฒ์ ์ข
๋ฃ");
+ game = false;
+ }
+ return game;
+ }
+
+ private void showMenu() {
+ ScannerUtil scannerUtil = new ScannerUtil();
+ System.out.println("๊ฒ์์ ์๋ก ์์ํ๋ ค๋ฉด 1, ์ข
๋ฃํ๋ ค๋ฉด 2๋ฅผ ์
๋ ฅํ์ธ์.");
+
+ int choice = scannerUtil.insertInt();
+ if (choice == 1) {
+ runGame();
+ }
+
+ while (choice != 1 && choice != 2) {
+ System.out.println("์๋ชป ์
๋ ฅํ์
จ์ต๋๋ค. ๋ค์์
๋ ฅํด์ฃผ์ธ์.");
+ choice = scannerUtil.insertInt();
+ }
+ System.out.println("์๊ณ ํ์
จ์ต๋๋ค.");
+ }
+} | Java | ํ์ผ ๋ง์ง๋ง์ ์ํฐ(๊ฐํ๋ฌธ์)๋ฅผ ๋ฃ์ด์ฃผ์ธ์ :)
์ด์ ๋ ๋ฆฌ๋ทฐ๋ฅผ ์งํํ ๋ ๊นํ๋ธ์์ ๊ฒฝ๊ณ ๋ฉ์์ง๋ฅผ ์ง์ฐ๊ณ ํน์ ๋ชจ๋ฅด๋ ํ์ผ ์ฝ๊ธฐ ์ค๋ฅ์ ๋๋นํ๊ธฐ ์ํจ์
๋๋ค.
์ข ๋ ์๊ณ ์ถ์ผ์๋ฉด ์ฐธ๊ณ ๋งํฌ๋ฅผ ๋ณด์
๋ ์ฌ๋ฐ์ ๊ฒ ๊ฐ๋ค์ :)
Intellij ๋ฅผ ์ฌ์ฉํ์ค ๊ฒฝ์ฐ์Preferences -> Editor -> General -> Ensure line feed at file end on save ๋ฅผ ์ฒดํฌํด์ฃผ์๋ฉดํ์ผ ์ ์ฅ ์ ๋ง์ง๋ง์ ๊ฐํ๋ฌธ์๋ฅผ ์๋์ผ๋ก ๋ฃ์ด์ค๋๋ค!
https://minz.dev/19https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline |
@@ -0,0 +1,62 @@
+package baseball;
+
+
+import java.util.List;
+
+public class GameUtil {
+
+ public void runGame() {
+
+ if (runOnlyOnceGame()) {
+ showMenu();
+ }
+ }
+
+ public boolean runOnlyOnceGame() {
+
+ Computer computer = new Computer();
+ List<Integer> computers = computer.makeNumbers();
+
+ return gameResult(computers);
+ }
+
+ private boolean gameResult(List<Integer> computers) {
+ boolean threeStrike = true;
+ while (threeStrike) {
+
+ Player player = new Player();
+ List<Integer> playerNumbers = player.makeNumbers();
+ Referee referee = new Referee();
+
+ String result = referee.informStrikeBall(computers, playerNumbers);
+ System.out.println(result);
+ threeStrike = isGame(threeStrike, result);
+ }
+
+ return true;
+ }
+
+ private boolean isGame(boolean game, String result) {
+ if (result.equals("3์คํธ๋ผ์ดํฌ")) {
+ System.out.println("3๊ฐ์ ์ซ์๋ฅผ ๋ชจ๋ ๋งํ์
จ์ต๋๋ค! ๊ฒ์ ์ข
๋ฃ");
+ game = false;
+ }
+ return game;
+ }
+
+ private void showMenu() {
+ ScannerUtil scannerUtil = new ScannerUtil();
+ System.out.println("๊ฒ์์ ์๋ก ์์ํ๋ ค๋ฉด 1, ์ข
๋ฃํ๋ ค๋ฉด 2๋ฅผ ์
๋ ฅํ์ธ์.");
+
+ int choice = scannerUtil.insertInt();
+ if (choice == 1) {
+ runGame();
+ }
+
+ while (choice != 1 && choice != 2) {
+ System.out.println("์๋ชป ์
๋ ฅํ์
จ์ต๋๋ค. ๋ค์์
๋ ฅํด์ฃผ์ธ์.");
+ choice = scannerUtil.insertInt();
+ }
+ System.out.println("์๊ณ ํ์
จ์ต๋๋ค.");
+ }
+} | Java | - ํต์ฌ ๋ก์ง์ ๊ตฌํํ๋ ์ฝ๋์ UI๋ฅผ ๋ด๋นํ๋ ๋ก์ง์ ๊ตฌ๋ถํ๋ค.
- UI ๋ก์ง์ InputView, ResultView์ ๊ฐ์ ํด๋์ค๋ฅผ ์ถ๊ฐํด ๋ถ๋ฆฌํ๋ค.
ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ์ฌํญ ์ค์ ์์ ๊ฐ์ ์๊ตฌ์ฌํญ์ด ์์๋๋ฐ์. ์ด ๋ถ๋ถ์ ์ด๋ป๊ฒ ๊ตฌํํ ์ง ์๊ฐํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,61 @@
+package baseball;
+
+import java.util.List;
+
+public class Referee {
+
+ public int countBall(List<Integer> list1, List<Integer> list2) {
+
+ int count = 0;
+ int strike = countStrike(list1, list2);
+
+ for (Integer number : list1) {
+ count = getCount(list2, count, number);
+ }
+ return count - strike;
+ }
+
+ public int countStrike(List<Integer> list1, List<Integer> list2) {
+
+ int count = 0;
+ for (int index = 0; index < 3; index++) {
+ count = getCountStrike(list1, list2, count, index);
+ }
+ return count;
+ }
+
+ public String informStrikeBall(List<Integer> list1, List<Integer> list2) {
+
+ int ball = countBall(list1, list2);
+ int strike = countStrike(list1, list2);
+
+ if (strike == 3) {
+ return strike + "์คํธ๋ผ์ดํฌ";
+ }
+
+ if (strike == 0 && ball != 0) {
+ return ball + "๋ณผ";
+ }
+
+ if (strike != 0 && ball != 0) {
+ return ball + "๋ณผ " + strike + "์คํธ๋ผ์ดํฌ";
+ }
+
+ return "๋ซ์ฑ";
+ }
+
+ private int getCountStrike(List<Integer> list1, List<Integer> list2, int count, int index) {
+ if (list1.get(index) == list2.get(index)) {
+ count++;
+ }
+ return count;
+ }
+
+ private int getCount(List<Integer> list2, int count, Integer number) {
+ if (list2.contains(number)) {
+ count++;
+ }
+ return count;
+ }
+
+} | Java | Referee ํด๋์ค์ ์ธ์คํด์ค๋ ์ด๋ค ์ํ(๋ด๋ถ ํ๋)๋ ๊ฐ์ง์ง ์๊ธฐ ๋๋ฌธ์ ์ด ํด๋์ค์ ๋ชจ๋ ๊ฐ์ฒด๋ค์ ๋์ผํ ๊ฐ์ฒด์ด๊ฒ ๋ค์ !
๋๋ฌด ๋ง์ ์ํ๋ฅผ ๊ฐ์ง๋ ๊ฒ์ ์ข์ ์ค๊ณ๊ฐ ์๋์ง๋ง, ์๋ฌด๊ฒ๋ ๊ฐ์ง์ง ์๋ ๋ฐฉ์ ๋ํ ๋ฐ๋์งํ ์ค๊ณ๋ ์๋๋๋ค.
์ ์ ํ ์ํ๋ฅผ ๊ฐ์ง ๊ฐ์ฒด๋ฅผ ์ค๊ณํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์.
๋ค๋ฅธ ํด๋์ค๋ค์๋ ํจ๊ป ์ ์ฉ๋๋ ํผ๋๋ฐฑ์ผ ๊ฒ ๊ฐ์ต๋๋ค :) |
@@ -0,0 +1,61 @@
+package baseball;
+
+import java.util.List;
+
+public class Referee {
+
+ public int countBall(List<Integer> list1, List<Integer> list2) {
+
+ int count = 0;
+ int strike = countStrike(list1, list2);
+
+ for (Integer number : list1) {
+ count = getCount(list2, count, number);
+ }
+ return count - strike;
+ }
+
+ public int countStrike(List<Integer> list1, List<Integer> list2) {
+
+ int count = 0;
+ for (int index = 0; index < 3; index++) {
+ count = getCountStrike(list1, list2, count, index);
+ }
+ return count;
+ }
+
+ public String informStrikeBall(List<Integer> list1, List<Integer> list2) {
+
+ int ball = countBall(list1, list2);
+ int strike = countStrike(list1, list2);
+
+ if (strike == 3) {
+ return strike + "์คํธ๋ผ์ดํฌ";
+ }
+
+ if (strike == 0 && ball != 0) {
+ return ball + "๋ณผ";
+ }
+
+ if (strike != 0 && ball != 0) {
+ return ball + "๋ณผ " + strike + "์คํธ๋ผ์ดํฌ";
+ }
+
+ return "๋ซ์ฑ";
+ }
+
+ private int getCountStrike(List<Integer> list1, List<Integer> list2, int count, int index) {
+ if (list1.get(index) == list2.get(index)) {
+ count++;
+ }
+ return count;
+ }
+
+ private int getCount(List<Integer> list2, int count, Integer number) {
+ if (list2.contains(number)) {
+ count++;
+ }
+ return count;
+ }
+
+} | Java | ์์์ ์ซ์๋ฅผ ์ฌ์ฉํ๋ ๋งค์ง๋๋ฒ๋์์ค ์ฝ๋๋ฅผ ์ฝ๊ธฐ ์ด๋ ต๊ฒ ๋ง๋๋๋ฐ์ !
์ด ๋ถ๋ถ์ ์ด๋ป๊ฒ ๊ฐ์ ํ ์ ์์๊น์? ๊ด๋ จ ๋งํฌ ๋จ๊ฒจ๋๊ฒ ์ต๋๋ค :)
https://www.slipp.net/questions/356 |
@@ -0,0 +1,60 @@
+package baseball;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class RefereeTest {
+
+ @Test
+ @DisplayName("๋ ๊ฐ์ ๋ฆฌ์คํธ์์ ๋ช ๊ฐ์ ์ซ์๊ฐ ๊ฐ์์ง ํ์ธ")
+ void countBall() throws Exception {
+
+ //given
+ Referee referee = new Referee();
+ List<Integer> list1 = Arrays.asList(1, 2, 3);
+ List<Integer> list2 = Arrays.asList(3, 1, 2);
+
+ //when
+ int ball = referee.countBall(list1, list2);
+
+ //then
+ assertThat(ball).isEqualTo(3);
+ }
+
+ @Test
+ @DisplayName("๋ ๊ฐ์ ๋ฆฌ์คํธ์์ ๊ฐ์ ์๋ฆฌ์ ๊ฐ์ ์ซ์๊ฐ ๋ช ๊ฐ ์๋์ง ํ์ธ")
+ void countStrike() throws Exception {
+
+ //given
+ Referee referee = new Referee();
+ List<Integer> list1 = Arrays.asList(1, 2, 3);
+ List<Integer> list2 = Arrays.asList(1, 2, 3);
+
+ //when
+ int strike = referee.countStrike(list1, list2);
+
+ //then
+ assertThat(strike).isEqualTo(3);
+ }
+
+ @Test
+ @DisplayName("๋ณผ, ์คํธ๋ผ์ดํฌ ๊ฐฏ์ ์๋ ค์ฃผ๊ธฐ")
+ void ballStrikeCount() throws Exception {
+
+ //given
+ Referee referee = new Referee();
+ List<Integer> list1 = Arrays.asList(1, 2, 3);
+ List<Integer> list2 = Arrays.asList(1, 2, 3);
+
+ //when
+ String inform = referee.informStrikeBall(list1, list2);
+
+ //then
+ assertThat(inform).isEqualTo("3์คํธ๋ผ์ดํฌ");
+ }
+} | Java | ํด๋น ํ
์คํธ ๋ฉ์๋์์ ์์ธ๋ฅผ ์ ์ธ์ ํ๋ ํน๋ณํ ์ด์ ๊ฐ ์๋ค๋ฉด ์ง์์ฃผ์ธ์ :)
ํ
์คํธ ๋ฉ์๋์ ์์ธ ์ ์ธ์ ํจ์ผ๋ก ์จ`ํด๋น ํ
์คํธ๊ฐ ์คํจํ ์๋ ์๋ค`๋ผ๋ ๋๋์ ์ฃผ๋ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,61 @@
+package baseball;
+
+import java.util.List;
+
+public class Referee {
+
+ public int countBall(List<Integer> list1, List<Integer> list2) {
+
+ int count = 0;
+ int strike = countStrike(list1, list2);
+
+ for (Integer number : list1) {
+ count = getCount(list2, count, number);
+ }
+ return count - strike;
+ }
+
+ public int countStrike(List<Integer> list1, List<Integer> list2) {
+
+ int count = 0;
+ for (int index = 0; index < 3; index++) {
+ count = getCountStrike(list1, list2, count, index);
+ }
+ return count;
+ }
+
+ public String informStrikeBall(List<Integer> list1, List<Integer> list2) {
+
+ int ball = countBall(list1, list2);
+ int strike = countStrike(list1, list2);
+
+ if (strike == 3) {
+ return strike + "์คํธ๋ผ์ดํฌ";
+ }
+
+ if (strike == 0 && ball != 0) {
+ return ball + "๋ณผ";
+ }
+
+ if (strike != 0 && ball != 0) {
+ return ball + "๋ณผ " + strike + "์คํธ๋ผ์ดํฌ";
+ }
+
+ return "๋ซ์ฑ";
+ }
+
+ private int getCountStrike(List<Integer> list1, List<Integer> list2, int count, int index) {
+ if (list1.get(index) == list2.get(index)) {
+ count++;
+ }
+ return count;
+ }
+
+ private int getCount(List<Integer> list2, int count, Integer number) {
+ if (list2.contains(number)) {
+ count++;
+ }
+ return count;
+ }
+
+} | Java | ํต์ฌ ๋ก์ง์ ํ๋ฉด(์ฝ์ ์ถ๋ ฅ)์ ๋ํ ์๊ตฌ์ฌํญ์ด ์์ฌ์๋ค์!
๋ง์ฝ ํ๋ฉด ์๊ตฌ์ฌํญ์ด ๋ณํ๊ฒ ๋๋ค๋ฉด ์ด๋ป๊ฒ ๋ ๊น์?
ํ๋ฉด์ ์๊ตฌ์ฌํญ์ด ๋ฐ๋์์ง๋ง ๊ฐ๋ฐ์๊ฐ ์์ ํด์ผํ๋ ๋ถ๋ถ์ ๋๋ฉ์ธ์ ํต์ฌ ๋ก์ง์
๋๋ค.
์๋ ์๊ตฌ์ฌํญ์ ๋ฐ์ํด์ผํ๋ ์ด์ ์ค ํ๋๊ฐ ๋ ์ ์๊ฒ ๋ค์ :)
> ํต์ฌ ๋ก์ง์ ๊ตฌํํ๋ ์ฝ๋์ UI๋ฅผ ๋ด๋นํ๋ ๋ก์ง์ ๊ตฌ๋ถํ๋ค. |
@@ -0,0 +1,56 @@
+package baseball;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Scanner;
+
+public class ScannerUtil {
+
+ public List<Integer> makeScannerNumbers() {
+ System.out.print("์ซ์๋ฅผ ์
๋ ฅํ์ธ์: ");
+
+ ListUtil listUtil = new ListUtil();
+
+ List<Integer> integers = stringListToIntegerList();
+
+ while (!listUtil.checkListSize(integers)) {
+ System.out.println("์๋ชป๋ ์ซ์์
๋๋ค.");
+ System.out.print("์ซ์๋ฅผ ์
๋ ฅํ์ธ์: ");
+
+ integers = stringListToIntegerList();
+ }
+ return integers;
+ }
+
+ public String insertString() {
+
+ Scanner scanner = new Scanner(System.in);
+ return scanner.next();
+ }
+
+ public int insertInt() {
+
+ Scanner scanner = new Scanner(System.in);
+ return scanner.nextInt();
+ }
+
+ private String[] splitStringList() {
+ String string = insertString();
+
+ return string.split("");
+ }
+
+ private List<Integer> stringListToIntegerList() {
+ String[] strings = splitStringList();
+
+ List<Integer> temp = new ArrayList<>();
+ ListUtil listUtil = new ListUtil();
+
+ for (String string : strings) {
+ int number = Integer.parseInt(string);
+ listUtil.distinctNumberAdd(temp, number);
+ }
+
+ return temp;
+ }
+} | Java | Scanner๋ ๋งค์ฐ ๋น์ผ ์์์
๋๋ค. ๋ฏธ๋ฆฌ ์์ฑํด๋๊ณ ์ฌํ์ฉํ๋๋ก ํด๋ณด๋ฉด ์ด๋จ๊น์ ?
static ๋ฉ์๋๋ฅผ ํ์ฉํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,62 @@
+import InputView from '../view/InputView.js';
+import Benefit from './Benefit.js';
+import Giveaway from './Giveaway.js';
+import OrderAmount from './OrderAmount.js';
+import OrderManager from './OrderManager.js';
+
+class OrderProcessor {
+ static async processOrder() {
+ const { visitDate, orderMenus } = await this.#inputOrder();
+ const orderManager = this.#createOrderManager(visitDate, orderMenus);
+ const orderAmount = this.#createOrderAmount(visitDate, orderManager);
+ const giveaway = this.#createGiveaway(orderAmount);
+ const benefit = this.#createBenefit(visitDate, orderManager, orderAmount, giveaway);
+ return { visitDate, orderMenus, orderManager, orderAmount, giveaway, benefit };
+ }
+
+ static async #inputOrder() {
+ return {
+ visitDate: await InputView.readVisitDate(),
+ orderMenus: await InputView.readOrderMenu(),
+ };
+ }
+
+ static #createOrderManager(visitDate, orderMenus) {
+ const orderManager = new OrderManager(visitDate, orderMenus);
+ const menusInfo = orderManager.getMenusInfo();
+ const dayOfWeek = orderManager.getDayOfWeek();
+ return { menusInfo, dayOfWeek };
+ }
+
+ static #createOrderAmount(visitDate, orderManager) {
+ const { menusInfo, dayOfWeek } = orderManager;
+ const beforeDiscount = OrderAmount.calculateAmountBeforeDiscount(menusInfo);
+ const totalDiscount = OrderAmount.calculateDiscountTotalAmount(visitDate, menusInfo, dayOfWeek);
+ const afterDiscount = OrderAmount.calculateOrderAmountAfterDiscount(
+ beforeDiscount,
+ totalDiscount,
+ );
+ return { beforeDiscount, totalDiscount, afterDiscount };
+ }
+
+ static #createGiveaway(orderAmount) {
+ const { beforeDiscount } = orderAmount;
+ const isAddGiveaway = Giveaway.checkAddGiveaway(beforeDiscount);
+ const giveawayDiscount = Giveaway.calculateGiveawyDiscountAmount(beforeDiscount);
+ return { isAddGiveaway, giveawayDiscount };
+ }
+
+ static #createBenefit(visitDate, orderManager, orderAmount, giveaway) {
+ const { menusInfo, dayOfWeek } = orderManager;
+ const { totalDiscount } = orderAmount;
+ const { giveawayDiscount } = giveaway;
+ const dDay = Benefit.calculateDDayDiscount(visitDate);
+ const weekDay = Benefit.calculateWeekDayDiscount(menusInfo, dayOfWeek);
+ const weekEnd = Benefit.calculateWeekEndDiscount(menusInfo, dayOfWeek);
+ const special = Benefit.calculateSpecialDiscount(visitDate, dayOfWeek);
+ const eventBadge = Benefit.getEventBadge(totalDiscount + giveawayDiscount);
+ return { dDay, weekDay, weekEnd, special, eventBadge };
+ }
+}
+
+export default OrderProcessor; | JavaScript | ํด๋น ํด๋์ค์ ํจ์๋ค์์ static์ ๋ชจ๋ ๋ถ์ด์
จ๋๋ฐ, static์ ์ ๋ถ์ฌ๋ ๊ด์ฐฎ์ง ์์๊น์?
๋ชจ๋ static์ ๋ถ์ด์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,24 @@
+import Benefit from './Benefit.js';
+
+class OrderAmount {
+ static calculateAmountBeforeDiscount(menusInfo) {
+ return menusInfo.reduce((acc, cur) => acc + cur[1] * cur[2], 0);
+ }
+
+ static calculateDiscountTotalAmount(visitDate, orderMenusInfo, dayOfWeek) {
+ return [
+ Benefit.calculateDDayDiscount(visitDate),
+ Benefit.calculateWeekDayDiscount(orderMenusInfo, dayOfWeek),
+ Benefit.calculateWeekEndDiscount(orderMenusInfo, dayOfWeek),
+ Benefit.calculateSpecialDiscount(visitDate, dayOfWeek),
+ ]
+ .filter(benefitDiscount => benefitDiscount !== false)
+ .reduce((acc, cur) => acc + cur, 0);
+ }
+
+ static calculateOrderAmountAfterDiscount(orderAmountBeforeDiscount, discountTotalAmount) {
+ return orderAmountBeforeDiscount + discountTotalAmount;
+ }
+}
+
+export default OrderAmount; | JavaScript | ๊ณ ์ฐจํจ์๋ฅผ ๊ต์ฅํ ์ ์ฐ์๋ค์! ํ์ฉ๋ฒ์ ๋ง์ด ๋ฐฐ์๊ฐ๋๋ค. ๐ |
@@ -0,0 +1,34 @@
+import OutputView from '../view/OutputView.js';
+import OrderProcessor from '../domains/OrderProcessor.js';
+
+class ChristmasEventController {
+ static async start() {
+ OutputView.printGreeting();
+ const processedOrder = await OrderProcessor.processOrder();
+ this.#printOutput(processedOrder);
+ }
+
+ static #printOutput(processedOrder) {
+ const { visitDate, orderMenus, orderAmount, giveaway, benefit } = processedOrder;
+ OutputView.printNotification(visitDate);
+ OutputView.printOrderMenu(orderMenus);
+ OutputView.printOrderAmountBeforeDiscount(orderAmount.beforeDiscount);
+ OutputView.printGiveawayMenu(giveaway.isAddGiveaway);
+ this.#printBenefitHistory(benefit, giveaway);
+ OutputView.printBenefitTotalAmount(orderAmount.totalDiscount, giveaway.giveawayDiscount);
+ OutputView.printOrderAmountAfterDiscount(orderAmount.afterDiscount);
+ OutputView.printEventBadge(benefit.eventBadge);
+ }
+
+ static #printBenefitHistory(benefit, giveaway) {
+ OutputView.printBenefitHistory(
+ benefit.dDay,
+ benefit.weekDay,
+ benefit.weekEnd,
+ benefit.special,
+ giveaway.giveawayDiscount,
+ );
+ }
+}
+
+export default ChristmasEventController; | JavaScript | controller์ OrderProcessor๋ฅผ ๋ถ๋ฆฌํ์ ๊ฒ ์ธ์ ๊น์์ต๋๋ค.
์ ๋ ์ด ๋ถ๋ถ์ ๋ง์ด ๊ณ ๋ฏผํ๋๋ฐ์.
์ด๋ฒ ๋ฏธ์
๊ฐ์ ์์ ์ฝ๋์์ ๋๋ฌด ๋๋๋ฉด ์คํ๋ ค ๊ฐ๋
์ฑ์ด ์์ข์์ง๊น ํ์ฌ controller์ OrderProcessor ๊ฐ์ ํจ์๋ค์ ๋ชจ๋ ๋ฃ์๋๋ฐ, ์ ๊ฐ ์๋ชป ์๊ฐํ๊ฑด ์๋๊ฐ ์ถ์ ์ฝ๋์์ต๋๋ค.
controller์ ์ญํ ์ ๋ํ ๊ณ ๋ฏผ์ด ๋ง๋ค์..!
๋ณํ๋๊ป์๋ controller์ ์ญํ ์ Processor๋ฅผ ์คํํ๊ณ , ํด๋น ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ view๋ก ๋ณด๋ด๋ ์ค๊ฐ์๋ก ๋ณด์ ๊ฒ ๋ง์๊น์? |
@@ -0,0 +1,48 @@
+import CONSTANTS from '../constants/constants.js';
+import MENU from '../constants/menu.js';
+import OrderMenuValidator from '../validators/OrderMenuValidator.js';
+import VisitDateValidator from '../validators/VisitDateValidator.js';
+
+class OrderManager {
+ #visitDate;
+
+ #orderMenus;
+
+ constructor(visitDate, orderMenus) {
+ this.#validate(visitDate, orderMenus);
+ this.#visitDate = visitDate;
+ this.#orderMenus = orderMenus;
+ }
+
+ #validate(visitDate, orderMenus) {
+ VisitDateValidator.validateVisitDate(visitDate);
+ OrderMenuValidator.validateOrderMenu(orderMenus);
+ }
+
+ getMenusInfo() {
+ const menuNames = this.#setMenuNames();
+ const menuPrices = this.#setMenuPrices(menuNames);
+ const menuCount = this.#setMenuCount();
+ return menuNames.map((element, index) => [element, menuPrices[index], menuCount[index]]);
+ }
+
+ getDayOfWeek() {
+ return new Date(new Date().getFullYear(), CONSTANTS.month.december, this.#visitDate).getDay();
+ }
+
+ #setMenuNames() {
+ return this.#orderMenus.map(orderMenu =>
+ Object.keys(MENU.menuName).find(key => MENU.menuName[key] === orderMenu[0]),
+ );
+ }
+
+ #setMenuPrices(menuNames) {
+ return menuNames.map(menuName => MENU.menu[MENU.menuName[menuName]].price);
+ }
+
+ #setMenuCount() {
+ return this.#orderMenus.map(orderMenu => Number(orderMenu[1]));
+ }
+}
+
+export default OrderManager; | JavaScript | getํจ์์์ ๋จ์ํ get๋ง ํ๋ฉด ์ด๋จ์ง์?
OrderProcessor์์ getํจ์๋ฅผ ํ ๋๋ง๋ค setํจ์๋ค์ ์คํํ๋ ๋นํจ์จ์ ์ด๋ผ ์๊ฐํ์ต๋๋ค. ๋ฌผ๋ก ์ง๊ธ์ ํ๋ฒ๋ง ์คํ์ด ๋์ง๋ง์!
return๋ฌธ์์ ๋ฐ์ดํฐ ๊ฐ๊ณต์ ๋ชจ๋ ํ ํ Processor๋ก ๋ณด๋ด๋ ๊ฒ์ 3์ฃผ์ฐจ ํผ๋๋ฐฑ์์ ๋์จ "getter๋ฅผ getter๋ก๋ง" ๋ด์ฉ์ ๋ง์ด ๊ณ ๋ฏผํ์ ๊ฒ ๊ฐ์ ์ธ์๊น์์ต๋๋ค! |
@@ -0,0 +1,151 @@
+import ERROR from '../../src/constants/error.js';
+import OrderMenuValidator from '../../src/validators/OrderMenuValidator.js';
+
+describe('์ฃผ๋ฌธ ๋ฉ๋ด์ ๊ฐ์ ์
๋ ฅ ์์ธ ์ํฉ ํ
์คํธ', () => {
+ test('๋ฉ๋ดํ์ ์๋ ๋ฉ๋ด์ธ ๊ฒฝ์ฐ undefined๋ฅผ ๋ฐํํ๋ค.', () => {
+ const value = [
+ ['์์ก์ด์ํ', '1'],
+ ['ํํ์ค', '2'],
+ ['๋ฐ๋นํ๋ฆฝ', '3'],
+ ];
+ const result = OrderMenuValidator.validateNoMenu(value);
+ expect(result).toBeUndefined();
+ });
+
+ test('๋ฉ๋ดํ์ ์๋ ๋ฉ๋ด๊ฐ ์๋ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ๋ฅผ ํ๋ค.', () => {
+ const value = [
+ ['ํผ์', '1'],
+ ['ํฌ๋ฆผํ์คํ', '2'],
+ ['๋ฐ๋ฆฌ๋ฒ๊ฑฐ', '3'],
+ ];
+ const result = () => {
+ OrderMenuValidator.validateNoMenu(value);
+ };
+ expect(result).toThrow(ERROR.menu.invalidOrder);
+ });
+
+ test('์ฃผ๋ฌธ ๋ฉ๋ด์ ๊ฐ์๊ฐ 1์ด์์ธ ๊ฒฝ์ฐ undefined๋ฅผ ๋ฐํํ๋ค.', () => {
+ const value = [
+ ['์์ก์ด์ํ', '1'],
+ ['ํํ์ค', '2'],
+ ['๋ฐ๋นํ๋ฆฝ', '3'],
+ ];
+ const result = OrderMenuValidator.validateOrderMenuQuantity(value);
+ expect(result).toBeUndefined();
+ });
+
+ test('์ฃผ๋ฌธ ๋ฉ๋ด์ ๊ฐ์๊ฐ 1๋ฏธ๋ง์ธ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ๋ฅผ ํ๋ค.', () => {
+ // given
+ const value = [
+ ['์์ก์ด์ํ', '1'],
+ ['ํํ์ค', '0'],
+ ['๋ฐ๋นํ๋ฆฝ', '3'],
+ ];
+ const result = () => {
+ OrderMenuValidator.validateOrderMenuQuantity(value);
+ };
+ expect(result).toThrow(ERROR.menu.invalidOrder);
+ });
+
+ test('ํ์์ด ์์์ ๊ฐ์ ๊ฒฝ์ฐ undefined๋ฅผ ๋ฐํํ๋ค.', () => {
+ const value = [
+ ['์์ก์ด์ํ', '1'],
+ ['ํํ์ค', '2'],
+ ['๋ฐ๋นํ๋ฆฝ', '3'],
+ ];
+ const result = OrderMenuValidator.validateFormat(value);
+ expect(result).toBeUndefined();
+ });
+
+ test.each([
+ [
+ ['ํด์ฐ๋ฌผํ์คํ', '2'],
+ ['๋ ๋์์ธ', '1์ด์ฝ์ผ์ดํฌ', '1'],
+ ],
+ [['ํด์ฐ๋ฌผํ์คํ', '2'], ['๋ ๋์์ธ', '1'], ['์ด์ฝ์ผ์ดํฌ', '1'], ['']],
+ [['ํด์ฐ๋ฌผํ์คํ2'], ['๋ ๋์์ธ', '1์ด์ฝ์ผ์ดํฌ', '1'], ['']],
+ ])('ํ์์ด ์์์ ๋ค๋ฅธ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ๋ฅผ ํ๋ค.', input => {
+ const result = () => OrderMenuValidator.validateFormat(input);
+ expect(result).toThrow(ERROR.menu.invalidOrder);
+ });
+
+ test('์ค๋ณต ๋ฉ๋ด๊ฐ ์์ ๊ฒฝ์ฐ undefined๋ฅผ ๋ฐํํ๋ค.', () => {
+ const value = [
+ ['ํด์ฐ๋ฌผํ์คํ', '1'],
+ ['ํํ์ค', '2'],
+ ['๋ฐ๋นํ๋ฆฝ', '3'],
+ ];
+ const result = OrderMenuValidator.validateDuplicatedMenu(value);
+ expect(result).toBeUndefined();
+ });
+
+ test('์ค๋ณต ๋ฉ๋ด๋ฅผ ์
๋ ฅํ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ๋ฅผ ํ๋ค.', () => {
+ const value = [
+ ['ํด์ฐ๋ฌผํ์คํ', '1'],
+ ['ํํ์ค', '2'],
+ ['ํด์ฐ๋ฌผํ์คํ', '3'],
+ ];
+ const result = () => OrderMenuValidator.validateDuplicatedMenu(value);
+ expect(result).toThrow(ERROR.menu.invalidOrder);
+ });
+
+ test('๋ฉ๋ด๊ฐ ๋ชจ๋ ์๋ฃ๊ฐ ์๋ ๊ฒฝ์ฐ undefined๋ฅผ ๋ฐํํ๋ค.', () => {
+ const value = [
+ ['ํด์ฐ๋ฌผํ์คํ', '1'],
+ ['ํํ์ค', '2'],
+ ['๋ฐ๋นํ๋ฆฝ', '3'],
+ ];
+ const result = OrderMenuValidator.validateAllBeverage(value);
+ expect(result).toBeUndefined();
+ });
+
+ test('๋ฉ๋ด๊ฐ ๋ชจ๋ ์๋ฃ์ผ ๊ฒฝ์ฐ ์์ธ์ฒ๋ฆฌ๋ฅผ ํ๋ค.', () => {
+ const value = [
+ ['ํด์ฐ๋ฌผํ์คํ', '1'],
+ ['ํํ์ค', '2'],
+ ['ํด์ฐ๋ฌผํ์คํ', '3'],
+ ];
+ const result = () => OrderMenuValidator.validateDuplicatedMenu(value);
+ expect(result).toThrow(ERROR.menu.invalidOrder);
+ });
+
+ test('๋ฉ๋ด์ ์ด ๊ฐ์๊ฐ 20์ ๋์ง ์์ ๊ฒฝ์ฐ undefined๋ฅผ ๋ฐํํ๋ค.', () => {
+ const value = [
+ ['ํด์ฐ๋ฌผํ์คํ', '1'],
+ ['ํํ์ค', '2'],
+ ['๋ฐ๋นํ๋ฆฝ', '3'],
+ ];
+ const result = OrderMenuValidator.validateMenuCountAboveThreshold(value);
+ expect(result).toBeUndefined();
+ });
+
+ test('๋ฉ๋ด์ ์ด ๊ฐ์๊ฐ 20์ ๋์ ๊ฒฝ์ฐ ์์ธ์ฒ๋ฆฌ๋ฅผ ํ๋ค.', () => {
+ const value = [
+ ['ํด์ฐ๋ฌผํ์คํ', '18'],
+ ['ํํ์ค', '2'],
+ ['๋ฐ๋นํ๋ฆฝ', '3'],
+ ];
+ const result = () => OrderMenuValidator.validateMenuCountAboveThreshold(value);
+ expect(result).toThrow(ERROR.menu.invalidOrder);
+ });
+
+ test('๋ฉ๋ด์ ๊ฐ์๊ฐ ์ซ์์ธ ๊ฒฝ์ฐ undefined๋ฅผ ๋ฐํํ๋ค.', () => {
+ const value = [
+ ['ํด์ฐ๋ฌผํ์คํ', '1'],
+ ['ํํ์ค', '2'],
+ ['๋ฐ๋นํ๋ฆฝ', '3'],
+ ];
+ const result = OrderMenuValidator.validateNaNCount(value);
+ expect(result).toBeUndefined();
+ });
+
+ test('๋ฉ๋ด์ ๊ฐ์๊ฐ ์ซ์๊ฐ ์๋ ๊ฒฝ์ฐ ์์ธ์ฒ๋ฆฌ๋ฅผ ํ๋ค.', () => {
+ const value = [
+ ['ํด์ฐ๋ฌผํ์คํ', '18'],
+ ['ํํ์ค', 'a'],
+ ['๋ฐ๋นํ๋ฆฝ', '3'],
+ ];
+ const result = () => OrderMenuValidator.validateNaNCount(value);
+ expect(result).toThrow(ERROR.menu.invalidOrder);
+ });
+}); | JavaScript | validator๋ค์ testํ ์๊ฐ์ ํ์ง ๋ชปํ์๋๋ฐ, ๊ต์ฅํ ๊ผผ๊ผผํ ํ
์คํธ์ฝ๋ ์์ฑํ์ ๊ฒ ๊ฐ๋ค์.
ํ๋ ๋ ๋ฐฐ์๊ฐ๋๋ค~! |
@@ -0,0 +1,31 @@
+import { Console } from '@woowacourse/mission-utils';
+import MESSAGE from '../constants/message.js';
+import VisitDateValidator from '../validators/VisitDateValidator.js';
+import OrderMenuValidator from '../validators/OrderMenuValidator.js';
+import reTry from '../utils/reTry.js';
+
+const InputView = {
+ async readVisitDate() {
+ return reTry(async () => {
+ const input = await Console.readLineAsync(MESSAGE.read.visitDate);
+ const visitDate = Number(input);
+ VisitDateValidator.validateVisitDate(visitDate);
+ return Number(visitDate);
+ });
+ },
+
+ async readOrderMenu() {
+ return reTry(async () => {
+ const input = await Console.readLineAsync(MESSAGE.read.orderMenu);
+ const orderMenus = input
+ .split(',')
+ .map(menu => menu.trim())
+ .map(menu => menu.split('-'))
+ .map(menuAndCount => menuAndCount.map(str => str.trim()));
+ OrderMenuValidator.validateOrderMenu(orderMenus);
+ return orderMenus;
+ });
+ },
+};
+
+export default InputView; | JavaScript | ์ด๊ฒ๋ ํ๋ฒ ํจ๊ป ๊ณ ๋ฏผํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์๋ฐ, ์ ๋ InputView์์๋ ์ ๋ง input๋ง return์ ํ๊ณ ๋ค๋ฅธ ์ ์ฒ๋ฆฌ๋ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ํ์ผ์์ ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์๋๋ฐ์.
ํน์ ์ด ๋ถ๋ถ์ ์ด๋ป๊ฒ ์๊ฐํ์๋์ง ๊ถ๊ธํฉ๋๋ค.
๋ฐ์ดํฐ ๊ด๋ จ ๋ก์ง์ด ์๋ input ๋ฐ์ ๋ฐ์ดํฐ์ ์ ์ฒ๋ฆฌ๋ inputView์์ ์ฒ๋ฆฌํ๊ณ ์ ํ์ ๊ฑด๊ฐ์?! |
@@ -0,0 +1,28 @@
+package racingcar.domain;
+
+import racingcar.constants.ErrorMsg;
+
+public class CarName {
+ private static final int CAR_NAME_LENGTH_MIN = 1;
+ private static final int CAR_NAME_LENGTH_MAX = 5;
+ private final String name;
+
+ public CarName(String name) {
+ validateLength(name);
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ private void validateLength(String name) {
+ if (name.length() < CAR_NAME_LENGTH_MIN) {
+ throw new IllegalArgumentException(ErrorMsg.CAR_NAME_LENGTH_MIN_ERROR.getMsg());
+ }
+
+ if (name.length() > CAR_NAME_LENGTH_MAX) {
+ throw new IllegalArgumentException(ErrorMsg.CAR_NAME_LENGTH_MAX_ERROR.getMsg());
+ }
+ }
+} | Java | ์์๊ฐ ํฌ์ฅ ๐ |
@@ -0,0 +1,30 @@
+package racingcar.domain;
+
+public class RacingCar {
+ private final CarName carName;
+ private int moveCount;
+
+ public RacingCar(String name) {
+ this.carName = new CarName(name);
+ moveCount = 0;
+ }
+
+ public int getMoveCount() {
+ return moveCount;
+ }
+
+ public String getCarName() {
+ return carName.getName();
+ }
+
+ public void attemptToMove(GameRule gameRule) {
+ if (gameRule.canMove()) {
+ move();
+ }
+ }
+
+ private void move() {
+ moveCount++;
+ }
+
+} | Java | ๋๋ค ์ซ์์ ์ํด ์๋์ฐจ์ ์ด๋ ํ๋จ์ญํ ์ด
RacinCar์ ์๋๊ฒ ๋ง๋์ง ํ๋ฒ ์๊ฐํด ๋ณด์๋ฉด ์ข์๊ฑฐ ๊ฐ์์
๐ข |
@@ -0,0 +1,45 @@
+package racingcar.controller;
+
+import racingcar.domain.*;
+import racingcar.view.UIManager;
+
+public class RacingGameController {
+ private UIManager uiManager;
+
+ public void run() {
+ uiManager = new UIManager();
+
+ uiManager.printGameStartMsg();
+ RacingCars racingCars = createRacingCars(uiManager.inputCarNames());
+
+ TrialCount trialCount = createTrialCount(uiManager.inputTrialCount());
+ RandomNumber randomNumber = new GameRandomNumber();
+ GameRule gameRule = new GameRule(trialCount, randomNumber);
+
+ uiManager.printGameResultMsg();
+ for (int i = 0; i < gameRule.getTrialCount(); i++) {
+ racingCars.moveCars(gameRule);
+ uiManager.printCarPositions(racingCars.getCarPositions());
+ }
+
+ uiManager.printWinners(racingCars.pickWinners());
+ }
+
+ public RacingCars createRacingCars(String userInput) {
+ try {
+ return new RacingCars(userInput);
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ return createRacingCars(uiManager.inputCarNames());
+ }
+ }
+
+ public TrialCount createTrialCount(String userInput) {
+ try {
+ return new TrialCount(userInput);
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ return createTrialCount(uiManager.inputTrialCount());
+ }
+ }
+} | Java | ํด๋น controller์์ ์์กด์ฑ ์ฃผ์
์ ๋ํด์ ํ๋ฒ ๊ณ ๋ฏผ ํด๋ณผ ํ์๊ฐ ์์ด์!
controller ๊ณ์ธต์ ์ฌ์ฉํ๊ธฐ์ํด์ ๋ง๋ ๊ฒ์ผ๋ก ๋ณด์ด๋๋ฐ
์ ๊ทผ์ ์ด์๊ฐ ์๋ ๊ฐ์ฒด๋ค์ ์ฌ์ฉํ์ ์ด์ ๊ฐ ์์๊น์ |
@@ -0,0 +1,22 @@
+package racingcar.constants;
+
+public enum ErrorMsg {
+ BLANK_IN_NAME_ERROR("์ด๋ฆ์๋ ๊ณต๋ฐฑ์ด ์์ด์ผ ํ๋ฉฐ ์ผํ(,) ์๋ค์๋ ๊ณต๋ฐฑ์ด ์์ด์ผ ํฉ๋๋ค."),
+ CAR_NAME_LENGTH_MIN_ERROR("์๋์ฐจ ์ด๋ฆ์ " + GameConstants.CAR_NAME_LENGTH_MIN.getNumber() + "์ ์ด์์ด์ด์ผ ํฉ๋๋ค."),
+ CAR_NAME_LENGTH_MAX_ERROR("์๋์ฐจ ์ด๋ฆ์ " + GameConstants.CAR_NAME_LENGTH_MAX.getNumber() + "์ ์ดํ์ฌ์ผ ํฉ๋๋ค."),
+ TRIAL_COUNT_INPUT_TYPE_ERROR("์๋ ํ์๋ ์ซ์๋ก ์
๋ ฅํด์ผ ํฉ๋๋ค."),
+ TRIAL_COUNT_EMPTY_INPUT_ERROR("์๋ ํ์๋ฅผ ์
๋ ฅํ์ง ์์์ต๋๋ค."),
+ TRIAL_COUNT_ZERO_ERROR("์๋ ํ์๋ 1 ์ด์์ด์ด์ผ ํฉ๋๋ค.");
+
+ private static final String ERROR_MSG_TAG = "[ERROR]";
+ private final String msg;
+
+ ErrorMsg(String msg) {
+ this.msg = ERROR_MSG_TAG + " " + msg + "\n";
+ }
+
+ public String getMsg() {
+ return msg;
+ }
+
+} | Java | default ์ ๊ทผ ์ ์ด์๋ฅผ ์ฌ์ฉํ์ ์ด์ ๊ฐ ์์๊น์? |
@@ -0,0 +1,16 @@
+package racingcar.constants;
+
+public enum GameConstants {
+ CAR_NAME_LENGTH_MIN(1),
+ CAR_NAME_LENGTH_MAX(5);
+
+ private final int number;
+
+ GameConstants(int number) {
+ this.number = number;
+ }
+
+ public int getNumber() {
+ return number;
+ }
+} | Java | ์ฌ๊ธฐ๋ ์ ญ๊ทผ์ ์ด์๊ฐ!@? |
@@ -0,0 +1,16 @@
+package racingcar.constants;
+
+public enum GameConstants {
+ CAR_NAME_LENGTH_MIN(1),
+ CAR_NAME_LENGTH_MAX(5);
+
+ private final int number;
+
+ GameConstants(int number) {
+ this.number = number;
+ }
+
+ public int getNumber() {
+ return number;
+ }
+} | Java | ํด๋น ํด๋์ค ์ญํ ์ด ๋ชจํธํ์ง ์๋์??
ํด๋์ค ์ด๋ฆ์ ๊ฒ์์ ์ฒด์ ์์๋ฅผ ๊ด๋ฆฌํ๋ ๊ฒ์ผ๋ก ๋ณด์ฌ์ง๋๋ฐ
๋ฉค๋ฒ ๋ณ์๋ ๊ธฐ๋ฅ์ total_count๋ number ์ธํ
๋ ๋ค์ด ์์ด์์ |
@@ -0,0 +1,40 @@
+package racingcar.domain;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import racingcar.constants.ErrorMsg;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class CarNameTest {
+ @Test
+ void ์๋์ฐจ์ด๋ฆ_๊ธธ์ด๋ฒ์์ต์_๋ฒ์ด๋ฌ์_๋() {
+ // Given
+ String carName = "";
+
+ // When & Then
+ assertThatThrownBy(() -> new CarName(carName))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(ErrorMsg.CAR_NAME_LENGTH_MIN_ERROR.getMsg());
+ }
+
+ @Test
+ void ์๋์ฐจ์ด๋ฆ_๊ธธ์ด๋ฒ์์ต๋_๋ฒ์ด๋ฌ์_๋() {
+ // Given
+ String carName = "ferrari";
+
+ // When & Then
+ assertThatThrownBy(() -> new CarName(carName))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(ErrorMsg.CAR_NAME_LENGTH_MAX_ERROR.getMsg());
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"a", "abc", "abcde"})
+ void ์๋์ฐจ์ด๋ฆ_๊ธธ์ด๋ฒ์_์ ์(String carName) {
+ assertThatCode(() -> new CarName(carName))
+ .doesNotThrowAnyException();
+ }
+} | Java | ํ
์คํธ ๋ฉ์๋ ์ด๋ฆ์ด ์ถฉ๋ถํ ๋ช
์์ ์ด๋ผ์
ํด๋น given/when/then ์ฃผ์์ ์์ด๋ ๋ ๊ฑฐ ๊ฐ์์
์กฐ๊ฑด์ด ๋ฐ๋๋๋ง๋ค
์ฃผ์์ ์์ ํด์ฃผ์ง ์์ผ๋ฉด ์ค์ ํ
์คํธ ์ฝ๋์ ์ฃผ์์ด ๋ถ์ผ์น ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ฐ์ ํ ์ ์์ด์ |
@@ -0,0 +1,40 @@
+package racingcar.domain;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import racingcar.constants.ErrorMsg;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class CarNameTest {
+ @Test
+ void ์๋์ฐจ์ด๋ฆ_๊ธธ์ด๋ฒ์์ต์_๋ฒ์ด๋ฌ์_๋() {
+ // Given
+ String carName = "";
+
+ // When & Then
+ assertThatThrownBy(() -> new CarName(carName))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(ErrorMsg.CAR_NAME_LENGTH_MIN_ERROR.getMsg());
+ }
+
+ @Test
+ void ์๋์ฐจ์ด๋ฆ_๊ธธ์ด๋ฒ์์ต๋_๋ฒ์ด๋ฌ์_๋() {
+ // Given
+ String carName = "ferrari";
+
+ // When & Then
+ assertThatThrownBy(() -> new CarName(carName))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(ErrorMsg.CAR_NAME_LENGTH_MAX_ERROR.getMsg());
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"a", "abc", "abcde"})
+ void ์๋์ฐจ์ด๋ฆ_๊ธธ์ด๋ฒ์_์ ์(String carName) {
+ assertThatCode(() -> new CarName(carName))
+ .doesNotThrowAnyException();
+ }
+} | Java | ์ธ๋ถ ์ค๋ฅ ๊ฒ์ฆ ๐ |
@@ -0,0 +1,67 @@
+package racingcar.domain;
+
+import racingcar.constants.ErrorMsg;
+
+import java.util.*;
+
+public class RacingCars {
+ private final List<RacingCar> racingCars;
+
+ public RacingCars(String userInput) {
+ racingCars = new ArrayList<>();
+ createRacingCars(userInput);
+ }
+
+ private void createRacingCars(String userInput) {
+ if (validateBlank(userInput)) {
+ throw new IllegalArgumentException(ErrorMsg.BLANK_IN_NAME_ERROR.getMsg());
+ }
+
+ addRacingCars(userInput);
+ }
+
+ private void addRacingCars(String userInput) {
+ for (String carName : userInput.split(",", -1)) {
+ racingCars.add(new RacingCar(carName));
+ }
+ }
+
+ private boolean validateBlank(String userInput) {
+ return userInput.contains(" ");
+ }
+
+ public void moveCars(GameRule gameRule) {
+ for (RacingCar car : racingCars) {
+ car.attemptToMove(gameRule);
+ }
+ }
+
+ public Map<String, Integer> getCarPositions() {
+ Map<String, Integer> carPosition = new LinkedHashMap<>();
+ for (RacingCar car : racingCars) {
+ carPosition.put(car.getCarName(), car.getMoveCount());
+ }
+ return carPosition;
+ }
+
+ public List<String> pickWinners() {
+ Winners winners = new Winners();
+ int maxStep = getMaxStep();
+
+ for (RacingCar car : racingCars) {
+ winners.pickWinner(car, maxStep);
+ }
+
+ return winners.getWinners();
+ }
+
+ private int getMaxStep() {
+ int maxStep = 0;
+ for (RacingCar car : racingCars) {
+ maxStep = Integer.max(maxStep, car.getMoveCount());
+ }
+
+ return maxStep;
+ }
+
+} | Java | ์ผ๊ธ์ปฌ๋ ์
ํ์ฉ ๐ฏ |
@@ -0,0 +1,67 @@
+package racingcar.domain;
+
+import racingcar.constants.ErrorMsg;
+
+import java.util.*;
+
+public class RacingCars {
+ private final List<RacingCar> racingCars;
+
+ public RacingCars(String userInput) {
+ racingCars = new ArrayList<>();
+ createRacingCars(userInput);
+ }
+
+ private void createRacingCars(String userInput) {
+ if (validateBlank(userInput)) {
+ throw new IllegalArgumentException(ErrorMsg.BLANK_IN_NAME_ERROR.getMsg());
+ }
+
+ addRacingCars(userInput);
+ }
+
+ private void addRacingCars(String userInput) {
+ for (String carName : userInput.split(",", -1)) {
+ racingCars.add(new RacingCar(carName));
+ }
+ }
+
+ private boolean validateBlank(String userInput) {
+ return userInput.contains(" ");
+ }
+
+ public void moveCars(GameRule gameRule) {
+ for (RacingCar car : racingCars) {
+ car.attemptToMove(gameRule);
+ }
+ }
+
+ public Map<String, Integer> getCarPositions() {
+ Map<String, Integer> carPosition = new LinkedHashMap<>();
+ for (RacingCar car : racingCars) {
+ carPosition.put(car.getCarName(), car.getMoveCount());
+ }
+ return carPosition;
+ }
+
+ public List<String> pickWinners() {
+ Winners winners = new Winners();
+ int maxStep = getMaxStep();
+
+ for (RacingCar car : racingCars) {
+ winners.pickWinner(car, maxStep);
+ }
+
+ return winners.getWinners();
+ }
+
+ private int getMaxStep() {
+ int maxStep = 0;
+ for (RacingCar car : racingCars) {
+ maxStep = Integer.max(maxStep, car.getMoveCount());
+ }
+
+ return maxStep;
+ }
+
+} | Java | ๋งค๊ฒ๋ณ์๋ฅผ ์ค์ผ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์๊ฑฐ ๊ฐ์๋ฐ ํ๋ฒ ๊ณ ๋ฏผํด๋ด์ฃผ์ธ์ |
@@ -0,0 +1,46 @@
+package racingcar.domain;
+
+import racingcar.constants.ErrorMsg;
+
+public class TrialCount {
+ private final int count;
+
+ public TrialCount(String userInput) {
+ validateEmptyInput(userInput);
+ validateInputType(userInput);
+ count = convertToInt(userInput);
+ validateTrialCountZero(count);
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ private int convertToInt(String userInput) {
+ return Integer.parseInt(userInput);
+ }
+
+ private void validateEmptyInput(String userInput) {
+ if (userInput.isEmpty()) {
+ throw new IllegalArgumentException(ErrorMsg.TRIAL_COUNT_EMPTY_INPUT_ERROR.getMsg());
+ }
+ }
+
+ private void validateInputType(String userInput) {
+ for (char c : userInput.toCharArray()) {
+ validateIsDigit(c);
+ }
+ }
+
+ private void validateIsDigit(char c) {
+ if (!Character.isDigit(c)) {
+ throw new IllegalArgumentException(ErrorMsg.TRIAL_COUNT_INPUT_TYPE_ERROR.getMsg());
+ }
+ }
+
+ private void validateTrialCountZero(int trialNumber) {
+ if (trialNumber <= 0) {
+ throw new IllegalArgumentException(ErrorMsg.TRIAL_COUNT_ZERO_ERROR.getMsg());
+ }
+ }
+} | Java | ์ค ์ด๋ถ๋ถ ๊ตฌ์กฐ๊ฐ ์ข์๋ณด์ฌ์
์ด๋ ์ซ์๋ฅผ ๊ฐ์ฒด๋ก ํฌ์ฅํด์
validation์ ํ๋ ์์ง๋ ฅ์ด ์๋น์ด ์ข์๋ณด์
๋๋ค. |
@@ -0,0 +1,30 @@
+package racingcar.domain;
+
+public class RacingCar {
+ private final CarName carName;
+ private int moveCount;
+
+ public RacingCar(String name) {
+ this.carName = new CarName(name);
+ moveCount = 0;
+ }
+
+ public int getMoveCount() {
+ return moveCount;
+ }
+
+ public String getCarName() {
+ return carName.getName();
+ }
+
+ public void attemptToMove(GameRule gameRule) {
+ if (gameRule.canMove()) {
+ move();
+ }
+ }
+
+ private void move() {
+ moveCount++;
+ }
+
+} | Java | GameRule ๋๋ฉ์ธ์ ์ถ๊ฐํ์ฌ ์ ์ง์ฌ๋ถ ํ๋จ ๋ก์ง์ ๋ถ๋ฆฌํ์์ต๋๋ค! |
@@ -0,0 +1,45 @@
+package racingcar.controller;
+
+import racingcar.domain.*;
+import racingcar.view.UIManager;
+
+public class RacingGameController {
+ private UIManager uiManager;
+
+ public void run() {
+ uiManager = new UIManager();
+
+ uiManager.printGameStartMsg();
+ RacingCars racingCars = createRacingCars(uiManager.inputCarNames());
+
+ TrialCount trialCount = createTrialCount(uiManager.inputTrialCount());
+ RandomNumber randomNumber = new GameRandomNumber();
+ GameRule gameRule = new GameRule(trialCount, randomNumber);
+
+ uiManager.printGameResultMsg();
+ for (int i = 0; i < gameRule.getTrialCount(); i++) {
+ racingCars.moveCars(gameRule);
+ uiManager.printCarPositions(racingCars.getCarPositions());
+ }
+
+ uiManager.printWinners(racingCars.pickWinners());
+ }
+
+ public RacingCars createRacingCars(String userInput) {
+ try {
+ return new RacingCars(userInput);
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ return createRacingCars(uiManager.inputCarNames());
+ }
+ }
+
+ public TrialCount createTrialCount(String userInput) {
+ try {
+ return new TrialCount(userInput);
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ return createTrialCount(uiManager.inputTrialCount());
+ }
+ }
+} | Java | ์ ๊ทผ์ ์ด์ private์ผ๋ก ์์ ํ์์ต๋๋ค. |
@@ -0,0 +1,22 @@
+package racingcar.constants;
+
+public enum ErrorMsg {
+ BLANK_IN_NAME_ERROR("์ด๋ฆ์๋ ๊ณต๋ฐฑ์ด ์์ด์ผ ํ๋ฉฐ ์ผํ(,) ์๋ค์๋ ๊ณต๋ฐฑ์ด ์์ด์ผ ํฉ๋๋ค."),
+ CAR_NAME_LENGTH_MIN_ERROR("์๋์ฐจ ์ด๋ฆ์ " + GameConstants.CAR_NAME_LENGTH_MIN.getNumber() + "์ ์ด์์ด์ด์ผ ํฉ๋๋ค."),
+ CAR_NAME_LENGTH_MAX_ERROR("์๋์ฐจ ์ด๋ฆ์ " + GameConstants.CAR_NAME_LENGTH_MAX.getNumber() + "์ ์ดํ์ฌ์ผ ํฉ๋๋ค."),
+ TRIAL_COUNT_INPUT_TYPE_ERROR("์๋ ํ์๋ ์ซ์๋ก ์
๋ ฅํด์ผ ํฉ๋๋ค."),
+ TRIAL_COUNT_EMPTY_INPUT_ERROR("์๋ ํ์๋ฅผ ์
๋ ฅํ์ง ์์์ต๋๋ค."),
+ TRIAL_COUNT_ZERO_ERROR("์๋ ํ์๋ 1 ์ด์์ด์ด์ผ ํฉ๋๋ค.");
+
+ private static final String ERROR_MSG_TAG = "[ERROR]";
+ private final String msg;
+
+ ErrorMsg(String msg) {
+ this.msg = ERROR_MSG_TAG + " " + msg + "\n";
+ }
+
+ public String getMsg() {
+ return msg;
+ }
+
+} | Java | Enumํด๋์ค์ ์์ฑ์๋ private๋ง ํ์ฉํ๋ฏ๋ก
์ ๊ทผ์ ์ด์๋ฅผ ์ง์ ํ์ง ์์ผ๋ฉด private์ด ๊ธฐ๋ณธ์ผ๋ก ์ง์ ๋๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก ์ง์ ์ ์ํ์ต๋๋ค!
๋ช
์์ ์ผ๋ก private ํค์๋๋ฅผ ๋ถ์ด๋ ๊ฒ์ด ์ข์๊น์? |
@@ -0,0 +1,16 @@
+package racingcar.constants;
+
+public enum GameConstants {
+ CAR_NAME_LENGTH_MIN(1),
+ CAR_NAME_LENGTH_MAX(5);
+
+ private final int number;
+
+ GameConstants(int number) {
+ this.number = number;
+ }
+
+ public int getNumber() {
+ return number;
+ }
+} | Java | ์์ ErrorMsg์ ๊ฐ์ ์ด์ ๋ก ์ ๊ทผ์ ์ด์๋ฅผ ์ง์ ํ์ง ์์์ต๋๋ค. |
@@ -0,0 +1,16 @@
+package racingcar.constants;
+
+public enum GameConstants {
+ CAR_NAME_LENGTH_MIN(1),
+ CAR_NAME_LENGTH_MAX(5);
+
+ private final int number;
+
+ GameConstants(int number) {
+ this.number = number;
+ }
+
+ public int getNumber() {
+ return number;
+ }
+} | Java | TRIAL_COUNT(์๋ํ์)๋ ๊ฒ์์ ์ฒด ์์ ์ค ํ๋๋ผ๊ณ ์๊ฐํ์๋๋ฐ,
๊ฒ์ ์์ ์ ๋ถํฐ ๊ฒฐ์ ๋๋ ์์์
๊ฒ์ ์์ ํ ๊ฒฐ์ ๋๋ TRIAL_COUNT๋ ์ฐจ์ด์ ์ด ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
TRIAL_COUNT๋ GameConstants์์๋ ์ญ์ ํ๊ณ , TrialCount ๊ฐ์ฒด๋ฅผ ํตํด์ ๊ฐ์ ๋ฐ์์ค๋๋ก ์์ ํ์์ต๋๋ค. |
@@ -0,0 +1,40 @@
+package racingcar.domain;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import racingcar.constants.ErrorMsg;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class CarNameTest {
+ @Test
+ void ์๋์ฐจ์ด๋ฆ_๊ธธ์ด๋ฒ์์ต์_๋ฒ์ด๋ฌ์_๋() {
+ // Given
+ String carName = "";
+
+ // When & Then
+ assertThatThrownBy(() -> new CarName(carName))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(ErrorMsg.CAR_NAME_LENGTH_MIN_ERROR.getMsg());
+ }
+
+ @Test
+ void ์๋์ฐจ์ด๋ฆ_๊ธธ์ด๋ฒ์์ต๋_๋ฒ์ด๋ฌ์_๋() {
+ // Given
+ String carName = "ferrari";
+
+ // When & Then
+ assertThatThrownBy(() -> new CarName(carName))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(ErrorMsg.CAR_NAME_LENGTH_MAX_ERROR.getMsg());
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"a", "abc", "abcde"})
+ void ์๋์ฐจ์ด๋ฆ_๊ธธ์ด๋ฒ์_์ ์(String carName) {
+ assertThatCode(() -> new CarName(carName))
+ .doesNotThrowAnyException();
+ }
+} | Java | ๋ถํ์ํ ์ฃผ์ ์ ๊ฑฐํ์ต๋๋ค! |
@@ -0,0 +1,67 @@
+package racingcar.domain;
+
+import racingcar.constants.ErrorMsg;
+
+import java.util.*;
+
+public class RacingCars {
+ private final List<RacingCar> racingCars;
+
+ public RacingCars(String userInput) {
+ racingCars = new ArrayList<>();
+ createRacingCars(userInput);
+ }
+
+ private void createRacingCars(String userInput) {
+ if (validateBlank(userInput)) {
+ throw new IllegalArgumentException(ErrorMsg.BLANK_IN_NAME_ERROR.getMsg());
+ }
+
+ addRacingCars(userInput);
+ }
+
+ private void addRacingCars(String userInput) {
+ for (String carName : userInput.split(",", -1)) {
+ racingCars.add(new RacingCar(carName));
+ }
+ }
+
+ private boolean validateBlank(String userInput) {
+ return userInput.contains(" ");
+ }
+
+ public void moveCars(GameRule gameRule) {
+ for (RacingCar car : racingCars) {
+ car.attemptToMove(gameRule);
+ }
+ }
+
+ public Map<String, Integer> getCarPositions() {
+ Map<String, Integer> carPosition = new LinkedHashMap<>();
+ for (RacingCar car : racingCars) {
+ carPosition.put(car.getCarName(), car.getMoveCount());
+ }
+ return carPosition;
+ }
+
+ public List<String> pickWinners() {
+ Winners winners = new Winners();
+ int maxStep = getMaxStep();
+
+ for (RacingCar car : racingCars) {
+ winners.pickWinner(car, maxStep);
+ }
+
+ return winners.getWinners();
+ }
+
+ private int getMaxStep() {
+ int maxStep = 0;
+ for (RacingCar car : racingCars) {
+ maxStep = Integer.max(maxStep, car.getMoveCount());
+ }
+
+ return maxStep;
+ }
+
+} | Java | Winners ๋๋ฉ์ธ์ ์ถ๊ฐํ์ฌ ์ฐ์น์ ์ถ์ถํ๋ ๋ก์ง์ ๋ถ๋ฆฌํ์์ต๋๋ค! |
@@ -0,0 +1,30 @@
+package racingcar.domain;
+
+public class RacingCar {
+ private final CarName carName;
+ private int moveCount;
+
+ public RacingCar(String name) {
+ this.carName = new CarName(name);
+ moveCount = 0;
+ }
+
+ public int getMoveCount() {
+ return moveCount;
+ }
+
+ public String getCarName() {
+ return carName.getName();
+ }
+
+ public void attemptToMove(GameRule gameRule) {
+ if (gameRule.canMove()) {
+ move();
+ }
+ }
+
+ private void move() {
+ moveCount++;
+ }
+
+} | Java | 1. ๊ธฐ๋ณธ์์ฑ์๋ฅผ ํ์ฉํ ๋๋ฉ์ธ ํ์ฉ์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ข์ ๋ฐฉ๋ฒ์ด ์๋์์. -> ํด๋น ๋๋ฉ์ธ์ ๋๋ฉ์ธ์ด๋ผ๊ธฐ ๋ณด๋จ static method๋ฅผ ํ์ฉ ํ๊ธฐ ์ํ vo ๋ก ์๊ฐ ๋์ ใ
ใ
2. GameRule์ ๋๋ฉ์ธ์ผ๋ก ๊ฐ์ฃผ ํ๋ค๋ฉด, racingcar ->gamerule ์ผ๋ก ์์กด์ฑ์ด ์๊ธฐ๋๋ฐ ํฉ๋ฆฌ์ ์ธ ๊ตฌ์กฐ๋ก ๋ณด์ด์ง๋ ์์์ใ
---------------
ํํธ์ค ๋ด์ฉ์ ์ข๋ ๊ณ ๋ฏผํด๋ณด๋ฉด ์ข์์๊ฑฐ ๊ฐ์์. ๋จ์ํ ํด๋น ๋๋ฉ์ธ์ ์ถ๊ฐํด์ ์ฒ๋ฆฌํ๋๊ฒ์ด ์๋๋ผ
๊ฐ ๋๋ฉ์ธ์ ์ด๋ค์์ผ๋ก ํธ์ถํ๊ณ ์์กด์ฑ ๊ด๋ฆฌํ ์ง ๊ณ ๋ฏผ์ ์ข๋ํด์ ์์ ํด๋ด์ |
@@ -0,0 +1,22 @@
+package racingcar.constants;
+
+public enum ErrorMsg {
+ BLANK_IN_NAME_ERROR("์ด๋ฆ์๋ ๊ณต๋ฐฑ์ด ์์ด์ผ ํ๋ฉฐ ์ผํ(,) ์๋ค์๋ ๊ณต๋ฐฑ์ด ์์ด์ผ ํฉ๋๋ค."),
+ CAR_NAME_LENGTH_MIN_ERROR("์๋์ฐจ ์ด๋ฆ์ " + GameConstants.CAR_NAME_LENGTH_MIN.getNumber() + "์ ์ด์์ด์ด์ผ ํฉ๋๋ค."),
+ CAR_NAME_LENGTH_MAX_ERROR("์๋์ฐจ ์ด๋ฆ์ " + GameConstants.CAR_NAME_LENGTH_MAX.getNumber() + "์ ์ดํ์ฌ์ผ ํฉ๋๋ค."),
+ TRIAL_COUNT_INPUT_TYPE_ERROR("์๋ ํ์๋ ์ซ์๋ก ์
๋ ฅํด์ผ ํฉ๋๋ค."),
+ TRIAL_COUNT_EMPTY_INPUT_ERROR("์๋ ํ์๋ฅผ ์
๋ ฅํ์ง ์์์ต๋๋ค."),
+ TRIAL_COUNT_ZERO_ERROR("์๋ ํ์๋ 1 ์ด์์ด์ด์ผ ํฉ๋๋ค.");
+
+ private static final String ERROR_MSG_TAG = "[ERROR]";
+ private final String msg;
+
+ ErrorMsg(String msg) {
+ this.msg = ERROR_MSG_TAG + " " + msg + "\n";
+ }
+
+ public String getMsg() {
+ return msg;
+ }
+
+} | Java | ์ค ์ด๊ฑด ์ ๊ฐ ์ค์ํ๋ค์. ๋๋ฆฌ๋ ๋ง์์ด ๋ง์์! |
@@ -0,0 +1,16 @@
+package racingcar.constants;
+
+public enum GameConstants {
+ CAR_NAME_LENGTH_MIN(1),
+ CAR_NAME_LENGTH_MAX(5);
+
+ private final int number;
+
+ GameConstants(int number) {
+ this.number = number;
+ }
+
+ public int getNumber() {
+ return number;
+ }
+} | Java | GameRule ๋๋ฉ์ธ์ด ๋ง๋ค์ด ์ ธ์, GameConstants์ ์ค๋ณต๋ ์๋ฏธ๋ฅผ ๋ด๋๊ฑฐ ๊ฐ์๋ฐ
ํด๋น ํด๋์ค์ ๋ํด ๊ณ ๋ฏผ์ด ์ข๋ ํ์ํด๋ณด์ฌ์ฉ |
@@ -0,0 +1,30 @@
+package racingcar.domain;
+
+public class RacingCar {
+ private final CarName carName;
+ private int moveCount;
+
+ public RacingCar(String name) {
+ this.carName = new CarName(name);
+ moveCount = 0;
+ }
+
+ public int getMoveCount() {
+ return moveCount;
+ }
+
+ public String getCarName() {
+ return carName.getName();
+ }
+
+ public void attemptToMove(GameRule gameRule) {
+ if (gameRule.canMove()) {
+ move();
+ }
+ }
+
+ private void move() {
+ moveCount++;
+ }
+
+} | Java | ### ํ์ฌ ์์ ์ฌํญ
- GameRule ๋๋ฉ์ธ์ ๋ฉค๋ฒ๋ณ์๋ก RandomNumber์ TrialCount ์ถ๊ฐ
- RandomNumber๋ ์๋์ฐจ์ ์ ์ง์ฌ๋ถ๋ฅผ ํ๋จํ ๋๋ง ํ์ํ๋ฐ, ์ ์ง์ฌ๋ถ ํ๋จ๋ก์ง์ธ GameRule์ ์์ผ๋ฏ๋ก RandomNumber๋ฅผ GameRule์ ๋ฉค๋ฒ๋ณ์๋ก ์ถ๊ฐํจ
- GameRule ๊ฐ์ฒด๋ฅผ RacingCar์ ๋ฉ์๋ ์์์ ์์ฑํ์ง ์๊ณ Controller์์ ์์ฑํ๋๋ก ์์
- Controller์์ RacingCars์ GameRule๊ฐ์ฒด๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ
- GameRandomNumber์ getRandomNumber() ๋ฉ์๋ ์์์ ๋ฐ๋ก ๋๋ค์ซ์๋ฅผ ๋ฐํํ๋๋ก ์์
- ๋๋ค์ซ์๊ฐ ํ์ํ ๋๋ง๋ค ์๋ก์ด RandomNumber ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๊ณ , GameRule์ด ๊ฐ์ง๊ณ ์๋ RandomNumber ๊ฐ์ฒด๋ฅผ ํตํด ๋๋ค์ซ์ ๊ฐ๋ง ๋ฐํ๋ฐ๋๋ก ํจ
### Issue 1 : GameRule ๋๋ฉ์ธ๊ณผ ๋ค๋ฅธ ๋๋ฉ์ธ ๊ฐ์ ์์กด๊ด๊ณ ์ค์ ์ ์ด๋ ค์
- RacingCars์ ๋ฉ์๋ ์์์ ๊ฐ ์๋์ฐจ ๊ฐ์ฒด๋ง๋ค ์ ์ง์ฌ๋ถํ๋จ์ด ํ์ํ๋ฐ, GameRule ๊ฐ์ฒด๋ฅผ RacingCar์๊ฒ ๋๊ฒจ์ฃผ์ง ์๊ณ ์ธ๋ถ์์ ํ๋จ ํ RacingCar๋ฅผ ์ ์ง์ํฌ ๋ฐฉ๋ฒ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค..!
### Issue 2 : GameRandomNumber์์ randomNumber๋ผ๋ ๋ฉค๋ฒ๋ณ์์ ํ์์ฑ
- getRandomNumber()์์ ๋ฐ๋ก ๋๋ค์ซ์๋ฅผ ๋ฐํํด์ฃผ๋๋ฐ, ๊ทธ๋ผ randomNumber ๋ฉค๋ฒ๋ณ์๊ฐ ์์ด๋ ๋๋ ๊ฒ์ผ๊น์?
### Issue 3 : GameConstants ํด๋์ค
- ๊ฒ์์์ 5๊ฐ ์ค, 3๊ฐ๋ ํ ๊ณณ์์๋ง ํธ์ถํ๊ณ ๋๋จธ์ง 2๊ฐ๋ ๋ ๊ตฐ๋ฐ์์ ํธ์ถํ๋๋ฐ, ์ด ์์๋ค์ ์ด๋ป๊ฒ ๊ด๋ฆฌํ๋ ๊ฒ์ด ํจ๊ณผ์ ์ผ์ง ๊ถ๊ธํฉ๋๋ค! ์๋๋ ์ ๊ฐ ์๊ฐํด ๋ณธ ๋ฐฉ๋ฒ๋ค์
๋๋ค.
- ๋ชจ๋ ์์๋ฅผ Enum ํด๋์ค๋ก ๋ชจ์์ ๊ด๋ฆฌ
- ํ ๊ณณ์์ ํธ์ถํ๋ ์์๋ ๊ฐ๊ฐ ์ฌ์ฉ๋๋ ๋๋ฉ์ธ์ ๋ฉค๋ฒ๋ณ์๋ก ์ ์ธ, ๋๋จธ์ง๋ Enumํด๋์ค๋ก ๊ด๋ฆฌ
- ๋ชจ๋ ์์๋ฅผ ๊ฐ๊ฐ ์ฌ์ฉ๋๋ ๋๋ฉ์ธ์ ๋ฉค๋ฒ๋ณ์๋ก ์ ์ธ
- ์ฌ๋ฌ ๊ณณ์์ ์ฌ์ฉ๋๋ ์์์ ๊ฒฝ์ฐ, ํน์ ๋๋ฉ์ธ์ ์ ์ ์์๋ก ์ ์ธํ๊ณ ๋ค๋ฅธ ๋๋ฉ์ธ์์ ํธ์ถ
---
๊ฐ ๋๋ฉ์ธ์ ์ญํ ์ ๊ตฌ๋ถํ๋ ๊ฒ๊ณผ ๋๋ฉ์ธ ๊ฐ ์์กด๊ด๊ณ๋ฅผ ์ค์ ํ๋ ๋ถ๋ถ์ด ์ด๋ ต๊ณ ๊ฐ์ธ์ ์ผ๋ก ์ฝํ ๊ฒ ๊ฐ์ต๋๋ค.
์ ์ฒด์ ์ผ๋ก ์ ๊ฐ ํผ๋์ค๋ฌ์ด ๋ถ๋ถ์ด ๋ง์์ ์ฝ๋๋ ๋ฌ๋ผ์ง ์ ์ด ๋ง์ด ์๊ณ , ์ด์๋ค ์์ ์ ์ด๋์์ต๋๋ค. ๋ฆฌ๋ทฐ ๋ถํ๋๋ฆฝ๋๋ค! |
@@ -0,0 +1,24 @@
+package com.example.chatgptcodereviewtest.controller;
+
+import com.example.chatgptcodereviewtest.service.TestService2;
+import lombok.RequiredArgsConstructor;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * ์ค๋ช
:
+ *
+ * @author ์ตํ๋ฒ(Jayce) / hb.choi@dreamus.io
+ * @since 2023/03/23
+ */
+@RestController
+@RequestMapping("/test2")
+@RequiredArgsConstructor
+public class TestController2 {
+
+ private final TestService2 testService2;
+
+ @GetMapping
+ public String getText(@RequestParam(value = "label", required = false) String label) {
+ return testService2.getUppercaseTestLabel(label);
+ }
+}
\ No newline at end of file | Java | ์ ์ฝ๋๋ `TestController2`๋ผ๋ REST API ์ปจํธ๋กค๋ฌ ํด๋์ค์
๋๋ค.
๊ฐ์ ์ ์:
1. Javadoc(์ฃผ์) ์์ฑ์ด ํ์ํฉ๋๋ค. ๋ฉ์๋์ ํด๋์ค์ ์ค๋ช
์ ๊ตฌ์ฒด์ ์ผ๋ก ๋ฌ์์ฃผ์ด ๊ฐ๋
์ฑ์ ๋์ผ ์ ์์ต๋๋ค.
2. ๋ง์ง๋ง ์ค์ ๊ฐํ ๋ฌธ์์ด์ด ์์ต๋๋ค. ์์ค์ฝ๋๋ฅผ ์์ ๋ฐ ํธ์งํ ๋ ๊น์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋๋ก ๊ฐํ ๋ฌธ์์ด์ ์ถ๊ฐํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
3. `@RequestParam` ์ด๋
ธํ
์ด์
์ required ์์ฑ๊ฐ์ false๋ก ๋์ด ์์ด, ํ์๋ก ๋ค์ด์ฌ ๊ฐ์ด ์๋๋๋ค. ์ด์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ํ๊ณ ์ถ๋ค๋ฉด, ํด๋น ํ๋ผ๋ฏธํฐ ๋ณ์์ ๋ ์ฒดํฌ๋ ๋ํดํธ๊ฐ ๋ฑ์ ์ง์ ํด์ผ ํฉ๋๋ค.
๋ฒ๊ทธ ์ํ์ด ์์ด ๋ณด์
๋๋ค. |
@@ -0,0 +1,17 @@
+package com.example.chatgptcodereviewtest.service;
+
+import org.springframework.stereotype.Service;
+
+/**
+ * ์ค๋ช
:
+ *
+ * @author ์ตํ๋ฒ(Jayce) / hb.choi@dreamus.io
+ * @since 2023/03/23
+ */
+@Service
+public class TestService2 {
+
+ public String getUppercaseTestLabel(String label) {
+ return label.toUpperCase();
+ }
+} | Java | ์ด ์ฝ๋๋ ๋ด์์ ์ธ ๋ฌธ์ ๋ ๋ฒ๊ทธ๊ฐ ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
๋จ์ํ๊ฒ ๋ฌธ์์ด์ ๋๋ฌธ์๋ก ๋ณ๊ฒฝํ๋ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, Spring์ Service ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ Bean์ผ๋ก ๋ฑ๋ก๋์ด ์์ต๋๋ค.
๋ณ์๋ช
๊ณผ ๋ฉ์๋๋ช
์ ์ง๊ด์ฑ ์ธก๋ฉด์์ ๊ฐ์ ํ ์ ์ด ์๋ ๊ฒ ๊ฐ์ต๋๋ค. 'label'์ ๊ตฌ์ฒด์ ์ธ ์๋ฏธ๋ฅผ ์๊ธฐ ์ด๋ ต์ต๋๋ค. ๋ ๊ตฌ์ฒด์ ์ธ ๋ช
์นญ์ ์ฌ์ฉํ๋ค๋ฉด ์ฝ๋์ ๊ฐ๋
์ฑ์ด ํฅ์๋ ๊ฒ์
๋๋ค. |
@@ -0,0 +1,35 @@
+package nextstep.subway.applicaion;
+
+import java.util.List;
+import nextstep.subway.applicaion.dto.PathResponse;
+import nextstep.subway.domain.Line;
+import nextstep.subway.domain.LineRepository;
+import nextstep.subway.domain.Path;
+import nextstep.subway.domain.PathFinder;
+import nextstep.subway.domain.Station;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@Transactional
+public class PathService {
+ private final LineRepository lineRepository;
+ private final StationService stationService;
+
+ public PathService(LineRepository lineRepository,
+ StationService stationService) {
+ this.lineRepository = lineRepository;
+ this.stationService = stationService;
+ }
+
+ public PathResponse searchPath(Long sourceId, Long targetId) {
+ Station source = stationService.findById(sourceId);
+ Station target = stationService.findById(targetId);
+ List<Line> lines = lineRepository.findAll();
+
+ PathFinder pathFinder = PathFinder.from(lines);
+ Path shortestPath = pathFinder.searchPath(source, target);
+ return PathResponse.from(shortestPath);
+ }
+
+} | Java | ๊ฒฝ๋ก๋ฅผ ์ฐพ๊ธฐ์ํด lines๋ผ๋ ์ ๋ณด๊ฐ ์๊ณ , ์ด ์ ๋ณด๋ฅผ ์ด์ฉํด ๊ฒฝ๋ก๋ฅผ ์ฐพ๋๋ฐ ์ด ๊ฒฝ๋ก๋ฅผ ์ฐพ๋ ์ฑ
์์ PathFinder๊ฐ ๊ฐ์ง๊ณ ์๊ณ ๊ทธ ๊ฒฐ๊ณผ ์ ๋ณด๋ Path๊ฐ ๊ฐ์ง๊ณ ์๋ค์!
๊ฐ์ฒด์งํฅ์์ ๊ฐ์ฒด๋ ์ํ์ ํ์๋ก ์ด๋ฃจ์ด์ ธ์๋ค๊ณ ํ๋๋ฐ์
๊ฒฝ๋ก๋ฅผ ์ฐพ๊ธฐ ์ํ ์ํ์ธ lines์ ํ์์ธ PathFinder๋ฅผ ํ๋์ ๊ฐ์ฒด๋ก ๋์ถํด๋ณผ ์๋ ์์๊น์?
๋ง์ฝ์ ์ด๋ ๊ฒ ๋ฆฌํฉํฐ๋ง ํ ๊ฒฝ์ฐ ๊ธฐ์กด์ ๋ง๋ค์ด ๋์๋ ํ
์คํธ๋ฅผ ํ์ฉํด์ ํ
์คํธ์ ๊ฒ์ฆ์ด๋ผ๋ ๋ณดํธ ์์ ๋ฆฌํฉํฐ๋ง์ ํด๋ณด์๋ ๊ฒ์ ๊ถํด๋๋ฆฝ๋๋ค! |
@@ -63,19 +63,19 @@ public void update(Station newUpStation, int minusDistance) {
}
public Section merge(Section section) {
- if (!isDownStation(section.upStation)) {
+ if (!hasDownStationAs(section.upStation)) {
throw new IllegalArgumentException("ํฉ์น๋ ค๋ ๊ตฌ๊ฐ์ ์ํ์ญ์ด ํํ์ญ๊ณผ ๊ฐ์์ผ ํฉ๋๋ค.");
}
return Section.of(line, upStation, section.downStation, distance + section.distance);
}
- public boolean isUpStation(Station station) {
+ public boolean hasUpStationAs(Station station) {
return upStation.equals(station);
}
- public boolean isDownStation(Station station) {
+ public boolean hasDownStationAs(Station station) {
return downStation.equals(station);
}
| Java | ๋ณ๊ฒฝ๋ ๋ค์ด๋ฐ์ด ์๋ฏธ๋ฅผ ์กฐ๊ธ ๋ ์ ์ ๋ฌํ๋ ๊ฒ ๊ฐ์์ ๐ |
@@ -1,6 +1,7 @@
package nextstep.subway.domain;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -34,23 +35,30 @@ public void add(Section section) {
public void remove(Station station) {
validationRemoveStation(station);
- Optional<Section> firstSection = sections.stream()
- .filter(s -> s.isDownStation(station))
- .findAny();
-
- Optional<Section> secondSection = sections.stream()
- .filter(s -> s.isUpStation(station))
- .findAny();
+ Optional<Section> sectionHasUpStation = findSectionHasUpStationAs(station);
+ Optional<Section> sectionHasDownStation = findSectionHasDownStationAs(station);
- firstSection.ifPresent(section -> sections.remove(section));
- secondSection.ifPresent(section -> sections.remove(section));
+ sectionHasUpStation.ifPresent(section -> sections.remove(section));
+ sectionHasDownStation.ifPresent(section -> sections.remove(section));
- if (firstSection.isPresent() && secondSection.isPresent()) {
- mergeExistingSections(firstSection.get(), secondSection.get());
+ if (sectionHasUpStation.isPresent() && sectionHasDownStation.isPresent()) {
+ mergeExistingSections(sectionHasUpStation.get(), sectionHasDownStation.get());
}
}
+ private Optional<Section> findSectionHasDownStationAs(Station station) {
+ return sections.stream()
+ .filter(s -> s.hasUpStationAs(station))
+ .findAny();
+ }
+
+ private Optional<Section> findSectionHasUpStationAs(Station station) {
+ return sections.stream()
+ .filter(s -> s.hasDownStationAs(station))
+ .findAny();
+ }
+
private List<Station> getStations() {
List<Station> stations = new ArrayList<>();
stations.add(sections.get(0).getUpStation());
@@ -152,4 +160,7 @@ private void mergeExistingSections(Section firstSection, Section secondSection)
sections.add(firstSection.merge(secondSection));
}
+ public List<Section> getSectionList() {
+ return Collections.unmodifiableList(sections);
+ }
} | Java | ์ ์ ํ ์ฌ์ด์ฆ๋ก ๋ฉ์๋๋ฅผ ๋๋์ด ์ฃผ์
จ๋ค์ ๐ |
@@ -0,0 +1,48 @@
+package nextstep.subway.unit;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import nextstep.subway.domain.Line;
+import nextstep.subway.domain.Station;
+import org.springframework.test.util.ReflectionTestUtils;
+
+public class PathFixture {
+ /** (10)
+ * ๊ต๋์ญ --- *2ํธ์ * --- ๊ฐ๋จ์ญ
+ * | |
+ * *3ํธ์ * (2) *์ ๋ถ๋น์ * (5)
+ * | |
+ * ๋จ๋ถํฐ๋ฏธ๋์ญ --- *3ํธ์ * --- ์์ฌ
+ (3) **/
+
+
+ public static Station ๊ฐ๋จ์ญ = new Station("๊ฐ๋จ์ญ");
+ public static Station ๊ต๋์ญ = new Station("๊ต๋์ญ");
+ public static Station ์์ฌ์ญ = new Station("์์ฌ์ญ");
+ public static Station ๋จ๋ถํฐ๋ฏธ๋์ญ = new Station("๋จ๋ถํฐ๋ฏธ๋์ญ");
+
+ public static int ๊ต๋์ญ_๊ฐ๋จ์ญ_๊ฑฐ๋ฆฌ = 10;
+ public static int ๊ฐ๋จ์ญ_์์ฌ์ญ_๊ฑฐ๋ฆฌ = 5;
+ public static int ๊ต๋์ญ_๋จ๋ถํฐ๋ฏธ๋์ญ_๊ฑฐ๋ฆฌ = 2;
+ public static int ๋จ๋ถํฐ๋ฏธ๋์ญ_์์ฌ์ญ_๊ฑฐ๋ฆฌ = 3;
+
+ public static Line ์ดํธ์ = Line.of("2ํธ์ ", "bg-green-600", ๊ต๋์ญ, ๊ฐ๋จ์ญ, ๊ต๋์ญ_๊ฐ๋จ์ญ_๊ฑฐ๋ฆฌ);
+ public static Line ์ผํธ์ = Line.of("3ํธ์ ", "bg-orange-500", ๊ต๋์ญ, ๋จ๋ถํฐ๋ฏธ๋์ญ, ๊ต๋์ญ_๋จ๋ถํฐ๋ฏธ๋์ญ_๊ฑฐ๋ฆฌ);
+ public static Line ์ ๋ถ๋น์ = Line.of("์ ๋ถ๋น์ ", "bg-red-500", ๊ฐ๋จ์ญ, ์์ฌ์ญ, ๊ฐ๋จ์ญ_์์ฌ์ญ_๊ฑฐ๋ฆฌ);
+
+ public static List<Line> ๋
ธ์ _๋ชฉ๋ก = new ArrayList<>();
+
+ static {
+ ์ผํธ์ .addSection(๋จ๋ถํฐ๋ฏธ๋์ญ, ์์ฌ์ญ, ๋จ๋ถํฐ๋ฏธ๋์ญ_์์ฌ์ญ_๊ฑฐ๋ฆฌ);
+
+ ReflectionTestUtils.setField(๊ฐ๋จ์ญ, "id", 1L);
+ ReflectionTestUtils.setField(๊ต๋์ญ, "id", 2L);
+ ReflectionTestUtils.setField(์์ฌ์ญ, "id", 3L);
+ ReflectionTestUtils.setField(๋จ๋ถํฐ๋ฏธ๋์ญ, "id", 4L);
+
+ ๋
ธ์ _๋ชฉ๋ก.addAll(Arrays.asList(์ดํธ์ , ์ผํธ์ , ์ ๋ถ๋น์ ));
+ }
+
+
+} | Java | ํฝ์ค์ณ ๋ถ๋ฆฌ๋ฅผ ํตํด ํ
์คํธ ์ฝ๋ ๋ด ํจ๊ณผ์ ์ธ ์ค๋ณต ์ ๊ฑฐ ๐ |
@@ -0,0 +1,120 @@
+package nextstep.subway.acceptance;
+
+import static nextstep.subway.acceptance.LineSteps.์งํ์ฒ _๋
ธ์ _์์ฑ_์์ฒญ;
+import static nextstep.subway.acceptance.LineSteps.์งํ์ฒ _๋
ธ์ ์_์งํ์ฒ _๊ตฌ๊ฐ_์์ฑ_์์ฒญ;
+import static nextstep.subway.acceptance.PathSteps.๊ฒฝ๋ก_์กฐํ_์์ฒญ;
+import static nextstep.subway.acceptance.StationSteps.์งํ์ฒ ์ญ_์์ฑ_์์ฒญ;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import io.restassured.response.ExtractableResponse;
+import io.restassured.response.Response;
+import java.util.HashMap;
+import java.util.Map;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.springframework.http.HttpStatus;
+
+@DisplayName("์งํ์ฒ ๊ฒฝ๋ก ๊ฒ์")
+public class PathAcceptanceTest extends AcceptanceTest {
+ private Long ๊ต๋์ญ;
+ private Long ๊ฐ๋จ์ญ;
+ private Long ์์ฌ์ญ;
+ private Long ๋จ๋ถํฐ๋ฏธ๋์ญ;
+ private Long ์ฒญ๋๋ฆฌ์ญ;
+ private Long ํ๊ธฐ์ญ;
+
+ private Long ์ผํธ์ ;
+ private Long ์ดํธ์ ;
+ private Long ์ ๋ถ๋น์ ;
+ private Long ์ผํธ์ ;
+
+ private int ๊ต๋์ญ_๊ฐ๋จ์ญ_๊ฑฐ๋ฆฌ = 10;
+ private int ๊ฐ๋จ์ญ_์์ฌ์ญ_๊ฑฐ๋ฆฌ = 5;
+ private int ๊ต๋์ญ_๋จ๋ถํฐ๋ฏธ๋์ญ_๊ฑฐ๋ฆฌ = 2;
+ private int ๋จ๋ถํฐ๋ฏธ๋์ญ_์์ฌ์ญ_๊ฑฐ๋ฆฌ = 3;
+ private int ์ฒญ๋๋ฆฌ์ญ_ํ๊ธฐ์ญ_๊ฑฐ๋ฆฌ = 7;
+
+ /** (10)
+ * ๊ต๋์ญ --- *2ํธ์ * --- ๊ฐ๋จ์ญ
+ * | |
+ * *3ํธ์ * (2) *์ ๋ถ๋น์ * (5)
+ * | |
+ * ๋จ๋ถํฐ๋ฏธ๋์ญ --- *3ํธ์ * --- ์์ฌ
+ (3) **/
+
+ @BeforeEach
+ public void setUp() {
+ super.setUp();
+
+ ๊ต๋์ญ = ์งํ์ฒ ์ญ_์์ฑ_์์ฒญ("๊ต๋์ญ").jsonPath().getLong("id");
+ ๊ฐ๋จ์ญ = ์งํ์ฒ ์ญ_์์ฑ_์์ฒญ("๊ฐ๋จ์ญ").jsonPath().getLong("id");
+ ์์ฌ์ญ = ์งํ์ฒ ์ญ_์์ฑ_์์ฒญ("์์ฌ์ญ").jsonPath().getLong("id");
+ ๋จ๋ถํฐ๋ฏธ๋์ญ = ์งํ์ฒ ์ญ_์์ฑ_์์ฒญ("๋จ๋ถํฐ๋ฏธ๋์ญ").jsonPath().getLong("id");
+ ์ฒญ๋๋ฆฌ์ญ = ์งํ์ฒ ์ญ_์์ฑ_์์ฒญ("์ฒญ๋๋ฆฌ์ญ").jsonPath().getLong("id");
+ ํ๊ธฐ์ญ = ์งํ์ฒ ์ญ_์์ฑ_์์ฒญ("ํ๊ธฐ์ญ").jsonPath().getLong("id");
+
+ ์ผํธ์ = ์งํ์ฒ _๋
ธ์ _์์ฑ_์์ฒญ("1ํธ์ ", "blue", ์ฒญ๋๋ฆฌ์ญ, ํ๊ธฐ์ญ, ์ฒญ๋๋ฆฌ์ญ_ํ๊ธฐ์ญ_๊ฑฐ๋ฆฌ).jsonPath().getLong("id");
+ ์ดํธ์ = ์งํ์ฒ _๋
ธ์ _์์ฑ_์์ฒญ("2ํธ์ ", "green", ๊ต๋์ญ, ๊ฐ๋จ์ญ, ๊ต๋์ญ_๊ฐ๋จ์ญ_๊ฑฐ๋ฆฌ).jsonPath().getLong("id");
+ ์ ๋ถ๋น์ = ์งํ์ฒ _๋
ธ์ _์์ฑ_์์ฒญ("์ ๋ถ๋น์ ", "red", ๊ฐ๋จ์ญ, ์์ฌ์ญ, ๊ฐ๋จ์ญ_์์ฌ์ญ_๊ฑฐ๋ฆฌ).jsonPath().getLong("id");
+ ์ผํธ์ = ์งํ์ฒ _๋
ธ์ _์์ฑ_์์ฒญ("3ํธ์ ", "orange", ๊ต๋์ญ, ๋จ๋ถํฐ๋ฏธ๋์ญ, ๊ต๋์ญ_๋จ๋ถํฐ๋ฏธ๋์ญ_๊ฑฐ๋ฆฌ).jsonPath().getLong("id");
+
+ ์งํ์ฒ _๋
ธ์ ์_์งํ์ฒ _๊ตฌ๊ฐ_์์ฑ_์์ฒญ(์ผํธ์ , createSectionCreateParams(๋จ๋ถํฐ๋ฏธ๋์ญ, ์์ฌ์ญ, ๋จ๋ถํฐ๋ฏธ๋์ญ_์์ฌ์ญ_๊ฑฐ๋ฆฌ));
+ }
+
+ /**
+ * Given ์งํ์ฒ ๋
ธ์ (2ํธ์ , 3ํธ์ , ์ ๋ถ๋น์ ) ์ ์์ฑํ๊ณ ์ญ, ๊ตฌ๊ฐ์ ์์ฑํ๋ค.
+ * When ์๋ก ๋ค๋ฅธ ๋ ์ญ์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ์กฐํํ๋ฉด
+ * Then ์ต๋จ ๊ฑฐ๋ฆฌ ์กฐํ์ ์ฑ๊ณตํ๋ค.
+ */
+ @DisplayName("์ต๋จ ๊ฑฐ๋ฆฌ ์กฐํํ๊ธฐ")
+ @Test
+ void searchShortestPath() {
+ // when
+ ExtractableResponse<Response> response = ๊ฒฝ๋ก_์กฐํ_์์ฒญ(๋จ๋ถํฐ๋ฏธ๋์ญ, ๊ฐ๋จ์ญ);
+
+ // then
+ assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
+ assertThat(response.jsonPath().getList("stations.id", Long.class)).containsExactly(๋จ๋ถํฐ๋ฏธ๋์ญ, ์์ฌ์ญ, ๊ฐ๋จ์ญ);
+ assertThat(response.jsonPath().getInt("distance")).isEqualTo(๋จ๋ถํฐ๋ฏธ๋์ญ_์์ฌ์ญ_๊ฑฐ๋ฆฌ + ๊ฐ๋จ์ญ_์์ฌ์ญ_๊ฑฐ๋ฆฌ);
+ }
+
+ /**
+ * Given ์งํ์ฒ ๋
ธ์ (2ํธ์ , 3ํธ์ , ์ ๋ถ๋น์ ) ์ ์์ฑํ๊ณ ์ญ, ๊ตฌ๊ฐ์ ์์ฑํ๋ค.
+ * ๊ธฐ์กด ๋
ธ์ ๊ณผ ์ฐ๊ฒฐ๋์ง ์๋ ์๋ก์ด ๋
ธ์ (1ํธ์ )์ ์์ฑํ๋ค.
+ * When ์ฐ๊ฒฐ ๋์ง ์์ ๋ ์ญ์ ์ต๋จ ๊ฑฐ๋ฆฌ ์กฐํ๋ฅผ ์์ฒญ ํ๋ฉด
+ * Then ์ต๋จ ๊ฑฐ๋ฆฌ ์กฐํ์ ์คํจํ๋ค.
+ */
+ @DisplayName("์ต๋จ ๊ฑฐ๋ฆฌ ์กฐํํ๊ธฐ - ์ฐ๊ฒฐ๋์ง ์์ ์ญ์ ์กฐํ ํ ๊ฒฝ์ฐ")
+ @Test
+ void searchShortestPathDoesNotExistPath() {
+ // when
+ ExtractableResponse<Response> response = ๊ฒฝ๋ก_์กฐํ_์์ฒญ(๊ฐ๋จ์ญ, ์ฒญ๋๋ฆฌ์ญ);
+
+ // then
+ assertThat(response.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());;
+ }
+
+ /**
+ * Given ์งํ์ฒ ๋
ธ์ (2ํธ์ , 3ํธ์ , ์ ๋ถ๋น์ ) ์ ์์ฑํ๊ณ ์ญ, ๊ตฌ๊ฐ์ ์์ฑํ๋ค.
+ * When ์ถ๋ฐ์ญ๊ณผ ๋์ฐฉ์ญ์ด ๋์ผํ๋ฐ ์ต๋จ ๊ฑฐ๋ฆฌ ์กฐํ๋ฅผ ์์ฒญํ๋ฉด
+ * Then ์ต๋จ ๊ฑฐ๋ฆฌ ์กฐํ์ ์คํจํ๋ค.
+ */
+ @DisplayName("์ต๋จ ๊ฑฐ๋ฆฌ ์กฐํํ๊ธฐ - ์ถ๋ฐ์ญ๊ณผ ๋์ฐฉ์ญ์ด ๋์ผํ ๊ฒฝ์ฐ")
+ @Test
+ void searchShortestPathSourceEqualsTarget() {
+ // when
+ ExtractableResponse<Response> response = ๊ฒฝ๋ก_์กฐํ_์์ฒญ(๊ฐ๋จ์ญ, ๊ฐ๋จ์ญ);
+
+ // then
+ assertThat(response.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());;
+ }
+
+ private Map<String, String> createSectionCreateParams(Long upStationId, Long downStationId, int distance) {
+ Map<String, String> params = new HashMap<>();
+ params.put("upStationId", upStationId + "");
+ params.put("downStationId", downStationId + "");
+ params.put("distance", distance + "");
+ return params;
+ }
+} | Java | ๋จ์ ํ
์คํธ์ ํฝ์ค์ณ ๊ด๋ฆฌ ๋ฐฉ๋ฒ์ฒ๋ผ ํด๋น ๊ฐ์ฒด๋ค์ ์ฌ์ฌ์ฉ์ด ํ์ํ ๊ฒฝ์ฐ ์ธ์ ํ
์คํธ์ฉ ํฝ์ค์ณ๋ฅผ ๋ง๋ค์ด์ ๊ด๋ฆฌํ ์ ๋ ์๊ฒ ๋ค์! |
@@ -1,7 +1,10 @@
package christmas;
+import christmas.controller.MainController;
+
public class Application {
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ MainController mainController = new MainController();
+ mainController.run();
}
} | Java | ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ new ์์ฝ์ด๋ณด๋ค ์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ์ฌ์ฉํด๋ณด์๋ฉด ์ด๋จ๊น์. ๊ฐ๋
์ฑ์ด๋ ๊ฐ์ฒด ์์ฑ ์ ์ ํ ๊ฐ์ ์ฌ๋ฌ ๊ฐ์ง ์ด์ ์ด ์์ด์.
https://tecoble.techcourse.co.kr/post/2020-05-26-static-factory-method/ |
@@ -0,0 +1,47 @@
+package christmas.controller;
+
+import java.util.Map;
+
+import christmas.view.*;
+import christmas.model.*;
+
+public class MainController {
+ private OrderMenu orderMenu;
+
+ public MainController() {
+ orderMenu = new OrderMenu();
+ }
+
+ public void run() {
+ orderMenu.setOrderDate(InputView.readDate());
+ setReadOrder();
+
+ OutputView.printPreview(orderMenu.getOrderDate());
+ OutputView.printOrder(orderMenu.getUserOrder());
+ int totalPrice = orderMenu.getTotalPrice();
+ OutputView.printTotalPrice(totalPrice);
+ OutputView.printPresent(totalPrice);
+ benefitControl(totalPrice);
+ }
+
+ private void benefitControl(int totalPrice) {
+ Benefit benefit = new Benefit(orderMenu.getOrderDate(), orderMenu.getCategoryCount(), totalPrice);
+ OutputView.printBenefits(benefit.getBenefits());
+
+ int totalBenefitPrice = benefit.getTotalBenefitPrice();
+ OutputView.printTotalBenefitPrice(totalBenefitPrice);
+ OutputView.printTotalPayment(totalPrice - totalBenefitPrice + benefit.getPresentPrice());
+ OutputView.printEventBadge(totalBenefitPrice);
+ }
+
+ private void setReadOrder() {
+ Map<String, Integer> orderMenu;
+ try {
+ orderMenu = InputView.readOrder();
+ this.orderMenu.setUserOrder(orderMenu);
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ setReadOrder();
+ }
+ }
+} | Java | OutputView๋ InputView์ ์
์ถ๋ ฅ ๊ธฐ๋ฅ๋ค์ ๋ชจ๋ ์คํํฑ ๋ฉ์๋๋ก ๊ตฌํํ์
จ๋ค์. ๋ณดํต์ ํด๋น ๊ฐ์ฒด๋ฅผ ์์ฑ ํ์ ์ผ๋ฐ ๋ฉ์๋๋ฅผ ํธ์ถํ๋๋ฐ ์คํํฑ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ ์ด์ ๊ฐ ์์ผ์ค๊น์? |
@@ -0,0 +1,97 @@
+package christmas.util;
+
+import java.util.Map;
+import java.util.List;
+
+public class Validator {
+ private final static char MIN_DIGIT = '0';
+ private final static char MAX_DIGIT = '9';
+ private final static char CHAR_HYPHEN = '-';
+ private final static char CHAR_COMMA = ',';
+ private final static int MIN_DAY = 1;
+ private final static int MAX_DAY = 31;
+ private final static int IS_TWO = 2;
+
+ public static void validateDate(String input) {
+ if (input.isEmpty()) {
+ throw new IllegalArgumentException(ErrorMessage.DATE_ERROR.get());
+ }
+ for (int i = Constant.ZERO.get(); i < input.length(); ++i) {
+ if (input.charAt(i) < MIN_DIGIT || input.charAt(i) > MAX_DIGIT) {
+ throw new IllegalArgumentException(ErrorMessage.DATE_ERROR.get());
+ }
+ }
+ int date = Integer.parseInt(input);
+ if (date < MIN_DAY || date > MAX_DAY) {
+ throw new IllegalArgumentException(ErrorMessage.DATE_ERROR.get());
+ }
+ }
+
+ public static void validateOrder(String input) {
+ // splitํ ๊ฒฝ์ฐ ๋ง์ง๋ง์ ๋น ๋ฌธ์์ด์ ์ฌ๋ผ์ง๋ฏ๋ก ๋ณ๋๋ก ์์ธ ์ฒ๋ฆฌ
+ if (input.isEmpty() || input.charAt(input.length() - Constant.ONE.get()) == CHAR_COMMA) {
+ throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get());
+ }
+ String[] splittedComma = input.split(Message.COMMA.get());
+ if (splittedComma.length == Constant.ZERO.get()) {
+ throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get());
+ }
+ for (String string : splittedComma) {
+ if (validateFormat(string)) {
+ throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get());
+ }
+ }
+ }
+
+ public static boolean validateMenu(Map<String, Integer> userOrder, List<Map<String, Integer>> menu) {
+ int[] countCategory = new int[Constant.CATEGORY_COUNT.get()];
+ for (Map.Entry<String, Integer> entry : userOrder.entrySet()) {
+ if (entry.getValue() <= Constant.ZERO.get()) {
+ return true;
+ }
+ int category = Utility.getCategory(entry.getKey(), menu);
+ if (category == Constant.NOT_IN_MENU.get()) {
+ return true;
+ }
+ countCategory[category] += entry.getValue();
+ }
+ if (validateCount(countCategory)) {
+ return true;
+ }
+ return false;
+ }
+
+ private static boolean validateCount(int[] countCategory) {
+ // ์๋ฃ๋ง ์ํจ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ
+ if (countCategory[Constant.EPPETIZER.get()] == Constant.ZERO.get()
+ && countCategory[Constant.MAINMENU.get()] == Constant.ZERO.get()
+ && countCategory[Constant.DESSERT.get()] == Constant.ZERO.get()) {
+ return true;
+ }
+ int totalCount = Constant.ZERO.get();
+ for (int count : countCategory) {
+ totalCount += count;
+ }
+ if (totalCount > Constant.MAX_MENU_COUNT.get()) {
+ return true;
+ }
+ return false;
+ }
+
+ private static boolean validateFormat(String input) {
+ if (input.isEmpty() || input.charAt(input.length() - Constant.ONE.get()) == CHAR_HYPHEN) {
+ return true;
+ }
+ String[] splittedHyphen = input.split(Message.HYPHEN.get());
+ if (splittedHyphen.length != IS_TWO) {
+ return true;
+ }
+ for (int i = Constant.ZERO.get(); i < splittedHyphen[Constant.VALUE_INDEX.get()].length(); ++i) {
+ if (splittedHyphen[Constant.VALUE_INDEX.get()].charAt(i) < MIN_DIGIT
+ || splittedHyphen[Constant.VALUE_INDEX.get()].charAt(i) > MAX_DIGIT) {
+ return true;
+ }
+ }
+ return false;
+ }
+} | Java | ์ ๋ ์ฑ๊ธฐ์ง ๋ชปํ ๋ถ๋ถ์ด๊ธดํฉ๋๋ค๋ง ํด๋น ๋ด์ฉ ๊ณต์ ํด๋๋ฆฌ์๋ฉด,
'์๋ฃ๋ง ์ฃผ๋ฌธ'๊ณผ ๊ฐ์ ์์ธ์ฒ๋ฆฌ๋ ๋ฌธ์์ด ํ์ฑ ์์ธ๊ฐ ์๋ ๋น์ฆ๋์ค ๋ก์ง ์์ธ์ฒ๋ฆฌ์ด๋ฏ๋ก
๊ฐ์ฒด์์ฑ์์ ์์ธ ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ๋ฉด ์ญํ ๋ถ๋ฆฌ ์์น์ ํจ์ฌ ๋ ์ข์ ์ฝ๋๊ฐ ๋ ๊ฒ ๊ฐ์ต๋๋ค~! |
@@ -1,7 +1,10 @@
package christmas;
+import christmas.controller.MainController;
+
public class Application {
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ MainController mainController = new MainController();
+ mainController.run();
}
} | Java | ์๊ฐํ์ง ๋ชปํ๋ ๊ฐ๋
์ธ๋ฐ ๊ฐ์ฌํฉ๋๋ค! ๋ฆฌํฉํ ๋งํ ๋ ์ ์ฉํ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค์ |
@@ -0,0 +1,39 @@
+package christmas.util;
+
+public enum Constant {
+ NOT_IN_MENU(-1),
+ EPPETIZER(0),
+ MAINMENU(1),
+ DESSERT(2),
+ BEVERAGE(3),
+ CATEGORY_COUNT(4),
+ ZERO(0),
+ ONE(1),
+ THOUSAND(1000),
+ HUNDRED(100),
+ CHRISTMAS_DAY(25),
+ MIN_BENEFIT_PRICE(10000),
+ PRESENT_COST(120000),
+ CHAMPAIGN_PRICE(25000),
+ WEEKDAYS(7),
+ FRIDAY(1),
+ SATURDAY(2),
+ SUNDAY(3),
+ EVENT_PRICE(2023),
+ SANTA(20000),
+ TREE(10000),
+ STAR(5000),
+ MAX_MENU_COUNT(20),
+ KEY_INDEX(0),
+ VALUE_INDEX(1);
+
+ private final int value;
+
+ Constant(int value) {
+ this.value = value;
+ }
+
+ public int get() {
+ return this.value;
+ }
+} | Java | Enum์ ๋ฉ๋ด์ ๋ณด ์ ์ฅํ๋ ์๋ฃ๊ตฌ์กฐ๋ก ์ฌ์ฉํ๋ฉด ์ฉ์ฒ๊ฐ ์ ๋ง์ ๊ฒ ๊ฐ์ต๋๋ค.
๋ฉ๋ด์ ํด๋นํ๋ ํ๊ธ์ด๋ฆ(String)๊ณผ ๊ฐ๊ฒฉ(Integer)๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ด์ง์. |
@@ -0,0 +1,68 @@
+package christmas.model;
+
+import christmas.util.Constant;
+import christmas.util.ErrorMessage;
+import christmas.util.Utility;
+import christmas.util.Validator;
+
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.List;
+
+public class OrderMenu {
+ private final List<Map<String, Integer>> menu;
+
+ private int orderDate;
+ private Map<String, Integer> userOrder;
+
+ public OrderMenu() {
+ menu = new ArrayList<>(List.of(
+ // EPPETIZER 0
+ Map.of("์์ก์ด์ํ", 6000, "ํํ์ค", 5500, "์์ ์๋ฌ๋", 8000),
+ // MAINMENU 1
+ Map.of("ํฐ๋ณธ์คํ
์ดํฌ", 55000, "๋ฐ๋นํ๋ฆฝ", 54000, "ํด์ฐ๋ฌผํ์คํ", 35000, "ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25000),
+ // DESSERT 2
+ Map.of("์ด์ฝ์ผ์ดํฌ", 15000, "์์ด์คํฌ๋ฆผ", 5000),
+ // BEVERAGE 3
+ Map.of("์ ๋ก์ฝ๋ผ", 3000, "๋ ๋์์ธ", 60000, "์ดํ์ธ", 25000)
+ ));
+ }
+
+ public int getOrderDate() {
+ return this.orderDate;
+ }
+
+ public Map<String, Integer> getUserOrder() {
+ return this.userOrder;
+ }
+
+ public int getTotalPrice() {
+ int totalPrice = Constant.ZERO.get();
+ for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) {
+ int category = Utility.getCategory(entry.getKey(), menu);
+ int price = menu.get(category).get(entry.getKey());
+ totalPrice += price * entry.getValue();
+ }
+ return totalPrice;
+ }
+
+ public int[] getCategoryCount() {
+ int[] categoryCount = new int[Constant.CATEGORY_COUNT.get()];
+ for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) {
+ int category = Utility.getCategory(entry.getKey(), menu);
+ categoryCount[category] += entry.getValue();
+ }
+ return categoryCount;
+ }
+
+ public void setOrderDate(int date) {
+ this.orderDate = date;
+ }
+
+ public void setUserOrder(Map<String, Integer> userOrder) {
+ if (Validator.validateMenu(userOrder, this.menu)) {
+ throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get());
+ }
+ this.userOrder = userOrder;
+ }
+} | Java | Map.of๋ก ์ด๊ธฐํํ๋ ํธํด๋ณด์ด๋ค์! ๋ฐฐ์ ์ต๋๋ค! ์ด๋๊ฐ์ ์จ๋จน์ด ๋ด์ผ๊ฒ ์ด์! |
@@ -0,0 +1,47 @@
+package christmas.controller;
+
+import java.util.Map;
+
+import christmas.view.*;
+import christmas.model.*;
+
+public class MainController {
+ private OrderMenu orderMenu;
+
+ public MainController() {
+ orderMenu = new OrderMenu();
+ }
+
+ public void run() {
+ orderMenu.setOrderDate(InputView.readDate());
+ setReadOrder();
+
+ OutputView.printPreview(orderMenu.getOrderDate());
+ OutputView.printOrder(orderMenu.getUserOrder());
+ int totalPrice = orderMenu.getTotalPrice();
+ OutputView.printTotalPrice(totalPrice);
+ OutputView.printPresent(totalPrice);
+ benefitControl(totalPrice);
+ }
+
+ private void benefitControl(int totalPrice) {
+ Benefit benefit = new Benefit(orderMenu.getOrderDate(), orderMenu.getCategoryCount(), totalPrice);
+ OutputView.printBenefits(benefit.getBenefits());
+
+ int totalBenefitPrice = benefit.getTotalBenefitPrice();
+ OutputView.printTotalBenefitPrice(totalBenefitPrice);
+ OutputView.printTotalPayment(totalPrice - totalBenefitPrice + benefit.getPresentPrice());
+ OutputView.printEventBadge(totalBenefitPrice);
+ }
+
+ private void setReadOrder() {
+ Map<String, Integer> orderMenu;
+ try {
+ orderMenu = InputView.readOrder();
+ this.orderMenu.setUserOrder(orderMenu);
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ setReadOrder();
+ }
+ }
+} | Java | MainController๊ฐ ํฌ๊ฒ ์ปจํธ๋กค(๊ฐ์ฒด๋ด ๋ฐ์ดํฐ๋ฅผ ์์ ๋ฐ ์ ์ฅ)ํ๋ ๋ชจ๋ธ์ orderMenu์ธ ๊ฒ ๊ฐ์๋ณด์
๋๋ค. ์ฆ ํด๋์ค ์ด๋ฆ์ด orderController์ด๋ฉด Model๊ณผ Controller๊ฐ์ ์ญํ ์์กด์ฑ์ ๋ ๋ง๋ ์๋ช
์ผ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,79 @@
+package christmas.model;
+
+import christmas.util.Constant;
+import christmas.util.Message;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Benefit {
+ private int orderDate;
+ private int[] categoryCount;
+ Map<String, Integer> benefits;
+
+ public Benefit(int orderDate, int[] categoryCount, int totalPrice) {
+ this.orderDate = orderDate;
+ this.categoryCount = categoryCount;
+ benefits = new HashMap<>();
+ if (totalPrice < Constant.MIN_BENEFIT_PRICE.get()) {
+ return;
+ }
+ christmasBenefit();
+ weekBenefit();
+ starBenefit();
+ if (totalPrice >= Constant.PRESENT_COST.get()) {
+ presentBenefit();
+ }
+ }
+
+ public Map<String, Integer> getBenefits() {
+ return this.benefits;
+ }
+
+ public int getTotalBenefitPrice() {
+ int totalBenefitPrice = Constant.ZERO.get();
+ for (Map.Entry<String, Integer> entry : this.benefits.entrySet()) {
+ int benefit = entry.getValue();
+ totalBenefitPrice += benefit;
+ }
+ return totalBenefitPrice;
+ }
+
+ public int getPresentPrice() {
+ if (this.benefits.containsKey(Message.PRESENT_BENEFIT.get())) {
+ return Constant.CHAMPAIGN_PRICE.get();
+ }
+ return Constant.ZERO.get();
+ }
+
+ private void christmasBenefit() {
+ if (orderDate > Constant.CHRISTMAS_DAY.get()) {
+ return;
+ }
+ int benefit = Constant.THOUSAND.get() + (orderDate - Constant.ONE.get()) * Constant.HUNDRED.get();
+ benefits.put(Message.D_DAY_BENEFIT.get(), benefit);
+ }
+
+ private void weekBenefit() {
+ if (orderDate % Constant.WEEKDAYS.get() == Constant.FRIDAY.get()
+ || orderDate % Constant.WEEKDAYS.get() == Constant.SATURDAY.get()) {
+ int benefit = Constant.EVENT_PRICE.get() * categoryCount[Constant.MAINMENU.get()];
+ benefits.put(Message.WEEKEND_BENEFIT.get(), benefit);
+ return;
+ }
+ int benefit = Constant.EVENT_PRICE.get() * categoryCount[Constant.DESSERT.get()];
+ benefits.put(Message.WEEKDAY_BENEFIT.get(), benefit);
+ }
+
+ private void starBenefit() {
+ if (orderDate != Constant.CHRISTMAS_DAY.get()
+ && orderDate % Constant.WEEKDAYS.get() != Constant.SUNDAY.get()) {
+ return;
+ }
+ benefits.put(Message.STAR_BENEFIT.get(), Constant.THOUSAND.get());
+ }
+
+ private void presentBenefit() {
+ benefits.put(Message.PRESENT_BENEFIT.get(), Constant.CHAMPAIGN_PRICE.get());
+ }
+} | Java | ์ ์ฒด์ ์ผ๋ก ํจ์๋ถ๋ฆฌ๊ฐ ์ ๋ผ์์ด์ ํจ์๋น ๋ผ์ธ์ด ์งง์ ํธ์ด๋ค์! ๊ต์ฅํ ๊ฐ๋
์ฑ ์ข์ ์ฝ๋๊ฐ ๋ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,39 @@
+package christmas.util;
+
+public enum Constant {
+ NOT_IN_MENU(-1),
+ EPPETIZER(0),
+ MAINMENU(1),
+ DESSERT(2),
+ BEVERAGE(3),
+ CATEGORY_COUNT(4),
+ ZERO(0),
+ ONE(1),
+ THOUSAND(1000),
+ HUNDRED(100),
+ CHRISTMAS_DAY(25),
+ MIN_BENEFIT_PRICE(10000),
+ PRESENT_COST(120000),
+ CHAMPAIGN_PRICE(25000),
+ WEEKDAYS(7),
+ FRIDAY(1),
+ SATURDAY(2),
+ SUNDAY(3),
+ EVENT_PRICE(2023),
+ SANTA(20000),
+ TREE(10000),
+ STAR(5000),
+ MAX_MENU_COUNT(20),
+ KEY_INDEX(0),
+ VALUE_INDEX(1);
+
+ private final int value;
+
+ Constant(int value) {
+ this.value = value;
+ }
+
+ public int get() {
+ return this.value;
+ }
+} | Java | ์์๋ค์ enum์ผ๋ก ๊ด๋ฆฌํ์๋ ๊ฒ์ ์ข์ ํฌ์ธํธ์ธ ๊ฒ ๊ฐ์ต๋๋ค. ํ์ง๋ง constant์ ๋ชจ๋ ์์๋ฅผ ๋ค ๋ฃ์ด๋ฒ๋ฆฌ์๋ฉด ๊ฐ๋ณ ์์๋ฅผ ์์ ํ๊ฑฐ๋ ๋ณ๊ฒฝํด์ผ ํ ๋ ์ฐพ๊ธฐ ์ด๋ ค์์ง ๊ฒ ๊ฐ์์. enum์ ์ข
๋ฅ๋ณ๋ก, ๋ฌธ๋งฅ๋ณ๋ก ๋ถ๋ฆฌํ์๋ ๊ฒ์ ์ถ์ฒ ๋๋ ค์. |
@@ -0,0 +1,79 @@
+package christmas.model;
+
+import christmas.util.Constant;
+import christmas.util.Message;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Benefit {
+ private int orderDate;
+ private int[] categoryCount;
+ Map<String, Integer> benefits;
+
+ public Benefit(int orderDate, int[] categoryCount, int totalPrice) {
+ this.orderDate = orderDate;
+ this.categoryCount = categoryCount;
+ benefits = new HashMap<>();
+ if (totalPrice < Constant.MIN_BENEFIT_PRICE.get()) {
+ return;
+ }
+ christmasBenefit();
+ weekBenefit();
+ starBenefit();
+ if (totalPrice >= Constant.PRESENT_COST.get()) {
+ presentBenefit();
+ }
+ }
+
+ public Map<String, Integer> getBenefits() {
+ return this.benefits;
+ }
+
+ public int getTotalBenefitPrice() {
+ int totalBenefitPrice = Constant.ZERO.get();
+ for (Map.Entry<String, Integer> entry : this.benefits.entrySet()) {
+ int benefit = entry.getValue();
+ totalBenefitPrice += benefit;
+ }
+ return totalBenefitPrice;
+ }
+
+ public int getPresentPrice() {
+ if (this.benefits.containsKey(Message.PRESENT_BENEFIT.get())) {
+ return Constant.CHAMPAIGN_PRICE.get();
+ }
+ return Constant.ZERO.get();
+ }
+
+ private void christmasBenefit() {
+ if (orderDate > Constant.CHRISTMAS_DAY.get()) {
+ return;
+ }
+ int benefit = Constant.THOUSAND.get() + (orderDate - Constant.ONE.get()) * Constant.HUNDRED.get();
+ benefits.put(Message.D_DAY_BENEFIT.get(), benefit);
+ }
+
+ private void weekBenefit() {
+ if (orderDate % Constant.WEEKDAYS.get() == Constant.FRIDAY.get()
+ || orderDate % Constant.WEEKDAYS.get() == Constant.SATURDAY.get()) {
+ int benefit = Constant.EVENT_PRICE.get() * categoryCount[Constant.MAINMENU.get()];
+ benefits.put(Message.WEEKEND_BENEFIT.get(), benefit);
+ return;
+ }
+ int benefit = Constant.EVENT_PRICE.get() * categoryCount[Constant.DESSERT.get()];
+ benefits.put(Message.WEEKDAY_BENEFIT.get(), benefit);
+ }
+
+ private void starBenefit() {
+ if (orderDate != Constant.CHRISTMAS_DAY.get()
+ && orderDate % Constant.WEEKDAYS.get() != Constant.SUNDAY.get()) {
+ return;
+ }
+ benefits.put(Message.STAR_BENEFIT.get(), Constant.THOUSAND.get());
+ }
+
+ private void presentBenefit() {
+ benefits.put(Message.PRESENT_BENEFIT.get(), Constant.CHAMPAIGN_PRICE.get());
+ }
+} | Java | ์ค์ ์ ํ๋ฆฌ์ผ์ด์
์์ ๋ฐฐ์ด ๋ณด๋ค๋ ๋ฆฌ์คํธ๋, ๋งต, ์
๋ฑ ์ปฌ๋ ์
๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ค๊ณ ํด์. ๊ทธ ์ด์ ๋ ์ง๋ ์ฃผ์ฐจ ํผ๋๋ฐฑ์ ๋ณด๋ฉด ๋ฐฐ์ด๋ณด๋ค ์ข ๋ ํธ๋ฆฌํ ์ฌ๋ฌ API๋ค์ ์ ๊ณตํ๊ธฐ ๋๋ฌธ์ธ๋ฐ์. ์ด์ธ์๋ ์ปฌ๋ ์
์ด ์ ํธ๋๋ ์ด์ ๋ ์ดํํฐ๋ธ ์๋ฐ ๋ฑ์์๋ ์ธ๊ธ๋์ด ์์ต๋๋ค.
https://incheol-jung.gitbook.io/docs/study/effective-java/undefined-3/2020-03-20-effective-28item |
@@ -0,0 +1,68 @@
+package christmas.model;
+
+import christmas.util.Constant;
+import christmas.util.ErrorMessage;
+import christmas.util.Utility;
+import christmas.util.Validator;
+
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.List;
+
+public class OrderMenu {
+ private final List<Map<String, Integer>> menu;
+
+ private int orderDate;
+ private Map<String, Integer> userOrder;
+
+ public OrderMenu() {
+ menu = new ArrayList<>(List.of(
+ // EPPETIZER 0
+ Map.of("์์ก์ด์ํ", 6000, "ํํ์ค", 5500, "์์ ์๋ฌ๋", 8000),
+ // MAINMENU 1
+ Map.of("ํฐ๋ณธ์คํ
์ดํฌ", 55000, "๋ฐ๋นํ๋ฆฝ", 54000, "ํด์ฐ๋ฌผํ์คํ", 35000, "ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25000),
+ // DESSERT 2
+ Map.of("์ด์ฝ์ผ์ดํฌ", 15000, "์์ด์คํฌ๋ฆผ", 5000),
+ // BEVERAGE 3
+ Map.of("์ ๋ก์ฝ๋ผ", 3000, "๋ ๋์์ธ", 60000, "์ดํ์ธ", 25000)
+ ));
+ }
+
+ public int getOrderDate() {
+ return this.orderDate;
+ }
+
+ public Map<String, Integer> getUserOrder() {
+ return this.userOrder;
+ }
+
+ public int getTotalPrice() {
+ int totalPrice = Constant.ZERO.get();
+ for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) {
+ int category = Utility.getCategory(entry.getKey(), menu);
+ int price = menu.get(category).get(entry.getKey());
+ totalPrice += price * entry.getValue();
+ }
+ return totalPrice;
+ }
+
+ public int[] getCategoryCount() {
+ int[] categoryCount = new int[Constant.CATEGORY_COUNT.get()];
+ for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) {
+ int category = Utility.getCategory(entry.getKey(), menu);
+ categoryCount[category] += entry.getValue();
+ }
+ return categoryCount;
+ }
+
+ public void setOrderDate(int date) {
+ this.orderDate = date;
+ }
+
+ public void setUserOrder(Map<String, Integer> userOrder) {
+ if (Validator.validateMenu(userOrder, this.menu)) {
+ throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get());
+ }
+ this.userOrder = userOrder;
+ }
+} | Java | ๋ฉ๋ด ์ด๋ฆ๊ณผ ๊ฐ๊ฒฉ์ ๊ฒฝ์ฐ ํ๋ ์ฝ๋ฉํ๊ธฐ๋ณด๋ค enum ๋ฑ์ผ๋ก ๋ถ๋ฆฌํ์ฌ ๊ด๋ฆฌํ๋ ๊ฒ์ด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,97 @@
+package christmas.util;
+
+import java.util.Map;
+import java.util.List;
+
+public class Validator {
+ private final static char MIN_DIGIT = '0';
+ private final static char MAX_DIGIT = '9';
+ private final static char CHAR_HYPHEN = '-';
+ private final static char CHAR_COMMA = ',';
+ private final static int MIN_DAY = 1;
+ private final static int MAX_DAY = 31;
+ private final static int IS_TWO = 2;
+
+ public static void validateDate(String input) {
+ if (input.isEmpty()) {
+ throw new IllegalArgumentException(ErrorMessage.DATE_ERROR.get());
+ }
+ for (int i = Constant.ZERO.get(); i < input.length(); ++i) {
+ if (input.charAt(i) < MIN_DIGIT || input.charAt(i) > MAX_DIGIT) {
+ throw new IllegalArgumentException(ErrorMessage.DATE_ERROR.get());
+ }
+ }
+ int date = Integer.parseInt(input);
+ if (date < MIN_DAY || date > MAX_DAY) {
+ throw new IllegalArgumentException(ErrorMessage.DATE_ERROR.get());
+ }
+ }
+
+ public static void validateOrder(String input) {
+ // splitํ ๊ฒฝ์ฐ ๋ง์ง๋ง์ ๋น ๋ฌธ์์ด์ ์ฌ๋ผ์ง๋ฏ๋ก ๋ณ๋๋ก ์์ธ ์ฒ๋ฆฌ
+ if (input.isEmpty() || input.charAt(input.length() - Constant.ONE.get()) == CHAR_COMMA) {
+ throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get());
+ }
+ String[] splittedComma = input.split(Message.COMMA.get());
+ if (splittedComma.length == Constant.ZERO.get()) {
+ throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get());
+ }
+ for (String string : splittedComma) {
+ if (validateFormat(string)) {
+ throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get());
+ }
+ }
+ }
+
+ public static boolean validateMenu(Map<String, Integer> userOrder, List<Map<String, Integer>> menu) {
+ int[] countCategory = new int[Constant.CATEGORY_COUNT.get()];
+ for (Map.Entry<String, Integer> entry : userOrder.entrySet()) {
+ if (entry.getValue() <= Constant.ZERO.get()) {
+ return true;
+ }
+ int category = Utility.getCategory(entry.getKey(), menu);
+ if (category == Constant.NOT_IN_MENU.get()) {
+ return true;
+ }
+ countCategory[category] += entry.getValue();
+ }
+ if (validateCount(countCategory)) {
+ return true;
+ }
+ return false;
+ }
+
+ private static boolean validateCount(int[] countCategory) {
+ // ์๋ฃ๋ง ์ํจ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ
+ if (countCategory[Constant.EPPETIZER.get()] == Constant.ZERO.get()
+ && countCategory[Constant.MAINMENU.get()] == Constant.ZERO.get()
+ && countCategory[Constant.DESSERT.get()] == Constant.ZERO.get()) {
+ return true;
+ }
+ int totalCount = Constant.ZERO.get();
+ for (int count : countCategory) {
+ totalCount += count;
+ }
+ if (totalCount > Constant.MAX_MENU_COUNT.get()) {
+ return true;
+ }
+ return false;
+ }
+
+ private static boolean validateFormat(String input) {
+ if (input.isEmpty() || input.charAt(input.length() - Constant.ONE.get()) == CHAR_HYPHEN) {
+ return true;
+ }
+ String[] splittedHyphen = input.split(Message.HYPHEN.get());
+ if (splittedHyphen.length != IS_TWO) {
+ return true;
+ }
+ for (int i = Constant.ZERO.get(); i < splittedHyphen[Constant.VALUE_INDEX.get()].length(); ++i) {
+ if (splittedHyphen[Constant.VALUE_INDEX.get()].charAt(i) < MIN_DIGIT
+ || splittedHyphen[Constant.VALUE_INDEX.get()].charAt(i) > MAX_DIGIT) {
+ return true;
+ }
+ }
+ return false;
+ }
+} | Java | ์ด๋ ๊ฒ ์ ํจ์ฑ ๊ฒ์ฌ ๋ก์ง์ ํ ๊ตฐ๋ฐ์์ ๊ด๋ฆฌํ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ธ ๊ฒ ๊ฐ์ต๋๋ค. ๊ฐ์ธ์ ์ธ ์๊ฐ์ด์ง๋ง ๋๋ฉ์ธ์์ ์ ํจ์ฑ ๊ฒ์ฌํ๊ฒ ๋๋ฉด ์ ํจ์ฑ ๊ฒ์ฌ ๋ก์ง๋ค์ด ํฉ์ด์ง๋ ๊ฒฝํฅ์ด ์์ด์ ๋ก์ง์ ๋ฐ๋ผ๊ฐ๊ธฐ ์ด๋ ค์ด๋ฐ ์ด๋ ๊ฒ ๋ชจ์๋๋ฉด ์ ํจ์ฑ ๊ฒ์ฌ ๋ฉ์๋๋ค๋ง ํ๋์ ๋ณผ ์ ์์ด์ ์ข์ ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,96 @@
+package christmas.view;
+
+import christmas.util.Constant;
+import christmas.util.Message;
+import java.util.Map;
+
+import java.text.DecimalFormat;
+
+public class OutputView {
+ public static void printPreview(int orderDate) {
+ System.out.println("12์ " + orderDate + "์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!");
+ System.out.println();
+ }
+
+ public static void printOrder(Map<String, Integer> userOrder) {
+ System.out.println("<์ฃผ๋ฌธ ๋ฉ๋ด>");
+ for (Map.Entry<String, Integer> entry : userOrder.entrySet()) {
+ System.out.println(entry.getKey() + Message.SPACE.get() + entry.getValue() + Message.COUNT.get());
+ }
+ System.out.println();
+ }
+
+ public static void printTotalPrice(int totalPrice) {
+ DecimalFormat decimalFormat = new DecimalFormat(Message.PRICE_FORMAT.get());
+
+ System.out.println("<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>");
+ String formattedNumber = decimalFormat.format(totalPrice);
+ System.out.println(formattedNumber + Message.WON.get());
+ System.out.println();
+ }
+
+ public static void printPresent(int totalPrice) {
+ System.out.println("<์ฆ์ ๋ฉ๋ด>");
+ if (totalPrice < Constant.PRESENT_COST.get()) {
+ System.out.println(Message.NOTHING.get());
+ System.out.println();
+ return;
+ }
+ System.out.println(Message.ONE_CHAMPAIGN.get());
+ System.out.println();
+ }
+
+ public static void printBenefits(Map<String, Integer> benefits) {
+ System.out.println("<ํํ ๋ด์ญ>");
+ if (benefits.isEmpty()) {
+ System.out.println(Message.NOTHING.get());
+ System.out.println();
+ return;
+ }
+ DecimalFormat decimalFormat = new DecimalFormat(Message.PRICE_FORMAT.get());
+ for (Map.Entry<String, Integer> entry : benefits.entrySet()) {
+ System.out.print(entry.getKey() + ": -");
+ String fomattedNumber = decimalFormat.format(entry.getValue());
+ System.out.println(fomattedNumber + Message.WON.get());
+ }
+ System.out.println();
+ }
+
+ public static void printTotalBenefitPrice(int totalBenefitPrice) {
+ System.out.println("<์ดํํ ๊ธ์ก>");
+ if (totalBenefitPrice == Constant.ZERO.get()) {
+ System.out.println(Message.NOTHING.get());
+ System.out.println();
+ return;
+ }
+ DecimalFormat decimalFormat = new DecimalFormat(Message.PRICE_FORMAT.get());
+ String formattedNumber = decimalFormat.format(totalBenefitPrice);
+ System.out.println(Message.HYPHEN.get() + formattedNumber + Message.WON.get());
+ System.out.println();
+ }
+
+ public static void printTotalPayment(int totalPayment) {
+ System.out.println("<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>");
+ DecimalFormat decimalFormat = new DecimalFormat(Message.PRICE_FORMAT.get());
+ String formattedNumber = decimalFormat.format(totalPayment);
+ System.out.println(formattedNumber + Message.WON.get());
+ System.out.println();
+ }
+
+ public static void printEventBadge(int totalBenefitPrice) {
+ System.out.println("<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>");
+ if (totalBenefitPrice >= Constant.SANTA.get()) {
+ System.out.println("์ฐํ");
+ return;
+ }
+ if (totalBenefitPrice >= Constant.TREE.get()) {
+ System.out.println("ํธ๋ฆฌ");
+ return;
+ }
+ if (totalBenefitPrice >= Constant.STAR.get()) {
+ System.out.println("๋ณ");
+ return;
+ }
+ System.out.println(Message.NOTHING.get());
+ }
+} | Java | ์ถ๋ ฅ ๋ทฐ ๋ฉ์๋์ ์ฃผ๋ฌธ์ด๋ ํ ์ธ ์ ์ฃผ๋ฌธ๊ธ์ก์ ๋ฐ์์ ์ฒ๋ฆฌ ํ ํํ์ด๋ ์ฆ์ ๋ฉ๋ด๋ฅผ ๊ณ์ฐํ๋ ๋ถ๊ธฐ ๋ก์ง์ด ๋ด๊ฒจ ์๋ค์. ์ถ๋ ฅ ๋ทฐ๋ ์ถ๋ ฅ๋ง์ ๋ํ ์ฑ
์์ ๊ฐ์ง๋ ๊ฒ์ด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. ํํ์ด๋ ์ฆ์ ๋ฉ๋ด๋ฅผ ๊ณ์ฐํ๋ ์ฑ
์์ ๊ฐ๊ฐ์ ๋๋ฉ์ธ ๊ฐ์ฒด๋ก ์ด๋ํ์๋ ๊ฒ ์ด๋จ๊น์? |
@@ -0,0 +1,135 @@
+package christmas;
+
+import christmas.model.OrderMenu;
+import christmas.util.Validator;
+
+import java.lang.reflect.Array;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.ArrayList;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ValidatorTest {
+ private List<Map<String, Integer>> menu = new ArrayList<>(List.of(
+ Map.of("์์ก์ด์ํ", 6000, "ํํ์ค", 5500, "์์ ์๋ฌ๋", 8000),
+ Map.of("ํฐ๋ณธ์คํ
์ดํฌ", 55000, "๋ฐ๋นํ๋ฆฝ", 54000, "ํด์ฐ๋ฌผํ์คํ", 35000, "ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25000),
+ Map.of("์ด์ฝ์ผ์ดํฌ", 15000, "์์ด์คํฌ๋ฆผ", 5000),
+ Map.of("์ ๋ก์ฝ๋ผ", 3000, "๋ ๋์์ธ", 60000, "์ดํ์ธ", 25000)
+ ));
+
+ @DisplayName("์
๋ ฅ๋ ๋ ์ง๊ฐ ๋น์ด์์ผ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
+ @Test
+ void isDateNoneTest() {
+ assertThatThrownBy(() -> Validator.validateDate(""))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @DisplayName("์
๋ ฅ๋ ๋ ์ง๊ฐ ์ซ์๊ฐ ์๋๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
+ @Test
+ void isDateInvalidTest() {
+ assertThatThrownBy(() -> Validator.validateDate("1a"))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @DisplayName("์
๋ ฅ๋ ๋ ์ง๊ฐ ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
+ @Test
+ void dateRangeTest() {
+ assertThatThrownBy(() -> Validator.validateDate("123"))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @DisplayName("์
๋ ฅ๋ ์ฃผ๋ฌธ์ด ๋น์ด์์ผ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
+ @Test
+ void isOrderNoneTest() {
+ assertThatThrownBy(() -> Validator.validateOrder(""))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @DisplayName("์
๋ ฅ๋ ์ฃผ๋ฌธ ์ค ๋น ๋ฌธ์์ด์ด ์์ผ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
+ @Test
+ void isParsedNoneTest() {
+ assertThatThrownBy(() -> Validator.validateOrder("ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,"))
+ .isInstanceOf(IllegalArgumentException.class);
+ assertThatThrownBy(() -> Validator.validateOrder(",ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1"))
+ .isInstanceOf(IllegalArgumentException.class);
+ assertThatThrownBy(() -> Validator.validateOrder("ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,"))
+ .isInstanceOf(IllegalArgumentException.class);
+ assertThatThrownBy(() -> Validator.validateOrder("ํด์ฐ๋ฌผํ์คํ-2,,๋ ๋์์ธ-1"))
+ .isInstanceOf(IllegalArgumentException.class);
+ assertThatThrownBy(() -> Validator.validateOrder(",,,"))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @DisplayName("์ฃผ๋ฌธ ํฌ๋งท์ด ์ฌ๋ฐ๋ฅด์ง ์์ผ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
+ @Test
+ void isFormatWrongTest() {
+ assertThatThrownBy(() -> Validator.validateOrder("ํด์ฐ๋ฌผํ์คํ"))
+ .isInstanceOf(IllegalArgumentException.class);
+ assertThatThrownBy(() -> Validator.validateOrder("ํด์ฐ๋ฌผํ์คํ-1-1,์ ๋ก์ฝ๋ผ-1"))
+ .isInstanceOf(IllegalArgumentException.class);
+ assertThatThrownBy(() -> Validator.validateOrder("ํด์ฐ๋ฌผํ์คํ-1,์ ๋ก์ฝ๋ผ-"))
+ .isInstanceOf(IllegalArgumentException.class);
+ assertThatThrownBy(() -> Validator.validateOrder("ํด์ฐ๋ฌผํ์คํ-1,---"))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @DisplayName("๊ฐ์์ ์ซ์๊ฐ ์๋ ๊ฐ์ด ๋ค์ด๊ฐ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
+ @Test
+ void isNotNumberTest() {
+ assertThatThrownBy(() -> Validator.validateOrder("ํด์ฐ๋ฌผํ์คํ-๋ ๋์์ธ"))
+ .isInstanceOf(IllegalArgumentException.class);
+ assertThatThrownBy(() -> Validator.validateOrder("ํด์ฐ๋ฌผํ์คํ-1,์ ๋ก์ฝ๋ผ-๋ ๋์์ธ"))
+ .isInstanceOf(IllegalArgumentException.class);
+ assertThatThrownBy(() -> Validator.validateOrder("ํด์ฐ๋ฌผํ์คํ-1,์ ๋ก์ฝ๋ผ-2๋ ๋์์ธ"))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @DisplayName("๋ฉ๋ดํ์ ์๋ ๋ฉ๋ด๋ฅผ ์ฃผ๋ฌธํ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
+ @Test
+ void isOrderInMenu() {
+ Map<String, Integer> order1 = Map.of("๋ ์๊ฟ", 1);
+ Map<String, Integer> order2 = Map.of("์์ก์ด์ํ", 2, "์ฐ์ ์ก์ฐ", 1);
+ Map<String, Integer> order3 = Map.of("์์ก์ด์ํ", 1, "์์ด์คํฌ๋ฆผ", 1);
+
+ assertThat(Validator.validateMenu(order1, menu)).isEqualTo(true);
+ assertThat(Validator.validateMenu(order2, menu)).isEqualTo(true);
+ assertThat(Validator.validateMenu(order3, menu)).isEqualTo(false);
+ }
+
+ @DisplayName("0๊ฐ ์ดํ์ธ ๊ฐ์๊ฐ ์๋ค๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
+ @Test
+ void isCountValid() {
+ Map<String, Integer> order1 = Map.of("์์ด์คํฌ๋ฆผ", 0);
+ Map<String, Integer> order2 = Map.of("์์ก์ด์ํ", 1, "์์ด์คํฌ๋ฆผ", 0);
+
+ assertThat(Validator.validateMenu(order1, menu)).isEqualTo(true);
+ assertThat(Validator.validateMenu(order2, menu)).isEqualTo(true);
+ }
+
+ @DisplayName("20๊ฐ๊ฐ ๋์ด๊ฐ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
+ @Test
+ void isUnderTwenty() {
+ Map<String, Integer> order3 = Map.of("์์ก์ด์ํ", 21);
+ Map<String, Integer> order4 = Map.of("์์ก์ด์ํ", 10, "์์ด์คํฌ๋ฆผ", 11);
+
+ assertThat(Validator.validateMenu(order3, menu)).isEqualTo(true);
+ assertThat(Validator.validateMenu(order4, menu)).isEqualTo(true);
+ }
+
+ @DisplayName("์๋ฃ๋ง ์ฃผ๋ฌธํ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
+ @Test
+ void isOnlyBeverage() {
+ Map<String, Integer> order3 = Map.of("์ ๋ก์ฝ๋ผ", 1);
+ Map<String, Integer> order4 = Map.of("์ ๋ก์ฝ๋ผ", 3, "๋ ๋์์ธ", 1);
+
+ assertThat(Validator.validateMenu(order3, menu)).isEqualTo(true);
+ assertThat(Validator.validateMenu(order4, menu)).isEqualTo(true);
+ }
+} | Java | ํ๋์ ํ
์คํธ๋ ํ๋์ ๊ธฐ๋ฅ ํน์ ์ฑ
์๋ง์ ํ
์คํธ ํ๋ ๊ฒ์ด ๋ ์ข์ ๊ฒ ๊ฐ์์. ์ผ๋ฐ์ ์ผ๋ก ํ๋์ ํ
์คํธ ๋ฉ์๋์์ ๋ฌด์์ ํ
์คํธํ๋ ค๋์ง ์ ๊ธฐ์ ํ ์ ์๊ธฐ ๋๋ฌธ์ ํ๋์ ํ
์คํธ์ ํ๋์ ๋จ์ธ๋ฌธ์ ์ฐ๋ ๊ฒ์ด ๊ถ์ฅ๋ฉ๋๋ค. |
@@ -0,0 +1,39 @@
+package christmas.util;
+
+public enum Constant {
+ NOT_IN_MENU(-1),
+ EPPETIZER(0),
+ MAINMENU(1),
+ DESSERT(2),
+ BEVERAGE(3),
+ CATEGORY_COUNT(4),
+ ZERO(0),
+ ONE(1),
+ THOUSAND(1000),
+ HUNDRED(100),
+ CHRISTMAS_DAY(25),
+ MIN_BENEFIT_PRICE(10000),
+ PRESENT_COST(120000),
+ CHAMPAIGN_PRICE(25000),
+ WEEKDAYS(7),
+ FRIDAY(1),
+ SATURDAY(2),
+ SUNDAY(3),
+ EVENT_PRICE(2023),
+ SANTA(20000),
+ TREE(10000),
+ STAR(5000),
+ MAX_MENU_COUNT(20),
+ KEY_INDEX(0),
+ VALUE_INDEX(1);
+
+ private final int value;
+
+ Constant(int value) {
+ this.value = value;
+ }
+
+ public int get() {
+ return this.value;
+ }
+} | Java | ์์๋ฅผ ํ๋์ enum์ ๊ด๋ฆฌํ์ ๊ฒ ๊ฐ์๋ฐ, enum์ ์ข ๋ถ๋ฆฌํด๋ณด๋ฉด ์ด๋จ๊น์? ๊ด๋ จ์ฑ์ด ์๋ ์์๋ค๋ผ๋ฆฌ ํ๊ตฐ๋ฐ ๋ชฐ๋ ค์๋ค๋ณด๋ ์ด์ํ๊ฒ ๋๊ปด์ง๋๋ค |
@@ -0,0 +1,68 @@
+package christmas.model;
+
+import christmas.util.Constant;
+import christmas.util.ErrorMessage;
+import christmas.util.Utility;
+import christmas.util.Validator;
+
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.List;
+
+public class OrderMenu {
+ private final List<Map<String, Integer>> menu;
+
+ private int orderDate;
+ private Map<String, Integer> userOrder;
+
+ public OrderMenu() {
+ menu = new ArrayList<>(List.of(
+ // EPPETIZER 0
+ Map.of("์์ก์ด์ํ", 6000, "ํํ์ค", 5500, "์์ ์๋ฌ๋", 8000),
+ // MAINMENU 1
+ Map.of("ํฐ๋ณธ์คํ
์ดํฌ", 55000, "๋ฐ๋นํ๋ฆฝ", 54000, "ํด์ฐ๋ฌผํ์คํ", 35000, "ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25000),
+ // DESSERT 2
+ Map.of("์ด์ฝ์ผ์ดํฌ", 15000, "์์ด์คํฌ๋ฆผ", 5000),
+ // BEVERAGE 3
+ Map.of("์ ๋ก์ฝ๋ผ", 3000, "๋ ๋์์ธ", 60000, "์ดํ์ธ", 25000)
+ ));
+ }
+
+ public int getOrderDate() {
+ return this.orderDate;
+ }
+
+ public Map<String, Integer> getUserOrder() {
+ return this.userOrder;
+ }
+
+ public int getTotalPrice() {
+ int totalPrice = Constant.ZERO.get();
+ for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) {
+ int category = Utility.getCategory(entry.getKey(), menu);
+ int price = menu.get(category).get(entry.getKey());
+ totalPrice += price * entry.getValue();
+ }
+ return totalPrice;
+ }
+
+ public int[] getCategoryCount() {
+ int[] categoryCount = new int[Constant.CATEGORY_COUNT.get()];
+ for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) {
+ int category = Utility.getCategory(entry.getKey(), menu);
+ categoryCount[category] += entry.getValue();
+ }
+ return categoryCount;
+ }
+
+ public void setOrderDate(int date) {
+ this.orderDate = date;
+ }
+
+ public void setUserOrder(Map<String, Integer> userOrder) {
+ if (Validator.validateMenu(userOrder, this.menu)) {
+ throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get());
+ }
+ this.userOrder = userOrder;
+ }
+} | Java | menu๋ฅผ list์ ๊ด๋ฆฌํ์
จ๊ตฐ์
์ ๋ต์ ์๋๊ฒ ๊ฐ๋ค๋ง ์ฐํ
์ฝ์์ enum์ ์ฌ์ฉํ๋ผ๊ณ ๊ถ์ฅํ์ผ๋ ์ด๋ฐ ๋ฉ๋ด๋ค์ enum์์ ๊ด๋ฆฌํ๋ฉด ์ข ๋ ์ข์ง ์์๊น ์๊ฐ์ด ๋ญ๋๋ค! |
@@ -0,0 +1,68 @@
+package christmas.model;
+
+import christmas.util.Constant;
+import christmas.util.ErrorMessage;
+import christmas.util.Utility;
+import christmas.util.Validator;
+
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.List;
+
+public class OrderMenu {
+ private final List<Map<String, Integer>> menu;
+
+ private int orderDate;
+ private Map<String, Integer> userOrder;
+
+ public OrderMenu() {
+ menu = new ArrayList<>(List.of(
+ // EPPETIZER 0
+ Map.of("์์ก์ด์ํ", 6000, "ํํ์ค", 5500, "์์ ์๋ฌ๋", 8000),
+ // MAINMENU 1
+ Map.of("ํฐ๋ณธ์คํ
์ดํฌ", 55000, "๋ฐ๋นํ๋ฆฝ", 54000, "ํด์ฐ๋ฌผํ์คํ", 35000, "ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25000),
+ // DESSERT 2
+ Map.of("์ด์ฝ์ผ์ดํฌ", 15000, "์์ด์คํฌ๋ฆผ", 5000),
+ // BEVERAGE 3
+ Map.of("์ ๋ก์ฝ๋ผ", 3000, "๋ ๋์์ธ", 60000, "์ดํ์ธ", 25000)
+ ));
+ }
+
+ public int getOrderDate() {
+ return this.orderDate;
+ }
+
+ public Map<String, Integer> getUserOrder() {
+ return this.userOrder;
+ }
+
+ public int getTotalPrice() {
+ int totalPrice = Constant.ZERO.get();
+ for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) {
+ int category = Utility.getCategory(entry.getKey(), menu);
+ int price = menu.get(category).get(entry.getKey());
+ totalPrice += price * entry.getValue();
+ }
+ return totalPrice;
+ }
+
+ public int[] getCategoryCount() {
+ int[] categoryCount = new int[Constant.CATEGORY_COUNT.get()];
+ for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) {
+ int category = Utility.getCategory(entry.getKey(), menu);
+ categoryCount[category] += entry.getValue();
+ }
+ return categoryCount;
+ }
+
+ public void setOrderDate(int date) {
+ this.orderDate = date;
+ }
+
+ public void setUserOrder(Map<String, Integer> userOrder) {
+ if (Validator.validateMenu(userOrder, this.menu)) {
+ throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get());
+ }
+ this.userOrder = userOrder;
+ }
+} | Java | ํด๋์ค๋ช
์ OrderMenu์ธ๋ฐ orderDate๋ฅผ ์ธ์คํด์ค ๋ณ์๋ก ๊ด๋ฆฌํ๋๊ฒ ์กฐ๊ธ ์ด์ํ๊ฒ ๋๊ปด์ง๋๋ค.
ํด๋์ค ๋ถ๋ฆฌ๋ฅผ ํตํด orderDate์ ๊ด๋ จํ ํด๋์ค๋ฅผ ๋ง๋ค๊ฑฐ๋, OrderMenu์ ํด๋์ค๋ช
์ด ๋ฉ๋ด์ ๋ ์ง๋ฅผ ๋ค ํฌํจํ๊ฒ๋ ์๊ฐ์ด ๋ค๊ฒ๋ ๋ณ๊ฒฝํด๋ณด๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,47 @@
+package christmas.controller;
+
+import java.util.Map;
+
+import christmas.view.*;
+import christmas.model.*;
+
+public class MainController {
+ private OrderMenu orderMenu;
+
+ public MainController() {
+ orderMenu = new OrderMenu();
+ }
+
+ public void run() {
+ orderMenu.setOrderDate(InputView.readDate());
+ setReadOrder();
+
+ OutputView.printPreview(orderMenu.getOrderDate());
+ OutputView.printOrder(orderMenu.getUserOrder());
+ int totalPrice = orderMenu.getTotalPrice();
+ OutputView.printTotalPrice(totalPrice);
+ OutputView.printPresent(totalPrice);
+ benefitControl(totalPrice);
+ }
+
+ private void benefitControl(int totalPrice) {
+ Benefit benefit = new Benefit(orderMenu.getOrderDate(), orderMenu.getCategoryCount(), totalPrice);
+ OutputView.printBenefits(benefit.getBenefits());
+
+ int totalBenefitPrice = benefit.getTotalBenefitPrice();
+ OutputView.printTotalBenefitPrice(totalBenefitPrice);
+ OutputView.printTotalPayment(totalPrice - totalBenefitPrice + benefit.getPresentPrice());
+ OutputView.printEventBadge(totalBenefitPrice);
+ }
+
+ private void setReadOrder() {
+ Map<String, Integer> orderMenu;
+ try {
+ orderMenu = InputView.readOrder();
+ this.orderMenu.setUserOrder(orderMenu);
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ setReadOrder();
+ }
+ }
+} | Java | ์ฌ์ค ์ ๊ฐ ์์ฒญ ์ค๋ ์ฐพ์๋ณด๊ณ ๊ณ ๋ฏผํ ๋ถ๋ถ์ด๊ธด ํ๋ฐ ๋ณดํต ์ฌ์ฉํ๋ ๋ฐฉ์์ ์ ๋ชฐ๋ผ์ ์ ๊ฐ ์๊ฐํ ๋๋ก ํ์ต๋๋ค. ๊ฐ์ธ์ ์ผ๋ก ์๊ฐํ View ํด๋์ค๋ ์ํ๋ฅผ ๊ฐ๊ณ ์์ง ์๊ณ ์ธ์คํด์คํ๋ฅผ ํ ํ์๊ฐ ์๋ ๊ฒ ๊ฐ์์ ์คํํฑ ํด๋์ค๋ ์ฑ๊ธํค ํจํด์ ์ฃผ๋ก ์ฌ์ฉํ๋ค๊ณ ๋ดค์๋๋ฐ, ๊ณผ์ ์์ View๋ฅผ ์์ํ ์ผ์ ์์ ๊ฒ ๊ฐ์์ ์คํํฑ ํด๋์ค๋ก ๊ตฌํํ์ต๋๋ค. ์ผ๋ฐ์ ์ผ๋ก๋ ์ฑ๊ธํค ํจํด์ ์ฌ์ฉํ๋ ๊ฑธ๊น์? |
@@ -0,0 +1,39 @@
+package christmas.util;
+
+public enum Constant {
+ NOT_IN_MENU(-1),
+ EPPETIZER(0),
+ MAINMENU(1),
+ DESSERT(2),
+ BEVERAGE(3),
+ CATEGORY_COUNT(4),
+ ZERO(0),
+ ONE(1),
+ THOUSAND(1000),
+ HUNDRED(100),
+ CHRISTMAS_DAY(25),
+ MIN_BENEFIT_PRICE(10000),
+ PRESENT_COST(120000),
+ CHAMPAIGN_PRICE(25000),
+ WEEKDAYS(7),
+ FRIDAY(1),
+ SATURDAY(2),
+ SUNDAY(3),
+ EVENT_PRICE(2023),
+ SANTA(20000),
+ TREE(10000),
+ STAR(5000),
+ MAX_MENU_COUNT(20),
+ KEY_INDEX(0),
+ VALUE_INDEX(1);
+
+ private final int value;
+
+ Constant(int value) {
+ this.value = value;
+ }
+
+ public int get() {
+ return this.value;
+ }
+} | Java | enum์ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ ๋ชฐ๋๋ ๊ฒ ๊ฐ์์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,47 @@
+package christmas.controller;
+
+import java.util.Map;
+
+import christmas.view.*;
+import christmas.model.*;
+
+public class MainController {
+ private OrderMenu orderMenu;
+
+ public MainController() {
+ orderMenu = new OrderMenu();
+ }
+
+ public void run() {
+ orderMenu.setOrderDate(InputView.readDate());
+ setReadOrder();
+
+ OutputView.printPreview(orderMenu.getOrderDate());
+ OutputView.printOrder(orderMenu.getUserOrder());
+ int totalPrice = orderMenu.getTotalPrice();
+ OutputView.printTotalPrice(totalPrice);
+ OutputView.printPresent(totalPrice);
+ benefitControl(totalPrice);
+ }
+
+ private void benefitControl(int totalPrice) {
+ Benefit benefit = new Benefit(orderMenu.getOrderDate(), orderMenu.getCategoryCount(), totalPrice);
+ OutputView.printBenefits(benefit.getBenefits());
+
+ int totalBenefitPrice = benefit.getTotalBenefitPrice();
+ OutputView.printTotalBenefitPrice(totalBenefitPrice);
+ OutputView.printTotalPayment(totalPrice - totalBenefitPrice + benefit.getPresentPrice());
+ OutputView.printEventBadge(totalBenefitPrice);
+ }
+
+ private void setReadOrder() {
+ Map<String, Integer> orderMenu;
+ try {
+ orderMenu = InputView.readOrder();
+ this.orderMenu.setUserOrder(orderMenu);
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ setReadOrder();
+ }
+ }
+} | Java | ๋ค์ ๋ณด๋๊น MainController๊ฐ ์ ์ ํ ์ด๋ฆ์ด ์๋ ๊ฒ ๊ฐ๋ค์, ๋ณดํต ๋ชจ๋ธ๊ณผ ์ปจํธ๋กค๋ฌ์ ๊ด๊ณ๊ฐ 1๋ 1๋ก ์ด๋ฃจ์ด์ง๋ค๊ณ ๋ณด๋ฉด ๋๋ ๊ฑธ๊น์? |
@@ -0,0 +1,39 @@
+package christmas.util;
+
+public enum Constant {
+ NOT_IN_MENU(-1),
+ EPPETIZER(0),
+ MAINMENU(1),
+ DESSERT(2),
+ BEVERAGE(3),
+ CATEGORY_COUNT(4),
+ ZERO(0),
+ ONE(1),
+ THOUSAND(1000),
+ HUNDRED(100),
+ CHRISTMAS_DAY(25),
+ MIN_BENEFIT_PRICE(10000),
+ PRESENT_COST(120000),
+ CHAMPAIGN_PRICE(25000),
+ WEEKDAYS(7),
+ FRIDAY(1),
+ SATURDAY(2),
+ SUNDAY(3),
+ EVENT_PRICE(2023),
+ SANTA(20000),
+ TREE(10000),
+ STAR(5000),
+ MAX_MENU_COUNT(20),
+ KEY_INDEX(0),
+ VALUE_INDEX(1);
+
+ private final int value;
+
+ Constant(int value) {
+ this.value = value;
+ }
+
+ public int get() {
+ return this.value;
+ }
+} | Java | ์์์ฒ๋ฆฌ์๋ง ๊ธ๊ธํด์ ๋ถ๋ฆฌํ์ง ๋ชปํ๋๋ฐ ์ง๊ธ ๋ณด๋ ๊ฐ๋
์ฑ ์ธก๋ฉด์์๋ ์ข์ง ์์ ๊ฒ ๊ฐ๋ค์. ์ด ๋ถ๋ถ์ ๊ผญ ์์ ํด์ผ๊ฒ ์ต๋๋ค ๊ฐ์ฌํฉ๋๋ค! |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.