code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -1,7 +1,19 @@ package christmas; +import christmas.back.application.service.ClientService; +import christmas.back.application.service.MenuOrderService; +import christmas.front.controller.IOController; +import christmas.back.controller.PlannerController; +import christmas.front.view.InputView; +import christmas.front.view.OutputView; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + var clientService = new ClientService(); + var menuOrderService = new MenuOrderService(); + var ioController = new IOController(new InputView(),new OutputView()); + PlannerController plannerController = new PlannerController(ioController,clientService,menuOrderService); + plannerController.startPlanner(); + plannerController.showOrderResult(); } }
Java
Controller๊ฐ€ Controller๋ฅผ (๋˜๋Š” Service๊ฐ€ Service๋ฅผ) ์˜์กดํ•˜๋Š” ๋ฐฉ์‹์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ถŒ์žฅ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ํฐ ์ด์œ ๊ฐ€ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๋ฉด ์ˆœํ™˜ ์ฐธ์กฐ๊ฐ€ ์ผ์–ด๋‚  ์—ฌ์ง€๋ฅผ ๋‚จ๊ธฐ๊ฒŒ ๋˜๊ณ  ์ด๋ฅผ ์˜ˆ๋ฐฉํ•˜๋ ค๋ฉด ์„œ๋กœ ๊ฐ„์˜ ๊ณ„์ธต์„ ๋šœ๋ ทํ•˜๊ฒŒ ๋‚˜๋ˆ„์–ด์•ผ ํ•˜๋Š”๋ฐ ๋ฐฉ๋ฒ•์ด ๋งˆ๋•…์น˜ ์•Š๋‹ค๋Š” ์ ์— ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋Š”์ง€ ์˜๊ฒฌ ๋‚˜๋ˆ„์–ด๋ณด๊ณ  ์‹ถ์–ด์š”.
@@ -0,0 +1,47 @@ +package christmas.back.application.service; + +import christmas.back.domain.event.gift.GiftEvent; +import christmas.back.domain.user.ClientRepository; +import christmas.back.domain.user.model.Client; +import christmas.back.infrastructure.repository.ClientRepositoryImpl; + +public class ClientService { + private final ClientRepository clientRepository; + + public ClientService() { + this.clientRepository = new ClientRepositoryImpl(); + } + + public Client saveClient(Client clientInfo) { + Client client = new Client(clientInfo); + return clientRepository.save(client); + } + + public Client findClientById(Long id) { + return clientRepository.findById(id); + } + + public Integer getTotalAmountBeforeDiscount(Long clientId) { + return findClientById(clientId).getPayment().getTotalPaymentBeforeDiscount(); + } + + public String getGiftEventMenu(Long clientId) { + return GiftEvent.getGiftMenu(findClientById(clientId)); + } + + public Integer getTotalDiscountAmount(Long clientId) { + return findClientById(clientId).getPayment().getTotalDiscountMoney(); + } + + public Integer getAfterDiscount(Long clientId) { + return findClientById(clientId).getPayment().getAfterDiscount(); + } + + public String getBadgeContent(Long clientId) { + return findClientById(clientId).getBadgeContent(); + } + + public void updateBadge(Long clientId) { + findClientById(clientId).applyBadge(); + } +}
Java
JPA๋ฅผ ์‚ฌ์šฉํ•œ ์„œ๋น„์Šค ๊ณ„์ธต์„ ๋ณด๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์ธ์ƒ ๊นŠ์Šต๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ์˜ ์˜๋ฏธ๋„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ „๋‹ฌ๋˜๋„ค์š” ๐Ÿ‘
@@ -0,0 +1,21 @@ +package christmas.back.application.service; + +import christmas.back.domain.order.MenuOrders; +import christmas.back.domain.order.MenuOrdersRepository; +import christmas.back.infrastructure.repository.MenuOrdersRepositoryImpl; + +public class MenuOrderService { + private final MenuOrdersRepository menuOrdersRepository; + + public MenuOrderService() { + this.menuOrdersRepository = new MenuOrdersRepositoryImpl(); + } + + public MenuOrders saveOrders(MenuOrders menuOrders) { + return menuOrdersRepository.save(menuOrders); + } + + public MenuOrders findMenuOrdersById(Long menuOrdersId) { + return menuOrdersRepository.findById(menuOrdersId); + } +}
Java
Controller์™€ Service๋Š” main ๋ฉ”์„œ๋“œ์—์„œ ์ƒ์„ฑํ•˜์…จ๋Š”๋ฐ Repository๋งŒ ์„œ๋น„์Šค์—์„œ ์ƒ์„ฑํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,36 @@ +package christmas.back.application.usecase; + +import christmas.back.application.service.ClientService; +import christmas.back.application.service.MenuOrderService; +import christmas.back.domain.event.config.BaseEvent; +import christmas.back.domain.event.config.EventConfig; +import christmas.back.domain.event.config.EventType; +import christmas.back.domain.order.MenuOrders; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + + +public class CheckEventAndUpdateClientPayment { + private final MenuOrderService menuOrderService; + private final ClientService clientService; + private final List<BaseEvent> events; + + public CheckEventAndUpdateClientPayment(ClientService clientService,MenuOrderService menuOrderService) { + this.events = EventConfig.configEvent(); + this.menuOrderService = menuOrderService; + this.clientService = clientService; + } + public List<Map<EventType,Integer>> execute(Long clientId) { + var client = clientService.findClientById(clientId); + List<Map<EventType,Integer>> output = new ArrayList<>(); + MenuOrders menuOrders = menuOrderService.findMenuOrdersById(client.getMenuOrdersId()); + events.forEach(event ->{ + if(event.canGetEvent(client,menuOrders)) { + output.add(event.getEventBenefit(client,menuOrders)); + event.updateClientBenefit(client,menuOrders); + } + }); + return output; + } +}
Java
ํ‚ค-๊ฐ’์˜ ํ•œ ์Œ์”ฉ์„ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑํ•˜๊ณ ์ž ํ•˜์‹ ๊ฑฐ๋ผ๋ฉด ๋ฌผ๋ก  Map๋„ ๋ฌผ๋ก  ์ข‹์ง€๋งŒ Entry๋„ ์ข‹์€ ์„ ํƒ์ง€์ผ ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ €๋„ ์ผ๊ธ‰๊ฐ์ฒด๋งŒ์„ ์‚ฌ์šฉํ•ด์„œ ์—ฌ๋Ÿฌ ํ‚ค-๊ฐ’์„ ์–ด๋–ป๊ฒŒ ๋ฐ˜ํ™˜ํ•ด์ค„ ์ง€ ๊ณ ๋ฏผํ–ˆ์—ˆ๋Š”๋ฐ List์— Map์„ ํ†ต์งธ๋กœ ๋„ฃ๋Š” ๊ฒƒ์€ ๋‚ญ๋น„๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค๋”๋ผ๊ตฌ์š”
@@ -0,0 +1,21 @@ +package christmas.back.application.service; + +import christmas.back.domain.order.MenuOrders; +import christmas.back.domain.order.MenuOrdersRepository; +import christmas.back.infrastructure.repository.MenuOrdersRepositoryImpl; + +public class MenuOrderService { + private final MenuOrdersRepository menuOrdersRepository; + + public MenuOrderService() { + this.menuOrdersRepository = new MenuOrdersRepositoryImpl(); + } + + public MenuOrders saveOrders(MenuOrders menuOrders) { + return menuOrdersRepository.save(menuOrders); + } + + public MenuOrders findMenuOrdersById(Long menuOrdersId) { + return menuOrdersRepository.findById(menuOrdersId); + } +}
Java
์˜์กด์„ฑ์„ ์ƒ๊ฐํ•˜์—ฌ ๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜ ๊ตฌํ˜„์ฒด๋Š” ์„œ๋น„์Šค์—์„œ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. (+ ํ† ์Šค ๊ฐœ๋ฐœ ์˜์ƒ ์ค‘ ๊ณ„์ธต๊ฐ„์˜ ์ด๋™์„ ๋›ฐ์–ด ๋„˜์ง€ ์•Š๋Š”๋‹ค๋ผ๋Š” ๋ฃฐ์ด ์ƒ๊ฐ๋‚ฌ์Šต๋‹ˆ๋‹ค) ๊ทธ๋ž˜๋„ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ๋งŒ๋“ค์–ด์„œ DI ๋กœ ๊ตฌํ˜„ํ–ˆ์œผ๋ฉด ๋” ์™„๋ฒฝํ–ˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -1,7 +1,19 @@ package christmas; +import christmas.back.application.service.ClientService; +import christmas.back.application.service.MenuOrderService; +import christmas.front.controller.IOController; +import christmas.back.controller.PlannerController; +import christmas.front.view.InputView; +import christmas.front.view.OutputView; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + var clientService = new ClientService(); + var menuOrderService = new MenuOrderService(); + var ioController = new IOController(new InputView(),new OutputView()); + PlannerController plannerController = new PlannerController(ioController,clientService,menuOrderService); + plannerController.startPlanner(); + plannerController.showOrderResult(); } }
Java
(์ผ๋‹จ ์งˆ๋ฌธ์„ ๋ฐ›์•„ ๋“œ๋Š” ์ƒ๊ฐ์„ ์ ์—ˆ์ง€๋งŒ ๋‚ด์ผ๊นŒ์ง€ ๋‹ค์‹œํ•œ๋ฒˆ ์ƒ๊ฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹คใ…Žใ…Ž) Front controller ์˜ ์—ญํ• ์„ ํ•˜๋Š” ๋””์ŠคํŒจ์ฒ˜ ์„œ๋ธ”๋ฆฟ์ด ์žˆ๋‹ค๋ผ๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•˜์—ฌ io ์ปจํŠธ๋กค๋Ÿฌ์˜ ๊ฒฝ์šฐ Front ์˜ ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐ์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ฉ”์ธ์ด ๋  ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ์ง์„ ๋‹ด๋‹นํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ์ธ PlannerController ์—์„œ ๋„๋ฉ”์ธ ๋ชจ๋ธ์„ ๋‹ค๋ฃจ์ง€ ์•Š๋Š” IO ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•ด๋„ ์ˆœํ™˜์€ ์•ˆํ• ๊ฒƒ ๊ฐ™๋‹ค๋ผ๊ณ  ์ƒ๊ฐ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.(ํด๋ผ์Šค ์ด๋ฆ„์„ ๋‹ค์‹œํ•œ๋ฒˆ ๊ณ ๋ฏผํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค !!) ์„œ๋น„์Šค์˜ ๊ฒฝ์šฐ ๊ด€๋ฆฌํ•˜๋Š” ๋„๋ฉ”์ธ์˜ ์˜์—ญ์ด ์ปค์ง€๋ฉด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•˜์—ฌ ์„œ๋กœ ์ฐธ์กฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ฐœ๋ฐœ์„ ํ• ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ˆœํ™˜์ฐธ์กฐ์˜ ์˜ค๋ฅ˜๋ฅผ ๊ฐ€์งˆ์ˆ˜ ์žˆ์ง€๋งŒ , ํ•˜๋‚˜์˜ ์„œ๋น„์Šค์—์„œ ๋‹ค๋ฅธ ๋„๋ฉ”์ธ๋“ค์˜ ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉด ๋„๋ฉ”์ธ๊ฐ„์˜ ๋ถ„๋ฆฌ๊ฐ€ ๋” ์–ด๋ ค์šธ ๊ฒƒ ๊ฐ™๋‹ค๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๊ณ„์ธต๊ฐ„์˜ ๋ถ„๋ฆฌ ์ฆ‰ ์ปจํŠธ๋กค๋Ÿฌ , ์„œ๋น„์Šค , ๋„๋ฉ”์ธ ๋ชจ๋ธ , ๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜ ๊ฒฝ๊ณ„๋ฅผ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์€ ๋™์˜ํ•˜๋Š” ์ž…์žฅ์ž…๋‹ˆ๋‹ค. ๋„๋ฉ”์ธ๊ฐ„์˜ ์„œ๋น„์Šค ๊ณ„์ธต์€ ๊ฐ™์€ ๊ณ„์ธต์œผ๋กœ ์ดํ•ด๋ฅผ ํ•˜์—ฌ ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š” ์ƒํ™ฉ์€ ๊ดœ์ฐฎ๋‹ค๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ restapi ๋ฅผ ์ง€์›ํ•˜๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฒฝ์šฐ ์–ธ๊ธ‰ํ•ด ์ฃผ์‹  ๊ฒƒ์ฒ˜๋Ÿผ ์„œ๋กœ ์ปจํŠธ๋กค๋Ÿฌ ๊ฐ„์— ์˜์กดํ•˜์ง€ ์•Š๊ณ  ๊ฐœ๋ฐœ์„ ํ• ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,47 @@ +package christmas.back.application.service; + +import christmas.back.domain.event.gift.GiftEvent; +import christmas.back.domain.user.ClientRepository; +import christmas.back.domain.user.model.Client; +import christmas.back.infrastructure.repository.ClientRepositoryImpl; + +public class ClientService { + private final ClientRepository clientRepository; + + public ClientService() { + this.clientRepository = new ClientRepositoryImpl(); + } + + public Client saveClient(Client clientInfo) { + Client client = new Client(clientInfo); + return clientRepository.save(client); + } + + public Client findClientById(Long id) { + return clientRepository.findById(id); + } + + public Integer getTotalAmountBeforeDiscount(Long clientId) { + return findClientById(clientId).getPayment().getTotalPaymentBeforeDiscount(); + } + + public String getGiftEventMenu(Long clientId) { + return GiftEvent.getGiftMenu(findClientById(clientId)); + } + + public Integer getTotalDiscountAmount(Long clientId) { + return findClientById(clientId).getPayment().getTotalDiscountMoney(); + } + + public Integer getAfterDiscount(Long clientId) { + return findClientById(clientId).getPayment().getAfterDiscount(); + } + + public String getBadgeContent(Long clientId) { + return findClientById(clientId).getBadgeContent(); + } + + public void updateBadge(Long clientId) { + findClientById(clientId).applyBadge(); + } +}
Java
๊ธฐํšŒ๊ฐ€๋˜์‹œ๋ฉด ์œ ํˆฌ๋ธŒ ์šฐ์•„ํ•œํ…Œํฌ ์„ธ๋ฏธ๋‚˜์˜ ์˜์กด์„ฑ๊ณผ ๊ด€๋ จ๋œ ์˜์ƒ์„ ์ถ”์ฒœ ๋“œ๋ฆฌ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค . ์›Œ๋‚™ ์œ ๋ช…ํ•œ ์˜์ƒ์ด๋ผ ์ด๋ฏธ ๋ณด์…จ์„ ์ˆ˜ ์žˆ์ง€๋งŒ , ๋‹จ์ˆœํ•˜๊ฒŒ ์œ ์ง€๋ณด์ˆ˜์˜ ์ธก๋ฉด์˜ ๋„๋ฉ”์ธ์„ ๋ถ„๋ฆฌํ•˜๋Š” ์„ค๊ณ„์˜ ์ค‘์š”์„ฑ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ์—ฐ๊ด€์˜ ๋Œ€ํ•œ ์„ค๋ช…๋„ ํฌํ•จํ•˜๋Š” ๋‚ด์šฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค :)
@@ -0,0 +1,36 @@ +package christmas.back.application.usecase; + +import christmas.back.application.service.ClientService; +import christmas.back.application.service.MenuOrderService; +import christmas.back.domain.event.config.BaseEvent; +import christmas.back.domain.event.config.EventConfig; +import christmas.back.domain.event.config.EventType; +import christmas.back.domain.order.MenuOrders; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + + +public class CheckEventAndUpdateClientPayment { + private final MenuOrderService menuOrderService; + private final ClientService clientService; + private final List<BaseEvent> events; + + public CheckEventAndUpdateClientPayment(ClientService clientService,MenuOrderService menuOrderService) { + this.events = EventConfig.configEvent(); + this.menuOrderService = menuOrderService; + this.clientService = clientService; + } + public List<Map<EventType,Integer>> execute(Long clientId) { + var client = clientService.findClientById(clientId); + List<Map<EventType,Integer>> output = new ArrayList<>(); + MenuOrders menuOrders = menuOrderService.findMenuOrdersById(client.getMenuOrdersId()); + events.forEach(event ->{ + if(event.canGetEvent(client,menuOrders)) { + output.add(event.getEventBenefit(client,menuOrders)); + event.updateClientBenefit(client,menuOrders); + } + }); + return output; + } +}
Java
๋งˆ์ง€๋ง‰์˜ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๋Œ€ํ•˜์—ฌ ๊ณ ๋ฏผ์„ ํ–ˆ๋˜ ๋ถ€๋ถ„์ธ๋ฐ ํ”ผ๋“œ๋ฐฑ ์ •๋ง ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,24 @@ +import React, { useState } from 'react'; +import ClickMeButton from './components/ClickMeButton'; +import NumberButtons from './components/NumberButtons'; + +export default function App() { + const [counterNumber, setCounterNumber] = useState(0); + + function handlerClickButton({ number }) { + setCounterNumber(counterNumber + number); + } + + return ( + <div> + <p>Counter</p> + <ClickMeButton + counterNumber={counterNumber} + onClick={handlerClickButton} + number={1} + /> + <br /> + <NumberButtons onClick={handlerClickButton} /> + </div> + ); +}
Unknown
```javascript import ClickMeButton from './components/ClickMeButton'; import NumberButtons from './components/NumberButtons'; ``` ์ด๋ ‡๊ฒŒ ํ™•์žฅ์ž ์—†์ด ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”
@@ -0,0 +1,24 @@ +import React, { useState } from 'react'; +import ClickMeButton from './components/ClickMeButton'; +import NumberButtons from './components/NumberButtons'; + +export default function App() { + const [counterNumber, setCounterNumber] = useState(0); + + function handlerClickButton({ number }) { + setCounterNumber(counterNumber + number); + } + + return ( + <div> + <p>Counter</p> + <ClickMeButton + counterNumber={counterNumber} + onClick={handlerClickButton} + number={1} + /> + <br /> + <NumberButtons onClick={handlerClickButton} /> + </div> + ); +}
Unknown
handlerํ•จ์ˆ˜์˜ ์ด๋ฆ„๊ณผ ์ปดํฌ๋„ŒํŠธ์˜ ์†์„ฑ ์ด๋ฆ„์„ ๋˜‘๊ฐ™์ด ๋งž์ถฐ์ฃผ์‹  ๊ฒƒ ๊ฐ™์•„์š”. ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜์— `handle*`์ด๋ผ๊ณ  ์ด๋ฆ„์„ ๋ถ™์ด๋Š” ์ด์œ ๋Š” ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๊ทธ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ```jsx function handleClick() { alert('๋ฒ„ํŠผ์ด ํด๋ฆญ๋์–ด์š”!'); } <Button onClick={handleClick} /> ``` ์œ„์˜ ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด ๋ณด๋ฉด `Click ํ–ˆ์„ ๋•Œ handleClick์ด ์ฒ˜๋ฆฌํ•œ๋‹ค` ๋ผ๊ณ  ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌํ•  ๋•Œ๋Š” on์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,9 @@ +import React from 'react'; + +export default function ClickMeButton({ counterNumber, onClick, number }) { + return ( + <button type="button" onClick={() => onClick({ number })}> + Click me ({counterNumber}) + </button> + ); +}
Unknown
๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ 1์”ฉ ์ฆ๊ฐ€ํ•ด์„œ `{ number: 1}`๋กœ ์ „๋‹ฌํ•˜๊ณ  ์žˆ๋„ค์š”. ๋งŒ์•ฝ์— 1์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ 2์”ฉ ์ฆ๊ฐ€ํ•˜๋„๋ก ๋ณ€๊ฒฝํ•œ๋‹ค๋ฉด ์ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด๋Ÿฌํ•œ ๋กœ์ง์„ ์™ธ๋ถ€์—์„œ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”
@@ -0,0 +1,24 @@ +import React, { useState } from 'react'; +import ClickMeButton from './components/ClickMeButton'; +import NumberButtons from './components/NumberButtons'; + +export default function App() { + const [counterNumber, setCounterNumber] = useState(0); + + function handlerClickButton({ number }) { + setCounterNumber(counterNumber + number); + } + + return ( + <div> + <p>Counter</p> + <ClickMeButton + counterNumber={counterNumber} + onClick={handlerClickButton} + number={1} + /> + <br /> + <NumberButtons onClick={handlerClickButton} /> + </div> + ); +}
Unknown
<img width="406" alt="แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-05-12 แ„‹แ…ฉแ„’แ…ฎ 7 00 47" src="https://github.com/CodeSoom/react-week2-assignment-1/assets/103479322/dc146c47-9a01-4296-8d1d-357ee875bf9f"> ํ™•์žฅ์ž๋ฅผ ์ง€์›Œ๋„ ๋˜‘๊ฐ™์ด ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,9 @@ +import React from 'react'; + +export default function ClickMeButton({ counterNumber, onClick, number }) { + return ( + <button type="button" onClick={() => onClick({ number })}> + Click me ({counterNumber}) + </button> + ); +}
Unknown
` <ClickMeButton counterNumber={counterNumber} onClick={handlerClickButton} whatNumberToAdd={1} />` ` return ( <button type="button" onClick={() => onClick({ number: whatNumberToAdd })}> Click me ({counterNumber}) </button> );` ์ด๋Ÿฐ์‹์œผ๋กœ ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑํ•  ๋•Œ whatNumberToAdd๋ผ๋Š” ๊ฐ’๋„ props๋กœ ๋„˜๊ฒจ์ฃผ๊ณ  ์‹ค์ œ clickํ•  ๋•Œ๋Š” ํ•ด๋‹น props์˜ ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋Š” ํ˜•ํƒœ๋กœ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค! ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋“ค์–ด๊ฐ€๋Š” ๋ณ€์ˆ˜๋Š” ํ•˜๋‚˜๋„ ์—†์ด ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’๋งŒ ๋ณด์—ฌ์ฃผ๊ณ , ๋ชจ๋‘ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•ธ๋“ค๋งํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ด€์‹ฌ์‚ฌ๊ฐ€ ๋ถ„๋ฆฌ๋œ ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค! ๐Ÿ™‚
@@ -0,0 +1,24 @@ +import React, { useState } from 'react'; +import ClickMeButton from './components/ClickMeButton'; +import NumberButtons from './components/NumberButtons'; + +export default function App() { + const [counterNumber, setCounterNumber] = useState(0); + + function handlerClickButton({ number }) { + setCounterNumber(counterNumber + number); + } + + return ( + <div> + <p>Counter</p> + <ClickMeButton + counterNumber={counterNumber} + onClick={handlerClickButton} + number={1} + /> + <br /> + <NumberButtons onClick={handlerClickButton} /> + </div> + ); +}
Unknown
ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜์— handle*์ด๋ผ๊ณ  ์ด๋ฆ„์„ ๋ถ™์ด๋Š” ์ด์œ ๋Š” ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๊ทธ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ผ๋Š” ๋งฅ๋ฝ์€ ์ดํ•ด๊ฐ€ ๋˜์—ˆ๊ณ , ๊ทธ๋ž˜์„œ on์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ์˜๋ฏธ๋„ ์ดํ•ด๋Š” ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ ๊ฒฝ์šฐ์—์„œ ์ž์‹์š”์†Œ์—์„œ handleClick์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์“ธ ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๊ฒƒ ๊ฐ™์€๋ฐ์š”! ``` <button onClick={()=>onClick()} ></button> ``` ์ด๋ ‡๊ฒŒ ๋  ๊ฒฝ์šฐ์—๋Š” onClick์ด๋ผ๋Š” ๊ฒƒ์„ onClick์œผ๋กœ ์ „๋‹ฌ๋ฐ›์€ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ธ๋ฐ ์ œ๊ฐ€ ๋ดค์„ ๋•Œ๋Š” onClick์„ ํ–ˆ์„ ๋•Œ onClickํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค๋Š”๊ฒŒ ์˜คํžˆ๋ ค ์–ด์ƒ‰ํ•œ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ๋„ ๋“ค์–ด์„œ์š”๐Ÿฅฒ ๊ทธ๋Ÿผ์—๋„ on์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ•์กฐํ•˜๋Š” ๋ชฉ์ ์ด ๋” ํฌ๋‹ˆ ์ด๋ ‡๊ฒŒ ์“ฐ๋Š”๊ฒŒ ๋” ๋‚˜์€ ๋ฐฉ์‹์ด๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ๋ ๊นŒ์š”?
@@ -0,0 +1,24 @@ +import React, { useState } from 'react'; +import ClickMeButton from './components/ClickMeButton'; +import NumberButtons from './components/NumberButtons'; + +export default function App() { + const [counterNumber, setCounterNumber] = useState(0); + + function handlerClickButton({ number }) { + setCounterNumber(counterNumber + number); + } + + return ( + <div> + <p>Counter</p> + <ClickMeButton + counterNumber={counterNumber} + onClick={handlerClickButton} + number={1} + /> + <br /> + <NumberButtons onClick={handlerClickButton} /> + </div> + ); +}
Unknown
https://baeharam.netlify.app/posts/lint/Lint-ESLint-+-Prettier-%EC%84%A4%EC%A0%95%ED%95%98%EA%B8%B0 ``` "rules": { "react/jsx-filename-extension": ["warn", { "extensions": [".js", ".jsx"] }] } ``` eslint-plugin-react ์„ค์น˜ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•˜๊ณ  ์œ„์˜ ๋ฃฐ์„ ์ถ”๊ฐ€๋กœ ๊ธฐ์ž…ํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐ๋œ๋‹ค๋Š” ์‚ฌ๋ก€๋„ ์žˆ์–ด์„œ ํ•ด๋ดค๋Š”๋ฐ ์ด ์—ญ์‹œ๋„ ์•ˆ๋˜๋„ค์š” ๐Ÿฅบ
@@ -0,0 +1,24 @@ +import React, { useState } from 'react'; +import ClickMeButton from './components/ClickMeButton'; +import NumberButtons from './components/NumberButtons'; + +export default function App() { + const [counterNumber, setCounterNumber] = useState(0); + + function handlerClickButton({ number }) { + setCounterNumber(counterNumber + number); + } + + return ( + <div> + <p>Counter</p> + <ClickMeButton + counterNumber={counterNumber} + onClick={handlerClickButton} + number={1} + /> + <br /> + <NumberButtons onClick={handlerClickButton} /> + </div> + ); +}
Unknown
ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์—์„œ onClick์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋‹ˆ ์–ด์ƒ‰ํ•˜๊ฒŒ ๋А๊ปด์งˆ ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”. ์‚ฌ์šฉํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์–ด์ƒ‰ํ•˜๊ฒŒ ๋А๊ปด์ง€์‹ ๋‹ค๋ฉด ์ด๋ฆ„์„ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธด ํ•˜๊ฒ ๋„ค์š”. ```javascript const ButtonComponent = ({ onClick: handleClick }) => { return ( <button onClick={handleClick}></button> ); }; ``` ์ €๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์“ฐ๋Š” ์ž…์žฅ์—์„œ ์ดํ•ดํ•˜๊ธฐ ์ข‹์€ ์ด๋ฆ„์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ์ƒ๊ฐ๋•Œ๋ฌธ์— ๊ทธ๋žฌ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž
@@ -0,0 +1,9 @@ +import React from 'react'; + +export default function ClickMeButton({ counterNumber, onClick, number }) { + return ( + <button type="button" onClick={() => onClick({ number })}> + Click me ({counterNumber}) + </button> + ); +}
Unknown
๋ณ„๋„์˜ ์†์„ฑ์„ ๋„ฃ์–ด์„œ ๊ฐ’์„ ๋ช‡์”ฉ ๋”ํ• ์ง€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š” ใ…Žใ…Ž ์ข‹์Šต๋‹ˆ๋‹ค. ์ €๋Š” ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜๋ฅผ ์™ธ๋ถ€์—์„œ ์ „๋‹ฌํ•˜๊ณ  ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ๋‹จ์ˆœํžˆ ์‹คํ–‰๋งŒ ํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•ด ๋ณด์•˜์–ด์š” ```javascript const handleClickMeButton = () => { setCounerNumber(counterNumber + 10); } return ( <ClickMeButton> counterNumber={counterNumber} onClick={handleClickMeButton}> </ClickMeButton> ) // export default function ClickMeButton({ counterNumber, onClick }) { return ( <button type="button" onClick={onClick}> Click me ( {counterNumber} ) </button> ); } ```
@@ -0,0 +1,24 @@ +import React, { useState } from 'react'; +import ClickMeButton from './components/ClickMeButton'; +import NumberButtons from './components/NumberButtons'; + +export default function App() { + const [counterNumber, setCounterNumber] = useState(0); + + function handlerClickButton({ number }) { + setCounterNumber(counterNumber + number); + } + + return ( + <div> + <p>Counter</p> + <ClickMeButton + counterNumber={counterNumber} + onClick={handlerClickButton} + number={1} + /> + <br /> + <NumberButtons onClick={handlerClickButton} /> + </div> + ); +}
Unknown
์•„ํ•˜! ์ œ์•ˆํ•ด์ฃผ์‹  ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ๊ฒƒ๊ฐ™์•„์š”! ์—ญ์‹œ ์ด๋ฆ„ ์ง“๋Š” ๊ฑด ์ฐธ ์–ด๋ ค์šด์ผ์ด๋„ค์š” ใ… ใ…  ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :) ใ…Žใ…Ž
@@ -0,0 +1,9 @@ +import React from 'react'; + +export default function ClickMeButton({ counterNumber, onClick, number }) { + return ( + <button type="button" onClick={() => onClick({ number })}> + Click me ({counterNumber}) + </button> + ); +}
Unknown
์•„ํ•˜! ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š” :) ์ € ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” handleClickButton์ด๋ผ๋Š” ํ•ธ๋“ค๋Ÿฌํ•จ์ˆ˜๋ฅผ clickmebutton ์ปดํฌ๋„ŒํŠธ์™€ numberbutton์— ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜๋กœ ๋‘๊ณ  ์‹ถ์—ˆ์–ด์š”! ์ด์œ ๋Š” ๋‘˜ ๋‹ค ํŠน์ • ๊ฐ’์„ ๋”ํ•ด์„œ ๋ฑ‰์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด ๊ฐ๊ฐ์˜ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ท! ๊ทธ๋ž˜์„œ ๊ฐ™์€ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜๋ฅผ ์“ฐ๊ณ , ๋”ํ•ด์ฃผ๋Š” ๊ฐ’๋งŒ ์‚ฌ์šฉ๋˜๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ์ข‹์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•ด์„œ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹คใ…Žใ…Ž ํ”ผ๋“œ๋ฐฑํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ด์š” :)
@@ -0,0 +1,24 @@ +import React, { useState } from 'react'; +import ClickMeButton from './components/ClickMeButton'; +import NumberButtons from './components/NumberButtons'; + +export default function App() { + const [counterNumber, setCounterNumber] = useState(0); + + function handlerClickButton({ number }) { + setCounterNumber(counterNumber + number); + } + + return ( + <div> + <p>Counter</p> + <ClickMeButton + counterNumber={counterNumber} + onClick={handlerClickButton} + number={1} + /> + <br /> + <NumberButtons onClick={handlerClickButton} /> + </div> + ); +}
Unknown
์†์„ฑ๋ช…์„ ์•„์ฃผ ์ž์„ธํžˆ ์ž‘์„ฑํ•ด ์ฃผ์…จ๋„ค์š”. ๊ทธ๋Ÿฌ๋‹ค๋ณด๋‹ˆ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์–ด๋–ค ์ผ์„ ํ•˜๋Š”์ง€๋ฅผ ๋ณ€์ˆ˜๋ช…์—๋„ ๋“œ๋Ÿฌ๋‚ฌ๋„ค์š”. ์ฃผ์–ด์ง„ ๊ฐ’์œผ๋กœ ๋”ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๋ฐ”๋กœ ์•Œ์•„์„œ ์ข‹์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ด๊ฑด ๋‹จ์ ์ด ๋ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ด ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๋งก๊ธฐ๋Š” ๊ฒƒ์€ ๋‹จ์ˆœํ•œ ๊ฐ’์ด๊ณ  ์ด ๊ฐ’์œผ๋กœ ๋ฌด์—‡์„ ํ• ์ง€๋Š” ์ด ์ปดํฌ๋„ŒํŠธ๋งŒ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒŒ ์บก์Аํ™”์ž…๋‹ˆ๋‹ค. ์กฐ๊ธˆ ์–ต์ง€์ผ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๋งŒ์•ฝ ์ฃผ์–ด์ง„ ๊ฐ’์„ ์ œ๊ณฑ์œผ๋กœ ๋”ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด ์†์„ฑ์˜ ์ด๋ฆ„๋„ ๋ฐ”๋€Œ์–ด์•ผ ํ• ๊ฑฐ์˜ˆ์š”.
@@ -0,0 +1,24 @@ +import React, { useState } from 'react'; +import ClickMeButton from './components/ClickMeButton'; +import NumberButtons from './components/NumberButtons'; + +export default function App() { + const [counterNumber, setCounterNumber] = useState(0); + + function handlerClickButton({ number }) { + setCounterNumber(counterNumber + number); + } + + return ( + <div> + <p>Counter</p> + <ClickMeButton + counterNumber={counterNumber} + onClick={handlerClickButton} + number={1} + /> + <br /> + <NumberButtons onClick={handlerClickButton} /> + </div> + ); +}
Unknown
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :) ๋•๋ถ„์— ์ถ”์ƒํ™”๋ฅผ ํ•ญ์ƒ ์œ ๋…ํ•˜๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ์งœ๊ฒŒ ๋˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”! ์†์„ฑ๋ช…์„ number๋กœ ๋ณ€๊ฒฝํ•ด์„œ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์—์„œ๋งŒ ๋ช…ํ™•ํ•œ ์—ญํ• ์„ ์•Œ๊ฒŒ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,58 @@ +package lotto.domain; + +import static lotto.global.Error.DUPLICATED; +import static lotto.global.Error.INVALID_SIZE; + +import java.util.Collections; +import java.util.List; + +public class Lotto { + private final List<Integer> numbers; + + public Lotto(List<Integer> numbers) { + validateSize(numbers); + validateDuplicated(numbers); + validateNumberRange(numbers); + this.numbers = numbers; + } + + private void validateSize(List<Integer> numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException(INVALID_SIZE.getMessage()); + } + } + + private void validateDuplicated(List<Integer> numbers) { + if (numbers.stream() + .distinct().count() != numbers.size()) { + throw new IllegalArgumentException(DUPLICATED.getMessage()); + } + } + + private void validateNumberRange(List<Integer> numbers) { + numbers.forEach(Number::new); + } + + // TODO: ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ + public WinningResult checkWinningResult(WinningLotto winningLotto) { + int winningCount = checkWinningCount(winningLotto); + boolean hasBonusNumber = hasBonusNumber(winningLotto); + return new WinningResult(winningCount, hasBonusNumber); + } + + private int checkWinningCount(WinningLotto winningLotto) { + return (int) numbers.stream() + .filter(number -> winningLotto + .getWinningNumbers() + .contains(number)) + .count(); + } + + private boolean hasBonusNumber(WinningLotto winningLotto) { + return numbers.contains(winningLotto.getBonusNumber()); + } + + public List<Integer> getNumbers() { + return Collections.unmodifiableList(numbers); + } +}
Java
๋งค์ง ๋„˜๋ฒ„ ๋”ฐ๋กœ ๊ด€๋ฆฌ ํ•ด์ฃผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,58 @@ +package lotto.domain; + +import static lotto.global.Error.DUPLICATED; +import static lotto.global.Error.INVALID_SIZE; + +import java.util.Collections; +import java.util.List; + +public class Lotto { + private final List<Integer> numbers; + + public Lotto(List<Integer> numbers) { + validateSize(numbers); + validateDuplicated(numbers); + validateNumberRange(numbers); + this.numbers = numbers; + } + + private void validateSize(List<Integer> numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException(INVALID_SIZE.getMessage()); + } + } + + private void validateDuplicated(List<Integer> numbers) { + if (numbers.stream() + .distinct().count() != numbers.size()) { + throw new IllegalArgumentException(DUPLICATED.getMessage()); + } + } + + private void validateNumberRange(List<Integer> numbers) { + numbers.forEach(Number::new); + } + + // TODO: ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ + public WinningResult checkWinningResult(WinningLotto winningLotto) { + int winningCount = checkWinningCount(winningLotto); + boolean hasBonusNumber = hasBonusNumber(winningLotto); + return new WinningResult(winningCount, hasBonusNumber); + } + + private int checkWinningCount(WinningLotto winningLotto) { + return (int) numbers.stream() + .filter(number -> winningLotto + .getWinningNumbers() + .contains(number)) + .count(); + } + + private boolean hasBonusNumber(WinningLotto winningLotto) { + return numbers.contains(winningLotto.getBonusNumber()); + } + + public List<Integer> getNumbers() { + return Collections.unmodifiableList(numbers); + } +}
Java
ํ•ด๋‹น ๋ถ€๋ถ„์„ Intstream์œผ๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์„๊นŒ์š”????
@@ -0,0 +1,30 @@ +package lotto.domain; + +import java.util.Collections; +import java.util.List; +import lotto.global.Error; + +public class WinningLotto { + private final List<Integer> winningNumbers; + private final int bonusNumber; + + public WinningLotto(List<Integer> winningNumbers, int bonusNumber) { + validateBonusNumber(winningNumbers, bonusNumber); + this.winningNumbers = winningNumbers; + this.bonusNumber = bonusNumber; + } + + private void validateBonusNumber(List<Integer> winningNumbers, int bonusNumber) { + if (winningNumbers.contains(bonusNumber)) { + throw new IllegalArgumentException(Error.INVALID_BONUS_NUMBER.getMessage()); + } + } + + public List<Integer> getWinningNumbers() { + return Collections.unmodifiableList(winningNumbers); + } + + public int getBonusNumber() { + return bonusNumber; + } +}
Java
๋‹น์ฒจ ๋ฒˆํ˜ธ ๋กœ๋˜๋ฅผ ๋”ฐ๋กœ ๋‘์…จ๋„ค์š”. ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฒƒ ๊ฐ™์•„์š”. ์ €๋Š” ๋˜‘๊ฐ™์€ ๋กœ๋˜๋ผ๊ณ  ๊ผญ ๊ฐ™์ด ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๊ณ ๋งŒ ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ. ์ด๋Ÿฌ๋ฉด ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ํ™•์ธ์„ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์„œ ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,38 @@ +package lotto.domain; + +import static lotto.domain.Rank.NONE; + +import java.util.Arrays; + +public class WinningResult { + private static final int SECOND_OR_THIRD = 5; + private final int winningCount; + private final boolean hasBonusNumber; + + public WinningResult(int winningCount, boolean matchingBonusNumber) { + this.winningCount = winningCount; + this.hasBonusNumber = matchingBonusNumber; + } + + public Rank toRank() { + if (winningCount == SECOND_OR_THIRD) { + return Arrays.stream(Rank.values()) + .filter(rank -> rank.getWinningCount() == winningCount) + .filter(rank -> rank.hasBonusNumber() == hasBonusNumber) + .findAny() + .orElse(NONE); + } + return Arrays.stream(Rank.values()) + .filter(rank -> rank.getWinningCount() == winningCount) + .findAny() + .orElse(NONE); + } + + public int getWinningCount() { + return winningCount; + } + + public boolean hasBonusNumber() { + return hasBonusNumber; + } +}
Java
enum์ด ์„ ์–ธ๋œ ์ˆœ์„œ๋Œ€๋กœ ์ธ๋ฑ์Šค๊ฐ€ ์ •ํ•ด์ง€๋Š” ๊ฑธ๋กœ ์•Œ๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•ด์„œ 2๋“ฑ, 3๋“ฑ์˜ ๋‹น์ฒจ ๋ฒˆํ˜ธ ๊ฐฏ์ˆ˜๋ฅผ filterํ•œ ํ›„ ๋‹ค์Œ filter๋กœ ๋ณด๋„ˆ์Šค ์—ฌ๋ถ€ ์ฒดํฌ ํ•˜๊ณ  ์ดํ›„ ๋‚˜๋จธ์ง€ ๋žญํฌ๋ฅผ ์ฒดํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์“ฐ๋ฉด ํ•ด๋‹น ๋ถ€๋ถ„์˜ ์ฝ”๋“œ๋ฅผ ์กฐ๊ธˆ ์ค„์ผ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”??? ์ƒ์ถ”๋‹˜์˜ ์ƒ๊ฐ์ด ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค^^
@@ -0,0 +1,26 @@ +package lotto.domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class LottoRepository { + private final List<Lotto> lottos = new ArrayList<>(); + + public void issueLottos(int purchaseQuantity) { + for (int i = 0; i < purchaseQuantity; i++) { + Lotto lotto = new Lotto(Number.generateNumbers()); + lottos.add(lotto); + } + } + + public List<WinningResult> checkWinningResults(WinningLotto winningLotto) { + return lottos.stream() + .map(lotto -> lotto.checkWinningResult(winningLotto)) + .collect(Collectors.toList()); + } + + public List<Lotto> getLottos() { + return lottos; + } +}
Java
์ „์—ญ๋ณ€์ˆ˜๋ฅผ ์ด๋ ‡๊ฒŒ new๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด๋ž‘ ์ƒ์„ฑ์ž๋กœ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋–„ this๋กœ ์ง€์ •ํ•ด์ค˜์„œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ์–ด๋–ค ๋ฐฉ์‹์ด ๋‚˜์€ ํ‘œํ˜„์ธ์ง€ ์˜๊ฒฌ์„ ๋‚˜๋ˆ ๋ณด๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,57 @@ +package lotto.view; + +import java.util.Arrays; +import java.util.Map; +import lotto.domain.Lotto; +import lotto.domain.LottoRepository; +import lotto.domain.Rank; +import lotto.domain.WinningStats; + +public class OutputView { + private static final String PURCHASE_QUANTITY_FORMAT = "%n%d๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.%n"; + private static final String WINNING_STATS = "\n๋‹น์ฒจ ํ†ต๊ณ„\n---"; + private static final String WINNING_STATS_FORMAT = "%d๊ฐœ ์ผ์น˜%s (%,d์›) - %d๊ฐœ%n"; + private static final String HAS_BONUS_NUMBER = ", ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"; + private static final String HAS_NOT_BONUS_NUMBER = ""; + private static final String TOTAL_PROFIT_RATE_FORMAT = "์ด ์ˆ˜์ต๋ฅ ์€ %.1f%%์ž…๋‹ˆ๋‹ค."; + + public static void printError(Exception e) { + System.out.println(e.getMessage()); + } + + public static void printPurchaseQuantity(int purchaseQuantity) { + System.out.printf(PURCHASE_QUANTITY_FORMAT, purchaseQuantity); + } + + public static void printLottos(LottoRepository lottoRepository) { + for (Lotto lotto : lottoRepository.getLottos()) { + System.out.println(lotto.getNumbers() + .stream() + .sorted() + .toList()); + } + } + + public static void printWinningStats(WinningStats winningStats) { + Map<Rank, Integer> rankCounts = winningStats.getWinningStats(); + System.out.println(WINNING_STATS); + Arrays.stream(Rank.values()) + .skip(1) + .forEach(rank -> System.out.printf(WINNING_STATS_FORMAT + , rank.getWinningCount() + , checkBonusNumber(rank) + , rank.getPrize() + , rankCounts.get(rank))); + } + + private static String checkBonusNumber(Rank rank) { + if (rank.hasBonusNumber()) { + return HAS_BONUS_NUMBER; + } + return HAS_NOT_BONUS_NUMBER; + } + + public static void printTotalProfitRate(double totalProfitRate) { + System.out.printf(TOTAL_PROFIT_RATE_FORMAT, totalProfitRate); + } +}
Java
stream์„ ๊ต‰์žฅํžˆ ์ž˜ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค.
@@ -0,0 +1,85 @@ +package lotto.controller; + +import java.util.List; +import lotto.domain.Lotto; +import lotto.domain.LottoRepository; +import lotto.domain.Number; +import lotto.domain.PurchaseAmount; +import lotto.domain.Rank; +import lotto.domain.WinningLotto; +import lotto.domain.WinningResult; +import lotto.domain.WinningStats; +import lotto.view.InputView; +import lotto.view.OutputView; + +public class Controller { + + public void run() { + PurchaseAmount purchaseAmount = createPurchaseAmount(); + int purchaseQuantity = createPurchaseQuantity(purchaseAmount); + LottoRepository lottoRepository = createLottoRepository(purchaseQuantity); + WinningLotto winningLotto = createWinningLotto(); + List<WinningResult> winningResults = lottoRepository.checkWinningResults(winningLotto); + WinningStats winningStats = createWinningStats(winningResults); + createTotalProfitRate(winningStats, purchaseAmount); + } + + private PurchaseAmount createPurchaseAmount() { + while (true) { + try { + return new PurchaseAmount(InputView.askPurchaseAmount()); + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private int createPurchaseQuantity(PurchaseAmount purchaseAmount) { + int purchaseQuantity = purchaseAmount.calculatePurchaseQuantity(); + OutputView.printPurchaseQuantity(purchaseQuantity); + return purchaseQuantity; + } + + private LottoRepository createLottoRepository(int purchaseQuantity) { + LottoRepository lottoRepository = new LottoRepository(); + lottoRepository.issueLottos(purchaseQuantity); + OutputView.printLottos(lottoRepository); + return lottoRepository; + } + + private List<Integer> createWinningNumbers() { + while (true) { + try { + List<Integer> winningNumbers = InputView.askWinningNumbers(); + new Lotto(winningNumbers); + return winningNumbers; + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private WinningLotto createWinningLotto() { + List<Integer> winningNumbers = createWinningNumbers(); + while (true) { + try { + int bonusNumber = InputView.askBonusNumber(); + new Number(bonusNumber); + return new WinningLotto(winningNumbers, bonusNumber); + } catch (IllegalArgumentException e) { + OutputView.printError(e); + } + } + } + + private WinningStats createWinningStats(List<WinningResult> winningResults) { + WinningStats winningStats = Rank.countRanks(winningResults); + OutputView.printWinningStats(winningStats); + return winningStats; + } + + private void createTotalProfitRate(WinningStats winningStats, PurchaseAmount purchaseAmount) { + double totalProfitRate = winningStats.calculateTotalProfitRate(purchaseAmount); + OutputView.printTotalProfitRate(totalProfitRate); + } +}
Java
์—ฌ๊ธฐ์„œ ์ƒ์„ฑ๋œ Lotto ๊ฐ์ฒด๋Š” ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ณ  List<Integer>๋งŒ returnํ•˜๋Š”๊ฑฐ ๊ฐ™์€๋ฐ ๋”ฐ๋กœ ์˜๋„ํ•˜์‹  ๋ถ€๋ถ„์ด ์žˆ๋Š”๊ฑด๊ฐ€์š” ?! ์•„๋ž˜ ๋ณด๋„ˆ์Šค๋„ ๊ทธ๋ ‡๊ตฌ์š” !!
@@ -0,0 +1,58 @@ +package lotto.domain; + +import static lotto.global.Error.DUPLICATED; +import static lotto.global.Error.INVALID_SIZE; + +import java.util.Collections; +import java.util.List; + +public class Lotto { + private final List<Integer> numbers; + + public Lotto(List<Integer> numbers) { + validateSize(numbers); + validateDuplicated(numbers); + validateNumberRange(numbers); + this.numbers = numbers; + } + + private void validateSize(List<Integer> numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException(INVALID_SIZE.getMessage()); + } + } + + private void validateDuplicated(List<Integer> numbers) { + if (numbers.stream() + .distinct().count() != numbers.size()) { + throw new IllegalArgumentException(DUPLICATED.getMessage()); + } + } + + private void validateNumberRange(List<Integer> numbers) { + numbers.forEach(Number::new); + } + + // TODO: ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ + public WinningResult checkWinningResult(WinningLotto winningLotto) { + int winningCount = checkWinningCount(winningLotto); + boolean hasBonusNumber = hasBonusNumber(winningLotto); + return new WinningResult(winningCount, hasBonusNumber); + } + + private int checkWinningCount(WinningLotto winningLotto) { + return (int) numbers.stream() + .filter(number -> winningLotto + .getWinningNumbers() + .contains(number)) + .count(); + } + + private boolean hasBonusNumber(WinningLotto winningLotto) { + return numbers.contains(winningLotto.getBonusNumber()); + } + + public List<Integer> getNumbers() { + return Collections.unmodifiableList(numbers); + } +}
Java
์˜ค .. ๊ทธ๋Ÿฐ๊ฒŒ ์žˆ์—ˆ๊ตฐ์š” ๊ฐœ์ธ์ ์œผ๋กœ ํ˜•๋ณ€ํ™˜ํ•˜๋Š”๊ฒŒ ์ง€์ €๋ถ„ํ•˜๊ฒŒ ๋ณด์ด๊ณ  ๊ฐ•์ œํ•˜๋Š”๊ฑฐ ๊ฐ™์•„์„œ ๋ณ„๋กœ ์„ ํ˜ธํ•˜์ง€ ์•Š์•˜์—ˆ๋Š”๋ฐ ์ƒˆ๋กœ ์•Œ๊ฒŒ๋๋„ค์š” (๋ฉ”๋ชจ)
@@ -0,0 +1,26 @@ +package lotto.domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class LottoRepository { + private final List<Lotto> lottos = new ArrayList<>(); + + public void issueLottos(int purchaseQuantity) { + for (int i = 0; i < purchaseQuantity; i++) { + Lotto lotto = new Lotto(Number.generateNumbers()); + lottos.add(lotto); + } + } + + public List<WinningResult> checkWinningResults(WinningLotto winningLotto) { + return lottos.stream() + .map(lotto -> lotto.checkWinningResult(winningLotto)) + .collect(Collectors.toList()); + } + + public List<Lotto> getLottos() { + return lottos; + } +}
Java
lottos ๋ณ€์ˆ˜๋Š” ์ „์—ญ๋ณ€์ˆ˜๋Š” ์•„๋‹Œ๊ฑฐ ๊ฐ™๊ณ  ์ดˆ๊ธฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด์ค˜์„œ ํ—ท๊ฐˆ๋ฆฌ์‹ ๊ฑฐ ๊ฐ™์•„์š” !!! ์•„๋ž˜ ์ฝ”๋“œ๋ณด๋ฉด lottos.add ํ•˜๋”๋ผ๊ตฌ์š” ์ €๋Š” ๊ฐœ์ธ์ ์œผ๋กœ this ํ‘œํ˜„์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ์ •๋ง ์•Œ์•„๋ณด๊ธฐ ํž˜๋“  ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ฉด ์ž˜ ์‚ฌ์šฉํ•˜์ง„ ์•Š์Šต๋‹ˆ๋‹ค๐Ÿค”
@@ -0,0 +1,30 @@ +package lotto.domain; + +import java.util.Collections; +import java.util.List; +import lotto.global.Error; + +public class WinningLotto { + private final List<Integer> winningNumbers; + private final int bonusNumber; + + public WinningLotto(List<Integer> winningNumbers, int bonusNumber) { + validateBonusNumber(winningNumbers, bonusNumber); + this.winningNumbers = winningNumbers; + this.bonusNumber = bonusNumber; + } + + private void validateBonusNumber(List<Integer> winningNumbers, int bonusNumber) { + if (winningNumbers.contains(bonusNumber)) { + throw new IllegalArgumentException(Error.INVALID_BONUS_NUMBER.getMessage()); + } + } + + public List<Integer> getWinningNumbers() { + return Collections.unmodifiableList(winningNumbers); + } + + public int getBonusNumber() { + return bonusNumber; + } +}
Java
winningNumbers ๋ฅผ List<Integer>๋Œ€์‹  Lottoํ˜•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š” ?!?! ์ €๋Š” ์‚ฌ์‹ค winningLotto์˜ ๊ฒฝ์šฐ Lotto1๊ฐœ๋ฅผ ๊ฐ€์ง€๋˜ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋„ ์ถ”๊ฐ€๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋Š” ํ˜•ํƒœ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์—ˆ๊ฑฐ๋“ ์š” ๊ฐ์ฒด์ƒํƒœ๊ฐ€ ์ˆซ์ž 6๊ฐœ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ๋„ ๋™์ผํ•˜๊ตฌ์š” !!! ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,38 @@ +package lotto.domain; + +import static lotto.domain.Rank.NONE; + +import java.util.Arrays; + +public class WinningResult { + private static final int SECOND_OR_THIRD = 5; + private final int winningCount; + private final boolean hasBonusNumber; + + public WinningResult(int winningCount, boolean matchingBonusNumber) { + this.winningCount = winningCount; + this.hasBonusNumber = matchingBonusNumber; + } + + public Rank toRank() { + if (winningCount == SECOND_OR_THIRD) { + return Arrays.stream(Rank.values()) + .filter(rank -> rank.getWinningCount() == winningCount) + .filter(rank -> rank.hasBonusNumber() == hasBonusNumber) + .findAny() + .orElse(NONE); + } + return Arrays.stream(Rank.values()) + .filter(rank -> rank.getWinningCount() == winningCount) + .findAny() + .orElse(NONE); + } + + public int getWinningCount() { + return winningCount; + } + + public boolean hasBonusNumber() { + return hasBonusNumber; + } +}
Java
์ œ ๊ฐœ์ธ์ ์ธ ์˜๊ฒฌ์œผ๋กœ ์ €๋Š” enum์ด ์„ ์–ธ๋œ ์ˆœ์„œ๋กœ ์ธ๋ฑ์Šค๊ฐ€ ๋ถ€์—ฌ๋˜์ง€๋งŒ enum์˜ ์ˆœ์„œ๋Š” ์–ธ์ œ๋“  ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๊ณ , ๋‹ค๋ฅธ ํ•ญ๋ชฉ์ด ์ถ”๊ฐ€๋˜๋ฉด์„œ enum ์ˆœ์„œ๊ฐ€ ์—‰ํ‚ฌ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค๋Š” ์ ์—์„œ ์ธ๋ฑ์Šค ์ˆœ์„œ๋กœ ์ฝ”๋“œ๋ฅผ ๊ฐœ๋ฐœํ•˜๋Š” ๋ฐฉ์‹์€ ์ง€์–‘ํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค !! ๊ทผ๋ฐ ์ฝ”๋ฉ˜ํŠธ ๋ณด๊ณ  ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ์ €๋„ enum ์ธ๋ฑ์Šค๋Œ€๋กœ print ์ฐ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๊ฒŒ ์žˆ์—ˆ๋˜๊ฑฐ ๊ฐ™์€๋ฐ .......๐Ÿค”๐Ÿฅฒ
@@ -0,0 +1,21 @@ +package lotto.global; + +public enum Error { + INVALID_SIZE("๋กœ๋˜๋Š” 6๊ฐœ์˜ ๋ฒˆํ˜ธ๋กœ ๊ตฌ์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."), + DUPLICATED("๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” ์„œ๋กœ ์ค‘๋ณต๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."), + INVALID_TYPE("์ˆซ์ž๋งŒ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."), + INVALID_UNIT("1,000์› ๋‹จ์œ„๋กœ๋งŒ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."), + INVALID_AMOUNT_RANGE("๊ตฌ๋งค๊ธˆ์•ก์€ 1,000์› ์ด์ƒ 100,000์› ์ดํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."), + INVALID_NUMBER_RANGE("๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1-45 ์‚ฌ์ด์˜ ์ˆซ์ž๋งŒ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."), + INVALID_BONUS_NUMBER("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋Š” ๋กœ๋˜๋ฒˆํ˜ธ์™€ ์ค‘๋ณต๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + private static final String ERROR = "[ERROR] "; + private final String message; + + Error(String message) { + this.message = ERROR + message; + } + + public String getMessage() { + return message; + } +}
Java
์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ์•ž์— ๋ถ™๋Š” [ERROR]๋ฅผ ์ƒ์ˆ˜ ์ฒ˜๋ฆฌ ํ•ด์ฃผ์…จ๋„ค์š”! ์ €๋Š” enumํด๋ž˜์Šค ์•ˆ์—์„œ๋Š” ์ƒ์ˆ˜์ฒ˜๋ฆฌํ•  ์ƒ๊ฐ์„ ๋ชปํ–ˆ๋Š”๋ฐ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค
@@ -0,0 +1,58 @@ +package lotto.domain; + +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public enum Rank { + NONE(0, false, 0), + FIFTH(3, false, 5_000), + FOURTH(4, false, 50_000), + THIRD(5, false, 1_500_000), + SECOND(5, true, 30_000_000), + FIRST(6, false, 2_000_000_000); + + private final int winningCount; + private final boolean hasBonusNumber; + private final int prize; + + Rank(int winningCount, boolean hasBonusNumber, int prize) { + this.winningCount = winningCount; + this.hasBonusNumber = hasBonusNumber; + this.prize = prize; + } + + public static WinningStats countRanks(List<WinningResult> winningResults) { + Map<Rank, Integer> winningStats = new EnumMap<>(Rank.class); + List<Rank> ranks = convertToRanks(winningResults); + for (Rank rank : values()) { + int count = Collections.frequency(ranks, rank); + winningStats.put(rank, count); + } + return new WinningStats(winningStats); + } + + private static List<Rank> convertToRanks(List<WinningResult> winningResults) { + return winningResults.stream() + .map(WinningResult::toRank) + .collect(Collectors.toList()); + } + + public long calculateProfit(int count) { + return (long) prize * count; + } + + public int getWinningCount() { + return winningCount; + } + + public boolean hasBonusNumber() { + return hasBonusNumber; + } + + public int getPrize() { + return prize; + } +}
Java
```java public enum BonusMatchStatus { MATCHED, NOT_MATCHED, IRRELEVANT; } ``` ```java FIRST(6, BonusMatchStatus.IRRELEVANT, 2_000_000_000), SECOND(5, BonusMatchStatus.MATCHED, 30_000_000), THIRD(5, BonusMatchStatus.NOT_MATCHED, 1_500_000), FOURTH(4, BonusMatchStatus.IRRELEVANT, 50_000), FIFTH(3, BonusMatchStatus.IRRELEVANT, 5_000), LOSING(0, BonusMatchStatus.IRRELEVANT, 0); ``` enum์•ˆ์—์„œ boolean ํƒ€์ž…์„ ์ด์šฉํ•˜์—ฌ 2๋“ฑ๊ณผ 3๋“ฑ์„ ๊ตฌ๋ถ„ํ•ด์ฃผ์…จ๋„ค์š”! ์ €๋Š” if๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ๋‹ค๊ตฌ๋ถ„ํ•ด์คฌ๋Š”๋ฐ ๋” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ธ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ €๋„ ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฅธ ๋ฆฌ๋ทฐ๋ฅผ ๋ณด๋ฉด์„œ ๊ณต๋ถ€ํ•˜๋‹ค๊ฐ€ ์•Œ๊ฒŒ ๋œ ์‚ฌ์‹ค์ธ๋ฐ ๋ณด๋„ˆ์Šค๋ณผ์˜ ์ผ์น˜์—ฌ๋ถ€๋ฅผ true/false๋กœ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ์„œ๋กœ false๊ฐ’์— ๋Œ€ํ•ด ์„œ๋กœ ๋‹ค๋ฅธ ๊ด€์ ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. false๋ผ๋Š” ๊ฐ’์ด "์ผ์น˜ํ•˜๋ฉด ์•ˆ๋œ๋‹ค๋Š” ๊ฒƒ", "์ƒ๊ด€์—†๋‹ค" ๋‘๊ฐœ๋กœ ๋‚˜๋‰  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 2๋“ฑ(์ผ์น˜ํ•ด์•ผํ•œ๋‹ค), 3๋“ฑ(์ผ์น˜ํ•˜๋ฉด ์•ˆ๋œ๋‹ค), ๋‚˜๋จธ์ง€๋“ฑ์ˆ˜(์ƒ๊ด€์—†๋‹ค) 3๊ฐ€์ง€ ์ผ€์ด์Šค๊ฐ€ ๋‚˜์˜จ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค ๊ทธ๋ž˜์„œ ์ด๋ ‡๊ฒŒ enum์‘ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,47 @@ +package lotto.view; + +import static lotto.global.Error.INVALID_TYPE; + +import camp.nextstep.edu.missionutils.Console; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class InputView { + private static final String ASK_PURCHASE_AMOUNT = "๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String ASK_WINNING_NUMBERS = "\n๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String ASK_BONUS_NUMBER = "\n๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + + + public static int askPurchaseAmount() { + System.out.println(ASK_PURCHASE_AMOUNT); + String input = Console.readLine(); + validateType(input); + return Integer.parseInt(input); + } + + public static List<Integer> askWinningNumbers() { + System.out.println(ASK_WINNING_NUMBERS); + String input = Console.readLine(); + return Arrays.stream(input.trim() + .split(",")) + .peek(number -> validateType(number)) + .mapToInt(number -> Integer.parseInt(number)) + .boxed() + .collect(Collectors.toList()); + } + + public static int askBonusNumber() { + System.out.println(ASK_BONUS_NUMBER); + String input = Console.readLine(); + validateType(input); + return Integer.parseInt(input); + } + + private static void validateType(String input) { + if (!input.chars() + .allMatch(Character::isDigit)) { + throw new IllegalArgumentException(INVALID_TYPE.getMessage()); + } + } +}
Java
ํ•ด๋‹น ๋ถ€๋ถ„์—์„œ ์•„๋ฌด๋Ÿฐ ์ž…๋ ฅ์—†์ด ์—”ํ„ฐ๋ฅผ ๋ˆŒ๋ €์„์‹œ ๋ฌธ์ž๋กœ ์žกํžˆ์ง€ ์•Š๊ณ  For input string: "" ์˜ค๋ฅ˜๋กœ ๋น ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ €๋„ ๊ทธ ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•˜์—ฌ ๊ตฌ๊ธ€๋ง์„ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. **isDigit** public static boolean isDigit(int codePoint) ์ง€์ •๋œ ๋ฌธ์ž (Unicode ์ฝ”๋“œ ํฌ์ธํŠธ)๊ฐ€ ์ˆซ์ž์ธ๊ฐ€ ์–ด๋–ค๊ฐ€๋ฅผ ํŒ์ •ํ•ฉ๋‹ˆ๋‹ค. [getType(codePoint)](http://cris.joongbu.ac.kr/course/java/api/java/lang/Character.html#getType(int)) ์— ์˜ํ•ด ๋‚˜ํƒ€๋‚˜๋Š” ๋ฒ”์šฉ ์นดํ…Œ๊ณ ๋ฆฌํ˜•์ด **DECIMAL_DIGIT_NUMBER**์ธ ๊ฒฝ์šฐ, ์ˆซ์ž๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. **DECIMAL_DIGIT_NUMBER** public static final byte DECIMAL_DIGIT_NUMBER Unicode ์‚ฌ์–‘์˜ ๋ฒ”์šฉ ์นดํ…Œ๊ณ ๋ฆฌ ใ€ŒNdใ€ ์ด๋Ÿฌํ•œ ์ด์œ  ๋•Œ๋ฌธ์— ์•„๋ฌด๊ฒƒ๋„ ์ž…๋ ฅ๋ฐ›์ง€ ์•Š์€ "" ์ฆ‰ Nd ์ผ๋•Œ ์ˆซ์ž๋กœ ํ†ต๊ณผ ํ•˜์˜€๋‹ค๊ฐ€ ๋‚˜์ค‘์— NumberFormatException ์˜ค๋ฅ˜๊ฐ€ ๋œจ๋Š” ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค. (isDigit ํด๋ž˜์Šค) http://cris.joongbu.ac.kr/course/java/api/java/lang/Character.html#isDigit(int)
@@ -0,0 +1,58 @@ +package lotto.domain; + +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public enum Rank { + NONE(0, false, 0), + FIFTH(3, false, 5_000), + FOURTH(4, false, 50_000), + THIRD(5, false, 1_500_000), + SECOND(5, true, 30_000_000), + FIRST(6, false, 2_000_000_000); + + private final int winningCount; + private final boolean hasBonusNumber; + private final int prize; + + Rank(int winningCount, boolean hasBonusNumber, int prize) { + this.winningCount = winningCount; + this.hasBonusNumber = hasBonusNumber; + this.prize = prize; + } + + public static WinningStats countRanks(List<WinningResult> winningResults) { + Map<Rank, Integer> winningStats = new EnumMap<>(Rank.class); + List<Rank> ranks = convertToRanks(winningResults); + for (Rank rank : values()) { + int count = Collections.frequency(ranks, rank); + winningStats.put(rank, count); + } + return new WinningStats(winningStats); + } + + private static List<Rank> convertToRanks(List<WinningResult> winningResults) { + return winningResults.stream() + .map(WinningResult::toRank) + .collect(Collectors.toList()); + } + + public long calculateProfit(int count) { + return (long) prize * count; + } + + public int getWinningCount() { + return winningCount; + } + + public boolean hasBonusNumber() { + return hasBonusNumber; + } + + public int getPrize() { + return prize; + } +}
Java
์ด๋ฒˆ์— ํฐ ์ˆ˜์˜ ์ž๋ฃŒํ˜•์„ ์ฐพ์•„๋ณด๋‹ค๊ฐ€ BigDecimal์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ ๋งŒ์•ฝ ๊ทธ๋Ÿด์ผ ์—†๊ฒ ์ง€๋งŒ long๋ฒ”์œ„๋„ ๋ฒ—์–ด๋‚œ๋‹ค๋ฉด ์“ฐ๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,30 @@ +package christmas.controller; + +import christmas.model.UserOrder; +import christmas.service.ChristmasEventService; + +public class ChristmasEventController { + + private static final String EVENT_START_MESSAGE = "์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค."; + + private final ChristmasEventService christmasEventService; + + public ChristmasEventController() { + System.out.println(EVENT_START_MESSAGE); + christmasEventService = new ChristmasEventService(); + } + + public void run() { + UserOrder userOrder = requestUserInput(); + + requestUserBenefit(userOrder); + } + + public UserOrder requestUserInput() { + return christmasEventService.requestUserInput(); + } + + public void requestUserBenefit(UserOrder userOrder) { + christmasEventService.requestUserBenefit(userOrder); + } +}
Java
ํ˜น์‹œ view๊ฐ€ ์•„๋‹Œ controller์— ๋”ฐ๋กœ ์ถœ๋ ฅ ๋ฉ”์„ธ์ง€๋ฅผ ๋‘์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ ์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹น
@@ -0,0 +1,22 @@ +package christmas.constant; + +public class Constants { + + public static final String DEFAULT_CONDITION = "์—†์Œ"; + public static final String STAR_BADGE = "๋ณ„"; + public static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + public static final String SANTA_BADGE = "์‚ฐํƒ€"; + public static final int MAX_ORDER_COUNT = 20; + public static final int MIN_INPUT_DATE = 1; + public static final int MAX_INPUT_DATE = 31; + public static final int D_DAY_SALE_DEFAULT = 1000; + public static final int D_DAY_SALE_DATE = 25; + public static final int D_DAY_INCREASE_PRICE = 100; + public static final int SPECIAL_SALE_DEFAULT = 1000; + public static final int WEEKDAY_SALE_DEFAULT = 2023; + public static final int WEEKEND_SALE_DEFAULT = 2023; + public static final int PRESENT_CHAMPAGNE_PRICE = 25000; + public static final int MIN_BENEFITABLE_PRICE = 10000; + public static final int MIN_PRESENTABLE_PRICE = 120000; + +}
Java
๋‹ค์Œ์—๋Š” enum์œผ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์‚ฌ์šฉํ•ด ๋ณด์‹œ๋Š”๊ฑธ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,187 @@ +package christmas.model; + +import static christmas.constant.Constants.DEFAULT_CONDITION; +import static christmas.constant.Constants.D_DAY_INCREASE_PRICE; +import static christmas.constant.Constants.D_DAY_SALE_DATE; +import static christmas.constant.Constants.D_DAY_SALE_DEFAULT; +import static christmas.constant.Constants.MIN_BENEFITABLE_PRICE; +import static christmas.constant.Constants.MIN_PRESENTABLE_PRICE; +import static christmas.constant.Constants.PRESENT_CHAMPAGNE_PRICE; +import static christmas.constant.Constants.SANTA_BADGE; +import static christmas.constant.Constants.SPECIAL_SALE_DEFAULT; +import static christmas.constant.Constants.STAR_BADGE; +import static christmas.constant.Constants.TREE_BADGE; +import static christmas.constant.Constants.WEEKDAY_SALE_DEFAULT; +import static christmas.constant.Constants.WEEKEND_SALE_DEFAULT; + +import java.util.LinkedHashMap; + +public class BenefitCalculator { + + private static int totalPrice; + private static int discountedTotalPrice; + + private static int dDaySalePrice; + private static int weekendSalePrice; + private static int weekdaySalePrice; + private static int specialSalePrice; + + private static String present; + private static int presentPrice; + + private static LinkedHashMap<String, Integer> totalBenefitResult; + private static int totalBenefitPrice; + + private static String eventBadge; + + private static UserOrder userOrder; + + public BenefitCalculator(UserOrder userOrder) { + this.userOrder = userOrder; + totalPrice = 0; + discountedTotalPrice = 0; + dDaySalePrice = 0; + weekendSalePrice = 0; + weekdaySalePrice = 0; + specialSalePrice = 0; + present = DEFAULT_CONDITION; + presentPrice = 0; + totalBenefitResult = new LinkedHashMap<>(); + totalBenefitPrice = 0; + eventBadge = DEFAULT_CONDITION; + } + + public void calculate() { + totalPrice(); + dDaySalePrice(); + weekdaySalePrice(); + weekendSalePrice(); + specialSalePrice(); + presentPrice(); + totalBenefit(); + discountedTotalPrice(); + eventBadge(); + } + + public LinkedHashMap<String, Integer> getReservationOrder() { + return userOrder.getReservationOrder(); + } + + public int getReservationDate() { + return userOrder.getReservationDate(); + } + + public int getTotalPrice() { + return totalPrice; + } + + public String getPresent() { + return present; + } + + public LinkedHashMap<String, Integer> getBenefitResult() { + return totalBenefitResult; + } + + public int getBenefitPrice() { + return totalBenefitPrice; + } + + public int getDiscountedTotalPrice() { + return discountedTotalPrice; + } + + public String getEventBadge() { + return eventBadge; + } + + private void totalPrice() { + getReservationOrder().forEach((key, value) -> { + totalPrice += Menu.getPrice(key) * value; + }); + } + + private void dDaySalePrice() { + int date = getReservationDate(); + if (date <= D_DAY_SALE_DATE) { + dDaySalePrice -= D_DAY_SALE_DEFAULT + (date - 1) * D_DAY_INCREASE_PRICE; + } + } + + private void weekdaySalePrice() { + if (!isWeekend()) { + getReservationOrder().forEach((key, value) -> { + if (Menu.isDessert(key)) { + weekdaySalePrice -= value * WEEKDAY_SALE_DEFAULT; + } + }); + } + } + + private void weekendSalePrice() { + if (isWeekend()) { + getReservationOrder().forEach((key, value) -> { + if (Menu.isMainDish(key)) { + weekendSalePrice -= value * WEEKEND_SALE_DEFAULT; + } + }); + } + } + + private void specialSalePrice() { + if (isSpecialDay()) { + specialSalePrice -= SPECIAL_SALE_DEFAULT; + } + } + + private void presentPrice() { + if (isPresentAvailable()) { + present = "์ƒดํŽ˜์ธ 1๊ฐœ"; + presentPrice -= PRESENT_CHAMPAGNE_PRICE; + } + } + + private void totalBenefit() { + totalBenefitResult.put("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ", dDaySalePrice); + totalBenefitResult.put("ํ‰์ผ ํ• ์ธ", weekdaySalePrice); + totalBenefitResult.put("์ฃผ๋ง ํ• ์ธ", weekendSalePrice); + totalBenefitResult.put("ํŠน๋ณ„ ํ• ์ธ", specialSalePrice); + totalBenefitResult.put("์ฆ์ • ์ด๋ฒคํŠธ", presentPrice); + + totalBenefitResult.forEach((key, value) -> { + totalBenefitPrice += value; + }); + } + + private void discountedTotalPrice() { + discountedTotalPrice = totalPrice + totalBenefitPrice - presentPrice; + } + + private void eventBadge() { + if (totalBenefitPrice < -5000) { + eventBadge = STAR_BADGE; + } + if (totalBenefitPrice < -10000) { + eventBadge = TREE_BADGE; + } + if (totalBenefitPrice < -20000) { + eventBadge = SANTA_BADGE; + } + } + + public boolean isBenefitAvailable() { + return totalPrice >= MIN_BENEFITABLE_PRICE; + } + + private boolean isSpecialDay() { + return getReservationDate() % 7 == 3 || getReservationDate() == 25; + } + + private boolean isWeekend() { + return getReservationDate() % 7 == 1 || getReservationDate() % 7 == 2; + } + + private boolean isPresentAvailable() { + return totalPrice >= MIN_PRESENTABLE_PRICE; + } +}
Java
๋งŽ์€ ํ•„๋“œ์˜ ์ˆ˜๋กœ ๋ณด์•„ ํด๋ž˜์Šค๊ฐ€ ๋งŽ์€ ์ฑ…์ž„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ถ„๋ฆฌ๋ฅผ ํ•ด๋ณด์‹œ๋Š”๊ฒŒ ์ข‹์„ ๊ฑฐ ๊ฐ™์•„์š”
@@ -0,0 +1,108 @@ +package christmas.validator; + +import static christmas.constant.Constants.MAX_ORDER_COUNT; +import static christmas.constant.ErrorMessages.WRONG_ORDER; + +import christmas.model.Menu; +import java.util.LinkedHashMap; +import java.util.List; + +public class OrderValidator { + + private static LinkedHashMap<String, Integer> reservationOrder; + + public OrderValidator() { + reservationOrder = new LinkedHashMap<>(); + } + + public LinkedHashMap<String, Integer> validate(String input) { + List<String> orders = isCorrectFormat(input); + isCountNumber(orders); + isMenuDuplicate(orders); + isInMenu(); + isMenuOnlyBeverage(); + isCountInRange(); + isTotalCountInRange(); + return reservationOrder; + } + + private List<String> isCorrectFormat(String input) { + List<String> orders = List.of(input.split(",")); + for (String order : orders) { + List<String> menuAndCount = List.of(order.split("-")); + if (menuAndCount.size() != 2) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + } + return orders; + } + + private void isCountNumber(List<String> orders) { + for (String order : orders) { + List<String> menuAndCount = List.of(order.split("-")); + try { + Integer.parseInt(menuAndCount.get(1)); + } catch (NumberFormatException e) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + } + } + + private void isMenuDuplicate(List<String> orders) { + for (String order : orders) { + List<String> menuAndCount = List.of(order.split("-")); + String foodName = menuAndCount.get(0); + int count = Integer.parseInt(menuAndCount.get(1)); + + if (reservationOrder.containsKey(foodName)) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + reservationOrder.put(foodName, count); + } + } + + private void isInMenu() { + for (String foodName : reservationOrder.keySet()) { + if (!Menu.isInMenu(foodName)) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + } + } + + private void isMenuOnlyBeverage() { + boolean flag = true; + for (String foodName : reservationOrder.keySet()) { + if (!Menu.isBeverage(foodName)) { + flag = false; + } + } + if (flag) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + } + + private void isCountInRange() { + for (int count : reservationOrder.values()) { + if (count < 1) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + } + } + + private void isTotalCountInRange() { + int totalCount = 0; + for (int count : reservationOrder.values()) { + totalCount += count; + } + if (totalCount > MAX_ORDER_COUNT) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + } +}
Java
์ฃผ๋ฌธ์ด ,๋กœ ๋‚˜๋‰˜์–ด์ ธ ์žˆ์ง€ ์•Š๋Š”๊ฒฝ์šฐ๋„ ์œ ํšจ์„ฑ ์ฒดํฌ๋ฅผ ํ•ด์ฃผ๋‚˜์š”?
@@ -0,0 +1,108 @@ +package christmas.validator; + +import static christmas.constant.Constants.MAX_ORDER_COUNT; +import static christmas.constant.ErrorMessages.WRONG_ORDER; + +import christmas.model.Menu; +import java.util.LinkedHashMap; +import java.util.List; + +public class OrderValidator { + + private static LinkedHashMap<String, Integer> reservationOrder; + + public OrderValidator() { + reservationOrder = new LinkedHashMap<>(); + } + + public LinkedHashMap<String, Integer> validate(String input) { + List<String> orders = isCorrectFormat(input); + isCountNumber(orders); + isMenuDuplicate(orders); + isInMenu(); + isMenuOnlyBeverage(); + isCountInRange(); + isTotalCountInRange(); + return reservationOrder; + } + + private List<String> isCorrectFormat(String input) { + List<String> orders = List.of(input.split(",")); + for (String order : orders) { + List<String> menuAndCount = List.of(order.split("-")); + if (menuAndCount.size() != 2) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + } + return orders; + } + + private void isCountNumber(List<String> orders) { + for (String order : orders) { + List<String> menuAndCount = List.of(order.split("-")); + try { + Integer.parseInt(menuAndCount.get(1)); + } catch (NumberFormatException e) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + } + } + + private void isMenuDuplicate(List<String> orders) { + for (String order : orders) { + List<String> menuAndCount = List.of(order.split("-")); + String foodName = menuAndCount.get(0); + int count = Integer.parseInt(menuAndCount.get(1)); + + if (reservationOrder.containsKey(foodName)) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + reservationOrder.put(foodName, count); + } + } + + private void isInMenu() { + for (String foodName : reservationOrder.keySet()) { + if (!Menu.isInMenu(foodName)) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + } + } + + private void isMenuOnlyBeverage() { + boolean flag = true; + for (String foodName : reservationOrder.keySet()) { + if (!Menu.isBeverage(foodName)) { + flag = false; + } + } + if (flag) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + } + + private void isCountInRange() { + for (int count : reservationOrder.values()) { + if (count < 1) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + } + } + + private void isTotalCountInRange() { + int totalCount = 0; + for (int count : reservationOrder.values()) { + totalCount += count; + } + if (totalCount > MAX_ORDER_COUNT) { + reservationOrder.clear(); + throw new IllegalArgumentException(WRONG_ORDER.getMessage()); + } + } +}
Java
','์‰ผํ‘œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ์ „์ฒด ์ž…๋ ฅ์„ ํ•˜๋‚˜์˜ ์ฃผ๋ฌธ์œผ๋กœ ์ธ์‹ํ•˜๊ณ  ๊ทธ ๋‹ค์Œ ๋‹จ๊ณ„์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋„๋ก ํ˜๋Ÿฌ๊ฐ€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๊ฒ€์ฆ ๋กœ์ง์— ์—ฌ๋Ÿฌ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ปค๋ฒ„ํ•˜๋Š” ๋กœ์ง์ด๋ผ ์ƒ๊ฐ๋˜์–ด ๋ถ„๋ฆฌ๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™์•„์š” ใ… 
@@ -0,0 +1,40 @@ +package racingcar.view; + +import camp.nextstep.edu.missionutils.Console; +import java.util.Arrays; +import java.util.List; +import racingcar.util.validator.AttemptsNumberValidator; +import racingcar.util.validator.CarNamesValidator; +import racingcar.util.Constant; +import racingcar.util.validator.Validator; + +public class InputView { + + private final Validator namesValidator; + private final Validator attempsValidator; + + private InputView() { + this.namesValidator = new CarNamesValidator(); + this.attempsValidator = new AttemptsNumberValidator(); + } + + public static InputView getInstance() { + return new InputView(); + } + + public List<String> readCarNames() { + String input = input(); + namesValidator.validate(input); + return Arrays.asList(input.split(Constant.DELIMITER_COMMA)); + } + + public int readAttemptsNumber() { + String input = input(); + attempsValidator.validate(input); + return Integer.parseInt(input); + } + + private String input() { + return Console.readLine(); + } +}
Java
์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์‹œ๋ ค๊ณ  ํ•œ๊ฒƒ ๊ฐ™์€๋ฐ ํ•„๋“œ ์ƒ์— InputView instance = new InputView() ๋ฅผ ์„ ์–ธํ•ด๋†“์€๊ฒŒ ์•„๋‹ˆ๋ฉด getInstance๋ฅผ ํ• ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์–ด์„œ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜์‹  ์˜๋ฏธ๊ฐ€ ์—†์ง€ ์•Š๋‚˜์š”??? ๊ถ๊ธˆํ•ด์„œ ์—ฌ์ญค๋ด…๋‹ˆ๋‹ค!
@@ -0,0 +1,31 @@ +package racingcar.domain.car; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class Cars { + + private final List<Car> cars = new ArrayList<>(); + + public void addCar(Car car) { + cars.add(car); + } + + public List<String> getWinners() { + return cars.stream() + .filter(car -> car.isWinner(getMaxPosition())) + .map(Car::getName) + .collect(Collectors.toList()); + } + + private int getMaxPosition() { + return Collections.max(cars.stream() + .map(Car::getPosition).toList()); + } + + public List<Car> getCars() { + return Collections.unmodifiableList(cars); + } +}
Java
๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ• ๋•Œ getter๋ฅผ ์ง€์–‘ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์„ค๊ณ„๋ฅผ ์›ํ•˜์‹ ๋‹ค๋ฉด ํ•ด๋‹น ๊ธ€์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค! https://tecoble.techcourse.co.kr/post/2020-04-28-ask-instead-of-getter/
@@ -0,0 +1,44 @@ +package racingcar.util.validator; + +import java.util.regex.Pattern; + +public class AttemptsNumberValidator extends Validator { + + private static final int ATTEMPTS_MIN_RANGE = 1; + private static final int ATTEMPTS_MAX_RANGE = 20; + private final Pattern numberValidatePattern = Pattern.compile("^[0-9]{1,2}$"); + + @Override + public void validate(String input) { + validateNumber(input); + validateNumberRange(input); + } + + private void validateNumberRange(String input) { + int number = Integer.parseInt(input); + if (number < ATTEMPTS_MIN_RANGE || number > ATTEMPTS_MAX_RANGE) { + throw new IllegalArgumentException(ErrorMessage.INVALID_NUMBER.getMessage()); + } + } + + private void validateNumber(String input) { + if (!numberValidatePattern.matcher(input).matches()) { + throw new IllegalArgumentException(ErrorMessage.INVALID_NUMBER.getMessage()); + } + } + + private enum ErrorMessage { + INVALID_NUMBER("1 ์ด์ƒ 20 ์ดํ•˜์˜ ์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."); + + private final String Message; + + ErrorMessage(String message) { + Message = message; + } + + public String getMessage() { + return Message; + } + } + +}
Java
ํ•ด๋‹น enum์— ์—๋Ÿฌ๋ฉ”์‹œ์ง€๊ฐ€ 1๊ฐœ๋ผ์„œ Validator์— ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•ด์ฃผ์…”๋„ ๋ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,53 @@ +package racingcar.util.validator; + +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; +import racingcar.util.Constant; + +public class CarNamesValidator extends Validator { + + private static final int CAR_NAMES_MIN_RANGE = 2; + private static final int CAR_NAMES_MAX_RANGE = 20; + private final Pattern nameValidatePattern = Pattern.compile("^[a-zA-Z๊ฐ€-ํžฃ0-9]{1,5}$"); + + @Override + public void validate(String input) { + List<String> carNames = Arrays.asList(input.split(Constant.DELIMITER_COMMA)); + validateIterator(carNames); + validateCarNamesNumbers(carNames); + } + + private void validateIterator(List<String> carNames) { + for (String carName : carNames) { + validateNamePattern(carName); + } + } + + private void validateNamePattern(String carName) { + if (!nameValidatePattern.matcher(carName).matches()) { + throw new IllegalArgumentException(ErrorMessage.INVALID_NAMES.getMessage()); + } + } + + private void validateCarNamesNumbers(List<String> carNames) { + if (carNames.size() < CAR_NAMES_MIN_RANGE || carNames.size() > CAR_NAMES_MAX_RANGE) { + throw new IllegalArgumentException(ErrorMessage.INVALID_NAMES_NUMBERS.getMessage()); + } + } + + private enum ErrorMessage { + INVALID_NAMES("ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ 5์ž ์ดํ•˜์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."), + INVALID_NAMES_NUMBERS("2์ด์ƒ 20๊ฐœ ์ดํ•˜์˜ ์ด๋ฆ„์„ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”."); + + private final String Message; + + ErrorMessage(String message) { + Message = message; + } + + public String getMessage() { + return Message; + } + } +}
Java
์ •๊ทœ์‹ ํ‘œํ˜„ ์—ฌ๊ธฐ์„œ๋„ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค!
@@ -0,0 +1,63 @@ +package racingcar.view; + +import java.util.List; +import racingcar.domain.car.Car; +import racingcar.domain.car.Cars; + +public class OutputView { + + private OutputView() { + } + + public static OutputView getInstance() { + return new OutputView(); + } + + public void printCarNamesInput() { + System.out.println(Message.INPUT_CAR_NAMES.getMessage()); + } + + public void printAttemptsInput() { + System.out.println(Message.INPUT_ATTEMPTS_NUMBER.getMessage()); + } + + public void printExecutionResult() { + System.out.printf(Message.EXECUTION_RESULT.getMessage()); + } + + public void printResult(Cars cars) { + for (Car car : cars.getCars()) { + System.out.printf(Message.RESULT_FORM.getMessage(), car.getName(), + Message.POSITION_MARK.getMessage().repeat(car.getPosition())); + } + } + + public void printSpace() { + System.out.println(); + } + + public void printWinners(List<String> winners) { + System.out.printf(Message.WINNERS.getMessage(), + String.join(Message.WINNERS_DELIMITER.getMessage(), winners)); + } + + private enum Message { + INPUT_CAR_NAMES("๊ฒฝ์ฃผํ•  ์ž๋™์ฐจ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.(์ด๋ฆ„์€ ์‰ผํ‘œ(,) ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„)"), + INPUT_ATTEMPTS_NUMBER("์‹œ๋„ํ•  ํšŒ์ˆ˜๋Š” ๋ช‡ํšŒ์ธ๊ฐ€์š”?"), + EXECUTION_RESULT("%n์‹คํ–‰๊ฒฐ๊ณผ%n"), + RESULT_FORM("%s : %s%n"), + POSITION_MARK("-"), + WINNERS("์ตœ์ข… ์šฐ์Šน์ž : %s%n"), + WINNERS_DELIMITER(", "); + + private final String message; + + Message(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + } +}
Java
```suggestion Message.POSITION_MARK .getMessage() .repeat(car.getPosition())); ``` ๊ฐœํ–‰์„ ํ†ตํ•ด ๊ฐ€๋…์„ฑ์„ ๋†’์—ฌ์ฃผ์‹œ๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,31 @@ +package racingcar.domain.car; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class Cars { + + private final List<Car> cars = new ArrayList<>(); + + public void addCar(Car car) { + cars.add(car); + } + + public List<String> getWinners() { + return cars.stream() + .filter(car -> car.isWinner(getMaxPosition())) + .map(Car::getName) + .collect(Collectors.toList()); + } + + private int getMaxPosition() { + return Collections.max(cars.stream() + .map(Car::getPosition).toList()); + } + + public List<Car> getCars() { + return Collections.unmodifiableList(cars); + } +}
Java
์ข‹์€ ์ž๋ฃŒ ๋งํฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,40 @@ +package racingcar.view; + +import camp.nextstep.edu.missionutils.Console; +import java.util.Arrays; +import java.util.List; +import racingcar.util.validator.AttemptsNumberValidator; +import racingcar.util.validator.CarNamesValidator; +import racingcar.util.Constant; +import racingcar.util.validator.Validator; + +public class InputView { + + private final Validator namesValidator; + private final Validator attempsValidator; + + private InputView() { + this.namesValidator = new CarNamesValidator(); + this.attempsValidator = new AttemptsNumberValidator(); + } + + public static InputView getInstance() { + return new InputView(); + } + + public List<String> readCarNames() { + String input = input(); + namesValidator.validate(input); + return Arrays.asList(input.split(Constant.DELIMITER_COMMA)); + } + + public int readAttemptsNumber() { + String input = input(); + attempsValidator.validate(input); + return Integer.parseInt(input); + } + + private String input() { + return Console.readLine(); + } +}
Java
์ œ๊ฐ€ ์ด๋ฆ„์„ ์˜คํ•ด์˜ ์†Œ์ง€๊ฐ€ ์žˆ๋„๋ก ์ž‘์„ฑํ–ˆ๋„ค์š”. ์‚ฌ์‹ค ์‹ฑ๊ธ€ํ†ค๋„ ๊ณ ๋ฏผ ํ–ˆ์—ˆ๊ธฐ๋„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฒˆ์—๋Š” ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ํŒจํ„ด ์‚ฌ์šฉ ์ด์œ  ์ค‘ ํ•˜๋‚˜์ธ new๋ฅผ ์ด์šฉํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ๋ณด๋‹ค ์ •์  ๋ฉ”์†Œ๋“œ์ด๋ฆ„์„ ํ†ตํ•ด ์ƒ์„ฑ ๋ชฉ์ ์— ๋Œ€ํ•œ ๊ฐ€๋…์„ฑ?์ด๋ผ๋Š” ์ธก๋ฉด์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ค์ง€ ์˜๊ฒฌ์„ ์—ฌ์ญค๋ณด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,35 @@ +package christmas.domain.booking; + +import christmas.domain.booking.dto.MenuItem; +import christmas.domain.booking.dto.MenuType; +import java.util.Arrays; +import java.util.List; + +public class Menu { + // APPETIZER + public static final MenuItem APPETIZER_1 = new MenuItem(MenuType.APPETIZER, "์–‘์†ก์ด์ˆ˜ํ”„", 6_000); + public static final MenuItem APPETIZER_2 = new MenuItem(MenuType.APPETIZER, "ํƒ€ํŒŒ์Šค", 5_500); + public static final MenuItem APPETIZER_3 = new MenuItem(MenuType.APPETIZER, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000); + + // MAIN + public static final MenuItem MAIN_1 = new MenuItem(MenuType.MAIN, "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000); + public static final MenuItem MAIN_2 = new MenuItem(MenuType.MAIN, "๋ฐ”๋น„ํ๋ฆฝ", 54_000); + public static final MenuItem MAIN_3 = new MenuItem(MenuType.MAIN, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000); + public static final MenuItem MAIN_4 = new MenuItem(MenuType.MAIN, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000); + + // DESSERT + public static final MenuItem DESSERT_1 = new MenuItem(MenuType.DESSERT, "์ดˆ์ฝ”์ผ€์ดํฌ", 15_000); + public static final MenuItem DESSERT_2 = new MenuItem(MenuType.DESSERT, "์•„์ด์Šคํฌ๋ฆผ", 5_000); + + // BEVERAGE + public static final MenuItem BEVERAGE_1 = new MenuItem(MenuType.BEVERAGE, "์ œ๋กœ์ฝœ๋ผ", 3_000); + public static final MenuItem BEVERAGE_2 = new MenuItem(MenuType.BEVERAGE, "๋ ˆ๋“œ์™€์ธ", 60_000); + public static final MenuItem BEVERAGE_3 = new MenuItem(MenuType.BEVERAGE, "์ƒดํŽ˜์ธ", 25_000); + + public static final List<MenuItem> MENU_ITEMS = Arrays.asList( + APPETIZER_1, APPETIZER_2, APPETIZER_3, + MAIN_1, MAIN_2, MAIN_3, MAIN_4, + DESSERT_1, DESSERT_2, + BEVERAGE_1, BEVERAGE_2, BEVERAGE_3 + ); +}
Java
Enum์„ ํ™œ์šฉํ•ด์„œ ๋ฉ”๋‰ด๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด์‹œ๋Š” ๊ฒƒ๋„ ์ถ”์ฒœ๋“œ๋ ค์š”!
@@ -0,0 +1,33 @@ +package christmas.domain.payment.discount; + +import static christmas.domain.payment.constants.Constant.PRESENT_YEAR; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.Month; + +public enum DayType { + WEEKDAY, + WEEKEND; + + public static boolean isWeekday(int day) { + LocalDate orderDate = LocalDate.of(PRESENT_YEAR, Month.DECEMBER, day); + DayOfWeek dayOfWeek = orderDate.getDayOfWeek(); + if (isSunday(day)) { + return true; + } + return dayOfWeek.getValue() <= DayOfWeek.THURSDAY.getValue(); + } + + public static boolean isSunday(int day) { + LocalDate orderDate = LocalDate.of(PRESENT_YEAR, Month.DECEMBER, day); + DayOfWeek dayOfWeek = orderDate.getDayOfWeek(); + return dayOfWeek.getValue() == DayOfWeek.SUNDAY.getValue(); + } + + public static boolean isWeekend(int day) { + LocalDate orderDate = LocalDate.of(PRESENT_YEAR, Month.DECEMBER, day); + DayOfWeek dayOfWeek = orderDate.getDayOfWeek(); + return dayOfWeek.getValue() >= DayOfWeek.FRIDAY.getValue(); + } +}
Java
๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์„ ํ™œ์šฉํ•œ๋‹ค๋ฉด ํ• ์ธ ์กฐ๊ฑด์— ํ•„์š”ํ•œ ์š”์ผ์„ ๊ตฌํ•˜๋Š” ์—ฐ์‚ฐ์„ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,161 @@ +package christmas.service; + +import static christmas.domain.booking.MenuSearch.findMenuItem; +import static christmas.domain.booking.constants.Constant.AMOUNT_INDEX; +import static christmas.domain.booking.constants.Constant.AMOUNT_MAX; +import static christmas.domain.booking.constants.Constant.FIRST_DAY; +import static christmas.domain.booking.constants.Constant.LAST_DAY; +import static christmas.domain.booking.constants.Constant.MENU_AMOUNT_DELIMITER; +import static christmas.domain.booking.constants.Constant.MENU_INDEX; +import static christmas.domain.booking.constants.Constant.MENU_TYPE_DELIMITER; +import static christmas.domain.booking.constants.Constant.SEPARATE_TWO; +import static christmas.exception.ErrorMessage.AMOUNT_OUT_OF_RANGE; +import static christmas.exception.ErrorMessage.DATE_OUT_OF_RANGE; +import static christmas.exception.ErrorMessage.REQUEST_INVALID_DATE; +import static christmas.exception.ErrorMessage.REQUEST_INVALID_MENU; + +import christmas.domain.booking.MenuSearch; +import christmas.domain.booking.dto.MenuItem; +import christmas.domain.booking.dto.MenuType; +import christmas.exception.PlannerException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class Parser { + + public static int parseInt(String input) { + try { + validateOutOfRange(input); + return Integer.parseInt(input); + } catch (NumberFormatException exception) { + throw PlannerException.of(REQUEST_INVALID_DATE, exception); + } + } + + private static void validateOutOfRange(String input) { + int day = Integer.parseInt(input); + if (FIRST_DAY <= day && day <= LAST_DAY) { + return; + } + throw PlannerException.from(DATE_OUT_OF_RANGE); + } + + public static Map<MenuItem, Integer> splitMenuAndAmount(String input) { + Map<MenuItem, Integer> menuAndAmountMap = new HashMap<>(); + try { + validateOmittedArgument(input); + validateDuplicateMenu(input); + validateFood(input); + String[] menuAndAmount = input.split(MENU_TYPE_DELIMITER); + Arrays.stream(menuAndAmount).forEach(entry -> processMenuEntry(entry, menuAndAmountMap)); + validateEmpty(menuAndAmountMap); + validateAmount(sumAmount(menuAndAmountMap)); + } catch (PlannerException exception) { + throw PlannerException.of(REQUEST_INVALID_MENU, exception); + } + return menuAndAmountMap; + } + + + private static void processMenuEntry(String entry, Map<MenuItem, Integer> menuAndAmountMap) { + String[] parts = entry.split(MENU_AMOUNT_DELIMITER); + Integer amount = 0; + try { + amount = Integer.parseInt(parts[AMOUNT_INDEX].trim()); + } catch (NumberFormatException exception) { + throw PlannerException.of(REQUEST_INVALID_MENU, exception); + } + if (parts.length == SEPARATE_TWO) { + int eachAmount = Integer.parseInt(parts[AMOUNT_INDEX].trim()); + validateAmount(eachAmount); + String menu = parts[MENU_INDEX].trim(); + Optional<MenuItem> item = MenuSearch.findMenuItem(menu); + Integer finalAmount = amount; + item.ifPresent(menuItem -> menuAndAmountMap.put(menuItem, finalAmount)); + } + } + + private static int sumAmount(Map<MenuItem, Integer> menuAndAmountMap) { + return menuAndAmountMap.values() + .stream() + .mapToInt(Integer::intValue) + .sum(); + } + + private static void validateFood(String input) { + try { + onlyBeverage(input); + certainFoods(input); + } catch (PlannerException exception) { + throw PlannerException.of(REQUEST_INVALID_MENU, exception); + } + } + + private static void certainFoods(String input) { + String[] menuAndAmount = input.split(MENU_TYPE_DELIMITER); + boolean allCertains = Arrays.stream(menuAndAmount) + .map(entry -> entry.split(MENU_AMOUNT_DELIMITER)[MENU_INDEX]) + .allMatch(Parser::isCertainFood); + if (!allCertains) { + throw PlannerException.from(REQUEST_INVALID_MENU); + } + } + + private static boolean isCertainFood(String menu) { + return findMenuItem(menu) + .map(menuItem -> Arrays.asList(MenuType.values()).contains(menuItem.type())) + .orElse(false); + } + + private static void onlyBeverage(String input) { + String[] menuAndAmount = input.split(MENU_TYPE_DELIMITER); + boolean allMatch = Arrays.stream(menuAndAmount) + .map(entry -> entry.split(MENU_AMOUNT_DELIMITER)[MENU_INDEX]) + .allMatch(menu -> findMenuItem(menu) + .map(menuItem -> menuItem.type() == MenuType.BEVERAGE) + .orElse(false) + ); + if (allMatch) { + throw PlannerException.from(REQUEST_INVALID_MENU); + } + } + + private static void validateDuplicateMenu(String input) { + String[] menuAndAmount = input.split(MENU_TYPE_DELIMITER); + String[] menus = Arrays.stream(menuAndAmount) + .map(entry -> entry.split(MENU_AMOUNT_DELIMITER)[MENU_INDEX]) + .toArray(String[]::new); + if (Arrays.stream(menus).distinct().count() != menus.length) { + throw PlannerException.from(REQUEST_INVALID_MENU); + } + } + + + private static void validateAmount(int amount) { + if (AMOUNT_INDEX <= amount && amount <= AMOUNT_MAX) { + return; + } + throw PlannerException.from(AMOUNT_OUT_OF_RANGE); + } + + private static void validateEmpty(Map<MenuItem, Integer> menuAndAmountMap) { + if (menuAndAmountMap.isEmpty()) { + throw PlannerException.from(REQUEST_INVALID_MENU); + } + } + + private static void validateOmittedArgument(String input) { + validateWhiteSpace(input); + if (input.endsWith(MENU_AMOUNT_DELIMITER) || input.endsWith(MENU_TYPE_DELIMITER)) { + throw PlannerException.from(REQUEST_INVALID_MENU); + } + } + + private static void validateWhiteSpace(String input) { + if (input.contains(" ")) { + throw PlannerException.from(REQUEST_INVALID_MENU); + } + } +}
Java
์ฐธ๊ณ  : [๋ธ”๋กœ๊ทธ ๊ธ€](https://velog.io/@kasania/Java-Static-import%EC%97%90-%EB%8C%80%ED%95%9C-%EA%B4%80%EC%B0%B0#static-import%EA%B0%80-%EC%9E%98%EB%AA%BB-%EC%82%AC%EC%9A%A9%EB%90%98%EB%8A%94-%EA%B2%BD%EC%9A%B0) > static import๋Š” ์ •๋ง ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค์˜ "์ด๋ฆ„๋งŒ ๋ณด์•„๋„ ์–ด๋””์— ์†ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋Š”," "์ •์  ๋ฉค๋ฒ„" ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ๋งŒ ์“ฐ๋„๋ก ํ•˜์ž. ์ „๋ฐ˜์ ์ธ ์ฝ”๋“œ ๊ตฌ์กฐ๋ฅผ ๋ชจ๋ฅด๋Š” ์‚ฌ๋žŒ์€ `AMOUNT_INDEX`๋งŒ ๋ณด๋”๋ผ๋„ ์ด๊ฒŒ `domain.booking.constants.Constant.java` ํŒŒ์ผ์— ์žˆ๋‹ค๊ณ  ์˜ˆ์ธกํ•˜๊ธฐ๋Š” ํž˜๋“ค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,161 @@ +package christmas.service; + +import static christmas.domain.booking.MenuSearch.findMenuItem; +import static christmas.domain.booking.constants.Constant.AMOUNT_INDEX; +import static christmas.domain.booking.constants.Constant.AMOUNT_MAX; +import static christmas.domain.booking.constants.Constant.FIRST_DAY; +import static christmas.domain.booking.constants.Constant.LAST_DAY; +import static christmas.domain.booking.constants.Constant.MENU_AMOUNT_DELIMITER; +import static christmas.domain.booking.constants.Constant.MENU_INDEX; +import static christmas.domain.booking.constants.Constant.MENU_TYPE_DELIMITER; +import static christmas.domain.booking.constants.Constant.SEPARATE_TWO; +import static christmas.exception.ErrorMessage.AMOUNT_OUT_OF_RANGE; +import static christmas.exception.ErrorMessage.DATE_OUT_OF_RANGE; +import static christmas.exception.ErrorMessage.REQUEST_INVALID_DATE; +import static christmas.exception.ErrorMessage.REQUEST_INVALID_MENU; + +import christmas.domain.booking.MenuSearch; +import christmas.domain.booking.dto.MenuItem; +import christmas.domain.booking.dto.MenuType; +import christmas.exception.PlannerException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class Parser { + + public static int parseInt(String input) { + try { + validateOutOfRange(input); + return Integer.parseInt(input); + } catch (NumberFormatException exception) { + throw PlannerException.of(REQUEST_INVALID_DATE, exception); + } + } + + private static void validateOutOfRange(String input) { + int day = Integer.parseInt(input); + if (FIRST_DAY <= day && day <= LAST_DAY) { + return; + } + throw PlannerException.from(DATE_OUT_OF_RANGE); + } + + public static Map<MenuItem, Integer> splitMenuAndAmount(String input) { + Map<MenuItem, Integer> menuAndAmountMap = new HashMap<>(); + try { + validateOmittedArgument(input); + validateDuplicateMenu(input); + validateFood(input); + String[] menuAndAmount = input.split(MENU_TYPE_DELIMITER); + Arrays.stream(menuAndAmount).forEach(entry -> processMenuEntry(entry, menuAndAmountMap)); + validateEmpty(menuAndAmountMap); + validateAmount(sumAmount(menuAndAmountMap)); + } catch (PlannerException exception) { + throw PlannerException.of(REQUEST_INVALID_MENU, exception); + } + return menuAndAmountMap; + } + + + private static void processMenuEntry(String entry, Map<MenuItem, Integer> menuAndAmountMap) { + String[] parts = entry.split(MENU_AMOUNT_DELIMITER); + Integer amount = 0; + try { + amount = Integer.parseInt(parts[AMOUNT_INDEX].trim()); + } catch (NumberFormatException exception) { + throw PlannerException.of(REQUEST_INVALID_MENU, exception); + } + if (parts.length == SEPARATE_TWO) { + int eachAmount = Integer.parseInt(parts[AMOUNT_INDEX].trim()); + validateAmount(eachAmount); + String menu = parts[MENU_INDEX].trim(); + Optional<MenuItem> item = MenuSearch.findMenuItem(menu); + Integer finalAmount = amount; + item.ifPresent(menuItem -> menuAndAmountMap.put(menuItem, finalAmount)); + } + } + + private static int sumAmount(Map<MenuItem, Integer> menuAndAmountMap) { + return menuAndAmountMap.values() + .stream() + .mapToInt(Integer::intValue) + .sum(); + } + + private static void validateFood(String input) { + try { + onlyBeverage(input); + certainFoods(input); + } catch (PlannerException exception) { + throw PlannerException.of(REQUEST_INVALID_MENU, exception); + } + } + + private static void certainFoods(String input) { + String[] menuAndAmount = input.split(MENU_TYPE_DELIMITER); + boolean allCertains = Arrays.stream(menuAndAmount) + .map(entry -> entry.split(MENU_AMOUNT_DELIMITER)[MENU_INDEX]) + .allMatch(Parser::isCertainFood); + if (!allCertains) { + throw PlannerException.from(REQUEST_INVALID_MENU); + } + } + + private static boolean isCertainFood(String menu) { + return findMenuItem(menu) + .map(menuItem -> Arrays.asList(MenuType.values()).contains(menuItem.type())) + .orElse(false); + } + + private static void onlyBeverage(String input) { + String[] menuAndAmount = input.split(MENU_TYPE_DELIMITER); + boolean allMatch = Arrays.stream(menuAndAmount) + .map(entry -> entry.split(MENU_AMOUNT_DELIMITER)[MENU_INDEX]) + .allMatch(menu -> findMenuItem(menu) + .map(menuItem -> menuItem.type() == MenuType.BEVERAGE) + .orElse(false) + ); + if (allMatch) { + throw PlannerException.from(REQUEST_INVALID_MENU); + } + } + + private static void validateDuplicateMenu(String input) { + String[] menuAndAmount = input.split(MENU_TYPE_DELIMITER); + String[] menus = Arrays.stream(menuAndAmount) + .map(entry -> entry.split(MENU_AMOUNT_DELIMITER)[MENU_INDEX]) + .toArray(String[]::new); + if (Arrays.stream(menus).distinct().count() != menus.length) { + throw PlannerException.from(REQUEST_INVALID_MENU); + } + } + + + private static void validateAmount(int amount) { + if (AMOUNT_INDEX <= amount && amount <= AMOUNT_MAX) { + return; + } + throw PlannerException.from(AMOUNT_OUT_OF_RANGE); + } + + private static void validateEmpty(Map<MenuItem, Integer> menuAndAmountMap) { + if (menuAndAmountMap.isEmpty()) { + throw PlannerException.from(REQUEST_INVALID_MENU); + } + } + + private static void validateOmittedArgument(String input) { + validateWhiteSpace(input); + if (input.endsWith(MENU_AMOUNT_DELIMITER) || input.endsWith(MENU_TYPE_DELIMITER)) { + throw PlannerException.from(REQUEST_INVALID_MENU); + } + } + + private static void validateWhiteSpace(String input) { + if (input.contains(" ")) { + throw PlannerException.from(REQUEST_INVALID_MENU); + } + } +}
Java
์–ด๋””์„ ๊ฐ€ ๋ดค์—ˆ๋Š”๋ฐ, "try ์•ˆ์— ์žˆ๋Š” ํ–‰๋™๋„ ํ•˜๋‚˜์˜ ์—ญํ• ์ด๋ผ ๋ณผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•œ์ค„๋กœ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ด ์ข‹๋‹ค"๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ```java public static Map<MenuItem, Integer> splitMenuAndAmount(String input) { Map<MenuItem, Integer> menuAndAmountMap = new HashMap<>(); try { validate(input); } catch (PlannerException exception) { throw PlannerException.of(REQUEST_INVALID_MENU, exception); } return menuAndAmountMap; } public static Map<MenuItem, Integer> validate(String input) { validateOmittedArgument(input); validateDuplicateMenu(input); validateFood(input); String[] menuAndAmount = input.split(MENU_TYPE_DELIMITER); Arrays.stream(menuAndAmount).forEach(entry -> processMenuEntry(entry, menuAndAmountMap)); validateEmpty(menuAndAmountMap); validateAmount(sumAmount(menuAndAmountMap)); } ```
@@ -0,0 +1,35 @@ +package christmas.view.output; + +import static christmas.domain.booking.constants.Message.AMOUNT_BEFORE_DISCOUNT; +import static christmas.domain.booking.constants.Message.ORDER; +import static christmas.domain.booking.constants.Message.ORDER_DETAIL; +import static christmas.domain.booking.constants.Message.RECEPTION; + +import christmas.domain.booking.dto.BookMessage; +import java.text.DecimalFormat; + +public class BookingWriterView extends OutputView { + private BookingWriterView() { + } + + public static void MenuOrder(BookMessage bookMessage) { + String message = String.format(RECEPTION.getMessage(), bookMessage.reservationDay()); + long[] amount = {0}; + bookMessage.menuAndAmountMap().forEach((key, value) -> { + amount[0] += (long) key.price() * value; + }); + println(message); + println(ORDER.getMessage()); + bookMessage.menuAndAmountMap().forEach((key, value) -> { + println(String.format(ORDER_DETAIL.getMessage(), key.name(), value)); + + }); + totalRawPrice(amount); + } + + private static void totalRawPrice(long[] amount) { + DecimalFormat df = new DecimalFormat("#,###"); + String formatted = df.format(amount[0]); + println(String.format(AMOUNT_BEFORE_DISCOUNT.getMessage(), formatted)); + } +}
Java
์˜ค static method ๋ฐ–์— ์—†์–ด ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ๋ง‰์œผ์…จ๊ตฐ์š”!
@@ -0,0 +1,35 @@ +package christmas.view.output; + +import static christmas.domain.booking.constants.Message.AMOUNT_BEFORE_DISCOUNT; +import static christmas.domain.booking.constants.Message.ORDER; +import static christmas.domain.booking.constants.Message.ORDER_DETAIL; +import static christmas.domain.booking.constants.Message.RECEPTION; + +import christmas.domain.booking.dto.BookMessage; +import java.text.DecimalFormat; + +public class BookingWriterView extends OutputView { + private BookingWriterView() { + } + + public static void MenuOrder(BookMessage bookMessage) { + String message = String.format(RECEPTION.getMessage(), bookMessage.reservationDay()); + long[] amount = {0}; + bookMessage.menuAndAmountMap().forEach((key, value) -> { + amount[0] += (long) key.price() * value; + }); + println(message); + println(ORDER.getMessage()); + bookMessage.menuAndAmountMap().forEach((key, value) -> { + println(String.format(ORDER_DETAIL.getMessage(), key.name(), value)); + + }); + totalRawPrice(amount); + } + + private static void totalRawPrice(long[] amount) { + DecimalFormat df = new DecimalFormat("#,###"); + String formatted = df.format(amount[0]); + println(String.format(AMOUNT_BEFORE_DISCOUNT.getMessage(), formatted)); + } +}
Java
- ํ”„๋ฆฌ์ฝ”์Šค 1์ฃผ์ฐจ ๊ณตํ†ต ํ”ผ๋“œ๋ฐฑ ์ค‘ > ์ถ•์•ฝํ•˜์ง€ ์•Š๋Š”๋‹ค ์˜๋„๋ฅผ ๋“œ๋Ÿฌ๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ด๋ฆ„์ด ๊ธธ์–ด์ ธ๋„ ๊ดœ์ฐฎ๋‹ค. ๋ˆ„๊ตฌ๋‚˜ ์‹ค์€ ํด๋ž˜์Šค, ๋ฉ”์„œ๋“œ, ๋˜๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ ์ค„์ด๋ ค๋Š” ์œ ํ˜น์— ๊ณง์ž˜ ๋น ์ง€๊ณค ํ•œ๋‹ค. ๊ทธ๋Ÿฐ ์œ ํ˜น์„ ๋ฟŒ๋ฆฌ์ณ๋ผ. ์ถ•์•ฝ์€ ํ˜ผ๋ž€์„ ์•ผ๊ธฐํ•˜๋ฉฐ, ๋” ํฐ ๋ฌธ์ œ๋ฅผ ์ˆจ๊ธฐ๋Š” ๊ฒฝํ–ฅ์ด ์žˆ๋‹ค. ํด๋ž˜์Šค์™€ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ํ•œ ๋‘ ๋‹จ์–ด๋กœ ์œ ์ง€ํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๊ณ  ๋ฌธ๋งฅ์„ ์ค‘๋ณตํ•˜๋Š” ์ด๋ฆ„์„ ์ž์ œํ•˜์ž. ํด๋ž˜์Šค ์ด๋ฆ„์ด Order๋ผ๋ฉด shipOrder๋ผ๊ณ  ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ์ง€์„ ํ•„์š”๊ฐ€ ์—†๋‹ค. ์งง๊ฒŒ ship()์ด๋ผ๊ณ  ํ•˜๋ฉด ํด๋ผ์ด์–ธํŠธ์—์„œ๋Š” order.ship()๋ผ๊ณ  ํ˜ธ์ถœํ•˜๋ฉฐ, ๊ฐ„๊ฒฐํ•œ ํ˜ธ์ถœ์˜ ํ‘œํ˜„์ด ๋œ๋‹ค. ๊ฐ์ฒด ์ง€ํ–ฅ ์ƒํ™œ ์ฒด์กฐ ์›์น™ 5: ์ค„์—ฌ์“ฐ์ง€ ์•Š๋Š”๋‹ค (์ถ•์•ฝ ๊ธˆ์ง€) - `new DecimalFormat("#,###")`์€ ๋งค๋ฒˆ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋˜๋ฏ€๋กœ `private static final`๋กœ ํด๋ž˜์Šค ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•ด์„œ ์“ฐ์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค!
@@ -0,0 +1,6 @@ +package christmas.view.output; + +public class PaymentWriterView extends OutputView { + public PaymentWriterView() { + } +}
Java
์ด ํด๋ž˜์Šค๋Š” ๋ฌด์—‡์„ ์œ„ํ•œ ๊ฒƒ์ผ๊นŒ์š”?
@@ -0,0 +1,33 @@ +package christmas.controller; + +import static org.junit.jupiter.api.Assertions.*; + +import christmas.domain.booking.dto.MenuItem; +import christmas.domain.booking.dto.MenuType; +import christmas.service.Parser; +import christmas.service.Payment; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.Test; + +class PaymentControllerTest { + + @Test + void getRawTotal() { + // given + int reservationDay = 26; + Map<MenuItem, Integer> expected = new HashMap<>(); + Map<MenuItem, Integer> menuAndAmountMap = new HashMap<>(); + MenuItem MenuItem1 = new MenuItem(MenuType.MAIN, "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000); + MenuItem MenuItem2 = new MenuItem(MenuType.MAIN, "๋ฐ”๋น„ํ๋ฆฝ", 54_000); + expected.put(MenuItem1, 1); + expected.put(MenuItem2, 1); + menuAndAmountMap = Parser.splitMenuAndAmount("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,๋ฐ”๋น„ํ๋ฆฝ-1"); + Payment payment = new Payment(reservationDay, menuAndAmountMap); + // when + int expectedTotal = 109_000; + int actualTotal = payment.getRawTotal(menuAndAmountMap); + // then + assertEquals(expectedTotal, actualTotal); + } +} \ No newline at end of file
Java
- ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์„ ํ†ตํ•ด ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์–ด๋–ค ์ƒํ™ฉ์—์„œ ๋ฌด์—‡์„ ํ…Œ์ŠคํŠธ ํ•˜์‹œ๋Š”์ง€ ๋‚˜ํƒ€๋‚ด์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ๋‹ค๋ฅธ์‚ฌ๋žŒ์ด ์™€์„œ ๋ณด๋”๋ผ๋„ ๋ญ˜ ํ…Œ์ŠคํŠธํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์‰ฌ์›Œ์•ผ ํ•˜๋‹ˆ๊นŒ์š” - `assertEquals()`๋ณด๋‹ค๋Š” `assertThat().isEqualTo()`๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์˜์–ด ๋ฌธ์žฅ ์ˆœ์„œ์™€ ๋™์ผํ•˜๊ฒŒ ๋˜์–ด์žˆ์–ด ํ›จ์”ฌ ์ฝ๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. - ์•„๋ž˜ ์ฝ”๋“œ๋Š” ์ œ๊ฐ€ ์ž‘์„ฑํ•œ ํ…Œ์ŠคํŠธ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. ```java @ParameterizedTest(name = "{0}; ๋””์ €ํŠธ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ : {1}") @MethodSource @DisplayName("์ฃผ๋ฌธํ•œ ์ด ๋””์ €ํŠธ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜๋ฅผ ์…€ ์ˆ˜ ์žˆ๋‹ค") void countDessertMenuTest(Map<Menu, Integer> menuToCount, int expected) { Order order = Order.from(menuToCount); int actual = order.countDessertMenu(); assertThat(actual).isEqualTo(expected); } private static Stream<Arguments> countDessertMenuTest() { return Stream.of( Arguments.of(Map.of(DRINK_EXAMPLE, 3, DESSERT_EXAMPLE, 4), 4), Arguments.of(Map.of(MAIN_EXAMPLE, 5, APPETIZER_EXAMPLE, 3), 0), Arguments.of(Map.of(Menu.CHOCOLATE_CAKE, 6, Menu.ICE_CREAM, 4), 10) ); } ```
@@ -0,0 +1,101 @@ +import React, { useEffect } from 'react'; +import styled from 'styled-components'; +import { BiMessageAlt } from 'react-icons/bi'; +import { useIssues } from '../hooks/useIssues'; +import { IssueSchema } from '../types/issuesApi'; +import { Link } from 'react-router-dom'; +import AD from '../components/AD'; +import InfiniteScroll from '../components/InfiniteScroll'; + +function Times(date: number) { + let times; + const time = Math.floor(date / (1000 * 60 * 60)); // ์‹œ๊ฐ„ + const day = Math.floor(date / (1000 * 60 * 60 * 24)); // ์ผ + const year = Math.floor(date / (1000 * 60 * 60 * 24 * 365)); // ๋…„ + + if (year > 0) { + times = `${year}๋…„ ์ „`; + } else if (day > 0) { + times = `${day}์ผ ์ „`; + } else { + times = `${time}์‹œ๊ฐ„ ์ „`; + } + return times; +} + +function Home() { + const { issueList, fetchIssues, isLoading } = useIssues(); + + useEffect(() => { + fetchIssues(); + }, []); + + return ( + <> + {issueList?.map((issue: IssueSchema, index: number) => { + const currentTime = new Date(); + const createdAt = new Date(issue.created_at); + const timeDifference = currentTime.getTime() - createdAt.getTime(); + + return ( + <React.Fragment key={issue.id}> + <InfiniteScroll> + <Container> + <Issues> + <Item> + <IssueLink to={`/issues/${issue.number}`}>{issue.title}</IssueLink> + <OpenedBy> + #{issue.number} opened {Times(timeDifference)} by {issue.user.login} + </OpenedBy> + </Item> + <Comment> + <BiMessageAlt /> {issue.comments} + </Comment> + </Issues> + </Container> + {<AD key={index} index={index} />} + </InfiniteScroll> + </React.Fragment> + ); + })} + {isLoading && <span>Loading...</span>} + </> + ); +} + +export default Home; + +const Container = styled.div` + border: 1px solid black; + height: auto; + padding: 8px; + &:hover { + background-color: lightgray; + } +`; + +const Issues = styled.li` + width: 100%; + display: flex; + justify-content: space-between; +`; + +const Item = styled.div` + display: block; + flex: auto; +`; + +const IssueLink = styled(Link)` + font-weight: bold; +`; + +const OpenedBy = styled.span` + font-size: small; + margin-top: 1px; + display: flex; + color: whitegray; +`; + +const Comment = styled.div` + white-space: nowrap; +`;
Unknown
์ตœ์ดˆ ๋กœ๋”ฉ์€ ์ž˜ ๋ณด์ด๋Š”๋ฐ, ๋ฌดํ•œ ์Šคํฌ๋กค์‹œ ๋กœ๋”ฉ์ด ์ž˜๋ ค์„œ ๊ทธ๋Ÿฐ๊ฑด์ง€ ์ž˜ ์•ˆ๋ณด์ด๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ๋งž๋‚˜์š”? ์ธํ„ฐ๋„ท ๋А๋ฆฌ๊ฒŒ ๋ณ€๊ฒฝํ•ด๋„ ์•ˆ๋ณด์ด๋„ค์š” ใ… ใ… 
@@ -0,0 +1,15 @@ +import { Outlet } from 'react-router-dom'; +import { styled } from 'styled-components'; + +const Title = styled.h1` + text-align: center; +`; + +export function Header() { + return ( + <> + <Title>facebook/react</Title> + <Outlet /> + </> + ); +}
Unknown
์ปดํฌ๋„ŒํŠธ๋ช…๊ณผ ํŒŒ์ผ๋ช…์ด ์ผ์น˜ํ•˜๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ๋˜ ๊ฐ•์‚ฌ๋‹˜์ด facebook/react๋ฅผ ํ•˜๋“œ์ฝ”๋”ฉ ์•ˆํ•˜๋Š” ๋ฐฉ์‹์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ํ•˜์…”์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋„ ์ƒ๊ฐํ•ด๋ณด์‹œ๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™๋„ค์š” :)
@@ -0,0 +1,41 @@ +import React from 'react'; +import { useIssue } from '../hooks/useIssue'; +import { useParams } from 'react-router-dom'; +import { styled } from 'styled-components'; +import MarkdownViewer from '../components/MarkDownViewer'; + +function Detail() { + const { issue, fetchIssue, isLoading } = useIssue(); + const { id } = useParams(); + + React.useEffect(() => { + if (id) { + const parsedId = parseInt(id); + fetchIssue(parsedId); + } + }, []); + + if (isLoading) return <div>Loading...</div>; + return ( + <> + <Title> + <bdi>{issue?.title}</bdi> + <span>#{issue?.number}</span> + </Title> + <br /> + <> + <MarkdownViewer markdown={issue?.body} /> + </> + </> + ); +} + +export default Detail; + +const Title = styled.h1` + span { + color: gray; + font-size: 0.7em; + margin-left: 5px; + } +`;
Unknown
๊ณผ์ œ ํŽ˜์ด์ง€์—์„œ ์ด์Šˆ ์ƒ์„ธ ํ™”๋ฉด์ชฝ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋‚ด์šฉ์„ ๋ณด๋ฉด `์ด์Šˆ๋ฒˆํ˜ธ, ์ด์Šˆ์ œ๋ชฉ, ์ž‘์„ฑ์ž, ์ž‘์„ฑ์ผ, ์ฝ”๋ฉ˜ํŠธ ์ˆ˜, ์ž‘์„ฑ์ž ํ”„๋กœํ•„ ์ด๋ฏธ์ง€, ๋ณธ๋ฌธ ํ‘œ์‹œ` ๋ผ๊ณ  ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. issue์ •๋ณด๋“ค์„ ๋” ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜์ง€ ์•Š์„๊นŒ ์‹ถ์–ด์š”
@@ -0,0 +1,41 @@ +import React from 'react'; +import { useIssue } from '../hooks/useIssue'; +import { useParams } from 'react-router-dom'; +import { styled } from 'styled-components'; +import MarkdownViewer from '../components/MarkDownViewer'; + +function Detail() { + const { issue, fetchIssue, isLoading } = useIssue(); + const { id } = useParams(); + + React.useEffect(() => { + if (id) { + const parsedId = parseInt(id); + fetchIssue(parsedId); + } + }, []); + + if (isLoading) return <div>Loading...</div>; + return ( + <> + <Title> + <bdi>{issue?.title}</bdi> + <span>#{issue?.number}</span> + </Title> + <br /> + <> + <MarkdownViewer markdown={issue?.body} /> + </> + </> + ); +} + +export default Detail; + +const Title = styled.h1` + span { + color: gray; + font-size: 0.7em; + margin-left: 5px; + } +`;
Unknown
์•„.. ๊ทธ๋Ÿฌ๋„ค์š” ์ œ๊ฐ€ ์ € ๋ถ€๋ถ„์„ ๋ชป๋ดค๋˜๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,21 @@ +import styled from "styled-components"; +import { flexCenter } from "@/styles/common"; +import { Product } from "@/types/products"; +import ItemCartMemo from "@/components/ItemCard"; + +const ItemCardList = ({ products }: { products: Product[] }) => { + return ( + <ItemCardWrapper> + {products && products.map((product) => <ItemCartMemo key={`${product.id}`} product={product} />)} + </ItemCardWrapper> + ); +}; + +export default ItemCardList; + +const ItemCardWrapper = styled.div` + display: flex; + gap: 14px; + flex-wrap: wrap; + ${flexCenter} +`;
Unknown
key ๊ฐ’์„ ์ด๋ ‡๊ฒŒ ๋žœ๋ค์œผ๋กœ ๊ด€๋ฆฌ๋ฅผ ํ•˜์…จ๊ตฐ์š” ! ์ €๋„ key ๊ฐ’์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์ด ์žˆ์—ˆ๋Š”๋ฐ ๋ณดํ†ต ์ด ๋ฐฉ๋ฒ•์ด ๋ณดํŽธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์ธ์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜‰
@@ -0,0 +1,21 @@ +import styled from "styled-components"; +import { flexCenter } from "@/styles/common"; +import { Product } from "@/types/products"; +import ItemCartMemo from "@/components/ItemCard"; + +const ItemCardList = ({ products }: { products: Product[] }) => { + return ( + <ItemCardWrapper> + {products && products.map((product) => <ItemCartMemo key={`${product.id}`} product={product} />)} + </ItemCardWrapper> + ); +}; + +export default ItemCardList; + +const ItemCardWrapper = styled.div` + display: flex; + gap: 14px; + flex-wrap: wrap; + ${flexCenter} +`;
Unknown
๋ฆฌ๋ทฐ์–ด์—๊ฒŒ ๋ฌผ์–ด๋ดค๋”๋‹ˆ UUID๋ฅผ ์ถ”์ฒœํ•˜์‹œ๋”๋ผ๊ตฌ์š”! ์ฌ๋ฐ์ด์ฒ˜๋Ÿผ 'product-id' ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋™์ ์ธ id๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๊ฒƒ๋„ ์ •๋ง ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์„œ ๊ทธ๋ ‡๊ฒŒ ๋ฐ”๊ฟ”๋ณด๋ ค๊ตฌ์š”! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,31 @@ +import { + ModalBody, + ModalButtonContainer, + ModalCloseButton, + ModalContainer, + ModalDescription, + ModalDimmedLayer, + ModalHeader, + ModalInputField, + ModalTitle, +} from './index'; + +interface ModalProp { + children: JSX.Element; +} + +const Modal = ({ children }: ModalProp) => { + return <>{children}</>; +}; + +Modal.Body = ModalBody; +Modal.ButtonContainer = ModalButtonContainer; +Modal.CloseButton = ModalCloseButton; +Modal.Container = ModalContainer; +Modal.Description = ModalDescription; +Modal.DimmedLayer = ModalDimmedLayer; +Modal.Header = ModalHeader; +Modal.InputField = ModalInputField; +Modal.Title = ModalTitle; + +export default Modal;
Unknown
ํ˜น์‹œ ์ด๋ฒˆ ๋ฏธ์…˜์—์„œ Compound Component ์‚ฌ์šฉ์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์ด ์žˆ์œผ์…จ์„๊นŒ์š”? ์ง€๊ธˆ Modal์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ณด๋ฉด, ๋ชจ๋“  ์˜ต์…˜๋“ค์ด prop์œผ๋กœ ๋“ค์–ด์˜ค๊ณ  ์žˆ์–ด์„œ ๊ด€์‹ฌ์‚ฌ ๋ณ„๋กœ ๋‚˜๋ˆ„์–ด ํ™•์ธํ•˜๋Š” ๋ฐ ์˜ค๋ž˜๊ฑธ๋ฆฌ๋Š” ๊ฒƒ ๊ฐ™์•„์š”..! ์˜ˆ๋ฅผ ๋“ค๋ฉด, ๋ชจ๋‹ฌ์˜ ์™ธ์ ์ธ ์š”์†Œ์ธ size, position์„ ๋ฌถ์–ด์„œ ์ƒ๊ฐํ•ด์•ผ ๋  ๋•Œ ๊ทธ ๋‘˜์„ ํ•œ ๋ˆˆ์— ํŒŒ์•…ํ•˜๊ธฐ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๋งŒ์•ฝ props์œผ๋กœ ๊ด€๋ฆฌํ•œ๋‹ค๋ฉด, ๊ด€์‹ฌ์‚ฌ ๋ณ„๋กœ ์กฐ๊ธˆ ๋ญ‰์ณ๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? ์•„๋ž˜ ์˜ˆ์‹œ ์ฝ”๋“œ ์ฒ˜๋Ÿผ์š”! ```typescript interface ModalProps { isOpened: boolean; size?: ModalSize; modalPosition?: ModalPosition; title?: string; showCloseButton?: boolean; description?: string; buttonPosition?: ButtonPosition; primaryButton?: ButtonProps; secondaryButton?: ButtonProps; primaryColor?: string; children?: JSX.Element; onClose: () => void; } ```
@@ -0,0 +1,31 @@ +import { + ModalBody, + ModalButtonContainer, + ModalCloseButton, + ModalContainer, + ModalDescription, + ModalDimmedLayer, + ModalHeader, + ModalInputField, + ModalTitle, +} from './index'; + +interface ModalProp { + children: JSX.Element; +} + +const Modal = ({ children }: ModalProp) => { + return <>{children}</>; +}; + +Modal.Body = ModalBody; +Modal.ButtonContainer = ModalButtonContainer; +Modal.CloseButton = ModalCloseButton; +Modal.Container = ModalContainer; +Modal.Description = ModalDescription; +Modal.DimmedLayer = ModalDimmedLayer; +Modal.Header = ModalHeader; +Modal.InputField = ModalInputField; +Modal.Title = ModalTitle; + +export default Modal;
Unknown
์˜ค.. ๋ฐ”๋‹ค๊ฐ€ ๊ณต์œ ํ•ด ์ค€ createPortal์„ ์‚ฌ์šฉํ•˜์…จ๊ตฐ์š”! createPortal์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์–ป์€ ์ธ์‚ฌ์ดํŠธ๋‚˜ ๋А๋‚€ ๋ฐ”๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ณต์œ ํ•ด์ฃผ์‹œ๋ฉด ์ •๋ง ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”~~~ ๋˜, Modal ์ปดํฌ๋„ŒํŠธ์˜ UI ๋กœ์ง์„ ์ •๋ง ์ž˜ ๋‚˜๋ˆ ์„œ ํ•œ ๋ˆˆ์— ํ™• ๋ณด์—ฌ์„œ ์ข‹์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,31 @@ +import { + ModalBody, + ModalButtonContainer, + ModalCloseButton, + ModalContainer, + ModalDescription, + ModalDimmedLayer, + ModalHeader, + ModalInputField, + ModalTitle, +} from './index'; + +interface ModalProp { + children: JSX.Element; +} + +const Modal = ({ children }: ModalProp) => { + return <>{children}</>; +}; + +Modal.Body = ModalBody; +Modal.ButtonContainer = ModalButtonContainer; +Modal.CloseButton = ModalCloseButton; +Modal.Container = ModalContainer; +Modal.Description = ModalDescription; +Modal.DimmedLayer = ModalDimmedLayer; +Modal.Header = ModalHeader; +Modal.InputField = ModalInputField; +Modal.Title = ModalTitle; + +export default Modal;
Unknown
๋ชจ๋‹ฌ ๋‚ด๋ถ€์˜ ๊ฐ ์„น์…˜์„ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์žฌ์‚ฌ์šฉ ํ•œ ์ ์ด ์ธ์ƒ์ ์ž…๋‹ˆ๋‹ค! ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž˜ ๋‚˜๋ˆ„์–ด์„œ Alert, Confirm, Prompt์™€ ๊ฐ™์€ ์ถ”๊ฐ€์ ์ธ ์š”๊ตฌ์‚ฌํ•ญ์ด ์™€๋„ ํฐ ๋ฌธ์ œ์—†์ด ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”!!
@@ -0,0 +1,31 @@ +import { + ModalBody, + ModalButtonContainer, + ModalCloseButton, + ModalContainer, + ModalDescription, + ModalDimmedLayer, + ModalHeader, + ModalInputField, + ModalTitle, +} from './index'; + +interface ModalProp { + children: JSX.Element; +} + +const Modal = ({ children }: ModalProp) => { + return <>{children}</>; +}; + +Modal.Body = ModalBody; +Modal.ButtonContainer = ModalButtonContainer; +Modal.CloseButton = ModalCloseButton; +Modal.Container = ModalContainer; +Modal.Description = ModalDescription; +Modal.DimmedLayer = ModalDimmedLayer; +Modal.Header = ModalHeader; +Modal.InputField = ModalInputField; +Modal.Title = ModalTitle; + +export default Modal;
Unknown
๋„ค ๋งž์•„์š”! Compound Component๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์ฃผ์–ด์ง„ alert, comfirm, prompt, ๊ทธ๋ฆฌ๊ณ  ์ถ”ํ›„ ์ถ”๊ฐ€์ ์œผ๋กœ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์š”๊ตฌ์‚ฌํ•ญ modal type์— ๋งž์ถฐ์„œ ์†์‰ฝ๊ฒŒ ๋Œ€์‘์ด ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค! ๋‹ค๋งŒ, compound component์— ์ง€์‹์ด ๋ถ€์กฑํ–ˆ๊ณ  ์—ฌ๊ธฐ์— ์Ÿ์„ ์‹œ๊ฐ„์ด ๋ถ€์กฑํ•ด์„œ compound component์˜ ์žฅ์ ์„ ์ œ๋Œ€๋กœ ์‚ด๋ฆฌ์ง€ ๋ชปํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋„ค์š”! interface๋ฅผ ์ž‘์„ฑํ•˜๊ณ  docs๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์ € ์Šค์Šค๋กœ๋„ ์ˆœ์„œ๊ฐ€ ํ—ท๊ฐˆ๋ฆฌ๊ณ  ์–ด๋–ป๊ฒŒ ์ •๋ ฌํ•ด์•ผ ์ข‹์„์ง€ ๊ณ ๋ฏผ์ด์—ˆ๋Š”๋ฐ ๋ง์”€ํ•ด ์ฃผ์‹  ๋Œ€๋กœ ๊ด€์‹ฌ์‚ฌ ๋ณ„๋กœ ๋ญ‰์ณ์„œ ์ž‘์„ฑํ•˜๋Š”๊ฒŒ ์ข‹์€ ๋ฐฉ๋ฒ•์ด ๋  ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,31 @@ +import { + ModalBody, + ModalButtonContainer, + ModalCloseButton, + ModalContainer, + ModalDescription, + ModalDimmedLayer, + ModalHeader, + ModalInputField, + ModalTitle, +} from './index'; + +interface ModalProp { + children: JSX.Element; +} + +const Modal = ({ children }: ModalProp) => { + return <>{children}</>; +}; + +Modal.Body = ModalBody; +Modal.ButtonContainer = ModalButtonContainer; +Modal.CloseButton = ModalCloseButton; +Modal.Container = ModalContainer; +Modal.Description = ModalDescription; +Modal.DimmedLayer = ModalDimmedLayer; +Modal.Header = ModalHeader; +Modal.InputField = ModalInputField; +Modal.Title = ModalTitle; + +export default Modal;
Unknown
์šฐ์„  createPortal์„ ์ด์šฉํ•ด์„œ document.body๋กœ ์œ„์น˜๋ฅผ ์˜ฎ๊ฒจ์ฃผ๋‹ค ๋ณด๋‹ˆ ๋‹ค๋ฅธ stack context๊ฐ€ ๋˜์–ด์„œ z-Index ๊ด€๋ฆฌ์— ์œ ์šฉํ•˜๋‹ค๋Š” ์ ์ด์—ˆ์–ด์š”! ๋งŒ์•ฝ ์‚ฌ์šฉํ•˜๋Š” ์œ ์ €๊ฐ€ ```xml <body> <app> <Modal /> <OtherComponent /> </app> </body> ``` ์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ์ €ํฌ๊ฐ€ ์ œ๊ณตํ•˜๋Š” modal์˜ zindex๊ฐ€ 300 ์ด๊ณ , use์˜ OtherComponent์˜ zindex๊ฐ€ 500์ด๋ผ๋ฉด ์ด ๋‘˜์€ ๊ฐ™์€ stack context์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, modal ์œ„๋กœ OtherComponent๊ฐ€ ์˜ฌ๋ผ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ ์ด ์žˆ๊ฒ ์ฃ ! ์‹ค์ œ๋กœ step1์—์„œ ์ด๋Ÿฐ ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›์•˜๊ณ , step1์˜ ๋ฆฌ๋ทฐ ๋ฐ˜์˜์—์„œ๋Š” Modal์— zindex๋ฅผ prop์œผ๋กœ ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ–ˆ์—ˆ์–ด์š”. ํ•˜์ง€๋งŒ ๊ฒฐ๊ตญ ์‚ฌ์šฉ์ž๋Š” component๋“ค๊ณผ modal์˜ zindex๋“ค์— ๋Œ€ํ•ด์„œ ๋งŽ์€ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•˜๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด component๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๊ณ  ํ•  ๋•Œ๋„ ์ด๋ฅผ ๊ณ ๋ฏผํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์•˜์–ด์š”. ```xml <body> <app> <OtherComponent /> </app> <Modal /> </body> ``` ํ•˜์ง€๋งŒ createPortal์„ ์ด์šฉํ•ด document.body๋กœ ๋ฌผ๋ฆฌ์ ์ธ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด, Modal์€ app๊ณผ ๊ฐ™์€ stack context ์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, app์˜ zindex๋ณด๋‹ค ๋†’๊ธฐ๋งŒ ํ•˜๋ฉด app ๋‚ด๋ถ€์˜ ์–ด๋–ค component๋ณด๋‹ค๋„ ์œ„์— ์œ„์น˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์–ด์„œ ์ด๋ถ€๋ถ„์ด ์ข‹์•˜๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,31 @@ +import { + ModalBody, + ModalButtonContainer, + ModalCloseButton, + ModalContainer, + ModalDescription, + ModalDimmedLayer, + ModalHeader, + ModalInputField, + ModalTitle, +} from './index'; + +interface ModalProp { + children: JSX.Element; +} + +const Modal = ({ children }: ModalProp) => { + return <>{children}</>; +}; + +Modal.Body = ModalBody; +Modal.ButtonContainer = ModalButtonContainer; +Modal.CloseButton = ModalCloseButton; +Modal.Container = ModalContainer; +Modal.Description = ModalDescription; +Modal.DimmedLayer = ModalDimmedLayer; +Modal.Header = ModalHeader; +Modal.InputField = ModalInputField; +Modal.Title = ModalTitle; + +export default Modal;
Unknown
์ด๋ถ€๋ถ„์„ ์œ„ํ•ด์„œ compound component๋ฅผ ์ ์šฉํ•˜๋ ค๊ณ  ํ–ˆ๊ณ , ์žฅ์ ์„ ์ž˜ ๋ชป์‚ด๋ ธ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์ˆ˜์•ผ๊ฐ€ ์ œ ์˜๋„๋ฅผ ์™„๋ฒฝํžˆ ํŒŒ์•…ํ•˜์‹ ๊ฑธ ๋ณด๋‹ˆ, ๊ทธ๋ ‡์ง€๋งŒ์€ ์•Š์•˜๋˜ ๊ฒƒ ๊ฐ™๋„ค์š”! ๐Ÿ˜ƒ
@@ -0,0 +1,31 @@ +import { + ModalBody, + ModalButtonContainer, + ModalCloseButton, + ModalContainer, + ModalDescription, + ModalDimmedLayer, + ModalHeader, + ModalInputField, + ModalTitle, +} from './index'; + +interface ModalProp { + children: JSX.Element; +} + +const Modal = ({ children }: ModalProp) => { + return <>{children}</>; +}; + +Modal.Body = ModalBody; +Modal.ButtonContainer = ModalButtonContainer; +Modal.CloseButton = ModalCloseButton; +Modal.Container = ModalContainer; +Modal.Description = ModalDescription; +Modal.DimmedLayer = ModalDimmedLayer; +Modal.Header = ModalHeader; +Modal.InputField = ModalInputField; +Modal.Title = ModalTitle; + +export default Modal;
Unknown
์ƒ์„ธํ•œ ๊ณต์œ  ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!! ์‚ฌ์šฉํ•ด๋ณด์ง„ ์•Š์•˜์ง€๋งŒ, ๋•๋ถ„์— ์–ด๋–ค ์œ ์šฉํ•จ์ด ์žˆ๋Š” ์ง€๋Š” ํ™•์‹คํžˆ ์•Œ๊ฒ ๋„ค์š”!!
@@ -0,0 +1,31 @@ +import { + ModalBody, + ModalButtonContainer, + ModalCloseButton, + ModalContainer, + ModalDescription, + ModalDimmedLayer, + ModalHeader, + ModalInputField, + ModalTitle, +} from './index'; + +interface ModalProp { + children: JSX.Element; +} + +const Modal = ({ children }: ModalProp) => { + return <>{children}</>; +}; + +Modal.Body = ModalBody; +Modal.ButtonContainer = ModalButtonContainer; +Modal.CloseButton = ModalCloseButton; +Modal.Container = ModalContainer; +Modal.Description = ModalDescription; +Modal.DimmedLayer = ModalDimmedLayer; +Modal.Header = ModalHeader; +Modal.InputField = ModalInputField; +Modal.Title = ModalTitle; + +export default Modal;
Unknown
compound component ์ ์šฉ๋งŒ ์•ˆํ•˜์…จ์ง€, ๊ฑฐ์ง„ ์ ์šฉ๋œ ๊ฑฐ๋‚˜ ๋‹ค๋ฆ„ ์—†์„ ์ •๋„๋กœ ์™„๋ฒฝํ•œ ๋ถ„๋ฆฌ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ใ…‹ใ…‹ใ…‹ ์ ์šฉํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์ˆ˜์ • ๋ณ„๋กœ ์—†์ด ๊ธˆ๋ฐฉ ์ ์šฉ๋  ๊ฒƒ ๊ฐ™๋„ค์š”.
@@ -0,0 +1,25 @@ +package oncall.exception; + +public enum ExceptionMessage { + INVALID_MONTH_DAY_INPUT("์›”๊ณผ ์š”์ผ์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."), + INVALID_MONTH_INPUT("์›”์€ ์ˆซ์ž๋งŒ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."), + INVALID_DAY_INPUT("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ์š”์ผ์ž…๋‹ˆ๋‹ค."), + INVALID_DAY_OF_MONTH("ํ•ด๋‹น ์›”์— ์‹œ์ž‘ ์š”์ผ์ด ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."), + INVALID_WORKER_COUNTS("๋น„์ƒ ๊ทผ๋ฌด ์‚ฌ์›์„ ์ตœ์†Œ 5๋ช… ์ด์ƒ ์ตœ๋Œ€ 35๋ช… ์‚ฌ์ด๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."), + INVALID_NICKNAME_LENGTH("์‚ฌ์›์˜ ๋‹‰๋„ค์ž„์€ ์ตœ๋Œ€ 5๊ธ€์ž๊นŒ์ง€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."), + INVALID_NICKNAME_CHARACTER("์‚ฌ์›์˜ ๋‹‰๋„ค์ž„์€ ํ•œ๊ธ€๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."), + DUPLICATED_NICKNAME("ํ•œ ๊ทผ๋ฌด์— ์‚ฌ์›์ด ์ค‘๋ณต๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."), + WORKER_NOT_MATCHED("ํ‰์ผ ์‚ฌ์›๊ณผ ํœด์ผ ์‚ฌ์›์ด ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."), + ; + + private static final String PREFIX = "[ERROR] "; + private final String message; + + ExceptionMessage(String message) { + this.message = PREFIX + message; + } + + public String getMessage() { + return message; + } +}
Java
`mesasge`๋ผ๋Š” ๋ณ€์ˆ˜๋ช… ์ž์ฒด๋Š” ์•ˆ๋‚ด ๋ฌธ๊ตฌ๊ฐ€ ๋” ์ ํ•ฉํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. `getMessage()`๋ฅผ ๋ถ€๋ฅผ ๋•Œ `PREFIX`๋ฅผ ๋”ํ•ด ์ฃผ๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,29 @@ +package oncall.domain; + +import java.time.LocalDate; +import java.util.Arrays; + +public enum Holiday { + NEW_YEAR(LocalDate.of(2023, 1, 1), "์‹ ์ •"), + MARCH_FIRST(LocalDate.of(2023, 3, 1), "์‚ผ์ผ์ ˆ"), + CHILDREN_DAY(LocalDate.of(2023, 5, 5), "์–ด๋ฆฐ์ด๋‚ "), + MEMORIAL_DAY(LocalDate.of(2023, 6, 6), "ํ˜„์ถฉ์ผ"), + LIBERATION_DAY(LocalDate.of(2023, 8, 15), "๊ด‘๋ณต์ ˆ"), + TEN_THIRD(LocalDate.of(2023, 10, 3), "๊ฐœ์ฒœ์ ˆ"), + KOREAN_DAY(LocalDate.of(2023, 10, 9), "ํ•œ๊ธ€๋‚ "), + CHRISTMAS_DAY(LocalDate.of(2023, 12, 25), "์„ฑํƒ„์ ˆ"), + ; + + private final LocalDate date; + private final String name; + + Holiday(LocalDate date, String name) { + this.date = date; + this.name = name; + } + + public static boolean isHoliday(LocalDate localDate) { + return Arrays.stream(values()) + .anyMatch(holiday -> holiday.date.equals(localDate)); + } +}
Java
๊ณตํœด์ผ์„ Enum์œผ๋กœ ๊ด€๋ฆฌํ•˜๋‹ˆ ํ›จ์”ฌ ๊น”๋”ํ•˜๋„ค์š”!
@@ -0,0 +1,16 @@ +package oncall.domain; + +import java.util.Collections; +import java.util.List; + +public class EmergencyWorkers { + private final List<EmergencyWorker> emergencyWorkers; + + public EmergencyWorkers(List<EmergencyWorker> emergencyWorkers) { + this.emergencyWorkers = emergencyWorkers; + } + + public List<EmergencyWorker> getEmergencyWorkers() { + return Collections.unmodifiableList(emergencyWorkers); + } +}
Java
๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋”๋ผ๋„, ๊ทธ ์ž์ฒด๋ฅผ ๋‚ด์ฃผ๋Š” ๊ฒƒ์€ ์ง€์–‘ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น `List`๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ€์„œ ์–ด๋–ค ์—ฐ์‚ฐ์„ ํ•˜๋Š”์ง€๋ฅผ ํŒŒ์•…ํ•œ ๋’ค์—, ๊ทธ ์—ฐ์‚ฐ์„ ์ง€์›ํ•˜๋„๋ก ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -1,7 +1,11 @@ package oncall; +import oncall.view.InputView; +import oncall.view.OutputView; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + EmergencyWorkPlanner emergencyWorkPlanner = new EmergencyWorkPlanner(new InputView(), new OutputView()); + emergencyWorkPlanner.run(); } }
Java
๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด์„œ ๊ฐ๊ฐ์˜ `View` ์„ ์–ธ์„ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ํ•ด์ค˜๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,47 @@ +package oncall.view; + +import static oncall.view.OutputView.OutputMessage.DATE_WORKER_FORMAT; +import static oncall.view.OutputView.OutputMessage.HOLIDAY; + +import java.time.LocalDate; +import oncall.domain.Day; +import oncall.domain.EmergencyWorkers; +import oncall.domain.Holiday; + +public class OutputView { + + public void printEmergencyWork(EmergencyWorkers emergencyWorkers) { + emergencyWorkers.getEmergencyWorkers().forEach(worker -> { + LocalDate localDate = worker.getLocalDate(); + String name = worker.getName(); + String dayOfWeek = Day.of(localDate.getDayOfWeek()); + if (Holiday.isHoliday(localDate)) { + dayOfWeek += HOLIDAY; + } + printForm(localDate, dayOfWeek, name); + }); + } + + private static void printForm(LocalDate localDate, String dayOfWeek, String name) { + System.out.println( + String.format(DATE_WORKER_FORMAT.getMessage(), localDate.getMonth().getValue(), + localDate.getDayOfMonth(), + dayOfWeek, name)); + } + + protected enum OutputMessage { + DATE_WORKER_FORMAT("%d์›” %d์ผ %s %s"), + HOLIDAY("(ํœด์ผ)"), + ; + + private final String message; + + OutputMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + } +}
Java
ํ•œ ๋ฒˆ์— ํ•˜๋Š” ์ผ์ด ๋„ˆ๋ฌด ๋งŽ์€ ๊ฒƒ ๊ฐ™์•„์š”. ๋ฉ”์„œ๋“œ๋ฅผ ๋ถ„๋ฆฌํ•ด์„œ ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข€ ๋” ๊ฐ„๋žตํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,154 @@ +package oncall.view; + +import static oncall.exception.ExceptionMessage.DUPLICATED_NICKNAME; +import static oncall.exception.ExceptionMessage.INVALID_DAY_OF_MONTH; +import static oncall.exception.ExceptionMessage.INVALID_MONTH_DAY_INPUT; +import static oncall.exception.ExceptionMessage.INVALID_MONTH_INPUT; +import static oncall.exception.ExceptionMessage.INVALID_NICKNAME_CHARACTER; +import static oncall.exception.ExceptionMessage.INVALID_NICKNAME_LENGTH; +import static oncall.exception.ExceptionMessage.INVALID_WORKER_COUNTS; +import static oncall.exception.ExceptionMessage.WORKER_NOT_MATCHED; +import static oncall.view.InputView.InputMessage.INPUT_MONTH_DAY; +import static oncall.view.InputView.InputMessage.INPUT_WEEKDAY_EMERGENCY_WORKERS; +import static oncall.view.InputView.InputMessage.INPUT_WEEKEND_EMERGENCY_WORKERS; + +import camp.nextstep.edu.missionutils.Console; +import java.time.LocalDate; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import oncall.domain.Day; +import oncall.domain.Worker; + +public class InputView { + private static final String COMMA = ","; + private static final int DEFAULT_YEAR = 2023; + private static final int DEFAULT_FIRST_DAY = 1; + private static final int MONTH_DAY_INPUT_EXACT_SIZE = 2; + private static final int MONTH_INDEX = 0; + private static final int MIN_MONTH = 1; + private static final int MAX_MONTH = 12; + private static final int MIN_WORKER_SIZE = 5; + private static final int MAX_WORKER_SIZE = 35; + private static final int MIN_WORKER_NICKNAME_LENGTH = 2; + private static final int MAX_WORKER_NICKNAME_LENGTH = 5; + private static final char MIN_KOREAN_CHARACTER = '๊ฐ€'; + private static final char MAX_KOREAN_CHARACTER = 'ํžฃ'; + + public LocalDate readEmergencyWorkMonthDay() { + System.out.print(INPUT_MONTH_DAY.getMessage()); + String input = Console.readLine(); + List<String> parsedMonthDay = parseByComma(input); + validateMonthDayForm(parsedMonthDay); + int month = parseMonth(parsedMonthDay); + validateMonth(month); + Day day = Day.of(parsedMonthDay.get(DEFAULT_FIRST_DAY)); + return validateDay(month, day); + } + + private static List<String> parseByComma(String input) { + return Arrays.asList(input.split(COMMA)); + } + + private static LocalDate validateDay(int month, Day day) { + LocalDate localDate = LocalDate.of(DEFAULT_YEAR, month, DEFAULT_FIRST_DAY); + validateDayOfMonth(day, localDate); + return localDate; + } + + private static void validateMonthDayForm(List<String> parsedMonthDay) { + if (parsedMonthDay.size() != MONTH_DAY_INPUT_EXACT_SIZE) { + throw new IllegalArgumentException(INVALID_MONTH_DAY_INPUT.getMessage()); + } + } + + private static int parseMonth(List<String> parsedMonthDay) { + try { + return Integer.parseInt(parsedMonthDay.get(MONTH_INDEX)); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(INVALID_MONTH_INPUT.getMessage()); + } + } + + private static void validateMonth(int month) { + if (month < MIN_MONTH || MAX_MONTH < month) { + throw new IllegalArgumentException(INVALID_MONTH_INPUT.getMessage()); + } + } + + private static void validateDayOfMonth(Day day, LocalDate localDate) { + if (!day.isSame(localDate.getDayOfWeek())) { + throw new IllegalArgumentException(INVALID_DAY_OF_MONTH.getMessage()); + } + } + + public Worker readWorker() { + System.out.print(INPUT_WEEKDAY_EMERGENCY_WORKERS.getMessage()); + List<String> weekdayWorkers = getWorkers(); + System.out.print(INPUT_WEEKEND_EMERGENCY_WORKERS.getMessage()); + List<String> holidayWorkers = getWorkers(); + validateWeekdayAndHolidayWorkers(weekdayWorkers, holidayWorkers); + return new Worker(weekdayWorkers, holidayWorkers); + } + + private static List<String> getWorkers() { + String weekdayWorkers = Console.readLine(); + List<String> parsedWorkers = parseByComma(weekdayWorkers); + validateWorkers(parsedWorkers); + return parsedWorkers; + } + + private static void validateWorkers(List<String> workers) { + validateWorkerCounts(workers); + if (isInvalidNickNameLength(workers)) { + throw new IllegalArgumentException(INVALID_NICKNAME_LENGTH.getMessage()); + } + if (isInvalidNickNameCharacter(workers)) { + throw new IllegalArgumentException(INVALID_NICKNAME_CHARACTER.getMessage()); + } + if (new HashSet<>(workers).size() != workers.size()) { + throw new IllegalArgumentException(DUPLICATED_NICKNAME.getMessage()); + } + } + + private static void validateWorkerCounts(List<String> parsedWeekdayWorkers) { + int workerSize = parsedWeekdayWorkers.size(); + if (workerSize < MIN_WORKER_SIZE || MAX_WORKER_SIZE < workerSize) { + throw new IllegalArgumentException(INVALID_WORKER_COUNTS.getMessage()); + } + } + + private static boolean isInvalidNickNameLength(List<String> parsedWeekdayWorkers) { + return parsedWeekdayWorkers.stream() + .anyMatch(nickName -> nickName.length() < MIN_WORKER_NICKNAME_LENGTH + || nickName.length() > MAX_WORKER_NICKNAME_LENGTH); + } + + private static boolean isInvalidNickNameCharacter(List<String> parsedWeekdayWorkers) { + return parsedWeekdayWorkers.stream() + .anyMatch(nickName -> nickName.chars() + .anyMatch(character -> character < MIN_KOREAN_CHARACTER || MAX_KOREAN_CHARACTER < character)); + } + + private static void validateWeekdayAndHolidayWorkers(List<String> weekdayWorkers, List<String> holidayWorkers) { + if (!new HashSet<>(weekdayWorkers).containsAll(holidayWorkers)) { + throw new IllegalArgumentException(WORKER_NOT_MATCHED.getMessage()); + } + } + + protected enum InputMessage { + INPUT_MONTH_DAY("๋น„์ƒ ๊ทผ๋ฌด๋ฅผ ๋ฐฐ์ •ํ•  ์›”๊ณผ ์‹œ์ž‘ ์š”์ผ์„ ์ž…๋ ฅํ•˜์„ธ์š”> "), + INPUT_WEEKDAY_EMERGENCY_WORKERS("ํ‰์ผ ๋น„์ƒ ๊ทผ๋ฌด ์ˆœ๋ฒˆ๋Œ€๋กœ ์‚ฌ์› ๋‹‰๋„ค์ž„์„ ์ž…๋ ฅํ•˜์„ธ์š”> "), + INPUT_WEEKEND_EMERGENCY_WORKERS("ํœด์ผ ๋น„์ƒ ๊ทผ๋ฌด ์ˆœ๋ฒˆ๋Œ€๋กœ ์‚ฌ์› ๋‹‰๋„ค์ž„์„ ์ž…๋ ฅํ•˜์„ธ์š”> "); + + private final String message; + + InputMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + } +}
Java
๊ฒ€์ฆ๊ณผ ๊ด€๋ จ๋œ ์ƒ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด `View`์— ์น˜์ค‘๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž˜๋ชป๋œ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ๋„๋ฉ”์ธ ๋‹จ์—์„œ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ํ•œ๋‹ค๋ฉด ๊ฐ์ฒด ์ƒ์„ฑ๋„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์–ด์š”. ๋‚˜์•„๊ฐ€ `View`๊ฐ€ ์ฑ…์ž„์„ ๋œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,56 @@ +package oncall.domain; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.temporal.TemporalAdjusters; +import java.util.ArrayList; +import java.util.List; + +public class Worker { + private static final int INCREASE_DATE = 1; + private static final int OFFSET = 1; + private final List<String> weekdayWorkers; + private final List<String> holidayWorkers; + + public Worker(List<String> weekdayWorkers, List<String> holidayWorkers) { + this.weekdayWorkers = weekdayWorkers; + this.holidayWorkers = holidayWorkers; + } + + public EmergencyWorkers makeEmergencyWork(LocalDate date) { + LocalDate lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth()); + List<EmergencyWorker> emergencyWorkers = new ArrayList<>(); + int weekdayIndex = 0; + int holidayIndex = 0; + while (!date.isAfter(lastDayOfMonth)) { + if (isWeekend(date) || Holiday.isHoliday(date)) { + holidayIndex = assignWorkerAndAdjustIndex(emergencyWorkers, holidayWorkers, holidayIndex, date); + date = date.plusDays(INCREASE_DATE); + continue; + } + weekdayIndex = assignWorkerAndAdjustIndex(emergencyWorkers, weekdayWorkers, weekdayIndex, date); + date = date.plusDays(INCREASE_DATE); + } + return new EmergencyWorkers(emergencyWorkers); + } + + private boolean isWeekend(LocalDate date) { + DayOfWeek dayOfWeek = date.getDayOfWeek(); + return dayOfWeek.equals(DayOfWeek.SATURDAY) || dayOfWeek.equals(DayOfWeek.SUNDAY); + } + + private int assignWorkerAndAdjustIndex(List<EmergencyWorker> emergencyWorkers, List<String> workers, int index, + LocalDate date) { + String worker = workers.get(index); + int nextIndex = (index + OFFSET) % workers.size(); + if (!emergencyWorkers.isEmpty() && emergencyWorkers.get(emergencyWorkers.size() - OFFSET).isSame(worker)) { + String nextWorker = workers.get(nextIndex); + workers.set(index, nextWorker); + workers.set(nextIndex, worker); + worker = nextWorker; + } + emergencyWorkers.add(new EmergencyWorker(worker, date)); + return nextIndex; + } +} +
Java
๋‘ ์กฐ๊ฑด์„ ๋ฌถ์–ด์„œ ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋กœ ๋‚˜ํƒ€๋‚ด๋„ ์ข‹์•˜๊ฒ ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,56 @@ +package oncall.domain; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.temporal.TemporalAdjusters; +import java.util.ArrayList; +import java.util.List; + +public class Worker { + private static final int INCREASE_DATE = 1; + private static final int OFFSET = 1; + private final List<String> weekdayWorkers; + private final List<String> holidayWorkers; + + public Worker(List<String> weekdayWorkers, List<String> holidayWorkers) { + this.weekdayWorkers = weekdayWorkers; + this.holidayWorkers = holidayWorkers; + } + + public EmergencyWorkers makeEmergencyWork(LocalDate date) { + LocalDate lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth()); + List<EmergencyWorker> emergencyWorkers = new ArrayList<>(); + int weekdayIndex = 0; + int holidayIndex = 0; + while (!date.isAfter(lastDayOfMonth)) { + if (isWeekend(date) || Holiday.isHoliday(date)) { + holidayIndex = assignWorkerAndAdjustIndex(emergencyWorkers, holidayWorkers, holidayIndex, date); + date = date.plusDays(INCREASE_DATE); + continue; + } + weekdayIndex = assignWorkerAndAdjustIndex(emergencyWorkers, weekdayWorkers, weekdayIndex, date); + date = date.plusDays(INCREASE_DATE); + } + return new EmergencyWorkers(emergencyWorkers); + } + + private boolean isWeekend(LocalDate date) { + DayOfWeek dayOfWeek = date.getDayOfWeek(); + return dayOfWeek.equals(DayOfWeek.SATURDAY) || dayOfWeek.equals(DayOfWeek.SUNDAY); + } + + private int assignWorkerAndAdjustIndex(List<EmergencyWorker> emergencyWorkers, List<String> workers, int index, + LocalDate date) { + String worker = workers.get(index); + int nextIndex = (index + OFFSET) % workers.size(); + if (!emergencyWorkers.isEmpty() && emergencyWorkers.get(emergencyWorkers.size() - OFFSET).isSame(worker)) { + String nextWorker = workers.get(nextIndex); + workers.set(index, nextWorker); + workers.set(nextIndex, worker); + worker = nextWorker; + } + emergencyWorkers.add(new EmergencyWorker(worker, date)); + return nextIndex; + } +} +
Java
`EmergencyWorker`๋Š” ์ด๊ณณ์—์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋‚˜์š”?
@@ -0,0 +1,16 @@ +package oncall.domain; + +import java.util.Collections; +import java.util.List; + +public class EmergencyWorkers { + private final List<EmergencyWorker> emergencyWorkers; + + public EmergencyWorkers(List<EmergencyWorker> emergencyWorkers) { + this.emergencyWorkers = emergencyWorkers; + } + + public List<EmergencyWorker> getEmergencyWorkers() { + return Collections.unmodifiableList(emergencyWorkers); + } +}
Java
๋„ค ๋งž์Šต๋‹ˆ๋‹ค. DTO๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•œ ๊ฒƒ์ด ์•„์‰ฝ์Šต๋‹ˆ๋‹ค..
@@ -0,0 +1,154 @@ +package oncall.view; + +import static oncall.exception.ExceptionMessage.DUPLICATED_NICKNAME; +import static oncall.exception.ExceptionMessage.INVALID_DAY_OF_MONTH; +import static oncall.exception.ExceptionMessage.INVALID_MONTH_DAY_INPUT; +import static oncall.exception.ExceptionMessage.INVALID_MONTH_INPUT; +import static oncall.exception.ExceptionMessage.INVALID_NICKNAME_CHARACTER; +import static oncall.exception.ExceptionMessage.INVALID_NICKNAME_LENGTH; +import static oncall.exception.ExceptionMessage.INVALID_WORKER_COUNTS; +import static oncall.exception.ExceptionMessage.WORKER_NOT_MATCHED; +import static oncall.view.InputView.InputMessage.INPUT_MONTH_DAY; +import static oncall.view.InputView.InputMessage.INPUT_WEEKDAY_EMERGENCY_WORKERS; +import static oncall.view.InputView.InputMessage.INPUT_WEEKEND_EMERGENCY_WORKERS; + +import camp.nextstep.edu.missionutils.Console; +import java.time.LocalDate; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import oncall.domain.Day; +import oncall.domain.Worker; + +public class InputView { + private static final String COMMA = ","; + private static final int DEFAULT_YEAR = 2023; + private static final int DEFAULT_FIRST_DAY = 1; + private static final int MONTH_DAY_INPUT_EXACT_SIZE = 2; + private static final int MONTH_INDEX = 0; + private static final int MIN_MONTH = 1; + private static final int MAX_MONTH = 12; + private static final int MIN_WORKER_SIZE = 5; + private static final int MAX_WORKER_SIZE = 35; + private static final int MIN_WORKER_NICKNAME_LENGTH = 2; + private static final int MAX_WORKER_NICKNAME_LENGTH = 5; + private static final char MIN_KOREAN_CHARACTER = '๊ฐ€'; + private static final char MAX_KOREAN_CHARACTER = 'ํžฃ'; + + public LocalDate readEmergencyWorkMonthDay() { + System.out.print(INPUT_MONTH_DAY.getMessage()); + String input = Console.readLine(); + List<String> parsedMonthDay = parseByComma(input); + validateMonthDayForm(parsedMonthDay); + int month = parseMonth(parsedMonthDay); + validateMonth(month); + Day day = Day.of(parsedMonthDay.get(DEFAULT_FIRST_DAY)); + return validateDay(month, day); + } + + private static List<String> parseByComma(String input) { + return Arrays.asList(input.split(COMMA)); + } + + private static LocalDate validateDay(int month, Day day) { + LocalDate localDate = LocalDate.of(DEFAULT_YEAR, month, DEFAULT_FIRST_DAY); + validateDayOfMonth(day, localDate); + return localDate; + } + + private static void validateMonthDayForm(List<String> parsedMonthDay) { + if (parsedMonthDay.size() != MONTH_DAY_INPUT_EXACT_SIZE) { + throw new IllegalArgumentException(INVALID_MONTH_DAY_INPUT.getMessage()); + } + } + + private static int parseMonth(List<String> parsedMonthDay) { + try { + return Integer.parseInt(parsedMonthDay.get(MONTH_INDEX)); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(INVALID_MONTH_INPUT.getMessage()); + } + } + + private static void validateMonth(int month) { + if (month < MIN_MONTH || MAX_MONTH < month) { + throw new IllegalArgumentException(INVALID_MONTH_INPUT.getMessage()); + } + } + + private static void validateDayOfMonth(Day day, LocalDate localDate) { + if (!day.isSame(localDate.getDayOfWeek())) { + throw new IllegalArgumentException(INVALID_DAY_OF_MONTH.getMessage()); + } + } + + public Worker readWorker() { + System.out.print(INPUT_WEEKDAY_EMERGENCY_WORKERS.getMessage()); + List<String> weekdayWorkers = getWorkers(); + System.out.print(INPUT_WEEKEND_EMERGENCY_WORKERS.getMessage()); + List<String> holidayWorkers = getWorkers(); + validateWeekdayAndHolidayWorkers(weekdayWorkers, holidayWorkers); + return new Worker(weekdayWorkers, holidayWorkers); + } + + private static List<String> getWorkers() { + String weekdayWorkers = Console.readLine(); + List<String> parsedWorkers = parseByComma(weekdayWorkers); + validateWorkers(parsedWorkers); + return parsedWorkers; + } + + private static void validateWorkers(List<String> workers) { + validateWorkerCounts(workers); + if (isInvalidNickNameLength(workers)) { + throw new IllegalArgumentException(INVALID_NICKNAME_LENGTH.getMessage()); + } + if (isInvalidNickNameCharacter(workers)) { + throw new IllegalArgumentException(INVALID_NICKNAME_CHARACTER.getMessage()); + } + if (new HashSet<>(workers).size() != workers.size()) { + throw new IllegalArgumentException(DUPLICATED_NICKNAME.getMessage()); + } + } + + private static void validateWorkerCounts(List<String> parsedWeekdayWorkers) { + int workerSize = parsedWeekdayWorkers.size(); + if (workerSize < MIN_WORKER_SIZE || MAX_WORKER_SIZE < workerSize) { + throw new IllegalArgumentException(INVALID_WORKER_COUNTS.getMessage()); + } + } + + private static boolean isInvalidNickNameLength(List<String> parsedWeekdayWorkers) { + return parsedWeekdayWorkers.stream() + .anyMatch(nickName -> nickName.length() < MIN_WORKER_NICKNAME_LENGTH + || nickName.length() > MAX_WORKER_NICKNAME_LENGTH); + } + + private static boolean isInvalidNickNameCharacter(List<String> parsedWeekdayWorkers) { + return parsedWeekdayWorkers.stream() + .anyMatch(nickName -> nickName.chars() + .anyMatch(character -> character < MIN_KOREAN_CHARACTER || MAX_KOREAN_CHARACTER < character)); + } + + private static void validateWeekdayAndHolidayWorkers(List<String> weekdayWorkers, List<String> holidayWorkers) { + if (!new HashSet<>(weekdayWorkers).containsAll(holidayWorkers)) { + throw new IllegalArgumentException(WORKER_NOT_MATCHED.getMessage()); + } + } + + protected enum InputMessage { + INPUT_MONTH_DAY("๋น„์ƒ ๊ทผ๋ฌด๋ฅผ ๋ฐฐ์ •ํ•  ์›”๊ณผ ์‹œ์ž‘ ์š”์ผ์„ ์ž…๋ ฅํ•˜์„ธ์š”> "), + INPUT_WEEKDAY_EMERGENCY_WORKERS("ํ‰์ผ ๋น„์ƒ ๊ทผ๋ฌด ์ˆœ๋ฒˆ๋Œ€๋กœ ์‚ฌ์› ๋‹‰๋„ค์ž„์„ ์ž…๋ ฅํ•˜์„ธ์š”> "), + INPUT_WEEKEND_EMERGENCY_WORKERS("ํœด์ผ ๋น„์ƒ ๊ทผ๋ฌด ์ˆœ๋ฒˆ๋Œ€๋กœ ์‚ฌ์› ๋‹‰๋„ค์ž„์„ ์ž…๋ ฅํ•˜์„ธ์š”> "); + + private final String message; + + InputMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + } +}
Java
๋งˆ์ง€๋ง‰์— Parser์™€ Validator๋ฅผ ์ƒ์„ฑํ•ด์„œ ํŒŒ์‹ฑ๊ณผ ๊ฒ€์ฆ์„ ๋ถ„๋ฆฌํ•˜๋ ค๊ณ  ํ•˜์˜€์œผ๋‚˜ ๋ฏธ์ฒ˜ ๋ฆฌํŒฉํ† ๋ง ํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ํ”ผ๋“œ๋ฐฑ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค.
@@ -0,0 +1,56 @@ +package oncall.domain; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.temporal.TemporalAdjusters; +import java.util.ArrayList; +import java.util.List; + +public class Worker { + private static final int INCREASE_DATE = 1; + private static final int OFFSET = 1; + private final List<String> weekdayWorkers; + private final List<String> holidayWorkers; + + public Worker(List<String> weekdayWorkers, List<String> holidayWorkers) { + this.weekdayWorkers = weekdayWorkers; + this.holidayWorkers = holidayWorkers; + } + + public EmergencyWorkers makeEmergencyWork(LocalDate date) { + LocalDate lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth()); + List<EmergencyWorker> emergencyWorkers = new ArrayList<>(); + int weekdayIndex = 0; + int holidayIndex = 0; + while (!date.isAfter(lastDayOfMonth)) { + if (isWeekend(date) || Holiday.isHoliday(date)) { + holidayIndex = assignWorkerAndAdjustIndex(emergencyWorkers, holidayWorkers, holidayIndex, date); + date = date.plusDays(INCREASE_DATE); + continue; + } + weekdayIndex = assignWorkerAndAdjustIndex(emergencyWorkers, weekdayWorkers, weekdayIndex, date); + date = date.plusDays(INCREASE_DATE); + } + return new EmergencyWorkers(emergencyWorkers); + } + + private boolean isWeekend(LocalDate date) { + DayOfWeek dayOfWeek = date.getDayOfWeek(); + return dayOfWeek.equals(DayOfWeek.SATURDAY) || dayOfWeek.equals(DayOfWeek.SUNDAY); + } + + private int assignWorkerAndAdjustIndex(List<EmergencyWorker> emergencyWorkers, List<String> workers, int index, + LocalDate date) { + String worker = workers.get(index); + int nextIndex = (index + OFFSET) % workers.size(); + if (!emergencyWorkers.isEmpty() && emergencyWorkers.get(emergencyWorkers.size() - OFFSET).isSame(worker)) { + String nextWorker = workers.get(nextIndex); + workers.set(index, nextWorker); + workers.set(nextIndex, worker); + worker = nextWorker; + } + emergencyWorkers.add(new EmergencyWorker(worker, date)); + return nextIndex; + } +} +
Java
์‹œํ—˜ ๋‹น์‹œ์—๋Š” EmergencyWorker ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์ „์— ์ €์žฅํ•ด๋‘๊ณ  ๋ณ€ํ™˜ํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ”ผ๋“œ๋ฐฑ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค ๋™ํ›ˆ๋‹˜!
@@ -0,0 +1,154 @@ +package oncall.view; + +import static oncall.exception.ExceptionMessage.DUPLICATED_NICKNAME; +import static oncall.exception.ExceptionMessage.INVALID_DAY_OF_MONTH; +import static oncall.exception.ExceptionMessage.INVALID_MONTH_DAY_INPUT; +import static oncall.exception.ExceptionMessage.INVALID_MONTH_INPUT; +import static oncall.exception.ExceptionMessage.INVALID_NICKNAME_CHARACTER; +import static oncall.exception.ExceptionMessage.INVALID_NICKNAME_LENGTH; +import static oncall.exception.ExceptionMessage.INVALID_WORKER_COUNTS; +import static oncall.exception.ExceptionMessage.WORKER_NOT_MATCHED; +import static oncall.view.InputView.InputMessage.INPUT_MONTH_DAY; +import static oncall.view.InputView.InputMessage.INPUT_WEEKDAY_EMERGENCY_WORKERS; +import static oncall.view.InputView.InputMessage.INPUT_WEEKEND_EMERGENCY_WORKERS; + +import camp.nextstep.edu.missionutils.Console; +import java.time.LocalDate; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import oncall.domain.Day; +import oncall.domain.Worker; + +public class InputView { + private static final String COMMA = ","; + private static final int DEFAULT_YEAR = 2023; + private static final int DEFAULT_FIRST_DAY = 1; + private static final int MONTH_DAY_INPUT_EXACT_SIZE = 2; + private static final int MONTH_INDEX = 0; + private static final int MIN_MONTH = 1; + private static final int MAX_MONTH = 12; + private static final int MIN_WORKER_SIZE = 5; + private static final int MAX_WORKER_SIZE = 35; + private static final int MIN_WORKER_NICKNAME_LENGTH = 2; + private static final int MAX_WORKER_NICKNAME_LENGTH = 5; + private static final char MIN_KOREAN_CHARACTER = '๊ฐ€'; + private static final char MAX_KOREAN_CHARACTER = 'ํžฃ'; + + public LocalDate readEmergencyWorkMonthDay() { + System.out.print(INPUT_MONTH_DAY.getMessage()); + String input = Console.readLine(); + List<String> parsedMonthDay = parseByComma(input); + validateMonthDayForm(parsedMonthDay); + int month = parseMonth(parsedMonthDay); + validateMonth(month); + Day day = Day.of(parsedMonthDay.get(DEFAULT_FIRST_DAY)); + return validateDay(month, day); + } + + private static List<String> parseByComma(String input) { + return Arrays.asList(input.split(COMMA)); + } + + private static LocalDate validateDay(int month, Day day) { + LocalDate localDate = LocalDate.of(DEFAULT_YEAR, month, DEFAULT_FIRST_DAY); + validateDayOfMonth(day, localDate); + return localDate; + } + + private static void validateMonthDayForm(List<String> parsedMonthDay) { + if (parsedMonthDay.size() != MONTH_DAY_INPUT_EXACT_SIZE) { + throw new IllegalArgumentException(INVALID_MONTH_DAY_INPUT.getMessage()); + } + } + + private static int parseMonth(List<String> parsedMonthDay) { + try { + return Integer.parseInt(parsedMonthDay.get(MONTH_INDEX)); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(INVALID_MONTH_INPUT.getMessage()); + } + } + + private static void validateMonth(int month) { + if (month < MIN_MONTH || MAX_MONTH < month) { + throw new IllegalArgumentException(INVALID_MONTH_INPUT.getMessage()); + } + } + + private static void validateDayOfMonth(Day day, LocalDate localDate) { + if (!day.isSame(localDate.getDayOfWeek())) { + throw new IllegalArgumentException(INVALID_DAY_OF_MONTH.getMessage()); + } + } + + public Worker readWorker() { + System.out.print(INPUT_WEEKDAY_EMERGENCY_WORKERS.getMessage()); + List<String> weekdayWorkers = getWorkers(); + System.out.print(INPUT_WEEKEND_EMERGENCY_WORKERS.getMessage()); + List<String> holidayWorkers = getWorkers(); + validateWeekdayAndHolidayWorkers(weekdayWorkers, holidayWorkers); + return new Worker(weekdayWorkers, holidayWorkers); + } + + private static List<String> getWorkers() { + String weekdayWorkers = Console.readLine(); + List<String> parsedWorkers = parseByComma(weekdayWorkers); + validateWorkers(parsedWorkers); + return parsedWorkers; + } + + private static void validateWorkers(List<String> workers) { + validateWorkerCounts(workers); + if (isInvalidNickNameLength(workers)) { + throw new IllegalArgumentException(INVALID_NICKNAME_LENGTH.getMessage()); + } + if (isInvalidNickNameCharacter(workers)) { + throw new IllegalArgumentException(INVALID_NICKNAME_CHARACTER.getMessage()); + } + if (new HashSet<>(workers).size() != workers.size()) { + throw new IllegalArgumentException(DUPLICATED_NICKNAME.getMessage()); + } + } + + private static void validateWorkerCounts(List<String> parsedWeekdayWorkers) { + int workerSize = parsedWeekdayWorkers.size(); + if (workerSize < MIN_WORKER_SIZE || MAX_WORKER_SIZE < workerSize) { + throw new IllegalArgumentException(INVALID_WORKER_COUNTS.getMessage()); + } + } + + private static boolean isInvalidNickNameLength(List<String> parsedWeekdayWorkers) { + return parsedWeekdayWorkers.stream() + .anyMatch(nickName -> nickName.length() < MIN_WORKER_NICKNAME_LENGTH + || nickName.length() > MAX_WORKER_NICKNAME_LENGTH); + } + + private static boolean isInvalidNickNameCharacter(List<String> parsedWeekdayWorkers) { + return parsedWeekdayWorkers.stream() + .anyMatch(nickName -> nickName.chars() + .anyMatch(character -> character < MIN_KOREAN_CHARACTER || MAX_KOREAN_CHARACTER < character)); + } + + private static void validateWeekdayAndHolidayWorkers(List<String> weekdayWorkers, List<String> holidayWorkers) { + if (!new HashSet<>(weekdayWorkers).containsAll(holidayWorkers)) { + throw new IllegalArgumentException(WORKER_NOT_MATCHED.getMessage()); + } + } + + protected enum InputMessage { + INPUT_MONTH_DAY("๋น„์ƒ ๊ทผ๋ฌด๋ฅผ ๋ฐฐ์ •ํ•  ์›”๊ณผ ์‹œ์ž‘ ์š”์ผ์„ ์ž…๋ ฅํ•˜์„ธ์š”> "), + INPUT_WEEKDAY_EMERGENCY_WORKERS("ํ‰์ผ ๋น„์ƒ ๊ทผ๋ฌด ์ˆœ๋ฒˆ๋Œ€๋กœ ์‚ฌ์› ๋‹‰๋„ค์ž„์„ ์ž…๋ ฅํ•˜์„ธ์š”> "), + INPUT_WEEKEND_EMERGENCY_WORKERS("ํœด์ผ ๋น„์ƒ ๊ทผ๋ฌด ์ˆœ๋ฒˆ๋Œ€๋กœ ์‚ฌ์› ๋‹‰๋„ค์ž„์„ ์ž…๋ ฅํ•˜์„ธ์š”> "); + + private final String message; + + InputMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + } +}
Java
InputView๋ฅผ ํ™•์ธํ•ด๋ณด๋‹ˆ validate๊ด€๋ จ ํ•จ์ˆ˜๋„ ์ „๋ถ€ ์ž‘์„ฑ๋˜์–ด์žˆ๋”๋ผ๊ณ ์š” ๊ทธ๋ž˜์„œ ๊ฒ€์ฆํ•˜๋Š” ๋กœ์ง์€ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋” ๊ฐ€๋…์„ฑ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค ์˜ˆ๋ฅผ ๋“ค๋ฉด ๊ฐ ๋„๋ฉ”์ธ์— ๋Œ€ํ•œ validate ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ด€๋ฆฌํ•ด๋„ ๋˜๊ณ ์š”! ํ”„๋กœ๊ทธ๋žจ์ด ์ปค์ง€๋‹ค ๋ณด๋ฉด ๊ฐ ๋ชจ๋ธ์— ๋Œ€ํ•œ ๊ฒ€์ฆ ์š”์†Œ๋“ค์ด ๋” ์ƒ๊ธธ ์ˆ˜ ์žˆ๊ณ  ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด๊ฑฐ ๊ด€๋ จ ํ•จ์ˆ˜๊ฐ€ ๋ญ์˜€์ง€,,ํ•˜๊ณ  ์ฐพ์•„๋ณด๋Š” ์‹œ๊ฐ„๋„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ๊ณ ์š”!
@@ -0,0 +1,72 @@ +# Domain Documentation + +## ๋„๋ฉ”์ธ ๊ณ„์ธต์˜ ํด๋ž˜์Šค(๊ฐ์ฒด) ๊ตฌ์กฐ๋„ + +<img width="609" alt="image" src="https://github.com/woowacourse-precourse/javascript-lotto-6/assets/87177577/24677451-b9f5-4c62-919f-f6dc3342bebf"/> + +๋‹จ, `PromotionResult`์˜ ๊ฒฝ์šฐ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ๊ฒฐ๊ณผ๋ฌผ๋กœ์จ ์กด์žฌํ•˜๋ฉฐ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” `PromotionResultService`์—์„œ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. + +<br/> + +## OrderMenuAmount + +> ์ฃผ๋ฌธ ์ด์•ก๊ณผ ๊ด€๋ จ๋œ ๋ชจ๋“ˆ๋กœ, ํ˜„์žฌ๋Š” ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ์„ค๊ณ„ํ•œ ๋ชจ๋“ˆ + +| ๋ฉ”์„œ๋“œ | ๋งค๊ฐœ ๋ณ€์ˆ˜ | ๋ฐ˜ํ™˜ ๊ฐ’ | ์„ค๋ช… | +| -------------- | --------------- | -------- | -------------------------------------------- | +| calculateTotal | `orderMenuInfo` | `number` | ์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด์˜ ์ด ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | + +<br/> + +## Menu Searcher + +> ๋ฉ”๋‰ด ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ๋ฉ”๋‰ด๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ ๋ชจ๋“ˆ + +| ๋ฉ”์„œ๋“œ | ๋งค๊ฐœ ๋ณ€์ˆ˜ | ๋ฐ˜ํ™˜ ๊ฐ’ | ์„ค๋ช… | +| ---------------- | ------------------------------ | --------------------- | ---------------------------------------------------------------- | +| findByMenuName | `menuName, category(optional)` | `undefined \| object` | ์ฃผ์–ด์ง„ ์นดํ…Œ๊ณ ๋ฆฌ์—์„œ ๋ฉ”๋‰ด ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” ๋ฉ”๋‰ด๋ฅผ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | +| isMenuInCategory | `menuName, category` | `boolean` | ์ฃผ์–ด์ง„ ์นดํ…Œ๊ณ ๋ฆฌ์— ๋ฉ”๋‰ด ์ด๋ฆ„์ด ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | + +<br/> + +## DecemberPromotionPlan + +> 12์›”์˜ ํ”„๋กœ๋ชจ์…˜ ๊ณ„ํš ๋“ค์„(ํฌ๋ฆฌ์Šค๋งˆ์Šค, ์ฃผ๋ง/ํ‰์ผ ํ• ์ธ, ํŠน๋ณ„ ์ด๋ฒคํŠธ ๋ฐ ์ฆ์ • ํ–‰์‚ฌ)ํ†ตํ•ด ํ• ์ธ ํ˜œํƒ ๋“ค์„ ์ œ๊ณต ํ•˜๋Š” ๋ชจ๋“ˆ + +| ๋ฉ”์„œ๋“œ | ๋งค๊ฐœ ๋ณ€์ˆ˜ | ๋ฐ˜ํ™˜ ๊ฐ’ | ์„ค๋ช… | +| ---------------------------- | ------------------------------------------------------------------------- | ------------------------ | --------------------------------------------------------------------------- | +| execute | `OrdererInfo { visitDate, totalOrderAmount, orderMenuInfo }` | `PromotionBenefitResult` | ๊ณ„ํš์„ ์‹คํ–‰ํ•˜์—ฌ ๊ฐ ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ์˜ ๊ธˆ์•ก ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | +| formatVisitDateForPromotion | `visitDate` | `Date` | ๋ฐฉ๋ฌธ ์ผ์ž๋ฅผ Date ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | +| isAvailablePromotion | `{ dateInfo: { formatVisitDate, startDate, endDate }, totalOrderAmount }` | `boolean` | ์ด๋ฒคํŠธ ๊ธฐ๊ฐ„๊ณผ ์ตœ์†Œ ์ฃผ๋ฌธ ๊ธˆ์•ก ์š”๊ฑด์„ ์ถฉ์กฑํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | +| createPromotionBenefitResult | `OrdererInfo` | `PromotionBenefitResult` | ์ฃผ๋ฌธ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ฐ ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ์˜ ๊ธˆ์•ก ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | +| calculateChristmasBenefit | `OrdererInfo` | `number \| 0` | ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. | +| calculateBenefitForDayType | `CalculateBenefitForDayTypeParams` | `number \| 0` | ์š”์ผ(์ฃผ๋ง/ํ‰์ผ) ํ• ์ธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. | +| calculateSpecialBenefit | `{ visitDate: Date }` | `number \| 0` | ํŠน๋ณ„ ํ• ์ธ์ด ์ ์šฉ๋˜๋Š” ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. | +| calculateGiftEvent | `{ totalOrderAmount: number }` | `number \| 0` | ์ฆ์ • ์ด๋ฒคํŠธ ์ ์šฉ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. | + +<br/> + +## PromotionReceipt + +> ์ด ํ˜œํƒ ๊ธˆ์•ก ๋ฐ ์˜ˆ์ƒ ์ง€์ถœ ๊ธˆ์•ก์„ ๋ฌถ์–ด ํ”„๋กœ๋ชจ์…˜ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์˜์ˆ˜์ฆ์„ ํ‘œํ˜„ํ•œ ๋ชจ๋“ˆ + +| ๋ฉ”์„œ๋“œ | ๋งค๊ฐœ ๋ณ€์ˆ˜ | ๋ฐ˜ํ™˜ ๊ฐ’ | ์„ค๋ช… | +| ------------------------ | ------------------------------------------------------------------ | ------------------ | ------------------------------------------------------------------- | +| issue | `{ promotionBenefitResult, totalOrderAmount }` | `PromotionReceipt` | ํ˜œํƒ ์ •๋ณด ๋ฐ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์„ ๋ฐ”ํƒ•์œผ๋กœ ์˜์ˆ˜์ฆ์„ ๋ฐœํ–‰ํ•ฉ๋‹ˆ๋‹ค. | +| calculateBenefitAmount | `PromotionBenefitResult` | `number` | ์ฃผ์–ด์ง„ ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ด ํ˜œํƒ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. | +| calculateExpectedPayment | `{ totalOrderAmount, promotionBenefitResult, totalBenefitAmount }` | `number` | ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก๊ณผ ์ด ํ˜œํƒ ๊ธˆ์•ก์„ ๋ฐ”ํƒ•์œผ๋กœ ์˜ˆ์ƒ ์ง€์ถœ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. | + +<br/> + +## EventBadgeMaker + +> 12์›”์˜ ์ด๋ฒคํŠธ ๋ฑƒ์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋“ˆ + +| ๋ฉ”์„œ๋“œ | ๋งค๊ฐœ ๋ณ€์ˆ˜ | ๋ฐ˜ํ™˜ ๊ฐ’ | ์„ค๋ช… | +| --------------------- | -------------------- | -------------------- | ------------------------------------------------------------------- | +| createByBenefitAmount | `totalBenefitAmount` | `EventBadge \| null` | ์ด ํ˜œํƒ ๊ธˆ์•ก์— ํ•ด๋‹นํ•˜๋Š” ์ด๋ฒคํŠธ ๋ฑƒ์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | +| searchTargetBadges | `totalBenefitAmount` | `Array` | ์ด ํ˜œํƒ ๊ธˆ์•ก์— ํ•ด๋‹น๋˜๋Š” ๋ฑƒ์ง€๊ฐ€ ์žˆ๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | + +<br/> + +`Custom Type` ๋“ค์€ `src/utils/jsDoc.js`์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
Unknown
๊น”๋”ํ•œ documentation ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค :)
@@ -0,0 +1,132 @@ +import { FORMAT_MESSAGE, OUTPUT_MESSAGE } from './module'; + +describe('๋ฉ”์‹œ์ง€ ํฌ๋งท ํ…Œ์ŠคํŠธ', () => { + describe('orderMenus ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: '์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” "ํ”ผ์ž 2๊ฐœ - ์ฝœ๋ผ 1๊ฐœ" ์ด๋‹ค.', + input: [ + ['ํ”ผ์ž', 2], + ['์ฝœ๋ผ', 1], + ], + output: 'ํ”ผ์ž 2๊ฐœ\n์ฝœ๋ผ 1๊ฐœ\n', + }, + ])('$description', ({ input, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.orderMenus(input)).toBe(output); + }); + }); + + describe('amount ํ…Œ์ŠคํŠธ', () => { + test.each([ + { input: 5000, output: '5,000์›' }, + { input: 0, output: '0์›' }, + ])('amount๊ฐ€ $input์ผ ๋•Œ, ์ถœ๋ ฅ ๋ฉ”์‹œ์ง€๋Š” "$output"์ด์–ด์•ผ ํ•œ๋‹ค', ({ input, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.amount(input)).toBe(output); + }); + }); + + describe('benefitHistory ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: + '์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ: -1,200์› - ํ‰์ผ ํ• ์ธ: -4,046์› - ํŠน๋ณ„ ํ• ์ธ: -1,000์› - ์ฆ์ • ์ด๋ฒคํŠธ: -25,000์›"์ด๋‹ค.', + input: { + xmasBenefitAmount: 1200, + weekDayBenefitAmount: 4046, + specialBenefitAmount: 1000, + giftAmount: 25000, + }, + output: + 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ: -1,200์›\nํ‰์ผ ํ• ์ธ: -4,046์›\nํŠน๋ณ„ ํ• ์ธ: -1,000์›\n์ฆ์ • ์ด๋ฒคํŠธ: -25,000์›', + }, + { + description: `ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ์ด ๋ชจ๋‘ 0์›์ธ ๊ฒฝ์šฐ ๊ฒฐ๊ณผ๋Š” "${OUTPUT_MESSAGE.nothing}"์ด๋‹ค.`, + input: { + xmasBenefitAmount: 0, + weekendBenefitAmount: 0, + weekDayBenefitAmount: 0, + specialBenefitAmount: 0, + giftAmount: 0, + }, + output: OUTPUT_MESSAGE.nothing, + }, + ])('$description', ({ input, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.benefitHistory(input)).toBe(output); + }); + }); + + describe('title ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: 'newLine ์˜ต์…˜์ด false์ผ ๊ฒฝ์šฐ, ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” "<์ œ๋ชฉ>"์ด๋‹ค.', + input: { config: { newLine: false }, title: '์ œ๋ชฉ' }, + output: '<์ œ๋ชฉ>', + }, + { + description: 'newLine ์˜ต์…˜์ด true์ผ ๊ฒฝ์šฐ, ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” "\\n<์ œ๋ชฉ>"์ด๋‹ค.', + input: { config: { newLine: true }, title: '์ œ๋ชฉ' }, + output: '\n<์ œ๋ชฉ>', + }, + ])('$description', ({ input, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.title(input.config, input.title)).toBe(output); + }); + }); + + describe('gift ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: '์ฆ์ • ์ด๋ฒคํŠธ ๊ธˆ์•ก์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” "์ƒดํŽ˜์ธ 1๊ฐœ" ์ด๋‹ค.', + input: 25000, + output: '์ƒดํŽ˜์ธ 1๊ฐœ', + }, + { + description: `์ฆ์ • ์ด๋ฒคํŠธ ๊ธˆ์•ก์ด ์—†๋Š” ๊ฒฝ์šฐ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” "${OUTPUT_MESSAGE.nothing}" ์ด๋‹ค.`, + input: 0, + output: OUTPUT_MESSAGE.nothing, + }, + ])('$description', ({ input: giftAmount, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.gift(giftAmount)).toBe(output); + }); + }); + + describe('totalBenefitAmount ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: '์ž…๋ ฅ์ด 25000์ธ ๊ฒฝ์šฐ "-25,000์›"์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', + input: 25000, + output: '-25,000์›', + }, + { + description: '์ž…๋ ฅ์ด 0์ธ ๊ฒฝ์šฐ "0์›"์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', + input: 0, + output: '0์›', + }, + ])('$description', ({ input: totalBenefitAmount, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.totalBenefitAmount(totalBenefitAmount)).toBe(output); + }); + }); + + describe('eventBadge ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: '์ž…๋ ฅ์ด "์‚ฐํƒ€"์ธ ๊ฒฝ์šฐ ๊ทธ๋Œ€๋กœ "์‚ฐํƒ€"๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', + input: '์‚ฐํƒ€', + output: '์‚ฐํƒ€', + }, + { + description: `์ž…๋ ฅ ๊ฐ’์ด null์ธ ๊ฒฝ์šฐ "${OUTPUT_MESSAGE.nothing}"์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.`, + input: null, + output: OUTPUT_MESSAGE.nothing, + }, + ])('$description', ({ input: eventBadge, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.eventBadge(eventBadge)).toBe(output); + }); + }); +});
JavaScript
input, output์œผ๋กœ ๋ฌถ์–ด์„œ testCase๋ฅผ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์—ˆ๊ตฐ์š”, ์ข‹์€ ๋ฐฉ๋ฒ• ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค.
@@ -0,0 +1,84 @@ +import { PROMOTION_DATE_INFO } from '../promotionSystem.js'; +import { SYMBOLS } from '../symbols.js'; + +export const INPUT_MESSAGE = Object.freeze({ + visitDate: `${PROMOTION_DATE_INFO.month}์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n`, + orderMenus: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', +}); + +export const OUTPUT_MESSAGE = Object.freeze({ + guideComments: `์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น ${PROMOTION_DATE_INFO.month}์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.`, + + title: Object.freeze({ + orderMenu: '์ฃผ๋ฌธ ๋ฉ”๋‰ด', + totalOrderAmount: 'ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก', + giftMenu: '์ฆ์ • ๋ฉ”๋‰ด', + benefitHistory: 'ํ˜œํƒ ๋‚ด์—ญ', + totalBenefitAmount: '์ดํ˜œํƒ ๊ธˆ์•ก', + expectPaymentAmount: 'ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก', + eventBadge: `${PROMOTION_DATE_INFO.month}์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€`, + }), + + benefitLabels: Object.freeze({ + xmasBenefitAmount: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', + weekDayBenefitAmount: 'ํ‰์ผ ํ• ์ธ', + weekendBenefitAmount: '์ฃผ๋ง ํ• ์ธ', + specialBenefitAmount: 'ํŠน๋ณ„ ํ• ์ธ', + giftAmount: '์ฆ์ • ์ด๋ฒคํŠธ', + }), + + nothing: '์—†์Œ', + + gift: '์ƒดํŽ˜์ธ 1๊ฐœ', +}); + +export const FORMAT_MESSAGE = Object.freeze({ + date(visitDate) { + return `${PROMOTION_DATE_INFO.month}์›” ${visitDate}์ผ`; + }, + + endGuideComments(visitDate) { + return `${this.date(visitDate)}์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!`; + }, + + title(config, title) { + return `${config.newLine ? '\n' : SYMBOLS.emptyString}<${title}>`; + }, + + orderMenus(orderMenuInfo) { + return orderMenuInfo + .map(([menuName, quantity]) => `${menuName} ${quantity}๊ฐœ\n`) + .join(SYMBOLS.emptyString); + }, + + amount(amount) { + return `${amount.toLocaleString()}์›`; + }, + + benefitHistory(benefitInfo) { + return ( + Object.entries(benefitInfo) + .reduce( + (acc, [benefitName, amount]) => + amount !== 0 + ? [...acc, `${OUTPUT_MESSAGE.benefitLabels[benefitName]}: -${this.amount(amount)}`] + : acc, + [], + ) + .join('\n') || OUTPUT_MESSAGE.nothing + ); + }, + + totalBenefitAmount(totalBenefitAmount) { + return totalBenefitAmount === 0 ? `${this.amount(0)}` : `-${this.amount(totalBenefitAmount)}`; + }, + + gift(giftAmount) { + return giftAmount === 0 ? OUTPUT_MESSAGE.nothing : OUTPUT_MESSAGE.gift; + }, + + eventBadge(eventBadge) { + return eventBadge ?? OUTPUT_MESSAGE.nothing; + }, +});
JavaScript
freeze๋œ ๊ฐ์ฒด ์•ˆ์— ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์„œ return๊ฐ’์œผ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ๋„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์‘์šฉํ–ˆ๋‹ค๋ฉด ๋งŽ์ด ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณผ ์ˆ˜ ์žˆ์—ˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ์ข‹์€ ์ •๋ณด ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,49 @@ +import InputView from '../views/InputView.js'; +import OutputView from '../views/OutputView.js'; +import PromotionResultService from '../service/PromotionResultService.js'; +import ErrorHandler from '../errors/ErrorHandler/module.js'; + +/** + * @module ChristmasPromotionController + * ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ”„๋กœ๋ชจ์…˜ ์ด๋ฒคํŠธ ๊ด€๋ จ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ๋ฆ„ ์ œ์–ด๋ฅผ ๋‹ด๋‹น + */ +const ChristmasPromotionController = { + /** + * @returns {Promise<void>} + */ + async play() { + const { visitDate, orderMenuInfo } = await processUserInput(); + + processPromotionResult({ visitDate, orderMenuInfo }); + }, +}; + +/** + * @returns {Promise<{visitDate : number, orderMenuInfo : [string, number][]}>} ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐฉ๋ฌธ ์ผ์ž ๋ฐ ๋ฉ”๋‰ด ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•  Promise ๊ฐ์ฒด + */ +async function processUserInput() { + OutputView.printStartGuideComments(); + + const visitDate = await ErrorHandler.retryOnErrors(InputView.readVisitDate.bind(InputView)); + const orderMenuInfo = await ErrorHandler.retryOnErrors( + InputView.readOrderMenuInfo.bind(InputView), + ); + + return { visitDate, orderMenuInfo }; +} + +/** + * @param {{visitDate : number, orderMenuInfo : [string, number][]}} params - ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐฉ๋ฌธ ์ผ์ž ๋ฐ ๋ฉ”๋‰ด ์ •๋ณด + * @returns {void} + */ +function processPromotionResult({ visitDate, orderMenuInfo }) { + const promotionResult = PromotionResultService.createPromotionResult({ + visitDate, + orderMenuInfo, + }); + + OutputView.printEndGuideComments(visitDate); + OutputView.printPromotionResult({ orderMenuInfo, promotionResult }); +} + +export default ChristmasPromotionController;
JavaScript
[Nit] ์ทจํ–ฅ์—์„œ ๊ฐˆ๋ฆฌ๋Š” ์˜์—ญ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ์ฝœ๋ฐฑ ๋Œ€์‹ ์— ๋™์  this binding์„ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ ๊ฐ€์š”?
@@ -0,0 +1,132 @@ +import { FORMAT_MESSAGE, OUTPUT_MESSAGE } from './module'; + +describe('๋ฉ”์‹œ์ง€ ํฌ๋งท ํ…Œ์ŠคํŠธ', () => { + describe('orderMenus ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: '์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” "ํ”ผ์ž 2๊ฐœ - ์ฝœ๋ผ 1๊ฐœ" ์ด๋‹ค.', + input: [ + ['ํ”ผ์ž', 2], + ['์ฝœ๋ผ', 1], + ], + output: 'ํ”ผ์ž 2๊ฐœ\n์ฝœ๋ผ 1๊ฐœ\n', + }, + ])('$description', ({ input, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.orderMenus(input)).toBe(output); + }); + }); + + describe('amount ํ…Œ์ŠคํŠธ', () => { + test.each([ + { input: 5000, output: '5,000์›' }, + { input: 0, output: '0์›' }, + ])('amount๊ฐ€ $input์ผ ๋•Œ, ์ถœ๋ ฅ ๋ฉ”์‹œ์ง€๋Š” "$output"์ด์–ด์•ผ ํ•œ๋‹ค', ({ input, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.amount(input)).toBe(output); + }); + }); + + describe('benefitHistory ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: + '์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” "ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ: -1,200์› - ํ‰์ผ ํ• ์ธ: -4,046์› - ํŠน๋ณ„ ํ• ์ธ: -1,000์› - ์ฆ์ • ์ด๋ฒคํŠธ: -25,000์›"์ด๋‹ค.', + input: { + xmasBenefitAmount: 1200, + weekDayBenefitAmount: 4046, + specialBenefitAmount: 1000, + giftAmount: 25000, + }, + output: + 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ: -1,200์›\nํ‰์ผ ํ• ์ธ: -4,046์›\nํŠน๋ณ„ ํ• ์ธ: -1,000์›\n์ฆ์ • ์ด๋ฒคํŠธ: -25,000์›', + }, + { + description: `ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ์ด ๋ชจ๋‘ 0์›์ธ ๊ฒฝ์šฐ ๊ฒฐ๊ณผ๋Š” "${OUTPUT_MESSAGE.nothing}"์ด๋‹ค.`, + input: { + xmasBenefitAmount: 0, + weekendBenefitAmount: 0, + weekDayBenefitAmount: 0, + specialBenefitAmount: 0, + giftAmount: 0, + }, + output: OUTPUT_MESSAGE.nothing, + }, + ])('$description', ({ input, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.benefitHistory(input)).toBe(output); + }); + }); + + describe('title ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: 'newLine ์˜ต์…˜์ด false์ผ ๊ฒฝ์šฐ, ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” "<์ œ๋ชฉ>"์ด๋‹ค.', + input: { config: { newLine: false }, title: '์ œ๋ชฉ' }, + output: '<์ œ๋ชฉ>', + }, + { + description: 'newLine ์˜ต์…˜์ด true์ผ ๊ฒฝ์šฐ, ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” "\\n<์ œ๋ชฉ>"์ด๋‹ค.', + input: { config: { newLine: true }, title: '์ œ๋ชฉ' }, + output: '\n<์ œ๋ชฉ>', + }, + ])('$description', ({ input, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.title(input.config, input.title)).toBe(output); + }); + }); + + describe('gift ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: '์ฆ์ • ์ด๋ฒคํŠธ ๊ธˆ์•ก์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” "์ƒดํŽ˜์ธ 1๊ฐœ" ์ด๋‹ค.', + input: 25000, + output: '์ƒดํŽ˜์ธ 1๊ฐœ', + }, + { + description: `์ฆ์ • ์ด๋ฒคํŠธ ๊ธˆ์•ก์ด ์—†๋Š” ๊ฒฝ์šฐ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” "${OUTPUT_MESSAGE.nothing}" ์ด๋‹ค.`, + input: 0, + output: OUTPUT_MESSAGE.nothing, + }, + ])('$description', ({ input: giftAmount, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.gift(giftAmount)).toBe(output); + }); + }); + + describe('totalBenefitAmount ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: '์ž…๋ ฅ์ด 25000์ธ ๊ฒฝ์šฐ "-25,000์›"์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', + input: 25000, + output: '-25,000์›', + }, + { + description: '์ž…๋ ฅ์ด 0์ธ ๊ฒฝ์šฐ "0์›"์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', + input: 0, + output: '0์›', + }, + ])('$description', ({ input: totalBenefitAmount, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.totalBenefitAmount(totalBenefitAmount)).toBe(output); + }); + }); + + describe('eventBadge ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: '์ž…๋ ฅ์ด "์‚ฐํƒ€"์ธ ๊ฒฝ์šฐ ๊ทธ๋Œ€๋กœ "์‚ฐํƒ€"๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', + input: '์‚ฐํƒ€', + output: '์‚ฐํƒ€', + }, + { + description: `์ž…๋ ฅ ๊ฐ’์ด null์ธ ๊ฒฝ์šฐ "${OUTPUT_MESSAGE.nothing}"์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.`, + input: null, + output: OUTPUT_MESSAGE.nothing, + }, + ])('$description', ({ input: eventBadge, output }) => { + // given - when - then + expect(FORMAT_MESSAGE.eventBadge(eventBadge)).toBe(output); + }); + }); +});
JavaScript
๊ตฌ์กฐ๋ถ„ํ•ด๋ฅผ ์ด์šฉํ•ด `description`์€ ๋ฒ„๋ฆฌ๊ณ  `input`, `output`๋งŒ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒŒ ์ข‹์€ ๊ฒƒ ๊ฐ™๋„ค์š”! ๊ทธ๋Ÿฐ๋ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ํ•˜๋‚˜์ธ ๊ฒƒ ๊ฐ™์€๋ฐ `test.each()`๋ฅผ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,49 @@ +import InputView from '../views/InputView.js'; +import OutputView from '../views/OutputView.js'; +import PromotionResultService from '../service/PromotionResultService.js'; +import ErrorHandler from '../errors/ErrorHandler/module.js'; + +/** + * @module ChristmasPromotionController + * ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ”„๋กœ๋ชจ์…˜ ์ด๋ฒคํŠธ ๊ด€๋ จ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ๋ฆ„ ์ œ์–ด๋ฅผ ๋‹ด๋‹น + */ +const ChristmasPromotionController = { + /** + * @returns {Promise<void>} + */ + async play() { + const { visitDate, orderMenuInfo } = await processUserInput(); + + processPromotionResult({ visitDate, orderMenuInfo }); + }, +}; + +/** + * @returns {Promise<{visitDate : number, orderMenuInfo : [string, number][]}>} ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐฉ๋ฌธ ์ผ์ž ๋ฐ ๋ฉ”๋‰ด ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•  Promise ๊ฐ์ฒด + */ +async function processUserInput() { + OutputView.printStartGuideComments(); + + const visitDate = await ErrorHandler.retryOnErrors(InputView.readVisitDate.bind(InputView)); + const orderMenuInfo = await ErrorHandler.retryOnErrors( + InputView.readOrderMenuInfo.bind(InputView), + ); + + return { visitDate, orderMenuInfo }; +} + +/** + * @param {{visitDate : number, orderMenuInfo : [string, number][]}} params - ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐฉ๋ฌธ ์ผ์ž ๋ฐ ๋ฉ”๋‰ด ์ •๋ณด + * @returns {void} + */ +function processPromotionResult({ visitDate, orderMenuInfo }) { + const promotionResult = PromotionResultService.createPromotionResult({ + visitDate, + orderMenuInfo, + }); + + OutputView.printEndGuideComments(visitDate); + OutputView.printPromotionResult({ orderMenuInfo, promotionResult }); +} + +export default ChristmasPromotionController;
JavaScript
์ปจํŠธ๋กค๋Ÿฌ ๋ชจ๋“ˆ์ด ์งง๊ณ  ์ฝ๊ธฐ ํŽธํ•œ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,31 @@ +import { PROMOTION_MENU_TABLE } from '../../constants/promotionSystem.js'; + +/** + * @module MenuSearcher + * ๋ฉ”๋‰ด ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ๋ฉ”๋‰ด๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ ๋ชจ๋“ˆ + */ +const MenuSearcher = Object.freeze({ + /** + * @param {string} menuName - ๋ฉ”๋‰ด ์ด๋ฆ„ + * @param {string} category - ๋ฉ”๋‰ด ์นดํ…Œ๊ณ ๋ฆฌ + * @returns {undefined | string} ๋งค์นญ ๋˜๊ฑฐ๋‚˜ ๋งค์นญ ๋˜์ง€ ์•Š์€(undefined) ๋ฉ”๋‰ด ์ด๋ฆ„ + */ + findByMenuName(menuName, category) { + const menusForSearch = + PROMOTION_MENU_TABLE[category] ?? + Object.values(PROMOTION_MENU_TABLE).flatMap((section) => section); + + return menusForSearch.find((menu) => menu.name === menuName); + }, + + /** + * @param {string} menuName - ๋ฉ”๋‰ด ์ด๋ฆ„ + * @param {string} category - ๋ฉ”๋‰ด ์นดํ…Œ๊ณ ๋ฆฌ + * @returns {boolean} ์นดํ…Œ๊ณ ๋ฆฌ์— ์žˆ๋Š” ๋ฉ”๋‰ด ์ด๋ฆ„์ด ์‹ค์ œ ๋ฉ”๋‰ด ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€ + */ + isMenuInCategory(menuName, category) { + return this.findByMenuName(menuName, category) !== undefined; + }, +}); + +export default MenuSearcher;
JavaScript
๋นŒํŠธ์ธ ๋ฉ”์„œ๋“œ ํ™œ์šฉ๐Ÿ‘๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,53 @@ +import { PROMOTION_DATE_INFO } from '../../constants/promotionSystem.js'; + +// constant.js๋Š” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๋ฐ module.js์—์„œ๋งŒ ์‚ฌ์šฉ +export const INITIAL_PROMOTION_BENEFIT_RESULT = Object.freeze({ + xmasBenefitAmount: 0, + weekDayBenefitAmount: 0, + weekendBenefitAmount: 0, + specialBenefitAmount: 0, + giftAmount: 0, +}); + +export const MINIMUM_TOTAL_ORDER_AMOUNT = 10000; + +export const BENEFIT_DATE_INFO = Object.freeze({ + startDate: new Date(`${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-01`), + endDate: new Date(`${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-31`), + christmas: new Date(`${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-25`), +}); + +export const DAY_OF_BENEFIT_CONDITION = Object.freeze({ + weekday: Object.freeze({ + menuCategory: '๋””์ €ํŠธ', + days: [0, 1, 2, 3, 4], + }), + + weekend: Object.freeze({ + menuCategory: '๋ฉ”์ธ', + days: [5, 6], + }), +}); + +export const BENEFIT_AMOUNT_INFO = Object.freeze({ + everyDay: 100, + christmas: 1000, + dayOfWeek: 2023, + special: 1000, +}); + +export const MINIMUM_ORDER_AMOUNT_FOR_GIFT = 120000; + +export const GIFT_INFO = { + menuCategory: '์Œ๋ฃŒ', + menuName: '์ƒดํŽ˜์ธ', +}; + +export const SPECIAL_DATES = new Set([ + `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-03`, + `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-10`, + `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-17`, + `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-24`, + `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-25`, + `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-31`, +]);
JavaScript
๋„๋ฉ”์ธ๋งˆ๋‹ค ์ƒ์ˆ˜ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ๊ณ„์‹œ๋Š”๋ฐ, ์ด๋ฏธ ์žˆ๋Š” `constants` ๋””๋ ‰ํ† ๋ฆฌ์— ์ƒ์ˆ˜๋ฅผ ๋ฌถ์–ด๋†“์ง€ ์•Š์œผ์‹œ๋Š” ์ด์œ ๊ฐ€ ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•ด์š”. ๋งŒ์•ฝ ์ƒˆ๋กœ์šด ๊ฐœ๋ฐœ์ž๊ฐ€ ๋“ค์–ด์˜จ๋‹ค๋ฉด, `constants`์— ๋ชจ๋“  ์ƒ์ˆ˜๊ฐ€ ๋“ค์–ด ์žˆ์„ ๊ฑฐ๋ผ๊ณ  ๊ธฐ๋Œ€ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ์š”. ์ค‘์š”ํ•œ ์ƒ์ˆ˜๋“ค์ด `domain` ์•ˆ์— ์žˆ๋Š” ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฌ์— ๋“ค์–ด์žˆ์–ด์„œ ํ˜ผ๋ž€์„ ์ค„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ํŒŒ์ผ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋ผ๊ณ  ์ƒ๊ฐํ•ด์š”๐Ÿ˜…. ์ƒ์ˆ˜๋‚˜ ํ…Œ์ŠคํŠธ ํŒŒ์ผ ๋ชจ๋‘ ํŠน์ • ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์— ๋ฌถ์œผ๋ฉด ์‚ฌ์šฉ์ด๋‚˜ ๊ด€๋ฆฌ๊ฐ€ ๋” ์ˆ˜์›”ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”? ๋˜ ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด `domain`์˜ ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฌ๋“ค์„ ์—†์•จ ์ˆ˜ ์žˆ์–ด์„œ `domain` ํŒŒ์ผ๋“ค๋„ ๊ด€๋ฆฌํ•˜๊ธฐ๊ฐ€ ์ˆ˜์›”ํ•ด์งˆ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,49 @@ +import InputView from '../views/InputView.js'; +import OutputView from '../views/OutputView.js'; +import PromotionResultService from '../service/PromotionResultService.js'; +import ErrorHandler from '../errors/ErrorHandler/module.js'; + +/** + * @module ChristmasPromotionController + * ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ”„๋กœ๋ชจ์…˜ ์ด๋ฒคํŠธ ๊ด€๋ จ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ๋ฆ„ ์ œ์–ด๋ฅผ ๋‹ด๋‹น + */ +const ChristmasPromotionController = { + /** + * @returns {Promise<void>} + */ + async play() { + const { visitDate, orderMenuInfo } = await processUserInput(); + + processPromotionResult({ visitDate, orderMenuInfo }); + }, +}; + +/** + * @returns {Promise<{visitDate : number, orderMenuInfo : [string, number][]}>} ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐฉ๋ฌธ ์ผ์ž ๋ฐ ๋ฉ”๋‰ด ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•  Promise ๊ฐ์ฒด + */ +async function processUserInput() { + OutputView.printStartGuideComments(); + + const visitDate = await ErrorHandler.retryOnErrors(InputView.readVisitDate.bind(InputView)); + const orderMenuInfo = await ErrorHandler.retryOnErrors( + InputView.readOrderMenuInfo.bind(InputView), + ); + + return { visitDate, orderMenuInfo }; +} + +/** + * @param {{visitDate : number, orderMenuInfo : [string, number][]}} params - ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐฉ๋ฌธ ์ผ์ž ๋ฐ ๋ฉ”๋‰ด ์ •๋ณด + * @returns {void} + */ +function processPromotionResult({ visitDate, orderMenuInfo }) { + const promotionResult = PromotionResultService.createPromotionResult({ + visitDate, + orderMenuInfo, + }); + + OutputView.printEndGuideComments(visitDate); + OutputView.printPromotionResult({ orderMenuInfo, promotionResult }); +} + +export default ChristmasPromotionController;
JavaScript
์ด๋ฆ„์„ ํ†ตํ•ด ์˜๋ฏธ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ์ฃผ์„์ด ์—†์–ด๋„ ๊ดœ์ฐฎ์ง€ ์•Š์„๊นŒ์š”?
@@ -0,0 +1,143 @@ +import MenuSearcher from '../MenuSearcher/module.js'; +import { PROMOTION_DATE_INFO } from '../../constants/promotionSystem.js'; +import { + DAY_OF_BENEFIT_CONDITION, + BENEFIT_DATE_INFO, + INITIAL_PROMOTION_BENEFIT_RESULT, + MINIMUM_TOTAL_ORDER_AMOUNT, + BENEFIT_AMOUNT_INFO, + MINIMUM_ORDER_AMOUNT_FOR_GIFT, + GIFT_INFO, + SPECIAL_DATES, +} from './constant.js'; + +/** + * @module DecemberPromotionPlan + * 12์›”์˜ ํ”„๋กœ๋ชจ์…˜ ๊ณ„ํš ๋“ค์„(ํฌ๋ฆฌ์Šค๋งˆ์Šค, ์ฃผ๋ง/ํ‰์ผ ํ• ์ธ, ํŠน๋ณ„ ์ด๋ฒคํŠธ ๋ฐ ์ฆ์ • ํ–‰์‚ฌ)ํ†ตํ•ด ํ• ์ธ ํ˜œํƒ ๋“ค์„ ์ œ๊ณต ํ•˜๋Š” ๋ชจ๋“ˆ + */ +const DecemberPromotionPlan = Object.freeze({ + /** + * @param {import('../../utils/jsDoc.js').OrdererInfo} ordererInfo - ์ฃผ๋ฌธ์ž ์ •๋ณด(๋ฐฉ๋ฌธ ์ผ์ž, ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก, ์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด ์ •๋ณด) + * @returns {import('../../utils/jsDoc.js').PromotionBenefitResult} ๊ฐ ํ˜œํƒ ๊ธˆ์•ก ์ •๋ณด + */ + execute({ visitDate, totalOrderAmount, orderMenuInfo }) { + const { startDate, endDate } = BENEFIT_DATE_INFO; + const formatVisitDate = formatVisitDateForPromotion(visitDate); + const params = { dateInfo: { formatVisitDate, startDate, endDate }, totalOrderAmount }; + + return isAvailablePromotion(params) + ? createPromotionBenefitResult({ + visitDate: formatVisitDate, + totalOrderAmount, + orderMenuInfo, + }) + : INITIAL_PROMOTION_BENEFIT_RESULT; + }, +}); + +export default DecemberPromotionPlan; + +/** + * @param {number} visitDate - ๋ฐฉ๋ฌธ ์ผ์ž + * @returns {Date} ๋ฐฉ๋ฌธ ์ผ์ž์— ๋Œ€ํ•œ Date ๊ฐ์ฒด + */ +function formatVisitDateForPromotion(visitDate) { + const { year, month } = PROMOTION_DATE_INFO; + + return visitDate < 10 + ? new Date(`${year}-${month}-0${visitDate}`) + : new Date(`${year}-${month}-${visitDate}`); +} + +/** + * @param {{dateInfo: {formatVisitDate: Date, startDate: Date, endDate: Date}, totalOrderAmount: number}} params - ํ”„๋กœ๋ชจ์…˜์ด ์œ ํšจํ•œ์ง€ ํ™•์ธํ•  ์ •๋ณด + * @returns {boolean} ์ด๋ฒคํŠธ ๊ธฐ๊ฐ„ ๋ฐ ์ตœ์†Œ ์ฃผ๋ฌธ ๊ธˆ์•ก ์š”๊ฑด์„ ์ถฉ์กฑํ•˜๋Š”์ง€ ์—ฌ๋ถ€ + */ +function isAvailablePromotion({ + dateInfo: { formatVisitDate, startDate, endDate }, + totalOrderAmount, +}) { + return ( + formatVisitDate >= startDate && + formatVisitDate <= endDate && + totalOrderAmount >= MINIMUM_TOTAL_ORDER_AMOUNT + ); +} + +/** + * @param {import('../../utils/jsDoc.js').OrdererInfo} ordererInfo - ์ฃผ๋ฌธ์ž ์ •๋ณด(๋ฐฉ๋ฌธ ์ผ์ž, ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก, ์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด ์ •๋ณด) + * @returns {import('../../utils/jsDoc.js').PromotionBenefitResult} ๊ฐ ํ˜œํƒ ๊ธˆ์•ก ์ •๋ณด + */ +function createPromotionBenefitResult(ordererInfo) { + const { weekday, weekend } = DAY_OF_BENEFIT_CONDITION; + + return { + xmasBenefitAmount: calculateChristmasBenefit(ordererInfo), + weekDayBenefitAmount: calculateBenefitForDayType({ ordererInfo, ...weekday }), + weekendBenefitAmount: calculateBenefitForDayType({ ordererInfo, ...weekend }), + specialBenefitAmount: calculateSpecialBenefit(ordererInfo), + giftAmount: calculateGiftEvent(ordererInfo), + }; +} + +/** + * @param {import('../../utils/jsDoc.js').OrdererInfo} ordererInfo - ์ฃผ๋ฌธ์ž ์ •๋ณด(๋ฐฉ๋ฌธ ์ผ์ž, ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก, ์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด ์ •๋ณด) + * @returns {number | 0} ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ์ด ์ ์šฉ๋˜๊ฑฐ๋‚˜ ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ธˆ์•ก + */ +function calculateChristmasBenefit({ visitDate }) { + const { startDate, christmas: endDate } = BENEFIT_DATE_INFO; + + if (!(visitDate >= startDate && visitDate <= endDate)) return 0; + + const millisecondPerDay = 1000 * 60 * 60 * 24; + const daysUntilChristmas = Math.floor((endDate - visitDate) / millisecondPerDay); + + const { christmas, everyDay } = BENEFIT_AMOUNT_INFO; + + const totalEventDay = 24; + return christmas + everyDay * (totalEventDay - daysUntilChristmas); +} + +/** + * + * @param {import('../../utils/jsDoc.js').CalculateBenefitForDayTypeParams} params - ์ฃผ๋ฌธ์ž ์ •๋ณด + ์š”์ผ ํ• ์ธ ์กฐ๊ฑด์ด ๋“ค์–ด์žˆ๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜ + * @returns {number | 0} ์š”์ผ(์ฃผ๋ง/ํ‰์ผ) ํ• ์ธ์ด ์ ์šฉ๋˜๊ฑฐ๋‚˜ ์ ์šฉ ๋˜์ง€ ์•Š์€ ๊ธˆ์•ก + */ +function calculateBenefitForDayType({ + ordererInfo: { orderMenuInfo, visitDate }, + menuCategory, + days, +}) { + return !days.includes(visitDate.getDay()) + ? 0 + : orderMenuInfo + .filter(([menuName]) => MenuSearcher.isMenuInCategory(menuName, menuCategory)) + .reduce( + (totalBenefit, [, quantity]) => totalBenefit + BENEFIT_AMOUNT_INFO.dayOfWeek * quantity, + 0, + ); +} + +/** + * @param {{visitDate : Date}} params - ๋ฐฉ๋ฌธ ์ผ์ž(Date ๊ฐ์ฒด)๊ฐ€ ํฌํ•จ๋œ ๊ฐ์ฒด + * @returns {number | 0} ํŠน๋ณ„ ํ• ์ธ์ด ์ ์šฉ๋˜๊ฑฐ๋‚˜ ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ธˆ์•ก + */ +function calculateSpecialBenefit({ visitDate }) { + const formattedVisitDate = visitDate.toISOString().substring(0, 10); + const specialDates = SPECIAL_DATES; + + return specialDates.has(formattedVisitDate) ? BENEFIT_AMOUNT_INFO.special : 0; +} + +/** + * @param {{totalOrderAmount : number}} params - ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์ด ํฌํ•จ๋œ ๊ฐ์ฒด + * @returns {number | 0} ์ฆ์ • ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋˜๊ฑฐ๋‚˜ ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ธˆ์•ก + */ +function calculateGiftEvent({ totalOrderAmount }) { + if (totalOrderAmount < MINIMUM_ORDER_AMOUNT_FOR_GIFT) return 0; + + const { menuCategory, menuName } = GIFT_INFO; + const champagne = MenuSearcher.findByMenuName(menuName, menuCategory); + + return champagne.price; +}
JavaScript
[Nit] ํŠน๋ณ„ ํ• ์ธ ์ผ์ž๋„ `Date`๋กœ ๊ด€๋ฆฌํ•œ๋‹ค๋ฉด ํŒŒ์‹ฑ ๋กœ์ง์„ `Date` ๊ฐ์ฒด์— ๋งž๊ฒŒ ์„ ์–ธ์ ์œผ๋กœ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”? ์•„๋‹ˆ๋ฉด ๋ฉ”๋ชจ๋ฆฌ์ƒ ์ด์ ์„ ๋…ธ๋ฆฌ์‹ ๊ฑด๊ฐ€์š”?
@@ -0,0 +1,63 @@ +export const PROMOTION_DATE_INFO = Object.freeze({ + year: 2023, + month: 12, +}); + +export const PROMOTION_MENU_TABLE = Object.freeze({ + ์• ํ”ผํƒ€์ด์ €: [ + { + name: '์–‘์†ก์ด์ˆ˜ํ”„', + price: 6_000, + }, + { + name: 'ํƒ€ํŒŒ์Šค', + price: 5_500, + }, + { + name: '์‹œ์ €์ƒ๋Ÿฌ๋“œ', + price: 8_000, + }, + ], + ๋ฉ”์ธ: [ + { + name: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', + price: 55_000, + }, + { + name: '๋ฐ”๋น„ํ๋ฆฝ', + price: 54_000, + }, + { + name: 'ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€', + price: 35_000, + }, + { + name: 'ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€', + price: 25_000, + }, + ], + ๋””์ €ํŠธ: [ + { + name: '์ดˆ์ฝ”์ผ€์ดํฌ', + price: 15_000, + }, + { + name: '์•„์ด์Šคํฌ๋ฆผ', + price: 5_000, + }, + ], + ์Œ๋ฃŒ: [ + { + name: '์ œ๋กœ์ฝœ๋ผ', + price: 3_000, + }, + { + name: '๋ ˆ๋“œ์™€์ธ', + price: 60_000, + }, + { + name: '์ƒดํŽ˜์ธ', + price: 25_000, + }, + ], +});
JavaScript
`Badge` ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” `EventBadgeMaker` ๋ชจ๋“ˆ๊ณผ ๊ฐ™์€ ๋””๋ ‰ํ† ๋ฆฌ์— ์กด์žฌํ•˜๋Š”๋ฐ ๋ฉ”๋‰ด๋„ ๊ฐ™์€ ๋ชจ๋“ˆ์— ์žˆ๋‹ค๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,277 @@ +import { PROMOTION_DATE_INFO } from '../../constants/promotionSystem'; +import { + BENEFIT_AMOUNT_INFO, + MINIMUM_ORDER_AMOUNT_FOR_GIFT, + INITIAL_PROMOTION_BENEFIT_RESULT, + MINIMUM_TOTAL_ORDER_AMOUNT, +} from './constant'; +import DecemberPromotionPlan from './module'; + +describe('12์›” ์ด๋ฒคํŠธ ๊ณ„ํš์— ๋”ฐ๋ฅธ ํ˜œํƒ ๊ณ„์‚ฐ ํ…Œ์ŠคํŠธ', () => { + const createBenefitResult = (ordererInfo) => DecemberPromotionPlan.execute(ordererInfo); + + const createOrdererInfoTestCase = ({ + visitDate = 3, + orderMenuInfo = [ + ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', 1], + ['๋ฐ”๋น„ํ๋ฆฝ', 1], + ['์ดˆ์ฝ”์ผ€์ดํฌ', 2], + ['์ œ๋กœ์ฝœ๋ผ', 1], + ], + totalOrderAmount = 142000, + } = {}) => ({ + visitDate, + orderMenuInfo, + totalOrderAmount, + }); + + describe('ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: `5000์›์€ ${MINIMUM_TOTAL_ORDER_AMOUNT}์› ๋ฏธ๋งŒ ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.`, + ordererInfo: { + visitDate: 3, + orderMenuInfo: ['์•„์ด์Šคํฌ๋ฆผ', 1], + totalOrderAmount: 5000, + }, + expectedBenefitInfo: { + ...INITIAL_PROMOTION_BENEFIT_RESULT, + }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + + // then + expect(benefitResult).toStrictEqual(expectedBenefitInfo); + }); + }); + + describe('ํ‰์ผ ํ• ์ธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: `${PROMOTION_DATE_INFO.month}์›” 3์ผ์€ ํ‰์ผ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ• ์ธ์ด ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase(), + }, + expectedBenefitInfo: { + weekDayBenefitAmount: 2 * BENEFIT_AMOUNT_INFO.dayOfWeek, + }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 2์ผ์€ ์ฃผ๋ง์ด๊ธฐ ๋•Œ๋ฌธ์— ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase({ + visitDate: 2, + }), + }, + expectedBenefitInfo: { + weekDayBenefitAmount: 0, + }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + + // then + expect(benefitResult.weekDayBenefitAmount).toBe(expectedBenefitInfo.weekDayBenefitAmount); + }); + }); + + describe('์ฃผ๋ง ํ• ์ธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: `${PROMOTION_DATE_INFO.month}์›” 2์ผ์€ ์ฃผ๋ง์ด๊ธฐ ๋•Œ๋ฌธ์— ํ• ์ธ์ด ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase({ + visitDate: 2, + }), + }, + expectedBenefitInfo: { + weekendBenefitAmount: BENEFIT_AMOUNT_INFO.dayOfWeek * 2, + }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 3์ผ์€ ํ‰์ผ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase(), + }, + expectedBenefitInfo: { + weekendBenefitAmount: 0, + }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + + // then + expect(benefitResult.weekendBenefitAmount).toBe(expectedBenefitInfo.weekendBenefitAmount); + }); + }); + + describe('12์›” ์ด๋ฒคํŠธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: `${PROMOTION_DATE_INFO.month}์›” 1์ผ์€ ${PROMOTION_DATE_INFO.month}์›” ํ• ์ธ์ด ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase({ visitDate: 1 }), + }, + expectedBenefitInfo: { + xmasBenefitAmount: BENEFIT_AMOUNT_INFO.christmas, + giftAmount: 25000, + specialBenefitAmount: 0, + weekDayBenefitAmount: 0, + weekendBenefitAmount: BENEFIT_AMOUNT_INFO.dayOfWeek * 2, + }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 31์ผ์€ 12์›” ํ• ์ธ์ด ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase({ + visitDate: 31, + }), + }, + expectedBenefitInfo: { + xmasBenefitAmount: 0, + weekDayBenefitAmount: BENEFIT_AMOUNT_INFO.dayOfWeek * 2, + weekendBenefitAmount: 0, + specialBenefitAmount: BENEFIT_AMOUNT_INFO.special, + giftAmount: 25000, + }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + + // then + expect(benefitResult).toStrictEqual(expectedBenefitInfo); + }); + }); + + describe('์ฆ์ • ์ด๋ฒคํŠธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: `88000์›์€ ${MINIMUM_ORDER_AMOUNT_FOR_GIFT}๋งŒ์› ๋ฏธ๋งŒ์ด๋ฏ€๋กœ ์ƒดํŽ˜์ธ์ด ์ฆ์ •๋˜์ง€ ์•Š๋Š”๋‹ค.`, + ordererInfo: { + visitDate: 2, + orderMenuInfo: [ + ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', 1], + ['์ดˆ์ฝ”์ผ€์ดํฌ', 2], + ['์ œ๋กœ์ฝœ๋ผ', 1], + ], + totalOrderAmount: 88000, + }, + expectedBenefitInfo: { + giftAmount: 0, + }, + }, + { + description: `142000์›์€ ${MINIMUM_ORDER_AMOUNT_FOR_GIFT}๋งŒ์› ์ดˆ๊ณผ์ด๋ฏ€๋กœ ์ƒดํŽ˜์ธ์ด ์ฆ์ •๋œ๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase(), + }, + expectedBenefitInfo: { + giftAmount: 25000, + }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + // then + expect(benefitResult.giftAmount).toBe(expectedBenefitInfo.giftAmount); + }); + }); + + describe('ํฌ๋ฆฌ์Šค๋งˆ์Šค ์ด๋ฒคํŠธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค๊ฐ€ ์ง€๋‚œ ์ดํ›„๋Š” ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.', + ordererInfo: { + ...createOrdererInfoTestCase({ + visitDate: 26, + }), + }, + expectedBenefitInfo: { + xmasBenefitAmount: 0, + }, + }, + { + description: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ์ „์—๋Š” ํ• ์ธ์ด ์ ์šฉ ๋œ๋‹ค.', + ordererInfo: { + ...createOrdererInfoTestCase({ + visitDate: 24, + }), + }, + expectedBenefitInfo: { + xmasBenefitAmount: 3300, + }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + + // then + expect(benefitResult.xmasBenefitAmount).toBe(expectedBenefitInfo.xmasBenefitAmount); + }); + }); + + describe('ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: `${PROMOTION_DATE_INFO.month}์›” 3์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 3, + }), + expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 10์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 10, + }), + expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 17์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 17, + }), + expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 24์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 24, + }), + expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 25์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 25, + }), + expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 31์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 31, + }), + expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 30์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 30, + }), + expectedBenefitInfo: { specialBenefitAmount: 0 }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + + // then + expect(benefitResult.specialBenefitAmount).toBe(expectedBenefitInfo.specialBenefitAmount); + }); + }); +});
JavaScript
`'ํฌ๋ฆฌ์Šค๋งˆ์Šค๊ฐ€ ์ง€๋‚œ ์ดํ›„'`์™€ `'ํฌ๋ฆฌ์Šค๋งˆ์Šค ์ „'`์„ ๊ธฐ์ค€์œผ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋Š”๋ฐ์š”. ์ด ๋ง๋Œ€๋กœ๋ผ๋ฉด `'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋‹น์ผ'`์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์„๊นŒ์š”? ์‹ค์ œ ๊ตฌํ˜„ ๋กœ์ง๋Œ€๋กœ๋ผ๋ฉด ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋‹น์ผ์—๋„ ์ด๋ฒคํŠธ ํ˜œํƒ์ด ์ ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— `'ํฌ๋ฆฌ์Šค๋งˆ์Šค ์ „์—๋Š” ํ• ์ธ์ด ์ ์šฉ๋œ๋‹ค'` ์ชฝ์˜ ํ…Œ์ŠคํŠธ์— ํฌํ•จ๋˜๊ฒ ์ง€๋งŒ, `'ํฌ๋ฆฌ์Šค๋งˆ์Šค ์ „'`์ด๋ผ๋Š” ๋ง์ด `'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋‹น์ผ'`์„ ํฌํ•จํ•˜์ง„ ์•Š์ž–์•„์š”. ๊ทธ๋ž˜์„œ ์ข€ ๋” ๋ช…ํ™•ํ•œ ๋œป์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๊ฒŒ๋” ์ž‘์„ฑํ•˜๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”! ์ €๋Š” ์ด๋Ÿฐ ์‹์œผ๋กœ ์ž‘์„ฑํ•˜๋Š”๋ฐ ๋„์›€์ด ๋˜์‹ค๊นŒ ํ•ด์„œ ์˜ฌ๋ ค๋ด…๋‹ˆ๋‹ค. ```javascript describe('ํฌ๋ฆฌ์Šค๋งˆ์Šค ์ด๋ฒคํŠธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { test.each([ // ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž…๋ ฅ ๊ฐ’๊ณผ ๊ธฐ๋Œ€ ๊ฐ’ ์ผ€์ด์Šค ])('OO์ผ์— ๋ฐฉ๋ฌธํ•˜๋ฉด XX์›๋งŒํผ ํ• ์ธ์ด ์ ์šฉ๋œ๋‹ค', () => { // ํ…Œ์ŠคํŠธ }) test.each([ // ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž…๋ ฅ ๊ฐ’๊ณผ ๊ธฐ๋Œ€ ๊ฐ’ ์ผ€์ด์Šค ])('OO์ผ์— ๋ฐฉ๋ฌธํ•˜๋ฉด ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค', () => { // ํ…Œ์ŠคํŠธ }) }) ```
@@ -0,0 +1,53 @@ +import { PROMOTION_DATE_INFO } from '../../constants/promotionSystem.js'; + +// constant.js๋Š” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๋ฐ module.js์—์„œ๋งŒ ์‚ฌ์šฉ +export const INITIAL_PROMOTION_BENEFIT_RESULT = Object.freeze({ + xmasBenefitAmount: 0, + weekDayBenefitAmount: 0, + weekendBenefitAmount: 0, + specialBenefitAmount: 0, + giftAmount: 0, +}); + +export const MINIMUM_TOTAL_ORDER_AMOUNT = 10000; + +export const BENEFIT_DATE_INFO = Object.freeze({ + startDate: new Date(`${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-01`), + endDate: new Date(`${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-31`), + christmas: new Date(`${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-25`), +}); + +export const DAY_OF_BENEFIT_CONDITION = Object.freeze({ + weekday: Object.freeze({ + menuCategory: '๋””์ €ํŠธ', + days: [0, 1, 2, 3, 4], + }), + + weekend: Object.freeze({ + menuCategory: '๋ฉ”์ธ', + days: [5, 6], + }), +}); + +export const BENEFIT_AMOUNT_INFO = Object.freeze({ + everyDay: 100, + christmas: 1000, + dayOfWeek: 2023, + special: 1000, +}); + +export const MINIMUM_ORDER_AMOUNT_FOR_GIFT = 120000; + +export const GIFT_INFO = { + menuCategory: '์Œ๋ฃŒ', + menuName: '์ƒดํŽ˜์ธ', +}; + +export const SPECIAL_DATES = new Set([ + `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-03`, + `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-10`, + `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-17`, + `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-24`, + `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-25`, + `${PROMOTION_DATE_INFO.year}-${PROMOTION_DATE_INFO.month}-31`, +]);
JavaScript
P5- ํŒŒ์‹ฑ ๋กœ์ง์ด ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋กœ ๋น ์งˆ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,48 @@ +import { addCalculation } from '../../utils/number.js'; + +/** + * @module PromotionReceipt + * ์ด ํ˜œํƒ ๊ธˆ์•ก ๋ฐ ์˜ˆ์ƒ ์ง€์ถœ ๊ธˆ์•ก์„ ๋ฌถ์–ด ํ”„๋กœ๋ชจ์…˜ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์˜์ˆ˜์ฆ์„ ํ‘œํ˜„ํ•œ ๋ชจ๋“ˆ + */ +const PromotionReceipt = Object.freeze({ + /** + * + * @param {{promotionBenefitResult : import('../../utils/jsDoc.js').PromotionBenefitResult, totalOrderAmount : number}} params - ํ˜œํƒ ์ •๋ณด ๋ฐ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์ด ๋“ค์–ด์žˆ๋Š” ๊ฐ์ฒด + * @returns {import('../../utils/jsDoc.js').PromotionReceipt} ์ด ํ˜œํƒ ๊ธˆ์•ก ๋ฐ ์˜ˆ์ƒ ์ง€์ถœ ๊ธˆ์•ก + */ + issue({ promotionBenefitResult, totalOrderAmount }) { + const totalBenefitAmount = calculateBenefitAmount(promotionBenefitResult); + const expectPaymentAmount = calculateExpectedPayment({ + promotionBenefitResult, + totalBenefitAmount, + totalOrderAmount, + }); + + return { totalBenefitAmount, expectPaymentAmount }; + }, +}); + +export default PromotionReceipt; + +/** + * @param {import('../../utils/jsDoc.js').PromotionBenefitResult} promotionBenefitResult - ํ˜œํƒ ๊ฒฐ๊ณผ + * @returns {number} ์ด ํ˜œํƒ ๊ธˆ์•ก + */ +function calculateBenefitAmount(promotionBenefitResult) { + return Object.values(promotionBenefitResult).reduce(addCalculation, 0); +} + +/** + * + * @param {{totalOrderAmount : number, promotionBenefitResult : import('../../utils/jsDoc.js').PromotionBenefitResult, totalBenefitAmount : number}} params - ํ•จ์ˆ˜ ์‹คํ–‰์— ํ•„์š”ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค + * @returns {number} ์˜ˆ์ƒ ์ง€์ถœ ๊ธˆ์•ก + */ +function calculateExpectedPayment({ + totalOrderAmount, + totalBenefitAmount, + promotionBenefitResult, +}) { + const benefitAmount = totalBenefitAmount - promotionBenefitResult.giftAmount; + + return totalOrderAmount - benefitAmount; +}
JavaScript
์˜ค ์ด๊ฑฐ ์‹ ๋ฐ•ํ•˜๋„ค์š”ใ…‹ใ…‹ใ…‹ `sort` ๊ฐ™์€ ๋ฉ”์„œ๋“œ์—์„œ๋„ ๋ณ€ํ˜•ํ•ด์„œ ์“ธ ์ˆ˜ ์žˆ๊ฒ ์–ด์š”!
@@ -0,0 +1,47 @@ +import MenuSearcher from './module.js'; + +describe('๋ฉ”๋‰ด ์ฐพ๊ธฐ ํ…Œ์ŠคํŠธ', () => { + describe('findByMenuName ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + menuName: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', + category: '์Šคํ…Œ์ดํฌ', + expectedMenu: { name: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', price: 55000 }, + }, + { + menuName: '์ดˆ์ฝ”์ผ€์ดํฌ', + category: '๋””์ €ํŠธ', + expectedMenu: { name: '์ดˆ์ฝ”์ผ€์ดํฌ', price: 15000 }, + }, + { menuName: '์ œ๋กœ์ฝœ๋ผ', category: '์Œ๋ฃŒ', expectedMenu: { name: '์ œ๋กœ์ฝœ๋ผ', price: 3000 } }, + { menuName: '์ดˆ์ฝ”์ผ€์ดํฌ', category: '์Œ๋ฃŒ', expectedMenu: undefined }, + ])( + '๋ฉ”๋‰ด ์ด๋ฆ„์ด $menuName์ด๊ณ , ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ $category์ผ ๊ฒฝ์šฐ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋Š” $expectedMenu ์ด๋‹ค.', + ({ menuName, category, expectedMenu }) => { + // given - when + const menu = MenuSearcher.findByMenuName(menuName, category); + + // then + expect(menu).toEqual(expectedMenu); + }, + ); + }); + + describe('isMenuInCategory ํ…Œ์ŠคํŠธ', () => { + test.each([ + { menuName: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', category: '๋ฉ”์ธ', expectedResult: true }, + { menuName: '์ดˆ์ฝ”์ผ€์ดํฌ', category: '๋””์ €ํŠธ', expectedResult: true }, + { menuName: '์ œ๋กœ์ฝœ๋ผ', category: '์Œ๋ฃŒ', expectedResult: true }, + { menuName: '์ดˆ์ฝ”์ผ€์ดํฌ', category: '์Œ๋ฃŒ', expectedResult: false }, + ])( + '๋ฉ”๋‰ด ์ด๋ฆ„์ด $menuName์ด๊ณ , ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ $category์ผ ๋•Œ, ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” $expectedResult ์ด๋‹ค.', + ({ menuName, category, expectedResult }) => { + // given - when + const result = MenuSearcher.isMenuInCategory(menuName, category); + + // then + expect(result).toBe(expectedResult); + }, + ); + }); +});
JavaScript
์š”์ฆ˜ ๋“ค์–ด ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋ฅผ ์งค๋•Œ `boolean ๋ฐ˜ํ™˜ ํ•จ์ˆ˜`์— ๋Œ€ํ•œ test suite ๋ถ„๋ฆฌ์— ๋Œ€ํ•ด ์ƒ๊ฐ์„ ํ•˜๊ฒŒ ๋˜๋”๋ผ๊ตฌ์š”. _`toBeTruthy`์™€ `toBeFalsy` matcher๋„ ์กด์žฌํ•˜๋Š”๋ฐ ๋ฐ˜ํ™˜๊ฐ’์ด `boolean`์ธ ๊ฒฝ์šฐ์—๋Š” test suite๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š”๊ฒŒ ๋งž๋‚˜?_ ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด์„œ์š”! matcher๋„ ๋” ๋ช…์‹œ์ ์œผ๋กœ ์ฝํžˆ๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ ํ…Œ์ŠคํŠธ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋„ ์ข€ ๋” ๊ฐ€๋ณ๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜๋„ ์žˆ์„๊ฒƒ ๊ฐ™๊ตฌ์š”. ์ง„์˜๋‹˜ ์˜๊ฒฌ์€ ์–ด๋– ์‹ ๊ฐ€์š”?
@@ -0,0 +1,277 @@ +import { PROMOTION_DATE_INFO } from '../../constants/promotionSystem'; +import { + BENEFIT_AMOUNT_INFO, + MINIMUM_ORDER_AMOUNT_FOR_GIFT, + INITIAL_PROMOTION_BENEFIT_RESULT, + MINIMUM_TOTAL_ORDER_AMOUNT, +} from './constant'; +import DecemberPromotionPlan from './module'; + +describe('12์›” ์ด๋ฒคํŠธ ๊ณ„ํš์— ๋”ฐ๋ฅธ ํ˜œํƒ ๊ณ„์‚ฐ ํ…Œ์ŠคํŠธ', () => { + const createBenefitResult = (ordererInfo) => DecemberPromotionPlan.execute(ordererInfo); + + const createOrdererInfoTestCase = ({ + visitDate = 3, + orderMenuInfo = [ + ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', 1], + ['๋ฐ”๋น„ํ๋ฆฝ', 1], + ['์ดˆ์ฝ”์ผ€์ดํฌ', 2], + ['์ œ๋กœ์ฝœ๋ผ', 1], + ], + totalOrderAmount = 142000, + } = {}) => ({ + visitDate, + orderMenuInfo, + totalOrderAmount, + }); + + describe('ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: `5000์›์€ ${MINIMUM_TOTAL_ORDER_AMOUNT}์› ๋ฏธ๋งŒ ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.`, + ordererInfo: { + visitDate: 3, + orderMenuInfo: ['์•„์ด์Šคํฌ๋ฆผ', 1], + totalOrderAmount: 5000, + }, + expectedBenefitInfo: { + ...INITIAL_PROMOTION_BENEFIT_RESULT, + }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + + // then + expect(benefitResult).toStrictEqual(expectedBenefitInfo); + }); + }); + + describe('ํ‰์ผ ํ• ์ธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: `${PROMOTION_DATE_INFO.month}์›” 3์ผ์€ ํ‰์ผ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ• ์ธ์ด ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase(), + }, + expectedBenefitInfo: { + weekDayBenefitAmount: 2 * BENEFIT_AMOUNT_INFO.dayOfWeek, + }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 2์ผ์€ ์ฃผ๋ง์ด๊ธฐ ๋•Œ๋ฌธ์— ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase({ + visitDate: 2, + }), + }, + expectedBenefitInfo: { + weekDayBenefitAmount: 0, + }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + + // then + expect(benefitResult.weekDayBenefitAmount).toBe(expectedBenefitInfo.weekDayBenefitAmount); + }); + }); + + describe('์ฃผ๋ง ํ• ์ธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: `${PROMOTION_DATE_INFO.month}์›” 2์ผ์€ ์ฃผ๋ง์ด๊ธฐ ๋•Œ๋ฌธ์— ํ• ์ธ์ด ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase({ + visitDate: 2, + }), + }, + expectedBenefitInfo: { + weekendBenefitAmount: BENEFIT_AMOUNT_INFO.dayOfWeek * 2, + }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 3์ผ์€ ํ‰์ผ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase(), + }, + expectedBenefitInfo: { + weekendBenefitAmount: 0, + }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + + // then + expect(benefitResult.weekendBenefitAmount).toBe(expectedBenefitInfo.weekendBenefitAmount); + }); + }); + + describe('12์›” ์ด๋ฒคํŠธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: `${PROMOTION_DATE_INFO.month}์›” 1์ผ์€ ${PROMOTION_DATE_INFO.month}์›” ํ• ์ธ์ด ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase({ visitDate: 1 }), + }, + expectedBenefitInfo: { + xmasBenefitAmount: BENEFIT_AMOUNT_INFO.christmas, + giftAmount: 25000, + specialBenefitAmount: 0, + weekDayBenefitAmount: 0, + weekendBenefitAmount: BENEFIT_AMOUNT_INFO.dayOfWeek * 2, + }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 31์ผ์€ 12์›” ํ• ์ธ์ด ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase({ + visitDate: 31, + }), + }, + expectedBenefitInfo: { + xmasBenefitAmount: 0, + weekDayBenefitAmount: BENEFIT_AMOUNT_INFO.dayOfWeek * 2, + weekendBenefitAmount: 0, + specialBenefitAmount: BENEFIT_AMOUNT_INFO.special, + giftAmount: 25000, + }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + + // then + expect(benefitResult).toStrictEqual(expectedBenefitInfo); + }); + }); + + describe('์ฆ์ • ์ด๋ฒคํŠธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: `88000์›์€ ${MINIMUM_ORDER_AMOUNT_FOR_GIFT}๋งŒ์› ๋ฏธ๋งŒ์ด๋ฏ€๋กœ ์ƒดํŽ˜์ธ์ด ์ฆ์ •๋˜์ง€ ์•Š๋Š”๋‹ค.`, + ordererInfo: { + visitDate: 2, + orderMenuInfo: [ + ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', 1], + ['์ดˆ์ฝ”์ผ€์ดํฌ', 2], + ['์ œ๋กœ์ฝœ๋ผ', 1], + ], + totalOrderAmount: 88000, + }, + expectedBenefitInfo: { + giftAmount: 0, + }, + }, + { + description: `142000์›์€ ${MINIMUM_ORDER_AMOUNT_FOR_GIFT}๋งŒ์› ์ดˆ๊ณผ์ด๋ฏ€๋กœ ์ƒดํŽ˜์ธ์ด ์ฆ์ •๋œ๋‹ค.`, + ordererInfo: { + ...createOrdererInfoTestCase(), + }, + expectedBenefitInfo: { + giftAmount: 25000, + }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + // then + expect(benefitResult.giftAmount).toBe(expectedBenefitInfo.giftAmount); + }); + }); + + describe('ํฌ๋ฆฌ์Šค๋งˆ์Šค ์ด๋ฒคํŠธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค๊ฐ€ ์ง€๋‚œ ์ดํ›„๋Š” ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.', + ordererInfo: { + ...createOrdererInfoTestCase({ + visitDate: 26, + }), + }, + expectedBenefitInfo: { + xmasBenefitAmount: 0, + }, + }, + { + description: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ์ „์—๋Š” ํ• ์ธ์ด ์ ์šฉ ๋œ๋‹ค.', + ordererInfo: { + ...createOrdererInfoTestCase({ + visitDate: 24, + }), + }, + expectedBenefitInfo: { + xmasBenefitAmount: 3300, + }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + + // then + expect(benefitResult.xmasBenefitAmount).toBe(expectedBenefitInfo.xmasBenefitAmount); + }); + }); + + describe('ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ ์ ์šฉ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ', () => { + test.each([ + { + description: `${PROMOTION_DATE_INFO.month}์›” 3์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 3, + }), + expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 10์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 10, + }), + expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 17์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 17, + }), + expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 24์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 24, + }), + expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 25์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 25, + }), + expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 31์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 31, + }), + expectedBenefitInfo: { specialBenefitAmount: BENEFIT_AMOUNT_INFO.special }, + }, + { + description: `${PROMOTION_DATE_INFO.month}์›” 30์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.`, + ordererInfo: createOrdererInfoTestCase({ + visitDate: 30, + }), + expectedBenefitInfo: { specialBenefitAmount: 0 }, + }, + ])('$description', ({ ordererInfo, expectedBenefitInfo }) => { + // given - when + const benefitResult = createBenefitResult(ordererInfo); + + // then + expect(benefitResult.specialBenefitAmount).toBe(expectedBenefitInfo.specialBenefitAmount); + }); + }); +});
JavaScript
`test.each()`๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€๋งŒ ์ฝ”๋“œ๊ฐ€ ์ค‘๋ณต๋˜๊ณ  ์žˆ๊ณ , ๋ผ์ธ ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๊ธด ๊ฒƒ ๊ฐ™์•„์š”. "ํ•˜๋‚˜์˜ ํ•จ์ˆ˜์˜ ๋ผ์ธ ์ˆ˜๊ฐ€ 15๋ฅผ ๋„˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค"๋Š” ์š”๊ตฌ ์‚ฌํ•ญ์ด ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋„ ๋ผ์ธ ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๊ธธ๋ฉด ์ฝ๊ธฐ๊ฐ€ ํž˜๋“ค ๊ฒƒ ๊ฐ™์•„์š”. ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ผ์ธ ์ˆ˜๋„ ํ™• ์ค„์ผ ์ˆ˜ ์žˆ๋Š”๋ฐ ๊ณ ๋ คํ•ด๋ณด์‹œ๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”? ```javascript test.each([3, 10, 17, 24 ...])( '12์›” %d์ผ์€ ํŠน๋ณ„ ํ• ์ธ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋œ๋‹ค', (visitDate) => { // given const expected = BENEFIT_AMOUNT_INFO.special; // when const result= createBenefitResult(ordererInfo); // then expect(result).toBe(expected ); }); ```
@@ -0,0 +1,143 @@ +import MenuSearcher from '../MenuSearcher/module.js'; +import { PROMOTION_DATE_INFO } from '../../constants/promotionSystem.js'; +import { + DAY_OF_BENEFIT_CONDITION, + BENEFIT_DATE_INFO, + INITIAL_PROMOTION_BENEFIT_RESULT, + MINIMUM_TOTAL_ORDER_AMOUNT, + BENEFIT_AMOUNT_INFO, + MINIMUM_ORDER_AMOUNT_FOR_GIFT, + GIFT_INFO, + SPECIAL_DATES, +} from './constant.js'; + +/** + * @module DecemberPromotionPlan + * 12์›”์˜ ํ”„๋กœ๋ชจ์…˜ ๊ณ„ํš ๋“ค์„(ํฌ๋ฆฌ์Šค๋งˆ์Šค, ์ฃผ๋ง/ํ‰์ผ ํ• ์ธ, ํŠน๋ณ„ ์ด๋ฒคํŠธ ๋ฐ ์ฆ์ • ํ–‰์‚ฌ)ํ†ตํ•ด ํ• ์ธ ํ˜œํƒ ๋“ค์„ ์ œ๊ณต ํ•˜๋Š” ๋ชจ๋“ˆ + */ +const DecemberPromotionPlan = Object.freeze({ + /** + * @param {import('../../utils/jsDoc.js').OrdererInfo} ordererInfo - ์ฃผ๋ฌธ์ž ์ •๋ณด(๋ฐฉ๋ฌธ ์ผ์ž, ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก, ์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด ์ •๋ณด) + * @returns {import('../../utils/jsDoc.js').PromotionBenefitResult} ๊ฐ ํ˜œํƒ ๊ธˆ์•ก ์ •๋ณด + */ + execute({ visitDate, totalOrderAmount, orderMenuInfo }) { + const { startDate, endDate } = BENEFIT_DATE_INFO; + const formatVisitDate = formatVisitDateForPromotion(visitDate); + const params = { dateInfo: { formatVisitDate, startDate, endDate }, totalOrderAmount }; + + return isAvailablePromotion(params) + ? createPromotionBenefitResult({ + visitDate: formatVisitDate, + totalOrderAmount, + orderMenuInfo, + }) + : INITIAL_PROMOTION_BENEFIT_RESULT; + }, +}); + +export default DecemberPromotionPlan; + +/** + * @param {number} visitDate - ๋ฐฉ๋ฌธ ์ผ์ž + * @returns {Date} ๋ฐฉ๋ฌธ ์ผ์ž์— ๋Œ€ํ•œ Date ๊ฐ์ฒด + */ +function formatVisitDateForPromotion(visitDate) { + const { year, month } = PROMOTION_DATE_INFO; + + return visitDate < 10 + ? new Date(`${year}-${month}-0${visitDate}`) + : new Date(`${year}-${month}-${visitDate}`); +} + +/** + * @param {{dateInfo: {formatVisitDate: Date, startDate: Date, endDate: Date}, totalOrderAmount: number}} params - ํ”„๋กœ๋ชจ์…˜์ด ์œ ํšจํ•œ์ง€ ํ™•์ธํ•  ์ •๋ณด + * @returns {boolean} ์ด๋ฒคํŠธ ๊ธฐ๊ฐ„ ๋ฐ ์ตœ์†Œ ์ฃผ๋ฌธ ๊ธˆ์•ก ์š”๊ฑด์„ ์ถฉ์กฑํ•˜๋Š”์ง€ ์—ฌ๋ถ€ + */ +function isAvailablePromotion({ + dateInfo: { formatVisitDate, startDate, endDate }, + totalOrderAmount, +}) { + return ( + formatVisitDate >= startDate && + formatVisitDate <= endDate && + totalOrderAmount >= MINIMUM_TOTAL_ORDER_AMOUNT + ); +} + +/** + * @param {import('../../utils/jsDoc.js').OrdererInfo} ordererInfo - ์ฃผ๋ฌธ์ž ์ •๋ณด(๋ฐฉ๋ฌธ ์ผ์ž, ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก, ์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด ์ •๋ณด) + * @returns {import('../../utils/jsDoc.js').PromotionBenefitResult} ๊ฐ ํ˜œํƒ ๊ธˆ์•ก ์ •๋ณด + */ +function createPromotionBenefitResult(ordererInfo) { + const { weekday, weekend } = DAY_OF_BENEFIT_CONDITION; + + return { + xmasBenefitAmount: calculateChristmasBenefit(ordererInfo), + weekDayBenefitAmount: calculateBenefitForDayType({ ordererInfo, ...weekday }), + weekendBenefitAmount: calculateBenefitForDayType({ ordererInfo, ...weekend }), + specialBenefitAmount: calculateSpecialBenefit(ordererInfo), + giftAmount: calculateGiftEvent(ordererInfo), + }; +} + +/** + * @param {import('../../utils/jsDoc.js').OrdererInfo} ordererInfo - ์ฃผ๋ฌธ์ž ์ •๋ณด(๋ฐฉ๋ฌธ ์ผ์ž, ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก, ์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด ์ •๋ณด) + * @returns {number | 0} ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ์ด ์ ์šฉ๋˜๊ฑฐ๋‚˜ ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ธˆ์•ก + */ +function calculateChristmasBenefit({ visitDate }) { + const { startDate, christmas: endDate } = BENEFIT_DATE_INFO; + + if (!(visitDate >= startDate && visitDate <= endDate)) return 0; + + const millisecondPerDay = 1000 * 60 * 60 * 24; + const daysUntilChristmas = Math.floor((endDate - visitDate) / millisecondPerDay); + + const { christmas, everyDay } = BENEFIT_AMOUNT_INFO; + + const totalEventDay = 24; + return christmas + everyDay * (totalEventDay - daysUntilChristmas); +} + +/** + * + * @param {import('../../utils/jsDoc.js').CalculateBenefitForDayTypeParams} params - ์ฃผ๋ฌธ์ž ์ •๋ณด + ์š”์ผ ํ• ์ธ ์กฐ๊ฑด์ด ๋“ค์–ด์žˆ๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜ + * @returns {number | 0} ์š”์ผ(์ฃผ๋ง/ํ‰์ผ) ํ• ์ธ์ด ์ ์šฉ๋˜๊ฑฐ๋‚˜ ์ ์šฉ ๋˜์ง€ ์•Š์€ ๊ธˆ์•ก + */ +function calculateBenefitForDayType({ + ordererInfo: { orderMenuInfo, visitDate }, + menuCategory, + days, +}) { + return !days.includes(visitDate.getDay()) + ? 0 + : orderMenuInfo + .filter(([menuName]) => MenuSearcher.isMenuInCategory(menuName, menuCategory)) + .reduce( + (totalBenefit, [, quantity]) => totalBenefit + BENEFIT_AMOUNT_INFO.dayOfWeek * quantity, + 0, + ); +} + +/** + * @param {{visitDate : Date}} params - ๋ฐฉ๋ฌธ ์ผ์ž(Date ๊ฐ์ฒด)๊ฐ€ ํฌํ•จ๋œ ๊ฐ์ฒด + * @returns {number | 0} ํŠน๋ณ„ ํ• ์ธ์ด ์ ์šฉ๋˜๊ฑฐ๋‚˜ ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ธˆ์•ก + */ +function calculateSpecialBenefit({ visitDate }) { + const formattedVisitDate = visitDate.toISOString().substring(0, 10); + const specialDates = SPECIAL_DATES; + + return specialDates.has(formattedVisitDate) ? BENEFIT_AMOUNT_INFO.special : 0; +} + +/** + * @param {{totalOrderAmount : number}} params - ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์ด ํฌํ•จ๋œ ๊ฐ์ฒด + * @returns {number | 0} ์ฆ์ • ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋˜๊ฑฐ๋‚˜ ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ธˆ์•ก + */ +function calculateGiftEvent({ totalOrderAmount }) { + if (totalOrderAmount < MINIMUM_ORDER_AMOUNT_FOR_GIFT) return 0; + + const { menuCategory, menuName } = GIFT_INFO; + const champagne = MenuSearcher.findByMenuName(menuName, menuCategory); + + return champagne.price; +}
JavaScript
๋งŽ์€ ๋ถ€๋ถ„์—์„œ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ณ„์‹œ๋Š”๋ฐ, `if`๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ์‹œ๋Š” ์ด์œ ๊ฐ€ ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,48 @@ +import { addCalculation } from '../../utils/number.js'; + +/** + * @module PromotionReceipt + * ์ด ํ˜œํƒ ๊ธˆ์•ก ๋ฐ ์˜ˆ์ƒ ์ง€์ถœ ๊ธˆ์•ก์„ ๋ฌถ์–ด ํ”„๋กœ๋ชจ์…˜ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์˜์ˆ˜์ฆ์„ ํ‘œํ˜„ํ•œ ๋ชจ๋“ˆ + */ +const PromotionReceipt = Object.freeze({ + /** + * + * @param {{promotionBenefitResult : import('../../utils/jsDoc.js').PromotionBenefitResult, totalOrderAmount : number}} params - ํ˜œํƒ ์ •๋ณด ๋ฐ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์ด ๋“ค์–ด์žˆ๋Š” ๊ฐ์ฒด + * @returns {import('../../utils/jsDoc.js').PromotionReceipt} ์ด ํ˜œํƒ ๊ธˆ์•ก ๋ฐ ์˜ˆ์ƒ ์ง€์ถœ ๊ธˆ์•ก + */ + issue({ promotionBenefitResult, totalOrderAmount }) { + const totalBenefitAmount = calculateBenefitAmount(promotionBenefitResult); + const expectPaymentAmount = calculateExpectedPayment({ + promotionBenefitResult, + totalBenefitAmount, + totalOrderAmount, + }); + + return { totalBenefitAmount, expectPaymentAmount }; + }, +}); + +export default PromotionReceipt; + +/** + * @param {import('../../utils/jsDoc.js').PromotionBenefitResult} promotionBenefitResult - ํ˜œํƒ ๊ฒฐ๊ณผ + * @returns {number} ์ด ํ˜œํƒ ๊ธˆ์•ก + */ +function calculateBenefitAmount(promotionBenefitResult) { + return Object.values(promotionBenefitResult).reduce(addCalculation, 0); +} + +/** + * + * @param {{totalOrderAmount : number, promotionBenefitResult : import('../../utils/jsDoc.js').PromotionBenefitResult, totalBenefitAmount : number}} params - ํ•จ์ˆ˜ ์‹คํ–‰์— ํ•„์š”ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค + * @returns {number} ์˜ˆ์ƒ ์ง€์ถœ ๊ธˆ์•ก + */ +function calculateExpectedPayment({ + totalOrderAmount, + totalBenefitAmount, + promotionBenefitResult, +}) { + const benefitAmount = totalBenefitAmount - promotionBenefitResult.giftAmount; + + return totalOrderAmount - benefitAmount; +}
JavaScript
์ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ถœ๋ ฅํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋ฅด๋Š” ์‚ฌ๋žŒ์ด ๋ณธ๋‹ค๋ฉด, `benefitAmount`์™€ `totalBenefitAmount`๋ฅผ ํ—ท๊ฐˆ๋ คํ•  ๊ฒƒ ๊ฐ™์•„์š”. ์ด ํ˜œํƒ ๊ธˆ์•ก์—์„œ ์ฆ์ • ๋ฉ”๋‰ด์˜ ๊ธˆ์•ก์„ ๋บ€ ๊ฐ’์€ ์ด ํ• ์ธ ๋ฐ›์€ ๊ธˆ์•ก์ด๋‹ˆ๊นŒ `discountedAmount`๋ผ๊ณ  ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”?