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๋ก ์ ์ธํ ์ด์ ๊ฐ ๋ฐ๋ก ์์๊น์..?! |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.