code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,47 @@
+package christmas.domain.gift;
+
+import christmas.domain.plan.Menu;
+import java.util.Map;
+import java.util.Objects;
+
+public enum Gift {
+ EVENT_GIFT(Map.of(Menu.CHAMPAGNE, 1)),
+ NOTHING(Map.of());
+
+ private static final int MIN_GIFT_PRICE = 120_000;
+
+ private final Map<Menu, Integer> menuToCount;
+
+ Gift(Map<Menu, Integer> menuToCount) {
+ this.menuToCount = Objects.requireNonNull(menuToCount);
+ }
+
+ public static Gift from(int totalPrice) {
+ if (isOverThanStandardPrice(totalPrice)) {
+ return EVENT_GIFT;
+ }
+ return NOTHING;
+ }
+
+ private static boolean isOverThanStandardPrice(int totalPrice) {
+ return totalPrice >= MIN_GIFT_PRICE;
+ }
+
+ public int calculateBenefitPrice() {
+ return menuToCount.keySet().stream()
+ .mapToInt(menu -> calculatePartOfPrice(menu, menuToCount.get(menu)))
+ .sum();
+ }
+
+ public int calculatePartOfPrice(Menu menu, int count) {
+ return menu.getPrice() * count;
+ }
+
+ public boolean isEmpty() {
+ return menuToCount.isEmpty();
+ }
+
+ public Map<Menu, Integer> getMenuToCount() {
+ return Map.copyOf(menuToCount);
+ }
+} | Java | ์ด ๋ถ๋ถ์ ์ฆ์ ํ์ด ์ฌ๋ฌ๊ฐ๊ฐ ๋ ์ ์๋ค๋ ํ์ฅ์ฑ์ ๊ณ ๋ คํ์ ๊ฑด๊ฐ์?๐ |
@@ -0,0 +1,21 @@
+package christmas.view;
+
+import christmas.domain.badge.Badge;
+import java.util.Map;
+
+class BadgeView {
+
+ private static final Map<Badge, String> BADGE_TO_MESSAGE = Map.of(
+ Badge.SANTA, "์ฐํ", Badge.TREE, "ํธ๋ฆฌ",
+ Badge.STAR, "๋ณ", Badge.NOTHING, "์์");
+
+ private BadgeView() {
+ }
+
+ public static String findView(Badge badge) {
+ if (!BADGE_TO_MESSAGE.containsKey(badge)) {
+ throw new IllegalArgumentException("Badge not enrolled at view; input : " + badge);
+ }
+ return BADGE_TO_MESSAGE.get(badge);
+ }
+} | Java | ๋ฆฌ๋ทฐ์ด๋์ ์ฝ๋๋ฅผ ๋ณด๊ณ ๊ถ๊ธํ๊ฒ ์๊ฒจ ์ด๋ป๊ฒ ์๊ฐํ์๋์ง ์ฌ์ญค๋ด
๋๋ค!
3์ฃผ์ฐจ ํผ๋๋ฐฑ ์ค "view๋ฅผ ์ํ ๋ก์ง์ ๋ถ๋ฆฌํ๋ค"๊ฐ ๋ชจ๋ธ์์๋ ๋ทฐ์ ๊ด๋ จ๋ ๊ฒ๋ค ์๋ฌด๊ฒ๋ ๊ฐ์ง ์ ์๋ ๊ฑธ ์๋ฏธํ๋ฉด ์ด ์ฒ๋ผ ๋ทฐ์์ ๋๋ฉ์ธ์ ๋ํด ๋ง์ ๊ฑธ ์์์ผํ๋ ์ ๋ ์๊ธธ ์๋ ์์ง ์์๊น์? |
@@ -0,0 +1,79 @@
+package christmas.controller;
+
+import christmas.domain.badge.Badge;
+import christmas.domain.discount.DiscountDetails;
+import christmas.domain.discount.TotalDiscountEvent;
+import christmas.domain.gift.Gift;
+import christmas.domain.plan.EventDate;
+import christmas.domain.plan.Menu;
+import christmas.domain.plan.Order;
+import christmas.domain.plan.Plan;
+import christmas.dto.PlanResultDto;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+import java.util.Map;
+import java.util.function.Supplier;
+
+public class ChristmasPlanerController {
+
+ private final TotalDiscountEvent discountEvent = TotalDiscountEvent.createDecemberEvent();
+ private final InputView inputView = new InputView();
+ private final OutputView outputView = new OutputView();
+
+ public void run() {
+ startApplication();
+ Plan plan = inputPlan();
+ PlanResultDto planResult = applyEvents(plan);
+ printEventResult(planResult);
+ }
+
+ private void startApplication() {
+ outputView.printApplicationTitle();
+ }
+
+ private Plan inputPlan() {
+ EventDate date = readRepeatedlyUntilNoException(this::readDate);
+ Order order = readRepeatedlyUntilNoException(this::readOrder);
+ return Plan.of(date, order);
+ }
+
+ private <T> T readRepeatedlyUntilNoException(Supplier<T> supplier) {
+ try {
+ return supplier.get();
+ } catch (IllegalArgumentException exception) {
+ outputView.printExceptionMessage(exception);
+ return readRepeatedlyUntilNoException(supplier);
+ }
+ }
+
+ private EventDate readDate() {
+ int date = inputView.readDate();
+ return EventDate.from(date);
+ }
+
+ private Order readOrder() {
+ Map<Menu, Integer> order = inputView.readOrder();
+ return Order.from(order);
+ }
+
+ private PlanResultDto applyEvents(Plan plan) {
+ DiscountDetails discountDetails = discountEvent.makeDiscountDetails(plan);
+ Gift gift = Gift.from(plan.calculateTotalPrice());
+ int totalBenefitPrice = calculateTotalBenefitPrice(discountDetails, gift);
+ Badge badge = Badge.from(totalBenefitPrice);
+
+ return PlanResultDto.builder()
+ .plan(plan).discountDetails(discountDetails)
+ .gift(gift).badge(badge).build();
+ }
+
+ private int calculateTotalBenefitPrice(DiscountDetails discountDetails, Gift gift) {
+ int totalDiscountPrice = discountDetails.calculateTotalDiscountPrice();
+ int giftBenefitPrice = gift.calculateBenefitPrice();
+ return totalDiscountPrice + giftBenefitPrice;
+ }
+
+ private void printEventResult(PlanResultDto planResult) {
+ outputView.printPlanResult(planResult);
+ }
+} | Java | ์ ๋ ์ด๋ฐ ๋๋์ธ๋ฐ์.
์ ๋ ๋ทฐ๋ก๋ถํฐ ์
๋ ฅ์ ์์ฒญํ๋ ์ปจํธ๋กค๋ฌ๋ค์ ์ปจํธ๋กคํ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ๋ง๋ค์์ต๋๋ค ๐ |
@@ -0,0 +1,79 @@
+package christmas.controller;
+
+import christmas.domain.badge.Badge;
+import christmas.domain.discount.DiscountDetails;
+import christmas.domain.discount.TotalDiscountEvent;
+import christmas.domain.gift.Gift;
+import christmas.domain.plan.EventDate;
+import christmas.domain.plan.Menu;
+import christmas.domain.plan.Order;
+import christmas.domain.plan.Plan;
+import christmas.dto.PlanResultDto;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+import java.util.Map;
+import java.util.function.Supplier;
+
+public class ChristmasPlanerController {
+
+ private final TotalDiscountEvent discountEvent = TotalDiscountEvent.createDecemberEvent();
+ private final InputView inputView = new InputView();
+ private final OutputView outputView = new OutputView();
+
+ public void run() {
+ startApplication();
+ Plan plan = inputPlan();
+ PlanResultDto planResult = applyEvents(plan);
+ printEventResult(planResult);
+ }
+
+ private void startApplication() {
+ outputView.printApplicationTitle();
+ }
+
+ private Plan inputPlan() {
+ EventDate date = readRepeatedlyUntilNoException(this::readDate);
+ Order order = readRepeatedlyUntilNoException(this::readOrder);
+ return Plan.of(date, order);
+ }
+
+ private <T> T readRepeatedlyUntilNoException(Supplier<T> supplier) {
+ try {
+ return supplier.get();
+ } catch (IllegalArgumentException exception) {
+ outputView.printExceptionMessage(exception);
+ return readRepeatedlyUntilNoException(supplier);
+ }
+ }
+
+ private EventDate readDate() {
+ int date = inputView.readDate();
+ return EventDate.from(date);
+ }
+
+ private Order readOrder() {
+ Map<Menu, Integer> order = inputView.readOrder();
+ return Order.from(order);
+ }
+
+ private PlanResultDto applyEvents(Plan plan) {
+ DiscountDetails discountDetails = discountEvent.makeDiscountDetails(plan);
+ Gift gift = Gift.from(plan.calculateTotalPrice());
+ int totalBenefitPrice = calculateTotalBenefitPrice(discountDetails, gift);
+ Badge badge = Badge.from(totalBenefitPrice);
+
+ return PlanResultDto.builder()
+ .plan(plan).discountDetails(discountDetails)
+ .gift(gift).badge(badge).build();
+ }
+
+ private int calculateTotalBenefitPrice(DiscountDetails discountDetails, Gift gift) {
+ int totalDiscountPrice = discountDetails.calculateTotalDiscountPrice();
+ int giftBenefitPrice = gift.calculateBenefitPrice();
+ return totalDiscountPrice + giftBenefitPrice;
+ }
+
+ private void printEventResult(PlanResultDto planResult) {
+ outputView.printPlanResult(planResult);
+ }
+} | Java | ์ค DTO์ ๋น๋ ํจํด์ ์ฌ์ฉํ์
จ๋ค์ ์คํ๋ง ์ด๋
ธํ
์ด์
์์ ์ฌ์ฉํ๋ ๊ฑด๋ฐ ํ๋ฅญํฉ๋๋ค! |
@@ -0,0 +1,27 @@
+package christmas.domain.discount.policy;
+
+import christmas.domain.plan.EventSchedule;
+import christmas.domain.plan.Plan;
+import java.util.function.IntUnaryOperator;
+
+public class ChristmasDDayDiscount extends DiscountPolicy {
+
+ private static final int START_DATE = 1;
+ private static final int END_DATE = 25;
+ private static final EventSchedule EVENT_SCHEDULE = EventSchedule.of(START_DATE, END_DATE);
+
+ private static final int DEFAULT_DISCOUNT_AMOUNT = 1_000;
+ private static final int INCREASING_DISCOUNT_AMOUNT = 100;
+ private static final IntUnaryOperator DISCOUNT_FUNCTION_BY_DATE
+ = date -> DEFAULT_DISCOUNT_AMOUNT + (date - 1) * INCREASING_DISCOUNT_AMOUNT;
+
+ @Override
+ boolean isSatisfyPrecondition(Plan plan) {
+ return plan.isContainedBy(EVENT_SCHEDULE);
+ }
+
+ @Override
+ int calculateDiscountAmount(Plan plan) {
+ return plan.calculateByDate(DISCOUNT_FUNCTION_BY_DATE);
+ }
+} | Java | ์ด๋ฒ ๊ณผ์ ์์ ๋๋ค๋ฅผ ๋ง์ด ์ฐ์ตํด๋ณด๊ณ ์ถ์๋๋ฐ {() -> } ์ธ์๋ ์ด๋ ๊ฒ ์ธ ์ ์๊ฒ ๊ตฐ์! ์ข์ ์ ๋ณด ์ป์ด๊ฐ๋๋ค! |
@@ -0,0 +1,29 @@
+package christmas.view;
+
+import christmas.exception.DateInputException;
+import christmas.exception.OnlyDrinkMenuException;
+import christmas.exception.OrderInputException;
+import christmas.exception.TotalMenuCountException;
+import java.util.Map;
+
+class ErrorMessageView {
+
+ private static final String ERROR_MESSAGE_PREFIX = "[ERROR] ";
+ private static final Map<Class<? extends IllegalArgumentException>, String> EXCEPTION_TO_ERROR_MESSAGE
+ = Map.of(DateInputException.class, "์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.",
+ OrderInputException.class, "์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.",
+ OnlyDrinkMenuException.class, "์ฃผ๋ฌธ์ ์๋ฃ๋ก๋ง ๊ตฌ์ฑ๋ ์ ์์ต๋๋ค",
+ TotalMenuCountException.class, "์ด ๋ฉ๋ด ๊ฐ์๋ 20๊ฐ๋ฅผ ์ด๊ณผํ ์ ์์ต๋๋ค");
+ private static final String DEFAULT_ERROR_MESSAGE = "์์์น ๋ชปํ ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค. ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํ์ธ์.";
+
+ private ErrorMessageView() {
+ }
+
+ public static String constructMessage(IllegalArgumentException exception) {
+ return ERROR_MESSAGE_PREFIX.concat(findErrorMessage(exception));
+ }
+
+ private static String findErrorMessage(IllegalArgumentException exception) {
+ return EXCEPTION_TO_ERROR_MESSAGE.getOrDefault(exception.getClass(), DEFAULT_ERROR_MESSAGE);
+ }
+} | Java | ๊ฐ ์์ธ ๋ฉ์์ง๋ฅผ ๋งต์ผ๋ก ๋ด๋ ๋ฐฉ๋ฒ ์ ๋ง ๊ธฐ๋ฅ์ฐจ๋ค์... |
@@ -0,0 +1,79 @@
+package christmas.controller;
+
+import christmas.domain.badge.Badge;
+import christmas.domain.discount.DiscountDetails;
+import christmas.domain.discount.TotalDiscountEvent;
+import christmas.domain.gift.Gift;
+import christmas.domain.plan.EventDate;
+import christmas.domain.plan.Menu;
+import christmas.domain.plan.Order;
+import christmas.domain.plan.Plan;
+import christmas.dto.PlanResultDto;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+import java.util.Map;
+import java.util.function.Supplier;
+
+public class ChristmasPlanerController {
+
+ private final TotalDiscountEvent discountEvent = TotalDiscountEvent.createDecemberEvent();
+ private final InputView inputView = new InputView();
+ private final OutputView outputView = new OutputView();
+
+ public void run() {
+ startApplication();
+ Plan plan = inputPlan();
+ PlanResultDto planResult = applyEvents(plan);
+ printEventResult(planResult);
+ }
+
+ private void startApplication() {
+ outputView.printApplicationTitle();
+ }
+
+ private Plan inputPlan() {
+ EventDate date = readRepeatedlyUntilNoException(this::readDate);
+ Order order = readRepeatedlyUntilNoException(this::readOrder);
+ return Plan.of(date, order);
+ }
+
+ private <T> T readRepeatedlyUntilNoException(Supplier<T> supplier) {
+ try {
+ return supplier.get();
+ } catch (IllegalArgumentException exception) {
+ outputView.printExceptionMessage(exception);
+ return readRepeatedlyUntilNoException(supplier);
+ }
+ }
+
+ private EventDate readDate() {
+ int date = inputView.readDate();
+ return EventDate.from(date);
+ }
+
+ private Order readOrder() {
+ Map<Menu, Integer> order = inputView.readOrder();
+ return Order.from(order);
+ }
+
+ private PlanResultDto applyEvents(Plan plan) {
+ DiscountDetails discountDetails = discountEvent.makeDiscountDetails(plan);
+ Gift gift = Gift.from(plan.calculateTotalPrice());
+ int totalBenefitPrice = calculateTotalBenefitPrice(discountDetails, gift);
+ Badge badge = Badge.from(totalBenefitPrice);
+
+ return PlanResultDto.builder()
+ .plan(plan).discountDetails(discountDetails)
+ .gift(gift).badge(badge).build();
+ }
+
+ private int calculateTotalBenefitPrice(DiscountDetails discountDetails, Gift gift) {
+ int totalDiscountPrice = discountDetails.calculateTotalDiscountPrice();
+ int giftBenefitPrice = gift.calculateBenefitPrice();
+ return totalDiscountPrice + giftBenefitPrice;
+ }
+
+ private void printEventResult(PlanResultDto planResult) {
+ outputView.printPlanResult(planResult);
+ }
+} | Java | Plan ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ EventDate์ Order ๊ฐ์ฒด๋ฅผ ํ ๋ฒ์ ๋ฃ์ด์ ์์ฑํ๋ ๊ฒ์ด ๊น๋ํ๊ธฐ๋ ํ์ง๋ง EventDate์ Order๋ฅผ ์์ฑ์ ๊ฒ์ฆํ๋๋ฐ ๋ฐฉ๋ฌธ ๋ ์ง์ ์ฃผ๋ฌธ ๋๋ฉ์ธ์ด ์ปจํธ๋กค๋ฌ์ ๋
ธ์ถ๋๋๊ฒ์ด ์๋๊ฐ ์๊ฐ์ด ๋๋๋ฐ์, ์ด ๋ถ๋ถ์ ์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,79 @@
+package christmas.controller;
+
+import christmas.domain.badge.Badge;
+import christmas.domain.discount.DiscountDetails;
+import christmas.domain.discount.TotalDiscountEvent;
+import christmas.domain.gift.Gift;
+import christmas.domain.plan.EventDate;
+import christmas.domain.plan.Menu;
+import christmas.domain.plan.Order;
+import christmas.domain.plan.Plan;
+import christmas.dto.PlanResultDto;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+import java.util.Map;
+import java.util.function.Supplier;
+
+public class ChristmasPlanerController {
+
+ private final TotalDiscountEvent discountEvent = TotalDiscountEvent.createDecemberEvent();
+ private final InputView inputView = new InputView();
+ private final OutputView outputView = new OutputView();
+
+ public void run() {
+ startApplication();
+ Plan plan = inputPlan();
+ PlanResultDto planResult = applyEvents(plan);
+ printEventResult(planResult);
+ }
+
+ private void startApplication() {
+ outputView.printApplicationTitle();
+ }
+
+ private Plan inputPlan() {
+ EventDate date = readRepeatedlyUntilNoException(this::readDate);
+ Order order = readRepeatedlyUntilNoException(this::readOrder);
+ return Plan.of(date, order);
+ }
+
+ private <T> T readRepeatedlyUntilNoException(Supplier<T> supplier) {
+ try {
+ return supplier.get();
+ } catch (IllegalArgumentException exception) {
+ outputView.printExceptionMessage(exception);
+ return readRepeatedlyUntilNoException(supplier);
+ }
+ }
+
+ private EventDate readDate() {
+ int date = inputView.readDate();
+ return EventDate.from(date);
+ }
+
+ private Order readOrder() {
+ Map<Menu, Integer> order = inputView.readOrder();
+ return Order.from(order);
+ }
+
+ private PlanResultDto applyEvents(Plan plan) {
+ DiscountDetails discountDetails = discountEvent.makeDiscountDetails(plan);
+ Gift gift = Gift.from(plan.calculateTotalPrice());
+ int totalBenefitPrice = calculateTotalBenefitPrice(discountDetails, gift);
+ Badge badge = Badge.from(totalBenefitPrice);
+
+ return PlanResultDto.builder()
+ .plan(plan).discountDetails(discountDetails)
+ .gift(gift).badge(badge).build();
+ }
+
+ private int calculateTotalBenefitPrice(DiscountDetails discountDetails, Gift gift) {
+ int totalDiscountPrice = discountDetails.calculateTotalDiscountPrice();
+ int giftBenefitPrice = gift.calculateBenefitPrice();
+ return totalDiscountPrice + giftBenefitPrice;
+ }
+
+ private void printEventResult(PlanResultDto planResult) {
+ outputView.printPlanResult(planResult);
+ }
+} | Java | ๋ง์ฐฌ๊ฐ์ง๋ก ์ปจํธ๋กค๋ฌ์์ ๋ฐฉ๋ฌธ๋ ์ง์ ์ฃผ๋ฌธ์ ๊ฐ์ง๊ณ ์๋ Plan ๊ฐ์ฒด๋ฅผ ํตํด ํ ์ธ์ ์ ์ฉํ์๋๋ฐ,
ํ ์ธ ์ ์ฉ์ด๋ผ๋ ๋น์ฆ๋์ค ๋ก์ง์ด ๋
ธ์ถ๋์ด์๋ ๊ฒ์ด ์๋๊ฐ ์๊ฐ์ด ๋ญ๋๋ค.
์๋น์ค ๋ ์ด์ด์์ ์ฒ๋ฆฌํ์ง ์์ ์ด์ ๊ฐ ์์ผ์ ์ง ๊ถ๊ธํฉ๋๋ค. |
@@ -0,0 +1,33 @@
+package christmas.domain.badge;
+
+import java.util.List;
+
+public enum Badge {
+
+ SANTA(20_000),
+ TREE(10_000),
+ STAR(5_000),
+ NOTHING(0);
+
+ private static final List<Badge> ORDER_BY_BENEFIT = List.of(SANTA, TREE, STAR, NOTHING);
+
+ private final int minBenefitAmount;
+
+ Badge(int minBenefitAmount) {
+ this.minBenefitAmount = minBenefitAmount;
+ }
+
+ public static Badge from(int benefitAmount) {
+ return ORDER_BY_BENEFIT.stream()
+ .filter(badge -> badge.isReach(benefitAmount))
+ .findFirst().orElseThrow(Badge::createIllegalBenefitAmountException);
+ }
+
+ private static IllegalArgumentException createIllegalBenefitAmountException() {
+ return new IllegalArgumentException("benefit amount must not be negative");
+ }
+
+ private boolean isReach(int benefitAmount) {
+ return benefitAmount >= this.minBenefitAmount;
+ }
+} | Java | values() ๋ฑ์ผ๋ก ๊ฐ์ ธ์ค๋ฉด ์์๊ฐ ์ ๊ฐ ์์ฑํ ์์์ ์ํฅ์ ๋ฐ๋๋ค๊ณ ์๊ฐํด์ ์ผ๋ถ๋ฌ `ORDER_BY_BENEFIT`์ ๋ง๋ค์์ต๋๋ค. ๊ทธ๋ฐ๋ฐ ๋ค๋ฅธ ์ฝ๋๋ค์ ๋ณด๋ `Arrays.stream(values()).sort(this::price)` ํ์์ผ๋ก ๋ง์ด ์ฌ์ฉ ํด์ผ ๊ฒ ๋๋ผ๊ตฌ์. |
@@ -0,0 +1,27 @@
+package christmas.domain.discount.policy;
+
+import christmas.domain.plan.EventSchedule;
+import christmas.domain.plan.Plan;
+import java.util.function.IntUnaryOperator;
+
+public class ChristmasDDayDiscount extends DiscountPolicy {
+
+ private static final int START_DATE = 1;
+ private static final int END_DATE = 25;
+ private static final EventSchedule EVENT_SCHEDULE = EventSchedule.of(START_DATE, END_DATE);
+
+ private static final int DEFAULT_DISCOUNT_AMOUNT = 1_000;
+ private static final int INCREASING_DISCOUNT_AMOUNT = 100;
+ private static final IntUnaryOperator DISCOUNT_FUNCTION_BY_DATE
+ = date -> DEFAULT_DISCOUNT_AMOUNT + (date - 1) * INCREASING_DISCOUNT_AMOUNT;
+
+ @Override
+ boolean isSatisfyPrecondition(Plan plan) {
+ return plan.isContainedBy(EVENT_SCHEDULE);
+ }
+
+ @Override
+ int calculateDiscountAmount(Plan plan) {
+ return plan.calculateByDate(DISCOUNT_FUNCTION_BY_DATE);
+ }
+} | Java | ๋๋ฉ์ธ๋ผ๋ฆฌ ์ ๋ณด๋ฅผ ์ฃผ๊ณ ๋ฐ๋๋ฐ getter๋ฅผ ์์ฐ๋ ๊ฒ์ด ๊ฐ์ธ์ ์ธ ์ด๋ฒ ๊ณผ์ ๋ชฉํ์์ต๋๋ค. ๊ทธ๋์ date ๋ผ๋ int ๊ฐ์ ๋ฐ์์ค๊ธฐ ๋ณด๋ค, date๋ฅผ ํตํด ๊ณ์ฐํ๋ ์์ ๋๊ฒจ์ฃผ์์ต๋๋ค.
๋ค์ ์๊ฐํด๋ณด๋ฉด ์ด์ ๋๋ getter๋ก ์จ๋ ๋์ง ์๋ ์ถ์ง๋ง, ๊ทธ๋ฅ ์ต๋ํ ๊ฐ์ฒด๋ต๊ฒ ์ฌ์ฉํ๋ ํ์์ผ๋ก ํด๋ณด์์ต๋๋ค! |
@@ -0,0 +1,47 @@
+package christmas.domain.gift;
+
+import christmas.domain.plan.Menu;
+import java.util.Map;
+import java.util.Objects;
+
+public enum Gift {
+ EVENT_GIFT(Map.of(Menu.CHAMPAGNE, 1)),
+ NOTHING(Map.of());
+
+ private static final int MIN_GIFT_PRICE = 120_000;
+
+ private final Map<Menu, Integer> menuToCount;
+
+ Gift(Map<Menu, Integer> menuToCount) {
+ this.menuToCount = Objects.requireNonNull(menuToCount);
+ }
+
+ public static Gift from(int totalPrice) {
+ if (isOverThanStandardPrice(totalPrice)) {
+ return EVENT_GIFT;
+ }
+ return NOTHING;
+ }
+
+ private static boolean isOverThanStandardPrice(int totalPrice) {
+ return totalPrice >= MIN_GIFT_PRICE;
+ }
+
+ public int calculateBenefitPrice() {
+ return menuToCount.keySet().stream()
+ .mapToInt(menu -> calculatePartOfPrice(menu, menuToCount.get(menu)))
+ .sum();
+ }
+
+ public int calculatePartOfPrice(Menu menu, int count) {
+ return menu.getPrice() * count;
+ }
+
+ public boolean isEmpty() {
+ return menuToCount.isEmpty();
+ }
+
+ public Map<Menu, Integer> getMenuToCount() {
+ return Map.copyOf(menuToCount);
+ }
+} | Java | ๋ต! ํ์ฅ์ฑ์ ๊ณ ๋ คํ์ต๋๋ค! |
@@ -0,0 +1,21 @@
+package christmas.view;
+
+import christmas.domain.badge.Badge;
+import java.util.Map;
+
+class BadgeView {
+
+ private static final Map<Badge, String> BADGE_TO_MESSAGE = Map.of(
+ Badge.SANTA, "์ฐํ", Badge.TREE, "ํธ๋ฆฌ",
+ Badge.STAR, "๋ณ", Badge.NOTHING, "์์");
+
+ private BadgeView() {
+ }
+
+ public static String findView(Badge badge) {
+ if (!BADGE_TO_MESSAGE.containsKey(badge)) {
+ throw new IllegalArgumentException("Badge not enrolled at view; input : " + badge);
+ }
+ return BADGE_TO_MESSAGE.get(badge);
+ }
+} | Java | ๋ง์ต๋๋ค. ์ด๋ ๊ฒ ๋๋ฉด View๊ฐ ๊ต์ฅํ ์ปค์ง๊ฒ ๋ฉ๋๋ค. ํ์์ ์ ๋ enum์์ ๊ด๋ฆฌํ๋๋ฐ ์ด๋ฒ์ฃผ ๊ณผ์ ์๋ ์๊ตฌ์ฌํญ์ ๊ทนํ์ผ๋ก ์ฑ๊ธฐ๊ณ ์ถ์์ต๋๋ค.
์ง๋ฌธํ์ ๋ถ๋ถ์ด ๊ต์ฅํ ๋ ์นด๋ก์ฐ์๋ค์. "๋ฐ์ง์ง ์๊ณ ์๊ตฌ ์ฌํญ์ ์ต๋ํ ์ง์ผ๋ณด์"๋ ๋ถ๋ถ์ ์ ๋ถ ์ธ๊ธํ์ ๊ฒ ๊ฐ์ต๋๋ค. ๋ ์นด๋ก์ด ๋ฆฌ๋ทฐ ๊ฐ์ฌ๋๋ฆฝ๋๋ค. |
@@ -0,0 +1,48 @@
+package christmas.domain.plan;
+
+public enum Menu {
+
+ MUSHROOM_SOUP(6_000, Category.APPETIZER),
+ TAPAS(5_500, Category.APPETIZER),
+ CAESAR_SALAD(8_000, Category.APPETIZER),
+
+ T_BONE_STEAK(55_000, Category.MAIN),
+ BARBECUE_RIBS(54_000, Category.MAIN),
+ SEAFOOD_PASTA(35_000, Category.MAIN),
+ CHRISTMAS_PASTA(25_000, Category.MAIN),
+
+ CHOCOLATE_CAKE(15_000, Category.DESSERT),
+ ICE_CREAM(5_000, Category.DESSERT),
+
+ ZERO_COKE(3_000, Category.DRINK),
+ RED_WINE(60_000, Category.DRINK),
+ CHAMPAGNE(25_000, Category.DRINK);
+
+ private final int price;
+ private final Category category;
+
+ Menu(int price, Category category) {
+ this.price = price;
+ this.category = category;
+ }
+
+ public boolean isMain() {
+ return this.category == Category.MAIN;
+ }
+
+ public boolean isDessert() {
+ return this.category == Category.DESSERT;
+ }
+
+ public boolean isDrink() {
+ return this.category == Category.DRINK;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ private enum Category {
+ APPETIZER, MAIN, DESSERT, DRINK
+ }
+} | Java | ๊ฐ ์นดํ
๊ณ ๋ฆฌ๋ณ๋ก๋ enum์ ์ ์ํด์ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ๋ ์์๊ฑฐ๋ผ๊ณ ์๊ฐ์ด ๋ญ๋๋ค! |
@@ -0,0 +1,27 @@
+package christmas.view;
+
+import christmas.dto.DiscountEventDto;
+import java.util.Map;
+
+class BenefitDetailView {
+
+ private static final Map<DiscountEventDto, String> DISCOUNT_EVENT_TO_MESSAGE = Map.of(
+ DiscountEventDto.CHRISTMAS_D_DAY, "ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ", DiscountEventDto.WEEKDAY, "ํ์ผ ํ ์ธ",
+ DiscountEventDto.WEEKEND, "์ฃผ๋ง ํ ์ธ", DiscountEventDto.SPECIAL, "ํน๋ณ ํ ์ธ"
+ );
+ private static final String GIFT_EVENT_MESSAGE = "์ฆ์ ์ด๋ฒคํธ";
+
+ private BenefitDetailView() {
+ }
+
+ public static String findView(DiscountEventDto discountEvent) {
+ if (!DISCOUNT_EVENT_TO_MESSAGE.containsKey(discountEvent)) {
+ throw new IllegalArgumentException("DiscountEventDto not enrolled at view; input : " + discountEvent);
+ }
+ return DISCOUNT_EVENT_TO_MESSAGE.get(discountEvent);
+ }
+
+ public static String findGiftEventView() {
+ return GIFT_EVENT_MESSAGE;
+ }
+} | Java | ์ธ์ธํ๊ฒ ๋๋๋ค๋ณด๋ ํ ์ธ์ ๋ํ ์ ๋ณด๋ฅผ ๋ทฐ๊ฐ ๊ฐ๊ฒ๋ ๊ฒ ๊ฐ๋ค์.
์์ ๋ค์ฌ๋์ด ๋ง์ํ์ ๊ฒ ์ฒ๋ผ ๋ทฐ๊ฐ ๋๋ฉ์ธ์ ๊ด๋ จํ ์ ๋ณด๋ฅผ ๊ฐ๊ฒ๋๊ณ ๋ณ๊ฒฝ ํฌ์ธํธ ๋ํ ๋์ง ์์๊น ํฉ๋๋ค. |
@@ -0,0 +1,103 @@
+package christmas.domain.plan;
+
+import christmas.exception.OnlyDrinkMenuException;
+import christmas.exception.OrderInputException;
+import christmas.exception.TotalMenuCountException;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.function.Predicate;
+
+public class Order {
+
+ private static final int TOTAL_COUNT_OF_MENU = 20;
+
+ private final Map<Menu, Integer> menuToCount;
+
+ private Order(Map<Menu, Integer> menuToCount) {
+ validate(menuToCount);
+ this.menuToCount = Map.copyOf(menuToCount);
+ }
+
+ private static void validate(Map<Menu, Integer> menuToCount) {
+ validateCountPerMenu(menuToCount);
+ validateTotalCount(menuToCount);
+ validateNotOnlyDrink(menuToCount);
+ }
+
+ private static void validateCountPerMenu(Map<Menu, Integer> menuToCount) {
+ if (isExistCountUnderOne(menuToCount)) {
+ throw new OrderInputException("quantity isn't less than 1");
+ }
+ }
+
+ private static boolean isExistCountUnderOne(Map<Menu, Integer> menuToCount) {
+ return menuToCount.values().stream()
+ .anyMatch(count -> count < 1);
+ }
+
+ private static void validateTotalCount(Map<Menu, Integer> menuToCount) {
+ if (isOverTotalCount(menuToCount)) {
+ throw new TotalMenuCountException(TOTAL_COUNT_OF_MENU);
+ }
+ }
+
+ private static boolean isOverTotalCount(Map<Menu, Integer> menuToCount) {
+ return countTotalMenu(menuToCount) > TOTAL_COUNT_OF_MENU;
+ }
+
+ private static int countTotalMenu(Map<Menu, Integer> menuToCount) {
+ return menuToCount.values().stream()
+ .mapToInt(Integer::intValue)
+ .sum();
+ }
+
+ private static void validateNotOnlyDrink(Map<Menu, Integer> menuToCount) {
+ if (isOnlyDrinkMenu(menuToCount)) {
+ throw new OnlyDrinkMenuException();
+ }
+ }
+
+ private static boolean isOnlyDrinkMenu(Map<Menu, Integer> menuToCount) {
+ return menuToCount.keySet().stream()
+ .allMatch(Menu::isDrink);
+ }
+
+ public static Order from(Map<Menu, Integer> menuToCount) {
+ return new Order(menuToCount);
+ }
+
+ public int countMainMenu() {
+ return countMenu(Menu::isMain);
+ }
+
+ public int countDessertMenu() {
+ return countMenu(Menu::isDessert);
+ }
+
+ private int countMenu(Predicate<Menu> condition) {
+ return menuToCount.keySet().stream()
+ .filter(condition)
+ .mapToInt(menuToCount::get)
+ .sum();
+ }
+
+ public int calculateTotalPrice() {
+ return menuToCount.entrySet().stream()
+ .mapToInt(this::calculateSubPrice)
+ .sum();
+ }
+
+ private int calculateSubPrice(Entry<Menu, Integer> menuCountPair) {
+ int menuPrice = menuCountPair.getKey().getPrice();
+ int count = menuCountPair.getValue();
+ return menuPrice * count;
+ }
+
+ public boolean isTotalPriceEqualOrMoreThan(int comparedPrice) {
+ return calculateTotalPrice() >= comparedPrice;
+ }
+
+ public Map<Menu, Integer> getMenuToCount() {
+ return Map.copyOf(menuToCount);
+ }
+} | Java | ์ ๋ ์ด๋ ๊ฒ ํ๋ค๊ฐ ํ์์ด ์ฅ์ ์ธ Map ๊ฐ์ง๊ณ ์๊พธ ์ํํ๋๋ฐ์ ์ฌ์ฉํ๋๊ฒ ๊ฐ์์
`public record menuToCount(Menu menu, int quantity){}`
์ด๋ ๊ฒ ๋ง๋ค์ด์ ์ํ์ ์ ์ ํ๊ฒ `List<menuToCount>` ์ด๋ ๊ฒ ์ฌ์ฉํ๋๋ฐ ๊ทธ๋ฅ Map ์ฌ์ฉํ์ ์ด์ ๊ฐ ์์๊น์?? |
@@ -0,0 +1,21 @@
+package christmas.domain.discount.policy;
+
+import christmas.domain.plan.EventSchedule;
+import christmas.domain.plan.Plan;
+import java.util.Set;
+
+public class SpecialDiscount extends DiscountPolicy {
+
+ private static final EventSchedule EVENT_SCHEDULE = EventSchedule.from(Set.of(3, 10, 17, 24, 25, 31));
+ private static final int DISCOUNT_AMOUNT = 1_000;
+
+ @Override
+ boolean isSatisfyPrecondition(Plan plan) {
+ return plan.isContainedBy(EVENT_SCHEDULE);
+ }
+
+ @Override
+ int calculateDiscountAmount(Plan plan) {
+ return DISCOUNT_AMOUNT;
+ }
+} | Java | ํน๋ณ ์ด๋ฒคํธ ๋ ์ง๋ ์ผ์์ผ + ํฌ๋ฆฌ์ค๋ง์ค๋ ์ธ๋ฐ, ์ด ๋ถ๋ถ์ ๋ช
ํํ๊ฒ ๋๋ ค๋์ผ๋ฉด ๋ ์ข์ง ์์์๊น ์ถ์ด์! |
@@ -0,0 +1,47 @@
+package christmas.domain.gift;
+
+import christmas.domain.plan.Menu;
+import java.util.Map;
+import java.util.Objects;
+
+public enum Gift {
+ EVENT_GIFT(Map.of(Menu.CHAMPAGNE, 1)),
+ NOTHING(Map.of());
+
+ private static final int MIN_GIFT_PRICE = 120_000;
+
+ private final Map<Menu, Integer> menuToCount;
+
+ Gift(Map<Menu, Integer> menuToCount) {
+ this.menuToCount = Objects.requireNonNull(menuToCount);
+ }
+
+ public static Gift from(int totalPrice) {
+ if (isOverThanStandardPrice(totalPrice)) {
+ return EVENT_GIFT;
+ }
+ return NOTHING;
+ }
+
+ private static boolean isOverThanStandardPrice(int totalPrice) {
+ return totalPrice >= MIN_GIFT_PRICE;
+ }
+
+ public int calculateBenefitPrice() {
+ return menuToCount.keySet().stream()
+ .mapToInt(menu -> calculatePartOfPrice(menu, menuToCount.get(menu)))
+ .sum();
+ }
+
+ public int calculatePartOfPrice(Menu menu, int count) {
+ return menu.getPrice() * count;
+ }
+
+ public boolean isEmpty() {
+ return menuToCount.isEmpty();
+ }
+
+ public Map<Menu, Integer> getMenuToCount() {
+ return Map.copyOf(menuToCount);
+ }
+} | Java | ์ด๋ ๊ฒ ์ฆ์ ์ด๋ฒคํธ๋ง ๋ฐ๋ก ๋ค๋ฃจ์
จ๊ตฐ์ ์ข์ ๋ฐฉ๋ฒ ์์๊ฐ๋๋ค! |
@@ -0,0 +1,76 @@
+package christmas.domain.plan;
+
+import christmas.exception.DateInputException;
+import java.time.DayOfWeek;
+import java.time.LocalDate;
+import java.util.Objects;
+import java.util.Set;
+import java.util.function.IntUnaryOperator;
+
+public class EventDate {
+
+ private static final int YEAR = 2023;
+ private static final int MONTH = 12;
+ private static final int MIN_DATE = 1;
+ private static final int MAX_DATE = 31;
+
+ private static final Set<DayOfWeek> WEEKEND = Set.of(DayOfWeek.FRIDAY, DayOfWeek.SATURDAY);
+
+ private final LocalDate date;
+
+ private EventDate(int date) {
+ validate(date);
+ this.date = LocalDate.of(YEAR, MONTH, date);
+ }
+
+ private static void validate(int date) {
+ if (isOutOfRange(date)) {
+ throw new DateInputException("Date is Out of Range : " + date);
+ }
+ }
+
+ private static boolean isOutOfRange(int date) {
+ return date < MIN_DATE || date > MAX_DATE;
+ }
+
+ public static EventDate from(int date) {
+ return new EventDate(date);
+ }
+
+ public boolean isWeekend() {
+ DayOfWeek dayOfWeek = date.getDayOfWeek();
+ return WEEKEND.contains(dayOfWeek);
+ }
+
+ public boolean isWeekDay() {
+ return !isWeekend();
+ }
+
+ public int calculateByDate(IntUnaryOperator function) {
+ return function.applyAsInt(getDate());
+ }
+
+ public int getDate() {
+ return date.getDayOfMonth();
+ }
+
+ @Override
+ public boolean equals(Object object) {
+ if (this == object) {
+ return true;
+ }
+ if (object == null || getClass() != object.getClass()) {
+ return false;
+ }
+ EventDate comparedDate = (EventDate) object;
+ return Objects.equals(date, comparedDate.date);
+ }
+
+ @Override
+ public int hashCode() {
+ if (date == null) {
+ return 0;
+ }
+ return date.hashCode();
+ }
+} | Java | ์ ๋ LocalDate๋ฅผ ๊ทธ๋ฅ ์ฌ์ฉํ์๋๋ฐ, ์ด๋ ๊ฒ ๋ฐ๋ก ํด๋์ค๋ก ํ์ ๋ค๋ฃจ๋๊ฒ ํจ์ฌ ๊น๋ํ๊ฒ ๊ฐ๋ค์ |
@@ -0,0 +1,79 @@
+package christmas.controller;
+
+import christmas.domain.badge.Badge;
+import christmas.domain.discount.DiscountDetails;
+import christmas.domain.discount.TotalDiscountEvent;
+import christmas.domain.gift.Gift;
+import christmas.domain.plan.EventDate;
+import christmas.domain.plan.Menu;
+import christmas.domain.plan.Order;
+import christmas.domain.plan.Plan;
+import christmas.dto.PlanResultDto;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+import java.util.Map;
+import java.util.function.Supplier;
+
+public class ChristmasPlanerController {
+
+ private final TotalDiscountEvent discountEvent = TotalDiscountEvent.createDecemberEvent();
+ private final InputView inputView = new InputView();
+ private final OutputView outputView = new OutputView();
+
+ public void run() {
+ startApplication();
+ Plan plan = inputPlan();
+ PlanResultDto planResult = applyEvents(plan);
+ printEventResult(planResult);
+ }
+
+ private void startApplication() {
+ outputView.printApplicationTitle();
+ }
+
+ private Plan inputPlan() {
+ EventDate date = readRepeatedlyUntilNoException(this::readDate);
+ Order order = readRepeatedlyUntilNoException(this::readOrder);
+ return Plan.of(date, order);
+ }
+
+ private <T> T readRepeatedlyUntilNoException(Supplier<T> supplier) {
+ try {
+ return supplier.get();
+ } catch (IllegalArgumentException exception) {
+ outputView.printExceptionMessage(exception);
+ return readRepeatedlyUntilNoException(supplier);
+ }
+ }
+
+ private EventDate readDate() {
+ int date = inputView.readDate();
+ return EventDate.from(date);
+ }
+
+ private Order readOrder() {
+ Map<Menu, Integer> order = inputView.readOrder();
+ return Order.from(order);
+ }
+
+ private PlanResultDto applyEvents(Plan plan) {
+ DiscountDetails discountDetails = discountEvent.makeDiscountDetails(plan);
+ Gift gift = Gift.from(plan.calculateTotalPrice());
+ int totalBenefitPrice = calculateTotalBenefitPrice(discountDetails, gift);
+ Badge badge = Badge.from(totalBenefitPrice);
+
+ return PlanResultDto.builder()
+ .plan(plan).discountDetails(discountDetails)
+ .gift(gift).badge(badge).build();
+ }
+
+ private int calculateTotalBenefitPrice(DiscountDetails discountDetails, Gift gift) {
+ int totalDiscountPrice = discountDetails.calculateTotalDiscountPrice();
+ int giftBenefitPrice = gift.calculateBenefitPrice();
+ return totalDiscountPrice + giftBenefitPrice;
+ }
+
+ private void printEventResult(PlanResultDto planResult) {
+ outputView.printPlanResult(planResult);
+ }
+} | Java | "์ด์ฉ ์ ์๋ ๋ถ๋ถ์ด๋ค"๋ผ๊ณ ์๊ฐํฉ๋๋ค.
์๋๋ ๊ทธ๋ฅ ์ง๊ธ ์ฆ๊ฐ์ ์ผ๋ก ๋ ์ ์๊ฐ์
๋๋ค. (๋ถ๋ช
ํ ์ฑ
์ด๋ ๊ธ์์ ๋ดค์ํ
๋ฐ...)
- ๊ฐ์ฒด๋ ์์ฑ์์์ ์ธ์๋ฅผ ํตํด ์ฃผ์
๋ฐ๋ ๊ฒ์ด ํ
์คํธ ์ฝ๋๋ฅผ ์ง๋๋ฐ ์ฉ์ดํ๋ค (๋ด๋ถ์์ ์์ฑํ์ง ๋ง๊ณ )
- ๊ฐ์ฒด๋ ์๋ก์ ๋ํด ๋ชจ๋ฅด๋ฉด ๋ชจ๋ฅผ์๋ก (์์กด์ฑ์ด ๋ฎ์์๋ก) ์ข๋ค.
์ฌ์ค 1๋ฒ์ ์งํฌ๋ ค๋ฉด ์ง๊ธ์ฒ๋ผ ์ธ๋ถ์์ ์์ฑํด์ ๋ฃ์ด์ฃผ์ด์ผ ํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ Controller์ ๋๋ฉ์ธ ๊ฐ์ ์ข
์์ฑ์ ์ค์ด๋ ค๋ฉด (2๋ฒ์ ์งํค๋ ค๋ฉด) EventDate์ Order๋ฅผ Plan๋ด๋ถ์์ ์์ฑํด์ผ ํ์ฃ . ๊ทธ๋ฅ ์งํฌ ๊ฒ ๋ง๋ค๋ณด๋ฉด ๋ชจ๋ ์งํฌ ์๋ ์์ฃ .
์๊ตฌ์ฌํญ ์์ฒด๊ฐ EventDate์ ํ์์ด ๋ค๋ฅผ ๋, ๋ฐ๋ก ๋ค์ ์
๋ ฅํด์ผ ํ๋๊น 1๋ฒ ๊ท์น์ ๋ฐ๋๋ค๊ณ ๋ณผ ์ ์์ต๋๋ค. ๋ฆฌ๋ทฐ์ด๋์ด ์๊ฐํ์ ๊ฒ์ ์ ๋๋ก ๋ต๋ณ์ ๋ชป๋๋ฆฐ ๊ฑฐ ๊ฐ์๋ฐ ์๋์ ์ด์ด์ ๋๋ฆฌ๊ฒ ์ต๋๋ค. |
@@ -0,0 +1,79 @@
+package christmas.controller;
+
+import christmas.domain.badge.Badge;
+import christmas.domain.discount.DiscountDetails;
+import christmas.domain.discount.TotalDiscountEvent;
+import christmas.domain.gift.Gift;
+import christmas.domain.plan.EventDate;
+import christmas.domain.plan.Menu;
+import christmas.domain.plan.Order;
+import christmas.domain.plan.Plan;
+import christmas.dto.PlanResultDto;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+import java.util.Map;
+import java.util.function.Supplier;
+
+public class ChristmasPlanerController {
+
+ private final TotalDiscountEvent discountEvent = TotalDiscountEvent.createDecemberEvent();
+ private final InputView inputView = new InputView();
+ private final OutputView outputView = new OutputView();
+
+ public void run() {
+ startApplication();
+ Plan plan = inputPlan();
+ PlanResultDto planResult = applyEvents(plan);
+ printEventResult(planResult);
+ }
+
+ private void startApplication() {
+ outputView.printApplicationTitle();
+ }
+
+ private Plan inputPlan() {
+ EventDate date = readRepeatedlyUntilNoException(this::readDate);
+ Order order = readRepeatedlyUntilNoException(this::readOrder);
+ return Plan.of(date, order);
+ }
+
+ private <T> T readRepeatedlyUntilNoException(Supplier<T> supplier) {
+ try {
+ return supplier.get();
+ } catch (IllegalArgumentException exception) {
+ outputView.printExceptionMessage(exception);
+ return readRepeatedlyUntilNoException(supplier);
+ }
+ }
+
+ private EventDate readDate() {
+ int date = inputView.readDate();
+ return EventDate.from(date);
+ }
+
+ private Order readOrder() {
+ Map<Menu, Integer> order = inputView.readOrder();
+ return Order.from(order);
+ }
+
+ private PlanResultDto applyEvents(Plan plan) {
+ DiscountDetails discountDetails = discountEvent.makeDiscountDetails(plan);
+ Gift gift = Gift.from(plan.calculateTotalPrice());
+ int totalBenefitPrice = calculateTotalBenefitPrice(discountDetails, gift);
+ Badge badge = Badge.from(totalBenefitPrice);
+
+ return PlanResultDto.builder()
+ .plan(plan).discountDetails(discountDetails)
+ .gift(gift).badge(badge).build();
+ }
+
+ private int calculateTotalBenefitPrice(DiscountDetails discountDetails, Gift gift) {
+ int totalDiscountPrice = discountDetails.calculateTotalDiscountPrice();
+ int giftBenefitPrice = gift.calculateBenefitPrice();
+ return totalDiscountPrice + giftBenefitPrice;
+ }
+
+ private void printEventResult(PlanResultDto planResult) {
+ outputView.printPlanResult(planResult);
+ }
+} | Java | ์๋ง๋ ์ค์ง์ ์ผ๋ก๋ ํ๋ก๊ทธ๋จ์ด ์์ ํฌ๊ธฐ์ด๋ค ๋ณด๋ Service Layer๋ก๊น์ง ๋ถ๋ฆฌํ ํ์์ฑ์ ๋ชป๋๋ ๊ฒ ๊ฐ์ต๋๋ค. ๋ฉ์๋๋ฅผ ๋ถ๋ฆฌํ๋ ๊ฒ ๋ง์ผ๋ก๋ ์ถฉ๋ถํ๋ค๊ณ ์๊ฐํ์ต๋๋ค. ๋ง์ฝ ์ฌ๊ธฐ์์ ํ๋ก๊ทธ๋จ์ด ์ ์ ์ปค์ง๋ค๋ฉด Service Layer๊ฐ ํ์ํ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,48 @@
+package christmas.domain.plan;
+
+public enum Menu {
+
+ MUSHROOM_SOUP(6_000, Category.APPETIZER),
+ TAPAS(5_500, Category.APPETIZER),
+ CAESAR_SALAD(8_000, Category.APPETIZER),
+
+ T_BONE_STEAK(55_000, Category.MAIN),
+ BARBECUE_RIBS(54_000, Category.MAIN),
+ SEAFOOD_PASTA(35_000, Category.MAIN),
+ CHRISTMAS_PASTA(25_000, Category.MAIN),
+
+ CHOCOLATE_CAKE(15_000, Category.DESSERT),
+ ICE_CREAM(5_000, Category.DESSERT),
+
+ ZERO_COKE(3_000, Category.DRINK),
+ RED_WINE(60_000, Category.DRINK),
+ CHAMPAGNE(25_000, Category.DRINK);
+
+ private final int price;
+ private final Category category;
+
+ Menu(int price, Category category) {
+ this.price = price;
+ this.category = category;
+ }
+
+ public boolean isMain() {
+ return this.category == Category.MAIN;
+ }
+
+ public boolean isDessert() {
+ return this.category == Category.DESSERT;
+ }
+
+ public boolean isDrink() {
+ return this.category == Category.DRINK;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ private enum Category {
+ APPETIZER, MAIN, DESSERT, DRINK
+ }
+} | Java | ์ ๋ Menu Enum์๊ฒ์ public method๋ฅผ ์ ๊ณตํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ด ๋ฐฉ์์ด ํจ์จ์ ์ด๋ผ๊ณ ์๊ฐํ์ต๋๋ค. ๊ฐ ์นดํ
๊ณ ๋ฆฌ๋ณ๋ก๋ enum์ ๊ด๋ฆฌํ๋ ์ฝ๋๋ ์ด๋ค ๋๋์ผ๊น์? ๊ทธ๋ฆฌ๊ณ ๊ทธ ๋ฐฉ์์ด ์ด ๋ฐฉ์๋ณด๋ค ๋ ์ข์ ์ ์ ๋ฌด์์ด ์์๊น์? |
@@ -0,0 +1,27 @@
+package christmas.view;
+
+import christmas.dto.DiscountEventDto;
+import java.util.Map;
+
+class BenefitDetailView {
+
+ private static final Map<DiscountEventDto, String> DISCOUNT_EVENT_TO_MESSAGE = Map.of(
+ DiscountEventDto.CHRISTMAS_D_DAY, "ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ", DiscountEventDto.WEEKDAY, "ํ์ผ ํ ์ธ",
+ DiscountEventDto.WEEKEND, "์ฃผ๋ง ํ ์ธ", DiscountEventDto.SPECIAL, "ํน๋ณ ํ ์ธ"
+ );
+ private static final String GIFT_EVENT_MESSAGE = "์ฆ์ ์ด๋ฒคํธ";
+
+ private BenefitDetailView() {
+ }
+
+ public static String findView(DiscountEventDto discountEvent) {
+ if (!DISCOUNT_EVENT_TO_MESSAGE.containsKey(discountEvent)) {
+ throw new IllegalArgumentException("DiscountEventDto not enrolled at view; input : " + discountEvent);
+ }
+ return DISCOUNT_EVENT_TO_MESSAGE.get(discountEvent);
+ }
+
+ public static String findGiftEventView() {
+ return GIFT_EVENT_MESSAGE;
+ }
+} | Java | ๋ณ๊ฒฝ ํฌ์ธํธ๊ฐ ๋์ด๋๋ค๋ ์ , ํฅ๋ฏธ๋กญ๋ค์. ํ์คํ ์ ์ฑ
์ด ํ๋ ๋ ์ถ๊ฐ๋๋ค๋ฉด, enum์๋ ์ถ๊ฐํ๊ณ BenefitDetailView์๋ ์ถ๊ฐ๋์ด์ผ ํ๋ ์ข ๊ทธ๋ ๋ค์. ์ด๊ฑธ ๊ตฌํํ ๋ ์ ์๊ฐ์ ์ด๋ฌ์ต๋๋ค.
> ๋ง์ฝ์ "ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ"์ด๋ผ๋ ์ถ๋ ฅ ํ
์คํธ๋ฅผ "ํฌ๋ฆฌ์ค๋ง์ค D-day ํ ์ธ"์ผ๋ก ์์ ํ๋ค๊ณ ํ๋ฉด, ํด๋น ์ฝ๋๋ ์ด๋์ ์์ด์ผ ํ ๊น?
์ด ์ง๋ฌธ์์ ์ถ๋ฐํ์ต๋๋ค. ์ ๋ '์ถ๋ ฅ ํ์์ ์์ 'ํ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ view package์ ์์ด์ผ ํ๋ค๊ณ ์๊ฐํ์ต๋๋ค. ์ถ๋ ฅ ํ์์ด ๋๋ฉ์ธ์ ๋ฐ๋ผ ์ฌ๊ธฐ ์ ๊ธฐ์ ํผ์ ธ์๋ ๊ฒ๋ ์์ ํ๋๋ฐ ๋ณ๋ก๋ผ๊ณ ์๊ฐํฉ๋๋ค. ๊ทธ๋์ ์ด ๋ถ๋ถ์ ์์ผ๋ก ์ด๋ป๊ฒ ๋ณ๊ฒฝ๋ ๊ฐ๋ฅ์ฑ์ด ์๋์ง์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๊ตฌํํ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -11,12 +11,178 @@ on:
jobs:
code_review:
runs-on: ubuntu-latest
- name: ChatGPT Code Review
+ name: Gemini Code Review
steps:
- - uses: anc95/ChatGPT-CodeReview@main
+ - uses: actions/checkout@v4
+
+ - name: Setup Node.js
+ uses: actions/setup-node@v4
+ with:
+ node-version: "20"
+
+ - name: Install dependencies
+ run: npm install @google/generative-ai parse-diff
+
+ - name: Review Changed Files
+ uses: actions/github-script@v7
env:
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
- # optional
- LANGUAGE: Korean
- MODEL: gpt-4o-mini-2024-07-18 # https://platform.openai.com/docs/models
+ GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
+ GEMINI_MODEL: ${{ secrets.GEMINI_MODEL }}
+ MAX_CONCURRENT_REVIEWS: 3 # ๋์์ ์ฒ๋ฆฌํ ํ์ผ ์
+ MAX_RETRIES: 3
+ RETRY_DELAY: 1000
+
+ with:
+ script: |
+ const { GoogleGenerativeAI } = require("@google/generative-ai");
+ const parseDiff = require('parse-diff');
+ // core๋ ์ด๋ฏธ github-script์์ ์ ๊ณต๋จ
+
+ // ์ฌ์๋ ๋ก์ง์ ํฌํจํ API ํธ์ถ ํจ์
+ async function withRetry(fn, retries = process.env.MAX_RETRIES) {
+ for (let i = 0; i < retries; i++) {
+ try {
+ return await fn();
+
+ } catch (error) {
+ if (error.message.includes('rate limit') && i < retries - 1) {
+ const delay = process.env.RETRY_DELAY * Math.pow(2, i);
+ console.log(`Rate limit hit, waiting ${delay}ms before retry ${i + 1}`);
+
+ await new Promise(resolve => setTimeout(resolve, delay));
+ continue;
+ }
+
+ throw error;
+ }
+ }
+ }
+
+ // Gemini AI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
+ const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
+ const model = genAI.getGenerativeModel({ model: process.env.GEMINI_MODEL });
+
+ /**
+ * ์ฝ๋ ๋ณ๊ฒฝ์ฌํญ์ ๊ฒํ ํ๊ณ ํผ๋๋ฐฑ์ ์์ฑ
+ *
+ * @param content ๋ฆฌ๋ทฐํ ์ฝ๋ ๋ด์ฉ
+ * @param filename ํ์ผ๋ช
+ * @returns ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ
+ */
+ async function reviewCode(content, filename) {
+ try {
+ // ํ์ผ๋ช
sanitize
+ const sanitizedFilename = filename.replace(/[^a-zA-Z0-9.-_\/]/g, '_');
+
+ const prompt = `๋น์ ์ ์๋์ด ๊ฐ๋ฐ์์
๋๋ค. ์๋ ${sanitizedFilename} ํ์ผ์ ์ฝ๋๋ฅผ ๊ฒํ ํ๊ณ ๋ค์ ์ฌํญ๋ค์ ํ๊ตญ์ด๋ก ๋ฆฌ๋ทฐํด์ฃผ์ธ์:
+ 1. ์ฝ๋์ ํ์ง๊ณผ ๊ฐ๋
์ฑ
+ 2. ์ ์ฌ์ ์ธ ๋ฒ๊ทธ๋ ๋ฌธ์ ์
+ 3. ์ฑ๋ฅ ๊ฐ์ ํฌ์ธํธ
+ 4. ๋ณด์ ๊ด๋ จ ์ด์
+ 5. ๊ฐ์ ์ ์ํ ๊ตฌ์ฒด์ ์ธ ์ ์
+
+ ์ฝ๋:
+ ${content}`;
+
+ const result = await withRetry(() => model.generateContent(prompt));
+ const response = await result.response;
+
+ if (!response.text()) {
+ throw new Error('Gemini API returned empty response');
+ }
+
+ console.log(`Successfully reviewed ${filename}`);
+ return response.text();
+
+ } catch (error) {
+ console.error(`Error reviewing ${filename}:`, error);
+ return `โ ๏ธ ์ฝ๋ ๋ฆฌ๋ทฐ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: ${error.message}
+ ๋ค์ ์๋ํ์๊ฑฐ๋ ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํด์ฃผ์ธ์.`;
+ }
+ }
+
+ /**
+ * PR์ ๊ฐ ํ์ผ์ ์ฒ๋ฆฌํ๊ณ ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ ์์ฑ
+ * @param file PR์์ ๋ณ๊ฒฝ๋ ํ์ผ ์ ๋ณด
+ * @param pr PR ์ ๋ณด
+ */
+ async function processFile(file, pr) {
+ if (file.status === 'removed') {
+ console.log(`Skipping removed file: ${file.filename}`);
+ return;
+ }
+
+ try {
+ if (!file.patch) {
+ console.warn(`No patch found for ${file.filename}, skipping.`);
+ return;
+ }
+
+ const diff = parseDiff(file.patch)[0];
+ if (!diff || !diff.chunks) {
+ console.log(`No valid diff found for ${file.filename}`);
+ return;
+ }
+
+ // ๋ชจ๋ ๋ณ๊ฒฝ์ฌํญ์ ํ๋๋ก ํฉ์น๊ธฐ (์ถ๊ฐ๋ ๋ถ๋ถ๋ง ํํฐ๋ง)
+ const changes = diff.chunks
+ .flatMap(chunk => chunk.changes)
+ .filter(change => change.type === 'add');
+
+ if (changes.length === 0) {
+ console.log(`No added changes found in ${file.filename}`);
+ return;
+ }
+
+ // ๋ณ๊ฒฝ์ฌํญ์ ํ๋์ ๋ฌธ์์ด๋ก ํฉ์น๊ธฐ
+ const content = changes.map(change => change.content).join('\n');
+ const review = await reviewCode(content, file.filename);
+
+ // PR์ ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ ์์ฑ (ํ์ผ๋ช
์ ํค๋๋ก ์ถ๊ฐ)
+ return github.rest.issues.createComment({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ issue_number: context.issue.number,
+ body: `## ${file.filename} ๋ฆฌ๋ทฐ\n\n${review}`
+ });
+
+ } catch (error) {
+ console.error(`Error processing ${file.filename}:`, error);
+ throw error;
+ }
+ }
+
+ try {
+ // PR ์ ๋ณด ์กฐํ
+ const { data: pr } = await github.rest.pulls.get({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ pull_number: context.issue.number,
+ });
+
+ // ๋ณ๊ฒฝ๋ ํ์ผ ๋ชฉ๋ก ์กฐํ
+ const { data: files } = await github.rest.pulls.listFiles({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ pull_number: context.issue.number,
+ });
+
+ console.log(`Starting review of ${files.length} files...`);
+
+ // ๋ชจ๋ ํ์ผ ์ฒ๋ฆฌ ์๋ฃ๋ฅผ ๊ธฐ๋ค๋ฆผ
+ const promises = [];
+ const batchSize = parseInt(process.env.MAX_CONCURRENT_REVIEWS);
+
+ for (let i = 0; i < files.length; i += batchSize) {
+ const batch = files.slice(i, i + batchSize);
+ const batchPromises = batch.map(file => processFile(file, pr));
+ promises.push(...batchPromises);
+ }
+
+ await Promise.all(promises);
+ console.log('All reviews completed successfully');
+
+ } catch (error) {
+ console.error('Workflow failed:', error);
+ core.setFailed(`Workflow failed: ${error.message}`);
+ } | Unknown | ์ด ์ฝ๋๋ GitHub Actions๋ฅผ ์ด์ฉํ์ฌ Google Gemini API๋ฅผ ํตํด Pull Request์ ๋ํ ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ์๋ํํ๋ ์์
์
๋๋ค. ํ์ง๋ง ๋ช ๊ฐ์ง ๊ฐ์ ์ด ํ์ํฉ๋๋ค.
**1. ์ฝ๋์ ํ์ง๊ณผ ๊ฐ๋
์ฑ:**
* **๊ฐ๋
์ฑ ๊ฐ์ :** `reviewCode` ํจ์ ๋ด๋ถ์ JSON ๊ตฌ์กฐ๊ฐ ์ค์ฒฉ๋์ด ๊ฐ๋
์ฑ์ด ๋จ์ด์ง๋๋ค. ๋ ๋ช
ํํ ๋ณ์๋ช
์ ์ฌ์ฉํ๊ณ , JSON ์์ฑ ๋ถ๋ถ์ ์ฌ๋ฌ ์ค๋ก ๋๋์ด ๊ฐ๋
์ฑ์ ๋์ฌ์ผ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด, `contents`, `parts` ๋ฑ์ ๋ณ์๋ช
์ ์ปจํ
์คํธ๋ฅผ ๋ช
ํํ ํ๋๋ก ์์ ํ ์ ์์ต๋๋ค.
* **์ค๋ฅ ์ฒ๋ฆฌ ๋ถ์กฑ:** `fetch` ํธ์ถ๊ณผ `response.json()`์์ ๋ฐ์ํ ์ ์๋ ๋คํธ์ํฌ ์๋ฌ๋ JSON ํ์ฑ ์๋ฌ์ ๋ํ ์ฒ๋ฆฌ๊ฐ ์ ํ ์์ต๋๋ค. `try...catch` ๋ธ๋ก์ ์ฌ์ฉํ์ฌ ์๋ฌ๋ฅผ ์ฒ๋ฆฌํ๊ณ , ์๋ฌ ๋ก๊ทธ๋ฅผ ๋จ๊ธฐ๊ฑฐ๋ ์ ์ ํ fallback ์ฒ๋ฆฌ๋ฅผ ํด์ผ ํฉ๋๋ค.
* **ํ๋์ฝ๋ฉ๋ API ์๋ํฌ์ธํธ:** `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent` ์ ๊ฐ์ API ์๋ํฌ์ธํธ๊ฐ ํ๋์ฝ๋ฉ๋์ด ์์ต๋๋ค. ํ๊ฒฝ ๋ณ์๋ก ๊ด๋ฆฌํ๊ฑฐ๋, ๋ ์ ์ฐํ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ฌ ์ถํ ๋ณ๊ฒฝ์ ๋๋นํด์ผ ํฉ๋๋ค.
* **๋นํจ์จ์ ์ธ ํจ์น ํ์ผ ์ฒ๋ฆฌ:** ๋ชจ๋ ํ์ผ์ `patch` ์ ์ฒด๋ฅผ Gemini API์ ์ ๋ฌํฉ๋๋ค. ํ์ผ์ด ๋งค์ฐ ํฌ๋ค๋ฉด API ์์ฒญ ์ ํ์ ๊ฑธ๋ฆด ์ ์์ต๋๋ค. ์์ ๋จ์๋ก ๋๋์ด ์ ๋ฌํ๊ฑฐ๋, ํ์ผ ํฌ๊ธฐ๋ฅผ ์ ํํ๋ ๋ก์ง์ด ํ์ํฉ๋๋ค.
* **๋ง์ง๋ง ์ค ๋ฒํธ:** `line: file.patch.split('\n').length` ๋ ํ์ผ์ ๋ง์ง๋ง ์ค์ ์ง์ ํฉ๋๋ค. ์ค์ ๋ณ๊ฒฝ ์ฌํญ์ด ์๋ ์ค์ ํน์ ํ๋ ๊ฒ์ด ๋ ์ ํํฉ๋๋ค. diff๋ฅผ ํ์ฑํ์ฌ ๋ณ๊ฒฝ๋ ์ค์ ์ฐพ๋ ๋ก์ง์ ์ถ๊ฐํด์ผ ํฉ๋๋ค.
**2. ์ ์ฌ์ ์ธ ๋ฒ๊ทธ๋ ๋ฌธ์ ์ :**
* **API ํค ๋
ธ์ถ:** `GEMINI_API_KEY` ๋ GitHub Secrets๋ก ๊ด๋ฆฌ๋์ง๋ง, ์ฝ๋ ์์ฒด์ ์ง์ ์ ์ผ๋ก ์ฌ์ฉ๋๋ฏ๋ก, ์ฝ๋๋ฒ ์ด์ค๊ฐ ์ ์ถ๋ ๊ฒฝ์ฐ API ํค๊ฐ ๋
ธ์ถ๋ ์ํ์ด ์์ต๋๋ค. API ํค๋ฅผ ์ง์ ์ฌ์ฉํ์ง ์๊ณ , ์ ์ ํ ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค. Google Cloud Client Library for Node.js๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๊ณ ๋ คํด ๋ณด์ธ์.
* **Gemini API์ ์๋ต ์ฒ๋ฆฌ:** `result.candidates[0].content.parts[0].text` ์ ๊ฐ์ด Gemini API์ ์๋ต ๊ตฌ์กฐ์ ์ง์ ์ ๊ทผํ๊ณ ์์ต๋๋ค. API ์๋ต ๊ตฌ์กฐ๊ฐ ๋ณ๊ฒฝ๋๋ฉด ์ฝ๋๊ฐ ๋์ํ์ง ์์ ์ ์์ต๋๋ค. ๋ ์์ ํ๊ณ ์ ์ฐํ ๋ฐฉ์์ผ๋ก ์๋ต์ ์ฒ๋ฆฌํด์ผ ํฉ๋๋ค. (์: ์๋ต์ ์ ํจ์ฑ ๊ฒ์ฌ)
* **๋๋์ Pull Request:** ๋งค์ฐ ํฐ Pull Request์ ๊ฒฝ์ฐ, API ํธ์ถ ํ์๊ฐ ๋ง์์ง๊ณ , GitHub Actions ์คํ ์๊ฐ ์ ํ์ ๊ฑธ๋ฆด ์ ์์ต๋๋ค.
* **๋น๋๊ธฐ ์ฒ๋ฆฌ ๋๋ฝ:** `for` ๋ฃจํ ๋ด๋ถ์์ ๋น๋๊ธฐ ํจ์ `reviewCode`๋ฅผ ํธ์ถํ์ง๋ง, ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ์์ฐจ์ ์ผ๋ก ๋ค์ ํ์ผ์ ์ฒ๋ฆฌํฉ๋๋ค. `Promise.all`์ ์ฌ์ฉํ์ฌ ๋ชจ๋ ๋ฆฌ๋ทฐ๊ฐ ์๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ๋ ๊ฒ์ด ์ข์ต๋๋ค.
**3. ์ฑ๋ฅ ๊ฐ์ ํฌ์ธํธ:**
* **๋ณ๋ ฌ ์ฒ๋ฆฌ:** `Promise.all` ์ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ํ์ผ์ ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ๋ณ๋ ฌ๋ก ์ฒ๋ฆฌํ์ฌ ์ ์ฒด ์ฒ๋ฆฌ ์๊ฐ์ ๋จ์ถํ ์ ์์ต๋๋ค.
* **์บ์ฑ:** ์ด์ ์ ๋ฆฌ๋ทฐํ ์ฝ๋๋ ์บ์ฑํ์ฌ ์ค๋ณต๋ API ํธ์ถ์ ์ค์ผ ์ ์์ต๋๋ค.
* **API ์์ฒญ ์ต์ ํ:** ํ ๋ฒ์ ์ฌ๋ฌ ํ์ผ์ ์ฒ๋ฆฌํ๊ฑฐ๋, ๋ ์์ ๋จ์๋ก ํ์ผ์ ๋ถํ ํ์ฌ API ์์ฒญ ํ์๋ฅผ ์ค์ผ ์ ์์ต๋๋ค.
**4. ๋ณด์ ๊ด๋ จ ์ด์:**
* **API ํค ๊ด๋ฆฌ:** ์์ ์ธ๊ธํ๋ฏ์ด, API ํค๋ฅผ ํ๊ฒฝ ๋ณ์๋ก ๊ด๋ฆฌํ๊ณ , ๊ฐ๋ฅํ๋ฉด Google Cloud Client Library๋ฅผ ์ฌ์ฉํ์ฌ API ํค๋ฅผ ์ง์ ๋
ธ์ถํ์ง ์๋๋ก ํด์ผ ํฉ๋๋ค.
* **์
๋ ฅ ๊ฒ์ฆ:** Gemini API์ ์ ๋ฌํ๋ ์ฝ๋์ ๋ํ ์
๋ ฅ ๊ฒ์ฆ์ด ํ์ํฉ๋๋ค. ์
์ฑ ์ฝ๋๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ API๋ฅผ ์
์ฉํ ์ ์์ต๋๋ค.
**5. ๊ฐ์ ์ ์ํ ๊ตฌ์ฒด์ ์ธ ์ ์:**
* **Google Cloud Client Library ์ฌ์ฉ:** Google Cloud Client Library for Node.js๋ฅผ ์ฌ์ฉํ์ฌ Gemini API๋ฅผ ๋ ์์ ํ๊ณ ํจ์จ์ ์ผ๋ก ํธ์ถํฉ๋๋ค.
* **์ค๋ฅ ์ฒ๋ฆฌ ๋ฐ ๋ก๊น
๊ฐํ:** `try...catch` ๋ธ๋ก์ ์ฌ์ฉํ์ฌ ์๋ฌ๋ฅผ ์ฒ๋ฆฌํ๊ณ , ์์ธํ ์๋ฌ ๋ก๊ทธ๋ฅผ ๋จ๊ฒจ ๋ฌธ์ ํด๊ฒฐ์ ์ฉ์ดํ๊ฒ ํฉ๋๋ค.
* **๋น๋๊ธฐ ์ฒ๋ฆฌ ๊ฐ์ :** `Promise.all`์ ์ฌ์ฉํ์ฌ ๋ณ๋ ฌ ์ฒ๋ฆฌ๋ฅผ ๊ตฌํํฉ๋๋ค.
* **์
๋ ฅ ๊ฒ์ฆ ์ถ๊ฐ:** Gemini API์ ์ ๋ฌํ๋ ์ฝ๋๋ฅผ ๊ฒ์ฆํ์ฌ ๋ณด์ ์ทจ์ฝ์ฑ์ ๋ฐฉ์งํฉ๋๋ค.
* **์๋ต ์ฒ๋ฆฌ ๊ฐ์ :** Gemini API์ ์๋ต ๊ตฌ์กฐ๋ฅผ ๊ณ ๋ คํ์ฌ ๋ ์์ ์ ์ธ ์๋ต ์ฒ๋ฆฌ ๋ก์ง์ ๊ตฌํํฉ๋๋ค. schema validation์ ๊ณ ๋ คํด๋ณผ ์ ์์ต๋๋ค.
* **์ฝ๋ ๋ถํ :** `reviewCode` ํจ์๋ฅผ ๋ ์๊ณ ํน์ ๊ธฐ๋ฅ์ ๋ด๋นํ๋ ์ฌ๋ฌ ํจ์๋ก ๋ถํ ํ์ฌ ๊ฐ๋
์ฑ๊ณผ ์ ์ง๋ณด์์ฑ์ ๋์
๋๋ค.
* **Diff ํ์ฑ:** `file.patch` ๋์ diff๋ฅผ ํ์ฑํ์ฌ ์ค์ ๋ณ๊ฒฝ๋ ์ค์ ์ฐพ์ ์ ํํ ์์น์ ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ๋ฅผ ๋จ๊น๋๋ค. `diff` ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๊ณ ๋ คํด๋ณด์ธ์.
* **Rate Limiting:** Gemini API์ rate limit์ ๊ณ ๋ คํ์ฌ ์์ฒญ ๋น๋๋ฅผ ์กฐ์ ํ๋ ๋ฉ์ปค๋์ฆ์ ์ถ๊ฐํด์ผ ํฉ๋๋ค.
์์ฝํ์๋ฉด, ์ด ์ฝ๋๋ ๊ธฐ๋ฅ์ ์ผ๋ก๋ ๋์ํ์ง๋ง, ๋ณด์, ์ค๋ฅ ์ฒ๋ฆฌ, ์ฑ๋ฅ ์ธก๋ฉด์์ ๊ฐ์ ์ด ํ์ํฉ๋๋ค. ์์์ ์ ์๋ ์ ์์ ๋ฐํ์ผ๋ก ์ฝ๋๋ฅผ ๊ฐ์ ํ๋ฉด ๋ ์์ ํ๊ณ ํจ์จ์ ์ธ ์ฝ๋ ๋ฆฌ๋ทฐ ์์คํ
์ ๊ตฌ์ถํ ์ ์์ต๋๋ค. ํนํ Google Cloud Client Library ์ฌ์ฉ์ ํ์์ ์ผ๋ก ๊ณ ๋ คํด์ผ ํ ๋ถ๋ถ์
๋๋ค. |
@@ -11,12 +11,178 @@ on:
jobs:
code_review:
runs-on: ubuntu-latest
- name: ChatGPT Code Review
+ name: Gemini Code Review
steps:
- - uses: anc95/ChatGPT-CodeReview@main
+ - uses: actions/checkout@v4
+
+ - name: Setup Node.js
+ uses: actions/setup-node@v4
+ with:
+ node-version: "20"
+
+ - name: Install dependencies
+ run: npm install @google/generative-ai parse-diff
+
+ - name: Review Changed Files
+ uses: actions/github-script@v7
env:
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
- # optional
- LANGUAGE: Korean
- MODEL: gpt-4o-mini-2024-07-18 # https://platform.openai.com/docs/models
+ GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
+ GEMINI_MODEL: ${{ secrets.GEMINI_MODEL }}
+ MAX_CONCURRENT_REVIEWS: 3 # ๋์์ ์ฒ๋ฆฌํ ํ์ผ ์
+ MAX_RETRIES: 3
+ RETRY_DELAY: 1000
+
+ with:
+ script: |
+ const { GoogleGenerativeAI } = require("@google/generative-ai");
+ const parseDiff = require('parse-diff');
+ // core๋ ์ด๋ฏธ github-script์์ ์ ๊ณต๋จ
+
+ // ์ฌ์๋ ๋ก์ง์ ํฌํจํ API ํธ์ถ ํจ์
+ async function withRetry(fn, retries = process.env.MAX_RETRIES) {
+ for (let i = 0; i < retries; i++) {
+ try {
+ return await fn();
+
+ } catch (error) {
+ if (error.message.includes('rate limit') && i < retries - 1) {
+ const delay = process.env.RETRY_DELAY * Math.pow(2, i);
+ console.log(`Rate limit hit, waiting ${delay}ms before retry ${i + 1}`);
+
+ await new Promise(resolve => setTimeout(resolve, delay));
+ continue;
+ }
+
+ throw error;
+ }
+ }
+ }
+
+ // Gemini AI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
+ const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
+ const model = genAI.getGenerativeModel({ model: process.env.GEMINI_MODEL });
+
+ /**
+ * ์ฝ๋ ๋ณ๊ฒฝ์ฌํญ์ ๊ฒํ ํ๊ณ ํผ๋๋ฐฑ์ ์์ฑ
+ *
+ * @param content ๋ฆฌ๋ทฐํ ์ฝ๋ ๋ด์ฉ
+ * @param filename ํ์ผ๋ช
+ * @returns ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ
+ */
+ async function reviewCode(content, filename) {
+ try {
+ // ํ์ผ๋ช
sanitize
+ const sanitizedFilename = filename.replace(/[^a-zA-Z0-9.-_\/]/g, '_');
+
+ const prompt = `๋น์ ์ ์๋์ด ๊ฐ๋ฐ์์
๋๋ค. ์๋ ${sanitizedFilename} ํ์ผ์ ์ฝ๋๋ฅผ ๊ฒํ ํ๊ณ ๋ค์ ์ฌํญ๋ค์ ํ๊ตญ์ด๋ก ๋ฆฌ๋ทฐํด์ฃผ์ธ์:
+ 1. ์ฝ๋์ ํ์ง๊ณผ ๊ฐ๋
์ฑ
+ 2. ์ ์ฌ์ ์ธ ๋ฒ๊ทธ๋ ๋ฌธ์ ์
+ 3. ์ฑ๋ฅ ๊ฐ์ ํฌ์ธํธ
+ 4. ๋ณด์ ๊ด๋ จ ์ด์
+ 5. ๊ฐ์ ์ ์ํ ๊ตฌ์ฒด์ ์ธ ์ ์
+
+ ์ฝ๋:
+ ${content}`;
+
+ const result = await withRetry(() => model.generateContent(prompt));
+ const response = await result.response;
+
+ if (!response.text()) {
+ throw new Error('Gemini API returned empty response');
+ }
+
+ console.log(`Successfully reviewed ${filename}`);
+ return response.text();
+
+ } catch (error) {
+ console.error(`Error reviewing ${filename}:`, error);
+ return `โ ๏ธ ์ฝ๋ ๋ฆฌ๋ทฐ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: ${error.message}
+ ๋ค์ ์๋ํ์๊ฑฐ๋ ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํด์ฃผ์ธ์.`;
+ }
+ }
+
+ /**
+ * PR์ ๊ฐ ํ์ผ์ ์ฒ๋ฆฌํ๊ณ ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ ์์ฑ
+ * @param file PR์์ ๋ณ๊ฒฝ๋ ํ์ผ ์ ๋ณด
+ * @param pr PR ์ ๋ณด
+ */
+ async function processFile(file, pr) {
+ if (file.status === 'removed') {
+ console.log(`Skipping removed file: ${file.filename}`);
+ return;
+ }
+
+ try {
+ if (!file.patch) {
+ console.warn(`No patch found for ${file.filename}, skipping.`);
+ return;
+ }
+
+ const diff = parseDiff(file.patch)[0];
+ if (!diff || !diff.chunks) {
+ console.log(`No valid diff found for ${file.filename}`);
+ return;
+ }
+
+ // ๋ชจ๋ ๋ณ๊ฒฝ์ฌํญ์ ํ๋๋ก ํฉ์น๊ธฐ (์ถ๊ฐ๋ ๋ถ๋ถ๋ง ํํฐ๋ง)
+ const changes = diff.chunks
+ .flatMap(chunk => chunk.changes)
+ .filter(change => change.type === 'add');
+
+ if (changes.length === 0) {
+ console.log(`No added changes found in ${file.filename}`);
+ return;
+ }
+
+ // ๋ณ๊ฒฝ์ฌํญ์ ํ๋์ ๋ฌธ์์ด๋ก ํฉ์น๊ธฐ
+ const content = changes.map(change => change.content).join('\n');
+ const review = await reviewCode(content, file.filename);
+
+ // PR์ ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ ์์ฑ (ํ์ผ๋ช
์ ํค๋๋ก ์ถ๊ฐ)
+ return github.rest.issues.createComment({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ issue_number: context.issue.number,
+ body: `## ${file.filename} ๋ฆฌ๋ทฐ\n\n${review}`
+ });
+
+ } catch (error) {
+ console.error(`Error processing ${file.filename}:`, error);
+ throw error;
+ }
+ }
+
+ try {
+ // PR ์ ๋ณด ์กฐํ
+ const { data: pr } = await github.rest.pulls.get({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ pull_number: context.issue.number,
+ });
+
+ // ๋ณ๊ฒฝ๋ ํ์ผ ๋ชฉ๋ก ์กฐํ
+ const { data: files } = await github.rest.pulls.listFiles({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ pull_number: context.issue.number,
+ });
+
+ console.log(`Starting review of ${files.length} files...`);
+
+ // ๋ชจ๋ ํ์ผ ์ฒ๋ฆฌ ์๋ฃ๋ฅผ ๊ธฐ๋ค๋ฆผ
+ const promises = [];
+ const batchSize = parseInt(process.env.MAX_CONCURRENT_REVIEWS);
+
+ for (let i = 0; i < files.length; i += batchSize) {
+ const batch = files.slice(i, i + batchSize);
+ const batchPromises = batch.map(file => processFile(file, pr));
+ promises.push(...batchPromises);
+ }
+
+ await Promise.all(promises);
+ console.log('All reviews completed successfully');
+
+ } catch (error) {
+ console.error('Workflow failed:', error);
+ core.setFailed(`Workflow failed: ${error.message}`);
+ } | Unknown | ## Gemini Code Review Workflow ํ์ผ ๋ฆฌ๋ทฐ (code_review.yml)
๋ค์์ ์ ๊ณต๋ `code_review.yml` ํ์ผ์ ๋ํ ์์ธ ๋ฆฌ๋ทฐ์
๋๋ค. ์๋์ด ๊ฐ๋ฐ์์ ๊ด์ ์์ ์ฝ๋ ํ์ง, ์ ์ฌ์ ๋ฌธ์ ์ , ์ฑ๋ฅ ๊ฐ์ , ๋ณด์ ์ด์ ๋ฐ ๊ฐ์ ์ ์์ ๋ค๋ฃน๋๋ค.
**1. ์ฝ๋์ ํ์ง๊ณผ ๊ฐ๋
์ฑ**
* **์ ๋ฐ์ ์ผ๋ก ์ํธ:** ์ฝ๋์ ๊ตฌ์กฐ๊ฐ ๋น๊ต์ ๋ช
ํํ๊ณ , ์ฃผ์์ด ์ ์ ํ๊ฒ ์ฌ์ฉ๋์ด ๊ฐ๋
์ฑ์ด ๋์ต๋๋ค. ํจ์๋ณ ์ญํ ๋ถ๋ด๋ ์ ๋์ด ์์ต๋๋ค.
* **ํจ์๋ช
:** ํจ์๋ช
(`reviewCode`, `processFile`)์ ์ญํ ์ ๋ช
ํํ ์ค๋ช
ํ๋ฉฐ, ์ฝ๋ ์ดํด๋๋ฅผ ๋์
๋๋ค.
* **์ฃผ์:** ๊ฐ ํจ์ ๋ฐ ์ฃผ์ ๋ก์ง์ ๋ํ ์ค๋ช
์ฃผ์์ด ์กด์ฌํ์ฌ ์ฝ๋์ ์๋๋ฅผ ํ์
ํ๊ธฐ ์ฉ์ดํฉ๋๋ค. ํ์ง๋ง ๋ ์์ธํ ์ค๋ช
์ด ํ์ํ ๋ถ๋ถ๋ ์์ต๋๋ค (์: `chunk` ํจ์).
* **๋ณ์๋ช
:** ๋ณ์๋ช
์ ์ ๋ฐ์ ์ผ๋ก ๋ช
ํํ๊ณ ์๋ฏธ๋ฅผ ์ ๋ํ๋
๋๋ค.
**2. ์ ์ฌ์ ์ธ ๋ฒ๊ทธ๋ ๋ฌธ์ ์ **
* **์๋ฌ ํธ๋ค๋ง:** `reviewCode` ํจ์์์ Gemini API ํธ์ถ ์คํจ ์ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ๋ฐํํ์ง๋ง, ์ดํ ํ๋ก์ธ์ค์์ ํด๋น ์ค๋ฅ๋ฅผ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ๋์ง์ ๋ํ ๊ณ ๋ ค๊ฐ ๋ถ์กฑํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ์ค๋ฅ๊ฐ ๋ฐ์ํ ํ์ผ์ ๋ํ ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ ์์ฑ์ ์ค๋จํ๊ณ , ๋ค๋ฅธ ํ์ผ๋ค์ ๊ณ์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์ ์ ํ์ง ํ์ธํด์ผ ํฉ๋๋ค.
* **API Rate Limiting:** `MAX_CONCURRENT_REVIEWS`๋ฅผ ์ฌ์ฉํ์ฌ rate limiting์ ๋ฐฉ์งํ๋ ๊ฒ์ ์ข์ ์ ๋ต์ด์ง๋ง, Gemini API ์์ฒด๊ฐ rate limiting์ ์ ์ฉํ ์ ์์ผ๋ฏ๋ก, ๋ robustํ ์๋ฌ ์ฒ๋ฆฌ ๋ฐ ์ฌ์๋ ๋ก์ง์ด ํ์ํ ์ ์์ต๋๋ค. ํนํ, ์ค๋ฅ ๋ฉ์์ง๋ฅผ ํ์ฑํ์ฌ rate limit ๊ด๋ จ ์๋ฌ์ธ์ง ํ์ธํ๊ณ , ์ง์ ๋ฐฑ์คํ(exponential backoff) ์ ๋ต์ ์ ์ฉํ๋ ๊ฒ์ ๊ณ ๋ คํด๋ณผ ์ ์์ต๋๋ค.
* **diff ํ์ฑ ์คํจ:** `parseDiff(file.patch)[0]`์์ `file.patch`๊ฐ ์ ํจํ์ง ์์ ๊ฒฝ์ฐ (์: binary ํ์ผ), ์๋ฌ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค. `parseDiff` ํจ์์ ๊ฒฐ๊ณผ๋ฅผ ์ ๋๋ก ๊ฒ์ฌํ๊ณ , ์๋ฌ ๋ฐ์ ์ ์ ์ ํ ๋ก๊น
๋๋ ์คํต ์ฒ๋ฆฌ๋ฅผ ํด์ผ ํฉ๋๋ค.
* **๋น ์๋ต ์ฒ๋ฆฌ:** `response.text()`๊ฐ ๋น์ด์๋ ๊ฒฝ์ฐ ์๋ฌ๋ฅผ ๋์ง์ง๋ง, ์ด ๊ฒฝ์ฐ GitHub์ ์ด๋ค ์ฝ๋ฉํธ๊ฐ ๋จ๊ฒจ์ง๋์ง ๋ช
ํํ์ง ์์ต๋๋ค. ๋น ์๋ต์ ๋ํ ๋ ์ฌ์ฉ์ ์นํ์ ์ธ ๋ฉ์์ง (์: "Gemini API์์ ์ ํจํ ์๋ต์ ๋ฐ์ง ๋ชปํ์ต๋๋ค.")๋ฅผ ๋จ๊ธฐ๋ ๊ฒ์ด ์ข์ต๋๋ค.
* **`lineNumber` ๋ณ์:** `lineNumber`๋ ์ฒญํฌ์ ์์ ๋ผ์ธ์ ๋ํ๋ด์ง๋ง, ๋ฆฌ๋ทฐ๊ฐ ํน์ ์ฝ๋ ๋ผ์ธ์ ๋ํ ๊ฒ์ผ ๊ฒฝ์ฐ ํด๋น ๋ผ์ธ์ ์ ํํ ๊ฐ๋ฆฌํค์ง ์์ ์ ์์ต๋๋ค. ๋์ฑ ์ ๋ฐํ ๋ฆฌ๋ทฐ๋ฅผ ์ํด change๊ฐ ๋ฐ์ํ ๋ผ์ธ ๋ฒํธ๋ฅผ ํน์ ํ๋ ๋ก์ง์ ์ถ๊ฐํ๋ ๊ฒ์ ๊ณ ๋ คํด๋ณผ ์ ์์ต๋๋ค.
* **์ญ์ ๋ ํ์ผ ์ฒ๋ฆฌ:** `file.status === 'removed'`์ธ ๊ฒฝ์ฐ ์คํตํ์ง๋ง, ์ค์ ๋ก ์ญ์ ๋ ํ์ผ์ ๋ํ ์ฝ๋ฉํธ๋ฅผ ๋จ๊ธฐ๋ ๊ฒ์ด ์ ์ฉํ ๊ฒฝ์ฐ๋ ์์ ์ ์์ต๋๋ค (์: ์ ์ญ์ ๋์๋์ง, ๋์ฒด ์ฝ๋๋ ๋ฌด์์ธ์ง ๋ฑ). ์ํฉ์ ๋ฐ๋ผ ์ญ์ ๋ ํ์ผ์ ๋ํ ๊ฐ๋จํ ์ฝ๋ฉํธ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ ๊ณ ๋ คํด๋ณผ ์ ์์ต๋๋ค.
**3. ์ฑ๋ฅ ๊ฐ์ ํฌ์ธํธ**
* **์ฒญํฌ ์ฒ๋ฆฌ:** ํ์ฌ ์ฝ๋๋ ๊ฐ ์ฒญํฌ๋ณ๋ก `reviewCode` ํจ์๋ฅผ ํธ์ถํฉ๋๋ค. ์ด๋ Gemini API ํธ์ถ ํ์๋ฅผ ๋๋ฆฌ๊ณ ์ ์ฒด ์คํ ์๊ฐ์ ์ฆ๊ฐ์ํฌ ์ ์์ต๋๋ค. ๊ฐ๋ฅํ๋ค๋ฉด, ์ฌ๋ฌ ์ฒญํฌ๋ฅผ ๋ฌถ์ด์ ํ ๋ฒ์ `reviewCode` ํจ์๋ฅผ ํธ์ถํ๋ ๊ฒ์ ๊ณ ๋ คํด๋ณผ ์ ์์ต๋๋ค. (ํ๋กฌํํธ ํฌ๊ธฐ ์ ํ์ ๊ณ ๋ คํด์ผ ํจ)
* **๋ณ๋ ฌ ์ฒ๋ฆฌ:** ํ์ฌ ํ์ผ ๋ชฉ๋ก์ ์ํํ๋ฉด์ `processFile` ํจ์๋ฅผ ๋ณ๋ ฌ๋ก ์คํํ์ง๋ง, `processFile` ํจ์ ๋ด์์๋ ์ฒญํฌ๋ค์ ๋ณ๋ ฌ๋ก ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. `Promise.all`์ ์ฌ์ฉํ์ฌ ์ฒญํฌ ์ฒ๋ฆฌ ์๋๋ฅผ ๊ฐ์ ํ ์ ์์ต๋๋ค. ๋ค๋ง, Gemini API์ rate limiting์ ๋์ฑ ์ฃผ์ํด์ผ ํฉ๋๋ค.
* **์บ์ฑ:** ๋ง์ฝ ๋์ผํ ์ฝ๋๊ฐ ์ฌ๋ฌ ๋ฒ ๋ฆฌ๋ทฐ๋๋ ๊ฒฝ์ฐ, Gemini API์ ์๋ต์ ์บ์ฑํ์ฌ ๋ถํ์ํ API ํธ์ถ์ ์ค์ผ ์ ์์ต๋๋ค. (์บ์ฑ ์ ๋ต ๋ฐ ๋ง๋ฃ ์๊ฐ์ ์ ์คํ๊ฒ ๊ฒฐ์ ํด์ผ ํจ)
**4. ๋ณด์ ๊ด๋ จ ์ด์**
* **API Key ๊ด๋ฆฌ:** Gemini API ํค(`GEMINI_API_KEY`)๋ฅผ GitHub Secrets์ ์์ ํ๊ฒ ์ ์ฅํ๋ ๊ฒ์ ๋งค์ฐ ์ค์ํฉ๋๋ค.
* **ํ๋กฌํํธ ์ธ์ ์
:** Gemini API์ ์ ๋ฌ๋๋ ํ๋กฌํํธ์ ์ฌ์ฉ์ ์
๋ ฅ (ํนํ `filename`)์ด ํฌํจ๋์ด ์์ต๋๋ค. ์
์์ ์ธ ์ฌ์ฉ์๊ฐ ํ์ผ ์ด๋ฆ์ ์กฐ์ํ์ฌ ์๋์น ์์ ๊ฒฐ๊ณผ๋ฅผ ์ด๋ํ ์ ์๋ ํ๋กฌํํธ ์ธ์ ์
๊ณต๊ฒฉ์ ์ทจ์ฝํ ์ ์์ต๋๋ค. ํ์ผ ์ด๋ฆ์ ๋ํ ์ ํจ์ฑ ๊ฒ์ฌ ๋๋ ํํฐ๋ง์ ์ถ๊ฐํ๋ ๊ฒ์ ๊ณ ๋ คํด์ผ ํฉ๋๋ค.
* **์ฝ๋ ์คํ:** `actions/github-script` ์ก์
์ JavaScript ์ฝ๋๋ฅผ ์คํํ๋ฏ๋ก, ์์กด์ฑ ํจํค์ง(`@google/generative-ai`, `parse-diff`)์ ๋ํ ๋ณด์ ์ทจ์ฝ์ ๊ด๋ฆฌ๊ฐ ์ค์ํฉ๋๋ค. ์ ๊ธฐ์ ์ผ๋ก ์์กด์ฑ ํจํค์ง๋ฅผ ์
๋ฐ์ดํธํ๊ณ , ๋ณด์ ๊ฒ์ฌ๋ฅผ ์ํํด์ผ ํฉ๋๋ค.
**5. ๊ฐ์ ์ ์ํ ๊ตฌ์ฒด์ ์ธ ์ ์**
* **์์ธํ ๋ก๊น
:** ๊ฐ ๋จ๊ณ๋ณ๋ก ๋ก๊ทธ๋ฅผ ์ถ๊ฐํ์ฌ ๋๋ฒ๊น
๋ฐ ๋ชจ๋ํฐ๋ง์ ์ฉ์ดํ๊ฒ ํฉ๋๋ค. ํนํ, API ํธ์ถ ์ฑ๊ณต/์คํจ ์ฌ๋ถ, rate limiting ๋ฐ์ ์ฌ๋ถ, ํ์ผ ์ฒ๋ฆฌ ์๊ฐ ๋ฑ์ ๋ก๊น
ํฉ๋๋ค.
* **์๋ฌ ์ฒ๋ฆฌ ๊ฐ์ :** `reviewCode` ํจ์์์ ๋ฐ์ํ ์ค๋ฅ๋ฅผ ๋ ์์ธํ ์ฒ๋ฆฌํ๊ณ , GitHub ์ฝ๋ฉํธ๋ฅผ ํตํด ์ฌ์ฉ์์๊ฒ ์๋ฆฝ๋๋ค. ์๋ฅผ ๋ค์ด, ์ค๋ฅ ์ ํ (API ์ค๋ฅ, rate limiting, ํ์ฑ ์ค๋ฅ ๋ฑ)์ ๋ช
์ํ๊ณ , ๋ฌธ์ ํด๊ฒฐ์ ์ํ ๊ฐ์ด๋๋ผ์ธ์ ์ ๊ณตํฉ๋๋ค.
* **ํ
์คํธ ์ผ์ด์ค ์ถ๊ฐ:** ์ฝ๋ ๋ณ๊ฒฝ ์ฌํญ์ ๊ฒ์ฆํ๊ธฐ ์ํ ํ
์คํธ ์ผ์ด์ค๋ฅผ ์ถ๊ฐํฉ๋๋ค. ํนํ, ๋ค์ํ ํ์ผ ํ์ (ํ
์คํธ, JSON, YAML ๋ฑ) ๋ฐ diff ํ์์ ๋ํ ํ
์คํธ๋ฅผ ์ํํ์ฌ ์ฝ๋์ ์์ ์ฑ์ ํ๋ณดํฉ๋๋ค.
* **๊ตฌ์ฑ ์ต์
์ถ๊ฐ:** `MAX_CONCURRENT_REVIEWS` ์ธ์๋, Gemini ๋ชจ๋ธ (`GEMINI_MODEL`) ๋ฐ ํ๋กฌํํธ ํ
ํ๋ฆฟ์ ๊ตฌ์ฑ ์ต์
์ผ๋ก ์ ๊ณตํ์ฌ ์ฌ์ฉ์๊ฐ ํ์์ ๋ฐ๋ผ ์ค์ ์ ๋ณ๊ฒฝํ ์ ์๋๋ก ํฉ๋๋ค.
* **์ฝ๋ ๋ฆฌ๋ทฐ ๊ฒฐ๊ณผ ์์ฝ:** ๊ฐ ํ์ผ๋ณ ๋ฆฌ๋ทฐ ๊ฒฐ๊ณผ๋ฅผ ์์ฝํ์ฌ PR์ ๋ํ ์ ์ฒด์ ์ธ ์ฝ๋ ํ์ง์ ํ์
ํ๊ธฐ ์ฝ๊ฒ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด, "์ด X๊ฐ์ ํ์ผ์์ Y๊ฐ์ ๋ฌธ์ ์ ์ด ๋ฐ๊ฒฌ๋์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ ๊ฐ ํ์ผ์ ์ฝ๋ฉํธ๋ฅผ ์ฐธ์กฐํ์ธ์."์ ๊ฐ์ ์์ฝ ์ ๋ณด๋ฅผ ์ ๊ณตํ ์ ์์ต๋๋ค.
* **ํ๋กฌํํธ ์ต์ ํ:** ์ฝ๋ ๋ฆฌ๋ทฐ ํ์ง์ ๋์ด๊ธฐ ์ํด Gemini API์ ์ ๋ฌ๋๋ ํ๋กฌํํธ๋ฅผ ์ง์์ ์ผ๋ก ๊ฐ์ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ์ฝ๋ ๋ฆฌ๋ทฐ ๊ธฐ์ค์ ๋์ฑ ๋ช
ํํ๊ฒ ์ ์ํ๊ฑฐ๋, ํน์ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ๋๋ ํ๋ ์์ํฌ์ ๋ํ ์ง์นจ์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
* **PR ํ
ํ๋ฆฟ ํ์ฉ:** PR ์์ฑ์๊ฐ ์ฝ๋ ๋ณ๊ฒฝ ์ฌํญ์ ๋ํ ์ ๋ณด๋ฅผ ๋ฏธ๋ฆฌ ์ ๊ณตํ๋๋ก PR ํ
ํ๋ฆฟ์ ํ์ฉํฉ๋๋ค. ์ด๋ Gemini API๊ฐ ๋์ฑ ์ ํํ๊ณ ๊ด๋ จ์ฑ ๋์ ๋ฆฌ๋ทฐ๋ฅผ ์์ฑํ๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค.
**๊ฒฐ๋ก :**
์ ๋ฐ์ ์ผ๋ก ์ ์์ฑ๋ ์ํฌํ๋ก์ฐ ํ์ผ์ด์ง๋ง, ์๋ฌ ์ฒ๋ฆฌ, API Rate Limiting, ๋ณด์ ๋ฐ ์ฑ๋ฅ ์ธก๋ฉด์์ ๊ฐ์ ํ ์ฌ์ง๊ฐ ์์ต๋๋ค. ์ ์๋ ๊ฐ์ ์ฌํญ๋ค์ ์ ์ฉํ์ฌ ์ฝ๋ ๋ฆฌ๋ทฐ ํ๋ก์ธ์ค๋ฅผ ๋์ฑ ์์ ์ ์ด๊ณ ํจ์จ์ ์ผ๋ก ๋ง๋ค ์ ์์ต๋๋ค. ํนํ, ์ค์ ์ด์ ํ๊ฒฝ์์ ๋ฐ์ํ ์ ์๋ ๋ค์ํ ์์ธ ์ํฉ์ ๊ณ ๋ คํ์ฌ ์ฝ๋๋ฅผ ๋ณด์ํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค. |
@@ -11,12 +11,178 @@ on:
jobs:
code_review:
runs-on: ubuntu-latest
- name: ChatGPT Code Review
+ name: Gemini Code Review
steps:
- - uses: anc95/ChatGPT-CodeReview@main
+ - uses: actions/checkout@v4
+
+ - name: Setup Node.js
+ uses: actions/setup-node@v4
+ with:
+ node-version: "20"
+
+ - name: Install dependencies
+ run: npm install @google/generative-ai parse-diff
+
+ - name: Review Changed Files
+ uses: actions/github-script@v7
env:
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
- # optional
- LANGUAGE: Korean
- MODEL: gpt-4o-mini-2024-07-18 # https://platform.openai.com/docs/models
+ GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
+ GEMINI_MODEL: ${{ secrets.GEMINI_MODEL }}
+ MAX_CONCURRENT_REVIEWS: 3 # ๋์์ ์ฒ๋ฆฌํ ํ์ผ ์
+ MAX_RETRIES: 3
+ RETRY_DELAY: 1000
+
+ with:
+ script: |
+ const { GoogleGenerativeAI } = require("@google/generative-ai");
+ const parseDiff = require('parse-diff');
+ // core๋ ์ด๋ฏธ github-script์์ ์ ๊ณต๋จ
+
+ // ์ฌ์๋ ๋ก์ง์ ํฌํจํ API ํธ์ถ ํจ์
+ async function withRetry(fn, retries = process.env.MAX_RETRIES) {
+ for (let i = 0; i < retries; i++) {
+ try {
+ return await fn();
+
+ } catch (error) {
+ if (error.message.includes('rate limit') && i < retries - 1) {
+ const delay = process.env.RETRY_DELAY * Math.pow(2, i);
+ console.log(`Rate limit hit, waiting ${delay}ms before retry ${i + 1}`);
+
+ await new Promise(resolve => setTimeout(resolve, delay));
+ continue;
+ }
+
+ throw error;
+ }
+ }
+ }
+
+ // Gemini AI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
+ const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
+ const model = genAI.getGenerativeModel({ model: process.env.GEMINI_MODEL });
+
+ /**
+ * ์ฝ๋ ๋ณ๊ฒฝ์ฌํญ์ ๊ฒํ ํ๊ณ ํผ๋๋ฐฑ์ ์์ฑ
+ *
+ * @param content ๋ฆฌ๋ทฐํ ์ฝ๋ ๋ด์ฉ
+ * @param filename ํ์ผ๋ช
+ * @returns ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ
+ */
+ async function reviewCode(content, filename) {
+ try {
+ // ํ์ผ๋ช
sanitize
+ const sanitizedFilename = filename.replace(/[^a-zA-Z0-9.-_\/]/g, '_');
+
+ const prompt = `๋น์ ์ ์๋์ด ๊ฐ๋ฐ์์
๋๋ค. ์๋ ${sanitizedFilename} ํ์ผ์ ์ฝ๋๋ฅผ ๊ฒํ ํ๊ณ ๋ค์ ์ฌํญ๋ค์ ํ๊ตญ์ด๋ก ๋ฆฌ๋ทฐํด์ฃผ์ธ์:
+ 1. ์ฝ๋์ ํ์ง๊ณผ ๊ฐ๋
์ฑ
+ 2. ์ ์ฌ์ ์ธ ๋ฒ๊ทธ๋ ๋ฌธ์ ์
+ 3. ์ฑ๋ฅ ๊ฐ์ ํฌ์ธํธ
+ 4. ๋ณด์ ๊ด๋ จ ์ด์
+ 5. ๊ฐ์ ์ ์ํ ๊ตฌ์ฒด์ ์ธ ์ ์
+
+ ์ฝ๋:
+ ${content}`;
+
+ const result = await withRetry(() => model.generateContent(prompt));
+ const response = await result.response;
+
+ if (!response.text()) {
+ throw new Error('Gemini API returned empty response');
+ }
+
+ console.log(`Successfully reviewed ${filename}`);
+ return response.text();
+
+ } catch (error) {
+ console.error(`Error reviewing ${filename}:`, error);
+ return `โ ๏ธ ์ฝ๋ ๋ฆฌ๋ทฐ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: ${error.message}
+ ๋ค์ ์๋ํ์๊ฑฐ๋ ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํด์ฃผ์ธ์.`;
+ }
+ }
+
+ /**
+ * PR์ ๊ฐ ํ์ผ์ ์ฒ๋ฆฌํ๊ณ ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ ์์ฑ
+ * @param file PR์์ ๋ณ๊ฒฝ๋ ํ์ผ ์ ๋ณด
+ * @param pr PR ์ ๋ณด
+ */
+ async function processFile(file, pr) {
+ if (file.status === 'removed') {
+ console.log(`Skipping removed file: ${file.filename}`);
+ return;
+ }
+
+ try {
+ if (!file.patch) {
+ console.warn(`No patch found for ${file.filename}, skipping.`);
+ return;
+ }
+
+ const diff = parseDiff(file.patch)[0];
+ if (!diff || !diff.chunks) {
+ console.log(`No valid diff found for ${file.filename}`);
+ return;
+ }
+
+ // ๋ชจ๋ ๋ณ๊ฒฝ์ฌํญ์ ํ๋๋ก ํฉ์น๊ธฐ (์ถ๊ฐ๋ ๋ถ๋ถ๋ง ํํฐ๋ง)
+ const changes = diff.chunks
+ .flatMap(chunk => chunk.changes)
+ .filter(change => change.type === 'add');
+
+ if (changes.length === 0) {
+ console.log(`No added changes found in ${file.filename}`);
+ return;
+ }
+
+ // ๋ณ๊ฒฝ์ฌํญ์ ํ๋์ ๋ฌธ์์ด๋ก ํฉ์น๊ธฐ
+ const content = changes.map(change => change.content).join('\n');
+ const review = await reviewCode(content, file.filename);
+
+ // PR์ ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ ์์ฑ (ํ์ผ๋ช
์ ํค๋๋ก ์ถ๊ฐ)
+ return github.rest.issues.createComment({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ issue_number: context.issue.number,
+ body: `## ${file.filename} ๋ฆฌ๋ทฐ\n\n${review}`
+ });
+
+ } catch (error) {
+ console.error(`Error processing ${file.filename}:`, error);
+ throw error;
+ }
+ }
+
+ try {
+ // PR ์ ๋ณด ์กฐํ
+ const { data: pr } = await github.rest.pulls.get({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ pull_number: context.issue.number,
+ });
+
+ // ๋ณ๊ฒฝ๋ ํ์ผ ๋ชฉ๋ก ์กฐํ
+ const { data: files } = await github.rest.pulls.listFiles({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ pull_number: context.issue.number,
+ });
+
+ console.log(`Starting review of ${files.length} files...`);
+
+ // ๋ชจ๋ ํ์ผ ์ฒ๋ฆฌ ์๋ฃ๋ฅผ ๊ธฐ๋ค๋ฆผ
+ const promises = [];
+ const batchSize = parseInt(process.env.MAX_CONCURRENT_REVIEWS);
+
+ for (let i = 0; i < files.length; i += batchSize) {
+ const batch = files.slice(i, i + batchSize);
+ const batchPromises = batch.map(file => processFile(file, pr));
+ promises.push(...batchPromises);
+ }
+
+ await Promise.all(promises);
+ console.log('All reviews completed successfully');
+
+ } catch (error) {
+ console.error('Workflow failed:', error);
+ core.setFailed(`Workflow failed: ${error.message}`);
+ } | Unknown | ## .github/workflows/code_review.yml ์ฝ๋ ๋ฆฌ๋ทฐ
๋ค์์ ์ฃผ์ด์ง `.github/workflows/code_review.yml` ํ์ผ์ ๋ํ ์ฝ๋ ๋ฆฌ๋ทฐ์
๋๋ค.
**1. ์ฝ๋ ํ์ง ๋ฐ ๊ฐ๋
์ฑ**
* **์ ๋ฐ์ ์ผ๋ก ์ํธ:** ์ฝ๋๋ ๋น๊ต์ ๊น๋ํ๊ณ ๊ฐ๋
์ฑ์ด ์ข์ต๋๋ค. ์ฃผ์๋ ์ ์ ํ๊ฒ ๋ฌ๋ ค ์์ด ์ฝ๋์ ์๋๋ฅผ ํ์
ํ๋ ๋ฐ ๋์์ด ๋ฉ๋๋ค.
* **์ผ๊ด์ฑ:** ๋ค์ฌ์ฐ๊ธฐ, ๋ณ์๋ช
๊ท์น ๋ฑ์ด ์ผ๊ด์ฑ์ ์ ์งํ๊ณ ์์ต๋๋ค.
* **๋ชจ๋ํ:** `reviewCode`, `processFile` ๋ฑ์ ํจ์๋ก ๋ก์ง์ ๋ถ๋ฆฌํ์ฌ ์ฌ์ฌ์ฉ์ฑ๊ณผ ์ ์ง๋ณด์์ฑ์ ๋์์ต๋๋ค.
* **ํต๊ณ ์์ง:** `stats` ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ๋ ๋ฆฌ๋ทฐ ๊ณผ์ ์ ๋ํ ํต๊ณ๋ฅผ ์์งํ๋ ๊ฒ์ ์ ์ฉํฉ๋๋ค. ๋ฌธ์ ๋ฐ์ ์ ๋๋ฒ๊น
์ ๋์์ด ๋ ์ ์์ต๋๋ค.
**2. ์ ์ฌ์ ์ธ ๋ฒ๊ทธ ๋๋ ๋ฌธ์ ์ **
* **์๋ฌ ํธ๋ค๋ง:** `try...catch` ๋ธ๋ก์ ์ฌ์ฉํ์ฌ ์๋ฌ๋ฅผ ์ฒ๋ฆฌํ๊ณ ์์ง๋ง, ์๋ฌ ๋ฉ์์ง๋ฅผ ์ฌ์ฉ์์๊ฒ ์น์ ํ๊ฒ ๋ณด์ฌ์ฃผ๋ ๊ฒ์ ์ค์ํฉ๋๋ค. `reviewCode` ํจ์์์ ์๋ฌ ๋ฐ์ ์ "โ ๏ธ ์ฝ๋ ๋ฆฌ๋ทฐ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค..."์ ๊ฐ์ ๋ฉ์์ง๋ฅผ ๋ณด์ฌ์ฃผ๋ ๊ฒ์ ์ข์ ์์์
๋๋ค.
* **Rate Limit ์ฒ๋ฆฌ:** `withRetry` ํจ์๋ฅผ ํตํด rate limit์ ์ฒ๋ฆฌํ๋ ๊ฒ์ ์ข์ต๋๋ค. ํ์ง๋ง `RETRY_DELAY`๋ฅผ ๊ณ ์ ๊ฐ์ผ๋ก ์ฌ์ฉํ๋ ๊ฒ๋ณด๋ค, ๋ฐฑ์คํ(backoff) ์ ๋ต์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ์๋ฅผ ๋ค์ด, ๋งค retry๋ง๋ค ๋๋ ์ด๋ฅผ ๋๋ฆฌ๋ ๋ฐฉ์์
๋๋ค.
* **`file.patch` ์๋ฌ ์ฒ๋ฆฌ:** `parseDiff(file.patch)[0]` ๋ถ๋ถ์์, `file.patch`๊ฐ ์กด์ฌํ์ง ์๊ฑฐ๋ ์ ํจํ์ง ์์ ๊ฒฝ์ฐ์ ๋ํ ์์ธ ์ฒ๋ฆฌ๊ฐ ํ์ํ ์ ์์ต๋๋ค. `parseDiff` ํจ์๊ฐ ์๋ฌ๋ฅผ ๋์ง ๊ฐ๋ฅ์ฑ์ด ์๋์ง ํ์ธํ๊ณ , ์๋ค๋ฉด `try...catch` ๋ธ๋ก์ผ๋ก ๊ฐ์ธ๋ ๊ฒ์ ๊ณ ๋ คํ์ญ์์ค.
* **ํ์ผ ์ด๋ฆ sanitize:** ํ์ผ ์ด๋ฆ sanitize ๋ก์ง์ด ์ถฉ๋ถํ ์์ ํ์ง ๊ฒํ ํด์ผ ํฉ๋๋ค. ํนํ ํน์๋ฌธ์๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ ์์์น ๋ชปํ ๊ฒฐ๊ณผ๋ฅผ ์ด๋ํ ์ ์์ต๋๋ค.
* **๋์์ฑ ์ฒ๋ฆฌ:** `MAX_CONCURRENT_REVIEWS`๋ฅผ ์ฌ์ฉํ์ฌ ๋์์ฑ ์ฒ๋ฆฌ๋ฅผ ํ๋ ๊ฒ์ ์ข์ง๋ง, ํ์ผ ์๊ฐ ์ ์ ๊ฒฝ์ฐ ๋ณ๋ ฌ ์ฒ๋ฆฌ๊ฐ ์คํ๋ ค ์ค๋ฒํค๋๊ฐ ๋ ์ ์์ต๋๋ค. ํ์ผ ์์ ๋ฐ๋ผ ๋์์ฑ ์ฒ๋ฆฌ๋ฅผ ํ์ฑํ/๋นํ์ฑํํ๋ ๋ก์ง์ ์ถ๊ฐํ๋ ๊ฒ์ ๊ณ ๋ คํด๋ณผ ์ ์์ต๋๋ค.
* **`summary` ์๋ฌ ๋ฉ์์ง ๋๋ฝ:** `stats.issues`์ ๋ด๊ธด ์๋ฌ ๋ฉ์์ง๊ฐ ๋งค์ฐ ๊ธธ ๊ฒฝ์ฐ, summary ์ฝ๋ฉํธ์ ์ ๋ถ ๋ด๊ธฐ์ง ์์ ์ ์์ต๋๋ค. summary ์ฝ๋ฉํธ์ ์ต๋ ๊ธธ์ด๋ฅผ ๊ณ ๋ คํ์ฌ ๋ฉ์์ง๋ฅผ ์๋ฅด๊ฑฐ๋ ์์ฝํ๋ ๋ฐฉ์์ ๊ณ ๋ คํด๋ณผ ์ ์์ต๋๋ค.
* **๋ฆฌ๋ทฐ ์ฝ๋ฉํธ ์์น:** ์ฒซ ๋ฒ์งธ ์ฒญํฌ์ ์์ ๋ผ์ธ์๋ง ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ๋ฅผ ์์ฑํ๋ ๊ฒ์ ๋ณ๊ฒฝ ์ฌํญ์ด ์ฌ๋ฌ ์ฒญํฌ์ ๊ฑธ์ณ ์์ ๊ฒฝ์ฐ ๋๋ฝ๋๋ ๋ถ๋ถ์ด ์์ ์ ์์ต๋๋ค. ๊ฐ ์ฒญํฌ๋ณ๋ก ์ฝ๋ฉํธ๋ฅผ ์์ฑํ๊ฑฐ๋, ๋ณ๊ฒฝ ์ฌํญ ์ ์ฒด์ ๋ํ ์ฝ๋ฉํธ๋ฅผ ์์ฑํ๋ ๊ฒ์ ๊ณ ๋ คํด๋ณผ ์ ์์ต๋๋ค.
* **`create-or-update-comment` ์ก์
์ ์ค๋ณต ํค๋:** workflow ๋ง์ง๋ง ๋ถ๋ถ์ "## Code Review Summary" ํค๋๊ฐ ์ค๋ณต๋ฉ๋๋ค. ์ด์ ์ฝ๋์์ Summary๋ฅผ ์ด๋ฏธ ์์ฑํ์ผ๋ฏ๋ก, ๋ง์ง๋ง ์ก์
์์๋ ๊ฐ๋จํ ์๋ฃ ๋ฉ์์ง๋ง ๋จ๊ธฐ๋ ๊ฒ์ด ์ข์ต๋๋ค.
**3. ์ฑ๋ฅ ๊ฐ์ ํฌ์ธํธ**
* **API ํธ์ถ ์ต์ ํ:** Gemini API ํธ์ถ ํ์๋ฅผ ์ค์ด๋ ๊ฒ์ด ์ค์ํฉ๋๋ค. ๊ฐ๋ฅํ๋ค๋ฉด ์ฌ๋ฌ ํ์ผ์ ๋ํ ๋ฆฌ๋ทฐ๋ฅผ ํ ๋ฒ์ API ํธ์ถ๋ก ์ฒ๋ฆฌํ๋ ๊ฒ์ ๊ณ ๋ คํ์ญ์์ค. (Gemini API์ ์ต๋ ์
๋ ฅ ํฌ๊ธฐ ์ ํ์ ๊ณ ๋ คํด์ผ ํฉ๋๋ค.)
* **๋ถํ์ํ ๋ฐ์ดํฐ ์ฒ๋ฆฌ:** PR ์ ๋ณด ์กฐํ(`github.rest.pulls.get`)๋ ๋ชจ๋ ํ์ผ์ ๋ํด ๋์ผํ๊ฒ ์ํ๋ฉ๋๋ค. PR ์ ๋ณด๋ฅผ ํ ๋ฒ๋ง ์กฐํํ๊ณ , ํ์ผ ์ฒ๋ฆฌ ํจ์์ ์ ๋ฌํ๋ ๊ฒ์ด ํจ์จ์ ์
๋๋ค.
* **`parse-diff` ๊ฒฐ๊ณผ ์บ์ฑ:** `parse-diff`๋ ๋น๊ต์ ๋ฌด๊ฑฐ์ด ์์
์ผ ์ ์์ต๋๋ค. `file.patch`๊ฐ ๋์ผํ ๊ฒฝ์ฐ `parse-diff` ๊ฒฐ๊ณผ๋ฅผ ์บ์ฑํ์ฌ ์ฌ์ฌ์ฉํ๋ ๊ฒ์ ๊ณ ๋ คํด๋ณผ ์ ์์ต๋๋ค. (๋จ, ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ๊ณ ๋ คํด์ผ ํฉ๋๋ค.)
**4. ๋ณด์ ๊ด๋ จ ์ด์**
* **API ํค ๋ณด์:** `GEMINI_API_KEY`๋ GitHub Actions secrets์ ์์ ํ๊ฒ ์ ์ฅ๋์ด ์์ผ๋ฏ๋ก ์์ ํฉ๋๋ค. ํ์ง๋ง, ์ฝ๋ ๋ด์์ API ํค๊ฐ ๋
ธ์ถ๋์ง ์๋๋ก ์ฃผ์ํด์ผ ํฉ๋๋ค.
* **์
๋ ฅ๊ฐ ๊ฒ์ฆ:** ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ๋ ๋ฐ์ดํฐ (์: ํ์ผ ์ด๋ฆ, ์ฝ๋ ๋ด์ฉ)์ ๋ํ ๊ฒ์ฆ์ ์ํํ์ฌ injection ๊ณต๊ฒฉ์ ๋ฐฉ์งํด์ผ ํฉ๋๋ค. ํ์ผ ์ด๋ฆ sanitize๋ ์ข์ ์์์ด์ง๋ง, ๋ค๋ฅธ ์
๋ ฅ๊ฐ์ ๋ํด์๋ ๊ฒ์ฆ์ ์ํํด์ผ ํฉ๋๋ค.
* **์๋ํํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ:** `@google/generative-ai`, `parse-diff` ๋ฑ ์๋ํํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ณด์ ์ทจ์ฝ์ ์ ์ฃผ๊ธฐ์ ์ผ๋ก ํ์ธํ๊ณ ์
๋ฐ์ดํธํด์ผ ํฉ๋๋ค. Dependabot ๋ฑ์ ์ฌ์ฉํ์ฌ ์์กด์ฑ ๊ด๋ฆฌ๋ฅผ ์๋ํํ๋ ๊ฒ์ ๊ณ ๋ คํ์ญ์์ค.
**5. ๊ฐ์ ์ ์ํ ๊ตฌ์ฒด์ ์ธ ์ ์**
1. **๋ฐฑ์คํ(Backoff) ์ ๋ต ์ ์ฉ:** `withRetry` ํจ์์์ `RETRY_DELAY`๋ฅผ ๊ณ ์ ๊ฐ ๋์ ์ง์ ๋ฐฑ์คํ(exponential backoff) ์ ๋ต์ ์ฌ์ฉํ์ฌ rate limit ์ํฉ์ ๋์ฑ ํจ๊ณผ์ ์ผ๋ก ๋์ํฉ๋๋ค.
```javascript
async function withRetry(fn, retries = process.env.MAX_RETRIES) {
for (let i = 0; i < retries; i++) {
try {
return await fn();
} catch (error) {
if (error.message.includes('rate limit') && i < retries - 1) {
const delay = process.env.RETRY_DELAY * Math.pow(2, i); // ์ง์ ๋ฐฑ์คํ
console.log(`Rate limit hit, waiting ${delay}ms before retry ${i + 1}`);
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
throw error;
}
}
}
```
2. **PR ์ ๋ณด ์บ์ฑ:** PR ์ ๋ณด๋ฅผ ํ ๋ฒ๋ง ์กฐํํ๊ณ , `processFile` ํจ์์ ์ ๋ฌํฉ๋๋ค.
```javascript
// PR ์ ๋ณด ์กฐํ
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
// ํ์ผ ์ฒ๋ฆฌ (3๊ฐ์ฉ ๋์ ์ฒ๋ฆฌ)
for (let i = 0; i < files.length; i += 3) {
const batch = files.slice(i, i + 3);
await Promise.all(batch.map(file => processFile(file, pr)));
}
// processFile ํจ์ ์์
async function processFile(file, pr) { ... }
```
3. **Rate Limit ์ ๋ณด ๋ก๊น
:** rate limit์ ๊ฑธ๋ ธ์ ๋, ๋จ์์๋ limit๊ณผ reset ์๊ฐ ์ ๋ณด๋ฅผ ๋ก๊ทธ์ ์ถ๋ ฅํ์ฌ ๋๋ฒ๊น
์ ํ์ฉํฉ๋๋ค. (Gemini API์์ ํด๋น ์ ๋ณด๋ฅผ ์ ๊ณตํ๋์ง ํ์ธํด์ผ ํฉ๋๋ค.)
4. **์์ธ ์๋ฌ ๋ก๊น
:** ์๋ฌ ๋ฐ์ ์ ์คํ ํธ๋ ์ด์ค ์ ์ฒด๋ฅผ ๋ก๊น
ํ์ฌ ๋ฌธ์ ํด๊ฒฐ์ ๋์์ ์ค๋๋ค.
```javascript
console.error(`Error reviewing ${filename}:`, error, error.stack);
```
5. **๋ฆฌ๋ทฐ ์์ฝ ๊ฐ์ :** summary ์ฝ๋ฉํธ์ ์๋ฌ ๋ฉ์์ง๊ฐ ์๋ ค์ ํ์๋๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด, ์๋ฌ ๋ฉ์์ง๋ฅผ ์์ฝํ๊ฑฐ๋, ์๋ฌ ๋ฉ์์ง ์ ์ฒด๋ฅผ ํ์ํ ์ ์๋ ๋ณ๋์ ํ์ด์ง๋ฅผ ๋งํฌํ๋ ๋ฐฉ์์ ๊ณ ๋ คํด๋ณผ ์ ์์ต๋๋ค.
6. **๋ง์ง๋ง ์ฝ๋ฉํธ ์ก์
์์ :** ๋ง์ง๋ง ์ฝ๋ฉํธ ์ก์
์์ ํค๋๋ฅผ ์ ๊ฑฐํ๊ณ , ๊ฐ๋จํ ์๋ฃ ๋ฉ์์ง๋ง ๋จ๊น๋๋ค.
```yaml
- name: Create or Update Comment
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
์ฝ๋ ๋ฆฌ๋ทฐ๊ฐ ์๋ฃ๋์์ต๋๋ค. ๊ฐ ํ์ผ์ ๋ณ๊ฒฝ์ฌํญ์ ๋ํ ํผ๋๋ฐฑ์ ํ์ธํด์ฃผ์ธ์.
edit-mode: replace
```
์ด๋ฌํ ๊ฐ์ ์ฌํญ๋ค์ ์ ์ฉํ๋ฉด ์ฝ๋ ๋ฆฌ๋ทฐ ์ํฌํ๋ก์ฐ์ ํ์ง, ์ฑ๋ฅ, ์์ ์ฑ์ ๋์ฑ ํฅ์์ํฌ ์ ์์ต๋๋ค. |
@@ -0,0 +1,88 @@
+import styled from "styled-components";
+
+export const ReviewSectionName = styled.h2`
+
+`;
+
+export const ReviewFormArea = styled.div`
+ position: absolute;
+ width: 918px;
+ height: 240px;
+ padding: 14px 23px;
+ background-color: #00462A;
+ border-radius: 20px;
+ flex-direction: column;
+ gap: 15px;
+`;
+
+export const Conditions = styled.div`
+ display: flex;
+ flex-direction: row;
+ align-items: center;
+ gap: 15px;
+`;
+
+export const IfReward = styled.label`
+ display: flex;
+ align-items: center;
+ gap: 4px;
+ flex-direction: row-reverse;
+ color: #FFFFFF;
+ font-size: 14px;
+`;
+
+export const Checkbox = styled.input`
+ width: 16px;
+ height: 16px;
+ cursor: pointer;
+ border-radius: 20px;
+`;
+
+export const Number = styled.input`
+ margin-left: 20px;
+ border-radius: 20px;
+ padding: 8px;
+ border: none;
+ width: 100px;
+`;
+
+export const DropDown = styled.select`
+ border-radius: 20px;
+ padding: 8px;
+ border: none;
+ width: 100px;
+ cursor: pointer;
+ appearance: none;
+
+ option{
+ font-size: 14px;
+ color: #333333;
+ }
+`;
+
+export const ReviewContent = styled.div`
+ margin-top: 15px;
+ width: 884px;
+ height: 99px;
+
+ textarea {
+ width: 100%;
+ height: 100%;
+ resize: none;
+ overflow: auto;
+ border-radius: 18px;
+ padding: 12px 17px;
+ border: none;
+ font-size: 14px;
+ }
+`;
+
+export const Button = styled.button`
+ margin-top: 40px;
+ outline: none;
+ border: none;
+ width: 923px;
+ height: 50px;
+ background-color: #F0F7F4;
+ border-radius: 20px;
+`;
\ No newline at end of file | Unknown | styled-component ๊น๋ํ๊ฒ ์ ์ฐ์ ๊ฒ ๊ฐ์์! ์๊ณ ๋ง์ผ์
จ์ต๋๋ค~ |
@@ -0,0 +1,150 @@
+//
+// MyDetailReviewViewController.swift
+// Pyonsnal-Color
+//
+// Created by ์กฐ์์ on 5/19/24.
+//
+
+import ModernRIBs
+import UIKit
+
+protocol MyDetailReviewPresentableListener: AnyObject {
+ func didTapBackButton()
+}
+
+final class MyDetailReviewViewController: UIViewController,
+ MyDetailReviewPresentable,
+ MyDetailReviewViewControllable {
+ // MARK: Interfaces
+ private typealias DataSource = UICollectionViewDiffableDataSource<Section, Item>
+ private typealias Snapshot = NSDiffableDataSourceSnapshot<Section, Item>
+
+ private enum Section {
+ case main
+ }
+
+ private enum Item: Hashable {
+ case productDetail(product: ProductDetailEntity)
+ case review(review: ReviewEntity)
+ case landing
+ }
+
+ weak var listener: MyDetailReviewPresentableListener?
+
+ // MARK: Private Properties
+ private let viewHolder: ViewHolder = .init()
+ private var dataSource: DataSource?
+ private var productDetail: ProductDetailEntity?
+ private var review: ReviewEntity?
+
+ // MARK: View Life Cycles
+ override func viewDidLoad() {
+ super.viewDidLoad()
+ viewHolder.place(in: view)
+ viewHolder.configureConstraints(for: view)
+ self.configureUI()
+ self.bindActions()
+ self.configureCollectionView()
+ self.configureDataSource()
+ self.applySnapshot(with: productDetail, review: review)
+ }
+
+ func update(with productDetail: ProductDetailEntity, review: ReviewEntity) {
+ self.productDetail = productDetail
+ self.review = review
+ }
+
+ // MARK: Private Methods
+ private func configureUI() {
+ self.view.backgroundColor = .white
+ }
+
+ private func bindActions() {
+ self.viewHolder.backNavigationView.delegate = self
+ }
+
+ private func configureCollectionView() {
+ self.registerCollectionViewCells()
+ self.viewHolder.collectionView.delegate = self
+ }
+
+ private func registerCollectionViewCells() {
+ self.viewHolder.collectionView.register(ProductDetailReviewCell.self)
+ self.viewHolder.collectionView.register(
+ UICollectionViewCell.self,
+ forCellWithReuseIdentifier: MyReviewContentView.identifier
+ )
+ self.viewHolder.collectionView.register(ActionButtonCell.self)
+ }
+
+ private func configureDataSource() {
+ dataSource = DataSource(collectionView: self.viewHolder.collectionView) { collectionView, indexPath, itemIdentifier in
+ switch itemIdentifier {
+ case .productDetail(let product):
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyReviewContentView.identifier, for: indexPath)
+ cell.contentConfiguration = MyReviewContentConfiguration(
+ mode: .tastes,
+ storeImageIcon: product.storeType,
+ imageUrl: product.imageURL,
+ title: "ํ๋ง๊ธ์ค) ๋งค์ฝคํ๋ง(๋) ํ๋ง๊ธ์ค) ๋งค์ฝคํ๋ง(๋)ํ๋ง๊ธ์ค) ๋งค์ฝคํ๋ง(๋)ํ๋ง๊ธ์ค) ๋งค์ฝคํ๋ง",
+ tastesTag: ["์นดํ์ธ ๋ฌ๋ฒ", "ํฌ์ฐฝ", "์บ๋ฆญํฐ์ปฌ๋ ํฐ", "์บ๋ฆญํฐ์ปฌ๋ ํฐ"]
+ )
+ return cell
+ case .review(let review):
+ let cell: ProductDetailReviewCell = collectionView.dequeueReusableCell(for: indexPath)
+ cell.payload = .init(hasEvaluateView: false, review: review)
+ return cell
+ case .landing:
+ let cell: ActionButtonCell = collectionView.dequeueReusableCell(for: indexPath)
+ cell.actionButton.setText(with: ActionButtonCell.Constants.Text.showProduct)
+ return cell
+ }
+ }
+ }
+
+ private func applySnapshot(with productDetail: ProductDetailEntity?, review: ReviewEntity?) {
+ guard let productDetail, let review else { return }
+ var snapshot = Snapshot()
+ snapshot.appendSections([.main])
+ snapshot.appendItems([.productDetail(product: productDetail)])
+ snapshot.appendItems([.review(review: review)])
+ snapshot.appendItems([.landing])
+ dataSource?.apply(snapshot, animatingDifferences: false)
+ }
+}
+
+// MARK: - UICollectionViewDelegateFlowLayout
+extension MyDetailReviewViewController: UICollectionViewDelegateFlowLayout {
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
+ guard let item = dataSource?.itemIdentifier(for: indexPath) else { return CGSize.zero }
+ let screenWidth = collectionView.bounds.width
+ switch item {
+ case .productDetail:
+ return .init(width: screenWidth, height: 136)
+ case .review(let review):
+ let estimateHeight: CGFloat = 1000
+ let cell = ProductDetailReviewCell()
+ cell.frame = .init(
+ origin: .zero,
+ size: .init(width: screenWidth, height: estimateHeight)
+ )
+ cell.payload = .init(hasEvaluateView: false, review: review)
+ cell.layoutIfNeeded()
+ let estimateSize = cell.systemLayoutSizeFitting(
+ .init(width: screenWidth, height: UIView.layoutFittingCompressedSize.height),
+ withHorizontalFittingPriority: .required,
+ verticalFittingPriority: .defaultLow
+ )
+ return estimateSize
+ case .landing:
+ return .init(width: screenWidth, height: 40)
+ }
+ }
+}
+
+// MARK: - BackNavigationViewDelegate
+extension MyDetailReviewViewController: BackNavigationViewDelegate {
+ func didTapBackButton() {
+ listener?.didTapBackButton()
+ }
+} | Swift | [C]
์๋์ `applySnapshot(with: , review:)` ํจ์์ ๋งค๊ฐ๋ณ์๋ฅผ ์ธ๋ถ์์ ์ ๋ฌ๋ฐ๋๋ก ํ๊ณ ๋ทฐ์ปจ ๋ด๋ถ์์๋ `productDetail, review` ๊ฐ์ ์ ๋ณด๋ฅผ ๋ชจ๋ฅด๊ฒ ํ๋ ๋ฐฉ๋ฒ์ ์ด๋จ๊น์?
(๋ทฐ์ปจ์ด ๋ฉ์ฒญํ๋๋ก..!) |
@@ -0,0 +1,96 @@
+//
+// MyReviewViewController.swift
+// Pyonsnal-Color
+//
+// Created by ์กฐ์์ on 5/19/24.
+//
+
+import ModernRIBs
+import UIKit
+
+protocol MyReviewPresentableListener: AnyObject {
+ func didTapMyReviewBackButton()
+ func didTapMyDetailReview(with productDetail: ProductDetailEntity, reviewId: String)
+}
+
+final class MyReviewViewController: UIViewController,
+ MyReviewPresentable,
+ MyReviewViewControllable {
+
+ weak var listener: MyReviewPresentableListener?
+ var viewHolder: MyReviewViewController.ViewHolder = .init()
+
+ override func viewDidLoad() {
+ super.viewDidLoad()
+ viewHolder.place(in: view)
+ viewHolder.configureConstraints(for: view)
+ self.configureUI()
+ self.bindActions()
+ self.configureTableView()
+ }
+
+ func update(reviews: [ReviewEntity]) {
+ self.updateNavigationTitle(reviewCount: reviews.count)
+ }
+
+ // MARK: - Private Mehods
+ private func configureUI() {
+ self.view.backgroundColor = .white
+ }
+
+ private func bindActions() {
+ self.viewHolder.backNavigationView.delegate = self
+ }
+
+ private func updateNavigationTitle(reviewCount: Int) {
+ let updatedTitle = Text.navigationTitleView + "(\(reviewCount))"
+ self.viewHolder.backNavigationView.setText(with: updatedTitle)
+ }
+
+ private func configureTableView() {
+ self.viewHolder.tableView.register(
+ UITableViewCell.self,
+ forCellReuseIdentifier: MyReviewContentView.identifier
+ )
+ self.viewHolder.tableView.delegate = self
+ self.viewHolder.tableView.dataSource = self
+ }
+}
+
+// MARK: - UITableViewDelegate
+extension MyReviewViewController: UITableViewDelegate {
+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+ listener?.didTapMyDetailReview(with: ProductDetailEntity(id: "", storeType: .cu, imageURL: URL(string: "www.naver.com")!, name: "", price: "", eventType: nil, productType: .event, updatedTime: "", description: nil, isNew: nil, viewCount: 0, category: nil, isFavorite: nil, originPrice: nil, giftImageURL: nil, giftTitle: nil, giftPrice: nil, isEventExpired: nil, reviews: [], avgScore: nil), reviewId: "1") // TODO: interactor๋ก๋ถํฐ ๋ฐ์์จ ๊ฐ์ผ๋ก ๋ณ๊ฒฝ
+ }
+
+ func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
+ return UITableView.automaticDimension
+ }
+}
+
+// MARK: - UITableViewDataSource
+extension MyReviewViewController: UITableViewDataSource {
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
+ return 10 // TODO: interactor๋ก๋ถํฐ ๋ฐ์์จ ๊ฐ์ผ๋ก ๋ณ๊ฒฝ
+ }
+
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
+ let cell = tableView.dequeueReusableCell(withIdentifier: MyReviewContentView.identifier, for: indexPath)
+ cell.selectionStyle = .none // TODO: interactor๋ก๋ถํฐ ๋ฐ์์จ ๊ฐ์ผ๋ก ๋ณ๊ฒฝ
+ cell.contentConfiguration = MyReviewContentConfiguration(
+ mode: .date,
+ storeImageIcon: .sevenEleven,
+ imageUrl: URL(string: "www.naver.com")!,
+ title: "ํ
์คํธ",
+ date: "2024.05.19"
+ )
+ return cell
+ }
+}
+
+// MARK: - BackNavigationViewDelegate
+extension MyReviewViewController: BackNavigationViewDelegate {
+ func didTapBackButton() {
+ listener?.didTapMyReviewBackButton()
+ }
+} | Swift | `automaticDimension` ์ง์ ํด์ค ์ด์ ๊ฐ ์
๋ง๋ค ๋ค๋ฅธ ๋์ด๊ฐ ์ฃผ๊ธฐ ์ํด์ ์ธ๊ฐ์? |
@@ -0,0 +1,41 @@
+//
+// MyReviewContentConfiguration.swift
+// Pyonsnal-Color
+//
+// Created by ์กฐ์์ on 5/19/24.
+//
+
+import UIKit
+
+struct MyReviewContentConfiguration: UIContentConfiguration {
+ var storeImageIcon: ConvenienceStore
+ var imageUrl: URL
+ var title: String
+ var date: String?
+ var mode: ProductInfoStackView.Mode
+ var tastesTag: [String]?
+
+ init(
+ mode: ProductInfoStackView.Mode,
+ storeImageIcon: ConvenienceStore,
+ imageUrl: URL,
+ title: String,
+ date: String? = nil,
+ tastesTag: [String]? = nil
+ ) {
+ self.mode = mode
+ self.storeImageIcon = storeImageIcon
+ self.imageUrl = imageUrl
+ self.title = title
+ self.date = date
+ self.tastesTag = tastesTag
+ }
+
+ func makeContentView() -> any UIView & UIContentView {
+ return MyReviewContentView(configuration: self, mode: mode)
+ }
+
+ func updated(for state: any UIConfigurationState) -> MyReviewContentConfiguration {
+ return self
+ }
+} | Swift | `UIContentConfiguration` ํ์ฉํ๋ ๋ฐฉ๋ฒ ๋๋ถ์ ๋ฐฐ์ ์ต๋๋ค ๐ |
@@ -0,0 +1,58 @@
+//
+// MyReviewContentView.swift
+// Pyonsnal-Color
+//
+// Created by ์กฐ์์ on 5/19/24.
+//
+
+import UIKit
+import SnapKit
+
+class MyReviewContentView: UIView, UIContentView {
+ static let identifier = "MyReviewContentView"
+
+ var configuration: any UIContentConfiguration
+ private let storeImageViewHeight: CGFloat = 20
+
+ private var productInfoStackView: ProductInfoStackView
+
+ init(configuration: any UIContentConfiguration, mode: ProductInfoStackView.Mode) {
+ self.configuration = configuration
+ self.productInfoStackView = ProductInfoStackView(mode: mode)
+ super.init(frame: .zero)
+ configureView()
+ configureUI()
+ }
+
+ required init?(coder: NSCoder) {
+ fatalError("init(coder:) has not been implemented")
+ }
+
+ private func configureView() {
+ self.addSubview(productInfoStackView)
+ productInfoStackView.snp.makeConstraints {
+ $0.edges.equalToSuperview()
+ }
+ }
+
+ private func configureUI() {
+ guard let configuration = configuration as? MyReviewContentConfiguration else { return }
+ self.productInfoStackView.productImageView.setImage(with: configuration.imageUrl)
+ self.productInfoStackView.productNameLabel.text = configuration.title
+ self.productInfoStackView.storeImageView.setImage(configuration.storeImageIcon.storeIcon)
+ self.productInfoStackView.storeImageView.snp.makeConstraints { make in
+ guard let storeIcon = configuration.storeImageIcon.storeIcon.image else { return }
+ let ratio = storeIcon.size.width / storeIcon.size.height
+ let newWidth = self.storeImageViewHeight * ratio
+ make.width.equalTo(newWidth)
+ }
+
+ if let date = configuration.date {
+ self.productInfoStackView.dateLabel.text = configuration.date
+ }
+
+ if let tastesTag = configuration.tastesTag {
+ self.productInfoStackView.setTastes(tastesTag: tastesTag)
+ }
+ }
+} | Swift | [C]
์ค~ ์ฒ์ ๋ณด๋ ์น๊ตฌ๋ค์ ๐ |
@@ -0,0 +1,33 @@
+import letters from "../mockData";
+
+const sleep = n => new Promise(resolve => setTimeout(resolve, n));
+
+export const getLetters = async () => {
+ await sleep(500);
+ return letters;
+};
+
+export const getLetterById = async id => {
+ await sleep(100);
+ return letters.find(letter => letter.id === id);
+};
+
+export const updateLetter = async (id, payload) => {
+ await sleep(100);
+ const { title, artist, imageUrl } = payload;
+ const songStory = payload.songStory;
+
+ const foundLetter = letters.find(letter => letter.id === id);
+
+ const updatedLetter = {
+ ...foundLetter,
+ song: { title, artist, imageUrl },
+ songStory
+ };
+
+ const index = letters.indexOf(foundLetter);
+ letters.splice(index, 1);
+ letters.push(updatedLetter);
+
+ return updatedLetter;
+}; | JavaScript | ๋ฐฐ์ด์์ ์ํ๋ ์กฐ๊ฑด๊น์ง๋ง ์ฐพ๋ find method๋ฅผ ์ฌ์ฉํ ๊ฑด ์ข์๋ณด์
๋๋ค ! |
@@ -0,0 +1,24 @@
+import React from "react";
+import styled from "styled-components";
+
+const Footer = () => {
+ return (
+ <FooterBlock>
+ <p className="copyright">© Museop Kim</p>
+ </FooterBlock>
+ );
+};
+
+const FooterBlock = styled.div`
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ width: 100%;
+ height: 8rem;
+ margin-top: 6rem;
+ background-color: #f5f5f7;
+ font-weight: 500;
+ box-shadow: 0px -10px 35px 5px #f1f1f1;
+`;
+
+export default Footer; | Unknown | style components๋ฅผ ์ฌ์ฉํ์๋๊น
`box-shadow: 0px -10px 35px 5px #f1f1f1;`
๋ถ๋ถ์ ์์ ๊ฐ์ ๊ฒฝ์ฐ๋ styled-components ThemeProvider๋ฅผ ์ฌ์ฉํด ๋ณด๋๊ฑด ์ด๋จ๊น์.
๋ชจ๋ ์ปดํฌ๋ํธ๊ฐ ์ ๋ฌ๋ฐ์ ์์์ ๋์ผํ๊ฒ ์ฌ์ฉํ๋ฉด ๋คํฌ๋ชจ๋์ ๊ฐ์ ๊ธฐ๋ฅ์ ๊ตฌํํ ๋๋ ์์ฝ๊ฒ ์คํ์ผ ๋ณ๋์ด ๊ฐ๋ฅํฉ๋๋ค.
๋๋ฌด ๋ง์ด ์งํ๋ ํ๋ก์ ํธ๋ ์คํ์ผ ์์คํ
์ ๊ตฌ์ถํ๊ธฐ ์ฝ์ง ์์ผ๋๊น ์ข ์์ ๋จ์์ผ ๋ ์๋ํด ๋ณด๋๊ฒ๋ ์ข์๊ฑฐ ๊ฐ์์ ๐ |
@@ -0,0 +1,47 @@
+import React from "react";
+import styled from "styled-components";
+import { Link } from "react-router-dom";
+
+const Navigation = () => {
+ return (
+ <NavigationBlock>
+ <Link to="/">์ ์ฒญ๊ณก</Link>
+ <Link to="/ranking">์ ์ฒญ๊ณก ์์</Link>
+ <Link to="/contents">์ ์ฒญ๊ณก ์ ํ๋ธ ์์</Link>
+ <Link to="/musicsheets">๋ฆฌ์ผํผ์๋
ธ ์
๋ณด์ง</Link>
+ </NavigationBlock>
+ );
+};
+
+const NavigationBlock = styled.div`
+ height: 100%;
+ display: flex;
+ align-items: center;
+
+ & > * {
+ /* padding: 25px 5px 10px 5px; */
+ box-sizing: border-box;
+ display: flex;
+ align-items: center;
+ margin: 0px 30px;
+ padding-top: 10px;
+ font-size: 1.2rem;
+ font-weight: 600;
+ text-decoration: none;
+ align-self: center;
+ color: inherit;
+ opacity: 0.5;
+ height: 100%;
+ /* border-bottom: 1px solid rgba(0, 0, 0, 0); */
+ border-bottom: 3px solid rgba(250, 162, 193, 0);
+ transition: 0.3s;
+
+ :hover {
+ opacity: 1;
+ /* color: #faa2c1; */
+ border-bottom: 3px solid rgba(250, 162, 193, 1);
+ }
+ }
+`;
+
+export default Navigation; | Unknown | ํฐํธ์ ๊ฐ์ด ๋ฐ์ํ์ ์ํฅ์ ๋ฐ์์ผ ํ๋ ๊ธฐ์ค๊ณผ ๊ทธ๋ ์ง ์์ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก px๊ณผ rem์ ์ฌ์ฉํ์๋ ๊ฑด๊ฐ์ ?
๊ทธ๋ฐ ์๋๋ผ๋ฉด ์ข์๋ณด์
๋๋ค. |
@@ -0,0 +1,147 @@
+import React from "react";
+import styled from "styled-components";
+
+const MAX_LENGTH = 100;
+
+const Letter = ({
+ id,
+ user,
+ song,
+ songStory,
+ createdDateTime,
+ onReadLetter
+}) => {
+ const { username, avatarUrl } = user;
+ const { title, artist, imageUrl } = song;
+
+ return (
+ <>
+ <LetterBlock onClick={() => onReadLetter(id)}>
+ <SongBlock>
+ <img src={imageUrl} alt="ALBUM IMAGE" className="album-image" />
+ <div className="song-about">
+ <span className="song-about__title">{title}</span>
+ <span className="song-about__artist">{artist}</span>
+ </div>
+ </SongBlock>
+ <SongStory>
+ {songStory.length > MAX_LENGTH
+ ? `${songStory.slice(0, MAX_LENGTH)} ...`
+ : songStory}
+ </SongStory>
+ <UserBlock>
+ <div className="created-time">{createdDateTime}</div>
+ <div className="user-about">
+ <img src={avatarUrl} className="user-about__avatar" />
+ <span className="user-about__name">{username}</span>
+ </div>
+ </UserBlock>
+ </LetterBlock>
+ </>
+ );
+};
+
+const LetterBlock = styled.li`
+ width: 25rem;
+ height: 23rem;
+ margin: 1.2rem;
+ margin-bottom: 5rem;
+ box-sizing: border-box;
+ display: flex;
+ flex-direction: column;
+ padding: 0.7rem;
+ border: #ffdeeb;
+ border-radius: 0.5rem;
+ cursor: pointer;
+ transition: 0.3s;
+ box-shadow: 0 13px 27px -5px rgba(50, 50, 93, 0.2),
+ 0 8px 16px -8px rgba(0, 0, 0, 0.2), 0 -6px 16px -6px rgba(0, 0, 0, 0.025);
+
+ &:hover {
+ box-shadow: 0px 25px 30px 3px rgba(0, 0, 0, 0.3);
+ }
+
+ .album-image {
+ width: 10rem;
+ height: 10rem;
+ }
+`;
+
+const SongBlock = styled.div`
+ display: flex;
+ justify-content: space-between;
+
+ .album-image {
+ position: relative;
+ top: -2.5rem;
+ box-shadow: 3px 2px 5px 1px rgba(0, 0, 0, 0.3);
+ border-radius: 5px;
+ }
+
+ .song-about {
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ margin-right: auto;
+ margin-left: 2rem;
+
+ .song-about__title {
+ overflow-x: hidden;
+ font-size: 1.4rem;
+ font-weight: 500;
+ color: #2c2c2c;
+ opacity: 0.9;
+ }
+
+ .song-about__artist {
+ margin-top: 0.5rem;
+ font-size: 1.2rem;
+ opacity: 0.8;
+ }
+ }
+`;
+
+const SongStory = styled.p`
+ width: 100%;
+ max-height: 8rem;
+ line-break: anywhere;
+ margin-top: -0.3rem;
+ font-size: 1.2rem;
+ line-height: 1.9rem;
+ opacity: 0.9;
+ padding: 0rem 0.5rem;
+`;
+
+const UserBlock = styled.div`
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ margin: auto 5px 3px 5px;
+
+ .created-time {
+ font-size: 1.1rem;
+ margin-top: 0.8rem;
+ opacity: 0.7;
+ }
+
+ .user-about {
+ display: flex;
+ align-items: center;
+ }
+
+ .user-about__avatar {
+ max-width: 2.5rem;
+ max-height: 2.5rem;
+ border-radius: 50%;
+ box-shadow: 3px 2px 10px 1px rgba(0, 0, 0, 0.3);
+ }
+
+ .user-about__name {
+ font-size: 1.1rem;
+ margin-top: 0.5rem;
+ margin-left: 0.5rem;
+ opacity: 0.8;
+ }
+`;
+
+export default Letter; | Unknown | className ์ปจ๋ฒค์
์ค "-"์ "_"๋ ์ด๋ค ๊ธฐ์ค์ด ์์๊น์ ? ๊ถ๊ธํฉ๋๋ค ใ
|
@@ -0,0 +1,29 @@
+import React from "react";
+import styled from "styled-components";
+import LetterContainer from "../../containers/Letter/LetterContainer";
+
+const LetterList = ({ letters }) => {
+ return (
+ <LetterListBlock>
+ {letters.map(letter => (
+ <LetterContainer
+ key={letter.id}
+ id={letter.id}
+ user={letter.user}
+ song={letter.song}
+ songStory={letter.songStory}
+ createdDateTime={letter.createdDateTime}
+ />
+ ))}
+ </LetterListBlock>
+ );
+};
+
+const LetterListBlock = styled.ul`
+ width: 100%;
+ display: flex;
+ flex-wrap: wrap;
+ margin-top: 4.5rem;
+`;
+
+export default LetterList; | Unknown | ๋ณดํต Container๋ ์ข ๋ ํฐ ๋จ์์์์ ์๋ฏธ์์ ์ฌ์ฉ๋๊ณค ํ๋๊ฑฐ ๊ฐ์์.
์ฌ๋ฌ ์์ ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ง๊ณ ๋ง์ ์ด๋ฒคํธ ๋ก์ง์ ๊ฐ์ง๊ณ ์๋ ์ผํฐ์ ๋๋์ผ๋ก ๋ค์ด๋ฐ์ ํ๊ณค ํ๋๋ฐ
LetterList๋ฉด LetterItem๊ณผ ๊ฐ์ด ๋จ์ผํ๊ฒ ํํํด ์ฃผ๋๊ฒ๋ ์ข์๊ฑฐ ๊ฐ์์ ! |
@@ -0,0 +1,30 @@
+import React, { useEffect, useState } from "react";
+import ModalTemplate from "../Template/Modal/ModalTemplate";
+import LetterDetailsSong from "./LetterDetailsSong";
+import LetterDetailsSongStory from "./LetterDetailsSongStory";
+import LetterModalTemplate from "../Template/LetterModal/LetterModalTemplate";
+import LetterDetailsUser from "./LetterDetailsUser";
+import LetterModalDiv from "../LetterModal/LetterModalContents/LetterModalDiv";
+import LetterModalHiddenButtonContainer from "../../containers/LetterModal/LetterModalHiddenButtonContainer";
+import LetterModalButtonContainer from "../../containers/LetterModal/LetterModalButtonContainer";
+import { useSelector } from "react-redux";
+
+const LetterDetails = ({ letter }) => {
+ const { song, songStory, createdDateTime, user } = letter;
+
+ return (
+ <ModalTemplate>
+ <LetterModalTemplate>
+ <LetterModalHiddenButtonContainer />
+ <LetterModalDiv>
+ <LetterDetailsSong song={song} />
+ <LetterDetailsSongStory songStory={songStory} />
+ <LetterDetailsUser user={user} createdDateTime={createdDateTime} />
+ <LetterModalButtonContainer />
+ </LetterModalDiv>
+ </LetterModalTemplate>
+ </ModalTemplate>
+ );
+};
+
+export default LetterDetails; | Unknown | `const { song, songStory, createdDateTime, user } = letter;`
๋๋ฌด ๋ง์ ์์ ๊ฐ์ฒด Props๊ฐ ์๋๋ผ๋ฉด ์ด ๋ถ๋ถ ๊ฐ์ ๊ฒฝ์ฐ๋ ๊ฐ๊ฐ ์ด๋ฆ์ ๊ฐ์ง Props๋ก ์ ๋ฌํด ์ฃผ๋๊ฒ๋ ์ข์๊ฑฐ ๊ฐ๋ค๋ ์๊ฐ์ด ๋ญ๋๋ค! ๊ทธ๋ฌ๋ฉด ๊ฐ๋จํ๊ฒ ์ปดํฌ๋ํธ์ ์ง์ ๋ค์ด์ ๋ณด์ง ์์๋ ์ ๋ฌ๋๋ Props ๋ง์ผ๋ก ์ญํ ์ ์ ์ถํด ๋ณผ ์ ์์๊ฑฐ ๊ฐ์์. |
@@ -0,0 +1,45 @@
+import React, { useEffect } from "react";
+import { MdMoreHoriz } from "react-icons/md";
+import styled, { css } from "styled-components";
+import LetterDetailsHiddenMenuButton from "./LetterDetailsHiddenMenuButton";
+
+const LetterDetailsHiddenMenu = ({
+ isMouseEnter,
+ isMenuOpen,
+ onToggle,
+ changeToEdit
+}) => {
+ return (
+ <ButtonBlock isMouseEnter={isMouseEnter} onClick={onToggle}>
+ <HiddenMenu />
+ {isMenuOpen && (
+ <LetterDetailsHiddenMenuButton changeToEdit={changeToEdit} />
+ )}
+ </ButtonBlock>
+ );
+};
+
+const ButtonBlock = styled.div`
+ ${props =>
+ props.isMouseEnter
+ ? css`
+ visibility: visible;
+ opacity: 1;
+ `
+ : css`
+ visibility: hidden;
+ opacity: 0;
+ `}
+ transition: 0.7s;
+`;
+
+const HiddenMenu = styled(MdMoreHoriz)`
+ position: absolute;
+ bottom: 93%;
+ right: 3%;
+ cursor: pointer;
+ font-size: 2.1rem;
+ color: gray;
+`;
+
+export default LetterDetailsHiddenMenu; | Unknown | visibility: hidden; , display : none ์ ์ฐจ์ด๋ฅผ ์์๋์ ? ใ
ใ
๋ชจ๋ฅด๊ณ ๊ณ์ ๋ค๋ฉด ํ๋ฒ ์์๋ณด๋๊ฒ๋ ์ข์ต๋๋ค ! ๋๋จํ๊ฑด ์๋๊ณ ๊ทธ๋ฅ DOM ํธ๋ฆฌ์ ๊ด๋ จ๋ ๋ด์ฉ์ด์์. |
@@ -0,0 +1,28 @@
+import React from "react";
+import LetterModalSong from "../LetterModal/LetterModalSong/LetterModalSong";
+import SongAbout from "../LetterModal/LetterModalSong/SongAbout";
+import SongArticle from "../LetterModal/LetterModalSong/SongArticle";
+import SongArticleItem from "../LetterModal/LetterModalSong/SongArticleItem";
+import SongArticleName from "../LetterModal/LetterModalSong/SongArticleName";
+import SongImage from "../LetterModal/LetterModalSong/SongImage";
+
+const LetterDetailsSong = ({ song }) => {
+ const { title, artist, imageUrl } = song;
+ return (
+ <LetterModalSong>
+ <SongImage imageUrl={imageUrl} />
+ <SongAbout>
+ <SongArticle>
+ <SongArticleName articleName={"TITLE"} />
+ <SongArticleItem articleName={"TITLE"} item={title} />
+ </SongArticle>
+ <SongArticle>
+ <SongArticleName articleName={"ARTIST"} />
+ <SongArticleItem articleName={"ARTIST"} item={artist} />
+ </SongArticle>
+ </SongAbout>
+ </LetterModalSong>
+ );
+};
+
+export default LetterDetailsSong; | Unknown | ์ด ๋ถ๋ถ์ ๊ฒฝ์ฐ ๋ฐ๋ณต๋๋ ์ปดํฌ๋ํธ๋ก ๋ณด์ด๋๋ฐ ์ด๊ฑธ ํ๋๋ก ๋ฌถ์ด์ ๊ด๋ฆฌํด ๋ณด๋๊ฑด ์ด๋จ๊น์ ?
<SongArticle>
<SongArticleName articleName={"TITLE"} />
<SongArticleItem articleName={"TITLE"} item={title} />
</SongArticle> |
@@ -0,0 +1,48 @@
+import React from "react";
+import styled from "styled-components";
+import { BiSearch } from "react-icons/bi";
+import useModal from "../../hooks/useModal";
+import SongSearchModal from "./SongSearchModal/SongSearchModal";
+
+const LetterEditorSearchButton = () => {
+ const [isOpened, onOpenModal, onCloseModal] = useModal();
+
+ return (
+ <>
+ <StyledButton onClick={onOpenModal}>
+ <SearchIcon />
+ ์ ์ฒญ๊ณก ๊ฒ์
+ </StyledButton>
+ <SongSearchModal isOpened={isOpened} onCloseModal={onCloseModal} />
+ </>
+ );
+};
+
+const StyledButton = styled.div`
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ position: absolute;
+ width: 11rem;
+ top: 14%;
+ right: 15%;
+
+ padding: 0.35rem 0rem;
+ border-style: none;
+ border-radius: 0.5rem;
+ font-size: 1.2rem;
+ background-color: #f06595;
+ box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, 0.15);
+ color: #fff;
+ font-weight: 500;
+ z-index: 1;
+ cursor: pointer;
+`;
+
+const SearchIcon = styled(BiSearch)`
+ font-size: 1.35rem;
+ margin-right: 0.35rem;
+ opacity: 0.9;
+`;
+
+export default LetterEditorSearchButton; | Unknown | modal๊ณผ ๊ด๋ จ๋ ๋์์ custom hook์ผ๋ก ์ฌ์ฉํ ๋ถ๋ถ ์ข์ ๋ณด์
๋๋ค ! |
@@ -0,0 +1,108 @@
+import React from "react";
+import styled from "styled-components";
+import { BiSearch } from "react-icons/bi";
+import { useDispatch } from "react-redux";
+import { searchSong } from "../../../modules/song";
+
+const SearchForm = ({ onCloseModal }) => {
+ const dispatch = useDispatch();
+
+ const onSearchSong = event => {
+ event.preventDefault();
+ dispatch(searchSong());
+ };
+
+ return (
+ <Form>
+ <SearchInputWrap>
+ <SearchLabel>์ํฐ์คํธ</SearchLabel>
+ <SearchInput type="text" name="artist" />
+ </SearchInputWrap>
+ <SearchInputWrap>
+ <SearchLabel>์ ๋ชฉ</SearchLabel>
+ <SearchInput type="text" name="title" />
+ </SearchInputWrap>
+ <SearchButton type="submit" onClick={onSearchSong}>
+ <SearchIcon />
+ </SearchButton>
+ </Form>
+ );
+};
+
+const Form = styled.form`
+ box-sizing: border-box;
+ width: 95%;
+ display: flex;
+ justify-content: center;
+ margin: 0rem auto 3.5rem auto;
+`;
+
+const SearchLabel = styled.span`
+ display: flex;
+ align-items: center;
+ position: absolute;
+ top: 0rem;
+ left: 0rem;
+ height: 2.37rem;
+ font-size: 1rem;
+ background-color: #727272;
+ padding: 0rem 0.7rem;
+ border-radius: 0.3rem 0rem 0rem 0.3rem;
+ color: white;
+ font-weight: 600;
+ box-shadow: 1px 0px 3px 1px rgba(0, 0, 0, 0.1);
+`;
+
+const SearchInput = styled.input`
+ border: none;
+ outline: none;
+ color: #868e96;
+ width: 100%;
+ margin: 0rem 0.5rem 0rem 5.5rem;
+ font-size: 1.2rem;
+ background-color: #fbfbfd;
+`;
+
+const SearchInputWrap = styled.div`
+ display: flex;
+ align-items: center;
+ position: relative;
+ border: 1px solid #e9ecef;
+ border-radius: 1rem 0rem 0rem 1rem;
+ padding: 0.1rem 0rem;
+ background: #fbfbfd;
+ height: 2.1rem;
+ flex: 11;
+
+ &:nth-child(2) {
+ & > ${SearchLabel} {
+ border-radius: 0.2rem;
+ margin-left: -0.25rem;
+ }
+
+ & > ${SearchInput} {
+ margin-left: 3.8rem;
+ }
+ }
+`;
+
+const SearchButton = styled.button`
+ display: flex;
+ position: relative;
+ justify-content: center;
+ align-items: center;
+ border: none;
+ outline: none;
+ cursor: pointer;
+ background-color: #e1999c;
+ border-radius: 0rem 0.5rem 0.5rem 0rem;
+ flex: 1;
+ box-shadow: 1px 0px 3px 1px rgba(0, 0, 0, 0.1);
+`;
+
+const SearchIcon = styled(BiSearch)`
+ color: #fff;
+ font-size: 1.4rem;
+`;
+
+export default SearchForm; | Unknown | Form์ด ์ ์ ๋ ๋ณต์กํ ์ํ๋ฅผ ๊ฐ์ง๋ฉด ๊ด๋ฆฌ๊ฐ ์ฝ์ง ์์๋ฐ ๊ทธ๋ด๋
https://react-hook-form.com/
์์ ๊ฐ์ ๋๊ตฌ๋ฅผ ๋์
ํด ๋ณผ ์๋ ์์ต๋๋ค ใ
|
@@ -0,0 +1,24 @@
+import React from "react";
+import styled from "styled-components";
+
+const Footer = () => {
+ return (
+ <FooterBlock>
+ <p className="copyright">© Museop Kim</p>
+ </FooterBlock>
+ );
+};
+
+const FooterBlock = styled.div`
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ width: 100%;
+ height: 8rem;
+ margin-top: 6rem;
+ background-color: #f5f5f7;
+ font-weight: 500;
+ box-shadow: 0px -10px 35px 5px #f1f1f1;
+`;
+
+export default Footer; | Unknown | ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฌ์ฉ ํ๋ฏ์ด ์คํ์ผ๋ ์ฌ๋ฌ ๊ณณ์ ์ฌ์ฌ์ฉ ํ๋ ๋ฐฉ๋ฒ์ด ์์๋ค์. `ThemeProvider`์ ๋ํด ์ ๋ชฐ๋๋๋ฐ ์ด๋ฒ ๊ธฐํ์ ํ๋ฒ ํ์ต ํด๋ณด๊ณ ํด๋ณผ ์ ์๋ค๋ฉด ์ ์ฉ๊น์ง ํด๋ณด๊ฒ ์ต๋๋ค! ์กฐ์ธ ํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค ๐ |
@@ -0,0 +1,47 @@
+import React from "react";
+import styled from "styled-components";
+import { Link } from "react-router-dom";
+
+const Navigation = () => {
+ return (
+ <NavigationBlock>
+ <Link to="/">์ ์ฒญ๊ณก</Link>
+ <Link to="/ranking">์ ์ฒญ๊ณก ์์</Link>
+ <Link to="/contents">์ ์ฒญ๊ณก ์ ํ๋ธ ์์</Link>
+ <Link to="/musicsheets">๋ฆฌ์ผํผ์๋
ธ ์
๋ณด์ง</Link>
+ </NavigationBlock>
+ );
+};
+
+const NavigationBlock = styled.div`
+ height: 100%;
+ display: flex;
+ align-items: center;
+
+ & > * {
+ /* padding: 25px 5px 10px 5px; */
+ box-sizing: border-box;
+ display: flex;
+ align-items: center;
+ margin: 0px 30px;
+ padding-top: 10px;
+ font-size: 1.2rem;
+ font-weight: 600;
+ text-decoration: none;
+ align-self: center;
+ color: inherit;
+ opacity: 0.5;
+ height: 100%;
+ /* border-bottom: 1px solid rgba(0, 0, 0, 0); */
+ border-bottom: 3px solid rgba(250, 162, 193, 0);
+ transition: 0.3s;
+
+ :hover {
+ opacity: 1;
+ /* color: #faa2c1; */
+ border-bottom: 3px solid rgba(250, 162, 193, 1);
+ }
+ }
+`;
+
+export default Navigation; | Unknown | ์ฒ์์๋ ๋ง์ ํ์ ๊ฒ์ฒ๋ผ px๊ณผ rem์ ๊ตฌ๋ถํด์ ์ฌ์ฉ ํ๋ค๊ฐ ์๋ ๊ฐ์ธ rem์ผ๋ก ๋จ์๋ฅผ ํต์ผํ๊ณ ์ ์ฉ ํ์ต๋๋ค. ํน์ ์ด๋ ๊ฒ ํ ๊ฒฝ์ฐ ์ฃผ์ํด์ผ ๋ ๊ณ ๋ ค์ฌํญ ๊ฐ์๊ฒ ์์๊น์? |
@@ -0,0 +1,147 @@
+import React from "react";
+import styled from "styled-components";
+
+const MAX_LENGTH = 100;
+
+const Letter = ({
+ id,
+ user,
+ song,
+ songStory,
+ createdDateTime,
+ onReadLetter
+}) => {
+ const { username, avatarUrl } = user;
+ const { title, artist, imageUrl } = song;
+
+ return (
+ <>
+ <LetterBlock onClick={() => onReadLetter(id)}>
+ <SongBlock>
+ <img src={imageUrl} alt="ALBUM IMAGE" className="album-image" />
+ <div className="song-about">
+ <span className="song-about__title">{title}</span>
+ <span className="song-about__artist">{artist}</span>
+ </div>
+ </SongBlock>
+ <SongStory>
+ {songStory.length > MAX_LENGTH
+ ? `${songStory.slice(0, MAX_LENGTH)} ...`
+ : songStory}
+ </SongStory>
+ <UserBlock>
+ <div className="created-time">{createdDateTime}</div>
+ <div className="user-about">
+ <img src={avatarUrl} className="user-about__avatar" />
+ <span className="user-about__name">{username}</span>
+ </div>
+ </UserBlock>
+ </LetterBlock>
+ </>
+ );
+};
+
+const LetterBlock = styled.li`
+ width: 25rem;
+ height: 23rem;
+ margin: 1.2rem;
+ margin-bottom: 5rem;
+ box-sizing: border-box;
+ display: flex;
+ flex-direction: column;
+ padding: 0.7rem;
+ border: #ffdeeb;
+ border-radius: 0.5rem;
+ cursor: pointer;
+ transition: 0.3s;
+ box-shadow: 0 13px 27px -5px rgba(50, 50, 93, 0.2),
+ 0 8px 16px -8px rgba(0, 0, 0, 0.2), 0 -6px 16px -6px rgba(0, 0, 0, 0.025);
+
+ &:hover {
+ box-shadow: 0px 25px 30px 3px rgba(0, 0, 0, 0.3);
+ }
+
+ .album-image {
+ width: 10rem;
+ height: 10rem;
+ }
+`;
+
+const SongBlock = styled.div`
+ display: flex;
+ justify-content: space-between;
+
+ .album-image {
+ position: relative;
+ top: -2.5rem;
+ box-shadow: 3px 2px 5px 1px rgba(0, 0, 0, 0.3);
+ border-radius: 5px;
+ }
+
+ .song-about {
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ margin-right: auto;
+ margin-left: 2rem;
+
+ .song-about__title {
+ overflow-x: hidden;
+ font-size: 1.4rem;
+ font-weight: 500;
+ color: #2c2c2c;
+ opacity: 0.9;
+ }
+
+ .song-about__artist {
+ margin-top: 0.5rem;
+ font-size: 1.2rem;
+ opacity: 0.8;
+ }
+ }
+`;
+
+const SongStory = styled.p`
+ width: 100%;
+ max-height: 8rem;
+ line-break: anywhere;
+ margin-top: -0.3rem;
+ font-size: 1.2rem;
+ line-height: 1.9rem;
+ opacity: 0.9;
+ padding: 0rem 0.5rem;
+`;
+
+const UserBlock = styled.div`
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ margin: auto 5px 3px 5px;
+
+ .created-time {
+ font-size: 1.1rem;
+ margin-top: 0.8rem;
+ opacity: 0.7;
+ }
+
+ .user-about {
+ display: flex;
+ align-items: center;
+ }
+
+ .user-about__avatar {
+ max-width: 2.5rem;
+ max-height: 2.5rem;
+ border-radius: 50%;
+ box-shadow: 3px 2px 10px 1px rgba(0, 0, 0, 0.3);
+ }
+
+ .user-about__name {
+ font-size: 1.1rem;
+ margin-top: 0.5rem;
+ margin-left: 0.5rem;
+ opacity: 0.8;
+ }
+`;
+
+export default Letter; | Unknown | ์ปดํฌ๋ํธ ์ด์ธ์ className์ ์ง์ ์ฌ์ฉํ ๋๋ ๋๋ถ๋ถ CSS ๋ค์ด๋ฐ ์ปจ๋ฒค์
์ผ๋ก ์๋ ค์ง `BEM` ์ ์ ์ฉ ํ์ต๋๋คใ
ใ
`-`๋ ๋ ๋จ์ด๋ฅผ ์ฐ๊ฒฐํด์ผ ํ ๋, `__`๋ ํน์ ๋ธ๋ญ์ ๊ตฌ์ฑํ๋ ํ์ ์์๋ฅผ ๊ตฌ๋ถํ ๋ ์ฌ์ฉ ํ๋ ๋ฏ ํ๋ค์.
[CSS BEM](https://css-tricks.com/bem-101/) ์ ์ฐธ๊ณ ํ์ต๋๋ค :) |
@@ -0,0 +1,29 @@
+import React from "react";
+import styled from "styled-components";
+import LetterContainer from "../../containers/Letter/LetterContainer";
+
+const LetterList = ({ letters }) => {
+ return (
+ <LetterListBlock>
+ {letters.map(letter => (
+ <LetterContainer
+ key={letter.id}
+ id={letter.id}
+ user={letter.user}
+ song={letter.song}
+ songStory={letter.songStory}
+ createdDateTime={letter.createdDateTime}
+ />
+ ))}
+ </LetterListBlock>
+ );
+};
+
+const LetterListBlock = styled.ul`
+ width: 100%;
+ display: flex;
+ flex-wrap: wrap;
+ margin-top: 4.5rem;
+`;
+
+export default LetterList; | Unknown | Container๋ฅผ ์ธ์ ์ฌ์ฉํด์ผ ํ ์ง ๋ช
ํํ ๊ธฐ์ค์ด ์์๋๋ฐ, ๋ฆฌ๋ทฐ ํด์ฃผ์ ๋ด์ฉ์ ๋ณด๊ณ ๊ณ ๋ฏผ ๋๋ ๊ฒ๋ค์ด ๋ง์ด ๋์ด์ง๋ ๋๋์ด์์.
๋ค์ ์์ ๋ ๋ฐ์ ํ๊ฒ ์ต๋๋ค ๐ |
@@ -0,0 +1,30 @@
+import React, { useEffect, useState } from "react";
+import ModalTemplate from "../Template/Modal/ModalTemplate";
+import LetterDetailsSong from "./LetterDetailsSong";
+import LetterDetailsSongStory from "./LetterDetailsSongStory";
+import LetterModalTemplate from "../Template/LetterModal/LetterModalTemplate";
+import LetterDetailsUser from "./LetterDetailsUser";
+import LetterModalDiv from "../LetterModal/LetterModalContents/LetterModalDiv";
+import LetterModalHiddenButtonContainer from "../../containers/LetterModal/LetterModalHiddenButtonContainer";
+import LetterModalButtonContainer from "../../containers/LetterModal/LetterModalButtonContainer";
+import { useSelector } from "react-redux";
+
+const LetterDetails = ({ letter }) => {
+ const { song, songStory, createdDateTime, user } = letter;
+
+ return (
+ <ModalTemplate>
+ <LetterModalTemplate>
+ <LetterModalHiddenButtonContainer />
+ <LetterModalDiv>
+ <LetterDetailsSong song={song} />
+ <LetterDetailsSongStory songStory={songStory} />
+ <LetterDetailsUser user={user} createdDateTime={createdDateTime} />
+ <LetterModalButtonContainer />
+ </LetterModalDiv>
+ </LetterModalTemplate>
+ </ModalTemplate>
+ );
+};
+
+export default LetterDetails; | Unknown | ๊ฐ๋ฅํ๋ค๋ฉด ๊ฐ์ฒด ์์ฒด๋ฅผ ๋๊ธฐ๋ ๊ฒ์ด ์ฌ๋ฌ ์ธก๋ฉด์์ ๋ ์ข์๊ฑฐ ๊ฐ๋ค๋ ์๊ฐ์ด์๋๋ฐ, ์๋ ๊ฒฝ์ฐ๋ ์๊ฒ ๋ค๋ ์๊ฐ์ด ๋๋ค์!
์์ ํ ๋ ๋ฐ์ ํ๊ฒ ์ต๋๋ค ๐ |
@@ -0,0 +1,45 @@
+import React, { useEffect } from "react";
+import { MdMoreHoriz } from "react-icons/md";
+import styled, { css } from "styled-components";
+import LetterDetailsHiddenMenuButton from "./LetterDetailsHiddenMenuButton";
+
+const LetterDetailsHiddenMenu = ({
+ isMouseEnter,
+ isMenuOpen,
+ onToggle,
+ changeToEdit
+}) => {
+ return (
+ <ButtonBlock isMouseEnter={isMouseEnter} onClick={onToggle}>
+ <HiddenMenu />
+ {isMenuOpen && (
+ <LetterDetailsHiddenMenuButton changeToEdit={changeToEdit} />
+ )}
+ </ButtonBlock>
+ );
+};
+
+const ButtonBlock = styled.div`
+ ${props =>
+ props.isMouseEnter
+ ? css`
+ visibility: visible;
+ opacity: 1;
+ `
+ : css`
+ visibility: hidden;
+ opacity: 0;
+ `}
+ transition: 0.7s;
+`;
+
+const HiddenMenu = styled(MdMoreHoriz)`
+ position: absolute;
+ bottom: 93%;
+ right: 3%;
+ cursor: pointer;
+ font-size: 2.1rem;
+ color: gray;
+`;
+
+export default LetterDetailsHiddenMenu; | Unknown | `display: none`์ DOM์ ์ํฅ์ ๋ฏธ์น์ง ์๋ ๋ฐ๋ฉด `visibility: hidden` ์ DOM์ ์ํฅ์ ์ฃผ๋ ๊ฒ์ผ๋ก ์๊ณ ์์์ด์ใ
ใ
`transition` ์ `display: none`์์ ์๋ํ์ง ์์์ `visibility` ์์ฑ์ ์ฌ์ฉํ๊ฒ ๋๋๋ฐ, ํน์ ์ปจ๋ฒค์
์ด ๋ฐ๋ก ์๋ค๊ฑฐ๋ ๋ ํจ์จ์ ์ธ ๋ฐฉ๋ฒ์ด ์์๊น์? |
@@ -0,0 +1,28 @@
+import React from "react";
+import LetterModalSong from "../LetterModal/LetterModalSong/LetterModalSong";
+import SongAbout from "../LetterModal/LetterModalSong/SongAbout";
+import SongArticle from "../LetterModal/LetterModalSong/SongArticle";
+import SongArticleItem from "../LetterModal/LetterModalSong/SongArticleItem";
+import SongArticleName from "../LetterModal/LetterModalSong/SongArticleName";
+import SongImage from "../LetterModal/LetterModalSong/SongImage";
+
+const LetterDetailsSong = ({ song }) => {
+ const { title, artist, imageUrl } = song;
+ return (
+ <LetterModalSong>
+ <SongImage imageUrl={imageUrl} />
+ <SongAbout>
+ <SongArticle>
+ <SongArticleName articleName={"TITLE"} />
+ <SongArticleItem articleName={"TITLE"} item={title} />
+ </SongArticle>
+ <SongArticle>
+ <SongArticleName articleName={"ARTIST"} />
+ <SongArticleItem articleName={"ARTIST"} item={artist} />
+ </SongArticle>
+ </SongAbout>
+ </LetterModalSong>
+ );
+};
+
+export default LetterDetailsSong; | Unknown | ์ปดํฌ๋ํธ๋ฅผ ์์ ํ ๋ค ๋ถ๋ฆฌ ํ ๋ค์ ํ๋ฒ ๋ ์ ๋ฆฌ๋ฅผ ํ์ด์ผ ํ๋๋ฐ ์ด๋ฐ ์ค๋ณต์ ๋ฌถ์ด์ ์ค์ด๋๊ฒ ์ข์๊ฑฐ ๊ฐ์์. ๋ฐ์ ํด๋ณด๊ฒ ์ต๋๋ค. ์๋ ค์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,108 @@
+import React from "react";
+import styled from "styled-components";
+import { BiSearch } from "react-icons/bi";
+import { useDispatch } from "react-redux";
+import { searchSong } from "../../../modules/song";
+
+const SearchForm = ({ onCloseModal }) => {
+ const dispatch = useDispatch();
+
+ const onSearchSong = event => {
+ event.preventDefault();
+ dispatch(searchSong());
+ };
+
+ return (
+ <Form>
+ <SearchInputWrap>
+ <SearchLabel>์ํฐ์คํธ</SearchLabel>
+ <SearchInput type="text" name="artist" />
+ </SearchInputWrap>
+ <SearchInputWrap>
+ <SearchLabel>์ ๋ชฉ</SearchLabel>
+ <SearchInput type="text" name="title" />
+ </SearchInputWrap>
+ <SearchButton type="submit" onClick={onSearchSong}>
+ <SearchIcon />
+ </SearchButton>
+ </Form>
+ );
+};
+
+const Form = styled.form`
+ box-sizing: border-box;
+ width: 95%;
+ display: flex;
+ justify-content: center;
+ margin: 0rem auto 3.5rem auto;
+`;
+
+const SearchLabel = styled.span`
+ display: flex;
+ align-items: center;
+ position: absolute;
+ top: 0rem;
+ left: 0rem;
+ height: 2.37rem;
+ font-size: 1rem;
+ background-color: #727272;
+ padding: 0rem 0.7rem;
+ border-radius: 0.3rem 0rem 0rem 0.3rem;
+ color: white;
+ font-weight: 600;
+ box-shadow: 1px 0px 3px 1px rgba(0, 0, 0, 0.1);
+`;
+
+const SearchInput = styled.input`
+ border: none;
+ outline: none;
+ color: #868e96;
+ width: 100%;
+ margin: 0rem 0.5rem 0rem 5.5rem;
+ font-size: 1.2rem;
+ background-color: #fbfbfd;
+`;
+
+const SearchInputWrap = styled.div`
+ display: flex;
+ align-items: center;
+ position: relative;
+ border: 1px solid #e9ecef;
+ border-radius: 1rem 0rem 0rem 1rem;
+ padding: 0.1rem 0rem;
+ background: #fbfbfd;
+ height: 2.1rem;
+ flex: 11;
+
+ &:nth-child(2) {
+ & > ${SearchLabel} {
+ border-radius: 0.2rem;
+ margin-left: -0.25rem;
+ }
+
+ & > ${SearchInput} {
+ margin-left: 3.8rem;
+ }
+ }
+`;
+
+const SearchButton = styled.button`
+ display: flex;
+ position: relative;
+ justify-content: center;
+ align-items: center;
+ border: none;
+ outline: none;
+ cursor: pointer;
+ background-color: #e1999c;
+ border-radius: 0rem 0.5rem 0.5rem 0rem;
+ flex: 1;
+ box-shadow: 1px 0px 3px 1px rgba(0, 0, 0, 0.1);
+`;
+
+const SearchIcon = styled(BiSearch)`
+ color: #fff;
+ font-size: 1.4rem;
+`;
+
+export default SearchForm; | Unknown | ์ง๊ธ ์ ๋์ ํผ ์ฌ์ฉ์์๋ ์ง์ ๊ตฌํ๋ ๋ฌด๋ฆฌ๊ฐ ์์์ง๋ง, ํผ์ด ๋ณต์กํด์ง๋ฉด ํ์คํ ๊ด๋ฆฌํ๊ธฐ๋ ์ฝ์ง ์์ ๋ฏ ํ๋๋ผ๊ตฌ์.
๊ธฐ์ต ํด๋์๋ค๊ฐ ํ์ํ ๋ ์ ์ฉ ํด๋ณด๊ฒ ์ต๋๋ค :) ์๋ ค์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค. |
@@ -0,0 +1,75 @@
+package bridge.controller;
+
+import bridge.BridgeRandomNumberGenerator;
+import bridge.constants.GameValue;
+import bridge.domain.Bridge;
+import bridge.domain.BridgeGame;
+import bridge.domain.BridgeMaker;
+import bridge.view.handler.InputHandler;
+import bridge.view.OutputView;
+
+public class BridgeController {
+ private final InputHandler inputHandler;
+ private final OutputView outputView;
+ private final BridgeGame bridgeGame;
+ private boolean status = true;
+
+ public BridgeController(InputHandler inputHandler, OutputView outputView, BridgeGame bridgeGame) {
+ this.inputHandler = inputHandler;
+ this.outputView = outputView;
+ this.bridgeGame = bridgeGame;
+ }
+
+ public void start() {
+ outputView.printStartMessage();
+ Bridge bridge = createBridge();
+ run(bridge);
+ }
+
+ private Bridge createBridge() {
+ outputView.printBridgeSizeInputMessage();
+ BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator());
+ return inputHandler.createValidatedBridge(bridgeMaker);
+ }
+
+ private void run(Bridge bridge) {
+ while (status) {
+ move(bridge);
+ if (isFinished()) {
+ finishGame();
+ }
+ if (!isFinished()) {
+ askRetry();
+ }
+ }
+ }
+
+ private void move(Bridge bridge) {
+ for (String square : bridge.getBridge()) {
+ outputView.printMoveInputMessage();
+ String moveCommand = inputHandler.readMoving();
+ bridgeGame.move(square, moveCommand);
+ outputView.printMap(bridgeGame.getMoveResult());
+ }
+ }
+
+ private boolean isFinished() {
+ return bridgeGame.isSuccessful();
+ }
+
+ private void finishGame() {
+ outputView.printResult(bridgeGame);
+ status = false;
+ }
+
+ private void askRetry() {
+ outputView.printRetryMessage();
+ String retryCommand = inputHandler.readGameCommand();
+ if (retryCommand.equals(GameValue.RETRY_COMMAND)) {
+ bridgeGame.retry();
+ }
+ if (retryCommand.equals(GameValue.QUIT_COMMAND)) {
+ outputView.printResult(bridgeGame);
+ }
+ }
+} | Java | ๋ค๋ฆฌ๋ฅผ ๋๋ฉด์ ์ด๋์ ๋ฐ๊ณ ์๋ ํํ๋ผ์,
์ด๋์ด ์คํจํด๋ ๋ฐ๋ก ์ฌ์๋ ์ฌ๋ถ๋ฅผ ๋ฌป์ง ์๊ณ , ๊ทธ๋ฅ ์งํ์ด ๋๋ ๊ฒ ๊ฐ์ต๋๋ค..! |
@@ -0,0 +1,75 @@
+package bridge.controller;
+
+import bridge.BridgeRandomNumberGenerator;
+import bridge.constants.GameValue;
+import bridge.domain.Bridge;
+import bridge.domain.BridgeGame;
+import bridge.domain.BridgeMaker;
+import bridge.view.handler.InputHandler;
+import bridge.view.OutputView;
+
+public class BridgeController {
+ private final InputHandler inputHandler;
+ private final OutputView outputView;
+ private final BridgeGame bridgeGame;
+ private boolean status = true;
+
+ public BridgeController(InputHandler inputHandler, OutputView outputView, BridgeGame bridgeGame) {
+ this.inputHandler = inputHandler;
+ this.outputView = outputView;
+ this.bridgeGame = bridgeGame;
+ }
+
+ public void start() {
+ outputView.printStartMessage();
+ Bridge bridge = createBridge();
+ run(bridge);
+ }
+
+ private Bridge createBridge() {
+ outputView.printBridgeSizeInputMessage();
+ BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator());
+ return inputHandler.createValidatedBridge(bridgeMaker);
+ }
+
+ private void run(Bridge bridge) {
+ while (status) {
+ move(bridge);
+ if (isFinished()) {
+ finishGame();
+ }
+ if (!isFinished()) {
+ askRetry();
+ }
+ }
+ }
+
+ private void move(Bridge bridge) {
+ for (String square : bridge.getBridge()) {
+ outputView.printMoveInputMessage();
+ String moveCommand = inputHandler.readMoving();
+ bridgeGame.move(square, moveCommand);
+ outputView.printMap(bridgeGame.getMoveResult());
+ }
+ }
+
+ private boolean isFinished() {
+ return bridgeGame.isSuccessful();
+ }
+
+ private void finishGame() {
+ outputView.printResult(bridgeGame);
+ status = false;
+ }
+
+ private void askRetry() {
+ outputView.printRetryMessage();
+ String retryCommand = inputHandler.readGameCommand();
+ if (retryCommand.equals(GameValue.RETRY_COMMAND)) {
+ bridgeGame.retry();
+ }
+ if (retryCommand.equals(GameValue.QUIT_COMMAND)) {
+ outputView.printResult(bridgeGame);
+ }
+ }
+} | Java | ์คํจํ๊ณ ์ฌ์๋๋ ์ข
๋ฃ๋ฅผ ์
๋ ฅํ์ ๋, ๊ฒ์์ด ์ข
๋ฃ๋์ง ์๊ณ ๊ณ์ ๋ค์ ์ด๋ ์
๋ ฅ์ ๋ฐ๋ ๊ฒ ๊ฐ์ต๋๋ค..!
์คํจํ๊ณ ์ฌ๊ธฐ๋ก ๋ค์ด์์ status๋ฅผ false๋ก ๋ฐ๊ฟ์ฃผ๋ ๋ถ๋ถ์ด ์์ด์์ธ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,75 @@
+package bridge.controller;
+
+import bridge.BridgeRandomNumberGenerator;
+import bridge.constants.GameValue;
+import bridge.domain.Bridge;
+import bridge.domain.BridgeGame;
+import bridge.domain.BridgeMaker;
+import bridge.view.handler.InputHandler;
+import bridge.view.OutputView;
+
+public class BridgeController {
+ private final InputHandler inputHandler;
+ private final OutputView outputView;
+ private final BridgeGame bridgeGame;
+ private boolean status = true;
+
+ public BridgeController(InputHandler inputHandler, OutputView outputView, BridgeGame bridgeGame) {
+ this.inputHandler = inputHandler;
+ this.outputView = outputView;
+ this.bridgeGame = bridgeGame;
+ }
+
+ public void start() {
+ outputView.printStartMessage();
+ Bridge bridge = createBridge();
+ run(bridge);
+ }
+
+ private Bridge createBridge() {
+ outputView.printBridgeSizeInputMessage();
+ BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator());
+ return inputHandler.createValidatedBridge(bridgeMaker);
+ }
+
+ private void run(Bridge bridge) {
+ while (status) {
+ move(bridge);
+ if (isFinished()) {
+ finishGame();
+ }
+ if (!isFinished()) {
+ askRetry();
+ }
+ }
+ }
+
+ private void move(Bridge bridge) {
+ for (String square : bridge.getBridge()) {
+ outputView.printMoveInputMessage();
+ String moveCommand = inputHandler.readMoving();
+ bridgeGame.move(square, moveCommand);
+ outputView.printMap(bridgeGame.getMoveResult());
+ }
+ }
+
+ private boolean isFinished() {
+ return bridgeGame.isSuccessful();
+ }
+
+ private void finishGame() {
+ outputView.printResult(bridgeGame);
+ status = false;
+ }
+
+ private void askRetry() {
+ outputView.printRetryMessage();
+ String retryCommand = inputHandler.readGameCommand();
+ if (retryCommand.equals(GameValue.RETRY_COMMAND)) {
+ bridgeGame.retry();
+ }
+ if (retryCommand.equals(GameValue.QUIT_COMMAND)) {
+ outputView.printResult(bridgeGame);
+ }
+ }
+} | Java | ์ด๋ํ์ ๋ X๊ฐ ๋์ค๋ฉด ์ฌ์์์ด ๋์์ผํ๋๋ฐ
์ด๊ฑฐ ์ ๊ฐ ๋ฌธ์ ๋ฅผ ์ ๋ชป์ดํดํ๋ค์ ใ
ใ
.. |
@@ -0,0 +1,75 @@
+package bridge.controller;
+
+import bridge.BridgeRandomNumberGenerator;
+import bridge.constants.GameValue;
+import bridge.domain.Bridge;
+import bridge.domain.BridgeGame;
+import bridge.domain.BridgeMaker;
+import bridge.view.handler.InputHandler;
+import bridge.view.OutputView;
+
+public class BridgeController {
+ private final InputHandler inputHandler;
+ private final OutputView outputView;
+ private final BridgeGame bridgeGame;
+ private boolean status = true;
+
+ public BridgeController(InputHandler inputHandler, OutputView outputView, BridgeGame bridgeGame) {
+ this.inputHandler = inputHandler;
+ this.outputView = outputView;
+ this.bridgeGame = bridgeGame;
+ }
+
+ public void start() {
+ outputView.printStartMessage();
+ Bridge bridge = createBridge();
+ run(bridge);
+ }
+
+ private Bridge createBridge() {
+ outputView.printBridgeSizeInputMessage();
+ BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator());
+ return inputHandler.createValidatedBridge(bridgeMaker);
+ }
+
+ private void run(Bridge bridge) {
+ while (status) {
+ move(bridge);
+ if (isFinished()) {
+ finishGame();
+ }
+ if (!isFinished()) {
+ askRetry();
+ }
+ }
+ }
+
+ private void move(Bridge bridge) {
+ for (String square : bridge.getBridge()) {
+ outputView.printMoveInputMessage();
+ String moveCommand = inputHandler.readMoving();
+ bridgeGame.move(square, moveCommand);
+ outputView.printMap(bridgeGame.getMoveResult());
+ }
+ }
+
+ private boolean isFinished() {
+ return bridgeGame.isSuccessful();
+ }
+
+ private void finishGame() {
+ outputView.printResult(bridgeGame);
+ status = false;
+ }
+
+ private void askRetry() {
+ outputView.printRetryMessage();
+ String retryCommand = inputHandler.readGameCommand();
+ if (retryCommand.equals(GameValue.RETRY_COMMAND)) {
+ bridgeGame.retry();
+ }
+ if (retryCommand.equals(GameValue.QUIT_COMMAND)) {
+ outputView.printResult(bridgeGame);
+ }
+ }
+} | Java | ๊ทธ๋ฌ๋ค์ ๋ถ๋ฆฌํ๊ธฐ์ ์๋ ์ ์๋์ํด์ ๋ฆฌํฉํ ๋ง๋จ๊ณ์์ ์ค์ํ๋๋ด
๋๋ค..
๋ง์ง๋ง์ ๋ค์ ํ๋ฒ ๊ผผ๊ผผํ ์ฒดํฌํด์ผ๊ฒ ๋ค์ ๐จ |
@@ -0,0 +1,14 @@
+package bridge.constants;
+
+public class GameValue {
+
+ private GameValue() {
+ }
+
+ public static final String MOVE_UP_COMMAND = "U";
+ public static final String MOVE_DOWN_COMMAND = "D";
+ public static final String RETRY_COMMAND = "R";
+ public static final String QUIT_COMMAND = "Q";
+ public static final String MOVE = "O";
+ public static final String STOP = "X";
+} | Java | ๊ด๋ จ ์๋ ์์๋ผ๋ฆฌ Enum ์ผ๋ก ๋ฌถ์ด๋ด๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค :) |
@@ -0,0 +1,17 @@
+package bridge.constants;
+
+import java.util.regex.Pattern;
+
+public enum RegexPattern {
+ ONLY_NUMBER(Pattern.compile("\\d+"));
+
+ private final Pattern pattern;
+
+ RegexPattern(Pattern pattern) {
+ this.pattern = pattern;
+ }
+
+ public boolean matches(String value) {
+ return pattern.matcher(value).matches();
+ }
+} | Java | Pattern ์์ฒด๋ฅผ Enum ์ผ๋ก ์ฌ์ฉํ๋ ๊ฒ์ ์๊ฐํด๋ณด์ง ๋ชปํ๋๋ฐ, ์ด๊ฑฐ ๊ฝค๋ ์ฌํ์ฉ์ฑ์ด ๋๊ฒ ๊ตฐ์ ๐ |
@@ -0,0 +1,75 @@
+package bridge.controller;
+
+import bridge.BridgeRandomNumberGenerator;
+import bridge.constants.GameValue;
+import bridge.domain.Bridge;
+import bridge.domain.BridgeGame;
+import bridge.domain.BridgeMaker;
+import bridge.view.handler.InputHandler;
+import bridge.view.OutputView;
+
+public class BridgeController {
+ private final InputHandler inputHandler;
+ private final OutputView outputView;
+ private final BridgeGame bridgeGame;
+ private boolean status = true;
+
+ public BridgeController(InputHandler inputHandler, OutputView outputView, BridgeGame bridgeGame) {
+ this.inputHandler = inputHandler;
+ this.outputView = outputView;
+ this.bridgeGame = bridgeGame;
+ }
+
+ public void start() {
+ outputView.printStartMessage();
+ Bridge bridge = createBridge();
+ run(bridge);
+ }
+
+ private Bridge createBridge() {
+ outputView.printBridgeSizeInputMessage();
+ BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator());
+ return inputHandler.createValidatedBridge(bridgeMaker);
+ }
+
+ private void run(Bridge bridge) {
+ while (status) {
+ move(bridge);
+ if (isFinished()) {
+ finishGame();
+ }
+ if (!isFinished()) {
+ askRetry();
+ }
+ }
+ }
+
+ private void move(Bridge bridge) {
+ for (String square : bridge.getBridge()) {
+ outputView.printMoveInputMessage();
+ String moveCommand = inputHandler.readMoving();
+ bridgeGame.move(square, moveCommand);
+ outputView.printMap(bridgeGame.getMoveResult());
+ }
+ }
+
+ private boolean isFinished() {
+ return bridgeGame.isSuccessful();
+ }
+
+ private void finishGame() {
+ outputView.printResult(bridgeGame);
+ status = false;
+ }
+
+ private void askRetry() {
+ outputView.printRetryMessage();
+ String retryCommand = inputHandler.readGameCommand();
+ if (retryCommand.equals(GameValue.RETRY_COMMAND)) {
+ bridgeGame.retry();
+ }
+ if (retryCommand.equals(GameValue.QUIT_COMMAND)) {
+ outputView.printResult(bridgeGame);
+ }
+ }
+} | Java | ์ด ๋ถ๋ถ์ ์ธ๋ดํธ๋ฅผ 1๋ก ๋ฆฌํฉํ ๋งํ๋ ๊ณ ํต์ค๋ฌ์ด(?) ๊ฒฝํ์ ํด๋ณด์
๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค!
์ ๋ ์ ๋ ๋ชป ์ค์ผ ๊ฒ ๊ฐ๋ ์ธ๋ดํธ๋ฅผ ์ค์ด๋ ค๊ณ ์ด๋ฆฌ์ ๋ฆฌ ๋นํ๋ค๋ณด๋(?) ๋ค์ํ ์ธ์ฌ์ดํธ๋ค์ด ๋ ์ค๋ฅด๋๋ผ๊ณ ์..! |
@@ -0,0 +1,82 @@
+package bridge.domain;
+
+import bridge.constants.GameValue;
+
+/**
+ * ๋ค๋ฆฌ ๊ฑด๋๊ธฐ ๊ฒ์์ ๊ด๋ฆฌํ๋ ํด๋์ค
+ */
+public class BridgeGame {
+
+ private int retryCount;
+ private MoveResult moveResult;
+
+ public BridgeGame(MoveResult moveResult, int retryCount) {
+ this.moveResult = moveResult;
+ this.retryCount = retryCount;
+ }
+
+ /**
+ * ์ฌ์ฉ์๊ฐ ์นธ์ ์ด๋ํ ๋ ์ฌ์ฉํ๋ ๋ฉ์๋
+ */
+ public void move(String square, String moveCommand) {
+ if (moveCommand.equals(GameValue.MOVE_UP_COMMAND)) {
+ moveUp(square, moveCommand);
+ }
+ if (moveCommand.equals(GameValue.MOVE_DOWN_COMMAND)) {
+ moveLower(square, moveCommand);
+ }
+ }
+
+ private boolean isMoveUp(String square, String moveCommand) {
+ return square.equals(moveCommand);
+ }
+
+ private void moveUp(String square, String moveCommand) {
+ if (isMoveUp(square, moveCommand)) {
+ moveResult.moveUp(GameValue.MOVE);
+ }
+ if (!isMoveUp(square, moveCommand)) {
+ moveResult.moveUp(GameValue.STOP);
+ }
+ }
+
+ private boolean isMoveLower(String square, String moveCommand) {
+ return square.equals(moveCommand);
+ }
+
+ private void moveLower(String square, String moveCommand) {
+ if (isMoveLower(square, moveCommand)) {
+ moveResult.moveLowwer(GameValue.MOVE);
+ }
+ if (!isMoveLower(square, moveCommand)) {
+ moveResult.moveLowwer(GameValue.STOP);
+ }
+ }
+
+ public boolean isSuccessful() {
+ return moveResult.isSuccessful();
+ }
+
+ public String getGameResult() {
+ if (isSuccessful()) {
+ return "์ฑ๊ณต";
+ }
+ return "์คํจ";
+ }
+
+ /**
+ * ์ฌ์ฉ์๊ฐ ๊ฒ์์ ๋ค์ ์๋ํ ๋ ์ฌ์ฉํ๋ ๋ฉ์๋
+ */
+ public void retry() {
+ retryCount++;
+ moveResult = new MoveResult();
+ }
+
+ public int getRetryCount() {
+ return retryCount;
+ }
+
+ public MoveResult getMoveResult() {
+ return moveResult;
+ }
+} | Java | ์ฌ๊ธฐ early return ์ด ๋น ์ง ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,37 @@
+package bridge.domain;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class MoveResult {
+ private final List<String> upperBridge;
+ private final List<String> lowerBridge;
+
+ public MoveResult() {
+ this.upperBridge = new ArrayList<>();
+ this.lowerBridge = new ArrayList<>();
+ }
+
+ public void moveUp(String moveResult) {
+ upperBridge.add(moveResult);
+ lowerBridge.add(" ");
+ }
+
+ public void moveLowwer(String moveResult) {
+ upperBridge.add(" ");
+ lowerBridge.add(moveResult);
+ }
+
+ public boolean isSuccessful() {
+ if (upperBridge.contains("X") || lowerBridge.contains("X")) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return "[ " + String.join(" | ", upperBridge) + " ]\n" +
+ "[ " + String.join(" | ", lowerBridge) + " ]";
+ }
+} | Java | > ํต์ฌ ๋ก์ง์ ๊ตฌํํ๋ ์ฝ๋์ UI๋ฅผ ๋ด๋นํ๋ ๋ก์ง์ ๋ถ๋ฆฌํด ๊ตฌํํ๋ค.
์ด ๋ถ๋ถ์ ์๊ตฌ์ฌํญ์ด ์ง์ผ์ง์ง ์์ ๊ฒ ๊ฐ์ต๋๋ค.
ํ๋ฆฌ์ฝ์ค ๊ธฐ๊ฐ ๋์ ๊ฐ์ฅ ๋ง์ด ์๊ตฌ๋ ํ๋ก๊ทธ๋๋ฐ ๊ธฐ๋ฒ์ด๋ผ, ์ด ๋ถ๋ถ์ ๊ผญ ๋ฆฌํฉํ ๋ง ํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,56 @@
+package bridge.view;
+
+import bridge.domain.BridgeGame;
+import bridge.domain.MoveResult;
+
+/**
+ * ์ฌ์ฉ์์๊ฒ ๊ฒ์ ์งํ ์ํฉ๊ณผ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ์ญํ ์ ํ๋ค.
+ */
+public class OutputView {
+
+ public void printStartMessage() {
+ println("๋ค๋ฆฌ ๊ฑด๋๊ธฐ ๊ฒ์์ ์์ํฉ๋๋ค.");
+ }
+
+ public void printBridgeSizeInputMessage() {
+ printNewLine();
+ println("๋ค๋ฆฌ์ ๊ธธ์ด๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
+ }
+
+ public void printMoveInputMessage() {
+ printNewLine();
+ println("์ด๋ํ ์นธ์ ์ ํํด์ฃผ์ธ์. (์: U, ์๋: D)");
+ }
+
+ /**
+ * ํ์ฌ๊น์ง ์ด๋ํ ๋ค๋ฆฌ์ ์ํ๋ฅผ ์ ํด์ง ํ์์ ๋ง์ถฐ ์ถ๋ ฅํ๋ค.
+ */
+ public void printMap(MoveResult moveResult) {
+ println(moveResult.toString());
+ }
+
+ /**
+ * ๊ฒ์์ ์ต์ข
๊ฒฐ๊ณผ๋ฅผ ์ ํด์ง ํ์์ ๋ง์ถฐ ์ถ๋ ฅํ๋ค.
+ */
+ public void printResult(BridgeGame bridgeGame) {
+ printNewLine();
+ println("์ต์ข
๊ฒ์ ๊ฒฐ๊ณผ");
+ printMap(bridgeGame.getMoveResult());
+ printNewLine();
+ System.out.printf("๊ฒ์ ์ฑ๊ณต ์ฌ๋ถ: %s\n", bridgeGame.getGameResult());
+ System.out.printf("์ด ์๋ํ ํ์: %d\n", bridgeGame.getRetryCount());
+ }
+
+ public void printRetryMessage() {
+ printNewLine();
+ println("๊ฒ์์ ๋ค์ ์๋ํ ์ง ์ฌ๋ถ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์. (์ฌ์๋: R, ์ข
๋ฃ: Q)");
+ }
+
+ private void println(String output) {
+ System.out.println(output);
+ }
+
+ private void printNewLine() {
+ System.out.println();
+ }
+} | Java | ํ์คํ ์ถ๋ ฅ์ฉ ๋ฉ์์ง๊ฐ ์ฌํ์ฉ๋๋ ๊ฒฝ์ฐ๊ฐ ์๋ค๋ณด๋, ์ด๋ ๊ฒ ํ๋ ์ฝ๋ฉํด๋ ๊ฝค๋ ์ง๊ด์ ์ด์์? ๋ผ๋ ๋๋์ ๋ฐ์์ต๋๋ค ๐ |
@@ -0,0 +1,42 @@
+package bridge.view.handler;
+
+import bridge.domain.Bridge;
+import bridge.domain.BridgeMaker;
+import bridge.view.ErrorView;
+import bridge.view.InputView;
+import java.util.function.Supplier;
+
+public class InputHandler {
+ private final InputView inputView;
+ private final ErrorView errorView;
+
+ public InputHandler(InputView inputView, ErrorView errorView) {
+ this.inputView = inputView;
+ this.errorView = errorView;
+ }
+
+ public Bridge createValidatedBridge(BridgeMaker bridgeMaker) {
+ return receiveValidatedInput(() -> {
+ int bridgeSize = inputView.readBridgeSize();
+ return Bridge.from(bridgeMaker.makeBridge(bridgeSize));
+ });
+ }
+
+ public String readMoving() {
+ return receiveValidatedInput(inputView::readMoving);
+ }
+
+ public String readGameCommand() {
+ return receiveValidatedInput(inputView::readGameCommand);
+ }
+
+ private <T> T receiveValidatedInput(Supplier<T> inputView) {
+ while (true) {
+ try {
+ return inputView.get();
+ } catch (IllegalArgumentException exception) {
+ errorView.printErrorMessage(exception.getMessage());
+ }
+ }
+ }
+} | Java | (๊ถ๊ธํด์!)
๊ทธ๋์ ํ์ค๋๊ณผ ๋ฆฌ๋ทฐํด์ค๋ฉด์ ์ด๋ฐ ์ฌ์๋ ๋ก์ง์ while ๋ก ์ฒ๋ฆฌํ ๊น, ์ฌ๊ท๋ก ์ฒ๋ฆฌํ ๊น ๊ณ ๋ฏผํ๋ค๊ฐ ์ ๋ ์ฌ๊ท๋ฅผ ์ ํํ์ต๋๋ค.
๊ทธ๋ฐ๋ฐ ์ฌ๊ท๋ฅผ ์ ํํด๋ ํธ์ถ ๋์ค๊ฐ ๊น์ด์ง๋๊ฒ ์๋๊ณ ๋์ค๊ฐ 1๋ก ๊ณ์ ์ ์ง๋๋ ๊ฒ ๊ฐ๋๋ผ๊ณ ์. (์ด์ฐจํผ catch๋ฅผ ํ๊ณ ์๋ฌ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ์ ํ๋ฏ๋ก)
ํ์ค๋์ ์ฌ๊ท ๋์ while์ ์ ํํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค :) |
@@ -0,0 +1,82 @@
+package bridge.domain;
+
+import bridge.constants.GameValue;
+
+/**
+ * ๋ค๋ฆฌ ๊ฑด๋๊ธฐ ๊ฒ์์ ๊ด๋ฆฌํ๋ ํด๋์ค
+ */
+public class BridgeGame {
+
+ private int retryCount;
+ private MoveResult moveResult;
+
+ public BridgeGame(MoveResult moveResult, int retryCount) {
+ this.moveResult = moveResult;
+ this.retryCount = retryCount;
+ }
+
+ /**
+ * ์ฌ์ฉ์๊ฐ ์นธ์ ์ด๋ํ ๋ ์ฌ์ฉํ๋ ๋ฉ์๋
+ */
+ public void move(String square, String moveCommand) {
+ if (moveCommand.equals(GameValue.MOVE_UP_COMMAND)) {
+ moveUp(square, moveCommand);
+ }
+ if (moveCommand.equals(GameValue.MOVE_DOWN_COMMAND)) {
+ moveLower(square, moveCommand);
+ }
+ }
+
+ private boolean isMoveUp(String square, String moveCommand) {
+ return square.equals(moveCommand);
+ }
+
+ private void moveUp(String square, String moveCommand) {
+ if (isMoveUp(square, moveCommand)) {
+ moveResult.moveUp(GameValue.MOVE);
+ }
+ if (!isMoveUp(square, moveCommand)) {
+ moveResult.moveUp(GameValue.STOP);
+ }
+ }
+
+ private boolean isMoveLower(String square, String moveCommand) {
+ return square.equals(moveCommand);
+ }
+
+ private void moveLower(String square, String moveCommand) {
+ if (isMoveLower(square, moveCommand)) {
+ moveResult.moveLowwer(GameValue.MOVE);
+ }
+ if (!isMoveLower(square, moveCommand)) {
+ moveResult.moveLowwer(GameValue.STOP);
+ }
+ }
+
+ public boolean isSuccessful() {
+ return moveResult.isSuccessful();
+ }
+
+ public String getGameResult() {
+ if (isSuccessful()) {
+ return "์ฑ๊ณต";
+ }
+ return "์คํจ";
+ }
+
+ /**
+ * ์ฌ์ฉ์๊ฐ ๊ฒ์์ ๋ค์ ์๋ํ ๋ ์ฌ์ฉํ๋ ๋ฉ์๋
+ */
+ public void retry() {
+ retryCount++;
+ moveResult = new MoveResult();
+ }
+
+ public int getRetryCount() {
+ return retryCount;
+ }
+
+ public MoveResult getMoveResult() {
+ return moveResult;
+ }
+} | Java | else๋ฌธ์ด ์๋๋ผ์ ์๋ฌด๋๋ return์ ์ถ๊ฐํด ๋ค์ ์กฐ๊ฑด์ ํ์ธํ์ง ์๋๋ก ํ๋๊ฒ ์ข๊ฒ ๋ค์ ! |
@@ -0,0 +1,37 @@
+package bridge.domain;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class MoveResult {
+ private final List<String> upperBridge;
+ private final List<String> lowerBridge;
+
+ public MoveResult() {
+ this.upperBridge = new ArrayList<>();
+ this.lowerBridge = new ArrayList<>();
+ }
+
+ public void moveUp(String moveResult) {
+ upperBridge.add(moveResult);
+ lowerBridge.add(" ");
+ }
+
+ public void moveLowwer(String moveResult) {
+ upperBridge.add(" ");
+ lowerBridge.add(moveResult);
+ }
+
+ public boolean isSuccessful() {
+ if (upperBridge.contains("X") || lowerBridge.contains("X")) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return "[ " + String.join(" | ", upperBridge) + " ]\n" +
+ "[ " + String.join(" | ", lowerBridge) + " ]";
+ }
+} | Java | ํ ๊ทธ ๋ถ๋ถ์ ๋์ณ๋ฒ๋ ธ๋ค์ ..
์ด๋ฒ 5๊ธฐ ์ต์ข
๊น์ง ์น๋ฉด์ ๋๋๊ฑด๋ฐ 5์๊ฐ์ด๋ผ๋ ์๊ฐ์ ์ฝ๋งค์ด์ง ์๊ณ
์ด๋ฐ์ ๊ธฐ๋ฅ ์๊ตฌ ์ฌํญ์ ๊ผผ๊ผผํ ๊ฒํ ํด์ผ ํ๋ค๋ ๊ฒ์ ๋ผ์ ๋ฆฌ๊ฒ ๋๊ผ์ต๋๋ค.. |
@@ -0,0 +1,42 @@
+package bridge.view.handler;
+
+import bridge.domain.Bridge;
+import bridge.domain.BridgeMaker;
+import bridge.view.ErrorView;
+import bridge.view.InputView;
+import java.util.function.Supplier;
+
+public class InputHandler {
+ private final InputView inputView;
+ private final ErrorView errorView;
+
+ public InputHandler(InputView inputView, ErrorView errorView) {
+ this.inputView = inputView;
+ this.errorView = errorView;
+ }
+
+ public Bridge createValidatedBridge(BridgeMaker bridgeMaker) {
+ return receiveValidatedInput(() -> {
+ int bridgeSize = inputView.readBridgeSize();
+ return Bridge.from(bridgeMaker.makeBridge(bridgeSize));
+ });
+ }
+
+ public String readMoving() {
+ return receiveValidatedInput(inputView::readMoving);
+ }
+
+ public String readGameCommand() {
+ return receiveValidatedInput(inputView::readGameCommand);
+ }
+
+ private <T> T receiveValidatedInput(Supplier<T> inputView) {
+ while (true) {
+ try {
+ return inputView.get();
+ } catch (IllegalArgumentException exception) {
+ errorView.printErrorMessage(exception.getMessage());
+ }
+ }
+ }
+} | Java | ์ฌ๊ท ํธ์ถ๋ก ๊ตฌํํ์ ๋ ์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ง๋ค๋ ์ฅ์ ์ ๊ฐ์ง๊ณ ์์ง๋ง
ํ์ ๊น์ด๊ฐ ๊น์ด ์ง ๋ ์ํํ ๊ฒ์ด๋ผ ์๊ฐํ์ต๋๋ค.
์ ํธ๋ ๋ง์๋๋ก 1๋ก ์ ์ง๋๋ค๋ฉด ์ด ์ ์ ์๋ฌด๋๋ ์ ์ธํด์ผํ ๊ฒ ๊ฐ๋ค์
ํ์ง๋ง while๋ฌธ์ ์ฌ์ฉํ์ ๋ ๋ฐ๋ณต ํ์๋ฅผ ์ ์ดํ ์ ์๋ค๋ ์ฅ์ ๋ ์๊ธฐ์ while๋ฌธ์ ์ฌ์ฉํ์์ต๋๋ค.
์๋ฅผ ๋ค์ด์ ์ค์ ๋ก๊ทธ์ธ ์ ์ฑ
์ฒ๋ผ ์๋ชป๋ ๊ฐ์ 3ํ ๋๋ 5ํ ์
๋ ฅ ํ์ ๊ฒฝ์ฐ ์
๋ ฅ์ ์ข
๋ฃ ์ํจ๋ค๋๊ฐ ๋ฑ์ ์ํฉ๋ ์๊ฐํด๋ณด์์ต๋๋ค! |
@@ -0,0 +1,42 @@
+package bridge.view.handler;
+
+import bridge.domain.Bridge;
+import bridge.domain.BridgeMaker;
+import bridge.view.ErrorView;
+import bridge.view.InputView;
+import java.util.function.Supplier;
+
+public class InputHandler {
+ private final InputView inputView;
+ private final ErrorView errorView;
+
+ public InputHandler(InputView inputView, ErrorView errorView) {
+ this.inputView = inputView;
+ this.errorView = errorView;
+ }
+
+ public Bridge createValidatedBridge(BridgeMaker bridgeMaker) {
+ return receiveValidatedInput(() -> {
+ int bridgeSize = inputView.readBridgeSize();
+ return Bridge.from(bridgeMaker.makeBridge(bridgeSize));
+ });
+ }
+
+ public String readMoving() {
+ return receiveValidatedInput(inputView::readMoving);
+ }
+
+ public String readGameCommand() {
+ return receiveValidatedInput(inputView::readGameCommand);
+ }
+
+ private <T> T receiveValidatedInput(Supplier<T> inputView) {
+ while (true) {
+ try {
+ return inputView.get();
+ } catch (IllegalArgumentException exception) {
+ errorView.printErrorMessage(exception.getMessage());
+ }
+ }
+ }
+} | Java | ํ์คํ **3ํ ๊น์ง๋ง ์ฌ์๋๊ฐ ๊ฐ๋ฅํ๋ค** ๋ผ๋ ๋ก์ง์ด ์์ ๊ฒฝ์ฐ์๋ while ๋ฌธ์ผ๋ก ์ฒ๋ฆฌํ๋๊ฒ ์ ์ฐํ๊ฒ ๋์ํ ์ ์๊ฒ ๋ค์!
์ธ์ฌ์ดํธ ๋ฐ๊ณ ๊ฐ๋๋ค :D |
@@ -1,9 +1,100 @@
-function Header(logo, searchBox) {
+import Input from './Input';
+import Button from './Button';
+import { handleSearch } from '../services/handleSeach';
+
+function Header({ logoSrc, logoAlt, onSearch }) {
const headerElement = document.createElement('header');
- headerElement.classList.add('header');
- headerElement.appendChild(logo);
- headerElement.appendChild(searchBox);
+ headerElement.innerHTML = `
+ <h1><img src="${logoSrc}" alt="${logoAlt}" loading="lazy"></h1>
+ <div class="search-box"></div>
+ `;
+
+ let page = 1;
+
+ const searchInput = Input({
+ id: 'search-input',
+ type: 'text',
+ placeholder: '๊ฒ์',
+ onKeyDown: async (event) => {
+ if (event.code === 'Enter') {
+ document.getElementById('more-movies').remove();
+ const sectionContainer = document.querySelector('.item-view');
+
+ sectionContainer.appendChild(
+ Button({
+ classNames: ['btn', 'primary', 'full-width'],
+ type: 'button',
+ id: 'more-movies',
+ name: '๋ ๋ณด๊ธฐ',
+ onClick: () => {
+ page += 1;
+ handleSearch(searchInput.value, page);
+ }
+ })
+ );
+
+ await onSearch(event.target.value, page);
+ }
+ }
+ });
+
+ const searchButton = Button({
+ classNames: ['search-button'],
+ name: '๊ฒ์',
+ type: 'button',
+ onClick: async () => {
+ document.getElementById('more-movies').remove();
+ const sectionContainer = document.querySelector('.item-view');
+
+ sectionContainer.appendChild(
+ Button({
+ classNames: ['btn', 'primary', 'full-width'],
+ type: 'button',
+ id: 'more-movies',
+ name: '๋ ๋ณด๊ธฐ',
+ onClick: () => {
+ page += 1;
+ handleSearch(searchInput.value, page);
+ }
+ })
+ );
+
+ const inputValue = document.getElementById('search-input').value;
+ await onSearch(inputValue, page);
+ }
+ });
+
+ const searchBox = headerElement.querySelector('.search-box');
+
+ searchBox.addEventListener('mouseenter', () => {
+ const width = document.querySelector('body').getBoundingClientRect().width;
+
+ if (width <= 390) {
+ const input = document.getElementById('search-input');
+ const logo = document.querySelector('header h1');
+
+ searchBox.classList.add('search-box-toggle');
+ input.classList.add('input-toggle');
+ logo.classList.add('logo-toggle');
+ }
+ });
+
+ searchBox.addEventListener('mouseleave', () => {
+ const width = document.querySelector('body').getBoundingClientRect().width;
+
+ if (width <= 390) {
+ const input = document.getElementById('search-input');
+ const logo = document.querySelector('header h1');
+
+ searchBox.classList.remove('search-box-toggle');
+ input.classList.remove('input-toggle');
+ logo.classList.remove('logo-toggle');
+ }
+ });
+
+ searchBox.appendChild(searchInput);
+ searchBox.appendChild(searchButton);
return headerElement;
} | JavaScript | ์ ๋ ์ด๋ฒคํธ์ ์ด๋ฆ์ด ์๋ฏธํ๋๊ฒ "ํ์์ ๋ฐ์" ์ด๋ผ๊ณ ์๊ฐํด์.
๊ฐ๋ น, `onChange` ๋ ์ค์ ๋ก change๊ฐ ์ผ์ด๋ฌ์ ๋์ด๊ณ , `onKeyDown` ์ ํค๋ฅผ ๋๋ ์ ๋ ์
๋๋ค.
`onSearch`๋ "๊ฒ์์ด ๋์์ ๋ ๋ฐ์" ์ธ๋ฐ, ์ด ์ปดํฌ๋ํธ์์ ์ค์ ๋ก "๊ฒ์"์ด๋ผ๋ ํ์๊ฐ ์ผ์ด๋ฌ๋ค๊ณ ํ ์ ์์๊น์?
์ง๊ธ์ "๊ฒ์"์ด๋ผ๋ ํ์๋ฅผ ๋ฐ์์ ์ฃผ์
ํด์ฃผ๊ณ , ์ด ์ปดํฌ๋ํธ ๋ด๋ถ์์ ์ผ์ด๋ ์ผ์ "์ํฐํค๋ฅผ ๋๋ ์" ์
๋๋ค.
์ฆ, ์ด๋ฒคํธ๋ "๊ฒ์ํ์ ๋ ๋ฐ์ํจ"์ ์ด์ผ๊ธฐํ๊ณ ์๋๋ฐ ๊ฒ์์ด๋ผ๋ ๊ธฐ๋ฅ์ ๋ฐ์์ ์ฃผ์
ํด์ค ์ ๋ฐ์ ์๋ ์ํฉ์ธ๊ฑฐ์ฃ . |
@@ -1,9 +1,100 @@
-function Header(logo, searchBox) {
+import Input from './Input';
+import Button from './Button';
+import { handleSearch } from '../services/handleSeach';
+
+function Header({ logoSrc, logoAlt, onSearch }) {
const headerElement = document.createElement('header');
- headerElement.classList.add('header');
- headerElement.appendChild(logo);
- headerElement.appendChild(searchBox);
+ headerElement.innerHTML = `
+ <h1><img src="${logoSrc}" alt="${logoAlt}" loading="lazy"></h1>
+ <div class="search-box"></div>
+ `;
+
+ let page = 1;
+
+ const searchInput = Input({
+ id: 'search-input',
+ type: 'text',
+ placeholder: '๊ฒ์',
+ onKeyDown: async (event) => {
+ if (event.code === 'Enter') {
+ document.getElementById('more-movies').remove();
+ const sectionContainer = document.querySelector('.item-view');
+
+ sectionContainer.appendChild(
+ Button({
+ classNames: ['btn', 'primary', 'full-width'],
+ type: 'button',
+ id: 'more-movies',
+ name: '๋ ๋ณด๊ธฐ',
+ onClick: () => {
+ page += 1;
+ handleSearch(searchInput.value, page);
+ }
+ })
+ );
+
+ await onSearch(event.target.value, page);
+ }
+ }
+ });
+
+ const searchButton = Button({
+ classNames: ['search-button'],
+ name: '๊ฒ์',
+ type: 'button',
+ onClick: async () => {
+ document.getElementById('more-movies').remove();
+ const sectionContainer = document.querySelector('.item-view');
+
+ sectionContainer.appendChild(
+ Button({
+ classNames: ['btn', 'primary', 'full-width'],
+ type: 'button',
+ id: 'more-movies',
+ name: '๋ ๋ณด๊ธฐ',
+ onClick: () => {
+ page += 1;
+ handleSearch(searchInput.value, page);
+ }
+ })
+ );
+
+ const inputValue = document.getElementById('search-input').value;
+ await onSearch(inputValue, page);
+ }
+ });
+
+ const searchBox = headerElement.querySelector('.search-box');
+
+ searchBox.addEventListener('mouseenter', () => {
+ const width = document.querySelector('body').getBoundingClientRect().width;
+
+ if (width <= 390) {
+ const input = document.getElementById('search-input');
+ const logo = document.querySelector('header h1');
+
+ searchBox.classList.add('search-box-toggle');
+ input.classList.add('input-toggle');
+ logo.classList.add('logo-toggle');
+ }
+ });
+
+ searchBox.addEventListener('mouseleave', () => {
+ const width = document.querySelector('body').getBoundingClientRect().width;
+
+ if (width <= 390) {
+ const input = document.getElementById('search-input');
+ const logo = document.querySelector('header h1');
+
+ searchBox.classList.remove('search-box-toggle');
+ input.classList.remove('input-toggle');
+ logo.classList.remove('logo-toggle');
+ }
+ });
+
+ searchBox.appendChild(searchInput);
+ searchBox.appendChild(searchButton);
return headerElement;
} | JavaScript | ์ด ๋ถ๋ถ๋ ๋์ผํฉ๋๋ค! |
@@ -1,9 +1,100 @@
-function Header(logo, searchBox) {
+import Input from './Input';
+import Button from './Button';
+import { handleSearch } from '../services/handleSeach';
+
+function Header({ logoSrc, logoAlt, onSearch }) {
const headerElement = document.createElement('header');
- headerElement.classList.add('header');
- headerElement.appendChild(logo);
- headerElement.appendChild(searchBox);
+ headerElement.innerHTML = `
+ <h1><img src="${logoSrc}" alt="${logoAlt}" loading="lazy"></h1>
+ <div class="search-box"></div>
+ `;
+
+ let page = 1;
+
+ const searchInput = Input({
+ id: 'search-input',
+ type: 'text',
+ placeholder: '๊ฒ์',
+ onKeyDown: async (event) => {
+ if (event.code === 'Enter') {
+ document.getElementById('more-movies').remove();
+ const sectionContainer = document.querySelector('.item-view');
+
+ sectionContainer.appendChild(
+ Button({
+ classNames: ['btn', 'primary', 'full-width'],
+ type: 'button',
+ id: 'more-movies',
+ name: '๋ ๋ณด๊ธฐ',
+ onClick: () => {
+ page += 1;
+ handleSearch(searchInput.value, page);
+ }
+ })
+ );
+
+ await onSearch(event.target.value, page);
+ }
+ }
+ });
+
+ const searchButton = Button({
+ classNames: ['search-button'],
+ name: '๊ฒ์',
+ type: 'button',
+ onClick: async () => {
+ document.getElementById('more-movies').remove();
+ const sectionContainer = document.querySelector('.item-view');
+
+ sectionContainer.appendChild(
+ Button({
+ classNames: ['btn', 'primary', 'full-width'],
+ type: 'button',
+ id: 'more-movies',
+ name: '๋ ๋ณด๊ธฐ',
+ onClick: () => {
+ page += 1;
+ handleSearch(searchInput.value, page);
+ }
+ })
+ );
+
+ const inputValue = document.getElementById('search-input').value;
+ await onSearch(inputValue, page);
+ }
+ });
+
+ const searchBox = headerElement.querySelector('.search-box');
+
+ searchBox.addEventListener('mouseenter', () => {
+ const width = document.querySelector('body').getBoundingClientRect().width;
+
+ if (width <= 390) {
+ const input = document.getElementById('search-input');
+ const logo = document.querySelector('header h1');
+
+ searchBox.classList.add('search-box-toggle');
+ input.classList.add('input-toggle');
+ logo.classList.add('logo-toggle');
+ }
+ });
+
+ searchBox.addEventListener('mouseleave', () => {
+ const width = document.querySelector('body').getBoundingClientRect().width;
+
+ if (width <= 390) {
+ const input = document.getElementById('search-input');
+ const logo = document.querySelector('header h1');
+
+ searchBox.classList.remove('search-box-toggle');
+ input.classList.remove('input-toggle');
+ logo.classList.remove('logo-toggle');
+ }
+ });
+
+ searchBox.appendChild(searchInput);
+ searchBox.appendChild(searchButton);
return headerElement;
} | JavaScript | ์ฌ๊ธฐ์ ์ฐ์ด๋ 390 ์ด๋ผ๋ ์ซ์๋ ์์๋ก ๋ง๋ค์ด์ ์ฌ์ฉํด์ฃผ๋ฉด ์ด๋จ๊น์!? |
@@ -1,9 +1,100 @@
-function Header(logo, searchBox) {
+import Input from './Input';
+import Button from './Button';
+import { handleSearch } from '../services/handleSeach';
+
+function Header({ logoSrc, logoAlt, onSearch }) {
const headerElement = document.createElement('header');
- headerElement.classList.add('header');
- headerElement.appendChild(logo);
- headerElement.appendChild(searchBox);
+ headerElement.innerHTML = `
+ <h1><img src="${logoSrc}" alt="${logoAlt}" loading="lazy"></h1>
+ <div class="search-box"></div>
+ `;
+
+ let page = 1;
+
+ const searchInput = Input({
+ id: 'search-input',
+ type: 'text',
+ placeholder: '๊ฒ์',
+ onKeyDown: async (event) => {
+ if (event.code === 'Enter') {
+ document.getElementById('more-movies').remove();
+ const sectionContainer = document.querySelector('.item-view');
+
+ sectionContainer.appendChild(
+ Button({
+ classNames: ['btn', 'primary', 'full-width'],
+ type: 'button',
+ id: 'more-movies',
+ name: '๋ ๋ณด๊ธฐ',
+ onClick: () => {
+ page += 1;
+ handleSearch(searchInput.value, page);
+ }
+ })
+ );
+
+ await onSearch(event.target.value, page);
+ }
+ }
+ });
+
+ const searchButton = Button({
+ classNames: ['search-button'],
+ name: '๊ฒ์',
+ type: 'button',
+ onClick: async () => {
+ document.getElementById('more-movies').remove();
+ const sectionContainer = document.querySelector('.item-view');
+
+ sectionContainer.appendChild(
+ Button({
+ classNames: ['btn', 'primary', 'full-width'],
+ type: 'button',
+ id: 'more-movies',
+ name: '๋ ๋ณด๊ธฐ',
+ onClick: () => {
+ page += 1;
+ handleSearch(searchInput.value, page);
+ }
+ })
+ );
+
+ const inputValue = document.getElementById('search-input').value;
+ await onSearch(inputValue, page);
+ }
+ });
+
+ const searchBox = headerElement.querySelector('.search-box');
+
+ searchBox.addEventListener('mouseenter', () => {
+ const width = document.querySelector('body').getBoundingClientRect().width;
+
+ if (width <= 390) {
+ const input = document.getElementById('search-input');
+ const logo = document.querySelector('header h1');
+
+ searchBox.classList.add('search-box-toggle');
+ input.classList.add('input-toggle');
+ logo.classList.add('logo-toggle');
+ }
+ });
+
+ searchBox.addEventListener('mouseleave', () => {
+ const width = document.querySelector('body').getBoundingClientRect().width;
+
+ if (width <= 390) {
+ const input = document.getElementById('search-input');
+ const logo = document.querySelector('header h1');
+
+ searchBox.classList.remove('search-box-toggle');
+ input.classList.remove('input-toggle');
+ logo.classList.remove('logo-toggle');
+ }
+ });
+
+ searchBox.appendChild(searchInput);
+ searchBox.appendChild(searchButton);
return headerElement;
} | JavaScript | ์ ๋ณด๋๊น ๋ก์ง์ด ๊ฑฐ์ ์ ์ฌํ๋ค์! |
@@ -0,0 +1,33 @@
+import handleMovie from '../services/handleMovie';
+import Button from './Button';
+
+function MainContent(initialPage) {
+ let page = initialPage;
+ const mainElement = document.createElement('main');
+ mainElement.id = 'main';
+
+ mainElement.innerHTML = `
+ <section class="item-view">
+ <h2>์ง๊ธ ์ธ๊ธฐ ์๋ ์ํ</h2>
+ <ul class="item-list"></ul>
+ </section>
+ `;
+
+ const sectionElement = mainElement.querySelector('.item-view');
+ sectionElement.appendChild(
+ Button({
+ classNames: ['btn', 'primary', 'full-width'],
+ type: 'button',
+ id: 'more-movies',
+ name: '๋ ๋ณด๊ธฐ',
+ onClick: () => {
+ page += 1;
+ handleMovie(page);
+ }
+ })
+ );
+
+ return mainElement;
+}
+
+export default MainContent; | JavaScript | ```suggestion
const mainElement = html(`
<main id="main">
<section class="item-view">
<h2>์ง๊ธ ์ธ๊ธฐ ์๋ ์ํ</h2>
<ul class="item-list"></ul>
</section>
<main>
`);
```
์ด๋ ๊ฒ ํํํ ์ ์๋๋ก ํจ์๋ฅผ ํ๋ ๋ง๋ค์ด์ฃผ์๋ฉด ์ด๋จ๊น์!? |
@@ -0,0 +1,33 @@
+import handleMovie from '../services/handleMovie';
+import Button from './Button';
+
+function MainContent(initialPage) {
+ let page = initialPage;
+ const mainElement = document.createElement('main');
+ mainElement.id = 'main';
+
+ mainElement.innerHTML = `
+ <section class="item-view">
+ <h2>์ง๊ธ ์ธ๊ธฐ ์๋ ์ํ</h2>
+ <ul class="item-list"></ul>
+ </section>
+ `;
+
+ const sectionElement = mainElement.querySelector('.item-view');
+ sectionElement.appendChild(
+ Button({
+ classNames: ['btn', 'primary', 'full-width'],
+ type: 'button',
+ id: 'more-movies',
+ name: '๋ ๋ณด๊ธฐ',
+ onClick: () => {
+ page += 1;
+ handleMovie(page);
+ }
+ })
+ );
+
+ return mainElement;
+}
+
+export default MainContent; | JavaScript | ์์์๋ html template์ผ๋ก ํํ๋๊ณ ์ฌ๊ธฐ์๋ ์ด๋ ๊ฒ ํํ๋๊ณ ์์ด์ ์ฝ๋๋ฅผ ์ฝ๋ ์ฌ๋ ์
์ฅ์์๋ ์กฐ๊ธ ํท๊ฐ๋ฆฌ์ง ์์๊น ์ถ์ด์! |
@@ -0,0 +1,47 @@
+import { BASE_URL } from '../constants/api';
+import { APIError } from '../apis/error';
+
+interface ApiClientType {
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
+ endpoint: string;
+ params?: Record<string, string>;
+ headers?: Record<string, string>;
+ body?: Record<string, any>;
+}
+
+class ApiClient {
+ static async request({ method, endpoint, params, headers = {}, body }: ApiClientType) {
+ const url = params
+ ? this.createUrlSearchParams({ baseUrl: `${BASE_URL}/${endpoint}`, params })
+ : `${BASE_URL}/${endpoint}`;
+
+ const options: RequestInit = {
+ method,
+ headers: {
+ 'Content-Type': 'application/json',
+ ...headers
+ },
+ ...(body && { body: JSON.stringify(body) })
+ };
+
+ try {
+ const response = await fetch(url, options);
+ const data = await response.json();
+
+ if (!response.ok) {
+ throw new APIError(data.status_code, data.status_message);
+ }
+ return data;
+ } catch (error: any) {
+ throw new APIError(400, error.message || 'An unknown error occurred');
+ }
+ }
+
+ static createUrlSearchParams({ baseUrl, params }: { baseUrl: string; params: Record<string, string> }) {
+ const url = new URL(baseUrl);
+ url.search = new URLSearchParams(params).toString();
+ return url.toString();
+ }
+}
+
+export default ApiClient; | TypeScript | ์ํ.. ์ด ํ์ผ ๋๋ฌธ์ tsconfig๊ฐ ์๊ธด๊ฑฐ์๊ตฐ์ |
@@ -0,0 +1,47 @@
+import { BASE_URL } from '../constants/api';
+import { APIError } from '../apis/error';
+
+interface ApiClientType {
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
+ endpoint: string;
+ params?: Record<string, string>;
+ headers?: Record<string, string>;
+ body?: Record<string, any>;
+}
+
+class ApiClient {
+ static async request({ method, endpoint, params, headers = {}, body }: ApiClientType) {
+ const url = params
+ ? this.createUrlSearchParams({ baseUrl: `${BASE_URL}/${endpoint}`, params })
+ : `${BASE_URL}/${endpoint}`;
+
+ const options: RequestInit = {
+ method,
+ headers: {
+ 'Content-Type': 'application/json',
+ ...headers
+ },
+ ...(body && { body: JSON.stringify(body) })
+ };
+
+ try {
+ const response = await fetch(url, options);
+ const data = await response.json();
+
+ if (!response.ok) {
+ throw new APIError(data.status_code, data.status_message);
+ }
+ return data;
+ } catch (error: any) {
+ throw new APIError(400, error.message || 'An unknown error occurred');
+ }
+ }
+
+ static createUrlSearchParams({ baseUrl, params }: { baseUrl: string; params: Record<string, string> }) {
+ const url = new URL(baseUrl);
+ url.search = new URLSearchParams(params).toString();
+ return url.toString();
+ }
+}
+
+export default ApiClient; | TypeScript | try ๊ตฌ๊ฐ์์ ์ค๋ฅ ๋ฐ์ -> catch๋ก ์ง์
-> ๋ฌด์กฐ๊ฑด status๋ 400์ผ๋ก ๊ณ ์ ํ์ฌ ๋ค์ error throw
์ด๋ ๊ฒ ๋ฉ๋๋ค.
์ ์๋ฏธํ ์๋ฌ ํ๋ก์ฐ์ผ๊น์~? |
@@ -0,0 +1,47 @@
+import { BASE_URL } from '../constants/api';
+import { APIError } from '../apis/error';
+
+interface ApiClientType {
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
+ endpoint: string;
+ params?: Record<string, string>;
+ headers?: Record<string, string>;
+ body?: Record<string, any>;
+}
+
+class ApiClient {
+ static async request({ method, endpoint, params, headers = {}, body }: ApiClientType) {
+ const url = params
+ ? this.createUrlSearchParams({ baseUrl: `${BASE_URL}/${endpoint}`, params })
+ : `${BASE_URL}/${endpoint}`;
+
+ const options: RequestInit = {
+ method,
+ headers: {
+ 'Content-Type': 'application/json',
+ ...headers
+ },
+ ...(body && { body: JSON.stringify(body) })
+ };
+
+ try {
+ const response = await fetch(url, options);
+ const data = await response.json();
+
+ if (!response.ok) {
+ throw new APIError(data.status_code, data.status_message);
+ }
+ return data;
+ } catch (error: any) {
+ throw new APIError(400, error.message || 'An unknown error occurred');
+ }
+ }
+
+ static createUrlSearchParams({ baseUrl, params }: { baseUrl: string; params: Record<string, string> }) {
+ const url = new URL(baseUrl);
+ url.search = new URLSearchParams(params).toString();
+ return url.toString();
+ }
+}
+
+export default ApiClient; | TypeScript | ๊ทธ๋ฐ๋ฐ ์ด ํ์ผ์ด ์ domain ์ ํด๋นํ๋๊ฑธ๊น์~?
domain์ด๋ผ๊ณ ํ๊ธฐ์ ๋ง ๊ทธ๋๋ก ์ฌ์ด๋ ์ดํํธ(client)๋ฅผ ํํํ๊ฑฐ๋ผ์... ์ ํ ์ฐ๊ด์ด ์์ด๋ณด์ฌ์! |
@@ -0,0 +1,24 @@
+import getMovieList from '../apis/getMovieList';
+import ConfirmModal from '../components/modal/ConfirmModal';
+import { Modal } from '../components/modal/container/Modal';
+import { NOT_MORE_MOVIES_MESSAGE } from '../constants/message';
+import renderMovies from '../view/renderMovies';
+import renderSkeletonMovies from '../view/renderSkeletonMovies';
+
+export default async function handleMovie(page) {
+ try {
+ renderSkeletonMovies({ loading: true });
+ const movieList = await getMovieList(page);
+ renderSkeletonMovies({ loading: false });
+
+ if (movieList.results.length === 0) {
+ document.getElementById('more-movies').remove();
+ new Modal(document.querySelector('body'), ConfirmModal(NOT_MORE_MOVIES_MESSAGE));
+ }
+
+ renderMovies({ movieList: movieList.results, resetHTML: false });
+ } catch (error) {
+ renderSkeletonMovies({ loading: false });
+ new Modal(document.querySelector('body'), ConfirmModal(error.message));
+ }
+} | JavaScript | api์์ ๋ฐ์ํ ์๋ฌ ์ค์ message๋ง ์ฌ์ฉํ๊ณ ์๋๋ฐ, CustomError๋ฅผ ๋ง๋ค์ด์ ์ฐ๋ ์๋ฏธ๊ฐ ์๋๊ฑด๊ฐ ์ถ์ด์! |
@@ -0,0 +1,47 @@
+import { BASE_URL } from '../constants/api';
+import { APIError } from '../apis/error';
+
+interface ApiClientType {
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
+ endpoint: string;
+ params?: Record<string, string>;
+ headers?: Record<string, string>;
+ body?: Record<string, any>;
+}
+
+class ApiClient {
+ static async request({ method, endpoint, params, headers = {}, body }: ApiClientType) {
+ const url = params
+ ? this.createUrlSearchParams({ baseUrl: `${BASE_URL}/${endpoint}`, params })
+ : `${BASE_URL}/${endpoint}`;
+
+ const options: RequestInit = {
+ method,
+ headers: {
+ 'Content-Type': 'application/json',
+ ...headers
+ },
+ ...(body && { body: JSON.stringify(body) })
+ };
+
+ try {
+ const response = await fetch(url, options);
+ const data = await response.json();
+
+ if (!response.ok) {
+ throw new APIError(data.status_code, data.status_message);
+ }
+ return data;
+ } catch (error: any) {
+ throw new APIError(400, error.message || 'An unknown error occurred');
+ }
+ }
+
+ static createUrlSearchParams({ baseUrl, params }: { baseUrl: string; params: Record<string, string> }) {
+ const url = new URL(baseUrl);
+ url.search = new URLSearchParams(params).toString();
+ return url.toString();
+ }
+}
+
+export default ApiClient; | TypeScript | domain์ด apis๋ฅผ ์ฐธ์กฐํ๋๊ฑด ์ด์ํด๋ณด์ฌ์! |
@@ -1,2 +1 @@
export const BASE_URL = 'https://api.themoviedb.org';
-export const IMAGE_BASE_URL = 'https://image.tmdb.org/t/p/w500'; | JavaScript | ์ด๋ฐ ์ ๋ณด๋ ์์ API๋ฅผ ์์ฒญํ๋ ์ชฝ์์ ๊ฐ์ง๊ณ ์์ผ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1 @@
+export const NOT_MORE_MOVIES_MESSAGE = '๋์ด์ ์ํ๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.'; | JavaScript | ์ด๋ฐ ์๋ฌ ๋ฉ์ธ์ง๋ ๋ฉ์ธ์ง๋ฅผ ์ฌ์ฉํ๋ ์ชฝ์์ ๊ฐ์ง๊ณ ์์ผ๋ฉด ์ด๋จ๊น์?
๋ค๋ฅธ ๊ตฌ๊ฐ์์๋ ์์ ์ฌ์ฉ๋ ๊ฒ ๊ฐ์ง ์์์์! |
@@ -0,0 +1,40 @@
+package org.sopt.androidseminar1
+
+import android.content.Intent
+import androidx.appcompat.app.AppCompatActivity
+import android.os.Bundle
+import android.widget.Toast
+import org.sopt.androidseminar1.databinding.SignInBinding
+
+class SignInActivity: AppCompatActivity() {
+ private lateinit var binding: SignInBinding // ๊ณ ์ ์ ์ธ Class ๊ฐ์ ์๋๋ค.
+ // activity_main -> ActivityMain
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ binding = SignInBinding.inflate(layoutInflater) // layoutInflater : xml์ ๋ถ๋ฌ์ฃผ๋ ์ ๋
+ setContentView(binding.root) // setContentView : xml์ ๊ทธ๋ ค์ฃผ๋ ํจ์
+
+
+ // intent ๋ณ๊ฒฝ
+ val goSignup: Intent = Intent(this, SignUpActivity::class.java)
+ val goHomeActivity: Intent = Intent(this, HomeActivity::class.java )
+
+ // ๋ก๊ทธ์ธ ๋ฒํผ
+ binding.btnLogin.setOnClickListener {
+ if (binding.etId.text.toString().isEmpty() || binding.etPw.text.toString().isEmpty()){
+ Toast.makeText(this, "๋ก๊ทธ์ธ ์คํจ", Toast.LENGTH_SHORT).show()
+ }
+ else {
+ startActivity(goHomeActivity)
+ }
+ }
+
+ // ํ์๊ฐ์
+ binding.btnSignup.setOnClickListener{
+ startActivity(goSignup)
+ }
+
+ setContentView(binding.root)
+ }
+}
\ No newline at end of file | Kotlin | ์ฝํ๋ฆฐ์์๋ ํ์
์ถ๋ก ๊ธฐ๋ฅ์ ์ง์ํด์ฃผ๊ธฐ๋๋ฌธ์
```suggestion
val goSignup = Intent(this, SignUpActivity::class.java)
val goHomeActivity = Intent(this, HomeActivity::class.java )
```
์ด๋ ๊ฒ๋ง ์์ฑํ์
๋ ๊ด์ฐฎ์ต๋๋น~:) |
@@ -0,0 +1,40 @@
+package org.sopt.androidseminar1
+
+import android.content.Intent
+import androidx.appcompat.app.AppCompatActivity
+import android.os.Bundle
+import android.widget.Toast
+import org.sopt.androidseminar1.databinding.SignInBinding
+
+class SignInActivity: AppCompatActivity() {
+ private lateinit var binding: SignInBinding // ๊ณ ์ ์ ์ธ Class ๊ฐ์ ์๋๋ค.
+ // activity_main -> ActivityMain
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ binding = SignInBinding.inflate(layoutInflater) // layoutInflater : xml์ ๋ถ๋ฌ์ฃผ๋ ์ ๋
+ setContentView(binding.root) // setContentView : xml์ ๊ทธ๋ ค์ฃผ๋ ํจ์
+
+
+ // intent ๋ณ๊ฒฝ
+ val goSignup: Intent = Intent(this, SignUpActivity::class.java)
+ val goHomeActivity: Intent = Intent(this, HomeActivity::class.java )
+
+ // ๋ก๊ทธ์ธ ๋ฒํผ
+ binding.btnLogin.setOnClickListener {
+ if (binding.etId.text.toString().isEmpty() || binding.etPw.text.toString().isEmpty()){
+ Toast.makeText(this, "๋ก๊ทธ์ธ ์คํจ", Toast.LENGTH_SHORT).show()
+ }
+ else {
+ startActivity(goHomeActivity)
+ }
+ }
+
+ // ํ์๊ฐ์
+ binding.btnSignup.setOnClickListener{
+ startActivity(goSignup)
+ }
+
+ setContentView(binding.root)
+ }
+}
\ No newline at end of file | Kotlin | ์ฌ๊ธฐ์ toString()์ ๊ฒฝ์ฐ ๋นผ์ฃผ์
๋ ๊ด์ฐฎ์ต๋๋ค~!! |
@@ -0,0 +1,40 @@
+package org.sopt.androidseminar1
+
+import android.content.Intent
+import androidx.appcompat.app.AppCompatActivity
+import android.os.Bundle
+import android.widget.Toast
+import org.sopt.androidseminar1.databinding.SignInBinding
+
+class SignInActivity: AppCompatActivity() {
+ private lateinit var binding: SignInBinding // ๊ณ ์ ์ ์ธ Class ๊ฐ์ ์๋๋ค.
+ // activity_main -> ActivityMain
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ binding = SignInBinding.inflate(layoutInflater) // layoutInflater : xml์ ๋ถ๋ฌ์ฃผ๋ ์ ๋
+ setContentView(binding.root) // setContentView : xml์ ๊ทธ๋ ค์ฃผ๋ ํจ์
+
+
+ // intent ๋ณ๊ฒฝ
+ val goSignup: Intent = Intent(this, SignUpActivity::class.java)
+ val goHomeActivity: Intent = Intent(this, HomeActivity::class.java )
+
+ // ๋ก๊ทธ์ธ ๋ฒํผ
+ binding.btnLogin.setOnClickListener {
+ if (binding.etId.text.toString().isEmpty() || binding.etPw.text.toString().isEmpty()){
+ Toast.makeText(this, "๋ก๊ทธ์ธ ์คํจ", Toast.LENGTH_SHORT).show()
+ }
+ else {
+ startActivity(goHomeActivity)
+ }
+ }
+
+ // ํ์๊ฐ์
+ binding.btnSignup.setOnClickListener{
+ startActivity(goSignup)
+ }
+
+ setContentView(binding.root)
+ }
+}
\ No newline at end of file | Kotlin | ํ์ฌ setContentView ํจ์๊ฐ ๋๋ฒ ํธ์ถ๋๊ณ ์๋๋ฐ ํ๋๋ ์ง์์ฃผ์
๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์์~!! |
@@ -0,0 +1,40 @@
+package org.sopt.androidseminar1
+
+import android.content.Intent
+import androidx.appcompat.app.AppCompatActivity
+import android.os.Bundle
+import android.widget.Toast
+import org.sopt.androidseminar1.databinding.SignInBinding
+
+class SignInActivity: AppCompatActivity() {
+ private lateinit var binding: SignInBinding // ๊ณ ์ ์ ์ธ Class ๊ฐ์ ์๋๋ค.
+ // activity_main -> ActivityMain
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ binding = SignInBinding.inflate(layoutInflater) // layoutInflater : xml์ ๋ถ๋ฌ์ฃผ๋ ์ ๋
+ setContentView(binding.root) // setContentView : xml์ ๊ทธ๋ ค์ฃผ๋ ํจ์
+
+
+ // intent ๋ณ๊ฒฝ
+ val goSignup: Intent = Intent(this, SignUpActivity::class.java)
+ val goHomeActivity: Intent = Intent(this, HomeActivity::class.java )
+
+ // ๋ก๊ทธ์ธ ๋ฒํผ
+ binding.btnLogin.setOnClickListener {
+ if (binding.etId.text.toString().isEmpty() || binding.etPw.text.toString().isEmpty()){
+ Toast.makeText(this, "๋ก๊ทธ์ธ ์คํจ", Toast.LENGTH_SHORT).show()
+ }
+ else {
+ startActivity(goHomeActivity)
+ }
+ }
+
+ // ํ์๊ฐ์
+ binding.btnSignup.setOnClickListener{
+ startActivity(goSignup)
+ }
+
+ setContentView(binding.root)
+ }
+}
\ No newline at end of file | Kotlin | ๋ชจ๋ ๋ก์ง์ ๋ค onCreate์ ๋ฃ๊ธฐ๋ณด๋ค๋ ๊ฐ๋
์ฑ, ์ฌ์ฌ์ฉ์ฑ ์ธก๋ฉด์ ๊ณ ๋ คํด์ ํจ์ํ๋ฅผ ํด์ฃผ์๋ ๊ฒ ์ข์ ๊ฒ ๊ฐ์์~!! |
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+
+ <corners android:radius="5dip"/>
+
+ <stroke android:width="2dp"
+ android:color="#F658A6"/>
+ <solid android:color="#F658A6"/>
+ <corners
+ android:topLeftRadius="12dp"
+ android:topRightRadius="12dp"
+ android:bottomLeftRadius="12dp"
+ android:bottomRightRadius="12dp"/>
+</shape>
\ No newline at end of file | Unknown | dip๋ dp์ ๊ฐ์ ๊ฐ๋
์
๋๋ค! ์์น๋ฅผ dp๋ก ๋ฐ๊ฟ์ฃผ๋๊ฒ ์ข์ ๊ฒ ๊ฐ์์~!!! |
@@ -0,0 +1,126 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:app="http://schemas.android.com/apk/res-auto"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ tools:context=".HomeActivity">
+
+ <TextView
+ android:id="@+id/textView10"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="70dp"
+ android:text="@string/sopthub"
+ android:textColor="#F658A6"
+ android:textSize="40sp"
+ android:textStyle="bold"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toTopOf="parent" />
+
+
+ <TextView
+ android:id="@+id/textView11"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="20dp"
+ android:layout_marginTop="40dp"
+ android:text="@string/kimdaeho"
+ android:textColor="@color/black"
+ android:textSize="15sp"
+ android:textStyle="bold"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintHorizontal_bias="0.059"
+ app:layout_constraintStart_toEndOf="@+id/textView15"
+ app:layout_constraintTop_toBottomOf="@+id/imageView2" />
+
+ <TextView
+ android:id="@+id/textView12"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/old"
+ android:textColor="@color/black"
+ android:textSize="15sp"
+ android:textStyle="bold"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintHorizontal_bias="0.193"
+ app:layout_constraintStart_toEndOf="@+id/textView16"
+ app:layout_constraintTop_toTopOf="@+id/textView16" />
+
+ <TextView
+ android:id="@+id/textView14"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="60dp"
+ android:text="@string/introduce"
+ android:textColor="@color/black"
+ android:textSize="15sp"
+ android:textStyle="bold"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintHorizontal_bias="0.453"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toBottomOf="@+id/textView13" />
+
+ <TextView
+ android:id="@+id/textView13"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/entj"
+ android:textColor="@color/black"
+ android:textSize="15sp"
+ android:textStyle="bold"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintHorizontal_bias="0.154"
+ app:layout_constraintStart_toEndOf="@+id/textView17"
+ app:layout_constraintTop_toTopOf="@+id/textView17" />
+
+ <ImageView
+ android:id="@+id/imageView2"
+ android:layout_width="119dp"
+ android:layout_height="147dp"
+ android:layout_marginTop="30dp"
+ app:layout_constraintEnd_toEndOf="@+id/textView10"
+ app:layout_constraintHorizontal_bias="0.491"
+ app:layout_constraintStart_toStartOf="@+id/textView10"
+ app:layout_constraintTop_toBottomOf="@+id/textView10"
+ app:srcCompat="@drawable/kimdaeho" />
+
+ <TextView
+ android:id="@+id/textView15"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="150dp"
+ android:text="@string/name"
+ android:textColor="@color/black"
+ android:textSize="15sp"
+ android:textStyle="bold"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toTopOf="@+id/textView11" />
+
+ <TextView
+ android:id="@+id/textView16"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="40dp"
+ android:text="@string/age"
+ android:textColor="@color/black"
+ android:textSize="15sp"
+ android:textStyle="bold"
+ app:layout_constraintStart_toStartOf="@+id/textView15"
+ app:layout_constraintTop_toBottomOf="@+id/textView15" />
+
+ <TextView
+ android:id="@+id/textView17"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="40dp"
+ android:text="@string/mbti"
+ android:textColor="@color/black"
+ android:textSize="15sp"
+ android:textStyle="bold"
+ app:layout_constraintStart_toStartOf="@+id/textView16"
+ app:layout_constraintTop_toBottomOf="@+id/textView16" />
+
+
+</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file | Unknown | View ์ปดํฌ๋ํธ์ id ๊ฐ์ ๋ค์ด๋ฐ์ ์ข ๋ ๊ธฐ๋ฅ์ ๋ง๊ฒ~~ ๊ตฌ์ฒด์ ์ผ๋ก ๋ค์ด๋ฐ ํ๋ ๊ฒ ์ข์ ๊ฒ ๊ฐ์์~!! |
@@ -0,0 +1,134 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:app="http://schemas.android.com/apk/res-auto"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ tools:context=".SignInActivity">
+
+ <!--
+ text size -> sp
+ hardcoded text -> @values ํด๋๋ก text ์ฎ๊ธฐ๊ธฐ
+ @values ์๋ค๊ฐ color ์ฎ๊ธฐ๊ธฐ
+ amulator ์ action bar ์ ๊ฑฐ
+ ์คํ
์ด๋์ค๋ฐ ์๊น ๋ณ๊ฒฝํด์ผํจ -> @themas-->
+ <TextView
+ android:id="@+id/textView"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="70dp"
+ android:text="@string/sopthub"
+ android:textColor="@color/pink"
+ android:textSize="40sp"
+ android:textStyle="bold"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toTopOf="parent" />
+
+ <TextView
+ android:id="@+id/textView2"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="40dp"
+ android:layout_marginTop="40dp"
+ android:text="@string/id"
+ android:textColor="@color/black"
+ android:textSize="20sp"
+ android:textStyle="bold"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toBottomOf="@+id/textView4" />
+
+ <EditText
+ android:id="@+id/et_id"
+ android:layout_width="0dp"
+ android:layout_height="50dp"
+ android:layout_marginTop="8dp"
+ android:layout_marginEnd="40dp"
+ android:background="@drawable/round_blank_edge"
+ android:ems="10"
+ android:inputType="textPersonName"
+ android:paddingStart="15dp"
+ android:hint="@string/input_id"
+ android:textColor="@color/gray"
+ android:textSize="15sp"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintHorizontal_bias="0.0"
+ app:layout_constraintStart_toStartOf="@+id/textView2"
+ app:layout_constraintTop_toBottomOf="@+id/textView2" />
+
+ <TextView
+ android:id="@+id/textView3"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="20dp"
+ android:text="@string/password"
+ android:textColor="@color/black"
+ android:textSize="20sp"
+ android:textStyle="bold"
+ app:layout_constraintStart_toStartOf="@+id/et_id"
+ app:layout_constraintTop_toBottomOf="@+id/et_id" />
+
+ <EditText
+ android:id="@+id/et_pw"
+ android:layout_width="0dp"
+ android:layout_height="50dp"
+ android:layout_marginTop="10dp"
+ android:layout_marginEnd="40dp"
+ android:background="@drawable/round_blank_edge"
+ android:ems="10"
+ android:inputType="textPassword"
+ android:minHeight="48dp"
+ android:paddingStart="15dp"
+ android:hint="@string/input_password"
+ android:textColor="@color/gray"
+ android:textSize="15sp"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="@+id/textView3"
+ app:layout_constraintTop_toBottomOf="@+id/textView3" />
+
+ <Button
+ android:id="@+id/btn_login"
+ android:layout_width="0dp"
+ android:layout_height="50dp"
+ android:layout_marginBottom="120dp"
+ android:background="@drawable/round_full_edge"
+ android:text="@string/login"
+ android:textColor="@color/white"
+ android:textSize="15sp"
+ android:textStyle="bold"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintEnd_toEndOf="@+id/et_pw"
+ app:layout_constraintHorizontal_bias="1.0"
+ app:layout_constraintStart_toStartOf="@+id/et_pw"
+ app:layout_constraintTop_toBottomOf="@+id/et_pw" />
+
+
+ <Button
+ android:id="@+id/btn_signup"
+ android:layout_width="0dp"
+ android:layout_height="50dp"
+ android:background="@color/white"
+ android:text="@string/signup"
+ android:textColor="@color/gray"
+ android:textSize="15sp"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintEnd_toEndOf="@+id/btn_login"
+ app:layout_constraintStart_toStartOf="@+id/btn_login"
+ app:layout_constraintTop_toBottomOf="@+id/btn_login"
+ app:layout_constraintVertical_bias="0.0" />
+
+ <TextView
+ android:id="@+id/textView4"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="10dp"
+ android:text="@string/login"
+ android:textColor="@color/black"
+ android:textSize="25sp"
+ android:textStyle="bold"
+ app:layout_constraintEnd_toEndOf="@+id/textView"
+ app:layout_constraintHorizontal_bias="0.486"
+ app:layout_constraintStart_toStartOf="@+id/textView"
+ app:layout_constraintTop_toBottomOf="@+id/textView" />
+
+</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file | Unknown | ๊ณ ์ dp๋ฅผ ๋์ด๋ ๋๋น๊ฐ์ผ๋ก ์ค ๊ฒฝ์ฐ ํด๋ํฐ ๊ธฐ์ข
์ ๋ฐ๋ผ ๋ด์ฉ์ด ์๋ฆด์๋์๊ณ , ๋ชจ์์ด ์ด์ํ๊ฒ ๋์ฌ ์๋ ์์ต๋๋ค~ ํน์ ๊ณ ์ dp๋ฅผ ๋์ด ๊ฐ์ผ๋ก ์ฃผ์ ํน๋ณํ ์ด์ ๊ฐ ์์๊น์~๐๐ |
@@ -0,0 +1,5 @@
+package model;
+
+public interface State {
+ int execute(int length);
+} | Java | ํ์ฅ ๊ฐ๋ฅ์ฑ์ ์๊ฐํ์ฌ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์๋ค์!! ๊ฐ ๋ค๋ฅธ ์ํ๋ง๋ค ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ Car์์ ๊ฐ์๋ผ์ฐ๋ ๊ฒ๋ ์ ์ ํฉ๋๋ค!! State ํจํด ์ข์ ๊ฒ ๋ฐฐ์๊ฐ๋๋ค ๐ |
@@ -0,0 +1,13 @@
+package function;
+
+import java.util.Random;
+
+public class RandomNumberMaker {
+
+ private static final Random random = new Random();
+ private static final int RANDOM_NUMBER_RANGE = 10;
+
+ public static int getRandomNumber() {
+ return random.nextInt(RANDOM_NUMBER_RANGE);
+ }
+} | Java | getRandomNumber ๊ฐ ํธ์ถ๋ ๋๋ง๋ค ์๋ก์ด Random๊ฐ์ฒด๊ฐ ๋ง๋ค์ด์ง์ง ์์๋ ๋ ๊ฒ ๊ฐ์์!!
๊ทธ๋ฆฌ๊ณ Random๊ฐ์boundary๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ ์ด์ ๊ฐ ์์๊น์?? |
@@ -2,22 +2,28 @@
public class Car{
- private String name;
- private int position = 0;
+ long number;
+ int length;
+ State state;
- public Car(String name){
- this.name = name;
+ public Car(long number) {
+ this.number = number;
+ length = 0;
}
- public String getName() {
- return name;
+ public int moveCount() {
+ return length;
}
- public int getPosition(){
- return position;
+ public long getCarNum() {
+ return number;
}
- public void move(){
- position++;
+ public void changeState(State cmd) {
+ state = cmd;
+ }
+
+ public void move() {
+ length = state.execute(length);
}
}
\ No newline at end of file | Java | number๋ฅผ Car๋ฅผ ์๋ณํด์ฃผ๋ ๋ฉค๋ฒ๋ก ์ฌ์ฉํ๊ณ ์๋ ๊ฒ ๊ฐ์์!! ์ ๊ทผ์ง์ ์์ final ํค์๋๋ฅผ ๋ถ์ฌ์ฃผ๋ ๊ฒ์ ๋ํด ์ด๋ป๊ฒ ์๊ฐํ์ค๊น์? |
@@ -1,21 +1,18 @@
package view;
-import controller.RacingGame;
import model.Car;
-import java.util.Collections;
import java.util.List;
public class ResultView {
- public static void printTrialResult(List<Car> cars){
- for (Car car : cars){
- System.out.print(car.getName() + " : " + String.join("", Collections.nCopies(car.getPosition(), "*")) + "\n");
+ private static final String RESULT_SYMBOL = "-";
+
+ public static void printAllCarsState(long id, int len) {
+ System.out.println("car" + id);
+ for (int i = 0; i < len; i++) {
+ System.out.print(RESULT_SYMBOL);
}
System.out.println();
}
-
- public static void printFinalWinner(RacingGame racingGame, List<Car> cars){
- System.out.print(String.join(",", racingGame.searchWinners(cars)) + "๊ฐ ์ต์ข
์ฐ์นํ์ต๋๋ค.");
- }
}
\ No newline at end of file | Java | ํ์ฌ ์ถ๋ ฅํ๋ ์ฉ๋๋ก ์ง๊ธ --- ์ด๋ ๊ฒ ๋ณด์ฌ์ฃผ๊ณ ์์ง๋ง, ๋์ค์๋ ๋ค๋ฅธ ๊ธฐํธ๋ก ๋ฐ๋์๋ ์์ ๊ฒ ๊ฐ์์!! ์ด ๊ธฐํธ๋ค ๋ฟ๋ง ์๋๋ผ InputView์ ResultView์ ์
์ถ๋ ฅ ๋ฉ์ธ์ง, ๋งค์ง๋๋ฒ๋ค์ ์์ํ ํด์ฃผ๋ฉด ์ข์ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,38 @@
+package controller;
+
+import function.RandomNumberMaker;
+import model.Car;
+import model.State;
+import model.Movable;
+import model.UnMovable;
+
+import java.util.List;
+
+public class CarController {
+
+ private static final int MOVE_CONDITION = 4;
+
+ public void registerCars(List<Car> cars, long len) {
+ for (int i = 0; i < len; i++) {
+ cars.add(new Car(i));
+ }
+ }
+
+ public void moveCar(List<Car> cars) {
+ for (Car car : cars) {
+ car.changeState(checkMoveOrStop(car));
+ car.move();
+ }
+ }
+
+ private State checkMoveOrStop(Car car) {
+ if (getRandomNumber() >= MOVE_CONDITION) {
+ return new Movable();
+ }
+ return new UnMovable();
+ }
+
+ private int getRandomNumber() {
+ return RandomNumberMaker.getRandomNumber();
+ }
+} | Java | ์ ์ฒด์ ์ผ๋ก Car๋ค์ ๊ด๋ฆฌํด์ฃผ๋ ์ญํ ์ ํ๊ณ ์๋ ๊ฒ ๊ฐ์์!
์ ๊ฐ ์๊ฐํ๋ Controller์ ์ญํ ๊ณผ๋ ์กฐ๊ธ ๋ค๋ฅด๋ค๊ณ ์๊ฐ์ด ๋ญ๋๋ค.
์ ๊ฐ ์๊ฐํ๋ Controller์ ์ญํ ์ ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ์์ฒญ๊ฐ์ ๋ฐ์, ์์ฒญ๊ฐ์ ๊ฐ์ง๊ณ ๋น์ฆ๋์ค ๋ก์ง์ ์คํํ๊ณ ํ์ํ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณตํด์ View์๊ฒ ๋ณด๋ด์ฃผ๋ ์ญํ ์ด๋ผ๊ณ ์๊ฐ์ ํฉ๋๋ค.
์ ๋ CarController๋ณด๋ค Cars๊ฐ์ ์ด๋ฆ์ด ๋ ์ด์ธ๋ฆฐ๋ค๊ณ ์๊ฐ์ด ๋๋๋ฐ ํ์ฐ๋์ ์ด๋ป๊ฒ ์๊ฐํ์ค๊น์??
์ด ์๋ฃ ์ฐธ๊ณ ํ์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค!
https://tecoble.techcourse.co.kr/post/2020-05-08-First-Class-Collection/ |
@@ -0,0 +1,47 @@
+package engine;
+
+import controller.CarController;
+import model.Car;
+import view.InputView;
+import view.ResultView;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class RacingGame {
+
+ private List<Car> cars;
+ private CarController carController;
+ private InputView inputView;
+
+ public RacingGame() {
+ cars = new ArrayList<>();
+ carController = new CarController();
+ inputView = new InputView();
+ }
+
+ public void run() {
+ long carNum = inputView.inputTotalCarNum();
+ int trial = inputView.inputTotalTrial();
+
+ register(carNum);
+ executeGame(trial);
+ printAll(cars);
+ }
+
+ private void executeGame(int trial) {
+ for (int i = 0; i < trial; i++) {
+ carController.moveCar(cars);
+ }
+ }
+
+ private void register(long carNum) {
+ carController.registerCars(cars, carNum);
+ }
+
+ private void printAll(List<Car> cars) {
+ for (Car car : cars) {
+ ResultView.printAllCarsState(car.getCarNum(), car.moveCount());
+ }
+ }
+} | Java | InputView๋ ์ธ์คํด์ค๋ก ๊ด๋ฆฌํ๊ณ ResultView๋ ์ ํธ๋ฆฌํฐ ํด๋์ค๋ก ์ ์ธํ ์ด์ ๊ฐ ๊ถ๊ธํ๋ค์!! ์ ๋ View๋ฅผ ๊ตฌํ์ ํ ๋, ๊ณ ๋ฏผ์ด ๋ง์ด ๋๋๋ฐ์! View ๊ฐ์ ๊ฒฝ์ฐ์ ์ํ๊ฐ ์์ด์ ์ ํธ๋ฆฌํฐ ํด๋์ค๋ก ๊ตฌํ์ ํด๋ ๋ ๊ฒ ๊ฐ์ง๋ง, ์ ํธ์ฑ ํด๋์ค์๋ ์๊น์ด ์กฐ๊ธ ๋ค๋ฅธ ๋๋๋ ์์ด์์!! ์ ๋ต์ ์๊ฒ ์ง๋ง, ์ ๋ ์ด๋ฒ์ View ๊ตฌํ์ ํ ๋ ์ฑ๊ธํด ํจํด์ ์ฌ์ฉํด๋ณด์์ด์!! ํ์ฐ๋์ด InputView์ OutputView๋ฅผ ๋ค๋ฅด๊ฒ ๊ตฌํํ์ ์ด ๊ถ๊ธํ๋ค์! |
@@ -0,0 +1,38 @@
+package controller;
+
+import function.RandomNumberMaker;
+import model.Car;
+import model.State;
+import model.Movable;
+import model.UnMovable;
+
+import java.util.List;
+
+public class CarController {
+
+ private static final int MOVE_CONDITION = 4;
+
+ public void registerCars(List<Car> cars, long len) {
+ for (int i = 0; i < len; i++) {
+ cars.add(new Car(i));
+ }
+ }
+
+ public void moveCar(List<Car> cars) {
+ for (Car car : cars) {
+ car.changeState(checkMoveOrStop(car));
+ car.move();
+ }
+ }
+
+ private State checkMoveOrStop(Car car) {
+ if (getRandomNumber() >= MOVE_CONDITION) {
+ return new Movable();
+ }
+ return new UnMovable();
+ }
+
+ private int getRandomNumber() {
+ return RandomNumberMaker.getRandomNumber();
+ }
+} | Java | ๋๋ค๊ฐ ๋๋ฌธ์ checkMoveOrStop ๋ฉ์๋๋ฅผ ํ
์คํธ ์ฝ๋ ์์ฑํ๊ธฐ ์ด๋ ค์ ์ ๊ฑฐ์์!!
https://tecoble.techcourse.co.kr/post/2020-05-07-appropriate_method_for_test_by_parameter/
https://tecoble.techcourse.co.kr/post/2020-05-17-appropriate_method_for_test_by_interface/
์๋ฃ ์ฐธ๊ณ ํ์๋ฉด ์ข์ ๊ฒ ๊ฐ์์!! ํ์ง๋ง ์ธํฐํ์ด์ค๋ฅผ ๋ถ๋ฆฌํ๋๊ฒ ๋ฌด์กฐ๊ฑด ์ข์ ๋ฐฉ๋ฒ์ ์๋๋ผ๊ณ ์๊ฐ์ด ๋ญ๋๋ค!! ์์ฌํ๋ฉด์ ๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์!! ํ์ฐ๋์ ์๊ฐ์ด ๋ค๋ฅด์ค ์ ๋ ์๊ตฌ์! |
@@ -0,0 +1,13 @@
+package function;
+
+import java.util.Random;
+
+public class RandomNumberMaker {
+
+ private static final Random random = new Random();
+ private static final int RANDOM_NUMBER_RANGE = 10;
+
+ public static int getRandomNumber() {
+ return random.nextInt(RANDOM_NUMBER_RANGE);
+ }
+} | Java | 1. getRandomNumber ๊ฐ ํธ์ถ๋ ๋๋ง๋ค ์๋ก์ด Random๊ฐ์ฒด๊ฐ ๋ง๋ค์ด์ง์ง ์์๋ ๋ ๊ฒ ๊ฐ์์!!
๋๊ฑด๋ ๋ง์๋๋ก getRandomNumber๋ฅผ ํธ์ถํ ๋๋ง๋ค Random ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ง ์๋ ๊ฒ์ด ์ข๋ค๊ณ ์๊ฐ์ด ๋ญ๋๋ค
Random๊ฐ์ฒด๋ฅผ ์ฑ๊ธํค ํจํด์ ์ด์ฉํ์ฌ ๊ตฌํํ๋ฉด ๋ฆฌ์์ค ๋ถ๋ถ์์ ๋ญ๋น๋ฅผ ์ค์ผ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค!! :)
2. Random๊ฐ์ boundary๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ ์ด์ ๊ฐ ์์๊น์??
์ ๊ฐ ์๊ฐํ๊ธฐ์๋ ๋์ค์ 10์์ ๋ค๋ฅธ ์ซ์๋ก ๋ฐ๊พธ๊ณ ์ถ์ "๋ณ๊ฒฝ ์ฌํญ"์ ์๊ฐํด๋ดค์ต๋๋ค.
์ฐจ๋ฅผ ์์ง์ด๊ฒ ํ๋ "CarController"์์ ์์ ํด์ฃผ๋ ๊ฒ์ด ์ข๋ค๊ณ ์๊ฐํ์ต๋๋ค. |
@@ -0,0 +1,13 @@
+package function;
+
+import java.util.Random;
+
+public class RandomNumberMaker {
+
+ private static final Random random = new Random();
+ private static final int RANDOM_NUMBER_RANGE = 10;
+
+ public static int getRandomNumber() {
+ return random.nextInt(RANDOM_NUMBER_RANGE);
+ }
+} | Java | ```java
private static Random random = new Random();
private static final int RANDOM_BOUNDARY = 10;
public static int getRandomNumber() {
return random.nextInt(RANDOM_BOUNDARY);
}
```
์ด๋ผ๊ณ ํด์ค ์ ์์ ๊ฒ ๊ฐ์์์!!
๋๋ค ๊ฐ์ ์์ฑํด์ฃผ๋ ์ญํ ์ ํ๋ ๊ฒ ๊ฐ์๋ฐ, ๋๋ค๊ฐ์ ๋ฒ์๋ ๋๋ค๊ฐ์ด ์์ฑํด์ฃผ๋ ๊ณณ์์ ์์์ผ ํ์ง ์์๊น ์ถ๋ค์! |
@@ -0,0 +1,13 @@
+package function;
+
+import java.util.Random;
+
+public class RandomNumberMaker {
+
+ private static final Random random = new Random();
+ private static final int RANDOM_NUMBER_RANGE = 10;
+
+ public static int getRandomNumber() {
+ return random.nextInt(RANDOM_NUMBER_RANGE);
+ }
+} | Java | "๋๋ค๊ฐ์ ๋ฒ์๋ ๋๋ค๊ฐ์ด ์์ฑํด์ฃผ๋ ๊ณณ์์ ์์์ผ ํ์ง ์์๊น ์ถ๋ค์"
์ด ๋ถ๋ถ์ด ๋ง๋ ๊ฒ ๊ฐ์ต๋๋ค. ์๊ฐํด๋ณด๋ CarController๊ฐ ๋๋ค๊ฐ์ ๋ฒ์๋ฅผ ์ ํ๋ค๋ฉด ๊ฒฐ๊ตญ Random๊ฐ์ ์์ฑํด์ฃผ๋ ๊ณณ์ ์์กดํ๊ฒ ๋์ด ๊ฒฐํฉ๋๊ฐ ๋์์ง๊ฒ ๋ค์!!
๊ฒฐํฉ๋๋ฅผ ์ค์ด๋ ๋ฐฉ๋ฒ ๋๋ฌด ์ข์ต๋๋ค ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,5 @@
+package model;
+
+public interface State {
+ int execute(int length);
+} | Java | ๊ฐ์ฌํฉ๋๋ค :) |
@@ -1,16 +1,54 @@
package view;
+import java.io.InputStream;
+import java.util.InputMismatchException;
import java.util.Scanner;
-public class InputView{
+public class InputView {
- public static String[] inputCarName(Scanner scanner){
- System.out.println("๊ฒฝ์ฃผํ ์ฐจ์ ์ด๋ฆ์ ์
๋ ฅํ์ธ์. (์ด๋ฆ์ ์ผํ(,) ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถ)");
- return scanner.nextLine().split(",");
+ Scanner sc;
+ private static final String CARS_COUNT = "์๋์ฐจ ๋์๋ ๋ช ๋ ์ธ๊ฐ์?";
+ private static final String PUT_NATURAL_NUMBER = "์์๊ฐ ์๋ ์์ฐ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์";
+ private static final String TRIAL_COUNT = "์๋ํ ํ์๋ ๋ช ํ ์ธ๊ฐ์?";
+ private static final String DO_NOT_PUT_STRING = "๋ฌธ์์ด์ด ์๋ ์์ฐ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์";
+
+ public InputView() {
+ sc = new Scanner(System.in);
+ }
+
+ public long inputTotalCarNum() {
+ System.out.println(CARS_COUNT);
+ long carCount = 0L;
+ try {
+ carCount = Long.parseLong(sc.nextLine());
+ if (carCount <= 0) {
+ throw new IllegalArgumentException();
+ }
+ } catch (IllegalArgumentException exception) {
+ System.out.println(PUT_NATURAL_NUMBER);
+ return inputTotalCarNum();
+ } catch (InputMismatchException exception) {
+ System.out.println(DO_NOT_PUT_STRING);
+ return inputTotalCarNum();
+ }
+ return carCount;
}
- public static int inputNumberOfTrials(Scanner scanner){
- System.out.println("์๋ํ ํ์๋ ๋ชํ์ธ๊ฐ์?");
- return scanner.nextInt();
+ public int inputTotalTrial() {
+ System.out.println(TRIAL_COUNT);
+ int trial = 0;
+ try {
+ trial = Integer.parseInt(sc.nextLine());
+ if (trial <= 0) {
+ throw new IllegalArgumentException();
+ }
+ } catch (IllegalArgumentException exception) {
+ System.out.println(PUT_NATURAL_NUMBER);
+ return inputTotalTrial();
+ } catch (InputMismatchException exception) {
+ System.out.println(DO_NOT_PUT_STRING);
+ return inputTotalTrial();
+ }
+ return trial;
}
-}
\ No newline at end of file
+} | Java | ์์๊ฐ ์
๋ ฅ๋์ ๊ฒฝ์ฐ์ ์์ธ๊ฐ ๋์ ธ์ง๋๋ก ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์
จ๋ค์! ์์์ผ ๊ฒฝ์ฐ ๋ง๊ณ ๋ ์ซ์๊ฐ ์๋ ๊ฐ์ด ์
๋ ฅ ๋์ ๊ฒฝ์ฐ๋ ์์ธ๊ฐ ๋์ ธ์ง๋๋ก ๋ช
์์ ์ผ๋ก ์ฒ๋ฆฌํด์ฃผ๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -1,16 +1,54 @@
package view;
+import java.io.InputStream;
+import java.util.InputMismatchException;
import java.util.Scanner;
-public class InputView{
+public class InputView {
- public static String[] inputCarName(Scanner scanner){
- System.out.println("๊ฒฝ์ฃผํ ์ฐจ์ ์ด๋ฆ์ ์
๋ ฅํ์ธ์. (์ด๋ฆ์ ์ผํ(,) ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถ)");
- return scanner.nextLine().split(",");
+ Scanner sc;
+ private static final String CARS_COUNT = "์๋์ฐจ ๋์๋ ๋ช ๋ ์ธ๊ฐ์?";
+ private static final String PUT_NATURAL_NUMBER = "์์๊ฐ ์๋ ์์ฐ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์";
+ private static final String TRIAL_COUNT = "์๋ํ ํ์๋ ๋ช ํ ์ธ๊ฐ์?";
+ private static final String DO_NOT_PUT_STRING = "๋ฌธ์์ด์ด ์๋ ์์ฐ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์";
+
+ public InputView() {
+ sc = new Scanner(System.in);
+ }
+
+ public long inputTotalCarNum() {
+ System.out.println(CARS_COUNT);
+ long carCount = 0L;
+ try {
+ carCount = Long.parseLong(sc.nextLine());
+ if (carCount <= 0) {
+ throw new IllegalArgumentException();
+ }
+ } catch (IllegalArgumentException exception) {
+ System.out.println(PUT_NATURAL_NUMBER);
+ return inputTotalCarNum();
+ } catch (InputMismatchException exception) {
+ System.out.println(DO_NOT_PUT_STRING);
+ return inputTotalCarNum();
+ }
+ return carCount;
}
- public static int inputNumberOfTrials(Scanner scanner){
- System.out.println("์๋ํ ํ์๋ ๋ชํ์ธ๊ฐ์?");
- return scanner.nextInt();
+ public int inputTotalTrial() {
+ System.out.println(TRIAL_COUNT);
+ int trial = 0;
+ try {
+ trial = Integer.parseInt(sc.nextLine());
+ if (trial <= 0) {
+ throw new IllegalArgumentException();
+ }
+ } catch (IllegalArgumentException exception) {
+ System.out.println(PUT_NATURAL_NUMBER);
+ return inputTotalTrial();
+ } catch (InputMismatchException exception) {
+ System.out.println(DO_NOT_PUT_STRING);
+ return inputTotalTrial();
+ }
+ return trial;
}
-}
\ No newline at end of file
+} | Java | ์ ๋ ๋๊ฑด๋๊ป ๋ฐฐ์ด๊ฑด๋ฐ if๋ฌธ ๋ฐ๋๊ฐ ํ ๋ฌธ์ฅ์ด๋๋ผ๋ ์ค๊ดํธ {}๋ฅผ ์ฌ์ฉํด์ฃผ๋๊ฒ์ด ๊ด๋ก๋ผ๊ณ ํ๋ค์! |
@@ -2,22 +2,28 @@
public class Car{
- private String name;
- private int position = 0;
+ long number;
+ int length;
+ State state;
- public Car(String name){
- this.name = name;
+ public Car(long number) {
+ this.number = number;
+ length = 0;
}
- public String getName() {
- return name;
+ public int moveCount() {
+ return length;
}
- public int getPosition(){
- return position;
+ public long getCarNum() {
+ return number;
}
- public void move(){
- position++;
+ public void changeState(State cmd) {
+ state = cmd;
+ }
+
+ public void move() {
+ length = state.execute(length);
}
}
\ No newline at end of file | Java | ์ ๊ทผ์ ํ์๋ ๊ฐ๋ฅํ ๊ฐ์ฅ ์ข์ ๋ฒ์๋ก ์ ์ธํด์ฃผ๋๊ฒ์ด ์ข๋ค๊ณ ํฉ๋๋ค. number, length, state ๊ฐ์ ๊ฒฝ์ฐ์๋ private๋ก ์ ์ธํด์ฃผ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,38 @@
+package controller;
+
+import function.RandomNumberMaker;
+import model.Car;
+import model.State;
+import model.Movable;
+import model.UnMovable;
+
+import java.util.List;
+
+public class CarController {
+
+ private static final int MOVE_CONDITION = 4;
+
+ public void registerCars(List<Car> cars, long len) {
+ for (int i = 0; i < len; i++) {
+ cars.add(new Car(i));
+ }
+ }
+
+ public void moveCar(List<Car> cars) {
+ for (Car car : cars) {
+ car.changeState(checkMoveOrStop(car));
+ car.move();
+ }
+ }
+
+ private State checkMoveOrStop(Car car) {
+ if (getRandomNumber() >= MOVE_CONDITION) {
+ return new Movable();
+ }
+ return new UnMovable();
+ }
+
+ private int getRandomNumber() {
+ return RandomNumberMaker.getRandomNumber();
+ }
+} | Java | RANDOM_NUMBER_RANGE ๋ ์ด๋จ๊น์? |
@@ -1,66 +0,0 @@
-package controller;
-
-import model.Car;
-import util.RandomGenerator;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-public class RacingGame {
-
- public static final int CRITICAL_POINT = 5;
- public static final int MAX_NAME_LENGTH = 5;
-
- public void oneTrial(List<Car> cars){
- for (Car car : cars){
- check(car);
- }
- }
-
- public void check(Car car){
- if (RandomGenerator.randInt(10) > CRITICAL_POINT){
- car.move();
- }
- }
-
- public List<String> searchWinners(List<Car> cars){
- List<String> winnerNames = new ArrayList<>();
- int maxPosition = searchMaxPosition(cars);
-
- for (Car car : cars){
- checkWinner(car, winnerNames, maxPosition);
- }
- return winnerNames;
- }
-
- public int searchMaxPosition(List<Car> cars){
- List<Integer> carPosition = new ArrayList<>();
-
- for (Car car : cars){
- carPosition.add(car.getPosition());
- }
- return Collections.max(carPosition);
- }
-
- public void checkWinner(Car car, List<String> winnerNames, int maxPosition){
- if (car.getPosition() == maxPosition){
- winnerNames.add(car.getName());
- }
- }
-
- public static List<Car> generateCars(String[] carNames) throws StringOutOfBoundsException{
- List<Car> cars = new ArrayList<>();
- for (String carName : carNames){
- checkNameLength(carName, cars);
- }
- return cars;
- }
-
- public static void checkNameLength(String carName, List<Car> cars) throws StringOutOfBoundsException {
- if(carName.length() > MAX_NAME_LENGTH){
- throw new StringOutOfBoundsException();
- }
- cars.add(new Car(carName));
- }
-} | Java | ๋ฉ์๋๋ช
์ ํ์๋ฅผ ํํํ๋ ๋์ฌ๋ก ์์ํ๋๊ฒ ์ด๋จ๊น์? |
@@ -1,66 +0,0 @@
-package controller;
-
-import model.Car;
-import util.RandomGenerator;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-public class RacingGame {
-
- public static final int CRITICAL_POINT = 5;
- public static final int MAX_NAME_LENGTH = 5;
-
- public void oneTrial(List<Car> cars){
- for (Car car : cars){
- check(car);
- }
- }
-
- public void check(Car car){
- if (RandomGenerator.randInt(10) > CRITICAL_POINT){
- car.move();
- }
- }
-
- public List<String> searchWinners(List<Car> cars){
- List<String> winnerNames = new ArrayList<>();
- int maxPosition = searchMaxPosition(cars);
-
- for (Car car : cars){
- checkWinner(car, winnerNames, maxPosition);
- }
- return winnerNames;
- }
-
- public int searchMaxPosition(List<Car> cars){
- List<Integer> carPosition = new ArrayList<>();
-
- for (Car car : cars){
- carPosition.add(car.getPosition());
- }
- return Collections.max(carPosition);
- }
-
- public void checkWinner(Car car, List<String> winnerNames, int maxPosition){
- if (car.getPosition() == maxPosition){
- winnerNames.add(car.getName());
- }
- }
-
- public static List<Car> generateCars(String[] carNames) throws StringOutOfBoundsException{
- List<Car> cars = new ArrayList<>();
- for (String carName : carNames){
- checkNameLength(carName, cars);
- }
- return cars;
- }
-
- public static void checkNameLength(String carName, List<Car> cars) throws StringOutOfBoundsException {
- if(carName.length() > MAX_NAME_LENGTH){
- throw new StringOutOfBoundsException();
- }
- cars.add(new Car(carName));
- }
-} | Java | check ๋ ๋ฌด์์ ์ฒดํฌํ๋ค๋๊ฑด์ง ์๊ธฐ ์ด๋ ค์ด ๊ฒ ๊ฐ์์~ ๋ด๋ถ์์๋ง ํ์ฉ๋๋ ํด๋์ค๋ฉด private ํค์๋๋ฅผ ๋ถ์ด๊ณ ๋ฉ์๋๋ช
์ ์ข ๋ ํ์ด์จ๋ณด๋ฉด ์ด๋จ๊น์? |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.