code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,55 @@ +import { createComponent, currentComponent } from "../createComponent"; +import { fetchMovies } from "../fetch"; +import { useState } from "../state"; +import { MovieCard } from "./MovieCard"; + +export async function MovieList() { + const [page, setPage] = useState(1); + + const movieData = await fetchMovies({ page }); + + const [movies, setMovies] = useState([...movieData.results]); + const [totalPage] = useState(movieData.total_page); + + const movieCards = await Promise.all( + movies.map((movie, index) => + createComponent(MovieCard, { + index, + title: movie.title, + score: movie.vote_average, + thumbnailUrl: `https://image.tmdb.org/t/p/w200/${movie.poster_path}`, + }) + ) + ); + + const handleMoreButton = async () => { + if (page >= totalPage) { + return; + } + + await setPage(page + 1); + + const movieData = await fetchMovies({ page: page + 1 }); + const newMovies = [...movies, ...movieData.results]; + + await setMovies(newMovies); + }; + + const bindEvents = () => { + movieCards.forEach((movieCard) => movieCard.bindEvents()); + + const $moreButton = document.querySelector("#more-button"); + + $moreButton.addEventListener("click", handleMoreButton); + }; + + return { + element: /* html */ ` + <ul class="item-list"> + ${movieCards.map((movieCard) => movieCard.element).join("")} + </ul> + <button id="more-button" class="btn primary full-width">๋” ๋ณด๊ธฐ</button> + `, + bindEvents, + }; +}
JavaScript
๋งŒ์•ฝ ํ•œ ํŽ˜์ด์ง€์— ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์˜ํ™”๋ชฉ๋ก์„ ๋„์›Œ์ค€๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ์ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌํ™œ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,55 @@ +import { createComponent, currentComponent } from "../createComponent"; +import { fetchMovies } from "../fetch"; +import { useState } from "../state"; +import { MovieCard } from "./MovieCard"; + +export async function MovieList() { + const [page, setPage] = useState(1); + + const movieData = await fetchMovies({ page }); + + const [movies, setMovies] = useState([...movieData.results]); + const [totalPage] = useState(movieData.total_page); + + const movieCards = await Promise.all( + movies.map((movie, index) => + createComponent(MovieCard, { + index, + title: movie.title, + score: movie.vote_average, + thumbnailUrl: `https://image.tmdb.org/t/p/w200/${movie.poster_path}`, + }) + ) + ); + + const handleMoreButton = async () => { + if (page >= totalPage) { + return; + } + + await setPage(page + 1); + + const movieData = await fetchMovies({ page: page + 1 }); + const newMovies = [...movies, ...movieData.results]; + + await setMovies(newMovies); + }; + + const bindEvents = () => { + movieCards.forEach((movieCard) => movieCard.bindEvents()); + + const $moreButton = document.querySelector("#more-button"); + + $moreButton.addEventListener("click", handleMoreButton); + }; + + return { + element: /* html */ ` + <ul class="item-list"> + ${movieCards.map((movieCard) => movieCard.element).join("")} + </ul> + <button id="more-button" class="btn primary full-width">๋” ๋ณด๊ธฐ</button> + `, + bindEvents, + }; +}
JavaScript
๋ชจ๋“  ๊ตฌ๊ฐ„์ด ๋‹ค async/await ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์ด์œ ๊ฐ€ ์—ฌ๊ธฐ์„œ ์ถœ๋ฐœ์ด ๋˜๋„ค์š”. ์ด ๋ถ€๋ถ„๋งŒ ๊ฐœ์„ ํ•˜๋ฉด async/awiat ์„ ์ „์ฒด์ ์œผ๋กœ ์ œ๊ฑฐํ• ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”?
@@ -0,0 +1,55 @@ +import { createComponent, currentComponent } from "../createComponent"; +import { fetchMovies } from "../fetch"; +import { useState } from "../state"; +import { MovieCard } from "./MovieCard"; + +export async function MovieList() { + const [page, setPage] = useState(1); + + const movieData = await fetchMovies({ page }); + + const [movies, setMovies] = useState([...movieData.results]); + const [totalPage] = useState(movieData.total_page); + + const movieCards = await Promise.all( + movies.map((movie, index) => + createComponent(MovieCard, { + index, + title: movie.title, + score: movie.vote_average, + thumbnailUrl: `https://image.tmdb.org/t/p/w200/${movie.poster_path}`, + }) + ) + ); + + const handleMoreButton = async () => { + if (page >= totalPage) { + return; + } + + await setPage(page + 1); + + const movieData = await fetchMovies({ page: page + 1 }); + const newMovies = [...movies, ...movieData.results]; + + await setMovies(newMovies); + }; + + const bindEvents = () => { + movieCards.forEach((movieCard) => movieCard.bindEvents()); + + const $moreButton = document.querySelector("#more-button"); + + $moreButton.addEventListener("click", handleMoreButton); + }; + + return { + element: /* html */ ` + <ul class="item-list"> + ${movieCards.map((movieCard) => movieCard.element).join("")} + </ul> + <button id="more-button" class="btn primary full-width">๋” ๋ณด๊ธฐ</button> + `, + bindEvents, + }; +}
JavaScript
movieData๋ฅผ props๋กœ ๋ฐ›์•„์˜ค๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,13 @@ +export async function fetchMovies({ page }) { + const baseUrl = "https://api.themoviedb.org/3/movie/popular"; + const param = new URLSearchParams({ + page, + language: "ko-KR", + }); + + const response = await fetch(`${baseUrl}?${param}`, { + headers: { authorization: `Bearer ${process.env.API_KEY}` }, + }); + + return await response.json(); +}
JavaScript
์‚ฌ์‹ค ๋ฐฐํฌ๋œ ์‚ฌ์ดํŠธ์˜ ๋„คํŠธ์›Œํฌ ์š”์ฒญ์„ ๊นŒ๋ณด๋ฉด API_KEY ๊ฐ€ ๊ทธ๋Œ€๋กœ ๋…ธ์ถœ๋˜๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค ใ…Žใ…Ž ์ด๊ฑด ์–ด๋–ป๊ฒŒ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,13 @@ +export async function fetchMovies({ page }) { + const baseUrl = "https://api.themoviedb.org/3/movie/popular"; + const param = new URLSearchParams({ + page, + language: "ko-KR", + }); + + const response = await fetch(`${baseUrl}?${param}`, { + headers: { authorization: `Bearer ${process.env.API_KEY}` }, + }); + + return await response.json(); +}
JavaScript
์—ฌ๊ธฐ์„œ await์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๊ฒŒ ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ํ˜น์‹œ ์•„์‹ค๊นŒ์š”~?
@@ -1,14 +1,40 @@ -import React from "react"; -import "./PostList.scss"; +import React, { useEffect, useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import Posts from './components/Posts'; +import './PostList.scss'; const PostList = () => { + const [postData, setPostData] = useState([]); -return( - <div className="postList"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); + const goToWrite = () => { + navigate('/post-add'); + }; + useEffect(() => { + fetch('/data/postList.json', { + method: 'GET', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + }) + .then(res => res.json()) + .then(result => { + setPostData(result.data); + }); + }, []); + + if (postData.length === 0) return null; + return ( + <div className="postList"> + <div className="posts"> + <Posts postData={postData} /> + </div> + <div className="buttonPlace"> + <button onClick={goToWrite}>๊ธ€์“ฐ๊ธฐ</button> + </div> + </div> + ); }; -export default PostList; \ No newline at end of file +export default PostList;
JavaScript
import ์ˆœ์„œ์—๋„ ์œ ์ง€๋ณด์ˆ˜ ๋ฐ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•œ ์ปจ๋ฒค์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์ฝ”๋“œ์˜ ์ปจ๋ฒค์…˜์€ ๊ฐ„๋žตํ•˜๊ฒŒ ์•„๋ž˜์™€ ๊ฐ™๊ณ , ์ฐธ๊ณ ํ•ด์„œ ์ˆ˜์ •ํ•ด ์ฃผ์„ธ์š”! - ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ - React ๊ด€๋ จ ํŒจํ‚ค์ง€ - ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ - ์ปดํฌ๋„ŒํŠธ - ๊ณตํ†ต ์ปดํฌ๋„ŒํŠธ โ†’ ๋จผ ์ปดํฌ๋„ŒํŠธ โ†’ ๊ฐ€๊นŒ์šด ์ปดํฌ๋„ŒํŠธ - ํ•จ์ˆ˜, ๋ณ€์ˆ˜ ๋ฐ ์„ค์ • ํŒŒ์ผ - ์‚ฌ์ง„ ๋“ฑ ๋ฏธ๋””์–ด ํŒŒ์ผ(`.png`) - css ํŒŒ์ผ (.`scss`)
@@ -0,0 +1,63 @@ +.postList { + margin: 0 auto; + display: flex; + width: 576px; + height: 80vh; + padding: 40px 24px; + flex-direction: column; + align-items: center; + border-radius: 16px; + border: 1px solid #e5e5e5; + background: white; + + .posts { + width: 100%; + overflow-y: scroll; + flex: 1; + cursor: pointer; + } + & ::-webkit-scrollbar { + display: none; + } + + .buttonPlace { + display: flex; + flex-direction: column; + width: 528px; + height: 56px; + margin-top: 28px; + align-items: flex-end; + gap: 8px; + align-self: stretch; + + a { + text-decoration: none; + } + + button { + display: flex; + width: 120px; + height: 56px; + padding: 0px 34px; + justify-content: center; + align-items: center; + border-radius: 6px; + background-color: #2d71f7; + color: #fff; + text-align: center; + font-feature-settings: + 'clig' off, + 'liga' off; + font-family: Apple SD Gothic Neo; + font-size: 16px; + font-style: normal; + font-weight: 800; + line-height: normal; + + &:hover { + border-radius: 6px; + background: #083e7f; + } + } + } +}
Unknown
๋ถ€๋ชจ ์„ ํƒ์ž(`&`)๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•ด๋‹น ์š”์†Œ์—๋งŒ ์ ์šฉ๋˜๋Š” ์Šคํƒ€์ผ์ž„์„ ์ข€ ๋” ๊น”๋”ํ•˜๊ฒŒ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ```suggestion .posts { overflow-y: scroll; flex: 1; &::-webkit-scrollbar { display: none; } } ```
@@ -1,7 +1,18 @@ @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@100;300;400;500;700;900&display=swap'); - * { box-sizing: border-box; font-family: 'Noto Sans KR', sans-serif; -} \ No newline at end of file +} + +body { + width: 100vw; + height: 100vh; + display: flex; + align-items: center; + justify-content: center; +} + +button { + cursor: pointer; +}
Unknown
๊ณต์šฉ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด์„œ ์˜ฌ๋ ค์ฃผ์…จ๋Š”๋ฐ, ๊ณต์šฉ ํŒŒ์ผ์˜ ์ˆ˜์ •์€ 1. ํŒ€์›๊ณผ์˜ ์ถฉ๋ถ„ํ•œ ์ƒ์˜ ํ›„์— 2. ํ•ด๋‹น ํŒŒ์ผ๋งŒ ์ˆ˜์ •ํ•˜๋Š” ๋‚ด์šฉ์˜ PR ์„ ์˜ฌ๋ ค์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,13 @@ +package msa.customer.exception; + +public class DeliveryCustomerException extends RuntimeException { + + public DeliveryCustomerException(String message) { + super(message); + } + + public DeliveryCustomerException(String message, Throwable cause) { + super(message, cause); + } + +}
Java
(C) Exception์€ ์„œ๋น„์Šค์˜ `์ •์ฑ… ์œ„๋ฐ˜`์„ ๋‚˜ํƒ€๋‚ด๋Š” documentation์˜ ์—ญํ• ์„ ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Exception์„ ํ†ตํ•ด ์„œ๋น„์Šค ์ •์ฑ…์„ ์œ„๋ฐ˜ํ•˜๋Š” ๊ฒฝ์šฐ, ์ •์ฑ…์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ์ž์ฒด exception์„ ์ •์˜ํ•˜๊ณค ํ•˜๋Š”๋ฐ delivery application์— ์ ์šฉํ•ด๋ณผ๋งŒํ•œ exception ๋ช‡ ๊ฐœ๋ฅผ ๊ฐ„๋žตํ•˜๊ฒŒ ๊ตฌ์„ฑํ•ด๋ดค์œผ๋‹ˆ ์ฐธ๊ณ ํ•ด๋ณด์‹œ๊ณ  Exception๋“ค์„ ์ž˜ ์ •์˜ํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค :)
@@ -3,6 +3,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import msa.customer.entity.store.Store; +import msa.customer.exception.store.StoreEmptyException; import msa.customer.service.store.StoreService; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerMapping; @@ -11,6 +12,7 @@ import java.util.Optional; public class StoreCheckInterceptor implements HandlerInterceptor { + private final StoreService storeService; public StoreCheckInterceptor(StoreService storeService) { @@ -19,13 +21,14 @@ public StoreCheckInterceptor(StoreService storeService) { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); - String storeId = (String) pathVariables.get("storeId"); + Map pathVariables = (Map)request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); + String storeId = (String)pathVariables.get("storeId"); Optional<Store> storeOptional = storeService.getStore(storeId); - if(storeOptional.isEmpty()){ - throw new NullPointerException("Store doesn't exist. " + storeId + " is not correct store id."); + if (storeOptional.isEmpty()) { + throw new StoreEmptyException(storeId); } request.setAttribute("storeEntity", storeOptional.get()); return true; } + }
Java
(C) optional์€ ๋ฒ—๊ฒจ์ง„ ์ƒํƒœ๋กœ ์˜ค๋Š”๊ฒŒ ์ข‹๊ฒ ์ง€๋งŒ, ์ผ๋‹จ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  custom exception์„ throw ํ–ˆ์Šต๋‹ˆ๋‹ค
@@ -6,6 +6,7 @@ import msa.restaurant.dto.store.StoreResponseDto; import msa.restaurant.entity.store.Store; import msa.restaurant.dto.store.StoreSqsDto; +import msa.restaurant.exception.store.StoreCreationFailedException; import msa.restaurant.service.member.MemberService; import msa.restaurant.sqs.SendingMessageConverter; import msa.restaurant.service.store.StoreService; @@ -26,16 +27,19 @@ public class StoreController { private final SendingMessageConverter sendingMessageConverter; private final SqsService sqsService; - public StoreController(StoreService storeService, SendingMessageConverter sendingMessageConverter, MemberService memberService, SqsService sqsService) { + public StoreController(StoreService storeService, + SendingMessageConverter sendingMessageConverter, + MemberService memberService, + SqsService sqsService) { this.storeService = storeService; this.sendingMessageConverter = sendingMessageConverter; this.sqsService = sqsService; } @GetMapping @ResponseStatus(HttpStatus.OK) - public List<StorePartResponseDto> storeList ( - @RequestAttribute("cognitoUsername") String managerId) { + public List<StorePartResponseDto> storeList( + @RequestAttribute("cognitoUsername") String managerId) { List<Store> storeList = storeService.getStoreList(managerId); List<StorePartResponseDto> storeListDto = new ArrayList<>(); storeList.forEach(store -> { @@ -46,12 +50,12 @@ public List<StorePartResponseDto> storeList ( @PostMapping @ResponseStatus(HttpStatus.CREATED) - public void createStore (@RequestAttribute("cognitoUsername") String managerId, - @RequestBody StoreRequestDto data) { + public void createStore(@RequestAttribute("cognitoUsername") String managerId, + @RequestBody StoreRequestDto data) { String storeId = storeService.createStore(data, managerId); Optional<Store> storeOptional = storeService.getStore(storeId); - if (storeOptional.isEmpty()){ - throw new RuntimeException("Store creation failed."); + if (storeOptional.isEmpty()) { + throw new StoreCreationFailedException(storeId); } Store store = storeOptional.get(); StoreSqsDto storeSqsDto = new StoreSqsDto(store); @@ -62,16 +66,16 @@ public void createStore (@RequestAttribute("cognitoUsername") String managerId, @GetMapping("/{storeId}") @ResponseStatus(HttpStatus.OK) - public StoreResponseDto storeInfo (@RequestAttribute("cognitoUsername") String managerId, - @RequestAttribute("store") Store store) { + public StoreResponseDto storeInfo(@RequestAttribute("cognitoUsername") String managerId, + @RequestAttribute("store") Store store) { return new StoreResponseDto(store); } @PutMapping("/{storeId}") @ResponseStatus(HttpStatus.OK) public void updateStore(@RequestAttribute("cognitoUsername") String managerId, @PathVariable String storeId, - @RequestBody StoreRequestDto data) { + @RequestBody StoreRequestDto data) { storeService.updateStore(storeId, data); Optional<Store> storeOptional = storeService.getStore(storeId); Store store = storeOptional.get(); @@ -85,9 +89,9 @@ public void updateStore(@RequestAttribute("cognitoUsername") String managerId, @ResponseStatus(HttpStatus.CREATED) public void changeStoreStatus(@RequestAttribute("cognitoUsername") String managerId, @PathVariable String storeId, - @RequestBody boolean open){ + @RequestBody boolean open) { String messageToChangeStatus; - if (open){ + if (open) { storeService.openStore(storeId); messageToChangeStatus = sendingMessageConverter.createMessageToOpenStore(storeId); } else { @@ -101,11 +105,12 @@ public void changeStoreStatus(@RequestAttribute("cognitoUsername") String manage @DeleteMapping("/{storeId}") @ResponseStatus(HttpStatus.NO_CONTENT) public void deleteStore(@RequestAttribute("cognitoUsername") String managerId, - @PathVariable String storeId) { + @PathVariable String storeId) { storeService.deleteStore(storeId); String messageToDeleteStore = sendingMessageConverter.createMessageToDeleteStore(storeId); sqsService.sendToCustomer(messageToDeleteStore); sqsService.sendToRider(messageToDeleteStore); } + }
Java
(C) ์—ฌ๊ธฐ๋„ StoreCreationFailedException์„ ๋˜์ง€์ง€๋งŒ, storeService.createStore ๋‚ด๋ถ€์—์„œ exception ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ  ๋‚˜์˜ค๋Š” ๊ฒƒ์ด ๋” ๋ฐ”๋žŒ์งํ•ด๋ณด์ž…๋‹ˆ๋‹ค.
@@ -18,39 +18,30 @@ public OrderService(OrderRepository orderRepository) { this.orderRepository = orderRepository; } - public void createOrder(Order order){ + public void createOrder(Order order) { orderRepository.createOrder(order); } - public List<Order> getOrderList(String storeId){ + public List<Order> getOrderList(String storeId) { return orderRepository.readOrderList(storeId); } - public Optional<Order> getOrder(String orderId){ + public Optional<Order> getOrder(String orderId) { return orderRepository.readOrder(orderId); } - public OrderStatus changeOrderStatusToOrderAccept(String orderId, OrderStatus orderStatus){ - if(orderStatus.equals(OrderStatus.ORDER_REQUEST)){ - return orderRepository.updateOrderStatus(orderId, OrderStatus.ORDER_ACCEPT); - } else { - throw new IllegalStateException("The current order status is not changeable"); - } - } + public OrderStatus changeOrderStatus(Order order, OrderStatus status, OrderStatusUpdatePolicy orderStatusUpdatePolicy) { + orderStatusUpdatePolicy.checkStatusUpdatable(status); - public OrderStatus changeOrderStatusToFoodReady(String orderId, OrderStatus orderStatus){ - if (orderStatus.equals(OrderStatus.RIDER_ASSIGNED)) { - return orderRepository.updateOrderStatus(orderId, OrderStatus.FOOD_READY); - } else { - throw new IllegalStateException("The current order status is not changeable"); - } + return orderRepository.updateOrderStatus(order.getOrderId(), status); } - public void assignRiderToOrder(String orderId, OrderStatus orderStatus, RiderPartDto riderPartDto){ + public void assignRiderToOrder(String orderId, OrderStatus orderStatus, RiderPartDto riderPartDto) { orderRepository.updateRiderInfo(orderId, orderStatus, riderPartDto); } - public void changeOrderStatusFromOtherServer(String orderId, OrderStatus orderStatus){ + public void changeOrderStatusFromOtherServer(String orderId, OrderStatus orderStatus) { orderRepository.updateOrderStatus(orderId, orderStatus); } + }
Java
(C) changeOrderStatusTo~ ๋ฉ”์„œ๋“œ์˜ ๋‚ด์šฉ์ด ๋Œ€๋ถ€๋ถ„ ์ค‘๋ณต๋˜์–ด์„œ, ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋“ค์„ changeOrderStatus๋ผ๋Š” ๋ฉ”์„œ๋“œ๋กœ ํ†ตํ•ฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ƒˆ๋กœ์šด ํด๋ž˜์Šค์ธ OrderStatusUpdatePolicy๊ฐ€ ๋งŒ๋“ค์–ด์กŒ๋Š”๋ฐ, ~Policy๋ผ๋Š” ๋ช…์นญ์€ ์„œ๋น„์Šค ์ •์ฑ…์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋ฒ”์šฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค ๋„ค์ด๋ฐ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. delivery-application์—๋„ ์„œ๋น„์Šค ์šด์˜์— ํ•„์š”ํ•œ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ •์ฑ…๋“ค์„ ๊ฐ–๊ณ  ์žˆ์„ํ…๋ฐ์š”, ์ด๋Ÿฐ ์ •์ฑ…๋“ค์€ ํด๋ž˜์Šค๋กœ ๋“œ๋Ÿฌ๋‚ด์ง€ ์•Š๊ณ  ๋ฉ”์„œ๋“œ๋‚˜ ๋กœ์ง์œผ๋กœ๋งŒ ๋‚˜ํƒ€๋‚ด๋ฉด ์ •์ฑ…๋“ค์ด ๋ถ„์‚ฐ๋˜๊ณ , ์•”์‹œ์ ์œผ๋กœ ํ‘œํ˜„๋˜์–ด์„œ ์šฐ๋ฆฌ ์„œ๋น„์Šค๊ฐ€ ์–ด๋–ค ์ •์ฑ…๋“ค์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ๋ช…ํ™•ํžˆ ํŒŒ์•…ํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. (์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด์„œ ์ฝ”๋“œ๊ฐ€ ์„œ๋น„์Šค ์ •์ฑ…์„ ๋”ฐ๋ผ๊ฐ€์ง€ ๋ชปํ•˜๋Š” ์ƒํ™ฉ๋„ ์ข…์ข… ์ƒ๊น๋‹ˆ๋‹ค) DDD๋ฅผ ์•„์ง ์ฝ์–ด๋ณด์‹ ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ, [์•”์‹œ์ ์ธ ๊ฐœ๋…์„ ๋ช…ํ™•ํ•˜๊ฒŒ](https://dhsim86.github.io/programming/2019/05/16/domain_driven_design_09-post.html)๋ผ๋Š” chapter๊ฐ€ ์žˆ๋Š”๋ฐ ์‹œ๊ฐ„๋‚˜์‹ค ๋•Œ ๋งํฌ ํ•œ๋ฒˆ ์ฝ์–ด๋ณด์‹œ๊ณ , ํ˜„์žฌ ์šฐ๋ฆฌ ์„œ๋น„์Šค์— ์—ฌ๊ธฐ์ €๊ธฐ ์ˆจ์–ด์žˆ๋Š” ์ •์ฑ…๋“ค์€ ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ์„์ง€ ์ฐพ์•„๋ณด์…”์„œ ๋ช…ํ™•ํ•˜๊ฒŒ ํด๋ž˜์Šค๋กœ ๋‚˜ํƒ€๋‚ด๋ณด์‹ ๋‹ค๋ฉด ๋งŽ์€ ๋„์›€์ด ๋˜์‹ค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค :)
@@ -0,0 +1,13 @@ +package msa.customer.exception; + +public class DeliveryCustomerException extends RuntimeException { + + public DeliveryCustomerException(String message) { + super(message); + } + + public DeliveryCustomerException(String message, Throwable cause) { + super(message, cause); + } + +}
Java
์ฐธ๊ณ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,99 @@ +## ์šฐ์•„ํ•œ ํ…Œํฌ์ฝ”์Šค ํ”„๋ฆฌ์ฝ”์Šค ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ”„๋กœ๋ชจ์…˜ + +### ์ง„ํ–‰๋ฐฉ์‹ +- ์‹œ์ž‘ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ +- ๋ฐฉ๋ฌธ ๋‚ ์งœ ์ž…๋ ฅ +- ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜ ์ž…๋ ฅ +- ์ž…๋ ฅ ์š”์ผ ์ด๋ฒคํŠธ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ +- ์ด๋ฒคํŠธ ํ˜œํƒ ์ถœ๋ ฅ + - ์ฃผ๋ฌธ ๋ฉ”๋‰ด + - ํ• ์ธ ์ „ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก + - ์ฆ์ • ๋ฉ”๋‰ด ์—ฌ๋ถ€ + - ํ˜œํƒ ๋‚ด์—ญ ์—ฌ๋ถ€ + - ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก + - 12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ์—ฌ๋ถ€ + +### ์ฃผ์š” ๊ตฌํ˜„ ์‚ฌํ•ญ +- [x] ์ž…๋ ฅ ๋ทฐ ๊ตฌํ˜„ +- [x] ์ถœ๋ ฅ ๋ทฐ ๊ตฌํ˜„ + +- [x] ๋‚ ์งœ ์ž…๋ ฅ ๋ฐ ์ €์žฅ ๊ฐ์ฒด ๊ตฌํ˜„ +- [x] ๋‚ ์งœ ์ž…๋ ฅ ์‹œ ๋‚ ์งœ๊ด€๋ จ ํ•ด๋‹น ์ด๋ฒคํŠธ ํ•ญ๋ชฉ ์ €์žฅ + - [x] ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ์ด ๊ธˆ์•ก ํ• ์ธ ํ•ญ๋ชฉ + - [x] ํ‰์ผ/์ฃผ๋ง ๊ตฌ๋ถ„ ํ• ์ธ ํƒ€์ž… ํ•ญ๋ณต + - [x] ๋‚ ์งœ๋‚˜ ๋‹ฌ๋ ฅ์˜ ๋ณ„์— ๋”ฐ๋ผ ์ŠคํŽ˜์…œ ํ• ์ธ ํ•ญ๋ชฉ +- [x] ๋‚ ์งœ ๊ด€๋ จ ์ด๋ฒคํŠธ ๋ฐ˜ํ™˜ DTO ๋ฐ˜ํ™˜ + +- [x] ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜ ์ž…๋ ฅ ๋ฐ ์ €์žฅ ๊ฐ์ฒด ๊ตฌํ˜„ + - [x] ๊ฐ ๋ฉ”๋‰ด๋ฅผ ๊ธฐ๋ณธ ๊ธˆ์•ก๊ณผ ๋ฉ”๋‰ด ๋ช…์œผ๋กœ Enum ์œผ๋กœ ๊ด€๋ฆฌ + - [x] ์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด ์ž…๋ ฅ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์ง„ํ–‰ ํ›„ ์ €์žฅ ๊ฐ์ฒด์— ์ €์žฅ +- [x] ์ฃผ๋ฌธ ๋ฉ”๋‰ด ๋‚ด์—ญ ๋ฐ˜ํ™˜ DTO ๋ฐ˜ํ™˜ + +- [x] ๋‚ ์งœ ์ด๋ฒคํŠธ์™€ ์ฃผ๋ฌธ ๋ฉ”๋‰ด DTO ๋ฅผ ํ†ตํ•œ ์ด๋ฒคํŠธ ํ˜œํƒ ์ ์šฉ +- [x] ์ด๋ฒคํŠธ ์ ์šฉ DTO ๋ฐ˜ํ™˜ ๋ฐ ์ถœ๋ ฅ + - [x] ํ• ์ธ ์ „ ๊ธˆ์•ก์„ ์ถœ๋ ฅ + - [x] ํ• ์ธ ์ „ ๊ธˆ์•ก์— ๋”ฐ๋ฅธ ์ฆ์ • ์—ฌ๋ถ€ ์ถœ๋ ฅ + - ์ฆ์ • ์—ฌ๋ถ€๊ฐ€ ์—†๋‹ค๋ฉด '์—†์Œ' ์ถœ๋ ฅ + - [x] ํ˜œํƒ ๋‚ด์—ญ ์ถœ๋ ฅ + - ์ ์šฉ๋œ ํ˜œํƒ ์—†๋‹ค๋ฉด '์—†์Œ' ์ถœ๋ ฅ + - [x] ํ˜œํƒ ๊ธˆ์•ก ์ถœ๋ ฅ + - [x] ์ฆ์ • ํ˜œํƒ ๊ธˆ์•ก์„ ์ œ์™ธํ•œ ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ธˆ์•ก ์ถœ๋ ฅ + - [x] ์ด ํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ฅธ ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ๋ถ€์—ฌ ์—ฌ๋ถ€ ์ถœ๋ ฅ + - ์ด๋ฒคํŠธ ๋ฐฐ์ง€๊ฐ€ ์—†๋‹ค๋ฉด '์—†์Œ' ์ถœ๋ ฅ + +- ์ถ”๊ฐ€ ๊ตฌํ˜„ ์‚ฌํ•ญ + - ์ด๋ฒคํŠธ ๊ฒฐ๊ณผ ๋„๋ฉ”์ธ์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅ ๋ฌธ์ž์—ด์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ Builder DTO ์ƒ์„ฑ/๋ฐ˜ํ™˜ + - ์ž˜๋ชป๋œ ์ž…๋ ฅ์„ ํ†ตํ•œ ๊ณผ์ •์€ ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ ๋ฐ ์žฌ์ž…๋ ฅ + - ์ด ์ฃผ๋ฌธ ๋ฉ”๋‰ด ๊ธˆ์•ก์ด 10,000์› ์ดํ•˜์ผ ๊ฒฝ์šฐ ๋ชจ๋“  ํ• ์ธ ๊ธˆ์•ก 0์œผ๋กœ ์ง€์ • + +### ์˜ˆ์™ธ ์ฒ˜๋ฆฌ +- ๋‚ ์งœ ์ž…๋ ฅ ์˜ˆ์™ธ + - [x] ์ž˜๋ชป๋œ ๋‚ ์งœ ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ + - [x] ๋‚ ์งœ๊ฐ€ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ณ€๊ฒฝ์ด ์•ˆ๋  ๋•Œ +- ๋ฉ”๋‰ด ์ž…๋ ฅ ์˜ˆ์™ธ + - [x] ๋ฉ”๋‰ด ์ž…๋ ฅ ์‹œ ์ •๊ทœํ‘œํ˜„์‹ ํŒจํ„ด์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ (TextProcessor ์ฒ˜๋ฆฌ) + - [x] ์ค‘๋ณต ๋ฉ”๋‰ด๋ช… ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ (TextProcessor ์ฒ˜๋ฆฌ) + - [x] ์—†๋Š” ๋ฉ”๋‰ด๋ช… ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ + - [x] ์ฃผ๋ฌธ ๋ฉ”๋‰ด์˜ ๊ฐœ์ˆ˜๊ฐ€ 1๋ณด๋‹ค ์ž‘์„ ๋•Œ + - [x] ์ฃผ๋ฌธ ๋ฉ”๋‰ด๊ฐ€ 20๊ฐœ๋ฅผ ๋„˜์„ ๋•Œ + - [x] ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ์‹œ ์ฃผ๋ฌธ ๋ถˆ๊ฐ€๋Šฅ + +### ํด๋ž˜์Šค ๊ตฌ์กฐ +- InputView : ํ™”๋ฉด ์ž…๋ ฅ์„ ๋‹ด๋‹นํ•˜๋Š” ๋ทฐ + - InputViewMessage : ์ž…๋ ฅ์„ ์œ„ํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” Enum +- OutputView : ํ™”๋ฉด ์ถœ๋ ฅ์„ ๋‹ด๋‹นํ•˜๋Š” ๋ทฐ + - OutputViewMessage : ์ถœ๋ ฅ์„ ์œ„ํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” Enum +- PromotionRun : ๋ทฐ์™€ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ํด๋ž˜์Šค๋กœ ์š”์ฒญ ์ „๋‹ฌ +- PromotionController : ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์š”์ฒญ์„ ์ „๋‹ฌํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ +- PromotionService : ํ”„๋กœ๋ชจ์…˜ ์œ ํšจ์„ฑ ๊ฒ€์ฆ์„ ๊ฑฐ์นœ ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์‹คํ–‰ํ•  ์„œ๋น„์Šค +- TextProcessor : ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ํŠน์ • ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ ์„œ๋น„์Šค +- OrderMenus : ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜ ์ €์žฅํ•˜๋Š” ํด๋ž˜์Šค + - MenuItem : ๋ฉ”๋‰ด ์ด๋ฆ„๊ณผ ๊ฐ€๊ฒฉ, ๋ฉ”๋‰ด ์ข…๋ฅ˜๋ฅผ ์ €์žฅํ•˜๋Š” Enum + - MenuCategory : ๋ฉ”๋‰ด ์ข…๋ฅ˜๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” Enum +- ReservationDateEvent : ๋‚ ์งœ ๊ด€๋ จ ์ด๋ฒคํŠธ ์ €์žฅ ํด๋ž˜์Šค + - ReservationDate : ์ž…๋ ฅ๋œ ๋‚ ์งœ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค + - ChristmasDiscount : ํฌ๋ฆฌ์Šค๋งˆ์Šค D-day ํ• ์ธ ๊ด€๋ฆฌ ํด๋ž˜์Šค + - WeekDiscountType : ์ฃผ๋ง ํ• ์ธ, ํ‰์ผ ํ• ์ธ์„ ๊ตฌ๋ถ„ํ•˜๋Š” Enum + - SpecialDayDiscount : ํŠน๋ณ„ ํ• ์ธ ์ผ์ž๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” Enum +- EventResult : ์ ์šฉ ์ค‘์ธ ๋ชจ๋“  ์ด๋ฒคํŠธ๋ฅผ ์ €์žฅํ•˜๋Š” ํด๋ž˜์Šค + - DiscountDetail : ์ฃผ๋ฌธ ๊ธˆ์•ก๊ณผ ํ• ์ธ, ์˜ˆ์ƒ ๊ธˆ์•ก ๋“ฑ์„ ๋ณด๊ด€ํ•œ ํด๋ž˜์Šค + - GiftEvent : ์ฆ์ • ์ƒํ’ˆ ์—ฌ๋ถ€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” Enum + - RewardBadge : ์ด๋ฒคํŠธ ๋ฐฐ์ง€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” Enum +- PromotionException : ์ง€์ •๋œ Enum ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€๋ฅผ ํ†ตํ•ด ๊ฐ’๋ฅผ ๋‹ด์•„ IllegalArgumentException ์ƒ์„ฑ + - ExceptionMessage : ์˜ˆ์™ธ ๋ฐœ์ƒ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๊ด€ํ•œ Enum Class +- PromotionConverter : Service ์—์„œ Domain to DTO ์ปจ๋ฒ„ํ„ฐ +- EventResultTextFactory : EventResultDTO ๋ฐ˜ํ™˜์„ ์œ„ํ•œ ๋ฌธ์ž์—ด ์กฐ์ž‘ ์„œ๋น„์Šค + - EventResultText : EventResultDTO ์— ํ•„์š”ํ•œ ๋ฌธ์ž์—ด Enum +- ํ…Œ์ŠคํŠธ : ํด๋ž˜์Šค๋ณ„ ๋ฉ”์†Œ๋“œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์ง„ํ–‰ + +### ์ด๋ฒˆ์— ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๊ณ ๋ฏผํ•œ ์š”์†Œ +- ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๊ตฌ์กฐ ์„ค๊ณ„ + - ์ดํ›„ ํ•„์š”ํ•œ ํด๋ž˜์Šค๋“ค์„ ์ถ”๊ฐ€ํ•˜๋ฉฐ ์„ค๊ณ„ ๋ณ€๊ฒฝ +- ๋™์ผํ•œ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ํ•˜๋Š” ๋ทฐ๋Š” ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๊ตฌํ˜„ +- ์ž…๋ ฅ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์™€ ๊ฐ์ฒด ์ƒ์„ฑ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๊ตฌ๋ถ„์— ๋Œ€ํ•œ ๊ณ ๋ฏผ +- ์ด๋ฒคํŠธ ์ ์šฉ ์‹œ ์ด๋ฒคํŠธ ๋‚ด์—ญ๊ณผ ์ฃผ๋ฌธ ๋‚ด์—ญ์„ ์‚ฌ์šฉํ•ด ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ Service ์—์„œ ์ง„ํ–‰ํ• ์ง€ ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ ์ง„ํ–‰ํ• ์ง€ ๊ณ ๋ฏผ +- ์ถœ๋ ฅ์„ ์œ„ํ•œ DTO ์ƒ์„ฑ์— ๋Œ€ํ•œ ๊ณ ๋ฏผ + - DTO ๋ฃฐ ์ถœ๋ ฅํ•˜๊ณ  ํ•ด๋‹น DTO ํ†ตํ•ด ๋‹ค์‹œ ์ด๋ฒคํŠธ ๋‚ด์—ญ ํ™•์ธํ•˜๋Š”๋ฐ ์žˆ์–ด DTO ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• + - EventResultDTO ๋ทฐ์— ์ถœ๋ ฅํ•  ๋ฌธ์ž์—ด์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ DTO ์ƒ์„ฑ ๋ฐฉ๋ฒ• ๊ณ ๋ฏผ ํ›„ Builder, EventResultTextFactory ์‚ฌ์šฉ +- ์ž˜๋ชป๋œ ์ž…๋ ฅ์— ๋Œ€ํ•œ ๋ฉ”์„œ๋“œ ์žฌ์‹คํ–‰ ๋กœ์ง์€ ์ง€๋‚œ์ฃผ [zangsu๋‹˜](https://github.com/zangsu) ์ฝ”๋“œ๋ฆฌ๋ทฐ๋กœ ๋ฐฐ์šด Supplier ๋ฐ˜๋ณต ์‚ฌ์šฉ +- ํ…Œ์ŠคํŠธ ์ฝ”๋“œ mock ๊ฐ์ฒด์— ๋Œ€ํ•œ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๊ณ ๋ฏผ \ No newline at end of file
Unknown
README์— ํด๋ž˜์Šค์ด๋ฆ„์„ ์ž‘์„ฑํ•˜๊ธฐ๋ณด๋‹ค ์–ด๋–ค ์—ญํ• ์˜ ํด๋ž˜์Šค์ธ์ง€ ์ •๋„๋งŒ ์ž‘์„ฑํ•˜์‹œ๋Š”๊ฒŒ ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ์•„๋ฌด๋ž˜๋„ ํด๋ž˜์Šค์ด๋ฆ„์ด๋‚˜ ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์€ ๊ฐ€๋ณ€์„ฑ์ด ๋†’๋‹ค๋ณด๋‹ˆ README๋„ ์ฃผ๊ธฐ์ ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•ด์•ผํ•ด์„œ ๋ฒˆ๊ฑฐ๋กœ์›€์ด ํฌ๊ฑฐ๋“ ์š”
@@ -0,0 +1,20 @@ +package christmas.exception; + +public enum ExceptionMessage { + INVALID_INPUT_DATE("์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + INVALID_INPUT_ORDER("์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + INVALID_MENU_ITEM("ํ•ด๋‹น ๋ฉ”๋‰ด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."), + INVALID_WEEK_DISCOUNT_TYPE("์ž˜๋ชป๋œ ์ฃผ๊ฐ„ ํ• ์ธ ํƒ€์ž…์ž…๋‹ˆ๋‹ค."), + ; + + private static final String PREFIX = "[ERROR] "; + private final String errorMessage; + + ExceptionMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public String getErrorMessage() { + return PREFIX + errorMessage; + } +}
Java
์ถ”๊ฐ€๋  enum์„ ๊ณ ๋ คํ•˜์…”์„œ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•œ ์˜๋„๋ผ๊ณ  ์ƒ๊ฐ์€ ๋˜์ง€๋งŒ, ๊ผผ๊ผผํ•˜๊ณ  ํŒŒ๊ณ ๋“ค์ž๋ฉด ,;๋ณด๋‹ค ;๋กœ ๋๋‚ด๋Š”๊ฒŒ ๋” ์ข‹์ง€ ์•Š์„๊นŒ์š”?? ๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค ใ…Žใ…Žใ…Ž
@@ -1,7 +1,20 @@ package christmas; + +import camp.nextstep.edu.missionutils.Console; +import christmas.run.PromotionRun; +import christmas.view.InputView; +import christmas.view.OutputView; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + try { + InputView inputView = SingletonView.getInputView(); + OutputView outputView = SingletonView.getOutputView(); + PromotionRun promotionRun = new PromotionRun(inputView, outputView); + promotionRun.runPromotion(); + } finally { + Console.close(); + } } }
Java
๋ทฐ๋ฅผ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๊ด€๋ฆฌํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,20 @@ +package christmas.exception; + +public enum ExceptionMessage { + INVALID_INPUT_DATE("์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + INVALID_INPUT_ORDER("์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + INVALID_MENU_ITEM("ํ•ด๋‹น ๋ฉ”๋‰ด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."), + INVALID_WEEK_DISCOUNT_TYPE("์ž˜๋ชป๋œ ์ฃผ๊ฐ„ ํ• ์ธ ํƒ€์ž…์ž…๋‹ˆ๋‹ค."), + ; + + private static final String PREFIX = "[ERROR] "; + private final String errorMessage; + + ExceptionMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public String getErrorMessage() { + return PREFIX + errorMessage; + } +}
Java
์ถ”๊ฐ€์ ์œผ๋กœ ์š”๊ตฌ์‚ฌํ•ญ์— ์—๋Ÿฌ๋ฉ”์‹œ์ง€๋Š” "[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."์œผ๋กœ ํ†ต์ผํ•˜๋ผ๊ณ  ๊ธฐ์žฌ๋˜์–ด ์žˆ์–ด ์ตœ์ข…์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋กœ ๊ฐ€์‹œ๊ฒŒ ๋˜๋ฉด ๊ผผ๊ผผํžˆ ๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,33 @@ +package christmas.model.event; + +import christmas.model.date.ReservationDate; + +public class ChristmasDiscount { + private static final int FIRST_DAY = 1; + private static final int CHRISTMAS_DAY = 25; + private static final int BASIC_DISCOUNT = 1000; + private static final int PLUS_DISCOUNT = 100; + private static final int NONE_DISCOUNT = 0; + + private final int discount; + + public ChristmasDiscount(ReservationDate date) { + this.discount = initDiscountAmount(date); + } + + private int initDiscountAmount(ReservationDate date) { + if (date.day() > CHRISTMAS_DAY) { + return NONE_DISCOUNT; + } + + return BASIC_DISCOUNT + getPlusDiscount(date.day()); + } + + private int getPlusDiscount(int day) { + return (day - FIRST_DAY) * PLUS_DISCOUNT; + } + + public int getDiscount() { + return discount; + } +}
Java
ReservationDate์—์„œ ์Šค์Šค๋กœ ์ž์‹ ์ด ๊ฐ€์ง„ ๊ฐ’์—๋Œ€ํ•ด ๊ฒ€์ฆํ•˜๋„๋ก ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์‹œ๋Š” ๊ฒƒ์ด ๋” ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. date.isChristmasDay() ์™€ ๊ฐ™์ด ๋ง์ด์ฃ !! 'getter๋ฅผ ์ง€์–‘ํ•˜๋ผ'๋Š” ์˜๋ฏธ๋Š” VO์—๊ฒŒ๋„ ๋™์ผํ•˜๊ฒŒ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ด€๋ จ ๋ฌธ์„œ๋ฅผ ์•„๋ž˜ ๋งํฌ๋กœ ๋‹ฌ์•„๋‘˜๊ฒŒ์š”! https://tecoble.techcourse.co.kr/post/2020-04-28-ask-instead-of-getter/
@@ -0,0 +1,62 @@ +package christmas.model.event; + +import christmas.model.event.dto.ReservationDateEventDTO; + +public class DiscountDetail { + private final int totalPriceBeforeDiscount; + private final int totalPriceAfterDiscount; + private final WeekDiscountType weekDiscountType; + private final int christmasDiscount; + private final int weekTypeDiscount; + private final int specialDiscount; + + protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount, int weekTypeDiscount) { + this.totalPriceBeforeDiscount = totalPriceBeforeDiscount; + this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + this.christmasDiscount = dateEventDTO.getChristmasDiscount(); + this.specialDiscount = dateEventDTO.getSpecialDiscount(); + this.weekTypeDiscount = weekTypeDiscount; + this.totalPriceAfterDiscount = initTotalPriceAfterDiscount(); + } + + protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount) { + this.totalPriceBeforeDiscount = totalPriceBeforeDiscount; + this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + this.christmasDiscount = 0; + this.specialDiscount = 0; + this.weekTypeDiscount = 0; + this.totalPriceAfterDiscount = initTotalPriceAfterDiscount(); + } + + private int initTotalPriceAfterDiscount() { + return totalPriceBeforeDiscount - getTotalDiscount(); + } + + public int getTotalPriceBeforeDiscount() { + return totalPriceBeforeDiscount; + } + + public int getTotalPriceAfterDiscount() { + return totalPriceAfterDiscount; + } + + public int getChristmasDiscount() { + return christmasDiscount; + } + + public int getSpecialDiscount() { + return specialDiscount; + } + + public int getWeekTypeDiscount() { + return weekTypeDiscount; + } + + public WeekDiscountType getWeekDiscountType() { + return weekDiscountType; + } + + public int getTotalDiscount() { + return christmasDiscount + weekTypeDiscount + specialDiscount; + } +}
Java
DiscountDetil์˜ ํ•„๋“œ๋กœ ์ด ๊ฐ’๋“ค์„ ๊ตณ์ด ๊ฐ€์ ธ์•ผ ํ•  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? ์žฌ์‚ฌ์šฉ์˜ ์—ฌ์ง€๊ฐ€ ์—†๋‹ค๋ฉด ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ด ๋” ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ด์š”!
@@ -0,0 +1,62 @@ +package christmas.model.event; + +import christmas.model.event.dto.ReservationDateEventDTO; + +public class DiscountDetail { + private final int totalPriceBeforeDiscount; + private final int totalPriceAfterDiscount; + private final WeekDiscountType weekDiscountType; + private final int christmasDiscount; + private final int weekTypeDiscount; + private final int specialDiscount; + + protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount, int weekTypeDiscount) { + this.totalPriceBeforeDiscount = totalPriceBeforeDiscount; + this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + this.christmasDiscount = dateEventDTO.getChristmasDiscount(); + this.specialDiscount = dateEventDTO.getSpecialDiscount(); + this.weekTypeDiscount = weekTypeDiscount; + this.totalPriceAfterDiscount = initTotalPriceAfterDiscount(); + } + + protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount) { + this.totalPriceBeforeDiscount = totalPriceBeforeDiscount; + this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + this.christmasDiscount = 0; + this.specialDiscount = 0; + this.weekTypeDiscount = 0; + this.totalPriceAfterDiscount = initTotalPriceAfterDiscount(); + } + + private int initTotalPriceAfterDiscount() { + return totalPriceBeforeDiscount - getTotalDiscount(); + } + + public int getTotalPriceBeforeDiscount() { + return totalPriceBeforeDiscount; + } + + public int getTotalPriceAfterDiscount() { + return totalPriceAfterDiscount; + } + + public int getChristmasDiscount() { + return christmasDiscount; + } + + public int getSpecialDiscount() { + return specialDiscount; + } + + public int getWeekTypeDiscount() { + return weekTypeDiscount; + } + + public WeekDiscountType getWeekDiscountType() { + return weekDiscountType; + } + + public int getTotalDiscount() { + return christmasDiscount + weekTypeDiscount + specialDiscount; + } +}
Java
์™ธ๋ถ€ ์ƒ์„ฑ์„ ๋ง‰๊ธฐ ์œ„ํ•ด ์ƒ์„ฑ์ž๋ฅผ protected๋กœ ๊ด€๋ฆฌํ•˜์‹  ๊ฒƒ์œผ๋กœ ๋ณด์ด๋Š”๋ฐ, private์œผ๋กœ ์ƒ์„ฑ์ž๋ฅผ ๋ง‰๊ณ  ์ •์ ํŒฉํ† ๋ฆฌ๋ฉ”์†Œ๋“œ ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์—„๋ฐ€ํ•˜๊ฒŒ ์บก์Аํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๊ด€๋ จ ์ž๋ฃŒ๋ฅผ ์•„๋ž˜ ๋งํฌ๋กœ ๋‹ฌ์•„๋‘˜๊ฒŒ์š”! https://hudi.blog/effective-java-static-factory-method/
@@ -0,0 +1,91 @@ +package christmas.model.event; + +import christmas.model.event.dto.ReservationDateEventDTO; +import christmas.model.menu.MenuItem; + +import java.util.*; +import java.util.stream.Stream; + +public class EventResult { + private static final int MIN_EVENT_AMOUNT = 10_000; + private static final int GIFT_AMOUNT = 120_000; + + private final Map<MenuItem, Integer> orderMenus; + private final DiscountDetail discountDetail; + private final GiftMenu giftMenu; + private final RewardBadge rewardBadge; + + public EventResult(ReservationDateEventDTO dateEventDTO, Map<MenuItem, Integer> orderMenus) { + this.orderMenus = orderMenus; + this.discountDetail = initDiscountDetail(dateEventDTO); + this.giftMenu = initGiftMenu(); + this.rewardBadge = initRewardBadge(); + } + + private DiscountDetail initDiscountDetail(ReservationDateEventDTO dateEventDTO) { + int totalPriceBeforeDiscount = getTotalPriceBeforeDiscount(); + if (totalPriceBeforeDiscount < MIN_EVENT_AMOUNT) { + return new DiscountDetail(dateEventDTO, totalPriceBeforeDiscount); + } + int weekDiscount = calculateWeekDiscount(dateEventDTO); + + return new DiscountDetail(dateEventDTO, totalPriceBeforeDiscount, weekDiscount); + } + + private GiftMenu initGiftMenu() { + if (discountDetail.getTotalPriceBeforeDiscount() < GIFT_AMOUNT) { + return GiftMenu.NONE; + } + + return GiftMenu.CHAMPAGNE; + } + + private RewardBadge initRewardBadge() { + int benefit = discountDetail.getTotalDiscount() + giftMenu.getGiftPrice(); + + return Arrays.stream(RewardBadge.values()) + .filter(badge -> + benefit >= badge.getBenefit()) + .max(Comparator.comparingInt(RewardBadge::getBenefit)) + .orElse(RewardBadge.NONE); + } + + private int getTotalPriceBeforeDiscount() { + return generateOrderMenuEntry(orderMenus) + .mapToInt(menu -> + menu.getKey().getItemPrice() * menu.getValue()) + .sum(); + } + + private int calculateWeekDiscount(ReservationDateEventDTO dateEventDTO) { + WeekDiscountType type = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + + return generateOrderMenuEntry(orderMenus) + .filter(menu -> + menu.getKey().getMenuCategory() == type.getDiscountCategory()) + .mapToInt(menu -> + menu.getValue() * type.getDiscountPrice()) + .sum(); + } + + private <K, V> Stream<Map.Entry<K, V>> generateOrderMenuEntry(Map<K, V> orderMenus) { + return orderMenus.entrySet() + .stream(); + } + + public Map<MenuItem, Integer> getOrderMenus() { + return Collections.unmodifiableMap(orderMenus); + } + + public DiscountDetail getDiscountDetail() { + return discountDetail; + } + + public GiftMenu getGiftMenu() { + return giftMenu; + } + + public RewardBadge getRewardBadge() { + return rewardBadge; + } +}
Java
ํ•„๋“œ ์ˆ˜๋ฅผ ์ค„์—ฌ ์‘์ง‘๋„๋ฅผ ๋‚ฎ์ถ”๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฒ„๊ทธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋ฆฌํŽ™ํ† ๋งํ• ๋ถ€๋ถ„์ด ๋งŽ์•„์ ธ์š”!!
@@ -0,0 +1,91 @@ +package christmas.model.event; + +import christmas.model.event.dto.ReservationDateEventDTO; +import christmas.model.menu.MenuItem; + +import java.util.*; +import java.util.stream.Stream; + +public class EventResult { + private static final int MIN_EVENT_AMOUNT = 10_000; + private static final int GIFT_AMOUNT = 120_000; + + private final Map<MenuItem, Integer> orderMenus; + private final DiscountDetail discountDetail; + private final GiftMenu giftMenu; + private final RewardBadge rewardBadge; + + public EventResult(ReservationDateEventDTO dateEventDTO, Map<MenuItem, Integer> orderMenus) { + this.orderMenus = orderMenus; + this.discountDetail = initDiscountDetail(dateEventDTO); + this.giftMenu = initGiftMenu(); + this.rewardBadge = initRewardBadge(); + } + + private DiscountDetail initDiscountDetail(ReservationDateEventDTO dateEventDTO) { + int totalPriceBeforeDiscount = getTotalPriceBeforeDiscount(); + if (totalPriceBeforeDiscount < MIN_EVENT_AMOUNT) { + return new DiscountDetail(dateEventDTO, totalPriceBeforeDiscount); + } + int weekDiscount = calculateWeekDiscount(dateEventDTO); + + return new DiscountDetail(dateEventDTO, totalPriceBeforeDiscount, weekDiscount); + } + + private GiftMenu initGiftMenu() { + if (discountDetail.getTotalPriceBeforeDiscount() < GIFT_AMOUNT) { + return GiftMenu.NONE; + } + + return GiftMenu.CHAMPAGNE; + } + + private RewardBadge initRewardBadge() { + int benefit = discountDetail.getTotalDiscount() + giftMenu.getGiftPrice(); + + return Arrays.stream(RewardBadge.values()) + .filter(badge -> + benefit >= badge.getBenefit()) + .max(Comparator.comparingInt(RewardBadge::getBenefit)) + .orElse(RewardBadge.NONE); + } + + private int getTotalPriceBeforeDiscount() { + return generateOrderMenuEntry(orderMenus) + .mapToInt(menu -> + menu.getKey().getItemPrice() * menu.getValue()) + .sum(); + } + + private int calculateWeekDiscount(ReservationDateEventDTO dateEventDTO) { + WeekDiscountType type = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + + return generateOrderMenuEntry(orderMenus) + .filter(menu -> + menu.getKey().getMenuCategory() == type.getDiscountCategory()) + .mapToInt(menu -> + menu.getValue() * type.getDiscountPrice()) + .sum(); + } + + private <K, V> Stream<Map.Entry<K, V>> generateOrderMenuEntry(Map<K, V> orderMenus) { + return orderMenus.entrySet() + .stream(); + } + + public Map<MenuItem, Integer> getOrderMenus() { + return Collections.unmodifiableMap(orderMenus); + } + + public DiscountDetail getDiscountDetail() { + return discountDetail; + } + + public GiftMenu getGiftMenu() { + return giftMenu; + } + + public RewardBadge getRewardBadge() { + return rewardBadge; + } +}
Java
์ด ๋ถ€๋ถ„๋„ getter๋กœ ๊บผ๋‚ด์™€์„œ ๊ฒ€์ฆํ•˜๊ธฐ๋ณด๋‹ค ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ ์Šค์Šค๋กœ๊ฒ€์ฆํ•˜๋„๋ก ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,28 @@ +package christmas.model.event; + +public enum SpecialDayDiscount { + THIRD_DAY(3, 1000), + TENTH_DAY(10, 1000), + SEVENTEENTH_DAY(17, 1000), + TWENTY_FOURTH_DAY(24, 1000), + TWENTY_FIFTH_DAY(25, 1000), + THIRTY_FIRST_DAY(31, 1000), + OTHER_DAY(0, 0), + ; + + private final int day; + private final int discountPrice; + + SpecialDayDiscount(int day, int discountPrice) { + this.day = day; + this.discountPrice = discountPrice; + } + + public int getDay() { + return day; + } + + public int getDiscountPrice() { + return discountPrice; + } +}
Java
1000์›์ด๋ผ๋Š” ๋™์ผํ•œ ํ• ์ธ๊ธˆ์•ก์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋‹ˆ List๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,55 @@ +package christmas.model.menu; + +import christmas.exception.ExceptionMessage; +import christmas.exception.PromotionException; + +import java.util.Arrays; + +public enum MenuItem { + APPETIZER_MUSHROOM_SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6_000, MenuCategory.APPETIZER), + APPETIZER_TAPAS("ํƒ€ํŒŒ์Šค", 5_500, MenuCategory.APPETIZER), + APPETIZER_CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000, MenuCategory.APPETIZER), + + MAIN_T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000, MenuCategory.MAIN_COURSE), + MAIN_BBQ_RIB("๋ฐ”๋น„ํ๋ฆฝ", 54_000, MenuCategory.MAIN_COURSE), + MAIN_SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000, MenuCategory.MAIN_COURSE), + MAIN_CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000, MenuCategory.MAIN_COURSE), + + DESSERT_CHOCO_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15_000, MenuCategory.DESSERT), + DESSERT_ICE_CREAM("์•„์ด์Šคํฌ๋ฆผ", 5_000, MenuCategory.DESSERT), + + BEVERAGE_ZERO_COLA("์ œ๋กœ์ฝœ๋ผ", 3_000, MenuCategory.BEVERAGE), + BEVERAGE_RED_WINE("๋ ˆ๋“œ์™€์ธ", 60_000, MenuCategory.BEVERAGE), + BEVERAGE_CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, MenuCategory.BEVERAGE), + ; + + private final String itemName; + private final int itemPrice; + private final MenuCategory menuCategory; + + MenuItem(String itemName, int itemPrice, MenuCategory menuCategory) { + this.itemName = itemName; + this.itemPrice = itemPrice; + this.menuCategory = menuCategory; + } + + public static MenuItem findMenuItemByName(String itemName, ExceptionMessage message) { + return Arrays.stream(MenuItem.values()) + .filter(menu -> menu.getItemName().equals(itemName)) + .findFirst() + .orElseThrow(() -> + new PromotionException(message)); + } + + public String getItemName() { + return itemName; + } + + public int getItemPrice() { + return itemPrice; + } + + public MenuCategory getMenuCategory() { + return menuCategory; + } +}
Java
MenuCategory, MenuItem์—์„œ ์ค‘๋ณต๋˜๋Š” ์š”์†Œ๋“ค์ด ์žˆ๋Š”๋ฐ ํ•˜๋‚˜๋กœ ๋ฌถ์–ด๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,79 @@ +package christmas.model.menu; + +import christmas.exception.ExceptionMessage; +import christmas.exception.PromotionException; + +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class OrderMenus { + private static final int MIN_ORDER_MENU_NUMBER = 1; + private static final int MAX_ORDER_MENUS_NUMBER = 20; + + private final Map<MenuItem, Integer> orderMenus; + + public OrderMenus(Map<String, Integer> orderMenus) { + this.orderMenus = validateOrderMenusByNames(orderMenus); + validateOrderMenus(); + } + + private Map<MenuItem, Integer> validateOrderMenusByNames(Map<String, Integer> orderMenus) { + return generateOrderMenuValues(orderMenus) + .collect(Collectors.toUnmodifiableMap( + entry -> MenuItem.findMenuItemByName(entry.getKey(), ExceptionMessage.INVALID_INPUT_ORDER), + Map.Entry::getValue + )); + } + + private void validateOrderMenus() { + validateOrderNumbers(); + validateTotalOrderNumbers(); + validateOrderAllBeverage(); + } + + private void validateOrderNumbers() { + generateOrderMenuValues(orderMenus) + .filter(menu -> + menu.getValue() < MIN_ORDER_MENU_NUMBER) + .findAny() + .ifPresent(menuName -> throwInvalidOrderException()); + } + + private void validateTotalOrderNumbers() { + int orderNumber = generateOrderMenuValues(orderMenus) + .mapToInt(Map.Entry::getValue) + .sum(); + + if (orderNumber > MAX_ORDER_MENUS_NUMBER) { + throwInvalidOrderException(); + } + } + + private void validateOrderAllBeverage() { + boolean allBeverage = generateOrderMenuValues(orderMenus) + .allMatch(orderMenu -> + orderMenu.getKey().getMenuCategory() == MenuCategory.BEVERAGE); + + if (allBeverage) { + throwInvalidOrderException(); + } + } + + private <K, V> Stream<Map.Entry<K, V>> generateOrderMenuValues(Map<K, V> orderMenus) { + return orderMenus.entrySet() + .stream(); + } + + private void throwInvalidOrderException() { + throw new PromotionException(ExceptionMessage.INVALID_INPUT_ORDER); + } + + public Map<String, Integer> getOrderMenus() { + Map<String, Integer> menuNames = new HashMap<>(); + orderMenus.forEach((menuItem, quantity) -> + menuNames.put(menuItem.getItemName(), quantity)); + + return Collections.unmodifiableMap(menuNames); + } +}
Java
Integer๋„ ์›์‹œ๊ฐ’์„ ํฌ์žฅํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”??
@@ -0,0 +1,76 @@ +package christmas.run; + +import christmas.controller.PromotionController; +import christmas.exception.PromotionException; +import christmas.model.event.dto.EventResultDTO; +import christmas.model.event.dto.ReservationDateEventDTO; +import christmas.model.menu.dto.OrderMenusDTO; +import christmas.view.InputView; +import christmas.view.OutputView; + +import java.util.function.Supplier; + +public class PromotionRun { + private final PromotionController controller = new PromotionController(); + private final InputView inputView; + private final OutputView outputView; + + public PromotionRun(InputView inputView, OutputView outputView) { + this.inputView = inputView; + this.outputView = outputView; + } + + public void runPromotion() { + ReservationDateEventDTO dateEventDto = inputCorrectReservationDate(); + OrderMenusDTO orderMenusDto = inputCorrectOrderMenus(); + displayEventPreview(dateEventDto); + + EventResultDTO responseDto = applyEventsAndGenerateResult(dateEventDto, orderMenusDto); + outputEventResult(responseDto); + } + + private ReservationDateEventDTO inputCorrectReservationDate() { + outputView.displayStartPromotion(); + + return getCorrectResult(this::generateDateEvent); + } + + private OrderMenusDTO inputCorrectOrderMenus() { + return getCorrectResult(this::receiveMenus); + } + + private ReservationDateEventDTO generateDateEvent() { + String inputDate = inputView.inputDate(); + + return controller.generateEvent(inputDate); + } + + private OrderMenusDTO receiveMenus() { + String inputMenus = inputView.inputOrderMenus(); + + return controller.receiveOrderMenus(inputMenus); + } + + private void displayEventPreview(ReservationDateEventDTO dateEventDto) { + outputView.displayPreviewEvent(dateEventDto.getDay()); + } + + private EventResultDTO applyEventsAndGenerateResult(ReservationDateEventDTO dateEventDTO, + OrderMenusDTO orderMenusDto) { + return controller.applyEvents(dateEventDTO, orderMenusDto); + } + + private void outputEventResult(EventResultDTO responseDto) { + outputView.outputEventResult(responseDto); + } + + private <T> T getCorrectResult(Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (PromotionException e) { + outputView.displayException(e); + } + } + } +}
Java
ํ•„๋“œ์ฃผ์ž…๋ณด๋‹ค ์ƒ์„ฑ์ž ์ฃผ์ž…์ด ์œ ์ง€๋ณด์ˆ˜ ๊ด€์ ์—์„œ ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.!!
@@ -0,0 +1,76 @@ +package christmas.run; + +import christmas.controller.PromotionController; +import christmas.exception.PromotionException; +import christmas.model.event.dto.EventResultDTO; +import christmas.model.event.dto.ReservationDateEventDTO; +import christmas.model.menu.dto.OrderMenusDTO; +import christmas.view.InputView; +import christmas.view.OutputView; + +import java.util.function.Supplier; + +public class PromotionRun { + private final PromotionController controller = new PromotionController(); + private final InputView inputView; + private final OutputView outputView; + + public PromotionRun(InputView inputView, OutputView outputView) { + this.inputView = inputView; + this.outputView = outputView; + } + + public void runPromotion() { + ReservationDateEventDTO dateEventDto = inputCorrectReservationDate(); + OrderMenusDTO orderMenusDto = inputCorrectOrderMenus(); + displayEventPreview(dateEventDto); + + EventResultDTO responseDto = applyEventsAndGenerateResult(dateEventDto, orderMenusDto); + outputEventResult(responseDto); + } + + private ReservationDateEventDTO inputCorrectReservationDate() { + outputView.displayStartPromotion(); + + return getCorrectResult(this::generateDateEvent); + } + + private OrderMenusDTO inputCorrectOrderMenus() { + return getCorrectResult(this::receiveMenus); + } + + private ReservationDateEventDTO generateDateEvent() { + String inputDate = inputView.inputDate(); + + return controller.generateEvent(inputDate); + } + + private OrderMenusDTO receiveMenus() { + String inputMenus = inputView.inputOrderMenus(); + + return controller.receiveOrderMenus(inputMenus); + } + + private void displayEventPreview(ReservationDateEventDTO dateEventDto) { + outputView.displayPreviewEvent(dateEventDto.getDay()); + } + + private EventResultDTO applyEventsAndGenerateResult(ReservationDateEventDTO dateEventDTO, + OrderMenusDTO orderMenusDto) { + return controller.applyEvents(dateEventDTO, orderMenusDto); + } + + private void outputEventResult(EventResultDTO responseDto) { + outputView.outputEventResult(responseDto); + } + + private <T> T getCorrectResult(Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (PromotionException e) { + outputView.displayException(e); + } + } + } +}
Java
์ œ๋„ˆ๋ฆญ์„ ์‚ฌ์šฉํ•˜์‹  ๋ถ€๋ถ„์ด ์ธ์ƒ๊นŠ์Šต๋‹ˆ๋‹ค.!
@@ -0,0 +1,27 @@ +package christmas.util; + +enum EventResultText { + ORDER_OUTPUT_REGEX("%s %d%s"), + EMPTY_TEXT(""), + SPACE(" "), + MENU_NUMBER("๊ฐœ"), + MENU_PRICE_UNIT("์›"), + CHRISTMAS_D_DAY_DISCOUNT("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"), + SPECIAL_DISCOUNT("ํŠน๋ณ„ ํ• ์ธ"), + GIFT_EVENT("์ฆ์ • ์ด๋ฒคํŠธ"), + NONE_BENEFIT("์—†์Œ"), + DISCOUNT_PRICE("-"), + BENEFIT_SEPARATOR(": "), + NEW_LINE("\n"), + ; + + private final String text; + + EventResultText(String text) { + this.text = text; + } + + public String getText() { + return text; + } +}
Java
์—ฐ๊ด€์„ฑ์ด ์—†๋Š” ์ƒ์ˆ˜๋“ค์€ static final๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋” ๊ฐ€๋…์„ฑ ์ธก๋ฉด์—์„œ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ด์š” !
@@ -0,0 +1,99 @@ +## ์šฐ์•„ํ•œ ํ…Œํฌ์ฝ”์Šค ํ”„๋ฆฌ์ฝ”์Šค ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ”„๋กœ๋ชจ์…˜ + +### ์ง„ํ–‰๋ฐฉ์‹ +- ์‹œ์ž‘ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ +- ๋ฐฉ๋ฌธ ๋‚ ์งœ ์ž…๋ ฅ +- ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜ ์ž…๋ ฅ +- ์ž…๋ ฅ ์š”์ผ ์ด๋ฒคํŠธ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ +- ์ด๋ฒคํŠธ ํ˜œํƒ ์ถœ๋ ฅ + - ์ฃผ๋ฌธ ๋ฉ”๋‰ด + - ํ• ์ธ ์ „ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก + - ์ฆ์ • ๋ฉ”๋‰ด ์—ฌ๋ถ€ + - ํ˜œํƒ ๋‚ด์—ญ ์—ฌ๋ถ€ + - ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก + - 12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ์—ฌ๋ถ€ + +### ์ฃผ์š” ๊ตฌํ˜„ ์‚ฌํ•ญ +- [x] ์ž…๋ ฅ ๋ทฐ ๊ตฌํ˜„ +- [x] ์ถœ๋ ฅ ๋ทฐ ๊ตฌํ˜„ + +- [x] ๋‚ ์งœ ์ž…๋ ฅ ๋ฐ ์ €์žฅ ๊ฐ์ฒด ๊ตฌํ˜„ +- [x] ๋‚ ์งœ ์ž…๋ ฅ ์‹œ ๋‚ ์งœ๊ด€๋ จ ํ•ด๋‹น ์ด๋ฒคํŠธ ํ•ญ๋ชฉ ์ €์žฅ + - [x] ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ์ด ๊ธˆ์•ก ํ• ์ธ ํ•ญ๋ชฉ + - [x] ํ‰์ผ/์ฃผ๋ง ๊ตฌ๋ถ„ ํ• ์ธ ํƒ€์ž… ํ•ญ๋ณต + - [x] ๋‚ ์งœ๋‚˜ ๋‹ฌ๋ ฅ์˜ ๋ณ„์— ๋”ฐ๋ผ ์ŠคํŽ˜์…œ ํ• ์ธ ํ•ญ๋ชฉ +- [x] ๋‚ ์งœ ๊ด€๋ จ ์ด๋ฒคํŠธ ๋ฐ˜ํ™˜ DTO ๋ฐ˜ํ™˜ + +- [x] ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜ ์ž…๋ ฅ ๋ฐ ์ €์žฅ ๊ฐ์ฒด ๊ตฌํ˜„ + - [x] ๊ฐ ๋ฉ”๋‰ด๋ฅผ ๊ธฐ๋ณธ ๊ธˆ์•ก๊ณผ ๋ฉ”๋‰ด ๋ช…์œผ๋กœ Enum ์œผ๋กœ ๊ด€๋ฆฌ + - [x] ์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด ์ž…๋ ฅ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์ง„ํ–‰ ํ›„ ์ €์žฅ ๊ฐ์ฒด์— ์ €์žฅ +- [x] ์ฃผ๋ฌธ ๋ฉ”๋‰ด ๋‚ด์—ญ ๋ฐ˜ํ™˜ DTO ๋ฐ˜ํ™˜ + +- [x] ๋‚ ์งœ ์ด๋ฒคํŠธ์™€ ์ฃผ๋ฌธ ๋ฉ”๋‰ด DTO ๋ฅผ ํ†ตํ•œ ์ด๋ฒคํŠธ ํ˜œํƒ ์ ์šฉ +- [x] ์ด๋ฒคํŠธ ์ ์šฉ DTO ๋ฐ˜ํ™˜ ๋ฐ ์ถœ๋ ฅ + - [x] ํ• ์ธ ์ „ ๊ธˆ์•ก์„ ์ถœ๋ ฅ + - [x] ํ• ์ธ ์ „ ๊ธˆ์•ก์— ๋”ฐ๋ฅธ ์ฆ์ • ์—ฌ๋ถ€ ์ถœ๋ ฅ + - ์ฆ์ • ์—ฌ๋ถ€๊ฐ€ ์—†๋‹ค๋ฉด '์—†์Œ' ์ถœ๋ ฅ + - [x] ํ˜œํƒ ๋‚ด์—ญ ์ถœ๋ ฅ + - ์ ์šฉ๋œ ํ˜œํƒ ์—†๋‹ค๋ฉด '์—†์Œ' ์ถœ๋ ฅ + - [x] ํ˜œํƒ ๊ธˆ์•ก ์ถœ๋ ฅ + - [x] ์ฆ์ • ํ˜œํƒ ๊ธˆ์•ก์„ ์ œ์™ธํ•œ ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ธˆ์•ก ์ถœ๋ ฅ + - [x] ์ด ํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ฅธ ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ๋ถ€์—ฌ ์—ฌ๋ถ€ ์ถœ๋ ฅ + - ์ด๋ฒคํŠธ ๋ฐฐ์ง€๊ฐ€ ์—†๋‹ค๋ฉด '์—†์Œ' ์ถœ๋ ฅ + +- ์ถ”๊ฐ€ ๊ตฌํ˜„ ์‚ฌํ•ญ + - ์ด๋ฒคํŠธ ๊ฒฐ๊ณผ ๋„๋ฉ”์ธ์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅ ๋ฌธ์ž์—ด์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ Builder DTO ์ƒ์„ฑ/๋ฐ˜ํ™˜ + - ์ž˜๋ชป๋œ ์ž…๋ ฅ์„ ํ†ตํ•œ ๊ณผ์ •์€ ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ ๋ฐ ์žฌ์ž…๋ ฅ + - ์ด ์ฃผ๋ฌธ ๋ฉ”๋‰ด ๊ธˆ์•ก์ด 10,000์› ์ดํ•˜์ผ ๊ฒฝ์šฐ ๋ชจ๋“  ํ• ์ธ ๊ธˆ์•ก 0์œผ๋กœ ์ง€์ • + +### ์˜ˆ์™ธ ์ฒ˜๋ฆฌ +- ๋‚ ์งœ ์ž…๋ ฅ ์˜ˆ์™ธ + - [x] ์ž˜๋ชป๋œ ๋‚ ์งœ ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ + - [x] ๋‚ ์งœ๊ฐ€ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ณ€๊ฒฝ์ด ์•ˆ๋  ๋•Œ +- ๋ฉ”๋‰ด ์ž…๋ ฅ ์˜ˆ์™ธ + - [x] ๋ฉ”๋‰ด ์ž…๋ ฅ ์‹œ ์ •๊ทœํ‘œํ˜„์‹ ํŒจํ„ด์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ (TextProcessor ์ฒ˜๋ฆฌ) + - [x] ์ค‘๋ณต ๋ฉ”๋‰ด๋ช… ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ (TextProcessor ์ฒ˜๋ฆฌ) + - [x] ์—†๋Š” ๋ฉ”๋‰ด๋ช… ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ + - [x] ์ฃผ๋ฌธ ๋ฉ”๋‰ด์˜ ๊ฐœ์ˆ˜๊ฐ€ 1๋ณด๋‹ค ์ž‘์„ ๋•Œ + - [x] ์ฃผ๋ฌธ ๋ฉ”๋‰ด๊ฐ€ 20๊ฐœ๋ฅผ ๋„˜์„ ๋•Œ + - [x] ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ์‹œ ์ฃผ๋ฌธ ๋ถˆ๊ฐ€๋Šฅ + +### ํด๋ž˜์Šค ๊ตฌ์กฐ +- InputView : ํ™”๋ฉด ์ž…๋ ฅ์„ ๋‹ด๋‹นํ•˜๋Š” ๋ทฐ + - InputViewMessage : ์ž…๋ ฅ์„ ์œ„ํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” Enum +- OutputView : ํ™”๋ฉด ์ถœ๋ ฅ์„ ๋‹ด๋‹นํ•˜๋Š” ๋ทฐ + - OutputViewMessage : ์ถœ๋ ฅ์„ ์œ„ํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” Enum +- PromotionRun : ๋ทฐ์™€ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ํด๋ž˜์Šค๋กœ ์š”์ฒญ ์ „๋‹ฌ +- PromotionController : ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์š”์ฒญ์„ ์ „๋‹ฌํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ +- PromotionService : ํ”„๋กœ๋ชจ์…˜ ์œ ํšจ์„ฑ ๊ฒ€์ฆ์„ ๊ฑฐ์นœ ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์‹คํ–‰ํ•  ์„œ๋น„์Šค +- TextProcessor : ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ํŠน์ • ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ ์„œ๋น„์Šค +- OrderMenus : ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜ ์ €์žฅํ•˜๋Š” ํด๋ž˜์Šค + - MenuItem : ๋ฉ”๋‰ด ์ด๋ฆ„๊ณผ ๊ฐ€๊ฒฉ, ๋ฉ”๋‰ด ์ข…๋ฅ˜๋ฅผ ์ €์žฅํ•˜๋Š” Enum + - MenuCategory : ๋ฉ”๋‰ด ์ข…๋ฅ˜๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” Enum +- ReservationDateEvent : ๋‚ ์งœ ๊ด€๋ จ ์ด๋ฒคํŠธ ์ €์žฅ ํด๋ž˜์Šค + - ReservationDate : ์ž…๋ ฅ๋œ ๋‚ ์งœ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค + - ChristmasDiscount : ํฌ๋ฆฌ์Šค๋งˆ์Šค D-day ํ• ์ธ ๊ด€๋ฆฌ ํด๋ž˜์Šค + - WeekDiscountType : ์ฃผ๋ง ํ• ์ธ, ํ‰์ผ ํ• ์ธ์„ ๊ตฌ๋ถ„ํ•˜๋Š” Enum + - SpecialDayDiscount : ํŠน๋ณ„ ํ• ์ธ ์ผ์ž๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” Enum +- EventResult : ์ ์šฉ ์ค‘์ธ ๋ชจ๋“  ์ด๋ฒคํŠธ๋ฅผ ์ €์žฅํ•˜๋Š” ํด๋ž˜์Šค + - DiscountDetail : ์ฃผ๋ฌธ ๊ธˆ์•ก๊ณผ ํ• ์ธ, ์˜ˆ์ƒ ๊ธˆ์•ก ๋“ฑ์„ ๋ณด๊ด€ํ•œ ํด๋ž˜์Šค + - GiftEvent : ์ฆ์ • ์ƒํ’ˆ ์—ฌ๋ถ€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” Enum + - RewardBadge : ์ด๋ฒคํŠธ ๋ฐฐ์ง€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” Enum +- PromotionException : ์ง€์ •๋œ Enum ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€๋ฅผ ํ†ตํ•ด ๊ฐ’๋ฅผ ๋‹ด์•„ IllegalArgumentException ์ƒ์„ฑ + - ExceptionMessage : ์˜ˆ์™ธ ๋ฐœ์ƒ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๊ด€ํ•œ Enum Class +- PromotionConverter : Service ์—์„œ Domain to DTO ์ปจ๋ฒ„ํ„ฐ +- EventResultTextFactory : EventResultDTO ๋ฐ˜ํ™˜์„ ์œ„ํ•œ ๋ฌธ์ž์—ด ์กฐ์ž‘ ์„œ๋น„์Šค + - EventResultText : EventResultDTO ์— ํ•„์š”ํ•œ ๋ฌธ์ž์—ด Enum +- ํ…Œ์ŠคํŠธ : ํด๋ž˜์Šค๋ณ„ ๋ฉ”์†Œ๋“œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์ง„ํ–‰ + +### ์ด๋ฒˆ์— ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๊ณ ๋ฏผํ•œ ์š”์†Œ +- ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๊ตฌ์กฐ ์„ค๊ณ„ + - ์ดํ›„ ํ•„์š”ํ•œ ํด๋ž˜์Šค๋“ค์„ ์ถ”๊ฐ€ํ•˜๋ฉฐ ์„ค๊ณ„ ๋ณ€๊ฒฝ +- ๋™์ผํ•œ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ํ•˜๋Š” ๋ทฐ๋Š” ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๊ตฌํ˜„ +- ์ž…๋ ฅ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์™€ ๊ฐ์ฒด ์ƒ์„ฑ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๊ตฌ๋ถ„์— ๋Œ€ํ•œ ๊ณ ๋ฏผ +- ์ด๋ฒคํŠธ ์ ์šฉ ์‹œ ์ด๋ฒคํŠธ ๋‚ด์—ญ๊ณผ ์ฃผ๋ฌธ ๋‚ด์—ญ์„ ์‚ฌ์šฉํ•ด ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ Service ์—์„œ ์ง„ํ–‰ํ• ์ง€ ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ ์ง„ํ–‰ํ• ์ง€ ๊ณ ๋ฏผ +- ์ถœ๋ ฅ์„ ์œ„ํ•œ DTO ์ƒ์„ฑ์— ๋Œ€ํ•œ ๊ณ ๋ฏผ + - DTO ๋ฃฐ ์ถœ๋ ฅํ•˜๊ณ  ํ•ด๋‹น DTO ํ†ตํ•ด ๋‹ค์‹œ ์ด๋ฒคํŠธ ๋‚ด์—ญ ํ™•์ธํ•˜๋Š”๋ฐ ์žˆ์–ด DTO ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• + - EventResultDTO ๋ทฐ์— ์ถœ๋ ฅํ•  ๋ฌธ์ž์—ด์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ DTO ์ƒ์„ฑ ๋ฐฉ๋ฒ• ๊ณ ๋ฏผ ํ›„ Builder, EventResultTextFactory ์‚ฌ์šฉ +- ์ž˜๋ชป๋œ ์ž…๋ ฅ์— ๋Œ€ํ•œ ๋ฉ”์„œ๋“œ ์žฌ์‹คํ–‰ ๋กœ์ง์€ ์ง€๋‚œ์ฃผ [zangsu๋‹˜](https://github.com/zangsu) ์ฝ”๋“œ๋ฆฌ๋ทฐ๋กœ ๋ฐฐ์šด Supplier ๋ฐ˜๋ณต ์‚ฌ์šฉ +- ํ…Œ์ŠคํŠธ ์ฝ”๋“œ mock ๊ฐ์ฒด์— ๋Œ€ํ•œ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๊ณ ๋ฏผ \ No newline at end of file
Unknown
์•„ ์ข‹์€ ์ƒ๊ฐ์ด๋„ค์š”. ์ดํ›„ ๊ด€๋ฆฌ์—๋Š” ์ƒ๊ฐํ•˜์ง€ ์•Š์•˜๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -1,7 +1,20 @@ package christmas; + +import camp.nextstep.edu.missionutils.Console; +import christmas.run.PromotionRun; +import christmas.view.InputView; +import christmas.view.OutputView; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + try { + InputView inputView = SingletonView.getInputView(); + OutputView outputView = SingletonView.getOutputView(); + PromotionRun promotionRun = new PromotionRun(inputView, outputView); + promotionRun.runPromotion(); + } finally { + Console.close(); + } } }
Java
ํ•œ๋ฒˆ๋งŒ ์ƒ์„ฑ๋˜๋ฉด ๋˜๊ณ  ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๊ฒ ๋‹ค ์‹ถ์–ด ์ ์šฉํ–ˆ๋Š”๋ฐ ์ปจํŠธ๋กค๋Ÿฌ, ์„œ๋น„์Šค ๋ชจ๋‘ ์ ์šฉํ•ด์•ผํ•˜๋‚˜ ์‹ถ๋‹ค๊ฐ€ ๋ทฐ๋งŒ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค..
@@ -0,0 +1,20 @@ +package christmas.exception; + +public enum ExceptionMessage { + INVALID_INPUT_DATE("์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + INVALID_INPUT_ORDER("์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + INVALID_MENU_ITEM("ํ•ด๋‹น ๋ฉ”๋‰ด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."), + INVALID_WEEK_DISCOUNT_TYPE("์ž˜๋ชป๋œ ์ฃผ๊ฐ„ ํ• ์ธ ํƒ€์ž…์ž…๋‹ˆ๋‹ค."), + ; + + private static final String PREFIX = "[ERROR] "; + private final String errorMessage; + + ExceptionMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public String getErrorMessage() { + return PREFIX + errorMessage; + } +}
Java
์•„ ํ•ด๋‹น ์˜ˆ์™ธ๋Š” ์ด๋ฏธ ์ฒ˜๋ฆฌ๋œ ๊ฒฐ๊ณผ DTO ๋“ค์„ ๊ฐ€์ง€๊ณ  ๋ฐœ์ƒ๋˜๋Š” ์˜ˆ์™ธ๋ผ์„œ ์ •์ƒ์ ์ด๋ผ๋ฉด ํ„ฐ์ง€์ง€ ์•Š๋Š” ์˜ˆ์™ธ ์ž…๋‹ˆ๋‹ค. ์ผ๋‹จ ์ œ ์ƒ๊ฐ์œผ๋กœ๋Š” ๋ฌด์Šจ ์ง“์„ ํ•ด๋„ ์ž…๋ ฅ์— ๋Œ€ํ•ด ํ•ด๋‹น ์˜ˆ์™ธ๋Š” ํ„ฐ์ง€์ง€๋Š” ์•Š์„ ๊ฑฐ๋ผ ์ƒ๊ฐํ•˜๋Š”๋ฐ ํ•ด๋‹น ๊ธฐ๋Šฅ์—์„œ๋งŒ ํ„ฐ์ง€๋Š” ์˜ˆ์™ธ๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,33 @@ +package christmas.model.event; + +import christmas.model.date.ReservationDate; + +public class ChristmasDiscount { + private static final int FIRST_DAY = 1; + private static final int CHRISTMAS_DAY = 25; + private static final int BASIC_DISCOUNT = 1000; + private static final int PLUS_DISCOUNT = 100; + private static final int NONE_DISCOUNT = 0; + + private final int discount; + + public ChristmasDiscount(ReservationDate date) { + this.discount = initDiscountAmount(date); + } + + private int initDiscountAmount(ReservationDate date) { + if (date.day() > CHRISTMAS_DAY) { + return NONE_DISCOUNT; + } + + return BASIC_DISCOUNT + getPlusDiscount(date.day()); + } + + private int getPlusDiscount(int day) { + return (day - FIRST_DAY) * PLUS_DISCOUNT; + } + + public int getDiscount() { + return discount; + } +}
Java
์˜ˆ์•ฝ ์ผ์ž ๊ฐ์ฒด๋Š” ์˜ˆ์•ฝ๋œ ์ผ์ž์— ๋Œ€ํ•ด์„œ๋งŒ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ํ• ์ธ์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•ด๋‹น ํ• ์ธ ๊ฐ์ฒด๊ฐ€ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ๋„ฃ์—ˆ๋Š”๋ฐ ๊ณ ๋ฏผ์ด ๋˜๋„ค์š”..
@@ -0,0 +1,62 @@ +package christmas.model.event; + +import christmas.model.event.dto.ReservationDateEventDTO; + +public class DiscountDetail { + private final int totalPriceBeforeDiscount; + private final int totalPriceAfterDiscount; + private final WeekDiscountType weekDiscountType; + private final int christmasDiscount; + private final int weekTypeDiscount; + private final int specialDiscount; + + protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount, int weekTypeDiscount) { + this.totalPriceBeforeDiscount = totalPriceBeforeDiscount; + this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + this.christmasDiscount = dateEventDTO.getChristmasDiscount(); + this.specialDiscount = dateEventDTO.getSpecialDiscount(); + this.weekTypeDiscount = weekTypeDiscount; + this.totalPriceAfterDiscount = initTotalPriceAfterDiscount(); + } + + protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount) { + this.totalPriceBeforeDiscount = totalPriceBeforeDiscount; + this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + this.christmasDiscount = 0; + this.specialDiscount = 0; + this.weekTypeDiscount = 0; + this.totalPriceAfterDiscount = initTotalPriceAfterDiscount(); + } + + private int initTotalPriceAfterDiscount() { + return totalPriceBeforeDiscount - getTotalDiscount(); + } + + public int getTotalPriceBeforeDiscount() { + return totalPriceBeforeDiscount; + } + + public int getTotalPriceAfterDiscount() { + return totalPriceAfterDiscount; + } + + public int getChristmasDiscount() { + return christmasDiscount; + } + + public int getSpecialDiscount() { + return specialDiscount; + } + + public int getWeekTypeDiscount() { + return weekTypeDiscount; + } + + public WeekDiscountType getWeekDiscountType() { + return weekDiscountType; + } + + public int getTotalDiscount() { + return christmasDiscount + weekTypeDiscount + specialDiscount; + } +}
Java
๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ƒํƒœ์˜ ๊ฐ์ฒด๋ฅผ ํ‘œํ˜„ํ•˜๋ ค ํ•˜๋‹ค๋ณด๋‹ˆ ์ด๋ ‡๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ ์•„์ง๋„ ๋ฉ”์„œ๋“œ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„์ง ๋‚ฏ์„  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.. ์ค‘๊ฐ„์ค‘๊ฐ„ ์ธ์ง€ํ•˜๋ คํ•ด๋„ ๋†“์น˜๋Š” ๋ถ€๋ถ„์ด ๋งŽ๋„ค์š”..
@@ -0,0 +1,62 @@ +package christmas.model.event; + +import christmas.model.event.dto.ReservationDateEventDTO; + +public class DiscountDetail { + private final int totalPriceBeforeDiscount; + private final int totalPriceAfterDiscount; + private final WeekDiscountType weekDiscountType; + private final int christmasDiscount; + private final int weekTypeDiscount; + private final int specialDiscount; + + protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount, int weekTypeDiscount) { + this.totalPriceBeforeDiscount = totalPriceBeforeDiscount; + this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + this.christmasDiscount = dateEventDTO.getChristmasDiscount(); + this.specialDiscount = dateEventDTO.getSpecialDiscount(); + this.weekTypeDiscount = weekTypeDiscount; + this.totalPriceAfterDiscount = initTotalPriceAfterDiscount(); + } + + protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount) { + this.totalPriceBeforeDiscount = totalPriceBeforeDiscount; + this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + this.christmasDiscount = 0; + this.specialDiscount = 0; + this.weekTypeDiscount = 0; + this.totalPriceAfterDiscount = initTotalPriceAfterDiscount(); + } + + private int initTotalPriceAfterDiscount() { + return totalPriceBeforeDiscount - getTotalDiscount(); + } + + public int getTotalPriceBeforeDiscount() { + return totalPriceBeforeDiscount; + } + + public int getTotalPriceAfterDiscount() { + return totalPriceAfterDiscount; + } + + public int getChristmasDiscount() { + return christmasDiscount; + } + + public int getSpecialDiscount() { + return specialDiscount; + } + + public int getWeekTypeDiscount() { + return weekTypeDiscount; + } + + public WeekDiscountType getWeekDiscountType() { + return weekDiscountType; + } + + public int getTotalDiscount() { + return christmasDiscount + weekTypeDiscount + specialDiscount; + } +}
Java
์—ฌ๋Ÿฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  static์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๋ณด๋‹ค๋Š” ์ €๋Š” ๊ฐœ์ธ์ ์œผ๋กœ ํ•ด๋‹น ๊ธฐ๋Šฅ์€ ํ• ์ธ์ด ์—†์„ ๊ฒฝ์šฐ์˜ ๊ฐ์ฒด๋ฅผ ์˜ค๋ฒ„๋กœ๋”ฉ์„ ํ†ตํ•ด ์ƒ์„ฑํ•˜๋Š”๊ฒŒ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐ๋ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,91 @@ +package christmas.model.event; + +import christmas.model.event.dto.ReservationDateEventDTO; +import christmas.model.menu.MenuItem; + +import java.util.*; +import java.util.stream.Stream; + +public class EventResult { + private static final int MIN_EVENT_AMOUNT = 10_000; + private static final int GIFT_AMOUNT = 120_000; + + private final Map<MenuItem, Integer> orderMenus; + private final DiscountDetail discountDetail; + private final GiftMenu giftMenu; + private final RewardBadge rewardBadge; + + public EventResult(ReservationDateEventDTO dateEventDTO, Map<MenuItem, Integer> orderMenus) { + this.orderMenus = orderMenus; + this.discountDetail = initDiscountDetail(dateEventDTO); + this.giftMenu = initGiftMenu(); + this.rewardBadge = initRewardBadge(); + } + + private DiscountDetail initDiscountDetail(ReservationDateEventDTO dateEventDTO) { + int totalPriceBeforeDiscount = getTotalPriceBeforeDiscount(); + if (totalPriceBeforeDiscount < MIN_EVENT_AMOUNT) { + return new DiscountDetail(dateEventDTO, totalPriceBeforeDiscount); + } + int weekDiscount = calculateWeekDiscount(dateEventDTO); + + return new DiscountDetail(dateEventDTO, totalPriceBeforeDiscount, weekDiscount); + } + + private GiftMenu initGiftMenu() { + if (discountDetail.getTotalPriceBeforeDiscount() < GIFT_AMOUNT) { + return GiftMenu.NONE; + } + + return GiftMenu.CHAMPAGNE; + } + + private RewardBadge initRewardBadge() { + int benefit = discountDetail.getTotalDiscount() + giftMenu.getGiftPrice(); + + return Arrays.stream(RewardBadge.values()) + .filter(badge -> + benefit >= badge.getBenefit()) + .max(Comparator.comparingInt(RewardBadge::getBenefit)) + .orElse(RewardBadge.NONE); + } + + private int getTotalPriceBeforeDiscount() { + return generateOrderMenuEntry(orderMenus) + .mapToInt(menu -> + menu.getKey().getItemPrice() * menu.getValue()) + .sum(); + } + + private int calculateWeekDiscount(ReservationDateEventDTO dateEventDTO) { + WeekDiscountType type = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + + return generateOrderMenuEntry(orderMenus) + .filter(menu -> + menu.getKey().getMenuCategory() == type.getDiscountCategory()) + .mapToInt(menu -> + menu.getValue() * type.getDiscountPrice()) + .sum(); + } + + private <K, V> Stream<Map.Entry<K, V>> generateOrderMenuEntry(Map<K, V> orderMenus) { + return orderMenus.entrySet() + .stream(); + } + + public Map<MenuItem, Integer> getOrderMenus() { + return Collections.unmodifiableMap(orderMenus); + } + + public DiscountDetail getDiscountDetail() { + return discountDetail; + } + + public GiftMenu getGiftMenu() { + return giftMenu; + } + + public RewardBadge getRewardBadge() { + return rewardBadge; + } +}
Java
๊ทธ ๋ถ€๋ถ„์€ @Dongwoongkim ๋‹˜์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ํ™•์‹คํžˆ ๊ทธ๋ ‡๋‹ค๊ณ  ๋А๊ปด์กŒ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฐ์ฒด ์•ˆ์— ์žˆ์–ด์•ผํ•œ๋‹ค๊ณ  ๋ฐ”๋ผ๋ณด๋‹ˆ ๊ทธ๋ ‡๊ฒŒ ์ƒ๊ฐ์„ ๋ชปํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์•„์š”.. EventResult๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ €๋„ ๋” ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,28 @@ +package christmas.model.event; + +public enum SpecialDayDiscount { + THIRD_DAY(3, 1000), + TENTH_DAY(10, 1000), + SEVENTEENTH_DAY(17, 1000), + TWENTY_FOURTH_DAY(24, 1000), + TWENTY_FIFTH_DAY(25, 1000), + THIRTY_FIRST_DAY(31, 1000), + OTHER_DAY(0, 0), + ; + + private final int day; + private final int discountPrice; + + SpecialDayDiscount(int day, int discountPrice) { + this.day = day; + this.discountPrice = discountPrice; + } + + public int getDay() { + return day; + } + + public int getDiscountPrice() { + return discountPrice; + } +}
Java
์ข‹์€ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค! enum์— ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋„ฃ์„ ์ƒ๊ฐ์„ ๋ชปํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,79 @@ +package christmas.model.menu; + +import christmas.exception.ExceptionMessage; +import christmas.exception.PromotionException; + +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class OrderMenus { + private static final int MIN_ORDER_MENU_NUMBER = 1; + private static final int MAX_ORDER_MENUS_NUMBER = 20; + + private final Map<MenuItem, Integer> orderMenus; + + public OrderMenus(Map<String, Integer> orderMenus) { + this.orderMenus = validateOrderMenusByNames(orderMenus); + validateOrderMenus(); + } + + private Map<MenuItem, Integer> validateOrderMenusByNames(Map<String, Integer> orderMenus) { + return generateOrderMenuValues(orderMenus) + .collect(Collectors.toUnmodifiableMap( + entry -> MenuItem.findMenuItemByName(entry.getKey(), ExceptionMessage.INVALID_INPUT_ORDER), + Map.Entry::getValue + )); + } + + private void validateOrderMenus() { + validateOrderNumbers(); + validateTotalOrderNumbers(); + validateOrderAllBeverage(); + } + + private void validateOrderNumbers() { + generateOrderMenuValues(orderMenus) + .filter(menu -> + menu.getValue() < MIN_ORDER_MENU_NUMBER) + .findAny() + .ifPresent(menuName -> throwInvalidOrderException()); + } + + private void validateTotalOrderNumbers() { + int orderNumber = generateOrderMenuValues(orderMenus) + .mapToInt(Map.Entry::getValue) + .sum(); + + if (orderNumber > MAX_ORDER_MENUS_NUMBER) { + throwInvalidOrderException(); + } + } + + private void validateOrderAllBeverage() { + boolean allBeverage = generateOrderMenuValues(orderMenus) + .allMatch(orderMenu -> + orderMenu.getKey().getMenuCategory() == MenuCategory.BEVERAGE); + + if (allBeverage) { + throwInvalidOrderException(); + } + } + + private <K, V> Stream<Map.Entry<K, V>> generateOrderMenuValues(Map<K, V> orderMenus) { + return orderMenus.entrySet() + .stream(); + } + + private void throwInvalidOrderException() { + throw new PromotionException(ExceptionMessage.INVALID_INPUT_ORDER); + } + + public Map<String, Integer> getOrderMenus() { + Map<String, Integer> menuNames = new HashMap<>(); + orderMenus.forEach((menuItem, quantity) -> + menuNames.put(menuItem.getItemName(), quantity)); + + return Collections.unmodifiableMap(menuNames); + } +}
Java
๊ฐœ์ˆ˜๋งŒ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋‹ค ๋ณด๋‹ˆ ํด๋ž˜์Šค๊ฐ€ ๋งŽ์•„์ ธ ๋ฌด๊ฑฐ์›Œ์งˆ ๊ฒƒ ๊ฐ™์•„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ ๋ญ๊ฐ€ ๋” ๋‚˜์€์ง€๋Š” ๊ณ ๋ฏผ์ด ๋˜๋„ค์š”..
@@ -0,0 +1,76 @@ +package christmas.run; + +import christmas.controller.PromotionController; +import christmas.exception.PromotionException; +import christmas.model.event.dto.EventResultDTO; +import christmas.model.event.dto.ReservationDateEventDTO; +import christmas.model.menu.dto.OrderMenusDTO; +import christmas.view.InputView; +import christmas.view.OutputView; + +import java.util.function.Supplier; + +public class PromotionRun { + private final PromotionController controller = new PromotionController(); + private final InputView inputView; + private final OutputView outputView; + + public PromotionRun(InputView inputView, OutputView outputView) { + this.inputView = inputView; + this.outputView = outputView; + } + + public void runPromotion() { + ReservationDateEventDTO dateEventDto = inputCorrectReservationDate(); + OrderMenusDTO orderMenusDto = inputCorrectOrderMenus(); + displayEventPreview(dateEventDto); + + EventResultDTO responseDto = applyEventsAndGenerateResult(dateEventDto, orderMenusDto); + outputEventResult(responseDto); + } + + private ReservationDateEventDTO inputCorrectReservationDate() { + outputView.displayStartPromotion(); + + return getCorrectResult(this::generateDateEvent); + } + + private OrderMenusDTO inputCorrectOrderMenus() { + return getCorrectResult(this::receiveMenus); + } + + private ReservationDateEventDTO generateDateEvent() { + String inputDate = inputView.inputDate(); + + return controller.generateEvent(inputDate); + } + + private OrderMenusDTO receiveMenus() { + String inputMenus = inputView.inputOrderMenus(); + + return controller.receiveOrderMenus(inputMenus); + } + + private void displayEventPreview(ReservationDateEventDTO dateEventDto) { + outputView.displayPreviewEvent(dateEventDto.getDay()); + } + + private EventResultDTO applyEventsAndGenerateResult(ReservationDateEventDTO dateEventDTO, + OrderMenusDTO orderMenusDto) { + return controller.applyEvents(dateEventDTO, orderMenusDto); + } + + private void outputEventResult(EventResultDTO responseDto) { + outputView.outputEventResult(responseDto); + } + + private <T> T getCorrectResult(Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (PromotionException e) { + outputView.displayException(e); + } + } + } +}
Java
ํ•ด๋‹น ๋ถ€๋ถ„์€ ์ €๋„ ์ด์ „ ์ฝ”๋“œ๋ฆฌ๋ทฐ๋ฅผ ํ†ตํ•ด ๋ฐฐ์šด ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์ธ๋ฐ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,27 @@ +package christmas.util; + +enum EventResultText { + ORDER_OUTPUT_REGEX("%s %d%s"), + EMPTY_TEXT(""), + SPACE(" "), + MENU_NUMBER("๊ฐœ"), + MENU_PRICE_UNIT("์›"), + CHRISTMAS_D_DAY_DISCOUNT("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"), + SPECIAL_DISCOUNT("ํŠน๋ณ„ ํ• ์ธ"), + GIFT_EVENT("์ฆ์ • ์ด๋ฒคํŠธ"), + NONE_BENEFIT("์—†์Œ"), + DISCOUNT_PRICE("-"), + BENEFIT_SEPARATOR(": "), + NEW_LINE("\n"), + ; + + private final String text; + + EventResultText(String text) { + this.text = text; + } + + public String getText() { + return text; + } +}
Java
๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•ด์ฃผ๊ธฐ ์œ„ํ•œ ์ƒ์ˆ˜๋“ค์„ ๋ฌถ์–ด ๋‘” Enum ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๊ณณ์—์„œ ์‚ฌ์šฉ๋˜์ง€๋Š” ์•Š์„ ๊ฒƒ ๊ฐ™์•„ default๋กœ enum์„ ์‚ฌ์šฉํ–ˆ์–ด์š”
@@ -0,0 +1,62 @@ +package christmas.model.event; + +import christmas.model.event.dto.ReservationDateEventDTO; + +public class DiscountDetail { + private final int totalPriceBeforeDiscount; + private final int totalPriceAfterDiscount; + private final WeekDiscountType weekDiscountType; + private final int christmasDiscount; + private final int weekTypeDiscount; + private final int specialDiscount; + + protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount, int weekTypeDiscount) { + this.totalPriceBeforeDiscount = totalPriceBeforeDiscount; + this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + this.christmasDiscount = dateEventDTO.getChristmasDiscount(); + this.specialDiscount = dateEventDTO.getSpecialDiscount(); + this.weekTypeDiscount = weekTypeDiscount; + this.totalPriceAfterDiscount = initTotalPriceAfterDiscount(); + } + + protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount) { + this.totalPriceBeforeDiscount = totalPriceBeforeDiscount; + this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek()); + this.christmasDiscount = 0; + this.specialDiscount = 0; + this.weekTypeDiscount = 0; + this.totalPriceAfterDiscount = initTotalPriceAfterDiscount(); + } + + private int initTotalPriceAfterDiscount() { + return totalPriceBeforeDiscount - getTotalDiscount(); + } + + public int getTotalPriceBeforeDiscount() { + return totalPriceBeforeDiscount; + } + + public int getTotalPriceAfterDiscount() { + return totalPriceAfterDiscount; + } + + public int getChristmasDiscount() { + return christmasDiscount; + } + + public int getSpecialDiscount() { + return specialDiscount; + } + + public int getWeekTypeDiscount() { + return weekTypeDiscount; + } + + public WeekDiscountType getWeekDiscountType() { + return weekDiscountType; + } + + public int getTotalDiscount() { + return christmasDiscount + weekTypeDiscount + specialDiscount; + } +}
Java
ํ•„๋“œ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๊ฒŒ ์—ฌ๋Ÿฌ๋ฒˆ ํ˜ธ์ถœ ๋  ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” ๊ฒƒ์ด ์ข‹๋‹ค ์ƒ๊ฐํ•ด์„œ ํ•ด๋‹น ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๋ช‡๋ช‡ ํ•„๋“œ๋Š” ๋ฉ”์„œ๋“œ๋กœ ํ•ด๋‹น ํ•„๋“œ๋งŒ์„ ๊ฐ€์ง€๊ณ  ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”
@@ -0,0 +1,105 @@ +package christmas.model.event.dto; + +public class EventResultDTO { + private final String orderMenus; + private final String totalPriceBeforeDiscount; + private final String giftMenu; + private final String benefitHistory; + private final String totalBenefit; + private final String totalPriceAfterDiscount; + private final String rewardBadge; + + private EventResultDTO(Builder builder) { + this.orderMenus = builder.orderMenus; + this.totalPriceBeforeDiscount = builder.totalPriceBeforeDiscount; + this.giftMenu = builder.giftMenu; + this.benefitHistory = builder.benefitHistory; + this.totalBenefit = builder.totalBenefit; + this.totalPriceAfterDiscount = builder.totalPriceAfterDiscount; + this.rewardBadge = builder.rewardBadge; + } + + public static class Builder { + private String orderMenus; + private String totalPriceBeforeDiscount; + private String giftMenu; + private String benefitHistory; + private String totalBenefit; + private String totalPriceAfterDiscount; + private String rewardBadge; + + public Builder() { + } + + public Builder orderMenus(String orderMenus) { + this.orderMenus = orderMenus; + return this; + } + + public Builder totalPriceBeforeDiscount(String totalPriceBeforeDiscount) { + this.totalPriceBeforeDiscount = totalPriceBeforeDiscount; + return this; + } + + public Builder giftMenu(String giftMenu) { + this.giftMenu = giftMenu; + return this; + } + + public Builder benefitHistory(String benefitHistory) { + this.benefitHistory = benefitHistory; + return this; + } + + public Builder totalBenefit(String totalBenefit) { + this.totalBenefit = totalBenefit; + return this; + } + + public Builder totalPriceAfterDiscount(String totalPriceAfterDiscount) { + this.totalPriceAfterDiscount = totalPriceAfterDiscount; + return this; + } + + public Builder rewardBadge(String rewardBadge) { + this.rewardBadge = rewardBadge; + return this; + } + + public EventResultDTO build() { + return new EventResultDTO(this); + } + } + + public static Builder builder() { + return new Builder(); + } + + public String getOrderMenus() { + return orderMenus; + } + + public String getTotalPriceBeforeDiscount() { + return totalPriceBeforeDiscount; + } + + public String getGiftMenu() { + return giftMenu; + } + + public String getBenefitHistory() { + return benefitHistory; + } + + public String getTotalBenefit() { + return totalBenefit; + } + + public String getTotalPriceAfterDiscount() { + return totalPriceAfterDiscount; + } + + public String getRewardBadge() { + return rewardBadge; + } +}
Java
๋นŒ๋” ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ฉด null ์ธ ๊ฐ’์ด ์˜ฌ ์ˆ˜ ๋„ ์žˆ์„ํ…๋ฐ ์ „๋ถ€ ๋‹ค ํ•„์š”ํ•ด์ด๋Š” ๋ณ€์ˆ˜๋“ค์ธ๋ฐ ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  ๋นŒ๋”ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,55 @@ +package christmas.model.menu; + +import christmas.exception.ExceptionMessage; +import christmas.exception.PromotionException; + +import java.util.Arrays; + +public enum MenuItem { + APPETIZER_MUSHROOM_SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6_000, MenuCategory.APPETIZER), + APPETIZER_TAPAS("ํƒ€ํŒŒ์Šค", 5_500, MenuCategory.APPETIZER), + APPETIZER_CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000, MenuCategory.APPETIZER), + + MAIN_T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000, MenuCategory.MAIN_COURSE), + MAIN_BBQ_RIB("๋ฐ”๋น„ํ๋ฆฝ", 54_000, MenuCategory.MAIN_COURSE), + MAIN_SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000, MenuCategory.MAIN_COURSE), + MAIN_CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000, MenuCategory.MAIN_COURSE), + + DESSERT_CHOCO_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15_000, MenuCategory.DESSERT), + DESSERT_ICE_CREAM("์•„์ด์Šคํฌ๋ฆผ", 5_000, MenuCategory.DESSERT), + + BEVERAGE_ZERO_COLA("์ œ๋กœ์ฝœ๋ผ", 3_000, MenuCategory.BEVERAGE), + BEVERAGE_RED_WINE("๋ ˆ๋“œ์™€์ธ", 60_000, MenuCategory.BEVERAGE), + BEVERAGE_CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, MenuCategory.BEVERAGE), + ; + + private final String itemName; + private final int itemPrice; + private final MenuCategory menuCategory; + + MenuItem(String itemName, int itemPrice, MenuCategory menuCategory) { + this.itemName = itemName; + this.itemPrice = itemPrice; + this.menuCategory = menuCategory; + } + + public static MenuItem findMenuItemByName(String itemName, ExceptionMessage message) { + return Arrays.stream(MenuItem.values()) + .filter(menu -> menu.getItemName().equals(itemName)) + .findFirst() + .orElseThrow(() -> + new PromotionException(message)); + } + + public String getItemName() { + return itemName; + } + + public int getItemPrice() { + return itemPrice; + } + + public MenuCategory getMenuCategory() { + return menuCategory; + } +}
Java
๋ฉ”๋‰ด ์•„์ดํ…œ CRUD API ๋ฅผ ๋งŒ๋“ค๋•Œ enum ์œผ๋กœ ํ•˜๊ฒŒ ๋˜๋ฉด ์ถ”๊ฐ€์— ๋Œ€ํ•ด ํ™•์žฅ์„ฑ์ด ๋–จ์–ด์ง€๋Š”๊ฒƒ์ด ์•„๋‹Œ๊ฐ€ ์ƒ๊ฐ์ด๋“ญ๋‹ˆ๋‹ค!
@@ -0,0 +1,57 @@ +package christmas.model.event; + +import christmas.model.date.ReservationDate; + +import java.util.Arrays; + +public class ReservationDateEvent { + private static final int WEEK = 7; + private static final int FRIDAY = 2; + private static final int SATURDAY = 3; + + private final ReservationDate reservationDate; + private final ChristmasDiscount christmasDiscount; + private final WeekDiscountType discountDateWeek; + private final SpecialDayDiscount specialDiscountDate; + + public ReservationDateEvent(int day) { + this.reservationDate = new ReservationDate(day); + this.christmasDiscount = new ChristmasDiscount(reservationDate); + this.discountDateWeek = initDiscountDateWeek(reservationDate); + this.specialDiscountDate = initSpecialDiscountDate(reservationDate); + } + + private WeekDiscountType initDiscountDateWeek(ReservationDate reservationDate) { + if (reservationDate.day() % WEEK == FRIDAY || + reservationDate.day() % WEEK == SATURDAY) { + + return WeekDiscountType.WEEKEND; + } + + return WeekDiscountType.WEEKDAY; + } + + private SpecialDayDiscount initSpecialDiscountDate(ReservationDate reservationDate) { + return Arrays.stream(SpecialDayDiscount.values()) + .filter(specialDay -> + reservationDate.day() == specialDay.getDay()) + .findFirst() + .orElse(SpecialDayDiscount.OTHER_DAY); + } + + public ReservationDate getReservationDate() { + return reservationDate; + } + + public ChristmasDiscount getChristmasDiscount() { + return christmasDiscount; + } + + public WeekDiscountType getDiscountDateWeek() { + return discountDateWeek; + } + + public SpecialDayDiscount getSpecialDiscountDate() { + return specialDiscountDate; + } +}
Java
์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์ด๋ฒคํŠธ๊ฐ€ ์ˆ˜๋ฐฑ๊ฐœ๊ฐ€ ๋˜๋ฉด ์ˆ˜๋ฐฑ๊ฐœ๋ฅผ ์ง์ ‘ ๋‹ค ๊ตฌํ˜„ํ•ด์ค˜์•ผ ๋˜์„œ ๋น„ํšจ์œจ์  ์ธ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๋ชจ๋‘ ํ• ์ธ์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ˆ inteface Discount ๋ฅผ ๋งŒ๋“ค์–ด ``` Discount ๋ฅผ ๊ตฌํ˜„ํ•œ ReservationDate implements Discount ChristmasDiscount implements Discount ........ ``` ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์„œ List<DIscount> discountList ์ด๋Ÿฐ์‹์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ๊ณตํ†ตํ•จ์ˆ˜ ๋ถ€๋ถ„๋งŒ ๋กœ์ง์— ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๊ด€๋ฆฌ๊ฐ€ ์‰ฌ์šธ๊ฒƒ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,58 @@ +package christmas.domain; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +public class Date { + public static final String DATE_RE_READ_REQUEST_MESSAGE = "์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final LocalDate CHRISTMAS_DAY = LocalDate.of(2023, 12, 25); + private static final int YEAR = 2023; + private static final int MONTH = 12; + private static final int DATE_MIN_NUMBER = 1; + private static final int DATE_MAX_NUMBER = 31; + + private final int dayNumber; + + public Date(int dayNumber) { + validateRange(dayNumber); + this.dayNumber = dayNumber; + } + + private void validateRange(int dayNumber) { + if ((dayNumber < DATE_MIN_NUMBER) || (dayNumber > DATE_MAX_NUMBER)) { + throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE); + } + } + + public boolean isDDayDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + + return localDate.isBefore(CHRISTMAS_DAY.plusDays(1)); + } + + public boolean isWeekdayDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + DayOfWeek day = localDate.getDayOfWeek(); + + return (day.equals(DayOfWeek.MONDAY) || day.equals(DayOfWeek.TUESDAY) || day.equals(DayOfWeek.WEDNESDAY) + || day.equals(DayOfWeek.THURSDAY) || day.equals(DayOfWeek.SUNDAY)); + } + + public boolean isWeekendDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + DayOfWeek day = localDate.getDayOfWeek(); + + return (day.equals(DayOfWeek.FRIDAY) || day.equals(DayOfWeek.SATURDAY)); + } + + public boolean isSpecialDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + DayOfWeek day = localDate.getDayOfWeek(); + + return (day.equals(DayOfWeek.SUNDAY) || localDate.equals(CHRISTMAS_DAY)); + } + + public int getDayNumber() { + return dayNumber; + } +} \ No newline at end of file
Java
(์ด๋Ÿฐ ๋ฐฉ๋ฒ•๋„ ์žˆ์–ด์š”!) ```suggestion try { LocalDate.of(YEAR, MONTH, dayNumber); } catch (NumberFormatException exception) { throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE); } ``` ์ด๋Ÿฐ์‹์œผ๋กœ LocalDate๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,100 @@ +package christmas.domain; + +import java.util.HashMap; +import java.util.Map; + +public class EventProcess { + private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT = 120000; + private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT = 10000; + private static final int D_DAY_DISCOUNT_AMOUNT_PER_DAY = 100; + private static final int D_DAY_DISCOUNT_DEFAULT_AMOUNT = 1000; + private static final int FIRST_DAY_NUMBER = 1; + private static final int WEEKDAY_DESSERT_DISCOUNT_AMOUNT = 2023; + private static final int WEEKEND_MAIN_DISCOUNT_AMOUNT = 2023; + private static final int SPECIAL_DISCOUNT_AMOUNT = 1000; + + public EventResult takeAllBenefit(Date date, Order order) { + Map<BenefitType, Integer> allBenefit = new HashMap<>(); + allBenefit.put(BenefitType.GIFT, takeGift(order)); + allBenefit.put(BenefitType.D_DAY, takeDDayDiscount(date)); + allBenefit.put(BenefitType.WEEKDAY, takeWeekdayDiscount(date, order)); + allBenefit.put(BenefitType.WEEKEND, takeWeekendDiscount(date, order)); + allBenefit.put(BenefitType.SPECIAL, takeSpecialDiscount(date)); + + cancelAllBenefitIfMeetCondition(order, allBenefit); + + return new EventResult(allBenefit); + } + + private int takeGift(Order order) { + if (order.calculateTotalOrderPrice() >= MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT) { + return Menu.CHAMPAGNE.getPrice(); + } + + return 0; + } + + private int takeDDayDiscount(Date date) { + if (date.isDDayDiscountActive()) { + return D_DAY_DISCOUNT_DEFAULT_AMOUNT + + (calculateDifferenceBetweenFirstDay(date) * D_DAY_DISCOUNT_AMOUNT_PER_DAY); + } + + return 0; + } + + private int calculateDifferenceBetweenFirstDay(Date date) { + return date.getDayNumber() - FIRST_DAY_NUMBER; + } + + private int takeWeekdayDiscount(Date date, Order order) { + int discount = 0; + + for (OrderedMenu orderedMenu : order.getOrder()) { + Menu menu = Menu.decideMenu(orderedMenu.getMenuName()); + if (date.isWeekdayDiscountActive() && MenuType.decideMenuType(menu) == MenuType.DESSERT) { + discount += orderedMenu.getQuantity() * WEEKDAY_DESSERT_DISCOUNT_AMOUNT; + } + } + + return discount; + } + + private int takeWeekendDiscount(Date date, Order order) { + int discount = 0; + + for (OrderedMenu orderedMenu : order.getOrder()) { + Menu menu = Menu.decideMenu(orderedMenu.getMenuName()); + if (date.isWeekendDiscountActive() && MenuType.decideMenuType(menu) == MenuType.MAIN) { + discount += orderedMenu.getQuantity() * WEEKEND_MAIN_DISCOUNT_AMOUNT; + } + } + + return discount; + } + + private int takeSpecialDiscount(Date date) { + if (date.isSpecialDiscountActive()) { + return SPECIAL_DISCOUNT_AMOUNT; + } + + return 0; + } + + private Map<BenefitType, Integer> cancelAllBenefitIfMeetCondition(Order order, + Map<BenefitType, Integer> allBenefit) { + if (isInsufficientTotalOrderPriceForEvent(order)) { + allBenefit.replace(BenefitType.GIFT, 0); + allBenefit.replace(BenefitType.D_DAY, 0); + allBenefit.replace(BenefitType.WEEKDAY, 0); + allBenefit.replace(BenefitType.WEEKEND, 0); + allBenefit.replace(BenefitType.SPECIAL, 0); + } + + return allBenefit; + } + + private boolean isInsufficientTotalOrderPriceForEvent(Order order) { + return order.calculateTotalOrderPrice() < MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT; + } +} \ No newline at end of file
Java
(๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค!) ํ• ์ธ์„ ์ ์šฉํ•˜๊ณ  ์กฐ๊ฑด์— ๋งž์ง€ ์•Š์œผ๋ฉด ๋˜๋Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•๋ณด๋‹ค๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š์œผ๋ฉด ํ• ์ธ์„ ์ ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ๋„ ํ•˜๋‚˜์˜ ๋ฐฉ๋ฒ•์ผ ๊ฒƒ ๊ฐ™์•„์š”! ```suggestion if (this.isInsufficientTotalOrderPriceForEvent(order)) { return EnumSet.allOf(BenefitType.class) .stream() .collect(collectingAndThen( toMap(Function.identity(), (benefit) -> 0), EventResult::new) ); } ``` ์œ„ ์ฝ”๋“œ์—์„œ stream์„ ํ™œ์šฉํ–ˆ๋Š”๋ฐ BenefitType์˜ ๋ชจ๋“  ์›์†Œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค์Œ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ฌผ์„ EventResult๋กœ ๋ž˜ํ•‘ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค :)
@@ -0,0 +1,100 @@ +package christmas.domain; + +import java.util.HashMap; +import java.util.Map; + +public class EventProcess { + private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT = 120000; + private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT = 10000; + private static final int D_DAY_DISCOUNT_AMOUNT_PER_DAY = 100; + private static final int D_DAY_DISCOUNT_DEFAULT_AMOUNT = 1000; + private static final int FIRST_DAY_NUMBER = 1; + private static final int WEEKDAY_DESSERT_DISCOUNT_AMOUNT = 2023; + private static final int WEEKEND_MAIN_DISCOUNT_AMOUNT = 2023; + private static final int SPECIAL_DISCOUNT_AMOUNT = 1000; + + public EventResult takeAllBenefit(Date date, Order order) { + Map<BenefitType, Integer> allBenefit = new HashMap<>(); + allBenefit.put(BenefitType.GIFT, takeGift(order)); + allBenefit.put(BenefitType.D_DAY, takeDDayDiscount(date)); + allBenefit.put(BenefitType.WEEKDAY, takeWeekdayDiscount(date, order)); + allBenefit.put(BenefitType.WEEKEND, takeWeekendDiscount(date, order)); + allBenefit.put(BenefitType.SPECIAL, takeSpecialDiscount(date)); + + cancelAllBenefitIfMeetCondition(order, allBenefit); + + return new EventResult(allBenefit); + } + + private int takeGift(Order order) { + if (order.calculateTotalOrderPrice() >= MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT) { + return Menu.CHAMPAGNE.getPrice(); + } + + return 0; + } + + private int takeDDayDiscount(Date date) { + if (date.isDDayDiscountActive()) { + return D_DAY_DISCOUNT_DEFAULT_AMOUNT + + (calculateDifferenceBetweenFirstDay(date) * D_DAY_DISCOUNT_AMOUNT_PER_DAY); + } + + return 0; + } + + private int calculateDifferenceBetweenFirstDay(Date date) { + return date.getDayNumber() - FIRST_DAY_NUMBER; + } + + private int takeWeekdayDiscount(Date date, Order order) { + int discount = 0; + + for (OrderedMenu orderedMenu : order.getOrder()) { + Menu menu = Menu.decideMenu(orderedMenu.getMenuName()); + if (date.isWeekdayDiscountActive() && MenuType.decideMenuType(menu) == MenuType.DESSERT) { + discount += orderedMenu.getQuantity() * WEEKDAY_DESSERT_DISCOUNT_AMOUNT; + } + } + + return discount; + } + + private int takeWeekendDiscount(Date date, Order order) { + int discount = 0; + + for (OrderedMenu orderedMenu : order.getOrder()) { + Menu menu = Menu.decideMenu(orderedMenu.getMenuName()); + if (date.isWeekendDiscountActive() && MenuType.decideMenuType(menu) == MenuType.MAIN) { + discount += orderedMenu.getQuantity() * WEEKEND_MAIN_DISCOUNT_AMOUNT; + } + } + + return discount; + } + + private int takeSpecialDiscount(Date date) { + if (date.isSpecialDiscountActive()) { + return SPECIAL_DISCOUNT_AMOUNT; + } + + return 0; + } + + private Map<BenefitType, Integer> cancelAllBenefitIfMeetCondition(Order order, + Map<BenefitType, Integer> allBenefit) { + if (isInsufficientTotalOrderPriceForEvent(order)) { + allBenefit.replace(BenefitType.GIFT, 0); + allBenefit.replace(BenefitType.D_DAY, 0); + allBenefit.replace(BenefitType.WEEKDAY, 0); + allBenefit.replace(BenefitType.WEEKEND, 0); + allBenefit.replace(BenefitType.SPECIAL, 0); + } + + return allBenefit; + } + + private boolean isInsufficientTotalOrderPriceForEvent(Order order) { + return order.calculateTotalOrderPrice() < MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT; + } +} \ No newline at end of file
Java
(๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค!) ```suggestion return new EventResult(new HashMap<>() {{ this.put(BenefitType.GIFT, takeGift(order)); this.put(BenefitType.D_DAY, takeDDayDiscount(date)); this.put(BenefitType.WEEKDAY, takeWeekdayDiscount(date, order)); this.put(BenefitType.WEEKEND, takeWeekendDiscount(date, order)); this.put(BenefitType.SPECIAL, takeSpecialDiscount(date)); }}); ``` ์ด๋Ÿฐ์‹์œผ๋กœ ์ƒ์„ฑ๊ณผ ๋™์‹œ์— HashMap์˜ ์›์†Œ๋ฅผ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์–ด์š” :)
@@ -0,0 +1,100 @@ +package christmas.domain; + +import java.util.HashMap; +import java.util.Map; + +public class EventProcess { + private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT = 120000; + private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT = 10000; + private static final int D_DAY_DISCOUNT_AMOUNT_PER_DAY = 100; + private static final int D_DAY_DISCOUNT_DEFAULT_AMOUNT = 1000; + private static final int FIRST_DAY_NUMBER = 1; + private static final int WEEKDAY_DESSERT_DISCOUNT_AMOUNT = 2023; + private static final int WEEKEND_MAIN_DISCOUNT_AMOUNT = 2023; + private static final int SPECIAL_DISCOUNT_AMOUNT = 1000; + + public EventResult takeAllBenefit(Date date, Order order) { + Map<BenefitType, Integer> allBenefit = new HashMap<>(); + allBenefit.put(BenefitType.GIFT, takeGift(order)); + allBenefit.put(BenefitType.D_DAY, takeDDayDiscount(date)); + allBenefit.put(BenefitType.WEEKDAY, takeWeekdayDiscount(date, order)); + allBenefit.put(BenefitType.WEEKEND, takeWeekendDiscount(date, order)); + allBenefit.put(BenefitType.SPECIAL, takeSpecialDiscount(date)); + + cancelAllBenefitIfMeetCondition(order, allBenefit); + + return new EventResult(allBenefit); + } + + private int takeGift(Order order) { + if (order.calculateTotalOrderPrice() >= MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT) { + return Menu.CHAMPAGNE.getPrice(); + } + + return 0; + } + + private int takeDDayDiscount(Date date) { + if (date.isDDayDiscountActive()) { + return D_DAY_DISCOUNT_DEFAULT_AMOUNT + + (calculateDifferenceBetweenFirstDay(date) * D_DAY_DISCOUNT_AMOUNT_PER_DAY); + } + + return 0; + } + + private int calculateDifferenceBetweenFirstDay(Date date) { + return date.getDayNumber() - FIRST_DAY_NUMBER; + } + + private int takeWeekdayDiscount(Date date, Order order) { + int discount = 0; + + for (OrderedMenu orderedMenu : order.getOrder()) { + Menu menu = Menu.decideMenu(orderedMenu.getMenuName()); + if (date.isWeekdayDiscountActive() && MenuType.decideMenuType(menu) == MenuType.DESSERT) { + discount += orderedMenu.getQuantity() * WEEKDAY_DESSERT_DISCOUNT_AMOUNT; + } + } + + return discount; + } + + private int takeWeekendDiscount(Date date, Order order) { + int discount = 0; + + for (OrderedMenu orderedMenu : order.getOrder()) { + Menu menu = Menu.decideMenu(orderedMenu.getMenuName()); + if (date.isWeekendDiscountActive() && MenuType.decideMenuType(menu) == MenuType.MAIN) { + discount += orderedMenu.getQuantity() * WEEKEND_MAIN_DISCOUNT_AMOUNT; + } + } + + return discount; + } + + private int takeSpecialDiscount(Date date) { + if (date.isSpecialDiscountActive()) { + return SPECIAL_DISCOUNT_AMOUNT; + } + + return 0; + } + + private Map<BenefitType, Integer> cancelAllBenefitIfMeetCondition(Order order, + Map<BenefitType, Integer> allBenefit) { + if (isInsufficientTotalOrderPriceForEvent(order)) { + allBenefit.replace(BenefitType.GIFT, 0); + allBenefit.replace(BenefitType.D_DAY, 0); + allBenefit.replace(BenefitType.WEEKDAY, 0); + allBenefit.replace(BenefitType.WEEKEND, 0); + allBenefit.replace(BenefitType.SPECIAL, 0); + } + + return allBenefit; + } + + private boolean isInsufficientTotalOrderPriceForEvent(Order order) { + return order.calculateTotalOrderPrice() < MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT; + } +} \ No newline at end of file
Java
(๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค!) ํ• ์ธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์ „์— ์ฃผ๋ง์ด๋ฉด 0์›์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์•„์š”! ```suggestion if (date.isWeekendDiscountActive()) { return 0; } return order.getOrder() .stream() .filter(orderedMenu -> DESSERT.equals(MenuType.decideMenuType(Menu.decideMenu(orderedMenu.getMenuName())))) .mapToInt(orderMenu -> orderMenu.getQuantity() * WEEKDAY_DESSERT_DISCOUNT_AMOUNT) .sum(); ```
@@ -0,0 +1,85 @@ +package christmas.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class EventResult { + private static final String NON_BADGE = "์—†์Œ"; + private static final String STAR_BADGE = "๋ณ„"; + private static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + private static final String SANTA_BADGE = "์‚ฐํƒ€"; + private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000; + private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000; + private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000; + + private final Map<BenefitType, Integer> allBenefit; + + public EventResult(Map<BenefitType, Integer> allBenefit) { + this.allBenefit = allBenefit; + } + + public int calculateTotalBenefitAmount() { + int totalBenefitAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalBenefitAmount += allBenefit.get(benefitType); + } + + return totalBenefitAmount; + } + + public int calculateTotalDiscountAmount() { + int totalDiscountAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalDiscountAmount += allBenefit.get(benefitType); + } + if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) { + totalDiscountAmount -= Menu.CHAMPAGNE.getPrice(); + } + + return totalDiscountAmount; + } + + public boolean isReceivedGiftBenefit() { + return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice(); + } + + public List<BenefitType> checkWhichBenefitExist() { + List<BenefitType> existingBenefit = new ArrayList<>(); + BenefitType[] benefitTypes = BenefitType.values(); + + for (BenefitType benefitType : benefitTypes) { + if (allBenefit.get(benefitType) != 0) { + existingBenefit.add(benefitType); + } + } + + return existingBenefit; + } + + public String decideEventBadge() { + int totalBenefitAmount = calculateTotalBenefitAmount(); + + if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) { + return STAR_BADGE; + } + if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) { + return TREE_BADGE; + } + if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { + return SANTA_BADGE; + } + return NON_BADGE; + } + + public Map<BenefitType, Integer> getAllBenefit() { + return Collections.unmodifiableMap(allBenefit); + } +} \ No newline at end of file
Java
(์ถ”์ฒœ๋“œ๋ ค์š”!) EventResult์˜ badge๊ด€๋ จ ์ฝ”๋“œ๋Š” enum์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,52 @@ +package christmas.ui; + +import static christmas.domain.Date.DATE_RE_READ_REQUEST_MESSAGE; +import static christmas.domain.OrderedMenu.ORDER_RE_READ_REQUEST_MESSAGE; + +import java.util.ArrayList; +import java.util.List; + +public class Converter { + private static final String ORDER_DELIMITER = ","; + private static final String NAME_AND_AMOUNT_DELIMITER = "-"; + + public static int convertDateNumeric(String Input) { + try { + return Integer.parseInt(Input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE); + } + } + + public static List<String> separateOrder(String order) { + return new ArrayList<>(List.of(order.split(ORDER_DELIMITER))); + } + + public static List<String> separateNameAndAmount(String orderedMenu) { + validateAppropriateDelimiter(orderedMenu); + List<String> separatedNameAndAmount = new ArrayList<>(List.of(orderedMenu.split(NAME_AND_AMOUNT_DELIMITER))); + validateAppropriateForm(separatedNameAndAmount); + + return separatedNameAndAmount; + } + + private static void validateAppropriateDelimiter(String orderedMenu) { + if (!orderedMenu.contains(NAME_AND_AMOUNT_DELIMITER)) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + private static void validateAppropriateForm(List<String> separatedNameAndAmount) { + if (separatedNameAndAmount.size() != 2) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + public static int convertAmountNumeric(String input) { + try { + return Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } +} \ No newline at end of file
Java
(์ด๋ ‡๊ฒŒ๋„ ๊ฐ€๋Šฅํ•ด์š”!) ArrayList๋กœ ๋‹ค์‹œ ๊ฐ์‹ธ์ง€ ์•Š์•„๋„ ์ถฉ๋ถ„ํ•  ๊ฒƒ ๊ฐ™์•„์š” :) ```suggestion return List.of(order.split(ORDER_DELIMITER)); ```
@@ -0,0 +1,63 @@ +package christmas.ui; + +import camp.nextstep.edu.missionutils.Console; +import christmas.domain.Date; +import christmas.domain.Order; +import christmas.domain.OrderedMenu; +import java.util.ArrayList; +import java.util.List; + +public class InputView { + private static final String OPENING_MESSAGE = "์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค."; + private static final String DATE_REQUEST_MESSAGE = "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"; + private static final String ORDER_REQUEST_MESSAGE + = "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)"; + + public static void notifyProgramStarted() { + System.out.println(OPENING_MESSAGE); + } + + public static Date inputDate() { + try { + return readDate(); + } catch (IllegalArgumentException e) { + OutputView.printErrorMessage(e); + return inputDate(); + } + } + + private static Date readDate() { + System.out.println(DATE_REQUEST_MESSAGE); + + String input = Console.readLine(); + int dayNumber = Converter.convertDateNumeric(input); + + return new Date(dayNumber); + } + + public static Order inputOrder() { + try { + return new Order(readOrder()); + } catch (IllegalArgumentException e) { + OutputView.printErrorMessage(e); + return inputOrder(); + } + } + + private static List<OrderedMenu> readOrder() { + System.out.println(ORDER_REQUEST_MESSAGE); + + String input = Console.readLine(); + List<String> separatedOrder = Converter.separateOrder(input); + List<OrderedMenu> order = new ArrayList<>(); + + for (String orderedMenu : separatedOrder) { + List<String> separatedNameAndAmount = Converter.separateNameAndAmount(orderedMenu); + String name = separatedNameAndAmount.get(0); + int amount = Converter.convertAmountNumeric(separatedNameAndAmount.get(1)); + order.add(new OrderedMenu(name, amount)); + } + + return order; + } +} \ No newline at end of file
Java
(์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์–ด์š”!) ```suggestion private static List<OrderedMenu> readOrder() { System.out.println(ORDER_REQUEST_MESSAGE); String input = Console.readLine(); List<String> separatedOrder = Converter.separateOrder(input); return separatedOrder.stream() .map(InputView::convertNameToOrderedMenu) .toList(); } private static OrderedMenu convertNameToOrderedMenu(String orderMenu) { List<String> separatedNameAndAmount = Converter.separateNameAndAmount(orderMenu); String name = separatedNameAndAmount.get(0); int amount = Converter.convertAmountNumeric(separatedNameAndAmount.get(1)); return new OrderedMenu(name, amount); } ```
@@ -0,0 +1,60 @@ +package christmas.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class EventProcessTest { + @DisplayName("์ด๋ฒคํŠธ์— ์ถฉ๋ถ„ํ•œ ์ „์ฒด ์ฃผ๋ฌธ ๊ธˆ์•ก์œผ๋กœ ์ „์ฒด ํ˜œํƒ ๋„์ถœ") + @Test + void takeAllBenefitWithSufficientTotalOrderPrice() { + Date dateInput = new Date(3); + Order orderInput = new Order(setUpSufficientTotalOrderPriceOrder()); + EventProcess eventProcess = new EventProcess(); + EventResult eventResult = eventProcess.takeAllBenefit(dateInput, orderInput); + Map<BenefitType, Integer> result = eventResult.getAllBenefit(); + + assertThat(result.get(BenefitType.GIFT)).isEqualTo(25000); + assertThat(result.get(BenefitType.D_DAY)).isEqualTo(1200); + assertThat(result.get(BenefitType.WEEKDAY)).isEqualTo(2023); + assertThat(result.get(BenefitType.WEEKEND)).isEqualTo(0); + assertThat(result.get(BenefitType.SPECIAL)).isEqualTo(1000); + } + + private List<OrderedMenu> setUpSufficientTotalOrderPriceOrder() { + List<OrderedMenu> orderedMenus = new ArrayList<>(); + orderedMenus.add(new OrderedMenu("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 2)); + orderedMenus.add(new OrderedMenu("๋ ˆ๋“œ์™€์ธ", 1)); + orderedMenus.add(new OrderedMenu("์ดˆ์ฝ”์ผ€์ดํฌ", 1)); + + return orderedMenus; + } + + @DisplayName("์ด๋ฒคํŠธ์— ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์€ ์ „์ฒด ์ฃผ๋ฌธ ๊ธˆ์•ก์œผ๋กœ ์ „์ฒด ํ˜œํƒ ๋„์ถœ") + @Test + void takeAllBenefitWithInSufficientTotalOrderPrice() { + Date dateInput = new Date(26); + Order orderInput = new Order(setUpInSufficientTotalOrderPriceOrder()); + EventProcess eventProcess = new EventProcess(); + EventResult eventResult = eventProcess.takeAllBenefit(dateInput, orderInput); + Map<BenefitType, Integer> result = eventResult.getAllBenefit(); + + assertThat(result.get(BenefitType.GIFT)).isEqualTo(0); + assertThat(result.get(BenefitType.D_DAY)).isEqualTo(0); + assertThat(result.get(BenefitType.WEEKDAY)).isEqualTo(0); + assertThat(result.get(BenefitType.WEEKEND)).isEqualTo(0); + assertThat(result.get(BenefitType.SPECIAL)).isEqualTo(0); + } + + private List<OrderedMenu> setUpInSufficientTotalOrderPriceOrder() { + List<OrderedMenu> orderedMenus = new ArrayList<>(); + orderedMenus.add(new OrderedMenu("ํƒ€ํŒŒ์Šค", 1)); + orderedMenus.add(new OrderedMenu("์ œ๋กœ์ฝœ๋ผ", 1)); + + return orderedMenus; + } +} \ No newline at end of file
Java
(์ถ”์ฒœ๋“œ๋ ค์š”) assertThat๋งŒ ์‚ฌ์šฉํ•˜๋ฉด 21๋ฒˆ์งธ ์ค„์—์„œ ์‹คํŒจํ–ˆ์„ ๋•Œ ๋ฐ‘์— 22~25๋ฒˆ ๋ผ์ธ๊นŒ์ง€ ๊ฒ€์ฆํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค! ๋ฐ˜๋ฉด assertAll์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  assertThat์„ ์‹คํ–‰์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด์—‡์ด ์‹คํŒจํ–ˆ๋Š”์ง€ ํ™•์‹คํ•˜๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค! ```suggestion assertAll( () -> assertThat(result.get(BenefitType.GIFT)).isEqualTo(25000), () -> assertThat(result.get(BenefitType.D_DAY)).isEqualTo(1200), () -> assertThat(result.get(BenefitType.WEEKDAY)).isEqualTo(2023), () -> assertThat(result.get(BenefitType.WEEKEND)).isEqualTo(0), () -> assertThat(result.get(BenefitType.SPECIAL)).isEqualTo(1000) ); ```
@@ -0,0 +1,23 @@ +package christmas.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class MenuTest { + @DisplayName("์ฃผ์–ด์ง„ ์ด๋ฆ„์„ ํ† ๋Œ€๋กœ ๋ฉ”๋‰ด๋ฅผ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค.") + @Test + void decideMenu() { + String inputTapas = "ํƒ€ํŒŒ์Šค"; + String inputSeafoodPasta = "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€"; + String inputRedWine = "๋ ˆ๋“œ์™€์ธ"; + Menu inputTapasResult = Menu.decideMenu(inputTapas); + Menu inputSeafoodPastaResult = Menu.decideMenu(inputSeafoodPasta); + Menu inputRedWineResult = Menu.decideMenu(inputRedWine); + + assertThat(inputTapasResult).isEqualTo(Menu.TAPAS); + assertThat(inputSeafoodPastaResult).isEqualTo(Menu.SEAFOOD_PASTA); + assertThat(inputRedWineResult).isEqualTo(Menu.RED_WINE); + } +} \ No newline at end of file
Java
(๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค!) ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋ฅผ ๋‘˜๋Ÿฌ๋ณด๋‹ˆ ๋Œ€๋ถ€๋ถ„ ํ•ดํ”ผ์ผ€์ด์Šค๋งŒ ์žˆ๋”๋ผ๊ตฌ์š”! ์‹คํŒจ์— ๋Œ€ํ•œ ๊ฒ€์ฆ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋„ ์—ฐ์Šตํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,58 @@ +package christmas.domain; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +public class Date { + public static final String DATE_RE_READ_REQUEST_MESSAGE = "์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final LocalDate CHRISTMAS_DAY = LocalDate.of(2023, 12, 25); + private static final int YEAR = 2023; + private static final int MONTH = 12; + private static final int DATE_MIN_NUMBER = 1; + private static final int DATE_MAX_NUMBER = 31; + + private final int dayNumber; + + public Date(int dayNumber) { + validateRange(dayNumber); + this.dayNumber = dayNumber; + } + + private void validateRange(int dayNumber) { + if ((dayNumber < DATE_MIN_NUMBER) || (dayNumber > DATE_MAX_NUMBER)) { + throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE); + } + } + + public boolean isDDayDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + + return localDate.isBefore(CHRISTMAS_DAY.plusDays(1)); + } + + public boolean isWeekdayDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + DayOfWeek day = localDate.getDayOfWeek(); + + return (day.equals(DayOfWeek.MONDAY) || day.equals(DayOfWeek.TUESDAY) || day.equals(DayOfWeek.WEDNESDAY) + || day.equals(DayOfWeek.THURSDAY) || day.equals(DayOfWeek.SUNDAY)); + } + + public boolean isWeekendDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + DayOfWeek day = localDate.getDayOfWeek(); + + return (day.equals(DayOfWeek.FRIDAY) || day.equals(DayOfWeek.SATURDAY)); + } + + public boolean isSpecialDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + DayOfWeek day = localDate.getDayOfWeek(); + + return (day.equals(DayOfWeek.SUNDAY) || localDate.equals(CHRISTMAS_DAY)); + } + + public int getDayNumber() { + return dayNumber; + } +} \ No newline at end of file
Java
์˜ค,, ์ด๋ฒˆ์— LocalDate๋ฅผ ์ฒ˜์Œ ์‚ฌ์šฉํ•ด๋ด์„œ ์ต์ˆ™ํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ์š” ์ด๋Ÿฐ ๋ฐฉ๋ฒ•๋„ ์žˆ์—ˆ๊ตฐ์š”,, ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,100 @@ +package christmas.domain; + +import java.util.HashMap; +import java.util.Map; + +public class EventProcess { + private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT = 120000; + private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT = 10000; + private static final int D_DAY_DISCOUNT_AMOUNT_PER_DAY = 100; + private static final int D_DAY_DISCOUNT_DEFAULT_AMOUNT = 1000; + private static final int FIRST_DAY_NUMBER = 1; + private static final int WEEKDAY_DESSERT_DISCOUNT_AMOUNT = 2023; + private static final int WEEKEND_MAIN_DISCOUNT_AMOUNT = 2023; + private static final int SPECIAL_DISCOUNT_AMOUNT = 1000; + + public EventResult takeAllBenefit(Date date, Order order) { + Map<BenefitType, Integer> allBenefit = new HashMap<>(); + allBenefit.put(BenefitType.GIFT, takeGift(order)); + allBenefit.put(BenefitType.D_DAY, takeDDayDiscount(date)); + allBenefit.put(BenefitType.WEEKDAY, takeWeekdayDiscount(date, order)); + allBenefit.put(BenefitType.WEEKEND, takeWeekendDiscount(date, order)); + allBenefit.put(BenefitType.SPECIAL, takeSpecialDiscount(date)); + + cancelAllBenefitIfMeetCondition(order, allBenefit); + + return new EventResult(allBenefit); + } + + private int takeGift(Order order) { + if (order.calculateTotalOrderPrice() >= MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT) { + return Menu.CHAMPAGNE.getPrice(); + } + + return 0; + } + + private int takeDDayDiscount(Date date) { + if (date.isDDayDiscountActive()) { + return D_DAY_DISCOUNT_DEFAULT_AMOUNT + + (calculateDifferenceBetweenFirstDay(date) * D_DAY_DISCOUNT_AMOUNT_PER_DAY); + } + + return 0; + } + + private int calculateDifferenceBetweenFirstDay(Date date) { + return date.getDayNumber() - FIRST_DAY_NUMBER; + } + + private int takeWeekdayDiscount(Date date, Order order) { + int discount = 0; + + for (OrderedMenu orderedMenu : order.getOrder()) { + Menu menu = Menu.decideMenu(orderedMenu.getMenuName()); + if (date.isWeekdayDiscountActive() && MenuType.decideMenuType(menu) == MenuType.DESSERT) { + discount += orderedMenu.getQuantity() * WEEKDAY_DESSERT_DISCOUNT_AMOUNT; + } + } + + return discount; + } + + private int takeWeekendDiscount(Date date, Order order) { + int discount = 0; + + for (OrderedMenu orderedMenu : order.getOrder()) { + Menu menu = Menu.decideMenu(orderedMenu.getMenuName()); + if (date.isWeekendDiscountActive() && MenuType.decideMenuType(menu) == MenuType.MAIN) { + discount += orderedMenu.getQuantity() * WEEKEND_MAIN_DISCOUNT_AMOUNT; + } + } + + return discount; + } + + private int takeSpecialDiscount(Date date) { + if (date.isSpecialDiscountActive()) { + return SPECIAL_DISCOUNT_AMOUNT; + } + + return 0; + } + + private Map<BenefitType, Integer> cancelAllBenefitIfMeetCondition(Order order, + Map<BenefitType, Integer> allBenefit) { + if (isInsufficientTotalOrderPriceForEvent(order)) { + allBenefit.replace(BenefitType.GIFT, 0); + allBenefit.replace(BenefitType.D_DAY, 0); + allBenefit.replace(BenefitType.WEEKDAY, 0); + allBenefit.replace(BenefitType.WEEKEND, 0); + allBenefit.replace(BenefitType.SPECIAL, 0); + } + + return allBenefit; + } + + private boolean isInsufficientTotalOrderPriceForEvent(Order order) { + return order.calculateTotalOrderPrice() < MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT; + } +} \ No newline at end of file
Java
๋ฏธ์…˜ ๋™์•ˆ์€ ์‹œ๊ฐ„์ด ์ด‰๋ฐ•ํ–ˆ์–ด์„œ stream๊ณต๋ถ€๋ฅผ ํ•˜์ง€ ๋ชปํ–ˆ๋Š”๋ฐ์š”, ์•ž์œผ๋กœ ๊ณต๋ถ€ํ•ด์„œ ์ถ”์ฒœํ•ด์ฃผ์‹  ๋ฐฉ๋ฒ• ์ดํ•ดํ•ด๋ณด๋„๋ก ํ• ๊ฒŒ์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!
@@ -0,0 +1,100 @@ +package christmas.domain; + +import java.util.HashMap; +import java.util.Map; + +public class EventProcess { + private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT = 120000; + private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT = 10000; + private static final int D_DAY_DISCOUNT_AMOUNT_PER_DAY = 100; + private static final int D_DAY_DISCOUNT_DEFAULT_AMOUNT = 1000; + private static final int FIRST_DAY_NUMBER = 1; + private static final int WEEKDAY_DESSERT_DISCOUNT_AMOUNT = 2023; + private static final int WEEKEND_MAIN_DISCOUNT_AMOUNT = 2023; + private static final int SPECIAL_DISCOUNT_AMOUNT = 1000; + + public EventResult takeAllBenefit(Date date, Order order) { + Map<BenefitType, Integer> allBenefit = new HashMap<>(); + allBenefit.put(BenefitType.GIFT, takeGift(order)); + allBenefit.put(BenefitType.D_DAY, takeDDayDiscount(date)); + allBenefit.put(BenefitType.WEEKDAY, takeWeekdayDiscount(date, order)); + allBenefit.put(BenefitType.WEEKEND, takeWeekendDiscount(date, order)); + allBenefit.put(BenefitType.SPECIAL, takeSpecialDiscount(date)); + + cancelAllBenefitIfMeetCondition(order, allBenefit); + + return new EventResult(allBenefit); + } + + private int takeGift(Order order) { + if (order.calculateTotalOrderPrice() >= MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT) { + return Menu.CHAMPAGNE.getPrice(); + } + + return 0; + } + + private int takeDDayDiscount(Date date) { + if (date.isDDayDiscountActive()) { + return D_DAY_DISCOUNT_DEFAULT_AMOUNT + + (calculateDifferenceBetweenFirstDay(date) * D_DAY_DISCOUNT_AMOUNT_PER_DAY); + } + + return 0; + } + + private int calculateDifferenceBetweenFirstDay(Date date) { + return date.getDayNumber() - FIRST_DAY_NUMBER; + } + + private int takeWeekdayDiscount(Date date, Order order) { + int discount = 0; + + for (OrderedMenu orderedMenu : order.getOrder()) { + Menu menu = Menu.decideMenu(orderedMenu.getMenuName()); + if (date.isWeekdayDiscountActive() && MenuType.decideMenuType(menu) == MenuType.DESSERT) { + discount += orderedMenu.getQuantity() * WEEKDAY_DESSERT_DISCOUNT_AMOUNT; + } + } + + return discount; + } + + private int takeWeekendDiscount(Date date, Order order) { + int discount = 0; + + for (OrderedMenu orderedMenu : order.getOrder()) { + Menu menu = Menu.decideMenu(orderedMenu.getMenuName()); + if (date.isWeekendDiscountActive() && MenuType.decideMenuType(menu) == MenuType.MAIN) { + discount += orderedMenu.getQuantity() * WEEKEND_MAIN_DISCOUNT_AMOUNT; + } + } + + return discount; + } + + private int takeSpecialDiscount(Date date) { + if (date.isSpecialDiscountActive()) { + return SPECIAL_DISCOUNT_AMOUNT; + } + + return 0; + } + + private Map<BenefitType, Integer> cancelAllBenefitIfMeetCondition(Order order, + Map<BenefitType, Integer> allBenefit) { + if (isInsufficientTotalOrderPriceForEvent(order)) { + allBenefit.replace(BenefitType.GIFT, 0); + allBenefit.replace(BenefitType.D_DAY, 0); + allBenefit.replace(BenefitType.WEEKDAY, 0); + allBenefit.replace(BenefitType.WEEKEND, 0); + allBenefit.replace(BenefitType.SPECIAL, 0); + } + + return allBenefit; + } + + private boolean isInsufficientTotalOrderPriceForEvent(Order order) { + return order.calculateTotalOrderPrice() < MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT; + } +} \ No newline at end of file
Java
ํ›จ์”ฌ ๊น”๋”ํ•˜๋„ค์š”,, ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!
@@ -0,0 +1,85 @@ +package christmas.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class EventResult { + private static final String NON_BADGE = "์—†์Œ"; + private static final String STAR_BADGE = "๋ณ„"; + private static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + private static final String SANTA_BADGE = "์‚ฐํƒ€"; + private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000; + private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000; + private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000; + + private final Map<BenefitType, Integer> allBenefit; + + public EventResult(Map<BenefitType, Integer> allBenefit) { + this.allBenefit = allBenefit; + } + + public int calculateTotalBenefitAmount() { + int totalBenefitAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalBenefitAmount += allBenefit.get(benefitType); + } + + return totalBenefitAmount; + } + + public int calculateTotalDiscountAmount() { + int totalDiscountAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalDiscountAmount += allBenefit.get(benefitType); + } + if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) { + totalDiscountAmount -= Menu.CHAMPAGNE.getPrice(); + } + + return totalDiscountAmount; + } + + public boolean isReceivedGiftBenefit() { + return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice(); + } + + public List<BenefitType> checkWhichBenefitExist() { + List<BenefitType> existingBenefit = new ArrayList<>(); + BenefitType[] benefitTypes = BenefitType.values(); + + for (BenefitType benefitType : benefitTypes) { + if (allBenefit.get(benefitType) != 0) { + existingBenefit.add(benefitType); + } + } + + return existingBenefit; + } + + public String decideEventBadge() { + int totalBenefitAmount = calculateTotalBenefitAmount(); + + if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) { + return STAR_BADGE; + } + if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) { + return TREE_BADGE; + } + if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { + return SANTA_BADGE; + } + return NON_BADGE; + } + + public Map<BenefitType, Integer> getAllBenefit() { + return Collections.unmodifiableMap(allBenefit); + } +} \ No newline at end of file
Java
์ถœ๋ ฅ ํ—ค๋”๋“ค์„ enum์œผ๋กœ ๊ด€๋ฆฌํ•  ์ง€ badge๋ฅผ enum์œผ๋กœ ๊ด€๋ฆฌํ•  ์ง€ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ์ถœ๋ ฅ ํ—ค๋”๋“ค๋งŒ enum์œผ๋กœ ๊ด€๋ฆฌํ–ˆ๋Š”๋ฐ์š”, ๋ฆฌ๋ทฐ ๋‚จ๊ฒจ๋“œ๋ฆฐ ๊ฒƒ์— ๋‹ต๋ณ€ ์ฃผ์‹  ํ•œ๋ฒˆ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๊ตณ์ด enum์œผ๋กœ ๊ด€๋ฆฌ ํ•˜์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™๋‹ค๋Š” ๋ง์”€์— ๊ณต๊ณต๊ฐํ•ด์„œ ๋ฐ˜๋Œ€๋กœ badge๋Š” ๊ผญ enum์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์•˜์„ ๊ฒƒ์ด๋ผ๋Š” ์ƒ๊ฐ๋„ ๋“œ๋„ค์š”. ๊ฐ์‚ฌํ•ด์š”
@@ -0,0 +1,60 @@ +package christmas.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class EventProcessTest { + @DisplayName("์ด๋ฒคํŠธ์— ์ถฉ๋ถ„ํ•œ ์ „์ฒด ์ฃผ๋ฌธ ๊ธˆ์•ก์œผ๋กœ ์ „์ฒด ํ˜œํƒ ๋„์ถœ") + @Test + void takeAllBenefitWithSufficientTotalOrderPrice() { + Date dateInput = new Date(3); + Order orderInput = new Order(setUpSufficientTotalOrderPriceOrder()); + EventProcess eventProcess = new EventProcess(); + EventResult eventResult = eventProcess.takeAllBenefit(dateInput, orderInput); + Map<BenefitType, Integer> result = eventResult.getAllBenefit(); + + assertThat(result.get(BenefitType.GIFT)).isEqualTo(25000); + assertThat(result.get(BenefitType.D_DAY)).isEqualTo(1200); + assertThat(result.get(BenefitType.WEEKDAY)).isEqualTo(2023); + assertThat(result.get(BenefitType.WEEKEND)).isEqualTo(0); + assertThat(result.get(BenefitType.SPECIAL)).isEqualTo(1000); + } + + private List<OrderedMenu> setUpSufficientTotalOrderPriceOrder() { + List<OrderedMenu> orderedMenus = new ArrayList<>(); + orderedMenus.add(new OrderedMenu("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 2)); + orderedMenus.add(new OrderedMenu("๋ ˆ๋“œ์™€์ธ", 1)); + orderedMenus.add(new OrderedMenu("์ดˆ์ฝ”์ผ€์ดํฌ", 1)); + + return orderedMenus; + } + + @DisplayName("์ด๋ฒคํŠธ์— ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์€ ์ „์ฒด ์ฃผ๋ฌธ ๊ธˆ์•ก์œผ๋กœ ์ „์ฒด ํ˜œํƒ ๋„์ถœ") + @Test + void takeAllBenefitWithInSufficientTotalOrderPrice() { + Date dateInput = new Date(26); + Order orderInput = new Order(setUpInSufficientTotalOrderPriceOrder()); + EventProcess eventProcess = new EventProcess(); + EventResult eventResult = eventProcess.takeAllBenefit(dateInput, orderInput); + Map<BenefitType, Integer> result = eventResult.getAllBenefit(); + + assertThat(result.get(BenefitType.GIFT)).isEqualTo(0); + assertThat(result.get(BenefitType.D_DAY)).isEqualTo(0); + assertThat(result.get(BenefitType.WEEKDAY)).isEqualTo(0); + assertThat(result.get(BenefitType.WEEKEND)).isEqualTo(0); + assertThat(result.get(BenefitType.SPECIAL)).isEqualTo(0); + } + + private List<OrderedMenu> setUpInSufficientTotalOrderPriceOrder() { + List<OrderedMenu> orderedMenus = new ArrayList<>(); + orderedMenus.add(new OrderedMenu("ํƒ€ํŒŒ์Šค", 1)); + orderedMenus.add(new OrderedMenu("์ œ๋กœ์ฝœ๋ผ", 1)); + + return orderedMenus; + } +} \ No newline at end of file
Java
์™€,, ํ™•์‹คํ•˜๊ฒŒ ํ…Œ์ŠคํŠธ ํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!
@@ -0,0 +1,23 @@ +package christmas.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class MenuTest { + @DisplayName("์ฃผ์–ด์ง„ ์ด๋ฆ„์„ ํ† ๋Œ€๋กœ ๋ฉ”๋‰ด๋ฅผ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค.") + @Test + void decideMenu() { + String inputTapas = "ํƒ€ํŒŒ์Šค"; + String inputSeafoodPasta = "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€"; + String inputRedWine = "๋ ˆ๋“œ์™€์ธ"; + Menu inputTapasResult = Menu.decideMenu(inputTapas); + Menu inputSeafoodPastaResult = Menu.decideMenu(inputSeafoodPasta); + Menu inputRedWineResult = Menu.decideMenu(inputRedWine); + + assertThat(inputTapasResult).isEqualTo(Menu.TAPAS); + assertThat(inputSeafoodPastaResult).isEqualTo(Menu.SEAFOOD_PASTA); + assertThat(inputRedWineResult).isEqualTo(Menu.RED_WINE); + } +} \ No newline at end of file
Java
๋งž์•„์š” ๊ฐœ์ธ์ ์œผ๋กœ ๊ฐ€์žฅ ์•„์‰ฌ์šด ๋ถ€๋ถ„์ธ๋ฐ์š”,, ๊ตฌํ˜„ํ•˜๊ณ  ๋ฆฌํŒฉํ„ฐ๋ง์„ ํ•˜๋„ ๋งŽ์ด ํ•ด์„œ ์‹œ๊ฐ„์ด ๋งค์šฐ ์ด‰๋ฐ•ํ–ˆ๋‹ค๋Š”,, ใ… ใ… 
@@ -0,0 +1,39 @@ +package christmas.domain; + +import java.util.Arrays; +import java.util.List; + +public enum MenuType { + APPETIZER("์• ํ”ผํƒ€์ด์ €", Arrays.asList(Menu.MUSHROOM_SOUP, Menu.TAPAS, Menu.CAESAR_SALAD)), + MAIN("๋ฉ”์ธ", Arrays.asList(Menu.T_BONE_STAKE, Menu.BARBECUE_LIP, Menu.SEAFOOD_PASTA, Menu.CHRISTMAS_PASTA)), + DESSERT("๋””์ €ํŠธ", Arrays.asList(Menu.CHOCOLATE_CAKE, Menu.ICE_CREAM)), + BEVERAGE("์Œ๋ฃŒ", Arrays.asList(Menu.ZERO_COKE, Menu.RED_WINE, Menu.CHAMPAGNE)); + + private final String name; + private final List<Menu> menus; + + MenuType(String name, List<Menu> menus) { + this.name = name; + this.menus = menus; + } + + public static MenuType decideMenuType(Menu menu) { + MenuType[] menuTypes = values(); + + for (MenuType menuType : menuTypes) { + if (menuType.getMenus().contains(menu)) { + return menuType; + } + } + + return null; + } + + public String getName() { + return name; + } + + public List<Menu> getMenus() { + return menus; + } +} \ No newline at end of file
Java
์ €ํ•œํ…Œ ๋‚จ๊ฒจ์ฃผ์…จ๋˜ ์ฝ”๋“œ ๋ฆฌ๋ทฐ์—์„œ ์–ธ๊ธ‰ํ•˜์‹  ํŒจํ„ด์ด๋„ค์š”! ์ข‹์€ ๊ณต๋ถ€๊ฐ€ ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,21 @@ +package christmas.domain; + +public enum OrderResultType { + ORDER_MENU("<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"), + TOTAL_ORDER_PRICE("<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>"), + GIFT_MENU("<์ฆ์ • ๋ฉ”๋‰ด>"), + BENEFIT_STATISTICS("<ํ˜œํƒ ๋‚ด์—ญ>"), + TOTAL_BENEFIT_AMOUNT("<์ดํ˜œํƒ ๊ธˆ์•ก>"), + ESTIMATED_PAYMENT("<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>"), + EVENT_BADGE("<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>"); + + private final String name; + + OrderResultType(String name) { + this.name = name; + } + + public String getName() { + return name; + } +}
Java
๐Ÿค” `OrderResultType`์˜ ์ œ๋ชฉ์€ ๊ทธ๋ ‡๋‹ค ์ณ๋„, < > ๊ฐ™์€ character ๋“ค์€ View์˜ ์ฑ…์ž„์— ๊ฐ€๊นŒ์›Œ ๋ณด์—ฌ์š”. View์—์„œ`"<%s>"` ์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ฑ…์ž„์„ ๋ถ„๋ฆฌํ•˜๊ณ  formatting์„ ์ด์šฉํ•ด ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋” ๋ณ€๊ฒฝ์— ๊ฐ•ํ•œ ์„ค๊ณ„ ์•„๋‹๊นŒ์š”?
@@ -0,0 +1,45 @@ +package christmas.domain; + +public class OrderedMenu { + public static final String ORDER_RE_READ_REQUEST_MESSAGE = "์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final int AMOUNT_MINIMUM_QUANTITY = 1; + + private final String menuName; + private final int quantity; + + public OrderedMenu(String menuName, int quantity) { + validateOrderNameExistInMenu(menuName); + validateOrderAmountQuantity(quantity); + + this.menuName = menuName; + this.quantity = quantity; + } + + private void validateOrderNameExistInMenu(String name) { + if (Menu.decideMenu(name) == Menu.NONE) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + private void validateOrderAmountQuantity(int amount) { + if (amount < AMOUNT_MINIMUM_QUANTITY) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + public boolean isBeverage() { + return MenuType.decideMenuType(Menu.decideMenu(menuName)) == MenuType.BEVERAGE; + } + + public int calculatePrice() { + return Menu.decideMenu(menuName).getPrice() * quantity; + } + + public String getMenuName() { + return menuName; + } + + public int getQuantity() { + return quantity; + } +} \ No newline at end of file
Java
`Menu` ์— ์†ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฒ€์ฆ์ด ๋˜์—ˆ๋‹ค๋ฉด, field๋ฅผ `String`์ด ์•„๋‹Œ `Menu`๋กœ ์ €์žฅํ•˜๋Š”๊ฒŒ ๋” ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์•„์š”. enum ์‚ฌ์šฉ์˜ ์˜๋ฏธ๊ฐ€ ์•ฝ๊ฐ„ ํ‡ด์ƒ‰๋˜์ง€ ์•Š์•˜๋‚˜ ์‹ถ๋„ค์š”!
@@ -0,0 +1,52 @@ +package christmas.ui; + +import static christmas.domain.Date.DATE_RE_READ_REQUEST_MESSAGE; +import static christmas.domain.OrderedMenu.ORDER_RE_READ_REQUEST_MESSAGE; + +import java.util.ArrayList; +import java.util.List; + +public class Converter { + private static final String ORDER_DELIMITER = ","; + private static final String NAME_AND_AMOUNT_DELIMITER = "-"; + + public static int convertDateNumeric(String Input) { + try { + return Integer.parseInt(Input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE); + } + } + + public static List<String> separateOrder(String order) { + return new ArrayList<>(List.of(order.split(ORDER_DELIMITER))); + } + + public static List<String> separateNameAndAmount(String orderedMenu) { + validateAppropriateDelimiter(orderedMenu); + List<String> separatedNameAndAmount = new ArrayList<>(List.of(orderedMenu.split(NAME_AND_AMOUNT_DELIMITER))); + validateAppropriateForm(separatedNameAndAmount); + + return separatedNameAndAmount; + } + + private static void validateAppropriateDelimiter(String orderedMenu) { + if (!orderedMenu.contains(NAME_AND_AMOUNT_DELIMITER)) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + private static void validateAppropriateForm(List<String> separatedNameAndAmount) { + if (separatedNameAndAmount.size() != 2) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + public static int convertAmountNumeric(String input) { + try { + return Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } +} \ No newline at end of file
Java
String์„ ์˜ค๋ธŒ์ ํŠธ๋กœ ๋ฐ”๊พธ๋Š” ํด๋ž˜์Šค๋Š” `Parser`๋ผ๊ณ  ์ด๋ฆ„ ์ง“๋Š” ๊ฒƒ์ด ๋” convention์— ๊ฐ€๊นŒ์šธ ๊ฒƒ ๊ฐ™์€๋ฐ, ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,63 @@ +package christmas.ui; + +import camp.nextstep.edu.missionutils.Console; +import christmas.domain.Date; +import christmas.domain.Order; +import christmas.domain.OrderedMenu; +import java.util.ArrayList; +import java.util.List; + +public class InputView { + private static final String OPENING_MESSAGE = "์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค."; + private static final String DATE_REQUEST_MESSAGE = "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"; + private static final String ORDER_REQUEST_MESSAGE + = "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)"; + + public static void notifyProgramStarted() { + System.out.println(OPENING_MESSAGE); + } + + public static Date inputDate() { + try { + return readDate(); + } catch (IllegalArgumentException e) { + OutputView.printErrorMessage(e); + return inputDate(); + } + } + + private static Date readDate() { + System.out.println(DATE_REQUEST_MESSAGE); + + String input = Console.readLine(); + int dayNumber = Converter.convertDateNumeric(input); + + return new Date(dayNumber); + } + + public static Order inputOrder() { + try { + return new Order(readOrder()); + } catch (IllegalArgumentException e) { + OutputView.printErrorMessage(e); + return inputOrder(); + } + } + + private static List<OrderedMenu> readOrder() { + System.out.println(ORDER_REQUEST_MESSAGE); + + String input = Console.readLine(); + List<String> separatedOrder = Converter.separateOrder(input); + List<OrderedMenu> order = new ArrayList<>(); + + for (String orderedMenu : separatedOrder) { + List<String> separatedNameAndAmount = Converter.separateNameAndAmount(orderedMenu); + String name = separatedNameAndAmount.get(0); + int amount = Converter.convertAmountNumeric(separatedNameAndAmount.get(1)); + order.add(new OrderedMenu(name, amount)); + } + + return order; + } +} \ No newline at end of file
Java
Supplier์˜ ํ˜•ํƒœ๋กœ handling ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์–ด์š”. inputDate์™€ inputOrder์˜ ํ˜•ํƒœ๊ฐ€ ๊ต‰์žฅํžˆ ๋‹ฎ์•„์žˆ์–ด์„œ, ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ ์šฉ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์œผ๋‹ˆ ์ด ์ž๋ฃŒ ํ•œ๋ฒˆ ์ฝ์–ด๋ณด์‹œ๊ธธ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. https://hwan33.tistory.com/17
@@ -0,0 +1,85 @@ +package christmas.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class EventResult { + private static final String NON_BADGE = "์—†์Œ"; + private static final String STAR_BADGE = "๋ณ„"; + private static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + private static final String SANTA_BADGE = "์‚ฐํƒ€"; + private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000; + private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000; + private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000; + + private final Map<BenefitType, Integer> allBenefit; + + public EventResult(Map<BenefitType, Integer> allBenefit) { + this.allBenefit = allBenefit; + } + + public int calculateTotalBenefitAmount() { + int totalBenefitAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalBenefitAmount += allBenefit.get(benefitType); + } + + return totalBenefitAmount; + } + + public int calculateTotalDiscountAmount() { + int totalDiscountAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalDiscountAmount += allBenefit.get(benefitType); + } + if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) { + totalDiscountAmount -= Menu.CHAMPAGNE.getPrice(); + } + + return totalDiscountAmount; + } + + public boolean isReceivedGiftBenefit() { + return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice(); + } + + public List<BenefitType> checkWhichBenefitExist() { + List<BenefitType> existingBenefit = new ArrayList<>(); + BenefitType[] benefitTypes = BenefitType.values(); + + for (BenefitType benefitType : benefitTypes) { + if (allBenefit.get(benefitType) != 0) { + existingBenefit.add(benefitType); + } + } + + return existingBenefit; + } + + public String decideEventBadge() { + int totalBenefitAmount = calculateTotalBenefitAmount(); + + if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) { + return STAR_BADGE; + } + if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) { + return TREE_BADGE; + } + if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { + return SANTA_BADGE; + } + return NON_BADGE; + } + + public Map<BenefitType, Integer> getAllBenefit() { + return Collections.unmodifiableMap(allBenefit); + } +} \ No newline at end of file
Java
Early return์„ ์‚ฌ์šฉํ•˜์…จ๋Š”๋ฐ, ๊ตณ์ด ๋ฒ”์œ„๋ฅผ max, min์„ ์ด์šฉํ•ด ์ผ์ผ์ด ์„ค๋ช…ํ•˜์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”. ๊ผญ maximum_amount๊ฐ€ ํ•„์š”ํ–ˆ์„๊นŒ์š”? (๊ทธ๋ฆฌ๊ณ  ๋ณ€์ˆ˜๋ช…์„ maximum์ด๋ผ๊ณ  ๋ณด๊ธฐ์—” ํ•ด๋‹น ๋ถ€๋ถ„์„ ํฌํ•จํ•˜์ง€ ์•Š์•„์„œ ํ˜ผ๋™์ด ์žˆ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฐจ๋ผ๋ฆฌ `STAR_BADGE_MAXIMUM_AMOUNT = 9999`์™€ ๊ฐ™์€ ๋ฐฉ์‹์ด์—ˆ๋‹ค๋ฉด...) ```suggestion if (totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) { return STAR_BADGE; } if (totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) { return TREE_BADGE; } if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { return SANTA_BADGE; } return NON_BADGE; ```
@@ -0,0 +1,85 @@ +package christmas.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class EventResult { + private static final String NON_BADGE = "์—†์Œ"; + private static final String STAR_BADGE = "๋ณ„"; + private static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + private static final String SANTA_BADGE = "์‚ฐํƒ€"; + private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000; + private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000; + private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000; + + private final Map<BenefitType, Integer> allBenefit; + + public EventResult(Map<BenefitType, Integer> allBenefit) { + this.allBenefit = allBenefit; + } + + public int calculateTotalBenefitAmount() { + int totalBenefitAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalBenefitAmount += allBenefit.get(benefitType); + } + + return totalBenefitAmount; + } + + public int calculateTotalDiscountAmount() { + int totalDiscountAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalDiscountAmount += allBenefit.get(benefitType); + } + if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) { + totalDiscountAmount -= Menu.CHAMPAGNE.getPrice(); + } + + return totalDiscountAmount; + } + + public boolean isReceivedGiftBenefit() { + return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice(); + } + + public List<BenefitType> checkWhichBenefitExist() { + List<BenefitType> existingBenefit = new ArrayList<>(); + BenefitType[] benefitTypes = BenefitType.values(); + + for (BenefitType benefitType : benefitTypes) { + if (allBenefit.get(benefitType) != 0) { + existingBenefit.add(benefitType); + } + } + + return existingBenefit; + } + + public String decideEventBadge() { + int totalBenefitAmount = calculateTotalBenefitAmount(); + + if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) { + return STAR_BADGE; + } + if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) { + return TREE_BADGE; + } + if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { + return SANTA_BADGE; + } + return NON_BADGE; + } + + public Map<BenefitType, Integer> getAllBenefit() { + return Collections.unmodifiableMap(allBenefit); + } +} \ No newline at end of file
Java
์ถ”๊ฐ€) NON_BADGE๋„ ๋ณ€์ˆ˜๋ช… ํ†ต์ผ์„ฑ์„ ์œ„ํ•ด NONE_BADGE๋กœ ์ž‘์„ฑํ•˜์…จ๋‹ค๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,39 @@ +package christmas.domain; + +import java.util.Arrays; +import java.util.List; + +public enum MenuType { + APPETIZER("์• ํ”ผํƒ€์ด์ €", Arrays.asList(Menu.MUSHROOM_SOUP, Menu.TAPAS, Menu.CAESAR_SALAD)), + MAIN("๋ฉ”์ธ", Arrays.asList(Menu.T_BONE_STAKE, Menu.BARBECUE_LIP, Menu.SEAFOOD_PASTA, Menu.CHRISTMAS_PASTA)), + DESSERT("๋””์ €ํŠธ", Arrays.asList(Menu.CHOCOLATE_CAKE, Menu.ICE_CREAM)), + BEVERAGE("์Œ๋ฃŒ", Arrays.asList(Menu.ZERO_COKE, Menu.RED_WINE, Menu.CHAMPAGNE)); + + private final String name; + private final List<Menu> menus; + + MenuType(String name, List<Menu> menus) { + this.name = name; + this.menus = menus; + } + + public static MenuType decideMenuType(Menu menu) { + MenuType[] menuTypes = values(); + + for (MenuType menuType : menuTypes) { + if (menuType.getMenus().contains(menu)) { + return menuType; + } + } + + return null; + } + + public String getName() { + return name; + } + + public List<Menu> getMenus() { + return menus; + } +} \ No newline at end of file
Java
๋งž์•„์š” ๋‹ค์‹œ ๋ฌผ์–ด๋ณด์…จ์–ด์„œ ๋Œ“๊ธ€์— ์ •๋ณด๋ฅผ ์–ป์€ ๋งํฌ ๋‚จ๊ฒจ๋“œ๋ ธ์—ˆ๋Š”๋ฐ์š” ๋ชป ๋ณด์‹  ๊ฒƒ ๊ฐ™์•„ ๋‹ค์‹œ ๋‚จ๊ฒจ๋“œ๋ ค์š”..!! https://techblog.woowahan.com/2527/ @Uniaut
@@ -0,0 +1,21 @@ +package christmas.domain; + +public enum OrderResultType { + ORDER_MENU("<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"), + TOTAL_ORDER_PRICE("<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>"), + GIFT_MENU("<์ฆ์ • ๋ฉ”๋‰ด>"), + BENEFIT_STATISTICS("<ํ˜œํƒ ๋‚ด์—ญ>"), + TOTAL_BENEFIT_AMOUNT("<์ดํ˜œํƒ ๊ธˆ์•ก>"), + ESTIMATED_PAYMENT("<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>"), + EVENT_BADGE("<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>"); + + private final String name; + + OrderResultType(String name) { + this.name = name; + } + + public String getName() { + return name; + } +}
Java
๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ธ๊ฒƒ ๊ฐ™๋„ค์š”!! ์ข‹์€ ์˜๊ฒฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,45 @@ +package christmas.domain; + +public class OrderedMenu { + public static final String ORDER_RE_READ_REQUEST_MESSAGE = "์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final int AMOUNT_MINIMUM_QUANTITY = 1; + + private final String menuName; + private final int quantity; + + public OrderedMenu(String menuName, int quantity) { + validateOrderNameExistInMenu(menuName); + validateOrderAmountQuantity(quantity); + + this.menuName = menuName; + this.quantity = quantity; + } + + private void validateOrderNameExistInMenu(String name) { + if (Menu.decideMenu(name) == Menu.NONE) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + private void validateOrderAmountQuantity(int amount) { + if (amount < AMOUNT_MINIMUM_QUANTITY) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + public boolean isBeverage() { + return MenuType.decideMenuType(Menu.decideMenu(menuName)) == MenuType.BEVERAGE; + } + + public int calculatePrice() { + return Menu.decideMenu(menuName).getPrice() * quantity; + } + + public String getMenuName() { + return menuName; + } + + public int getQuantity() { + return quantity; + } +} \ No newline at end of file
Java
๋งž๋„ค์š”..! ์ข‹์€ ์˜๊ฒฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!! Menu์—ด๊ฑฐ์—์„œ ๋ฉ”๋‰ด์— ์†ํ•˜์ง€ ์•Š๋Š” ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด "์—†์Œ"์„ ๋งŒ๋“ค์—ˆ์—ˆ๋Š”๋ฐ, ๋งŒ์•ฝ null๋กœ ์ฒ˜๋ฆฌํ–ˆ์—ˆ๋‹ค๋ฉด Menu์— ์†ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์•„์ง ๊ฒ€์ฆ์ด ์•ˆ๋˜์—ˆ๋˜ ๊ฑฐ๋‹ˆ๊นŒ ์ง€๊ธˆ์ฒ˜๋Ÿผ String์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์ด ๋งž์„๊นŒ์š”? ์ œ๊ฐ€ ์ดํ•ดํ•œ ๊ฒƒ์ด ๋งž๋Š”์ง€ ๊ถ๊ธˆํ•ด์š” @Uniaut
@@ -0,0 +1,58 @@ +package christmas.domain; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +public class Date { + public static final String DATE_RE_READ_REQUEST_MESSAGE = "์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final LocalDate CHRISTMAS_DAY = LocalDate.of(2023, 12, 25); + private static final int YEAR = 2023; + private static final int MONTH = 12; + private static final int DATE_MIN_NUMBER = 1; + private static final int DATE_MAX_NUMBER = 31; + + private final int dayNumber; + + public Date(int dayNumber) { + validateRange(dayNumber); + this.dayNumber = dayNumber; + } + + private void validateRange(int dayNumber) { + if ((dayNumber < DATE_MIN_NUMBER) || (dayNumber > DATE_MAX_NUMBER)) { + throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE); + } + } + + public boolean isDDayDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + + return localDate.isBefore(CHRISTMAS_DAY.plusDays(1)); + } + + public boolean isWeekdayDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + DayOfWeek day = localDate.getDayOfWeek(); + + return (day.equals(DayOfWeek.MONDAY) || day.equals(DayOfWeek.TUESDAY) || day.equals(DayOfWeek.WEDNESDAY) + || day.equals(DayOfWeek.THURSDAY) || day.equals(DayOfWeek.SUNDAY)); + } + + public boolean isWeekendDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + DayOfWeek day = localDate.getDayOfWeek(); + + return (day.equals(DayOfWeek.FRIDAY) || day.equals(DayOfWeek.SATURDAY)); + } + + public boolean isSpecialDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + DayOfWeek day = localDate.getDayOfWeek(); + + return (day.equals(DayOfWeek.SUNDAY) || localDate.equals(CHRISTMAS_DAY)); + } + + public int getDayNumber() { + return dayNumber; + } +} \ No newline at end of file
Java
@JaeHongDev ์ €๋„ ์ƒˆ๋กญ๊ฒŒ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค..!
@@ -0,0 +1,85 @@ +package christmas.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class EventResult { + private static final String NON_BADGE = "์—†์Œ"; + private static final String STAR_BADGE = "๋ณ„"; + private static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + private static final String SANTA_BADGE = "์‚ฐํƒ€"; + private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000; + private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000; + private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000; + + private final Map<BenefitType, Integer> allBenefit; + + public EventResult(Map<BenefitType, Integer> allBenefit) { + this.allBenefit = allBenefit; + } + + public int calculateTotalBenefitAmount() { + int totalBenefitAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalBenefitAmount += allBenefit.get(benefitType); + } + + return totalBenefitAmount; + } + + public int calculateTotalDiscountAmount() { + int totalDiscountAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalDiscountAmount += allBenefit.get(benefitType); + } + if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) { + totalDiscountAmount -= Menu.CHAMPAGNE.getPrice(); + } + + return totalDiscountAmount; + } + + public boolean isReceivedGiftBenefit() { + return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice(); + } + + public List<BenefitType> checkWhichBenefitExist() { + List<BenefitType> existingBenefit = new ArrayList<>(); + BenefitType[] benefitTypes = BenefitType.values(); + + for (BenefitType benefitType : benefitTypes) { + if (allBenefit.get(benefitType) != 0) { + existingBenefit.add(benefitType); + } + } + + return existingBenefit; + } + + public String decideEventBadge() { + int totalBenefitAmount = calculateTotalBenefitAmount(); + + if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) { + return STAR_BADGE; + } + if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) { + return TREE_BADGE; + } + if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { + return SANTA_BADGE; + } + return NON_BADGE; + } + + public Map<BenefitType, Integer> getAllBenefit() { + return Collections.unmodifiableMap(allBenefit); + } +} \ No newline at end of file
Java
๋งž์•„์š”!! ์ €๋„ ๊ฐœ์ธ์ ์œผ๋กœ ์ด ๋ฉ”์„œ๋“œ์˜ ๋กœ์ง์„ ๋งŒ๋“ค์—ˆ๋˜ ๋ฐฉ์‹์ด ์•„์‰ฌ์› ๋Š”๋ฐ์š” ,, ๋ง์”€ํ•ด ์ฃผ์‹  ๋ฐฉ๋ฒ•์ด ํ›จ์”ฌ ๊น”๋”ํ•œ ๊ฒƒ ๊ฐ™์•„์š” ๋Œ€์‹  if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { return SANTA_BADGE; } if (totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) { return TREE_BADGE; } if (totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) { return STAR_BADGE; } return NON_BADGE; ์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฐํƒ€ ๋ฐฐ์ง€๊ฐ€ ์ œ์ผ ์œ„๋กœ ๊ฐ€์•ผ 30000์›์ผ ๋•Œ ๋ณ„ ๋ฐฐ์ง€๋ฅผ ๋ฆฌํ„ดํ•˜์ง€ ์•Š๊ณ  ์‚ฐํƒ€ ๋ฐฐ์ง€๋ฅผ ๋ฆฌํ„ด ํ•  ๊ฒƒ ๊ฐ™์•„์š” ์ œ๊ฐ€ ์ดํ•ดํ•œ๊ฒŒ ๋งž๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค @Uniaut Maximum์ด๋ผ๋Š” ํ‘œํ˜„์€ ํ•ด๋‹น ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ํ‘œํ˜„์ด๋ž€ ๊ฒƒ์„ ๋†“์ณค๋˜ ๊ฒƒ ๊ฐ™๋„ค์š”!! ๋‚ ์นด๋กœ์šด ์ง€์  ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,85 @@ +package christmas.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class EventResult { + private static final String NON_BADGE = "์—†์Œ"; + private static final String STAR_BADGE = "๋ณ„"; + private static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + private static final String SANTA_BADGE = "์‚ฐํƒ€"; + private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000; + private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000; + private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000; + + private final Map<BenefitType, Integer> allBenefit; + + public EventResult(Map<BenefitType, Integer> allBenefit) { + this.allBenefit = allBenefit; + } + + public int calculateTotalBenefitAmount() { + int totalBenefitAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalBenefitAmount += allBenefit.get(benefitType); + } + + return totalBenefitAmount; + } + + public int calculateTotalDiscountAmount() { + int totalDiscountAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalDiscountAmount += allBenefit.get(benefitType); + } + if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) { + totalDiscountAmount -= Menu.CHAMPAGNE.getPrice(); + } + + return totalDiscountAmount; + } + + public boolean isReceivedGiftBenefit() { + return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice(); + } + + public List<BenefitType> checkWhichBenefitExist() { + List<BenefitType> existingBenefit = new ArrayList<>(); + BenefitType[] benefitTypes = BenefitType.values(); + + for (BenefitType benefitType : benefitTypes) { + if (allBenefit.get(benefitType) != 0) { + existingBenefit.add(benefitType); + } + } + + return existingBenefit; + } + + public String decideEventBadge() { + int totalBenefitAmount = calculateTotalBenefitAmount(); + + if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) { + return STAR_BADGE; + } + if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) { + return TREE_BADGE; + } + if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { + return SANTA_BADGE; + } + return NON_BADGE; + } + + public Map<BenefitType, Integer> getAllBenefit() { + return Collections.unmodifiableMap(allBenefit); + } +} \ No newline at end of file
Java
if๋ฌธ ์ „๊นŒ์ง€๊ฐ€ calculateTotalBenefitAmount๋ž‘ ๊ฐ™์•„์„œ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ ์„ ํ•„์š”์—†์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,85 @@ +package christmas.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class EventResult { + private static final String NON_BADGE = "์—†์Œ"; + private static final String STAR_BADGE = "๋ณ„"; + private static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + private static final String SANTA_BADGE = "์‚ฐํƒ€"; + private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000; + private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000; + private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000; + + private final Map<BenefitType, Integer> allBenefit; + + public EventResult(Map<BenefitType, Integer> allBenefit) { + this.allBenefit = allBenefit; + } + + public int calculateTotalBenefitAmount() { + int totalBenefitAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalBenefitAmount += allBenefit.get(benefitType); + } + + return totalBenefitAmount; + } + + public int calculateTotalDiscountAmount() { + int totalDiscountAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalDiscountAmount += allBenefit.get(benefitType); + } + if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) { + totalDiscountAmount -= Menu.CHAMPAGNE.getPrice(); + } + + return totalDiscountAmount; + } + + public boolean isReceivedGiftBenefit() { + return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice(); + } + + public List<BenefitType> checkWhichBenefitExist() { + List<BenefitType> existingBenefit = new ArrayList<>(); + BenefitType[] benefitTypes = BenefitType.values(); + + for (BenefitType benefitType : benefitTypes) { + if (allBenefit.get(benefitType) != 0) { + existingBenefit.add(benefitType); + } + } + + return existingBenefit; + } + + public String decideEventBadge() { + int totalBenefitAmount = calculateTotalBenefitAmount(); + + if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) { + return STAR_BADGE; + } + if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) { + return TREE_BADGE; + } + if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { + return SANTA_BADGE; + } + return NON_BADGE; + } + + public Map<BenefitType, Integer> getAllBenefit() { + return Collections.unmodifiableMap(allBenefit); + } +} \ No newline at end of file
Java
์ด if๋ฌธ์˜ ์กฐ๊ฑด๋„ isReceivedGiftBenefit ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜๊ฒ ๋„ค์š”!
@@ -0,0 +1,76 @@ +package christmas.domain; + +import static christmas.domain.OrderedMenu.ORDER_RE_READ_REQUEST_MESSAGE; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class Order { + private static final int MAXIMUM_MENU_QUANTITY = 20; + + private final List<OrderedMenu> order; + + public Order(List<OrderedMenu> order) { + validateOrderedMenuNamesNotAllBeverage(order); + validateOrderedMenuNamesNotDuplicate(order); + validateTotalOrderedMenuQuantity(order); + + this.order = order; + } + + private void validateOrderedMenuNamesNotAllBeverage(List<OrderedMenu> order) { + List<Boolean> beverageChecker = new ArrayList<>(); + + for (OrderedMenu orderedMenu : order) { + if (orderedMenu.isBeverage()) { + beverageChecker.add(true); + } + if (!orderedMenu.isBeverage()) { + beverageChecker.add(false); + } + } + + if (!beverageChecker.contains(false)) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + private void validateOrderedMenuNamesNotDuplicate(List<OrderedMenu> order) { + Set<String> duplicateChecker = new HashSet<>(); + + for (OrderedMenu orderedMenu : order) { + duplicateChecker.add(orderedMenu.getMenuName()); + } + if (duplicateChecker.size() != order.size()) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + private void validateTotalOrderedMenuQuantity(List<OrderedMenu> order) { + int totalQuantity = 0; + + for (OrderedMenu orderedMenu : order) { + totalQuantity += orderedMenu.getQuantity(); + } + if (totalQuantity > MAXIMUM_MENU_QUANTITY) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + public int calculateTotalOrderPrice() { + int totalOrderPrice = 0; + + for (OrderedMenu orderedMenu : order) { + totalOrderPrice += orderedMenu.calculatePrice(); + } + + return totalOrderPrice; + } + + public List<OrderedMenu> getOrder() { + return Collections.unmodifiableList(order); + } +} \ No newline at end of file
Java
๋ฉ”์„œ๋“œ๋ช…์ด ๊ธธ์–ด๋„ ํ™•์‹คํžˆ ์ด๋ฆ„๋งŒ ๋ณด๊ณ ๋„ ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š”์ง€ ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,21 @@ +package nextstep.subway; + +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class ControllerExceptionHandler { + + @ExceptionHandler(DataIntegrityViolationException.class) + public ResponseEntity handleIllegalArgsException(DataIntegrityViolationException e) { + return ResponseEntity.badRequest().build(); + } + + @ExceptionHandler(IllegalArgumentException.class) + public ResponseEntity handleIllegalArgumentException(IllegalArgumentException illegalArgumentException){ + String message = illegalArgumentException.getMessage(); + return ResponseEntity.badRequest().body(message); + } +}
Java
@RestControllerAdvice ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ์—๋Ÿฌ๋ฅผ ํ•œ๊ตฐ๋ฐ์„œ ์ฒ˜๋ฆฌํ•ด์ค„ ์ˆ˜ ์žˆ๊ฒŒํ•ด์ค€ ๋ถ€๋ถ„ ์ข‹์Šต๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,21 @@ +package nextstep.subway; + +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class ControllerExceptionHandler { + + @ExceptionHandler(DataIntegrityViolationException.class) + public ResponseEntity handleIllegalArgsException(DataIntegrityViolationException e) { + return ResponseEntity.badRequest().build(); + } + + @ExceptionHandler(IllegalArgumentException.class) + public ResponseEntity handleIllegalArgumentException(IllegalArgumentException illegalArgumentException){ + String message = illegalArgumentException.getMessage(); + return ResponseEntity.badRequest().body(message); + } +}
Java
์ด ๋ถ€๋ถ„์€ ์กฐ๊ธˆ ๊ถ๊ธˆํ•˜์—ฌ ์—ฌ์ญค๋ด…๋‹ˆ๋‹ค! DataIntegrityViolationException ๋Š” ํ˜„์žฌ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์—์„œ ์–ธ์ œ, ์–ด๋–ค ์ผ€์ด์Šค์—์„œ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋‚˜์š”...?
@@ -0,0 +1,38 @@ +package nextstep.subway.line.domain; + +import java.util.ArrayList; +import java.util.List; +import nextstep.subway.station.domain.Station; +import org.jgrapht.GraphPath; +import org.jgrapht.alg.shortestpath.DijkstraShortestPath; +import org.jgrapht.graph.DefaultWeightedEdge; +import org.jgrapht.graph.WeightedMultigraph; + +public class Lines { + + List<Line> lines = new ArrayList<>(); + + public Lines(List<Line> lines) { + this.lines = lines; + } + + public GraphPath minPath(Station sourceStation, Station targetStation) { + + validatePath(sourceStation, targetStation); + WeightedMultigraph<Station, DefaultWeightedEdge> graph = new WeightedMultigraph(DefaultWeightedEdge.class); + + for (Line line : lines) { + line.addPathInfo(graph); + } + + DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(graph); + + return dijkstraShortestPath.getPath(sourceStation, targetStation); + } + + private void validatePath(Station sourceStation, Station targetStation){ + if(sourceStation.equals(targetStation)){ + throw new IllegalArgumentException("๊ฒฝ๋กœ ์‹œ์ž‘์—ญ๊ณผ ๋„์ฐฉ์—ญ์ด ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค."); + } + } +}
Java
์ ‘๊ทผ์ œํ•œ์ž๊ฐ€ ์—†๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,38 @@ +package nextstep.subway.line.domain; + +import java.util.ArrayList; +import java.util.List; +import nextstep.subway.station.domain.Station; +import org.jgrapht.GraphPath; +import org.jgrapht.alg.shortestpath.DijkstraShortestPath; +import org.jgrapht.graph.DefaultWeightedEdge; +import org.jgrapht.graph.WeightedMultigraph; + +public class Lines { + + List<Line> lines = new ArrayList<>(); + + public Lines(List<Line> lines) { + this.lines = lines; + } + + public GraphPath minPath(Station sourceStation, Station targetStation) { + + validatePath(sourceStation, targetStation); + WeightedMultigraph<Station, DefaultWeightedEdge> graph = new WeightedMultigraph(DefaultWeightedEdge.class); + + for (Line line : lines) { + line.addPathInfo(graph); + } + + DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(graph); + + return dijkstraShortestPath.getPath(sourceStation, targetStation); + } + + private void validatePath(Station sourceStation, Station targetStation){ + if(sourceStation.equals(targetStation)){ + throw new IllegalArgumentException("๊ฒฝ๋กœ ์‹œ์ž‘์—ญ๊ณผ ๋„์ฐฉ์—ญ์ด ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค."); + } + } +}
Java
๋™์‚ฌ๊ตฌ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ํ‘œํ˜„ํ•ด๋ณด๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”...? ๊ทธ๋ฆฌ๊ณ  ํ˜น์‹œ ์ œ๋„ˆ๋ฆญ์„ ์•ˆ์“ฐ์‹œ๋Š” ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”...?
@@ -6,9 +6,11 @@ import javax.persistence.CascadeType; import javax.persistence.Embeddable; import javax.persistence.OneToMany; -import nextstep.subway.SectionsNotAddedException; -import nextstep.subway.SectionsNotRemovedException; +import nextstep.subway.line.exception.SectionsNotAddedException; +import nextstep.subway.line.exception.SectionsNotRemovedException; import nextstep.subway.station.domain.Station; +import org.jgrapht.graph.DefaultWeightedEdge; +import org.jgrapht.graph.WeightedMultigraph; @Embeddable public class Sections { @@ -34,41 +36,36 @@ public void requestAddSection(Section section) { sections.add(section); return; } - checkExistsStation(section, section.getUpStation(), section.getDownStation()); + checkExistsStation(section); } - private void checkExistsStation(Section section, Station upStation, Station downStation) { - boolean upMatchesLineUp = stationExistsInUp(upStation); - boolean downMatchsLineDown = stationExistsInDown(downStation); - boolean matchesAtFront = stationExistsInUp(downStation); - boolean matchesAtBack = stationExistsInDown(upStation); - if (!upMatchesLineUp && !matchesAtFront && !matchesAtBack && !downMatchsLineDown) { + private void checkExistsStation(Section section) { + boolean upMatchesLineUp = stationExistsInUp(section.getUpStation()); + boolean downMatchesLineDown = stationExistsInDown(section.getDownStation()); + boolean matchesAtEnd = stationExistsAtEnd(section); + + if (!upMatchesLineUp && !downMatchesLineDown && !matchesAtEnd) { throw new SectionsNotAddedException("๊ตฌ๊ฐ„ ์ค‘ ์–ด๋– ํ•œ ์—ญ๋„ ํ˜„์žฌ ๊ตฌ๊ฐ„์— ์—†์Šต๋‹ˆ๋‹ค."); } - if (upMatchesLineUp && downMatchsLineDown) { + if (upMatchesLineUp && downMatchesLineDown) { throw new SectionsNotAddedException("์ด๋ฏธ ๋“ฑ๋ก๋œ ๊ตฌ๊ฐ„ ์ž…๋‹ˆ๋‹ค."); } - addSectionToMatchStation(section, upStation, downStation, upMatchesLineUp, downMatchsLineDown, matchesAtFront, - matchesAtBack); - + addSectionToMatchStation(section, upMatchesLineUp, downMatchesLineDown); } - private void addSectionToMatchStation(Section section, Station upStation, Station downStation, - boolean upMatchesLineUp, boolean downMatchsLineDown, boolean matchesAtFront, - boolean matchesAtBack) { + private void addSectionToMatchStation(Section section, boolean upMatchesLineUp, boolean downMatchsLineDown) { + Station upStation = section.getUpStation(); + Station downStation = section.getDownStation(); if (upMatchesLineUp) { - addUpStationExists(section, upStation, downStation, section.getDistance()); - return; + updateUpStationMatch(section, upStation, downStation, section.getDistance()); } if (downMatchsLineDown) { - addDownStationExists(section, upStation, downStation, section.getDistance()); - return; + updateDownStatioMatch(section, upStation, downStation, section.getDistance()); } - if (matchesAtFront || matchesAtBack) { - sections.add(section); - } + //์–‘์ชฝ ๋์— ๋”ํ•ด์งˆ ๊ฒฝ์šฐ + sections.add(section); } private boolean stationExistsInUp(Station station) { @@ -79,33 +76,40 @@ private boolean stationExistsInDown(Station station) { return sections.stream().anyMatch(section -> section.getDownStation().equals(station)); } + private boolean stationExistsAtEnd(Section section) { + return sections.stream().anyMatch( + sectionIterate -> sectionIterate.getDownStation().equals(section.getUpStation()) + || sectionIterate.getUpStation().equals(section.getDownStation())); + } - private void addUpStationExists(Section section, Station upStation, Station downStation, int distance) { + private void updateUpStationMatch(Section section, Station upStation, Station downStation, int distance) { sections.stream().filter(sectionIterate -> upStation.equals(sectionIterate.getUpStation())).findFirst() .ifPresent(sectionIterate -> sectionIterate.updateUpStation(downStation, distance)); - sections.add(section); } - private void addDownStationExists(Section section, Station upStation, Station downStation, int distance) { + private void updateDownStatioMatch(Section section, Station upStation, Station downStation, int distance) { sections.stream().filter(sectionIterate -> downStation.equals(sectionIterate.getDownStation())).findFirst() .ifPresent(it -> it.updateDownStation(upStation, distance)); - sections.add(section); } + public void removeLineStation(Station station, Line line) { if (sections.size() <= ONE) { throw new SectionsNotRemovedException("ํ˜„์žฌ ๊ตฌ๊ฐ„์ด ํ•˜๋‚˜๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค."); } - Optional<Section> upLineStation = sections.stream().filter(it -> it.getUpStation() == station).findFirst(); - Optional<Section> downLineStation = sections.stream().filter(it -> it.getDownStation() == station).findFirst(); + Optional<Section> upLineStation = sections.stream().filter(section -> section.getUpStation().equals(station)) + .findFirst(); + Optional<Section> downLineStation = sections.stream() + .filter(section -> section.getDownStation().equals(station)).findFirst(); connectFrontBackSection(line, upLineStation, downLineStation); - upLineStation.ifPresent(it -> sections.remove(it)); - downLineStation.ifPresent(it -> sections.remove(it)); + upLineStation.ifPresent(section -> sections.remove(section)); + downLineStation.ifPresent(section -> sections.remove(section)); } - private void connectFrontBackSection(Line line, Optional<Section> upLineStation, Optional<Section> downLineStation) { + private void connectFrontBackSection(Line line, Optional<Section> upLineStation, + Optional<Section> downLineStation) { if (upLineStation.isPresent() && downLineStation.isPresent()) { Station newUpStation = downLineStation.get().getUpStation(); Station newDownStation = upLineStation.get().getDownStation(); @@ -114,4 +118,11 @@ private void connectFrontBackSection(Line line, Optional<Section> upLineStation, sections.add(new Section(line, newUpStation, newDownStation, newDistance)); } } + + public void addGraphEdge(WeightedMultigraph<Station, DefaultWeightedEdge> graph) { + for (Section section : sections) { + section.addGraphEdge(graph); + } + } + } \ No newline at end of file
Java
์ด๋ถ€๋ถ„๋„ Section์— ๋ฉ”์‹œ์ง€๋ฅผ ๋˜์ ธ์„œ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -6,9 +6,11 @@ import javax.persistence.CascadeType; import javax.persistence.Embeddable; import javax.persistence.OneToMany; -import nextstep.subway.SectionsNotAddedException; -import nextstep.subway.SectionsNotRemovedException; +import nextstep.subway.line.exception.SectionsNotAddedException; +import nextstep.subway.line.exception.SectionsNotRemovedException; import nextstep.subway.station.domain.Station; +import org.jgrapht.graph.DefaultWeightedEdge; +import org.jgrapht.graph.WeightedMultigraph; @Embeddable public class Sections { @@ -34,41 +36,36 @@ public void requestAddSection(Section section) { sections.add(section); return; } - checkExistsStation(section, section.getUpStation(), section.getDownStation()); + checkExistsStation(section); } - private void checkExistsStation(Section section, Station upStation, Station downStation) { - boolean upMatchesLineUp = stationExistsInUp(upStation); - boolean downMatchsLineDown = stationExistsInDown(downStation); - boolean matchesAtFront = stationExistsInUp(downStation); - boolean matchesAtBack = stationExistsInDown(upStation); - if (!upMatchesLineUp && !matchesAtFront && !matchesAtBack && !downMatchsLineDown) { + private void checkExistsStation(Section section) { + boolean upMatchesLineUp = stationExistsInUp(section.getUpStation()); + boolean downMatchesLineDown = stationExistsInDown(section.getDownStation()); + boolean matchesAtEnd = stationExistsAtEnd(section); + + if (!upMatchesLineUp && !downMatchesLineDown && !matchesAtEnd) { throw new SectionsNotAddedException("๊ตฌ๊ฐ„ ์ค‘ ์–ด๋– ํ•œ ์—ญ๋„ ํ˜„์žฌ ๊ตฌ๊ฐ„์— ์—†์Šต๋‹ˆ๋‹ค."); } - if (upMatchesLineUp && downMatchsLineDown) { + if (upMatchesLineUp && downMatchesLineDown) { throw new SectionsNotAddedException("์ด๋ฏธ ๋“ฑ๋ก๋œ ๊ตฌ๊ฐ„ ์ž…๋‹ˆ๋‹ค."); } - addSectionToMatchStation(section, upStation, downStation, upMatchesLineUp, downMatchsLineDown, matchesAtFront, - matchesAtBack); - + addSectionToMatchStation(section, upMatchesLineUp, downMatchesLineDown); } - private void addSectionToMatchStation(Section section, Station upStation, Station downStation, - boolean upMatchesLineUp, boolean downMatchsLineDown, boolean matchesAtFront, - boolean matchesAtBack) { + private void addSectionToMatchStation(Section section, boolean upMatchesLineUp, boolean downMatchsLineDown) { + Station upStation = section.getUpStation(); + Station downStation = section.getDownStation(); if (upMatchesLineUp) { - addUpStationExists(section, upStation, downStation, section.getDistance()); - return; + updateUpStationMatch(section, upStation, downStation, section.getDistance()); } if (downMatchsLineDown) { - addDownStationExists(section, upStation, downStation, section.getDistance()); - return; + updateDownStatioMatch(section, upStation, downStation, section.getDistance()); } - if (matchesAtFront || matchesAtBack) { - sections.add(section); - } + //์–‘์ชฝ ๋์— ๋”ํ•ด์งˆ ๊ฒฝ์šฐ + sections.add(section); } private boolean stationExistsInUp(Station station) { @@ -79,33 +76,40 @@ private boolean stationExistsInDown(Station station) { return sections.stream().anyMatch(section -> section.getDownStation().equals(station)); } + private boolean stationExistsAtEnd(Section section) { + return sections.stream().anyMatch( + sectionIterate -> sectionIterate.getDownStation().equals(section.getUpStation()) + || sectionIterate.getUpStation().equals(section.getDownStation())); + } - private void addUpStationExists(Section section, Station upStation, Station downStation, int distance) { + private void updateUpStationMatch(Section section, Station upStation, Station downStation, int distance) { sections.stream().filter(sectionIterate -> upStation.equals(sectionIterate.getUpStation())).findFirst() .ifPresent(sectionIterate -> sectionIterate.updateUpStation(downStation, distance)); - sections.add(section); } - private void addDownStationExists(Section section, Station upStation, Station downStation, int distance) { + private void updateDownStatioMatch(Section section, Station upStation, Station downStation, int distance) { sections.stream().filter(sectionIterate -> downStation.equals(sectionIterate.getDownStation())).findFirst() .ifPresent(it -> it.updateDownStation(upStation, distance)); - sections.add(section); } + public void removeLineStation(Station station, Line line) { if (sections.size() <= ONE) { throw new SectionsNotRemovedException("ํ˜„์žฌ ๊ตฌ๊ฐ„์ด ํ•˜๋‚˜๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค."); } - Optional<Section> upLineStation = sections.stream().filter(it -> it.getUpStation() == station).findFirst(); - Optional<Section> downLineStation = sections.stream().filter(it -> it.getDownStation() == station).findFirst(); + Optional<Section> upLineStation = sections.stream().filter(section -> section.getUpStation().equals(station)) + .findFirst(); + Optional<Section> downLineStation = sections.stream() + .filter(section -> section.getDownStation().equals(station)).findFirst(); connectFrontBackSection(line, upLineStation, downLineStation); - upLineStation.ifPresent(it -> sections.remove(it)); - downLineStation.ifPresent(it -> sections.remove(it)); + upLineStation.ifPresent(section -> sections.remove(section)); + downLineStation.ifPresent(section -> sections.remove(section)); } - private void connectFrontBackSection(Line line, Optional<Section> upLineStation, Optional<Section> downLineStation) { + private void connectFrontBackSection(Line line, Optional<Section> upLineStation, + Optional<Section> downLineStation) { if (upLineStation.isPresent() && downLineStation.isPresent()) { Station newUpStation = downLineStation.get().getUpStation(); Station newDownStation = upLineStation.get().getDownStation(); @@ -114,4 +118,11 @@ private void connectFrontBackSection(Line line, Optional<Section> upLineStation, sections.add(new Section(line, newUpStation, newDownStation, newDistance)); } } + + public void addGraphEdge(WeightedMultigraph<Station, DefaultWeightedEdge> graph) { + for (Section section : sections) { + section.addGraphEdge(graph); + } + } + } \ No newline at end of file
Java
์—ฌ๊ธฐ๊นŒ์ง€ ํ”ผ๋“œ๋ฐฑ ๋ฐ˜์˜ ์ข‹๋„ค์š” ใ…Žใ…Ž ๐Ÿ‘
@@ -0,0 +1,38 @@ +package nextstep.subway.path.application; + +import nextstep.subway.station.exception.StationNotFoundException; +import nextstep.subway.line.domain.LineRepository; +import nextstep.subway.line.domain.Lines; +import nextstep.subway.path.dto.PathResponse; +import nextstep.subway.station.domain.Station; +import nextstep.subway.station.domain.StationRepository; +import org.jgrapht.GraphPath; +import org.springframework.stereotype.Service; + +@Service +public class PathService { + + private final StationRepository stationRepository; + private final LineRepository lineRepository; + + public PathService(StationRepository stationRepository, LineRepository lineRepository) { + this.stationRepository = stationRepository; + this.lineRepository = lineRepository; + } + + public PathResponse minPath(Long sourceStationId, Long targetStationId) { + System.out.println("PathService.minPath"); + + Station sourceStation = stationRepository.findById(sourceStationId) + .orElseThrow(() -> new StationNotFoundException("๊ฒฝ๋กœ ์‹œ์ž‘์—ญ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + Station targetStation = stationRepository.findById(targetStationId) + .orElseThrow(() -> new StationNotFoundException("๊ฒฝ๋กœ ๋„์ฐฉ์—ญ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + + Lines lines = new Lines(lineRepository.findAll()); + GraphPath graphPath = lines.minPath(sourceStation, targetStation); + + PathResponse pathResponse = new PathResponse(graphPath.getVertexList(), (int) graphPath.getWeight()); + + return pathResponse; + } +}
Java
์—ฌ๊ธฐ์— ํ˜น์‹œ System ํ•จ์ˆ˜๋ฅผ ์“ฐ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”...? Sysytem ํ•จ์ˆ˜๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ๋งŽ์ด ๋จน์–ด์„œ ์ตœ๋Œ€ํ•œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,38 @@ +package nextstep.subway.path.application; + +import nextstep.subway.station.exception.StationNotFoundException; +import nextstep.subway.line.domain.LineRepository; +import nextstep.subway.line.domain.Lines; +import nextstep.subway.path.dto.PathResponse; +import nextstep.subway.station.domain.Station; +import nextstep.subway.station.domain.StationRepository; +import org.jgrapht.GraphPath; +import org.springframework.stereotype.Service; + +@Service +public class PathService { + + private final StationRepository stationRepository; + private final LineRepository lineRepository; + + public PathService(StationRepository stationRepository, LineRepository lineRepository) { + this.stationRepository = stationRepository; + this.lineRepository = lineRepository; + } + + public PathResponse minPath(Long sourceStationId, Long targetStationId) { + System.out.println("PathService.minPath"); + + Station sourceStation = stationRepository.findById(sourceStationId) + .orElseThrow(() -> new StationNotFoundException("๊ฒฝ๋กœ ์‹œ์ž‘์—ญ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + Station targetStation = stationRepository.findById(targetStationId) + .orElseThrow(() -> new StationNotFoundException("๊ฒฝ๋กœ ๋„์ฐฉ์—ญ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + + Lines lines = new Lines(lineRepository.findAll()); + GraphPath graphPath = lines.minPath(sourceStation, targetStation); + + PathResponse pathResponse = new PathResponse(graphPath.getVertexList(), (int) graphPath.getWeight()); + + return pathResponse; + } +}
Java
Repository ์กฐํšŒํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๋Š”๋ฐ readOnly ์˜ต์…˜์„ ์ฃผ๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”....?
@@ -1,9 +1,111 @@ package nextstep.subway.path; +import static nextstep.subway.line.acceptance.LineAcceptanceTest.์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ; +import static org.assertj.core.api.Assertions.assertThat; + +import io.restassured.RestAssured; +import io.restassured.response.ExtractableResponse; +import io.restassured.response.Response; +import java.util.HashMap; +import java.util.Map; import nextstep.subway.AcceptanceTest; +import nextstep.subway.line.acceptance.LineSectionAcceptanceTest; +import nextstep.subway.line.dto.LineRequest; +import nextstep.subway.line.dto.LineResponse; +import nextstep.subway.station.StationAcceptanceTest; +import nextstep.subway.station.dto.StationResponse; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; @DisplayName("์ง€ํ•˜์ฒ  ๊ฒฝ๋กœ ์กฐํšŒ") public class PathAcceptanceTest extends AcceptanceTest { + + private LineResponse ์‹ ๋ถ„๋‹น์„ ; + private LineResponse ์ดํ˜ธ์„ ; + private LineResponse ์‚ผํ˜ธ์„ ; + private StationResponse ๊ฐ•๋‚จ์—ญ; + private StationResponse ์–‘์žฌ์—ญ; + private StationResponse ๊ต๋Œ€์—ญ; + private StationResponse ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ; + + /** + * ๊ต๋Œ€์—ญ --- *2ํ˜ธ์„ * --- ๊ฐ•๋‚จ์—ญ + * | | + * *3ํ˜ธ์„ * *์‹ ๋ถ„๋‹น์„ * + * | | + * ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ --- *3ํ˜ธ์„ * --- ์–‘์žฌ + */ + @BeforeEach + public void setUp() { + super.setUp(); + + ๊ฐ•๋‚จ์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("๊ฐ•๋‚จ์—ญ").as(StationResponse.class); + ์–‘์žฌ์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("์–‘์žฌ์—ญ").as(StationResponse.class); + ๊ต๋Œ€์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("๊ต๋Œ€์—ญ").as(StationResponse.class); + ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ").as(StationResponse.class); + + ์‹ ๋ถ„๋‹น์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(new LineRequest("์‹ ๋ถ„๋‹น์„ ", "bg-red-600", ๊ฐ•๋‚จ์—ญ.getId(), ์–‘์žฌ์—ญ.getId(), 10)).as( + LineResponse.class); + ์ดํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(new LineRequest("์ดํ˜ธ์„ ", "bg-red-600", ๊ต๋Œ€์—ญ.getId(), ๊ฐ•๋‚จ์—ญ.getId(), 10)).as(LineResponse.class); + ์‚ผํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(new LineRequest("์‚ผํ˜ธ์„ ", "bg-red-600", ๊ต๋Œ€์—ญ.getId(), ์–‘์žฌ์—ญ.getId(), 5)).as(LineResponse.class); + + ์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(์‚ผํ˜ธ์„ , ๊ต๋Œ€์—ญ, ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, 3); + } + + private ExtractableResponse<Response> ์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(LineResponse lineResponse, StationResponse upStation, + StationResponse downStation, + int distance) { + return LineSectionAcceptanceTest.์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก_์š”์ฒญ(lineResponse, + upStation, downStation, distance); + } + + @Test + @DisplayName("๊ต๋Œ€์—ญ-์–‘์žฌ์—ญ ์ตœ๋‹จ๊ฒฝ๋กœ ์กฐํšŒํ•˜๊ธฐ (๊ต๋Œ€์—ญ-๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ-์–‘์žฌ์—ญ)") + public void findMinPath() { + ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ = ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(๊ต๋Œ€์—ญ, ์–‘์žฌ์—ญ); + ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_๊ฒ€์ฆ(์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ, 5, "๊ต๋Œ€์—ญ","๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ", "์–‘์žฌ์—ญ"); + } + + + @Test + @DisplayName("๊ฒฝ๋กœ ์‹œ์ž‘์—ญ ๋„์ฐฉ์—ญ ์ผ์น˜ ์กฐํšŒ ์—๋Ÿฌ ํ™•์ธ") + public void findMinPathSourceTargetSame() { + ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ = ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(๊ต๋Œ€์—ญ, ๊ต๋Œ€์—ญ); + ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_์š”์ฒญ๊ฒฐ๊ณผ_์—๋Ÿฌ(์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ); + } + + @Test + @DisplayName("๊ฒฝ๋กœ ์กฐํšŒ์‹œ ๋ฏธ์กด์žฌ ์—ญ ์—๋Ÿฌ ํ™•์ธ") + public void findMinPathNotExsistStation() { + ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ = ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(new StationResponse(), new StationResponse()); + ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_์š”์ฒญ๊ฒฐ๊ณผ_์—๋Ÿฌ(์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ); + } + + private void ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_์š”์ฒญ๊ฒฐ๊ณผ_์—๋Ÿฌ(ExtractableResponse<Response> response) { + assertThat(response.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value()); + } + + private void ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_๊ฒ€์ฆ(ExtractableResponse<Response> response,int distance, String... stationNames) { + assertThat(response.jsonPath().getInt("distance")).isEqualTo(distance); + assertThat(response.jsonPath().getList("stations.name")).containsExactly(stationNames); + } + + private ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(StationResponse source, StationResponse target) { + Map<String, Long> param = new HashMap<>(); + param.put("source", source.getId()); + param.put("target", target.getId()); + + return RestAssured + .given().log().all() + .queryParams(param) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .when().get("/paths") + .then().log().all() + .extract(); + } + }
Java
์ธ์ˆ˜ํ…Œ์ŠคํŠธ ์ข‹์Šต๋‹ˆ๋‹ค ๐Ÿ‘ ๋‹ค๋งŒ ์ด๋ฒˆ์— ์ƒˆ๋กœ๋งŒ๋“œ์‹  Lines์— ๋Œ€ํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ, PathService์— ๋Œ€ํ•œ ๋‹จ์œ„ํ…Œ์ŠคํŠธ๋„ ์ถ”๊ฐ€๋˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! Lines์˜ ๊ฒฝ์šฐ ์‹ค์ œ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ํ•ด๋‹น ์‹ค์ œ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•˜๊ณ , PathService์˜ ๊ฒฝ์šฐ๋Š” ์™ธ๋ถ€ ์˜์กด์„ฑ์„ Mocking ํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์€ ๊ณต๋ถ€๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -1,9 +1,111 @@ package nextstep.subway.path; +import static nextstep.subway.line.acceptance.LineAcceptanceTest.์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ; +import static org.assertj.core.api.Assertions.assertThat; + +import io.restassured.RestAssured; +import io.restassured.response.ExtractableResponse; +import io.restassured.response.Response; +import java.util.HashMap; +import java.util.Map; import nextstep.subway.AcceptanceTest; +import nextstep.subway.line.acceptance.LineSectionAcceptanceTest; +import nextstep.subway.line.dto.LineRequest; +import nextstep.subway.line.dto.LineResponse; +import nextstep.subway.station.StationAcceptanceTest; +import nextstep.subway.station.dto.StationResponse; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; @DisplayName("์ง€ํ•˜์ฒ  ๊ฒฝ๋กœ ์กฐํšŒ") public class PathAcceptanceTest extends AcceptanceTest { + + private LineResponse ์‹ ๋ถ„๋‹น์„ ; + private LineResponse ์ดํ˜ธ์„ ; + private LineResponse ์‚ผํ˜ธ์„ ; + private StationResponse ๊ฐ•๋‚จ์—ญ; + private StationResponse ์–‘์žฌ์—ญ; + private StationResponse ๊ต๋Œ€์—ญ; + private StationResponse ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ; + + /** + * ๊ต๋Œ€์—ญ --- *2ํ˜ธ์„ * --- ๊ฐ•๋‚จ์—ญ + * | | + * *3ํ˜ธ์„ * *์‹ ๋ถ„๋‹น์„ * + * | | + * ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ --- *3ํ˜ธ์„ * --- ์–‘์žฌ + */ + @BeforeEach + public void setUp() { + super.setUp(); + + ๊ฐ•๋‚จ์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("๊ฐ•๋‚จ์—ญ").as(StationResponse.class); + ์–‘์žฌ์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("์–‘์žฌ์—ญ").as(StationResponse.class); + ๊ต๋Œ€์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("๊ต๋Œ€์—ญ").as(StationResponse.class); + ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ").as(StationResponse.class); + + ์‹ ๋ถ„๋‹น์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(new LineRequest("์‹ ๋ถ„๋‹น์„ ", "bg-red-600", ๊ฐ•๋‚จ์—ญ.getId(), ์–‘์žฌ์—ญ.getId(), 10)).as( + LineResponse.class); + ์ดํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(new LineRequest("์ดํ˜ธ์„ ", "bg-red-600", ๊ต๋Œ€์—ญ.getId(), ๊ฐ•๋‚จ์—ญ.getId(), 10)).as(LineResponse.class); + ์‚ผํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(new LineRequest("์‚ผํ˜ธ์„ ", "bg-red-600", ๊ต๋Œ€์—ญ.getId(), ์–‘์žฌ์—ญ.getId(), 5)).as(LineResponse.class); + + ์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(์‚ผํ˜ธ์„ , ๊ต๋Œ€์—ญ, ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, 3); + } + + private ExtractableResponse<Response> ์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(LineResponse lineResponse, StationResponse upStation, + StationResponse downStation, + int distance) { + return LineSectionAcceptanceTest.์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก_์š”์ฒญ(lineResponse, + upStation, downStation, distance); + } + + @Test + @DisplayName("๊ต๋Œ€์—ญ-์–‘์žฌ์—ญ ์ตœ๋‹จ๊ฒฝ๋กœ ์กฐํšŒํ•˜๊ธฐ (๊ต๋Œ€์—ญ-๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ-์–‘์žฌ์—ญ)") + public void findMinPath() { + ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ = ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(๊ต๋Œ€์—ญ, ์–‘์žฌ์—ญ); + ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_๊ฒ€์ฆ(์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ, 5, "๊ต๋Œ€์—ญ","๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ", "์–‘์žฌ์—ญ"); + } + + + @Test + @DisplayName("๊ฒฝ๋กœ ์‹œ์ž‘์—ญ ๋„์ฐฉ์—ญ ์ผ์น˜ ์กฐํšŒ ์—๋Ÿฌ ํ™•์ธ") + public void findMinPathSourceTargetSame() { + ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ = ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(๊ต๋Œ€์—ญ, ๊ต๋Œ€์—ญ); + ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_์š”์ฒญ๊ฒฐ๊ณผ_์—๋Ÿฌ(์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ); + } + + @Test + @DisplayName("๊ฒฝ๋กœ ์กฐํšŒ์‹œ ๋ฏธ์กด์žฌ ์—ญ ์—๋Ÿฌ ํ™•์ธ") + public void findMinPathNotExsistStation() { + ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ = ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(new StationResponse(), new StationResponse()); + ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_์š”์ฒญ๊ฒฐ๊ณผ_์—๋Ÿฌ(์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ); + } + + private void ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_์š”์ฒญ๊ฒฐ๊ณผ_์—๋Ÿฌ(ExtractableResponse<Response> response) { + assertThat(response.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value()); + } + + private void ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_๊ฒ€์ฆ(ExtractableResponse<Response> response,int distance, String... stationNames) { + assertThat(response.jsonPath().getInt("distance")).isEqualTo(distance); + assertThat(response.jsonPath().getList("stations.name")).containsExactly(stationNames); + } + + private ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(StationResponse source, StationResponse target) { + Map<String, Long> param = new HashMap<>(); + param.put("source", source.getId()); + param.put("target", target.getId()); + + return RestAssured + .given().log().all() + .queryParams(param) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .when().get("/paths") + .then().log().all() + .extract(); + } + }
Java
์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์€ ์—ญ์— ๋Œ€ํ•œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ์กฐํšŒ์˜ ๊ฒฝ์šฐ์—๋„ ํ…Œ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”...?
@@ -0,0 +1,152 @@ +package nextstep.subway.acceptance; + +import static nextstep.subway.acceptance.FavoritesSteps.์ฆ๊ฒจ์ฐพ๊ธฐ_๊ถŒํ•œ์ด_์—†์Œ; +import static nextstep.subway.acceptance.FavoritesSteps.์ฆ๊ฒจ์ฐพ๊ธฐ_๋ชฉ๋ก_์กฐํšŒ_์š”์ฒญ; +import static nextstep.subway.acceptance.FavoritesSteps.์ฆ๊ฒจ์ฐพ๊ธฐ_์‚ญ์ œ_์š”์ฒญ; +import static nextstep.subway.acceptance.FavoritesSteps.์ฆ๊ฒจ์ฐพ๊ธฐ_์‚ญ์ œ๋จ; +import static nextstep.subway.acceptance.FavoritesSteps.์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ_์š”์ฒญ; +import static nextstep.subway.acceptance.FavoritesSteps.์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ๋จ; +import static nextstep.subway.acceptance.FavoritesSteps.์ฆ๊ฒจ์ฐพ๊ธฐ_์ •๋ณด_์กฐํšŒ๋จ; +import static nextstep.subway.acceptance.LineSteps.์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ _๊ตฌ๊ฐ„_์ƒ์„ฑ_์š”์ฒญ; +import static nextstep.subway.acceptance.MemberSteps.๋กœ๊ทธ์ธ_๋˜์–ด_์žˆ์Œ; +import static nextstep.subway.acceptance.MemberSteps.ํšŒ์›_์ƒ์„ฑ_์š”์ฒญ; +import static nextstep.subway.acceptance.StationSteps.์ง€ํ•˜์ฒ ์—ญ_์ƒ์„ฑ_์š”์ฒญ; + +import io.restassured.response.ExtractableResponse; +import io.restassured.response.Response; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("์ฆ๊ฒจ์ฐพ๊ธฐ ๊ธฐ๋Šฅ") +public class FavoritesAcceptanceTest extends AcceptanceTest { + + public static final String EMAIL = "email@email.com"; + public static final String PASSWORD = "password"; + public static final int AGE = 20; + + private Long ๊ต๋Œ€์—ญ; + private Long ๊ฐ•๋‚จ์—ญ; + private Long ์–‘์žฌ์—ญ; + private Long ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ; + private Long ์ดํ˜ธ์„ ; + private Long ์‹ ๋ถ„๋‹น์„ ; + private Long ์‚ผํ˜ธ์„ ; + private String ๋กœ๊ทธ์ธํ† ํฐ; + + + /** + * ๊ต๋Œ€์—ญ --- *2ํ˜ธ์„ * --- ๊ฐ•๋‚จ์—ญ | | *3ํ˜ธ์„ * *์‹ ๋ถ„๋‹น์„ * | + * | ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ --- *3ํ˜ธ์„ * --- ์–‘์žฌ + */ + @BeforeEach + public void setUp() { + super.setUp(); + + ๊ต๋Œ€์—ญ = ์ง€ํ•˜์ฒ ์—ญ_์ƒ์„ฑ_์š”์ฒญ("๊ต๋Œ€์—ญ").jsonPath().getLong("id"); + ๊ฐ•๋‚จ์—ญ = ์ง€ํ•˜์ฒ ์—ญ_์ƒ์„ฑ_์š”์ฒญ("๊ฐ•๋‚จ์—ญ").jsonPath().getLong("id"); + ์–‘์žฌ์—ญ = ์ง€ํ•˜์ฒ ์—ญ_์ƒ์„ฑ_์š”์ฒญ("์–‘์žฌ์—ญ").jsonPath().getLong("id"); + ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ = ์ง€ํ•˜์ฒ ์—ญ_์ƒ์„ฑ_์š”์ฒญ("๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ").jsonPath().getLong("id"); + + ์ดํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _์ƒ์„ฑ_์š”์ฒญ("2ํ˜ธ์„ ", "green", ๊ต๋Œ€์—ญ, ๊ฐ•๋‚จ์—ญ, 10); + ์‹ ๋ถ„๋‹น์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _์ƒ์„ฑ_์š”์ฒญ("์‹ ๋ถ„๋‹น์„ ", "red", ๊ฐ•๋‚จ์—ญ, ์–‘์žฌ์—ญ, 10); + ์‚ผํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _์ƒ์„ฑ_์š”์ฒญ("3ํ˜ธ์„ ", "orange", ๊ต๋Œ€์—ญ, ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, 2); + + ์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ _๊ตฌ๊ฐ„_์ƒ์„ฑ_์š”์ฒญ(์‚ผํ˜ธ์„ , createSectionCreateParams(๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, ์–‘์žฌ์—ญ, 3)); + + ํšŒ์›_์ƒ์„ฑ_์š”์ฒญ(EMAIL, PASSWORD, AGE); + ๋กœ๊ทธ์ธํ† ํฐ = ๋กœ๊ทธ์ธ_๋˜์–ด_์žˆ์Œ(EMAIL, PASSWORD); + } + + @DisplayName("์ฆ๊ฒจ์ฐพ๊ธฐ ์ถ”๊ฐ€ ํ•˜๊ธฐ") + @Test + public void addFavorite() { + // when + ExtractableResponse<Response> response = ์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ_์š”์ฒญ(๋กœ๊ทธ์ธํ† ํฐ, ๊ฐ•๋‚จ์—ญ, ์–‘์žฌ์—ญ); + + // then + ์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ๋จ(response); + } + + @DisplayName("์ฆ๊ฒจ์ฐพ๊ธฐ ์กฐํšŒ ํ•˜๊ธฐ") + @Test + public void getFavorites() { + // given + ์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ_์š”์ฒญ(๋กœ๊ทธ์ธํ† ํฐ, ๊ฐ•๋‚จ์—ญ, ์–‘์žฌ์—ญ); + ์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ_์š”์ฒญ(๋กœ๊ทธ์ธํ† ํฐ, ์–‘์žฌ์—ญ, ๊ต๋Œ€์—ญ); + + // when + ExtractableResponse<Response> response = ์ฆ๊ฒจ์ฐพ๊ธฐ_๋ชฉ๋ก_์กฐํšŒ_์š”์ฒญ(๋กœ๊ทธ์ธํ† ํฐ); + + // then + ์ฆ๊ฒจ์ฐพ๊ธฐ_์ •๋ณด_์กฐํšŒ๋จ(response, new Long[]{๊ฐ•๋‚จ์—ญ, ์–‘์žฌ์—ญ}, new Long[]{์–‘์žฌ์—ญ, ๊ต๋Œ€์—ญ}); + } + + @DisplayName("์ฆ๊ฒจ์ฐพ๊ธฐ ์‚ญ์ œ ํ•˜๊ธฐ") + @Test + public void deleteFavorites() { + // given + ExtractableResponse<Response> createResponse = ์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ_์š”์ฒญ(๋กœ๊ทธ์ธํ† ํฐ, ๊ฐ•๋‚จ์—ญ, ์–‘์žฌ์—ญ); + + // when + ExtractableResponse<Response> response = ์ฆ๊ฒจ์ฐพ๊ธฐ_์‚ญ์ œ_์š”์ฒญ(๋กœ๊ทธ์ธํ† ํฐ, createResponse); + + // then + ์ฆ๊ฒจ์ฐพ๊ธฐ_์‚ญ์ œ๋จ(response); + } + + @DisplayName("์ฆ๊ฒจ์ฐพ๊ธฐ ์ถ”๊ฐ€ ํ•˜๊ธฐ - ์œ ํšจํ•˜์ง€ ์•Š์€ ํ† ํฐ ์ผ ๊ฒฝ์šฐ") + @Test + public void addFavoriteWithInvalidToken() { + // when + ExtractableResponse<Response> response = ์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ_์š”์ฒญ("invalid_token", ๊ฐ•๋‚จ์—ญ, ์–‘์žฌ์—ญ); + + // then + ์ฆ๊ฒจ์ฐพ๊ธฐ_๊ถŒํ•œ์ด_์—†์Œ(response); + } + + @DisplayName("์ฆ๊ฒจ์ฐพ๊ธฐ ๋ชฉ๋ก์„ ๊ด€๋ฆฌํ•œ๋‹ค.") + @Test + public void manageFavorites() { + // when ์ฆ๊ฒจ์ฐพ๊ธฐ ์ƒ์„ฑ์„ ์š”์ฒญ + ExtractableResponse<Response> ์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ_์‘๋‹ต = ์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ_์š”์ฒญ(๋กœ๊ทธ์ธํ† ํฐ, ๊ฐ•๋‚จ์—ญ, ์–‘์žฌ์—ญ); + // then ์ฆ๊ฒจ์ฐพ๊ธฐ ์ƒ์„ฑ๋จ + ์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ๋จ(์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ_์‘๋‹ต); + + // when ์ฆ๊ฒจ์ฐพ๊ธฐ ๋ชฉ๋ก ์กฐํšŒ ์š”์ฒญ + ExtractableResponse<Response> ์ฆ๊ฒจ์ฐพ๊ธฐ_๋ชฉ๋ก_์กฐํšŒ_์‘๋‹ต = ์ฆ๊ฒจ์ฐพ๊ธฐ_๋ชฉ๋ก_์กฐํšŒ_์š”์ฒญ(๋กœ๊ทธ์ธํ† ํฐ); + // then ์ฆ๊ฒจ์ฐพ๊ธฐ ๋ชฉ๋ก ์กฐํšŒ๋จ + ์ฆ๊ฒจ์ฐพ๊ธฐ_์ •๋ณด_์กฐํšŒ๋จ(์ฆ๊ฒจ์ฐพ๊ธฐ_๋ชฉ๋ก_์กฐํšŒ_์‘๋‹ต, new Long[]{ ๊ฐ•๋‚จ์—ญ }, new Long[]{ ์–‘์žฌ์—ญ }); + + // when ์ฆ๊ฒจ์ฐพ๊ธฐ ์‚ญ์ œ ์š”์ฒญ + ExtractableResponse<Response> ์ฆ๊ฒจ์ฐพ๊ธฐ_์‚ญ์ œ_์‘๋‹ต = ์ฆ๊ฒจ์ฐพ๊ธฐ_์‚ญ์ œ_์š”์ฒญ(๋กœ๊ทธ์ธํ† ํฐ, ์ฆ๊ฒจ์ฐพ๊ธฐ_์ƒ์„ฑ_์‘๋‹ต); + // then ์ฆ๊ฒจ์ฐพ๊ธฐ ์‚ญ์ œ๋จ + ์ฆ๊ฒจ์ฐพ๊ธฐ_์‚ญ์ œ๋จ(์ฆ๊ฒจ์ฐพ๊ธฐ_์‚ญ์ œ_์‘๋‹ต); + } + + + private Long ์ง€ํ•˜์ฒ _๋…ธ์„ _์ƒ์„ฑ_์š”์ฒญ(String name, String color, Long upStation, Long downStation, + int distance) { + Map<String, String> lineCreateParams; + lineCreateParams = new HashMap<>(); + lineCreateParams.put("name", name); + lineCreateParams.put("color", color); + lineCreateParams.put("upStationId", upStation + ""); + lineCreateParams.put("downStationId", downStation + ""); + lineCreateParams.put("distance", distance + ""); + + return LineSteps.์ง€ํ•˜์ฒ _๋…ธ์„ _์ƒ์„ฑ_์š”์ฒญ(lineCreateParams).jsonPath().getLong("id"); + } + + private Map<String, String> createSectionCreateParams(Long upStationId, Long downStationId, + int distance) { + Map<String, String> params = new HashMap<>(); + params.put("upStationId", upStationId + ""); + params.put("downStationId", downStationId + ""); + params.put("distance", distance + ""); + return params; + } + +}
Java
`์ฆ๊ฒจ์ฐพ๊ธฐ ๋ชฉ๋ก์„ ๊ด€๋ฆฌํ•œ๋‹ค` ์—์„œ ์‹œ๋‚˜๋ฆฌ์˜ค ํ˜•์‹์œผ๋กœ ์ž˜ ์ž‘์„ฑํ•ด์ฃผ์…”์„œ ํ•ด๋‹น ํ…Œ์ŠคํŠธ๋“ค์€ ์ž‘์„ฑํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ˜„
@@ -0,0 +1,50 @@ +package nextstep.subway.applicaion; + +import java.util.List; +import java.util.stream.Collectors; +import nextstep.member.application.MemberService; +import nextstep.member.domain.LoginMember; +import nextstep.member.domain.MemberRepository; +import nextstep.subway.applicaion.dto.FavoriteRequest; +import nextstep.subway.applicaion.dto.FavoriteResponse; +import nextstep.subway.domain.Favorite; +import nextstep.subway.domain.FavoriteRepository; +import nextstep.subway.domain.Station; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class FavoriteService { + private final StationService stationService; + private final PathService pathService; + private final MemberRepository memberRepository; + private final FavoriteRepository favoriteRepository; + + public FavoriteService(StationService stationService, PathService pathService, + MemberRepository memberRepository, FavoriteRepository favoriteRepository) { + this.stationService = stationService; + this.pathService = pathService; + this.memberRepository = memberRepository; + this.favoriteRepository = favoriteRepository; + } + + public FavoriteResponse createFavorite(FavoriteRequest favoriteRequest, Long memberId) { + Station source = stationService.findById(favoriteRequest.getSource()); + Station target = stationService.findById(favoriteRequest.getTarget()); + pathService.findPath(source, target); + Favorite favorite = favoriteRepository.save(Favorite.of(memberId, source, target)); + return FavoriteResponse.from(favorite); + } + + @Transactional(readOnly = true) + public List<FavoriteResponse> findFavorites(Long memberId) { + List<Favorite> favorites = favoriteRepository.findAllByMemberId(memberId); + return favorites.stream().map(FavoriteResponse::from).collect(Collectors.toList()); + } + + public void deleteFavorite(Long favoriteId, Long memberId) { + Favorite favorite = favoriteRepository.getById(favoriteId); + favorite.hasPermission(memberId); + favoriteRepository.delete(favorite); + } +}
Java
memberRepository ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€ ์•Š๋Š”๋ฐ์š”, ์ฆ๊ฒจ์ฐพ๊ธฐ ์ƒ์„ฑ, ์กฐํšŒ, ์‚ญ์ œ ์‹œ ์œ ์š”ํ•œ memberId ์ธ์ง€ ํ™•์ธํ•˜์ง€ ์•Š์•„๋„ ๋ ๊นŒ์š”? ๐Ÿ˜„
@@ -22,10 +22,14 @@ public PathService(LineService lineService, StationService stationService) { public PathResponse findPath(Long source, Long target) { Station upStation = stationService.findById(source); Station downStation = stationService.findById(target); - List<Line> lines = lineService.findLines(); - SubwayMap subwayMap = new SubwayMap(lines); - Path path = subwayMap.findPath(upStation, downStation); + Path path = findPath(upStation, downStation); return PathResponse.of(path); } + + Path findPath(Station upStation, Station downStation) { + List<Line> lines = lineService.findLines(); + SubwayMap subwayMap = new SubwayMap(lines); + return subwayMap.findPath(upStation, downStation); + } }
Java
์ ‘๊ทผ์ œ์–ด์ž๋ฅผ ์ƒ๋žตํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ ๊ฐ€์š”? ๐Ÿ˜„
@@ -0,0 +1,39 @@ +package nextstep.subway.applicaion.dto; + +import nextstep.subway.domain.Favorite; + +public class FavoriteResponse { + private Long id; + private Long memberId; + private StationResponse source; + private StationResponse target; + + private FavoriteResponse(Long id, Long memberId, + StationResponse source, StationResponse target) { + this.id = id; + this.memberId = memberId; + this.source = source; + this.target = target; + } + + public static FavoriteResponse from(Favorite favorite) { + return new FavoriteResponse(favorite.getId(), favorite.getMemberId(), + StationResponse.of(favorite.getSource()), StationResponse.of(favorite.getTarget())); + } + + public Long getId() { + return id; + } + + public Long getMemberId() { + return memberId; + } + + public StationResponse getSource() { + return source; + } + + public StationResponse getTarget() { + return target; + } +}
Java
์š”๊ตฌ์‚ฌํ•ญ์— memberId ๋Š” ์—†๋„ค์š”! ์š”๊ตฌ์‚ฌํ•ญ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š” ๐Ÿ˜„
@@ -0,0 +1,49 @@ +package nextstep.subway.ui; + +import java.net.URI; +import java.util.List; +import nextstep.auth.authorization.AuthenticationPrincipal; +import nextstep.member.domain.LoginMember; +import nextstep.subway.applicaion.FavoriteService; +import nextstep.subway.applicaion.dto.FavoriteRequest; +import nextstep.subway.applicaion.dto.FavoriteResponse; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/favorites") +public class FavoriteController { + + private final FavoriteService favoriteService; + + public FavoriteController(FavoriteService favoriteService) { + this.favoriteService = favoriteService; + } + + @PostMapping + public ResponseEntity<FavoriteResponse> createFavorite( + @AuthenticationPrincipal LoginMember loginMember, + @RequestBody FavoriteRequest favoriteRequest) { + FavoriteResponse favoriteResponse = favoriteService.createFavorite(favoriteRequest, loginMember.getId()); + return ResponseEntity.created(URI.create("/favorites/" + favoriteResponse.getId())).body(favoriteResponse); + } + + @GetMapping + public ResponseEntity<List<FavoriteResponse>> getFavorites(@AuthenticationPrincipal LoginMember loginMember) { + List<FavoriteResponse> favoriteResponses = favoriteService.findFavorites(loginMember.getId()); + return ResponseEntity.ok(favoriteResponses); + } + + @DeleteMapping("/{favoriteId}") + public ResponseEntity<Void> deleteFavorite(@AuthenticationPrincipal LoginMember loginMember, @PathVariable Long favoriteId) { + favoriteService.deleteFavorite(favoriteId, loginMember.getId()); + return ResponseEntity.noContent().build(); + } + +}
Java
์ฆ๊ฒจ์ฐพ๊ธฐ ์ƒ์„ฑ ํ›„ ๋ฐ˜ํ™˜๊ฐ’์— ๋Œ€ํ•œ ์š”๊ตฌ์‚ฌํ•ญ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”! ๐Ÿ˜„ ![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-02-22 แ„‹แ…ฉแ„’แ…ฎ 7 58 34](https://user-images.githubusercontent.com/48339310/155118827-d664b3b1-32e7-4255-a58c-e5d09ce3550d.png)
@@ -1,5 +1,6 @@ package nextstep.subway.domain; +import java.util.Optional; import org.jgrapht.GraphPath; import org.jgrapht.alg.shortestpath.DijkstraShortestPath; import org.jgrapht.graph.SimpleDirectedWeightedGraph; @@ -22,6 +23,11 @@ public Path findPath(Station source, Station target) { DijkstraShortestPath<Station, SectionEdge> dijkstraShortestPath = new DijkstraShortestPath<>(graph); GraphPath<Station, SectionEdge> result = dijkstraShortestPath.getPath(source, target); + Optional.ofNullable(result) + .orElseThrow(() -> { + throw new IllegalArgumentException("์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + }); + List<Section> sections = result.getEdgeList().stream() .map(it -> it.getSection()) .collect(Collectors.toList());
Java
Optional ์„ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ ๊ฐ€์š”? ์–ด๋– ํ•œ ์ด์ ์ด ์žˆ๋‚˜์š”? ๐Ÿ˜„ ```suggestion if (Objects.isNull(result)) { throw new IllegalArgumentException("์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); } ```
@@ -0,0 +1,22 @@ +package codesquad.dto.question; + +import lombok.*; + +@AllArgsConstructor +@Getter +@Setter +@Builder +public class QuestionRequestDto { + private String writer; + private String title; + private String contents; + + @Override + public String toString() { + return "QuestionDto{" + + "writer='" + writer + '\'' + + ", title='" + title + '\'' + + ", contents='" + contents + '\'' + + '}'; + } +}
Java
๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -1,22 +1,36 @@ package codesquad; -import codesquad.repository.ArrayUserRepository; +import codesquad.repository.QuestionRepositoryImpl; +import codesquad.repository.UserRepositoryImpl; +import codesquad.repository.QuestionRepository; import codesquad.repository.UserRepository; +import codesquad.service.QuestionService; +import codesquad.service.QuestionServiceImpl; import codesquad.service.UserService; import codesquad.service.UserServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { - + @Bean public UserService userService() { return new UserServiceImpl(userRepository()); } @Bean public UserRepository userRepository() { - return new ArrayUserRepository(); + return new UserRepositoryImpl(); + } + + @Bean + public QuestionService questionService() { + return new QuestionServiceImpl(questionRepository()); + } + + @Bean + public QuestionRepository questionRepository() { + return new QuestionRepositoryImpl(); } }
Java
์ปจํŠธ๋กค๋Ÿฌ๋Š” @Controller ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•ด ๋นˆ์„ ๋“ฑ๋กํ•˜๊ณ  ๊ณ„์‹œ๋Š”๋ฐ, Service์™€ Repository๋Š” Config ํŒŒ์ผ์—์„œ ๋นˆ์„ ๋”ฐ๋กœ ๋“ฑ๋กํ•ด์ฃผ๊ณ  ๊ณ„์‹œ๋Š” ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -7,6 +7,8 @@ public interface UserRepository { void register(UserEntity userEntity); + void update(String userId, UserEntity userEntity); + UserEntity findById(String userId); List<UserEntity> findAll();
Java
update()๋Š” dto๋ฅผ ๋„˜๊ฒจ์ฃผ๊ณ  ์žˆ๋Š”๋ฐ, register()๋Š” ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,30 @@ +package codesquad.service; + +import codesquad.entity.QuestionEntity; +import codesquad.repository.QuestionRepository; + +import java.util.List; + +public class QuestionServiceImpl implements QuestionService { + private final QuestionRepository questionRepository; + + public QuestionServiceImpl(QuestionRepository questionRepository) { + this.questionRepository = questionRepository; + } + + @Override + public void postQuestion(QuestionEntity questionEntity) { + questionRepository.post(questionEntity); + questionRepository.setBaseTime(questionEntity); + } + + @Override + public List<QuestionEntity> findQuestions() { + return questionRepository.findAll(); + } + + @Override + public QuestionEntity findQuestion(String id) { + return questionRepository.findAll().get(Integer.parseInt(id) - 1); + } +}
Java
์—”ํ‹ฐํ‹ฐ๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์œผ๋ฉด ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ์žˆ์„์ง€ ๊ณ ๋ฏผํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,30 @@ +package codesquad.service; + +import codesquad.entity.QuestionEntity; +import codesquad.repository.QuestionRepository; + +import java.util.List; + +public class QuestionServiceImpl implements QuestionService { + private final QuestionRepository questionRepository; + + public QuestionServiceImpl(QuestionRepository questionRepository) { + this.questionRepository = questionRepository; + } + + @Override + public void postQuestion(QuestionEntity questionEntity) { + questionRepository.post(questionEntity); + questionRepository.setBaseTime(questionEntity); + } + + @Override + public List<QuestionEntity> findQuestions() { + return questionRepository.findAll(); + } + + @Override + public QuestionEntity findQuestion(String id) { + return questionRepository.findAll().get(Integer.parseInt(id) - 1); + } +}
Java
Entity, Model, Dto, VO์˜ ์ฐจ์ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,74 @@ +package codesquad.controller; + +import codesquad.AppConfig; +import codesquad.dto.question.QuestionRequestDto; +import codesquad.dto.question.QuestionResponseDto; +import codesquad.entity.QuestionEntity; +import codesquad.mapper.QuestionMapper; +import codesquad.service.QuestionService; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.List; +import java.util.stream.Collectors; + +@Controller +public class QuestionController { + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); + QuestionService questionService = applicationContext.getBean("questionService", QuestionService.class); + + @GetMapping("/questions/post") + public String getQuestionForm() { + return "qna/form"; + } + + @PostMapping("/questions") + public String postQuestion(QuestionRequestDto questionRequestDto) { + QuestionEntity questionEntity = QuestionMapper.dtoToEntity(questionRequestDto); + questionService.postQuestion(questionEntity); + return "redirect:/questions"; + } + + @GetMapping("/questions") + public String getQuestionList(Model model) { + List<QuestionResponseDto> questions = questionService.findQuestions().stream() + .map(questionEntity -> QuestionMapper.entityToDto(questionEntity)) + .collect(Collectors.toList()); + + model.addAttribute("questions", questions); + + return "qna/list"; + } + + @GetMapping("/questions/{id}") + public String getQuestionShow(@PathVariable String id, Model model) { + QuestionResponseDto question = new QuestionResponseDto(questionService.findQuestion(id)); + + model.addAttribute("writer", question.getWriter()); + model.addAttribute("title", question.getTitle()); + model.addAttribute("contents", question.getContents()); + model.addAttribute("date", question.getDate()); + + return "qna/show"; + } + + @GetMapping("/") + public String getHome(Model model) { + List<QuestionEntity> questionEntityList = questionService.findQuestions(); + if (questionEntityList.size() == 0) { + return "qna/list"; + } + + List<QuestionResponseDto> questionResponseDtoList = questionService.findQuestions().stream() + .map(questionEntity -> QuestionMapper.entityToDto(questionEntity)) + .collect(Collectors.toList()); + + model.addAttribute("questions", questionResponseDtoList); + return "qna/list"; + } +} \ No newline at end of file
Java
applicationcontext๋กœ๋ถ€ํ„ฐ ์ง์ ‘ ๋นˆ์„ ๊ฐ€์ ธ์˜ค์…”์„œ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -1,18 +1,17 @@ package codesquad.controller; import codesquad.AppConfig; -import codesquad.dto.UserDto; +import codesquad.dto.user.UserDto; +import codesquad.dto.user.UserUpdateRequestDto; import codesquad.entity.UserEntity; +import codesquad.mapper.UserMapper; import codesquad.service.UserService; import org.modelmapper.ModelMapper; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.stream.Collectors; @@ -39,7 +38,6 @@ public String registerUser(UserDto userDto) { @GetMapping() public String getUserList(Model model) { - System.out.println("userService.findUsers().get(0) = " + userService.findUsers().get(0)); List<UserDto> users = userService.findUsers().stream() .map(userEntity -> modelMapper.map(userEntity, UserDto.class)).collect(Collectors.toList()); model.addAttribute("users", users); @@ -57,4 +55,15 @@ public String getUserProfile(@PathVariable String userId, Model model) { return "user/profile"; } + + @GetMapping("/{userId}/form") + public String getProfileEditForm() { + return "user/update_form"; + } + + @PutMapping("/{userId}/update") + public String updateUserProfile(@PathVariable("userId") String userId, UserUpdateRequestDto requestDto) { + userService.updateUser(userId, UserMapper.dtoToEntity(requestDto)); + return "redirect:/users"; + } }
Java
์—…๋ฐ์ดํŠธ์—๋Š” ๋ณดํ†ต PUT๊ณผ PATCH๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘˜์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?
@@ -1,22 +1,36 @@ package codesquad; -import codesquad.repository.ArrayUserRepository; +import codesquad.repository.QuestionRepositoryImpl; +import codesquad.repository.UserRepositoryImpl; +import codesquad.repository.QuestionRepository; import codesquad.repository.UserRepository; +import codesquad.service.QuestionService; +import codesquad.service.QuestionServiceImpl; import codesquad.service.UserService; import codesquad.service.UserServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { - + @Bean public UserService userService() { return new UserServiceImpl(userRepository()); } @Bean public UserRepository userRepository() { - return new ArrayUserRepository(); + return new UserRepositoryImpl(); + } + + @Bean + public QuestionService questionService() { + return new QuestionServiceImpl(questionRepository()); + } + + @Bean + public QuestionRepository questionRepository() { + return new QuestionRepositoryImpl(); } }
Java
์šฐ์„  ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜์—ฌ ์ฝ”๋“œ/๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๊ธฐ ์œ„ํ•ด Service์™€ Repository๋Š” ์ž๋ฐ” ์ฝ”๋“œ๋กœ ์ง์ ‘ ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐœ๋ฐœ์„ ์ง„ํ–‰ํ•˜๋‹ค Repository๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์ผ ๋•Œ, Config์— ๋“ฑ๋ก๋œ Repository Bean๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ Controller์˜ ์—ญํ• ์„ ์ƒ๊ฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ปจํŠธ๋กค๋Ÿฌ๋Š” View์™€ Service๋ฅผ ์ด์–ด์ฃผ๋Š” ์—ญํ• ๋งŒ ํ•˜๊ณ  ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Repository๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ๋ณ€๊ฒฝ๋˜์–ด๋„ Controller๋Š” ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ Config์—์„œ ๋“ฑ๋กํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ref: https://inf.run/SZEa -> ๋ง ๋‹ค๋“ฌ๋Š”๋ฐ ๋„์›€ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค:)
@@ -0,0 +1,74 @@ +package codesquad.controller; + +import codesquad.AppConfig; +import codesquad.dto.question.QuestionRequestDto; +import codesquad.dto.question.QuestionResponseDto; +import codesquad.entity.QuestionEntity; +import codesquad.mapper.QuestionMapper; +import codesquad.service.QuestionService; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.List; +import java.util.stream.Collectors; + +@Controller +public class QuestionController { + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); + QuestionService questionService = applicationContext.getBean("questionService", QuestionService.class); + + @GetMapping("/questions/post") + public String getQuestionForm() { + return "qna/form"; + } + + @PostMapping("/questions") + public String postQuestion(QuestionRequestDto questionRequestDto) { + QuestionEntity questionEntity = QuestionMapper.dtoToEntity(questionRequestDto); + questionService.postQuestion(questionEntity); + return "redirect:/questions"; + } + + @GetMapping("/questions") + public String getQuestionList(Model model) { + List<QuestionResponseDto> questions = questionService.findQuestions().stream() + .map(questionEntity -> QuestionMapper.entityToDto(questionEntity)) + .collect(Collectors.toList()); + + model.addAttribute("questions", questions); + + return "qna/list"; + } + + @GetMapping("/questions/{id}") + public String getQuestionShow(@PathVariable String id, Model model) { + QuestionResponseDto question = new QuestionResponseDto(questionService.findQuestion(id)); + + model.addAttribute("writer", question.getWriter()); + model.addAttribute("title", question.getTitle()); + model.addAttribute("contents", question.getContents()); + model.addAttribute("date", question.getDate()); + + return "qna/show"; + } + + @GetMapping("/") + public String getHome(Model model) { + List<QuestionEntity> questionEntityList = questionService.findQuestions(); + if (questionEntityList.size() == 0) { + return "qna/list"; + } + + List<QuestionResponseDto> questionResponseDtoList = questionService.findQuestions().stream() + .map(questionEntity -> QuestionMapper.entityToDto(questionEntity)) + .collect(Collectors.toList()); + + model.addAttribute("questions", questionResponseDtoList); + return "qna/list"; + } +} \ No newline at end of file
Java
AppConfig์—์„œ @ComponentScan ๊ฐ™์€ Spring Bean์„ ์ž๋™์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๊ธฐ์— ์ง์ ‘ ๋นˆ์„ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค! (์‚ฌ์‹ค ์งˆ๋ฌธ ์˜๋„๋ฅผ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ถ€์—ฐ ์„ค๋ช… ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!)
@@ -1,18 +1,17 @@ package codesquad.controller; import codesquad.AppConfig; -import codesquad.dto.UserDto; +import codesquad.dto.user.UserDto; +import codesquad.dto.user.UserUpdateRequestDto; import codesquad.entity.UserEntity; +import codesquad.mapper.UserMapper; import codesquad.service.UserService; import org.modelmapper.ModelMapper; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.stream.Collectors; @@ -39,7 +38,6 @@ public String registerUser(UserDto userDto) { @GetMapping() public String getUserList(Model model) { - System.out.println("userService.findUsers().get(0) = " + userService.findUsers().get(0)); List<UserDto> users = userService.findUsers().stream() .map(userEntity -> modelMapper.map(userEntity, UserDto.class)).collect(Collectors.toList()); model.addAttribute("users", users); @@ -57,4 +55,15 @@ public String getUserProfile(@PathVariable String userId, Model model) { return "user/profile"; } + + @GetMapping("/{userId}/form") + public String getProfileEditForm() { + return "user/update_form"; + } + + @PutMapping("/{userId}/update") + public String updateUserProfile(@PathVariable("userId") String userId, UserUpdateRequestDto requestDto) { + userService.updateUser(userId, UserMapper.dtoToEntity(requestDto)); + return "redirect:/users"; + } }
Java
`PUT`์€ ์ž์› ์ „์ฒด๋ฅผ ๊ต์ฒดํ•˜์—ฌ, ์ž์›์˜ ๋ชจ๋“  ํ•„๋“œ๊ฐ€ ํ•„์š”ํ•˜๊ณ  `PATCH`๋Š” ์ž์›์˜ ๋ถ€๋ถ„๋งŒ ๊ต์ฒดํ•˜์—ฌ, ์ž์›์˜ ์ผ๋ถ€ ํ•„๋“œ๋งŒ ์žˆ์–ด๋„ ๋ฉ๋‹ˆ๋‹ค. GOOD ``` PUT /userId/1 { "userId" : "mywnajsldkf", "age" : 24 } GET /userId/1 { "userId" : "mywnajsldkf", "age" : 24 } ``` BAD ``` PUT /userId/1 { "userId" : "mywnajsldkf", } GET /userId/1 { "userId" : "mywnajsldkf", "age" : null } ``` GOOD ``` PATCH /userId/1 { "userId" : "mywnajsldkf", } GET /userId/1 { "userId" : "mywnajsldkf", "age" : 24 } ```
@@ -7,6 +7,8 @@ public interface UserRepository { void register(UserEntity userEntity); + void update(String userId, UserEntity userEntity); + UserEntity findById(String userId); List<UserEntity> findAll();
Java
dto <-> entity ์‚ฌ์ด์˜ ๋งตํ•‘์„ ์–ด๋””์„œ ํ•ด์•ผํ• ์ง€ ๊ณ ๋ฏผ์„ ํ–ˆ๋Š”๋ฐ์š”. DTO๋Š” ์ปจํŠธ๋กค๋Ÿฌ ๋ ˆ์ด์–ด์— ์ข…์†๋˜์–ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋งตํ•‘์— ๊ด€๋ จ๋œ ๊ฒƒ์€ ๋ชจ๋‘ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋‘๋ ค๊ณ  ํ•˜์˜€์Šต๋‹ˆ๋‹ค. update()์— ๋Œ€ํ•œ ๋ถ€๋ถ„์€ ํ”ผ๋“œ๋ฐฑ์„ ๋ณด๊ณ  ๋‹ค์‹œ ๋ณด๋‹ˆ, ์ƒˆ๋กœ์šด userEntity๋ฅผ ์ƒ์„ฑํ•˜๋Š” register์™€ ๋‹ค๋ฅด๊ฒŒ ๊ธฐ์กด์— ์žˆ๋Š” userEntity๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค๋ณด๋‹ˆ ๊ฐ’์„ updateํ•˜๋Š” ๋ฐฉํ–ฅ์— ์ง‘์ค‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๋ณด๋‹ˆ DTO -> Entity๋„ ์ƒˆ๋กญ๊ฒŒ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ์ƒ๊ฐ์— ๊ฑฐ์น˜์ง€ ์•Š์•˜๋„ค์š”.๐Ÿ˜… ๋‹ค์‹œ๋ณด๋‹ˆ register() ์ฒ˜๋Ÿผ controller์—์„œ entity๋กœ ๋ฐ”๊ฟ”์„œ ๋„˜๊ฒจ์ง€๊ณ  entity์— ์žˆ๋Š” ๊ฐ’๋“ค์„ repository์—์„œ ๊บผ๋‚ด์–ด ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์€๋ฐ @saint6839 ๋‹˜์˜ ์ƒ๊ฐ์€ ์–ด๋– ์„ธ์š”? +) dto <-> entity๋Š” ์–ด๋””์—์— ๊ด€ํ•œ ๋ถ€๋ถ„์€... ์ด์ „ ๋ฆฌ๋ทฐ์—์„œ๋„ ์งˆ๋ฌธ๋“œ๋ฆฌ๊ธฐ๋„ ํ–ˆ๊ณ , ๋‹ค์–‘ํ•œ [ํ† ๋ก ๊ธ€ 1](https://okky.kr/articles/708060), [ํ† ๋ก ๊ธ€ 2](https://tecoble.techcourse.co.kr/post/2021-04-25-dto-layer-scope/) ๋“ฑ์„ ์ฐพ์•„๋ณด๊ธฐ๋„ ํ–ˆ๋Š”๋ฐ์š”~ ์Šค์Šค๋กœ ๋‚ด๋ฆฐ ์ž‘์—… ๋ฐฉํ–ฅ์„ฑ์€ **repository๊นŒ์ง€ ๋“ค์–ด๊ฐ€์ง€ ๋ง ๊ฒƒ, ํ”„๋กœ์ ํŠธ ๋‚ด์— ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•  ๊ฒƒ, (๋ฌด์—‡๋ณด๋‹ค) ๊ฐ™์ด ํ•˜๋Š” ์‚ฌ๋žŒ์ด๋ž‘ ๋งž์ถœ ๊ฒƒ** ์ž…๋‹ˆ๋‹ค.๐Ÿ™‚ ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ƒ์—ฝ๋‹˜์˜ ์ƒ๊ฐ๋„ ๋“ค์–ด๋ณด๊ณ  ์‹ถ๋„ค์š”~