code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,152 @@ +package christmas.domain; + +import christmas.domain.constants.Badge; +import christmas.domain.constants.Gift; +import christmas.domain.constants.event.EventDiscount; +import christmas.domain.constants.event.EventValue; +import christmas.domain.constants.menu.MenuGroup; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class Event { + private final Map<EventDiscount, Integer> eventDiscountGroup; + private final Map<Gift, Integer> gifts; + + public Event() { + this.eventDiscountGroup = initEventDiscounts(); + this.gifts = initGifts(); + } + + private Map<EventDiscount, Integer> initEventDiscounts() { + Map<EventDiscount, Integer> result = new HashMap<>(); + Arrays.stream(EventDiscount.values()) + .forEach(rank -> result.put(rank, 0)); + return result; + } + + private Map<Gift, Integer> initGifts() { + return new HashMap<>(); + } + + public void eventSetting(VisitDate visitDate, EventCalendar eventCalendar, StarDate starDate, Menu menu) { + if (menu.getTotalMenuPrice() >= EventValue.ORDER_MIN_PRICE.getValue()) { + giftSetting(visitDate, eventCalendar, menu); + eventDiscountSetting(visitDate, eventCalendar, starDate, menu); + } + } + + public void eventDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, StarDate starDate, Menu menu) { + weekdayDiscountSetting(visitDate, eventCalendar, menu); + weekendDiscountSetting(visitDate, eventCalendar, menu); + christmasDDayDiscountSetting(visitDate, eventCalendar); + specialDiscountSetting(visitDate, starDate); + eventDiscountGroupAddGift(); + } + + public void giftSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) { + if (isVisitDateAllDateEvent(visitDate, eventCalendar)) { + Arrays.stream(Gift.values()) + .filter(gift -> gift.isGiftApplicable(menu.getTotalMenuPrice())) + .filter(Gift::getGift) + .forEach(gift -> gifts.put(gift, gift.getCount() * gift.getPrice())); + } + } + + public void eventDiscountGroupAddGift() { + this.gifts.forEach((key, value) -> this.eventDiscountGroup.put( + EventDiscount.GIFT, + this.eventDiscountGroup.getOrDefault(EventDiscount.GIFT, 0) + value + )); + } + + public void weekdayDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) { + if (!visitDate.isWeekend(eventCalendar) && isVisitDateAllDateEvent(visitDate, eventCalendar)) { + this.eventDiscountGroup.put(EventDiscount.WEEKDAY, + this.eventDiscountGroup.getOrDefault( + EventDiscount.WEEKDAY, 0) + + menu.categoryMenuCount(MenuGroup.DESSERT) * EventDiscount.WEEKDAY.getDiscount() + ); + } + } + + public void weekendDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) { + if (visitDate.isWeekend(eventCalendar) && isVisitDateAllDateEvent(visitDate, eventCalendar)) { + this.eventDiscountGroup.put(EventDiscount.WEEKEND, + this.eventDiscountGroup.getOrDefault( + EventDiscount.WEEKEND, 0) + + menu.categoryMenuCount(MenuGroup.MAIN) * EventDiscount.WEEKEND.getDiscount()); + } + } + + public void christmasDDayDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar) { + if (isVisitDateChristmasDDay(visitDate, eventCalendar)) { + this.eventDiscountGroup.put(EventDiscount.CHRISTMAS, + this.eventDiscountGroup.getOrDefault( + EventDiscount.CHRISTMAS, 0) + christmasDDayCalculate(visitDate) + ); + } + } + + public void specialDiscountSetting(VisitDate visitDate, StarDate starDate) { + if (visitDate.containsStarDate(starDate)) { + this.eventDiscountGroup.put(EventDiscount.SPECIAL, + this.eventDiscountGroup.getOrDefault( + EventDiscount.SPECIAL, 0) + EventDiscount.SPECIAL.getDiscount() + ); + } + } + + public int christmasDDayCalculate(VisitDate visitDate) { + return EventDiscount.CHRISTMAS.getDiscount() + + (EventValue.CHRISTMAS_ON_THE_RISE_FORM_DAY.getValue() * + visitDate.christmasDDayCalculate()); + } + + public int getTotalGiftAmount() { + return this.gifts.values() + .stream() + .mapToInt(amount -> amount) + .sum(); + } + + public int getTotalEventDiscount() { + return this.eventDiscountGroup.entrySet() + .stream() + .filter(entry -> entry.getKey() != EventDiscount.GIFT) + .mapToInt(Map.Entry::getValue) + .sum(); + } + + public int getTotalBenefitsAmount() { + return this.eventDiscountGroup.values() + .stream() + .mapToInt(amount -> amount) + .sum(); + } + + public Badge getBadge() { + return Arrays.stream(Badge.values()) + .filter(badge -> badge.getWhichBadgeFromAmount(getTotalBenefitsAmount())) + .findFirst() + .orElse(Badge.NONE); + } + + public boolean isVisitDateChristmasDDay(VisitDate visitDate, EventCalendar eventCalendar) { + return visitDate.isChristmasDDay(eventCalendar); + } + + public boolean isVisitDateAllDateEvent(VisitDate visitDate, EventCalendar eventCalendar) { + return visitDate.containsAllDay(eventCalendar); + } + + public Map<EventDiscount, Integer> getEventDiscountGroup() { + return Collections.unmodifiableMap(eventDiscountGroup); + } + + public Map<Gift, Integer> getGifts() { + return Collections.unmodifiableMap(gifts); + } +}
Java
`Enum` ์„ `Map` ์œผ๋กœ ์‚ฌ์šฉํ• ๋•Œ๋Š” `EnumMap` ์ด ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ๊ณผ ์„ฑ๋Šฅ๋ฉด์—์„œ ๋” ์šฐ์ˆ˜ํ•ด์„œ `EnumMap` ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ๋„ ๊ณ ๋ คํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,152 @@ +package christmas.domain; + +import christmas.domain.constants.Badge; +import christmas.domain.constants.Gift; +import christmas.domain.constants.event.EventDiscount; +import christmas.domain.constants.event.EventValue; +import christmas.domain.constants.menu.MenuGroup; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class Event { + private final Map<EventDiscount, Integer> eventDiscountGroup; + private final Map<Gift, Integer> gifts; + + public Event() { + this.eventDiscountGroup = initEventDiscounts(); + this.gifts = initGifts(); + } + + private Map<EventDiscount, Integer> initEventDiscounts() { + Map<EventDiscount, Integer> result = new HashMap<>(); + Arrays.stream(EventDiscount.values()) + .forEach(rank -> result.put(rank, 0)); + return result; + } + + private Map<Gift, Integer> initGifts() { + return new HashMap<>(); + } + + public void eventSetting(VisitDate visitDate, EventCalendar eventCalendar, StarDate starDate, Menu menu) { + if (menu.getTotalMenuPrice() >= EventValue.ORDER_MIN_PRICE.getValue()) { + giftSetting(visitDate, eventCalendar, menu); + eventDiscountSetting(visitDate, eventCalendar, starDate, menu); + } + } + + public void eventDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, StarDate starDate, Menu menu) { + weekdayDiscountSetting(visitDate, eventCalendar, menu); + weekendDiscountSetting(visitDate, eventCalendar, menu); + christmasDDayDiscountSetting(visitDate, eventCalendar); + specialDiscountSetting(visitDate, starDate); + eventDiscountGroupAddGift(); + } + + public void giftSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) { + if (isVisitDateAllDateEvent(visitDate, eventCalendar)) { + Arrays.stream(Gift.values()) + .filter(gift -> gift.isGiftApplicable(menu.getTotalMenuPrice())) + .filter(Gift::getGift) + .forEach(gift -> gifts.put(gift, gift.getCount() * gift.getPrice())); + } + } + + public void eventDiscountGroupAddGift() { + this.gifts.forEach((key, value) -> this.eventDiscountGroup.put( + EventDiscount.GIFT, + this.eventDiscountGroup.getOrDefault(EventDiscount.GIFT, 0) + value + )); + } + + public void weekdayDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) { + if (!visitDate.isWeekend(eventCalendar) && isVisitDateAllDateEvent(visitDate, eventCalendar)) { + this.eventDiscountGroup.put(EventDiscount.WEEKDAY, + this.eventDiscountGroup.getOrDefault( + EventDiscount.WEEKDAY, 0) + + menu.categoryMenuCount(MenuGroup.DESSERT) * EventDiscount.WEEKDAY.getDiscount() + ); + } + } + + public void weekendDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) { + if (visitDate.isWeekend(eventCalendar) && isVisitDateAllDateEvent(visitDate, eventCalendar)) { + this.eventDiscountGroup.put(EventDiscount.WEEKEND, + this.eventDiscountGroup.getOrDefault( + EventDiscount.WEEKEND, 0) + + menu.categoryMenuCount(MenuGroup.MAIN) * EventDiscount.WEEKEND.getDiscount()); + } + } + + public void christmasDDayDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar) { + if (isVisitDateChristmasDDay(visitDate, eventCalendar)) { + this.eventDiscountGroup.put(EventDiscount.CHRISTMAS, + this.eventDiscountGroup.getOrDefault( + EventDiscount.CHRISTMAS, 0) + christmasDDayCalculate(visitDate) + ); + } + } + + public void specialDiscountSetting(VisitDate visitDate, StarDate starDate) { + if (visitDate.containsStarDate(starDate)) { + this.eventDiscountGroup.put(EventDiscount.SPECIAL, + this.eventDiscountGroup.getOrDefault( + EventDiscount.SPECIAL, 0) + EventDiscount.SPECIAL.getDiscount() + ); + } + } + + public int christmasDDayCalculate(VisitDate visitDate) { + return EventDiscount.CHRISTMAS.getDiscount() + + (EventValue.CHRISTMAS_ON_THE_RISE_FORM_DAY.getValue() * + visitDate.christmasDDayCalculate()); + } + + public int getTotalGiftAmount() { + return this.gifts.values() + .stream() + .mapToInt(amount -> amount) + .sum(); + } + + public int getTotalEventDiscount() { + return this.eventDiscountGroup.entrySet() + .stream() + .filter(entry -> entry.getKey() != EventDiscount.GIFT) + .mapToInt(Map.Entry::getValue) + .sum(); + } + + public int getTotalBenefitsAmount() { + return this.eventDiscountGroup.values() + .stream() + .mapToInt(amount -> amount) + .sum(); + } + + public Badge getBadge() { + return Arrays.stream(Badge.values()) + .filter(badge -> badge.getWhichBadgeFromAmount(getTotalBenefitsAmount())) + .findFirst() + .orElse(Badge.NONE); + } + + public boolean isVisitDateChristmasDDay(VisitDate visitDate, EventCalendar eventCalendar) { + return visitDate.isChristmasDDay(eventCalendar); + } + + public boolean isVisitDateAllDateEvent(VisitDate visitDate, EventCalendar eventCalendar) { + return visitDate.containsAllDay(eventCalendar); + } + + public Map<EventDiscount, Integer> getEventDiscountGroup() { + return Collections.unmodifiableMap(eventDiscountGroup); + } + + public Map<Gift, Integer> getGifts() { + return Collections.unmodifiableMap(gifts); + } +}
Java
๊ฐœ์ธ์ ์œผ๋กœ `Event` ํด๋ž˜์Šค๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์€ ์ฑ…์ž„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ `Event` ํด๋ž˜์Šค๋Š” 1. ๋ฐฉ๋ฌธํ•˜๋Š” ๋‚ ์งœ์™€ ๋งค๋‰ด๋ฅผ ๊ฐ€์ง€๊ณ  ์กด์žฌํ•˜๋Š” 4๊ฐœ์˜ ์ด๋ฒคํŠธ๋“ค์— ํ•ด๋‹นํ•˜๋Š”์ง€ ์—ฌ๋ถ€ ํŒ๋ณ„ 2. ๊ฐ์ข… ํ• ์ธ๊ธˆ์•ก ๊ณ„์‚ฐ 3. ์‚ฌ์šฉ์ž์˜ ๋ฐฉ๋ฌธ์— ๋”ฐ๋ฅธ ์ด๋ฒคํŠธ ๊ฒฐ๊ณผ๊ฐ์ฒด ์ƒ์„ฑ 4. ์ฆ์ •ํ’ˆ ์ •๋ณด ์ถ”๊ฐ€ 5. ํ• ์ธ ๊ธˆ์•ก์— ๋”ฐ๋ฅธ ๋ฐฐ์ง€ ์ •๋ณด ์ถ”๊ฐ€ ์˜ ๊ธฐ๋Šฅ๋“ค์„ ํ•˜๊ณ  ์žˆ๋Š”๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ํ•˜๋‚˜์˜ ํด๋ž˜์Šค์— ๋งŽ์€ ๊ธฐ๋Šฅ์„ ๋„ฃ์œผ๋ฉด ๋ด์•ผํ•  ํด๋ž˜์Šค๋“ค์ด ์ƒ๋Œ€์ ์œผ๋กœ ์ ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ํŽธํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ถ”ํ›„ Event ์˜ ์š”๊ตฌ์‚ฌํ•ญ์ด ๋ฐ”๋€Œ๊ฑฐ๋‚˜ ํ•  ๊ฒฝ์šฐ ์†๋ด์•ผํ•  ๋ถ€๋ถ„์ด ๋งŽ์•„์งˆ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ ์ž์ฒด๋„ 150 ์ค„์— ๋‹ฌํ•˜๊ณ  ์žˆ์–ด์„œ ์—ญํ• ๊ณผ ์ฑ…์ž„์˜ ๋ถ„๋ฆฌ๋ฅผ ๊ณ ๋ คํ•˜์‹œ๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,43 @@ +package christmas.domain; + +import christmas.domain.constants.event.EventValue; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +public class EventCalendar { + private final LocalDate currentDate; + + public EventCalendar(int year, int month) { + this.currentDate = LocalDate.of(year, month, 1); + } + + public boolean isWeekend(int day) { + DayOfWeek dayOfWeek = currentDate.withDayOfMonth(day).getDayOfWeek(); + return dayOfWeek == DayOfWeek.FRIDAY || dayOfWeek == DayOfWeek.SATURDAY; + } + + public boolean isChristmasDDay(int day) { + return day >= EventValue.CHRISTMAS_START_DAY.getValue() && day <= EventValue.CHRISTMAS_END_DAY.getValue(); + } + + public boolean containsAllDay(int day) { + return day >= EventValue.DECEMBER_START_DAY.getValue() && day <= EventValue.DECEMBER_END_DAY.getValue(); + } + + public int getYear() { + return currentDate.getYear(); + } + + public int getMonth() { + return currentDate.getMonthValue(); + } + + public int getStartDate() { + return currentDate.getDayOfMonth(); + } + + public int getEndDate() { + return currentDate.lengthOfMonth(); + } +}
Java
์ด๋ฒคํŠธ์— ๋งž๋Š” ๋‚ ์งœ๋ฅผ ์„ธํŒ…ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ •๋ง ์ข‹์€ ๋ฐฉ๋ฒ• ๊ฐ™์Šต๋‹ˆ๋‹ค ๐Ÿ‘ ์ด ๋ฐฉ๋ฒ•์ด ํ›จ์”ฌ ๊น”๋”ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํŽธํ•œ๋ฐฉ๋ฒ• ๊ฐ™์•„์š”.
@@ -0,0 +1,115 @@ +package christmas.domain; + +import christmas.domain.constants.event.EventValue; +import christmas.domain.constants.menu.MenuGroup; +import christmas.domain.constants.menu.MenuInterface; +import christmas.message.ErrorMessage; +import christmas.util.NumericConverter; +import christmas.view.constants.OutputMessage; + +import java.util.*; + +public class Menu { + private static final int MAX_MENU_COUNT = 20; + + private final Map<String, Map<MenuInterface, Integer>> menus; + + public Menu(String menuInput) { + String[] menuCommaSplit = menuInput.split(OutputMessage.COMMA.getMessage()); + validateMenuCountLimits(menuCommaSplit); + validateDuplicateMenu(menuCommaSplit); + this.menus = menuSetting(menuCommaSplit); + validateOnlyBeverage(); + } + + public Map<String, Map<MenuInterface, Integer>> menuSetting(String[] menuNameAndCountArr) { + Map<String, Map<MenuInterface, Integer>> menuGroups = new HashMap<>(); + Arrays.stream(menuNameAndCountArr) + .forEach(menuNameAndCount -> { + String menuName = menuNameAndCount.split(OutputMessage.HYPHEN.getMessage())[0]; + String menuCount = menuNameAndCount.split(OutputMessage.HYPHEN.getMessage())[1]; + MenuGroup menuGroup = MenuGroup.findMenuCategory(menuName); + menuGroups.computeIfAbsent(menuGroup.getTitle(), k -> new HashMap<>()) + .put(menuGroup.getMenuByName(menuName), Integer.parseInt(menuCount)); + }); + return menuGroups; + } + + public int validateMenuCount(String menuCount) { + NumericConverter numericConverter = new NumericConverter(); + try { + int count = numericConverter.convert(menuCount); + validateMenuCountIsPositive(count); + return count; + } catch (NumberFormatException e) { + throw new IllegalArgumentException(ErrorMessage.INVALID_MENU_COUNT.getReasonFormattedMessage()); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException(e.getMessage()); + + } + } + + public void validateMenuCountIsPositive(int menuCount) { + if (menuCount <= 0) { + throw new IllegalArgumentException(ErrorMessage.INVALID_MINIMUM_MENU_COUNT.getMessage()); + } + } + + public void validateDuplicateMenu(String[] menuNameAndCount) { + List<String> menuNames = Arrays.stream(menuNameAndCount) + .map(nameAndCount -> nameAndCount.split(OutputMessage.HYPHEN.getMessage())[0].trim()) + .toList(); + if (menuNames.size() != menuNames.stream().distinct().count()) { + throw new IllegalArgumentException(ErrorMessage.INVALID_DUPLICATE_MENU.getReasonFormattedMessage()); + } + } + + public void validateOnlyBeverage() { + if (this.menus.entrySet().stream() + .allMatch(entry -> entry.getKey().equals(MenuGroup.BEVERAGE.getTitle()))) { + throw new IllegalArgumentException(ErrorMessage.INVALID_ONLY_BEVERAGE.getReasonFormattedMessage()); + } + } + + public void validateMenuCountLimits(String[] menuNameAndCount) { + if (Arrays.stream(menuNameAndCount) + .mapToInt(nameAndCount -> validateMenuCount( + nameAndCount.split(OutputMessage.HYPHEN.getMessage())[1]) + ) + .sum() > MAX_MENU_COUNT) { + throw new IllegalArgumentException(ErrorMessage.INVALID_MENU_COUNT_LIMITS.getReasonFormattedMessage()); + } + } + + public int getTotalMenuPrice() { + return this.menus.values() + .stream() + .flatMap(menuGroup -> menuGroup.entrySet().stream()) + .mapToInt(menu -> menu.getKey().getPrice() * menu.getValue()) + .sum(); + } + + public boolean isTotalPriceTenThousandOrMore() { + return getTotalMenuPrice() >= EventValue.ORDER_MIN_PRICE.getValue(); + } + + public int categoryMenuCount(MenuGroup categoryMenu) { + return this.menus.entrySet() + .stream() + .filter(menuGroup -> menuGroup.getKey().equals(categoryMenu.getTitle())) + .mapToInt(menuGroup -> menuGroup.getValue().values().stream().mapToInt(Integer::intValue).sum()) + .sum(); + } + + @Override + public String toString() { + StringJoiner stringJoiner = new StringJoiner("\n"); + this.menus.forEach((key, value) -> value.forEach((menu, count) -> + stringJoiner.add( + menu.getName() + + OutputMessage.BLANK.getMessage() + + String.format(OutputMessage.COUNT.getMessage(), count)) + )); + return stringJoiner.toString(); + } +}
Java
`menus` ๋ผ๋Š” ๊ฐ์ฒด๋Š” ํ˜„์žฌ ์ฃผ๋ฌธํ•œ ๋งค๋‰ด๋“ค์„ ๋‹ด์€ ๊ฐ์ฒด๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ๋งค๋‰ด๋ผ๋Š” ์ด๋ฆ„์˜ ๊ฐ์ฒด ์•ˆ์— ํ˜„์žฌ ์ฃผ๋ฌธํ•œ ๋งค๋‰ด๋“ค์ด ์žˆ๋Š” ๊ตฌ์กฐ๊ฐ€ ์กฐ๊ธˆ ์–ด์ƒ‰ํ•˜๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค. ๋งค๋‰ด๋“ค์„ ์ถ”์ƒํ™”ํ•œ `MenuInterface` ๊ฐ€ ์žˆ์–ด์„œ ์ง€๊ธˆ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์‹ ๊ฒŒ ์•„๋‹๊นŒ ํ•˜๋Š” ์ƒ๊ฐ์ด ๋“œ๋Š”๋ฐ ์ด ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด์ธ `~Menu` ๋„ ์กด์žฌํ•˜๋Š” ๋งŒํผ ํด๋ž˜์Šค๋ฅผ ์ข€๋” ์ง๊ด€์ ์ธ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ฐ”๊พธ๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”? ์˜ˆ๋ฅผ๋“ค๋ฉด `Order` ๊ฐ™์€ ์ด๋ฆ„๋„ ์ข‹์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,115 @@ +package christmas.domain; + +import christmas.domain.constants.event.EventValue; +import christmas.domain.constants.menu.MenuGroup; +import christmas.domain.constants.menu.MenuInterface; +import christmas.message.ErrorMessage; +import christmas.util.NumericConverter; +import christmas.view.constants.OutputMessage; + +import java.util.*; + +public class Menu { + private static final int MAX_MENU_COUNT = 20; + + private final Map<String, Map<MenuInterface, Integer>> menus; + + public Menu(String menuInput) { + String[] menuCommaSplit = menuInput.split(OutputMessage.COMMA.getMessage()); + validateMenuCountLimits(menuCommaSplit); + validateDuplicateMenu(menuCommaSplit); + this.menus = menuSetting(menuCommaSplit); + validateOnlyBeverage(); + } + + public Map<String, Map<MenuInterface, Integer>> menuSetting(String[] menuNameAndCountArr) { + Map<String, Map<MenuInterface, Integer>> menuGroups = new HashMap<>(); + Arrays.stream(menuNameAndCountArr) + .forEach(menuNameAndCount -> { + String menuName = menuNameAndCount.split(OutputMessage.HYPHEN.getMessage())[0]; + String menuCount = menuNameAndCount.split(OutputMessage.HYPHEN.getMessage())[1]; + MenuGroup menuGroup = MenuGroup.findMenuCategory(menuName); + menuGroups.computeIfAbsent(menuGroup.getTitle(), k -> new HashMap<>()) + .put(menuGroup.getMenuByName(menuName), Integer.parseInt(menuCount)); + }); + return menuGroups; + } + + public int validateMenuCount(String menuCount) { + NumericConverter numericConverter = new NumericConverter(); + try { + int count = numericConverter.convert(menuCount); + validateMenuCountIsPositive(count); + return count; + } catch (NumberFormatException e) { + throw new IllegalArgumentException(ErrorMessage.INVALID_MENU_COUNT.getReasonFormattedMessage()); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException(e.getMessage()); + + } + } + + public void validateMenuCountIsPositive(int menuCount) { + if (menuCount <= 0) { + throw new IllegalArgumentException(ErrorMessage.INVALID_MINIMUM_MENU_COUNT.getMessage()); + } + } + + public void validateDuplicateMenu(String[] menuNameAndCount) { + List<String> menuNames = Arrays.stream(menuNameAndCount) + .map(nameAndCount -> nameAndCount.split(OutputMessage.HYPHEN.getMessage())[0].trim()) + .toList(); + if (menuNames.size() != menuNames.stream().distinct().count()) { + throw new IllegalArgumentException(ErrorMessage.INVALID_DUPLICATE_MENU.getReasonFormattedMessage()); + } + } + + public void validateOnlyBeverage() { + if (this.menus.entrySet().stream() + .allMatch(entry -> entry.getKey().equals(MenuGroup.BEVERAGE.getTitle()))) { + throw new IllegalArgumentException(ErrorMessage.INVALID_ONLY_BEVERAGE.getReasonFormattedMessage()); + } + } + + public void validateMenuCountLimits(String[] menuNameAndCount) { + if (Arrays.stream(menuNameAndCount) + .mapToInt(nameAndCount -> validateMenuCount( + nameAndCount.split(OutputMessage.HYPHEN.getMessage())[1]) + ) + .sum() > MAX_MENU_COUNT) { + throw new IllegalArgumentException(ErrorMessage.INVALID_MENU_COUNT_LIMITS.getReasonFormattedMessage()); + } + } + + public int getTotalMenuPrice() { + return this.menus.values() + .stream() + .flatMap(menuGroup -> menuGroup.entrySet().stream()) + .mapToInt(menu -> menu.getKey().getPrice() * menu.getValue()) + .sum(); + } + + public boolean isTotalPriceTenThousandOrMore() { + return getTotalMenuPrice() >= EventValue.ORDER_MIN_PRICE.getValue(); + } + + public int categoryMenuCount(MenuGroup categoryMenu) { + return this.menus.entrySet() + .stream() + .filter(menuGroup -> menuGroup.getKey().equals(categoryMenu.getTitle())) + .mapToInt(menuGroup -> menuGroup.getValue().values().stream().mapToInt(Integer::intValue).sum()) + .sum(); + } + + @Override + public String toString() { + StringJoiner stringJoiner = new StringJoiner("\n"); + this.menus.forEach((key, value) -> value.forEach((menu, count) -> + stringJoiner.add( + menu.getName() + + OutputMessage.BLANK.getMessage() + + String.format(OutputMessage.COUNT.getMessage(), count)) + )); + return stringJoiner.toString(); + } +}
Java
`Menu` ์ž…๋ ฅ์— ๋Œ€ํ•œ ๊ฒ€์ฆ๋ถ€๋ถ„์ด ์—ฌ๊ธฐ์— ์กด์žฌํ–ˆ๊ตฐ์š”! ์ €๋Š” 4์ฃผ์ฐจ์˜ ๊ณผ์ œ ๋‚ด๋‚ด ์ž…๋ ฅ๊ฐ’์— ๋Œ€ํ•œ ๊ฒ€์ฆ ๋กœ์ง์€ ํ•ญ์ƒ ๋ถ„๋ฆฌํ•ด์„œ ๊ตฌํ˜„ํ•˜๋Š”๊ฒŒ ์˜ณ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ๊ทธ๋ ‡๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ์˜์„๋‹˜์˜ ์ƒ๊ฐ์€ ์–ด๋–ค์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,115 @@ +package christmas.domain; + +import christmas.domain.constants.event.EventValue; +import christmas.domain.constants.menu.MenuGroup; +import christmas.domain.constants.menu.MenuInterface; +import christmas.message.ErrorMessage; +import christmas.util.NumericConverter; +import christmas.view.constants.OutputMessage; + +import java.util.*; + +public class Menu { + private static final int MAX_MENU_COUNT = 20; + + private final Map<String, Map<MenuInterface, Integer>> menus; + + public Menu(String menuInput) { + String[] menuCommaSplit = menuInput.split(OutputMessage.COMMA.getMessage()); + validateMenuCountLimits(menuCommaSplit); + validateDuplicateMenu(menuCommaSplit); + this.menus = menuSetting(menuCommaSplit); + validateOnlyBeverage(); + } + + public Map<String, Map<MenuInterface, Integer>> menuSetting(String[] menuNameAndCountArr) { + Map<String, Map<MenuInterface, Integer>> menuGroups = new HashMap<>(); + Arrays.stream(menuNameAndCountArr) + .forEach(menuNameAndCount -> { + String menuName = menuNameAndCount.split(OutputMessage.HYPHEN.getMessage())[0]; + String menuCount = menuNameAndCount.split(OutputMessage.HYPHEN.getMessage())[1]; + MenuGroup menuGroup = MenuGroup.findMenuCategory(menuName); + menuGroups.computeIfAbsent(menuGroup.getTitle(), k -> new HashMap<>()) + .put(menuGroup.getMenuByName(menuName), Integer.parseInt(menuCount)); + }); + return menuGroups; + } + + public int validateMenuCount(String menuCount) { + NumericConverter numericConverter = new NumericConverter(); + try { + int count = numericConverter.convert(menuCount); + validateMenuCountIsPositive(count); + return count; + } catch (NumberFormatException e) { + throw new IllegalArgumentException(ErrorMessage.INVALID_MENU_COUNT.getReasonFormattedMessage()); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException(e.getMessage()); + + } + } + + public void validateMenuCountIsPositive(int menuCount) { + if (menuCount <= 0) { + throw new IllegalArgumentException(ErrorMessage.INVALID_MINIMUM_MENU_COUNT.getMessage()); + } + } + + public void validateDuplicateMenu(String[] menuNameAndCount) { + List<String> menuNames = Arrays.stream(menuNameAndCount) + .map(nameAndCount -> nameAndCount.split(OutputMessage.HYPHEN.getMessage())[0].trim()) + .toList(); + if (menuNames.size() != menuNames.stream().distinct().count()) { + throw new IllegalArgumentException(ErrorMessage.INVALID_DUPLICATE_MENU.getReasonFormattedMessage()); + } + } + + public void validateOnlyBeverage() { + if (this.menus.entrySet().stream() + .allMatch(entry -> entry.getKey().equals(MenuGroup.BEVERAGE.getTitle()))) { + throw new IllegalArgumentException(ErrorMessage.INVALID_ONLY_BEVERAGE.getReasonFormattedMessage()); + } + } + + public void validateMenuCountLimits(String[] menuNameAndCount) { + if (Arrays.stream(menuNameAndCount) + .mapToInt(nameAndCount -> validateMenuCount( + nameAndCount.split(OutputMessage.HYPHEN.getMessage())[1]) + ) + .sum() > MAX_MENU_COUNT) { + throw new IllegalArgumentException(ErrorMessage.INVALID_MENU_COUNT_LIMITS.getReasonFormattedMessage()); + } + } + + public int getTotalMenuPrice() { + return this.menus.values() + .stream() + .flatMap(menuGroup -> menuGroup.entrySet().stream()) + .mapToInt(menu -> menu.getKey().getPrice() * menu.getValue()) + .sum(); + } + + public boolean isTotalPriceTenThousandOrMore() { + return getTotalMenuPrice() >= EventValue.ORDER_MIN_PRICE.getValue(); + } + + public int categoryMenuCount(MenuGroup categoryMenu) { + return this.menus.entrySet() + .stream() + .filter(menuGroup -> menuGroup.getKey().equals(categoryMenu.getTitle())) + .mapToInt(menuGroup -> menuGroup.getValue().values().stream().mapToInt(Integer::intValue).sum()) + .sum(); + } + + @Override + public String toString() { + StringJoiner stringJoiner = new StringJoiner("\n"); + this.menus.forEach((key, value) -> value.forEach((menu, count) -> + stringJoiner.add( + menu.getName() + + OutputMessage.BLANK.getMessage() + + String.format(OutputMessage.COUNT.getMessage(), count)) + )); + return stringJoiner.toString(); + } +}
Java
์ด๋ฉ”์ผ๋กœ ์™”๋˜ ํ”ผ๋“œ๋ฐฑ์ค‘์— ๋ณ€์ˆ˜๋ช…์— ์ž๋ฃŒํ˜•์„ ๋„ฃ์ง€ ๋ง๋ผ๋Š” ํ”ผ๋“œ๋ฐฑ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค! ๋’ค์— `Arr` ์€ ๋นผ๋„ ์ข‹์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ๐Ÿ˜„
@@ -0,0 +1,152 @@ +package christmas.domain; + +import christmas.domain.constants.Badge; +import christmas.domain.constants.Gift; +import christmas.domain.constants.event.EventDiscount; +import christmas.domain.constants.event.EventValue; +import christmas.domain.constants.menu.MenuGroup; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class Event { + private final Map<EventDiscount, Integer> eventDiscountGroup; + private final Map<Gift, Integer> gifts; + + public Event() { + this.eventDiscountGroup = initEventDiscounts(); + this.gifts = initGifts(); + } + + private Map<EventDiscount, Integer> initEventDiscounts() { + Map<EventDiscount, Integer> result = new HashMap<>(); + Arrays.stream(EventDiscount.values()) + .forEach(rank -> result.put(rank, 0)); + return result; + } + + private Map<Gift, Integer> initGifts() { + return new HashMap<>(); + } + + public void eventSetting(VisitDate visitDate, EventCalendar eventCalendar, StarDate starDate, Menu menu) { + if (menu.getTotalMenuPrice() >= EventValue.ORDER_MIN_PRICE.getValue()) { + giftSetting(visitDate, eventCalendar, menu); + eventDiscountSetting(visitDate, eventCalendar, starDate, menu); + } + } + + public void eventDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, StarDate starDate, Menu menu) { + weekdayDiscountSetting(visitDate, eventCalendar, menu); + weekendDiscountSetting(visitDate, eventCalendar, menu); + christmasDDayDiscountSetting(visitDate, eventCalendar); + specialDiscountSetting(visitDate, starDate); + eventDiscountGroupAddGift(); + } + + public void giftSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) { + if (isVisitDateAllDateEvent(visitDate, eventCalendar)) { + Arrays.stream(Gift.values()) + .filter(gift -> gift.isGiftApplicable(menu.getTotalMenuPrice())) + .filter(Gift::getGift) + .forEach(gift -> gifts.put(gift, gift.getCount() * gift.getPrice())); + } + } + + public void eventDiscountGroupAddGift() { + this.gifts.forEach((key, value) -> this.eventDiscountGroup.put( + EventDiscount.GIFT, + this.eventDiscountGroup.getOrDefault(EventDiscount.GIFT, 0) + value + )); + } + + public void weekdayDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) { + if (!visitDate.isWeekend(eventCalendar) && isVisitDateAllDateEvent(visitDate, eventCalendar)) { + this.eventDiscountGroup.put(EventDiscount.WEEKDAY, + this.eventDiscountGroup.getOrDefault( + EventDiscount.WEEKDAY, 0) + + menu.categoryMenuCount(MenuGroup.DESSERT) * EventDiscount.WEEKDAY.getDiscount() + ); + } + } + + public void weekendDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) { + if (visitDate.isWeekend(eventCalendar) && isVisitDateAllDateEvent(visitDate, eventCalendar)) { + this.eventDiscountGroup.put(EventDiscount.WEEKEND, + this.eventDiscountGroup.getOrDefault( + EventDiscount.WEEKEND, 0) + + menu.categoryMenuCount(MenuGroup.MAIN) * EventDiscount.WEEKEND.getDiscount()); + } + } + + public void christmasDDayDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar) { + if (isVisitDateChristmasDDay(visitDate, eventCalendar)) { + this.eventDiscountGroup.put(EventDiscount.CHRISTMAS, + this.eventDiscountGroup.getOrDefault( + EventDiscount.CHRISTMAS, 0) + christmasDDayCalculate(visitDate) + ); + } + } + + public void specialDiscountSetting(VisitDate visitDate, StarDate starDate) { + if (visitDate.containsStarDate(starDate)) { + this.eventDiscountGroup.put(EventDiscount.SPECIAL, + this.eventDiscountGroup.getOrDefault( + EventDiscount.SPECIAL, 0) + EventDiscount.SPECIAL.getDiscount() + ); + } + } + + public int christmasDDayCalculate(VisitDate visitDate) { + return EventDiscount.CHRISTMAS.getDiscount() + + (EventValue.CHRISTMAS_ON_THE_RISE_FORM_DAY.getValue() * + visitDate.christmasDDayCalculate()); + } + + public int getTotalGiftAmount() { + return this.gifts.values() + .stream() + .mapToInt(amount -> amount) + .sum(); + } + + public int getTotalEventDiscount() { + return this.eventDiscountGroup.entrySet() + .stream() + .filter(entry -> entry.getKey() != EventDiscount.GIFT) + .mapToInt(Map.Entry::getValue) + .sum(); + } + + public int getTotalBenefitsAmount() { + return this.eventDiscountGroup.values() + .stream() + .mapToInt(amount -> amount) + .sum(); + } + + public Badge getBadge() { + return Arrays.stream(Badge.values()) + .filter(badge -> badge.getWhichBadgeFromAmount(getTotalBenefitsAmount())) + .findFirst() + .orElse(Badge.NONE); + } + + public boolean isVisitDateChristmasDDay(VisitDate visitDate, EventCalendar eventCalendar) { + return visitDate.isChristmasDDay(eventCalendar); + } + + public boolean isVisitDateAllDateEvent(VisitDate visitDate, EventCalendar eventCalendar) { + return visitDate.containsAllDay(eventCalendar); + } + + public Map<EventDiscount, Integer> getEventDiscountGroup() { + return Collections.unmodifiableMap(eventDiscountGroup); + } + + public Map<Gift, Integer> getGifts() { + return Collections.unmodifiableMap(gifts); + } +}
Java
`StarDate` ํด๋ž˜์Šค์—์„œ ํŠน๋ณ„ ์ด๋ฒคํŠธ๋“ค์— ํ•ด๋‹นํ•˜๋Š” ๋‚ ์งœ์ธ ์ผ์š”์ผ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ `starDateSettingWithSpecialDay` ๋ฅผ ํ†ตํ•ด์„œ ํฌ๋ฆฌ์Šค๋งˆ์Šค ์ •๋ณด๋„ ๋„ฃ๋Š”๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. `EventCalendar` ๋Œ€์‹ ์— `StarDate` ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,45 @@ +package christmas.domain; + +import christmas.domain.constants.event.EventDate; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.temporal.TemporalAdjusters; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class StarDate { + private final Set<Integer> starDates; + + public StarDate(EventCalendar eventCalendar) { + this.starDates = new HashSet<>() {{ + addAll(starDatesSettingWithSunday(eventCalendar)); + addAll(starDateSettingWithSpecialDay(eventCalendar)); + }}; + } + + public boolean visitDateContainsStarDates(int day) { + return starDates.contains(day); + } + + public Set<Integer> starDatesSettingWithSunday(EventCalendar eventCalendar) { + LocalDate localDate = LocalDate.of(eventCalendar.getYear(), eventCalendar.getMonth(), eventCalendar.getStartDate()); + Set<Integer> sundays = new HashSet<>(); + LocalDate currentSunday = localDate.with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)); + + while (!currentSunday.isAfter(localDate.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY)))) { + sundays.add(currentSunday.getDayOfMonth()); + currentSunday = currentSunday.plusWeeks(1); + } + return sundays; + } + + public Set<Integer> starDateSettingWithSpecialDay(EventCalendar eventCalendar) { + return Arrays.stream(EventDate.values()) + .filter(eventDate -> eventDate.getMonth() == eventCalendar.getMonth()) + .findFirst() + .map(EventDate::getEventDates) + .orElse(EventDate.NONE.getEventDates()); + } +}
Java
์ €๋Š” ๊ฐœ์ธ์ ์œผ๋กœ ์ด๋ฒˆ ๊ณผ์ œ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ด๋ฒคํŠธ๋“ค์„ ์ถ”์ƒํ™”ํ•˜๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ ์ด๋ฒคํŠธ ๊ฐ์ฒด๋“ค์—๋งŒ ์‹ ๊ฒฝ์„ ์จ์„œ ์ƒ๋Œ€์ ์œผ๋กœ ๋‚ ์งœ ์ •๋ณด๋“ค์€ ์กฐ๊ธˆ ๋Œ€์ถฉ(?) ๋‹ค๋ค˜๋˜๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์˜์„๋‹˜ ์ฒ˜๋Ÿผ ๋‚ ์งœ ์ •๋ณด๋“ค์„ ๊ผผ๊ผผํ•˜๊ฒŒ ๋‹ค๋ค„์•ผํ–ˆ๋‚˜ ํ•˜๋Š” ์ƒ๊ฐ์ด ๋“œ๋„ค์š” ๐Ÿฅฒ
@@ -0,0 +1,31 @@ +package christmas.domain.constants; + +import java.util.function.Predicate; + +public enum Badge { + NONE("์—†์Œ", (amount) -> amount >= 0 && amount < 5_000), + START("๋ณ„", (amount) -> amount >= 5_000 && amount < 10_000), + TREE("ํŠธ๋ฆฌ", (amount) -> amount >= 10_000 && amount < 20_000), + SANTA("์‚ฐํƒ€", (amount) -> amount >= 20_000 && amount < Integer.MAX_VALUE); + + private final String badge; + private final Predicate<Integer> whichBadge; + + Badge(String badge, Predicate<Integer> whichBadge) { + this.badge = badge; + this.whichBadge = whichBadge; + } + + public String getBadge() { + return badge; + } + + public boolean getWhichBadgeFromAmount(int amount) { + return whichBadge.test(amount); + } + + @Override + public String toString() { + return badge; + } +} \ No newline at end of file
Java
์ •๋ง ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค! `Predicate` ๋ฅผ ํ™œ์šฉํ•˜๋ฉด์„œ๋„ ๋ฏธ์ฒ˜ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•œ ๋ฐฉ๋ฒ•์ด๋„ค์š” ๐Ÿ‘
@@ -0,0 +1,41 @@ +package christmas.domain.constants; + +import java.util.function.Function; + +public enum Gift { + CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, 1, true, (amount) -> amount >= 120_000); + + private final String name; + private final int price; + private final int count; + private final boolean gift; + private final Function<Integer, Boolean> giftApplicable; + + Gift(String name, int price, int count, boolean gift, Function<Integer, Boolean> giftApplicable) { + this.name = name; + this.price = price; + this.count = count; + this.gift = gift; + this.giftApplicable = giftApplicable; + } + + public String getName() { + return this.name; + } + + public int getPrice() { + return this.price; + } + + public int getCount() { + return count; + } + + public boolean getGift() { + return gift; + } + + public boolean isGiftApplicable(int amount) { + return giftApplicable.apply(amount); + } +}
Java
`Gift` enum ํด๋ž˜์Šค์•ˆ์— ์กด์žฌํ•˜๋Š” ์ƒ์ˆ˜๋“ค์€ ์ด๋ฏธ ์ฆ์ •ํ’ˆ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ์ด์œ ๋กœ ๊ตฌํ˜„ํ•˜์…จ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,35 @@ +package christmas.domain.constants.event; + +import java.util.Set; + +public enum EventDate { + NONE(0, Set.of()), + JANUARY(1, Set.of(1)), + FEBRUARY(2, Set.of(14)), + MARCH(3, Set.of(1, 14)), + APRIL(4, Set.of()), + MAY(5, Set.of(5, 8, 15)), + JUNE(6, Set.of(6)), + JULY(7, Set.of()), + AUGUST(8, Set.of(15)), + SEPTEMBER(9, Set.of()), + OCTOBER(10, Set.of(3, 9)), + NOVEMBER(11, Set.of(11)), + DECEMBER(12, Set.of(25)); + + private final int month; + private final Set<Integer> eventDates; + + EventDate(int month, Set<Integer> eventDates) { + this.month = month; + this.eventDates = eventDates; + } + + public int getMonth() { + return month; + } + + public Set<Integer> getEventDates() { + return eventDates; + } +}
Java
๊ฐ ์›”์˜ ํŠน๋ณ„ํ•œ ๋‚ ์งœ๋“ค์„ ์ •์˜ํ•ด๋†“์€ enum ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๊ทธ ๊ธฐ์ค€์ด ์กฐ๊ธˆ ๋ชจํ˜ธํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. `2์›”` ์ด๋‚˜ `3์›”` ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๊ณตํœด์ผ์ด ์•„๋‹Œ ๋ฐœ๋ Œํƒ€์ธ ๋ฐ์ด ๊ฐ™์€ ๋‚ ๋“ค์„ ํฌํ•จํ•˜๋Š” ๋ฐ˜๋ฉด ๊ณตํœด์ผ๋งŒ์„ ํฌํ•จํ•˜๋Š” ๋‹ฌ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ `5์›” 27์ผ` (๋ถ€์ฒ˜๋‹˜ ์˜ค์‹  ๋‚ ), `9์›” 28 ~ 30์ผ` (์ถ”์„) ์€ ์ œ์™ธ๋˜์–ด ์žˆ๊ณ  ํ• ๋กœ์œˆ ๋ฐ์ด์ฒ˜๋Ÿผ ๋น ์ง„ ๋‚ ๋“ค๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์ค€์„ ์„ธ์›Œ์„œ ๊ณตํœด์ผ๋งŒ ๋„ฃ๋Š”๋‹ค๋“ ์ง€ ๊ณตํœด์ผ ํฌํ•จ ๋ชจ๋“  ์˜๋ฏธ์žˆ๋Š” ๋‚ ๋“ค? ์„ ๋„ฃ๋Š”๋‹ค๋“ ์ง€ ํ•ด์„œ ๊ธฐ์ค€์„ ์„ธ์šฐ๋ฉด ์ข€๋” ๋ช…ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์„๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์˜์„๋‹˜์˜ ์ƒ๊ฐ์€ ์–ด๋– ์‹ ์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค ๐Ÿค”
@@ -0,0 +1,12 @@ +package christmas.util.validation; + +import christmas.message.ErrorMessage; + +public class CommonValidator { + + public static void isBlank(String text) { + if (text == null || text.isBlank()) { + throw new IllegalArgumentException(ErrorMessage.BLANK.getReasonFormattedMessage()); + } + } +}
Java
์ €๋„ ์ด๋Ÿฐ์‹์œผ๋กœ ์ž…๋ ฅ๊ฐ’์ด ๊ณต๋ฐฑ์ธ์ง€ ์ฒดํฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์—ˆ์—ˆ๋Š”๋ฐ `Console.readLine().trim();` ์œผ๋กœ ๊ณต๋ฐฑ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋”๋ผ๊ตฌ์š” ๐Ÿ˜‚
@@ -0,0 +1,16 @@ +package christmas.view.constants; + +public enum InputMessage { + VISIT_DATE("12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"), + ORDER_MENU("์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)"); + + private final String message; + + InputMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +}
Java
๋‹ฌ๋ ฅ ๋ฐ ๋‚ ์งœ๊ด€๋ จ ํด๋ž˜์Šค์— 12์›” ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๋‚ ๋“ค๋„ ์ •์˜ํ•ด๋‘์…จ์œผ๋‹ˆ ์ด๋ถ€๋ถ„๋„ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›๋„๋ก ์ฒ˜๋ฆฌํ•˜๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,53 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.menu.MenuGroup; +import christmas.domain.menu.MenuType; + +public final class WeekdayDiscountEvent implements Event { + + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 31; + private static final int CHRISTMAS_EVENT_DESERT_DISCOUNT = 2_023; + private static final String WEEKDAY_DISCOUNT_EVENT_TITLE = "ํ‰์ผ ํ• ์ธ"; + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date) && !date.isWeekend(); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + benefit = order.getMenus().menuList().stream() + .mapToInt(menu -> { + MenuType menuType = MenuType.findByMenuName(menu.getMenuName()); + MenuGroup menuGroup = MenuGroup.findByMenuType(menuType); + int menuQuantity = menu.getMenuQuantity(); + if (menuGroup == MenuGroup.DESSERT) { + return CHRISTMAS_EVENT_DESERT_DISCOUNT * menuQuantity; + } + return 0; + }) + .sum(); + return benefit; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return WEEKDAY_DISCOUNT_EVENT_TITLE; + } +}
Java
์ƒ์ˆ˜๋กœ ๋‚ ์งœ๋ฅผ ์ •์˜ํ•œ๋‹ค๊ธฐ ๋ณด๋‹ค๋Š” java.util.Calendar ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜๋ฉด ๋” ์‰ฝ๊ฒŒ ํ‘œ๊ธฐ๊ฐ€๋Šฅ ํ• ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,53 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.menu.MenuGroup; +import christmas.domain.menu.MenuType; + +public final class WeekendDiscountEvent implements Event { + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 31; + private static final int CHRISTMAS_EVENT_DESERT_DISCOUNT = 2_023; + private static final String WEEKEND_DISCOUNT_EVENT_TITLE = "์ฃผ๋ง ํ• ์ธ"; + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date) && + date.isWeekend(); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + benefit = order.getMenus().menuList().stream() + .mapToInt(menu -> { + MenuType menuType = MenuType.findByMenuName(menu.getMenuName()); + MenuGroup menuGroup = MenuGroup.findByMenuType(menuType); + int menuQuantity = menu.getMenuQuantity(); + if (menuGroup == MenuGroup.MAIN) { + return CHRISTMAS_EVENT_DESERT_DISCOUNT * menuQuantity; + } + return 0; + }) + .sum(); + return benefit; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return WEEKEND_DISCOUNT_EVENT_TITLE; + } +}
Java
์ด ๋ถ€๋ถ„๋„ Calendar ํด๋ž˜์Šค์˜ after before ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์‰ฝ๊ฒŒ ๋น„๊ต ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,55 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.menu.Menu; +import christmas.domain.menu.MenuType; + +public final class FreeGiftEvent implements Event { + + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 31; + private static final int CHRISTMAS_EVENT_FREE_STANDARD = 120_000; + private static final String CHRISTMAS_EVENT_FREE_QUANTITY = "1"; + private static final String FREE_GIFT_EVENT_TITLE = "์ฆ์ • ์ด๋ฒคํŠธ"; + private Menu benefitGift = new Menu(MenuType.EMPTY.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + int beforeMoney = order.getBeforeMoney(); + if (beforeMoney >= CHRISTMAS_EVENT_FREE_STANDARD) { + MenuType champagne = MenuType.CHAMPAGNE; + benefitGift = new Menu(champagne.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + benefit = champagne.getPrice(); + } + return benefit; + } + + public Menu getBenefitGift() { + return benefitGift; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return FREE_GIFT_EVENT_TITLE; + } + +}
Java
์ƒ์„ฑ์ž๋กœ DI ์ฃผ์ž… ๋ฐ›์œผ์‹œ๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,51 @@ +package christmas.controller; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.calendar.Planner; +import christmas.domain.menu.Menus; +import christmas.view.InputView; +import christmas.view.OutputView; + +public class UtecoRestaurantController { + + public void startOperation() { + Date date = inputDate(); + Menus menus = inputMenus(); + Order order = new Order(menus); + Planner planner = new Planner(date, order); + + OutputView.printBasicPreview(date, menus, order); + OutputView.printPlanner(order, planner); + } + + private static Date inputDate() { + OutputView.printRestaurantIntro(); + Date date = createDate(); + return date; + } + + private static Date createDate() { + try { + return new Date(InputView.inputDate()); + } catch (IllegalArgumentException e) { + OutputView.printException(e); + return createDate(); + } + } + + private static Menus inputMenus() { + OutputView.printRestaurantMenu(); + Menus menus = createMenus(); + return menus; + } + + private static Menus createMenus() { + try { + return InputView.inputOrderMenu(); + } catch (IllegalArgumentException e) { + OutputView.printException(e); + return createMenus(); + } + } +}
Java
์ €๋ž‘ ๋น„์Šทํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์…จ๋„ค์š” ! ๋‹ค๋งŒ View๋ฅผ ์Šคํƒœํ‹ฑ์œผ๋กœ ๊ด€๋ฆฌํ•˜์‹  ์ด์œ ๊ฐ€์žˆ์„๊นŒ์š”?
@@ -0,0 +1,65 @@ +package christmas.domain.calendar; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.format.TextStyle; +import java.util.Locale; + +public class Date { + private static final int VISIT_YEAR = 2023; + private static final int VISIT_MONTH = 12; + private Year year; + private Month month; + private Day day; + private String dayFormat; + + public Date(int day) { + this.year = new Year(VISIT_YEAR); + this.month = new Month(VISIT_MONTH); + this.day = new Day(day); + this.dayFormat = createVisitingDayFormat(); + } + + public String createVisitingDayFormat() { + LocalDate visitDay = createDateTimeFormat(); + DayOfWeek dayOfWeek = visitDay.getDayOfWeek(); + return dayOfWeek.getDisplayName(TextStyle.FULL, Locale.KOREA); + } + + public boolean isWeekend() { + LocalDate visitDay = createDateTimeFormat(); + DayOfWeek dayOfWeek = visitDay.getDayOfWeek(); + return dayOfWeek == DayOfWeek.FRIDAY || dayOfWeek == DayOfWeek.SATURDAY; + } + + public boolean isSunday() { + LocalDate visitDay = createDateTimeFormat(); + DayOfWeek dayOfWeek = visitDay.getDayOfWeek(); + return dayOfWeek == DayOfWeek.SUNDAY; + } + + public LocalDate createDateTimeFormat() { + return LocalDate.of(getYear(), getMonth(), getDay()); + } + + @Override + public String toString() { + return String.format("%s์›” %s์ผ", month.month(), day.day()); + } + + public int getYear() { + return year.year(); + } + + public int getMonth() { + return month.month(); + } + + public int getDay() { + return day.day(); + } + + public String getDayFormat() { + return dayFormat; + } +}
Java
๊ฐํƒ„ํ•˜๊ณ ๊ฐ‘๋‹ˆ๋‹ค.. ! ๋‚ ์งœ๋ฅผ ์ด๋Ÿฐ์‹์œผ๋กœ ๊ด€๋ฆฌํ• ์ˆ˜๋„์žˆ๋„ค์š”!
@@ -0,0 +1,48 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import java.time.LocalDate; +import java.time.Period; + +public final class ChristmasDailyDiscountEvent implements Event { + + private static final int CHRISTMAS_EVENT_YEAR = 2023; + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 25; + private static final int CHRISTMAS_EVENT_START_MONEY = 1_000; + private static final int CHRISTMAS_EVENT_DISCOUNT_UNIT = 100; + private static final String CHRISTMAS_DAILY_DISCOUNT_EVENT_TITLE = "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"; + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + LocalDate startDay = LocalDate.of(CHRISTMAS_EVENT_YEAR, CHRISTMAS_EVENT_MONTH, CHRISTMAS_EVENT_MIN_DAY); + LocalDate visitDay = date.createDateTimeFormat(); + Period period = Period.between(startDay, visitDay); + benefit = CHRISTMAS_EVENT_START_MONEY + CHRISTMAS_EVENT_DISCOUNT_UNIT * period.getDays(); + return benefit; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return CHRISTMAS_DAILY_DISCOUNT_EVENT_TITLE; + } +}
Java
์กฐ๊ธˆ ์–ต์ง€์Šค๋Ÿฌ์šด ์ง€์ ์ด๊ธด ํ•˜์ง€๋งŒ, &&๋ฅผ ์•ž์œผ๋กœ ๋บด๋ ค๋ฉด ๋ชจ๋‘ ๋‹ค ๋นผ๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™์•„์š”! date.getMonth() == CHRISTMAS_EVENT_MONTH && date.getDay() >= CHRISTMAS_EVENT_MIN_DAY && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY;
@@ -0,0 +1,18 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; + +public sealed interface Event permits ChristmasDailyDiscountEvent, WeekdayDiscountEvent, WeekendDiscountEvent, + SpecialDiscountEvent, + FreeGiftEvent { + + boolean isApplicable(Date date); + + int calculateDiscount(Date date, Order order); + + int getBenefit(); + + String getEventName(); + +}
Java
sealed ํ‚ค์›Œ๋“œ๋ฅผ ์ฒ˜์Œ๋ด์„œ ์‹ ๊ธฐํ•˜๋„ค์š”..! ์ €๋„ ๊ณต๋ถ€ํ•ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,55 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.menu.Menu; +import christmas.domain.menu.MenuType; + +public final class FreeGiftEvent implements Event { + + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 31; + private static final int CHRISTMAS_EVENT_FREE_STANDARD = 120_000; + private static final String CHRISTMAS_EVENT_FREE_QUANTITY = "1"; + private static final String FREE_GIFT_EVENT_TITLE = "์ฆ์ • ์ด๋ฒคํŠธ"; + private Menu benefitGift = new Menu(MenuType.EMPTY.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + int beforeMoney = order.getBeforeMoney(); + if (beforeMoney >= CHRISTMAS_EVENT_FREE_STANDARD) { + MenuType champagne = MenuType.CHAMPAGNE; + benefitGift = new Menu(champagne.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + benefit = champagne.getPrice(); + } + return benefit; + } + + public Menu getBenefitGift() { + return benefitGift; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return FREE_GIFT_EVENT_TITLE; + } + +}
Java
์ด ๋ถ€๋ถ„์€ ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด๊ฐ€ ๋ชจ๋‘ ๋™์ผํ•œ ๋กœ์ง์„ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— Event๋ฅผ ์ถ”์ƒํด๋ž˜์Šค๋กœ ๊ด€๋ฆฌํ•˜๋ฉด ์ฝ”๋“œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์–ด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,65 @@ +package christmas.domain.calendar; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.format.TextStyle; +import java.util.Locale; + +public class Date { + private static final int VISIT_YEAR = 2023; + private static final int VISIT_MONTH = 12; + private Year year; + private Month month; + private Day day; + private String dayFormat; + + public Date(int day) { + this.year = new Year(VISIT_YEAR); + this.month = new Month(VISIT_MONTH); + this.day = new Day(day); + this.dayFormat = createVisitingDayFormat(); + } + + public String createVisitingDayFormat() { + LocalDate visitDay = createDateTimeFormat(); + DayOfWeek dayOfWeek = visitDay.getDayOfWeek(); + return dayOfWeek.getDisplayName(TextStyle.FULL, Locale.KOREA); + } + + public boolean isWeekend() { + LocalDate visitDay = createDateTimeFormat(); + DayOfWeek dayOfWeek = visitDay.getDayOfWeek(); + return dayOfWeek == DayOfWeek.FRIDAY || dayOfWeek == DayOfWeek.SATURDAY; + } + + public boolean isSunday() { + LocalDate visitDay = createDateTimeFormat(); + DayOfWeek dayOfWeek = visitDay.getDayOfWeek(); + return dayOfWeek == DayOfWeek.SUNDAY; + } + + public LocalDate createDateTimeFormat() { + return LocalDate.of(getYear(), getMonth(), getDay()); + } + + @Override + public String toString() { + return String.format("%s์›” %s์ผ", month.month(), day.day()); + } + + public int getYear() { + return year.year(); + } + + public int getMonth() { + return month.month(); + } + + public int getDay() { + return day.day(); + } + + public String getDayFormat() { + return dayFormat; + } +}
Java
์ €๋„ ํ•œ์ˆ˜ ๋ฐฐ์šฐ๊ณ ๊ฐ‘๋‹ˆ๋‹ค ์ €๋Š” ๊ทธ๋ƒฅ ์ด๋ฒคํŠธ๊ฐ€ 12์›” ํ•œ์ •์ด๋ผ์„œ ๊ทธ๋ƒฅ ๋‚˜๋จธ์ง€์—ฐ์‚ฐ์œผ๋กœ ํ–ˆ๋Š”๋ฐ.. ๋ฐ˜์„ฑํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,55 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.menu.Menu; +import christmas.domain.menu.MenuType; + +public final class FreeGiftEvent implements Event { + + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 31; + private static final int CHRISTMAS_EVENT_FREE_STANDARD = 120_000; + private static final String CHRISTMAS_EVENT_FREE_QUANTITY = "1"; + private static final String FREE_GIFT_EVENT_TITLE = "์ฆ์ • ์ด๋ฒคํŠธ"; + private Menu benefitGift = new Menu(MenuType.EMPTY.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + int beforeMoney = order.getBeforeMoney(); + if (beforeMoney >= CHRISTMAS_EVENT_FREE_STANDARD) { + MenuType champagne = MenuType.CHAMPAGNE; + benefitGift = new Menu(champagne.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + benefit = champagne.getPrice(); + } + return benefit; + } + + public Menu getBenefitGift() { + return benefitGift; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return FREE_GIFT_EVENT_TITLE; + } + +}
Java
์ €๋„ ์ฒ˜์Œ์— ์ด๋ ‡๊ฒŒ ํ–ˆ๋Š”๋ฐ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ฝ์–ด๋ณด๋‹ˆ ์ฆ์ •์ด๋ฒคํŠธ์—๋Š” ํ• ์ธ์ด๋ผ๋Š” ๊ฐœ๋…์ด ์—†์–ด์„œ ์ €๋Š” ํ˜œํƒ๊ธˆ์•ก๊ณ„์‚ฐ์œผ๋กœ ๋ฐ”๊ฟจ์–ด์š”..!
@@ -0,0 +1,51 @@ +package christmas.domain.menu; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public enum MenuGroup { + APPETIZER("์—ํ”ผํƒ€์ด์ €", Arrays.asList(MenuType.BUTTON_MUSHROOM_SOUP, MenuType.TAPAS, MenuType.CAESAR_SALAD)), + MAIN("๋ฉ”์ธ", Arrays.asList(MenuType.T_BONE_STEAK, MenuType.BARBECUE_RIBS, MenuType.SEAFOOD_PASTA, + MenuType.CHRISTMAS_PASTA)), + DESSERT("๋””์ €ํŠธ", Arrays.asList(MenuType.CHOCOLATE_CAKE, MenuType.ICE_CREAM)), + BEVERAGE("์Œ๋ฃŒ", Arrays.asList(MenuType.ZERO_COLA, MenuType.RED_WINE, MenuType.CHAMPAGNE)), + EMPTY("์—†์Œ", Collections.EMPTY_LIST); + + private final String title; + private final List<MenuType> menuList; + + MenuGroup(String title, List<MenuType> menuList) { + this.title = title; + this.menuList = menuList; + } + + public static MenuGroup findByMenuType(MenuType menuType) { + return Arrays.stream(MenuGroup.values()) + .filter(menuGroup -> menuGroup.hasMenuName(menuType)) + .findAny() + .orElse(EMPTY); + } + + private boolean hasMenuName(MenuType menuType) { + return menuList.stream() + .anyMatch(menu -> menu == menuType); + } + + @Override + public String toString() { + return "<" + title + ">\n" + + menuList.stream() + .map(MenuType::toString) + .collect(Collectors.joining(", ")); + } + + public String getTitle() { + return title; + } + + public List<MenuType> getMenuList() { + return menuList; + } +}
Java
์˜ค ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฉ”๋‰ด๋ฅผ ์ƒ์ˆ˜์ฒ˜๋ฆฌํ•˜์…จ๊ตฐ์š” ๋ฆฌ์ŠคํŠธ๋กœ ๋‹ด๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋„ค์š”!
@@ -0,0 +1,53 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.menu.MenuGroup; +import christmas.domain.menu.MenuType; + +public final class WeekdayDiscountEvent implements Event { + + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 31; + private static final int CHRISTMAS_EVENT_DESERT_DISCOUNT = 2_023; + private static final String WEEKDAY_DISCOUNT_EVENT_TITLE = "ํ‰์ผ ํ• ์ธ"; + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date) && !date.isWeekend(); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + benefit = order.getMenus().menuList().stream() + .mapToInt(menu -> { + MenuType menuType = MenuType.findByMenuName(menu.getMenuName()); + MenuGroup menuGroup = MenuGroup.findByMenuType(menuType); + int menuQuantity = menu.getMenuQuantity(); + if (menuGroup == MenuGroup.DESSERT) { + return CHRISTMAS_EVENT_DESERT_DISCOUNT * menuQuantity; + } + return 0; + }) + .sum(); + return benefit; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return WEEKDAY_DISCOUNT_EVENT_TITLE; + } +}
Java
ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ‰์ผ ์ด๋ฒคํŠธ๊ฐ€ ์š”๊ตฌ ์‚ฌํ•ญ์— ๋”ฐ๋ผ ๋ณ€๊ฒฝ์ด ๋˜๋Š” ๊ธฐ๊ฐ„์ด๋ผ 12์›”์— ๋Œ€ํ•œ ์ƒ์ˆ˜์ฒ˜๋ฆฌ๋กœ ๊ธฐ๊ฐ„์„ ํ‘œํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค !
@@ -0,0 +1,55 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.menu.Menu; +import christmas.domain.menu.MenuType; + +public final class FreeGiftEvent implements Event { + + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 31; + private static final int CHRISTMAS_EVENT_FREE_STANDARD = 120_000; + private static final String CHRISTMAS_EVENT_FREE_QUANTITY = "1"; + private static final String FREE_GIFT_EVENT_TITLE = "์ฆ์ • ์ด๋ฒคํŠธ"; + private Menu benefitGift = new Menu(MenuType.EMPTY.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + int beforeMoney = order.getBeforeMoney(); + if (beforeMoney >= CHRISTMAS_EVENT_FREE_STANDARD) { + MenuType champagne = MenuType.CHAMPAGNE; + benefitGift = new Menu(champagne.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + benefit = champagne.getPrice(); + } + return benefit; + } + + public Menu getBenefitGift() { + return benefitGift; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return FREE_GIFT_EVENT_TITLE; + } + +}
Java
๊ธฐ๋Šฅ์„ ๊ฑฐ์˜ ๋งˆ๋ฌด๋ฆฌํ•˜๊ณ  ์ถœ๋ ฅ๋ฌธ์„ ๊ตฌํ˜„ํ•  ๋•Œ, ์ฆ์ • ์ƒํ’ˆ์ด ์—†์„ ๊ฒฝ์šฐ๋‚˜ ์ฆ์ •ํ’ˆ ๊ธˆ์•ก ์š”๊ตฌ์‚ฌํ•ญ์— if ์กฐ๊ฑด์„ ๋‘์ง€ ์•Š๊ณ  Menu์— Empty, ์—†์Œ ์ฒ˜๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ์ฒ˜๋ฆฌํ•˜๋ ค๊ณ  ๋งŒ๋“ค๋‹ค๊ฐ€ ํ•˜๋“œ์ฝ”๋”ฉ์ด ๋œ ๋ถ€๋ถ„์ธ ๊ฒƒ ๊ฐ™๋„ค์š” ! ์ƒ์„ฑ์ž๋กœ ๋ฐ›์•˜์œผ๋ฉด ๋” ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค :)
@@ -0,0 +1,53 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.menu.MenuGroup; +import christmas.domain.menu.MenuType; + +public final class WeekendDiscountEvent implements Event { + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 31; + private static final int CHRISTMAS_EVENT_DESERT_DISCOUNT = 2_023; + private static final String WEEKEND_DISCOUNT_EVENT_TITLE = "์ฃผ๋ง ํ• ์ธ"; + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date) && + date.isWeekend(); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + benefit = order.getMenus().menuList().stream() + .mapToInt(menu -> { + MenuType menuType = MenuType.findByMenuName(menu.getMenuName()); + MenuGroup menuGroup = MenuGroup.findByMenuType(menuType); + int menuQuantity = menu.getMenuQuantity(); + if (menuGroup == MenuGroup.MAIN) { + return CHRISTMAS_EVENT_DESERT_DISCOUNT * menuQuantity; + } + return 0; + }) + .sum(); + return benefit; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return WEEKEND_DISCOUNT_EVENT_TITLE; + } +}
Java
12์›” ์ด๋ฒคํŠธ ๋ฟ๋งŒ์•„๋‹ˆ๋ผ 12์›”์—์„œ 1์›”๊นŒ์ง€ ์—ฐ์žฅ์ด ๋œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด, Calendar์™€ LocalDate ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค :) ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,51 @@ +package christmas.controller; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.calendar.Planner; +import christmas.domain.menu.Menus; +import christmas.view.InputView; +import christmas.view.OutputView; + +public class UtecoRestaurantController { + + public void startOperation() { + Date date = inputDate(); + Menus menus = inputMenus(); + Order order = new Order(menus); + Planner planner = new Planner(date, order); + + OutputView.printBasicPreview(date, menus, order); + OutputView.printPlanner(order, planner); + } + + private static Date inputDate() { + OutputView.printRestaurantIntro(); + Date date = createDate(); + return date; + } + + private static Date createDate() { + try { + return new Date(InputView.inputDate()); + } catch (IllegalArgumentException e) { + OutputView.printException(e); + return createDate(); + } + } + + private static Menus inputMenus() { + OutputView.printRestaurantMenu(); + Menus menus = createMenus(); + return menus; + } + + private static Menus createMenus() { + try { + return InputView.inputOrderMenu(); + } catch (IllegalArgumentException e) { + OutputView.printException(e); + return createMenus(); + } + } +}
Java
Controller๊ฐ€ ์ง์ ‘ InputView์™€ OutputView๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ปจํŠธ๋กค๋Ÿฌ ๋ถ€๋ถ„์ด ๋” ๊น”๋”ํ•ด์ง€์ง€ ์•Š์„๊นŒ.. ๋ผ๋Š” ๊ฐœ์ธ์ ์ธ ์˜๊ฒฌ์ด ๋“ค์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค DI๋ฅผ ๋ฐ›๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„์— ๋” ๋งž๋Š” ๋ฐฉ์‹์ด์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋„ค์š” ๐Ÿฅฒ
@@ -0,0 +1,48 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import java.time.LocalDate; +import java.time.Period; + +public final class ChristmasDailyDiscountEvent implements Event { + + private static final int CHRISTMAS_EVENT_YEAR = 2023; + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 25; + private static final int CHRISTMAS_EVENT_START_MONEY = 1_000; + private static final int CHRISTMAS_EVENT_DISCOUNT_UNIT = 100; + private static final String CHRISTMAS_DAILY_DISCOUNT_EVENT_TITLE = "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"; + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + LocalDate startDay = LocalDate.of(CHRISTMAS_EVENT_YEAR, CHRISTMAS_EVENT_MONTH, CHRISTMAS_EVENT_MIN_DAY); + LocalDate visitDay = date.createDateTimeFormat(); + Period period = Period.between(startDay, visitDay); + benefit = CHRISTMAS_EVENT_START_MONEY + CHRISTMAS_EVENT_DISCOUNT_UNIT * period.getDays(); + return benefit; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return CHRISTMAS_DAILY_DISCOUNT_EVENT_TITLE; + } +}
Java
์•„๋‹™๋‹ˆ๋‹ค ! ์ฐพ์•„์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ‘๐Ÿ‘๐Ÿ‘ ์ฝ”๋“œ๋ฅผ ์ง„ํ–‰ํ•˜๋‹ค๊ฐ€ ์ค„ ๋ฐ”๊ฟˆ์„ ๋ณด์ง€ ๋ชปํ•˜๊ณ  ์ฝ”๋“œ ์ผ๊ด€์„ฑ์ด ์–ด๊ธ‹๋‚˜์žˆ๋„ค์š” ! ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,55 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.menu.Menu; +import christmas.domain.menu.MenuType; + +public final class FreeGiftEvent implements Event { + + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 31; + private static final int CHRISTMAS_EVENT_FREE_STANDARD = 120_000; + private static final String CHRISTMAS_EVENT_FREE_QUANTITY = "1"; + private static final String FREE_GIFT_EVENT_TITLE = "์ฆ์ • ์ด๋ฒคํŠธ"; + private Menu benefitGift = new Menu(MenuType.EMPTY.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + int beforeMoney = order.getBeforeMoney(); + if (beforeMoney >= CHRISTMAS_EVENT_FREE_STANDARD) { + MenuType champagne = MenuType.CHAMPAGNE; + benefitGift = new Menu(champagne.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + benefit = champagne.getPrice(); + } + return benefit; + } + + public Menu getBenefitGift() { + return benefitGift; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return FREE_GIFT_EVENT_TITLE; + } + +}
Java
๋ฆฌ๋ทฐ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค :)
@@ -0,0 +1,55 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.menu.Menu; +import christmas.domain.menu.MenuType; + +public final class FreeGiftEvent implements Event { + + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 31; + private static final int CHRISTMAS_EVENT_FREE_STANDARD = 120_000; + private static final String CHRISTMAS_EVENT_FREE_QUANTITY = "1"; + private static final String FREE_GIFT_EVENT_TITLE = "์ฆ์ • ์ด๋ฒคํŠธ"; + private Menu benefitGift = new Menu(MenuType.EMPTY.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + int beforeMoney = order.getBeforeMoney(); + if (beforeMoney >= CHRISTMAS_EVENT_FREE_STANDARD) { + MenuType champagne = MenuType.CHAMPAGNE; + benefitGift = new Menu(champagne.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + benefit = champagne.getPrice(); + } + return benefit; + } + + public Menu getBenefitGift() { + return benefitGift; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return FREE_GIFT_EVENT_TITLE; + } + +}
Java
๋™์˜ํ•ฉ๋‹ˆ๋‹ค.. ์ฆ์ • ์ด๋ฒคํŠธ๋กœ ์ƒํ’ˆ์„ ์ค˜์„œ, ์ด ํ˜œํƒ ๊ธˆ์•ก์—๋„ ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋”๋ผ๊ตฌ์š”.. ํ˜„์žฌ Event์—์„œ 5๊ฐ€์ง€ ์ด๋ฒคํŠธ๋ฅผ ์ œํ•œํ•˜์—ฌ ์ƒ์† ๋ฐ›์•„, ๊ฒ€์ฆ, ํ• ์ธ๊ณ„์‚ฐ ์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ƒดํŽ˜์ธ์„ ์ฃผ๋ฌธํ•œ ํšŒ์›์ด๋ผ๋ฉด ์ฆ์ •์ƒํ’ˆ์„ ๋ฐ›์ง€์•Š๊ณ  ๊ฐ€๊ฒฉํ• ์ธ์— ์ ์šฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ์„ํ…๋ฐ ๋”ฐ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ตฌํ˜„ํ•ด์•ผํ• ๊นŒ? ๋ผ๋Š” ์ƒ๊ฐ์— ํ• ์ธ๊ณ„์‚ฐ ๋ฉ”์„œ๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์™€์„œ ๊ฐ™์ด ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค :)
@@ -0,0 +1,65 @@ +package christmas.domain.calendar; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.format.TextStyle; +import java.util.Locale; + +public class Date { + private static final int VISIT_YEAR = 2023; + private static final int VISIT_MONTH = 12; + private Year year; + private Month month; + private Day day; + private String dayFormat; + + public Date(int day) { + this.year = new Year(VISIT_YEAR); + this.month = new Month(VISIT_MONTH); + this.day = new Day(day); + this.dayFormat = createVisitingDayFormat(); + } + + public String createVisitingDayFormat() { + LocalDate visitDay = createDateTimeFormat(); + DayOfWeek dayOfWeek = visitDay.getDayOfWeek(); + return dayOfWeek.getDisplayName(TextStyle.FULL, Locale.KOREA); + } + + public boolean isWeekend() { + LocalDate visitDay = createDateTimeFormat(); + DayOfWeek dayOfWeek = visitDay.getDayOfWeek(); + return dayOfWeek == DayOfWeek.FRIDAY || dayOfWeek == DayOfWeek.SATURDAY; + } + + public boolean isSunday() { + LocalDate visitDay = createDateTimeFormat(); + DayOfWeek dayOfWeek = visitDay.getDayOfWeek(); + return dayOfWeek == DayOfWeek.SUNDAY; + } + + public LocalDate createDateTimeFormat() { + return LocalDate.of(getYear(), getMonth(), getDay()); + } + + @Override + public String toString() { + return String.format("%s์›” %s์ผ", month.month(), day.day()); + } + + public int getYear() { + return year.year(); + } + + public int getMonth() { + return month.month(); + } + + public int getDay() { + return day.day(); + } + + public String getDayFormat() { + return dayFormat; + } +}
Java
ํฌ์œผ DayOfWeek ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค!
@@ -0,0 +1,20 @@ +package christmas.domain.calendar; + +import static christmas.message.ErrorMessages.INVALID_DATE_RANGE; + +public record Day(int day) { + + private static int MIN_DAY = 1; + private static int EVEN_MONTHS_MAX_DAY = 31; + + public Day { + validateRangeFromDay(day); + } + + private void validateRangeFromDay(int day) { + if (day < MIN_DAY || day > EVEN_MONTHS_MAX_DAY) { + throw new IllegalArgumentException(INVALID_DATE_RANGE.getMessage()); + } + } + +}
Java
Exception์„ enum์œผ๋กœ ๊ด€๋ฆฌํ•ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,57 @@ +package christmas.domain.calendar; + +import christmas.domain.event.ChristmasDailyDiscountEvent; +import christmas.domain.event.Event; +import christmas.domain.event.EventBadge; +import christmas.domain.event.FreeGiftEvent; +import christmas.domain.event.SpecialDiscountEvent; +import christmas.domain.event.WeekdayDiscountEvent; +import christmas.domain.event.WeekendDiscountEvent; +import java.util.Collections; +import java.util.List; + +public class Planner { + + private static final int CHRISTMAS_EVENT_MIN_ORDER_AMOUNT = 10_000; + private static final int START_BENEFIT_AMOUNT = 0; + private final List<Event> events; + private int afterAmount; + private EventBadge eventBadge; + + public Planner(Date date, Order order) { + if (order.getBeforeMoney() < CHRISTMAS_EVENT_MIN_ORDER_AMOUNT) { + this.events = Collections.EMPTY_LIST; + this.afterAmount = START_BENEFIT_AMOUNT; + return; + } + + this.events = List.of( + new ChristmasDailyDiscountEvent(), + new WeekdayDiscountEvent(), + new WeekendDiscountEvent(), + new FreeGiftEvent(), + new SpecialDiscountEvent() + ); + this.afterAmount = calculateBenefits(date, order); + } + + public int calculateBenefits(Date date, Order order) { + + return events.stream() + .filter(event -> event.isApplicable(date)) + .mapToInt(event -> event.calculateDiscount(date, order)) + .sum(); + } + + public List<Event> getEvents() { + return events; + } + + public int getAfterAmount() { + return afterAmount; + } + + public EventBadge getEventBadge() { + return EventBadge.findByBadgeType(afterAmount); + } +}
Java
์˜คํ˜ธ ์—ฌ๊ธฐ์„œ ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„ค์š”! ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ์ข‹์•„ ๋ณด์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,57 @@ +package christmas.domain.calendar; + +import christmas.domain.event.ChristmasDailyDiscountEvent; +import christmas.domain.event.Event; +import christmas.domain.event.EventBadge; +import christmas.domain.event.FreeGiftEvent; +import christmas.domain.event.SpecialDiscountEvent; +import christmas.domain.event.WeekdayDiscountEvent; +import christmas.domain.event.WeekendDiscountEvent; +import java.util.Collections; +import java.util.List; + +public class Planner { + + private static final int CHRISTMAS_EVENT_MIN_ORDER_AMOUNT = 10_000; + private static final int START_BENEFIT_AMOUNT = 0; + private final List<Event> events; + private int afterAmount; + private EventBadge eventBadge; + + public Planner(Date date, Order order) { + if (order.getBeforeMoney() < CHRISTMAS_EVENT_MIN_ORDER_AMOUNT) { + this.events = Collections.EMPTY_LIST; + this.afterAmount = START_BENEFIT_AMOUNT; + return; + } + + this.events = List.of( + new ChristmasDailyDiscountEvent(), + new WeekdayDiscountEvent(), + new WeekendDiscountEvent(), + new FreeGiftEvent(), + new SpecialDiscountEvent() + ); + this.afterAmount = calculateBenefits(date, order); + } + + public int calculateBenefits(Date date, Order order) { + + return events.stream() + .filter(event -> event.isApplicable(date)) + .mapToInt(event -> event.calculateDiscount(date, order)) + .sum(); + } + + public List<Event> getEvents() { + return events; + } + + public int getAfterAmount() { + return afterAmount; + } + + public EventBadge getEventBadge() { + return EventBadge.findByBadgeType(afterAmount); + } +}
Java
์—ฌ๊ธฐ์„œ ๊ถ๊ธˆํ•œ๊ฒŒ ```java public sealed interface Event permits ChristmasDailyDiscountEvent, WeekdayDiscountEvent, WeekendDiscountEvent, SpecialDiscountEvent, FreeGiftEvent { ... } ``` ์ด๋ ‡๊ฒŒ ๋ช…์‹œํ•ด๋‘์—ˆ๋Š”๋ฐ, List.of ๋ถ€๋ถ„์„ ๋ณ€๊ฒฝํ•˜๋ฉด Event ์ธํ„ฐํŽ˜์ด์Šค๋„ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋‚˜์š”?
@@ -0,0 +1,55 @@ +package christmas.domain.event; + +import christmas.domain.calendar.Date; +import christmas.domain.calendar.Order; +import christmas.domain.menu.Menu; +import christmas.domain.menu.MenuType; + +public final class FreeGiftEvent implements Event { + + private static final int CHRISTMAS_EVENT_MONTH = 12; + private static final int CHRISTMAS_EVENT_MIN_DAY = 1; + private static final int CHRISTMAS_EVENT_MAX_DAY = 31; + private static final int CHRISTMAS_EVENT_FREE_STANDARD = 120_000; + private static final String CHRISTMAS_EVENT_FREE_QUANTITY = "1"; + private static final String FREE_GIFT_EVENT_TITLE = "์ฆ์ • ์ด๋ฒคํŠธ"; + private Menu benefitGift = new Menu(MenuType.EMPTY.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + private int benefit; + + @Override + public boolean isApplicable(Date date) { + return isEventPeriod(date); + } + + private static boolean isEventPeriod(Date date) { + return date.getMonth() == CHRISTMAS_EVENT_MONTH && + date.getDay() >= CHRISTMAS_EVENT_MIN_DAY + && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; + } + + @Override + public int calculateDiscount(Date date, Order order) { + int beforeMoney = order.getBeforeMoney(); + if (beforeMoney >= CHRISTMAS_EVENT_FREE_STANDARD) { + MenuType champagne = MenuType.CHAMPAGNE; + benefitGift = new Menu(champagne.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY); + benefit = champagne.getPrice(); + } + return benefit; + } + + public Menu getBenefitGift() { + return benefitGift; + } + + @Override + public int getBenefit() { + return benefit; + } + + @Override + public String getEventName() { + return FREE_GIFT_EVENT_TITLE; + } + +}
Java
์ €๋Š” ์ฆ์ • ์ด๋ฒคํŠธ์™€ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ๊ฐ–๋Š” ๊ฐ’์˜ ์ •๋ณด๋„ ๋‹ฌ๋ผ์„œ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ๊ด€๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,16 @@ +package christmas.message; + +public enum ErrorMessages { + INVALID_DATE_RANGE("[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + INVALID_ORDER_FORMAT("[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + + private String message; + + ErrorMessages(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +}
Java
์—ฌ๊ธฐ์„œ ๊ทธ๋ƒฅ Exception์„ throw ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์•„ ๋ณด์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,106 @@ +package christmas; + +import christmas.domain.badge.BadgeService; +import christmas.domain.badge.model.Badge; +import christmas.domain.benefit.BenefitService; +import christmas.domain.benefit.model.Benefits; +import christmas.domain.bills.Bills; +import christmas.domain.date.DateService; +import christmas.domain.date.model.PromotionDay; +import christmas.domain.menu.MenuService; +import christmas.domain.menu.model.collection.OrderSheet; +import christmas.exception.handler.ExceptionHandler; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.List; +import java.util.function.Supplier; + +public class Controller { + private final DateService dateService = new DateService(); + private final MenuService menuService = new MenuService(); + private final BenefitService benefitService = new BenefitService(); + private final BadgeService badgeService = new BadgeService(); + + private final InputView inputView = new InputView(); + + private final OutputView outputView = new OutputView(); + private final ExceptionHandler handler; + + public Controller(ExceptionHandler handler) { + this.handler = handler; + } + + public void run() { + printHello(); + + PromotionDay promotionDay = getPromotionDay(); + OrderSheet orderSheet = getOrderSheet(); + Benefits benefits = getBenefits(promotionDay, orderSheet); + Badge badge = getBadge(benefits); + + printResult(Bills.builder() + .date(promotionDay.getDate()) + .orderSheet(orderSheet) + .benefits(benefits) + .Badge(badge) + .build()); + } + + private void printHello() { + outputView.sayHello(); + } + + private PromotionDay getPromotionDay() { + return getResultUsingExceptionHandler(() -> { + int visitDate = inputView.getVisitDate(); + return dateService.getPromotionDay(visitDate); + }); + } + + private OrderSheet getOrderSheet() { + return getResultUsingExceptionHandler(() -> { + List<String> orders = inputView.getOrders(); + inputView.closeConsole(); + return menuService.getOrderSheet(orders.toArray(String[]::new)); + }); + } + + private <T> T getResultUsingExceptionHandler(Supplier<T> logic) { + return handler.get(logic, outputView::printException); + } + + private Benefits getBenefits(PromotionDay promotionDay, OrderSheet orderSheet) { + return benefitService.getBenefits(promotionDay, orderSheet); + } + + private Badge getBadge(Benefits benefits) { + return badgeService.getBadge(benefits); + } + + private void printResult(Bills bills) { + outputView.printBenefitTitle(bills.getDate()); + printOrderSheeet(bills.getOrderSheet()); + printBenefits(bills.getBenefits()); + printDiscountedPrice(bills); + printBadge(bills.getBadge()); + } + + private void printOrderSheeet(OrderSheet orderSheet) { + outputView.printOrderMenu(orderSheet); + outputView.printTotalPriceNoDiscount(orderSheet); + } + + private void printBenefits(Benefits benefits) { + outputView.printGifts(benefits); + outputView.printBenefits(benefits); + outputView.printTotalBenefitPrice(benefits); + } + + private void printDiscountedPrice(Bills bills) { + outputView.printDiscountedPrice(bills.getDiscountedPrice()); + } + + private void printBadge(Badge badge) { + outputView.printBadge(badge); + } +}
Java
์ €๋Š” ํ•ญ์ƒ print๋ถ€๋ถ„์ด ์ง€์ €๋ถ„ํ•ด์ ธ์„œ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€ ๊ณ ๋ฏผ์„ ๋งŽ์ดํ–ˆ๋Š”๋ฐ, ์‘ค๋‹˜ ๋ณด๊ณ  ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž ๐Ÿ‘
@@ -0,0 +1,41 @@ +package christmas.domain.badge.model; + +import java.util.Arrays; + +public enum Badge { + NONE("์—†์Œ", 0, 5_000), + STAR("๋ณ„", 5_000, 10_000), + TREE("ํŠธ๋ฆฌ", 10_000, 20_000), + SANTA("์‚ฐํƒ€", 20_000, Integer.MAX_VALUE); + + private static final Badge[] badges = Badge.values(); + private final String name; + private final int minPrice; + private final int maxPrice; + + Badge(String name, int minPrice, int maxPrice) { + this.name = name; + this.minPrice = minPrice; + this.maxPrice = maxPrice; + } + + public static Badge from(int price) { + return Arrays.stream(badges) + .filter(badge -> badge.match(price)) + .findFirst() + .orElse(NONE); + } + + private boolean match(int price) { + return betweenBadgePrice(price); + } + + private boolean betweenBadgePrice(int price) { + int absPrice = Math.abs(price); + return absPrice >= minPrice && absPrice < maxPrice; + } + + public String getName() { + return name; + } +}
Java
์ €์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ์ •ํ•ด์ฃผ์‹œ๊ณ  ํƒ์ƒ‰ํ•˜์…จ๊ตฐ์š” !
@@ -0,0 +1,15 @@ +package christmas.domain.benefit.constance; + +public interface BenefitConst { + String GIFT_DESCRIPTION = "์ฆ์ • ์ด๋ฒคํŠธ"; + + int D_DAY_INITIAL_PRICE = 1_000; + int D_DAY_INCREASE_PRICE = 100; + int WEEKDAY_DISCOUNT_PRICE = 2_023; + int WEEKEND_DISCOUNT_PRICE = 2_023; + int SPECIAL_DISCOUNT_PRICE = 1_000; + + int MIN_PRICE_FOR_BENEFIT = 10_000; + + int MIN_PRICE_FOR_CHAMPAGNE = 120_000; +}
Java
์ €๋„ ๋งŽ์€ ๊ณ ๋ฏผ์„ ํ–ˆ์—ˆ๋˜ ๋ถ€๋ถ„์ธ๋ฐ, enum์œผ๋กœ ๊ด€๋ฆฌํ•˜์ง€ ์•Š๊ณ  ์ธํ„ฐํŽ˜์ด์Šค ์•ˆ์—์„œ ์ƒ์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜์‹œ๋Š” ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,32 @@ +package christmas.domain.benefit; + +import christmas.domain.benefit.constance.BenefitConst; +import christmas.domain.benefit.model.Benefit; +import christmas.domain.benefit.model.Benefits; +import christmas.domain.benefit.model.discount.DiscountFactories; +import christmas.domain.benefit.model.gift.GiftFactories; +import christmas.domain.date.model.PromotionDay; +import christmas.domain.menu.model.collection.OrderSheet; +import java.util.ArrayList; +import java.util.List; + +public class BenefitService { + + public Benefits getBenefits(PromotionDay promotionDay, OrderSheet orderSheet) { + if (orderSheet.getTotalPrice() < BenefitConst.MIN_PRICE_FOR_BENEFIT) { + return new Benefits(List.of()); + } + List<Benefit> benefits = new ArrayList<>(); + benefits.addAll(getGifts(promotionDay, orderSheet)); + benefits.addAll(getDiscounts(promotionDay, orderSheet)); + return new Benefits(benefits); + } + + private List<Benefit> getGifts(PromotionDay promotionDay, OrderSheet orderSheet) { + return GiftFactories.of(promotionDay, orderSheet); + } + + private List<Benefit> getDiscounts(PromotionDay promotionDay, OrderSheet orderSheet) { + return DiscountFactories.of(promotionDay, orderSheet); + } +}
Java
์ฝ”๋“œ๊ฐ€ ์ธ์ƒ์ ์ด๋„ค์š” :)
@@ -0,0 +1,11 @@ +package christmas.domain.benefit.model; + +import christmas.domain.date.model.PromotionDay; +import christmas.domain.menu.model.collection.OrderSheet; + +public interface BenefitFactory { + + boolean canApply(PromotionDay promotionDay, OrderSheet orderSheet); + + Benefit generate(PromotionDay promotionDay, OrderSheet orderSheet); +}
Java
generate๋ผ๋Š” ๋„ค์ด๋ฐ์€ ์ฃผ๋กœ ์–ด๋–ค ์˜๋ฏธ๋ฅผ ์ „๋‹ฌํ•˜๋ ค๊ณ  ํ•˜์‹ค ๋•Œ ์‚ฌ์šฉํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,44 @@ +package christmas.domain.benefit.model.discount; + +import christmas.domain.benefit.model.Benefit; +import java.util.Objects; + +public class Discount implements Benefit { + private final DiscountType type; + private final int price; + + Discount(DiscountType type, int price) { + this.type = type; + this.price = price; + } + @Override + public String getDescription() { + return type.getName(); + } + + @Override + public int getBenefitPrice() { + return -price; + } + + DiscountType getType() { + return type; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Discount discount = (Discount) o; + return price == discount.price && type == discount.type; + } + + @Override + public int hashCode() { + return Objects.hash(type, price); + } +}
Java
price๋„ ๊ฐ™์•„์•ผ ํ• ๊นŒ์š”?
@@ -0,0 +1,59 @@ +package christmas.domain.date.model; + +import christmas.domain.date.constance.DateConst; +import christmas.exception.PromotionException; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.Set; + +public class PromotionDay { + + private static final Set<DayOfWeek> weekend = Set.of(DayOfWeek.FRIDAY, DayOfWeek.SATURDAY); + + private static final Set<Integer> specialDay = Set.of(3, 10, 17, 24, 25, 31); + + private static final LocalDate christmas = LocalDate.of(DateConst.YEAR, DateConst.MONTH, 25); + + private final LocalDate localDate; + + private PromotionDay(LocalDate localDate) { + this.localDate = localDate; + } + + public static PromotionDay from(int date) { + validateDate(date); + LocalDate localDate = LocalDate.of(DateConst.YEAR, DateConst.MONTH, date); + return new PromotionDay(localDate); + } + + private static void validateDate(int date) { + if (date < DateConst.DECEMBER_DATE_START || date > DateConst.DECEMBER_DATE_END) { + throw PromotionException.INVALID_DATE.makeException(); + } + } + + public boolean isWeekend() { + return weekend.contains(localDate.getDayOfWeek()); + } + + public boolean isWeekDay() { + return !isWeekend(); + } + + public boolean isSpecialDay() { + int day = this.localDate.getDayOfMonth(); + return specialDay.contains(day); + } + + public int getDDayFromXMax() { + return christmas.compareTo(this.localDate); + } + + public int getDayFromStart() { + return getDate() - 1; + } + + public int getDate() { + return this.localDate.getDayOfMonth(); + } +}
Java
Start day๊ฐ€ ์š”๊ตฌ์‚ฌํ•ญ์— ๋”ฐ๋ผ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ 1์„ ์ƒ์ˆ˜๋กœ ๊ด€๋ฆฌ ํ•ด์คฌ์—ˆ๋Š”๋ฐ, ๋งค์ง ๋„˜๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ์ƒ์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ค‘ ์–ด๋А๊ฒƒ์ด ๋” ๊ดœ์ฐฎ์„๊นŒ์š”??
@@ -0,0 +1,41 @@ +package christmas.domain.badge.model; + +import java.util.Arrays; + +public enum Badge { + NONE("์—†์Œ", 0, 5_000), + STAR("๋ณ„", 5_000, 10_000), + TREE("ํŠธ๋ฆฌ", 10_000, 20_000), + SANTA("์‚ฐํƒ€", 20_000, Integer.MAX_VALUE); + + private static final Badge[] badges = Badge.values(); + private final String name; + private final int minPrice; + private final int maxPrice; + + Badge(String name, int minPrice, int maxPrice) { + this.name = name; + this.minPrice = minPrice; + this.maxPrice = maxPrice; + } + + public static Badge from(int price) { + return Arrays.stream(badges) + .filter(badge -> badge.match(price)) + .findFirst() + .orElse(NONE); + } + + private boolean match(int price) { + return betweenBadgePrice(price); + } + + private boolean betweenBadgePrice(int price) { + int absPrice = Math.abs(price); + return absPrice >= minPrice && absPrice < maxPrice; + } + + public String getName() { + return name; + } +}
Java
์—ด๊ฑฐํ˜• ๊ฐ’๋“ค์„ ์บ์‹ฑํ•ด๋‘๋Š” ์ „๋žต์ด ์ •๋ง ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค :) ์ถ”๊ฐ€์ ์œผ๋กœ `Badge[]`๋กœ ๋ฐฐ์ง€๋“ค์„ ์บ์‹ฑํ•ด๋‘” ์ด์œ ๋„ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! ์บ์‹ฑ์„ ํ–ˆ์ง€๋งŒ ๋ฐฐ์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— ์–ด์ฉ” ์ˆ˜ ์—†์ด ์ˆœํšŒํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•œ๋ฐ, ์–ด๋–ค ์ ์„ ์—ผ๋‘ํ•ด์„œ ์บ์‹ฑํ•˜์‹ ๊ฑด์ง€ ๊ถ๊ธˆํ•ด์š”!! ๋งค ๋ฒˆ `.values()`๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์€ ๋น„ํšจ์œจ์ ์ด๋ผ ํŒ๋‹จํ•˜์‹ ๊ฑธ๊นŒ์š”?!
@@ -0,0 +1,41 @@ +package christmas.domain.badge.model; + +import java.util.Arrays; + +public enum Badge { + NONE("์—†์Œ", 0, 5_000), + STAR("๋ณ„", 5_000, 10_000), + TREE("ํŠธ๋ฆฌ", 10_000, 20_000), + SANTA("์‚ฐํƒ€", 20_000, Integer.MAX_VALUE); + + private static final Badge[] badges = Badge.values(); + private final String name; + private final int minPrice; + private final int maxPrice; + + Badge(String name, int minPrice, int maxPrice) { + this.name = name; + this.minPrice = minPrice; + this.maxPrice = maxPrice; + } + + public static Badge from(int price) { + return Arrays.stream(badges) + .filter(badge -> badge.match(price)) + .findFirst() + .orElse(NONE); + } + + private boolean match(int price) { + return betweenBadgePrice(price); + } + + private boolean betweenBadgePrice(int price) { + int absPrice = Math.abs(price); + return absPrice >= minPrice && absPrice < maxPrice; + } + + public String getName() { + return name; + } +}
Java
๋ฐฐ์ง€์˜ ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์„ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•ด์ฃผ๊ณ , Predicate์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„ค์š”.. ์ •๋ง ์ข‹์€ ์ฝ”๋“œ์ธ ๊ฒƒ ๊ฐ™์•„์š”! ์ €๋Š” ์ตœ๋Œ“๊ฐ’์„ ์ƒ๊ฐ ๋ชปํ•ด์„œ if ๋ถ„๊ธฐ๋ฌธ์ด ์—ฌ๋Ÿฌ๊ฐœ ๋“ค์–ด๊ฐ”๋Š”๋ฐ, ํ˜์ˆ˜๋‹˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,41 @@ +package christmas.domain.badge.model; + +import java.util.Arrays; + +public enum Badge { + NONE("์—†์Œ", 0, 5_000), + STAR("๋ณ„", 5_000, 10_000), + TREE("ํŠธ๋ฆฌ", 10_000, 20_000), + SANTA("์‚ฐํƒ€", 20_000, Integer.MAX_VALUE); + + private static final Badge[] badges = Badge.values(); + private final String name; + private final int minPrice; + private final int maxPrice; + + Badge(String name, int minPrice, int maxPrice) { + this.name = name; + this.minPrice = minPrice; + this.maxPrice = maxPrice; + } + + public static Badge from(int price) { + return Arrays.stream(badges) + .filter(badge -> badge.match(price)) + .findFirst() + .orElse(NONE); + } + + private boolean match(int price) { + return betweenBadgePrice(price); + } + + private boolean betweenBadgePrice(int price) { + int absPrice = Math.abs(price); + return absPrice >= minPrice && absPrice < maxPrice; + } + + public String getName() { + return name; + } +}
Java
 NONE์œผ๋กœ ์—†์Œ ์ฒ˜๋ฆฌํ•˜์‹  ๊ฒƒ๋„ ์ข‹์€ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์ด๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค :) `Optional`๋„ ๊ณ ๋ คํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”! ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด from ๋ฉ”์„œ๋“œ๊ฐ€ ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฐ”๋€” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ```java public static Optional<Badge> from(int price) { Optional<Badge> result = Arrays.stream(badges) .filter(badge -> badge.match(price)) .findFirst(); if(result.isPresent()) { return Optional.get(); } return Optional.empty(): } ``` ์ •ํ™•ํžˆ ์–ด๋А ์ง€์ ์—์„œ Optional์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€, Enum ๋‚ด๋ถ€์—์„œ Optional์„ ์‚ฌ์šฉํ•ด๋„ ๊ดœ์ฐฎ์„์ง€ ์ €๋„ ์•„์ง ๋ช…ํ™•ํ•œ ๊ธฐ์ค€์€ ์—†์ง€๋งŒ **์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค ๋ผ๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ์š”๊ตฌ์‚ฌํ•ญ์„ ์ฝ”๋“œ๋กœ ๋ช…์‹œ** ํ•  ์ˆ˜ ์žˆ๋Š” ์ธก๋ฉด์—์„œ ์•„์ฃผ ๊ฐ•๋ ฅํ•œ ์žฅ์ ์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š” :) ๊ทธ๋ƒฅ ์ฐธ๊ณ  ์ •๋„๋งŒ ํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ๐Ÿ˜Š
@@ -0,0 +1,32 @@ +package christmas.domain.benefit; + +import christmas.domain.benefit.constance.BenefitConst; +import christmas.domain.benefit.model.Benefit; +import christmas.domain.benefit.model.Benefits; +import christmas.domain.benefit.model.discount.DiscountFactories; +import christmas.domain.benefit.model.gift.GiftFactories; +import christmas.domain.date.model.PromotionDay; +import christmas.domain.menu.model.collection.OrderSheet; +import java.util.ArrayList; +import java.util.List; + +public class BenefitService { + + public Benefits getBenefits(PromotionDay promotionDay, OrderSheet orderSheet) { + if (orderSheet.getTotalPrice() < BenefitConst.MIN_PRICE_FOR_BENEFIT) { + return new Benefits(List.of()); + } + List<Benefit> benefits = new ArrayList<>(); + benefits.addAll(getGifts(promotionDay, orderSheet)); + benefits.addAll(getDiscounts(promotionDay, orderSheet)); + return new Benefits(benefits); + } + + private List<Benefit> getGifts(PromotionDay promotionDay, OrderSheet orderSheet) { + return GiftFactories.of(promotionDay, orderSheet); + } + + private List<Benefit> getDiscounts(PromotionDay promotionDay, OrderSheet orderSheet) { + return DiscountFactories.of(promotionDay, orderSheet); + } +}
Java
if ์กฐ๊ฑด๋ฌธ์€ ๊ฐœ์ธ์ ์œผ๋กœ ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๊ทธ ์ด์œ ๋Š” 20, 21 line์—์„  ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ํ†ตํ•ด ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋™์ผํ•œ depth๋กœ ํ†ต์ผ์‹œ์ผœ ์ฃผ์‹ ๋‹ค๋ฉด ์กฐ๊ธˆ ๋” ์ฝ๊ธฐ ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š” :)
@@ -0,0 +1,19 @@ +package christmas.domain.benefit.model.discount; + +public enum DiscountType { + D_DAY("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"), //12์›” 25์ผ์„ ์ง€๋‚˜์ง€ ์•Š์€ ๊ฒฝ์šฐ //๋‚ ์งœ์— ๋”ฐ๋ผ 100์›์”ฉ ์ฆ๊ฐ€ + WEEKDAY("ํ‰์ผ ํ• ์ธ"), //ํ‰์ผ์ธ ๊ฒฝ์šฐ // ๋””์ €ํŠธ ๋ฉ”๋‰ด * ๊ธˆ์•ก + WEEKEND("์ฃผ๋ง ํ• ์ธ"), //์ฃผ๋ง์ธ ๊ฒฝ์šฐ // ๋ฉ”์ธ ๋ฉ”๋‰ด * ๊ธˆ์•ก + SPECIAL("ํŠน๋ณ„ ํ• ์ธ") //์ŠคํŽ˜์…œ ๋ฐ์ด์ธ ๊ฒฝ์šฐ // ๋ฌด์กฐ๊ฑด 1000์› + ; + + private final String name; + + DiscountType(String name) { + this.name = name; + } + + String getName() { + return name; + } +}
Java
์ €๋Š” `ํ• ์ธ ์œ ํ˜•(DiscountType)`, `ํ• ์ธ ์œ ํ˜•์— ๋Œ€ํ•œ ๋‚ ์งœ`, `ํ• ์ธ ์œ ํ˜•์— ๋Œ€ํ•œ ํ• ์ธ ๊ณ„์‚ฐ ๋กœ์ง` ์œ„์˜ 3๊ฐ€์ง€๋Š” DiscountType Enum๊ณผ **์•„์ฃผ ๊ธด๋ฐ€ํ•˜๊ฒŒ ์—ฐ๊ด€๋˜์–ด ์žˆ๋Š” ์ƒ์ˆ˜**๋ผ ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค! ๊ทธ๋ž˜์„œ ์ด๋Ÿฐ์‹์œผ๋กœ DiscountType์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ```java public enum DiscountType { CHRISTMAS("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ", EventDayCalculator.christmasDay(), orderDay -> { final int basicDiscountAmount = 1000; final int discountAmountUnit = 100; return (basicDiscountAmount + discountAmountUnit * (orderDay - 1)) * -1; }), WEEKDAY("ํ‰์ผ ํ• ์ธ", EventDayCalculator.weekDay(), dessertCount -> { final int discountAmountUnit = 2023; return (discountAmountUnit * dessertCount) * -1; }), WEEKEND("์ฃผ๋ง ํ• ์ธ", EventDayCalculator.weekEnd(), mainCourseCount -> { final int discountAmountUnit = 2023; return (discountAmountUnit * mainCourseCount) * -1; }), SPECIAL("ํŠน๋ณ„ ํ• ์ธ", EventDayCalculator.specialDay(), anything -> { return -1000; }); // something ``` ์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋‹ˆ **ํ• ์ธ ์œ ํ˜•์ด๋ผ๋Š” ์ƒํƒœ** ์™€ **๊ณ„์‚ฐ ๋กœ์ง์ด๋ผ๋Š” ํ–‰์œ„** ๋ฅผ ๋ฐ€์ง‘์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ• ์ธ ์œ ํ˜•์ด๋ผ๋Š” ์ƒํƒœ์™€ ๊ณ„์‚ฐ์ด๋ผ๋Š” ํ–‰์œ„๊ฐ€ ์‚ฐ๊ฐœ๋˜์–ด ์žˆ์—ˆ๋Š”๋ฐ, ๋ฐ€์ง‘์‹œํ‚ค๋‹ˆ ๊ต‰์žฅํžˆ ๊ด€๋ฆฌํ•˜๊ธฐ ํŽธํ•˜๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค๋”๋ผ๊ตฌ์š”. ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์œผ๋กœ ์งฑ์ˆ˜๋‹˜ ์ฝ”๋“œ๋ฅผ ๋ฆฌํŒฉํ„ฐ๋ง ํ•œ๋‹ค๋ฉด, `BenefitFactory`์˜ `isApply()`๋ฅผ `DiscountType`์ด ๋‚ด๋ถ€์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๊ณ , ํ• ์ธ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ผ๋Š” ๋ช…๋ น ์ฒ˜๋ฆฌ ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค๋งŒ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ฝ”๋“œ ๋ณต์žก๋„๊ฐ€ ์กฐ๊ธˆ ์ค„์–ด๋“ค์ง€ ์•Š์„๊นŒ..? ๋ผ๋Š” ๊ฐœ์ธ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค ใ…Žใ…Žใ…Ž ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์€ ์–ด๋– ์‹ ์ง€ ์งฑ์ˆ˜๋‹˜์˜ ์ƒ๊ฐ์„ ์—ฌ์ญ™๊ณ  ์‹ถ์–ด์š” :)
@@ -0,0 +1,52 @@ +package christmas.domain.menu.model; + +import christmas.exception.PromotionException; +import java.util.Arrays; + +public enum Menu { + + MUSHROOM_SOUP(MenuType.APPETIZER, "์–‘์†ก์ด์ˆ˜ํ”„", 6_000), + TAPAS(MenuType.APPETIZER, "ํƒ€ํŒŒ์Šค", 5_500), + CAESAR_SALAD(MenuType.APPETIZER, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000), + + T_BONE_STEAK(MenuType.MAIN, "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000), + BARBCUE_RIBS(MenuType.MAIN, "๋ฐ”๋น„ํ๋ฆฝ", 54_000), + SEAFOOD_PASTA(MenuType.MAIN, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000), + CHRISTMAS_PASTA(MenuType.MAIN, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000), + + CHOCOLATE_CAKE(MenuType.DESSERT, "์ดˆ์ฝ”์ผ€์ดํฌ", 15_000), + ICE_CREAM(MenuType.DESSERT, "์•„์ด์Šคํฌ๋ฆผ", 5_000), + + ZERO_COKE(MenuType.DRINK, "์ œ๋กœ์ฝœ๋ผ", 3_000), + RED_WINE(MenuType.DRINK, "๋ ˆ๋“œ์™€์ธ", 60_000), + CHAMPAGNE(MenuType.DRINK, "์ƒดํŽ˜์ธ", 25_000); + + private final MenuType type; + private final String name; + private final int price; + + Menu(MenuType type, String name, int price) { + this.type = type; + this.name = name; + this.price = price; + } + + public static Menu from(String menuName) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.name.equals(menuName)) + .findFirst() + .orElseThrow(PromotionException.INVALID_ORDER::makeException); + } + + public boolean isTypeOf(MenuType type){ + return this.type.equals(type); + } + + String getName() { + return name; + } + + int getPrice() { + return price; + } +}
Java
Badge Enum์—์„œ `.values()`๋ฅผ `๋ฐฐ์—ด๋กœ ์บ์‹ฑ`ํ•˜์‹  ๊ฒƒ๊ณผ ๊ฐ™์€ ๋งฅ๋ฝ์œผ๋กœ `HashMap์œผ๋กœ ์บ์‹ฑ` ํ•ด๋‘๋Š” ๋ฐฉ๋ฒ•๋„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. Enum์—์„  static ๋ธ”๋ก์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ์š”, ์š”๋Ÿฐ์‹์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ```java public enum Menu { // ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜๋“ค ... private static final Map<String, Menu> cachedMenu; static { for (Menu menu : values()) { cachedMenu.put(menu.getName(), menu); } } } ``` ์ด๋ ‡๊ฒŒ Map์œผ๋กœ ์บ์‹ฑํ•ด๋‘๋ฉด, ๋งค ๋ฒˆ ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜๋“ค์„ ์ˆœํšŒํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค :) ํ•˜์ง€๋งŒ NPE๊ฐ€ ๋ฐœ์ƒํ•  ์œ„ํ—˜์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, Optional์„ ์‚ฌ์šฉํ•ด๋ณด์‹œ๋Š” ๊ฒƒ๋„ ๊ณ ๋ คํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ```java public static Optional<Menu> from(String menuName) { return Optional.ofNullalbe(cachedMenu.get(menuName); } ``` ์œ„์™€ ๊ฐ™์ด ๋ฆฌํŒฉํ„ฐ๋ง ํ•ด๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š” :)
@@ -0,0 +1,37 @@ +package christmas.domain.menu.model.collection; + +import christmas.domain.menu.model.MenuAndCount; +import christmas.domain.menu.model.MenuType; +import java.util.Collections; +import java.util.List; + +/** ์ฃผ๋ฌธ ํ™•์ธ์„ ์œ„ํ•œ ๋ถˆ๋ณ€ ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜ ์ž…๋‹ˆ๋‹ค. */ +public class OrderSheet { + private final List<MenuAndCount> orderSheet; + + public OrderSheet(List<MenuAndCount> orderSheet) { + this.orderSheet = orderSheet; + } + + public List<MenuAndCount> getOrderSheet() { + return Collections.unmodifiableList(orderSheet); + } + + public int getTotalPrice() { + return orderSheet.stream() + .mapToInt(MenuAndCount::getTotalPrice) + .sum(); + } + + public boolean hasMenuOfType(MenuType type) { + return orderSheet.stream() + .anyMatch(menuAndCount -> menuAndCount.isTypeOf(type)); + } + + public int getCountOfMenuType(MenuType type) { + return orderSheet.stream() + .filter(menuAndCount -> menuAndCount.isTypeOf(type)) + .mapToInt(MenuAndCount::getCount) + .sum(); + } +}
Java
`OrderSheet`์™€ `Orders`๋ฅผ ๋ถ„๋ฆฌํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค !
@@ -0,0 +1,10 @@ +package christmas.domain.badge; + +import christmas.domain.badge.model.Badge; +import christmas.domain.benefit.model.Benefits; + +public class BadgeService { + public Badge getBadge(Benefits benefits) { + return Badge.from(benefits.getTotalPrice()); + } +}
Java
ํŒจํ‚ค์ง€ ๊ตฌ์กฐ ์ „๋ฐ˜์— ๋Œ€ํ•œ ์งฑ์ˆ˜๋‹˜์˜ ์˜๊ฒฌ์ด ๊ถ๊ธˆํ•ด์„œ ์งˆ๋ฌธ ๋“œ๋ ค์š”! ์งฑ์ˆ˜๋‹˜์˜ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ์ง„ํ–‰ํ•˜์…จ๋”๋ผ๊ตฌ์š”! ``` domain ใ„ด badge ใ„ด ๊ด€๋ จ domain ๊ฐ์ฒด ใ„ด badgeService ใ„ด benefit ใ„ด ๊ด€๋ จ domain ๊ฐ์ฒด ใ„ด benefitService ``` ์ €๋Š” ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ์š”! ``` domain ใ„ด badge ใ„ด ๊ด€๋ จ domain ๊ฐ์ฒด ใ„ด benefit ใ„ด ๊ด€๋ จ domain ๊ฐ์ฒด service ใ„ด badgeService ใ„ด benefitService ``` ์ฒ˜์Œ ์ œ ์ƒ๊ฐ์€ `domain layer`๋Š” domain ๊ด€๋ จ ๊ฐ์ฒด๋“ค๋งŒ ๋ชจ์•„ ๋†“๋Š” ๊ณณ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์—ฌ ์œ„์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. `Service layer`๋Š” ์ ์ ˆํ•œ domain์—๊ฒŒ ๋ช…๋ น์„ ์š”์ฒญํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์งฑ์ˆ˜๋‹˜์˜ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ๋ฅผ ๋ณด๋‹ˆ `์‘์ง‘๋„๊ฐ€ ๋” ๋†’์•„์งˆ ์ˆ˜ ์žˆ๊ฒ ๊ตฌ๋‚˜` ๋ผ๋Š” ์žฅ์ ์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š”! ํ”„๋กœ๋•ํŠธ ์ง„ํ–‰ํ•˜๋ฉฐ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ๋ฅผ ์œ ๊ธฐ์ ์œผ๋กœ ๋ฐ”๊พธ์–ด ๋‚˜๊ฐ€๋Š”๊ฒŒ ์ค‘์š”ํ•˜๋‹ค๋Š” ์ƒ๊ฐ์ด ์žˆ๋Š”๋ฐ, ์งฑ์ˆ˜๋‹˜์€ ์–ด๋–ค ๊ธฐ์ค€์œผ๋กœ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ๋ฅผ ์œ„์™€ ๊ฐ™์ด ๊ตฌ์กฐํ™”ํ•˜์‹ ๊ฑธ๊นŒ์š”?!
@@ -0,0 +1,59 @@ +package christmas.domain.date.model; + +import christmas.domain.date.constance.DateConst; +import christmas.exception.PromotionException; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.Set; + +public class PromotionDay { + + private static final Set<DayOfWeek> weekend = Set.of(DayOfWeek.FRIDAY, DayOfWeek.SATURDAY); + + private static final Set<Integer> specialDay = Set.of(3, 10, 17, 24, 25, 31); + + private static final LocalDate christmas = LocalDate.of(DateConst.YEAR, DateConst.MONTH, 25); + + private final LocalDate localDate; + + private PromotionDay(LocalDate localDate) { + this.localDate = localDate; + } + + public static PromotionDay from(int date) { + validateDate(date); + LocalDate localDate = LocalDate.of(DateConst.YEAR, DateConst.MONTH, date); + return new PromotionDay(localDate); + } + + private static void validateDate(int date) { + if (date < DateConst.DECEMBER_DATE_START || date > DateConst.DECEMBER_DATE_END) { + throw PromotionException.INVALID_DATE.makeException(); + } + } + + public boolean isWeekend() { + return weekend.contains(localDate.getDayOfWeek()); + } + + public boolean isWeekDay() { + return !isWeekend(); + } + + public boolean isSpecialDay() { + int day = this.localDate.getDayOfMonth(); + return specialDay.contains(day); + } + + public int getDDayFromXMax() { + return christmas.compareTo(this.localDate); + } + + public int getDayFromStart() { + return getDate() - 1; + } + + public int getDate() { + return this.localDate.getDayOfMonth(); + } +}
Java
ํ•˜๋“œ์ฝ”๋”ฉํ•˜์ง€ ์•Š๊ณ ๋„ LocalDate๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋˜๋Š”๊ตฐ์š”.. ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค ๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,106 @@ +package christmas; + +import christmas.domain.badge.BadgeService; +import christmas.domain.badge.model.Badge; +import christmas.domain.benefit.BenefitService; +import christmas.domain.benefit.model.Benefits; +import christmas.domain.bills.Bills; +import christmas.domain.date.DateService; +import christmas.domain.date.model.PromotionDay; +import christmas.domain.menu.MenuService; +import christmas.domain.menu.model.collection.OrderSheet; +import christmas.exception.handler.ExceptionHandler; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.List; +import java.util.function.Supplier; + +public class Controller { + private final DateService dateService = new DateService(); + private final MenuService menuService = new MenuService(); + private final BenefitService benefitService = new BenefitService(); + private final BadgeService badgeService = new BadgeService(); + + private final InputView inputView = new InputView(); + + private final OutputView outputView = new OutputView(); + private final ExceptionHandler handler; + + public Controller(ExceptionHandler handler) { + this.handler = handler; + } + + public void run() { + printHello(); + + PromotionDay promotionDay = getPromotionDay(); + OrderSheet orderSheet = getOrderSheet(); + Benefits benefits = getBenefits(promotionDay, orderSheet); + Badge badge = getBadge(benefits); + + printResult(Bills.builder() + .date(promotionDay.getDate()) + .orderSheet(orderSheet) + .benefits(benefits) + .Badge(badge) + .build()); + } + + private void printHello() { + outputView.sayHello(); + } + + private PromotionDay getPromotionDay() { + return getResultUsingExceptionHandler(() -> { + int visitDate = inputView.getVisitDate(); + return dateService.getPromotionDay(visitDate); + }); + } + + private OrderSheet getOrderSheet() { + return getResultUsingExceptionHandler(() -> { + List<String> orders = inputView.getOrders(); + inputView.closeConsole(); + return menuService.getOrderSheet(orders.toArray(String[]::new)); + }); + } + + private <T> T getResultUsingExceptionHandler(Supplier<T> logic) { + return handler.get(logic, outputView::printException); + } + + private Benefits getBenefits(PromotionDay promotionDay, OrderSheet orderSheet) { + return benefitService.getBenefits(promotionDay, orderSheet); + } + + private Badge getBadge(Benefits benefits) { + return badgeService.getBadge(benefits); + } + + private void printResult(Bills bills) { + outputView.printBenefitTitle(bills.getDate()); + printOrderSheeet(bills.getOrderSheet()); + printBenefits(bills.getBenefits()); + printDiscountedPrice(bills); + printBadge(bills.getBadge()); + } + + private void printOrderSheeet(OrderSheet orderSheet) { + outputView.printOrderMenu(orderSheet); + outputView.printTotalPriceNoDiscount(orderSheet); + } + + private void printBenefits(Benefits benefits) { + outputView.printGifts(benefits); + outputView.printBenefits(benefits); + outputView.printTotalBenefitPrice(benefits); + } + + private void printDiscountedPrice(Bills bills) { + outputView.printDiscountedPrice(bills.getDiscountedPrice()); + } + + private void printBadge(Badge badge) { + outputView.printBadge(badge); + } +}
Java
BIlls๋ผ๋Š” ๋ณ„๋„์˜ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ , ๋นŒ๋”๋ฅผ ์ด์šฉํ•˜๋‹ˆ ์ถœ๋ ฅ์ด ์ •๋ง ๊น”๋”ํ•ด์ง€๋„ค์š”! ์ •๋ง ์ข‹์€ ์•„์ด๋””์–ด ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,10 @@ +package christmas.domain.badge; + +import christmas.domain.badge.model.Badge; +import christmas.domain.benefit.model.Benefits; + +public class BadgeService { + public Badge getBadge(Benefits benefits) { + return Badge.from(benefits.getTotalPrice()); + } +}
Java
์ €๋„ ์ด ๋ถ€๋ถ„์ด ๊ถ๊ธˆํ•ด์„œ ์ปค๋ฉ˜ํŠธ ๋‚จ๊ธฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค! ์ €๋„ june๋‹˜๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ, ์ฝ๋Š” ์ž…์žฅ์—์„œ ๋ณด๋‹ˆ ๋„๋ฉ”์ธ ์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ์„œ๋น„์Šค ์ฝ”๋“œ๋ฅผ ์ฝ์œผ๋‹ˆ ์ดํ•ด๊ฐ€ ํ›จ์”ฌ ์ˆ˜์›”ํ•˜๋”๋ผ๊ตฌ์š”!
@@ -0,0 +1,41 @@ +package christmas.domain.badge.model; + +import java.util.Arrays; + +public enum Badge { + NONE("์—†์Œ", 0, 5_000), + STAR("๋ณ„", 5_000, 10_000), + TREE("ํŠธ๋ฆฌ", 10_000, 20_000), + SANTA("์‚ฐํƒ€", 20_000, Integer.MAX_VALUE); + + private static final Badge[] badges = Badge.values(); + private final String name; + private final int minPrice; + private final int maxPrice; + + Badge(String name, int minPrice, int maxPrice) { + this.name = name; + this.minPrice = minPrice; + this.maxPrice = maxPrice; + } + + public static Badge from(int price) { + return Arrays.stream(badges) + .filter(badge -> badge.match(price)) + .findFirst() + .orElse(NONE); + } + + private boolean match(int price) { + return betweenBadgePrice(price); + } + + private boolean betweenBadgePrice(int price) { + int absPrice = Math.abs(price); + return absPrice >= minPrice && absPrice < maxPrice; + } + + public String getName() { + return name; + } +}
Java
orElse์— ์ƒ์ˆ˜์™€ ๊ด€๋ จ์—†๋Š” ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์ด ๋ดค๋Š”๋ฐ, ์ด ์ฝ”๋“œ์—์„  orElse๋งˆ์ € ์—ญํ• ์ด ๋ถ„๋ช…ํ•ด์„œ ๋„ˆ๋ฌด ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,41 @@ +package christmas.domain.badge.model; + +import java.util.Arrays; + +public enum Badge { + NONE("์—†์Œ", 0, 5_000), + STAR("๋ณ„", 5_000, 10_000), + TREE("ํŠธ๋ฆฌ", 10_000, 20_000), + SANTA("์‚ฐํƒ€", 20_000, Integer.MAX_VALUE); + + private static final Badge[] badges = Badge.values(); + private final String name; + private final int minPrice; + private final int maxPrice; + + Badge(String name, int minPrice, int maxPrice) { + this.name = name; + this.minPrice = minPrice; + this.maxPrice = maxPrice; + } + + public static Badge from(int price) { + return Arrays.stream(badges) + .filter(badge -> badge.match(price)) + .findFirst() + .orElse(NONE); + } + + private boolean match(int price) { + return betweenBadgePrice(price); + } + + private boolean betweenBadgePrice(int price) { + int absPrice = Math.abs(price); + return absPrice >= minPrice && absPrice < maxPrice; + } + + public String getName() { + return name; + } +}
Java
์—ฌ๊ธฐ์„œ ์ ˆ๋Œ“๊ฐ’์„ ์ด์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!!
@@ -0,0 +1,32 @@ +package christmas.domain.benefit; + +import christmas.domain.benefit.constance.BenefitConst; +import christmas.domain.benefit.model.Benefit; +import christmas.domain.benefit.model.Benefits; +import christmas.domain.benefit.model.discount.DiscountFactories; +import christmas.domain.benefit.model.gift.GiftFactories; +import christmas.domain.date.model.PromotionDay; +import christmas.domain.menu.model.collection.OrderSheet; +import java.util.ArrayList; +import java.util.List; + +public class BenefitService { + + public Benefits getBenefits(PromotionDay promotionDay, OrderSheet orderSheet) { + if (orderSheet.getTotalPrice() < BenefitConst.MIN_PRICE_FOR_BENEFIT) { + return new Benefits(List.of()); + } + List<Benefit> benefits = new ArrayList<>(); + benefits.addAll(getGifts(promotionDay, orderSheet)); + benefits.addAll(getDiscounts(promotionDay, orderSheet)); + return new Benefits(benefits); + } + + private List<Benefit> getGifts(PromotionDay promotionDay, OrderSheet orderSheet) { + return GiftFactories.of(promotionDay, orderSheet); + } + + private List<Benefit> getDiscounts(PromotionDay promotionDay, OrderSheet orderSheet) { + return DiscountFactories.of(promotionDay, orderSheet); + } +}
Java
์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•ด์„œ ์ดํ•ดํ•˜๊ธฐ ์ •๋ง ์‰ฝ์ง€๋งŒ, if๋ฌธ ์•ˆ์˜ ์กฐ๊ฑด์‹์„ "ํ• ์ธ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ๊ฒฝ์šฐ"๋ฅผ ์•”์‹œํ•˜๋Š” ๋ณ„๋„์˜ ๋ฉ”์„œ๋“œ๋กœ ๋ถ„๋ฆฌํ•˜๋ฉด ๋”๋”์šฑ ์ฝ๊ธฐ ์ข‹์•„์งˆ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!!
@@ -0,0 +1,15 @@ +package christmas.domain.benefit.constance; + +public interface BenefitConst { + String GIFT_DESCRIPTION = "์ฆ์ • ์ด๋ฒคํŠธ"; + + int D_DAY_INITIAL_PRICE = 1_000; + int D_DAY_INCREASE_PRICE = 100; + int WEEKDAY_DISCOUNT_PRICE = 2_023; + int WEEKEND_DISCOUNT_PRICE = 2_023; + int SPECIAL_DISCOUNT_PRICE = 1_000; + + int MIN_PRICE_FOR_BENEFIT = 10_000; + + int MIN_PRICE_FOR_CHAMPAGNE = 120_000; +}
Java
์ €๋„ ์ด ๋ถ€๋ถ„์€ ๊ถ๊ธˆํ•ด์„œ ๋Œ“๊ธ€ ๋‚จ๊ธฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค!!
@@ -0,0 +1,40 @@ +package christmas.domain.benefit.model; + +import christmas.domain.benefit.model.discount.Discount; +import christmas.domain.benefit.model.gift.Gifts; +import christmas.domain.menu.model.MenuAndCount; +import java.util.Collections; +import java.util.List; + +public class Benefits { + private final List<Benefit> benefits; + + public Benefits(List<Benefit> benefits) { + this.benefits = benefits; + } + + public List<MenuAndCount> getGifts() { + return benefits.stream() + .filter(benefit -> benefit.getClass().equals(Gifts.class)) + .map(gift -> ((Gifts) gift).getGifts()) + .flatMap(List::stream) + .toList(); + } + + public List<Benefit> getBenefits() { + return Collections.unmodifiableList(benefits); + } + + public int getTotalPrice() { + return benefits.stream() + .mapToInt(Benefit::getBenefitPrice) + .sum(); + } + + public int getDiscountPrice() { + return benefits.stream() + .filter(benefit -> benefit.getClass().equals(Discount.class)) + .mapToInt(Benefit::getBenefitPrice) + .sum(); + } +}
Java
getClass ๋Œ€์‹  instanceOf๋ฅผ ์‚ฌ์šฉํ• ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,40 @@ +package christmas.domain.benefit.model; + +import christmas.domain.benefit.model.discount.Discount; +import christmas.domain.benefit.model.gift.Gifts; +import christmas.domain.menu.model.MenuAndCount; +import java.util.Collections; +import java.util.List; + +public class Benefits { + private final List<Benefit> benefits; + + public Benefits(List<Benefit> benefits) { + this.benefits = benefits; + } + + public List<MenuAndCount> getGifts() { + return benefits.stream() + .filter(benefit -> benefit.getClass().equals(Gifts.class)) + .map(gift -> ((Gifts) gift).getGifts()) + .flatMap(List::stream) + .toList(); + } + + public List<Benefit> getBenefits() { + return Collections.unmodifiableList(benefits); + } + + public int getTotalPrice() { + return benefits.stream() + .mapToInt(Benefit::getBenefitPrice) + .sum(); + } + + public int getDiscountPrice() { + return benefits.stream() + .filter(benefit -> benefit.getClass().equals(Discount.class)) + .mapToInt(Benefit::getBenefitPrice) + .sum(); + } +}
Java
์ด ๋ถ€๋ถ„์—์„œ ์บ์ŠคํŒ… ๋ง๊ณ  ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”์ง€ ๋‹ค๋ฅธ ๋ฆฌ๋ทฐ์–ด๋ถ„๋“ค์˜ ์˜๊ฒฌ์ด ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! ์ €๋„ ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ ๋‹ต์„ ๋ชป ๋‚ด๋ ธ๋„ค์š”
@@ -0,0 +1,92 @@ +package christmas.domain.benefit.model.discount; + +import christmas.domain.benefit.constance.BenefitConst; +import christmas.domain.benefit.model.Benefit; +import christmas.domain.benefit.model.BenefitFactory; +import christmas.domain.date.model.PromotionDay; +import christmas.domain.menu.model.MenuType; +import christmas.domain.menu.model.collection.OrderSheet; +import java.util.Arrays; +import java.util.List; + +/** + * Discount ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—ญํ• ๋“ค์„ `enum` ์ƒ์ˆ˜๋กœ ๊ด€๋ฆฌ + */ +public enum DiscountFactories implements BenefitFactory { + + D_DAY_FACTORY(DiscountType.D_DAY) { + @Override + public boolean canApply(PromotionDay promotionDay, OrderSheet orderSheet) { + return promotionDay.getDDayFromXMax() >= 0; + } + + @Override + int getDiscountPrice(PromotionDay promotionDay, OrderSheet orderSheet) { + return BenefitConst.D_DAY_INITIAL_PRICE + + promotionDay.getDayFromStart() * BenefitConst.D_DAY_INCREASE_PRICE; + } + }, + WEEKDAY_FACTORY(DiscountType.WEEKDAY) { + @Override + public boolean canApply(PromotionDay promotionDay, OrderSheet orderSheet) { + return promotionDay.isWeekDay() && + orderSheet.hasMenuOfType(MenuType.DESSERT); + } + + @Override + int getDiscountPrice(PromotionDay promotionDay, OrderSheet orderSheet) { + return orderSheet.getCountOfMenuType(MenuType.DESSERT) * + BenefitConst.WEEKDAY_DISCOUNT_PRICE; + } + }, + WEEKEND_FACTORY(DiscountType.WEEKEND) { + @Override + public boolean canApply(PromotionDay promotionDay, OrderSheet orderSheet) { + return promotionDay.isWeekend() && + orderSheet.hasMenuOfType(MenuType.MAIN); + } + + @Override + int getDiscountPrice(PromotionDay promotionDay, OrderSheet orderSheet) { + return orderSheet.getCountOfMenuType(MenuType.MAIN) * + BenefitConst.WEEKEND_DISCOUNT_PRICE; + } + }, + SPECIAL_FACTORY(DiscountType.SPECIAL) { + @Override + public boolean canApply(PromotionDay promotionDay, OrderSheet orderSheet) { + return promotionDay.isSpecialDay(); + } + + @Override + int getDiscountPrice(PromotionDay promotionDay, OrderSheet orderSheet) { + return BenefitConst.SPECIAL_DISCOUNT_PRICE; + } + }; + + private static final DiscountFactories[] discountFactories = DiscountFactories.values(); + + private final DiscountType type; + + DiscountFactories(DiscountType type) { + this.type = type; + } + + public static List<Benefit> of(PromotionDay promotionDay, OrderSheet orderSheet) { + return Arrays.stream(discountFactories) + .filter(factory -> factory.canApply(promotionDay, orderSheet)) + .map(factory -> factory.generate(promotionDay, orderSheet)) + .toList(); + } + + abstract int getDiscountPrice(PromotionDay promotionDay, OrderSheet orderSheet); + + @Override + public Benefit generate(PromotionDay promotionDay, OrderSheet orderSheet) { + return new Discount(this.getType(), getDiscountPrice(promotionDay, orderSheet)); + } + + private DiscountType getType() { + return type; + } +}
Java
enum๊ณผ ํŒฉํ† ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ฒ˜์Œ ๋ณด๋Š”๋ฐ, ์ฝ”๋“œ์˜ ๋ถ„๋Ÿ‰์ด๋‚˜ ์ค‘๋ณต์„ ๋– ๋‚˜์„œ ๊ฐ€๋…์„ฑ ๋ฉด์—์„œ๋Š” ์ •๋ง ํ›Œ๋ฅญํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,19 @@ +package christmas.domain.benefit.model.discount; + +public enum DiscountType { + D_DAY("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"), //12์›” 25์ผ์„ ์ง€๋‚˜์ง€ ์•Š์€ ๊ฒฝ์šฐ //๋‚ ์งœ์— ๋”ฐ๋ผ 100์›์”ฉ ์ฆ๊ฐ€ + WEEKDAY("ํ‰์ผ ํ• ์ธ"), //ํ‰์ผ์ธ ๊ฒฝ์šฐ // ๋””์ €ํŠธ ๋ฉ”๋‰ด * ๊ธˆ์•ก + WEEKEND("์ฃผ๋ง ํ• ์ธ"), //์ฃผ๋ง์ธ ๊ฒฝ์šฐ // ๋ฉ”์ธ ๋ฉ”๋‰ด * ๊ธˆ์•ก + SPECIAL("ํŠน๋ณ„ ํ• ์ธ") //์ŠคํŽ˜์…œ ๋ฐ์ด์ธ ๊ฒฝ์šฐ // ๋ฌด์กฐ๊ฑด 1000์› + ; + + private final String name; + + DiscountType(String name) { + this.name = name; + } + + String getName() { + return name; + } +}
Java
์ €๋Š” june๋‹˜์˜ ๋ฐฉ๋ฒ•๋„, zangsu๋‹˜์˜ ๋ฐฉ๋ฒ•๋„ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค! Factory๋ฅผ ๋ณ„๋„๋กœ ์ •์˜ํ•˜๋‹ˆ ์ฝ”๋“œ๋Ÿ‰ ์ž์ฒด๋Š” ๋งŽ์•„์ง€๊ธด ํ–ˆ์ง€๋งŒ, ๊ฐ ํ• ์ธ ์ƒ์„ฑ์˜ ๋ฐฉ๋ฒ•์ด ๋ช…ํ™•ํ•˜๊ฒŒ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,82 @@ +package christmas.controller; + +import java.util.Set; +import java.util.function.Supplier; + +import christmas.domain.event.PresentEvent; +import christmas.dto.result.PresentEventResult; +import christmas.service.EventPlannerService; +import christmas.domain.order.OrderSheet; +import christmas.dto.order.OrderInput; +import christmas.dto.order.VisitDate; +import christmas.dto.result.EventResult; +import christmas.exception.CommonIllegalArgumentException; +import christmas.view.InputView; +import christmas.view.OutputView; + +public class EventPlannerController { + private final EventPlannerService eventPlannerService; + + public EventPlannerController(EventPlannerService eventPlannerService) { + this.eventPlannerService = eventPlannerService; + } + + public void run() { + OutputView.printGreeting(); + + OrderSheet orderSheet = getOrders(); + + OutputView.printStartEventPlanner(orderSheet.getVisitDate()); + + showOrders(orderSheet); + showEventBenefits(orderSheet); + } + + private OrderSheet getOrders() { + VisitDate visitDate = tryUntilInputIsValid(this::getVisitDate); + OrderInput orderInput = tryUntilInputIsValid(this::getOrderInput); + return eventPlannerService.createOrderSheet(visitDate, orderInput); + } + + private VisitDate getVisitDate() { + return new VisitDate(InputView.readVisitDate()); + } + + private OrderInput getOrderInput() { + return new OrderInput(InputView.readOrders()); + } + + private void showOrders(OrderSheet orderSheet) { + OutputView.printOrders(orderSheet.getOrders()); + OutputView.printTotalPayment(orderSheet.getTotalPayment()); + } + + private void showEventBenefits(OrderSheet orderSheet) { + PresentEventResult presentEventResult = eventPlannerService.getSpecificEventResult(orderSheet, PresentEvent.class); + OutputView.printPresentEventResult(presentEventResult); + + Set<EventResult> results = eventPlannerService.getEventResults(orderSheet); + OutputView.printEventResults(results); + + int totalBenefits = eventPlannerService.getTotalBenefits(results); + OutputView.printEventBenefits(totalBenefits); + + int expectedPayment = eventPlannerService.getExpectedPayment(orderSheet); + OutputView.printExpectedPayment(expectedPayment); + + showBadge(totalBenefits); + } + + private void showBadge(int totalBenefits) { + OutputView.printBadgeResult(eventPlannerService.getBadge(totalBenefits)); + } + + private <T> T tryUntilInputIsValid(Supplier<T> function) { + try { + return function.get(); + } catch (CommonIllegalArgumentException e) { + System.out.println(e.getMessage()); + return tryUntilInputIsValid(function); + } + } +}
Java
๋ฐ˜๋ณต ๊ตฌํ˜„ํ•˜์‹ ๊ฑฐ ๋ฉ‹์ง‘๋‹ˆ๋‹ค!
@@ -0,0 +1,29 @@ +package christmas.domain.badge; + +import java.util.Arrays; +import java.util.function.Predicate; + +public enum Badge { + STAR("๋ณ„", benefits -> (5_000 <= benefits && benefits < 10_000)), + TREE("ํŠธ๋ฆฌ", benefits -> (10_000 <= benefits && benefits < 20_000)), + SANTA("์‚ฐํƒ€", benefits -> (20_000 <= benefits)); + + private String name; + private Predicate<Integer> condition; + + Badge(String name, Predicate<Integer> condition) { + this.name = name; + this.condition = condition; + } + + public static Badge of(int benefits) { + return Arrays.stream(Badge.values()) + .filter(badge -> badge.condition.test(benefits)) + .findFirst() + .orElse(null); + } + + public String getName() { + return name; + } +}
Java
- ๊ฐœ์ธ์ ์œผ๋กœ๋Š” NOTHING์ด๋ผ๋Š” enum ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. (์ด๊ฑฐ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ null์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค๋Š” ์ ์„ ๊ฐ„๊ณผํ•  ์ˆ˜ ์žˆ์–ด์š”) - ํ˜น์€ [Optional](https://mangkyu.tistory.com/70)์„ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค.
@@ -0,0 +1,38 @@ +package christmas.domain.event; + +import java.time.LocalDate; + +import christmas.domain.order.OrderSheet; +import christmas.dto.result.ChristmasDDayEventResult; +import christmas.dto.result.EventResult; + +public class ChristmasDDayEvent extends Event implements Discountable{ + private static final int baseDiscount = 1_000; + private static final int increment = 100; + + public ChristmasDDayEvent(EventType eventType, LocalDate startDate, LocalDate endDate) { + super(eventType, startDate, endDate); + } + + @Override + public boolean isNotSatisfiedBy(OrderSheet orderSheet) { + return isDayOfWeekNotInDuration(orderSheet.getVisitDate()); + } + + @Override + public EventResult getEventBenefits(OrderSheet orderSheet) { + if (isNotSatisfiedBy(orderSheet)) { + return null; + } + + return new ChristmasDDayEventResult(eventType.getName(), calculateDiscount(orderSheet)); + } + + private int calculateDiscount(OrderSheet orderSheet) { + return (baseDiscount + (getDuration(orderSheet) * increment)); + } + + private int getDuration(OrderSheet orderSheet){ + return orderSheet.getVisitDate().getDayOfMonth() - startDate.getDayOfMonth(); + } +}
Java
์œ„์—์„œ๋„ ์–ธ๊ธ‰ํ–ˆ์ง€๋งŒ null์„ ๋ฐ˜ํ™˜ํ•˜์‹ค ๊ฒƒ์ด๋ผ๋ฉด Optional์„ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค.
@@ -0,0 +1,41 @@ +package christmas.domain.event; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +import christmas.domain.menu.MenuType; +import christmas.domain.order.OrderSheet; +import christmas.dto.result.EventResult; +import christmas.dto.result.WeekendEventResult; + +public class WeekdayEvent extends Event implements Discountable{ + private static final MenuType applicableMenuType = MenuType.DESSERT; + private static final int discount = 2_023; + + public WeekdayEvent(EventType eventType, LocalDate startDate, LocalDate endDate) { + super(eventType, startDate, endDate); + } + + @Override + protected boolean isNotSatisfiedBy(OrderSheet orderSheet) { + return isDayOfWeekNotInDuration(orderSheet.getVisitDate()) || isNotWeekDay(orderSheet.getVisitDate()); + } + + @Override + public EventResult getEventBenefits(OrderSheet orderSheet) { + if (isNotSatisfiedBy(orderSheet)) { + return null; + } + + int benefitSum = orderSheet.getOrders().entrySet().stream() + .filter(order -> order.getKey().getType().equals(applicableMenuType)) + .mapToInt(order -> order.getValue() * discount) + .sum(); + + return new WeekendEventResult(eventType.getName(), benefitSum); + } + + private boolean isNotWeekDay(LocalDate localDate) { + return localDate.getDayOfWeek().equals(DayOfWeek.SATURDAY) || localDate.getDayOfWeek().equals(DayOfWeek.FRIDAY); + } +}
Java
order์—์„œ get~().get~() ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ธฐ์ฐจ ์ถฉ๋Œ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์ฐจ ์ถฉ๋Œ์€ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋งŽ์ด ์ง€์–‘ํ•ฉ๋‹ˆ๋‹ค. order์—์„œ public method๋กœ `boolean isType(Type type)` ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. - ์ฐธ๊ณ  ์ž๋ฃŒ - ํ”„๋ฆฌ์ฝ”์Šค 3์ฃผ์ฐจ ๊ณตํ†ต ํ”ผ๋“œ๋ฐฑ "๊ฐ์ฒด๋Š” ๊ฐ์ฒด์Šค๋Ÿฝ๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค" - https://hyesun03.github.io/2019/04/01/method-chain-vs-train-wrek/
@@ -0,0 +1,41 @@ +package christmas.domain.event; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +import christmas.domain.menu.MenuType; +import christmas.domain.order.OrderSheet; +import christmas.dto.result.EventResult; +import christmas.dto.result.WeekendEventResult; + +public class WeekdayEvent extends Event implements Discountable{ + private static final MenuType applicableMenuType = MenuType.DESSERT; + private static final int discount = 2_023; + + public WeekdayEvent(EventType eventType, LocalDate startDate, LocalDate endDate) { + super(eventType, startDate, endDate); + } + + @Override + protected boolean isNotSatisfiedBy(OrderSheet orderSheet) { + return isDayOfWeekNotInDuration(orderSheet.getVisitDate()) || isNotWeekDay(orderSheet.getVisitDate()); + } + + @Override + public EventResult getEventBenefits(OrderSheet orderSheet) { + if (isNotSatisfiedBy(orderSheet)) { + return null; + } + + int benefitSum = orderSheet.getOrders().entrySet().stream() + .filter(order -> order.getKey().getType().equals(applicableMenuType)) + .mapToInt(order -> order.getValue() * discount) + .sum(); + + return new WeekendEventResult(eventType.getName(), benefitSum); + } + + private boolean isNotWeekDay(LocalDate localDate) { + return localDate.getDayOfWeek().equals(DayOfWeek.SATURDAY) || localDate.getDayOfWeek().equals(DayOfWeek.FRIDAY); + } +}
Java
๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ `isWeekend()`๋กœ ํ•˜์‹œ๋ฉด ๋” ์ฝ๊ธฐ ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค. > notFound, nonDone, notSuccessful ๊ณผ ๊ฐ™์€ ์ด๋ฆ„์€ ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋ถ€์ •์ด ๋์„ ๋•Œ ์ฝ๊ธฐ ์–ด๋ ต๋‹ค. ์ฐธ๊ณ  ์ž๋ฃŒ : https://tecoble.techcourse.co.kr/post/2020-04-24-variable_naming/
@@ -0,0 +1,28 @@ +package christmas.dto.order; + +import java.time.DateTimeException; +import java.time.LocalDate; +import java.time.YearMonth; + +import christmas.exception.IllegalDateException; + +public class VisitDate { + private final YearMonth visitYearMonth = YearMonth.of(2023, 12); + private final LocalDate visitDate; + + public VisitDate(int input) { + this.visitDate = convertBy(input); + } + + public LocalDate getVisitDate() { + return this.visitDate; + } + + private LocalDate convertBy(int input) { + try { + return visitYearMonth.atDay(input); + } catch (DateTimeException e) { + throw new IllegalDateException(); + } + } +}
Java
์˜ค! ์ด๋Ÿฐ๊ฒŒ ์žˆ๋Š” ์ค„ ๋ชฐ๋ž๋„ค์š”! ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค!
@@ -1,7 +1,11 @@ package christmas; +import christmas.controller.EventPlannerController; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + AppConfig appConfig = new AppConfig(); + EventPlannerController controller = new EventPlannerController(appConfig.createEventPlanner()); + controller.run(); } }
Java
ํ•ด๋‹น ํŒŒ์ผ์—์„œ๋งŒ ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ 4์นธ์ด๊ณ  ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ๋Š” 8์นธ์ด๋„ค์š”. - ์ฐธ๊ณ  ์ž๋ฃŒ : [Java ์ฝ”๋“œ ์ปจ๋ฒค์…˜](https://github.com/woowacourse/woowacourse-docs/tree/master/styleguide/java) > 4.2 ๋ธ”๋Ÿญ ๋“ค์—ฌ์“ฐ๊ธฐ: +4 ์ŠคํŽ˜์ด์Šค ์ƒˆ ๋ธ”๋ก ๋˜๋Š” ๋ธ”๋ก๊ณผ ์œ ์‚ฌํ•œ ๊ตฌ์กฐ(block-like construct)๊ฐ€ ์—ด๋ฆด ๋•Œ๋งˆ๋‹ค ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ๋„ค ์นธ์”ฉ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก์ด ๋๋‚˜๋ฉด ๋“ค์—ฌ์“ฐ๊ธฐ๋Š” ์ด์ „ ๋“ค์—ฌ์“ฐ๊ธฐ ๋‹จ๊ณ„๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค. ๋“ค์—ฌ์“ฐ๊ธฐ ๋‹จ๊ณ„๋Š” ๋ธ”๋ก ์ „์ฒด์˜ ์ฝ”๋“œ์™€ ์ฃผ์„ ๋ชจ๋‘์— ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,29 @@ +package christmas.domain.badge; + +import java.util.Arrays; +import java.util.function.Predicate; + +public enum Badge { + STAR("๋ณ„", benefits -> (5_000 <= benefits && benefits < 10_000)), + TREE("ํŠธ๋ฆฌ", benefits -> (10_000 <= benefits && benefits < 20_000)), + SANTA("์‚ฐํƒ€", benefits -> (20_000 <= benefits)); + + private String name; + private Predicate<Integer> condition; + + Badge(String name, Predicate<Integer> condition) { + this.name = name; + this.condition = condition; + } + + public static Badge of(int benefits) { + return Arrays.stream(Badge.values()) + .filter(badge -> badge.condition.test(benefits)) + .findFirst() + .orElse(null); + } + + public String getName() { + return name; + } +}
Java
null ์ฒ˜๋ฆฌ๊ฐ€ ๊ณ ๋ฏผ์ด์—ˆ๋Š”๋ฐ NOTHING์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•๊ฐ™์•„์š” ๐Ÿ‘
@@ -0,0 +1,41 @@ +package christmas.domain.event; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +import christmas.domain.menu.MenuType; +import christmas.domain.order.OrderSheet; +import christmas.dto.result.EventResult; +import christmas.dto.result.WeekendEventResult; + +public class WeekdayEvent extends Event implements Discountable{ + private static final MenuType applicableMenuType = MenuType.DESSERT; + private static final int discount = 2_023; + + public WeekdayEvent(EventType eventType, LocalDate startDate, LocalDate endDate) { + super(eventType, startDate, endDate); + } + + @Override + protected boolean isNotSatisfiedBy(OrderSheet orderSheet) { + return isDayOfWeekNotInDuration(orderSheet.getVisitDate()) || isNotWeekDay(orderSheet.getVisitDate()); + } + + @Override + public EventResult getEventBenefits(OrderSheet orderSheet) { + if (isNotSatisfiedBy(orderSheet)) { + return null; + } + + int benefitSum = orderSheet.getOrders().entrySet().stream() + .filter(order -> order.getKey().getType().equals(applicableMenuType)) + .mapToInt(order -> order.getValue() * discount) + .sum(); + + return new WeekendEventResult(eventType.getName(), benefitSum); + } + + private boolean isNotWeekDay(LocalDate localDate) { + return localDate.getDayOfWeek().equals(DayOfWeek.SATURDAY) || localDate.getDayOfWeek().equals(DayOfWeek.FRIDAY); + } +}
Java
์˜ค,, ์ด๋ถ€๋ถ„๋„ ๊ตฌํ˜„ํ•˜๋ฉด์„œ ๋งŒ์กฑ์Šค๋Ÿฝ์ง€ ์•Š์•„์„œ ๊ณ ๋ฏผ์ด ๋งŽ์•˜๋Š”๋ฐ ์กฐ์–ธํ•ด์ฃผ์‹  ๋ฐฉ๋ฒ•๋Œ€๋กœ Order ๋‚ด๋ถ€์—์„œ ํ™•์ธํ•˜๋Š”๊ฒŒ ๋” ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™๋„ค์š”.
@@ -1,7 +1,11 @@ package christmas; +import christmas.controller.EventPlannerController; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + AppConfig appConfig = new AppConfig(); + EventPlannerController controller = new EventPlannerController(appConfig.createEventPlanner()); + controller.run(); } }
Java
๋ถ„๋ช… 4์นธ์œผ๋กœ ์„ค์ •ํ–ˆ๋Š”๋ฐ ์™œ ์ด๋ ‡๊ฒŒ ๋œ์ง€ ๋ชจ๋ฅด๊ฒ ๋„ค์š” ๐Ÿ˜ญ
@@ -0,0 +1,29 @@ +package christmas.domain.badge; + +import java.util.Arrays; +import java.util.function.Predicate; + +public enum Badge { + STAR("๋ณ„", benefits -> (5_000 <= benefits && benefits < 10_000)), + TREE("ํŠธ๋ฆฌ", benefits -> (10_000 <= benefits && benefits < 20_000)), + SANTA("์‚ฐํƒ€", benefits -> (20_000 <= benefits)); + + private String name; + private Predicate<Integer> condition; + + Badge(String name, Predicate<Integer> condition) { + this.name = name; + this.condition = condition; + } + + public static Badge of(int benefits) { + return Arrays.stream(Badge.values()) + .filter(badge -> badge.condition.test(benefits)) + .findFirst() + .orElse(null); + } + + public String getName() { + return name; + } +}
Java
Predicate๋กœ ๊ตฌํ˜„ํ•˜๋‹ˆ ํ›จ์”ฌ ๊น”๋”ํ•˜๋„ค์š”!
@@ -0,0 +1,26 @@ +package christmas.domain.event; + +import java.time.LocalDate; + +import christmas.domain.order.OrderSheet; +import christmas.dto.result.EventResult; + +public abstract class Event { + EventType eventType; + LocalDate startDate; + LocalDate endDate; + + public Event(EventType eventType, LocalDate startDate, LocalDate endDate) { + this.eventType = eventType; + this.startDate = startDate; + this.endDate = endDate; + } + + protected boolean isDayOfWeekNotInDuration(LocalDate localDate) { + return localDate.isBefore(startDate) || localDate.isAfter(endDate); + } + + protected abstract boolean isNotSatisfiedBy(OrderSheet orderSheet); + + public abstract EventResult getEventBenefits(OrderSheet orderSheet); +}
Java
์ถ”์ƒ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค ํ™œ์šฉ์ด ๋ฉ‹์ง€๋„ค์š”! ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค
@@ -0,0 +1,9 @@ +package christmas.exception; + +public class CommonIllegalArgumentException extends IllegalArgumentException { + public static final String EXCEPTION_PREFIX = "[ERROR] %s"; + + public CommonIllegalArgumentException(String message) { + super(String.format(EXCEPTION_PREFIX, message)); + } +}
Java
EXCEPTION_PREFIX, INVALID_ORDER_MESSAGE ๋“ฑ์˜ ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€๋Š” ์—ฌ๋Ÿฌ ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ๋˜๋Š”๋ฐ Enum์œผ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์œผ์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,9 @@ +package christmas.exception; + +public class CommonIllegalArgumentException extends IllegalArgumentException { + public static final String EXCEPTION_PREFIX = "[ERROR] %s"; + + public CommonIllegalArgumentException(String message) { + super(String.format(EXCEPTION_PREFIX, message)); + } +}
Java
์ƒ์ˆ˜๋“ค์„ ๋ฌด์กฐ๊ฑด ENUM ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ๋ณด๋‹ค๋Š” ํ•„์š”ํ•œ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์œ„์น˜์‹œํ‚ค๋Š”๊ฒŒ ๋งž๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ENUM์œผ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ €๋„ ์ด ๋ถ€๋ถ„์„ ๋‹ค์‹œ ๋ณด๋ฉด์„œ ๊ณ ๋ฏผ์ด ๋งŽ์•˜๋Š”๋ฐ, ๊ณตํ†ต๋œ ์ถœ๋ ฅ ํ˜•์‹์ด ์žˆ๋‹ค๋ฉด ENUM์œผ๋กœ ๊ด€๋ฆฌํ–ˆ์–ด๋„ ๊ดœ์ฐฎ์•˜์„ ๊ฒƒ ๊ฐ™๋„ค์š” !
@@ -0,0 +1,57 @@ +package christmas.domain.menu; + +import java.util.Arrays; + +import christmas.exception.MenuNotFoundException; + +public enum Menu { + MUSHROOM_SOUP(MenuType.APPETIZER, "์–‘์†ก์ด์ˆ˜ํ”„", 6_000), + TAPAS(MenuType.APPETIZER, "ํƒ€ํŒŒ์Šค", 5_500), + CAESAR_SALAD(MenuType.APPETIZER, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000), + T_BONE_STEAK(MenuType.MAIN, "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000), + BARBECUE_RIB(MenuType.MAIN, "๋ฐ”๋น„ํ๋ฆฝ", 54_000), + SEAFOOD_PASTA(MenuType.MAIN, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000), + CHRISTMAS_PASTA(MenuType.MAIN, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000), + CHOCOLATE_CAKE(MenuType.DESSERT, "์ดˆ์ฝ”์ผ€์ดํฌ", 15_000), + ICECREAM(MenuType.DESSERT, "์•„์ด์Šคํฌ๋ฆผ", 5_000), + ZERO_COKE(MenuType.DRINK, "์ œ๋กœ์ฝœ๋ผ", 3_000), + RED_WINE(MenuType.DRINK, "๋ ˆ๋“œ์™€์ธ", 60_000), + CHAMPAGNE(MenuType.DRINK, "์ƒดํŽ˜์ธ", 25_000); + private MenuType type; + private String name; + private int price; + + Menu(MenuType type, String name, int price) { + this.type = type; + this.name = name; + this.price = price; + } + + public static Menu from(String input) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.name.equals(input)) + .findFirst() + .orElseThrow(MenuNotFoundException::new); + } + + public MenuType getType() { + return this.type; + } + + public String getMenuName() { + return this.name; + } + + public int getPrice() { + return this.price; + } + + public int getPayment(int quantity) { + return (this.price * quantity); + } + + public static boolean isMatchTo(String input, MenuType type) { + return Arrays.stream(Menu.values()) + .anyMatch(menu -> menu.name.equals(input) && menu.type.equals(type)); + } +}
Java
enum ์ƒ์ˆ˜๋“ค์ด ์„ธ๋ถ€ ์นดํ…Œ๊ณ ๋ฆฌํ™” ๋  ์ˆ˜ ์žˆ์„ ๋•Œ๋Š” ๊ฐœํ–‰์„ ํ†ตํ•ด ๊ตฌ๋ถ„์‹œ์ผœ ์ฃผ๋ฉด ๊ฐ€๋…์„ฑ์ด ํ›จ์”ฌ ์ข‹์•„์ง‘๋‹ˆ๋‹ค! ```suggestion CAESAR_SALAD(MenuType.APPETIZER, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000), T_BONE_STEAK(MenuType.MAIN, "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000), ```
@@ -0,0 +1,57 @@ +package christmas.domain.menu; + +import java.util.Arrays; + +import christmas.exception.MenuNotFoundException; + +public enum Menu { + MUSHROOM_SOUP(MenuType.APPETIZER, "์–‘์†ก์ด์ˆ˜ํ”„", 6_000), + TAPAS(MenuType.APPETIZER, "ํƒ€ํŒŒ์Šค", 5_500), + CAESAR_SALAD(MenuType.APPETIZER, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000), + T_BONE_STEAK(MenuType.MAIN, "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000), + BARBECUE_RIB(MenuType.MAIN, "๋ฐ”๋น„ํ๋ฆฝ", 54_000), + SEAFOOD_PASTA(MenuType.MAIN, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000), + CHRISTMAS_PASTA(MenuType.MAIN, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000), + CHOCOLATE_CAKE(MenuType.DESSERT, "์ดˆ์ฝ”์ผ€์ดํฌ", 15_000), + ICECREAM(MenuType.DESSERT, "์•„์ด์Šคํฌ๋ฆผ", 5_000), + ZERO_COKE(MenuType.DRINK, "์ œ๋กœ์ฝœ๋ผ", 3_000), + RED_WINE(MenuType.DRINK, "๋ ˆ๋“œ์™€์ธ", 60_000), + CHAMPAGNE(MenuType.DRINK, "์ƒดํŽ˜์ธ", 25_000); + private MenuType type; + private String name; + private int price; + + Menu(MenuType type, String name, int price) { + this.type = type; + this.name = name; + this.price = price; + } + + public static Menu from(String input) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.name.equals(input)) + .findFirst() + .orElseThrow(MenuNotFoundException::new); + } + + public MenuType getType() { + return this.type; + } + + public String getMenuName() { + return this.name; + } + + public int getPrice() { + return this.price; + } + + public int getPayment(int quantity) { + return (this.price * quantity); + } + + public static boolean isMatchTo(String input, MenuType type) { + return Arrays.stream(Menu.values()) + .anyMatch(menu -> menu.name.equals(input) && menu.type.equals(type)); + } +}
Java
์ „๋‹ฌ ๋ฐ›๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฉ”๋‰ด ์ด๋ฆ„์ด๋ผ๋Š” ๊ฒƒ ์ •๋„๋Š” ์ตœ์†Œํ•œ์œผ๋กœ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด ์ค€๋‹ค๋ฉด ํ•ด๋‹น ๋ฉ”์„œ๋“œ์˜ ์ •์˜๋ถ€๋งŒ ๋ณด๊ณ ๋„ ๋ฉ”์„œ๋“œ์˜ ํ–‰๋™์„ ์ถ”๋ก ํ•˜๊ธฐ ๋” ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค! ```suggestion public static Menu from(String name) { ```
@@ -0,0 +1,57 @@ +package christmas.domain.menu; + +import java.util.Arrays; + +import christmas.exception.MenuNotFoundException; + +public enum Menu { + MUSHROOM_SOUP(MenuType.APPETIZER, "์–‘์†ก์ด์ˆ˜ํ”„", 6_000), + TAPAS(MenuType.APPETIZER, "ํƒ€ํŒŒ์Šค", 5_500), + CAESAR_SALAD(MenuType.APPETIZER, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000), + T_BONE_STEAK(MenuType.MAIN, "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000), + BARBECUE_RIB(MenuType.MAIN, "๋ฐ”๋น„ํ๋ฆฝ", 54_000), + SEAFOOD_PASTA(MenuType.MAIN, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000), + CHRISTMAS_PASTA(MenuType.MAIN, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000), + CHOCOLATE_CAKE(MenuType.DESSERT, "์ดˆ์ฝ”์ผ€์ดํฌ", 15_000), + ICECREAM(MenuType.DESSERT, "์•„์ด์Šคํฌ๋ฆผ", 5_000), + ZERO_COKE(MenuType.DRINK, "์ œ๋กœ์ฝœ๋ผ", 3_000), + RED_WINE(MenuType.DRINK, "๋ ˆ๋“œ์™€์ธ", 60_000), + CHAMPAGNE(MenuType.DRINK, "์ƒดํŽ˜์ธ", 25_000); + private MenuType type; + private String name; + private int price; + + Menu(MenuType type, String name, int price) { + this.type = type; + this.name = name; + this.price = price; + } + + public static Menu from(String input) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.name.equals(input)) + .findFirst() + .orElseThrow(MenuNotFoundException::new); + } + + public MenuType getType() { + return this.type; + } + + public String getMenuName() { + return this.name; + } + + public int getPrice() { + return this.price; + } + + public int getPayment(int quantity) { + return (this.price * quantity); + } + + public static boolean isMatchTo(String input, MenuType type) { + return Arrays.stream(Menu.values()) + .anyMatch(menu -> menu.name.equals(input) && menu.type.equals(type)); + } +}
Java
๋ฉ”๋‰ด๊ฐ€ ์–ด๋–ค ํƒ€์ž…์ธ์ง€ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์€ ๋ฉ”๋‰ด ํด๋ž˜์Šค์˜ ์ฑ…์ž„์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์š”. getter๋ฅผ ๋งŒ๋“œ๋Š” ๋Œ€์‹  `boolean isTypeOf(MenuType expectedType)` ๊ณผ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”? ํ•ด๋‹น ํด๋ž˜์Šค์˜ ์บก์Аํ™”๊ฐ€ ๋” ์ž˜ ์ œ๊ณต๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,36 @@ +package christmas.domain.event; + +import java.time.LocalDate; + +import christmas.domain.menu.Menu; +import christmas.domain.order.OrderSheet; +import christmas.dto.result.EventResult; +import christmas.dto.result.PresentEventResult; + +public class PresentEvent extends Event{ + private final Menu present = Menu.CHAMPAGNE; + private static final int presentQuantity = 1; + private static final int requiredPayment = 120_000; + + public PresentEvent(EventType eventType, LocalDate startDate, LocalDate endDate) { + super(eventType, startDate, endDate); + } + + @Override + protected boolean isNotSatisfiedBy(OrderSheet orderSheet) { + return isDayOfWeekNotInDuration(orderSheet.getVisitDate()) || isLessThanRequiredPayment(orderSheet.getTotalPayment()); + } + + @Override + public EventResult getEventBenefits(OrderSheet orderSheet) { + if (isNotSatisfiedBy(orderSheet)) { + return null; + } + + return new PresentEventResult(eventType.getName(), present.getPrice(), Menu.CHAMPAGNE, presentQuantity); + } + + private boolean isLessThanRequiredPayment(int totalPayment) { + return totalPayment < requiredPayment; + } +}
Java
ํ•œ ์ค„์—์„œ ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด์ง„๋‹ค๋ฉด ์ ์ ˆํžˆ ๊ฐœํ–‰์„ ํ•ด ์ฃผ์–ด ๊ฐ€๋…์„ฑ์„ ๋†’์—ฌ์ค„ ์ˆ˜ ์žˆ๋Š”๋ฐ์š” ๋ณดํ†ต ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž๋กœ ์ฝ”๋“œ๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๋ฉด ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž ์•ž์—์„œ ๊ฐœํ–‰์„ ์‹œ์ผœ์ฃผ๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค! [๋„ค์ด๋ฒ„ ์บ ํผ์Šค ํ•ต๋ฐ์ด ์ปจ๋ฒค์…˜](https://naver.github.io/hackday-conventions-java/) ```suggestion return isDayOfWeekNotInDuration(orderSheet.getVisitDate()) || isLessThanRequiredPayment(orderSheet.getTotalPayment()); ```
@@ -0,0 +1,28 @@ +package christmas.dto.order; + +import java.time.DateTimeException; +import java.time.LocalDate; +import java.time.YearMonth; + +import christmas.exception.IllegalDateException; + +public class VisitDate { + private final YearMonth visitYearMonth = YearMonth.of(2023, 12); + private final LocalDate visitDate; + + public VisitDate(int input) { + this.visitDate = convertBy(input); + } + + public LocalDate getVisitDate() { + return this.visitDate; + } + + private LocalDate convertBy(int input) { + try { + return visitYearMonth.atDay(input); + } catch (DateTimeException e) { + throw new IllegalDateException(); + } + } +}
Java
ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ ๋‚ ์งœ์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋งŒํผ ์ด ํด๋ž˜์Šค๋Š” DTO์˜ ์—ญํ• ๋งŒ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋ณด๋‹จ, ํ•ด๋‹น ๋‚ ์งœ๊ฐ€ ์ฃผ๋ง์ธ์ง€ ๋“ฑ์˜ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์ด ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ์˜ ์‘์ง‘๋„๊ฐ€ ๋†’์•„์งˆ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,36 @@ +package christmas.domain.event; + +import java.time.LocalDate; + +import christmas.domain.menu.Menu; +import christmas.domain.order.OrderSheet; +import christmas.dto.result.EventResult; +import christmas.dto.result.PresentEventResult; + +public class PresentEvent extends Event{ + private final Menu present = Menu.CHAMPAGNE; + private static final int presentQuantity = 1; + private static final int requiredPayment = 120_000; + + public PresentEvent(EventType eventType, LocalDate startDate, LocalDate endDate) { + super(eventType, startDate, endDate); + } + + @Override + protected boolean isNotSatisfiedBy(OrderSheet orderSheet) { + return isDayOfWeekNotInDuration(orderSheet.getVisitDate()) || isLessThanRequiredPayment(orderSheet.getTotalPayment()); + } + + @Override + public EventResult getEventBenefits(OrderSheet orderSheet) { + if (isNotSatisfiedBy(orderSheet)) { + return null; + } + + return new PresentEventResult(eventType.getName(), present.getPrice(), Menu.CHAMPAGNE, presentQuantity); + } + + private boolean isLessThanRequiredPayment(int totalPayment) { + return totalPayment < requiredPayment; + } +}
Java
ํ›จ์”ฌ ๊น”๋”ํ•ด์ง„ ๊ฒƒ ๊ฐ™๋„ค์š” :) ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,57 @@ +package christmas.domain.menu; + +import java.util.Arrays; + +import christmas.exception.MenuNotFoundException; + +public enum Menu { + MUSHROOM_SOUP(MenuType.APPETIZER, "์–‘์†ก์ด์ˆ˜ํ”„", 6_000), + TAPAS(MenuType.APPETIZER, "ํƒ€ํŒŒ์Šค", 5_500), + CAESAR_SALAD(MenuType.APPETIZER, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000), + T_BONE_STEAK(MenuType.MAIN, "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000), + BARBECUE_RIB(MenuType.MAIN, "๋ฐ”๋น„ํ๋ฆฝ", 54_000), + SEAFOOD_PASTA(MenuType.MAIN, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000), + CHRISTMAS_PASTA(MenuType.MAIN, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000), + CHOCOLATE_CAKE(MenuType.DESSERT, "์ดˆ์ฝ”์ผ€์ดํฌ", 15_000), + ICECREAM(MenuType.DESSERT, "์•„์ด์Šคํฌ๋ฆผ", 5_000), + ZERO_COKE(MenuType.DRINK, "์ œ๋กœ์ฝœ๋ผ", 3_000), + RED_WINE(MenuType.DRINK, "๋ ˆ๋“œ์™€์ธ", 60_000), + CHAMPAGNE(MenuType.DRINK, "์ƒดํŽ˜์ธ", 25_000); + private MenuType type; + private String name; + private int price; + + Menu(MenuType type, String name, int price) { + this.type = type; + this.name = name; + this.price = price; + } + + public static Menu from(String input) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.name.equals(input)) + .findFirst() + .orElseThrow(MenuNotFoundException::new); + } + + public MenuType getType() { + return this.type; + } + + public String getMenuName() { + return this.name; + } + + public int getPrice() { + return this.price; + } + + public int getPayment(int quantity) { + return (this.price * quantity); + } + + public static boolean isMatchTo(String input, MenuType type) { + return Arrays.stream(Menu.values()) + .anyMatch(menu -> menu.name.equals(input) && menu.type.equals(type)); + } +}
Java
ํ™•์‹คํžˆ ์œ„์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜๋Š”๊ฒŒ ์บก์Аํ™” ์ธก๋ฉด์—์„œ ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š” ๊ผผ๊ผผํ•œ ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜„
@@ -0,0 +1,28 @@ +package christmas.dto.order; + +import java.time.DateTimeException; +import java.time.LocalDate; +import java.time.YearMonth; + +import christmas.exception.IllegalDateException; + +public class VisitDate { + private final YearMonth visitYearMonth = YearMonth.of(2023, 12); + private final LocalDate visitDate; + + public VisitDate(int input) { + this.visitDate = convertBy(input); + } + + public LocalDate getVisitDate() { + return this.visitDate; + } + + private LocalDate convertBy(int input) { + try { + return visitYearMonth.atDay(input); + } catch (DateTimeException e) { + throw new IllegalDateException(); + } + } +}
Java
VisitDate ๋‚ด๋ถ€์—์„œ ์ฃผ๋ง์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์—ˆ๋‹ค๋ฉด Event ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ getter๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋˜์—ˆ๊ฒ ๋„ค์š” ๐Ÿ‘
@@ -0,0 +1,42 @@ +package christmas.view; + +import static christmas.controller.Parser.parseDate; +import static christmas.controller.Parser.parseMenus; + +import camp.nextstep.edu.missionutils.Console; +import christmas.exception.ErrorMessage; +import christmas.exception.PromotionException; +import java.util.List; + +public class InputView { + private static final String DATE_PATTERN = "[\\d]{1,2}"; + private static final String MENUS_PATTERN = "(([๊ฐ€-ํžฃ]+)-([\\d]{1,2}),)*([๊ฐ€-ํžฃ]+)-([\\d]{1,2})"; + + public int readVisitDate() { + System.out.println("12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"); + String date = Console.readLine(); + validateDateInput(date); + return parseDate(date); + } + + private void validateDateInput(String input) { + if (input.matches(DATE_PATTERN)) { + return; + } + throw new PromotionException(ErrorMessage.INVALID_DATE_MESSAGE); + } + + public List<String> readMenus() { + System.out.println("์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)"); + String menus = Console.readLine().trim(); + validateMenuInput(menus); + return parseMenus(menus, ","); + } + + private void validateMenuInput(String menus) { + if (menus.matches(MENUS_PATTERN)) { + return; + } + throw new PromotionException(ErrorMessage.INVALID_ORDER_MESSAGE); + } +}
Java
์ด๊ฑฐ ํ• ๋ ค๋‹ค๊ฐ€ ํฌ๊ธฐํ–ˆ๋Š”๋ฐ, ์ •๋ง ๋Œ€๋‹จํ•˜์‹ญ๋‹ˆ๋‹ค...
@@ -0,0 +1,21 @@ +package christmas.controller; + +import christmas.dto.MenuCount; +import java.util.Arrays; +import java.util.List; + +public class Parser { + + public static int parseDate(String input) { + return Integer.parseInt(input); + } + + public static List<String> parseMenus(String input, String delimiter) { + return Arrays.stream(input.split(delimiter)).toList(); + } + + public static MenuCount parseMenu(String input, String delimiter) { + String[] split = input.split(delimiter); + return new MenuCount(split[0], Integer.parseInt(split[1])); + } +}
Java
์ €๋Š” Parsing ์ž์ฒด๋ฅผ InputView์—์„œ ํ–ˆ๋Š”๋ฐ, ์ด๊ฑธ Parser๋กœ ๋ถ„๋ฆฌํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”?
@@ -0,0 +1,112 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.Date; +import christmas.domain.Menus; +import christmas.domain.Order; +import christmas.domain.event.Event; +import christmas.dto.DiscountAmount; +import christmas.dto.MenuCount; +import christmas.exception.PromotionException; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.List; +import java.util.Map; + +public class PromotionController { + private final InputView inputView = new InputView(); + private final OutputView outputView = new OutputView(); + private Order order; + + public void run() { + this.order = createOrder(); + showPlan(); + showBadge(); + } + + private Order createOrder() { + Date date = askVisitDate(); + while (true) { + try { + Menus menus = askMenus(); + return new Order(date, menus); + } catch (PromotionException e) { + outputView.printErrorMessage(e); + } + } + } + + private Date askVisitDate() { + while (true) { + try { + return new Date(inputView.readVisitDate()); + } catch (PromotionException e) { + outputView.printErrorMessage(e); + } + } + } + + private Menus askMenus() { + while (true) { + try { + List<String> input = inputView.readMenus(); + Menus menus = new Menus(); + input.stream() + .map(menu -> Parser.parseMenu(menu, "-")) + .forEach(menuCount -> menus.add(menuCount.menu(), menuCount.count())); + return menus; + } catch (PromotionException e) { + outputView.printErrorMessage(e); + } + } + } + + private void showPlan() { + outputView.printPlanHeader(order.getDate()); + showOrder(); + showTotalAmount(); + showGiveaway(); + showBenefitDetails(); + showTotalBenefit(); + showFinalAmount(); + } + + private void showTotalAmount() { + outputView.printTotalAmount(order.totalAmount()); + } + + private void showOrder() { + List<MenuCount> menuCounts = order.getOrderMenu().entrySet() + .stream() + .map(MenuCount::apply) + .toList(); + outputView.printOrder(menuCounts); + } + + private void showGiveaway() { + outputView.printGiveaway(order.giveGiveaway()); + } + + private void showBenefitDetails() { + Map<Event, Integer> details = order.benefitDetails(); + + List<DiscountAmount> discountAmounts = details.entrySet().stream() + .map(DiscountAmount::apply) + .toList(); + outputView.printBenefitDetails(discountAmounts); + } + + private void showTotalBenefit() { + int totalBenefit = order.totalBenefit(); + outputView.printTotalBenefit(totalBenefit); + } + + private void showFinalAmount() { + outputView.printFinalAmount(order.finalAmount()); + } + + private void showBadge() { + Badge badge = Badge.fromBenefit(order.totalBenefit()); + outputView.printBadge(badge.displayName()); + } +}
Java
Meun ์ƒ์„ฑ์‹œ์— ์ธ์ž๋กœ ๋ฐ›๋Š” ๊ฒƒ์ด ๋งŒ๋“ค๊ณ  ๋‚˜์„œ addํ•˜๋Š”๊ฒƒ ๋ณด๋‹ค ์ € ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. Menu๊ฐ€ add๋˜๊ธฐ ์ „๊นŒ์ง€๋Š” ๋นˆ ๋ฉ”๋‰ด์ธ ์ƒํƒœ๊ฐ€ ์œ ์ง€๋˜๋‹ˆ๊นŒ์š”. ๊ทธ๋ฆฌ๊ณ  ๋ถˆ๋ณ€ ๊ฐ์ฒด ๋ฌธ์ œ๋„ ์žˆ๊ฒ ๋„ค์š”. - ์ฐธ๊ณ  : [๋ธ”๋กœ๊ทธ ๊ธ€](https://velog.io/@conatuseus/Java-Immutable-Object%EB%B6%88%EB%B3%80%EA%B0%9D%EC%B2%B4#immutable-object%EC%9D%98-%EC%9E%A5%EB%8B%A8%EC%A0%90) - ๊ตณ์ด ๊ฐ€๋ณ€๊ฐ์ฒด๋กœ ๋งŒ๋“ค ์ด์œ ๋Š” ์—†์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,85 @@ +package christmas.domain; + +import christmas.domain.menu.Category; +import christmas.domain.menu.Menu; +import christmas.exception.ErrorMessage; +import christmas.exception.PromotionException; +import java.util.EnumMap; +import java.util.Map; +import java.util.Map.Entry; + +public class Menus { + public static final int MIN_MENU_COUNT = 1; + public static final int MAX_ORDER_COUNT = 20; + private final Map<Menu, Integer> menus = new EnumMap<>(Menu.class); + + public Map<Menu, Integer> getMenus() { + return menus; + } + + public void add(String menu, int count) { + validate(menu, count); + menus.put(Menu.fromDescription(menu), count); + } + + private void validate(String menu, int count) { + validateMenuExists(menu); + validateMenuNotDuplicated(menu); + validateCount(count); + validateTotalCount(count); + } + + public boolean isAllInCategory(Category category) { + return menus.keySet().stream() + .map(Menu::category) + .allMatch(c -> c.equals(category)); + } + + public int countByCategory(Category category) { + return menus.keySet().stream() + .filter(key -> category.equals(key.category())) + .mapToInt(menus::get) + .sum(); + } + + public int totalCount() { + return menus.values().stream() + .mapToInt(Integer::intValue) + .sum(); + } + + public int totalAmount() { + return menus.entrySet().stream() + .mapToInt(this::itemPrice) + .sum(); + } + + private int itemPrice(Entry<Menu, Integer> item) { + return item.getKey().price() * item.getValue(); + } + + private void validateMenuExists(String menu) { + if (Menu.exists(menu)) { + return; + } + throw new PromotionException(ErrorMessage.INVALID_ORDER_MESSAGE); + } + + private void validateCount(int count) { + if (count < MIN_MENU_COUNT) { + throw new PromotionException(ErrorMessage.INVALID_ORDER_MESSAGE); + } + } + + private void validateMenuNotDuplicated(String menu) { + if (menus.containsKey(Menu.fromDescription(menu))) { + throw new PromotionException(ErrorMessage.INVALID_ORDER_MESSAGE); + } + } + + private void validateTotalCount(int count) { + if (totalCount() + count > MAX_ORDER_COUNT) { + throw new PromotionException(ErrorMessage.INVALID_ORDER_MESSAGE); + } + } +}
Java
๊ฒ€์ฆ ๊ณผ์ •์„ ๊ฐ ๋ฉ”์„œ๋“œ๋ณ„๋กœ ๋‚˜๋ˆ„์‹  ๊ฒƒ์ด ์ธ์ƒ๊นŠ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,74 @@ +package christmas.domain; + +import christmas.domain.event.Event; +import christmas.domain.event.Giveaway; +import christmas.domain.menu.Category; +import christmas.domain.menu.Menu; +import christmas.dto.MenuCount; +import christmas.exception.ErrorMessage; +import christmas.exception.PromotionException; +import java.util.AbstractMap.SimpleEntry; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.stream.Collectors; + +public class Order { + private final Menus menus; + private final Date date; + + public Order(Date date, Menus menus) { + validateCategory(menus); + this.date = date; + this.menus = menus; + } + + public int getDate() { + return date.getDate(); + } + + public Map<Menu, Integer> getOrderMenu() { + return menus.getMenus(); + } + + private void validateCategory(Menus menus) { + if (menus.isAllInCategory(Category.BEVERAGE)) { + throw new PromotionException(ErrorMessage.INVALID_ORDER_MESSAGE); + } + } + + public int totalAmount() { + return menus.totalAmount(); + } + + public Optional<MenuCount> giveGiveaway() { + Giveaway giveaway = (Giveaway) Event.GIVEAWAY.policy(); + return giveaway.giveGiveaway(date, menus); + } + + public Map<Event, Integer> benefitDetails() { + return Arrays.stream(Event.values()) + .map(event -> new SimpleEntry<>(event, event.benefitAmount(date, menus))) + .filter(entry -> entry.getValue() < 0) + .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (x, y) -> y, LinkedHashMap::new)); + } + + public int totalBenefit() { + return Arrays.stream(Event.values()) + .mapToInt(event -> event.benefitAmount(date, menus)) + .sum(); + } + + public int totalDiscount() { + return Arrays.stream(Event.values()) + .filter(event -> event != Event.GIVEAWAY) + .mapToInt(event -> event.benefitAmount(date, menus)) + .sum(); + } + + public int finalAmount() { + return totalAmount() + totalDiscount(); + } +}
Java
ํ•ด๋‹น ๊ฒ€์ฆ ๊ณผ์ •์€ Menus์—์„œ ํ•˜๋Š”๊ฒŒ ์–ด๋–จ๊นŒ์š”? Menu ์ž์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ํŽธ์ด ๊ฐœ์ธ์ ์œผ๋กœ ๋” ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,33 @@ +package christmas.domain.event; + +import christmas.domain.Date; +import christmas.domain.Menus; +import christmas.domain.menu.Menu; +import christmas.dto.MenuCount; +import java.util.Optional; + +public class Giveaway implements EventPolicy { + private static final int MIN_TOTAL_AMOUNT = 120_000; //shadowing + private static final Menu giveaway = Menu.CHAMPAGNE; + private static final int numGiveaway = 1; + + @Override + public boolean canBeApplied(Date date, Menus menus) { + return menus.totalAmount() >= MIN_TOTAL_AMOUNT; + } + + @Override + public int amount(Date date, Menus menus) { + if (canBeApplied(date, menus)) { + return -1 * numGiveaway * giveaway.price(); + } + return NONE; + } + + public Optional<MenuCount> giveGiveaway(Date date, Menus menus) { + if (canBeApplied(date, menus)) { + return Optional.of(new MenuCount(giveaway.description(), numGiveaway)); + } + return Optional.empty(); + } +}
Java
`//shadowing` ์ฃผ์„์˜ ์˜๋ฏธ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?
@@ -0,0 +1,42 @@ +package christmas.view; + +import static christmas.controller.Parser.parseDate; +import static christmas.controller.Parser.parseMenus; + +import camp.nextstep.edu.missionutils.Console; +import christmas.exception.ErrorMessage; +import christmas.exception.PromotionException; +import java.util.List; + +public class InputView { + private static final String DATE_PATTERN = "[\\d]{1,2}"; + private static final String MENUS_PATTERN = "(([๊ฐ€-ํžฃ]+)-([\\d]{1,2}),)*([๊ฐ€-ํžฃ]+)-([\\d]{1,2})"; + + public int readVisitDate() { + System.out.println("12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"); + String date = Console.readLine(); + validateDateInput(date); + return parseDate(date); + } + + private void validateDateInput(String input) { + if (input.matches(DATE_PATTERN)) { + return; + } + throw new PromotionException(ErrorMessage.INVALID_DATE_MESSAGE); + } + + public List<String> readMenus() { + System.out.println("์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)"); + String menus = Console.readLine().trim(); + validateMenuInput(menus); + return parseMenus(menus, ","); + } + + private void validateMenuInput(String menus) { + if (menus.matches(MENUS_PATTERN)) { + return; + } + throw new PromotionException(ErrorMessage.INVALID_ORDER_MESSAGE); + } +}
Java
`private static final DATE_REQUEST_MESSAGE = "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"` ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,74 @@ +package christmas.domain; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import christmas.exception.ErrorMessage; +import christmas.exception.PromotionException; +import java.time.DayOfWeek; +import java.util.List; +import java.util.stream.IntStream; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +class DateTest { + + @DisplayName("๋‚ ์งœ ๋„๋ฉ”์ธ ์ƒ์„ฑ ํ…Œ์ŠคํŠธ") + @Test + void create() { + IntStream.range(1, 32) + .forEach(date -> assertDoesNotThrow(() -> new Date(date))); + } + + @DisplayName("๋‚ ์งœ ๋„๋ฉ”์ธ_๋ฒ”์œ„ ๋ฐ–์˜ ๋‚ ์งœ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ") + @ParameterizedTest + @ValueSource(ints = {0, 32}) + void createOutRange(int date) { + assertThatThrownBy(() -> new Date(date)) + .isInstanceOf(PromotionException.class) + .hasMessage(ErrorMessage.INVALID_DATE_MESSAGE.message()); + } + + @DisplayName("๋‚ ์งœ -> ์š”์ผ ํ…Œ์ŠคํŠธ") + @ParameterizedTest + @CsvSource({"1,FRIDAY", "25,MONDAY", "31,SUNDAY"}) + void dayOfWeek(int date, String expected) { + DayOfWeek dayOfWeek = new Date(date).dayOfWeek(); + + assertThat(dayOfWeek.name()).isEqualTo(expected); + } + + @DisplayName("ํŠน์ •๋‚ ๋“ค์— ํ•ด๋‹น ๋‚ ์งœ๊ฐ€ ํฌํ•จ๋˜๋Š”์ง€ ํ…Œ์ŠคํŠธ") + @ParameterizedTest + @CsvSource({"1,true", "25,false"}) + void isIncluded(int dateSource, boolean expected) { + Date date = new Date(dateSource); + List<Integer> dates = List.of(1, 2); + + assertThat(date.isIncluded(dates)).isEqualTo(expected); + } + + @DisplayName("ํŠน์ • ๊ธฐ๊ฐ„์— ํ•ด๋‹น๋‚ ์งœ๊ฐ€ ํฌํ•จ๋˜๋Š”์ง€ ํ…Œ์ŠคํŠธ") + @ParameterizedTest + @CsvSource({"1,true", "26,false"}) + void isInRange(int dateSource, boolean expected) { + Date date = new Date(dateSource); + + assertThat(date.isInRange(1, 25)) + .isEqualTo(expected); + } + + @DisplayName("ํ•ด๋‹น ๋‚ ์งœ๊ฐ€ ํŠน์ •๋‚ ๋กœ๋ถ€ํ„ฐ ๋ช‡๋ฒˆ์งธ ๋‚ ์ธ์ง€ ๊ตฌํ•˜๋Š” ๊ธฐ๋Šฅ ํ…Œ์ŠคํŠธ") + @ParameterizedTest + @CsvSource({"1,1,0", "31,25,6"}) + void isInRange(int dateSource, int from, int expectedDay) { + Date date = new Date(dateSource); + + assertThat(date.dayFromDate(from)) + .isEqualTo(expectedDay); + } +}
Java
์ „๋ฐ˜์ ์œผ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์ฐธ ๊น”๋”ํ•˜๊ฒŒ ์ž‘์„ฑํ•˜์…จ๋„ค์š”! ์ž˜ ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค!
@@ -0,0 +1,112 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.Date; +import christmas.domain.Menus; +import christmas.domain.Order; +import christmas.domain.event.Event; +import christmas.dto.DiscountAmount; +import christmas.dto.MenuCount; +import christmas.exception.PromotionException; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.List; +import java.util.Map; + +public class PromotionController { + private final InputView inputView = new InputView(); + private final OutputView outputView = new OutputView(); + private Order order; + + public void run() { + this.order = createOrder(); + showPlan(); + showBadge(); + } + + private Order createOrder() { + Date date = askVisitDate(); + while (true) { + try { + Menus menus = askMenus(); + return new Order(date, menus); + } catch (PromotionException e) { + outputView.printErrorMessage(e); + } + } + } + + private Date askVisitDate() { + while (true) { + try { + return new Date(inputView.readVisitDate()); + } catch (PromotionException e) { + outputView.printErrorMessage(e); + } + } + } + + private Menus askMenus() { + while (true) { + try { + List<String> input = inputView.readMenus(); + Menus menus = new Menus(); + input.stream() + .map(menu -> Parser.parseMenu(menu, "-")) + .forEach(menuCount -> menus.add(menuCount.menu(), menuCount.count())); + return menus; + } catch (PromotionException e) { + outputView.printErrorMessage(e); + } + } + } + + private void showPlan() { + outputView.printPlanHeader(order.getDate()); + showOrder(); + showTotalAmount(); + showGiveaway(); + showBenefitDetails(); + showTotalBenefit(); + showFinalAmount(); + } + + private void showTotalAmount() { + outputView.printTotalAmount(order.totalAmount()); + } + + private void showOrder() { + List<MenuCount> menuCounts = order.getOrderMenu().entrySet() + .stream() + .map(MenuCount::apply) + .toList(); + outputView.printOrder(menuCounts); + } + + private void showGiveaway() { + outputView.printGiveaway(order.giveGiveaway()); + } + + private void showBenefitDetails() { + Map<Event, Integer> details = order.benefitDetails(); + + List<DiscountAmount> discountAmounts = details.entrySet().stream() + .map(DiscountAmount::apply) + .toList(); + outputView.printBenefitDetails(discountAmounts); + } + + private void showTotalBenefit() { + int totalBenefit = order.totalBenefit(); + outputView.printTotalBenefit(totalBenefit); + } + + private void showFinalAmount() { + outputView.printFinalAmount(order.finalAmount()); + } + + private void showBadge() { + Badge badge = Badge.fromBenefit(order.totalBenefit()); + outputView.printBadge(badge.displayName()); + } +}
Java
๋ฐฉ๋ฌธ ๋‚ ์งœ์™€ ์ฃผ๋ฌธ ๋ฉ”๋‰ด์— ๊ด€ํ•œ ๋„๋ฉ”์ธ์ด ์ปจํŠธ๋กค๋Ÿฌ์— ๋…ธ์ถœ๋˜์–ด์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์„œ๋น„์Šค ๋ ˆ์ด์–ด๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์€ ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ ์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,46 @@ +package christmas.domain; + +import christmas.exception.ErrorMessage; +import christmas.exception.PromotionException; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.List; + +public class Date { + public static final int EVENT_YEAR = 2023; + public static final int EVENT_MONTH = 12; + public static final int MIN_DATE = 1; + public static final int MAX_DATE = 31; + private final int date; + + public Date(int date) { + validate(date); + this.date = date; + } + + public int getDate() { + return date; + } + + private void validate(int date) { + if (date < MIN_DATE || date > MAX_DATE) { + throw new PromotionException(ErrorMessage.INVALID_DATE_MESSAGE); + } + } + + public int dayFromDate(int other) { + return date - other; + } + + public DayOfWeek dayOfWeek() { + return LocalDate.of(EVENT_YEAR, EVENT_MONTH, date).getDayOfWeek(); + } + + public boolean isIncluded(List<Integer> dates) { + return dates.contains(date); + } + + public boolean isInRange(int startInclusive, int endInclusive) { + return startInclusive <= date && date <= endInclusive; + } +}
Java
๋‚ ์งœ๋ฅผ int ํƒ€์ž…์œผ๋กœ ํ•˜์…”์„œ 1์ผ๊ณผ 31์ผ ๋ฒ”์œ„์— ๋Œ€ํ•œ ๊ฒ€์ฆ ๋กœ์ง์„ ์ž‘์„ฑํ•˜์…จ๋„ค์š”. ์•„๋ฌด๋ž˜๋„ ๋ช…๋ฐฑํžˆ ๋‚ ์งœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค๋‹ค๋ณด๋‹ˆ LocalDate๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ ์งœ๋ฅผ ๊ด€๋ฆฌํ•˜๋ฉด ์ƒ์„ฑ์‹œ ์ž˜๋ชป๋œ ๋‚ ์งœ์ธ ๊ฒฝ์šฐ์— DateTimeException์ด ๋ฐœ์ƒํ•˜๊ณ  ์ด ์—๋Ÿฌ๋ฅผ ์žก์•„์„œ ์ฒ˜๋ฆฌํ•˜์—ฌ ๊ฒ€์ฆํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์ ์šฉํ•ด๋ณด์‹œ๋ฉด ์ข€ ๋” ๋ฒ”์šฉ์ ์ธ ๋‚ ์งœ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,32 @@ +package christmas.domain.event; + +import christmas.domain.Date; +import christmas.domain.Menus; + +public enum Event { + D_DAY_DISCOUNT(new DDayDiscount(), "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"), + WEEKDAY_DISCOUNT(new WeekdayDiscount(), "ํ‰์ผ ํ• ์ธ"), + WEEKEND_DISCOUNT(new WeekendDiscount(), "์ฃผ๋ง ํ• ์ธ"), + SPECIAL_DISCOUNT(new SpecialDiscount(), "ํŠน๋ณ„ ํ• ์ธ"), + GIVEAWAY(new Giveaway(), "์ฆ์ • ์ด๋ฒคํŠธ"); + + private final EventPolicy policy; + private final String description; + + Event(EventPolicy policy, String description) { + this.policy = policy; + this.description = description; + } + + public String description() { + return description; + } + + public EventPolicy policy() { + return policy; + } + + public int benefitAmount(Date date, Menus menus) { + return policy.amount(date, menus); + } +}
Java
์ด๋ฒคํŠธ๋ฅผ enum์— ์ƒ์„ฑํ•ด์„œ ๊ด€๋ฆฌํ•˜์…จ๋„ค์š” ์ €๋Š” ์ด๋Ÿฐ ์ƒ๊ฐ์„ ๋ชปํ–ˆ๋Š”๋ฐ ์ƒˆ๋กœ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋„ค์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,59 @@ +package christmas.domain.menu; + +import static christmas.domain.menu.Category.APPETIZER; +import static christmas.domain.menu.Category.BEVERAGE; +import static christmas.domain.menu.Category.DESSERT; +import static christmas.domain.menu.Category.MAIN; + +import java.util.Arrays; + +public enum Menu { + MUSHROOM_SOUP(APPETIZER, "์–‘์†ก์ด์ˆ˜ํ”„", 6_000), + TAPAS(APPETIZER, "ํƒ€ํŒŒ์Šค", 5_500), + CAESAR_SALAD(APPETIZER, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000), + + T_BONE_STEAK(MAIN, "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000), + BARBECUE_RIBS(MAIN, "๋ฐ”๋น„ํ๋ฆฝ", 54_000), + SEAFOOD_PASTA(MAIN, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000), + CHRISTMAS_PASTA(MAIN, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000), + + CHOCOLATE_CAKE(DESSERT, "์ดˆ์ฝ”์ผ€์ดํฌ", 15_000), + ICE_CREAM(DESSERT, "์•„์ด์Šคํฌ๋ฆผ", 5_000), + + ZERO_COLA(BEVERAGE, "์ œ๋กœ์ฝœ๋ผ", 3_000), + RED_WINE(BEVERAGE, "๋ ˆ๋“œ์™€์ธ", 60_000), + CHAMPAGNE(BEVERAGE, "์ƒดํŽ˜์ธ", 25_000); + + private final Category category; + private final String description; + private final int price; + + Menu(Category category, String description, int price) { + this.category = category; + this.description = description; + this.price = price; + } + + public static boolean exists(String description) { + return Arrays.stream(Menu.values()) + .anyMatch(m -> description.equals(m.description())); + } + + public static Menu fromDescription(String description) { + return Arrays.stream(Menu.values()) + .filter(m -> description.equals(m.description)) + .findAny().get(); + } + + public Category category() { + return category; + } + + public String description() { + return description; + } + + public int price() { + return price; + } +}
Java
๊ฐ ๋ฉ”๋‰ด์˜ ์นดํ…Œ๊ณ ๋ฆฌ๋„ enum์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์–ด๋–จ๊นŒ ํ•˜๋„ค์š”.
@@ -0,0 +1,21 @@ +package christmas.controller; + +import christmas.dto.MenuCount; +import java.util.Arrays; +import java.util.List; + +public class Parser { + + public static int parseDate(String input) { + return Integer.parseInt(input); + } + + public static List<String> parseMenus(String input, String delimiter) { + return Arrays.stream(input.split(delimiter)).toList(); + } + + public static MenuCount parseMenu(String input, String delimiter) { + String[] split = input.split(delimiter); + return new MenuCount(split[0], Integer.parseInt(split[1])); + } +}
Java
InputView์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…์„ ๋ชจ๋‘ String์œผ๋กœ ์ ์ ˆํ•œ ํ˜•๋ณ€ํ™˜์€ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ํ•˜์ž ๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ Parser๋ฅผ ๋ถ„๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค. (์ง€๊ธˆ ๋ณด๋‹ˆ date๋Š” InputView์—์„œ ํŒŒ์‹ฑ์„ ํ•ด๋†”์„œ ์•„์‰ฝ๋„ค์š”)
@@ -0,0 +1,112 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.Date; +import christmas.domain.Menus; +import christmas.domain.Order; +import christmas.domain.event.Event; +import christmas.dto.DiscountAmount; +import christmas.dto.MenuCount; +import christmas.exception.PromotionException; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.List; +import java.util.Map; + +public class PromotionController { + private final InputView inputView = new InputView(); + private final OutputView outputView = new OutputView(); + private Order order; + + public void run() { + this.order = createOrder(); + showPlan(); + showBadge(); + } + + private Order createOrder() { + Date date = askVisitDate(); + while (true) { + try { + Menus menus = askMenus(); + return new Order(date, menus); + } catch (PromotionException e) { + outputView.printErrorMessage(e); + } + } + } + + private Date askVisitDate() { + while (true) { + try { + return new Date(inputView.readVisitDate()); + } catch (PromotionException e) { + outputView.printErrorMessage(e); + } + } + } + + private Menus askMenus() { + while (true) { + try { + List<String> input = inputView.readMenus(); + Menus menus = new Menus(); + input.stream() + .map(menu -> Parser.parseMenu(menu, "-")) + .forEach(menuCount -> menus.add(menuCount.menu(), menuCount.count())); + return menus; + } catch (PromotionException e) { + outputView.printErrorMessage(e); + } + } + } + + private void showPlan() { + outputView.printPlanHeader(order.getDate()); + showOrder(); + showTotalAmount(); + showGiveaway(); + showBenefitDetails(); + showTotalBenefit(); + showFinalAmount(); + } + + private void showTotalAmount() { + outputView.printTotalAmount(order.totalAmount()); + } + + private void showOrder() { + List<MenuCount> menuCounts = order.getOrderMenu().entrySet() + .stream() + .map(MenuCount::apply) + .toList(); + outputView.printOrder(menuCounts); + } + + private void showGiveaway() { + outputView.printGiveaway(order.giveGiveaway()); + } + + private void showBenefitDetails() { + Map<Event, Integer> details = order.benefitDetails(); + + List<DiscountAmount> discountAmounts = details.entrySet().stream() + .map(DiscountAmount::apply) + .toList(); + outputView.printBenefitDetails(discountAmounts); + } + + private void showTotalBenefit() { + int totalBenefit = order.totalBenefit(); + outputView.printTotalBenefit(totalBenefit); + } + + private void showFinalAmount() { + outputView.printFinalAmount(order.finalAmount()); + } + + private void showBadge() { + Badge badge = Badge.fromBenefit(order.totalBenefit()); + outputView.printBadge(badge.displayName()); + } +}
Java
์ €๋ฒˆ ์ฃผ์ฐจ์— ์„œ๋น„์Šค ๋ ˆ์ด์–ด๋Š” ์ตœ๋Œ€ํ•œ ์–‡๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๋Š” ๋ฆฌ๋ทฐ๊ฐ€ ์žˆ์—ˆ๊ณ  ๊ทธ์— ๋™์˜ํ•ด์„œ ์ด๋ฒˆ์ฃผ์ฐจ๋Š” ์„œ๋น„์Šค ๋ ˆ์ด์–ด ์—†์ด ๋„๋ฉ”์ธ์— ์ตœ๋Œ€ํ•œ ๋กœ์ง์„ ๋‹ด์ž๋ฅผ ๋ชฉํ‘œ๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,46 @@ +package christmas.domain; + +import christmas.exception.ErrorMessage; +import christmas.exception.PromotionException; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.List; + +public class Date { + public static final int EVENT_YEAR = 2023; + public static final int EVENT_MONTH = 12; + public static final int MIN_DATE = 1; + public static final int MAX_DATE = 31; + private final int date; + + public Date(int date) { + validate(date); + this.date = date; + } + + public int getDate() { + return date; + } + + private void validate(int date) { + if (date < MIN_DATE || date > MAX_DATE) { + throw new PromotionException(ErrorMessage.INVALID_DATE_MESSAGE); + } + } + + public int dayFromDate(int other) { + return date - other; + } + + public DayOfWeek dayOfWeek() { + return LocalDate.of(EVENT_YEAR, EVENT_MONTH, date).getDayOfWeek(); + } + + public boolean isIncluded(List<Integer> dates) { + return dates.contains(date); + } + + public boolean isInRange(int startInclusive, int endInclusive) { + return startInclusive <= date && date <= endInclusive; + } +}
Java
๋„ต ๋งŒ์•ฝ ๋‹ค๋ฅธ ๋‹ฌ๋„ ์žˆ์—ˆ๋‹ค๋ฉด LocalDate ์ž์ฒด๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ํ•˜์ง€๋งŒ ์ด๋ฒˆ ๋ฏธ์…˜์— ๊ฒฝ์šฐ ์ด๋ฒคํŠธ ๋‹ฌ์ธ 2023๋…„ 12์›”์— ํ•œ์ •๋๊ธฐ ๋•Œ๋ฌธ์— int๋งŒ ๊ฐ์‹ธ์„œ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.