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์ ์ด๋ฒคํธ ๋ฐฐ์ง
+
+## ๐ฆ ํจํค์ง ๊ตฌ์กฐ
+
+
+
+## ๐บ๏ธ ํ๋ก์ฐ ์ฐจํธ
+
+```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
+
+
+
+---
+
+- ### controller
+
+
+
+---
+
+- ### constant
+
+
\ 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์ ํผ๋๋ ์ ์์ด์ ๋ค๋ฅธ ๋ค์ด๋ฐ์ ๋ฐ์ํ๋๊ฒ๋ ์ข์ ๊ฒ ๊ฐ์์ |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.