code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,68 @@ +import { ERROR_MESSAGES } from '../constants/messages.js'; +import { SYMBOLS } from '../constants/system.js'; +import validationErrorHandler from '../errors/index.js'; +import { isValidMenuName } from '../validators/index.js'; +import { isOnlyBeverage } from '../validators/is-valid-menu/name.js'; +import { + isValidEachMenuQuantity, + isValidTotalMenuQuantity, +} from '../validators/is-valid-menu/quantity.js'; + +class Order { + #orderList = new Set(); + + constructor(orderInput) { + this.#validate(orderInput); + this.#orderList = this.#parse(orderInput); + } + + /** + * ์‚ฌ์šฉ์ž ์ž…๋ ฅํ•œ ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜๋ฅผ ํŒŒ์‹ฑํ•˜์—ฌ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ + * @param {string} orderInput - ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ชจ๋“  ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ + * @returns {{ key: string, value: string }[]} - ์ฃผ๋ฌธ์ด ํŒŒ์‹ฑ๋œ ๋ฐฐ์—ด + */ + #parse(orderInput) { + this.#splitOrder(orderInput); + + return [...this.#orderList]; + } + + /** + * orderInput ๋‚ด ๊ฐœ๋ณ„ ์ฃผ๋ฌธ๋“ค์„ ํŒŒ์‹ฑ + * @param {string} orderInput - ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ชจ๋“  ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ + * @returns {Set<{ menu: string, quantity: number }>} - ์ฃผ๋ฌธ์ด ํŒŒ์‹ฑ๋œ Set + */ + #splitOrder(orderInput) { + return orderInput.split(SYMBOLS.comma).map(order => this.#splitMenu(order)); + } + + /** + * ์ฃผ๋ฌธ ๋ฉ”๋‰ด๋ช…๊ณผ ๊ฐœ์ˆ˜๋ฅผ orderList Set์— ์ถ”๊ฐ€ + * @param {string} order ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ํŠน์ • ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ + */ + #splitMenu(order) { + const [menu, quantity] = order.split(SYMBOLS.dash); + + this.#orderList.add({ menu: menu.trim(), quantity: Number(quantity) }); + } + + #validate(orderInput) { + if (!isValidMenuName(orderInput) || !isValidEachMenuQuantity(orderInput)) { + validationErrorHandler(ERROR_MESSAGES.invalidMenu); + } + + if (!isValidTotalMenuQuantity(orderInput)) { + validationErrorHandler(ERROR_MESSAGES.exceedQuantity); + } + + if (isOnlyBeverage(orderInput)) { + validationErrorHandler(ERROR_MESSAGES.onlyBeverage); + } + } + + getOrderList() { + return this.#orderList; + } +} + +export default Order;
JavaScript
์ œ ์ฝ”๋“œ๋Š” ์Šค์Šค๋กœ ๋‹ค์†Œ ๋‚œ์žกํ•œ ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“œ๋Š”๋ฐ, ๋‹คํ–‰์ž…๋‹ˆ๋‹ค ํ•˜ํ•˜
@@ -0,0 +1,15 @@ +import { UNITS } from '../constants/system.js'; + +/** + * number type์˜ ๊ฐ€๊ฒฉ์„ ์ž…๋ ฅ๋ฐ›์•„ ๊ฐ€๊ฒฉ์„ ๋ณ€ํ™˜ ํ›„ '์›'์„ ํฌํ•จํ•œ string type์œผ๋กœ ๋ณ€ํ™˜ + * 20000์ด๋ผ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด '20,000์›'์„ ๋ฐ˜ํ™˜ + * @param {number} price ๊ฐ€๊ฒฉ + * @returns {string} + */ +const priceFormatter = price => { + const formattedPrice = new Intl.NumberFormat().format(price); + + return `${formattedPrice}${UNITS.won}`; +}; + +export default priceFormatter;
JavaScript
์ €๋Š” ์ด ๋ถ€๋ถ„์„ [Number.toLocaleString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString)์„ ์ ์šฉํ–ˆ๋Š”๋ฐ, ์ˆซ์ž ํ˜•์‹๊ณผ ๊ด€๋ จ๋œ ๋ถ€๋ถ„์€ Intl ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ๋” ์ •ํ™•ํ•  ๊ฒƒ ๊ฐ™๋„ค์š” ๐Ÿ‘ (๋‚ด๋ถ€์ ์œผ๋กœ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฑฐ ๊ฐ™์•„์š” ใ…Žใ…Ž)
@@ -0,0 +1,24 @@ +const MENUS = Object.freeze({ + appetizer: { + ์–‘์†ก์ด์ˆ˜ํ”„: 6_000, + ํƒ€ํŒŒ์Šค: 5_500, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: 8_000, + }, + main: { + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: 55_000, + ๋ฐ”๋น„ํ๋ฆฝ: 54_000, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: 35_000, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: 25_000, + }, + dessert: { + ์ดˆ์ฝ”์ผ€์ดํฌ: 15_000, + ์•„์ด์Šคํฌ๋ฆผ: 5_000, + }, + beverage: { + ์ œ๋กœ์ฝœ๋ผ: 3_000, + ๋ ˆ๋“œ์™€์ธ: 60_000, + ์ƒดํŽ˜์ธ: 25_000, + }, +}); + +export default MENUS;
JavaScript
ํ˜น์‹œ menu๋„ data ํด๋”์— ๋„ฃ๋Š” ๋ฐฉํ–ฅ๋„ ์ƒ๊ฐํ•ด ๋ณด์…จ์„๊นŒ์š”? ์ € ๊ฐ™์€ ๊ฒฝ์šฐ์—” ์ƒ์ˆ˜๋กœ ๋ฌถ๊ธฐ์—” ๊ฑธ๋ฆฌ๋Š” ๋ถ€๋ถ„๋“ค์ด ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™์•„์„œ์š”! ๊ฐœ์ธ์ ์œผ๋กœ ๊ถ๊ธˆํ•ด์„œ ์—ฌ์ญค๋ด…๋‹ˆ๋‹ค! ๐Ÿ™‚
@@ -1,5 +1,15 @@ +import EventController from './controller/index.js'; + class App { - async run() {} + #controller; + + constructor() { + this.#controller = new EventController(); + } + + async run() { + await this.#controller.startEvent(); + } } export default App;
JavaScript
App์ด ์ •๋ง ๊น”๋”ํ•˜๋„ค์š”..! ๋’ค์— ์ˆจ๊ฒจ์ง„ ์ฝ”๋“œ๋“ค ๋ณด๊ณ  ๋งŽ์ด ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค! ๐Ÿƒโ€โ™‚๏ธ
@@ -0,0 +1,52 @@ +import { PROMOTION_TITLES, TIME, UNITS } from './system.js'; + +export const DISCOUNT_PRICES = Object.freeze({ + dDay: 1_000, + dDayInc: 100, + week: 2_023, + special: 1_000, +}); + +export const BADGES = Object.freeze({ + star: { title: '๋ณ„', price: 5_000 }, + tree: { title: 'ํŠธ๋ฆฌ', price: 10_000 }, + santa: { title: '์‚ฐํƒ€', price: 20_000 }, +}); + +export const TITLES = Object.freeze({ + dDay: `${PROMOTION_TITLES[TIME.month]} ๋””๋ฐ์ด ํ• ์ธ`, + weekday: 'ํ‰์ผ ํ• ์ธ', + weekend: '์ฃผ๋ง ํ• ์ธ', + special: 'ํŠน๋ณ„ ํ• ์ธ', + giveaway: '์ฆ์ • ์ด๋ฒคํŠธ', +}); + +export const GIVEAWAYS = Object.freeze({ + giveaway: '์ƒดํŽ˜์ธ', + giveawayUnit: 1, + giveawayPrice: 120_000, +}); + +export const ORDER = Object.freeze({ + minPrice: 10_000, + minQuantity: 1, + maxQuantity: 20, +}); + +export const DATES = Object.freeze({ + startDate: 1, + endDate: 31, + christmas: 25, + specialDayIndex: 0, + weekendIndex: 5, +}); + +export const BENEFITS = Object.freeze({ + menu: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + preTotalPrice: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + giveaway: '<์ฆ์ • ๋ฉ”๋‰ด>', + benefitList: '<ํ˜œํƒ ๋‚ด์—ญ>', + totalBenefitPrice: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + totalPrice: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + badge: `<${TIME.month}${UNITS.month} ์ด๋ฒคํŠธ ๋ฐฐ์ง€>`, +});
JavaScript
`dDayInc`๋ณด๋‹ค๋Š” ์ข€๋” ๊ตฌ์ฒด์ ์ธ ์ƒ์ˆ˜๋ช…์€ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,52 @@ +import { PROMOTION_TITLES, TIME, UNITS } from './system.js'; + +export const DISCOUNT_PRICES = Object.freeze({ + dDay: 1_000, + dDayInc: 100, + week: 2_023, + special: 1_000, +}); + +export const BADGES = Object.freeze({ + star: { title: '๋ณ„', price: 5_000 }, + tree: { title: 'ํŠธ๋ฆฌ', price: 10_000 }, + santa: { title: '์‚ฐํƒ€', price: 20_000 }, +}); + +export const TITLES = Object.freeze({ + dDay: `${PROMOTION_TITLES[TIME.month]} ๋””๋ฐ์ด ํ• ์ธ`, + weekday: 'ํ‰์ผ ํ• ์ธ', + weekend: '์ฃผ๋ง ํ• ์ธ', + special: 'ํŠน๋ณ„ ํ• ์ธ', + giveaway: '์ฆ์ • ์ด๋ฒคํŠธ', +}); + +export const GIVEAWAYS = Object.freeze({ + giveaway: '์ƒดํŽ˜์ธ', + giveawayUnit: 1, + giveawayPrice: 120_000, +}); + +export const ORDER = Object.freeze({ + minPrice: 10_000, + minQuantity: 1, + maxQuantity: 20, +}); + +export const DATES = Object.freeze({ + startDate: 1, + endDate: 31, + christmas: 25, + specialDayIndex: 0, + weekendIndex: 5, +}); + +export const BENEFITS = Object.freeze({ + menu: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + preTotalPrice: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + giveaway: '<์ฆ์ • ๋ฉ”๋‰ด>', + benefitList: '<ํ˜œํƒ ๋‚ด์—ญ>', + totalBenefitPrice: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + totalPrice: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + badge: `<${TIME.month}${UNITS.month} ์ด๋ฒคํŠธ ๋ฐฐ์ง€>`, +});
JavaScript
์ƒˆํ•ด ์ด๋ฒคํŠธ๊นŒ์ง€๋Š” ์ƒ๊ฐ์•ˆํ•ด๋ดค๋Š”๋ฐ ๊ผผ๊ผผํ•˜์‹ญ๋‹ˆ๋‹ค!๐Ÿ‘
@@ -0,0 +1,15 @@ +import { UNITS } from '../constants/system.js'; + +/** + * number type์˜ ๊ฐ€๊ฒฉ์„ ์ž…๋ ฅ๋ฐ›์•„ ๊ฐ€๊ฒฉ์„ ๋ณ€ํ™˜ ํ›„ '์›'์„ ํฌํ•จํ•œ string type์œผ๋กœ ๋ณ€ํ™˜ + * 20000์ด๋ผ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด '20,000์›'์„ ๋ฐ˜ํ™˜ + * @param {number} price ๊ฐ€๊ฒฉ + * @returns {string} + */ +const priceFormatter = price => { + const formattedPrice = new Intl.NumberFormat().format(price); + + return `${formattedPrice}${UNITS.won}`; +}; + +export default priceFormatter;
JavaScript
๋งž์Šต๋‹ˆ๋‹ค! ์„ํ‘œ๋‹˜ ๋ง์”€์ฒ˜๋Ÿผ ๊ฒฐ๊ตญ `toLocaleString()`์ด ๊ถ๊ทน์ ์œผ๋กœ `Intl.NumberFormat` API๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๊ตฌ์š” :) ์–ด๋–ป๊ฒŒ๋ณด๋ฉด ์ข€๋” ์†์‰ฌ์šด ์‚ฌ์šฉ์„ ์œ„ํ•œ `toLocaleString()` ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ์ด ๋” ๋ชฉ์ ์— ๋ถ€ํ•ฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“œ๋„ค์š” :)
@@ -0,0 +1,36 @@ +import EventCalendar from '../../src/domain/EventCalendar'; +import { CHRISTMAS_D_DAY, GIFT, SPECIAL } from '../../src/domain/Events'; + +import { + EVENT_MONTH, + EVENT_NAMES, + EVENT_YEAR, +} from '../../src/constants/event'; +import { MAIN_COURSES } from '../../src/constants/menus'; + +describe('EventCalendar ํ…Œ์ŠคํŠธ', () => { + test('getEventBenefit ๊ธฐ๋Šฅ ํ…Œ์ŠคํŠธ', () => { + // given + const YEAR = EVENT_YEAR; + const MONTH = EVENT_MONTH; + const DATE = 25; + const TOTAL_PRICE = 130_000; + const ORDER_CATEGORIES = [MAIN_COURSES]; + + const RESULT = { + [EVENT_NAMES.CHRISTMAS_D_DAY]: CHRISTMAS_D_DAY.getEvent().getBenefit, + [EVENT_NAMES.SPECIAL]: SPECIAL.getEvent().getBenefit, + [EVENT_NAMES.GIFT]: GIFT.getEvent().getBenefit, + }; + + // when + const eventCalendar = new EventCalendar(YEAR, MONTH, DATE); + eventCalendar.setAvailableEvents({ + totalPrice: TOTAL_PRICE, + orderCategories: ORDER_CATEGORIES, + }); + + //then + expect(eventCalendar.availableEvents).toEqual(RESULT); + }); +});
JavaScript
ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์—์„œ ์‹ค์ œ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ƒ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ข‹์ง€ ์•Š์•„ ๋ณด์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,117 @@ +import { EVENT_NAMES } from '../../src/constants/event'; +import EventPlanner from '../../src/services/EventPlanner'; + +const ORDERS = [ + { menu: '์–‘์†ก์ด์ˆ˜ํ”„', amount: 1 }, + { menu: 'ํƒ€ํŒŒ์Šค', amount: 1 }, + { menu: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', amount: 2 }, + { menu: '์ดˆ์ฝ”์ผ€์ดํฌ', amount: 1 }, + { menu: '์•„์ด์Šคํฌ๋ฆผ', amount: 1 }, +]; + +async function getEventPlanner(orders) { + // given + const VISIT_DATE = 25; + + // when + const eventPlanner = new EventPlanner(VISIT_DATE); + await eventPlanner.generateReceipt(orders); + await eventPlanner.generateBenefits(); + + return eventPlanner; +} + +describe('EventPlanner ๊ธฐ๋Šฅ ํ…Œ์ŠคํŠธ', () => { + test.each([32, 3.1])('validate - ์ •์ƒ ์ž‘๋™', async (input) => { + expect(() => new EventPlanner(input)).toThrow('[ERROR]'); + }); +}); + +describe('EventPlanner ํ˜œํƒ ์กฐํšŒ ํ…Œ์ŠคํŠธ', () => { + test('์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ ํ…Œ์ŠคํŠธ - gift', async () => { + const gift = { + menu: '์ƒดํŽ˜์ธ', + amount: 1, + price: 25_000, + }; + + const eventPlanner = await getEventPlanner(ORDERS); + + expect(eventPlanner.gift).toEqual(gift); + }); + + test('์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ ํ…Œ์ŠคํŠธ - benefits', async () => { + const benefits = { + [EVENT_NAMES.CHRISTMAS_D_DAY]: { amount: 1, price: 3400 }, + [EVENT_NAMES.SPECIAL]: { amount: 1, price: 1000 }, + [EVENT_NAMES.WEEKDAY]: { amount: 2, price: 2023 }, + [EVENT_NAMES.GIFT]: { + menu: '์ƒดํŽ˜์ธ', + amount: 1, + price: 25_000, + }, + }; + + const eventPlanner = await getEventPlanner(ORDERS); + + expect(eventPlanner.benefits).toEqual(benefits); + }); + + test('์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ ํ…Œ์ŠคํŠธ - totalBenefitMoney', async () => { + const totalBenefitMoney = 33_446; + + const eventPlanner = await getEventPlanner(ORDERS); + + expect(eventPlanner.totalBenefitMoney).toBe(totalBenefitMoney); + }); + + test('์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ ํ…Œ์ŠคํŠธ - payment', async () => { + const originalPrice = 141_500; + const discountAmount = 8_446; + + const eventPlanner = await getEventPlanner(ORDERS); + + expect(eventPlanner.payment).toBe(originalPrice - discountAmount); + }); + + test.each([ + { + orders: [ + { menu: '์–‘์†ก์ด์ˆ˜ํ”„', amount: 1 }, + { menu: 'ํƒ€ํŒŒ์Šค', amount: 1 }, + { menu: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', amount: 2 }, + { menu: '์ดˆ์ฝ”์ผ€์ดํฌ', amount: 1 }, + { menu: '์•„์ด์Šคํฌ๋ฆผ', amount: 1 }, + ], + badge: '์‚ฐํƒ€', + }, + { + orders: [ + { menu: '์–‘์†ก์ด์ˆ˜ํ”„', amount: 1 }, + { menu: 'ํƒ€ํŒŒ์Šค', amount: 1 }, + { menu: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', amount: 1 }, + { menu: '์•„์ด์Šคํฌ๋ฆผ', amount: 4 }, + ], + badge: 'ํŠธ๋ฆฌ', + }, + { + orders: [ + { menu: '์–‘์†ก์ด์ˆ˜ํ”„', amount: 1 }, + { menu: 'ํƒ€ํŒŒ์Šค', amount: 1 }, + { menu: '์•„์ด์Šคํฌ๋ฆผ', amount: 1 }, + ], + badge: '๋ณ„', + }, + { + orders: [ + { menu: '์–‘์†ก์ด์ˆ˜ํ”„', amount: 1 }, + { menu: 'ํƒ€ํŒŒ์Šค', amount: 1 }, + ], + badge: '์—†์Œ', + }, + ])('์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ ํ…Œ์ŠคํŠธ - badge', async ({ orders, badge }) => { + const eventPlanner = await getEventPlanner(orders); + + expect(eventPlanner.badge).toBe(badge); + }); +});
JavaScript
"\_"์ƒˆ๋กญ๊ฒŒ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค!
@@ -0,0 +1,117 @@ +import { EVENT_NAMES } from '../constants/event.js'; +import { DESSERTS, MAIN_COURSES } from '../constants/menus.js'; + +export const CHRISTMAS_D_DAY = { + getBenefit({ date }) { + const baseAmount = 1000; + const weightAmount = 100; + + return { + amount: 1, + price: baseAmount + weightAmount * (date - 1), + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.CHRISTMAS_D_DAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isChristmasPeriod }) { + return isChristmasPeriod === true; + }, +}; + +export const WEEKDAY = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[DESSERTS], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKDAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === false && orderCategories.includes(DESSERTS); + }, +}; + +export const WEEKEND = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[MAIN_COURSES], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKEND, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === true && orderCategories.includes(MAIN_COURSES); + }, +}; + +export const SPECIAL = { + getBenefit() { + const discountAmount = 1_000; + return { + amount: 1, + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.SPECIAL, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isSpecialDate }) { + return isSpecialDate === true; + }, +}; + +export const GIFT = { + getBenefit() { + return { + menu: '์ƒดํŽ˜์ธ', + amount: 1, + price: 25_000, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.GIFT, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ totalPrice }) { + return totalPrice >= 120_000; + }, +}; + +export const ALL_EVENTS = Object.freeze({ + CHRISTMAS_D_DAY, + WEEKDAY, + WEEKEND, + SPECIAL, + GIFT, +});
JavaScript
isWeekend === true๋Š” ๋ถˆํ•„์š”ํ•ด ๋ณด์ด๋Š”๋ฐ, ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ ๊ฐ€์š”?
@@ -0,0 +1,117 @@ +import { EVENT_NAMES } from '../constants/event.js'; +import { DESSERTS, MAIN_COURSES } from '../constants/menus.js'; + +export const CHRISTMAS_D_DAY = { + getBenefit({ date }) { + const baseAmount = 1000; + const weightAmount = 100; + + return { + amount: 1, + price: baseAmount + weightAmount * (date - 1), + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.CHRISTMAS_D_DAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isChristmasPeriod }) { + return isChristmasPeriod === true; + }, +}; + +export const WEEKDAY = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[DESSERTS], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKDAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === false && orderCategories.includes(DESSERTS); + }, +}; + +export const WEEKEND = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[MAIN_COURSES], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKEND, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === true && orderCategories.includes(MAIN_COURSES); + }, +}; + +export const SPECIAL = { + getBenefit() { + const discountAmount = 1_000; + return { + amount: 1, + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.SPECIAL, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isSpecialDate }) { + return isSpecialDate === true; + }, +}; + +export const GIFT = { + getBenefit() { + return { + menu: '์ƒดํŽ˜์ธ', + amount: 1, + price: 25_000, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.GIFT, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ totalPrice }) { + return totalPrice >= 120_000; + }, +}; + +export const ALL_EVENTS = Object.freeze({ + CHRISTMAS_D_DAY, + WEEKDAY, + WEEKEND, + SPECIAL, + GIFT, +});
JavaScript
๋งค์ง ๋„˜๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๊ฒƒ ๊ฐ™์œผ์‹ ๋ฐ์š”, ํ˜น์‹œ ๋ณธ์ธ๋งŒ์— ๊ธฐ์ค€์ด ์žˆ์œผ์‹ค๊นŒ์š”? ์ด๊ณณ์—์„œ๋งŒ ๋”ฐ๋กœ ๋ถ„๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์œผ์‹  ๊ฒƒ ๊ฐ™์•„์„œ์š”.
@@ -0,0 +1,47 @@ +import { Console } from '@woowacourse/mission-utils'; +import MESSAGES from '../constants/messages.js'; + +const OutputView = { + printResult(msgObj, result) { + const { title, printMsg } = msgObj; + + Console.print(title); + Console.print(printMsg(result)); + }, + + printStart() { + Console.print(MESSAGES.outputs.sayHi); + }, + + printMenus({ visitDate, menus }) { + Console.print(MESSAGES.outputs.eventPreview(visitDate)); + + this.printResult(MESSAGES.outputs.menus, menus); + }, + + printOriginalPrice(price) { + this.printResult(MESSAGES.outputs.originalPrice, price); + }, + + printGift(gift) { + this.printResult(MESSAGES.outputs.gift, gift); + }, + + printBenefits(benefits) { + this.printResult(MESSAGES.outputs.benefits, benefits); + }, + + printTotalBenefitMoney(money) { + this.printResult(MESSAGES.outputs.totalBenefitMoney, money); + }, + + printPayment(money) { + this.printResult(MESSAGES.outputs.payment, money); + }, + + printBadge(badge) { + this.printResult(MESSAGES.outputs.badge, badge); + }, +}; + +export default OutputView;
JavaScript
์ด๋Ÿฐ์‹์œผ๋กœ ๋ณด๋‚ด๋ฉด ์กฐ๊ธˆ ๋” ๊น”๋”ํ•ด์ง€๋Š”๊ตฐ์š”...!!!
@@ -0,0 +1,94 @@ +import OrderItem from './OrderItem.js'; + +import MESSAGES from '../constants/messages.js'; + +import throwError from '../utils/throwError.js'; +import { + hasDuplicatedMenu, + hasOnlyBeverages, + isTotalAmountOfMenusValid, +} from '../utils/validators.js'; + +class Receipt { + #orders; + + #orderItems; + + constructor(orders) { + this.#validate(orders); + this.#orders = orders; + this.#orderItems = []; + } + + #validate(orders) { + if (!isTotalAmountOfMenusValid(orders)) + throwError(MESSAGES.errors.invalidOrders); + + if (hasDuplicatedMenu(orders)) throwError(MESSAGES.errors.invalidOrders); + + if (hasOnlyBeverages(orders)) throwError(MESSAGES.errors.invalidOrders); + } + + generateOrders() { + this.#orderItems = this.#orders.map( + ({ menu, amount }) => new OrderItem(menu, amount), + ); + } + + get totalPrice() { + return this.#orderItems.reduce( + (totalPrice, orderItem) => totalPrice + orderItem.totalPrice, + 0, + ); + } + + get receipt() { + const receipt = {}; + + this.#orderItems.forEach((orderItem) => { + const { category, menu, amount } = orderItem.item; + + if (!Array.isArray(receipt[category])) { + receipt[category] = []; + } + + receipt[category].push({ menu, amount }); + }); + + return receipt; + } + + get orderCategories() { + const categories = new Set(); + + this.#orderItems.forEach((orderItem) => { + const { category } = orderItem.item; + categories.add(category); + }); + + return [...categories]; + } + + get orderCntByCategory() { + const orderCnt = {}; + + this.#orderItems.forEach((orderItem) => { + const { category, amount } = orderItem.item; + + if (!orderCnt[category]) orderCnt[category] = 0; + + orderCnt[category] += amount; + }); + + return orderCnt; + } + + get menus() { + return this.#orderItems.map((orderItem) => { + const { menu, amount } = orderItem.item; + return { menu, amount }; + }); + } +} + +export default Receipt;
JavaScript
get์˜ ์‚ฌ์šฉ๋ฒ• ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค...!
@@ -0,0 +1,46 @@ +import MESSAGES from '../constants/messages.js'; +import { CATEGORIES, MENUS } from '../constants/menus.js'; + +import { getValueOfField } from '../utils/object.js'; +import throwError from '../utils/throwError.js'; +import { isMenuExists } from '../utils/validators.js'; + +class OrderItem { + #category; + + #menu; + + #amount; + + constructor(menu, amount) { + this.#validate(menu); + this.#setCategory(menu); + this.#menu = menu; + this.#amount = amount; + } + + #validate(menu) { + if (!isMenuExists(menu)) throwError(MESSAGES.errors.invalidMenu); + } + + #setCategory(menu) { + this.#category = CATEGORIES.find((category) => + getValueOfField(MENUS, `${category}.${menu}`), + ); + } + + get totalPrice() { + const price = getValueOfField(MENUS, `${this.#category}.${this.#menu}`); + return price * this.#amount; + } + + get item() { + return { + category: this.#category, + menu: this.#menu, + amount: this.#amount, + }; + } +} + +export default OrderItem;
JavaScript
get set ํ•จ์ˆ˜๋ฅผ ์ด๋ ‡๊ฒŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ์ฒ˜์Œ ์•Œ๊ฒŒ๋˜์—ˆ๋„ค์š”!
@@ -0,0 +1,38 @@ +/** + * ๋‘ ๊ฐ์ฒด๊ฐ€ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ์–•๊ฒŒ ๋น„๊ต + * @param {object} obj1 + * @param {object} obj2 + */ +export const shallowEqual = (objA, objB) => { + let result = true; + + // ๋‘ ๊ฐ์ฒด์˜ key ๊ฐœ์ˆ˜ ๋น„๊ต + const keysA = Object.keys(objA); + const keysB = Object.keys(objB); + if (keysA.length !== keysB.length) result = false; + + // ๋‘ ๊ฐ์ฒด์˜ value ๊ฐ’ ๋น„๊ต + keysA.forEach((key) => { + if (objA[key] !== objB[key]) result = false; + }); + + return result; +}; + +/** + * ์  ํ‘œ๊ธฐ๋ฒ•(.)์„ ์‚ฌ์šฉํ•ด์„œ ์ค‘์ฒฉ ๊ฐ์ฒด์˜ ๊ฐ’์„ ์ฐพ๋Š” ํ•จ์ˆ˜ + * ์ฐธ๊ณ : https://elvanov.com/2286 + * @param {object} obj + * @param {string} field + */ +export const getValueOfField = (obj, field) => { + if (!field) { + return null; + } + + const keys = field.split('.'); + + return keys.reduce((curObj, curKey) => (curObj ? curObj[curKey] : null), obj); +}; + +export const isEmptyObject = (obj) => JSON.stringify(obj) === '{}';
JavaScript
๋‚ด์šฉ ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž
@@ -0,0 +1,36 @@ +import EventCalendar from '../../src/domain/EventCalendar'; +import { CHRISTMAS_D_DAY, GIFT, SPECIAL } from '../../src/domain/Events'; + +import { + EVENT_MONTH, + EVENT_NAMES, + EVENT_YEAR, +} from '../../src/constants/event'; +import { MAIN_COURSES } from '../../src/constants/menus'; + +describe('EventCalendar ํ…Œ์ŠคํŠธ', () => { + test('getEventBenefit ๊ธฐ๋Šฅ ํ…Œ์ŠคํŠธ', () => { + // given + const YEAR = EVENT_YEAR; + const MONTH = EVENT_MONTH; + const DATE = 25; + const TOTAL_PRICE = 130_000; + const ORDER_CATEGORIES = [MAIN_COURSES]; + + const RESULT = { + [EVENT_NAMES.CHRISTMAS_D_DAY]: CHRISTMAS_D_DAY.getEvent().getBenefit, + [EVENT_NAMES.SPECIAL]: SPECIAL.getEvent().getBenefit, + [EVENT_NAMES.GIFT]: GIFT.getEvent().getBenefit, + }; + + // when + const eventCalendar = new EventCalendar(YEAR, MONTH, DATE); + eventCalendar.setAvailableEvents({ + totalPrice: TOTAL_PRICE, + orderCategories: ORDER_CATEGORIES, + }); + + //then + expect(eventCalendar.availableEvents).toEqual(RESULT); + }); +});
JavaScript
ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์˜ ์˜์กด์„ฑ์„ ์ค„์—ฌ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ผ๊นŒ์š”? ๊ทธ๋ ‡๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋Š” ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค..!
@@ -0,0 +1,117 @@ +import { EVENT_NAMES } from '../constants/event.js'; +import { DESSERTS, MAIN_COURSES } from '../constants/menus.js'; + +export const CHRISTMAS_D_DAY = { + getBenefit({ date }) { + const baseAmount = 1000; + const weightAmount = 100; + + return { + amount: 1, + price: baseAmount + weightAmount * (date - 1), + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.CHRISTMAS_D_DAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isChristmasPeriod }) { + return isChristmasPeriod === true; + }, +}; + +export const WEEKDAY = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[DESSERTS], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKDAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === false && orderCategories.includes(DESSERTS); + }, +}; + +export const WEEKEND = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[MAIN_COURSES], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKEND, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === true && orderCategories.includes(MAIN_COURSES); + }, +}; + +export const SPECIAL = { + getBenefit() { + const discountAmount = 1_000; + return { + amount: 1, + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.SPECIAL, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isSpecialDate }) { + return isSpecialDate === true; + }, +}; + +export const GIFT = { + getBenefit() { + return { + menu: '์ƒดํŽ˜์ธ', + amount: 1, + price: 25_000, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.GIFT, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ totalPrice }) { + return totalPrice >= 120_000; + }, +}; + +export const ALL_EVENTS = Object.freeze({ + CHRISTMAS_D_DAY, + WEEKDAY, + WEEKEND, + SPECIAL, + GIFT, +});
JavaScript
๋ฌผ๋ก  Truthy๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋Š” ์ƒํ™ฉ์ด์ง€๋งŒ, ์‹ค์ œ๋กœ ์šด์˜ํ•˜๋Š” ์„œ๋น„์Šค๋ผ๋ฉด ์ƒํƒœ๊ฐ€ ์ด์ƒํ•˜๊ฑฐ๋‚˜ ์„œ๋ฒ„์—์„œ ์ž˜๋ชป๋œ ๊ฐ’์ด ๋‚ ์•„์™”์„ ๋•Œ ์žก์„ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ์—ฌ์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค! ๊ทธ๋ž˜์„œ ํƒ€์ž…๊ณผ ๊ฐ’์„ ์ •ํ™•ํžˆ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ž‘์„ฑํ–ˆ์–ด์š”.
@@ -0,0 +1,117 @@ +import { EVENT_NAMES } from '../constants/event.js'; +import { DESSERTS, MAIN_COURSES } from '../constants/menus.js'; + +export const CHRISTMAS_D_DAY = { + getBenefit({ date }) { + const baseAmount = 1000; + const weightAmount = 100; + + return { + amount: 1, + price: baseAmount + weightAmount * (date - 1), + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.CHRISTMAS_D_DAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isChristmasPeriod }) { + return isChristmasPeriod === true; + }, +}; + +export const WEEKDAY = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[DESSERTS], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKDAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === false && orderCategories.includes(DESSERTS); + }, +}; + +export const WEEKEND = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[MAIN_COURSES], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKEND, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === true && orderCategories.includes(MAIN_COURSES); + }, +}; + +export const SPECIAL = { + getBenefit() { + const discountAmount = 1_000; + return { + amount: 1, + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.SPECIAL, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isSpecialDate }) { + return isSpecialDate === true; + }, +}; + +export const GIFT = { + getBenefit() { + return { + menu: '์ƒดํŽ˜์ธ', + amount: 1, + price: 25_000, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.GIFT, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ totalPrice }) { + return totalPrice >= 120_000; + }, +}; + +export const ALL_EVENTS = Object.freeze({ + CHRISTMAS_D_DAY, + WEEKDAY, + WEEKEND, + SPECIAL, + GIFT, +});
JavaScript
๋น„์ฆˆ๋‹ˆ์Šค์™€ ๊ด€๋ จํ•œ ์ค‘์š”ํ•œ ๋กœ์ง์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์ •๋ณด๊ฐ€ ๋น ๋ฅด๊ฒŒ ๋ณด์—ฌ์•ผ ํ•˜๋ฉด์„œ ๋‹ค๋ฅธ ๊ณณ์—์„œ ์žฌ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ์ˆ˜๋Š” ์ƒ์ˆ˜๋กœ ๋นผ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ '์ฆ์ •ํ’ˆ ์ˆ˜๋ น ์ตœ์†Œ ์ฃผ๋ฌธ ๊ธˆ์•ก'์ด๋ผ๋Š” ์ด๋ฆ„์˜ ์ƒ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด ๊ทธ ์˜๋ฏธ๋Š” ๋‹ด๊ณ  ์žˆ๊ฒ ์ง€๋งŒ, ๊ทธ๊ฒŒ ์–ผ๋งˆ์ธ์ง€ ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค๋ฅธ ํŒŒ์ผ์„ ์—ด์–ด ๋ณด๊ฑฐ๋‚˜ ์‹œ์„ ์„ ์ด๋™ํ•ด์•ผ ํ•˜๋Š” ๋ถˆํŽธํ•จ์ด ์ƒ๊ธธ ๊ฑฐ๋ผ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. :) ๋น„์Šทํ•œ ๊ฒฐ์ •์„ ๋‚ด๋ฆฐ ์‚ฌ๋ก€๋กœ EventPlanner์˜ getBadge ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.๐Ÿ˜„
@@ -0,0 +1,117 @@ +import { EVENT_NAMES } from '../constants/event.js'; +import { DESSERTS, MAIN_COURSES } from '../constants/menus.js'; + +export const CHRISTMAS_D_DAY = { + getBenefit({ date }) { + const baseAmount = 1000; + const weightAmount = 100; + + return { + amount: 1, + price: baseAmount + weightAmount * (date - 1), + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.CHRISTMAS_D_DAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isChristmasPeriod }) { + return isChristmasPeriod === true; + }, +}; + +export const WEEKDAY = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[DESSERTS], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKDAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === false && orderCategories.includes(DESSERTS); + }, +}; + +export const WEEKEND = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[MAIN_COURSES], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKEND, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === true && orderCategories.includes(MAIN_COURSES); + }, +}; + +export const SPECIAL = { + getBenefit() { + const discountAmount = 1_000; + return { + amount: 1, + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.SPECIAL, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isSpecialDate }) { + return isSpecialDate === true; + }, +}; + +export const GIFT = { + getBenefit() { + return { + menu: '์ƒดํŽ˜์ธ', + amount: 1, + price: 25_000, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.GIFT, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ totalPrice }) { + return totalPrice >= 120_000; + }, +}; + +export const ALL_EVENTS = Object.freeze({ + CHRISTMAS_D_DAY, + WEEKDAY, + WEEKEND, + SPECIAL, + GIFT, +});
JavaScript
์•„ํ•˜ ๊ทธ๋ ‡๊ตฐ์š”. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ๋Š” ์–ด๋– ์‹ ๊ฐ€์š”? ``` isEventAvailable({ totalPrice }) { const evenAvaliableAmount = 120_000; return totalPrice >= evenAvaliableAmount; ``` ๋ถˆํ•„์š”ํ•œ ํ•œ ์ค„์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹ค ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ง์”€ํ•˜์‹  ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉด์„œ๋„ ์˜๋ฏธ๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,36 @@ +import EventCalendar from '../../src/domain/EventCalendar'; +import { CHRISTMAS_D_DAY, GIFT, SPECIAL } from '../../src/domain/Events'; + +import { + EVENT_MONTH, + EVENT_NAMES, + EVENT_YEAR, +} from '../../src/constants/event'; +import { MAIN_COURSES } from '../../src/constants/menus'; + +describe('EventCalendar ํ…Œ์ŠคํŠธ', () => { + test('getEventBenefit ๊ธฐ๋Šฅ ํ…Œ์ŠคํŠธ', () => { + // given + const YEAR = EVENT_YEAR; + const MONTH = EVENT_MONTH; + const DATE = 25; + const TOTAL_PRICE = 130_000; + const ORDER_CATEGORIES = [MAIN_COURSES]; + + const RESULT = { + [EVENT_NAMES.CHRISTMAS_D_DAY]: CHRISTMAS_D_DAY.getEvent().getBenefit, + [EVENT_NAMES.SPECIAL]: SPECIAL.getEvent().getBenefit, + [EVENT_NAMES.GIFT]: GIFT.getEvent().getBenefit, + }; + + // when + const eventCalendar = new EventCalendar(YEAR, MONTH, DATE); + eventCalendar.setAvailableEvents({ + totalPrice: TOTAL_PRICE, + orderCategories: ORDER_CATEGORIES, + }); + + //then + expect(eventCalendar.availableEvents).toEqual(RESULT); + }); +});
JavaScript
๋„ค ์˜์กด์„ฑ ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ €๋Š” ์ƒ์ˆ˜ ์ž์ฒด๋„ ํ•˜๋‚˜์˜ ๋ชจ๋“ˆ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ์š”, ํ•˜๋‚˜์˜ ๋ชจ๋“ˆ์ด ์‹ค์ œ ์ฝ”๋“œ์™€, ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๋‘ ๊ตฐ๋ฐ ๋ชจ๋‘ ์˜์กดํ•˜๋Š” ๊ฒƒ์€ ์ข‹์•„๋ณด์ด์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๊ธฐ๋„ ํ•˜๊ณ , ํŠนํžˆ๋‚˜ ํ…Œ์ŠคํŠธ๋Š” ๋…๋ฆฝ์ ์ด์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,83 @@ +import deepFreeze from '../utils/deepFreeze.js'; +import { EVENT_MONTH } from './event.js'; + +const MESSAGES = deepFreeze({ + // InputView + inputs: { + getVisitDate: `${EVENT_MONTH}์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n`, + getOrders: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', + }, + + // OutputView + outputs: { + sayHi: `์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น ${EVENT_MONTH}์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.`, + + eventPreview: (date) => + `${EVENT_MONTH}์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n`, + + menus: { + title: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + printMsg: (menus) => + menus.reduce( + (acc, { menu, amount }) => `${acc}${menu} ${amount}๊ฐœ\n`, + '', + ), + }, + + originalPrice: { + title: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + printMsg: (price) => `${price.toLocaleString()}์›\n`, + }, + + gift: { + title: '<์ฆ์ • ๋ฉ”๋‰ด>', + printMsg: (gift) => { + if (!gift) return '์—†์Œ\n'; + + return `${gift.menu} ${gift.amount}๊ฐœ\n`; + }, + }, + + benefits: { + title: '<ํ˜œํƒ ๋‚ด์—ญ>', + printMsg: (benefits) => { + if (!benefits) return '์—†์Œ\n'; + + return Object.entries(benefits).reduce( + (acc, [eventName, { amount, price }]) => + `${acc}${eventName}: -${(amount * price).toLocaleString()}์›\n`, + '', + ); + }, + }, + + totalBenefitMoney: { + title: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + printMsg: (money) => { + if (!money) return '0์›\n'; + + return `-${money.toLocaleString()}์›\n`; + }, + }, + + payment: { + title: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + printMsg: (money) => `${money.toLocaleString()}์›\n`, + }, + + badge: { + title: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + printMsg: (badge) => `${badge}`, + }, + }, + + // Error + errors: { + prefix: '[ERROR]', + invalidDate: '์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + invalidOrders: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + }, +}); + +export default MESSAGES;
JavaScript
EVENT_MONTH๋Š” ์ƒ์ˆ˜๋ณด๋‹ค๋Š” 12์›”๋กœ ๋ฐ”๋กœ ๋„ฃ๋Š” ๊ฒƒ๋„ ๊ณ ๋ คํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ๋งŽ์ด ๊ณ ๋ฏผํ–ˆ์—ˆ๋Š”๋ฐ, ํ•ด๋‹น ํฌ๋ฆฌ์Šค๋งˆ์Šค ์ด๋ฒคํŠธ๋Š” ๋‚ด๋…„์—๋„ ๊ฐœ์ตœ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ ๋…„๋„๋Š” ๋ฐ”๋€Œ๋”๋ผ๋„, ํฌ๋ฆฌ์Šค๋งˆ์Šค๋‹ˆ๊นŒ 12์›”์€ ์•ˆ๋ณ€ํ•˜๊ฒ ๋‹ค ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,83 @@ +import deepFreeze from '../utils/deepFreeze.js'; +import { EVENT_MONTH } from './event.js'; + +const MESSAGES = deepFreeze({ + // InputView + inputs: { + getVisitDate: `${EVENT_MONTH}์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n`, + getOrders: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', + }, + + // OutputView + outputs: { + sayHi: `์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น ${EVENT_MONTH}์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.`, + + eventPreview: (date) => + `${EVENT_MONTH}์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n`, + + menus: { + title: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + printMsg: (menus) => + menus.reduce( + (acc, { menu, amount }) => `${acc}${menu} ${amount}๊ฐœ\n`, + '', + ), + }, + + originalPrice: { + title: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + printMsg: (price) => `${price.toLocaleString()}์›\n`, + }, + + gift: { + title: '<์ฆ์ • ๋ฉ”๋‰ด>', + printMsg: (gift) => { + if (!gift) return '์—†์Œ\n'; + + return `${gift.menu} ${gift.amount}๊ฐœ\n`; + }, + }, + + benefits: { + title: '<ํ˜œํƒ ๋‚ด์—ญ>', + printMsg: (benefits) => { + if (!benefits) return '์—†์Œ\n'; + + return Object.entries(benefits).reduce( + (acc, [eventName, { amount, price }]) => + `${acc}${eventName}: -${(amount * price).toLocaleString()}์›\n`, + '', + ); + }, + }, + + totalBenefitMoney: { + title: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + printMsg: (money) => { + if (!money) return '0์›\n'; + + return `-${money.toLocaleString()}์›\n`; + }, + }, + + payment: { + title: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + printMsg: (money) => `${money.toLocaleString()}์›\n`, + }, + + badge: { + title: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + printMsg: (badge) => `${badge}`, + }, + }, + + // Error + errors: { + prefix: '[ERROR]', + invalidDate: '์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + invalidOrders: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + }, +}); + +export default MESSAGES;
JavaScript
toLocaleString ๋ถ€๋ถ„์ด ๊ฐ€๊ฒฉ์„ ๋ฐ˜ํ™˜ํ•ด์ค„๋•Œ๋Š” ๋‹ค ์“ฐ์ด๋Š”๋ฐ, ๋”ฐ๋กœ ๋ชจ๋“ˆํ™” ์‹œํ‚ค๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,83 @@ +import deepFreeze from '../utils/deepFreeze.js'; +import { EVENT_MONTH } from './event.js'; + +const MESSAGES = deepFreeze({ + // InputView + inputs: { + getVisitDate: `${EVENT_MONTH}์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n`, + getOrders: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', + }, + + // OutputView + outputs: { + sayHi: `์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น ${EVENT_MONTH}์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.`, + + eventPreview: (date) => + `${EVENT_MONTH}์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n`, + + menus: { + title: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + printMsg: (menus) => + menus.reduce( + (acc, { menu, amount }) => `${acc}${menu} ${amount}๊ฐœ\n`, + '', + ), + }, + + originalPrice: { + title: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + printMsg: (price) => `${price.toLocaleString()}์›\n`, + }, + + gift: { + title: '<์ฆ์ • ๋ฉ”๋‰ด>', + printMsg: (gift) => { + if (!gift) return '์—†์Œ\n'; + + return `${gift.menu} ${gift.amount}๊ฐœ\n`; + }, + }, + + benefits: { + title: '<ํ˜œํƒ ๋‚ด์—ญ>', + printMsg: (benefits) => { + if (!benefits) return '์—†์Œ\n'; + + return Object.entries(benefits).reduce( + (acc, [eventName, { amount, price }]) => + `${acc}${eventName}: -${(amount * price).toLocaleString()}์›\n`, + '', + ); + }, + }, + + totalBenefitMoney: { + title: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + printMsg: (money) => { + if (!money) return '0์›\n'; + + return `-${money.toLocaleString()}์›\n`; + }, + }, + + payment: { + title: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + printMsg: (money) => `${money.toLocaleString()}์›\n`, + }, + + badge: { + title: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + printMsg: (badge) => `${badge}`, + }, + }, + + // Error + errors: { + prefix: '[ERROR]', + invalidDate: '์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + invalidOrders: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + }, +}); + +export default MESSAGES;
JavaScript
๋ฐ‘์—์„œ๋Š” `totalBenefitMoney`๋กœ ์ƒ์„ธํ•˜๊ฒŒ ๋ฉ”์„œ๋“œ๋ช…์„ ์ง€์œผ์…”์„œ ์ด๊ฒƒ๋„ `benefits`๋ณด๋‹ค๋Š” `benefitsDetail`๋กœ ์ข€๋” ๊ตฌ์ฒด์ ์ธ๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,83 @@ +import deepFreeze from '../utils/deepFreeze.js'; +import { EVENT_MONTH } from './event.js'; + +const MESSAGES = deepFreeze({ + // InputView + inputs: { + getVisitDate: `${EVENT_MONTH}์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n`, + getOrders: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', + }, + + // OutputView + outputs: { + sayHi: `์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น ${EVENT_MONTH}์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.`, + + eventPreview: (date) => + `${EVENT_MONTH}์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n`, + + menus: { + title: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + printMsg: (menus) => + menus.reduce( + (acc, { menu, amount }) => `${acc}${menu} ${amount}๊ฐœ\n`, + '', + ), + }, + + originalPrice: { + title: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + printMsg: (price) => `${price.toLocaleString()}์›\n`, + }, + + gift: { + title: '<์ฆ์ • ๋ฉ”๋‰ด>', + printMsg: (gift) => { + if (!gift) return '์—†์Œ\n'; + + return `${gift.menu} ${gift.amount}๊ฐœ\n`; + }, + }, + + benefits: { + title: '<ํ˜œํƒ ๋‚ด์—ญ>', + printMsg: (benefits) => { + if (!benefits) return '์—†์Œ\n'; + + return Object.entries(benefits).reduce( + (acc, [eventName, { amount, price }]) => + `${acc}${eventName}: -${(amount * price).toLocaleString()}์›\n`, + '', + ); + }, + }, + + totalBenefitMoney: { + title: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + printMsg: (money) => { + if (!money) return '0์›\n'; + + return `-${money.toLocaleString()}์›\n`; + }, + }, + + payment: { + title: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + printMsg: (money) => `${money.toLocaleString()}์›\n`, + }, + + badge: { + title: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + printMsg: (badge) => `${badge}`, + }, + }, + + // Error + errors: { + prefix: '[ERROR]', + invalidDate: '์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + invalidOrders: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + }, +}); + +export default MESSAGES;
JavaScript
์•— ์•ž์—์„œ๋Š” ์ƒ์ˆ˜๊ฐ’์„ ๊ฐ€์ ธ์™”์—ˆ๋Š”๋ฐ, ์ด๊ฑด ํ•˜๋“œ์ฝ”๋”ฉ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค! ํ•˜๋‚˜๋กœ ํ†ต์ผํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,63 @@ +import { FRIDAY, SATURDAY, SPECIAL_DATES } from '../constants/calendar.js'; +import { EVENT_PERIOD } from '../constants/event.js'; +import { ALL_EVENTS } from './Events.js'; + +class EventCalendar { + #year; + + #month; + + #date; + + #availableEvents; + + constructor(year, month, date) { + this.#year = year; + this.#month = month; + this.#date = date; + this.#availableEvents = {}; + } + + async setAvailableEvents({ totalPrice, orderCategories }) { + const state = this.#getState(totalPrice, orderCategories); + + await Object.values(ALL_EVENTS).forEach((event) => { + const { name, getBenefit } = event.getEvent(); + + if (event.isEventAvailable(state)) + this.#availableEvents[name] = getBenefit; + }); + } + + #getState(totalPrice, orderCategories) { + return { + isWeekend: this.#isWeekend(), + isSpecialDate: this.#isSpecialDate(), + isChristmasPeriod: this.#isChristmasPeriod(), + totalPrice, + orderCategories, + }; + } + + #isWeekend() { + const dayOfWeek = new Date( + `${this.#year}-${this.#month}-${this.#date}`, + ).getDay(); + return dayOfWeek === FRIDAY || dayOfWeek === SATURDAY; + } + + #isSpecialDate() { + return SPECIAL_DATES.includes(this.#date); + } + + #isChristmasPeriod() { + const { startDate, endDate } = EVENT_PERIOD; + return this.#date >= startDate && this.#date <= endDate; + } + + get availableEvents() { + return this.#availableEvents; + } +} + +export default EventCalendar;
JavaScript
`Period` ๊ผผ๊ผผํ•˜์‹ญ๋‹ˆ๋‹ค! ๐Ÿ‘๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,63 @@ +import { FRIDAY, SATURDAY, SPECIAL_DATES } from '../constants/calendar.js'; +import { EVENT_PERIOD } from '../constants/event.js'; +import { ALL_EVENTS } from './Events.js'; + +class EventCalendar { + #year; + + #month; + + #date; + + #availableEvents; + + constructor(year, month, date) { + this.#year = year; + this.#month = month; + this.#date = date; + this.#availableEvents = {}; + } + + async setAvailableEvents({ totalPrice, orderCategories }) { + const state = this.#getState(totalPrice, orderCategories); + + await Object.values(ALL_EVENTS).forEach((event) => { + const { name, getBenefit } = event.getEvent(); + + if (event.isEventAvailable(state)) + this.#availableEvents[name] = getBenefit; + }); + } + + #getState(totalPrice, orderCategories) { + return { + isWeekend: this.#isWeekend(), + isSpecialDate: this.#isSpecialDate(), + isChristmasPeriod: this.#isChristmasPeriod(), + totalPrice, + orderCategories, + }; + } + + #isWeekend() { + const dayOfWeek = new Date( + `${this.#year}-${this.#month}-${this.#date}`, + ).getDay(); + return dayOfWeek === FRIDAY || dayOfWeek === SATURDAY; + } + + #isSpecialDate() { + return SPECIAL_DATES.includes(this.#date); + } + + #isChristmasPeriod() { + const { startDate, endDate } = EVENT_PERIOD; + return this.#date >= startDate && this.#date <= endDate; + } + + get availableEvents() { + return this.#availableEvents; + } +} + +export default EventCalendar;
JavaScript
new Date(2023,11,25)๋“ฑ์œผ๋กœ๋„ ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๊ทธ๋Ÿฐ๋ฐ month์˜ ๊ฒฝ์šฐ 0์œผ๋กœ ์‹œ์ž‘ํ•ด์„œ 1๋นผ์ค˜์•ผ ํ•˜๋”๋ผ๊ตฌ์š”...
@@ -0,0 +1,63 @@ +import { FRIDAY, SATURDAY, SPECIAL_DATES } from '../constants/calendar.js'; +import { EVENT_PERIOD } from '../constants/event.js'; +import { ALL_EVENTS } from './Events.js'; + +class EventCalendar { + #year; + + #month; + + #date; + + #availableEvents; + + constructor(year, month, date) { + this.#year = year; + this.#month = month; + this.#date = date; + this.#availableEvents = {}; + } + + async setAvailableEvents({ totalPrice, orderCategories }) { + const state = this.#getState(totalPrice, orderCategories); + + await Object.values(ALL_EVENTS).forEach((event) => { + const { name, getBenefit } = event.getEvent(); + + if (event.isEventAvailable(state)) + this.#availableEvents[name] = getBenefit; + }); + } + + #getState(totalPrice, orderCategories) { + return { + isWeekend: this.#isWeekend(), + isSpecialDate: this.#isSpecialDate(), + isChristmasPeriod: this.#isChristmasPeriod(), + totalPrice, + orderCategories, + }; + } + + #isWeekend() { + const dayOfWeek = new Date( + `${this.#year}-${this.#month}-${this.#date}`, + ).getDay(); + return dayOfWeek === FRIDAY || dayOfWeek === SATURDAY; + } + + #isSpecialDate() { + return SPECIAL_DATES.includes(this.#date); + } + + #isChristmasPeriod() { + const { startDate, endDate } = EVENT_PERIOD; + return this.#date >= startDate && this.#date <= endDate; + } + + get availableEvents() { + return this.#availableEvents; + } +} + +export default EventCalendar;
JavaScript
์›๋ž˜ ๋ฐ˜ํ™˜๋˜๋Š” 0,1,2,~ ๋Œ€์‹  FRIDAY๋กœ ๋ฐ”๊พธ๋‹ˆ๊นŒ ํ›จ์”ฌ ๊ฐ€๋…์„ฑ ์ข‹์•„์ง€๋„ค์š”! ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค!๐Ÿ‘๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,117 @@ +import { EVENT_NAMES } from '../constants/event.js'; +import { DESSERTS, MAIN_COURSES } from '../constants/menus.js'; + +export const CHRISTMAS_D_DAY = { + getBenefit({ date }) { + const baseAmount = 1000; + const weightAmount = 100; + + return { + amount: 1, + price: baseAmount + weightAmount * (date - 1), + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.CHRISTMAS_D_DAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isChristmasPeriod }) { + return isChristmasPeriod === true; + }, +}; + +export const WEEKDAY = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[DESSERTS], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKDAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === false && orderCategories.includes(DESSERTS); + }, +}; + +export const WEEKEND = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[MAIN_COURSES], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKEND, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === true && orderCategories.includes(MAIN_COURSES); + }, +}; + +export const SPECIAL = { + getBenefit() { + const discountAmount = 1_000; + return { + amount: 1, + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.SPECIAL, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isSpecialDate }) { + return isSpecialDate === true; + }, +}; + +export const GIFT = { + getBenefit() { + return { + menu: '์ƒดํŽ˜์ธ', + amount: 1, + price: 25_000, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.GIFT, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ totalPrice }) { + return totalPrice >= 120_000; + }, +}; + +export const ALL_EVENTS = Object.freeze({ + CHRISTMAS_D_DAY, + WEEKDAY, + WEEKEND, + SPECIAL, + GIFT, +});
JavaScript
1_000, 1000 ์ค‘์— ํ•˜๋‚˜๋กœ ํ†ต์ผํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,94 @@ +import OrderItem from './OrderItem.js'; + +import MESSAGES from '../constants/messages.js'; + +import throwError from '../utils/throwError.js'; +import { + hasDuplicatedMenu, + hasOnlyBeverages, + isTotalAmountOfMenusValid, +} from '../utils/validators.js'; + +class Receipt { + #orders; + + #orderItems; + + constructor(orders) { + this.#validate(orders); + this.#orders = orders; + this.#orderItems = []; + } + + #validate(orders) { + if (!isTotalAmountOfMenusValid(orders)) + throwError(MESSAGES.errors.invalidOrders); + + if (hasDuplicatedMenu(orders)) throwError(MESSAGES.errors.invalidOrders); + + if (hasOnlyBeverages(orders)) throwError(MESSAGES.errors.invalidOrders); + } + + generateOrders() { + this.#orderItems = this.#orders.map( + ({ menu, amount }) => new OrderItem(menu, amount), + ); + } + + get totalPrice() { + return this.#orderItems.reduce( + (totalPrice, orderItem) => totalPrice + orderItem.totalPrice, + 0, + ); + } + + get receipt() { + const receipt = {}; + + this.#orderItems.forEach((orderItem) => { + const { category, menu, amount } = orderItem.item; + + if (!Array.isArray(receipt[category])) { + receipt[category] = []; + } + + receipt[category].push({ menu, amount }); + }); + + return receipt; + } + + get orderCategories() { + const categories = new Set(); + + this.#orderItems.forEach((orderItem) => { + const { category } = orderItem.item; + categories.add(category); + }); + + return [...categories]; + } + + get orderCntByCategory() { + const orderCnt = {}; + + this.#orderItems.forEach((orderItem) => { + const { category, amount } = orderItem.item; + + if (!orderCnt[category]) orderCnt[category] = 0; + + orderCnt[category] += amount; + }); + + return orderCnt; + } + + get menus() { + return this.#orderItems.map((orderItem) => { + const { menu, amount } = orderItem.item; + return { menu, amount }; + }); + } +} + +export default Receipt;
JavaScript
ํ˜น์‹œ ์—ฌ๊ธฐ ๊ณต๋ฐฑ์ด ์žˆ๋Š” ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”??
@@ -0,0 +1,94 @@ +import OrderItem from './OrderItem.js'; + +import MESSAGES from '../constants/messages.js'; + +import throwError from '../utils/throwError.js'; +import { + hasDuplicatedMenu, + hasOnlyBeverages, + isTotalAmountOfMenusValid, +} from '../utils/validators.js'; + +class Receipt { + #orders; + + #orderItems; + + constructor(orders) { + this.#validate(orders); + this.#orders = orders; + this.#orderItems = []; + } + + #validate(orders) { + if (!isTotalAmountOfMenusValid(orders)) + throwError(MESSAGES.errors.invalidOrders); + + if (hasDuplicatedMenu(orders)) throwError(MESSAGES.errors.invalidOrders); + + if (hasOnlyBeverages(orders)) throwError(MESSAGES.errors.invalidOrders); + } + + generateOrders() { + this.#orderItems = this.#orders.map( + ({ menu, amount }) => new OrderItem(menu, amount), + ); + } + + get totalPrice() { + return this.#orderItems.reduce( + (totalPrice, orderItem) => totalPrice + orderItem.totalPrice, + 0, + ); + } + + get receipt() { + const receipt = {}; + + this.#orderItems.forEach((orderItem) => { + const { category, menu, amount } = orderItem.item; + + if (!Array.isArray(receipt[category])) { + receipt[category] = []; + } + + receipt[category].push({ menu, amount }); + }); + + return receipt; + } + + get orderCategories() { + const categories = new Set(); + + this.#orderItems.forEach((orderItem) => { + const { category } = orderItem.item; + categories.add(category); + }); + + return [...categories]; + } + + get orderCntByCategory() { + const orderCnt = {}; + + this.#orderItems.forEach((orderItem) => { + const { category, amount } = orderItem.item; + + if (!orderCnt[category]) orderCnt[category] = 0; + + orderCnt[category] += amount; + }); + + return orderCnt; + } + + get menus() { + return this.#orderItems.map((orderItem) => { + const { menu, amount } = orderItem.item; + return { menu, amount }; + }); + } +} + +export default Receipt;
JavaScript
๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น ์ž˜ ์“ฐ์‹ญ๋‹ˆ๋‹น! ๐Ÿ‘
@@ -0,0 +1,144 @@ +import EventCalendar from '../domain/EventCalendar.js'; +import Receipt from '../domain/Receipt.js'; + +import { + EVENT_MONTH, + EVENT_NAMES, + EVENT_PERIOD, + EVENT_YEAR, +} from '../constants/event.js'; +import MESSAGES from '../constants/messages.js'; + +import { isInteger, isNumberInRange } from '../utils/validators.js'; +import throwError from '../utils/throwError.js'; +import { isEmptyObject } from '../utils/object.js'; + +class EventPlanner { + #visitDate; + + #eventCalendar; + + #receipt; + + #benefits; + + constructor(date) { + this.#validate(date); + this.#visitDate = date; + this.#eventCalendar = new EventCalendar(EVENT_YEAR, EVENT_MONTH, date); + this.#benefits = {}; + } + + #validate(date) { + const { startDate, endDate } = EVENT_PERIOD; + + if (!isNumberInRange(startDate, endDate, date)) + throwError(MESSAGES.errors.invalidDate); + + if (!isInteger(date)) throwError(MESSAGES.errors.invalidDate); + } + + generateReceipt(orders) { + this.#receipt = new Receipt(orders); + this.#receipt.generateOrders(orders); + } + + generateBenefits() { + const { totalPrice, orderCategories } = this.#receipt; + + this.#eventCalendar.setAvailableEvents({ + totalPrice, + orderCategories, + }); + + this.#setBenefits(); + } + + #setBenefits() { + if (this.originalPrice < 10_000) return; + + const conditions = { + orderCntByCategory: this.#receipt.orderCntByCategory, + date: this.#visitDate, + }; + + Object.entries(this.#eventCalendar.availableEvents).forEach( + ([eventName, getBenefit]) => { + this.#benefits[eventName] = getBenefit(conditions); + }, + ); + } + + #getBadge() { + const money = this.totalBenefitMoney; + + if (money >= 20_000) return '์‚ฐํƒ€'; + + if (money >= 10_000) return 'ํŠธ๋ฆฌ'; + + if (money >= 5000) return '๋ณ„'; + + return '์—†์Œ'; + } + + get visitDate() { + return this.#visitDate; + } + + // ์ฃผ๋ฌธ ๋ฉ”๋‰ด + get menus() { + return this.#receipt.menus; + } + + // ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก + get originalPrice() { + return this.#receipt.totalPrice; + } + + // ์ฆ์ • ๋ฉ”๋‰ด + get gift() { + if (isEmptyObject(this.#benefits)) return null; + + return this.#benefits[EVENT_NAMES.GIFT]; + } + + // ํ˜œํƒ ๋‚ด์—ญ + get benefits() { + if (isEmptyObject(this.#benefits)) return null; + + return this.#benefits; + } + + // ์ด ํ˜œํƒ ๊ธˆ์•ก + get totalBenefitMoney() { + if (isEmptyObject(this.#benefits)) return 0; + + return Object.values(this.#benefits).reduce( + (totalBenefitMoney, { amount, price }) => + totalBenefitMoney + amount * price, + 0, + ); + } + + // ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก + get payment() { + if (isEmptyObject(this.#benefits)) return this.originalPrice; + + const discountAmount = Object.entries(this.#benefits).reduce( + (payment, [eventName, { amount, price }]) => { + if (eventName !== EVENT_NAMES.GIFT) return payment + amount * price; + return payment; + }, + 0, + ); + + return this.originalPrice - discountAmount; + } + + // 12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€ + get badge() { + return this.#getBadge(); + } +} + +export default EventPlanner;
JavaScript
ํ•ด๋‹น ๋ถ€๋ถ„์€ ๋”ฐ๋กœ ๊ฐ์ฒด๋กœ ๊ด€๋ฆฌํ•ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,11 @@ +const deepFreeze = (object) => { + Object.keys(object).forEach((prop) => { + if (typeof object[prop] === 'object' && !Object.isFrozen(object[prop])) { + deepFreeze(object[prop]); + } + }); + + return Object.freeze(object); +}; + +export default deepFreeze;
JavaScript
deepFreeze ๊ผผ๊ผผํ•˜์‹ญ๋‹ˆ๋‹ค~ ๐Ÿ‘๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,36 @@ +import EventCalendar from '../../src/domain/EventCalendar'; +import { CHRISTMAS_D_DAY, GIFT, SPECIAL } from '../../src/domain/Events'; + +import { + EVENT_MONTH, + EVENT_NAMES, + EVENT_YEAR, +} from '../../src/constants/event'; +import { MAIN_COURSES } from '../../src/constants/menus'; + +describe('EventCalendar ํ…Œ์ŠคํŠธ', () => { + test('getEventBenefit ๊ธฐ๋Šฅ ํ…Œ์ŠคํŠธ', () => { + // given + const YEAR = EVENT_YEAR; + const MONTH = EVENT_MONTH; + const DATE = 25; + const TOTAL_PRICE = 130_000; + const ORDER_CATEGORIES = [MAIN_COURSES]; + + const RESULT = { + [EVENT_NAMES.CHRISTMAS_D_DAY]: CHRISTMAS_D_DAY.getEvent().getBenefit, + [EVENT_NAMES.SPECIAL]: SPECIAL.getEvent().getBenefit, + [EVENT_NAMES.GIFT]: GIFT.getEvent().getBenefit, + }; + + // when + const eventCalendar = new EventCalendar(YEAR, MONTH, DATE); + eventCalendar.setAvailableEvents({ + totalPrice: TOTAL_PRICE, + orderCategories: ORDER_CATEGORIES, + }); + + //then + expect(eventCalendar.availableEvents).toEqual(RESULT); + }); +});
JavaScript
์‹ค์ œ๋กœ ๋ชจ๋“ˆํ™”๋œ ์ƒ์ˆ˜์˜ ๊ตฌ์กฐ๋‚˜ ๊ฐ’์ด ๋ฐ”๋€Œ๋ฉด ํ…Œ์ŠคํŠธ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฑธ ๊ฒฝํ—˜ํ•˜๊ณ  ์˜๋ฌธ์„ ํ’ˆ๊ธด ํ–ˆ๋Š”๋ฐ, ๋…๋ฆฝ์„ฑ์„ ์ง€ํ‚ค์ง€ ์•Š์€ ํ–‰๋™์ด์—ˆ๊ตฐ์š”! ๋•๋ถ„์— ๊นจ๋‹ฌ์•˜์Šต๋‹ˆ๋‹ค. ์ข‹์€ ํ”ผ๋“œ๋ฐฑ ๊ฐ์‚ฌํ•ด์š”.๐Ÿ™
@@ -0,0 +1,117 @@ +import { EVENT_NAMES } from '../constants/event.js'; +import { DESSERTS, MAIN_COURSES } from '../constants/menus.js'; + +export const CHRISTMAS_D_DAY = { + getBenefit({ date }) { + const baseAmount = 1000; + const weightAmount = 100; + + return { + amount: 1, + price: baseAmount + weightAmount * (date - 1), + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.CHRISTMAS_D_DAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isChristmasPeriod }) { + return isChristmasPeriod === true; + }, +}; + +export const WEEKDAY = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[DESSERTS], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKDAY, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === false && orderCategories.includes(DESSERTS); + }, +}; + +export const WEEKEND = { + getBenefit({ orderCntByCategory }) { + const discountAmount = 2_023; + return { + amount: orderCntByCategory[MAIN_COURSES], + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.WEEKEND, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isWeekend, orderCategories }) { + return isWeekend === true && orderCategories.includes(MAIN_COURSES); + }, +}; + +export const SPECIAL = { + getBenefit() { + const discountAmount = 1_000; + return { + amount: 1, + price: discountAmount, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.SPECIAL, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ isSpecialDate }) { + return isSpecialDate === true; + }, +}; + +export const GIFT = { + getBenefit() { + return { + menu: '์ƒดํŽ˜์ธ', + amount: 1, + price: 25_000, + }; + }, + + getEvent() { + return { + name: EVENT_NAMES.GIFT, + getBenefit: this.getBenefit, + }; + }, + + isEventAvailable({ totalPrice }) { + return totalPrice >= 120_000; + }, +}; + +export const ALL_EVENTS = Object.freeze({ + CHRISTMAS_D_DAY, + WEEKDAY, + WEEKEND, + SPECIAL, + GIFT, +});
JavaScript
์ข‹์€ ๋ฐฉ๋ฒ•์ด๋„ค์š”! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. :)
@@ -0,0 +1,83 @@ +import deepFreeze from '../utils/deepFreeze.js'; +import { EVENT_MONTH } from './event.js'; + +const MESSAGES = deepFreeze({ + // InputView + inputs: { + getVisitDate: `${EVENT_MONTH}์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n`, + getOrders: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', + }, + + // OutputView + outputs: { + sayHi: `์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น ${EVENT_MONTH}์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.`, + + eventPreview: (date) => + `${EVENT_MONTH}์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n`, + + menus: { + title: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + printMsg: (menus) => + menus.reduce( + (acc, { menu, amount }) => `${acc}${menu} ${amount}๊ฐœ\n`, + '', + ), + }, + + originalPrice: { + title: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + printMsg: (price) => `${price.toLocaleString()}์›\n`, + }, + + gift: { + title: '<์ฆ์ • ๋ฉ”๋‰ด>', + printMsg: (gift) => { + if (!gift) return '์—†์Œ\n'; + + return `${gift.menu} ${gift.amount}๊ฐœ\n`; + }, + }, + + benefits: { + title: '<ํ˜œํƒ ๋‚ด์—ญ>', + printMsg: (benefits) => { + if (!benefits) return '์—†์Œ\n'; + + return Object.entries(benefits).reduce( + (acc, [eventName, { amount, price }]) => + `${acc}${eventName}: -${(amount * price).toLocaleString()}์›\n`, + '', + ); + }, + }, + + totalBenefitMoney: { + title: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + printMsg: (money) => { + if (!money) return '0์›\n'; + + return `-${money.toLocaleString()}์›\n`; + }, + }, + + payment: { + title: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + printMsg: (money) => `${money.toLocaleString()}์›\n`, + }, + + badge: { + title: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + printMsg: (badge) => `${badge}`, + }, + }, + + // Error + errors: { + prefix: '[ERROR]', + invalidDate: '์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + invalidOrders: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + }, +}); + +export default MESSAGES;
JavaScript
์ €๋„ ๊ณ ๋ฏผํ–ˆ๋˜ ๋ถ€๋ถ„์ธ๋ฐ, ๋‹ค๋ฅธ ๋‹ฌ์— ์ด๋ฒคํŠธ๋ฅผ ๊ฐœ์ตœํ•œ๋‹ค๋ฉด ์ƒ์ˆ˜๋กœ ๋นผ๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„ ์ด๋ ‡๊ฒŒ ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,83 @@ +import deepFreeze from '../utils/deepFreeze.js'; +import { EVENT_MONTH } from './event.js'; + +const MESSAGES = deepFreeze({ + // InputView + inputs: { + getVisitDate: `${EVENT_MONTH}์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n`, + getOrders: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', + }, + + // OutputView + outputs: { + sayHi: `์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น ${EVENT_MONTH}์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.`, + + eventPreview: (date) => + `${EVENT_MONTH}์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n`, + + menus: { + title: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + printMsg: (menus) => + menus.reduce( + (acc, { menu, amount }) => `${acc}${menu} ${amount}๊ฐœ\n`, + '', + ), + }, + + originalPrice: { + title: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + printMsg: (price) => `${price.toLocaleString()}์›\n`, + }, + + gift: { + title: '<์ฆ์ • ๋ฉ”๋‰ด>', + printMsg: (gift) => { + if (!gift) return '์—†์Œ\n'; + + return `${gift.menu} ${gift.amount}๊ฐœ\n`; + }, + }, + + benefits: { + title: '<ํ˜œํƒ ๋‚ด์—ญ>', + printMsg: (benefits) => { + if (!benefits) return '์—†์Œ\n'; + + return Object.entries(benefits).reduce( + (acc, [eventName, { amount, price }]) => + `${acc}${eventName}: -${(amount * price).toLocaleString()}์›\n`, + '', + ); + }, + }, + + totalBenefitMoney: { + title: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + printMsg: (money) => { + if (!money) return '0์›\n'; + + return `-${money.toLocaleString()}์›\n`; + }, + }, + + payment: { + title: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + printMsg: (money) => `${money.toLocaleString()}์›\n`, + }, + + badge: { + title: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + printMsg: (badge) => `${badge}`, + }, + }, + + // Error + errors: { + prefix: '[ERROR]', + invalidDate: '์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + invalidOrders: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + }, +}); + +export default MESSAGES;
JavaScript
์•—, ๋†“์นœ ๋ถ€๋ถ„์ด๋„ค์š”! ์งš์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.๐Ÿ™
@@ -0,0 +1,63 @@ +import { FRIDAY, SATURDAY, SPECIAL_DATES } from '../constants/calendar.js'; +import { EVENT_PERIOD } from '../constants/event.js'; +import { ALL_EVENTS } from './Events.js'; + +class EventCalendar { + #year; + + #month; + + #date; + + #availableEvents; + + constructor(year, month, date) { + this.#year = year; + this.#month = month; + this.#date = date; + this.#availableEvents = {}; + } + + async setAvailableEvents({ totalPrice, orderCategories }) { + const state = this.#getState(totalPrice, orderCategories); + + await Object.values(ALL_EVENTS).forEach((event) => { + const { name, getBenefit } = event.getEvent(); + + if (event.isEventAvailable(state)) + this.#availableEvents[name] = getBenefit; + }); + } + + #getState(totalPrice, orderCategories) { + return { + isWeekend: this.#isWeekend(), + isSpecialDate: this.#isSpecialDate(), + isChristmasPeriod: this.#isChristmasPeriod(), + totalPrice, + orderCategories, + }; + } + + #isWeekend() { + const dayOfWeek = new Date( + `${this.#year}-${this.#month}-${this.#date}`, + ).getDay(); + return dayOfWeek === FRIDAY || dayOfWeek === SATURDAY; + } + + #isSpecialDate() { + return SPECIAL_DATES.includes(this.#date); + } + + #isChristmasPeriod() { + const { startDate, endDate } = EVENT_PERIOD; + return this.#date >= startDate && this.#date <= endDate; + } + + get availableEvents() { + return this.#availableEvents; + } +} + +export default EventCalendar;
JavaScript
๋งž์•„์š”! ์‹ค์ˆ˜๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด์„œ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. :)
@@ -0,0 +1,56 @@ +package christmas.domain; + + +import static christmas.domain.Badge.NONE; +import static christmas.domain.Badge.SANTA; +import static christmas.domain.Badge.STAR; +import static christmas.domain.Badge.TREE; + +import java.util.Map; + +public class Benefits { + private final Map<Event, Integer> benefits; + + public Benefits(Map<Event, Integer> benefits) { + this.benefits = benefits; + } + + public int calculateTotalBenefitAmount() { + return benefits.values() + .stream() + .mapToInt(amount -> amount) + .sum(); + } + + public Badge toEventBadge(int totalBenefit) { + if (isStarBadge(totalBenefit)) { + return STAR; + } + if (isTreeBadge(totalBenefit)) { + return TREE; + } + if (isSantaBadge(totalBenefit)) { + return SANTA; + } + return NONE; + } + + private boolean isStarBadge(int totalBenefit) { + return totalBenefit >= STAR.getAmount() && totalBenefit < TREE.getAmount(); + } + + private boolean isTreeBadge(int totalBenefit) { + return totalBenefit >= TREE.getAmount() && totalBenefit < SANTA.getAmount(); + } + + private boolean isSantaBadge(int totalBenefit) { + return totalBenefit >= SANTA.getAmount(); + } + + public Map<Event, Integer> getBenefits() { + return benefits; + } +} + + +
Java
is{BadgeType}Badge ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋˜๋ฉด ๋ฐฐ์ง€๊ฐ€ ์ƒ๊ธฐ๊ฑฐ๋‚˜ ์‚ญ์ œํ• ๋•Œ๋งˆ๋‹ค ๋ฉ”์†Œ๋“œ ๊ด€๋ฆฌ๋ฅผ ๊ณ„์† ํ•ด์ค˜์•ผํ•œ๋‹ค๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™์•„์š”!!
@@ -0,0 +1,48 @@ +package christmas.domain; + +public enum Menu { + MUSHROOM_SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6_000, "์• ํ”ผํƒ€์ด์ €"), + TAPAS("ํƒ€ํŒŒ์Šค", 5_500, "์• ํ”ผํƒ€์ด์ €"), + CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000, "์• ํ”ผํƒ€์ด์ €"), + T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000, "๋ฉ”์ธ"), + BBQ_RIBS("๋ฐ”๋น„ํ๋ฆฝ", 54_000, "๋ฉ”์ธ"), + SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000, "๋ฉ”์ธ"), + CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000, "๋ฉ”์ธ"), + CHOCOLATE_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15_000, "๋””์ €ํŠธ"), + ICE_CREAM("์•„์ด์Šคํฌ๋ฆผ", 5_000, "๋””์ €ํŠธ"), + ZERO_COKE("์ œ๋กœ์ฝœ๋ผ", 3_000, "์Œ๋ฃŒ"), + RED_WINE("๋ ˆ๋“œ์™€์ธ", 60_000, "์Œ๋ฃŒ"), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, "์Œ๋ฃŒ"), + NONE("์—†์Œ", 0, "์—†์Œ"); + + private final String menuName; + private final int price; + private final String type; + + Menu(String menuName, int price, String type) { + this.menuName = menuName; + this.price = price; + this.type = type; + } + + public static Menu from(String menuName) { + for (Menu menu : values()) { + if (menu.getMenuName().equals(menuName)) { + return menu; + } + } + return NONE; + } + + public String getMenuName() { + return menuName; + } + + public int getPrice() { + return price; + } + + public String getType() { + return type; + } +}
Java
์Œ์‹ Type์˜ ๊ฒฝ์šฐ ๊ณตํ†ต๋˜๋Š” ํ•ญ๋ชฉ (์—ํ”ผํƒ€์ด์ €/๋ฉ”์ธ/๋””์ €ํŠธ/์Œ๋ฃŒ)์— ๋Œ€ํ•ด์„œ ๋ณ„๋„๋กœ ๊ด€๋ฆฌํ•ด๋„ ์ข‹์„๊ฑฐ ๊ฐ™์•„์š”
@@ -0,0 +1,76 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.Benefits; +import christmas.domain.Convertor; +import christmas.domain.Date; +import christmas.domain.EventManager; +import christmas.domain.Orders; +import christmas.view.InputView; +import christmas.view.OutputView; + +public class EventController { + public void start() { + OutputView.printWelcome(); + Date date = createDate(); + Orders orders = createOrders(); + int totalOrderAmount = createTotalOrderAmount(orders); + OutputView.printGift(orders.hasGift()); + Benefits benefits = createBenefits(orders, date); + int totalBenefitAmount = createTotalBenefitAmount(benefits); + createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount); + createBadge(benefits, totalBenefitAmount); + } + + private Date createDate() { + while (true) { + try { + Date date = new Date(InputView.readDate()); + OutputView.printPreview(date); + return date; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private Orders createOrders() { + while (true) { + try { + Orders orders = Convertor.toOrders(InputView.readOrder()); + OutputView.printOrders(orders); + return orders; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private int createTotalOrderAmount(Orders orders) { + int totalOrderAmount = orders.calculateTotalAmount(); + OutputView.printTotalOrderAmount(totalOrderAmount); + return totalOrderAmount; + } + + private Benefits createBenefits(Orders orders, Date date) { + Benefits benefits = EventManager.toBenefits(orders, date); + OutputView.printBenefitDetail(benefits); + return benefits; + } + + private int createTotalBenefitAmount(Benefits benefits) { + int totalBenefitAmount = benefits.calculateTotalBenefitAmount(); + OutputView.printTotalBenefitAmount(totalBenefitAmount); + return totalBenefitAmount; + } + + private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) { + int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount); + OutputView.printPaymentAmount(paymentAmount); + } + + private void createBadge(Benefits benefits, int totalBenefitAmount) { + Badge badge = benefits.toEventBadge(totalBenefitAmount); + OutputView.printEventBadge(badge); + } +}
Java
์ž…๋ ฅ ๊ฒ€์ฆ์„ ํ›„ ์žฌ์ž…๋ ฅ ๋ฐ›๋Š”๊ฒƒ์„ inputview์—์„œ ์ง„ํ–‰ํ•˜๋Š”๊ฒƒ์— ๋Œ€ํ•ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”??? ๊ตฌํ˜„ ํ•˜๋ฉด์„œ ๊ณ ๋ฏผํ–ˆ์—ˆ๋Š”๋ฐ ์–ด๋А ๋ฐฉํ–ฅ์œผ๋กœ ๊ตฌํ˜„ํ•ด์•ผ ๋” ๋‚˜์€์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋”๋ผ๊ตฌ์š”
@@ -0,0 +1,76 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.Benefits; +import christmas.domain.Convertor; +import christmas.domain.Date; +import christmas.domain.EventManager; +import christmas.domain.Orders; +import christmas.view.InputView; +import christmas.view.OutputView; + +public class EventController { + public void start() { + OutputView.printWelcome(); + Date date = createDate(); + Orders orders = createOrders(); + int totalOrderAmount = createTotalOrderAmount(orders); + OutputView.printGift(orders.hasGift()); + Benefits benefits = createBenefits(orders, date); + int totalBenefitAmount = createTotalBenefitAmount(benefits); + createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount); + createBadge(benefits, totalBenefitAmount); + } + + private Date createDate() { + while (true) { + try { + Date date = new Date(InputView.readDate()); + OutputView.printPreview(date); + return date; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private Orders createOrders() { + while (true) { + try { + Orders orders = Convertor.toOrders(InputView.readOrder()); + OutputView.printOrders(orders); + return orders; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private int createTotalOrderAmount(Orders orders) { + int totalOrderAmount = orders.calculateTotalAmount(); + OutputView.printTotalOrderAmount(totalOrderAmount); + return totalOrderAmount; + } + + private Benefits createBenefits(Orders orders, Date date) { + Benefits benefits = EventManager.toBenefits(orders, date); + OutputView.printBenefitDetail(benefits); + return benefits; + } + + private int createTotalBenefitAmount(Benefits benefits) { + int totalBenefitAmount = benefits.calculateTotalBenefitAmount(); + OutputView.printTotalBenefitAmount(totalBenefitAmount); + return totalBenefitAmount; + } + + private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) { + int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount); + OutputView.printPaymentAmount(paymentAmount); + } + + private void createBadge(Benefits benefits, int totalBenefitAmount) { + Badge badge = benefits.toEventBadge(totalBenefitAmount); + OutputView.printEventBadge(badge); + } +}
Java
๋‹ค๋“ค ์˜ˆ์™ธ๋ฅผ e ๋ผ๊ณ  ํ‘œ๊ธฐํ•˜์ง€๋งŒ ๊ทธ๋ž˜๋„ error ๋ผ๊ณ  ์กฐ๊ธˆ ๋” ์ƒ์„ธํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ €๋„ ์˜ค๋ผ๊ณ  ํ–ˆ์—ˆ๋Š”๋ฐ ์–ด๋–ค ๋ถ„์ด ๋ฆฌ๋ทฐ๋•Œ ์ด๋Ÿฌํ•œ ๋ถ€๋ถ„์„ ์–ธ๊ธ‰ํ•œ ๊ฒƒ์„ ๋ณธ ์ ์ด ์žˆ์–ด์„œ์š”
@@ -0,0 +1,48 @@ +package christmas.domain; + +public enum Menu { + MUSHROOM_SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6_000, "์• ํ”ผํƒ€์ด์ €"), + TAPAS("ํƒ€ํŒŒ์Šค", 5_500, "์• ํ”ผํƒ€์ด์ €"), + CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000, "์• ํ”ผํƒ€์ด์ €"), + T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000, "๋ฉ”์ธ"), + BBQ_RIBS("๋ฐ”๋น„ํ๋ฆฝ", 54_000, "๋ฉ”์ธ"), + SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000, "๋ฉ”์ธ"), + CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000, "๋ฉ”์ธ"), + CHOCOLATE_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15_000, "๋””์ €ํŠธ"), + ICE_CREAM("์•„์ด์Šคํฌ๋ฆผ", 5_000, "๋””์ €ํŠธ"), + ZERO_COKE("์ œ๋กœ์ฝœ๋ผ", 3_000, "์Œ๋ฃŒ"), + RED_WINE("๋ ˆ๋“œ์™€์ธ", 60_000, "์Œ๋ฃŒ"), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, "์Œ๋ฃŒ"), + NONE("์—†์Œ", 0, "์—†์Œ"); + + private final String menuName; + private final int price; + private final String type; + + Menu(String menuName, int price, String type) { + this.menuName = menuName; + this.price = price; + this.type = type; + } + + public static Menu from(String menuName) { + for (Menu menu : values()) { + if (menu.getMenuName().equals(menuName)) { + return menu; + } + } + return NONE; + } + + public String getMenuName() { + return menuName; + } + + public int getPrice() { + return price; + } + + public String getType() { + return type; + } +}
Java
Stream์„ ์ด์šฉํ•ด ๋ฉ”๋‰ด๊ฐ€ ํฌํ•จ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด contains๋กœ ํ™•์ธํ•˜๋Š” ๊ฒŒ ์กฐ๊ธˆ ๋” ๊ฐ„๊ฒฐํ•ด ๋ณด์ผ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”??
@@ -0,0 +1,17 @@ +package christmas.domain; + +public enum Error { + INVALID_DATE("[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + INVALID_ORDER("[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + INVALID_COUNT("[ERROR] ๋ฉ”๋‰ด๋Š” ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 20๊ฐœ๊นŒ์ง€๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”"), + INVALID_TYPE("[ERROR] ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ์‹œ, ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”"); + private final String message; + + Error(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +}
Java
[Error] ์€ ๊ณตํ†ต๋ถ€๋ถ„์ด๋‹ค๋ณด๋‹ˆ ๋”ฐ๋กœ ๋นผ๋‘์–ด์„œ ๋ถˆ๋Ÿฌ์˜ฌ๋•Œ this message = " [ error ]"+ message ๋กœ ํ•ด๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,76 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.Benefits; +import christmas.domain.Convertor; +import christmas.domain.Date; +import christmas.domain.EventManager; +import christmas.domain.Orders; +import christmas.view.InputView; +import christmas.view.OutputView; + +public class EventController { + public void start() { + OutputView.printWelcome(); + Date date = createDate(); + Orders orders = createOrders(); + int totalOrderAmount = createTotalOrderAmount(orders); + OutputView.printGift(orders.hasGift()); + Benefits benefits = createBenefits(orders, date); + int totalBenefitAmount = createTotalBenefitAmount(benefits); + createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount); + createBadge(benefits, totalBenefitAmount); + } + + private Date createDate() { + while (true) { + try { + Date date = new Date(InputView.readDate()); + OutputView.printPreview(date); + return date; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private Orders createOrders() { + while (true) { + try { + Orders orders = Convertor.toOrders(InputView.readOrder()); + OutputView.printOrders(orders); + return orders; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private int createTotalOrderAmount(Orders orders) { + int totalOrderAmount = orders.calculateTotalAmount(); + OutputView.printTotalOrderAmount(totalOrderAmount); + return totalOrderAmount; + } + + private Benefits createBenefits(Orders orders, Date date) { + Benefits benefits = EventManager.toBenefits(orders, date); + OutputView.printBenefitDetail(benefits); + return benefits; + } + + private int createTotalBenefitAmount(Benefits benefits) { + int totalBenefitAmount = benefits.calculateTotalBenefitAmount(); + OutputView.printTotalBenefitAmount(totalBenefitAmount); + return totalBenefitAmount; + } + + private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) { + int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount); + OutputView.printPaymentAmount(paymentAmount); + } + + private void createBadge(Benefits benefits, int totalBenefitAmount) { + Badge badge = benefits.toEventBadge(totalBenefitAmount); + OutputView.printEventBadge(badge); + } +}
Java
์ „์ฒด์ ์œผ๋กœ create๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ช…์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ create๊ฐ€ ๊ฐ์ฒด ์ƒ์„ฑ์˜ ๋А๋‚Œ์„ ์ฃผ๋Š”๊ฑฐ๊ฐ™์•„์„œ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ฉ”์„œ๋“œ๋ช…์„ ์ž‘์„ฑํ•ด๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š” ?
@@ -0,0 +1,43 @@ +package christmas.domain; + +import static christmas.domain.Error.INVALID_ORDER; + +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +public class Convertor { + private static final String ORDERS_DELIMITER = ","; + private static final String ORDER_DELIMITER = "-"; + private static final int MENU_INDEX = 0; + private static final int COUNT_INDEX = 1; + private static final String REGEXP_PATTERN_ORDER = "([๊ฐ€-ํžฃ]+)(-)(\\d+)"; + + public static Orders toOrders(String input) { + List<Order> orders = Arrays.stream(input.split(ORDERS_DELIMITER)) + .map(String::trim) + .map(Convertor::toOrder) + .collect(Collectors.toList()); + return new Orders(orders); + } + + private static Order toOrder(String input) { + validatePattern(input); + String[] menus = input.split(ORDER_DELIMITER); + String menuName = menus[MENU_INDEX]; + int count = Integer.parseInt(menus[COUNT_INDEX]); + return new Order(menuName, count); + } + + private static void validatePattern(String input) { + if (!isValidPattern(input)) { + throw new IllegalArgumentException(INVALID_ORDER.getMessage()); + } + } + + private static boolean isValidPattern(String input) { + return Pattern.matches(REGEXP_PATTERN_ORDER, input); + } + +}
Java
์ •์  ๋ฉ”์†Œ๋“œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์ค€์ด ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค. ํ”„๋ฆฌ์ฝ”์Šค ๊ธฐ๊ฐ„๋™์•ˆ ๋ฏธ์…˜ํ•˜๋ฉด์„œ ์จ๋ณด๊ธฐ๋Š” ํ–ˆ๋Š”๋ฐ ์–ด๋–จ๋•Œ ์จ์•ผํ• ์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์–ด์„œ์š”
@@ -0,0 +1,24 @@ +package christmas.domain; + +public enum Badge { + STAR("๋ณ„", 5_000), + TREE("ํŠธ๋ฆฌ", 10_000), + SANTA("์‚ฐํƒ€", 20_000), + NONE("์—†์Œ", 0); + + private final String type; + private final int amount; + + Badge(String type, int amount) { + this.type = type; + this.amount = amount; + } + + public String getType() { + return type; + } + + public int getAmount() { + return amount; + } +}
Java
private์„ ์ง€์ •ํ•ด ์ฃผ์ง€ ์•Š์€ ์ด์œ ๊ฐ€ ๋”ฐ๋กœ ์žˆ๋‚˜์š”?
@@ -0,0 +1,56 @@ +package christmas.domain; + + +import static christmas.domain.Badge.NONE; +import static christmas.domain.Badge.SANTA; +import static christmas.domain.Badge.STAR; +import static christmas.domain.Badge.TREE; + +import java.util.Map; + +public class Benefits { + private final Map<Event, Integer> benefits; + + public Benefits(Map<Event, Integer> benefits) { + this.benefits = benefits; + } + + public int calculateTotalBenefitAmount() { + return benefits.values() + .stream() + .mapToInt(amount -> amount) + .sum(); + } + + public Badge toEventBadge(int totalBenefit) { + if (isStarBadge(totalBenefit)) { + return STAR; + } + if (isTreeBadge(totalBenefit)) { + return TREE; + } + if (isSantaBadge(totalBenefit)) { + return SANTA; + } + return NONE; + } + + private boolean isStarBadge(int totalBenefit) { + return totalBenefit >= STAR.getAmount() && totalBenefit < TREE.getAmount(); + } + + private boolean isTreeBadge(int totalBenefit) { + return totalBenefit >= TREE.getAmount() && totalBenefit < SANTA.getAmount(); + } + + private boolean isSantaBadge(int totalBenefit) { + return totalBenefit >= SANTA.getAmount(); + } + + public Map<Event, Integer> getBenefits() { + return benefits; + } +} + + +
Java
๋‘์ค„ ๊ฐœํ–‰์ด ๋“ค์–ด๊ฐ„๊ฑฐ ๊ฐ™์•„์š” !! ๋‹ค์Œ๋ฒˆ์—” ์ž์ž˜ํ•œ ์ปจ๋ฒค์…˜๋„ ์ฑ™๊ธฐ๋ฉด ์ข‹์„๊ฑฐ๊ฐ™์•„์š”
@@ -0,0 +1,51 @@ +package christmas.domain; + +import static christmas.domain.Error.INVALID_DATE; +import static java.time.DayOfWeek.FRIDAY; +import static java.time.DayOfWeek.SATURDAY; +import static java.time.DayOfWeek.SUNDAY; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +public class Date { + private static final int MIN_DATE = 1; + private static final int MAX_DATE = 31; + private static final int THIS_YEAR = 2023; + private static final int THIS_MONTH = 12; + private static final int CHRISTMAS_DATE = 25; + private final int value; + + public Date(int value) { + validateRange(value); + this.value = value; + } + + public boolean isDayBeforeChristmas() { + return value <= CHRISTMAS_DATE; + } + + public boolean isWeekend() { + DayOfWeek dayOfWeek = createDayOfWeek(); + return dayOfWeek == FRIDAY || dayOfWeek == SATURDAY; + } + + public boolean isStarDay() { + return createDayOfWeek() == SUNDAY || value == CHRISTMAS_DATE; + } + + public int getValue() { + return value; + } + + private DayOfWeek createDayOfWeek() { + LocalDate localDate = LocalDate.of(THIS_YEAR, THIS_MONTH, value); + return localDate.getDayOfWeek(); + } + + private void validateRange(int value) { + if (value > MAX_DATE || value < MIN_DATE) { + throw new IllegalArgumentException(INVALID_DATE.getMessage()); + } + } +} \ No newline at end of file
Java
Date๋ผ๋Š” ํด๋ž˜์Šค๊ฐ€ validateRange๋„ ๊ทธ๋ ‡๊ณ  12์›”์—๋งŒ ํ•œ์ •๋œ๊ฑฐ ๊ฐ™์€๋ฐ Date๋ผ๊ณ  class๋ช…์„ ์ง“๋Š”๊ฑด ๋„ˆ๋ฌด ํฌ๊ด„์ ์ด๊ฒŒ ๋А๊ปด์ ธ์„œ ์กฐ๊ธˆ ๋” ๊ตฌ์ฒด์ ์ธ ๋‹ค๋ฅธ์ด๋ฆ„์„ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด ์–ด๋–จ๊นŒ ์‹ถ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,48 @@ +package christmas.domain; + +public enum Menu { + MUSHROOM_SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6_000, "์• ํ”ผํƒ€์ด์ €"), + TAPAS("ํƒ€ํŒŒ์Šค", 5_500, "์• ํ”ผํƒ€์ด์ €"), + CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000, "์• ํ”ผํƒ€์ด์ €"), + T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000, "๋ฉ”์ธ"), + BBQ_RIBS("๋ฐ”๋น„ํ๋ฆฝ", 54_000, "๋ฉ”์ธ"), + SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000, "๋ฉ”์ธ"), + CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000, "๋ฉ”์ธ"), + CHOCOLATE_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15_000, "๋””์ €ํŠธ"), + ICE_CREAM("์•„์ด์Šคํฌ๋ฆผ", 5_000, "๋””์ €ํŠธ"), + ZERO_COKE("์ œ๋กœ์ฝœ๋ผ", 3_000, "์Œ๋ฃŒ"), + RED_WINE("๋ ˆ๋“œ์™€์ธ", 60_000, "์Œ๋ฃŒ"), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, "์Œ๋ฃŒ"), + NONE("์—†์Œ", 0, "์—†์Œ"); + + private final String menuName; + private final int price; + private final String type; + + Menu(String menuName, int price, String type) { + this.menuName = menuName; + this.price = price; + this.type = type; + } + + public static Menu from(String menuName) { + for (Menu menu : values()) { + if (menu.getMenuName().equals(menuName)) { + return menu; + } + } + return NONE; + } + + public String getMenuName() { + return menuName; + } + + public int getPrice() { + return price; + } + + public String getType() { + return type; + } +}
Java
์—ฌ๊ธฐ๋„ ์ ‘๊ทผ ์ง€์ •์ž๊ฐ€ ๋”ฐ๋กœ ์„ค์ •๋˜์ง€ ์•Š์€ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š” ?
@@ -0,0 +1,92 @@ +package christmas.domain; + +import static christmas.domain.Error.INVALID_ORDER; +import static christmas.domain.Menu.NONE; + +import java.util.Objects; + +public class Order { + private static final int MIN_ORDER_COUNT = 1; + private final String menuName; + private final int count; + + public Order(String menuName, int count) { + validateMenuName(menuName); + validateCount(count); + this.menuName = menuName; + this.count = count; + } + + public int calculateByMenu() { + Menu menu = Menu.from(menuName); + return menu.getPrice() * count; + } + + + public boolean isBeverage() { + Menu menu = Menu.from(menuName); + return menu.getType().equals("์Œ๋ฃŒ"); + } + + public int countDessert() { + if (isDessert()) { + return count; + } + return 0; + } + + private boolean isDessert() { + Menu menu = Menu.from(menuName); + return menu.getType().equals("๋””์ €ํŠธ"); + } + + public int countMain() { + if (isMain()) { + return count; + } + return 0; + } + + + private boolean isMain() { + Menu menu = Menu.from(menuName); + return menu.getType().equals("๋ฉ”์ธ"); + } + + public String getMenuName() { + return menuName; + } + + public int getCount() { + return count; + } + + @Override + public boolean equals(Object o) { + if (o instanceof Order order) { + return menuName.equals(order.menuName); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(menuName); + } + + private void validateMenuName(String menu) { + if (isInvalidMenu(menu)) { + throw new IllegalArgumentException(INVALID_ORDER.getMessage()); + } + } + + private boolean isInvalidMenu(String menu) { + return Menu.from(menu) == NONE; + } + + private void validateCount(int count) { + if (count < MIN_ORDER_COUNT) { + throw new IllegalArgumentException(INVALID_ORDER.getMessage()); + } + } +}
Java
์•„๊นŒ Type์„ ๋ณ„๋„๋กœ Enum ๊ฐ™์€ ๊ฐ์ฒด๋กœ ๊ด€๋ฆฌ ํ–ˆ๋”๋ผ๋ฉด "์Œ๋ฃŒ"-> ์™€ ๊ฐ™์€ stringํ˜•์œผ๋กœ ๋น„๊ตํ•˜์ง€ ์•Š์•„๋„ ๋์„ ๊ฑฐ ๊ฐ™์•„์š” !! ๋งŒ์•ฝ Type์˜ ์ด๋ฆ„์ด ๋ฐ”๋€Œ๊ฒŒ ๋์„๋•Œ ํ•˜๋‚˜ํ•˜๋‚˜ ๋‹ค์‹œ ์ฝ”๋“œ๋ฅผ ๋ฐ”๊พธ๋Š” ์ˆ˜๊ณ ๋ฅผ ๋œ ์ˆ˜ ์žˆ์—ˆ์„๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค:D
@@ -0,0 +1,92 @@ +package christmas.domain; + +import static christmas.domain.Error.INVALID_ORDER; +import static christmas.domain.Menu.NONE; + +import java.util.Objects; + +public class Order { + private static final int MIN_ORDER_COUNT = 1; + private final String menuName; + private final int count; + + public Order(String menuName, int count) { + validateMenuName(menuName); + validateCount(count); + this.menuName = menuName; + this.count = count; + } + + public int calculateByMenu() { + Menu menu = Menu.from(menuName); + return menu.getPrice() * count; + } + + + public boolean isBeverage() { + Menu menu = Menu.from(menuName); + return menu.getType().equals("์Œ๋ฃŒ"); + } + + public int countDessert() { + if (isDessert()) { + return count; + } + return 0; + } + + private boolean isDessert() { + Menu menu = Menu.from(menuName); + return menu.getType().equals("๋””์ €ํŠธ"); + } + + public int countMain() { + if (isMain()) { + return count; + } + return 0; + } + + + private boolean isMain() { + Menu menu = Menu.from(menuName); + return menu.getType().equals("๋ฉ”์ธ"); + } + + public String getMenuName() { + return menuName; + } + + public int getCount() { + return count; + } + + @Override + public boolean equals(Object o) { + if (o instanceof Order order) { + return menuName.equals(order.menuName); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(menuName); + } + + private void validateMenuName(String menu) { + if (isInvalidMenu(menu)) { + throw new IllegalArgumentException(INVALID_ORDER.getMessage()); + } + } + + private boolean isInvalidMenu(String menu) { + return Menu.from(menu) == NONE; + } + + private void validateCount(int count) { + if (count < MIN_ORDER_COUNT) { + throw new IllegalArgumentException(INVALID_ORDER.getMessage()); + } + } +}
Java
hashCode๋ฅผ ์‚ฌ์šฉํ•˜์…จ๋„ค์š” !!!! ํ˜น์‹œ hashCode๋ฅผ ์‚ฌ์šฉํ•œ ์ด์œ ๋ฅผ ์—ฌ์ญค๋ด๋„ ๋ ๊นŒ์š”?๐Ÿ‘€
@@ -0,0 +1,106 @@ +package christmas.domain; + +import static christmas.domain.Error.INVALID_COUNT; +import static christmas.domain.Error.INVALID_ORDER; +import static christmas.domain.Error.INVALID_TYPE; + +import java.util.List; + +public class Orders { + private static final int MAX_TOTAL_ORDER_COUNT = 20; + private static final int LOWER_LIMIT_AMOUNT_FOR_BENEFIT = 10_000; + private static final int LOWER_LIMIT_AMOUNT_FOR_GIFT = 120_000; + + private final List<Order> orders; + + + public Orders(List<Order> orders) { + validateDuplicated(orders); + validateTotalCount(orders); + validateType(orders); + this.orders = orders; + } + + public int calculatePaymentAmount(int totalAmount, int totalBenefit) { + int paymentAmount = totalAmount - totalBenefit; + if (hasGift()) { + paymentAmount += Event.GIFT.getInitPrice(); + paymentAmount = minusValueToZero(paymentAmount); + return paymentAmount; + } + paymentAmount = minusValueToZero(paymentAmount); + return paymentAmount; + } + + private int minusValueToZero(int paymentAmount) { + return Math.max(paymentAmount, 0); + } + + public int calculateTotalAmount() { + return orders.stream() + .mapToInt(Order::calculateByMenu) + .sum(); + } + + public boolean isEligible() { + return calculateTotalAmount() > LOWER_LIMIT_AMOUNT_FOR_BENEFIT; + } + + public boolean hasGift() { + return calculateTotalAmount() >= LOWER_LIMIT_AMOUNT_FOR_GIFT; + } + + + public int countDesserts() { + return orders.stream() + .mapToInt(Order::countDessert) + .sum(); + } + + public int countMains() { + return orders.stream() + .mapToInt(Order::countMain) + .sum(); + } + + public List<Order> getOrders() { + return orders; + } + + private void validateDuplicated(List<Order> orders) { + if (isDuplicated(orders)) { + throw new IllegalArgumentException(INVALID_ORDER.getMessage()); + } + } + + private boolean isDuplicated(List<Order> orders) { + return orders.stream() + .distinct() + .count() != orders.size(); + } + + private void validateTotalCount(List<Order> orders) { + if (calculateTotalCount(orders) > MAX_TOTAL_ORDER_COUNT) { + throw new IllegalArgumentException(INVALID_COUNT.getMessage()); + } + } + + private int calculateTotalCount(List<Order> orders) { + return orders.stream() + .mapToInt(Order::getCount) + .sum(); + } + + private void validateType(List<Order> orders) { + if (isInvalidType(orders)) { + throw new IllegalArgumentException(INVALID_TYPE.getMessage()); + } + } + + private boolean isInvalidType(List<Order> orders) { + return orders.stream() + .allMatch(Order::isBeverage); + } + + +}
Java
Benefit๊ณผ gif์˜ limit ๊ธˆ์•ก๋ฅผ Order์—์„œ ์ •ํ•ด์ฃผ๋Š”๊ฒƒ๋ณด๋‹ค Benefit์— ์ฑ…์ž„์„ ๋ฌผ๋ ค์„œ ๊ฒ€์ฆํ•˜๋Š”๊ฑด ์–ด๋• ์„๊นŒ์š”?
@@ -0,0 +1,25 @@ +package christmas.view; + +enum Notice { + WELCOME("์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค."), + ASK_DATE_OF_VISIT("12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"), + ASK_MENU_AND_COUNT("์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)"), + PREVIEW("12์›” %s์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!%n"), + ORDER_MENU("\n<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"), + TOTAL_ORDER_AMOUNT("\n<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>"), + GIFT_MENU("\n<์ฆ์ • ๋ฉ”๋‰ด>"), + BENEFIT_LIST("\n<ํ˜œํƒ ๋‚ด์—ญ>"), + TOTAL_BENEFIT_AMOUNT("\n<์ดํ˜œํƒ ๊ธˆ์•ก>"), + TOTAL_PAYMENT_AMOUNT("\n<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>"), + EVENT_BADGE("\n<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>"); + + private final String message; + + Notice(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +}
Java
%s์ผ ์‚ฌ์šฉํ•˜์‹  ๊ฒƒ์ฒ˜๋Ÿผ 12์›”๋„ ๋™์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด๋ณผ ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™์•„์š” :) ๐Ÿ‘
@@ -0,0 +1,38 @@ +package christmas.domain; + +import static christmas.domain.Badge.NONE; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class BenefitsTest { + Benefits benefits; + + @Test + @DisplayName("ํ˜œํƒ์˜ ์ดํ•ฉ ๊ณ„์‚ฐ") + void calculateTotalBenefit() { + int totalBenefit = benefits.calculateTotalBenefitAmount(); + assertThat(totalBenefit).isEqualTo(31246); + } + + @ParameterizedTest + @DisplayName("์ด ํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ฅธ ๋ฑƒ์ง€ ์ง€๊ธ‰") + @CsvSource(value = {"5500,๋ณ„", "10500,ํŠธ๋ฆฌ", "21000,์‚ฐํƒ€"}) + void toEventBadge(int input, String expect) { + Badge badge = benefits.toEventBadge(input); + if (badge != NONE) { + assertThat(badge.getType()).isEqualTo(expect); + } + } + + @BeforeEach + void setUp() { + Date date = new Date(3); + Orders orders = Convertor.toOrders("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,๋ฐ”๋น„ํ๋ฆฝ-1,์ดˆ์ฝ”์ผ€์ดํฌ-2,์ œ๋กœ์ฝœ๋ผ-1"); + benefits = EventManager.toBenefits(orders, date); + } +} \ No newline at end of file
Java
CsvSouce๊ฐ€ ์žˆ์—ˆ๊ตฐ์š” ?!!! ์ €๋Š” ์ด๊ฑธ ๋ชฐ๋ผ์„œ ํ•œ๊ฐ€์ง€ ๋ณ€์ˆ˜๋กœ๋งŒ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋А๋ผ ์• ๋จน์€ ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋Š”๋ฐ ,, ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค :O ์œ ์šฉํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ์„๊ฑฐ๊ฐ™์•„์š”
@@ -0,0 +1,38 @@ +package christmas.domain; + +import static christmas.domain.Badge.NONE; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class BenefitsTest { + Benefits benefits; + + @Test + @DisplayName("ํ˜œํƒ์˜ ์ดํ•ฉ ๊ณ„์‚ฐ") + void calculateTotalBenefit() { + int totalBenefit = benefits.calculateTotalBenefitAmount(); + assertThat(totalBenefit).isEqualTo(31246); + } + + @ParameterizedTest + @DisplayName("์ด ํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ฅธ ๋ฑƒ์ง€ ์ง€๊ธ‰") + @CsvSource(value = {"5500,๋ณ„", "10500,ํŠธ๋ฆฌ", "21000,์‚ฐํƒ€"}) + void toEventBadge(int input, String expect) { + Badge badge = benefits.toEventBadge(input); + if (badge != NONE) { + assertThat(badge.getType()).isEqualTo(expect); + } + } + + @BeforeEach + void setUp() { + Date date = new Date(3); + Orders orders = Convertor.toOrders("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,๋ฐ”๋น„ํ๋ฆฝ-1,์ดˆ์ฝ”์ผ€์ดํฌ-2,์ œ๋กœ์ฝœ๋ผ-1"); + benefits = EventManager.toBenefits(orders, date); + } +} \ No newline at end of file
Java
setUp ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ ๊ฐ€๋…์„ฑ(?)์„ ์œ„ํ•ด ์ƒ๋‹จ์— ์œ„์น˜์‹œํ‚ค๋ฉด ๋” ์ข‹์„๊ฑฐ๊ฐ™์•„์š”
@@ -0,0 +1,70 @@ +# ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๋ชฉ๋ก + +## view + +- [x] **InputView** + - ๋ฐฉ๋ฌธ ๋‚ ์งœ๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ์ •์ˆ˜๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. + - ์ˆซ์ž ์ด์™ธ์˜ ๊ฐ’์„ ์ž…๋ ฅ์‹œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ ์‹œํ‚จ๋‹ค. + - ์ฃผ๋ฌธ ๋‚ด์šฉ์„ ์ž…๋ ฅ ๋ฐ›์•„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. +- [x] **Notice** + - InputView์™€ OutputView์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ €์žฅํ•œ๋‹ค. +- [x] **OutputView** + - ์—๋Ÿฌ ๋ฐœ์ƒ์‹œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. + - ์ธ์‚ฌ๋ง์„ ์ถœ๋ ฅํ•œ๋‹ค. + - ํ˜œํƒ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•œ๋‹ค. + - ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ์ถœ๋ ฅ ํ˜•์‹์— ๋งž๊ฒŒ ๊ฐ€๊ณตํ•œ๋‹ค. + +## domain + +- [x] **Date** + - ์ž…๋ ฅ ๋ฐ›์€ ๋‚ ์งœ๋ฅผ ์ €์žฅํ•œ๋‹ค. + - 1-31 ์ด์™ธ์˜ ๋‚ ์งœ๋ฅผ ๋ฐ›์œผ๋ฉด ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ ์‹œํ‚จ๋‹ค. + - 25์ผ ์ „์ธ์ง€ ํ™•์ธํ•œ๋‹ค. + - ์ฃผ๋ง์ธ์ง€ ํ™•์ธํ•œ๋‹ค. + - ์ด๋ฒคํŠธ ๋‹ฌ๋ ฅ์ƒ ๋ณ„์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. +- [x] **Convertor** + - ์ฃผ๋ฌธ ๋‚ด์šฉ์ด ๋‹ด๊ธด ๋ฌธ์ž์—ด์„ Order ๊ฐ์ฒด๋กœ ๊ตฌ์„ฑ๋œ Orders ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. + - ์ฝค๋งˆ(,) ๋‹จ์œ„๋กœ Order ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. + - ๋ฉ”๋‰ด์™€ ์ฃผ๋ฌธ๋Ÿ‰์„ Order ๊ฐ์ฒด์— ์ €์žฅํ•œ๋‹ค. + - ์˜ˆ์‹œ์™€ ๋‹ค๋ฅธ ํ˜•์‹์˜ ์ฃผ๋ฌธ ๋‚ด์šฉ์„ ๋ฐ›์œผ๋ฉด ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. +- [x] **Order** + - ์ž…๋ ฅ ๋ฐ›์€ ๋ฉ”๋‰ด๊ฐ€ ๋ฉ”๋‰ดํŒ์— ์—†์œผ๋ฉด ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. + - ๋ฉ”๋‰ด๋ณ„ ์ฃผ๋ฌธ๋Ÿ‰์ด 1๊ฐœ๋ณด๋‹ค ์ž‘์œผ๋ฉด ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. + - ์ฃผ๋ฌธ๋ณ„ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•œ๋‹ค. + - ๋ฉ”๋‰ด์˜ ์ข…๋ฅ˜๋ฅผ ํ™•์ธํ•œ๋‹ค. + - ๋ฉ”๋‰ด ์ข…๋ฅ˜๋ณ„ ๊ฐฏ์ˆ˜๋ฅผ ์„ผ๋‹ค. +- [x] **Orders** + - ๋ฉ”๋‰ด์˜ ์ค‘๋ณต์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์—ฌ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. + - ์ด ์ฃผ๋ฌธ๋Ÿ‰์ด 20๊ฐœ๊ฐ€ ๋„˜์œผ๋ฉด ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. + - ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•˜๋ฉด ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. + - ์ด ์ฃผ๋ฌธ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•œ๋‹ค. + - ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•œ๋‹ค. + - ๊ฒฐ์ œ ๊ธˆ์•ก์ด ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. + - ์ด๋ฒคํŠธ ์ฐธ์—ฌ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•œ๋‹ค. + - ์‚ฌ์€ํ’ˆ ์ฆ์ • ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•œ๋‹ค. + - ๋ฉ”๋‰ด ์ข…๋ฅ˜๋ณ„ ์ฃผ๋ฌธ๋Ÿ‰์„ ๊ณ„์‚ฐํ•œ๋‹ค. +- [x] **EventManager** + - ๊ฐ ์ด๋ฒคํŠธ๋ณ„ ์ ์šฉ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ณ  ํ˜œํƒ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•œ๋‹ค. + - ํ˜œํƒ ๋‚ด์—ญ์„ ๋‹ด์€ Benefits ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค. +- [x] **Benefits** + - ์ด ํ˜œํƒ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•œ๋‹ค. + - ํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ฅธ ๋ฑƒ์ง€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค. +- [x] **Menu** + - ๋ฉ”๋‰ด ์ด๋ฆ„, ๊ฐ€๊ฒฉ, ์ข…๋ฅ˜๋ฅผ ์ €์žฅํ•œ๋‹ค. + - ๋ฉ”๋‰ด ์ด๋ฆ„์„ ๋ฐ›์•„ ์ผ์น˜ํ•˜๋Š” ๋ฉ”๋‰ด ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. + - ์ผ์น˜ํ•˜๋Š” ๋ฉ”๋‰ด๊ฐ€ ์—†๋‹ค๋ฉด NONE์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. +- [x] **Event** + - ์ด๋ฒคํŠธ ์ด๋ฆ„, ์ดˆ๊ธฐ ์ ์šฉ ๊ธˆ์•ก, ๋‹จ์œ„๋‹น ์ ์šฉ ๊ธˆ์•ก์„ ์ €์žฅํ•œ๋‹ค. +- [x] **Badge** + - ๋ฑƒ์ง€ ์ข…๋ฅ˜, ๊ธฐ์ค€ ๊ธˆ์•ก์„ ์ €์žฅํ•œ๋‹ค. +- [x] **Error** + - ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ์— ์‚ฌ์šฉ๋˜๋Š” ๋‚ด์šฉ์„ ์ €์žฅํ•œ๋‹ค. + +## controller + +- [x] **EventController** + - ์˜ˆ์™ธ ๋ฐœ์ƒ์‹œ ํ•ด๋‹น ๋ถ€๋ถ„์„ ๋ฐ˜๋ณต์‹œํ‚จ๋‹ค. + - view์˜ ๋‚ด์šฉ์„ domain์— ์ „๋‹ฌํ•œ๋‹ค. + - domain์˜ ๋‚ด์šฉ์„ view์— ์ „๋‹ฌํ•œ๋‹ค. + +
Unknown
์ฝ”๋“œ๋ฆฌ๋ทฐ์— ์•ž์„œ, ๊ฐ์ฒด ๊ธฐ๋Šฅ๋ณ„๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ์–ด ์ „์ฒด ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ๋ฐ ๊ฐ ๊ฐ์ฒด์˜ ์—ญํ• ์„ ํŒŒ์•…ํ•˜๊ธฐ ํŽธ๋ฆฌํ–ˆ์–ด์š” ๐Ÿ‘
@@ -0,0 +1,76 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.Benefits; +import christmas.domain.Convertor; +import christmas.domain.Date; +import christmas.domain.EventManager; +import christmas.domain.Orders; +import christmas.view.InputView; +import christmas.view.OutputView; + +public class EventController { + public void start() { + OutputView.printWelcome(); + Date date = createDate(); + Orders orders = createOrders(); + int totalOrderAmount = createTotalOrderAmount(orders); + OutputView.printGift(orders.hasGift()); + Benefits benefits = createBenefits(orders, date); + int totalBenefitAmount = createTotalBenefitAmount(benefits); + createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount); + createBadge(benefits, totalBenefitAmount); + } + + private Date createDate() { + while (true) { + try { + Date date = new Date(InputView.readDate()); + OutputView.printPreview(date); + return date; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private Orders createOrders() { + while (true) { + try { + Orders orders = Convertor.toOrders(InputView.readOrder()); + OutputView.printOrders(orders); + return orders; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private int createTotalOrderAmount(Orders orders) { + int totalOrderAmount = orders.calculateTotalAmount(); + OutputView.printTotalOrderAmount(totalOrderAmount); + return totalOrderAmount; + } + + private Benefits createBenefits(Orders orders, Date date) { + Benefits benefits = EventManager.toBenefits(orders, date); + OutputView.printBenefitDetail(benefits); + return benefits; + } + + private int createTotalBenefitAmount(Benefits benefits) { + int totalBenefitAmount = benefits.calculateTotalBenefitAmount(); + OutputView.printTotalBenefitAmount(totalBenefitAmount); + return totalBenefitAmount; + } + + private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) { + int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount); + OutputView.printPaymentAmount(paymentAmount); + } + + private void createBadge(Benefits benefits, int totalBenefitAmount) { + Badge badge = benefits.toEventBadge(totalBenefitAmount); + OutputView.printEventBadge(badge); + } +}
Java
๊ฐœํ–‰๋„ ํ•˜๋‚˜์˜ ์ปจ๋ฒค์…˜์ด๋‹ค! ์กฐ๊ธˆ ๋” ๊ธฐ๋Šฅ๋ณ„๋กœ ๋ถ„๋ฆฌํ•ด์„œ ๊ฐœํ–‰์œผ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค๋ฉด ๋” ์ฝ๊ธฐ ํŽธํ•  ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,76 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.Benefits; +import christmas.domain.Convertor; +import christmas.domain.Date; +import christmas.domain.EventManager; +import christmas.domain.Orders; +import christmas.view.InputView; +import christmas.view.OutputView; + +public class EventController { + public void start() { + OutputView.printWelcome(); + Date date = createDate(); + Orders orders = createOrders(); + int totalOrderAmount = createTotalOrderAmount(orders); + OutputView.printGift(orders.hasGift()); + Benefits benefits = createBenefits(orders, date); + int totalBenefitAmount = createTotalBenefitAmount(benefits); + createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount); + createBadge(benefits, totalBenefitAmount); + } + + private Date createDate() { + while (true) { + try { + Date date = new Date(InputView.readDate()); + OutputView.printPreview(date); + return date; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private Orders createOrders() { + while (true) { + try { + Orders orders = Convertor.toOrders(InputView.readOrder()); + OutputView.printOrders(orders); + return orders; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private int createTotalOrderAmount(Orders orders) { + int totalOrderAmount = orders.calculateTotalAmount(); + OutputView.printTotalOrderAmount(totalOrderAmount); + return totalOrderAmount; + } + + private Benefits createBenefits(Orders orders, Date date) { + Benefits benefits = EventManager.toBenefits(orders, date); + OutputView.printBenefitDetail(benefits); + return benefits; + } + + private int createTotalBenefitAmount(Benefits benefits) { + int totalBenefitAmount = benefits.calculateTotalBenefitAmount(); + OutputView.printTotalBenefitAmount(totalBenefitAmount); + return totalBenefitAmount; + } + + private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) { + int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount); + OutputView.printPaymentAmount(paymentAmount); + } + + private void createBadge(Benefits benefits, int totalBenefitAmount) { + Badge badge = benefits.toEventBadge(totalBenefitAmount); + OutputView.printEventBadge(badge); + } +}
Java
![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-11-17 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 10 16 39](https://github.com/parksangchu/java-christmas-6-parksangchu/assets/112257466/f75ebc13-0ec4-4cf1-976c-766f1db54275) ์ƒ์ถ”๋‹˜๊ป˜์„œ ๋ฆฌ๋ทฐ ์ฃผ์…จ๋˜ ๋‚ด์šฉ์— ๋Œ€ํ•ด์„œ, ํ•œ๋ฒˆ ์—ฌ์ญ™๊ณ  ์‹ถ์€ ๋ถ€๋ถ„์ด ์ƒ๊ฒจ ์—ฌ๊ธฐ์—๋„ ๊ธ€์„ ๋‚จ๊ฒจ๋ด…๋‹ˆ๋‹ค. 1. ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์ „์ฒด ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๊ด€ํ• ํ•˜๊ณ  ์žˆ์–ด์š”. `์ปจํŠธ๋กค๋Ÿฌ Layer ๋‹จ์œ„ํ…Œ์ŠคํŠธ = ํ†ตํ•ฉํ…Œ์ŠคํŠธ`์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ, ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ถ„๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์„๊นŒ์š”? 1-1. ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ถ„๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๊ณ  ํŒ๋‹จ๋˜๋ฉด, EventController(Main์˜ ๊ธฐ๋Šฅ)๊ฐ€ ๋‹ค๋ฅธ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์˜์กดํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ปจํŠธ๋กค๋Ÿฌ๋Š” View์™€ Domain์„ ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• ์ธ๋ฐ, ์ปจํŠธ๋กค๋Ÿฌ๊ฐ„ ํ˜ธ์ถœ์ด ์žฆ์•„์งˆ ๊ฒƒ ๊ฐ™์•„์š”. 1-2. EventController์—์„œ ๋ชจ๋“  ๋กœ์ง์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๊ณผ, Application.main์—์„œ ๋ชจ๋“  ๋กœ์ง์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,56 @@ +package christmas.domain; + + +import static christmas.domain.Badge.NONE; +import static christmas.domain.Badge.SANTA; +import static christmas.domain.Badge.STAR; +import static christmas.domain.Badge.TREE; + +import java.util.Map; + +public class Benefits { + private final Map<Event, Integer> benefits; + + public Benefits(Map<Event, Integer> benefits) { + this.benefits = benefits; + } + + public int calculateTotalBenefitAmount() { + return benefits.values() + .stream() + .mapToInt(amount -> amount) + .sum(); + } + + public Badge toEventBadge(int totalBenefit) { + if (isStarBadge(totalBenefit)) { + return STAR; + } + if (isTreeBadge(totalBenefit)) { + return TREE; + } + if (isSantaBadge(totalBenefit)) { + return SANTA; + } + return NONE; + } + + private boolean isStarBadge(int totalBenefit) { + return totalBenefit >= STAR.getAmount() && totalBenefit < TREE.getAmount(); + } + + private boolean isTreeBadge(int totalBenefit) { + return totalBenefit >= TREE.getAmount() && totalBenefit < SANTA.getAmount(); + } + + private boolean isSantaBadge(int totalBenefit) { + return totalBenefit >= SANTA.getAmount(); + } + + public Map<Event, Integer> getBenefits() { + return benefits; + } +} + + +
Java
๋งŒ์•ฝ ๋ฑƒ์ง€๊ฐ€ 100๊ฐœ๋ผ๋ฉด?, 1000๊ฐœ๋ผ๋ฉด? ๋ฑƒ์ง€์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋งŽ์•„์ง€๋Š” ์ƒํ™ฉ์—์„œ ๋ชจ๋“  ๋กœ์ง์„ ํ•˜๋“œ์ฝ”๋”ฉ ํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์ด ์ƒ๊ธธ ๊ฒƒ ๊ฐ™์•„์š”. ์ด ๋ถ€๋ถ„๋„, ํ•˜๋“œ์ฝ”๋”ฉ์ด ์•„๋‹ˆ๋ผ, ๋ฑƒ์ง€์˜ ์†์„ฑ์— ๋”ฐ๋ผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐฉ๋ฒ•์„ ๋ชจ์ƒ‰ํ•ด๋ณด์‹œ๋ฉด, ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,51 @@ +package christmas.domain; + +import static christmas.domain.Error.INVALID_DATE; +import static java.time.DayOfWeek.FRIDAY; +import static java.time.DayOfWeek.SATURDAY; +import static java.time.DayOfWeek.SUNDAY; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +public class Date { + private static final int MIN_DATE = 1; + private static final int MAX_DATE = 31; + private static final int THIS_YEAR = 2023; + private static final int THIS_MONTH = 12; + private static final int CHRISTMAS_DATE = 25; + private final int value; + + public Date(int value) { + validateRange(value); + this.value = value; + } + + public boolean isDayBeforeChristmas() { + return value <= CHRISTMAS_DATE; + } + + public boolean isWeekend() { + DayOfWeek dayOfWeek = createDayOfWeek(); + return dayOfWeek == FRIDAY || dayOfWeek == SATURDAY; + } + + public boolean isStarDay() { + return createDayOfWeek() == SUNDAY || value == CHRISTMAS_DATE; + } + + public int getValue() { + return value; + } + + private DayOfWeek createDayOfWeek() { + LocalDate localDate = LocalDate.of(THIS_YEAR, THIS_MONTH, value); + return localDate.getDayOfWeek(); + } + + private void validateRange(int value) { + if (value > MAX_DATE || value < MIN_DATE) { + throw new IllegalArgumentException(INVALID_DATE.getMessage()); + } + } +} \ No newline at end of file
Java
ํ•ด๋‹น ์ด๋ฒคํŠธ๋Š” 1์›”์—๋„ ์„ ๋ฌผ์„ ์ฆ์ •ํ•˜๋ฉด์„œ, `์žฌ์‚ฌ์šฉ ์˜ˆ์ •`์ž„์„ ์€์—ฐ์ค‘์— ์•”์‹œํ–ˆ์–ด์š”. int ํƒ€์ž… ํ•˜๋“œ์ฝ”๋”ฉ์ด ์•„๋‹ˆ๋ผ, LocalDate๋ฅผ ํ™œ์šฉํ•ด 2023๋…„ 12์›”๊ณผ 2024๋…„ 1์›”์„ ๋ผ์›Œ๋„ฃ์–ด์„œ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„ํ•˜๋ฉด, ๋” ์œ ์—ฐํ•œ ์„ค๊ณ„๊ฐ€ ๋˜์—ˆ์„ ๊ฒƒ์ด๋ผ ์ƒ๊ฐ๋˜์–ด์š”!
@@ -0,0 +1,33 @@ +package christmas.domain; + +import static christmas.domain.Menu.CHAMPAGNE; + +public enum Event { + CHRISTMAS_D_DAY("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ", 1_000, 100), + WEEKDAY("ํ‰์ผ ํ• ์ธ", 0, 2_023), + WEEKEND("์ฃผ๋ง ํ• ์ธ", 0, 2_023), + SPECIAL("ํŠน๋ณ„ ํ• ์ธ", 1_000, 0), + GIFT("์ฆ์ • ์ด๋ฒคํŠธ", CHAMPAGNE.getPrice(), 0); + + private final String eventName; + private final int initPrice; + private final int unitPrice; + + Event(String eventName, int initPrice, int unitPrice) { + this.eventName = eventName; + this.initPrice = initPrice; + this.unitPrice = unitPrice; + } + + public String getEventName() { + return eventName; + } + + public int getInitPrice() { + return initPrice; + } + + public int getUnitPrice() { + return unitPrice; + } +}
Java
`initPrice` ์ง๊ด€์ ์œผ๋กœ ๋ณ€์ˆ˜๋ช…๋งŒ ๋ดค์„ ๋•Œ, ์–ด๋–ค int๊ฐ’์„ ๋‹ด๋Š”์ง€ ๋ชจํ˜ธํ•œ ๊ฐ์ด ์žˆ์–ด์š”
@@ -0,0 +1,60 @@ +package christmas.domain; + +import static christmas.domain.Event.CHRISTMAS_D_DAY; +import static christmas.domain.Event.GIFT; +import static christmas.domain.Event.SPECIAL; +import static christmas.domain.Event.WEEKDAY; +import static christmas.domain.Event.WEEKEND; + +import java.util.EnumMap; +import java.util.Map; + +public class EventManager { + + public static Benefits toBenefits(Orders orders, Date date) { + Map<Event, Integer> benefits = new EnumMap<>(Event.class); + if (orders.isEligible()) { + benefits.put(CHRISTMAS_D_DAY, calculateDDayDiscount(date)); + benefits.put(WEEKDAY, calculateWeekdayDiscount(orders, date)); + benefits.put(WEEKEND, calculateWeekendDiscount(orders, date)); + benefits.put(SPECIAL, calculateSpecialDiscount(date)); + benefits.put(GIFT, calculateGiftAmount(orders)); + } + return new Benefits(benefits); + } + + private static int calculateDDayDiscount(Date date) { + if (date.isDayBeforeChristmas()) { + return CHRISTMAS_D_DAY.getInitPrice() + ((date.getValue() - 1) * CHRISTMAS_D_DAY.getUnitPrice()); + } + return 0; + } + + private static int calculateWeekdayDiscount(Orders orders, Date date) { + if (!date.isWeekend()) { + return orders.countDesserts() * WEEKDAY.getUnitPrice(); + } + return 0; + } + + private static int calculateWeekendDiscount(Orders orders, Date date) { + if (date.isWeekend()) { + return orders.countMains() * WEEKEND.getUnitPrice(); + } + return 0; + } + + private static int calculateSpecialDiscount(Date date) { + if (date.isStarDay()) { + return SPECIAL.getInitPrice(); + } + return 0; + } + + private static int calculateGiftAmount(Orders orders) { + if (orders.hasGift()) { + return GIFT.getInitPrice(); + } + return 0; + } +}
Java
์˜ค๋Š˜ ์ผ์ˆ˜ * 100 + 900 ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค >_<
@@ -0,0 +1,25 @@ +package christmas.view; + +enum Notice { + WELCOME("์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค."), + ASK_DATE_OF_VISIT("12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"), + ASK_MENU_AND_COUNT("์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)"), + PREVIEW("12์›” %s์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!%n"), + ORDER_MENU("\n<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"), + TOTAL_ORDER_AMOUNT("\n<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>"), + GIFT_MENU("\n<์ฆ์ • ๋ฉ”๋‰ด>"), + BENEFIT_LIST("\n<ํ˜œํƒ ๋‚ด์—ญ>"), + TOTAL_BENEFIT_AMOUNT("\n<์ดํ˜œํƒ ๊ธˆ์•ก>"), + TOTAL_PAYMENT_AMOUNT("\n<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>"), + EVENT_BADGE("\n<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>"); + + private final String message; + + Notice(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +}
Java
์ €๋„ @ddrongy๋‹˜์˜ ์˜๊ฒฌ์— ๋™์˜ํ•ฉ๋‹ˆ๋‹ค!,๊ทธ๋ ‡๋‹ค๋ฉด 1์›”๋กœ ๋ฐ”๋€Œ์—ˆ์„๋•Œ, (2024๋…„ 1์›”) System Constraint๋„ ์ž๋™์œผ๋กœ ๋ฐ”๋€Œ๋„๋ก(LocalDate..?) ์„ค์ •ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ˆ˜์ •๋„ ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,76 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.Benefits; +import christmas.domain.Convertor; +import christmas.domain.Date; +import christmas.domain.EventManager; +import christmas.domain.Orders; +import christmas.view.InputView; +import christmas.view.OutputView; + +public class EventController { + public void start() { + OutputView.printWelcome(); + Date date = createDate(); + Orders orders = createOrders(); + int totalOrderAmount = createTotalOrderAmount(orders); + OutputView.printGift(orders.hasGift()); + Benefits benefits = createBenefits(orders, date); + int totalBenefitAmount = createTotalBenefitAmount(benefits); + createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount); + createBadge(benefits, totalBenefitAmount); + } + + private Date createDate() { + while (true) { + try { + Date date = new Date(InputView.readDate()); + OutputView.printPreview(date); + return date; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private Orders createOrders() { + while (true) { + try { + Orders orders = Convertor.toOrders(InputView.readOrder()); + OutputView.printOrders(orders); + return orders; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private int createTotalOrderAmount(Orders orders) { + int totalOrderAmount = orders.calculateTotalAmount(); + OutputView.printTotalOrderAmount(totalOrderAmount); + return totalOrderAmount; + } + + private Benefits createBenefits(Orders orders, Date date) { + Benefits benefits = EventManager.toBenefits(orders, date); + OutputView.printBenefitDetail(benefits); + return benefits; + } + + private int createTotalBenefitAmount(Benefits benefits) { + int totalBenefitAmount = benefits.calculateTotalBenefitAmount(); + OutputView.printTotalBenefitAmount(totalBenefitAmount); + return totalBenefitAmount; + } + + private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) { + int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount); + OutputView.printPaymentAmount(paymentAmount); + } + + private void createBadge(Benefits benefits, int totalBenefitAmount) { + Badge badge = benefits.toEventBadge(totalBenefitAmount); + OutputView.printEventBadge(badge); + } +}
Java
๋ชจ๋“  ๊ฒ€์ฆ์„ InputView์—์„œ ๋ฐ›๋Š”๋‹ค๋ฉด ์น˜์šฐ๋‹˜ ๋ง์”€๋Œ€๋กœ ํ•˜๋Š”๊ฒŒ ๋งž๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค! ํ•˜์ง€๋งŒ ๋‚ ์งœ๊ฐ€ 1 - 31์ผ ์ค‘ ํ•˜๋‚˜์—ฌ์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ๊ฒ€์ฆ์€ VIew๊ฐ€ ์•„๋‹ˆ๋ผ Domain ์—์„œ ์ด๋ค„์ง€๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ์ ์œผ๋กœ View์™€ Domain ์–‘์ชฝ์—์„œ ๊ฒ€์ฆ์ด ์ด๋ค„์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์–‘์ชฝ์˜ ํ†ต๋กœ ์—ญํ• ์„ ํ•˜๋Š” controller์—์„œ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋งž๋‹ค๊ณ  ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค~
@@ -0,0 +1,76 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.Benefits; +import christmas.domain.Convertor; +import christmas.domain.Date; +import christmas.domain.EventManager; +import christmas.domain.Orders; +import christmas.view.InputView; +import christmas.view.OutputView; + +public class EventController { + public void start() { + OutputView.printWelcome(); + Date date = createDate(); + Orders orders = createOrders(); + int totalOrderAmount = createTotalOrderAmount(orders); + OutputView.printGift(orders.hasGift()); + Benefits benefits = createBenefits(orders, date); + int totalBenefitAmount = createTotalBenefitAmount(benefits); + createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount); + createBadge(benefits, totalBenefitAmount); + } + + private Date createDate() { + while (true) { + try { + Date date = new Date(InputView.readDate()); + OutputView.printPreview(date); + return date; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private Orders createOrders() { + while (true) { + try { + Orders orders = Convertor.toOrders(InputView.readOrder()); + OutputView.printOrders(orders); + return orders; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private int createTotalOrderAmount(Orders orders) { + int totalOrderAmount = orders.calculateTotalAmount(); + OutputView.printTotalOrderAmount(totalOrderAmount); + return totalOrderAmount; + } + + private Benefits createBenefits(Orders orders, Date date) { + Benefits benefits = EventManager.toBenefits(orders, date); + OutputView.printBenefitDetail(benefits); + return benefits; + } + + private int createTotalBenefitAmount(Benefits benefits) { + int totalBenefitAmount = benefits.calculateTotalBenefitAmount(); + OutputView.printTotalBenefitAmount(totalBenefitAmount); + return totalBenefitAmount; + } + + private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) { + int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount); + OutputView.printPaymentAmount(paymentAmount); + } + + private void createBadge(Benefits benefits, int totalBenefitAmount) { + Badge badge = benefits.toEventBadge(totalBenefitAmount); + OutputView.printEventBadge(badge); + } +}
Java
e๋ผ๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ž๋ฐ”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ๋“ค๋ผ๋ฆฌ์˜ ์•ฝ์†์ธ๊ฑฐ ๊ฐ™์€๋ฐ ์ด๋ถ€๋ถ„์€ ํ•œ๋ฒˆ ๋Œ€ํ™”๋ฅผ ๋‚˜๋ˆ ๋ณด๋ฉด ์ข‹์„๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,76 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.Benefits; +import christmas.domain.Convertor; +import christmas.domain.Date; +import christmas.domain.EventManager; +import christmas.domain.Orders; +import christmas.view.InputView; +import christmas.view.OutputView; + +public class EventController { + public void start() { + OutputView.printWelcome(); + Date date = createDate(); + Orders orders = createOrders(); + int totalOrderAmount = createTotalOrderAmount(orders); + OutputView.printGift(orders.hasGift()); + Benefits benefits = createBenefits(orders, date); + int totalBenefitAmount = createTotalBenefitAmount(benefits); + createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount); + createBadge(benefits, totalBenefitAmount); + } + + private Date createDate() { + while (true) { + try { + Date date = new Date(InputView.readDate()); + OutputView.printPreview(date); + return date; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private Orders createOrders() { + while (true) { + try { + Orders orders = Convertor.toOrders(InputView.readOrder()); + OutputView.printOrders(orders); + return orders; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private int createTotalOrderAmount(Orders orders) { + int totalOrderAmount = orders.calculateTotalAmount(); + OutputView.printTotalOrderAmount(totalOrderAmount); + return totalOrderAmount; + } + + private Benefits createBenefits(Orders orders, Date date) { + Benefits benefits = EventManager.toBenefits(orders, date); + OutputView.printBenefitDetail(benefits); + return benefits; + } + + private int createTotalBenefitAmount(Benefits benefits) { + int totalBenefitAmount = benefits.calculateTotalBenefitAmount(); + OutputView.printTotalBenefitAmount(totalBenefitAmount); + return totalBenefitAmount; + } + + private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) { + int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount); + OutputView.printPaymentAmount(paymentAmount); + } + + private void createBadge(Benefits benefits, int totalBenefitAmount) { + Badge badge = benefits.toEventBadge(totalBenefitAmount); + OutputView.printEventBadge(badge); + } +}
Java
> ์ „์ฒด์ ์œผ๋กœ create๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ช…์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ create๊ฐ€ ๊ฐ์ฒด ์ƒ์„ฑ์˜ ๋А๋‚Œ์„ ์ฃผ๋Š”๊ฑฐ๊ฐ™์•„์„œ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ฉ”์„œ๋“œ๋ช…์„ ์ž‘์„ฑํ•ด๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š” ? ์‹ค์ œ๋กœ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ๋ฆฌํ„ดํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๊ณ  ์žˆ์–ด์„œ create ๋ผ๊ณ  ๋ช…๋ช…ํ–ˆ๋Š”๋ฐ ํ˜น์‹œ ๋‹ค๋ฅธ ์ข‹์€ ์ด๋ฆ„์ด ์žˆ์„๊นŒ์š”~?
@@ -0,0 +1,24 @@ +package christmas.domain; + +public enum Badge { + STAR("๋ณ„", 5_000), + TREE("ํŠธ๋ฆฌ", 10_000), + SANTA("์‚ฐํƒ€", 20_000), + NONE("์—†์Œ", 0); + + private final String type; + private final int amount; + + Badge(String type, int amount) { + this.type = type; + this.amount = amount; + } + + public String getType() { + return type; + } + + public int getAmount() { + return amount; + } +}
Java
์ธํ…”๋ฆฌ์ œ์ด ์ž๋™ ์ƒ์„ฑ๊ธฐ๋Šฅ์œผ๋กœ ๋งŒ๋“ ๊ฑด๋ฐ ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” ์ƒ๊ฐํ•ด๋ณด์ง€ ๋ชปํ–ˆ๋„ค์š” ใ…œใ…œ private๋กœ ์ง€์ •ํ•˜๋Š”๊ฒŒ ๋งž๋Š”๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -41,7 +41,7 @@ public Object checkReviewGroupAccess(ProceedingJoinPoint joinPoint, private boolean canMemberAccess(ReviewGroup reviewGroup, HttpServletRequest request) { GitHubMember gitHubMember = sessionManager.getGitHubMember(request); - return gitHubMember != null && reviewGroup.getMemberId() == gitHubMember.getMemberId(); + return gitHubMember != null && reviewGroup.isMadeByMember(gitHubMember.getMemberId()); } private boolean canGuestAccess(ReviewGroup reviewGroup, HttpServletRequest request) {
Java
**๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๋ถ€๋ถ„์€ ์ด ๋ถ€๋ถ„์ด์—ˆ์Šต๋‹ˆ๋‹ค.** ํšŒ์›์ด ์ ‘๊ทผํ•˜๋ ค๋Š” ๋ฆฌ๋ทฐ ๊ทธ๋ฃน์ด "๋น„ํšŒ์›์ด ๋งŒ๋“  ๋ฆฌ๋ทฐ๊ทธ๋ฃน"์ธ ๊ฒฝ์šฐ, reviewGroup.getMemberId() ๊ฐ€ null ์ด๊ธฐ ๋•Œ๋ฌธ์— long์ธ gitHubMember.getMemberId() ์™€์˜ == ์—ฐ์‚ฐ์„ ๋ชปํ•˜๋ฏ€๋กœ NPE๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ผ์ด ์ƒ๊ธฐ์ง€ ์•Š๊ฒŒํ•˜๋ ค๋ฉด 1์ฐจ์›์ ์œผ๋กœ `gitHubMember != null && (reviewGroup.getMemberId() != null && reviewGroup.getMemberId() == gitHubMember.getMemberId())` ๋ฅผ ํ• ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋„ˆ๋ฌด ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ ธ์„œ ํŽธ์˜ ๋ฉ”์„œ๋“œ isMadeByMember๋ฅผ ์ถ”๊ฐ€ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.
@@ -57,6 +57,13 @@ void setUp() { RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); } + @Test + void ์กด์žฌํ•˜์ง€_์•Š๋Š”_๋ฆฌ๋ทฐ์—_์ ‘๊ทผํ•˜๋ฉด_NotFound_์˜ˆ์™ธ๊ฐ€_๋ฐœ์ƒํ•œ๋‹ค() { + // when & then + assertThatCode(() -> aopTestClass.testReviewMethod(1L)) + .isInstanceOf(ReviewNotFoundException.class); + } + @Nested class ์„ฑ๊ณต์ ์œผ๋กœ_๋ฆฌ๋ทฐ์—_์ ‘๊ทผํ• _์ˆ˜_์žˆ๋‹ค { @@ -90,7 +97,7 @@ class ์„ฑ๊ณต์ ์œผ๋กœ_๋ฆฌ๋ทฐ์—_์ ‘๊ทผํ• _์ˆ˜_์žˆ๋‹ค { } @Test - void ๋น„ํšŒ์›์€_์ž์‹ ์ด_๋งŒ๋“ _๋ฆฌ๋ทฐ_๊ทธ๋ฃน์—_์ž‘์„ฑ๋œ_๋ฆฌ๋ทฐ์—_์ ‘๊ทผํ• _์ˆ˜_์žˆ๋‹ค() { + void ๋ฆฌ๋ทฐ_๊ทธ๋ฃน์„_์ธ์ฆํ•œ_๋น„ํšŒ์›์€_๋ฆฌ๋ทฐ_๊ทธ๋ฃน์—_์ž‘์„ฑ๋œ_๋ฆฌ๋ทฐ์—_์ ‘๊ทผํ• _์ˆ˜_์žˆ๋‹ค() { // given ReviewGroup reviewGroup = reviewGroupRepository.save(๋น„ํšŒ์›_๋ฆฌ๋ทฐ_๊ทธ๋ฃน()); Review review = reviewRepository.save(๋น„ํšŒ์›_์ž‘์„ฑ_๋ฆฌ๋ทฐ(1L, reviewGroup.getId(), List.of())); @@ -102,13 +109,6 @@ class ์„ฑ๊ณต์ ์œผ๋กœ_๋ฆฌ๋ทฐ์—_์ ‘๊ทผํ• _์ˆ˜_์žˆ๋‹ค { } } - @Test - void ์กด์žฌํ•˜์ง€_์•Š๋Š”_๋ฆฌ๋ทฐ์—_์ ‘๊ทผํ•˜๋ฉด_NotFound_์˜ˆ์™ธ๊ฐ€_๋ฐœ์ƒํ•œ๋‹ค() { - // when & then - assertThatCode(() -> aopTestClass.testReviewMethod(1L)) - .isInstanceOf(ReviewNotFoundException.class); - } - @Nested class ์œ ํšจํ•˜์ง€_์•Š์€_์„ธ์…˜์œผ๋กœ_์ ‘๊ทผํ•˜๋ฉด_์˜ˆ์™ธ๊ฐ€_๋ฐœ์ƒํ•œ๋‹ค {
Java
์†Œ์†Œํ•˜๊ฒŒ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๋ฆฌํŒฉํ„ฐ๋งํ–ˆ์Šต๋‹ˆ๋‹ค. ์ƒ๊ฐํ•ด๋ณด๋ฉด ๋น„ํšŒ์›์€ ์ž๊ธฐ๊ฐ€ ๋งŒ๋“ค์ง€ ์•Š์•˜๋”๋ผ๋„ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜๋ฉด ํ†ต๊ณผ๋˜๋Š”๊ฑฐ๋ผ "๋ฆฌ๋ทฐ ๊ทธ๋ฃน์„ ์ธ์ฆํ•œ" ์œผ๋กœ ๋ช…ํ™•ํ•œ ์˜๋ฏธ๋ฅผ ์ „๋‹ฌํ•˜๋ ค ํ–ˆ์”๋‹ˆ๋‹ค~
@@ -0,0 +1,37 @@ +import CONSTANTS from '../constants/constants.js'; +import ERROR from '../constants/error.js'; + +class NumbersValidator { + static validateNumbers(numbers) { + const validators = [ + this.#validateEmpty, + this.#validateNegative, + this.#validateNaN, + this.#validateLength, + this.#validateDuplicated, + ]; + validators.forEach(validator => validator(numbers)); + } + + static #validateLength(numbers) { + if (numbers.length !== CONSTANTS.number.numberSize) throw new Error(ERROR.numbers.length); + } + + static #validateNaN(numbers) { + if (Number.isNaN(Number(numbers))) throw new Error(ERROR.numbers.notANumber); + } + + static #validateNegative(numbers) { + if (Number(numbers) < CONSTANTS.number.zero) throw new Error(ERROR.numbers.negative); + } + + static #validateDuplicated(numbers) { + if (numbers.length !== new Set(numbers).size) throw new Error(ERROR.numbers.duplicated); + } + + static #validateEmpty(numbers) { + if (numbers.length === CONSTANTS.number.zero) throw new Error(ERROR.numbers.empty); + } +} + +export default NumbersValidator;
JavaScript
๊ฐ ๋งค์„œ๋“œ๋“ค์„ ๋ฐ”๋กœ ํ˜ธ์ถœ์‹œํ‚ค์ง€ ์•Š๊ณ  ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ forEach ๋ฉ”์„œ๋“œ๋กœ ํ•˜๋‚˜์”ฉ ํ˜ธ์ถœ์‹œํ‚จ ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”!
@@ -0,0 +1,20 @@ +import { Console } from '@woowacourse/mission-utils'; +import MESSAGE from '../constants/message.js'; +import NumbersValidator from '../validators/NumbersValidator.js'; +import RestartValidator from '../validators/RestartValidator.js'; + +const InputView = { + async readNumbers() { + const numbers = await Console.readLineAsync(MESSAGE.read.numbers); + NumbersValidator.validateNumbers(numbers); + return [...numbers].map(Number); + }, + + async readRestart() { + const restart = await Console.readLineAsync(MESSAGE.read.restart); + RestartValidator.validateRestart(restart); + return Number(restart); + }, +}; + +export default InputView;
JavaScript
๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ์ถœ๋ ฅํ•˜๋Š” view์˜ ์—ญํ• ์„ ์ƒ๊ฐํ–ˆ์„ ๋•Œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋‹จ๊ณ„๊ฐ€ view์— ์žˆ๋Š”๊ฒŒ ๋งž๋Š”์ง€ ๊ถ๊ธˆ์ฆ์ด ๋“œ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹น :)
@@ -0,0 +1,42 @@ +import CONSTANTS from '../constants/constants.js'; +import MESSAGE from '../constants/message.js'; + +class Hint { + #numbers; + + #computerNumbers; + + constructor(numbers, computerNumbers) { + this.#numbers = numbers; + this.#computerNumbers = computerNumbers; + } + + calculateStrikeCount() { + return this.#numbers.reduce( + (count, digit, index) => (digit === this.#computerNumbers[index] ? count + 1 : count), + 0, + ); + } + + calculateBallCount(strikeCount) { + return ( + this.#numbers.reduce( + (count, digit) => (this.#computerNumbers.includes(digit) ? count + 1 : count), + 0, + ) - strikeCount + ); + } + + generateHintMessage(strikeCount, ballCount) { + const hintMessage = []; + if (ballCount !== CONSTANTS.number.zero) hintMessage.push(`${ballCount}${MESSAGE.print.ball}`); + if (strikeCount !== CONSTANTS.number.zero) + hintMessage.push(`${strikeCount}${MESSAGE.print.strike}`); + if (ballCount === CONSTANTS.number.zero && strikeCount === CONSTANTS.number.zero) + hintMessage.push(MESSAGE.print.nothing); + + return hintMessage; + } +} + +export default Hint;
JavaScript
์ฒ˜์Œ์— Hint ํด๋ž˜์Šค๋ช…์— ์˜๋ฌธ์ด ๋“ค์—ˆ๋Š”๋ฐ ๊ฒŒ์ž„์ด๋‹ˆ๊นŒ ์–ผ๋งˆ๋‚˜ ๋งž์ท„๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์ด๋ผ ์ ์ ˆํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”! ๐Ÿ‘๐Ÿป
@@ -0,0 +1,20 @@ +import { Console } from '@woowacourse/mission-utils'; +import MESSAGE from '../constants/message.js'; +import NumbersValidator from '../validators/NumbersValidator.js'; +import RestartValidator from '../validators/RestartValidator.js'; + +const InputView = { + async readNumbers() { + const numbers = await Console.readLineAsync(MESSAGE.read.numbers); + NumbersValidator.validateNumbers(numbers); + return [...numbers].map(Number); + }, + + async readRestart() { + const restart = await Console.readLineAsync(MESSAGE.read.restart); + RestartValidator.validateRestart(restart); + return Number(restart); + }, +}; + +export default InputView;
JavaScript
์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ์ €๋„ ๊ณ ๋ฏผ์„ ๋งŽ์ด ํ•ด๋ดค๋Š”๋ฐ ํ”„๋ก ํŠธ(InputVIew)์™€ ๋ฐฑ์—”๋“œ(model)์˜ ์—ญํ• ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ, view์™€ model ๋‘˜ ๋‹ค ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋งž๋‹ค๊ณ  ํŒ๋‹จํ•˜์—ฌ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,37 @@ +import CONSTANTS from '../constants/constants.js'; +import ERROR from '../constants/error.js'; + +class NumbersValidator { + static validateNumbers(numbers) { + const validators = [ + this.#validateEmpty, + this.#validateNegative, + this.#validateNaN, + this.#validateLength, + this.#validateDuplicated, + ]; + validators.forEach(validator => validator(numbers)); + } + + static #validateLength(numbers) { + if (numbers.length !== CONSTANTS.number.numberSize) throw new Error(ERROR.numbers.length); + } + + static #validateNaN(numbers) { + if (Number.isNaN(Number(numbers))) throw new Error(ERROR.numbers.notANumber); + } + + static #validateNegative(numbers) { + if (Number(numbers) < CONSTANTS.number.zero) throw new Error(ERROR.numbers.negative); + } + + static #validateDuplicated(numbers) { + if (numbers.length !== new Set(numbers).size) throw new Error(ERROR.numbers.duplicated); + } + + static #validateEmpty(numbers) { + if (numbers.length === CONSTANTS.number.zero) throw new Error(ERROR.numbers.empty); + } +} + +export default NumbersValidator;
JavaScript
์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ๋ชจ๋‘ ์‹œํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•จ์œผ๋กœ์„œ ๊ฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ฐ€ ์‹œํ–‰๋˜๋„๋ก ์ž‘์„ฑํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,38 @@ +# ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ฒดํฌ๋ฆฌ์ŠคํŠธ + +## ๊ธฐ๋Šฅ ๋ชฉ๋ก + +### ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ + +- [x] ์„ธ ์ž๋ฆฌ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. + - [x] ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ (1 ~ 9) - ์ •๊ทœ ํ‘œํ˜„์‹ + - [x] ์ˆซ์ž๊ฐ€ 3๊ฐœ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ + - [x] ์ค‘๋ณต๋˜๋Š” ์ˆซ์ž๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ + +### ์ปดํ“จํ„ฐ์˜ ์ˆ˜ ์ƒ์„ฑ + +- [x] 3๊ฐœ์˜ ์ˆซ์ž ์ƒ์„ฑ (์ง€์ •๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด์šฉ) + - [x] ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ ์žฌ์ƒ์„ฑ + +### ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ˆ˜์™€ ์ปดํ“จํ„ฐ๊ฐ€ ์ƒ์„ฑํ•œ ์ˆ˜ ๋น„๊ต + +- [x] ๊ฐ ์ž๋ฆฌ ์ˆซ์ž ๋น„๊ตํ•˜๊ธฐ + - [x] ์ˆซ์ž๊ฐ€ ๋˜‘๊ฐ™๋‹ค๋ฉด ์ŠคํŠธ๋ผ์ดํฌ +1 + - [x] ๋‹ค๋ฅธ ์œ„์น˜์— ์žˆ๋‹ค๋ฉด ๋ณผ +1 + +### ๊ฒฐ๊ณผ + +- [x] ๋ณผ, ์ŠคํŠธ๋ผ์ดํฌ, ๋‚ซ์‹ฑ ์ถœ๋ ฅ +- [x] 3์ŠคํŠธ๋ผ์ดํฌ์ธ ๊ฒฝ์šฐ ๊ฒŒ์ž„ ์ข…๋ฃŒ + +### ์žฌ์‹œ์ž‘ ํ™•์ธ + +- [x] ์žฌ์‹œ์ž‘ ์ž…๋ ฅ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. + - [x] ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ + - [x] 1(์žฌ์‹œ์ž‘), 2(์ข…๋ฃŒ)๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ +- [x] 1์„ ์ž…๋ ฅ๋ฐ›์€ ๊ฒฝ์šฐ ์žฌ์‹œ์ž‘ +- [x] 2๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๊ฒฝ์šฐ ์ข…๋ฃŒ + +## ๋ชฉํ‘œ + +1. ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด์Šค๋Ÿฝ๊ฒŒ ์‚ฌ์šฉํ•˜์ž!!! \ No newline at end of file
Unknown
๋ฆฌ๋ทฐ ์‹œ์ž‘์— ์•ž์„œ ๋ฆฌ๋“œ๋ฏธ์— ๊ฐ ํด๋ž˜์Šค๋ณ„ ์—ญํ• ์— ๋Œ€ํ•ด ๊ธฐ์žฌํ•ด์ฃผ์‹œ๋ฉด ์ฝ”๋“œ ํŒŒ์•…์— ๋” ๋„์›€์ด ๋ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,58 @@ +package baseball.controller; + +import baseball.domain.Computer; +import baseball.domain.Referee; +import baseball.domain.Result; +import baseball.util.GameProgress; +import baseball.view.InputView; +import baseball.view.OutputView; +import java.util.List; + +public class GameController { + + private final Computer computer; + private final Referee referee; + private GameProgress gameProgress; + + public GameController() { + this.computer = new Computer(); + this.referee = new Referee(); + this.gameProgress = GameProgress.START; + } + + public void play() { + playOrEnd(); + } + + private void playOrEnd() { + OutputView.printStart(); + while (!GameProgress.isQuit(gameProgress)) { + List<Integer> computerNumbers = computer.generateRandomNumbers(); + playUntilThreeStrikes(computerNumbers); + selectRetry(); + } + } + + private void selectRetry() { + OutputView.printRetryOrEnd(); + gameProgress = GameProgress.judgeEnd(InputView.readNumber()); + } + + private void playUntilThreeStrikes(List<Integer> computerNumbers) { + while (!GameProgress.isQuit(gameProgress)) { + OutputView.printInputNumbers(); + Result result = referee.judge(InputView.readNumbers(), computerNumbers); + OutputView.printResult(result.announceResult()); + judgeRetry(result); + } + } + + private void judgeRetry(Result result) { + if (!result.isThreeStrikes()) { + gameProgress = GameProgress.RETRY; + return; + } + OutputView.printGameEnd(); + gameProgress = GameProgress.QUIT; + } +}
Java
ํ”Œ๋ ˆ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ๋‹จ์ˆœํžˆ playOrEnd๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ๋Š”๋ฐ play๋‚˜ playOrEnd์ค‘ ํ•˜๋‚˜๋ฅผ ์—†์• ๊ณ  ํ•ฉ์ณ๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,58 @@ +package baseball.controller; + +import baseball.domain.Computer; +import baseball.domain.Referee; +import baseball.domain.Result; +import baseball.util.GameProgress; +import baseball.view.InputView; +import baseball.view.OutputView; +import java.util.List; + +public class GameController { + + private final Computer computer; + private final Referee referee; + private GameProgress gameProgress; + + public GameController() { + this.computer = new Computer(); + this.referee = new Referee(); + this.gameProgress = GameProgress.START; + } + + public void play() { + playOrEnd(); + } + + private void playOrEnd() { + OutputView.printStart(); + while (!GameProgress.isQuit(gameProgress)) { + List<Integer> computerNumbers = computer.generateRandomNumbers(); + playUntilThreeStrikes(computerNumbers); + selectRetry(); + } + } + + private void selectRetry() { + OutputView.printRetryOrEnd(); + gameProgress = GameProgress.judgeEnd(InputView.readNumber()); + } + + private void playUntilThreeStrikes(List<Integer> computerNumbers) { + while (!GameProgress.isQuit(gameProgress)) { + OutputView.printInputNumbers(); + Result result = referee.judge(InputView.readNumbers(), computerNumbers); + OutputView.printResult(result.announceResult()); + judgeRetry(result); + } + } + + private void judgeRetry(Result result) { + if (!result.isThreeStrikes()) { + gameProgress = GameProgress.RETRY; + return; + } + OutputView.printGameEnd(); + gameProgress = GameProgress.QUIT; + } +}
Java
while์˜ ์กฐ๊ฑด๋ฌธ์„ ๊ธ์ •๋ฌธ์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋…์„ฑ์— ๋” ๋„์›€์ด ๋œ๋‹ค๊ณ  ํ•˜๋”๋ผ๊ตฌ์š”. ```suggestion while (GameProgress.isRetry(gameProgress)) { ``` ์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ”๋ณด์‹œ๋Š” ๊ฒƒ๋„ ํ•œ๋ฒˆ ์ œ์•ˆ๋“œ๋ ค๋ด…๋‹ˆ๋‹ค!
@@ -0,0 +1,23 @@ +package baseball.domain; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; +import java.util.List; + +public class Computer { + + private static final int NUMBERS_LENGTH = 3; + private static final int START_RANGE_NUMBER = 1; + private static final int END_RANGE_NUMBER = 9; + + public List<Integer> generateRandomNumbers() { + List<Integer> computer = new ArrayList<>(); + while (computer.size() < NUMBERS_LENGTH) { + int randomNumber = Randoms.pickNumberInRange(START_RANGE_NUMBER, END_RANGE_NUMBER); + if (!computer.contains(randomNumber)) { + computer.add(randomNumber); + } + } + return computer; + } +}
Java
์ •์  ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•˜์‹  ๊ธฐ์ค€์„ ์‚ดํŽด๋ณด๋ฉด Computer๋„ ์œ ํ‹ธ ๊ธฐ๋Šฅ + ์ƒํƒœ๊ฐ’์„ ๊ฐ€์ง€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ •์  ๋ฉ”์„œ๋“œ๋กœ ์„ ์–ธํ•ด๋„ ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ์–ด๋– ์‹ ๊ฐ€์š”?
@@ -0,0 +1,17 @@ +package baseball.domain; + +import java.util.List; + +public class Referee { + + public Result judge(List<Integer> userNumbers, List<Integer> computerNumbers) { + Result result = new Result(); + for (int i = 0; i < userNumbers.size(); i++) { + if (result.isStrike(userNumbers, computerNumbers, i)) { + continue; + } + result.countBall(userNumbers, computerNumbers, i); + } + return result; + } +}
Java
์ปดํ“จํ„ฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ ˆํผ๋ฆฌ๋„ ์ •์  ๋ฉ”์„œ๋“œ๋กœ ์„ ์–ธํ•ด๋„ ๊ดœ์ฐฎ์ง€ ์•Š์„๊นŒ ํ•˜๋Š” ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค!
@@ -0,0 +1,19 @@ +package baseball.util; + +public enum GameProgress { + + START, RETRY, QUIT; + + private static final int RETRY_NUMBER = 1; + + public static boolean isQuit(GameProgress gameProgress) { + return gameProgress == QUIT; + } + + public static GameProgress judgeEnd(int number) { + if (number == RETRY_NUMBER) { + return START; + } + return QUIT; + } +}
Java
ํ”„๋กœ๊ทธ๋žจ์˜ ํ™•์žฅ์„ฑ์„ ๊ณ ๋ คํ•˜์…”์„œ enum์œผ๋กœ ๋งŒ๋“œ์‹ ๊ฑฐ ๋งž์œผ์‹ค๊นŒ์š”? ์ €๋„ ์ฒ˜์Œ์— ์ข…๋ฃŒ ๋ฉ”๋‰ด๋ฅผ enum์œผ๋กœ ๋งŒ๋“œ๋ ค๊ณ  ํ•˜๋‹ค๊ฐ€ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ 2๊ฐ€์ง€์—ฌ์„œ class๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋” ๋งŽ์•˜๋‹ค๋ฉด ์ €๋„ enum์œผ๋กœ ๋งŒ๋“ค์—ˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”!
@@ -0,0 +1,19 @@ +package baseball.util; + +public enum GameProgress { + + START, RETRY, QUIT; + + private static final int RETRY_NUMBER = 1; + + public static boolean isQuit(GameProgress gameProgress) { + return gameProgress == QUIT; + } + + public static GameProgress judgeEnd(int number) { + if (number == RETRY_NUMBER) { + return START; + } + return QUIT; + } +}
Java
๋”ฐ๋กœ ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•˜์‹œ๊ธฐ ๋ณด๋‹ค RETRY(1), QUIT(2); ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ์‹œ๋Š”๊ฑด ์–ด๋– ์‹ ์ง€ ์ œ์•ˆ๋“œ๋ ค๋ด…๋‹ˆ๋‹ค!
@@ -0,0 +1,34 @@ +package baseball.view; + +import baseball.util.Validator; +import camp.nextstep.edu.missionutils.Console; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class InputView { + + public static final String DELIMITER = ""; + + public static List<Integer> readNumbers() { + String input = input(); + Validator.validateNumbersPattern(input); + + List<Integer> numbers = Stream.of(input.split(DELIMITER)) + .map(Integer::parseInt) + .distinct() + .collect(Collectors.toList()); + Validator.validateDuplication(numbers); + return numbers; + } + + public static int readNumber() { + String input = input(); + Validator.validateNumberPattern(input); + return Integer.parseInt(input); + } + + private static String input() { + return Console.readLine(); + } +}
Java
view๊ฐ€ inputd์„ domain์—์„œ ์‚ฌ์šฉ๋  ํ˜•ํƒœ๋กœ ๊ฐ€๊ณตํ•ด์„œ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒƒ์ด ๋งž๋Š” ๊ฑธ๊นŒ์š”??? ์ €๋„ ํ”„๋ฆฌ์ฝ”์Šค๋ฅผ ํ•˜๋ฉด์„œ ํ•ญ์ƒ ์˜๊ตฌ์‹ฌ ๊ฐ€์ ธ์™”๋˜ ๋ถ€๋ถ„์ด๋ผ์„œ ๋ ˆ์ด์‹ฑ์นด ๋‹ค์‹œ ๋งŒ๋“ค๊ธฐ์—์„œ๋Š” ์‹œ๋„ํšŸ์ˆ˜ ๋˜ํ•œ String ๊ฐ’์œผ๋กœ๋งŒ ๋„˜๊ฒจ์คฌ์—ˆ๋Š”๋ฐ์š”. ์ด ๋ถ€๋ถ„ ๋ชฉ์š”์ผ์— ๊ผญ ์–˜๊ธฐ ๋‚˜๋ˆ ๋ดค์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,65 @@ +package baseball.domain; + +import java.util.List; + +public class Result { + + private static final int DEFAULT_NUMBER = 0; + private static final int END_NUMBER = 3; + + private int ball; + private int strike; + + public Result() { + this.ball = DEFAULT_NUMBER; + this.strike = DEFAULT_NUMBER; + } + + public boolean isStrike(List<Integer> userNumbers, List<Integer> computerNumbers, int index) { + if (userNumbers.get(index).equals(computerNumbers.get(index))) { + strike++; + return true; + } + return false; + } + + public void countBall(List<Integer> userNumbers, List<Integer> computerNumbers, int index) { + if (computerNumbers.contains(userNumbers.get(index))) { + ball++; + } + } + + public boolean isThreeStrikes() { + return strike == END_NUMBER; + } + + public String announceResult() { + if (ball == DEFAULT_NUMBER && strike == DEFAULT_NUMBER) { + return Message.NOTHING.getMessage(); + } + if (ball == DEFAULT_NUMBER) { + return String.format(Message.STRIKE.getMessage(), strike); + } + if (strike == DEFAULT_NUMBER) { + return String.format(Message.BALL.getMessage(), ball); + } + return String.format(Message.BALL_AND_STRIKE.getMessage(), ball, strike); + } + + private enum Message { + NOTHING("๋‚ซ์‹ฑ"), + BALL("%d๋ณผ"), + STRIKE("%d์ŠคํŠธ๋ผ์ดํฌ"), + BALL_AND_STRIKE("%d๋ณผ %d์ŠคํŠธ๋ผ์ดํฌ"); + + private final String message; + + Message(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + } +}
Java
ํ•ด๋‹น ์ถœ๋ ฅ์— ๋Œ€ํ•œ ์ฑ…์ž„์„ OutputView์— ๋„˜๊ฒจ์ฃผ์‹œ๋Š”๊ฑด ์–ด๋– ์‹ ๊ฐ€์š”?
@@ -0,0 +1,32 @@ +package baseball.util; + +import java.util.List; +import java.util.regex.Pattern; + +public class Validator { + + private static final String DUPLICATION_MESSAGE = "์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."; + private static final String NUMBERS_PATTERN_MESSAGE = "1๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž ์ค‘ 3๊ฐ€์ง€ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."; + private static final String NUMBER_PATTERN_MESSAGE = "์žฌ์‹œ์ž‘์„ ์›ํ•˜๋ฉด 1, ์ข…๋ฃŒ๋ฅผ ์›ํ•˜๋ฉด 2๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."; + private static final Pattern REGEX_NUMBERS = Pattern.compile("^[1-9]{3}$"); + private static final Pattern REGEX_NUMBER = Pattern.compile("^[1-2]{1}$"); + private static final int NUMBERS_LENGTH = 3; + + public static void validateDuplication(List<Integer> input) { + if (input.size() < NUMBERS_LENGTH) { + throw new IllegalArgumentException(DUPLICATION_MESSAGE); + } + } + + public static void validateNumbersPattern(String input) { + if (!REGEX_NUMBERS.matcher(input).matches()) { + throw new IllegalArgumentException(NUMBERS_PATTERN_MESSAGE); + } + } + + public static void validateNumberPattern(String input) { + if (!REGEX_NUMBER.matcher(input).matches()) { + throw new IllegalArgumentException(NUMBER_PATTERN_MESSAGE); + } + } +}
Java
์ •๊ทœ์‹ ํ‘œํ˜„์ด ๋งค์šฐ ๊น”๋”ํ•˜๋„ค์š”! ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค.
@@ -0,0 +1,58 @@ +package baseball.controller; + +import baseball.domain.Computer; +import baseball.domain.Referee; +import baseball.domain.Result; +import baseball.util.GameProgress; +import baseball.view.InputView; +import baseball.view.OutputView; +import java.util.List; + +public class GameController { + + private final Computer computer; + private final Referee referee; + private GameProgress gameProgress; + + public GameController() { + this.computer = new Computer(); + this.referee = new Referee(); + this.gameProgress = GameProgress.START; + } + + public void play() { + playOrEnd(); + } + + private void playOrEnd() { + OutputView.printStart(); + while (!GameProgress.isQuit(gameProgress)) { + List<Integer> computerNumbers = computer.generateRandomNumbers(); + playUntilThreeStrikes(computerNumbers); + selectRetry(); + } + } + + private void selectRetry() { + OutputView.printRetryOrEnd(); + gameProgress = GameProgress.judgeEnd(InputView.readNumber()); + } + + private void playUntilThreeStrikes(List<Integer> computerNumbers) { + while (!GameProgress.isQuit(gameProgress)) { + OutputView.printInputNumbers(); + Result result = referee.judge(InputView.readNumbers(), computerNumbers); + OutputView.printResult(result.announceResult()); + judgeRetry(result); + } + } + + private void judgeRetry(Result result) { + if (!result.isThreeStrikes()) { + gameProgress = GameProgress.RETRY; + return; + } + OutputView.printGameEnd(); + gameProgress = GameProgress.QUIT; + } +}
Java
ํ•จ์ˆ˜๋ช…์„ ThreeStrike์ฒ˜๋Ÿผ ์šฐ์Šน ์กฐ๊ฑด์— ๋Œ€ํ•ด ์ง์ ‘์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ๋ณด๋‹ค playUntilWin ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ž‘์„ฑํ•˜๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”??
@@ -0,0 +1,65 @@ +package baseball.domain; + +import java.util.List; + +public class Result { + + private static final int DEFAULT_NUMBER = 0; + private static final int END_NUMBER = 3; + + private int ball; + private int strike; + + public Result() { + this.ball = DEFAULT_NUMBER; + this.strike = DEFAULT_NUMBER; + } + + public boolean isStrike(List<Integer> userNumbers, List<Integer> computerNumbers, int index) { + if (userNumbers.get(index).equals(computerNumbers.get(index))) { + strike++; + return true; + } + return false; + } + + public void countBall(List<Integer> userNumbers, List<Integer> computerNumbers, int index) { + if (computerNumbers.contains(userNumbers.get(index))) { + ball++; + } + } + + public boolean isThreeStrikes() { + return strike == END_NUMBER; + } + + public String announceResult() { + if (ball == DEFAULT_NUMBER && strike == DEFAULT_NUMBER) { + return Message.NOTHING.getMessage(); + } + if (ball == DEFAULT_NUMBER) { + return String.format(Message.STRIKE.getMessage(), strike); + } + if (strike == DEFAULT_NUMBER) { + return String.format(Message.BALL.getMessage(), ball); + } + return String.format(Message.BALL_AND_STRIKE.getMessage(), ball, strike); + } + + private enum Message { + NOTHING("๋‚ซ์‹ฑ"), + BALL("%d๋ณผ"), + STRIKE("%d์ŠคํŠธ๋ผ์ดํฌ"), + BALL_AND_STRIKE("%d๋ณผ %d์ŠคํŠธ๋ผ์ดํฌ"); + + private final String message; + + Message(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + } +}
Java
isStrike์™€ ํ†ต์ผํ•˜์—ฌ isBall๋กœ ์•ˆํ•œ ์ด์œ ๊ฐ€ ๋”ฐ๋กœ ์žˆ๋‚˜์š” !?
@@ -0,0 +1,65 @@ +package baseball.domain; + +import java.util.List; + +public class Result { + + private static final int DEFAULT_NUMBER = 0; + private static final int END_NUMBER = 3; + + private int ball; + private int strike; + + public Result() { + this.ball = DEFAULT_NUMBER; + this.strike = DEFAULT_NUMBER; + } + + public boolean isStrike(List<Integer> userNumbers, List<Integer> computerNumbers, int index) { + if (userNumbers.get(index).equals(computerNumbers.get(index))) { + strike++; + return true; + } + return false; + } + + public void countBall(List<Integer> userNumbers, List<Integer> computerNumbers, int index) { + if (computerNumbers.contains(userNumbers.get(index))) { + ball++; + } + } + + public boolean isThreeStrikes() { + return strike == END_NUMBER; + } + + public String announceResult() { + if (ball == DEFAULT_NUMBER && strike == DEFAULT_NUMBER) { + return Message.NOTHING.getMessage(); + } + if (ball == DEFAULT_NUMBER) { + return String.format(Message.STRIKE.getMessage(), strike); + } + if (strike == DEFAULT_NUMBER) { + return String.format(Message.BALL.getMessage(), ball); + } + return String.format(Message.BALL_AND_STRIKE.getMessage(), ball, strike); + } + + private enum Message { + NOTHING("๋‚ซ์‹ฑ"), + BALL("%d๋ณผ"), + STRIKE("%d์ŠคํŠธ๋ผ์ดํฌ"), + BALL_AND_STRIKE("%d๋ณผ %d์ŠคํŠธ๋ผ์ดํฌ"); + + private final String message; + + Message(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + } +}
Java
์˜ค .. ์ด๋ฐฉ์‹์œผ๋กœ ํ•˜๋Š”๊ฑฐ ์ข‹์€๊ฑฐ ๊ฐ™๋„ค์š” ๊ทผ๋ฐ ์•„๋ž˜ ์กฐ๊ฑด์—๋Š” else if ๋กœ ๋‚˜ํƒ€๋‚ด๋„ ์ข‹์„๊ฑฐ๊ฐ™์•„์š” !
@@ -0,0 +1,73 @@ +package com.eureka.spartaonetoone.review.application; + +import com.eureka.spartaonetoone.review.application.dtos.request.ReviewRequestDto; +import com.eureka.spartaonetoone.review.application.dtos.response.ReviewResponseDto; +import com.eureka.spartaonetoone.review.application.exception.ReviewException; +import com.eureka.spartaonetoone.review.domain.Review; +import com.eureka.spartaonetoone.review.domain.repository.ReviewRepository; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import org.springframework.validation.annotation.Validated; + +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +import jakarta.validation.Valid; + +@Service +@Validated +public class ReviewService { + + private final ReviewRepository reviewRepository; + + public ReviewService(ReviewRepository reviewRepository) { + this.reviewRepository = reviewRepository; + } + + // ๋ฆฌ๋ทฐ ๋“ฑ๋ก - DTO๋ฅผ ์—”ํ‹ฐํ‹ฐ๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ ์ €์žฅํ•˜๊ณ , ์ €์žฅ๋œ ์—”ํ‹ฐํ‹ฐ๋ฅผ DTO๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ + public ReviewResponseDto createReview(@Valid ReviewRequestDto dto) { + Review review = dto.createReview(); + Review savedReview = reviewRepository.save(review); + return ReviewResponseDto.from(savedReview); + } + + // ํŠน์ • ๋ฆฌ๋ทฐ ์กฐํšŒ - ์ฃผ์–ด์ง„ reviewId๋กœ Review ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•˜๋ฉฐ, ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด CustomException์„ ๋ฐœ์ƒ + public ReviewResponseDto getReviewById(UUID reviewId) { + Review review = reviewRepository.findById(reviewId) + .orElseThrow(ReviewException.ReviewNotFoundException::new); + return ReviewResponseDto.from(review); + } + + // ํŠน์ • ์ฃผ๋ฌธ(orderId)์— ์†ํ•œ ๋ฆฌ๋ทฐ ๋ชฉ๋ก ์กฐํšŒ + // ReviewRepository์˜ findByOrderId()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์ฃผ๋ฌธ์˜ ๋ชจ๋“  ๋ฆฌ๋ทฐ๋ฅผ ์กฐํšŒ + // ๋ฆฌ์ŠคํŠธ๋ฅผ Pageable๋กœ ๊ฐ์‹ธ์„œ ๋ฐ˜ํ™˜ + public Page<ReviewResponseDto> getReviewsByOrderId(UUID orderId, Pageable pageable) { + List<Review> reviews = reviewRepository.findByOrderId(orderId); + List<ReviewResponseDto> dtoList = reviews.stream() + .map(ReviewResponseDto::from) + .collect(Collectors.toList()); + return new PageImpl<>(dtoList, pageable, dtoList.size()); + } + + // ๋ฆฌ๋ทฐ ์ˆ˜์ • - ํŠน์ • reviewId์— ํ•ด๋‹นํ•˜๋Š” Review ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•œ ํ›„, DTO์˜ ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ์ €์žฅ + public ReviewResponseDto updateReview(UUID reviewId, @Valid ReviewRequestDto dto) { + Review review = reviewRepository.findById(reviewId) + .orElseThrow(ReviewException.ReviewNotFoundException::new); + review.update(dto.getContent(), dto.getRating(), dto.getImage()); + Review updatedReview = reviewRepository.save(review); + return ReviewResponseDto.from(updatedReview); + } + + + // ๋ฆฌ๋ทฐ ์‚ญ์ œ - ํŠน์ • reviewId๋กœ Review ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•œ ํ›„, markDeleted()๋ฅผ ํ˜ธ์ถœ + public void deleteReview(UUID reviewId) { + Review review = reviewRepository.findById(reviewId) + .orElseThrow(ReviewException.ReviewNotFoundException::new); + review.markDeleted(); + reviewRepository.save(review); + } +} \ No newline at end of file
Java
`@Transactional`์ด ๋น ์ง„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,73 @@ +package com.eureka.spartaonetoone.review.application; + +import com.eureka.spartaonetoone.review.application.dtos.request.ReviewRequestDto; +import com.eureka.spartaonetoone.review.application.dtos.response.ReviewResponseDto; +import com.eureka.spartaonetoone.review.application.exception.ReviewException; +import com.eureka.spartaonetoone.review.domain.Review; +import com.eureka.spartaonetoone.review.domain.repository.ReviewRepository; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import org.springframework.validation.annotation.Validated; + +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +import jakarta.validation.Valid; + +@Service +@Validated +public class ReviewService { + + private final ReviewRepository reviewRepository; + + public ReviewService(ReviewRepository reviewRepository) { + this.reviewRepository = reviewRepository; + } + + // ๋ฆฌ๋ทฐ ๋“ฑ๋ก - DTO๋ฅผ ์—”ํ‹ฐํ‹ฐ๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ ์ €์žฅํ•˜๊ณ , ์ €์žฅ๋œ ์—”ํ‹ฐํ‹ฐ๋ฅผ DTO๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ + public ReviewResponseDto createReview(@Valid ReviewRequestDto dto) { + Review review = dto.createReview(); + Review savedReview = reviewRepository.save(review); + return ReviewResponseDto.from(savedReview); + } + + // ํŠน์ • ๋ฆฌ๋ทฐ ์กฐํšŒ - ์ฃผ์–ด์ง„ reviewId๋กœ Review ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•˜๋ฉฐ, ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด CustomException์„ ๋ฐœ์ƒ + public ReviewResponseDto getReviewById(UUID reviewId) { + Review review = reviewRepository.findById(reviewId) + .orElseThrow(ReviewException.ReviewNotFoundException::new); + return ReviewResponseDto.from(review); + } + + // ํŠน์ • ์ฃผ๋ฌธ(orderId)์— ์†ํ•œ ๋ฆฌ๋ทฐ ๋ชฉ๋ก ์กฐํšŒ + // ReviewRepository์˜ findByOrderId()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์ฃผ๋ฌธ์˜ ๋ชจ๋“  ๋ฆฌ๋ทฐ๋ฅผ ์กฐํšŒ + // ๋ฆฌ์ŠคํŠธ๋ฅผ Pageable๋กœ ๊ฐ์‹ธ์„œ ๋ฐ˜ํ™˜ + public Page<ReviewResponseDto> getReviewsByOrderId(UUID orderId, Pageable pageable) { + List<Review> reviews = reviewRepository.findByOrderId(orderId); + List<ReviewResponseDto> dtoList = reviews.stream() + .map(ReviewResponseDto::from) + .collect(Collectors.toList()); + return new PageImpl<>(dtoList, pageable, dtoList.size()); + } + + // ๋ฆฌ๋ทฐ ์ˆ˜์ • - ํŠน์ • reviewId์— ํ•ด๋‹นํ•˜๋Š” Review ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•œ ํ›„, DTO์˜ ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ์ €์žฅ + public ReviewResponseDto updateReview(UUID reviewId, @Valid ReviewRequestDto dto) { + Review review = reviewRepository.findById(reviewId) + .orElseThrow(ReviewException.ReviewNotFoundException::new); + review.update(dto.getContent(), dto.getRating(), dto.getImage()); + Review updatedReview = reviewRepository.save(review); + return ReviewResponseDto.from(updatedReview); + } + + + // ๋ฆฌ๋ทฐ ์‚ญ์ œ - ํŠน์ • reviewId๋กœ Review ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•œ ํ›„, markDeleted()๋ฅผ ํ˜ธ์ถœ + public void deleteReview(UUID reviewId) { + Review review = reviewRepository.findById(reviewId) + .orElseThrow(ReviewException.ReviewNotFoundException::new); + review.markDeleted(); + reviewRepository.save(review); + } +} \ No newline at end of file
Java
์ด ๋ถ€๋ถ„์ด ์ •์ ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์•„๋งˆ `Review.createReview(dto)`๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,59 @@ +package com.eureka.spartaonetoone.review.presentation; + +import com.eureka.spartaonetoone.review.application.ReviewService; +import com.eureka.spartaonetoone.review.application.dtos.request.ReviewRequestDto; +import com.eureka.spartaonetoone.review.application.dtos.response.ReviewResponseDto; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.UUID; + +@RestController +@RequestMapping("/api/v1/reviews") +public class ReviewController { + + private final ReviewService reviewService; + + public ReviewController(ReviewService reviewService) { + this.reviewService = reviewService; + } + + // ๋ฆฌ๋ทฐ ์ƒ์„ฑ + @PostMapping + public ResponseEntity<ReviewResponseDto> createReview(@RequestBody ReviewRequestDto reviewRequestDto) { + ReviewResponseDto responseDto = reviewService.createReview(reviewRequestDto); + return ResponseEntity.ok(responseDto); + } + + // ํŠน์ • ๋ฆฌ๋ทฐ ์กฐํšŒ + @GetMapping("/{review_id}") + public ResponseEntity<ReviewResponseDto> getReview(@PathVariable("review_id") UUID reviewId) { + ReviewResponseDto responseDto = reviewService.getReviewById(reviewId); + return ResponseEntity.ok(responseDto); + } + + // ํŠน์ •์ฃผ๋ฌธ(orderId)์— ์†ํ•œ ๋ฆฌ๋ทฐ ์ „์ฒด ๋ชฉ๋ก ์กฐํšŒ + @GetMapping + public ResponseEntity<Page<ReviewResponseDto>> getReviewsByOrderId(@RequestParam("order_id") UUID orderId, Pageable pageable) { + Page<ReviewResponseDto> responseDtos = reviewService.getReviewsByOrderId(orderId, pageable); + return ResponseEntity.ok(responseDtos); + } + + // ๋ฆฌ๋ทฐ ์ˆ˜์ • + @PutMapping("/{review_id}") + public ResponseEntity<ReviewResponseDto> updateReview(@PathVariable("review_id") UUID reviewId, + @RequestBody ReviewRequestDto reviewRequestDto) { + ReviewResponseDto responseDto = reviewService.updateReview(reviewId, reviewRequestDto); + return ResponseEntity.ok(responseDto); + } + + // ๋ฆฌ๋ทฐ ์‚ญ์ œ + @DeleteMapping("/{review_id}") + public ResponseEntity<Void> deleteReview(@PathVariable("review_id") UUID reviewId) { + reviewService.deleteReview(reviewId); + return ResponseEntity.noContent().build(); + } +}
Java
dto ์•ž์— `@Valid` ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,59 @@ +package com.eureka.spartaonetoone.review.presentation; + +import com.eureka.spartaonetoone.review.application.ReviewService; +import com.eureka.spartaonetoone.review.application.dtos.request.ReviewRequestDto; +import com.eureka.spartaonetoone.review.application.dtos.response.ReviewResponseDto; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.UUID; + +@RestController +@RequestMapping("/api/v1/reviews") +public class ReviewController { + + private final ReviewService reviewService; + + public ReviewController(ReviewService reviewService) { + this.reviewService = reviewService; + } + + // ๋ฆฌ๋ทฐ ์ƒ์„ฑ + @PostMapping + public ResponseEntity<ReviewResponseDto> createReview(@RequestBody ReviewRequestDto reviewRequestDto) { + ReviewResponseDto responseDto = reviewService.createReview(reviewRequestDto); + return ResponseEntity.ok(responseDto); + } + + // ํŠน์ • ๋ฆฌ๋ทฐ ์กฐํšŒ + @GetMapping("/{review_id}") + public ResponseEntity<ReviewResponseDto> getReview(@PathVariable("review_id") UUID reviewId) { + ReviewResponseDto responseDto = reviewService.getReviewById(reviewId); + return ResponseEntity.ok(responseDto); + } + + // ํŠน์ •์ฃผ๋ฌธ(orderId)์— ์†ํ•œ ๋ฆฌ๋ทฐ ์ „์ฒด ๋ชฉ๋ก ์กฐํšŒ + @GetMapping + public ResponseEntity<Page<ReviewResponseDto>> getReviewsByOrderId(@RequestParam("order_id") UUID orderId, Pageable pageable) { + Page<ReviewResponseDto> responseDtos = reviewService.getReviewsByOrderId(orderId, pageable); + return ResponseEntity.ok(responseDtos); + } + + // ๋ฆฌ๋ทฐ ์ˆ˜์ • + @PutMapping("/{review_id}") + public ResponseEntity<ReviewResponseDto> updateReview(@PathVariable("review_id") UUID reviewId, + @RequestBody ReviewRequestDto reviewRequestDto) { + ReviewResponseDto responseDto = reviewService.updateReview(reviewId, reviewRequestDto); + return ResponseEntity.ok(responseDto); + } + + // ๋ฆฌ๋ทฐ ์‚ญ์ œ + @DeleteMapping("/{review_id}") + public ResponseEntity<Void> deleteReview(@PathVariable("review_id") UUID reviewId) { + reviewService.deleteReview(reviewId); + return ResponseEntity.noContent().build(); + } +}
Java
์—ฌ๊ธฐ๋„ dto ์•ž์— `@Valid` ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,62 @@ +package com.eureka.spartaonetoone.store.presentation; + +import com.eureka.spartaonetoone.store.application.StoreService; +import com.eureka.spartaonetoone.store.application.dtos.request.StoreRequestDto; // +import com.eureka.spartaonetoone.store.application.dtos.response.StoreResponseDto; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.UUID; + +@RestController +@RequestMapping("/api/v1/stores") +public class StoreController { + + private final StoreService storeService; + + // ์ƒ์„ฑ์ž ์ฃผ์ž… + public StoreController(StoreService storeService) { + this.storeService = storeService; + } + + // ๊ฐ€๊ฒŒ ๋“ฑ๋ก + @PostMapping + public ResponseEntity<StoreResponseDto> createStore(@RequestBody StoreRequestDto storeRequestDto) { // + StoreResponseDto responseDto = storeService.createStore(storeRequestDto); + return ResponseEntity.ok(responseDto); + } + + // ํŠน์ • ๊ฐ€๊ฒŒ ์กฐํšŒ + @GetMapping("/{store_id}") + public ResponseEntity<StoreResponseDto> getStore(@PathVariable(name = "store_id") UUID storeId) { + StoreResponseDto responseDto = storeService.getStoreById(storeId); + return ResponseEntity.ok(responseDto); + } // getStore(@PathVariable(name = "id") UUID id) { // ์ด๋ ‡๊ฒŒ ์ˆ˜์ • - ์Šคํ”„๋ง์ด ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋„๋ก / requesstํŒจ๋Ÿด? ์ฐพ์•„๋ณด๊ธฐ / ๋งํฌ ์ฐธ๊ณ  + // https://teamsparta.notion.site/RequestParam-PathVariable-Autowired-335787e51fac41418598434fa7f7ed51 + + + // ์ „์ฒด ๊ฐ€๊ฒŒ ๋ชฉ๋ก ์กฐํšŒ + @GetMapping + public ResponseEntity<Page<StoreResponseDto>> getAllStores(Pageable pageable) { + Page<StoreResponseDto> responseDtos = storeService.getAllStores(pageable); + return ResponseEntity.ok(responseDtos); + } + + // ๊ฐ€๊ฒŒ ์ˆ˜์ • + @PutMapping("/{store_id}") + public ResponseEntity<StoreResponseDto> updateStore(@PathVariable(name = "store_id") UUID storeId, + @RequestBody StoreRequestDto storeRequestDto) { + StoreResponseDto responseDto = storeService.updateStore(storeId, storeRequestDto); + return ResponseEntity.ok(responseDto); + } + + // ๊ฐ€๊ฒŒ ์‚ญ์ œ + @DeleteMapping("/{store_id}") + public ResponseEntity<Void> deleteStore(@PathVariable(name = "store_id") UUID storeId) { + storeService.deleteStore(storeId); + return ResponseEntity.noContent().build(); + } +}
Java
์ฃผ์„ ๋ถ€๋ถ„๋งŒ ๋‚˜์ค‘์— ์ œ๊ฑฐํ•ด์ฃผ์…”๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”
@@ -29,7 +29,7 @@ public void initialize() { beanFactory.initialize(); } - public Collection<Object> controllers() { + public Collection<Object> getControllers() { return beanFactory.beansAnnotatedWith(Controller.class); } }
Java
ํ”ผ๋“œ๋ฐฑ ๋ฐ˜์˜ ์ž˜ ํ•ด์ฃผ์…จ๋„ค์š” ๐Ÿ‘
@@ -1,6 +1,5 @@ package next.controller; -import core.annotation.Inject; import core.annotation.web.Controller; import core.annotation.web.RequestMapping; import core.annotation.web.RequestMethod; @@ -22,14 +21,8 @@ public class ApiQnaController extends AbstractNewController { private static final Logger logger = LoggerFactory.getLogger( ApiQnaController.class ); - private final QuestionDao questionDao; - private final AnswerDao answerDao; - - @Inject - public ApiQnaController(QuestionDao questionDao, AnswerDao answerDao) { - this.questionDao = questionDao; - this.answerDao = answerDao; - } + private final QuestionDao questionDao = QuestionDao.getInstance(); + private final AnswerDao answerDao = AnswerDao.getInstance(); @RequestMapping(value = "/api/qna/list", method = RequestMethod.GET) public ModelAndView questions(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Java
BeanFactory, getInstantiateSingletons NPE ๋ฐœ์ƒ ๋ฌธ์ œ๋กœ getInstance() ํ˜ธ์ถœ๋กœ ์ˆ˜์ •ํ•˜์‹ ๊ฑฐ ๋งž์„๊นŒ์š”? ์›์ธ์ด ๋ฌด์—‡์ด์—ˆ๋‚˜์š”? ์ด์ „ ์ฝ”๋“œ(์ƒ์„ฑ์ž inject) ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”. MyConfiguration ํด๋ž˜์Šค์—์„œ ๋“ฑ๋กํ•œ DataSource๋„ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ–ˆ์œผ๋‹ˆ JdbcTemplate์—์„œ ์‚ฌ์šฉํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๊ตฌ์š”!
@@ -0,0 +1,86 @@ +package nextstep.subway.application; + +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Optional; +import java.util.stream.Collectors; +import nextstep.subway.domain.Line; +import nextstep.subway.domain.LineRepository; +import nextstep.subway.domain.Station; +import nextstep.subway.domain.StationRepository; +import nextstep.subway.dto.LineCreateRequest; +import nextstep.subway.dto.LineResponse; +import nextstep.subway.dto.LineUpdateRequest; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(readOnly = true) +public class LineService { + private final LineRepository lineRepository; + + private final StationRepository stationRepository; + + public LineService(LineRepository lineRepository, StationRepository stationRepository) { + this.lineRepository = lineRepository; + this.stationRepository = stationRepository; + } + + @Transactional + public LineResponse saveLine(LineCreateRequest lineCreateRequest) { + Line persistLine = lineRepository.save(lineCreateRequest.toLine()); + addStations(persistLine, lineCreateRequest); + return LineResponse.of(persistLine); + } + + public LineResponse findLine(Long id) { + Optional<Line> line = lineRepository.findById(id); + return LineResponse.of(line.get()); + } + + public List<LineResponse> findAllLines() { + return lineRepository.findAll().stream() + .map(LineResponse::of) + .collect(Collectors.toList()); + } + + @Transactional + public LineResponse updateLine(Long id, LineUpdateRequest lineUpdateRequest) { + Line line = lineRepository.findById(id).get(); + line.updateLine(lineUpdateRequest); + return LineResponse.of(line); + } + + @Transactional + public void deleteLineById(Long id) { + Optional<Line> line = lineRepository.findById(id); + if (line.isPresent()) { + line.get().clearRelatedLines(); + lineRepository.delete(line.get()); + } + } + + private void addStations(Line persistLine, LineCreateRequest lineCreateRequest) { + addUpLine(persistLine, lineCreateRequest); + addDownLine(persistLine, lineCreateRequest); + } + + private void addUpLine(Line line, LineCreateRequest lineCreateRequest) { + if (lineCreateRequest.hasUpStationsId()) { + Station station = getStation(lineCreateRequest.getUpStationId()); + line.addStation(station); + } + } + + private void addDownLine(Line line, LineCreateRequest lineCreateRequest) { + if (lineCreateRequest.hasDownStationsId()) { + Station station = getStation(lineCreateRequest.getDownStationId()); + line.addStation(station); + } + } + + private Station getStation(Long stationId) { + return stationRepository.findById(stationId) + .orElseThrow(() -> new NoSuchElementException("id(" + stationId + ")์— ํ•ด๋‹นํ•˜๋Š” ์—ญ์ด ์—†์Šต๋‹ˆ๋‹ค.")); + } +}
Java
deleteById ๊ตฌํ˜„์ฒด๋ฅผ ๋ณด์‹œ๋ฉด ์กฐํšŒ ํ›„์— ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ์—๋งŒ ์‚ญ์ œํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ถ€๋ถ„๋“ค์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ง์ ‘ ์ž‘์„ฑํ•˜๊ธฐ๋ณด๋‹ค๋Š” Infrastructre layer์—์„œ ๋‹ด๋‹นํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”? ```java @Transactional @Override public void deleteById(ID id) { Assert.notNull(id, ID_MUST_NOT_BE_NULL); delete(findById(id).orElseThrow(() -> new EmptyResultDataAccessException( String.format("No %s entity with id %s exists!", entityInformation.getJavaType(), id), 1))); }
@@ -0,0 +1,86 @@ +package nextstep.subway.application; + +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Optional; +import java.util.stream.Collectors; +import nextstep.subway.domain.Line; +import nextstep.subway.domain.LineRepository; +import nextstep.subway.domain.Station; +import nextstep.subway.domain.StationRepository; +import nextstep.subway.dto.LineCreateRequest; +import nextstep.subway.dto.LineResponse; +import nextstep.subway.dto.LineUpdateRequest; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(readOnly = true) +public class LineService { + private final LineRepository lineRepository; + + private final StationRepository stationRepository; + + public LineService(LineRepository lineRepository, StationRepository stationRepository) { + this.lineRepository = lineRepository; + this.stationRepository = stationRepository; + } + + @Transactional + public LineResponse saveLine(LineCreateRequest lineCreateRequest) { + Line persistLine = lineRepository.save(lineCreateRequest.toLine()); + addStations(persistLine, lineCreateRequest); + return LineResponse.of(persistLine); + } + + public LineResponse findLine(Long id) { + Optional<Line> line = lineRepository.findById(id); + return LineResponse.of(line.get()); + } + + public List<LineResponse> findAllLines() { + return lineRepository.findAll().stream() + .map(LineResponse::of) + .collect(Collectors.toList()); + } + + @Transactional + public LineResponse updateLine(Long id, LineUpdateRequest lineUpdateRequest) { + Line line = lineRepository.findById(id).get(); + line.updateLine(lineUpdateRequest); + return LineResponse.of(line); + } + + @Transactional + public void deleteLineById(Long id) { + Optional<Line> line = lineRepository.findById(id); + if (line.isPresent()) { + line.get().clearRelatedLines(); + lineRepository.delete(line.get()); + } + } + + private void addStations(Line persistLine, LineCreateRequest lineCreateRequest) { + addUpLine(persistLine, lineCreateRequest); + addDownLine(persistLine, lineCreateRequest); + } + + private void addUpLine(Line line, LineCreateRequest lineCreateRequest) { + if (lineCreateRequest.hasUpStationsId()) { + Station station = getStation(lineCreateRequest.getUpStationId()); + line.addStation(station); + } + } + + private void addDownLine(Line line, LineCreateRequest lineCreateRequest) { + if (lineCreateRequest.hasDownStationsId()) { + Station station = getStation(lineCreateRequest.getDownStationId()); + line.addStation(station); + } + } + + private Station getStation(Long stationId) { + return stationRepository.findById(stationId) + .orElseThrow(() -> new NoSuchElementException("id(" + stationId + ")์— ํ•ด๋‹นํ•˜๋Š” ์—ญ์ด ์—†์Šต๋‹ˆ๋‹ค.")); + } +}
Java
๋ฉ”์†Œ๋“œ ๋ณ€์ˆ˜๋กœ ํ• ๋‹นํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ ๊ฐ€์š”? ``` java findAll().stream() .map(LineResponse::of) .collect(Collectors.toList()); ```
@@ -1,16 +1,35 @@ package nextstep.subway.domain; -import javax.persistence.*; +import java.util.Objects; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; @Entity public class Station extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Column(unique = true) private String name; - public Station() { + @ManyToOne + @JoinColumn(name = "line_id") + private Line line; + + private Integer distance; + + protected Station() { + } + + Station(Long id, String name) { + this.id = id; + this.name = name; } public Station(String name) { @@ -24,4 +43,61 @@ public Long getId() { public String getName() { return name; } + + public Integer getDistance() { + return distance; + } + + public Line getLine() { + return line; + } + + public void setLine(Line line) { + if (Objects.nonNull(this.line)) { + this.line.getStations().removeStation(this); + } + this.line = line; + if (!line.getStations().containsStation(this)) { + line.getStations().addStation(this); + } + } + + public void clearLine() { + this.line = null; + } + + public void clearRelatedStation() { + if (line != null) { + this.line.getStations().removeStation(this); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + Station station = (Station) o; + + return id.equals(station.id); + } + + @Override + public int hashCode() { + return id.hashCode(); + } + + @Override + public String toString() { + return "Station{" + + "id=" + id + + ", name='" + name + '\'' + + ", line=" + line + + ", distance=" + distance + + '}'; + } }
Java
์—ญ์ด Line์„ ๊ฐ€์ง€๋Š”๊ฒŒ ๋งž์„๊นŒ์š”? ํ•˜๋‚˜์˜ ์—ญ์ด ์—ฌ๋Ÿฌ ๋…ธ์„ ์— ์†ํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ•๋‚จ์—ญ์€ 2ํ˜ธ์„ ๊ณผ ์‹ ๋ถ„๋‹น์„  ๋‘๊ฐ€์ง€๊ฐ€ ์žˆ์–ด์š”.
@@ -0,0 +1,156 @@ +package nextstep.subway.acceptance; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.restassured.RestAssured; +import io.restassured.response.ExtractableResponse; +import io.restassured.response.Response; +import java.util.HashMap; +import java.util.List; +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.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.test.context.jdbc.Sql; + +@Sql("classpath:testdb/truncate.sql") +@DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ๊ด€๋ จ ๊ธฐ๋Šฅ") +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +class LineAcceptanceTest extends AcceptanceTest { + private final static String API_URL_LINES = "/lines"; + private final static String COLOR_RED = "bg-red-400"; + private final static String LINE_NUMBER_1 = "1ํ˜ธ์„ "; + private final static String LINE_NUMBER_2 = "2ํ˜ธ์„ "; + @LocalServerPort + int port; + + @BeforeEach + public void setUp() { + if (RestAssured.port == RestAssured.UNDEFINED_PORT) { + RestAssured.port = port; + } + } + + + /** + * When ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๋ฉด + * Then ์ง€ํ•˜์ฒ  ๋…ธ์„  ๋ชฉ๋ก ์กฐํšŒ ์‹œ ์ƒ์„ฑํ•œ ๋…ธ์„ ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค + */ + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ์ƒ์„ฑ") + @Test + void createLine() { + // when + registerLine(LINE_NUMBER_2); + + // then + List<String> lineNames = findLines().jsonPath().getList("name", String.class); + assertThat(lineNames).contains(LINE_NUMBER_2); + + } + + /** + * Given 2๊ฐœ์˜ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๊ณ  + * When ์ง€ํ•˜์ฒ  ๋…ธ์„  ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋ฉด + * Then ์ง€ํ•˜์ฒ  ๋…ธ์„  ๋ชฉ๋ก ์กฐํšŒ ์‹œ 2๊ฐœ์˜ ๋…ธ์„ ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. + */ + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ๋ชฉ๋ก ์กฐํšŒ") + @Test + void getLines() { + // given + registerLine(LINE_NUMBER_1); + registerLine(LINE_NUMBER_2); + + // when + List<String> lineNames = findLines().jsonPath().getList("name", String.class); + + // then + assertThat(lineNames).contains(LINE_NUMBER_1, LINE_NUMBER_2); + } + + /** + * Given ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๊ณ  + * When ์ƒ์„ฑํ•œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์กฐํšŒํ•˜๋ฉด + * Then ์ƒ์„ฑํ•œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์˜ ์ •๋ณด๋ฅผ ์‘๋‹ต๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. + */ + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ์กฐํšŒ") + @Test + void getLine() { + // given + String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id"); + + // when + ExtractableResponse<Response> response = findLine(lineId); + + // then + assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); + } + + /** + * Given ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๊ณ  + * When ์ƒ์„ฑํ•œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ˆ˜์ •ํ•˜๋ฉด + * Then ํ•ด๋‹น ์ง€ํ•˜์ฒ  ๋…ธ์„  ์ •๋ณด๋Š” ์ˆ˜์ •๋œ๋‹ค + */ + + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ์ˆ˜์ •") + @Test + void updateLine() { + // given + String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id"); + + // when + modifyLine(lineId, COLOR_RED); + + // then + String actual = findLine(lineId).jsonPath().getString("color"); + assertThat(actual).isEqualTo(COLOR_RED); + } + + /** + * Given ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๊ณ  + * When ์ƒ์„ฑํ•œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์‚ญ์ œํ•˜๋ฉด + * Then ํ•ด๋‹น ์ง€ํ•˜์ฒ  ๋…ธ์„  ์ •๋ณด๋Š” ์‚ญ์ œ๋œ๋‹ค + */ + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ์‚ญ์ œ") + @Test + void deleteLine() { + // given + String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id"); + + // when + removeLine(lineId); + List<String> lineNames = findLines().jsonPath().getList("name", String.class); + + // then + assertThat(lineNames.isEmpty() || !lineNames.contains(LINE_NUMBER_2)).isTrue(); + } + + private ExtractableResponse<Response> registerLine(String lineName) { + Map<String, Object> lineMap = new HashMap<>(); + lineMap.put("name", lineName); + lineMap.put("color", "red"); + lineMap.put("distance", 2); + return sendPost(lineMap, API_URL_LINES); + } + + private ExtractableResponse<Response> findLines() { + return sendGet(API_URL_LINES); + } + + private ExtractableResponse<Response> findLine(String lineId) { + return sendGet(API_URL_LINES + "/{id}", lineId); + } + + private ExtractableResponse<Response> modifyLine(String lineId, String color) { + Map<String, Object> lineMap = new HashMap<>(); + lineMap.put("name", "redred"); + lineMap.put("color", color); + return sendPut(lineMap, API_URL_LINES + "/{id}", lineId); + } + + private ExtractableResponse<Response> removeLine(String lineId) { + return sendDelete(API_URL_LINES + "/{id}", lineId); + } +}
Java
๋…ธ์„  ์ƒ์„ฑ ์‹œ ์ƒํ–‰์ข…์ ์—ญ๊ณผ ํ•˜ํ–‰์ข…์ ์—ญ์„ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฒˆ ๋‹จ๊ณ„์—์„œ๋Š” ์ง€ํ•˜์ฒ  ๋…ธ์„ ์— ์—ญ์„ ๋งตํ•‘ํ•˜๋Š” ๊ธฐ๋Šฅ์€ ์•„์ง ์—†์ง€๋งŒ ๋…ธ์„  ์กฐํšŒ์‹œ ํฌํ•จ๋œ ์—ญ ๋ชฉ๋ก์ด ํ•จ๊ป˜ ์‘๋‹ต๋ฉ๋‹ˆ๋‹ค. statusCode ์ฝ”๋“œ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‹ค์ œ ์‘๋‹ต์—๋Œ€ํ•ด์„œ ํ…Œ์ŠคํŠธ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,156 @@ +package nextstep.subway.acceptance; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.restassured.RestAssured; +import io.restassured.response.ExtractableResponse; +import io.restassured.response.Response; +import java.util.HashMap; +import java.util.List; +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.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.test.context.jdbc.Sql; + +@Sql("classpath:testdb/truncate.sql") +@DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ๊ด€๋ จ ๊ธฐ๋Šฅ") +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +class LineAcceptanceTest extends AcceptanceTest { + private final static String API_URL_LINES = "/lines"; + private final static String COLOR_RED = "bg-red-400"; + private final static String LINE_NUMBER_1 = "1ํ˜ธ์„ "; + private final static String LINE_NUMBER_2 = "2ํ˜ธ์„ "; + @LocalServerPort + int port; + + @BeforeEach + public void setUp() { + if (RestAssured.port == RestAssured.UNDEFINED_PORT) { + RestAssured.port = port; + } + } + + + /** + * When ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๋ฉด + * Then ์ง€ํ•˜์ฒ  ๋…ธ์„  ๋ชฉ๋ก ์กฐํšŒ ์‹œ ์ƒ์„ฑํ•œ ๋…ธ์„ ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค + */ + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ์ƒ์„ฑ") + @Test + void createLine() { + // when + registerLine(LINE_NUMBER_2); + + // then + List<String> lineNames = findLines().jsonPath().getList("name", String.class); + assertThat(lineNames).contains(LINE_NUMBER_2); + + } + + /** + * Given 2๊ฐœ์˜ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๊ณ  + * When ์ง€ํ•˜์ฒ  ๋…ธ์„  ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋ฉด + * Then ์ง€ํ•˜์ฒ  ๋…ธ์„  ๋ชฉ๋ก ์กฐํšŒ ์‹œ 2๊ฐœ์˜ ๋…ธ์„ ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. + */ + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ๋ชฉ๋ก ์กฐํšŒ") + @Test + void getLines() { + // given + registerLine(LINE_NUMBER_1); + registerLine(LINE_NUMBER_2); + + // when + List<String> lineNames = findLines().jsonPath().getList("name", String.class); + + // then + assertThat(lineNames).contains(LINE_NUMBER_1, LINE_NUMBER_2); + } + + /** + * Given ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๊ณ  + * When ์ƒ์„ฑํ•œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์กฐํšŒํ•˜๋ฉด + * Then ์ƒ์„ฑํ•œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์˜ ์ •๋ณด๋ฅผ ์‘๋‹ต๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. + */ + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ์กฐํšŒ") + @Test + void getLine() { + // given + String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id"); + + // when + ExtractableResponse<Response> response = findLine(lineId); + + // then + assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); + } + + /** + * Given ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๊ณ  + * When ์ƒ์„ฑํ•œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ˆ˜์ •ํ•˜๋ฉด + * Then ํ•ด๋‹น ์ง€ํ•˜์ฒ  ๋…ธ์„  ์ •๋ณด๋Š” ์ˆ˜์ •๋œ๋‹ค + */ + + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ์ˆ˜์ •") + @Test + void updateLine() { + // given + String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id"); + + // when + modifyLine(lineId, COLOR_RED); + + // then + String actual = findLine(lineId).jsonPath().getString("color"); + assertThat(actual).isEqualTo(COLOR_RED); + } + + /** + * Given ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๊ณ  + * When ์ƒ์„ฑํ•œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์‚ญ์ œํ•˜๋ฉด + * Then ํ•ด๋‹น ์ง€ํ•˜์ฒ  ๋…ธ์„  ์ •๋ณด๋Š” ์‚ญ์ œ๋œ๋‹ค + */ + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ์‚ญ์ œ") + @Test + void deleteLine() { + // given + String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id"); + + // when + removeLine(lineId); + List<String> lineNames = findLines().jsonPath().getList("name", String.class); + + // then + assertThat(lineNames.isEmpty() || !lineNames.contains(LINE_NUMBER_2)).isTrue(); + } + + private ExtractableResponse<Response> registerLine(String lineName) { + Map<String, Object> lineMap = new HashMap<>(); + lineMap.put("name", lineName); + lineMap.put("color", "red"); + lineMap.put("distance", 2); + return sendPost(lineMap, API_URL_LINES); + } + + private ExtractableResponse<Response> findLines() { + return sendGet(API_URL_LINES); + } + + private ExtractableResponse<Response> findLine(String lineId) { + return sendGet(API_URL_LINES + "/{id}", lineId); + } + + private ExtractableResponse<Response> modifyLine(String lineId, String color) { + Map<String, Object> lineMap = new HashMap<>(); + lineMap.put("name", "redred"); + lineMap.put("color", color); + return sendPut(lineMap, API_URL_LINES + "/{id}", lineId); + } + + private ExtractableResponse<Response> removeLine(String lineId) { + return sendDelete(API_URL_LINES + "/{id}", lineId); + } +}
Java
์„ฑํ•œ ํ”ฝ์Šค์ฒ˜ ๋ฐ ๋ฐ์ดํ„ฐ ์ง์ ‘ ์‚ญ์ œํ•ด์ฃผ์…จ์–ด์š”. ๋‹ค๋งŒ ์ƒ์„ฑํ•ด์•ผ ํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ๊ฑฐ๋‚˜, ์—ฐ๊ด€ ๊ด€๊ณ„ ๋งตํ•‘์ด ์žˆ์œผ๋ฉด ์–ด๋–ป๊ฒŒ ๋ ์ง€ ๊ณ ๋ฏผํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ๊ฐ™์•„์š”.
@@ -0,0 +1,156 @@ +package nextstep.subway.acceptance; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.restassured.RestAssured; +import io.restassured.response.ExtractableResponse; +import io.restassured.response.Response; +import java.util.HashMap; +import java.util.List; +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.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.test.context.jdbc.Sql; + +@Sql("classpath:testdb/truncate.sql") +@DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ๊ด€๋ จ ๊ธฐ๋Šฅ") +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +class LineAcceptanceTest extends AcceptanceTest { + private final static String API_URL_LINES = "/lines"; + private final static String COLOR_RED = "bg-red-400"; + private final static String LINE_NUMBER_1 = "1ํ˜ธ์„ "; + private final static String LINE_NUMBER_2 = "2ํ˜ธ์„ "; + @LocalServerPort + int port; + + @BeforeEach + public void setUp() { + if (RestAssured.port == RestAssured.UNDEFINED_PORT) { + RestAssured.port = port; + } + } + + + /** + * When ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๋ฉด + * Then ์ง€ํ•˜์ฒ  ๋…ธ์„  ๋ชฉ๋ก ์กฐํšŒ ์‹œ ์ƒ์„ฑํ•œ ๋…ธ์„ ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค + */ + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ์ƒ์„ฑ") + @Test + void createLine() { + // when + registerLine(LINE_NUMBER_2); + + // then + List<String> lineNames = findLines().jsonPath().getList("name", String.class); + assertThat(lineNames).contains(LINE_NUMBER_2); + + } + + /** + * Given 2๊ฐœ์˜ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๊ณ  + * When ์ง€ํ•˜์ฒ  ๋…ธ์„  ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋ฉด + * Then ์ง€ํ•˜์ฒ  ๋…ธ์„  ๋ชฉ๋ก ์กฐํšŒ ์‹œ 2๊ฐœ์˜ ๋…ธ์„ ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. + */ + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ๋ชฉ๋ก ์กฐํšŒ") + @Test + void getLines() { + // given + registerLine(LINE_NUMBER_1); + registerLine(LINE_NUMBER_2); + + // when + List<String> lineNames = findLines().jsonPath().getList("name", String.class); + + // then + assertThat(lineNames).contains(LINE_NUMBER_1, LINE_NUMBER_2); + } + + /** + * Given ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๊ณ  + * When ์ƒ์„ฑํ•œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์กฐํšŒํ•˜๋ฉด + * Then ์ƒ์„ฑํ•œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์˜ ์ •๋ณด๋ฅผ ์‘๋‹ต๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. + */ + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ์กฐํšŒ") + @Test + void getLine() { + // given + String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id"); + + // when + ExtractableResponse<Response> response = findLine(lineId); + + // then + assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); + } + + /** + * Given ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๊ณ  + * When ์ƒ์„ฑํ•œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ˆ˜์ •ํ•˜๋ฉด + * Then ํ•ด๋‹น ์ง€ํ•˜์ฒ  ๋…ธ์„  ์ •๋ณด๋Š” ์ˆ˜์ •๋œ๋‹ค + */ + + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ์ˆ˜์ •") + @Test + void updateLine() { + // given + String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id"); + + // when + modifyLine(lineId, COLOR_RED); + + // then + String actual = findLine(lineId).jsonPath().getString("color"); + assertThat(actual).isEqualTo(COLOR_RED); + } + + /** + * Given ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์ƒ์„ฑํ•˜๊ณ  + * When ์ƒ์„ฑํ•œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ์„ ์‚ญ์ œํ•˜๋ฉด + * Then ํ•ด๋‹น ์ง€ํ•˜์ฒ  ๋…ธ์„  ์ •๋ณด๋Š” ์‚ญ์ œ๋œ๋‹ค + */ + @DisplayName("์ง€ํ•˜์ฒ ๋…ธ์„  ์‚ญ์ œ") + @Test + void deleteLine() { + // given + String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id"); + + // when + removeLine(lineId); + List<String> lineNames = findLines().jsonPath().getList("name", String.class); + + // then + assertThat(lineNames.isEmpty() || !lineNames.contains(LINE_NUMBER_2)).isTrue(); + } + + private ExtractableResponse<Response> registerLine(String lineName) { + Map<String, Object> lineMap = new HashMap<>(); + lineMap.put("name", lineName); + lineMap.put("color", "red"); + lineMap.put("distance", 2); + return sendPost(lineMap, API_URL_LINES); + } + + private ExtractableResponse<Response> findLines() { + return sendGet(API_URL_LINES); + } + + private ExtractableResponse<Response> findLine(String lineId) { + return sendGet(API_URL_LINES + "/{id}", lineId); + } + + private ExtractableResponse<Response> modifyLine(String lineId, String color) { + Map<String, Object> lineMap = new HashMap<>(); + lineMap.put("name", "redred"); + lineMap.put("color", color); + return sendPut(lineMap, API_URL_LINES + "/{id}", lineId); + } + + private ExtractableResponse<Response> removeLine(String lineId) { + return sendDelete(API_URL_LINES + "/{id}", lineId); + } +}
Java
๋…ธ์„  ์ƒ์„ฑ ์‹œ ์ƒํ–‰์ข…์ ์—ญ๊ณผ ํ•˜ํ–‰์ข…์ ์—ญ์„ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์š”๊ตฌ์‚ฌํ•ญ์ด ๊ตฌํ˜„๋˜์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™์•„์š”.