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
์ƒ์ˆ˜์ฒ˜๋ฆฌ์—๋งŒ ๊ธ‰๊ธ‰ํ•ด์„œ ๋ถ„๋ฆฌํ•˜์ง€ ๋ชปํ–ˆ๋Š”๋ฐ ์ง€๊ธˆ ๋ณด๋‹ˆ ๊ฐ€๋…์„ฑ ์ธก๋ฉด์—์„œ๋„ ์ข‹์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™๋„ค์š”. ์ด ๋ถ€๋ถ„์€ ๊ผญ ์ˆ˜์ •ํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!