code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,54 @@
+const generateBasicToken = (userId: string, userPassword: string): string => {
+ const token = btoa(`${userId}:${userPassword}`);
+ return `Basic ${token}`;
+};
+
+const API_URL = `${import.meta.env.VITE_API_URL}`;
+const USER_ID = `${import.meta.env.VITE_USER_ID}`;
+const USER_PASSWORD = `${import.meta.env.VITE_USER_PASSWORD}`;
+
+try {
+ if (!API_URL || !USER_ID || !USER_PASSWORD) {
+ throw new Error(
+ "API_URL, USER_ID, PASSWORD environment variables are not set",
+ );
+ }
+} catch (error) {
+ console.log(error);
+}
+
+type HttpMethod = "GET" | "POST" | "PATCH" | "DELETE";
+
+interface RequestOptions {
+ method: HttpMethod;
+ body?: Record<string, unknown>;
+}
+
+export const fetchWithAuth = async (path: string, options: RequestOptions) => {
+ const requestInit = requestBuilder(options);
+ const response = await fetch(path, requestInit);
+
+ try {
+ if (!response.ok) {
+ throw new Error(`Failed to ${options.method} ${path}`);
+ }
+ } catch (error) {
+ console.log(error);
+ }
+
+ return response;
+};
+
+const requestBuilder = ({ method, body }: RequestOptions): RequestInit => {
+ const token = generateBasicToken(USER_ID, USER_PASSWORD);
+ const headers: HeadersInit = {
+ "Content-Type": "application/json",
+ Authorization: token,
+ };
+
+ return {
+ method,
+ headers,
+ body: body ? JSON.stringify(body) : undefined,
+ };
+}; | TypeScript | ์ค, ๋ฐ๋ก ์ ๊ทน ๋ฐ์ ํ๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,61 @@
+import { forwardRef, useContext } from "react";
+import * as PI from "./ProductItem.style";
+import CartControlButton from "../../Button/CartControlButton";
+import { deleteProductInCart, postProductInCart } from "@api/index";
+import { useError } from "@hooks/index";
+import { CartItemsContext } from "@context/CartItemsContext";
+
+interface ProductProps {
+ product: Product;
+}
+
+const ProductItem = forwardRef<HTMLDivElement, ProductProps>(
+ ({ product }, ref) => {
+ const { cartItems, refreshCartItems } = useContext(CartItemsContext);
+
+ const cartItemIds = cartItems.map((item) => item.product.id);
+ const isInCart = cartItemIds.includes(product.id);
+
+ const { showError } = useError();
+
+ const handleIsInCart = async () => {
+ try {
+ if (!isInCart) {
+ await postProductInCart(product.id);
+ refreshCartItems();
+ return;
+ }
+
+ const filteredItem = cartItems.find(
+ (item) => item.product.id === product.id,
+ );
+ if (filteredItem) {
+ await deleteProductInCart(filteredItem.id);
+ refreshCartItems();
+ }
+ } catch (error) {
+ if (error instanceof Error) {
+ showError(error.message);
+ }
+ }
+ };
+
+ return (
+ <PI.ProductItemStyle ref={ref}>
+ <PI.ProductImg
+ src={`${product.imageUrl}`}
+ alt={`${product.name} ์ํ ์ด๋ฏธ์ง`}
+ />
+ <PI.ProductGroup>
+ <PI.ProductContent>
+ <PI.ProductName>{product.name}</PI.ProductName>
+ <span>{product.price.toLocaleString("ko-kr")}์</span>
+ </PI.ProductContent>
+ <CartControlButton onClick={handleIsInCart} isInCart={isInCart} />
+ </PI.ProductGroup>
+ </PI.ProductItemStyle>
+ );
+ },
+);
+
+export default ProductItem; | Unknown | ์ง์ง ์ ์ฑ์ด๋ฆฐ ๋ต๋ณ ์ต๊ณ .. |
@@ -0,0 +1,44 @@
+import { FILTER_CATEGORIES, SORT_PRICE } from "@constants/rules";
+import * as PLH from "./ProductListHeader.style";
+
+interface ProductListHeaderProps {
+ handleCategory: (category: Category) => void;
+ handleSort: (sort: Sort) => void;
+}
+
+const ProductListHeader = ({
+ handleCategory,
+ handleSort,
+}: ProductListHeaderProps) => {
+ return (
+ <PLH.Header>
+ <PLH.Title>bpple ์ํ ๋ชฉ๋ก</PLH.Title>
+ <PLH.SelectBoxGroup>
+ <PLH.SelectBox
+ name=""
+ id=""
+ onChange={(e) => handleCategory(e.target.value as Category)}
+ >
+ {Object.entries(FILTER_CATEGORIES).map(([key, value]) => (
+ <option value={key} key={key}>
+ {value}
+ </option>
+ ))}
+ </PLH.SelectBox>
+ <PLH.SelectBox
+ name=""
+ id=""
+ onChange={(e) => handleSort(e.target.value as Sort)}
+ >
+ {Object.entries(SORT_PRICE).map(([key, value]) => (
+ <option value={key} key={key}>
+ {value}
+ </option>
+ ))}
+ </PLH.SelectBox>
+ </PLH.SelectBoxGroup>
+ </PLH.Header>
+ );
+};
+
+export default ProductListHeader; | Unknown | ์.. ๊ทธ๊ฒ์ ๋ฏธ์ฒ ์์ฑํ์ง ๋ชปํ ์ ์ ์ค์.. |
@@ -0,0 +1,44 @@
+import { FILTER_CATEGORIES, SORT_PRICE } from "@constants/rules";
+import * as PLH from "./ProductListHeader.style";
+
+interface ProductListHeaderProps {
+ handleCategory: (category: Category) => void;
+ handleSort: (sort: Sort) => void;
+}
+
+const ProductListHeader = ({
+ handleCategory,
+ handleSort,
+}: ProductListHeaderProps) => {
+ return (
+ <PLH.Header>
+ <PLH.Title>bpple ์ํ ๋ชฉ๋ก</PLH.Title>
+ <PLH.SelectBoxGroup>
+ <PLH.SelectBox
+ name=""
+ id=""
+ onChange={(e) => handleCategory(e.target.value as Category)}
+ >
+ {Object.entries(FILTER_CATEGORIES).map(([key, value]) => (
+ <option value={key} key={key}>
+ {value}
+ </option>
+ ))}
+ </PLH.SelectBox>
+ <PLH.SelectBox
+ name=""
+ id=""
+ onChange={(e) => handleSort(e.target.value as Sort)}
+ >
+ {Object.entries(SORT_PRICE).map(([key, value]) => (
+ <option value={key} key={key}>
+ {value}
+ </option>
+ ))}
+ </PLH.SelectBox>
+ </PLH.SelectBoxGroup>
+ </PLH.Header>
+ );
+};
+
+export default ProductListHeader; | Unknown | ๋ฐ๋ก ๋ถ๋ฆฌํ ๊น ๊ณ ๋ฏผํ์๋๋ฐ, ์๊ฒฌ์ด ๋ค์ด์์ผ๋ ๋ถ๋ฆฌํด๋ณด๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,195 @@
+# https://www.acmicpc.net/problem/9663
+
+# import sys
+# from turtle import width
+# n = int(sys.stdin.readline())
+
+# all_page = [0] * n #์ด
+# a = [False] * n #ํ
+# b = [False] * ((n * 2) - 1) #์ ๋ฐฉํฅ ๋๊ฐ
+# c = [False] * ((n * 2) - 1) #์ญ๋ฐฉํฅ ๋๊ฐ
+
+
+# def queen(n) -> None:
+# first = 0
+# count = 0
+# for i in range(n):
+# print("i=",i)
+# if (not a[i]
+# and not b[first+i]
+# and not c[first-i+(n-1)]
+# ):
+# print("qwe")
+# count += 1
+# if i == n-1:
+# print(count)
+# else:
+# a[i] = True
+# b[i+first] = True
+# c[i-first+(n-1)] = True
+# queen(first+1)
+# a[i] = False
+# b[i+first] = False
+# c[i-first+(n-1)] = False
+# queen(8)
+
+
+# import sys
+# n = int(sys.stdin.readline())
+
+# pos = [0] * n #์ด
+# flag_a = [False] * n #ํ
+# flag_b = [False] * ((n * 2) - 1) #์ ๋ฐฉํฅ ๋๊ฐ
+# flag_c = [False] * ((n * 2) - 1) #์ญ๋ฐฉํฅ ๋๊ฐ
+# sum = [0]
+
+
+# def queen(i) -> None:
+# for j in range(n):
+# if(not flag_a[j]
+# and not flag_b[i+j]
+# and not flag_c[i-j+(n-1)]):
+# pos[i] = j
+# if i == n-1:
+# # for i in range(n):
+# # print(f'{pos[i]:2}', end='')
+# # print()
+# sum[0] += 1
+# else:
+# flag_a[j] = flag_b[i+j] = flag_c[i-j+(n-1)] = True
+# queen(i + 1)
+# flag_a[j] = flag_b[i+j] = flag_c[i-j+(n-1)] = False
+# queen(0)
+# print(sum[0])
+
+# import sys
+# n = int(sys.stdin.readline())
+
+# pos = [0] * n #์ด
+# flag_a = [False] * n #ํ
+# flag_b = [False] * ((n * 2) - 1) #์ ๋ฐฉํฅ ๋๊ฐ
+# flag_c = [False] * ((n * 2) - 1) #์ญ๋ฐฉํฅ ๋๊ฐ
+# sum = [0]
+
+
+# def queen(i) -> None:
+# for j in range(n):
+# if(not flag_a[j]
+# and not flag_b[i+j]
+# and not flag_c[i-j+7]):
+# pos[i] = j
+# if i == 7:
+# # for i in range(8):
+# # print(f'{pos[i]:2}', end='')
+# # print()
+# sum[0] += 1
+# else:
+# flag_a[j] = flag_b[i+j] = flag_c[i-j+7] = True
+# queen(i + 1)
+# flag_a[j] = flag_b[i+j] = flag_c[i-j+7] = False
+# queen(0)
+# print(sum[0])
+
+# https://seongonion.tistory.com/103
+# import sys
+
+# n = int(sys.stdin.readline())
+
+# ans = 0
+# row = [0] * n
+
+# def is_promising(x):
+# for i in range(x):
+# if row[x] == row[i] or abs(row[x] - row[i]) == abs(x - i):
+# return False
+
+# return True
+
+# def n_queens(x):
+# global ans
+# if x == n:
+# ans += 1
+# return
+
+# else:
+# for i in range(n):
+# # [x, i]์ ํธ์ ๋๊ฒ ๋ค.
+# row[x] = i
+# if is_promising(x):
+# n_queens(x+1)
+
+# n_queens(0)
+# print(ans)
+
+# https://velog.io/@inhwa1025/BOJ-9663%EB%B2%88-N-Queen-Python-%ED%8C%8C%EC%9D%B4%EC%8D%AC
+
+# n = int(input())
+# result = 0
+
+
+# # ํธ์ ๋์ ํ ๊ทธ ์ดํ์ ์ค์ ๋ํด์๋ง ๋ถ๊ฐ๋ฅํ ์นธ ์ฒดํฌ
+# def visit(x, y, in_visited):
+# tmp_visited = [visi[:] for visi in in_visited]
+# for i in range(1, n-x):
+# tmp_visited[x+i][y] = True # ์๋ ๋ฐฉํฅ ์ฒดํฌ
+# if 0 <= y-i < n:
+# tmp_visited[x+i][y-i] = True # ์ผ์ชฝ ์๋ ๋๊ฐ์ ์ฒดํฌ
+# if 0 <= y+i < n:
+# tmp_visited[x+i][y+i] = True # ์ค๋ฅธ์ชฝ ์๋ ๋๊ฐ์ ์ฒดํฌ
+# return tmp_visited
+
+
+# def recursion(q, _visited): # q๋ฒ์งธ ์ค์ ํธ์ ๋ ์ ์๋ ๊ฒฝ์ฐ๋ค์ ํ์ธํ๋ ์ฌ๊ทํจ์
+# global result
+# # ํ ์ค์ ํธ์ด ํ๋์ฉ ๋ค์ด๊ฐ์ผ ํจ
+# # ํ ์ค ์ ์ฒด๊ฐ ๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ ์์ n๊ฐ์ ํธ์ ๋ชจ๋ ๋์ ์ ์์ผ๋ฏ๋ก ์ฌ๊ท ์ข
๋ฃ
+# for idx in range(q, n):
+# if sum(_visited[idx]) == n:
+# return 0
+# # ๋ง์ง๋ง ์ค์ ๋๋ฌํ ๊ฒฝ์ฐ ๊ฐ๋ฅํ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ์ธ๊ณ ์ฌ๊ท ์ข
๋ฃ
+# if q == (n-1):
+# result += n - sum(_visited[q])
+# return 0
+
+# for i in range(n):
+# if not _visited[q][i]: # ํธ์ ๋ ์ ์๋ ๊ฒฝ์ฐ
+# tmp = visit(q, i, _visited) # ํธ์ ๋์ ๋ ๋ถ๊ฐ๋ฅํ ์นธ๋ค ์ฒดํฌ
+# recursion(q+1, tmp) # ๊ทธ ๋ค์ ์ค์ ๋ํด ์ฌ๊ท ํธ์ถ
+# # ์ฌ๊ทํธ์ถ ์ข
๋ฃ ํ ํธ์ ๋ ์ ์๋ ๋ค๋ฅธ ๊ฒฝ์ฐ์ ๋ํด ์ฒดํฌ
+
+
+# visited = [[False for _ in range(n)] for _ in range(n)]
+# recursion(0, visited) # 0๋ฒ์งธ ์ค๋ถํฐ ํ์ ์์
+# print(result)
+
+
+
+#๋ฐ์ฐฌ์ฐ๋์ ์ฝ๋
+
+global cnt
+cnt = 0
+
+def n_queen (col, i) :
+ n = len(col) - 1
+ if (judge(col, i)) :
+ if(i==n) :
+ print(col[1: n+1])
+ else :
+ for j in range(1, n+1) :
+ col[i+1] = j
+ n_queen(col, i+1, cnt)
+
+ print(cnt)
+
+def judge(col, i) :
+ k = 1
+ flag = True
+ while (k < i and flag) :
+ if (col[i] == col[k] or abs(col[i] - col[k]) == (i - k)) :
+ flag = False
+ k += 1
+ return flag
+
+n = 8
+a = [0] * (n+1)
+n_queen(a, 0, 0)
\ No newline at end of file | Python | ์ ๋ ๋จ์ ๊ฑฐ ์ฐธ๊ณ ํ๊ฑฐ๋ผ ๋ถ๋๋ฝ๋ค์ ใ
์ด์ฐจ์ ๋ฐฐ์ด๋ก ํ๋ค๊ฐ 1์ฐจ์๋ฐฐ์ด๋ก ์ค์ผ ์ ์๋ค๋ ๋ฐ์์ด ์ด๋ ค์ ๋ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,23 @@
+# https://www.acmicpc.net/problem/1181
+# https://blockdmask.tistory.com/543
+# set์ ์ด์ฉํ๋ฉด ์ค๋ณต์ด ์ ๊ฑฐ๋จ
+# https://always-challenger-lab.tistory.com/22
+# ๋๋ค ์ด์ฉํด์ ๋ค์ค์กฐ๊ฑด ์ ๋ ฌ
+import sys
+
+n = int(sys.stdin.readline())
+s = []
+m = []
+for i in range(n):
+ s.append(str(sys.stdin.readline().strip()))
+
+
+set_s = list(set(s))
+for j in set_s:
+ m.append([j,len(j)])
+
+print(m)
+m.sort(key=lambda x:(x[1],x[0]))
+
+for i in m:
+ print(i[0])
\ No newline at end of file | Python | ๋๋ค ์ฌ์ฉ์ด ์ ๋ฐํค๋ค์ ใ
.ใ
์ฐธ๊ณ ๊ฐ ๋์ต๋๋ค. |
@@ -0,0 +1,9 @@
+package com.codesquad.baseballgame.domain.game.controller;
+
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class GameController {
+
+
+} | Java | ์. ์ด๋ฐ ์ปจํธ๋กค๋ฌ๋ ์ฐ์ง ์๋๋ค๋ฉด ๊ทธ๋๊ทธ๋ ์ง์์ฃผ๋ ๊ฒ์ด ์ข๊ฒ ์ต๋๋ค. |
@@ -0,0 +1,24 @@
+package com.codesquad.baseballgame.domain.game.controller;
+
+import com.codesquad.baseballgame.domain.game.service.MatchService;
+import lombok.RequiredArgsConstructor;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequiredArgsConstructor
+public class MatchController {
+
+ private final MatchService matchService;
+
+ @PostMapping("games/{id}")
+ public ResponseEntity<Boolean> matching(@PathVariable int id) {
+ if (matchService.matchStatus(id)) {
+ return new ResponseEntity<>(true, HttpStatus.OK);
+ }
+ return new ResponseEntity<>(false, HttpStatus.OK);
+ }
+} | Java | ์ค๊ณํ๊ธฐ ๋๋ฆ์ด๊ธด ํ์ง๋ง, `@PathVariable` ๋ง์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๋ ์๋ํฌ์ธํธ์์ `POST` ๋ฅผ ์ฐ๋๊ฑด ๋ค์ ์ด์ํด๋ณด์ด๊ธด ํ๋ค์.
๋ญ๊ฐ๊ฐ ๋๋ฝ๋ ๊ฑด ์๋๊ฐ์. ๋ฆฌํด ๋ฐ๋์ `true` `false` ๋ง์ ์ฃ๋ ๊ฒ๋ ์ข ์ด์ํฉ๋๋ค. |
@@ -0,0 +1,7 @@
+package com.codesquad.baseballgame.domain.game.dao;
+
+import org.springframework.stereotype.Repository;
+
+@Repository
+public class GameDao {
+} | Java | ์ ์ฌ๊ธฐ๋ ๋น์ด์๊ตฐ์? |
@@ -0,0 +1,14 @@
+package com.codesquad.baseballgame.domain.game.dto;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.ToString;
+
+@Getter @ToString
+@RequiredArgsConstructor
+public class BallCountDto {
+
+ private int strike;
+ private int ball;
+ private int out;
+} | Java | ์ ๋
ธํ
์ด์
์ ํ ์ค์ ํ๋๋ฅผ ์ฐ๋ ๊ฒ์ ์์น์ผ๋ก ํด ์ฃผ์ธ์.
```suggestion
@Getter
@ToString |
@@ -0,0 +1,14 @@
+package com.codesquad.baseballgame.domain.game.dto;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.ToString;
+
+@Getter @ToString
+@RequiredArgsConstructor
+public class BallCountDto {
+
+ private int strike;
+ private int ball;
+ private int out;
+} | Java | ์์ฑ์๊ฐ ์ํ๋ ๋๋ก ์์ฑ๋์๋์?
์ ๊ฐ ์๊ธฐ๋ก ์ด ์ ๋
ธํ
์ด์
์ `final` ํ๋์ ๋ํด์๋ง ์์ฑ์๋ฅผ ๋ง๋๋๋ฐ์.
์๋ ์ด๋ค ํ๋๋ `final` ์์ฝ์ด๋ฅผ ๊ฐ์ง ์๋ค์. |
@@ -0,0 +1,10 @@
+package com.codesquad.baseballgame.domain.game.service;
+
+import org.springframework.stereotype.Service;
+
+@Service
+public class GameService {
+
+
+
+} | Java | ์ ์ด๋ ๊ฒ ๋น์ด์๋ ํด๋์ค๋ค์ด ๋ง์๊ฑฐ์ฃ ... |
@@ -0,0 +1,23 @@
+package com.codesquad.baseballgame.domain.game.service;
+
+import com.codesquad.baseballgame.domain.team.dao.TeamDao;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Isolation;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@RequiredArgsConstructor
+public class MatchService {
+
+ private final TeamDao teamDao;
+
+ @Transactional(isolation = Isolation.SERIALIZABLE)
+ public Boolean matchStatus(int id) {
+ return countMatchUser(id) == 2;
+ }
+
+ public Integer countMatchUser(int id) {
+ return teamDao.countMatchUser(id);
+ }
+} | Java | `SERIALIZABLE` ์ ๋ง ๊ด์ฐฎ์๊น์? ์ ํํ ์ดํดํ๊ณ ์ฌ์ฉํ์ ๊ฑด๊ฐ์.
๋๋ถ๋ถ์ ๊ฒฝ์ฐ `SERIALIZABLE` ๊น์ง ๊ณ ๋ฆฝ์์ผ์ผ ํ ํธ๋์ญ์
์ ์์ต๋๋ค.
์ ํ๋ฆฌ์ผ์ด์
๋ ์ด์ด์์ ์ด๋ฐ ์์ค์ ๊ณ ๋ฆฝ์ด ํ์ํ ๊ฒฝ์ฐ, ์ ํ๋ฆฌ์ผ์ด์
๋ก์ง์ ๋ค์ ๊ฒํ ํด์ ์ ๋ง ์ด์ ๋์ ํธ๋์ญ์
์ด ํ์ํ์ง ์ดํด๋ณด๊ณ ,
์ ๋ง ๊ณ ๋ฆฝ ์์ค ์กฐ์ ์ด ํ์ํ ๊ฒฝ์ฐ์๋, ์ฌ์ค์ ๊ณ ๋ฆฝ ์์ค ์กฐ์ ์ด ์๋๋ผ propagation ์ ๋ต ์์ ์ผ๋ก ๋์ํ ์ ์์ง๋ ์์์ง ๋ค์ ํ ๋ฒ ์๊ฐํด๋ด์ผ ํฉ๋๋ค.
์๋ง ๊ฐฏ์๋ฅผ ์ธ๋ ์ฟผ๋ฆฌ๊ฐ ์๋ค๋ณด๋ ์ฟผ๋ฆฌ๊ฐ ๋๋ ๋์ ์๋ก์ด ์ปค๋ฐ์ด ์ผ์ด๋์ ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฐ๋๋ ์ผ์ ์ฐ๋ คํ์์ง๋ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
๊ทธ๋ฐ ๊ฒฝ์ฐ๋ผ๋ฉด `READ COMMITTED` ๋ก ์ถฉ๋ถํ์ง ์๋์. ์..... |
@@ -0,0 +1,23 @@
+package com.codesquad.baseballgame.domain.game.service;
+
+import com.codesquad.baseballgame.domain.team.dao.TeamDao;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Isolation;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@RequiredArgsConstructor
+public class MatchService {
+
+ private final TeamDao teamDao;
+
+ @Transactional(isolation = Isolation.SERIALIZABLE)
+ public Boolean matchStatus(int id) {
+ return countMatchUser(id) == 2;
+ }
+
+ public Integer countMatchUser(int id) {
+ return teamDao.countMatchUser(id);
+ }
+} | Java | `@Transactional` ์ด ์ ์ธ๋ ๋ฉ์๋์์ ์ด๋ ๊ฒ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด, ํธ๋์ญ์
์ด ์ ์ฉ๋์ง ์์ต๋๋ค.
https://mommoo.tistory.com/92 |
@@ -0,0 +1,20 @@
+package com.codesquad.baseballgame.domain.hitter.mapper;
+
+import com.codesquad.baseballgame.domain.hitter.dto.HitterListDto;
+import org.springframework.jdbc.core.RowMapper;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+public class HitterListMapper implements RowMapper<HitterListDto> {
+
+ @Override
+ public HitterListDto mapRow(ResultSet rs, int rowNum) throws SQLException {
+ return HitterListDto.builder()
+ .teamName(rs.getString("name"))
+ .totalsBatterBox(rs.getInt("total_sum"))
+ .totalsHit(rs.getInt("total_hit"))
+ .totalsOut(rs.getInt("total_out"))
+ .build();
+ }
+} | Java | `RowMapper` ์์ ๋ฐ๋ก DTO๋ก ๋งคํํ๋ค์.
๋ชจ๋ธ ํด๋์ค๊ฐ ํ๋ ์๋๊ฒ ์ข๊ธด ํ๋ฐ... ๋ชจ๋ธ ํด๋์ค์ ํ์์ฑ์ด ์์๋๋ณด๋ค์. |
@@ -0,0 +1,15 @@
+package com.codesquad.baseballgame.domain.scoreboard.controller;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class ScoreBoardController {
+
+ @GetMapping("games/{id}/scoreboard")
+ public void showScoreBoard(@PathVariable int id) {
+
+
+ }
+} | Java | ์...... |
@@ -0,0 +1,23 @@
+package com.codesquad.baseballgame.domain.scoreboard.dao;
+
+import com.codesquad.baseballgame.domain.scoreboard.dto.ScoreBoardInningDto;
+import com.codesquad.baseballgame.domain.team.mapper.TeamDtoMapper;
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
+import org.springframework.stereotype.Repository;
+
+import javax.sql.DataSource;
+
+@Repository
+public class ScoreBoardDao {
+
+ private final NamedParameterJdbcTemplate namedJdbcTemplate;
+ private final TeamDtoMapper teamDtoMapper = new TeamDtoMapper();
+
+ public ScoreBoardDao(DataSource dataSource) {
+ this.namedJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
+ }
+
+// public ScoreBoardInningDto findInningById(int id) {
+// String inningSql = ""
+// }
+} | Java | ์ฃผ์์ ์ง์์ฃผ์ธ์. |
@@ -0,0 +1,9 @@
+package com.codesquad.baseballgame.domain.game.controller;
+
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class GameController {
+
+
+} | Java | ๋ค ๊ผญ ์ง์ฐ๋๋ก ํ๊ฒ ์ต๋๋ค ์ ๋ ํด์ ์๋๋ ์ง์ ํด๋ฒ๋ ธ๊ตฐ์. ๊ผญ ๋ช
์ฌํ๊ฒ ์ต๋๋ค |
@@ -0,0 +1,24 @@
+package com.codesquad.baseballgame.domain.game.controller;
+
+import com.codesquad.baseballgame.domain.game.service.MatchService;
+import lombok.RequiredArgsConstructor;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequiredArgsConstructor
+public class MatchController {
+
+ private final MatchService matchService;
+
+ @PostMapping("games/{id}")
+ public ResponseEntity<Boolean> matching(@PathVariable int id) {
+ if (matchService.matchStatus(id)) {
+ return new ResponseEntity<>(true, HttpStatus.OK);
+ }
+ return new ResponseEntity<>(false, HttpStatus.OK);
+ }
+} | Java | ์ฐ๋ฉด์ ์๋ฟ์ธ.. ํ๊ณ ๋๋์ ์์๋๋ฐ.. ๋ง์ง๋ง์ ๋ง๋๋๋ผ ๊ธฐ๋ณธ ๋ฒ์น์ ์๊ณ ๋ง ๋ง๋ ๊ฒ ์์ธ์ด์์ต๋๋ค. ๊ธํ๋๋ผ๋ ์งํฌ๊ฑด ์งํค๋ฉด์ ๋ง๋ค์ด์ผํ๋๋ฐ ์ ๊ฐ ๋๋ฌด ์๋๋ ๋ค์. ์ต์ํ์ Json์ ๋ง๋ค์์ด์ผํ๋๋ฐ ๋ค์์๋ ์งํค๋๋ก ํ๊ฒ ์ต๋๋ค. |
@@ -0,0 +1,7 @@
+package com.codesquad.baseballgame.domain.game.dao;
+
+import org.springframework.stereotype.Repository;
+
+@Repository
+public class GameDao {
+} | Java | ๋ค... ๋ง์์ ์ง์ ์ด์ผํ์ด์. |
@@ -0,0 +1,14 @@
+package com.codesquad.baseballgame.domain.game.dto;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.ToString;
+
+@Getter @ToString
+@RequiredArgsConstructor
+public class BallCountDto {
+
+ private int strike;
+ private int ball;
+ private int out;
+} | Java | ์ฌ๋ฌ์๊ฐ ํ๋ฉด์ ์ฝ๋ฉํ๋ค๊ฐ ์๋ฌด๊ฑฐ๋ ๋ค ๋ฃ๋๋ฐ๋์ ์๊ธด ๋ฌธ์ ์
๋๋ค. ๊ธฐ๋ฅ ํ๋๋ง๋ค ๋ ๊ทธ๋ ์ง์คํ์ด์ผ ํ๋๋ฐ ์ด๊ฒ ์ ๊ฒํ๋ค๊ฐ ์ด๋์ ๋ ์๋๊ฒ ๋์์ต๋๋ค. ์ ๊ฐ ์ด๊ฑธ ๋ฃ์๋ค๋๊ฒ๋ ์ง๊ธ ์์๋ค์. Dto ์์ ๋ฃ์ ์๊ฐ ์์๋๋ฐ.... ์ด๋ฒ ๊ธฐํ๋ก ์ ์ ๋๋ฐ๋ก ์ฐจ๋ ค์ผ๊ฒ ์ต๋๋ค |
@@ -0,0 +1,23 @@
+package com.codesquad.baseballgame.domain.game.service;
+
+import com.codesquad.baseballgame.domain.team.dao.TeamDao;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Isolation;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@RequiredArgsConstructor
+public class MatchService {
+
+ private final TeamDao teamDao;
+
+ @Transactional(isolation = Isolation.SERIALIZABLE)
+ public Boolean matchStatus(int id) {
+ return countMatchUser(id) == 2;
+ }
+
+ public Integer countMatchUser(int id) {
+ return teamDao.countMatchUser(id);
+ }
+} | Java | ํธ๋์ ์
์ ๋ํ ๋ด์ฉ์ ๋ธ๋ก๊ทธ์์ ๊ฐ๋ณ๊ฒ ๋ณด๊ณ ์ด ๋ ์ฐ๋๊ฑด๊ฐ ํ๊ณ ์ผ๋๋ฐ ์ข ๋ ๊ณต๋ถ๊ฐ ํ์ํ๋๊ตฐ์. Read Committed ์ ๋ํด์ ์กฐ์ฌํ๊ฒ ์ต๋๋ค. |
@@ -0,0 +1,20 @@
+package com.codesquad.baseballgame.domain.hitter.mapper;
+
+import com.codesquad.baseballgame.domain.hitter.dto.HitterListDto;
+import org.springframework.jdbc.core.RowMapper;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+public class HitterListMapper implements RowMapper<HitterListDto> {
+
+ @Override
+ public HitterListDto mapRow(ResultSet rs, int rowNum) throws SQLException {
+ return HitterListDto.builder()
+ .teamName(rs.getString("name"))
+ .totalsBatterBox(rs.getInt("total_sum"))
+ .totalsHit(rs.getInt("total_hit"))
+ .totalsOut(rs.getInt("total_out"))
+ .build();
+ }
+} | Java | ๋ชจ๋ธ ํด๋์ค์ ํ์์ฑ์ด ์์๋ค๊ธฐ๋ณด๋จ ์ค๊ณ๊ฐ ์๋ชป๋์์ต๋๋ค. ๋ง์ฝ ๋ชจ๋ธํด๋์ค์ ๋ง๊ฒ ํ๋ค๋ฉด ์ง๊ธ๋ณด๋ค ๋์ฑ ๋น ๋ฅด๊ฒ ์ฝ๋ฉ์ด ๋์์ํ
๋ฐ ์์ฝ์ต๋๋ค. ์ค๊ณ๋ฅผ ๊ผญ ์ ๋๋ก ๋ง์ถฐ์ผ๊ฒ ๋ค์ |
@@ -0,0 +1,23 @@
+package com.codesquad.baseballgame.domain.scoreboard.dao;
+
+import com.codesquad.baseballgame.domain.scoreboard.dto.ScoreBoardInningDto;
+import com.codesquad.baseballgame.domain.team.mapper.TeamDtoMapper;
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
+import org.springframework.stereotype.Repository;
+
+import javax.sql.DataSource;
+
+@Repository
+public class ScoreBoardDao {
+
+ private final NamedParameterJdbcTemplate namedJdbcTemplate;
+ private final TeamDtoMapper teamDtoMapper = new TeamDtoMapper();
+
+ public ScoreBoardDao(DataSource dataSource) {
+ this.namedJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
+ }
+
+// public ScoreBoardInningDto findInningById(int id) {
+// String inningSql = ""
+// }
+} | Java | ๋ต |
@@ -0,0 +1,15 @@
+package com.codesquad.baseballgame.domain.scoreboard.controller;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class ScoreBoardController {
+
+ @GetMapping("games/{id}/scoreboard")
+ public void showScoreBoard(@PathVariable int id) {
+
+
+ }
+} | Java | ์ฉ์ํด์ฃผ์ญ์์ค... |
@@ -0,0 +1,23 @@
+package com.codesquad.baseballgame.domain.game.service;
+
+import com.codesquad.baseballgame.domain.team.dao.TeamDao;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Isolation;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@RequiredArgsConstructor
+public class MatchService {
+
+ private final TeamDao teamDao;
+
+ @Transactional(isolation = Isolation.SERIALIZABLE)
+ public Boolean matchStatus(int id) {
+ return countMatchUser(id) == 2;
+ }
+
+ public Integer countMatchUser(int id) {
+ return teamDao.countMatchUser(id);
+ }
+} | Java | ํธ๋์ ์
์ ๋ํด์ ํ์คํ๊ฒ ๊ณต๋ถํด์ผ๊ฒ ๋ค์. ์ด์คํ ์ฌ์ฉ์ ์ง์ํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. |
@@ -0,0 +1,23 @@
+package com.codesquad.baseballgame.domain.game.service;
+
+import com.codesquad.baseballgame.domain.team.dao.TeamDao;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Isolation;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@RequiredArgsConstructor
+public class MatchService {
+
+ private final TeamDao teamDao;
+
+ @Transactional(isolation = Isolation.SERIALIZABLE)
+ public Boolean matchStatus(int id) {
+ return countMatchUser(id) == 2;
+ }
+
+ public Integer countMatchUser(int id) {
+ return teamDao.countMatchUser(id);
+ }
+} | Java | ์ ์ค๋ ์ ํํ๊ฒ ์์
์ ํ๋๊ตฐ์ ๋ฆฌ๋ทฐ๋ณด์๋ง์ ์์
์ ํ๋ ๊ธฐ์ฉ๋๋ค. Repeatable Read๋ก๋ง ์ปค๋ฒ๊ฐ ๊ฐ๋ฅํ๋ค์ |
@@ -8,71 +8,72 @@
import bridge.view.OutputView;
public class BridgeGameController {
- private final OutputView outputView = new OutputView();
- private final InputView inputView = new InputView();
+ private final OutputView outputView;
+ private final InputView inputView;
private BridgeGame bridgeGame;
- private boolean isGameEndStatus = false;
- private boolean isGameRunning = false;
+
+ public BridgeGameController(OutputView outputView, InputView inputView) {
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
public void play() {
outputView.initGame();
Bridge bridge = initBridge();
bridgeGame = new BridgeGame(bridge);
- while (!isGameRunning) {
+ while (bridgeGame.isRunning()) {
playOneRound(bridge);
}
- outputView.printResult(isGameEndStatus, bridgeGame.getTryCount());
+ outputView.printResult(bridgeGame.isGameSuccess(), bridgeGame.getTryCount());
+ }
+
+
+ private void playOneRound(Bridge bridge) {
+ checkMove(bridge);
+ bridgeGame.isGameEnd();
+ if (bridgeGame.isGameSuccess()) {
+ return;
+ }
+ playOneRound(bridge);
+ }
+
+
+ private void checkMove(Bridge bridge) {
+ inputMoving();
+ outputView.printMap(bridgeGame.getUserBridge().getUserBridge(), bridge.getBridge());
+ if (!bridgeGame.isRunning()) {
+ retry();
+ }
}
private Bridge initBridge() {
try {
int bridgeSize = inputView.readBridgeSize();
- return new Bridge(new BridgeMaker(new BridgeRandomNumberGenerator()).makeBridge(bridgeSize));
+ BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator());
+ return new Bridge(bridgeMaker.makeBridge(bridgeSize));
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
return initBridge();
}
}
- private boolean inputMoving() {
+ private void inputMoving() {
try {
String moveCommand = inputView.readMoving();
- return bridgeGame.move(moveCommand);
+ bridgeGame.move(moveCommand);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
- return inputMoving();
- }
- }
-
- private void playOneRound(Bridge bridge) {
- checkMove(bridge);
- isGameEndStatus = bridgeGame.isGameEnd();
- if (isGameEndStatus) {
- return;
- }
- playOneRound(bridge);
- }
-
- private void checkMove(Bridge bridge) {
- boolean isMove = inputMoving();
- outputView.printMap(bridgeGame.getUserBridge().getUserBridge(), bridge.getBridge());
- if (!isMove) {
- if (!retry()) {
- isGameEndStatus = false;
- isGameRunning = false;
- return;
- }
+ inputMoving();
}
- isGameRunning = true;
}
- private boolean retry() {
+ private void retry() {
try {
String gameCommand = inputView.readGameCommand();
- return bridgeGame.retry(gameCommand);
+ bridgeGame.retry(gameCommand);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
- return retry();
+ retry();
}
}
| Java | ๋ฉ์๋์ ์ญํ ์ด ์กฐ๊ธ ๋ง์ ๊ฒ ๊ฐ์ต๋๋ค!
์์ง์ ์
๋ ฅ์ ๋ฐ๋ ๋ฉ์๋์์ `bridgeGame.move()` ๊ฐ ์กด์ฌํ๋ ๊ฒ์ ์กฐ๊ธ ์ด์ํฉ๋๋ค! |
@@ -8,71 +8,72 @@
import bridge.view.OutputView;
public class BridgeGameController {
- private final OutputView outputView = new OutputView();
- private final InputView inputView = new InputView();
+ private final OutputView outputView;
+ private final InputView inputView;
private BridgeGame bridgeGame;
- private boolean isGameEndStatus = false;
- private boolean isGameRunning = false;
+
+ public BridgeGameController(OutputView outputView, InputView inputView) {
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
public void play() {
outputView.initGame();
Bridge bridge = initBridge();
bridgeGame = new BridgeGame(bridge);
- while (!isGameRunning) {
+ while (bridgeGame.isRunning()) {
playOneRound(bridge);
}
- outputView.printResult(isGameEndStatus, bridgeGame.getTryCount());
+ outputView.printResult(bridgeGame.isGameSuccess(), bridgeGame.getTryCount());
+ }
+
+
+ private void playOneRound(Bridge bridge) {
+ checkMove(bridge);
+ bridgeGame.isGameEnd();
+ if (bridgeGame.isGameSuccess()) {
+ return;
+ }
+ playOneRound(bridge);
+ }
+
+
+ private void checkMove(Bridge bridge) {
+ inputMoving();
+ outputView.printMap(bridgeGame.getUserBridge().getUserBridge(), bridge.getBridge());
+ if (!bridgeGame.isRunning()) {
+ retry();
+ }
}
private Bridge initBridge() {
try {
int bridgeSize = inputView.readBridgeSize();
- return new Bridge(new BridgeMaker(new BridgeRandomNumberGenerator()).makeBridge(bridgeSize));
+ BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator());
+ return new Bridge(bridgeMaker.makeBridge(bridgeSize));
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
return initBridge();
}
}
- private boolean inputMoving() {
+ private void inputMoving() {
try {
String moveCommand = inputView.readMoving();
- return bridgeGame.move(moveCommand);
+ bridgeGame.move(moveCommand);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
- return inputMoving();
- }
- }
-
- private void playOneRound(Bridge bridge) {
- checkMove(bridge);
- isGameEndStatus = bridgeGame.isGameEnd();
- if (isGameEndStatus) {
- return;
- }
- playOneRound(bridge);
- }
-
- private void checkMove(Bridge bridge) {
- boolean isMove = inputMoving();
- outputView.printMap(bridgeGame.getUserBridge().getUserBridge(), bridge.getBridge());
- if (!isMove) {
- if (!retry()) {
- isGameEndStatus = false;
- isGameRunning = false;
- return;
- }
+ inputMoving();
}
- isGameRunning = true;
}
- private boolean retry() {
+ private void retry() {
try {
String gameCommand = inputView.readGameCommand();
- return bridgeGame.retry(gameCommand);
+ bridgeGame.retry(gameCommand);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
- return retry();
+ retry();
}
}
| Java | ๊ฐ์ธ์ ์ผ๋ก ์ด ๋ถ๋ถ์ ํ๋ฆ ์ฒ๋ฆฌ๊ฐ ์กฐ๊ธ ์์ฝ๋ค๊ณ ์๊ฐํฉ๋๋ค.
์ฐ์ ์๋์ `inputMoving()` ๋ฉ์๋์ ์ญํ ์ด ์กฐ๊ธ ๋ง๊ณ ,
`playOneRount()`๋ฅผ ๋ฐ๋ณตํ๋ ๋ก์ง๋ while ๋ฌธ์ ์ฌ์ฉํ๋ฉด ์ข ๋ ์ง๊ด์ ์ผ๋ก ๋ณด์ด์ง ์์๊น ์๊ฐ๋ฉ๋๋ค!
ํ๋ฒ์ฏค ๊ณ ๋ฏผํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -8,71 +8,72 @@
import bridge.view.OutputView;
public class BridgeGameController {
- private final OutputView outputView = new OutputView();
- private final InputView inputView = new InputView();
+ private final OutputView outputView;
+ private final InputView inputView;
private BridgeGame bridgeGame;
- private boolean isGameEndStatus = false;
- private boolean isGameRunning = false;
+
+ public BridgeGameController(OutputView outputView, InputView inputView) {
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
public void play() {
outputView.initGame();
Bridge bridge = initBridge();
bridgeGame = new BridgeGame(bridge);
- while (!isGameRunning) {
+ while (bridgeGame.isRunning()) {
playOneRound(bridge);
}
- outputView.printResult(isGameEndStatus, bridgeGame.getTryCount());
+ outputView.printResult(bridgeGame.isGameSuccess(), bridgeGame.getTryCount());
+ }
+
+
+ private void playOneRound(Bridge bridge) {
+ checkMove(bridge);
+ bridgeGame.isGameEnd();
+ if (bridgeGame.isGameSuccess()) {
+ return;
+ }
+ playOneRound(bridge);
+ }
+
+
+ private void checkMove(Bridge bridge) {
+ inputMoving();
+ outputView.printMap(bridgeGame.getUserBridge().getUserBridge(), bridge.getBridge());
+ if (!bridgeGame.isRunning()) {
+ retry();
+ }
}
private Bridge initBridge() {
try {
int bridgeSize = inputView.readBridgeSize();
- return new Bridge(new BridgeMaker(new BridgeRandomNumberGenerator()).makeBridge(bridgeSize));
+ BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator());
+ return new Bridge(bridgeMaker.makeBridge(bridgeSize));
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
return initBridge();
}
}
- private boolean inputMoving() {
+ private void inputMoving() {
try {
String moveCommand = inputView.readMoving();
- return bridgeGame.move(moveCommand);
+ bridgeGame.move(moveCommand);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
- return inputMoving();
- }
- }
-
- private void playOneRound(Bridge bridge) {
- checkMove(bridge);
- isGameEndStatus = bridgeGame.isGameEnd();
- if (isGameEndStatus) {
- return;
- }
- playOneRound(bridge);
- }
-
- private void checkMove(Bridge bridge) {
- boolean isMove = inputMoving();
- outputView.printMap(bridgeGame.getUserBridge().getUserBridge(), bridge.getBridge());
- if (!isMove) {
- if (!retry()) {
- isGameEndStatus = false;
- isGameRunning = false;
- return;
- }
+ inputMoving();
}
- isGameRunning = true;
}
- private boolean retry() {
+ private void retry() {
try {
String gameCommand = inputView.readGameCommand();
- return bridgeGame.retry(gameCommand);
+ bridgeGame.retry(gameCommand);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
- return retry();
+ retry();
}
}
| Java | > ๊ฐ์ธ์ ์ผ๋ก ์ด ๋ถ๋ถ์ ํ๋ฆ ์ฒ๋ฆฌ๊ฐ ์กฐ๊ธ ์์ฝ๋ค๊ณ ์๊ฐํฉ๋๋ค. ์ฐ์ ์๋์ `inputMoving()` ๋ฉ์๋์ ์ญํ ์ด ์กฐ๊ธ ๋ง๊ณ , `playOneRount()`๋ฅผ ๋ฐ๋ณตํ๋ ๋ก์ง๋ while ๋ฌธ์ ์ฌ์ฉํ๋ฉด ์ข ๋ ์ง๊ด์ ์ผ๋ก ๋ณด์ด์ง ์์๊น ์๊ฐ๋ฉ๋๋ค! ํ๋ฒ์ฏค ๊ณ ๋ฏผํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์์!
๊ธฐ์กด์ ์ฌ๊ท๋ก ๊ตฌ์ฑํ๋ ์ฝ๋๋ฅผ while๋ก ๋ฐ๊ฟ ์๊ฐ์ ๋ชปํ๋ค์.. ๊ตณ์ด ์กฐ๊ฑด๊ฑธ์ด์ ์ฌ๊ท๋ฅผ ํ๊ธฐ๋ณด๋ค ์๊ฒฌ์ฃผ์ while์ ์ฌ์ฉํ๋๊ฒ ํ์คํ ๋ ์ข์๋ณด์ด๊ธด ํฉ๋๋ค ! ๊ฐ์ฌํฉ๋๋ค |
@@ -8,71 +8,72 @@
import bridge.view.OutputView;
public class BridgeGameController {
- private final OutputView outputView = new OutputView();
- private final InputView inputView = new InputView();
+ private final OutputView outputView;
+ private final InputView inputView;
private BridgeGame bridgeGame;
- private boolean isGameEndStatus = false;
- private boolean isGameRunning = false;
+
+ public BridgeGameController(OutputView outputView, InputView inputView) {
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
public void play() {
outputView.initGame();
Bridge bridge = initBridge();
bridgeGame = new BridgeGame(bridge);
- while (!isGameRunning) {
+ while (bridgeGame.isRunning()) {
playOneRound(bridge);
}
- outputView.printResult(isGameEndStatus, bridgeGame.getTryCount());
+ outputView.printResult(bridgeGame.isGameSuccess(), bridgeGame.getTryCount());
+ }
+
+
+ private void playOneRound(Bridge bridge) {
+ checkMove(bridge);
+ bridgeGame.isGameEnd();
+ if (bridgeGame.isGameSuccess()) {
+ return;
+ }
+ playOneRound(bridge);
+ }
+
+
+ private void checkMove(Bridge bridge) {
+ inputMoving();
+ outputView.printMap(bridgeGame.getUserBridge().getUserBridge(), bridge.getBridge());
+ if (!bridgeGame.isRunning()) {
+ retry();
+ }
}
private Bridge initBridge() {
try {
int bridgeSize = inputView.readBridgeSize();
- return new Bridge(new BridgeMaker(new BridgeRandomNumberGenerator()).makeBridge(bridgeSize));
+ BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator());
+ return new Bridge(bridgeMaker.makeBridge(bridgeSize));
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
return initBridge();
}
}
- private boolean inputMoving() {
+ private void inputMoving() {
try {
String moveCommand = inputView.readMoving();
- return bridgeGame.move(moveCommand);
+ bridgeGame.move(moveCommand);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
- return inputMoving();
- }
- }
-
- private void playOneRound(Bridge bridge) {
- checkMove(bridge);
- isGameEndStatus = bridgeGame.isGameEnd();
- if (isGameEndStatus) {
- return;
- }
- playOneRound(bridge);
- }
-
- private void checkMove(Bridge bridge) {
- boolean isMove = inputMoving();
- outputView.printMap(bridgeGame.getUserBridge().getUserBridge(), bridge.getBridge());
- if (!isMove) {
- if (!retry()) {
- isGameEndStatus = false;
- isGameRunning = false;
- return;
- }
+ inputMoving();
}
- isGameRunning = true;
}
- private boolean retry() {
+ private void retry() {
try {
String gameCommand = inputView.readGameCommand();
- return bridgeGame.retry(gameCommand);
+ bridgeGame.retry(gameCommand);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
- return retry();
+ retry();
}
}
| Java | > ๋ฉ์๋์ ์ญํ ์ด ์กฐ๊ธ ๋ง์ ๊ฒ ๊ฐ์ต๋๋ค! ์์ง์ ์
๋ ฅ์ ๋ฐ๋ ๋ฉ์๋์์ `bridgeGame.move()` ๊ฐ ์กด์ฌํ๋ ๊ฒ์ ์กฐ๊ธ ์ด์ํฉ๋๋ค!
์.. ๊ทธ๋ด๊น์ ? ๋ฉ์๋ ๋ช
์ด ์ด์ํ๊ฑธ๊น์ ๋ฉ์๋ ์ญํ ์ด ๋ฌธ์ ์ผ๊น์ ?
์ ๋ ์ปจํธ๋กค๋ฌ๋ผ๊ณ ์๊ฐํด์ '์
๋ ฅ์ ํตํด์ ์คํํ๋ค' ๊ฐ ํ๋์ ์ญํ ์ ํ๋ค๊ณ ์๊ฐํ๋๋ฐ ๊ทธ๊ฒ ์๋์์๊น์.. ์ ๋ง ์ด๋ ต๊ธดํ๋ค์ |
@@ -8,71 +8,72 @@
import bridge.view.OutputView;
public class BridgeGameController {
- private final OutputView outputView = new OutputView();
- private final InputView inputView = new InputView();
+ private final OutputView outputView;
+ private final InputView inputView;
private BridgeGame bridgeGame;
- private boolean isGameEndStatus = false;
- private boolean isGameRunning = false;
+
+ public BridgeGameController(OutputView outputView, InputView inputView) {
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
public void play() {
outputView.initGame();
Bridge bridge = initBridge();
bridgeGame = new BridgeGame(bridge);
- while (!isGameRunning) {
+ while (bridgeGame.isRunning()) {
playOneRound(bridge);
}
- outputView.printResult(isGameEndStatus, bridgeGame.getTryCount());
+ outputView.printResult(bridgeGame.isGameSuccess(), bridgeGame.getTryCount());
+ }
+
+
+ private void playOneRound(Bridge bridge) {
+ checkMove(bridge);
+ bridgeGame.isGameEnd();
+ if (bridgeGame.isGameSuccess()) {
+ return;
+ }
+ playOneRound(bridge);
+ }
+
+
+ private void checkMove(Bridge bridge) {
+ inputMoving();
+ outputView.printMap(bridgeGame.getUserBridge().getUserBridge(), bridge.getBridge());
+ if (!bridgeGame.isRunning()) {
+ retry();
+ }
}
private Bridge initBridge() {
try {
int bridgeSize = inputView.readBridgeSize();
- return new Bridge(new BridgeMaker(new BridgeRandomNumberGenerator()).makeBridge(bridgeSize));
+ BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator());
+ return new Bridge(bridgeMaker.makeBridge(bridgeSize));
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
return initBridge();
}
}
- private boolean inputMoving() {
+ private void inputMoving() {
try {
String moveCommand = inputView.readMoving();
- return bridgeGame.move(moveCommand);
+ bridgeGame.move(moveCommand);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
- return inputMoving();
- }
- }
-
- private void playOneRound(Bridge bridge) {
- checkMove(bridge);
- isGameEndStatus = bridgeGame.isGameEnd();
- if (isGameEndStatus) {
- return;
- }
- playOneRound(bridge);
- }
-
- private void checkMove(Bridge bridge) {
- boolean isMove = inputMoving();
- outputView.printMap(bridgeGame.getUserBridge().getUserBridge(), bridge.getBridge());
- if (!isMove) {
- if (!retry()) {
- isGameEndStatus = false;
- isGameRunning = false;
- return;
- }
+ inputMoving();
}
- isGameRunning = true;
}
- private boolean retry() {
+ private void retry() {
try {
String gameCommand = inputView.readGameCommand();
- return bridgeGame.retry(gameCommand);
+ bridgeGame.retry(gameCommand);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
- return retry();
+ retry();
}
}
| Java | ๊ทธ๋ ๊ฒ ํ๋๋ก ๋ณผ ์๋ ์๋ค๊ณ ์๊ฐํฉ๋๋ค!
๋ค๋ง ์ ๊ฐ ์๊ฐํ์ ๋๋ ์
๋ ฅ ๋ฐ๊ธฐ -> ์์ง์ธ๋ค ๊น์ง๊ฐ ํ๋์ round ๋ก ๋ฌถ์ด๊ณ ,
isSuccess(๋์ ๋๋ฌํจ) ๋ ๋จ์ด์ง ์ํ๊ฐ ๋์์ ๋ ๋ค์ ํ๋ฆ์ ์ ์ดํ๋ ๋ก์ง์ด ํ๋์ ๋ ํฐ ๋ฉ์๋๋ก
round๋ฅผ ๋ฐ๋ณต์ํค๋ ๊ฒ์ด ์ข๋ค๊ณ ์๊ฐํ์ต๋๋ค!
์ ๋ต์ ์์ผ๋ ํ๋ฒ ๊ณ ๋ฏผํด๋ณด์๊ณ ๋ ์ ์ ํ๋ค๊ณ ๋๋ผ๋ ๊ตฌ์กฐ๋ฅผ ์ก์๊ฐ๋ ๊ฒ์ด ์ค์ํ๋ค๊ณ ์๊ฐํฉ๋๋ค! |
@@ -0,0 +1,47 @@
+package bridge.view;
+
+import java.util.List;
+import java.util.StringJoiner;
+
+public class StringUtil {
+
+ public static String createBridge(List<String> userBridge, List<String> gameBridge) {
+ StringJoiner bridge = new StringJoiner("\n");
+
+ StringJoiner upBridge = new StringJoiner(" | ");
+ StringJoiner downBridge = new StringJoiner(" | ");
+
+ for (int i = 0; i < userBridge.size(); i++) {
+ addBridgeSegment(userBridge.get(i), gameBridge.get(i), upBridge, downBridge);
+ }
+ return bridge.add(String.format("[ %s ]", upBridge)).add(String.format("[ %s ]", downBridge)).toString();
+ }
+
+ private static void addBridgeSegment(String userSegment, String gameSegment, StringJoiner upBridge, StringJoiner downBridge) {
+ if (userSegment.equals(gameSegment)) {
+ addMatchingSegment(userSegment, upBridge, downBridge);
+ } else {
+ addNonMatchingSegment(userSegment, upBridge, downBridge);
+ }
+ }
+
+ private static void addMatchingSegment(String segment, StringJoiner upBridge, StringJoiner downBridge) {
+ if (segment.equals("U")) {
+ upBridge.add("O");
+ downBridge.add(" ");
+ } else if (segment.equals("D")) {
+ upBridge.add(" ");
+ downBridge.add("O");
+ }
+ }
+
+ private static void addNonMatchingSegment(String segment, StringJoiner upBridge, StringJoiner downBridge) {
+ if (segment.equals("U")) {
+ upBridge.add("X");
+ downBridge.add(" ");
+ } else if (segment.equals("D")) {
+ upBridge.add(" ");
+ downBridge.add("X");
+ }
+ }
+} | Java | ๋ฌธ์์ด๋ค์ StringUtil ์ ๊ตฌํํด์ OutputView์ ์ฑ
์์ ๋์ด์ค ๊ฑด ์ข๋ค๊ณ ์๊ฐํฉ๋๋ค!
๋ค๋ง ํ๋ผ๋ฏธํฐ๊ฐ ๋ค์ ๋ง๊ณ , ๋ชจ๋ ๊ฒฐ๊ณผ์ ๋ํ ๊ฒฐ๊ณผ ๋น๊ต๋ฅผ StringUtil์์ ๋ค ํ๊ณ ์๋ ๋ถ๋ถ์ ์กฐ๊ธ ์์ฌ์ด ๊ฒ ๊ฐ์ต๋๋ค
์๋น์ค ๊ณ์ธต์์ ์ด๋์ ๋ ๊ณ์ฐํ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ๊ณตํด์ ๋ฐํํ ์ ์๋๋ก ๊ตฌํํ๋ฉด ์ด๋จ๊น ์ถ์ต๋๋ค |
@@ -0,0 +1,47 @@
+package bridge.view;
+
+import java.util.List;
+import java.util.StringJoiner;
+
+public class StringUtil {
+
+ public static String createBridge(List<String> userBridge, List<String> gameBridge) {
+ StringJoiner bridge = new StringJoiner("\n");
+
+ StringJoiner upBridge = new StringJoiner(" | ");
+ StringJoiner downBridge = new StringJoiner(" | ");
+
+ for (int i = 0; i < userBridge.size(); i++) {
+ addBridgeSegment(userBridge.get(i), gameBridge.get(i), upBridge, downBridge);
+ }
+ return bridge.add(String.format("[ %s ]", upBridge)).add(String.format("[ %s ]", downBridge)).toString();
+ }
+
+ private static void addBridgeSegment(String userSegment, String gameSegment, StringJoiner upBridge, StringJoiner downBridge) {
+ if (userSegment.equals(gameSegment)) {
+ addMatchingSegment(userSegment, upBridge, downBridge);
+ } else {
+ addNonMatchingSegment(userSegment, upBridge, downBridge);
+ }
+ }
+
+ private static void addMatchingSegment(String segment, StringJoiner upBridge, StringJoiner downBridge) {
+ if (segment.equals("U")) {
+ upBridge.add("O");
+ downBridge.add(" ");
+ } else if (segment.equals("D")) {
+ upBridge.add(" ");
+ downBridge.add("O");
+ }
+ }
+
+ private static void addNonMatchingSegment(String segment, StringJoiner upBridge, StringJoiner downBridge) {
+ if (segment.equals("U")) {
+ upBridge.add("X");
+ downBridge.add(" ");
+ } else if (segment.equals("D")) {
+ upBridge.add(" ");
+ downBridge.add("X");
+ }
+ }
+} | Java | ๋ง๋๋ง์ธ๊ฑฐ๊ฐ์ต๋๋ค.. ์ธ์๊ฐ 4๊ฐ๋ ๋๋ค๋ณด๋ ์ ๋ง ๋ง์ ์๋๋ ๋ฉ์๋์๋๋ฐ์ ์ฌ๊ธธ ๋ฆฌํฉํ ๋ง ํ๋ ค๊ณ ํ๋ map์ ์ฌ์ฉํ๊ฑฐ๋ ํด์ผํ๋๋ฐ ๊ณ์ ๋ฆฌํฉํ ๋ง ๋์ ํด๋ดค๋๋ฐ ๊ธฐ์กด ๊ตฌ์กฐ๋ฅผ ๊นจ๊ณ ๋ง๋ค์ด์ผ๋๋ ๋ถ๋ถ์ด ์กด์ฌํด์ ๊ธฐ์กด๊ตฌ์กฐ ์ ์งํ๊ณ ๊ฐ๋๋ผ ์์ฌ์ด ๋ถ๋ถ์ด ๋์จ๊ฒ ๊ฐ์ต๋๋คใ
ใ
|
@@ -0,0 +1,28 @@
+package christmas.util;
+
+public class DateValidator {
+ private static final String INVALID_DATE_MESSAGE = "์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+
+ public static void validateInput(String input) {
+ validateNumeric(input);
+ int date = Integer.parseInt(input);
+ validateDate(date);
+ }
+
+ private static void validateNumeric(String input) {
+ if (input == null) {
+ throw new ValidationException(INVALID_DATE_MESSAGE);
+ }
+ try {
+ Integer.parseInt(input);
+ } catch (NumberFormatException e) {
+ throw new ValidationException(INVALID_DATE_MESSAGE);
+ }
+ }
+
+ private static void validateDate(int date) {
+ if (date < 1 || date > 31) {
+ throw new ValidationException(INVALID_DATE_MESSAGE);
+ }
+ }
+} | Java | 31์ผ๋ก ๋๋์ง ์๋ ๋ฌ๋ ์๊ธฐ๋๋ฌธ์ 12์์ ๋ํ ์์กด์ฑ์ ์ฃผ์
๋ฐ์์ ํด๋นํ๋ ์(ๆ)์ ๋ง์ถฐ ์ต๋ ์ผ์๋ฅผ ๋ณ๊ฒฝ ํ๋์ง ํด๋น ์ด๋ฒคํธ์ ๋ํ ์์๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ ๊ทธ ์ธ ๋ค์ํ ๋ฐฉ์์ผ๋ก ์กฐ๊ฑด์ ์กฐ์ ํ๋ ๊ฒ๋ ์ข์๊ฑฐ๋ผ ์๊ฐํฉ๋๋ค! |
@@ -0,0 +1,103 @@
+package christmas.util;
+
+import christmas.domain.type.Menu;
+
+import java.util.*;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+public class OrderValidator {
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+
+ private static final int MIN_AMOUNT = 1;
+ private static final int MAX_AMOUNT = 20;
+ private static final String INPUT_PATTERN = "([\\S]+-\\d+)(,\\s*[\\S]+-\\d+)*";
+ private static final String INVALID_ORDER_MESSAGE = "์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String BEVERAGE_ONLY_MESSAGE = "์๋ฃ๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.";
+ private static final String INVALID_ORDER_AMOUNT_MESSAGE = "๋ฉ๋ด๋ ํ ๋ฒ์ 1๊ฐ์ด์, ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.";
+ private static final String MENU_NOT_EXIST_MESSAGE = "์กด์ฌํ์ง ์๋ ๋ฉ๋ด์
๋๋ค.";
+ private static final String DUPLICATED_MENU_ORDER_MESSAGE = "์ค๋ณต๋ ๋ฉ๋ด ํญ๋ชฉ์ด ์์ต๋๋ค.";
+
+ public static void validateInput(String input) {
+ checkInputPattern(input);
+ checkInputPatternMore(input);
+ List<String> menuNames = parseMenuName(input);
+ checkDuplicated(menuNames);
+
+ for (String menuName : menuNames) {
+ checkMenu(menuName);
+ }
+ }
+
+ public static void validateOrder(Map<Menu, Integer> order) {
+ checkAmount(order);
+ checkBeverageOnly(order);
+ }
+
+ private static void checkBeverageOnly(Map<Menu, Integer> order) {
+ if (order.keySet()
+ .stream()
+ .allMatch(menu -> menu.getCategory() == Menu.Category.BEVERAGE)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + BEVERAGE_ONLY_MESSAGE);
+ }
+ }
+
+ private static void checkAmount(Map<Menu, Integer> order) {
+ int totalAmount = order.values()
+ .stream()
+ .mapToInt(Integer::intValue).sum();
+
+ if (totalAmount < MIN_AMOUNT || totalAmount > MAX_AMOUNT) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + INVALID_ORDER_AMOUNT_MESSAGE);
+ }
+
+ for (Integer amount : order.values()) {
+ if (amount < MIN_AMOUNT) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + INVALID_ORDER_AMOUNT_MESSAGE);
+ }
+ }
+ }
+
+ private static void checkMenu(String menuName) {
+ boolean menuExists = Arrays.stream(Menu.values()).anyMatch(menu -> menu.getName().equals(menuName));
+
+ if (!menuExists) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + MENU_NOT_EXIST_MESSAGE);
+ }
+ }
+ private static void checkDuplicated(List<String> menuNames) {
+ Set<String> uniqueMenuNames = new HashSet<>();
+
+ for (String name : menuNames) {
+ if (!uniqueMenuNames.add(name)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + DUPLICATED_MENU_ORDER_MESSAGE);
+ }
+ }
+ }
+
+ private static void checkInputPattern(String input) {
+ if (input == null || !Pattern.matches(INPUT_PATTERN, input)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE);
+ }
+ }
+
+ private static List<String> parseMenuName(String input) {
+ List<String> menuNames = Arrays.stream(input.split(","))
+ .map(order -> order.trim().split("-")[0].trim())
+ .collect(Collectors.toList());
+
+ return menuNames;
+ }
+
+ private static void checkInputPatternMore(String input) {
+ String[] items = input.split(",");
+
+ for (String item : items) {
+ int count = StringUtil.countMatches(item, '-');
+ if (count != 1) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE);
+ }
+ }
+ }
+
+} | Java | ์ ๊ท์์ ์ฌ์ฉํ ๋ ๋ฏธ๋ฆฌ ์บ์ฑํ๋ ๋ฐฉ๋ฒ์ด ์๋ค๊ณ ํฉ๋๋ค!
https://mntdev.tistory.com/69#%F0%9F%91%8D%20%EC%A0%95%EC%A0%81%20%ED%8C%A9%ED%84%B0%EB%A6%AC%EB%A5%BC%20%EC%A0%9C%EA%B3%B5%ED%95%98%EB%8A%94%20%EB%B6%88%EB%B3%80%20%ED%81%B4%EB%9E%98%EC%8A%A4-1
์ด ๊ธ์ ํ๋ฒ ์ฐธ๊ณ ํด๋ณด์๋ฉด ์ข์ ๊ฒ๊ฐ์์! ๐ |
@@ -0,0 +1,103 @@
+package christmas.util;
+
+import christmas.domain.type.Menu;
+
+import java.util.*;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+public class OrderValidator {
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+
+ private static final int MIN_AMOUNT = 1;
+ private static final int MAX_AMOUNT = 20;
+ private static final String INPUT_PATTERN = "([\\S]+-\\d+)(,\\s*[\\S]+-\\d+)*";
+ private static final String INVALID_ORDER_MESSAGE = "์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String BEVERAGE_ONLY_MESSAGE = "์๋ฃ๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.";
+ private static final String INVALID_ORDER_AMOUNT_MESSAGE = "๋ฉ๋ด๋ ํ ๋ฒ์ 1๊ฐ์ด์, ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.";
+ private static final String MENU_NOT_EXIST_MESSAGE = "์กด์ฌํ์ง ์๋ ๋ฉ๋ด์
๋๋ค.";
+ private static final String DUPLICATED_MENU_ORDER_MESSAGE = "์ค๋ณต๋ ๋ฉ๋ด ํญ๋ชฉ์ด ์์ต๋๋ค.";
+
+ public static void validateInput(String input) {
+ checkInputPattern(input);
+ checkInputPatternMore(input);
+ List<String> menuNames = parseMenuName(input);
+ checkDuplicated(menuNames);
+
+ for (String menuName : menuNames) {
+ checkMenu(menuName);
+ }
+ }
+
+ public static void validateOrder(Map<Menu, Integer> order) {
+ checkAmount(order);
+ checkBeverageOnly(order);
+ }
+
+ private static void checkBeverageOnly(Map<Menu, Integer> order) {
+ if (order.keySet()
+ .stream()
+ .allMatch(menu -> menu.getCategory() == Menu.Category.BEVERAGE)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + BEVERAGE_ONLY_MESSAGE);
+ }
+ }
+
+ private static void checkAmount(Map<Menu, Integer> order) {
+ int totalAmount = order.values()
+ .stream()
+ .mapToInt(Integer::intValue).sum();
+
+ if (totalAmount < MIN_AMOUNT || totalAmount > MAX_AMOUNT) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + INVALID_ORDER_AMOUNT_MESSAGE);
+ }
+
+ for (Integer amount : order.values()) {
+ if (amount < MIN_AMOUNT) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + INVALID_ORDER_AMOUNT_MESSAGE);
+ }
+ }
+ }
+
+ private static void checkMenu(String menuName) {
+ boolean menuExists = Arrays.stream(Menu.values()).anyMatch(menu -> menu.getName().equals(menuName));
+
+ if (!menuExists) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + MENU_NOT_EXIST_MESSAGE);
+ }
+ }
+ private static void checkDuplicated(List<String> menuNames) {
+ Set<String> uniqueMenuNames = new HashSet<>();
+
+ for (String name : menuNames) {
+ if (!uniqueMenuNames.add(name)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + DUPLICATED_MENU_ORDER_MESSAGE);
+ }
+ }
+ }
+
+ private static void checkInputPattern(String input) {
+ if (input == null || !Pattern.matches(INPUT_PATTERN, input)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE);
+ }
+ }
+
+ private static List<String> parseMenuName(String input) {
+ List<String> menuNames = Arrays.stream(input.split(","))
+ .map(order -> order.trim().split("-")[0].trim())
+ .collect(Collectors.toList());
+
+ return menuNames;
+ }
+
+ private static void checkInputPatternMore(String input) {
+ String[] items = input.split(",");
+
+ for (String item : items) {
+ int count = StringUtil.countMatches(item, '-');
+ if (count != 1) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE);
+ }
+ }
+ }
+
+} | Java | "," ๋ "-"๋ ์์๋ก ๊ด๋ฆฌํ๋ฉด ์ข์ ๊ฒ ๊ฐ์์! ์ด๋ฒคํธ์ ๋ฐ๋ผ ์
๋ ฅ๋ฐ๋ ๋ฐฉ์์ด ๋ฐ๋ ์๋ ์์์์ ๐ |
@@ -0,0 +1,76 @@
+package christmas.domain.logic;
+
+import christmas.domain.type.Badge;
+import christmas.domain.type.Menu;
+import org.junit.jupiter.api.Test;
+
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class EventCalculatorTest {
+
+ @Test
+ void isEligibleForEvent() {
+ assertTrue(EventCalculator.isEligibleForEvent(15000));
+ assertFalse(EventCalculator.isEligibleForEvent(5000));
+ }
+
+ @Test
+ void calculateWeekdayDiscount_์ฃผ์ค_๋์ ํธ() {
+ Map<Menu, Integer> order = Map.of(Menu.CHOCOLATE_CAKE, 1);
+ int discount = EventCalculator.calculateWeekdayDiscount(3, order);
+ assertEquals(2023, discount);
+ }
+
+ @Test
+ void calculateWeekdayDiscount_์ฃผ์ค_๋์ ํธ์์() {
+ Map<Menu, Integer> order = Map.of(Menu.CAESAR_SALAD, 1, Menu.TAPAS, 1);
+ int discount = EventCalculator.calculateWeekdayDiscount(3, order);
+ assertEquals(0, discount);
+ }
+
+ @Test
+ void calculateWeekendDiscount_์ฃผ๋ง_๋ฉ์ธ() {
+ Map<Menu, Integer> order = Map.of(Menu.T_BONE_STEAK, 2, Menu.TAPAS, 1);
+ int discount = EventCalculator.calculateWeekendDiscount(4, order);
+ assertEquals(2023 * 2, discount);
+ }
+
+ @Test
+ void calculateWeekendDiscount_์ฃผ๋ง_๋ฉ์ธ์์() {
+ Map<Menu, Integer> order = Map.of(Menu.ICE_CREAM, 1);
+ int discount = EventCalculator.calculateWeekendDiscount(4, order);
+ assertEquals(0, discount);
+ }
+
+ @Test
+ void calculateSpecialDiscount_๋ณ๋ฐ์ด() {
+ int discount = EventCalculator.calculateSpecialDiscount(25);
+ assertEquals(1000, discount);
+ }
+
+ @Test
+ void calculateSpecialDiscount_๋ณ๋ฐ์ด_์๋() {
+ int discount = EventCalculator.calculateSpecialDiscount(26);
+ assertEquals(0, discount);
+ }
+
+ @Test
+ void calculateGiveawayEvent() {
+ Map<Menu, Integer> giveaway = EventCalculator.calculateGiveawayEvent(120000);
+ assertNotNull(giveaway);
+ assertTrue(giveaway.containsKey(Menu.CHAMPAGNE));
+ assertEquals(1, giveaway.get(Menu.CHAMPAGNE));
+ }
+
+ @Test
+ void calculateBadge() {
+ assertEquals(Badge.SANTA, EventCalculator.calculateBadge(20000));
+ assertEquals(Badge.SANTA, EventCalculator.calculateBadge(30000));
+ assertEquals(Badge.TREE, EventCalculator.calculateBadge(15000));
+ assertEquals(Badge.TREE, EventCalculator.calculateBadge(19999));
+ assertEquals(Badge.STAR, EventCalculator.calculateBadge(7000));
+ assertEquals(Badge.NONE, EventCalculator.calculateBadge(4000));
+ }
+} | Java | ํด๋ฆฐ์ฝ๋์ ๋์ค๋ ๋ด์ฉ์ธ๋ฐ ํ ํ
์คํธ ๋ฉ์๋ ์์ ์ฌ๋ฌ assert๋ฌธ์ ์ฌ์ฉํ๋ ๊ฒ์ ์ข์ง ์๋ค๊ณ ํด์! ํด๋น ๋ฉ์๋์์@ParameterizedTest ์ @MethodSource๋ฅผ ์ฌ์ฉํด๋ณด๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,20 @@
+package christmas.domain.logic;
+
+import christmas.domain.model.DecemberEvent;
+
+public class ChristmasEventCalculator {
+ public static int calculateChristmasDiscount(int date) {
+ int startDay = DecemberEvent.getEventStartDate();
+ int discountPricePerDay = DecemberEvent.getDayDiscountPrice();
+ int discountPriceOfFirstDay = DecemberEvent.getStartDiscountPrice();
+
+ if (date > DecemberEvent.CHRISTMAS_DAY) {
+ return 0;
+ }
+
+ int daysUntilDate = date - startDay;
+ int totalDiscount = discountPriceOfFirstDay + (daysUntilDate * discountPricePerDay);
+
+ return totalDiscount;
+ }
+} | Java | ์ ํธ๋ฆฌํฐ ํด๋์ค๋ก ๋ณด์ด๋๋ฐ ์์ฑ์๋ฅผ private๋ก ๋ง๋๋๊ฒ ์ข์๋ณด์ฌ์! ๐ |
@@ -0,0 +1,71 @@
+package christmas.domain.model;
+
+import christmas.domain.type.Menu;
+
+import java.util.*;
+
+public class DecemberEvent {
+ public static final int CHRISTMAS_DAY = 25;
+ private static final int EVENT_START_DATE = 1;
+ private static final int DAY_DISCOUNT_PRICE = 100;
+ private static final int START_DISCOUNT_PRICE = 1_000;
+ private static final int MIN_PRICE = 10_000;
+ private static final int WEEKDAY_DISCOUNT_PRICE = 2_023;
+ private static final int WEEKEND_DISCOUNT_PRICE = 2_023;
+ private static final Menu.Category WEEKDAY_DISCOUNT_CATEGORY = Menu.Category.DESSERT;
+ private static final Menu.Category WEEKEND_DISCOUNT_CATEGORY = Menu.Category.MAIN;
+ private static final List<Integer> WEEKEND_DATES = Arrays.asList(1, 2, 8, 9, 15, 16, 22, 23, 29, 30);
+ private static final List<Integer> STAR_DATES = Arrays.asList(3, 10, 17, 24, 25, 31);
+ private static final int SPECIAL_DISCOUNT_PRICE = 1_000;
+ private static final int GIVEAWAY_CONDITION_PRICE = 120_000;
+ private static final Map<Menu, Integer> GIVEAWAY_BENEFITS = Map.of(Menu.CHAMPAGNE, 1);
+
+
+ public static boolean isOverThanMinPrice(int totalPrice) {
+ return totalPrice >= MIN_PRICE;
+ }
+
+ public static boolean isStarDate(int date) {
+ return STAR_DATES.contains(date);
+ }
+
+ public static boolean isWeekend(int date) {
+ return WEEKEND_DATES.contains(date);
+ }
+
+ public static boolean isEligibleForGiveaway(int totalPrice) {
+ return totalPrice >= GIVEAWAY_CONDITION_PRICE;
+ }
+
+ public static int getSpecialDiscountPrice() {
+ return SPECIAL_DISCOUNT_PRICE;
+ }
+
+ public static int getWeekdayDiscountPrice() { return WEEKDAY_DISCOUNT_PRICE; }
+
+ public static int getWeekendDiscountPrice() { return WEEKEND_DISCOUNT_PRICE; }
+
+ public static Menu.Category getWeekdayDiscountCategory() {
+ return WEEKDAY_DISCOUNT_CATEGORY;
+ }
+
+ public static Menu.Category getWeekendDiscountCategory() {
+ return WEEKEND_DISCOUNT_CATEGORY;
+ }
+
+ public static Map<Menu, Integer> getGiveawayBenefits() {
+ return GIVEAWAY_BENEFITS;
+ }
+
+ public static int getDayDiscountPrice() {
+ return DAY_DISCOUNT_PRICE;
+ }
+
+ public static int getStartDiscountPrice() {
+ return START_DISCOUNT_PRICE;
+ }
+
+ public static int getEventStartDate() {
+ return EVENT_START_DATE;
+ }
+} | Java | ์์๋ฅผ ๊ด๋ฆฌํ๋ ํด๋์ค๋ฅผ ๋ค๋ฅธ ํจํค์ง๋ก ๊ตฌ๋ถํ๋ ๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,103 @@
+package christmas.util;
+
+import christmas.domain.type.Menu;
+
+import java.util.*;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+public class OrderValidator {
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+
+ private static final int MIN_AMOUNT = 1;
+ private static final int MAX_AMOUNT = 20;
+ private static final String INPUT_PATTERN = "([\\S]+-\\d+)(,\\s*[\\S]+-\\d+)*";
+ private static final String INVALID_ORDER_MESSAGE = "์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String BEVERAGE_ONLY_MESSAGE = "์๋ฃ๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.";
+ private static final String INVALID_ORDER_AMOUNT_MESSAGE = "๋ฉ๋ด๋ ํ ๋ฒ์ 1๊ฐ์ด์, ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.";
+ private static final String MENU_NOT_EXIST_MESSAGE = "์กด์ฌํ์ง ์๋ ๋ฉ๋ด์
๋๋ค.";
+ private static final String DUPLICATED_MENU_ORDER_MESSAGE = "์ค๋ณต๋ ๋ฉ๋ด ํญ๋ชฉ์ด ์์ต๋๋ค.";
+
+ public static void validateInput(String input) {
+ checkInputPattern(input);
+ checkInputPatternMore(input);
+ List<String> menuNames = parseMenuName(input);
+ checkDuplicated(menuNames);
+
+ for (String menuName : menuNames) {
+ checkMenu(menuName);
+ }
+ }
+
+ public static void validateOrder(Map<Menu, Integer> order) {
+ checkAmount(order);
+ checkBeverageOnly(order);
+ }
+
+ private static void checkBeverageOnly(Map<Menu, Integer> order) {
+ if (order.keySet()
+ .stream()
+ .allMatch(menu -> menu.getCategory() == Menu.Category.BEVERAGE)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + BEVERAGE_ONLY_MESSAGE);
+ }
+ }
+
+ private static void checkAmount(Map<Menu, Integer> order) {
+ int totalAmount = order.values()
+ .stream()
+ .mapToInt(Integer::intValue).sum();
+
+ if (totalAmount < MIN_AMOUNT || totalAmount > MAX_AMOUNT) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + INVALID_ORDER_AMOUNT_MESSAGE);
+ }
+
+ for (Integer amount : order.values()) {
+ if (amount < MIN_AMOUNT) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + INVALID_ORDER_AMOUNT_MESSAGE);
+ }
+ }
+ }
+
+ private static void checkMenu(String menuName) {
+ boolean menuExists = Arrays.stream(Menu.values()).anyMatch(menu -> menu.getName().equals(menuName));
+
+ if (!menuExists) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + MENU_NOT_EXIST_MESSAGE);
+ }
+ }
+ private static void checkDuplicated(List<String> menuNames) {
+ Set<String> uniqueMenuNames = new HashSet<>();
+
+ for (String name : menuNames) {
+ if (!uniqueMenuNames.add(name)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + DUPLICATED_MENU_ORDER_MESSAGE);
+ }
+ }
+ }
+
+ private static void checkInputPattern(String input) {
+ if (input == null || !Pattern.matches(INPUT_PATTERN, input)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE);
+ }
+ }
+
+ private static List<String> parseMenuName(String input) {
+ List<String> menuNames = Arrays.stream(input.split(","))
+ .map(order -> order.trim().split("-")[0].trim())
+ .collect(Collectors.toList());
+
+ return menuNames;
+ }
+
+ private static void checkInputPatternMore(String input) {
+ String[] items = input.split(",");
+
+ for (String item : items) {
+ int count = StringUtil.countMatches(item, '-');
+ if (count != 1) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE);
+ }
+ }
+ }
+
+} | Java | getter๋ฅผ ์ฌ์ฉํ๋ ๋์ menu๊ฐ์ฒด๊ฐ ์ง์ ์นดํ
๊ณ ๋ฆฌ๋ฅผ ๋น๊ตํ๊ฒ ํด๋ณด๋๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,61 @@
+package christmas.view;
+
+import camp.nextstep.edu.missionutils.Console;
+import christmas.domain.type.Menu;
+import christmas.util.DateValidator;
+import christmas.util.OrderValidator;
+import christmas.util.ValidationException;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class InputView {
+ private static final String ASK_DATE_MESSAGE = "12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)";
+ private static final String ASK_ORDER_MESSAGE = "์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)";
+
+ public int readDate() {
+ while (true) {
+ try {
+ System.out.println(ASK_DATE_MESSAGE);
+ String input = Console.readLine();
+ DateValidator.validateInput(input);
+ int date = Integer.parseInt(input);
+ return date;
+ } catch (ValidationException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+ }
+
+ public Map<Menu, Integer> readOrder() {
+ while (true) {
+ try {
+ System.out.println(ASK_ORDER_MESSAGE);
+ String input = Console.readLine();
+ OrderValidator.validateInput(input);
+ Map<Menu, Integer> order = parseOrder(input);
+ OrderValidator.validateOrder(order);
+
+ return order;
+ } catch (ValidationException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+ }
+
+ private Map<Menu, Integer> parseOrder(String input) {
+ Map<Menu, Integer> order = new HashMap<>();
+
+ String[] items = input.split(",");
+ for (String item : items) {
+ String[] parts = item.trim()
+ .split("-");
+ String menuName = parts[0].trim();
+ int amount = Integer.parseInt(parts[1].trim());
+
+ Menu menu = Menu.findByName(menuName);
+ order.put(menu, amount);
+ }
+ return order;
+ }
+} | Java | ๋ฐ๋ณต๋์ง๋ง ๋ฏธ์ธํ๊ฒ ๋ค๋ฅธ ์ฝ๋๋ค์! ์ด๋ป๊ฒ ๋ฐ๋ณต์ ์ค์ผ์ง ๊ณ ๋ฏผํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์์! ๐ |
@@ -0,0 +1,103 @@
+package christmas.util;
+
+import christmas.domain.type.Menu;
+
+import java.util.*;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+public class OrderValidator {
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+
+ private static final int MIN_AMOUNT = 1;
+ private static final int MAX_AMOUNT = 20;
+ private static final String INPUT_PATTERN = "([\\S]+-\\d+)(,\\s*[\\S]+-\\d+)*";
+ private static final String INVALID_ORDER_MESSAGE = "์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String BEVERAGE_ONLY_MESSAGE = "์๋ฃ๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.";
+ private static final String INVALID_ORDER_AMOUNT_MESSAGE = "๋ฉ๋ด๋ ํ ๋ฒ์ 1๊ฐ์ด์, ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.";
+ private static final String MENU_NOT_EXIST_MESSAGE = "์กด์ฌํ์ง ์๋ ๋ฉ๋ด์
๋๋ค.";
+ private static final String DUPLICATED_MENU_ORDER_MESSAGE = "์ค๋ณต๋ ๋ฉ๋ด ํญ๋ชฉ์ด ์์ต๋๋ค.";
+
+ public static void validateInput(String input) {
+ checkInputPattern(input);
+ checkInputPatternMore(input);
+ List<String> menuNames = parseMenuName(input);
+ checkDuplicated(menuNames);
+
+ for (String menuName : menuNames) {
+ checkMenu(menuName);
+ }
+ }
+
+ public static void validateOrder(Map<Menu, Integer> order) {
+ checkAmount(order);
+ checkBeverageOnly(order);
+ }
+
+ private static void checkBeverageOnly(Map<Menu, Integer> order) {
+ if (order.keySet()
+ .stream()
+ .allMatch(menu -> menu.getCategory() == Menu.Category.BEVERAGE)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + BEVERAGE_ONLY_MESSAGE);
+ }
+ }
+
+ private static void checkAmount(Map<Menu, Integer> order) {
+ int totalAmount = order.values()
+ .stream()
+ .mapToInt(Integer::intValue).sum();
+
+ if (totalAmount < MIN_AMOUNT || totalAmount > MAX_AMOUNT) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + INVALID_ORDER_AMOUNT_MESSAGE);
+ }
+
+ for (Integer amount : order.values()) {
+ if (amount < MIN_AMOUNT) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + INVALID_ORDER_AMOUNT_MESSAGE);
+ }
+ }
+ }
+
+ private static void checkMenu(String menuName) {
+ boolean menuExists = Arrays.stream(Menu.values()).anyMatch(menu -> menu.getName().equals(menuName));
+
+ if (!menuExists) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + MENU_NOT_EXIST_MESSAGE);
+ }
+ }
+ private static void checkDuplicated(List<String> menuNames) {
+ Set<String> uniqueMenuNames = new HashSet<>();
+
+ for (String name : menuNames) {
+ if (!uniqueMenuNames.add(name)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + DUPLICATED_MENU_ORDER_MESSAGE);
+ }
+ }
+ }
+
+ private static void checkInputPattern(String input) {
+ if (input == null || !Pattern.matches(INPUT_PATTERN, input)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE);
+ }
+ }
+
+ private static List<String> parseMenuName(String input) {
+ List<String> menuNames = Arrays.stream(input.split(","))
+ .map(order -> order.trim().split("-")[0].trim())
+ .collect(Collectors.toList());
+
+ return menuNames;
+ }
+
+ private static void checkInputPatternMore(String input) {
+ String[] items = input.split(",");
+
+ for (String item : items) {
+ int count = StringUtil.countMatches(item, '-');
+ if (count != 1) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE);
+ }
+ }
+ }
+
+} | Java | throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + ******);
์ด๋ถ๋ถ์ด ๋ฐ๋ณต๋๋ค์! ์ ๋ ๋ฉ์๋๋ ์์๋ก ๋ฐ๋ณต์ ์ค์ผ ์ ์๋ค๊ณ ์๊ฐํ๋๋ฐ ์ด๋ค๊ฐ์? ๐ |
@@ -0,0 +1,28 @@
+package christmas.util;
+
+public class DateValidator {
+ private static final String INVALID_DATE_MESSAGE = "์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+
+ public static void validateInput(String input) {
+ validateNumeric(input);
+ int date = Integer.parseInt(input);
+ validateDate(date);
+ }
+
+ private static void validateNumeric(String input) {
+ if (input == null) {
+ throw new ValidationException(INVALID_DATE_MESSAGE);
+ }
+ try {
+ Integer.parseInt(input);
+ } catch (NumberFormatException e) {
+ throw new ValidationException(INVALID_DATE_MESSAGE);
+ }
+ }
+
+ private static void validateDate(int date) {
+ if (date < 1 || date > 31) {
+ throw new ValidationException(INVALID_DATE_MESSAGE);
+ }
+ }
+} | Java | ์ ๋ง๋ค์ ์ ๊ฐ ์ถ๊ตฌํ๋ ๋ฐฉํฅ์ด `12์ ์ด๋ฒคํธ` ํด๋์ค๋ง ๋งค๋ฌ ํด๋นํ๋ ๋ฌ์ ๊ฐ์ ๋ผ์ฐ๋ ๊ฑฐ์๊ฑฐ๋ ์ `๋ช์.LAST_DATE` ์ด๋ฐ์์ผ๋ก ๋ฐ๊ฟ์ผ ๊ฒ ์ต๋๋ค. |
@@ -0,0 +1,76 @@
+package christmas.domain.logic;
+
+import christmas.domain.type.Badge;
+import christmas.domain.type.Menu;
+import org.junit.jupiter.api.Test;
+
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class EventCalculatorTest {
+
+ @Test
+ void isEligibleForEvent() {
+ assertTrue(EventCalculator.isEligibleForEvent(15000));
+ assertFalse(EventCalculator.isEligibleForEvent(5000));
+ }
+
+ @Test
+ void calculateWeekdayDiscount_์ฃผ์ค_๋์ ํธ() {
+ Map<Menu, Integer> order = Map.of(Menu.CHOCOLATE_CAKE, 1);
+ int discount = EventCalculator.calculateWeekdayDiscount(3, order);
+ assertEquals(2023, discount);
+ }
+
+ @Test
+ void calculateWeekdayDiscount_์ฃผ์ค_๋์ ํธ์์() {
+ Map<Menu, Integer> order = Map.of(Menu.CAESAR_SALAD, 1, Menu.TAPAS, 1);
+ int discount = EventCalculator.calculateWeekdayDiscount(3, order);
+ assertEquals(0, discount);
+ }
+
+ @Test
+ void calculateWeekendDiscount_์ฃผ๋ง_๋ฉ์ธ() {
+ Map<Menu, Integer> order = Map.of(Menu.T_BONE_STEAK, 2, Menu.TAPAS, 1);
+ int discount = EventCalculator.calculateWeekendDiscount(4, order);
+ assertEquals(2023 * 2, discount);
+ }
+
+ @Test
+ void calculateWeekendDiscount_์ฃผ๋ง_๋ฉ์ธ์์() {
+ Map<Menu, Integer> order = Map.of(Menu.ICE_CREAM, 1);
+ int discount = EventCalculator.calculateWeekendDiscount(4, order);
+ assertEquals(0, discount);
+ }
+
+ @Test
+ void calculateSpecialDiscount_๋ณ๋ฐ์ด() {
+ int discount = EventCalculator.calculateSpecialDiscount(25);
+ assertEquals(1000, discount);
+ }
+
+ @Test
+ void calculateSpecialDiscount_๋ณ๋ฐ์ด_์๋() {
+ int discount = EventCalculator.calculateSpecialDiscount(26);
+ assertEquals(0, discount);
+ }
+
+ @Test
+ void calculateGiveawayEvent() {
+ Map<Menu, Integer> giveaway = EventCalculator.calculateGiveawayEvent(120000);
+ assertNotNull(giveaway);
+ assertTrue(giveaway.containsKey(Menu.CHAMPAGNE));
+ assertEquals(1, giveaway.get(Menu.CHAMPAGNE));
+ }
+
+ @Test
+ void calculateBadge() {
+ assertEquals(Badge.SANTA, EventCalculator.calculateBadge(20000));
+ assertEquals(Badge.SANTA, EventCalculator.calculateBadge(30000));
+ assertEquals(Badge.TREE, EventCalculator.calculateBadge(15000));
+ assertEquals(Badge.TREE, EventCalculator.calculateBadge(19999));
+ assertEquals(Badge.STAR, EventCalculator.calculateBadge(7000));
+ assertEquals(Badge.NONE, EventCalculator.calculateBadge(4000));
+ }
+} | Java | ์ค... ์์ง ํ
์คํธ์ฝ๋๊ฐ ์ต์์น์์๋๋ฐ ํ์ฉํด ๋ณด๊ฒ ์ต๋๋ค. |
@@ -0,0 +1,20 @@
+package christmas.domain.logic;
+
+import christmas.domain.model.DecemberEvent;
+
+public class ChristmasEventCalculator {
+ public static int calculateChristmasDiscount(int date) {
+ int startDay = DecemberEvent.getEventStartDate();
+ int discountPricePerDay = DecemberEvent.getDayDiscountPrice();
+ int discountPriceOfFirstDay = DecemberEvent.getStartDiscountPrice();
+
+ if (date > DecemberEvent.CHRISTMAS_DAY) {
+ return 0;
+ }
+
+ int daysUntilDate = date - startDay;
+ int totalDiscount = discountPriceOfFirstDay + (daysUntilDate * discountPricePerDay);
+
+ return totalDiscount;
+ }
+} | Java | ์ํ ๊ตณ์ด ๊ฐ์ฒด๊ฐ ์์ฑ๋ ํ์๊ฐ ์๊ตฐ์ |
@@ -0,0 +1,71 @@
+package christmas.domain.model;
+
+import christmas.domain.type.Menu;
+
+import java.util.*;
+
+public class DecemberEvent {
+ public static final int CHRISTMAS_DAY = 25;
+ private static final int EVENT_START_DATE = 1;
+ private static final int DAY_DISCOUNT_PRICE = 100;
+ private static final int START_DISCOUNT_PRICE = 1_000;
+ private static final int MIN_PRICE = 10_000;
+ private static final int WEEKDAY_DISCOUNT_PRICE = 2_023;
+ private static final int WEEKEND_DISCOUNT_PRICE = 2_023;
+ private static final Menu.Category WEEKDAY_DISCOUNT_CATEGORY = Menu.Category.DESSERT;
+ private static final Menu.Category WEEKEND_DISCOUNT_CATEGORY = Menu.Category.MAIN;
+ private static final List<Integer> WEEKEND_DATES = Arrays.asList(1, 2, 8, 9, 15, 16, 22, 23, 29, 30);
+ private static final List<Integer> STAR_DATES = Arrays.asList(3, 10, 17, 24, 25, 31);
+ private static final int SPECIAL_DISCOUNT_PRICE = 1_000;
+ private static final int GIVEAWAY_CONDITION_PRICE = 120_000;
+ private static final Map<Menu, Integer> GIVEAWAY_BENEFITS = Map.of(Menu.CHAMPAGNE, 1);
+
+
+ public static boolean isOverThanMinPrice(int totalPrice) {
+ return totalPrice >= MIN_PRICE;
+ }
+
+ public static boolean isStarDate(int date) {
+ return STAR_DATES.contains(date);
+ }
+
+ public static boolean isWeekend(int date) {
+ return WEEKEND_DATES.contains(date);
+ }
+
+ public static boolean isEligibleForGiveaway(int totalPrice) {
+ return totalPrice >= GIVEAWAY_CONDITION_PRICE;
+ }
+
+ public static int getSpecialDiscountPrice() {
+ return SPECIAL_DISCOUNT_PRICE;
+ }
+
+ public static int getWeekdayDiscountPrice() { return WEEKDAY_DISCOUNT_PRICE; }
+
+ public static int getWeekendDiscountPrice() { return WEEKEND_DISCOUNT_PRICE; }
+
+ public static Menu.Category getWeekdayDiscountCategory() {
+ return WEEKDAY_DISCOUNT_CATEGORY;
+ }
+
+ public static Menu.Category getWeekendDiscountCategory() {
+ return WEEKEND_DISCOUNT_CATEGORY;
+ }
+
+ public static Map<Menu, Integer> getGiveawayBenefits() {
+ return GIVEAWAY_BENEFITS;
+ }
+
+ public static int getDayDiscountPrice() {
+ return DAY_DISCOUNT_PRICE;
+ }
+
+ public static int getStartDiscountPrice() {
+ return START_DISCOUNT_PRICE;
+ }
+
+ public static int getEventStartDate() {
+ return EVENT_START_DATE;
+ }
+} | Java | 12์ ์ด๋ฒคํธ์๋ ํ ์ธ ๊ธ์ก์ด๋ ๊ธฐ๊ฐ๋ฑ์ด ์์ด์ ๋น์ฆ๋์ค ๋ก์ง๊ณผ ๋ฐ์ ํ๋ค๊ณ ์๊ฐํด์ ์ ๊ธฐ์ ์์นํ๊ธด ํ์ต๋๋ค. |
@@ -0,0 +1,103 @@
+package christmas.util;
+
+import christmas.domain.type.Menu;
+
+import java.util.*;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+public class OrderValidator {
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+
+ private static final int MIN_AMOUNT = 1;
+ private static final int MAX_AMOUNT = 20;
+ private static final String INPUT_PATTERN = "([\\S]+-\\d+)(,\\s*[\\S]+-\\d+)*";
+ private static final String INVALID_ORDER_MESSAGE = "์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String BEVERAGE_ONLY_MESSAGE = "์๋ฃ๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.";
+ private static final String INVALID_ORDER_AMOUNT_MESSAGE = "๋ฉ๋ด๋ ํ ๋ฒ์ 1๊ฐ์ด์, ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.";
+ private static final String MENU_NOT_EXIST_MESSAGE = "์กด์ฌํ์ง ์๋ ๋ฉ๋ด์
๋๋ค.";
+ private static final String DUPLICATED_MENU_ORDER_MESSAGE = "์ค๋ณต๋ ๋ฉ๋ด ํญ๋ชฉ์ด ์์ต๋๋ค.";
+
+ public static void validateInput(String input) {
+ checkInputPattern(input);
+ checkInputPatternMore(input);
+ List<String> menuNames = parseMenuName(input);
+ checkDuplicated(menuNames);
+
+ for (String menuName : menuNames) {
+ checkMenu(menuName);
+ }
+ }
+
+ public static void validateOrder(Map<Menu, Integer> order) {
+ checkAmount(order);
+ checkBeverageOnly(order);
+ }
+
+ private static void checkBeverageOnly(Map<Menu, Integer> order) {
+ if (order.keySet()
+ .stream()
+ .allMatch(menu -> menu.getCategory() == Menu.Category.BEVERAGE)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + BEVERAGE_ONLY_MESSAGE);
+ }
+ }
+
+ private static void checkAmount(Map<Menu, Integer> order) {
+ int totalAmount = order.values()
+ .stream()
+ .mapToInt(Integer::intValue).sum();
+
+ if (totalAmount < MIN_AMOUNT || totalAmount > MAX_AMOUNT) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + INVALID_ORDER_AMOUNT_MESSAGE);
+ }
+
+ for (Integer amount : order.values()) {
+ if (amount < MIN_AMOUNT) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + INVALID_ORDER_AMOUNT_MESSAGE);
+ }
+ }
+ }
+
+ private static void checkMenu(String menuName) {
+ boolean menuExists = Arrays.stream(Menu.values()).anyMatch(menu -> menu.getName().equals(menuName));
+
+ if (!menuExists) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + MENU_NOT_EXIST_MESSAGE);
+ }
+ }
+ private static void checkDuplicated(List<String> menuNames) {
+ Set<String> uniqueMenuNames = new HashSet<>();
+
+ for (String name : menuNames) {
+ if (!uniqueMenuNames.add(name)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + DUPLICATED_MENU_ORDER_MESSAGE);
+ }
+ }
+ }
+
+ private static void checkInputPattern(String input) {
+ if (input == null || !Pattern.matches(INPUT_PATTERN, input)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE);
+ }
+ }
+
+ private static List<String> parseMenuName(String input) {
+ List<String> menuNames = Arrays.stream(input.split(","))
+ .map(order -> order.trim().split("-")[0].trim())
+ .collect(Collectors.toList());
+
+ return menuNames;
+ }
+
+ private static void checkInputPatternMore(String input) {
+ String[] items = input.split(",");
+
+ for (String item : items) {
+ int count = StringUtil.countMatches(item, '-');
+ if (count != 1) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE);
+ }
+ }
+ }
+
+} | Java | ํด๋น ๊ท์น์ ๋ํ ์ฑ
์์ ๋ฉ๋ด๋ณด๋ค๋ ์ฃผ๋ฌธ๊ฒ์ฆ์ ์๋ค๊ณ ์๊ฐํด์ ์ ๋ ๊ฒ ๊ตฌํํ๊ธด ํ๋๋ฐ ๋ฉ๋ด๊ฐ ์ค์ค๋ก ํ๋จ ํ ์ ์๋๊ฒ์ ์ข๋ ๊ตฌ๋ถํด๋์ผ๋ฉด ๋ ์ข์์ง๊ฒ ๋ค์! |
@@ -0,0 +1,41 @@
+package christmas.domain.model;
+
+import christmas.domain.type.Menu;
+
+import java.util.Map;
+
+public class OrderInfo {
+ private final int date;
+ private final Map<Menu, Integer> order;
+ private final int totalOrderPrice;
+
+ public OrderInfo(int date, Map<Menu, Integer> order) {
+ this.date = date;
+ this.order = order;
+ this.totalOrderPrice = calculateTotalOrderPrice();
+ }
+
+ public int getDate() {
+ return date;
+ }
+ public Map<Menu, Integer> getOrder() {
+ return order;
+ }
+
+ public int getTotalOrderPrice() {
+ return totalOrderPrice;
+ }
+
+ private int calculateTotalOrderPrice() {
+ int totalOrderPrice = 0;
+
+ for (Map.Entry<Menu, Integer> entry : order.entrySet()) {
+ Menu menu = entry.getKey();
+ int quantity = entry.getValue();
+
+ totalOrderPrice += menu.getPrice() * quantity;
+ }
+
+ return totalOrderPrice;
+ }
+} | Java | calculateTotalOrderPrice๋ก ํญ์ ๊ฐ์ ๊ตฌํ ์ ์์ผ๋ totalOrderPrice๋ฅผ ๋ฉค๋ฒ๋ณ์๋ก ๊ฐ๋ ๊ฒ ๋ณด๋ค ์ธ๋ถ์์ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๋ฐฉ์์ ์ด๋ ์ ๊ฐ์? |
@@ -0,0 +1,58 @@
+package christmas.domain.type;
+
+public enum Menu {
+ //์ํผํ์ด์
+ MUSHROOM_SOUP("์์ก์ด์ํ", 6_000, Category.APPETIZER),
+ TAPAS("ํํ์ค", 5_500, Category.APPETIZER),
+ CAESAR_SALAD("์์ ์๋ฌ๋", 8_000, Category.APPETIZER),
+
+ //๋ฉ์ธ
+ T_BONE_STEAK("ํฐ๋ณธ์คํ
์ดํฌ", 55_000, Category.MAIN),
+ BBQ_RIBS("๋ฐ๋นํ๋ฆฝ", 54_000, Category.MAIN),
+ SEAFOOD_PASTA("ํด์ฐ๋ฌผํ์คํ", 35_000, Category.MAIN),
+ CHRISTMAS_PASTA("ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25_000, Category.MAIN),
+
+ //๋์ ํธ
+ CHOCOLATE_CAKE("์ด์ฝ์ผ์ดํฌ", 15_000, Category.DESSERT),
+ ICE_CREAM("์์ด์คํฌ๋ฆผ", 5_000, Category.DESSERT),
+
+ //์๋ฃ
+ ZERO_COKE("์ ๋ก์ฝ๋ผ", 3_000, Category.BEVERAGE),
+ RED_WINE("๋ ๋์์ธ", 60_000, Category.BEVERAGE),
+ CHAMPAGNE("์ดํ์ธ", 25_000, Category.BEVERAGE);
+
+ private final String name;
+ private final int price;
+ private final Category category;
+
+ Menu(String name, int price, Category category) {
+ this.name = name;
+ this.price = price;
+ this.category = category;
+ }
+
+ public static Menu findByName(String menuName) {
+ for (Menu menu : Menu.values()) {
+ if (menu.getName().equals(menuName)) {
+ return menu;
+ }
+ }
+ return null;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public Category getCategory() {
+ return category;
+ }
+
+ public enum Category {
+ APPETIZER, MAIN, DESSERT, BEVERAGE
+ }
+} | Java | ์๋ฐ ์คํธ๋ฆผ api ๋ฅผ ์ฌ์ฉํ์๋ฉด ์ธ๋ดํธ๋ฅผ ์ค์ด๊ณ ๋ช
ํ์ฑ์ ๋์ด์ค ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,58 @@
+package christmas.domain.type;
+
+public enum Menu {
+ //์ํผํ์ด์
+ MUSHROOM_SOUP("์์ก์ด์ํ", 6_000, Category.APPETIZER),
+ TAPAS("ํํ์ค", 5_500, Category.APPETIZER),
+ CAESAR_SALAD("์์ ์๋ฌ๋", 8_000, Category.APPETIZER),
+
+ //๋ฉ์ธ
+ T_BONE_STEAK("ํฐ๋ณธ์คํ
์ดํฌ", 55_000, Category.MAIN),
+ BBQ_RIBS("๋ฐ๋นํ๋ฆฝ", 54_000, Category.MAIN),
+ SEAFOOD_PASTA("ํด์ฐ๋ฌผํ์คํ", 35_000, Category.MAIN),
+ CHRISTMAS_PASTA("ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25_000, Category.MAIN),
+
+ //๋์ ํธ
+ CHOCOLATE_CAKE("์ด์ฝ์ผ์ดํฌ", 15_000, Category.DESSERT),
+ ICE_CREAM("์์ด์คํฌ๋ฆผ", 5_000, Category.DESSERT),
+
+ //์๋ฃ
+ ZERO_COKE("์ ๋ก์ฝ๋ผ", 3_000, Category.BEVERAGE),
+ RED_WINE("๋ ๋์์ธ", 60_000, Category.BEVERAGE),
+ CHAMPAGNE("์ดํ์ธ", 25_000, Category.BEVERAGE);
+
+ private final String name;
+ private final int price;
+ private final Category category;
+
+ Menu(String name, int price, Category category) {
+ this.name = name;
+ this.price = price;
+ this.category = category;
+ }
+
+ public static Menu findByName(String menuName) {
+ for (Menu menu : Menu.values()) {
+ if (menu.getName().equals(menuName)) {
+ return menu;
+ }
+ }
+ return null;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public Category getCategory() {
+ return category;
+ }
+
+ public enum Category {
+ APPETIZER, MAIN, DESSERT, BEVERAGE
+ }
+} | Java | InputView์์๋ ๋น์ทํ ์ฝ๋๋ก ๋ฉ๋ด ์ด๋ฆ์ ๊ฒ์ฆํ์๋๋ฐ null ์ ๋ฐํํ๋ ๋์ ์ฌ๊ธฐ์ ๋ฐ๋ก ์์ธ๋ฅผ ๋์ง์๋ ๊ฑด ์ด๋ ์ ๊ฐ์? |
@@ -0,0 +1,70 @@
+package christmas.domain.logic;
+
+import christmas.domain.model.DecemberEvent;
+import christmas.domain.type.Badge;
+import christmas.domain.type.Menu;
+
+import java.util.Collections;
+import java.util.Map;
+
+public class EventCalculator {
+ public static boolean isEligibleForEvent(int totalPrice) {
+ return DecemberEvent.isOverThanMinPrice(totalPrice);
+ }
+
+ public static int calculateWeekdayDiscount(int date, Map<Menu, Integer> order) {
+ int totalDiscount = 0;
+ Menu.Category category = DecemberEvent.getWeekdayDiscountCategory();
+ int discountPrice = DecemberEvent.getWeekdayDiscountPrice();
+
+ for (Map.Entry<Menu, Integer> entry : order.entrySet()) {
+ Menu menu = entry.getKey();
+ int quantity = entry.getValue();
+
+ totalDiscount += calculateDiscountByCategory(menu, quantity, category, discountPrice);
+ }
+
+ return totalDiscount;
+ }
+
+ public static int calculateWeekendDiscount(int date, Map<Menu, Integer> order) {
+ int totalDiscount = 0;
+ Menu.Category category = DecemberEvent.getWeekendDiscountCategory();
+ int discountPrice = DecemberEvent.getWeekendDiscountPrice();
+
+ for (Map.Entry<Menu, Integer> entry : order.entrySet()) {
+ Menu menu = entry.getKey();
+ int quantity = entry.getValue();
+
+ totalDiscount += calculateDiscountByCategory(menu, quantity, category, discountPrice);
+ }
+
+ return totalDiscount;
+ }
+
+ public static int calculateSpecialDiscount(int date) {
+ if (DecemberEvent.isStarDate(date)) {
+ return DecemberEvent.getSpecialDiscountPrice();
+ }
+ return 0;
+ }
+
+ public static Map<Menu, Integer> calculateGiveawayEvent(int totalPrice) {
+ if (DecemberEvent.isEligibleForGiveaway(totalPrice)) {
+ return DecemberEvent.getGiveawayBenefits();
+ }
+ return Collections.emptyMap();
+ }
+
+ public static Badge calculateBadge(int totalDiscount) {
+ Badge badge = Badge.findByTotalDiscount(totalDiscount);
+ return badge;
+ }
+
+ private static int calculateDiscountByCategory(Menu menu, int quantity, Menu.Category category, int discountPrice) {
+ if (menu.getCategory().equals(category)) {
+ return quantity * discountPrice;
+ }
+ return 0;
+ }
+} | Java | ์ด ๋ฉ์๋์์ date๋ ์ฌ์ฉํ์ง ์๋๋ฐ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ผ์๋ ์ด์ ๊ฐ ์์ผ์ค๊น์? |
@@ -0,0 +1,70 @@
+package christmas.domain.logic;
+
+import christmas.domain.model.DecemberEvent;
+import christmas.domain.type.Badge;
+import christmas.domain.type.Menu;
+
+import java.util.Collections;
+import java.util.Map;
+
+public class EventCalculator {
+ public static boolean isEligibleForEvent(int totalPrice) {
+ return DecemberEvent.isOverThanMinPrice(totalPrice);
+ }
+
+ public static int calculateWeekdayDiscount(int date, Map<Menu, Integer> order) {
+ int totalDiscount = 0;
+ Menu.Category category = DecemberEvent.getWeekdayDiscountCategory();
+ int discountPrice = DecemberEvent.getWeekdayDiscountPrice();
+
+ for (Map.Entry<Menu, Integer> entry : order.entrySet()) {
+ Menu menu = entry.getKey();
+ int quantity = entry.getValue();
+
+ totalDiscount += calculateDiscountByCategory(menu, quantity, category, discountPrice);
+ }
+
+ return totalDiscount;
+ }
+
+ public static int calculateWeekendDiscount(int date, Map<Menu, Integer> order) {
+ int totalDiscount = 0;
+ Menu.Category category = DecemberEvent.getWeekendDiscountCategory();
+ int discountPrice = DecemberEvent.getWeekendDiscountPrice();
+
+ for (Map.Entry<Menu, Integer> entry : order.entrySet()) {
+ Menu menu = entry.getKey();
+ int quantity = entry.getValue();
+
+ totalDiscount += calculateDiscountByCategory(menu, quantity, category, discountPrice);
+ }
+
+ return totalDiscount;
+ }
+
+ public static int calculateSpecialDiscount(int date) {
+ if (DecemberEvent.isStarDate(date)) {
+ return DecemberEvent.getSpecialDiscountPrice();
+ }
+ return 0;
+ }
+
+ public static Map<Menu, Integer> calculateGiveawayEvent(int totalPrice) {
+ if (DecemberEvent.isEligibleForGiveaway(totalPrice)) {
+ return DecemberEvent.getGiveawayBenefits();
+ }
+ return Collections.emptyMap();
+ }
+
+ public static Badge calculateBadge(int totalDiscount) {
+ Badge badge = Badge.findByTotalDiscount(totalDiscount);
+ return badge;
+ }
+
+ private static int calculateDiscountByCategory(Menu menu, int quantity, Menu.Category category, int discountPrice) {
+ if (menu.getCategory().equals(category)) {
+ return quantity * discountPrice;
+ }
+ return 0;
+ }
+} | Java | Map<Menu, Integer> order ๋ก ์นดํ
์ฝ๋ฆฌ๋ณ ๋ฉ๋ด ๊ฐ์๋ฅผ ์
์ ์์ผ๋ ์ด๋ฅผ OrderInfo ๋ชจ๋ธ์ ์ญํ ๋ก ๋๊ธฐ์๋ ๊ฑด ์ด๋ ์ค๊น์? |
@@ -0,0 +1,61 @@
+package christmas.domain.model;
+
+import christmas.domain.type.Badge;
+import christmas.domain.type.Benefit;
+
+import java.util.Map;
+
+public class EventResult {
+ private final OrderInfo orderInfo;
+ private final Map<Benefit, Integer> benefitHistory;
+ private final int totalBenefitsPrice;
+ private final int finalPrice;
+ private final Badge badge;
+
+ public EventResult(OrderInfo orderInfo, Map<Benefit, Integer> benefitHistory, int totalBenefitsPrice, Badge badge) {
+ this.orderInfo = orderInfo;
+ this.benefitHistory = benefitHistory;
+ this.totalBenefitsPrice = totalBenefitsPrice;
+ this.finalPrice = calculateFinalPrice();
+ this.badge = badge;
+ }
+
+ // getter
+ public OrderInfo getOrder() {
+ return orderInfo;
+ }
+
+ public Map<Benefit, Integer> getBenefitHistory() {
+ return benefitHistory;
+ }
+
+ public int getTotalBenefitsPrice() {
+ return totalBenefitsPrice;
+ }
+
+ public int getFinalPrice() {
+ return finalPrice;
+ }
+
+ public Badge getBadge() {
+ return badge;
+ }
+
+ public boolean hasGiveawayBenefits() {
+ if (benefitHistory.containsKey(Benefit.GIVEAWAY)) {
+ return true;
+ }
+ return false;
+ }
+
+ //private ํฌํผ
+ private int calculateFinalPrice() {
+ int finalPrice = orderInfo.getTotalOrderPrice() - totalBenefitsPrice;
+
+ if (benefitHistory.containsKey(Benefit.GIVEAWAY)) {
+ finalPrice += benefitHistory.get(Benefit.GIVEAWAY);
+ }
+
+ return finalPrice;
+ }
+} | Java | EventResult๋ dto๋ก ์ฌ์ฉํ์ ๊ฑธ๊น์? |
@@ -0,0 +1,103 @@
+package christmas.util;
+
+import christmas.domain.type.Menu;
+
+import java.util.*;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+public class OrderValidator {
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+
+ private static final int MIN_AMOUNT = 1;
+ private static final int MAX_AMOUNT = 20;
+ private static final String INPUT_PATTERN = "([\\S]+-\\d+)(,\\s*[\\S]+-\\d+)*";
+ private static final String INVALID_ORDER_MESSAGE = "์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String BEVERAGE_ONLY_MESSAGE = "์๋ฃ๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.";
+ private static final String INVALID_ORDER_AMOUNT_MESSAGE = "๋ฉ๋ด๋ ํ ๋ฒ์ 1๊ฐ์ด์, ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.";
+ private static final String MENU_NOT_EXIST_MESSAGE = "์กด์ฌํ์ง ์๋ ๋ฉ๋ด์
๋๋ค.";
+ private static final String DUPLICATED_MENU_ORDER_MESSAGE = "์ค๋ณต๋ ๋ฉ๋ด ํญ๋ชฉ์ด ์์ต๋๋ค.";
+
+ public static void validateInput(String input) {
+ checkInputPattern(input);
+ checkInputPatternMore(input);
+ List<String> menuNames = parseMenuName(input);
+ checkDuplicated(menuNames);
+
+ for (String menuName : menuNames) {
+ checkMenu(menuName);
+ }
+ }
+
+ public static void validateOrder(Map<Menu, Integer> order) {
+ checkAmount(order);
+ checkBeverageOnly(order);
+ }
+
+ private static void checkBeverageOnly(Map<Menu, Integer> order) {
+ if (order.keySet()
+ .stream()
+ .allMatch(menu -> menu.getCategory() == Menu.Category.BEVERAGE)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + BEVERAGE_ONLY_MESSAGE);
+ }
+ }
+
+ private static void checkAmount(Map<Menu, Integer> order) {
+ int totalAmount = order.values()
+ .stream()
+ .mapToInt(Integer::intValue).sum();
+
+ if (totalAmount < MIN_AMOUNT || totalAmount > MAX_AMOUNT) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + INVALID_ORDER_AMOUNT_MESSAGE);
+ }
+
+ for (Integer amount : order.values()) {
+ if (amount < MIN_AMOUNT) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + INVALID_ORDER_AMOUNT_MESSAGE);
+ }
+ }
+ }
+
+ private static void checkMenu(String menuName) {
+ boolean menuExists = Arrays.stream(Menu.values()).anyMatch(menu -> menu.getName().equals(menuName));
+
+ if (!menuExists) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + MENU_NOT_EXIST_MESSAGE);
+ }
+ }
+ private static void checkDuplicated(List<String> menuNames) {
+ Set<String> uniqueMenuNames = new HashSet<>();
+
+ for (String name : menuNames) {
+ if (!uniqueMenuNames.add(name)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE + LINE_SEPARATOR + DUPLICATED_MENU_ORDER_MESSAGE);
+ }
+ }
+ }
+
+ private static void checkInputPattern(String input) {
+ if (input == null || !Pattern.matches(INPUT_PATTERN, input)) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE);
+ }
+ }
+
+ private static List<String> parseMenuName(String input) {
+ List<String> menuNames = Arrays.stream(input.split(","))
+ .map(order -> order.trim().split("-")[0].trim())
+ .collect(Collectors.toList());
+
+ return menuNames;
+ }
+
+ private static void checkInputPatternMore(String input) {
+ String[] items = input.split(",");
+
+ for (String item : items) {
+ int count = StringUtil.countMatches(item, '-');
+ if (count != 1) {
+ throw new ValidationException(INVALID_ORDER_MESSAGE);
+ }
+ }
+ }
+
+} | Java | More ๋ณด๋ค ๋ฉ์๋ ์๋๋ฅผ ๋ํ๋ด๋ ๋ฉ์๋๋ช
์ ์ฌ์ฉํ์๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,41 @@
+package christmas.domain.model;
+
+import christmas.domain.type.Menu;
+
+import java.util.Map;
+
+public class OrderInfo {
+ private final int date;
+ private final Map<Menu, Integer> order;
+ private final int totalOrderPrice;
+
+ public OrderInfo(int date, Map<Menu, Integer> order) {
+ this.date = date;
+ this.order = order;
+ this.totalOrderPrice = calculateTotalOrderPrice();
+ }
+
+ public int getDate() {
+ return date;
+ }
+ public Map<Menu, Integer> getOrder() {
+ return order;
+ }
+
+ public int getTotalOrderPrice() {
+ return totalOrderPrice;
+ }
+
+ private int calculateTotalOrderPrice() {
+ int totalOrderPrice = 0;
+
+ for (Map.Entry<Menu, Integer> entry : order.entrySet()) {
+ Menu menu = entry.getKey();
+ int quantity = entry.getValue();
+
+ totalOrderPrice += menu.getPrice() * quantity;
+ }
+
+ return totalOrderPrice;
+ }
+} | Java | ์์ ์ด ๊ฐ์ง ๊ฐ์ผ๋ก ํ๋ฒ ์ ํด์ง๊ณ ๋ณํ์ง ์๋ ๊ฐ์ด์ด์ ์ด๋ ๊ฒ ๊ตฌํํ์ต๋๋ค!
orderInfo์ ๊ฐ์ ๋ค๋ฅธ ๊ณณ์์ ํ์ฉ๋ฉ๋๋ค |
@@ -0,0 +1,58 @@
+package christmas.domain.type;
+
+public enum Menu {
+ //์ํผํ์ด์
+ MUSHROOM_SOUP("์์ก์ด์ํ", 6_000, Category.APPETIZER),
+ TAPAS("ํํ์ค", 5_500, Category.APPETIZER),
+ CAESAR_SALAD("์์ ์๋ฌ๋", 8_000, Category.APPETIZER),
+
+ //๋ฉ์ธ
+ T_BONE_STEAK("ํฐ๋ณธ์คํ
์ดํฌ", 55_000, Category.MAIN),
+ BBQ_RIBS("๋ฐ๋นํ๋ฆฝ", 54_000, Category.MAIN),
+ SEAFOOD_PASTA("ํด์ฐ๋ฌผํ์คํ", 35_000, Category.MAIN),
+ CHRISTMAS_PASTA("ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25_000, Category.MAIN),
+
+ //๋์ ํธ
+ CHOCOLATE_CAKE("์ด์ฝ์ผ์ดํฌ", 15_000, Category.DESSERT),
+ ICE_CREAM("์์ด์คํฌ๋ฆผ", 5_000, Category.DESSERT),
+
+ //์๋ฃ
+ ZERO_COKE("์ ๋ก์ฝ๋ผ", 3_000, Category.BEVERAGE),
+ RED_WINE("๋ ๋์์ธ", 60_000, Category.BEVERAGE),
+ CHAMPAGNE("์ดํ์ธ", 25_000, Category.BEVERAGE);
+
+ private final String name;
+ private final int price;
+ private final Category category;
+
+ Menu(String name, int price, Category category) {
+ this.name = name;
+ this.price = price;
+ this.category = category;
+ }
+
+ public static Menu findByName(String menuName) {
+ for (Menu menu : Menu.values()) {
+ if (menu.getName().equals(menuName)) {
+ return menu;
+ }
+ }
+ return null;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public Category getCategory() {
+ return category;
+ }
+
+ public enum Category {
+ APPETIZER, MAIN, DESSERT, BEVERAGE
+ }
+} | Java | ๋ค์ฌ๋ ์ฝ๋์์ ํ์ ๋ฐฐ์ ์ต๋๋ค! |
@@ -0,0 +1,58 @@
+package christmas.domain.type;
+
+public enum Menu {
+ //์ํผํ์ด์
+ MUSHROOM_SOUP("์์ก์ด์ํ", 6_000, Category.APPETIZER),
+ TAPAS("ํํ์ค", 5_500, Category.APPETIZER),
+ CAESAR_SALAD("์์ ์๋ฌ๋", 8_000, Category.APPETIZER),
+
+ //๋ฉ์ธ
+ T_BONE_STEAK("ํฐ๋ณธ์คํ
์ดํฌ", 55_000, Category.MAIN),
+ BBQ_RIBS("๋ฐ๋นํ๋ฆฝ", 54_000, Category.MAIN),
+ SEAFOOD_PASTA("ํด์ฐ๋ฌผํ์คํ", 35_000, Category.MAIN),
+ CHRISTMAS_PASTA("ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25_000, Category.MAIN),
+
+ //๋์ ํธ
+ CHOCOLATE_CAKE("์ด์ฝ์ผ์ดํฌ", 15_000, Category.DESSERT),
+ ICE_CREAM("์์ด์คํฌ๋ฆผ", 5_000, Category.DESSERT),
+
+ //์๋ฃ
+ ZERO_COKE("์ ๋ก์ฝ๋ผ", 3_000, Category.BEVERAGE),
+ RED_WINE("๋ ๋์์ธ", 60_000, Category.BEVERAGE),
+ CHAMPAGNE("์ดํ์ธ", 25_000, Category.BEVERAGE);
+
+ private final String name;
+ private final int price;
+ private final Category category;
+
+ Menu(String name, int price, Category category) {
+ this.name = name;
+ this.price = price;
+ this.category = category;
+ }
+
+ public static Menu findByName(String menuName) {
+ for (Menu menu : Menu.values()) {
+ if (menu.getName().equals(menuName)) {
+ return menu;
+ }
+ }
+ return null;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public Category getCategory() {
+ return category;
+ }
+
+ public enum Category {
+ APPETIZER, MAIN, DESSERT, BEVERAGE
+ }
+} | Java | ์ฑ
์์ ๋ถ๋ฆฌํ๋ ค๊ณ ๊ทธ๋ ๊ฒ ์์ฑํ์ต๋๋ค.
๋ฐ๋ก ๋ฉ๋ด์์ ์์ธ๋ฅผ ๋์ง๋ฉด ์ถ์ ํ๊ธฐ ์ฌ์ธ์ ์๊ฒ ๋ค์ |
@@ -0,0 +1,41 @@
+package christmas.domain.model;
+
+import christmas.domain.type.Menu;
+
+import java.util.Map;
+
+public class OrderInfo {
+ private final int date;
+ private final Map<Menu, Integer> order;
+ private final int totalOrderPrice;
+
+ public OrderInfo(int date, Map<Menu, Integer> order) {
+ this.date = date;
+ this.order = order;
+ this.totalOrderPrice = calculateTotalOrderPrice();
+ }
+
+ public int getDate() {
+ return date;
+ }
+ public Map<Menu, Integer> getOrder() {
+ return order;
+ }
+
+ public int getTotalOrderPrice() {
+ return totalOrderPrice;
+ }
+
+ private int calculateTotalOrderPrice() {
+ int totalOrderPrice = 0;
+
+ for (Map.Entry<Menu, Integer> entry : order.entrySet()) {
+ Menu menu = entry.getKey();
+ int quantity = entry.getValue();
+
+ totalOrderPrice += menu.getPrice() * quantity;
+ }
+
+ return totalOrderPrice;
+ }
+} | Java | date์ ๋ํ validation์ InputView์์ ์งํํ์
จ๊ตฐ์!
์ ๋ ์ด ๋ถ๋ถ์ ๋ํด์ ์ด๋ค ๊ฒ์ด ๋ ํจ์จ์ ์ผ๊น ๊ณ ๋ฏผ์ ๋ง์ด ํด๋ดค๋๋ฐ @T3rryAhn ๋์ ์๊ฐ์ ์ด๋ค ์ง ์ฌ์ญ๊ณ ์ถ์ต๋๋ค.
์ ๋ ์ฒ์์๋ ์
๋ ฅ๊ฐ์ ๋ํ validation์ InputView์์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์ข์์ง domain์์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์ข์ ์ง ๊ณ ๋ฏผ์ ๋ง์ด ํ์๋๋ฐ์. ํ๋ก๊ทธ๋จ์ด ๋ณต์กํด์ง ์๋ก InputView๊ฐ ๊ฒ์ฆํด์ผํ validation์ ์ข
๋ฅ๊ฐ ๋ง์์ง์ผ๋ก ์ ๋ validation์ domain์ผ๋ก ์ฑ
์์ ๋๊ธฐ๋ ๊ฒ์ด ๋ ์ข๋ค๊ณ ํ๋จํ์ต๋๋ค.
์๋ฅผ ๋ค์ด ์ ๋ InputView์์ validation์ ์ต์ํ์ ์
๋ ฅ ์ค๋ฅ(Ex. `NumberFormatException`, ๋น ์
๋ ฅ๊ฐ, ๊ธ์์ ์ ํ ๋ฑ)๋ง ๊ฒ์ฆํ๊ณ , ์
๋ ฅ์ด domain ๊ด๋ จ ๊ฐ์ผ๋ก ์ ํจํ ์ง ํ๋จํ๋ ๊ฑด, ๋ชจ๋ธ ๊ฐ์ฒด ์ค์ค๋ก๊ฐ ์์ ์ ๊ฐ์ ๊ฒ์ฆํ๋๋ก ๊ตฌ์กฐ๋ฅผ ์ธ์ ์ต๋๋ค.
@T3rryAhn ๋์ ์๊ฐ์ ์ด๋ ์ ๊ฐ์?
์ฐธ๊ณ : https://tecoble.techcourse.co.kr/post/2020-05-29-wrap-primitive-type/ |
@@ -0,0 +1,41 @@
+package christmas.domain.model;
+
+import christmas.domain.type.Menu;
+
+import java.util.Map;
+
+public class OrderInfo {
+ private final int date;
+ private final Map<Menu, Integer> order;
+ private final int totalOrderPrice;
+
+ public OrderInfo(int date, Map<Menu, Integer> order) {
+ this.date = date;
+ this.order = order;
+ this.totalOrderPrice = calculateTotalOrderPrice();
+ }
+
+ public int getDate() {
+ return date;
+ }
+ public Map<Menu, Integer> getOrder() {
+ return order;
+ }
+
+ public int getTotalOrderPrice() {
+ return totalOrderPrice;
+ }
+
+ private int calculateTotalOrderPrice() {
+ int totalOrderPrice = 0;
+
+ for (Map.Entry<Menu, Integer> entry : order.entrySet()) {
+ Menu menu = entry.getKey();
+ int quantity = entry.getValue();
+
+ totalOrderPrice += menu.getPrice() * quantity;
+ }
+
+ return totalOrderPrice;
+ }
+} | Java | Map<Menu, Integer>์ ๊ฒฝ์ฐ ์ปฌ๋ ์
์ ํด๋นํฉ๋๋ค.
์ด ๊ฒฝ์ฐ ์ผ๊ธ ์ปฌ๋ ์
์ ์ฌ์ฉํด์ ๊ด๋ฆฌํ๋ ๊ฑด ์ด๋จ๊น์?
์ฐธ๊ณ : https://tecoble.techcourse.co.kr/post/2020-05-08-First-Class-Collection/ |
@@ -0,0 +1,41 @@
+package christmas.domain.model;
+
+import christmas.domain.type.Menu;
+
+import java.util.Map;
+
+public class OrderInfo {
+ private final int date;
+ private final Map<Menu, Integer> order;
+ private final int totalOrderPrice;
+
+ public OrderInfo(int date, Map<Menu, Integer> order) {
+ this.date = date;
+ this.order = order;
+ this.totalOrderPrice = calculateTotalOrderPrice();
+ }
+
+ public int getDate() {
+ return date;
+ }
+ public Map<Menu, Integer> getOrder() {
+ return order;
+ }
+
+ public int getTotalOrderPrice() {
+ return totalOrderPrice;
+ }
+
+ private int calculateTotalOrderPrice() {
+ int totalOrderPrice = 0;
+
+ for (Map.Entry<Menu, Integer> entry : order.entrySet()) {
+ Menu menu = entry.getKey();
+ int quantity = entry.getValue();
+
+ totalOrderPrice += menu.getPrice() * quantity;
+ }
+
+ return totalOrderPrice;
+ }
+} | Java | 3์ฃผ์ฐจ ๊ณตํต ํผ๋๋ฐฑ์์ '**๊ฐ์ฒด๋ ๊ฐ์ฒด๋ต๊ฒ **'๋ผ๋ ๊ฒ์ด ์์์ต๋๋ค.
์ด ๊ฒฝ์ฐ `private final Map<Menu, Integer> order;`์ผ๋ก ์ ์ธํ order์ ๋ฐํํ๋๋ฐ์.
Map<Menu, Integer>์ ์ปฌ๋ ์
์ค์ ํ๋์
๋๋ค.
๋ฐ๋ผ์ final ํค์๋๋ฅผ ์ฌ์ฉํ๋ค๊ณ ํด๋ ๊ฐ์ ๋ณ๊ฒฝ์ด ๋ฐ์ํ ์ ์์ต๋๋ค ใ
ใ
กใ
.
๋ฐ๋ผ์ ์ปฌ๋ ์
๋ณ์๋ ์ต๋ํ getter ์ฌ์ฉ์ ์ง์ํ๊ณ ๊ฐ์ฒด์ ๋ฉ์์ง๋ฅผ ๋์ง๋ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํ๋ฉด ์ข์๋ฐ์.
์์ comment์ ์ฐ๊ดํ์ฌ '์ผ๊ธ์ปฌ๋ ์
' ๊ฐ๋
์ด ๋ง์ด ๋์ ๋์ค ๊ฒ ๊ฐ์ต๋๋ค!
์ฐธ๊ณ : https://tecoble.techcourse.co.kr/post/2020-05-08-First-Class-Collection/ |
@@ -0,0 +1,112 @@
+package christmas.view;
+
+import christmas.domain.model.DecemberEvent;
+import christmas.domain.model.EventResult;
+import christmas.domain.type.Badge;
+import christmas.domain.type.Benefit;
+import christmas.domain.type.Menu;
+import christmas.util.MapToStringConverter;
+
+import java.util.Map;
+
+public class OutputView {
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+ private static final String INTRO_MESSAGE = "์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น 12์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค.";
+ private static final String RESULT_INTRO_MESSAGE = "12์ %d์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!";
+ private static final String EVENT_CAUTION = "์ด๋ฒคํธ ์ฃผ์ ์ฌํญ!" + LINE_SEPARATOR +
+ " - ์ด์ฃผ๋ฌธ ๊ธ์ก 10,000์ ์ด์๋ถํฐ ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋ฉ๋๋ค." + LINE_SEPARATOR +
+ " - ์๋ฃ๋ง ์ฃผ๋ฌธ ์, ์ฃผ๋ฌธํ ์ ์์ต๋๋ค." + LINE_SEPARATOR +
+ " - ๋ฉ๋ด๋ ํ๋ฒ์ ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค." + LINE_SEPARATOR;
+ private static final String RESULT_HEAD_ORDERED_MENU = "<์ฃผ๋ฌธ ๋ฉ๋ด>";
+ private static final String RESULT_HEAD_TOTAL_PRICE = "<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>";
+ private static final String RESULT_HEAD_GIVEAWAY_MENU = "<์ฆ์ ๋ฉ๋ด>";
+ private static final String RESULT_HEAD_BENEFIT_DETAILS = "<ํํ ๋ด์ญ>";
+ private static final String RESULT_HEAD_TOTAL_BENEFIT_PRICE = "<์ดํํ ๊ธ์ก>";
+ private static final String RESULT_HEAD_DISCOUNTED_TOTAL_PRICE = "<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>";
+ private static final String RESULT_HEAD_BADGE = "<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>";
+ private static final String PRICE = "%,d์";
+ private static final String NONE = "์์";
+
+ public void printIntro() {
+ System.out.printf(INTRO_MESSAGE + LINE_SEPARATOR);
+ }
+
+ public void printCaution() {
+ System.out.printf(EVENT_CAUTION + LINE_SEPARATOR);
+ }
+
+ public void printResult(EventResult eventResult) {
+ printResultIntro(eventResult.getOrder().getDate());
+ printOrder(eventResult.getOrder().getOrder());
+ printTotalPrice(eventResult.getOrder().getTotalOrderPrice());
+ printGiveawayMenu(eventResult.hasGiveawayBenefits());
+ printBenefitDetails(eventResult.getBenefitHistory());
+ printTotalBenefitPrice(eventResult.getTotalBenefitsPrice());
+ printDiscountedPrice(eventResult.getFinalPrice());
+ printBadge(eventResult.getBadge());
+ }
+
+ public void printResultIntro(int date) {
+ System.out.printf(LINE_SEPARATOR + RESULT_INTRO_MESSAGE + LINE_SEPARATOR, date);
+ }
+
+ public void printOrder(Map<Menu, Integer> order) {
+ System.out.printf(LINE_SEPARATOR + RESULT_HEAD_ORDERED_MENU + LINE_SEPARATOR);
+ String result = MapToStringConverter.orderToString(order);
+ System.out.printf(result);
+ }
+
+
+
+ public void printTotalPrice(int totalOrderPrice) {
+ System.out.printf(LINE_SEPARATOR + RESULT_HEAD_TOTAL_PRICE + LINE_SEPARATOR);
+
+ int price = totalOrderPrice;
+ System.out.printf(PRICE, price);
+ System.out.println();
+ }
+
+ public void printGiveawayMenu(boolean hasGiveawayBenefits) {
+ System.out.printf(LINE_SEPARATOR + RESULT_HEAD_GIVEAWAY_MENU + LINE_SEPARATOR);
+
+ String result = NONE + LINE_SEPARATOR;
+
+ if (hasGiveawayBenefits) {
+ result = MapToStringConverter.orderToString(DecemberEvent.getGiveawayBenefits());
+ }
+
+ System.out.printf(result);
+ }
+
+ public void printBenefitDetails(Map<Benefit, Integer> benefitAndPrice) {
+ System.out.printf(LINE_SEPARATOR + RESULT_HEAD_BENEFIT_DETAILS + LINE_SEPARATOR);
+ String result = NONE + LINE_SEPARATOR;
+
+ if (!benefitAndPrice.isEmpty()) {
+ result = MapToStringConverter.benefitToString(benefitAndPrice);
+ }
+
+ System.out.printf(result);
+ }
+
+ public void printTotalBenefitPrice(int totalBenefitPrice) {
+ System.out.printf(LINE_SEPARATOR + RESULT_HEAD_TOTAL_BENEFIT_PRICE + LINE_SEPARATOR);
+ String result = "-" + PRICE + LINE_SEPARATOR;
+ if (totalBenefitPrice == 0) {
+ result = PRICE + "\n";
+ }
+ System.out.printf(result, totalBenefitPrice);
+ }
+
+ public void printDiscountedPrice(int finalPrice) {
+ System.out.printf(LINE_SEPARATOR + RESULT_HEAD_DISCOUNTED_TOTAL_PRICE + LINE_SEPARATOR);
+
+ System.out.printf(PRICE + LINE_SEPARATOR, finalPrice);
+ }
+
+ public void printBadge(Badge badge) {
+ System.out.printf(LINE_SEPARATOR + RESULT_HEAD_BADGE + LINE_SEPARATOR);
+
+ System.out.printf(badge.getName() + LINE_SEPARATOR);
+ }
+} | Java | OutputView์์ ์ฌ์ฉ๋๋ ์์๊ฐ ๊ฒฝ์ฐ, enum์ผ๋ก ๋ฌถ์ด์ ๊ด๋ฆฌํ๋ฉด ์ฝ๋๊ฐ ํจ์ฌ ๊น๋ํ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,173 @@
+์์ฑ์: ์ํ๋ฆฌ
+
+# ๐
ํฌ๋ฆฌ์ค๋ง์ค ํ๋ก๋ชจ์
+
+**์ด๋ฒคํธ ๋ชฉํ**
+````
+1. ์ค๋ณต๋ ํ ์ธ๊ณผ ์ฆ์ ์ ํ์ฉํด์, ๊ณ ๊ฐ๋ค์ด ํํ์ ๋ง์ด ๋ฐ๋๋ค๋ ๊ฒ์ ์ฒด๊ฐํ ์ ์๊ฒ ํ๋ ๊ฒ
+2. ์ฌํด 12์์ ์ง๋ 5๋
์ค ์ต๊ณ ์ ํ๋งค ๊ธ์ก์ ๋ฌ์ฑ
+3. 12์ ์ด๋ฒคํธ ์ฐธ์ฌ ๊ณ ๊ฐ์ 5%๊ฐ ๋ด๋
1์ ์ํด ์ด๋ฒคํธ์ ์ฌ์ฐธ์ฌํ๋ ๊ฒ
+````
+
+---------------
+## โ ๊ธฐ๋ฅ ๋ชฉ๋ก
+
+### ์ฌ์ฉ์ ์ธํฐํ์ด์ค
+ - ์ฌ์ฉ์์๊ฒ ์
๋ ฅ์ ์์ฒญํ๋ ์๋ด ๋ฉ์์ง ์ถ๋ ฅ
+ - ์ฌ์ฉ์ ์
๋ ฅ ์๋ฌ ๋ฐ์ ์, '[ERROR]'๋ก ์์ํ๋ ์๋ฌ ๋ฉ์์ง ์ถ๋ ฅ
+### ๋ ์ง ์
๋ ฅ ๋ฐ ๊ฒ์ฆ
+ - ์ฌ์ฉ์๋ก๋ถํฐ 12์ ์ค ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ฅผ ์ซ์๋ก ์
๋ ฅ๋ฐ์
+ - ์
๋ ฅ๋ฐ์ ๋ ์ง๊ฐ 1 ์ด์ 31 ์ดํ์ธ์ง ๊ฒ์ฆ
+ - ์ ํจํ์ง ์์ ๋ ์ง ์
๋ ฅ ์ ์๋ฌ ๋ฉ์์ง ์ ๋ฌ ๋ฐ ์ฌ์
๋ ฅ ์์ฒญ
+### ๋ฉ๋ด ์ฃผ๋ฌธ ์
๋ ฅ ๋ฐ ๊ฒ์ฆ
+ - ์ฌ์ฉ์๋ก๋ถํฐ ์ฃผ๋ฌธํ ๋ฉ๋ด์ ์๋์ ์
๋ ฅ๋ฐ์ (์: ํด์ฐ๋ฌผํ์คํ-2, ๋ ๋์์ธ-1)
+ - ์
๋ ฅ๋ฐ์ ๋ฉ๋ด๊ฐ ๋ฉ๋ดํ์ ์๋์ง, ์๋์ด ์ ํจํ์ง ์ฃผ๋ฌธ ๊ฒ์ฆ
+ - ์ ํจํ์ง ์์ ์ฃผ๋ฌธ ์
๋ ฅ ์ ์๋ฌ ๋ฉ์์ง ์ ๋ฌ ๋ฐ ์ฌ์
๋ ฅ ์์ฒญ
+### ํ ์ธ ๋ฐ ์ฆ์ ์ด๋ฒคํธ ๊ณ์ฐ
+ - ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ ๊ณ์ฐ
+ - ํ์ผ ํ ์ธ ๊ณ์ฐ (๋์ ํธ ๋ฉ๋ด)
+ - ์ฃผ๋ง ํ ์ธ ๊ณ์ฐ (๋ฉ์ธ ๋ฉ๋ด)
+ - ํน๋ณ ํ ์ธ ๊ณ์ฐ (๋ฌ๋ ฅ์ ๋ณ ํ์๋ ๋ )
+ - ์ฆ์ ์ด๋ฒคํธ ์ ์ฉ (์ด์ฃผ๋ฌธ ๊ธ์ก์ด 12๋ง ์ ์ด์์ผ ๊ฒฝ์ฐ ์ดํ์ธ ์ฆ์ )
+### ํํ ๋ด์ญ ๋ฐ ์ดํํ ๊ธ์ก ๊ณ์ฐ
+ - ํ ์ธ ๊ธ์ก์ ํฉ๊ณ ๊ณ์ฐ
+ - ์ฆ์ ๋ฉ๋ด ๊ฐ๊ฒฉ ๊ณ์ฐ
+ - ์ดํํ ๊ธ์ก ๊ณ์ฐ
+### ์์ ๊ฒฐ์ ๊ธ์ก ๊ณ์ฐ
+ - ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก ๊ณ์ฐ
+### ์ด๋ฒคํธ ๋ฐฐ์ง ๋ถ์ฌ
+ - ์ดํํ ๊ธ์ก์ ๋ฐ๋ฅธ ์ด๋ฒคํธ ๋ฐฐ์ง ๋ถ์ฌ (๋ณ, ํธ๋ฆฌ, ์ฐํ)
+### ๊ฒฐ๊ณผ ์ถ๋ ฅ
+ - ์ฃผ๋ฌธ ๋ฉ๋ด, ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก, ์ฆ์ ๋ฉ๋ด, ํํ ๋ด์ญ, ์ดํํ ๊ธ์ก, ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก, ์ด๋ฒคํธ ๋ฐฐ์ง๋ฅผ ์ถ๋ ฅ
+### ๋จ์ ํ
์คํธ
+ - ๊ฐ ๊ธฐ๋ฅ๋ณ๋ก JUnit 5์ AssertJ๋ฅผ ์ด์ฉํ์ฌ ๋จ์ ํ
์คํธ ์์ฑ
+
+------------
+
+## ํฌ๋ฆฌ์ค๋ง์ค ํ๋ก๋ชจ์
ํ๋ก์ ํธ ๊ตฌ์กฐ
+
+```
+๐ป๐main
+ ๐ป๐java
+ ๐ป๐christmas
+ ๐ป๐controller
+ EventPlanner.java
+ ๐ป๐domain
+ ๐ป๐logic
+ ChristmasEventCalculator.java
+ EventCalculator.java
+ EventService.java
+ ๐ป๐model
+ DecemberEvent.java
+ EventResult.java
+ OrderInfo.java
+ ๐ป๐type
+ Badge.java (enum)
+ Benefit.java (enum)
+ Menu.java (enum)
+ ๐ป๐view
+ InputView.java
+ OutputView.java
+ ๐ป๐util
+ DateValidator.java
+ MapToStringConverter.java
+ OrderValidator.java
+ StringUtil.java
+ ValidationException.java
+ Application.java
+```
+### ์ค๋ช
+#### controller
+> ์ฌ์ฉ์์ ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ , ๋ชจ๋ธ์ ์กฐ์, ์ ์ ํ ์๋ต์ ๋ทฐ์ ์ ๋ฌํ๋ ํด๋์ค๊ฐ ์์นํฉ๋๋ค.
+
+#### domain
+> ๋๋ฉ์ธ ๋ก์ง์ ๋ด๋นํ๋ ํด๋์ค๋ค์ด ์์นํฉ๋๋ค. <br>
+
+- #### logic
+> **EventCalculator**: ์ด๋ฒคํธ ํ ์ธ์ ๊ณ์ฐํฉ๋๋ค. <br>
+> **ChristmasEventCalculator**: ๋ ์ง ๊ธฐ๋ฐ ํ ์ธ ๋ก์ง์ ์ฒ๋ฆฌํฉ๋๋ค. <br>
+> **EventService**: ์ฃผ๋ฌธ ์ ๋ณด๋ก ์ด๋ฒคํธ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ด์ฃผ๋ ํด๋์ค์
๋๋ค. <br>
+
+- #### model
+> **DecemberEvent**: ์ด๋ฒคํธ ๊ด๋ จ ๋ก์ง์ ์ฒ๋ฆฌํฉ๋๋ค. <br>
+> **EventResult**: ์ด๋ฒคํธ ๊ฒฐ๊ณผ๋ฅผ ๋ด๋ ํด๋์ค์
๋๋ค. <br>
+> **OrderInfo**: ์ฃผ๋ฌธ ์ ๋ณด๋ฅผ ๋ด๋ ํด๋์ค์
๋๋ค. <br>
+
+- #### type
+> **Benefit**: ํํ ์ข
๋ฅ๋ฅผ ๋ํ๋ด๋ ์ด๊ฑฐํ์
๋๋ค. <br>
+> **Menu**: ๋ฉ๋ด๋ฅผ ๋ํ๋ด๋ ์ด๊ฑฐํ์
๋๋ค. <br>
+> **Badge**: ๋ฑ์ง๋ฅผ ๋ํ๋ด๋ ์ด๊ฑฐํ์
๋๋ค. <br>
+
+
+#### view
+> ์ฌ์ฉ์ ์ธํฐํ์ด์ค๋ฅผ ๋ด๋นํ๋ ํด๋์ค๋ค์ด ์์นํฉ๋๋ค. <br>
+>
+> **InputView**: ์ฌ์ฉ์ ์
๋ ฅ์ ์ฒ๋ฆฌ. <br>
+> **OutputView**: ๊ฒฐ๊ณผ ์ถ๋ ฅ์ ์ฒ๋ฆฌ. <br>
+
+#### util
+> ์ ํธ๋ฆฌํฐ ํด๋์ค๋ค์ด ์์นํฉ๋๋ค. <br>
+>
+> **DateValidator**: ๋ ์ง ์ ํจ์ฑ ๊ฒ์ฌ ๋ก์ง์ ํฌํจ. <br>
+> **MapToStringConverter**: Map์ key, value ๊ฐ์ ๋ฌธ์์ด๋ก ๋ณํํ๋ ์์
์ฒ๋ฆฌ. <br>
+> **OrderValidator**: ๋ฉ๋ด์ ์๋ ์
๋ ฅ ์ ํจ์ฑ ๊ฒ์ฌ ๋ก์ง์ ํฌํจ. <br>
+> **StringUtil**: ๋ฌธ์์ด์ ํํท ๋ฌธ์๊ฐ ๋ช๊ฐ์๋์ง ์นด์ดํธํ๋ ๊ธฐ๋ฅ. <br>
+> **ValidationException**: ์ฌ์ฉ์ ์ ์ ์์ธ์ฒ๋ฆฌ ํด๋์ค. <br>
+
+----------------------
+## ๋์
+````
+1. Applicaton ์์ InputView, OutputView ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ EventPlaner(inputView, outputView)์ ์ฃผ์
์์ผ EventPlanner๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
+2. EventPlanner์์ InputView๋ฅผ ํตํด ์ฌ์ฉ์์๊ฒ ๋ ์ง(int date)์ ์ฃผ๋ฌธ(Map<๋ฉ๋ด, ์๋>)์ ๊ฒ์ฆํ ์
๋ ฅ๋ฐ๋๋ค.
+3. EventService(๋ ์ง, ์ฃผ๋ฌธ)์ ์ฃผ์
ํด ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค. eventService(๊ฐ์ฒด)๊ฐ ์์ฑ๋จ๊ณผ ๋์์ orderInfo(๊ฐ์ฒด)๊ฐ ์์ฑ๋๋ค.
+4. orderInfo๋ ์ค์ค๋ก ์ฃผ๋ฌธ ์ด์ก์ ๊ณ์ฐํด์ ์์ฑ๋๋ค. orderInfo์ ์ ๋ณด๋ฅผ ์ฌ์ฉํด
+ eventService์์ ๋ก์ง์ ๊ณ์ฐ๊ธฐ(EventCalculator, ChristmasEventCalculator) ํด๋์ค๋ค์ ํ์ฉํด ํ ์ธ ํํ์ ๊ณ์ฐํ๋ค.
+5. ๊ณ์ฐ๊ธฐ๋ค์ DecemberEvent์๊ฒ ํ ์ธ ์ฌ๋ถ๋ฅผ ๋ฌป๋๋ค.
+6. eventService์์ ๊ณ์ฐ๋ ๊ฐ๋ค๋ก eventResult๋ฅผ ์์ฑํ๋ค.
+7. eventResult์ ๊ฐ๋ค์ outView์์ ์ถ๋ ฅํ๋ค.
+````
+
+-------------------
+## ์์ธ ์ํฉ
+
+๋ ์ง ์
๋ ฅ
+- ๋ฌธ์์ด ์
๋ ฅ, ๊ณต๋ฐฑ ์
๋ ฅ
+
+์ฃผ๋ฌธ ์
๋ ฅ
+- **case 1: ๊ณต๋ฐฑ๋ง์ ์ ์ธํ ์๋ชป๋ ์
๋ ฅ์ ๋ฐ์๋ค๋ฉด ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ํต๊ณผ ์ํฌ ๊ฒ์ธ๊ฐ?**
+````
+์
๋ ฅ ์์: ํํ์ค -1, ์ ๋ก ์ฝ๋ผ-1,์ด์ฝ ์ผ์ดํฌ- 3
+
+์ฌ์ฉ์ ์นํ์ ๋ฐฉ์: ๊ณต๋ฐฑ ์ ๊ฑฐ ๊ธฐ๋ฅ์ ์ด์ฉํ๋ฉด ๋๋ฏ๋ก ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ํต๊ณผ์ํจ๋ค.
+ํ์ง๋ง, ๋ฐ์ดํฐ์ ์ผ๊ด์ฑ ์ ์งํ๊ธฐ ์ด๋ ค์ธ ์ ์์, ๋ก์ง์ด ๋ณต์กํด์ ธ์ ์ค๋ฅ๋ฐ์ ๊ฐ๋ฅ์ฑ์ด ์์.
+(ex. ํ ํ์ค, ํํ์ค, ํํ ์ค)์ ๊ฐ์ด ๊ณต๋ฐฑ์ด ์ ๋๋ก ์ ๊ฑฐ๊ฐ ์๋๊ฒ ๋ก์ง์ ์งฐ๋์ง ์ ํ์ธํด์ผํจ.
+๊ฒฐ๋ก : ํ๋ก ํธ์์ ๊ณต๋ฐฑ์ฌ๋ถ๋ฅผ ์ ๊ฐ๊ณตํด์ ์ ๋ฌ๋ฐ์ ๋ฐฑ์๋์์๋ ์ ํํ ๊ฐ์ ์
๋ ฅ๋ฐ๋๋ก ๊ตฌํํ๋ ๊ฒํ๋ ๊ฒ์ด ๋ง๋๊ฒ ๊ฐ๋ค.
+````
+
+- ์ค๋ณต๋ ๋ฉ๋ด๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+- ์๋ ๋ฉ๋ด๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+
+**case 2**
+- ์ฃผ๋ฌธ ์
๋ ฅ: ์ดํ์ธ-1-1, ์ด์ฝ์ผ์ดํฌ-1 ์ ํต๊ณผํด์ ๋ฐ์.
+- ์ฃผ๋ฌธ ์
๋ ฅ: ๋ฉ๋ด -- ์
๋ ฅ์ ์ค๋ฅ๋ฐ์.
+> ',' ๋ก ๋ถ๋ฆฌํ ๋ฌธ์์ด์ '-' ๊ฐฏ์๊ฐ 1๊ฐ ๊ฐ ์๋๋ผ๋ฉด ์์ธ์ฒ๋ฆฌ.
+
+
+--------
+## ๋ฆฌํํ ๋ง ํ ๋ฒํ ๊ฒ๋ค
+
+util์ MapReader ํด๋์ค ๋ง๋ค๊ธฐ
+```
+private void readMenuAndQuantity(Map<Menu, Integer> menuAndQuantity) {
+ for (Map.Entry<Menu, Integer> entry : menuAndQuantity.entrySet()) {
+ Menu menu = entry.getKey();
+ int quantity = entry.getValue();
+
+ //์ฐ์ฐ ๋ช
๋ น
+ }
+
+}
+```
+์ด์ : ์์ ๊ฐ์ ํํ๊ฐ ๋ง์ด ๋ฐ๋ณตํด์ ์ฐ์ด๊ณ ์์.
+
+//์ฐ์ฐ ๋ช
๋ น ๋ถ๋ถ๋ง ๊ฐ์๋ผ์ฐ๋ ๋ฐฉ๋ฒ์ด ์์๊น? > BiConsumer ์์๋ณด๊ธฐ. | Unknown | README ๊น๋ํด์ ์ ๋ฆฌ๊ฐ ์ ๋ ๊ฒ ๊ฐ์ต๋๋ค :) |
@@ -0,0 +1,116 @@
+package christmas.domain.logic;
+
+import christmas.domain.model.DecemberEvent;
+import christmas.domain.model.OrderInfo;
+import christmas.domain.model.EventResult;
+import christmas.domain.type.Badge;
+import christmas.domain.type.Benefit;
+import christmas.domain.type.Menu;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class EventService {
+ private final OrderInfo orderInfo;
+ private Map<Benefit, Integer> benefitHistory = new HashMap<>();
+
+ public EventService(int date, Map<Menu, Integer> order) {
+ this.orderInfo = new OrderInfo(date, order);
+ }
+
+ public EventResult makeEventResult() {
+ int totalOrderPrice = orderInfo.getTotalOrderPrice();
+ int date = orderInfo.getDate();
+ int totalBenefitsPrice = 0;
+ Badge badge = Badge.NONE;
+
+ if (EventCalculator.isEligibleForEvent(totalOrderPrice)) {
+ checkAll(totalOrderPrice, date);
+ totalBenefitsPrice = calculateTotalBenefitsPrice();
+ badge = checkBadge(totalBenefitsPrice);
+ }
+
+ EventResult eventResult = new EventResult(orderInfo, benefitHistory, totalBenefitsPrice, badge);
+ return eventResult;
+ }
+
+ private void checkAll(int totalOrderPrice, int date) {
+ checkChristmasEvent(date);
+ checkWeekdayDiscount(date);
+ checkWeekendDiscount(date);
+ checkSpecialDiscount(date);
+ checkGiveaway(totalOrderPrice);
+ }
+
+ private void updateBenefitHistory(Benefit benefit, int discount) {
+ if (discount != 0) {
+ benefitHistory.put(benefit, discount);
+ }
+ }
+
+ private void checkGiveaway(int totalPrice) {
+ Map<Menu, Integer> giveaway = EventCalculator.calculateGiveawayEvent(totalPrice);
+ int discount = 0;
+
+ for (Map.Entry<Menu, Integer> entry : giveaway.entrySet()) {
+ Menu menu = entry.getKey();
+ int quantity = entry.getValue();
+ int menuPrice = menu.getPrice();
+
+ discount += menuPrice * quantity;
+ }
+
+ if (discount != 0) {
+ updateBenefitHistory(Benefit.GIVEAWAY, discount);
+ }
+ }
+
+ private void checkWeekdayDiscount(int date) {
+ int discount = 0;
+
+ if (!DecemberEvent.isWeekend(date)) {
+ discount = EventCalculator.calculateWeekdayDiscount(date, orderInfo.getOrder());
+ updateBenefitHistory(Benefit.WEEKDAY, discount);
+ }
+ }
+
+ private void checkWeekendDiscount(int date) {
+ int discount = 0;
+
+ if (DecemberEvent.isWeekend(date)) {
+ discount = EventCalculator.calculateWeekendDiscount(date, orderInfo.getOrder());
+ updateBenefitHistory(Benefit.WEEKEND, discount);
+ }
+ }
+
+ private void checkChristmasEvent(int date) {
+ int discount = ChristmasEventCalculator.calculateChristmasDiscount(date);
+
+ if (discount != 0) {
+ updateBenefitHistory(Benefit.CHRISTMAS_D_DAY, discount);
+ }
+ }
+
+ private void checkSpecialDiscount(int date) {
+ int discount = EventCalculator.calculateSpecialDiscount(date);
+
+ if (discount != 0) {
+ updateBenefitHistory(Benefit.SPECIAL, discount);
+ }
+ }
+
+ private int calculateTotalBenefitsPrice() {
+ int totalBenefitsPrice = benefitHistory
+ .values()
+ .stream()
+ .mapToInt(Integer::intValue)
+ .sum();
+
+ return totalBenefitsPrice;
+ }
+
+ private Badge checkBadge(int totalBenefitsPrice) {
+ return EventCalculator.calculateBadge(totalBenefitsPrice);
+ }
+}
+ | Java | EventService๋ผ๋ ๊ฐ์ฒด์ ์กด์ฌ ์ด์ ๊ฐ ์ ์๋ฟ์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ์๋น์ค ๊ฐ์ฒด์ธ์ง, ์ ํธ์ฉ ๊ฐ์ฒด์ธ์ง, ํน์ ๋๋ฉ์ธ ๊ฐ์ฒด์ธ์ง ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ์๋น์ค ๊ฐ์ฒด๋ผ๋ฉด logic์ด๋ผ๋ ํจํค์ง๋ช
์ด ๋ชจํธํ๋ค๊ณ ์๊ฐํ๊ณ , service ํจํค์ง๋ฅผ ๋ง๋ค์ด ๊ทธ ํ์ ์์ด์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,82 @@
+package christmas.controller;
+
+import static christmas.domain.event.EventConstants.*;
+
+import christmas.domain.restaurant.Orders;
+import christmas.domain.restaurant.VisitDate;
+import christmas.service.RestaurantCalculator;
+import christmas.view.inputview.InputValueType;
+import christmas.view.inputview.InputView;
+import christmas.view.outputview.OutputView;
+
+public class WootecoRestaurantController {
+ private final RestaurantCalculator restaurantCalculator;
+ private final EventPlanerController eventPlanerController;
+
+ public WootecoRestaurantController(RestaurantCalculator restaurantCalculator,
+ EventPlanerController eventPlanerController) {
+ this.restaurantCalculator = restaurantCalculator;
+ this.eventPlanerController = eventPlanerController;
+ }
+
+ public void takeReservation() {
+ VisitDate visitDate = getVisitDate();
+ Orders orders = getOrders();
+ int orderTotalAmount = getOrderTotalAmount(orders);
+
+ showEventPreviewMessage(visitDate);
+ showOrderDetails(orders, orderTotalAmount);
+ executeEvent(visitDate, orders, orderTotalAmount);
+ }
+
+ private VisitDate getVisitDate() {
+ OutputView.printHelloMessage();
+ VisitDate visitDate = inputVisitDate();
+ return visitDate;
+ }
+
+ private Orders getOrders() {
+ OutputView.printMenu();
+ Orders orders = inputOrders();
+ return orders;
+ }
+
+ private int getOrderTotalAmount(Orders orders) {
+ return restaurantCalculator.getOrderTotalAmount(orders);
+ }
+
+ private static void showEventPreviewMessage(VisitDate visitDate) {
+ OutputView.printEventPreviewMessage(visitDate);
+ }
+
+ private static void showOrderDetails(Orders orders, int orderTotalAmount) {
+ OutputView.printOrder(orders, orderTotalAmount);
+ }
+
+ private void executeEvent(VisitDate visitDate, Orders orders, int orderTotalAmount) {
+ if (canApplyEvent(orderTotalAmount)) {
+ eventPlanerController.executeEventPlaner(visitDate, orders, orderTotalAmount);
+ }
+ }
+
+ private boolean canApplyEvent(int orderTotalAmount) {
+ if (isOrderTotalAmountValidForEvent(orderTotalAmount)) {
+ return true;
+ }
+
+ eventPlanerController.notApplyEvent(orderTotalAmount);
+ return false;
+ }
+
+ private static boolean isOrderTotalAmountValidForEvent(int orderTotalAmount) {
+ return orderTotalAmount >= EVENT_MIN_PAY_AMOUNT.getValue();
+ }
+
+ private Orders inputOrders() {
+ return (Orders) InputView.inputValue(InputValueType.ORDERS);
+ }
+
+ private VisitDate inputVisitDate() {
+ return (VisitDate) InputView.inputValue(InputValueType.VISIT_DATE);
+ }
+} | Java | ๊ฐ์ธ์ ์ธ ์๊ฐ์ผ๋ก, ์ปจํธ๋กค๋ฌ๊ฐ ๋ค๋ฅธ ์ปจํธ๋กค๋ฌ๋ฅผ ์ธ์คํด์ค ๋ณ์๋ก ๊ฐ์ง๊ณ ํธ์ถํ๋ ๊ฒ์ด ๋ง๋์ง ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
๋ง์ฝ ๊ณตํต๋ ๋ถ๋ถ์ด ์์ด์๋ผ๋ฉด, ์๋น์ค๋ฅผ ๋ฐ๋ก ๋๊ฑฐ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ํ๋๋ก ํฉ์น๋๊ฑธ ์ถ์ฒ๋๋ฆฝ๋๋ค! |
@@ -0,0 +1,78 @@
+package christmas.domain.restaurant;
+
+import christmas.domain.food.Dessert;
+import christmas.domain.food.Food;
+import christmas.domain.food.MainFood;
+import christmas.util.parser.Parser;
+import christmas.util.validator.OrderValidator;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+public class Orders {
+ private final Map<Food, Integer> orders;
+
+ private Orders(String orders) {
+ validate(orders);
+ this.orders = registerOrders(orders);
+ }
+
+ public static Orders create(String orders) {
+ return new Orders(orders);
+ }
+
+ public int getMainMenuCount() {
+ int mainMenuCount = 0;
+
+ for (Entry<Food, Integer> entry : orders.entrySet()) {
+ Food menu = entry.getKey();
+ int menuCount = entry.getValue();
+
+ if (menu instanceof MainFood) {
+ mainMenuCount += menuCount;
+ }
+ }
+
+ return mainMenuCount;
+ }
+
+ public int getDessertMenuCount() {
+ int dessertMenuCount = 0;
+
+ for (Entry<Food, Integer> entry : orders.entrySet()) {
+ Food menu = entry.getKey();
+ int menuCount = entry.getValue();
+
+ if (menu instanceof Dessert) {
+ dessertMenuCount += menuCount;
+ }
+ }
+
+ return dessertMenuCount;
+ }
+
+ private void validate(String orders) {
+ OrderValidator.validateOrders(orders);
+ }
+
+ private Map<Food, Integer> registerOrders(String orders) {
+ List<String> menus = Parser.parseMenus(orders);
+ List<Integer> counts = Parser.parseMenuCounts(orders);
+ Map<Food, Integer> order = new HashMap<>();
+
+ for (int idx = 0; idx < menus.size(); idx++) {
+ String menuName = menus.get(idx);
+ Food food = Menu.getFoodByName(menuName);
+ int count = counts.get(idx);
+
+ order.put(food, count);
+ }
+ return order;
+ }
+
+ public Map<Food, Integer> getOrders() {
+ return Collections.unmodifiableMap(orders);
+ }
+} | Java | Map ์๋ฃ๊ตฌ์กฐ๋ ์ฐ๋ค๋ณด๋ฉด ์๊ทผ ์์ด ๋ง์ด ๊ฐ๊ฒ๋๋ ์๋ฃ๊ตฌ์กฐ๋ผ๋ ์๊ฐ์ด ๋ค๋๋ผ๊ตฌ์.
๋ฌผ๋ก ์๋ฃ๊ตฌ์กฐ๋ง๋ค ์ฅ์ ๊ณผ ๋จ์ ์ด ์์ง๋ง
๊ฐ์ธ์ ์ผ๋ก ์ฌ๊ธฐ์๋ Map๋ณด๋ค๋ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ๊ฐ์ง ๋ณ๋์ Order ๊ฐ์ฒด๋ฅผ ๋๋ ๊ฒ์ด ์ข์๋ณด์
๋๋ค! |
@@ -0,0 +1,82 @@
+package christmas.controller;
+
+import static christmas.domain.event.EventConstants.*;
+
+import christmas.domain.restaurant.Orders;
+import christmas.domain.restaurant.VisitDate;
+import christmas.service.RestaurantCalculator;
+import christmas.view.inputview.InputValueType;
+import christmas.view.inputview.InputView;
+import christmas.view.outputview.OutputView;
+
+public class WootecoRestaurantController {
+ private final RestaurantCalculator restaurantCalculator;
+ private final EventPlanerController eventPlanerController;
+
+ public WootecoRestaurantController(RestaurantCalculator restaurantCalculator,
+ EventPlanerController eventPlanerController) {
+ this.restaurantCalculator = restaurantCalculator;
+ this.eventPlanerController = eventPlanerController;
+ }
+
+ public void takeReservation() {
+ VisitDate visitDate = getVisitDate();
+ Orders orders = getOrders();
+ int orderTotalAmount = getOrderTotalAmount(orders);
+
+ showEventPreviewMessage(visitDate);
+ showOrderDetails(orders, orderTotalAmount);
+ executeEvent(visitDate, orders, orderTotalAmount);
+ }
+
+ private VisitDate getVisitDate() {
+ OutputView.printHelloMessage();
+ VisitDate visitDate = inputVisitDate();
+ return visitDate;
+ }
+
+ private Orders getOrders() {
+ OutputView.printMenu();
+ Orders orders = inputOrders();
+ return orders;
+ }
+
+ private int getOrderTotalAmount(Orders orders) {
+ return restaurantCalculator.getOrderTotalAmount(orders);
+ }
+
+ private static void showEventPreviewMessage(VisitDate visitDate) {
+ OutputView.printEventPreviewMessage(visitDate);
+ }
+
+ private static void showOrderDetails(Orders orders, int orderTotalAmount) {
+ OutputView.printOrder(orders, orderTotalAmount);
+ }
+
+ private void executeEvent(VisitDate visitDate, Orders orders, int orderTotalAmount) {
+ if (canApplyEvent(orderTotalAmount)) {
+ eventPlanerController.executeEventPlaner(visitDate, orders, orderTotalAmount);
+ }
+ }
+
+ private boolean canApplyEvent(int orderTotalAmount) {
+ if (isOrderTotalAmountValidForEvent(orderTotalAmount)) {
+ return true;
+ }
+
+ eventPlanerController.notApplyEvent(orderTotalAmount);
+ return false;
+ }
+
+ private static boolean isOrderTotalAmountValidForEvent(int orderTotalAmount) {
+ return orderTotalAmount >= EVENT_MIN_PAY_AMOUNT.getValue();
+ }
+
+ private Orders inputOrders() {
+ return (Orders) InputView.inputValue(InputValueType.ORDERS);
+ }
+
+ private VisitDate inputVisitDate() {
+ return (VisitDate) InputView.inputValue(InputValueType.VISIT_DATE);
+ }
+} | Java | ํด๋น ๋ถ๋ถ์ ์ด๋ฒคํธ ๋๋ฉ์ธ ๊ด๋ จ ํด๋์ค์ ์์นํ๋ ๊ฒ์ด ์ข์๋ณด์
๋๋ค.
์ด๋ฒคํธ๋ฅผ ์ ์ฉํ ์ ์๋์ง ์๋์ง์ ๋ํด์๋ ๋น์ฆ๋์ค ๋ก์ง์ ํด๋น๋๋ค๊ณ ์๊ฐํด์! |
@@ -0,0 +1,82 @@
+package christmas.controller;
+
+import static christmas.domain.event.EventConstants.*;
+
+import christmas.domain.restaurant.Orders;
+import christmas.domain.restaurant.VisitDate;
+import christmas.service.RestaurantCalculator;
+import christmas.view.inputview.InputValueType;
+import christmas.view.inputview.InputView;
+import christmas.view.outputview.OutputView;
+
+public class WootecoRestaurantController {
+ private final RestaurantCalculator restaurantCalculator;
+ private final EventPlanerController eventPlanerController;
+
+ public WootecoRestaurantController(RestaurantCalculator restaurantCalculator,
+ EventPlanerController eventPlanerController) {
+ this.restaurantCalculator = restaurantCalculator;
+ this.eventPlanerController = eventPlanerController;
+ }
+
+ public void takeReservation() {
+ VisitDate visitDate = getVisitDate();
+ Orders orders = getOrders();
+ int orderTotalAmount = getOrderTotalAmount(orders);
+
+ showEventPreviewMessage(visitDate);
+ showOrderDetails(orders, orderTotalAmount);
+ executeEvent(visitDate, orders, orderTotalAmount);
+ }
+
+ private VisitDate getVisitDate() {
+ OutputView.printHelloMessage();
+ VisitDate visitDate = inputVisitDate();
+ return visitDate;
+ }
+
+ private Orders getOrders() {
+ OutputView.printMenu();
+ Orders orders = inputOrders();
+ return orders;
+ }
+
+ private int getOrderTotalAmount(Orders orders) {
+ return restaurantCalculator.getOrderTotalAmount(orders);
+ }
+
+ private static void showEventPreviewMessage(VisitDate visitDate) {
+ OutputView.printEventPreviewMessage(visitDate);
+ }
+
+ private static void showOrderDetails(Orders orders, int orderTotalAmount) {
+ OutputView.printOrder(orders, orderTotalAmount);
+ }
+
+ private void executeEvent(VisitDate visitDate, Orders orders, int orderTotalAmount) {
+ if (canApplyEvent(orderTotalAmount)) {
+ eventPlanerController.executeEventPlaner(visitDate, orders, orderTotalAmount);
+ }
+ }
+
+ private boolean canApplyEvent(int orderTotalAmount) {
+ if (isOrderTotalAmountValidForEvent(orderTotalAmount)) {
+ return true;
+ }
+
+ eventPlanerController.notApplyEvent(orderTotalAmount);
+ return false;
+ }
+
+ private static boolean isOrderTotalAmountValidForEvent(int orderTotalAmount) {
+ return orderTotalAmount >= EVENT_MIN_PAY_AMOUNT.getValue();
+ }
+
+ private Orders inputOrders() {
+ return (Orders) InputView.inputValue(InputValueType.ORDERS);
+ }
+
+ private VisitDate inputVisitDate() {
+ return (VisitDate) InputView.inputValue(InputValueType.VISIT_DATE);
+ }
+} | Java | ๋ง์ฐฌ๊ฐ์ง์ ์ด์ ๋ก, view์์๋ ๋๋ฉ์ธ ๋ชจ๋ธ ์์ฒด๊ฐ ์๋ ๋๋ฉ์ธ ๋ชจ๋ธ์ ๋ฐ์ดํฐ๋ง ์ ๋ฌํ๋ ๊ฒ์ด ์ข๋ค๊ณ ์๊ฐํฉ๋๋ค |
@@ -0,0 +1,82 @@
+package christmas.controller;
+
+import static christmas.domain.event.EventConstants.*;
+
+import christmas.domain.restaurant.Orders;
+import christmas.domain.restaurant.VisitDate;
+import christmas.service.RestaurantCalculator;
+import christmas.view.inputview.InputValueType;
+import christmas.view.inputview.InputView;
+import christmas.view.outputview.OutputView;
+
+public class WootecoRestaurantController {
+ private final RestaurantCalculator restaurantCalculator;
+ private final EventPlanerController eventPlanerController;
+
+ public WootecoRestaurantController(RestaurantCalculator restaurantCalculator,
+ EventPlanerController eventPlanerController) {
+ this.restaurantCalculator = restaurantCalculator;
+ this.eventPlanerController = eventPlanerController;
+ }
+
+ public void takeReservation() {
+ VisitDate visitDate = getVisitDate();
+ Orders orders = getOrders();
+ int orderTotalAmount = getOrderTotalAmount(orders);
+
+ showEventPreviewMessage(visitDate);
+ showOrderDetails(orders, orderTotalAmount);
+ executeEvent(visitDate, orders, orderTotalAmount);
+ }
+
+ private VisitDate getVisitDate() {
+ OutputView.printHelloMessage();
+ VisitDate visitDate = inputVisitDate();
+ return visitDate;
+ }
+
+ private Orders getOrders() {
+ OutputView.printMenu();
+ Orders orders = inputOrders();
+ return orders;
+ }
+
+ private int getOrderTotalAmount(Orders orders) {
+ return restaurantCalculator.getOrderTotalAmount(orders);
+ }
+
+ private static void showEventPreviewMessage(VisitDate visitDate) {
+ OutputView.printEventPreviewMessage(visitDate);
+ }
+
+ private static void showOrderDetails(Orders orders, int orderTotalAmount) {
+ OutputView.printOrder(orders, orderTotalAmount);
+ }
+
+ private void executeEvent(VisitDate visitDate, Orders orders, int orderTotalAmount) {
+ if (canApplyEvent(orderTotalAmount)) {
+ eventPlanerController.executeEventPlaner(visitDate, orders, orderTotalAmount);
+ }
+ }
+
+ private boolean canApplyEvent(int orderTotalAmount) {
+ if (isOrderTotalAmountValidForEvent(orderTotalAmount)) {
+ return true;
+ }
+
+ eventPlanerController.notApplyEvent(orderTotalAmount);
+ return false;
+ }
+
+ private static boolean isOrderTotalAmountValidForEvent(int orderTotalAmount) {
+ return orderTotalAmount >= EVENT_MIN_PAY_AMOUNT.getValue();
+ }
+
+ private Orders inputOrders() {
+ return (Orders) InputView.inputValue(InputValueType.ORDERS);
+ }
+
+ private VisitDate inputVisitDate() {
+ return (VisitDate) InputView.inputValue(InputValueType.VISIT_DATE);
+ }
+} | Java | ์ด ๋ถ๋ถ์ ๊ตฌ์กฐ ๊ฐ์ ์ด ํ์ํ๊ฒ ๋ค์. ๋ง์ํด์ฃผ์ ๋๋ก ์ปจํธ๋กค๋ฌ ๋ด๋ถ์ ์ปจํธ๋กค๋ฌ๋ฅผ ๋ ํ์๋ ์์๋ค๊ณ ์๊ฐํฉ๋๋ค. EventPlanerController๋ Service์ ๋๋ ๊ฒ ๋ง์๊ฒ ๋ค์.
๋๋ถ์ ์ด๋ ดํํ๊ฒ ์์๋ ์๋น์ค์ ์ปจํธ๋กค๋ฌ์ ์ญํ ์ ๋ํด ๋ค์ ํ ๋ฒ ์๊ฐํด๋ณผ ์ ์๋ ๊ธฐํ๊ฐ ๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ข์ ํผ๋๋ฐฑ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,82 @@
+package christmas.controller;
+
+import static christmas.domain.event.EventConstants.*;
+
+import christmas.domain.restaurant.Orders;
+import christmas.domain.restaurant.VisitDate;
+import christmas.service.RestaurantCalculator;
+import christmas.view.inputview.InputValueType;
+import christmas.view.inputview.InputView;
+import christmas.view.outputview.OutputView;
+
+public class WootecoRestaurantController {
+ private final RestaurantCalculator restaurantCalculator;
+ private final EventPlanerController eventPlanerController;
+
+ public WootecoRestaurantController(RestaurantCalculator restaurantCalculator,
+ EventPlanerController eventPlanerController) {
+ this.restaurantCalculator = restaurantCalculator;
+ this.eventPlanerController = eventPlanerController;
+ }
+
+ public void takeReservation() {
+ VisitDate visitDate = getVisitDate();
+ Orders orders = getOrders();
+ int orderTotalAmount = getOrderTotalAmount(orders);
+
+ showEventPreviewMessage(visitDate);
+ showOrderDetails(orders, orderTotalAmount);
+ executeEvent(visitDate, orders, orderTotalAmount);
+ }
+
+ private VisitDate getVisitDate() {
+ OutputView.printHelloMessage();
+ VisitDate visitDate = inputVisitDate();
+ return visitDate;
+ }
+
+ private Orders getOrders() {
+ OutputView.printMenu();
+ Orders orders = inputOrders();
+ return orders;
+ }
+
+ private int getOrderTotalAmount(Orders orders) {
+ return restaurantCalculator.getOrderTotalAmount(orders);
+ }
+
+ private static void showEventPreviewMessage(VisitDate visitDate) {
+ OutputView.printEventPreviewMessage(visitDate);
+ }
+
+ private static void showOrderDetails(Orders orders, int orderTotalAmount) {
+ OutputView.printOrder(orders, orderTotalAmount);
+ }
+
+ private void executeEvent(VisitDate visitDate, Orders orders, int orderTotalAmount) {
+ if (canApplyEvent(orderTotalAmount)) {
+ eventPlanerController.executeEventPlaner(visitDate, orders, orderTotalAmount);
+ }
+ }
+
+ private boolean canApplyEvent(int orderTotalAmount) {
+ if (isOrderTotalAmountValidForEvent(orderTotalAmount)) {
+ return true;
+ }
+
+ eventPlanerController.notApplyEvent(orderTotalAmount);
+ return false;
+ }
+
+ private static boolean isOrderTotalAmountValidForEvent(int orderTotalAmount) {
+ return orderTotalAmount >= EVENT_MIN_PAY_AMOUNT.getValue();
+ }
+
+ private Orders inputOrders() {
+ return (Orders) InputView.inputValue(InputValueType.ORDERS);
+ }
+
+ private VisitDate inputVisitDate() {
+ return (VisitDate) InputView.inputValue(InputValueType.VISIT_DATE);
+ }
+} | Java | ์ ๊ฐ ํด๋น ๋ถ๋ถ์ ๊ธฐ๋ฅ ๊ตฌํ์ ๊น๋จน๊ณ ์๋ค๊ฐ ๋ง์ง๋ง์ ํ๊ฒ ๋์๋๋ฐ, ์ด๋ฌํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค์.
๊ธฐ๋ณธ MVC ํจํด์ ์ปจํธ๋กค๋ฌ ์ญํ ์ ์๊ฐํด๋ณด๋ฉด ์ด๋ฒคํธ ๋๋ฉ์ธ์ด๋ ์๋น์ค์์ ํด๋น ๋ก์ง์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์ข์๋ณด์
๋๋ค. ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,74 @@
+package christmas.controller;
+
+import christmas.domain.*;
+import christmas.message.ErrorMessage;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+public class ChristmasPromotionController {
+ private EventCalendar eventCalendar;
+ private VisitDate visitDate;
+ private Menu menu;
+ private Event event;
+ private StarDate starDate;
+
+ public void run() {
+ eventCalendarSetting(2023, 12);
+ starDateSetting();
+ visitDate();
+ menuSetting();
+ eventSetting();
+ result();
+ }
+
+ private void eventCalendarSetting(int year, int month) {
+ this.eventCalendar = new EventCalendar(year, month);
+ }
+
+ private void starDateSetting() {
+ this.starDate = new StarDate(eventCalendar);
+ }
+
+ private void visitDate() {
+ OutputView.printStart();
+ while (true) {
+ try {
+ this.visitDate = VisitDate.of(InputView.readDate(), this.eventCalendar);
+ return;
+ } catch (IllegalArgumentException e) {
+ OutputView.printErrorMessage(ErrorMessage.INVALID_DATE.getFormattedMessage());
+ System.out.println(e.getMessage());
+ System.out.println();
+ }
+ }
+ }
+
+ private void menuSetting() {
+ while (true) {
+ try {
+ this.menu = new Menu(InputView.readMenu());
+ return;
+ } catch (IllegalArgumentException e) {
+ OutputView.printErrorMessage(ErrorMessage.INVALID_ORDER.getFormattedMessage());
+ System.out.println(e.getMessage());
+ OutputView.printAllMenu(e.getMessage());
+ System.out.println();
+ }
+ }
+ }
+
+ private void eventSetting() {
+ this.event = new Event();
+ this.event.eventSetting(visitDate, eventCalendar, starDate, menu);
+ }
+
+ private void result() {
+ OutputView.printSelectMenu(menu);
+ OutputView.printBeforeDiscountMenuTotalPrice(menu);
+ OutputView.printGift(event);
+ OutputView.printBenefitHistory(event);
+ OutputView.printBenefitsAllAmount(event);
+ OutputView.printAfterDiscount(menu, event);
+ OutputView.printBadge(event, eventCalendar);
+ }
+} | Java | `InputView` ์์ `CommonValidator` ๋ฅผ ํตํด์ ๊ฒ์ฆ์ ํ๋๊ฒ์ผ๋ก ๋ณด์
๋๋ค. ๊ทธ๋ ๋ค๋ฉด `InputValidator` ์์ ์ถ๊ฐ์ ์ธ ๊ฒ์ฆ๋ ํจ๊ป ์งํํด์ ์์ธ๋ฅผ ์ฒ๋ฆฌํ๋ ์์ผ๋ก ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ์ด๋ป๊ฒ ์๊ฐํ์๋์?
์ ๋ ์
๋ ฅ์ ๋ด๋นํ๋ ํด๋์ค์์ ์
๋ ฅ์ ๋ฐ๊ณ ๊ฒ์ฆ ํด๋์ค๋ฅผ ํตํด ๊ฒ์ฆํ ์์ธ๊น์ง ๋์ง๋๋ก ๊ตฌํํ๋๋ ๊น๋ํ๊ฒ ๊ตฌํํ๋๊ฑฐ ๊ฐ์์ ์ฌ์ญค๋ด
๋๋ค! |
@@ -0,0 +1,152 @@
+package christmas.domain;
+
+import christmas.domain.constants.Badge;
+import christmas.domain.constants.Gift;
+import christmas.domain.constants.event.EventDiscount;
+import christmas.domain.constants.event.EventValue;
+import christmas.domain.constants.menu.MenuGroup;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public class Event {
+ private final Map<EventDiscount, Integer> eventDiscountGroup;
+ private final Map<Gift, Integer> gifts;
+
+ public Event() {
+ this.eventDiscountGroup = initEventDiscounts();
+ this.gifts = initGifts();
+ }
+
+ private Map<EventDiscount, Integer> initEventDiscounts() {
+ Map<EventDiscount, Integer> result = new HashMap<>();
+ Arrays.stream(EventDiscount.values())
+ .forEach(rank -> result.put(rank, 0));
+ return result;
+ }
+
+ private Map<Gift, Integer> initGifts() {
+ return new HashMap<>();
+ }
+
+ public void eventSetting(VisitDate visitDate, EventCalendar eventCalendar, StarDate starDate, Menu menu) {
+ if (menu.getTotalMenuPrice() >= EventValue.ORDER_MIN_PRICE.getValue()) {
+ giftSetting(visitDate, eventCalendar, menu);
+ eventDiscountSetting(visitDate, eventCalendar, starDate, menu);
+ }
+ }
+
+ public void eventDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, StarDate starDate, Menu menu) {
+ weekdayDiscountSetting(visitDate, eventCalendar, menu);
+ weekendDiscountSetting(visitDate, eventCalendar, menu);
+ christmasDDayDiscountSetting(visitDate, eventCalendar);
+ specialDiscountSetting(visitDate, starDate);
+ eventDiscountGroupAddGift();
+ }
+
+ public void giftSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) {
+ if (isVisitDateAllDateEvent(visitDate, eventCalendar)) {
+ Arrays.stream(Gift.values())
+ .filter(gift -> gift.isGiftApplicable(menu.getTotalMenuPrice()))
+ .filter(Gift::getGift)
+ .forEach(gift -> gifts.put(gift, gift.getCount() * gift.getPrice()));
+ }
+ }
+
+ public void eventDiscountGroupAddGift() {
+ this.gifts.forEach((key, value) -> this.eventDiscountGroup.put(
+ EventDiscount.GIFT,
+ this.eventDiscountGroup.getOrDefault(EventDiscount.GIFT, 0) + value
+ ));
+ }
+
+ public void weekdayDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) {
+ if (!visitDate.isWeekend(eventCalendar) && isVisitDateAllDateEvent(visitDate, eventCalendar)) {
+ this.eventDiscountGroup.put(EventDiscount.WEEKDAY,
+ this.eventDiscountGroup.getOrDefault(
+ EventDiscount.WEEKDAY, 0) +
+ menu.categoryMenuCount(MenuGroup.DESSERT) * EventDiscount.WEEKDAY.getDiscount()
+ );
+ }
+ }
+
+ public void weekendDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) {
+ if (visitDate.isWeekend(eventCalendar) && isVisitDateAllDateEvent(visitDate, eventCalendar)) {
+ this.eventDiscountGroup.put(EventDiscount.WEEKEND,
+ this.eventDiscountGroup.getOrDefault(
+ EventDiscount.WEEKEND, 0) +
+ menu.categoryMenuCount(MenuGroup.MAIN) * EventDiscount.WEEKEND.getDiscount());
+ }
+ }
+
+ public void christmasDDayDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar) {
+ if (isVisitDateChristmasDDay(visitDate, eventCalendar)) {
+ this.eventDiscountGroup.put(EventDiscount.CHRISTMAS,
+ this.eventDiscountGroup.getOrDefault(
+ EventDiscount.CHRISTMAS, 0) + christmasDDayCalculate(visitDate)
+ );
+ }
+ }
+
+ public void specialDiscountSetting(VisitDate visitDate, StarDate starDate) {
+ if (visitDate.containsStarDate(starDate)) {
+ this.eventDiscountGroup.put(EventDiscount.SPECIAL,
+ this.eventDiscountGroup.getOrDefault(
+ EventDiscount.SPECIAL, 0) + EventDiscount.SPECIAL.getDiscount()
+ );
+ }
+ }
+
+ public int christmasDDayCalculate(VisitDate visitDate) {
+ return EventDiscount.CHRISTMAS.getDiscount() +
+ (EventValue.CHRISTMAS_ON_THE_RISE_FORM_DAY.getValue() *
+ visitDate.christmasDDayCalculate());
+ }
+
+ public int getTotalGiftAmount() {
+ return this.gifts.values()
+ .stream()
+ .mapToInt(amount -> amount)
+ .sum();
+ }
+
+ public int getTotalEventDiscount() {
+ return this.eventDiscountGroup.entrySet()
+ .stream()
+ .filter(entry -> entry.getKey() != EventDiscount.GIFT)
+ .mapToInt(Map.Entry::getValue)
+ .sum();
+ }
+
+ public int getTotalBenefitsAmount() {
+ return this.eventDiscountGroup.values()
+ .stream()
+ .mapToInt(amount -> amount)
+ .sum();
+ }
+
+ public Badge getBadge() {
+ return Arrays.stream(Badge.values())
+ .filter(badge -> badge.getWhichBadgeFromAmount(getTotalBenefitsAmount()))
+ .findFirst()
+ .orElse(Badge.NONE);
+ }
+
+ public boolean isVisitDateChristmasDDay(VisitDate visitDate, EventCalendar eventCalendar) {
+ return visitDate.isChristmasDDay(eventCalendar);
+ }
+
+ public boolean isVisitDateAllDateEvent(VisitDate visitDate, EventCalendar eventCalendar) {
+ return visitDate.containsAllDay(eventCalendar);
+ }
+
+ public Map<EventDiscount, Integer> getEventDiscountGroup() {
+ return Collections.unmodifiableMap(eventDiscountGroup);
+ }
+
+ public Map<Gift, Integer> getGifts() {
+ return Collections.unmodifiableMap(gifts);
+ }
+} | Java | `Enum` ์ `Map` ์ผ๋ก ์ฌ์ฉํ ๋๋ `EnumMap` ์ด ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ๊ณผ ์ฑ๋ฅ๋ฉด์์ ๋ ์ฐ์ํด์ `EnumMap` ์ผ๋ก ์ฌ์ฉํ๋๊ฒ๋ ๊ณ ๋ คํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,152 @@
+package christmas.domain;
+
+import christmas.domain.constants.Badge;
+import christmas.domain.constants.Gift;
+import christmas.domain.constants.event.EventDiscount;
+import christmas.domain.constants.event.EventValue;
+import christmas.domain.constants.menu.MenuGroup;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public class Event {
+ private final Map<EventDiscount, Integer> eventDiscountGroup;
+ private final Map<Gift, Integer> gifts;
+
+ public Event() {
+ this.eventDiscountGroup = initEventDiscounts();
+ this.gifts = initGifts();
+ }
+
+ private Map<EventDiscount, Integer> initEventDiscounts() {
+ Map<EventDiscount, Integer> result = new HashMap<>();
+ Arrays.stream(EventDiscount.values())
+ .forEach(rank -> result.put(rank, 0));
+ return result;
+ }
+
+ private Map<Gift, Integer> initGifts() {
+ return new HashMap<>();
+ }
+
+ public void eventSetting(VisitDate visitDate, EventCalendar eventCalendar, StarDate starDate, Menu menu) {
+ if (menu.getTotalMenuPrice() >= EventValue.ORDER_MIN_PRICE.getValue()) {
+ giftSetting(visitDate, eventCalendar, menu);
+ eventDiscountSetting(visitDate, eventCalendar, starDate, menu);
+ }
+ }
+
+ public void eventDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, StarDate starDate, Menu menu) {
+ weekdayDiscountSetting(visitDate, eventCalendar, menu);
+ weekendDiscountSetting(visitDate, eventCalendar, menu);
+ christmasDDayDiscountSetting(visitDate, eventCalendar);
+ specialDiscountSetting(visitDate, starDate);
+ eventDiscountGroupAddGift();
+ }
+
+ public void giftSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) {
+ if (isVisitDateAllDateEvent(visitDate, eventCalendar)) {
+ Arrays.stream(Gift.values())
+ .filter(gift -> gift.isGiftApplicable(menu.getTotalMenuPrice()))
+ .filter(Gift::getGift)
+ .forEach(gift -> gifts.put(gift, gift.getCount() * gift.getPrice()));
+ }
+ }
+
+ public void eventDiscountGroupAddGift() {
+ this.gifts.forEach((key, value) -> this.eventDiscountGroup.put(
+ EventDiscount.GIFT,
+ this.eventDiscountGroup.getOrDefault(EventDiscount.GIFT, 0) + value
+ ));
+ }
+
+ public void weekdayDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) {
+ if (!visitDate.isWeekend(eventCalendar) && isVisitDateAllDateEvent(visitDate, eventCalendar)) {
+ this.eventDiscountGroup.put(EventDiscount.WEEKDAY,
+ this.eventDiscountGroup.getOrDefault(
+ EventDiscount.WEEKDAY, 0) +
+ menu.categoryMenuCount(MenuGroup.DESSERT) * EventDiscount.WEEKDAY.getDiscount()
+ );
+ }
+ }
+
+ public void weekendDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) {
+ if (visitDate.isWeekend(eventCalendar) && isVisitDateAllDateEvent(visitDate, eventCalendar)) {
+ this.eventDiscountGroup.put(EventDiscount.WEEKEND,
+ this.eventDiscountGroup.getOrDefault(
+ EventDiscount.WEEKEND, 0) +
+ menu.categoryMenuCount(MenuGroup.MAIN) * EventDiscount.WEEKEND.getDiscount());
+ }
+ }
+
+ public void christmasDDayDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar) {
+ if (isVisitDateChristmasDDay(visitDate, eventCalendar)) {
+ this.eventDiscountGroup.put(EventDiscount.CHRISTMAS,
+ this.eventDiscountGroup.getOrDefault(
+ EventDiscount.CHRISTMAS, 0) + christmasDDayCalculate(visitDate)
+ );
+ }
+ }
+
+ public void specialDiscountSetting(VisitDate visitDate, StarDate starDate) {
+ if (visitDate.containsStarDate(starDate)) {
+ this.eventDiscountGroup.put(EventDiscount.SPECIAL,
+ this.eventDiscountGroup.getOrDefault(
+ EventDiscount.SPECIAL, 0) + EventDiscount.SPECIAL.getDiscount()
+ );
+ }
+ }
+
+ public int christmasDDayCalculate(VisitDate visitDate) {
+ return EventDiscount.CHRISTMAS.getDiscount() +
+ (EventValue.CHRISTMAS_ON_THE_RISE_FORM_DAY.getValue() *
+ visitDate.christmasDDayCalculate());
+ }
+
+ public int getTotalGiftAmount() {
+ return this.gifts.values()
+ .stream()
+ .mapToInt(amount -> amount)
+ .sum();
+ }
+
+ public int getTotalEventDiscount() {
+ return this.eventDiscountGroup.entrySet()
+ .stream()
+ .filter(entry -> entry.getKey() != EventDiscount.GIFT)
+ .mapToInt(Map.Entry::getValue)
+ .sum();
+ }
+
+ public int getTotalBenefitsAmount() {
+ return this.eventDiscountGroup.values()
+ .stream()
+ .mapToInt(amount -> amount)
+ .sum();
+ }
+
+ public Badge getBadge() {
+ return Arrays.stream(Badge.values())
+ .filter(badge -> badge.getWhichBadgeFromAmount(getTotalBenefitsAmount()))
+ .findFirst()
+ .orElse(Badge.NONE);
+ }
+
+ public boolean isVisitDateChristmasDDay(VisitDate visitDate, EventCalendar eventCalendar) {
+ return visitDate.isChristmasDDay(eventCalendar);
+ }
+
+ public boolean isVisitDateAllDateEvent(VisitDate visitDate, EventCalendar eventCalendar) {
+ return visitDate.containsAllDay(eventCalendar);
+ }
+
+ public Map<EventDiscount, Integer> getEventDiscountGroup() {
+ return Collections.unmodifiableMap(eventDiscountGroup);
+ }
+
+ public Map<Gift, Integer> getGifts() {
+ return Collections.unmodifiableMap(gifts);
+ }
+} | Java | ๊ฐ์ธ์ ์ผ๋ก `Event` ํด๋์ค๊ฐ ๋๋ฌด ๋ง์ ์ฑ
์์ ๊ฐ์ง๊ณ ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
ํ์ฌ `Event` ํด๋์ค๋
1. ๋ฐฉ๋ฌธํ๋ ๋ ์ง์ ๋งค๋ด๋ฅผ ๊ฐ์ง๊ณ ์กด์ฌํ๋ 4๊ฐ์ ์ด๋ฒคํธ๋ค์ ํด๋นํ๋์ง ์ฌ๋ถ ํ๋ณ
2. ๊ฐ์ข
ํ ์ธ๊ธ์ก ๊ณ์ฐ
3. ์ฌ์ฉ์์ ๋ฐฉ๋ฌธ์ ๋ฐ๋ฅธ ์ด๋ฒคํธ ๊ฒฐ๊ณผ๊ฐ์ฒด ์์ฑ
4. ์ฆ์ ํ ์ ๋ณด ์ถ๊ฐ
5. ํ ์ธ ๊ธ์ก์ ๋ฐ๋ฅธ ๋ฐฐ์ง ์ ๋ณด ์ถ๊ฐ
์ ๊ธฐ๋ฅ๋ค์ ํ๊ณ ์๋๊ฒ์ผ๋ก ๋ณด์
๋๋ค. ๋ฌผ๋ก ํ๋์ ํด๋์ค์ ๋ง์ ๊ธฐ๋ฅ์ ๋ฃ์ผ๋ฉด ๋ด์ผํ ํด๋์ค๋ค์ด ์๋์ ์ผ๋ก ์ ์ด์ง๊ธฐ ๋๋ฌธ์ ํธํ ์๋ ์์ง๋ง ์ถํ Event ์ ์๊ตฌ์ฌํญ์ด ๋ฐ๋๊ฑฐ๋ ํ ๊ฒฝ์ฐ ์๋ด์ผํ ๋ถ๋ถ์ด ๋ง์์ง๊ฒ ๊ฐ์ต๋๋ค. ์ฝ๋ ์์ฒด๋ 150 ์ค์ ๋ฌํ๊ณ ์์ด์ ์ญํ ๊ณผ ์ฑ
์์ ๋ถ๋ฆฌ๋ฅผ ๊ณ ๋ คํ์๋๊ฒ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,43 @@
+package christmas.domain;
+
+import christmas.domain.constants.event.EventValue;
+
+import java.time.DayOfWeek;
+import java.time.LocalDate;
+
+public class EventCalendar {
+ private final LocalDate currentDate;
+
+ public EventCalendar(int year, int month) {
+ this.currentDate = LocalDate.of(year, month, 1);
+ }
+
+ public boolean isWeekend(int day) {
+ DayOfWeek dayOfWeek = currentDate.withDayOfMonth(day).getDayOfWeek();
+ return dayOfWeek == DayOfWeek.FRIDAY || dayOfWeek == DayOfWeek.SATURDAY;
+ }
+
+ public boolean isChristmasDDay(int day) {
+ return day >= EventValue.CHRISTMAS_START_DAY.getValue() && day <= EventValue.CHRISTMAS_END_DAY.getValue();
+ }
+
+ public boolean containsAllDay(int day) {
+ return day >= EventValue.DECEMBER_START_DAY.getValue() && day <= EventValue.DECEMBER_END_DAY.getValue();
+ }
+
+ public int getYear() {
+ return currentDate.getYear();
+ }
+
+ public int getMonth() {
+ return currentDate.getMonthValue();
+ }
+
+ public int getStartDate() {
+ return currentDate.getDayOfMonth();
+ }
+
+ public int getEndDate() {
+ return currentDate.lengthOfMonth();
+ }
+} | Java | ์ด๋ฒคํธ์ ๋ง๋ ๋ ์ง๋ฅผ ์ธํ
ํ๋ ๋ฐฉ๋ฒ์ ์ ๋ง ์ข์ ๋ฐฉ๋ฒ ๊ฐ์ต๋๋ค ๐ ์ด ๋ฐฉ๋ฒ์ด ํจ์ฌ ๊น๋ํ๊ณ ์ฌ์ฉํ๊ธฐ๋ ํธํ๋ฐฉ๋ฒ ๊ฐ์์. |
@@ -0,0 +1,115 @@
+package christmas.domain;
+
+import christmas.domain.constants.event.EventValue;
+import christmas.domain.constants.menu.MenuGroup;
+import christmas.domain.constants.menu.MenuInterface;
+import christmas.message.ErrorMessage;
+import christmas.util.NumericConverter;
+import christmas.view.constants.OutputMessage;
+
+import java.util.*;
+
+public class Menu {
+ private static final int MAX_MENU_COUNT = 20;
+
+ private final Map<String, Map<MenuInterface, Integer>> menus;
+
+ public Menu(String menuInput) {
+ String[] menuCommaSplit = menuInput.split(OutputMessage.COMMA.getMessage());
+ validateMenuCountLimits(menuCommaSplit);
+ validateDuplicateMenu(menuCommaSplit);
+ this.menus = menuSetting(menuCommaSplit);
+ validateOnlyBeverage();
+ }
+
+ public Map<String, Map<MenuInterface, Integer>> menuSetting(String[] menuNameAndCountArr) {
+ Map<String, Map<MenuInterface, Integer>> menuGroups = new HashMap<>();
+ Arrays.stream(menuNameAndCountArr)
+ .forEach(menuNameAndCount -> {
+ String menuName = menuNameAndCount.split(OutputMessage.HYPHEN.getMessage())[0];
+ String menuCount = menuNameAndCount.split(OutputMessage.HYPHEN.getMessage())[1];
+ MenuGroup menuGroup = MenuGroup.findMenuCategory(menuName);
+ menuGroups.computeIfAbsent(menuGroup.getTitle(), k -> new HashMap<>())
+ .put(menuGroup.getMenuByName(menuName), Integer.parseInt(menuCount));
+ });
+ return menuGroups;
+ }
+
+ public int validateMenuCount(String menuCount) {
+ NumericConverter numericConverter = new NumericConverter();
+ try {
+ int count = numericConverter.convert(menuCount);
+ validateMenuCountIsPositive(count);
+ return count;
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_MENU_COUNT.getReasonFormattedMessage());
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException(e.getMessage());
+
+ }
+ }
+
+ public void validateMenuCountIsPositive(int menuCount) {
+ if (menuCount <= 0) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_MINIMUM_MENU_COUNT.getMessage());
+ }
+ }
+
+ public void validateDuplicateMenu(String[] menuNameAndCount) {
+ List<String> menuNames = Arrays.stream(menuNameAndCount)
+ .map(nameAndCount -> nameAndCount.split(OutputMessage.HYPHEN.getMessage())[0].trim())
+ .toList();
+ if (menuNames.size() != menuNames.stream().distinct().count()) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_DUPLICATE_MENU.getReasonFormattedMessage());
+ }
+ }
+
+ public void validateOnlyBeverage() {
+ if (this.menus.entrySet().stream()
+ .allMatch(entry -> entry.getKey().equals(MenuGroup.BEVERAGE.getTitle()))) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_ONLY_BEVERAGE.getReasonFormattedMessage());
+ }
+ }
+
+ public void validateMenuCountLimits(String[] menuNameAndCount) {
+ if (Arrays.stream(menuNameAndCount)
+ .mapToInt(nameAndCount -> validateMenuCount(
+ nameAndCount.split(OutputMessage.HYPHEN.getMessage())[1])
+ )
+ .sum() > MAX_MENU_COUNT) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_MENU_COUNT_LIMITS.getReasonFormattedMessage());
+ }
+ }
+
+ public int getTotalMenuPrice() {
+ return this.menus.values()
+ .stream()
+ .flatMap(menuGroup -> menuGroup.entrySet().stream())
+ .mapToInt(menu -> menu.getKey().getPrice() * menu.getValue())
+ .sum();
+ }
+
+ public boolean isTotalPriceTenThousandOrMore() {
+ return getTotalMenuPrice() >= EventValue.ORDER_MIN_PRICE.getValue();
+ }
+
+ public int categoryMenuCount(MenuGroup categoryMenu) {
+ return this.menus.entrySet()
+ .stream()
+ .filter(menuGroup -> menuGroup.getKey().equals(categoryMenu.getTitle()))
+ .mapToInt(menuGroup -> menuGroup.getValue().values().stream().mapToInt(Integer::intValue).sum())
+ .sum();
+ }
+
+ @Override
+ public String toString() {
+ StringJoiner stringJoiner = new StringJoiner("\n");
+ this.menus.forEach((key, value) -> value.forEach((menu, count) ->
+ stringJoiner.add(
+ menu.getName() +
+ OutputMessage.BLANK.getMessage() +
+ String.format(OutputMessage.COUNT.getMessage(), count))
+ ));
+ return stringJoiner.toString();
+ }
+} | Java | `menus` ๋ผ๋ ๊ฐ์ฒด๋ ํ์ฌ ์ฃผ๋ฌธํ ๋งค๋ด๋ค์ ๋ด์ ๊ฐ์ฒด๋ก ๋ณด์
๋๋ค. ๋งค๋ด๋ผ๋ ์ด๋ฆ์ ๊ฐ์ฒด ์์ ํ์ฌ ์ฃผ๋ฌธํ ๋งค๋ด๋ค์ด ์๋ ๊ตฌ์กฐ๊ฐ ์กฐ๊ธ ์ด์ํ๋ค๋ ์๊ฐ์ด ๋ญ๋๋ค.
๋งค๋ด๋ค์ ์ถ์ํํ `MenuInterface` ๊ฐ ์์ด์ ์ง๊ธ ๋ฐฉ์์ผ๋ก ๊ตฌํํ์ ๊ฒ ์๋๊น ํ๋ ์๊ฐ์ด ๋๋๋ฐ ์ด ์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด์ธ `~Menu` ๋ ์กด์ฌํ๋ ๋งํผ ํด๋์ค๋ฅผ ์ข๋ ์ง๊ด์ ์ธ ๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ๋ฐ๊พธ๋๊ฑด ์ด๋จ๊น์? ์๋ฅผ๋ค๋ฉด `Order` ๊ฐ์ ์ด๋ฆ๋ ์ข์๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,115 @@
+package christmas.domain;
+
+import christmas.domain.constants.event.EventValue;
+import christmas.domain.constants.menu.MenuGroup;
+import christmas.domain.constants.menu.MenuInterface;
+import christmas.message.ErrorMessage;
+import christmas.util.NumericConverter;
+import christmas.view.constants.OutputMessage;
+
+import java.util.*;
+
+public class Menu {
+ private static final int MAX_MENU_COUNT = 20;
+
+ private final Map<String, Map<MenuInterface, Integer>> menus;
+
+ public Menu(String menuInput) {
+ String[] menuCommaSplit = menuInput.split(OutputMessage.COMMA.getMessage());
+ validateMenuCountLimits(menuCommaSplit);
+ validateDuplicateMenu(menuCommaSplit);
+ this.menus = menuSetting(menuCommaSplit);
+ validateOnlyBeverage();
+ }
+
+ public Map<String, Map<MenuInterface, Integer>> menuSetting(String[] menuNameAndCountArr) {
+ Map<String, Map<MenuInterface, Integer>> menuGroups = new HashMap<>();
+ Arrays.stream(menuNameAndCountArr)
+ .forEach(menuNameAndCount -> {
+ String menuName = menuNameAndCount.split(OutputMessage.HYPHEN.getMessage())[0];
+ String menuCount = menuNameAndCount.split(OutputMessage.HYPHEN.getMessage())[1];
+ MenuGroup menuGroup = MenuGroup.findMenuCategory(menuName);
+ menuGroups.computeIfAbsent(menuGroup.getTitle(), k -> new HashMap<>())
+ .put(menuGroup.getMenuByName(menuName), Integer.parseInt(menuCount));
+ });
+ return menuGroups;
+ }
+
+ public int validateMenuCount(String menuCount) {
+ NumericConverter numericConverter = new NumericConverter();
+ try {
+ int count = numericConverter.convert(menuCount);
+ validateMenuCountIsPositive(count);
+ return count;
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_MENU_COUNT.getReasonFormattedMessage());
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException(e.getMessage());
+
+ }
+ }
+
+ public void validateMenuCountIsPositive(int menuCount) {
+ if (menuCount <= 0) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_MINIMUM_MENU_COUNT.getMessage());
+ }
+ }
+
+ public void validateDuplicateMenu(String[] menuNameAndCount) {
+ List<String> menuNames = Arrays.stream(menuNameAndCount)
+ .map(nameAndCount -> nameAndCount.split(OutputMessage.HYPHEN.getMessage())[0].trim())
+ .toList();
+ if (menuNames.size() != menuNames.stream().distinct().count()) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_DUPLICATE_MENU.getReasonFormattedMessage());
+ }
+ }
+
+ public void validateOnlyBeverage() {
+ if (this.menus.entrySet().stream()
+ .allMatch(entry -> entry.getKey().equals(MenuGroup.BEVERAGE.getTitle()))) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_ONLY_BEVERAGE.getReasonFormattedMessage());
+ }
+ }
+
+ public void validateMenuCountLimits(String[] menuNameAndCount) {
+ if (Arrays.stream(menuNameAndCount)
+ .mapToInt(nameAndCount -> validateMenuCount(
+ nameAndCount.split(OutputMessage.HYPHEN.getMessage())[1])
+ )
+ .sum() > MAX_MENU_COUNT) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_MENU_COUNT_LIMITS.getReasonFormattedMessage());
+ }
+ }
+
+ public int getTotalMenuPrice() {
+ return this.menus.values()
+ .stream()
+ .flatMap(menuGroup -> menuGroup.entrySet().stream())
+ .mapToInt(menu -> menu.getKey().getPrice() * menu.getValue())
+ .sum();
+ }
+
+ public boolean isTotalPriceTenThousandOrMore() {
+ return getTotalMenuPrice() >= EventValue.ORDER_MIN_PRICE.getValue();
+ }
+
+ public int categoryMenuCount(MenuGroup categoryMenu) {
+ return this.menus.entrySet()
+ .stream()
+ .filter(menuGroup -> menuGroup.getKey().equals(categoryMenu.getTitle()))
+ .mapToInt(menuGroup -> menuGroup.getValue().values().stream().mapToInt(Integer::intValue).sum())
+ .sum();
+ }
+
+ @Override
+ public String toString() {
+ StringJoiner stringJoiner = new StringJoiner("\n");
+ this.menus.forEach((key, value) -> value.forEach((menu, count) ->
+ stringJoiner.add(
+ menu.getName() +
+ OutputMessage.BLANK.getMessage() +
+ String.format(OutputMessage.COUNT.getMessage(), count))
+ ));
+ return stringJoiner.toString();
+ }
+} | Java | `Menu` ์
๋ ฅ์ ๋ํ ๊ฒ์ฆ๋ถ๋ถ์ด ์ฌ๊ธฐ์ ์กด์ฌํ๊ตฐ์! ์ ๋ 4์ฃผ์ฐจ์ ๊ณผ์ ๋ด๋ด ์
๋ ฅ๊ฐ์ ๋ํ ๊ฒ์ฆ ๋ก์ง์ ํญ์ ๋ถ๋ฆฌํด์ ๊ตฌํํ๋๊ฒ ์ณ๋ค๊ณ ์๊ฐํด์ ๊ทธ๋ ๊ฒ ๊ตฌํํ๋๋ฐ ์ด ๋ถ๋ถ์ ๋ํด์ ์์๋์ ์๊ฐ์ ์ด๋ค์ง ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,115 @@
+package christmas.domain;
+
+import christmas.domain.constants.event.EventValue;
+import christmas.domain.constants.menu.MenuGroup;
+import christmas.domain.constants.menu.MenuInterface;
+import christmas.message.ErrorMessage;
+import christmas.util.NumericConverter;
+import christmas.view.constants.OutputMessage;
+
+import java.util.*;
+
+public class Menu {
+ private static final int MAX_MENU_COUNT = 20;
+
+ private final Map<String, Map<MenuInterface, Integer>> menus;
+
+ public Menu(String menuInput) {
+ String[] menuCommaSplit = menuInput.split(OutputMessage.COMMA.getMessage());
+ validateMenuCountLimits(menuCommaSplit);
+ validateDuplicateMenu(menuCommaSplit);
+ this.menus = menuSetting(menuCommaSplit);
+ validateOnlyBeverage();
+ }
+
+ public Map<String, Map<MenuInterface, Integer>> menuSetting(String[] menuNameAndCountArr) {
+ Map<String, Map<MenuInterface, Integer>> menuGroups = new HashMap<>();
+ Arrays.stream(menuNameAndCountArr)
+ .forEach(menuNameAndCount -> {
+ String menuName = menuNameAndCount.split(OutputMessage.HYPHEN.getMessage())[0];
+ String menuCount = menuNameAndCount.split(OutputMessage.HYPHEN.getMessage())[1];
+ MenuGroup menuGroup = MenuGroup.findMenuCategory(menuName);
+ menuGroups.computeIfAbsent(menuGroup.getTitle(), k -> new HashMap<>())
+ .put(menuGroup.getMenuByName(menuName), Integer.parseInt(menuCount));
+ });
+ return menuGroups;
+ }
+
+ public int validateMenuCount(String menuCount) {
+ NumericConverter numericConverter = new NumericConverter();
+ try {
+ int count = numericConverter.convert(menuCount);
+ validateMenuCountIsPositive(count);
+ return count;
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_MENU_COUNT.getReasonFormattedMessage());
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException(e.getMessage());
+
+ }
+ }
+
+ public void validateMenuCountIsPositive(int menuCount) {
+ if (menuCount <= 0) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_MINIMUM_MENU_COUNT.getMessage());
+ }
+ }
+
+ public void validateDuplicateMenu(String[] menuNameAndCount) {
+ List<String> menuNames = Arrays.stream(menuNameAndCount)
+ .map(nameAndCount -> nameAndCount.split(OutputMessage.HYPHEN.getMessage())[0].trim())
+ .toList();
+ if (menuNames.size() != menuNames.stream().distinct().count()) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_DUPLICATE_MENU.getReasonFormattedMessage());
+ }
+ }
+
+ public void validateOnlyBeverage() {
+ if (this.menus.entrySet().stream()
+ .allMatch(entry -> entry.getKey().equals(MenuGroup.BEVERAGE.getTitle()))) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_ONLY_BEVERAGE.getReasonFormattedMessage());
+ }
+ }
+
+ public void validateMenuCountLimits(String[] menuNameAndCount) {
+ if (Arrays.stream(menuNameAndCount)
+ .mapToInt(nameAndCount -> validateMenuCount(
+ nameAndCount.split(OutputMessage.HYPHEN.getMessage())[1])
+ )
+ .sum() > MAX_MENU_COUNT) {
+ throw new IllegalArgumentException(ErrorMessage.INVALID_MENU_COUNT_LIMITS.getReasonFormattedMessage());
+ }
+ }
+
+ public int getTotalMenuPrice() {
+ return this.menus.values()
+ .stream()
+ .flatMap(menuGroup -> menuGroup.entrySet().stream())
+ .mapToInt(menu -> menu.getKey().getPrice() * menu.getValue())
+ .sum();
+ }
+
+ public boolean isTotalPriceTenThousandOrMore() {
+ return getTotalMenuPrice() >= EventValue.ORDER_MIN_PRICE.getValue();
+ }
+
+ public int categoryMenuCount(MenuGroup categoryMenu) {
+ return this.menus.entrySet()
+ .stream()
+ .filter(menuGroup -> menuGroup.getKey().equals(categoryMenu.getTitle()))
+ .mapToInt(menuGroup -> menuGroup.getValue().values().stream().mapToInt(Integer::intValue).sum())
+ .sum();
+ }
+
+ @Override
+ public String toString() {
+ StringJoiner stringJoiner = new StringJoiner("\n");
+ this.menus.forEach((key, value) -> value.forEach((menu, count) ->
+ stringJoiner.add(
+ menu.getName() +
+ OutputMessage.BLANK.getMessage() +
+ String.format(OutputMessage.COUNT.getMessage(), count))
+ ));
+ return stringJoiner.toString();
+ }
+} | Java | ์ด๋ฉ์ผ๋ก ์๋ ํผ๋๋ฐฑ์ค์ ๋ณ์๋ช
์ ์๋ฃํ์ ๋ฃ์ง ๋ง๋ผ๋ ํผ๋๋ฐฑ์ด ์์์ต๋๋ค! ๋ค์ `Arr` ์ ๋นผ๋ ์ข์๊ฒ ๊ฐ์ต๋๋ค ๐ |
@@ -0,0 +1,152 @@
+package christmas.domain;
+
+import christmas.domain.constants.Badge;
+import christmas.domain.constants.Gift;
+import christmas.domain.constants.event.EventDiscount;
+import christmas.domain.constants.event.EventValue;
+import christmas.domain.constants.menu.MenuGroup;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public class Event {
+ private final Map<EventDiscount, Integer> eventDiscountGroup;
+ private final Map<Gift, Integer> gifts;
+
+ public Event() {
+ this.eventDiscountGroup = initEventDiscounts();
+ this.gifts = initGifts();
+ }
+
+ private Map<EventDiscount, Integer> initEventDiscounts() {
+ Map<EventDiscount, Integer> result = new HashMap<>();
+ Arrays.stream(EventDiscount.values())
+ .forEach(rank -> result.put(rank, 0));
+ return result;
+ }
+
+ private Map<Gift, Integer> initGifts() {
+ return new HashMap<>();
+ }
+
+ public void eventSetting(VisitDate visitDate, EventCalendar eventCalendar, StarDate starDate, Menu menu) {
+ if (menu.getTotalMenuPrice() >= EventValue.ORDER_MIN_PRICE.getValue()) {
+ giftSetting(visitDate, eventCalendar, menu);
+ eventDiscountSetting(visitDate, eventCalendar, starDate, menu);
+ }
+ }
+
+ public void eventDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, StarDate starDate, Menu menu) {
+ weekdayDiscountSetting(visitDate, eventCalendar, menu);
+ weekendDiscountSetting(visitDate, eventCalendar, menu);
+ christmasDDayDiscountSetting(visitDate, eventCalendar);
+ specialDiscountSetting(visitDate, starDate);
+ eventDiscountGroupAddGift();
+ }
+
+ public void giftSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) {
+ if (isVisitDateAllDateEvent(visitDate, eventCalendar)) {
+ Arrays.stream(Gift.values())
+ .filter(gift -> gift.isGiftApplicable(menu.getTotalMenuPrice()))
+ .filter(Gift::getGift)
+ .forEach(gift -> gifts.put(gift, gift.getCount() * gift.getPrice()));
+ }
+ }
+
+ public void eventDiscountGroupAddGift() {
+ this.gifts.forEach((key, value) -> this.eventDiscountGroup.put(
+ EventDiscount.GIFT,
+ this.eventDiscountGroup.getOrDefault(EventDiscount.GIFT, 0) + value
+ ));
+ }
+
+ public void weekdayDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) {
+ if (!visitDate.isWeekend(eventCalendar) && isVisitDateAllDateEvent(visitDate, eventCalendar)) {
+ this.eventDiscountGroup.put(EventDiscount.WEEKDAY,
+ this.eventDiscountGroup.getOrDefault(
+ EventDiscount.WEEKDAY, 0) +
+ menu.categoryMenuCount(MenuGroup.DESSERT) * EventDiscount.WEEKDAY.getDiscount()
+ );
+ }
+ }
+
+ public void weekendDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar, Menu menu) {
+ if (visitDate.isWeekend(eventCalendar) && isVisitDateAllDateEvent(visitDate, eventCalendar)) {
+ this.eventDiscountGroup.put(EventDiscount.WEEKEND,
+ this.eventDiscountGroup.getOrDefault(
+ EventDiscount.WEEKEND, 0) +
+ menu.categoryMenuCount(MenuGroup.MAIN) * EventDiscount.WEEKEND.getDiscount());
+ }
+ }
+
+ public void christmasDDayDiscountSetting(VisitDate visitDate, EventCalendar eventCalendar) {
+ if (isVisitDateChristmasDDay(visitDate, eventCalendar)) {
+ this.eventDiscountGroup.put(EventDiscount.CHRISTMAS,
+ this.eventDiscountGroup.getOrDefault(
+ EventDiscount.CHRISTMAS, 0) + christmasDDayCalculate(visitDate)
+ );
+ }
+ }
+
+ public void specialDiscountSetting(VisitDate visitDate, StarDate starDate) {
+ if (visitDate.containsStarDate(starDate)) {
+ this.eventDiscountGroup.put(EventDiscount.SPECIAL,
+ this.eventDiscountGroup.getOrDefault(
+ EventDiscount.SPECIAL, 0) + EventDiscount.SPECIAL.getDiscount()
+ );
+ }
+ }
+
+ public int christmasDDayCalculate(VisitDate visitDate) {
+ return EventDiscount.CHRISTMAS.getDiscount() +
+ (EventValue.CHRISTMAS_ON_THE_RISE_FORM_DAY.getValue() *
+ visitDate.christmasDDayCalculate());
+ }
+
+ public int getTotalGiftAmount() {
+ return this.gifts.values()
+ .stream()
+ .mapToInt(amount -> amount)
+ .sum();
+ }
+
+ public int getTotalEventDiscount() {
+ return this.eventDiscountGroup.entrySet()
+ .stream()
+ .filter(entry -> entry.getKey() != EventDiscount.GIFT)
+ .mapToInt(Map.Entry::getValue)
+ .sum();
+ }
+
+ public int getTotalBenefitsAmount() {
+ return this.eventDiscountGroup.values()
+ .stream()
+ .mapToInt(amount -> amount)
+ .sum();
+ }
+
+ public Badge getBadge() {
+ return Arrays.stream(Badge.values())
+ .filter(badge -> badge.getWhichBadgeFromAmount(getTotalBenefitsAmount()))
+ .findFirst()
+ .orElse(Badge.NONE);
+ }
+
+ public boolean isVisitDateChristmasDDay(VisitDate visitDate, EventCalendar eventCalendar) {
+ return visitDate.isChristmasDDay(eventCalendar);
+ }
+
+ public boolean isVisitDateAllDateEvent(VisitDate visitDate, EventCalendar eventCalendar) {
+ return visitDate.containsAllDay(eventCalendar);
+ }
+
+ public Map<EventDiscount, Integer> getEventDiscountGroup() {
+ return Collections.unmodifiableMap(eventDiscountGroup);
+ }
+
+ public Map<Gift, Integer> getGifts() {
+ return Collections.unmodifiableMap(gifts);
+ }
+} | Java | `StarDate` ํด๋์ค์์ ํน๋ณ ์ด๋ฒคํธ๋ค์ ํด๋นํ๋ ๋ ์ง์ธ ์ผ์์ผ ๋ฟ๋ง ์๋๋ผ `starDateSettingWithSpecialDay` ๋ฅผ ํตํด์ ํฌ๋ฆฌ์ค๋ง์ค ์ ๋ณด๋ ๋ฃ๋๊ฒ์ผ๋ก ๋ณด์
๋๋ค. `EventCalendar` ๋์ ์ `StarDate` ๋ฅผ ํ์ฉํ๋ ๊ฒ์ ์ด๋จ๊น์? |
@@ -0,0 +1,45 @@
+package christmas.domain;
+
+import christmas.domain.constants.event.EventDate;
+
+import java.time.DayOfWeek;
+import java.time.LocalDate;
+import java.time.temporal.TemporalAdjusters;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+public class StarDate {
+ private final Set<Integer> starDates;
+
+ public StarDate(EventCalendar eventCalendar) {
+ this.starDates = new HashSet<>() {{
+ addAll(starDatesSettingWithSunday(eventCalendar));
+ addAll(starDateSettingWithSpecialDay(eventCalendar));
+ }};
+ }
+
+ public boolean visitDateContainsStarDates(int day) {
+ return starDates.contains(day);
+ }
+
+ public Set<Integer> starDatesSettingWithSunday(EventCalendar eventCalendar) {
+ LocalDate localDate = LocalDate.of(eventCalendar.getYear(), eventCalendar.getMonth(), eventCalendar.getStartDate());
+ Set<Integer> sundays = new HashSet<>();
+ LocalDate currentSunday = localDate.with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY));
+
+ while (!currentSunday.isAfter(localDate.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY)))) {
+ sundays.add(currentSunday.getDayOfMonth());
+ currentSunday = currentSunday.plusWeeks(1);
+ }
+ return sundays;
+ }
+
+ public Set<Integer> starDateSettingWithSpecialDay(EventCalendar eventCalendar) {
+ return Arrays.stream(EventDate.values())
+ .filter(eventDate -> eventDate.getMonth() == eventCalendar.getMonth())
+ .findFirst()
+ .map(EventDate::getEventDates)
+ .orElse(EventDate.NONE.getEventDates());
+ }
+} | Java | ์ ๋ ๊ฐ์ธ์ ์ผ๋ก ์ด๋ฒ ๊ณผ์ ๋ ์ฌ๋ฌ๊ฐ์ง ์ด๋ฒคํธ๋ค์ ์ถ์ํํ๋ ๊ฒ์ด ํต์ฌ์ด๋ผ๊ณ ์๊ฐํด์ ์ด๋ฒคํธ ๊ฐ์ฒด๋ค์๋ง ์ ๊ฒฝ์ ์จ์ ์๋์ ์ผ๋ก ๋ ์ง ์ ๋ณด๋ค์ ์กฐ๊ธ ๋์ถฉ(?) ๋ค๋ค๋๊ฒ ๊ฐ์ต๋๋ค. ์์๋ ์ฒ๋ผ ๋ ์ง ์ ๋ณด๋ค์ ๊ผผ๊ผผํ๊ฒ ๋ค๋ค์ผํ๋ ํ๋ ์๊ฐ์ด ๋๋ค์ ๐ฅฒ |
@@ -0,0 +1,31 @@
+package christmas.domain.constants;
+
+import java.util.function.Predicate;
+
+public enum Badge {
+ NONE("์์", (amount) -> amount >= 0 && amount < 5_000),
+ START("๋ณ", (amount) -> amount >= 5_000 && amount < 10_000),
+ TREE("ํธ๋ฆฌ", (amount) -> amount >= 10_000 && amount < 20_000),
+ SANTA("์ฐํ", (amount) -> amount >= 20_000 && amount < Integer.MAX_VALUE);
+
+ private final String badge;
+ private final Predicate<Integer> whichBadge;
+
+ Badge(String badge, Predicate<Integer> whichBadge) {
+ this.badge = badge;
+ this.whichBadge = whichBadge;
+ }
+
+ public String getBadge() {
+ return badge;
+ }
+
+ public boolean getWhichBadgeFromAmount(int amount) {
+ return whichBadge.test(amount);
+ }
+
+ @Override
+ public String toString() {
+ return badge;
+ }
+}
\ No newline at end of file | Java | ์ ๋ง ์ข์ ๋ฐฉ๋ฒ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค! `Predicate` ๋ฅผ ํ์ฉํ๋ฉด์๋ ๋ฏธ์ฒ ์๊ฐํ์ง ๋ชปํ ๋ฐฉ๋ฒ์ด๋ค์ ๐ |
@@ -0,0 +1,41 @@
+package christmas.domain.constants;
+
+import java.util.function.Function;
+
+public enum Gift {
+ CHAMPAGNE("์ดํ์ธ", 25_000, 1, true, (amount) -> amount >= 120_000);
+
+ private final String name;
+ private final int price;
+ private final int count;
+ private final boolean gift;
+ private final Function<Integer, Boolean> giftApplicable;
+
+ Gift(String name, int price, int count, boolean gift, Function<Integer, Boolean> giftApplicable) {
+ this.name = name;
+ this.price = price;
+ this.count = count;
+ this.gift = gift;
+ this.giftApplicable = giftApplicable;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public int getPrice() {
+ return this.price;
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ public boolean getGift() {
+ return gift;
+ }
+
+ public boolean isGiftApplicable(int amount) {
+ return giftApplicable.apply(amount);
+ }
+} | Java | `Gift` enum ํด๋์ค์์ ์กด์ฌํ๋ ์์๋ค์ ์ด๋ฏธ ์ฆ์ ํ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค. ์ด๋ค ์ด์ ๋ก ๊ตฌํํ์
จ๋์ง ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,35 @@
+package christmas.domain.constants.event;
+
+import java.util.Set;
+
+public enum EventDate {
+ NONE(0, Set.of()),
+ JANUARY(1, Set.of(1)),
+ FEBRUARY(2, Set.of(14)),
+ MARCH(3, Set.of(1, 14)),
+ APRIL(4, Set.of()),
+ MAY(5, Set.of(5, 8, 15)),
+ JUNE(6, Set.of(6)),
+ JULY(7, Set.of()),
+ AUGUST(8, Set.of(15)),
+ SEPTEMBER(9, Set.of()),
+ OCTOBER(10, Set.of(3, 9)),
+ NOVEMBER(11, Set.of(11)),
+ DECEMBER(12, Set.of(25));
+
+ private final int month;
+ private final Set<Integer> eventDates;
+
+ EventDate(int month, Set<Integer> eventDates) {
+ this.month = month;
+ this.eventDates = eventDates;
+ }
+
+ public int getMonth() {
+ return month;
+ }
+
+ public Set<Integer> getEventDates() {
+ return eventDates;
+ }
+} | Java | ๊ฐ ์์ ํน๋ณํ ๋ ์ง๋ค์ ์ ์ํด๋์ enum ์ผ๋ก ๋ณด์
๋๋ค. ๋ค๋ง ๊ทธ ๊ธฐ์ค์ด ์กฐ๊ธ ๋ชจํธํ ๊ฒ ๊ฐ์ต๋๋ค.
`2์` ์ด๋ `3์` ๊ฐ์ ๊ฒฝ์ฐ๋ ๊ณตํด์ผ์ด ์๋ ๋ฐ๋ ํ์ธ ๋ฐ์ด ๊ฐ์ ๋ ๋ค์ ํฌํจํ๋ ๋ฐ๋ฉด ๊ณตํด์ผ๋ง์ ํฌํจํ๋ ๋ฌ๋ ์์ต๋๋ค. ํ์ง๋ง `5์ 27์ผ` (๋ถ์ฒ๋ ์ค์ ๋ ), `9์ 28 ~ 30์ผ` (์ถ์) ์ ์ ์ธ๋์ด ์๊ณ ํ ๋ก์ ๋ฐ์ด์ฒ๋ผ ๋น ์ง ๋ ๋ค๋ ์์ต๋๋ค.
๊ธฐ์ค์ ์ธ์์ ๊ณตํด์ผ๋ง ๋ฃ๋๋ค๋ ์ง ๊ณตํด์ผ ํฌํจ ๋ชจ๋ ์๋ฏธ์๋ ๋ ๋ค? ์ ๋ฃ๋๋ค๋ ์ง ํด์ ๊ธฐ์ค์ ์ธ์ฐ๋ฉด ์ข๋ ๋ช
ํํ๊ฒ ์ดํดํ ์ ์์๊ฒ ๊ฐ๋ค๋ ์๊ฐ์ด ๋ค์์ต๋๋ค. ์์๋์ ์๊ฐ์ ์ด๋ ์ ์ง ๊ถ๊ธํฉ๋๋ค ๐ค |
@@ -0,0 +1,12 @@
+package christmas.util.validation;
+
+import christmas.message.ErrorMessage;
+
+public class CommonValidator {
+
+ public static void isBlank(String text) {
+ if (text == null || text.isBlank()) {
+ throw new IllegalArgumentException(ErrorMessage.BLANK.getReasonFormattedMessage());
+ }
+ }
+} | Java | ์ ๋ ์ด๋ฐ์์ผ๋ก ์
๋ ฅ๊ฐ์ด ๊ณต๋ฐฑ์ธ์ง ์ฒดํฌํ๋ ๋ฉ์๋๋ฅผ ๋ง๋ค์์๋๋ฐ `Console.readLine().trim();` ์ผ๋ก ๊ณต๋ฐฑ ์ ๊ฑฐํ๋ ๋ฐฉ๋ฒ๋ ์๋๋ผ๊ตฌ์ ๐ |
@@ -0,0 +1,16 @@
+package christmas.view.constants;
+
+public enum InputMessage {
+ VISIT_DATE("12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)"),
+ ORDER_MENU("์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)");
+
+ private final String message;
+
+ InputMessage(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+} | Java | ๋ฌ๋ ฅ ๋ฐ ๋ ์ง๊ด๋ จ ํด๋์ค์ 12์ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ ๋ค๋ ์ ์ํด๋์
จ์ผ๋ ์ด๋ถ๋ถ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ๋๋ก ์ฒ๋ฆฌํ๋ฉด ์ข์๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,53 @@
+package christmas.domain.event;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+import christmas.domain.menu.MenuGroup;
+import christmas.domain.menu.MenuType;
+
+public final class WeekdayDiscountEvent implements Event {
+
+ private static final int CHRISTMAS_EVENT_MONTH = 12;
+ private static final int CHRISTMAS_EVENT_MIN_DAY = 1;
+ private static final int CHRISTMAS_EVENT_MAX_DAY = 31;
+ private static final int CHRISTMAS_EVENT_DESERT_DISCOUNT = 2_023;
+ private static final String WEEKDAY_DISCOUNT_EVENT_TITLE = "ํ์ผ ํ ์ธ";
+ private int benefit;
+
+ @Override
+ public boolean isApplicable(Date date) {
+ return isEventPeriod(date) && !date.isWeekend();
+ }
+
+ private static boolean isEventPeriod(Date date) {
+ return date.getMonth() == CHRISTMAS_EVENT_MONTH &&
+ date.getDay() >= CHRISTMAS_EVENT_MIN_DAY
+ && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY;
+ }
+
+ @Override
+ public int calculateDiscount(Date date, Order order) {
+ benefit = order.getMenus().menuList().stream()
+ .mapToInt(menu -> {
+ MenuType menuType = MenuType.findByMenuName(menu.getMenuName());
+ MenuGroup menuGroup = MenuGroup.findByMenuType(menuType);
+ int menuQuantity = menu.getMenuQuantity();
+ if (menuGroup == MenuGroup.DESSERT) {
+ return CHRISTMAS_EVENT_DESERT_DISCOUNT * menuQuantity;
+ }
+ return 0;
+ })
+ .sum();
+ return benefit;
+ }
+
+ @Override
+ public int getBenefit() {
+ return benefit;
+ }
+
+ @Override
+ public String getEventName() {
+ return WEEKDAY_DISCOUNT_EVENT_TITLE;
+ }
+} | Java | ์์๋ก ๋ ์ง๋ฅผ ์ ์ํ๋ค๊ธฐ ๋ณด๋ค๋ java.util.Calendar ํด๋์ค๋ฅผ ์ด์ฉํ๋ฉด ๋ ์ฝ๊ฒ ํ๊ธฐ๊ฐ๋ฅ ํ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,53 @@
+package christmas.domain.event;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+import christmas.domain.menu.MenuGroup;
+import christmas.domain.menu.MenuType;
+
+public final class WeekendDiscountEvent implements Event {
+ private static final int CHRISTMAS_EVENT_MONTH = 12;
+ private static final int CHRISTMAS_EVENT_MIN_DAY = 1;
+ private static final int CHRISTMAS_EVENT_MAX_DAY = 31;
+ private static final int CHRISTMAS_EVENT_DESERT_DISCOUNT = 2_023;
+ private static final String WEEKEND_DISCOUNT_EVENT_TITLE = "์ฃผ๋ง ํ ์ธ";
+ private int benefit;
+
+ @Override
+ public boolean isApplicable(Date date) {
+ return isEventPeriod(date) &&
+ date.isWeekend();
+ }
+
+ private static boolean isEventPeriod(Date date) {
+ return date.getMonth() == CHRISTMAS_EVENT_MONTH &&
+ date.getDay() >= CHRISTMAS_EVENT_MIN_DAY
+ && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY;
+ }
+
+ @Override
+ public int calculateDiscount(Date date, Order order) {
+ benefit = order.getMenus().menuList().stream()
+ .mapToInt(menu -> {
+ MenuType menuType = MenuType.findByMenuName(menu.getMenuName());
+ MenuGroup menuGroup = MenuGroup.findByMenuType(menuType);
+ int menuQuantity = menu.getMenuQuantity();
+ if (menuGroup == MenuGroup.MAIN) {
+ return CHRISTMAS_EVENT_DESERT_DISCOUNT * menuQuantity;
+ }
+ return 0;
+ })
+ .sum();
+ return benefit;
+ }
+
+ @Override
+ public int getBenefit() {
+ return benefit;
+ }
+
+ @Override
+ public String getEventName() {
+ return WEEKEND_DISCOUNT_EVENT_TITLE;
+ }
+} | Java | ์ด ๋ถ๋ถ๋ Calendar ํด๋์ค์ after before ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ ์ฝ๊ฒ ๋น๊ต ๊ฐ๋ฅํฉ๋๋ค! |
@@ -0,0 +1,55 @@
+package christmas.domain.event;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+import christmas.domain.menu.Menu;
+import christmas.domain.menu.MenuType;
+
+public final class FreeGiftEvent implements Event {
+
+ private static final int CHRISTMAS_EVENT_MONTH = 12;
+ private static final int CHRISTMAS_EVENT_MIN_DAY = 1;
+ private static final int CHRISTMAS_EVENT_MAX_DAY = 31;
+ private static final int CHRISTMAS_EVENT_FREE_STANDARD = 120_000;
+ private static final String CHRISTMAS_EVENT_FREE_QUANTITY = "1";
+ private static final String FREE_GIFT_EVENT_TITLE = "์ฆ์ ์ด๋ฒคํธ";
+ private Menu benefitGift = new Menu(MenuType.EMPTY.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY);
+ private int benefit;
+
+ @Override
+ public boolean isApplicable(Date date) {
+ return isEventPeriod(date);
+ }
+
+ private static boolean isEventPeriod(Date date) {
+ return date.getMonth() == CHRISTMAS_EVENT_MONTH &&
+ date.getDay() >= CHRISTMAS_EVENT_MIN_DAY
+ && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY;
+ }
+
+ @Override
+ public int calculateDiscount(Date date, Order order) {
+ int beforeMoney = order.getBeforeMoney();
+ if (beforeMoney >= CHRISTMAS_EVENT_FREE_STANDARD) {
+ MenuType champagne = MenuType.CHAMPAGNE;
+ benefitGift = new Menu(champagne.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY);
+ benefit = champagne.getPrice();
+ }
+ return benefit;
+ }
+
+ public Menu getBenefitGift() {
+ return benefitGift;
+ }
+
+ @Override
+ public int getBenefit() {
+ return benefit;
+ }
+
+ @Override
+ public String getEventName() {
+ return FREE_GIFT_EVENT_TITLE;
+ }
+
+} | Java | ์์ฑ์๋ก DI ์ฃผ์
๋ฐ์ผ์๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,51 @@
+package christmas.controller;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+import christmas.domain.calendar.Planner;
+import christmas.domain.menu.Menus;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+public class UtecoRestaurantController {
+
+ public void startOperation() {
+ Date date = inputDate();
+ Menus menus = inputMenus();
+ Order order = new Order(menus);
+ Planner planner = new Planner(date, order);
+
+ OutputView.printBasicPreview(date, menus, order);
+ OutputView.printPlanner(order, planner);
+ }
+
+ private static Date inputDate() {
+ OutputView.printRestaurantIntro();
+ Date date = createDate();
+ return date;
+ }
+
+ private static Date createDate() {
+ try {
+ return new Date(InputView.inputDate());
+ } catch (IllegalArgumentException e) {
+ OutputView.printException(e);
+ return createDate();
+ }
+ }
+
+ private static Menus inputMenus() {
+ OutputView.printRestaurantMenu();
+ Menus menus = createMenus();
+ return menus;
+ }
+
+ private static Menus createMenus() {
+ try {
+ return InputView.inputOrderMenu();
+ } catch (IllegalArgumentException e) {
+ OutputView.printException(e);
+ return createMenus();
+ }
+ }
+} | Java | ์ ๋ ๋น์ทํ๊ฒ ๊ตฌํํ์
จ๋ค์ ! ๋ค๋ง View๋ฅผ ์คํํฑ์ผ๋ก ๊ด๋ฆฌํ์ ์ด์ ๊ฐ์์๊น์? |
@@ -0,0 +1,65 @@
+package christmas.domain.calendar;
+
+import java.time.DayOfWeek;
+import java.time.LocalDate;
+import java.time.format.TextStyle;
+import java.util.Locale;
+
+public class Date {
+ private static final int VISIT_YEAR = 2023;
+ private static final int VISIT_MONTH = 12;
+ private Year year;
+ private Month month;
+ private Day day;
+ private String dayFormat;
+
+ public Date(int day) {
+ this.year = new Year(VISIT_YEAR);
+ this.month = new Month(VISIT_MONTH);
+ this.day = new Day(day);
+ this.dayFormat = createVisitingDayFormat();
+ }
+
+ public String createVisitingDayFormat() {
+ LocalDate visitDay = createDateTimeFormat();
+ DayOfWeek dayOfWeek = visitDay.getDayOfWeek();
+ return dayOfWeek.getDisplayName(TextStyle.FULL, Locale.KOREA);
+ }
+
+ public boolean isWeekend() {
+ LocalDate visitDay = createDateTimeFormat();
+ DayOfWeek dayOfWeek = visitDay.getDayOfWeek();
+ return dayOfWeek == DayOfWeek.FRIDAY || dayOfWeek == DayOfWeek.SATURDAY;
+ }
+
+ public boolean isSunday() {
+ LocalDate visitDay = createDateTimeFormat();
+ DayOfWeek dayOfWeek = visitDay.getDayOfWeek();
+ return dayOfWeek == DayOfWeek.SUNDAY;
+ }
+
+ public LocalDate createDateTimeFormat() {
+ return LocalDate.of(getYear(), getMonth(), getDay());
+ }
+
+ @Override
+ public String toString() {
+ return String.format("%s์ %s์ผ", month.month(), day.day());
+ }
+
+ public int getYear() {
+ return year.year();
+ }
+
+ public int getMonth() {
+ return month.month();
+ }
+
+ public int getDay() {
+ return day.day();
+ }
+
+ public String getDayFormat() {
+ return dayFormat;
+ }
+} | Java | ๊ฐํํ๊ณ ๊ฐ๋๋ค.. ! ๋ ์ง๋ฅผ ์ด๋ฐ์์ผ๋ก ๊ด๋ฆฌํ ์๋์๋ค์! |
@@ -0,0 +1,48 @@
+package christmas.domain.event;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+import java.time.LocalDate;
+import java.time.Period;
+
+public final class ChristmasDailyDiscountEvent implements Event {
+
+ private static final int CHRISTMAS_EVENT_YEAR = 2023;
+ private static final int CHRISTMAS_EVENT_MONTH = 12;
+ private static final int CHRISTMAS_EVENT_MIN_DAY = 1;
+ private static final int CHRISTMAS_EVENT_MAX_DAY = 25;
+ private static final int CHRISTMAS_EVENT_START_MONEY = 1_000;
+ private static final int CHRISTMAS_EVENT_DISCOUNT_UNIT = 100;
+ private static final String CHRISTMAS_DAILY_DISCOUNT_EVENT_TITLE = "ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ";
+ private int benefit;
+
+ @Override
+ public boolean isApplicable(Date date) {
+ return isEventPeriod(date);
+ }
+
+ private static boolean isEventPeriod(Date date) {
+ return date.getMonth() == CHRISTMAS_EVENT_MONTH &&
+ date.getDay() >= CHRISTMAS_EVENT_MIN_DAY
+ && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY;
+ }
+
+ @Override
+ public int calculateDiscount(Date date, Order order) {
+ LocalDate startDay = LocalDate.of(CHRISTMAS_EVENT_YEAR, CHRISTMAS_EVENT_MONTH, CHRISTMAS_EVENT_MIN_DAY);
+ LocalDate visitDay = date.createDateTimeFormat();
+ Period period = Period.between(startDay, visitDay);
+ benefit = CHRISTMAS_EVENT_START_MONEY + CHRISTMAS_EVENT_DISCOUNT_UNIT * period.getDays();
+ return benefit;
+ }
+
+ @Override
+ public int getBenefit() {
+ return benefit;
+ }
+
+ @Override
+ public String getEventName() {
+ return CHRISTMAS_DAILY_DISCOUNT_EVENT_TITLE;
+ }
+} | Java | ์กฐ๊ธ ์ต์ง์ค๋ฌ์ด ์ง์ ์ด๊ธด ํ์ง๋ง, &&๋ฅผ ์์ผ๋ก ๋บด๋ ค๋ฉด ๋ชจ๋ ๋ค ๋นผ๋ฉด ์ข์๊ฒ ๊ฐ์์!
date.getMonth() == CHRISTMAS_EVENT_MONTH
&& date.getDay() >= CHRISTMAS_EVENT_MIN_DAY
&& date.getDay() <= CHRISTMAS_EVENT_MAX_DAY; |
@@ -0,0 +1,18 @@
+package christmas.domain.event;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+
+public sealed interface Event permits ChristmasDailyDiscountEvent, WeekdayDiscountEvent, WeekendDiscountEvent,
+ SpecialDiscountEvent,
+ FreeGiftEvent {
+
+ boolean isApplicable(Date date);
+
+ int calculateDiscount(Date date, Order order);
+
+ int getBenefit();
+
+ String getEventName();
+
+} | Java | sealed ํค์๋๋ฅผ ์ฒ์๋ด์ ์ ๊ธฐํ๋ค์..! ์ ๋ ๊ณต๋ถํด๋ด์ผ๊ฒ ์ต๋๋ค |
@@ -0,0 +1,55 @@
+package christmas.domain.event;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+import christmas.domain.menu.Menu;
+import christmas.domain.menu.MenuType;
+
+public final class FreeGiftEvent implements Event {
+
+ private static final int CHRISTMAS_EVENT_MONTH = 12;
+ private static final int CHRISTMAS_EVENT_MIN_DAY = 1;
+ private static final int CHRISTMAS_EVENT_MAX_DAY = 31;
+ private static final int CHRISTMAS_EVENT_FREE_STANDARD = 120_000;
+ private static final String CHRISTMAS_EVENT_FREE_QUANTITY = "1";
+ private static final String FREE_GIFT_EVENT_TITLE = "์ฆ์ ์ด๋ฒคํธ";
+ private Menu benefitGift = new Menu(MenuType.EMPTY.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY);
+ private int benefit;
+
+ @Override
+ public boolean isApplicable(Date date) {
+ return isEventPeriod(date);
+ }
+
+ private static boolean isEventPeriod(Date date) {
+ return date.getMonth() == CHRISTMAS_EVENT_MONTH &&
+ date.getDay() >= CHRISTMAS_EVENT_MIN_DAY
+ && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY;
+ }
+
+ @Override
+ public int calculateDiscount(Date date, Order order) {
+ int beforeMoney = order.getBeforeMoney();
+ if (beforeMoney >= CHRISTMAS_EVENT_FREE_STANDARD) {
+ MenuType champagne = MenuType.CHAMPAGNE;
+ benefitGift = new Menu(champagne.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY);
+ benefit = champagne.getPrice();
+ }
+ return benefit;
+ }
+
+ public Menu getBenefitGift() {
+ return benefitGift;
+ }
+
+ @Override
+ public int getBenefit() {
+ return benefit;
+ }
+
+ @Override
+ public String getEventName() {
+ return FREE_GIFT_EVENT_TITLE;
+ }
+
+} | Java | ์ด ๋ถ๋ถ์ ์ธํฐํ์ด์ค ๊ตฌํ์ฒด๊ฐ ๋ชจ๋ ๋์ผํ ๋ก์ง์ ๊ฐ๊ธฐ ๋๋ฌธ์ Event๋ฅผ ์ถ์ํด๋์ค๋ก ๊ด๋ฆฌํ๋ฉด ์ฝ๋ ์ค๋ณต์ ์ ๊ฑฐํ ์ ์์ด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,65 @@
+package christmas.domain.calendar;
+
+import java.time.DayOfWeek;
+import java.time.LocalDate;
+import java.time.format.TextStyle;
+import java.util.Locale;
+
+public class Date {
+ private static final int VISIT_YEAR = 2023;
+ private static final int VISIT_MONTH = 12;
+ private Year year;
+ private Month month;
+ private Day day;
+ private String dayFormat;
+
+ public Date(int day) {
+ this.year = new Year(VISIT_YEAR);
+ this.month = new Month(VISIT_MONTH);
+ this.day = new Day(day);
+ this.dayFormat = createVisitingDayFormat();
+ }
+
+ public String createVisitingDayFormat() {
+ LocalDate visitDay = createDateTimeFormat();
+ DayOfWeek dayOfWeek = visitDay.getDayOfWeek();
+ return dayOfWeek.getDisplayName(TextStyle.FULL, Locale.KOREA);
+ }
+
+ public boolean isWeekend() {
+ LocalDate visitDay = createDateTimeFormat();
+ DayOfWeek dayOfWeek = visitDay.getDayOfWeek();
+ return dayOfWeek == DayOfWeek.FRIDAY || dayOfWeek == DayOfWeek.SATURDAY;
+ }
+
+ public boolean isSunday() {
+ LocalDate visitDay = createDateTimeFormat();
+ DayOfWeek dayOfWeek = visitDay.getDayOfWeek();
+ return dayOfWeek == DayOfWeek.SUNDAY;
+ }
+
+ public LocalDate createDateTimeFormat() {
+ return LocalDate.of(getYear(), getMonth(), getDay());
+ }
+
+ @Override
+ public String toString() {
+ return String.format("%s์ %s์ผ", month.month(), day.day());
+ }
+
+ public int getYear() {
+ return year.year();
+ }
+
+ public int getMonth() {
+ return month.month();
+ }
+
+ public int getDay() {
+ return day.day();
+ }
+
+ public String getDayFormat() {
+ return dayFormat;
+ }
+} | Java | ์ ๋ ํ์ ๋ฐฐ์ฐ๊ณ ๊ฐ๋๋ค ์ ๋ ๊ทธ๋ฅ ์ด๋ฒคํธ๊ฐ 12์ ํ์ ์ด๋ผ์ ๊ทธ๋ฅ ๋๋จธ์ง์ฐ์ฐ์ผ๋ก ํ๋๋ฐ.. ๋ฐ์ฑํฉ๋๋ค! |
@@ -0,0 +1,55 @@
+package christmas.domain.event;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+import christmas.domain.menu.Menu;
+import christmas.domain.menu.MenuType;
+
+public final class FreeGiftEvent implements Event {
+
+ private static final int CHRISTMAS_EVENT_MONTH = 12;
+ private static final int CHRISTMAS_EVENT_MIN_DAY = 1;
+ private static final int CHRISTMAS_EVENT_MAX_DAY = 31;
+ private static final int CHRISTMAS_EVENT_FREE_STANDARD = 120_000;
+ private static final String CHRISTMAS_EVENT_FREE_QUANTITY = "1";
+ private static final String FREE_GIFT_EVENT_TITLE = "์ฆ์ ์ด๋ฒคํธ";
+ private Menu benefitGift = new Menu(MenuType.EMPTY.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY);
+ private int benefit;
+
+ @Override
+ public boolean isApplicable(Date date) {
+ return isEventPeriod(date);
+ }
+
+ private static boolean isEventPeriod(Date date) {
+ return date.getMonth() == CHRISTMAS_EVENT_MONTH &&
+ date.getDay() >= CHRISTMAS_EVENT_MIN_DAY
+ && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY;
+ }
+
+ @Override
+ public int calculateDiscount(Date date, Order order) {
+ int beforeMoney = order.getBeforeMoney();
+ if (beforeMoney >= CHRISTMAS_EVENT_FREE_STANDARD) {
+ MenuType champagne = MenuType.CHAMPAGNE;
+ benefitGift = new Menu(champagne.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY);
+ benefit = champagne.getPrice();
+ }
+ return benefit;
+ }
+
+ public Menu getBenefitGift() {
+ return benefitGift;
+ }
+
+ @Override
+ public int getBenefit() {
+ return benefit;
+ }
+
+ @Override
+ public String getEventName() {
+ return FREE_GIFT_EVENT_TITLE;
+ }
+
+} | Java | ์ ๋ ์ฒ์์ ์ด๋ ๊ฒ ํ๋๋ฐ ์๊ตฌ์ฌํญ์ ์ฝ์ด๋ณด๋ ์ฆ์ ์ด๋ฒคํธ์๋ ํ ์ธ์ด๋ผ๋ ๊ฐ๋
์ด ์์ด์ ์ ๋ ํํ๊ธ์ก๊ณ์ฐ์ผ๋ก ๋ฐ๊ฟจ์ด์..! |
@@ -0,0 +1,51 @@
+package christmas.domain.menu;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public enum MenuGroup {
+ APPETIZER("์ํผํ์ด์ ", Arrays.asList(MenuType.BUTTON_MUSHROOM_SOUP, MenuType.TAPAS, MenuType.CAESAR_SALAD)),
+ MAIN("๋ฉ์ธ", Arrays.asList(MenuType.T_BONE_STEAK, MenuType.BARBECUE_RIBS, MenuType.SEAFOOD_PASTA,
+ MenuType.CHRISTMAS_PASTA)),
+ DESSERT("๋์ ํธ", Arrays.asList(MenuType.CHOCOLATE_CAKE, MenuType.ICE_CREAM)),
+ BEVERAGE("์๋ฃ", Arrays.asList(MenuType.ZERO_COLA, MenuType.RED_WINE, MenuType.CHAMPAGNE)),
+ EMPTY("์์", Collections.EMPTY_LIST);
+
+ private final String title;
+ private final List<MenuType> menuList;
+
+ MenuGroup(String title, List<MenuType> menuList) {
+ this.title = title;
+ this.menuList = menuList;
+ }
+
+ public static MenuGroup findByMenuType(MenuType menuType) {
+ return Arrays.stream(MenuGroup.values())
+ .filter(menuGroup -> menuGroup.hasMenuName(menuType))
+ .findAny()
+ .orElse(EMPTY);
+ }
+
+ private boolean hasMenuName(MenuType menuType) {
+ return menuList.stream()
+ .anyMatch(menu -> menu == menuType);
+ }
+
+ @Override
+ public String toString() {
+ return "<" + title + ">\n" +
+ menuList.stream()
+ .map(MenuType::toString)
+ .collect(Collectors.joining(", "));
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public List<MenuType> getMenuList() {
+ return menuList;
+ }
+} | Java | ์ค ์ด๋ฐ์์ผ๋ก ๋ฉ๋ด๋ฅผ ์์์ฒ๋ฆฌํ์
จ๊ตฐ์ ๋ฆฌ์คํธ๋ก ๋ด๋ ๋ฐฉ๋ฒ๋ ์๋ค์! |
@@ -0,0 +1,53 @@
+package christmas.domain.event;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+import christmas.domain.menu.MenuGroup;
+import christmas.domain.menu.MenuType;
+
+public final class WeekdayDiscountEvent implements Event {
+
+ private static final int CHRISTMAS_EVENT_MONTH = 12;
+ private static final int CHRISTMAS_EVENT_MIN_DAY = 1;
+ private static final int CHRISTMAS_EVENT_MAX_DAY = 31;
+ private static final int CHRISTMAS_EVENT_DESERT_DISCOUNT = 2_023;
+ private static final String WEEKDAY_DISCOUNT_EVENT_TITLE = "ํ์ผ ํ ์ธ";
+ private int benefit;
+
+ @Override
+ public boolean isApplicable(Date date) {
+ return isEventPeriod(date) && !date.isWeekend();
+ }
+
+ private static boolean isEventPeriod(Date date) {
+ return date.getMonth() == CHRISTMAS_EVENT_MONTH &&
+ date.getDay() >= CHRISTMAS_EVENT_MIN_DAY
+ && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY;
+ }
+
+ @Override
+ public int calculateDiscount(Date date, Order order) {
+ benefit = order.getMenus().menuList().stream()
+ .mapToInt(menu -> {
+ MenuType menuType = MenuType.findByMenuName(menu.getMenuName());
+ MenuGroup menuGroup = MenuGroup.findByMenuType(menuType);
+ int menuQuantity = menu.getMenuQuantity();
+ if (menuGroup == MenuGroup.DESSERT) {
+ return CHRISTMAS_EVENT_DESERT_DISCOUNT * menuQuantity;
+ }
+ return 0;
+ })
+ .sum();
+ return benefit;
+ }
+
+ @Override
+ public int getBenefit() {
+ return benefit;
+ }
+
+ @Override
+ public String getEventName() {
+ return WEEKDAY_DISCOUNT_EVENT_TITLE;
+ }
+} | Java | ํฌ๋ฆฌ์ค๋ง์ค ํ์ผ ์ด๋ฒคํธ๊ฐ ์๊ตฌ ์ฌํญ์ ๋ฐ๋ผ ๋ณ๊ฒฝ์ด ๋๋ ๊ธฐ๊ฐ์ด๋ผ
12์์ ๋ํ ์์์ฒ๋ฆฌ๋ก ๊ธฐ๊ฐ์ ํํํ์ต๋๋ค ! |
@@ -0,0 +1,55 @@
+package christmas.domain.event;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+import christmas.domain.menu.Menu;
+import christmas.domain.menu.MenuType;
+
+public final class FreeGiftEvent implements Event {
+
+ private static final int CHRISTMAS_EVENT_MONTH = 12;
+ private static final int CHRISTMAS_EVENT_MIN_DAY = 1;
+ private static final int CHRISTMAS_EVENT_MAX_DAY = 31;
+ private static final int CHRISTMAS_EVENT_FREE_STANDARD = 120_000;
+ private static final String CHRISTMAS_EVENT_FREE_QUANTITY = "1";
+ private static final String FREE_GIFT_EVENT_TITLE = "์ฆ์ ์ด๋ฒคํธ";
+ private Menu benefitGift = new Menu(MenuType.EMPTY.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY);
+ private int benefit;
+
+ @Override
+ public boolean isApplicable(Date date) {
+ return isEventPeriod(date);
+ }
+
+ private static boolean isEventPeriod(Date date) {
+ return date.getMonth() == CHRISTMAS_EVENT_MONTH &&
+ date.getDay() >= CHRISTMAS_EVENT_MIN_DAY
+ && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY;
+ }
+
+ @Override
+ public int calculateDiscount(Date date, Order order) {
+ int beforeMoney = order.getBeforeMoney();
+ if (beforeMoney >= CHRISTMAS_EVENT_FREE_STANDARD) {
+ MenuType champagne = MenuType.CHAMPAGNE;
+ benefitGift = new Menu(champagne.getTitle(), CHRISTMAS_EVENT_FREE_QUANTITY);
+ benefit = champagne.getPrice();
+ }
+ return benefit;
+ }
+
+ public Menu getBenefitGift() {
+ return benefitGift;
+ }
+
+ @Override
+ public int getBenefit() {
+ return benefit;
+ }
+
+ @Override
+ public String getEventName() {
+ return FREE_GIFT_EVENT_TITLE;
+ }
+
+} | Java | ๊ธฐ๋ฅ์ ๊ฑฐ์ ๋ง๋ฌด๋ฆฌํ๊ณ ์ถ๋ ฅ๋ฌธ์ ๊ตฌํํ ๋,
์ฆ์ ์ํ์ด ์์ ๊ฒฝ์ฐ๋ ์ฆ์ ํ ๊ธ์ก ์๊ตฌ์ฌํญ์ if ์กฐ๊ฑด์ ๋์ง ์๊ณ
Menu์ Empty, ์์ ์ฒ๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ์ฒ๋ฆฌํ๋ ค๊ณ ๋ง๋ค๋ค๊ฐ ํ๋์ฝ๋ฉ์ด ๋ ๋ถ๋ถ์ธ ๊ฒ ๊ฐ๋ค์ !
์์ฑ์๋ก ๋ฐ์์ผ๋ฉด ๋ ์ข์์ ๊ฒ ๊ฐ์ต๋๋ค :) |
@@ -0,0 +1,53 @@
+package christmas.domain.event;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+import christmas.domain.menu.MenuGroup;
+import christmas.domain.menu.MenuType;
+
+public final class WeekendDiscountEvent implements Event {
+ private static final int CHRISTMAS_EVENT_MONTH = 12;
+ private static final int CHRISTMAS_EVENT_MIN_DAY = 1;
+ private static final int CHRISTMAS_EVENT_MAX_DAY = 31;
+ private static final int CHRISTMAS_EVENT_DESERT_DISCOUNT = 2_023;
+ private static final String WEEKEND_DISCOUNT_EVENT_TITLE = "์ฃผ๋ง ํ ์ธ";
+ private int benefit;
+
+ @Override
+ public boolean isApplicable(Date date) {
+ return isEventPeriod(date) &&
+ date.isWeekend();
+ }
+
+ private static boolean isEventPeriod(Date date) {
+ return date.getMonth() == CHRISTMAS_EVENT_MONTH &&
+ date.getDay() >= CHRISTMAS_EVENT_MIN_DAY
+ && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY;
+ }
+
+ @Override
+ public int calculateDiscount(Date date, Order order) {
+ benefit = order.getMenus().menuList().stream()
+ .mapToInt(menu -> {
+ MenuType menuType = MenuType.findByMenuName(menu.getMenuName());
+ MenuGroup menuGroup = MenuGroup.findByMenuType(menuType);
+ int menuQuantity = menu.getMenuQuantity();
+ if (menuGroup == MenuGroup.MAIN) {
+ return CHRISTMAS_EVENT_DESERT_DISCOUNT * menuQuantity;
+ }
+ return 0;
+ })
+ .sum();
+ return benefit;
+ }
+
+ @Override
+ public int getBenefit() {
+ return benefit;
+ }
+
+ @Override
+ public String getEventName() {
+ return WEEKEND_DISCOUNT_EVENT_TITLE;
+ }
+} | Java | 12์ ์ด๋ฒคํธ ๋ฟ๋ง์๋๋ผ 12์์์ 1์๊น์ง ์ฐ์ฅ์ด ๋๋ค๊ณ ๊ฐ์ ํ๋ฉด,
Calendar์ LocalDate ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ์ข์์ ๊ฒ ๊ฐ์ต๋๋ค :) ๋ฆฌ๋ทฐ ๊ฐ์ฌํฉ๋๋ค |
@@ -0,0 +1,51 @@
+package christmas.controller;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+import christmas.domain.calendar.Planner;
+import christmas.domain.menu.Menus;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+public class UtecoRestaurantController {
+
+ public void startOperation() {
+ Date date = inputDate();
+ Menus menus = inputMenus();
+ Order order = new Order(menus);
+ Planner planner = new Planner(date, order);
+
+ OutputView.printBasicPreview(date, menus, order);
+ OutputView.printPlanner(order, planner);
+ }
+
+ private static Date inputDate() {
+ OutputView.printRestaurantIntro();
+ Date date = createDate();
+ return date;
+ }
+
+ private static Date createDate() {
+ try {
+ return new Date(InputView.inputDate());
+ } catch (IllegalArgumentException e) {
+ OutputView.printException(e);
+ return createDate();
+ }
+ }
+
+ private static Menus inputMenus() {
+ OutputView.printRestaurantMenu();
+ Menus menus = createMenus();
+ return menus;
+ }
+
+ private static Menus createMenus() {
+ try {
+ return InputView.inputOrderMenu();
+ } catch (IllegalArgumentException e) {
+ OutputView.printException(e);
+ return createMenus();
+ }
+ }
+} | Java | Controller๊ฐ ์ง์ InputView์ OutputView๋ฅผ ์ฌ์ฉํ๋ค๋ฉด
์ปจํธ๋กค๋ฌ ๋ถ๋ถ์ด ๋ ๊น๋ํด์ง์ง ์์๊น.. ๋ผ๋ ๊ฐ์ธ์ ์ธ ์๊ฒฌ์ด ๋ค์ด๊ฐ ์์ต๋๋ค
DI๋ฅผ ๋ฐ๋ ๋ฐฉ์์ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ฐ์ฒด์งํฅ ์ค๊ณ์ ๋ ๋ง๋ ๋ฐฉ์์ด์๋ ๊ฒ ๊ฐ๋ค์ ๐ฅฒ |
@@ -0,0 +1,48 @@
+package christmas.domain.event;
+
+import christmas.domain.calendar.Date;
+import christmas.domain.calendar.Order;
+import java.time.LocalDate;
+import java.time.Period;
+
+public final class ChristmasDailyDiscountEvent implements Event {
+
+ private static final int CHRISTMAS_EVENT_YEAR = 2023;
+ private static final int CHRISTMAS_EVENT_MONTH = 12;
+ private static final int CHRISTMAS_EVENT_MIN_DAY = 1;
+ private static final int CHRISTMAS_EVENT_MAX_DAY = 25;
+ private static final int CHRISTMAS_EVENT_START_MONEY = 1_000;
+ private static final int CHRISTMAS_EVENT_DISCOUNT_UNIT = 100;
+ private static final String CHRISTMAS_DAILY_DISCOUNT_EVENT_TITLE = "ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ";
+ private int benefit;
+
+ @Override
+ public boolean isApplicable(Date date) {
+ return isEventPeriod(date);
+ }
+
+ private static boolean isEventPeriod(Date date) {
+ return date.getMonth() == CHRISTMAS_EVENT_MONTH &&
+ date.getDay() >= CHRISTMAS_EVENT_MIN_DAY
+ && date.getDay() <= CHRISTMAS_EVENT_MAX_DAY;
+ }
+
+ @Override
+ public int calculateDiscount(Date date, Order order) {
+ LocalDate startDay = LocalDate.of(CHRISTMAS_EVENT_YEAR, CHRISTMAS_EVENT_MONTH, CHRISTMAS_EVENT_MIN_DAY);
+ LocalDate visitDay = date.createDateTimeFormat();
+ Period period = Period.between(startDay, visitDay);
+ benefit = CHRISTMAS_EVENT_START_MONEY + CHRISTMAS_EVENT_DISCOUNT_UNIT * period.getDays();
+ return benefit;
+ }
+
+ @Override
+ public int getBenefit() {
+ return benefit;
+ }
+
+ @Override
+ public String getEventName() {
+ return CHRISTMAS_DAILY_DISCOUNT_EVENT_TITLE;
+ }
+} | Java | ์๋๋๋ค ! ์ฐพ์์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค ๐๐๐
์ฝ๋๋ฅผ ์งํํ๋ค๊ฐ ์ค ๋ฐ๊ฟ์ ๋ณด์ง ๋ชปํ๊ณ ์ฝ๋ ์ผ๊ด์ฑ์ด ์ด๊ธ๋์๋ค์ ! ๋ฆฌ๋ทฐ ๊ฐ์ฌํฉ๋๋ค :) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.