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">&copy; 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">&copy; 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 ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๊ณ  ๋ฉ”์„œ๋“œ๋ช…์„ ์ข€ ๋” ํ’€์–ด์จ๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?