code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,21 @@ +package christmas.constants; + +public enum AmountConstants { + CHRISTMAS_BASE(1_000), + CHRISTMAS_UNIT(100), + DISCOUNT_THRESHOLD(10_000), + HOLIDAY_UNIT(2_023), + STAR_DISCOUNT(1_000), + WEEKDAY_UNIT(2_023), + CHAMPAGNE_PRESENT_BASE(120_000); + + private final int amount; + + AmountConstants(int amount) { + this.amount = amount; + } + + public int getAmount() { + return this.amount; + } +}
Java
์šฐ์„  ์ œ ์ฝ”๋“œ ๋ฆฌ๋ทฐ๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‹ค๋Š” ์‚ฌ์‹ค์— ๊ฐ๊ฒฉ์Šค๋Ÿฝ๋‹ค๋Š” ๋ง์„ ์ „ํ•˜๊ณ  ์‹ถ์–ด์š”! ๊ฐ์‚ฌํ•˜๋‹ค๋Š” ๋ง์„ ๋“ค์„ ๋•Œ๋งˆ๋‹ค ํž˜์ด ๋‚˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›์œผ๋‹ˆ ๋‘๋ฐฐ๋กœ ๊ฐ๊ฒฉ์Šค๋Ÿฌ์›Œ์š”! ์ œ์•ˆ์— ๋™์˜ํ•ฉ๋‹ˆ๋‹ค. ํ™•์‹คํžˆ ๋ชจ๋“  ์ด๋ฒคํŠธ๋Š” ๋งŒ์›์„ ๋„˜์–ด์•ผํ•˜๋‹ˆ DISCOUNT ๋ณด๋‹ค EVENT ๊ฐ€ ๋ฌธ๋งฅ์— ๋งž์„ ๊ฒƒ ๊ฐ™๊ณ , "์ด๋ฒคํŠธ๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•ด ์ด๋งŒํผ ํ•„์š”ํ•˜๋‹ค"๋ผ๋Š” ๋ฌธ๋งฅ์„ ์ž˜ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋„๋ก REQUIREMENT๊ฐ€ ๋”์šฑ ์ž˜ ๋งž๋Š” ๊ฒƒ ๊ฐ™์•„์š”. (์ด๊ฑด ์ œ๊ฐ€ ์˜์–ด๊ณต๋ถ€ ์ข€ ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค)
@@ -0,0 +1,49 @@ +package christmas.domain.event.discount; + +import christmas.constants.AmountConstants; +import christmas.domain.day.Day; +import christmas.domain.order.OrderSheet; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public enum DiscountEventManager { + CHRISTMAS { + @Override + protected DiscountEvent create(Day day, OrderSheet orderSheet) { + return new ChristmasDiscountEvent(day); + } + }, + HOLIDAY { + @Override + protected DiscountEvent create(Day day, OrderSheet orderSheet) { + return new HolidayDiscountEvent(day, orderSheet); + } + }, + WEEKDAY { + @Override + protected DiscountEvent create(Day day, OrderSheet orderSheet) { + return new WeekDayDiscountEvent(day, orderSheet); + } + }, + STAR { + @Override + protected DiscountEvent create(Day day, OrderSheet orderSheet) { + return new StarDiscountEvent(day); + } + }; + + abstract protected DiscountEvent create(Day day, OrderSheet orderSheet); + + public static DiscountResult getDiscountResult(Day day, OrderSheet orderSheet) { + if (orderSheet.isMoreThanTotal(AmountConstants.DISCOUNT_THRESHOLD.getAmount())) { + List<DiscountEvent> discountEvents = Arrays.stream(values()) + .map(event -> event.create(day, orderSheet)) + .filter(DiscountEvent::isDiscountable) + .toList(); + return new DiscountResult(discountEvents); + } + return new DiscountResult(new ArrayList<>()); + } +}
Java
๋„ค ๋งž์Šต๋‹ˆ๋‹ค. ์ด ํฌ์ŠคํŠธ๋ฅผ ๋ณด๊ธฐ ์ „ "๋งŒ์•ฝ ์ด๋ฒคํŠธ๊ฐ€ ๋Š˜์–ด๋‚œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•˜์ง€?"๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฒคํŠธ ๊ฐ์ž๋งˆ๋‹ค ์ด๋ฒคํŠธ ์ ์šฉ๊ฐ€๋Šฅํ•œ์ง€ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค๋ฅธ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ข…๋ฅ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ดํ›„์— ์ด ํฌ์ŠคํŠธ์—์„œ Enum์˜ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋กœ ์ƒ์ˆ˜ ๋ณ„ ๋ฉ”์„œ๋“œ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด๊ณ  ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค๋ฉด ์œ ์—ฐํ•˜๊ฒŒ ์ด๋ฒคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” Enum ๋ฉ”์„œ๋“œ๊ฐ€ ๊ฐ์ฒด ์ƒ์„ฑ์„ ๋‹ด๋‹นํ•˜๋Š” ํŒจํ„ด, Enum Factory Method Pattern ์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด ํŒฉํ† ๋ฆฌ ํŒจํ„ด๊ณผ ๋‹ค๋ฅด๊ฒŒ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ํŒฉํ† ๋ฆฌ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๋กœ ์ด๋ฃจ์–ด์ง€์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ์ด๋ฒคํŠธ ๊ฐ์ฒด๊ฐ€ ์ƒ๊ธธ ๋•Œ๋งˆ๋‹ค ์ด๋ฒคํŠธ ํŒฉํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด์•ผํ–ˆ๋˜ ๊ธฐ์กด ํŒจํ„ด๊ณผ ๋‹ค๋ฅด๊ฒŒ ํด๋ž˜์Šค ์•ˆ์—์„œ ์‰ฝ๊ฒŒ ์ •์˜๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ https://inpa.tistory.com/entry/GOF-%F0%9F%92%A0-Enum-Factory-Method-%EB%B3%80%ED%98%95-%ED%8C%A8%ED%84%B4 ์„ ๋ณด๊ณ  ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,30 @@ +package christmas.domain.event.present; + +import christmas.domain.order.Menu; +import christmas.domain.order.OrderSheet; + +import java.util.HashMap; +import java.util.Map; + +public enum PresentEventManager { + CHAMPAGNE { + @Override + protected PresentEvent create(OrderSheet orderSheet) { + return new ChampagnePresentEvent(orderSheet); + } + }; + + abstract protected PresentEvent create(OrderSheet orderSheet); + + public static PresentResult getPresentResult(OrderSheet orderSheet) { + Map<Menu, Integer> presents = new HashMap<>(); + for (PresentEventManager value : values()) { + PresentEvent event = value.create(orderSheet); + if (event.isPresentable()) { + int presentCount = presents.getOrDefault(event.getPresent(), 1); + presents.put(event.getPresent(), presentCount); + } + } + return new PresentResult(presents); + } +}
Java
๋งž์Šต๋‹ˆ๋‹ค. Present์™€ Discount ์„œ๋กœ ์ด๋ฒคํŠธ ์ ์šฉ์˜ ์„ฑ์งˆ์ด ๋‹ฌ๋ผ ๋”ฐ๋กœ ๊ตฌ๋ถ„ํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ฌ๋Ÿฌ ์œ ํ˜•์˜ ์„ ๋ฌผ์ด ์žˆ์„ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•ด Present์˜ ํ™•์žฅ์„ฑ์„ ๊ณ ๋ คํ•œ ๊ฒƒ๋„ ๋งž์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์˜ค๋žœ ์‹œ๊ฐ„ ๊ณ ๋ คํ–ˆ๋˜ ๋ถ€๋ถ„์„ ์บ์น˜ํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,73 @@ +package christmas.domain.order; + +import christmas.constants.ErrorMessage; +import christmas.domain.Price; +import christmas.dto.MenuDTO; + +import java.util.*; + +public class OrderSheet { + private static final int TOTAL_COUNT_LIMIT = 20; + + private final Map<Menu, Integer> orderSheet; + private int totalPrice; + + public OrderSheet(Map<String, Integer> menus) { + orderSheet = new HashMap<>(); + validateExceedMenuCount(menus); + countMenu(menus); + calculateTotalPrice(); + validateOnlyDrink(); + } + + public int getMenuCountByMenuType(MenuType menuType) { + int menuCount = orderSheet.keySet().stream() + .filter(menu -> menu.compareType(menuType)) + .map(menu -> orderSheet.get(menu)) + .reduce(0, Integer::sum); + return menuCount; + } + + public int calculateTotalPrice() { + this.totalPrice = orderSheet.keySet().stream() + .map(menu -> menu.getPrice() * orderSheet.get(menu)) + .reduce(0, Integer::sum); + return totalPrice; + } + + private void countMenu(Map<String, Integer> menus) { + for (String menuName : menus.keySet()) { + Menu menu = Menu.getMenuByName(menuName); + orderSheet.put(menu, menus.get(menuName)); + } + } + + public Price getTotalPrice() { + return new Price(totalPrice); + } + + public List<MenuDTO> getOrderSheet() { + return orderSheet.keySet().stream() + .map(menu -> new MenuDTO(menu.getName(), orderSheet.get(menu))) + .toList(); + } + + public boolean isMoreThanTotal(int amount) { + return totalPrice >= amount; + } + + private void validateExceedMenuCount(Map<String, Integer> menus) { + int totalMenuCount = menus.values().stream() + .reduce(0, Integer::sum); + if (totalMenuCount <= TOTAL_COUNT_LIMIT) return; + throw new IllegalArgumentException(ErrorMessage.EXCEED_MENU_COUNT.getErrorMessage()); + } + + private void validateOnlyDrink() { + int drinkCount = (int) orderSheet.keySet().stream() + .filter(menu -> menu.compareType(MenuType.DRINK)) + .count(); + if (drinkCount == orderSheet.size()) + throw new IllegalArgumentException(ErrorMessage.ONLY_DRINK.getErrorMessage()); + } +}
Java
๋‹ค๋ฅธ ๋ถ„๋“ค์ด ๋ณผ ๋•Œ๋Š” ์ด ๋ฉ”์„œ๋“œ ์ด๋ฆ„์ด "Menu์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜"๋ผ๊ณ  ๋ณด์ผ ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”. ์ €๋Š” "๋ฉ”๋‰ด ์ด๋ฆ„์œผ๋กœ ๋ฉ”๋‰ด ๊ฐ์ฒด๋ฅผ ์ฐพ๊ณ  ๊ทธ ๊ฐ์ฒด๋ฅผ ์นด์šดํŠธํ•˜๋Š” ํ•จ์ˆ˜"๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฐ”๊พผ๋‹ค๋ฉด `countMenu`์—์„œ `convertToMenu`๋ผ๊ณ  ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”
@@ -0,0 +1,73 @@ +package christmas.domain.order; + +import christmas.constants.ErrorMessage; +import christmas.domain.Price; +import christmas.dto.MenuDTO; + +import java.util.*; + +public class OrderSheet { + private static final int TOTAL_COUNT_LIMIT = 20; + + private final Map<Menu, Integer> orderSheet; + private int totalPrice; + + public OrderSheet(Map<String, Integer> menus) { + orderSheet = new HashMap<>(); + validateExceedMenuCount(menus); + countMenu(menus); + calculateTotalPrice(); + validateOnlyDrink(); + } + + public int getMenuCountByMenuType(MenuType menuType) { + int menuCount = orderSheet.keySet().stream() + .filter(menu -> menu.compareType(menuType)) + .map(menu -> orderSheet.get(menu)) + .reduce(0, Integer::sum); + return menuCount; + } + + public int calculateTotalPrice() { + this.totalPrice = orderSheet.keySet().stream() + .map(menu -> menu.getPrice() * orderSheet.get(menu)) + .reduce(0, Integer::sum); + return totalPrice; + } + + private void countMenu(Map<String, Integer> menus) { + for (String menuName : menus.keySet()) { + Menu menu = Menu.getMenuByName(menuName); + orderSheet.put(menu, menus.get(menuName)); + } + } + + public Price getTotalPrice() { + return new Price(totalPrice); + } + + public List<MenuDTO> getOrderSheet() { + return orderSheet.keySet().stream() + .map(menu -> new MenuDTO(menu.getName(), orderSheet.get(menu))) + .toList(); + } + + public boolean isMoreThanTotal(int amount) { + return totalPrice >= amount; + } + + private void validateExceedMenuCount(Map<String, Integer> menus) { + int totalMenuCount = menus.values().stream() + .reduce(0, Integer::sum); + if (totalMenuCount <= TOTAL_COUNT_LIMIT) return; + throw new IllegalArgumentException(ErrorMessage.EXCEED_MENU_COUNT.getErrorMessage()); + } + + private void validateOnlyDrink() { + int drinkCount = (int) orderSheet.keySet().stream() + .filter(menu -> menu.compareType(MenuType.DRINK)) + .count(); + if (drinkCount == orderSheet.size()) + throw new IllegalArgumentException(ErrorMessage.ONLY_DRINK.getErrorMessage()); + } +}
Java
Stream์— allMatch()๋ผ๋Š” ์ข‹์€ ๊ธฐ๋Šฅ์ด ์žˆ๋Š” ์ค„ ๋ชฐ๋ž์Šต๋‹ˆ๋‹ค! ํ™•์‹คํžˆ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ์š”๊ตฌ์‚ฌํ•ญ(๋ชจ๋“  ๋ฉ”๋‰ด๊ฐ€ ์Œ๋ฃŒ์ˆ˜์ธ์ง€ ํ™•์ธ)์„ ๊ตฌํ˜„ํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ์ž๋ฐ” API์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. 4์ฃผ์ฐจ ๋ฏธ์…˜์„ ๋ฆฌํŒฉํ† ๋งํ•  ๋•Œ ์ ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,88 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.day.Day; +import christmas.domain.order.OrderSheet; +import christmas.domain.Price; +import christmas.domain.event.TotalEventResult; +import christmas.domain.event.discount.DiscountEventManager; +import christmas.domain.event.discount.DiscountResult; +import christmas.domain.event.present.PresentEventManager; +import christmas.domain.event.present.PresentResult; +import christmas.dto.BadgeDTO; +import christmas.dto.PriceDTO; +import christmas.view.InputView; +import christmas.view.OutputView; + +import java.util.Map; + +public class PromotionController { + private final InputView inputView; + private final OutputView outputView; + + public PromotionController(InputView inputView, OutputView outputView) { + this.inputView = inputView; + this.outputView = outputView; + } + + public void run() { + outputView.printStart(); + Day orderDay = getOrderDay(); + OrderSheet orderSheet = getOrderSheet(); + printPreview(orderDay, orderSheet); + } + + private Day getOrderDay() { + boolean isInValidInput = true; + Day orderDay = null; + while (isInValidInput) { + try { + int dayNumber = inputView.readDate(); + orderDay = new Day(dayNumber); + isInValidInput = false; + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e.getMessage()); + } + } + return orderDay; + } + + private OrderSheet getOrderSheet() { + boolean isInValidInput = true; + OrderSheet orderSheet = null; + while (isInValidInput) { + try { + Map<String, Integer> orderMenus = inputView.readMenu(); + orderSheet = new OrderSheet(orderMenus); + isInValidInput = false; + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e.getMessage()); + } + } + return orderSheet; + } + + private void printPreview(Day orderDay, OrderSheet orderSheet) { + printBeforeEvent(orderSheet); + printAfterEvent(orderDay, orderSheet); + } + + private void printBeforeEvent(OrderSheet orderSheet) { + outputView.printPreviewStart(); + outputView.printOrderSheet(orderSheet.getOrderSheet()); + outputView.printTotalPriceBeforeDiscount(PriceDTO.from(orderSheet.getTotalPrice())); + } + + private void printAfterEvent(Day orderDay, OrderSheet orderSheet) { + DiscountResult discountResult = DiscountEventManager.getDiscountResult(orderDay, orderSheet); + PresentResult presentResult = PresentEventManager.getPresentResult(orderSheet); + outputView.printPresents(presentResult.getPresents()); + outputView.printEvents(discountResult.getDiscountResults(), PriceDTO.from(presentResult.getTotalPresentPrice())); + + TotalEventResult totalEventResult = new TotalEventResult(discountResult, presentResult); + outputView.printTotalDiscountPrice(totalEventResult.getDiscountPrice()); + outputView.printTotalDiscountedPrice(totalEventResult.getDiscountedPrice(orderSheet.getTotalPrice())); + BadgeDTO badge = totalEventResult.getBadge(); + outputView.printBadge(badge); + } +}
Java
์ด๋Ÿฐ ๊ฟ€ํŒ ๊ณ ๋ง™์Šต๋‹ˆ๋‹ค. ์ €๋„ Intellij๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ ๋งค๋ฒˆ ๋ฆฌํŒฉํ† ๋งํ•  ๋•Œ๋งˆ๋‹ค ํ•„์š”๊ฐ€ ์—†๋Š” `import`๋ฅผ ๋†“์น˜๊ณค ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ณต์œ ํ•ด์ฃผ์‹  ํฌ์ŠคํŠธ ๋ณด๊ณ  ๋ฐฉ๊ธˆ ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ œ ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ๋ฐ›์œผ์…”์„œ ๋ฆฌ๋ทฐ๋ฅผ ํ•˜์‹  ๋…ธ๊ณ ์— ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๋์œผ๋กœ 4์ฃผ์ฐจ ๋ฏธ์…˜์ด public์œผ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์ง€๋งŒ pull request๊ฐ€ ์—†๋Š” ๊ฒƒ์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋ทฐ ์šฉ pull request ๋‚ ๋ฆฌ์‹œ๋ฉด ๋ฆฌ๋ทฐํ•˜๋Ÿฌ ๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,49 @@ +package christmas.domain.event.discount; + +import christmas.constants.AmountConstants; +import christmas.domain.day.Day; +import christmas.domain.order.OrderSheet; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public enum DiscountEventManager { + CHRISTMAS { + @Override + protected DiscountEvent create(Day day, OrderSheet orderSheet) { + return new ChristmasDiscountEvent(day); + } + }, + HOLIDAY { + @Override + protected DiscountEvent create(Day day, OrderSheet orderSheet) { + return new HolidayDiscountEvent(day, orderSheet); + } + }, + WEEKDAY { + @Override + protected DiscountEvent create(Day day, OrderSheet orderSheet) { + return new WeekDayDiscountEvent(day, orderSheet); + } + }, + STAR { + @Override + protected DiscountEvent create(Day day, OrderSheet orderSheet) { + return new StarDiscountEvent(day); + } + }; + + abstract protected DiscountEvent create(Day day, OrderSheet orderSheet); + + public static DiscountResult getDiscountResult(Day day, OrderSheet orderSheet) { + if (orderSheet.isMoreThanTotal(AmountConstants.DISCOUNT_THRESHOLD.getAmount())) { + List<DiscountEvent> discountEvents = Arrays.stream(values()) + .map(event -> event.create(day, orderSheet)) + .filter(DiscountEvent::isDiscountable) + .toList(); + return new DiscountResult(discountEvents); + } + return new DiscountResult(new ArrayList<>()); + } +}
Java
Enum ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ƒ๊ฐ์˜ ๊ณผ์ • ์ž˜ ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค! Enum์ด ์ƒ์ˆ˜ ํƒ€์ž…์ด๋‹ˆ ๊ฐ ์š”์†Œ๋“ค์€ ์‹ฑ๊ธ€ํ„ด์œผ๋กœ ์ƒ์„ฑ์ด ๋˜๊ฒ ๊ตฐ์š”. ๋ง์”€ํ•˜์‹ ๋Œ€๋กœ ๋”ฐ๋กœ ํŒฉํ„ฐ๋ฆฌ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•  ํ•„์š” ์—†์ด Enum ํด๋ž˜์Šค ์•ˆ์—์„œ ์‰ฝ๊ฒŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”. ๐Ÿ‘
@@ -0,0 +1,88 @@ +package christmas.controller; + +import christmas.domain.Badge; +import christmas.domain.day.Day; +import christmas.domain.order.OrderSheet; +import christmas.domain.Price; +import christmas.domain.event.TotalEventResult; +import christmas.domain.event.discount.DiscountEventManager; +import christmas.domain.event.discount.DiscountResult; +import christmas.domain.event.present.PresentEventManager; +import christmas.domain.event.present.PresentResult; +import christmas.dto.BadgeDTO; +import christmas.dto.PriceDTO; +import christmas.view.InputView; +import christmas.view.OutputView; + +import java.util.Map; + +public class PromotionController { + private final InputView inputView; + private final OutputView outputView; + + public PromotionController(InputView inputView, OutputView outputView) { + this.inputView = inputView; + this.outputView = outputView; + } + + public void run() { + outputView.printStart(); + Day orderDay = getOrderDay(); + OrderSheet orderSheet = getOrderSheet(); + printPreview(orderDay, orderSheet); + } + + private Day getOrderDay() { + boolean isInValidInput = true; + Day orderDay = null; + while (isInValidInput) { + try { + int dayNumber = inputView.readDate(); + orderDay = new Day(dayNumber); + isInValidInput = false; + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e.getMessage()); + } + } + return orderDay; + } + + private OrderSheet getOrderSheet() { + boolean isInValidInput = true; + OrderSheet orderSheet = null; + while (isInValidInput) { + try { + Map<String, Integer> orderMenus = inputView.readMenu(); + orderSheet = new OrderSheet(orderMenus); + isInValidInput = false; + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e.getMessage()); + } + } + return orderSheet; + } + + private void printPreview(Day orderDay, OrderSheet orderSheet) { + printBeforeEvent(orderSheet); + printAfterEvent(orderDay, orderSheet); + } + + private void printBeforeEvent(OrderSheet orderSheet) { + outputView.printPreviewStart(); + outputView.printOrderSheet(orderSheet.getOrderSheet()); + outputView.printTotalPriceBeforeDiscount(PriceDTO.from(orderSheet.getTotalPrice())); + } + + private void printAfterEvent(Day orderDay, OrderSheet orderSheet) { + DiscountResult discountResult = DiscountEventManager.getDiscountResult(orderDay, orderSheet); + PresentResult presentResult = PresentEventManager.getPresentResult(orderSheet); + outputView.printPresents(presentResult.getPresents()); + outputView.printEvents(discountResult.getDiscountResults(), PriceDTO.from(presentResult.getTotalPresentPrice())); + + TotalEventResult totalEventResult = new TotalEventResult(discountResult, presentResult); + outputView.printTotalDiscountPrice(totalEventResult.getDiscountPrice()); + outputView.printTotalDiscountedPrice(totalEventResult.getDiscountedPrice(orderSheet.getTotalPrice())); + BadgeDTO badge = totalEventResult.getBadge(); + outputView.printBadge(badge); + } +}
Java
๋ฆฌ๋ทฐ์šฉ์œผ๋กœ pull request ๋‚ ๋ ธ์Šต๋‹ˆ๋‹ค! ์ž๋น„์—†๋Š” ์ฝ”๋“œ๋ฆฌ๋ทฐ(?) ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค! ๐Ÿฅฒ
@@ -0,0 +1,31 @@ +package com.codesquad.datastructurestudy.basic; + +public class BinarySearch { + + private static int binarySearch(int[] numbers, int number, int first, int last) { + while (first <= last) { + int mid = (first + last) / 2; + + if (numbers[mid] == number) { + return mid; + } else if (numbers[mid] < number) { + first = mid + 1; + } else { + last = mid - 1; + } + } + return -1; + } + + public static void main(String[] args) { + int[] numbers = {1,2,3,4,5,6,7,8,9}; + int number = 2; + int index = binarySearch(numbers, number, 0, numbers.length - 1); + + if (index == -1) { + System.out.println("๋ชป์ฐพ์•˜๋‹ค"); + } else { + System.out.println(index + "๋ฒˆ์งธ ์ธ๋ฑ์Šค์—์„œ ์ฐพ์•˜๋‹ค"); + } + } +}
Java
์ด ๋ถ€๋ถ„์€ int overflow๋ฅผ ์กฐ์‹ฌํ•˜์…”์•ผํ•ด์š”. https://endorphin0710.tistory.com/112 ์—ฌ๊ธธ ์ฐธ๊ณ ํ•ด๋ณด์„ธ์š”.
@@ -0,0 +1,44 @@ +package com.codesquad.datastructurestudy.basic; + +import java.util.Arrays; + +public class BinarySearchOperationCount { + + private static int binarySearch(int[] numbers, int number, int first, int last) { + int mid; + int opCount = 0; + + while (first <= last) { + mid = (first + last) / 2; + + if (numbers[mid] == number) { + return mid; + } else if (numbers[mid] < number) { + first = mid + 1; + } else { + last = mid - 1; + } + opCount++; + } + return opCount; + } + + public static void main(String[] args) { + int[] arr1 = new int[500]; + int[] arr2 = new int[5000]; + int[] arr3 = new int[50000]; + int number = 5000; + + Arrays.fill(arr1, 0); + Arrays.fill(arr2, 0); + Arrays.fill(arr3, 0); + + int opCount1 = binarySearch(arr1, number, 0, arr1.length - 1); + int opCount2 = binarySearch(arr2, number, 0, arr2.length - 1); + int opCount3 = binarySearch(arr3, number, 0, arr3.length - 1); + + System.out.println("๋น„๊ต์—ฐ์‚ฐํšŸ์ˆ˜ : " + opCount1); + System.out.println("๋น„๊ต์—ฐ์‚ฐํšŸ์ˆ˜ : " + opCount2); + System.out.println("๋น„๊ต์—ฐ์‚ฐํšŸ์ˆ˜ : " + opCount3); + } +}
Java
์ด๊ฑด ์•ฝ๊ฐ„ ์œค์„ฑ์šฐ๋‹˜ ์ฑ… ์Šคํƒ€์ผ๋กœ ์„ ์–ธ์„ ํ–ˆ๋„ค์š” ใ…‹ใ…‹ ์ €๋ผ๋ฉด ์ดˆ๊ธฐํ™”์™€ ๋™์‹œ์— ์„ ์–ธํ•˜๋Š” ์Šคํƒ€์ผ์„ ์‚ฌ์šฉํ–ˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,44 @@ +package com.codesquad.datastructurestudy.basic; + +import java.util.Arrays; + +public class BinarySearchOperationCount { + + private static int binarySearch(int[] numbers, int number, int first, int last) { + int mid; + int opCount = 0; + + while (first <= last) { + mid = (first + last) / 2; + + if (numbers[mid] == number) { + return mid; + } else if (numbers[mid] < number) { + first = mid + 1; + } else { + last = mid - 1; + } + opCount++; + } + return opCount; + } + + public static void main(String[] args) { + int[] arr1 = new int[500]; + int[] arr2 = new int[5000]; + int[] arr3 = new int[50000]; + int number = 5000; + + Arrays.fill(arr1, 0); + Arrays.fill(arr2, 0); + Arrays.fill(arr3, 0); + + int opCount1 = binarySearch(arr1, number, 0, arr1.length - 1); + int opCount2 = binarySearch(arr2, number, 0, arr2.length - 1); + int opCount3 = binarySearch(arr3, number, 0, arr3.length - 1); + + System.out.println("๋น„๊ต์—ฐ์‚ฐํšŸ์ˆ˜ : " + opCount1); + System.out.println("๋น„๊ต์—ฐ์‚ฐํšŸ์ˆ˜ : " + opCount2); + System.out.println("๋น„๊ต์—ฐ์‚ฐํšŸ์ˆ˜ : " + opCount3); + } +}
Java
์—ฌ๊ธฐ๋„ ๋ฆฌ๋ทฐ ์ฐธ๊ณ ํ•ด์„œ ์ˆ˜์ •ํ•˜์‹œ๊ธธ ๋ฐ”๋ž„๊ฒŒ์š”!
@@ -0,0 +1,44 @@ +package com.codesquad.datastructurestudy.basic; + +import java.util.Arrays; + +public class BinarySearchOperationCount { + + private static int binarySearch(int[] numbers, int number, int first, int last) { + int mid; + int opCount = 0; + + while (first <= last) { + mid = (first + last) / 2; + + if (numbers[mid] == number) { + return mid; + } else if (numbers[mid] < number) { + first = mid + 1; + } else { + last = mid - 1; + } + opCount++; + } + return opCount; + } + + public static void main(String[] args) { + int[] arr1 = new int[500]; + int[] arr2 = new int[5000]; + int[] arr3 = new int[50000]; + int number = 5000; + + Arrays.fill(arr1, 0); + Arrays.fill(arr2, 0); + Arrays.fill(arr3, 0); + + int opCount1 = binarySearch(arr1, number, 0, arr1.length - 1); + int opCount2 = binarySearch(arr2, number, 0, arr2.length - 1); + int opCount3 = binarySearch(arr3, number, 0, arr3.length - 1); + + System.out.println("๋น„๊ต์—ฐ์‚ฐํšŸ์ˆ˜ : " + opCount1); + System.out.println("๋น„๊ต์—ฐ์‚ฐํšŸ์ˆ˜ : " + opCount2); + System.out.println("๋น„๊ต์—ฐ์‚ฐํšŸ์ˆ˜ : " + opCount3); + } +}
Java
์ œ๊ฐ€ ์œค์„ฑ์šฐ ์ฑ…์—์„œ ์•ˆ์ข‹์•„ํ•˜๋Š” ๋ถ€๋ถ„์ด ๋ช…ํ™•ํ•˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๋ช…์ด์—์š”. ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๊ธฐ ๋ณด๋‹ค๋Š” ๋ณด๋‹ค ๋ช…ํ™•ํ•œ ๋ณ€์ˆ˜๋ช…์„ ์‚ฌ์šฉํ•˜์‹œ๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,26 @@ +package com.codesquad.datastructurestudy.basic; + +public class LinearSearch { + + private static int linearSearch(int[] numbers, int number) { + int i; + for (i = 0; i < numbers.length; i++) { + if (numbers[i] == number) { + return i; + } + } + return -1; + } + + public static void main(String[] args) { + int[] arr = {1,2,3,4,5}; + int number = 2; + int index = linearSearch(arr, number); + + if (index == -1) { + System.out.println("๋ชป์ฐพ์•˜๋‹ค"); + } else { + System.out.println((index+1) + "๋ฒˆ์งธ ์—์„œ ์ฐพ์•˜๋‹ค"); + } + } +}
Java
์•—... ์•„์•„...
@@ -0,0 +1,26 @@ +package com.codesquad.datastructurestudy.basic; + +public class LinearSearch { + + private static int linearSearch(int[] numbers, int number) { + int i; + for (i = 0; i < numbers.length; i++) { + if (numbers[i] == number) { + return i; + } + } + return -1; + } + + public static void main(String[] args) { + int[] arr = {1,2,3,4,5}; + int number = 2; + int index = linearSearch(arr, number); + + if (index == -1) { + System.out.println("๋ชป์ฐพ์•˜๋‹ค"); + } else { + System.out.println((index+1) + "๋ฒˆ์งธ ์—์„œ ์ฐพ์•˜๋‹ค"); + } + } +}
Java
```suggestion System.out.println((index + 1) + "๋ฒˆ์งธ ์—์„œ ์ฐพ์•˜๋‹ค"); ``` ์‚ฌ์†Œํ•œ๊ฑฐ์ง€๋งŒ, ์ด๋Ÿฐ๊ฑด ๋„์–ด์“ฐ๋Š”๊ฒŒ ๊ด€๋ก€์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,85 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class ArrayList implements List<Integer> { + + private int[] numbers; + private int numOfData; + + public ArrayList() { + this.numbers = new int[100]; + this.numOfData = 0; + } + + public ArrayList(int[] numbers, int numOfData) { + this.numbers = numbers; + this.numOfData = numOfData; + } + + public void add(int index, Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); +// throw new ArrayIndexOutOfBoundsException(); + } + this.numbers[index] = element; + numOfData++; + } + + public void add(Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); + } + this.numbers[numOfData] = element; + numOfData++; + } + + public int count() { + return this.numOfData; + } + + public Integer get(int index) { + return this.numbers[index]; + } + + public Integer remove(int index) { + Integer data = this.numbers[index]; + + for (int i = index; i < this.numOfData - 1; i++) { + this.numbers[i] = this.numbers[i+1]; + } + + this.numOfData--; + return data; + } + + public int size() { + return this.numbers.length; + } + + public static void main(String[] args) { + List<Integer> list = new ArrayList(); + + // ๋ฐ์ดํ„ฐ ์‚ฝ์ž… + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + // ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ + System.out.println("ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + list.remove(2); + list.remove(3); + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐฐ์—ด ํฌ๊ธฐ: " + list.size()); + } +}
Java
ํŠน์ดํ•˜๋„ค์š” ใ…‹ใ…‹ใ…‹ ์ œ๋„ค๋ฆญ์„ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,85 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class ArrayList implements List<Integer> { + + private int[] numbers; + private int numOfData; + + public ArrayList() { + this.numbers = new int[100]; + this.numOfData = 0; + } + + public ArrayList(int[] numbers, int numOfData) { + this.numbers = numbers; + this.numOfData = numOfData; + } + + public void add(int index, Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); +// throw new ArrayIndexOutOfBoundsException(); + } + this.numbers[index] = element; + numOfData++; + } + + public void add(Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); + } + this.numbers[numOfData] = element; + numOfData++; + } + + public int count() { + return this.numOfData; + } + + public Integer get(int index) { + return this.numbers[index]; + } + + public Integer remove(int index) { + Integer data = this.numbers[index]; + + for (int i = index; i < this.numOfData - 1; i++) { + this.numbers[i] = this.numbers[i+1]; + } + + this.numOfData--; + return data; + } + + public int size() { + return this.numbers.length; + } + + public static void main(String[] args) { + List<Integer> list = new ArrayList(); + + // ๋ฐ์ดํ„ฐ ์‚ฝ์ž… + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + // ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ + System.out.println("ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + list.remove(2); + list.remove(3); + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐฐ์—ด ํฌ๊ธฐ: " + list.size()); + } +}
Java
์Œ ๊ฐ€๊ธ‰์ ์ด๋ฉด ์ˆซ์ž ์ƒ์ˆ˜๋„ private static final๋กœ ๋นผ์„œ ์‚ฌ์šฉํ•˜๋Š” ํŽธ์ด ์ข‹์ง€ ์•Š์„๊นŒ ์‹ถ์–ด์š”. ๊ทธ๋ฆฌ๊ณ  numOfData๋Š” ๊ทธ๋ƒฅ ์ œ๊ฑฐํ•ด๋„ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ๋˜๋‹ˆ ๊ทธ ์ ์„ ์—ผ๋‘์— ๋‘์‹œ๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š”.
@@ -0,0 +1,85 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class ArrayList implements List<Integer> { + + private int[] numbers; + private int numOfData; + + public ArrayList() { + this.numbers = new int[100]; + this.numOfData = 0; + } + + public ArrayList(int[] numbers, int numOfData) { + this.numbers = numbers; + this.numOfData = numOfData; + } + + public void add(int index, Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); +// throw new ArrayIndexOutOfBoundsException(); + } + this.numbers[index] = element; + numOfData++; + } + + public void add(Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); + } + this.numbers[numOfData] = element; + numOfData++; + } + + public int count() { + return this.numOfData; + } + + public Integer get(int index) { + return this.numbers[index]; + } + + public Integer remove(int index) { + Integer data = this.numbers[index]; + + for (int i = index; i < this.numOfData - 1; i++) { + this.numbers[i] = this.numbers[i+1]; + } + + this.numOfData--; + return data; + } + + public int size() { + return this.numbers.length; + } + + public static void main(String[] args) { + List<Integer> list = new ArrayList(); + + // ๋ฐ์ดํ„ฐ ์‚ฝ์ž… + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + // ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ + System.out.println("ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + list.remove(2); + list.remove(3); + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐฐ์—ด ํฌ๊ธฐ: " + list.size()); + } +}
Java
์ถ”๊ฐ€๋กœ... ์ €๋ผ๋ฉด ์ƒ์„ฑ์ž๋ฅผ ```java public ArrayList(int initialSize) { this.numbers = new int[initialSize]; } ``` ์ด๋Ÿฐ ์‹์œผ๋กœ ๋งŒ๋“ค๊ณ  ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์„ ํ†ตํ•ด์„œ ```java public ArrayList() { this(INITIAL_SIZE); } ``` ์™€ ๊ฐ™์€ ์‹์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ดค์„ ๊ฒƒ ๊ฐ™๋„ค์š”.
@@ -0,0 +1,85 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class ArrayList implements List<Integer> { + + private int[] numbers; + private int numOfData; + + public ArrayList() { + this.numbers = new int[100]; + this.numOfData = 0; + } + + public ArrayList(int[] numbers, int numOfData) { + this.numbers = numbers; + this.numOfData = numOfData; + } + + public void add(int index, Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); +// throw new ArrayIndexOutOfBoundsException(); + } + this.numbers[index] = element; + numOfData++; + } + + public void add(Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); + } + this.numbers[numOfData] = element; + numOfData++; + } + + public int count() { + return this.numOfData; + } + + public Integer get(int index) { + return this.numbers[index]; + } + + public Integer remove(int index) { + Integer data = this.numbers[index]; + + for (int i = index; i < this.numOfData - 1; i++) { + this.numbers[i] = this.numbers[i+1]; + } + + this.numOfData--; + return data; + } + + public int size() { + return this.numbers.length; + } + + public static void main(String[] args) { + List<Integer> list = new ArrayList(); + + // ๋ฐ์ดํ„ฐ ์‚ฝ์ž… + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + // ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ + System.out.println("ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + list.remove(2); + list.remove(3); + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐฐ์—ด ํฌ๊ธฐ: " + list.size()); + } +}
Java
์‚ด์ง ์• ๋งคํ•œ ์ƒ์„ฑ์ž ๊ฐ™์•„์š”. ์ด ์ƒ์„ฑ์ž๊ฐ€ ๊ณผ์—ฐ ํ•„์š”ํ• ๊นŒ? ๋ผ๋Š” ์ƒ๊ฐ๋„ ๋“ค๊ณ ์š”.
@@ -0,0 +1,85 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class ArrayList implements List<Integer> { + + private int[] numbers; + private int numOfData; + + public ArrayList() { + this.numbers = new int[100]; + this.numOfData = 0; + } + + public ArrayList(int[] numbers, int numOfData) { + this.numbers = numbers; + this.numOfData = numOfData; + } + + public void add(int index, Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); +// throw new ArrayIndexOutOfBoundsException(); + } + this.numbers[index] = element; + numOfData++; + } + + public void add(Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); + } + this.numbers[numOfData] = element; + numOfData++; + } + + public int count() { + return this.numOfData; + } + + public Integer get(int index) { + return this.numbers[index]; + } + + public Integer remove(int index) { + Integer data = this.numbers[index]; + + for (int i = index; i < this.numOfData - 1; i++) { + this.numbers[i] = this.numbers[i+1]; + } + + this.numOfData--; + return data; + } + + public int size() { + return this.numbers.length; + } + + public static void main(String[] args) { + List<Integer> list = new ArrayList(); + + // ๋ฐ์ดํ„ฐ ์‚ฝ์ž… + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + // ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ + System.out.println("ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + list.remove(2); + list.remove(3); + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐฐ์—ด ํฌ๊ธฐ: " + list.size()); + } +}
Java
์•ˆ์“ฐ๋Š” ์ฃผ์„์€ ์ œ๊ฑฐํ•ด์ฃผ์„ธ์š”~
@@ -0,0 +1,85 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class ArrayList implements List<Integer> { + + private int[] numbers; + private int numOfData; + + public ArrayList() { + this.numbers = new int[100]; + this.numOfData = 0; + } + + public ArrayList(int[] numbers, int numOfData) { + this.numbers = numbers; + this.numOfData = numOfData; + } + + public void add(int index, Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); +// throw new ArrayIndexOutOfBoundsException(); + } + this.numbers[index] = element; + numOfData++; + } + + public void add(Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); + } + this.numbers[numOfData] = element; + numOfData++; + } + + public int count() { + return this.numOfData; + } + + public Integer get(int index) { + return this.numbers[index]; + } + + public Integer remove(int index) { + Integer data = this.numbers[index]; + + for (int i = index; i < this.numOfData - 1; i++) { + this.numbers[i] = this.numbers[i+1]; + } + + this.numOfData--; + return data; + } + + public int size() { + return this.numbers.length; + } + + public static void main(String[] args) { + List<Integer> list = new ArrayList(); + + // ๋ฐ์ดํ„ฐ ์‚ฝ์ž… + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + // ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ + System.out.println("ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + list.remove(2); + list.remove(3); + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐฐ์—ด ํฌ๊ธฐ: " + list.size()); + } +}
Java
์ด ๋ฉ”์„œ๋“œ ํ™•์‹คํ•œ๊ฐ€์š”? ํ–‰๋™ํ•˜๋Š” ๊ฒƒ์€ replace์— ๊ฐ€๊นŒ์šด๋ฐ, ํ•˜๋Š” ๋™์ž‘์— ๊ตฐ๋”๋”๊ธฐ๊ฐ€ ๋งŽ์ด ๋“ค์–ด๊ฐ„ ๋А๋‚Œ์ž…๋‹ˆ๋‹ค. ํŠนํžˆ ๊ฐœ์ˆ˜ ์ฒดํฌํ•˜๋Š” ๋กœ์ง์€ ์—†์–ด๋„ ๋  ๊ฒƒ ๊ฐ™๊ณ , ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ `numOfData++;` ๋„ ๋ถˆํ•„์š”ํ•ด๋ณด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  `IndexOutOfBoundException`๋„ ๋ฐœ์ƒ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. 1. add์ธ๋ฐ, ๋งŒ์•ฝ 0๋ฒˆ ์œ„์น˜์— ๊ณ„์† ๋ฐ์ดํ„ฐ๋ฅผ ์ง‘์–ด๋„ฃ์œผ๋ฉด? ๋‚˜์ค‘์—” ๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ๋‹ค๊ณ  ํ•˜๋ฉด์„œ ๋ฐ์ดํ„ฐ๋Š” ๋ฐ”๋€Œ๊ณ , `numOfData`๋„ ๊ณ„์† ์ฆ๊ฐ€ํ• ๊ฒ๋‹ˆ๋‹ค. if๋ฌธ์ด ์ข…๋ฃŒ๋˜๊ณ  ์•„๋ž˜ ๋ผ์ธ๋“ค์ด ์‹คํ–‰๋  ํ•„์š”๊ฐ€ ์—†๋‹ค๋ฉด `return`๋ฌธ์„ ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”. 2. `numOfData`๋ผ๋Š” ๋ฉค๋ฒ„๋ณ€์ˆ˜์— `this` ํ‚ค์›Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”. ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. 23๋ฒˆ ๋ผ์ธ์— `IndexOutOfBoundException`์ด ๋ฐœ์ƒ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๊ฐ€ ๊ทธ๋Œ€๋กœ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋˜์ง€, if ๋ฌธ์œผ๋กœ ์กฐ๊ฑด์„ ๋‹ฌ์•„์ฃผ์„ธ์š”. (์ธ๋ฑ์Šค์˜ ๋ฒ”์œ„๋ฅผ ์ƒ๊ฐํ•ด๋ด…์‹œ๋‹ค.) 4. 23๋ฒˆ ๋ผ์ธ์— element๊ฐ€ unboxing๋  ๋•Œ, ์–ด๋–ป๊ฒŒ ๋ ์ง€๋„ ์ƒ๊ฐํ•ด๋ด…์‹œ๋‹ค. `null`์ด ๊ทธ๋Œ€๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค๋ฉด? ์–ด๋–ค ๊ฒฐ๊ณผ๊ฐ€ ๋ฒŒ์–ด์งˆ๊นŒ์š”?
@@ -0,0 +1,85 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class ArrayList implements List<Integer> { + + private int[] numbers; + private int numOfData; + + public ArrayList() { + this.numbers = new int[100]; + this.numOfData = 0; + } + + public ArrayList(int[] numbers, int numOfData) { + this.numbers = numbers; + this.numOfData = numOfData; + } + + public void add(int index, Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); +// throw new ArrayIndexOutOfBoundsException(); + } + this.numbers[index] = element; + numOfData++; + } + + public void add(Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); + } + this.numbers[numOfData] = element; + numOfData++; + } + + public int count() { + return this.numOfData; + } + + public Integer get(int index) { + return this.numbers[index]; + } + + public Integer remove(int index) { + Integer data = this.numbers[index]; + + for (int i = index; i < this.numOfData - 1; i++) { + this.numbers[i] = this.numbers[i+1]; + } + + this.numOfData--; + return data; + } + + public int size() { + return this.numbers.length; + } + + public static void main(String[] args) { + List<Integer> list = new ArrayList(); + + // ๋ฐ์ดํ„ฐ ์‚ฝ์ž… + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + // ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ + System.out.println("ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + list.remove(2); + list.remove(3); + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐฐ์—ด ํฌ๊ธฐ: " + list.size()); + } +}
Java
์—ฌ๊ธฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋ฉ˜ํŠธ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์ •ํ™•ํ•œ ๋กœ์ง์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”.
@@ -0,0 +1,85 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class ArrayList implements List<Integer> { + + private int[] numbers; + private int numOfData; + + public ArrayList() { + this.numbers = new int[100]; + this.numOfData = 0; + } + + public ArrayList(int[] numbers, int numOfData) { + this.numbers = numbers; + this.numOfData = numOfData; + } + + public void add(int index, Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); +// throw new ArrayIndexOutOfBoundsException(); + } + this.numbers[index] = element; + numOfData++; + } + + public void add(Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); + } + this.numbers[numOfData] = element; + numOfData++; + } + + public int count() { + return this.numOfData; + } + + public Integer get(int index) { + return this.numbers[index]; + } + + public Integer remove(int index) { + Integer data = this.numbers[index]; + + for (int i = index; i < this.numOfData - 1; i++) { + this.numbers[i] = this.numbers[i+1]; + } + + this.numOfData--; + return data; + } + + public int size() { + return this.numbers.length; + } + + public static void main(String[] args) { + List<Integer> list = new ArrayList(); + + // ๋ฐ์ดํ„ฐ ์‚ฝ์ž… + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + // ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ + System.out.println("ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + list.remove(2); + list.remove(3); + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐฐ์—ด ํฌ๊ธฐ: " + list.size()); + } +}
Java
๋ฉ”์„œ๋“œ๋ช…์€ ๊ด€๋ก€์ƒ `size()`๊ฐ€ ์ข€ ๋” ์ ํ•ฉํ•ด๋ณด์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,85 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class ArrayList implements List<Integer> { + + private int[] numbers; + private int numOfData; + + public ArrayList() { + this.numbers = new int[100]; + this.numOfData = 0; + } + + public ArrayList(int[] numbers, int numOfData) { + this.numbers = numbers; + this.numOfData = numOfData; + } + + public void add(int index, Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); +// throw new ArrayIndexOutOfBoundsException(); + } + this.numbers[index] = element; + numOfData++; + } + + public void add(Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); + } + this.numbers[numOfData] = element; + numOfData++; + } + + public int count() { + return this.numOfData; + } + + public Integer get(int index) { + return this.numbers[index]; + } + + public Integer remove(int index) { + Integer data = this.numbers[index]; + + for (int i = index; i < this.numOfData - 1; i++) { + this.numbers[i] = this.numbers[i+1]; + } + + this.numOfData--; + return data; + } + + public int size() { + return this.numbers.length; + } + + public static void main(String[] args) { + List<Integer> list = new ArrayList(); + + // ๋ฐ์ดํ„ฐ ์‚ฝ์ž… + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + // ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ + System.out.println("ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + list.remove(2); + list.remove(3); + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐฐ์—ด ํฌ๊ธฐ: " + list.size()); + } +}
Java
`wrapper type`์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๋ฉด `auto boxing`์ด ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„  ๋ถˆํ•„์š”ํ•˜๋‹ค๊ณ  ๋ณด์ž…๋‹ˆ๋‹ค. `Integer`์™€ `int` ํƒ€์ž…์€ ๋งŽ์€ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„, Nullable ๋“ฑ์˜ ์ฐจ์ด๊ฐ€ ์žˆ๊ฒ ๋„ค์š”.
@@ -0,0 +1,85 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class ArrayList implements List<Integer> { + + private int[] numbers; + private int numOfData; + + public ArrayList() { + this.numbers = new int[100]; + this.numOfData = 0; + } + + public ArrayList(int[] numbers, int numOfData) { + this.numbers = numbers; + this.numOfData = numOfData; + } + + public void add(int index, Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); +// throw new ArrayIndexOutOfBoundsException(); + } + this.numbers[index] = element; + numOfData++; + } + + public void add(Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); + } + this.numbers[numOfData] = element; + numOfData++; + } + + public int count() { + return this.numOfData; + } + + public Integer get(int index) { + return this.numbers[index]; + } + + public Integer remove(int index) { + Integer data = this.numbers[index]; + + for (int i = index; i < this.numOfData - 1; i++) { + this.numbers[i] = this.numbers[i+1]; + } + + this.numOfData--; + return data; + } + + public int size() { + return this.numbers.length; + } + + public static void main(String[] args) { + List<Integer> list = new ArrayList(); + + // ๋ฐ์ดํ„ฐ ์‚ฝ์ž… + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + // ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ + System.out.println("ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + list.remove(2); + list.remove(3); + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐฐ์—ด ํฌ๊ธฐ: " + list.size()); + } +}
Java
๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋‹ค๋ฉด 0์„ ์ฃผ๊ฒ ๋„ค์š”? ๋ญ”๊ฐ€ ์ด์ƒํ•˜์ง€ ์•Š์„๊นŒ์š” ใ…Žใ…Ž
@@ -0,0 +1,85 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class ArrayList implements List<Integer> { + + private int[] numbers; + private int numOfData; + + public ArrayList() { + this.numbers = new int[100]; + this.numOfData = 0; + } + + public ArrayList(int[] numbers, int numOfData) { + this.numbers = numbers; + this.numOfData = numOfData; + } + + public void add(int index, Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); +// throw new ArrayIndexOutOfBoundsException(); + } + this.numbers[index] = element; + numOfData++; + } + + public void add(Integer element) { + if (numOfData > this.numbers.length) { + System.out.println("๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค"); + } + this.numbers[numOfData] = element; + numOfData++; + } + + public int count() { + return this.numOfData; + } + + public Integer get(int index) { + return this.numbers[index]; + } + + public Integer remove(int index) { + Integer data = this.numbers[index]; + + for (int i = index; i < this.numOfData - 1; i++) { + this.numbers[i] = this.numbers[i+1]; + } + + this.numOfData--; + return data; + } + + public int size() { + return this.numbers.length; + } + + public static void main(String[] args) { + List<Integer> list = new ArrayList(); + + // ๋ฐ์ดํ„ฐ ์‚ฝ์ž… + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + // ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ + System.out.println("ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + list.remove(2); + list.remove(3); + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜: " + list.count()); + + for (int i = 0; i < list.count(); i++) { + System.out.println(list.get(i)); + } + + System.out.println("์‚ญ์ œ ํ›„ ๋ฐฐ์—ด ํฌ๊ธฐ: " + list.size()); + } +}
Java
์‘? ์ด๊ฑฐ ์–ด๋–ป๊ฒŒ๋ณด๋ฉด ๋งž๋Š” ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๊ณ  ์•„๋‹Œ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. java์˜ size๋Š” ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋‚˜์š”?
@@ -0,0 +1,27 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public interface List<T> { + + void add(T element); + void add(int index, T element); + + int count(); + + T get(int index); + + T remove(int index); + + int size(); + +// int indexOf(T o); +// int lastIndexOf(T o); + +// ListIterator listIterator(); +// ListIterator listIterator(int index); + +// T set(int index, T element); + +// void sort(Comparator c); + +// List subList(int fromIndex, int toIndex); +}
Java
ใ…‹ใ…‹ใ…‹ ๋‚˜์ค‘์— ๊ตฌํ˜„ํ•  ์‚ฌํ•ญ๋“ค์ธ๊ฐ€์š”?
@@ -0,0 +1,35 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +public class ListMain { + + public static void main(String[] args) { + List<Integer> list = new ArrayList<>(); + for (int i = 0; i < 9; i++) { + list.add(i,i+1); + } + + int sum = 0; + for (int i = 0; i < list.size(); i++) { + sum += list.get(i); + } + System.out.println("๋ฆฌ์ŠคํŠธ์˜ ํ•ฉ๊ณ„๋Š” : " + sum); + + List list1 = new ArrayList(); + + for (int i = 0; i < list.size(); i++) { + if (!(list.get(i) % 3 == 0 || list.get(i) % 2 == 0)) { + list1.add(list.get(i)); + } + } + + Iterator<Integer> iterator = list1.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } +}
Java
์ „๋ฐ˜์ ์œผ๋กœ java coding convention์ด ์ž˜ ์ง€์ผœ์ง€์ง€ ์•Š๋Š” ๋ชจ์Šต์ด๋„ค์š”.
@@ -0,0 +1,35 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +public class ListMain { + + public static void main(String[] args) { + List<Integer> list = new ArrayList<>(); + for (int i = 0; i < 9; i++) { + list.add(i,i+1); + } + + int sum = 0; + for (int i = 0; i < list.size(); i++) { + sum += list.get(i); + } + System.out.println("๋ฆฌ์ŠคํŠธ์˜ ํ•ฉ๊ณ„๋Š” : " + sum); + + List list1 = new ArrayList(); + + for (int i = 0; i < list.size(); i++) { + if (!(list.get(i) % 3 == 0 || list.get(i) % 2 == 0)) { + list1.add(list.get(i)); + } + } + + Iterator<Integer> iterator = list1.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } +}
Java
enhanced for loop๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,50 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class NameCard { + + private String[] name; + private String[] phone; + + // NameCard ๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜์˜ ๋™์  ํ• ๋‹น ๋ฐ ์ดˆ๊ธฐํ™” ํ›„ ์ฃผ์†Œ ๊ฐ’ ๋ฐ˜ํ™˜ + public NameCard(String[] name, String[] phone) { + this.name = name; + this.phone = phone; + } + + public NameCard() { + this.name = new String[30]; + this.phone = new String[30]; + } + + // NameCard ๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜์˜ ์ •๋ณด ์ถœ๋ ฅ + public static void showNameCardInfo(NameCard nameCard) { + System.out.println("[์ด๋ฆ„] : " + nameCard.name); + System.out.println("[๋ฒˆํ˜ธ] : " + nameCard.phone); + } + + // ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด true, ๋‹ค๋ฅด๋ฉด false ๊ฐ’ ๋ฐ˜ํ™˜ + public static boolean nameCompare(NameCard nameCard, String[] name) { + return nameCard.name.equals(name); + } + + // ์ „ํ™”๋ฒˆํ˜ธ ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝ + public static void changePhoneNum(NameCard nameCard, String[] phone) { + nameCard.phone = phone; + } + + public static void main(String[] args) { + NameCard nameCard = new NameCard(); + List<Integer> list = new ArrayList(); + + // ์ด 3๋ช…์˜ ์ „ํ™”๋ฒˆํ˜ธ ์ •๋ณด๋ฅผ, ์•ž์„œ ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ˜„ํ•œ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค. + nameCard.name[0] = "์ด์ง„์ˆ˜"; + nameCard.name[1] = "ํ•œ์ง€์˜"; + nameCard.name[2] = "์กฐ์ˆ˜์ง„"; + nameCard.phone[0] = "010-1111-2222"; + nameCard.phone[1] = "010-2222-5555"; + nameCard.phone[2] = "010-3333-7777"; + + } + + +}
Java
์ข€ ๋” ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ์ ‘๊ทผ์„ ํ•ด๋ณด์„ธ์š” ใ…Žใ…Ž
@@ -0,0 +1,50 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public class NameCard { + + private String[] name; + private String[] phone; + + // NameCard ๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜์˜ ๋™์  ํ• ๋‹น ๋ฐ ์ดˆ๊ธฐํ™” ํ›„ ์ฃผ์†Œ ๊ฐ’ ๋ฐ˜ํ™˜ + public NameCard(String[] name, String[] phone) { + this.name = name; + this.phone = phone; + } + + public NameCard() { + this.name = new String[30]; + this.phone = new String[30]; + } + + // NameCard ๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜์˜ ์ •๋ณด ์ถœ๋ ฅ + public static void showNameCardInfo(NameCard nameCard) { + System.out.println("[์ด๋ฆ„] : " + nameCard.name); + System.out.println("[๋ฒˆํ˜ธ] : " + nameCard.phone); + } + + // ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด true, ๋‹ค๋ฅด๋ฉด false ๊ฐ’ ๋ฐ˜ํ™˜ + public static boolean nameCompare(NameCard nameCard, String[] name) { + return nameCard.name.equals(name); + } + + // ์ „ํ™”๋ฒˆํ˜ธ ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝ + public static void changePhoneNum(NameCard nameCard, String[] phone) { + nameCard.phone = phone; + } + + public static void main(String[] args) { + NameCard nameCard = new NameCard(); + List<Integer> list = new ArrayList(); + + // ์ด 3๋ช…์˜ ์ „ํ™”๋ฒˆํ˜ธ ์ •๋ณด๋ฅผ, ์•ž์„œ ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ˜„ํ•œ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค. + nameCard.name[0] = "์ด์ง„์ˆ˜"; + nameCard.name[1] = "ํ•œ์ง€์˜"; + nameCard.name[2] = "์กฐ์ˆ˜์ง„"; + nameCard.phone[0] = "010-1111-2222"; + nameCard.phone[1] = "010-2222-5555"; + nameCard.phone[2] = "010-3333-7777"; + + } + + +}
Java
์š” ์ฝ”๋“œ๋Š” ๋‹ค์‹œ ์ž‘์„ฑํ•˜๋Š”๊ฒŒ ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค. ์ „์ฒด์ ์œผ๋กœ ์ž๋ฐ”๋กœ ์ง  ๋А๋‚Œ์ด ํ•˜๋‚˜๋„ ๋“ค์ง€ ์•Š์•˜์–ด์š”.
@@ -0,0 +1,13 @@ +package com.codesquad.datastructurestudy.list.arraylist; + +public interface Wallet { + + // ๋ชจ๋“  ๋ฉค๋ฒ„๋ณ€์ˆ˜๋Š” public static final. ์ƒ๋žต ๊ฐ€๋Šฅ + int coin100Num = 0; + int bill5000Num = 0; + + // ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋Š” public abstract ์ด์–ด์•ผํ•จ. ์ƒ๋žต ๊ฐ€๋Šฅ + int takeOutMoney(int coin100Num, int bill5000Num); + void putMoney(int coin100Num, int bill5000Num); + +}
Java
ํ•™์Šต ๋‚ด์šฉ์€ ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค. ๊ทผ๋ฐ ๋ณดํ†ต interface์—์„œ๋Š” ๋ฉค๋ฒ„๋ณ€์ˆ˜ ์„ ์–ธ์„ ์ž˜ ์•ˆํ•˜๋Š” ํŽธ์ด๊ณ , ๋„ค์ด๋ฐ ์ปจ๋ฒค์…˜์— ๋งž๊ฒŒ ์ž‘์„ฑํ•ด์ฃผ์‹œ๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,164 @@ +package com.codesquad.datastructurestudy.list.linkedlist.yoon; + +public class CircularLinkedList { + + private Node tail; + private int count; + private Node before; + private Node cur; + + public void add(int data) { + Node newNode = new Node(data); + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + tail = newNode; + } + count++; + } + + public void addName(int data, String name) { + Node newNode = new Node(data); + newNode.name = name; + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + tail = newNode; + } + count++; + } + + public void addFront(int data) { + Node newNode = new Node(data); + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + } + count++; + } + + public void addNameFront(int data, String name) { + Node newNode = new Node(data); + newNode.name = name; + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + } + count++; + } + + public boolean first() { + if (tail == null) { + return false; + } else { + before = tail; + cur = tail.next; + return true; + } + } + + public boolean next(int data) { + if (tail == null) { + return false; + } else { + before = cur; + cur = cur.next; + return true; + } + } + + public int remove() { + Node deleteNode = cur; + int data = deleteNode.data; + + if (deleteNode == tail) { + if (tail == tail.next) { + tail = null; + } else { + tail = before; + } + } + + before.next = cur.next; + cur = before; + count--; + return data; + } + + public int size() { + return this.count; + } + + public String identifyWatcher(String name, int afterNum) { + Node searchNode = new Node(); + searchNode.next = tail.next; + int count = 0; + String watcher = null; + + while (true) { + if (searchNode.name == name) { + break; + } else { + searchNode = searchNode.next; + count++; + } + } + + for (int i = 0; i < afterNum + count; i++) { + if (i != afterNum + count - 1) { + searchNode = searchNode.next; + continue; + } else { + watcher = searchNode.name; + } + } + return watcher; + } + + public static void main(String[] args) { + CircularLinkedList members = new CircularLinkedList(); + + members.addName(1, "๊น€"); + members.addName(2, "๋‚˜"); + members.addName(3, "๋ฐ•"); + members.addName(4, "์ด"); + + System.out.println("์„œ์น˜ ์‹œ์ž‘"); + + System.out.println(members.identifyWatcher("๊น€", 7)); + + } + + private class Node { + + private int data; + private String name; + private Node next; + + public Node() { + this.data = 0; + this.next = null; + } + + public Node(int data) { + this.data = data; + this.next = null; + } + } +}
Java
์ƒ์„ฑ์ž์— ๋Œ€ํ•ด์„œ ์ข€๋งŒ ๋” ๊ณต๋ถ€ํ•ด๋ณด์„ธ์š”. ๋ถˆํ•„์š”ํ•œ ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋Š” ์ œ๊ฑฐํ•ด์ฃผ๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,164 @@ +package com.codesquad.datastructurestudy.list.linkedlist.yoon; + +public class CircularLinkedList { + + private Node tail; + private int count; + private Node before; + private Node cur; + + public void add(int data) { + Node newNode = new Node(data); + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + tail = newNode; + } + count++; + } + + public void addName(int data, String name) { + Node newNode = new Node(data); + newNode.name = name; + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + tail = newNode; + } + count++; + } + + public void addFront(int data) { + Node newNode = new Node(data); + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + } + count++; + } + + public void addNameFront(int data, String name) { + Node newNode = new Node(data); + newNode.name = name; + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + } + count++; + } + + public boolean first() { + if (tail == null) { + return false; + } else { + before = tail; + cur = tail.next; + return true; + } + } + + public boolean next(int data) { + if (tail == null) { + return false; + } else { + before = cur; + cur = cur.next; + return true; + } + } + + public int remove() { + Node deleteNode = cur; + int data = deleteNode.data; + + if (deleteNode == tail) { + if (tail == tail.next) { + tail = null; + } else { + tail = before; + } + } + + before.next = cur.next; + cur = before; + count--; + return data; + } + + public int size() { + return this.count; + } + + public String identifyWatcher(String name, int afterNum) { + Node searchNode = new Node(); + searchNode.next = tail.next; + int count = 0; + String watcher = null; + + while (true) { + if (searchNode.name == name) { + break; + } else { + searchNode = searchNode.next; + count++; + } + } + + for (int i = 0; i < afterNum + count; i++) { + if (i != afterNum + count - 1) { + searchNode = searchNode.next; + continue; + } else { + watcher = searchNode.name; + } + } + return watcher; + } + + public static void main(String[] args) { + CircularLinkedList members = new CircularLinkedList(); + + members.addName(1, "๊น€"); + members.addName(2, "๋‚˜"); + members.addName(3, "๋ฐ•"); + members.addName(4, "์ด"); + + System.out.println("์„œ์น˜ ์‹œ์ž‘"); + + System.out.println(members.identifyWatcher("๊น€", 7)); + + } + + private class Node { + + private int data; + private String name; + private Node next; + + public Node() { + this.data = 0; + this.next = null; + } + + public Node(int data) { + this.data = data; + this.next = null; + } + } +}
Java
์ƒ์„ฑ์ž๋ฅผ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค. `Node newNode = new Node(data, name);`
@@ -0,0 +1,164 @@ +package com.codesquad.datastructurestudy.list.linkedlist.yoon; + +public class CircularLinkedList { + + private Node tail; + private int count; + private Node before; + private Node cur; + + public void add(int data) { + Node newNode = new Node(data); + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + tail = newNode; + } + count++; + } + + public void addName(int data, String name) { + Node newNode = new Node(data); + newNode.name = name; + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + tail = newNode; + } + count++; + } + + public void addFront(int data) { + Node newNode = new Node(data); + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + } + count++; + } + + public void addNameFront(int data, String name) { + Node newNode = new Node(data); + newNode.name = name; + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + } + count++; + } + + public boolean first() { + if (tail == null) { + return false; + } else { + before = tail; + cur = tail.next; + return true; + } + } + + public boolean next(int data) { + if (tail == null) { + return false; + } else { + before = cur; + cur = cur.next; + return true; + } + } + + public int remove() { + Node deleteNode = cur; + int data = deleteNode.data; + + if (deleteNode == tail) { + if (tail == tail.next) { + tail = null; + } else { + tail = before; + } + } + + before.next = cur.next; + cur = before; + count--; + return data; + } + + public int size() { + return this.count; + } + + public String identifyWatcher(String name, int afterNum) { + Node searchNode = new Node(); + searchNode.next = tail.next; + int count = 0; + String watcher = null; + + while (true) { + if (searchNode.name == name) { + break; + } else { + searchNode = searchNode.next; + count++; + } + } + + for (int i = 0; i < afterNum + count; i++) { + if (i != afterNum + count - 1) { + searchNode = searchNode.next; + continue; + } else { + watcher = searchNode.name; + } + } + return watcher; + } + + public static void main(String[] args) { + CircularLinkedList members = new CircularLinkedList(); + + members.addName(1, "๊น€"); + members.addName(2, "๋‚˜"); + members.addName(3, "๋ฐ•"); + members.addName(4, "์ด"); + + System.out.println("์„œ์น˜ ์‹œ์ž‘"); + + System.out.println(members.identifyWatcher("๊น€", 7)); + + } + + private class Node { + + private int data; + private String name; + private Node next; + + public Node() { + this.data = 0; + this.next = null; + } + + public Node(int data) { + this.data = data; + this.next = null; + } + } +}
Java
๋ฉ”์†Œ๋“œ๋ช…๋งŒ ๋ณด๊ณ  ์–ด๋–ค ๊ธฐ๋Šฅ์„ ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๋„ค์š”.
@@ -0,0 +1,164 @@ +package com.codesquad.datastructurestudy.list.linkedlist.yoon; + +public class CircularLinkedList { + + private Node tail; + private int count; + private Node before; + private Node cur; + + public void add(int data) { + Node newNode = new Node(data); + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + tail = newNode; + } + count++; + } + + public void addName(int data, String name) { + Node newNode = new Node(data); + newNode.name = name; + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + tail = newNode; + } + count++; + } + + public void addFront(int data) { + Node newNode = new Node(data); + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + } + count++; + } + + public void addNameFront(int data, String name) { + Node newNode = new Node(data); + newNode.name = name; + + if (tail == null) { + tail = newNode; + newNode.next = newNode; + } else { + newNode.next = tail.next; + tail.next = newNode; + } + count++; + } + + public boolean first() { + if (tail == null) { + return false; + } else { + before = tail; + cur = tail.next; + return true; + } + } + + public boolean next(int data) { + if (tail == null) { + return false; + } else { + before = cur; + cur = cur.next; + return true; + } + } + + public int remove() { + Node deleteNode = cur; + int data = deleteNode.data; + + if (deleteNode == tail) { + if (tail == tail.next) { + tail = null; + } else { + tail = before; + } + } + + before.next = cur.next; + cur = before; + count--; + return data; + } + + public int size() { + return this.count; + } + + public String identifyWatcher(String name, int afterNum) { + Node searchNode = new Node(); + searchNode.next = tail.next; + int count = 0; + String watcher = null; + + while (true) { + if (searchNode.name == name) { + break; + } else { + searchNode = searchNode.next; + count++; + } + } + + for (int i = 0; i < afterNum + count; i++) { + if (i != afterNum + count - 1) { + searchNode = searchNode.next; + continue; + } else { + watcher = searchNode.name; + } + } + return watcher; + } + + public static void main(String[] args) { + CircularLinkedList members = new CircularLinkedList(); + + members.addName(1, "๊น€"); + members.addName(2, "๋‚˜"); + members.addName(3, "๋ฐ•"); + members.addName(4, "์ด"); + + System.out.println("์„œ์น˜ ์‹œ์ž‘"); + + System.out.println(members.identifyWatcher("๊น€", 7)); + + } + + private class Node { + + private int data; + private String name; + private Node next; + + public Node() { + this.data = 0; + this.next = null; + } + + public Node(int data) { + this.data = data; + this.next = null; + } + } +}
Java
`this` ๋ฅผ ๊ฐ€๋Šฅํ•œํ•œ ๋ถ™์—ฌ์ฃผ์„ธ์š”.
@@ -0,0 +1,118 @@ +import { GUIDE_MESSAGES } from '../constants/messages.js'; +import { + RESULT_BENEFIT_TITLES, + RESULT_ITEM_TITLES, +} from '../constants/resultTitles.js'; +import { SEPARATORS } from '../constants/separators.js'; +import { UNITS } from '../constants/units.js'; +import { GIFT_MENUS } from '../constants/gifts.js'; +import { SIGNS } from '../constants/signs.js'; +import { BADGES } from '../constants/badges.js'; + +export const generateResultStartMessageFormat = (visitDate) => + `12${UNITS.MONTH} ${visitDate}${UNITS.DATE}์— ${GUIDE_MESSAGES.SHOW_RESULT}`; + +export const generateOrderedMenusFormat = (orderedMenus) => { + return `${RESULT_ITEM_TITLES.ORDERED_MENUS}${ + SEPARATORS.LINE_BREAK_SEPARATOR + }${Object.entries(orderedMenus) + .map((orderedMenu) => { + const [orderedMenuName, orderedMenuCount] = orderedMenu; + return `${orderedMenuName} ${orderedMenuCount}${UNITS.COUNT}`; + }) + .join(SEPARATORS.LINE_BREAK_SEPARATOR)}`; +}; + +export const generateNumberWithCommasFormat = (number) => + number.toLocaleString(); + +export const generateTotalOrderAmountFormat = (totalOrderAmount) => { + return `${RESULT_ITEM_TITLES.TOTAL_ORDER_AMOUNT}${ + SEPARATORS.LINE_BREAK_SEPARATOR + }${generateNumberWithCommasFormat(totalOrderAmount)}${UNITS.WON}`; +}; + +export const generateGiftMenuFormat = (giftAmount) => { + return `${RESULT_ITEM_TITLES.GIFT_MENU}${SEPARATORS.LINE_BREAK_SEPARATOR}${ + giftAmount + ? `${GIFT_MENUS.GIFT_MENU} 1${UNITS.COUNT}` + : GUIDE_MESSAGES.NOT_EXSIT_MATCHED_RESULT + }`; +}; + +export const generateDetailFormat = (benefitTitle, benefitAmount) => { + return benefitAmount + ? `${benefitTitle}: ${SIGNS.MINUS}${generateNumberWithCommasFormat( + benefitAmount + )}${UNITS.WON}` + : ''; +}; + +export const generateDetailsFormat = ({ tbA, cddA, wddA, wedA, sdA, gA }) => { + const benefitDetailsTitle = `${RESULT_ITEM_TITLES.BENEFIT_DETAILS}${SEPARATORS.LINE_BREAK_SEPARATOR}`; + if (tbA === 0) + return `${benefitDetailsTitle}${GUIDE_MESSAGES.NOT_EXSIT_MATCHED_RESULT}`; + const benefitDetails = []; + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.CDD, cddA)); + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.WDD, wddA)); + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.WED, wedA)); + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.SD, sdA)); + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.G, gA)); + return `${benefitDetailsTitle}${benefitDetails + .filter((benefitDetail) => benefitDetail.length) + .join(SEPARATORS.LINE_BREAK_SEPARATOR)}`; +}; + +export const generateTotalBenefitAmountFormat = (totalBenefitAmount) => { + return `${RESULT_ITEM_TITLES.TOTAL_BENEFIT_AMOUNT}${ + SEPARATORS.LINE_BREAK_SEPARATOR + }${ + totalBenefitAmount + ? `${SIGNS.MINUS}${generateNumberWithCommasFormat(totalBenefitAmount)}` + : 0 + }${UNITS.WON}`; +}; + +export const generatePaymentAmountFormat = (paymentAmount) => { + return `${RESULT_ITEM_TITLES.PAYMENT_AMOUNT}${ + SEPARATORS.LINE_BREAK_SEPARATOR + }${generateNumberWithCommasFormat(paymentAmount)}${UNITS.WON}`; +}; + +export const generateEventBadgeFormat = (eventBadge) => { + return `${RESULT_ITEM_TITLES.EVENT_BADGE}${SEPARATORS.LINE_BREAK_SEPARATOR}${ + eventBadge === BADGES.NOT_MATCHED_BADGE + ? GUIDE_MESSAGES.NOT_EXSIT_MATCHED_RESULT + : eventBadge + }`; +}; + +export const generateResultFormat = (params) => { + return [ + generateResultStartMessageFormat(params.visitDate), + generateOrderedMenusFormat(params.orderedMenus), + generateTotalOrderAmountFormat(params.totalOrderAmount), + generateGiftMenuFormat(params.giftAmount), + generateDetailsFormat({ + tbA: params.totalBenefitAmount, + ...params.benefitAmounts, + }), + generateTotalBenefitAmountFormat(params.totalBenefitAmount), + generatePaymentAmountFormat(params.paymentAmount), + generateEventBadgeFormat(params.eventBadge), + ].join(SEPARATORS.RESULT_ITEM_SEOARATOR); +}; + +export const formatter = { + generateResultStartMessageFormat, + generateOrderedMenusFormat, + generateNumberWithCommasFormat, + generateTotalOrderAmountFormat, + generateGiftMenuFormat, + generateDetailFormat, + generateDetailsFormat, + generateTotalBenefitAmountFormat, + generatePaymentAmountFormat, + generateEventBadgeFormat, + generateResultFormat, +};
JavaScript
์—ฌ๊ธฐ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž ์˜๋ฏธ ์—†๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์—†์• ๋„ ๋˜์ง€ ์•Š์„๊นŒ์š”?? ``` if(benefitAmount) return ``${benefitTitle}: ${SIGNS.MINUS}${generateNumberWithCommasFormat( benefitAmount )}${UNITS.WON}`` ``` [์ฝ”๋“œ์ปจ๋ฒค์…˜](https://github.com/ParkSB/javascript-style-guide#comparison--unneeded-ternary)
@@ -0,0 +1,118 @@ +import { GUIDE_MESSAGES } from '../constants/messages.js'; +import { + RESULT_BENEFIT_TITLES, + RESULT_ITEM_TITLES, +} from '../constants/resultTitles.js'; +import { SEPARATORS } from '../constants/separators.js'; +import { UNITS } from '../constants/units.js'; +import { GIFT_MENUS } from '../constants/gifts.js'; +import { SIGNS } from '../constants/signs.js'; +import { BADGES } from '../constants/badges.js'; + +export const generateResultStartMessageFormat = (visitDate) => + `12${UNITS.MONTH} ${visitDate}${UNITS.DATE}์— ${GUIDE_MESSAGES.SHOW_RESULT}`; + +export const generateOrderedMenusFormat = (orderedMenus) => { + return `${RESULT_ITEM_TITLES.ORDERED_MENUS}${ + SEPARATORS.LINE_BREAK_SEPARATOR + }${Object.entries(orderedMenus) + .map((orderedMenu) => { + const [orderedMenuName, orderedMenuCount] = orderedMenu; + return `${orderedMenuName} ${orderedMenuCount}${UNITS.COUNT}`; + }) + .join(SEPARATORS.LINE_BREAK_SEPARATOR)}`; +}; + +export const generateNumberWithCommasFormat = (number) => + number.toLocaleString(); + +export const generateTotalOrderAmountFormat = (totalOrderAmount) => { + return `${RESULT_ITEM_TITLES.TOTAL_ORDER_AMOUNT}${ + SEPARATORS.LINE_BREAK_SEPARATOR + }${generateNumberWithCommasFormat(totalOrderAmount)}${UNITS.WON}`; +}; + +export const generateGiftMenuFormat = (giftAmount) => { + return `${RESULT_ITEM_TITLES.GIFT_MENU}${SEPARATORS.LINE_BREAK_SEPARATOR}${ + giftAmount + ? `${GIFT_MENUS.GIFT_MENU} 1${UNITS.COUNT}` + : GUIDE_MESSAGES.NOT_EXSIT_MATCHED_RESULT + }`; +}; + +export const generateDetailFormat = (benefitTitle, benefitAmount) => { + return benefitAmount + ? `${benefitTitle}: ${SIGNS.MINUS}${generateNumberWithCommasFormat( + benefitAmount + )}${UNITS.WON}` + : ''; +}; + +export const generateDetailsFormat = ({ tbA, cddA, wddA, wedA, sdA, gA }) => { + const benefitDetailsTitle = `${RESULT_ITEM_TITLES.BENEFIT_DETAILS}${SEPARATORS.LINE_BREAK_SEPARATOR}`; + if (tbA === 0) + return `${benefitDetailsTitle}${GUIDE_MESSAGES.NOT_EXSIT_MATCHED_RESULT}`; + const benefitDetails = []; + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.CDD, cddA)); + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.WDD, wddA)); + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.WED, wedA)); + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.SD, sdA)); + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.G, gA)); + return `${benefitDetailsTitle}${benefitDetails + .filter((benefitDetail) => benefitDetail.length) + .join(SEPARATORS.LINE_BREAK_SEPARATOR)}`; +}; + +export const generateTotalBenefitAmountFormat = (totalBenefitAmount) => { + return `${RESULT_ITEM_TITLES.TOTAL_BENEFIT_AMOUNT}${ + SEPARATORS.LINE_BREAK_SEPARATOR + }${ + totalBenefitAmount + ? `${SIGNS.MINUS}${generateNumberWithCommasFormat(totalBenefitAmount)}` + : 0 + }${UNITS.WON}`; +}; + +export const generatePaymentAmountFormat = (paymentAmount) => { + return `${RESULT_ITEM_TITLES.PAYMENT_AMOUNT}${ + SEPARATORS.LINE_BREAK_SEPARATOR + }${generateNumberWithCommasFormat(paymentAmount)}${UNITS.WON}`; +}; + +export const generateEventBadgeFormat = (eventBadge) => { + return `${RESULT_ITEM_TITLES.EVENT_BADGE}${SEPARATORS.LINE_BREAK_SEPARATOR}${ + eventBadge === BADGES.NOT_MATCHED_BADGE + ? GUIDE_MESSAGES.NOT_EXSIT_MATCHED_RESULT + : eventBadge + }`; +}; + +export const generateResultFormat = (params) => { + return [ + generateResultStartMessageFormat(params.visitDate), + generateOrderedMenusFormat(params.orderedMenus), + generateTotalOrderAmountFormat(params.totalOrderAmount), + generateGiftMenuFormat(params.giftAmount), + generateDetailsFormat({ + tbA: params.totalBenefitAmount, + ...params.benefitAmounts, + }), + generateTotalBenefitAmountFormat(params.totalBenefitAmount), + generatePaymentAmountFormat(params.paymentAmount), + generateEventBadgeFormat(params.eventBadge), + ].join(SEPARATORS.RESULT_ITEM_SEOARATOR); +}; + +export const formatter = { + generateResultStartMessageFormat, + generateOrderedMenusFormat, + generateNumberWithCommasFormat, + generateTotalOrderAmountFormat, + generateGiftMenuFormat, + generateDetailFormat, + generateDetailsFormat, + generateTotalBenefitAmountFormat, + generatePaymentAmountFormat, + generateEventBadgeFormat, + generateResultFormat, +};
JavaScript
์ œ๊ฐ€ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜๊ณ  ์žˆ๋Š” ๊ฑด์ง€ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ๐Ÿ˜… ์œ„์— ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์—†์• ๋ฉด return ๊ฐ’์ด undefined๊ฐ€ ๋ ํ…Œ๋‹ˆ๊นŒ ์ด๊ฒŒ ๋งŒ์•ฝ ๊ฐ’์ด ์žˆ๋Š” ๊ฒƒ๋งŒ ์ถ”์ถœํ•˜๋Š” ๊ฑฐ๋ผ๋ฉด ``` .filter(benefitDetail => benefitDetail) ``` ๋กœ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ์ดํ•ด๋ฅผ ์ž˜๋ชป ํ•˜๊ณ  ์žˆ๋Š” ๊ฑฐ๋ผ๋ฉด ์ง€์ ํ•ด์ฃผ์„ธ์š”๐Ÿ˜…
@@ -0,0 +1,6 @@ +export const UNITS = Object.freeze({ + MONTH: '์›”', + DATE: '์ผ', + COUNT: '๊ฐœ', + WON: '์›', +});
JavaScript
์™€-์šฐ ๋ชจ๋“  ์ƒ์ˆ˜๋ฅผ ํŒŒ์ผํ™” ํ•˜์…จ๋„ค์š”..!๐Ÿ‘
@@ -0,0 +1,17 @@ +export const RESULT_ITEM_TITLES = Object.freeze({ + ORDERED_MENUS: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + TOTAL_ORDER_AMOUNT: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + GIFT_MENU: '<์ฆ์ • ๋ฉ”๋‰ด>', + BENEFIT_DETAILS: '<ํ˜œํƒ ๋‚ด์—ญ>', + TOTAL_BENEFIT_AMOUNT: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + PAYMENT_AMOUNT: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + EVENT_BADGE: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', +}); + +export const RESULT_BENEFIT_TITLES = Object.freeze({ + CDD: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', + WDD: 'ํ‰์ผ ํ• ์ธ', + WED: '์ฃผ๋ง ํ• ์ธ', + SD: 'ํŠน๋ณ„ ํ• ์ธ', + G: '์ฆ์ • ์ด๋ฒคํŠธ', +});
JavaScript
์—ฌ๊ธฐ ์กฐ๊ธˆ์€ ํ’€์–ด ์“ฐ๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”! ์ €๋„ ๊ธด๊ฑฐ ์‹ซ์–ดํ•ด์„œ ๊ฐ€๋Šฅํ•œ ํ•œ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•˜๋ ค๊ณ ๋Š” ํ•˜๋Š”๋ฐ ๋„ˆ๋ฌด ์ค„์ด๋ฉด ์ดํ•ด๋ฅผ ๋ชปํ•˜๊ฒ ๋”๋ผ๊ตฌ์š”... ์•„๋ž˜์—์„œ ์ด๊ฒŒ ๋ญ์ง€? ํ•˜๊ณ  ์˜ฌ๋ผ์™”์–ด์š”ใ…Ž...
@@ -0,0 +1,17 @@ +export const RESULT_ITEM_TITLES = Object.freeze({ + ORDERED_MENUS: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + TOTAL_ORDER_AMOUNT: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + GIFT_MENU: '<์ฆ์ • ๋ฉ”๋‰ด>', + BENEFIT_DETAILS: '<ํ˜œํƒ ๋‚ด์—ญ>', + TOTAL_BENEFIT_AMOUNT: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + PAYMENT_AMOUNT: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + EVENT_BADGE: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', +}); + +export const RESULT_BENEFIT_TITLES = Object.freeze({ + CDD: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', + WDD: 'ํ‰์ผ ํ• ์ธ', + WED: '์ฃผ๋ง ํ• ์ธ', + SD: 'ํŠน๋ณ„ ํ• ์ธ', + G: '์ฆ์ • ์ด๋ฒคํŠธ', +});
JavaScript
์‚ฌ์‹ค ์›๋ž˜๋Š” ๊ธธ๊ฒŒ ์ผ์—ˆ๋Š”๋ฐ ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ 15์ค„์ด์ƒ ์“ฐ๋ฉด ์•ˆ๋œ๋‹ค๋Š” ์ œ์•ฝ๋•Œ๋ฌธ์— ์–ด์ฉ” ์ˆ˜ ์—†์ด ๋ณ€์ˆ˜๋ช…์„ ์ค„์ด๋‹ค๋ณด๋‹ˆ ์ด๋ ‡๊ฒŒ ๋๋„ค์š” ใ… ใ… 
@@ -0,0 +1,17 @@ +export const RESULT_ITEM_TITLES = Object.freeze({ + ORDERED_MENUS: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + TOTAL_ORDER_AMOUNT: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + GIFT_MENU: '<์ฆ์ • ๋ฉ”๋‰ด>', + BENEFIT_DETAILS: '<ํ˜œํƒ ๋‚ด์—ญ>', + TOTAL_BENEFIT_AMOUNT: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + PAYMENT_AMOUNT: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + EVENT_BADGE: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', +}); + +export const RESULT_BENEFIT_TITLES = Object.freeze({ + CDD: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', + WDD: 'ํ‰์ผ ํ• ์ธ', + WED: '์ฃผ๋ง ํ• ์ธ', + SD: 'ํŠน๋ณ„ ํ• ์ธ', + G: '์ฆ์ • ์ด๋ฒคํŠธ', +});
JavaScript
์•„ ํ”„๋ฆฌํ‹ฐ์–ด ๋•Œ๋ฌธ์— ๋„˜์–ด๊ฐ€์„œใ…‹ใ…‹ใ…‹ ์•Œ์•„์š” ์ €๋„ ๊ทธ๋ž˜์„œ ํ”„๋ฆฌํ‹ฐ์–ด ์„ค์ •์„ ๋ฐ”๊ฟ”๋ฒ„๋ ธ์–ด์š”...
@@ -0,0 +1,75 @@ +import InputView from '../views/InputView.js'; +import InputValidator from '../validators/InputValidator.js'; +import OutputView from '../views/OutputView.js'; +import { + removeWhiteSpaceFromBothEndsOfString, + getOrderedMenusObject, +} from '../utils/general.js'; +import OrderSheet from '../models/OrderSheet.js'; +import { generateResultFormat } from '../utils/formatting.js'; + +class EventPlannerController { + #inputView; + #outputView; + #orderSheet; + + constructor() { + this.#inputView = new InputView(); + this.#outputView = new OutputView(); + this.#orderSheet = new OrderSheet(); + } + + async start() { + this.printEventPlannerStart(); + await this.getVisitDate(); + await this.getOrderedMenus(); + this.printResult(); + } + + async getVisitDate() { + const inputValidator = new InputValidator(); + while (true) { + const visitDateInput = removeWhiteSpaceFromBothEndsOfString( + await this.#inputView.readVisitDate() + ); + try { + inputValidator.validateVisitDate(visitDateInput); + this.#orderSheet.setVisitDate(Number(visitDateInput)); + break; + } catch (error) { + this.#outputView.printErrorMessage(error.message); + } + } + } + + async getOrderedMenus() { + const inputValidator = new InputValidator(); + while (true) { + const orderedMenusInput = removeWhiteSpaceFromBothEndsOfString( + await this.#inputView.readMenus() + ); + try { + inputValidator.validateOrderedMenus(orderedMenusInput); + this.#orderSheet.setOrderedMenus( + getOrderedMenusObject(orderedMenusInput) + ); + break; + } catch (error) { + this.#outputView.printErrorMessage(error.message); + } + } + } + + printEventPlannerStart() { + this.#outputView.printStartMessage(); + } + + printResult() { + const resultOutputFormat = generateResultFormat({ + ...this.#orderSheet.getResult(), + }); + this.#outputView.printResult(resultOutputFormat); + } +} + +export default EventPlannerController;
JavaScript
์ „์ฒด ํ๋ฆ„์„ ํ•จ์ˆ˜ํ™”ํ•˜์—ฌ start์—์„œ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ž‘์„ฑํ•˜์—ฌ ์ฝ”๋“œ์˜ ์ „๋ฐ˜์ ์ธ ํ๋ฆ„์ด ํ•œ ๋ˆˆ์— ๋ณด์ด๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ๋Š” private์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋ฉด ๋” ์˜๋„๊ฐ€ ๋“œ๋Ÿฌ๋‚˜๊ณ  ์ฝ”๋“œ๋ฅผ ์ฝ์„ ๋•Œ ํ•œ๊ฒฐ ํŽธํ•  ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,75 @@ +import InputView from '../views/InputView.js'; +import InputValidator from '../validators/InputValidator.js'; +import OutputView from '../views/OutputView.js'; +import { + removeWhiteSpaceFromBothEndsOfString, + getOrderedMenusObject, +} from '../utils/general.js'; +import OrderSheet from '../models/OrderSheet.js'; +import { generateResultFormat } from '../utils/formatting.js'; + +class EventPlannerController { + #inputView; + #outputView; + #orderSheet; + + constructor() { + this.#inputView = new InputView(); + this.#outputView = new OutputView(); + this.#orderSheet = new OrderSheet(); + } + + async start() { + this.printEventPlannerStart(); + await this.getVisitDate(); + await this.getOrderedMenus(); + this.printResult(); + } + + async getVisitDate() { + const inputValidator = new InputValidator(); + while (true) { + const visitDateInput = removeWhiteSpaceFromBothEndsOfString( + await this.#inputView.readVisitDate() + ); + try { + inputValidator.validateVisitDate(visitDateInput); + this.#orderSheet.setVisitDate(Number(visitDateInput)); + break; + } catch (error) { + this.#outputView.printErrorMessage(error.message); + } + } + } + + async getOrderedMenus() { + const inputValidator = new InputValidator(); + while (true) { + const orderedMenusInput = removeWhiteSpaceFromBothEndsOfString( + await this.#inputView.readMenus() + ); + try { + inputValidator.validateOrderedMenus(orderedMenusInput); + this.#orderSheet.setOrderedMenus( + getOrderedMenusObject(orderedMenusInput) + ); + break; + } catch (error) { + this.#outputView.printErrorMessage(error.message); + } + } + } + + printEventPlannerStart() { + this.#outputView.printStartMessage(); + } + + printResult() { + const resultOutputFormat = generateResultFormat({ + ...this.#orderSheet.getResult(), + }); + this.#outputView.printResult(resultOutputFormat); + } +} + +export default EventPlannerController;
JavaScript
๋ฌดํ•œ๋ฃจํ”„๋ฅผ ํ†ตํ•ด์„œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ›„ ์žฌ์ž…๋ ฅ์„ ๋ฐ›๋Š” ์š”๊ตฌ์‚ฌํ•ญ์„ ํ•ด๊ฒฐํ•˜์‹  ๊ฒƒ ๊ฐ™๋„ค์š”! ๋ฌดํ•œ ๋ฃจํ”„๊ฐ€ ์œ„ํ—˜์„ฑ์ด ์กฐ๊ธˆ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ, ๋‹ค๋ฅธ ํ•ด๊ฒฐ๋ฐฉ์‹๋„ ๊ณ ๋ คํ•ด๋ณด์‹ ๊ฒŒ ์žˆ์œผ์‹ ๊ฐ€์š”?
@@ -0,0 +1,75 @@ +import InputView from '../views/InputView.js'; +import InputValidator from '../validators/InputValidator.js'; +import OutputView from '../views/OutputView.js'; +import { + removeWhiteSpaceFromBothEndsOfString, + getOrderedMenusObject, +} from '../utils/general.js'; +import OrderSheet from '../models/OrderSheet.js'; +import { generateResultFormat } from '../utils/formatting.js'; + +class EventPlannerController { + #inputView; + #outputView; + #orderSheet; + + constructor() { + this.#inputView = new InputView(); + this.#outputView = new OutputView(); + this.#orderSheet = new OrderSheet(); + } + + async start() { + this.printEventPlannerStart(); + await this.getVisitDate(); + await this.getOrderedMenus(); + this.printResult(); + } + + async getVisitDate() { + const inputValidator = new InputValidator(); + while (true) { + const visitDateInput = removeWhiteSpaceFromBothEndsOfString( + await this.#inputView.readVisitDate() + ); + try { + inputValidator.validateVisitDate(visitDateInput); + this.#orderSheet.setVisitDate(Number(visitDateInput)); + break; + } catch (error) { + this.#outputView.printErrorMessage(error.message); + } + } + } + + async getOrderedMenus() { + const inputValidator = new InputValidator(); + while (true) { + const orderedMenusInput = removeWhiteSpaceFromBothEndsOfString( + await this.#inputView.readMenus() + ); + try { + inputValidator.validateOrderedMenus(orderedMenusInput); + this.#orderSheet.setOrderedMenus( + getOrderedMenusObject(orderedMenusInput) + ); + break; + } catch (error) { + this.#outputView.printErrorMessage(error.message); + } + } + } + + printEventPlannerStart() { + this.#outputView.printStartMessage(); + } + + printResult() { + const resultOutputFormat = generateResultFormat({ + ...this.#orderSheet.getResult(), + }); + this.#outputView.printResult(resultOutputFormat); + } +} + +export default EventPlannerController;
JavaScript
15๋ผ์ธ์ด ๋„˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค...!
@@ -0,0 +1,118 @@ +import { GUIDE_MESSAGES } from '../constants/messages.js'; +import { + RESULT_BENEFIT_TITLES, + RESULT_ITEM_TITLES, +} from '../constants/resultTitles.js'; +import { SEPARATORS } from '../constants/separators.js'; +import { UNITS } from '../constants/units.js'; +import { GIFT_MENUS } from '../constants/gifts.js'; +import { SIGNS } from '../constants/signs.js'; +import { BADGES } from '../constants/badges.js'; + +export const generateResultStartMessageFormat = (visitDate) => + `12${UNITS.MONTH} ${visitDate}${UNITS.DATE}์— ${GUIDE_MESSAGES.SHOW_RESULT}`; + +export const generateOrderedMenusFormat = (orderedMenus) => { + return `${RESULT_ITEM_TITLES.ORDERED_MENUS}${ + SEPARATORS.LINE_BREAK_SEPARATOR + }${Object.entries(orderedMenus) + .map((orderedMenu) => { + const [orderedMenuName, orderedMenuCount] = orderedMenu; + return `${orderedMenuName} ${orderedMenuCount}${UNITS.COUNT}`; + }) + .join(SEPARATORS.LINE_BREAK_SEPARATOR)}`; +}; + +export const generateNumberWithCommasFormat = (number) => + number.toLocaleString(); + +export const generateTotalOrderAmountFormat = (totalOrderAmount) => { + return `${RESULT_ITEM_TITLES.TOTAL_ORDER_AMOUNT}${ + SEPARATORS.LINE_BREAK_SEPARATOR + }${generateNumberWithCommasFormat(totalOrderAmount)}${UNITS.WON}`; +}; + +export const generateGiftMenuFormat = (giftAmount) => { + return `${RESULT_ITEM_TITLES.GIFT_MENU}${SEPARATORS.LINE_BREAK_SEPARATOR}${ + giftAmount + ? `${GIFT_MENUS.GIFT_MENU} 1${UNITS.COUNT}` + : GUIDE_MESSAGES.NOT_EXSIT_MATCHED_RESULT + }`; +}; + +export const generateDetailFormat = (benefitTitle, benefitAmount) => { + return benefitAmount + ? `${benefitTitle}: ${SIGNS.MINUS}${generateNumberWithCommasFormat( + benefitAmount + )}${UNITS.WON}` + : ''; +}; + +export const generateDetailsFormat = ({ tbA, cddA, wddA, wedA, sdA, gA }) => { + const benefitDetailsTitle = `${RESULT_ITEM_TITLES.BENEFIT_DETAILS}${SEPARATORS.LINE_BREAK_SEPARATOR}`; + if (tbA === 0) + return `${benefitDetailsTitle}${GUIDE_MESSAGES.NOT_EXSIT_MATCHED_RESULT}`; + const benefitDetails = []; + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.CDD, cddA)); + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.WDD, wddA)); + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.WED, wedA)); + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.SD, sdA)); + benefitDetails.push(generateDetailFormat(RESULT_BENEFIT_TITLES.G, gA)); + return `${benefitDetailsTitle}${benefitDetails + .filter((benefitDetail) => benefitDetail.length) + .join(SEPARATORS.LINE_BREAK_SEPARATOR)}`; +}; + +export const generateTotalBenefitAmountFormat = (totalBenefitAmount) => { + return `${RESULT_ITEM_TITLES.TOTAL_BENEFIT_AMOUNT}${ + SEPARATORS.LINE_BREAK_SEPARATOR + }${ + totalBenefitAmount + ? `${SIGNS.MINUS}${generateNumberWithCommasFormat(totalBenefitAmount)}` + : 0 + }${UNITS.WON}`; +}; + +export const generatePaymentAmountFormat = (paymentAmount) => { + return `${RESULT_ITEM_TITLES.PAYMENT_AMOUNT}${ + SEPARATORS.LINE_BREAK_SEPARATOR + }${generateNumberWithCommasFormat(paymentAmount)}${UNITS.WON}`; +}; + +export const generateEventBadgeFormat = (eventBadge) => { + return `${RESULT_ITEM_TITLES.EVENT_BADGE}${SEPARATORS.LINE_BREAK_SEPARATOR}${ + eventBadge === BADGES.NOT_MATCHED_BADGE + ? GUIDE_MESSAGES.NOT_EXSIT_MATCHED_RESULT + : eventBadge + }`; +}; + +export const generateResultFormat = (params) => { + return [ + generateResultStartMessageFormat(params.visitDate), + generateOrderedMenusFormat(params.orderedMenus), + generateTotalOrderAmountFormat(params.totalOrderAmount), + generateGiftMenuFormat(params.giftAmount), + generateDetailsFormat({ + tbA: params.totalBenefitAmount, + ...params.benefitAmounts, + }), + generateTotalBenefitAmountFormat(params.totalBenefitAmount), + generatePaymentAmountFormat(params.paymentAmount), + generateEventBadgeFormat(params.eventBadge), + ].join(SEPARATORS.RESULT_ITEM_SEOARATOR); +}; + +export const formatter = { + generateResultStartMessageFormat, + generateOrderedMenusFormat, + generateNumberWithCommasFormat, + generateTotalOrderAmountFormat, + generateGiftMenuFormat, + generateDetailFormat, + generateDetailsFormat, + generateTotalBenefitAmountFormat, + generatePaymentAmountFormat, + generateEventBadgeFormat, + generateResultFormat, +};
JavaScript
๋ณต์žกํ•œ ๋ฌธ์ž์—ด ํฌ๋งคํŒ… ์ฒ˜๋ฆฌ๋ฅผ ์œ ํ‹ธ๋ฆฌํ‹ฐ๋กœ ๋นผ๋‚ด์–ด ๊ตฌํ˜„ํ•˜์…จ๊ตฐ์š”! ํ˜น์‹œ ๋ชจ๋“  ํ•จ์ˆ˜๊ฐ€ export๋˜์–ด ์žˆ๋Š”๋ฐ, ์ถ”๊ฐ€๋กœ exportํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ ๊ฐ€์š”?
@@ -0,0 +1,75 @@ +import InputView from '../views/InputView.js'; +import InputValidator from '../validators/InputValidator.js'; +import OutputView from '../views/OutputView.js'; +import { + removeWhiteSpaceFromBothEndsOfString, + getOrderedMenusObject, +} from '../utils/general.js'; +import OrderSheet from '../models/OrderSheet.js'; +import { generateResultFormat } from '../utils/formatting.js'; + +class EventPlannerController { + #inputView; + #outputView; + #orderSheet; + + constructor() { + this.#inputView = new InputView(); + this.#outputView = new OutputView(); + this.#orderSheet = new OrderSheet(); + } + + async start() { + this.printEventPlannerStart(); + await this.getVisitDate(); + await this.getOrderedMenus(); + this.printResult(); + } + + async getVisitDate() { + const inputValidator = new InputValidator(); + while (true) { + const visitDateInput = removeWhiteSpaceFromBothEndsOfString( + await this.#inputView.readVisitDate() + ); + try { + inputValidator.validateVisitDate(visitDateInput); + this.#orderSheet.setVisitDate(Number(visitDateInput)); + break; + } catch (error) { + this.#outputView.printErrorMessage(error.message); + } + } + } + + async getOrderedMenus() { + const inputValidator = new InputValidator(); + while (true) { + const orderedMenusInput = removeWhiteSpaceFromBothEndsOfString( + await this.#inputView.readMenus() + ); + try { + inputValidator.validateOrderedMenus(orderedMenusInput); + this.#orderSheet.setOrderedMenus( + getOrderedMenusObject(orderedMenusInput) + ); + break; + } catch (error) { + this.#outputView.printErrorMessage(error.message); + } + } + } + + printEventPlannerStart() { + this.#outputView.printStartMessage(); + } + + printResult() { + const resultOutputFormat = generateResultFormat({ + ...this.#orderSheet.getResult(), + }); + this.#outputView.printResult(resultOutputFormat); + } +} + +export default EventPlannerController;
JavaScript
```javascript const visitDateInput = await this.#inputView.readVisitDate(); ``` ํ•ด๋‹น ์œ„์น˜์—์„œ๋Š” ์ด๋Ÿฐ์‹์œผ๋กœ ์ž‘์„ฑํ•˜๊ณ , ์ž…๋ ฅ์„ ๋ฐ›๋Š” ๊ณณ์—์„œ removeWhiteSpaceFromBothEndsOfString ์œ ํ‹ธ๋ฆฌํ‹ฐ ์‚ฌ์šฉ, ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ํ›„ ๊ฐ€๊ณต๋œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค๋ฉด ์–ด๋• ์„๊นŒ์š”? ๊ฐœ์ธ์ ์œผ๋กœ ์ƒ์œ„ ๋กœ์ง์€ ํฐ ๋‹จ์œ„๋กœ ์ž‘์„ฑํ•˜๊ณ , ๋””ํ…Œ์ผํ•œ ์ฒ˜๋ฆฌ๋Š” ํ•˜์œ„ ๋กœ์ง์—์„œ ๋‹ด๋‹นํ•˜๋Š”๊ฒŒ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค! SungHyun627๋‹˜์€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,145 @@ +import { BADGES } from '../constants/badges.js'; +import { DISCOUNT_EVENTS_DATES } from '../constants/dates.js'; +import { DISCOUNT_UNITS, DISCOUNT_AMOUNTS } from '../constants/discounts.js'; +import { GIFT_MENUS, GIFT_AMOUNTS } from '../constants/gifts.js'; +import { MENU_PROPERTIES, MENU_LIST, MENU_TYPES } from '../constants/menus.js'; +import { getCountOfMenuType } from '../utils/general.js'; +import DiscountEventValidator from '../validators/DiscountEventValidator.js'; +import EventBadgeValidator from '../validators/EventBadgeValidator.js'; +import GiftEventValidator from '../validators/GiftEventValidator.js'; +class OrderSheet { + #visitDate; + #orderedMenus; + + setVisitDate(visitDate) { + this.#visitDate = visitDate; + } + + setOrderedMenus(orderedMenus) { + this.#orderedMenus = { ...orderedMenus }; + } + + getVisitDate() { + return this.#visitDate; + } + + getOrderedMenus() { + return { ...this.#orderedMenus }; + } + + getResult() { + return { + visitDate: this.getVisitDate(), + orderedMenus: this.getOrderedMenus(), + totalOrderAmount: this.getTotalOrderAmount(), + totalBenefitAmount: this.getTotalBenefitAmount(), + giftAmount: this.getGiftAmount(), + paymentAmount: this.getPaymentAmount(), + eventBadge: this.getEventBadge(), + benefitAmounts: { + cddA: this.getChristmasDdayDiscountAmount(), + wddA: this.getWeekDayDiscountAmount(), + wedA: this.getWeekendDiscountAmount(), + sdA: this.getSpecialDiscountAmount(), + gA: this.getGiftAmount(), + }, + }; + } + + getTotalOrderAmount() { + const menus = this.getOrderedMenus(); + const menuNames = Object.keys(menus); + return menuNames.reduce((acc, menuName) => { + const menuCount = menus[menuName]; + return acc + MENU_LIST[menuName][MENU_PROPERTIES.MENU_PRICE] * menuCount; + }, 0); + } + + getTotalDiscountAmount() { + const totalOrderAmount = this.getTotalOrderAmount(); + const discountEventValidator = new DiscountEventValidator(); + + return discountEventValidator.isDiscountEventApplicable(totalOrderAmount) + ? this.getChristmasDdayDiscountAmount() + + this.getWeekDayDiscountAmount() + + this.getWeekendDiscountAmount() + + this.getSpecialDiscountAmount() + : DISCOUNT_AMOUNTS.ZERO_DISCOUNT_AMOUNT; + } + + getGiftAmount() { + const totalOrderAmount = this.getTotalOrderAmount(); + const giftEventValidator = new GiftEventValidator(); + + return giftEventValidator.isGiftEventApplicable(totalOrderAmount) + ? MENU_LIST[GIFT_MENUS.GIFT_MENU][MENU_PROPERTIES.MENU_PRICE] + : GIFT_AMOUNTS.ZERO_GIFT_AMOUNT; + } + + getTotalBenefitAmount() { + return this.getTotalDiscountAmount() + this.getGiftAmount(); + } + + getPaymentAmount() { + return this.getTotalOrderAmount() - this.getTotalDiscountAmount(); + } + + getEventBadge() { + const totalBenefitAmount = this.getTotalBenefitAmount(); + const eventBadgeValidator = new EventBadgeValidator(); + if (eventBadgeValidator.isStarBadgeGettable(totalBenefitAmount)) + return BADGES.STAR; + if (eventBadgeValidator.isTreeBadgeGettable(totalBenefitAmount)) + return BADGES.TREE; + if (eventBadgeValidator.isSantaBadgeGettable(totalBenefitAmount)) + return BADGES.SANTA; + return BADGES.NOT_MATCHED_BADGE; + } + + getChristmasDdayDiscountAmount() { + const visitDate = this.getVisitDate(); + const discountEventValidator = new DiscountEventValidator(); + return discountEventValidator.isChristmasDdayDiscountApplicable(visitDate) + ? DISCOUNT_AMOUNTS.INITIAL_CHRISTMAS_DDAY_DISCOUNT_AMOUNT + + (visitDate - + DISCOUNT_EVENTS_DATES.CHRISTMAS_DDAY_DISCOUNT_START_DATE) * + DISCOUNT_UNITS.CHRISTMAS_DDAY_DISCOUNT_UNIT + : DISCOUNT_AMOUNTS.ZERO_DISCOUNT_AMOUNT; + } + + getWeekDayDiscountAmount() { + const visitDate = this.getVisitDate(); + const menus = this.getOrderedMenus(); + const countOfDessertMenus = getCountOfMenuType({ + menus, + menuType: MENU_TYPES.DESSERT, + }); + const discountEventValidator = new DiscountEventValidator(); + return discountEventValidator.isWeekDayDiscountApplicable(visitDate) + ? countOfDessertMenus * DISCOUNT_UNITS.WEEKDAY_DISCOUNT_UNIT + : 0; + } + + getWeekendDiscountAmount() { + const visitDate = this.getVisitDate(); + const menus = this.getOrderedMenus(); + const countOfMainDishMenus = getCountOfMenuType({ + menus, + menuType: MENU_TYPES.MAIN_DISH, + }); + const discountEventValidator = new DiscountEventValidator(); + return discountEventValidator.isWeekendDiscountApplicable(visitDate) + ? countOfMainDishMenus * DISCOUNT_UNITS.WEEKEND_DISCOUNT_UNIT + : 0; + } + + getSpecialDiscountAmount() { + const visitDate = this.getVisitDate(); + const discountEventValidator = new DiscountEventValidator(); + return discountEventValidator.isSpecialDiscountApplicable(visitDate) + ? DISCOUNT_AMOUNTS.SPECIAL_DISCOUNT_AMOUNT + : 0; + } +} + +export default OrderSheet;
JavaScript
`OrderSheet` ํด๋ž˜์Šค๋ฅผ ๋” ์„ธ๋ถ„ํ™”ํ•˜์—ฌ ์—ญํ• ์— ๋”ฐ๋ผ ๋ถ„๋ฆฌํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•˜๋Š”๊ฒŒ ์ข‹์„๊นŒ์š”?
@@ -0,0 +1,145 @@ +import { BADGES } from '../constants/badges.js'; +import { DISCOUNT_EVENTS_DATES } from '../constants/dates.js'; +import { DISCOUNT_UNITS, DISCOUNT_AMOUNTS } from '../constants/discounts.js'; +import { GIFT_MENUS, GIFT_AMOUNTS } from '../constants/gifts.js'; +import { MENU_PROPERTIES, MENU_LIST, MENU_TYPES } from '../constants/menus.js'; +import { getCountOfMenuType } from '../utils/general.js'; +import DiscountEventValidator from '../validators/DiscountEventValidator.js'; +import EventBadgeValidator from '../validators/EventBadgeValidator.js'; +import GiftEventValidator from '../validators/GiftEventValidator.js'; +class OrderSheet { + #visitDate; + #orderedMenus; + + setVisitDate(visitDate) { + this.#visitDate = visitDate; + } + + setOrderedMenus(orderedMenus) { + this.#orderedMenus = { ...orderedMenus }; + } + + getVisitDate() { + return this.#visitDate; + } + + getOrderedMenus() { + return { ...this.#orderedMenus }; + } + + getResult() { + return { + visitDate: this.getVisitDate(), + orderedMenus: this.getOrderedMenus(), + totalOrderAmount: this.getTotalOrderAmount(), + totalBenefitAmount: this.getTotalBenefitAmount(), + giftAmount: this.getGiftAmount(), + paymentAmount: this.getPaymentAmount(), + eventBadge: this.getEventBadge(), + benefitAmounts: { + cddA: this.getChristmasDdayDiscountAmount(), + wddA: this.getWeekDayDiscountAmount(), + wedA: this.getWeekendDiscountAmount(), + sdA: this.getSpecialDiscountAmount(), + gA: this.getGiftAmount(), + }, + }; + } + + getTotalOrderAmount() { + const menus = this.getOrderedMenus(); + const menuNames = Object.keys(menus); + return menuNames.reduce((acc, menuName) => { + const menuCount = menus[menuName]; + return acc + MENU_LIST[menuName][MENU_PROPERTIES.MENU_PRICE] * menuCount; + }, 0); + } + + getTotalDiscountAmount() { + const totalOrderAmount = this.getTotalOrderAmount(); + const discountEventValidator = new DiscountEventValidator(); + + return discountEventValidator.isDiscountEventApplicable(totalOrderAmount) + ? this.getChristmasDdayDiscountAmount() + + this.getWeekDayDiscountAmount() + + this.getWeekendDiscountAmount() + + this.getSpecialDiscountAmount() + : DISCOUNT_AMOUNTS.ZERO_DISCOUNT_AMOUNT; + } + + getGiftAmount() { + const totalOrderAmount = this.getTotalOrderAmount(); + const giftEventValidator = new GiftEventValidator(); + + return giftEventValidator.isGiftEventApplicable(totalOrderAmount) + ? MENU_LIST[GIFT_MENUS.GIFT_MENU][MENU_PROPERTIES.MENU_PRICE] + : GIFT_AMOUNTS.ZERO_GIFT_AMOUNT; + } + + getTotalBenefitAmount() { + return this.getTotalDiscountAmount() + this.getGiftAmount(); + } + + getPaymentAmount() { + return this.getTotalOrderAmount() - this.getTotalDiscountAmount(); + } + + getEventBadge() { + const totalBenefitAmount = this.getTotalBenefitAmount(); + const eventBadgeValidator = new EventBadgeValidator(); + if (eventBadgeValidator.isStarBadgeGettable(totalBenefitAmount)) + return BADGES.STAR; + if (eventBadgeValidator.isTreeBadgeGettable(totalBenefitAmount)) + return BADGES.TREE; + if (eventBadgeValidator.isSantaBadgeGettable(totalBenefitAmount)) + return BADGES.SANTA; + return BADGES.NOT_MATCHED_BADGE; + } + + getChristmasDdayDiscountAmount() { + const visitDate = this.getVisitDate(); + const discountEventValidator = new DiscountEventValidator(); + return discountEventValidator.isChristmasDdayDiscountApplicable(visitDate) + ? DISCOUNT_AMOUNTS.INITIAL_CHRISTMAS_DDAY_DISCOUNT_AMOUNT + + (visitDate - + DISCOUNT_EVENTS_DATES.CHRISTMAS_DDAY_DISCOUNT_START_DATE) * + DISCOUNT_UNITS.CHRISTMAS_DDAY_DISCOUNT_UNIT + : DISCOUNT_AMOUNTS.ZERO_DISCOUNT_AMOUNT; + } + + getWeekDayDiscountAmount() { + const visitDate = this.getVisitDate(); + const menus = this.getOrderedMenus(); + const countOfDessertMenus = getCountOfMenuType({ + menus, + menuType: MENU_TYPES.DESSERT, + }); + const discountEventValidator = new DiscountEventValidator(); + return discountEventValidator.isWeekDayDiscountApplicable(visitDate) + ? countOfDessertMenus * DISCOUNT_UNITS.WEEKDAY_DISCOUNT_UNIT + : 0; + } + + getWeekendDiscountAmount() { + const visitDate = this.getVisitDate(); + const menus = this.getOrderedMenus(); + const countOfMainDishMenus = getCountOfMenuType({ + menus, + menuType: MENU_TYPES.MAIN_DISH, + }); + const discountEventValidator = new DiscountEventValidator(); + return discountEventValidator.isWeekendDiscountApplicable(visitDate) + ? countOfMainDishMenus * DISCOUNT_UNITS.WEEKEND_DISCOUNT_UNIT + : 0; + } + + getSpecialDiscountAmount() { + const visitDate = this.getVisitDate(); + const discountEventValidator = new DiscountEventValidator(); + return discountEventValidator.isSpecialDiscountApplicable(visitDate) + ? DISCOUNT_AMOUNTS.SPECIAL_DISCOUNT_AMOUNT + : 0; + } +} + +export default OrderSheet;
JavaScript
์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•  method, ๋‚ด๋ถ€๋กœ์ง์—์„œ ์‚ฌ์šฉ๋˜๋Š” method๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ์ž‘์„ฑํ•˜๋ฉด ํด๋ž˜์Šค๋ฅผ ์ดํ•ดํ•˜๋Š”๊ฒŒ ์ข€ ๋” ์ˆ˜์›”ํ•  ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,75 @@ +import InputView from '../views/InputView.js'; +import InputValidator from '../validators/InputValidator.js'; +import OutputView from '../views/OutputView.js'; +import { + removeWhiteSpaceFromBothEndsOfString, + getOrderedMenusObject, +} from '../utils/general.js'; +import OrderSheet from '../models/OrderSheet.js'; +import { generateResultFormat } from '../utils/formatting.js'; + +class EventPlannerController { + #inputView; + #outputView; + #orderSheet; + + constructor() { + this.#inputView = new InputView(); + this.#outputView = new OutputView(); + this.#orderSheet = new OrderSheet(); + } + + async start() { + this.printEventPlannerStart(); + await this.getVisitDate(); + await this.getOrderedMenus(); + this.printResult(); + } + + async getVisitDate() { + const inputValidator = new InputValidator(); + while (true) { + const visitDateInput = removeWhiteSpaceFromBothEndsOfString( + await this.#inputView.readVisitDate() + ); + try { + inputValidator.validateVisitDate(visitDateInput); + this.#orderSheet.setVisitDate(Number(visitDateInput)); + break; + } catch (error) { + this.#outputView.printErrorMessage(error.message); + } + } + } + + async getOrderedMenus() { + const inputValidator = new InputValidator(); + while (true) { + const orderedMenusInput = removeWhiteSpaceFromBothEndsOfString( + await this.#inputView.readMenus() + ); + try { + inputValidator.validateOrderedMenus(orderedMenusInput); + this.#orderSheet.setOrderedMenus( + getOrderedMenusObject(orderedMenusInput) + ); + break; + } catch (error) { + this.#outputView.printErrorMessage(error.message); + } + } + } + + printEventPlannerStart() { + this.#outputView.printStartMessage(); + } + + printResult() { + const resultOutputFormat = generateResultFormat({ + ...this.#orderSheet.getResult(), + }); + this.#outputView.printResult(resultOutputFormat); + } +} + +export default EventPlannerController;
JavaScript
๊ฒฐ๊ณผ ๊ณ„์‚ฐ, ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ ํฌ๋งทํŒ…, ํฌ๋งทํŒ…ํ•œ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์—ญํ•  ๋ถ„๋ฆฌ๋ฅผ ์ž˜ํ•˜์…จ๋„ค์š”!
@@ -0,0 +1,15 @@ +export const DISCOUNT_UNITS = Object.freeze({ + CHRISTMAS_DDAY_DISCOUNT_UNIT: 100, + WEEKDAY_DISCOUNT_UNIT: 2023, + WEEKEND_DISCOUNT_UNIT: 2023, +}); + +export const DISCOUNT_AMOUNTS = Object.freeze({ + ZERO_DISCOUNT_AMOUNT: 0, + INITIAL_CHRISTMAS_DDAY_DISCOUNT_AMOUNT: 1000, + SPECIAL_DISCOUNT_AMOUNT: 1000, +}); + +export const DISCOUNT_EVENT_CRITERIAS = Object.freeze({ + MIN_TOTAL_ORDER_AMOUNT_FOR_DISCOUNT_EVENT: 10000, +});
JavaScript
```javascript export const DISCOUNT_UNIT = Object.freeze({ christmasDDay: 100, weekday: 2023, weekend: 2023, }); ``` airbnb convention์— ์˜ํ•˜๋ฉด, ๊ฐ์ฒด ์ƒ์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ key๋Š” camelCase๋กœ ํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค! ๋˜, discount unit์ด๋ผ๋Š” ์˜๋ฏธ๊ฐ€ ์ค‘๋ณต๋˜๊ธฐ ๋•Œ๋ฌธ์— ์—†์• ๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”! ํ•ด๋‹น ๊ฐ์ฒด๋Š” ์˜ˆ์‹œ๋ฅผ ์œ„ํ•ด ํŠน์ •ํ•œ ๊ฒƒ์ด๊ณ , ๋‹ค๋ฅธ ์ƒ์ˆ˜ ๊ฐ์ฒด๋“ค์—๋„ ๋™์ผํ•œ ๋ฆฌ๋ทฐ ๋“œ๋ฆฌ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,145 @@ +import { BADGES } from '../constants/badges.js'; +import { DISCOUNT_EVENTS_DATES } from '../constants/dates.js'; +import { DISCOUNT_UNITS, DISCOUNT_AMOUNTS } from '../constants/discounts.js'; +import { GIFT_MENUS, GIFT_AMOUNTS } from '../constants/gifts.js'; +import { MENU_PROPERTIES, MENU_LIST, MENU_TYPES } from '../constants/menus.js'; +import { getCountOfMenuType } from '../utils/general.js'; +import DiscountEventValidator from '../validators/DiscountEventValidator.js'; +import EventBadgeValidator from '../validators/EventBadgeValidator.js'; +import GiftEventValidator from '../validators/GiftEventValidator.js'; +class OrderSheet { + #visitDate; + #orderedMenus; + + setVisitDate(visitDate) { + this.#visitDate = visitDate; + } + + setOrderedMenus(orderedMenus) { + this.#orderedMenus = { ...orderedMenus }; + } + + getVisitDate() { + return this.#visitDate; + } + + getOrderedMenus() { + return { ...this.#orderedMenus }; + } + + getResult() { + return { + visitDate: this.getVisitDate(), + orderedMenus: this.getOrderedMenus(), + totalOrderAmount: this.getTotalOrderAmount(), + totalBenefitAmount: this.getTotalBenefitAmount(), + giftAmount: this.getGiftAmount(), + paymentAmount: this.getPaymentAmount(), + eventBadge: this.getEventBadge(), + benefitAmounts: { + cddA: this.getChristmasDdayDiscountAmount(), + wddA: this.getWeekDayDiscountAmount(), + wedA: this.getWeekendDiscountAmount(), + sdA: this.getSpecialDiscountAmount(), + gA: this.getGiftAmount(), + }, + }; + } + + getTotalOrderAmount() { + const menus = this.getOrderedMenus(); + const menuNames = Object.keys(menus); + return menuNames.reduce((acc, menuName) => { + const menuCount = menus[menuName]; + return acc + MENU_LIST[menuName][MENU_PROPERTIES.MENU_PRICE] * menuCount; + }, 0); + } + + getTotalDiscountAmount() { + const totalOrderAmount = this.getTotalOrderAmount(); + const discountEventValidator = new DiscountEventValidator(); + + return discountEventValidator.isDiscountEventApplicable(totalOrderAmount) + ? this.getChristmasDdayDiscountAmount() + + this.getWeekDayDiscountAmount() + + this.getWeekendDiscountAmount() + + this.getSpecialDiscountAmount() + : DISCOUNT_AMOUNTS.ZERO_DISCOUNT_AMOUNT; + } + + getGiftAmount() { + const totalOrderAmount = this.getTotalOrderAmount(); + const giftEventValidator = new GiftEventValidator(); + + return giftEventValidator.isGiftEventApplicable(totalOrderAmount) + ? MENU_LIST[GIFT_MENUS.GIFT_MENU][MENU_PROPERTIES.MENU_PRICE] + : GIFT_AMOUNTS.ZERO_GIFT_AMOUNT; + } + + getTotalBenefitAmount() { + return this.getTotalDiscountAmount() + this.getGiftAmount(); + } + + getPaymentAmount() { + return this.getTotalOrderAmount() - this.getTotalDiscountAmount(); + } + + getEventBadge() { + const totalBenefitAmount = this.getTotalBenefitAmount(); + const eventBadgeValidator = new EventBadgeValidator(); + if (eventBadgeValidator.isStarBadgeGettable(totalBenefitAmount)) + return BADGES.STAR; + if (eventBadgeValidator.isTreeBadgeGettable(totalBenefitAmount)) + return BADGES.TREE; + if (eventBadgeValidator.isSantaBadgeGettable(totalBenefitAmount)) + return BADGES.SANTA; + return BADGES.NOT_MATCHED_BADGE; + } + + getChristmasDdayDiscountAmount() { + const visitDate = this.getVisitDate(); + const discountEventValidator = new DiscountEventValidator(); + return discountEventValidator.isChristmasDdayDiscountApplicable(visitDate) + ? DISCOUNT_AMOUNTS.INITIAL_CHRISTMAS_DDAY_DISCOUNT_AMOUNT + + (visitDate - + DISCOUNT_EVENTS_DATES.CHRISTMAS_DDAY_DISCOUNT_START_DATE) * + DISCOUNT_UNITS.CHRISTMAS_DDAY_DISCOUNT_UNIT + : DISCOUNT_AMOUNTS.ZERO_DISCOUNT_AMOUNT; + } + + getWeekDayDiscountAmount() { + const visitDate = this.getVisitDate(); + const menus = this.getOrderedMenus(); + const countOfDessertMenus = getCountOfMenuType({ + menus, + menuType: MENU_TYPES.DESSERT, + }); + const discountEventValidator = new DiscountEventValidator(); + return discountEventValidator.isWeekDayDiscountApplicable(visitDate) + ? countOfDessertMenus * DISCOUNT_UNITS.WEEKDAY_DISCOUNT_UNIT + : 0; + } + + getWeekendDiscountAmount() { + const visitDate = this.getVisitDate(); + const menus = this.getOrderedMenus(); + const countOfMainDishMenus = getCountOfMenuType({ + menus, + menuType: MENU_TYPES.MAIN_DISH, + }); + const discountEventValidator = new DiscountEventValidator(); + return discountEventValidator.isWeekendDiscountApplicable(visitDate) + ? countOfMainDishMenus * DISCOUNT_UNITS.WEEKEND_DISCOUNT_UNIT + : 0; + } + + getSpecialDiscountAmount() { + const visitDate = this.getVisitDate(); + const discountEventValidator = new DiscountEventValidator(); + return discountEventValidator.isSpecialDiscountApplicable(visitDate) + ? DISCOUNT_AMOUNTS.SPECIAL_DISCOUNT_AMOUNT + : 0; + } +} + +export default OrderSheet;
JavaScript
ํ•ด๋‹น ๋กœ์ง๋„ ํ•จ์ˆ˜ํ™”ํ•˜์—ฌ ๋„ค์ด๋ฐ์„ ํ†ตํ•ด ์˜๋ฏธ๋ฅผ ๋ช…์‹œํ•ด์ค€๋‹ค๋ฉด ์–ด๋• ์„๊นŒ์š”?
@@ -0,0 +1,41 @@ +import domain.Point + +class AroundMineChecker(private val mineBoard: Map<Point, Square>, private val currentPoint: Point) { + + fun isFinish(): Boolean = mineBoard.getValue(currentPoint).isMine() + + fun getBoard(): Map<Point, Square> { + val checkLeft = checkLeft(currentPoint, mineBoard) + return checkRight(currentPoint, checkLeft) + } + + private fun checkLeft(currentPoint: Point, acc: Map<Point, Square> = emptyMap()): Map<Point, Square> { + val board = acc.toMutableMap() + + if (!board.containsKey(currentPoint)) return acc + if (board.getValue(currentPoint).isMine()) return acc + + val countMine = AroundMine(board, currentPoint).countMine() + board.replace(currentPoint, NonMine(countMine)) + + val diagonalUpLeft = checkLeft(currentPoint.diagonalUpLeft(), board) + val left = checkLeft(currentPoint.left(), diagonalUpLeft) + val diagonalBottomLeft = checkLeft(currentPoint.diagonalBottomLeft(), left) + return checkLeft(currentPoint.bottom(), diagonalBottomLeft) + } + + private fun checkRight(currentPoint: Point, acc: Map<Point, Square> = emptyMap()): Map<Point, Square> { + val board = acc.toMutableMap() + + if (!board.containsKey(currentPoint)) return acc + if (board.getValue(currentPoint).isMine()) return acc + + val countMine = AroundMine(board, currentPoint).countMine() + board.replace(currentPoint, NonMine(countMine)) + val up = checkRight(currentPoint.up(), board) + val diagonalUpRight = checkRight(currentPoint.diagonalUpRight(), up) + val diagonalBottomRight = checkRight(currentPoint.diagonalBottomRight(), diagonalUpRight) + return checkRight(currentPoint.right(), diagonalBottomRight) + } + +}
Kotlin
[๊ณต์‹ ์ปจ๋ฒค์…˜](https://kotlinlang.org/docs/coding-conventions.html#control-flow-statements)์— ๋”ฐ๋ฅด๋ฉด if๋ฌธ๊ณผ when๋ฌธ์€ 2์ค„ ์ด์ƒ์ด๋ฉด ์ค‘๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ํ˜„์žฌ ์ฝ”๋“œ๋Š” early return์„ ํ•˜๊ณ  ์žˆ์–ด์„œ ๊ตณ์ด else ๋ฌธ์„ ์“ธ ํ•„์š”๊ฐ€ ์—†์–ด๋ณด์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,41 @@ +import domain.Point + +class AroundMineChecker(private val mineBoard: Map<Point, Square>, private val currentPoint: Point) { + + fun isFinish(): Boolean = mineBoard.getValue(currentPoint).isMine() + + fun getBoard(): Map<Point, Square> { + val checkLeft = checkLeft(currentPoint, mineBoard) + return checkRight(currentPoint, checkLeft) + } + + private fun checkLeft(currentPoint: Point, acc: Map<Point, Square> = emptyMap()): Map<Point, Square> { + val board = acc.toMutableMap() + + if (!board.containsKey(currentPoint)) return acc + if (board.getValue(currentPoint).isMine()) return acc + + val countMine = AroundMine(board, currentPoint).countMine() + board.replace(currentPoint, NonMine(countMine)) + + val diagonalUpLeft = checkLeft(currentPoint.diagonalUpLeft(), board) + val left = checkLeft(currentPoint.left(), diagonalUpLeft) + val diagonalBottomLeft = checkLeft(currentPoint.diagonalBottomLeft(), left) + return checkLeft(currentPoint.bottom(), diagonalBottomLeft) + } + + private fun checkRight(currentPoint: Point, acc: Map<Point, Square> = emptyMap()): Map<Point, Square> { + val board = acc.toMutableMap() + + if (!board.containsKey(currentPoint)) return acc + if (board.getValue(currentPoint).isMine()) return acc + + val countMine = AroundMine(board, currentPoint).countMine() + board.replace(currentPoint, NonMine(countMine)) + val up = checkRight(currentPoint.up(), board) + val diagonalUpRight = checkRight(currentPoint.diagonalUpRight(), up) + val diagonalBottomRight = checkRight(currentPoint.diagonalBottomRight(), diagonalUpRight) + return checkRight(currentPoint.right(), diagonalBottomRight) + } + +}
Kotlin
์žฌ๊ท€์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด ์ข‹๋„ค์š”. ๋‹ค๋งŒ ๋ฐ˜ํ™˜์„ ํ•˜์ง€ ์•Š๊ณ  ๋‚ด๋ถ€์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค๋Š” ์ ์ด ์กฐ๊ธˆ ์•„์‰ฝ์Šต๋‹ˆ๋‹ค. ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š”๋Œ€์‹  ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋งŒ๋“ค๊ณ  ์ˆœ์ˆ˜ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? ์•„๋ž˜์™€ ๊ฐ™์€ ํ•จ์ˆ˜ ์„œ๋ช…์œผ๋กœ ๋งŒ๋“ค์–ด๋ณด๋ฉด ์ข‹๊ฒ ์–ด์š”. ```kotlin private fun checkLeft( currentPoint: Point, acc: Map<Point, Square> = emptyMap(), ): Map<Point, Square> { // TODO } ```
@@ -0,0 +1,41 @@ +import domain.Point + +class AroundMineChecker(private val mineBoard: Map<Point, Square>, private val currentPoint: Point) { + + fun isFinish(): Boolean = mineBoard.getValue(currentPoint).isMine() + + fun getBoard(): Map<Point, Square> { + val checkLeft = checkLeft(currentPoint, mineBoard) + return checkRight(currentPoint, checkLeft) + } + + private fun checkLeft(currentPoint: Point, acc: Map<Point, Square> = emptyMap()): Map<Point, Square> { + val board = acc.toMutableMap() + + if (!board.containsKey(currentPoint)) return acc + if (board.getValue(currentPoint).isMine()) return acc + + val countMine = AroundMine(board, currentPoint).countMine() + board.replace(currentPoint, NonMine(countMine)) + + val diagonalUpLeft = checkLeft(currentPoint.diagonalUpLeft(), board) + val left = checkLeft(currentPoint.left(), diagonalUpLeft) + val diagonalBottomLeft = checkLeft(currentPoint.diagonalBottomLeft(), left) + return checkLeft(currentPoint.bottom(), diagonalBottomLeft) + } + + private fun checkRight(currentPoint: Point, acc: Map<Point, Square> = emptyMap()): Map<Point, Square> { + val board = acc.toMutableMap() + + if (!board.containsKey(currentPoint)) return acc + if (board.getValue(currentPoint).isMine()) return acc + + val countMine = AroundMine(board, currentPoint).countMine() + board.replace(currentPoint, NonMine(countMine)) + val up = checkRight(currentPoint.up(), board) + val diagonalUpRight = checkRight(currentPoint.diagonalUpRight(), up) + val diagonalBottomRight = checkRight(currentPoint.diagonalBottomRight(), diagonalUpRight) + return checkRight(currentPoint.right(), diagonalBottomRight) + } + +}
Kotlin
๊ฐ ๋ฐฉํ–ฅ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋“ค ๊ฐ€๋…์„ฑ์ด ์ข‹๋„ค์š” ๐Ÿ‘
@@ -0,0 +1,41 @@ +import domain.Point + +class AroundMineChecker(private val mineBoard: Map<Point, Square>, private val currentPoint: Point) { + + fun isFinish(): Boolean = mineBoard.getValue(currentPoint).isMine() + + fun getBoard(): Map<Point, Square> { + val checkLeft = checkLeft(currentPoint, mineBoard) + return checkRight(currentPoint, checkLeft) + } + + private fun checkLeft(currentPoint: Point, acc: Map<Point, Square> = emptyMap()): Map<Point, Square> { + val board = acc.toMutableMap() + + if (!board.containsKey(currentPoint)) return acc + if (board.getValue(currentPoint).isMine()) return acc + + val countMine = AroundMine(board, currentPoint).countMine() + board.replace(currentPoint, NonMine(countMine)) + + val diagonalUpLeft = checkLeft(currentPoint.diagonalUpLeft(), board) + val left = checkLeft(currentPoint.left(), diagonalUpLeft) + val diagonalBottomLeft = checkLeft(currentPoint.diagonalBottomLeft(), left) + return checkLeft(currentPoint.bottom(), diagonalBottomLeft) + } + + private fun checkRight(currentPoint: Point, acc: Map<Point, Square> = emptyMap()): Map<Point, Square> { + val board = acc.toMutableMap() + + if (!board.containsKey(currentPoint)) return acc + if (board.getValue(currentPoint).isMine()) return acc + + val countMine = AroundMine(board, currentPoint).countMine() + board.replace(currentPoint, NonMine(countMine)) + val up = checkRight(currentPoint.up(), board) + val diagonalUpRight = checkRight(currentPoint.diagonalUpRight(), up) + val diagonalBottomRight = checkRight(currentPoint.diagonalBottomRight(), diagonalUpRight) + return checkRight(currentPoint.right(), diagonalBottomRight) + } + +}
Kotlin
์ด ํ•จ์ˆ˜๋“ค์„ Point ํด๋ž˜์Šค๋กœ ์˜ฎ๊ฒจ๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,45 @@ +* ๊ธฐ๋Šฅ ์š”๊ตฌ ์‚ฌํ•ญ + 1. ์ฃผ์–ด์ง„ ํšŸ์ˆ˜ ๋™์•ˆ n ๋Œ€์˜ ์ž๋™์ฐจ๋Š” ์ „์ง„ ๋˜๋Š” ๋ฉˆ์ถœ ์ˆ˜ ์žˆ๋‹ค. + 2. ์‚ฌ์šฉ์ž๋Š” ๋ช‡ ๋Œ€์˜ ์ž๋™์ฐจ๋กœ ๋ช‡ ๋ฒˆ์˜ ์ด๋™์„ ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. + 3. ์ „์ง„ํ•˜๋Š” ์กฐ๊ฑด์€ 0์—์„œ 9 ์‚ฌ์ด์—์„œ random ๊ฐ’์„ ๊ตฌํ•œ ํ›„ random ๊ฐ’์ด 4 ์ด์ƒ์ผ ๊ฒฝ์šฐ์ด๋‹ค. + 4. ์ž๋™์ฐจ์˜ ์ƒํƒœ๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•œ๋‹ค. ์–ด๋А ์‹œ์ ์— ์ถœ๋ ฅํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ์ œ์•ฝ์€ ์—†๋‹ค. + +* ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ ์‚ฌํ•ญ + 1. ๋ชจ๋“  ๋กœ์ง์— ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. ๋‹จ, UI(System.out, System.in) ๋กœ์ง์€ ์ œ์™ธ + 2. else ์˜ˆ์•ฝ์–ด๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. + +* ๊ธฐ๋Šฅ ๋ชฉ๋ก ๋ฐ commit ๋กœ๊ทธ ์š”๊ตฌ์‚ฌํ•ญ + git์˜ commit ๋‹จ์œ„๋Š” ์•ž ๋‹จ๊ณ„์—์„œ README.md ํŒŒ์ผ์— ์ •๋ฆฌํ•œ ๊ธฐ๋Šฅ ๋ชฉ๋ก ๋‹จ์œ„๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค. + +* ๊ธฐ๋Šฅ ๋ชฉ๋ก + 1. ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž๋™์ฐจ ๋Œ€์ˆ˜์™€ ์ด๋™ํšŸ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. + 2. 0์—์„œ 9 ์‚ฌ์ด์˜ ๋žœ๋ค ๊ฐ’์„ ๊ตฌํ•œ๋‹ค. + 3. ๋žœ๋ค ๊ฐ’์ด 4 ์ด์ƒ(4 <= n <= 9)์ผ ๋•Œ ์ „์ง„ํ•œ๋‹ค. ์ด ์™ธ์˜ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์›€์ง์ด์ง€ ์•Š๋Š”๋‹ค. + 4. ์ž๋™์ฐจ์˜ ์ƒํƒœ๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•œ๋‹ค. + +P.S +์•ˆ๋…•ํ•˜์„ธ์š” ์ด๋™๊ทœ ๋ฆฌ๋ทฐ์–ด๋‹˜ + +์Šคํ… 3 ๋ถ€ํ„ฐ ๋‚œ์ด๋„๊ฐ€ ๋งŽ์ด ์˜ฌ๋ผ๊ฐ”๋„ค์š”..ใ… ใ…  + +์š”๊ตฌ์‚ฌํ•ญ ๋Œ€๋กœ ๋งž๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋Š”์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค ใ… ใ…  +๋ถ€์กฑํ•œ ๋ถ€๋ถ„๋“ค์— ๋Œ€ํ•ด ๊ฐ€๊ฐ์—†์ด ํŒฉํŠธ ํญ๊ฒฉ ๋ถ€ํƒ๋“œ๋ฆด๊ฒŒ์š”..ใ… ใ…  + +๊ทธ๋ฆฌ๊ณ  ํ•œ ๊ฐ€์ง€ ์งˆ๋ฌธ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +ํ…Œ์ŠคํŠธ ์ง„ํ–‰ ์‹œ ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋งˆ๋‹ค ์‚ฐ์ถœ๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅผ ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งœ์•ผํ•˜๋‚˜์š”? +์˜ˆ๋ฅผ ๋“ค์–ด, ์ œ๊ฐ€ ๊ตฌํ˜„ํ•œ ํ…Œ์ŠคํŠธ ์†Œ์Šค์—์„œ moving() ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ + +๊ฐ๊ฐ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ ๋งˆ๋‹ค ๋ฌธ์ž์—ด ๊ฐ’์ด ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๋Š”๋ฐ ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ๋น„๊ตํ•˜๋ฉด ๋˜๋‚˜์š”? + +์ œ๊ฐ€ ์ƒ๊ฐํ•ด๋ณธ๊ฑด if ์กฐ๊ฑด๋ฌธ์„ ๊ฑธ์–ด์„œ => if (randomValueForMove == N) +๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ ๋งˆ๋‹ค ๋‚˜์™€์•ผ ํ•˜๋Š” ๊ฐ’์„ ์ผ์ผ์ด ๋น„๊ตํ•˜๋Š” ๊ฑด๋ฐ ๋„ˆ๋ฌด ๋ฒˆ๊ฑฐ๋กœ์šด ๊ฒƒ ๊ฐ™์•„์„œ์š”.. + +์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ์ฝ”๋ฉ˜ํŠธ ๋ถ€ํƒ๋“œ๋ฆด๊ฒŒ์š”! + +๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. + + + + +
Unknown
ํŒŒ๋ผ๋ฏธํ„ฐ ๋งˆ๋‹ค ๋‹ค๋ฅธ ๊ฐ’์ด ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๋Š” ParameterizedTest๋ฅผ ํ™œ์šฉํ•˜์‹œ๋ฉด ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค~
@@ -0,0 +1,113 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +public class RacingCar { + + static String move = "-"; + + static List<Car> recordMovingCar = new ArrayList<>(); + + public static void main(String[] args) { + + InputView userInput = scannerInput(); + + int numberOfCarsValue = userInput.getNumberOfCars(); + int numberOfAttemptsValue = userInput.getNumberOfAttempts(); + + System.out.println("======= READY ========="); + for (int carNumber=0; carNumber < numberOfCarsValue; carNumber++) { System.out.println(move); } + + System.out.println("======= START!! ======="); + randomMove(numberOfCarsValue, numberOfAttemptsValue); + + } + + public static void randomMove(int numberOfCarsValue, int numberOfAttemptsValue) { + Random random = new Random(); + + for (int attempt=0; attempt < numberOfAttemptsValue; attempt++) { + for (int eachCar = 0; eachCar < numberOfCarsValue; eachCar++) { + + int randomValueForMove = random.nextInt(numberOfAttemptsValue); + + String move = "-"; + Car car = new Car(); + car.setMoving(move); + recordMovingCar.add(car); + + String beforeMoving = ""; + +// System.out.println("๋žœ๋ค ๊ฐ’ : " + randomValueForMove); + + if (randomValueForMove > 9 || randomValueForMove < 4) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String doNotMove = beforeMoving + moving(randomValueForMove); + car.setMoving(doNotMove); + } + + + if (randomValueForMove > 0 & randomValueForMove > 3 & randomValueForMove <= 9) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String plusMoving = beforeMoving + moving(randomValueForMove); + car.setMoving(plusMoving); + } + + recordMovingCar.add(car); + recordMovingCar.get(eachCar).setMoving(car.getMoving()); + System.out.println(recordMovingCar.get(eachCar).getMoving()); + } + System.out.print(System.lineSeparator()); + } + } + + public static String moving(int randomValueForMove) { + String move = "-"; + + if (randomValueForMove > 9 || randomValueForMove < 4) { + move = ""; + return move; + } + + for (int number=0; number < randomValueForMove; number++) { move = move + "-";} + + return move; + } + + public static InputView scannerInput() { + + InputView inputView = new InputView(); + Scanner scanner = new Scanner(System.in); + + System.out.println("์ž๋™์ฐจ ๋Œ€์ˆ˜๋Š” ๋ช‡ ๋Œ€์ธ๊ฐ€์š”?"); + inputView.setNumberOfCars(scanner.nextInt()); + + System.out.println("์‹œ๋„ ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ์ธ๊ฐ€์š”?"); + inputView.setNumberOfAttempts(scanner.nextInt()); + + return inputView; + } + +} + + + + + + + + + + + + + + + + + + + + +
Java
์ฝ”๋“œ๋ฅผ ํ•œ์ค„๋กœ ์ž‘์„ฑํ•˜์‹œ๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์œผ์‹ ๋ฐ์š”. ๊ฐœํ–‰์„ ํ•˜๋Š” ์Šต๊ด€์„ ๋“ค์ด์‹œ๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค. ๋‹น์žฅ์€ ์ฝ”๋“œ ๋ผ์ธ ์ˆ˜๊ฐ€ ์ค„์–ด๋“ค์–ด ๊น”๋”ํ•œ ์ธ์ƒ์„ ์ค„ ์ˆ˜ ์žˆ์œผ๋‚˜, ํœด๋จผ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์–ด์š”. --- ๊ธฐ๋Šฅ ์š”๊ตฌ์‚ฌํ•ญ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ์™€ ๋‹ค๋ฅด๊ตฐ์š”~ --- code format์ด ์•ˆ๋งž๋Š”๋ฐ์š”. IntelliJ ๋‹จ์ถ•ํ‚ค๋Š” Ctrl+Alt+L(์œˆ๋„์šฐ), Cmd+Alt+L(๋งฅ) ์ž…๋‹ˆ๋‹ค~
@@ -0,0 +1,113 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +public class RacingCar { + + static String move = "-"; + + static List<Car> recordMovingCar = new ArrayList<>(); + + public static void main(String[] args) { + + InputView userInput = scannerInput(); + + int numberOfCarsValue = userInput.getNumberOfCars(); + int numberOfAttemptsValue = userInput.getNumberOfAttempts(); + + System.out.println("======= READY ========="); + for (int carNumber=0; carNumber < numberOfCarsValue; carNumber++) { System.out.println(move); } + + System.out.println("======= START!! ======="); + randomMove(numberOfCarsValue, numberOfAttemptsValue); + + } + + public static void randomMove(int numberOfCarsValue, int numberOfAttemptsValue) { + Random random = new Random(); + + for (int attempt=0; attempt < numberOfAttemptsValue; attempt++) { + for (int eachCar = 0; eachCar < numberOfCarsValue; eachCar++) { + + int randomValueForMove = random.nextInt(numberOfAttemptsValue); + + String move = "-"; + Car car = new Car(); + car.setMoving(move); + recordMovingCar.add(car); + + String beforeMoving = ""; + +// System.out.println("๋žœ๋ค ๊ฐ’ : " + randomValueForMove); + + if (randomValueForMove > 9 || randomValueForMove < 4) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String doNotMove = beforeMoving + moving(randomValueForMove); + car.setMoving(doNotMove); + } + + + if (randomValueForMove > 0 & randomValueForMove > 3 & randomValueForMove <= 9) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String plusMoving = beforeMoving + moving(randomValueForMove); + car.setMoving(plusMoving); + } + + recordMovingCar.add(car); + recordMovingCar.get(eachCar).setMoving(car.getMoving()); + System.out.println(recordMovingCar.get(eachCar).getMoving()); + } + System.out.print(System.lineSeparator()); + } + } + + public static String moving(int randomValueForMove) { + String move = "-"; + + if (randomValueForMove > 9 || randomValueForMove < 4) { + move = ""; + return move; + } + + for (int number=0; number < randomValueForMove; number++) { move = move + "-";} + + return move; + } + + public static InputView scannerInput() { + + InputView inputView = new InputView(); + Scanner scanner = new Scanner(System.in); + + System.out.println("์ž๋™์ฐจ ๋Œ€์ˆ˜๋Š” ๋ช‡ ๋Œ€์ธ๊ฐ€์š”?"); + inputView.setNumberOfCars(scanner.nextInt()); + + System.out.println("์‹œ๋„ ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ์ธ๊ฐ€์š”?"); + inputView.setNumberOfAttempts(scanner.nextInt()); + + return inputView; + } + +} + + + + + + + + + + + + + + + + + + + + +
Java
```suggestion int numberOfCars = userInput.getNumberOfCars(); int numberOfAttempts = userInput.getNumberOfAttempts(); ``` - ๋„ค์ด๋ฐ์‹œ์— ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๊ฑฐ๋‚˜, ์ค‘๋ณต๋˜๋Š” ๋ถ€๋ถ„์€ ์ตœ๋Œ€ํ•œ ์—†์• ๋ณด์„ธ์š”
@@ -0,0 +1,113 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +public class RacingCar { + + static String move = "-"; + + static List<Car> recordMovingCar = new ArrayList<>(); + + public static void main(String[] args) { + + InputView userInput = scannerInput(); + + int numberOfCarsValue = userInput.getNumberOfCars(); + int numberOfAttemptsValue = userInput.getNumberOfAttempts(); + + System.out.println("======= READY ========="); + for (int carNumber=0; carNumber < numberOfCarsValue; carNumber++) { System.out.println(move); } + + System.out.println("======= START!! ======="); + randomMove(numberOfCarsValue, numberOfAttemptsValue); + + } + + public static void randomMove(int numberOfCarsValue, int numberOfAttemptsValue) { + Random random = new Random(); + + for (int attempt=0; attempt < numberOfAttemptsValue; attempt++) { + for (int eachCar = 0; eachCar < numberOfCarsValue; eachCar++) { + + int randomValueForMove = random.nextInt(numberOfAttemptsValue); + + String move = "-"; + Car car = new Car(); + car.setMoving(move); + recordMovingCar.add(car); + + String beforeMoving = ""; + +// System.out.println("๋žœ๋ค ๊ฐ’ : " + randomValueForMove); + + if (randomValueForMove > 9 || randomValueForMove < 4) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String doNotMove = beforeMoving + moving(randomValueForMove); + car.setMoving(doNotMove); + } + + + if (randomValueForMove > 0 & randomValueForMove > 3 & randomValueForMove <= 9) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String plusMoving = beforeMoving + moving(randomValueForMove); + car.setMoving(plusMoving); + } + + recordMovingCar.add(car); + recordMovingCar.get(eachCar).setMoving(car.getMoving()); + System.out.println(recordMovingCar.get(eachCar).getMoving()); + } + System.out.print(System.lineSeparator()); + } + } + + public static String moving(int randomValueForMove) { + String move = "-"; + + if (randomValueForMove > 9 || randomValueForMove < 4) { + move = ""; + return move; + } + + for (int number=0; number < randomValueForMove; number++) { move = move + "-";} + + return move; + } + + public static InputView scannerInput() { + + InputView inputView = new InputView(); + Scanner scanner = new Scanner(System.in); + + System.out.println("์ž๋™์ฐจ ๋Œ€์ˆ˜๋Š” ๋ช‡ ๋Œ€์ธ๊ฐ€์š”?"); + inputView.setNumberOfCars(scanner.nextInt()); + + System.out.println("์‹œ๋„ ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ์ธ๊ฐ€์š”?"); + inputView.setNumberOfAttempts(scanner.nextInt()); + + return inputView; + } + +} + + + + + + + + + + + + + + + + + + + + +
Java
RacingCar๊ฐ€ Application์„ ๊ตฌ๋™ํ•˜๋Š” ์ฑ…์ž„์„ ๊ฐ€์ง€๋Š”๊ฑธ๋กœ ๋ณด์ด๋Š”๋ฐ์š”. ๊ทธ๋ž˜์„œ InputView๋ฅผ ์˜์กดํ•˜๊ณ  ์žˆ๊ณ , main ๋ฉ”์†Œ๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ๋„ค์š”. ๊ทธ๋Ÿฐ ๋งฅ๋ฝ์—์„œ randomMove ๋“ฑ์˜ ์—ญํ• ๋„ ๋ชจ๋‘ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์€ SRP (๋‹จ์ผ ์ฑ…์ž„์›์น™) ์œ„๋ฐ˜์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ๋‹จ์ผ์ฑ…์ž„์›์น™์€, ๊ฐ ํด๋ž˜์Šค๊ฐ€ ๋ณ€๊ฒฝ๋  ์ด์œ ๊ฐ€ ํ•˜๋‚˜๋ผ๋Š” ์˜๋ฏธ์ธ๋ฐ์š”. ํ˜„์žฌ๋Š” ์ž…๋ ฅ, ์ˆ˜ํ–‰, ์ถœ๋ ฅ ๋“ฑ์„ ์ œ์–ด, ์ž๋™์ฐจ ๊ฒŒ์ž„ ์ˆ˜ํ–‰, ์ž…๋ ฅ, ์ถœ๋ ฅ ๋“ฑ ๋ชจ๋“  ๋กœ์ง์ด RacingCar class์— ์กด์žฌํ•˜๋„ค์š”.
@@ -0,0 +1,113 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +public class RacingCar { + + static String move = "-"; + + static List<Car> recordMovingCar = new ArrayList<>(); + + public static void main(String[] args) { + + InputView userInput = scannerInput(); + + int numberOfCarsValue = userInput.getNumberOfCars(); + int numberOfAttemptsValue = userInput.getNumberOfAttempts(); + + System.out.println("======= READY ========="); + for (int carNumber=0; carNumber < numberOfCarsValue; carNumber++) { System.out.println(move); } + + System.out.println("======= START!! ======="); + randomMove(numberOfCarsValue, numberOfAttemptsValue); + + } + + public static void randomMove(int numberOfCarsValue, int numberOfAttemptsValue) { + Random random = new Random(); + + for (int attempt=0; attempt < numberOfAttemptsValue; attempt++) { + for (int eachCar = 0; eachCar < numberOfCarsValue; eachCar++) { + + int randomValueForMove = random.nextInt(numberOfAttemptsValue); + + String move = "-"; + Car car = new Car(); + car.setMoving(move); + recordMovingCar.add(car); + + String beforeMoving = ""; + +// System.out.println("๋žœ๋ค ๊ฐ’ : " + randomValueForMove); + + if (randomValueForMove > 9 || randomValueForMove < 4) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String doNotMove = beforeMoving + moving(randomValueForMove); + car.setMoving(doNotMove); + } + + + if (randomValueForMove > 0 & randomValueForMove > 3 & randomValueForMove <= 9) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String plusMoving = beforeMoving + moving(randomValueForMove); + car.setMoving(plusMoving); + } + + recordMovingCar.add(car); + recordMovingCar.get(eachCar).setMoving(car.getMoving()); + System.out.println(recordMovingCar.get(eachCar).getMoving()); + } + System.out.print(System.lineSeparator()); + } + } + + public static String moving(int randomValueForMove) { + String move = "-"; + + if (randomValueForMove > 9 || randomValueForMove < 4) { + move = ""; + return move; + } + + for (int number=0; number < randomValueForMove; number++) { move = move + "-";} + + return move; + } + + public static InputView scannerInput() { + + InputView inputView = new InputView(); + Scanner scanner = new Scanner(System.in); + + System.out.println("์ž๋™์ฐจ ๋Œ€์ˆ˜๋Š” ๋ช‡ ๋Œ€์ธ๊ฐ€์š”?"); + inputView.setNumberOfCars(scanner.nextInt()); + + System.out.println("์‹œ๋„ ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ์ธ๊ฐ€์š”?"); + inputView.setNumberOfAttempts(scanner.nextInt()); + + return inputView; + } + +} + + + + + + + + + + + + + + + + + + + + +
Java
https://edu.nextstep.camp/s/wLaV8qhA/lt/SuvyWqa2 ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋ฏธ์ค€์ˆ˜ํ•˜์…จ๋„ค์š”. ์•„๋ž˜ ๊ทœ์น™์„ ์ค€์ˆ˜ํ•˜๋ฉฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋‹ค๋ณด๋ฉด ๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„ ์—ฐ์Šต์ด ๋˜๋‹ˆ, ์ง€์ผœ์ฃผ์…”์š”~ ๊ทœ์น™ 1: ํ•œ ๋ฉ”์„œ๋“œ์— ์˜ค์ง ํ•œ ๋‹จ๊ณ„์˜ ๋“ค์—ฌ์“ฐ๊ธฐ๋งŒ ํ•œ๋‹ค. ๊ทœ์น™ 2: else ์˜ˆ์•ฝ์–ด๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. ๋ฉ”์†Œ๋“œ์˜ ๋ผ์ธ ์ˆ˜๋ฅผ 15๋ผ์ธ์ด ๋„˜์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. --- TDD ๋ฅผ ํ–ˆ์„ ๋•Œ์˜ ์žฅ์ ์ด ์—ฌ๋Ÿฟ ์žˆ์ง€๋งŒ, ๊ทธ ์ค‘ ํ•˜๋‚˜๋Š” ๊ธฐ๋Šฅ ๋‹จ์œ„๋กœ ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์ด๋ฅผ ๊ฒ€์ฆํ•˜๋ฉฐ ์ ์ง„์ ์œผ๋กœ ๊ฐœ๋ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด์—์š”. ์†Œํ”„ํŠธ์›จ์–ด๊ฐ€ ์ข€๋งŒ ๋ณต์žกํ•ด์ ธ๋„ ์šฐ๋ฆฌ๋Š” ํ•œ๋ฒˆ์— ๋ชจ๋“  ๋กœ์ง์„ ๋ฐ”๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†์–ด์š”. ๊ธฐ๋Šฅ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋ณด๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์€ ์š”๊ตฌ์‚ฌํ•ญ๋“ค์ด ์žˆ์ง€์š”. - ์ฃผ์–ด์ง„ ํšŸ์ˆ˜ ๋™์•ˆ n๋Œ€์˜ ์ž๋™์ฐจ๋Š” ์ „์ง„ ๋˜๋Š” ๋ฉˆ์ถœ ์ˆ˜ ์žˆ๋‹ค. - ์‚ฌ์šฉ์ž๋Š” ๋ช‡ ๋Œ€์˜ ์ž๋™์ฐจ๋กœ ๋ช‡ ๋ฒˆ์˜ ์ด๋™์„ ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. - ์ „์ง„ํ•˜๋Š” ์กฐ๊ฑด์€ 0์—์„œ 9 ์‚ฌ์ด์—์„œ random ๊ฐ’์„ ๊ตฌํ•œ ํ›„ random ๊ฐ’์ด 4์ด์ƒ์ผ ๊ฒฝ์šฐ์ด๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์€ ๋‹จ์œ„๋ถ€ํ„ฐ ์ฐจ๊ณก์ฐจ๊ณก ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด๊ฐˆ ์ˆ˜ ์žˆ์–ด์š”. - ์ž๋™์ฐจ๊ฐ€ ๋žœ๋ค ๊ฐ’์ด 4์ด์ƒ์ผ ๊ฒฝ์šฐ, ์ „์ง„ํ•œ๋‹ค. - ์ž๋™์ฐจ๊ฐ€ ๋žœ๋ค ๊ฐ’์ด 4๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ, ๋ฉˆ์ถ˜๋‹ค. - ์ฃผ์–ด์ง„ ํšŸ์ˆ˜ ๋™์•ˆ n๋Œ€์˜ ์ž๋™์ฐจ๊ฐ€ ๊ฒŒ์ž„์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ... ์œ„์™€ ๊ฐ™์ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ, ํ˜„์žฌ ์–ด๋–ค ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ๊ณ„์† ๋””๋ฒ„๊น…ํ•ด๊ฐ€๋ฉด์„œ ์ž‘์„ฑํ•  ์ˆ˜ ๋ฐ–์— ์—†๊ณ , ์ด์— ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ์„ค๊ณ„๊ฐ€ ์•„๋‹Œ ์ ˆ์ฐจ์ง€ํ–ฅ์ ์ธ ์‚ฌ๊ณ ๋ฅผ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ์ข€ ๋‚ฏ์„ค๋”๋ผ๋„ ํฌ๋น„๊ฐ€ ๊ฐ€์ด๋“œํ•˜๋Š”๋Œ€๋กœ ์ฒœ์ฒœํžˆ ๋ฏธ์…˜์„ ์ˆ˜ํ–‰ํ•ด๋ณด์‹œ๊ธธ ๋ฐ”๋ž˜์š”.
@@ -0,0 +1,113 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +public class RacingCar { + + static String move = "-"; + + static List<Car> recordMovingCar = new ArrayList<>(); + + public static void main(String[] args) { + + InputView userInput = scannerInput(); + + int numberOfCarsValue = userInput.getNumberOfCars(); + int numberOfAttemptsValue = userInput.getNumberOfAttempts(); + + System.out.println("======= READY ========="); + for (int carNumber=0; carNumber < numberOfCarsValue; carNumber++) { System.out.println(move); } + + System.out.println("======= START!! ======="); + randomMove(numberOfCarsValue, numberOfAttemptsValue); + + } + + public static void randomMove(int numberOfCarsValue, int numberOfAttemptsValue) { + Random random = new Random(); + + for (int attempt=0; attempt < numberOfAttemptsValue; attempt++) { + for (int eachCar = 0; eachCar < numberOfCarsValue; eachCar++) { + + int randomValueForMove = random.nextInt(numberOfAttemptsValue); + + String move = "-"; + Car car = new Car(); + car.setMoving(move); + recordMovingCar.add(car); + + String beforeMoving = ""; + +// System.out.println("๋žœ๋ค ๊ฐ’ : " + randomValueForMove); + + if (randomValueForMove > 9 || randomValueForMove < 4) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String doNotMove = beforeMoving + moving(randomValueForMove); + car.setMoving(doNotMove); + } + + + if (randomValueForMove > 0 & randomValueForMove > 3 & randomValueForMove <= 9) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String plusMoving = beforeMoving + moving(randomValueForMove); + car.setMoving(plusMoving); + } + + recordMovingCar.add(car); + recordMovingCar.get(eachCar).setMoving(car.getMoving()); + System.out.println(recordMovingCar.get(eachCar).getMoving()); + } + System.out.print(System.lineSeparator()); + } + } + + public static String moving(int randomValueForMove) { + String move = "-"; + + if (randomValueForMove > 9 || randomValueForMove < 4) { + move = ""; + return move; + } + + for (int number=0; number < randomValueForMove; number++) { move = move + "-";} + + return move; + } + + public static InputView scannerInput() { + + InputView inputView = new InputView(); + Scanner scanner = new Scanner(System.in); + + System.out.println("์ž๋™์ฐจ ๋Œ€์ˆ˜๋Š” ๋ช‡ ๋Œ€์ธ๊ฐ€์š”?"); + inputView.setNumberOfCars(scanner.nextInt()); + + System.out.println("์‹œ๋„ ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ์ธ๊ฐ€์š”?"); + inputView.setNumberOfAttempts(scanner.nextInt()); + + return inputView; + } + +} + + + + + + + + + + + + + + + + + + + + +
Java
๊ฐ’์„ ํ•˜๋“œ์ฝ”๋”ฉํ•˜๊ธฐ๋ณด๋‹ค๋Š” ์ƒ์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ์— ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ƒ์ˆ˜(static final)๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋ฉด ์ด ๋ณ€์ˆ˜์˜ ์—ญํ• ์ด ๋ฌด์—‡์ธ์ง€ ์˜๋„๋ฅผ ๋“œ๋Ÿฌ๋‚ด๊ธฐ์— ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,19 @@ +public class Car { + + private String moving; + + public Car () {} + + public Car(String moving) { + this.moving = moving; + } + + public String getMoving() { + return moving; + } + + public void setMoving(String moving) { + this.moving = moving; + } + +}
Java
๋‹จ์ˆœํžˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” dto๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” getter, setter๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ง๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•ด๋ณด์„ธ์š”. (์ ์–ด๋„ ์ด๋ฒˆ ๊ณผ์ •์—์„œ๋งŒ์ด๋ผ๋„์š”) ํ˜น์—ฌ getter๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋Š” ๋ชปํ•˜๊ฒ ๋‹ค ํ•˜๋”๋ผ๋„ setter ์—†์ด๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,113 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +public class RacingCar { + + static String move = "-"; + + static List<Car> recordMovingCar = new ArrayList<>(); + + public static void main(String[] args) { + + InputView userInput = scannerInput(); + + int numberOfCarsValue = userInput.getNumberOfCars(); + int numberOfAttemptsValue = userInput.getNumberOfAttempts(); + + System.out.println("======= READY ========="); + for (int carNumber=0; carNumber < numberOfCarsValue; carNumber++) { System.out.println(move); } + + System.out.println("======= START!! ======="); + randomMove(numberOfCarsValue, numberOfAttemptsValue); + + } + + public static void randomMove(int numberOfCarsValue, int numberOfAttemptsValue) { + Random random = new Random(); + + for (int attempt=0; attempt < numberOfAttemptsValue; attempt++) { + for (int eachCar = 0; eachCar < numberOfCarsValue; eachCar++) { + + int randomValueForMove = random.nextInt(numberOfAttemptsValue); + + String move = "-"; + Car car = new Car(); + car.setMoving(move); + recordMovingCar.add(car); + + String beforeMoving = ""; + +// System.out.println("๋žœ๋ค ๊ฐ’ : " + randomValueForMove); + + if (randomValueForMove > 9 || randomValueForMove < 4) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String doNotMove = beforeMoving + moving(randomValueForMove); + car.setMoving(doNotMove); + } + + + if (randomValueForMove > 0 & randomValueForMove > 3 & randomValueForMove <= 9) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String plusMoving = beforeMoving + moving(randomValueForMove); + car.setMoving(plusMoving); + } + + recordMovingCar.add(car); + recordMovingCar.get(eachCar).setMoving(car.getMoving()); + System.out.println(recordMovingCar.get(eachCar).getMoving()); + } + System.out.print(System.lineSeparator()); + } + } + + public static String moving(int randomValueForMove) { + String move = "-"; + + if (randomValueForMove > 9 || randomValueForMove < 4) { + move = ""; + return move; + } + + for (int number=0; number < randomValueForMove; number++) { move = move + "-";} + + return move; + } + + public static InputView scannerInput() { + + InputView inputView = new InputView(); + Scanner scanner = new Scanner(System.in); + + System.out.println("์ž๋™์ฐจ ๋Œ€์ˆ˜๋Š” ๋ช‡ ๋Œ€์ธ๊ฐ€์š”?"); + inputView.setNumberOfCars(scanner.nextInt()); + + System.out.println("์‹œ๋„ ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ์ธ๊ฐ€์š”?"); + inputView.setNumberOfAttempts(scanner.nextInt()); + + return inputView; + } + +} + + + + + + + + + + + + + + + + + + + + +
Java
static ๋ณ€์ˆ˜๋ฅผ ๊ณต์œ  ๊ฐ€๋ณ€ ํ•„๋“œ๋กœ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ง€์–‘ํ•˜์„ธ์š”. ํ˜„์žฌ๋Š” ๋กœ์ปฌ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋‹ˆ ๋ณ„๋‹ค๋ฅธ ๋ฌธ์ œ๋ฅผ ๋ชป๋А๋ผ์‹œ๊ฒ ์ง€๋งŒ, ๋‹ค์ˆ˜์˜ ์‚ฌ์šฉ์ž๊ฐ€ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, thread-safeํ•˜์ง€ ์•Š์•„ ๋™๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๋ณต์žกํ•œ ๋กœ์ง๋“ค์„ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋™์‹œ์„ฑ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ ๊ธฐ๋ณธ์ ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๊ณ„๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. - ์ƒํƒœ๋ณ€์ˆ˜๋ฅผ ์“ฐ๋ ˆ๋“œ๊ฐ„ ๊ณต์œ ํ•˜์ง€ ์•Š๋Š”๋‹ค. - ์ƒํƒœ ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋„๋ก ๋งŒ๋“ ๋‹ค. - [๋ถˆ๋ณ€๊ฐ์ฒด](https://brainbackdoor.tistory.com/141)
@@ -0,0 +1,113 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +public class RacingCar { + + static String move = "-"; + + static List<Car> recordMovingCar = new ArrayList<>(); + + public static void main(String[] args) { + + InputView userInput = scannerInput(); + + int numberOfCarsValue = userInput.getNumberOfCars(); + int numberOfAttemptsValue = userInput.getNumberOfAttempts(); + + System.out.println("======= READY ========="); + for (int carNumber=0; carNumber < numberOfCarsValue; carNumber++) { System.out.println(move); } + + System.out.println("======= START!! ======="); + randomMove(numberOfCarsValue, numberOfAttemptsValue); + + } + + public static void randomMove(int numberOfCarsValue, int numberOfAttemptsValue) { + Random random = new Random(); + + for (int attempt=0; attempt < numberOfAttemptsValue; attempt++) { + for (int eachCar = 0; eachCar < numberOfCarsValue; eachCar++) { + + int randomValueForMove = random.nextInt(numberOfAttemptsValue); + + String move = "-"; + Car car = new Car(); + car.setMoving(move); + recordMovingCar.add(car); + + String beforeMoving = ""; + +// System.out.println("๋žœ๋ค ๊ฐ’ : " + randomValueForMove); + + if (randomValueForMove > 9 || randomValueForMove < 4) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String doNotMove = beforeMoving + moving(randomValueForMove); + car.setMoving(doNotMove); + } + + + if (randomValueForMove > 0 & randomValueForMove > 3 & randomValueForMove <= 9) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String plusMoving = beforeMoving + moving(randomValueForMove); + car.setMoving(plusMoving); + } + + recordMovingCar.add(car); + recordMovingCar.get(eachCar).setMoving(car.getMoving()); + System.out.println(recordMovingCar.get(eachCar).getMoving()); + } + System.out.print(System.lineSeparator()); + } + } + + public static String moving(int randomValueForMove) { + String move = "-"; + + if (randomValueForMove > 9 || randomValueForMove < 4) { + move = ""; + return move; + } + + for (int number=0; number < randomValueForMove; number++) { move = move + "-";} + + return move; + } + + public static InputView scannerInput() { + + InputView inputView = new InputView(); + Scanner scanner = new Scanner(System.in); + + System.out.println("์ž๋™์ฐจ ๋Œ€์ˆ˜๋Š” ๋ช‡ ๋Œ€์ธ๊ฐ€์š”?"); + inputView.setNumberOfCars(scanner.nextInt()); + + System.out.println("์‹œ๋„ ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ์ธ๊ฐ€์š”?"); + inputView.setNumberOfAttempts(scanner.nextInt()); + + return inputView; + } + +} + + + + + + + + + + + + + + + + + + + + +
Java
์ด ๋กœ์ง์€ InputView์— ์—ญํ• ์„ ์œ„์ž„ํ•˜๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”
@@ -0,0 +1,113 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +public class RacingCar { + + static String move = "-"; + + static List<Car> recordMovingCar = new ArrayList<>(); + + public static void main(String[] args) { + + InputView userInput = scannerInput(); + + int numberOfCarsValue = userInput.getNumberOfCars(); + int numberOfAttemptsValue = userInput.getNumberOfAttempts(); + + System.out.println("======= READY ========="); + for (int carNumber=0; carNumber < numberOfCarsValue; carNumber++) { System.out.println(move); } + + System.out.println("======= START!! ======="); + randomMove(numberOfCarsValue, numberOfAttemptsValue); + + } + + public static void randomMove(int numberOfCarsValue, int numberOfAttemptsValue) { + Random random = new Random(); + + for (int attempt=0; attempt < numberOfAttemptsValue; attempt++) { + for (int eachCar = 0; eachCar < numberOfCarsValue; eachCar++) { + + int randomValueForMove = random.nextInt(numberOfAttemptsValue); + + String move = "-"; + Car car = new Car(); + car.setMoving(move); + recordMovingCar.add(car); + + String beforeMoving = ""; + +// System.out.println("๋žœ๋ค ๊ฐ’ : " + randomValueForMove); + + if (randomValueForMove > 9 || randomValueForMove < 4) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String doNotMove = beforeMoving + moving(randomValueForMove); + car.setMoving(doNotMove); + } + + + if (randomValueForMove > 0 & randomValueForMove > 3 & randomValueForMove <= 9) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String plusMoving = beforeMoving + moving(randomValueForMove); + car.setMoving(plusMoving); + } + + recordMovingCar.add(car); + recordMovingCar.get(eachCar).setMoving(car.getMoving()); + System.out.println(recordMovingCar.get(eachCar).getMoving()); + } + System.out.print(System.lineSeparator()); + } + } + + public static String moving(int randomValueForMove) { + String move = "-"; + + if (randomValueForMove > 9 || randomValueForMove < 4) { + move = ""; + return move; + } + + for (int number=0; number < randomValueForMove; number++) { move = move + "-";} + + return move; + } + + public static InputView scannerInput() { + + InputView inputView = new InputView(); + Scanner scanner = new Scanner(System.in); + + System.out.println("์ž๋™์ฐจ ๋Œ€์ˆ˜๋Š” ๋ช‡ ๋Œ€์ธ๊ฐ€์š”?"); + inputView.setNumberOfCars(scanner.nextInt()); + + System.out.println("์‹œ๋„ ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ์ธ๊ฐ€์š”?"); + inputView.setNumberOfAttempts(scanner.nextInt()); + + return inputView; + } + +} + + + + + + + + + + + + + + + + + + + + +
Java
```suggestion public static void main(String[] args) { int numberOfCars = InputView.inputNumberOfCars(); int numberOfAttempts = InputView.inputNumberOfAttempts(); RacingGame racingGame = new RacingGame(numberOfCars); GameResult result = racingGame.play(numberOfAttempts); ResultView.draw(result); } ``` - ํ•œ๋ฒˆ ์œ„์™€ ๊ฐ™์ด main ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์ง„ํ–‰ํ•ด๋ณด์‹œ๊ฒ ์–ด์š”? (์—†๋Š” ๊ฐ์ฒด๋“ค์€ ์ถ”๊ฐ€ํ•ด์„œ ์ง„ํ–‰ํ•ด๋ณด์„ธ์š”.)
@@ -0,0 +1,77 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import static org.assertj.core.api.Assertions.*; + +public class RacingCarTest { + + List<Car> recordMovingCar = new ArrayList<>(); + + @ParameterizedTest + @ValueSource(ints = {1, 2, 3, 4, 5}) + @DisplayName("์ž…๋ ฅํ•œ ํšŸ์ˆ˜ ๋งŒํผ์˜ '-' ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ") + public String moving(int randomValueForMove) { + String move = "-"; + + if (randomValueForMove > 9 || randomValueForMove < 4) { + move = ""; + return move; + } + + for (int number=0; number < randomValueForMove; number++) { move = move + "-";} + + return move; + } + + @ParameterizedTest + @CsvSource({ + "1, 2", + "5, 7", + "3, 5", + "2, 7" + }) + @DisplayName("์ž…๋ ฅํ•œ ์ž๋™์ฐจ ๋Œ€์ˆ˜ ๋ฐ ์‹œ๋„ ํšŸ์ˆ˜์— ๋งž๊ฒŒ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ ๋˜๋Š”๊ฐ€") + public void randomNumberForMove(int numberOfCarsValue, int numberOfAttemptsValue) { + Random random = new Random(); + + for (int attempt=0; attempt < numberOfAttemptsValue; attempt++) { + for (int eachCar = 0; eachCar < numberOfCarsValue; eachCar++) { + int carIndex = eachCar + 1; + int randomValueForMove = random.nextInt(numberOfAttemptsValue); + + String move = "-"; + Car car = new Car(); + car.setMoving(move); + recordMovingCar.add(car); + + String beforeMoving = ""; + +// System.out.println("๋žœ๋ค ๊ฐ’ : " + randomValueForMove); + + if (randomValueForMove > 9 || randomValueForMove < 4) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String doNotMove = beforeMoving + moving(randomValueForMove); + car.setMoving(doNotMove); + } + + if (randomValueForMove > 0 & randomValueForMove > 3 & randomValueForMove <= 9) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String plusMoving = beforeMoving + moving(randomValueForMove); + car.setMoving(plusMoving); + } + + recordMovingCar.add(car); + recordMovingCar.get(eachCar).setMoving(car.getMoving()); + System.out.println(recordMovingCar.get(eachCar).getMoving()); + } + System.out.print(System.lineSeparator()); + } + } +}
Java
- ํ…Œ์ŠคํŠธ์ฝ”๋“œ์— if ์กฐ๊ฑด ๋ถ„๊ธฐ๋ฅผ ์ž‘์„ฑํ•  ์ผ์€ ์—†์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธํ•˜๊ณ ์ž ํ•˜๋Š” ๋Œ€์ƒ์— ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ธ ํ›„ ์‘๋‹ต๋ฐ›์€ ๊ฐ’์ด ์˜๋„ํ•œ ๊ฒฐ๊ณผ์™€ ๊ฐ™์€์ง€๋งŒ ๊ฒ€์ฆํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. - ๋‹จ์œ„ ํ…Œ์ŠคํŠธ์— return ๋ฌธ์„ ์ž‘์„ฑํ•œ ํ›„ ์ด๋ฅผ ํ™œ์šฉํ•˜๋Š” ํ˜•ํƒœ๋Š” ์ง€์–‘ํ•ฉ๋‹ˆ๋‹ค. ํ•„์š”ํ•˜๋‹ค๋ฉด ๋ณ„๋„์˜ Test Fixture๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ํ™œ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,77 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import static org.assertj.core.api.Assertions.*; + +public class RacingCarTest { + + List<Car> recordMovingCar = new ArrayList<>(); + + @ParameterizedTest + @ValueSource(ints = {1, 2, 3, 4, 5}) + @DisplayName("์ž…๋ ฅํ•œ ํšŸ์ˆ˜ ๋งŒํผ์˜ '-' ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ") + public String moving(int randomValueForMove) { + String move = "-"; + + if (randomValueForMove > 9 || randomValueForMove < 4) { + move = ""; + return move; + } + + for (int number=0; number < randomValueForMove; number++) { move = move + "-";} + + return move; + } + + @ParameterizedTest + @CsvSource({ + "1, 2", + "5, 7", + "3, 5", + "2, 7" + }) + @DisplayName("์ž…๋ ฅํ•œ ์ž๋™์ฐจ ๋Œ€์ˆ˜ ๋ฐ ์‹œ๋„ ํšŸ์ˆ˜์— ๋งž๊ฒŒ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ ๋˜๋Š”๊ฐ€") + public void randomNumberForMove(int numberOfCarsValue, int numberOfAttemptsValue) { + Random random = new Random(); + + for (int attempt=0; attempt < numberOfAttemptsValue; attempt++) { + for (int eachCar = 0; eachCar < numberOfCarsValue; eachCar++) { + int carIndex = eachCar + 1; + int randomValueForMove = random.nextInt(numberOfAttemptsValue); + + String move = "-"; + Car car = new Car(); + car.setMoving(move); + recordMovingCar.add(car); + + String beforeMoving = ""; + +// System.out.println("๋žœ๋ค ๊ฐ’ : " + randomValueForMove); + + if (randomValueForMove > 9 || randomValueForMove < 4) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String doNotMove = beforeMoving + moving(randomValueForMove); + car.setMoving(doNotMove); + } + + if (randomValueForMove > 0 & randomValueForMove > 3 & randomValueForMove <= 9) { + beforeMoving = recordMovingCar.get(eachCar).getMoving(); + String plusMoving = beforeMoving + moving(randomValueForMove); + car.setMoving(plusMoving); + } + + recordMovingCar.add(car); + recordMovingCar.get(eachCar).setMoving(car.getMoving()); + System.out.println(recordMovingCar.get(eachCar).getMoving()); + } + System.out.print(System.lineSeparator()); + } + } +}
Java
- ์œ„์— ์–ธ๊ธ‰ํ–ˆ๋“ฏ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์— ๋„๋ฉ”์ธ ๋กœ์ง์„ ์ž‘์„ฑํ•ด์„  ์•ˆ๋ฉ๋‹ˆ๋‹ค. - ๋‹จ์œ„ํ…Œ์ŠคํŠธ๋Š” assertion์œผ๋กœ๋งŒ ๊ฒ€์ฆํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,330 @@ +body { + font-family: '-apple-system', 'BlinkMacSystemFont', 'Segoe UI', 'Roboto', 'Helvetica Neue', + 'Arial', 'Noto Sans', 'sans-serif', 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', + 'Noto Color Emoji'; + font-size: 18px; + color: #212124; +} + +/* components > header */ +.header { + position: fixed; + top: 0; + left: 0; + display: flex; + width: 100%; + height: 68px; + justify-content: center; + background-color: white; + z-index: 10; +} + +.nav__box { + display: flex; + padding: 0 1.5rem; + width: 100%; + max-width: 1200px; + align-items: center; + justify-content: space-between; +} + +.nav__list { + display: flex; + gap: 56px; +} + +.nav__link { + text-decoration: none; + font-size: 16px; + font-weight: 600; + line-height: 0; +} + +.nav__link:hover { + color: #868b94; +} + +/* components > footer */ +.footer__wrapper { + display: flex; + width: 100%; + justify-content: center; + align-items: center; + margin-top: 160px; + border-top: solid 1px #f2f3f6; + padding: 70px 0px 96px 0px; +} + +.footer__container { + display: flex; + flex-direction: column; + gap: 36px; + padding: 0px 24px; +} + +.footer__nav { + max-width: 1200px; + display: flex; + /* ์ž„์˜ ์„ค์ • */ + gap: 500px; + align-items: center; + justify-content: space-between; +} + +.footer__nav > ul { + padding: 0px; + display: flex; + margin: 0px; +} + +.footer__nav__link { + font-size: 13px; + font-weight: 400; + color: #212124; + cursor: pointer; + margin-left: 48px; +} + +.footer__nav__link:first-child { + margin-left: 0px; +} + +.footer__nav__link:hover { + color: #868b94; + filter: invert(57%) sepia(4%) saturate(791%) hue-rotate(180deg) brightness(97%) contrast(77%); +} + +.footer__nav__link > a { + display: inline-flex; + align-items: center; + opacity: 1; +} + +.footer__nav__link > a > h4 { + font-size: 13px; + font-weight: 600; +} + +.footer__nav__link > a > img { + width: 16px; + height: 16px; +} + +.sns__box { + display: flex; + gap: 24px; + padding: 0px; +} + +.sns__link { + width: 24px; + height: 24px; + cursor: pointer; +} + +.sns__link:hover { + filter: invert(57%) sepia(4%) saturate(791%) hue-rotate(180deg) brightness(97%) contrast(77%); +} + +.footer__information { + font-size: 14px; + color: #868b94; + font-weight: 400; +} + +.footer__information__box { + display: flex; +} + +.footer__information__address { + margin-right: 8px; +} + +.footer__contact { + text-decoration: underline; +} + +.corporate__name { + font-size: 13px; + color: #868b94; + font-weight: 600; + margin-top: 16px; +} + +/* Blog */ +.blog__wrapper { + margin: 40px 120px 0px 120px; + padding: 0 24px; + display: flex; + flex-direction: column; + justify-content: center; +} + +.tag__box { + display: flex; + gap: 10px; + margin-bottom: 44px; + margin: 0px 48px; +} + +.card__box { + display: grid; + grid-template-columns: repeat(2, 1fr); + row-gap: 3rem; + margin-top: 44px; +} + +.card__box > a { + display: flex; + justify-items: center; + justify-content: center; +} + +/* Blog > MainCard */ +.maincard__wrapper { + display: flex; + flex-direction: column; + margin-bottom: 100px; + cursor: pointer; + margin: 0px 48px 100px 48px; +} + +.maincard__thumbnail { + border-radius: 40px; + max-width: 1200px; + aspect-ratio: 1160 / 650; +} + +.maincard__body { + display: flex; + flex-direction: column; + align-items: center; + margin-top: 44px; +} + +.maincard__body > h1 { + line-height: 39px; + font-size: 32px; + margin-bottom: 2px; + font-weight: 600; +} + +.maincard__body > p { + line-height: 28px; + font-size: 18px; + color: #868b94; +} + +/* Blog > Tag */ +.tag__wrapper { + /* display: flex; */ + border-radius: 40px; + line-height: 14px; + border: none; + padding: 14px 20px; + font-size: 18px; + color: #868b94; + background-color: #f2f3f6; + cursor: pointer; +} + +.tag__wrapper:hover { + background-color: #212124; + color: #ffffff; + font-weight: 600; +} + +/* Blog > Card */ +.card__wrapper { + width: 100%; + display: flex; + flex-direction: column; + cursor: pointer; + margin: 0 48px; +} + +.card__wrapper:hover { + transform: translateY(-8px); + transition: all 0.3s ease-in-out; +} + +.card__wrapper > img { + border-radius: 20px; +} + +.card__body > h3 { + font-size: 22px; + margin-top: 20px; + font-weight: 600; +} + +.card__body > h3:hover { + color: #4d5159; +} + +.card__body > p { + font-size: 16px; + margin-top: 12px; + color: #868b94; + white-space: pre-line; +} + +.card__samlltagbox { + display: flex; +} + +.card__smalltag { + font-size: 13px; + height: 30px; + color: #868b94; + background-color: #f2f3f6; + border-radius: 40px; + padding: 4px 10px; + margin-top: 24px; +} + +/* Post */ +.post__wrapper { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + margin: 40px 120px 0px 120px; + padding: 0px 24px; +} + +.post_article { + margin: 0px 126px; +} + +.post__title { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + margin-bottom: 66px; +} + +.post__title > h1 { + font-size: 42px; + margin-bottom: 12px; +} + +.post__title > div { + font-size: 16px; + color: #868b94; +} + +.post__image { + padding: 0 24px; +} + +.post__image > img { + object-fit: cover; + border-radius: 30px; + margin-bottom: 88px; +} + +.post_article > p { + padding: 0 24px; +}
Unknown
CSS์˜ ๊ฒฝ์šฐ, [BEM ๋ฐฉ๋ฒ•๋ก ](https://getbem.com/)์„ ํ† ๋Œ€๋กœ ์ž‘์„ฑํ•˜๊ณ ์ž ๋…ธ๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,108 @@ +package christmas.validate; + +import christmas.constants.Constants; +import christmas.domain.Menu; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Validate { + private static final String errorMessage = "[ERROR]"; + private static final String COMMA = ","; + private static final int MENUMAXCOUNT = 20; + private static final String menuInputPattern = "(([๊ฐ€-ํžฃ]*)-[0-9]*)"; + private static final String integerPattern = "([0-9]*)"; + private static final String delimeterPattern = "(-[0-9]*.)"; + private static final String delimeterNamePattern = "(.[๊ฐ€-ํžฃ]+-)"; + private static final String mentionErrorDate = " ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String mentionErrorMenu = " ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + + public static void isOnlyDrinkValidate(String order) { + List<String> menuName = replaceStringToName(order); + try { + check(!menuName.stream().allMatch(menu -> Menu.isOnlyDrinkMenu(menu))); + } catch (IllegalArgumentException e) { + System.out.println(errorMessage + mentionErrorMenu); + } + } + + public static void integerTypeValidate(String number) { + try { + check(number.matches(integerPattern)); + } catch (IllegalArgumentException e) { + System.out.println(errorMessage + mentionErrorDate); + throw new IllegalArgumentException(); + } + } + + public static void menuFormatValidate(String order) { + List<String> orderList = Arrays.stream(order.split(COMMA)).toList(); + try { + if (!orderList.stream().allMatch(s -> s.matches(menuInputPattern))) { + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException e) { + System.out.println(errorMessage + mentionErrorMenu); + throw new IllegalArgumentException(); + } + } + + public static void menuDuplicationValidate(String order) { + List<String> menuName = replaceStringToName(order); + try { + check(menuName.stream().distinct().collect(Collectors.toList()).size() == menuName.size()); + } catch (IllegalArgumentException e) { + System.out.println(errorMessage + mentionErrorMenu); + throw new IllegalArgumentException(); + } + } + + public static void menuOrderNumberValidate(String order) { + try { + check(replaceStringToCount(order).stream().mapToInt(Integer::intValue).sum() <= MENUMAXCOUNT); + } catch (IllegalArgumentException e) { + System.out.println(errorMessage + mentionErrorMenu); + throw new IllegalArgumentException(); + } + } + + private static void check(boolean validate) { + if (!validate) { + throw new IllegalArgumentException(); + } + } + + public static void menuValidate(String order) { + List<String> menuName = replaceStringToName(order); + try { + for (String input : menuName) { + check(Arrays.stream(Menu.values()).anyMatch(Menu -> Menu.getName().equals(input))); + } + } catch (IllegalArgumentException e) { + System.out.println(errorMessage + mentionErrorMenu); + throw new IllegalArgumentException(); + } + } + + public static void visitDateValidate(int visitDate) { + try { + check(visitDate <= Constants.MAXDATE && visitDate >= Constants.MINDATE); + } catch (IllegalArgumentException e) { + System.out.println(errorMessage + mentionErrorDate); + throw new IllegalArgumentException(); + } + } + + private static List<String> replaceStringToName(String order) { + return Arrays.stream(order.replaceAll(delimeterPattern, ",").split(",")) + .collect(Collectors.toList()); + } + + public static List<Integer> replaceStringToCount(String order) { + return Arrays.stream(order.replaceAll(delimeterNamePattern, ",").split(",")) + .filter(s -> !s.equals("")) + .map(Integer::parseInt).collect(Collectors.toList()); + } + +}
Java
static finalํ•œ ๋ณ€์ˆ˜๋“ค์€ SCREAMING_SNAKE_CASE๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ปจ๋ฒค์…˜์ž…๋‹ˆ๋‹ค
@@ -0,0 +1,117 @@ +package christmas.model; + +import christmas.domain.Holiday; +import christmas.domain.Menu; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Discount { + + private final static int DISCOUNTUNIT = 100; + private final static int CHRISTMASDATE = 25; + private final static int BASICDISCOUNT = 1000; + private final static int PERIOD = 7; + private final static int DISCOUNT = 2023; + private final static int STARTDISCOUNT = 1000; + private final static int NOTDISCOUNT = 0; + private final static int CHAMPAGNEEVENT = 120000; + private final static String christmasDayDiscount = "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"; + private final static String weekendDiscount = "ํ‰์ผ ํ• ์ธ"; + private final static String holidayDiscount = "์ฃผ๋ง ํ• ์ธ"; + private final static String specialDiscount = "ํŠน๋ณ„ ํ• ์ธ"; + private final static String bonusEvent = "์ฆ์ • ์ด๋ฒคํŠธ"; + + public Map<String, Integer> discountTotal(int date, Map<Menu, Integer> orderMenu, int payAmount) { + Map<String, Integer> discountResult = new LinkedHashMap<>(); + discountInput(date, orderMenu, payAmount, discountResult); + return discountResult; + } + + private void discountInput(int date, Map<Menu, Integer> orderMenu, int payAmount, Map<String, Integer> discountResult) { + if (discountChristmas(date) != 0) { + discountResult.put(christmasDayDiscount, discountChristmas(date)); + } + if (discountDay(date).stream().anyMatch(menu -> orderMenu.containsKey(menu))) { + weekEndOrHolidayTotalDiscount(date, discountResult, discountDay(date), orderMenu); + } + if (discountStar(date) != 0) { + discountResult.put(specialDiscount, discountStar(date)); + } + if (eventChampagne(payAmount)) { + discountResult.put(bonusEvent, Menu.CHAMPAGNE.getPrice()); + } + } + + public int discountTotalAmount(Map<String, Integer> discountTotal) { + return discountTotal.values().stream().mapToInt(Integer::intValue).sum(); + } + + public int discountTotalAmountExceptBonus(Map<String, Integer> discountTotal) { + return discountTotal.keySet().stream().filter(key -> !key.equals(bonusEvent)) + .mapToInt(key -> discountTotal.get(key)).sum(); + } + + public void weekEndOrHolidayTotalDiscount(int date, Map<String, Integer> discountResult, + List<Menu> discount, Map<Menu, Integer> orderMenu) { + int sum = orderMenu.keySet().stream() + .filter(menu -> discount.contains(menu)).mapToInt(key -> orderMenu.get(key) * DISCOUNT).sum(); + if (weekOrHoliday(date)) { + discountResult.put(holidayDiscount, sum); + return; + } + discountResult.put(weekendDiscount, sum); + } + + public boolean eventChampagne(int orderAmount) { + if (orderAmount >= CHAMPAGNEEVENT) { + return true; + } + return false; + } + + public int discountStar(int date) { + if (Arrays.stream(Holiday.values()).anyMatch(i -> i.getDate() == date)) { + return STARTDISCOUNT; + } + return NOTDISCOUNT; + } + + public int discountChristmas(int date) { + int result = 0; + if (date <= CHRISTMASDATE) { + result = BASICDISCOUNT + DISCOUNTUNIT * (date - 1); + } + return result; + } + + public List<Menu> discountDay(int date) { + if (weekOrHoliday(date)) { + return discountHoliday(); + } + return discountWeekday(); + } + + //true : ์ฃผ๋ง | false : ํ‰์ผ + private boolean weekOrHoliday(int date) { + if (date % 7 == 1 || date % 7 == 2) { + return true; + } + return false; + } + + private List<Menu> discountWeekday() { + return Arrays.stream(Menu.values()).filter(menu -> menu != menu.NONE) + .filter(menu -> menu.getType().equals("desert")) + .collect(Collectors.toList()); + } + + private List<Menu> discountHoliday() { + return Arrays.stream(Menu.values()).filter(menu -> menu != menu.NONE) + .filter(menu -> menu.getType().equals("main")) + .collect(Collectors.toList()); + } +}
Java
private static final์˜ ์ˆœ์„œ๋กœ ์ž‘์„ฑํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,13 @@ +package christmas.model; + +import christmas.domain.Badge; + +import java.util.Arrays; +import java.util.Comparator; + +public class BadgeEvent { + public Badge giveBadge(int orderAmount) { + return Arrays.stream(Badge.values()).sorted(Comparator.reverseOrder()) + .filter(b -> b.getCutLine() <= orderAmount).findFirst().orElse(Badge.NONE); + } +}
Java
๋„๋ฉ”์ธ๊ณผ ๋ชจ๋ธ์˜ ์ฐจ์ด๊ฐ€ ๋ฌด์—‡์ธ๊ฐ€์š”?
@@ -0,0 +1,117 @@ +package christmas.model; + +import christmas.domain.Holiday; +import christmas.domain.Menu; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Discount { + + private final static int DISCOUNTUNIT = 100; + private final static int CHRISTMASDATE = 25; + private final static int BASICDISCOUNT = 1000; + private final static int PERIOD = 7; + private final static int DISCOUNT = 2023; + private final static int STARTDISCOUNT = 1000; + private final static int NOTDISCOUNT = 0; + private final static int CHAMPAGNEEVENT = 120000; + private final static String christmasDayDiscount = "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"; + private final static String weekendDiscount = "ํ‰์ผ ํ• ์ธ"; + private final static String holidayDiscount = "์ฃผ๋ง ํ• ์ธ"; + private final static String specialDiscount = "ํŠน๋ณ„ ํ• ์ธ"; + private final static String bonusEvent = "์ฆ์ • ์ด๋ฒคํŠธ"; + + public Map<String, Integer> discountTotal(int date, Map<Menu, Integer> orderMenu, int payAmount) { + Map<String, Integer> discountResult = new LinkedHashMap<>(); + discountInput(date, orderMenu, payAmount, discountResult); + return discountResult; + } + + private void discountInput(int date, Map<Menu, Integer> orderMenu, int payAmount, Map<String, Integer> discountResult) { + if (discountChristmas(date) != 0) { + discountResult.put(christmasDayDiscount, discountChristmas(date)); + } + if (discountDay(date).stream().anyMatch(menu -> orderMenu.containsKey(menu))) { + weekEndOrHolidayTotalDiscount(date, discountResult, discountDay(date), orderMenu); + } + if (discountStar(date) != 0) { + discountResult.put(specialDiscount, discountStar(date)); + } + if (eventChampagne(payAmount)) { + discountResult.put(bonusEvent, Menu.CHAMPAGNE.getPrice()); + } + } + + public int discountTotalAmount(Map<String, Integer> discountTotal) { + return discountTotal.values().stream().mapToInt(Integer::intValue).sum(); + } + + public int discountTotalAmountExceptBonus(Map<String, Integer> discountTotal) { + return discountTotal.keySet().stream().filter(key -> !key.equals(bonusEvent)) + .mapToInt(key -> discountTotal.get(key)).sum(); + } + + public void weekEndOrHolidayTotalDiscount(int date, Map<String, Integer> discountResult, + List<Menu> discount, Map<Menu, Integer> orderMenu) { + int sum = orderMenu.keySet().stream() + .filter(menu -> discount.contains(menu)).mapToInt(key -> orderMenu.get(key) * DISCOUNT).sum(); + if (weekOrHoliday(date)) { + discountResult.put(holidayDiscount, sum); + return; + } + discountResult.put(weekendDiscount, sum); + } + + public boolean eventChampagne(int orderAmount) { + if (orderAmount >= CHAMPAGNEEVENT) { + return true; + } + return false; + } + + public int discountStar(int date) { + if (Arrays.stream(Holiday.values()).anyMatch(i -> i.getDate() == date)) { + return STARTDISCOUNT; + } + return NOTDISCOUNT; + } + + public int discountChristmas(int date) { + int result = 0; + if (date <= CHRISTMASDATE) { + result = BASICDISCOUNT + DISCOUNTUNIT * (date - 1); + } + return result; + } + + public List<Menu> discountDay(int date) { + if (weekOrHoliday(date)) { + return discountHoliday(); + } + return discountWeekday(); + } + + //true : ์ฃผ๋ง | false : ํ‰์ผ + private boolean weekOrHoliday(int date) { + if (date % 7 == 1 || date % 7 == 2) { + return true; + } + return false; + } + + private List<Menu> discountWeekday() { + return Arrays.stream(Menu.values()).filter(menu -> menu != menu.NONE) + .filter(menu -> menu.getType().equals("desert")) + .collect(Collectors.toList()); + } + + private List<Menu> discountHoliday() { + return Arrays.stream(Menu.values()).filter(menu -> menu != menu.NONE) + .filter(menu -> menu.getType().equals("main")) + .collect(Collectors.toList()); + } +}
Java
์‚ฌ์šฉ๋˜๊ณ  ์žˆ์ง€ ์•Š์€ ๋ณ€์ˆ˜์ธ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,25 @@ +package christmas.domain; + +public enum Badge { + NONE("์—†์Œ", 0), + STAR("๋ณ„", 5000), + TREE("ํŠธ๋ฆฌ", 10000), + SANTA("์‚ฐํƒ€", 20000); + + + private String name; + private int cutLine; + + Badge(String name, int cutLine) { + this.name = name; + this.cutLine = cutLine; + } + + public String getName() { + return name; + } + + public int getCutLine() { + return cutLine; + } +}
Java
enum์˜ ์ด ๋ณ€์ˆ˜๋“ค์€ ์žฌํ• ๋‹น ๋  ์ผ์ด ์—†์œผ๋‹ˆ private finalํ•˜๊ฒŒ ์ž‘์„ฑํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,13 @@ +package christmas.model; + +import christmas.domain.Badge; + +import java.util.Arrays; +import java.util.Comparator; + +public class BadgeEvent { + public Badge giveBadge(int orderAmount) { + return Arrays.stream(Badge.values()).sorted(Comparator.reverseOrder()) + .filter(b -> b.getCutLine() <= orderAmount).findFirst().orElse(Badge.NONE); + } +}
Java
1. ์ŠคํŠธ๋ฆผ์€ ํ•œ ์ค„์— ํ•œ ๊ฐœ์”ฉ ํ•ด์„œ ๋Š˜์—ฌ์จ ์ฃผ์‹œ๋ฉด ๋ณด๊ธฐ ํŽธํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค 2. ์ปคํŠธ๋ผ์ธ์„ ๋ฌผ์–ด๋ณธ ๋‹ค์Œ ์ง์ ‘ ๋กœ์ง์„ ๊ณ„์‚ฐํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ, ์ปคํŠธ๋ผ์ธ์„ ๋„˜๋ƒ๊ณ  ์‹œํ‚ค๋ฉด ์–ด๋–จ๊นŒ์š”? 3. ๋žŒ๋‹ค์˜ b๋ผ๋Š” ๋ณ€์ˆ˜๋ช…์„ ์กฐ๊ธˆ ๋” ํŒŒ์•…ํ•˜๊ธฐ ์‰ฌ์šด ์ด๋ฆ„์„ ์ ์–ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” 4. ์ด ์ฝ”๋“œ๋Š” Badge์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๊ฒŒ ๋˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค. ์ปคํŠธ๋ผ์ธ์„ ๊ธฐ์ค€์œผ๋กœ reverseOrder๋กœ ์ •๋ ฌํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์•„์š” ```suggestion public Badge giveBadge(int orderAmount) { return Arrays.stream(Badge.values()) .filter(badge -> badge.isOverCutLine(orderAmount)) .max(Comparator.comparingInt(Badge::getCutLine)) .orElse(Badge.NONE); } ```
@@ -0,0 +1,62 @@ +package christmas.domain; + +import java.util.Arrays; + +public enum Menu { + + STAKE("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "main"), + LEAF("๋ฐ”๋น„ํ๋ฆฝ", 54000, "main"), + PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "main"), + CHRISMASPASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000, "main"), + + COKE("์ œ๋กœ์ฝœ๋ผ", 3000, "drink"), + WINE("๋ ˆ๋“œ์™€์ธ", 60000, "drink"), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25000, "drink"), + + CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "desert"), + ICECREAM("์•„์ด์Šคํฌ๋ฆผ", 5000, "desert"), + + SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "appetizer"), + TAPAS("ํƒ€ํŒŒ์Šค", 5500, "appetizer"), + SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000, "appetizer"), + + NONE("์—†์Œ", 0, "none"); + + private String name; + private int price; + private String type; + + public String getType() { + return type; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + Menu(String name, int price, String type) { + this.name = name; + this.price = price; + this.type = type; + } + + public static Menu getMenuWithName(String menuName) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(menuName)).findAny().get(); + } + + public static int getPriceWithName(String name) { + return Arrays.stream(Menu.values()).filter(Menu -> Menu.name.equals(name)).findAny() + .orElse(NONE).getPrice(); + } + + // Drink๋งŒ ํฌํ•จ๋œ ๋ฉ”๋‰ด๊ฐ€ ์•„๋‹ˆ๋ฉด True + public static boolean isOnlyDrinkMenu(String name) { + return Arrays.stream(Menu.values()).filter(n -> n.getName().equals(name)) + .allMatch(n -> n.getType().equals("drink")); + } +}
Java
1. ์—†๋Š” ๋ฉ”๋‰ด ์ด๋ฆ„์ด ๋“ค์–ด์˜ค๋ฉด ํ„ฐ์งˆ ์ˆ˜ ์žˆ์œผ๋‹ˆ, get๋ณด๋‹จ orElseThrow์™€ ๊ฐ™์€ ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” 2. ์žˆ์„ ์ˆ˜๋„, ์—†์„ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ get๋ณด๋‹จ `findMenuByName` ๊ฐ™์€ ์ด๋ฆ„๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š”
@@ -0,0 +1,30 @@ +package christmas.domain; + +import java.util.Map; + +public class Order { + + private int orderDate; + private Map<Menu, Integer> orderMenu; + + public Order(int orderDate, Map<Menu, Integer> orderMenu) { + this.orderDate = orderDate; + this.orderMenu = orderMenu; + } + + public int getOrderDate() { + return orderDate; + } + + public Map<Menu, Integer> getOrderMenu() { + return orderMenu; + } + + public int orderAmount() { + int result = 0; + for (Menu menu : orderMenu.keySet()) { + result += Menu.getPriceWithName(menu.getName()) * orderMenu.get(menu); + } + return result; + } +}
Java
```suggestion result += menu.getPrice() * orderMenu.get(menu); ``` ๊ทธ๋ƒฅ menu.getPrice๋ฅผ ์จ๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ˜„
@@ -0,0 +1,62 @@ +package christmas.domain; + +import java.util.Arrays; + +public enum Menu { + + STAKE("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "main"), + LEAF("๋ฐ”๋น„ํ๋ฆฝ", 54000, "main"), + PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "main"), + CHRISMASPASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000, "main"), + + COKE("์ œ๋กœ์ฝœ๋ผ", 3000, "drink"), + WINE("๋ ˆ๋“œ์™€์ธ", 60000, "drink"), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25000, "drink"), + + CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "desert"), + ICECREAM("์•„์ด์Šคํฌ๋ฆผ", 5000, "desert"), + + SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "appetizer"), + TAPAS("ํƒ€ํŒŒ์Šค", 5500, "appetizer"), + SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000, "appetizer"), + + NONE("์—†์Œ", 0, "none"); + + private String name; + private int price; + private String type; + + public String getType() { + return type; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + Menu(String name, int price, String type) { + this.name = name; + this.price = price; + this.type = type; + } + + public static Menu getMenuWithName(String menuName) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(menuName)).findAny().get(); + } + + public static int getPriceWithName(String name) { + return Arrays.stream(Menu.values()).filter(Menu -> Menu.name.equals(name)).findAny() + .orElse(NONE).getPrice(); + } + + // Drink๋งŒ ํฌํ•จ๋œ ๋ฉ”๋‰ด๊ฐ€ ์•„๋‹ˆ๋ฉด True + public static boolean isOnlyDrinkMenu(String name) { + return Arrays.stream(Menu.values()).filter(n -> n.getName().equals(name)) + .allMatch(n -> n.getType().equals("drink")); + } +}
Java
```suggestion STAKE("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, Category.MAIN), ``` ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์˜คํƒ€ ๋ฐฉ์ง€๋‚˜ ๋ณ€๊ฒฝ์˜ ์œ ์—ฐํ•จ์„ ์œ„ํ•ด ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ enum์œผ๋กœ ๋งŒ๋“ค๊ฑฐ๋‚˜, ์ƒ์ˆ˜๋“ค์„ ๋ณ€์ˆ˜๋กœ ๋นผ์คฌ์–ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,62 @@ +package christmas.domain; + +import java.util.Arrays; + +public enum Menu { + + STAKE("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "main"), + LEAF("๋ฐ”๋น„ํ๋ฆฝ", 54000, "main"), + PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "main"), + CHRISMASPASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000, "main"), + + COKE("์ œ๋กœ์ฝœ๋ผ", 3000, "drink"), + WINE("๋ ˆ๋“œ์™€์ธ", 60000, "drink"), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25000, "drink"), + + CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "desert"), + ICECREAM("์•„์ด์Šคํฌ๋ฆผ", 5000, "desert"), + + SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "appetizer"), + TAPAS("ํƒ€ํŒŒ์Šค", 5500, "appetizer"), + SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000, "appetizer"), + + NONE("์—†์Œ", 0, "none"); + + private String name; + private int price; + private String type; + + public String getType() { + return type; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + Menu(String name, int price, String type) { + this.name = name; + this.price = price; + this.type = type; + } + + public static Menu getMenuWithName(String menuName) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(menuName)).findAny().get(); + } + + public static int getPriceWithName(String name) { + return Arrays.stream(Menu.values()).filter(Menu -> Menu.name.equals(name)).findAny() + .orElse(NONE).getPrice(); + } + + // Drink๋งŒ ํฌํ•จ๋œ ๋ฉ”๋‰ด๊ฐ€ ์•„๋‹ˆ๋ฉด True + public static boolean isOnlyDrinkMenu(String name) { + return Arrays.stream(Menu.values()).filter(n -> n.getName().equals(name)) + .allMatch(n -> n.getType().equals("drink")); + } +}
Java
1. ๋žŒ๋‹ค์‹์˜ ๋ณ€์ˆ˜๋Š” camelCase๋กœ ์ ์–ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” 2. `getMenuWithName`์˜ ๋กœ์ง๊ณผ ์ค‘๋ณต์ด์ง€ ์•Š๋‚˜์š”? ๋ฉ”์†Œ๋“œ๋กœ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,62 @@ +package christmas.domain; + +import java.util.Arrays; + +public enum Menu { + + STAKE("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "main"), + LEAF("๋ฐ”๋น„ํ๋ฆฝ", 54000, "main"), + PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "main"), + CHRISMASPASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000, "main"), + + COKE("์ œ๋กœ์ฝœ๋ผ", 3000, "drink"), + WINE("๋ ˆ๋“œ์™€์ธ", 60000, "drink"), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25000, "drink"), + + CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "desert"), + ICECREAM("์•„์ด์Šคํฌ๋ฆผ", 5000, "desert"), + + SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "appetizer"), + TAPAS("ํƒ€ํŒŒ์Šค", 5500, "appetizer"), + SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000, "appetizer"), + + NONE("์—†์Œ", 0, "none"); + + private String name; + private int price; + private String type; + + public String getType() { + return type; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + Menu(String name, int price, String type) { + this.name = name; + this.price = price; + this.type = type; + } + + public static Menu getMenuWithName(String menuName) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(menuName)).findAny().get(); + } + + public static int getPriceWithName(String name) { + return Arrays.stream(Menu.values()).filter(Menu -> Menu.name.equals(name)).findAny() + .orElse(NONE).getPrice(); + } + + // Drink๋งŒ ํฌํ•จ๋œ ๋ฉ”๋‰ด๊ฐ€ ์•„๋‹ˆ๋ฉด True + public static boolean isOnlyDrinkMenu(String name) { + return Arrays.stream(Menu.values()).filter(n -> n.getName().equals(name)) + .allMatch(n -> n.getType().equals("drink")); + } +}
Java
์ด๊ฑด ์ด ํด๋ž˜์Šค์˜ ๋ฉ”๋‰ด๋“ค ์ค‘, name๊ณผ ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ฉ”๋‰ด๊ฐ€ drink์ธ์ง€์— ๋Œ€ํ•œ ๋กœ์ง์ธ ๊ฒƒ ๊ฐ™์•„์š”. ๋‹ค์‹œ ๋งํ•ด, name์œผ๋กœ ๋“ค์–ด์˜จ ์• ๊ฐ€ drink์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๋กœ์ง์ธ๋ฐ์š”. ์ฃผ์„์œผ๋กœ ์ž‘์„ฑํ•˜์‹  ์„ค๋ช…๊ณผ ์กฐ๊ธˆ ๋‹ค๋ฅธ ๊ฒƒ ๊ฐ™์•„์š” `isDrinkMenu`๊ฐ€ ์–ด์šธ๋ฆฌ์ง€ ์•Š์„๊นŒ์š”?
@@ -0,0 +1,62 @@ +package christmas.domain; + +import java.util.Arrays; + +public enum Menu { + + STAKE("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "main"), + LEAF("๋ฐ”๋น„ํ๋ฆฝ", 54000, "main"), + PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "main"), + CHRISMASPASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000, "main"), + + COKE("์ œ๋กœ์ฝœ๋ผ", 3000, "drink"), + WINE("๋ ˆ๋“œ์™€์ธ", 60000, "drink"), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25000, "drink"), + + CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "desert"), + ICECREAM("์•„์ด์Šคํฌ๋ฆผ", 5000, "desert"), + + SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "appetizer"), + TAPAS("ํƒ€ํŒŒ์Šค", 5500, "appetizer"), + SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000, "appetizer"), + + NONE("์—†์Œ", 0, "none"); + + private String name; + private int price; + private String type; + + public String getType() { + return type; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + Menu(String name, int price, String type) { + this.name = name; + this.price = price; + this.type = type; + } + + public static Menu getMenuWithName(String menuName) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(menuName)).findAny().get(); + } + + public static int getPriceWithName(String name) { + return Arrays.stream(Menu.values()).filter(Menu -> Menu.name.equals(name)).findAny() + .orElse(NONE).getPrice(); + } + + // Drink๋งŒ ํฌํ•จ๋œ ๋ฉ”๋‰ด๊ฐ€ ์•„๋‹ˆ๋ฉด True + public static boolean isOnlyDrinkMenu(String name) { + return Arrays.stream(Menu.values()).filter(n -> n.getName().equals(name)) + .allMatch(n -> n.getType().equals("drink")); + } +}
Java
๋žŒ๋‹ค ์‹์˜ ๋ณ€์ˆ˜๋ช…์„ ์กฐ๊ธˆ๋” ์ž์„ธํžˆ ์ ์–ด์ฃผ์‹œ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,32 @@ +package christmas.model; + +import christmas.domain.Menu; + +import java.util.Map; + +public class Condition { + + private final static int CONDITION = 10000; + private final static int ORDERCOUNTCONDITION = 20; + + public boolean checkEventConditionAmount(int orderAmount) { + if (orderAmount >= CONDITION) { + return true; + } + return false; + } + + public boolean checkEventConditionMenu(Map<Menu, Integer> orderMenu) { + if (orderMenu.keySet().stream().allMatch(menu -> Menu.isOnlyDrinkMenu(menu.getName()))) { + return false; + } + return true; + } + + public boolean checkEventConditionMenuCount(Map<Menu, Integer> orderMenu) { + if (orderMenu.values().stream().mapToInt(Integer::intValue).sum() > ORDERCOUNTCONDITION) { + return false; + } + return true; + } +}
Java
`return orderAmount >= CONDITION;` ๊ณผ ๊ฐ™์ด ์ค„์—ฌ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,32 @@ +package christmas.model; + +import christmas.domain.Menu; + +import java.util.Map; + +public class Condition { + + private final static int CONDITION = 10000; + private final static int ORDERCOUNTCONDITION = 20; + + public boolean checkEventConditionAmount(int orderAmount) { + if (orderAmount >= CONDITION) { + return true; + } + return false; + } + + public boolean checkEventConditionMenu(Map<Menu, Integer> orderMenu) { + if (orderMenu.keySet().stream().allMatch(menu -> Menu.isOnlyDrinkMenu(menu.getName()))) { + return false; + } + return true; + } + + public boolean checkEventConditionMenuCount(Map<Menu, Integer> orderMenu) { + if (orderMenu.values().stream().mapToInt(Integer::intValue).sum() > ORDERCOUNTCONDITION) { + return false; + } + return true; + } +}
Java
์ปจ๋””์…˜์€ ์กฐ๊ฑด์ด๋ž€ ๋œป์ธ๋ฐ, int ๋ณ€์ˆ˜์ธ์ง€๋ผ ์กฐ๊ฑด์ด๋ž‘์€ ์•ˆ ๋งž๋Š” ๊ฒƒ ๊ฐ™์•„์š”. MAX_ORDER_COUNT ๊ฐ™์€ ์ด๋ฆ„์€ ์–ด๋–จ๊นŒ์š”
@@ -0,0 +1,117 @@ +package christmas.model; + +import christmas.domain.Holiday; +import christmas.domain.Menu; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Discount { + + private final static int DISCOUNTUNIT = 100; + private final static int CHRISTMASDATE = 25; + private final static int BASICDISCOUNT = 1000; + private final static int PERIOD = 7; + private final static int DISCOUNT = 2023; + private final static int STARTDISCOUNT = 1000; + private final static int NOTDISCOUNT = 0; + private final static int CHAMPAGNEEVENT = 120000; + private final static String christmasDayDiscount = "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"; + private final static String weekendDiscount = "ํ‰์ผ ํ• ์ธ"; + private final static String holidayDiscount = "์ฃผ๋ง ํ• ์ธ"; + private final static String specialDiscount = "ํŠน๋ณ„ ํ• ์ธ"; + private final static String bonusEvent = "์ฆ์ • ์ด๋ฒคํŠธ"; + + public Map<String, Integer> discountTotal(int date, Map<Menu, Integer> orderMenu, int payAmount) { + Map<String, Integer> discountResult = new LinkedHashMap<>(); + discountInput(date, orderMenu, payAmount, discountResult); + return discountResult; + } + + private void discountInput(int date, Map<Menu, Integer> orderMenu, int payAmount, Map<String, Integer> discountResult) { + if (discountChristmas(date) != 0) { + discountResult.put(christmasDayDiscount, discountChristmas(date)); + } + if (discountDay(date).stream().anyMatch(menu -> orderMenu.containsKey(menu))) { + weekEndOrHolidayTotalDiscount(date, discountResult, discountDay(date), orderMenu); + } + if (discountStar(date) != 0) { + discountResult.put(specialDiscount, discountStar(date)); + } + if (eventChampagne(payAmount)) { + discountResult.put(bonusEvent, Menu.CHAMPAGNE.getPrice()); + } + } + + public int discountTotalAmount(Map<String, Integer> discountTotal) { + return discountTotal.values().stream().mapToInt(Integer::intValue).sum(); + } + + public int discountTotalAmountExceptBonus(Map<String, Integer> discountTotal) { + return discountTotal.keySet().stream().filter(key -> !key.equals(bonusEvent)) + .mapToInt(key -> discountTotal.get(key)).sum(); + } + + public void weekEndOrHolidayTotalDiscount(int date, Map<String, Integer> discountResult, + List<Menu> discount, Map<Menu, Integer> orderMenu) { + int sum = orderMenu.keySet().stream() + .filter(menu -> discount.contains(menu)).mapToInt(key -> orderMenu.get(key) * DISCOUNT).sum(); + if (weekOrHoliday(date)) { + discountResult.put(holidayDiscount, sum); + return; + } + discountResult.put(weekendDiscount, sum); + } + + public boolean eventChampagne(int orderAmount) { + if (orderAmount >= CHAMPAGNEEVENT) { + return true; + } + return false; + } + + public int discountStar(int date) { + if (Arrays.stream(Holiday.values()).anyMatch(i -> i.getDate() == date)) { + return STARTDISCOUNT; + } + return NOTDISCOUNT; + } + + public int discountChristmas(int date) { + int result = 0; + if (date <= CHRISTMASDATE) { + result = BASICDISCOUNT + DISCOUNTUNIT * (date - 1); + } + return result; + } + + public List<Menu> discountDay(int date) { + if (weekOrHoliday(date)) { + return discountHoliday(); + } + return discountWeekday(); + } + + //true : ์ฃผ๋ง | false : ํ‰์ผ + private boolean weekOrHoliday(int date) { + if (date % 7 == 1 || date % 7 == 2) { + return true; + } + return false; + } + + private List<Menu> discountWeekday() { + return Arrays.stream(Menu.values()).filter(menu -> menu != menu.NONE) + .filter(menu -> menu.getType().equals("desert")) + .collect(Collectors.toList()); + } + + private List<Menu> discountHoliday() { + return Arrays.stream(Menu.values()).filter(menu -> menu != menu.NONE) + .filter(menu -> menu.getType().equals("main")) + .collect(Collectors.toList()); + } +}
Java
1. Menu.NONE์€ static ๋ณ€์ˆ˜์ด๋‹ˆ, menu.NONE๊ณผ ๊ฐ™์ด ์ธ์Šคํ„ด์Šคํ™”ํ•œ ๋ณ€์ˆ˜์—์„œ ์•ก์„ธ์Šค ๋˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค 2. isDesert ์™€ ๊ฐ™์ด, Menu์—๊ฒŒ ์ง์ ‘ ๋””์ €ํŠธ๋ƒ๊ณ  ๋ฌผ์–ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š” 3. toList()๋กœ ์ถ•์•ฝํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š” ```suggestion return Arrays.stream(Menu.values()) .filter(menu -> menu != Menu.NONE) .filter(Menu::isDesert) .toList(); ```
@@ -0,0 +1,32 @@ +package christmas.model; + +import christmas.domain.Menu; + +import java.util.Map; + +public class Condition { + + private final static int CONDITION = 10000; + private final static int ORDERCOUNTCONDITION = 20; + + public boolean checkEventConditionAmount(int orderAmount) { + if (orderAmount >= CONDITION) { + return true; + } + return false; + } + + public boolean checkEventConditionMenu(Map<Menu, Integer> orderMenu) { + if (orderMenu.keySet().stream().allMatch(menu -> Menu.isOnlyDrinkMenu(menu.getName()))) { + return false; + } + return true; + } + + public boolean checkEventConditionMenuCount(Map<Menu, Integer> orderMenu) { + if (orderMenu.values().stream().mapToInt(Integer::intValue).sum() > ORDERCOUNTCONDITION) { + return false; + } + return true; + } +}
Java
menu์˜ ๋ฐฐ์—ด์„ ๋Œ๋ฉด์„œ ๋ชจ๋‘ drink์ธ์ง€ ํŒ๋‹จํ•˜๋Š” ๋กœ์ง์ž…๋‹ˆ๋‹ค. allMatch์—์„œ, ๊ฐ–๊ณ  ์žˆ๋Š” menuํ•œํ…Œ drink ๋ฉ”๋‰ด์ธ์ง€ ๋ฌผ์–ด๋ณด๋ฉด ๋˜๋Š”๋ฐ์š”. ๊ตณ์ด ์ด๋ฆ„์„ ๊บผ๋‚ด์„œ, Menu์˜ static ํ•จ์ˆ˜ํ•œํ…Œ ๋ฌผ์–ด๋ณผ ํ•„์š”๊ฐ€ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ```java return !orderMenu.keySet() .stream() .allMatch(Menu::isDrink); ```
@@ -0,0 +1,52 @@ +package christmas.model; + +import camp.nextstep.edu.missionutils.Console; +import christmas.domain.Menu; +import christmas.validate.Validate; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class Input { + + private static final String delimeterComma = "(,)"; + private static final String delimeterDash = "(-)"; + + public int inputOrderDate() { + String input = Console.readLine(); + try { + Validate.integerTypeValidate(input); + int date = Integer.parseInt(input); + Validate.visitDateValidate(date); + } catch (IllegalArgumentException e) { + inputOrderDate(); + } + return Integer.parseInt(input); + } + + public Map<Menu, Integer> inputOrderMenu() { + String input = Console.readLine(); + try { + Validate.menuFormatValidate(input); + Validate.isOnlyDrinkValidate(input); + Validate.menuDuplicationValidate(input); + Validate.menuValidate(input); + Validate.menuOrderNumberValidate(input); + } catch (IllegalArgumentException e) { + inputOrderMenu(); + } + return createOrder(input); + } + + private Map<Menu, Integer> createOrder(String input) { + Map<Menu, Integer> result = new LinkedHashMap<>(); + List<String[]> pair = Arrays.stream(input.trim().split(delimeterComma)) + .map(s -> s.split(delimeterDash)).toList(); + for (String[] strings : pair) { + result.put(Menu.getMenuWithName(strings[0]), Integer.parseInt(strings[1])); + } + return result; + } +}
Java
Input ๊ทธ ์ž์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ Input์„ ๋ฐ›๋Š” ํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋” ์˜๋ฏธ๊ฐ€ ๋“œ๋Ÿฌ๋‚˜๋Š” ์ด๋ฆ„์ด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,52 @@ +package christmas.model; + +import camp.nextstep.edu.missionutils.Console; +import christmas.domain.Menu; +import christmas.validate.Validate; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class Input { + + private static final String delimeterComma = "(,)"; + private static final String delimeterDash = "(-)"; + + public int inputOrderDate() { + String input = Console.readLine(); + try { + Validate.integerTypeValidate(input); + int date = Integer.parseInt(input); + Validate.visitDateValidate(date); + } catch (IllegalArgumentException e) { + inputOrderDate(); + } + return Integer.parseInt(input); + } + + public Map<Menu, Integer> inputOrderMenu() { + String input = Console.readLine(); + try { + Validate.menuFormatValidate(input); + Validate.isOnlyDrinkValidate(input); + Validate.menuDuplicationValidate(input); + Validate.menuValidate(input); + Validate.menuOrderNumberValidate(input); + } catch (IllegalArgumentException e) { + inputOrderMenu(); + } + return createOrder(input); + } + + private Map<Menu, Integer> createOrder(String input) { + Map<Menu, Integer> result = new LinkedHashMap<>(); + List<String[]> pair = Arrays.stream(input.trim().split(delimeterComma)) + .map(s -> s.split(delimeterDash)).toList(); + for (String[] strings : pair) { + result.put(Menu.getMenuWithName(strings[0]), Integer.parseInt(strings[1])); + } + return result; + } +}
Java
๋งํฌ๋“œ ํ•ด์‹œ๋งต์œผ๋กœ ์ž‘์„ฑํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,59 @@ +package christmas.domain.order; + +import christmas.domain.menu.Menu; +import christmas.error.ErrorMessage; +import java.util.Objects; + +public class OrderMenu { + + private static final int MIN_QUANTITY = 1; + private static final int MAX_QUANTITY = 20; + + private final Menu menu; + private final int quantity; + + public OrderMenu(Menu menu, int quantity) { + this.menu = menu; + if (quantity < MIN_QUANTITY || quantity > MAX_QUANTITY) { + throw new IllegalArgumentException(ErrorMessage.INVALID_ORDER.getMessage()); + } + this.quantity = quantity; + } + + public boolean isMenuMain() { + return menu.isMain(); + } + + public boolean isMenuDessert() { + return menu.isDessert(); + } + + public int calculatePrice() { + return menu.getPrice() * quantity; + } + + public boolean isNotBeverage() { + return menu.isNotBeverage(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + OrderMenu orderMenu = (OrderMenu) o; + return menu == orderMenu.menu; + } + + @Override + public int hashCode() { + return Objects.hash(menu); + } + + public Menu getMenu() { + return menu; + } + + public int getQuantity() { + return quantity; + } +}
Java
OrderMenus์—์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํ•˜์‹  ๊ฑฐ ๊ฐ™์€๋ฐ ์—ฌ๊ธฐ์„œ๋Š” ์™œ ํ•˜์…จ๋Š”์ง€ ์˜๊ฒฌ์ด ๊ถ๊ธˆํ•ด์š”!
@@ -0,0 +1,28 @@ +package christmas.domain.event; + +import java.util.function.Predicate; + +public enum EventBadge { + + SANTA("์‚ฐํƒ€", (amount) -> amount >= 20_000), + TREE("ํŠธ๋ฆฌ", (amount) -> amount >= 10_000 && amount < 20_000), + STAR("๋ณ„", (amount) -> amount >= 5_000 && amount < 10_000), + MISS("์—†์Œ", (amount) -> amount < 5_000); + + private final String name; + private final Predicate<Integer> totalBenefitAmountMatcher; + + EventBadge(String name, Predicate<Integer> totalBenefitAmountMatcher) { + this.name = name; + this.totalBenefitAmountMatcher = totalBenefitAmountMatcher; + } + + public boolean matchesTotalBenefitAmount(int totalBenefitAmount) { + return totalBenefitAmountMatcher.test(totalBenefitAmount); + } + + @Override + public String toString() { + return name; + } +}
Java
์ด๊ฑธ ์‚ฌ์šฉํ•˜๋Š”๊ฑฐ๋ž‘ ๋”ฐ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ ์ค‘์— Predicate๋ฅผ ์„ ํƒํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”
@@ -0,0 +1,52 @@ +package christmas.domain; + +import christmas.error.ErrorMessage; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.List; + +public class VisitDay { + + private static final int MIN_VISIT_DAY = 1; + private static final int MAX_VISIT_DAY = 31; + private static final int EVENT_YEAR = 2023; + private static final int EVENT_MONTH = 12; + + private final int visitDay; + + public VisitDay(int visitDay) { + validate(visitDay); + this.visitDay = visitDay; + } + + private void validate(int visitDay) { + if (invalidDay(visitDay)) { + throw new IllegalArgumentException(ErrorMessage.INVALID_VISIT_DAY.getMessage()); + } + } + + private boolean invalidDay(int visitDay) { + return visitDay < MIN_VISIT_DAY || visitDay > MAX_VISIT_DAY; + } + + public boolean isNotWithinChristmasPeriod(int christmasDDay) { + return visitDay < MIN_VISIT_DAY || visitDay > christmasDDay; + } + + public int decreaseOneDay() { + return visitDay - 1; + } + + public boolean isWeekend() { + DayOfWeek visitDayOfWeek = LocalDate.of(EVENT_YEAR, EVENT_MONTH, visitDay).getDayOfWeek(); + return visitDayOfWeek == DayOfWeek.FRIDAY || visitDayOfWeek == DayOfWeek.SATURDAY; + } + + public boolean isNotSpecialDay(List<Integer> specialDays) { + return !specialDays.contains(visitDay); + } + + public int getVisitDay() { + return visitDay; + } +}
Java
์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค `DayOfWeek`๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์•„๋‹ˆ๋ผ ๊ฐ์ฒด ์ƒ์„ฑ์‹œ์— ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“์€ ๋‹ค์Œ์— ์“ฐ๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”? ์–ด์งœํ”ผ visitDay๋Š” ์•ˆ๋ฐ”๋€Œ๋‹ˆ๊นŒ์š”.
@@ -0,0 +1,57 @@ +package christmas.domain.menu; + +import christmas.error.ErrorMessage; +import java.util.Arrays; + +public enum Menu { + + MUSHROOM_SOUP("์–‘์†ก์ด ์ˆ˜ํ”„", 6_000, MenuCategory.APPETIZER), + TAPAS("ํƒ€ํŒŒ์Šค", 5_500, MenuCategory.APPETIZER), + CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000, MenuCategory.APPETIZER), + T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000, MenuCategory.MAIN), + BARBECUE_RIBS("๋ฐ”๋น„ํ๋ฆฝ", 54_000, MenuCategory.MAIN), + SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000, MenuCategory.MAIN), + CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000, MenuCategory.MAIN), + CHOCOLATE_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15_000, MenuCategory.DESSERT), + ICE_CREAM("์•„์ด์Šคํฌ๋ฆผ", 5_000, MenuCategory.DESSERT), + COKE_ZERO("์ œ๋กœ์ฝœ๋ผ", 3_000, MenuCategory.BEVERAGE), + RED_WINE("๋ ˆ๋“œ์™€์ธ", 60_000, MenuCategory.BEVERAGE), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, MenuCategory.BEVERAGE); + + private final String name; + private final int price; + private final MenuCategory category; + + Menu(String name, int price, MenuCategory category) { + this.name = name; + this.price = price; + this.category = category; + } + + public static Menu from(String menuName) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.name.equals(menuName)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(ErrorMessage.INVALID_ORDER.getMessage())); + } + + public boolean isMain() { + return this.category == MenuCategory.MAIN; + } + + public boolean isDessert() { + return this.category == MenuCategory.DESSERT; + } + + public boolean isNotBeverage() { + return this.category != MenuCategory.BEVERAGE; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } +}
Java
๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด๋‹ต๊ฒŒ ์‚ฌ์šฉํ•˜์‹  ์ฝ”๋“œ๋„ค์š”!
@@ -0,0 +1,59 @@ +package christmas.domain.order; + +import christmas.domain.menu.Menu; +import christmas.error.ErrorMessage; +import java.util.Objects; + +public class OrderMenu { + + private static final int MIN_QUANTITY = 1; + private static final int MAX_QUANTITY = 20; + + private final Menu menu; + private final int quantity; + + public OrderMenu(Menu menu, int quantity) { + this.menu = menu; + if (quantity < MIN_QUANTITY || quantity > MAX_QUANTITY) { + throw new IllegalArgumentException(ErrorMessage.INVALID_ORDER.getMessage()); + } + this.quantity = quantity; + } + + public boolean isMenuMain() { + return menu.isMain(); + } + + public boolean isMenuDessert() { + return menu.isDessert(); + } + + public int calculatePrice() { + return menu.getPrice() * quantity; + } + + public boolean isNotBeverage() { + return menu.isNotBeverage(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + OrderMenu orderMenu = (OrderMenu) o; + return menu == orderMenu.menu; + } + + @Override + public int hashCode() { + return Objects.hash(menu); + } + + public Menu getMenu() { + return menu; + } + + public int getQuantity() { + return quantity; + } +}
Java
eqauls()์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ๋งŽ์ด ํ•˜์…จ๋„ค์š”! hashCode๊นŒ์ง€ ๋‹ค์‹œ ๊ตฌํ˜„ํ•˜์‹ ๊ฑฐ ์ธ์ƒ๊นŠ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,51 @@ +package christmas.service; + +import christmas.domain.BenefitDetails; +import christmas.domain.ExpectedPaymentAmountCalculator; +import christmas.domain.event.EventBadge; +import christmas.domain.event.EventBadgeAssigner; +import christmas.domain.menu.Menu; +import christmas.domain.order.OrderMenu; +import christmas.domain.order.OrderMenus; +import christmas.dto.OrderMenusDto; +import java.util.List; +import java.util.stream.Collectors; + +public class Service { + + private final ExpectedPaymentAmountCalculator expectedPaymentAmountCalculator; + private final EventBadgeAssigner eventBadgeAssigner; + + public Service( + ExpectedPaymentAmountCalculator expectedPaymentAmountCalculator, + EventBadgeAssigner eventBadgeAssigner) { + this.expectedPaymentAmountCalculator = expectedPaymentAmountCalculator; + this.eventBadgeAssigner = eventBadgeAssigner; + } + + public OrderMenus order(OrderMenusDto orderMenusDto) { + return new OrderMenus(convertToOrderMenuList(orderMenusDto)); + } + + private List<OrderMenu> convertToOrderMenuList(OrderMenusDto orderMenusDto) { + return orderMenusDto.getMenus().entrySet() + .stream() + .map(orderMenus -> new OrderMenu(Menu.from(orderMenus.getKey()), orderMenus.getValue())) + .collect(Collectors.toList()); + } + + public int calculateTotalOrderAmount(OrderMenus orderMenus) { + return orderMenus.calculateTotalOrderAmount(); + } + + public int calculateExpectedPaymentAmount(OrderMenus orderMenus, BenefitDetails benefitDetails) { + return expectedPaymentAmountCalculator.calculateExpectedPaymentAmount( + orderMenus.calculateTotalOrderAmount(), + benefitDetails.calculateTotalBenefitAmount() + ); + } + + public EventBadge assignEventBadge(BenefitDetails benefitDetails) { + return eventBadgeAssigner.assignEventBadge(benefitDetails.calculateTotalBenefitAmount()); + } +}
Java
์ „๋ฐ˜์ ์œผ๋กœ ์—ฌ๋Ÿฌ ๊ณณ์—์„œ stream์„ ์ž˜ ์“ฐ์…”์„œ ๋“œ๋ฆด ๋ง์”€์ด ์—†๋„ค์š”;; ์ถ”๊ฐ€์ ์œผ๋กœ ์ด์•ผ๊ธฐ๋งŒ ๋“œ๋ฆฌ๋ฉด stream์—์„œ `( )` ์•ˆ์ด ๊ธธ๋‹ค๋ฉด ๋ฉ”์„œ๋“œ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ```java private List<OrderMenu> convertToOrderMenuList(OrderMenusDto orderMenusDto) { return orderMenusDto.getMenus().entrySet() .stream() .map(this::convertToOrderMenu) .collect(Collectors.toList()); } private OrderMenu convertToOrderMenu(OrderMenuDto orderMenu) { return new OrderMenu(Menu.from(orderMenu.getKey()), orderMenu.getValue()) } ```
@@ -0,0 +1,35 @@ +package christmas.dto; + +import christmas.domain.order.OrderMenu; +import christmas.domain.order.OrderMenus; +import java.util.Map; +import java.util.stream.Collectors; + +public class OrderMenusDto { + + private final Map<String, Integer> menus; + + private OrderMenusDto(OrderMenus orderMenus) { + this.menus = orderMenus.getOrderMenus() + .stream() + .collect(Collectors.toMap( + orderMenu -> orderMenu.getMenu().getName(), + OrderMenu::getQuantity)); + } + + private OrderMenusDto(Map<String, Integer> inputOrderMenus) { + this.menus = inputOrderMenus; + } + + public static OrderMenusDto from(OrderMenus orderMenus) { + return new OrderMenusDto(orderMenus); + } + + public static OrderMenusDto from(Map<String, Integer> inputOrderMenus) { + return new OrderMenusDto(inputOrderMenus); + } + + public Map<String, Integer> getMenus() { + return menus; + } +}
Java
OrderMenuDto ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ•ด๋‹น ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์ •์ ์œผ๋กœ ์„ ์–ธํ•˜์…จ๊ตฐ์š”! ์ฐธ์‹ ํ•œ ๋ฐฉ๋ฒ•์ด๋„ค์š” ๐Ÿซข ์ €๋Š” ์žฌ๋ชจ๋‹˜ ์ฝ”๋“œ๋กœ ์น˜๋ฉด OrderMenus ๊ฐ์ฒด ๋‚ด๋ถ€์— toDto() ๋ฉ”์„œ๋“œ๋กœ OrderMenusDto ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ–ˆ๋Š”๋ฐ, ์žฌ๋ชจ๋‹˜์€ ์–ด๋–ค ์ด์ ์„ ์ƒ๊ฐํ•˜์…”์„œ ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•˜์…จ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! ๐Ÿ˜Š (์ €๋„ ๋‹ค์Œ์— ์จ๋จน์–ด ๋ณด๋ ค๊ตฌ์š” :-))
@@ -0,0 +1,92 @@ +package constants; + +public class Menus { + + public enum Appetizer { + + BUTTON_MUSHROOM_SOUP("์–‘์†ก์ด์ˆ˜ํ”„",6000), + TAPAS("ํƒ€ํŒŒ์Šค",5500), + CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ",8000); + + private final String dish; + private final int price; + + Appetizer(String dish,int price) { + this.dish=dish; + this.price=price; + } + + public String appetizer() { + return dish; + } + public int price() { + return price; + } + } + + public enum MainMenu { + + TBONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ",55000), + BARBECUE_RIP("๋ฐ”๋น„ํ๋ฆฝ",54000), + SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€",35000), + CHRISTRMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€",25000); + + private final String dish; + private final int price; + + MainMenu(String dish,int price) { + this.dish=dish; + this.price=price; + } + + public String main() { + return dish; + } + public int price() { + return price; + } + } + + public enum Dessert { + + CHOCOLATE_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ",15000), + ICECREAM("์•„์ด์Šคํฌ๋ฆผ",5000); + + private final String dish; + private final int price; + + Dessert(String dish,int price) { + this.dish=dish; + this.price=price; + } + + public String dessert() { + return dish; + } + public int price() { + return price; + } + } + + public enum Beverage { + + ZERO_COLA("์ œ๋กœ์ฝœ๋ผ",3000), + RED_WINE("๋ ˆ๋“œ์™€์ธ",60000), + CHAMPAGNE("์ƒดํŽ˜์ธ",25000); + + private final String dish; + private final int price; + + Beverage(String dish,int price) { + this.dish=dish; + this.price=price; + } + + public String beverage() { + return dish; + } + public int price() { + return price; + } + } +}
Java
์—ด๊ฑฐ์ฒด๋งˆ๋‹ค ์ค‘๋ณต ์ฝ”๋“œ๊ฐ€ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š”! ์ฐจ๋ผ๋ฆฌ ์—ด๊ฑฐ์ฒด์— `MenuType`๊ฐ™์€ ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ•˜๋‚˜๋กœ ํ†ต์ผํ•˜๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,25 @@ +package constants; + +import creator.ObjectCreator; + +public class Constants { + + //input ์˜ค๋ฅ˜ + public final static String INVALID_DATE_OUTPUT=ErrorMessages.INVALID_DATE_OUTPUT.detected(); + public final static String INVALID_MENU_OUTPUT=ErrorMessages.INVALID_MENU_OUTPUT.detected(); + + //ํ• ์ธ๊ฐ€์ •๋ณด + public final static int D_DAY_DISCOUNT_BASIC=-1000; + public final static int D_DAY_DISCOUNT_PER_DAY=-100; + public final static int WEEK_DISCOUNT=-2023; + public final static int SPECIAL_DISCOUNT=-1000; + public final static int DISCOUNT_TYPES_CNT=4; + + public final static int DAYS_OF_A_WEEK=7; + public final static int SUNDAY_METER=3; + public final static int SATURDAY_METER=2; + public final static int FRIDAY_METER=1; + public final static int DATE_OF_CHRISTMAS=25; + + public final static ObjectCreator CREATE=new ObjectCreator(); +}
Java
์ด ๊ฐ’์„ `Constants`์— ํฌํ•จ์‹œํ‚จ ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”? ์™ธ๋ถ€์—์„œ `ErrorMessages`๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•ด๋„ ์ถฉ๋ถ„ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,40 @@ +package creator; + +import data.EventData; +import features.Controller; +import features.Input; +import features.Statistics; +import features.Validator; +import view.InputView; +import view.OutputView; + +public class ObjectCreator { + + public EventData dto() { + return new EventData(); + } + + public Input input() { + return new Input(); + } + + public Validator valid() { + return new Validator(); + } + + public InputView reader() { + return new InputView(); + } + + public OutputView announcer() { + return new OutputView(); + } + + public Statistics stat(EventData dto) { + return new Statistics(dto); + } + + public Controller player() { + return new Controller(); + } +}
Java
๋ฉ”์„œ๋“œ๋ช…์ด ๋„ˆ๋ฌด ์ถ•์•ฝ๋œ ๊ฒƒ ๊ฐ™์•„์š”! `create` + ๊ฐ์ฒด๋ช… ๊ณผ ๊ฐ™์ด ์˜๋ฏธ๊ฐ€ ๋ช…ํ™•ํ•œ ๋„ค์ด๋ฐ์€ ์–ด๋–จ๊นŒ์š”? (e.g. `cretaeOutputView`)
@@ -0,0 +1,163 @@ +package features; + +import java.util.Map; + +import constants.Menus.Appetizer; +import constants.Menus.Beverage; +import constants.Menus.Dessert; +import constants.Menus.MainMenu; +import data.EventData; + +import static constants.Constants.*; + +public class Statistics { + + public Statistics(EventData dto) { + this.visitDate=dto.getVisitDate(); + this.orders=dto.getOrders(); + this.day_week_meter=visitDate%DAYS_OF_A_WEEK; + } + + private Map<String, Integer> orders; + private int visitDate; + private int day_week_meter; + + //์ฒ˜๋ฆฌ์š”๊ตฌ ๋ฐ์ดํ„ฐ + private int totalCostOrigin; + private int totalDiscount; + private int[] discountDetails=new int[5]; + + private int dessertOrderedCnt; + private int mainMenuOrderedCnt; + + //์ฆ์ •๋ฉ”๋‰ด + public boolean deserveGift() { + if(totalCostOrigin>=120000) { + return true; + } + return false; + } + + //ํ• ์ธ์ „ ์ด์ฃผ๋ฌธ๊ธˆ์•ก + public int getTotalCostOrigin() { + for(Map.Entry<String, Integer> order:orders.entrySet()) { + getSum(order.getKey(), order.getValue()); + } + return totalCostOrigin; + } + + private void getSum(String dish,int servings) { + appetizerCost(dish, servings); + mainMenuCost(dish, servings); + dessertCost(dish, servings); + beverageCost(dish, servings); + } + + private void appetizerCost(String dish,int servings) { + for(Appetizer ap:Appetizer.values()) { + if(ap.appetizer().equals(dish)) { + totalCostOrigin+=servings*ap.price(); + } + } + } + + private void mainMenuCost(String dish,int servings) { + for(MainMenu ma:MainMenu.values()) { + if(ma.main().equals(dish)) { + totalCostOrigin+=servings*ma.price(); + mainMenuOrderedCnt+=servings; + } + } + } + + private void dessertCost(String dish,int servings) { + for(Dessert de:Dessert.values()) { + if(de.dessert().equals(dish)) { + totalCostOrigin+=servings*de.price(); + dessertOrderedCnt+=servings; + } + } + } + + private void beverageCost(String dish,int servings) { + for(Beverage be:Beverage.values()) { + if(be.beverage().equals(dish)) { + totalCostOrigin+=servings*be.price(); + } + } + } + + //ํ˜œํƒ๋‚ด์—ญ + public int[] getDiscountDetails() { + discountDetails[0]=d_dayDiscount(); + discountDetails[1]=weekDiscount(); + discountDetails[2]=specialDiscount(); + discountDetails[3]=giftDiscount(); + + return discountDetails; + } + + private int d_dayDiscount() { + if(visitDate<=25) { + return D_DAY_DISCOUNT_BASIC+(visitDate-1)*D_DAY_DISCOUNT_PER_DAY; + } + return 0; + } + + private int weekDiscount() { + if(isWeekend()) { + discountDetails[4]=1; + return mainMenuOrderedCnt*WEEK_DISCOUNT; + } + discountDetails[4]=0; + return dessertOrderedCnt*WEEK_DISCOUNT; + } + + private boolean isWeekend() { + if(day_week_meter==FRIDAY_METER||day_week_meter==SATURDAY_METER) { + return true; + } + return false; + } + + private int specialDiscount() { + if(day_week_meter==SUNDAY_METER||visitDate==DATE_OF_CHRISTMAS) { + return SPECIAL_DISCOUNT; + } + return 0; + } + + private int giftDiscount() { + if(deserveGift()) { + return -Beverage.CHAMPAGNE.price(); + } + return 0; + } + + //์ดํ˜œํƒ๊ธˆ์•ก + public int getTotalDiscount() { + for(int i=0;i<DISCOUNT_TYPES_CNT;i++) { + totalDiscount+=discountDetails[i]; + } + return totalDiscount; + } + + //ํ• ์ธํ›„ ์˜ˆ์ƒ๊ฒฐ์ œ๊ธˆ์•ก + public int getResultCost() { + return totalCostOrigin+totalDiscount; + } + + //์ด๋ฒคํŠธ ๋ฐฐ์ง€ + public String badgeReceivable() { + if(-totalDiscount>=5000&&-totalDiscount<10000) { + return "๋ณ„"; + } + if(-totalDiscount>=10000&&-totalDiscount<20000) { + return "ํŠธ๋ฆฌ"; + } + if(-totalDiscount>=20000) { + return "์‚ฐํƒ€"; + } + return "์—†์Œ"; + } +}
Java
์ˆ˜์‹์„ ๋ฐ˜ํ™˜ํ•ด๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”! ```java return totalCostOrigin >= 120000; ```
@@ -0,0 +1,163 @@ +package features; + +import java.util.Map; + +import constants.Menus.Appetizer; +import constants.Menus.Beverage; +import constants.Menus.Dessert; +import constants.Menus.MainMenu; +import data.EventData; + +import static constants.Constants.*; + +public class Statistics { + + public Statistics(EventData dto) { + this.visitDate=dto.getVisitDate(); + this.orders=dto.getOrders(); + this.day_week_meter=visitDate%DAYS_OF_A_WEEK; + } + + private Map<String, Integer> orders; + private int visitDate; + private int day_week_meter; + + //์ฒ˜๋ฆฌ์š”๊ตฌ ๋ฐ์ดํ„ฐ + private int totalCostOrigin; + private int totalDiscount; + private int[] discountDetails=new int[5]; + + private int dessertOrderedCnt; + private int mainMenuOrderedCnt; + + //์ฆ์ •๋ฉ”๋‰ด + public boolean deserveGift() { + if(totalCostOrigin>=120000) { + return true; + } + return false; + } + + //ํ• ์ธ์ „ ์ด์ฃผ๋ฌธ๊ธˆ์•ก + public int getTotalCostOrigin() { + for(Map.Entry<String, Integer> order:orders.entrySet()) { + getSum(order.getKey(), order.getValue()); + } + return totalCostOrigin; + } + + private void getSum(String dish,int servings) { + appetizerCost(dish, servings); + mainMenuCost(dish, servings); + dessertCost(dish, servings); + beverageCost(dish, servings); + } + + private void appetizerCost(String dish,int servings) { + for(Appetizer ap:Appetizer.values()) { + if(ap.appetizer().equals(dish)) { + totalCostOrigin+=servings*ap.price(); + } + } + } + + private void mainMenuCost(String dish,int servings) { + for(MainMenu ma:MainMenu.values()) { + if(ma.main().equals(dish)) { + totalCostOrigin+=servings*ma.price(); + mainMenuOrderedCnt+=servings; + } + } + } + + private void dessertCost(String dish,int servings) { + for(Dessert de:Dessert.values()) { + if(de.dessert().equals(dish)) { + totalCostOrigin+=servings*de.price(); + dessertOrderedCnt+=servings; + } + } + } + + private void beverageCost(String dish,int servings) { + for(Beverage be:Beverage.values()) { + if(be.beverage().equals(dish)) { + totalCostOrigin+=servings*be.price(); + } + } + } + + //ํ˜œํƒ๋‚ด์—ญ + public int[] getDiscountDetails() { + discountDetails[0]=d_dayDiscount(); + discountDetails[1]=weekDiscount(); + discountDetails[2]=specialDiscount(); + discountDetails[3]=giftDiscount(); + + return discountDetails; + } + + private int d_dayDiscount() { + if(visitDate<=25) { + return D_DAY_DISCOUNT_BASIC+(visitDate-1)*D_DAY_DISCOUNT_PER_DAY; + } + return 0; + } + + private int weekDiscount() { + if(isWeekend()) { + discountDetails[4]=1; + return mainMenuOrderedCnt*WEEK_DISCOUNT; + } + discountDetails[4]=0; + return dessertOrderedCnt*WEEK_DISCOUNT; + } + + private boolean isWeekend() { + if(day_week_meter==FRIDAY_METER||day_week_meter==SATURDAY_METER) { + return true; + } + return false; + } + + private int specialDiscount() { + if(day_week_meter==SUNDAY_METER||visitDate==DATE_OF_CHRISTMAS) { + return SPECIAL_DISCOUNT; + } + return 0; + } + + private int giftDiscount() { + if(deserveGift()) { + return -Beverage.CHAMPAGNE.price(); + } + return 0; + } + + //์ดํ˜œํƒ๊ธˆ์•ก + public int getTotalDiscount() { + for(int i=0;i<DISCOUNT_TYPES_CNT;i++) { + totalDiscount+=discountDetails[i]; + } + return totalDiscount; + } + + //ํ• ์ธํ›„ ์˜ˆ์ƒ๊ฒฐ์ œ๊ธˆ์•ก + public int getResultCost() { + return totalCostOrigin+totalDiscount; + } + + //์ด๋ฒคํŠธ ๋ฐฐ์ง€ + public String badgeReceivable() { + if(-totalDiscount>=5000&&-totalDiscount<10000) { + return "๋ณ„"; + } + if(-totalDiscount>=10000&&-totalDiscount<20000) { + return "ํŠธ๋ฆฌ"; + } + if(-totalDiscount>=20000) { + return "์‚ฐํƒ€"; + } + return "์—†์Œ"; + } +}
Java
๋ฐฐ์ง€ ๋ฐœ๊ธ‰์€ ๋‹ค๋ฅธ ๊ฐ์ฒด์—์„œ ์ฒ˜๋ฆฌํ•ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,163 @@ +package features; + +import java.util.Map; + +import constants.Menus.Appetizer; +import constants.Menus.Beverage; +import constants.Menus.Dessert; +import constants.Menus.MainMenu; +import data.EventData; + +import static constants.Constants.*; + +public class Statistics { + + public Statistics(EventData dto) { + this.visitDate=dto.getVisitDate(); + this.orders=dto.getOrders(); + this.day_week_meter=visitDate%DAYS_OF_A_WEEK; + } + + private Map<String, Integer> orders; + private int visitDate; + private int day_week_meter; + + //์ฒ˜๋ฆฌ์š”๊ตฌ ๋ฐ์ดํ„ฐ + private int totalCostOrigin; + private int totalDiscount; + private int[] discountDetails=new int[5]; + + private int dessertOrderedCnt; + private int mainMenuOrderedCnt; + + //์ฆ์ •๋ฉ”๋‰ด + public boolean deserveGift() { + if(totalCostOrigin>=120000) { + return true; + } + return false; + } + + //ํ• ์ธ์ „ ์ด์ฃผ๋ฌธ๊ธˆ์•ก + public int getTotalCostOrigin() { + for(Map.Entry<String, Integer> order:orders.entrySet()) { + getSum(order.getKey(), order.getValue()); + } + return totalCostOrigin; + } + + private void getSum(String dish,int servings) { + appetizerCost(dish, servings); + mainMenuCost(dish, servings); + dessertCost(dish, servings); + beverageCost(dish, servings); + } + + private void appetizerCost(String dish,int servings) { + for(Appetizer ap:Appetizer.values()) { + if(ap.appetizer().equals(dish)) { + totalCostOrigin+=servings*ap.price(); + } + } + } + + private void mainMenuCost(String dish,int servings) { + for(MainMenu ma:MainMenu.values()) { + if(ma.main().equals(dish)) { + totalCostOrigin+=servings*ma.price(); + mainMenuOrderedCnt+=servings; + } + } + } + + private void dessertCost(String dish,int servings) { + for(Dessert de:Dessert.values()) { + if(de.dessert().equals(dish)) { + totalCostOrigin+=servings*de.price(); + dessertOrderedCnt+=servings; + } + } + } + + private void beverageCost(String dish,int servings) { + for(Beverage be:Beverage.values()) { + if(be.beverage().equals(dish)) { + totalCostOrigin+=servings*be.price(); + } + } + } + + //ํ˜œํƒ๋‚ด์—ญ + public int[] getDiscountDetails() { + discountDetails[0]=d_dayDiscount(); + discountDetails[1]=weekDiscount(); + discountDetails[2]=specialDiscount(); + discountDetails[3]=giftDiscount(); + + return discountDetails; + } + + private int d_dayDiscount() { + if(visitDate<=25) { + return D_DAY_DISCOUNT_BASIC+(visitDate-1)*D_DAY_DISCOUNT_PER_DAY; + } + return 0; + } + + private int weekDiscount() { + if(isWeekend()) { + discountDetails[4]=1; + return mainMenuOrderedCnt*WEEK_DISCOUNT; + } + discountDetails[4]=0; + return dessertOrderedCnt*WEEK_DISCOUNT; + } + + private boolean isWeekend() { + if(day_week_meter==FRIDAY_METER||day_week_meter==SATURDAY_METER) { + return true; + } + return false; + } + + private int specialDiscount() { + if(day_week_meter==SUNDAY_METER||visitDate==DATE_OF_CHRISTMAS) { + return SPECIAL_DISCOUNT; + } + return 0; + } + + private int giftDiscount() { + if(deserveGift()) { + return -Beverage.CHAMPAGNE.price(); + } + return 0; + } + + //์ดํ˜œํƒ๊ธˆ์•ก + public int getTotalDiscount() { + for(int i=0;i<DISCOUNT_TYPES_CNT;i++) { + totalDiscount+=discountDetails[i]; + } + return totalDiscount; + } + + //ํ• ์ธํ›„ ์˜ˆ์ƒ๊ฒฐ์ œ๊ธˆ์•ก + public int getResultCost() { + return totalCostOrigin+totalDiscount; + } + + //์ด๋ฒคํŠธ ๋ฐฐ์ง€ + public String badgeReceivable() { + if(-totalDiscount>=5000&&-totalDiscount<10000) { + return "๋ณ„"; + } + if(-totalDiscount>=10000&&-totalDiscount<20000) { + return "ํŠธ๋ฆฌ"; + } + if(-totalDiscount>=20000) { + return "์‚ฐํƒ€"; + } + return "์—†์Œ"; + } +}
Java
๋„ˆ๋ฌด ๋งŽ์€ ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”! ์ผ๋ถ€ ๊ธฐ๋Šฅ๋“ค์€ ๋‹ค๋ฅธ ๊ฐ์ฒด์— ์œ„์ž„ํ•˜๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,17 @@ +package christmas.domain.menu + +import christmas.exception.OrdersException + +data class MenuCount(private val count: Int) { + init { + require(count >= 1) { OrdersException.INVALID.message } + } + + fun getCount() = count + + override fun toString(): String = "%,d$UNIT_SUFFIX".format(count) + + companion object { + private const val UNIT_SUFFIX = "๊ฐœ" + } +} \ No newline at end of file
Kotlin
val ์„ ์–ธ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ private๋กœ ์ œํ•œํ•˜์ง€ ์•Š์•„๋„ ๊ฐ’ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ• ํ…๋ฐ, ํ˜น์‹œ private๋กœ ์„ ์–ธํ•œ ์ด์œ ๊ฐ€ ๋”ฐ๋กœ ์žˆ์„๊นŒ์š”..?!