code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,14 @@ +package christmas.constant; + +public class MenuConstant { + + public static class MenuType { + + public static final String APPETIZER = "appetizer"; + public static final String MAIN = "main"; + public static final String DESSERT = "dessert"; + public static final String DRINK = "drink"; + public static final String NONE = "none"; + + } +}
Java
enum์— APPETIZER, MAIN, DESSERT, DRINK๋งŒ ์ ์œผ์…”๋„ ๋์„ ๊ฒƒ ๊ฐ™์•„์š”. string ์ƒ์ˆ˜๊ฐ€ ์ด์šฉ๋˜๋Š” ๊ฑธ ๋ณด์ง€ ๋ชปํ–ˆ์–ด์š”.
@@ -0,0 +1,103 @@ +package christmas.domain; + +import christmas.constant.EventConstant.Condition; +import christmas.constant.EventConstant.Days; +import christmas.constant.EventConstant.Discount; +import christmas.constant.EventConstant.Message; +import christmas.constant.EventConstant.Target; +import christmas.constant.EventConstant.TotalDiscount; +import java.util.Arrays; +import java.util.List; + +public enum Event { + + /* + index0(Condition): ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก ์กฐ๊ฑด + index1(Days): ์ ์šฉ ๋‚ ์งœ + index2(Target): ํ• ์ธ ๋ฉ”๋‰ด ๋˜๋Š” ์ฆ์ •ํ’ˆ + index3(Discount): ํ• ์ธ ๊ธˆ์•ก + index4(DiscountFromTotal): ์ด๊ธˆ์•ก์—์„œ ํ• ์ธ ๊ธˆ์•ก + index5(Message): ํ• ์ธ ๋ฉ”์‹œ์ง€ (e.g. WEEKDAY -> "ํ‰์ผ ํ• ์ธ:") + */ + SPECIAL(Condition.CASE_A, + Days.SPECIAL, + Target.SPECIAL, + Discount.SPECIAL, + TotalDiscount.SPECIAL, + Message.SPECIAL), + + WEEKDAY(Condition.CASE_A, + Days.WEEKDAY, + Target.WEEKDAY, + Discount.WEEKDAY, + TotalDiscount.OTHER, + Message.WEEKDAY), + + WEEKEND(Condition.CASE_A, + Days.WEEKEND, + Target.WEEKEND, + Discount.WEEKEND, + TotalDiscount.OTHER, + Message.WEEKEND), + + CHRISTMAS(Condition.CASE_A, + Days.EVERY, + Target.CHRISTMAS, + Discount.CHRISTMAS, + TotalDiscount.OTHER, + Message.CHRISTMAS), + + PRESENTATION(Condition.CASE_B, + Days.EVERY, + Target.PRESENTATION, + Discount.PRESENTATION, + TotalDiscount.OTHER, + Message.PRESENTATION); + + + private final int condition; + private final List<Integer> days; + private final String target; + private final int discount; + private final int discountFromTotal; + private final String message; + + Event(int condition, List<Integer> days, String target, int discountMenu, int discountTotal, String message) { + this.condition = condition; + this.days = days; + this.target = target; + this.discount = discountMenu; + this.discountFromTotal = discountTotal; + this.message = message; + } + + public static List<Event> findAllByDay(int day) { + return Arrays.stream(Event.values()) + .filter(event -> event.days.contains(day) || event.days.contains(day % 7)) + .toList(); + } + + public static List<Event> getAllEvent() { + return Arrays.stream(Event.values()).toList(); + } + + public int getDiscount() { + return discount; + } + + public int getDiscountFromTotal() { + return discountFromTotal; + } + + public String getTarget() { + return target; + } + + public int getCondition() { + return condition; + } + + public String getMessage() { + return message; + } +}
Java
๋‚ด๋ถ€ ๊ฐ’๊ณผ ๊ตฌํ˜„์ด ๋ฟ”๋ฟ”์ด ํฉ์–ด์ ธ ์žˆ๋„ค์š”... ๋ณต์žกํ•˜์ง€ ์•Š๋‹ค๋ฉด, enum ํ•˜๋‚˜์— ์ „๋ถ€ ๋„ฃ์–ด์ฃผ์„ธ์š”. ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค๋ฉด ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ๋” ํž˜๋“ค์–ด์ ธ์š”.
@@ -0,0 +1,65 @@ +package christmas.domain; + +import christmas.constant.MenuConstant.MenuType; +import java.util.Arrays; + +public enum Menu { + + //์• ํ”ผํƒ€์ด์ € + MUSHROOM_SOUP(MenuType.APPETIZER, "์–‘์†ก์ด์ˆ˜ํ”„", 6_000), + TAPAS(MenuType.APPETIZER, "ํƒ€ํŒŒ์Šค", 5_500), + CAESAR_SALAD(MenuType.APPETIZER, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000), + + //๋ฉ”์ธ + T_BONE_STEAK(MenuType.MAIN, "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000), + BBQ_RIBS(MenuType.MAIN, "๋ฐ”๋น„ํ๋ฆฝ", 54_000), + SEAFOOD_PASTA(MenuType.MAIN, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000), + CHRISTMAS_PASTA(MenuType.MAIN, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000), + + //๋””์ €ํŠธ + CHOCOLATE_CAKE(MenuType.DESSERT, "์ดˆ์ฝ”์ผ€์ดํฌ", 15_000), + ICE_CREAM(MenuType.DESSERT, "์•„์ด์Šคํฌ๋ฆผ", 5_000), + + //์Œ๋ฃŒ + ZERO_COKE(MenuType.DRINK, "์ œ๋กœ์ฝœ๋ผ", 3_000), + RED_WINE(MenuType.DRINK, "๋ ˆ๋“œ์™€์ธ", 60_000), + CHAMPAGNE(MenuType.DRINK, "์ƒดํŽ˜์ธ", 25_000), + + //NONE + NONE(MenuType.NONE, "์—†๋Š”๋ฉ”๋‰ด", 0); + + private final String type; + private final String name; + private final int price; + + Menu(String type, String name, int price) { + this.type = type; + this.name = name; + this.price = price; + } + + public String getType() { + return type; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public static Menu findByName(String menuName) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(menuName)) + .findFirst() + .orElse(NONE); + } + + public static boolean isExistByName(String menuName) { + return Arrays.stream(Menu.values()) + .anyMatch(menu -> menu.getName().equals(menuName)); + } + +}
Java
์ด ์ฝ”๋“œ๋Š” ๊น”๋”ํ•˜๊ฒŒ ์ž˜ ์ž‘์„ฑํ•˜์…จ๋„ค์š”. ๐Ÿ‘
@@ -0,0 +1,30 @@ +package christmas.domain; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class EventTest { + + @DisplayName("๋‚ ์งœ์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ์ด๋ฒคํŠธ๋“ค์„ ์ฐพ๋Š”๋‹ค.") + @Test + void findAllByDay() throws Exception { + //given + int day = 3; + //when + List<Event> events = Event.findAllByDay(3); + //then + assertAll( + + () -> assertThat(events).contains(Event.WEEKDAY), + () -> assertThat(events).contains(Event.CHRISTMAS), + () -> assertThat(events).contains(Event.SPECIAL), + () -> assertThat(events).contains(Event.PRESENTATION) + ); + } + + +} \ No newline at end of file
Java
ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ผ ํ•ด๋„ ๋งค์ง ๋„˜๋ฒ„๋ฅผ ์ œ๊ฑฐํ•ด์ฃผ๋ฉด ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,83 @@ +package christmas.service; + +import christmas.constant.Constant; +import christmas.domain.Badge; +import christmas.domain.Benefit; +import christmas.domain.Event; +import christmas.domain.OrderMenu; +import christmas.view.input.template.InputCallback; +import christmas.view.input.template.InputTemplate; + +public class ChristmasService { + + private final InputTemplate inputTemplate; + + public ChristmasService() { + this.inputTemplate = new InputTemplate(); + } + + public <T> T getInputRequestResult(InputCallback<T> callback) { + return inputTemplate.execute(callback); + } + + public Benefit getBenefit(OrderMenu orderMenu, int day) { + Benefit benefit = new Benefit(); + addDiscountIntoBenefitDetails(orderMenu, benefit, day); + return benefit; + } + + public String getOrderMenuDetails(OrderMenu orderMenu) { + return orderMenu.detailsToString(); + } + + public String getTotalPriceBeforeDiscount(OrderMenu orderMenu) { + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), orderMenu.calculateTotalPrice()); + } + + public String getPresentationMenu(Benefit benefit) { + return benefit.presentationMenuToString(); + } + + public String getBenefitDetails(Benefit benefit) { + return benefit.detailsToString(); + } + + public String getTotalDiscount(Benefit benefit) { + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), benefit.calculateTotalDiscount()); + } + + public String getTotalPriceAfterDiscount(OrderMenu orderMenu, Benefit benefit) { + int totalPrice = orderMenu.calculateTotalPrice(); + int totalDiscount = benefit.calculateTotalDiscount(); + totalPrice += (totalDiscount - benefit.calculatePresentationPrice()); + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), totalPrice); + } + + public String getBadge(Benefit benefit) { + return Badge.findByTotalDiscount(benefit.calculateTotalDiscount()); + } + + + private void addDiscountIntoBenefitDetails(OrderMenu orderMenu, Benefit benefit, int day) { + Event.findAllByDay(day) + .stream() + .filter(event -> orderMenu.calculateTotalPrice() > event.getCondition()) + .forEach(event -> benefit.addDiscountIntoDetails(event, calculateDiscount(orderMenu, event, day))); + } + + private int calculateDiscount(OrderMenu orderMenu, Event event, int day) { + int result = 0; + int orderMenuCount = orderMenu.countMenuByEventTarget(event.getTarget()); + int discount = event.getDiscount(); + int discountFromTotal = event.getDiscountFromTotal(); + result -= (discount * orderMenuCount) + discountFromTotal; + + if (event == Event.CHRISTMAS && day <= 25) { + result -= discount * (day - 1) + 1000; + } + if (event == Event.PRESENTATION) { + result -= discount; + } + return result; + } +}
Java
> ์œ„์—์„œ inputView์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๋„˜๊ฒจ๋ฐ›๊ณ  ์‹คํ–‰ํ•˜๋Š” ๋ชจ์Šต์ด๋˜๋ฐ, ๊ทธ๋ƒฅ ChristmasController์—์„œ ์ง„ํ–‰ํ•˜๋Š” ๊ฒŒ ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์•„์š”. ๊ตณ์ด ์„œ๋น„์Šค๊นŒ์ง€ ๊ฑธ์น  ํ•„์š”๊ฐ€ ์—†์—ˆ์„ ๊ฒƒ ๊ฐ™์•„์š”. ์„œ๋น„์Šค ๊ฐ์ฒด ๋‚ด์—์„œ ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๊ฐ€ ์“ฐ์ธ ๊ฒƒ๋„ ์•„๋‹ˆ๊ณ , ์˜ค๋กœ์ง€ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ๋งŒ ์ด ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  ์žˆ์–ด์š”. ์ € ๋ฉ”์„œ๋“œ๋Š” ๋ง์”€ํ•˜์‹ ๋Œ€๋กœ Controller์— ์žˆ์–ด๋„ ๋ฌด๋ฐฉํ•˜๊ธด ํ•ฉ๋‹ˆ๋‹ค :) ํ•˜์ง€๋งŒ Controller๊ฐ€ View, Service์™ธ์— ๋‹ค๋ฅธ ๊ฐ์ฒด์— ์˜์กดํ•˜์ง€ ์•Š๊ฒŒ๋” InputTemplate๋ฅผ Service์— ๋‘”๊ฑฐ์˜€์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,72 @@ +package christmas.domain; + +import christmas.constant.Constant; +import java.util.EnumMap; +import java.util.Map; +import java.util.Map.Entry; + +public class Benefit { + + private final Map<Event, Integer> details; + + public Benefit() { + this.details = new EnumMap<>(Event.class); + initialBenefitDetails(); + } + + public int calculateTotalDiscount() { + return details.values() + .stream() + .reduce(0, Integer::sum); + } + + public void addDiscountIntoDetails(Event event, int discount) { + details.computeIfPresent(event, (key, value) -> value + discount); + } + + public String detailsToString() { + StringBuilder sb = new StringBuilder(Constant.NOTHING + System.lineSeparator()); + if (calculateTotalDiscount() != 0) { + sb.setLength(0); + appendDetails(sb); + } + return sb.toString(); + } + + public int calculatePresentationPrice() { + return details.get(Event.PRESENTATION); + } + + public String presentationMenuToString() { + Event presentation = Event.PRESENTATION; + StringBuilder sb = new StringBuilder(Constant.NOTHING + System.lineSeparator()); + int discount = details.get(presentation); + if (discount != 0) { + sb.setLength(0); + sb.append(presentation.getTarget()); + sb.append(Constant.SPACE); + sb.append(String.format(Constant.MENU_UNIT, Math.abs(discount / Event.PRESENTATION.getDiscount()))); + sb.append(System.lineSeparator()); + } + return sb.toString(); + } + + + private void appendDetails(StringBuilder benefitDetail) { + for (Entry<Event, Integer> entry : details.entrySet()) { + String eventMessage = entry.getKey().getMessage(); + int discount = entry.getValue(); + if (discount != 0) { + benefitDetail.append(eventMessage); + benefitDetail.append(Constant.SPACE); + benefitDetail.append(String.format(Constant.PRICE_UNIT, discount)); + benefitDetail.append(System.lineSeparator()); + } + } + } + + private void initialBenefitDetails() { + Event.getAllEvent().forEach(event -> details.put(event, 0)); + } + +}
Java
> add ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์ƒ์„ฑ์ž์—์„œ ์™„์„ฑ๋œ EnumMap์„ ๋ฐ›๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”. add ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ EnumMap์„ ๋งŒ๋“œ๋Š” ์ฑ…์ž„์€ BenefitBuilder ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์„œ ๋งก๊ธฐ๋Š” ๊ฒŒ ์ข‹์•„์š”. ๊ทธ๋ ‡๊ฒ ๋„ค์š”! ๋นŒ๋”๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ถ”ํ›„ ์ž˜๋ชป๋œ Benfit ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™”์„๋•Œ ํฌ๋กœ์Šค์ฒดํฌํ•˜๊ธฐ๋„ ์šฉ์ดํ•˜๊ฒ ๋„ค์š” ! ์ข‹์€ ํ”ผ๋“œ๋ฐฑ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž
@@ -0,0 +1,83 @@ +package christmas.service; + +import christmas.constant.Constant; +import christmas.domain.Badge; +import christmas.domain.Benefit; +import christmas.domain.Event; +import christmas.domain.OrderMenu; +import christmas.view.input.template.InputCallback; +import christmas.view.input.template.InputTemplate; + +public class ChristmasService { + + private final InputTemplate inputTemplate; + + public ChristmasService() { + this.inputTemplate = new InputTemplate(); + } + + public <T> T getInputRequestResult(InputCallback<T> callback) { + return inputTemplate.execute(callback); + } + + public Benefit getBenefit(OrderMenu orderMenu, int day) { + Benefit benefit = new Benefit(); + addDiscountIntoBenefitDetails(orderMenu, benefit, day); + return benefit; + } + + public String getOrderMenuDetails(OrderMenu orderMenu) { + return orderMenu.detailsToString(); + } + + public String getTotalPriceBeforeDiscount(OrderMenu orderMenu) { + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), orderMenu.calculateTotalPrice()); + } + + public String getPresentationMenu(Benefit benefit) { + return benefit.presentationMenuToString(); + } + + public String getBenefitDetails(Benefit benefit) { + return benefit.detailsToString(); + } + + public String getTotalDiscount(Benefit benefit) { + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), benefit.calculateTotalDiscount()); + } + + public String getTotalPriceAfterDiscount(OrderMenu orderMenu, Benefit benefit) { + int totalPrice = orderMenu.calculateTotalPrice(); + int totalDiscount = benefit.calculateTotalDiscount(); + totalPrice += (totalDiscount - benefit.calculatePresentationPrice()); + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), totalPrice); + } + + public String getBadge(Benefit benefit) { + return Badge.findByTotalDiscount(benefit.calculateTotalDiscount()); + } + + + private void addDiscountIntoBenefitDetails(OrderMenu orderMenu, Benefit benefit, int day) { + Event.findAllByDay(day) + .stream() + .filter(event -> orderMenu.calculateTotalPrice() > event.getCondition()) + .forEach(event -> benefit.addDiscountIntoDetails(event, calculateDiscount(orderMenu, event, day))); + } + + private int calculateDiscount(OrderMenu orderMenu, Event event, int day) { + int result = 0; + int orderMenuCount = orderMenu.countMenuByEventTarget(event.getTarget()); + int discount = event.getDiscount(); + int discountFromTotal = event.getDiscountFromTotal(); + result -= (discount * orderMenuCount) + discountFromTotal; + + if (event == Event.CHRISTMAS && day <= 25) { + result -= discount * (day - 1) + 1000; + } + if (event == Event.PRESENTATION) { + result -= discount; + } + return result; + } +}
Java
> benefit init์„ ํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ์™ธ๋ถ€์—์„œ ๋ฐ›์•„์˜ค๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ๋‚ด๋ถ€์—์„œ benefit๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒŒ ์ข‹์•„์š”. ์™ธ๋ถ€์—์„œ ๋ฐ›์•„์˜จ benefit์— ๋Œ€ํ•ด ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋ฉด side effect๊ฐ€ ๋ฐœ์ƒํ•  ์—ฌ์ง€๊ฐ€ ๋†’์•„์š”. ํ•ด๋‹น ์ฝ”๋“œ๋Š” ๋ฉ”์†Œ๋“œ๋กœ ๋ถ„๋ฆฌํ•˜์ง€ ์•Š๋Š” ๊ฒŒ ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์•„์š”. > > ๋นŒ๋”๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋” ์ข‹์Šต๋‹ˆ๋‹ค :DD ์Œ benefit์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š”๊ฒŒ ํ•„๋“œ์— ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ด์•ผํ•œ๋‹ค๋Š” ๋ง์”€์ผ๊นŒ์š”??!
@@ -0,0 +1,83 @@ +package christmas.service; + +import christmas.constant.Constant; +import christmas.domain.Badge; +import christmas.domain.Benefit; +import christmas.domain.Event; +import christmas.domain.OrderMenu; +import christmas.view.input.template.InputCallback; +import christmas.view.input.template.InputTemplate; + +public class ChristmasService { + + private final InputTemplate inputTemplate; + + public ChristmasService() { + this.inputTemplate = new InputTemplate(); + } + + public <T> T getInputRequestResult(InputCallback<T> callback) { + return inputTemplate.execute(callback); + } + + public Benefit getBenefit(OrderMenu orderMenu, int day) { + Benefit benefit = new Benefit(); + addDiscountIntoBenefitDetails(orderMenu, benefit, day); + return benefit; + } + + public String getOrderMenuDetails(OrderMenu orderMenu) { + return orderMenu.detailsToString(); + } + + public String getTotalPriceBeforeDiscount(OrderMenu orderMenu) { + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), orderMenu.calculateTotalPrice()); + } + + public String getPresentationMenu(Benefit benefit) { + return benefit.presentationMenuToString(); + } + + public String getBenefitDetails(Benefit benefit) { + return benefit.detailsToString(); + } + + public String getTotalDiscount(Benefit benefit) { + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), benefit.calculateTotalDiscount()); + } + + public String getTotalPriceAfterDiscount(OrderMenu orderMenu, Benefit benefit) { + int totalPrice = orderMenu.calculateTotalPrice(); + int totalDiscount = benefit.calculateTotalDiscount(); + totalPrice += (totalDiscount - benefit.calculatePresentationPrice()); + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), totalPrice); + } + + public String getBadge(Benefit benefit) { + return Badge.findByTotalDiscount(benefit.calculateTotalDiscount()); + } + + + private void addDiscountIntoBenefitDetails(OrderMenu orderMenu, Benefit benefit, int day) { + Event.findAllByDay(day) + .stream() + .filter(event -> orderMenu.calculateTotalPrice() > event.getCondition()) + .forEach(event -> benefit.addDiscountIntoDetails(event, calculateDiscount(orderMenu, event, day))); + } + + private int calculateDiscount(OrderMenu orderMenu, Event event, int day) { + int result = 0; + int orderMenuCount = orderMenu.countMenuByEventTarget(event.getTarget()); + int discount = event.getDiscount(); + int discountFromTotal = event.getDiscountFromTotal(); + result -= (discount * orderMenuCount) + discountFromTotal; + + if (event == Event.CHRISTMAS && day <= 25) { + result -= discount * (day - 1) + 1000; + } + if (event == Event.PRESENTATION) { + result -= discount; + } + return result; + } +}
Java
> ํ• ์ธ์•ก์„ ๊ณ„์‚ฐํ•˜๋Š” ๋กœ์ง์ด ์™ธ๋ถ€๋กœ ๋…ธ์ถœ๋˜์–ด ์žˆ์–ด์š”. enum์„ ํ™œ์šฉํ•˜์…จ์œผ๋‹ˆ ์ž˜ ๋‹ค๋“ฌ์œผ๋ฉด ์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์–ด์š”. > > ์ด๋Ÿฌ๋ฉด ๋ฉ”์†Œ๋“œ๋กœ ๋ถ„๋ฆฌํ•  ํ•„์š”๋„ ์—†์–ด์ง€๊ณ  ์ข‹์•„์š”. ์ด๋ฒคํŠธ enum๋งˆ๋‹ค ๋žŒ๋‹ค์‹์„ ์ž‘์„ฑํ•ด์„œ ํ˜œํƒ์•ก์„ ๊ณ„์‚ฐํ•ด๋ณด์„ธ์š”. enum์— ๋žŒ๋‹ค์‹์ด ์š”์†Œ๋กœ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋Š”๊ฑธ ํด๋ผ์šฐ๋“œ๋‹˜ ์ฝ”๋“œ ๋ณด๊ณ  ์•Œ์•˜์Šต๋‹ˆ๋‹ค! ๋งŒ์•ฝ enum ์ƒ์ˆ˜๋“ค์ด ํ• ์ธ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” ๋žŒ๋‹ค์‹์„ ๊ฐ–๊ณ  ์žˆ์œผ๋ฉด if๋ฅผ ๋‘๋ฒˆ ์‚ฌ์šฉํ•  ์ผ๋„ ์—†๊ฒ ๋„ค์š”! ๋ง์”€ํ•ด์ฃผ์‹  ํžŒํŠธ๋กœ ๋ฆฌํŒฉํ† ๋ง ํ•œ ๋ฒˆ ์ง„ํ–‰ํ•ด๋ณผ๊ฒŒ์š”! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž
@@ -0,0 +1,83 @@ +package christmas.service; + +import christmas.constant.Constant; +import christmas.domain.Badge; +import christmas.domain.Benefit; +import christmas.domain.Event; +import christmas.domain.OrderMenu; +import christmas.view.input.template.InputCallback; +import christmas.view.input.template.InputTemplate; + +public class ChristmasService { + + private final InputTemplate inputTemplate; + + public ChristmasService() { + this.inputTemplate = new InputTemplate(); + } + + public <T> T getInputRequestResult(InputCallback<T> callback) { + return inputTemplate.execute(callback); + } + + public Benefit getBenefit(OrderMenu orderMenu, int day) { + Benefit benefit = new Benefit(); + addDiscountIntoBenefitDetails(orderMenu, benefit, day); + return benefit; + } + + public String getOrderMenuDetails(OrderMenu orderMenu) { + return orderMenu.detailsToString(); + } + + public String getTotalPriceBeforeDiscount(OrderMenu orderMenu) { + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), orderMenu.calculateTotalPrice()); + } + + public String getPresentationMenu(Benefit benefit) { + return benefit.presentationMenuToString(); + } + + public String getBenefitDetails(Benefit benefit) { + return benefit.detailsToString(); + } + + public String getTotalDiscount(Benefit benefit) { + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), benefit.calculateTotalDiscount()); + } + + public String getTotalPriceAfterDiscount(OrderMenu orderMenu, Benefit benefit) { + int totalPrice = orderMenu.calculateTotalPrice(); + int totalDiscount = benefit.calculateTotalDiscount(); + totalPrice += (totalDiscount - benefit.calculatePresentationPrice()); + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), totalPrice); + } + + public String getBadge(Benefit benefit) { + return Badge.findByTotalDiscount(benefit.calculateTotalDiscount()); + } + + + private void addDiscountIntoBenefitDetails(OrderMenu orderMenu, Benefit benefit, int day) { + Event.findAllByDay(day) + .stream() + .filter(event -> orderMenu.calculateTotalPrice() > event.getCondition()) + .forEach(event -> benefit.addDiscountIntoDetails(event, calculateDiscount(orderMenu, event, day))); + } + + private int calculateDiscount(OrderMenu orderMenu, Event event, int day) { + int result = 0; + int orderMenuCount = orderMenu.countMenuByEventTarget(event.getTarget()); + int discount = event.getDiscount(); + int discountFromTotal = event.getDiscountFromTotal(); + result -= (discount * orderMenuCount) + discountFromTotal; + + if (event == Event.CHRISTMAS && day <= 25) { + result -= discount * (day - 1) + 1000; + } + if (event == Event.PRESENTATION) { + result -= discount; + } + return result; + } +}
Java
> ์ด๋Ÿฐ ์ž๋ช…ํ•œ ์ฝ”๋“œ๋“ค์€ ๋ฉ”์†Œ๋“œ๋กœ ๋ถ„๋ฆฌํ•  ํ•„์š”๊ฐ€ ์—†์–ด์š”. ์„œ๋น„์Šค ๊ฐ์ฒด๊นŒ์ง€ ์ด์šฉํ•  ํ•„์š”๋Š” ๋”๋”์šฑ ์—†์–ด์š”. ์ด ๋ถ€๋ถ„๋„ Controller์—์„œ ๋ฐ”๋กœ ํ˜ธ์ถœํ• ์ง€ ๊ณ ๋ฏผํ–ˆ๋˜ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. Controller์— Model(๋„๋ฉ”์ธ)์— ๊ด€๋ จ๋œ ์ฝ”๋“œ๊ฐ€ ์žˆ์–ด๋„ ๋˜๊ธดํ•˜์ง€๋งŒ Service๋งŒ ์˜์กดํ•˜๊ฒŒ๋” ํ•˜๊ณ ์‹ถ์—ˆ๊ฑฐ๋“ ์š”! ๊ทผ๋ฐ ๋ง์”€ํ•˜์‹ ๋Œ€๋กœ ์ด๋Ÿฐ ์ž๋ช…ํ•œ ์ฝ”๋“œ๋“ค์€ ๋ถˆํ•„์š”ํ•˜๊ธด ํ•˜๋„ค์š”.. ๊ณ ๋ฏผ์„ ํ•œ๋ฒˆ ํ•ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค ใ… 
@@ -0,0 +1,14 @@ +package christmas.constant; + +public class MenuConstant { + + public static class MenuType { + + public static final String APPETIZER = "appetizer"; + public static final String MAIN = "main"; + public static final String DESSERT = "dessert"; + public static final String DRINK = "drink"; + public static final String NONE = "none"; + + } +}
Java
> enum์— APPETIZER, MAIN, DESSERT, DRINK๋งŒ ์ ์œผ์…”๋„ ๋์„ ๊ฒƒ ๊ฐ™์•„์š”. string ์ƒ์ˆ˜๊ฐ€ ์ด์šฉ๋˜๋Š” ๊ฑธ ๋ณด์ง€ ๋ชปํ–ˆ์–ด์š”. Event์—๋„ ์‚ฌ์šฉ๋˜๋Š” ์ƒ์ˆ˜๋“ค์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์ˆ˜ํ™”๋ฅผ ์ง„ํ–‰ํ•œ๊ฑฐ์˜€์Šต๋‹ˆ๋‹ค! WEEKDAY, WEEKEND์ผ๋•Œ ๊ฐ๊ฐ dessert์™€ main์„ ํ• ์ธ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น string์˜ ์žฌ์‚ฌ์šฉ์„ฑ์œ„ํ•ด ๋”ฐ๋กœ ์ƒ์ˆ˜ํ™”๋ฅผ ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,103 @@ +package christmas.domain; + +import christmas.constant.EventConstant.Condition; +import christmas.constant.EventConstant.Days; +import christmas.constant.EventConstant.Discount; +import christmas.constant.EventConstant.Message; +import christmas.constant.EventConstant.Target; +import christmas.constant.EventConstant.TotalDiscount; +import java.util.Arrays; +import java.util.List; + +public enum Event { + + /* + index0(Condition): ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก ์กฐ๊ฑด + index1(Days): ์ ์šฉ ๋‚ ์งœ + index2(Target): ํ• ์ธ ๋ฉ”๋‰ด ๋˜๋Š” ์ฆ์ •ํ’ˆ + index3(Discount): ํ• ์ธ ๊ธˆ์•ก + index4(DiscountFromTotal): ์ด๊ธˆ์•ก์—์„œ ํ• ์ธ ๊ธˆ์•ก + index5(Message): ํ• ์ธ ๋ฉ”์‹œ์ง€ (e.g. WEEKDAY -> "ํ‰์ผ ํ• ์ธ:") + */ + SPECIAL(Condition.CASE_A, + Days.SPECIAL, + Target.SPECIAL, + Discount.SPECIAL, + TotalDiscount.SPECIAL, + Message.SPECIAL), + + WEEKDAY(Condition.CASE_A, + Days.WEEKDAY, + Target.WEEKDAY, + Discount.WEEKDAY, + TotalDiscount.OTHER, + Message.WEEKDAY), + + WEEKEND(Condition.CASE_A, + Days.WEEKEND, + Target.WEEKEND, + Discount.WEEKEND, + TotalDiscount.OTHER, + Message.WEEKEND), + + CHRISTMAS(Condition.CASE_A, + Days.EVERY, + Target.CHRISTMAS, + Discount.CHRISTMAS, + TotalDiscount.OTHER, + Message.CHRISTMAS), + + PRESENTATION(Condition.CASE_B, + Days.EVERY, + Target.PRESENTATION, + Discount.PRESENTATION, + TotalDiscount.OTHER, + Message.PRESENTATION); + + + private final int condition; + private final List<Integer> days; + private final String target; + private final int discount; + private final int discountFromTotal; + private final String message; + + Event(int condition, List<Integer> days, String target, int discountMenu, int discountTotal, String message) { + this.condition = condition; + this.days = days; + this.target = target; + this.discount = discountMenu; + this.discountFromTotal = discountTotal; + this.message = message; + } + + public static List<Event> findAllByDay(int day) { + return Arrays.stream(Event.values()) + .filter(event -> event.days.contains(day) || event.days.contains(day % 7)) + .toList(); + } + + public static List<Event> getAllEvent() { + return Arrays.stream(Event.values()).toList(); + } + + public int getDiscount() { + return discount; + } + + public int getDiscountFromTotal() { + return discountFromTotal; + } + + public String getTarget() { + return target; + } + + public int getCondition() { + return condition; + } + + public String getMessage() { + return message; + } +}
Java
> ๋‚ด๋ถ€ ๊ฐ’๊ณผ ๊ตฌํ˜„์ด ๋ฟ”๋ฟ”์ด ํฉ์–ด์ ธ ์žˆ๋„ค์š”... ๋ณต์žกํ•˜์ง€ ์•Š๋‹ค๋ฉด, enum ํ•˜๋‚˜์— ์ „๋ถ€ ๋„ฃ์–ด์ฃผ์„ธ์š”. ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค๋ฉด ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ๋” ํž˜๋“ค์–ด์ ธ์š”. ์ €๋Š” ์š”์†Œ๋“ค์ด ๋งŽ์•„์„œ ๋ณต์žกํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฐ€๋…์„ฑ๋„ ๋†’์ผ๊ฒธ ๋‚ด๋ถ€ ๊ฐ’์„ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค! ๊ทธ๋ฆฌ๊ณ  ์œ ์ง€๋ณด์ˆ˜์‹œ์—๋„ ํ•ด๋‹น ๊ฐ’๋“ค์ด ๋ชจ์—ฌ์žˆ๋Š” ํŒŒ์ผ๋งŒ ๋ณด๋ฉด ๋˜๋„๋ก ์˜๋„ํ•œ๊ฑฐ์˜€์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,65 @@ +package christmas.domain; + +import christmas.constant.MenuConstant.MenuType; +import java.util.Arrays; + +public enum Menu { + + //์• ํ”ผํƒ€์ด์ € + MUSHROOM_SOUP(MenuType.APPETIZER, "์–‘์†ก์ด์ˆ˜ํ”„", 6_000), + TAPAS(MenuType.APPETIZER, "ํƒ€ํŒŒ์Šค", 5_500), + CAESAR_SALAD(MenuType.APPETIZER, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000), + + //๋ฉ”์ธ + T_BONE_STEAK(MenuType.MAIN, "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000), + BBQ_RIBS(MenuType.MAIN, "๋ฐ”๋น„ํ๋ฆฝ", 54_000), + SEAFOOD_PASTA(MenuType.MAIN, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000), + CHRISTMAS_PASTA(MenuType.MAIN, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000), + + //๋””์ €ํŠธ + CHOCOLATE_CAKE(MenuType.DESSERT, "์ดˆ์ฝ”์ผ€์ดํฌ", 15_000), + ICE_CREAM(MenuType.DESSERT, "์•„์ด์Šคํฌ๋ฆผ", 5_000), + + //์Œ๋ฃŒ + ZERO_COKE(MenuType.DRINK, "์ œ๋กœ์ฝœ๋ผ", 3_000), + RED_WINE(MenuType.DRINK, "๋ ˆ๋“œ์™€์ธ", 60_000), + CHAMPAGNE(MenuType.DRINK, "์ƒดํŽ˜์ธ", 25_000), + + //NONE + NONE(MenuType.NONE, "์—†๋Š”๋ฉ”๋‰ด", 0); + + private final String type; + private final String name; + private final int price; + + Menu(String type, String name, int price) { + this.type = type; + this.name = name; + this.price = price; + } + + public String getType() { + return type; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public static Menu findByName(String menuName) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(menuName)) + .findFirst() + .orElse(NONE); + } + + public static boolean isExistByName(String menuName) { + return Arrays.stream(Menu.values()) + .anyMatch(menu -> menu.getName().equals(menuName)); + } + +}
Java
> ์ด ์ฝ”๋“œ๋Š” ๊น”๋”ํ•˜๊ฒŒ ์ž˜ ์ž‘์„ฑํ•˜์…จ๋„ค์š”. ๐Ÿ‘ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž!
@@ -0,0 +1,30 @@ +package christmas.domain; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class EventTest { + + @DisplayName("๋‚ ์งœ์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ์ด๋ฒคํŠธ๋“ค์„ ์ฐพ๋Š”๋‹ค.") + @Test + void findAllByDay() throws Exception { + //given + int day = 3; + //when + List<Event> events = Event.findAllByDay(3); + //then + assertAll( + + () -> assertThat(events).contains(Event.WEEKDAY), + () -> assertThat(events).contains(Event.CHRISTMAS), + () -> assertThat(events).contains(Event.SPECIAL), + () -> assertThat(events).contains(Event.PRESENTATION) + ); + } + + +} \ No newline at end of file
Java
> ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ผ ํ•ด๋„ ๋งค์ง ๋„˜๋ฒ„๋ฅผ ์ œ๊ฑฐํ•ด์ฃผ๋ฉด ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์•„์š”. ๋™์˜ํ•ฉ๋‹ˆ๋‹ค! ์•ˆ๊ทธ๋ž˜๋„ ์ €๋Ÿฐ ๋งค์ง๋„˜๋ฒ„๋ฅผ ๋”ฐ๋กœ enum์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์„œ ํ…Œ์ŠคํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋”๋ผ๊ตฌ์š” ใ…Žใ…Ž ํ•œ๋ฒˆ ๊ฐœ์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,83 @@ +package christmas.service; + +import christmas.constant.Constant; +import christmas.domain.Badge; +import christmas.domain.Benefit; +import christmas.domain.Event; +import christmas.domain.OrderMenu; +import christmas.view.input.template.InputCallback; +import christmas.view.input.template.InputTemplate; + +public class ChristmasService { + + private final InputTemplate inputTemplate; + + public ChristmasService() { + this.inputTemplate = new InputTemplate(); + } + + public <T> T getInputRequestResult(InputCallback<T> callback) { + return inputTemplate.execute(callback); + } + + public Benefit getBenefit(OrderMenu orderMenu, int day) { + Benefit benefit = new Benefit(); + addDiscountIntoBenefitDetails(orderMenu, benefit, day); + return benefit; + } + + public String getOrderMenuDetails(OrderMenu orderMenu) { + return orderMenu.detailsToString(); + } + + public String getTotalPriceBeforeDiscount(OrderMenu orderMenu) { + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), orderMenu.calculateTotalPrice()); + } + + public String getPresentationMenu(Benefit benefit) { + return benefit.presentationMenuToString(); + } + + public String getBenefitDetails(Benefit benefit) { + return benefit.detailsToString(); + } + + public String getTotalDiscount(Benefit benefit) { + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), benefit.calculateTotalDiscount()); + } + + public String getTotalPriceAfterDiscount(OrderMenu orderMenu, Benefit benefit) { + int totalPrice = orderMenu.calculateTotalPrice(); + int totalDiscount = benefit.calculateTotalDiscount(); + totalPrice += (totalDiscount - benefit.calculatePresentationPrice()); + return String.format(Constant.PRICE_UNIT + System.lineSeparator(), totalPrice); + } + + public String getBadge(Benefit benefit) { + return Badge.findByTotalDiscount(benefit.calculateTotalDiscount()); + } + + + private void addDiscountIntoBenefitDetails(OrderMenu orderMenu, Benefit benefit, int day) { + Event.findAllByDay(day) + .stream() + .filter(event -> orderMenu.calculateTotalPrice() > event.getCondition()) + .forEach(event -> benefit.addDiscountIntoDetails(event, calculateDiscount(orderMenu, event, day))); + } + + private int calculateDiscount(OrderMenu orderMenu, Event event, int day) { + int result = 0; + int orderMenuCount = orderMenu.countMenuByEventTarget(event.getTarget()); + int discount = event.getDiscount(); + int discountFromTotal = event.getDiscountFromTotal(); + result -= (discount * orderMenuCount) + discountFromTotal; + + if (event == Event.CHRISTMAS && day <= 25) { + result -= discount * (day - 1) + 1000; + } + if (event == Event.PRESENTATION) { + result -= discount; + } + return result; + } +}
Java
์™ธ๋ถ€์—์„œ benefit์„ ๋ฐ›์•„์˜ค์ง€ ๋ง๊ณ , ๋‚ด๋ถ€์—์„œ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒŒ ์ข‹๋‹ค๋Š” ๋œป์ด์—ˆ์–ด์š”. ```suggestion private Benefit addDiscountIntoBenefitDetails(OrderMenu orderMenu, int day) { Benefit benefit = new Benefit(); Event.findAllByDay(day) .stream() .filter(event -> orderMenu.calculateTotalPrice() > event.getCondition()) .forEach(event -> benefit.addDiscountIntoDetails(event, calculateDiscount(orderMenu, event, day))); return benefit; } ``` ๋นŒ๋”๋ฅผ ๋งŒ๋“ค๋ฉด, ํ•ด๋‹น ์ž‘์—…์„ ๋นŒ๋”๊ฐ€ ๋Œ€์‹ ํ•˜๊ฒŒ ๋  ๊ฑฐ์—์š”!
@@ -0,0 +1,164 @@ +# ๐ŸŽ„ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ”„๋กœ๋ชจ์…˜ App๐ŸŽ„ + +๋ณธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ์ฃผ๋ฌธ ๋ฉ”๋‰ด์— ๋”ฐ๋ผ ๋ฐ›๊ฒŒ ๋˜๋Š” 2023๋…„ 12์›” ํ˜œํƒ๋“ค์„ ๋ฏธ๋ฆฌ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +## ๐Ÿ” ์ง„ํ–‰๊ณผ์ • + +1. ๋ฐฉ๋ฌธ์„ ํฌ๋งํ•˜์‹œ๋Š” ๋‚ ์งœ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” + +- ๋‚ ์งœ๋Š” ์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” (๊ณต๋ฐฑ์€ ํฌํ•จ ๋˜์–ด๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.) + +``` +์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค. +12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!) +3 +``` + +2. ์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด๋ฅผ ๊ฐฏ์ˆ˜์™€ ํ•จ๊ป˜ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” + +- `๋ฉ”๋‰ด์ด๋ฆ„-๋ฉ”๋‰ด๊ฐฏ์ˆ˜` ํ˜•์‹์„ ๊ผญ ์ง€์ผœ์ฃผ์„ธ์š” (๊ณต๋ฐฑ์€ ํฌํ•จ ๋˜์–ด๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.) +- ๋ฉ”๋‰ด๋Š” ๊ผญ 1๊ฐœ ์ด์ƒ ์ฃผ๋ฌธํ•ด์ฃผ์„ธ์š”(e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1 [O] // ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-0 [X]) +- ๊ฐ ๋ฉ”๋‰ด์˜ ๊ฐฏ์ˆ˜ ํ•ฉ์„ 20๊ฐœ ์ดํ•˜๋กœ ์ฃผ๋ฌธํ•ด์ฃผ์„ธ์š”(e.g. ๋ฐ”๋น„ํ๋ฆฝ-5,๋ ˆ๋“œ์™€์ธ-5 [O] // ๋ฐ”๋น„ํ๋ฆฝ-11, ๋ ˆ๋“œ์™€์ธ-10 [X]) +- ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์–ด์š”. +- ๋ฉ”๋‰ดํŒ์— ์—†๋Š” ๋ฉ”๋‰ด๋Š” ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์–ด์š” +- ์ค‘๋ณต๋œ ๋ฉ”๋‰ด๋Š” ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์–ด์š”(e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1 [X]) + +``` +ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,๋ฐ”๋น„ํ๋ฆฝ-1,์ดˆ์ฝ”์ผ€์ดํฌ-2,์ œ๋กœ์ฝœ๋ผ-1 +``` + +3. ์ฃผ๋ฌธ ๋ฐ ํ˜œํƒ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š” + +- ์ฃผ๋ฌธ ๋ฉ”๋‰ด ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ด 7๊ฐ€์ง€์˜ ํƒ€์ดํ‹€์— ํ•ด๋‹นํ•˜๋Š” ๋‚ด์—ญ๋“ค์ด ๋งž๋Š”์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”. +- ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก์€ **์ฆ์ • ์ด๋ฒคํŠธ ํ• ์ธ์„ ์ œ์™ธ**ํ•œ ๊ธˆ์•ก์ด์—์š” + +``` +<์ฃผ๋ฌธ ๋ฉ”๋‰ด> +ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ 1๊ฐœ +๋ฐ”๋น„ํ๋ฆฝ 1๊ฐœ +์ดˆ์ฝ”์ผ€์ดํฌ 2๊ฐœ +์ œ๋กœ์ฝœ๋ผ 1๊ฐœ + +<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก> +142,000์› + +<์ฆ์ • ๋ฉ”๋‰ด> +์ƒดํŽ˜์ธ 1๊ฐœ + +<ํ˜œํƒ ๋‚ด์—ญ> +ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ: -1,200์› +ํ‰์ผ ํ• ์ธ: -4,046์› +ํŠน๋ณ„ ํ• ์ธ: -1,000์› +์ฆ์ • ์ด๋ฒคํŠธ: -25,000์› + +<์ดํ˜œํƒ ๊ธˆ์•ก> +-31,246์› + +<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก> +135,754์› + +<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€> +์‚ฐํƒ€ +``` + +## ๐ŸŽฏ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๋ชฉ๋ก + +- ### ๋ฉ”๋‰ดํŒ(Menu) + - [X] ํŒ๋งค ์ค‘์ธ ๋ฉ”๋‰ด๋“ค์„ ์—ด๊ฑฐํ•˜์—ฌ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ณณ ์ž…๋‹ˆ๋‹ค. + - [X] ๊ฐ ๋ฉ”๋‰ด๋Š” ํƒ€์ž…๊ณผ ๊ฐ€๊ฒฉ ์ •๋ณด๋ฅผ ๊ฐ–๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. + - [X] ๋ฉ”๋‰ดํŒ์— ๋ฉ”๋‰ด์˜ ์กด์žฌ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. +- ### ์ฃผ๋ฌธ(OrderMenu) + - [X] ์‚ฌ์šฉ์ž ์ž…๋ ฅํ•œ ์ฃผ๋ฌธ ๋‚ด์—ญ๋“ค์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. + - [X] ์˜ˆ์™ธ์ฒ˜๋ฆฌ + - ๋ฉ”๋‰ดํŒ์— ์—†๋Š” ๋ฉ”๋‰ด๋ฅผ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ + - ์ค‘๋ณต ๋ฉ”๋‰ด๋ฅผ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ + - ์Œ๋ฃŒ ๋ฉ”๋‰ด๋งŒ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ + - ๋ฉ”๋‰ด ๊ฐฏ์ˆ˜์˜ ํ•ฉ์ด 20์„ ์ดˆ๊ณผํ•œ ๊ฒฝ์šฐ + - [X] ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„ ์ฃผ๋ฌธ ๋‚ด์—ญ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. + - [X] ์ฃผ๋ฌธํ•œ ์ด๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. + - [X] ์ด๋ฒคํŠธ ๋Œ€์ƒ์ธ ๋ฉ”๋‰ด์˜ ๊ฐฏ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. +- ### ์ด๋ฒคํŠธ(Event) + - [X] ์ง„ํ–‰ ์ค‘์ธ ์ด๋ฒคํŠธ๋“ค์„ ์—ด๊ฑฐํ•˜์—ฌ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ณณ ์ž…๋‹ˆ๋‹ค. + - [X] ๊ฐ ์ด๋ฒคํŠธ๋Š” ํ•ด๋‹นํ•˜๋Š” ์ •๋ณด๋“ค์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + - ์กฐ๊ฑด(์ฃผ๋ฌธ ์ด ๊ธˆ์•ก์œผ๋กœ ๊ฒฐ์ •๋จ) + - ์ ์šฉ ๋‚ ์งœ + - ๋Œ€์ƒ ๋ฉ”๋‰ด ๋˜๋Š” ์ฆ์ •ํ’ˆ + - ํ• ์ธ ๊ธˆ์•ก + - ์ฃผ๋ฌธ ์ด ๊ธˆ์•ก์—์„œ ํ• ์ธ ๊ธˆ์•ก + - ํ• ์ธ ์‹œ ๋ฉ”์„ธ์ง€ + - [X] ์ž…๋ ฅ๋œ ๋‚ ์งœ์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ์ด๋ฒคํŠธ๋“ค์„ ์ฐพ์Šต๋‹ˆ๋‹ค. + - [X] ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  ์ด๋ฒคํŠธ๋“ค์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. +- ### ํ˜œํƒ(Benefit) + - [X] ํ˜œํƒ ๋ฐ›์€ ๋‚ด์—ญ๋“ค์„ ์ €์žฅํ•˜๋Š” ๊ณณ ์ž…๋‹ˆ๋‹ค. + - [X] ํ• ์ธ ๋ฐ›์€ ์ด ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. + - [X] ์ด๋ฒคํŠธ ๋ณ„ ํ• ์ธ ๋ฐ›์€ ๊ธˆ์•ก์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. + - [X] ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„ ํ˜œํƒ ๋‚ด์—ญ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. + - [X] ์ฆ์ • ์ด๋ฒคํŠธ ํ• ์ธ ๊ฐ€๊ฒฉ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. + - [X] ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„ ์ฆ์ • ๋ฉ”๋‰ด ๋‚ด์—ญ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. +- ### ๋ฐฐ์ง€(Badge) + - [X] ์ด๋ฒคํŠธ ๋ฐฐ์ง€๋“ค์„ ์—ด๊ฑฐํ•˜์—ฌ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ณณ ์ž…๋‹ˆ๋‹ค. + - [X] ์ด ํ• ์ธ ๊ธˆ์•ก์œผ๋กœ ํ•ด๋‹นํ•˜๋Š” ๋ฐฐ์ง€๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. +- ### ํฌ๋ฆฌ์Šค๋งˆ์Šค ์„œ๋น„์Šค(ChristMasService) + - [X] ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๋‚ ์งœ์— ๋งž๊ฒŒ ํ˜œํƒ ๋‚ด์—ญ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. + - [X] ์ฆ์ • ๋ฉ”๋‰ด ๊ฐ€๊ฒฉ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.(ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐ์„ ์œ„ํ•ด ์‚ฌ์šฉ) + - [X] ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„ ํ•ญ๋ชฉ๋“ค์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. + - ์ฃผ๋ฌธ ๋ฉ”๋‰ด ๋‚ด์—ญ + - ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก + - ์ฆ์ • ๋ฉ”๋‰ด + - ํ˜œํƒ ๋‚ด์—ญ + - ์ดํ˜œํƒ ๊ธˆ์•ก + - ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก + - 12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€ +- ### View(InputView / OutputView) + - #### InputView + - [X] ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฉ”๋‰ด์™€ ๋‚ ์งœ๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. + - [X] ์˜ˆ์™ธ์ฒ˜๋ฆฌ + - `๋ฉ”๋‰ด์ด๋ฆ„-๋ฉ”๋‰ด๊ฐฏ์ˆ˜` ํ˜•์‹์ด ์•„๋‹Œ๊ฒฝ์šฐ + - ๊ฐ ๋ฉ”๋‰ด์˜ ๊ฐฏ์ˆ˜๊ฐ€ 1์ด์ƒ์ด ์•„๋‹Œ ๊ฒฝ์šฐ + - ๋‚ ์งœ๊ฐ€ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ + - ๋‚ ์งœ๊ฐ€ 1๊ณผ31 ์‚ฌ์ด๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ + - #### OutputView + -[X] ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž๊ฒŒ ๋‚ด์—ญ๋“ค์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. + - ์ด๋‹ˆ์…œ ๋ฉ˜ํŠธ + - ๋‚ ์งœ ์š”์ฒญ ๋ฉ”์‹œ์ง€ / ์ฃผ๋ฌธ ๋ฉ”๋‰ด ์š”์ฒญ ๋ฉ”์‹œ์ง€ + - ์ฃผ๋ฌธ ๋ฉ”๋‰ด ๋‚ด์—ญ + - ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก + - ์ฆ์ • ๋ฉ”๋‰ด + - ํ˜œํƒ ๋‚ด์—ญ + - ์ดํ˜œํƒ ๊ธˆ์•ก + - ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก + - 12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€ + +## ๐Ÿ“ฆ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ + +![img.png](package.png) + +## ๐Ÿ—บ๏ธ ํ”Œ๋กœ์šฐ ์ฐจํŠธ + +```mermaid +flowchart TD + A([Application<br>App ์‹คํ–‰]) --> B[/OutputView<br>๋‚ ์งœ ์ž…๋ ฅ ์š”์ฒญ ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ/] --> C[/InputView<br>๋‚ ์งœ ์ž…๋ ฅ/] + C --> D{Validator<br>๋‚ ์งœ ์œ ํšจ์„ฑ ๊ฒ€์ฆ} -->|NO| C + D -->|YES| E[/OutputView<br>์ฃผ๋ฌธ ๋ฉ”๋‰ด ์ž…๋ ฅ ์š”์ฒญ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ/] --> F[/InputputView<br>์ฃผ๋ฌธ ๋ฉ”๋‰ด ์ž…๋ ฅ/] + F --> G{Validator<br>์ฃผ๋ฌธ ๋ฉ”๋‰ด ์œ ํšจ์„ฑ ๊ฒ€์ฆ} -->|NO| F + G -->|YES| H[ChristmasService<br>๊ฐ ๋„๋ฉ”์ธ์œผ๋กœ ๋ถ€ํ„ฐ ํ•„์š”ํ•œ ๋‚ด์—ญ๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๊ฐ€์ ธ์˜ด] + H --> I[/OutputView<br>ํ˜œํƒ ๋‚ด์—ญ๋“ค ์ˆœ์ฐจ์ ์œผ๋กœ ์ถœ๋ ฅ/] --> J([Application<br>App ์ข…๋ฃŒ]) +``` + +## ๐Ÿงฉ ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ + +- ### domain + +![img.png](domain.png) + +--- + +- ### controller + +![img.png](controller.png) + +--- + +- ### constant + +![img.png](constant.png) \ No newline at end of file
Unknown
๋ฌธ์„œ ๊ผผ๊ผผํ•˜๊ฒŒ ์ฝ์–ด๋ดค์Šต๋‹ˆ๋‹ค! ํ™•์‹คํžˆ ์ด๋ฏธ์ง€๋กœ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์˜ ์ˆœ์„œ๋„๋ฅผ ํ™•์ธํ•˜๋‹ˆ๊นŒ ๊ฐ€์‹œ์„ฑ์ด ์ข‹๋„ค์š”:)
@@ -0,0 +1,16 @@ +package christmas.constant; + +import java.util.regex.Pattern; + +public class Constant { + + public static final String DELIMITER_COMMA = ","; + public static final String DELIMITER_HYPHEN = "-"; + public static final String SPACE = " "; + public static final String MENU_UNIT = "%d๊ฐœ"; + public static final String PRICE_UNIT = "%,d์›"; + public static final String NOTHING = "์—†์Œ"; + public static final int MENU_MAXIMUM_QUANTITY = 20; + public static final Pattern MENU_PATTERN = Pattern.compile("^[a-z|A-z|ใ„ฑ-ใ…Ž|๊ฐ€-ํžฃ|0-9|\s]+-[0-9\s]+$"); + public static final Pattern DAY_PATTERN = Pattern.compile("^[0-9\s]+$"); +}
Java
์ •๊ทœ ํ‘œํ˜„์‹๊นŒ์ง€ ํ™œ์šฉํ•ด์„œ ์ƒ์ˆ˜ ๊ด€๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ์ตœ๋Œ€ํ•œ ๋‹จ์ผ ํด๋ž˜์Šค ํ•œ์ • private ํ•„๋“œ ์ƒ์ˆ˜๋ฅผ ์ค„์ด๋ ค๊ณ  ํ•˜์‹  ๋…ธ๋ ฅ์ด ๋ณด์ž…๋‹ˆ๋‹ค. ์•„์ฃผ ์ข‹๋„ค์š”!
@@ -0,0 +1,58 @@ +package christmas.constant; + +import christmas.constant.MenuConstant.MenuType; +import christmas.domain.Menu; +import java.util.List; + +public class EventConstant { + public static class Condition { + + public static final int CASE_A = 10000; + public static final int CASE_B = 120000; + } + + public static class Days { + + public static final List<Integer> EVERY = List.of(1, 2, 3, 4, 5, 6); + public static final List<Integer> WEEKDAY = List.of(0, 3, 4, 5, 6); + public static final List<Integer> WEEKEND = List.of(1, 2); + public static final List<Integer> SPECIAL = List.of(3, 25); + + } + + public static class Target { + + public static final String WEEKDAY = MenuType.DESSERT; + public static final String WEEKEND = MenuType.MAIN; + public static final String CHRISTMAS = MenuType.NONE; + public static final String SPECIAL = MenuType.NONE; + public static final String PRESENTATION = Menu.CHAMPAGNE.getName(); + + } + + public static class Discount { + + public static final int WEEKDAY = 2023; + public static final int WEEKEND = 2023; + public static final int CHRISTMAS = 100; + public static final int SPECIAL = 0; + public static final int PRESENTATION = Menu.CHAMPAGNE.getPrice() * 1; + + } + + public static class TotalDiscount { + public static final int SPECIAL = 1000; + public static final int OTHER = 0; + } + + public static class Message { + + public static final String SPECIAL = "ํŠน๋ณ„ ํ• ์ธ:"; + public static final String WEEKDAY = "ํ‰์ผ ํ• ์ธ:"; + public static final String WEEKEND = "์ฃผ๋ง ํ• ์ธ:"; + public static final String CHRISTMAS = "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ:"; + public static final String PRESENTATION = "์ฆ์ • ์ด๋ฒคํŠธ:"; + + } + +}
Java
๋ฐฑ์˜ ์ž๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„์ž "_"๋ฅผ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๊ฐ€๋…์„ฑ์ด ์ฆ์ง„๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค:)
@@ -0,0 +1,58 @@ +package christmas.constant; + +import christmas.constant.MenuConstant.MenuType; +import christmas.domain.Menu; +import java.util.List; + +public class EventConstant { + public static class Condition { + + public static final int CASE_A = 10000; + public static final int CASE_B = 120000; + } + + public static class Days { + + public static final List<Integer> EVERY = List.of(1, 2, 3, 4, 5, 6); + public static final List<Integer> WEEKDAY = List.of(0, 3, 4, 5, 6); + public static final List<Integer> WEEKEND = List.of(1, 2); + public static final List<Integer> SPECIAL = List.of(3, 25); + + } + + public static class Target { + + public static final String WEEKDAY = MenuType.DESSERT; + public static final String WEEKEND = MenuType.MAIN; + public static final String CHRISTMAS = MenuType.NONE; + public static final String SPECIAL = MenuType.NONE; + public static final String PRESENTATION = Menu.CHAMPAGNE.getName(); + + } + + public static class Discount { + + public static final int WEEKDAY = 2023; + public static final int WEEKEND = 2023; + public static final int CHRISTMAS = 100; + public static final int SPECIAL = 0; + public static final int PRESENTATION = Menu.CHAMPAGNE.getPrice() * 1; + + } + + public static class TotalDiscount { + public static final int SPECIAL = 1000; + public static final int OTHER = 0; + } + + public static class Message { + + public static final String SPECIAL = "ํŠน๋ณ„ ํ• ์ธ:"; + public static final String WEEKDAY = "ํ‰์ผ ํ• ์ธ:"; + public static final String WEEKEND = "์ฃผ๋ง ํ• ์ธ:"; + public static final String CHRISTMAS = "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ:"; + public static final String PRESENTATION = "์ฆ์ • ์ด๋ฒคํŠธ:"; + + } + +}
Java
์ •์  ์ค‘์ฒฉ ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•ด์„œ ์ธ์Šคํ„ด์Šคํ™”๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋„ค์š”...! ์ถ”๊ฐ€๋กœ ๊ฐ ํด๋ž˜์Šค ๋ณ„๋กœ ๊ณต๋ฐฑ ์ค„๋ฐ”๊ฟˆ์„ ํ™œ์šฉํ•ด์„œ ๊ฐ€๋…์„ฑ์„ ๋†’์ด์‹  ๊ฒŒ ์•„์ฃผ ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค:)
@@ -0,0 +1,87 @@ +package christmas.controller; + +import christmas.domain.Benefit; +import christmas.domain.OrderMenu; +import christmas.service.ChristmasService; +import christmas.view.input.InputView; +import christmas.view.output.OutputView; + +public class ChristmasController { + + private final InputView inputView; + private final OutputView outputView; + private final ChristmasService christmasService; + + public ChristmasController() { + this.inputView = new InputView(); + this.outputView = new OutputView(); + this.christmasService = new ChristmasService(); + } + + public void run() { + int day = requestDay(); + OrderMenu orderMenu = requestOrderMenu(); + Benefit benefit = christmasService.getBenefit(orderMenu, day); + responseAll(orderMenu, benefit); + + } + + private int requestDay() { + outputView.printRequestDayMessage(); + return christmasService.getInputRequestResult(inputView::requestDay); + } + + private OrderMenu requestOrderMenu() { + outputView.printRequestOrderMessage(); + return christmasService.getInputRequestResult(inputView::requestOrderMenu); + } + + private void responseAll(OrderMenu orderMenu, Benefit benefit) { + outputView.printPreviewMessage(); + + responseOrderMenusDetails(orderMenu); + responseTotalPriceBeforeDiscount(orderMenu); + responsePresentationMenu(benefit); + responseBenefitDetails(benefit); + responseTotalDiscount(benefit); + responseTotalPriceAfterDiscount(orderMenu, benefit); + responseBadge(benefit); + } + + private void responseOrderMenusDetails(OrderMenu orderMenus) { + String orderMenusDetails = christmasService.getOrderMenuDetails(orderMenus); + outputView.printOrderMenusDetails(orderMenusDetails); + } + + private void responseTotalPriceBeforeDiscount(OrderMenu orderMenu) { + String totalPriceBeforeDiscount = christmasService.getTotalPriceBeforeDiscount(orderMenu); + outputView.printTotalPriceBeforeDiscount(totalPriceBeforeDiscount); + } + + private void responsePresentationMenu(Benefit benefit) { + String presentationMenu = christmasService.getPresentationMenu(benefit); + outputView.printPresentationMenu(presentationMenu); + } + + private void responseBenefitDetails(Benefit benefit) { + String benefitDetails = christmasService.getBenefitDetails(benefit); + outputView.printBenefitDetails(benefitDetails); + } + + private void responseTotalDiscount(Benefit benefit) { + String totalDiscount = christmasService.getTotalDiscount(benefit); + outputView.printTotalDiscount(totalDiscount); + } + + private void responseTotalPriceAfterDiscount(OrderMenu orderMenus, Benefit benefit) { + String totalPriceAfterDiscount = christmasService.getTotalPriceAfterDiscount(orderMenus, benefit); + outputView.printTotalPriceAfterDiscount(totalPriceAfterDiscount); + } + + private void responseBadge(Benefit benefit) { + String badge = christmasService.getBadge(benefit); + outputView.printBadge(badge); + } + + +}
Java
์š”์ฒญ๊ฐ’๋“ค์„ ์ „๋ถ€ ๋ฐ›์•„์„œ ์›ํ•˜๋Š” ๋ฆฌํ„ด๊ฐ’์„ ๋‚ด๋†“๋Š” ๋ฉ”์†Œ๋“œ ๊ฐ™์€๋ฐ ๋ช…๋ช…์ด ์กฐ๊ธˆ ์•„์‰ฌ์šด ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๋” ์ข‹์€ ๋ฉ”์†Œ๋“œ ๋ช…๋ช…์ด ์žˆ์ง€ ์•Š์•˜์„๊นŒ ์‹ถ์–ด์š”:)
@@ -0,0 +1,72 @@ +package christmas.domain; + +import christmas.constant.Constant; +import java.util.EnumMap; +import java.util.Map; +import java.util.Map.Entry; + +public class Benefit { + + private final Map<Event, Integer> details; + + public Benefit() { + this.details = new EnumMap<>(Event.class); + initialBenefitDetails(); + } + + public int calculateTotalDiscount() { + return details.values() + .stream() + .reduce(0, Integer::sum); + } + + public void addDiscountIntoDetails(Event event, int discount) { + details.computeIfPresent(event, (key, value) -> value + discount); + } + + public String detailsToString() { + StringBuilder sb = new StringBuilder(Constant.NOTHING + System.lineSeparator()); + if (calculateTotalDiscount() != 0) { + sb.setLength(0); + appendDetails(sb); + } + return sb.toString(); + } + + public int calculatePresentationPrice() { + return details.get(Event.PRESENTATION); + } + + public String presentationMenuToString() { + Event presentation = Event.PRESENTATION; + StringBuilder sb = new StringBuilder(Constant.NOTHING + System.lineSeparator()); + int discount = details.get(presentation); + if (discount != 0) { + sb.setLength(0); + sb.append(presentation.getTarget()); + sb.append(Constant.SPACE); + sb.append(String.format(Constant.MENU_UNIT, Math.abs(discount / Event.PRESENTATION.getDiscount()))); + sb.append(System.lineSeparator()); + } + return sb.toString(); + } + + + private void appendDetails(StringBuilder benefitDetail) { + for (Entry<Event, Integer> entry : details.entrySet()) { + String eventMessage = entry.getKey().getMessage(); + int discount = entry.getValue(); + if (discount != 0) { + benefitDetail.append(eventMessage); + benefitDetail.append(Constant.SPACE); + benefitDetail.append(String.format(Constant.PRICE_UNIT, discount)); + benefitDetail.append(System.lineSeparator()); + } + } + } + + private void initialBenefitDetails() { + Event.getAllEvent().forEach(event -> details.put(event, 0)); + } + +}
Java
์ดํ˜œํƒ๊ฐ’์ด 0์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋ถ„๊ธฐ์ ์œผ๋กœ ๋‘ฌ์„œ ์ถœ๋ ฅ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •์ผ€ ํ•˜๋„ค์š”! ์ˆ˜๋‹ฌ๋‹˜ ๋กœ์ง์„ ๋ณด๋‹ˆ๊นŒ ๊ตณ์ด ๊ฐ์ž ๋”ฐ๋กœ๋”ฐ๋กœ ์ƒ๊ฐํ•  ํ•„์š”๊ฐ€ ์—†์—ˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”. ์ข‹์€ ์•„์ด๋””์–ด ๊ฐ™์Šต๋‹ˆ๋‹ค:)
@@ -0,0 +1,82 @@ +package study; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Equation { + + private final String equation; + + private static final String INVALID_CALCULATION_FORMAT = "์ž˜๋ชป๋œ ๊ณ„์‚ฐ์‹์ž…๋‹ˆ๋‹ค."; + private static final String DELIMITER = " "; + + public Equation(String equation) { + this.equation = equation; + checkEquation(); + } + + public void checkEquation() { + String[] split = equation.split(DELIMITER); + + for (int i = 0; i < split.length; ) { + checkDoubleNumber(split, i); + i += 2; + } + + for (int i = 1; i < split.length; ) { + checkDoubleSymbol(split, i); + i += 2; + } + + } + + public List<Integer> getNumbers() { + List<Integer> numList = new ArrayList<>(); + String[] inputArr = equation.split(DELIMITER); + Arrays.stream(inputArr).filter(this::isParesInt).forEach(s -> addNumList(numList, s)); + return numList; + } + + public List<String> getSymbolsList() { + List<String> symbolList = new ArrayList<>(); + String[] inputArr = equation.split(DELIMITER); + Arrays.stream(inputArr).filter(s -> !isParesInt(s)) + .forEach(s -> addSymbolList(symbolList, s)); + return symbolList; + } + + private boolean isParesInt(String input) { + return input.chars().allMatch(Character::isDigit); + } + + private void addNumList(List<Integer> numList, String input) { + numList.add(Integer.parseInt(input)); + } + + private void addSymbolList(List<String> symbolList, String input) { + checkSymbol(input); + symbolList.add(input); + } + + private void checkSymbol(String input) { + if (!SymbolStatus.checkSymbol(input)) { + throw new IllegalStateException(INVALID_CALCULATION_FORMAT); + } + } + + private void checkDoubleNumber(String[] split, int i) { + boolean parseInt = isParesInt(split[i]); + if (!parseInt) { + throw new IllegalStateException(INVALID_CALCULATION_FORMAT); + } + } + + private void checkDoubleSymbol(String[] split, int i) { + boolean parse = isParesInt(split[i]); + if (parse) { + throw new IllegalStateException(INVALID_CALCULATION_FORMAT); + } + } + +}
Java
ํด๋ž˜์Šค์˜ ๊ตฌํ˜„ ์ปจ๋ฒค์…˜์— ๋งž์ถฐ์„œ ๊ตฌํ˜„ํ•˜๊ฒŒ๋˜๋ฉด ์ผ๊ด€๋œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค :) ``` class A { ์ƒ์ˆ˜(static final) ๋˜๋Š” ํด๋ž˜์Šค ๋ณ€์ˆ˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์ƒ์„ฑ์ž ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ๋ฉ”์†Œ๋“œ ๊ธฐ๋ณธ ๋ฉ”์†Œ๋“œ (equals, hashCode, toString) } ```
@@ -0,0 +1,16 @@ +package study; + +import java.util.Scanner; + +public class InputView { + + private Scanner scanner; + + public InputView(Scanner scanner) { + this.scanner = scanner; + } + + public String readEquation() { + return scanner.nextLine(); + } +}
Java
InputView์—์„œ ์ƒ์„ฑ์ž์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ Scanner๋ฅผ ์„ค์ •ํ•ด์ฃผ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ ์˜์กด์„ฑ ์ฃผ์ž…์„ ํ†ตํ•ด ์–ป๊ฒŒ๋˜๋Š” ์žฅ์ ์ด ์—†์–ด๋ณด์ด๋Š” ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿค”
@@ -0,0 +1,13 @@ +package study; + +public class ResultView { + + public void initStart() { + System.out.println("๊ณ„์‚ฐ์‹์„ ์ž…๋ ฅํ•˜์„ธ์š”."); + } + + public void viewResult(SimpleCalculator simpleCalculator) { + int result = simpleCalculator.calEquation(); + System.out.println("๊ณ„์‚ฐ ๊ฒฐ๊ณผ = " + result); + } +}
Java
๊ณ„์‚ฐ์‹์„ ์ž…๋ ฅํ•˜๋ผ๊ณ  ์ฝ˜์†”์— ์ถœ๋ ฅ๋˜๋Š” ๋‚ด์šฉ์€ InputView์— ์ข€ ๋” ์ ํ•ฉํ•ด๋ณด์ด๊ธฐ๋„ ํ•˜๋„ค์š”. InputView์™€ ResultView๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ์ด์œ ๋Š” ์š”์ฒญ๊ณผ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ๋ถ€๋ถ„์ด๊ธฐ ๋•Œ๋ฌธ์— ResultView์—์„œ๋งŒ ์ถœ๋ ฅ์„ ํ•ด์•ผํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค :)
@@ -0,0 +1,13 @@ +package study; + +public class ResultView { + + public void initStart() { + System.out.println("๊ณ„์‚ฐ์‹์„ ์ž…๋ ฅํ•˜์„ธ์š”."); + } + + public void viewResult(SimpleCalculator simpleCalculator) { + int result = simpleCalculator.calEquation(); + System.out.println("๊ณ„์‚ฐ ๊ฒฐ๊ณผ = " + result); + } +}
Java
๊ณ„์‚ฐ๊ธฐ ๊ฐ์ฒด๋ฅผ viewResult()์˜ ์ธ์ž๋กœ ๋„˜๊ธฐ๋Š” ๊ฒƒ ๋ณด๋‹ค๋Š” ๊ฒฐ๊ณผ ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”? ๊ณ„์‚ฐ๊ธฐ ๊ฐ์ฒด์™€ View ๊ฐ์ฒด๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ์ด์œ ๋Š” ์„œ๋กœ์˜ ์˜์กด์„ฑ์„ ๋Š์–ด๋‚ด๊ณ  ๋ณ€๊ฒฝ์˜ ์˜ํ–ฅ์„ ์ตœ์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ณ„์‚ฐ๊ธฐ ๊ฐ์ฒด์˜ calEquation() ๋ฉ”์„œ๋“œ์˜ ๋„ค์ด๋ฐ์ด ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค๋ฉด ResultView ์—๋„ ๋ณ€๊ฒฝ์ด ์ƒ๊ธฐ์ง€ ์•Š์„๊นŒ์š”? ```suggestion public void viewResult(int result) { System.out.println("๊ณ„์‚ฐ ๊ฒฐ๊ณผ = " + result); } ```
@@ -0,0 +1,75 @@ +package study; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +public class SimpleCalculatorTest { + + @Test + void plusTest() { + //given + String input = "10 + 5"; + Equation equation = new Equation(input); + SimpleCalculator calculator = new SimpleCalculator(equation); + + //when + int result = calculator.cal( + equation.getSymbolsList().get(0), + equation.getNumbers().get(0), + equation.getNumbers().get(1)); + + //then + assertThat(result).isEqualTo(15); + } + + @Test + void minusTest() { + //given + String input = "10 - 5"; + Equation equation = new Equation(input); + SimpleCalculator calculator = new SimpleCalculator(equation); + + //when + int result = calculator.cal( + equation.getSymbolsList().get(0), + equation.getNumbers().get(0), + equation.getNumbers().get(1)); + + //then + assertThat(result).isEqualTo(5); + } + + @Test + void multiplyTest() { + //given + String input = "10 * 5"; + Equation equation = new Equation(input); + SimpleCalculator calculator = new SimpleCalculator(equation); + //when + int result = calculator.cal( + equation.getSymbolsList().get(0), + equation.getNumbers().get(0), + equation.getNumbers().get(1)); + + //then + assertThat(result).isEqualTo(50); + } + + @Test + void divisionTest() { + //given + String input = "10 / 5"; + Equation equation = new Equation(input); + SimpleCalculator calculator = new SimpleCalculator(equation); + //when + int result = calculator.cal( + equation.getSymbolsList().get(0), + equation.getNumbers().get(0), + equation.getNumbers().get(1)); + + //then + assertThat(result).isEqualTo(2); + } + +}
Java
๊ณ„์‚ฐ๊ธฐ ํ…Œ์ŠคํŠธ์—๋„ @DisplayNameย ์–ด๋…ธํ…Œ์ด์…˜์„ ํ™œ์šฉํ•˜๋ฉด ๋” ๊ฐ€๋…์„ฑ ์žˆ๋Š” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค :)
@@ -0,0 +1,82 @@ +package study; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Equation { + + private final String equation; + + private static final String INVALID_CALCULATION_FORMAT = "์ž˜๋ชป๋œ ๊ณ„์‚ฐ์‹์ž…๋‹ˆ๋‹ค."; + private static final String DELIMITER = " "; + + public Equation(String equation) { + this.equation = equation; + checkEquation(); + } + + public void checkEquation() { + String[] split = equation.split(DELIMITER); + + for (int i = 0; i < split.length; ) { + checkDoubleNumber(split, i); + i += 2; + } + + for (int i = 1; i < split.length; ) { + checkDoubleSymbol(split, i); + i += 2; + } + + } + + public List<Integer> getNumbers() { + List<Integer> numList = new ArrayList<>(); + String[] inputArr = equation.split(DELIMITER); + Arrays.stream(inputArr).filter(this::isParesInt).forEach(s -> addNumList(numList, s)); + return numList; + } + + public List<String> getSymbolsList() { + List<String> symbolList = new ArrayList<>(); + String[] inputArr = equation.split(DELIMITER); + Arrays.stream(inputArr).filter(s -> !isParesInt(s)) + .forEach(s -> addSymbolList(symbolList, s)); + return symbolList; + } + + private boolean isParesInt(String input) { + return input.chars().allMatch(Character::isDigit); + } + + private void addNumList(List<Integer> numList, String input) { + numList.add(Integer.parseInt(input)); + } + + private void addSymbolList(List<String> symbolList, String input) { + checkSymbol(input); + symbolList.add(input); + } + + private void checkSymbol(String input) { + if (!SymbolStatus.checkSymbol(input)) { + throw new IllegalStateException(INVALID_CALCULATION_FORMAT); + } + } + + private void checkDoubleNumber(String[] split, int i) { + boolean parseInt = isParesInt(split[i]); + if (!parseInt) { + throw new IllegalStateException(INVALID_CALCULATION_FORMAT); + } + } + + private void checkDoubleSymbol(String[] split, int i) { + boolean parse = isParesInt(split[i]); + if (parse) { + throw new IllegalStateException(INVALID_CALCULATION_FORMAT); + } + } + +}
Java
์ž„์˜์˜ ์ˆซ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋งค์ง ๋„˜๋ฒ„๋Š” ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ฝ๊ธฐ ์–ด๋ ต๊ฒŒ ๋งŒ๋“œ๋Š”๋ฐ์š” ! ์ˆซ์ž 2๋Š” ์–ด๋–ค ์˜๋ฏธ์ผ๊นŒ์š”?
@@ -0,0 +1,20 @@ +package study; + +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) { + ResultView resultView = new ResultView(); + + resultView.initStart(); + Scanner scanner = new Scanner(System.in); + InputView inputView = new InputView(scanner); + + Equation equation = new Equation(inputView.readEquation()); + SimpleCalculator simpleCalculator = new SimpleCalculator(equation); + + resultView.viewResult(simpleCalculator); + + } +}
Java
ํŒŒ์ผ ๋งˆ์ง€๋ง‰์— ์—”ํ„ฐ(๊ฐœํ–‰๋ฌธ์ž)๋ฅผ ๋„ฃ์–ด์ฃผ์„ธ์š” :) ์ด์œ ๋Š” ๋ฆฌ๋ทฐ๋ฅผ ์ง„ํ–‰ํ•  ๋•Œ ๊นƒํ—ˆ๋ธŒ์—์„œ ๊ฒฝ๊ณ ๋ฉ”์‹œ์ง€๋ฅผ ์ง€์šฐ๊ณ  ํ˜น์‹œ ๋ชจ๋ฅด๋Š” ํŒŒ์ผ ์ฝ๊ธฐ ์˜ค๋ฅ˜์— ๋Œ€๋น„ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ์ข€ ๋” ์•Œ๊ณ ์‹ถ์œผ์‹œ๋ฉด ์ฐธ๊ณ  ๋งํฌ๋ฅผ ๋ณด์…”๋„ ์žฌ๋ฐŒ์„ ๊ฒƒ ๊ฐ™๋„ค์š” :) Intellij ๋ฅผ ์‚ฌ์šฉํ•˜์‹ค ๊ฒฝ์šฐ์—”Preferences -> Editor -> General -> Ensure line feed at file end on save ๋ฅผ ์ฒดํฌํ•ด์ฃผ์‹œ๋ฉดํŒŒ์ผ ์ €์žฅ ์‹œ ๋งˆ์ง€๋ง‰์— ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์ž๋™์œผ๋กœ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค! https://minz.dev/19https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline
@@ -0,0 +1,52 @@ +package study; + +public class SimpleCalculator { + + private final Equation equation; + + private static final String NO_DIVIDE_BY_ZERO = "0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."; + + public SimpleCalculator(Equation equation) { + this.equation = equation; + } + + public int cal(String symbol, Integer num1, Integer num2) { + if (symbol.equals(SymbolStatus.PLUS.toString())) { + return num1 + num2; + } + + if (symbol.equals(SymbolStatus.MINUS.toString())) { + return num1 - num2; + } + + if (symbol.equals(SymbolStatus.MULTIPLY.toString())) { + return num1 * num2; + } + + if (symbol.equals(SymbolStatus.DIVISION.toString())) { + checkDivideByZero(num2); + return num1 / num2; + } + + throw new IllegalStateException("์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค.."); + } + + private void checkDivideByZero(Integer num) { + if (num == 0) { + throw new ArithmeticException(NO_DIVIDE_BY_ZERO); + } + } + + public int calEquation() { + int cal = cal( + equation.getSymbolsList().get(0), + equation.getNumbers().get(0), + equation.getNumbers().get(1)); + + for (int i = 1; i < equation.getSymbolsList().size(); i++) { + cal = cal(equation.getSymbolsList().get(i), cal, equation.getNumbers().get(i + 1)); + } + + return cal; + } +}
Java
`๊ณ„์‚ฐ๊ธฐ`๊ฐ€ ํ•˜๋‚˜์˜ `๋ฐฉ์ •์‹`์„ ์ƒํƒœ๋กœ ๊ฐ€์ง€๋„๋ก ๊ตฌํ˜„ํ•˜์…จ๋„ค์š” ! ๊ณ„์‚ฐ๊ธฐ๊ฐ€ ๋˜ ๋‹ค๋ฅธ ๋ฐฉ์ •์‹์— ๋Œ€ํ•œ ๊ฐ’์€ ์–ด๋–ป๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ํ˜„์žฌ ์ƒํƒœ์—์„œ๋Š” ํ•˜๋‚˜์˜ ๊ณ„์‚ฐ๊ธฐ ๊ฐ์ฒด๊ฐ€ ํ•˜๋‚˜์˜ ๋ฐฉ์ •์‹๋งŒ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ์š”. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐฉ์ •์‹์— ๋Œ€ํ•ด ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐœ์„ ํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค :)
@@ -0,0 +1,52 @@ +package study; + +public class SimpleCalculator { + + private final Equation equation; + + private static final String NO_DIVIDE_BY_ZERO = "0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."; + + public SimpleCalculator(Equation equation) { + this.equation = equation; + } + + public int cal(String symbol, Integer num1, Integer num2) { + if (symbol.equals(SymbolStatus.PLUS.toString())) { + return num1 + num2; + } + + if (symbol.equals(SymbolStatus.MINUS.toString())) { + return num1 - num2; + } + + if (symbol.equals(SymbolStatus.MULTIPLY.toString())) { + return num1 * num2; + } + + if (symbol.equals(SymbolStatus.DIVISION.toString())) { + checkDivideByZero(num2); + return num1 / num2; + } + + throw new IllegalStateException("์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค.."); + } + + private void checkDivideByZero(Integer num) { + if (num == 0) { + throw new ArithmeticException(NO_DIVIDE_BY_ZERO); + } + } + + public int calEquation() { + int cal = cal( + equation.getSymbolsList().get(0), + equation.getNumbers().get(0), + equation.getNumbers().get(1)); + + for (int i = 1; i < equation.getSymbolsList().size(); i++) { + cal = cal(equation.getSymbolsList().get(i), cal, equation.getNumbers().get(i + 1)); + } + + return cal; + } +}
Java
๋ฉ”์„œ๋“œ๋‚˜ ๋ณ€์ˆ˜๋ช…์— ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ง€์–‘ํ•ด์ฃผ์„ธ์š”. ์•ฝ์–ด๋Š” ๋ณ„๋„์˜ ๊ทœ์•ฝ์œผ๋กœ ์ •ํ•ด์ง€์ง€ ์•Š์œผ๋ฉด ์†Œ์Šค์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ์–ด๋ ต๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค :) ```suggestion public int calculate(String symbol, Integer num1, Integer num2) { ```
@@ -0,0 +1,52 @@ +package study; + +public class SimpleCalculator { + + private final Equation equation; + + private static final String NO_DIVIDE_BY_ZERO = "0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."; + + public SimpleCalculator(Equation equation) { + this.equation = equation; + } + + public int cal(String symbol, Integer num1, Integer num2) { + if (symbol.equals(SymbolStatus.PLUS.toString())) { + return num1 + num2; + } + + if (symbol.equals(SymbolStatus.MINUS.toString())) { + return num1 - num2; + } + + if (symbol.equals(SymbolStatus.MULTIPLY.toString())) { + return num1 * num2; + } + + if (symbol.equals(SymbolStatus.DIVISION.toString())) { + checkDivideByZero(num2); + return num1 / num2; + } + + throw new IllegalStateException("์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค.."); + } + + private void checkDivideByZero(Integer num) { + if (num == 0) { + throw new ArithmeticException(NO_DIVIDE_BY_ZERO); + } + } + + public int calEquation() { + int cal = cal( + equation.getSymbolsList().get(0), + equation.getNumbers().get(0), + equation.getNumbers().get(1)); + + for (int i = 1; i < equation.getSymbolsList().size(); i++) { + cal = cal(equation.getSymbolsList().get(i), cal, equation.getNumbers().get(i + 1)); + } + + return cal; + } +}
Java
SymbolStatus enum ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์…จ๋„ค์š” ๐Ÿ‘ SymbolStatus๋ฅผ ํ†ตํ•ด ์—ฐ์‚ฐ๊ธฐํ˜ธ์˜ `์ƒํƒœ`๋ฅผ ํ•œ ๊ณณ์—์„œ ๊ด€๋ฆฌํ•˜๊ฒŒ ํ•ด์ฃผ์…จ์œผ๋‹ˆ ๊ณ„์‚ฐ์„ ํ•˜๋Š” `ํ–‰์œ„`๋„ ํ•จ๊ป˜ enum์— ๊ตฌํ˜„ํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? https://techblog.woowahan.com/2527/
@@ -0,0 +1,75 @@ +import pandas as pd +import numpy as np + +#์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ(์ˆ˜๊ธฐ ์ž…๋ ฅ) +videos = ['์กฐ์šฉ์›', '์ •ํƒœํ˜ธ', '์ •์šฐ๋ฏผ', '์žฅ์ง„ํฌ', '์ž„์ฑ„์€', '์ด์Šน์ˆ˜', '์ด๋‚จ์ค€', '์œค๋‹ค์†œ', '์˜ค์œ ๋ฆฌ๋‚˜', '์‹ ์ˆ˜์•„', '๋ฐฑ์†กํ•˜', '๋ฐ•์›๋นˆ', '๊น€ํ˜„์šฐ', '๊น€ํ˜„์ˆ˜', '๊น€ํƒœํ˜ธ', '๊น€ํƒœ์—ฐ', '๊น€์˜์‹', '๊น€์˜์šฐ', '๊น€์„ธํ˜•', '๊ฐ•์ง€ํ˜ธ', '๊ฐ•์ˆ˜ํ˜„'] +#Urclass ์ˆ˜๊ฐ•์ƒ ํ˜„ํ™ฉ csv ๋ถˆ๋Ÿฌ์˜ค๊ธฐ +df = pd.read_csv('1012.csv') + +def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): + """ + ์ธ์› ๋ฐฐ์น˜๋Š” ๋žœ๋ค์ด๋˜ ๊ฐ ์กฐ์— ์ œ์ถœ์ž์˜ ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๊ฒŒ๋” ํ”„๋กœ์ ํŠธ ์กฐ ์งœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค + + --ํŒŒ๋ผ๋ฏธํ„ฐ-- + * Random seed + * video = ์‹ค์ œ ์ œ์ถœ์ž ์ˆ˜ (int) -> ์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋๋‚˜ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ + * videos = ์ œ์ถœ์ž ๋ช…๋‹จ (list) + * ur_list = Urclass์—์„œ exportํ•œ ๊ธฐ์ˆ˜ ๋ช…๋‹จ (dataframe) + * group_num = ์กฐ ์ˆ˜ (int) + """ + try: + assert len(videos) == video #์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ + + #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ + non_videos = [name for name in ur_list['์ด๋ฆ„'].values if name not in videos] + + try: #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž˜ ๋ฝ‘ํ˜”๋Š”์ง€ ํ™•์ธ (ํ•œ๊ธ€์ด๋ผ ์ž˜๋ชป ๋ฝ‘ํžˆ๋Š” ๊ฒฝ์šฐ ์žˆ์Œ) + assert len(videos) + len(non_videos) == len(ur_list) + + #์กฐ ๋ฐฐ์ • ์‹œ์ž‘! + np.random.seed(random_seed) + np.random.shuffle(videos) + np.random.shuffle(non_videos) + + video = int(len(videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ์ œ์ถœ์ž ์ธ์› + non_video = int(len(non_videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ๋ฏธ์ œ์ถœ์ž ์ธ์› + groups = {} + + for i in range(group_num+1): + if i == group_num: + #1์กฐ๋ถ€ํ„ฐ ๋ฏธ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(non_videos[non_video*i:])): + groups[f'{j+1}์กฐ'].append(non_videos.pop()) + + #๋’ท ์กฐ๋ถ€ํ„ฐ ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(videos[video*i:])): + groups[f'{group_num-j}์กฐ'].append(videos.pop()) + + return groups + + group = videos[video*i:video*(i+1)] + group = group + non_videos[non_video*i:non_video*(i+1)] + groups[f'{i+1}์กฐ'] = group + + except: + print(f'๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”') + print(f'\t* ์ด ์ธ์› ์ˆ˜: {len(ur_list)}๋ช…, \n\t* ๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(non_videos)}๋ช…, \n{non_videos}, \n\t* ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(videos)}๋ช…, {videos}') + + except: + print('์ž…๋ ฅํ•œ ์ œ์ถœ์ž ์ธ์› ์ˆ˜์™€ ์ œ์ถœ์ž ๋ช…๋‹จ์— ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์ž ๋ช…๋‹จ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”') + +if __name__ == "__main__": + #๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ + groups = group_maker(random_seed=1012, video=21) + + #๋ช…๋‹จ ์ถœ๋ ฅ + for group in groups: + print(groups.get(group)) + + #์ˆ˜๊ฐ•์ƒ ์ด๋ฆ„์ด ๋น ์ง์—†์ด ๋“ค์–ด๊ฐ”๋Š”์ง€ ์žฌํ™•์ธ + j=0 + for group in groups: + i = len(groups.get(group)) + j += i + + assert j == len(df)
Python
read_csv ๋ฅผ ์ด์šฉํ•ด์„œ list ๋กœ ์ฝ๊ธฐ ์œ„ํ•จ์ด๋ผ๋ฉด, csv reader ๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”? `import pandas` ๋ฅผ ํ•˜๋ ค๋ฉด 45MB ์ •๋„์˜ pandas ๊ฐ€ ํ•„์š”ํ•œ๋Œ€์‹  csv ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด 16KB ๋งŒ์— ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,75 @@ +import pandas as pd +import numpy as np + +#์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ(์ˆ˜๊ธฐ ์ž…๋ ฅ) +videos = ['์กฐ์šฉ์›', '์ •ํƒœํ˜ธ', '์ •์šฐ๋ฏผ', '์žฅ์ง„ํฌ', '์ž„์ฑ„์€', '์ด์Šน์ˆ˜', '์ด๋‚จ์ค€', '์œค๋‹ค์†œ', '์˜ค์œ ๋ฆฌ๋‚˜', '์‹ ์ˆ˜์•„', '๋ฐฑ์†กํ•˜', '๋ฐ•์›๋นˆ', '๊น€ํ˜„์šฐ', '๊น€ํ˜„์ˆ˜', '๊น€ํƒœํ˜ธ', '๊น€ํƒœ์—ฐ', '๊น€์˜์‹', '๊น€์˜์šฐ', '๊น€์„ธํ˜•', '๊ฐ•์ง€ํ˜ธ', '๊ฐ•์ˆ˜ํ˜„'] +#Urclass ์ˆ˜๊ฐ•์ƒ ํ˜„ํ™ฉ csv ๋ถˆ๋Ÿฌ์˜ค๊ธฐ +df = pd.read_csv('1012.csv') + +def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): + """ + ์ธ์› ๋ฐฐ์น˜๋Š” ๋žœ๋ค์ด๋˜ ๊ฐ ์กฐ์— ์ œ์ถœ์ž์˜ ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๊ฒŒ๋” ํ”„๋กœ์ ํŠธ ์กฐ ์งœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค + + --ํŒŒ๋ผ๋ฏธํ„ฐ-- + * Random seed + * video = ์‹ค์ œ ์ œ์ถœ์ž ์ˆ˜ (int) -> ์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋๋‚˜ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ + * videos = ์ œ์ถœ์ž ๋ช…๋‹จ (list) + * ur_list = Urclass์—์„œ exportํ•œ ๊ธฐ์ˆ˜ ๋ช…๋‹จ (dataframe) + * group_num = ์กฐ ์ˆ˜ (int) + """ + try: + assert len(videos) == video #์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ + + #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ + non_videos = [name for name in ur_list['์ด๋ฆ„'].values if name not in videos] + + try: #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž˜ ๋ฝ‘ํ˜”๋Š”์ง€ ํ™•์ธ (ํ•œ๊ธ€์ด๋ผ ์ž˜๋ชป ๋ฝ‘ํžˆ๋Š” ๊ฒฝ์šฐ ์žˆ์Œ) + assert len(videos) + len(non_videos) == len(ur_list) + + #์กฐ ๋ฐฐ์ • ์‹œ์ž‘! + np.random.seed(random_seed) + np.random.shuffle(videos) + np.random.shuffle(non_videos) + + video = int(len(videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ์ œ์ถœ์ž ์ธ์› + non_video = int(len(non_videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ๋ฏธ์ œ์ถœ์ž ์ธ์› + groups = {} + + for i in range(group_num+1): + if i == group_num: + #1์กฐ๋ถ€ํ„ฐ ๋ฏธ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(non_videos[non_video*i:])): + groups[f'{j+1}์กฐ'].append(non_videos.pop()) + + #๋’ท ์กฐ๋ถ€ํ„ฐ ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(videos[video*i:])): + groups[f'{group_num-j}์กฐ'].append(videos.pop()) + + return groups + + group = videos[video*i:video*(i+1)] + group = group + non_videos[non_video*i:non_video*(i+1)] + groups[f'{i+1}์กฐ'] = group + + except: + print(f'๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”') + print(f'\t* ์ด ์ธ์› ์ˆ˜: {len(ur_list)}๋ช…, \n\t* ๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(non_videos)}๋ช…, \n{non_videos}, \n\t* ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(videos)}๋ช…, {videos}') + + except: + print('์ž…๋ ฅํ•œ ์ œ์ถœ์ž ์ธ์› ์ˆ˜์™€ ์ œ์ถœ์ž ๋ช…๋‹จ์— ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์ž ๋ช…๋‹จ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”') + +if __name__ == "__main__": + #๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ + groups = group_maker(random_seed=1012, video=21) + + #๋ช…๋‹จ ์ถœ๋ ฅ + for group in groups: + print(groups.get(group)) + + #์ˆ˜๊ฐ•์ƒ ์ด๋ฆ„์ด ๋น ์ง์—†์ด ๋“ค์–ด๊ฐ”๋Š”์ง€ ์žฌํ™•์ธ + j=0 + for group in groups: + i = len(groups.get(group)) + j += i + + assert j == len(df)
Python
numpy ๋„ 23 MB ์ž…๋‹ˆ๋‹ค. python ์— ๊ธฐ๋ณธ์œผ๋กœ ๋“ค์–ด๊ฐ€์žˆ๋Š” random ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์‹œ๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,75 @@ +import pandas as pd +import numpy as np + +#์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ(์ˆ˜๊ธฐ ์ž…๋ ฅ) +videos = ['์กฐ์šฉ์›', '์ •ํƒœํ˜ธ', '์ •์šฐ๋ฏผ', '์žฅ์ง„ํฌ', '์ž„์ฑ„์€', '์ด์Šน์ˆ˜', '์ด๋‚จ์ค€', '์œค๋‹ค์†œ', '์˜ค์œ ๋ฆฌ๋‚˜', '์‹ ์ˆ˜์•„', '๋ฐฑ์†กํ•˜', '๋ฐ•์›๋นˆ', '๊น€ํ˜„์šฐ', '๊น€ํ˜„์ˆ˜', '๊น€ํƒœํ˜ธ', '๊น€ํƒœ์—ฐ', '๊น€์˜์‹', '๊น€์˜์šฐ', '๊น€์„ธํ˜•', '๊ฐ•์ง€ํ˜ธ', '๊ฐ•์ˆ˜ํ˜„'] +#Urclass ์ˆ˜๊ฐ•์ƒ ํ˜„ํ™ฉ csv ๋ถˆ๋Ÿฌ์˜ค๊ธฐ +df = pd.read_csv('1012.csv') + +def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): + """ + ์ธ์› ๋ฐฐ์น˜๋Š” ๋žœ๋ค์ด๋˜ ๊ฐ ์กฐ์— ์ œ์ถœ์ž์˜ ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๊ฒŒ๋” ํ”„๋กœ์ ํŠธ ์กฐ ์งœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค + + --ํŒŒ๋ผ๋ฏธํ„ฐ-- + * Random seed + * video = ์‹ค์ œ ์ œ์ถœ์ž ์ˆ˜ (int) -> ์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋๋‚˜ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ + * videos = ์ œ์ถœ์ž ๋ช…๋‹จ (list) + * ur_list = Urclass์—์„œ exportํ•œ ๊ธฐ์ˆ˜ ๋ช…๋‹จ (dataframe) + * group_num = ์กฐ ์ˆ˜ (int) + """ + try: + assert len(videos) == video #์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ + + #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ + non_videos = [name for name in ur_list['์ด๋ฆ„'].values if name not in videos] + + try: #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž˜ ๋ฝ‘ํ˜”๋Š”์ง€ ํ™•์ธ (ํ•œ๊ธ€์ด๋ผ ์ž˜๋ชป ๋ฝ‘ํžˆ๋Š” ๊ฒฝ์šฐ ์žˆ์Œ) + assert len(videos) + len(non_videos) == len(ur_list) + + #์กฐ ๋ฐฐ์ • ์‹œ์ž‘! + np.random.seed(random_seed) + np.random.shuffle(videos) + np.random.shuffle(non_videos) + + video = int(len(videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ์ œ์ถœ์ž ์ธ์› + non_video = int(len(non_videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ๋ฏธ์ œ์ถœ์ž ์ธ์› + groups = {} + + for i in range(group_num+1): + if i == group_num: + #1์กฐ๋ถ€ํ„ฐ ๋ฏธ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(non_videos[non_video*i:])): + groups[f'{j+1}์กฐ'].append(non_videos.pop()) + + #๋’ท ์กฐ๋ถ€ํ„ฐ ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(videos[video*i:])): + groups[f'{group_num-j}์กฐ'].append(videos.pop()) + + return groups + + group = videos[video*i:video*(i+1)] + group = group + non_videos[non_video*i:non_video*(i+1)] + groups[f'{i+1}์กฐ'] = group + + except: + print(f'๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”') + print(f'\t* ์ด ์ธ์› ์ˆ˜: {len(ur_list)}๋ช…, \n\t* ๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(non_videos)}๋ช…, \n{non_videos}, \n\t* ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(videos)}๋ช…, {videos}') + + except: + print('์ž…๋ ฅํ•œ ์ œ์ถœ์ž ์ธ์› ์ˆ˜์™€ ์ œ์ถœ์ž ๋ช…๋‹จ์— ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์ž ๋ช…๋‹จ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”') + +if __name__ == "__main__": + #๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ + groups = group_maker(random_seed=1012, video=21) + + #๋ช…๋‹จ ์ถœ๋ ฅ + for group in groups: + print(groups.get(group)) + + #์ˆ˜๊ฐ•์ƒ ์ด๋ฆ„์ด ๋น ์ง์—†์ด ๋“ค์–ด๊ฐ”๋Š”์ง€ ์žฌํ™•์ธ + j=0 + for group in groups: + i = len(groups.get(group)) + j += i + + assert j == len(df)
Python
csv ํŒŒ์ผ์„ ์ฝ๋Š” ๋ถ€๋ถ„์„ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค๋ฉด ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ๋” ํŽธ๋ฆฌํ•  ๋“ฏํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,75 @@ +import pandas as pd +import numpy as np + +#์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ(์ˆ˜๊ธฐ ์ž…๋ ฅ) +videos = ['์กฐ์šฉ์›', '์ •ํƒœํ˜ธ', '์ •์šฐ๋ฏผ', '์žฅ์ง„ํฌ', '์ž„์ฑ„์€', '์ด์Šน์ˆ˜', '์ด๋‚จ์ค€', '์œค๋‹ค์†œ', '์˜ค์œ ๋ฆฌ๋‚˜', '์‹ ์ˆ˜์•„', '๋ฐฑ์†กํ•˜', '๋ฐ•์›๋นˆ', '๊น€ํ˜„์šฐ', '๊น€ํ˜„์ˆ˜', '๊น€ํƒœํ˜ธ', '๊น€ํƒœ์—ฐ', '๊น€์˜์‹', '๊น€์˜์šฐ', '๊น€์„ธํ˜•', '๊ฐ•์ง€ํ˜ธ', '๊ฐ•์ˆ˜ํ˜„'] +#Urclass ์ˆ˜๊ฐ•์ƒ ํ˜„ํ™ฉ csv ๋ถˆ๋Ÿฌ์˜ค๊ธฐ +df = pd.read_csv('1012.csv') + +def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): + """ + ์ธ์› ๋ฐฐ์น˜๋Š” ๋žœ๋ค์ด๋˜ ๊ฐ ์กฐ์— ์ œ์ถœ์ž์˜ ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๊ฒŒ๋” ํ”„๋กœ์ ํŠธ ์กฐ ์งœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค + + --ํŒŒ๋ผ๋ฏธํ„ฐ-- + * Random seed + * video = ์‹ค์ œ ์ œ์ถœ์ž ์ˆ˜ (int) -> ์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋๋‚˜ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ + * videos = ์ œ์ถœ์ž ๋ช…๋‹จ (list) + * ur_list = Urclass์—์„œ exportํ•œ ๊ธฐ์ˆ˜ ๋ช…๋‹จ (dataframe) + * group_num = ์กฐ ์ˆ˜ (int) + """ + try: + assert len(videos) == video #์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ + + #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ + non_videos = [name for name in ur_list['์ด๋ฆ„'].values if name not in videos] + + try: #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž˜ ๋ฝ‘ํ˜”๋Š”์ง€ ํ™•์ธ (ํ•œ๊ธ€์ด๋ผ ์ž˜๋ชป ๋ฝ‘ํžˆ๋Š” ๊ฒฝ์šฐ ์žˆ์Œ) + assert len(videos) + len(non_videos) == len(ur_list) + + #์กฐ ๋ฐฐ์ • ์‹œ์ž‘! + np.random.seed(random_seed) + np.random.shuffle(videos) + np.random.shuffle(non_videos) + + video = int(len(videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ์ œ์ถœ์ž ์ธ์› + non_video = int(len(non_videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ๋ฏธ์ œ์ถœ์ž ์ธ์› + groups = {} + + for i in range(group_num+1): + if i == group_num: + #1์กฐ๋ถ€ํ„ฐ ๋ฏธ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(non_videos[non_video*i:])): + groups[f'{j+1}์กฐ'].append(non_videos.pop()) + + #๋’ท ์กฐ๋ถ€ํ„ฐ ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(videos[video*i:])): + groups[f'{group_num-j}์กฐ'].append(videos.pop()) + + return groups + + group = videos[video*i:video*(i+1)] + group = group + non_videos[non_video*i:non_video*(i+1)] + groups[f'{i+1}์กฐ'] = group + + except: + print(f'๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”') + print(f'\t* ์ด ์ธ์› ์ˆ˜: {len(ur_list)}๋ช…, \n\t* ๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(non_videos)}๋ช…, \n{non_videos}, \n\t* ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(videos)}๋ช…, {videos}') + + except: + print('์ž…๋ ฅํ•œ ์ œ์ถœ์ž ์ธ์› ์ˆ˜์™€ ์ œ์ถœ์ž ๋ช…๋‹จ์— ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์ž ๋ช…๋‹จ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”') + +if __name__ == "__main__": + #๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ + groups = group_maker(random_seed=1012, video=21) + + #๋ช…๋‹จ ์ถœ๋ ฅ + for group in groups: + print(groups.get(group)) + + #์ˆ˜๊ฐ•์ƒ ์ด๋ฆ„์ด ๋น ์ง์—†์ด ๋“ค์–ด๊ฐ”๋Š”์ง€ ์žฌํ™•์ธ + j=0 + for group in groups: + i = len(groups.get(group)) + j += i + + assert j == len(df)
Python
ํ•˜๋‚˜์˜ ํ•จ์ˆ˜์—๋Š” ํ•˜๋‚˜์˜ ๋กœ์ง์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ๋ณ€๊ฒฝํ•ด๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,75 @@ +import pandas as pd +import numpy as np + +#์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ(์ˆ˜๊ธฐ ์ž…๋ ฅ) +videos = ['์กฐ์šฉ์›', '์ •ํƒœํ˜ธ', '์ •์šฐ๋ฏผ', '์žฅ์ง„ํฌ', '์ž„์ฑ„์€', '์ด์Šน์ˆ˜', '์ด๋‚จ์ค€', '์œค๋‹ค์†œ', '์˜ค์œ ๋ฆฌ๋‚˜', '์‹ ์ˆ˜์•„', '๋ฐฑ์†กํ•˜', '๋ฐ•์›๋นˆ', '๊น€ํ˜„์šฐ', '๊น€ํ˜„์ˆ˜', '๊น€ํƒœํ˜ธ', '๊น€ํƒœ์—ฐ', '๊น€์˜์‹', '๊น€์˜์šฐ', '๊น€์„ธํ˜•', '๊ฐ•์ง€ํ˜ธ', '๊ฐ•์ˆ˜ํ˜„'] +#Urclass ์ˆ˜๊ฐ•์ƒ ํ˜„ํ™ฉ csv ๋ถˆ๋Ÿฌ์˜ค๊ธฐ +df = pd.read_csv('1012.csv') + +def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): + """ + ์ธ์› ๋ฐฐ์น˜๋Š” ๋žœ๋ค์ด๋˜ ๊ฐ ์กฐ์— ์ œ์ถœ์ž์˜ ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๊ฒŒ๋” ํ”„๋กœ์ ํŠธ ์กฐ ์งœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค + + --ํŒŒ๋ผ๋ฏธํ„ฐ-- + * Random seed + * video = ์‹ค์ œ ์ œ์ถœ์ž ์ˆ˜ (int) -> ์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋๋‚˜ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ + * videos = ์ œ์ถœ์ž ๋ช…๋‹จ (list) + * ur_list = Urclass์—์„œ exportํ•œ ๊ธฐ์ˆ˜ ๋ช…๋‹จ (dataframe) + * group_num = ์กฐ ์ˆ˜ (int) + """ + try: + assert len(videos) == video #์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ + + #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ + non_videos = [name for name in ur_list['์ด๋ฆ„'].values if name not in videos] + + try: #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž˜ ๋ฝ‘ํ˜”๋Š”์ง€ ํ™•์ธ (ํ•œ๊ธ€์ด๋ผ ์ž˜๋ชป ๋ฝ‘ํžˆ๋Š” ๊ฒฝ์šฐ ์žˆ์Œ) + assert len(videos) + len(non_videos) == len(ur_list) + + #์กฐ ๋ฐฐ์ • ์‹œ์ž‘! + np.random.seed(random_seed) + np.random.shuffle(videos) + np.random.shuffle(non_videos) + + video = int(len(videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ์ œ์ถœ์ž ์ธ์› + non_video = int(len(non_videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ๋ฏธ์ œ์ถœ์ž ์ธ์› + groups = {} + + for i in range(group_num+1): + if i == group_num: + #1์กฐ๋ถ€ํ„ฐ ๋ฏธ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(non_videos[non_video*i:])): + groups[f'{j+1}์กฐ'].append(non_videos.pop()) + + #๋’ท ์กฐ๋ถ€ํ„ฐ ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(videos[video*i:])): + groups[f'{group_num-j}์กฐ'].append(videos.pop()) + + return groups + + group = videos[video*i:video*(i+1)] + group = group + non_videos[non_video*i:non_video*(i+1)] + groups[f'{i+1}์กฐ'] = group + + except: + print(f'๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”') + print(f'\t* ์ด ์ธ์› ์ˆ˜: {len(ur_list)}๋ช…, \n\t* ๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(non_videos)}๋ช…, \n{non_videos}, \n\t* ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(videos)}๋ช…, {videos}') + + except: + print('์ž…๋ ฅํ•œ ์ œ์ถœ์ž ์ธ์› ์ˆ˜์™€ ์ œ์ถœ์ž ๋ช…๋‹จ์— ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์ž ๋ช…๋‹จ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”') + +if __name__ == "__main__": + #๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ + groups = group_maker(random_seed=1012, video=21) + + #๋ช…๋‹จ ์ถœ๋ ฅ + for group in groups: + print(groups.get(group)) + + #์ˆ˜๊ฐ•์ƒ ์ด๋ฆ„์ด ๋น ์ง์—†์ด ๋“ค์–ด๊ฐ”๋Š”์ง€ ์žฌํ™•์ธ + j=0 + for group in groups: + i = len(groups.get(group)) + j += i + + assert j == len(df)
Python
์–ด๋–ค ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„๊นŒ์š”? ์—๋Ÿฌ๋ฅผ ํ™•์ธํ•ด๋ณด๊ณ  `except {??Exception}` ์œผ๋กœ ๊ด€๋ฆฌํ•ด์ฃผ์‹ ๋‹ค๋ฉด ์—๋Ÿฌ๊ด€๋ฆฌํ•˜๊ธฐ ๋” ์ˆ˜์›”ํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค !
@@ -0,0 +1,75 @@ +import pandas as pd +import numpy as np + +#์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ(์ˆ˜๊ธฐ ์ž…๋ ฅ) +videos = ['์กฐ์šฉ์›', '์ •ํƒœํ˜ธ', '์ •์šฐ๋ฏผ', '์žฅ์ง„ํฌ', '์ž„์ฑ„์€', '์ด์Šน์ˆ˜', '์ด๋‚จ์ค€', '์œค๋‹ค์†œ', '์˜ค์œ ๋ฆฌ๋‚˜', '์‹ ์ˆ˜์•„', '๋ฐฑ์†กํ•˜', '๋ฐ•์›๋นˆ', '๊น€ํ˜„์šฐ', '๊น€ํ˜„์ˆ˜', '๊น€ํƒœํ˜ธ', '๊น€ํƒœ์—ฐ', '๊น€์˜์‹', '๊น€์˜์šฐ', '๊น€์„ธํ˜•', '๊ฐ•์ง€ํ˜ธ', '๊ฐ•์ˆ˜ํ˜„'] +#Urclass ์ˆ˜๊ฐ•์ƒ ํ˜„ํ™ฉ csv ๋ถˆ๋Ÿฌ์˜ค๊ธฐ +df = pd.read_csv('1012.csv') + +def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): + """ + ์ธ์› ๋ฐฐ์น˜๋Š” ๋žœ๋ค์ด๋˜ ๊ฐ ์กฐ์— ์ œ์ถœ์ž์˜ ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๊ฒŒ๋” ํ”„๋กœ์ ํŠธ ์กฐ ์งœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค + + --ํŒŒ๋ผ๋ฏธํ„ฐ-- + * Random seed + * video = ์‹ค์ œ ์ œ์ถœ์ž ์ˆ˜ (int) -> ์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋๋‚˜ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ + * videos = ์ œ์ถœ์ž ๋ช…๋‹จ (list) + * ur_list = Urclass์—์„œ exportํ•œ ๊ธฐ์ˆ˜ ๋ช…๋‹จ (dataframe) + * group_num = ์กฐ ์ˆ˜ (int) + """ + try: + assert len(videos) == video #์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ + + #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ + non_videos = [name for name in ur_list['์ด๋ฆ„'].values if name not in videos] + + try: #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž˜ ๋ฝ‘ํ˜”๋Š”์ง€ ํ™•์ธ (ํ•œ๊ธ€์ด๋ผ ์ž˜๋ชป ๋ฝ‘ํžˆ๋Š” ๊ฒฝ์šฐ ์žˆ์Œ) + assert len(videos) + len(non_videos) == len(ur_list) + + #์กฐ ๋ฐฐ์ • ์‹œ์ž‘! + np.random.seed(random_seed) + np.random.shuffle(videos) + np.random.shuffle(non_videos) + + video = int(len(videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ์ œ์ถœ์ž ์ธ์› + non_video = int(len(non_videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ๋ฏธ์ œ์ถœ์ž ์ธ์› + groups = {} + + for i in range(group_num+1): + if i == group_num: + #1์กฐ๋ถ€ํ„ฐ ๋ฏธ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(non_videos[non_video*i:])): + groups[f'{j+1}์กฐ'].append(non_videos.pop()) + + #๋’ท ์กฐ๋ถ€ํ„ฐ ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(videos[video*i:])): + groups[f'{group_num-j}์กฐ'].append(videos.pop()) + + return groups + + group = videos[video*i:video*(i+1)] + group = group + non_videos[non_video*i:non_video*(i+1)] + groups[f'{i+1}์กฐ'] = group + + except: + print(f'๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”') + print(f'\t* ์ด ์ธ์› ์ˆ˜: {len(ur_list)}๋ช…, \n\t* ๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(non_videos)}๋ช…, \n{non_videos}, \n\t* ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(videos)}๋ช…, {videos}') + + except: + print('์ž…๋ ฅํ•œ ์ œ์ถœ์ž ์ธ์› ์ˆ˜์™€ ์ œ์ถœ์ž ๋ช…๋‹จ์— ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์ž ๋ช…๋‹จ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”') + +if __name__ == "__main__": + #๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ + groups = group_maker(random_seed=1012, video=21) + + #๋ช…๋‹จ ์ถœ๋ ฅ + for group in groups: + print(groups.get(group)) + + #์ˆ˜๊ฐ•์ƒ ์ด๋ฆ„์ด ๋น ์ง์—†์ด ๋“ค์–ด๊ฐ”๋Š”์ง€ ์žฌํ™•์ธ + j=0 + for group in groups: + i = len(groups.get(group)) + j += i + + assert j == len(df)
Python
์–ด๋–ค ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”์ง€ ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„๋“ฏํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,75 @@ +import pandas as pd +import numpy as np + +#์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ(์ˆ˜๊ธฐ ์ž…๋ ฅ) +videos = ['์กฐ์šฉ์›', '์ •ํƒœํ˜ธ', '์ •์šฐ๋ฏผ', '์žฅ์ง„ํฌ', '์ž„์ฑ„์€', '์ด์Šน์ˆ˜', '์ด๋‚จ์ค€', '์œค๋‹ค์†œ', '์˜ค์œ ๋ฆฌ๋‚˜', '์‹ ์ˆ˜์•„', '๋ฐฑ์†กํ•˜', '๋ฐ•์›๋นˆ', '๊น€ํ˜„์šฐ', '๊น€ํ˜„์ˆ˜', '๊น€ํƒœํ˜ธ', '๊น€ํƒœ์—ฐ', '๊น€์˜์‹', '๊น€์˜์šฐ', '๊น€์„ธํ˜•', '๊ฐ•์ง€ํ˜ธ', '๊ฐ•์ˆ˜ํ˜„'] +#Urclass ์ˆ˜๊ฐ•์ƒ ํ˜„ํ™ฉ csv ๋ถˆ๋Ÿฌ์˜ค๊ธฐ +df = pd.read_csv('1012.csv') + +def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): + """ + ์ธ์› ๋ฐฐ์น˜๋Š” ๋žœ๋ค์ด๋˜ ๊ฐ ์กฐ์— ์ œ์ถœ์ž์˜ ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๊ฒŒ๋” ํ”„๋กœ์ ํŠธ ์กฐ ์งœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค + + --ํŒŒ๋ผ๋ฏธํ„ฐ-- + * Random seed + * video = ์‹ค์ œ ์ œ์ถœ์ž ์ˆ˜ (int) -> ์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋๋‚˜ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ + * videos = ์ œ์ถœ์ž ๋ช…๋‹จ (list) + * ur_list = Urclass์—์„œ exportํ•œ ๊ธฐ์ˆ˜ ๋ช…๋‹จ (dataframe) + * group_num = ์กฐ ์ˆ˜ (int) + """ + try: + assert len(videos) == video #์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ + + #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ + non_videos = [name for name in ur_list['์ด๋ฆ„'].values if name not in videos] + + try: #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž˜ ๋ฝ‘ํ˜”๋Š”์ง€ ํ™•์ธ (ํ•œ๊ธ€์ด๋ผ ์ž˜๋ชป ๋ฝ‘ํžˆ๋Š” ๊ฒฝ์šฐ ์žˆ์Œ) + assert len(videos) + len(non_videos) == len(ur_list) + + #์กฐ ๋ฐฐ์ • ์‹œ์ž‘! + np.random.seed(random_seed) + np.random.shuffle(videos) + np.random.shuffle(non_videos) + + video = int(len(videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ์ œ์ถœ์ž ์ธ์› + non_video = int(len(non_videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ๋ฏธ์ œ์ถœ์ž ์ธ์› + groups = {} + + for i in range(group_num+1): + if i == group_num: + #1์กฐ๋ถ€ํ„ฐ ๋ฏธ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(non_videos[non_video*i:])): + groups[f'{j+1}์กฐ'].append(non_videos.pop()) + + #๋’ท ์กฐ๋ถ€ํ„ฐ ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(videos[video*i:])): + groups[f'{group_num-j}์กฐ'].append(videos.pop()) + + return groups + + group = videos[video*i:video*(i+1)] + group = group + non_videos[non_video*i:non_video*(i+1)] + groups[f'{i+1}์กฐ'] = group + + except: + print(f'๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”') + print(f'\t* ์ด ์ธ์› ์ˆ˜: {len(ur_list)}๋ช…, \n\t* ๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(non_videos)}๋ช…, \n{non_videos}, \n\t* ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(videos)}๋ช…, {videos}') + + except: + print('์ž…๋ ฅํ•œ ์ œ์ถœ์ž ์ธ์› ์ˆ˜์™€ ์ œ์ถœ์ž ๋ช…๋‹จ์— ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์ž ๋ช…๋‹จ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”') + +if __name__ == "__main__": + #๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ + groups = group_maker(random_seed=1012, video=21) + + #๋ช…๋‹จ ์ถœ๋ ฅ + for group in groups: + print(groups.get(group)) + + #์ˆ˜๊ฐ•์ƒ ์ด๋ฆ„์ด ๋น ์ง์—†์ด ๋“ค์–ด๊ฐ”๋Š”์ง€ ์žฌํ™•์ธ + j=0 + for group in groups: + i = len(groups.get(group)) + j += i + + assert j == len(df)
Python
์ด ๋ถ€๋ถ„๋„ ํ•จ์ˆ˜ํ™”๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š”๋ถ€๋ถ„์ด์ง€ ์•Š์„๊นŒ ์‹ถ์Šต๋‹ˆ๋‹ค !
@@ -0,0 +1,75 @@ +import pandas as pd +import numpy as np + +#์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ(์ˆ˜๊ธฐ ์ž…๋ ฅ) +videos = ['์กฐ์šฉ์›', '์ •ํƒœํ˜ธ', '์ •์šฐ๋ฏผ', '์žฅ์ง„ํฌ', '์ž„์ฑ„์€', '์ด์Šน์ˆ˜', '์ด๋‚จ์ค€', '์œค๋‹ค์†œ', '์˜ค์œ ๋ฆฌ๋‚˜', '์‹ ์ˆ˜์•„', '๋ฐฑ์†กํ•˜', '๋ฐ•์›๋นˆ', '๊น€ํ˜„์šฐ', '๊น€ํ˜„์ˆ˜', '๊น€ํƒœํ˜ธ', '๊น€ํƒœ์—ฐ', '๊น€์˜์‹', '๊น€์˜์šฐ', '๊น€์„ธํ˜•', '๊ฐ•์ง€ํ˜ธ', '๊ฐ•์ˆ˜ํ˜„'] +#Urclass ์ˆ˜๊ฐ•์ƒ ํ˜„ํ™ฉ csv ๋ถˆ๋Ÿฌ์˜ค๊ธฐ +df = pd.read_csv('1012.csv') + +def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): + """ + ์ธ์› ๋ฐฐ์น˜๋Š” ๋žœ๋ค์ด๋˜ ๊ฐ ์กฐ์— ์ œ์ถœ์ž์˜ ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๊ฒŒ๋” ํ”„๋กœ์ ํŠธ ์กฐ ์งœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค + + --ํŒŒ๋ผ๋ฏธํ„ฐ-- + * Random seed + * video = ์‹ค์ œ ์ œ์ถœ์ž ์ˆ˜ (int) -> ์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋๋‚˜ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ + * videos = ์ œ์ถœ์ž ๋ช…๋‹จ (list) + * ur_list = Urclass์—์„œ exportํ•œ ๊ธฐ์ˆ˜ ๋ช…๋‹จ (dataframe) + * group_num = ์กฐ ์ˆ˜ (int) + """ + try: + assert len(videos) == video #์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ + + #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ + non_videos = [name for name in ur_list['์ด๋ฆ„'].values if name not in videos] + + try: #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž˜ ๋ฝ‘ํ˜”๋Š”์ง€ ํ™•์ธ (ํ•œ๊ธ€์ด๋ผ ์ž˜๋ชป ๋ฝ‘ํžˆ๋Š” ๊ฒฝ์šฐ ์žˆ์Œ) + assert len(videos) + len(non_videos) == len(ur_list) + + #์กฐ ๋ฐฐ์ • ์‹œ์ž‘! + np.random.seed(random_seed) + np.random.shuffle(videos) + np.random.shuffle(non_videos) + + video = int(len(videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ์ œ์ถœ์ž ์ธ์› + non_video = int(len(non_videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ๋ฏธ์ œ์ถœ์ž ์ธ์› + groups = {} + + for i in range(group_num+1): + if i == group_num: + #1์กฐ๋ถ€ํ„ฐ ๋ฏธ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(non_videos[non_video*i:])): + groups[f'{j+1}์กฐ'].append(non_videos.pop()) + + #๋’ท ์กฐ๋ถ€ํ„ฐ ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(videos[video*i:])): + groups[f'{group_num-j}์กฐ'].append(videos.pop()) + + return groups + + group = videos[video*i:video*(i+1)] + group = group + non_videos[non_video*i:non_video*(i+1)] + groups[f'{i+1}์กฐ'] = group + + except: + print(f'๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”') + print(f'\t* ์ด ์ธ์› ์ˆ˜: {len(ur_list)}๋ช…, \n\t* ๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(non_videos)}๋ช…, \n{non_videos}, \n\t* ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(videos)}๋ช…, {videos}') + + except: + print('์ž…๋ ฅํ•œ ์ œ์ถœ์ž ์ธ์› ์ˆ˜์™€ ์ œ์ถœ์ž ๋ช…๋‹จ์— ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์ž ๋ช…๋‹จ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”') + +if __name__ == "__main__": + #๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ + groups = group_maker(random_seed=1012, video=21) + + #๋ช…๋‹จ ์ถœ๋ ฅ + for group in groups: + print(groups.get(group)) + + #์ˆ˜๊ฐ•์ƒ ์ด๋ฆ„์ด ๋น ์ง์—†์ด ๋“ค์–ด๊ฐ”๋Š”์ง€ ์žฌํ™•์ธ + j=0 + for group in groups: + i = len(groups.get(group)) + j += i + + assert j == len(df)
Python
ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด if ๋ฌธ์„ ์“ฐ๋Š”๊ฒŒ ์–ด๋–จ๊นŒ์š”?!
@@ -0,0 +1,75 @@ +import pandas as pd +import numpy as np + +#์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ(์ˆ˜๊ธฐ ์ž…๋ ฅ) +videos = ['์กฐ์šฉ์›', '์ •ํƒœํ˜ธ', '์ •์šฐ๋ฏผ', '์žฅ์ง„ํฌ', '์ž„์ฑ„์€', '์ด์Šน์ˆ˜', '์ด๋‚จ์ค€', '์œค๋‹ค์†œ', '์˜ค์œ ๋ฆฌ๋‚˜', '์‹ ์ˆ˜์•„', '๋ฐฑ์†กํ•˜', '๋ฐ•์›๋นˆ', '๊น€ํ˜„์šฐ', '๊น€ํ˜„์ˆ˜', '๊น€ํƒœํ˜ธ', '๊น€ํƒœ์—ฐ', '๊น€์˜์‹', '๊น€์˜์šฐ', '๊น€์„ธํ˜•', '๊ฐ•์ง€ํ˜ธ', '๊ฐ•์ˆ˜ํ˜„'] +#Urclass ์ˆ˜๊ฐ•์ƒ ํ˜„ํ™ฉ csv ๋ถˆ๋Ÿฌ์˜ค๊ธฐ +df = pd.read_csv('1012.csv') + +def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): + """ + ์ธ์› ๋ฐฐ์น˜๋Š” ๋žœ๋ค์ด๋˜ ๊ฐ ์กฐ์— ์ œ์ถœ์ž์˜ ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๊ฒŒ๋” ํ”„๋กœ์ ํŠธ ์กฐ ์งœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค + + --ํŒŒ๋ผ๋ฏธํ„ฐ-- + * Random seed + * video = ์‹ค์ œ ์ œ์ถœ์ž ์ˆ˜ (int) -> ์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋๋‚˜ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ + * videos = ์ œ์ถœ์ž ๋ช…๋‹จ (list) + * ur_list = Urclass์—์„œ exportํ•œ ๊ธฐ์ˆ˜ ๋ช…๋‹จ (dataframe) + * group_num = ์กฐ ์ˆ˜ (int) + """ + try: + assert len(videos) == video #์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ + + #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ + non_videos = [name for name in ur_list['์ด๋ฆ„'].values if name not in videos] + + try: #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž˜ ๋ฝ‘ํ˜”๋Š”์ง€ ํ™•์ธ (ํ•œ๊ธ€์ด๋ผ ์ž˜๋ชป ๋ฝ‘ํžˆ๋Š” ๊ฒฝ์šฐ ์žˆ์Œ) + assert len(videos) + len(non_videos) == len(ur_list) + + #์กฐ ๋ฐฐ์ • ์‹œ์ž‘! + np.random.seed(random_seed) + np.random.shuffle(videos) + np.random.shuffle(non_videos) + + video = int(len(videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ์ œ์ถœ์ž ์ธ์› + non_video = int(len(non_videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ๋ฏธ์ œ์ถœ์ž ์ธ์› + groups = {} + + for i in range(group_num+1): + if i == group_num: + #1์กฐ๋ถ€ํ„ฐ ๋ฏธ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(non_videos[non_video*i:])): + groups[f'{j+1}์กฐ'].append(non_videos.pop()) + + #๋’ท ์กฐ๋ถ€ํ„ฐ ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(videos[video*i:])): + groups[f'{group_num-j}์กฐ'].append(videos.pop()) + + return groups + + group = videos[video*i:video*(i+1)] + group = group + non_videos[non_video*i:non_video*(i+1)] + groups[f'{i+1}์กฐ'] = group + + except: + print(f'๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”') + print(f'\t* ์ด ์ธ์› ์ˆ˜: {len(ur_list)}๋ช…, \n\t* ๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(non_videos)}๋ช…, \n{non_videos}, \n\t* ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(videos)}๋ช…, {videos}') + + except: + print('์ž…๋ ฅํ•œ ์ œ์ถœ์ž ์ธ์› ์ˆ˜์™€ ์ œ์ถœ์ž ๋ช…๋‹จ์— ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์ž ๋ช…๋‹จ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”') + +if __name__ == "__main__": + #๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ + groups = group_maker(random_seed=1012, video=21) + + #๋ช…๋‹จ ์ถœ๋ ฅ + for group in groups: + print(groups.get(group)) + + #์ˆ˜๊ฐ•์ƒ ์ด๋ฆ„์ด ๋น ์ง์—†์ด ๋“ค์–ด๊ฐ”๋Š”์ง€ ์žฌํ™•์ธ + j=0 + for group in groups: + i = len(groups.get(group)) + j += i + + assert j == len(df)
Python
ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด if ๋ฌธ์„ ์“ฐ๋Š”๊ฒŒ ์–ด๋–จ๊นŒ์š”?!
@@ -0,0 +1,75 @@ +import pandas as pd +import numpy as np + +#์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ(์ˆ˜๊ธฐ ์ž…๋ ฅ) +videos = ['์กฐ์šฉ์›', '์ •ํƒœํ˜ธ', '์ •์šฐ๋ฏผ', '์žฅ์ง„ํฌ', '์ž„์ฑ„์€', '์ด์Šน์ˆ˜', '์ด๋‚จ์ค€', '์œค๋‹ค์†œ', '์˜ค์œ ๋ฆฌ๋‚˜', '์‹ ์ˆ˜์•„', '๋ฐฑ์†กํ•˜', '๋ฐ•์›๋นˆ', '๊น€ํ˜„์šฐ', '๊น€ํ˜„์ˆ˜', '๊น€ํƒœํ˜ธ', '๊น€ํƒœ์—ฐ', '๊น€์˜์‹', '๊น€์˜์šฐ', '๊น€์„ธํ˜•', '๊ฐ•์ง€ํ˜ธ', '๊ฐ•์ˆ˜ํ˜„'] +#Urclass ์ˆ˜๊ฐ•์ƒ ํ˜„ํ™ฉ csv ๋ถˆ๋Ÿฌ์˜ค๊ธฐ +df = pd.read_csv('1012.csv') + +def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): + """ + ์ธ์› ๋ฐฐ์น˜๋Š” ๋žœ๋ค์ด๋˜ ๊ฐ ์กฐ์— ์ œ์ถœ์ž์˜ ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๊ฒŒ๋” ํ”„๋กœ์ ํŠธ ์กฐ ์งœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค + + --ํŒŒ๋ผ๋ฏธํ„ฐ-- + * Random seed + * video = ์‹ค์ œ ์ œ์ถœ์ž ์ˆ˜ (int) -> ์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋๋‚˜ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ + * videos = ์ œ์ถœ์ž ๋ช…๋‹จ (list) + * ur_list = Urclass์—์„œ exportํ•œ ๊ธฐ์ˆ˜ ๋ช…๋‹จ (dataframe) + * group_num = ์กฐ ์ˆ˜ (int) + """ + try: + assert len(videos) == video #์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ + + #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ + non_videos = [name for name in ur_list['์ด๋ฆ„'].values if name not in videos] + + try: #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž˜ ๋ฝ‘ํ˜”๋Š”์ง€ ํ™•์ธ (ํ•œ๊ธ€์ด๋ผ ์ž˜๋ชป ๋ฝ‘ํžˆ๋Š” ๊ฒฝ์šฐ ์žˆ์Œ) + assert len(videos) + len(non_videos) == len(ur_list) + + #์กฐ ๋ฐฐ์ • ์‹œ์ž‘! + np.random.seed(random_seed) + np.random.shuffle(videos) + np.random.shuffle(non_videos) + + video = int(len(videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ์ œ์ถœ์ž ์ธ์› + non_video = int(len(non_videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ๋ฏธ์ œ์ถœ์ž ์ธ์› + groups = {} + + for i in range(group_num+1): + if i == group_num: + #1์กฐ๋ถ€ํ„ฐ ๋ฏธ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(non_videos[non_video*i:])): + groups[f'{j+1}์กฐ'].append(non_videos.pop()) + + #๋’ท ์กฐ๋ถ€ํ„ฐ ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(videos[video*i:])): + groups[f'{group_num-j}์กฐ'].append(videos.pop()) + + return groups + + group = videos[video*i:video*(i+1)] + group = group + non_videos[non_video*i:non_video*(i+1)] + groups[f'{i+1}์กฐ'] = group + + except: + print(f'๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”') + print(f'\t* ์ด ์ธ์› ์ˆ˜: {len(ur_list)}๋ช…, \n\t* ๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(non_videos)}๋ช…, \n{non_videos}, \n\t* ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(videos)}๋ช…, {videos}') + + except: + print('์ž…๋ ฅํ•œ ์ œ์ถœ์ž ์ธ์› ์ˆ˜์™€ ์ œ์ถœ์ž ๋ช…๋‹จ์— ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์ž ๋ช…๋‹จ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”') + +if __name__ == "__main__": + #๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ + groups = group_maker(random_seed=1012, video=21) + + #๋ช…๋‹จ ์ถœ๋ ฅ + for group in groups: + print(groups.get(group)) + + #์ˆ˜๊ฐ•์ƒ ์ด๋ฆ„์ด ๋น ์ง์—†์ด ๋“ค์–ด๊ฐ”๋Š”์ง€ ์žฌํ™•์ธ + j=0 + for group in groups: + i = len(groups.get(group)) + j += i + + assert j == len(df)
Python
try except๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ™•์ธ์šฉ์ด๋ผ๋ฉด assert ๋ฌธ์„ ํ™œ์šฉํ•˜์…”๋„ ์ข‹์„๋“ฏํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,75 @@ +import pandas as pd +import numpy as np + +#์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ(์ˆ˜๊ธฐ ์ž…๋ ฅ) +videos = ['์กฐ์šฉ์›', '์ •ํƒœํ˜ธ', '์ •์šฐ๋ฏผ', '์žฅ์ง„ํฌ', '์ž„์ฑ„์€', '์ด์Šน์ˆ˜', '์ด๋‚จ์ค€', '์œค๋‹ค์†œ', '์˜ค์œ ๋ฆฌ๋‚˜', '์‹ ์ˆ˜์•„', '๋ฐฑ์†กํ•˜', '๋ฐ•์›๋นˆ', '๊น€ํ˜„์šฐ', '๊น€ํ˜„์ˆ˜', '๊น€ํƒœํ˜ธ', '๊น€ํƒœ์—ฐ', '๊น€์˜์‹', '๊น€์˜์šฐ', '๊น€์„ธํ˜•', '๊ฐ•์ง€ํ˜ธ', '๊ฐ•์ˆ˜ํ˜„'] +#Urclass ์ˆ˜๊ฐ•์ƒ ํ˜„ํ™ฉ csv ๋ถˆ๋Ÿฌ์˜ค๊ธฐ +df = pd.read_csv('1012.csv') + +def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): + """ + ์ธ์› ๋ฐฐ์น˜๋Š” ๋žœ๋ค์ด๋˜ ๊ฐ ์กฐ์— ์ œ์ถœ์ž์˜ ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๊ฒŒ๋” ํ”„๋กœ์ ํŠธ ์กฐ ์งœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค + + --ํŒŒ๋ผ๋ฏธํ„ฐ-- + * Random seed + * video = ์‹ค์ œ ์ œ์ถœ์ž ์ˆ˜ (int) -> ์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋๋‚˜ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ + * videos = ์ œ์ถœ์ž ๋ช…๋‹จ (list) + * ur_list = Urclass์—์„œ exportํ•œ ๊ธฐ์ˆ˜ ๋ช…๋‹จ (dataframe) + * group_num = ์กฐ ์ˆ˜ (int) + """ + try: + assert len(videos) == video #์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ + + #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ + non_videos = [name for name in ur_list['์ด๋ฆ„'].values if name not in videos] + + try: #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž˜ ๋ฝ‘ํ˜”๋Š”์ง€ ํ™•์ธ (ํ•œ๊ธ€์ด๋ผ ์ž˜๋ชป ๋ฝ‘ํžˆ๋Š” ๊ฒฝ์šฐ ์žˆ์Œ) + assert len(videos) + len(non_videos) == len(ur_list) + + #์กฐ ๋ฐฐ์ • ์‹œ์ž‘! + np.random.seed(random_seed) + np.random.shuffle(videos) + np.random.shuffle(non_videos) + + video = int(len(videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ์ œ์ถœ์ž ์ธ์› + non_video = int(len(non_videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ๋ฏธ์ œ์ถœ์ž ์ธ์› + groups = {} + + for i in range(group_num+1): + if i == group_num: + #1์กฐ๋ถ€ํ„ฐ ๋ฏธ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(non_videos[non_video*i:])): + groups[f'{j+1}์กฐ'].append(non_videos.pop()) + + #๋’ท ์กฐ๋ถ€ํ„ฐ ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(videos[video*i:])): + groups[f'{group_num-j}์กฐ'].append(videos.pop()) + + return groups + + group = videos[video*i:video*(i+1)] + group = group + non_videos[non_video*i:non_video*(i+1)] + groups[f'{i+1}์กฐ'] = group + + except: + print(f'๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”') + print(f'\t* ์ด ์ธ์› ์ˆ˜: {len(ur_list)}๋ช…, \n\t* ๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(non_videos)}๋ช…, \n{non_videos}, \n\t* ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(videos)}๋ช…, {videos}') + + except: + print('์ž…๋ ฅํ•œ ์ œ์ถœ์ž ์ธ์› ์ˆ˜์™€ ์ œ์ถœ์ž ๋ช…๋‹จ์— ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์ž ๋ช…๋‹จ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”') + +if __name__ == "__main__": + #๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ + groups = group_maker(random_seed=1012, video=21) + + #๋ช…๋‹จ ์ถœ๋ ฅ + for group in groups: + print(groups.get(group)) + + #์ˆ˜๊ฐ•์ƒ ์ด๋ฆ„์ด ๋น ์ง์—†์ด ๋“ค์–ด๊ฐ”๋Š”์ง€ ์žฌํ™•์ธ + j=0 + for group in groups: + i = len(groups.get(group)) + j += i + + assert j == len(df)
Python
`def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): ` # ํ•จ์ˆ˜๊ฐ€ ์„ ์–ธ๋˜๋Š” ๋ถ€๋ถ„์—๋Š” `video=len(videos)`์ฒ˜๋Ÿผ ๋‚ด์žฅํ•จ์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. # ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ ํ•จ์ˆ˜ ๋‚ด๋ถ€์— `video` ๋ณ€์ˆ˜๊ฐ’์„ ๋ฐ›์•„์˜ค๋Š” ๊ณ„์‚ฐ์‹์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. # ์™œ๋ƒํ•˜๋ฉด ํ•จ์ˆ˜์„ ์–ธ๋ถ€์— ๊ณ„์‚ฐ์‹์ด๋‚˜ ๋‚ด์žฅํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด `project_group.py์ฝ”๋“œ`๊ฐ€ ํ˜ธ์ถœ๋˜๊ฑฐ๋‚˜ `group_makerํ•จ์ˆ˜`๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ถ”๊ฐ€์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์†Œ๋น„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,75 @@ +import pandas as pd +import numpy as np + +#์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ(์ˆ˜๊ธฐ ์ž…๋ ฅ) +videos = ['์กฐ์šฉ์›', '์ •ํƒœํ˜ธ', '์ •์šฐ๋ฏผ', '์žฅ์ง„ํฌ', '์ž„์ฑ„์€', '์ด์Šน์ˆ˜', '์ด๋‚จ์ค€', '์œค๋‹ค์†œ', '์˜ค์œ ๋ฆฌ๋‚˜', '์‹ ์ˆ˜์•„', '๋ฐฑ์†กํ•˜', '๋ฐ•์›๋นˆ', '๊น€ํ˜„์šฐ', '๊น€ํ˜„์ˆ˜', '๊น€ํƒœํ˜ธ', '๊น€ํƒœ์—ฐ', '๊น€์˜์‹', '๊น€์˜์šฐ', '๊น€์„ธํ˜•', '๊ฐ•์ง€ํ˜ธ', '๊ฐ•์ˆ˜ํ˜„'] +#Urclass ์ˆ˜๊ฐ•์ƒ ํ˜„ํ™ฉ csv ๋ถˆ๋Ÿฌ์˜ค๊ธฐ +df = pd.read_csv('1012.csv') + +def group_maker(random_seed, video=len(videos), videos=videos, ur_list=df, group_num=7): + """ + ์ธ์› ๋ฐฐ์น˜๋Š” ๋žœ๋ค์ด๋˜ ๊ฐ ์กฐ์— ์ œ์ถœ์ž์˜ ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๊ฒŒ๋” ํ”„๋กœ์ ํŠธ ์กฐ ์งœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค + + --ํŒŒ๋ผ๋ฏธํ„ฐ-- + * Random seed + * video = ์‹ค์ œ ์ œ์ถœ์ž ์ˆ˜ (int) -> ์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋๋‚˜ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ + * videos = ์ œ์ถœ์ž ๋ช…๋‹จ (list) + * ur_list = Urclass์—์„œ exportํ•œ ๊ธฐ์ˆ˜ ๋ช…๋‹จ (dataframe) + * group_num = ์กฐ ์ˆ˜ (int) + """ + try: + assert len(videos) == video #์ œ์ถœ์ž ๋ช…๋‹จ์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ + + #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ + non_videos = [name for name in ur_list['์ด๋ฆ„'].values if name not in videos] + + try: #๋ฏธ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž˜ ๋ฝ‘ํ˜”๋Š”์ง€ ํ™•์ธ (ํ•œ๊ธ€์ด๋ผ ์ž˜๋ชป ๋ฝ‘ํžˆ๋Š” ๊ฒฝ์šฐ ์žˆ์Œ) + assert len(videos) + len(non_videos) == len(ur_list) + + #์กฐ ๋ฐฐ์ • ์‹œ์ž‘! + np.random.seed(random_seed) + np.random.shuffle(videos) + np.random.shuffle(non_videos) + + video = int(len(videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ์ œ์ถœ์ž ์ธ์› + non_video = int(len(non_videos)/group_num) #ํ•œ ์กฐ ์ตœ์†Œ ๋ฏธ์ œ์ถœ์ž ์ธ์› + groups = {} + + for i in range(group_num+1): + if i == group_num: + #1์กฐ๋ถ€ํ„ฐ ๋ฏธ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(non_videos[non_video*i:])): + groups[f'{j+1}์กฐ'].append(non_videos.pop()) + + #๋’ท ์กฐ๋ถ€ํ„ฐ ์ œ์ถœ์ž ๋‚˜๋จธ์ง€ ์ธ์› ๋ฐฐ์น˜ + for j in range(len(videos[video*i:])): + groups[f'{group_num-j}์กฐ'].append(videos.pop()) + + return groups + + group = videos[video*i:video*(i+1)] + group = group + non_videos[non_video*i:non_video*(i+1)] + groups[f'{i+1}์กฐ'] = group + + except: + print(f'๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”') + print(f'\t* ์ด ์ธ์› ์ˆ˜: {len(ur_list)}๋ช…, \n\t* ๋ฏธ ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(non_videos)}๋ช…, \n{non_videos}, \n\t* ์ œ์ถœ์ž ๋ฆฌ์ŠคํŠธ: {len(videos)}๋ช…, {videos}') + + except: + print('์ž…๋ ฅํ•œ ์ œ์ถœ์ž ์ธ์› ์ˆ˜์™€ ์ œ์ถœ์ž ๋ช…๋‹จ์— ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์ž ๋ช…๋‹จ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”') + +if __name__ == "__main__": + #๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ + groups = group_maker(random_seed=1012, video=21) + + #๋ช…๋‹จ ์ถœ๋ ฅ + for group in groups: + print(groups.get(group)) + + #์ˆ˜๊ฐ•์ƒ ์ด๋ฆ„์ด ๋น ์ง์—†์ด ๋“ค์–ด๊ฐ”๋Š”์ง€ ์žฌํ™•์ธ + j=0 + for group in groups: + i = len(groups.get(group)) + j += i + + assert j == len(df)
Python
โ—๏ธ ์œ„์— `ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด if ๋ฌธ์„ ์“ฐ๋Š”๊ฒŒ ์–ด๋–จ๊นŒ์š”?!` ๋ผ๊ณ  ๋‚จ๊ฒผ๋Š”๋ฐ, ๋” ์ฐพ์•„๋ณด๋‹ˆ ์–ด๋–ค ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•˜๊ณ , ๊ทธ ์ƒํ™ฉ์ด `AssertError` ๋กœ ๊ด€๋ฆฌ๋˜์–ด์•ผํ•œ๋‹ค๋ฉด if ๋ณด๋‹ค assert ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ๋” ๊ฐ„๊ฒฐํ•˜๊ณ , ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œํ˜„๋  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š” ใ…‡0ใ…‡ ์ œ ์ฝ”๋ฉ˜ํŠธ๋Š” ๋ฌด์‹œํ•˜์…”๋„ ์ข‹์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,11 @@ +import styled from 'styled-components'; + +export const HeaderWrapper = styled.header` + display: flex; + justify-content: space-between; + align-items: center; + width: 100%; + height: ${({ theme }) => theme.boxHeight}; + padding: 0px 24px; + background: ${({ theme }) => theme.color.primary.main}; +`;
Unknown
์ด ๊ตฌํ˜„๋„ ์ •๋ง ์ข‹์€๋ฐ์š”~ ๋งŽ์ด ๋‚ด๋ ค๊ฐ„ ์ƒํƒœ์—์„œ ํ—ค๋”๋ฅผ ๋‹ค์‹œ ๋ณด๋ ค๋ฉด ์œ„๋กœ ๋งŽ์ด ์˜ฌ๋ผ๊ฐ€์•ผํ•ด์„œ ์–ด๋ ค์šธ ๊ฒƒ ๊ฐ™์•„์š” ๋ฌดํ•œ ํŽ˜์ด์ง€ ํŠน์„ฑ์ƒ position:sticky ์˜ต์…˜์„ ํ™œ์šฉํ•˜์—ฌ ๋„ค๋น„๊ฒŒ์ด์…˜๋ฐ”๊ฐ€ ๋œฌ๋‹ค๋ฉด UX๊ฒฝํ—˜์ด ๋” ์ข‹์•„์งˆ ๊ฑฐ๊ฐ™์•„์š”.
@@ -0,0 +1,146 @@ +import { CartStoreState, User } from 'types/index'; +import { useDispatch, useSelector } from 'react-redux'; + +import Link from 'components/@shared/Link'; +import Logo from 'components/Logo/Logo'; +import PATH from 'constants/path'; +import RightMenu from './RightMenu'; +import { isLogin } from 'utils/auth'; +import styled from 'styled-components'; +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; +import { userActions } from 'redux/actions'; + +function Header() { + const navigate = useNavigate(); + const dispatch = useDispatch(); + + const cart = useSelector( + (state: { cart: CartStoreState }) => state.cart.cart, + ); + const userName = useSelector((state: { user: User }) => state.user.username); + + const [showUserToggle, setShowUserToggle] = useState(false); + + const onClickLogoutButton = () => { + dispatch(userActions.resetUser()); + + localStorage.removeItem('accessToken'); + sessionStorage.removeItem('accessToken'); + + navigate(PATH.BASE); + }; + + const onClickEditUserInfoButton = () => { + navigate(PATH.EDIT_USER_INFO); + }; + + return ( + <> + <StyledHeader> + <Link to={PATH.BASE}> + <Logo /> + </Link> + <RightMenu> + <Link to={PATH.CART}> + ์žฅ๋ฐ”๊ตฌ๋‹ˆ + <Badge>{cart.length}</Badge> + </Link> + <Link to={PATH.BASE}>์ฃผ๋ฌธ๋ชฉ๋ก</Link> + </RightMenu> + </StyledHeader> + <StyledSubHeader> + <RightMenu gap="30px"> + {!isLogin() ? ( + <> + <Link to={PATH.LOGIN}>๋กœ๊ทธ์ธ</Link> + <Link to={PATH.SIGNUP}>ํšŒ์›๊ฐ€์ž…</Link> + </> + ) : ( + <> + {userName}๋‹˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค + <StyledControlUserButton onClick={onClickLogoutButton}> + ๋กœ๊ทธ์•„์›ƒ + </StyledControlUserButton> + <StyledControlUserButton onClick={onClickEditUserInfoButton}> + ํšŒ์› ์ •๋ณด ์ˆ˜์ • + </StyledControlUserButton> + </> + )} + </RightMenu> + </StyledSubHeader> + </> + ); +} + +const StyledHeader = styled.div` + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; + position: sticky; + + font-size: 20px; + height: 60px; + padding: 0 10%; + top: 0px; + z-index: ${({ theme: { zPriorities } }) => zPriorities.overEverything}; + + background: ${({ theme: { colors } }) => colors.redPink}; + color: ${({ theme: { colors } }) => colors.white}; + + ${RightMenu} { + text-shadow: -0.5px 0 ${({ theme: { colors } }) => colors.gray}, + 0 0.5px ${({ theme: { colors } }) => colors.gray}, + 0.5px 0 ${({ theme: { colors } }) => colors.gray}, + 0 -0.5px ${({ theme: { colors } }) => colors.gray}; + } +`; + +const Badge = styled.div` + display: inline-block; + position: absolute; + top: 10px; + text-align: center; + + width: 15px; + height: 15px; + border: 0.5px solid ${({ theme: { colors } }) => colors.white}; + border-radius: 50%; + + background: ${({ theme: { colors } }) => colors.pink}; + color: ${({ theme: { colors } }) => colors.black}; + + font-size: 14px; + font-weight: normal !important; +`; + +const StyledSubHeader = styled.div` + display: flex; + flex-direction: row; + justify-content: flex-end; + align-items: center; + position: sticky; + + font-size: 16px; + height: 24px; + padding: 0 10%; + top: 60px; + z-index: ${({ theme: { zPriorities } }) => zPriorities.overEverything}; + + background: ${({ theme: { colors } }) => colors.white}; + color: ${({ theme: { colors } }) => colors.black}; +`; + +const StyledControlUserButton = styled.button` + border-radius: 12px; + padding: 0 12px; + + background-color: ${({ theme: { colors } }) => colors.pink}; + color: ${({ theme: { colors } }) => colors.white}; + + font-weight: 800; + font-size: 14px; +`; + +export default Header;
Unknown
state.cart.cart๋Š” ์‚ด์ง ์•„์‰ฌ์šด ๋„ค์ด๋ฐ์ธ๊ฒƒ ๊ฐ™์•„์š”! items ์ •๋„๋ฉด ๊ดœ์ฐฎ์€ ๋„ค์ด๋ฐ์ด์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•ด๋ด…๋‹ˆ๋‹ค ๐Ÿ™
@@ -0,0 +1,146 @@ +import { CartStoreState, User } from 'types/index'; +import { useDispatch, useSelector } from 'react-redux'; + +import Link from 'components/@shared/Link'; +import Logo from 'components/Logo/Logo'; +import PATH from 'constants/path'; +import RightMenu from './RightMenu'; +import { isLogin } from 'utils/auth'; +import styled from 'styled-components'; +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; +import { userActions } from 'redux/actions'; + +function Header() { + const navigate = useNavigate(); + const dispatch = useDispatch(); + + const cart = useSelector( + (state: { cart: CartStoreState }) => state.cart.cart, + ); + const userName = useSelector((state: { user: User }) => state.user.username); + + const [showUserToggle, setShowUserToggle] = useState(false); + + const onClickLogoutButton = () => { + dispatch(userActions.resetUser()); + + localStorage.removeItem('accessToken'); + sessionStorage.removeItem('accessToken'); + + navigate(PATH.BASE); + }; + + const onClickEditUserInfoButton = () => { + navigate(PATH.EDIT_USER_INFO); + }; + + return ( + <> + <StyledHeader> + <Link to={PATH.BASE}> + <Logo /> + </Link> + <RightMenu> + <Link to={PATH.CART}> + ์žฅ๋ฐ”๊ตฌ๋‹ˆ + <Badge>{cart.length}</Badge> + </Link> + <Link to={PATH.BASE}>์ฃผ๋ฌธ๋ชฉ๋ก</Link> + </RightMenu> + </StyledHeader> + <StyledSubHeader> + <RightMenu gap="30px"> + {!isLogin() ? ( + <> + <Link to={PATH.LOGIN}>๋กœ๊ทธ์ธ</Link> + <Link to={PATH.SIGNUP}>ํšŒ์›๊ฐ€์ž…</Link> + </> + ) : ( + <> + {userName}๋‹˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค + <StyledControlUserButton onClick={onClickLogoutButton}> + ๋กœ๊ทธ์•„์›ƒ + </StyledControlUserButton> + <StyledControlUserButton onClick={onClickEditUserInfoButton}> + ํšŒ์› ์ •๋ณด ์ˆ˜์ • + </StyledControlUserButton> + </> + )} + </RightMenu> + </StyledSubHeader> + </> + ); +} + +const StyledHeader = styled.div` + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; + position: sticky; + + font-size: 20px; + height: 60px; + padding: 0 10%; + top: 0px; + z-index: ${({ theme: { zPriorities } }) => zPriorities.overEverything}; + + background: ${({ theme: { colors } }) => colors.redPink}; + color: ${({ theme: { colors } }) => colors.white}; + + ${RightMenu} { + text-shadow: -0.5px 0 ${({ theme: { colors } }) => colors.gray}, + 0 0.5px ${({ theme: { colors } }) => colors.gray}, + 0.5px 0 ${({ theme: { colors } }) => colors.gray}, + 0 -0.5px ${({ theme: { colors } }) => colors.gray}; + } +`; + +const Badge = styled.div` + display: inline-block; + position: absolute; + top: 10px; + text-align: center; + + width: 15px; + height: 15px; + border: 0.5px solid ${({ theme: { colors } }) => colors.white}; + border-radius: 50%; + + background: ${({ theme: { colors } }) => colors.pink}; + color: ${({ theme: { colors } }) => colors.black}; + + font-size: 14px; + font-weight: normal !important; +`; + +const StyledSubHeader = styled.div` + display: flex; + flex-direction: row; + justify-content: flex-end; + align-items: center; + position: sticky; + + font-size: 16px; + height: 24px; + padding: 0 10%; + top: 60px; + z-index: ${({ theme: { zPriorities } }) => zPriorities.overEverything}; + + background: ${({ theme: { colors } }) => colors.white}; + color: ${({ theme: { colors } }) => colors.black}; +`; + +const StyledControlUserButton = styled.button` + border-radius: 12px; + padding: 0 12px; + + background-color: ${({ theme: { colors } }) => colors.pink}; + color: ${({ theme: { colors } }) => colors.white}; + + font-weight: 800; + font-size: 14px; +`; + +export default Header;
Unknown
showUserToggle์€ ์–ด๋””์—์„œ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜์ธ๊ฐ€์š”? ์ฐพ์•„๋ณด๊ณ  ์žˆ๋Š”๋ฐ ์ž˜ ์•ˆ ๋ณด์ด๋„ค์š” ๐Ÿ˜ฅ
@@ -0,0 +1,146 @@ +import { CartStoreState, User } from 'types/index'; +import { useDispatch, useSelector } from 'react-redux'; + +import Link from 'components/@shared/Link'; +import Logo from 'components/Logo/Logo'; +import PATH from 'constants/path'; +import RightMenu from './RightMenu'; +import { isLogin } from 'utils/auth'; +import styled from 'styled-components'; +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; +import { userActions } from 'redux/actions'; + +function Header() { + const navigate = useNavigate(); + const dispatch = useDispatch(); + + const cart = useSelector( + (state: { cart: CartStoreState }) => state.cart.cart, + ); + const userName = useSelector((state: { user: User }) => state.user.username); + + const [showUserToggle, setShowUserToggle] = useState(false); + + const onClickLogoutButton = () => { + dispatch(userActions.resetUser()); + + localStorage.removeItem('accessToken'); + sessionStorage.removeItem('accessToken'); + + navigate(PATH.BASE); + }; + + const onClickEditUserInfoButton = () => { + navigate(PATH.EDIT_USER_INFO); + }; + + return ( + <> + <StyledHeader> + <Link to={PATH.BASE}> + <Logo /> + </Link> + <RightMenu> + <Link to={PATH.CART}> + ์žฅ๋ฐ”๊ตฌ๋‹ˆ + <Badge>{cart.length}</Badge> + </Link> + <Link to={PATH.BASE}>์ฃผ๋ฌธ๋ชฉ๋ก</Link> + </RightMenu> + </StyledHeader> + <StyledSubHeader> + <RightMenu gap="30px"> + {!isLogin() ? ( + <> + <Link to={PATH.LOGIN}>๋กœ๊ทธ์ธ</Link> + <Link to={PATH.SIGNUP}>ํšŒ์›๊ฐ€์ž…</Link> + </> + ) : ( + <> + {userName}๋‹˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค + <StyledControlUserButton onClick={onClickLogoutButton}> + ๋กœ๊ทธ์•„์›ƒ + </StyledControlUserButton> + <StyledControlUserButton onClick={onClickEditUserInfoButton}> + ํšŒ์› ์ •๋ณด ์ˆ˜์ • + </StyledControlUserButton> + </> + )} + </RightMenu> + </StyledSubHeader> + </> + ); +} + +const StyledHeader = styled.div` + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; + position: sticky; + + font-size: 20px; + height: 60px; + padding: 0 10%; + top: 0px; + z-index: ${({ theme: { zPriorities } }) => zPriorities.overEverything}; + + background: ${({ theme: { colors } }) => colors.redPink}; + color: ${({ theme: { colors } }) => colors.white}; + + ${RightMenu} { + text-shadow: -0.5px 0 ${({ theme: { colors } }) => colors.gray}, + 0 0.5px ${({ theme: { colors } }) => colors.gray}, + 0.5px 0 ${({ theme: { colors } }) => colors.gray}, + 0 -0.5px ${({ theme: { colors } }) => colors.gray}; + } +`; + +const Badge = styled.div` + display: inline-block; + position: absolute; + top: 10px; + text-align: center; + + width: 15px; + height: 15px; + border: 0.5px solid ${({ theme: { colors } }) => colors.white}; + border-radius: 50%; + + background: ${({ theme: { colors } }) => colors.pink}; + color: ${({ theme: { colors } }) => colors.black}; + + font-size: 14px; + font-weight: normal !important; +`; + +const StyledSubHeader = styled.div` + display: flex; + flex-direction: row; + justify-content: flex-end; + align-items: center; + position: sticky; + + font-size: 16px; + height: 24px; + padding: 0 10%; + top: 60px; + z-index: ${({ theme: { zPriorities } }) => zPriorities.overEverything}; + + background: ${({ theme: { colors } }) => colors.white}; + color: ${({ theme: { colors } }) => colors.black}; +`; + +const StyledControlUserButton = styled.button` + border-radius: 12px; + padding: 0 12px; + + background-color: ${({ theme: { colors } }) => colors.pink}; + color: ${({ theme: { colors } }) => colors.white}; + + font-weight: 800; + font-size: 14px; +`; + +export default Header;
Unknown
localStorage์™€ sessionStorage๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•œ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,143 @@ +import CheckBox from 'components/@shared/CheckBox'; +import Link from 'components/@shared/Link'; +import PATH from 'constants/path'; +import { USER_MESSAGE } from 'constants/message'; +import authAPI from 'apis/auth'; +import { createInputValueGetter } from 'utils/dom'; +import styled from 'styled-components'; +import { useDispatch } from 'react-redux'; +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; +import { userActions } from 'redux/actions'; + +function LoginForm() { + const dispatch = useDispatch(); + const navigate = useNavigate(); + + const [checked, setChecked] = useState(false); + + const toggleChecked = ( + e: React.MouseEvent<HTMLElement> | React.ChangeEvent<HTMLElement>, + ) => { + e.preventDefault(); + + setChecked(prevState => !prevState); + }; + + const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { + e.preventDefault(); + if (!(e.target instanceof HTMLFormElement)) return; + + const formElement = e.target.elements; + const getInputValue = createInputValueGetter(formElement); + const user = { + username: getInputValue('id'), + password: getInputValue('password'), + }; + + try { + const userInfo = await authAPI.login(user, checked); + + dispatch(userActions.setUser(userInfo)); + navigate(PATH.BASE); + } catch (error) { + if (error instanceof Error) { + alert(USER_MESSAGE.FAIL_LOGIN); + } + } + }; + + return ( + <StyledForm onSubmit={handleSubmit}> + <label htmlFor="id">์•„์ด๋””</label> + <input id="id" type="text" placeholder="์•„์ด๋””๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”" required /> + <label htmlFor="password">๋น„๋ฐ€๋ฒˆํ˜ธ</label> + <input + id="password" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”" + required + /> + <StyledLoginHelper> + <StyledKeepLogin> + <CheckBox + id="keep-login" + checked={checked} + onChange={toggleChecked} + marginBottom="0px" + /> + <label htmlFor="keep-login">๋กœ๊ทธ์ธ ์ƒํƒœ ์œ ์ง€</label> + </StyledKeepLogin> + <StyledFindLoginInfo> + <Link to="#">์•„์ด๋”” ์ฐพ๊ธฐ</Link> + <Link to="#">๋น„๋ฐ€๋ฒˆํ˜ธ ์ฐพ๊ธฐ</Link> + </StyledFindLoginInfo> + </StyledLoginHelper> + <StyledLoginButton type="submit">๋กœ๊ทธ์ธ</StyledLoginButton> + </StyledForm> + ); +} + +const StyledForm = styled.form` + display: flex; + flex-direction: column; + gap: 12px; + + width: 100%; + + > label { + margin-top: 10px; + font-size: 14px; + } + + > input { + border: 1px solid ${({ theme: { colors } }) => colors.lightGray}; + border-radius: 2px; + padding: 6px 8px; + } +`; + +const StyledLoginHelper = styled.div` + display: flex; + justify-content: space-between; + margin-top: 4px; + width: 100%; +`; + +const StyledKeepLogin = styled.div` + display: flex; + align-items: center; + gap: 5px; + + > label { + font-size: 10px; + } +`; + +const StyledFindLoginInfo = styled.div` + display: flex; + align-items: center; + gap: 10px; + + color: ${({ theme: { colors } }) => colors.gray}; + + font-size: 10px; + + a:hover { + font-weight: 900; + } +`; + +const StyledLoginButton = styled.button` + background: ${({ theme: { colors } }) => colors.redPink}; + color: ${({ theme: { colors } }) => colors.white}; + border-radius: 5px; + + height: 40px; + margin-top: 20px; + + font-size: 17px; + font-weight: 900; +`; + +export default LoginForm;
Unknown
์‹œ๊ฐ„์ด ๋œ๋‹ค๋ฉด import ๋ฌธ๋“ค์˜ ์ˆœ์„œ๋ฅผ ์ •๋ฆฌํ•ด์ฃผ๋Š”๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”! hooks๋Š” ๋ณดํ†ต ์ตœ์ƒ์œ„์—์„œ ๋ถˆ๋Ÿฌ์™€์ฃผ๋Š”๊ฒƒ์œผ๋กœ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,37 @@ +import { ApolloServer } from 'apollo-server'; + +import typeDefs from './typeDefs'; +import resolvers from './resolvers'; + +import model from './database/models'; +import * as jwtManager from './util/jwt-manager'; + +// Set GraphQL Apollo server +const context = ({ req }) => { + const authorizationHeader = req.headers.authorization || ''; + const token = authorizationHeader.split(' ')[1]; + const user = jwtManager.isTokenValid(token); + + return { user, model }; +}; + +const formatError = err => { + console.error('--- GraphQL Error ---'); + console.error('Path:', err.path); + console.error('Message:', err.message); + console.error('Code:', err.extensions.code); + console.error('Original Error', err.originalError); + return err; +}; + +const server = new ApolloServer({ + typeDefs, + resolvers, + context, + formatError, + debug: false, +}); + +server.listen().then(({ url }) => { + console.log(`๐Ÿš€ Server ready at ${url}`); +});
JavaScript
`authorization`์ด ์—†์„๋•Œ์˜ ์ฒ˜๋ฆฌ๊ฐ€ ์ข‹๋„ค์š”~
@@ -0,0 +1,37 @@ +import { ApolloServer } from 'apollo-server'; + +import typeDefs from './typeDefs'; +import resolvers from './resolvers'; + +import model from './database/models'; +import * as jwtManager from './util/jwt-manager'; + +// Set GraphQL Apollo server +const context = ({ req }) => { + const authorizationHeader = req.headers.authorization || ''; + const token = authorizationHeader.split(' ')[1]; + const user = jwtManager.isTokenValid(token); + + return { user, model }; +}; + +const formatError = err => { + console.error('--- GraphQL Error ---'); + console.error('Path:', err.path); + console.error('Message:', err.message); + console.error('Code:', err.extensions.code); + console.error('Original Error', err.originalError); + return err; +}; + +const server = new ApolloServer({ + typeDefs, + resolvers, + context, + formatError, + debug: false, +}); + +server.listen().then(({ url }) => { + console.log(`๐Ÿš€ Server ready at ${url}`); +});
JavaScript
`token`, `user`์„ ๋งŒ๋“œ๋Š” ๊ตฌ๋ฌธ์€ ์–ด์ฐจํ”ผ `authorization`์ด ์—†์œผ๋ฉด ๋ฌดํšจํ•œ ๋กœ์ง์œผ๋กœ ๋ณด์ด๋Š”๋ฐ์š” ๋ณ„๋„์˜ ํ๋ฆ„์„ ๋งŒ๋“ค๊ฑฐ๋‚˜ ๋ณ„๋„์˜ ํ•จ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•˜์ง€ ์•Š์€ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? ๋˜ํ•œ `[1]`์œผ๋กœ ๋ฐฐ์—ด์—์„œ ์•„์ดํ…œ์„ ํ”ฝํ•ด์˜ค๋Š” ๊ฒƒ์€ ์•”์‹œ์ ์œผ๋กœ ๋А๊ปด์ง€๋Š”๋ฐ์š”. ๋ช…์‹œ์ ์œผ๋กœ ๋ณ€๊ฒฝํ•ด๋ณด์‹œ๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,38 @@ +// import dotenv from 'dotenv'; +const dotenv = require('dotenv'); +dotenv.config(); + +const { + DB_DEV_USER, + DB_DEV_PASSWORD, + DB_DEV_DATABASE, + DB_DEV_HOST, + DB_PRODUCTION_USER, + DB_PRODUCTION_PASSWORD, + DB_PRODUCTION_DATABASE, + DB_PRODUCTION_HOST, +} = process.env; + +module.exports = { + development: { + username: DB_DEV_USER, + password: DB_DEV_PASSWORD, + database: DB_DEV_DATABASE, + host: DB_DEV_HOST, + dialect: 'mysql', + }, + test: { + username: DB_DEV_USER, + password: DB_DEV_PASSWORD, + database: DB_DEV_DATABASE, + host: DB_DEV_HOST, + dialect: 'mysql', + }, + production: { + username: DB_PRODUCTION_USER, + password: DB_PRODUCTION_PASSWORD, + database: DB_PRODUCTION_DATABASE, + host: DB_PRODUCTION_HOST, + dialect: 'mysql', + }, +};
JavaScript
๋ถˆํ•„์š”ํ•œ ์ฃผ์„์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค~
@@ -0,0 +1,38 @@ +// import dotenv from 'dotenv'; +const dotenv = require('dotenv'); +dotenv.config(); + +const { + DB_DEV_USER, + DB_DEV_PASSWORD, + DB_DEV_DATABASE, + DB_DEV_HOST, + DB_PRODUCTION_USER, + DB_PRODUCTION_PASSWORD, + DB_PRODUCTION_DATABASE, + DB_PRODUCTION_HOST, +} = process.env; + +module.exports = { + development: { + username: DB_DEV_USER, + password: DB_DEV_PASSWORD, + database: DB_DEV_DATABASE, + host: DB_DEV_HOST, + dialect: 'mysql', + }, + test: { + username: DB_DEV_USER, + password: DB_DEV_PASSWORD, + database: DB_DEV_DATABASE, + host: DB_DEV_HOST, + dialect: 'mysql', + }, + production: { + username: DB_PRODUCTION_USER, + password: DB_PRODUCTION_PASSWORD, + database: DB_PRODUCTION_DATABASE, + host: DB_PRODUCTION_HOST, + dialect: 'mysql', + }, +};
JavaScript
์ข‹์€ ์Šต๊ด€์ž…๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,19 @@ +FROM node:11.11.0 + +# ์•ฑ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ +WORKDIR /usr/src/app + +# ์•ฑ ์˜์กด์„ฑ ์„ค์น˜ +# ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ(npm@5+) package.json๊ณผ package-lock.json์„ ๋ชจ๋‘ ๋ณต์‚ฌํ•˜๊ธฐ ์œ„ํ•ด +# ์™€์ผ๋“œ์นด๋“œ๋ฅผ ์‚ฌ์šฉ +COPY package*.json ./ + +RUN npm install +# ํ”„๋กœ๋•์…˜์„ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ๋นŒ๋“œํ•˜๋Š” ๊ฒฝ์šฐ +# RUN npm ci --only=production + +# ์•ฑ ์†Œ์Šค ์ถ”๊ฐ€ +COPY . . + +EXPOSE 4000 +CMD [ "npm", "start" ] \ No newline at end of file
Unknown
`RUN npm ci --only=production`๋กœ ์น˜ํ™˜ํ•˜๋Š”๊ฒŒ ๋” ๋ฐ”๋žŒ์งํ•ด๋ณด์ด๋„ค์š” :)
@@ -0,0 +1,38 @@ +// import dotenv from 'dotenv'; +const dotenv = require('dotenv'); +dotenv.config(); + +const { + DB_DEV_USER, + DB_DEV_PASSWORD, + DB_DEV_DATABASE, + DB_DEV_HOST, + DB_PRODUCTION_USER, + DB_PRODUCTION_PASSWORD, + DB_PRODUCTION_DATABASE, + DB_PRODUCTION_HOST, +} = process.env; + +module.exports = { + development: { + username: DB_DEV_USER, + password: DB_DEV_PASSWORD, + database: DB_DEV_DATABASE, + host: DB_DEV_HOST, + dialect: 'mysql', + }, + test: { + username: DB_DEV_USER, + password: DB_DEV_PASSWORD, + database: DB_DEV_DATABASE, + host: DB_DEV_HOST, + dialect: 'mysql', + }, + production: { + username: DB_PRODUCTION_USER, + password: DB_PRODUCTION_PASSWORD, + database: DB_PRODUCTION_DATABASE, + host: DB_PRODUCTION_HOST, + dialect: 'mysql', + }, +};
JavaScript
ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ .envํŒŒ์ผ์— `DEV_`์™€ `PRODUCTION` ๊ตฌ๋ถ„์„ ๋‘์–ด ์ €์žฅํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ช…์„ ์ผ์น˜์‹œํ‚ค๊ณ (=dev, prod ๊ตฌ๋ถ„์—†์ด) ์ถ”๊ฐ€๋กœ `.env.test`, `.env.dev` ์™€ ๊ฐ™์€ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๊ด€๋ฆฌํ•˜๋Š”๊ฑธ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. https://github.com/motdotla/dotenv#should-i-have-multiple-env-files
@@ -0,0 +1,38 @@ +// import dotenv from 'dotenv'; +const dotenv = require('dotenv'); +dotenv.config(); + +const { + DB_DEV_USER, + DB_DEV_PASSWORD, + DB_DEV_DATABASE, + DB_DEV_HOST, + DB_PRODUCTION_USER, + DB_PRODUCTION_PASSWORD, + DB_PRODUCTION_DATABASE, + DB_PRODUCTION_HOST, +} = process.env; + +module.exports = { + development: { + username: DB_DEV_USER, + password: DB_DEV_PASSWORD, + database: DB_DEV_DATABASE, + host: DB_DEV_HOST, + dialect: 'mysql', + }, + test: { + username: DB_DEV_USER, + password: DB_DEV_PASSWORD, + database: DB_DEV_DATABASE, + host: DB_DEV_HOST, + dialect: 'mysql', + }, + production: { + username: DB_PRODUCTION_USER, + password: DB_PRODUCTION_PASSWORD, + database: DB_PRODUCTION_DATABASE, + host: DB_PRODUCTION_HOST, + dialect: 'mysql', + }, +};
JavaScript
์ด ๋ผ์ธ์ด ๊ผญ ์ฃผ์„์ฒ˜๋ฆฌ ๋˜์–ด์•ผํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,31 @@ +'use strict'; +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('room_options', { + bed: { + type: Sequelize.INTEGER, + }, + bedroom: { + type: Sequelize.INTEGER, + }, + bathroom: { + type: Sequelize.INTEGER, + }, + free_parking: { + type: Sequelize.BOOLEAN, + }, + wifi: { + type: Sequelize.BOOLEAN, + }, + kitchen: { + type: Sequelize.BOOLEAN, + }, + washer: { + type: Sequelize.BOOLEAN, + }, + }); + }, + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable('room_options'); + }, +};
JavaScript
'use strict'๋ฅผ ๊ผญ ์จ์•ผํ•˜๋‚˜์š”? ๋งŒ์•ฝ ๊ผญ ํ•„์š”ํ•˜๋‹ค๋ฉด, ์ด ๋ผ์ธ์ด ์—†๋Š” ํŒŒ์ผ๋„ ์žˆ๋˜๋ฐ ์ฃผ์„์œผ๋กœ ์„ค๋ช…์„ ๋‹ฌ์•„๋†“์œผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š” :)
@@ -0,0 +1,37 @@ +'use strict'; + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.bulkInsert( + 'users', + [ + { + name: '์ผ์ง€์ˆ˜', + email: 'init@init.com', + password: 'password', + salt: 'salt', + is_super_host: false, + }, + { + name: '์ด์ง€์ˆ˜', + email: 'init2@init.com', + password: 'password', + salt: 'salt', + is_super_host: false, + }, + { + name: '์‚ผ์ง€์ˆ˜', + email: 'init3@init.com', + password: 'password', + salt: 'salt', + is_super_host: false, + }, + ], + {}, + ); + }, + + down: (queryInterface, Sequelize) => { + return queryInterface.bulkDelete('users', null, {}); + }, +};
JavaScript
mock ๋ฐ์ดํ„ฐ์ง€๋งŒ ์ž‘๋ช…์„ผ์Šค ๐Ÿ‘
@@ -0,0 +1,38 @@ +// import dotenv from 'dotenv'; +const dotenv = require('dotenv'); +dotenv.config(); + +const { + DB_DEV_USER, + DB_DEV_PASSWORD, + DB_DEV_DATABASE, + DB_DEV_HOST, + DB_PRODUCTION_USER, + DB_PRODUCTION_PASSWORD, + DB_PRODUCTION_DATABASE, + DB_PRODUCTION_HOST, +} = process.env; + +module.exports = { + development: { + username: DB_DEV_USER, + password: DB_DEV_PASSWORD, + database: DB_DEV_DATABASE, + host: DB_DEV_HOST, + dialect: 'mysql', + }, + test: { + username: DB_DEV_USER, + password: DB_DEV_PASSWORD, + database: DB_DEV_DATABASE, + host: DB_DEV_HOST, + dialect: 'mysql', + }, + production: { + username: DB_PRODUCTION_USER, + password: DB_PRODUCTION_PASSWORD, + database: DB_PRODUCTION_DATABASE, + host: DB_PRODUCTION_HOST, + dialect: 'mysql', + }, +};
JavaScript
์ค‘๋ณต๋˜๋Š” ์ฝ”๋“œ๋Š” ๊ฐ€๋Šฅํ•˜๋ฉด ์ค„์ด๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์ง€๊ธˆ์€ ์ฝ”๋“œ๊ฐ€ ๋งŽ์ง€ ์•Š์•„์„œ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค๊ณ  ๋А๋‚„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ดํ›„ ๋ณต์žก๋„๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•  ๋•Œ ์œ ์ง€๋ณด์ˆ˜์˜ ์ธก๋ฉด์—์„œ ๋ฒ„๊ทธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. `DRY(Don't Repeat Yourself)` ์›์น™์€ ์ดํ›„๋กœ๋„ ํ•ญ์ƒ ๊ธฐ์–ตํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,37 @@ +import fs from 'fs'; +import path from 'path'; +import Sequelize from 'sequelize'; +import configs from '../../config/database.js'; + +const basename = path.basename(__filename); +const env = process.env.NODE_ENV || 'development'; +const config = configs[env]; + +const db = {}; + +let sequelize; +if (config.use_env_variable) { + sequelize = new Sequelize(process.env[config.use_env_variable], config); +} else { + sequelize = new Sequelize(config.database, config.username, config.password, config); +} + +fs.readdirSync(__dirname) + .filter(file => { + return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js'; + }) + .forEach(file => { + const model = sequelize['import'](path.join(__dirname, file)); + db[model.name] = model; + }); + +Object.keys(db).forEach(modelName => { + if (db[modelName].associate) { + db[modelName].associate(db); + } +}); + +db.sequelize = sequelize; +db.Sequelize = Sequelize; + +module.exports = db;
JavaScript
db ๊ฐ์ฒด์˜ ํ•„๋“œ๊ฐ’์ด ์ด๋ ‡๊ฒŒ ๋น„์Šทํ•˜๋ฉด ์ดํ›„์— ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‰ฌ์šธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜ ๋ช…์„ ์กฐ๊ธˆ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ๊ตฌ๋ถ„์„ ํ•ด ์ฃผ๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,28 @@ +'use strict'; + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.bulkInsert( + 'room_types', + [ + { + name: '์ง‘ ์ „์ฒด', + }, + { + name: '๊ฐœ์ธ์‹ค', + }, + { + name: 'ํ˜ธํ…” ๊ฐ์‹ค', + }, + { + name: '๋‹ค์ธ์‹ค', + }, + ], + {}, + ); + }, + + down: (queryInterface, Sequelize) => { + return queryInterface.bulkDelete('room_types', null, {}); + }, +};
JavaScript
๊ฐ์‹ค์„ ๋ถ™์—ฌ์„œ ์จ์•ผ ํ•˜๋Š”๋ฐ ์˜คํƒ€์ธ ๊ฒƒ ๊ฐ™๋„ค์š”
@@ -0,0 +1,13 @@ +NODE_ENV= + +DB_DEV_USER= +DB_DEV_PASSWORD= +DB_DEV_DATABASE= +DB_DEV_HOST= + +DB_PRODUCTION_USER= +DB_PRODUCTION_PASSWORD= +DB_PRODUCTION_DATABASE= +DB_PRODUCTION_HOST= + +TOKEN_SECRET_KEY= \ No newline at end of file
Unknown
.env ํŒŒ์ผ์€ ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ฏผ๊ฐํ•œ ์ •๋ณด๋“ค์ด ๋งŽ์ด ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ .dev.env ํ˜•ํƒœ๋กœ github์—์„œ ๋ณด์ด์ง€ ์•Š๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ๋„ ๋ณด์•ˆ์ƒ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,37 @@ +import fs from 'fs'; +import path from 'path'; +import Sequelize from 'sequelize'; +import configs from '../../config/database.js'; + +const basename = path.basename(__filename); +const env = process.env.NODE_ENV || 'development'; +const config = configs[env]; + +const db = {}; + +let sequelize; +if (config.use_env_variable) { + sequelize = new Sequelize(process.env[config.use_env_variable], config); +} else { + sequelize = new Sequelize(config.database, config.username, config.password, config); +} + +fs.readdirSync(__dirname) + .filter(file => { + return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js'; + }) + .forEach(file => { + const model = sequelize['import'](path.join(__dirname, file)); + db[model.name] = model; + }); + +Object.keys(db).forEach(modelName => { + if (db[modelName].associate) { + db[modelName].associate(db); + } +}); + +db.sequelize = sequelize; +db.Sequelize = Sequelize; + +module.exports = db;
JavaScript
ํ•œ ์ค„์ด๋ผ๋„ ๋งŒ์•ฝ ํ•œ ๋ˆˆ์— ๋ณด๊ธฐ์— ์กฐ๊ธˆ ๋ณต์žกํ•˜๋‹ค๋Š” ๋А๋‚Œ์ด ๋“ ๋‹ค๋ฉด ๋ณ„๋„์˜ ํ•จ์ˆ˜ ํ˜•ํƒœ๋กœ ์™ธ๋ถ€์— ๋นผ๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”. ์ดํ›„์— ์กฐ๊ฑด์ด ๋” ๋ถ™๋Š”๋‹ค๋ฉด ๊ทธ๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๋Š”๋ฐ ํ›จ์”ฌ ๋” ๋„์›€์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,41 @@ +INSERT INTO board_entity (board_name) +VALUES + ('์ž์œ  ๊ฒŒ์‹œํŒ'), + ('๊ฐœ๋ฐœ ๊ฒŒ์‹œํŒ'), + ('์ผ์ƒ ๊ฒŒ์‹œํŒ'), + ('์‚ฌ๊ฑด์‚ฌ๊ณ  ๊ฒŒ์‹œํŒ'); + +INSERT INTO article_entity (title, content, password, board_id) +VALUES + ('์ˆ™๋Œ€์ž…๊ตฌ์—ญ ๋ง›์ง‘ ์ถ”์ฒœ', '์ˆ™๋Œ€์ž…๊ตฌ์—ญ ๊ทผ์ฒ˜์— ์žˆ๋Š” ํ‰ํ™”๋‚จ์˜ ์‹๋‹น์ด ๋ง›์žˆ์–ด์š”!', '1234', 1), + ('์—ฐ์•  ํ”„๋กœ๊ทธ๋žจ์„ ๋ณด๋ฉด์„œ..', '๊ฐœ๋ฐœ์ž๋“ค์„ ์œ„ํ•œ ์˜ˆ๋Šฅ ํ”„๋กœ๊ทธ๋žจ์ด ์žˆ์—ˆ์œผ๋ฉด ์ข‹๊ฒ ๋‹ค.. ์•„ ๊ทผ๋ฐ ๊ฐœ๋ฐœ์ž๋“ค์ด ์นด๋ฉ”๋ผ ์•ž์— ์„ค๋ฆฌ๊ฐ€ ์—†์ง€ ใ…‹ใ…‹ใ…‹ใ…‹', '1234', 1), + ('GPT 3.5๋ฅผ ์“ฐ๋ฉด์„œ..', '๋‹Œ ๊ทธ๋ƒฅ ์ฝ”๋”ฉ ํ•˜์ง€ ๋ง์•„๋ผ ์† ํ„ฐ์ง„๋‹ค.', '1234', 2), + ('JS์™€ Java๋ฅผ ๊ณต๋ถ€ํ•˜๋‹ˆ๊นŒ', '๋„ˆ๋ฌด ํ—ท๊ฐˆ๋ ค์š” ใ…‹ใ…‹ใ…‹ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋„ฃ์„ ๋•Œ ์ž๊พธ ํƒ€์ž…์„ ์•ˆ์”€;;', '1234', 2), + ('๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ style ์ฝ”๋“œ ๋ฆฌ๋ทฐ', '์˜ค๋Š˜์€ ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ์•Œ์•„๋ณผ๊ฒŒ์š”! ์ €๋„ ์ฐธ ๊ถ๊ธˆํ•˜๋„ค์š”!', '1234', 2), + ('์š”์ฆ˜ ๊ทผํ™ฉ', '๋ฐฅ ๋จน์–ด ์ฝ”๋”ฉ ํ•ด ๋˜ ๋ฐฅ ๋จน์–ด ์ฝ”๋”ฉ ํ•ด ์œ ํŠœ๋ธŒ ๋ด ๋˜ ์ฝ”๋”ฉ ํ•ด', '1234', 3), + ('์š”์ฆ˜ ๊ทผํ™ฉ', '๋ฐฅ๋ฐฅ๋ฐฅ๋ฐฅ ์˜ค๋Š˜ ์ €๋…์€ ์ œ์œก๋ณถ์Œ~~', '1234', 3), + ('์‚ฌ๊ฑด์‚ฌ๊ณ ? ๊ทธ๋Ÿฐ๊ฑด ๋‚ด ์‚ฌ์ „์— ์—†๋‹ค.', '์™œ๋ƒํ•˜๋ฉด ์ฝ”๋”ฉ์„ ํ•œ ์ค„๋„ ์•ˆ์น˜๊ธฐ ๋•Œ๋ฌธ์ด์ง€ ํ•˜ํ•˜ํ•˜!', '1234', 4), + ('์•ผ ์—๋Ÿฌ ๋œจ๋Š” ๋†ˆ๋“ค ๋ด๋ผ ใ…‹ใ…‹ใ…‹', '์—๋Ÿฌ? ์•„๋‹ˆ ์ปดํ“จํ„ฐ ๋„ˆ๊ฐ€ ํ‹€๋ฆฌ๊ณ  ๋‚ด๊ฐ€ ๋งž์–ด', '1234', 4); + +INSERT INTO comment_entity(content, password, article_id) +VALUES + ('ํ‰ํ™”๋‚จ์˜ ๊ฑฐ๊ธฐ ๋ง›์žˆ์–ด์š”!', '1234', 1), + ('์ธํ…Œ๋ฆฌ์–ด๊ฐ€ ์ด๋ป์š”!', '1234', 1), + ('๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ์˜ˆ๋Šฅ์ด๋ผ๋‹ˆ.. ์•„์•„ ๊ฟˆ์„ ๊พธ์—ˆ์Šต๋‹ˆ๋‹ค..', '1234', 2), + ('์•„์ฃผ ํ–‰๋ณตํ•œ ๊ฟˆ์ด์—ˆ์Šต๋‹ˆ๋‹ค..', '1234', 2), + ('๊ทผ๋ฐ ์™œ ์šฐ๋Š” ๊ฒƒ์ด๋ƒ', '1234', 2), + ('๊ทธ๊ฑด ์ด๋ค„์งˆ ์ˆ˜ ์—†๋Š” ๊ฟˆ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.', '1234', 2), + ('GPT๋Š” ์˜คํƒ€ ์žก๊ธฐ ์ตœ๊ณ ๋ผ๊ณ  ใ…‹ใ…‹ใ…‹ใ…‹', '1234', 3), + ('๋ฐฑ์—”๋“œ์˜ ๊ทผ๋ณธ์€ Java์ด์ง€ ใ…‹ใ…‹ใ…‹ใ…‹', '1234', 4), + ('js๋Š” ๊ทผ๋ณธ์ด ์—†๋‹ค ์•„์ž…๋‹ˆ๊บผ ๊ทผ๋ณธ์ด! ', '1234', 4), + ('์ด์™• ์ด๋ ‡๊ฒŒ ๋œ๊ฑฐ ์ฝ”ํ‹€๋ฆฐ์œผ๋กœ ๊ฐ„๋‹ค.', '1234', 4), + ('์‹คํŒจํ•˜๋ฉด ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์„ฑ๊ณตํ•˜๋ฉด ๋™์ ์–ธ์–ด ์ตœ๊ณ ์•„๋‹™๋‹ˆ๊นŒ!.', '1234', 4), + ('ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋„ Java๋ž‘ ํƒ€์ž… ์“ฐ๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฆ„ ใ…‹ใ…‹ใ…‹ใ…‹ ์ˆ˜๊ณ ์š”.', '1234', 4), + ('๋น„๋ฐ€ ๋Œ“๊ธ€์ž…๋‹ˆ๋‹ค.', '1234', 5), + ('๋น„๋ฐ€ ๋Œ“๊ธ€์ž…๋‹ˆ๋‹ค.', '1234', 5), + ('๊ฐœ๋ฐœ ๊ณต๋ถ€ํ•˜๋ฉด ์›๋ž˜ ๋ฐ˜์ฏค ๋ฏธ์ณ๊ฐ€๋‚˜์š”?', '1234', 5), + ('์ผ์ƒ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํผ ๋ฏธ์ณค๋‹ค', '1234', 6), + ('๋„ˆ ์•„๋ž˜ ๊ธ€ ์“ด ๋†ˆ์ด์ง€?', '1234', 7), + ('๊ฐœ๋ฐœ ๊ณต๋ถ€ํ•˜๋ฉด ์›๋ž˜ ๋ฐ˜์ฏค ๋ฏธ์น˜๋‚˜์š”?', '1234', 7), + ('๊ทธ๋Ÿฐ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋„ค ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹', '1234', 8), + ('404 Not Found๋ผ๊ณ ? ๊ทธ๋Ÿผ ์ฐพ์–ด!', '1234', 9);
Unknown
yaml์„ค์ • create๋ผ ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ ์ „๋ถ€ ์ƒ์„ฑํ•ด๋†“์€ ์„ฌ์„ธํ•จ์— ๋†€๋ž์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,147 @@ +package com.subject.board.article; + +import com.subject.board.board.BoardService; +import com.subject.board.comment.CommentService; +import com.subject.board.entity.ArticleEntity; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; + +import java.util.ArrayList; +import java.util.List; + + +@Controller +@RequestMapping("/article") +@RequiredArgsConstructor +public class ArticleController { + private final ArticleService articleService; + private final BoardService boardService; + private final CommentService commentService; + + // Create + // ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ฑ view๋กœ ์ด๋™ + @GetMapping("/create") + public String createPage() { + return "article/create"; + } + + // create ๋กœ์ง + @PostMapping("/create") + public String create( + @RequestParam("board-id") Long boardId, + @RequestParam("title") String title, + @RequestParam("content") String content, + @RequestParam("password") String password + ) { + articleService.create(boardId, title, content, password); + return "redirect:/article"; + } + + // Read + // ์ „์ฒด ๋ณด๊ธฐ(= ์ „์ฒด ๊ฒŒ์‹œํŒ) + @GetMapping + public String readAll(Model model) { + model.addAttribute("articles", articleService.readAll()); + return "home"; + } + + // ์ƒ์„ธ ๋ณด๊ธฐ + @GetMapping("/{articleId}") + public String readOne( + @PathVariable("articleId") Long articleId, + Model model + ) { + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("comments", commentService.findByArticleId(articleId)); + return "article/read"; + } + + // Update + // ๋น„๋ฐ€๋ฒˆํ˜ธ view + @GetMapping("/{articleId}/password-view/update") + public String passwordViewUpdate( + @PathVariable("articleId") Long articleId, + Model model + ) { + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("type", "article"); + model.addAttribute("method", "update"); + return "password"; + } + + // ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ ํ›„ -> update view๋กœ ์ด๋™ + @PostMapping("/{articleId}/passwordCheck/update") + public String checkPasswordUpdate( + @PathVariable("articleId") Long articleId, + @RequestParam("input-password") String inputPassword, + RedirectAttributes redirectAttributes, + Model model + ) { + if (articleService.checkPassword(articleId, inputPassword)) { + // ๋น„๋ฐ€๋ฒˆํ˜ธ ์ผ์น˜ + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("boards", boardService.readAll()); + return "article/update"; + } else { + // ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ถˆ์ผ์น˜ + redirectAttributes.addFlashAttribute("error", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + return "redirect:/article/" + articleId + "/password-view/update"; // ๋‹ค์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ + } + } + + // update ์‹คํ–‰ + @PostMapping("/{articleId}/update") + public String update( + @PathVariable("articleId") Long articleId, + @RequestParam("board-id") Long boardId, + @RequestParam("title") String title, + @RequestParam("content") String content + ) { + articleService.update(articleId, boardId, title, content); + return String.format("redirect:/article/%d", articleId); + } + + // Delete + // ๋น„๋ฐ€๋ฒˆํ˜ธ view + @GetMapping("/{articleId}/password-view/delete") + public String passwordViewDelete( + @PathVariable("articleId") Long articleId, + Model model + ) { + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("type", "article"); + model.addAttribute("method", "delete"); + return "password"; + } + + // ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜๋ฉด ์‚ญ์ œ, ํ‹€๋ฆฌ๋ฉด ๊ฒฝ๊ณ ์ฐฝ + @PostMapping("/{articleId}/passwordCheck/delete") + public String checkPasswordDelete( + @PathVariable("articleId") Long articleId, + @RequestParam("input-password") String inputPassword, + RedirectAttributes redirectAttributes, + Model model + ) { + if (articleService.checkPassword(articleId, inputPassword)) { + articleService.delete(articleId); + return "redirect:/article"; + } else { + redirectAttributes.addFlashAttribute("error", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + return "redirect:/article/" + articleId + "/password-view/delete"; // ๋‹ค์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ + } + } + + // Search + @PostMapping("/search") + public String searchArticle( + @RequestParam("category") String category, + @RequestParam("search") String search, + Model model + ) { + model.addAttribute("articles", articleService.search(category, search)); + return "article/searchArticle"; + } +}
Java
์ „์ฒด ๊ฒŒ์‹œ๊ธ€์—์„œ ๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑ์„ ์‹œ๋„ํ•˜๋‹ˆ ๊ฒŒ์‹œ๊ธ€ ์ฃผ์ œ๋ฅผ ์„ ํƒํ•˜๋Š” ๋“œ๋กญ๋‹ค์šด์— ์•„๋ฌด๊ฒƒ๋„ ๋‚˜์˜ค์งˆ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฒŒ์‹œํŒ์˜ ์ •๋ณด๋ฅผ model์— ๋‹ด์•„ ์ „๋‹ฌํ•˜๋Š” ๊ณผ์ •์ด ๋น ์ง„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,147 @@ +package com.subject.board.article; + +import com.subject.board.board.BoardService; +import com.subject.board.comment.CommentService; +import com.subject.board.entity.ArticleEntity; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; + +import java.util.ArrayList; +import java.util.List; + + +@Controller +@RequestMapping("/article") +@RequiredArgsConstructor +public class ArticleController { + private final ArticleService articleService; + private final BoardService boardService; + private final CommentService commentService; + + // Create + // ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ฑ view๋กœ ์ด๋™ + @GetMapping("/create") + public String createPage() { + return "article/create"; + } + + // create ๋กœ์ง + @PostMapping("/create") + public String create( + @RequestParam("board-id") Long boardId, + @RequestParam("title") String title, + @RequestParam("content") String content, + @RequestParam("password") String password + ) { + articleService.create(boardId, title, content, password); + return "redirect:/article"; + } + + // Read + // ์ „์ฒด ๋ณด๊ธฐ(= ์ „์ฒด ๊ฒŒ์‹œํŒ) + @GetMapping + public String readAll(Model model) { + model.addAttribute("articles", articleService.readAll()); + return "home"; + } + + // ์ƒ์„ธ ๋ณด๊ธฐ + @GetMapping("/{articleId}") + public String readOne( + @PathVariable("articleId") Long articleId, + Model model + ) { + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("comments", commentService.findByArticleId(articleId)); + return "article/read"; + } + + // Update + // ๋น„๋ฐ€๋ฒˆํ˜ธ view + @GetMapping("/{articleId}/password-view/update") + public String passwordViewUpdate( + @PathVariable("articleId") Long articleId, + Model model + ) { + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("type", "article"); + model.addAttribute("method", "update"); + return "password"; + } + + // ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ ํ›„ -> update view๋กœ ์ด๋™ + @PostMapping("/{articleId}/passwordCheck/update") + public String checkPasswordUpdate( + @PathVariable("articleId") Long articleId, + @RequestParam("input-password") String inputPassword, + RedirectAttributes redirectAttributes, + Model model + ) { + if (articleService.checkPassword(articleId, inputPassword)) { + // ๋น„๋ฐ€๋ฒˆํ˜ธ ์ผ์น˜ + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("boards", boardService.readAll()); + return "article/update"; + } else { + // ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ถˆ์ผ์น˜ + redirectAttributes.addFlashAttribute("error", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + return "redirect:/article/" + articleId + "/password-view/update"; // ๋‹ค์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ + } + } + + // update ์‹คํ–‰ + @PostMapping("/{articleId}/update") + public String update( + @PathVariable("articleId") Long articleId, + @RequestParam("board-id") Long boardId, + @RequestParam("title") String title, + @RequestParam("content") String content + ) { + articleService.update(articleId, boardId, title, content); + return String.format("redirect:/article/%d", articleId); + } + + // Delete + // ๋น„๋ฐ€๋ฒˆํ˜ธ view + @GetMapping("/{articleId}/password-view/delete") + public String passwordViewDelete( + @PathVariable("articleId") Long articleId, + Model model + ) { + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("type", "article"); + model.addAttribute("method", "delete"); + return "password"; + } + + // ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜๋ฉด ์‚ญ์ œ, ํ‹€๋ฆฌ๋ฉด ๊ฒฝ๊ณ ์ฐฝ + @PostMapping("/{articleId}/passwordCheck/delete") + public String checkPasswordDelete( + @PathVariable("articleId") Long articleId, + @RequestParam("input-password") String inputPassword, + RedirectAttributes redirectAttributes, + Model model + ) { + if (articleService.checkPassword(articleId, inputPassword)) { + articleService.delete(articleId); + return "redirect:/article"; + } else { + redirectAttributes.addFlashAttribute("error", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + return "redirect:/article/" + articleId + "/password-view/delete"; // ๋‹ค์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ + } + } + + // Search + @PostMapping("/search") + public String searchArticle( + @RequestParam("category") String category, + @RequestParam("search") String search, + Model model + ) { + model.addAttribute("articles", articleService.search(category, search)); + return "article/searchArticle"; + } +}
Java
์ œ๊ฐ€ ๋”ฑ ์›ํ•˜๋˜ ๊ธฐ๋Šฅ์ด์—ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๊ตฐ์š”! ์ž˜ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค.
@@ -0,0 +1,21 @@ +package com.subject.board.entity; + +import jakarta.persistence.*; +import lombok.Data; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +@Entity +public class BoardEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String boardName; + @OneToMany(mappedBy = "board") + @OrderBy("id DESC") // article์˜ id๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ + private List<ArticleEntity> articles; +}
Java
entity์—์„œ article์˜ id๋ฅผ ์ •๋ ฌํ•ด ๋ถ€๊ฐ€์ ์ธ ์ฝ”๋“œ์˜ ์ถ”๊ฐ€ ์—†์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ฒŒ์‹œ๊ธ€์„ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์—ˆ๋„ค์š”. ๋ฐฐ์šฐ๊ณ ๊ฐ‘๋‹ˆ๋‹ค.
@@ -0,0 +1,58 @@ +package com.subject.board.comment; + +import com.subject.board.article.ArticleRepository; +import com.subject.board.entity.ArticleEntity; +import com.subject.board.entity.CommentEntity; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class CommentService { + private final ArticleRepository articleRepository; + private final CommentRepository commentRepository; + + // Create + public void create( + String content, + String password, + Long articleId) { + Optional<ArticleEntity> article = articleRepository.findById(articleId); + + CommentEntity comment = new CommentEntity(); + comment.setContent(content); + comment.setPassword(password); + comment.setArticle(article.orElse(null)); + commentRepository.save(comment); + } + + // Read + // ๊ฒŒ์‹œ๊ธ€๋ณ„ ๋Œ“๊ธ€ ์กฐํšŒ + public List<CommentEntity> findByArticleId(Long articleId) { + return commentRepository.findByArticleId(articleId); + } + + public CommentEntity readOne(Long commentId) { + return commentRepository.findById(commentId) + .orElse(null); + } + + // Delete + public void delete(Long commentId) { + commentRepository.deleteById(commentId); + } + + // ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ + public Boolean checkPassword( + Long commentId, + String inputPassword + ) { + String passwrod = readOne(commentId).getPassword(); + if (passwrod.equals(inputPassword)) { + return true; + } else return false; + } +}
Java
password ์˜คํƒ€์žˆ์Šต๋‹ˆ๋‹ค. return password.equals(inputPassword); ๋กœ ์ค„์ผ์ˆ˜ ์žˆ์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,41 @@ +INSERT INTO board_entity (board_name) +VALUES + ('์ž์œ  ๊ฒŒ์‹œํŒ'), + ('๊ฐœ๋ฐœ ๊ฒŒ์‹œํŒ'), + ('์ผ์ƒ ๊ฒŒ์‹œํŒ'), + ('์‚ฌ๊ฑด์‚ฌ๊ณ  ๊ฒŒ์‹œํŒ'); + +INSERT INTO article_entity (title, content, password, board_id) +VALUES + ('์ˆ™๋Œ€์ž…๊ตฌ์—ญ ๋ง›์ง‘ ์ถ”์ฒœ', '์ˆ™๋Œ€์ž…๊ตฌ์—ญ ๊ทผ์ฒ˜์— ์žˆ๋Š” ํ‰ํ™”๋‚จ์˜ ์‹๋‹น์ด ๋ง›์žˆ์–ด์š”!', '1234', 1), + ('์—ฐ์•  ํ”„๋กœ๊ทธ๋žจ์„ ๋ณด๋ฉด์„œ..', '๊ฐœ๋ฐœ์ž๋“ค์„ ์œ„ํ•œ ์˜ˆ๋Šฅ ํ”„๋กœ๊ทธ๋žจ์ด ์žˆ์—ˆ์œผ๋ฉด ์ข‹๊ฒ ๋‹ค.. ์•„ ๊ทผ๋ฐ ๊ฐœ๋ฐœ์ž๋“ค์ด ์นด๋ฉ”๋ผ ์•ž์— ์„ค๋ฆฌ๊ฐ€ ์—†์ง€ ใ…‹ใ…‹ใ…‹ใ…‹', '1234', 1), + ('GPT 3.5๋ฅผ ์“ฐ๋ฉด์„œ..', '๋‹Œ ๊ทธ๋ƒฅ ์ฝ”๋”ฉ ํ•˜์ง€ ๋ง์•„๋ผ ์† ํ„ฐ์ง„๋‹ค.', '1234', 2), + ('JS์™€ Java๋ฅผ ๊ณต๋ถ€ํ•˜๋‹ˆ๊นŒ', '๋„ˆ๋ฌด ํ—ท๊ฐˆ๋ ค์š” ใ…‹ใ…‹ใ…‹ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋„ฃ์„ ๋•Œ ์ž๊พธ ํƒ€์ž…์„ ์•ˆ์”€;;', '1234', 2), + ('๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ style ์ฝ”๋“œ ๋ฆฌ๋ทฐ', '์˜ค๋Š˜์€ ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ์•Œ์•„๋ณผ๊ฒŒ์š”! ์ €๋„ ์ฐธ ๊ถ๊ธˆํ•˜๋„ค์š”!', '1234', 2), + ('์š”์ฆ˜ ๊ทผํ™ฉ', '๋ฐฅ ๋จน์–ด ์ฝ”๋”ฉ ํ•ด ๋˜ ๋ฐฅ ๋จน์–ด ์ฝ”๋”ฉ ํ•ด ์œ ํŠœ๋ธŒ ๋ด ๋˜ ์ฝ”๋”ฉ ํ•ด', '1234', 3), + ('์š”์ฆ˜ ๊ทผํ™ฉ', '๋ฐฅ๋ฐฅ๋ฐฅ๋ฐฅ ์˜ค๋Š˜ ์ €๋…์€ ์ œ์œก๋ณถ์Œ~~', '1234', 3), + ('์‚ฌ๊ฑด์‚ฌ๊ณ ? ๊ทธ๋Ÿฐ๊ฑด ๋‚ด ์‚ฌ์ „์— ์—†๋‹ค.', '์™œ๋ƒํ•˜๋ฉด ์ฝ”๋”ฉ์„ ํ•œ ์ค„๋„ ์•ˆ์น˜๊ธฐ ๋•Œ๋ฌธ์ด์ง€ ํ•˜ํ•˜ํ•˜!', '1234', 4), + ('์•ผ ์—๋Ÿฌ ๋œจ๋Š” ๋†ˆ๋“ค ๋ด๋ผ ใ…‹ใ…‹ใ…‹', '์—๋Ÿฌ? ์•„๋‹ˆ ์ปดํ“จํ„ฐ ๋„ˆ๊ฐ€ ํ‹€๋ฆฌ๊ณ  ๋‚ด๊ฐ€ ๋งž์–ด', '1234', 4); + +INSERT INTO comment_entity(content, password, article_id) +VALUES + ('ํ‰ํ™”๋‚จ์˜ ๊ฑฐ๊ธฐ ๋ง›์žˆ์–ด์š”!', '1234', 1), + ('์ธํ…Œ๋ฆฌ์–ด๊ฐ€ ์ด๋ป์š”!', '1234', 1), + ('๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ์˜ˆ๋Šฅ์ด๋ผ๋‹ˆ.. ์•„์•„ ๊ฟˆ์„ ๊พธ์—ˆ์Šต๋‹ˆ๋‹ค..', '1234', 2), + ('์•„์ฃผ ํ–‰๋ณตํ•œ ๊ฟˆ์ด์—ˆ์Šต๋‹ˆ๋‹ค..', '1234', 2), + ('๊ทผ๋ฐ ์™œ ์šฐ๋Š” ๊ฒƒ์ด๋ƒ', '1234', 2), + ('๊ทธ๊ฑด ์ด๋ค„์งˆ ์ˆ˜ ์—†๋Š” ๊ฟˆ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.', '1234', 2), + ('GPT๋Š” ์˜คํƒ€ ์žก๊ธฐ ์ตœ๊ณ ๋ผ๊ณ  ใ…‹ใ…‹ใ…‹ใ…‹', '1234', 3), + ('๋ฐฑ์—”๋“œ์˜ ๊ทผ๋ณธ์€ Java์ด์ง€ ใ…‹ใ…‹ใ…‹ใ…‹', '1234', 4), + ('js๋Š” ๊ทผ๋ณธ์ด ์—†๋‹ค ์•„์ž…๋‹ˆ๊บผ ๊ทผ๋ณธ์ด! ', '1234', 4), + ('์ด์™• ์ด๋ ‡๊ฒŒ ๋œ๊ฑฐ ์ฝ”ํ‹€๋ฆฐ์œผ๋กœ ๊ฐ„๋‹ค.', '1234', 4), + ('์‹คํŒจํ•˜๋ฉด ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์„ฑ๊ณตํ•˜๋ฉด ๋™์ ์–ธ์–ด ์ตœ๊ณ ์•„๋‹™๋‹ˆ๊นŒ!.', '1234', 4), + ('ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋„ Java๋ž‘ ํƒ€์ž… ์“ฐ๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฆ„ ใ…‹ใ…‹ใ…‹ใ…‹ ์ˆ˜๊ณ ์š”.', '1234', 4), + ('๋น„๋ฐ€ ๋Œ“๊ธ€์ž…๋‹ˆ๋‹ค.', '1234', 5), + ('๋น„๋ฐ€ ๋Œ“๊ธ€์ž…๋‹ˆ๋‹ค.', '1234', 5), + ('๊ฐœ๋ฐœ ๊ณต๋ถ€ํ•˜๋ฉด ์›๋ž˜ ๋ฐ˜์ฏค ๋ฏธ์ณ๊ฐ€๋‚˜์š”?', '1234', 5), + ('์ผ์ƒ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํผ ๋ฏธ์ณค๋‹ค', '1234', 6), + ('๋„ˆ ์•„๋ž˜ ๊ธ€ ์“ด ๋†ˆ์ด์ง€?', '1234', 7), + ('๊ฐœ๋ฐœ ๊ณต๋ถ€ํ•˜๋ฉด ์›๋ž˜ ๋ฐ˜์ฏค ๋ฏธ์น˜๋‚˜์š”?', '1234', 7), + ('๊ทธ๋Ÿฐ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋„ค ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹', '1234', 8), + ('404 Not Found๋ผ๊ณ ? ๊ทธ๋Ÿผ ์ฐพ์–ด!', '1234', 9);
Unknown
๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ์–ด์„œ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์ข‹์•˜์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,147 @@ +package com.subject.board.article; + +import com.subject.board.board.BoardService; +import com.subject.board.comment.CommentService; +import com.subject.board.entity.ArticleEntity; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; + +import java.util.ArrayList; +import java.util.List; + + +@Controller +@RequestMapping("/article") +@RequiredArgsConstructor +public class ArticleController { + private final ArticleService articleService; + private final BoardService boardService; + private final CommentService commentService; + + // Create + // ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ฑ view๋กœ ์ด๋™ + @GetMapping("/create") + public String createPage() { + return "article/create"; + } + + // create ๋กœ์ง + @PostMapping("/create") + public String create( + @RequestParam("board-id") Long boardId, + @RequestParam("title") String title, + @RequestParam("content") String content, + @RequestParam("password") String password + ) { + articleService.create(boardId, title, content, password); + return "redirect:/article"; + } + + // Read + // ์ „์ฒด ๋ณด๊ธฐ(= ์ „์ฒด ๊ฒŒ์‹œํŒ) + @GetMapping + public String readAll(Model model) { + model.addAttribute("articles", articleService.readAll()); + return "home"; + } + + // ์ƒ์„ธ ๋ณด๊ธฐ + @GetMapping("/{articleId}") + public String readOne( + @PathVariable("articleId") Long articleId, + Model model + ) { + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("comments", commentService.findByArticleId(articleId)); + return "article/read"; + } + + // Update + // ๋น„๋ฐ€๋ฒˆํ˜ธ view + @GetMapping("/{articleId}/password-view/update") + public String passwordViewUpdate( + @PathVariable("articleId") Long articleId, + Model model + ) { + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("type", "article"); + model.addAttribute("method", "update"); + return "password"; + } + + // ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ ํ›„ -> update view๋กœ ์ด๋™ + @PostMapping("/{articleId}/passwordCheck/update") + public String checkPasswordUpdate( + @PathVariable("articleId") Long articleId, + @RequestParam("input-password") String inputPassword, + RedirectAttributes redirectAttributes, + Model model + ) { + if (articleService.checkPassword(articleId, inputPassword)) { + // ๋น„๋ฐ€๋ฒˆํ˜ธ ์ผ์น˜ + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("boards", boardService.readAll()); + return "article/update"; + } else { + // ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ถˆ์ผ์น˜ + redirectAttributes.addFlashAttribute("error", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + return "redirect:/article/" + articleId + "/password-view/update"; // ๋‹ค์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ + } + } + + // update ์‹คํ–‰ + @PostMapping("/{articleId}/update") + public String update( + @PathVariable("articleId") Long articleId, + @RequestParam("board-id") Long boardId, + @RequestParam("title") String title, + @RequestParam("content") String content + ) { + articleService.update(articleId, boardId, title, content); + return String.format("redirect:/article/%d", articleId); + } + + // Delete + // ๋น„๋ฐ€๋ฒˆํ˜ธ view + @GetMapping("/{articleId}/password-view/delete") + public String passwordViewDelete( + @PathVariable("articleId") Long articleId, + Model model + ) { + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("type", "article"); + model.addAttribute("method", "delete"); + return "password"; + } + + // ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜๋ฉด ์‚ญ์ œ, ํ‹€๋ฆฌ๋ฉด ๊ฒฝ๊ณ ์ฐฝ + @PostMapping("/{articleId}/passwordCheck/delete") + public String checkPasswordDelete( + @PathVariable("articleId") Long articleId, + @RequestParam("input-password") String inputPassword, + RedirectAttributes redirectAttributes, + Model model + ) { + if (articleService.checkPassword(articleId, inputPassword)) { + articleService.delete(articleId); + return "redirect:/article"; + } else { + redirectAttributes.addFlashAttribute("error", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + return "redirect:/article/" + articleId + "/password-view/delete"; // ๋‹ค์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ + } + } + + // Search + @PostMapping("/search") + public String searchArticle( + @RequestParam("category") String category, + @RequestParam("search") String search, + Model model + ) { + model.addAttribute("articles", articleService.search(category, search)); + return "article/searchArticle"; + } +}
Java
ArticleEntity๊ฐ€ ์ด๋ฏธ Comment ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด comments๋ฅผ ๋ชจ๋ธ๋กœ ๋”ฐ๋กœ ๋„˜๊ธฐ์ง€ ์•Š์•„๋„ ๋ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,54 @@ +package christmas.constant; + +public final class CommentConstants { + public static final String EVENT_INFORMATION_MESSAGE = """ + ์šฐํ…Œ์ฝ” ์‹๋‹น์˜ ์ตœ๋Œ€ ์ด๋ฒคํŠธ! 12์›” ์ด๋ฒคํŠธ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค! + ์ตœ๋Œ€ 3๋ฒˆ์˜ ์ค‘๋ณต ํ• ์ธ๊ณผ ์šฐํ…Œ์ฝ”๊ฐ€ ์ค€๋น„ํ•œ ๋‘ ๊ฐœ์˜ ๊นœ์ง ์„ ๋ฌผ์„ ๋ฐ›์•„ ๊ฐ€์„ธ์š”! + * ๋ชจ๋“  ์ด๋ฒคํŠธ๋Š” ํƒ€ ์ด๋ฒคํŠธ์™€ ์ค‘๋ณต ์ ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. + + ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด๋ฅผ ๊ฐ™์ด ์„ธ์–ด๋ณด์•„์š”. + ์ตœ๋Œ€ 3400์› ํ• ์ธ! 1์ผ 1000์›์œผ๋กœ ์‹œ์ž‘ํ•˜์—ฌ 25์ผ๊นŒ์ง€ 100์›์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ํ• ์ธ! + ํฌ๋ฆฌ์Šค๋งˆ์Šค์˜ ์„ ๋ฌผ์„ ๋А๊ปด๋ณด์„ธ์š”! + * 25์ผ๊นŒ์ง€ + + ์ง€๋‚˜๊ฐ€๋Š” 2023๋…„์„ ์œ„ํ•œ, 2,023์› ํ• ์ธ ์ด๋ฒคํŠธ! + ๊ธˆ์š”์ผ, ํ† ์š”์ผ์—๋Š” ๋ฉ”์ธ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์›์„ ํ• ์ธํ•ฉ๋‹ˆ๋‹ค! + ๋‹ค๋ฅธ ๋‚  ๋ฐฉ๋ฌธํ•ด๋„ ์„ญ์„ญํ•ดํ•˜์ง€ ๋งˆ์„ธ์š”. + ์ผ์š”์ผ๋ถ€ํ„ฐ ๋ชฉ์š”์ผ๊นŒ์ง€๋Š” ๋‹ฌ์ฝคํ•œ ๋””์ €ํŠธ๋ฅผ 1๊ฐœ๋‹น 2,023์›์˜ ํ˜œํƒ์„ ๋ฐ›์•„๋ณด์„ธ์š”. + + ๋ณ„์ด ๋œจ๋Š” ๋‚ ์—๋Š” ํ–‰์šด์ด ์ฐพ์•„์˜ฌ ๊ฑฐ์•ผ + ๋‹ฌ๋ ฅ์— ๋ณ„์ด ๋œฌ ๋‚ ์—๋Š” 1,000์›์˜ ์ถ”๊ฐ€ํ• ์ธ์ด ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค! + + ํŠน๋ณ„ํ•œ ๋‚ ์„ ๋” ํŠน๋ณ„ํ•˜๊ฒŒ, ์ƒดํŽ˜์ธ๊ณผ ํ•จ๊ป˜ํ•˜๋Š” ์šฐ์•„ํ•œ ์‹œ๊ฐ„ + ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก 12๋งŒ ์› ์ด์ƒ ์‹œ ์ƒดํŽ˜์ธ์„ ์ œ๊ณตํ•ด ๋“œ๋ฆฝ๋‹ˆ๋‹ค. + * ํ• ์ธ ์ „ ๊ธˆ์•ก ๊ธฐ์ค€ + + 1์„ 2์กฐ, ํ˜œํƒ๋„ ๋ฐ›๊ณ , ์ƒˆํ•ด์„ ๋ฌผ๋„ ๋ฐ›์ž! + ํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ผ ๋ฐ›๋Š” ์ด๋ฒคํŠธ ๋ฐฐ์ง€๋กœ 1์›” ์ƒˆํ•ด ์„ ๋ฌผ ๋ฐ›์•„ ๊ฐ€์„ธ์š”! ์„ ๋ฌผ์€ ์ƒˆํ•ด์ด๋ฒคํŠธ์— ๊ณต๊ฐœ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. + * 5์ฒœ ์› ์ด์ƒ : ๋ณ„, 1๋งŒ ์› ์ด์ƒ : ํŠธ๋ฆฌ, 2๋งŒ ์› ์ด์ƒ : ์‚ฐํƒ€ + + * ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก 10,000์› ์ด์ƒ๋ถ€ํ„ฐ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. + * ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ์‹œ, ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. + * ๋ฉ”๋‰ด๋Š” ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 20๊ฐœ๊นŒ์ง€๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + ------------------------ ์šฐํ…Œ์ฝ” ์‹๋‹น ๋ฉ”๋‰ด ------------------------ + + <์• ํ”ผํƒ€์ด์ €> + ์–‘์†ก์ด์ˆ˜ํ”„(6,000), ํƒ€ํŒŒ์Šค(5,500), ์‹œ์ €์ƒ๋Ÿฌ๋“œ(8,000) + + <๋ฉ”์ธ> + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ(55,000), ๋ฐ”๋น„ํ๋ฆฝ(54,000), ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€(35,000), ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€(25,000) + + <๋””์ €ํŠธ> + ์ดˆ์ฝ”์ผ€์ดํฌ(15,000), ์•„์ด์Šคํฌ๋ฆผ(5,000) + + <์Œ๋ฃŒ> + ์ œ๋กœ์ฝœ๋ผ(3,000), ๋ ˆ๋“œ์™€์ธ(60,000), ์ƒดํŽ˜์ธ(25,000) + + --------------------------------------------------------------- + ์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค. + """; + + private CommentConstants() { + } +} \ No newline at end of file
Java
์—‡ ์ด๊ฑด ํ˜น์‹œ ์‚ฌ์šฉ๋˜๋Š” ์•Š๋Š” ๋ฉ”์‹œ์ง€์ผ๊นŒ์š”!?
@@ -0,0 +1,11 @@ +package christmas.domain.discount; + +import christmas.constant.DiscountPolicyName; +import christmas.service.dto.OrderDto; + +public interface DiscountPolicy { + + int discount(final OrderDto order); + + DiscountPolicyName getDiscountPolicyName(); +} \ No newline at end of file
Java
์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์ž˜ ์‚ฌ์šฉํ•˜์‹  ๊ฑฐ ๊ฐ™์•„์š”! ๊ฐ ํ• ์ธ์— ๋Œ€ํ•ด ํ™•์žฅ์ด๋‚˜ ๋ณ€๊ฒฝ์ด ์‰ฌ์šธ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,52 @@ +package christmas.domain.menu; + +import java.util.Arrays; + +public enum Menu { + + MUSHROOM_SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6_000, MenuCategory.APPETIZER), + TAPAS("ํƒ€ํŒŒ์Šค", 5_500, MenuCategory.APPETIZER), + CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000, MenuCategory.APPETIZER), + + T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000, MenuCategory.MAIN), + BBQ_RIB("๋ฐ”๋น„ํ๋ฆฝ", 54_000, MenuCategory.MAIN), + SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000, MenuCategory.MAIN), + CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000, MenuCategory.MAIN), + + CHOCOLATE_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15_000, MenuCategory.DESSERT), + ICE_CREAM("์•„์ด์Šคํฌ๋ฆผ", 5_000, MenuCategory.DESSERT), + + ZERO_COLA("์ œ๋กœ์ฝœ๋ผ", 3_000, MenuCategory.DRINK), + RED_WINE("๋ ˆ๋“œ์™€์ธ", 60_000, MenuCategory.DRINK), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, MenuCategory.DRINK); + + private static final String INVALID_ORDER_MESSAGE = "์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private final String name; + private final int price; + private final MenuCategory menuCategory; + + Menu(final String name, final int price, final MenuCategory menuCategory) { + this.name = name; + this.price = price; + this.menuCategory = menuCategory; + } + + public static Menu findByName(final String name) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(name)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(INVALID_ORDER_MESSAGE)); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public MenuCategory getMenuCategory() { + return menuCategory; + } +} \ No newline at end of file
Java
์š” ์ฝ”๋“œ๋Š” ์ €๋ž‘ ๊ฑฐ์˜ ๋˜‘๊ฐ™๋„ค์š” ใ…Žใ…Ž ์ด๋ฆ„๋„ ๋˜‘๊ฐ™์€๋ฐ ์‹ ๊ธฐํ•˜๋„ค์š” ใ…‹ใ…‹ใ…‹ ๋ฉ”๋‰ด์— ๋Œ€ํ•œ ์ถ”๊ฐ€์ ์ธ ๋กœ์ง์ด ์—†์–ด์„œ ์ด๋ ‡๊ฒŒ ๋ฉ”๋‰ด๋ฅผ ๋ชจ์•„๋†“๋Š” ๊ฒŒ ๋” ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,77 @@ +package christmas.domain.discount; + +import christmas.constant.DiscountConstants; +import christmas.constant.DiscountPolicyName; +import christmas.domain.menu.Menu; +import christmas.domain.menu.MenuCategory; +import christmas.service.dto.OrderDto; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.Map; +import java.util.Map.Entry; + + +public class WeekendDiscountPolicy implements DiscountPolicy { + private static final int WEEKEND_DISCOUNT_AMOUNT = 2023; + + @Override + public int discount(final OrderDto order) { + LocalDate orderDate = order.getDate(); + if (isWithinDiscountPeriod(orderDate) && isWeekend(orderDate)) { + return calculateTotalDiscount(order.getMenus()); + } + return DiscountConstants.NO_DISCOUNT; + } + + @Override + public DiscountPolicyName getDiscountPolicyName() { + return DiscountPolicyName.WEEKEND_DISCOUNT; + } + + private int calculateTotalDiscount(Map<Menu, Integer> menus) { + int totalDiscount = 0; + for (Entry<Menu, Integer> menu : menus.entrySet()) { + totalDiscount += calculateDiscountForMenu(menu); + } + return totalDiscount; + } + + private int calculateDiscountForMenu(Entry<Menu, Integer> menu) { + if (isMainCategory(menu.getKey())) { + return calculateMenuDiscount(menu); + } + return 0; + } + + private boolean isMainCategory(Menu menu) { + return menu.getMenuCategory() == MenuCategory.MAIN; + } + + private int calculateMenuDiscount(Map.Entry<Menu, Integer> menu) { + return WEEKEND_DISCOUNT_AMOUNT * menu.getValue(); + } + + private boolean isWithinDiscountPeriod(LocalDate date) { + return isNotBeforeStartDate(date) && isNotAfterEndDate(date); + } + + private boolean isNotBeforeStartDate(LocalDate date) { + return !date.isBefore(DiscountConstants.START_DAY_OF_MONTH); + } + + private boolean isNotAfterEndDate(LocalDate date) { + return !date.isAfter(DiscountConstants.END_DAY_OF_MONTH); + } + + private boolean isWeekend(LocalDate date) { + return isFriday(date) || isSaturday(date); + } + + private boolean isFriday(LocalDate date) { + return date.getDayOfWeek() == DayOfWeek.FRIDAY; + } + + private boolean isSaturday(LocalDate date) { + return date.getDayOfWeek() == DayOfWeek.SATURDAY; + } +} \ No newline at end of file
Java
DayOfWeek ์‚ฌ์šฉ๋ฒ• ์ €๋งŒ ๋ชฐ๋ž๊ตฐ์š”.. ๋˜๊ฒŒ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฑฐ ๊ฐ™์•„์š”! ๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,74 @@ +package christmas.domain; + +import christmas.constant.DiscountConstants; +import christmas.constant.DiscountPolicyName; +import christmas.domain.discount.ChristmasDailyDiscountPolicy; +import christmas.domain.discount.DiscountPolicy; +import christmas.domain.discount.GiftEventPolicy; +import christmas.domain.discount.SpecialDiscountPolicy; +import christmas.domain.discount.WeekdayDiscountPolicy; +import christmas.domain.discount.WeekendDiscountPolicy; +import christmas.domain.menu.Menu; +import christmas.service.dto.OrderDto; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; + +public class ChristmasEvent { + private static final int MINIMUM_AMOUNT_FOR_DISCOUNT = 10000; + private static final int MINIMUM_AMOUNT_FOR_GIFT = 120000; + private final List<DiscountPolicy> discountPolicies; + private final GiftEventPolicy giftEventPolicy; + private final Map<Menu, Integer> giftMenu; + + public ChristmasEvent() { + this.discountPolicies = List.of( + new ChristmasDailyDiscountPolicy(), + new WeekdayDiscountPolicy(), + new WeekendDiscountPolicy(), + new SpecialDiscountPolicy()); + this.giftEventPolicy = new GiftEventPolicy(); + this.giftMenu = new EnumMap<>(Menu.class); + } + + public Map<DiscountPolicyName, Integer> calculateBenefitDetails(final OrderDto order) { + Map<DiscountPolicyName, Integer> benefitDetail = new EnumMap<>(DiscountPolicyName.class); + if (order.getTotalPrice() >= MINIMUM_AMOUNT_FOR_DISCOUNT) { + benefitDetail = applyDiscountPolicies(order); + addGift(order, benefitDetail); + } + return benefitDetail; + } + + public int calculateTotalDiscount(final OrderDto order) { + Map<DiscountPolicyName, Integer> discountDetail = calculateBenefitDetails(order); + return discountDetail.values() + .stream() + .mapToInt(Integer::intValue) + .sum(); + } + + private void addGift(final OrderDto order, final Map<DiscountPolicyName, Integer> benefitDetail) { + if (order.getTotalPrice() >= MINIMUM_AMOUNT_FOR_GIFT) { + int giftPrice = giftEventPolicy.discountGiftPrice(order); + benefitDetail.put(DiscountPolicyName.GIFT_EVENT, giftPrice); + giftMenu.put(Menu.CHAMPAGNE, 1); + } + } + + private Map<DiscountPolicyName, Integer> applyDiscountPolicies(final OrderDto order) { + Map<DiscountPolicyName, Integer> discountDetail = new EnumMap<>(DiscountPolicyName.class); + for (DiscountPolicy discountPolicy : discountPolicies) { + int discountAmount = discountPolicy.discount(order); + if (discountAmount != DiscountConstants.NO_DISCOUNT) { + discountDetail.put(discountPolicy.getDiscountPolicyName(), discountAmount); + } + } + return discountDetail; + } + + public Map<Menu, Integer> getGiftMenu(final OrderDto order) { + calculateBenefitDetails(order); + return giftMenu; + } +} \ No newline at end of file
Java
์ฆ์ • ๋ฉ”๋‰ด์— ๋Œ€ํ•ด์„œ ํ™•์žฅ์„ฑ์ด ๋ณด์—ฌ์„œ ์ข‹์€ ๊ฑฐ ๊ฐ™์•„์š”! ๐Ÿ‘
@@ -0,0 +1,54 @@ +package christmas.constant; + +public final class CommentConstants { + public static final String EVENT_INFORMATION_MESSAGE = """ + ์šฐํ…Œ์ฝ” ์‹๋‹น์˜ ์ตœ๋Œ€ ์ด๋ฒคํŠธ! 12์›” ์ด๋ฒคํŠธ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค! + ์ตœ๋Œ€ 3๋ฒˆ์˜ ์ค‘๋ณต ํ• ์ธ๊ณผ ์šฐํ…Œ์ฝ”๊ฐ€ ์ค€๋น„ํ•œ ๋‘ ๊ฐœ์˜ ๊นœ์ง ์„ ๋ฌผ์„ ๋ฐ›์•„ ๊ฐ€์„ธ์š”! + * ๋ชจ๋“  ์ด๋ฒคํŠธ๋Š” ํƒ€ ์ด๋ฒคํŠธ์™€ ์ค‘๋ณต ์ ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. + + ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด๋ฅผ ๊ฐ™์ด ์„ธ์–ด๋ณด์•„์š”. + ์ตœ๋Œ€ 3400์› ํ• ์ธ! 1์ผ 1000์›์œผ๋กœ ์‹œ์ž‘ํ•˜์—ฌ 25์ผ๊นŒ์ง€ 100์›์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ํ• ์ธ! + ํฌ๋ฆฌ์Šค๋งˆ์Šค์˜ ์„ ๋ฌผ์„ ๋А๊ปด๋ณด์„ธ์š”! + * 25์ผ๊นŒ์ง€ + + ์ง€๋‚˜๊ฐ€๋Š” 2023๋…„์„ ์œ„ํ•œ, 2,023์› ํ• ์ธ ์ด๋ฒคํŠธ! + ๊ธˆ์š”์ผ, ํ† ์š”์ผ์—๋Š” ๋ฉ”์ธ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์›์„ ํ• ์ธํ•ฉ๋‹ˆ๋‹ค! + ๋‹ค๋ฅธ ๋‚  ๋ฐฉ๋ฌธํ•ด๋„ ์„ญ์„ญํ•ดํ•˜์ง€ ๋งˆ์„ธ์š”. + ์ผ์š”์ผ๋ถ€ํ„ฐ ๋ชฉ์š”์ผ๊นŒ์ง€๋Š” ๋‹ฌ์ฝคํ•œ ๋””์ €ํŠธ๋ฅผ 1๊ฐœ๋‹น 2,023์›์˜ ํ˜œํƒ์„ ๋ฐ›์•„๋ณด์„ธ์š”. + + ๋ณ„์ด ๋œจ๋Š” ๋‚ ์—๋Š” ํ–‰์šด์ด ์ฐพ์•„์˜ฌ ๊ฑฐ์•ผ + ๋‹ฌ๋ ฅ์— ๋ณ„์ด ๋œฌ ๋‚ ์—๋Š” 1,000์›์˜ ์ถ”๊ฐ€ํ• ์ธ์ด ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค! + + ํŠน๋ณ„ํ•œ ๋‚ ์„ ๋” ํŠน๋ณ„ํ•˜๊ฒŒ, ์ƒดํŽ˜์ธ๊ณผ ํ•จ๊ป˜ํ•˜๋Š” ์šฐ์•„ํ•œ ์‹œ๊ฐ„ + ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก 12๋งŒ ์› ์ด์ƒ ์‹œ ์ƒดํŽ˜์ธ์„ ์ œ๊ณตํ•ด ๋“œ๋ฆฝ๋‹ˆ๋‹ค. + * ํ• ์ธ ์ „ ๊ธˆ์•ก ๊ธฐ์ค€ + + 1์„ 2์กฐ, ํ˜œํƒ๋„ ๋ฐ›๊ณ , ์ƒˆํ•ด์„ ๋ฌผ๋„ ๋ฐ›์ž! + ํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ผ ๋ฐ›๋Š” ์ด๋ฒคํŠธ ๋ฐฐ์ง€๋กœ 1์›” ์ƒˆํ•ด ์„ ๋ฌผ ๋ฐ›์•„ ๊ฐ€์„ธ์š”! ์„ ๋ฌผ์€ ์ƒˆํ•ด์ด๋ฒคํŠธ์— ๊ณต๊ฐœ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. + * 5์ฒœ ์› ์ด์ƒ : ๋ณ„, 1๋งŒ ์› ์ด์ƒ : ํŠธ๋ฆฌ, 2๋งŒ ์› ์ด์ƒ : ์‚ฐํƒ€ + + * ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก 10,000์› ์ด์ƒ๋ถ€ํ„ฐ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. + * ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ์‹œ, ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. + * ๋ฉ”๋‰ด๋Š” ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 20๊ฐœ๊นŒ์ง€๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + ------------------------ ์šฐํ…Œ์ฝ” ์‹๋‹น ๋ฉ”๋‰ด ------------------------ + + <์• ํ”ผํƒ€์ด์ €> + ์–‘์†ก์ด์ˆ˜ํ”„(6,000), ํƒ€ํŒŒ์Šค(5,500), ์‹œ์ €์ƒ๋Ÿฌ๋“œ(8,000) + + <๋ฉ”์ธ> + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ(55,000), ๋ฐ”๋น„ํ๋ฆฝ(54,000), ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€(35,000), ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€(25,000) + + <๋””์ €ํŠธ> + ์ดˆ์ฝ”์ผ€์ดํฌ(15,000), ์•„์ด์Šคํฌ๋ฆผ(5,000) + + <์Œ๋ฃŒ> + ์ œ๋กœ์ฝœ๋ผ(3,000), ๋ ˆ๋“œ์™€์ธ(60,000), ์ƒดํŽ˜์ธ(25,000) + + --------------------------------------------------------------- + ์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค. + """; + + private CommentConstants() { + } +} \ No newline at end of file
Java
์—‡ ์ด๊ฑด ํ˜น์‹œ ์‚ฌ์šฉ๋˜๋Š” ์•Š๋Š” ๋ฉ”์‹œ์ง€์ผ๊นŒ์š”!?
@@ -0,0 +1,11 @@ +package christmas.domain.discount; + +import christmas.constant.DiscountPolicyName; +import christmas.service.dto.OrderDto; + +public interface DiscountPolicy { + + int discount(final OrderDto order); + + DiscountPolicyName getDiscountPolicyName(); +} \ No newline at end of file
Java
์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์ž˜ ์‚ฌ์šฉํ•˜์‹  ๊ฑฐ ๊ฐ™์•„์š”! ๊ฐ ํ• ์ธ์— ๋Œ€ํ•ด ํ™•์žฅ์ด๋‚˜ ๋ณ€๊ฒฝ์ด ์‰ฌ์šธ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,52 @@ +package christmas.domain.menu; + +import java.util.Arrays; + +public enum Menu { + + MUSHROOM_SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6_000, MenuCategory.APPETIZER), + TAPAS("ํƒ€ํŒŒ์Šค", 5_500, MenuCategory.APPETIZER), + CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000, MenuCategory.APPETIZER), + + T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000, MenuCategory.MAIN), + BBQ_RIB("๋ฐ”๋น„ํ๋ฆฝ", 54_000, MenuCategory.MAIN), + SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000, MenuCategory.MAIN), + CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000, MenuCategory.MAIN), + + CHOCOLATE_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15_000, MenuCategory.DESSERT), + ICE_CREAM("์•„์ด์Šคํฌ๋ฆผ", 5_000, MenuCategory.DESSERT), + + ZERO_COLA("์ œ๋กœ์ฝœ๋ผ", 3_000, MenuCategory.DRINK), + RED_WINE("๋ ˆ๋“œ์™€์ธ", 60_000, MenuCategory.DRINK), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, MenuCategory.DRINK); + + private static final String INVALID_ORDER_MESSAGE = "์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private final String name; + private final int price; + private final MenuCategory menuCategory; + + Menu(final String name, final int price, final MenuCategory menuCategory) { + this.name = name; + this.price = price; + this.menuCategory = menuCategory; + } + + public static Menu findByName(final String name) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(name)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(INVALID_ORDER_MESSAGE)); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public MenuCategory getMenuCategory() { + return menuCategory; + } +} \ No newline at end of file
Java
์š” ์ฝ”๋“œ๋Š” ์ €๋ž‘ ๊ฑฐ์˜ ๋˜‘๊ฐ™๋„ค์š” ใ…Žใ…Ž ์ด๋ฆ„๋„ ๋˜‘๊ฐ™์€๋ฐ ์‹ ๊ธฐํ•˜๋„ค์š” ใ…‹ใ…‹ใ…‹ ๋ฉ”๋‰ด์— ๋Œ€ํ•œ ์ถ”๊ฐ€์ ์ธ ๋กœ์ง์ด ์—†์–ด์„œ ์ด๋ ‡๊ฒŒ ๋ฉ”๋‰ด๋ฅผ ๋ชจ์•„๋†“๋Š” ๊ฒŒ ๋” ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,77 @@ +package christmas.domain.discount; + +import christmas.constant.DiscountConstants; +import christmas.constant.DiscountPolicyName; +import christmas.domain.menu.Menu; +import christmas.domain.menu.MenuCategory; +import christmas.service.dto.OrderDto; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.Map; +import java.util.Map.Entry; + + +public class WeekendDiscountPolicy implements DiscountPolicy { + private static final int WEEKEND_DISCOUNT_AMOUNT = 2023; + + @Override + public int discount(final OrderDto order) { + LocalDate orderDate = order.getDate(); + if (isWithinDiscountPeriod(orderDate) && isWeekend(orderDate)) { + return calculateTotalDiscount(order.getMenus()); + } + return DiscountConstants.NO_DISCOUNT; + } + + @Override + public DiscountPolicyName getDiscountPolicyName() { + return DiscountPolicyName.WEEKEND_DISCOUNT; + } + + private int calculateTotalDiscount(Map<Menu, Integer> menus) { + int totalDiscount = 0; + for (Entry<Menu, Integer> menu : menus.entrySet()) { + totalDiscount += calculateDiscountForMenu(menu); + } + return totalDiscount; + } + + private int calculateDiscountForMenu(Entry<Menu, Integer> menu) { + if (isMainCategory(menu.getKey())) { + return calculateMenuDiscount(menu); + } + return 0; + } + + private boolean isMainCategory(Menu menu) { + return menu.getMenuCategory() == MenuCategory.MAIN; + } + + private int calculateMenuDiscount(Map.Entry<Menu, Integer> menu) { + return WEEKEND_DISCOUNT_AMOUNT * menu.getValue(); + } + + private boolean isWithinDiscountPeriod(LocalDate date) { + return isNotBeforeStartDate(date) && isNotAfterEndDate(date); + } + + private boolean isNotBeforeStartDate(LocalDate date) { + return !date.isBefore(DiscountConstants.START_DAY_OF_MONTH); + } + + private boolean isNotAfterEndDate(LocalDate date) { + return !date.isAfter(DiscountConstants.END_DAY_OF_MONTH); + } + + private boolean isWeekend(LocalDate date) { + return isFriday(date) || isSaturday(date); + } + + private boolean isFriday(LocalDate date) { + return date.getDayOfWeek() == DayOfWeek.FRIDAY; + } + + private boolean isSaturday(LocalDate date) { + return date.getDayOfWeek() == DayOfWeek.SATURDAY; + } +} \ No newline at end of file
Java
DayOfWeek ์‚ฌ์šฉ๋ฒ• ์ €๋งŒ ๋ชฐ๋ž๊ตฐ์š”.. ๋˜๊ฒŒ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฑฐ ๊ฐ™์•„์š”! ๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,74 @@ +package christmas.domain; + +import christmas.constant.DiscountConstants; +import christmas.constant.DiscountPolicyName; +import christmas.domain.discount.ChristmasDailyDiscountPolicy; +import christmas.domain.discount.DiscountPolicy; +import christmas.domain.discount.GiftEventPolicy; +import christmas.domain.discount.SpecialDiscountPolicy; +import christmas.domain.discount.WeekdayDiscountPolicy; +import christmas.domain.discount.WeekendDiscountPolicy; +import christmas.domain.menu.Menu; +import christmas.service.dto.OrderDto; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; + +public class ChristmasEvent { + private static final int MINIMUM_AMOUNT_FOR_DISCOUNT = 10000; + private static final int MINIMUM_AMOUNT_FOR_GIFT = 120000; + private final List<DiscountPolicy> discountPolicies; + private final GiftEventPolicy giftEventPolicy; + private final Map<Menu, Integer> giftMenu; + + public ChristmasEvent() { + this.discountPolicies = List.of( + new ChristmasDailyDiscountPolicy(), + new WeekdayDiscountPolicy(), + new WeekendDiscountPolicy(), + new SpecialDiscountPolicy()); + this.giftEventPolicy = new GiftEventPolicy(); + this.giftMenu = new EnumMap<>(Menu.class); + } + + public Map<DiscountPolicyName, Integer> calculateBenefitDetails(final OrderDto order) { + Map<DiscountPolicyName, Integer> benefitDetail = new EnumMap<>(DiscountPolicyName.class); + if (order.getTotalPrice() >= MINIMUM_AMOUNT_FOR_DISCOUNT) { + benefitDetail = applyDiscountPolicies(order); + addGift(order, benefitDetail); + } + return benefitDetail; + } + + public int calculateTotalDiscount(final OrderDto order) { + Map<DiscountPolicyName, Integer> discountDetail = calculateBenefitDetails(order); + return discountDetail.values() + .stream() + .mapToInt(Integer::intValue) + .sum(); + } + + private void addGift(final OrderDto order, final Map<DiscountPolicyName, Integer> benefitDetail) { + if (order.getTotalPrice() >= MINIMUM_AMOUNT_FOR_GIFT) { + int giftPrice = giftEventPolicy.discountGiftPrice(order); + benefitDetail.put(DiscountPolicyName.GIFT_EVENT, giftPrice); + giftMenu.put(Menu.CHAMPAGNE, 1); + } + } + + private Map<DiscountPolicyName, Integer> applyDiscountPolicies(final OrderDto order) { + Map<DiscountPolicyName, Integer> discountDetail = new EnumMap<>(DiscountPolicyName.class); + for (DiscountPolicy discountPolicy : discountPolicies) { + int discountAmount = discountPolicy.discount(order); + if (discountAmount != DiscountConstants.NO_DISCOUNT) { + discountDetail.put(discountPolicy.getDiscountPolicyName(), discountAmount); + } + } + return discountDetail; + } + + public Map<Menu, Integer> getGiftMenu(final OrderDto order) { + calculateBenefitDetails(order); + return giftMenu; + } +} \ No newline at end of file
Java
์ฆ์ • ๋ฉ”๋‰ด์— ๋Œ€ํ•ด์„œ ํ™•์žฅ์„ฑ์ด ๋ณด์—ฌ์„œ ์ข‹์€ ๊ฑฐ ๊ฐ™์•„์š”! ๐Ÿ‘
@@ -0,0 +1,115 @@ +package christmas.controller; + +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import christmas.model.strategy.WootecoBadgeStrategy; +import christmas.model.strategy.WootecoDiscountStrategy; +import christmas.service.PromotionService; +import christmas.util.Parser; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.EnumMap; +import java.util.Map; +import java.util.function.Supplier; + +public class PromotionController { + private final PromotionService promotion; + private final DiscountStrategy discountStrategy = new WootecoDiscountStrategy(); + private final BadgeStrategy badgeStrategy = new WootecoBadgeStrategy(); + + public PromotionController() { + this.promotion = new PromotionService(discountStrategy, badgeStrategy); + } + + public void run() { + InputView.printGreetingMessage(); + Date date = getDate(); + MenuCount menuCount = getMenu(); + displayEventGuideMessage(date); + displayMenuAndOrderAmount(menuCount); + displayPromotionResult(menuCount, date); + } + + private Date getDate() { + return executeWithExceptionHandle(() -> { + int inputDate = InputView.readDate(); + return Date.of(inputDate); + }); + } + + private MenuCount getMenu() { + return executeWithExceptionHandle(() -> { + String input = InputView.readMenu(); + Map<String, Integer> parsedMenu = Parser.parseMenuCount(input); + return new MenuCount(parsedMenu); + }); + } + + private void displayMenuAndOrderAmount(MenuCount menuCount) { + displayMenuInfo(menuCount); + displayTotalOrderAmount(menuCount); + } + + private void displayPromotionResult(MenuCount menuCount, Date date) { + displayGiftEvent(menuCount); + displayPromotionEvent(menuCount, date); + displayTotalPromotionAmount(menuCount, date); + displayExpectedPaymentAmount(menuCount, date); + displayEventBadge(menuCount, date); + } + + private void displayEventGuideMessage(Date date) { + OutputView.printEventGuideMessage(date.getValue()); + } + + + private void displayMenuInfo(MenuCount menuCount) { + OutputView.printOrderedMenu(menuCount); + } + + private void displayTotalOrderAmount(MenuCount menuCount) { + OutputView.printTotalOrderAmount(menuCount.calculateTotalAmount()); + } + + private void displayGiftEvent(MenuCount menuCount) { + boolean canGiveGift = menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount(); + OutputView.printGiftEvent(canGiveGift); + } + + private void displayPromotionEvent(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = promotion.calculatePromotionStatus(menuCount, date); + OutputView.printPromotionStatus(promotionStatus); + } + + private void displayTotalPromotionAmount(MenuCount menuCount, Date date) { + int totalPromotionAmount = promotion.calculatePromotionAmount(menuCount, date); + OutputView.printPromotionAmount(totalPromotionAmount); + } + + private void displayExpectedPaymentAmount(MenuCount menuCount, Date date) { + int expectedPayment = + menuCount.calculateTotalAmount() - promotion.calaulateTotalDiscountAmount(menuCount, date); + OutputView.printExpectedPaymentAmount(expectedPayment); + } + + private void displayEventBadge(MenuCount menuCount, Date date) { + String badgeName = promotion.calculateBadgeStatus(menuCount, date); + OutputView.printEventBadge(badgeName); + } + + private static <T> T executeWithExceptionHandle(final Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + +}
Java
์ปจํŠธ๋กค๋Ÿฌ๋Š” ํ๋ฆ„์„ ์ œ์–ดํ•˜๋Š” ์ฑ…์ž„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ ์ ์šฉ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ์ฑ…์ž„์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,115 @@ +package christmas.controller; + +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import christmas.model.strategy.WootecoBadgeStrategy; +import christmas.model.strategy.WootecoDiscountStrategy; +import christmas.service.PromotionService; +import christmas.util.Parser; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.EnumMap; +import java.util.Map; +import java.util.function.Supplier; + +public class PromotionController { + private final PromotionService promotion; + private final DiscountStrategy discountStrategy = new WootecoDiscountStrategy(); + private final BadgeStrategy badgeStrategy = new WootecoBadgeStrategy(); + + public PromotionController() { + this.promotion = new PromotionService(discountStrategy, badgeStrategy); + } + + public void run() { + InputView.printGreetingMessage(); + Date date = getDate(); + MenuCount menuCount = getMenu(); + displayEventGuideMessage(date); + displayMenuAndOrderAmount(menuCount); + displayPromotionResult(menuCount, date); + } + + private Date getDate() { + return executeWithExceptionHandle(() -> { + int inputDate = InputView.readDate(); + return Date.of(inputDate); + }); + } + + private MenuCount getMenu() { + return executeWithExceptionHandle(() -> { + String input = InputView.readMenu(); + Map<String, Integer> parsedMenu = Parser.parseMenuCount(input); + return new MenuCount(parsedMenu); + }); + } + + private void displayMenuAndOrderAmount(MenuCount menuCount) { + displayMenuInfo(menuCount); + displayTotalOrderAmount(menuCount); + } + + private void displayPromotionResult(MenuCount menuCount, Date date) { + displayGiftEvent(menuCount); + displayPromotionEvent(menuCount, date); + displayTotalPromotionAmount(menuCount, date); + displayExpectedPaymentAmount(menuCount, date); + displayEventBadge(menuCount, date); + } + + private void displayEventGuideMessage(Date date) { + OutputView.printEventGuideMessage(date.getValue()); + } + + + private void displayMenuInfo(MenuCount menuCount) { + OutputView.printOrderedMenu(menuCount); + } + + private void displayTotalOrderAmount(MenuCount menuCount) { + OutputView.printTotalOrderAmount(menuCount.calculateTotalAmount()); + } + + private void displayGiftEvent(MenuCount menuCount) { + boolean canGiveGift = menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount(); + OutputView.printGiftEvent(canGiveGift); + } + + private void displayPromotionEvent(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = promotion.calculatePromotionStatus(menuCount, date); + OutputView.printPromotionStatus(promotionStatus); + } + + private void displayTotalPromotionAmount(MenuCount menuCount, Date date) { + int totalPromotionAmount = promotion.calculatePromotionAmount(menuCount, date); + OutputView.printPromotionAmount(totalPromotionAmount); + } + + private void displayExpectedPaymentAmount(MenuCount menuCount, Date date) { + int expectedPayment = + menuCount.calculateTotalAmount() - promotion.calaulateTotalDiscountAmount(menuCount, date); + OutputView.printExpectedPaymentAmount(expectedPayment); + } + + private void displayEventBadge(MenuCount menuCount, Date date) { + String badgeName = promotion.calculateBadgeStatus(menuCount, date); + OutputView.printEventBadge(badgeName); + } + + private static <T> T executeWithExceptionHandle(final Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + +}
Java
`๋‚ ์งœ๋‚˜ ๋ฉ”๋‰ด๋ฅผ ์–ป๋Š”๋‹ค`๋ณด๋‹ค `์œ ํšจํ•œ ๊ฐ’์„ ์–ป์„๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค`๋ผ๋Š” ์ข€ ๋” ๋ช…ํ™•ํ•œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”? e.g.)`retryInputForValidVisitDate`, `returyInputForValidOrders`
@@ -0,0 +1,70 @@ +package christmas.service; + +import static christmas.model.constant.DiscountConstant.MIN_DISCOUNT_SERVICE; +import static christmas.model.constant.DiscountConstant.NO_DISCOUNT; +import static christmas.model.constant.PromotionConstant.CHRISTMAS_DISCOUNT; +import static christmas.model.constant.PromotionConstant.GIFT_EVENT; +import static christmas.model.constant.PromotionConstant.SPECIAL_DISCOUNT; +import static christmas.model.constant.PromotionConstant.WEEKDAY_DISCOUNT; +import static christmas.model.constant.PromotionConstant.WEEKEND_DISCOUNT; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.EventBadge; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import java.util.EnumMap; + +public class PromotionService { + private final DiscountStrategy discountStrategy; + private final BadgeStrategy badgeStrategy; + + + public PromotionService(DiscountStrategy discountStrategy, BadgeStrategy badgeStrategy) { + this.discountStrategy = discountStrategy; + this.badgeStrategy = badgeStrategy; + } + + public boolean canGetDiscount(MenuCount menuCount) { + return menuCount.calculateTotalAmount() >= MIN_DISCOUNT_SERVICE.getAmount(); + } + + public int calculatePromotionAmount(MenuCount menuCount, Date date) { + if (canGetDiscount(menuCount)) { + return calaulateTotalDiscountAmount(menuCount, date) + + discountStrategy.giftEventDiscount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + public int calaulateTotalDiscountAmount(MenuCount menuCount, Date date) { + if (canGetDiscount(menuCount)) { + return discountStrategy.christmasDiscount(date) + + discountStrategy.weekdayDiscount(menuCount, date) + + discountStrategy.specialDayDiscount(date) + + discountStrategy.weekendDiscount(menuCount, date); + } + return NO_DISCOUNT.getAmount(); + } + + public EnumMap<PromotionConstant, Integer> calculatePromotionStatus(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = new EnumMap<>(PromotionConstant.class); + if (canGetDiscount(menuCount)) { + promotionStatus.put(CHRISTMAS_DISCOUNT, discountStrategy.christmasDiscount(date)); + promotionStatus.put(WEEKDAY_DISCOUNT, discountStrategy.weekdayDiscount(menuCount, date)); + promotionStatus.put(WEEKEND_DISCOUNT, discountStrategy.weekendDiscount(menuCount, date)); + promotionStatus.put(SPECIAL_DISCOUNT, discountStrategy.specialDayDiscount(date)); + promotionStatus.put(GIFT_EVENT, discountStrategy.giftEventDiscount(menuCount)); + return promotionStatus; + } + return promotionStatus; + } + + public String calculateBadgeStatus(MenuCount menuCount, Date date) { + int promotionAmount = calculatePromotionAmount(menuCount, date); + EventBadge badge = badgeStrategy.calculateBadgeGrade(promotionAmount); + return badge.getName(); + } + +}
Java
์ด๋ฒคํŠธ๊ฐ€ ์‹ ๊ทœ๋กœ ๋„์ž…๋˜๊ฑฐ๋‚˜ ์‚ญ์ œ๋˜๋Š” ๊ฒฝ์šฐ ์„œ๋น„์Šค์— ์žˆ๋Š” ๊ณ„์‚ฐ ๊ธฐ๋Šฅ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์˜์กด๋„๋ฅผ ๋‚ฎ์ถ”๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,15 @@ +package christmas.model.strategy; + +import christmas.model.constant.EventBadge; + +public class WootecoBadgeStrategy implements BadgeStrategy { + @Override + public EventBadge calculateBadgeGrade(int promotionAmount) { + for (EventBadge badge : EventBadge.values()){ + if (promotionAmount >= badge.getBaseAmount()){ + return badge; + } + } + return EventBadge.NOTHING; + } +}
Java
ํ˜„์žฌ๋Š” EventBadge๊ฐ€ ๊ธฐ์ค€ ๊ธˆ์•ก์ด ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ์ง€๋งŒ ๋งŒ์ผ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์ˆœ์„œ๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ๋ฑƒ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๊ณ ๋ คํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,115 @@ +package christmas.controller; + +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import christmas.model.strategy.WootecoBadgeStrategy; +import christmas.model.strategy.WootecoDiscountStrategy; +import christmas.service.PromotionService; +import christmas.util.Parser; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.EnumMap; +import java.util.Map; +import java.util.function.Supplier; + +public class PromotionController { + private final PromotionService promotion; + private final DiscountStrategy discountStrategy = new WootecoDiscountStrategy(); + private final BadgeStrategy badgeStrategy = new WootecoBadgeStrategy(); + + public PromotionController() { + this.promotion = new PromotionService(discountStrategy, badgeStrategy); + } + + public void run() { + InputView.printGreetingMessage(); + Date date = getDate(); + MenuCount menuCount = getMenu(); + displayEventGuideMessage(date); + displayMenuAndOrderAmount(menuCount); + displayPromotionResult(menuCount, date); + } + + private Date getDate() { + return executeWithExceptionHandle(() -> { + int inputDate = InputView.readDate(); + return Date.of(inputDate); + }); + } + + private MenuCount getMenu() { + return executeWithExceptionHandle(() -> { + String input = InputView.readMenu(); + Map<String, Integer> parsedMenu = Parser.parseMenuCount(input); + return new MenuCount(parsedMenu); + }); + } + + private void displayMenuAndOrderAmount(MenuCount menuCount) { + displayMenuInfo(menuCount); + displayTotalOrderAmount(menuCount); + } + + private void displayPromotionResult(MenuCount menuCount, Date date) { + displayGiftEvent(menuCount); + displayPromotionEvent(menuCount, date); + displayTotalPromotionAmount(menuCount, date); + displayExpectedPaymentAmount(menuCount, date); + displayEventBadge(menuCount, date); + } + + private void displayEventGuideMessage(Date date) { + OutputView.printEventGuideMessage(date.getValue()); + } + + + private void displayMenuInfo(MenuCount menuCount) { + OutputView.printOrderedMenu(menuCount); + } + + private void displayTotalOrderAmount(MenuCount menuCount) { + OutputView.printTotalOrderAmount(menuCount.calculateTotalAmount()); + } + + private void displayGiftEvent(MenuCount menuCount) { + boolean canGiveGift = menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount(); + OutputView.printGiftEvent(canGiveGift); + } + + private void displayPromotionEvent(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = promotion.calculatePromotionStatus(menuCount, date); + OutputView.printPromotionStatus(promotionStatus); + } + + private void displayTotalPromotionAmount(MenuCount menuCount, Date date) { + int totalPromotionAmount = promotion.calculatePromotionAmount(menuCount, date); + OutputView.printPromotionAmount(totalPromotionAmount); + } + + private void displayExpectedPaymentAmount(MenuCount menuCount, Date date) { + int expectedPayment = + menuCount.calculateTotalAmount() - promotion.calaulateTotalDiscountAmount(menuCount, date); + OutputView.printExpectedPaymentAmount(expectedPayment); + } + + private void displayEventBadge(MenuCount menuCount, Date date) { + String badgeName = promotion.calculateBadgeStatus(menuCount, date); + OutputView.printEventBadge(badgeName); + } + + private static <T> T executeWithExceptionHandle(final Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + +}
Java
ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ž˜ ํ™œ์šฉํ•˜์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ข‹์€ ์ง€์‹์„ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค.
@@ -0,0 +1,115 @@ +package christmas.controller; + +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import christmas.model.strategy.WootecoBadgeStrategy; +import christmas.model.strategy.WootecoDiscountStrategy; +import christmas.service.PromotionService; +import christmas.util.Parser; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.EnumMap; +import java.util.Map; +import java.util.function.Supplier; + +public class PromotionController { + private final PromotionService promotion; + private final DiscountStrategy discountStrategy = new WootecoDiscountStrategy(); + private final BadgeStrategy badgeStrategy = new WootecoBadgeStrategy(); + + public PromotionController() { + this.promotion = new PromotionService(discountStrategy, badgeStrategy); + } + + public void run() { + InputView.printGreetingMessage(); + Date date = getDate(); + MenuCount menuCount = getMenu(); + displayEventGuideMessage(date); + displayMenuAndOrderAmount(menuCount); + displayPromotionResult(menuCount, date); + } + + private Date getDate() { + return executeWithExceptionHandle(() -> { + int inputDate = InputView.readDate(); + return Date.of(inputDate); + }); + } + + private MenuCount getMenu() { + return executeWithExceptionHandle(() -> { + String input = InputView.readMenu(); + Map<String, Integer> parsedMenu = Parser.parseMenuCount(input); + return new MenuCount(parsedMenu); + }); + } + + private void displayMenuAndOrderAmount(MenuCount menuCount) { + displayMenuInfo(menuCount); + displayTotalOrderAmount(menuCount); + } + + private void displayPromotionResult(MenuCount menuCount, Date date) { + displayGiftEvent(menuCount); + displayPromotionEvent(menuCount, date); + displayTotalPromotionAmount(menuCount, date); + displayExpectedPaymentAmount(menuCount, date); + displayEventBadge(menuCount, date); + } + + private void displayEventGuideMessage(Date date) { + OutputView.printEventGuideMessage(date.getValue()); + } + + + private void displayMenuInfo(MenuCount menuCount) { + OutputView.printOrderedMenu(menuCount); + } + + private void displayTotalOrderAmount(MenuCount menuCount) { + OutputView.printTotalOrderAmount(menuCount.calculateTotalAmount()); + } + + private void displayGiftEvent(MenuCount menuCount) { + boolean canGiveGift = menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount(); + OutputView.printGiftEvent(canGiveGift); + } + + private void displayPromotionEvent(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = promotion.calculatePromotionStatus(menuCount, date); + OutputView.printPromotionStatus(promotionStatus); + } + + private void displayTotalPromotionAmount(MenuCount menuCount, Date date) { + int totalPromotionAmount = promotion.calculatePromotionAmount(menuCount, date); + OutputView.printPromotionAmount(totalPromotionAmount); + } + + private void displayExpectedPaymentAmount(MenuCount menuCount, Date date) { + int expectedPayment = + menuCount.calculateTotalAmount() - promotion.calaulateTotalDiscountAmount(menuCount, date); + OutputView.printExpectedPaymentAmount(expectedPayment); + } + + private void displayEventBadge(MenuCount menuCount, Date date) { + String badgeName = promotion.calculateBadgeStatus(menuCount, date); + OutputView.printEventBadge(badgeName); + } + + private static <T> T executeWithExceptionHandle(final Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + +}
Java
ํ•„๋“œ ์ฃผ์ž…๋ณด๋‹ค, ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ํ™œ์šฉํ•ด๋ณด๋ฉด ๋” ์œ ์—ฐํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,115 @@ +package christmas.controller; + +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import christmas.model.strategy.WootecoBadgeStrategy; +import christmas.model.strategy.WootecoDiscountStrategy; +import christmas.service.PromotionService; +import christmas.util.Parser; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.EnumMap; +import java.util.Map; +import java.util.function.Supplier; + +public class PromotionController { + private final PromotionService promotion; + private final DiscountStrategy discountStrategy = new WootecoDiscountStrategy(); + private final BadgeStrategy badgeStrategy = new WootecoBadgeStrategy(); + + public PromotionController() { + this.promotion = new PromotionService(discountStrategy, badgeStrategy); + } + + public void run() { + InputView.printGreetingMessage(); + Date date = getDate(); + MenuCount menuCount = getMenu(); + displayEventGuideMessage(date); + displayMenuAndOrderAmount(menuCount); + displayPromotionResult(menuCount, date); + } + + private Date getDate() { + return executeWithExceptionHandle(() -> { + int inputDate = InputView.readDate(); + return Date.of(inputDate); + }); + } + + private MenuCount getMenu() { + return executeWithExceptionHandle(() -> { + String input = InputView.readMenu(); + Map<String, Integer> parsedMenu = Parser.parseMenuCount(input); + return new MenuCount(parsedMenu); + }); + } + + private void displayMenuAndOrderAmount(MenuCount menuCount) { + displayMenuInfo(menuCount); + displayTotalOrderAmount(menuCount); + } + + private void displayPromotionResult(MenuCount menuCount, Date date) { + displayGiftEvent(menuCount); + displayPromotionEvent(menuCount, date); + displayTotalPromotionAmount(menuCount, date); + displayExpectedPaymentAmount(menuCount, date); + displayEventBadge(menuCount, date); + } + + private void displayEventGuideMessage(Date date) { + OutputView.printEventGuideMessage(date.getValue()); + } + + + private void displayMenuInfo(MenuCount menuCount) { + OutputView.printOrderedMenu(menuCount); + } + + private void displayTotalOrderAmount(MenuCount menuCount) { + OutputView.printTotalOrderAmount(menuCount.calculateTotalAmount()); + } + + private void displayGiftEvent(MenuCount menuCount) { + boolean canGiveGift = menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount(); + OutputView.printGiftEvent(canGiveGift); + } + + private void displayPromotionEvent(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = promotion.calculatePromotionStatus(menuCount, date); + OutputView.printPromotionStatus(promotionStatus); + } + + private void displayTotalPromotionAmount(MenuCount menuCount, Date date) { + int totalPromotionAmount = promotion.calculatePromotionAmount(menuCount, date); + OutputView.printPromotionAmount(totalPromotionAmount); + } + + private void displayExpectedPaymentAmount(MenuCount menuCount, Date date) { + int expectedPayment = + menuCount.calculateTotalAmount() - promotion.calaulateTotalDiscountAmount(menuCount, date); + OutputView.printExpectedPaymentAmount(expectedPayment); + } + + private void displayEventBadge(MenuCount menuCount, Date date) { + String badgeName = promotion.calculateBadgeStatus(menuCount, date); + OutputView.printEventBadge(badgeName); + } + + private static <T> T executeWithExceptionHandle(final Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + +}
Java
ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ static์œผ๋กœ ์ž‘์„ฑํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? static์ด ์•„๋‹ˆ์–ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,29 @@ +package christmas.view; + +import camp.nextstep.edu.missionutils.Console; +import christmas.util.Parser; +import christmas.validator.MenuValidator; + +public class InputView { + + public static void printGreetingMessage() { + System.out.println(ViewMessage.GREETING.getMessage()); + } + + + public static int readDate() { + System.out.println(ViewMessage.DATE.getMessage()); + + String input = Console.readLine(); + return Parser.parseDate(input); + } + + + public static String readMenu() { + System.out.println(ViewMessage.MENU.getMessage()); + String input = Console.readLine(); + MenuValidator.validateMenuInputFormat(input); + return input; + } + +}
Java
static ๋ฉ”์†Œ๋“œ๋ฐ–์— ์—†๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ, ์ธ์Šคํ„ด์Šคํ™”๊ฐ€ ํ•„์š” ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ฑ์ž๋ฅผ private์œผ๋กœ ํ•˜์—ฌ ์ธ์Šคํ„ด์Šคํ™”๋ฅผ ๋ง‰์•„์ฃผ๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค https://rules.sonarsource.com/java/RSPEC-1118/
@@ -0,0 +1,44 @@ +package christmas.model.constant; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public enum DateConstant { + MIN_DATE(1), + MAX_DATE(31), + XMAS(25); + + public static final Set<Integer> WEEKDAYS = new HashSet<>(); + public static final Set<Integer> WEEKENDS = new HashSet<>(); + public static final Set<Integer> SPECIAL_DAYS = new HashSet<>(Arrays.asList( + 3, 10, 17, 24, 25, 31 + )); + + static { + LocalDate start = LocalDate.of(2023, 12, MIN_DATE.date); + LocalDate end = LocalDate.of(2023, 12, MAX_DATE.date); + + for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) { + DayOfWeek day = date.getDayOfWeek(); + if (day == DayOfWeek.SATURDAY || day == DayOfWeek.FRIDAY) { + WEEKENDS.add(date.getDayOfMonth()); + continue; + } + WEEKDAYS.add(date.getDayOfMonth()); + } + } + + private final int date; + + DateConstant(int date) { + this.date = date; + } + + public int getDate() { + return date; + } + +}
Java
```suggestion public static final Set<Integer> SPECIAL_DAYS = Set.of(3, 10, 17, 24, 25, 31); ``` `Set.of` ๋ฅผ ํ™œ์šฉํ•ด์„œ ๋” ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,44 @@ +package christmas.model.constant; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public enum DateConstant { + MIN_DATE(1), + MAX_DATE(31), + XMAS(25); + + public static final Set<Integer> WEEKDAYS = new HashSet<>(); + public static final Set<Integer> WEEKENDS = new HashSet<>(); + public static final Set<Integer> SPECIAL_DAYS = new HashSet<>(Arrays.asList( + 3, 10, 17, 24, 25, 31 + )); + + static { + LocalDate start = LocalDate.of(2023, 12, MIN_DATE.date); + LocalDate end = LocalDate.of(2023, 12, MAX_DATE.date); + + for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) { + DayOfWeek day = date.getDayOfWeek(); + if (day == DayOfWeek.SATURDAY || day == DayOfWeek.FRIDAY) { + WEEKENDS.add(date.getDayOfMonth()); + continue; + } + WEEKDAYS.add(date.getDayOfMonth()); + } + } + + private final int date; + + DateConstant(int date) { + this.date = date; + } + + public int getDate() { + return date; + } + +}
Java
SPECIAL_DAYS๊ฐ€ public์œผ๋กœ ์—ด๋ ค์žˆ์œผ๋ฉด์„œ ๋ถˆ๋ณ€์ด ์•„๋‹ˆ๋ฏ€๋กœ (HashMap) ์™ธ๋ถ€์—์„œ ๊ฐ’์„ ์ˆ˜์ •ํ•˜๋Š” ํ•ดํ‚น์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค! ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค ```java DateConstant.SPECIAL_DAYS.add(-1); ```
@@ -0,0 +1,40 @@ +package christmas.exception; + +public class CustomException extends IllegalArgumentException { + private static final String MENU_ERROR_MESSAGE = "[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String DATE_ERROR_MESSAGE = "[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + public CustomException(String s) { + super(s); + } + + @Override + public synchronized Throwable fillInStackTrace(){ + return this; + } + + public static final CustomException DATE_NOT_INTEGER_EXCEPTION = + new CustomException(DATE_ERROR_MESSAGE); + + public static final CustomException DATE_RANGE_EXCEPTION = + new CustomException(DATE_ERROR_MESSAGE); + + public static final CustomException DUPLICATE_MENU_EXCEPTION = + new CustomException(MENU_ERROR_MESSAGE); + + public static final CustomException MENU_COUNT_ZERO_EXCEPTION = + new CustomException(MENU_ERROR_MESSAGE); + + public static final CustomException MENU_FORMAT_EXCEPTION = + new CustomException(MENU_ERROR_MESSAGE); + + public static final CustomException MENU_NOT_FOUND_EXCEPTION = + new CustomException(MENU_ERROR_MESSAGE); + + public static final CustomException ONLY_DRINK_EXCEPTION = + new CustomException(MENU_ERROR_MESSAGE); + + public static final CustomException MENU_AMOUNT_OVER_LIMIT_EXCEPTION = + new CustomException(MENU_ERROR_MESSAGE); + + +}
Java
์ปค์Šคํ…€ ์ต์…‰์…˜์ด๋ผ๋Š” ์ด๋ฆ„๋ณด๋‹ค, ์กฐ๊ธˆ ๋” ์˜ˆ์™ธ์˜ ์ด์œ  ๋“ฑ์˜ ์˜๋ฏธ๊ฐ€ ๋“œ๋Ÿฌ๋‚˜๋Š” ์ด๋ฆ„์ด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,56 @@ +package christmas.model.constant; + +import java.util.Arrays; + +public enum Menu { + SOUP("์• ํ”ผํƒ€์ด์ €", "์–‘์†ก์ด์ˆ˜ํ”„", 6000), + TAPAS("์• ํ”ผํƒ€์ด์ €", "ํƒ€ํŒŒ์Šค", 5500), + SALAD("์• ํ”ผํƒ€์ด์ €", "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + STEAK("๋ฉ”์ธ", "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000), + BBQ("๋ฉ”์ธ", "๋ฐ”๋น„ํ๋ฆฝ", 54000), + SEAFOOD_PASTA("๋ฉ”์ธ", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000), + XMAS_PASTA("๋ฉ”์ธ", "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + CHOCO_CAKE("๋””์ €ํŠธ", "์ดˆ์ฝ”์ผ€์ดํฌ", 15000), + ICECREAM("๋””์ €ํŠธ", "์•„์ด์Šคํฌ๋ฆผ", 5000), + ZEROCOKE("์Œ๋ฃŒ", "์ œ๋กœ์ฝœ๋ผ", 3000), + REDWINE("์Œ๋ฃŒ", "๋ ˆ๋“œ์™€์ธ", 60000), + CHAMPAGNE("์Œ๋ฃŒ", "์ƒดํŽ˜์ธ", 25000); + + + private final String category; + private final String name; + private final int price; + + Menu(String category, String name, int price) { + this.category = category; + this.name = name; + this.price = price; + } + + public static Menu of(String name) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(name)) + .findFirst() + .orElse(null); + } + + public boolean isMainCategory() { + return "๋ฉ”์ธ".equals(this.category); + } + + public boolean isDessertCategory() { + return "๋””์ €ํŠธ".equals(this.category); + } + + public String getCategory() { + return category; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } +}
Java
String ๋ฆฌํ„ฐ๋Ÿด๋กœ "๋ฉ”์ธ"๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ฉ”๋‰ด์˜ ์นดํ…Œ๊ณ ๋ฆฌ๋„ enum์œผ๋กœ ๊ด€๋ฆฌํ•˜๋ฉด ์กฐ๊ธˆ ๋” ์šฉ์ดํ•˜์ง€ ์•Š์„๊นŒ์š”?
@@ -0,0 +1,52 @@ +package christmas.model; + +import static christmas.exception.CustomException.DATE_RANGE_EXCEPTION; +import static christmas.model.constant.DateConstant.SPECIAL_DAYS; +import static christmas.model.constant.DateConstant.WEEKDAYS; +import static christmas.model.constant.DateConstant.WEEKENDS; +import static christmas.model.constant.DateConstant.XMAS; + +import christmas.model.constant.DateConstant; + +public class Date { + private final int MIN_DATE = DateConstant.MIN_DATE.getDate(); + private final int MAX_DATE = DateConstant.MAX_DATE.getDate(); + private final int date; + + private Date(int date) { + validateDateRange(date); + this.date = date; + } + + public static Date of(int date) { + return new Date(date); + } + + public int getValue() { + return date; + } + + private void validateDateRange(int date) { + if (date < MIN_DATE || date > MAX_DATE) { + throw DATE_RANGE_EXCEPTION; + } + } + + public boolean isWeekday() { + return WEEKDAYS.contains(date); + } + + public boolean isWeekend() { + return WEEKENDS.contains(date); + } + + public boolean isBeforeXmas() { + return XMAS.getDate() >= date; + } + + public boolean isSpecialDay() { + return SPECIAL_DAYS.contains(date); + } + + +}
Java
์ž๋ฐ”์˜ ๋นŒํŠธ์ธ ๊ฐ์ฒด์ธ [Date](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html)์™€ ํ˜ผ๋™์˜ ์—ฌ์ง€๊ฐ€ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค..! (์ผ๋‹จ ์ „ ํ—ท๊ฐˆ๋ ธ์–ด์š” ๐Ÿ˜ญ)
@@ -0,0 +1,22 @@ +package christmas.model.constant; + +public enum PromotionConstant { + + CHRISTMAS_DISCOUNT("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"), + WEEKDAY_DISCOUNT("ํ‰์ผ ํ• ์ธ"), + WEEKEND_DISCOUNT("์ฃผ๋ง ํ• ์ธ"), + SPECIAL_DISCOUNT("ํŠน๋ณ„ ํ• ์ธ"), + GIFT_EVENT("์ฆ์ • ์ด๋ฒคํŠธ"); + + private String name; + + PromotionConstant(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + +}
Java
private final๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,75 @@ +package christmas.model.strategy; + +import static christmas.model.constant.DiscountConstant.CHRISTMAS_START_DISCOUNT; +import static christmas.model.constant.DiscountConstant.DISCOUNT_INCREMENT; +import static christmas.model.constant.DiscountConstant.FIXED_DISCOUNT; +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; +import static christmas.model.constant.DiscountConstant.NO_DISCOUNT; +import static christmas.model.constant.DiscountConstant.SPECIAL_DAY_DISCOUNT; + +import christmas.model.Date; +import christmas.model.constant.Menu; +import christmas.model.MenuCount; + +public class WootecoDiscountStrategy implements DiscountStrategy { + + @Override + public int christmasDiscount(Date date) { + if (date.isBeforeXmas()) { + return (CHRISTMAS_START_DISCOUNT.getAmount() + (date.getValue() - 1) + * DISCOUNT_INCREMENT.getAmount()); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int weekendDiscount(MenuCount menuCount, Date date) { + if (date.isWeekend()) { + return calculateWeekendDiscountAmount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int weekdayDiscount(MenuCount menuCount, Date date) { + if (date.isWeekday()) { + return calculateWeekdayDiscountAmount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int specialDayDiscount(Date date) { + if (date.isSpecialDay()) { + return SPECIAL_DAY_DISCOUNT.getAmount(); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int giftEventDiscount(MenuCount menuCount) { + if (menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount()) { + return Menu.CHAMPAGNE.getPrice(); + } + return NO_DISCOUNT.getAmount(); + } + + + private int calculateWeekendDiscountAmount(MenuCount menuCount) { + return menuCount.getValue() + .entrySet() + .stream() + .filter(entry -> entry.getKey().isMainCategory()) + .mapToInt(entry -> FIXED_DISCOUNT.getAmount() * entry.getValue()) + .sum(); + } + + private int calculateWeekdayDiscountAmount(MenuCount menuCount) { + return menuCount.getValue() + .entrySet() + .stream() + .filter(entry -> entry.getKey().isDessertCategory()) + .mapToInt(entry -> FIXED_DISCOUNT.getAmount() * entry.getValue()) + .sum(); + } +}
Java
์—ฌ๋Ÿฌ ํ• ์ธ ์ „๋žต์„ ํ•œ ํด๋ž˜์Šค์— ๋ชฐ์•„๋„ฃ๊ธฐ๋ณด๋‹ค๋Š” ๊ฐ๊ฐ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š” + ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋ ‡๊ฒŒ ๋‚˜๋ˆ„์–ด์„œ, ์šฐํ…Œ์ฝ” ํ• ์ธ ์ „๋žต์ด๋ผ๋Š” ์ด๋ฆ„๋ณด๋‹ค๋Š” ๋ฌด์Šจ ํ• ์ธ ์ „๋žต์ธ์ง€์— ๋Œ€ํ•œ ์˜๋ฏธ๊ฐ€ ๋“œ๋Ÿฌ๋‚˜๋Š” ์ด๋ฆ„์ด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,75 @@ +package christmas.model.strategy; + +import static christmas.model.constant.DiscountConstant.CHRISTMAS_START_DISCOUNT; +import static christmas.model.constant.DiscountConstant.DISCOUNT_INCREMENT; +import static christmas.model.constant.DiscountConstant.FIXED_DISCOUNT; +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; +import static christmas.model.constant.DiscountConstant.NO_DISCOUNT; +import static christmas.model.constant.DiscountConstant.SPECIAL_DAY_DISCOUNT; + +import christmas.model.Date; +import christmas.model.constant.Menu; +import christmas.model.MenuCount; + +public class WootecoDiscountStrategy implements DiscountStrategy { + + @Override + public int christmasDiscount(Date date) { + if (date.isBeforeXmas()) { + return (CHRISTMAS_START_DISCOUNT.getAmount() + (date.getValue() - 1) + * DISCOUNT_INCREMENT.getAmount()); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int weekendDiscount(MenuCount menuCount, Date date) { + if (date.isWeekend()) { + return calculateWeekendDiscountAmount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int weekdayDiscount(MenuCount menuCount, Date date) { + if (date.isWeekday()) { + return calculateWeekdayDiscountAmount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int specialDayDiscount(Date date) { + if (date.isSpecialDay()) { + return SPECIAL_DAY_DISCOUNT.getAmount(); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int giftEventDiscount(MenuCount menuCount) { + if (menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount()) { + return Menu.CHAMPAGNE.getPrice(); + } + return NO_DISCOUNT.getAmount(); + } + + + private int calculateWeekendDiscountAmount(MenuCount menuCount) { + return menuCount.getValue() + .entrySet() + .stream() + .filter(entry -> entry.getKey().isMainCategory()) + .mapToInt(entry -> FIXED_DISCOUNT.getAmount() * entry.getValue()) + .sum(); + } + + private int calculateWeekdayDiscountAmount(MenuCount menuCount) { + return menuCount.getValue() + .entrySet() + .stream() + .filter(entry -> entry.getKey().isDessertCategory()) + .mapToInt(entry -> FIXED_DISCOUNT.getAmount() * entry.getValue()) + .sum(); + } +}
Java
ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ• ์ธ ์ „๋žต์—์„œ ๋ณ€๋™์ด ์ƒ๊ธฐ๊ฒŒ ๋œ๋‹ค๋ฉด (์˜ˆ: ์‹œ์ž‘ํ• ์ธ์•ก์ด 1000์›์—์„œ 2000์›์œผ๋กœ ๋ณ€๊ฒฝ), ํ•ด๋‹น ๋ณ€๊ฒฝ์‚ฌํ•ญ์— ๋Œ€ํ•œ ์—ฌํŒŒ๊ฐ€ ํ• ์ธ ์ „๋žต๋งŒ ์—ฌํŒŒ๋ฅผ ๋ฏธ์ณ์•ผ ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ enum์ธ DiscountConstant์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๊ฒŒ ๋ผ์š”. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฃผ๋ง ํ• ์ธ ์ „๋žต์—๋„ ๋ณ€๋™์ด ์ƒ๊ธด๋‹ค๋ฉด DiscountConstant์ด ๋ฐ”๋€Œ์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ด๋Š” `ํด๋ž˜์Šค๊ฐ€ ๋ณ€๊ฒฝ๋  ์ด์œ ๋Š” ํ•œ ๊ฐ€์ง€์—ฌ์•ผ ํ•œ๋‹ค`๋ผ๋Š” SRP์›์น™์„ ์œ„๋ฐ˜ํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,75 @@ +package christmas.model.strategy; + +import static christmas.model.constant.DiscountConstant.CHRISTMAS_START_DISCOUNT; +import static christmas.model.constant.DiscountConstant.DISCOUNT_INCREMENT; +import static christmas.model.constant.DiscountConstant.FIXED_DISCOUNT; +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; +import static christmas.model.constant.DiscountConstant.NO_DISCOUNT; +import static christmas.model.constant.DiscountConstant.SPECIAL_DAY_DISCOUNT; + +import christmas.model.Date; +import christmas.model.constant.Menu; +import christmas.model.MenuCount; + +public class WootecoDiscountStrategy implements DiscountStrategy { + + @Override + public int christmasDiscount(Date date) { + if (date.isBeforeXmas()) { + return (CHRISTMAS_START_DISCOUNT.getAmount() + (date.getValue() - 1) + * DISCOUNT_INCREMENT.getAmount()); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int weekendDiscount(MenuCount menuCount, Date date) { + if (date.isWeekend()) { + return calculateWeekendDiscountAmount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int weekdayDiscount(MenuCount menuCount, Date date) { + if (date.isWeekday()) { + return calculateWeekdayDiscountAmount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int specialDayDiscount(Date date) { + if (date.isSpecialDay()) { + return SPECIAL_DAY_DISCOUNT.getAmount(); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int giftEventDiscount(MenuCount menuCount) { + if (menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount()) { + return Menu.CHAMPAGNE.getPrice(); + } + return NO_DISCOUNT.getAmount(); + } + + + private int calculateWeekendDiscountAmount(MenuCount menuCount) { + return menuCount.getValue() + .entrySet() + .stream() + .filter(entry -> entry.getKey().isMainCategory()) + .mapToInt(entry -> FIXED_DISCOUNT.getAmount() * entry.getValue()) + .sum(); + } + + private int calculateWeekdayDiscountAmount(MenuCount menuCount) { + return menuCount.getValue() + .entrySet() + .stream() + .filter(entry -> entry.getKey().isDessertCategory()) + .mapToInt(entry -> FIXED_DISCOUNT.getAmount() * entry.getValue()) + .sum(); + } +}
Java
๊ทธ๋Ÿฐ ์ด์œ ์—์„œ, CHRISTMAS_START_DISCOUNT๋Š” enum์œผ๋กœ publicํ•˜๊ฒŒ ์•„๋ฌด ํด๋ž˜์Šค์—์„œ๋‚˜ ๊ณต๊ฐœ๋  ํ•„์š”๊ฐ€ ์—†์ด, ํ•ด๋‹น ํ• ์ธ ์ •์ฑ… ํด๋ž˜์Šค์—์„œ `private static final`๊ณผ ๊ฐ™์ด ๊ด€๋ฆฌํ•˜๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” (ํ• ์ธ ์ •์ฑ…์ด ๋ฐ”๋€Œ๋”๋ผ๋„ ํ• ์ธ ์ •์ฑ… ํด๋ž˜์Šค๋งŒ ์˜ํ–ฅ์„ ๋ฐ›๊ฒŒ ๋˜๊ฒ ์ฃ )
@@ -0,0 +1,70 @@ +package christmas.service; + +import static christmas.model.constant.DiscountConstant.MIN_DISCOUNT_SERVICE; +import static christmas.model.constant.DiscountConstant.NO_DISCOUNT; +import static christmas.model.constant.PromotionConstant.CHRISTMAS_DISCOUNT; +import static christmas.model.constant.PromotionConstant.GIFT_EVENT; +import static christmas.model.constant.PromotionConstant.SPECIAL_DISCOUNT; +import static christmas.model.constant.PromotionConstant.WEEKDAY_DISCOUNT; +import static christmas.model.constant.PromotionConstant.WEEKEND_DISCOUNT; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.EventBadge; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import java.util.EnumMap; + +public class PromotionService { + private final DiscountStrategy discountStrategy; + private final BadgeStrategy badgeStrategy; + + + public PromotionService(DiscountStrategy discountStrategy, BadgeStrategy badgeStrategy) { + this.discountStrategy = discountStrategy; + this.badgeStrategy = badgeStrategy; + } + + public boolean canGetDiscount(MenuCount menuCount) { + return menuCount.calculateTotalAmount() >= MIN_DISCOUNT_SERVICE.getAmount(); + } + + public int calculatePromotionAmount(MenuCount menuCount, Date date) { + if (canGetDiscount(menuCount)) { + return calaulateTotalDiscountAmount(menuCount, date) + + discountStrategy.giftEventDiscount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + public int calaulateTotalDiscountAmount(MenuCount menuCount, Date date) { + if (canGetDiscount(menuCount)) { + return discountStrategy.christmasDiscount(date) + + discountStrategy.weekdayDiscount(menuCount, date) + + discountStrategy.specialDayDiscount(date) + + discountStrategy.weekendDiscount(menuCount, date); + } + return NO_DISCOUNT.getAmount(); + } + + public EnumMap<PromotionConstant, Integer> calculatePromotionStatus(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = new EnumMap<>(PromotionConstant.class); + if (canGetDiscount(menuCount)) { + promotionStatus.put(CHRISTMAS_DISCOUNT, discountStrategy.christmasDiscount(date)); + promotionStatus.put(WEEKDAY_DISCOUNT, discountStrategy.weekdayDiscount(menuCount, date)); + promotionStatus.put(WEEKEND_DISCOUNT, discountStrategy.weekendDiscount(menuCount, date)); + promotionStatus.put(SPECIAL_DISCOUNT, discountStrategy.specialDayDiscount(date)); + promotionStatus.put(GIFT_EVENT, discountStrategy.giftEventDiscount(menuCount)); + return promotionStatus; + } + return promotionStatus; + } + + public String calculateBadgeStatus(MenuCount menuCount, Date date) { + int promotionAmount = calculatePromotionAmount(menuCount, date); + EventBadge badge = badgeStrategy.calculateBadgeGrade(promotionAmount); + return badge.getName(); + } + +}
Java
early return์„ ์‚ฌ์šฉํ•˜๋ฉด ์กฐ๊ธˆ ๋” ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•ด์งˆ ๊ฒƒ ๊ฐ™์•„์š” ```java if(!canGetDiscount(menuCount) { return Collections.EMPTY_MAP; } EnumMap<PromotionConstant, Integer> promotionStatus = new EnumMap<>(PromotionConstant.class); promotionStatus.put(CHRISTMAS_DISCOUNT, discountStrategy.christmasDiscount(date)); promotionStatus.put(WEEKDAY_DISCOUNT, discountStrategy.weekdayDiscount(menuCount, date)); promotionStatus.put(WEEKEND_DISCOUNT, discountStrategy.weekendDiscount(menuCount, date)); promotionStatus.put(SPECIAL_DISCOUNT, discountStrategy.specialDayDiscount(date)); promotionStatus.put(GIFT_EVENT, discountStrategy.giftEventDiscount(menuCount)); return promotionStatus; ``` ์ด๋ ‡๊ฒŒ ๋ฆฌํŒฉํ† ๋ง์„ ํ–ˆ๋”๋‹ˆ enummap์„ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ private ๋ฉ”์†Œ๋“œ๋กœ ์ถ”์ถœํ•ด์•ผ๊ฒ ๋‹ค๋Š” ๋А๋‚Œ๋„ ์˜ค๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค (if๋ฌธ์œผ๋กœ ํ• ์ธ ํ™•์ธํ•˜๋Š” ๋กœ์ง, map ๋งŒ๋“œ๋Š” ๋กœ์ง๊นŒ์ง€ ์ด 2๊ฐœ์˜ ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ์œผ๋‹ˆ๊น์š”)
@@ -0,0 +1,52 @@ +package christmas.model; + +import static christmas.exception.CustomException.DATE_RANGE_EXCEPTION; +import static christmas.model.constant.DateConstant.SPECIAL_DAYS; +import static christmas.model.constant.DateConstant.WEEKDAYS; +import static christmas.model.constant.DateConstant.WEEKENDS; +import static christmas.model.constant.DateConstant.XMAS; + +import christmas.model.constant.DateConstant; + +public class Date { + private final int MIN_DATE = DateConstant.MIN_DATE.getDate(); + private final int MAX_DATE = DateConstant.MAX_DATE.getDate(); + private final int date; + + private Date(int date) { + validateDateRange(date); + this.date = date; + } + + public static Date of(int date) { + return new Date(date); + } + + public int getValue() { + return date; + } + + private void validateDateRange(int date) { + if (date < MIN_DATE || date > MAX_DATE) { + throw DATE_RANGE_EXCEPTION; + } + } + + public boolean isWeekday() { + return WEEKDAYS.contains(date); + } + + public boolean isWeekend() { + return WEEKENDS.contains(date); + } + + public boolean isBeforeXmas() { + return XMAS.getDate() >= date; + } + + public boolean isSpecialDay() { + return SPECIAL_DAYS.contains(date); + } + + +}
Java
์ฃผ๋ง์ธ์ง€ ํ‰์ผ์ธ์ง€๋ฅผ ์ฒดํฌํ•˜๋Š” ๋กœ์ง์„ `DateConstant`์— ๋ชฐ๊ธฐ๋ณด๋‹ค, Date ํด๋ž˜์Šค์—์„œ ์ฒ˜๋ฆฌํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”? ์ƒ์ˆ˜ ํด๋ž˜์Šค์— ๋กœ์ง์ด ๋“ค์–ด์žˆ๋Š” ๊ฒƒ์€ ์ด์ƒํ•œ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด์š”
@@ -0,0 +1,44 @@ +package christmas.model.constant; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public enum DateConstant { + MIN_DATE(1), + MAX_DATE(31), + XMAS(25); + + public static final Set<Integer> WEEKDAYS = new HashSet<>(); + public static final Set<Integer> WEEKENDS = new HashSet<>(); + public static final Set<Integer> SPECIAL_DAYS = new HashSet<>(Arrays.asList( + 3, 10, 17, 24, 25, 31 + )); + + static { + LocalDate start = LocalDate.of(2023, 12, MIN_DATE.date); + LocalDate end = LocalDate.of(2023, 12, MAX_DATE.date); + + for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) { + DayOfWeek day = date.getDayOfWeek(); + if (day == DayOfWeek.SATURDAY || day == DayOfWeek.FRIDAY) { + WEEKENDS.add(date.getDayOfMonth()); + continue; + } + WEEKDAYS.add(date.getDayOfMonth()); + } + } + + private final int date; + + DateConstant(int date) { + this.date = date; + } + + public int getDate() { + return date; + } + +}
Java
๋ฆฌํ„ฐ๋Ÿด ๊ฐ’๋„ ์ปจ๋ฒค์…˜์— ๋งž๊ฒŒ ์ƒ์ˆ˜ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,56 @@ +package christmas.model.constant; + +import java.util.Arrays; + +public enum Menu { + SOUP("์• ํ”ผํƒ€์ด์ €", "์–‘์†ก์ด์ˆ˜ํ”„", 6000), + TAPAS("์• ํ”ผํƒ€์ด์ €", "ํƒ€ํŒŒ์Šค", 5500), + SALAD("์• ํ”ผํƒ€์ด์ €", "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + STEAK("๋ฉ”์ธ", "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000), + BBQ("๋ฉ”์ธ", "๋ฐ”๋น„ํ๋ฆฝ", 54000), + SEAFOOD_PASTA("๋ฉ”์ธ", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000), + XMAS_PASTA("๋ฉ”์ธ", "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + CHOCO_CAKE("๋””์ €ํŠธ", "์ดˆ์ฝ”์ผ€์ดํฌ", 15000), + ICECREAM("๋””์ €ํŠธ", "์•„์ด์Šคํฌ๋ฆผ", 5000), + ZEROCOKE("์Œ๋ฃŒ", "์ œ๋กœ์ฝœ๋ผ", 3000), + REDWINE("์Œ๋ฃŒ", "๋ ˆ๋“œ์™€์ธ", 60000), + CHAMPAGNE("์Œ๋ฃŒ", "์ƒดํŽ˜์ธ", 25000); + + + private final String category; + private final String name; + private final int price; + + Menu(String category, String name, int price) { + this.category = category; + this.name = name; + this.price = price; + } + + public static Menu of(String name) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(name)) + .findFirst() + .orElse(null); + } + + public boolean isMainCategory() { + return "๋ฉ”์ธ".equals(this.category); + } + + public boolean isDessertCategory() { + return "๋””์ €ํŠธ".equals(this.category); + } + + public String getCategory() { + return category; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } +}
Java
์Œ์‹์˜ ์ข…๋ฅ˜๋„ Enum์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ํŠน์ • ๋ฉ”๋‰ด ์ข…๋ฅ˜์— ํ•ด๋‹น๋˜๋Š” ์Œ์‹์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋กœ์ง์„ ์งค ๋•Œ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”:)
@@ -0,0 +1,52 @@ +package christmas.model; + +import static christmas.exception.CustomException.DATE_RANGE_EXCEPTION; +import static christmas.model.constant.DateConstant.SPECIAL_DAYS; +import static christmas.model.constant.DateConstant.WEEKDAYS; +import static christmas.model.constant.DateConstant.WEEKENDS; +import static christmas.model.constant.DateConstant.XMAS; + +import christmas.model.constant.DateConstant; + +public class Date { + private final int MIN_DATE = DateConstant.MIN_DATE.getDate(); + private final int MAX_DATE = DateConstant.MAX_DATE.getDate(); + private final int date; + + private Date(int date) { + validateDateRange(date); + this.date = date; + } + + public static Date of(int date) { + return new Date(date); + } + + public int getValue() { + return date; + } + + private void validateDateRange(int date) { + if (date < MIN_DATE || date > MAX_DATE) { + throw DATE_RANGE_EXCEPTION; + } + } + + public boolean isWeekday() { + return WEEKDAYS.contains(date); + } + + public boolean isWeekend() { + return WEEKENDS.contains(date); + } + + public boolean isBeforeXmas() { + return XMAS.getDate() >= date; + } + + public boolean isSpecialDay() { + return SPECIAL_DAYS.contains(date); + } + + +}
Java
๋นŒํŠธ์ธ ๊ฐ์ฒด Date์™€ ํ˜ผ๋™๋  ์ˆ˜ ์žˆ์–ด์„œ ๋‹ค๋ฅธ ๋„ค์ด๋ฐ์„ ๋ฐ˜์˜ํ•˜๋Š”๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”