code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,174 @@ +import { christmasInstance } from "./InputView.js"; +import menuAndQuantity from "./utils/menuAndQuantity.js"; + +const MENUS_PRICE = { + ์–‘์†ก์ด์ˆ˜ํ”„: 6000, + ํƒ€ํŒŒ์Šค: 5500, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: 8000, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: 55000, + ๋ฐ”๋น„ํ๋ฆฝ: 54000, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: 35000, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: 25000, + ์ดˆ์ฝ”์ผ€์ดํฌ: 15000, + ์•„์ด์Šคํฌ๋ฆผ: 5000, + ์ œ๋กœ์ฝœ๋ผ: 3000, + ๋ ˆ๋“œ์™€์ธ: 60000, + ์ƒดํŽ˜์ธ: 25000, +}; +const WEEKDAY = [ + 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, +]; +const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30]; +const STAR_DAY = [3, 10, 17, 24, 25, 31]; + +export function toTalPriceLogic() { + const MENUS = christmasInstance.getMenus(); + let totalPrice = 0; + + MENUS.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (MENUS_PRICE.hasOwnProperty(MENU_NAME)) + totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY; + }); + + return totalPrice; +} + +export function ChampagnePromotionAvailable() { + const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus()); + if (TOTAL_PRICE >= 120000) { + return true; + } else return false; +} + +// d-day ํ• ์ธ +export function receivedD_dayPromotion() { + const DATE = christmasInstance.getDate(); + let minusPrice = 1000; + let available = true; + + if (DATE >= 26) { + available = false; + } + + if (available) { + for (let i = 1; i < DATE; i++) { + minusPrice += 100; + } + } else { + minusPrice = 0; + } + + return minusPrice; +} + +function minusPriceCalculator( + menus, + date, + validDays, + targetMenuCategories, + discountMultiplier +) { + let minusPrice = 0; + + if (!validDays.includes(date)) { + return minusPrice; + } + + menus.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (targetMenuCategories.includes(MENU_NAME)) + minusPrice += discountMultiplier * QUANTITY; + }); + + return minusPrice; +} + +// WEEKDAY ํ• ์ธ +export function receivedWeekDayPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const DESSERT = [`์ดˆ์ฝ”์ผ€์ดํฌ`, `์•„์ด์Šคํฌ๋ฆผ`]; + + return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023); +} + +// WEEKEND ํ• ์ธ +export function receivedWeekendPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const MAIN = ["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", "๋ฐ”๋น„ํ๋ฆฝ", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€"]; + + return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023); +} + +export function receivedSpecialPromotion() { + const DATE = christmasInstance.getDate(); + const MINUS_PRICE = 1000; + if (STAR_DAY.includes(DATE)) { + return MINUS_PRICE; + } else return 0; +} + +// ์ƒดํŽ˜์ธ ์ฆ์ • ์ด๋ฒคํŠธ +export function receivedChampagnePromotion() { + const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable(); + const CHAMPAGNE_PRICE = 25000; + + if (CHAMPAGNE_AVAILABLE) { + return CHAMPAGNE_PRICE; + } else return 0; +} + +export function receivedTotalBenefitPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + let resultTotalBenefitPrice; + + const totalBenefitPrice = + DDAY_AVAILABLE + + WEEKDAY_AVAILABLE + + WEEKEND_AVAILABLE + + CHAMPAGNE_AVAILABLE + + SPECIAL_AVAILABLE; + + if (totalBenefitPrice === 0) return 0; + if (totalBenefitPrice > 0) return -totalBenefitPrice; +} + +export function receivedTotalDsicountPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + + const TOTAL_DISCOUNTPRICE = + DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE; + + return TOTAL_DISCOUNTPRICE; +} + +export function totalPriceAfterDiscount() { + const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice(); + const TOTAL_PRICE = toTalPriceLogic(); + + const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE; + + return TOTAL_AFTER; +} + +export function sendBadge() { + const TOTAL_AFTER = -receivedTotalBenefitPrice(); + if (TOTAL_AFTER >= 20000) { + return "์‚ฐํƒ€"; + } else if (TOTAL_AFTER >= 10000) { + return "ํŠธ๋ฆฌ"; + } else if (TOTAL_AFTER >= 5000) { + return "๋ณ„"; + } else { + return "์—†์Œ"; + } +}
JavaScript
reduce๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ตณ์ด ์กฐ๊ฑด๋ฌธ์„ ๊ฑธ์ง€ ์•Š์•„๋„ Menu ๊ฐ€๊ฒฉ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. hasOwnProperty ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ž์ฒด์ ์œผ๋กœ ํ•ด๋‹น๊ฐ’์ด ์—†์„ ์‹œ 0์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์„ค์ •ํ•œ๋‹ค๋ฉด ์•„๋ž˜ reduce ๋ฉ”์„œ๋“œ์—์„œ ์‚ผํ•ญ์—ฐ์‚ฐ์ž ๋ถ€๋ถ„๋„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š” ```js MENUS.reduce((acc, menu) => { const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); return acc + (MENUS_PRICE.hasOwnProperty(MENU_NAME) ? MENUS_PRICE[MENU_NAME] * QUANTITY : 0); }, 0); ```
@@ -0,0 +1,174 @@ +import { christmasInstance } from "./InputView.js"; +import menuAndQuantity from "./utils/menuAndQuantity.js"; + +const MENUS_PRICE = { + ์–‘์†ก์ด์ˆ˜ํ”„: 6000, + ํƒ€ํŒŒ์Šค: 5500, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: 8000, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: 55000, + ๋ฐ”๋น„ํ๋ฆฝ: 54000, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: 35000, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: 25000, + ์ดˆ์ฝ”์ผ€์ดํฌ: 15000, + ์•„์ด์Šคํฌ๋ฆผ: 5000, + ์ œ๋กœ์ฝœ๋ผ: 3000, + ๋ ˆ๋“œ์™€์ธ: 60000, + ์ƒดํŽ˜์ธ: 25000, +}; +const WEEKDAY = [ + 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, +]; +const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30]; +const STAR_DAY = [3, 10, 17, 24, 25, 31]; + +export function toTalPriceLogic() { + const MENUS = christmasInstance.getMenus(); + let totalPrice = 0; + + MENUS.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (MENUS_PRICE.hasOwnProperty(MENU_NAME)) + totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY; + }); + + return totalPrice; +} + +export function ChampagnePromotionAvailable() { + const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus()); + if (TOTAL_PRICE >= 120000) { + return true; + } else return false; +} + +// d-day ํ• ์ธ +export function receivedD_dayPromotion() { + const DATE = christmasInstance.getDate(); + let minusPrice = 1000; + let available = true; + + if (DATE >= 26) { + available = false; + } + + if (available) { + for (let i = 1; i < DATE; i++) { + minusPrice += 100; + } + } else { + minusPrice = 0; + } + + return minusPrice; +} + +function minusPriceCalculator( + menus, + date, + validDays, + targetMenuCategories, + discountMultiplier +) { + let minusPrice = 0; + + if (!validDays.includes(date)) { + return minusPrice; + } + + menus.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (targetMenuCategories.includes(MENU_NAME)) + minusPrice += discountMultiplier * QUANTITY; + }); + + return minusPrice; +} + +// WEEKDAY ํ• ์ธ +export function receivedWeekDayPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const DESSERT = [`์ดˆ์ฝ”์ผ€์ดํฌ`, `์•„์ด์Šคํฌ๋ฆผ`]; + + return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023); +} + +// WEEKEND ํ• ์ธ +export function receivedWeekendPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const MAIN = ["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", "๋ฐ”๋น„ํ๋ฆฝ", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€"]; + + return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023); +} + +export function receivedSpecialPromotion() { + const DATE = christmasInstance.getDate(); + const MINUS_PRICE = 1000; + if (STAR_DAY.includes(DATE)) { + return MINUS_PRICE; + } else return 0; +} + +// ์ƒดํŽ˜์ธ ์ฆ์ • ์ด๋ฒคํŠธ +export function receivedChampagnePromotion() { + const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable(); + const CHAMPAGNE_PRICE = 25000; + + if (CHAMPAGNE_AVAILABLE) { + return CHAMPAGNE_PRICE; + } else return 0; +} + +export function receivedTotalBenefitPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + let resultTotalBenefitPrice; + + const totalBenefitPrice = + DDAY_AVAILABLE + + WEEKDAY_AVAILABLE + + WEEKEND_AVAILABLE + + CHAMPAGNE_AVAILABLE + + SPECIAL_AVAILABLE; + + if (totalBenefitPrice === 0) return 0; + if (totalBenefitPrice > 0) return -totalBenefitPrice; +} + +export function receivedTotalDsicountPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + + const TOTAL_DISCOUNTPRICE = + DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE; + + return TOTAL_DISCOUNTPRICE; +} + +export function totalPriceAfterDiscount() { + const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice(); + const TOTAL_PRICE = toTalPriceLogic(); + + const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE; + + return TOTAL_AFTER; +} + +export function sendBadge() { + const TOTAL_AFTER = -receivedTotalBenefitPrice(); + if (TOTAL_AFTER >= 20000) { + return "์‚ฐํƒ€"; + } else if (TOTAL_AFTER >= 10000) { + return "ํŠธ๋ฆฌ"; + } else if (TOTAL_AFTER >= 5000) { + return "๋ณ„"; + } else { + return "์—†์Œ"; + } +}
JavaScript
์ž…๋ ฅํ•œ ๋‚ ์งœ๋งŒํผ 100์›์”ฉ ์ถ”๊ฐ€๋กœ ๋งˆ์ด๋„ˆ์Šค ๊ฐ€๊ฒฉ์„ ํ•ฉ์‚ฐํ•˜์—ฌ ์ตœ์ข… ํ•ฉ์‚ฐ ํ• ์ธ๊ธˆ์•ก์„ ๊ณ„์‚ฐ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. for๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ์ž…๋ ฅ ๋‚ ์งœ๋ฅผ ์ด์šฉํ•ด์„œ ์ˆ˜์‹์œผ๋กœ ํ•ด๋‹น ์ผ์ž์˜ ์ ์šฉํ• ์ธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณ ๋ฏผํ•ด๋ณด์‹œ๊ณ  ์ˆ˜์‹์„ ํ™œ์šฉํ•ด ๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฑฐ ๊ฐ™์•„์š” (๊ฐ„๋‹จํžˆ ์ˆ˜์‹๋“ค์€ ์•Œ๊ณ  ์žˆ์œผ๋ฉด ์ƒ๊ฐ๋ณด๋‹ค ์ฝ”๋“œ์ž‘์„ฑ์„ ์ˆ˜์›”ํ•˜๊ฒŒ ๋„์™€์ค๋‹ˆ๋‹ค) ๊ทธ๋ฆฌ๊ณ  ๊ฐ€๊ธ‰์  for๋ฌธ์„ ์•ˆ์“ฐ๊ณ  ํ•ด๊ฒฐํ•ด๋ณด๋ ค๊ณ  ํ•ด๋ณด์„ธ์š”. ์ €๋Š” ์ด ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค๋‹ค๋ณด๋‹ˆ ์ƒˆ๋กœ์šด ๋ฐฉ๋ฒ•๋“ค์„ ์ž๊พธ ์ฐพ๊ฒŒ๋˜๊ณ  ๊ทธ ์†์—์„œ ๋ฐฐ์šฐ๋Š” ๊ฒƒ๋“ค์ด ์ •๋ง ๋งŽ๋”๋ผ๊ณ ์š”
@@ -0,0 +1,174 @@ +import { christmasInstance } from "./InputView.js"; +import menuAndQuantity from "./utils/menuAndQuantity.js"; + +const MENUS_PRICE = { + ์–‘์†ก์ด์ˆ˜ํ”„: 6000, + ํƒ€ํŒŒ์Šค: 5500, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: 8000, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: 55000, + ๋ฐ”๋น„ํ๋ฆฝ: 54000, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: 35000, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: 25000, + ์ดˆ์ฝ”์ผ€์ดํฌ: 15000, + ์•„์ด์Šคํฌ๋ฆผ: 5000, + ์ œ๋กœ์ฝœ๋ผ: 3000, + ๋ ˆ๋“œ์™€์ธ: 60000, + ์ƒดํŽ˜์ธ: 25000, +}; +const WEEKDAY = [ + 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, +]; +const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30]; +const STAR_DAY = [3, 10, 17, 24, 25, 31]; + +export function toTalPriceLogic() { + const MENUS = christmasInstance.getMenus(); + let totalPrice = 0; + + MENUS.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (MENUS_PRICE.hasOwnProperty(MENU_NAME)) + totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY; + }); + + return totalPrice; +} + +export function ChampagnePromotionAvailable() { + const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus()); + if (TOTAL_PRICE >= 120000) { + return true; + } else return false; +} + +// d-day ํ• ์ธ +export function receivedD_dayPromotion() { + const DATE = christmasInstance.getDate(); + let minusPrice = 1000; + let available = true; + + if (DATE >= 26) { + available = false; + } + + if (available) { + for (let i = 1; i < DATE; i++) { + minusPrice += 100; + } + } else { + minusPrice = 0; + } + + return minusPrice; +} + +function minusPriceCalculator( + menus, + date, + validDays, + targetMenuCategories, + discountMultiplier +) { + let minusPrice = 0; + + if (!validDays.includes(date)) { + return minusPrice; + } + + menus.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (targetMenuCategories.includes(MENU_NAME)) + minusPrice += discountMultiplier * QUANTITY; + }); + + return minusPrice; +} + +// WEEKDAY ํ• ์ธ +export function receivedWeekDayPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const DESSERT = [`์ดˆ์ฝ”์ผ€์ดํฌ`, `์•„์ด์Šคํฌ๋ฆผ`]; + + return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023); +} + +// WEEKEND ํ• ์ธ +export function receivedWeekendPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const MAIN = ["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", "๋ฐ”๋น„ํ๋ฆฝ", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€"]; + + return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023); +} + +export function receivedSpecialPromotion() { + const DATE = christmasInstance.getDate(); + const MINUS_PRICE = 1000; + if (STAR_DAY.includes(DATE)) { + return MINUS_PRICE; + } else return 0; +} + +// ์ƒดํŽ˜์ธ ์ฆ์ • ์ด๋ฒคํŠธ +export function receivedChampagnePromotion() { + const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable(); + const CHAMPAGNE_PRICE = 25000; + + if (CHAMPAGNE_AVAILABLE) { + return CHAMPAGNE_PRICE; + } else return 0; +} + +export function receivedTotalBenefitPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + let resultTotalBenefitPrice; + + const totalBenefitPrice = + DDAY_AVAILABLE + + WEEKDAY_AVAILABLE + + WEEKEND_AVAILABLE + + CHAMPAGNE_AVAILABLE + + SPECIAL_AVAILABLE; + + if (totalBenefitPrice === 0) return 0; + if (totalBenefitPrice > 0) return -totalBenefitPrice; +} + +export function receivedTotalDsicountPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + + const TOTAL_DISCOUNTPRICE = + DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE; + + return TOTAL_DISCOUNTPRICE; +} + +export function totalPriceAfterDiscount() { + const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice(); + const TOTAL_PRICE = toTalPriceLogic(); + + const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE; + + return TOTAL_AFTER; +} + +export function sendBadge() { + const TOTAL_AFTER = -receivedTotalBenefitPrice(); + if (TOTAL_AFTER >= 20000) { + return "์‚ฐํƒ€"; + } else if (TOTAL_AFTER >= 10000) { + return "ํŠธ๋ฆฌ"; + } else if (TOTAL_AFTER >= 5000) { + return "๋ณ„"; + } else { + return "์—†์Œ"; + } +}
JavaScript
์ƒํ™ฉ๋งˆ๋‹ค ๋‹ค๋ฅด์ง€๋งŒ ์•ž์— ์Œ์ˆ˜๋ฅผ ๋ถ™์—ฌ์„œ ์–‘์ˆ˜๋กœ ๋งŒ๋“ค ๋•Œ ์ง๊ด€์ ์ด๊ณ  ๋ช…ํ™•ํžˆ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ํŽธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  else if์€ ๋‚˜์—ด๋˜๋Š” ์กฐ๊ฑด๋ฌธ์ด ๊ธธ์–ด์งˆ ์ˆ˜๋ก ๊ฐ€๋…์„ฑ์„ ํ•ด์น˜๊ธฐ์— ์ง€์–‘ํ•˜๋Š” ํŽธ์ด ์ข‹๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์ฒ˜๋Ÿผ ์ˆ˜์ •ํ•ด๋ณด์‹œ๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”? ```js const TOTAL_AFTER = Math.abs(receivedTotalBenefitPrice()) if (TOTAL_AFTER >= 20000) return "์‚ฐํƒ€"; if (TOTAL_AFTER >= 10000) return "ํŠธ๋ฆฌ"; if (TOTAL_AFTER >= 5000) return "๋ณ„"; return "์—†์Œ"; ```
@@ -0,0 +1,86 @@ +class ChristmasPromotion { + #date; + #menus; + + constructor(date, menus) { + this.#date = date; + this.#menus = menus; + } + + #dateValidate(date) { + if (!(date >= 1 && date <= 31)) { + throw new Error("[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + } + + startDateValidate(date) { + this.#dateValidate(date); + this.#date = date; + } + + #menusValidate(menus) { + const SEENMENUS = new Set(); + + for (const menu of menus) { + if (!/^(.+)-(.+)$/.test(menu)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + const MENU_LIST = [ + "์–‘์†ก์ด์ˆ˜ํ”„", + "ํƒ€ํŒŒ์Šค", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", + "๋ฐ”๋น„ํ๋ฆฝ", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", + "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", + "์ดˆ์ฝ”์ผ€์ดํฌ", + "์•„์ด์Šคํฌ๋ฆผ", + "์ œ๋กœ์ฝœ๋ผ", + "๋ ˆ๋“œ์™€์ธ", + "์ƒดํŽ˜์ธ", + ]; + + const [MENU_PART, QUANTITY] = menu.split("-"); + const QUANTITY_PART = parseInt(QUANTITY, 10); + + if (!MENU_LIST.includes(MENU_PART)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + // ์ค‘๋ณต ๋””ํ…Œ์ผํ•˜๊ฒŒ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ•„์š” + if (SEENMENUS.has(menu)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + SEENMENUS.add(menu); + } + } + + startMenuValidate(menus) { + this.#menusValidate(menus); + this.#menus = menus; + } + + getDate() { + return this.#date; + } + + getMenus() { + return this.#menus; + } +} + +export default ChristmasPromotion;
JavaScript
for..of๋Š” javascript airbnb์—์„œ ์ถ”์ฒœํ•˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค ! forEach()๋‚˜ map()์„ ์จ๋ณด์‹œ๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”? ํ•ด๋‹น ํ•จ์ˆ˜๋Š” for..of์™€ if๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š”๊ฑฐ๋‹ˆ `filter()`๊ฐ€ ์ ํ•ฉํ•ด๋ณด์ž…๋‹ˆ๋‹ค ! ์ฐธ๊ณ  : [Javascript Airbnb 11.1](https://github.com/tipjs/javascript-style-guide#11.1)
@@ -0,0 +1,86 @@ +class ChristmasPromotion { + #date; + #menus; + + constructor(date, menus) { + this.#date = date; + this.#menus = menus; + } + + #dateValidate(date) { + if (!(date >= 1 && date <= 31)) { + throw new Error("[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + } + + startDateValidate(date) { + this.#dateValidate(date); + this.#date = date; + } + + #menusValidate(menus) { + const SEENMENUS = new Set(); + + for (const menu of menus) { + if (!/^(.+)-(.+)$/.test(menu)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + const MENU_LIST = [ + "์–‘์†ก์ด์ˆ˜ํ”„", + "ํƒ€ํŒŒ์Šค", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", + "๋ฐ”๋น„ํ๋ฆฝ", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", + "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", + "์ดˆ์ฝ”์ผ€์ดํฌ", + "์•„์ด์Šคํฌ๋ฆผ", + "์ œ๋กœ์ฝœ๋ผ", + "๋ ˆ๋“œ์™€์ธ", + "์ƒดํŽ˜์ธ", + ]; + + const [MENU_PART, QUANTITY] = menu.split("-"); + const QUANTITY_PART = parseInt(QUANTITY, 10); + + if (!MENU_LIST.includes(MENU_PART)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + // ์ค‘๋ณต ๋””ํ…Œ์ผํ•˜๊ฒŒ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ•„์š” + if (SEENMENUS.has(menu)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + SEENMENUS.add(menu); + } + } + + startMenuValidate(menus) { + this.#menusValidate(menus); + this.#menus = menus; + } + + getDate() { + return this.#date; + } + + getMenus() { + return this.#menus; + } +} + +export default ChristmasPromotion;
JavaScript
has๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋””ํ…Œ์ผํ•˜๊ฒŒ ์ค‘๋ณต ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ตฐ์š”...! ์ €๋Š” ๋ฉ”๋‰ด๋ฅผ ๋ถ„๋ฆฌํ•ด์„œ setํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค !!
@@ -0,0 +1,174 @@ +import { christmasInstance } from "./InputView.js"; +import menuAndQuantity from "./utils/menuAndQuantity.js"; + +const MENUS_PRICE = { + ์–‘์†ก์ด์ˆ˜ํ”„: 6000, + ํƒ€ํŒŒ์Šค: 5500, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: 8000, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: 55000, + ๋ฐ”๋น„ํ๋ฆฝ: 54000, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: 35000, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: 25000, + ์ดˆ์ฝ”์ผ€์ดํฌ: 15000, + ์•„์ด์Šคํฌ๋ฆผ: 5000, + ์ œ๋กœ์ฝœ๋ผ: 3000, + ๋ ˆ๋“œ์™€์ธ: 60000, + ์ƒดํŽ˜์ธ: 25000, +}; +const WEEKDAY = [ + 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, +]; +const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30]; +const STAR_DAY = [3, 10, 17, 24, 25, 31]; + +export function toTalPriceLogic() { + const MENUS = christmasInstance.getMenus(); + let totalPrice = 0; + + MENUS.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (MENUS_PRICE.hasOwnProperty(MENU_NAME)) + totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY; + }); + + return totalPrice; +} + +export function ChampagnePromotionAvailable() { + const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus()); + if (TOTAL_PRICE >= 120000) { + return true; + } else return false; +} + +// d-day ํ• ์ธ +export function receivedD_dayPromotion() { + const DATE = christmasInstance.getDate(); + let minusPrice = 1000; + let available = true; + + if (DATE >= 26) { + available = false; + } + + if (available) { + for (let i = 1; i < DATE; i++) { + minusPrice += 100; + } + } else { + minusPrice = 0; + } + + return minusPrice; +} + +function minusPriceCalculator( + menus, + date, + validDays, + targetMenuCategories, + discountMultiplier +) { + let minusPrice = 0; + + if (!validDays.includes(date)) { + return minusPrice; + } + + menus.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (targetMenuCategories.includes(MENU_NAME)) + minusPrice += discountMultiplier * QUANTITY; + }); + + return minusPrice; +} + +// WEEKDAY ํ• ์ธ +export function receivedWeekDayPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const DESSERT = [`์ดˆ์ฝ”์ผ€์ดํฌ`, `์•„์ด์Šคํฌ๋ฆผ`]; + + return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023); +} + +// WEEKEND ํ• ์ธ +export function receivedWeekendPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const MAIN = ["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", "๋ฐ”๋น„ํ๋ฆฝ", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€"]; + + return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023); +} + +export function receivedSpecialPromotion() { + const DATE = christmasInstance.getDate(); + const MINUS_PRICE = 1000; + if (STAR_DAY.includes(DATE)) { + return MINUS_PRICE; + } else return 0; +} + +// ์ƒดํŽ˜์ธ ์ฆ์ • ์ด๋ฒคํŠธ +export function receivedChampagnePromotion() { + const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable(); + const CHAMPAGNE_PRICE = 25000; + + if (CHAMPAGNE_AVAILABLE) { + return CHAMPAGNE_PRICE; + } else return 0; +} + +export function receivedTotalBenefitPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + let resultTotalBenefitPrice; + + const totalBenefitPrice = + DDAY_AVAILABLE + + WEEKDAY_AVAILABLE + + WEEKEND_AVAILABLE + + CHAMPAGNE_AVAILABLE + + SPECIAL_AVAILABLE; + + if (totalBenefitPrice === 0) return 0; + if (totalBenefitPrice > 0) return -totalBenefitPrice; +} + +export function receivedTotalDsicountPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + + const TOTAL_DISCOUNTPRICE = + DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE; + + return TOTAL_DISCOUNTPRICE; +} + +export function totalPriceAfterDiscount() { + const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice(); + const TOTAL_PRICE = toTalPriceLogic(); + + const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE; + + return TOTAL_AFTER; +} + +export function sendBadge() { + const TOTAL_AFTER = -receivedTotalBenefitPrice(); + if (TOTAL_AFTER >= 20000) { + return "์‚ฐํƒ€"; + } else if (TOTAL_AFTER >= 10000) { + return "ํŠธ๋ฆฌ"; + } else if (TOTAL_AFTER >= 5000) { + return "๋ณ„"; + } else { + return "์—†์Œ"; + } +}
JavaScript
์•„๋‹ˆ๋ฉด ๊ฐ์ฒด๋กœ ์„ ์–ธํ•˜์‹  ๋‹ค์Œ์— find๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๊ธˆ์•ก ์กฐ๊ฑด์ด 3๊ฐœ๋‹ˆ๊นŒ ๊ดœ์ฐฎ์•„ ๋ณด์ž…๋‹ˆ๋‹ค !
@@ -1,7 +1,27 @@ -export default InputView = { - async readDate() { - const input = await Console.readLineAsync("12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"); - // ... - } - // ... -} +import { MissionUtils } from "@woowacourse/mission-utils"; +import ChristmasDomain from "./ChristmasPromotion.js"; +import Order from "./Order.js"; + +export const christmasInstance = new ChristmasDomain(); + +const InputView = { + async readDate() { + const input = await MissionUtils.Console.readLineAsync( + "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n" + ); + const DATE = parseInt(input, 10); + christmasInstance.startDateValidate(DATE); + }, + + async readOrderMenu() { + const input = await MissionUtils.Console.readLineAsync( + "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n" + ); + new Order(input); + const MENUES = input.split(","); + christmasInstance.startMenuValidate(MENUES); + }, + // ... +}; + +export default InputView;
JavaScript
ํ•ด๋‹น ๋ฌธ๊ตฌ๋Š” ๋ณ€ํ•˜์ง€ ์•Š๋Š” ์ƒ์ˆ˜๊ฐ’์ด๋‹ˆ ๋ณ€ํ•˜์ง€ ์•Š์€ ์ƒ์ˆ˜๊ฐ’๋“ค์€ ๋”ฐ๋กœ constant๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•œ ๋ฒˆ์— ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค !
@@ -1,7 +1,27 @@ -export default InputView = { - async readDate() { - const input = await Console.readLineAsync("12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"); - // ... - } - // ... -} +import { MissionUtils } from "@woowacourse/mission-utils"; +import ChristmasDomain from "./ChristmasPromotion.js"; +import Order from "./Order.js"; + +export const christmasInstance = new ChristmasDomain(); + +const InputView = { + async readDate() { + const input = await MissionUtils.Console.readLineAsync( + "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n" + ); + const DATE = parseInt(input, 10); + christmasInstance.startDateValidate(DATE); + }, + + async readOrderMenu() { + const input = await MissionUtils.Console.readLineAsync( + "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n" + ); + new Order(input); + const MENUES = input.split(","); + christmasInstance.startMenuValidate(MENUES); + }, + // ... +}; + +export default InputView;
JavaScript
ํ•ด๋‹น ๋ฏธ์…˜์€ ์šฐํ…Œ์ฝ” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ Random์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋‹ˆ `Console`๋กœ๋งŒ ์„ ์–ธํ•˜์‹œ๋ฉด ์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ ๋” ๊น”๋”ํ•ด ์งˆ ๊ฒƒ ๊ฐ™์•„์š” !
@@ -0,0 +1,24 @@ +import menuAndQuantity from "./utils/menuAndQuantity.js"; + +class Order { + #order; + + constructor(order) { + this.#order = Array.isArray(order) ? order : [order]; + this.#orderQuantityValidate(); + } + + #orderQuantityValidate() { + this.#order.forEach((menu) => { + const { QUANTITY } = menuAndQuantity(menu); + + if (QUANTITY > 20) { + throw new Error( + `[ERROR] ๋ฉ”๋‰ด๋Š” ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 20๊ฐœ๊นŒ์ง€๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.` + ); + } + }); + } +} + +export default Order;
JavaScript
forEach()๋กœ ์„ ์–ธํ•˜์‹  ์ด์œ ๋ฅผ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ์š”??
@@ -1,7 +1,110 @@ -export default OutputView = { - printMenu() { - Console.print("<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"); - // ... +import { MissionUtils } from "@woowacourse/mission-utils"; +import { christmasInstance } from "./InputView.js"; +import { + ChampagnePromotionAvailable, + receivedChampagnePromotion, + receivedD_dayPromotion, + receivedSpecialPromotion, + receivedTotalBenefitPrice, + receivedWeekDayPromotion, + receivedWeekendPromotion, + sendBadge, + toTalPriceLogic, + totalPriceAfterDiscount, +} from "./DomainLogic.js"; +import menuAndQuantity from "./utils/menuAndQuantity.js"; + +const OutputView = { + printIntroduction() { + MissionUtils.Console.print( + "์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค." + ); + }, + + printBenefitIntroduction() { + MissionUtils.Console.print( + "12์›” 3์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n" + ); + }, + + printMenu() { + MissionUtils.Console.print("<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"); + const ORDERED_MENUS = christmasInstance.getMenus(); + ORDERED_MENUS.map(function (eachMenu) { + const { MENU_NAME, QUANTITY } = menuAndQuantity(eachMenu); + MissionUtils.Console.print(`${MENU_NAME} ${QUANTITY}๊ฐœ`); + }); + }, + + printTotalPrice() { + MissionUtils.Console.print("\n<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>"); + const TOTAL_PRICE = toTalPriceLogic().toLocaleString(); + MissionUtils.Console.print(`${TOTAL_PRICE}์›`); + }, + + printChampagnePromotion() { + MissionUtils.Console.print("\n<์ฆ์ • ๋ฉ”๋‰ด>"); + const CHAMPAGNEPROMOTION_AVAILABLE = ChampagnePromotionAvailable(); + if (CHAMPAGNEPROMOTION_AVAILABLE === true) { + MissionUtils.Console.print("์ƒดํŽ˜์ธ 1๊ฐœ"); + } else MissionUtils.Console.print("์—†์Œ"); + }, + + printReceivedPromotion() { + MissionUtils.Console.print("\n<ํ˜œํƒ ๋‚ด์—ญ>"); + const DDAY_AVAILABLE = receivedD_dayPromotion().toLocaleString(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion().toLocaleString(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion().toLocaleString(); + const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion().toLocaleString(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion().toLocaleString(); + + let anyPromotionApplied = false; + + if (DDAY_AVAILABLE !== "0") { + MissionUtils.Console.print( + `ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ: -${DDAY_AVAILABLE}์›` + ); + anyPromotionApplied = true; } - // ... -} + + if (WEEKDAY_AVAILABLE !== "0") { + MissionUtils.Console.print(`ํ‰์ผ ํ• ์ธ: -${WEEKDAY_AVAILABLE}์›`); + anyPromotionApplied = true; + } + if (WEEKEND_AVAILABLE !== "0") { + MissionUtils.Console.print(`์ฃผ๋ง ํ• ์ธ: -${WEEKEND_AVAILABLE}์›`); + anyPromotionApplied = true; + } + if (SPECIAL_AVAILABLE !== "0") { + MissionUtils.Console.print(`ํŠน๋ณ„ ํ• ์ธ: -${SPECIAL_AVAILABLE}์›`); + anyPromotionApplied = true; + } + if (CHAMPAGNE_AVAILABLE !== "0") { + MissionUtils.Console.print(`์ฆ์ • ์ด๋ฒคํŠธ: -${CHAMPAGNE_AVAILABLE}์›`); + anyPromotionApplied = true; + } + if (!anyPromotionApplied) { + MissionUtils.Console.print("์—†์Œ"); + } + }, + + printReceivedTotalBenefitPrice() { + MissionUtils.Console.print("\n<์ดํ˜œํƒ ๊ธˆ์•ก>"); + const TOTAL_BENEFITPRICE = receivedTotalBenefitPrice().toLocaleString(); + MissionUtils.Console.print(`${TOTAL_BENEFITPRICE}์›`); + }, + + printTotalPriceAfterDiscount() { + MissionUtils.Console.print("\n<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>"); + const TOTAL_AFTER = totalPriceAfterDiscount().toLocaleString(); + MissionUtils.Console.print(`${TOTAL_AFTER}์›`); + }, + + printEventBadge() { + MissionUtils.Console.print("\n<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>"); + const GET_BADGE = sendBadge(); + MissionUtils.Console.print(`${GET_BADGE}`); + }, +}; + +export default OutputView;
JavaScript
ํ•ด๋‹น ํ˜œํƒ ๋‚ด์—ญ์„ ๋ฐฐ์—ด๋กœ ์ž…๋ ฅ๋ฐ›์•„์„œ forEach()ํ•จ์ˆ˜๋ฅผ ์จ์„œ ์ถœ๋ ฅํ•˜๋ฉด ์กฐ๊ธˆ ๋” ๊น”๋”ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ! 0์›์ด๊ฑฐ๋‚˜ ํ•ด๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด undefined๋กœ ์ž…๋ ฅ๋ฐ›๊ณ  filter()๋กœ ์ œ๊ฑฐํ•˜๋ฉด ์ฝ”๋“œ ๊ธธ์ด๊ฐ€ ํ™• ์ค„์–ด๋“ค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค !
@@ -1,5 +1,29 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; +import InputView from "./InputView.js"; +import OutputView from "./OutputView.js"; + class App { - async run() {} + async run() { + await christmasPromotionProcess(); + } +} + +async function christmasPromotionProcess() { + try { + OutputView.printIntroduction(); + await InputView.readDate(); + await InputView.readOrderMenu(); + OutputView.printBenefitIntroduction(); + OutputView.printMenu(); + OutputView.printTotalPrice(); + OutputView.printChampagnePromotion(); + OutputView.printReceivedPromotion(); + OutputView.printReceivedTotalBenefitPrice(); + OutputView.printTotalPriceAfterDiscount(); + OutputView.printEventBadge(); + } catch (error) { + MissionUtils.Console.print(error.message); + } } export default App;
JavaScript
ํ˜ธ์ถœ ๋ฉ”์„œ๋“œ๊ฐ€ ๋งŽ์•„ ๊ธธ์–ด์ง„ ๊ฑธ ์ •๋ฆฌํ•˜์ง€ ์•Š์•˜์—ˆ๋Š”๋ฐ printResult () ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ์‹ ์ข‹์•„์š”!
@@ -0,0 +1,86 @@ +class ChristmasPromotion { + #date; + #menus; + + constructor(date, menus) { + this.#date = date; + this.#menus = menus; + } + + #dateValidate(date) { + if (!(date >= 1 && date <= 31)) { + throw new Error("[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + } + + startDateValidate(date) { + this.#dateValidate(date); + this.#date = date; + } + + #menusValidate(menus) { + const SEENMENUS = new Set(); + + for (const menu of menus) { + if (!/^(.+)-(.+)$/.test(menu)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + const MENU_LIST = [ + "์–‘์†ก์ด์ˆ˜ํ”„", + "ํƒ€ํŒŒ์Šค", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", + "๋ฐ”๋น„ํ๋ฆฝ", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", + "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", + "์ดˆ์ฝ”์ผ€์ดํฌ", + "์•„์ด์Šคํฌ๋ฆผ", + "์ œ๋กœ์ฝœ๋ผ", + "๋ ˆ๋“œ์™€์ธ", + "์ƒดํŽ˜์ธ", + ]; + + const [MENU_PART, QUANTITY] = menu.split("-"); + const QUANTITY_PART = parseInt(QUANTITY, 10); + + if (!MENU_LIST.includes(MENU_PART)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + // ์ค‘๋ณต ๋””ํ…Œ์ผํ•˜๊ฒŒ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ•„์š” + if (SEENMENUS.has(menu)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + SEENMENUS.add(menu); + } + } + + startMenuValidate(menus) { + this.#menusValidate(menus); + this.#menus = menus; + } + + getDate() { + return this.#date; + } + + getMenus() { + return this.#menus; + } +} + +export default ChristmasPromotion;
JavaScript
์ง€๊ธˆ๊นŒ์ง€ ์ƒ์ˆ˜๋ผ๋Š” ๊ฐœ๋…์€ ์•Œ์•˜์ง€๋งŒ ์ƒ์ˆ˜๋ฅผ ์™œ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๊ณ ๋ฏผ์ด ์—†์—ˆ๋˜ ๊ฒƒ ๊ฐ™์•„์š”. ๐Ÿฅฒ๐Ÿ™Œ
@@ -0,0 +1,86 @@ +class ChristmasPromotion { + #date; + #menus; + + constructor(date, menus) { + this.#date = date; + this.#menus = menus; + } + + #dateValidate(date) { + if (!(date >= 1 && date <= 31)) { + throw new Error("[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + } + + startDateValidate(date) { + this.#dateValidate(date); + this.#date = date; + } + + #menusValidate(menus) { + const SEENMENUS = new Set(); + + for (const menu of menus) { + if (!/^(.+)-(.+)$/.test(menu)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + const MENU_LIST = [ + "์–‘์†ก์ด์ˆ˜ํ”„", + "ํƒ€ํŒŒ์Šค", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", + "๋ฐ”๋น„ํ๋ฆฝ", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", + "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", + "์ดˆ์ฝ”์ผ€์ดํฌ", + "์•„์ด์Šคํฌ๋ฆผ", + "์ œ๋กœ์ฝœ๋ผ", + "๋ ˆ๋“œ์™€์ธ", + "์ƒดํŽ˜์ธ", + ]; + + const [MENU_PART, QUANTITY] = menu.split("-"); + const QUANTITY_PART = parseInt(QUANTITY, 10); + + if (!MENU_LIST.includes(MENU_PART)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + // ์ค‘๋ณต ๋””ํ…Œ์ผํ•˜๊ฒŒ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ•„์š” + if (SEENMENUS.has(menu)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + SEENMENUS.add(menu); + } + } + + startMenuValidate(menus) { + this.#menusValidate(menus); + this.#menus = menus; + } + + getDate() { + return this.#date; + } + + getMenus() { + return this.#menus; + } +} + +export default ChristmasPromotion;
JavaScript
๋ฌด์ž‘์ • ํ•ด๊ฒฐํ•ด๋ณด๊ณ  ๋ณธ๋‹ค ์Šคํƒ€์ผ์ด์—ˆ๋Š”๋ฐ, ์ •๋ง ํ•ด๊ฒฐํ•˜๊ณ  ๋์ด์—ˆ๋„ค์š”. ์ž‘์„ฑํ•  ๋•Œ๋ถ€ํ„ฐ ์ƒํ™ฉ์— ๋งž๋Š” ํšจ์œจ์ ์ธ ๋ฉ”์„œ๋“œ๋ฅผ ํƒํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ์ง€๋งŒ, ํ•˜๊ณ  ๋‚˜์„œ ํ™•์ธํ–ˆ์„ ๋•Œ '์—ฌ๊ธฐ์— ๋งž๋Š” ๋” ์ข‹์€ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‚˜?' ๊ณ ๋ฏผํ•ด ๋ด์•ผ๊ฒ ์–ด์š”!
@@ -0,0 +1,86 @@ +class ChristmasPromotion { + #date; + #menus; + + constructor(date, menus) { + this.#date = date; + this.#menus = menus; + } + + #dateValidate(date) { + if (!(date >= 1 && date <= 31)) { + throw new Error("[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + } + + startDateValidate(date) { + this.#dateValidate(date); + this.#date = date; + } + + #menusValidate(menus) { + const SEENMENUS = new Set(); + + for (const menu of menus) { + if (!/^(.+)-(.+)$/.test(menu)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + const MENU_LIST = [ + "์–‘์†ก์ด์ˆ˜ํ”„", + "ํƒ€ํŒŒ์Šค", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", + "๋ฐ”๋น„ํ๋ฆฝ", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", + "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", + "์ดˆ์ฝ”์ผ€์ดํฌ", + "์•„์ด์Šคํฌ๋ฆผ", + "์ œ๋กœ์ฝœ๋ผ", + "๋ ˆ๋“œ์™€์ธ", + "์ƒดํŽ˜์ธ", + ]; + + const [MENU_PART, QUANTITY] = menu.split("-"); + const QUANTITY_PART = parseInt(QUANTITY, 10); + + if (!MENU_LIST.includes(MENU_PART)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + // ์ค‘๋ณต ๋””ํ…Œ์ผํ•˜๊ฒŒ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ•„์š” + if (SEENMENUS.has(menu)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + SEENMENUS.add(menu); + } + } + + startMenuValidate(menus) { + this.#menusValidate(menus); + this.#menus = menus; + } + + getDate() { + return this.#date; + } + + getMenus() { + return this.#menus; + } +} + +export default ChristmasPromotion;
JavaScript
๋งค์ง๋„˜๋ฒ„๋ฅผ ์ œ๊ฐ€ ์‚ฌ์šฉํ–ˆ๋˜ ๊ฑฐ๊ตฐ์š”! ์˜๋ฏธ์žˆ๊ฒŒ ์ฝ”๋“œ ๊ธธ์ด๊ฐ€ ๊ธธ์–ด์ง„ ๊ฒƒ ๊ฐ™์•„์š”. ๋˜, ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ์‚ฌ์šฉ(๊ฐ์ฒด...!)์„ ์ ๊ทน์ ์œผ๋กœ ์•ž์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ๊ฒ ์–ด์š”.
@@ -0,0 +1,174 @@ +import { christmasInstance } from "./InputView.js"; +import menuAndQuantity from "./utils/menuAndQuantity.js"; + +const MENUS_PRICE = { + ์–‘์†ก์ด์ˆ˜ํ”„: 6000, + ํƒ€ํŒŒ์Šค: 5500, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: 8000, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: 55000, + ๋ฐ”๋น„ํ๋ฆฝ: 54000, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: 35000, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: 25000, + ์ดˆ์ฝ”์ผ€์ดํฌ: 15000, + ์•„์ด์Šคํฌ๋ฆผ: 5000, + ์ œ๋กœ์ฝœ๋ผ: 3000, + ๋ ˆ๋“œ์™€์ธ: 60000, + ์ƒดํŽ˜์ธ: 25000, +}; +const WEEKDAY = [ + 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, +]; +const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30]; +const STAR_DAY = [3, 10, 17, 24, 25, 31]; + +export function toTalPriceLogic() { + const MENUS = christmasInstance.getMenus(); + let totalPrice = 0; + + MENUS.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (MENUS_PRICE.hasOwnProperty(MENU_NAME)) + totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY; + }); + + return totalPrice; +} + +export function ChampagnePromotionAvailable() { + const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus()); + if (TOTAL_PRICE >= 120000) { + return true; + } else return false; +} + +// d-day ํ• ์ธ +export function receivedD_dayPromotion() { + const DATE = christmasInstance.getDate(); + let minusPrice = 1000; + let available = true; + + if (DATE >= 26) { + available = false; + } + + if (available) { + for (let i = 1; i < DATE; i++) { + minusPrice += 100; + } + } else { + minusPrice = 0; + } + + return minusPrice; +} + +function minusPriceCalculator( + menus, + date, + validDays, + targetMenuCategories, + discountMultiplier +) { + let minusPrice = 0; + + if (!validDays.includes(date)) { + return minusPrice; + } + + menus.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (targetMenuCategories.includes(MENU_NAME)) + minusPrice += discountMultiplier * QUANTITY; + }); + + return minusPrice; +} + +// WEEKDAY ํ• ์ธ +export function receivedWeekDayPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const DESSERT = [`์ดˆ์ฝ”์ผ€์ดํฌ`, `์•„์ด์Šคํฌ๋ฆผ`]; + + return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023); +} + +// WEEKEND ํ• ์ธ +export function receivedWeekendPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const MAIN = ["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", "๋ฐ”๋น„ํ๋ฆฝ", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€"]; + + return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023); +} + +export function receivedSpecialPromotion() { + const DATE = christmasInstance.getDate(); + const MINUS_PRICE = 1000; + if (STAR_DAY.includes(DATE)) { + return MINUS_PRICE; + } else return 0; +} + +// ์ƒดํŽ˜์ธ ์ฆ์ • ์ด๋ฒคํŠธ +export function receivedChampagnePromotion() { + const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable(); + const CHAMPAGNE_PRICE = 25000; + + if (CHAMPAGNE_AVAILABLE) { + return CHAMPAGNE_PRICE; + } else return 0; +} + +export function receivedTotalBenefitPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + let resultTotalBenefitPrice; + + const totalBenefitPrice = + DDAY_AVAILABLE + + WEEKDAY_AVAILABLE + + WEEKEND_AVAILABLE + + CHAMPAGNE_AVAILABLE + + SPECIAL_AVAILABLE; + + if (totalBenefitPrice === 0) return 0; + if (totalBenefitPrice > 0) return -totalBenefitPrice; +} + +export function receivedTotalDsicountPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + + const TOTAL_DISCOUNTPRICE = + DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE; + + return TOTAL_DISCOUNTPRICE; +} + +export function totalPriceAfterDiscount() { + const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice(); + const TOTAL_PRICE = toTalPriceLogic(); + + const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE; + + return TOTAL_AFTER; +} + +export function sendBadge() { + const TOTAL_AFTER = -receivedTotalBenefitPrice(); + if (TOTAL_AFTER >= 20000) { + return "์‚ฐํƒ€"; + } else if (TOTAL_AFTER >= 10000) { + return "ํŠธ๋ฆฌ"; + } else if (TOTAL_AFTER >= 5000) { + return "๋ณ„"; + } else { + return "์—†์Œ"; + } +}
JavaScript
์ œ๊ฐ€ ํ•˜๋“œ์ฝ”๋”ฉ์„ ์ œ๋Œ€๋กœ ํ–ˆ๋„ค์š”. ```js let weekday = true; const date = new Date(`2023-12-${์ €์žฅ๋œ ๊ฐ’}`); if (date.getDay() === 5 || date.getDay() === 6) { weekday = false; } else { weekday = true; } ``` ์ด๋Ÿฐ ์‹์œผ๋กœ ์ƒ๊ฐ์„ ๋ฐ”๊ฟ”๋ณผ๊ฒŒ์š”!
@@ -0,0 +1,174 @@ +import { christmasInstance } from "./InputView.js"; +import menuAndQuantity from "./utils/menuAndQuantity.js"; + +const MENUS_PRICE = { + ์–‘์†ก์ด์ˆ˜ํ”„: 6000, + ํƒ€ํŒŒ์Šค: 5500, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: 8000, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: 55000, + ๋ฐ”๋น„ํ๋ฆฝ: 54000, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: 35000, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: 25000, + ์ดˆ์ฝ”์ผ€์ดํฌ: 15000, + ์•„์ด์Šคํฌ๋ฆผ: 5000, + ์ œ๋กœ์ฝœ๋ผ: 3000, + ๋ ˆ๋“œ์™€์ธ: 60000, + ์ƒดํŽ˜์ธ: 25000, +}; +const WEEKDAY = [ + 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, +]; +const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30]; +const STAR_DAY = [3, 10, 17, 24, 25, 31]; + +export function toTalPriceLogic() { + const MENUS = christmasInstance.getMenus(); + let totalPrice = 0; + + MENUS.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (MENUS_PRICE.hasOwnProperty(MENU_NAME)) + totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY; + }); + + return totalPrice; +} + +export function ChampagnePromotionAvailable() { + const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus()); + if (TOTAL_PRICE >= 120000) { + return true; + } else return false; +} + +// d-day ํ• ์ธ +export function receivedD_dayPromotion() { + const DATE = christmasInstance.getDate(); + let minusPrice = 1000; + let available = true; + + if (DATE >= 26) { + available = false; + } + + if (available) { + for (let i = 1; i < DATE; i++) { + minusPrice += 100; + } + } else { + minusPrice = 0; + } + + return minusPrice; +} + +function minusPriceCalculator( + menus, + date, + validDays, + targetMenuCategories, + discountMultiplier +) { + let minusPrice = 0; + + if (!validDays.includes(date)) { + return minusPrice; + } + + menus.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (targetMenuCategories.includes(MENU_NAME)) + minusPrice += discountMultiplier * QUANTITY; + }); + + return minusPrice; +} + +// WEEKDAY ํ• ์ธ +export function receivedWeekDayPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const DESSERT = [`์ดˆ์ฝ”์ผ€์ดํฌ`, `์•„์ด์Šคํฌ๋ฆผ`]; + + return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023); +} + +// WEEKEND ํ• ์ธ +export function receivedWeekendPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const MAIN = ["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", "๋ฐ”๋น„ํ๋ฆฝ", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€"]; + + return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023); +} + +export function receivedSpecialPromotion() { + const DATE = christmasInstance.getDate(); + const MINUS_PRICE = 1000; + if (STAR_DAY.includes(DATE)) { + return MINUS_PRICE; + } else return 0; +} + +// ์ƒดํŽ˜์ธ ์ฆ์ • ์ด๋ฒคํŠธ +export function receivedChampagnePromotion() { + const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable(); + const CHAMPAGNE_PRICE = 25000; + + if (CHAMPAGNE_AVAILABLE) { + return CHAMPAGNE_PRICE; + } else return 0; +} + +export function receivedTotalBenefitPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + let resultTotalBenefitPrice; + + const totalBenefitPrice = + DDAY_AVAILABLE + + WEEKDAY_AVAILABLE + + WEEKEND_AVAILABLE + + CHAMPAGNE_AVAILABLE + + SPECIAL_AVAILABLE; + + if (totalBenefitPrice === 0) return 0; + if (totalBenefitPrice > 0) return -totalBenefitPrice; +} + +export function receivedTotalDsicountPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + + const TOTAL_DISCOUNTPRICE = + DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE; + + return TOTAL_DISCOUNTPRICE; +} + +export function totalPriceAfterDiscount() { + const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice(); + const TOTAL_PRICE = toTalPriceLogic(); + + const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE; + + return TOTAL_AFTER; +} + +export function sendBadge() { + const TOTAL_AFTER = -receivedTotalBenefitPrice(); + if (TOTAL_AFTER >= 20000) { + return "์‚ฐํƒ€"; + } else if (TOTAL_AFTER >= 10000) { + return "ํŠธ๋ฆฌ"; + } else if (TOTAL_AFTER >= 5000) { + return "๋ณ„"; + } else { + return "์—†์Œ"; + } +}
JavaScript
`forEach`์™€ `if๋ฌธ`์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ Menu ๊ฐ€๊ฒฉ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋„ค์š”!
@@ -0,0 +1,174 @@ +import { christmasInstance } from "./InputView.js"; +import menuAndQuantity from "./utils/menuAndQuantity.js"; + +const MENUS_PRICE = { + ์–‘์†ก์ด์ˆ˜ํ”„: 6000, + ํƒ€ํŒŒ์Šค: 5500, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: 8000, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: 55000, + ๋ฐ”๋น„ํ๋ฆฝ: 54000, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: 35000, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: 25000, + ์ดˆ์ฝ”์ผ€์ดํฌ: 15000, + ์•„์ด์Šคํฌ๋ฆผ: 5000, + ์ œ๋กœ์ฝœ๋ผ: 3000, + ๋ ˆ๋“œ์™€์ธ: 60000, + ์ƒดํŽ˜์ธ: 25000, +}; +const WEEKDAY = [ + 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, +]; +const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30]; +const STAR_DAY = [3, 10, 17, 24, 25, 31]; + +export function toTalPriceLogic() { + const MENUS = christmasInstance.getMenus(); + let totalPrice = 0; + + MENUS.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (MENUS_PRICE.hasOwnProperty(MENU_NAME)) + totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY; + }); + + return totalPrice; +} + +export function ChampagnePromotionAvailable() { + const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus()); + if (TOTAL_PRICE >= 120000) { + return true; + } else return false; +} + +// d-day ํ• ์ธ +export function receivedD_dayPromotion() { + const DATE = christmasInstance.getDate(); + let minusPrice = 1000; + let available = true; + + if (DATE >= 26) { + available = false; + } + + if (available) { + for (let i = 1; i < DATE; i++) { + minusPrice += 100; + } + } else { + minusPrice = 0; + } + + return minusPrice; +} + +function minusPriceCalculator( + menus, + date, + validDays, + targetMenuCategories, + discountMultiplier +) { + let minusPrice = 0; + + if (!validDays.includes(date)) { + return minusPrice; + } + + menus.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (targetMenuCategories.includes(MENU_NAME)) + minusPrice += discountMultiplier * QUANTITY; + }); + + return minusPrice; +} + +// WEEKDAY ํ• ์ธ +export function receivedWeekDayPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const DESSERT = [`์ดˆ์ฝ”์ผ€์ดํฌ`, `์•„์ด์Šคํฌ๋ฆผ`]; + + return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023); +} + +// WEEKEND ํ• ์ธ +export function receivedWeekendPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const MAIN = ["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", "๋ฐ”๋น„ํ๋ฆฝ", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€"]; + + return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023); +} + +export function receivedSpecialPromotion() { + const DATE = christmasInstance.getDate(); + const MINUS_PRICE = 1000; + if (STAR_DAY.includes(DATE)) { + return MINUS_PRICE; + } else return 0; +} + +// ์ƒดํŽ˜์ธ ์ฆ์ • ์ด๋ฒคํŠธ +export function receivedChampagnePromotion() { + const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable(); + const CHAMPAGNE_PRICE = 25000; + + if (CHAMPAGNE_AVAILABLE) { + return CHAMPAGNE_PRICE; + } else return 0; +} + +export function receivedTotalBenefitPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + let resultTotalBenefitPrice; + + const totalBenefitPrice = + DDAY_AVAILABLE + + WEEKDAY_AVAILABLE + + WEEKEND_AVAILABLE + + CHAMPAGNE_AVAILABLE + + SPECIAL_AVAILABLE; + + if (totalBenefitPrice === 0) return 0; + if (totalBenefitPrice > 0) return -totalBenefitPrice; +} + +export function receivedTotalDsicountPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + + const TOTAL_DISCOUNTPRICE = + DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE; + + return TOTAL_DISCOUNTPRICE; +} + +export function totalPriceAfterDiscount() { + const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice(); + const TOTAL_PRICE = toTalPriceLogic(); + + const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE; + + return TOTAL_AFTER; +} + +export function sendBadge() { + const TOTAL_AFTER = -receivedTotalBenefitPrice(); + if (TOTAL_AFTER >= 20000) { + return "์‚ฐํƒ€"; + } else if (TOTAL_AFTER >= 10000) { + return "ํŠธ๋ฆฌ"; + } else if (TOTAL_AFTER >= 5000) { + return "๋ณ„"; + } else { + return "์—†์Œ"; + } +}
JavaScript
```js if (available) { minusPrice += (DATE -1) * 100; } else { minusPrice = 0; } ``` Songhynseop ๋ฆฌ๋ทฐ๋กœ ์•Œ๋ ค์ฃผ์‹  ๊ฒƒ์„ ๋ณด๊ณ  ๊ณ ์ณ๋ณด์•˜์–ด์š”. ์ˆ˜์‹์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋„ค์š”. 'ํŠน์ • ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•  ๋•Œ ๊ผญ ํ•„์š”ํ•œ ๊ฐ€? ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•  ์ˆ˜ ์—†๋‚˜' ์ƒ๊ฐํ•ด ๋ณผ๊ฒŒ์š”!
@@ -0,0 +1,174 @@ +import { christmasInstance } from "./InputView.js"; +import menuAndQuantity from "./utils/menuAndQuantity.js"; + +const MENUS_PRICE = { + ์–‘์†ก์ด์ˆ˜ํ”„: 6000, + ํƒ€ํŒŒ์Šค: 5500, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: 8000, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: 55000, + ๋ฐ”๋น„ํ๋ฆฝ: 54000, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: 35000, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: 25000, + ์ดˆ์ฝ”์ผ€์ดํฌ: 15000, + ์•„์ด์Šคํฌ๋ฆผ: 5000, + ์ œ๋กœ์ฝœ๋ผ: 3000, + ๋ ˆ๋“œ์™€์ธ: 60000, + ์ƒดํŽ˜์ธ: 25000, +}; +const WEEKDAY = [ + 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, +]; +const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30]; +const STAR_DAY = [3, 10, 17, 24, 25, 31]; + +export function toTalPriceLogic() { + const MENUS = christmasInstance.getMenus(); + let totalPrice = 0; + + MENUS.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (MENUS_PRICE.hasOwnProperty(MENU_NAME)) + totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY; + }); + + return totalPrice; +} + +export function ChampagnePromotionAvailable() { + const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus()); + if (TOTAL_PRICE >= 120000) { + return true; + } else return false; +} + +// d-day ํ• ์ธ +export function receivedD_dayPromotion() { + const DATE = christmasInstance.getDate(); + let minusPrice = 1000; + let available = true; + + if (DATE >= 26) { + available = false; + } + + if (available) { + for (let i = 1; i < DATE; i++) { + minusPrice += 100; + } + } else { + minusPrice = 0; + } + + return minusPrice; +} + +function minusPriceCalculator( + menus, + date, + validDays, + targetMenuCategories, + discountMultiplier +) { + let minusPrice = 0; + + if (!validDays.includes(date)) { + return minusPrice; + } + + menus.forEach((menu) => { + const { MENU_NAME, QUANTITY } = menuAndQuantity(menu); + if (targetMenuCategories.includes(MENU_NAME)) + minusPrice += discountMultiplier * QUANTITY; + }); + + return minusPrice; +} + +// WEEKDAY ํ• ์ธ +export function receivedWeekDayPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const DESSERT = [`์ดˆ์ฝ”์ผ€์ดํฌ`, `์•„์ด์Šคํฌ๋ฆผ`]; + + return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023); +} + +// WEEKEND ํ• ์ธ +export function receivedWeekendPromotion() { + const MENUS = christmasInstance.getMenus(); + const DATE = christmasInstance.getDate(); + const MAIN = ["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", "๋ฐ”๋น„ํ๋ฆฝ", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€"]; + + return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023); +} + +export function receivedSpecialPromotion() { + const DATE = christmasInstance.getDate(); + const MINUS_PRICE = 1000; + if (STAR_DAY.includes(DATE)) { + return MINUS_PRICE; + } else return 0; +} + +// ์ƒดํŽ˜์ธ ์ฆ์ • ์ด๋ฒคํŠธ +export function receivedChampagnePromotion() { + const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable(); + const CHAMPAGNE_PRICE = 25000; + + if (CHAMPAGNE_AVAILABLE) { + return CHAMPAGNE_PRICE; + } else return 0; +} + +export function receivedTotalBenefitPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + let resultTotalBenefitPrice; + + const totalBenefitPrice = + DDAY_AVAILABLE + + WEEKDAY_AVAILABLE + + WEEKEND_AVAILABLE + + CHAMPAGNE_AVAILABLE + + SPECIAL_AVAILABLE; + + if (totalBenefitPrice === 0) return 0; + if (totalBenefitPrice > 0) return -totalBenefitPrice; +} + +export function receivedTotalDsicountPrice() { + const DDAY_AVAILABLE = receivedD_dayPromotion(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion(); + + const TOTAL_DISCOUNTPRICE = + DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE; + + return TOTAL_DISCOUNTPRICE; +} + +export function totalPriceAfterDiscount() { + const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice(); + const TOTAL_PRICE = toTalPriceLogic(); + + const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE; + + return TOTAL_AFTER; +} + +export function sendBadge() { + const TOTAL_AFTER = -receivedTotalBenefitPrice(); + if (TOTAL_AFTER >= 20000) { + return "์‚ฐํƒ€"; + } else if (TOTAL_AFTER >= 10000) { + return "ํŠธ๋ฆฌ"; + } else if (TOTAL_AFTER >= 5000) { + return "๋ณ„"; + } else { + return "์—†์Œ"; + } +}
JavaScript
Songhyunseop ๋•๋ถ„์— ์ ˆ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” `Math.abs()`์˜ ์‚ฌ์šฉ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ์–ด์š”. ์ด๋ฏธ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹๋‹ค๊ณ  ํ”ผ๋“œ๋ฐฑ์—์„œ ๋ณด์•˜๋Š”๋ฐ ์ด ๋œป์ธ ๊ฒƒ ๊ฐ™์•„์š”. else if๋ฌธ ๋Œ€์‹  if์—์„œ return ์ ์šฉ ์˜ˆ์ œ ๋ณด์—ฌ์ฃผ์‹  ๊ฑฐ ๊ฐ์‚ฌํ•ด์š”. hyurim ๋ฆฌ๋ทฐ์— ๋”ฐ๋ฅด๋ฉด ๋” ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์•„์š”. ```JS const BADGE_THRESHOLDS = { ์‚ฐํƒ€: 20000, ํŠธ๋ฆฌ: 10000, ๋ณ„: 5000 }; export function sendBadge() { const TOTAL_AFTER = Math.abs(receivedTotalBenefitPrice()); for (const BADGE of Object.keys(BADGE_THRESHOLDS)) { if (TOTAL_AFTER >= BADGE_THRESHOLDS[BADGE]) { return BADGE; } } return "์—†์Œ"; } ``` ์œ ์ง€ ๋ณด์ˆ˜์—๋„ ํŽธํ•  ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,78 @@ +import { + ChampagnePromotionAvailable, + receivedD_dayPromotion, + toTalPriceLogic, +} from "../src/DomainLogic"; +import menuAndQuantity from "../src/utils/menuAndQuantity"; + +jest.mock("../src/InputView", () => ({ + christmasInstance: { + getMenus: jest.fn(), + getDate: jest.fn(), + }, +})); + +jest.mock("../src/utils/menuAndQuantity", () => ({ + __esModule: true, + default: jest.fn(), +})); + +jest.mock("../src/DomainLogic", () => ({ + ...jest.requireActual("../src/DomainLogic"), + toTalPriceLogic: jest.fn(), +})); + +describe("DomainLogic ๊ธฐ๋Šฅ ํ…Œ์ŠคํŠธ", () => { + test("ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก", () => { + const mockedMenus = ["์–‘์†ก์ด์ˆ˜ํ”„-2", "์–‘์†ก์ด์ˆ˜ํ”„-1", "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-3"]; + require("../src/InputView").christmasInstance.getMenus.mockReturnValue( + mockedMenus + ); + + menuAndQuantity + .mockReturnValueOnce({ MENU_NAME: "์–‘์†ก์ด์ˆ˜ํ”„", QUANTITY: 2 }) + .mockReturnValueOnce({ MENU_NAME: "์ดˆ์ฝ”์ผ€์ดํฌ", QUANTITY: 1 }) + .mockReturnValueOnce({ MENU_NAME: "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", QUANTITY: 3 }); + + const MENUS_PRICE = { + ์–‘์†ก์ด์ˆ˜ํ”„: 6000, + ์ดˆ์ฝ”์ผ€์ดํฌ: 15000, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: 55000, + }; + + toTalPriceLogic.mockReturnValueOnce(192000); + + const result = toTalPriceLogic(MENUS_PRICE); + const expectedTotalPrice = 2 * 6000 + 1 * 15000 + 3 * 55000; + + expect(result).toBe(expectedTotalPrice); + }); + + test("์ฆ์ • ๋ฉ”๋‰ด", () => { + const mockedMenus = ["์•„์ด์Šคํฌ๋ฆผ-2", "์ดˆ์ฝ”์ผ€์ดํฌ-1"]; + require("../src/InputView").christmasInstance.getMenus.mockReturnValue( + mockedMenus + ); + + toTalPriceLogic.mockReturnValueOnce(25000); + + const result = ChampagnePromotionAvailable(); + expect(result).toBe(false); + }); + + test("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ", () => { + require("../src/InputView").christmasInstance.getDate.mockReturnValue(25); + + const result = receivedD_dayPromotion(); + + expect(result).toBe(3400); + }); + + test("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ ์˜ˆ์™ธ", () => { + require("../src/InputView").christmasInstance.getDate.mockReturnValue(26); + + const result = receivedD_dayPromotion(); + + expect(result).toBe(0); + }); +});
JavaScript
christmasInstance๋ฅผ ์ œ์™ธํ•˜๋ฉด ํ•จ์ˆ˜๋ผ mock๋ฅผ ์ ์šฉํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,86 @@ +class ChristmasPromotion { + #date; + #menus; + + constructor(date, menus) { + this.#date = date; + this.#menus = menus; + } + + #dateValidate(date) { + if (!(date >= 1 && date <= 31)) { + throw new Error("[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + } + + startDateValidate(date) { + this.#dateValidate(date); + this.#date = date; + } + + #menusValidate(menus) { + const SEENMENUS = new Set(); + + for (const menu of menus) { + if (!/^(.+)-(.+)$/.test(menu)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + const MENU_LIST = [ + "์–‘์†ก์ด์ˆ˜ํ”„", + "ํƒ€ํŒŒ์Šค", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", + "๋ฐ”๋น„ํ๋ฆฝ", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", + "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", + "์ดˆ์ฝ”์ผ€์ดํฌ", + "์•„์ด์Šคํฌ๋ฆผ", + "์ œ๋กœ์ฝœ๋ผ", + "๋ ˆ๋“œ์™€์ธ", + "์ƒดํŽ˜์ธ", + ]; + + const [MENU_PART, QUANTITY] = menu.split("-"); + const QUANTITY_PART = parseInt(QUANTITY, 10); + + if (!MENU_LIST.includes(MENU_PART)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + // ์ค‘๋ณต ๋””ํ…Œ์ผํ•˜๊ฒŒ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ•„์š” + if (SEENMENUS.has(menu)) { + throw new Error( + `[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` + ); + } + + SEENMENUS.add(menu); + } + } + + startMenuValidate(menus) { + this.#menusValidate(menus); + this.#menus = menus; + } + + getDate() { + return this.#date; + } + + getMenus() { + return this.#menus; + } +} + +export default ChristmasPromotion;
JavaScript
````js menus.forEach((menu) => { . . . ```` ์ด๋Ÿฐ ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๊ณ ์น˜๊ฒ ์Šต๋‹ˆ๋‹ค. `for..of`๋Š” ์‚ฌ์šฉ์„ ์ง€์–‘ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -1,7 +1,27 @@ -export default InputView = { - async readDate() { - const input = await Console.readLineAsync("12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"); - // ... - } - // ... -} +import { MissionUtils } from "@woowacourse/mission-utils"; +import ChristmasDomain from "./ChristmasPromotion.js"; +import Order from "./Order.js"; + +export const christmasInstance = new ChristmasDomain(); + +const InputView = { + async readDate() { + const input = await MissionUtils.Console.readLineAsync( + "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n" + ); + const DATE = parseInt(input, 10); + christmasInstance.startDateValidate(DATE); + }, + + async readOrderMenu() { + const input = await MissionUtils.Console.readLineAsync( + "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n" + ); + new Order(input); + const MENUES = input.split(","); + christmasInstance.startMenuValidate(MENUES); + }, + // ... +}; + +export default InputView;
JavaScript
์˜คํ˜ธ! ```js import { Console } from "@woowacourse/mission-utils"; ``` import ํ•ด์˜ค๋Š” ๊ฒƒ๋„ ์„ ์–ธํ–ˆ๋‹ค๊ณ  ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ๋„ ์ฒ˜์Œ ์•Œ์•˜์–ด์š”! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -1,7 +1,27 @@ -export default InputView = { - async readDate() { - const input = await Console.readLineAsync("12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"); - // ... - } - // ... -} +import { MissionUtils } from "@woowacourse/mission-utils"; +import ChristmasDomain from "./ChristmasPromotion.js"; +import Order from "./Order.js"; + +export const christmasInstance = new ChristmasDomain(); + +const InputView = { + async readDate() { + const input = await MissionUtils.Console.readLineAsync( + "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n" + ); + const DATE = parseInt(input, 10); + christmasInstance.startDateValidate(DATE); + }, + + async readOrderMenu() { + const input = await MissionUtils.Console.readLineAsync( + "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n" + ); + new Order(input); + const MENUES = input.split(","); + christmasInstance.startMenuValidate(MENUES); + }, + // ... +}; + +export default InputView;
JavaScript
๋„ค ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์€ ์ƒ์ˆ˜๋กœ ๋ฐ”๊ฟ”์•ผ ์ข‹๋„ค์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,24 @@ +import menuAndQuantity from "./utils/menuAndQuantity.js"; + +class Order { + #order; + + constructor(order) { + this.#order = Array.isArray(order) ? order : [order]; + this.#orderQuantityValidate(); + } + + #orderQuantityValidate() { + this.#order.forEach((menu) => { + const { QUANTITY } = menuAndQuantity(menu); + + if (QUANTITY > 20) { + throw new Error( + `[ERROR] ๋ฉ”๋‰ด๋Š” ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 20๊ฐœ๊นŒ์ง€๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.` + ); + } + }); + } +} + +export default Order;
JavaScript
```js export default function menuAndQuantity(menu) { const [MENU_NAME, NUMBER] = menu.split("-"); const QUANTITY = parseInt(NUMBER, 10); return { MENU_NAME, QUANTITY }; } ``` **์ด `menuAndQuantity(menu)` ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ˆ˜๋Ÿ‰ ํŒŒ์•…**ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” menu (menu-quantity)๊ฐ€ ํ•˜๋‚˜์”ฉ ์ „๋‹ฌํ•ด์•ผ ํ•ด์„œ `forEach()`๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
@@ -1,7 +1,110 @@ -export default OutputView = { - printMenu() { - Console.print("<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"); - // ... +import { MissionUtils } from "@woowacourse/mission-utils"; +import { christmasInstance } from "./InputView.js"; +import { + ChampagnePromotionAvailable, + receivedChampagnePromotion, + receivedD_dayPromotion, + receivedSpecialPromotion, + receivedTotalBenefitPrice, + receivedWeekDayPromotion, + receivedWeekendPromotion, + sendBadge, + toTalPriceLogic, + totalPriceAfterDiscount, +} from "./DomainLogic.js"; +import menuAndQuantity from "./utils/menuAndQuantity.js"; + +const OutputView = { + printIntroduction() { + MissionUtils.Console.print( + "์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค." + ); + }, + + printBenefitIntroduction() { + MissionUtils.Console.print( + "12์›” 3์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n" + ); + }, + + printMenu() { + MissionUtils.Console.print("<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"); + const ORDERED_MENUS = christmasInstance.getMenus(); + ORDERED_MENUS.map(function (eachMenu) { + const { MENU_NAME, QUANTITY } = menuAndQuantity(eachMenu); + MissionUtils.Console.print(`${MENU_NAME} ${QUANTITY}๊ฐœ`); + }); + }, + + printTotalPrice() { + MissionUtils.Console.print("\n<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>"); + const TOTAL_PRICE = toTalPriceLogic().toLocaleString(); + MissionUtils.Console.print(`${TOTAL_PRICE}์›`); + }, + + printChampagnePromotion() { + MissionUtils.Console.print("\n<์ฆ์ • ๋ฉ”๋‰ด>"); + const CHAMPAGNEPROMOTION_AVAILABLE = ChampagnePromotionAvailable(); + if (CHAMPAGNEPROMOTION_AVAILABLE === true) { + MissionUtils.Console.print("์ƒดํŽ˜์ธ 1๊ฐœ"); + } else MissionUtils.Console.print("์—†์Œ"); + }, + + printReceivedPromotion() { + MissionUtils.Console.print("\n<ํ˜œํƒ ๋‚ด์—ญ>"); + const DDAY_AVAILABLE = receivedD_dayPromotion().toLocaleString(); + const WEEKDAY_AVAILABLE = receivedWeekDayPromotion().toLocaleString(); + const WEEKEND_AVAILABLE = receivedWeekendPromotion().toLocaleString(); + const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion().toLocaleString(); + const SPECIAL_AVAILABLE = receivedSpecialPromotion().toLocaleString(); + + let anyPromotionApplied = false; + + if (DDAY_AVAILABLE !== "0") { + MissionUtils.Console.print( + `ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ: -${DDAY_AVAILABLE}์›` + ); + anyPromotionApplied = true; } - // ... -} + + if (WEEKDAY_AVAILABLE !== "0") { + MissionUtils.Console.print(`ํ‰์ผ ํ• ์ธ: -${WEEKDAY_AVAILABLE}์›`); + anyPromotionApplied = true; + } + if (WEEKEND_AVAILABLE !== "0") { + MissionUtils.Console.print(`์ฃผ๋ง ํ• ์ธ: -${WEEKEND_AVAILABLE}์›`); + anyPromotionApplied = true; + } + if (SPECIAL_AVAILABLE !== "0") { + MissionUtils.Console.print(`ํŠน๋ณ„ ํ• ์ธ: -${SPECIAL_AVAILABLE}์›`); + anyPromotionApplied = true; + } + if (CHAMPAGNE_AVAILABLE !== "0") { + MissionUtils.Console.print(`์ฆ์ • ์ด๋ฒคํŠธ: -${CHAMPAGNE_AVAILABLE}์›`); + anyPromotionApplied = true; + } + if (!anyPromotionApplied) { + MissionUtils.Console.print("์—†์Œ"); + } + }, + + printReceivedTotalBenefitPrice() { + MissionUtils.Console.print("\n<์ดํ˜œํƒ ๊ธˆ์•ก>"); + const TOTAL_BENEFITPRICE = receivedTotalBenefitPrice().toLocaleString(); + MissionUtils.Console.print(`${TOTAL_BENEFITPRICE}์›`); + }, + + printTotalPriceAfterDiscount() { + MissionUtils.Console.print("\n<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>"); + const TOTAL_AFTER = totalPriceAfterDiscount().toLocaleString(); + MissionUtils.Console.print(`${TOTAL_AFTER}์›`); + }, + + printEventBadge() { + MissionUtils.Console.print("\n<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>"); + const GET_BADGE = sendBadge(); + MissionUtils.Console.print(`${GET_BADGE}`); + }, +}; + +export default OutputView;
JavaScript
return ๊ฐ’์ด 0, ๊ณตํ†ต๋ถ€๋ถ„์„ ํ™œ์šฉํ•˜๋ฉด ์ •๋ง ๊น”๋”ํ•œ ์ฝ”๋“œ๊ฐ€ ๋˜๊ฒ ๋„ค์š”! ๋ฐ”๋กœ ๊ณ ์น˜๊ธฐ์—๋Š” ์ข€ ์–ด๋ ค์›Œ์„œ ๋” ์ƒ๊ฐํ•ด๋ณด๊ณ  ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,5 @@ +package store.constants; + +public class MembershipConstants { + public static final double DISCOUNT_RATE = 0.30; +}
Java
์ด ๋ถ€๋ถ„์€ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๊ฒŒ๋” ์ƒ์„ฑ์ž๋ฅผ private์œผ๋กœ ๋งŒ๋“ค๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,18 @@ +package store.constants; + +public class ReceiptConstants { + public static final String ORDER_DETAIL_FORMAT = "%-10s %6s %12s"; + public static final String PROMOTION_DETAIL_FORMAT = "%-10s %6s"; + public static final String TOTAL_DETAIL_FORMAT = "%-10s %18s"; + public static final String LINE_SEPARATOR = "===================================="; + public static final String PROMOTION_HEADER = "=============์ฆ\t\t์ •==============="; + public static final String RECEIPT_HEADER = "==============W ํŽธ์˜์ ================"; + public static final String PRODUCT_HEADER = "%-10s %6s %12s"; + public static final String PRODUCT_NAME = "์ƒํ’ˆ๋ช…"; + public static final String QUANTITY = "์ˆ˜๋Ÿ‰"; + public static final String PRICE = "๊ธˆ์•ก"; + public static final String TOTAL_PURCHASE_AMOUNT = "์ด๊ตฌ๋งค์•ก"; + public static final String EVENT_DISCOUNT = "ํ–‰์‚ฌํ• ์ธ"; + public static final String MEMBERSHIP_DISCOUNT = "๋ฉค๋ฒ„์‹ญํ• ์ธ"; + public static final String FINAL_AMOUNT = "๋‚ด์‹ค๋ˆ"; +}
Java
Enum์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? String.format์„ ์‚ฌ์šฉํ•˜์‹ค ๋•Œ Enum ๋‚ด์— ๋ณ€ํ™˜ํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋ฉด OuptView์—์„œ๋„ ๋” ๊น”๋”ํ•˜๊ฒŒ ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,38 @@ +package store.controller; + +import java.util.List; +import java.util.Map; +import store.domain.Product; +import store.domain.Promotion; +import store.dto.StoreDto; +import store.dto.StoreInitializationDto; +import store.parser.ProductParser; +import store.parser.PromotionParser; +import store.service.FileReaderService; + +public class FileReaderController { + private final FileReaderService fileReaderService; + private final PromotionParser promotionParser; + private final ProductParser productParser; + + public FileReaderController() { + this.fileReaderService = new FileReaderService(); + this.productParser = new ProductParser(); + this.promotionParser = new PromotionParser(); + } + + public StoreDto runFileData() { + return initialize(); + } + + public StoreDto initialize() { + StoreInitializationDto storeInitializationDto = fileReaderService.initializeStoreData(); + return parseStoreData(storeInitializationDto); + } + + public StoreDto parseStoreData(StoreInitializationDto storeInitializationDto) { + List<Promotion> promotions = promotionParser.parse(storeInitializationDto.promotionDtos()); + Map<String, Product> products = productParser.parse(storeInitializationDto.productDtos(), promotions); + return new StoreDto(products, promotions); + } +}
Java
์ด๋Ÿฐ ๋ถ€๋ถ„์€ private๋ฅผ ํ†ตํ•ด ์บก์Аํ™”ํ•˜๋Š” ๊ฒƒ์ด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,23 @@ +package store.controller; + +import store.domain.Store; +import store.dto.StoreDto; +import store.service.StoreService; + +public class StoreController { + + private final StoreService storeService; + + public StoreController() { + this.storeService = new StoreService(); + } + + public void run(StoreDto storeDto) { + Store store = createStore(storeDto); + storeService.processOrder(store); + } + + private Store createStore(StoreDto storeDto) { + return new Store(storeDto); + } +}
Java
์™ธ๋ถ€์—์„œ ์˜์กด์„ฑ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,35 @@ +package store.domain; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class Cart { + private final Map<String, CartItem> cartItems; + + public Cart() { + this.cartItems = new LinkedHashMap<>(); + } + + public void addItem(List<CartItem> cartItems) { + for (CartItem cartItem : cartItems) { + this.cartItems.put(cartItem.getProduct().getName(), cartItem); + } + } + + public List<CartItem> getAllItemsInCart() { + return cartItems.values().stream().toList(); + } + + public int getTotalFreeItemQuantity() { + return cartItems.values().stream().mapToInt(CartItem::getFreeQuantity).sum(); + } + + public int getTotalFreeItemPrice() { + return cartItems.values().stream().mapToInt(CartItem::getTotalFreePrice).sum(); + } + + public int getTotalItemPrice() { + return cartItems.values().stream().mapToInt(CartItem::totalPrice).sum(); + } +}
Java
Stream API๋Š” ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ์—”ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,56 @@ +package store.domain; + +import static store.message.ErrorMessage.INSUFFICIENT_STOCK_ERROR; + +import store.validation.CartItemValidator; + +public class CartItem { + private final Product product; + private int quantity; + private int freeQuantity; + + public CartItem(Product product, int quantity) { + CartItemValidator.validateCartItem(quantity); + this.product = product; + this.quantity = quantity; + } + + public void increaseQuantity(int updateQuantity) { + this.freeQuantity += updateQuantity; + } + + public void decreaseQuantity(int updateQuantity) { + this.quantity -= updateQuantity; + if (quantity <= 0) { + throw new IllegalArgumentException(INSUFFICIENT_STOCK_ERROR.getMessage()); + } + } + + public String getProductName() { + return product.getName(); + } + + public int totalPrice() { + return product.getPrice() * getTotalQuantity(); + } + + public int getTotalFreePrice() { + return product.getPrice() * freeQuantity; + } + + public int getQuantity() { + return quantity; + } + + public int getTotalQuantity() { + return quantity + freeQuantity; + } + + public Product getProduct() { + return product; + } + + public int getFreeQuantity() { + return freeQuantity; + } +}
Java
๋””๋ฏธํ„ฐ ๋ฒ•์น™์„ ์ค€์ˆ˜ํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ•˜์‹  ๋ฉ”์„œ๋“œ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ์ €๋„ ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋Š”๋ฐ์š”, 3์ฃผ์ฐจ ํ”ผ๋“œ๋ฐฑ๊ณผ ์ž๋ฐ” ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ์—์„œ Getter๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋ณด๋‹ค ๊ฐ์ฒด ๋‚ด์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด ๋” ์ข‹๋‹ค๋Š” ๋‚ด์šฉ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,136 @@ +package store.domain; + +import static store.constants.ProductConstants.OUT_OF_STOCK; +import static store.constants.ProductConstants.PRODUCT_DESCRIPTION_PREFIX; +import static store.constants.ProductConstants.UNIT_QUANTITY; +import static store.constants.ProductConstants.UNIT_WON; +import static store.message.ErrorMessage.NOT_FOUND_PROMOTION; + +import java.text.NumberFormat; +import java.util.Locale; +import java.util.Optional; +import store.dto.ProductDto; +import store.validation.ProductValidator; + +public class Product { + private String name; + private int price; + private int quantity; + private Optional<Promotion> promotion; + + private Product() { + } + + public Product(ProductDto productDto, Optional<Promotion> promotion) { + ProductValidator.validate(productDto); + this.name = productDto.name(); + this.price = Integer.parseInt(productDto.price()); + this.quantity = Integer.parseInt(productDto.quantity()); + this.promotion = promotion; + } + + /** + * ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉ ์—ฌ๋ถ€ ๊ฒ€์ฆ + */ + public boolean isEligibleForStandardPromotion(int orderedQuantity) { + if (promotion.isEmpty()) { + return false; + } + Promotion promotionInfo = promotion.get(); + return orderedQuantity <= quantity + && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity; + } + + /** + * ๋ณด๋„ˆ์Šค ์ƒํ’ˆ์ด ์ ์šฉ ์—ฌ๋ถ€ ๊ฒ€์ฆ + */ + public boolean isEligibleForBonusProduct(int orderedQuantity) { + if (promotion.isEmpty()) { + return false; + } + Promotion promotionInfo = promotion.get(); + return getRemainingItemsForPromotion(orderedQuantity, promotionInfo) >= promotionInfo.getBuy() + && orderedQuantity + promotionInfo.getGet() <= quantity; + } + + /** + * ํ”„๋กœ๋ชจ์…˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋‚จ์€ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int getRemainingItemsForPromotion(int orderedQuantity, Promotion promotionInfo) { + return orderedQuantity % promotionInfo.getTotalRequiredQuantity(); + } + + /** + * ๋ณด๋„ˆ์Šค ์ƒํ’ˆ์˜ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int calculateBonusQuantity(int orderedQuantity, Promotion promotion) { + return (orderedQuantity / promotion.getTotalRequiredQuantity()) * promotion.getGet(); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ํ›„, ์‹ค์ œ ์ฃผ๋ฌธ ๊ฐ€๋Šฅํ•œ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int calculateQuantityAfterPromotion(int orderedQuantity) { + Promotion promotionInfo = getPromotionOrElseThrow(); + int requiredQuantity = promotionInfo.getTotalRequiredQuantity(); + return (orderedQuantity - quantity) + (quantity % requiredQuantity); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ + */ + public Promotion getPromotionOrElseThrow() { + return promotion.orElseThrow(() -> new IllegalArgumentException(NOT_FOUND_PROMOTION.getMessage())); + } + + /** + * ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฃผ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒํ’ˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ํฌ๋งท์„ ํ•ฉ๋‹ˆ๋‹ค. + */ + @Override + public String toString() { + return PRODUCT_DESCRIPTION_PREFIX + + name + " " + + getFormatKoreanLocale(price) + UNIT_WON + + getFormattedQuantity() + + getFormattedPromotion(); + } + + public String getFormattedQuantity() { + if (quantity < 1) { + return OUT_OF_STOCK; + } + return getFormatKoreanLocale(quantity) + UNIT_QUANTITY; + } + + public String getFormatKoreanLocale(int number) { + return NumberFormat.getInstance(Locale.KOREA).format(number); + } + + public String getFormattedPromotion() { + return promotion.map(Promotion::getName).orElse(""); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public Optional<Promotion> getPromotion() { + return promotion; + } + + public boolean isPromotionalProduct() { + return promotion.isPresent(); + } + + public void decreaseQuantity(int orderQuantity) { + this.quantity -= orderQuantity; + } +}
Java
1์ฃผ์ฐจ ํ”ผ๋“œ๋ฐฑ์—์„œ, ์ฃผ์„์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋ณด๋‹ค๋Š” ๋ฉ”์„œ๋“œ๋ช…์„ ํ†ตํ•ด ์˜๋„๋ฅผ ํŒŒ์•…ํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹๋‹ค๋Š” ๋‚ด์šฉ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์— ๋”ฐ๋กœ ์ฃผ์„์„ ํ‘œ์‹œํ•œ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,55 @@ +package store.domain; + +import java.time.LocalDateTime; +import store.dto.PromotionDto; +import store.util.DateUtil; +import store.validation.PromotionValidator; + +public class Promotion { + + private String name; //ํ”„๋กœ๋ชจ์…˜๋ช… + private int buy; // ๊ตฌ๋งค์กฐ๊ฑด + private int get; //์ฆ์ •์ˆ˜๋Ÿ‰ + private LocalDateTime startDate; + private LocalDateTime endDate; + + private Promotion() { + } + + public Promotion(PromotionDto promotionDto) { + PromotionValidator.validate(promotionDto); + this.name = promotionDto.name(); + this.buy = Integer.parseInt(promotionDto.buy()); + this.get = Integer.parseInt(promotionDto.get()); + this.startDate = DateUtil.dateParse(promotionDto.startDate()); + this.endDate = DateUtil.dateParse(promotionDto.endDate()); + } + + public boolean isPromotionName(String productPromotionName) { + return this.name.equals(productPromotionName); + } + + public String getName() { + return name; + } + + public int getTotalRequiredQuantity() { + return buy + get; + } + + public int getBuy() { + return buy; + } + + public int getGet() { + return get; + } + + public LocalDateTime getStartDate() { + return startDate; + } + + public LocalDateTime getEndDate() { + return endDate; + } +}
Java
์š”๊ฒƒ๋„ ์ฃผ์„๋ณด๋‹ค๋Š” ๋ณ€์ˆ˜๋ช…์„ ์ž˜ ์ง“๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,55 @@ +package store.domain; + +import java.time.LocalDateTime; +import store.dto.PromotionDto; +import store.util.DateUtil; +import store.validation.PromotionValidator; + +public class Promotion { + + private String name; //ํ”„๋กœ๋ชจ์…˜๋ช… + private int buy; // ๊ตฌ๋งค์กฐ๊ฑด + private int get; //์ฆ์ •์ˆ˜๋Ÿ‰ + private LocalDateTime startDate; + private LocalDateTime endDate; + + private Promotion() { + } + + public Promotion(PromotionDto promotionDto) { + PromotionValidator.validate(promotionDto); + this.name = promotionDto.name(); + this.buy = Integer.parseInt(promotionDto.buy()); + this.get = Integer.parseInt(promotionDto.get()); + this.startDate = DateUtil.dateParse(promotionDto.startDate()); + this.endDate = DateUtil.dateParse(promotionDto.endDate()); + } + + public boolean isPromotionName(String productPromotionName) { + return this.name.equals(productPromotionName); + } + + public String getName() { + return name; + } + + public int getTotalRequiredQuantity() { + return buy + get; + } + + public int getBuy() { + return buy; + } + + public int getGet() { + return get; + } + + public LocalDateTime getStartDate() { + return startDate; + } + + public LocalDateTime getEndDate() { + return endDate; + } +}
Java
getGet() ๋ฉ”์„œ๋“œ๋ช…์„ ๋ณด์‹œ๋ฉด ๊ฐ€๋…์„ฑ์ด ๋งค์šฐ ๋–จ์–ด์ง€๋Š” ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,152 @@ +package store.domain; + +import static store.constants.ReceiptConstants.EVENT_DISCOUNT; +import static store.constants.ReceiptConstants.FINAL_AMOUNT; +import static store.constants.ReceiptConstants.LINE_SEPARATOR; +import static store.constants.ReceiptConstants.MEMBERSHIP_DISCOUNT; +import static store.constants.ReceiptConstants.ORDER_DETAIL_FORMAT; +import static store.constants.ReceiptConstants.PRICE; +import static store.constants.ReceiptConstants.PRODUCT_HEADER; +import static store.constants.ReceiptConstants.PRODUCT_NAME; +import static store.constants.ReceiptConstants.PROMOTION_DETAIL_FORMAT; +import static store.constants.ReceiptConstants.PROMOTION_HEADER; +import static store.constants.ReceiptConstants.QUANTITY; +import static store.constants.ReceiptConstants.RECEIPT_HEADER; +import static store.constants.ReceiptConstants.TOTAL_DETAIL_FORMAT; +import static store.constants.ReceiptConstants.TOTAL_PURCHASE_AMOUNT; + +import java.text.NumberFormat; +import java.util.Locale; + +public class Receipt { + private final Store store; + private final Cart cart; + private final Membership membership; + + public Receipt(Store store, Cart cart, Membership membership) { + this.store = store; + this.cart = cart; + this.membership = membership; + } + + @Override + public String toString() { + return buildReceiptContent(); + } + + public boolean hasFreeItems() { + return cart.getTotalFreeItemQuantity() > 0; + } + + /** + * ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ฐ ๋ฉค๋ฒ„์‹ญ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์˜์ˆ˜์ฆ ๋‚ด์šฉ์„ ๋นŒ๋“œํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + * <p> + * ์ถ”๊ฐ€๋กœ ์ฆ์ •๋œ ์ƒํ’ˆ์ด ์—†๋‹ค๋ฉด ์ƒํ’ˆ๋‚ด์—ญ ์ •๋ณด๋Š” ์ถœ๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + */ + public String buildReceiptContent() { + StringBuilder result = new StringBuilder(); + result.append(getReceiptHeader()); + result.append(toStringOrderDetail()); + if (hasFreeItems()) { + result.append(toStringRemainingItemsForPromotion()); + } + result.append(toStringTotal()); + return result.toString(); + } + + /** + * ์˜์ˆ˜์ฆ ํ—ค๋”๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String getReceiptHeader() { + StringBuilder header = new StringBuilder(); + header.append(RECEIPT_HEADER).append("\n"); + header.append(String.format(PRODUCT_HEADER, PRODUCT_NAME, QUANTITY, PRICE)).append("\n"); + return header.toString(); + } + + /** + * ์ฃผ๋ฌธ ๋‚ด์—ญ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String toStringOrderDetail() { + StringBuilder orderDetail = new StringBuilder(); + + for (CartItem cartItem : cart.getAllItemsInCart()) { + orderDetail.append(String.format(ORDER_DETAIL_FORMAT, cartItem.getProductName(), + numberFormat(cartItem.getQuantity()), numberFormat(cartItem.totalPrice()))) + .append("\n"); + } + return orderDetail.toString(); + } + + /** + * ์ฆ์ • ์ƒํ’ˆ ๋‚ด์—ญ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String toStringRemainingItemsForPromotion() { + StringBuilder remainingitems = new StringBuilder(); + remainingitems.append(PROMOTION_HEADER).append("\n"); + + for (CartItem cartItem : cart.getAllItemsInCart()) { + if (cartItem.getFreeQuantity() > 0) { + remainingitems.append(String.format(PROMOTION_DETAIL_FORMAT, cartItem.getProductName(), + numberFormat(cartItem.getFreeQuantity()))) + .append("\n"); + } + } + return remainingitems.toString(); + } + + /** + * ์ด ๊ธˆ์•ก ๋‚ด์—ญ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String toStringTotal() { + StringBuilder total = new StringBuilder(); + total.append(LINE_SEPARATOR).append("\n"); + + total.append(formatTotalItemPrice()) + .append("\n"); + total.append(formatEventDiscount()) + .append("\n"); + total.append(formatMembershipDiscount()) + .append("\n"); + total.append(formatFinalAmount()) + .append("\n"); + + return total.toString(); + } + + /** + * ์ด ๊ตฌ๋งค ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatTotalItemPrice() { + return String.format(TOTAL_DETAIL_FORMAT, TOTAL_PURCHASE_AMOUNT, numberFormat(cart.getTotalItemPrice())); + } + + /** + * ์ด๋ฒคํŠธ ํ• ์ธ ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatEventDiscount() { + return String.format(TOTAL_DETAIL_FORMAT, EVENT_DISCOUNT, numberFormat(cart.getTotalFreeItemPrice())); + } + + /** + * ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatMembershipDiscount() { + return String.format(TOTAL_DETAIL_FORMAT, MEMBERSHIP_DISCOUNT, numberFormat(membership.getMembershipPrice())); + } + + /** + * ์ตœ์ข… ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatFinalAmount() { + int finalAmount = cart.getTotalItemPrice() - cart.getTotalFreeItemPrice() - membership.getMembershipPrice(); + return String.format(TOTAL_DETAIL_FORMAT, FINAL_AMOUNT, numberFormat(finalAmount)); + } + + /** + * ์ˆซ์ž๋ฅผ ํ•œ๊ตญ์˜ ์ˆซ์ž ํฌ๋งท์— ๋งž๊ฒŒ ํ˜•์‹ํ™”ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String numberFormat(int number) { + return NumberFormat.getInstance(Locale.KOREA).format(number); + } +}
Java
์—ฌ๊ธฐ๋Š” ์™œ ๋นˆ๋ผ์ธ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์œผ์…จ๋‚˜์š”? ์ด์ „ ์ฝ”๋“œ์—๋Š” ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ์ถ”๊ฐ€ํ•œ ๊ฒƒ ๊ฐ™์•„์„œ์š”!
@@ -0,0 +1,152 @@ +package store.domain; + +import static store.constants.ReceiptConstants.EVENT_DISCOUNT; +import static store.constants.ReceiptConstants.FINAL_AMOUNT; +import static store.constants.ReceiptConstants.LINE_SEPARATOR; +import static store.constants.ReceiptConstants.MEMBERSHIP_DISCOUNT; +import static store.constants.ReceiptConstants.ORDER_DETAIL_FORMAT; +import static store.constants.ReceiptConstants.PRICE; +import static store.constants.ReceiptConstants.PRODUCT_HEADER; +import static store.constants.ReceiptConstants.PRODUCT_NAME; +import static store.constants.ReceiptConstants.PROMOTION_DETAIL_FORMAT; +import static store.constants.ReceiptConstants.PROMOTION_HEADER; +import static store.constants.ReceiptConstants.QUANTITY; +import static store.constants.ReceiptConstants.RECEIPT_HEADER; +import static store.constants.ReceiptConstants.TOTAL_DETAIL_FORMAT; +import static store.constants.ReceiptConstants.TOTAL_PURCHASE_AMOUNT; + +import java.text.NumberFormat; +import java.util.Locale; + +public class Receipt { + private final Store store; + private final Cart cart; + private final Membership membership; + + public Receipt(Store store, Cart cart, Membership membership) { + this.store = store; + this.cart = cart; + this.membership = membership; + } + + @Override + public String toString() { + return buildReceiptContent(); + } + + public boolean hasFreeItems() { + return cart.getTotalFreeItemQuantity() > 0; + } + + /** + * ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ฐ ๋ฉค๋ฒ„์‹ญ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์˜์ˆ˜์ฆ ๋‚ด์šฉ์„ ๋นŒ๋“œํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + * <p> + * ์ถ”๊ฐ€๋กœ ์ฆ์ •๋œ ์ƒํ’ˆ์ด ์—†๋‹ค๋ฉด ์ƒํ’ˆ๋‚ด์—ญ ์ •๋ณด๋Š” ์ถœ๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + */ + public String buildReceiptContent() { + StringBuilder result = new StringBuilder(); + result.append(getReceiptHeader()); + result.append(toStringOrderDetail()); + if (hasFreeItems()) { + result.append(toStringRemainingItemsForPromotion()); + } + result.append(toStringTotal()); + return result.toString(); + } + + /** + * ์˜์ˆ˜์ฆ ํ—ค๋”๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String getReceiptHeader() { + StringBuilder header = new StringBuilder(); + header.append(RECEIPT_HEADER).append("\n"); + header.append(String.format(PRODUCT_HEADER, PRODUCT_NAME, QUANTITY, PRICE)).append("\n"); + return header.toString(); + } + + /** + * ์ฃผ๋ฌธ ๋‚ด์—ญ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String toStringOrderDetail() { + StringBuilder orderDetail = new StringBuilder(); + + for (CartItem cartItem : cart.getAllItemsInCart()) { + orderDetail.append(String.format(ORDER_DETAIL_FORMAT, cartItem.getProductName(), + numberFormat(cartItem.getQuantity()), numberFormat(cartItem.totalPrice()))) + .append("\n"); + } + return orderDetail.toString(); + } + + /** + * ์ฆ์ • ์ƒํ’ˆ ๋‚ด์—ญ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String toStringRemainingItemsForPromotion() { + StringBuilder remainingitems = new StringBuilder(); + remainingitems.append(PROMOTION_HEADER).append("\n"); + + for (CartItem cartItem : cart.getAllItemsInCart()) { + if (cartItem.getFreeQuantity() > 0) { + remainingitems.append(String.format(PROMOTION_DETAIL_FORMAT, cartItem.getProductName(), + numberFormat(cartItem.getFreeQuantity()))) + .append("\n"); + } + } + return remainingitems.toString(); + } + + /** + * ์ด ๊ธˆ์•ก ๋‚ด์—ญ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String toStringTotal() { + StringBuilder total = new StringBuilder(); + total.append(LINE_SEPARATOR).append("\n"); + + total.append(formatTotalItemPrice()) + .append("\n"); + total.append(formatEventDiscount()) + .append("\n"); + total.append(formatMembershipDiscount()) + .append("\n"); + total.append(formatFinalAmount()) + .append("\n"); + + return total.toString(); + } + + /** + * ์ด ๊ตฌ๋งค ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatTotalItemPrice() { + return String.format(TOTAL_DETAIL_FORMAT, TOTAL_PURCHASE_AMOUNT, numberFormat(cart.getTotalItemPrice())); + } + + /** + * ์ด๋ฒคํŠธ ํ• ์ธ ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatEventDiscount() { + return String.format(TOTAL_DETAIL_FORMAT, EVENT_DISCOUNT, numberFormat(cart.getTotalFreeItemPrice())); + } + + /** + * ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatMembershipDiscount() { + return String.format(TOTAL_DETAIL_FORMAT, MEMBERSHIP_DISCOUNT, numberFormat(membership.getMembershipPrice())); + } + + /** + * ์ตœ์ข… ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatFinalAmount() { + int finalAmount = cart.getTotalItemPrice() - cart.getTotalFreeItemPrice() - membership.getMembershipPrice(); + return String.format(TOTAL_DETAIL_FORMAT, FINAL_AMOUNT, numberFormat(finalAmount)); + } + + /** + * ์ˆซ์ž๋ฅผ ํ•œ๊ตญ์˜ ์ˆซ์ž ํฌ๋งท์— ๋งž๊ฒŒ ํ˜•์‹ํ™”ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String numberFormat(int number) { + return NumberFormat.getInstance(Locale.KOREA).format(number); + } +}
Java
๊ฐœํ–‰ ๋ฌธ์ž๋„ ์ƒ์ˆ˜ํ™”ํ•˜๋ฉด ์–ด๋– ์‹ ๊ฐ€์š”?
@@ -0,0 +1,68 @@ +package store.parser; + +import static store.message.ErrorMessage.INVALID_DATA_FORMAT; + +import java.util.List; +import store.dto.ProductDto; +import store.dto.PromotionDto; + +public class FileReaderParser { + + private static final String COMMA_DELIMITER = ","; + private static final String NULL_PROMOTION = "null"; + private static final int EXPECTED_PRODUCT_LENGTH = 4; + private static final int EXPECTED_PROMOTION_LENGTH = 5; + private static final int HEADER_SKIP_COUNT = 1; + + public List<ProductDto> parseProduct(List<String> productData) { + List<String> productRows = removeHeader(productData); + return productRows.stream() + .map(this::parseProductRow) + .toList(); + } + + public List<PromotionDto> parsePromotion(List<String> promotionData) { + List<String> promotionRows = removeHeader(promotionData); + return promotionRows.stream() + .map(this::parsePromotionRow) + .toList(); + } + + public ProductDto parseProductRow(String row) { + String[] split = row.split(COMMA_DELIMITER); + validateDataLength(split, EXPECTED_PRODUCT_LENGTH); + return createProductDto(split); + } + + public PromotionDto parsePromotionRow(String row) { + String[] split = row.split(COMMA_DELIMITER); + validateDataLength(split, EXPECTED_PROMOTION_LENGTH); + return createPromotionDto(split); + } + + public void validateDataLength(String[] split, int expectedLength) { + if (split.length != expectedLength) { + throw new IllegalStateException(INVALID_DATA_FORMAT.getMessage()); + } + } + + public ProductDto createProductDto(String[] split) { + return ProductDto.toProductDto(split[0], split[1], split[2], normalizePromotion(split[3])); + } + + public PromotionDto createPromotionDto(String[] split) { + return new PromotionDto(split[0], split[1], split[2], split[3], split[4]); + } + + public String normalizePromotion(String promotion) { + if (NULL_PROMOTION.equals(promotion)) { + return null; + } + return promotion; + } + + public List<String> removeHeader(List<String> rows) { + return rows.stream().skip(HEADER_SKIP_COUNT).toList(); + } + +}
Java
ํŒŒ์„œ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์œ ํ‹ธ ํด๋ž˜์Šค๋กœ ๋”ฐ๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š์œผ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,21 @@ +package store; + +import store.controller.FileReaderController; +import store.controller.StoreController; +import store.domain.Store; +import store.dto.StoreDto; + +public class FrontController { + private final FileReaderController fileReaderController; + private final StoreController storeController; + + public FrontController() { + this.fileReaderController = new FileReaderController(); + this.storeController = new StoreController(); + } + + public void run() { + StoreDto storeDto = fileReaderController.runFileData(); + storeController.run(storeDto); + } +}
Java
frontController๋ฅผ ์ด์šฉํ•ด์„œ ํŒŒ์ผ ๋จผ์ € ์ž…๋ ฅ์„ ๋ฐ›์œผ์…จ๊ตฐ์š” ์ข‹์€๊ฒƒ ๊ฐ™์•„์š”! ๐Ÿ‘
@@ -0,0 +1,136 @@ +package store.domain; + +import static store.constants.ProductConstants.OUT_OF_STOCK; +import static store.constants.ProductConstants.PRODUCT_DESCRIPTION_PREFIX; +import static store.constants.ProductConstants.UNIT_QUANTITY; +import static store.constants.ProductConstants.UNIT_WON; +import static store.message.ErrorMessage.NOT_FOUND_PROMOTION; + +import java.text.NumberFormat; +import java.util.Locale; +import java.util.Optional; +import store.dto.ProductDto; +import store.validation.ProductValidator; + +public class Product { + private String name; + private int price; + private int quantity; + private Optional<Promotion> promotion; + + private Product() { + } + + public Product(ProductDto productDto, Optional<Promotion> promotion) { + ProductValidator.validate(productDto); + this.name = productDto.name(); + this.price = Integer.parseInt(productDto.price()); + this.quantity = Integer.parseInt(productDto.quantity()); + this.promotion = promotion; + } + + /** + * ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉ ์—ฌ๋ถ€ ๊ฒ€์ฆ + */ + public boolean isEligibleForStandardPromotion(int orderedQuantity) { + if (promotion.isEmpty()) { + return false; + } + Promotion promotionInfo = promotion.get(); + return orderedQuantity <= quantity + && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity; + } + + /** + * ๋ณด๋„ˆ์Šค ์ƒํ’ˆ์ด ์ ์šฉ ์—ฌ๋ถ€ ๊ฒ€์ฆ + */ + public boolean isEligibleForBonusProduct(int orderedQuantity) { + if (promotion.isEmpty()) { + return false; + } + Promotion promotionInfo = promotion.get(); + return getRemainingItemsForPromotion(orderedQuantity, promotionInfo) >= promotionInfo.getBuy() + && orderedQuantity + promotionInfo.getGet() <= quantity; + } + + /** + * ํ”„๋กœ๋ชจ์…˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋‚จ์€ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int getRemainingItemsForPromotion(int orderedQuantity, Promotion promotionInfo) { + return orderedQuantity % promotionInfo.getTotalRequiredQuantity(); + } + + /** + * ๋ณด๋„ˆ์Šค ์ƒํ’ˆ์˜ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int calculateBonusQuantity(int orderedQuantity, Promotion promotion) { + return (orderedQuantity / promotion.getTotalRequiredQuantity()) * promotion.getGet(); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ํ›„, ์‹ค์ œ ์ฃผ๋ฌธ ๊ฐ€๋Šฅํ•œ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int calculateQuantityAfterPromotion(int orderedQuantity) { + Promotion promotionInfo = getPromotionOrElseThrow(); + int requiredQuantity = promotionInfo.getTotalRequiredQuantity(); + return (orderedQuantity - quantity) + (quantity % requiredQuantity); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ + */ + public Promotion getPromotionOrElseThrow() { + return promotion.orElseThrow(() -> new IllegalArgumentException(NOT_FOUND_PROMOTION.getMessage())); + } + + /** + * ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฃผ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒํ’ˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ํฌ๋งท์„ ํ•ฉ๋‹ˆ๋‹ค. + */ + @Override + public String toString() { + return PRODUCT_DESCRIPTION_PREFIX + + name + " " + + getFormatKoreanLocale(price) + UNIT_WON + + getFormattedQuantity() + + getFormattedPromotion(); + } + + public String getFormattedQuantity() { + if (quantity < 1) { + return OUT_OF_STOCK; + } + return getFormatKoreanLocale(quantity) + UNIT_QUANTITY; + } + + public String getFormatKoreanLocale(int number) { + return NumberFormat.getInstance(Locale.KOREA).format(number); + } + + public String getFormattedPromotion() { + return promotion.map(Promotion::getName).orElse(""); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public Optional<Promotion> getPromotion() { + return promotion; + } + + public boolean isPromotionalProduct() { + return promotion.isPresent(); + } + + public void decreaseQuantity(int orderQuantity) { + this.quantity -= orderQuantity; + } +}
Java
์ด๋ฆ„์€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ์ด ์—†๊ธฐ๋•Œ๋ฌธ์— final๋กœ ์„ ์–ธํ•ด์ฃผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,136 @@ +package store.domain; + +import static store.constants.ProductConstants.OUT_OF_STOCK; +import static store.constants.ProductConstants.PRODUCT_DESCRIPTION_PREFIX; +import static store.constants.ProductConstants.UNIT_QUANTITY; +import static store.constants.ProductConstants.UNIT_WON; +import static store.message.ErrorMessage.NOT_FOUND_PROMOTION; + +import java.text.NumberFormat; +import java.util.Locale; +import java.util.Optional; +import store.dto.ProductDto; +import store.validation.ProductValidator; + +public class Product { + private String name; + private int price; + private int quantity; + private Optional<Promotion> promotion; + + private Product() { + } + + public Product(ProductDto productDto, Optional<Promotion> promotion) { + ProductValidator.validate(productDto); + this.name = productDto.name(); + this.price = Integer.parseInt(productDto.price()); + this.quantity = Integer.parseInt(productDto.quantity()); + this.promotion = promotion; + } + + /** + * ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉ ์—ฌ๋ถ€ ๊ฒ€์ฆ + */ + public boolean isEligibleForStandardPromotion(int orderedQuantity) { + if (promotion.isEmpty()) { + return false; + } + Promotion promotionInfo = promotion.get(); + return orderedQuantity <= quantity + && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity; + } + + /** + * ๋ณด๋„ˆ์Šค ์ƒํ’ˆ์ด ์ ์šฉ ์—ฌ๋ถ€ ๊ฒ€์ฆ + */ + public boolean isEligibleForBonusProduct(int orderedQuantity) { + if (promotion.isEmpty()) { + return false; + } + Promotion promotionInfo = promotion.get(); + return getRemainingItemsForPromotion(orderedQuantity, promotionInfo) >= promotionInfo.getBuy() + && orderedQuantity + promotionInfo.getGet() <= quantity; + } + + /** + * ํ”„๋กœ๋ชจ์…˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋‚จ์€ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int getRemainingItemsForPromotion(int orderedQuantity, Promotion promotionInfo) { + return orderedQuantity % promotionInfo.getTotalRequiredQuantity(); + } + + /** + * ๋ณด๋„ˆ์Šค ์ƒํ’ˆ์˜ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int calculateBonusQuantity(int orderedQuantity, Promotion promotion) { + return (orderedQuantity / promotion.getTotalRequiredQuantity()) * promotion.getGet(); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ํ›„, ์‹ค์ œ ์ฃผ๋ฌธ ๊ฐ€๋Šฅํ•œ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int calculateQuantityAfterPromotion(int orderedQuantity) { + Promotion promotionInfo = getPromotionOrElseThrow(); + int requiredQuantity = promotionInfo.getTotalRequiredQuantity(); + return (orderedQuantity - quantity) + (quantity % requiredQuantity); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ + */ + public Promotion getPromotionOrElseThrow() { + return promotion.orElseThrow(() -> new IllegalArgumentException(NOT_FOUND_PROMOTION.getMessage())); + } + + /** + * ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฃผ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒํ’ˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ํฌ๋งท์„ ํ•ฉ๋‹ˆ๋‹ค. + */ + @Override + public String toString() { + return PRODUCT_DESCRIPTION_PREFIX + + name + " " + + getFormatKoreanLocale(price) + UNIT_WON + + getFormattedQuantity() + + getFormattedPromotion(); + } + + public String getFormattedQuantity() { + if (quantity < 1) { + return OUT_OF_STOCK; + } + return getFormatKoreanLocale(quantity) + UNIT_QUANTITY; + } + + public String getFormatKoreanLocale(int number) { + return NumberFormat.getInstance(Locale.KOREA).format(number); + } + + public String getFormattedPromotion() { + return promotion.map(Promotion::getName).orElse(""); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public Optional<Promotion> getPromotion() { + return promotion; + } + + public boolean isPromotionalProduct() { + return promotion.isPresent(); + } + + public void decreaseQuantity(int orderQuantity) { + this.quantity -= orderQuantity; + } +}
Java
Optional ์ข‹๋„ค์š” ๐Ÿ‘
@@ -0,0 +1,136 @@ +package store.domain; + +import static store.constants.ProductConstants.OUT_OF_STOCK; +import static store.constants.ProductConstants.PRODUCT_DESCRIPTION_PREFIX; +import static store.constants.ProductConstants.UNIT_QUANTITY; +import static store.constants.ProductConstants.UNIT_WON; +import static store.message.ErrorMessage.NOT_FOUND_PROMOTION; + +import java.text.NumberFormat; +import java.util.Locale; +import java.util.Optional; +import store.dto.ProductDto; +import store.validation.ProductValidator; + +public class Product { + private String name; + private int price; + private int quantity; + private Optional<Promotion> promotion; + + private Product() { + } + + public Product(ProductDto productDto, Optional<Promotion> promotion) { + ProductValidator.validate(productDto); + this.name = productDto.name(); + this.price = Integer.parseInt(productDto.price()); + this.quantity = Integer.parseInt(productDto.quantity()); + this.promotion = promotion; + } + + /** + * ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉ ์—ฌ๋ถ€ ๊ฒ€์ฆ + */ + public boolean isEligibleForStandardPromotion(int orderedQuantity) { + if (promotion.isEmpty()) { + return false; + } + Promotion promotionInfo = promotion.get(); + return orderedQuantity <= quantity + && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity; + } + + /** + * ๋ณด๋„ˆ์Šค ์ƒํ’ˆ์ด ์ ์šฉ ์—ฌ๋ถ€ ๊ฒ€์ฆ + */ + public boolean isEligibleForBonusProduct(int orderedQuantity) { + if (promotion.isEmpty()) { + return false; + } + Promotion promotionInfo = promotion.get(); + return getRemainingItemsForPromotion(orderedQuantity, promotionInfo) >= promotionInfo.getBuy() + && orderedQuantity + promotionInfo.getGet() <= quantity; + } + + /** + * ํ”„๋กœ๋ชจ์…˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋‚จ์€ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int getRemainingItemsForPromotion(int orderedQuantity, Promotion promotionInfo) { + return orderedQuantity % promotionInfo.getTotalRequiredQuantity(); + } + + /** + * ๋ณด๋„ˆ์Šค ์ƒํ’ˆ์˜ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int calculateBonusQuantity(int orderedQuantity, Promotion promotion) { + return (orderedQuantity / promotion.getTotalRequiredQuantity()) * promotion.getGet(); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ํ›„, ์‹ค์ œ ์ฃผ๋ฌธ ๊ฐ€๋Šฅํ•œ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int calculateQuantityAfterPromotion(int orderedQuantity) { + Promotion promotionInfo = getPromotionOrElseThrow(); + int requiredQuantity = promotionInfo.getTotalRequiredQuantity(); + return (orderedQuantity - quantity) + (quantity % requiredQuantity); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ + */ + public Promotion getPromotionOrElseThrow() { + return promotion.orElseThrow(() -> new IllegalArgumentException(NOT_FOUND_PROMOTION.getMessage())); + } + + /** + * ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฃผ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒํ’ˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ํฌ๋งท์„ ํ•ฉ๋‹ˆ๋‹ค. + */ + @Override + public String toString() { + return PRODUCT_DESCRIPTION_PREFIX + + name + " " + + getFormatKoreanLocale(price) + UNIT_WON + + getFormattedQuantity() + + getFormattedPromotion(); + } + + public String getFormattedQuantity() { + if (quantity < 1) { + return OUT_OF_STOCK; + } + return getFormatKoreanLocale(quantity) + UNIT_QUANTITY; + } + + public String getFormatKoreanLocale(int number) { + return NumberFormat.getInstance(Locale.KOREA).format(number); + } + + public String getFormattedPromotion() { + return promotion.map(Promotion::getName).orElse(""); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public Optional<Promotion> getPromotion() { + return promotion; + } + + public boolean isPromotionalProduct() { + return promotion.isPresent(); + } + + public void decreaseQuantity(int orderQuantity) { + this.quantity -= orderQuantity; + } +}
Java
Model์—์„œ DTO๋ฅผ ์˜์กดํ•˜๋ฉด View์— ์˜์กดํ•˜๋Š”๊ฒƒ๊ณผ ๋น„์Šทํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. DTO์˜ ๋ณ€๊ฒฝ์— Model์˜ ์ฝ”๋“œ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด์•ผํ•ด์š”! Service์—์„œ DTO๊ด€๋ จ๋œ ๋กœ์ง์„ ์ „์ฒ˜๋ฆฌ ํ•ด์ฃผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”๐Ÿ˜ƒ
@@ -0,0 +1,136 @@ +package store.domain; + +import static store.constants.ProductConstants.OUT_OF_STOCK; +import static store.constants.ProductConstants.PRODUCT_DESCRIPTION_PREFIX; +import static store.constants.ProductConstants.UNIT_QUANTITY; +import static store.constants.ProductConstants.UNIT_WON; +import static store.message.ErrorMessage.NOT_FOUND_PROMOTION; + +import java.text.NumberFormat; +import java.util.Locale; +import java.util.Optional; +import store.dto.ProductDto; +import store.validation.ProductValidator; + +public class Product { + private String name; + private int price; + private int quantity; + private Optional<Promotion> promotion; + + private Product() { + } + + public Product(ProductDto productDto, Optional<Promotion> promotion) { + ProductValidator.validate(productDto); + this.name = productDto.name(); + this.price = Integer.parseInt(productDto.price()); + this.quantity = Integer.parseInt(productDto.quantity()); + this.promotion = promotion; + } + + /** + * ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉ ์—ฌ๋ถ€ ๊ฒ€์ฆ + */ + public boolean isEligibleForStandardPromotion(int orderedQuantity) { + if (promotion.isEmpty()) { + return false; + } + Promotion promotionInfo = promotion.get(); + return orderedQuantity <= quantity + && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity; + } + + /** + * ๋ณด๋„ˆ์Šค ์ƒํ’ˆ์ด ์ ์šฉ ์—ฌ๋ถ€ ๊ฒ€์ฆ + */ + public boolean isEligibleForBonusProduct(int orderedQuantity) { + if (promotion.isEmpty()) { + return false; + } + Promotion promotionInfo = promotion.get(); + return getRemainingItemsForPromotion(orderedQuantity, promotionInfo) >= promotionInfo.getBuy() + && orderedQuantity + promotionInfo.getGet() <= quantity; + } + + /** + * ํ”„๋กœ๋ชจ์…˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋‚จ์€ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int getRemainingItemsForPromotion(int orderedQuantity, Promotion promotionInfo) { + return orderedQuantity % promotionInfo.getTotalRequiredQuantity(); + } + + /** + * ๋ณด๋„ˆ์Šค ์ƒํ’ˆ์˜ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int calculateBonusQuantity(int orderedQuantity, Promotion promotion) { + return (orderedQuantity / promotion.getTotalRequiredQuantity()) * promotion.getGet(); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ํ›„, ์‹ค์ œ ์ฃผ๋ฌธ ๊ฐ€๋Šฅํ•œ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int calculateQuantityAfterPromotion(int orderedQuantity) { + Promotion promotionInfo = getPromotionOrElseThrow(); + int requiredQuantity = promotionInfo.getTotalRequiredQuantity(); + return (orderedQuantity - quantity) + (quantity % requiredQuantity); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ + */ + public Promotion getPromotionOrElseThrow() { + return promotion.orElseThrow(() -> new IllegalArgumentException(NOT_FOUND_PROMOTION.getMessage())); + } + + /** + * ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฃผ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒํ’ˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ํฌ๋งท์„ ํ•ฉ๋‹ˆ๋‹ค. + */ + @Override + public String toString() { + return PRODUCT_DESCRIPTION_PREFIX + + name + " " + + getFormatKoreanLocale(price) + UNIT_WON + + getFormattedQuantity() + + getFormattedPromotion(); + } + + public String getFormattedQuantity() { + if (quantity < 1) { + return OUT_OF_STOCK; + } + return getFormatKoreanLocale(quantity) + UNIT_QUANTITY; + } + + public String getFormatKoreanLocale(int number) { + return NumberFormat.getInstance(Locale.KOREA).format(number); + } + + public String getFormattedPromotion() { + return promotion.map(Promotion::getName).orElse(""); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public Optional<Promotion> getPromotion() { + return promotion; + } + + public boolean isPromotionalProduct() { + return promotion.isPresent(); + } + + public void decreaseQuantity(int orderQuantity) { + this.quantity -= orderQuantity; + } +}
Java
Optional์˜ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”!? ```suggestion public boolean isEligibleForStandardPromotion(int orderedQuantity) { return promotion.map(promotionInfo -> orderedQuantity <= quantity && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity ).orElse(false); } ```
@@ -0,0 +1,136 @@ +package store.domain; + +import static store.constants.ProductConstants.OUT_OF_STOCK; +import static store.constants.ProductConstants.PRODUCT_DESCRIPTION_PREFIX; +import static store.constants.ProductConstants.UNIT_QUANTITY; +import static store.constants.ProductConstants.UNIT_WON; +import static store.message.ErrorMessage.NOT_FOUND_PROMOTION; + +import java.text.NumberFormat; +import java.util.Locale; +import java.util.Optional; +import store.dto.ProductDto; +import store.validation.ProductValidator; + +public class Product { + private String name; + private int price; + private int quantity; + private Optional<Promotion> promotion; + + private Product() { + } + + public Product(ProductDto productDto, Optional<Promotion> promotion) { + ProductValidator.validate(productDto); + this.name = productDto.name(); + this.price = Integer.parseInt(productDto.price()); + this.quantity = Integer.parseInt(productDto.quantity()); + this.promotion = promotion; + } + + /** + * ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉ ์—ฌ๋ถ€ ๊ฒ€์ฆ + */ + public boolean isEligibleForStandardPromotion(int orderedQuantity) { + if (promotion.isEmpty()) { + return false; + } + Promotion promotionInfo = promotion.get(); + return orderedQuantity <= quantity + && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity; + } + + /** + * ๋ณด๋„ˆ์Šค ์ƒํ’ˆ์ด ์ ์šฉ ์—ฌ๋ถ€ ๊ฒ€์ฆ + */ + public boolean isEligibleForBonusProduct(int orderedQuantity) { + if (promotion.isEmpty()) { + return false; + } + Promotion promotionInfo = promotion.get(); + return getRemainingItemsForPromotion(orderedQuantity, promotionInfo) >= promotionInfo.getBuy() + && orderedQuantity + promotionInfo.getGet() <= quantity; + } + + /** + * ํ”„๋กœ๋ชจ์…˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋‚จ์€ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int getRemainingItemsForPromotion(int orderedQuantity, Promotion promotionInfo) { + return orderedQuantity % promotionInfo.getTotalRequiredQuantity(); + } + + /** + * ๋ณด๋„ˆ์Šค ์ƒํ’ˆ์˜ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int calculateBonusQuantity(int orderedQuantity, Promotion promotion) { + return (orderedQuantity / promotion.getTotalRequiredQuantity()) * promotion.getGet(); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ํ›„, ์‹ค์ œ ์ฃผ๋ฌธ ๊ฐ€๋Šฅํ•œ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐ + */ + public int calculateQuantityAfterPromotion(int orderedQuantity) { + Promotion promotionInfo = getPromotionOrElseThrow(); + int requiredQuantity = promotionInfo.getTotalRequiredQuantity(); + return (orderedQuantity - quantity) + (quantity % requiredQuantity); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ + */ + public Promotion getPromotionOrElseThrow() { + return promotion.orElseThrow(() -> new IllegalArgumentException(NOT_FOUND_PROMOTION.getMessage())); + } + + /** + * ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฃผ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒํ’ˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ํฌ๋งท์„ ํ•ฉ๋‹ˆ๋‹ค. + */ + @Override + public String toString() { + return PRODUCT_DESCRIPTION_PREFIX + + name + " " + + getFormatKoreanLocale(price) + UNIT_WON + + getFormattedQuantity() + + getFormattedPromotion(); + } + + public String getFormattedQuantity() { + if (quantity < 1) { + return OUT_OF_STOCK; + } + return getFormatKoreanLocale(quantity) + UNIT_QUANTITY; + } + + public String getFormatKoreanLocale(int number) { + return NumberFormat.getInstance(Locale.KOREA).format(number); + } + + public String getFormattedPromotion() { + return promotion.map(Promotion::getName).orElse(""); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public Optional<Promotion> getPromotion() { + return promotion; + } + + public boolean isPromotionalProduct() { + return promotion.isPresent(); + } + + public void decreaseQuantity(int orderQuantity) { + this.quantity -= orderQuantity; + } +}
Java
๊ผผ๊ผผํ•จ์ด ๋ณด์ด๋„ค์š” ๐Ÿ‘€
@@ -0,0 +1,152 @@ +package store.domain; + +import static store.constants.ReceiptConstants.EVENT_DISCOUNT; +import static store.constants.ReceiptConstants.FINAL_AMOUNT; +import static store.constants.ReceiptConstants.LINE_SEPARATOR; +import static store.constants.ReceiptConstants.MEMBERSHIP_DISCOUNT; +import static store.constants.ReceiptConstants.ORDER_DETAIL_FORMAT; +import static store.constants.ReceiptConstants.PRICE; +import static store.constants.ReceiptConstants.PRODUCT_HEADER; +import static store.constants.ReceiptConstants.PRODUCT_NAME; +import static store.constants.ReceiptConstants.PROMOTION_DETAIL_FORMAT; +import static store.constants.ReceiptConstants.PROMOTION_HEADER; +import static store.constants.ReceiptConstants.QUANTITY; +import static store.constants.ReceiptConstants.RECEIPT_HEADER; +import static store.constants.ReceiptConstants.TOTAL_DETAIL_FORMAT; +import static store.constants.ReceiptConstants.TOTAL_PURCHASE_AMOUNT; + +import java.text.NumberFormat; +import java.util.Locale; + +public class Receipt { + private final Store store; + private final Cart cart; + private final Membership membership; + + public Receipt(Store store, Cart cart, Membership membership) { + this.store = store; + this.cart = cart; + this.membership = membership; + } + + @Override + public String toString() { + return buildReceiptContent(); + } + + public boolean hasFreeItems() { + return cart.getTotalFreeItemQuantity() > 0; + } + + /** + * ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ฐ ๋ฉค๋ฒ„์‹ญ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์˜์ˆ˜์ฆ ๋‚ด์šฉ์„ ๋นŒ๋“œํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + * <p> + * ์ถ”๊ฐ€๋กœ ์ฆ์ •๋œ ์ƒํ’ˆ์ด ์—†๋‹ค๋ฉด ์ƒํ’ˆ๋‚ด์—ญ ์ •๋ณด๋Š” ์ถœ๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + */ + public String buildReceiptContent() { + StringBuilder result = new StringBuilder(); + result.append(getReceiptHeader()); + result.append(toStringOrderDetail()); + if (hasFreeItems()) { + result.append(toStringRemainingItemsForPromotion()); + } + result.append(toStringTotal()); + return result.toString(); + } + + /** + * ์˜์ˆ˜์ฆ ํ—ค๋”๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String getReceiptHeader() { + StringBuilder header = new StringBuilder(); + header.append(RECEIPT_HEADER).append("\n"); + header.append(String.format(PRODUCT_HEADER, PRODUCT_NAME, QUANTITY, PRICE)).append("\n"); + return header.toString(); + } + + /** + * ์ฃผ๋ฌธ ๋‚ด์—ญ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String toStringOrderDetail() { + StringBuilder orderDetail = new StringBuilder(); + + for (CartItem cartItem : cart.getAllItemsInCart()) { + orderDetail.append(String.format(ORDER_DETAIL_FORMAT, cartItem.getProductName(), + numberFormat(cartItem.getQuantity()), numberFormat(cartItem.totalPrice()))) + .append("\n"); + } + return orderDetail.toString(); + } + + /** + * ์ฆ์ • ์ƒํ’ˆ ๋‚ด์—ญ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String toStringRemainingItemsForPromotion() { + StringBuilder remainingitems = new StringBuilder(); + remainingitems.append(PROMOTION_HEADER).append("\n"); + + for (CartItem cartItem : cart.getAllItemsInCart()) { + if (cartItem.getFreeQuantity() > 0) { + remainingitems.append(String.format(PROMOTION_DETAIL_FORMAT, cartItem.getProductName(), + numberFormat(cartItem.getFreeQuantity()))) + .append("\n"); + } + } + return remainingitems.toString(); + } + + /** + * ์ด ๊ธˆ์•ก ๋‚ด์—ญ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String toStringTotal() { + StringBuilder total = new StringBuilder(); + total.append(LINE_SEPARATOR).append("\n"); + + total.append(formatTotalItemPrice()) + .append("\n"); + total.append(formatEventDiscount()) + .append("\n"); + total.append(formatMembershipDiscount()) + .append("\n"); + total.append(formatFinalAmount()) + .append("\n"); + + return total.toString(); + } + + /** + * ์ด ๊ตฌ๋งค ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatTotalItemPrice() { + return String.format(TOTAL_DETAIL_FORMAT, TOTAL_PURCHASE_AMOUNT, numberFormat(cart.getTotalItemPrice())); + } + + /** + * ์ด๋ฒคํŠธ ํ• ์ธ ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatEventDiscount() { + return String.format(TOTAL_DETAIL_FORMAT, EVENT_DISCOUNT, numberFormat(cart.getTotalFreeItemPrice())); + } + + /** + * ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatMembershipDiscount() { + return String.format(TOTAL_DETAIL_FORMAT, MEMBERSHIP_DISCOUNT, numberFormat(membership.getMembershipPrice())); + } + + /** + * ์ตœ์ข… ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatFinalAmount() { + int finalAmount = cart.getTotalItemPrice() - cart.getTotalFreeItemPrice() - membership.getMembershipPrice(); + return String.format(TOTAL_DETAIL_FORMAT, FINAL_AMOUNT, numberFormat(finalAmount)); + } + + /** + * ์ˆซ์ž๋ฅผ ํ•œ๊ตญ์˜ ์ˆซ์ž ํฌ๋งท์— ๋งž๊ฒŒ ํ˜•์‹ํ™”ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String numberFormat(int number) { + return NumberFormat.getInstance(Locale.KOREA).format(number); + } +}
Java
Model๋ ˆ์ด์–ด์—์„œ View์˜ ๋‚ด์šฉ์„ ๊ฐ€๊ณตํ•˜๊ธฐ ๋ณด๋‹จ, Controller or Service์—์„œ ์ง„ํ–‰ํ•ด์ฃผ๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”? ์ง€๊ธˆ์€ View๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด Model๋„ ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ• ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,152 @@ +package store.domain; + +import static store.constants.ReceiptConstants.EVENT_DISCOUNT; +import static store.constants.ReceiptConstants.FINAL_AMOUNT; +import static store.constants.ReceiptConstants.LINE_SEPARATOR; +import static store.constants.ReceiptConstants.MEMBERSHIP_DISCOUNT; +import static store.constants.ReceiptConstants.ORDER_DETAIL_FORMAT; +import static store.constants.ReceiptConstants.PRICE; +import static store.constants.ReceiptConstants.PRODUCT_HEADER; +import static store.constants.ReceiptConstants.PRODUCT_NAME; +import static store.constants.ReceiptConstants.PROMOTION_DETAIL_FORMAT; +import static store.constants.ReceiptConstants.PROMOTION_HEADER; +import static store.constants.ReceiptConstants.QUANTITY; +import static store.constants.ReceiptConstants.RECEIPT_HEADER; +import static store.constants.ReceiptConstants.TOTAL_DETAIL_FORMAT; +import static store.constants.ReceiptConstants.TOTAL_PURCHASE_AMOUNT; + +import java.text.NumberFormat; +import java.util.Locale; + +public class Receipt { + private final Store store; + private final Cart cart; + private final Membership membership; + + public Receipt(Store store, Cart cart, Membership membership) { + this.store = store; + this.cart = cart; + this.membership = membership; + } + + @Override + public String toString() { + return buildReceiptContent(); + } + + public boolean hasFreeItems() { + return cart.getTotalFreeItemQuantity() > 0; + } + + /** + * ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ฐ ๋ฉค๋ฒ„์‹ญ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์˜์ˆ˜์ฆ ๋‚ด์šฉ์„ ๋นŒ๋“œํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + * <p> + * ์ถ”๊ฐ€๋กœ ์ฆ์ •๋œ ์ƒํ’ˆ์ด ์—†๋‹ค๋ฉด ์ƒํ’ˆ๋‚ด์—ญ ์ •๋ณด๋Š” ์ถœ๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + */ + public String buildReceiptContent() { + StringBuilder result = new StringBuilder(); + result.append(getReceiptHeader()); + result.append(toStringOrderDetail()); + if (hasFreeItems()) { + result.append(toStringRemainingItemsForPromotion()); + } + result.append(toStringTotal()); + return result.toString(); + } + + /** + * ์˜์ˆ˜์ฆ ํ—ค๋”๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String getReceiptHeader() { + StringBuilder header = new StringBuilder(); + header.append(RECEIPT_HEADER).append("\n"); + header.append(String.format(PRODUCT_HEADER, PRODUCT_NAME, QUANTITY, PRICE)).append("\n"); + return header.toString(); + } + + /** + * ์ฃผ๋ฌธ ๋‚ด์—ญ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String toStringOrderDetail() { + StringBuilder orderDetail = new StringBuilder(); + + for (CartItem cartItem : cart.getAllItemsInCart()) { + orderDetail.append(String.format(ORDER_DETAIL_FORMAT, cartItem.getProductName(), + numberFormat(cartItem.getQuantity()), numberFormat(cartItem.totalPrice()))) + .append("\n"); + } + return orderDetail.toString(); + } + + /** + * ์ฆ์ • ์ƒํ’ˆ ๋‚ด์—ญ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String toStringRemainingItemsForPromotion() { + StringBuilder remainingitems = new StringBuilder(); + remainingitems.append(PROMOTION_HEADER).append("\n"); + + for (CartItem cartItem : cart.getAllItemsInCart()) { + if (cartItem.getFreeQuantity() > 0) { + remainingitems.append(String.format(PROMOTION_DETAIL_FORMAT, cartItem.getProductName(), + numberFormat(cartItem.getFreeQuantity()))) + .append("\n"); + } + } + return remainingitems.toString(); + } + + /** + * ์ด ๊ธˆ์•ก ๋‚ด์—ญ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String toStringTotal() { + StringBuilder total = new StringBuilder(); + total.append(LINE_SEPARATOR).append("\n"); + + total.append(formatTotalItemPrice()) + .append("\n"); + total.append(formatEventDiscount()) + .append("\n"); + total.append(formatMembershipDiscount()) + .append("\n"); + total.append(formatFinalAmount()) + .append("\n"); + + return total.toString(); + } + + /** + * ์ด ๊ตฌ๋งค ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatTotalItemPrice() { + return String.format(TOTAL_DETAIL_FORMAT, TOTAL_PURCHASE_AMOUNT, numberFormat(cart.getTotalItemPrice())); + } + + /** + * ์ด๋ฒคํŠธ ํ• ์ธ ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatEventDiscount() { + return String.format(TOTAL_DETAIL_FORMAT, EVENT_DISCOUNT, numberFormat(cart.getTotalFreeItemPrice())); + } + + /** + * ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatMembershipDiscount() { + return String.format(TOTAL_DETAIL_FORMAT, MEMBERSHIP_DISCOUNT, numberFormat(membership.getMembershipPrice())); + } + + /** + * ์ตœ์ข… ๊ธˆ์•ก์„ ํฌ๋งทํŒ…ํ•˜์—ฌ ๋ฐ˜ํ™˜ + */ + public String formatFinalAmount() { + int finalAmount = cart.getTotalItemPrice() - cart.getTotalFreeItemPrice() - membership.getMembershipPrice(); + return String.format(TOTAL_DETAIL_FORMAT, FINAL_AMOUNT, numberFormat(finalAmount)); + } + + /** + * ์ˆซ์ž๋ฅผ ํ•œ๊ตญ์˜ ์ˆซ์ž ํฌ๋งท์— ๋งž๊ฒŒ ํ˜•์‹ํ™”ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. + */ + public String numberFormat(int number) { + return NumberFormat.getInstance(Locale.KOREA).format(number); + } +}
Java
ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋„ depth๊ฐ€ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค! ๋ชจ๋“ˆํ™”๋ฅผ ํ•ด๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,68 @@ +package store.parser; + +import static store.message.ErrorMessage.INVALID_DATA_FORMAT; + +import java.util.List; +import store.dto.ProductDto; +import store.dto.PromotionDto; + +public class FileReaderParser { + + private static final String COMMA_DELIMITER = ","; + private static final String NULL_PROMOTION = "null"; + private static final int EXPECTED_PRODUCT_LENGTH = 4; + private static final int EXPECTED_PROMOTION_LENGTH = 5; + private static final int HEADER_SKIP_COUNT = 1; + + public List<ProductDto> parseProduct(List<String> productData) { + List<String> productRows = removeHeader(productData); + return productRows.stream() + .map(this::parseProductRow) + .toList(); + } + + public List<PromotionDto> parsePromotion(List<String> promotionData) { + List<String> promotionRows = removeHeader(promotionData); + return promotionRows.stream() + .map(this::parsePromotionRow) + .toList(); + } + + public ProductDto parseProductRow(String row) { + String[] split = row.split(COMMA_DELIMITER); + validateDataLength(split, EXPECTED_PRODUCT_LENGTH); + return createProductDto(split); + } + + public PromotionDto parsePromotionRow(String row) { + String[] split = row.split(COMMA_DELIMITER); + validateDataLength(split, EXPECTED_PROMOTION_LENGTH); + return createPromotionDto(split); + } + + public void validateDataLength(String[] split, int expectedLength) { + if (split.length != expectedLength) { + throw new IllegalStateException(INVALID_DATA_FORMAT.getMessage()); + } + } + + public ProductDto createProductDto(String[] split) { + return ProductDto.toProductDto(split[0], split[1], split[2], normalizePromotion(split[3])); + } + + public PromotionDto createPromotionDto(String[] split) { + return new PromotionDto(split[0], split[1], split[2], split[3], split[4]); + } + + public String normalizePromotion(String promotion) { + if (NULL_PROMOTION.equals(promotion)) { + return null; + } + return promotion; + } + + public List<String> removeHeader(List<String> rows) { + return rows.stream().skip(HEADER_SKIP_COUNT).toList(); + } + +}
Java
๋งค์ง ๋„˜๋ฒ„๋ฅผ ์ƒ์ˆ˜ํ™” ํ•ด์ฃผ๋ฉด ์ข‹์„๊ฒƒ๊ฐ™๋„ค์š”๐Ÿ˜ƒ
@@ -0,0 +1,153 @@ +package store.parser; + +import static store.message.ErrorMessage.INVALID_INPUT_FORMAT_ERROR; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import store.dto.CartItemDto; + +public class InputParser { + private static final String ITEM_SEPARATOR_REGEX = "],\\["; + private static final char OPEN_BRACKET = '['; + private static final char CLOSE_BRACKET = ']'; + private static final String OPEN_BRACKET_STR = "["; + private static final String CLOSE_BRACKET_STR = "]"; + private static final String NESTED_BRACKET_ERROR_REGEX = "\\[\\["; + private static final String NESTED_BRACKET_CLOSE_ERROR_REGEX = "]]"; + private static final String ITEM_FORMAT_REGEX = "[^\\-]+-\\d+"; + private static final String QUANTITY_SEPARATOR = "-"; + private static final int MIN_ORDER_ITEMS_INPUT_LENGTH = 5; + + private static final char YES = 'Y'; + private static final char NO = 'N'; + private static final int YES_OR_NO_INPUT_LENGTH = 1; + + public List<CartItemDto> parseOrderItems(String input) { + validateOrderItems(input); + String[] items = splitItems(input); + List<CartItemDto> cartItemDtos = new ArrayList<>(); + for (String item : items) { + cartItemDtos.add(convertToOrderItemDtos(item)); + } + validateDuplicateItemName(cartItemDtos); + return cartItemDtos; + } + + public boolean parseYesOrNo(String input) { + validateYesOrNo(input); + return input.charAt(YES_OR_NO_INPUT_LENGTH - 1) == YES; + } + + public void validateOrderItems(String input) { + validateInputLength(input); + validateBrackets(input); + } + + public void validateYesOrNo(String input) { + validateEmpty(input); + validateYNLength(input); + validateUppercase(input); + validateYN(input); + } + + private void validateUppercase(String input) { + if (!Character.isUpperCase(input.charAt(0))) { + exception(); + } + } + + private void validateDuplicateItemName(List<CartItemDto> cartItemDtos) { + Set<String> productNames = new HashSet<>(); + for (CartItemDto cartItemDto : cartItemDtos) { + if (!productNames.add(cartItemDto.productName())) { + exception(); + } + } + } + + private void validateYN(String input) { + char YN = input.charAt(YES_OR_NO_INPUT_LENGTH - 1); + if (YN == YES || YN == NO) { + return; + } + exception(); + } + + private String[] splitItems(String input) { + String cleanInput = input.substring(1, input.length() - 1); + return cleanInput.split(ITEM_SEPARATOR_REGEX); + } + + private void validateInputLength(String input) { + if (input.length() < MIN_ORDER_ITEMS_INPUT_LENGTH) { + exception(); + } + } + + private void validateEmpty(String input) { + if (input == null || input.isEmpty()) { + exception(); + } + } + + private void validateYNLength(String input) { + if (input.length() != YES_OR_NO_INPUT_LENGTH) { + exception(); + } + } + + private void validateBrackets(String input) { + checkBracketsBalance(input); + checkNestedBrackets(input); + checkBracketStructure(input); + } + + private void checkBracketsBalance(String input) { + int openBrackets = countOpenBrackets(input); + if (openBrackets != 0) { + exception(); + } + } + + private int countOpenBrackets(String input) { + int openBrackets = 0; + for (char c : input.toCharArray()) { + if (c == OPEN_BRACKET) { + openBrackets++; + } + if (c == CLOSE_BRACKET) { + openBrackets--; + } + } + return openBrackets; + } + + + private void checkNestedBrackets(String input) { + if (input.contains(NESTED_BRACKET_ERROR_REGEX) || + input.contains(NESTED_BRACKET_CLOSE_ERROR_REGEX)) { + exception(); + } + } + + private void checkBracketStructure(String input) { + if (!(input.startsWith(OPEN_BRACKET_STR) && input.endsWith( + CLOSE_BRACKET_STR))) { + exception(); + } + } + + private CartItemDto convertToOrderItemDtos(String item) { + if (!item.matches(ITEM_FORMAT_REGEX)) { + exception(); + } + String[] itemParts = item.split(QUANTITY_SEPARATOR); + return new CartItemDto(itemParts[0], Integer.parseInt(itemParts[1])); + } + + private void exception() { + throw new IllegalArgumentException(INVALID_INPUT_FORMAT_ERROR.getMessage()); + } +}
Java
confirm๊ณผ ๊ฐ™์€ ๋ณ€์ˆ˜๋ช…์€ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,128 @@ +package store.parser; + +import static store.constants.PromotionConstants.NO_PROMOTION_SUFFIX; +import static store.constants.PromotionConstants.PROMOTION_SUFFIX; +import static store.message.ErrorMessage.EMPTY_DATA; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import store.domain.Product; +import store.domain.Promotion; +import store.dto.ProductDto; +import store.util.PromotionUtil; + +public class ProductParser { + + private final PromotionUtil promotionUtil; + + public ProductParser() { + this.promotionUtil = new PromotionUtil(); + } + + public Map<String, Product> parse(List<ProductDto> productDtos, List<Promotion> availablePromotions) { + validateProductDtos(productDtos); + return groupAndConvertProductDtos(productDtos, availablePromotions); + } + + private Map<String, Product> groupAndConvertProductDtos(List<ProductDto> productDtos, + List<Promotion> availablePromotions) { + Map<String, List<ProductDto>> groupedProductDtos = groupProductDtosByProductName(productDtos); + applyDefaultPromotionForSingleProduct(groupedProductDtos); + return convertGroupedProductDtosToProducts(groupedProductDtos, availablePromotions); + } + + /** + * ์ฃผ์–ด์ง„ ๊ทธ๋ฃนํ™”๋œ ์ œํ’ˆ DTO ๋ชฉ๋ก์„ ๊ธฐ๋ฐ˜์œผ๋กœ, ํ”„๋กœ๋ชจ์…˜์„ ๊ณ ๋ คํ•˜์—ฌ ์ œํ’ˆ ๋งต์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. + */ + private Map<String, Product> convertGroupedProductDtosToProducts(Map<String, List<ProductDto>> groupedProductDtos, + List<Promotion> availablePromotions) { + Map<String, Product> productMap = new LinkedHashMap<>(); + for (Entry<String, List<ProductDto>> entry : groupedProductDtos.entrySet()) { + for (ProductDto productDto : entry.getValue()) { + if (addProductWithoutPromotion(availablePromotions, entry, productDto, productMap)) { + continue; + } + addProductWithPromotion(availablePromotions, entry, productDto, productMap); + } + } + return productMap; + } + + /** + * ์ฃผ์–ด์ง„ ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉํ•˜์—ฌ ์ œํ’ˆ์„ ์ƒ์„ฑํ•œ ํ›„, ์ œํ’ˆ ๋งต์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. + */ + private void addProductWithPromotion(List<Promotion> availablePromotions, Entry<String, List<ProductDto>> entry, + ProductDto productDto, Map<String, Product> productMap) { + productMap.put(entry.getKey() + PROMOTION_SUFFIX, + createProductFromDto(productDto, availablePromotions)); + } + + /** + * ํ”„๋กœ๋ชจ์…˜์ด ์—†๋Š” ์ œํ’ˆ์„ ์ œํ’ˆ ๋งต์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ œํ’ˆ DTO์˜ ํ”„๋กœ๋ชจ์…˜ ํ•„๋“œ๊ฐ€ null์ธ ๊ฒฝ์šฐ์—๋งŒ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. + */ + private boolean addProductWithoutPromotion(List<Promotion> availablePromotions, + Entry<String, List<ProductDto>> entry, + ProductDto productDto, Map<String, Product> productMap) { + if (productDto.promotion() == null) { + productMap.put(entry.getKey() + NO_PROMOTION_SUFFIX, + createProductFromDto(productDto, availablePromotions)); + return true; + } + return false; + } + + /** + * ์ฃผ์–ด์ง„ ์ œํ’ˆ DTO์™€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ”„๋กœ๋ชจ์…˜ ๋ชฉ๋ก์„ ๊ธฐ๋ฐ˜์œผ๋กœ Product ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. + * + * @param productDto ๋ณ€ํ™˜ํ•  ์ œํ’ˆ DTO + * @param availablePromotions ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ”„๋กœ๋ชจ์…˜ ๋ชฉ๋ก + * @return ์ƒ์„ฑ๋œ Product ๊ฐ์ฒด + */ + private Product createProductFromDto(ProductDto productDto, List<Promotion> availablePromotions) { + Promotion promotion = promotionUtil.findMatchingPromotion(productDto.promotion(), availablePromotions); + return new Product(productDto, Optional.ofNullable(promotion)); + } + + /** + * ๊ทธ๋ฃนํ™”๋œ ์ œํ’ˆ DTO ๋ชฉ๋ก์—์„œ ํ”„๋กœ๋ชจ์…˜์ด ์žˆ๋Š” ๋‹จ์ผ ์ œํ’ˆ์— ๊ธฐ๋ณธ ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. + * + * @param groupedProductDtos ์ œํ’ˆ ์ด๋ฆ„์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ™”๋œ ์ œํ’ˆ DTO ๋งต + */ + private void applyDefaultPromotionForSingleProduct(Map<String, List<ProductDto>> groupedProductDtos) { + for (List<ProductDto> productDtos : groupedProductDtos.values()) { + if (isSingleProductWithPromotion(productDtos)) { + productDtos.add(ProductDto.toGeneralProductDto(productDtos.getFirst())); + } + } + } + + /** + * ์ฃผ์–ด์ง„ ์ œํ’ˆ DTO ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋‹จ์ผ ์ œํ’ˆ์ด๊ณ , ํ•ด๋‹น ์ œํ’ˆ์— ํ”„๋กœ๋ชจ์…˜์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. + */ + private boolean isSingleProductWithPromotion(List<ProductDto> productDtos) { + return productDtos.size() == 1 && productDtos.getFirst().promotion() != null; + } + + /** + * ์ œํ’ˆ DTO ๋ชฉ๋ก์„ ์ œํ’ˆ ์ด๋ฆ„์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. + */ + private Map<String, List<ProductDto>> groupProductDtosByProductName(List<ProductDto> productDtos) { + Map<String, List<ProductDto>> groupedProductDtos = new LinkedHashMap<>(); + for (ProductDto productDto : productDtos) { + groupedProductDtos.computeIfAbsent(productDto.name(), k -> new ArrayList<>()) + .add(productDto); + } + return groupedProductDtos; + } + + private void validateProductDtos(List<ProductDto> productDtos) { + if (productDtos.isEmpty()) { + throw new IllegalArgumentException(EMPTY_DATA.getMessage()); + } + } + +}
Java
depth๊ฐ€ ๋‹ค์†Œ ๊นŠ์€ ๊ฒƒ ๊ฐ™์•„์š”! ๋ชจ๋“ˆํ™”๋ฅผ ํ•ด๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,112 @@ +package store.custom.service.editor; + +import java.util.ArrayList; +import java.util.List; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Product; +import store.custom.model.product.Products; + +public class ProductsEditor { + // ์žฌ๊ณ  ๋ชฉ๋ก ์ดˆ๊ธฐ ์„ค์ •: ๋ชฉ๋ก์— ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ ์ผ๋ฐ˜ ์ƒํ’ˆ ์ถ”๊ฐ€ + public static Products inspectProductCatalog(Products originalCatalog) { + List<Product> resultCatalog = new ArrayList<>(); + int currentIndex; + for (currentIndex = 0; currentIndex < originalCatalog.getProductsSize() - 1; currentIndex++) { + currentIndex += inspectProduct(originalCatalog, resultCatalog, currentIndex); + } + addLastProduct(originalCatalog, resultCatalog, currentIndex); + + return new Products(resultCatalog); + } + + private static int inspectProduct(Products original, List<Product> result, int currentIndex) { + Product currentProduct = original.getProductByIndex(currentIndex); + Product nextProduct = original.getProductByIndex(currentIndex + 1); + + if (hasSameNameAndPrice(result, currentProduct, nextProduct)) { + return 1; // ๋‘๊ฐœ์˜ ์ƒํ’ˆ์„ ๊ฒ€์‚ฌ์™„๋ฃŒํ–ˆ์œผ๋ฏ€๋กœ index ์˜ ๊ฐ’ 1 ์ฆ๊ฐ€ + } + + return addProductConsideringPromotion(currentProduct, result); + } + + private static boolean hasSameNameAndPrice(List<Product> result, Product currentProduct, Product nextProduct) { + if (currentProduct.getName().equals(nextProduct.getName()) + && currentProduct.getPrice() == nextProduct.getPrice()) { + result.add(currentProduct); + result.add(nextProduct); + return true; + } + return false; + } + + private static int addProductConsideringPromotion(Product currentProduct, List<Product> result) { + result.add(currentProduct); + + if (currentProduct.getPromotion() != null) { + result.add(createZeroStockProduct(currentProduct)); // ์žฌ๊ณ ๊ฐ€ 0์ธ ์ œํ’ˆ ์ถ”๊ฐ€ + } + return 0; + } + + private static Product createZeroStockProduct(Product product) { + return new Product(product.getName(), product.getPrice(), 0, null); + } + + private static void addLastProduct(Products originalCatalog, List<Product> result, int currentIndex) { + if (currentIndex == originalCatalog.getProductsSize() - 1) { + Product lastProduct = originalCatalog.getProductByIndex(originalCatalog.getProductsSize() - 1); + addProductConsideringPromotion(lastProduct, result); + } + } + + // ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์— ๋”ฐ๋ฅธ ์žฌ๊ณ  ์กฐ์ • + public static void adjustInventoryForOrders(OrderSheet orderSheet, Products productCatalog) { + for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) { + processOrderedProduct(orderedProduct, productCatalog); + } + } + + private static void processOrderedProduct(OrderedProduct orderedProduct, Products productCatalog) { + int remainQuantity = orderedProduct.getQuantity(); + for (Product product : productCatalog.getProducts()) { + if (remainQuantity == 0) { + break; + } + remainQuantity = updateProductQuantity(orderedProduct, product, remainQuantity); + } + } + + private static int updateProductQuantity(OrderedProduct orderedProduct, Product product, int remainQuantity) { + if (product.getName().equals(orderedProduct.getName())) { + if (product.getPromotion() != null) { + return calculateRemainingQuantityWithPromotion(product, remainQuantity); + } + return calculateRemainingQuantityWithoutPromotion(product, remainQuantity); + } + return remainQuantity; + } + + private static int calculateRemainingQuantityWithPromotion(Product product, int remainQuantity) { + int productQuantity = product.getQuantity(); + + if (remainQuantity >= productQuantity) { + product.setQuantity(0); + return remainQuantity - productQuantity; + } + product.setQuantity(productQuantity - remainQuantity); + return 0; + } + + private static int calculateRemainingQuantityWithoutPromotion(Product product, int remainQuantity) { + int productQuantity = product.getQuantity(); + + if (remainQuantity == productQuantity) { + product.setQuantity(0); + return 0; + } + product.setQuantity(productQuantity - remainQuantity); + return 0; + } +} \ No newline at end of file
Java
[์งˆ๋ฌธ] `OrderSheetEditor`์˜ ๊ฒฝ์šฐ์—๋Š” ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ์„ ํƒํ•˜์…จ๋Š”๋ฐ, `ProductsEditor`๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•œ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,35 @@ +package store.custom.model.product; + +public class Product { + private final String name; + private final int price; + private int quantity; + private final String promotion; + + public Product(String name, int price, int quantity, String promotion) { + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotion = promotion; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public String getPromotion() { + return promotion; + } +} \ No newline at end of file
Java
`product`์˜ `promotion` ํ•„๋“œ๊ฐ€ `null` ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์–ด Editor ๋กœ์ง๋“ค์—์„œ `null` ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. `Optional`์„ ์‚ฌ์šฉํ•˜์—ฌ `promotion` ํ•„๋“œ๊ฐ€ ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜, "" ์™€ ๊ฐ™์€ ๊ธฐ๋ณธ ๊ฐ’์„ ๊ฐ€์ง€๋„๋ก ํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,21 @@ +package store.custom.service.filehandler; + +import static store.custom.validator.CustomErrorMessages.FILE_READING_FAIL; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import store.custom.validator.Validator; + +public class FileReader { + public static List<String> run(String filePath) { + Validator.validateFilePath(filePath); + + try { + return Files.readAllLines(Path.of(filePath)); + } catch (IOException e) { + throw new RuntimeException(FILE_READING_FAIL + e); + } + } +} \ No newline at end of file
Java
readAllLines๋ผ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์—ˆ๋„ค์š”! ์ €๋Š” while๋ฌธ์œผ๋กœ ํŒŒ์ผ์˜ ๋๊นŒ์ง€ ์ฝ์–ด์˜ค๋„๋ก ํ–ˆ๋Š”๋ฐ, ์‚ฌ์šฉํ•˜์‹  ๋ฐฉ๋ฒ•์ด ์ฐธ ๊ฐ„๋‹จํ•œ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,112 @@ +package store.custom.service.editor; + +import java.util.ArrayList; +import java.util.List; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Product; +import store.custom.model.product.Products; + +public class ProductsEditor { + // ์žฌ๊ณ  ๋ชฉ๋ก ์ดˆ๊ธฐ ์„ค์ •: ๋ชฉ๋ก์— ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ ์ผ๋ฐ˜ ์ƒํ’ˆ ์ถ”๊ฐ€ + public static Products inspectProductCatalog(Products originalCatalog) { + List<Product> resultCatalog = new ArrayList<>(); + int currentIndex; + for (currentIndex = 0; currentIndex < originalCatalog.getProductsSize() - 1; currentIndex++) { + currentIndex += inspectProduct(originalCatalog, resultCatalog, currentIndex); + } + addLastProduct(originalCatalog, resultCatalog, currentIndex); + + return new Products(resultCatalog); + } + + private static int inspectProduct(Products original, List<Product> result, int currentIndex) { + Product currentProduct = original.getProductByIndex(currentIndex); + Product nextProduct = original.getProductByIndex(currentIndex + 1); + + if (hasSameNameAndPrice(result, currentProduct, nextProduct)) { + return 1; // ๋‘๊ฐœ์˜ ์ƒํ’ˆ์„ ๊ฒ€์‚ฌ์™„๋ฃŒํ–ˆ์œผ๋ฏ€๋กœ index ์˜ ๊ฐ’ 1 ์ฆ๊ฐ€ + } + + return addProductConsideringPromotion(currentProduct, result); + } + + private static boolean hasSameNameAndPrice(List<Product> result, Product currentProduct, Product nextProduct) { + if (currentProduct.getName().equals(nextProduct.getName()) + && currentProduct.getPrice() == nextProduct.getPrice()) { + result.add(currentProduct); + result.add(nextProduct); + return true; + } + return false; + } + + private static int addProductConsideringPromotion(Product currentProduct, List<Product> result) { + result.add(currentProduct); + + if (currentProduct.getPromotion() != null) { + result.add(createZeroStockProduct(currentProduct)); // ์žฌ๊ณ ๊ฐ€ 0์ธ ์ œํ’ˆ ์ถ”๊ฐ€ + } + return 0; + } + + private static Product createZeroStockProduct(Product product) { + return new Product(product.getName(), product.getPrice(), 0, null); + } + + private static void addLastProduct(Products originalCatalog, List<Product> result, int currentIndex) { + if (currentIndex == originalCatalog.getProductsSize() - 1) { + Product lastProduct = originalCatalog.getProductByIndex(originalCatalog.getProductsSize() - 1); + addProductConsideringPromotion(lastProduct, result); + } + } + + // ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์— ๋”ฐ๋ฅธ ์žฌ๊ณ  ์กฐ์ • + public static void adjustInventoryForOrders(OrderSheet orderSheet, Products productCatalog) { + for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) { + processOrderedProduct(orderedProduct, productCatalog); + } + } + + private static void processOrderedProduct(OrderedProduct orderedProduct, Products productCatalog) { + int remainQuantity = orderedProduct.getQuantity(); + for (Product product : productCatalog.getProducts()) { + if (remainQuantity == 0) { + break; + } + remainQuantity = updateProductQuantity(orderedProduct, product, remainQuantity); + } + } + + private static int updateProductQuantity(OrderedProduct orderedProduct, Product product, int remainQuantity) { + if (product.getName().equals(orderedProduct.getName())) { + if (product.getPromotion() != null) { + return calculateRemainingQuantityWithPromotion(product, remainQuantity); + } + return calculateRemainingQuantityWithoutPromotion(product, remainQuantity); + } + return remainQuantity; + } + + private static int calculateRemainingQuantityWithPromotion(Product product, int remainQuantity) { + int productQuantity = product.getQuantity(); + + if (remainQuantity >= productQuantity) { + product.setQuantity(0); + return remainQuantity - productQuantity; + } + product.setQuantity(productQuantity - remainQuantity); + return 0; + } + + private static int calculateRemainingQuantityWithoutPromotion(Product product, int remainQuantity) { + int productQuantity = product.getQuantity(); + + if (remainQuantity == productQuantity) { + product.setQuantity(0); + return 0; + } + product.setQuantity(productQuantity - remainQuantity); + return 0; + } +} \ No newline at end of file
Java
์ €๋Š” ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ ์ผ๋ฐ˜ ์ƒํ’ˆ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜์ง€ ๋ชปํ–ˆ๋Š”๋ฐ, ๊ทธ๋ž˜์„œ ๊ทธ๋Ÿฐ์ง€ ๋” ์ธ์ƒ์ ์ด์—์š”...๐Ÿ˜‚ ๊ผผ๊ผผํ•œ ๋กœ์ง์—์„œ ๋งŽ์ด ๋ฐฐ์›๋‹ˆ๋‹ค.
@@ -0,0 +1,105 @@ +package store.custom.validator; + +import static store.custom.constants.RegexConstants.PRODUCT_ORDER_REGEX; +import static store.custom.constants.StringConstants.RESPONSE_NO; +import static store.custom.constants.StringConstants.RESPONSE_YES; +import static store.custom.validator.CustomErrorMessages.INVALID_FILE_PATH; +import static store.custom.validator.CustomErrorMessages.INVALID_INPUT; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Product; +import store.custom.model.product.Products; + +public class Validator { + // ํŒŒ์ผ ๋ฆฌ๋”๊ธฐ ๊ด€๋ จ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ + public static void validateFilePath(String filePath) { + if (!Files.exists(Path.of(filePath))) { + throw new IllegalArgumentException(INVALID_FILE_PATH + filePath); + } + } + + // ์ž…๋ ฅ ๊ด€๋ จ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ (๊ณตํ†ต) + public static void validateEmptyInput(String input) { + checkNullInput(input); + checkEmptyInput(input); + checkWhitespaceOnlyInput(input); + } + + private static void checkNullInput(String input) { + if (input == null) { + throw new IllegalArgumentException(INVALID_INPUT); + } + } + + private static void checkEmptyInput(String input) { + if (input.isEmpty()) { + throw new IllegalArgumentException(INVALID_INPUT); + } + } + + private static void checkWhitespaceOnlyInput(String input) { + if (input.trim().isEmpty()) { + throw new IllegalArgumentException(INVALID_INPUT); + } + } + + // ์ฃผ๋ฌธ ์ž…๋ ฅ ๊ด€๋ จ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ + public static void validateOrderForm(List<String> orderForms) { + for (String orderForm : orderForms) { + if (!orderForm.matches(PRODUCT_ORDER_REGEX)) { + throw new IllegalArgumentException(CustomErrorMessages.INVALID_ORDER_FORMAT); + } + } + } + + public static void validateOrderSheet(Products products, OrderSheet orderSheet) { + validateOrderedProductsName(products, orderSheet); + validateOrderedProductsQuantity(products, orderSheet); + } + + public static void validateOrderedProductsName(Products products, OrderSheet orderSheet) { + for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) { + if (!isProductNameMatched(products, orderedProduct)) { // ์ œํ’ˆ์ด ์—†์œผ๋ฉด + throw new IllegalArgumentException(CustomErrorMessages.NON_EXISTENT_PRODUCT); + } + } + } + + private static boolean isProductNameMatched(Products products, OrderedProduct orderedProduct) { + for (Product product : products.getProducts()) { + if (product.getName().equals(orderedProduct.getName())) { + return true; // ์ œํ’ˆ์ด ์กด์žฌํ•  ๋•Œ true ๋ฐ˜ํ™˜ + } + } + return false; // ์ œํ’ˆ์ด ์กด์žฌํ•˜์ง€์•Š์„ ๋•Œ false ๋ฐ˜ํ™˜ + } + + public static void validateOrderedProductsQuantity(Products products, OrderSheet orderSheet) { + for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) { + if (orderedProduct.getQuantity() > calculateProductTotalQuantity(products, orderedProduct)) { + throw new IllegalArgumentException(CustomErrorMessages.INSUFFICIENT_STOCK); + } + } + } + + private static int calculateProductTotalQuantity(Products products, OrderedProduct orderedProduct) { + int totalQuantity = 0; + for (Product product : products.getProducts()) { + if (product.getName().equals(orderedProduct.getName())) { + totalQuantity += product.getQuantity(); + } + } + return totalQuantity; + } + + // ์‘๋‹ต ์ž…๋ ฅ ๊ด€๋ จ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ + public static void validateYesOrNoInput(String response) { + if (!response.equals(RESPONSE_YES) && !response.equals(RESPONSE_NO)) { + throw new IllegalArgumentException(INVALID_INPUT); + } + } +} \ No newline at end of file
Java
์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋‹ˆ ์ •๋ง ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฒ˜๋ฆฌ๋˜๋„ค์š”!! ๋งค๋ฒˆ ๋А๋ผ๋Š” ๊ฑฐ์ง€๋งŒ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์„ ์ž˜ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,99 @@ +package store.custom.service.maker; + +import static store.custom.constants.NumberConstants.NOT_FOUND; +import static store.custom.constants.StringConstants.AFTER_PROMOTION_END; +import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START; + +import java.util.ArrayList; +import java.util.List; +import store.custom.model.PromotionInfo; +import store.custom.model.PromotionResult.PromotionResult; +import store.custom.model.PromotionResult.PromotionResults; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Product; +import store.custom.model.product.Products; + +public class PromotionResultMaker { + public PromotionResults createPromotionResults(Products products, OrderSheet orderSheet) { + List<PromotionResult> results = new ArrayList<>(); + + for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) { + PromotionInfo promotionInfo = createPromotionInfo(products.getProducts(), orderProduct); + PromotionResult result = calculateResult(orderProduct, promotionInfo); + results.add(result); + } + return new PromotionResults(results); + } + + // ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์— ๋”ฐ๋ฅธ ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ํšŸ์ˆ˜ ์ƒ์„ฑ + private PromotionInfo createPromotionInfo(List<Product> products, OrderedProduct orderProduct) { + int promotionQuantity = quantityWithPromotion(products, orderProduct.getName()); + int orderQuantity = orderProduct.getQuantity(); + if (isPromotionNull(orderProduct)) { + return new PromotionInfo(promotionQuantity, NOT_FOUND, orderQuantity, NOT_FOUND, NOT_FOUND); + } + int promotionConditions = orderProduct.getBuy() + orderProduct.getGet(); + return new PromotionInfo(promotionQuantity, promotionConditions, orderQuantity, + orderQuantity / promotionConditions, orderQuantity % promotionConditions); + } + + private int quantityWithPromotion(List<Product> products, String productName) { + int quantity = 0; + for (Product product : products) { + if (product.getName().equals(productName) && product.getPromotion() != null) { + quantity = product.getQuantity(); + } + } + return quantity; + } + + private boolean isPromotionNull(OrderedProduct orderProduct) { + String promotion = orderProduct.getPromotion(); + return (promotion == null || promotion.equals(BEFORE_PROMOTION_START) || promotion.equals(AFTER_PROMOTION_END)); + } + + // ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ๊ฒฐ๊ณผ ๊ณ„์‚ฐ + private PromotionResult calculateResult(OrderedProduct orderProduct, PromotionInfo promotionInfo) { + if (promotionInfo.getRemainder() == NOT_FOUND) { // ํ”„๋กœ๋ชจ์…˜์ด ์—†๋Š” ๊ฒฝ์šฐ + return new PromotionResult(NOT_FOUND, NOT_FOUND, NOT_FOUND); + } + return hasPromotion(orderProduct, promotionInfo); + } + + private PromotionResult hasPromotion(OrderedProduct orderProduct, PromotionInfo promotionInfo) { + if (promotionInfo.getRemainder() == 0) { + return applyFullPromotionConditions(promotionInfo); + } + if (promotionInfo.getRemainder() < orderProduct.getBuy()) { + return applyPartialPromotionConditions(promotionInfo); + } + return applyAdditionalPromotionConditions(promotionInfo); + } + + private PromotionResult applyFullPromotionConditions(PromotionInfo promotionInfo) { + if (promotionInfo.getOrderQuantity() > promotionInfo.getPromotionQuantity()) { + int validPromotionCount = promotionInfo.getPromotionQuantity() / promotionInfo.getConditions(); + int excludedCount = (promotionInfo.getQuotient() - validPromotionCount) * promotionInfo.getConditions(); + return new PromotionResult(validPromotionCount, 0, excludedCount); + } + // if (promotionInfo.getOrderQuantity() <= promotionInfo.getPromotionQuantity()) + return new PromotionResult(promotionInfo.getQuotient(), 0, 0); + } + + private PromotionResult applyPartialPromotionConditions(PromotionInfo promotionInfo) { + if (promotionInfo.getConditions() * promotionInfo.getQuotient() > promotionInfo.getPromotionQuantity()) { + int validPromotionCount = promotionInfo.getPromotionQuantity() / promotionInfo.getConditions(); + int excludedCount = (promotionInfo.getQuotient() - validPromotionCount) * promotionInfo.getConditions(); + return new PromotionResult(validPromotionCount, 0, excludedCount + promotionInfo.getRemainder()); + } + return new PromotionResult(promotionInfo.getQuotient(), 0, promotionInfo.getRemainder()); + } + + private PromotionResult applyAdditionalPromotionConditions(PromotionInfo promotionInfo) { + if (promotionInfo.getConditions() * (promotionInfo.getQuotient() + 1) > promotionInfo.getPromotionQuantity()) { + applyPartialPromotionConditions(promotionInfo); + } + return new PromotionResult(promotionInfo.getQuotient(), 1, 0); + } +} \ No newline at end of file
Java
์•— ๋ฉ”์„œ๋“œ๋ช… ์—ฌ๊ธฐ๋‘์š”...!
@@ -0,0 +1,153 @@ +package store.custom.service.editor; + +import static store.custom.constants.StringConstants.AFTER_PROMOTION_END; +import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START; +import static store.custom.constants.StringConstants.RESPONSE_NO; +import static store.custom.constants.StringConstants.RESPONSE_YES; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.time.LocalDate; +import store.custom.model.PromotionResult.PromotionResult; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Product; +import store.custom.model.product.Products; +import store.custom.model.promotion.Promotion; +import store.custom.model.promotion.Promotions; + +public class OrderSheetEditor { + // ์ฃผ๋ฌธ์„œ ์ดˆ๊ธฐ ์„ค์ •: ๊ฐ€๊ฒฉ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ •๋ณด ์ถ”๊ฐ€ + public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) { + for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) { + setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ€๊ฒฉ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ถ”๊ฐ€ + setPromotionDetails(orderProduct, promotions); // ํ”„๋กœ๋ชจ์…˜ ๊ด€๋ จ ์ •๋ณด ์ถ”๊ฐ€ + } + } + + private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) { + Product product = findOrderedProduct(orderProduct, productCatalog); + if (product != null) { + orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity()); + setPromotionIfExist(orderProduct, product); + } + } + + private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) { + for (Product product : productCatalog.getProducts()) { + if (product.getName().equals(orderProduct.getName())) { + return product; + } + } + return null; + } + + private void setPromotionIfExist(OrderedProduct orderProduct, Product product) { + if (product.getPromotion() != null) { + orderProduct.setPromotion(product.getPromotion()); + } + } + + private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) { + if (orderProduct.getPromotion() != null) { + LocalDate today = DateTimes.now().toLocalDate(); + Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions); + if (promotion != null) { + applyPromotionDates(orderProduct, today, promotion); + applyPromotionBuyAndGet(orderProduct, promotion); + } + } + } + + private Promotion findPromotion(String promotionName, Promotions promotions) { + for (Promotion promotion : promotions.getPromotions()) { + if (promotion.getName().equals(promotionName)) { + return promotion; + } + } + return null; + } + + private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) { + if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) { + orderProduct.setPromotion(BEFORE_PROMOTION_START); + } + + if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) { + orderProduct.setPromotion(AFTER_PROMOTION_END); + } + } + + private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) { + String orderProductPromotion = orderProduct.getPromotion(); + + if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START) + && !orderProductPromotion.equals(AFTER_PROMOTION_END)) { + orderProduct.setBuy(promotion.getBuy()); + orderProduct.setGet(promotion.getGet()); + } + } + + // ์ฃผ๋ฌธ์„œ ์ˆ˜์ •: ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์— ๋‹ค๋ฅธ ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ๊ฒฐ๊ณผ ๋ฐ˜์˜ + public void applyResponseForNoPromotion(String response, PromotionResult promotionResult, + OrderedProduct orderedProduct) { + if (response.equals(RESPONSE_YES)) { + processPurchaseWithNoDiscount(orderedProduct, promotionResult); + } + if (response.equals(RESPONSE_NO)) { + processNonPurchaseWithNoDiscount(orderedProduct, promotionResult); + } + } + + private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + int nonPromotionProductCount = promotionResult.getNonPromotionProductCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } + + private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + int nonPromotionProductCount = promotionResult.getNonPromotionProductCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity(); + orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์ˆ˜๋Ÿ‰ ๊ฐ์†Œ + orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์ˆ˜๋Ÿ‰ ๊ฐ์†Œ์— ๋”ฐ๋ฅธ ์ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ + } + + public void applyResponseForFreeProduct(String response, PromotionResult promotionResult, + OrderedProduct orderedProduct) { + if (response.equals(RESPONSE_YES)) { + processAdditionalPromotionApplied(orderedProduct, promotionResult); + } + if (response.equals(RESPONSE_NO)) { + processAdditionalPromotionNotApplied(orderedProduct, promotionResult); + } + } + + private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1)); + int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity(); + orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€ + orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€์— ๋”ฐ๋ฅธ ์ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ + orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1)); + } + + private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1)); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } + + public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } +} \ No newline at end of file
Java
OrderSheetEditor ํด๋ž˜์Šค ๋ถ€๋ถ„์„ ์ฐธ๊ณ ํ•˜๋ฉด ์ œ๊ฐ€ ์žฌ์ž…๋ ฅ๊ณผ ๊ด€๋ จํ•ด ๊ณ ๋ฏผํ•˜๋˜ ๋ถ€๋ถ„์ด ์ผ๋ถ€ ํ•ด๊ฒฐ๋  ๊ฒƒ ๊ฐ™์•„์š”. ์ถ”๊ฐ€๋กœ, ์ „์ฒด์ ์œผ๋กœ ์ˆ ์ˆ  ์ž˜ ์ฝํžˆ๋Š” ์ฝ”๋“œ์™€ ๋กœ์ง์— ๊ฐํƒ„ํ•˜๊ณ  ๊ฐ‘๋‹ˆ๋‹ค.
@@ -0,0 +1,35 @@ +package store.custom.model.product; + +public class Product { + private final String name; + private final int price; + private int quantity; + private final String promotion; + + public Product(String name, int price, int quantity, String promotion) { + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotion = promotion; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public String getPromotion() { + return promotion; + } +} \ No newline at end of file
Java
promotion ํ•„๋“œ๋Š” ํ”„๋กœ๋ชจ์…˜ ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ฐธ์กฐํ•˜๋„๋ก ํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,153 @@ +package store.custom.service.editor; + +import static store.custom.constants.StringConstants.AFTER_PROMOTION_END; +import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START; +import static store.custom.constants.StringConstants.RESPONSE_NO; +import static store.custom.constants.StringConstants.RESPONSE_YES; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.time.LocalDate; +import store.custom.model.PromotionResult.PromotionResult; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Product; +import store.custom.model.product.Products; +import store.custom.model.promotion.Promotion; +import store.custom.model.promotion.Promotions; + +public class OrderSheetEditor { + // ์ฃผ๋ฌธ์„œ ์ดˆ๊ธฐ ์„ค์ •: ๊ฐ€๊ฒฉ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ •๋ณด ์ถ”๊ฐ€ + public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) { + for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) { + setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ€๊ฒฉ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ถ”๊ฐ€ + setPromotionDetails(orderProduct, promotions); // ํ”„๋กœ๋ชจ์…˜ ๊ด€๋ จ ์ •๋ณด ์ถ”๊ฐ€ + } + } + + private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) { + Product product = findOrderedProduct(orderProduct, productCatalog); + if (product != null) { + orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity()); + setPromotionIfExist(orderProduct, product); + } + } + + private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) { + for (Product product : productCatalog.getProducts()) { + if (product.getName().equals(orderProduct.getName())) { + return product; + } + } + return null; + } + + private void setPromotionIfExist(OrderedProduct orderProduct, Product product) { + if (product.getPromotion() != null) { + orderProduct.setPromotion(product.getPromotion()); + } + } + + private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) { + if (orderProduct.getPromotion() != null) { + LocalDate today = DateTimes.now().toLocalDate(); + Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions); + if (promotion != null) { + applyPromotionDates(orderProduct, today, promotion); + applyPromotionBuyAndGet(orderProduct, promotion); + } + } + } + + private Promotion findPromotion(String promotionName, Promotions promotions) { + for (Promotion promotion : promotions.getPromotions()) { + if (promotion.getName().equals(promotionName)) { + return promotion; + } + } + return null; + } + + private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) { + if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) { + orderProduct.setPromotion(BEFORE_PROMOTION_START); + } + + if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) { + orderProduct.setPromotion(AFTER_PROMOTION_END); + } + } + + private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) { + String orderProductPromotion = orderProduct.getPromotion(); + + if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START) + && !orderProductPromotion.equals(AFTER_PROMOTION_END)) { + orderProduct.setBuy(promotion.getBuy()); + orderProduct.setGet(promotion.getGet()); + } + } + + // ์ฃผ๋ฌธ์„œ ์ˆ˜์ •: ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์— ๋‹ค๋ฅธ ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ๊ฒฐ๊ณผ ๋ฐ˜์˜ + public void applyResponseForNoPromotion(String response, PromotionResult promotionResult, + OrderedProduct orderedProduct) { + if (response.equals(RESPONSE_YES)) { + processPurchaseWithNoDiscount(orderedProduct, promotionResult); + } + if (response.equals(RESPONSE_NO)) { + processNonPurchaseWithNoDiscount(orderedProduct, promotionResult); + } + } + + private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + int nonPromotionProductCount = promotionResult.getNonPromotionProductCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } + + private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + int nonPromotionProductCount = promotionResult.getNonPromotionProductCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity(); + orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์ˆ˜๋Ÿ‰ ๊ฐ์†Œ + orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์ˆ˜๋Ÿ‰ ๊ฐ์†Œ์— ๋”ฐ๋ฅธ ์ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ + } + + public void applyResponseForFreeProduct(String response, PromotionResult promotionResult, + OrderedProduct orderedProduct) { + if (response.equals(RESPONSE_YES)) { + processAdditionalPromotionApplied(orderedProduct, promotionResult); + } + if (response.equals(RESPONSE_NO)) { + processAdditionalPromotionNotApplied(orderedProduct, promotionResult); + } + } + + private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1)); + int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity(); + orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€ + orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€์— ๋”ฐ๋ฅธ ์ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ + orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1)); + } + + private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1)); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } + + public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } +} \ No newline at end of file
Java
OrderSheetEditor์—์„œ ๋งŽ์€ ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ๋Š”๋“ฏ ํ•ฉ๋‹ˆ๋‹ค ์ฃผ๋ฌธ์„œ ํŽธ์ง‘, ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ, ์žฌ๊ณ  ๊ด€๋ฆฌ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ๊ฐ๊ฐ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒŒ ์–ด๋–จ๊นŒ์š”? ์‚ฌ์‹ค ์ด๋Ÿฐ ๋ถ€๋ถ„์—์„œ ์ €๋„ ๊ณ ๋ฏผ์ด ๋งŽ์€ ํ„ฐ๋ผ ๋ผ์ ค๋‹˜์˜ ๋‹ค๋ฅธ ์˜๊ฒฌ์ด ์žˆ๋‹ค๋ฉด ๋“ฃ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,63 @@ +package store.custom.model.order; + +public class OrderedProduct { + private final String name; + private int quantity; + private int totalPrice; + private String promotion; + private int buy; + private int get; + + public OrderedProduct(String name, int quantity, int totalPrice, String promotion, int buy, int get) { + this.name = name; + this.quantity = quantity; + this.totalPrice = totalPrice; + this.promotion = promotion; + this.buy = buy; + this.get = get; + } + + public String getName() { + return name; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public int getTotalPrice() { + return totalPrice; + } + + public void setTotalPrice(int totalPrice) { + this.totalPrice = totalPrice; + } + + public String getPromotion() { + return promotion; + } + + public void setPromotion(String promotion) { + this.promotion = promotion; + } + + public int getBuy() { + return buy; + } + + public void setBuy(int buy) { + this.buy = buy; + } + + public int getGet() { + return get; + } + + public void setGet(int get) { + this.get = get; + } +} \ No newline at end of file
Java
setter๋ฅผ ์—ด์–ด๋‘๋Š”๊ฒƒ์€ ์ข‹์ง€ ์•Š๋‹ค๊ณ  ๋ฐฐ์› ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ์žฌ๊ณ ๋ฅผ ์ฐจ๊ฐํ•œ๋‹ค๋˜๊ฐ€ ๋‹ค๋ฅธ ๊ฐ’๋“ค์„ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ• ๋•Œ ํ• ๋‹นํ•œ๋‹ค๋˜๊ฐ€ ํ•˜๋Š” ๋กœ์ง์„ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์„œ ์ฒ˜๋ฆฌํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,58 @@ +package store.custom.service.parser; + +import static store.custom.constants.RegexConstants.SINGLE_COMMA; +import static store.custom.constants.StringConstants.NO_PROMOTION; + +import java.util.ArrayList; +import java.util.List; +import store.custom.model.product.Product; +import store.custom.model.product.Products; + +public class ProductParser { + public static Products run(List<String> lines) { + List<Product> productCatalog = new ArrayList<>(); + + if (lines != null) { + parseProductLines(lines, productCatalog); + } + + return new Products(productCatalog); + } + + private static void parseProductLines(List<String> lines, List<Product> productCatalog) { + for (int currentLine = 1; currentLine < lines.size(); currentLine++) { + List<String> currentLineParts = List.of(lines.get(currentLine).split(SINGLE_COMMA)); + Product product = createProduct(currentLineParts); + productCatalog.add(product); + } + } + + private static Product createProduct(List<String> parts) { + String name = extractProductName(parts); + int price = extractProductPrice(parts); + int quantity = extractProductQuantity(parts); + String promotion = extractProductPromotion(parts); + + return new Product(name, price, quantity, promotion); + } + + private static String extractProductName(List<String> parts) { + return parts.get(0).trim(); + } + + private static int extractProductPrice(List<String> parts) { + return Integer.parseInt(parts.get(1).trim()); + } + + private static int extractProductQuantity(List<String> parts) { + return Integer.parseInt(parts.get(2).trim()); + } + + private static String extractProductPromotion(List<String> parts) { + String promotion = parts.get(3).trim(); + if (promotion.equals(NO_PROMOTION)) { + return null; + } + return promotion; + } +} \ No newline at end of file
Java
๋ณ„๊ฑฐ ์•„๋‹Œ ๋ถ€๋ถ„์ด๊ธด ํ•œ๋ฐ, ์ธ๋ฑ์Šค๋ฅผ ํ•˜๋“œ์ฝ”๋”ฉํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค ์ƒ์ˆ˜๋กœ ๋นผ์„œ nameIndex, priceIndex ์ฒ˜๋Ÿผ ๋‘์—ˆ์œผ๋ฉด ๋” ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,153 @@ +package store.custom.service.editor; + +import static store.custom.constants.StringConstants.AFTER_PROMOTION_END; +import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START; +import static store.custom.constants.StringConstants.RESPONSE_NO; +import static store.custom.constants.StringConstants.RESPONSE_YES; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.time.LocalDate; +import store.custom.model.PromotionResult.PromotionResult; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Product; +import store.custom.model.product.Products; +import store.custom.model.promotion.Promotion; +import store.custom.model.promotion.Promotions; + +public class OrderSheetEditor { + // ์ฃผ๋ฌธ์„œ ์ดˆ๊ธฐ ์„ค์ •: ๊ฐ€๊ฒฉ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ •๋ณด ์ถ”๊ฐ€ + public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) { + for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) { + setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ€๊ฒฉ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ถ”๊ฐ€ + setPromotionDetails(orderProduct, promotions); // ํ”„๋กœ๋ชจ์…˜ ๊ด€๋ จ ์ •๋ณด ์ถ”๊ฐ€ + } + } + + private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) { + Product product = findOrderedProduct(orderProduct, productCatalog); + if (product != null) { + orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity()); + setPromotionIfExist(orderProduct, product); + } + } + + private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) { + for (Product product : productCatalog.getProducts()) { + if (product.getName().equals(orderProduct.getName())) { + return product; + } + } + return null; + } + + private void setPromotionIfExist(OrderedProduct orderProduct, Product product) { + if (product.getPromotion() != null) { + orderProduct.setPromotion(product.getPromotion()); + } + } + + private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) { + if (orderProduct.getPromotion() != null) { + LocalDate today = DateTimes.now().toLocalDate(); + Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions); + if (promotion != null) { + applyPromotionDates(orderProduct, today, promotion); + applyPromotionBuyAndGet(orderProduct, promotion); + } + } + } + + private Promotion findPromotion(String promotionName, Promotions promotions) { + for (Promotion promotion : promotions.getPromotions()) { + if (promotion.getName().equals(promotionName)) { + return promotion; + } + } + return null; + } + + private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) { + if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) { + orderProduct.setPromotion(BEFORE_PROMOTION_START); + } + + if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) { + orderProduct.setPromotion(AFTER_PROMOTION_END); + } + } + + private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) { + String orderProductPromotion = orderProduct.getPromotion(); + + if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START) + && !orderProductPromotion.equals(AFTER_PROMOTION_END)) { + orderProduct.setBuy(promotion.getBuy()); + orderProduct.setGet(promotion.getGet()); + } + } + + // ์ฃผ๋ฌธ์„œ ์ˆ˜์ •: ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์— ๋‹ค๋ฅธ ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ๊ฒฐ๊ณผ ๋ฐ˜์˜ + public void applyResponseForNoPromotion(String response, PromotionResult promotionResult, + OrderedProduct orderedProduct) { + if (response.equals(RESPONSE_YES)) { + processPurchaseWithNoDiscount(orderedProduct, promotionResult); + } + if (response.equals(RESPONSE_NO)) { + processNonPurchaseWithNoDiscount(orderedProduct, promotionResult); + } + } + + private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + int nonPromotionProductCount = promotionResult.getNonPromotionProductCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } + + private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + int nonPromotionProductCount = promotionResult.getNonPromotionProductCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity(); + orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์ˆ˜๋Ÿ‰ ๊ฐ์†Œ + orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์ˆ˜๋Ÿ‰ ๊ฐ์†Œ์— ๋”ฐ๋ฅธ ์ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ + } + + public void applyResponseForFreeProduct(String response, PromotionResult promotionResult, + OrderedProduct orderedProduct) { + if (response.equals(RESPONSE_YES)) { + processAdditionalPromotionApplied(orderedProduct, promotionResult); + } + if (response.equals(RESPONSE_NO)) { + processAdditionalPromotionNotApplied(orderedProduct, promotionResult); + } + } + + private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1)); + int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity(); + orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€ + orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€์— ๋”ฐ๋ฅธ ์ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ + orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1)); + } + + private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1)); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } + + public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } +} \ No newline at end of file
Java
OrderSheetEditor์˜ ์—ญํ• ์ด ๋งŽ๋‹ค๋Š” ์ ์—์„œ ์ €๋„ ๋น„์Šทํ•œ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค! + **3์ฃผ์ฐจ ํ”ผ๋“œ๋ฐฑ ์ค‘ ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด๋‹ต๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค** ํ”ผ๋“œ๋ฐฑ์ด ๋– ์˜ฌ๋ž์–ด์š”. ์ผ๋ถ€ ๋กœ์ง ์ค‘์—์„œ๋Š” ๋ชจ๋ธ ๋‚ด๋ถ€์—์„œ ์ฑ…์ž„์„ ๊ฐ€์ ธ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค๋„ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ์•„๋ž˜ ๋ฉ”์„œ๋“œ๊ฐ€ ๊ทธ๋Ÿฐ ๊ฒƒ ๊ฐ™์•„์š”! ``` private void setPromotionIfExist(OrderedProduct orderProduct, Product product) { if (product.getPromotion() != null) { orderProduct.setPromotion(product.getPromotion()); } } ``` product.getPromotion()์ด String์ด๊ธฐ ๋•Œ๋ฌธ์— orderProduct ๋‚ด๋ถ€์—์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์œ„ ์ฒ˜๋Ÿผ ๊ฐ ๋„๋ฉ”์ธ์—์„œ ์ฒ˜๋ฆฌ๊ฐ€๋Šฅํ•œ ๋กœ์ง๋“ค์€ ์—†์—ˆ์„์ง€ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ ์‹ถ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,63 @@ +package store.custom.model.order; + +public class OrderedProduct { + private final String name; + private int quantity; + private int totalPrice; + private String promotion; + private int buy; + private int get; + + public OrderedProduct(String name, int quantity, int totalPrice, String promotion, int buy, int get) { + this.name = name; + this.quantity = quantity; + this.totalPrice = totalPrice; + this.promotion = promotion; + this.buy = buy; + this.get = get; + } + + public String getName() { + return name; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public int getTotalPrice() { + return totalPrice; + } + + public void setTotalPrice(int totalPrice) { + this.totalPrice = totalPrice; + } + + public String getPromotion() { + return promotion; + } + + public void setPromotion(String promotion) { + this.promotion = promotion; + } + + public int getBuy() { + return buy; + } + + public void setBuy(int buy) { + this.buy = buy; + } + + public int getGet() { + return get; + } + + public void setGet(int get) { + this.get = get; + } +} \ No newline at end of file
Java
์œ„์— ์–ธ๊ธ‰๋“œ๋ฆฐ 3์ฃผ์ฐจ ํ”ผ๋“œ๋ฐฑ ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด๋‹ต๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ์™€ ์—ฐ๊ด€๋œ ๋ฆฌ๋ทฐ๊ฐ€ ์•„๋‹๊นŒ ์‹ถ๋„ค์š”! 3์ฃผ์ฐจ ํ”ผ๋“œ๋ฐฑ ์ค‘ ๋ฐœ์ทŒ >Lotto์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด์ง€(get) ๋ง๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ๋˜์ง€๋„๋ก ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊ฟ” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด๊ฐ€ ์ผํ•˜๋„๋ก ํ•œ๋‹ค. ์ด์ฒ˜๋Ÿผ Lotto ๊ฐ์ฒด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด(get) ์‚ฌ์šฉํ•˜๊ธฐ๋ณด๋‹ค๋Š”, ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๊ฐ€ ์Šค์Šค๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์กฐ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ์™ธ๋ถ€์—์„œ ๊ฐ€์ ธ์™€(get) ์ฒ˜๋ฆฌํ•˜์ง€ ๋ง๊ณ , ๊ฐ์ฒด๊ฐ€ ์ž์‹ ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์Šค์Šค๋กœ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋ฉ”์‹œ์ง€๋ฅผ ๋˜์ง€๊ฒŒ ํ•œ๋‹ค.
@@ -0,0 +1,190 @@ +package store.custom.controller; + +import static store.custom.constants.StringConstants.PRODUCTS_FILE_PATH; +import static store.custom.constants.StringConstants.PROMOTIONS_FILE_PATH; +import static store.custom.constants.StringConstants.RESPONSE_YES; + +import java.util.List; +import store.custom.model.PromotionResult.PromotionResult; +import store.custom.model.PromotionResult.PromotionResults; +import store.custom.model.ReceiptDetails; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Products; +import store.custom.model.promotion.Promotions; +import store.custom.service.editor.OrderSheetEditor; +import store.custom.service.editor.ProductsEditor; +import store.custom.service.filehandler.FileReader; +import store.custom.service.maker.PromotionResultMaker; +import store.custom.service.maker.ReceiptDetailsMaker; +import store.custom.service.parser.OrderParser; +import store.custom.service.parser.ProductParser; +import store.custom.service.parser.PromotionParser; +import store.custom.service.parser.ResponseParser; +import store.custom.view.InputView; +import store.custom.view.OutputView; + +public class StoreController { + private final OrderSheetEditor orderSheetEditor; + private final PromotionResultMaker promotionResultsMaker; + private final ReceiptDetailsMaker receiptDetailsMaker; + private final OrderParser orderParser; + private final ResponseParser responseParser; + + private final InputView inputView; + private final OutputView outputView; + + public StoreController(InputView inputView, OutputView outputView) { + this.orderSheetEditor = new OrderSheetEditor(); + this.promotionResultsMaker = new PromotionResultMaker(); + this.receiptDetailsMaker = new ReceiptDetailsMaker(); + this.orderParser = new OrderParser(); + this.responseParser = new ResponseParser(); + + this.inputView = inputView; + this.outputView = outputView; + } + + public void start() { + Products productCatalog = setUpProductCatalog(); + Promotions promotionCatalog = setUpPromotionCatalog(); + + String repeat = RESPONSE_YES; + while (RESPONSE_YES.equals(repeat)) { + outputView.displayInventoryStatus(productCatalog); + repeat = handleStoreOrder(productCatalog, promotionCatalog); + } + } + + // ํŽธ์˜์  ํ”„๋กœ๊ทธ๋žจ ์ดˆ๊ธฐ ์…‹์—… ๋ฉ”์„œ๋“œ + private Products setUpProductCatalog() { + List<String> productsLines = FileReader.run(PRODUCTS_FILE_PATH); + Products productCatalog = ProductParser.run(productsLines); + return ProductsEditor.inspectProductCatalog(productCatalog); + } + + private Promotions setUpPromotionCatalog() { + List<String> promotionLines = FileReader.run(PROMOTIONS_FILE_PATH); + return PromotionParser.run(promotionLines); + } + + // ์ฃผ๋ฌธ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ + private String handleStoreOrder(Products productCatalog, Promotions promotionCatalog) { + OrderSheet orderSheet = handleOrderSheet(productCatalog, promotionCatalog); + ProductsEditor.adjustInventoryForOrders(orderSheet, productCatalog); // ์ฃผ๋ฌธ์„œ์— ๋งž์ถฐ ์žฌ๊ณ  ๊ด€๋ฆฌ + ReceiptDetails receiptDetails = receiptDetailsMaker.run(orderSheet, inputResponseForMembership()); + + outputView.displayReceipt(orderSheet, receiptDetails); // ๋ ˆ์‹œํ”ผ ์ถœ๋ ฅ + return inputResponseForAdditionalPurchase(); + } + + // ์ฃผ๋ฌธ์„œ ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ + private OrderSheet handleOrderSheet(Products productCatalog, Promotions promotionCatalog) { + OrderSheet orderSheet = inputOrderRequest(productCatalog); + orderSheetEditor.addPromotionInfo(productCatalog, promotionCatalog, orderSheet); + + PromotionResults promotionResults = + promotionResultsMaker.createPromotionResults(productCatalog, orderSheet); // ํ”„๋กœ๋ชจ์…˜ ๊ฒฐ๊ณผ์ง€ ๋งŒ๋“ค๊ธฐ + handlePromotionResults(orderSheet, promotionResults); // ํ”„๋กœ๋ชจ์…˜ ๊ฒฐ๊ณผ์ง€๋ฅผ ์ฃผ๋ฌธ์„œ์— ๋ฐ˜์˜ + + return orderSheet; + } + + // ํ”„๋กœ๋ชจ์…˜ ํ–‰์‚ฌ ์ ์šฉ ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ + private void handlePromotionResults(OrderSheet orderSheet, PromotionResults promotionResults) { + for (int index = 0; index < promotionResults.getPromotionResultCount(); index++) { + PromotionResult promotionResult = promotionResults.getPromotionResultByIndex(index); + OrderedProduct orderedProduct = orderSheet.getOrderSheetByIndex(index); + + handleExcludedPromotionProduct(orderedProduct, promotionResult); + handleAdditionalFreebie(orderedProduct, promotionResult); + handlePromotionProduct(orderedProduct, promotionResult); + } + } + + private void handleExcludedPromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) { + String orderedProductName = orderedProduct.getName(); + int nonPromotionProduct = promotionResult.getNonPromotionProductCount(); + + if (nonPromotionProduct > 0) { + String responseForNoPromotion = inputResponseForNoPromotion(orderedProductName, nonPromotionProduct); + orderSheetEditor.applyResponseForNoPromotion(responseForNoPromotion, promotionResult, orderedProduct); + } + } + + private void handleAdditionalFreebie(OrderedProduct orderedProduct, PromotionResult promotionResult) { + String orderedProductName = orderedProduct.getName(); + int extraPromotionCount = promotionResult.getExtraPromotionCount(); + + if (extraPromotionCount > 0) { + int additionalFreebie = orderedProduct.getGet(); + String responseForFreeProduct = inputResponseForFreebie(orderedProductName, additionalFreebie); + orderSheetEditor.applyResponseForFreeProduct(responseForFreeProduct, promotionResult, orderedProduct); + } + } + + private void handlePromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int nonPromotionProduct = promotionResult.getNonPromotionProductCount(); + int extraPromotionCount = promotionResult.getExtraPromotionCount(); + + if (extraPromotionCount == 0 && nonPromotionProduct == 0) { + orderSheetEditor.computeTotalWithPromotion(orderedProduct, promotionResult); + } + } + + // ์‚ฌ์šฉ์ž ์ž…๋ ฅ ๊ด€๋ จ ๋ฉ”์„œ๋“œ (Error ์‹œ ์žฌ์ž…๋ ฅ) + private OrderSheet inputOrderRequest(Products productCatalog) { + while (true) { + try { + String orderRequest = inputView.askForProductsToPurchase(); + return orderParser.run(orderRequest, productCatalog); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } + + private String inputResponseForNoPromotion(String name, int noPromotionProductCount) { + while (true) { + try { + String response = inputView.askForProductWithoutPromotion(name, noPromotionProductCount); + return responseParser.run(response); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } + + private String inputResponseForFreebie(String name, int additionalFreeProduct) { + while (true) { + try { + String response = inputView.askForFreebie(name, additionalFreeProduct); + return responseParser.run(response); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } + + private String inputResponseForMembership() { + while (true) { + try { + String response = inputView.askForMembershipDiscount(); + return responseParser.run(response); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } + + private String inputResponseForAdditionalPurchase() { + while (true) { + try { + String response = inputView.askForAdditionalPurchase(); + return responseParser.run(response); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } +} \ No newline at end of file
Java
while(true)์™€ try-catch๊ฐ€ ๋ฐ˜๋ณต๋˜๋Š”๋ฐ, ๋ฐ˜ํ™˜๊ฐ’์ด ๊ฐ™์€ ๊ฒƒ ๊ฐ™์•„์„œ์š”! ํ•ธ๋“ค๋Ÿฌ๋กœ ๋นผ์„œ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋” ์œ ์—ฐํ•œ ์ฝ”๋“œ๊ฐ€ ๋˜์ง€ ์•Š์„๊นŒ ์‹ถ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,63 @@ +package store.custom.model.order; + +public class OrderedProduct { + private final String name; + private int quantity; + private int totalPrice; + private String promotion; + private int buy; + private int get; + + public OrderedProduct(String name, int quantity, int totalPrice, String promotion, int buy, int get) { + this.name = name; + this.quantity = quantity; + this.totalPrice = totalPrice; + this.promotion = promotion; + this.buy = buy; + this.get = get; + } + + public String getName() { + return name; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public int getTotalPrice() { + return totalPrice; + } + + public void setTotalPrice(int totalPrice) { + this.totalPrice = totalPrice; + } + + public String getPromotion() { + return promotion; + } + + public void setPromotion(String promotion) { + this.promotion = promotion; + } + + public int getBuy() { + return buy; + } + + public void setBuy(int buy) { + this.buy = buy; + } + + public int getGet() { + return get; + } + + public void setGet(int get) { + this.get = get; + } +} \ No newline at end of file
Java
๋™์˜ํ•ฉ๋‹ˆ๋‹ค! ๊ฐ์ฒด๊ฐ€ ์ˆ˜๋™์ ์ธ ๊ฒƒ์ด ์•„๋‹Œ, '๋Šฅ๋™์ '์ธ ์ธ์Šคํ„ด์Šค๋ผ๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•˜์‹œ๋ฉด ๋” ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ๋ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,35 @@ +package store.custom.model.product; + +public class Product { + private final String name; + private final int price; + private int quantity; + private final String promotion; + + public Product(String name, int price, int quantity, String promotion) { + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotion = promotion; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public String getPromotion() { + return promotion; + } +} \ No newline at end of file
Java
๋™์˜ํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,153 @@ +package store.custom.service.editor; + +import static store.custom.constants.StringConstants.AFTER_PROMOTION_END; +import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START; +import static store.custom.constants.StringConstants.RESPONSE_NO; +import static store.custom.constants.StringConstants.RESPONSE_YES; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.time.LocalDate; +import store.custom.model.PromotionResult.PromotionResult; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Product; +import store.custom.model.product.Products; +import store.custom.model.promotion.Promotion; +import store.custom.model.promotion.Promotions; + +public class OrderSheetEditor { + // ์ฃผ๋ฌธ์„œ ์ดˆ๊ธฐ ์„ค์ •: ๊ฐ€๊ฒฉ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ •๋ณด ์ถ”๊ฐ€ + public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) { + for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) { + setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ€๊ฒฉ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ถ”๊ฐ€ + setPromotionDetails(orderProduct, promotions); // ํ”„๋กœ๋ชจ์…˜ ๊ด€๋ จ ์ •๋ณด ์ถ”๊ฐ€ + } + } + + private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) { + Product product = findOrderedProduct(orderProduct, productCatalog); + if (product != null) { + orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity()); + setPromotionIfExist(orderProduct, product); + } + } + + private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) { + for (Product product : productCatalog.getProducts()) { + if (product.getName().equals(orderProduct.getName())) { + return product; + } + } + return null; + } + + private void setPromotionIfExist(OrderedProduct orderProduct, Product product) { + if (product.getPromotion() != null) { + orderProduct.setPromotion(product.getPromotion()); + } + } + + private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) { + if (orderProduct.getPromotion() != null) { + LocalDate today = DateTimes.now().toLocalDate(); + Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions); + if (promotion != null) { + applyPromotionDates(orderProduct, today, promotion); + applyPromotionBuyAndGet(orderProduct, promotion); + } + } + } + + private Promotion findPromotion(String promotionName, Promotions promotions) { + for (Promotion promotion : promotions.getPromotions()) { + if (promotion.getName().equals(promotionName)) { + return promotion; + } + } + return null; + } + + private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) { + if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) { + orderProduct.setPromotion(BEFORE_PROMOTION_START); + } + + if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) { + orderProduct.setPromotion(AFTER_PROMOTION_END); + } + } + + private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) { + String orderProductPromotion = orderProduct.getPromotion(); + + if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START) + && !orderProductPromotion.equals(AFTER_PROMOTION_END)) { + orderProduct.setBuy(promotion.getBuy()); + orderProduct.setGet(promotion.getGet()); + } + } + + // ์ฃผ๋ฌธ์„œ ์ˆ˜์ •: ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์— ๋‹ค๋ฅธ ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ๊ฒฐ๊ณผ ๋ฐ˜์˜ + public void applyResponseForNoPromotion(String response, PromotionResult promotionResult, + OrderedProduct orderedProduct) { + if (response.equals(RESPONSE_YES)) { + processPurchaseWithNoDiscount(orderedProduct, promotionResult); + } + if (response.equals(RESPONSE_NO)) { + processNonPurchaseWithNoDiscount(orderedProduct, promotionResult); + } + } + + private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + int nonPromotionProductCount = promotionResult.getNonPromotionProductCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } + + private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + int nonPromotionProductCount = promotionResult.getNonPromotionProductCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity(); + orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์ˆ˜๋Ÿ‰ ๊ฐ์†Œ + orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์ˆ˜๋Ÿ‰ ๊ฐ์†Œ์— ๋”ฐ๋ฅธ ์ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ + } + + public void applyResponseForFreeProduct(String response, PromotionResult promotionResult, + OrderedProduct orderedProduct) { + if (response.equals(RESPONSE_YES)) { + processAdditionalPromotionApplied(orderedProduct, promotionResult); + } + if (response.equals(RESPONSE_NO)) { + processAdditionalPromotionNotApplied(orderedProduct, promotionResult); + } + } + + private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1)); + int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity(); + orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€ + orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€์— ๋”ฐ๋ฅธ ์ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ + orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1)); + } + + private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1)); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } + + public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } +} \ No newline at end of file
Java
๋™์˜ํ•ฉ๋‹ˆ๋‹ค! ํ˜„์žฌ ํด๋ž˜์Šค์—์„œ ์—ญํ• ์„ ๋งŽ์ด ๊ฐ–๊ณ  ์žˆ์–ด์„œ ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜, ๋ถ„๋ฆฌํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,190 @@ +package store.custom.controller; + +import static store.custom.constants.StringConstants.PRODUCTS_FILE_PATH; +import static store.custom.constants.StringConstants.PROMOTIONS_FILE_PATH; +import static store.custom.constants.StringConstants.RESPONSE_YES; + +import java.util.List; +import store.custom.model.PromotionResult.PromotionResult; +import store.custom.model.PromotionResult.PromotionResults; +import store.custom.model.ReceiptDetails; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Products; +import store.custom.model.promotion.Promotions; +import store.custom.service.editor.OrderSheetEditor; +import store.custom.service.editor.ProductsEditor; +import store.custom.service.filehandler.FileReader; +import store.custom.service.maker.PromotionResultMaker; +import store.custom.service.maker.ReceiptDetailsMaker; +import store.custom.service.parser.OrderParser; +import store.custom.service.parser.ProductParser; +import store.custom.service.parser.PromotionParser; +import store.custom.service.parser.ResponseParser; +import store.custom.view.InputView; +import store.custom.view.OutputView; + +public class StoreController { + private final OrderSheetEditor orderSheetEditor; + private final PromotionResultMaker promotionResultsMaker; + private final ReceiptDetailsMaker receiptDetailsMaker; + private final OrderParser orderParser; + private final ResponseParser responseParser; + + private final InputView inputView; + private final OutputView outputView; + + public StoreController(InputView inputView, OutputView outputView) { + this.orderSheetEditor = new OrderSheetEditor(); + this.promotionResultsMaker = new PromotionResultMaker(); + this.receiptDetailsMaker = new ReceiptDetailsMaker(); + this.orderParser = new OrderParser(); + this.responseParser = new ResponseParser(); + + this.inputView = inputView; + this.outputView = outputView; + } + + public void start() { + Products productCatalog = setUpProductCatalog(); + Promotions promotionCatalog = setUpPromotionCatalog(); + + String repeat = RESPONSE_YES; + while (RESPONSE_YES.equals(repeat)) { + outputView.displayInventoryStatus(productCatalog); + repeat = handleStoreOrder(productCatalog, promotionCatalog); + } + } + + // ํŽธ์˜์  ํ”„๋กœ๊ทธ๋žจ ์ดˆ๊ธฐ ์…‹์—… ๋ฉ”์„œ๋“œ + private Products setUpProductCatalog() { + List<String> productsLines = FileReader.run(PRODUCTS_FILE_PATH); + Products productCatalog = ProductParser.run(productsLines); + return ProductsEditor.inspectProductCatalog(productCatalog); + } + + private Promotions setUpPromotionCatalog() { + List<String> promotionLines = FileReader.run(PROMOTIONS_FILE_PATH); + return PromotionParser.run(promotionLines); + } + + // ์ฃผ๋ฌธ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ + private String handleStoreOrder(Products productCatalog, Promotions promotionCatalog) { + OrderSheet orderSheet = handleOrderSheet(productCatalog, promotionCatalog); + ProductsEditor.adjustInventoryForOrders(orderSheet, productCatalog); // ์ฃผ๋ฌธ์„œ์— ๋งž์ถฐ ์žฌ๊ณ  ๊ด€๋ฆฌ + ReceiptDetails receiptDetails = receiptDetailsMaker.run(orderSheet, inputResponseForMembership()); + + outputView.displayReceipt(orderSheet, receiptDetails); // ๋ ˆ์‹œํ”ผ ์ถœ๋ ฅ + return inputResponseForAdditionalPurchase(); + } + + // ์ฃผ๋ฌธ์„œ ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ + private OrderSheet handleOrderSheet(Products productCatalog, Promotions promotionCatalog) { + OrderSheet orderSheet = inputOrderRequest(productCatalog); + orderSheetEditor.addPromotionInfo(productCatalog, promotionCatalog, orderSheet); + + PromotionResults promotionResults = + promotionResultsMaker.createPromotionResults(productCatalog, orderSheet); // ํ”„๋กœ๋ชจ์…˜ ๊ฒฐ๊ณผ์ง€ ๋งŒ๋“ค๊ธฐ + handlePromotionResults(orderSheet, promotionResults); // ํ”„๋กœ๋ชจ์…˜ ๊ฒฐ๊ณผ์ง€๋ฅผ ์ฃผ๋ฌธ์„œ์— ๋ฐ˜์˜ + + return orderSheet; + } + + // ํ”„๋กœ๋ชจ์…˜ ํ–‰์‚ฌ ์ ์šฉ ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ + private void handlePromotionResults(OrderSheet orderSheet, PromotionResults promotionResults) { + for (int index = 0; index < promotionResults.getPromotionResultCount(); index++) { + PromotionResult promotionResult = promotionResults.getPromotionResultByIndex(index); + OrderedProduct orderedProduct = orderSheet.getOrderSheetByIndex(index); + + handleExcludedPromotionProduct(orderedProduct, promotionResult); + handleAdditionalFreebie(orderedProduct, promotionResult); + handlePromotionProduct(orderedProduct, promotionResult); + } + } + + private void handleExcludedPromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) { + String orderedProductName = orderedProduct.getName(); + int nonPromotionProduct = promotionResult.getNonPromotionProductCount(); + + if (nonPromotionProduct > 0) { + String responseForNoPromotion = inputResponseForNoPromotion(orderedProductName, nonPromotionProduct); + orderSheetEditor.applyResponseForNoPromotion(responseForNoPromotion, promotionResult, orderedProduct); + } + } + + private void handleAdditionalFreebie(OrderedProduct orderedProduct, PromotionResult promotionResult) { + String orderedProductName = orderedProduct.getName(); + int extraPromotionCount = promotionResult.getExtraPromotionCount(); + + if (extraPromotionCount > 0) { + int additionalFreebie = orderedProduct.getGet(); + String responseForFreeProduct = inputResponseForFreebie(orderedProductName, additionalFreebie); + orderSheetEditor.applyResponseForFreeProduct(responseForFreeProduct, promotionResult, orderedProduct); + } + } + + private void handlePromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int nonPromotionProduct = promotionResult.getNonPromotionProductCount(); + int extraPromotionCount = promotionResult.getExtraPromotionCount(); + + if (extraPromotionCount == 0 && nonPromotionProduct == 0) { + orderSheetEditor.computeTotalWithPromotion(orderedProduct, promotionResult); + } + } + + // ์‚ฌ์šฉ์ž ์ž…๋ ฅ ๊ด€๋ จ ๋ฉ”์„œ๋“œ (Error ์‹œ ์žฌ์ž…๋ ฅ) + private OrderSheet inputOrderRequest(Products productCatalog) { + while (true) { + try { + String orderRequest = inputView.askForProductsToPurchase(); + return orderParser.run(orderRequest, productCatalog); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } + + private String inputResponseForNoPromotion(String name, int noPromotionProductCount) { + while (true) { + try { + String response = inputView.askForProductWithoutPromotion(name, noPromotionProductCount); + return responseParser.run(response); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } + + private String inputResponseForFreebie(String name, int additionalFreeProduct) { + while (true) { + try { + String response = inputView.askForFreebie(name, additionalFreeProduct); + return responseParser.run(response); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } + + private String inputResponseForMembership() { + while (true) { + try { + String response = inputView.askForMembershipDiscount(); + return responseParser.run(response); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } + + private String inputResponseForAdditionalPurchase() { + while (true) { + try { + String response = inputView.askForAdditionalPurchase(); + return responseParser.run(response); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } +} \ No newline at end of file
Java
์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๊ธฐ ๋ณด๋‹ค๋Š”,,, ์›์†Œ๋กœ ์ ‘๊ทผํ•ด๋„ ์ข‹์ง€ ์•Š์•˜์„๊นŒ์š”? ๋กœ์ง์ƒ์œผ๋กœ๋Š” ๊ฐ ์›์†Œ๊ฐ€ ์Œ์œผ๋กœ ๋“ค์–ด์˜ค์ง€๋งŒ ๊ทธ๊ฑธ ์ดํ•ดํ•˜๋ ค๋ฉด ์ฝ”๋“œ๋ฅผ ๊ณ„์† ๋”ฐ๋ผ๊ฐ€์•ผํ•˜๋‹ˆ๊นŒ,,, ๋‘ ๊ฐœ์˜ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ ‘๊ทผํ•ด์•ผํ•œ๋‹ค๋Š”๊ฒŒ ๋ฌธ์ œ๊ธด ํ•œ๋ฐ, ์–ด์ฐจํ”ผ ๋’ค์—๋„ ๋‘ ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ณ„์† ์“ฐ๊ณ , ๋˜ ๋ฐ˜๋ณต์‹œํ‚ค๋Š” ๊ฒƒ ๊ฐ™์•„์„œpromotionResults๋ฅผ ๋ฝ‘์„ ๋•Œ orderSheet์˜ ์›์†Œ์˜ ๋‚ด์šฉ์„ ํฌํ•จํ•˜๋Š” ๋ฌด์–ธ๊ฐ€๋กœ ๋งคํ•‘์‹œํ‚ค๊ฑฐ๋‚˜ ํ•  ์ˆ˜๋„ ์žˆ์ง€ ์•Š์„๊นŒ์š”?
@@ -0,0 +1,58 @@ +package store.custom.service.parser; + +import static store.custom.constants.RegexConstants.SINGLE_COMMA; +import static store.custom.constants.StringConstants.NO_PROMOTION; + +import java.util.ArrayList; +import java.util.List; +import store.custom.model.product.Product; +import store.custom.model.product.Products; + +public class ProductParser { + public static Products run(List<String> lines) { + List<Product> productCatalog = new ArrayList<>(); + + if (lines != null) { + parseProductLines(lines, productCatalog); + } + + return new Products(productCatalog); + } + + private static void parseProductLines(List<String> lines, List<Product> productCatalog) { + for (int currentLine = 1; currentLine < lines.size(); currentLine++) { + List<String> currentLineParts = List.of(lines.get(currentLine).split(SINGLE_COMMA)); + Product product = createProduct(currentLineParts); + productCatalog.add(product); + } + } + + private static Product createProduct(List<String> parts) { + String name = extractProductName(parts); + int price = extractProductPrice(parts); + int quantity = extractProductQuantity(parts); + String promotion = extractProductPromotion(parts); + + return new Product(name, price, quantity, promotion); + } + + private static String extractProductName(List<String> parts) { + return parts.get(0).trim(); + } + + private static int extractProductPrice(List<String> parts) { + return Integer.parseInt(parts.get(1).trim()); + } + + private static int extractProductQuantity(List<String> parts) { + return Integer.parseInt(parts.get(2).trim()); + } + + private static String extractProductPromotion(List<String> parts) { + String promotion = parts.get(3).trim(); + if (promotion.equals(NO_PROMOTION)) { + return null; + } + return promotion; + } +} \ No newline at end of file
Java
์š”๊ฒƒ๋„ ์ธ๋ฑ์Šค ์ ‘๊ทผ๋ณด๋‹ค ์›์†Œ ์ ‘๊ทผ์œผ๋กœ ๋ฐ”๊พธ๋Š”๊ฒŒ ๋” ์ง๊ด€์ ์ด๊ณ  ํšจ์œจ์ ์ผ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,112 @@ +package store.custom.service.editor; + +import java.util.ArrayList; +import java.util.List; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Product; +import store.custom.model.product.Products; + +public class ProductsEditor { + // ์žฌ๊ณ  ๋ชฉ๋ก ์ดˆ๊ธฐ ์„ค์ •: ๋ชฉ๋ก์— ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ ์ผ๋ฐ˜ ์ƒํ’ˆ ์ถ”๊ฐ€ + public static Products inspectProductCatalog(Products originalCatalog) { + List<Product> resultCatalog = new ArrayList<>(); + int currentIndex; + for (currentIndex = 0; currentIndex < originalCatalog.getProductsSize() - 1; currentIndex++) { + currentIndex += inspectProduct(originalCatalog, resultCatalog, currentIndex); + } + addLastProduct(originalCatalog, resultCatalog, currentIndex); + + return new Products(resultCatalog); + } + + private static int inspectProduct(Products original, List<Product> result, int currentIndex) { + Product currentProduct = original.getProductByIndex(currentIndex); + Product nextProduct = original.getProductByIndex(currentIndex + 1); + + if (hasSameNameAndPrice(result, currentProduct, nextProduct)) { + return 1; // ๋‘๊ฐœ์˜ ์ƒํ’ˆ์„ ๊ฒ€์‚ฌ์™„๋ฃŒํ–ˆ์œผ๋ฏ€๋กœ index ์˜ ๊ฐ’ 1 ์ฆ๊ฐ€ + } + + return addProductConsideringPromotion(currentProduct, result); + } + + private static boolean hasSameNameAndPrice(List<Product> result, Product currentProduct, Product nextProduct) { + if (currentProduct.getName().equals(nextProduct.getName()) + && currentProduct.getPrice() == nextProduct.getPrice()) { + result.add(currentProduct); + result.add(nextProduct); + return true; + } + return false; + } + + private static int addProductConsideringPromotion(Product currentProduct, List<Product> result) { + result.add(currentProduct); + + if (currentProduct.getPromotion() != null) { + result.add(createZeroStockProduct(currentProduct)); // ์žฌ๊ณ ๊ฐ€ 0์ธ ์ œํ’ˆ ์ถ”๊ฐ€ + } + return 0; + } + + private static Product createZeroStockProduct(Product product) { + return new Product(product.getName(), product.getPrice(), 0, null); + } + + private static void addLastProduct(Products originalCatalog, List<Product> result, int currentIndex) { + if (currentIndex == originalCatalog.getProductsSize() - 1) { + Product lastProduct = originalCatalog.getProductByIndex(originalCatalog.getProductsSize() - 1); + addProductConsideringPromotion(lastProduct, result); + } + } + + // ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์— ๋”ฐ๋ฅธ ์žฌ๊ณ  ์กฐ์ • + public static void adjustInventoryForOrders(OrderSheet orderSheet, Products productCatalog) { + for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) { + processOrderedProduct(orderedProduct, productCatalog); + } + } + + private static void processOrderedProduct(OrderedProduct orderedProduct, Products productCatalog) { + int remainQuantity = orderedProduct.getQuantity(); + for (Product product : productCatalog.getProducts()) { + if (remainQuantity == 0) { + break; + } + remainQuantity = updateProductQuantity(orderedProduct, product, remainQuantity); + } + } + + private static int updateProductQuantity(OrderedProduct orderedProduct, Product product, int remainQuantity) { + if (product.getName().equals(orderedProduct.getName())) { + if (product.getPromotion() != null) { + return calculateRemainingQuantityWithPromotion(product, remainQuantity); + } + return calculateRemainingQuantityWithoutPromotion(product, remainQuantity); + } + return remainQuantity; + } + + private static int calculateRemainingQuantityWithPromotion(Product product, int remainQuantity) { + int productQuantity = product.getQuantity(); + + if (remainQuantity >= productQuantity) { + product.setQuantity(0); + return remainQuantity - productQuantity; + } + product.setQuantity(productQuantity - remainQuantity); + return 0; + } + + private static int calculateRemainingQuantityWithoutPromotion(Product product, int remainQuantity) { + int productQuantity = product.getQuantity(); + + if (remainQuantity == productQuantity) { + product.setQuantity(0); + return 0; + } + product.setQuantity(productQuantity - remainQuantity); + return 0; + } +} \ No newline at end of file
Java
์ข€ ๋” ๋ช…๋ฃŒํ•œ ์ฝ”๋“œ ํ๋ฆ„์ด ์—†์„๊นŒ์š”? ์Œ... ArrayList์—ฌ์„œ ๋” ๋น„ํšจ์œจ์ ์ผ ์ˆ˜๋Š” ์žˆ์ง€๋งŒ promotion์ด ์žˆ๋Š” ์ƒํ’ˆ๋“ค๋งŒ ๋ฐ˜๋ณต์‹œํ‚ค๋ฉด์„œ ์ผ๋ฐ˜ ์žฌ๊ณ ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์—†์œผ๋ฉด ์ƒ์„ฑํ•˜๋Š”...?๊ฒƒ๋„ ์žฌ๋ฐŒ์ง€ ์•Š์„๊นŒ์š”?
@@ -0,0 +1,29 @@ +package store.custom.model.product; + +import static store.custom.validator.CustomErrorMessages.INVALID_INDEX; + +import java.util.ArrayList; +import java.util.List; + +public class Products { + private final List<Product> productCatalog; + + public Products(List<Product> productCatalog) { + this.productCatalog = new ArrayList<>(productCatalog); + } + + public List<Product> getProducts() { + return productCatalog; + } + + public int getProductsSize() { + return productCatalog.size(); + } + + public Product getProductByIndex(int index) { + if (index < 0 || index >= productCatalog.size()) { + throw new IndexOutOfBoundsException(INVALID_INDEX + index); + } + return productCatalog.get(index); + } +} \ No newline at end of file
Java
์ œ๊ฐ€ ์ผ๊ธ‰์ปฌ๋ ‰์…˜์„ ์ž˜ ๋ชจ๋ฅด๊ธด ํ•˜์ง€๋งŒ,,, ์ œ๊ฐ€ ์ดํ•ดํ•œ ๋ฐ”๋กœ๋Š” ์ข€ ๋” ๋ฉฑํ™•ํ•˜๊ฒŒ ์บก์Аํ™”ํ•˜๋Š”๊ฒŒ ์žฅ์ ์ธ ๊ฒƒ์œผ๋กœ ์ดํ•ดํ•˜๊ณ  ์žˆ์–ด์š”! ๊ทผ๋ฐ ์ผ๊ธ‰ ์ปฌ๋ž™์…˜์˜ ๊ตฌํ˜„์ด ๋‚ด๋ถ€์˜ ์š”์†Œ๋ฅผ ์™ธ๋ถ€๋กœ ๋ฐ˜์ถœ์‹œํ‚ค๋Š” ๊ฒƒ๋งŒ ์žˆ๋Š”๊ฒŒ ์˜คํžˆ๋ ค ์บก์Аํ™”, ์€๋‹‰ํ™”๋ฅผ ํ•ด์น  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”? ์™ธ๋ถ€์—์„œ product๋‚˜ products์—์„œ ํ™œ์šฉํ•˜๋Š” ์—ฌ๋Ÿฌ ๋ฉ”์„œ๋“œ๋“ค ์—ญ์‹œ ๋ชจ๋ธ์˜ ์ฑ…์ž„ ์•„๋ž˜์— ์žˆ์œผ๋‹ˆ๊นŒ ๋‚ด๋ถ€๋กœ ํฌํ•จ์‹œํ‚ค๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,190 @@ +package store.custom.controller; + +import static store.custom.constants.StringConstants.PRODUCTS_FILE_PATH; +import static store.custom.constants.StringConstants.PROMOTIONS_FILE_PATH; +import static store.custom.constants.StringConstants.RESPONSE_YES; + +import java.util.List; +import store.custom.model.PromotionResult.PromotionResult; +import store.custom.model.PromotionResult.PromotionResults; +import store.custom.model.ReceiptDetails; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Products; +import store.custom.model.promotion.Promotions; +import store.custom.service.editor.OrderSheetEditor; +import store.custom.service.editor.ProductsEditor; +import store.custom.service.filehandler.FileReader; +import store.custom.service.maker.PromotionResultMaker; +import store.custom.service.maker.ReceiptDetailsMaker; +import store.custom.service.parser.OrderParser; +import store.custom.service.parser.ProductParser; +import store.custom.service.parser.PromotionParser; +import store.custom.service.parser.ResponseParser; +import store.custom.view.InputView; +import store.custom.view.OutputView; + +public class StoreController { + private final OrderSheetEditor orderSheetEditor; + private final PromotionResultMaker promotionResultsMaker; + private final ReceiptDetailsMaker receiptDetailsMaker; + private final OrderParser orderParser; + private final ResponseParser responseParser; + + private final InputView inputView; + private final OutputView outputView; + + public StoreController(InputView inputView, OutputView outputView) { + this.orderSheetEditor = new OrderSheetEditor(); + this.promotionResultsMaker = new PromotionResultMaker(); + this.receiptDetailsMaker = new ReceiptDetailsMaker(); + this.orderParser = new OrderParser(); + this.responseParser = new ResponseParser(); + + this.inputView = inputView; + this.outputView = outputView; + } + + public void start() { + Products productCatalog = setUpProductCatalog(); + Promotions promotionCatalog = setUpPromotionCatalog(); + + String repeat = RESPONSE_YES; + while (RESPONSE_YES.equals(repeat)) { + outputView.displayInventoryStatus(productCatalog); + repeat = handleStoreOrder(productCatalog, promotionCatalog); + } + } + + // ํŽธ์˜์  ํ”„๋กœ๊ทธ๋žจ ์ดˆ๊ธฐ ์…‹์—… ๋ฉ”์„œ๋“œ + private Products setUpProductCatalog() { + List<String> productsLines = FileReader.run(PRODUCTS_FILE_PATH); + Products productCatalog = ProductParser.run(productsLines); + return ProductsEditor.inspectProductCatalog(productCatalog); + } + + private Promotions setUpPromotionCatalog() { + List<String> promotionLines = FileReader.run(PROMOTIONS_FILE_PATH); + return PromotionParser.run(promotionLines); + } + + // ์ฃผ๋ฌธ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ + private String handleStoreOrder(Products productCatalog, Promotions promotionCatalog) { + OrderSheet orderSheet = handleOrderSheet(productCatalog, promotionCatalog); + ProductsEditor.adjustInventoryForOrders(orderSheet, productCatalog); // ์ฃผ๋ฌธ์„œ์— ๋งž์ถฐ ์žฌ๊ณ  ๊ด€๋ฆฌ + ReceiptDetails receiptDetails = receiptDetailsMaker.run(orderSheet, inputResponseForMembership()); + + outputView.displayReceipt(orderSheet, receiptDetails); // ๋ ˆ์‹œํ”ผ ์ถœ๋ ฅ + return inputResponseForAdditionalPurchase(); + } + + // ์ฃผ๋ฌธ์„œ ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ + private OrderSheet handleOrderSheet(Products productCatalog, Promotions promotionCatalog) { + OrderSheet orderSheet = inputOrderRequest(productCatalog); + orderSheetEditor.addPromotionInfo(productCatalog, promotionCatalog, orderSheet); + + PromotionResults promotionResults = + promotionResultsMaker.createPromotionResults(productCatalog, orderSheet); // ํ”„๋กœ๋ชจ์…˜ ๊ฒฐ๊ณผ์ง€ ๋งŒ๋“ค๊ธฐ + handlePromotionResults(orderSheet, promotionResults); // ํ”„๋กœ๋ชจ์…˜ ๊ฒฐ๊ณผ์ง€๋ฅผ ์ฃผ๋ฌธ์„œ์— ๋ฐ˜์˜ + + return orderSheet; + } + + // ํ”„๋กœ๋ชจ์…˜ ํ–‰์‚ฌ ์ ์šฉ ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ + private void handlePromotionResults(OrderSheet orderSheet, PromotionResults promotionResults) { + for (int index = 0; index < promotionResults.getPromotionResultCount(); index++) { + PromotionResult promotionResult = promotionResults.getPromotionResultByIndex(index); + OrderedProduct orderedProduct = orderSheet.getOrderSheetByIndex(index); + + handleExcludedPromotionProduct(orderedProduct, promotionResult); + handleAdditionalFreebie(orderedProduct, promotionResult); + handlePromotionProduct(orderedProduct, promotionResult); + } + } + + private void handleExcludedPromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) { + String orderedProductName = orderedProduct.getName(); + int nonPromotionProduct = promotionResult.getNonPromotionProductCount(); + + if (nonPromotionProduct > 0) { + String responseForNoPromotion = inputResponseForNoPromotion(orderedProductName, nonPromotionProduct); + orderSheetEditor.applyResponseForNoPromotion(responseForNoPromotion, promotionResult, orderedProduct); + } + } + + private void handleAdditionalFreebie(OrderedProduct orderedProduct, PromotionResult promotionResult) { + String orderedProductName = orderedProduct.getName(); + int extraPromotionCount = promotionResult.getExtraPromotionCount(); + + if (extraPromotionCount > 0) { + int additionalFreebie = orderedProduct.getGet(); + String responseForFreeProduct = inputResponseForFreebie(orderedProductName, additionalFreebie); + orderSheetEditor.applyResponseForFreeProduct(responseForFreeProduct, promotionResult, orderedProduct); + } + } + + private void handlePromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int nonPromotionProduct = promotionResult.getNonPromotionProductCount(); + int extraPromotionCount = promotionResult.getExtraPromotionCount(); + + if (extraPromotionCount == 0 && nonPromotionProduct == 0) { + orderSheetEditor.computeTotalWithPromotion(orderedProduct, promotionResult); + } + } + + // ์‚ฌ์šฉ์ž ์ž…๋ ฅ ๊ด€๋ จ ๋ฉ”์„œ๋“œ (Error ์‹œ ์žฌ์ž…๋ ฅ) + private OrderSheet inputOrderRequest(Products productCatalog) { + while (true) { + try { + String orderRequest = inputView.askForProductsToPurchase(); + return orderParser.run(orderRequest, productCatalog); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } + + private String inputResponseForNoPromotion(String name, int noPromotionProductCount) { + while (true) { + try { + String response = inputView.askForProductWithoutPromotion(name, noPromotionProductCount); + return responseParser.run(response); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } + + private String inputResponseForFreebie(String name, int additionalFreeProduct) { + while (true) { + try { + String response = inputView.askForFreebie(name, additionalFreeProduct); + return responseParser.run(response); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } + + private String inputResponseForMembership() { + while (true) { + try { + String response = inputView.askForMembershipDiscount(); + return responseParser.run(response); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } + + private String inputResponseForAdditionalPurchase() { + while (true) { + try { + String response = inputView.askForAdditionalPurchase(); + return responseParser.run(response); + } catch (IllegalArgumentException e) { + outputView.displayErrorMessage(e.getMessage()); + } + } + } +} \ No newline at end of file
Java
ํ•ธ๋“ค๋Ÿฌ ์ƒ๊ฐ์„ ๋ชปํ–ˆ๋„ค์š”! ์ข‹์€ ๋ฐฉ๋ฒ• ์•Œ๋ ค์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,63 @@ +package store.custom.model.order; + +public class OrderedProduct { + private final String name; + private int quantity; + private int totalPrice; + private String promotion; + private int buy; + private int get; + + public OrderedProduct(String name, int quantity, int totalPrice, String promotion, int buy, int get) { + this.name = name; + this.quantity = quantity; + this.totalPrice = totalPrice; + this.promotion = promotion; + this.buy = buy; + this.get = get; + } + + public String getName() { + return name; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public int getTotalPrice() { + return totalPrice; + } + + public void setTotalPrice(int totalPrice) { + this.totalPrice = totalPrice; + } + + public String getPromotion() { + return promotion; + } + + public void setPromotion(String promotion) { + this.promotion = promotion; + } + + public int getBuy() { + return buy; + } + + public void setBuy(int buy) { + this.buy = buy; + } + + public int getGet() { + return get; + } + + public void setGet(int get) { + this.get = get; + } +} \ No newline at end of file
Java
๊ฐ์ฒด์— ๋Œ€ํ•œ ๊ณต๋ถ€๋ฅผ ๋” ํ•ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค! ์•Œ๋ ค์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,35 @@ +package store.custom.model.product; + +public class Product { + private final String name; + private final int price; + private int quantity; + private final String promotion; + + public Product(String name, int price, int quantity, String promotion) { + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotion = promotion; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public String getPromotion() { + return promotion; + } +} \ No newline at end of file
Java
๊ทธ ์ƒ๊ฐ์„ ๋ชปํ•ด๋ดค๋„ค์š”! ํ™•์‹คํžˆ ํ”„๋กœ๋ชจ์…˜ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š”!
@@ -0,0 +1,153 @@ +package store.custom.service.editor; + +import static store.custom.constants.StringConstants.AFTER_PROMOTION_END; +import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START; +import static store.custom.constants.StringConstants.RESPONSE_NO; +import static store.custom.constants.StringConstants.RESPONSE_YES; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.time.LocalDate; +import store.custom.model.PromotionResult.PromotionResult; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Product; +import store.custom.model.product.Products; +import store.custom.model.promotion.Promotion; +import store.custom.model.promotion.Promotions; + +public class OrderSheetEditor { + // ์ฃผ๋ฌธ์„œ ์ดˆ๊ธฐ ์„ค์ •: ๊ฐ€๊ฒฉ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ •๋ณด ์ถ”๊ฐ€ + public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) { + for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) { + setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ€๊ฒฉ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ถ”๊ฐ€ + setPromotionDetails(orderProduct, promotions); // ํ”„๋กœ๋ชจ์…˜ ๊ด€๋ จ ์ •๋ณด ์ถ”๊ฐ€ + } + } + + private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) { + Product product = findOrderedProduct(orderProduct, productCatalog); + if (product != null) { + orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity()); + setPromotionIfExist(orderProduct, product); + } + } + + private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) { + for (Product product : productCatalog.getProducts()) { + if (product.getName().equals(orderProduct.getName())) { + return product; + } + } + return null; + } + + private void setPromotionIfExist(OrderedProduct orderProduct, Product product) { + if (product.getPromotion() != null) { + orderProduct.setPromotion(product.getPromotion()); + } + } + + private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) { + if (orderProduct.getPromotion() != null) { + LocalDate today = DateTimes.now().toLocalDate(); + Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions); + if (promotion != null) { + applyPromotionDates(orderProduct, today, promotion); + applyPromotionBuyAndGet(orderProduct, promotion); + } + } + } + + private Promotion findPromotion(String promotionName, Promotions promotions) { + for (Promotion promotion : promotions.getPromotions()) { + if (promotion.getName().equals(promotionName)) { + return promotion; + } + } + return null; + } + + private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) { + if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) { + orderProduct.setPromotion(BEFORE_PROMOTION_START); + } + + if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) { + orderProduct.setPromotion(AFTER_PROMOTION_END); + } + } + + private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) { + String orderProductPromotion = orderProduct.getPromotion(); + + if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START) + && !orderProductPromotion.equals(AFTER_PROMOTION_END)) { + orderProduct.setBuy(promotion.getBuy()); + orderProduct.setGet(promotion.getGet()); + } + } + + // ์ฃผ๋ฌธ์„œ ์ˆ˜์ •: ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์— ๋‹ค๋ฅธ ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ๊ฒฐ๊ณผ ๋ฐ˜์˜ + public void applyResponseForNoPromotion(String response, PromotionResult promotionResult, + OrderedProduct orderedProduct) { + if (response.equals(RESPONSE_YES)) { + processPurchaseWithNoDiscount(orderedProduct, promotionResult); + } + if (response.equals(RESPONSE_NO)) { + processNonPurchaseWithNoDiscount(orderedProduct, promotionResult); + } + } + + private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + int nonPromotionProductCount = promotionResult.getNonPromotionProductCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } + + private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + int nonPromotionProductCount = promotionResult.getNonPromotionProductCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity(); + orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์ˆ˜๋Ÿ‰ ๊ฐ์†Œ + orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์ˆ˜๋Ÿ‰ ๊ฐ์†Œ์— ๋”ฐ๋ฅธ ์ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ + } + + public void applyResponseForFreeProduct(String response, PromotionResult promotionResult, + OrderedProduct orderedProduct) { + if (response.equals(RESPONSE_YES)) { + processAdditionalPromotionApplied(orderedProduct, promotionResult); + } + if (response.equals(RESPONSE_NO)) { + processAdditionalPromotionNotApplied(orderedProduct, promotionResult); + } + } + + private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1)); + int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity(); + orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€ + orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€์— ๋”ฐ๋ฅธ ์ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ + orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1)); + } + + private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1)); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } + + public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } +} \ No newline at end of file
Java
OrdersheetEditor์˜ ์—ญํ• ์„ ์ด๋ฏธ ์ƒ์„ฑ๋œ ์ฃผ๋ฌธ์„œ์˜ ๋ณ€๊ฒฝ(์ฃผ๋ฌธ์„œ์˜ ์ดˆ๊ธฐ ์„ค์ •, ํ”„๋กœ๋ชจ์…˜ ๋‚ด์šฉ ์ ์šฉ)์œผ๋กœ ์ƒ๊ฐํ•˜๊ณ  ์ž‘์„ฑํ•˜์˜€๋Š”๋ฐ, ๊ฐ์ฒด๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์—ญํ• ์˜ ๋ฒ”์œ„๋ฅผ ์ž˜ ํŒŒ์•…ํ•˜์ง€ ๋ชปํ•ด ๊ธฐ๋ณธ์ ์ธ ๊ฒƒ์„ ์ œ์™ธํ•˜๊ณ  editor์— ๋ชจ๋‘ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ ๋‚ด๋ถ€์—์„œ ์ฑ…์ž„์„ ๊ฐ€์ ธ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์„ ๊ณ ๋ฏผํ•ด ๋ณด์•„์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ข‹์€ ์˜๊ฒฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,153 @@ +package store.custom.service.editor; + +import static store.custom.constants.StringConstants.AFTER_PROMOTION_END; +import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START; +import static store.custom.constants.StringConstants.RESPONSE_NO; +import static store.custom.constants.StringConstants.RESPONSE_YES; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.time.LocalDate; +import store.custom.model.PromotionResult.PromotionResult; +import store.custom.model.order.OrderSheet; +import store.custom.model.order.OrderedProduct; +import store.custom.model.product.Product; +import store.custom.model.product.Products; +import store.custom.model.promotion.Promotion; +import store.custom.model.promotion.Promotions; + +public class OrderSheetEditor { + // ์ฃผ๋ฌธ์„œ ์ดˆ๊ธฐ ์„ค์ •: ๊ฐ€๊ฒฉ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ •๋ณด ์ถ”๊ฐ€ + public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) { + for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) { + setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ€๊ฒฉ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ถ”๊ฐ€ + setPromotionDetails(orderProduct, promotions); // ํ”„๋กœ๋ชจ์…˜ ๊ด€๋ จ ์ •๋ณด ์ถ”๊ฐ€ + } + } + + private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) { + Product product = findOrderedProduct(orderProduct, productCatalog); + if (product != null) { + orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity()); + setPromotionIfExist(orderProduct, product); + } + } + + private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) { + for (Product product : productCatalog.getProducts()) { + if (product.getName().equals(orderProduct.getName())) { + return product; + } + } + return null; + } + + private void setPromotionIfExist(OrderedProduct orderProduct, Product product) { + if (product.getPromotion() != null) { + orderProduct.setPromotion(product.getPromotion()); + } + } + + private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) { + if (orderProduct.getPromotion() != null) { + LocalDate today = DateTimes.now().toLocalDate(); + Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions); + if (promotion != null) { + applyPromotionDates(orderProduct, today, promotion); + applyPromotionBuyAndGet(orderProduct, promotion); + } + } + } + + private Promotion findPromotion(String promotionName, Promotions promotions) { + for (Promotion promotion : promotions.getPromotions()) { + if (promotion.getName().equals(promotionName)) { + return promotion; + } + } + return null; + } + + private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) { + if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) { + orderProduct.setPromotion(BEFORE_PROMOTION_START); + } + + if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) { + orderProduct.setPromotion(AFTER_PROMOTION_END); + } + } + + private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) { + String orderProductPromotion = orderProduct.getPromotion(); + + if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START) + && !orderProductPromotion.equals(AFTER_PROMOTION_END)) { + orderProduct.setBuy(promotion.getBuy()); + orderProduct.setGet(promotion.getGet()); + } + } + + // ์ฃผ๋ฌธ์„œ ์ˆ˜์ •: ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์— ๋‹ค๋ฅธ ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ๊ฒฐ๊ณผ ๋ฐ˜์˜ + public void applyResponseForNoPromotion(String response, PromotionResult promotionResult, + OrderedProduct orderedProduct) { + if (response.equals(RESPONSE_YES)) { + processPurchaseWithNoDiscount(orderedProduct, promotionResult); + } + if (response.equals(RESPONSE_NO)) { + processNonPurchaseWithNoDiscount(orderedProduct, promotionResult); + } + } + + private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + int nonPromotionProductCount = promotionResult.getNonPromotionProductCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } + + private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + int nonPromotionProductCount = promotionResult.getNonPromotionProductCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity(); + orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์ˆ˜๋Ÿ‰ ๊ฐ์†Œ + orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์ˆ˜๋Ÿ‰ ๊ฐ์†Œ์— ๋”ฐ๋ฅธ ์ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ + } + + public void applyResponseForFreeProduct(String response, PromotionResult promotionResult, + OrderedProduct orderedProduct) { + if (response.equals(RESPONSE_YES)) { + processAdditionalPromotionApplied(orderedProduct, promotionResult); + } + if (response.equals(RESPONSE_NO)) { + processAdditionalPromotionNotApplied(orderedProduct, promotionResult); + } + } + + private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1)); + int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity(); + orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€ + orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€์— ๋”ฐ๋ฅธ ์ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ + orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1)); + } + + private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1)); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } + + public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) { + int promotionAppliedCount = promotionResult.getApplicablePromotionCount(); + + orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount); + orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount); + } +} \ No newline at end of file
Java
์นญ์ฐฌํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,369 @@ +# 4์ฃผ ์ฐจ - ํŽธ์˜์  + +--- +## ๊ธฐ๋Šฅ ์š”๊ตฌ์‚ฌํ•ญ + +--- + +๊ตฌ๋งค์ž์˜ ํ• ์ธ ํ˜œํƒ๊ณผ ์žฌ๊ณ  ์ƒํ™ฉ์„ ๊ณ ๋ คํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๊ณ  ์•ˆ๋‚ดํ•˜๋Š” ๊ฒฐ์ œ ์‹œ์Šคํ…œ์„ ๊ตฌํ˜„ํ•œ๋‹ค. + +* ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•œ๋‹ค. + * ์ด๊ตฌ๋งค์•ก์€ ์ƒํ’ˆ๋ณ„ ๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰์„ ๊ณฑํ•˜์—ฌ ๊ณ„์‚ฐํ•˜๋ฉฐ, ํ”„๋กœ๋ชจ์…˜ ๋ฐ ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ์ •์ฑ…์„ ๋ฐ˜์˜ํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ์‚ฐ์ถœํ•œ๋‹ค. +* ๊ตฌ๋งค ๋‚ด์—ญ๊ณผ ์‚ฐ์ถœํ•œ ๊ธˆ์•ก ์ •๋ณด๋ฅผ ์˜์ˆ˜์ฆ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. +* ์˜์ˆ˜์ฆ ์ถœ๋ ฅ ํ›„ ์ถ”๊ฐ€ ๊ตฌ๋งค๋ฅผ ์ง„ํ–‰ํ• ์ง€ ๋˜๋Š” ์ข…๋ฃŒํ• ์ง€๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. +* ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ ``IllegalArgumentException``๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ ํ›„ ๊ทธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋‹ค์‹œ ๋ฐ›๋Š”๋‹ค. + * ``Exception``์ด ์•„๋‹Œ ``IllegalArgumentException``, ``IllegalStateException`` ๋“ฑ๊ณผ ๊ฐ™์€ ๋ช…ํ™•ํ•œ ์œ ํ˜•์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. + + +**์žฌ๊ณ  ๊ด€๋ฆฌ** +* ๊ฐ ์ƒํ’ˆ์˜ ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ๊ณ ๋ คํ•˜์—ฌ ๊ฒฐ์ œ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•œ๋‹ค. +* ๊ณ ๊ฐ์ด ์ƒํ’ˆ์„ ๊ตฌ๋งคํ•  ๋•Œ๋งˆ๋‹ค, ๊ฒฐ์ œ๋œ ์ˆ˜๋Ÿ‰๋งŒํผ ํ•ด๋‹น ์ƒํ’ˆ์˜ ์žฌ๊ณ ์—์„œ ์ฐจ๊ฐํ•˜์—ฌ ์ˆ˜๋Ÿ‰์„ ๊ด€๋ฆฌํ•œ๋‹ค. +* ์žฌ๊ณ ๋ฅผ ์ฐจ๊ฐํ•จ์œผ๋กœ์จ ์‹œ์Šคํ…œ์€ ์ตœ์‹  ์žฌ๊ณ  ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋ฉฐ, ๋‹ค์Œ ๊ณ ๊ฐ์ด ๊ตฌ๋งคํ•  ๋•Œ ์ •ํ™•ํ•œ ์žฌ๊ณ  ์ •๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค. + + +**ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ** +* ์˜ค๋Š˜ ๋‚ ์งœ๊ฐ€ ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„ ๋‚ด์— ํฌํ•จ๋œ ๊ฒฝ์šฐ์—๋งŒ ํ• ์ธ์„ ์ ์šฉํ•œ๋‹ค. +* ํ”„๋กœ๋ชจ์…˜์€ N๊ฐœ ๊ตฌ๋งค ์‹œ 1๊ฐœ ๋ฌด๋ฃŒ ์ฆ์ •(Buy N Get 1 Free)์˜ ํ˜•ํƒœ๋กœ ์ง„ํ–‰๋œ๋‹ค. +* 1+1 ๋˜๋Š” 2+1 ํ”„๋กœ๋ชจ์…˜์ด ๊ฐ๊ฐ ์ง€์ •๋œ ์ƒํ’ˆ์— ์ ์šฉ๋˜๋ฉฐ, ๋™์ผ ์ƒํ’ˆ์— ์—ฌ๋Ÿฌ ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค. +* ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ์€ ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ๋‚ด์—์„œ๋งŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. +* ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„ ์ค‘์ด๋ผ๋ฉด ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์ฐจ๊ฐํ•˜๋ฉฐ, ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•  ๊ฒฝ์šฐ์—๋Š” ์ผ๋ฐ˜ ์žฌ๊ณ ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. +* ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ์ด ๊ฐ€๋Šฅํ•œ ์ƒํ’ˆ์— ๋Œ€ํ•ด ๊ณ ๊ฐ์ด ํ•ด๋‹น ์ˆ˜๋Ÿ‰๋ณด๋‹ค ์ ๊ฒŒ ๊ฐ€์ ธ์˜จ ๊ฒฝ์šฐ, ํ•„์š”ํ•œ ์ˆ˜๋Ÿ‰์„ ์ถ”๊ฐ€๋กœ ๊ฐ€์ ธ์˜ค๋ฉด ํ˜œํƒ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ์„ ์•ˆ๋‚ดํ•œ๋‹ค. +* ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜์—ฌ ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์„ ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ ์—†์ด ๊ฒฐ์ œํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์— ๋Œ€ํ•ด ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ•˜๊ฒŒ ๋จ์„ ์•ˆ๋‚ดํ•œ๋‹ค. + + +**๋ฉค๋ฒ„์‹ญ ํ• ์ธ** +* ๋ฉค๋ฒ„์‹ญ ํšŒ์›์€ ํ”„๋กœ๋ชจ์…˜ ๋ฏธ์ ์šฉ ๊ธˆ์•ก์˜ 30%๋ฅผ ํ• ์ธ๋ฐ›๋Š”๋‹ค. +* ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ํ›„ ๋‚จ์€ ๊ธˆ์•ก์— ๋Œ€ํ•ด ๋ฉค๋ฒ„์‹ญ ํ• ์ธ์„ ์ ์šฉํ•œ๋‹ค. +* ๋ฉค๋ฒ„์‹ญ ํ• ์ธ์˜ ์ตœ๋Œ€ ํ•œ๋„๋Š” 8,000์›์ด๋‹ค. + + +**์˜์ˆ˜์ฆ ์ถœ๋ ฅ** +* ์˜์ˆ˜์ฆ์€ ๊ณ ๊ฐ์˜ ๊ตฌ๋งค ๋‚ด์—ญ๊ณผ ํ• ์ธ์„ ์š”์•ฝํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค. +* ์˜์ˆ˜์ฆ ํ•ญ๋ชฉ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. + * ๊ตฌ๋งค ์ƒํ’ˆ ๋‚ด์—ญ: ๊ตฌ๋งคํ•œ ์ƒํ’ˆ๋ช…, ์ˆ˜๋Ÿ‰, ๊ฐ€๊ฒฉ + * ์ฆ์ • ์ƒํ’ˆ ๋‚ด์—ญ: ํ”„๋กœ๋ชจ์…˜์— ๋”ฐ๋ผ ๋ฌด๋ฃŒ๋กœ ์ œ๊ณต๋œ ์ฆ์ • ์ƒํ’ˆ์˜ ๋ชฉ๋ก + * ๊ธˆ์•ก ์ •๋ณด + * ์ด๊ตฌ๋งค์•ก: ๊ตฌ๋งคํ•œ ์ƒํ’ˆ์˜ ์ด ์ˆ˜๋Ÿ‰๊ณผ ์ด ๊ธˆ์•ก + * ํ–‰์‚ฌํ• ์ธ: ํ”„๋กœ๋ชจ์…˜์— ์˜ํ•ด ํ• ์ธ๋œ ๊ธˆ์•ก + * ๋ฉค๋ฒ„์‹ญํ• ์ธ: ๋ฉค๋ฒ„์‹ญ์— ์˜ํ•ด ์ถ”๊ฐ€๋กœ ํ• ์ธ๋œ ๊ธˆ์•ก + * ๋‚ด์‹ค๋ˆ: ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก +* ์˜์ˆ˜์ฆ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ •๋ ฌํ•˜์—ฌ ๊ณ ๊ฐ์ด ์‰ฝ๊ฒŒ ๊ธˆ์•ก๊ณผ ์ˆ˜๋Ÿ‰์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. + + +### **์ž…์ถœ๋ ฅ ์š”๊ตฌ ์‚ฌํ•ญ** + +**์ž…๋ ฅ** +* ๊ตฌํ˜„์— ํ•„์š”ํ•œ ์ƒํ’ˆ ๋ชฉ๋ก๊ณผ ํ–‰์‚ฌ ๋ชฉ๋ก์„ ํŒŒ์ผ ์ž…์ถœ๋ ฅ์„ ํ†ตํ•ด ๋ถˆ๋Ÿฌ์˜จ๋‹ค. + * src/main/resources/products.md๊ณผ src/main/resources/promotions.md ํŒŒ์ผ์„ ์ด์šฉํ•œ๋‹ค. + * ๋‘ ํŒŒ์ผ ๋ชจ๋‘ ๋‚ด์šฉ์˜ ํ˜•์‹์„ ์œ ์ง€ํ•œ๋‹ค๋ฉด ๊ฐ’์€ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. +* ๊ตฌ๋งคํ•  ์ƒํ’ˆ๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. ์ƒํ’ˆ๋ช…, ์ˆ˜๋Ÿ‰์€ ํ•˜์ดํ”ˆ(-)์œผ๋กœ, ๊ฐœ๋ณ„ ์ƒํ’ˆ์€ ๋Œ€๊ด„ํ˜ธ([])๋กœ ๋ฌถ์–ด ์‰ผํ‘œ(,)๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค. +~~~ +[์ฝœ๋ผ-10],[์‚ฌ์ด๋‹ค-3] +~~~ + +* ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ์ด ๊ฐ€๋Šฅํ•œ ์ƒํ’ˆ์— ๋Œ€ํ•ด ๊ณ ๊ฐ์ด ํ•ด๋‹น ์ˆ˜๋Ÿ‰๋ณด๋‹ค ์ ๊ฒŒ ๊ฐ€์ ธ์˜จ ๊ฒฝ์šฐ, ๊ทธ ์ˆ˜๋Ÿ‰๋งŒํผ ์ถ”๊ฐ€ ์—ฌ๋ถ€๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. + * Y: ์ฆ์ • ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ƒํ’ˆ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. + * N: ์ฆ์ • ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ƒํ’ˆ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค. +~~~ +Y +~~~ + +* ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜์—ฌ ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์„ ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ ์—†์ด ๊ฒฐ์ œํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์— ๋Œ€ํ•ด ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. + * Y: ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์— ๋Œ€ํ•ด ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ•œ๋‹ค. + * N: ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ•ด์•ผํ•˜๋Š” ์ˆ˜๋Ÿ‰๋งŒํผ ์ œ์™ธํ•œ ํ›„ ๊ฒฐ์ œ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. +~~~ +Y +~~~ + +* ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ์ ์šฉ ์—ฌ๋ถ€๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. + * Y: ๋ฉค๋ฒ„์‹ญ ํ• ์ธ์„ ์ ์šฉํ•œ๋‹ค. + * N: ๋ฉค๋ฒ„์‹ญ ํ• ์ธ์„ ์ ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. +~~~ +Y +~~~ + +* ์ถ”๊ฐ€ ๊ตฌ๋งค ์—ฌ๋ถ€๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. + * Y: ์žฌ๊ณ ๊ฐ€ ์—…๋ฐ์ดํŠธ๋œ ์ƒํ’ˆ ๋ชฉ๋ก์„ ํ™•์ธ ํ›„ ์ถ”๊ฐ€๋กœ ๊ตฌ๋งค๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. + * N: ๊ตฌ๋งค๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค. +~~~ +Y +~~~ + +**์ถœ๋ ฅ** +* ํ™˜์˜ ์ธ์‚ฌ์™€ ํ•จ๊ป˜ ์ƒํ’ˆ๋ช…, ๊ฐ€๊ฒฉ, ํ”„๋กœ๋ชจ์…˜ ์ด๋ฆ„, ์žฌ๊ณ ๋ฅผ ์•ˆ๋‚ดํ•œ๋‹ค. ๋งŒ์•ฝ ์žฌ๊ณ ๊ฐ€ 0๊ฐœ๋ผ๋ฉด ์žฌ๊ณ  ์—†์Œ์„ ์ถœ๋ ฅํ•œ๋‹ค. +~~~ +์•ˆ๋…•ํ•˜์„ธ์š”. WํŽธ์˜์ ์ž…๋‹ˆ๋‹ค. +ํ˜„์žฌ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค. + +- ์ฝœ๋ผ 1,000์› 10๊ฐœ ํƒ„์‚ฐ2+1 +- ์ฝœ๋ผ 1,000์› 10๊ฐœ +- ์‚ฌ์ด๋‹ค 1,000์› 8๊ฐœ ํƒ„์‚ฐ2+1 +- ์‚ฌ์ด๋‹ค 1,000์› 7๊ฐœ +- ์˜ค๋ Œ์ง€์ฃผ์Šค 1,800์› 9๊ฐœ MD์ถ”์ฒœ์ƒํ’ˆ +- ์˜ค๋ Œ์ง€์ฃผ์Šค 1,800์› ์žฌ๊ณ  ์—†์Œ +- ํƒ„์‚ฐ์ˆ˜ 1,200์› 5๊ฐœ ํƒ„์‚ฐ2+1 +- ํƒ„์‚ฐ์ˆ˜ 1,200์› ์žฌ๊ณ  ์—†์Œ +- ๋ฌผ 500์› 10๊ฐœ +- ๋น„ํƒ€๋ฏผ์›Œํ„ฐ 1,500์› 6๊ฐœ +- ๊ฐ์ž์นฉ 1,500์› 5๊ฐœ ๋ฐ˜์งํ• ์ธ +- ๊ฐ์ž์นฉ 1,500์› 5๊ฐœ +- ์ดˆ์ฝ”๋ฐ” 1,200์› 5๊ฐœ MD์ถ”์ฒœ์ƒํ’ˆ +- ์ดˆ์ฝ”๋ฐ” 1,200์› 5๊ฐœ +- ์—๋„ˆ์ง€๋ฐ” 2,000์› 5๊ฐœ +- ์ •์‹๋„์‹œ๋ฝ 6,400์› 8๊ฐœ +- ์ปต๋ผ๋ฉด 1,700์› 1๊ฐœ MD์ถ”์ฒœ์ƒํ’ˆ +- ์ปต๋ผ๋ฉด 1,700์› 10๊ฐœ + +๊ตฌ๋งคํ•˜์‹ค ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (์˜ˆ: [์‚ฌ์ด๋‹ค-2],[๊ฐ์ž์นฉ-1]) +~~~ + +* ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ์ด ๊ฐ€๋Šฅํ•œ ์ƒํ’ˆ์— ๋Œ€ํ•ด ๊ณ ๊ฐ์ด ํ•ด๋‹น ์ˆ˜๋Ÿ‰๋งŒํผ ๊ฐ€์ ธ์˜ค์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ, ํ˜œํƒ์— ๋Œ€ํ•œ ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +~~~ +ํ˜„์žฌ {์ƒํ’ˆ๋ช…}์€(๋Š”) 1๊ฐœ๋ฅผ ๋ฌด๋ฃŒ๋กœ ๋” ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N) +~~~ + +* ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜์—ฌ ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์„ ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ ์—†์ด ๊ฒฐ์ œํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์— ๋Œ€ํ•ด ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ• ์ง€ ์—ฌ๋ถ€์— ๋Œ€ํ•œ ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +~~~ +ํ˜„์žฌ {์ƒํ’ˆ๋ช…} {์ˆ˜๋Ÿ‰}๊ฐœ๋Š” ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ๊ตฌ๋งคํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N) +~~~ + +* ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ์ ์šฉ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์•ˆ๋‚ด ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +~~~ +๋ฉค๋ฒ„์‹ญ ํ• ์ธ์„ ๋ฐ›์œผ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N) +~~~ + +* ๊ตฌ๋งค ์ƒํ’ˆ ๋‚ด์—ญ, ์ฆ์ • ์ƒํ’ˆ ๋‚ด์—ญ, ๊ธˆ์•ก ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +~~~ +===========W ํŽธ์˜์ ============= +์ƒํ’ˆ๋ช… ์ˆ˜๋Ÿ‰ ๊ธˆ์•ก +์ฝœ๋ผ 3 3,000 +์—๋„ˆ์ง€๋ฐ” 5 10,000 +===========์ฆ ์ •============= +์ฝœ๋ผ 1 +============================== +์ด๊ตฌ๋งค์•ก 8 13,000 +ํ–‰์‚ฌํ• ์ธ -1,000 +๋ฉค๋ฒ„์‹ญํ• ์ธ -3,000 +๋‚ด์‹ค๋ˆ 9,000 +~~~ + +* ์ถ”๊ฐ€ ๊ตฌ๋งค ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์•ˆ๋‚ด ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +~~~ +๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋งคํ•˜๊ณ  ์‹ถ์€ ๋‹ค๋ฅธ ์ƒํ’ˆ์ด ์žˆ๋‚˜์š”? (Y/N) +~~~ + +* ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ, "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์™€ ํ•จ๊ป˜ ์ƒํ™ฉ์— ๋งž๋Š” ์•ˆ๋‚ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. + * ๊ตฌ๋งคํ•  ์ƒํ’ˆ๊ณผ ์ˆ˜๋Ÿ‰ ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒฝ์šฐ: ```[ERROR] ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ํ˜•์‹์œผ๋กœ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.``` + * ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒํ’ˆ์„ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ: ```[ERROR] ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.``` + * ๊ตฌ๋งค ์ˆ˜๋Ÿ‰์ด ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•œ ๊ฒฝ์šฐ: ```[ERROR] ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•˜์—ฌ ๊ตฌ๋งคํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.``` + * ๊ธฐํƒ€ ์ž˜๋ชป๋œ ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ: ```[ERROR] ์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.``` + + +**์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ** +~~~ +์•ˆ๋…•ํ•˜์„ธ์š”. WํŽธ์˜์ ์ž…๋‹ˆ๋‹ค. +ํ˜„์žฌ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค. + +- ์ฝœ๋ผ 1,000์› 10๊ฐœ ํƒ„์‚ฐ2+1 +- ์ฝœ๋ผ 1,000์› 10๊ฐœ +- ์‚ฌ์ด๋‹ค 1,000์› 8๊ฐœ ํƒ„์‚ฐ2+1 +- ์‚ฌ์ด๋‹ค 1,000์› 7๊ฐœ +- ์˜ค๋ Œ์ง€์ฃผ์Šค 1,800์› 9๊ฐœ MD์ถ”์ฒœ์ƒํ’ˆ +- ์˜ค๋ Œ์ง€์ฃผ์Šค 1,800์› ์žฌ๊ณ  ์—†์Œ +- ํƒ„์‚ฐ์ˆ˜ 1,200์› 5๊ฐœ ํƒ„์‚ฐ2+1 +- ํƒ„์‚ฐ์ˆ˜ 1,200์› ์žฌ๊ณ  ์—†์Œ +- ๋ฌผ 500์› 10๊ฐœ +- ๋น„ํƒ€๋ฏผ์›Œํ„ฐ 1,500์› 6๊ฐœ +- ๊ฐ์ž์นฉ 1,500์› 5๊ฐœ ๋ฐ˜์งํ• ์ธ +- ๊ฐ์ž์นฉ 1,500์› 5๊ฐœ +- ์ดˆ์ฝ”๋ฐ” 1,200์› 5๊ฐœ MD์ถ”์ฒœ์ƒํ’ˆ +- ์ดˆ์ฝ”๋ฐ” 1,200์› 5๊ฐœ +- ์—๋„ˆ์ง€๋ฐ” 2,000์› 5๊ฐœ +- ์ •์‹๋„์‹œ๋ฝ 6,400์› 8๊ฐœ +- ์ปต๋ผ๋ฉด 1,700์› 1๊ฐœ MD์ถ”์ฒœ์ƒํ’ˆ +- ์ปต๋ผ๋ฉด 1,700์› 10๊ฐœ + +๊ตฌ๋งคํ•˜์‹ค ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (์˜ˆ: [์‚ฌ์ด๋‹ค-2],[๊ฐ์ž์นฉ-1]) +[์ฝœ๋ผ-3],[์—๋„ˆ์ง€๋ฐ”-5] + +๋ฉค๋ฒ„์‹ญ ํ• ์ธ์„ ๋ฐ›์œผ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N) +Y + +===========W ํŽธ์˜์ ============= +์ƒํ’ˆ๋ช… ์ˆ˜๋Ÿ‰ ๊ธˆ์•ก +์ฝœ๋ผ 3 3,000 +์—๋„ˆ์ง€๋ฐ” 5 10,000 +===========์ฆ ์ •============= +์ฝœ๋ผ 1 +============================== +์ด๊ตฌ๋งค์•ก 8 13,000 +ํ–‰์‚ฌํ• ์ธ -1,000 +๋ฉค๋ฒ„์‹ญํ• ์ธ -3,000 +๋‚ด์‹ค๋ˆ 9,000 + +๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋งคํ•˜๊ณ  ์‹ถ์€ ๋‹ค๋ฅธ ์ƒํ’ˆ์ด ์žˆ๋‚˜์š”? (Y/N) +Y + +์•ˆ๋…•ํ•˜์„ธ์š”. WํŽธ์˜์ ์ž…๋‹ˆ๋‹ค. +ํ˜„์žฌ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค. + +- ์ฝœ๋ผ 1,000์› 7๊ฐœ ํƒ„์‚ฐ2+1 +- ์ฝœ๋ผ 1,000์› 10๊ฐœ +- ์‚ฌ์ด๋‹ค 1,000์› 8๊ฐœ ํƒ„์‚ฐ2+1 +- ์‚ฌ์ด๋‹ค 1,000์› 7๊ฐœ +- ์˜ค๋ Œ์ง€์ฃผ์Šค 1,800์› 9๊ฐœ MD์ถ”์ฒœ์ƒํ’ˆ +- ์˜ค๋ Œ์ง€์ฃผ์Šค 1,800์› ์žฌ๊ณ  ์—†์Œ +- ํƒ„์‚ฐ์ˆ˜ 1,200์› 5๊ฐœ ํƒ„์‚ฐ2+1 +- ํƒ„์‚ฐ์ˆ˜ 1,200์› ์žฌ๊ณ  ์—†์Œ +- ๋ฌผ 500์› 10๊ฐœ +- ๋น„ํƒ€๋ฏผ์›Œํ„ฐ 1,500์› 6๊ฐœ +- ๊ฐ์ž์นฉ 1,500์› 5๊ฐœ ๋ฐ˜์งํ• ์ธ +- ๊ฐ์ž์นฉ 1,500์› 5๊ฐœ +- ์ดˆ์ฝ”๋ฐ” 1,200์› 5๊ฐœ MD์ถ”์ฒœ์ƒํ’ˆ +- ์ดˆ์ฝ”๋ฐ” 1,200์› 5๊ฐœ +- ์—๋„ˆ์ง€๋ฐ” 2,000์› ์žฌ๊ณ  ์—†์Œ +- ์ •์‹๋„์‹œ๋ฝ 6,400์› 8๊ฐœ +- ์ปต๋ผ๋ฉด 1,700์› 1๊ฐœ MD์ถ”์ฒœ์ƒํ’ˆ +- ์ปต๋ผ๋ฉด 1,700์› 10๊ฐœ + +๊ตฌ๋งคํ•˜์‹ค ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (์˜ˆ: [์‚ฌ์ด๋‹ค-2],[๊ฐ์ž์นฉ-1]) +[์ฝœ๋ผ-10] + +ํ˜„์žฌ ์ฝœ๋ผ 4๊ฐœ๋Š” ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ๊ตฌ๋งคํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N) +Y + +๋ฉค๋ฒ„์‹ญ ํ• ์ธ์„ ๋ฐ›์œผ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N) +N + +===========W ํŽธ์˜์ ============= +์ƒํ’ˆ๋ช… ์ˆ˜๋Ÿ‰ ๊ธˆ์•ก +์ฝœ๋ผ 10 10,000 +===========์ฆ ์ •============= +์ฝœ๋ผ 2 +============================== +์ด๊ตฌ๋งค์•ก 10 10,000 +ํ–‰์‚ฌํ• ์ธ -2,000 +๋ฉค๋ฒ„์‹ญํ• ์ธ -0 +๋‚ด์‹ค๋ˆ 8,000 + +๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋งคํ•˜๊ณ  ์‹ถ์€ ๋‹ค๋ฅธ ์ƒํ’ˆ์ด ์žˆ๋‚˜์š”? (Y/N) +Y + +์•ˆ๋…•ํ•˜์„ธ์š”. WํŽธ์˜์ ์ž…๋‹ˆ๋‹ค. +ํ˜„์žฌ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค. + +- ์ฝœ๋ผ 1,000์› ์žฌ๊ณ  ์—†์Œ ํƒ„์‚ฐ2+1 +- ์ฝœ๋ผ 1,000์› 7๊ฐœ +- ์‚ฌ์ด๋‹ค 1,000์› 8๊ฐœ ํƒ„์‚ฐ2+1 +- ์‚ฌ์ด๋‹ค 1,000์› 7๊ฐœ +- ์˜ค๋ Œ์ง€์ฃผ์Šค 1,800์› 9๊ฐœ MD์ถ”์ฒœ์ƒํ’ˆ +- ์˜ค๋ Œ์ง€์ฃผ์Šค 1,800์› ์žฌ๊ณ  ์—†์Œ +- ํƒ„์‚ฐ์ˆ˜ 1,200์› 5๊ฐœ ํƒ„์‚ฐ2+1 +- ํƒ„์‚ฐ์ˆ˜ 1,200์› ์žฌ๊ณ  ์—†์Œ +- ๋ฌผ 500์› 10๊ฐœ +- ๋น„ํƒ€๋ฏผ์›Œํ„ฐ 1,500์› 6๊ฐœ +- ๊ฐ์ž์นฉ 1,500์› 5๊ฐœ ๋ฐ˜์งํ• ์ธ +- ๊ฐ์ž์นฉ 1,500์› 5๊ฐœ +- ์ดˆ์ฝ”๋ฐ” 1,200์› 5๊ฐœ MD์ถ”์ฒœ์ƒํ’ˆ +- ์ดˆ์ฝ”๋ฐ” 1,200์› 5๊ฐœ +- ์—๋„ˆ์ง€๋ฐ” 2,000์› ์žฌ๊ณ  ์—†์Œ +- ์ •์‹๋„์‹œ๋ฝ 6,400์› 8๊ฐœ +- ์ปต๋ผ๋ฉด 1,700์› 1๊ฐœ MD์ถ”์ฒœ์ƒํ’ˆ +- ์ปต๋ผ๋ฉด 1,700์› 10๊ฐœ + +๊ตฌ๋งคํ•˜์‹ค ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (์˜ˆ: [์‚ฌ์ด๋‹ค-2],[๊ฐ์ž์นฉ-1]) +[์˜ค๋ Œ์ง€์ฃผ์Šค-1] + +ํ˜„์žฌ ์˜ค๋ Œ์ง€์ฃผ์Šค์€(๋Š”) 1๊ฐœ๋ฅผ ๋ฌด๋ฃŒ๋กœ ๋” ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N) +Y + +๋ฉค๋ฒ„์‹ญ ํ• ์ธ์„ ๋ฐ›์œผ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N) +Y + +===========W ํŽธ์˜์ ============= +์ƒํ’ˆ๋ช… ์ˆ˜๋Ÿ‰ ๊ธˆ์•ก +์˜ค๋ Œ์ง€์ฃผ์Šค 2 3,600 +===========์ฆ ์ •============= +์˜ค๋ Œ์ง€์ฃผ์Šค 1 +============================== +์ด๊ตฌ๋งค์•ก 2 3,600 +ํ–‰์‚ฌํ• ์ธ -1,800 +๋ฉค๋ฒ„์‹ญํ• ์ธ -0 +๋‚ด์‹ค๋ˆ 1,800 + +๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋งคํ•˜๊ณ  ์‹ถ์€ ๋‹ค๋ฅธ ์ƒํ’ˆ์ด ์žˆ๋‚˜์š”? (Y/N) +N +~~~ + + +## ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ ์‚ฌํ•ญ 1 + +---- +* JDK 21 ๋ฒ„์ „์—์„œ ์‹คํ–‰ ๊ฐ€๋Šฅํ•ด์•ผ ํ•œ๋‹ค. +* ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์˜ ์‹œ์ž‘์ ์€ Application์˜ main()์ด๋‹ค. +* build.gradle ํŒŒ์ผ์€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ์ œ๊ณต๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด์™ธ์˜ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. +* ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ ์‹œ System.exit()๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค. +* ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ ์‚ฌํ•ญ์—์„œ ๋‹ฌ๋ฆฌ ๋ช…์‹œํ•˜์ง€ ์•Š๋Š” ํ•œ ํŒŒ์ผ, ํŒจํ‚ค์ง€ ๋“ฑ์˜ ์ด๋ฆ„์„ ๋ฐ”๊พธ๊ฑฐ๋‚˜ ์ด๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. +* ์ž๋ฐ” ์ฝ”๋“œ ์ปจ๋ฒค์…˜์„ ์ง€ํ‚ค๋ฉด์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•œ๋‹ค. + * ๊ธฐ๋ณธ์ ์œผ๋กœ Java Style Guide๋ฅผ ์›์น™์œผ๋กœ ํ•œ๋‹ค. + + +## ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ ์‚ฌํ•ญ 2 + +---- +* indent(์ธ๋ดํŠธ, ๋“ค์—ฌ์“ฐ๊ธฐ) depth๋ฅผ 3์ด ๋„˜์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. 2๊นŒ์ง€๋งŒ ํ—ˆ์šฉํ•œ๋‹ค. + * ์˜ˆ๋ฅผ ๋“ค์–ด while๋ฌธ ์•ˆ์— if๋ฌธ์ด ์žˆ์œผ๋ฉด ๋“ค์—ฌ์“ฐ๊ธฐ๋Š” 2์ด๋‹ค. + * ํžŒํŠธ: indent(์ธ๋ดํŠธ, ๋“ค์—ฌ์“ฐ๊ธฐ) depth๋ฅผ ์ค„์ด๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์€ ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)๋ฅผ ๋ถ„๋ฆฌํ•˜๋ฉด ๋œ๋‹ค. +* 3ํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. +* ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)๊ฐ€ ํ•œ ๊ฐ€์ง€ ์ผ๋งŒ ํ•˜๋„๋ก ์ตœ๋Œ€ํ•œ ์ž‘๊ฒŒ ๋งŒ๋“ค์–ด๋ผ. +* JUnit 5์™€ AssertJ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ฆฌํ•œ ๊ธฐ๋Šฅ ๋ชฉ๋ก์ด ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋กœ ํ™•์ธํ•œ๋‹ค. + * ํ…Œ์ŠคํŠธ ๋„๊ตฌ ์‚ฌ์šฉ๋ฒ•์ด ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด ์•„๋ž˜ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ•™์Šตํ•œ ํ›„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. + * [JUnit 5 User Guide](https://junit.org/junit5/docs/current/user-guide/) + * [AssertJ User Guide](https://assertj.github.io/doc/) + * [AssertJ Exception Assertions](https://www.baeldung.com/assertj-exception-assertion) + * [Guide to JUnit 5 Parameterized Tests](https://www.baeldung.com/parameterized-tests-junit-5) + + +## ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ ์‚ฌํ•ญ 3 + +--- +* else ์˜ˆ์•ฝ์–ด๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. + * else๋ฅผ ์“ฐ์ง€ ๋ง๋ผ๊ณ  ํ•˜๋‹ˆ switch/case๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ switch/case๋„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. + * ํžŒํŠธ: if ์กฐ๊ฑด์ ˆ์—์„œ ๊ฐ’์„ returnํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด else๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. +* Java Enum์„ ์ ์šฉํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•œ๋‹ค. +* ๊ตฌํ˜„ํ•œ ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ๋‹จ, UI(System.out, System.in, Scanner) ๋กœ์ง์€ ์ œ์™ธํ•œ๋‹ค. + + +## ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ ์‚ฌํ•ญ 4 + +---- +* ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)์˜ ๊ธธ์ด๊ฐ€ 10๋ผ์ธ์„ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. + * ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)๊ฐ€ ํ•œ ๊ฐ€์ง€ ์ผ๋งŒ ์ž˜ ํ•˜๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. +* ์ž…์ถœ๋ ฅ์„ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋ณ„๋„๋กœ ๊ตฌํ˜„ํ•œ๋‹ค. + * ์•„๋ž˜ ``InputView``, ``OutputView`` ํด๋ž˜์Šค๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž…์ถœ๋ ฅ ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. + * ํด๋ž˜์Šค ์ด๋ฆ„, ๋ฉ”์†Œ๋“œ ๋ฐ˜ํ™˜ ์œ ํ˜•, ์‹œ๊ทธ๋‹ˆ์ฒ˜ ๋“ฑ์€ ์ž์œ ๋กญ๊ฒŒ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. +~~~ +public class InputView { + public String readItem() { + System.out.println("๊ตฌ๋งคํ•˜์‹ค ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (์˜ˆ: [์‚ฌ์ด๋‹ค-2],[๊ฐ์ž์นฉ-1])"); + String input = Console.readLine(); + // ... + } + // ... +} +~~~ +~~~ +public class OutputView { + public void printProducts() { + System.out.println("- ์ฝœ๋ผ 1,000์› 10๊ฐœ ํƒ„์‚ฐ2+1"); + // ... + } + // ... +} +~~~ + + +## ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ +* ``camp.nextstep.edu.missionutils``์—์„œ ์ œ๊ณตํ•˜๋Š” ``DateTimes`` ๋ฐ ``Console`` API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. + * ํ˜„์žฌ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด ``camp.nextstep.edu.missionutils.DateTimes``์˜ ``now()``๋ฅผ ํ™œ์šฉํ•œ๋‹ค. + * ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•˜๋Š” ๊ฐ’์€ ``camp.nextstep.edu.missionutils.Console``์˜ ``readLine()``์„ ํ™œ์šฉํ•œ๋‹ค.
Unknown
์ด๋ฒˆ ๋ฏธ์…˜์ด ์ •๋ง ๋ณต์žกํ•ด์„œ ์–ด๋ ค์› ๋Š”๋ฐ ์ €๋„ ์„ค๊ณ„๋ฅผ ์ง„ํ–‰ํ• ๋•Œ ํ”Œ๋กœ์šฐ ์ฐจํŠธ์™€ ๋น„์Šทํ•˜๊ฒŒ ๋™์ž‘ ๊ณผ์ •์„ ์ •๋ฆฌํ•˜๊ณ  ๊ฐœ๋ฐœ์„ ์ง„ํ–‰ํ–ˆ๋˜๊ฒƒ ๊ฐ™์€๋ฐ ๋ฌธ์ƒํœ˜๋‹˜๋„ ๋น„์Šทํ•˜๊ฒŒ ์ง„ํ–‰ํ•œ ๊ฒƒ ๋ณด๊ณ  ๋†€๋ž๋„ค์š”
@@ -0,0 +1,137 @@ +package store.controller; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.util.ArrayList; +import java.util.List; +import store.domain.BuyProductParser; +import store.domain.BuyProducts; +import store.domain.FileReader; +import store.domain.CustomerReceipt; +import store.domain.Products; +import store.domain.Promotions; +import store.domain.Receipt; +import store.domain.service.MemberShipCalculator; +import store.domain.service.PromotionCalculator; +import store.domain.service.PromotionChecker; +import store.domain.vo.MemberShip; +import store.view.InputView; +import store.view.OutputView; +import store.view.dto.request.PromotionRequest; +import store.view.dto.response.ErrorResponse; +import store.view.dto.request.MemberShipRequest; +import store.view.dto.response.PromotionResponse; + +public class StoreController { + + private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md"; + private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md"; + private static final String YES = "Y"; + + private final MemberShipCalculator memberShipCalculator; + private final PromotionCalculator promotionCalculator; + private final OutputView outputView; + private final InputView inputView; + + public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator, + OutputView outputView, InputView inputView) { + this.memberShipCalculator = memberShipCalculator; + this.promotionCalculator = promotionCalculator; + this.outputView = outputView; + this.inputView = inputView; + } + + public void run() { + boolean continueShopping = true; + + Promotions promotions = getPromotions(); + Products products = getProducts(promotions); + + while (continueShopping) { + showInventory(products); + BuyProducts buyProducts = buyProducts(promotions, products); + CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts); + showReceipt(customerReceipt); + continueShopping = inputView.continueShopping() + .equals(YES); + } + + } + + private Promotions getPromotions() { + FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH); + return new Promotions(promotionsContents.fileContents(), DateTimes.now()); + } + + private Products getProducts(Promotions promotions) { + FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH); + return new Products(productsContents.fileContents(), promotions); + } + + private void showInventory(Products products) { + outputView.welcomeStore(); + products.displayProducts() + .forEach(product -> outputView.showInventory( + product.getName(), + product.getPrice(), + product.showQuantity(), + product.getPromotionType() + )); + } + + private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) { + List<PromotionRequest> requests = checkPromotionCase(products, buyProducts); + + Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests); + MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES)); + MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request); + return new CustomerReceipt(receipt, memberShip); + } + + private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) { + PromotionChecker promotionChecker = new PromotionChecker(); + List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts); + List<PromotionRequest> requests = new ArrayList<>(); + for (PromotionResponse response : responses) { + checkNoPromotionRequest(response, requests); + checkFreePromotionRequest(response, requests); + } + return requests; + } + + private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isFreePromotion()) { + boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(), + response.freePromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), false, isFreePromotion)); + } + } + + private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isNoPromotion()) { + boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(), + response.noPromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), isNoPromotion, false)); + } + } + + private void showReceipt(CustomerReceipt customerReceipt) { + outputView.guideBuyProducts(); + outputView.showBuyProducts(customerReceipt.showBuyProducts()); + outputView.guideFreeGetQuantity(); + outputView.showFreeQuantity(customerReceipt.showFreeProductInfo()); + outputView.showCalculateResult(customerReceipt.showCalculateInfo()); + } + + private BuyProducts buyProducts(Promotions promotions, Products products) { + outputView.getCustomerProducts(); + while (true) { + try { + BuyProductParser parser = new BuyProductParser(inputView.buyProducts()); + return new BuyProducts(parser.getProducts(), promotions, products); + } catch (IllegalArgumentException e) { + ErrorResponse errorResponse = new ErrorResponse(e.getMessage()); + outputView.showError(errorResponse); + } + } + } +}
Java
๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ์ฝ”๋“œ๋ฅผ ๋ชฐ๋ž˜๋ชฐ๋ž˜ ๋ดค์„๋•Œ ์ด๋Ÿฐ์”ฉ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์„œ ๋ณ€์ˆ˜๋ฅผ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋”๊ตฐ์š”. ์‹œ๊ฐ์— ๋”ฐ๋ผ ์ข‹์„์ˆ˜๋„ ์žˆ๊ณ  ์•ˆ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ฝ”๋“œ๋ณด๋‹ค๊ฐ€ ์ƒ๊ฐ๋‚˜์„œ ๋„ฃ์–ด๋‘˜๊ฒŒ์š” ```suggestion do { showInventory(products); BuyProducts buyProducts = buyProducts(promotions, products); CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts); showReceipt(customerReceipt); } while (inputView.continueShopping() .equals(YES)) ```
@@ -0,0 +1,137 @@ +package store.controller; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.util.ArrayList; +import java.util.List; +import store.domain.BuyProductParser; +import store.domain.BuyProducts; +import store.domain.FileReader; +import store.domain.CustomerReceipt; +import store.domain.Products; +import store.domain.Promotions; +import store.domain.Receipt; +import store.domain.service.MemberShipCalculator; +import store.domain.service.PromotionCalculator; +import store.domain.service.PromotionChecker; +import store.domain.vo.MemberShip; +import store.view.InputView; +import store.view.OutputView; +import store.view.dto.request.PromotionRequest; +import store.view.dto.response.ErrorResponse; +import store.view.dto.request.MemberShipRequest; +import store.view.dto.response.PromotionResponse; + +public class StoreController { + + private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md"; + private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md"; + private static final String YES = "Y"; + + private final MemberShipCalculator memberShipCalculator; + private final PromotionCalculator promotionCalculator; + private final OutputView outputView; + private final InputView inputView; + + public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator, + OutputView outputView, InputView inputView) { + this.memberShipCalculator = memberShipCalculator; + this.promotionCalculator = promotionCalculator; + this.outputView = outputView; + this.inputView = inputView; + } + + public void run() { + boolean continueShopping = true; + + Promotions promotions = getPromotions(); + Products products = getProducts(promotions); + + while (continueShopping) { + showInventory(products); + BuyProducts buyProducts = buyProducts(promotions, products); + CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts); + showReceipt(customerReceipt); + continueShopping = inputView.continueShopping() + .equals(YES); + } + + } + + private Promotions getPromotions() { + FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH); + return new Promotions(promotionsContents.fileContents(), DateTimes.now()); + } + + private Products getProducts(Promotions promotions) { + FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH); + return new Products(productsContents.fileContents(), promotions); + } + + private void showInventory(Products products) { + outputView.welcomeStore(); + products.displayProducts() + .forEach(product -> outputView.showInventory( + product.getName(), + product.getPrice(), + product.showQuantity(), + product.getPromotionType() + )); + } + + private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) { + List<PromotionRequest> requests = checkPromotionCase(products, buyProducts); + + Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests); + MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES)); + MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request); + return new CustomerReceipt(receipt, memberShip); + } + + private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) { + PromotionChecker promotionChecker = new PromotionChecker(); + List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts); + List<PromotionRequest> requests = new ArrayList<>(); + for (PromotionResponse response : responses) { + checkNoPromotionRequest(response, requests); + checkFreePromotionRequest(response, requests); + } + return requests; + } + + private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isFreePromotion()) { + boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(), + response.freePromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), false, isFreePromotion)); + } + } + + private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isNoPromotion()) { + boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(), + response.noPromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), isNoPromotion, false)); + } + } + + private void showReceipt(CustomerReceipt customerReceipt) { + outputView.guideBuyProducts(); + outputView.showBuyProducts(customerReceipt.showBuyProducts()); + outputView.guideFreeGetQuantity(); + outputView.showFreeQuantity(customerReceipt.showFreeProductInfo()); + outputView.showCalculateResult(customerReceipt.showCalculateInfo()); + } + + private BuyProducts buyProducts(Promotions promotions, Products products) { + outputView.getCustomerProducts(); + while (true) { + try { + BuyProductParser parser = new BuyProductParser(inputView.buyProducts()); + return new BuyProducts(parser.getProducts(), promotions, products); + } catch (IllegalArgumentException e) { + ErrorResponse errorResponse = new ErrorResponse(e.getMessage()); + outputView.showError(errorResponse); + } + } + } +}
Java
๋’ค์— .equals(YES)๋ผ๊ณ  ํ•˜๋Š” ๊ฒฝ์šฐ ํŒ๋‹จ์„ controller์—์„œ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” view์—์„œ YES๋ฉด true๋ฅผ ๋„ฃ์–ด controller์˜ ์ฑ…์ž„์„ ์ค„์—ฌ์ฃผ๋Š”๊ฒƒ์— ๋Œ€ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,137 @@ +package store.controller; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.util.ArrayList; +import java.util.List; +import store.domain.BuyProductParser; +import store.domain.BuyProducts; +import store.domain.FileReader; +import store.domain.CustomerReceipt; +import store.domain.Products; +import store.domain.Promotions; +import store.domain.Receipt; +import store.domain.service.MemberShipCalculator; +import store.domain.service.PromotionCalculator; +import store.domain.service.PromotionChecker; +import store.domain.vo.MemberShip; +import store.view.InputView; +import store.view.OutputView; +import store.view.dto.request.PromotionRequest; +import store.view.dto.response.ErrorResponse; +import store.view.dto.request.MemberShipRequest; +import store.view.dto.response.PromotionResponse; + +public class StoreController { + + private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md"; + private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md"; + private static final String YES = "Y"; + + private final MemberShipCalculator memberShipCalculator; + private final PromotionCalculator promotionCalculator; + private final OutputView outputView; + private final InputView inputView; + + public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator, + OutputView outputView, InputView inputView) { + this.memberShipCalculator = memberShipCalculator; + this.promotionCalculator = promotionCalculator; + this.outputView = outputView; + this.inputView = inputView; + } + + public void run() { + boolean continueShopping = true; + + Promotions promotions = getPromotions(); + Products products = getProducts(promotions); + + while (continueShopping) { + showInventory(products); + BuyProducts buyProducts = buyProducts(promotions, products); + CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts); + showReceipt(customerReceipt); + continueShopping = inputView.continueShopping() + .equals(YES); + } + + } + + private Promotions getPromotions() { + FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH); + return new Promotions(promotionsContents.fileContents(), DateTimes.now()); + } + + private Products getProducts(Promotions promotions) { + FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH); + return new Products(productsContents.fileContents(), promotions); + } + + private void showInventory(Products products) { + outputView.welcomeStore(); + products.displayProducts() + .forEach(product -> outputView.showInventory( + product.getName(), + product.getPrice(), + product.showQuantity(), + product.getPromotionType() + )); + } + + private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) { + List<PromotionRequest> requests = checkPromotionCase(products, buyProducts); + + Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests); + MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES)); + MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request); + return new CustomerReceipt(receipt, memberShip); + } + + private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) { + PromotionChecker promotionChecker = new PromotionChecker(); + List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts); + List<PromotionRequest> requests = new ArrayList<>(); + for (PromotionResponse response : responses) { + checkNoPromotionRequest(response, requests); + checkFreePromotionRequest(response, requests); + } + return requests; + } + + private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isFreePromotion()) { + boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(), + response.freePromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), false, isFreePromotion)); + } + } + + private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isNoPromotion()) { + boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(), + response.noPromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), isNoPromotion, false)); + } + } + + private void showReceipt(CustomerReceipt customerReceipt) { + outputView.guideBuyProducts(); + outputView.showBuyProducts(customerReceipt.showBuyProducts()); + outputView.guideFreeGetQuantity(); + outputView.showFreeQuantity(customerReceipt.showFreeProductInfo()); + outputView.showCalculateResult(customerReceipt.showCalculateInfo()); + } + + private BuyProducts buyProducts(Promotions promotions, Products products) { + outputView.getCustomerProducts(); + while (true) { + try { + BuyProductParser parser = new BuyProductParser(inputView.buyProducts()); + return new BuyProducts(parser.getProducts(), promotions, products); + } catch (IllegalArgumentException e) { + ErrorResponse errorResponse = new ErrorResponse(e.getMessage()); + outputView.showError(errorResponse); + } + } + } +}
Java
FileReader๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์˜์กด์„ฑ ์ฃผ์ž…์„ ํ†ตํ•ด ์‚ฌ์šฉํ•˜์‹œ๋ฉด ์ถ”ํ›„ ํ”„๋กœ๋ชจ์…˜์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์ด ๋ฐ”๋€Œ์–ด๋„ ์‰ฝ๊ฒŒ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•ด์งˆ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,41 @@ +package store.domain; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import store.domain.vo.PromotionType; + +public class Promotions { + + private static final int BUY_INDEX = 1; + private static final int FREE_GET_INDEX = 2; + private static final int START_DATE_INDEX = 3; + + private final List<Promotion> promotions; + + public Promotions(List<List<String>> contents, LocalDateTime currentTime) { + this.promotions = makePromotions(contents, currentTime); + } + + public Promotion getPromotionByType(PromotionType type) { + return promotions.stream() + .filter(promotion -> promotion.getPromotionType().equals(type.getPromotionType())) + .findFirst() + .orElse(Promotion.noPromotion()); + } + + private List<Promotion> makePromotions(List<List<String>> contents, LocalDateTime currentTime) { + List<Promotion> promotions = new ArrayList<>(); + makePromotion(contents, currentTime, promotions); + return promotions; + } + + private void makePromotion(List<List<String>> contents, LocalDateTime currentTime, List<Promotion> promotions) { + for (List<String> content : contents) { + Promotion promotion = Promotion.from( + PromotionType.from(content.getFirst()), currentTime, content.get(BUY_INDEX), content.get(FREE_GET_INDEX), content.get(START_DATE_INDEX), content.getLast() + ); + promotions.add(promotion); + } + } +}
Java
List<List<String>>์œผ๋กœ ์ •๋ณด๋ฅผ ๋ฝ‘์•„๋‚ด๋Š” ๊ฒƒ์€ ๋„๋ฉ”์ธ์˜ ์—ญํ• ์ด ์•„๋‹Œ view์˜ ์—ญํ• ์— ๋” ๊ฐ€๊นŒ์šด ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. View์—์„œ ์–ด๋А์ •๋„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•œ ํ›„ DTO๋ฅผ ํ†ตํ•ด product๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,60 @@ +package store.domain; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import store.domain.vo.ProductQuantity; +import store.domain.vo.PromotionType; + +public class Promotion { + + private final PromotionType promotionType; + private final ProductQuantity buyQuantity; + private final ProductQuantity getQuantity; + private final boolean isPromotion; + + public static Promotion from(PromotionType promotionType, LocalDateTime currentDate, String buyQuantity, String getQuantity, + String startDate, String endDate) { + + return new Promotion(promotionType, ProductQuantity.from(buyQuantity), ProductQuantity.from(getQuantity), isPromotion(currentDate, startDate, endDate)); + } + + public static Promotion noPromotion() { + return new Promotion(PromotionType.none(), ProductQuantity.none(), ProductQuantity.none(), false); + } + + private Promotion(PromotionType promotionType, ProductQuantity buyQuantity, ProductQuantity getQuantity, + boolean isPromotion) { + this.promotionType = promotionType; + this.buyQuantity = buyQuantity; + this.getQuantity = getQuantity; + this.isPromotion = isPromotion; + } + + public String getPromotionType() { + return promotionType.getPromotionType(); + } + + public boolean isPromotion() { + return isPromotion; + } + + public int getBuyQuantity() { + return buyQuantity.getQuantity(); + } + + public int getGettableQuantity() { + return getQuantity.getQuantity(); + } + + public int getOnePromotionCycle() { + return buyQuantity.getQuantity() + getQuantity.getQuantity(); + } + + private static boolean isPromotion(LocalDateTime currentDate, String startDate, String endDate) { + LocalDateTime start = LocalDate.parse(startDate).atStartOfDay(); + LocalDateTime end = LocalDate.parse(endDate).atTime(LocalTime.MAX); + + return currentDate.isAfter(start) && currentDate.isBefore(end); + } +}
Java
ํ”„๋กœ๋ชจ์…˜ ์ƒ์„ฑ ๊ณผ์ •์„ ๋ณด๋ฉด ํ˜„์žฌ ์‹œ๊ฐ„์— ๋งž์ถฐ ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ์„ ์ •ํ•˜๋Š”๋ฐ ์žฌ๊ณ ๋ฅผ ๋„ฃ๋Š” ์‹œ๊ฐ„๋Œ€์™€ ๋ฌผํ’ˆ์„ ์‚ฌ๋Š” ์‹œ๊ฐ„์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,137 @@ +package store.controller; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.util.ArrayList; +import java.util.List; +import store.domain.BuyProductParser; +import store.domain.BuyProducts; +import store.domain.FileReader; +import store.domain.CustomerReceipt; +import store.domain.Products; +import store.domain.Promotions; +import store.domain.Receipt; +import store.domain.service.MemberShipCalculator; +import store.domain.service.PromotionCalculator; +import store.domain.service.PromotionChecker; +import store.domain.vo.MemberShip; +import store.view.InputView; +import store.view.OutputView; +import store.view.dto.request.PromotionRequest; +import store.view.dto.response.ErrorResponse; +import store.view.dto.request.MemberShipRequest; +import store.view.dto.response.PromotionResponse; + +public class StoreController { + + private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md"; + private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md"; + private static final String YES = "Y"; + + private final MemberShipCalculator memberShipCalculator; + private final PromotionCalculator promotionCalculator; + private final OutputView outputView; + private final InputView inputView; + + public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator, + OutputView outputView, InputView inputView) { + this.memberShipCalculator = memberShipCalculator; + this.promotionCalculator = promotionCalculator; + this.outputView = outputView; + this.inputView = inputView; + } + + public void run() { + boolean continueShopping = true; + + Promotions promotions = getPromotions(); + Products products = getProducts(promotions); + + while (continueShopping) { + showInventory(products); + BuyProducts buyProducts = buyProducts(promotions, products); + CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts); + showReceipt(customerReceipt); + continueShopping = inputView.continueShopping() + .equals(YES); + } + + } + + private Promotions getPromotions() { + FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH); + return new Promotions(promotionsContents.fileContents(), DateTimes.now()); + } + + private Products getProducts(Promotions promotions) { + FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH); + return new Products(productsContents.fileContents(), promotions); + } + + private void showInventory(Products products) { + outputView.welcomeStore(); + products.displayProducts() + .forEach(product -> outputView.showInventory( + product.getName(), + product.getPrice(), + product.showQuantity(), + product.getPromotionType() + )); + } + + private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) { + List<PromotionRequest> requests = checkPromotionCase(products, buyProducts); + + Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests); + MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES)); + MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request); + return new CustomerReceipt(receipt, memberShip); + } + + private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) { + PromotionChecker promotionChecker = new PromotionChecker(); + List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts); + List<PromotionRequest> requests = new ArrayList<>(); + for (PromotionResponse response : responses) { + checkNoPromotionRequest(response, requests); + checkFreePromotionRequest(response, requests); + } + return requests; + } + + private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isFreePromotion()) { + boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(), + response.freePromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), false, isFreePromotion)); + } + } + + private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isNoPromotion()) { + boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(), + response.noPromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), isNoPromotion, false)); + } + } + + private void showReceipt(CustomerReceipt customerReceipt) { + outputView.guideBuyProducts(); + outputView.showBuyProducts(customerReceipt.showBuyProducts()); + outputView.guideFreeGetQuantity(); + outputView.showFreeQuantity(customerReceipt.showFreeProductInfo()); + outputView.showCalculateResult(customerReceipt.showCalculateInfo()); + } + + private BuyProducts buyProducts(Promotions promotions, Products products) { + outputView.getCustomerProducts(); + while (true) { + try { + BuyProductParser parser = new BuyProductParser(inputView.buyProducts()); + return new BuyProducts(parser.getProducts(), promotions, products); + } catch (IllegalArgumentException e) { + ErrorResponse errorResponse = new ErrorResponse(e.getMessage()); + outputView.showError(errorResponse); + } + } + } +}
Java
์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿฌ๋ฒˆ ํ˜ธ์ถœ์ด ๋œ๋‹ค๋ฉด IO์ž‘์—…์ด ๋Š˜์–ด๋‚˜๊ฒŒ ๋˜๋ฉด์„œ ์„ฑ๋Šฅ์ ์œผ๋กœ ์•„์‰ฌ์›€์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. List๋ฅผ ํ†ตํ•ด ํ•œ๋ฒˆ์— ๋ณด๋‚ธ ํ›„ IO์ž‘์—…์„ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•˜์‹œ๋Š”๊ฒƒ์€ ์–ด๋–ค๊ฐ€์š”?
@@ -0,0 +1,76 @@ +package store.view; + +import java.util.List; +import store.view.dto.response.BuyProductResponse; +import store.view.dto.response.ErrorResponse; +import store.view.dto.response.FreeProductResponse; +import store.view.dto.response.ProductCalculateResponse; + +public class OutputView { + + private static final String LINE_SEPARATOR = System.lineSeparator(); + + public void welcomeStore() { + System.out.println(LINE_SEPARATOR + "์•ˆ๋…•ํ•˜์„ธ์š”. WํŽธ์˜์ ์ž…๋‹ˆ๋‹ค." + LINE_SEPARATOR + "ํ˜„์žฌ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค." + LINE_SEPARATOR); + } + + public void showInventory(String name, int price, String quantity, String promotionType) { + System.out.printf( + "- %s %,d์› %s %s" + + LINE_SEPARATOR, + name, price, quantity, promotionType + ); + } + + public void getCustomerProducts() { + System.out.println(LINE_SEPARATOR + "๊ตฌ๋งคํ•˜์‹ค ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (์˜ˆ: [์‚ฌ์ด๋‹ค-2],[๊ฐ์ž์นฉ-1])"); + } + + public void guideBuyProducts() { + System.out.printf( + LINE_SEPARATOR + + "==============W ํŽธ์˜์ ================" + + LINE_SEPARATOR + + "%-10s %9s %9s%n" + , "์ƒํ’ˆ๋ช…", "์ˆ˜๋Ÿ‰" ,"๊ธˆ์•ก" + ); + } + + public void showBuyProducts(List<BuyProductResponse> buyProducts) { + for (BuyProductResponse product : buyProducts) { + System.out.printf( + "%-10s %9d %,14d" + LINE_SEPARATOR, + product.name(), product.quantity(), product.totalPrice() + ); + } + } + + public void guideFreeGetQuantity() { + System.out.println("=============์ฆ\t\t์ •==============="); + } + + public void showFreeQuantity(List<FreeProductResponse> freeProductResponses) { + for (FreeProductResponse freeProductResponse : freeProductResponses) { + System.out.printf( + "%-10s %9d" + LINE_SEPARATOR, + freeProductResponse.name(), freeProductResponse.quantity() + ); + } + } + + public void showCalculateResult(ProductCalculateResponse calculateInfo) { + System.out.printf( + "====================================" + LINE_SEPARATOR + + "์ด๊ตฌ๋งค์•ก\t\t\t\t%d\t\t %,d" + LINE_SEPARATOR + + "ํ–‰์‚ฌํ• ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR + + "๋ฉค๋ฒ„์‹ญํ• ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR + + "๋‚ด์‹ค๋ˆ\t\t\t\t\t\t %,d" + LINE_SEPARATOR + , calculateInfo.buyCounts(), calculateInfo.buyProductsPrice(), + calculateInfo.promotionPrice(), calculateInfo.memberShip(), calculateInfo.totalPrice() + ); + } + + public void showError(ErrorResponse errorResponse) { + System.out.println(errorResponse.message()); + } +}
Java
format์„ ์ƒ์ˆ˜๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–ค๊ฐ€์š”? ๋งŒ์•ฝ ์ถœ๋ ฅ ํ˜•์‹์ด ์ข€ ๋‹ฌ๋ผ์ง„๋‹ค๋ฉด ์ˆ˜์ •์ด ๋”์šฑ ๋ฒˆ๊ฑฐ๋กœ์šธ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,76 @@ +package store.view; + +import java.util.List; +import store.view.dto.response.BuyProductResponse; +import store.view.dto.response.ErrorResponse; +import store.view.dto.response.FreeProductResponse; +import store.view.dto.response.ProductCalculateResponse; + +public class OutputView { + + private static final String LINE_SEPARATOR = System.lineSeparator(); + + public void welcomeStore() { + System.out.println(LINE_SEPARATOR + "์•ˆ๋…•ํ•˜์„ธ์š”. WํŽธ์˜์ ์ž…๋‹ˆ๋‹ค." + LINE_SEPARATOR + "ํ˜„์žฌ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค." + LINE_SEPARATOR); + } + + public void showInventory(String name, int price, String quantity, String promotionType) { + System.out.printf( + "- %s %,d์› %s %s" + + LINE_SEPARATOR, + name, price, quantity, promotionType + ); + } + + public void getCustomerProducts() { + System.out.println(LINE_SEPARATOR + "๊ตฌ๋งคํ•˜์‹ค ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (์˜ˆ: [์‚ฌ์ด๋‹ค-2],[๊ฐ์ž์นฉ-1])"); + } + + public void guideBuyProducts() { + System.out.printf( + LINE_SEPARATOR + + "==============W ํŽธ์˜์ ================" + + LINE_SEPARATOR + + "%-10s %9s %9s%n" + , "์ƒํ’ˆ๋ช…", "์ˆ˜๋Ÿ‰" ,"๊ธˆ์•ก" + ); + } + + public void showBuyProducts(List<BuyProductResponse> buyProducts) { + for (BuyProductResponse product : buyProducts) { + System.out.printf( + "%-10s %9d %,14d" + LINE_SEPARATOR, + product.name(), product.quantity(), product.totalPrice() + ); + } + } + + public void guideFreeGetQuantity() { + System.out.println("=============์ฆ\t\t์ •==============="); + } + + public void showFreeQuantity(List<FreeProductResponse> freeProductResponses) { + for (FreeProductResponse freeProductResponse : freeProductResponses) { + System.out.printf( + "%-10s %9d" + LINE_SEPARATOR, + freeProductResponse.name(), freeProductResponse.quantity() + ); + } + } + + public void showCalculateResult(ProductCalculateResponse calculateInfo) { + System.out.printf( + "====================================" + LINE_SEPARATOR + + "์ด๊ตฌ๋งค์•ก\t\t\t\t%d\t\t %,d" + LINE_SEPARATOR + + "ํ–‰์‚ฌํ• ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR + + "๋ฉค๋ฒ„์‹ญํ• ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR + + "๋‚ด์‹ค๋ˆ\t\t\t\t\t\t %,d" + LINE_SEPARATOR + , calculateInfo.buyCounts(), calculateInfo.buyProductsPrice(), + calculateInfo.promotionPrice(), calculateInfo.memberShip(), calculateInfo.totalPrice() + ); + } + + public void showError(ErrorResponse errorResponse) { + System.out.println(errorResponse.message()); + } +}
Java
System.lineSeparator๋ฅผ ํ†ตํ•œ ์ค„๋ฐ”๊ฟˆ ์ฒ˜๋ฆฌ ๊ต‰์žฅํžˆ ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,46 @@ +package store.domain.service; + +import java.util.ArrayList; +import java.util.List; +import store.domain.BuyProducts; +import store.domain.Product; +import store.domain.Products; +import store.view.dto.response.PromotionResponse; + +public class PromotionChecker { + + private static final int ZERO = 0; + + public List<PromotionResponse> checkPromotion(Products products, BuyProducts buyProducts) { + List<PromotionResponse> promotionResponses = new ArrayList<>(); + for (Product buyProduct : buyProducts.products()) { + checkPromotionCase(products, buyProduct, promotionResponses); + } + return promotionResponses; + } + + private void checkPromotionCase(Products products, Product buyProduct, List<PromotionResponse> promotionResponses) { + if (buyProduct.isPromotion()) { + Product promotionProduct = products.getPromotionProducts(buyProduct.getName()).getFirst(); + checkNoPromotion(buyProduct, promotionProduct, promotionResponses); + checkFreePromotion(buyProduct, promotionResponses); + } + } + + private void checkFreePromotion(Product buyProduct, List<PromotionResponse> promotionResponses) { + if (buyProduct.getQuantity() % buyProduct.getOnePromotionCycle() == buyProduct.getPromotionBuyQuantity()) { + promotionResponses.add( + new PromotionResponse(buyProduct.getName(), false, ZERO, true, buyProduct.getPromotionGettableQuantity()) + ); + } + } + + private void checkNoPromotion(Product buyProduct, Product promotionProduct, List<PromotionResponse> promotionResponses) { + if (buyProduct.getQuantity() > promotionProduct.getQuantity()) { + int noPromotionQuantity = buyProduct.getQuantity() - promotionProduct.getOnePromotionCycle() * promotionProduct.getPromotionQuantity(); + promotionResponses.add( + new PromotionResponse(buyProduct.getName(), true, noPromotionQuantity, false, ZERO) + ); + } + } +}
Java
ํ”„๋กœ๋ชจ์…˜์„ ํ™•์ธํ•˜๋Š” ๋ถ€๋ถ„์„ ๋”ฐ๋กœ ๋ฝ‘์€ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,137 @@ +package store.controller; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.util.ArrayList; +import java.util.List; +import store.domain.BuyProductParser; +import store.domain.BuyProducts; +import store.domain.FileReader; +import store.domain.CustomerReceipt; +import store.domain.Products; +import store.domain.Promotions; +import store.domain.Receipt; +import store.domain.service.MemberShipCalculator; +import store.domain.service.PromotionCalculator; +import store.domain.service.PromotionChecker; +import store.domain.vo.MemberShip; +import store.view.InputView; +import store.view.OutputView; +import store.view.dto.request.PromotionRequest; +import store.view.dto.response.ErrorResponse; +import store.view.dto.request.MemberShipRequest; +import store.view.dto.response.PromotionResponse; + +public class StoreController { + + private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md"; + private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md"; + private static final String YES = "Y"; + + private final MemberShipCalculator memberShipCalculator; + private final PromotionCalculator promotionCalculator; + private final OutputView outputView; + private final InputView inputView; + + public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator, + OutputView outputView, InputView inputView) { + this.memberShipCalculator = memberShipCalculator; + this.promotionCalculator = promotionCalculator; + this.outputView = outputView; + this.inputView = inputView; + } + + public void run() { + boolean continueShopping = true; + + Promotions promotions = getPromotions(); + Products products = getProducts(promotions); + + while (continueShopping) { + showInventory(products); + BuyProducts buyProducts = buyProducts(promotions, products); + CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts); + showReceipt(customerReceipt); + continueShopping = inputView.continueShopping() + .equals(YES); + } + + } + + private Promotions getPromotions() { + FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH); + return new Promotions(promotionsContents.fileContents(), DateTimes.now()); + } + + private Products getProducts(Promotions promotions) { + FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH); + return new Products(productsContents.fileContents(), promotions); + } + + private void showInventory(Products products) { + outputView.welcomeStore(); + products.displayProducts() + .forEach(product -> outputView.showInventory( + product.getName(), + product.getPrice(), + product.showQuantity(), + product.getPromotionType() + )); + } + + private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) { + List<PromotionRequest> requests = checkPromotionCase(products, buyProducts); + + Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests); + MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES)); + MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request); + return new CustomerReceipt(receipt, memberShip); + } + + private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) { + PromotionChecker promotionChecker = new PromotionChecker(); + List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts); + List<PromotionRequest> requests = new ArrayList<>(); + for (PromotionResponse response : responses) { + checkNoPromotionRequest(response, requests); + checkFreePromotionRequest(response, requests); + } + return requests; + } + + private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isFreePromotion()) { + boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(), + response.freePromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), false, isFreePromotion)); + } + } + + private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isNoPromotion()) { + boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(), + response.noPromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), isNoPromotion, false)); + } + } + + private void showReceipt(CustomerReceipt customerReceipt) { + outputView.guideBuyProducts(); + outputView.showBuyProducts(customerReceipt.showBuyProducts()); + outputView.guideFreeGetQuantity(); + outputView.showFreeQuantity(customerReceipt.showFreeProductInfo()); + outputView.showCalculateResult(customerReceipt.showCalculateInfo()); + } + + private BuyProducts buyProducts(Promotions promotions, Products products) { + outputView.getCustomerProducts(); + while (true) { + try { + BuyProductParser parser = new BuyProductParser(inputView.buyProducts()); + return new BuyProducts(parser.getProducts(), promotions, products); + } catch (IllegalArgumentException e) { + ErrorResponse errorResponse = new ErrorResponse(e.getMessage()); + outputView.showError(errorResponse); + } + } + } +}
Java
false์™€ isFreePromotion์„ enum์„ ํ†ตํ•œ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์—ด๊ฑฐํ˜•์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,137 @@ +package store.controller; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.util.ArrayList; +import java.util.List; +import store.domain.BuyProductParser; +import store.domain.BuyProducts; +import store.domain.FileReader; +import store.domain.CustomerReceipt; +import store.domain.Products; +import store.domain.Promotions; +import store.domain.Receipt; +import store.domain.service.MemberShipCalculator; +import store.domain.service.PromotionCalculator; +import store.domain.service.PromotionChecker; +import store.domain.vo.MemberShip; +import store.view.InputView; +import store.view.OutputView; +import store.view.dto.request.PromotionRequest; +import store.view.dto.response.ErrorResponse; +import store.view.dto.request.MemberShipRequest; +import store.view.dto.response.PromotionResponse; + +public class StoreController { + + private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md"; + private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md"; + private static final String YES = "Y"; + + private final MemberShipCalculator memberShipCalculator; + private final PromotionCalculator promotionCalculator; + private final OutputView outputView; + private final InputView inputView; + + public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator, + OutputView outputView, InputView inputView) { + this.memberShipCalculator = memberShipCalculator; + this.promotionCalculator = promotionCalculator; + this.outputView = outputView; + this.inputView = inputView; + } + + public void run() { + boolean continueShopping = true; + + Promotions promotions = getPromotions(); + Products products = getProducts(promotions); + + while (continueShopping) { + showInventory(products); + BuyProducts buyProducts = buyProducts(promotions, products); + CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts); + showReceipt(customerReceipt); + continueShopping = inputView.continueShopping() + .equals(YES); + } + + } + + private Promotions getPromotions() { + FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH); + return new Promotions(promotionsContents.fileContents(), DateTimes.now()); + } + + private Products getProducts(Promotions promotions) { + FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH); + return new Products(productsContents.fileContents(), promotions); + } + + private void showInventory(Products products) { + outputView.welcomeStore(); + products.displayProducts() + .forEach(product -> outputView.showInventory( + product.getName(), + product.getPrice(), + product.showQuantity(), + product.getPromotionType() + )); + } + + private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) { + List<PromotionRequest> requests = checkPromotionCase(products, buyProducts); + + Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests); + MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES)); + MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request); + return new CustomerReceipt(receipt, memberShip); + } + + private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) { + PromotionChecker promotionChecker = new PromotionChecker(); + List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts); + List<PromotionRequest> requests = new ArrayList<>(); + for (PromotionResponse response : responses) { + checkNoPromotionRequest(response, requests); + checkFreePromotionRequest(response, requests); + } + return requests; + } + + private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isFreePromotion()) { + boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(), + response.freePromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), false, isFreePromotion)); + } + } + + private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isNoPromotion()) { + boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(), + response.noPromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), isNoPromotion, false)); + } + } + + private void showReceipt(CustomerReceipt customerReceipt) { + outputView.guideBuyProducts(); + outputView.showBuyProducts(customerReceipt.showBuyProducts()); + outputView.guideFreeGetQuantity(); + outputView.showFreeQuantity(customerReceipt.showFreeProductInfo()); + outputView.showCalculateResult(customerReceipt.showCalculateInfo()); + } + + private BuyProducts buyProducts(Promotions promotions, Products products) { + outputView.getCustomerProducts(); + while (true) { + try { + BuyProductParser parser = new BuyProductParser(inputView.buyProducts()); + return new BuyProducts(parser.getProducts(), promotions, products); + } catch (IllegalArgumentException e) { + ErrorResponse errorResponse = new ErrorResponse(e.getMessage()); + outputView.showError(errorResponse); + } + } + } +}
Java
์ด ๋ถ€๋ถ„์„ outputView์—์„œ ์ˆœ์„œ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,137 @@ +package store.controller; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.util.ArrayList; +import java.util.List; +import store.domain.BuyProductParser; +import store.domain.BuyProducts; +import store.domain.FileReader; +import store.domain.CustomerReceipt; +import store.domain.Products; +import store.domain.Promotions; +import store.domain.Receipt; +import store.domain.service.MemberShipCalculator; +import store.domain.service.PromotionCalculator; +import store.domain.service.PromotionChecker; +import store.domain.vo.MemberShip; +import store.view.InputView; +import store.view.OutputView; +import store.view.dto.request.PromotionRequest; +import store.view.dto.response.ErrorResponse; +import store.view.dto.request.MemberShipRequest; +import store.view.dto.response.PromotionResponse; + +public class StoreController { + + private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md"; + private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md"; + private static final String YES = "Y"; + + private final MemberShipCalculator memberShipCalculator; + private final PromotionCalculator promotionCalculator; + private final OutputView outputView; + private final InputView inputView; + + public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator, + OutputView outputView, InputView inputView) { + this.memberShipCalculator = memberShipCalculator; + this.promotionCalculator = promotionCalculator; + this.outputView = outputView; + this.inputView = inputView; + } + + public void run() { + boolean continueShopping = true; + + Promotions promotions = getPromotions(); + Products products = getProducts(promotions); + + while (continueShopping) { + showInventory(products); + BuyProducts buyProducts = buyProducts(promotions, products); + CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts); + showReceipt(customerReceipt); + continueShopping = inputView.continueShopping() + .equals(YES); + } + + } + + private Promotions getPromotions() { + FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH); + return new Promotions(promotionsContents.fileContents(), DateTimes.now()); + } + + private Products getProducts(Promotions promotions) { + FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH); + return new Products(productsContents.fileContents(), promotions); + } + + private void showInventory(Products products) { + outputView.welcomeStore(); + products.displayProducts() + .forEach(product -> outputView.showInventory( + product.getName(), + product.getPrice(), + product.showQuantity(), + product.getPromotionType() + )); + } + + private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) { + List<PromotionRequest> requests = checkPromotionCase(products, buyProducts); + + Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests); + MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES)); + MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request); + return new CustomerReceipt(receipt, memberShip); + } + + private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) { + PromotionChecker promotionChecker = new PromotionChecker(); + List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts); + List<PromotionRequest> requests = new ArrayList<>(); + for (PromotionResponse response : responses) { + checkNoPromotionRequest(response, requests); + checkFreePromotionRequest(response, requests); + } + return requests; + } + + private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isFreePromotion()) { + boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(), + response.freePromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), false, isFreePromotion)); + } + } + + private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) { + if (response.isNoPromotion()) { + boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(), + response.noPromotionQuantity()).equals(YES); + requests.add(new PromotionRequest(response.name(), isNoPromotion, false)); + } + } + + private void showReceipt(CustomerReceipt customerReceipt) { + outputView.guideBuyProducts(); + outputView.showBuyProducts(customerReceipt.showBuyProducts()); + outputView.guideFreeGetQuantity(); + outputView.showFreeQuantity(customerReceipt.showFreeProductInfo()); + outputView.showCalculateResult(customerReceipt.showCalculateInfo()); + } + + private BuyProducts buyProducts(Promotions promotions, Products products) { + outputView.getCustomerProducts(); + while (true) { + try { + BuyProductParser parser = new BuyProductParser(inputView.buyProducts()); + return new BuyProducts(parser.getProducts(), promotions, products); + } catch (IllegalArgumentException e) { + ErrorResponse errorResponse = new ErrorResponse(e.getMessage()); + outputView.showError(errorResponse); + } + } + } +}
Java
while์กฐ๊ฑด ์•ˆ์— true๋ฅผ ๋„ฃ๋Š” ๋ฐฉ์‹์€ ๋งค์šฐ ์ข‹์ง€ ๋ชปํ•œ ๋ฐฉ์‹์ด๋ผ๊ณ  ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋กœ boolean๊ฐ’์„ ์‚ฌ์šฉํ•ด์„œ ์กฐ๊ฑด์„ ๋„ฃ๋Š” ๊ฒƒ์ด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,63 @@ +package store.domain; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import store.domain.exception.ProductErrorCode; +import store.domain.exception.ProductException; + +public class BuyProductParser { + + private static final int NAME_INDEX = 1; + private static final int PRICE_INDEX = 2; + private static final String INPUT_DELIMITER = ","; + private static final String ORDER_FORM = "\\[(.+)-(.+)]"; + + private final Map<String, String> products; + + public BuyProductParser(String buyProduct) { + this.products = makeProductsForm(buyProduct); + } + + public Map<String, String> getProducts() { + return Collections.unmodifiableMap(products); + } + + private Map<String, String> makeProductsForm(String buyProducts) { + Map<String, String> products = new LinkedHashMap<>(); + parseProducts(buyProducts, products); + return products; + } + + private void parseProducts(String buyProducts, Map<String, String> products) { + Pattern pattern = Pattern.compile(ORDER_FORM); + parseByRest(buyProducts).forEach(product -> { + validateFrom(product); + Matcher matcher = pattern.matcher(product); + validateMatcher(matcher); + products.put(matcher.group(NAME_INDEX).trim(), matcher.group(PRICE_INDEX).trim()); + }); + } + + private void validateMatcher(Matcher matcher) { + if (!matcher.matches()) { + throw new ProductException(ProductErrorCode.WRONG_INPUT); + } + } + + private List<String> parseByRest(String buyProduct) { + if (buyProduct.contains(INPUT_DELIMITER)) { + return List.of(buyProduct.split(INPUT_DELIMITER)); + } + return List.of(buyProduct); + } + + private void validateFrom(String buyProducts) { + if (!buyProducts.matches(ORDER_FORM)) { + throw new ProductException(ProductErrorCode.WRONG_INPUT); + } + } +}
Java
Hash์˜ ๊ฒฝ์šฐ ์‚ฌ๊ธฐ์ ์ธ ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ์ข‹์ง€๋งŒ ํŠน์ˆ˜ํ•œ ์ƒํ™ฉ์—์„œ๋Š” Hash์˜ ๊ฒฝ์šฐ ๊ต‰์žฅํžˆ ๋А๋ ค์งˆ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Hash๋ณด๋‹จ ๋А๋ฆฌ์ง€๋งŒ ์•ˆ์ •์ ์ธ ์„ฑ๋Šฅ์„ ๊ฐ€์ง€๋Š” TreeMap๋„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๊ทผ๋ฐ TreeMap์„ ์‚ฌ์šฉํ•˜๋ฉด ์˜ˆ์‹œ์™€ ๋‹ค๋ฅด๊ฒŒ ์ •๋ ฌ๋œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,168 @@ +## ๊ณ ๋ฏผ์‚ฌํ•ญ + +**๊ณ ๋ฏผ** +~~~ +๊ตฌํ˜„์— ํ•„์š”ํ•œ ์ƒํ’ˆ ๋ชฉ๋ก๊ณผ ํ–‰์‚ฌ ๋ชฉ๋ก์„ ํŒŒ์ผ ์ž…์ถœ๋ ฅ์„ ํ†ตํ•ด ๋ถˆ๋Ÿฌ์˜จ๋‹ค. +src/main/resources/products.md๊ณผ +src/main/resources/promotions.md ํŒŒ์ผ์„ ์ด์šฉํ•œ๋‹ค. +~~~ +* ๋งˆํฌ๋‹ค์šด ํŒŒ์ผ์— ์žˆ๋Š” ๋‚ด์šฉ์„ ์–ด๋–ป๊ฒŒ ๋ถˆ๋Ÿฌ์™€ ์ฝ”๋“œ์— ์ ์šฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ์„๊นŒ? + +**ํ•ด๊ฒฐ ๊ณผ์ •** +* BufferedReader ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ ๋‚ด์šฉ์˜ ํ•œ ์ค„์”ฉ ์ž…๋ ฅ๋ฐ›๋Š” ํ˜•์‹์œผ๋กœ ํ•œ๋‹ค. +* ๋ฐ›์•„์˜จ ์ค„์„ ์‰ผํ‘œ(,)๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ถ„๋ฆฌํ•œ๋‹ค +* ๋ถ„๋ฆฌํ•œ ๋‚ด์šฉ์„ ์•Œ๋งž๊ฒŒ ์›์‹œ๊ฐ’ ํฌ์žฅ ํด๋ž˜์Šค์— ํ• ๋‹นํ•œ๋‹ค. +* ํ• ๋‹น๋  ๋•Œ, ๋นˆ ๊ฐ’์ด๋‚˜ ํ˜•์‹์ด ๋งž์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. +* ๋˜ํ•œ, ํ…Œ์ŠคํŠธ ํ•ด์•ผ ํ•  ๊ฒฝ์šฐ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ํ•ด๋‹น ๊ฐ์ฒด ๋‚ด๋ถ€์— ๊ฒฐ์ •ํ•˜๋ฉด ๋‹ค๋ฅธ ํŒŒ์ผ๋กœ ํ…Œ์ŠคํŠธ ํ•˜์ง€ ๋ชปํ•œ๋‹ค. +* ๋”ฐ๋ผ์„œ, ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ฃผ์ž…๋ฐ›๋Š” ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•˜์—ฌ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. +* ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ ํŒŒ์ผ ์„ค์ • ์‹œ, ๋งˆํฌ๋‹ค์šด์— ๋นˆ ์ค„์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•œ๋‹ค. -> ๋นˆ ์ค„์ด ๋“ค์–ด๊ฐ€๋ฉด ๋ฒ„ํผ๋ฆฌ๋”๊ฐ€ ๊ณต๋ฐฑ์„ ์ฝ์–ด๋ฒ„๋ฆฐ๋‹ค. + + +* ๋˜ํ•œ, ๋ชจ๋“  ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ €์žฅํ•œ๋‹ค. ํ”„๋กœ๊ทธ๋žจ ์š”๊ตฌ ์‚ฌํ•ญ์— ๋”ฐ๋ผ, ํŒŒ์ผ ์ž…์ถœ๋ ฅ์„ ํ†ตํ•ด ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•œ๋‹ค. -> ํŒŒ์ผ์— ์žˆ๋Š” ๋‚ด์šฉ์€ enum์œผ๋กœ ๋งŒ๋“ค์ง€ ์•Š์„ ๊ฒƒ. + +---- + +## ๊ณ ๋ฏผ์‚ฌํ•ญ + +**๊ณ ๋ฏผ** +* ๊ฐ์ฒด๋ฅผ ์–ด๋–ค์‹์œผ๋กœ ๋‚˜๋ˆ ์•ผ ํ• ๊นŒ? + +**ํ•ด๊ฒฐ ๊ณผ์ •** +* ์ž…๋ ฅ ํ˜•์‹์ด ``[์‚ฌ์ด๋‹ค-2],[๊ฐ์ž์นฉ-1]`` ์ด๋Ÿฐ์‹์œผ๋กœ ๋“ค์–ด์˜ด. ์ฆ‰, ์ค‘์š”ํ•œ๊ฑด ์ƒํ’ˆ ์ด๋ฆ„๊ณผ ์ˆ˜๋Ÿ‰ +* ๊ฐ€๊ฒฉ์€ ๊ทธ๋•Œ ๊ทธ๋•Œ ๋”ํ•ด์ฃผ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์ƒํ’ˆ์ด๋ฆ„๊ณผ ๊ฐ€๊ฒฉ์„ enum ์œผ๋กœ ์ƒ์„ฑ +* ์ƒํ’ˆ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋ฆ„๊ณผ ์ˆ˜๋Ÿ‰์„ ํ•„๋“œ๋กœ ๊ฐ€์ง€๋„๋ก ํ•จ +* ๊ทธ๋ฆฌ๊ณ  ํ”„๋กœ๋ชจ์…˜์˜ ๊ฒฝ์šฐ๋Š” Map ์ž๋ฃŒ๊ตฌ์กฐ ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ``Map<์ƒํ’ˆ, ํ”„๋กœ๋ชจ์…˜>`` ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋ƒ„ +* ์ด๋Ÿฐ ํ˜•์‹์œผ๋กœ ํ•˜๋ฉด ์–ด๋–ค ์ƒํ’ˆ์ด ์–ด๋–ค ํ”„๋กœ๋ชจ์…˜์ธ์ง€ ์ž˜ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Œ. +* ๋‚˜์ค‘์— ํ”„๋กœ๋ชจ์…˜ ์ •๋ณด๋งŒ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด๋„ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์„œ ๊ตฌ์ž…ํ•  ๋•Œ, ๋น„๊ตํ•˜๋„๋ก ํ•˜๋ฉด ๋จ. + + + +* ``Map<์ƒํ’ˆ, ํ”„๋กœ๋ชจ์…˜>`` ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์Œ์„ ์•Œ๊ฒŒ ๋Œ. +* ์ƒํ’ˆ ํ•„๋“œ์— ์›์‹œ๊ฐ’ ํฌ์žฅํ•œ ๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ๊ฐ€์ ธ๋‹ค ์จ๋„ ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜์Œ. +* ๋”ฐ๋ผ์„œ, ``List<์ƒํ’ˆ>`` ํ˜•์‹์œผ๋กœ ํ•„๋“œ๋ฅผ ๊ตฌ์„ฑ +---- + +## ๊ณ ๋ฏผ์‚ฌํ•ญ + +**๊ณ ๋ฏผ** +* ์›์‹œ๊ฐ’ ํฌ์žฅ ๊ฐ์ฒด์— ์žˆ๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ ค ํ•˜๋‹ˆ, ``Product.name().name()`` ํ˜•์‹์„ ํ†ตํ•ด ๋ฐ˜ํ™˜๋œ๋‹ค. ์ค‘๋ณต์„ ์ค„์ด๊ณ  ์‹ถ์Œ. + +**ํ•ด๊ฒฐ ๊ณผ์ •** +* ๊ฐ์ฒด๋ฅผ ๋ฐ›๋Š” ์ž…์žฅ์—์„œ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ๊ฐ์ฒด๊ฐ€ ์ „๋‹ฌ๋˜๋Š”์ง€ ์ •ํ™•ํžˆ ์•Œ์•„์•ผ ํ• ๊นŒ? +* ํ•ด๋‹น๋œ ๊ฐ’๋งŒ ์ž˜ ๋ฐ›์•„์„œ ๊ตฌํ˜„๋˜๋ฉด ๋œ๋‹ค. +* ๋”ฐ๋ผ์„œ, ``Product.name()`` ๋งŒ ํ–ˆ์„ ๋•Œ, ๊ฐ’์ด ๋ฐ˜ํ™˜๋˜๋„๋ก ํ•œ๋‹ค. + +---- + +## ๊ณ ๋ฏผ์‚ฌํ•ญ + +**๊ณ ๋ฏผ** +* ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒํ’ˆ์˜ ๊ฒฝ์šฐ๋ฅผ ์–ด๋–ค์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์„๊นŒ? + +**ํ•ด๊ฒฐ ๊ณผ์ •** +* ์ž…๋ ฅ๋ฐ›์€ ํ’ˆ๋ชฉ๋“ค์„ Products ํด๋ž˜์Šค ํŠน์ • ๋ฉ”์„œ๋“œ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋˜์ง„๋‹ค +* ์ด๋•Œ, ์–ด๋–ป๊ฒŒ ํ•ด๋‹น ํ•ญ๋ชฉ์ด ๋“ค์–ด์žˆ๋Š”์ง€ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ์„๊นŒ? +* ์ƒํ’ˆ๋ช…์ด ๊ฐ™์€ ๊ฒฝ์šฐ ๋“ค์–ด์žˆ๋‹ค๊ณ  ํŒ๋‹จํ•ด์•ผ ํ•œ๋‹ค. +* ์ƒํ’ˆ๋ช…์ด ๊ฐ™์€ ๊ฒฝ์šฐ, ์ธ์Šคํ„ด์Šค ์ž์ฒด๊ฐ€ ๊ฐ™๋‹ค๊ณ  ํŒ๋‹จํ•˜๋ฉด ๋กœ์ง์ด ๋” ๊ฐ„ํŽธํ•ด์งˆ ๊ฒƒ ๊ฐ™๋‹ค. +* ๋”ฐ๋ผ์„œ, Product ํด๋ž˜์Šค์— equals์™€ hashcode ๋ฉ”์„œ๋“œ๋ฅผ name์— ๊ด€ํ•ด์„œ๋งŒ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. +* ProductName ํด๋ž˜์Šค์—๋„ equals์™€ hashcode ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. + +---- + +## ๊ณ ๋ฏผ์‚ฌํ•ญ + +**๊ณ ๋ฏผ** +* ๊ตฌ๋งค ์ˆ˜๋Ÿ‰์ด ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•œ ๊ฒฝ์šฐ๋ฅผ ์–ด๋–ค ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์„๊นŒ? + +**ํ•ด๊ฒฐ ๊ณผ์ •** +* ๋จผ์ €, ํ•ด๋‹น ์ƒํ’ˆ์˜ ์ด๋ฆ„๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. +* ๊ฐ€์ ธ์˜จ ๋ชจ๋“  ์ƒํ’ˆ์˜ ์žฌ๊ณ ๋ฅผ ํ•ฉํ•˜์—ฌ ๋น„๊ตํ•œ๋‹ค. + + +* ์›๋ž˜ ์žˆ๋˜ Products ํด๋ž˜์Šค์— ์žฌ๊ณ  ๊ฒ€์ฆ ์ฑ…์ž„์„ ๋ถ€์—ฌํ•˜๋‹ˆ, ๋กœ์ง์ด ๋„ˆ๋ฌด ๋งŽ์•„์ง€๊ณ  ๋งŽ์€ ์ฑ…์ž„์„ ์ง€๊ฒŒ ๋œ๋‹ค. +* ๋”ฐ๋ผ์„œ, ์žฌ๊ณ  ๊ฒ€์ฆํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋”ฐ๋กœ ์ƒ์„ฑํ•œ๋‹ค. + +--- + +## ๊ณ ๋ฏผ์‚ฌํ•ญ + +**๊ณ ๋ฏผ** +* ์ƒํ’ˆ ์ž…๋ ฅ ์‹œ, ๊ธฐํƒ€ ์ž˜๋ชป๋œ ๊ฒฝ์šฐ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ์„๊นŒ? + +**ํ•ด๊ฒฐ ๊ณผ์ •** +* ๊ธฐํƒ€ ์ž˜๋ชป๋œ ๊ฒฝ์šฐ๋ฅผ ๊ฒ€์ฆํ•˜๋ ค๋ฉด [์ƒํ’ˆ-๊ฐœ์ˆ˜] ํ•ด๋‹น ํ˜•์‹ ์ž์ฒด๊ฐ€ ์•„๋‹ˆ์–ด์•ผ ํ•œ๋‹ค. +* ๋”ฐ๋ผ์„œ ์ •๊ทœ์‹์„ ํ†ตํ•ด ๊ฒ€์ฆํ•œ๋‹ค. +1. ```\\[``` : ```[```๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ +2. ```(.+)``` : ์•„๋ฌด ๋ฌธ์ž๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ ์žˆ๋Š”์ง€ +3. ``-`` : ``-``๋กœ ๋‚˜๋‰˜๋Š”์ง€ +4. ```]``` : ``]`` ๋กœ ๋๋‚˜๋Š”์ง€ + + +* ์ตœ์ข… : ``\\[(.+)-(.+)]`` + +----- + +## ๊ณ ๋ฏผ์‚ฌํ•ญ + +**๊ณ ๋ฏผ** +* ์ƒํ’ˆ์˜ ํ”„๋กœ๋ชจ์…˜ ์œ ๋ฌด๋ฅผ ๋”ฐ์ ธ ์–ด๋–ป๊ฒŒ ๊ตฌ๋งคํ• ๊นŒ? + +**ํ•ด๊ฒฐ ๊ณผ์ •** +1. ํ•ด๋‹น ์ด๋ฆ„์œผ๋กœ ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์ด ์žˆ๋Š”์ง€ ํŒ๋ณ„ํ•œ๋‹ค +2. ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์ด ์žˆ์„ ๊ฒฝ์šฐ + * ํ”„๋กœ๋ชจ์…˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ตฌ๋งคํ•œ ๊ฐœ์ˆ˜๋ณด๋‹ค ๋งŽ์€์ง€ ํŒ๋ณ„ํ•œ๋‹ค + * ํ”„๋กœ๋ชจ์…˜ ๊ฐœ์ˆ˜ >= ๊ตฌ๋งคํ•œ ๊ฐœ์ˆ˜ : ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๋ฅผ ์ฐจ๊ฐํ•œ๋‹ค + * ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ์ด ๊ฐ€๋Šฅํ•œ ์ƒํ’ˆ์— ๋Œ€ํ•ด ๊ณ ๊ฐ์ด ํ•ด๋‹น ์ˆ˜๋Ÿ‰๋งŒํผ ๊ฐ€์ ธ์˜ค์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ, ํ˜œํƒ์— ๋Œ€ํ•œ ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. + * ํ”„๋กœ๋ชจ์…˜ ๊ฐœ์ˆ˜ < ๊ตฌ๋งคํ•œ ๊ฐœ์ˆ˜ + * ํ”„๋กœ๋ชจ์…˜์ด ์–ผ๋งˆ๋‚˜ ์ ์šฉ๋˜๋Š”์ง€ ํŒŒ์•…ํ•˜๊ณ , ์–ผ๋งˆ๋‚˜ ์ ์šฉ ์•ˆ๋˜๋Š”์ง€ ํŒŒ์•…ํ•œ๋‹ค + * ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ์—์„œ ์ ์šฉ๋˜๋Š” ๋งŒํผ ์ฐจ๊ฐํ•˜๊ณ , ์ ์šฉ ์•ˆ๋˜๋Š” ๊ฐœ์ˆ˜๋ฅผ ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ์™€ ์ผ๋ฐ˜ ์žฌ๊ณ ์—์„œ ์ฐจ๊ฐํ•œ๋‹ค. + ![ํ”„๋กœ๋ชจ์…˜ ๊ณ„์‚ฐ ์ด๋ฏธ์ง€](/docs/image/ํ”„๋กœ๋ชจ์…˜๊ณ„์‚ฐ.jpeg) + * ํ•ด๋‹น ์ด๋ฏธ์ง€๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๊ณ„์‚ฐ ๋กœ์ง์„ ๊ตฌํ˜„ํ•œ๋‹ค + * ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜์—ฌ ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์„ ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ ์—†์ด ๊ฒฐ์ œํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์— ๋Œ€ํ•ด ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ• ์ง€ ์—ฌ๋ถ€์— ๋Œ€ํ•œ ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. + + + + +---- +## ๊ณ ๋ฏผ์‚ฌํ•ญ + +**๊ณ ๋ฏผ** +* ๊ตฌ๋งคํ•œ ์ƒํ’ˆ์—์„œ, ๊ตฌ๋งคํ•œ ๊ฐœ์ˆ˜์— ๋”ฐ๋ฅธ ํ”„๋กœ๋ชจ์…˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผ ํ•˜์ง€๋งŒ, ํ˜„์žฌ ๊ตฌ๋งคํ•œ ๋ชจ๋“  ์ƒํ’ˆ์€ promotion์— noPromotion์„ ํ• ๋‹นํ•จ + +**ํ•ด๊ฒฐ ๊ณผ์ •** +* enum ์„ ์„ ์–ธํ•˜์—ฌ ``์ด๋ฆ„ - ์–ด๋–ค ํ”„๋กœ๋ชจ์…˜์ธ์ง€ `` ํ˜•์‹์œผ๋กœ ์ƒ์ˆ˜๋“ค์„ ์ •ํ•ด์คŒ +* static ๋ฉ”์„œ๋“œ๋กœ ์ด๋ฆ„์— ๋”ฐ๋ผ ์–ด๋–ค ํ”„๋กœ๋ชจ์…˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ์ง€ ์ •ํ•ด์คŒ +* ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ตฌ๋งคํ•œ ์ƒํ’ˆ์„ ์ƒ์„ฑํ•  ๋•Œ, Promotion ๊ฐ’๋„ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. + +---- +## ๊ณ ๋ฏผ์‚ฌํ•ญ + +**๊ณ ๋ฏผ** +* Promotion์„ ํ•„๋“œ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”, Product ํด๋ž˜์Šค์— ํ”„๋กœ๋ชจ์…˜ ๊ณ„์‚ฐ ๋กœ์ง์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋งž์„๊นŒ? + +**ํ•ด๊ฒฐ ๊ณผ์ •** +* Product ํด๋ž˜์Šค์— Promotion ๊ณ„์‚ฐ ๋กœ์ง์„ ์ž‘์„ฑํ•˜๋‹ˆ, ๋„ˆ๋ฌด ๋งŽ์€ ์ฑ…์ž„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. +* ์‘์ง‘๋„ ํ–ฅ์ƒ์„ ์œ„ํ•ด Product ํด๋ž˜์Šค์— ์ƒํ’ˆ - ํ”„๋กœ๋ชจ์…˜ ๊ด€๋ จ ํ•„๋“œ๋ฅผ ๋‘์—ˆ์ง€๋งŒ ๊ณ„์‚ฐ ์ฑ…์ž„๊นŒ์ง€ ๊ฐ€์ง€๋Š” ๊ฒƒ์€ ๋น„ํšจ์œจ์ ์ž„. +* ๋”ฐ๋ผ์„œ Promotion ๊ด€๋ จ ๊ณ„์‚ฐ์„ ์ง„ํ–‰ํ•˜๋Š” PromotionService ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค. + +---- + +## ๊ณ ๋ฏผ์‚ฌํ•ญ + +**๊ณ ๋ฏผ** +* ์˜์ˆ˜์ฆ์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด, ์˜์ˆ˜์ฆ ๊ฐ์ฒด์— ์–ด๋–ค ์ •๋ณด๋ฅผ ๋‹ด์•„์•ผ ํ• ๊นŒ? + +**ํ•ด๊ฒฐ ๊ณผ์ •** +* ์˜์ˆ˜์ฆ์€ ๊ตฌ๋งคํ•œ ์ƒํ’ˆ์˜ ์ •๋ณด, ์ฆ์ • ์ˆ˜๋Ÿ‰, ์ด๊ตฌ๋งค์•ก, ํ–‰์‚ฌํ• ์ธ, ๋ฉค๋ฒ„์‹ญ, ๋‚ด์‹ค ๋ˆ ์ •๋ณด๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค. +* ํ˜„์žฌ ํ”„๋กœ๋ชจ์…˜์— ๊ด€ํ•œ ์ •๋ณด๋งŒ ์˜์ˆ˜์ฆ์— ๋“ฑ๋กํ•  ๊ฒƒ์ด๊ธฐ์— ๋ฉฅ๋ฒ„์‹ญ ๊ด€๋ จ ์ •๋ณด๋Š” ์ œ์™ธํ•˜๊ณ  ์ง„ํ–‰ํ•œ๋‹ค. +* Receipt ๊ฐ์ฒด์— ๊ตฌ๋งค ์ƒํ’ˆ ์ •๋ณด, ์ฆ์ •์ˆ˜๋Ÿ‰ ์ •๋ณด๋งŒ ๊ฐ€์ ธ์™€์„œ ๊ณ„์‚ฐํ•˜๋ฉด ๋œ๋‹ค. + * ํ–‰์‚ฌ ํ• ์ธ ์ •๋ณด์˜ ๊ฒฝ์šฐ, ํ•ด๋‹น ์ƒํ’ˆ์˜ ์ฆ์ • ์ˆ˜๋Ÿ‰์„ ํŒŒ์•…ํ•ด ๊ณ„์‚ฐํ•˜๋ฉด ๋œ๋‹ค. + * ์ด ๊ตฌ๋งค์•ก์˜ ๊ฒฝ์šฐ, ํ•ด๋‹น ์ƒํ’ˆ์„ ๊ตฌ๋งคํ•œ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ๊ณ„์‚ฐํ•˜๋ฉด ๋œ๋‹ค. + * ๋‚ด์‹ค ๋ˆ ์ •๋ณด์˜ ๊ฒฝ์šฐ, ์ด ๊ตฌ๋งค์•ก์—์„œ ํ• ์ธ ๋“ค์–ด๊ฐ„ ๊ฐ€๊ฒฉ์„ ๋นผ๋ฉด ๋œ๋‹ค. +* ๊ตฌ๋งค ์ƒํ’ˆ ์ •๋ณด์™€ ์ฆ์ •์ˆ˜๋Ÿ‰์„ ๋งค์นญํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— List ํ˜•ํƒœ ๋ณด๋‹ค๋Š”, Map ์ž๋ฃŒ๊ตฌ์กฐ ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์•„๋ณด์ธ๋‹ค. + + +* ์˜์ˆ˜์ฆ์— ์ •๋ณด๋ฅผ ๋‹ด์•˜๋Š”๋ฐ, ์•„์ง ๋ฉค๋ฒ„์‹ญ์„ ํ•ฉ์น˜์ง€ ์•Š์•˜์Œ. +* ๋ฉค๋ฒ„์‹ญ์„ ํ•ฉ์ณ์„œ ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์€ CustomerReceipt ํด๋ž˜์Šค๋ฅผ ํŒŒ์„œ ์ง„ํ–‰ (ํ•ด๋‹น ํด๋ž˜์Šค๋Š” ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด์ฃผ๋Š” ์—ญํ• ๋งŒ ํ•˜๋„๋ก) + +----
Unknown
README์— ๋ชจ๋‘ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ , ์—ฌ๋Ÿฌ๊ฐœ๋กœ ๋ถ„๋ฆฌํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”??
@@ -1,7 +1,19 @@ package store; +import store.controller.StoreController; +import store.domain.service.MemberShipCalculator; +import store.domain.service.PromotionCalculator; +import store.view.InputView; +import store.view.OutputView; + public class Application { public static void main(String[] args) { // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + OutputView outputView = new OutputView(); + InputView inputView = new InputView(); + MemberShipCalculator memberShipCalculator = new MemberShipCalculator(); + PromotionCalculator promotionCalculateService = new PromotionCalculator(); + StoreController storeController = new StoreController(memberShipCalculator, promotionCalculateService, outputView, inputView); + storeController.run(); } }
Java
controller ๋‚ด๋ถ€์—์„œ ์ƒ์„ฑ์ž ์ฃผ์ž…์œผ๋กœ ์ด๋ฏธ ๋ช…์‹œ๋ฅผ ํ•ด๋‘์—ˆ๋Š”๋ฐ, ๊ฐ์ฒด ์ƒ์„ฑ์„ ํ•œ๋ฒˆ ๋” ํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”! InputView์™€ OutputView๋Š” static์œผ๋กœ ํ˜ธ์ถœํ•˜๊ณ  ์•„๋ž˜์ฒ˜๋Ÿผ ํ•˜์‹œ๋ฉด ์–ด๋–จ๊นŒ์š”?? ```suggestion StoreController storeController = new StoreController(new MemberShipCalculator(), new PromotionCalculateService()); ```
@@ -0,0 +1,79 @@ +package store.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map.Entry; +import store.domain.vo.MemberShip; +import store.domain.vo.ProductQuantity; +import store.view.dto.response.BuyProductResponse; +import store.view.dto.response.FreeProductResponse; +import store.view.dto.response.ProductCalculateResponse; + +public class CustomerReceipt { + + private final Receipt receipt; + private final MemberShip memberShipDiscount; + + public CustomerReceipt(Receipt receipt, MemberShip memberShipDiscount) { + this.receipt = receipt; + this.memberShipDiscount = memberShipDiscount; + } + + public List<BuyProductResponse> showBuyProducts() { + List<BuyProductResponse> products = new ArrayList<>(); + for (Entry<Product, ProductQuantity> entry : receipt.getReceipt().entrySet()) { + products.add(new BuyProductResponse(entry.getKey().getName(), entry.getKey().getQuantity(), + entry.getKey().getQuantity() * entry.getKey().getPrice())); + } + return Collections.unmodifiableList(products); + } + + public List<FreeProductResponse> showFreeProductInfo() { + List<FreeProductResponse> freeProductResponses = new ArrayList<>(); + for (Entry<Product, ProductQuantity> entry : receipt.getReceipt().entrySet()) { + freeProductResponses.add( + new FreeProductResponse(entry.getKey().getName(), entry.getValue().getQuantity()) + ); + } + return Collections.unmodifiableList(freeProductResponses); + } + + public ProductCalculateResponse showCalculateInfo() { + return new ProductCalculateResponse( + productsAllQuantity(), + productsAllPrice(), + promotionPrice(), + memberShipDiscount.getDisCountPrice(), + totalPrice() + ); + } + + private int totalPrice() { + return productsAllPrice() - promotionPrice() - memberShipDiscount.getDisCountPrice(); + } + + private int productsAllPrice() { + return receipt.getReceipt() + .keySet() + .stream() + .mapToInt(product -> product.getPrice() * product.getQuantity()) + .sum(); + } + + private int promotionPrice() { + return receipt.getReceipt() + .entrySet() + .stream() + .mapToInt(entry -> entry.getKey().getPrice() * entry.getValue().getQuantity()) + .sum(); + } + + private int productsAllQuantity() { + return receipt.getReceipt() + .keySet() + .stream() + .mapToInt(Product::getQuantity) + .sum(); + } +}
Java
๋ฉ”์„œ๋“œ๋ช…์ด ๋ชจ๋‘ ์ง๊ด€์ ์ด์–ด์„œ ์ข‹๋„ค์š”๐Ÿ‘
@@ -0,0 +1,54 @@ +package store.domain; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import store.domain.exception.ProductErrorCode; +import store.domain.exception.ProductException; + +public class FileReader { + + private static final String CONTENT_SPLIT_DELIMITER = ","; + + private final List<List<String>> fileContents; + + public FileReader(String filePath) { + this.fileContents = makeFileContents(filePath); + } + + public List<List<String>> fileContents() { + return Collections.unmodifiableList(fileContents); + } + + private List<List<String>> makeFileContents(String filePath) { + List<List<String>> fileContents = new ArrayList<>(); + + try (BufferedReader fileReader = new BufferedReader(new java.io.FileReader(filePath))) { + skipOneLine(fileReader); + makeFileContent(fileReader, fileContents); + } catch (IOException e) { + throw new ProductException(ProductErrorCode.NOT_FOUND_FILE_PATH); + } + + return fileContents; + } + + private void makeFileContent(BufferedReader fileReader, List<List<String>> fileContents) throws IOException { + String line; + + while ((line = fileReader.readLine()) != null) { + List<String> contentInfo = splitProducts(line); + fileContents.add(contentInfo); + } + } + + private void skipOneLine(BufferedReader fileReader) throws IOException { + fileReader.readLine(); + } + + private List<String> splitProducts(String productInformation) { + return List.of(productInformation.split(CONTENT_SPLIT_DELIMITER)); + } +}
Java
List<List<String>>์œผ๋กœ ๊ตฌํ˜„ํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,45 @@ +package store.domain; + +import store.domain.exception.ProductErrorCode; +import store.domain.exception.ProductException; + +public enum ProductInfo { + + COKE("์ฝœ๋ผ", "1000"), + CIDER("์‚ฌ์ด๋‹ค", "1000"), + ORANGE_JUICE("์˜ค๋ Œ์ง€์ฃผ์Šค", "1800"), + SPARKLING_WATER("ํƒ„์‚ฐ์ˆ˜", "1200"), + WATER("๋ฌผ", "500"), + VITAMIN_WATER("๋น„ํƒ€๋ฏผ์›Œํ„ฐ", "1500"), + POTATO_CHIPS("๊ฐ์ž์นฉ", "1500"), + CHOCOLATE_BAR("์ดˆ์ฝ”๋ฐ”", "1200"), + ENERGY_BAR("์—๋„ˆ์ง€๋ฐ”", "2000"), + MEAL_BOX("์ •์‹๋„์‹œ๋ฝ", "6400"), + CUP_NOODLES("์ปต๋ผ๋ฉด", "1700"); + + private final String name; + private final String price; + + ProductInfo(String name, String price) { + this.name = name; + this.price = price; + } + + public static String findPriceByName(String name) { + for (ProductInfo product : ProductInfo.values()) { + if (product.getName().equals(name)) { + return product.getPrice(); + } + } + + throw new ProductException(ProductErrorCode.NOT_FOUND_PRODUCT_NAME); + } + + private String getName() { + return name; + } + + private String getPrice() { + return price; + } +}
Java
ํ•ด๋‹น ๋ถ€๋ถ„์€ ํŒŒ์ผ ๋‚ด์— ์žˆ๋Š” ๋‚ด์šฉ์„ ํ™œ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•ด์•ผํ•˜์ง€ ์•Š๋‚˜์šฉ?? ๋”ฐ๋กœ ๋ช…์‹œํ•ด๋‘์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,76 @@ +package store.view; + +import java.util.List; +import store.view.dto.response.BuyProductResponse; +import store.view.dto.response.ErrorResponse; +import store.view.dto.response.FreeProductResponse; +import store.view.dto.response.ProductCalculateResponse; + +public class OutputView { + + private static final String LINE_SEPARATOR = System.lineSeparator(); + + public void welcomeStore() { + System.out.println(LINE_SEPARATOR + "์•ˆ๋…•ํ•˜์„ธ์š”. WํŽธ์˜์ ์ž…๋‹ˆ๋‹ค." + LINE_SEPARATOR + "ํ˜„์žฌ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค." + LINE_SEPARATOR); + } + + public void showInventory(String name, int price, String quantity, String promotionType) { + System.out.printf( + "- %s %,d์› %s %s" + + LINE_SEPARATOR, + name, price, quantity, promotionType + ); + } + + public void getCustomerProducts() { + System.out.println(LINE_SEPARATOR + "๊ตฌ๋งคํ•˜์‹ค ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (์˜ˆ: [์‚ฌ์ด๋‹ค-2],[๊ฐ์ž์นฉ-1])"); + } + + public void guideBuyProducts() { + System.out.printf( + LINE_SEPARATOR + + "==============W ํŽธ์˜์ ================" + + LINE_SEPARATOR + + "%-10s %9s %9s%n" + , "์ƒํ’ˆ๋ช…", "์ˆ˜๋Ÿ‰" ,"๊ธˆ์•ก" + ); + } + + public void showBuyProducts(List<BuyProductResponse> buyProducts) { + for (BuyProductResponse product : buyProducts) { + System.out.printf( + "%-10s %9d %,14d" + LINE_SEPARATOR, + product.name(), product.quantity(), product.totalPrice() + ); + } + } + + public void guideFreeGetQuantity() { + System.out.println("=============์ฆ\t\t์ •==============="); + } + + public void showFreeQuantity(List<FreeProductResponse> freeProductResponses) { + for (FreeProductResponse freeProductResponse : freeProductResponses) { + System.out.printf( + "%-10s %9d" + LINE_SEPARATOR, + freeProductResponse.name(), freeProductResponse.quantity() + ); + } + } + + public void showCalculateResult(ProductCalculateResponse calculateInfo) { + System.out.printf( + "====================================" + LINE_SEPARATOR + + "์ด๊ตฌ๋งค์•ก\t\t\t\t%d\t\t %,d" + LINE_SEPARATOR + + "ํ–‰์‚ฌํ• ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR + + "๋ฉค๋ฒ„์‹ญํ• ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR + + "๋‚ด์‹ค๋ˆ\t\t\t\t\t\t %,d" + LINE_SEPARATOR + , calculateInfo.buyCounts(), calculateInfo.buyProductsPrice(), + calculateInfo.promotionPrice(), calculateInfo.memberShip(), calculateInfo.totalPrice() + ); + } + + public void showError(ErrorResponse errorResponse) { + System.out.println(errorResponse.message()); + } +}
Java
System.lineSeparator()๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ตฐ์š”!! ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค:)
@@ -0,0 +1,6 @@ +package store.view.dto.request; + +public record MemberShipRequest( + boolean isMemberShip +) { +}
Java
๋ถˆ๋ณ€์ž„์„ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋Š” record ์‚ฌ์šฉ ์ข‹๋„ค์š”!!๐Ÿ‘
@@ -0,0 +1,63 @@ +package store.domain; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import store.domain.exception.ProductErrorCode; +import store.domain.exception.ProductException; + +public class BuyProductParser { + + private static final int NAME_INDEX = 1; + private static final int PRICE_INDEX = 2; + private static final String INPUT_DELIMITER = ","; + private static final String ORDER_FORM = "\\[(.+)-(.+)]"; + + private final Map<String, String> products; + + public BuyProductParser(String buyProduct) { + this.products = makeProductsForm(buyProduct); + } + + public Map<String, String> getProducts() { + return Collections.unmodifiableMap(products); + } + + private Map<String, String> makeProductsForm(String buyProducts) { + Map<String, String> products = new LinkedHashMap<>(); + parseProducts(buyProducts, products); + return products; + } + + private void parseProducts(String buyProducts, Map<String, String> products) { + Pattern pattern = Pattern.compile(ORDER_FORM); + parseByRest(buyProducts).forEach(product -> { + validateFrom(product); + Matcher matcher = pattern.matcher(product); + validateMatcher(matcher); + products.put(matcher.group(NAME_INDEX).trim(), matcher.group(PRICE_INDEX).trim()); + }); + } + + private void validateMatcher(Matcher matcher) { + if (!matcher.matches()) { + throw new ProductException(ProductErrorCode.WRONG_INPUT); + } + } + + private List<String> parseByRest(String buyProduct) { + if (buyProduct.contains(INPUT_DELIMITER)) { + return List.of(buyProduct.split(INPUT_DELIMITER)); + } + return List.of(buyProduct); + } + + private void validateFrom(String buyProducts) { + if (!buyProducts.matches(ORDER_FORM)) { + throw new ProductException(ProductErrorCode.WRONG_INPUT); + } + } +}
Java
parse ํด๋ž˜์Šค๋Š” domain๋ณด๋‹ค util ํŒจํ‚ค์ง€ ๋‚ด์— ์žˆ๋Š”๊ฒŒ ์ ํ•ฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ, ์ƒํœ˜๋‹˜์˜ ์˜๊ฒฌ์€ ์–ด๋– ์‹ ๊ฐ€์š”?
@@ -0,0 +1,54 @@ +package store.domain; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import store.domain.exception.ProductErrorCode; +import store.domain.exception.ProductException; + +public class FileReader { + + private static final String CONTENT_SPLIT_DELIMITER = ","; + + private final List<List<String>> fileContents; + + public FileReader(String filePath) { + this.fileContents = makeFileContents(filePath); + } + + public List<List<String>> fileContents() { + return Collections.unmodifiableList(fileContents); + } + + private List<List<String>> makeFileContents(String filePath) { + List<List<String>> fileContents = new ArrayList<>(); + + try (BufferedReader fileReader = new BufferedReader(new java.io.FileReader(filePath))) { + skipOneLine(fileReader); + makeFileContent(fileReader, fileContents); + } catch (IOException e) { + throw new ProductException(ProductErrorCode.NOT_FOUND_FILE_PATH); + } + + return fileContents; + } + + private void makeFileContent(BufferedReader fileReader, List<List<String>> fileContents) throws IOException { + String line; + + while ((line = fileReader.readLine()) != null) { + List<String> contentInfo = splitProducts(line); + fileContents.add(contentInfo); + } + } + + private void skipOneLine(BufferedReader fileReader) throws IOException { + fileReader.readLine(); + } + + private List<String> splitProducts(String productInformation) { + return List.of(productInformation.split(CONTENT_SPLIT_DELIMITER)); + } +}
Java
FileReader๋„ util ํŒจํ‚ค์ง€ ๋‚ด์— ์žˆ๋Š”๊ฒŒ ์ ํ•ฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,86 @@ +package store.domain; + +import java.util.Objects; +import store.domain.vo.ProductName; +import store.domain.vo.ProductPrice; +import store.domain.vo.ProductQuantity; + +public class Product { + + private final ProductName name; + private final ProductPrice price; + private final ProductQuantity quantity; + private final Promotion promotion; + + public Product(String name, String price, String quantity, Promotion promotion) { + this.name = ProductName.from(name); + this.price = ProductPrice.from(price); + this.quantity = ProductQuantity.from(quantity); + this.promotion = promotion; + } + + public String getName() { + return name.getName(); + } + + public int getPrice() { + return price.getPrice(); + } + + public int getQuantity() { + return quantity.getQuantity(); + } + + public String showQuantity() { + return quantity.showQuantity(); + } + + public String getPromotionType() { + return promotion.getPromotionType(); + } + + public boolean isPromotion() { + return promotion.isPromotion(); + } + + public int getPromotionBuyQuantity() { + return promotion.getBuyQuantity(); + } + + public int getPromotionGettableQuantity() { + return promotion.getGettableQuantity(); + } + + public int getPromotionQuantity() { + return (quantity.getQuantity() / getOnePromotionCycle()) * promotion.getGettableQuantity(); + } + + public int getOnePromotionCycle() { + return promotion.getOnePromotionCycle(); + } + + public void buyProduct(int buyQuantity) { + quantity.minusQuantity(buyQuantity); + } + + public void freeProduct(int addQuantity) { + quantity.addQuantity(addQuantity); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Product that = (Product) o; + return name.equals(that.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } +}
Java
๋„๋ฉ”์ธ ํŒจํ‚ค์ง€ ๋‚ด์— ํด๋ž˜์Šค๊ฐ€ ๋งŽ์€ ๊ฒƒ ๊ฐ™์•„์š”! product์™€ promotion ๋“ฑ์˜ ํŒจํ‚ค์ง€๋ฅผ ํ•œ๋ฒˆ ๋” ๋งŒ๋“ค์–ด์„œ ๋ถ„๋ฆฌํ•ด์ฃผ์‹œ๋ฉด ๋„๋ฉ”์ธ์˜ ์—ญํ• ์ด ๋ช…ํ™•ํžˆ ๋ณด์ผ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,63 @@ +package store.domain; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import store.domain.exception.ProductErrorCode; +import store.domain.exception.ProductException; + +public class BuyProductParser { + + private static final int NAME_INDEX = 1; + private static final int PRICE_INDEX = 2; + private static final String INPUT_DELIMITER = ","; + private static final String ORDER_FORM = "\\[(.+)-(.+)]"; + + private final Map<String, String> products; + + public BuyProductParser(String buyProduct) { + this.products = makeProductsForm(buyProduct); + } + + public Map<String, String> getProducts() { + return Collections.unmodifiableMap(products); + } + + private Map<String, String> makeProductsForm(String buyProducts) { + Map<String, String> products = new LinkedHashMap<>(); + parseProducts(buyProducts, products); + return products; + } + + private void parseProducts(String buyProducts, Map<String, String> products) { + Pattern pattern = Pattern.compile(ORDER_FORM); + parseByRest(buyProducts).forEach(product -> { + validateFrom(product); + Matcher matcher = pattern.matcher(product); + validateMatcher(matcher); + products.put(matcher.group(NAME_INDEX).trim(), matcher.group(PRICE_INDEX).trim()); + }); + } + + private void validateMatcher(Matcher matcher) { + if (!matcher.matches()) { + throw new ProductException(ProductErrorCode.WRONG_INPUT); + } + } + + private List<String> parseByRest(String buyProduct) { + if (buyProduct.contains(INPUT_DELIMITER)) { + return List.of(buyProduct.split(INPUT_DELIMITER)); + } + return List.of(buyProduct); + } + + private void validateFrom(String buyProducts) { + if (!buyProducts.matches(ORDER_FORM)) { + throw new ProductException(ProductErrorCode.WRONG_INPUT); + } + } +}
Java
private ๋ฉ”์„œ๋“œ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,56 @@ +package store.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import store.domain.exception.ProductErrorCode; +import store.domain.exception.ProductException; + +public class BuyProducts { + + private final List<Product> products; + + public BuyProducts(Map<String, String> splitProducts, Promotions promotions, Products products) { + this.products = createBuyProducts(splitProducts, promotions); + validateExistence(products); + validateQuantity(products); + } + + public List<Product> products() { + return Collections.unmodifiableList(products); + } + + private List<Product> createBuyProducts(Map<String, String> splitProducts, Promotions promotions) { + List<Product> products = new ArrayList<>(); + + for (Entry<String, String> entry : splitProducts.entrySet()) { + Product product = new Product( + entry.getKey(), + ProductInfo.findPriceByName(entry.getKey()), + entry.getValue(), + promotions.getPromotionByType(PromotionInfo.findPromotionByName(entry.getKey()))); + products.add(product); + } + + return products; + } + + private void validateExistence(Products inventoryProducts) { + for (Product buyProduct : products) { + if (!inventoryProducts.getProducts().contains(buyProduct)) { + throw new ProductException(ProductErrorCode.NOT_FOUND_PRODUCT_NAME); + } + } + } + + private void validateQuantity(Products inventoryProducts) { + for (Product buyProduct : products) { + int availableQuantity = inventoryProducts.countProductsByName(buyProduct.getName()); + if (availableQuantity < buyProduct.getQuantity()) { + throw new ProductException(ProductErrorCode.OVER_QUANTITY); + } + } + } +}
Java
๋ฐ˜ํ™˜ํ•ด์ค„๋–„ ๋ฐ์ดํ„ฐ์˜ ์•ˆ์ •์„ฑ์„ ์ง€ํ‚ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜์…จ๊ตฐ์š” ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค
@@ -0,0 +1,56 @@ +package store.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import store.domain.exception.ProductErrorCode; +import store.domain.exception.ProductException; + +public class BuyProducts { + + private final List<Product> products; + + public BuyProducts(Map<String, String> splitProducts, Promotions promotions, Products products) { + this.products = createBuyProducts(splitProducts, promotions); + validateExistence(products); + validateQuantity(products); + } + + public List<Product> products() { + return Collections.unmodifiableList(products); + } + + private List<Product> createBuyProducts(Map<String, String> splitProducts, Promotions promotions) { + List<Product> products = new ArrayList<>(); + + for (Entry<String, String> entry : splitProducts.entrySet()) { + Product product = new Product( + entry.getKey(), + ProductInfo.findPriceByName(entry.getKey()), + entry.getValue(), + promotions.getPromotionByType(PromotionInfo.findPromotionByName(entry.getKey()))); + products.add(product); + } + + return products; + } + + private void validateExistence(Products inventoryProducts) { + for (Product buyProduct : products) { + if (!inventoryProducts.getProducts().contains(buyProduct)) { + throw new ProductException(ProductErrorCode.NOT_FOUND_PRODUCT_NAME); + } + } + } + + private void validateQuantity(Products inventoryProducts) { + for (Product buyProduct : products) { + int availableQuantity = inventoryProducts.countProductsByName(buyProduct.getName()); + if (availableQuantity < buyProduct.getQuantity()) { + throw new ProductException(ProductErrorCode.OVER_QUANTITY); + } + } + } +}
Java
์ €๋„ entry๋ฅผ ์ฝ”ํ…Œ๋ฅผ ๋ณผ ๋•Œ ๋ณ„๋„์˜ ํด๋ž˜์Šค ์ž‘์„ฑ์ด ํ•„์š”์—†๊ณ  ํŽธํ•ด ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ด๋Š” ๊ฐ€๋…์„ฑ ๋ฉด์—์„œ ๋งŽ์ด ๋–จ์–ด์ง„๋‹ค๊ณ  ๋А๋‚๋‹ˆ๋‹ค ์ด๋ณด๋‹ค๋Š” ๋”ฐ๋กœ DTO๋ฅผ ๋งŒ๋“œ๋Š”๊ฒŒ ๋‚˜์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค