code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,43 @@ +package christmas.model + +import christmas.utils.Constant.CHRISTMAS_DAY +import christmas.utils.Constant.EMPTY + +enum class DiscountType(val eventName: String) { + THE_DAY_DISCOUNT("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"), + WEEKDAY_DISCOUNT("ํ‰์ผ ํ• ์ธ"), + WEEKEND_DISCOUNT("์ฃผ๋ง ํ• ์ธ"), + SPECIAL_DISCOUNT("ํŠน๋ณ„ ํ• ์ธ"); + + companion object { + private val decemberEvent = DecemberEvent() + + fun requestTheDayDiscount(visitDate: Int): Int { + if (visitDate <= CHRISTMAS_DAY) + return decemberEvent.applyTheDayDiscount(visitDate) + + return EMPTY + } + + fun requestWeekdayDiscount(visitDate: Int, orderMenu: List<OrderItems>): Int { + if (visitDate in DecemberCalender.WEEKDAY.dates) + return decemberEvent.applyWeekdayDiscount(orderMenu) + + return EMPTY + } + + fun requestWeekendDiscount(visitDate: Int, orderMenu: List<OrderItems>): Int { + if (visitDate in DecemberCalender.WEEKEND.dates) + return decemberEvent.applyWeekendDiscount(orderMenu) + + return EMPTY + } + + fun requestSpecialDiscount(visitDate: Int): Int { + if (visitDate in DecemberCalender.SPECIAL_DAY.dates) + return decemberEvent.applySpecialDiscount() + + return EMPTY + } + } +} \ No newline at end of file
Kotlin
enum class ์† companion object๋Š” ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋‚˜์š”? ์ž˜ ๋ชฐ๋ผ์„œ ๋ฌผ์–ด๋ด…๋‹ˆ๋‹ค ๐Ÿ˜“
@@ -0,0 +1,59 @@ +package christmas.utils + +object Constant { + /* Model */ + const val FIRST_DAY_DISCOUNT_AMOUNT = 1_000 + const val DAILY_DISCOUNT_AMOUNT = 100 + const val ONE_DAY_DECREASE = 1 + const val WEEK_DISCOUNT_AMOUNT = 2_023 + const val CHRISTMAS_DAY_DISCOUNT_AMOUNT = 1_000 + const val CHAMPAGNE_CONDITION_AMOUNT = 120_000 + const val CHRISTMAS_DAY = 25 + const val DISCOUNT_THRESHOLD = 10_000 + const val EMPTY = 0 + const val BADGE_STAR = "๋ณ„" + const val BADGE_TREE = "ํŠธ๋ฆฌ" + const val BADGE_SANTA = "์‚ฐํƒ€" + const val NONE = "์—†์Œ" + const val STAR_MIN = 5_000 + const val STAR_MAX = 9_999 + const val TREE_MIN = 10_000 + const val TREE_MAX = 19_999 + const val SANTA_MIN = 20_000 + const val FIRST_INDEX = 0 + const val SECOND_INDEX = 1 + + /* Validate */ + const val ORDER_ITEMS_SIZE = 2 + const val VISIT_DATE_MIN = 1 + const val VISIT_DATE_MAX = 31 + const val QUANTITY_MINIMUM = 1 + const val QUANTITY_MAXIMUM = 20 + const val HYPHEN = "-" + const val COMMA = "," + const val NUMERIC_REGEX = "^\\d+$" + private const val ERROR_MESSAGE_PREFIX = "[ERROR] " + const val ERROR_MESSAGE_INPUT_VISIT_DATE = ERROR_MESSAGE_PREFIX + "์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + const val ERROR_MESSAGE_INPUT_MENU = ERROR_MESSAGE_PREFIX + "์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + + /* InputView */ + const val VISIT_DATE_ANNOUNCE_MESSAGE = "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)" + const val MENU_ANNOUNCE_MESSAGE = "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)" + + /* OutPutView */ + private const val PRINT_ENTER = "\n" + const val START_PLANNER_MESSAGE = "์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค." + const val PREVIEW_ANNOUNCE_MESSAGE = "12์›” %d์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!" + const val ORDER_MENU_TITLE_MESSAGE = "$PRINT_ENTER<์ฃผ๋ฌธ ๋ฉ”๋‰ด>" + const val ITEMS_RESULT = "%s %d๊ฐœ" + const val TOTAL_ORDER_SUM_TITLE_MESSAGE = "$PRINT_ENTER<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>" + const val TOTAL_AMOUNT_RESULT = "%,d์›" + const val PRESENT_MENU_TITLE_MESSAGE = "$PRINT_ENTER<์ฆ์ • ๋ฉ”๋‰ด>" + const val PRESENT_AMOUNT = 1 + const val BENEFIT_DETAILS_TITLE_MESSAGE = "$PRINT_ENTER<ํ˜œํƒ ๋‚ด์—ญ>" + const val FREEBIE_EVENT_NAME = "์ฆ์ • ์ด๋ฒคํŠธ" + const val BENEFIT_RESULT = "%s: -%,d์›" + const val BENEFIT_AMOUNT_TITLE_MESSAGE = "$PRINT_ENTER<์ดํ˜œํƒ ๊ธˆ์•ก>" + const val PAYMENT_AMOUNT_TITLE_MESSAGE = "$PRINT_ENTER<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>" + const val BADGE_TITLE_MESSAGE = "$PRINT_ENTER<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>" +} \ No newline at end of file
Kotlin
์ด๋ ‡๊ฒŒ๋„ ์“ธ ์ˆ˜ ์žˆ๊ตฐ์š”! ์ €๋Š” ์ด๋Ÿฐ์‹์œผ๋กœ ์ผ์–ด์š”! ``` enum class ErrorMessage(private val message: String) { ERROR_MENU_INPUT("์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), ERROR_INPUT_DAY("์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); fun getMessage(): String = "[ERROR] $message" } ```
@@ -0,0 +1,10 @@ +package christmas.model + +data class OrderItems( + private val menuName: String, + private val quantity: Int +) { + fun getMenuName(): String = menuName + + fun getQuantity(): Int = quantity +}
Kotlin
val ์„ ์–ธ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ private๋กœ ์ œํ•œํ•˜์ง€ ์•Š์•„๋„ ๊ฐ’ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ• ํ…๋ฐ, ํ˜น์‹œ private๋กœ ์„ ์–ธํ•œ ์ด์œ ๊ฐ€ ๋”ฐ๋กœ ์žˆ์„๊นŒ์š”..?!
@@ -0,0 +1,56 @@ +package christmas + +import christmas.model.DecemberEvent +import christmas.model.OrderItems +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class DecemberEventTest { + private val decemberEvent: DecemberEvent = DecemberEvent() + + @Test + fun `๋””๋ฐ์ด ํ• ์ธ ๊ธˆ์•ก ํ…Œ์ŠคํŠธ`() { + val discountAmount = decemberEvent.applyTheDayDiscount(25) + + Assertions.assertTrue(discountAmount == 3400) + } + + @Test + fun `ํ‰์ผ ํ• ์ธ ๊ธˆ์•ก ํ…Œ์ŠคํŠธ`() { + val discountAmount = decemberEvent.applyWeekdayDiscount(listOf(OrderItems("์ดˆ์ฝ”์ผ€์ดํฌ", 2), OrderItems("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 3))) + + Assertions.assertTrue(discountAmount == 4046) + } + + @Test + fun `์ฃผ๋ง ํ• ์ธ ๊ธˆ์•ก ํ…Œ์ŠคํŠธ`() { + val discountAmount = decemberEvent.applyWeekendDiscount(listOf(OrderItems("์ดˆ์ฝ”์ผ€์ดํฌ", 2), OrderItems("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 3))) + + Assertions.assertTrue(discountAmount == 6069) + } + + @Test + fun `ํŠน๋ณ„ ํ• ์ธ ๊ธˆ์•ก ํ…Œ์ŠคํŠธ`() { + val discountAmount = decemberEvent.applySpecialDiscount() + + Assertions.assertTrue(discountAmount == 1000) + } + + @Test + fun `์ƒดํŽ˜์ธ ์ฆ์ • ํ…Œ์ŠคํŠธ`() { + Assertions.assertTrue(decemberEvent.presentChampagne(130_000)) + } + + @Test + fun `๋ฐฐ์ง€ ๋ถ€์—ฌ ํ…Œ์ŠคํŠธ`() { + val noneBadge = decemberEvent.assignBadge(0) + val starBadge = decemberEvent.assignBadge(5000) + val treeBadge = decemberEvent.assignBadge(10000) + val santaBadge = decemberEvent.assignBadge(200000) + + Assertions.assertTrue(noneBadge == "์—†์Œ") + Assertions.assertTrue(starBadge == "๋ณ„") + Assertions.assertTrue(treeBadge == "ํŠธ๋ฆฌ") + Assertions.assertTrue(santaBadge == "์‚ฐํƒ€") + } +} \ No newline at end of file
Kotlin
์ด ๋ถ€๋ถ„๋„ decemberEvent.assignBadge๊ฐ€ ๋ฐ˜๋ณต๋˜๊ธฐ ๋•Œ๋ฌธ์— @ParameterizedTest๋กœ ๊ฐ„๋‹จํžˆ ๋งŒ๋“ค์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™์•„์š”!! ๊ฐœ์ธ์ ์œผ๋กœ ์ €๋Š” ์ €๋ ‡๊ฒŒ ์ธ์ž๊ฐ€ ๋‘๊ฐœ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” @MethodSource๋กœ ๊ฐ„๋‹จํžˆ ํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค! [์—ฌ๊ธฐ ๋งํฌ](https://ebabby.tistory.com/28) ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ ๊ฑฐ ๊ฐ™์•„์š”!!
@@ -0,0 +1,82 @@ +package christmas.view + +import christmas.model.DiscountType +import christmas.model.OrderItems +import christmas.model.StoreMenu +import christmas.utils.Constant.BADGE_TITLE_MESSAGE +import christmas.utils.Constant.BENEFIT_AMOUNT_TITLE_MESSAGE +import christmas.utils.Constant.BENEFIT_DETAILS_TITLE_MESSAGE +import christmas.utils.Constant.BENEFIT_RESULT +import christmas.utils.Constant.FREEBIE_EVENT_NAME +import christmas.utils.Constant.EMPTY +import christmas.utils.Constant.ITEMS_RESULT +import christmas.utils.Constant.NONE +import christmas.utils.Constant.ORDER_MENU_TITLE_MESSAGE +import christmas.utils.Constant.PAYMENT_AMOUNT_TITLE_MESSAGE +import christmas.utils.Constant.PRESENT_AMOUNT +import christmas.utils.Constant.PRESENT_MENU_TITLE_MESSAGE +import christmas.utils.Constant.PREVIEW_ANNOUNCE_MESSAGE +import christmas.utils.Constant.START_PLANNER_MESSAGE +import christmas.utils.Constant.TOTAL_AMOUNT_RESULT +import christmas.utils.Constant.TOTAL_ORDER_SUM_TITLE_MESSAGE + +class OutputView { + fun printStartPlanner() { + println(START_PLANNER_MESSAGE) + } + + fun printPreviewAnnounce(visitDate: Int) { + println(PREVIEW_ANNOUNCE_MESSAGE.format(visitDate)) + } + + fun printOrderMenu(orderMenu: List<OrderItems>) { + println(ORDER_MENU_TITLE_MESSAGE) + orderMenu.forEach { items -> + println(ITEMS_RESULT.format(items.getMenuName(), items.getQuantity())) + } + } + + fun printTotalOrderPrize(totalOrderSum: Int) { + println(TOTAL_ORDER_SUM_TITLE_MESSAGE) + println(TOTAL_AMOUNT_RESULT.format(totalOrderSum)) + + } + + fun printFreebieMenu(freebie: Boolean) { + println(PRESENT_MENU_TITLE_MESSAGE) + when { + freebie -> println(ITEMS_RESULT.format(StoreMenu.CHAMPAGNE.menuName, PRESENT_AMOUNT)) + !freebie -> println(NONE) + } + } + + fun printBenefitDetails(discountHistory: List<Pair<DiscountType, Int>>, freebie: Boolean) { + val discountHistoryExist = discountHistory.filter { it.second != EMPTY } + + println(BENEFIT_DETAILS_TITLE_MESSAGE) + discountHistoryExist.forEach { (discountType, discountAmount) -> + println(BENEFIT_RESULT.format(discountType.eventName, discountAmount)) + } + + if(freebie) + println(BENEFIT_RESULT.format(FREEBIE_EVENT_NAME, StoreMenu.CHAMPAGNE.menuPrice)) + + if(discountHistoryExist.isEmpty() && !freebie) + println(NONE) + } + + fun printBenefitAmount(totalBenefitAmount: Int) { + println(BENEFIT_AMOUNT_TITLE_MESSAGE) + println(TOTAL_AMOUNT_RESULT.format(EMPTY - totalBenefitAmount)) + } + + fun printPaymentAmount(totalOrderSum: Int, discountHistory: List<Pair<DiscountType, Int>>) { + println(PAYMENT_AMOUNT_TITLE_MESSAGE) + println(TOTAL_AMOUNT_RESULT.format(totalOrderSum - discountHistory.sumOf { it.second })) + } + + fun printBadge(badge: String) { + println(BADGE_TITLE_MESSAGE) + println(badge) + } +} \ No newline at end of file
Kotlin
์ด ๋ถ€๋ถ„์€ else๋ฅผ ์ง€์–‘ํ•ด์•ผํ•œ๋‹ค๋Š” ์š”๊ตฌ์‚ฌํ•ญ๋•Œ๋ฌธ์— ๊ณ ๋ฏผ์ด ๋งŽ์•˜์—ˆ๋Š”๋ฐ, ์ €๋Š” ๊ทธ๋ž˜๋„ ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜์„ ์ง€ํ‚ค๋Š”๊ฒŒ ์ข‹์„๊ฑฐ ๊ฐ™์•„์„œ if else ๋ฌธ์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค! ์ด ๋ถ€๋ถ„์€ ์‚ฌ๋žŒ๋งˆ๋‹ค ๊ด€์ ์ด ๋‹ค๋ฅผ๊ฑฐ ๊ฐ™์•„์„œ ๊ทธ๋ƒฅ ์ฐธ๊ณ ๋งŒ ํ•ด์ฃผ์„ธ์š”!! [๊ด€๋ จ ์ปจ๋ฒค์…˜ ๋งํฌ](https://kotlinlang.org/docs/coding-conventions.html#if-versus-when)์ž…๋‹ˆ๋‹ค!!
@@ -0,0 +1,79 @@ +package christmas.model + +import christmas.utils.Constant.BADGE_SANTA +import christmas.utils.Constant.BADGE_STAR +import christmas.utils.Constant.BADGE_TREE +import christmas.utils.Constant.CHAMPAGNE_CONDITION_AMOUNT +import christmas.utils.Constant.CHRISTMAS_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.DAILY_DISCOUNT_AMOUNT +import christmas.utils.Constant.FIRST_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.EMPTY +import christmas.utils.Constant.NONE +import christmas.utils.Constant.ONE_DAY_DECREASE +import christmas.utils.Constant.SANTA_MIN +import christmas.utils.Constant.STAR_MAX +import christmas.utils.Constant.STAR_MIN +import christmas.utils.Constant.TREE_MAX +import christmas.utils.Constant.TREE_MIN +import christmas.utils.Constant.WEEK_DISCOUNT_AMOUNT + +class DecemberEvent { + fun applyTheDayDiscount(date: Int): Int { + val dailyDiscount = (date - ONE_DAY_DECREASE) * DAILY_DISCOUNT_AMOUNT + + return FIRST_DAY_DISCOUNT_AMOUNT + dailyDiscount + } + + fun applyWeekdayDiscount(orderMenu: List<OrderItems>): Int { + var dessertMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseDessertMenuCount(item.getMenuName(), item.getQuantity()) + dessertMenuCount += increaseCount + } + + return dessertMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseDessertMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.CHOCO_CAKE.menuName || menuName == StoreMenu.ICE_CREAM.menuName) + return quantity + + return EMPTY + } + + + fun applyWeekendDiscount(orderMenu: List<OrderItems>): Int { + var mainMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseMainMenuCount(item.getMenuName(), item.getQuantity()) + mainMenuCount += increaseCount + } + + return mainMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseMainMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.T_BONE_STEAK.menuName || menuName == StoreMenu.BBQ_RIBS.menuName || + menuName == StoreMenu.SEAFOOD_PASTA.menuName || menuName == StoreMenu.CHRISTMAS_PASTA.menuName + ) + return quantity + + return EMPTY + } + + fun applySpecialDiscount(): Int = CHRISTMAS_DAY_DISCOUNT_AMOUNT + + fun presentChampagne(totalOrderAmount: Int): Boolean = totalOrderAmount >= CHAMPAGNE_CONDITION_AMOUNT + + fun assignBadge(benefitAmount: Int): String { + when { + benefitAmount in STAR_MIN..STAR_MAX -> return BADGE_STAR + benefitAmount in TREE_MIN..TREE_MAX -> return BADGE_TREE + benefitAmount >= SANTA_MIN -> return BADGE_SANTA + } + + return NONE + } +} \ No newline at end of file
Kotlin
์œผ์•…,,๊ฐ€๋…์„ฑ์„ ๋งŽ์ด ์‹ ๊ฒฝ ์“ด ๋ฏธ์…˜์ด์—ˆ๋Š”๋ฐ ๋†“์นœ ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋„ค์š”. ๋ง์”€ํ•ด์ฃผ์‹  ๋ฐฉ๋ฒ•๋“ค๋„ ELSE๋ฅผ ์ง€์–‘ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ๊ณ ๋ คํ•ด๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”. ์ง€๊ธˆ ๋“œ๋Š” ์ƒ๊ฐ์€ ๋ฉ”์ธ ๋ฉ”๋‰ด๋“ค์„ ๋งต์ด๋‚˜ ๋ฆฌ์ŠคํŠธ๋กœ ๊ทธ๋ฃนํ™”ํ•˜๊ณ  ์กฐ๊ฑด ๊ฒ€์‚ฌ๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค๋ฉด ํ•œ๋ฒˆ์˜ ๊ฒ€์‚ฌ๋กœ๋„ ์ถฉ๋ถ„ํžˆ ๋™์ผํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ–ˆ์„ ์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™์•„์š”..๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ๋Š” ๋™์ผํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฆฌํŒฉํ† ๋ง์„ ์ง„ํ–‰ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„์‰ฌ์›€์ด ๋งŽ์ด ๋‚จ๋Š” ๋ถ€๋ถ„์ด๋„ค์š”..๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,59 @@ +package christmas.utils + +object Constant { + /* Model */ + const val FIRST_DAY_DISCOUNT_AMOUNT = 1_000 + const val DAILY_DISCOUNT_AMOUNT = 100 + const val ONE_DAY_DECREASE = 1 + const val WEEK_DISCOUNT_AMOUNT = 2_023 + const val CHRISTMAS_DAY_DISCOUNT_AMOUNT = 1_000 + const val CHAMPAGNE_CONDITION_AMOUNT = 120_000 + const val CHRISTMAS_DAY = 25 + const val DISCOUNT_THRESHOLD = 10_000 + const val EMPTY = 0 + const val BADGE_STAR = "๋ณ„" + const val BADGE_TREE = "ํŠธ๋ฆฌ" + const val BADGE_SANTA = "์‚ฐํƒ€" + const val NONE = "์—†์Œ" + const val STAR_MIN = 5_000 + const val STAR_MAX = 9_999 + const val TREE_MIN = 10_000 + const val TREE_MAX = 19_999 + const val SANTA_MIN = 20_000 + const val FIRST_INDEX = 0 + const val SECOND_INDEX = 1 + + /* Validate */ + const val ORDER_ITEMS_SIZE = 2 + const val VISIT_DATE_MIN = 1 + const val VISIT_DATE_MAX = 31 + const val QUANTITY_MINIMUM = 1 + const val QUANTITY_MAXIMUM = 20 + const val HYPHEN = "-" + const val COMMA = "," + const val NUMERIC_REGEX = "^\\d+$" + private const val ERROR_MESSAGE_PREFIX = "[ERROR] " + const val ERROR_MESSAGE_INPUT_VISIT_DATE = ERROR_MESSAGE_PREFIX + "์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + const val ERROR_MESSAGE_INPUT_MENU = ERROR_MESSAGE_PREFIX + "์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + + /* InputView */ + const val VISIT_DATE_ANNOUNCE_MESSAGE = "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)" + const val MENU_ANNOUNCE_MESSAGE = "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)" + + /* OutPutView */ + private const val PRINT_ENTER = "\n" + const val START_PLANNER_MESSAGE = "์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค." + const val PREVIEW_ANNOUNCE_MESSAGE = "12์›” %d์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!" + const val ORDER_MENU_TITLE_MESSAGE = "$PRINT_ENTER<์ฃผ๋ฌธ ๋ฉ”๋‰ด>" + const val ITEMS_RESULT = "%s %d๊ฐœ" + const val TOTAL_ORDER_SUM_TITLE_MESSAGE = "$PRINT_ENTER<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>" + const val TOTAL_AMOUNT_RESULT = "%,d์›" + const val PRESENT_MENU_TITLE_MESSAGE = "$PRINT_ENTER<์ฆ์ • ๋ฉ”๋‰ด>" + const val PRESENT_AMOUNT = 1 + const val BENEFIT_DETAILS_TITLE_MESSAGE = "$PRINT_ENTER<ํ˜œํƒ ๋‚ด์—ญ>" + const val FREEBIE_EVENT_NAME = "์ฆ์ • ์ด๋ฒคํŠธ" + const val BENEFIT_RESULT = "%s: -%,d์›" + const val BENEFIT_AMOUNT_TITLE_MESSAGE = "$PRINT_ENTER<์ดํ˜œํƒ ๊ธˆ์•ก>" + const val PAYMENT_AMOUNT_TITLE_MESSAGE = "$PRINT_ENTER<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>" + const val BADGE_TITLE_MESSAGE = "$PRINT_ENTER<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>" +} \ No newline at end of file
Kotlin
์˜คํƒ€ ๋ณด๋‹ค๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ํŒŒ์ƒ๋  ์ˆ˜ ์žˆ๋Š” ์—๋Ÿฌ ๋ฉ”์„ธ์ง€๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์˜๋„์— ์ž‘์„ฑํ–ˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™์•„์š”. ๋‹ค์Œ ๋ฆฌ๋ทฐ์— cbj0010๋‹˜์ด ๊ด€๋ จํ•˜์—ฌ ์ œ์‹œํ•ด์ฃผ์‹  Enum Class๋ฅผ ํ†ตํ•ด ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์–ด์„œ park-yina๋‹˜๊ป˜๋„ ๊ณต์œ ๋“œ๋ฆฝ๋‹ˆ๋‹ค! ``` enum class ErrorMessage(private val message: String) { ERROR_MENU_INPUT("์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), ERROR_INPUT_DAY("์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); fun getMessage(): String = "[ERROR] $message" } ```
@@ -0,0 +1,58 @@ +package christmas.controller + +import christmas.model.Customer +import christmas.model.EventHelper +import christmas.model.OrderItems +import christmas.model.StoreMenu.Companion.sortOrderMenu +import christmas.model.StoreMenu.Companion.splitOrderMenu +import christmas.model.StoreMenu.Companion.splitOrderMenuForValidate +import christmas.utils.Validate.validateOrderMenu +import christmas.utils.Validate.validateVisitDate +import christmas.view.InputView +import christmas.view.OutputView + +class ChristmasController { + private val input = InputView() + private val output = OutputView() + + fun run() { + output.printStartPlanner() + val customer = Customer(inputVisitDate(), inputOrderMenu()) + + customer.requestApplyDecemberEvent() + previewBenefit(customer, customer.getEventHelper()) + } + + private fun inputVisitDate(): Int { + return try { + val visitDateInput = input.readVisitDate() + validateVisitDate(visitDateInput) + visitDateInput.toInt() + } catch (e: IllegalArgumentException) { + println(e.message) + inputVisitDate() + } + } + + private fun inputOrderMenu(): List<OrderItems> { + return try { + val orderMenuInput = input.readOrderMenu() + validateOrderMenu(splitOrderMenuForValidate(orderMenuInput)) + splitOrderMenu(orderMenuInput) + } catch (e: IllegalArgumentException) { + println(e.message) + inputOrderMenu() + } + } + + private fun previewBenefit(customer: Customer, eventHelper: EventHelper) { + output.printPreviewAnnounce(customer.getVisitDate()) + output.printOrderMenu(sortOrderMenu(customer.getOrderMenu())) + output.printTotalOrderPrize(customer.calculateTotalOrderSum()) + output.printFreebieMenu(eventHelper.getFreebie()) + output.printBenefitDetails(eventHelper.getDiscountHistory(), eventHelper.getFreebie()) + output.printBenefitAmount(eventHelper.calculateTotalBenefitAmount()) + output.printPaymentAmount(customer.calculateTotalOrderSum(), eventHelper.getDiscountHistory()) + output.printBadge(eventHelper.getBadge()) + } +} \ No newline at end of file
Kotlin
validateVisitDate(visitDateInput) ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ณผ์ •์—์„œ IllegalArgumentException์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ž…๋ ฅ์ด ์œ ํšจํ•œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์„ ํ•˜์ง€ ์•Š๊ณ  Int๋กœ๋งŒ ๋ณ€ํ™˜ํ•˜์—ฌ return ํ•ด๋„ ๋  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. validateVisitDate() ํ•จ์ˆ˜๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ์— ๊ด€ํ•œ ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ญํ• ๊ณผ ์ฑ…์ž„์— ๋Œ€ํ•ด์„œ๋„ ๊ณ ๋ฏผํ•˜๊ณ  ์ž‘์„ฑํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๊ธด ํ•˜์ง€๋งŒ ์ œ์‹œํ•ด์ฃผ์‹  ์˜๊ฒฌ๋Œ€๋กœ validateVisitDate()์— return type์„ ์ง€์ •ํ•˜์—ฌ์„œ return ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฒƒ๋„ ํ•˜๋‚˜์˜ ๋ฐฉ๋ฒ•์ด ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,79 @@ +package christmas.model + +import christmas.utils.Constant.BADGE_SANTA +import christmas.utils.Constant.BADGE_STAR +import christmas.utils.Constant.BADGE_TREE +import christmas.utils.Constant.CHAMPAGNE_CONDITION_AMOUNT +import christmas.utils.Constant.CHRISTMAS_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.DAILY_DISCOUNT_AMOUNT +import christmas.utils.Constant.FIRST_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.EMPTY +import christmas.utils.Constant.NONE +import christmas.utils.Constant.ONE_DAY_DECREASE +import christmas.utils.Constant.SANTA_MIN +import christmas.utils.Constant.STAR_MAX +import christmas.utils.Constant.STAR_MIN +import christmas.utils.Constant.TREE_MAX +import christmas.utils.Constant.TREE_MIN +import christmas.utils.Constant.WEEK_DISCOUNT_AMOUNT + +class DecemberEvent { + fun applyTheDayDiscount(date: Int): Int { + val dailyDiscount = (date - ONE_DAY_DECREASE) * DAILY_DISCOUNT_AMOUNT + + return FIRST_DAY_DISCOUNT_AMOUNT + dailyDiscount + } + + fun applyWeekdayDiscount(orderMenu: List<OrderItems>): Int { + var dessertMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseDessertMenuCount(item.getMenuName(), item.getQuantity()) + dessertMenuCount += increaseCount + } + + return dessertMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseDessertMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.CHOCO_CAKE.menuName || menuName == StoreMenu.ICE_CREAM.menuName) + return quantity + + return EMPTY + } + + + fun applyWeekendDiscount(orderMenu: List<OrderItems>): Int { + var mainMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseMainMenuCount(item.getMenuName(), item.getQuantity()) + mainMenuCount += increaseCount + } + + return mainMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseMainMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.T_BONE_STEAK.menuName || menuName == StoreMenu.BBQ_RIBS.menuName || + menuName == StoreMenu.SEAFOOD_PASTA.menuName || menuName == StoreMenu.CHRISTMAS_PASTA.menuName + ) + return quantity + + return EMPTY + } + + fun applySpecialDiscount(): Int = CHRISTMAS_DAY_DISCOUNT_AMOUNT + + fun presentChampagne(totalOrderAmount: Int): Boolean = totalOrderAmount >= CHAMPAGNE_CONDITION_AMOUNT + + fun assignBadge(benefitAmount: Int): String { + when { + benefitAmount in STAR_MIN..STAR_MAX -> return BADGE_STAR + benefitAmount in TREE_MIN..TREE_MAX -> return BADGE_TREE + benefitAmount >= SANTA_MIN -> return BADGE_SANTA + } + + return NONE + } +} \ No newline at end of file
Kotlin
๋งŒ์•ฝ์— ์ œ๊ฐ€ kotlin api๋ฅผ ํ™œ์šฉํ–‡ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด, ``` dessertMenuCount = orderMenu.filter { it.menuName == StoreMenu.CHOCO_CAKE.menuName || it.menuName == StoreMenu.ICE_CREAM.menuName }.sumOf { it.quantity } ``` ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฉ”์„œ๋“œ ํ•˜๋‚˜๋กœ ๋””์ €ํŠธ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ• ์ˆ˜๋„ ์žˆ์–ด์š”!!
@@ -0,0 +1,79 @@ +package christmas.model + +import christmas.utils.Constant.BADGE_SANTA +import christmas.utils.Constant.BADGE_STAR +import christmas.utils.Constant.BADGE_TREE +import christmas.utils.Constant.CHAMPAGNE_CONDITION_AMOUNT +import christmas.utils.Constant.CHRISTMAS_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.DAILY_DISCOUNT_AMOUNT +import christmas.utils.Constant.FIRST_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.EMPTY +import christmas.utils.Constant.NONE +import christmas.utils.Constant.ONE_DAY_DECREASE +import christmas.utils.Constant.SANTA_MIN +import christmas.utils.Constant.STAR_MAX +import christmas.utils.Constant.STAR_MIN +import christmas.utils.Constant.TREE_MAX +import christmas.utils.Constant.TREE_MIN +import christmas.utils.Constant.WEEK_DISCOUNT_AMOUNT + +class DecemberEvent { + fun applyTheDayDiscount(date: Int): Int { + val dailyDiscount = (date - ONE_DAY_DECREASE) * DAILY_DISCOUNT_AMOUNT + + return FIRST_DAY_DISCOUNT_AMOUNT + dailyDiscount + } + + fun applyWeekdayDiscount(orderMenu: List<OrderItems>): Int { + var dessertMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseDessertMenuCount(item.getMenuName(), item.getQuantity()) + dessertMenuCount += increaseCount + } + + return dessertMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseDessertMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.CHOCO_CAKE.menuName || menuName == StoreMenu.ICE_CREAM.menuName) + return quantity + + return EMPTY + } + + + fun applyWeekendDiscount(orderMenu: List<OrderItems>): Int { + var mainMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseMainMenuCount(item.getMenuName(), item.getQuantity()) + mainMenuCount += increaseCount + } + + return mainMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseMainMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.T_BONE_STEAK.menuName || menuName == StoreMenu.BBQ_RIBS.menuName || + menuName == StoreMenu.SEAFOOD_PASTA.menuName || menuName == StoreMenu.CHRISTMAS_PASTA.menuName + ) + return quantity + + return EMPTY + } + + fun applySpecialDiscount(): Int = CHRISTMAS_DAY_DISCOUNT_AMOUNT + + fun presentChampagne(totalOrderAmount: Int): Boolean = totalOrderAmount >= CHAMPAGNE_CONDITION_AMOUNT + + fun assignBadge(benefitAmount: Int): String { + when { + benefitAmount in STAR_MIN..STAR_MAX -> return BADGE_STAR + benefitAmount in TREE_MIN..TREE_MAX -> return BADGE_TREE + benefitAmount >= SANTA_MIN -> return BADGE_SANTA + } + + return NONE + } +} \ No newline at end of file
Kotlin
์ด๋ ‡๊ฒŒ ์กฐ๊ฑด์ด ๋งŽ์„ ๊ฒฝ์šฐ์—๋Š”, ``` val mainMenus = listOf(StoreMenu.T_BONE_STEAK.menuName....) if (mainMenus.contains(menu)) { return quantity } ``` ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ข€๋” ๊ฐ„๋‹จํžˆ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์–ด์š”!!
@@ -0,0 +1,79 @@ +package christmas.model + +import christmas.utils.Constant.BADGE_SANTA +import christmas.utils.Constant.BADGE_STAR +import christmas.utils.Constant.BADGE_TREE +import christmas.utils.Constant.CHAMPAGNE_CONDITION_AMOUNT +import christmas.utils.Constant.CHRISTMAS_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.DAILY_DISCOUNT_AMOUNT +import christmas.utils.Constant.FIRST_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.EMPTY +import christmas.utils.Constant.NONE +import christmas.utils.Constant.ONE_DAY_DECREASE +import christmas.utils.Constant.SANTA_MIN +import christmas.utils.Constant.STAR_MAX +import christmas.utils.Constant.STAR_MIN +import christmas.utils.Constant.TREE_MAX +import christmas.utils.Constant.TREE_MIN +import christmas.utils.Constant.WEEK_DISCOUNT_AMOUNT + +class DecemberEvent { + fun applyTheDayDiscount(date: Int): Int { + val dailyDiscount = (date - ONE_DAY_DECREASE) * DAILY_DISCOUNT_AMOUNT + + return FIRST_DAY_DISCOUNT_AMOUNT + dailyDiscount + } + + fun applyWeekdayDiscount(orderMenu: List<OrderItems>): Int { + var dessertMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseDessertMenuCount(item.getMenuName(), item.getQuantity()) + dessertMenuCount += increaseCount + } + + return dessertMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseDessertMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.CHOCO_CAKE.menuName || menuName == StoreMenu.ICE_CREAM.menuName) + return quantity + + return EMPTY + } + + + fun applyWeekendDiscount(orderMenu: List<OrderItems>): Int { + var mainMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseMainMenuCount(item.getMenuName(), item.getQuantity()) + mainMenuCount += increaseCount + } + + return mainMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseMainMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.T_BONE_STEAK.menuName || menuName == StoreMenu.BBQ_RIBS.menuName || + menuName == StoreMenu.SEAFOOD_PASTA.menuName || menuName == StoreMenu.CHRISTMAS_PASTA.menuName + ) + return quantity + + return EMPTY + } + + fun applySpecialDiscount(): Int = CHRISTMAS_DAY_DISCOUNT_AMOUNT + + fun presentChampagne(totalOrderAmount: Int): Boolean = totalOrderAmount >= CHAMPAGNE_CONDITION_AMOUNT + + fun assignBadge(benefitAmount: Int): String { + when { + benefitAmount in STAR_MIN..STAR_MAX -> return BADGE_STAR + benefitAmount in TREE_MIN..TREE_MAX -> return BADGE_TREE + benefitAmount >= SANTA_MIN -> return BADGE_SANTA + } + + return NONE + } +} \ No newline at end of file
Kotlin
์•„๋งˆ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜์‹  ์ด์œ ๊ฐ€, ๊ทธ๋ƒฅ >= ๊ธฐํ˜ธ๋งŒ ์‚ฌ์šฉํ•˜๋ฉด STAR ๋ฐฐ์ง€๋งŒ return ๋˜์–ด์„œ ๊ทธ๋Ÿฐ๊ฑฐ ๊ฐ™์€๋ฐ, ``` when { benefitAmount >= 20000 -> retrun BADGE_SANTA benefitAmount >= 10000 -> retrun BADGE_TREE benefitAmount >= 5000 -> retrun BADGE_STAR } ``` ์ด๋ ‡๊ฒŒ ๊ฑฐ๊พธ๋กœ ์ž‘์„ฑํ•˜๋ฉด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,79 @@ +package christmas.model + +import christmas.utils.Constant.BADGE_SANTA +import christmas.utils.Constant.BADGE_STAR +import christmas.utils.Constant.BADGE_TREE +import christmas.utils.Constant.CHAMPAGNE_CONDITION_AMOUNT +import christmas.utils.Constant.CHRISTMAS_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.DAILY_DISCOUNT_AMOUNT +import christmas.utils.Constant.FIRST_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.EMPTY +import christmas.utils.Constant.NONE +import christmas.utils.Constant.ONE_DAY_DECREASE +import christmas.utils.Constant.SANTA_MIN +import christmas.utils.Constant.STAR_MAX +import christmas.utils.Constant.STAR_MIN +import christmas.utils.Constant.TREE_MAX +import christmas.utils.Constant.TREE_MIN +import christmas.utils.Constant.WEEK_DISCOUNT_AMOUNT + +class DecemberEvent { + fun applyTheDayDiscount(date: Int): Int { + val dailyDiscount = (date - ONE_DAY_DECREASE) * DAILY_DISCOUNT_AMOUNT + + return FIRST_DAY_DISCOUNT_AMOUNT + dailyDiscount + } + + fun applyWeekdayDiscount(orderMenu: List<OrderItems>): Int { + var dessertMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseDessertMenuCount(item.getMenuName(), item.getQuantity()) + dessertMenuCount += increaseCount + } + + return dessertMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseDessertMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.CHOCO_CAKE.menuName || menuName == StoreMenu.ICE_CREAM.menuName) + return quantity + + return EMPTY + } + + + fun applyWeekendDiscount(orderMenu: List<OrderItems>): Int { + var mainMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseMainMenuCount(item.getMenuName(), item.getQuantity()) + mainMenuCount += increaseCount + } + + return mainMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseMainMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.T_BONE_STEAK.menuName || menuName == StoreMenu.BBQ_RIBS.menuName || + menuName == StoreMenu.SEAFOOD_PASTA.menuName || menuName == StoreMenu.CHRISTMAS_PASTA.menuName + ) + return quantity + + return EMPTY + } + + fun applySpecialDiscount(): Int = CHRISTMAS_DAY_DISCOUNT_AMOUNT + + fun presentChampagne(totalOrderAmount: Int): Boolean = totalOrderAmount >= CHAMPAGNE_CONDITION_AMOUNT + + fun assignBadge(benefitAmount: Int): String { + when { + benefitAmount in STAR_MIN..STAR_MAX -> return BADGE_STAR + benefitAmount in TREE_MIN..TREE_MAX -> return BADGE_TREE + benefitAmount >= SANTA_MIN -> return BADGE_SANTA + } + + return NONE + } +} \ No newline at end of file
Kotlin
๋ฏธ์…˜์˜ ์š”๊ตฌ ์‚ฌํ•ญ์— 'else๋ฅผ ์ง€์–‘ํ•œ๋‹ค.'๋Š” ์‚ฌํ•ญ์— ๋Œ€ํ•ด์„œ ๊ฐ€๋…์„ฑ์€ ์œ ์ง€ํ•จ๊ณผ ๋™์‹œ์— else๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•˜์—ฌ ๊ณ ๋ฏผํ•˜๊ณ  ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ œ์‹œํ•ด์ฃผ์‹  return when๋„ ๊ตฌํ˜„ ๊ณผ์ •์—์„œ ๊ณ ๋ คํ–ˆ์—ˆ์ง€๋งŒ ์ตœ์ข…์ ์œผ๋กœ๋Š” when์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ๋˜ ๊ฒƒ ๊ฐ™์•„์š”. else๋ฅผ ์ง€์–‘ํ•˜๋ฉด์„œ ๋” ๋‚˜์€ ๊ฐ€๋…์„ฑ์„ ๊ฐ–์ถœ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ๋” ๊ณ ๋ฏผํ•ด๋ด์•ผ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,59 @@ +package christmas.utils + +object Constant { + /* Model */ + const val FIRST_DAY_DISCOUNT_AMOUNT = 1_000 + const val DAILY_DISCOUNT_AMOUNT = 100 + const val ONE_DAY_DECREASE = 1 + const val WEEK_DISCOUNT_AMOUNT = 2_023 + const val CHRISTMAS_DAY_DISCOUNT_AMOUNT = 1_000 + const val CHAMPAGNE_CONDITION_AMOUNT = 120_000 + const val CHRISTMAS_DAY = 25 + const val DISCOUNT_THRESHOLD = 10_000 + const val EMPTY = 0 + const val BADGE_STAR = "๋ณ„" + const val BADGE_TREE = "ํŠธ๋ฆฌ" + const val BADGE_SANTA = "์‚ฐํƒ€" + const val NONE = "์—†์Œ" + const val STAR_MIN = 5_000 + const val STAR_MAX = 9_999 + const val TREE_MIN = 10_000 + const val TREE_MAX = 19_999 + const val SANTA_MIN = 20_000 + const val FIRST_INDEX = 0 + const val SECOND_INDEX = 1 + + /* Validate */ + const val ORDER_ITEMS_SIZE = 2 + const val VISIT_DATE_MIN = 1 + const val VISIT_DATE_MAX = 31 + const val QUANTITY_MINIMUM = 1 + const val QUANTITY_MAXIMUM = 20 + const val HYPHEN = "-" + const val COMMA = "," + const val NUMERIC_REGEX = "^\\d+$" + private const val ERROR_MESSAGE_PREFIX = "[ERROR] " + const val ERROR_MESSAGE_INPUT_VISIT_DATE = ERROR_MESSAGE_PREFIX + "์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + const val ERROR_MESSAGE_INPUT_MENU = ERROR_MESSAGE_PREFIX + "์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + + /* InputView */ + const val VISIT_DATE_ANNOUNCE_MESSAGE = "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)" + const val MENU_ANNOUNCE_MESSAGE = "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)" + + /* OutPutView */ + private const val PRINT_ENTER = "\n" + const val START_PLANNER_MESSAGE = "์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค." + const val PREVIEW_ANNOUNCE_MESSAGE = "12์›” %d์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!" + const val ORDER_MENU_TITLE_MESSAGE = "$PRINT_ENTER<์ฃผ๋ฌธ ๋ฉ”๋‰ด>" + const val ITEMS_RESULT = "%s %d๊ฐœ" + const val TOTAL_ORDER_SUM_TITLE_MESSAGE = "$PRINT_ENTER<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>" + const val TOTAL_AMOUNT_RESULT = "%,d์›" + const val PRESENT_MENU_TITLE_MESSAGE = "$PRINT_ENTER<์ฆ์ • ๋ฉ”๋‰ด>" + const val PRESENT_AMOUNT = 1 + const val BENEFIT_DETAILS_TITLE_MESSAGE = "$PRINT_ENTER<ํ˜œํƒ ๋‚ด์—ญ>" + const val FREEBIE_EVENT_NAME = "์ฆ์ • ์ด๋ฒคํŠธ" + const val BENEFIT_RESULT = "%s: -%,d์›" + const val BENEFIT_AMOUNT_TITLE_MESSAGE = "$PRINT_ENTER<์ดํ˜œํƒ ๊ธˆ์•ก>" + const val PAYMENT_AMOUNT_TITLE_MESSAGE = "$PRINT_ENTER<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>" + const val BADGE_TITLE_MESSAGE = "$PRINT_ENTER<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>" +} \ No newline at end of file
Kotlin
์ž‘์„ฑํ•ด์ฃผ์‹  ์ฝ”๋“œ๋„ ๋งค์šฐ ๊น”๋”ํ•˜๊ณ  ๊ฐ€๋…์„ฑ์ด ๋†’์•„ ๋ณด์—ฌ์š”! ์—๋Ÿฌ ๋ฉ”์„ธ์ง€๋“ค์ด ์—ฐ๊ด€์„ฑ์ด ์žˆ๋Š” ์ƒ์ˆ˜๋“ค์ด์ง€๋งŒ Enum Class๋„ ํ•˜๋‚˜์˜ ๋ชจ๋ธ๋กœ ๊ตฌ์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ ๋ณด๋‹ค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์ˆ˜ํ™” ์ฒ˜๋ฆฌ๋งŒ ํ•˜์˜€๋˜ ๊ฒƒ์ธ๋ฐ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€๋“ค์„ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์•„์ง ๋งŽ์ด ๋ถ€์กฑํ•œ ๊ฒƒ์€ ์‚ฌ์‹ค์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ตฌ์„ฑ๊ณผ ์„ค๊ณ„, ๊ฐ€๋…์„ฑ์„ ๋†’์ด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋” ํ•™์Šตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,43 @@ +package christmas.model + +import christmas.utils.Constant.CHRISTMAS_DAY +import christmas.utils.Constant.EMPTY + +enum class DiscountType(val eventName: String) { + THE_DAY_DISCOUNT("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"), + WEEKDAY_DISCOUNT("ํ‰์ผ ํ• ์ธ"), + WEEKEND_DISCOUNT("์ฃผ๋ง ํ• ์ธ"), + SPECIAL_DISCOUNT("ํŠน๋ณ„ ํ• ์ธ"); + + companion object { + private val decemberEvent = DecemberEvent() + + fun requestTheDayDiscount(visitDate: Int): Int { + if (visitDate <= CHRISTMAS_DAY) + return decemberEvent.applyTheDayDiscount(visitDate) + + return EMPTY + } + + fun requestWeekdayDiscount(visitDate: Int, orderMenu: List<OrderItems>): Int { + if (visitDate in DecemberCalender.WEEKDAY.dates) + return decemberEvent.applyWeekdayDiscount(orderMenu) + + return EMPTY + } + + fun requestWeekendDiscount(visitDate: Int, orderMenu: List<OrderItems>): Int { + if (visitDate in DecemberCalender.WEEKEND.dates) + return decemberEvent.applyWeekendDiscount(orderMenu) + + return EMPTY + } + + fun requestSpecialDiscount(visitDate: Int): Int { + if (visitDate in DecemberCalender.SPECIAL_DAY.dates) + return decemberEvent.applySpecialDiscount() + + return EMPTY + } + } +} \ No newline at end of file
Kotlin
๋‹ต๋ณ€์„ ๋“œ๋ฆฌ๊ณ ์ž ๋‚ด์šฉ์„ ์ž‘์„ฑํ•˜์˜€๋Š”๋ฐ..์ฝ”๋“œ ์ž์ฒด์ ์œผ๋กœ๋„ ๋‹จ์ผ ์ฑ…์ž„ ์›์น™์— ๊ด€ํ•ด ๋” ๊ณ ๋ คํ•  ๋ถ€๋ถ„์ด ์žˆ์–ด ๋ณด์ด๊ณ  ํ˜น์‹œ๋‚˜ ์ œ ๋‹ต๋ณ€์ด ์‚ฌ์‹ค๊ณผ ๋‹ค๋ฅธ ๋‚ด์šฉ์ด ํฌํ•จ๋œ๋‹ค๋ฉด ํ•™์Šต์— ํ˜ผ๋ž€์„ ๋“œ๋ฆด ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„ ์ž˜ ์„ค๋ช…๋œ ๋งํฌ๋ฅผ ์ฒจ๋ถ€ํ•ฉ๋‹ˆ๋‹ค! ํฐ ๋„์›€์„ ๋“œ๋ฆฌ์ง€ ๋ชปํ•ด ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค.. ๋งํฌ 1. https://kotlinlang.org/docs/object-declarations.html#companion-objects ๋งํฌ 2. https://www.baeldung.com/kotlin/enum-static-method
@@ -0,0 +1,59 @@ +package christmas.utils + +object Constant { + /* Model */ + const val FIRST_DAY_DISCOUNT_AMOUNT = 1_000 + const val DAILY_DISCOUNT_AMOUNT = 100 + const val ONE_DAY_DECREASE = 1 + const val WEEK_DISCOUNT_AMOUNT = 2_023 + const val CHRISTMAS_DAY_DISCOUNT_AMOUNT = 1_000 + const val CHAMPAGNE_CONDITION_AMOUNT = 120_000 + const val CHRISTMAS_DAY = 25 + const val DISCOUNT_THRESHOLD = 10_000 + const val EMPTY = 0 + const val BADGE_STAR = "๋ณ„" + const val BADGE_TREE = "ํŠธ๋ฆฌ" + const val BADGE_SANTA = "์‚ฐํƒ€" + const val NONE = "์—†์Œ" + const val STAR_MIN = 5_000 + const val STAR_MAX = 9_999 + const val TREE_MIN = 10_000 + const val TREE_MAX = 19_999 + const val SANTA_MIN = 20_000 + const val FIRST_INDEX = 0 + const val SECOND_INDEX = 1 + + /* Validate */ + const val ORDER_ITEMS_SIZE = 2 + const val VISIT_DATE_MIN = 1 + const val VISIT_DATE_MAX = 31 + const val QUANTITY_MINIMUM = 1 + const val QUANTITY_MAXIMUM = 20 + const val HYPHEN = "-" + const val COMMA = "," + const val NUMERIC_REGEX = "^\\d+$" + private const val ERROR_MESSAGE_PREFIX = "[ERROR] " + const val ERROR_MESSAGE_INPUT_VISIT_DATE = ERROR_MESSAGE_PREFIX + "์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + const val ERROR_MESSAGE_INPUT_MENU = ERROR_MESSAGE_PREFIX + "์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + + /* InputView */ + const val VISIT_DATE_ANNOUNCE_MESSAGE = "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)" + const val MENU_ANNOUNCE_MESSAGE = "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)" + + /* OutPutView */ + private const val PRINT_ENTER = "\n" + const val START_PLANNER_MESSAGE = "์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค." + const val PREVIEW_ANNOUNCE_MESSAGE = "12์›” %d์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!" + const val ORDER_MENU_TITLE_MESSAGE = "$PRINT_ENTER<์ฃผ๋ฌธ ๋ฉ”๋‰ด>" + const val ITEMS_RESULT = "%s %d๊ฐœ" + const val TOTAL_ORDER_SUM_TITLE_MESSAGE = "$PRINT_ENTER<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>" + const val TOTAL_AMOUNT_RESULT = "%,d์›" + const val PRESENT_MENU_TITLE_MESSAGE = "$PRINT_ENTER<์ฆ์ • ๋ฉ”๋‰ด>" + const val PRESENT_AMOUNT = 1 + const val BENEFIT_DETAILS_TITLE_MESSAGE = "$PRINT_ENTER<ํ˜œํƒ ๋‚ด์—ญ>" + const val FREEBIE_EVENT_NAME = "์ฆ์ • ์ด๋ฒคํŠธ" + const val BENEFIT_RESULT = "%s: -%,d์›" + const val BENEFIT_AMOUNT_TITLE_MESSAGE = "$PRINT_ENTER<์ดํ˜œํƒ ๊ธˆ์•ก>" + const val PAYMENT_AMOUNT_TITLE_MESSAGE = "$PRINT_ENTER<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>" + const val BADGE_TITLE_MESSAGE = "$PRINT_ENTER<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>" +} \ No newline at end of file
Kotlin
์™€ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ์ •๋ง ๊น”๋”ํ•ด์งˆ ๊ฒƒ ๊ฐ™์•„์š”! ์ €ํ•œํ…Œ ๊ผญ ํ•„์š”ํ•œ ํ•™์Šต ๋ถ€๋ถ„์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋ถ€ํ„ฐ ์ ์šฉํ•ด ํ™•์‹คํžˆ ๊ฐœ์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ๋ฆฌ๋ทฐ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค :)
@@ -0,0 +1,10 @@ +package christmas.model + +data class OrderItems( + private val menuName: String, + private val quantity: Int +) { + fun getMenuName(): String = menuName + + fun getQuantity(): Int = quantity +}
Kotlin
3์ฃผ์ฐจ ๊ณตํ†ต ํ”ผ๋“œ๋ฐฑ์—๋„ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋‚ด์šฉ์ธ๋ฐ val์€ ๊ฐ’์„ ๋ถˆ๋ณ€์œผ๋กœ ์ œํ•œํ•˜๊ณ ์ž ํ•˜๋Š” ์˜๋„์ด๊ณ  private์€ ๊ฐ์ฒด์˜ ์ƒํƒœ ์ ‘๊ทผ์„ ์ œํ•œํ•˜๋Š” ์˜๋„๋ผ๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค! ๊ฐ’์˜ ์ˆ˜์ •์„ ๋ง‰๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•˜์ง€๋งŒ private ์ ‘๊ทผ ์ œํ•œ์„ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ํ™•์‹คํ•˜๊ฒŒ ๋ณดํ˜ธํ•˜๊ณ ์ž ํ•˜๋Š” ์˜๋„์— ์ž‘์„ฑํ•˜๊ฒŒ ์ฝ”๋“œ์ธ ๊ฒƒ ๊ฐ™์•„์š”. ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,82 @@ +package christmas.view + +import christmas.model.DiscountType +import christmas.model.OrderItems +import christmas.model.StoreMenu +import christmas.utils.Constant.BADGE_TITLE_MESSAGE +import christmas.utils.Constant.BENEFIT_AMOUNT_TITLE_MESSAGE +import christmas.utils.Constant.BENEFIT_DETAILS_TITLE_MESSAGE +import christmas.utils.Constant.BENEFIT_RESULT +import christmas.utils.Constant.FREEBIE_EVENT_NAME +import christmas.utils.Constant.EMPTY +import christmas.utils.Constant.ITEMS_RESULT +import christmas.utils.Constant.NONE +import christmas.utils.Constant.ORDER_MENU_TITLE_MESSAGE +import christmas.utils.Constant.PAYMENT_AMOUNT_TITLE_MESSAGE +import christmas.utils.Constant.PRESENT_AMOUNT +import christmas.utils.Constant.PRESENT_MENU_TITLE_MESSAGE +import christmas.utils.Constant.PREVIEW_ANNOUNCE_MESSAGE +import christmas.utils.Constant.START_PLANNER_MESSAGE +import christmas.utils.Constant.TOTAL_AMOUNT_RESULT +import christmas.utils.Constant.TOTAL_ORDER_SUM_TITLE_MESSAGE + +class OutputView { + fun printStartPlanner() { + println(START_PLANNER_MESSAGE) + } + + fun printPreviewAnnounce(visitDate: Int) { + println(PREVIEW_ANNOUNCE_MESSAGE.format(visitDate)) + } + + fun printOrderMenu(orderMenu: List<OrderItems>) { + println(ORDER_MENU_TITLE_MESSAGE) + orderMenu.forEach { items -> + println(ITEMS_RESULT.format(items.getMenuName(), items.getQuantity())) + } + } + + fun printTotalOrderPrize(totalOrderSum: Int) { + println(TOTAL_ORDER_SUM_TITLE_MESSAGE) + println(TOTAL_AMOUNT_RESULT.format(totalOrderSum)) + + } + + fun printFreebieMenu(freebie: Boolean) { + println(PRESENT_MENU_TITLE_MESSAGE) + when { + freebie -> println(ITEMS_RESULT.format(StoreMenu.CHAMPAGNE.menuName, PRESENT_AMOUNT)) + !freebie -> println(NONE) + } + } + + fun printBenefitDetails(discountHistory: List<Pair<DiscountType, Int>>, freebie: Boolean) { + val discountHistoryExist = discountHistory.filter { it.second != EMPTY } + + println(BENEFIT_DETAILS_TITLE_MESSAGE) + discountHistoryExist.forEach { (discountType, discountAmount) -> + println(BENEFIT_RESULT.format(discountType.eventName, discountAmount)) + } + + if(freebie) + println(BENEFIT_RESULT.format(FREEBIE_EVENT_NAME, StoreMenu.CHAMPAGNE.menuPrice)) + + if(discountHistoryExist.isEmpty() && !freebie) + println(NONE) + } + + fun printBenefitAmount(totalBenefitAmount: Int) { + println(BENEFIT_AMOUNT_TITLE_MESSAGE) + println(TOTAL_AMOUNT_RESULT.format(EMPTY - totalBenefitAmount)) + } + + fun printPaymentAmount(totalOrderSum: Int, discountHistory: List<Pair<DiscountType, Int>>) { + println(PAYMENT_AMOUNT_TITLE_MESSAGE) + println(TOTAL_AMOUNT_RESULT.format(totalOrderSum - discountHistory.sumOf { it.second })) + } + + fun printBadge(badge: String) { + println(BADGE_TITLE_MESSAGE) + println(badge) + } +} \ No newline at end of file
Kotlin
else๋ฅผ ์ง€์–‘ํ•˜๋Š” ๊ฒƒ์ด ๊ตฌํ˜„ ๊ณผ์ •์—์„œ ์—ฌ๋Ÿฌ๋ฒˆ ๊ณ ๋ฏผ์— ๋น ์ง€๊ฒŒ ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์•„์š”.. ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ  ์ปจ๋ฒค์…˜์„ ์ง€ํ‚ฌ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๋” ์ฃผ์ฒด์ ์œผ๋กœ ํŒ๋‹จํ•˜๊ณ  ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๋„ ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉํ–ฅ์ด๋ผ๊ณ  ๊ณต๊ฐ์ด ๊ฐ€๋Š” ๊ฒƒ ๊ฐ™์•„์š”! ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,79 @@ +package christmas.model + +import christmas.utils.Constant.BADGE_SANTA +import christmas.utils.Constant.BADGE_STAR +import christmas.utils.Constant.BADGE_TREE +import christmas.utils.Constant.CHAMPAGNE_CONDITION_AMOUNT +import christmas.utils.Constant.CHRISTMAS_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.DAILY_DISCOUNT_AMOUNT +import christmas.utils.Constant.FIRST_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.EMPTY +import christmas.utils.Constant.NONE +import christmas.utils.Constant.ONE_DAY_DECREASE +import christmas.utils.Constant.SANTA_MIN +import christmas.utils.Constant.STAR_MAX +import christmas.utils.Constant.STAR_MIN +import christmas.utils.Constant.TREE_MAX +import christmas.utils.Constant.TREE_MIN +import christmas.utils.Constant.WEEK_DISCOUNT_AMOUNT + +class DecemberEvent { + fun applyTheDayDiscount(date: Int): Int { + val dailyDiscount = (date - ONE_DAY_DECREASE) * DAILY_DISCOUNT_AMOUNT + + return FIRST_DAY_DISCOUNT_AMOUNT + dailyDiscount + } + + fun applyWeekdayDiscount(orderMenu: List<OrderItems>): Int { + var dessertMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseDessertMenuCount(item.getMenuName(), item.getQuantity()) + dessertMenuCount += increaseCount + } + + return dessertMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseDessertMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.CHOCO_CAKE.menuName || menuName == StoreMenu.ICE_CREAM.menuName) + return quantity + + return EMPTY + } + + + fun applyWeekendDiscount(orderMenu: List<OrderItems>): Int { + var mainMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseMainMenuCount(item.getMenuName(), item.getQuantity()) + mainMenuCount += increaseCount + } + + return mainMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseMainMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.T_BONE_STEAK.menuName || menuName == StoreMenu.BBQ_RIBS.menuName || + menuName == StoreMenu.SEAFOOD_PASTA.menuName || menuName == StoreMenu.CHRISTMAS_PASTA.menuName + ) + return quantity + + return EMPTY + } + + fun applySpecialDiscount(): Int = CHRISTMAS_DAY_DISCOUNT_AMOUNT + + fun presentChampagne(totalOrderAmount: Int): Boolean = totalOrderAmount >= CHAMPAGNE_CONDITION_AMOUNT + + fun assignBadge(benefitAmount: Int): String { + when { + benefitAmount in STAR_MIN..STAR_MAX -> return BADGE_STAR + benefitAmount in TREE_MIN..TREE_MAX -> return BADGE_TREE + benefitAmount >= SANTA_MIN -> return BADGE_SANTA + } + + return NONE + } +} \ No newline at end of file
Kotlin
์ œ์‹œํ•ด์ฃผ์‹  ์ฝ”๋“œ๊ฐ€ ํ™•์‹คํžˆ ๊ฐ„๊ฒฐํ•˜๊ณ  ์ฝ”ํ‹€๋ฆฐ์Šค๋Ÿฌ์›Œ ๋ณด์ด๋„ค์š”! ์•ž์œผ๋กœ์˜ ๊ณผ์ •์—์„œ๋Š” ์ฝ”ํ‹€๋ฆฐ์— ๋Œ€ํ•œ ํ•™์Šต ์‹œ๊ฐ„์„ ์ผ์ • ์‹œ๊ฐ„ ์ง€์ •ํ•ด์„œ ์Šค์Šค๋กœ ์ˆ™๋ จ๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ฌ ํ•„์š”๊ฐ€ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”. ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,79 @@ +package christmas.model + +import christmas.utils.Constant.BADGE_SANTA +import christmas.utils.Constant.BADGE_STAR +import christmas.utils.Constant.BADGE_TREE +import christmas.utils.Constant.CHAMPAGNE_CONDITION_AMOUNT +import christmas.utils.Constant.CHRISTMAS_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.DAILY_DISCOUNT_AMOUNT +import christmas.utils.Constant.FIRST_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.EMPTY +import christmas.utils.Constant.NONE +import christmas.utils.Constant.ONE_DAY_DECREASE +import christmas.utils.Constant.SANTA_MIN +import christmas.utils.Constant.STAR_MAX +import christmas.utils.Constant.STAR_MIN +import christmas.utils.Constant.TREE_MAX +import christmas.utils.Constant.TREE_MIN +import christmas.utils.Constant.WEEK_DISCOUNT_AMOUNT + +class DecemberEvent { + fun applyTheDayDiscount(date: Int): Int { + val dailyDiscount = (date - ONE_DAY_DECREASE) * DAILY_DISCOUNT_AMOUNT + + return FIRST_DAY_DISCOUNT_AMOUNT + dailyDiscount + } + + fun applyWeekdayDiscount(orderMenu: List<OrderItems>): Int { + var dessertMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseDessertMenuCount(item.getMenuName(), item.getQuantity()) + dessertMenuCount += increaseCount + } + + return dessertMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseDessertMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.CHOCO_CAKE.menuName || menuName == StoreMenu.ICE_CREAM.menuName) + return quantity + + return EMPTY + } + + + fun applyWeekendDiscount(orderMenu: List<OrderItems>): Int { + var mainMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseMainMenuCount(item.getMenuName(), item.getQuantity()) + mainMenuCount += increaseCount + } + + return mainMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseMainMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.T_BONE_STEAK.menuName || menuName == StoreMenu.BBQ_RIBS.menuName || + menuName == StoreMenu.SEAFOOD_PASTA.menuName || menuName == StoreMenu.CHRISTMAS_PASTA.menuName + ) + return quantity + + return EMPTY + } + + fun applySpecialDiscount(): Int = CHRISTMAS_DAY_DISCOUNT_AMOUNT + + fun presentChampagne(totalOrderAmount: Int): Boolean = totalOrderAmount >= CHAMPAGNE_CONDITION_AMOUNT + + fun assignBadge(benefitAmount: Int): String { + when { + benefitAmount in STAR_MIN..STAR_MAX -> return BADGE_STAR + benefitAmount in TREE_MIN..TREE_MAX -> return BADGE_TREE + benefitAmount >= SANTA_MIN -> return BADGE_SANTA + } + + return NONE + } +} \ No newline at end of file
Kotlin
์ œ์‹œํ•ด์ฃผ์‹  ์ฝ”๋“œ๊ฐ€ ํ™•์‹คํžˆ ๊ฐœ์„ ๋œ ์ฝ”๋“œ์ธ ๊ฑฐ ๊ฐ™์•„์š”. ์‚ฌ์‹ค ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ ๋„ ๋ฐ˜๋ณต๋˜๋Š” ๋ฆฌํŒฉํ† ๋ง ์†์—์„œ ํ•ด๋‹น ๋ถ€๋ถ„์„ ๋†“์ณฃ๋‹ค๋Š”๊ฒŒ ์•„์‰ฌ์›€์„ ๋งŽ์ด ๋‚จ๊ฒผ์Šต๋‹ˆ๋‹ค.. ๊น”๋”ํ•œ ์ฝ”๋“œ์™€ ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,79 @@ +package christmas.model + +import christmas.utils.Constant.BADGE_SANTA +import christmas.utils.Constant.BADGE_STAR +import christmas.utils.Constant.BADGE_TREE +import christmas.utils.Constant.CHAMPAGNE_CONDITION_AMOUNT +import christmas.utils.Constant.CHRISTMAS_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.DAILY_DISCOUNT_AMOUNT +import christmas.utils.Constant.FIRST_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.EMPTY +import christmas.utils.Constant.NONE +import christmas.utils.Constant.ONE_DAY_DECREASE +import christmas.utils.Constant.SANTA_MIN +import christmas.utils.Constant.STAR_MAX +import christmas.utils.Constant.STAR_MIN +import christmas.utils.Constant.TREE_MAX +import christmas.utils.Constant.TREE_MIN +import christmas.utils.Constant.WEEK_DISCOUNT_AMOUNT + +class DecemberEvent { + fun applyTheDayDiscount(date: Int): Int { + val dailyDiscount = (date - ONE_DAY_DECREASE) * DAILY_DISCOUNT_AMOUNT + + return FIRST_DAY_DISCOUNT_AMOUNT + dailyDiscount + } + + fun applyWeekdayDiscount(orderMenu: List<OrderItems>): Int { + var dessertMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseDessertMenuCount(item.getMenuName(), item.getQuantity()) + dessertMenuCount += increaseCount + } + + return dessertMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseDessertMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.CHOCO_CAKE.menuName || menuName == StoreMenu.ICE_CREAM.menuName) + return quantity + + return EMPTY + } + + + fun applyWeekendDiscount(orderMenu: List<OrderItems>): Int { + var mainMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseMainMenuCount(item.getMenuName(), item.getQuantity()) + mainMenuCount += increaseCount + } + + return mainMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseMainMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.T_BONE_STEAK.menuName || menuName == StoreMenu.BBQ_RIBS.menuName || + menuName == StoreMenu.SEAFOOD_PASTA.menuName || menuName == StoreMenu.CHRISTMAS_PASTA.menuName + ) + return quantity + + return EMPTY + } + + fun applySpecialDiscount(): Int = CHRISTMAS_DAY_DISCOUNT_AMOUNT + + fun presentChampagne(totalOrderAmount: Int): Boolean = totalOrderAmount >= CHAMPAGNE_CONDITION_AMOUNT + + fun assignBadge(benefitAmount: Int): String { + when { + benefitAmount in STAR_MIN..STAR_MAX -> return BADGE_STAR + benefitAmount in TREE_MIN..TREE_MAX -> return BADGE_TREE + benefitAmount >= SANTA_MIN -> return BADGE_SANTA + } + + return NONE + } +} \ No newline at end of file
Kotlin
์•„ ๋™์ž‘ ์ˆœ์„œ์— ๋”ฐ๋ผ์„œ ๋˜ ๋‹ค๋ฅด๊ฒŒ ์ž‘์„ฑ๋  ์ˆ˜ ์žˆ๋„ค์š”. ์ œ์‹œ ํ•ด์ฃผ์‹  ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋‚ฎ์€ ๊ธˆ์•ก๋ณด๋‹ค๋Š” ๋†’์€ ๊ธˆ์•ก๋ถ€ํ„ฐ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ์ด ์ฒ˜๋ฆฌ ๊ณผ์ •์„ ํ™•์‹คํžˆ ์ค„์ผ ์ˆ˜ ์žˆ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด์š”! ๋งŽ์€ ๋ถ€๋ถ„์—์„œ ๋„์›€์„ ๋ฐ›๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™์•„์š”.. ์ •๋ง ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,56 @@ +package christmas + +import christmas.model.DecemberEvent +import christmas.model.OrderItems +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class DecemberEventTest { + private val decemberEvent: DecemberEvent = DecemberEvent() + + @Test + fun `๋””๋ฐ์ด ํ• ์ธ ๊ธˆ์•ก ํ…Œ์ŠคํŠธ`() { + val discountAmount = decemberEvent.applyTheDayDiscount(25) + + Assertions.assertTrue(discountAmount == 3400) + } + + @Test + fun `ํ‰์ผ ํ• ์ธ ๊ธˆ์•ก ํ…Œ์ŠคํŠธ`() { + val discountAmount = decemberEvent.applyWeekdayDiscount(listOf(OrderItems("์ดˆ์ฝ”์ผ€์ดํฌ", 2), OrderItems("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 3))) + + Assertions.assertTrue(discountAmount == 4046) + } + + @Test + fun `์ฃผ๋ง ํ• ์ธ ๊ธˆ์•ก ํ…Œ์ŠคํŠธ`() { + val discountAmount = decemberEvent.applyWeekendDiscount(listOf(OrderItems("์ดˆ์ฝ”์ผ€์ดํฌ", 2), OrderItems("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 3))) + + Assertions.assertTrue(discountAmount == 6069) + } + + @Test + fun `ํŠน๋ณ„ ํ• ์ธ ๊ธˆ์•ก ํ…Œ์ŠคํŠธ`() { + val discountAmount = decemberEvent.applySpecialDiscount() + + Assertions.assertTrue(discountAmount == 1000) + } + + @Test + fun `์ƒดํŽ˜์ธ ์ฆ์ • ํ…Œ์ŠคํŠธ`() { + Assertions.assertTrue(decemberEvent.presentChampagne(130_000)) + } + + @Test + fun `๋ฐฐ์ง€ ๋ถ€์—ฌ ํ…Œ์ŠคํŠธ`() { + val noneBadge = decemberEvent.assignBadge(0) + val starBadge = decemberEvent.assignBadge(5000) + val treeBadge = decemberEvent.assignBadge(10000) + val santaBadge = decemberEvent.assignBadge(200000) + + Assertions.assertTrue(noneBadge == "์—†์Œ") + Assertions.assertTrue(starBadge == "๋ณ„") + Assertions.assertTrue(treeBadge == "ํŠธ๋ฆฌ") + Assertions.assertTrue(santaBadge == "์‚ฐํƒ€") + } +} \ No newline at end of file
Kotlin
ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์— ๋Œ€ํ•ด์„œ๋Š” ์•„์ง ์–ด๋ ค์›€์„ ๋А๋ผ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ ํ•™์Šต์ด ํ•„์š”ํ•œ ๊ฒƒ ๊ฐ™์•„์š”. ์ž์„ธํ•œ ์„ค๋ช…๊ณผ ๋งํฌ๊นŒ์ง€..๋ฆฌ๋ทฐ ์ •๋ง ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!
@@ -0,0 +1,79 @@ +package christmas.model + +import christmas.utils.Constant.BADGE_SANTA +import christmas.utils.Constant.BADGE_STAR +import christmas.utils.Constant.BADGE_TREE +import christmas.utils.Constant.CHAMPAGNE_CONDITION_AMOUNT +import christmas.utils.Constant.CHRISTMAS_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.DAILY_DISCOUNT_AMOUNT +import christmas.utils.Constant.FIRST_DAY_DISCOUNT_AMOUNT +import christmas.utils.Constant.EMPTY +import christmas.utils.Constant.NONE +import christmas.utils.Constant.ONE_DAY_DECREASE +import christmas.utils.Constant.SANTA_MIN +import christmas.utils.Constant.STAR_MAX +import christmas.utils.Constant.STAR_MIN +import christmas.utils.Constant.TREE_MAX +import christmas.utils.Constant.TREE_MIN +import christmas.utils.Constant.WEEK_DISCOUNT_AMOUNT + +class DecemberEvent { + fun applyTheDayDiscount(date: Int): Int { + val dailyDiscount = (date - ONE_DAY_DECREASE) * DAILY_DISCOUNT_AMOUNT + + return FIRST_DAY_DISCOUNT_AMOUNT + dailyDiscount + } + + fun applyWeekdayDiscount(orderMenu: List<OrderItems>): Int { + var dessertMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseDessertMenuCount(item.getMenuName(), item.getQuantity()) + dessertMenuCount += increaseCount + } + + return dessertMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseDessertMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.CHOCO_CAKE.menuName || menuName == StoreMenu.ICE_CREAM.menuName) + return quantity + + return EMPTY + } + + + fun applyWeekendDiscount(orderMenu: List<OrderItems>): Int { + var mainMenuCount = EMPTY + + for (item in orderMenu) { + val increaseCount = increaseMainMenuCount(item.getMenuName(), item.getQuantity()) + mainMenuCount += increaseCount + } + + return mainMenuCount * WEEK_DISCOUNT_AMOUNT + } + + private fun increaseMainMenuCount(menuName: String, quantity: Int): Int { + if (menuName == StoreMenu.T_BONE_STEAK.menuName || menuName == StoreMenu.BBQ_RIBS.menuName || + menuName == StoreMenu.SEAFOOD_PASTA.menuName || menuName == StoreMenu.CHRISTMAS_PASTA.menuName + ) + return quantity + + return EMPTY + } + + fun applySpecialDiscount(): Int = CHRISTMAS_DAY_DISCOUNT_AMOUNT + + fun presentChampagne(totalOrderAmount: Int): Boolean = totalOrderAmount >= CHAMPAGNE_CONDITION_AMOUNT + + fun assignBadge(benefitAmount: Int): String { + when { + benefitAmount in STAR_MIN..STAR_MAX -> return BADGE_STAR + benefitAmount in TREE_MIN..TREE_MAX -> return BADGE_TREE + benefitAmount >= SANTA_MIN -> return BADGE_SANTA + } + + return NONE + } +} \ No newline at end of file
Kotlin
ํ—‰ ์ €๋„๋ชจ๋ฅด๊ฒŒ when๋ฌธ ์•ˆ์— ๋“ค์–ด์žˆ๋Š” else๋Š” if-else๋ž‘ ๋‹ค๋ฅด๋‹ค๊ณ  ์ƒ๊ฐ์„ ํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค..! ์ œ๊ฐ€ ๋†“์นœ ๋ถ€๋ถ„์„ ์งš์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•˜๋„ค์š”!
@@ -0,0 +1,39 @@ +import { getProductListProps } from './product'; + +interface Props extends getProductListProps { + baseUrl: string; +} + +const buildGetProductListURL = ({ + baseUrl, + category, + page, + size, + order, +}: Props) => { + const params = []; + + if (category) { + params.push(`category=${encodeURIComponent(category)}`); + } + if (page !== undefined) { + params.push(`page=${page}`); + } + if (size !== undefined) { + params.push(`size=${size}`); + } + + if (order === 'desc') { + params.push(`sort=price%2C${order}&sort=name`); + } else { + params.push('sort=price&sort=name'); + } + + if (params.length > 0) { + baseUrl += `?${params.join('&')}`; + } + + return baseUrl; +}; + +export default buildGetProductListURL;
TypeScript
api ๊ด€๋ฆฌํ•œ๊ฑฐ ๋ฉ‹์žˆ์–ด์š”... ๐Ÿ‘ step2 ๋•Œ ์ €๋„ ์ด๋ ‡๊ฒŒํ•ด๋ณด๊ณ ์‹ถ๋„ค์š”!!
@@ -0,0 +1,47 @@ +import React from 'react'; +import styled from '@emotion/styled'; +import { theme } from '@/style/theme.style'; + +type ButtonThemeType = 'dark' | 'light' | 'disabled'; + +const BUTTON_THEME = { + dark: { + backgroundColor: theme.color.black, + color: theme.color.white, + }, + light: { + backgroundColor: theme.color.lightGray, + color: theme.color.black, + }, + disabled: { + backgroundColor: theme.color.gray, + color: theme.color.white, + }, +}; + +interface Props extends React.ButtonHTMLAttributes<HTMLButtonElement> { + children?: React.ReactNode; + $theme?: ButtonThemeType; + $width?: string; + $height?: string; + $borderRadius?: string; +} + +const BaseButton = ({ children, ...rest }: Props) => { + return <StyledButton {...rest}>{children}</StyledButton>; +}; + +export default BaseButton; + +const StyledButton = styled.button<Props>` + display: flex; + justify-content: center; + align-items: center; + background-color: ${({ $theme }) => + $theme ? BUTTON_THEME[$theme].backgroundColor : 'transparent'}; + color: ${({ $theme }) => ($theme ? BUTTON_THEME[$theme].color : 'inherit')}; + width: ${({ $width }) => ($width ? $width : 'fit-content')}; + height: ${({ $height }) => ($height ? $height : 'fit-content')}; + border-radius: ${({ $borderRadius }) => + $borderRadius ? $borderRadius : '4px'}; +`;
Unknown
styled ์ด๋ ‡๊ฒŒ ํ•œ ํŒŒ์ผ์— ์ ๋Š”๊ฒƒ๋„ ๊ฐ€๋…์„ฑ ์ข‹๋„ค์š” ์ €๋„ emotion ์ผ๋Š”๋ฐ ui ์ž‘์—…ํ•˜๋ฉด์„œ ๊ณ„์† ์™€๋ฆฌ๊ฐ€๋ฆฌํ–ˆ๋˜ ๊ฒฝํ—˜์ด์žˆ์–ด์„œ..
@@ -0,0 +1,49 @@ +package com.codesquad.rocket.config; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpHeaders; +import org.springframework.stereotype.Component; + +@Component +public class SimpleCorsFilter implements Filter { + + private final Logger logger = LoggerFactory.getLogger(SimpleCorsFilter.class); + + public SimpleCorsFilter() { + logger.info("SimpleCORSFilter init"); + } + + @Override + public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws + IOException, ServletException { + + HttpServletRequest request = (HttpServletRequest) req; + HttpServletResponse response = (HttpServletResponse) res; + response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, request.getHeader("Origin")); + response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); + response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "POST, GET, OPTIONS, DELETE, PUT, PATCH"); + response.setHeader(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "3600"); + response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "Content-Type, Accept, X-Requested-With, remember-me, Authorization"); + chain.doFilter(req, res); + } + + @Override + public void init(FilterConfig filterConfig) { + } + + @Override + public void destroy() { + } +}
Java
HttpHeaders์— static finalํ•œ ํ˜•ํƒœ๋กœ ํ—ค๋”์— ์ง€์ •ํ•˜๋Š” ํ…์ŠคํŠธ๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•œ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌํŒฉํ† ๋งํ•œ๋‹ค.
@@ -0,0 +1,35 @@ +package christmas.domain.discount.policy; + +import christmas.domain.common.Date; +import christmas.domain.common.Money; +import christmas.domain.discount.DateDiscountPolicy; +import christmas.domain.discount.DiscountResult; + +public class ChristmasDDayDiscountPolicy extends DateDiscountPolicy { + + private static final Date DISCOUNTABLE_STANDARD_DATE = Date.from(25); + private static final Money BASE_DISCOUNT_AMOUNT = Money.from(-1_000); + private static final Money ADD_AMOUNT = Money.from(-100); + private static final String POLICY_NAME = "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"; + + @Override + protected boolean isSatisfied(Date date) { + return !date.isLaterThan(DISCOUNTABLE_STANDARD_DATE); + } + + @Override + protected DiscountResult bulidDiscountResult(Date date) { + Money discountAmount = calculateDiscountAmount(date); + + return new DiscountResult(POLICY_NAME, discountAmount); + } + + private Money calculateDiscountAmount(Date date) { + Money discountAmount = BASE_DISCOUNT_AMOUNT; + for (int day = 1; day < date.getDayOfMonth(); day++) { + discountAmount = discountAmount.add(ADD_AMOUNT); + } + + return discountAmount; + } +}
Java
์ด๋ ‡๊ฒŒ ํ•˜๋ฉด, ๋งค ๋ฐ˜๋ณต๋งˆ๋‹ค getDayOfMonth ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„ฑ๋Šฅ๋ฉด์—์„œ ์ข‹์ง€ ์•Š์„๊ฑฐ ๊ฐ™์•„์š”! ์ง€๊ธˆ์€ getDayOfMonth์— ๋ณต์žกํ•œ ๋กœ์ง์ด ์—†์–ด ํฌ๊ฒŒ ์ฐจ์ด๋Š” ์—†์„๊ฑฐ ๊ฐ™์ง€๋งŒ, ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋Š” ์„ฑ๋Šฅ ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฉ”์†Œ๋“œ์˜ ๊ฐ’์„ ๋ณ€์ˆ˜๋กœ ํ• ๋‹นํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–ค๊ฐ€์š”? ๐Ÿ˜€
@@ -0,0 +1,55 @@ +package christmas.domain.menu; + +import christmas.domain.common.Date; +import christmas.domain.common.Money; +import christmas.domain.discount.DateDiscountPolicy; +import christmas.domain.discount.DiscountResult; +import java.util.Objects; + +public abstract class AbstractMenu implements Menu { + + protected final MenuName name; + protected final Money price; + protected final DateDiscountPolicy policy; + + protected AbstractMenu(MenuName name, Money price, DateDiscountPolicy policy) { + this.name = name; + this.price = price; + this.policy = policy; + } + + @Override + public Money calculatePriceWith(int quantity) { + return price.multiply(quantity); + } + + @Override + public DiscountResult calculateDiscountBenefitWith(Date date, int quantity) { + DiscountResult menuDiscountResult = policy.discount(date); + + return new DiscountResult(menuDiscountResult.policyName(), menuDiscountResult.amount().multiply(quantity)); + } + + @Override + public boolean isDiscountable(Date date) { + return policy.isDiscountable(date); + } + + @Override + public String getName() { + return name.getName(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + AbstractMenu that = (AbstractMenu) o; + return Objects.equals(name, that.name) && Objects.equals(price, that.price); + } + + @Override + public int hashCode() { + return Objects.hash(name, price); + } +}
Java
์˜ค์ž‰ ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ์„ ์–ธํ•œ ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”. ์ œ๊ฐ€ ๋ดค์„ ๋• ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๊ฐ€ ์—†๋Š”๊ฑฐ ๊ฐ™์€๋ฐ ๋งž์„๊นŒ์š”? ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๊ฐ€ ์—†๋Š”๋ฐ ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ์„ ์–ธํ•œ ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,35 @@ +package christmas.domain.discount.policy; + +import christmas.domain.common.Date; +import christmas.domain.common.Money; +import christmas.domain.discount.DateDiscountPolicy; +import christmas.domain.discount.DiscountResult; + +public class ChristmasDDayDiscountPolicy extends DateDiscountPolicy { + + private static final Date DISCOUNTABLE_STANDARD_DATE = Date.from(25); + private static final Money BASE_DISCOUNT_AMOUNT = Money.from(-1_000); + private static final Money ADD_AMOUNT = Money.from(-100); + private static final String POLICY_NAME = "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"; + + @Override + protected boolean isSatisfied(Date date) { + return !date.isLaterThan(DISCOUNTABLE_STANDARD_DATE); + } + + @Override + protected DiscountResult bulidDiscountResult(Date date) { + Money discountAmount = calculateDiscountAmount(date); + + return new DiscountResult(POLICY_NAME, discountAmount); + } + + private Money calculateDiscountAmount(Date date) { + Money discountAmount = BASE_DISCOUNT_AMOUNT; + for (int day = 1; day < date.getDayOfMonth(); day++) { + discountAmount = discountAmount.add(ADD_AMOUNT); + } + + return discountAmount; + } +}
Java
๋ถˆ๊ณผ ๋ช‡๋‹ฌ์ „์— ์ฑ…์—์„œ ๋ดค๋˜ ๋‚ด์šฉ์ด๋„ค์š”.. ์ด์ œ ์ ˆ๋Œ€ ๊นŒ๋จน์„๋ฆฌ ์—†์„ ๊ฒƒ ๊ฐ™๋„ค์š”! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ™‡๐Ÿปโ€โ™‚๏ธ
@@ -0,0 +1,55 @@ +package christmas.domain.menu; + +import christmas.domain.common.Date; +import christmas.domain.common.Money; +import christmas.domain.discount.DateDiscountPolicy; +import christmas.domain.discount.DiscountResult; +import java.util.Objects; + +public abstract class AbstractMenu implements Menu { + + protected final MenuName name; + protected final Money price; + protected final DateDiscountPolicy policy; + + protected AbstractMenu(MenuName name, Money price, DateDiscountPolicy policy) { + this.name = name; + this.price = price; + this.policy = policy; + } + + @Override + public Money calculatePriceWith(int quantity) { + return price.multiply(quantity); + } + + @Override + public DiscountResult calculateDiscountBenefitWith(Date date, int quantity) { + DiscountResult menuDiscountResult = policy.discount(date); + + return new DiscountResult(menuDiscountResult.policyName(), menuDiscountResult.amount().multiply(quantity)); + } + + @Override + public boolean isDiscountable(Date date) { + return policy.isDiscountable(date); + } + + @Override + public String getName() { + return name.getName(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + AbstractMenu that = (AbstractMenu) o; + return Objects.equals(name, that.name) && Objects.equals(price, that.price); + } + + @Override + public int hashCode() { + return Objects.hash(name, price); + } +}
Java
์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ ๋ง‰๊ณ , ๊ณตํ†ต ๋กœ์ง์„ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ abstract๋กœ ๋งŒ๋“ค์—ˆ์–ด์š”! ํ•˜์ง€๋งŒ, ํ•˜๋Š˜๋‹˜์ฒ˜๋Ÿผ ํƒ€์ž… ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฉ”๋‰ด๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋ฉด ์ด๋Ÿฐ ์ฝ”๋“œ๋Š” ๋ถˆํ•„์š”ํ•  ๊ฒƒ ๊ฐ™๋„ค์š”!
@@ -0,0 +1,77 @@ +package christmas.domain.common; + +import static christmas.global.util.ObjectUtil.requireNonNull; + +import christmas.global.util.DateUtil; +import java.util.Objects; + +public class Date { + + public static final int BASE_MONTH = 12; + private static final int BASE_YEAR = 2023; + private static final int DAY_OF_MONTH_MIN_RANGE = 1; + private static final int DAY_OF_MONTH_MAX_RANGE = 31; + private static final String INVALID_DAY_OF_MONTH_RANGE_MESSAGE = + "๋‚ ์งœ๋Š” " + DAY_OF_MONTH_MIN_RANGE + "๊ณผ " + DAY_OF_MONTH_MAX_RANGE + " ์‚ฌ์ด์˜ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."; + private static final String UNKNOWN_DATE_MESSAGE = "์•Œ ์ˆ˜ ์—†๋Š” ๋‚ ์งœ์ž…๋‹ˆ๋‹ค."; + + private final int dayOfMonth; + + private Date(int dayOfMonth) { + this.dayOfMonth = dayOfMonth; + } + + public static Date from(int dayOfMonth) { + checkDayOfMonthRange(dayOfMonth); + + return new Date(dayOfMonth); + } + + private static void checkDayOfMonthRange(int dayOfMonth) { + checkDayOfMonthMinRange(dayOfMonth); + checkDayOfMonthMaxRange(dayOfMonth); + } + + private static void checkDayOfMonthMinRange(int dayOfMonth) { + if (dayOfMonth < DAY_OF_MONTH_MIN_RANGE) { + throw new IllegalArgumentException(INVALID_DAY_OF_MONTH_RANGE_MESSAGE); + } + } + + private static void checkDayOfMonthMaxRange(int dayOfMonth) { + if (dayOfMonth > DAY_OF_MONTH_MAX_RANGE) { + throw new IllegalArgumentException(INVALID_DAY_OF_MONTH_RANGE_MESSAGE); + } + } + + public boolean isLaterThan(Date target) { + requireNonNull(target, UNKNOWN_DATE_MESSAGE); + + return this.dayOfMonth > target.dayOfMonth; + } + + public boolean isWeekDay() { + return DateUtil.isWeekday(BASE_YEAR, BASE_MONTH, dayOfMonth); + } + + public boolean isWeekend() { + return DateUtil.isWeekend(BASE_YEAR, BASE_MONTH, dayOfMonth); + } + + public int getDayOfMonth() { + return dayOfMonth; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Date date = (Date) o; + return dayOfMonth == date.dayOfMonth; + } + + @Override + public int hashCode() { + return Objects.hash(dayOfMonth); + } +}
Java
> ๋ฐฉ๋ฌธํ•  ๋‚ ์งœ๋Š” 1 ์ด์ƒ 31 ์ดํ•˜์˜ ์ˆซ์ž๋กœ๋งŒ ์ž…๋ ฅ๋ฐ›์•„ ์ฃผ์„ธ์š”. > 1 ์ด์ƒ 31 ์ดํ•˜์˜ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ, "[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."๋ผ๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด์—ฌ ์ฃผ์„ธ์š”. ์š”๊ตฌ์‚ฌํ•ญ์—์„œ ๋ช…์‹œํ•œ ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€ ๋ง๊ณ  ์ด๋Ÿฌํ•œ ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€๋ฅผ ์„ ํƒํ•œ ์ด์œ ๋Š” ๊ตฌ์ฒด์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•จ์ด์˜€๋‚˜์š” ? ๐Ÿ˜„
@@ -0,0 +1,63 @@ +package christmas.domain.common; + +import static christmas.global.util.ObjectUtil.requireNonNull; + +import java.util.Objects; + +public class Money { + + private static final int ZERO_MONEY_AMOUNT = 0; + private static final String UNKNOWN_MONEY_MESSAGE = "์•Œ ์ˆ˜ ์—†๋Š” ๋ˆ๊ณผ ํ•ด๋‹น ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."; + public static final String CURRENCY = "์›"; + + private final long amount; + + private Money(long amount) { + this.amount = amount; + } + + public static Money from(long amount) { + return new Money(amount); + } + + public static Money zero() { + return new Money(ZERO_MONEY_AMOUNT); + } + + public Money add(Money target) { + requireNonNull(target, UNKNOWN_MONEY_MESSAGE); + + return new Money(this.amount + target.amount); + } + + public Money subtract(Money target) { + requireNonNull(target, UNKNOWN_MONEY_MESSAGE); + + return new Money(this.amount - target.amount); + } + + public Money multiply(int multiplier) { + return new Money(this.amount * multiplier); + } + + public boolean isGreaterThanEqual(int amount) { + return this.amount >= amount; + } + + public long getAmount() { + return amount; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Money money = (Money) o; + return amount == money.amount; + } + + @Override + public int hashCode() { + return Objects.hash(amount); + } +}
Java
๋ถˆ๋ณ€ ๊ฐ์ฒด๋กœ ํ™œ์šฉํ•œ ์ ์ด ์ข‹๋„ค์š” ~ ๐Ÿ˜„
@@ -0,0 +1,30 @@ +package christmas.domain.discount; + +import static christmas.global.util.ObjectUtil.requireNonNull; + +import christmas.domain.common.Date; + +public abstract class DateDiscountPolicy { + + private static final String UNKNOWN_DATE_MESSAGE = "์•Œ ์ˆ˜ ์—†๋Š” ๋‚ ์งœ์ž…๋‹ˆ๋‹ค."; + private static final String CANT_APPLY_DISCOUNT_MESSAGE = "ํ• ์ธ์„ ์ ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."; + + public DiscountResult discount(Date date) { + requireNonNull(date, UNKNOWN_DATE_MESSAGE); + if (!isDiscountable(date)) { + throw new IllegalStateException(CANT_APPLY_DISCOUNT_MESSAGE); + } + + return bulidDiscountResult(date); + } + + public boolean isDiscountable(Date date) { + requireNonNull(date, UNKNOWN_DATE_MESSAGE); + + return isSatisfied(date); + } + + protected abstract boolean isSatisfied(Date date); + + protected abstract DiscountResult bulidDiscountResult(Date date); +}
Java
ํ•˜์œ„ ์ž์‹ ๊ตฌํ˜„์ฒด๊ฐ€ ์ƒ์œ„ ๋ถ€๋ชจ ๊ฐ์ฒด์˜ ๋กœ์ง์— ์˜ํ–ฅ์„ ์ฃผ๊ธฐ ์œ„ํ•ด์„œ protected abstract๋กœ ์„ ์–ธํ•œ ๊ฒƒ์ด ์ธ์ƒ์ ์ด๋„ค์š”~ ๐Ÿ˜„
@@ -0,0 +1,18 @@ +package christmas.domain.discount.policy; + +import christmas.domain.common.Date; +import christmas.domain.discount.DateDiscountPolicy; +import christmas.domain.discount.DiscountResult; + +public class NoneDiscountPolicy extends DateDiscountPolicy { + + @Override + protected boolean isSatisfied(Date date) { + return false; + } + + @Override + protected DiscountResult bulidDiscountResult(Date date) { + return null; + } +}
Java
ํ•ด๋‹น Policy๋Š” ์–ด๋–ค ์ด์œ  ๋•Œ๋ฌธ์— ๊ตฌ์„ฑํ•˜์…จ๋‚˜์š” ~ ? ๐Ÿ˜„
@@ -0,0 +1,35 @@ +package christmas.domain.discount.policy; + +import christmas.domain.common.Date; +import christmas.domain.common.Money; +import christmas.domain.discount.DateDiscountPolicy; +import christmas.domain.discount.DiscountResult; + +public class ChristmasDDayDiscountPolicy extends DateDiscountPolicy { + + private static final Date DISCOUNTABLE_STANDARD_DATE = Date.from(25); + private static final Money BASE_DISCOUNT_AMOUNT = Money.from(-1_000); + private static final Money ADD_AMOUNT = Money.from(-100); + private static final String POLICY_NAME = "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"; + + @Override + protected boolean isSatisfied(Date date) { + return !date.isLaterThan(DISCOUNTABLE_STANDARD_DATE); + } + + @Override + protected DiscountResult bulidDiscountResult(Date date) { + Money discountAmount = calculateDiscountAmount(date); + + return new DiscountResult(POLICY_NAME, discountAmount); + } + + private Money calculateDiscountAmount(Date date) { + Money discountAmount = BASE_DISCOUNT_AMOUNT; + for (int day = 1; day < date.getDayOfMonth(); day++) { + discountAmount = discountAmount.add(ADD_AMOUNT); + } + + return discountAmount; + } +}
Java
๊ฐœ์ธ์ ์œผ๋กœ ์ „ ํ• ์ธ๋œ ๊ธˆ์•ก์„ ์–‘์ˆ˜ ๊ฐ’์œผ๋กœ ์œ ์ง€ํ•˜๊ณ , View๋กœ ๋ณด์—ฌ์ฃผ๋Š” ์‹œ์ ์— "-" ๋งˆ์ด๋„ˆ์Šค ๋ถ€ํ˜ธ๋ฅผ ๋ถ™์—ฌ์ฃผ๋„๋ก ํ™œ์šฉํ•จ์œผ๋กœ์จ ์ด ๊ธˆ์•ก์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์—์„œ ์–ผ๋งˆ๋‚˜ ๋นผ์•ผ ํ•  ๊ธˆ์•ก์ธ์ง€๋ฅผ ์ง๊ด€์ ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ์˜๋„ํ–ˆ๋Š”๋ฐ ํ•˜๋Š˜๋‹˜์€ ์–ด๋–ค ์ด์œ  ๋•Œ๋ฌธ์— ์ฒ˜์Œ๋ถ€ํ„ฐ ํ• ์ธ๋œ ๊ธˆ์•ก์„ ์Œ์ˆ˜ ๊ฐ’์œผ๋ฃŒ ์œ ์ง€ํ•˜๊ณ ์ž ํ•˜์…จ๋‚˜์š”~? ๐Ÿ˜„
@@ -0,0 +1,35 @@ +package christmas.domain.discount.policy; + +import christmas.domain.common.Date; +import christmas.domain.common.Money; +import christmas.domain.discount.DateDiscountPolicy; +import christmas.domain.discount.DiscountResult; +import java.util.List; + +public class SpecialDiscountPolicy extends DateDiscountPolicy { + + private static final List<Date> specialDays; + private static final Money DISCOUNT_AMOUNT = Money.from(-1_000); + private static final String POLICY_NAME = "ํŠน๋ณ„ ํ• ์ธ"; + + static { + specialDays = List.of( + Date.from(3), + Date.from(10), + Date.from(17), + Date.from(24), + Date.from(25), + Date.from(31) + ); + } + + @Override + protected boolean isSatisfied(Date date) { + return specialDays.contains(date); + } + + @Override + protected DiscountResult bulidDiscountResult(Date date) { + return new DiscountResult(POLICY_NAME, DISCOUNT_AMOUNT); + } +}
Java
ํŠน๋ณ„ํ•œ ๋‚ ์งœ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ List๋ฅผ ํ™œ์šฉํ•˜์…จ๋Š”๋ฐ, Set์€ ์–ด๋–จ๊นŒ์š”~ ? ๐Ÿ˜„
@@ -0,0 +1,46 @@ +package christmas.domain.menu; + +import static christmas.global.util.ObjectUtil.requireIncludeNonNull; +import static christmas.global.util.ObjectUtil.requireNonNull; + +import java.util.HashSet; +import java.util.List; +import java.util.Objects; + +public class Menus { + + private static final String UNKNOWN_MENUS_MESSAGE = "์•Œ ์ˆ˜ ์—†๋Š” ๋ฉ”๋‰ด ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค."; + private static final String UNKNOWN_MENU_MESSAGE = "์•Œ ์ˆ˜ ์—†๋Š” ๋ฉ”๋‰ด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค."; + + private final List<Menu> menus; + + private Menus(List<Menu> menus) { + this.menus = menus; + } + + public static Menus from(List<Menu> menus) { + requireNonNull(menus, UNKNOWN_MENUS_MESSAGE); + requireIncludeNonNull(menus, UNKNOWN_MENU_MESSAGE); + + return new Menus(menus); + } + + public boolean containsAll(Menus other) { + requireNonNull(other, UNKNOWN_MENUS_MESSAGE); + + return new HashSet<>(menus).containsAll(other.menus); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Menus menus1 = (Menus) o; + return Objects.equals(menus, menus1.menus); + } + + @Override + public int hashCode() { + return Objects.hash(menus); + } +}
Java
List์„ Set์œผ๋กœ ๋ณ€๊ฒฝํ•ด์„œ ํ™•์ธํ•˜๋Š” ๋ถ€๋ถ„์ด ์ข‹๋„ค์š” ~ ๐Ÿ˜„
@@ -0,0 +1,77 @@ +package christmas.domain.common; + +import static christmas.global.util.ObjectUtil.requireNonNull; + +import christmas.global.util.DateUtil; +import java.util.Objects; + +public class Date { + + public static final int BASE_MONTH = 12; + private static final int BASE_YEAR = 2023; + private static final int DAY_OF_MONTH_MIN_RANGE = 1; + private static final int DAY_OF_MONTH_MAX_RANGE = 31; + private static final String INVALID_DAY_OF_MONTH_RANGE_MESSAGE = + "๋‚ ์งœ๋Š” " + DAY_OF_MONTH_MIN_RANGE + "๊ณผ " + DAY_OF_MONTH_MAX_RANGE + " ์‚ฌ์ด์˜ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."; + private static final String UNKNOWN_DATE_MESSAGE = "์•Œ ์ˆ˜ ์—†๋Š” ๋‚ ์งœ์ž…๋‹ˆ๋‹ค."; + + private final int dayOfMonth; + + private Date(int dayOfMonth) { + this.dayOfMonth = dayOfMonth; + } + + public static Date from(int dayOfMonth) { + checkDayOfMonthRange(dayOfMonth); + + return new Date(dayOfMonth); + } + + private static void checkDayOfMonthRange(int dayOfMonth) { + checkDayOfMonthMinRange(dayOfMonth); + checkDayOfMonthMaxRange(dayOfMonth); + } + + private static void checkDayOfMonthMinRange(int dayOfMonth) { + if (dayOfMonth < DAY_OF_MONTH_MIN_RANGE) { + throw new IllegalArgumentException(INVALID_DAY_OF_MONTH_RANGE_MESSAGE); + } + } + + private static void checkDayOfMonthMaxRange(int dayOfMonth) { + if (dayOfMonth > DAY_OF_MONTH_MAX_RANGE) { + throw new IllegalArgumentException(INVALID_DAY_OF_MONTH_RANGE_MESSAGE); + } + } + + public boolean isLaterThan(Date target) { + requireNonNull(target, UNKNOWN_DATE_MESSAGE); + + return this.dayOfMonth > target.dayOfMonth; + } + + public boolean isWeekDay() { + return DateUtil.isWeekday(BASE_YEAR, BASE_MONTH, dayOfMonth); + } + + public boolean isWeekend() { + return DateUtil.isWeekend(BASE_YEAR, BASE_MONTH, dayOfMonth); + } + + public int getDayOfMonth() { + return dayOfMonth; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Date date = (Date) o; + return dayOfMonth == date.dayOfMonth; + } + + @Override + public int hashCode() { + return Objects.hash(dayOfMonth); + } +}
Java
๋„ต ๋งž์Šต๋‹ˆ๋‹ค. ์š”๊ตฌ์‚ฌํ•ญ์— ๋‚˜์˜ค๋Š” ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€๋Š” ๋„ˆ๋ฌด ์ผ๋ฐ˜์ ์—ฌ์„œ์š”. ๊ฐœ๋ฐœํ•˜๋ฉด์„œ ๋กœ๊ทธ๋ฅผ ์ฐ์–ด ๋ณผ ์ผ์ด ์—ˆ์—ˆ๋Š”๋ฐ, ๋ถˆํŽธํ•˜๋”๋ผ๊ตฌ์š”. ๋”ฐ๋ผ์„œ, ์œ„์™€ ๊ฐ™์ด ๊ตฌ์ฒด์ ์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ณ , ๋‹ค์‹œ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ”๊พธ๋Š” ์ž‘์—…์„ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,18 @@ +package christmas.domain.discount.policy; + +import christmas.domain.common.Date; +import christmas.domain.discount.DateDiscountPolicy; +import christmas.domain.discount.DiscountResult; + +public class NoneDiscountPolicy extends DateDiscountPolicy { + + @Override + protected boolean isSatisfied(Date date) { + return false; + } + + @Override + protected DiscountResult bulidDiscountResult(Date date) { + return null; + } +}
Java
```java @Override public DiscountResult calculateDiscountBenefitWith(Date date, int quantity) { DiscountResult menuDiscountResult = policy.discount(date); return new DiscountResult(menuDiscountResult.policyName(), menuDiscountResult.amount().multiply(quantity)); } ``` DateDiscountPolicy๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ๋Š” ์œ„์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ discount(๋‚ด๋ถ€์—์„œ buildDiscountResult, isSatisfied ํ˜ธ์ถœ)๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ์š”. ๋ฉ”๋‰ด๊ฐ€ ์ •์ฑ…์ด ์—†๋Š” ๊ฒƒ์„ ์ด๊ณณ์—์„œ ํŒ๋‹จํ•˜๊ธฐ ๋ณด๋‹ค๋Š”, NoneDiscountPolicy๋ฅผ ๋งŒ๋“ค์–ด์„œ ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค.!
@@ -0,0 +1,35 @@ +package christmas.domain.discount.policy; + +import christmas.domain.common.Date; +import christmas.domain.common.Money; +import christmas.domain.discount.DateDiscountPolicy; +import christmas.domain.discount.DiscountResult; + +public class ChristmasDDayDiscountPolicy extends DateDiscountPolicy { + + private static final Date DISCOUNTABLE_STANDARD_DATE = Date.from(25); + private static final Money BASE_DISCOUNT_AMOUNT = Money.from(-1_000); + private static final Money ADD_AMOUNT = Money.from(-100); + private static final String POLICY_NAME = "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"; + + @Override + protected boolean isSatisfied(Date date) { + return !date.isLaterThan(DISCOUNTABLE_STANDARD_DATE); + } + + @Override + protected DiscountResult bulidDiscountResult(Date date) { + Money discountAmount = calculateDiscountAmount(date); + + return new DiscountResult(POLICY_NAME, discountAmount); + } + + private Money calculateDiscountAmount(Date date) { + Money discountAmount = BASE_DISCOUNT_AMOUNT; + for (int day = 1; day < date.getDayOfMonth(); day++) { + discountAmount = discountAmount.add(ADD_AMOUNT); + } + + return discountAmount; + } +}
Java
View์—์„œ ๋งˆ์ด๋„ˆ์Šค ๋ถ€ํ˜ธ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋ฒˆ๊ฑฐ๋กœ์›Œ์„œ ๊ทธ๋Ÿฐ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ™•์‹คํžˆ ์–‘์ˆ˜๋กœ ํ•˜๋Š” ๊ฒƒ์ด, ๋ง์”€ ์ฃผ์‹  ๊ฒƒ์ฒ˜๋Ÿผ ๋”์šฑ ์ง๊ด€์ ์ธ ๊ฒƒ ๊ฐ™์•„์š”! ++ Money์— substract ์—ฐ์‚ฐ์„ ๋งŒ๋“  ์ด์œ ๋„ ์ฒ˜์Œ์—๋Š” ํ• ์ธ ๊ฐ’์„ ์–‘์ˆ˜๋กœ ์œ ์ง€ํ•˜๋ ค๊ณ  ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ์š”. ์ง€๊ธˆ๋ณด๋‹ˆ, money.substract๋ฅผ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋Š”๊ตฐ์š”... ํ”ผ๋“œ๋ฐฑ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,84 @@ +import { ๋น„๋™๊ธฐ_์š”์ฒญ } from 'constants/'; + +const errorReturn = (error) => ({ + status: ๋น„๋™๊ธฐ_์š”์ฒญ.FAIL, + content: `์„œ๋ฒ„์™€์˜ ํ†ต์‹ ์— ์‹คํŒจํ•˜์˜€์Šต๋‹ˆ๋‹ค. (${error.message})`, +}); + +const authorizedHeader = (header) => ({ + ...header, + Authorization: `Bearer ${sessionStorage.getItem('accessToken')}`, +}); + +class RequestAsync { + constructor() { + this.HOST_NAME = process.env.REACT_APP_API_URL; + this.header = { 'Content-Type': 'application/json' }; + } + + async #getRefinedResponse(response) { + const responseString = await response.text(); + + return { + status: response.ok ? ๋น„๋™๊ธฐ_์š”์ฒญ.SUCCESS : ๋น„๋™๊ธฐ_์š”์ฒญ.FAILURE, + content: responseString ? JSON.parse(responseString) : {}, + }; + } + + async get(path, authorize = false) { + try { + const response = await fetch(`${this.HOST_NAME}/${path}`, { + method: 'GET', + headers: authorize ? authorizedHeader(this.header) : this.header, + }); + + return this.#getRefinedResponse(response); + } catch (error) { + return errorReturn(error); + } + } + + async post(path, bodyData, authorize = false) { + try { + const response = await fetch(`${this.HOST_NAME}/${path}`, { + method: 'POST', + headers: authorize ? authorizedHeader(this.header) : this.header, + body: JSON.stringify(bodyData), + }); + + return this.#getRefinedResponse(response); + } catch (error) { + return errorReturn(error); + } + } + + async put(path, bodyData, authorize = false) { + try { + const response = await fetch(`${this.HOST_NAME}/${path}`, { + method: 'PUT', + headers: authorize ? authorizedHeader(this.header) : this.header, + body: JSON.stringify(bodyData), + }); + + return this.#getRefinedResponse(response); + } catch (error) { + return errorReturn(error); + } + } + + async delete(path, authorize = false) { + try { + const response = await fetch(`${this.HOST_NAME}/${path}`, { + method: 'DELETE', + headers: authorize ? authorizedHeader(this.header) : this.header, + }); + + return this.#getRefinedResponse(response); + } catch (error) { + return errorReturn(error); + } + } +} + +const requestAsync = new RequestAsync(); +export default requestAsync;
JavaScript
์˜ค~
@@ -0,0 +1,67 @@ +package com.codesquad.airbnb.business; + +import com.codesquad.airbnb.exception.ErrorMessage; +import com.codesquad.airbnb.exception.custom.EntityNotFoundException; +import com.codesquad.airbnb.common.utils.DAOUtils; +import com.codesquad.airbnb.dao.AccommodationMapper; +import com.codesquad.airbnb.dao.BookingMapper; +import com.codesquad.airbnb.dao.FeePolicyMapper; +import com.codesquad.airbnb.domain.dto.AccommodationDTO; +import com.codesquad.airbnb.domain.model.Accommodation; +import com.codesquad.airbnb.domain.model.Booking; +import com.codesquad.airbnb.domain.model.Filter; +import com.codesquad.airbnb.domain.model.User; +import org.springframework.stereotype.Service; + +import java.util.Map; + +@Service +public class BookService { + + private AccommodationMapper accommodationMapper; + private FeePolicyMapper feePolicyMapper; + private BookingMapper bookingMapper; + + public BookService(AccommodationMapper accommodationMapper, + FeePolicyMapper feePolicyMapper, + BookingMapper bookingMapper) { + this.accommodationMapper = accommodationMapper; + this.feePolicyMapper = feePolicyMapper; + this.bookingMapper = bookingMapper; + } + + public AccommodationDTO getAccommodation(Integer id, Filter filter) { + if(accommodationMapper.countById(id) == 0) { + throw new EntityNotFoundException(ErrorMessage.ENTITY_NOT_FOUND.getMessage()); + } + + Map<String, Object> parameters = DAOUtils.createParameters(filter, id); + Accommodation accommodation = accommodationMapper.findAccommodationChargeInfoById(parameters); + + float accommodationTaxRate = feePolicyMapper.findAccommodationTax(); + + return new AccommodationDTO.Builder(accommodation.getId()) + .pricePerNight(accommodation.getOriginalPricePerNight()) + .pricePerNightDiscounted(accommodation.getDiscountedPricePerNight()) + .priceDuringPeriod(accommodation.getPriceDuringPeriod(filter)) + .cleaningFee(accommodation.getCleaningFee()) + .serviceTax(accommodation.getServiceTax(filter)) + .accommodationTax(accommodation.getAccommodationTax(filter, accommodationTaxRate)) + .totalPrice(accommodation.getTotalPrice(filter, accommodationTaxRate)) + .build(); + } + + public boolean booking(Integer id, Filter filter, User user) { + if(accommodationMapper.countById(id) == 0) { + throw new EntityNotFoundException(ErrorMessage.ENTITY_NOT_FOUND.getMessage()); + } + + Booking newBooking = new Booking(user, id, filter); + + if (bookingMapper.countByAccommodationIdAndPeriod(newBooking) > 0) { + return false; + } + bookingMapper.insertBooking(newBooking); + return true; + } +}
Java
๋‹จ์ˆœํžˆ null-safety๋ฅผ ์œ„ํ•ด ์นด์šดํŠธ ๋กœ์ง์„ ํ•œ ๋ฒˆ ๋” ๋„ฃ์€ ๊ฑฐ๋ผ๋ฉด, null check๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ์ƒ์— ๋” ํฐ ์ด์ ์ด ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.
@@ -0,0 +1,67 @@ +package com.codesquad.airbnb.business; + +import com.codesquad.airbnb.exception.ErrorMessage; +import com.codesquad.airbnb.exception.custom.EntityNotFoundException; +import com.codesquad.airbnb.common.utils.DAOUtils; +import com.codesquad.airbnb.dao.AccommodationMapper; +import com.codesquad.airbnb.dao.BookingMapper; +import com.codesquad.airbnb.dao.FeePolicyMapper; +import com.codesquad.airbnb.domain.dto.AccommodationDTO; +import com.codesquad.airbnb.domain.model.Accommodation; +import com.codesquad.airbnb.domain.model.Booking; +import com.codesquad.airbnb.domain.model.Filter; +import com.codesquad.airbnb.domain.model.User; +import org.springframework.stereotype.Service; + +import java.util.Map; + +@Service +public class BookService { + + private AccommodationMapper accommodationMapper; + private FeePolicyMapper feePolicyMapper; + private BookingMapper bookingMapper; + + public BookService(AccommodationMapper accommodationMapper, + FeePolicyMapper feePolicyMapper, + BookingMapper bookingMapper) { + this.accommodationMapper = accommodationMapper; + this.feePolicyMapper = feePolicyMapper; + this.bookingMapper = bookingMapper; + } + + public AccommodationDTO getAccommodation(Integer id, Filter filter) { + if(accommodationMapper.countById(id) == 0) { + throw new EntityNotFoundException(ErrorMessage.ENTITY_NOT_FOUND.getMessage()); + } + + Map<String, Object> parameters = DAOUtils.createParameters(filter, id); + Accommodation accommodation = accommodationMapper.findAccommodationChargeInfoById(parameters); + + float accommodationTaxRate = feePolicyMapper.findAccommodationTax(); + + return new AccommodationDTO.Builder(accommodation.getId()) + .pricePerNight(accommodation.getOriginalPricePerNight()) + .pricePerNightDiscounted(accommodation.getDiscountedPricePerNight()) + .priceDuringPeriod(accommodation.getPriceDuringPeriod(filter)) + .cleaningFee(accommodation.getCleaningFee()) + .serviceTax(accommodation.getServiceTax(filter)) + .accommodationTax(accommodation.getAccommodationTax(filter, accommodationTaxRate)) + .totalPrice(accommodation.getTotalPrice(filter, accommodationTaxRate)) + .build(); + } + + public boolean booking(Integer id, Filter filter, User user) { + if(accommodationMapper.countById(id) == 0) { + throw new EntityNotFoundException(ErrorMessage.ENTITY_NOT_FOUND.getMessage()); + } + + Booking newBooking = new Booking(user, id, filter); + + if (bookingMapper.countByAccommodationIdAndPeriod(newBooking) > 0) { + return false; + } + bookingMapper.insertBooking(newBooking); + return true; + } +}
Java
๋ฉ”์†Œ๋“œ ์ด๋ฆ„์„ ์กฐ๊ธˆ ๋” ์ง๊ด€์ ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ์„ธ์š”. `insertNewBooking` ์ด๋ž„์ง€ `saveNewBooking` ์ด๋ž„์ง€....
@@ -0,0 +1,97 @@ +package com.codesquad.airbnb.business; + +import com.codesquad.airbnb.common.security.GithubToken; +import com.codesquad.airbnb.common.utils.JWTUtils; +import com.codesquad.airbnb.dao.UserMapper; +import com.codesquad.airbnb.domain.model.User; +import com.fasterxml.jackson.databind.JsonNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.*; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +@Service +public class LoginService { + + private static final Logger log = LoggerFactory.getLogger(LoginService.class); + + private UserMapper userMapper; + + @Value("${github.oauth.client.id}") + private String CLIENTID; + + @Value("${github.oauth.client.secret}") + private String CLIENTSECRET; + + @Value("${github.oauth.access_token.uri}") + private String GITHUB_ACCESS_TOKEN_URI; + + @Value("${github.oauth.scope.user.email.uri}") + private String GITHUB_USER_EMAIL_URI; + + private static final String JWT_PAYLOAD = "email"; + + public LoginService(UserMapper userMapper) { + this.userMapper = userMapper; + } + + public String parseJWTFromGithubAccount(String code) { + GithubToken githubToken = getAccessToken(code); + User loginUser = new User(getUserEmailFromOAuthToken(githubToken)); + saveUserIfNotExist(loginUser); + return parseJWTByUser(loginUser); + } + + public User getUserFromJWT(String token) { + User loginUser = new User(JWTUtils.getStringFromJWT(JWT_PAYLOAD, token)); + loginUser = userMapper.findByEmail(loginUser); + + log.debug("logined User: {}", loginUser); + + return loginUser; + } + + private GithubToken getAccessToken(String code) { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + Map<String, String> payload = new HashMap<>(); + payload.put("code", code); + payload.put("client_id", CLIENTID); + payload.put("client_secret", CLIENTSECRET); + + ResponseEntity<GithubToken> response = new RestTemplate().postForEntity(GITHUB_ACCESS_TOKEN_URI, + new HttpEntity<>(payload, headers), + GithubToken.class); + + return response.getBody(); + } + + private String getUserEmailFromOAuthToken(GithubToken githubToken) { + HttpHeaders headers = new HttpHeaders(); + headers.setBearerAuth(githubToken.getAccessToken()); + + ResponseEntity<JsonNode> response = new RestTemplate().exchange(GITHUB_USER_EMAIL_URI, + HttpMethod.GET, + new HttpEntity<>(headers), + JsonNode.class); + + return Objects.requireNonNull(response.getBody()).findValue("email").asText(); + } + + private void saveUserIfNotExist(User loginUser) { + if (userMapper.findByEmail(loginUser) == null) { + userMapper.insertUser(loginUser); + } + } + + private String parseJWTByUser(User user) { + return JWTUtils.createTokenByString(JWT_PAYLOAD, user.getEmail()); + } +}
Java
์ƒ์ˆ˜์˜ ์„ฑ๊ฒฉ์„ ์ง€๋‹ˆ๊ณ  ์žˆ๊ธด ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„  ๋ถ„๋ช…ํžˆ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์ด๋ฏ€๋กœ, ์นด๋ฉœ ์ผ€์ด์Šค๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋งž์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,6 @@ +package com.codesquad.airbnb.common.utils; + +public class CurrencyConvertor { + + public static final float EXCHANGE_RATE_FROM_USD_TO_KRW = 1231.54f; +}
Java
ํ˜ธ์˜ค์˜ค.... ํ™˜์œจ์ด ์–ธ์ œ ์ด๋ ‡๊ฒŒ ์˜ฌ๋ž์ฃ 
@@ -0,0 +1,13 @@ +package com.codesquad.airbnb.common.utils; + +import org.springframework.validation.BindingResult; +import org.springframework.validation.ObjectError; + +import java.util.stream.Collectors; + +public class CustomValidatorUtils { + + public static String getErrorMessage(BindingResult result) { + return result.getAllErrors().stream().map(ObjectError::getCode).collect(Collectors.joining(", ")); + } +}
Java
`Collectors.joining` ํ™œ์šฉ ์ข‹์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,16 @@ +package com.codesquad.airbnb.domain.model; + +public class City { + + private Integer id; + + private String name; + + private String country; + + public City() {} + + public String getName() { + return name; + } +}
Java
๊ธฐ๋ณธ์ƒ์„ฑ์ž ์‚ฌ์šฉ ์ข‹์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ฐ ํ•„๋“œ๋“ค์„ ์ดˆ๊ธฐํ™”ํ•ด์ค„ ํ•„์š”๋Š” ์ „ํ˜€ ์—†๋‚˜์š”?
@@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="DataSourceManagerImpl" format="xml" multifile-model="true"> + <data-source source="LOCAL" name="baseball-10" uuid="873ec135-86e5-491d-a981-98740f2170c3"> + <driver-ref>mysql</driver-ref> + <synchronize>true</synchronize> + <jdbc-driver>com.mysql.jdbc.Driver</jdbc-driver> + <jdbc-url>jdbc:mysql://baseball-10-proeject.csjhbz8c8oyh.ap-northeast-2.rds.amazonaws.com:3306</jdbc-url> + </data-source> + <data-source source="LOCAL" name="baseball-10-real" uuid="24139289-bb1d-4da5-b58f-bd3fb57713eb"> + <driver-ref>mysql.8</driver-ref> + <synchronize>true</synchronize> + <jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver> + <jdbc-url>jdbc:mysql://baseball-10-rds.csjhbz8c8oyh.ap-northeast-2.rds.amazonaws.com:3306</jdbc-url> + </data-source> + </component> +</project> \ No newline at end of file
Unknown
์ธํ…”๋ฆฌ์ œ์ด Datasource ๊ธฐ๋Šฅ ์‚ฌ์šฉํ•ด๋ณด์‹  ๊ฒƒ ์ข‹๋„ค์š”.
@@ -0,0 +1,21 @@ +# Getting Started + +### Reference Documentation +For further reference, please consider the following sections: + +* [Official Gradle documentation](https://docs.gradle.org) +* [Spring Boot Gradle Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.2.6.RELEASE/gradle-plugin/reference/html/) +* [Spring Web](https://docs.spring.io/spring-boot/docs/2.2.6.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications) + +### Guides +The following guides illustrate how to use some features concretely: + +* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/) +* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/) +* [Building REST services with Spring](https://spring.io/guides/tutorials/bookmarks/) + +### Additional Links +These additional references should also help you: + +* [Gradle Build Scans โ€“ insights for your project's build](https://scans.gradle.com#gradle) +
Unknown
๊ธฐ๋ณธ ์ƒ์„ฑ๋œ ๋งˆํฌ๋‹ค์šด ํŒŒ์ผ์€ ๊ณผ๊ฐํžˆ ๋‚ ๋ ค์ฃผ์„ธ์š”.
@@ -0,0 +1,65 @@ +import InputView from './view/InputView.js'; +import OutputView from './view/OutputView.js'; +import Service from './Service.js'; +import { print } from './utils/missionUtils.js'; + +class Controller { + constructor() { + this.service = new Service(); + } + + async run() { + await this.inputControl(); + + const totalPrice = this.service.totalOrderPrice(); + const BenefitDetails = this.service.BenefitsDetails(); + const totalBenefitPrice = this.service.totalBenefitsPrice(); + const badge = this.service.calculateBadge(); + + OutputView.printTotalPriceAndGift(totalPrice); + OutputView.printBenefits(totalPrice, BenefitDetails, totalBenefitPrice); + OutputView.printTotalBenefitAmount(totalPrice, totalBenefitPrice); + OutputView.printEstimatedAmount(this.service.calculatePayment()); + OutputView.printBadge(badge); + } + + async inputControl() { + OutputView.printWelcome(); + const date = await this.#inputDate(); + const order = await this.#inputOrder(); + OutputView.printGuide(date); + OutputView.printMenu(order); + } + + async #inputDate() { + let date; + + while (!date) { + try { + const getDate = await InputView.readDate(); + date = this.service.benefitsCreate(getDate); + } catch (error) { + print(error.message); + } + } + + return date; + } + + async #inputOrder() { + let order; + + while (!order) { + try { + const getOrder = await InputView.readMenu(); + order = this.service.orderMenuCreate(getOrder); + } catch (error) { + print(error.message); + } + } + + return order; + } +} + +export default Controller;
JavaScript
์ ‘๊ทผ์ œํ•œ์„ ๋‘์ง€ ์•Š์œผ์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”?
@@ -0,0 +1,113 @@ +import Benefits from './domain/Benefits.js'; +import OrderMenu from './domain/OrderMenu.js'; +import { + BENEFITS_THRESHOLD, + GIFT_THRESHOLD, + MENU_PRICE, +} from './utils/constants.js'; + +const BADGE = { + ์‚ฐํƒ€: 20000, + ํŠธ๋ฆฌ: 10000, + ๋ณ„: 5000, +}; + +class Service { + #benefits; + + #orderMenu; + + benefitsCreate(date) { + this.#benefits = new Benefits(date); + return date; + } + + orderMenuCreate(order) { + this.#orderMenu = new OrderMenu(order); + return order; + } + + totalOrderPrice() { + const price = this.#orderMenu.totalOrderAmount(); + return price; + } + + // ํ˜œํƒ ๋‚ด์—ญ + BenefitsDetails() { + const dessertCount = this.#orderMenu.dessertCount(); + const mainCount = this.#orderMenu.mainCount(); + const benefitsArray = [ + [ + this.#benefits.christmasDiscount(), + this.#benefits.weekdayDiscount(dessertCount), + this.#benefits.weekendDiscount(mainCount), + this.#benefits.specialDiscount(), + ], + [this.#giftEvent()], + ]; + + return benefitsArray; + } + + // ์ฆ์ • ์ด๋ฒคํŠธ + #giftEvent() { + let giftDiscount = 0; + + if (this.totalOrderPrice() >= GIFT_THRESHOLD) { + giftDiscount += MENU_PRICE.์ƒดํŽ˜์ธ; + } + return ['์ฆ์ • ์ด๋ฒคํŠธ', giftDiscount]; + } + + // ์ด ํ˜œํƒ๊ธˆ์•ก + totalBenefitsPrice() { + const BenefitsDetails = this.BenefitsDetails(); + + const totalBenefitsPrice = BenefitsDetails.reduce( + (acc, innerArray) => + acc + + innerArray.reduce( + (total, [, discountAmount]) => total + discountAmount, + 0, + ), + 0, + ); + + return totalBenefitsPrice; + } + + // ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก + calculatePayment() { + const totalPrice = this.totalOrderPrice(); + const totalBenefitPrice = this.totalBenefitsPrice(); + + if (totalPrice < BENEFITS_THRESHOLD) { + return totalPrice; + } + + return this.calculateEstimatedAmount(totalPrice, totalBenefitPrice); + } + + calculateEstimatedAmount(totalPrice, totalBenefitPrice) { + if (totalPrice >= GIFT_THRESHOLD) { + const benefitPrice = totalBenefitPrice - MENU_PRICE.์ƒดํŽ˜์ธ; + const payment = totalPrice - benefitPrice; + return payment; + } + + const payment = totalPrice - totalBenefitPrice; + return payment; + } + + calculateBadge() { + const totalBenefitPrice = this.totalBenefitsPrice(); + + const badge = Object.keys(BADGE).find( + (key) => totalBenefitPrice >= BADGE[key], + ); + + return badge; + } +} + +export default Service;
JavaScript
innerArray๋ณด๋‹ค ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ๋ฌด์Šจ ๊ฐ’์„ ์˜๋ฏธํ•˜๋Š”์ง€ ๋‚˜ํƒ€๋‚ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,8 @@ +import { MissionUtils } from '@woowacourse/mission-utils'; + +export const readLineAsync = async (query) => { + const input = await MissionUtils.Console.readLineAsync(query); + return input; +}; + +export const print = (message) => MissionUtils.Console.print(message);
JavaScript
๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น์œผ๋กœ Console๋กœ๋งŒ ์ฒ˜๋ฆฌํ•ด๋„ ๋˜์ง€ ์•Š๋‚˜์š”?
@@ -0,0 +1,104 @@ +import { print } from '../utils/missionUtils.js'; +import { + GIFT_THRESHOLD, + BENEFITS_THRESHOLD, + MENU_PRICE, +} from '../utils/constants.js'; + +const OutputView = { + printWelcome() { + print('์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.'); + }, + + printGuide(date) { + print(`12์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!`); + }, + + printMenu(order) { + const dividedOrder = order.split(','); + const orderArray = dividedOrder.map((value) => { + const [menuName, count] = value.split('-'); + return [menuName, count]; + }); + + print('\n<์ฃผ๋ฌธ ๋ฉ”๋‰ด>'); + orderArray.forEach((item) => { + const [menuName, count] = item; + print(`${menuName} ${count}๊ฐœ`); + }); + }, + + printTotalPriceAndGift(price) { + const showPrice = price.toLocaleString(); + + print('\n<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>'); + print(`${showPrice}์›`); + print('\n<์ฆ์ • ๋ฉ”๋‰ด>'); + + if (price >= GIFT_THRESHOLD) { + print('์ƒดํŽ˜์ธ 1๊ฐœ'); + } else { + print('์—†์Œ'); + } + }, + + printBenefits(price, benefitsArray, totalBenefitPrice) { + print('\n<ํ˜œํƒ ๋‚ด์—ญ>'); + + if (price < BENEFITS_THRESHOLD) { + print('์—†์Œ'); + return; + } + + this.dateBenefits(benefitsArray); + + if (totalBenefitPrice === 0) { + print('์—†์Œ'); + } + }, + + dateBenefits(benefitsArray) { + benefitsArray.forEach((dateBenefits) => { + dateBenefits.forEach((giftBenefit) => { + const [eventName, discount] = giftBenefit; + const showDiscount = discount.toLocaleString(); + if (discount !== 0) { + print(`${eventName}: -${showDiscount}์›`); + } + }); + }); + }, + + printTotalBenefitAmount(totalPrice, totalBenefitPrice) { + const showTotalDiscount = totalBenefitPrice.toLocaleString(); + + print('\n<์ดํ˜œํƒ ๊ธˆ์•ก>'); + + if (totalBenefitPrice === 0 || totalPrice < BENEFITS_THRESHOLD) { + print('0์›'); + } + + if (totalBenefitPrice !== 0 && totalPrice >= BENEFITS_THRESHOLD) { + print(`-${showTotalDiscount}์›`); + } + }, + + printEstimatedAmount(payment) { + const estimatedAmount = payment.toLocaleString(); + + print('\n<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>'); + print(estimatedAmount); + }, + + printBadge(calculateBadge) { + print('\n<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>'); + + if (calculateBadge === undefined) { + print('์—†์Œ'); + return; + } + print(calculateBadge); + }, +}; + +export default OutputView;
JavaScript
๋ณ€์ˆ˜๋ช…์— ์ž๋ฃŒํ˜•์„ ๋„ฃ๋Š” ๊ฒŒ ์ข‹์ง€ ์•Š๋‹ค๊ณ  ๋“ค์—ˆ์–ด์š”. orders์™€ ๊ฐ™์ด ๋ณต์ˆ˜ํ‘œํ˜„์œผ๋กœ ๋ฐ”๊พธ์–ด ์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,115 @@ +import OrderMenu from '../domain/OrderMenu.js'; + +describe('์ฃผ๋ฌธ ๋ฉ”๋‰ด ๊ฒ€์ฆ ํ…Œ์ŠคํŠธ', () => { + const orderMenuTestCase = [ + { + testName: '์ฃผ๋ฌธ ํ˜•์‹์ด ์˜ˆ์‹œ์™€ ๋‹ค๋ฅด๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.', + input: '์–‘์†ก์ด์ˆ˜ํ”„ 1๊ฐœ, ์ดˆ์ฝ”์ผ€์ดํฌ 2๊ฐœ', + }, + { + testName: '๋ฉ”๋‰ดํŒ์— ์—†๋Š” ๋ฉ”๋‰ด๊ฐ€ ์žˆ์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.', + input: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,๋งˆ๋ผํƒ•-1', + }, + { + testName: '์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์ด 1 ๋ฏธ๋งŒ์ด๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.', + input: 'ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-0,์ œ๋กœ์ฝœ๋ผ-1', + }, + { + testName: '์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์˜ ์ดํ•ฉ์ด 20์ด ๋„˜์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.', + input: '์‹œ์ €์ƒ๋Ÿฌ๋“œ-14,๋ฐ”๋น„ํ๋ฆฝ-5,ํƒ€ํŒŒ์Šค-2', + }, + { + testName: '์ค‘๋ณต๋œ ๋ฉ”๋‰ด๊ฐ€ ์žˆ์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.', + input: '๋ฐ”๋น„ํ๋ฆฝ-2,๋ ˆ๋“œ์™€์ธ-1,๋ฐ”๋น„ํ๋ฆฝ-1', + }, + ]; + + orderMenuTestCase.forEach((testCase) => { + test(testCase.testName, () => { + expect(() => { + new OrderMenu(testCase.input); + }).toThrow('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + }); + }); + + test('์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.', () => { + expect(() => { + new OrderMenu('์ œ๋กœ์ฝœ๋ผ-5,๋ ˆ๋“œ์™€์ธ-2,์ƒดํŽ˜์ธ-3'); + }).toThrow('[ERROR] ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + }); +}); + +describe('์ƒํƒœ๊ฐ’ ํ™œ์šฉ ํ…Œ์ŠคํŠธ', () => { + test('์ˆ˜๋Ÿ‰๊ณผ ๊ฐ€๊ฒฉ์„ ๊ณฑํ•ด์„œ ์ด ์ฃผ๋ฌธ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•œ๋‹ค.', () => { + const orderMenuTestCase = [ + { + input: 'ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-3,์–‘์†ก์ด์ˆ˜ํ”„-1,ํƒ€ํŒŒ์Šค-2,์ œ๋กœ์ฝœ๋ผ-4', + result: 134000, + }, + { + input: '๋ฐ”๋น„ํ๋ฆฝ-5,์‹œ์ €์ƒ๋Ÿฌ๋“œ-3,๋ ˆ๋“œ์™€์ธ-2,์•„์ด์Šคํฌ๋ฆผ-5', + result: 439000, + }, + { + input: + 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-2,ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1,ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€-10,์ดˆ์ฝ”์ผ€์ดํฌ-3,์ƒดํŽ˜์ธ-4', + result: 540000, + }, + ]; + + orderMenuTestCase.forEach((testCase) => { + // given + const orderMenu = new OrderMenu(testCase.input); + + // when + const received = orderMenu.totalOrderAmount(); + + // then + expect(received).toBe(testCase.result); + }); + }); + + test('๋””์ €ํŠธ ๋ฉ”๋‰ด์˜ ์ฃผ๋ฌธ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.', () => { + const orderMenuTestCase = [ + { input: '๋ฐ”๋น„ํ๋ฆฝ-1,์ดˆ์ฝ”์ผ€์ดํฌ-9,์ œ๋กœ์ฝœ๋ผ-2,์•„์ด์Šคํฌ๋ฆผ-3', result: 12 }, + { input: 'ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€-3,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-5,๋ ˆ๋“œ์™€์ธ-1', result: 0 }, + { + input: '์–‘์†ก์ด์ˆ˜ํ”„-2,์‹œ์ €์ƒ๋Ÿฌ๋“œ-5,ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-3,์ดˆ์ฝ”์ผ€์ดํฌ-1', + result: 1, + }, + ]; + + orderMenuTestCase.forEach((testCase) => { + // given + const orderMenu = new OrderMenu(testCase.input); + + // when + const received = orderMenu.dessertCount(); + + // then + expect(received).toBe(testCase.result); + }); + }); + + test('๋ฉ”์ธ ๋ฉ”๋‰ด์˜ ์ฃผ๋ฌธ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.', () => { + const orderMenuTestCase = [ + { + input: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-2,์–‘์†ก์ด์ˆ˜ํ”„-1,์‹œ์ €์ƒ๋Ÿฌ๋“œ-1,ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-4', + result: 6, + }, + { input: 'ํƒ€ํŒŒ์Šค-7,ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€-4,๋ ˆ๋“œ์™€์ธ-1', result: 4 }, + { input: '์–‘์†ก์ด์ˆ˜ํ”„-2,์ œ๋กœ์ฝœ๋ผ-4,์ดˆ์ฝ”์ผ€์ดํฌ-13', result: 0 }, + ]; + + orderMenuTestCase.forEach((testCase) => { + // given + const orderMenu = new OrderMenu(testCase.input); + + // when + const received = orderMenu.mainCount(); + + // then + expect(received).toBe(testCase.result); + }); + }); +});
JavaScript
test.each()๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”?
@@ -0,0 +1,126 @@ +import { MENU_PRICE } from '../utils/constants.js'; + +const MENU_TYPES = Object.freeze({ + appetizer: ['์–‘์†ก์ด์ˆ˜ํ”„', 'ํƒ€ํŒŒ์Šค', '์‹œ์ €์ƒ๋Ÿฌ๋“œ'], + main: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', '๋ฐ”๋น„ํ๋ฆฝ', 'ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€', 'ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€'], + dessert: ['์ดˆ์ฝ”์ผ€์ดํฌ', '์•„์ด์Šคํฌ๋ฆผ'], + drink: ['์ œ๋กœ์ฝœ๋ผ', '๋ ˆ๋“œ์™€์ธ', '์ƒดํŽ˜์ธ'], +}); + +const LIMIT_NUMBER = Object.freeze({ + minNumber: 1, + maxNumber: 20, +}); + +class OrderMenu { + #orderMenu; + + constructor(order) { + this.#validateFormat(order); // ์ž…๋ ฅ ํ˜•์‹ ๊ฒ€์ฆ + this.#arrayMenu(order); // ๊ฐ€๊ฒฉ๊ณผ ํ•จ๊ป˜ ๋ฐฐ์—ด ๊ฐ์ฒด ์ €์žฅ + this.#validateMenuItems(); // ๋ฉ”๋‰ด ์œ ๋ฌด ๊ฒ€์ฆ + this.#validateCount(); // ์ฃผ๋ฌธ ๊ฐœ์ˆ˜ ๊ฒ€์ฆ + this.#validateTotalCount(); // ์ฃผ๋ฌธ ์ด๊ฐœ์ˆ˜ ๊ฒ€์ฆ + this.#validateDuplicateMenu(); // ๋ฉ”๋‰ด ์ค‘๋ณต ๊ฒ€์ฆ + this.#validateOnlyDrink(); // ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ๊ฒ€์ฆ + } + + #arrayMenu(order) { + const menuItem = order.split(','); + this.#orderMenu = menuItem.map((item) => { + const [menuName, count] = item.split('-'); + const price = MENU_PRICE[menuName] || 0; + const type = Object.keys(MENU_TYPES).find((category) => + MENU_TYPES[category].includes(menuName), + ); + return { menuName, count: Number(count), price, type }; + }); + } + + #validateFormat(order) { + const regex = /^([๊ฐ€-ํžฃ]+-\d+,)*[๊ฐ€-ํžฃ]+-\d+$/; + const testResult = regex.test(order); + + if (testResult === false) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateMenuItems() { + const result = this.#orderMenu.some((item) => item.price === 0); + + if (result) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateCount() { + this.#orderMenu.forEach((menu) => { + if (menu.count < LIMIT_NUMBER.minNumber) { + throw new Error( + '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + ); + } + }); + } + + #validateTotalCount() { + const totalCount = this.#orderMenu.reduce( + (sum, item) => sum + item.count, + 0, + ); + + if (totalCount > LIMIT_NUMBER.maxNumber) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateDuplicateMenu() { + const menuNameSet = new Set(this.#orderMenu.map((item) => item.menuName)); + + if (menuNameSet.size !== this.#orderMenu.length) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateOnlyDrink() { + const drinkMenu = MENU_TYPES.drink; + const result = this.#orderMenu.every((item) => + drinkMenu.includes(item.menuName), + ); + + if (result) { + throw new Error('[ERROR] ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + // ์ด ์ฃผ๋ฌธ๊ธˆ์•ก + totalOrderAmount() { + const totalAmount = this.#orderMenu.reduce( + (sum, item) => sum + item.price * item.count, + 0, + ); + + return totalAmount; + } + + // ๋””์ €ํŠธ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ + dessertCount() { + const dessertMenu = this.#orderMenu.filter( + (item) => item.type === 'dessert', + ); + const count = dessertMenu.reduce((total, item) => total + item.count, 0); + + return count; + } + + // ๋ฉ”์ธ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ + mainCount() { + const mainMenu = this.#orderMenu.filter((item) => item.type === 'main'); + const count = mainMenu.reduce((total, item) => total + item.count, 0); + + return count; + } +} + +export default OrderMenu;
JavaScript
์ด ๋ถ€๋ถ„๋„ ์—ฌ๊ธฐ์— ๋‘์‹  ์ด์œ ๊ฐ€ ๋”ฐ๋กœ ์žˆ์œผ์‹ ์ง€ ๊ถ๊ธˆํ•˜๋„ค์š”..!
@@ -0,0 +1,88 @@ +const DATE = Object.freeze({ + eventEndDay: 31, + christmasEndDay: 25, +}); + +const WEEKDAY = Object.freeze([ + 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, +]); + +const WEEKEND = Object.freeze([1, 2, 8, 9, 15, 16, 22, 23, 29, 30]); + +const SPECIAL = Object.freeze([3, 10, 17, 24, 25, 31]); + +class Benefits { + #date; + + constructor(date) { + this.#validateNumber(date); // ์ž์—ฐ์ˆ˜ ๊ฒ€์ฆ + this.#date = Number(date); + this.#validateDate(); // ๋‚ ์งœ ๋ฒ”์œ„ ๊ฒ€์ฆ + } + + #validateNumber(date) { + const regex = /^[1-9]\d*$/; + const testResult = regex.test(date); + + if (testResult === false) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateDate() { + if (this.#date > DATE.eventEndDay) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + // ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ + christmasDiscount() { + const START_PRICE = 1000; + const INCREASE_PRICE = 100; + let discountedAmount = 0; + + if (this.#date <= DATE.christmasEndDay) { + discountedAmount = START_PRICE + (this.#date - 1) * INCREASE_PRICE; + } + + return ['ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', discountedAmount]; + } + + // ํ‰์ผ ํ• ์ธ + weekdayDiscount(dessertCount) { + const WEEKDAY_DISCOUNT = 2023; + let discountedAmount = 0; + + if (WEEKDAY.includes(this.#date)) { + discountedAmount = WEEKDAY_DISCOUNT * dessertCount; + } + + return ['ํ‰์ผ ํ• ์ธ', discountedAmount]; + } + + // ์ฃผ๋ง ํ• ์ธ + weekendDiscount(mainCount) { + const WEEKEND_DISCOUNT = 2023; + let discountedAmount = 0; + + if (WEEKEND.includes(this.#date)) { + discountedAmount = WEEKEND_DISCOUNT * mainCount; + } + + return ['์ฃผ๋ง ํ• ์ธ', discountedAmount]; + } + + // ํŠน๋ณ„ ํ• ์ธ + specialDiscount() { + const DISCOUNT_PRICE = 1000; + let discountedAmount = 0; + + if (SPECIAL.includes(this.#date)) { + discountedAmount = DISCOUNT_PRICE; + } + + return ['ํŠน๋ณ„ ํ• ์ธ', discountedAmount]; + } +} + +export default Benefits;
JavaScript
DATE.eventEndDay ์ƒ์ˆ˜์ฒ˜๋ฆฌํ™”ํ•˜์‹  ๋ถ€๋ถ„์ด ์ธ์ƒ๊นŠ๋„ค์š”:)
@@ -0,0 +1,88 @@ +const DATE = Object.freeze({ + eventEndDay: 31, + christmasEndDay: 25, +}); + +const WEEKDAY = Object.freeze([ + 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, +]); + +const WEEKEND = Object.freeze([1, 2, 8, 9, 15, 16, 22, 23, 29, 30]); + +const SPECIAL = Object.freeze([3, 10, 17, 24, 25, 31]); + +class Benefits { + #date; + + constructor(date) { + this.#validateNumber(date); // ์ž์—ฐ์ˆ˜ ๊ฒ€์ฆ + this.#date = Number(date); + this.#validateDate(); // ๋‚ ์งœ ๋ฒ”์œ„ ๊ฒ€์ฆ + } + + #validateNumber(date) { + const regex = /^[1-9]\d*$/; + const testResult = regex.test(date); + + if (testResult === false) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateDate() { + if (this.#date > DATE.eventEndDay) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + // ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ + christmasDiscount() { + const START_PRICE = 1000; + const INCREASE_PRICE = 100; + let discountedAmount = 0; + + if (this.#date <= DATE.christmasEndDay) { + discountedAmount = START_PRICE + (this.#date - 1) * INCREASE_PRICE; + } + + return ['ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', discountedAmount]; + } + + // ํ‰์ผ ํ• ์ธ + weekdayDiscount(dessertCount) { + const WEEKDAY_DISCOUNT = 2023; + let discountedAmount = 0; + + if (WEEKDAY.includes(this.#date)) { + discountedAmount = WEEKDAY_DISCOUNT * dessertCount; + } + + return ['ํ‰์ผ ํ• ์ธ', discountedAmount]; + } + + // ์ฃผ๋ง ํ• ์ธ + weekendDiscount(mainCount) { + const WEEKEND_DISCOUNT = 2023; + let discountedAmount = 0; + + if (WEEKEND.includes(this.#date)) { + discountedAmount = WEEKEND_DISCOUNT * mainCount; + } + + return ['์ฃผ๋ง ํ• ์ธ', discountedAmount]; + } + + // ํŠน๋ณ„ ํ• ์ธ + specialDiscount() { + const DISCOUNT_PRICE = 1000; + let discountedAmount = 0; + + if (SPECIAL.includes(this.#date)) { + discountedAmount = DISCOUNT_PRICE; + } + + return ['ํŠน๋ณ„ ํ• ์ธ', discountedAmount]; + } +} + +export default Benefits;
JavaScript
ํ‰์ผ์— ๋Œ€ํ•œ ๋‚ ์งœ๋ฅผ ์ง์ ‘ ์ž‘์„ฑ ๋ฐ ์ƒ์ˆ˜์ฒ˜๋ฆฌํ•˜์…”์„œ WEEKDAY ๊ฐ์ฒด์•ˆ์— ํ•ด๋‹น ๋‚ ์งœ๊ฐ€ ํฌํ•จ๋˜๋Š” ์ง€๋ฅผ ๋น„๊ตํ•œ ๋กœ์ง์œผ๋กœ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ 2023๋…„ 12์›”์ด๋ผ๋Š” ๋ฒ”์œ„์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ์ด ๋œ๋‹ค๋ฉด (2024๋…„ 1์›”์˜ ํ‰์ผ,2024๋…„ 4์›”์˜ ํ‰์ผ ๋“ฑ) ๋งค๋ฒˆ ๋‚ ์งœ๋ฅผ ํ‰์ผ์ธ์ง€ ํ™•์ธํ•ด ์ง์ ‘ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•จ์œผ๋กœ ํ™•์žฅ์„ฑ์— ์–ด๋ ค์›€์ด ์ƒ๊ธธ ๊ฒƒ ๊ฐ™์•„์š”. js ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋‚ด์žฅ ๊ฐ์ฒด Date ๋ฅผ ํ™œ์šฉํ•ด ํ‰์ผ์„ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฑด ์–ด๋– ์‹ ๊ฐ€์š”?
@@ -0,0 +1,88 @@ +const DATE = Object.freeze({ + eventEndDay: 31, + christmasEndDay: 25, +}); + +const WEEKDAY = Object.freeze([ + 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, +]); + +const WEEKEND = Object.freeze([1, 2, 8, 9, 15, 16, 22, 23, 29, 30]); + +const SPECIAL = Object.freeze([3, 10, 17, 24, 25, 31]); + +class Benefits { + #date; + + constructor(date) { + this.#validateNumber(date); // ์ž์—ฐ์ˆ˜ ๊ฒ€์ฆ + this.#date = Number(date); + this.#validateDate(); // ๋‚ ์งœ ๋ฒ”์œ„ ๊ฒ€์ฆ + } + + #validateNumber(date) { + const regex = /^[1-9]\d*$/; + const testResult = regex.test(date); + + if (testResult === false) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateDate() { + if (this.#date > DATE.eventEndDay) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + // ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ + christmasDiscount() { + const START_PRICE = 1000; + const INCREASE_PRICE = 100; + let discountedAmount = 0; + + if (this.#date <= DATE.christmasEndDay) { + discountedAmount = START_PRICE + (this.#date - 1) * INCREASE_PRICE; + } + + return ['ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', discountedAmount]; + } + + // ํ‰์ผ ํ• ์ธ + weekdayDiscount(dessertCount) { + const WEEKDAY_DISCOUNT = 2023; + let discountedAmount = 0; + + if (WEEKDAY.includes(this.#date)) { + discountedAmount = WEEKDAY_DISCOUNT * dessertCount; + } + + return ['ํ‰์ผ ํ• ์ธ', discountedAmount]; + } + + // ์ฃผ๋ง ํ• ์ธ + weekendDiscount(mainCount) { + const WEEKEND_DISCOUNT = 2023; + let discountedAmount = 0; + + if (WEEKEND.includes(this.#date)) { + discountedAmount = WEEKEND_DISCOUNT * mainCount; + } + + return ['์ฃผ๋ง ํ• ์ธ', discountedAmount]; + } + + // ํŠน๋ณ„ ํ• ์ธ + specialDiscount() { + const DISCOUNT_PRICE = 1000; + let discountedAmount = 0; + + if (SPECIAL.includes(this.#date)) { + discountedAmount = DISCOUNT_PRICE; + } + + return ['ํŠน๋ณ„ ํ• ์ธ', discountedAmount]; + } +} + +export default Benefits;
JavaScript
๋กœ์ง์˜ ์ผ๊ด€์„ฑ์ด ๋†’์•„ ์ฝ๊ธฐ ๋งค์šฐ ํŽธํ•˜๋„ค์š”!
@@ -0,0 +1,126 @@ +import { MENU_PRICE } from '../utils/constants.js'; + +const MENU_TYPES = Object.freeze({ + appetizer: ['์–‘์†ก์ด์ˆ˜ํ”„', 'ํƒ€ํŒŒ์Šค', '์‹œ์ €์ƒ๋Ÿฌ๋“œ'], + main: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', '๋ฐ”๋น„ํ๋ฆฝ', 'ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€', 'ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€'], + dessert: ['์ดˆ์ฝ”์ผ€์ดํฌ', '์•„์ด์Šคํฌ๋ฆผ'], + drink: ['์ œ๋กœ์ฝœ๋ผ', '๋ ˆ๋“œ์™€์ธ', '์ƒดํŽ˜์ธ'], +}); + +const LIMIT_NUMBER = Object.freeze({ + minNumber: 1, + maxNumber: 20, +}); + +class OrderMenu { + #orderMenu; + + constructor(order) { + this.#validateFormat(order); // ์ž…๋ ฅ ํ˜•์‹ ๊ฒ€์ฆ + this.#arrayMenu(order); // ๊ฐ€๊ฒฉ๊ณผ ํ•จ๊ป˜ ๋ฐฐ์—ด ๊ฐ์ฒด ์ €์žฅ + this.#validateMenuItems(); // ๋ฉ”๋‰ด ์œ ๋ฌด ๊ฒ€์ฆ + this.#validateCount(); // ์ฃผ๋ฌธ ๊ฐœ์ˆ˜ ๊ฒ€์ฆ + this.#validateTotalCount(); // ์ฃผ๋ฌธ ์ด๊ฐœ์ˆ˜ ๊ฒ€์ฆ + this.#validateDuplicateMenu(); // ๋ฉ”๋‰ด ์ค‘๋ณต ๊ฒ€์ฆ + this.#validateOnlyDrink(); // ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ๊ฒ€์ฆ + } + + #arrayMenu(order) { + const menuItem = order.split(','); + this.#orderMenu = menuItem.map((item) => { + const [menuName, count] = item.split('-'); + const price = MENU_PRICE[menuName] || 0; + const type = Object.keys(MENU_TYPES).find((category) => + MENU_TYPES[category].includes(menuName), + ); + return { menuName, count: Number(count), price, type }; + }); + } + + #validateFormat(order) { + const regex = /^([๊ฐ€-ํžฃ]+-\d+,)*[๊ฐ€-ํžฃ]+-\d+$/; + const testResult = regex.test(order); + + if (testResult === false) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateMenuItems() { + const result = this.#orderMenu.some((item) => item.price === 0); + + if (result) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateCount() { + this.#orderMenu.forEach((menu) => { + if (menu.count < LIMIT_NUMBER.minNumber) { + throw new Error( + '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + ); + } + }); + } + + #validateTotalCount() { + const totalCount = this.#orderMenu.reduce( + (sum, item) => sum + item.count, + 0, + ); + + if (totalCount > LIMIT_NUMBER.maxNumber) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateDuplicateMenu() { + const menuNameSet = new Set(this.#orderMenu.map((item) => item.menuName)); + + if (menuNameSet.size !== this.#orderMenu.length) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateOnlyDrink() { + const drinkMenu = MENU_TYPES.drink; + const result = this.#orderMenu.every((item) => + drinkMenu.includes(item.menuName), + ); + + if (result) { + throw new Error('[ERROR] ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + // ์ด ์ฃผ๋ฌธ๊ธˆ์•ก + totalOrderAmount() { + const totalAmount = this.#orderMenu.reduce( + (sum, item) => sum + item.price * item.count, + 0, + ); + + return totalAmount; + } + + // ๋””์ €ํŠธ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ + dessertCount() { + const dessertMenu = this.#orderMenu.filter( + (item) => item.type === 'dessert', + ); + const count = dessertMenu.reduce((total, item) => total + item.count, 0); + + return count; + } + + // ๋ฉ”์ธ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ + mainCount() { + const mainMenu = this.#orderMenu.filter((item) => item.type === 'main'); + const count = mainMenu.reduce((total, item) => total + item.count, 0); + + return count; + } +} + +export default OrderMenu;
JavaScript
ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ํด๋ก  ๋ฐ›๊ธฐ ์–ด๋ ค์šด ์ƒํƒœ๋ผ ์ •ํ™•ํžˆ ํ™•์ธํ•˜๊ธฐ ์–ด๋ ค์›Œ ์กฐ์‹ฌ์Šค๋Ÿฝ๊ฒŒ ์—ฌ์ญค๋ณด๋„ค์š” ใ…  ํ˜น์‹œ ํ•ด๋‹น ์ •๊ทœ์‹์ด "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-3" ์ž…๋ ฅ๋ฐ›์•„๋„ ํ†ต๊ณผ๋˜๋‚˜์š”?
@@ -0,0 +1,126 @@ +import { MENU_PRICE } from '../utils/constants.js'; + +const MENU_TYPES = Object.freeze({ + appetizer: ['์–‘์†ก์ด์ˆ˜ํ”„', 'ํƒ€ํŒŒ์Šค', '์‹œ์ €์ƒ๋Ÿฌ๋“œ'], + main: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', '๋ฐ”๋น„ํ๋ฆฝ', 'ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€', 'ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€'], + dessert: ['์ดˆ์ฝ”์ผ€์ดํฌ', '์•„์ด์Šคํฌ๋ฆผ'], + drink: ['์ œ๋กœ์ฝœ๋ผ', '๋ ˆ๋“œ์™€์ธ', '์ƒดํŽ˜์ธ'], +}); + +const LIMIT_NUMBER = Object.freeze({ + minNumber: 1, + maxNumber: 20, +}); + +class OrderMenu { + #orderMenu; + + constructor(order) { + this.#validateFormat(order); // ์ž…๋ ฅ ํ˜•์‹ ๊ฒ€์ฆ + this.#arrayMenu(order); // ๊ฐ€๊ฒฉ๊ณผ ํ•จ๊ป˜ ๋ฐฐ์—ด ๊ฐ์ฒด ์ €์žฅ + this.#validateMenuItems(); // ๋ฉ”๋‰ด ์œ ๋ฌด ๊ฒ€์ฆ + this.#validateCount(); // ์ฃผ๋ฌธ ๊ฐœ์ˆ˜ ๊ฒ€์ฆ + this.#validateTotalCount(); // ์ฃผ๋ฌธ ์ด๊ฐœ์ˆ˜ ๊ฒ€์ฆ + this.#validateDuplicateMenu(); // ๋ฉ”๋‰ด ์ค‘๋ณต ๊ฒ€์ฆ + this.#validateOnlyDrink(); // ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ๊ฒ€์ฆ + } + + #arrayMenu(order) { + const menuItem = order.split(','); + this.#orderMenu = menuItem.map((item) => { + const [menuName, count] = item.split('-'); + const price = MENU_PRICE[menuName] || 0; + const type = Object.keys(MENU_TYPES).find((category) => + MENU_TYPES[category].includes(menuName), + ); + return { menuName, count: Number(count), price, type }; + }); + } + + #validateFormat(order) { + const regex = /^([๊ฐ€-ํžฃ]+-\d+,)*[๊ฐ€-ํžฃ]+-\d+$/; + const testResult = regex.test(order); + + if (testResult === false) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateMenuItems() { + const result = this.#orderMenu.some((item) => item.price === 0); + + if (result) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateCount() { + this.#orderMenu.forEach((menu) => { + if (menu.count < LIMIT_NUMBER.minNumber) { + throw new Error( + '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + ); + } + }); + } + + #validateTotalCount() { + const totalCount = this.#orderMenu.reduce( + (sum, item) => sum + item.count, + 0, + ); + + if (totalCount > LIMIT_NUMBER.maxNumber) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateDuplicateMenu() { + const menuNameSet = new Set(this.#orderMenu.map((item) => item.menuName)); + + if (menuNameSet.size !== this.#orderMenu.length) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateOnlyDrink() { + const drinkMenu = MENU_TYPES.drink; + const result = this.#orderMenu.every((item) => + drinkMenu.includes(item.menuName), + ); + + if (result) { + throw new Error('[ERROR] ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + // ์ด ์ฃผ๋ฌธ๊ธˆ์•ก + totalOrderAmount() { + const totalAmount = this.#orderMenu.reduce( + (sum, item) => sum + item.price * item.count, + 0, + ); + + return totalAmount; + } + + // ๋””์ €ํŠธ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ + dessertCount() { + const dessertMenu = this.#orderMenu.filter( + (item) => item.type === 'dessert', + ); + const count = dessertMenu.reduce((total, item) => total + item.count, 0); + + return count; + } + + // ๋ฉ”์ธ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ + mainCount() { + const mainMenu = this.#orderMenu.filter((item) => item.type === 'main'); + const count = mainMenu.reduce((total, item) => total + item.count, 0); + + return count; + } +} + +export default OrderMenu;
JavaScript
#orderMenu ์•ˆ์— ๋ฐฐ์—ด์˜ ํ˜•ํƒœ๋กœ ๊ฐ๊ฐ์˜ ๋ฉ”๋‰ด๊ฐ€ { menuName, count: Number(count), price, type } ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋‹ด๊ฒจ์ ธ ์ €์žฅ๋˜๋Š”๊ฒƒ์„ ํ™•์ธ๋ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋ฉ”๋‰ด ๋˜ํ•œ ํด๋ž˜์Šคํ˜•ํƒœ๋กœ ์ €์žฅํ•˜์ง€ ์•Š๊ณ  ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์˜ ํ˜•์‹์œผ๋กœ ์ €์žฅํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•˜๋„ค์š” :)
@@ -0,0 +1,197 @@ +# ๐ŸŽ…๐Ÿป ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ”„๋กœ๋ชจ์…˜ ๐ŸŽ„ + +## ์ง„ํ–‰ ๋ฐฉ์‹ + +### ๐Ÿง‘๐Ÿป ๊ณ ๊ฐ ๐Ÿ‘ฉ๐Ÿป + +- ๋ฐฉ๋ฌธํ•  ๋‚ ์งœ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. +- ์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด ์ด๋ฆ„๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•œ๋‹ค. + +### ๐Ÿšช ์‹๋‹น ๐Ÿด + +1. ์†Œ๊ฐœ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +2. ๋ฐฉ๋ฌธ ๋‚ ์งœ์˜ ์ด๋ฒคํŠธ ํ˜œํƒ ์•ˆ๋‚ด๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +3. ๊ณ ๊ฐ์ด ์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +4. ์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด์˜ ์ด ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. +5. ์ฆ์ • ๋ฉ”๋‰ด์˜ ์—ฌ๋ถ€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +6. ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ํ˜œํƒ ๋‚ด์—ญ์„ ์ถœ๋ ฅํ•œ๋‹ค. +7. ํ˜œํƒ๋‚ด์—ญ์„ ๋ชจ๋‘ ๋”ํ•œ ์ด ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. +8. ์ด ์ฃผ๋ฌธ๊ธˆ์•ก์—์„œ ํ• ์ธ๋˜๋Š” ๊ธˆ์•ก์„ ๋บ€ ์˜ˆ์ƒ ๊ฒฐ์ œ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. +9. ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ์—ฌ๋ถ€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. + +#### 5๋ฒˆ, 6๋ฒˆ, 7๋ฒˆ, 9๋ฒˆ์˜ ๊ฒฝ์šฐ, ์—†์œผ๋ฉด `์—†์Œ` ์„ ์ถœ๋ ฅํ•œ๋‹ค. + +<br> + +## ๊ธฐ๋Šฅ ๋ชฉ๋ก + +### ๐Ÿ’ก ๊ฒ€์ฆ ๐Ÿ” + +#### ๋ฐฉ๋ฌธ ๋‚ ์งœ + +- ์ž์—ฐ์ˆ˜๊ฐ€ ๋งž๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. +- 31 ์ดํ•˜์˜ ์ˆซ์ž๊ฐ€ ๋งž๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. + +[x] ๊ฐ ๊ฒ€์ฆ ๋ชฉ๋ก์„ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•  ๊ฒฝ์šฐ์—๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ , +`[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ ํ›„ ์žฌ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. + +#### ์ฃผ๋ฌธ ๋ฉ”๋‰ด + +- ์ฃผ๋ฌธ ํ˜•์‹์ด ์˜ˆ์‹œ์™€ ๊ฐ™์€์ง€ ํ™•์ธํ•œ๋‹ค. +- ๋ฉ”๋‰ดํŒ์— ์žˆ๋Š” ๋ฉ”๋‰ด์ธ์ง€ ํ™•์ธํ•œ๋‹ค. +- ๊ฐ ๋ฉ”๋‰ด์˜ ์ˆ˜๋Ÿ‰์ด 1๊ฐœ ์ด์ƒ์ธ์ง€ ํ™•์ธํ•œ๋‹ค. +- ์ฃผ๋ฌธ ๊ฐœ์ˆ˜์˜ ์ดํ•ฉ์ด 20๊ฐœ ์ดํ•˜์ธ์ง€ ํ™•์ธํ•œ๋‹ค. +- ์ค‘๋ณต๋˜๋Š” ๋ฉ”๋‰ด๊ฐ€ ์—†๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. + +[x] ๊ฐ ๊ฒ€์ฆ ๋ชฉ๋ก์„ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•  ๊ฒฝ์šฐ์—๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ , +`[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ ํ›„ ์žฌ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. + +- ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•˜์ง€ ์•Š์•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. + +[x] ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•œ ๊ฒฝ์šฐ์—๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ , +`[ERROR] ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.` ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ ํ›„ ์žฌ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. + +--- + +### ๐Ÿ“† ํ˜œํƒ ๐Ÿ’ธ + +- `Benefits` ํด๋ž˜์Šค ์ƒ์„ฑํ•  ๋•Œ `๋‚ ์งœ` ๋ฅผ ๋ฐ›๊ณ , **๊ฒ€์ฆ ํ›„ ์ƒํƒœ๊ฐ’์œผ๋กœ ์ €์žฅ** + +- `๋‚ ์งœ` ๋ฅผ ์ด์šฉํ•ด **ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ, ํ‰์ผ ํ• ์ธ, ์ฃผ๋ง ํ• ์ธ, ํŠน๋ณ„ ํ• ์ธ** ์„ ๊ณ„์‚ฐํ•œ๋‹ค. + +**๐Ÿ“ 12์›” ๋‚ ์งœ ๊ด€๋ จ ์ด๋ฒคํŠธ ์•ˆ๋‚ด ๐Ÿ“ข** + +| Event Name | Period | Discount Details | +| ---------------------- | --------------- | ------------------------------------------------------------------- | +| ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ | 1์ผ ~ 25์ผ | 1์ผ์— `1,000`์›์œผ๋กœ ์‹œ์ž‘ํ•ด์„œ ํ•˜๋ฃจ์— `100`์›์”ฉ ์ฆ๊ฐ€ํ•œ ๊ฐ’์„ ํ• ์ธํ•œ๋‹ค. | +| ํ‰์ผ ํ• ์ธ | ์ผ์š”์ผ ~ ๋ชฉ์š”์ผ | ๋””์ €ํŠธ ๋ฉ”๋‰ด๋ฅผ 1๊ฐœ๋‹น `2,023`์›์„ ํ• ์ธํ•œ๋‹ค. | +| ์ฃผ๋ง ํ• ์ธ | ๊ธˆ์š”์ผ, ํ† ์š”์ผ | ๋ฉ”์ธ ๋ฉ”๋‰ด๋ฅผ 1๊ฐœ๋‹น `2,023`์›์„ ํ• ์ธํ•œ๋‹ค. | +| ํŠน๋ณ„ ํ• ์ธ | ์ผ์š”์ผ, 25์ผ | `1,000`์›์„ ํ• ์ธํ•œ๋‹ค. | + +--- + +### ๐Ÿ“‹ ์ฃผ๋ฌธ ๐Ÿงพ + +- `OrderMenu` ํด๋ž˜์Šค ์ƒ์„ฑํ•  ๋•Œ `์ฃผ๋ฌธ` ์„ ๋ฐ›๊ณ , **ํ˜•์‹ ๊ฒ€์ฆ ํ›„ ๊ฐ€๊ฒฉ, ํƒ€์ž…๊ณผ ํ•จ๊ป˜ ์ƒํƒœ๊ฐ’์œผ๋กœ ์ €์žฅํ•œ ๋’ค ๋‚˜๋จธ์ง€ ๊ฒ€์ฆ ์ง„ํ–‰** +- ์ฃผ๋ฌธ ๋‚ด์—ญ์„ ์ด์šฉํ•ด **๋ฉ”์ธ ๋ฉ”๋‰ด์˜ ๊ฐœ์ˆ˜, ๋””์ €ํŠธ ๋ฉ”๋‰ด์˜ ๊ฐœ์ˆ˜, ์ด ์ฃผ๋ฌธ๊ธˆ์•ก** ์„ ๊ณ„์‚ฐํ•œ๋‹ค. + +**๐Ÿ“ 12์›” ์ด ์ฃผ๋ฌธ๊ธˆ์•ก ๊ด€๋ จ ์ด๋ฒคํŠธ ์•ˆ๋‚ด ๐Ÿ“ข** + +| Event Name | Condition | Gift Description | +| ----------- | ---------------- | ---------------- | +| ์ฆ์ • ์ด๋ฒคํŠธ | 12๋งŒ์› ์ด์ƒ ์ฃผ๋ฌธ | ์ƒดํŽ˜์ธ 1๊ฐœ ์ฆ์ • | + +--- + +### ๐Ÿ”— ์„œ๋น„์Šค(ํ˜œํƒ๊ณผ ์ฃผ๋ฌธ์˜ ์—ฐ๊ฒฐ๊ณ ๋ฆฌ) โญ + +- ํ˜œํƒ๋“ค์˜ ์ด ํ• ์ธ ๊ธˆ์•ก๊ณผ ์ฃผ๋ฌธ์˜ ์ฆ์ • ์ด๋ฒคํŠธ ์—ฌ๋ถ€์— ๋”ฐ๋ฅธ ํ˜œํƒ ๊ธˆ์•ก์„ ๋”ํ•ด **์ด ํ˜œํƒ๊ธˆ์•ก** ์„ ๊ณ„์‚ฐํ•œ๋‹ค. +- ์ด ํ˜œํƒ ๊ธˆ์•ก์„ ์ด์šฉํ•ด **๋ฐฐ์ง€** ์˜ ์—ฌ๋ถ€๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. + +**๐Ÿ“ 12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ๊ด€๋ จ ์•ˆ๋‚ด ๐Ÿ“ข** + +| Total Benefits | Badge | +| -------------- | ----- | +| 5,000์› ์ด์ƒ | ๋ณ„ | +| 10,000์› ์ด์ƒ | ํŠธ๋ฆฌ | +| 20,000์› ์ด์ƒ | ์‚ฐํƒ€ | + +--- + +### ๐Ÿšจ ์ฃผ์˜์‚ฌํ•ญ ๐Ÿšง + +#### ํ˜œํƒ ๋‚ด์—ญ + +- ๊ณ ๊ฐ์—๊ฒŒ ํ•ด๋‹น๋œ ์ด๋ฒคํŠธ ๋‚ด์—ญ๋งŒ ์ถœ๋ ฅํ•œ๋‹ค. +- ์ด ์ฃผ๋ฌธ๊ธˆ์•ก์ด `10,000`์› ๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ, ํ˜œํƒ์ด ์ ์šฉ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ `์—†์Œ` ์„ ์ถœ๋ ฅํ•œ๋‹ค. +- ์ ์šฉ๋œ ํ˜œํƒ์ด ํ•˜๋‚˜๋„ ์—†์„ ๊ฒฝ์šฐ, `์—†์Œ` ์„ ์ถœ๋ ฅํ•œ๋‹ค. + ``` + // ์˜ˆ์‹œ + - 26์ผ~28์ผ์— ๋ฐฉ๋ฌธํ•˜์—ฌ ๋””์ €ํŠธ ๋ฉ”๋‰ด๋ฅผ ์‹œํ‚ค์ง€ ์•Š๊ณ , ์ด ์ฃผ๋ฌธ๊ธˆ์•ก์ด 12๋งŒ์› ๋ฏธ๋งŒ์ธ ๊ฒฝ์šฐ + - 29์ผ, 30์ผ์— ๋ฐฉ๋ฌธํ•˜์—ฌ ๋ฉ”์ธ ๋ฉ”๋‰ด๋ฅผ ์‹œํ‚ค์ง€ ์•Š๊ณ , ์ด ์ฃผ๋ฌธ๊ธˆ์•ก์ด 12๋งŒ์› ๋ฏธ๋งŒ์ธ ๊ฒฝ์šฐ + ``` + +#### ์ดํ˜œํƒ ๊ธˆ์•ก + +- ์ด ์ฃผ๋ฌธ๊ธˆ์•ก์ด `10,000`์› ๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ, ํ˜œํƒ์ด ์ ์šฉ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ `0์›` ์„ ์ถœ๋ ฅํ•œ๋‹ค. + +#### ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก + +- ์ฆ์ • ์ด๋ฒคํŠธ๋Š” ํ• ์ธ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ฃผ๋ฌธ๊ธˆ์•ก์—์„œ ๊ฐ’์„ ๋นผ์ง€ ์•Š๋Š”๋‹ค. + +## ํ…Œ์ŠคํŠธ ์ฝ”๋“œ + +### ๐Ÿ“Œ Benefits ํด๋ž˜์Šค ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ + +- ๋ฐฉ๋ฌธ ๋‚ ์งœ ๊ฒ€์ฆ ํ…Œ์ŠคํŠธ + + - [x] ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. + - [x] ์—์„œ 31 ์‚ฌ์ด์˜ ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. + +- ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ ํ…Œ์ŠคํŠธ + + - [x] 1์ผ๋ถ€ํ„ฐ 25์ผ ์‚ฌ์ด์˜ ํ• ์ธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•œ๋‹ค. + - [x] 25์ผ ์ดํ›„์˜ ํ• ์ธ ๊ธˆ์•ก์€ 0์ด๋‹ค. + +- ํ‰์ผ ํ• ์ธ ํ…Œ์ŠคํŠธ + + - [x] ํ‰์ผ์ด๋ฉด ๋””์ €ํŠธ ๋ฉ”๋‰ด์˜ ์ˆ˜๋Ÿ‰์— ๋”ฐ๋ผ ํ• ์ธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•œ๋‹ค. + - [x] ์ฃผ๋ง์ด๋ฉด ํ• ์ธ ๊ธˆ์•ก์ด 0์ด๋‹ค. + +- ์ฃผ๋ง ํ• ์ธ ํ…Œ์ŠคํŠธ + + - [x] ์ฃผ๋ง์ด๋ฉด ๋ฉ”์ธ ๋ฉ”๋‰ด์˜ ์ˆ˜๋Ÿ‰์— ๋”ฐ๋ผ ํ• ์ธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•œ๋‹ค. + - [x] ํ‰์ผ์ด๋ฉด ํ• ์ธ ๊ธˆ์•ก์ด 0์ด๋‹ค. + +- ํŠน๋ณ„ ํ• ์ธ ํ…Œ์ŠคํŠธ + - [x] ์ผ์š”์ผ์ด๋‚˜ 25์ผ์ด๋ฉด ํŠน๋ณ„ ํ• ์ธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. + - [x] ์ด์™ธ์˜ ๋‚ ์€ ํ• ์ธ ๊ธˆ์•ก์ด 0์ด๋‹ค. + +### ๐Ÿ“Œ OrderMenu ํด๋ž˜์Šค ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ + +- ์ฃผ๋ฌธ ๋ฉ”๋‰ด ๊ฒ€์ฆ ํ…Œ์ŠคํŠธ + + - [x] ์ฃผ๋ฌธ ํ˜•์‹์ด ์˜ˆ์‹œ์™€ ๋‹ค๋ฅด๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. + - [x] ๋ฉ”๋‰ดํŒ์— ์—†๋Š” ๋ฉ”๋‰ด๊ฐ€ ์žˆ์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. + - [x] ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์ด 1 ๋ฏธ๋งŒ์ด๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. + - [x] ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์˜ ์ดํ•ฉ์ด 20์ด ๋„˜์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. + - [x] ์ค‘๋ณต๋œ ๋ฉ”๋‰ด๊ฐ€ ์žˆ์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. + - [x] ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. + +- ์ƒํƒœ๊ฐ’ ํ™œ์šฉ ํ…Œ์ŠคํŠธ + - [x] ์ˆ˜๋Ÿ‰๊ณผ ๊ฐ€๊ฒฉ์„ ๊ณฑํ•ด์„œ ์ด ์ฃผ๋ฌธ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•œ๋‹ค. + - [x] ๋””์ €ํŠธ ๋ฉ”๋‰ด์˜ ์ฃผ๋ฌธ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. + - [x] ๋ฉ”์ธ ๋ฉ”๋‰ด์˜ ์ฃผ๋ฌธ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. + +### ๐Ÿ“Œ Service ํด๋ž˜์Šค ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ + +- ์ด ์ฃผ๋ฌธ๊ธˆ์•ก์ด 12๋งŒ์› ์ด์ƒ์ผ ๋•Œ, ์ด ํ˜œํƒ๊ธˆ์•ก ๊ตฌํ•˜๋Š” ํ…Œ์ŠคํŠธ + + - [x] ํฌ๋ฆฌ์Šค๋งˆ์Šค+ํ‰์ผ+ํŠน๋ณ„+์ฆ์ • + - [x] ํฌ๋ฆฌ์Šค๋งˆ์Šค+ํ‰์ผ+์ฆ์ • + - [x] ํฌ๋ฆฌ์Šค๋งˆ์Šค+์ฃผ๋ง+์ฆ์ • + - [x] ํ‰์ผ+ํŠน๋ณ„+์ฆ์ • + - [x] ์ฃผ๋ง+์ฆ์ • + - [x] ํฌ๋ฆฌ์Šค๋งˆ์Šค+ํŠน๋ณ„+์ฆ์ • + - [x] ํŠน๋ณ„+์ฆ์ • + +- ์ด ์ฃผ๋ฌธ๊ธˆ์•ก์ด 12๋งŒ์› ๋ฏธ๋งŒ์ผ ๋•Œ, ์ด ํ˜œํƒ๊ธˆ์•ก ๊ตฌํ•˜๋Š” ํ…Œ์ŠคํŠธ + + - [x] ํฌ๋ฆฌ์Šค๋งˆ์Šค+ํ‰์ผ+ํŠน๋ณ„ + - [x] ํฌ๋ฆฌ์Šค๋งˆ์Šค+ํ‰์ผ + - [x] ํฌ๋ฆฌ์Šค๋งˆ์Šค+์ฃผ๋ง + - [x] ํ‰์ผ+ํŠน๋ณ„ + - [x] ์ฃผ๋ง + - [x] ํฌ๋ฆฌ์Šค๋งˆ์Šค+ํŠน๋ณ„ + - [x] ํŠน๋ณ„ + +- ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก ๊ณ„์‚ฐ ํ…Œ์ŠคํŠธ + + - [x] ํ˜œํƒ์ด ์—†์œผ๋ฉด ์ด ์ฃผ๋ฌธ๊ธˆ์•ก์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. + - [x] ์ฆ์ • ์ด๋ฒคํŠธ๊ฐ€ ์—†์œผ๋ฉด ์ด ๊ธˆ์•ก์—์„œ ํ˜œํƒ ๊ธˆ์•ก์„ ๋บ€๋‹ค. + - [x] ์ฆ์ • ์ด๋ฒคํŠธ๊ฐ€ ์žˆ์œผ๋ฉด ์ƒดํŽ˜์ธ ๊ฐ’์„ ๋นผ๊ณ  ๊ณ„์‚ฐํ•œ๋‹ค. + +- 12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ํ…Œ์ŠคํŠธ + - [x] ์ด ํ˜œํƒ๊ธˆ์•ก์ด 5,000์› ์ด์ƒ์ด๋ฉด "๋ณ„" ๋ฐฐ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. + - [x] ์ด ํ˜œํƒ๊ธˆ์•ก์ด 10,000์› ์ด์ƒ์ด๋ฉด "ํŠธ๋ฆฌ" ๋ฐฐ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. + - [x] ์ด ํ˜œํƒ๊ธˆ์•ก์ด 20,000์› ์ด์ƒ์ด๋ฉด "์‚ฐํƒ€" ๋ฐฐ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
Unknown
์ •์„ฑ๋“ค์—ฌ ์ž‘์„ฑํ•˜์‹ ๊ฒŒ ๋А๊ปด์ง€๋Š” README๋„ค์š” ๐Ÿ‘
@@ -0,0 +1,88 @@ +const DATE = Object.freeze({ + eventEndDay: 31, + christmasEndDay: 25, +}); + +const WEEKDAY = Object.freeze([ + 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, +]); + +const WEEKEND = Object.freeze([1, 2, 8, 9, 15, 16, 22, 23, 29, 30]); + +const SPECIAL = Object.freeze([3, 10, 17, 24, 25, 31]); + +class Benefits { + #date; + + constructor(date) { + this.#validateNumber(date); // ์ž์—ฐ์ˆ˜ ๊ฒ€์ฆ + this.#date = Number(date); + this.#validateDate(); // ๋‚ ์งœ ๋ฒ”์œ„ ๊ฒ€์ฆ + } + + #validateNumber(date) { + const regex = /^[1-9]\d*$/; + const testResult = regex.test(date); + + if (testResult === false) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateDate() { + if (this.#date > DATE.eventEndDay) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + // ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ + christmasDiscount() { + const START_PRICE = 1000; + const INCREASE_PRICE = 100; + let discountedAmount = 0; + + if (this.#date <= DATE.christmasEndDay) { + discountedAmount = START_PRICE + (this.#date - 1) * INCREASE_PRICE; + } + + return ['ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', discountedAmount]; + } + + // ํ‰์ผ ํ• ์ธ + weekdayDiscount(dessertCount) { + const WEEKDAY_DISCOUNT = 2023; + let discountedAmount = 0; + + if (WEEKDAY.includes(this.#date)) { + discountedAmount = WEEKDAY_DISCOUNT * dessertCount; + } + + return ['ํ‰์ผ ํ• ์ธ', discountedAmount]; + } + + // ์ฃผ๋ง ํ• ์ธ + weekendDiscount(mainCount) { + const WEEKEND_DISCOUNT = 2023; + let discountedAmount = 0; + + if (WEEKEND.includes(this.#date)) { + discountedAmount = WEEKEND_DISCOUNT * mainCount; + } + + return ['์ฃผ๋ง ํ• ์ธ', discountedAmount]; + } + + // ํŠน๋ณ„ ํ• ์ธ + specialDiscount() { + const DISCOUNT_PRICE = 1000; + let discountedAmount = 0; + + if (SPECIAL.includes(this.#date)) { + discountedAmount = DISCOUNT_PRICE; + } + + return ['ํŠน๋ณ„ ํ• ์ธ', discountedAmount]; + } +} + +export default Benefits;
JavaScript
์‚ฌ์†Œํ•˜๋‹ค๋ฉด ์‚ฌ์†Œํ•œ ๋ถ€๋ถ„์ด์ง€๋งŒ '[ERROR] '๋Š” ๋ชจ๋“  ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€์—์„œ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ prefix๋กœ ์ƒ์ˆ˜ํ™”ํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”. ๋’ค์— ์ด์–ด์ง€๋Š” ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๊ตฌ์š”!
@@ -0,0 +1,126 @@ +import { MENU_PRICE } from '../utils/constants.js'; + +const MENU_TYPES = Object.freeze({ + appetizer: ['์–‘์†ก์ด์ˆ˜ํ”„', 'ํƒ€ํŒŒ์Šค', '์‹œ์ €์ƒ๋Ÿฌ๋“œ'], + main: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', '๋ฐ”๋น„ํ๋ฆฝ', 'ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€', 'ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€'], + dessert: ['์ดˆ์ฝ”์ผ€์ดํฌ', '์•„์ด์Šคํฌ๋ฆผ'], + drink: ['์ œ๋กœ์ฝœ๋ผ', '๋ ˆ๋“œ์™€์ธ', '์ƒดํŽ˜์ธ'], +}); + +const LIMIT_NUMBER = Object.freeze({ + minNumber: 1, + maxNumber: 20, +}); + +class OrderMenu { + #orderMenu; + + constructor(order) { + this.#validateFormat(order); // ์ž…๋ ฅ ํ˜•์‹ ๊ฒ€์ฆ + this.#arrayMenu(order); // ๊ฐ€๊ฒฉ๊ณผ ํ•จ๊ป˜ ๋ฐฐ์—ด ๊ฐ์ฒด ์ €์žฅ + this.#validateMenuItems(); // ๋ฉ”๋‰ด ์œ ๋ฌด ๊ฒ€์ฆ + this.#validateCount(); // ์ฃผ๋ฌธ ๊ฐœ์ˆ˜ ๊ฒ€์ฆ + this.#validateTotalCount(); // ์ฃผ๋ฌธ ์ด๊ฐœ์ˆ˜ ๊ฒ€์ฆ + this.#validateDuplicateMenu(); // ๋ฉ”๋‰ด ์ค‘๋ณต ๊ฒ€์ฆ + this.#validateOnlyDrink(); // ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ๊ฒ€์ฆ + } + + #arrayMenu(order) { + const menuItem = order.split(','); + this.#orderMenu = menuItem.map((item) => { + const [menuName, count] = item.split('-'); + const price = MENU_PRICE[menuName] || 0; + const type = Object.keys(MENU_TYPES).find((category) => + MENU_TYPES[category].includes(menuName), + ); + return { menuName, count: Number(count), price, type }; + }); + } + + #validateFormat(order) { + const regex = /^([๊ฐ€-ํžฃ]+-\d+,)*[๊ฐ€-ํžฃ]+-\d+$/; + const testResult = regex.test(order); + + if (testResult === false) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateMenuItems() { + const result = this.#orderMenu.some((item) => item.price === 0); + + if (result) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateCount() { + this.#orderMenu.forEach((menu) => { + if (menu.count < LIMIT_NUMBER.minNumber) { + throw new Error( + '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + ); + } + }); + } + + #validateTotalCount() { + const totalCount = this.#orderMenu.reduce( + (sum, item) => sum + item.count, + 0, + ); + + if (totalCount > LIMIT_NUMBER.maxNumber) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateDuplicateMenu() { + const menuNameSet = new Set(this.#orderMenu.map((item) => item.menuName)); + + if (menuNameSet.size !== this.#orderMenu.length) { + throw new Error('[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + #validateOnlyDrink() { + const drinkMenu = MENU_TYPES.drink; + const result = this.#orderMenu.every((item) => + drinkMenu.includes(item.menuName), + ); + + if (result) { + throw new Error('[ERROR] ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.'); + } + } + + // ์ด ์ฃผ๋ฌธ๊ธˆ์•ก + totalOrderAmount() { + const totalAmount = this.#orderMenu.reduce( + (sum, item) => sum + item.price * item.count, + 0, + ); + + return totalAmount; + } + + // ๋””์ €ํŠธ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ + dessertCount() { + const dessertMenu = this.#orderMenu.filter( + (item) => item.type === 'dessert', + ); + const count = dessertMenu.reduce((total, item) => total + item.count, 0); + + return count; + } + + // ๋ฉ”์ธ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ + mainCount() { + const mainMenu = this.#orderMenu.filter((item) => item.type === 'main'); + const count = mainMenu.reduce((total, item) => total + item.count, 0); + + return count; + } +} + +export default OrderMenu;
JavaScript
https://regexr.com/ ์ด ์‚ฌ์ดํŠธ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,141 @@ +## ๐Ÿ“ ๊ตฌํ˜„ ๋Œ€์ƒ + +### ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ”„๋กœ๋ชจ์…˜ + +### - ๋ชฉํ‘œ + +=> ์œ ์—ฐ์„ฑ๊ณผ ํ™•์žฅ์„ฑ์„ ๊ฐ•์กฐํ•œ 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ๋ฅผ ๊ฐœ๋ฐœํ•˜์—ฌ ๋ณ€ํ™”์— ๋Œ€์‘ํ•˜๊ณ , ๊ฐ์ฒด์ง€ํ–ฅ์˜ ์žฅ์ ์„ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•œ๋‹ค. +์ค‘์•™์ฒ˜๋ฆฌ์‹ ์‹œ์Šคํ…œ์ด ์•„๋‹Œ ๊ฐ์ฒด ๊ฐ„์˜ ํ˜‘๋ ฅ์„ ๊ฐ•์กฐํ•˜์—ฌ '์‚ด์•„์žˆ๋Š”' ์‹œ์Šคํ…œ์„ ์„ค๊ณ„ํ•˜๊ณ , +๋ชจ๋“  ๋„๋ฉ”์ธ ๋กœ์ง์— ์ฒ ์ €ํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ๋„์ž…ํ•˜์—ฌ ์‹œ์Šคํ…œ์˜ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณด์žฅํ•œ๋‹ค. +์ด๋ฅผ ํ†ตํ•ด ํ™•์žฅ์„ฑ ์žˆ๊ณ  ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์šฉ์ดํ•œ ํšจ๊ณผ์ ์ธ ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค. + +### - ์„ธ๋ถ€ ๋ชฉํ‘œ ๋ฐ ์ง€์นจ + +- ํด๋ž˜์Šค๋ณด๋‹ค๋Š” ๋ฉ”์‹œ์ง€ ์ค‘์‹ฌ ์„ค๊ณ„ +- UI๋Š” ๋„๋ฉ”์ธ ๋กœ์ง๊ณผ ๋ถ„๋ฆฌ +- ๋‹จ์ผ ์ฑ…์ž„ ์›์น™์„ ์ง€ํ‚ค์ž +- ์˜์กด์„ฑ ์—ญ์ „ ์›์น™์„ ์ง€ํ‚ค์ž +- ํด๋ž˜์Šค ๋ถ„๋ฆฌ ๋ฐ ์—ญํ•  ๋ถ€์—ฌ +- ์—ญํ• , ์ฑ…์ž„, ํ˜‘๋ ฅ ์„ค๊ณ„ +- ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์˜ ์ˆ˜๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•˜์ž +- ๋ฌด๋ถ„๋ณ„ํ•œ getter ์‚ฌ์šฉ ๋Œ€์‹  ๊ฐ์ฒด์— ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด ๊ฐ์ฒด๊ฐ€ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜์ž +- ๊ฐ์ฒด๋Š” ๊ฐ์ฒด์Šค๋Ÿฝ๊ฒŒ ์‚ฌ์šฉํ•˜์ž +- ๋‹จ์œ„ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์–ด๋ ค์šด ์ฝ”๋“œ๋ฅผ ๋‹จ์œ„ ํ…Œ์ŠคํŠธํ•ด๋ณด์ž + +--- + +### - ์„ค๊ณ„ + +#### 1์ฐจ ๋‹จ๊ณ„ (๋ฉ”์„ธ์ง€ ๋‹ค์ด์–ด๊ทธ๋žจ ๊ตฌ์„ฑ) + +<img src="image/xmas_promo_3rd_msg.png"> + +#### ๊ธฐ๋Šฅ ๋ช…์„ธ + +- ์ž…๋ ฅ + +> [๊ธฐ๋Šฅ] ๋ฐฉ๋ฌธ ๋‚ ์งœ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ๊ธฐ๋Šฅ<br> +> [๊ธฐ๋Šฅ] ์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด์™€ ๊ฐฏ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ๊ธฐ๋Šฅ<br> + +- ์ถœ๋ ฅ + +> [๊ธฐ๋Šฅ] ๊ณ ๊ฐ์—๊ฒŒ ์•ˆ๋‚ดํ•  ์ด๋ฒคํŠธ ์ฃผ์˜์‚ฌํ•ญ์„ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ +> - ์ด์ฃผ๋ฌธ ๊ธˆ์•ก 10,000์› ์ด์ƒ๋ถ€ํ„ฐ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค๊ณ  ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ์‹œ, ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋ฉ”๋‰ด๋Š” ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 20๊ฐœ๊นŒ์ง€๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ<br> + +> [๊ธฐ๋Šฅ] ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ ์‹คํ–‰ ๊ฒฐ๊ณผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ +> - ์ฃผ๋ฌธ ๋ฉ”๋‰ด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ํ• ์ธ ์ „ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์ฆ์ • ๋ฉ”๋‰ด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ํ˜œํƒ ๋‚ด์—ญ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์ดํ˜œํƒ ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - 12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ<br> + +> [๊ธฐ๋Šฅ] ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ +> - ์˜ˆ์™ธ ๋ช…์„ธ์„œ์— ๋ช…์‹œ๋œ ์˜ˆ์™ธ์— ๋Œ€ํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ + +- ๋กœ์ง + +> [๊ธฐ๋Šฅ] ๋ฉ”๋‰ด ๊ด€๋ จ ๊ธฐ๋Šฅ <br> +> - ๋ฉ”๋‰ด ๋ชฉ๋ก์„ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ<br> +> - ๋ฉ”๋‰ด์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ <br> +> - ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ <br> +> - ๋ฉ”๋‰ด์˜ ํƒ€์ž…(์—ํ”ผํƒ€์ด์ €/๋ฉ”์ธ/๋””์ €ํŠธ/์Œ๋ฃŒ)์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋ฉ”๋‰ด ์ด๋ฆ„์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฉ”๋‰ด ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ธฐ๋Šฅ<br> + +> [๊ธฐ๋Šฅ] ์ฆ์ • ๋ฉ”๋‰ด ๊ด€๋ จ ๊ธฐ๋Šฅ <br> +> - ์ฆ์ • ๋ฉ”๋‰ด ๋ชฉ๋ก์„ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ<br> +> - ์ฆ์ • ๋ฉ”๋‰ด์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์ฆ์ • ๋ฉ”๋‰ด์˜ ์ด๊ฐ€๊ฒฉ(๊ฐ€๊ฒฉ * ์ˆ˜๋Ÿ‰)์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์ฆ์ • ๋ฉ”๋‰ด ๋ชฉ๋ก์˜ ์ด ๊ฐ€๊ฒฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์ถœ๋ ฅํ•  ์ฆ์ • ๋ฉ”๋‰ด ์ •๋ณด๋ฅผ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ<br> + +> [๊ธฐ๋Šฅ] ์ฃผ๋ฌธ ๊ด€๋ จ ๊ธฐ๋Šฅ <br> +> - ์ฃผ๋ฌธ ํ•ญ๋ชฉ ํ•˜๋‚˜์˜ ์ด๊ฐ€๊ฒฉ(๊ฐ€๊ฒฉ * ์ˆ˜๋Ÿ‰)์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์ฃผ๋ฌธ ํ•ญ๋ชฉ์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์ฃผ๋ฌธ ํ•ญ๋ชฉ์˜ ์ˆ˜๋Ÿ‰์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์ฃผ๋ฌธ ํ•ญ๋ชฉ์˜ ๋ฉ”๋‰ด ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์ฃผ๋ฌธ์— ์ฃผ๋ฌธ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ <br> +> - ์ฃผ๋ฌธ ํ•ญ๋ชฉ๋“ค์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ <br> +> - ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก(ํ• ์ธ ์ „ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก)์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์ถœ๋ ฅํ•  ์ฃผ๋ฌธ ์ •๋ณด๋ฅผ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ<br> + +> [๊ธฐ๋Šฅ] ๋‚ ์งœ ๊ด€๋ จ ๊ธฐ๋Šฅ <br> +> - ๋‚ ์งœ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋‚ ์งœ๊ฐ€ ๊ธˆ์š”์ผ์ธ์ง€ ์ฒดํฌํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋‚ ์งœ๊ฐ€ ํ† ์š”์ผ์ธ์ง€ ์ฒดํฌํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋‚ ์งœ๊ฐ€ ์ผ์š”์ผ์ธ์ง€ ์ฒดํฌํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋‚ ์งœ๊ฐ€ ํฌ๋ฆฌ์Šค๋งˆ์Šค์ธ์ง€ ์ฒดํฌํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋‚ ์งœ๊ฐ€ ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ๊ธฐ๊ฐ„์ธ์ง€ ์ฒดํฌํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋‚ ์งœ๊ฐ€ ํ‰์ผ์ธ์ง€ ์ฒดํฌํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋‚ ์งœ๊ฐ€ ์ฃผ๋ง์ธ์ง€ ์ฒดํฌํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋‚ ์งœ๊ฐ€ ๋ณ„์„ ๊ฐ€์ง€๋Š”์ง€ ์ฒดํฌํ•˜๋Š” ๊ธฐ๋Šฅ<br> + +> [๊ธฐ๋Šฅ] ์ด๋ฒคํŠธ ํ• ์ธ๊ณผ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ <br> +> - ๋ฐฉ๋ฌธ ๋‚ ์งœ์˜ ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋ฐฉ๋ฌธ ๋‚ ์งœ์˜ ํ‰์ผ ํ• ์ธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋ฐฉ๋ฌธ ๋‚ ์งœ์˜ ์ฃผ๋ง ํ• ์ธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ๋ฐฉ๋ฌธ ๋‚ ์งœ์˜ ํŠน๋ณ„ ํ• ์ธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ธฐ๋Šฅ<br> + +> [๊ธฐ๋Šฅ] ์ฆ์ • ์ด๋ฒคํŠธ์™€ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ <br> +> - ์ฆ์ • ์ด๋ฒคํŠธ๋ฅผ ์ ์šฉ ๊ฐ€๋Šฅํ•œ์ง€ ์ฒดํฌํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์ฆ์ • ์ด๋ฒคํŠธ๋กœ ํ˜œํƒ๋ณธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ธฐ๋Šฅ<br> + +> [๊ธฐ๋Šฅ] ํ˜œํƒ ๋‚ด์—ญ๊ณผ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ <br> +> - ์ฃผ๋ฌธ์— ์ด๋ฒคํŠธ๋ฅผ ์ ์šฉ ๊ฐ€๋Šฅํ•œ์ง€ ์ฒดํฌํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ํ˜œํƒ ๋‚ด์—ญ์„ ์ƒ์„ฑํ•˜๋Š” ๊ธฐ๋Šฅ <br> +> - ์ด ํ˜œํƒ ๊ธˆ์•ก์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ<br> +> - ์ถœ๋ ฅํ•  ํ˜œํƒ ๋‚ด์—ญ ์ •๋ณด๋ฅผ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ<br> + +> [๊ธฐ๋Šฅ] ์ด๋ฒคํŠธ ๋ฐฐ์ง€์™€ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ<br> +> - ์ดํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ผ ์ด๋ฒคํŠธ ๋ฒ ์ง€๋ฅผ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ<br> +> - ์ด๋ฒคํŠธ ๋ฐฐ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ<br> + +> [๊ธฐ๋Šฅ] ๊ธฐํƒ€ ๊ธฐ๋Šฅ <br> +> - ์ž…๋ ฅํ•œ ์ฃผ๋ฌธ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ฃผ๋ฌธ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ<br> +> - ๊ธˆ์•ก์„ ###.##0 ํ˜•์‹์œผ๋กœ ํฌ๋งคํŒ…ํ•˜๋Š” ๊ธฐ๋Šฅ<br> + + +- ์˜ˆ์™ธ +> [์˜ˆ์™ธ] ์ž…๋ ฅ๋ฐ›์€ ๋ฐฉ๋ฌธ ๋‚ ์งœ ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ +> - ๋ฐฉ๋ฌธ ๋‚ ์งœ๊ฐ€ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ<br> +> - ๋ฐฉ๋ฌธ ๋‚ ์งœ๊ฐ€ 1~31์„ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ<br> + +> [์˜ˆ์™ธ] ์ž…๋ ฅ๋ฐ›์€ ์ฃผ๋ฌธ ๋ฉ”๋‰ด์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ +> - ๋ฉ”๋‰ด ํ˜•์‹์ด ์˜ˆ์‹œ์™€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ<br> +> - ๋ฉ”๋‰ดํŒ์— ์—†๋Š” ๋ฉ”๋‰ด๋ฅผ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ<br> +> - ์ค‘๋ณต ๋ฉ”๋‰ด๋ฅผ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ<br> +> - ์ž…๋ ฅํ•œ ๋ฉ”๋‰ด์˜ ๊ฐฏ์ˆ˜๊ฐ€ 1 ์ด์ƒ์˜ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ<br> + +> [์˜ˆ์™ธ] ๋Ÿฐํƒ€์ž„ ์˜ˆ์™ธ์ฒ˜๋ฆฌ +> - ์ฃผ๋ฌธ๋‚ด์—ญ์ด 20๊ฐœ๋ฅผ ์ดˆ๊ณผํ•˜๋Š” ๊ฒฝ์šฐ<br> +> - ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•œ ๊ฒฝ์šฐ<br> + +--- +### - ์ตœ์ข… ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ +<img src="image/xmasFinalDiagram.png"> + +
Unknown
์–ด๋–ค ๋ถ€๋ถ„์„ ๊ณ ๋ฏผํ•˜๊ณ  ๊ตฌํ˜„ํ•˜์…จ๋Š”์ง€ ๋ณด๊ธฐ ํŽธํ•ด์„œ ์ข‹๋„ค์š” ๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,54 @@ +package christmas.controller; + +import christmas.domain.entity.Day; +import christmas.domain.entity.Order; +import christmas.view.InputView; +import java.util.function.Supplier; + +import static christmas.domain.verifier.RuntimeVerifier.RUNTIME_VERIFIER; +import static christmas.system.IOMessage.ORDER_PROMPT_MESSAGE; +import static christmas.system.IOMessage.VISIT_DATE_PROMPT_MESSAGE; +import static christmas.view.InputView.*; +import static christmas.view.OutputView.*; + +public class PlannerSystem { + + private final Day day; + private final Order order; + private final SettlementSystem settlementSystem; + + public PlannerSystem() { + printMessage(VISIT_DATE_PROMPT_MESSAGE); + this.day = readDay(); + printEventNotice(); + printMessage(ORDER_PROMPT_MESSAGE); + this.order = readOrder(InputView::tryReadOrder); + this.settlementSystem = new SettlementSystem(day, order); + } + public void run() { + renderResult(); + } + + private Order readOrder(Supplier<Order> orderSupplier) { + while (true) { + try { + Order order = orderSupplier.get(); + RUNTIME_VERIFIER.validate(order); + return order; + } catch (IllegalStateException e) { + printMessage(e.getMessage()); + } + } + } + + private void renderResult() { + renderPlannerResult(); + settlementSystem.renderSettlementResult(); + + } + + private void renderPlannerResult() { + printResultStartMessage(day); + printOrderList(order); + } +}
Java
renderResult()๋ฅผ ๋”ฐ๋กœ ๋ฉ”์„œ๋“œ๋กœ ๋นผ์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”?
@@ -0,0 +1,54 @@ +package christmas.controller; + +import christmas.domain.entity.Day; +import christmas.domain.entity.Order; +import christmas.view.InputView; +import java.util.function.Supplier; + +import static christmas.domain.verifier.RuntimeVerifier.RUNTIME_VERIFIER; +import static christmas.system.IOMessage.ORDER_PROMPT_MESSAGE; +import static christmas.system.IOMessage.VISIT_DATE_PROMPT_MESSAGE; +import static christmas.view.InputView.*; +import static christmas.view.OutputView.*; + +public class PlannerSystem { + + private final Day day; + private final Order order; + private final SettlementSystem settlementSystem; + + public PlannerSystem() { + printMessage(VISIT_DATE_PROMPT_MESSAGE); + this.day = readDay(); + printEventNotice(); + printMessage(ORDER_PROMPT_MESSAGE); + this.order = readOrder(InputView::tryReadOrder); + this.settlementSystem = new SettlementSystem(day, order); + } + public void run() { + renderResult(); + } + + private Order readOrder(Supplier<Order> orderSupplier) { + while (true) { + try { + Order order = orderSupplier.get(); + RUNTIME_VERIFIER.validate(order); + return order; + } catch (IllegalStateException e) { + printMessage(e.getMessage()); + } + } + } + + private void renderResult() { + renderPlannerResult(); + settlementSystem.renderSettlementResult(); + + } + + private void renderPlannerResult() { + printResultStartMessage(day); + printOrderList(order); + } +}
Java
outputView๋ฅผ static์œผ๋กœ ํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”?
@@ -0,0 +1,54 @@ +package christmas.controller; + +import christmas.domain.entity.Day; +import christmas.domain.entity.Order; +import christmas.view.InputView; +import java.util.function.Supplier; + +import static christmas.domain.verifier.RuntimeVerifier.RUNTIME_VERIFIER; +import static christmas.system.IOMessage.ORDER_PROMPT_MESSAGE; +import static christmas.system.IOMessage.VISIT_DATE_PROMPT_MESSAGE; +import static christmas.view.InputView.*; +import static christmas.view.OutputView.*; + +public class PlannerSystem { + + private final Day day; + private final Order order; + private final SettlementSystem settlementSystem; + + public PlannerSystem() { + printMessage(VISIT_DATE_PROMPT_MESSAGE); + this.day = readDay(); + printEventNotice(); + printMessage(ORDER_PROMPT_MESSAGE); + this.order = readOrder(InputView::tryReadOrder); + this.settlementSystem = new SettlementSystem(day, order); + } + public void run() { + renderResult(); + } + + private Order readOrder(Supplier<Order> orderSupplier) { + while (true) { + try { + Order order = orderSupplier.get(); + RUNTIME_VERIFIER.validate(order); + return order; + } catch (IllegalStateException e) { + printMessage(e.getMessage()); + } + } + } + + private void renderResult() { + renderPlannerResult(); + settlementSystem.renderSettlementResult(); + + } + + private void renderPlannerResult() { + printResultStartMessage(day); + printOrderList(order); + } +}
Java
static import๋ฅผ ํ•˜์‹ค ๋•Œ๋Š” ์–ด๋–ค ๊ฒƒ๋“ค์„ importํ–ˆ๋Š”์ง€ ์ž˜ ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๋„๋ก ์™€์ผ๋“œ์นด๋“œ(*)๋ฅผ ์‚ฌ์šฉํ•˜์ง€์•Š๊ณ  ์ „๋ถ€ ํ‘œ์‹œํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•˜๋”๋ผ๊ตฌ์š”. ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,32 @@ +package christmas.controller; + +import christmas.domain.entity.Day; +import christmas.domain.entity.DiscountRecord; +import christmas.domain.entity.Order; +import christmas.domain.manager.BonusEventManager; +import christmas.domain.manager.DiscountManager; + +import static christmas.domain.entity.EventBadge.createBadge; +import static christmas.view.OutputView.*; + +public class SettlementSystem { + + private final Order order; + private final DiscountRecord discountRecord; + + public SettlementSystem(Day day, Order order) { + DiscountManager discountManager = new DiscountManager(); + BonusEventManager bonusEventManager = new BonusEventManager(); + this.order = order; + this.discountRecord = DiscountRecord.create(day, order, discountManager, bonusEventManager); + } + + public void renderSettlementResult() { + printOriginalTotalAmount(order); + printBonusMenu(order); + printDiscountRecord(discountRecord); + printTotalDiscountAmount(discountRecord); + printExpectedPayment(order, discountRecord); + printEventBadge(createBadge(discountRecord)); + } +}
Java
์ €๋„ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์—ฌ๋Ÿฌ๊ฐ€์ง€๋กœ ๋‚˜๋ˆ ๋ณด๋ ค๊ณ  ํ–ˆ์–ด์„œ ๋ฐ˜๊ฐ‘๋„ค์š”! ์–ด๋–ค ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ ๋ณด๋ ค๊ณ  ํ•˜์…จ๋Š”์ง€ ๊ถ๊ธˆํ•ด์š”
@@ -0,0 +1,48 @@ +package christmas.domain.entity; + +import christmas.domain.util.Util; + +import java.util.Arrays; + +import static christmas.system.IOMessage.EMPTY_STRING; +import static christmas.system.IOMessage.QUANTITY_UNIT; + +public enum BonusMenu { + CHAMPAGNE(Menu.CHAMPAGNE, 1); + + private final Menu menu; + private final int quantity; + + BonusMenu(Menu menu, int quantity) { + this.menu = menu; + this.quantity = quantity; + } + + public static int calculateTotalPriceForAllMenus() { + return Arrays.stream(BonusMenu.values()) + .mapToInt(BonusMenu::getTotalPrice) + .sum(); + } + + public static String generateAllMenuDetails() { + StringBuilder stringBuilder = new StringBuilder(); + for (BonusMenu bonusMenu : BonusMenu.values()) { + stringBuilder.append(bonusMenu.toString()).append(System.lineSeparator()); + } + return stringBuilder.toString(); + } + + @Override + public String toString() { + return getName() + EMPTY_STRING + Util.createFormattedAmount(quantity) + QUANTITY_UNIT; + } + + private String getName() { + return menu.getMenuName(); + } + + private int getTotalPrice() { + return menu.getMenuPrice() * quantity; + } + +}
Java
์ด๋ถ€๋ถ„์€ ์ถœ๋ ฅ ๋ถ€๋ถ„๊ณผ ๊ด€๊ณ„ ์žˆ์–ด์„œ OutputView๊ฐ€ ์ผํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,65 @@ +package christmas.domain.manager; + + +import christmas.domain.entity.Day; +import christmas.domain.entity.Order; + +import static christmas.domain.entity.MenuType.*; +import static christmas.system.Constant.ZERO; + +public class DiscountManager { + + private final int DISCOUNT_AMOUNT = 2023; + private final int INITIAL_DISCOUNT_AMOUNT = 1000; + private final int DAILY_DISCOUNT_INCREASE = 100; + + public DiscountManager() { + } + + public int calculateDDayDiscount(Day day) { + if (day.isChristmasSeason()) { + return calculateDDayAmount(day); + } + return ZERO; + } + + public int calculateWeekDayDiscount(Day day, Order order) { + if (day.isWeekday()) { + return calculateWeekDayAmount(order); + } + return ZERO; + } + + public int calculateWeekendDiscount(Day day, Order order) { + if (day.isWeekend()) { + return calculateWeekendAmount(order); + } + return ZERO; + } + + public int calculateSpecialDayDiscount(Day day) { + int STAR_DISCOUNT = 1000; + if (day.isSpecialDay()) { + return STAR_DISCOUNT; + } + return ZERO; + } + + private int calculateDDayAmount(Day day) { + return INITIAL_DISCOUNT_AMOUNT + DAILY_DISCOUNT_INCREASE * (day.getDay() - 1); + } + + private int calculateWeekDayAmount(Order order) { + return order.getOrderItems().stream() + .filter(orderItem -> orderItem.getMenuType() == DESSERT) + .mapToInt(orderItem -> DISCOUNT_AMOUNT * orderItem.getQuantity()) + .sum(); + } + + private int calculateWeekendAmount(Order order) { + return order.getOrderItems().stream() + .filter(orderItem -> orderItem.getMenuType() == MAIN) + .mapToInt(orderItem -> DISCOUNT_AMOUNT * orderItem.getQuantity()) + .sum(); + } +}
Java
day ๊ฐ์ฒด์— ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ๋„ฃ์–ด๋†“์œผ๋‹ˆ ์ฝ๊ธฐ๋„ ๋งค์šฐ ํŽธํ•ด์„œ ์ข‹๋„ค์š” ๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,12 @@ +package christmas.domain.util; + + +import java.text.DecimalFormat; + +public final class Util { + public static String createFormattedAmount(int target) { + DecimalFormat formatter = new DecimalFormat("###,##0"); + return formatter.format(target); + } + +}
Java
split ํ•˜๋Š” ๊ธฐ์ค€์ด ๋‚˜์ค‘์—๋Š” ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ƒ์ˆ˜๋กœ ๋งŒ๋“ค์–ด ๊ด€๋ฆฌํ•˜๋ฉด ๋‚˜์ค‘์— ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,54 @@ +package christmas.controller; + +import christmas.domain.entity.Day; +import christmas.domain.entity.Order; +import christmas.view.InputView; +import java.util.function.Supplier; + +import static christmas.domain.verifier.RuntimeVerifier.RUNTIME_VERIFIER; +import static christmas.system.IOMessage.ORDER_PROMPT_MESSAGE; +import static christmas.system.IOMessage.VISIT_DATE_PROMPT_MESSAGE; +import static christmas.view.InputView.*; +import static christmas.view.OutputView.*; + +public class PlannerSystem { + + private final Day day; + private final Order order; + private final SettlementSystem settlementSystem; + + public PlannerSystem() { + printMessage(VISIT_DATE_PROMPT_MESSAGE); + this.day = readDay(); + printEventNotice(); + printMessage(ORDER_PROMPT_MESSAGE); + this.order = readOrder(InputView::tryReadOrder); + this.settlementSystem = new SettlementSystem(day, order); + } + public void run() { + renderResult(); + } + + private Order readOrder(Supplier<Order> orderSupplier) { + while (true) { + try { + Order order = orderSupplier.get(); + RUNTIME_VERIFIER.validate(order); + return order; + } catch (IllegalStateException e) { + printMessage(e.getMessage()); + } + } + } + + private void renderResult() { + renderPlannerResult(); + settlementSystem.renderSettlementResult(); + + } + + private void renderPlannerResult() { + printResultStartMessage(day); + printOrderList(order); + } +}
Java
์‚ฌ์‹ค ์ด๊ฒŒ system. run()์˜ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์ค‘์— ํ•˜๋‚˜๊ฐ€ renderResult()์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ ๋บ์–ด์š”!. ๋‚˜์ค‘์— ํ™•์žฅ์„ฑ์žˆ๊ฒŒ ๋‹ค๋ฅธ ๊ธฐ๋Šฅ๋“ค๋„ ๋„ฃ์„์ˆ˜ ์žˆ๊ฒŒํ•˜๊ธฐ ์œ„ํ•ด์„œ์š”!
@@ -0,0 +1,27 @@ +package christmas.system; + +public final class IOMessage { + public static final String VISIT_DATE_PROMPT_MESSAGE = "์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.\n" + + "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"; + + public static final String ORDER_PROMPT_MESSAGE = "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)"; + + public static final String NONE = "์—†์Œ"; + public static final String EMPTY_STRING = " "; + public static final String MESSAGE_DELIMITER = ": -"; + + public static final String QUANTITY_UNIT = "๊ฐœ"; + + public static final String MONEY_UNIT = "์›"; + + public static final String XMAS_DDAY_DISCOUNT = "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"; + public static final String WEEKDAY_DISCOUNT = "ํ‰์ผ ํ• ์ธ"; + public static final String WEEKEND_DISCOUNT = "์ฃผ๋ง ํ• ์ธ"; + public static final String SPECIAL_DISCOUNT = "ํŠน๋ณ„ ํ• ์ธ"; + public static final String BONUS_EVENT = "์ฆ์ • ์ด๋ฒคํŠธ"; + + + private IOMessage() { + + } +}
Java
์ƒ์„ฑ์ž๋ฅผ `private`์œผ๋กœ ๋ง‰์•„๋‘์‹  ๋ชจ์Šต์ด ๋ณด๊ธฐ ์ข‹์Šต๋‹ˆ๋‹ค! ๐Ÿ˜Š
@@ -0,0 +1,54 @@ +package christmas.controller; + +import christmas.domain.entity.Day; +import christmas.domain.entity.Order; +import christmas.view.InputView; +import java.util.function.Supplier; + +import static christmas.domain.verifier.RuntimeVerifier.RUNTIME_VERIFIER; +import static christmas.system.IOMessage.ORDER_PROMPT_MESSAGE; +import static christmas.system.IOMessage.VISIT_DATE_PROMPT_MESSAGE; +import static christmas.view.InputView.*; +import static christmas.view.OutputView.*; + +public class PlannerSystem { + + private final Day day; + private final Order order; + private final SettlementSystem settlementSystem; + + public PlannerSystem() { + printMessage(VISIT_DATE_PROMPT_MESSAGE); + this.day = readDay(); + printEventNotice(); + printMessage(ORDER_PROMPT_MESSAGE); + this.order = readOrder(InputView::tryReadOrder); + this.settlementSystem = new SettlementSystem(day, order); + } + public void run() { + renderResult(); + } + + private Order readOrder(Supplier<Order> orderSupplier) { + while (true) { + try { + Order order = orderSupplier.get(); + RUNTIME_VERIFIER.validate(order); + return order; + } catch (IllegalStateException e) { + printMessage(e.getMessage()); + } + } + } + + private void renderResult() { + renderPlannerResult(); + settlementSystem.renderSettlementResult(); + + } + + private void renderPlannerResult() { + printResultStartMessage(day); + printOrderList(order); + } +}
Java
static์ด ์•„๋‹ˆ๊ฒŒ ๊ตฌํ˜„ํ•ด์„œ ์˜์กด๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ฒŒ ํ•ด์•ผํ• ๊นŒ ๊ณ ๋ฏผํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ํ™•์‹คํžˆ ์˜์กด์„ฑ ์ฃผ์ž…์ด ํ•„์š”ํ•œ ๊ด€๊ณ„์ด๋‹ค ์‹ถ์€๊ฑฐ๋Š” ์ฃผ์ž…์„ ์‹œ์ผฐ๋Š”๋ฐ inputview์™€ outputview๊นŒ์ง€ ์‹œ์ผœ์•ผํ•˜๋‚˜ ํ•ด์„œ์š”. ์‹œํ‚ค๋Š”๊ฒŒ ๋งž์„๊นŒ์š”?
@@ -0,0 +1,54 @@ +package christmas.controller; + +import christmas.domain.entity.Day; +import christmas.domain.entity.Order; +import christmas.view.InputView; +import java.util.function.Supplier; + +import static christmas.domain.verifier.RuntimeVerifier.RUNTIME_VERIFIER; +import static christmas.system.IOMessage.ORDER_PROMPT_MESSAGE; +import static christmas.system.IOMessage.VISIT_DATE_PROMPT_MESSAGE; +import static christmas.view.InputView.*; +import static christmas.view.OutputView.*; + +public class PlannerSystem { + + private final Day day; + private final Order order; + private final SettlementSystem settlementSystem; + + public PlannerSystem() { + printMessage(VISIT_DATE_PROMPT_MESSAGE); + this.day = readDay(); + printEventNotice(); + printMessage(ORDER_PROMPT_MESSAGE); + this.order = readOrder(InputView::tryReadOrder); + this.settlementSystem = new SettlementSystem(day, order); + } + public void run() { + renderResult(); + } + + private Order readOrder(Supplier<Order> orderSupplier) { + while (true) { + try { + Order order = orderSupplier.get(); + RUNTIME_VERIFIER.validate(order); + return order; + } catch (IllegalStateException e) { + printMessage(e.getMessage()); + } + } + } + + private void renderResult() { + renderPlannerResult(); + settlementSystem.renderSettlementResult(); + + } + + private void renderPlannerResult() { + printResultStartMessage(day); + printOrderList(order); + } +}
Java
ํ— ๋ชฐ๋ž์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.!!
@@ -0,0 +1,32 @@ +package christmas.controller; + +import christmas.domain.entity.Day; +import christmas.domain.entity.DiscountRecord; +import christmas.domain.entity.Order; +import christmas.domain.manager.BonusEventManager; +import christmas.domain.manager.DiscountManager; + +import static christmas.domain.entity.EventBadge.createBadge; +import static christmas.view.OutputView.*; + +public class SettlementSystem { + + private final Order order; + private final DiscountRecord discountRecord; + + public SettlementSystem(Day day, Order order) { + DiscountManager discountManager = new DiscountManager(); + BonusEventManager bonusEventManager = new BonusEventManager(); + this.order = order; + this.discountRecord = DiscountRecord.create(day, order, discountManager, bonusEventManager); + } + + public void renderSettlementResult() { + printOriginalTotalAmount(order); + printBonusMenu(order); + printDiscountRecord(discountRecord); + printTotalDiscountAmount(discountRecord); + printExpectedPayment(order, discountRecord); + printEventBadge(createBadge(discountRecord)); + } +}
Java
์ด๊ฒŒ ์—ฌ๋Ÿฌ๊ฐ€์ง€๋กœ ๋‚˜๋ˆ ๋ณผ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ๋„ˆ๋ฌด ์–ด๋ ต๋”๋ผ๊ตฌ์š”... ๋‚˜๋ˆ ์•ผ ๊น”๋”ํ• ๊ฑฐ ๊ฐ™๊ธฐ๋Š” ํ•œ๋ฐ... ๊ทธ๋ƒฅ ์ €ํฌ๊ฐ€ plannerSystem์„ ๊ตฌํ˜„ํ•˜๋Š”๊ฑฐ์˜€๊ณ , ๊ธฐ๋Šฅ๋“ค์ด ๋‹ค ์ •์‚ฐ(๊ธˆ์œต)๊ณผ ๊ด€๋ จ๋œ ๋ถ€๋ถ„์ด๋ผ์„œ settlementSystem์„ ๋‘์—ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,76 @@ +package christmas.domain.entity; + +import static christmas.domain.verifier.VisitDateVerifier.VISIT_DATE_VERIFIER; +import static christmas.system.Constant.CHRISTMAS; +import static christmas.system.Constant.FIRST_DAY_OF_MONTH; + +public class Day { + + private static final int WEEK_LENGTH = 7; + private final int day; + + public Day(int day) { + VISIT_DATE_VERIFIER.validateInputInDomain(day); + this.day = day; + } + + public int getDay() { + return day; + } + + public boolean isChristmasSeason() { + if (day >= FIRST_DAY_OF_MONTH && day <= CHRISTMAS) { + return true; + } + return false; + } + + public boolean isWeekday() { + if (!(isFriday() || isSaturday())) { + return true; + } + return false; + } + + public boolean isWeekend() { + if (isFriday() || isSaturday()) { + return true; + } + return false; + } + + public boolean isSpecialDay() { + if (isSunday() || isChristmas()) { + return true; + } + return false; + } + + private boolean isFriday() { + if (day % WEEK_LENGTH == 1) { + return true; + } + return false; + } + + private boolean isSaturday() { + if (day % WEEK_LENGTH == 2) { + return true; + } + return false; + } + + private boolean isSunday() { + if (day % WEEK_LENGTH == 3) { + return true; + } + return false; + } + + private boolean isChristmas() { + if (day == CHRISTMAS) { + return true; + } + return false; + } +}
Java
๋ฉ”์„œ๋“œ๋ช…์„ ์ •๋ง ์ž˜ ์ง€์œผ์‹ ๊ฑฐ ๊ฐ™์•„์š”! ๐Ÿ˜
@@ -0,0 +1,48 @@ +package christmas.domain.entity; + +import christmas.domain.util.Util; + +import java.util.Arrays; + +import static christmas.system.IOMessage.EMPTY_STRING; +import static christmas.system.IOMessage.QUANTITY_UNIT; + +public enum BonusMenu { + CHAMPAGNE(Menu.CHAMPAGNE, 1); + + private final Menu menu; + private final int quantity; + + BonusMenu(Menu menu, int quantity) { + this.menu = menu; + this.quantity = quantity; + } + + public static int calculateTotalPriceForAllMenus() { + return Arrays.stream(BonusMenu.values()) + .mapToInt(BonusMenu::getTotalPrice) + .sum(); + } + + public static String generateAllMenuDetails() { + StringBuilder stringBuilder = new StringBuilder(); + for (BonusMenu bonusMenu : BonusMenu.values()) { + stringBuilder.append(bonusMenu.toString()).append(System.lineSeparator()); + } + return stringBuilder.toString(); + } + + @Override + public String toString() { + return getName() + EMPTY_STRING + Util.createFormattedAmount(quantity) + QUANTITY_UNIT; + } + + private String getName() { + return menu.getMenuName(); + } + + private int getTotalPrice() { + return menu.getMenuPrice() * quantity; + } + +}
Java
์ฒ˜์Œ์—๋Š” ๊ทธ๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ํ–ˆ์—ˆ๋Š”๋ฐ, ๊ฐ ํด๋ž˜์Šค์—์„œ, ํด๋ž˜์Šค์— ๋Œ€ํ•ด ์ถœ๋ ฅํ•  ์ •๋ณด๋ฅผ ๋งŒ๋“œ๋Š”๊ฑฐ๊นŒ์ง€๋Š” ๊ฐ์ฒด๊ฐ€ ๋‹ด๋‹นํ•ด์•ผํ•  ๋ชซ์ด์ง€ ์•Š์„๊นŒ ํ•ด์„œ ๋ฐ”๊ฟจ์—ˆ์–ด์š”... ์ถœ๋ ฅ์€ outputview์—์„œ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ๋‹ค๋ฅธ๋ถ€๋ถ„์˜ ์˜๊ฒฌ๋„ ๋“ฃ๊ณ  ์‹ถ์–ด์š”!! 1. outputview์—์„œ getter๊ฐ€ ์•„๋‹ˆ๋”๋ผ๋„, ์ธ์ž๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅํ•  ํด๋ž˜์Šค์˜ ์ •๋ณด๋ฅผ ๊บผ๋‚ด์„œ ์ถœ๋ ฅ ๋ฉ”์‹œ์ง€๋ฅผ ๋งŒ๋“ค๊ณ  ์ถœ๋ ฅ๊นŒ์ง€ ํ•˜๋Š”๊ฑธ ๋‹ด๋‹นํ•ด์•ผํ• ๊นŒ? 2. ์ถœ๋ ฅํ•ด์•ผํ•  ํด๋ž˜์Šค์˜ ์ •๋ณด๋Š” ํด๋ž˜์Šค ๋”ด์—์„œ ๋งŒ๋“ค๊ณ , ์ถœ๋ ฅ๋งŒ outputview์—์„œ ํ•ด์•ผํ•˜๋Š”๊ฒŒ ๋งž์„๊นŒ์š”?
@@ -0,0 +1,12 @@ +package christmas.domain.util; + + +import java.text.DecimalFormat; + +public final class Util { + public static String createFormattedAmount(int target) { + DecimalFormat formatter = new DecimalFormat("###,##0"); + return formatter.format(target); + } + +}
Java
์ฐธ๊ณ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋„ˆ๋ฌด๋„ˆ๋ฌด ํ”ผ๋“œ๋ฐฑ ์—ด์‹ฌํžˆ ํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ด์š” ์˜ˆ์Šฌ๋‹˜ ๐Ÿ™ƒ
@@ -0,0 +1,76 @@ +package christmas.domain.entity; + +import static christmas.domain.verifier.VisitDateVerifier.VISIT_DATE_VERIFIER; +import static christmas.system.Constant.CHRISTMAS; +import static christmas.system.Constant.FIRST_DAY_OF_MONTH; + +public class Day { + + private static final int WEEK_LENGTH = 7; + private final int day; + + public Day(int day) { + VISIT_DATE_VERIFIER.validateInputInDomain(day); + this.day = day; + } + + public int getDay() { + return day; + } + + public boolean isChristmasSeason() { + if (day >= FIRST_DAY_OF_MONTH && day <= CHRISTMAS) { + return true; + } + return false; + } + + public boolean isWeekday() { + if (!(isFriday() || isSaturday())) { + return true; + } + return false; + } + + public boolean isWeekend() { + if (isFriday() || isSaturday()) { + return true; + } + return false; + } + + public boolean isSpecialDay() { + if (isSunday() || isChristmas()) { + return true; + } + return false; + } + + private boolean isFriday() { + if (day % WEEK_LENGTH == 1) { + return true; + } + return false; + } + + private boolean isSaturday() { + if (day % WEEK_LENGTH == 2) { + return true; + } + return false; + } + + private boolean isSunday() { + if (day % WEEK_LENGTH == 3) { + return true; + } + return false; + } + + private boolean isChristmas() { + if (day == CHRISTMAS) { + return true; + } + return false; + } +}
Java
๊ฐœ์ธ์ ์ธ ์˜๊ฒฌ์ธ๋ฐ, `isFriday() || isSaturday()`๊ฐ€ `isWeekend()`๋ฉ”์„œ๋“œ์™€ ๋˜‘๊ฐ™์œผ๋‹ˆ, ์ด๊ฑธ ํ™œ์šฉํ•˜๋Š”๊ฒŒ ์–ด๋–จ๊นŒ์š”? ```java public boolean isWeekday() { return !isWeekend(); } ``` ์ด๋ ‡๊ฒŒ์š”! ๐Ÿ˜‰
@@ -0,0 +1,59 @@ +package christmas.domain.entity; + +import java.util.HashMap; +import java.util.Map; + +import static christmas.domain.entity.MenuType.*; + +public enum Menu { + + MUSHROOM_SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6_000, APPETIZER), + TAPAS("ํƒ€ํŒŒ์Šค", 5_500, APPETIZER), + CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000, APPETIZER), + + T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000, MAIN), + BBQ_RIBS("๋ฐ”๋น„ํ๋ฆฝ", 54_000, MAIN), + SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000, MAIN), + CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000, MAIN), + + CHOCOLATE_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15_000, DESSERT), + ICE_CREAM("์•„์ด์Šคํฌ๋ฆผ", 5_000, DESSERT), + + ZERO_COLA("์ œ๋กœ์ฝœ๋ผ", 3_000, BEVERAGE), + RED_WINE("๋ ˆ๋“œ์™€์ธ", 60_000, BEVERAGE), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, BEVERAGE); + + private static final Map<String, Menu> MENU_MAP = new HashMap<>(); + private final String menuName; + private final int price; + private final MenuType menuType; + + Menu(String menuName, int price, MenuType menuType) { + this.menuName = menuName; + this.price = price; + this.menuType = menuType; + } + + static { + for (Menu menu : Menu.values()) { + MENU_MAP.put(menu.menuName, menu); + } + } + + public static Menu getMenuItemByName(String menuName) { + return MENU_MAP.get(menuName); + } + + public String getMenuName() { + return menuName; + } + + public int getMenuPrice() { + return price; + } + + public MenuType getMenuType() { + return menuType; + } + +}
Java
๋”ฐ๋กœ `HashMap`์„ ๋งŒ๋“ค์–ด ๋‘” ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! `getMenuItemByName`๋ฉ”์„œ๋“œ์—์„œ ๊ฒ€์ƒ‰์†๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•จ์ธ๊ฐ€์š”?
@@ -0,0 +1,12 @@ +package christmas.domain.util; + + +import java.text.DecimalFormat; + +public final class Util { + public static String createFormattedAmount(int target) { + DecimalFormat formatter = new DecimalFormat("###,##0"); + return formatter.format(target); + } + +}
Java
split์„ ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์‚ฌ์ด์ฆˆ๊ฐ€ 1์ด์–ด์„œ `orderItemSpec[1]`์˜ ์ ‘๊ทผ์ด `IndexOutOfBoundsException`์„ ์ผ์œผํ‚ฌ ๊ฒฝ์šฐ๋Š” ์—†๋‚˜์š”? ์ด๋ฅผ ์—ผ๋‘ํ•ด๋‘์…จ๋‚˜๋ฉด ์–ด๋–ป๊ฒŒ ๊ฒ€์ฆ์„ ํ•˜์…จ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,96 @@ +package christmas.domain.verifier; + + +import christmas.domain.entity.Menu; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static christmas.system.ExceptionMessage.*; + +public class OrderMenuVerifier implements Verifier<String> { + + public static final OrderMenuVerifier ORDER_MENU_VERIFIER = new OrderMenuVerifier(); + + private final Pattern pattern; + + private OrderMenuVerifier() { + this.pattern = Pattern.compile("[๊ฐ€-ํžฃ]+-\\d+(,[๊ฐ€-ํžฃ]+-\\d+)*"); + } + + @Override + public void validate(final String input) { + validateMenuFormat(input); + } + + public void validateInputInDomain(final String input) { + validateMenuExistence(input); + validateDistinctMenu(input); + validateMenuQuantity(input); + } + + private void validateMenuFormat(final String input) { + String[] orders = input.split(","); + for (String order : orders) { + validateOrderFormat(order); + } + } + + private void validateMenuExistence(final String input) { + String[] orders = input.split(","); + for (String order : orders) { + validateOrderExistence(order); + } + } + + private void validateDistinctMenu(final String input) { + Set<String> uniqueMenuItems = new HashSet<>(); + String[] orders = input.split(","); + for (String order : orders) { + validateDistinctMenu(order, uniqueMenuItems); + } + } + + private void validateMenuQuantity(final String input) { + String[] orders = input.split(","); + for (String order : orders) { + validateOrderQuantity(order); + } + } + + private void validateOrderFormat(final String order) { + Matcher matcher = pattern.matcher(order); + if (!matcher.matches()) { + Verifier.throwIllegalArgumentError(INVALID_ORDER_MESSAGE); + } + } + + private void validateOrderExistence(final String order) { + String[] orderInfo = order.split("-"); + if (Menu.getMenuItemByName(orderInfo[0]) == null) { + Verifier.throwIllegalArgumentError(INVALID_ORDER_MESSAGE); + } + } + + private void validateDistinctMenu(final String order, Set<String> uniqueMenuItems) { + String[] orderInfo = order.split("-"); + if (!uniqueMenuItems.add(orderInfo[0])) { + Verifier.throwIllegalArgumentError(INVALID_ORDER_MESSAGE); + } + } + + private void validateOrderQuantity(final String order) { + String[] orderInfo = order.split("-"); + try { + int quantity = Integer.parseInt(orderInfo[1]); + if (quantity < 1) { + Verifier.throwIllegalArgumentError(INVALID_ORDER_MESSAGE); + } + } catch (NumberFormatException e) { + Verifier.throwIllegalArgumentError(INVALID_ORDER_MESSAGE); + } + } + + +}
Java
๋”ฐ๋กœ ์ธ์ž์— ๋”ฐ๋ผ์„œ ํŒจํ„ด์ด ๋‹ฌ๋ผ์ง€๋Š” ๊ฒƒ ๊ฐ™์ง„ ์•Š์€๋ฐ, ํ•„๋“œ์—์„œ ๋ฐ”๋กœ ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š๊ณ , ์ƒ์„ฑ์ž ๋‚ด๋ถ€์—์„œ ์ดˆ๊ธฐํ™”ํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! ๋ญ๊ฐ€ ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ธ์ง€ ์ €๋„ ์ž˜ ๋ชจ๋ฅด๊ฒ ์–ด์š”.. ๐Ÿ˜‚
@@ -0,0 +1,14 @@ +package christmas.domain.verifier; + +public interface Verifier<T> { + + abstract public void validate(T input); + + static void throwIllegalArgumentError(String errorMessage) { + throw new IllegalArgumentException(errorMessage); + } + + static void throwIllegalStateError(String errorMessage) { + throw new IllegalStateException(errorMessage); + } +}
Java
์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋“ค์€ `check`๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ํ•„์š”ํ•œ ๊ฒ€์ฆ์ด ๋‹ค ๋˜๋Š” ๋ฐฉ์‹์ด๋„ค์š”! ๊น”๋”ํ•˜๊ณ  ์ข‹๋„ค์š”! ๐Ÿ˜Š
@@ -0,0 +1,59 @@ +package christmas.domain.entity; + +import java.util.HashMap; +import java.util.Map; + +import static christmas.domain.entity.MenuType.*; + +public enum Menu { + + MUSHROOM_SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6_000, APPETIZER), + TAPAS("ํƒ€ํŒŒ์Šค", 5_500, APPETIZER), + CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000, APPETIZER), + + T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000, MAIN), + BBQ_RIBS("๋ฐ”๋น„ํ๋ฆฝ", 54_000, MAIN), + SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000, MAIN), + CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000, MAIN), + + CHOCOLATE_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15_000, DESSERT), + ICE_CREAM("์•„์ด์Šคํฌ๋ฆผ", 5_000, DESSERT), + + ZERO_COLA("์ œ๋กœ์ฝœ๋ผ", 3_000, BEVERAGE), + RED_WINE("๋ ˆ๋“œ์™€์ธ", 60_000, BEVERAGE), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, BEVERAGE); + + private static final Map<String, Menu> MENU_MAP = new HashMap<>(); + private final String menuName; + private final int price; + private final MenuType menuType; + + Menu(String menuName, int price, MenuType menuType) { + this.menuName = menuName; + this.price = price; + this.menuType = menuType; + } + + static { + for (Menu menu : Menu.values()) { + MENU_MAP.put(menu.menuName, menu); + } + } + + public static Menu getMenuItemByName(String menuName) { + return MENU_MAP.get(menuName); + } + + public String getMenuName() { + return menuName; + } + + public int getMenuPrice() { + return price; + } + + public MenuType getMenuType() { + return menuType; + } + +}
Java
๋„ค ๋งž์•„์š”! HashMap์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฒ€์ƒ‰์†๋„๊ฐ€ O(1)์— ๊ฐ€๊นŒ์šด ์†๋„๋กœ ๋งค์šฐ ํ–ฅ์ƒ๋œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.!!
@@ -0,0 +1,96 @@ +package christmas.domain.verifier; + + +import christmas.domain.entity.Menu; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static christmas.system.ExceptionMessage.*; + +public class OrderMenuVerifier implements Verifier<String> { + + public static final OrderMenuVerifier ORDER_MENU_VERIFIER = new OrderMenuVerifier(); + + private final Pattern pattern; + + private OrderMenuVerifier() { + this.pattern = Pattern.compile("[๊ฐ€-ํžฃ]+-\\d+(,[๊ฐ€-ํžฃ]+-\\d+)*"); + } + + @Override + public void validate(final String input) { + validateMenuFormat(input); + } + + public void validateInputInDomain(final String input) { + validateMenuExistence(input); + validateDistinctMenu(input); + validateMenuQuantity(input); + } + + private void validateMenuFormat(final String input) { + String[] orders = input.split(","); + for (String order : orders) { + validateOrderFormat(order); + } + } + + private void validateMenuExistence(final String input) { + String[] orders = input.split(","); + for (String order : orders) { + validateOrderExistence(order); + } + } + + private void validateDistinctMenu(final String input) { + Set<String> uniqueMenuItems = new HashSet<>(); + String[] orders = input.split(","); + for (String order : orders) { + validateDistinctMenu(order, uniqueMenuItems); + } + } + + private void validateMenuQuantity(final String input) { + String[] orders = input.split(","); + for (String order : orders) { + validateOrderQuantity(order); + } + } + + private void validateOrderFormat(final String order) { + Matcher matcher = pattern.matcher(order); + if (!matcher.matches()) { + Verifier.throwIllegalArgumentError(INVALID_ORDER_MESSAGE); + } + } + + private void validateOrderExistence(final String order) { + String[] orderInfo = order.split("-"); + if (Menu.getMenuItemByName(orderInfo[0]) == null) { + Verifier.throwIllegalArgumentError(INVALID_ORDER_MESSAGE); + } + } + + private void validateDistinctMenu(final String order, Set<String> uniqueMenuItems) { + String[] orderInfo = order.split("-"); + if (!uniqueMenuItems.add(orderInfo[0])) { + Verifier.throwIllegalArgumentError(INVALID_ORDER_MESSAGE); + } + } + + private void validateOrderQuantity(final String order) { + String[] orderInfo = order.split("-"); + try { + int quantity = Integer.parseInt(orderInfo[1]); + if (quantity < 1) { + Verifier.throwIllegalArgumentError(INVALID_ORDER_MESSAGE); + } + } catch (NumberFormatException e) { + Verifier.throwIllegalArgumentError(INVALID_ORDER_MESSAGE); + } + } + + +}
Java
์ €๋„ ๋ญ๊ฐ€ ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ธ์ง€ ๋ชฐ๋ผ์„œ ์ฐพ์•„๋ดค์—ˆ๋Š”๋ฐ, ์ƒ์„ฑ์ž์—์„œ ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜๋ฉด, ์ƒ์„ฑ์ž ์•ˆ์˜ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ด์ง€๊ณ , ์™ธ๋ถ€์—์„œ ๋‹ค๋ฅธ pattern๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๊ธฐ๊ฐ€ ์‰ฌ์›Œ์ง„๋‹ค๊ณ  ํ•˜๋”๋ผ๊ตฌ์š”. ๋ฐ˜๋ฉด์— ํ•„๋“œ์—์„œ ์ดˆ๊ธฐํ™”ํ•˜๋ฉด ๊ฐ€๋…์„ฑ๊ณผ ๊ฐ„๊ฒฐํ•จ์ด ์žฅ์ ์ด์ฃ . ์ƒ์„ฑ์ž์•ˆ์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋กœ์ง์ด ์—†์ง€๋งŒ ์ด๋ ‡๊ฒŒ๋„ ํ•œ๋ฒˆ ํ•ด๋ดค์–ด์š”! ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅด๊ฒŒ ํ•ด๋ณด๋ฉด ์ข‹์„๊ฑฐ ๊ฐ™์•„์š”!!
@@ -0,0 +1,14 @@ +package christmas.domain.verifier; + +public interface Verifier<T> { + + abstract public void validate(T input); + + static void throwIllegalArgumentError(String errorMessage) { + throw new IllegalArgumentException(errorMessage); + } + + static void throwIllegalStateError(String errorMessage) { + throw new IllegalStateException(errorMessage); + } +}
Java
ํ”ผ๋“œ๋ฐฑ ์ •๋ง ๊ฐ์‚ฌํ•ด์š”! ์„ธ์›…๋‹˜๋„ ํฌ๋ฆฌ์Šค๋งˆ์Šค ์ฝ”๋“œ ๋ฆฌ๋ทฐ ํ•ด๋“œ๋ฆด๋ ค๊ณ  ํ•˜๋Š”๋ฐ repository๊ฐ€ private์ธ๊ฐ€๋ด์š”๐Ÿ˜‚
@@ -0,0 +1,76 @@ +package christmas.domain.entity; + +import static christmas.domain.verifier.VisitDateVerifier.VISIT_DATE_VERIFIER; +import static christmas.system.Constant.CHRISTMAS; +import static christmas.system.Constant.FIRST_DAY_OF_MONTH; + +public class Day { + + private static final int WEEK_LENGTH = 7; + private final int day; + + public Day(int day) { + VISIT_DATE_VERIFIER.validateInputInDomain(day); + this.day = day; + } + + public int getDay() { + return day; + } + + public boolean isChristmasSeason() { + if (day >= FIRST_DAY_OF_MONTH && day <= CHRISTMAS) { + return true; + } + return false; + } + + public boolean isWeekday() { + if (!(isFriday() || isSaturday())) { + return true; + } + return false; + } + + public boolean isWeekend() { + if (isFriday() || isSaturday()) { + return true; + } + return false; + } + + public boolean isSpecialDay() { + if (isSunday() || isChristmas()) { + return true; + } + return false; + } + + private boolean isFriday() { + if (day % WEEK_LENGTH == 1) { + return true; + } + return false; + } + + private boolean isSaturday() { + if (day % WEEK_LENGTH == 2) { + return true; + } + return false; + } + + private boolean isSunday() { + if (day % WEEK_LENGTH == 3) { + return true; + } + return false; + } + + private boolean isChristmas() { + if (day == CHRISTMAS) { + return true; + } + return false; + } +}
Java
์˜ค... ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.! ๋ฆฌํŽ™ํ† ๋ง ํ•ด๋ณผ๊ฒŒ์š”
@@ -0,0 +1,12 @@ +package christmas.domain.util; + + +import java.text.DecimalFormat; + +public final class Util { + public static String createFormattedAmount(int target) { + DecimalFormat formatter = new DecimalFormat("###,##0"); + return formatter.format(target); + } + +}
Java
์˜ˆ๋ฅผ๋“ค์–ด, Input: ์ƒดํŽ˜์ธ- ์ด๋ ‡๊ฒŒ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, orderMenuVerifier์˜ checkMenuFormat ๋ฉ”์†Œ๋“œ์— ์˜ํ•ด ์˜ฌ๋ฐ”๋ฅธ ๋ฉ”๋‰ด ํ˜•์‹์ด ์•„๋‹Œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์–ด์ ธ๋ฒ„๋ ค ์œ„์˜ Util๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์—†๋„๋ก ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. <img width="474" alt="image" src="https://github.com/Hoo-sung/java-christmas-6-Hoo-sung/assets/121723421/e2a0a147-e665-4a6a-94ce-3876632e0d0b"> ์ด๋ ‡๊ฒŒ orderMenuVerifier๋กœ ๊ฒ€์ฆ์„ ๋งˆ์น˜๊ณ  Util class๋ฅผ ์ด์šฉํ•ด์„œ order๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.
@@ -1,152 +1,44 @@ package com.example.project.compile.service; +import com.example.project.compile.domain.CompileLanguage; import com.example.project.error.dto.ErrorMessage; -import org.springframework.beans.factory.annotation.Value; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.io.IOException; -import java.nio.file.*; -import java.util.UUID; +import java.nio.file.Files; +import java.nio.file.Path; +@Slf4j @Service +@RequiredArgsConstructor public class CompileService { - private final String code_dir; - public CompileService(@Value("${compile.url}") String codeDir) { - this.code_dir = codeDir; - } + private final FileService fileService; + private final CommandExecutorService commandExecutorService; - public String compileAndRun(String language, String code) throws IOException, InterruptedException { + public String compileAndRun(String language, String code, String codeDir) throws IOException { + CompileLanguage compileLanguage = CompileLanguage.JAVA.getByLanguageName(language); + Path filePath = null; try { - Path codePath = Paths.get(code_dir); - if (!Files.exists(codePath)) { - Files.createDirectories(codePath); + filePath = fileService.createCodeFile(code, compileLanguage, codeDir); + return executeCode(filePath); + } finally { + if (filePath != null) { + Files.deleteIfExists(filePath); } - - String filename = generateFileName(language); - Path filePath = codePath.resolve(filename); - - Files.write(filePath, code.getBytes()); - - return executeCode(language, filePath, codePath); - } catch (FileAlreadyExistsException e) { - throw new IOException(ErrorMessage.CODE_DIRECTORY_CREATION_FAILED.getMessage(), e); - } catch (IOException e) { - throw new IOException(ErrorMessage.CODE_WRITE_FAILED.getMessage(), e); } } - private String executeCode(String language, Path filePath, Path codePath) throws IOException, InterruptedException { + private String executeCode(Path filePath) throws IOException { + String result; try { - //TODO Enum ์‚ฌ์šฉํ•˜๊ธฐ - return switch (language.toLowerCase()) { - case "c" -> compileAndRun(filePath, "gcc", codePath.resolve("output").toString(), ""); - case "java" -> compileJava(filePath); - case "python" -> compilePython(filePath); - default -> throw new IllegalArgumentException(ErrorMessage.UNSUPPORTED_LANGUAGE.getMessage()); - }; - } catch (IllegalArgumentException e) { - throw new IllegalArgumentException(ErrorMessage.UNSUPPORTED_LANGUAGE.getMessage(), e); + String command = filePath.getParent().resolve("output").toString(); + result = commandExecutorService.runCommand(command); } catch (IOException | InterruptedException e) { throw new IOException(ErrorMessage.GENERAL_COMPILE_ERROR.getMessage(), e); } - } - - private String compileAndRun(Path filePath, String compiler, String outputFileName, String additionalArgs) throws IOException, InterruptedException { - String command = String.format("%s %s -o %s %s", compiler, filePath, outputFileName, additionalArgs); - Process compileProcess = Runtime.getRuntime().exec(command); - compileProcess.waitFor(); - - if (compileProcess.exitValue() == 0) { - return runCompiledProgram(outputFileName); - } else { - String errorOutput = new String(compileProcess.getErrorStream().readAllBytes()); - throw new IOException(ErrorMessage.COMPILATION_FAILED.getMessage() + "\n" + errorOutput); - } - } - - private String runCompiledProgram(String outputFileName) throws IOException, InterruptedException { - Process runProcess = Runtime.getRuntime().exec(outputFileName); - runProcess.waitFor(); - - if (runProcess.exitValue() == 0) { - return new String(runProcess.getInputStream().readAllBytes()); - } else { - String errorOutput = new String(runProcess.getErrorStream().readAllBytes()); - throw new IOException(ErrorMessage.EXECUTION_FAILED.getMessage() + "\n" + errorOutput); - } - } - - private String compileJava(Path filePath) throws IOException, InterruptedException { - String className = extractClassName(filePath); - Path correctFilePath = filePath.getParent().resolve(className + ".java"); - - try { - Files.move(filePath, correctFilePath, StandardCopyOption.REPLACE_EXISTING); - } catch (FileAlreadyExistsException e) { - throw new IOException(ErrorMessage.JAVA_FILE_RENAMING_FAILED.getMessage(), e); - } - - String command = String.format("javac %s", correctFilePath); - Process compileProcess = Runtime.getRuntime().exec(command); - compileProcess.waitFor(); - - if (compileProcess.exitValue() == 0) { - return runJavaProgram(correctFilePath); - } else { - String errorOutput = new String(compileProcess.getErrorStream().readAllBytes()); - throw new IOException(ErrorMessage.COMPILATION_FAILED.getMessage() + "\n" + errorOutput); - } - } - - private String extractClassName(Path filePath) throws IOException { - try { - String content = Files.readString(filePath); - return content.split("public class ")[1].split("\\s")[0].trim(); - } catch (Exception e) { - throw new IOException(ErrorMessage.JAVA_CLASS_EXTRACTION_FAILED.getMessage(), e); - } - } - - private String runJavaProgram(Path filePath) throws IOException, InterruptedException { - String className = filePath.getFileName().toString().replace(".java", ""); - String runCommand = String.format("java -cp %s %s", filePath.getParent(), className); - - Process runProcess = Runtime.getRuntime().exec(runCommand); - runProcess.waitFor(); - - if (runProcess.exitValue() == 0) { - return new String(runProcess.getInputStream().readAllBytes()); - } else { - String errorOutput = new String(runProcess.getErrorStream().readAllBytes()); - throw new IOException(ErrorMessage.EXECUTION_FAILED.getMessage() + "\n" + errorOutput); - } - } - - private String compilePython(Path filePath) throws IOException, InterruptedException { - String command = String.format("python %s", filePath); - Process process = Runtime.getRuntime().exec(command); - process.waitFor(); - - if (process.exitValue() == 0) { - return new String(process.getInputStream().readAllBytes()); - } else { - String errorOutput = new String(process.getErrorStream().readAllBytes()); - throw new IOException(ErrorMessage.EXECUTION_FAILED.getMessage() + "\n" + errorOutput); - } - } - - private String generateFileName(String language) { - return UUID.randomUUID() + getExtension(language); - } - - private String getExtension(String language) { - //TODO ENUM - return switch (language.toLowerCase()) { - case "c" -> ".c"; - case "java" -> ".java"; - case "python" -> ".py"; - default -> throw new IllegalArgumentException(ErrorMessage.UNSUPPORTED_LANGUAGE.getMessage()); - }; + return result; } } \ No newline at end of file
Java
enum์„ ์‚ฌ์šฉํ•ฉ์‹œ๋‹ค
@@ -1,152 +1,44 @@ package com.example.project.compile.service; +import com.example.project.compile.domain.CompileLanguage; import com.example.project.error.dto.ErrorMessage; -import org.springframework.beans.factory.annotation.Value; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.io.IOException; -import java.nio.file.*; -import java.util.UUID; +import java.nio.file.Files; +import java.nio.file.Path; +@Slf4j @Service +@RequiredArgsConstructor public class CompileService { - private final String code_dir; - public CompileService(@Value("${compile.url}") String codeDir) { - this.code_dir = codeDir; - } + private final FileService fileService; + private final CommandExecutorService commandExecutorService; - public String compileAndRun(String language, String code) throws IOException, InterruptedException { + public String compileAndRun(String language, String code, String codeDir) throws IOException { + CompileLanguage compileLanguage = CompileLanguage.JAVA.getByLanguageName(language); + Path filePath = null; try { - Path codePath = Paths.get(code_dir); - if (!Files.exists(codePath)) { - Files.createDirectories(codePath); + filePath = fileService.createCodeFile(code, compileLanguage, codeDir); + return executeCode(filePath); + } finally { + if (filePath != null) { + Files.deleteIfExists(filePath); } - - String filename = generateFileName(language); - Path filePath = codePath.resolve(filename); - - Files.write(filePath, code.getBytes()); - - return executeCode(language, filePath, codePath); - } catch (FileAlreadyExistsException e) { - throw new IOException(ErrorMessage.CODE_DIRECTORY_CREATION_FAILED.getMessage(), e); - } catch (IOException e) { - throw new IOException(ErrorMessage.CODE_WRITE_FAILED.getMessage(), e); } } - private String executeCode(String language, Path filePath, Path codePath) throws IOException, InterruptedException { + private String executeCode(Path filePath) throws IOException { + String result; try { - //TODO Enum ์‚ฌ์šฉํ•˜๊ธฐ - return switch (language.toLowerCase()) { - case "c" -> compileAndRun(filePath, "gcc", codePath.resolve("output").toString(), ""); - case "java" -> compileJava(filePath); - case "python" -> compilePython(filePath); - default -> throw new IllegalArgumentException(ErrorMessage.UNSUPPORTED_LANGUAGE.getMessage()); - }; - } catch (IllegalArgumentException e) { - throw new IllegalArgumentException(ErrorMessage.UNSUPPORTED_LANGUAGE.getMessage(), e); + String command = filePath.getParent().resolve("output").toString(); + result = commandExecutorService.runCommand(command); } catch (IOException | InterruptedException e) { throw new IOException(ErrorMessage.GENERAL_COMPILE_ERROR.getMessage(), e); } - } - - private String compileAndRun(Path filePath, String compiler, String outputFileName, String additionalArgs) throws IOException, InterruptedException { - String command = String.format("%s %s -o %s %s", compiler, filePath, outputFileName, additionalArgs); - Process compileProcess = Runtime.getRuntime().exec(command); - compileProcess.waitFor(); - - if (compileProcess.exitValue() == 0) { - return runCompiledProgram(outputFileName); - } else { - String errorOutput = new String(compileProcess.getErrorStream().readAllBytes()); - throw new IOException(ErrorMessage.COMPILATION_FAILED.getMessage() + "\n" + errorOutput); - } - } - - private String runCompiledProgram(String outputFileName) throws IOException, InterruptedException { - Process runProcess = Runtime.getRuntime().exec(outputFileName); - runProcess.waitFor(); - - if (runProcess.exitValue() == 0) { - return new String(runProcess.getInputStream().readAllBytes()); - } else { - String errorOutput = new String(runProcess.getErrorStream().readAllBytes()); - throw new IOException(ErrorMessage.EXECUTION_FAILED.getMessage() + "\n" + errorOutput); - } - } - - private String compileJava(Path filePath) throws IOException, InterruptedException { - String className = extractClassName(filePath); - Path correctFilePath = filePath.getParent().resolve(className + ".java"); - - try { - Files.move(filePath, correctFilePath, StandardCopyOption.REPLACE_EXISTING); - } catch (FileAlreadyExistsException e) { - throw new IOException(ErrorMessage.JAVA_FILE_RENAMING_FAILED.getMessage(), e); - } - - String command = String.format("javac %s", correctFilePath); - Process compileProcess = Runtime.getRuntime().exec(command); - compileProcess.waitFor(); - - if (compileProcess.exitValue() == 0) { - return runJavaProgram(correctFilePath); - } else { - String errorOutput = new String(compileProcess.getErrorStream().readAllBytes()); - throw new IOException(ErrorMessage.COMPILATION_FAILED.getMessage() + "\n" + errorOutput); - } - } - - private String extractClassName(Path filePath) throws IOException { - try { - String content = Files.readString(filePath); - return content.split("public class ")[1].split("\\s")[0].trim(); - } catch (Exception e) { - throw new IOException(ErrorMessage.JAVA_CLASS_EXTRACTION_FAILED.getMessage(), e); - } - } - - private String runJavaProgram(Path filePath) throws IOException, InterruptedException { - String className = filePath.getFileName().toString().replace(".java", ""); - String runCommand = String.format("java -cp %s %s", filePath.getParent(), className); - - Process runProcess = Runtime.getRuntime().exec(runCommand); - runProcess.waitFor(); - - if (runProcess.exitValue() == 0) { - return new String(runProcess.getInputStream().readAllBytes()); - } else { - String errorOutput = new String(runProcess.getErrorStream().readAllBytes()); - throw new IOException(ErrorMessage.EXECUTION_FAILED.getMessage() + "\n" + errorOutput); - } - } - - private String compilePython(Path filePath) throws IOException, InterruptedException { - String command = String.format("python %s", filePath); - Process process = Runtime.getRuntime().exec(command); - process.waitFor(); - - if (process.exitValue() == 0) { - return new String(process.getInputStream().readAllBytes()); - } else { - String errorOutput = new String(process.getErrorStream().readAllBytes()); - throw new IOException(ErrorMessage.EXECUTION_FAILED.getMessage() + "\n" + errorOutput); - } - } - - private String generateFileName(String language) { - return UUID.randomUUID() + getExtension(language); - } - - private String getExtension(String language) { - //TODO ENUM - return switch (language.toLowerCase()) { - case "c" -> ".c"; - case "java" -> ".java"; - case "python" -> ".py"; - default -> throw new IllegalArgumentException(ErrorMessage.UNSUPPORTED_LANGUAGE.getMessage()); - }; + return result; } } \ No newline at end of file
Java
ํด๋ž˜์Šค๊ฐ€ ๋„ˆ๋ฌด ํฝ๋‹ˆ๋‹ค. ์—ญํ• ์„ ๋‚˜๋ˆ ์ฃผ์„ธ์š”
@@ -1,36 +1,40 @@ package com.example.project.compile.controller.rest; +import com.example.project.auth.service.AuthTokenService; import com.example.project.common.dto.ResponseDto; import com.example.project.common.dto.ResponseMessage; -import com.example.project.solution.dto.request.user.SolutionCompileRequest; +import com.example.project.common.util.HeaderUtil; +import com.example.project.compile.dto.CompileRequest; import com.example.project.compile.service.CompileService; +import com.example.project.error.dto.ErrorMessage; +import com.example.project.error.dto.ErrorResponseDto; +import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; -import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import java.io.IOException; +@Slf4j @RestController @RequiredArgsConstructor @RequestMapping("/api/compile") public class CompileApiController { + private final CompileService compileService; + private final AuthTokenService authTokenService; @PostMapping - @PreAuthorize("hasAnyRole('USER', 'ADMIN')") - public ResponseEntity<?> compileCode(@RequestBody SolutionCompileRequest request) { - try { - String result = compileService.compileAndRun(request.getLanguage(), request.getCode()); + public ResponseEntity<?> compileCode(@RequestBody CompileRequest request, HttpServletRequest httpServletRequest, @Value("${compile.url}") String codeDir) throws IOException { + if (authTokenService.isValidateToken(HeaderUtil.resolveToken(httpServletRequest))) { + String result = compileService.compileAndRun( + request.getLanguage(), + request.getCode(), + codeDir + ); return ResponseDto.toResponseEntity(ResponseMessage.COMPILE_SUCCESS, result); - } catch (IllegalArgumentException e) { - return ResponseDto.toResponseEntity(ResponseMessage.INVALID_LANGUAGE, e.getMessage()); - } catch (IOException e) { - return ResponseDto.toResponseEntity(ResponseMessage.IO_ERROR, e.getMessage()); - } catch (InterruptedException e) { - return ResponseDto.toResponseEntity(ResponseMessage.EXECUTION_INTERRUPTED, e.getMessage()); - } catch (Exception e) { - return ResponseDto.toResponseEntity(ResponseMessage.GENERAL_COMPILE_ERROR, e.getMessage()); - } + } else return ErrorResponseDto.of(ErrorMessage.NOT_FOUND_CLIENT_ID_HEADER); } } \ No newline at end of file
Java
HttpServletRequest๋งŒ ๋ฐ›์•„์˜ค๋ฉด ํ† ํฐ ๊ฐ’์„ ๋”ฐ๋กœ ๋ฐ›์•„์˜ค์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,37 @@ +package com.example.project.compile.service; + +import com.example.project.compile.domain.CompileLanguage; +import com.example.project.error.dto.ErrorMessage; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.UUID; + +@Service +public class FileService { + + public Path createCodeFile(String code, CompileLanguage compileLanguage, String codeDir) throws IOException { + try { + Path codePath = Paths.get(codeDir); + if (!Files.exists(codePath)) { + Files.createDirectories(codePath); + } + + //UUID๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๊ณ ์œ ํ•œ ํŒŒ์ผ ์ด๋ฆ„์„ ์ƒ์„ฑํ•˜์—ฌ ํŒŒ์ผ์˜ ์ถฉ๋Œ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. + String filename = "Main_" + UUID.randomUUID() + compileLanguage.getExtension(); + + Path filePath = codePath.resolve(filename); + Files.writeString(filePath, code); + return filePath; + + } catch (InvalidPathException e) { + throw new IOException(ErrorMessage.INVALID_PATH_EXCEPTION.getMessage(), e); + } catch (SecurityException e) { + throw new IOException(ErrorMessage.ACCESS_PERMISSION_EXCEPTION.getMessage(), e); + } + } +} \ No newline at end of file
Java
yml ์— ์„ค์ •ํ•œ ๊ฐ’์„ ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”. ๊ทธ๋ฆฌ๊ณ  static์„ ์‚ฌ์šฉํ•  ํ•„์š”๋Š” ์—†์–ด๋ณด์ž…๋‹ˆ๋‹ค.
@@ -1,36 +1,40 @@ package com.example.project.compile.controller.rest; +import com.example.project.auth.service.AuthTokenService; import com.example.project.common.dto.ResponseDto; import com.example.project.common.dto.ResponseMessage; -import com.example.project.solution.dto.request.user.SolutionCompileRequest; +import com.example.project.common.util.HeaderUtil; +import com.example.project.compile.dto.CompileRequest; import com.example.project.compile.service.CompileService; +import com.example.project.error.dto.ErrorMessage; +import com.example.project.error.dto.ErrorResponseDto; +import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; -import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import java.io.IOException; +@Slf4j @RestController @RequiredArgsConstructor @RequestMapping("/api/compile") public class CompileApiController { + private final CompileService compileService; + private final AuthTokenService authTokenService; @PostMapping - @PreAuthorize("hasAnyRole('USER', 'ADMIN')") - public ResponseEntity<?> compileCode(@RequestBody SolutionCompileRequest request) { - try { - String result = compileService.compileAndRun(request.getLanguage(), request.getCode()); + public ResponseEntity<?> compileCode(@RequestBody CompileRequest request, HttpServletRequest httpServletRequest, @Value("${compile.url}") String codeDir) throws IOException { + if (authTokenService.isValidateToken(HeaderUtil.resolveToken(httpServletRequest))) { + String result = compileService.compileAndRun( + request.getLanguage(), + request.getCode(), + codeDir + ); return ResponseDto.toResponseEntity(ResponseMessage.COMPILE_SUCCESS, result); - } catch (IllegalArgumentException e) { - return ResponseDto.toResponseEntity(ResponseMessage.INVALID_LANGUAGE, e.getMessage()); - } catch (IOException e) { - return ResponseDto.toResponseEntity(ResponseMessage.IO_ERROR, e.getMessage()); - } catch (InterruptedException e) { - return ResponseDto.toResponseEntity(ResponseMessage.EXECUTION_INTERRUPTED, e.getMessage()); - } catch (Exception e) { - return ResponseDto.toResponseEntity(ResponseMessage.GENERAL_COMPILE_ERROR, e.getMessage()); - } + } else return ErrorResponseDto.of(ErrorMessage.NOT_FOUND_CLIENT_ID_HEADER); } } \ No newline at end of file
Java
์ปดํŒŒ์ผํ•˜๋Š” ์„œ๋ฒ„์—์„œ๋„ JWT ํ† ํฐ์„ ๊ฐ€์ง€๊ณ  ์œ ํšจํ•œ ์ ‘๊ทผ์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•ด์„œ ์ปดํŒŒ์ผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ์„ธ์š”
@@ -0,0 +1,37 @@ +package com.example.project.compile.service; + +import com.example.project.compile.domain.CompileLanguage; +import com.example.project.error.dto.ErrorMessage; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.UUID; + +@Service +public class FileService { + + public Path createCodeFile(String code, CompileLanguage compileLanguage, String codeDir) throws IOException { + try { + Path codePath = Paths.get(codeDir); + if (!Files.exists(codePath)) { + Files.createDirectories(codePath); + } + + //UUID๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๊ณ ์œ ํ•œ ํŒŒ์ผ ์ด๋ฆ„์„ ์ƒ์„ฑํ•˜์—ฌ ํŒŒ์ผ์˜ ์ถฉ๋Œ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. + String filename = "Main_" + UUID.randomUUID() + compileLanguage.getExtension(); + + Path filePath = codePath.resolve(filename); + Files.writeString(filePath, code); + return filePath; + + } catch (InvalidPathException e) { + throw new IOException(ErrorMessage.INVALID_PATH_EXCEPTION.getMessage(), e); + } catch (SecurityException e) { + throw new IOException(ErrorMessage.ACCESS_PERMISSION_EXCEPTION.getMessage(), e); + } + } +} \ No newline at end of file
Java
๋ณ€์ˆ˜๋ช…์€ ์นด๋ฉœ์ผ€์ด์Šค๋ฅผ ์ง€์ผœ์„œ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”
@@ -0,0 +1,54 @@ +const generateBasicToken = (userId: string, userPassword: string): string => { + const token = btoa(`${userId}:${userPassword}`); + return `Basic ${token}`; +}; + +const API_URL = `${import.meta.env.VITE_API_URL}`; +const USER_ID = `${import.meta.env.VITE_USER_ID}`; +const USER_PASSWORD = `${import.meta.env.VITE_USER_PASSWORD}`; + +try { + if (!API_URL || !USER_ID || !USER_PASSWORD) { + throw new Error( + "API_URL, USER_ID, PASSWORD environment variables are not set", + ); + } +} catch (error) { + console.log(error); +} + +type HttpMethod = "GET" | "POST" | "PATCH" | "DELETE"; + +interface RequestOptions { + method: HttpMethod; + body?: Record<string, unknown>; +} + +export const fetchWithAuth = async (path: string, options: RequestOptions) => { + const requestInit = requestBuilder(options); + const response = await fetch(path, requestInit); + + try { + if (!response.ok) { + throw new Error(`Failed to ${options.method} ${path}`); + } + } catch (error) { + console.log(error); + } + + return response; +}; + +const requestBuilder = ({ method, body }: RequestOptions): RequestInit => { + const token = generateBasicToken(USER_ID, USER_PASSWORD); + const headers: HeadersInit = { + "Content-Type": "application/json", + Authorization: token, + }; + + return { + method, + headers, + body: body ? JSON.stringify(body) : undefined, + }; +};
TypeScript
``` const API_URL = `${import.meta.env.VITE_API_URL}`; const USER_ID = `${import.meta.env.VITE_USER_ID}`; const USER_PASSWORD = `${import.meta.env.VITE_USER_PASSWORD}`; ``` ์ด ๋ถ€๋ถ„์€ ์ƒ์ˆ˜ ํŒŒ์ผ์—์„œ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•ด๋ณด๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”? `API_URL `์ด ์•„์ด๋Š” endPoint, fetchWithAuth์—์„œ ๋‘ ๋ฒˆ ์„ ์–ธ๋˜๋„ค์š”
@@ -0,0 +1,123 @@ +import { RULE } from "@constants/rules"; +import { + CART_ITEMS_COUNTS_ENDPOINT, + CART_ITEMS_ENDPOINT, + PRODUCTS_ENDPOINT, +} from "./endpoints"; +import { fetchWithAuth } from "./fetchWithAuth"; + +/** + * @example + * const params: QueryParams = { + * category: 'fashion', + * page: 1, + * size: 20, + * sort: ['price', 'asc'] + * } + */ +interface QueryParams { + [key: string]: + | undefined + | string + | number + | boolean + | (string | number | boolean)[]; +} + +export interface GetProductsParams { + category?: Category; + page?: number; + size?: number; + sort?: Sort; +} + +const createQueryString = (params: QueryParams) => { + return Object.entries(params) + .filter(([, value]) => value !== undefined) + .map(([key, value]) => { + if (Array.isArray(value)) { + return `${encodeURIComponent(key)}=${encodeURIComponent(value.join(","))}`; + } + return `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`; + }) + .join("&"); +}; + +export const getProducts = async ({ + category, + page = 0, + size = 20, + sort = "asc", +}: GetProductsParams = {}): Promise<Product[]> => { + const params = { + category, + page, + size, + sort: ["price", sort], + }; + const queryString = createQueryString(params) + RULE.sortQueryByIdAsc; + + const response = await fetchWithAuth(`${PRODUCTS_ENDPOINT}?${queryString}`, { + method: "GET", + }); + const data = await response.json(); + + if (!response.ok) { + throw new Error("Failed to get products item"); + } + + return data.content; +}; + +export const postProductInCart = async ( + productId: number, + quantity: number = 1, +) => { + const response = await fetchWithAuth(CART_ITEMS_ENDPOINT, { + method: "POST", + body: { + productId, + quantity, + }, + }); + + if (!response.ok) { + throw new Error("Failed to post product item in cart"); + } +}; + +export const deleteProductInCart = async (cartId: number) => { + const response = await fetchWithAuth(`${CART_ITEMS_ENDPOINT}/${cartId}`, { + method: "DELETE", + }); + + if (!response.ok) { + throw new Error("Failed to delete product item in cart"); + } +}; + +export const getCartItemsCount = async (): Promise<number> => { + const response = await fetchWithAuth(CART_ITEMS_COUNTS_ENDPOINT, { + method: "GET", + }); + const data = await response.json(); + + if (!response.ok) { + throw new Error("Failed to get cart items count"); + } + + return data; +}; + +export const getCartItems = async (): Promise<CartItem[]> => { + const response = await fetchWithAuth(CART_ITEMS_ENDPOINT, { + method: "GET", + }); + const data = await response.json(); + + if (!response.ok) { + throw new Error("Failed to get cart items count"); + } + + return data.content; +};
TypeScript
์š”์ฒญํ•ด์•ผํ•˜๋Š” api ๊ฐ€ ๋งŽ์•„์ง„๋‹ค๋ฉด, api/index.ts ์ฝ”๋“œ๊ฐ€ ๋งŽ์•„์ง€๊ฒ ๋„ค์š”. ๊ด€๋ จ์žˆ๋Š” api ์š”์ฒญ ๋ผ๋ฆฌ ๋ฌถ์–ด์„œ ํŒŒ์ผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ , index.ts์—์„œ ํ•ด๋‹น ํŒŒ์ผ๋“ค์˜ api ์š”์ฒญ ๋ฉ”์„œ๋“œ๋ฅผ export ํ•˜๋Š” ๊ฒƒ์„ ์–ด๋–จ๊นŒ์š”? ```dash api ใ„ด cart ใ„ด products ใ„ด index ```
@@ -0,0 +1,61 @@ +import { forwardRef, useContext } from "react"; +import * as PI from "./ProductItem.style"; +import CartControlButton from "../../Button/CartControlButton"; +import { deleteProductInCart, postProductInCart } from "@api/index"; +import { useError } from "@hooks/index"; +import { CartItemsContext } from "@context/CartItemsContext"; + +interface ProductProps { + product: Product; +} + +const ProductItem = forwardRef<HTMLDivElement, ProductProps>( + ({ product }, ref) => { + const { cartItems, refreshCartItems } = useContext(CartItemsContext); + + const cartItemIds = cartItems.map((item) => item.product.id); + const isInCart = cartItemIds.includes(product.id); + + const { showError } = useError(); + + const handleIsInCart = async () => { + try { + if (!isInCart) { + await postProductInCart(product.id); + refreshCartItems(); + return; + } + + const filteredItem = cartItems.find( + (item) => item.product.id === product.id, + ); + if (filteredItem) { + await deleteProductInCart(filteredItem.id); + refreshCartItems(); + } + } catch (error) { + if (error instanceof Error) { + showError(error.message); + } + } + }; + + return ( + <PI.ProductItemStyle ref={ref}> + <PI.ProductImg + src={`${product.imageUrl}`} + alt={`${product.name} ์ƒํ’ˆ ์ด๋ฏธ์ง€`} + /> + <PI.ProductGroup> + <PI.ProductContent> + <PI.ProductName>{product.name}</PI.ProductName> + <span>{product.price.toLocaleString("ko-kr")}์›</span> + </PI.ProductContent> + <CartControlButton onClick={handleIsInCart} isInCart={isInCart} /> + </PI.ProductGroup> + </PI.ProductItemStyle> + ); + }, +); + +export default ProductItem;
Unknown
์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๋Š” ๋กœ์ง๊ณผ ๋นผ๋Š” ๋กœ์ง์„ ํ•จ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•˜๊ณ , if else๋Œ€์‹  early return ์„ ์‚ฌ์šฉํ•ด๋ณด๋Š” ๊ฒƒ์„ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,61 @@ +import { forwardRef, useContext } from "react"; +import * as PI from "./ProductItem.style"; +import CartControlButton from "../../Button/CartControlButton"; +import { deleteProductInCart, postProductInCart } from "@api/index"; +import { useError } from "@hooks/index"; +import { CartItemsContext } from "@context/CartItemsContext"; + +interface ProductProps { + product: Product; +} + +const ProductItem = forwardRef<HTMLDivElement, ProductProps>( + ({ product }, ref) => { + const { cartItems, refreshCartItems } = useContext(CartItemsContext); + + const cartItemIds = cartItems.map((item) => item.product.id); + const isInCart = cartItemIds.includes(product.id); + + const { showError } = useError(); + + const handleIsInCart = async () => { + try { + if (!isInCart) { + await postProductInCart(product.id); + refreshCartItems(); + return; + } + + const filteredItem = cartItems.find( + (item) => item.product.id === product.id, + ); + if (filteredItem) { + await deleteProductInCart(filteredItem.id); + refreshCartItems(); + } + } catch (error) { + if (error instanceof Error) { + showError(error.message); + } + } + }; + + return ( + <PI.ProductItemStyle ref={ref}> + <PI.ProductImg + src={`${product.imageUrl}`} + alt={`${product.name} ์ƒํ’ˆ ์ด๋ฏธ์ง€`} + /> + <PI.ProductGroup> + <PI.ProductContent> + <PI.ProductName>{product.name}</PI.ProductName> + <span>{product.price.toLocaleString("ko-kr")}์›</span> + </PI.ProductContent> + <CartControlButton onClick={handleIsInCart} isInCart={isInCart} /> + </PI.ProductGroup> + </PI.ProductItemStyle> + ); + }, +); + +export default ProductItem;
Unknown
alt์— ๋Œ€ํ•ด์„œ ์ €๋ฒˆ ๋ฏธ์…˜์—์„œ ํ•˜๋ฃจ์—๊ฒŒ ๋ฐ›์€ ํ”ผ๋“œ๋ฐฑ์„ ๊ณต์œ ํ•  ๊ฒŒ์š”. image์˜ alt๋Š” 'ํฐ ์šด๋™ํ™”์— ๊ฒ€์€์ƒ‰ ์ค„์ด ํ•˜๋‚˜ ๋“ค์–ด๊ฐ„ ์•„๋””๋‹ค์Šค ์šด๋™ํ™”'์ฒ˜๋Ÿผ ํ•ด๋‹น ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ๊ตฌ์ฒด์ ์ธ ์„ค๋ช…์ด ๋“ค์–ด๊ฐ€์•ผํ•ด์š”. ํ•˜์ง€๋งŒ ํ˜„์žฌ ์„œ๋ฒ„์—์„œ ์ฃผ๋Š” ์ž์„ธํ•œ ์ƒํ’ˆ ์„ค๋ช…์ด ์—†๋Š” ์ƒํ™ฉ์—์„œ๋Š”`alt=''"`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ํ•˜๋„ค์š”. alt๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ๊ณผ alt์„ ๋นˆ๋ฌธ์ž์—ด๋กœ ๋‘๋Š” ๊ฒƒ์˜ ์ฐจ์ด๋ฅผ ๊ณต๋ถ€ํ•ด๋ณธ๋‹ค๋ฉด, ์†Œํ•˜์—๊ฒŒ ๋„์›€์ด ๋ ๊ฑฐ์—์š”. ๐Ÿ˜†
@@ -0,0 +1,44 @@ +import { FILTER_CATEGORIES, SORT_PRICE } from "@constants/rules"; +import * as PLH from "./ProductListHeader.style"; + +interface ProductListHeaderProps { + handleCategory: (category: Category) => void; + handleSort: (sort: Sort) => void; +} + +const ProductListHeader = ({ + handleCategory, + handleSort, +}: ProductListHeaderProps) => { + return ( + <PLH.Header> + <PLH.Title>bpple ์ƒํ’ˆ ๋ชฉ๋ก</PLH.Title> + <PLH.SelectBoxGroup> + <PLH.SelectBox + name="" + id="" + onChange={(e) => handleCategory(e.target.value as Category)} + > + {Object.entries(FILTER_CATEGORIES).map(([key, value]) => ( + <option value={key} key={key}> + {value} + </option> + ))} + </PLH.SelectBox> + <PLH.SelectBox + name="" + id="" + onChange={(e) => handleSort(e.target.value as Sort)} + > + {Object.entries(SORT_PRICE).map(([key, value]) => ( + <option value={key} key={key}> + {value} + </option> + ))} + </PLH.SelectBox> + </PLH.SelectBoxGroup> + </PLH.Header> + ); +}; + +export default ProductListHeader;
Unknown
selectbox ๋‘˜ ๋‹ค name,id๋ฅผ ๋นˆ๋ฌธ์ž์—ด๋กœ ๋‘” ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,44 @@ +import { FILTER_CATEGORIES, SORT_PRICE } from "@constants/rules"; +import * as PLH from "./ProductListHeader.style"; + +interface ProductListHeaderProps { + handleCategory: (category: Category) => void; + handleSort: (sort: Sort) => void; +} + +const ProductListHeader = ({ + handleCategory, + handleSort, +}: ProductListHeaderProps) => { + return ( + <PLH.Header> + <PLH.Title>bpple ์ƒํ’ˆ ๋ชฉ๋ก</PLH.Title> + <PLH.SelectBoxGroup> + <PLH.SelectBox + name="" + id="" + onChange={(e) => handleCategory(e.target.value as Category)} + > + {Object.entries(FILTER_CATEGORIES).map(([key, value]) => ( + <option value={key} key={key}> + {value} + </option> + ))} + </PLH.SelectBox> + <PLH.SelectBox + name="" + id="" + onChange={(e) => handleSort(e.target.value as Sort)} + > + {Object.entries(SORT_PRICE).map(([key, value]) => ( + <option value={key} key={key}> + {value} + </option> + ))} + </PLH.SelectBox> + </PLH.SelectBoxGroup> + </PLH.Header> + ); +}; + +export default ProductListHeader;
Unknown
selectbox๋ฅผ ํ•˜๋‚˜์˜ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ถ„๋ฆฌํ•ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.