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`๋ผ๊ณ ํํํ ์ ์์ง ์์๊น์? |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.