code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,63 @@
+.postList {
+ margin: 0 auto;
+ display: flex;
+ width: 576px;
+ height: 80vh;
+ padding: 40px 24px;
+ flex-direction: column;
+ align-items: center;
+ border-radius: 16px;
+ border: 1px solid #e5e5e5;
+ background: white;
+
+ .posts {
+ width: 100%;
+ overflow-y: scroll;
+ flex: 1;
+ cursor: pointer;
+ }
+ & ::-webkit-scrollbar {
+ display: none;
+ }
+
+ .buttonPlace {
+ display: flex;
+ flex-direction: column;
+ width: 528px;
+ height: 56px;
+ margin-top: 28px;
+ align-items: flex-end;
+ gap: 8px;
+ align-self: stretch;
+
+ a {
+ text-decoration: none;
+ }
+
+ button {
+ display: flex;
+ width: 120px;
+ height: 56px;
+ padding: 0px 34px;
+ justify-content: center;
+ align-items: center;
+ border-radius: 6px;
+ background-color: #2d71f7;
+ color: #fff;
+ text-align: center;
+ font-feature-settings:
+ 'clig' off,
+ 'liga' off;
+ font-family: Apple SD Gothic Neo;
+ font-size: 16px;
+ font-style: normal;
+ font-weight: 800;
+ line-height: normal;
+
+ &:hover {
+ border-radius: 6px;
+ background: #083e7f;
+ }
+ }
+ }
+} | Unknown | ๋ถ๋ชจ ์ ํ์(`&`)๋ฅผ ์ฌ์ฉํด์ ํด๋น ์์์๋ง ์ ์ฉ๋๋ ์คํ์ผ์์ ์ข ๋ ๊น๋ํ๊ฒ ๋ช
์ํ ์ ์์ต๋๋ค!
```suggestion
.posts {
overflow-y: scroll;
flex: 1;
&::-webkit-scrollbar {
display: none;
}
}
``` |
@@ -1,7 +1,18 @@
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@100;300;400;500;700;900&display=swap');
-
* {
box-sizing: border-box;
font-family: 'Noto Sans KR', sans-serif;
-}
\ No newline at end of file
+}
+
+body {
+ width: 100vw;
+ height: 100vh;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+}
+
+button {
+ cursor: pointer;
+} | Unknown | ๊ณต์ฉ ํ์ผ์ ์์ ํด์ ์ฌ๋ ค์ฃผ์
จ๋๋ฐ, ๊ณต์ฉ ํ์ผ์ ์์ ์
1. ํ์๊ณผ์ ์ถฉ๋ถํ ์์ ํ์
2. ํด๋น ํ์ผ๋ง ์์ ํ๋ ๋ด์ฉ์ PR
์ ์ฌ๋ ค์ฃผ์
์ผ ํฉ๋๋ค! |
@@ -0,0 +1,17 @@
+package lotto
+
+import lotto.service.LottoService
+import lotto.view.Input
+import lotto.view.Output
+
+fun main() {
+ val money = Input.money()
+ val randomNums = Input.getRandomLottoNumsPerMoney(money)
+ val winNums = Input.winNums()
+
+ val lottoService = LottoService()
+ val lottoRanks = lottoService.getLottoRanks(randomNums, winNums)
+ val revenueRate = lottoService.getRevenueRate(money.toDouble(), lottoRanks)
+
+ Output.result(lottoRanks, revenueRate)
+} | Kotlin | VO์ ๋ํด ์์๋ณด๊ณ ์์๊ฐ์ ํฌ์ฅํด๋ณด๋ฉด ์ด๋จ๊น์? [์ฐธ๊ณ ](https://tecoble.techcourse.co.kr/post/2020-05-29-wrap-primitive-type/) |
@@ -0,0 +1,36 @@
+package lotto.view
+
+import lotto.domain.LottoTicket.Companion.TICKET_PRICE
+
+object Input {
+ fun money(): Int {
+ println("๊ตฌ์
๊ธ์ก์ ์
๋ ฅํด ์ฃผ์ธ์.")
+ return readln().toInt()
+ }
+
+ fun getRandomLottoNumsPerMoney(money: Int): List<List<Int>> {
+ val ticketNumber = money.div(TICKET_PRICE)
+ println(ticketNumber)
+
+ return (1..ticketNumber)
+ .map {
+ val lottoNums = generateRandomLottoNums()
+ println(lottoNums)
+
+ lottoNums
+ }
+ }
+
+ fun winNums(): List<Int> {
+ println("์ง๋ ์ฃผ ๋น์ฒจ ๋ฒํธ๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.")
+ val split = readln().split(",")
+
+ return split.map { it.toInt() }
+ }
+
+ private fun generateRandomLottoNums(): List<Int> {
+ val lottoNumRange = 1..45
+
+ return lottoNumRange.shuffled().subList(0, 6)
+ }
+} | Kotlin | ์ค์ฒฉ ์ปฌ๋ ์
๋ณด๋ค๋ ์๋ฏธ๋ฅผ ๋ด์ ์ ์๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด๋ณด๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,36 @@
+package lotto.view
+
+import lotto.domain.LottoTicket.Companion.TICKET_PRICE
+
+object Input {
+ fun money(): Int {
+ println("๊ตฌ์
๊ธ์ก์ ์
๋ ฅํด ์ฃผ์ธ์.")
+ return readln().toInt()
+ }
+
+ fun getRandomLottoNumsPerMoney(money: Int): List<List<Int>> {
+ val ticketNumber = money.div(TICKET_PRICE)
+ println(ticketNumber)
+
+ return (1..ticketNumber)
+ .map {
+ val lottoNums = generateRandomLottoNums()
+ println(lottoNums)
+
+ lottoNums
+ }
+ }
+
+ fun winNums(): List<Int> {
+ println("์ง๋ ์ฃผ ๋น์ฒจ ๋ฒํธ๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.")
+ val split = readln().split(",")
+
+ return split.map { it.toInt() }
+ }
+
+ private fun generateRandomLottoNums(): List<Int> {
+ val lottoNumRange = 1..45
+
+ return lottoNumRange.shuffled().subList(0, 6)
+ }
+} | Kotlin | ๊ตฌ๋งคํ ํฐ์ผ์๋์ view์์ ๊ตฌํ๋๊ฒ์ด ์ ์ ํ ์์น์ผ์ง ๊ณ ๋ฏผํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,36 @@
+package lotto.view
+
+import lotto.domain.LottoTicket.Companion.TICKET_PRICE
+
+object Input {
+ fun money(): Int {
+ println("๊ตฌ์
๊ธ์ก์ ์
๋ ฅํด ์ฃผ์ธ์.")
+ return readln().toInt()
+ }
+
+ fun getRandomLottoNumsPerMoney(money: Int): List<List<Int>> {
+ val ticketNumber = money.div(TICKET_PRICE)
+ println(ticketNumber)
+
+ return (1..ticketNumber)
+ .map {
+ val lottoNums = generateRandomLottoNums()
+ println(lottoNums)
+
+ lottoNums
+ }
+ }
+
+ fun winNums(): List<Int> {
+ println("์ง๋ ์ฃผ ๋น์ฒจ ๋ฒํธ๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.")
+ val split = readln().split(",")
+
+ return split.map { it.toInt() }
+ }
+
+ private fun generateRandomLottoNums(): List<Int> {
+ val lottoNumRange = 1..45
+
+ return lottoNumRange.shuffled().subList(0, 6)
+ }
+} | Kotlin | ๋งค์ง๋๋ฒ๋ฅผ ์์ํํด๋ณด๋ฉด ์ด๋จ๊น์?! |
@@ -9,3 +9,8 @@
- [x] ๋ฌธ์์ด ๊ณ์ฐ๊ธฐ์ ์ซ์ ์ด์ธ์ ๊ฐ ๋๋ ์์๋ฅผ ์ ๋ฌํ๋ ๊ฒฝ์ฐ RuntimeException ์์ธ๋ฅผ throw ํ๋ค.
- [x] ์์๊ฐ์ ํฌ์ฅํ ๋๋ฉ์ธ๊ฐ์ฒด ๊ตฌํ
- [x] ์ฐ์ฐ์ ์ค๋ฒ๋ผ์ด๋ฉ ๊ตฌํ
+
+## ๋ก๋ (์๋) ๊ธฐ๋ฅ ์๊ตฌ์ฌํญ
+- [x] ๋ก๋ ๊ตฌ์
๊ธ์ก์ ์
๋ ฅํ๋ฉด ๊ตฌ์
๊ธ์ก์ ํด๋นํ๋ ๋ก๋๋ฅผ ๋ฐ๊ธํด์ผ ํ๋ค.
+ - [x] ๊ธ์ก์ 0 ์ด์์ด๋ค.
+- [x] ๋ก๋ 1์ฅ์ ๊ฐ๊ฒฉ์ 1000์์ด๋ค. | Unknown | ํด๋น validation์ ์ด๋์ ์ํํด์ฃผ๊ณ ์๋์?! |
@@ -0,0 +1,23 @@
+package lotto.service
+
+import lotto.domain.LottoTicketMachine
+import lotto.domain.Rank
+
+class LottoService {
+ fun getLottoRanks(nums: List<List<Int>>, winNums: List<Int>): List<Rank> {
+ return nums.map {
+ val issue = LottoTicketMachine.issue(it)
+ val winLottoTicket = LottoTicketMachine.issue(winNums)
+
+ Rank.determineLottoTicket(winLottoTicket, issue)
+ }
+ }
+
+ fun getRevenueRate(investment: Double, ranks: List<Rank>): Double {
+ val totalPrizeSum = ranks.sumOf {
+ it.prize
+ }.toDouble()
+
+ return totalPrizeSum / investment
+ }
+} | Kotlin | object ์ฌ์ฉ์ ๊ณ ๋ คํด๋ณด๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,17 @@
+package lotto.domain
+
+object LottoTicketMachine {
+ fun issue(nums: List<Int>): LottoTicket {
+ require(nums.size == 6) {
+ "๋ก๋ ๊ฐ์๋ฅผ ์๋ชป ์
๋ ฅํ์ต๋๋ค"
+ }
+
+ val lottoBox = LottoBox()
+
+ val lottoNums = nums.map {
+ lottoBox.getLottoNum(it)
+ }.toList()
+
+ return LottoTicket(lottoNums)
+ }
+} | Kotlin | issue๋ผ๋ ๋ค์ด๋ฐ์ ๋ถ์ด์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,24 @@
+package lotto.domain
+
+class LottoBox {
+ private val lottoNums: MutableMap<Int, LottoNum>
+
+ init {
+ val intRange = 1..45
+
+ lottoNums = intRange
+ .map { LottoNum(it) }
+ .associateBy { it.num }
+ .toMutableMap()
+ }
+
+ fun getLottoNum(num: Int): LottoNum {
+ val lottoNum = lottoNums[num]
+ require(lottoNum != null) {
+ "๋ก๋ ์ซ์๋ ์ค๋ณตํด์ ์
๋ ฅํ ์ ์
์ต๋๋ค"
+ }
+
+ lottoNums.remove(lottoNum.num)
+ return lottoNum
+ }
+} | Kotlin | LottoBox๋ง ๋ดค์๋ lottoNum์ด null์ด๋ผ๊ณ ํด์ ์ค๋ณต๋ ์ซ์๋ฅผ ์
๋ ฅํ๋ค๊ณ ๋ณด์ฅํ ์ ์์๊น์? |
@@ -0,0 +1,28 @@
+package lotto.domain
+
+class LottoNum(num: Int) {
+ val num: Int
+
+ init {
+ require(num in 1..45) {
+ "๋ก๋ ์ซ์ ๋ฒ์๋ 1 ~ 45 ์
๋๋ค."
+ }
+
+ this.num = num
+ }
+
+ override fun equals(other: Any?): Boolean {
+ if (this === other) return true
+ if (javaClass != other?.javaClass) return false
+
+ other as LottoNum
+
+ if (num != other.num) return false
+
+ return true
+ }
+
+ override fun hashCode(): Int {
+ return num
+ }
+} | Kotlin | inline class๋ฅผ ํ์ฉํด๋ด๋ ์ข์ ๊ฒ ๊ฐ์์!! :)
> ๋ํผํด๋์ค๋ก inline class์ value class ๋ฅผ ์ฌ์ฉํ๋๊ฑฐ ๊ฐ์๋ฐ์!
์ ์ดํด๊ฐ ์๊ฐ์์~
๋ญ๊ฐ ์ข์ ๊ธฐ๋ฅ๊ฐ๊ธฐ๋ ํ๋ฐ, ์ค๋ฌด์์ ์ด๋ป๊ฒ, ์ด๋ค์ํฉ์ ์ฌ์ฉํ๋ฉด ์ข์์ง ์ง๋ฌธ๋๋ฆฝ๋๋ค!
์ฌ์ค ์ ๋ ์ ์ฌ์ฉํ๋ค๊ณ ๋ง์๋๋ฆฌ๊ธฐ๋ ์ด๋ ต์ง๋ง, ์ ๋ ์๋์ ๊ฐ์ ๊ธฐ์ค์ ์ถฉ์กฑ์ํค๋ ๊ฒฝ์ฐ ์ธ๋ผ์ธ ํด๋์ค๋ฅผ ์ฌ์ฉํ๊ณ ์์ด์
* ํด๋น ๋ํผํ์
์ด ์ฌ๋ฌ๊ณณ์์ ํธ์ถ๋๋๊ฐ?
* ํด๋น ํด๋์ค๊ฐ data class์ ํน์ง์ ๊ฐ์ง๊ณ ์๋๊ฐ?
์ค๋ฌด์์ ์ ์ฉ์ ํ์๊ฐ ์๋๊ณ ํ์๋ค๋ง๋ค ์๊ฒฌ์ด ๋ค๋ฅผ ์ ์์ผ๋ ํ ๋ด๋ถ์ ์ผ๋ก๋ ์ด์ผ๊ธฐ๋ฅผ ํด๋ณด๊ณ ์ปจ๋ฒค์
์ ์ก์๊ฐ๋ณด๋๊ฒ๋ ๋ฐฉ๋ฒ์ผ ๊ฒ ๊ฐ์์!
[์ด๋ฐ ๊ธ](https://github.com/Kotlin/KEEP/blob/master/notes/value-classes.md)๋ ์ฝ์ด๋ณด๋ฉด ๋์์ด ๋ ๊ฒ ๊ฐ์์!! :) |
@@ -0,0 +1,28 @@
+package lotto.domain
+
+class LottoNum(num: Int) {
+ val num: Int
+
+ init {
+ require(num in 1..45) {
+ "๋ก๋ ์ซ์ ๋ฒ์๋ 1 ~ 45 ์
๋๋ค."
+ }
+
+ this.num = num
+ }
+
+ override fun equals(other: Any?): Boolean {
+ if (this === other) return true
+ if (javaClass != other?.javaClass) return false
+
+ other as LottoNum
+
+ if (num != other.num) return false
+
+ return true
+ }
+
+ override fun hashCode(): Int {
+ return num
+ }
+} | Kotlin | ์ฌ๊ธฐ๋ ๋งค์ง๋๋ฒ๋ฅผ ์์ํํด๋ณด๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,13 @@
+export class NetworkError extends Error {
+ constructor(message) {
+ super(message);
+ this.name = "NetworkError";
+ }
+}
+
+export class HttpError extends Error {
+ constructor(message) {
+ super(message);
+ this.name = "HttpError";
+ }
+} | JavaScript | ์ฌ์ฉ๋์ง ์๋ ์ฝ๋๊ฐ ๋จ์์๋ค์, ํน์ ์ด๋ค ์๋๋ก ๋ง๋์
จ์๊น์?! |
@@ -0,0 +1,39 @@
+function formatUrl(config) {
+ const { href, query } = config;
+ const searchParams = new URLSearchParams(query);
+
+ return `${href}?${searchParams.toString()}`;
+}
+
+function formatHeaders(headersRecord) {
+ const headers = new Headers();
+
+ headers.append("content-type", "application/json");
+ Object.entries(headersRecord).forEach(([key, value]) => {
+ headers.append(key, value);
+ });
+ return headers;
+}
+
+export async function createAPIRequest(request) {
+ const response = await fetch(
+ formatUrl({
+ href: request.url,
+ query: request.query ?? {},
+ }),
+ {
+ method: request.method || "GET",
+ headers: formatHeaders(request.headers),
+ body: request.body,
+ },
+ ).catch((error) => {
+ throw new Error("Network error: ", error);
+ });
+
+ if (!response.ok) {
+ throw new Error("Http error: ", response.status);
+ }
+
+ const data = !response.body ? null : await response.json();
+ return data;
+} | JavaScript | [MDN Error() ์์ฑ์](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Error/Error) ๋ฌธ์ ์ฒจ๋ถ๋๋ฆฝ๋๋ค!
error ๊ฐ์ฒด๋ ์ฒซ๋ฒ์งธ ์ธ์๋ก `message`๋ฅผ ๋ฐ์ผ๋ฉฐ, ๋๋ฒ์งธ ์ธ๋ฒ์งธ ์ธ์๊ฐ ์๊ธดํ์ง๋ง, ๋นํ์ค์
๋๋ค!
์ค์ ๋ก ์๋ฌ๋ฅผ ์ผ์ผ์ผ๋ณด๋ฉด alert์์ `Http error: ` , `Network error: `
ํด๋น ๋ฌธ์์ด๋ง ๋
ธ์ถ๋๋ ๊ฑธ ํ์ธํ์ค ์ ์์๊ฑฐ์์! |
@@ -0,0 +1,39 @@
+function formatUrl(config) {
+ const { href, query } = config;
+ const searchParams = new URLSearchParams(query);
+
+ return `${href}?${searchParams.toString()}`;
+}
+
+function formatHeaders(headersRecord) {
+ const headers = new Headers();
+
+ headers.append("content-type", "application/json");
+ Object.entries(headersRecord).forEach(([key, value]) => {
+ headers.append(key, value);
+ });
+ return headers;
+}
+
+export async function createAPIRequest(request) {
+ const response = await fetch(
+ formatUrl({
+ href: request.url,
+ query: request.query ?? {},
+ }),
+ {
+ method: request.method || "GET",
+ headers: formatHeaders(request.headers),
+ body: request.body,
+ },
+ ).catch((error) => {
+ throw new Error("Network error: ", error);
+ });
+
+ if (!response.ok) {
+ throw new Error("Http error: ", response.status);
+ }
+
+ const data = !response.body ? null : await response.json();
+ return data;
+} | JavaScript | ์ด ํ์ผ์ ๋ํ ์ ๋ฐ์ ์ธ ๋๋์ `formatUrl`, `formatHeaders` ํจ์์ ํ์์ฑ์ ๋ชจ๋ฅด๊ฒ ๋ค.. ์ด๊ธด ํฉ๋๋ค!
`createAPIRequest` ์ฒ๋ผ ์ ์ฒด์ ์ธ ์์ฒญ์ ๋ํ ๊ณตํต ํจ์๋ ์์ผ๋ฉด ๋น์ฐํ ์ข๋ค!
์ด๊ธดํ๋ฐ ํํ์ ๋ํด์๋ ์กฐ๊ธ ์์ฌ์ด ๋ถ๋ถ์ด ์๋ค๊ณ ๋๊ปด์ง๋๋ค!
์ฝ๋ฉํธ๋ก ํ๋ ํ๋ ์์ฑํด๋ณผ๊ฒ์! |
@@ -0,0 +1,39 @@
+function formatUrl(config) {
+ const { href, query } = config;
+ const searchParams = new URLSearchParams(query);
+
+ return `${href}?${searchParams.toString()}`;
+}
+
+function formatHeaders(headersRecord) {
+ const headers = new Headers();
+
+ headers.append("content-type", "application/json");
+ Object.entries(headersRecord).forEach(([key, value]) => {
+ headers.append(key, value);
+ });
+ return headers;
+}
+
+export async function createAPIRequest(request) {
+ const response = await fetch(
+ formatUrl({
+ href: request.url,
+ query: request.query ?? {},
+ }),
+ {
+ method: request.method || "GET",
+ headers: formatHeaders(request.headers),
+ body: request.body,
+ },
+ ).catch((error) => {
+ throw new Error("Network error: ", error);
+ });
+
+ if (!response.ok) {
+ throw new Error("Http error: ", response.status);
+ }
+
+ const data = !response.body ? null : await response.json();
+ return data;
+} | JavaScript | `headers.append("content-type", "application/json")`;
์ฌ์ค์ ์ญํ ์ ๋ชจ๋ ์์ฒญ์ ํด๋น ํค๋๋ฅผ ๋ํด์ฃผ๋ ์ญํ ๋ฐ์ ์๋๋ฐ
`createAPIRequest`์ headers์์ ํด์ฃผ๋ฉด ๋์ง ์์๊น ์ถ๋ค์!
```js
headers: {
...request.headers
'content-type': "application/json"
}
``` |
@@ -0,0 +1,39 @@
+function formatUrl(config) {
+ const { href, query } = config;
+ const searchParams = new URLSearchParams(query);
+
+ return `${href}?${searchParams.toString()}`;
+}
+
+function formatHeaders(headersRecord) {
+ const headers = new Headers();
+
+ headers.append("content-type", "application/json");
+ Object.entries(headersRecord).forEach(([key, value]) => {
+ headers.append(key, value);
+ });
+ return headers;
+}
+
+export async function createAPIRequest(request) {
+ const response = await fetch(
+ formatUrl({
+ href: request.url,
+ query: request.query ?? {},
+ }),
+ {
+ method: request.method || "GET",
+ headers: formatHeaders(request.headers),
+ body: request.body,
+ },
+ ).catch((error) => {
+ throw new Error("Network error: ", error);
+ });
+
+ if (!response.ok) {
+ throw new Error("Http error: ", response.status);
+ }
+
+ const data = !response.body ? null : await response.json();
+ return data;
+} | JavaScript | ๊ฐ์ฅ ๋จผ์ searchParams์ ์ฌ๋ถ์ ๊ด๊ณ ์์ด URL์ `?`๊ฐ ๋ถ์ต๋๋ค!
๊ทธ๋ฆฌ๊ณ `createAPIRequest`๋ณด๋ค ์ค์ ์์ฒญํ๋ ๊ณณ์์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ด๋จ๊น ์ถ๊ธดํฉ๋๋ค!
์ด๋ค ์๋ฏธ๋๋ฉด์!
fetch API๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ฌ๋ด๊ณ , ์ฌ์ฉ์ฒ์์ ์ด๋ฅผ ์์๋ณผ ์ ์์ผ๋ฉด ๋ ์ข๊ฒ ๋ค๋ ์๊ฐ์
๋๋ค.
`fetchPopularMovies`๋ fetch API์ ์ง์ ์ฌ์ฉํ ์ ์๋ ํํ์ `createAPIRequest`์ ์๋ง์ ์ธ์๋ฅผ ๋๊ฒจ์ค๋๋ค.
`createAPIRequest`๋ ์ด ์ธ์๋ฅผ ๊ฐ๊ณตํด์ fetch API์ ์ฌ์ฉํ ์ ์๋ ํํ๋ก ๋ณ๊ฒฝ์ ํ๊ณ ์๋๋ฐ์!
์ด ํจ์๋ฅผ ์ฌ์ฉํ๋ ์
์ฅ์์ `createAPIRequest`์ ๋์๊ณผ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ชจ๋ฅด๋๋ผ๋
fetch API์ request ์ ๋ณด์ ๋ง๋ ์ธ์๋ฅผ ๋๊ฒจ์ฃผ๋ฉด, ํ์ํ ์ถ๊ฐ์ ์ธ ์ฒ๋ฆฌ๋ฅผ ํด์ ์์ฒญ์ ํด์ค๋ค! ๊ฐ ๋ ์ ์์ ๊ฒ ๊ฐ์์~
๊ทธ๋ฐ ์๋ฏธ์์, fetch๋ ์ฒซ๋ฒ์งธ ์ธ์๋ก URL์ ๋ฐ์ผ๋, createAPIRequest์์ ์ฌ์ฉ๋๋ค๋ฉด,
createAPIRequest๋ ์ธ์๋ก fetch์ request ์ ๋ณด์ ๋์ผํ ํํ์ ์ธ์๋ฅผ ๋ฐ์ ์ ์๊ฒ ๋๊ณ , ์ฌ์ฉํ๋ ์
์ฅ์์ ์ถ๊ฐ์ ์ธ ์ฝ๋ ํ์ต์ด ํ์ํ๊ฒ๋ ๊ฒ ๊ฐ์์~ |
@@ -0,0 +1,39 @@
+function formatUrl(config) {
+ const { href, query } = config;
+ const searchParams = new URLSearchParams(query);
+
+ return `${href}?${searchParams.toString()}`;
+}
+
+function formatHeaders(headersRecord) {
+ const headers = new Headers();
+
+ headers.append("content-type", "application/json");
+ Object.entries(headersRecord).forEach(([key, value]) => {
+ headers.append(key, value);
+ });
+ return headers;
+}
+
+export async function createAPIRequest(request) {
+ const response = await fetch(
+ formatUrl({
+ href: request.url,
+ query: request.query ?? {},
+ }),
+ {
+ method: request.method || "GET",
+ headers: formatHeaders(request.headers),
+ body: request.body,
+ },
+ ).catch((error) => {
+ throw new Error("Network error: ", error);
+ });
+
+ if (!response.ok) {
+ throw new Error("Http error: ", response.status);
+ }
+
+ const data = !response.body ? null : await response.json();
+ return data;
+} | JavaScript | ์ฝ๋ฉํธ๊ฐ ๋ง์ด ๊ธธ์ด์ก๋ค์.. ใ
ใ
ใ
ใ
์์ฝํ์๋ฉด, ์ธํฐํ์ด์ค๋ฅผ ๋๋ฃ๊ฐ ๋ฐ๋ก ๋ฏ์ด๋ณด์ง ์์๋ ๋ฐ๋ก ์ดํดํ ์ ์๋ ํํ๋ก ์์ฑํ์..! ์
๋๋ค |
@@ -0,0 +1,39 @@
+function formatUrl(config) {
+ const { href, query } = config;
+ const searchParams = new URLSearchParams(query);
+
+ return `${href}?${searchParams.toString()}`;
+}
+
+function formatHeaders(headersRecord) {
+ const headers = new Headers();
+
+ headers.append("content-type", "application/json");
+ Object.entries(headersRecord).forEach(([key, value]) => {
+ headers.append(key, value);
+ });
+ return headers;
+}
+
+export async function createAPIRequest(request) {
+ const response = await fetch(
+ formatUrl({
+ href: request.url,
+ query: request.query ?? {},
+ }),
+ {
+ method: request.method || "GET",
+ headers: formatHeaders(request.headers),
+ body: request.body,
+ },
+ ).catch((error) => {
+ throw new Error("Network error: ", error);
+ });
+
+ if (!response.ok) {
+ throw new Error("Http error: ", response.status);
+ }
+
+ const data = !response.body ? null : await response.json();
+ return data;
+} | JavaScript | `data` ๋ณ์๋ฅผ ์์ฑํ ํ์ํ ์์ด ๋ฐ๋ก ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค์~ |
@@ -0,0 +1,37 @@
+import "./components/layout/app-header.js";
+import "./components/movie/movie-card.js";
+import "./components/movie/movie-list.js";
+import "./components/movie/movie-container.js";
+
+import { html } from "./utils/template.js";
+
+class App extends HTMLElement {
+ constructor() {
+ super();
+ this.attachShadow({ mode: "open" });
+ }
+
+ connectedCallback() {
+ this.render();
+ }
+
+ render() {
+ this.shadowRoot.innerHTML = this.template();
+ }
+
+ template() {
+ return html`
+ <style>
+ * {
+ padding-bottom: 48px;
+ }
+ </style>
+ <app-header></app-header>
+ <main>
+ <movie-container></movie-container>
+ </main>
+ `;
+ }
+}
+
+customElements.define("movie-app", App); | JavaScript | ์ ๊ฑฐ ํด๋ ๊ด์ฐฎ์ ์คํ์ผ ๊ฐ์๋ฐ ์ด๋ค ์๋ฏธ๊ฐ ์์๊น์? |
@@ -0,0 +1,2 @@
+export const html = String.raw;
+export const css = String.raw; | JavaScript | ๋ง์ฐฌ๊ฐ์ง๋ก ์ฌ์ฉ๋์ง ์๊ณ ์๋ค์! |
@@ -0,0 +1,58 @@
+import endedPopularMovies from "../fixtures/ended-popular-movies.json";
+
+describe("์ํ ๋ชฉ๋ก", () => {
+ beforeEach(() => {
+ const url = "http://localhost:8080";
+ cy.visit(url);
+
+ cy.get("movie-app")
+ .as("movieApp")
+ .shadow()
+ .find("movie-container")
+ .as("movieContainer")
+ .shadow()
+ .find("movie-list")
+ .as("movieList")
+ .shadow()
+ .find("movie-card")
+ .as("movieCard");
+ });
+
+ it("์ต์ด ๋ก๋์ 20๊ฐ์ ์ํ ์นด๋๋ฅผ ๋ณด์ฌ์ค๋ค.", () => {
+ cy.get("@movieCard").should("have.length", 20);
+ });
+
+ it("๋๋ณด๊ธฐ ๋ฒํผ ํด๋ฆญ์ 20๊ฐ์ ์ํ๋ฅผ ์ถ๊ฐ๋ก ๋ณด์ฌ์ค๋ค.", () => {
+ cy.get("@movieContainer").shadow().find(".show-more").as("showMoreButton");
+
+ cy.get("@movieCard").should("have.length", 20);
+ cy.get("@showMoreButton").contains("๋๋ณด๊ธฐ").click();
+ cy.get("@movieCard").should("have.length", 40);
+ });
+
+ it("๋ก๋ฉ์ค์ผ ๋๋ ์ค์ผ๋ ํค ์นด๋๋ฅผ ๋ณด์ฌ์ค๋ค.", () => {
+ cy.intercept("GET", "**/popular*", (req) => {
+ req.on("response", (res) => {
+ res.setDelay(1000); // Delay the response to simulate loading
+ });
+ }).as("getPopularMoviesDelayed");
+
+ cy.reload();
+ cy.get("@movieCard").shadow().find(".skeleton").should("exist");
+ cy.wait("@getPopularMoviesDelayed");
+
+ cy.get("@movieCard").should("have.length", 20);
+ });
+
+ it("์ํ ๋ชฉ๋ก์ ๋์ด์ ๋ถ๋ฌ์ฌ ์ ์์ผ๋ฉด '๋๋ณด๊ธฐ' ๋ฒํผ์ด ์ฌ๋ผ์ง๋ค.", () => {
+ cy.intercept("GET", "**/popular*", endedPopularMovies).as(
+ "getEndedPopularMovies",
+ );
+ cy.get("@movieContainer").shadow().find(".show-more").as("showMoreButton");
+ cy.get("@showMoreButton").should("exist");
+ cy.get("@showMoreButton").click();
+
+ cy.wait("@getEndedPopularMovies");
+ cy.get("@movieContainer").shadow().find("show-more").should("not.exist");
+ });
+}); | JavaScript | ํ
์คํธ๊ฐ ๋ชจ๋ ํดํผ์ผ์ด์ค๋ฅผ ๊ฐ์ ํ๊ณ ์์ฑ๋ ๊ฒ ๊ฐ์์!
๋คํธ์ํฌ ์๋ฌ๋ HTTP ์๋ฌ์ ๊ดํ ์ํฉ์ ์ ์ ํ๊ฒ ์์ฑ๋ ์ฝ๋๋๋ก ๋์ํ๋์ง๋ ํ์ธํ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค์! |
@@ -0,0 +1,143 @@
+import StarFilled from "../../../images/star_filled.png";
+import { html } from "../../utils/template.js";
+
+const BASE_URL = "https://image.tmdb.org/t/p/w220_and_h330_face";
+
+class MovieCard extends HTMLElement {
+ static get observedAttributes() {
+ return ["posterPath", "title", "voteAverage", "isLoading"];
+ }
+
+ constructor() {
+ super();
+ this.attachShadow({ mode: "open" });
+ }
+
+ get posterPath() {
+ return this.getAttribute("posterPath");
+ }
+
+ get title() {
+ return this.getAttribute("title");
+ }
+
+ get voteAverage() {
+ return this.getAttribute("voteAverage");
+ }
+
+ get isLoading() {
+ return this.getAttribute("isLoading") === "true";
+ }
+
+ attributeChangedCallback(name, oldValue, newValue) {
+ if (oldValue !== newValue) {
+ this.render();
+ }
+ }
+
+ connectedCallback() {
+ this.render();
+ }
+
+ render() {
+ const state = {
+ posterPath: this.posterPath,
+ title: this.title,
+ voteAverage: this.voteAverage,
+ isLoading: this.isLoading,
+ };
+ this.shadowRoot.innerHTML = this.template(state);
+ }
+
+ template({ posterPath, title, voteAverage, isLoading }) {
+ return html`
+ <style>
+ .movie-card {
+ display: flex;
+ flex-direction: column;
+ }
+
+ .movie-thumbnail {
+ border-radius: 8px;
+ width: 180px;
+ height: 270px;
+ background-size: contain;
+ }
+
+ .movie-title {
+ margin-top: 16px;
+ font-size: 1.2rem;
+ font-weight: bold;
+ }
+
+ .movie-score {
+ margin-top: 16px;
+ font-size: 1.2rem;
+ }
+
+ .movie-score::after {
+ margin-left: 8px;
+ }
+
+ a {
+ color: inherit;
+ text-decoration: none;
+ }
+
+ .movie-title.skeleton::after,
+ .movie-score.skeleton::after {
+ font-size: 0;
+ content: "loading";
+ }
+
+ .movie-card .skeleton {
+ background: linear-gradient(-90deg, #aaa, #f0f0f0, #aaa, #f0f0f0);
+ background-size: 400%;
+ animation: skeleton-animation 5s infinite ease-out;
+ border-radius: 8px;
+ }
+
+ @keyframes skeleton-animation {
+ 0% {
+ background-position: 0% 50%;
+ }
+ 50% {
+ background-position: 100% 50%;
+ }
+ 100% {
+ background-position: 0% 50%;
+ }
+ }
+ </style>
+ <li>
+ ${isLoading
+ ? html`
+ <div class="movie-card">
+ <div class="movie-thumbnail skeleton"></div>
+ <p class="movie-title skeleton"></p>
+ <p class="movie-score skeleton"></p>
+ </div>
+ `
+ : html`
+ <a href="#">
+ <div class="movie-card">
+ <img
+ class="movie-thumbnail"
+ src="${BASE_URL}/${posterPath}"
+ loading="lazy"
+ alt="${title}"
+ />
+ <p class="movie-title">${title}</p>
+ <p class="movie-score">
+ <img src="${StarFilled}" alt="๋ณ์ " />
+ ${Number(voteAverage).toFixed(1)}
+ </p>
+ </div>
+ </a>
+ `}
+ </li>
+ `;
+ }
+}
+
+customElements.define("movie-card", MovieCard); | JavaScript | `a` ํ๊ทธ ๋ฐ `href="#'`๋ฅผ ์ฌ์ฉํ์ ์ด์ ๊ฐ ์๋์? |
@@ -0,0 +1,40 @@
+package nextstep.security.authentication;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import nextstep.security.core.Authentication;
+import nextstep.security.core.context.SecurityContextHolder;
+import nextstep.security.exception.AuthenticationException;
+import nextstep.security.web.authentication.AuthenticationConverter;
+import nextstep.security.web.authentication.BasicAuthenticationConverter;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+public class BasicAuthenticationFilter extends OncePerRequestFilter {
+
+ private final AuthenticationManager authenticationManager;
+ private final AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter();
+
+ public BasicAuthenticationFilter(AuthenticationManager authenticationManager) {
+ this.authenticationManager = authenticationManager;
+ }
+
+ @Override
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
+ throws IOException, ServletException {
+ try {
+ Authentication authRequest = authenticationConverter.convert(request);
+
+ if (authRequest != null) {
+ Authentication authResult = authenticationManager.authenticate(authRequest);
+ SecurityContextHolder.getContext().setAuthentication(authResult);
+ }
+
+ filterChain.doFilter(request, response);
+ } catch (AuthenticationException e) {
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+ }
+ }
+} | Java | BasicAuthenticationFilter ํํฐ๋ฅผ ์์ฑํ ๋ UserDetailsService ๋์ AuthenticationManager๋ฅผ ์ฃผ์
๋ฐ์ผ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,35 @@
+package nextstep.security.authentication;
+
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Map;
+import nextstep.security.core.Authentication;
+import nextstep.security.exception.AuthenticationException;
+import nextstep.security.exception.AuthenticationServiceException;
+import nextstep.security.web.authentication.AbstractAuthenticationProcessingFilter;
+
+public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
+ private static final String DEFAULT_REQUEST_URI = "/login";
+
+ public UsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) {
+ super(DEFAULT_REQUEST_URI, authenticationManager);
+ }
+
+ @Override
+ public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
+ throws AuthenticationException, IOException, ServletException {
+ if (!"POST".equalsIgnoreCase(request.getMethod())) {
+ throw new AuthenticationServiceException();
+ }
+ Map<String, String[]> parameterMap = request.getParameterMap();
+ String username = parameterMap.get("username")[0];
+ String password = parameterMap.get("password")[0];
+
+ UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
+ username, password, false);
+
+ return this.getAuthenticationManager().authenticate(authRequest);
+ }
+} | Java | ์ฌ๊ธฐ๋ ๋ง์ฐฌ๊ฐ์ง๋ก UserDetailsService ๋์ AuthenticationManager๋ฅผ ์ฃผ์
๋ฐ์ผ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,40 @@
+package nextstep.security.authentication;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import nextstep.security.core.Authentication;
+import nextstep.security.core.context.SecurityContextHolder;
+import nextstep.security.exception.AuthenticationException;
+import nextstep.security.web.authentication.AuthenticationConverter;
+import nextstep.security.web.authentication.BasicAuthenticationConverter;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+public class BasicAuthenticationFilter extends OncePerRequestFilter {
+
+ private final AuthenticationManager authenticationManager;
+ private final AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter();
+
+ public BasicAuthenticationFilter(AuthenticationManager authenticationManager) {
+ this.authenticationManager = authenticationManager;
+ }
+
+ @Override
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
+ throws IOException, ServletException {
+ try {
+ Authentication authRequest = authenticationConverter.convert(request);
+
+ if (authRequest != null) {
+ Authentication authResult = authenticationManager.authenticate(authRequest);
+ SecurityContextHolder.getContext().setAuthentication(authResult);
+ }
+
+ filterChain.doFilter(request, response);
+ } catch (AuthenticationException e) {
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+ }
+ }
+} | Java | Authentication ์์ฑ์ ์ฑ
์์ ๋ณ๋์ ํด๋์ค๋ก ๋ถ๋ฆฌํด๋ณด๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,40 @@
+package nextstep.security.authentication;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import nextstep.security.core.Authentication;
+import nextstep.security.core.context.SecurityContextHolder;
+import nextstep.security.exception.AuthenticationException;
+import nextstep.security.web.authentication.AuthenticationConverter;
+import nextstep.security.web.authentication.BasicAuthenticationConverter;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+public class BasicAuthenticationFilter extends OncePerRequestFilter {
+
+ private final AuthenticationManager authenticationManager;
+ private final AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter();
+
+ public BasicAuthenticationFilter(AuthenticationManager authenticationManager) {
+ this.authenticationManager = authenticationManager;
+ }
+
+ @Override
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
+ throws IOException, ServletException {
+ try {
+ Authentication authRequest = authenticationConverter.convert(request);
+
+ if (authRequest != null) {
+ Authentication authResult = authenticationManager.authenticate(authRequest);
+ SecurityContextHolder.getContext().setAuthentication(authResult);
+ }
+
+ filterChain.doFilter(request, response);
+ } catch (AuthenticationException e) {
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+ }
+ }
+} | Java | convert ๋ฉ์๋๋ Token์ผ๋ก ๋ณํํด์ฃผ๋ ์ญํ ๋ง ํ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,35 @@
+package nextstep.security.authentication;
+
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Map;
+import nextstep.security.core.Authentication;
+import nextstep.security.exception.AuthenticationException;
+import nextstep.security.exception.AuthenticationServiceException;
+import nextstep.security.web.authentication.AbstractAuthenticationProcessingFilter;
+
+public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
+ private static final String DEFAULT_REQUEST_URI = "/login";
+
+ public UsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) {
+ super(DEFAULT_REQUEST_URI, authenticationManager);
+ }
+
+ @Override
+ public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
+ throws AuthenticationException, IOException, ServletException {
+ if (!"POST".equalsIgnoreCase(request.getMethod())) {
+ throw new AuthenticationServiceException();
+ }
+ Map<String, String[]> parameterMap = request.getParameterMap();
+ String username = parameterMap.get("username")[0];
+ String password = parameterMap.get("password")[0];
+
+ UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
+ username, password, false);
+
+ return this.getAuthenticationManager().authenticate(authRequest);
+ }
+} | Java | ์ํ๋ฆฌํฐ์ ์ค์ ์ฝ๋๋ฅผ ์ดํด๋ณด๋ฉด ์ธ์ฆ ํํฐ์์ ์ค๋ณต๋๋ ๋ถ๋ถ์ AbstractAuthenticationProcessingFilter๋ก ๋ถ๋ฆฌ๋์ด์์ต๋๋ค! UsernamePasswordAuthenticationFilter์์ ํด๋น ๋ถ๋ถ์ ๋ถ๋ฆฌํด๋ณด๋ ์๋๋ฅผ ํด๋ณด์๋ฉด ์ด๋จ๊น์?
ํ์ ์๊ตฌ์ฌํญ์ ์๋์ง๋ง ๋น ๋ฅด๊ฒ ๋ฏธ์
์ ์ ํด์ฃผ์
จ๊ณ ์ ํ ์๊ตฌ์ฌํญ์ผ๋ก 3์ฃผ์ฐจ ๋ฏธ์
์ํ์ ๋์์ด ๋ ๊ฒ ๊ฐ์์ ์ ์๋๋ ค์~ |
@@ -0,0 +1,40 @@
+package nextstep.security.authentication;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import nextstep.security.core.Authentication;
+import nextstep.security.core.context.SecurityContextHolder;
+import nextstep.security.exception.AuthenticationException;
+import nextstep.security.web.authentication.AuthenticationConverter;
+import nextstep.security.web.authentication.BasicAuthenticationConverter;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+public class BasicAuthenticationFilter extends OncePerRequestFilter {
+
+ private final AuthenticationManager authenticationManager;
+ private final AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter();
+
+ public BasicAuthenticationFilter(AuthenticationManager authenticationManager) {
+ this.authenticationManager = authenticationManager;
+ }
+
+ @Override
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
+ throws IOException, ServletException {
+ try {
+ Authentication authRequest = authenticationConverter.convert(request);
+
+ if (authRequest != null) {
+ Authentication authResult = authenticationManager.authenticate(authRequest);
+ SecurityContextHolder.getContext().setAuthentication(authResult);
+ }
+
+ filterChain.doFilter(request, response);
+ } catch (AuthenticationException e) {
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+ }
+ }
+} | Java | ๊ธฐ์กด ๊ตฌ์กฐ์์ ๊ณ์ ๋ฐ์ ์์ผ ๋๊ฐ๋ค ๋ณด๋ ๋ฏธ์ฒ ์ ๊ฒฝ์ฐ์ง ๋ชปํ๊ฒ ๊ฐ์ต๋๋ค ๐
๊ตฌ์ฒด๊ฐ ์๋ ์ถ์์ ์์กดํด์ผ ํ๋ค๋๊ฑธ ์๊ธฐ์์ผ์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค ๐ |
@@ -0,0 +1,35 @@
+package nextstep.security.authentication;
+
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Map;
+import nextstep.security.core.Authentication;
+import nextstep.security.exception.AuthenticationException;
+import nextstep.security.exception.AuthenticationServiceException;
+import nextstep.security.web.authentication.AbstractAuthenticationProcessingFilter;
+
+public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
+ private static final String DEFAULT_REQUEST_URI = "/login";
+
+ public UsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) {
+ super(DEFAULT_REQUEST_URI, authenticationManager);
+ }
+
+ @Override
+ public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
+ throws AuthenticationException, IOException, ServletException {
+ if (!"POST".equalsIgnoreCase(request.getMethod())) {
+ throw new AuthenticationServiceException();
+ }
+ Map<String, String[]> parameterMap = request.getParameterMap();
+ String username = parameterMap.get("username")[0];
+ String password = parameterMap.get("password")[0];
+
+ UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
+ username, password, false);
+
+ return this.getAuthenticationManager().authenticate(authRequest);
+ }
+} | Java | ๋ง์ฐฌ๊ฐ์ง๋ก [fa5f619](https://github.com/next-step/spring-security-authentication/pull/33/commits/fa5f61942bfba0e2f794bb43c5b86af416c679b8) ์ปค๋ฐ์ผ๋ก ๋ฐ์ํ์ต๋๋ค! |
@@ -0,0 +1,40 @@
+package nextstep.security.authentication;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import nextstep.security.core.Authentication;
+import nextstep.security.core.context.SecurityContextHolder;
+import nextstep.security.exception.AuthenticationException;
+import nextstep.security.web.authentication.AuthenticationConverter;
+import nextstep.security.web.authentication.BasicAuthenticationConverter;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+public class BasicAuthenticationFilter extends OncePerRequestFilter {
+
+ private final AuthenticationManager authenticationManager;
+ private final AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter();
+
+ public BasicAuthenticationFilter(AuthenticationManager authenticationManager) {
+ this.authenticationManager = authenticationManager;
+ }
+
+ @Override
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
+ throws IOException, ServletException {
+ try {
+ Authentication authRequest = authenticationConverter.convert(request);
+
+ if (authRequest != null) {
+ Authentication authResult = authenticationManager.authenticate(authRequest);
+ SecurityContextHolder.getContext().setAuthentication(authResult);
+ }
+
+ filterChain.doFilter(request, response);
+ } catch (AuthenticationException e) {
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+ }
+ }
+} | Java | [81fa490](https://github.com/next-step/spring-security-authentication/pull/33/commits/81fa490d0c5ef702821a1b226a17c3363bb5131a)๋ก ๋ฐ์ํ์ต๋๋ค! |
@@ -0,0 +1,40 @@
+package nextstep.security.authentication;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import nextstep.security.core.Authentication;
+import nextstep.security.core.context.SecurityContextHolder;
+import nextstep.security.exception.AuthenticationException;
+import nextstep.security.web.authentication.AuthenticationConverter;
+import nextstep.security.web.authentication.BasicAuthenticationConverter;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+public class BasicAuthenticationFilter extends OncePerRequestFilter {
+
+ private final AuthenticationManager authenticationManager;
+ private final AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter();
+
+ public BasicAuthenticationFilter(AuthenticationManager authenticationManager) {
+ this.authenticationManager = authenticationManager;
+ }
+
+ @Override
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
+ throws IOException, ServletException {
+ try {
+ Authentication authRequest = authenticationConverter.convert(request);
+
+ if (authRequest != null) {
+ Authentication authResult = authenticationManager.authenticate(authRequest);
+ SecurityContextHolder.getContext().setAuthentication(authResult);
+ }
+
+ filterChain.doFilter(request, response);
+ } catch (AuthenticationException e) {
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+ }
+ }
+} | Java | DRY ์๋ฐ๋ ๊ฒธํ๊ณ ์์๋ค์!
ํด๋์ค ๋ถ๋ฆฌํ๊ณ ๋ณด๋ ๊ธฐ์กด ๋ฉ์๋์ ์ฑ
์์ด ๊ณผ๋ํ๊ณ , ํ์์น ์์ ๋๋ธ์ฒดํฌ๊ฐ ๋ค์ด๊ฐ๋ค๋๊ฒ ๋ฐ๋ก ํ์ธ๋์ด์ ์ข์์ต๋๋ค! |
@@ -0,0 +1,35 @@
+package nextstep.security.authentication;
+
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Map;
+import nextstep.security.core.Authentication;
+import nextstep.security.exception.AuthenticationException;
+import nextstep.security.exception.AuthenticationServiceException;
+import nextstep.security.web.authentication.AbstractAuthenticationProcessingFilter;
+
+public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
+ private static final String DEFAULT_REQUEST_URI = "/login";
+
+ public UsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) {
+ super(DEFAULT_REQUEST_URI, authenticationManager);
+ }
+
+ @Override
+ public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
+ throws AuthenticationException, IOException, ServletException {
+ if (!"POST".equalsIgnoreCase(request.getMethod())) {
+ throw new AuthenticationServiceException();
+ }
+ Map<String, String[]> parameterMap = request.getParameterMap();
+ String username = parameterMap.get("username")[0];
+ String password = parameterMap.get("password")[0];
+
+ UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
+ username, password, false);
+
+ return this.getAuthenticationManager().authenticate(authRequest);
+ }
+} | Java | [dbde31c](https://github.com/next-step/spring-security-authentication/pull/33/commits/dbde31cb10aee34fe4e1c767064389589b2fa2ba)๋ก ์๋ํด ๋ณด์์ต๋๋ค!
์ค์ ์ฝ๋๋ฅผ ๋ถ์ํ๋ฉด์ ํฅ๋ฏธ๋ก์ ๋์ ์
SecurityContextRepository.saveContext() ํธ์ถ์
SecurityContextHolderFilter๊ฐ ์๋ AbstractAuthenticationProcessingFilter์์ ํด์ฃผ๊ณ ์๋ค๋ ์ ์ด์๋๋ฐ์.

๊ทธ๋ ๊ฒ ๋๋ฉด ์์ ๊ทธ๋ฆผ์ 3๋ฒ ํ๋ก์ฐ๋ ์๋ง๋๊ฑฐ ์๋๊ฐ? ์ดํด๋ฅผ ๋๊ธฐ ์ํ ๊ทธ๋ฆผ์ผ๊น? ์ด๋ฐ ์ ๋ฐ ์๊ฐ๋ค์ ํ๋ฉด์ ๋๊ฒ ์ฌ๋ฏธ์๊ฒ ๋ณธ ๊ฒ ๊ฐ์ต๋๋ค ใ
ใ
ใ
..
์ ๊ฐ ์๋ชป ์ดํดํ๊ฑฐ๋ผ๋ฉด ์ง์ด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค!
๋ํ AbstractAuthenticationProcessingFilter ๊ตฌํํ๋ฉด์
RequestMatcher์ด๋, strategy ๋์์ธ ํจํด์ผ๋ก ๊ตฌํํด์ผ ํ๋ ๋ถ๋ถ์
ํ ์์ ์์๋ ์ค๋ฒ์์ง๋์ด๋ง์ด๋ผ๊ณ ํ๋จํด ๋ฃ์ง ์์์ต๋๋ค!
์๋ง ์ด๋ฐ ๋ถ๋ถ๊น์ง๋ ๋ฐ๋ผ์ง ์์ผ์
จ์..๊ฒ ๊ฐ์์์! ๐ |
@@ -0,0 +1,35 @@
+package nextstep.security.authentication;
+
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Map;
+import nextstep.security.core.Authentication;
+import nextstep.security.exception.AuthenticationException;
+import nextstep.security.exception.AuthenticationServiceException;
+import nextstep.security.web.authentication.AbstractAuthenticationProcessingFilter;
+
+public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
+ private static final String DEFAULT_REQUEST_URI = "/login";
+
+ public UsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) {
+ super(DEFAULT_REQUEST_URI, authenticationManager);
+ }
+
+ @Override
+ public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
+ throws AuthenticationException, IOException, ServletException {
+ if (!"POST".equalsIgnoreCase(request.getMethod())) {
+ throw new AuthenticationServiceException();
+ }
+ Map<String, String[]> parameterMap = request.getParameterMap();
+ String username = parameterMap.get("username")[0];
+ String password = parameterMap.get("password")[0];
+
+ UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
+ username, password, false);
+
+ return this.getAuthenticationManager().authenticate(authRequest);
+ }
+} | Java | ์ฐ์ ์คํ๋๊ป์ ์ฝ๋๋ก ํ์ธํ ๋ถ๋ถ์ด ๋ง์ต๋๋ค!
์ ๊ฐ ๊ฐ์์์ SecurityContextPersistenceFilter์ SecurityContextHolderFilter๋ ๊ฐ๊ณ ๋ฒ์ ์ ์ฐจ์ด๋ผ๊ณ ๋ง ๋ง์๋๋ ธ๋๋ฐ
๊ฐ์ ์ญํ ์ ํ๋ค๋ ๊ด์ ์์ ๊ฐ๋ค๊ณ ๋ง์๋๋ ธ๊ณ ์ฌ์ค ๋ค๋ฅด๊ฒ ๋์ํ๋ ๋ถ๋ถ์ด ์์ต๋๋ค.
[SecurityContextPersistenceFilter](https://docs.spring.io/spring-security/reference/servlet/authentication/persistence.html#securitycontextpersistencefilter)์ ์ค๋ช
์ ๋ณด๋ฉด repository์ ์ ์ฅํ๋ ๋ถ๋ถ๊น์ง ํฌํจ์ด์ง๋ง [SecurityContextHolderFilter](https://docs.spring.io/spring-security/reference/servlet/authentication/persistence.html#securitycontextholderfilter)์ ์ค๋ช
์ ๋ณด๋ฉด ์ ์ฅํ๋ ๋ถ๋ถ์ ํฌํจํ๊ณ ์์ง ์์ต๋๋ค. |
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="ko">
+ <head>
+ <meta charset="UTF-8" />
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+
+ <title>์ํ๊ด</title>
+ <script defer src="./bundle.js"></script></head>
+ <body>
+ <div id="app"></div>
+ </body>
+</html> | Unknown | ์๋ง ๋ฐฐํฌ ๋๋ฌธ์ dist ํด๋๋ฅผ ์ด๋ ๊ฒ ์ฌ๋ ค์ฃผ์ ๊ฒ ๊ฐ๋ค์ ใ
ใ
gh-pages ๋ธ๋์น๋ฅผ ์ด์ฉํ๋ฉด ์ด๋จ๊น ์ถ์ด์! |
@@ -0,0 +1,20 @@
+import { createComponent } from "../createComponent";
+import { Header } from "./Header";
+import { Main } from "./Main";
+
+export async function App() {
+ const header = await createComponent(Header);
+ const main = await createComponent(Main);
+
+ const bindEvents = () => {
+ main.bindEvents();
+ };
+
+ return {
+ element: `
+ ${header.element}
+ ${main.element}
+ `,
+ bindEvents,
+ };
+} | JavaScript | ์ด์ ๋ณด๋ header๋ main ๋๋ฌธ์ด๊ตฐ์ ใ
ใ
|
@@ -0,0 +1,13 @@
+export let currentComponent = null;
+
+export async function createComponent(component, props) {
+ const previousComponent = currentComponent;
+
+ currentComponent = { name: component.name, stateIndex: 0 };
+
+ const componentInstance = await component(props);
+
+ currentComponent = previousComponent;
+
+ return componentInstance;
+} | JavaScript | ์ฌ๊ธฐ์ previousComponent๊ฐ ํ๋ ์ญํ ์ ๋ฌด์์ผ๊น์? |
@@ -0,0 +1,55 @@
+import { createComponent, currentComponent } from "../createComponent";
+import { fetchMovies } from "../fetch";
+import { useState } from "../state";
+import { MovieCard } from "./MovieCard";
+
+export async function MovieList() {
+ const [page, setPage] = useState(1);
+
+ const movieData = await fetchMovies({ page });
+
+ const [movies, setMovies] = useState([...movieData.results]);
+ const [totalPage] = useState(movieData.total_page);
+
+ const movieCards = await Promise.all(
+ movies.map((movie, index) =>
+ createComponent(MovieCard, {
+ index,
+ title: movie.title,
+ score: movie.vote_average,
+ thumbnailUrl: `https://image.tmdb.org/t/p/w200/${movie.poster_path}`,
+ })
+ )
+ );
+
+ const handleMoreButton = async () => {
+ if (page >= totalPage) {
+ return;
+ }
+
+ await setPage(page + 1);
+
+ const movieData = await fetchMovies({ page: page + 1 });
+ const newMovies = [...movies, ...movieData.results];
+
+ await setMovies(newMovies);
+ };
+
+ const bindEvents = () => {
+ movieCards.forEach((movieCard) => movieCard.bindEvents());
+
+ const $moreButton = document.querySelector("#more-button");
+
+ $moreButton.addEventListener("click", handleMoreButton);
+ };
+
+ return {
+ element: /* html */ `
+ <ul class="item-list">
+ ${movieCards.map((movieCard) => movieCard.element).join("")}
+ </ul>
+ <button id="more-button" class="btn primary full-width">๋ ๋ณด๊ธฐ</button>
+ `,
+ bindEvents,
+ };
+} | JavaScript | ๋ง์ฝ ํ ํ์ด์ง์ ์นดํ
๊ณ ๋ฆฌ๋ณ๋ก ์ฌ๋ฌ ๊ฐ์ ์ํ๋ชฉ๋ก์ ๋์์ค๋ค๊ณ ํ์ ๋,
์ด ์ปดํฌ๋ํธ๋ฅผ ์ฌํ์ฉํ ์ ์์๊น์? |
@@ -0,0 +1,55 @@
+import { createComponent, currentComponent } from "../createComponent";
+import { fetchMovies } from "../fetch";
+import { useState } from "../state";
+import { MovieCard } from "./MovieCard";
+
+export async function MovieList() {
+ const [page, setPage] = useState(1);
+
+ const movieData = await fetchMovies({ page });
+
+ const [movies, setMovies] = useState([...movieData.results]);
+ const [totalPage] = useState(movieData.total_page);
+
+ const movieCards = await Promise.all(
+ movies.map((movie, index) =>
+ createComponent(MovieCard, {
+ index,
+ title: movie.title,
+ score: movie.vote_average,
+ thumbnailUrl: `https://image.tmdb.org/t/p/w200/${movie.poster_path}`,
+ })
+ )
+ );
+
+ const handleMoreButton = async () => {
+ if (page >= totalPage) {
+ return;
+ }
+
+ await setPage(page + 1);
+
+ const movieData = await fetchMovies({ page: page + 1 });
+ const newMovies = [...movies, ...movieData.results];
+
+ await setMovies(newMovies);
+ };
+
+ const bindEvents = () => {
+ movieCards.forEach((movieCard) => movieCard.bindEvents());
+
+ const $moreButton = document.querySelector("#more-button");
+
+ $moreButton.addEventListener("click", handleMoreButton);
+ };
+
+ return {
+ element: /* html */ `
+ <ul class="item-list">
+ ${movieCards.map((movieCard) => movieCard.element).join("")}
+ </ul>
+ <button id="more-button" class="btn primary full-width">๋ ๋ณด๊ธฐ</button>
+ `,
+ bindEvents,
+ };
+} | JavaScript | ๋ชจ๋ ๊ตฌ๊ฐ์ด ๋ค async/await ์ผ๋ก ๋ง๋ค์ด์ง ์ด์ ๊ฐ ์ฌ๊ธฐ์ ์ถ๋ฐ์ด ๋๋ค์.
์ด ๋ถ๋ถ๋ง ๊ฐ์ ํ๋ฉด async/awiat ์ ์ ์ฒด์ ์ผ๋ก ์ ๊ฑฐํ ์ ์์ง ์์๊น์? |
@@ -0,0 +1,55 @@
+import { createComponent, currentComponent } from "../createComponent";
+import { fetchMovies } from "../fetch";
+import { useState } from "../state";
+import { MovieCard } from "./MovieCard";
+
+export async function MovieList() {
+ const [page, setPage] = useState(1);
+
+ const movieData = await fetchMovies({ page });
+
+ const [movies, setMovies] = useState([...movieData.results]);
+ const [totalPage] = useState(movieData.total_page);
+
+ const movieCards = await Promise.all(
+ movies.map((movie, index) =>
+ createComponent(MovieCard, {
+ index,
+ title: movie.title,
+ score: movie.vote_average,
+ thumbnailUrl: `https://image.tmdb.org/t/p/w200/${movie.poster_path}`,
+ })
+ )
+ );
+
+ const handleMoreButton = async () => {
+ if (page >= totalPage) {
+ return;
+ }
+
+ await setPage(page + 1);
+
+ const movieData = await fetchMovies({ page: page + 1 });
+ const newMovies = [...movies, ...movieData.results];
+
+ await setMovies(newMovies);
+ };
+
+ const bindEvents = () => {
+ movieCards.forEach((movieCard) => movieCard.bindEvents());
+
+ const $moreButton = document.querySelector("#more-button");
+
+ $moreButton.addEventListener("click", handleMoreButton);
+ };
+
+ return {
+ element: /* html */ `
+ <ul class="item-list">
+ ${movieCards.map((movieCard) => movieCard.element).join("")}
+ </ul>
+ <button id="more-button" class="btn primary full-width">๋ ๋ณด๊ธฐ</button>
+ `,
+ bindEvents,
+ };
+} | JavaScript | movieData๋ฅผ props๋ก ๋ฐ์์ค๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,13 @@
+export async function fetchMovies({ page }) {
+ const baseUrl = "https://api.themoviedb.org/3/movie/popular";
+ const param = new URLSearchParams({
+ page,
+ language: "ko-KR",
+ });
+
+ const response = await fetch(`${baseUrl}?${param}`, {
+ headers: { authorization: `Bearer ${process.env.API_KEY}` },
+ });
+
+ return await response.json();
+} | JavaScript | ์ฌ์ค ๋ฐฐํฌ๋ ์ฌ์ดํธ์ ๋คํธ์ํฌ ์์ฒญ์ ๊น๋ณด๋ฉด API_KEY ๊ฐ ๊ทธ๋๋ก ๋
ธ์ถ๋๋๊ฑธ ํ์ธํ ์ ์๋ต๋๋ค ใ
ใ
์ด๊ฑด ์ด๋ป๊ฒ ๋ฐฉ์งํ ์ ์์๊น์? |
@@ -0,0 +1,13 @@
+export async function fetchMovies({ page }) {
+ const baseUrl = "https://api.themoviedb.org/3/movie/popular";
+ const param = new URLSearchParams({
+ page,
+ language: "ko-KR",
+ });
+
+ const response = await fetch(`${baseUrl}?${param}`, {
+ headers: { authorization: `Bearer ${process.env.API_KEY}` },
+ });
+
+ return await response.json();
+} | JavaScript | ์ฌ๊ธฐ์ await์ ์ฌ์ฉํ๋ ๊ฒ๊ณผ ์ฌ์ฉํ์ง ์๋๊ฒ ์ด๋ค ์ฐจ์ด๊ฐ ์๋์ง ํน์ ์์ค๊น์~? |
@@ -0,0 +1,99 @@
+## ์ฐ์ํ ํ
ํฌ์ฝ์ค ํ๋ฆฌ์ฝ์ค ํฌ๋ฆฌ์ค๋ง์ค ํ๋ก๋ชจ์
+
+### ์งํ๋ฐฉ์
+- ์์ ๋ฉ์์ง ์ถ๋ ฅ
+- ๋ฐฉ๋ฌธ ๋ ์ง ์
๋ ฅ
+- ์ฃผ๋ฌธ ๋ฉ๋ด์ ๊ฐ์ ์
๋ ฅ
+- ์
๋ ฅ ์์ผ ์ด๋ฒคํธ ๋ฉ์์ง ์ถ๋ ฅ
+- ์ด๋ฒคํธ ํํ ์ถ๋ ฅ
+ - ์ฃผ๋ฌธ ๋ฉ๋ด
+ - ํ ์ธ ์ ์ด ์ฃผ๋ฌธ ๊ธ์ก
+ - ์ฆ์ ๋ฉ๋ด ์ฌ๋ถ
+ - ํํ ๋ด์ญ ์ฌ๋ถ
+ - ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก
+ - 12์ ์ด๋ฒคํธ ๋ฐฐ์ง ์ฌ๋ถ
+
+### ์ฃผ์ ๊ตฌํ ์ฌํญ
+- [x] ์
๋ ฅ ๋ทฐ ๊ตฌํ
+- [x] ์ถ๋ ฅ ๋ทฐ ๊ตฌํ
+
+- [x] ๋ ์ง ์
๋ ฅ ๋ฐ ์ ์ฅ ๊ฐ์ฒด ๊ตฌํ
+- [x] ๋ ์ง ์
๋ ฅ ์ ๋ ์ง๊ด๋ จ ํด๋น ์ด๋ฒคํธ ํญ๋ชฉ ์ ์ฅ
+ - [x] ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ์ด ๊ธ์ก ํ ์ธ ํญ๋ชฉ
+ - [x] ํ์ผ/์ฃผ๋ง ๊ตฌ๋ถ ํ ์ธ ํ์
ํญ๋ณต
+ - [x] ๋ ์ง๋ ๋ฌ๋ ฅ์ ๋ณ์ ๋ฐ๋ผ ์คํ์
ํ ์ธ ํญ๋ชฉ
+- [x] ๋ ์ง ๊ด๋ จ ์ด๋ฒคํธ ๋ฐํ DTO ๋ฐํ
+
+- [x] ์ฃผ๋ฌธ ๋ฉ๋ด์ ๊ฐ์ ์
๋ ฅ ๋ฐ ์ ์ฅ ๊ฐ์ฒด ๊ตฌํ
+ - [x] ๊ฐ ๋ฉ๋ด๋ฅผ ๊ธฐ๋ณธ ๊ธ์ก๊ณผ ๋ฉ๋ด ๋ช
์ผ๋ก Enum ์ผ๋ก ๊ด๋ฆฌ
+ - [x] ์ฃผ๋ฌธํ ๋ฉ๋ด ์
๋ ฅ์ ๋ํ ์ ํจ์ฑ ๊ฒ์ฌ ์งํ ํ ์ ์ฅ ๊ฐ์ฒด์ ์ ์ฅ
+- [x] ์ฃผ๋ฌธ ๋ฉ๋ด ๋ด์ญ ๋ฐํ DTO ๋ฐํ
+
+- [x] ๋ ์ง ์ด๋ฒคํธ์ ์ฃผ๋ฌธ ๋ฉ๋ด DTO ๋ฅผ ํตํ ์ด๋ฒคํธ ํํ ์ ์ฉ
+- [x] ์ด๋ฒคํธ ์ ์ฉ DTO ๋ฐํ ๋ฐ ์ถ๋ ฅ
+ - [x] ํ ์ธ ์ ๊ธ์ก์ ์ถ๋ ฅ
+ - [x] ํ ์ธ ์ ๊ธ์ก์ ๋ฐ๋ฅธ ์ฆ์ ์ฌ๋ถ ์ถ๋ ฅ
+ - ์ฆ์ ์ฌ๋ถ๊ฐ ์๋ค๋ฉด '์์' ์ถ๋ ฅ
+ - [x] ํํ ๋ด์ญ ์ถ๋ ฅ
+ - ์ ์ฉ๋ ํํ ์๋ค๋ฉด '์์' ์ถ๋ ฅ
+ - [x] ํํ ๊ธ์ก ์ถ๋ ฅ
+ - [x] ์ฆ์ ํํ ๊ธ์ก์ ์ ์ธํ ํ ์ธ ํ ์์ ๊ธ์ก ์ถ๋ ฅ
+ - [x] ์ด ํํ ๊ธ์ก์ ๋ฐ๋ฅธ ์ด๋ฒคํธ ๋ฐฐ์ง ๋ถ์ฌ ์ฌ๋ถ ์ถ๋ ฅ
+ - ์ด๋ฒคํธ ๋ฐฐ์ง๊ฐ ์๋ค๋ฉด '์์' ์ถ๋ ฅ
+
+- ์ถ๊ฐ ๊ตฌํ ์ฌํญ
+ - ์ด๋ฒคํธ ๊ฒฐ๊ณผ ๋๋ฉ์ธ์ ์ ๋ณด๋ฅผ ์ถ๋ ฅ ๋ฌธ์์ด์ผ๋ก ๋ณ๊ฒฝํ์ฌ Builder DTO ์์ฑ/๋ฐํ
+ - ์๋ชป๋ ์
๋ ฅ์ ํตํ ๊ณผ์ ์ ์์ธ ๋ฉ์์ง ์ถ๋ ฅ ๋ฐ ์ฌ์
๋ ฅ
+ - ์ด ์ฃผ๋ฌธ ๋ฉ๋ด ๊ธ์ก์ด 10,000์ ์ดํ์ผ ๊ฒฝ์ฐ ๋ชจ๋ ํ ์ธ ๊ธ์ก 0์ผ๋ก ์ง์
+
+### ์์ธ ์ฒ๋ฆฌ
+- ๋ ์ง ์
๋ ฅ ์์ธ
+ - [x] ์๋ชป๋ ๋ ์ง ์
๋ ฅ์ ๊ฒฝ์ฐ
+ - [x] ๋ ์ง๊ฐ ์ ์ํ์ผ๋ก ๋ณ๊ฒฝ์ด ์๋ ๋
+- ๋ฉ๋ด ์
๋ ฅ ์์ธ
+ - [x] ๋ฉ๋ด ์
๋ ฅ ์ ์ ๊ทํํ์ ํจํด์ด ๋ค๋ฅผ ๊ฒฝ์ฐ (TextProcessor ์ฒ๋ฆฌ)
+ - [x] ์ค๋ณต ๋ฉ๋ด๋ช
์
๋ ฅ์ ๊ฒฝ์ฐ (TextProcessor ์ฒ๋ฆฌ)
+ - [x] ์๋ ๋ฉ๋ด๋ช
์
๋ ฅ์ ๊ฒฝ์ฐ
+ - [x] ์ฃผ๋ฌธ ๋ฉ๋ด์ ๊ฐ์๊ฐ 1๋ณด๋ค ์์ ๋
+ - [x] ์ฃผ๋ฌธ ๋ฉ๋ด๊ฐ 20๊ฐ๋ฅผ ๋์ ๋
+ - [x] ์๋ฃ๋ง ์ฃผ๋ฌธ ์ ์ฃผ๋ฌธ ๋ถ๊ฐ๋ฅ
+
+### ํด๋์ค ๊ตฌ์กฐ
+- InputView : ํ๋ฉด ์
๋ ฅ์ ๋ด๋นํ๋ ๋ทฐ
+ - InputViewMessage : ์
๋ ฅ์ ์ํ ๋ฉ์์ง๋ฅผ ๊ด๋ฆฌํ๋ Enum
+- OutputView : ํ๋ฉด ์ถ๋ ฅ์ ๋ด๋นํ๋ ๋ทฐ
+ - OutputViewMessage : ์ถ๋ ฅ์ ์ํ ๋ฉ์์ง๋ฅผ ๊ด๋ฆฌํ๋ Enum
+- PromotionRun : ๋ทฐ์ ์ปจํธ๋กค๋ฌ๋ฅผ ์ฐ๊ฒฐํ๋ ํด๋์ค๋ก ์์ฒญ ์ ๋ฌ
+- PromotionController : ๋น์ฆ๋์ค ๋ก์ง์ ์คํํ๊ธฐ ์ํด ์์ฒญ์ ์ ๋ฌํ๋ ์ปจํธ๋กค๋ฌ
+- PromotionService : ํ๋ก๋ชจ์
์ ํจ์ฑ ๊ฒ์ฆ์ ๊ฑฐ์น ๊ฐ์ฒด ์์ฑ๊ณผ ๋น์ฆ๋์ค ๋ก์ง์ ์คํํ ์๋น์ค
+- TextProcessor : ์
๋ ฅ๋ฐ์ ๋ฌธ์์ด์ ํน์ ํ์
์ผ๋ก ๋ฐํํ๊ธฐ ์ํ ์๋น์ค
+- OrderMenus : ์ฃผ๋ฌธ ๋ฉ๋ด์ ๊ฐ์ ์ ์ฅํ๋ ํด๋์ค
+ - MenuItem : ๋ฉ๋ด ์ด๋ฆ๊ณผ ๊ฐ๊ฒฉ, ๋ฉ๋ด ์ข
๋ฅ๋ฅผ ์ ์ฅํ๋ Enum
+ - MenuCategory : ๋ฉ๋ด ์ข
๋ฅ๋ฅผ ๊ตฌ๋ถํ๋ Enum
+- ReservationDateEvent : ๋ ์ง ๊ด๋ จ ์ด๋ฒคํธ ์ ์ฅ ํด๋์ค
+ - ReservationDate : ์
๋ ฅ๋ ๋ ์ง๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ํด๋์ค
+ - ChristmasDiscount : ํฌ๋ฆฌ์ค๋ง์ค D-day ํ ์ธ ๊ด๋ฆฌ ํด๋์ค
+ - WeekDiscountType : ์ฃผ๋ง ํ ์ธ, ํ์ผ ํ ์ธ์ ๊ตฌ๋ถํ๋ Enum
+ - SpecialDayDiscount : ํน๋ณ ํ ์ธ ์ผ์๊ฐ ์ ์ฅ๋์ด ์๋ Enum
+- EventResult : ์ ์ฉ ์ค์ธ ๋ชจ๋ ์ด๋ฒคํธ๋ฅผ ์ ์ฅํ๋ ํด๋์ค
+ - DiscountDetail : ์ฃผ๋ฌธ ๊ธ์ก๊ณผ ํ ์ธ, ์์ ๊ธ์ก ๋ฑ์ ๋ณด๊ดํ ํด๋์ค
+ - GiftEvent : ์ฆ์ ์ํ ์ฌ๋ถ๋ฅผ ๊ด๋ฆฌํ๋ Enum
+ - RewardBadge : ์ด๋ฒคํธ ๋ฐฐ์ง๋ฅผ ๊ด๋ฆฌํ๋ Enum
+- PromotionException : ์ง์ ๋ Enum ์์ธ ๋ฉ์์ง๋ฅผ ํตํด ๊ฐ๋ฅผ ๋ด์ IllegalArgumentException ์์ฑ
+ - ExceptionMessage : ์์ธ ๋ฐ์ ๋ฉ์์ง๋ฅผ ๋ณด๊ดํ Enum Class
+- PromotionConverter : Service ์์ Domain to DTO ์ปจ๋ฒํฐ
+- EventResultTextFactory : EventResultDTO ๋ฐํ์ ์ํ ๋ฌธ์์ด ์กฐ์ ์๋น์ค
+ - EventResultText : EventResultDTO ์ ํ์ํ ๋ฌธ์์ด Enum
+- ํ
์คํธ : ํด๋์ค๋ณ ๋ฉ์๋ ๋จ์ ํ
์คํธ ์งํ
+
+### ์ด๋ฒ์ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ๊ณ ๋ฏผํ ์์
+- ํด๋น ๊ธฐ๋ฅ์ ๊ตฌํํ๊ธฐ ์ํ ๊ตฌ์กฐ ์ค๊ณ
+ - ์ดํ ํ์ํ ํด๋์ค๋ค์ ์ถ๊ฐํ๋ฉฐ ์ค๊ณ ๋ณ๊ฒฝ
+- ๋์ผํ ์
๋ ฅ๊ณผ ์ถ๋ ฅ์ ํ๋ ๋ทฐ๋ ์ฑ๊ธํค์ผ๋ก ๊ตฌํ
+- ์
๋ ฅ์ ๋ํ ์ ํจ์ฑ ๊ฒ์ฌ์ ๊ฐ์ฒด ์์ฑ์ ๋ํ ์ ํจ์ฑ ๊ฒ์ฌ ๊ตฌ๋ถ์ ๋ํ ๊ณ ๋ฏผ
+- ์ด๋ฒคํธ ์ ์ฉ ์ ์ด๋ฒคํธ ๋ด์ญ๊ณผ ์ฃผ๋ฌธ ๋ด์ญ์ ์ฌ์ฉํด ๊ฐ์ ์ ์ฅํ๋ ๋ฐฉ๋ฒ์ Service ์์ ์งํํ ์ง ๊ฐ์ฒด ๋ด๋ถ์์ ์งํํ ์ง ๊ณ ๋ฏผ
+- ์ถ๋ ฅ์ ์ํ DTO ์์ฑ์ ๋ํ ๊ณ ๋ฏผ
+ - DTO ๋ฃฐ ์ถ๋ ฅํ๊ณ ํด๋น DTO ํตํด ๋ค์ ์ด๋ฒคํธ ๋ด์ญ ํ์ธํ๋๋ฐ ์์ด DTO ๊ฐ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
+ - EventResultDTO ๋ทฐ์ ์ถ๋ ฅํ ๋ฌธ์์ด์ ๊ฐ์ง๊ณ ์์ DTO ์์ฑ ๋ฐฉ๋ฒ ๊ณ ๋ฏผ ํ Builder, EventResultTextFactory ์ฌ์ฉ
+- ์๋ชป๋ ์
๋ ฅ์ ๋ํ ๋ฉ์๋ ์ฌ์คํ ๋ก์ง์ ์ง๋์ฃผ [zangsu๋](https://github.com/zangsu) ์ฝ๋๋ฆฌ๋ทฐ๋ก ๋ฐฐ์ด Supplier ๋ฐ๋ณต ์ฌ์ฉ
+- ํ
์คํธ ์ฝ๋ mock ๊ฐ์ฒด์ ๋ํ ์ฌ์ฉ๋ฐฉ๋ฒ์ ๋ํ ๊ณ ๋ฏผ
\ No newline at end of file | Unknown | README์ ํด๋์ค์ด๋ฆ์ ์์ฑํ๊ธฐ๋ณด๋ค ์ด๋ค ์ญํ ์ ํด๋์ค์ธ์ง ์ ๋๋ง ์์ฑํ์๋๊ฒ ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! ์๋ฌด๋๋ ํด๋์ค์ด๋ฆ์ด๋ ๋ฉ์๋ ์ด๋ฆ์ ๊ฐ๋ณ์ฑ์ด ๋๋ค๋ณด๋ README๋ ์ฃผ๊ธฐ์ ์ผ๋ก ์
๋ฐ์ดํธํด์ผํด์ ๋ฒ๊ฑฐ๋ก์์ด ํฌ๊ฑฐ๋ ์ |
@@ -0,0 +1,20 @@
+package christmas.exception;
+
+public enum ExceptionMessage {
+ INVALID_INPUT_DATE("์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ INVALID_INPUT_ORDER("์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ INVALID_MENU_ITEM("ํด๋น ๋ฉ๋ด๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค."),
+ INVALID_WEEK_DISCOUNT_TYPE("์๋ชป๋ ์ฃผ๊ฐ ํ ์ธ ํ์
์
๋๋ค."),
+ ;
+
+ private static final String PREFIX = "[ERROR] ";
+ private final String errorMessage;
+
+ ExceptionMessage(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+
+ public String getErrorMessage() {
+ return PREFIX + errorMessage;
+ }
+} | Java | ์ถ๊ฐ๋ enum์ ๊ณ ๋ คํ์
์ ์ด๋ ๊ฒ ์์ฑํ ์๋๋ผ๊ณ ์๊ฐ์ ๋์ง๋ง, ๊ผผ๊ผผํ๊ณ ํ๊ณ ๋ค์๋ฉด ,;๋ณด๋ค ;๋ก ๋๋ด๋๊ฒ ๋ ์ข์ง ์์๊น์?? ๊ฐ์ธ์ ์ธ ์๊ฐ์
๋๋ค ใ
ใ
ใ
|
@@ -1,7 +1,20 @@
package christmas;
+
+import camp.nextstep.edu.missionutils.Console;
+import christmas.run.PromotionRun;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
public class Application {
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ try {
+ InputView inputView = SingletonView.getInputView();
+ OutputView outputView = SingletonView.getOutputView();
+ PromotionRun promotionRun = new PromotionRun(inputView, outputView);
+ promotionRun.runPromotion();
+ } finally {
+ Console.close();
+ }
}
} | Java | ๋ทฐ๋ฅผ ์ฑ๊ธํค์ผ๋ก ๊ด๋ฆฌํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,20 @@
+package christmas.exception;
+
+public enum ExceptionMessage {
+ INVALID_INPUT_DATE("์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ INVALID_INPUT_ORDER("์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ INVALID_MENU_ITEM("ํด๋น ๋ฉ๋ด๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค."),
+ INVALID_WEEK_DISCOUNT_TYPE("์๋ชป๋ ์ฃผ๊ฐ ํ ์ธ ํ์
์
๋๋ค."),
+ ;
+
+ private static final String PREFIX = "[ERROR] ";
+ private final String errorMessage;
+
+ ExceptionMessage(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+
+ public String getErrorMessage() {
+ return PREFIX + errorMessage;
+ }
+} | Java | ์ถ๊ฐ์ ์ผ๋ก ์๊ตฌ์ฌํญ์ ์๋ฌ๋ฉ์์ง๋ "[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."์ผ๋ก ํต์ผํ๋ผ๊ณ ๊ธฐ์ฌ๋์ด ์์ด ์ต์ข
์ฝ๋ฉํ
์คํธ๋ก ๊ฐ์๊ฒ ๋๋ฉด ๊ผผ๊ผผํ ๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,33 @@
+package christmas.model.event;
+
+import christmas.model.date.ReservationDate;
+
+public class ChristmasDiscount {
+ private static final int FIRST_DAY = 1;
+ private static final int CHRISTMAS_DAY = 25;
+ private static final int BASIC_DISCOUNT = 1000;
+ private static final int PLUS_DISCOUNT = 100;
+ private static final int NONE_DISCOUNT = 0;
+
+ private final int discount;
+
+ public ChristmasDiscount(ReservationDate date) {
+ this.discount = initDiscountAmount(date);
+ }
+
+ private int initDiscountAmount(ReservationDate date) {
+ if (date.day() > CHRISTMAS_DAY) {
+ return NONE_DISCOUNT;
+ }
+
+ return BASIC_DISCOUNT + getPlusDiscount(date.day());
+ }
+
+ private int getPlusDiscount(int day) {
+ return (day - FIRST_DAY) * PLUS_DISCOUNT;
+ }
+
+ public int getDiscount() {
+ return discount;
+ }
+} | Java | ReservationDate์์ ์ค์ค๋ก ์์ ์ด ๊ฐ์ง ๊ฐ์๋ํด ๊ฒ์ฆํ๋๋ก ๋ฉ์๋๋ฅผ ๊ตฌํํ์๋ ๊ฒ์ด ๋ ๊ฐ์ฒด์งํฅ์ ์ธ ์ฝ๋๋ผ๊ณ ์๊ฐํฉ๋๋ค.
date.isChristmasDay() ์ ๊ฐ์ด ๋ง์ด์ฃ !! 'getter๋ฅผ ์ง์ํ๋ผ'๋ ์๋ฏธ๋ VO์๊ฒ๋ ๋์ผํ๊ฒ ์ ์ฉ๋ฉ๋๋ค.
๊ด๋ จ ๋ฌธ์๋ฅผ ์๋ ๋งํฌ๋ก ๋ฌ์๋๊ฒ์!
https://tecoble.techcourse.co.kr/post/2020-04-28-ask-instead-of-getter/ |
@@ -0,0 +1,62 @@
+package christmas.model.event;
+
+import christmas.model.event.dto.ReservationDateEventDTO;
+
+public class DiscountDetail {
+ private final int totalPriceBeforeDiscount;
+ private final int totalPriceAfterDiscount;
+ private final WeekDiscountType weekDiscountType;
+ private final int christmasDiscount;
+ private final int weekTypeDiscount;
+ private final int specialDiscount;
+
+ protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount, int weekTypeDiscount) {
+ this.totalPriceBeforeDiscount = totalPriceBeforeDiscount;
+ this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+ this.christmasDiscount = dateEventDTO.getChristmasDiscount();
+ this.specialDiscount = dateEventDTO.getSpecialDiscount();
+ this.weekTypeDiscount = weekTypeDiscount;
+ this.totalPriceAfterDiscount = initTotalPriceAfterDiscount();
+ }
+
+ protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount) {
+ this.totalPriceBeforeDiscount = totalPriceBeforeDiscount;
+ this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+ this.christmasDiscount = 0;
+ this.specialDiscount = 0;
+ this.weekTypeDiscount = 0;
+ this.totalPriceAfterDiscount = initTotalPriceAfterDiscount();
+ }
+
+ private int initTotalPriceAfterDiscount() {
+ return totalPriceBeforeDiscount - getTotalDiscount();
+ }
+
+ public int getTotalPriceBeforeDiscount() {
+ return totalPriceBeforeDiscount;
+ }
+
+ public int getTotalPriceAfterDiscount() {
+ return totalPriceAfterDiscount;
+ }
+
+ public int getChristmasDiscount() {
+ return christmasDiscount;
+ }
+
+ public int getSpecialDiscount() {
+ return specialDiscount;
+ }
+
+ public int getWeekTypeDiscount() {
+ return weekTypeDiscount;
+ }
+
+ public WeekDiscountType getWeekDiscountType() {
+ return weekDiscountType;
+ }
+
+ public int getTotalDiscount() {
+ return christmasDiscount + weekTypeDiscount + specialDiscount;
+ }
+} | Java | DiscountDetil์ ํ๋๋ก ์ด ๊ฐ๋ค์ ๊ตณ์ด ๊ฐ์ ธ์ผ ํ ์ด์ ๊ฐ ์์๊น์? ์ฌ์ฌ์ฉ์ ์ฌ์ง๊ฐ ์๋ค๋ฉด ๋ฉ์๋๋ฅผ ํตํด ๋ฉ์์ง๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ์์ด ๋ ์ข๋ค๊ณ ์๊ฐํด์! |
@@ -0,0 +1,62 @@
+package christmas.model.event;
+
+import christmas.model.event.dto.ReservationDateEventDTO;
+
+public class DiscountDetail {
+ private final int totalPriceBeforeDiscount;
+ private final int totalPriceAfterDiscount;
+ private final WeekDiscountType weekDiscountType;
+ private final int christmasDiscount;
+ private final int weekTypeDiscount;
+ private final int specialDiscount;
+
+ protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount, int weekTypeDiscount) {
+ this.totalPriceBeforeDiscount = totalPriceBeforeDiscount;
+ this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+ this.christmasDiscount = dateEventDTO.getChristmasDiscount();
+ this.specialDiscount = dateEventDTO.getSpecialDiscount();
+ this.weekTypeDiscount = weekTypeDiscount;
+ this.totalPriceAfterDiscount = initTotalPriceAfterDiscount();
+ }
+
+ protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount) {
+ this.totalPriceBeforeDiscount = totalPriceBeforeDiscount;
+ this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+ this.christmasDiscount = 0;
+ this.specialDiscount = 0;
+ this.weekTypeDiscount = 0;
+ this.totalPriceAfterDiscount = initTotalPriceAfterDiscount();
+ }
+
+ private int initTotalPriceAfterDiscount() {
+ return totalPriceBeforeDiscount - getTotalDiscount();
+ }
+
+ public int getTotalPriceBeforeDiscount() {
+ return totalPriceBeforeDiscount;
+ }
+
+ public int getTotalPriceAfterDiscount() {
+ return totalPriceAfterDiscount;
+ }
+
+ public int getChristmasDiscount() {
+ return christmasDiscount;
+ }
+
+ public int getSpecialDiscount() {
+ return specialDiscount;
+ }
+
+ public int getWeekTypeDiscount() {
+ return weekTypeDiscount;
+ }
+
+ public WeekDiscountType getWeekDiscountType() {
+ return weekDiscountType;
+ }
+
+ public int getTotalDiscount() {
+ return christmasDiscount + weekTypeDiscount + specialDiscount;
+ }
+} | Java | ์ธ๋ถ ์์ฑ์ ๋ง๊ธฐ ์ํด ์์ฑ์๋ฅผ protected๋ก ๊ด๋ฆฌํ์ ๊ฒ์ผ๋ก ๋ณด์ด๋๋ฐ, private์ผ๋ก ์์ฑ์๋ฅผ ๋ง๊ณ ์ ์ ํฉํ ๋ฆฌ๋ฉ์๋ ์์ฑ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ ์๋ฐํ๊ฒ ์บก์ํํ ์ ์์ต๋๋ค! ๊ด๋ จ ์๋ฃ๋ฅผ ์๋ ๋งํฌ๋ก ๋ฌ์๋๊ฒ์!
https://hudi.blog/effective-java-static-factory-method/ |
@@ -0,0 +1,91 @@
+package christmas.model.event;
+
+import christmas.model.event.dto.ReservationDateEventDTO;
+import christmas.model.menu.MenuItem;
+
+import java.util.*;
+import java.util.stream.Stream;
+
+public class EventResult {
+ private static final int MIN_EVENT_AMOUNT = 10_000;
+ private static final int GIFT_AMOUNT = 120_000;
+
+ private final Map<MenuItem, Integer> orderMenus;
+ private final DiscountDetail discountDetail;
+ private final GiftMenu giftMenu;
+ private final RewardBadge rewardBadge;
+
+ public EventResult(ReservationDateEventDTO dateEventDTO, Map<MenuItem, Integer> orderMenus) {
+ this.orderMenus = orderMenus;
+ this.discountDetail = initDiscountDetail(dateEventDTO);
+ this.giftMenu = initGiftMenu();
+ this.rewardBadge = initRewardBadge();
+ }
+
+ private DiscountDetail initDiscountDetail(ReservationDateEventDTO dateEventDTO) {
+ int totalPriceBeforeDiscount = getTotalPriceBeforeDiscount();
+ if (totalPriceBeforeDiscount < MIN_EVENT_AMOUNT) {
+ return new DiscountDetail(dateEventDTO, totalPriceBeforeDiscount);
+ }
+ int weekDiscount = calculateWeekDiscount(dateEventDTO);
+
+ return new DiscountDetail(dateEventDTO, totalPriceBeforeDiscount, weekDiscount);
+ }
+
+ private GiftMenu initGiftMenu() {
+ if (discountDetail.getTotalPriceBeforeDiscount() < GIFT_AMOUNT) {
+ return GiftMenu.NONE;
+ }
+
+ return GiftMenu.CHAMPAGNE;
+ }
+
+ private RewardBadge initRewardBadge() {
+ int benefit = discountDetail.getTotalDiscount() + giftMenu.getGiftPrice();
+
+ return Arrays.stream(RewardBadge.values())
+ .filter(badge ->
+ benefit >= badge.getBenefit())
+ .max(Comparator.comparingInt(RewardBadge::getBenefit))
+ .orElse(RewardBadge.NONE);
+ }
+
+ private int getTotalPriceBeforeDiscount() {
+ return generateOrderMenuEntry(orderMenus)
+ .mapToInt(menu ->
+ menu.getKey().getItemPrice() * menu.getValue())
+ .sum();
+ }
+
+ private int calculateWeekDiscount(ReservationDateEventDTO dateEventDTO) {
+ WeekDiscountType type = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+
+ return generateOrderMenuEntry(orderMenus)
+ .filter(menu ->
+ menu.getKey().getMenuCategory() == type.getDiscountCategory())
+ .mapToInt(menu ->
+ menu.getValue() * type.getDiscountPrice())
+ .sum();
+ }
+
+ private <K, V> Stream<Map.Entry<K, V>> generateOrderMenuEntry(Map<K, V> orderMenus) {
+ return orderMenus.entrySet()
+ .stream();
+ }
+
+ public Map<MenuItem, Integer> getOrderMenus() {
+ return Collections.unmodifiableMap(orderMenus);
+ }
+
+ public DiscountDetail getDiscountDetail() {
+ return discountDetail;
+ }
+
+ public GiftMenu getGiftMenu() {
+ return giftMenu;
+ }
+
+ public RewardBadge getRewardBadge() {
+ return rewardBadge;
+ }
+} | Java | ํ๋ ์๋ฅผ ์ค์ฌ ์์ง๋๋ฅผ ๋ฎ์ถ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. ๋ฒ๊ทธ๊ฐ ๋ฐ์ํ๋ฉด ๋ฆฌํํ ๋งํ ๋ถ๋ถ์ด ๋ง์์ ธ์!! |
@@ -0,0 +1,91 @@
+package christmas.model.event;
+
+import christmas.model.event.dto.ReservationDateEventDTO;
+import christmas.model.menu.MenuItem;
+
+import java.util.*;
+import java.util.stream.Stream;
+
+public class EventResult {
+ private static final int MIN_EVENT_AMOUNT = 10_000;
+ private static final int GIFT_AMOUNT = 120_000;
+
+ private final Map<MenuItem, Integer> orderMenus;
+ private final DiscountDetail discountDetail;
+ private final GiftMenu giftMenu;
+ private final RewardBadge rewardBadge;
+
+ public EventResult(ReservationDateEventDTO dateEventDTO, Map<MenuItem, Integer> orderMenus) {
+ this.orderMenus = orderMenus;
+ this.discountDetail = initDiscountDetail(dateEventDTO);
+ this.giftMenu = initGiftMenu();
+ this.rewardBadge = initRewardBadge();
+ }
+
+ private DiscountDetail initDiscountDetail(ReservationDateEventDTO dateEventDTO) {
+ int totalPriceBeforeDiscount = getTotalPriceBeforeDiscount();
+ if (totalPriceBeforeDiscount < MIN_EVENT_AMOUNT) {
+ return new DiscountDetail(dateEventDTO, totalPriceBeforeDiscount);
+ }
+ int weekDiscount = calculateWeekDiscount(dateEventDTO);
+
+ return new DiscountDetail(dateEventDTO, totalPriceBeforeDiscount, weekDiscount);
+ }
+
+ private GiftMenu initGiftMenu() {
+ if (discountDetail.getTotalPriceBeforeDiscount() < GIFT_AMOUNT) {
+ return GiftMenu.NONE;
+ }
+
+ return GiftMenu.CHAMPAGNE;
+ }
+
+ private RewardBadge initRewardBadge() {
+ int benefit = discountDetail.getTotalDiscount() + giftMenu.getGiftPrice();
+
+ return Arrays.stream(RewardBadge.values())
+ .filter(badge ->
+ benefit >= badge.getBenefit())
+ .max(Comparator.comparingInt(RewardBadge::getBenefit))
+ .orElse(RewardBadge.NONE);
+ }
+
+ private int getTotalPriceBeforeDiscount() {
+ return generateOrderMenuEntry(orderMenus)
+ .mapToInt(menu ->
+ menu.getKey().getItemPrice() * menu.getValue())
+ .sum();
+ }
+
+ private int calculateWeekDiscount(ReservationDateEventDTO dateEventDTO) {
+ WeekDiscountType type = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+
+ return generateOrderMenuEntry(orderMenus)
+ .filter(menu ->
+ menu.getKey().getMenuCategory() == type.getDiscountCategory())
+ .mapToInt(menu ->
+ menu.getValue() * type.getDiscountPrice())
+ .sum();
+ }
+
+ private <K, V> Stream<Map.Entry<K, V>> generateOrderMenuEntry(Map<K, V> orderMenus) {
+ return orderMenus.entrySet()
+ .stream();
+ }
+
+ public Map<MenuItem, Integer> getOrderMenus() {
+ return Collections.unmodifiableMap(orderMenus);
+ }
+
+ public DiscountDetail getDiscountDetail() {
+ return discountDetail;
+ }
+
+ public GiftMenu getGiftMenu() {
+ return giftMenu;
+ }
+
+ public RewardBadge getRewardBadge() {
+ return rewardBadge;
+ }
+} | Java | ์ด ๋ถ๋ถ๋ getter๋ก ๊บผ๋ด์์ ๊ฒ์ฆํ๊ธฐ๋ณด๋ค ๊ฐ์ฒด ๋ด๋ถ์์ ์ค์ค๋ก๊ฒ์ฆํ๋๋ก ํ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,28 @@
+package christmas.model.event;
+
+public enum SpecialDayDiscount {
+ THIRD_DAY(3, 1000),
+ TENTH_DAY(10, 1000),
+ SEVENTEENTH_DAY(17, 1000),
+ TWENTY_FOURTH_DAY(24, 1000),
+ TWENTY_FIFTH_DAY(25, 1000),
+ THIRTY_FIRST_DAY(31, 1000),
+ OTHER_DAY(0, 0),
+ ;
+
+ private final int day;
+ private final int discountPrice;
+
+ SpecialDayDiscount(int day, int discountPrice) {
+ this.day = day;
+ this.discountPrice = discountPrice;
+ }
+
+ public int getDay() {
+ return day;
+ }
+
+ public int getDiscountPrice() {
+ return discountPrice;
+ }
+} | Java | 1000์์ด๋ผ๋ ๋์ผํ ํ ์ธ๊ธ์ก์ ๊ฐ์ง๊ณ ์์ผ๋ List๋ก ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,55 @@
+package christmas.model.menu;
+
+import christmas.exception.ExceptionMessage;
+import christmas.exception.PromotionException;
+
+import java.util.Arrays;
+
+public enum MenuItem {
+ APPETIZER_MUSHROOM_SOUP("์์ก์ด์ํ", 6_000, MenuCategory.APPETIZER),
+ APPETIZER_TAPAS("ํํ์ค", 5_500, MenuCategory.APPETIZER),
+ APPETIZER_CAESAR_SALAD("์์ ์๋ฌ๋", 8_000, MenuCategory.APPETIZER),
+
+ MAIN_T_BONE_STEAK("ํฐ๋ณธ์คํ
์ดํฌ", 55_000, MenuCategory.MAIN_COURSE),
+ MAIN_BBQ_RIB("๋ฐ๋นํ๋ฆฝ", 54_000, MenuCategory.MAIN_COURSE),
+ MAIN_SEAFOOD_PASTA("ํด์ฐ๋ฌผํ์คํ", 35_000, MenuCategory.MAIN_COURSE),
+ MAIN_CHRISTMAS_PASTA("ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25_000, MenuCategory.MAIN_COURSE),
+
+ DESSERT_CHOCO_CAKE("์ด์ฝ์ผ์ดํฌ", 15_000, MenuCategory.DESSERT),
+ DESSERT_ICE_CREAM("์์ด์คํฌ๋ฆผ", 5_000, MenuCategory.DESSERT),
+
+ BEVERAGE_ZERO_COLA("์ ๋ก์ฝ๋ผ", 3_000, MenuCategory.BEVERAGE),
+ BEVERAGE_RED_WINE("๋ ๋์์ธ", 60_000, MenuCategory.BEVERAGE),
+ BEVERAGE_CHAMPAGNE("์ดํ์ธ", 25_000, MenuCategory.BEVERAGE),
+ ;
+
+ private final String itemName;
+ private final int itemPrice;
+ private final MenuCategory menuCategory;
+
+ MenuItem(String itemName, int itemPrice, MenuCategory menuCategory) {
+ this.itemName = itemName;
+ this.itemPrice = itemPrice;
+ this.menuCategory = menuCategory;
+ }
+
+ public static MenuItem findMenuItemByName(String itemName, ExceptionMessage message) {
+ return Arrays.stream(MenuItem.values())
+ .filter(menu -> menu.getItemName().equals(itemName))
+ .findFirst()
+ .orElseThrow(() ->
+ new PromotionException(message));
+ }
+
+ public String getItemName() {
+ return itemName;
+ }
+
+ public int getItemPrice() {
+ return itemPrice;
+ }
+
+ public MenuCategory getMenuCategory() {
+ return menuCategory;
+ }
+} | Java | MenuCategory, MenuItem์์ ์ค๋ณต๋๋ ์์๋ค์ด ์๋๋ฐ ํ๋๋ก ๋ฌถ์ด๋ ๋ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,79 @@
+package christmas.model.menu;
+
+import christmas.exception.ExceptionMessage;
+import christmas.exception.PromotionException;
+
+import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public class OrderMenus {
+ private static final int MIN_ORDER_MENU_NUMBER = 1;
+ private static final int MAX_ORDER_MENUS_NUMBER = 20;
+
+ private final Map<MenuItem, Integer> orderMenus;
+
+ public OrderMenus(Map<String, Integer> orderMenus) {
+ this.orderMenus = validateOrderMenusByNames(orderMenus);
+ validateOrderMenus();
+ }
+
+ private Map<MenuItem, Integer> validateOrderMenusByNames(Map<String, Integer> orderMenus) {
+ return generateOrderMenuValues(orderMenus)
+ .collect(Collectors.toUnmodifiableMap(
+ entry -> MenuItem.findMenuItemByName(entry.getKey(), ExceptionMessage.INVALID_INPUT_ORDER),
+ Map.Entry::getValue
+ ));
+ }
+
+ private void validateOrderMenus() {
+ validateOrderNumbers();
+ validateTotalOrderNumbers();
+ validateOrderAllBeverage();
+ }
+
+ private void validateOrderNumbers() {
+ generateOrderMenuValues(orderMenus)
+ .filter(menu ->
+ menu.getValue() < MIN_ORDER_MENU_NUMBER)
+ .findAny()
+ .ifPresent(menuName -> throwInvalidOrderException());
+ }
+
+ private void validateTotalOrderNumbers() {
+ int orderNumber = generateOrderMenuValues(orderMenus)
+ .mapToInt(Map.Entry::getValue)
+ .sum();
+
+ if (orderNumber > MAX_ORDER_MENUS_NUMBER) {
+ throwInvalidOrderException();
+ }
+ }
+
+ private void validateOrderAllBeverage() {
+ boolean allBeverage = generateOrderMenuValues(orderMenus)
+ .allMatch(orderMenu ->
+ orderMenu.getKey().getMenuCategory() == MenuCategory.BEVERAGE);
+
+ if (allBeverage) {
+ throwInvalidOrderException();
+ }
+ }
+
+ private <K, V> Stream<Map.Entry<K, V>> generateOrderMenuValues(Map<K, V> orderMenus) {
+ return orderMenus.entrySet()
+ .stream();
+ }
+
+ private void throwInvalidOrderException() {
+ throw new PromotionException(ExceptionMessage.INVALID_INPUT_ORDER);
+ }
+
+ public Map<String, Integer> getOrderMenus() {
+ Map<String, Integer> menuNames = new HashMap<>();
+ orderMenus.forEach((menuItem, quantity) ->
+ menuNames.put(menuItem.getItemName(), quantity));
+
+ return Collections.unmodifiableMap(menuNames);
+ }
+} | Java | Integer๋ ์์๊ฐ์ ํฌ์ฅํ๋ ๊ฒ์ ์ด๋จ๊น์?? |
@@ -0,0 +1,76 @@
+package christmas.run;
+
+import christmas.controller.PromotionController;
+import christmas.exception.PromotionException;
+import christmas.model.event.dto.EventResultDTO;
+import christmas.model.event.dto.ReservationDateEventDTO;
+import christmas.model.menu.dto.OrderMenusDTO;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+import java.util.function.Supplier;
+
+public class PromotionRun {
+ private final PromotionController controller = new PromotionController();
+ private final InputView inputView;
+ private final OutputView outputView;
+
+ public PromotionRun(InputView inputView, OutputView outputView) {
+ this.inputView = inputView;
+ this.outputView = outputView;
+ }
+
+ public void runPromotion() {
+ ReservationDateEventDTO dateEventDto = inputCorrectReservationDate();
+ OrderMenusDTO orderMenusDto = inputCorrectOrderMenus();
+ displayEventPreview(dateEventDto);
+
+ EventResultDTO responseDto = applyEventsAndGenerateResult(dateEventDto, orderMenusDto);
+ outputEventResult(responseDto);
+ }
+
+ private ReservationDateEventDTO inputCorrectReservationDate() {
+ outputView.displayStartPromotion();
+
+ return getCorrectResult(this::generateDateEvent);
+ }
+
+ private OrderMenusDTO inputCorrectOrderMenus() {
+ return getCorrectResult(this::receiveMenus);
+ }
+
+ private ReservationDateEventDTO generateDateEvent() {
+ String inputDate = inputView.inputDate();
+
+ return controller.generateEvent(inputDate);
+ }
+
+ private OrderMenusDTO receiveMenus() {
+ String inputMenus = inputView.inputOrderMenus();
+
+ return controller.receiveOrderMenus(inputMenus);
+ }
+
+ private void displayEventPreview(ReservationDateEventDTO dateEventDto) {
+ outputView.displayPreviewEvent(dateEventDto.getDay());
+ }
+
+ private EventResultDTO applyEventsAndGenerateResult(ReservationDateEventDTO dateEventDTO,
+ OrderMenusDTO orderMenusDto) {
+ return controller.applyEvents(dateEventDTO, orderMenusDto);
+ }
+
+ private void outputEventResult(EventResultDTO responseDto) {
+ outputView.outputEventResult(responseDto);
+ }
+
+ private <T> T getCorrectResult(Supplier<T> supplier) {
+ while (true) {
+ try {
+ return supplier.get();
+ } catch (PromotionException e) {
+ outputView.displayException(e);
+ }
+ }
+ }
+} | Java | ํ๋์ฃผ์
๋ณด๋ค ์์ฑ์ ์ฃผ์
์ด ์ ์ง๋ณด์ ๊ด์ ์์ ๋ ์ข์ ๊ฒ ๊ฐ์์.!! |
@@ -0,0 +1,76 @@
+package christmas.run;
+
+import christmas.controller.PromotionController;
+import christmas.exception.PromotionException;
+import christmas.model.event.dto.EventResultDTO;
+import christmas.model.event.dto.ReservationDateEventDTO;
+import christmas.model.menu.dto.OrderMenusDTO;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+import java.util.function.Supplier;
+
+public class PromotionRun {
+ private final PromotionController controller = new PromotionController();
+ private final InputView inputView;
+ private final OutputView outputView;
+
+ public PromotionRun(InputView inputView, OutputView outputView) {
+ this.inputView = inputView;
+ this.outputView = outputView;
+ }
+
+ public void runPromotion() {
+ ReservationDateEventDTO dateEventDto = inputCorrectReservationDate();
+ OrderMenusDTO orderMenusDto = inputCorrectOrderMenus();
+ displayEventPreview(dateEventDto);
+
+ EventResultDTO responseDto = applyEventsAndGenerateResult(dateEventDto, orderMenusDto);
+ outputEventResult(responseDto);
+ }
+
+ private ReservationDateEventDTO inputCorrectReservationDate() {
+ outputView.displayStartPromotion();
+
+ return getCorrectResult(this::generateDateEvent);
+ }
+
+ private OrderMenusDTO inputCorrectOrderMenus() {
+ return getCorrectResult(this::receiveMenus);
+ }
+
+ private ReservationDateEventDTO generateDateEvent() {
+ String inputDate = inputView.inputDate();
+
+ return controller.generateEvent(inputDate);
+ }
+
+ private OrderMenusDTO receiveMenus() {
+ String inputMenus = inputView.inputOrderMenus();
+
+ return controller.receiveOrderMenus(inputMenus);
+ }
+
+ private void displayEventPreview(ReservationDateEventDTO dateEventDto) {
+ outputView.displayPreviewEvent(dateEventDto.getDay());
+ }
+
+ private EventResultDTO applyEventsAndGenerateResult(ReservationDateEventDTO dateEventDTO,
+ OrderMenusDTO orderMenusDto) {
+ return controller.applyEvents(dateEventDTO, orderMenusDto);
+ }
+
+ private void outputEventResult(EventResultDTO responseDto) {
+ outputView.outputEventResult(responseDto);
+ }
+
+ private <T> T getCorrectResult(Supplier<T> supplier) {
+ while (true) {
+ try {
+ return supplier.get();
+ } catch (PromotionException e) {
+ outputView.displayException(e);
+ }
+ }
+ }
+} | Java | ์ ๋๋ฆญ์ ์ฌ์ฉํ์ ๋ถ๋ถ์ด ์ธ์๊น์ต๋๋ค.! |
@@ -0,0 +1,27 @@
+package christmas.util;
+
+enum EventResultText {
+ ORDER_OUTPUT_REGEX("%s %d%s"),
+ EMPTY_TEXT(""),
+ SPACE(" "),
+ MENU_NUMBER("๊ฐ"),
+ MENU_PRICE_UNIT("์"),
+ CHRISTMAS_D_DAY_DISCOUNT("ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ"),
+ SPECIAL_DISCOUNT("ํน๋ณ ํ ์ธ"),
+ GIFT_EVENT("์ฆ์ ์ด๋ฒคํธ"),
+ NONE_BENEFIT("์์"),
+ DISCOUNT_PRICE("-"),
+ BENEFIT_SEPARATOR(": "),
+ NEW_LINE("\n"),
+ ;
+
+ private final String text;
+
+ EventResultText(String text) {
+ this.text = text;
+ }
+
+ public String getText() {
+ return text;
+ }
+} | Java | ์ฐ๊ด์ฑ์ด ์๋ ์์๋ค์ static final๋ก ๊ด๋ฆฌํ๋ ๊ฒ์ด ๋ ๊ฐ๋
์ฑ ์ธก๋ฉด์์ ์ข๋ค๊ณ ์๊ฐํด์ ! |
@@ -0,0 +1,99 @@
+## ์ฐ์ํ ํ
ํฌ์ฝ์ค ํ๋ฆฌ์ฝ์ค ํฌ๋ฆฌ์ค๋ง์ค ํ๋ก๋ชจ์
+
+### ์งํ๋ฐฉ์
+- ์์ ๋ฉ์์ง ์ถ๋ ฅ
+- ๋ฐฉ๋ฌธ ๋ ์ง ์
๋ ฅ
+- ์ฃผ๋ฌธ ๋ฉ๋ด์ ๊ฐ์ ์
๋ ฅ
+- ์
๋ ฅ ์์ผ ์ด๋ฒคํธ ๋ฉ์์ง ์ถ๋ ฅ
+- ์ด๋ฒคํธ ํํ ์ถ๋ ฅ
+ - ์ฃผ๋ฌธ ๋ฉ๋ด
+ - ํ ์ธ ์ ์ด ์ฃผ๋ฌธ ๊ธ์ก
+ - ์ฆ์ ๋ฉ๋ด ์ฌ๋ถ
+ - ํํ ๋ด์ญ ์ฌ๋ถ
+ - ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก
+ - 12์ ์ด๋ฒคํธ ๋ฐฐ์ง ์ฌ๋ถ
+
+### ์ฃผ์ ๊ตฌํ ์ฌํญ
+- [x] ์
๋ ฅ ๋ทฐ ๊ตฌํ
+- [x] ์ถ๋ ฅ ๋ทฐ ๊ตฌํ
+
+- [x] ๋ ์ง ์
๋ ฅ ๋ฐ ์ ์ฅ ๊ฐ์ฒด ๊ตฌํ
+- [x] ๋ ์ง ์
๋ ฅ ์ ๋ ์ง๊ด๋ จ ํด๋น ์ด๋ฒคํธ ํญ๋ชฉ ์ ์ฅ
+ - [x] ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ์ด ๊ธ์ก ํ ์ธ ํญ๋ชฉ
+ - [x] ํ์ผ/์ฃผ๋ง ๊ตฌ๋ถ ํ ์ธ ํ์
ํญ๋ณต
+ - [x] ๋ ์ง๋ ๋ฌ๋ ฅ์ ๋ณ์ ๋ฐ๋ผ ์คํ์
ํ ์ธ ํญ๋ชฉ
+- [x] ๋ ์ง ๊ด๋ จ ์ด๋ฒคํธ ๋ฐํ DTO ๋ฐํ
+
+- [x] ์ฃผ๋ฌธ ๋ฉ๋ด์ ๊ฐ์ ์
๋ ฅ ๋ฐ ์ ์ฅ ๊ฐ์ฒด ๊ตฌํ
+ - [x] ๊ฐ ๋ฉ๋ด๋ฅผ ๊ธฐ๋ณธ ๊ธ์ก๊ณผ ๋ฉ๋ด ๋ช
์ผ๋ก Enum ์ผ๋ก ๊ด๋ฆฌ
+ - [x] ์ฃผ๋ฌธํ ๋ฉ๋ด ์
๋ ฅ์ ๋ํ ์ ํจ์ฑ ๊ฒ์ฌ ์งํ ํ ์ ์ฅ ๊ฐ์ฒด์ ์ ์ฅ
+- [x] ์ฃผ๋ฌธ ๋ฉ๋ด ๋ด์ญ ๋ฐํ DTO ๋ฐํ
+
+- [x] ๋ ์ง ์ด๋ฒคํธ์ ์ฃผ๋ฌธ ๋ฉ๋ด DTO ๋ฅผ ํตํ ์ด๋ฒคํธ ํํ ์ ์ฉ
+- [x] ์ด๋ฒคํธ ์ ์ฉ DTO ๋ฐํ ๋ฐ ์ถ๋ ฅ
+ - [x] ํ ์ธ ์ ๊ธ์ก์ ์ถ๋ ฅ
+ - [x] ํ ์ธ ์ ๊ธ์ก์ ๋ฐ๋ฅธ ์ฆ์ ์ฌ๋ถ ์ถ๋ ฅ
+ - ์ฆ์ ์ฌ๋ถ๊ฐ ์๋ค๋ฉด '์์' ์ถ๋ ฅ
+ - [x] ํํ ๋ด์ญ ์ถ๋ ฅ
+ - ์ ์ฉ๋ ํํ ์๋ค๋ฉด '์์' ์ถ๋ ฅ
+ - [x] ํํ ๊ธ์ก ์ถ๋ ฅ
+ - [x] ์ฆ์ ํํ ๊ธ์ก์ ์ ์ธํ ํ ์ธ ํ ์์ ๊ธ์ก ์ถ๋ ฅ
+ - [x] ์ด ํํ ๊ธ์ก์ ๋ฐ๋ฅธ ์ด๋ฒคํธ ๋ฐฐ์ง ๋ถ์ฌ ์ฌ๋ถ ์ถ๋ ฅ
+ - ์ด๋ฒคํธ ๋ฐฐ์ง๊ฐ ์๋ค๋ฉด '์์' ์ถ๋ ฅ
+
+- ์ถ๊ฐ ๊ตฌํ ์ฌํญ
+ - ์ด๋ฒคํธ ๊ฒฐ๊ณผ ๋๋ฉ์ธ์ ์ ๋ณด๋ฅผ ์ถ๋ ฅ ๋ฌธ์์ด์ผ๋ก ๋ณ๊ฒฝํ์ฌ Builder DTO ์์ฑ/๋ฐํ
+ - ์๋ชป๋ ์
๋ ฅ์ ํตํ ๊ณผ์ ์ ์์ธ ๋ฉ์์ง ์ถ๋ ฅ ๋ฐ ์ฌ์
๋ ฅ
+ - ์ด ์ฃผ๋ฌธ ๋ฉ๋ด ๊ธ์ก์ด 10,000์ ์ดํ์ผ ๊ฒฝ์ฐ ๋ชจ๋ ํ ์ธ ๊ธ์ก 0์ผ๋ก ์ง์
+
+### ์์ธ ์ฒ๋ฆฌ
+- ๋ ์ง ์
๋ ฅ ์์ธ
+ - [x] ์๋ชป๋ ๋ ์ง ์
๋ ฅ์ ๊ฒฝ์ฐ
+ - [x] ๋ ์ง๊ฐ ์ ์ํ์ผ๋ก ๋ณ๊ฒฝ์ด ์๋ ๋
+- ๋ฉ๋ด ์
๋ ฅ ์์ธ
+ - [x] ๋ฉ๋ด ์
๋ ฅ ์ ์ ๊ทํํ์ ํจํด์ด ๋ค๋ฅผ ๊ฒฝ์ฐ (TextProcessor ์ฒ๋ฆฌ)
+ - [x] ์ค๋ณต ๋ฉ๋ด๋ช
์
๋ ฅ์ ๊ฒฝ์ฐ (TextProcessor ์ฒ๋ฆฌ)
+ - [x] ์๋ ๋ฉ๋ด๋ช
์
๋ ฅ์ ๊ฒฝ์ฐ
+ - [x] ์ฃผ๋ฌธ ๋ฉ๋ด์ ๊ฐ์๊ฐ 1๋ณด๋ค ์์ ๋
+ - [x] ์ฃผ๋ฌธ ๋ฉ๋ด๊ฐ 20๊ฐ๋ฅผ ๋์ ๋
+ - [x] ์๋ฃ๋ง ์ฃผ๋ฌธ ์ ์ฃผ๋ฌธ ๋ถ๊ฐ๋ฅ
+
+### ํด๋์ค ๊ตฌ์กฐ
+- InputView : ํ๋ฉด ์
๋ ฅ์ ๋ด๋นํ๋ ๋ทฐ
+ - InputViewMessage : ์
๋ ฅ์ ์ํ ๋ฉ์์ง๋ฅผ ๊ด๋ฆฌํ๋ Enum
+- OutputView : ํ๋ฉด ์ถ๋ ฅ์ ๋ด๋นํ๋ ๋ทฐ
+ - OutputViewMessage : ์ถ๋ ฅ์ ์ํ ๋ฉ์์ง๋ฅผ ๊ด๋ฆฌํ๋ Enum
+- PromotionRun : ๋ทฐ์ ์ปจํธ๋กค๋ฌ๋ฅผ ์ฐ๊ฒฐํ๋ ํด๋์ค๋ก ์์ฒญ ์ ๋ฌ
+- PromotionController : ๋น์ฆ๋์ค ๋ก์ง์ ์คํํ๊ธฐ ์ํด ์์ฒญ์ ์ ๋ฌํ๋ ์ปจํธ๋กค๋ฌ
+- PromotionService : ํ๋ก๋ชจ์
์ ํจ์ฑ ๊ฒ์ฆ์ ๊ฑฐ์น ๊ฐ์ฒด ์์ฑ๊ณผ ๋น์ฆ๋์ค ๋ก์ง์ ์คํํ ์๋น์ค
+- TextProcessor : ์
๋ ฅ๋ฐ์ ๋ฌธ์์ด์ ํน์ ํ์
์ผ๋ก ๋ฐํํ๊ธฐ ์ํ ์๋น์ค
+- OrderMenus : ์ฃผ๋ฌธ ๋ฉ๋ด์ ๊ฐ์ ์ ์ฅํ๋ ํด๋์ค
+ - MenuItem : ๋ฉ๋ด ์ด๋ฆ๊ณผ ๊ฐ๊ฒฉ, ๋ฉ๋ด ์ข
๋ฅ๋ฅผ ์ ์ฅํ๋ Enum
+ - MenuCategory : ๋ฉ๋ด ์ข
๋ฅ๋ฅผ ๊ตฌ๋ถํ๋ Enum
+- ReservationDateEvent : ๋ ์ง ๊ด๋ จ ์ด๋ฒคํธ ์ ์ฅ ํด๋์ค
+ - ReservationDate : ์
๋ ฅ๋ ๋ ์ง๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ํด๋์ค
+ - ChristmasDiscount : ํฌ๋ฆฌ์ค๋ง์ค D-day ํ ์ธ ๊ด๋ฆฌ ํด๋์ค
+ - WeekDiscountType : ์ฃผ๋ง ํ ์ธ, ํ์ผ ํ ์ธ์ ๊ตฌ๋ถํ๋ Enum
+ - SpecialDayDiscount : ํน๋ณ ํ ์ธ ์ผ์๊ฐ ์ ์ฅ๋์ด ์๋ Enum
+- EventResult : ์ ์ฉ ์ค์ธ ๋ชจ๋ ์ด๋ฒคํธ๋ฅผ ์ ์ฅํ๋ ํด๋์ค
+ - DiscountDetail : ์ฃผ๋ฌธ ๊ธ์ก๊ณผ ํ ์ธ, ์์ ๊ธ์ก ๋ฑ์ ๋ณด๊ดํ ํด๋์ค
+ - GiftEvent : ์ฆ์ ์ํ ์ฌ๋ถ๋ฅผ ๊ด๋ฆฌํ๋ Enum
+ - RewardBadge : ์ด๋ฒคํธ ๋ฐฐ์ง๋ฅผ ๊ด๋ฆฌํ๋ Enum
+- PromotionException : ์ง์ ๋ Enum ์์ธ ๋ฉ์์ง๋ฅผ ํตํด ๊ฐ๋ฅผ ๋ด์ IllegalArgumentException ์์ฑ
+ - ExceptionMessage : ์์ธ ๋ฐ์ ๋ฉ์์ง๋ฅผ ๋ณด๊ดํ Enum Class
+- PromotionConverter : Service ์์ Domain to DTO ์ปจ๋ฒํฐ
+- EventResultTextFactory : EventResultDTO ๋ฐํ์ ์ํ ๋ฌธ์์ด ์กฐ์ ์๋น์ค
+ - EventResultText : EventResultDTO ์ ํ์ํ ๋ฌธ์์ด Enum
+- ํ
์คํธ : ํด๋์ค๋ณ ๋ฉ์๋ ๋จ์ ํ
์คํธ ์งํ
+
+### ์ด๋ฒ์ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ๊ณ ๋ฏผํ ์์
+- ํด๋น ๊ธฐ๋ฅ์ ๊ตฌํํ๊ธฐ ์ํ ๊ตฌ์กฐ ์ค๊ณ
+ - ์ดํ ํ์ํ ํด๋์ค๋ค์ ์ถ๊ฐํ๋ฉฐ ์ค๊ณ ๋ณ๊ฒฝ
+- ๋์ผํ ์
๋ ฅ๊ณผ ์ถ๋ ฅ์ ํ๋ ๋ทฐ๋ ์ฑ๊ธํค์ผ๋ก ๊ตฌํ
+- ์
๋ ฅ์ ๋ํ ์ ํจ์ฑ ๊ฒ์ฌ์ ๊ฐ์ฒด ์์ฑ์ ๋ํ ์ ํจ์ฑ ๊ฒ์ฌ ๊ตฌ๋ถ์ ๋ํ ๊ณ ๋ฏผ
+- ์ด๋ฒคํธ ์ ์ฉ ์ ์ด๋ฒคํธ ๋ด์ญ๊ณผ ์ฃผ๋ฌธ ๋ด์ญ์ ์ฌ์ฉํด ๊ฐ์ ์ ์ฅํ๋ ๋ฐฉ๋ฒ์ Service ์์ ์งํํ ์ง ๊ฐ์ฒด ๋ด๋ถ์์ ์งํํ ์ง ๊ณ ๋ฏผ
+- ์ถ๋ ฅ์ ์ํ DTO ์์ฑ์ ๋ํ ๊ณ ๋ฏผ
+ - DTO ๋ฃฐ ์ถ๋ ฅํ๊ณ ํด๋น DTO ํตํด ๋ค์ ์ด๋ฒคํธ ๋ด์ญ ํ์ธํ๋๋ฐ ์์ด DTO ๊ฐ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
+ - EventResultDTO ๋ทฐ์ ์ถ๋ ฅํ ๋ฌธ์์ด์ ๊ฐ์ง๊ณ ์์ DTO ์์ฑ ๋ฐฉ๋ฒ ๊ณ ๋ฏผ ํ Builder, EventResultTextFactory ์ฌ์ฉ
+- ์๋ชป๋ ์
๋ ฅ์ ๋ํ ๋ฉ์๋ ์ฌ์คํ ๋ก์ง์ ์ง๋์ฃผ [zangsu๋](https://github.com/zangsu) ์ฝ๋๋ฆฌ๋ทฐ๋ก ๋ฐฐ์ด Supplier ๋ฐ๋ณต ์ฌ์ฉ
+- ํ
์คํธ ์ฝ๋ mock ๊ฐ์ฒด์ ๋ํ ์ฌ์ฉ๋ฐฉ๋ฒ์ ๋ํ ๊ณ ๋ฏผ
\ No newline at end of file | Unknown | ์ ์ข์ ์๊ฐ์ด๋ค์. ์ดํ ๊ด๋ฆฌ์๋ ์๊ฐํ์ง ์์๋ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -1,7 +1,20 @@
package christmas;
+
+import camp.nextstep.edu.missionutils.Console;
+import christmas.run.PromotionRun;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
public class Application {
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ try {
+ InputView inputView = SingletonView.getInputView();
+ OutputView outputView = SingletonView.getOutputView();
+ PromotionRun promotionRun = new PromotionRun(inputView, outputView);
+ promotionRun.runPromotion();
+ } finally {
+ Console.close();
+ }
}
} | Java | ํ๋ฒ๋ง ์์ฑ๋๋ฉด ๋๊ณ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ ํ์๊ฐ ์๊ฒ ๋ค ์ถ์ด ์ ์ฉํ๋๋ฐ ์ปจํธ๋กค๋ฌ, ์๋น์ค ๋ชจ๋ ์ ์ฉํด์ผํ๋ ์ถ๋ค๊ฐ ๋ทฐ๋ง ์์ฑํ๊ฒ ๋์์ต๋๋ค.. |
@@ -0,0 +1,20 @@
+package christmas.exception;
+
+public enum ExceptionMessage {
+ INVALID_INPUT_DATE("์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ INVALID_INPUT_ORDER("์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ INVALID_MENU_ITEM("ํด๋น ๋ฉ๋ด๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค."),
+ INVALID_WEEK_DISCOUNT_TYPE("์๋ชป๋ ์ฃผ๊ฐ ํ ์ธ ํ์
์
๋๋ค."),
+ ;
+
+ private static final String PREFIX = "[ERROR] ";
+ private final String errorMessage;
+
+ ExceptionMessage(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+
+ public String getErrorMessage() {
+ return PREFIX + errorMessage;
+ }
+} | Java | ์ ํด๋น ์์ธ๋ ์ด๋ฏธ ์ฒ๋ฆฌ๋ ๊ฒฐ๊ณผ DTO ๋ค์ ๊ฐ์ง๊ณ ๋ฐ์๋๋ ์์ธ๋ผ์ ์ ์์ ์ด๋ผ๋ฉด ํฐ์ง์ง ์๋ ์์ธ ์
๋๋ค. ์ผ๋จ ์ ์๊ฐ์ผ๋ก๋ ๋ฌด์จ ์ง์ ํด๋ ์
๋ ฅ์ ๋ํด ํด๋น ์์ธ๋ ํฐ์ง์ง๋ ์์ ๊ฑฐ๋ผ ์๊ฐํ๋๋ฐ ํด๋น ๊ธฐ๋ฅ์์๋ง ํฐ์ง๋ ์์ธ๋ฅผ ๊ตฌ๋ถํ๊ธฐ ์ํด์ ์ฌ์ฉํ์ต๋๋ค! |
@@ -0,0 +1,33 @@
+package christmas.model.event;
+
+import christmas.model.date.ReservationDate;
+
+public class ChristmasDiscount {
+ private static final int FIRST_DAY = 1;
+ private static final int CHRISTMAS_DAY = 25;
+ private static final int BASIC_DISCOUNT = 1000;
+ private static final int PLUS_DISCOUNT = 100;
+ private static final int NONE_DISCOUNT = 0;
+
+ private final int discount;
+
+ public ChristmasDiscount(ReservationDate date) {
+ this.discount = initDiscountAmount(date);
+ }
+
+ private int initDiscountAmount(ReservationDate date) {
+ if (date.day() > CHRISTMAS_DAY) {
+ return NONE_DISCOUNT;
+ }
+
+ return BASIC_DISCOUNT + getPlusDiscount(date.day());
+ }
+
+ private int getPlusDiscount(int day) {
+ return (day - FIRST_DAY) * PLUS_DISCOUNT;
+ }
+
+ public int getDiscount() {
+ return discount;
+ }
+} | Java | ์์ฝ ์ผ์ ๊ฐ์ฒด๋ ์์ฝ๋ ์ผ์์ ๋ํด์๋ง ๊ฐ์ ๊ฐ์ง๊ณ ์์ด ํ ์ธ์ ์ ์ฉํ๊ธฐ ์ํ ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ํด๋น ํ ์ธ ๊ฐ์ฒด๊ฐ ์ํํด์ผ ํ๋ค๊ณ ์๊ฐํด์ ๋ฃ์๋๋ฐ ๊ณ ๋ฏผ์ด ๋๋ค์.. |
@@ -0,0 +1,62 @@
+package christmas.model.event;
+
+import christmas.model.event.dto.ReservationDateEventDTO;
+
+public class DiscountDetail {
+ private final int totalPriceBeforeDiscount;
+ private final int totalPriceAfterDiscount;
+ private final WeekDiscountType weekDiscountType;
+ private final int christmasDiscount;
+ private final int weekTypeDiscount;
+ private final int specialDiscount;
+
+ protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount, int weekTypeDiscount) {
+ this.totalPriceBeforeDiscount = totalPriceBeforeDiscount;
+ this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+ this.christmasDiscount = dateEventDTO.getChristmasDiscount();
+ this.specialDiscount = dateEventDTO.getSpecialDiscount();
+ this.weekTypeDiscount = weekTypeDiscount;
+ this.totalPriceAfterDiscount = initTotalPriceAfterDiscount();
+ }
+
+ protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount) {
+ this.totalPriceBeforeDiscount = totalPriceBeforeDiscount;
+ this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+ this.christmasDiscount = 0;
+ this.specialDiscount = 0;
+ this.weekTypeDiscount = 0;
+ this.totalPriceAfterDiscount = initTotalPriceAfterDiscount();
+ }
+
+ private int initTotalPriceAfterDiscount() {
+ return totalPriceBeforeDiscount - getTotalDiscount();
+ }
+
+ public int getTotalPriceBeforeDiscount() {
+ return totalPriceBeforeDiscount;
+ }
+
+ public int getTotalPriceAfterDiscount() {
+ return totalPriceAfterDiscount;
+ }
+
+ public int getChristmasDiscount() {
+ return christmasDiscount;
+ }
+
+ public int getSpecialDiscount() {
+ return specialDiscount;
+ }
+
+ public int getWeekTypeDiscount() {
+ return weekTypeDiscount;
+ }
+
+ public WeekDiscountType getWeekDiscountType() {
+ return weekDiscountType;
+ }
+
+ public int getTotalDiscount() {
+ return christmasDiscount + weekTypeDiscount + specialDiscount;
+ }
+} | Java | ๊ฐ์ฒด๊ฐ ๊ฐ์ง๊ณ ์๋ ์ํ์ ๊ฐ์ฒด๋ฅผ ํํํ๋ ค ํ๋ค๋ณด๋ ์ด๋ ๊ฒ ๋์๋๋ฐ ์์ง๋ ๋ฉ์๋๋ก ๋ฉ์์ง๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์ ์์ง ๋ฏ์ ๊ฒ ๊ฐ์ต๋๋ค.. ์ค๊ฐ์ค๊ฐ ์ธ์งํ๋ คํด๋ ๋์น๋ ๋ถ๋ถ์ด ๋ง๋ค์.. |
@@ -0,0 +1,62 @@
+package christmas.model.event;
+
+import christmas.model.event.dto.ReservationDateEventDTO;
+
+public class DiscountDetail {
+ private final int totalPriceBeforeDiscount;
+ private final int totalPriceAfterDiscount;
+ private final WeekDiscountType weekDiscountType;
+ private final int christmasDiscount;
+ private final int weekTypeDiscount;
+ private final int specialDiscount;
+
+ protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount, int weekTypeDiscount) {
+ this.totalPriceBeforeDiscount = totalPriceBeforeDiscount;
+ this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+ this.christmasDiscount = dateEventDTO.getChristmasDiscount();
+ this.specialDiscount = dateEventDTO.getSpecialDiscount();
+ this.weekTypeDiscount = weekTypeDiscount;
+ this.totalPriceAfterDiscount = initTotalPriceAfterDiscount();
+ }
+
+ protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount) {
+ this.totalPriceBeforeDiscount = totalPriceBeforeDiscount;
+ this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+ this.christmasDiscount = 0;
+ this.specialDiscount = 0;
+ this.weekTypeDiscount = 0;
+ this.totalPriceAfterDiscount = initTotalPriceAfterDiscount();
+ }
+
+ private int initTotalPriceAfterDiscount() {
+ return totalPriceBeforeDiscount - getTotalDiscount();
+ }
+
+ public int getTotalPriceBeforeDiscount() {
+ return totalPriceBeforeDiscount;
+ }
+
+ public int getTotalPriceAfterDiscount() {
+ return totalPriceAfterDiscount;
+ }
+
+ public int getChristmasDiscount() {
+ return christmasDiscount;
+ }
+
+ public int getSpecialDiscount() {
+ return specialDiscount;
+ }
+
+ public int getWeekTypeDiscount() {
+ return weekTypeDiscount;
+ }
+
+ public WeekDiscountType getWeekDiscountType() {
+ return weekDiscountType;
+ }
+
+ public int getTotalDiscount() {
+ return christmasDiscount + weekTypeDiscount + specialDiscount;
+ }
+} | Java | ์ฌ๋ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ static์ผ๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ๋ณด๋ค๋ ์ ๋ ๊ฐ์ธ์ ์ผ๋ก ํด๋น ๊ธฐ๋ฅ์ ํ ์ธ์ด ์์ ๊ฒฝ์ฐ์ ๊ฐ์ฒด๋ฅผ ์ค๋ฒ๋ก๋ฉ์ ํตํด ์์ฑํ๋๊ฒ ์ข๋ค๊ณ ์๊ฐ๋ฉ๋๋ค |
@@ -0,0 +1,91 @@
+package christmas.model.event;
+
+import christmas.model.event.dto.ReservationDateEventDTO;
+import christmas.model.menu.MenuItem;
+
+import java.util.*;
+import java.util.stream.Stream;
+
+public class EventResult {
+ private static final int MIN_EVENT_AMOUNT = 10_000;
+ private static final int GIFT_AMOUNT = 120_000;
+
+ private final Map<MenuItem, Integer> orderMenus;
+ private final DiscountDetail discountDetail;
+ private final GiftMenu giftMenu;
+ private final RewardBadge rewardBadge;
+
+ public EventResult(ReservationDateEventDTO dateEventDTO, Map<MenuItem, Integer> orderMenus) {
+ this.orderMenus = orderMenus;
+ this.discountDetail = initDiscountDetail(dateEventDTO);
+ this.giftMenu = initGiftMenu();
+ this.rewardBadge = initRewardBadge();
+ }
+
+ private DiscountDetail initDiscountDetail(ReservationDateEventDTO dateEventDTO) {
+ int totalPriceBeforeDiscount = getTotalPriceBeforeDiscount();
+ if (totalPriceBeforeDiscount < MIN_EVENT_AMOUNT) {
+ return new DiscountDetail(dateEventDTO, totalPriceBeforeDiscount);
+ }
+ int weekDiscount = calculateWeekDiscount(dateEventDTO);
+
+ return new DiscountDetail(dateEventDTO, totalPriceBeforeDiscount, weekDiscount);
+ }
+
+ private GiftMenu initGiftMenu() {
+ if (discountDetail.getTotalPriceBeforeDiscount() < GIFT_AMOUNT) {
+ return GiftMenu.NONE;
+ }
+
+ return GiftMenu.CHAMPAGNE;
+ }
+
+ private RewardBadge initRewardBadge() {
+ int benefit = discountDetail.getTotalDiscount() + giftMenu.getGiftPrice();
+
+ return Arrays.stream(RewardBadge.values())
+ .filter(badge ->
+ benefit >= badge.getBenefit())
+ .max(Comparator.comparingInt(RewardBadge::getBenefit))
+ .orElse(RewardBadge.NONE);
+ }
+
+ private int getTotalPriceBeforeDiscount() {
+ return generateOrderMenuEntry(orderMenus)
+ .mapToInt(menu ->
+ menu.getKey().getItemPrice() * menu.getValue())
+ .sum();
+ }
+
+ private int calculateWeekDiscount(ReservationDateEventDTO dateEventDTO) {
+ WeekDiscountType type = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+
+ return generateOrderMenuEntry(orderMenus)
+ .filter(menu ->
+ menu.getKey().getMenuCategory() == type.getDiscountCategory())
+ .mapToInt(menu ->
+ menu.getValue() * type.getDiscountPrice())
+ .sum();
+ }
+
+ private <K, V> Stream<Map.Entry<K, V>> generateOrderMenuEntry(Map<K, V> orderMenus) {
+ return orderMenus.entrySet()
+ .stream();
+ }
+
+ public Map<MenuItem, Integer> getOrderMenus() {
+ return Collections.unmodifiableMap(orderMenus);
+ }
+
+ public DiscountDetail getDiscountDetail() {
+ return discountDetail;
+ }
+
+ public GiftMenu getGiftMenu() {
+ return giftMenu;
+ }
+
+ public RewardBadge getRewardBadge() {
+ return rewardBadge;
+ }
+} | Java | ๊ทธ ๋ถ๋ถ์ @Dongwoongkim ๋์ ์ฝ๋๋ฅผ ๋ณด๊ณ ํ์คํ ๊ทธ๋ ๋ค๊ณ ๋๊ปด์ก๋ ๊ฒ ๊ฐ์ต๋๋ค. ํด๋น ๊ฐ์ฒด ์์ ์์ด์ผํ๋ค๊ณ ๋ฐ๋ผ๋ณด๋ ๊ทธ๋ ๊ฒ ์๊ฐ์ ๋ชปํ๋ ๊ฒ ๊ฐ์์.. EventResult๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ด ์ ๋ ๋ ์ฌ๋ฐ๋ฅธ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,28 @@
+package christmas.model.event;
+
+public enum SpecialDayDiscount {
+ THIRD_DAY(3, 1000),
+ TENTH_DAY(10, 1000),
+ SEVENTEENTH_DAY(17, 1000),
+ TWENTY_FOURTH_DAY(24, 1000),
+ TWENTY_FIFTH_DAY(25, 1000),
+ THIRTY_FIRST_DAY(31, 1000),
+ OTHER_DAY(0, 0),
+ ;
+
+ private final int day;
+ private final int discountPrice;
+
+ SpecialDayDiscount(int day, int discountPrice) {
+ this.day = day;
+ this.discountPrice = discountPrice;
+ }
+
+ public int getDay() {
+ return day;
+ }
+
+ public int getDiscountPrice() {
+ return discountPrice;
+ }
+} | Java | ์ข์ ์๊ฐ์
๋๋ค! enum์ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๋ฃ์ ์๊ฐ์ ๋ชปํ๋ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,79 @@
+package christmas.model.menu;
+
+import christmas.exception.ExceptionMessage;
+import christmas.exception.PromotionException;
+
+import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public class OrderMenus {
+ private static final int MIN_ORDER_MENU_NUMBER = 1;
+ private static final int MAX_ORDER_MENUS_NUMBER = 20;
+
+ private final Map<MenuItem, Integer> orderMenus;
+
+ public OrderMenus(Map<String, Integer> orderMenus) {
+ this.orderMenus = validateOrderMenusByNames(orderMenus);
+ validateOrderMenus();
+ }
+
+ private Map<MenuItem, Integer> validateOrderMenusByNames(Map<String, Integer> orderMenus) {
+ return generateOrderMenuValues(orderMenus)
+ .collect(Collectors.toUnmodifiableMap(
+ entry -> MenuItem.findMenuItemByName(entry.getKey(), ExceptionMessage.INVALID_INPUT_ORDER),
+ Map.Entry::getValue
+ ));
+ }
+
+ private void validateOrderMenus() {
+ validateOrderNumbers();
+ validateTotalOrderNumbers();
+ validateOrderAllBeverage();
+ }
+
+ private void validateOrderNumbers() {
+ generateOrderMenuValues(orderMenus)
+ .filter(menu ->
+ menu.getValue() < MIN_ORDER_MENU_NUMBER)
+ .findAny()
+ .ifPresent(menuName -> throwInvalidOrderException());
+ }
+
+ private void validateTotalOrderNumbers() {
+ int orderNumber = generateOrderMenuValues(orderMenus)
+ .mapToInt(Map.Entry::getValue)
+ .sum();
+
+ if (orderNumber > MAX_ORDER_MENUS_NUMBER) {
+ throwInvalidOrderException();
+ }
+ }
+
+ private void validateOrderAllBeverage() {
+ boolean allBeverage = generateOrderMenuValues(orderMenus)
+ .allMatch(orderMenu ->
+ orderMenu.getKey().getMenuCategory() == MenuCategory.BEVERAGE);
+
+ if (allBeverage) {
+ throwInvalidOrderException();
+ }
+ }
+
+ private <K, V> Stream<Map.Entry<K, V>> generateOrderMenuValues(Map<K, V> orderMenus) {
+ return orderMenus.entrySet()
+ .stream();
+ }
+
+ private void throwInvalidOrderException() {
+ throw new PromotionException(ExceptionMessage.INVALID_INPUT_ORDER);
+ }
+
+ public Map<String, Integer> getOrderMenus() {
+ Map<String, Integer> menuNames = new HashMap<>();
+ orderMenus.forEach((menuItem, quantity) ->
+ menuNames.put(menuItem.getItemName(), quantity));
+
+ return Collections.unmodifiableMap(menuNames);
+ }
+} | Java | ๊ฐ์๋ง์ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด๋ค ๋ณด๋ ํด๋์ค๊ฐ ๋ง์์ ธ ๋ฌด๊ฑฐ์์ง ๊ฒ ๊ฐ์ ์ฌ์ฉํ์ง ์์๋๋ฐ ๋ญ๊ฐ ๋ ๋์์ง๋ ๊ณ ๋ฏผ์ด ๋๋ค์.. |
@@ -0,0 +1,76 @@
+package christmas.run;
+
+import christmas.controller.PromotionController;
+import christmas.exception.PromotionException;
+import christmas.model.event.dto.EventResultDTO;
+import christmas.model.event.dto.ReservationDateEventDTO;
+import christmas.model.menu.dto.OrderMenusDTO;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+import java.util.function.Supplier;
+
+public class PromotionRun {
+ private final PromotionController controller = new PromotionController();
+ private final InputView inputView;
+ private final OutputView outputView;
+
+ public PromotionRun(InputView inputView, OutputView outputView) {
+ this.inputView = inputView;
+ this.outputView = outputView;
+ }
+
+ public void runPromotion() {
+ ReservationDateEventDTO dateEventDto = inputCorrectReservationDate();
+ OrderMenusDTO orderMenusDto = inputCorrectOrderMenus();
+ displayEventPreview(dateEventDto);
+
+ EventResultDTO responseDto = applyEventsAndGenerateResult(dateEventDto, orderMenusDto);
+ outputEventResult(responseDto);
+ }
+
+ private ReservationDateEventDTO inputCorrectReservationDate() {
+ outputView.displayStartPromotion();
+
+ return getCorrectResult(this::generateDateEvent);
+ }
+
+ private OrderMenusDTO inputCorrectOrderMenus() {
+ return getCorrectResult(this::receiveMenus);
+ }
+
+ private ReservationDateEventDTO generateDateEvent() {
+ String inputDate = inputView.inputDate();
+
+ return controller.generateEvent(inputDate);
+ }
+
+ private OrderMenusDTO receiveMenus() {
+ String inputMenus = inputView.inputOrderMenus();
+
+ return controller.receiveOrderMenus(inputMenus);
+ }
+
+ private void displayEventPreview(ReservationDateEventDTO dateEventDto) {
+ outputView.displayPreviewEvent(dateEventDto.getDay());
+ }
+
+ private EventResultDTO applyEventsAndGenerateResult(ReservationDateEventDTO dateEventDTO,
+ OrderMenusDTO orderMenusDto) {
+ return controller.applyEvents(dateEventDTO, orderMenusDto);
+ }
+
+ private void outputEventResult(EventResultDTO responseDto) {
+ outputView.outputEventResult(responseDto);
+ }
+
+ private <T> T getCorrectResult(Supplier<T> supplier) {
+ while (true) {
+ try {
+ return supplier.get();
+ } catch (PromotionException e) {
+ outputView.displayException(e);
+ }
+ }
+ }
+} | Java | ํด๋น ๋ถ๋ถ์ ์ ๋ ์ด์ ์ฝ๋๋ฆฌ๋ทฐ๋ฅผ ํตํด ๋ฐฐ์ด ๊ตฌํ ๋ฐฉ๋ฒ์ธ๋ฐ ์ข์ ๋ฐฉ๋ฒ์ธ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,27 @@
+package christmas.util;
+
+enum EventResultText {
+ ORDER_OUTPUT_REGEX("%s %d%s"),
+ EMPTY_TEXT(""),
+ SPACE(" "),
+ MENU_NUMBER("๊ฐ"),
+ MENU_PRICE_UNIT("์"),
+ CHRISTMAS_D_DAY_DISCOUNT("ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ"),
+ SPECIAL_DISCOUNT("ํน๋ณ ํ ์ธ"),
+ GIFT_EVENT("์ฆ์ ์ด๋ฒคํธ"),
+ NONE_BENEFIT("์์"),
+ DISCOUNT_PRICE("-"),
+ BENEFIT_SEPARATOR(": "),
+ NEW_LINE("\n"),
+ ;
+
+ private final String text;
+
+ EventResultText(String text) {
+ this.text = text;
+ }
+
+ public String getText() {
+ return text;
+ }
+} | Java | ๊ฒฐ๊ณผ ๋ฌธ์์ด์ ์ถ๋ ฅํด์ฃผ๊ธฐ ์ํ ์์๋ค์ ๋ฌถ์ด ๋ Enum ์
๋๋ค. ๋ค๋ฅธ ๊ณณ์์ ์ฌ์ฉ๋์ง๋ ์์ ๊ฒ ๊ฐ์ default๋ก enum์ ์ฌ์ฉํ์ด์ |
@@ -0,0 +1,62 @@
+package christmas.model.event;
+
+import christmas.model.event.dto.ReservationDateEventDTO;
+
+public class DiscountDetail {
+ private final int totalPriceBeforeDiscount;
+ private final int totalPriceAfterDiscount;
+ private final WeekDiscountType weekDiscountType;
+ private final int christmasDiscount;
+ private final int weekTypeDiscount;
+ private final int specialDiscount;
+
+ protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount, int weekTypeDiscount) {
+ this.totalPriceBeforeDiscount = totalPriceBeforeDiscount;
+ this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+ this.christmasDiscount = dateEventDTO.getChristmasDiscount();
+ this.specialDiscount = dateEventDTO.getSpecialDiscount();
+ this.weekTypeDiscount = weekTypeDiscount;
+ this.totalPriceAfterDiscount = initTotalPriceAfterDiscount();
+ }
+
+ protected DiscountDetail(ReservationDateEventDTO dateEventDTO, int totalPriceBeforeDiscount) {
+ this.totalPriceBeforeDiscount = totalPriceBeforeDiscount;
+ this.weekDiscountType = WeekDiscountType.checkWeekType(dateEventDTO.getDateWeek());
+ this.christmasDiscount = 0;
+ this.specialDiscount = 0;
+ this.weekTypeDiscount = 0;
+ this.totalPriceAfterDiscount = initTotalPriceAfterDiscount();
+ }
+
+ private int initTotalPriceAfterDiscount() {
+ return totalPriceBeforeDiscount - getTotalDiscount();
+ }
+
+ public int getTotalPriceBeforeDiscount() {
+ return totalPriceBeforeDiscount;
+ }
+
+ public int getTotalPriceAfterDiscount() {
+ return totalPriceAfterDiscount;
+ }
+
+ public int getChristmasDiscount() {
+ return christmasDiscount;
+ }
+
+ public int getSpecialDiscount() {
+ return specialDiscount;
+ }
+
+ public int getWeekTypeDiscount() {
+ return weekTypeDiscount;
+ }
+
+ public WeekDiscountType getWeekDiscountType() {
+ return weekDiscountType;
+ }
+
+ public int getTotalDiscount() {
+ return christmasDiscount + weekTypeDiscount + specialDiscount;
+ }
+} | Java | ํ๋๋ก ๊ฐ์ง๊ณ ์๋๊ฒ ์ฌ๋ฌ๋ฒ ํธ์ถ ๋ ๊ฒฝ์ฐ ๋ฐ์ดํฐ๋ง ๊ฐ์ง๊ณ ์ค๋ ๊ฒ์ด ์ข๋ค ์๊ฐํด์ ํด๋น ๋ฐฉ์์ผ๋ก ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค ์๊ฐํ๋๋ฐ ๋ช๋ช ํ๋๋ ๋ฉ์๋๋ก ํด๋น ํ๋๋ง์ ๊ฐ์ง๊ณ ๋ฐํํ ์ ์์ ๊ฒ ๊ฐ๋ค์ |
@@ -0,0 +1,105 @@
+package christmas.model.event.dto;
+
+public class EventResultDTO {
+ private final String orderMenus;
+ private final String totalPriceBeforeDiscount;
+ private final String giftMenu;
+ private final String benefitHistory;
+ private final String totalBenefit;
+ private final String totalPriceAfterDiscount;
+ private final String rewardBadge;
+
+ private EventResultDTO(Builder builder) {
+ this.orderMenus = builder.orderMenus;
+ this.totalPriceBeforeDiscount = builder.totalPriceBeforeDiscount;
+ this.giftMenu = builder.giftMenu;
+ this.benefitHistory = builder.benefitHistory;
+ this.totalBenefit = builder.totalBenefit;
+ this.totalPriceAfterDiscount = builder.totalPriceAfterDiscount;
+ this.rewardBadge = builder.rewardBadge;
+ }
+
+ public static class Builder {
+ private String orderMenus;
+ private String totalPriceBeforeDiscount;
+ private String giftMenu;
+ private String benefitHistory;
+ private String totalBenefit;
+ private String totalPriceAfterDiscount;
+ private String rewardBadge;
+
+ public Builder() {
+ }
+
+ public Builder orderMenus(String orderMenus) {
+ this.orderMenus = orderMenus;
+ return this;
+ }
+
+ public Builder totalPriceBeforeDiscount(String totalPriceBeforeDiscount) {
+ this.totalPriceBeforeDiscount = totalPriceBeforeDiscount;
+ return this;
+ }
+
+ public Builder giftMenu(String giftMenu) {
+ this.giftMenu = giftMenu;
+ return this;
+ }
+
+ public Builder benefitHistory(String benefitHistory) {
+ this.benefitHistory = benefitHistory;
+ return this;
+ }
+
+ public Builder totalBenefit(String totalBenefit) {
+ this.totalBenefit = totalBenefit;
+ return this;
+ }
+
+ public Builder totalPriceAfterDiscount(String totalPriceAfterDiscount) {
+ this.totalPriceAfterDiscount = totalPriceAfterDiscount;
+ return this;
+ }
+
+ public Builder rewardBadge(String rewardBadge) {
+ this.rewardBadge = rewardBadge;
+ return this;
+ }
+
+ public EventResultDTO build() {
+ return new EventResultDTO(this);
+ }
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public String getOrderMenus() {
+ return orderMenus;
+ }
+
+ public String getTotalPriceBeforeDiscount() {
+ return totalPriceBeforeDiscount;
+ }
+
+ public String getGiftMenu() {
+ return giftMenu;
+ }
+
+ public String getBenefitHistory() {
+ return benefitHistory;
+ }
+
+ public String getTotalBenefit() {
+ return totalBenefit;
+ }
+
+ public String getTotalPriceAfterDiscount() {
+ return totalPriceAfterDiscount;
+ }
+
+ public String getRewardBadge() {
+ return rewardBadge;
+ }
+} | Java | ๋น๋ ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ฉด null ์ธ ๊ฐ์ด ์ฌ ์ ๋ ์์ํ
๋ฐ ์ ๋ถ ๋ค ํ์ํด์ด๋ ๋ณ์๋ค์ธ๋ฐ ์์ฑ์๋ฅผ ์ด์ฉํ์ง ์๊ณ ๋น๋ํด๋์ค๋ฅผ ํ์ฉํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,55 @@
+package christmas.model.menu;
+
+import christmas.exception.ExceptionMessage;
+import christmas.exception.PromotionException;
+
+import java.util.Arrays;
+
+public enum MenuItem {
+ APPETIZER_MUSHROOM_SOUP("์์ก์ด์ํ", 6_000, MenuCategory.APPETIZER),
+ APPETIZER_TAPAS("ํํ์ค", 5_500, MenuCategory.APPETIZER),
+ APPETIZER_CAESAR_SALAD("์์ ์๋ฌ๋", 8_000, MenuCategory.APPETIZER),
+
+ MAIN_T_BONE_STEAK("ํฐ๋ณธ์คํ
์ดํฌ", 55_000, MenuCategory.MAIN_COURSE),
+ MAIN_BBQ_RIB("๋ฐ๋นํ๋ฆฝ", 54_000, MenuCategory.MAIN_COURSE),
+ MAIN_SEAFOOD_PASTA("ํด์ฐ๋ฌผํ์คํ", 35_000, MenuCategory.MAIN_COURSE),
+ MAIN_CHRISTMAS_PASTA("ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25_000, MenuCategory.MAIN_COURSE),
+
+ DESSERT_CHOCO_CAKE("์ด์ฝ์ผ์ดํฌ", 15_000, MenuCategory.DESSERT),
+ DESSERT_ICE_CREAM("์์ด์คํฌ๋ฆผ", 5_000, MenuCategory.DESSERT),
+
+ BEVERAGE_ZERO_COLA("์ ๋ก์ฝ๋ผ", 3_000, MenuCategory.BEVERAGE),
+ BEVERAGE_RED_WINE("๋ ๋์์ธ", 60_000, MenuCategory.BEVERAGE),
+ BEVERAGE_CHAMPAGNE("์ดํ์ธ", 25_000, MenuCategory.BEVERAGE),
+ ;
+
+ private final String itemName;
+ private final int itemPrice;
+ private final MenuCategory menuCategory;
+
+ MenuItem(String itemName, int itemPrice, MenuCategory menuCategory) {
+ this.itemName = itemName;
+ this.itemPrice = itemPrice;
+ this.menuCategory = menuCategory;
+ }
+
+ public static MenuItem findMenuItemByName(String itemName, ExceptionMessage message) {
+ return Arrays.stream(MenuItem.values())
+ .filter(menu -> menu.getItemName().equals(itemName))
+ .findFirst()
+ .orElseThrow(() ->
+ new PromotionException(message));
+ }
+
+ public String getItemName() {
+ return itemName;
+ }
+
+ public int getItemPrice() {
+ return itemPrice;
+ }
+
+ public MenuCategory getMenuCategory() {
+ return menuCategory;
+ }
+} | Java | ๋ฉ๋ด ์์ดํ
CRUD API ๋ฅผ ๋ง๋ค๋ enum ์ผ๋ก ํ๊ฒ ๋๋ฉด ์ถ๊ฐ์ ๋ํด ํ์ฅ์ฑ์ด ๋จ์ด์ง๋๊ฒ์ด ์๋๊ฐ ์๊ฐ์ด๋ญ๋๋ค! |
@@ -0,0 +1,57 @@
+package christmas.model.event;
+
+import christmas.model.date.ReservationDate;
+
+import java.util.Arrays;
+
+public class ReservationDateEvent {
+ private static final int WEEK = 7;
+ private static final int FRIDAY = 2;
+ private static final int SATURDAY = 3;
+
+ private final ReservationDate reservationDate;
+ private final ChristmasDiscount christmasDiscount;
+ private final WeekDiscountType discountDateWeek;
+ private final SpecialDayDiscount specialDiscountDate;
+
+ public ReservationDateEvent(int day) {
+ this.reservationDate = new ReservationDate(day);
+ this.christmasDiscount = new ChristmasDiscount(reservationDate);
+ this.discountDateWeek = initDiscountDateWeek(reservationDate);
+ this.specialDiscountDate = initSpecialDiscountDate(reservationDate);
+ }
+
+ private WeekDiscountType initDiscountDateWeek(ReservationDate reservationDate) {
+ if (reservationDate.day() % WEEK == FRIDAY ||
+ reservationDate.day() % WEEK == SATURDAY) {
+
+ return WeekDiscountType.WEEKEND;
+ }
+
+ return WeekDiscountType.WEEKDAY;
+ }
+
+ private SpecialDayDiscount initSpecialDiscountDate(ReservationDate reservationDate) {
+ return Arrays.stream(SpecialDayDiscount.values())
+ .filter(specialDay ->
+ reservationDate.day() == specialDay.getDay())
+ .findFirst()
+ .orElse(SpecialDayDiscount.OTHER_DAY);
+ }
+
+ public ReservationDate getReservationDate() {
+ return reservationDate;
+ }
+
+ public ChristmasDiscount getChristmasDiscount() {
+ return christmasDiscount;
+ }
+
+ public WeekDiscountType getDiscountDateWeek() {
+ return discountDateWeek;
+ }
+
+ public SpecialDayDiscount getSpecialDiscountDate() {
+ return specialDiscountDate;
+ }
+} | Java | ์ด๋ ๊ฒ ๋๋ฉด ์ด๋ฒคํธ๊ฐ ์๋ฐฑ๊ฐ๊ฐ ๋๋ฉด ์๋ฐฑ๊ฐ๋ฅผ ์ง์ ๋ค ๊ตฌํํด์ค์ผ ๋์ ๋นํจ์จ์ ์ธ๊ฒ ๊ฐ์ต๋๋ค!
๋ชจ๋ ํ ์ธ์ ์ ์ฉํ๋ ๊ฒ์ด๋ inteface Discount ๋ฅผ ๋ง๋ค์ด
```
Discount ๋ฅผ ๊ตฌํํ ReservationDate implements Discount
ChristmasDiscount implements Discount ........
```
์ด๋ ๊ฒ ๋ง๋ค์ด์ List<DIscount> discountList ์ด๋ฐ์์ผ๋ก ๋ง๋ค์ด์ ๊ณตํตํจ์ ๋ถ๋ถ๋ง ๋ก์ง์ ๋ฃ์ด์ฃผ์๋ฉด ๊ด๋ฆฌ๊ฐ ์ฌ์ธ๊ฒ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,13 @@
+package msa.customer.exception;
+
+public class DeliveryCustomerException extends RuntimeException {
+
+ public DeliveryCustomerException(String message) {
+ super(message);
+ }
+
+ public DeliveryCustomerException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+} | Java | (C) Exception์ ์๋น์ค์ `์ ์ฑ
์๋ฐ`์ ๋ํ๋ด๋ documentation์ ์ญํ ์ ํ๊ธฐ๋ ํฉ๋๋ค.
๋ฐ๋ผ์ Exception์ ํตํด ์๋น์ค ์ ์ฑ
์ ์๋ฐํ๋ ๊ฒฝ์ฐ, ์ ์ฑ
์ ๋ช
ํํ๊ฒ ๋ํ๋ผ ์ ์๋ ์์ฒด exception์ ์ ์ํ๊ณค ํ๋๋ฐ delivery application์ ์ ์ฉํด๋ณผ๋งํ exception ๋ช ๊ฐ๋ฅผ ๊ฐ๋ตํ๊ฒ ๊ตฌ์ฑํด๋ดค์ผ๋ ์ฐธ๊ณ ํด๋ณด์๊ณ Exception๋ค์ ์ ์ ์ํด์ฃผ์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค :) |
@@ -3,6 +3,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import msa.customer.entity.store.Store;
+import msa.customer.exception.store.StoreEmptyException;
import msa.customer.service.store.StoreService;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
@@ -11,6 +12,7 @@
import java.util.Optional;
public class StoreCheckInterceptor implements HandlerInterceptor {
+
private final StoreService storeService;
public StoreCheckInterceptor(StoreService storeService) {
@@ -19,13 +21,14 @@ public StoreCheckInterceptor(StoreService storeService) {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
- String storeId = (String) pathVariables.get("storeId");
+ Map pathVariables = (Map)request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
+ String storeId = (String)pathVariables.get("storeId");
Optional<Store> storeOptional = storeService.getStore(storeId);
- if(storeOptional.isEmpty()){
- throw new NullPointerException("Store doesn't exist. " + storeId + " is not correct store id.");
+ if (storeOptional.isEmpty()) {
+ throw new StoreEmptyException(storeId);
}
request.setAttribute("storeEntity", storeOptional.get());
return true;
}
+
} | Java | (C) optional์ ๋ฒ๊ฒจ์ง ์ํ๋ก ์ค๋๊ฒ ์ข๊ฒ ์ง๋ง, ์ผ๋จ ํ์ฌ ์ํ๋ฅผ ๊ฐ์ ํ๊ณ custom exception์ throw ํ์ต๋๋ค |
@@ -6,6 +6,7 @@
import msa.restaurant.dto.store.StoreResponseDto;
import msa.restaurant.entity.store.Store;
import msa.restaurant.dto.store.StoreSqsDto;
+import msa.restaurant.exception.store.StoreCreationFailedException;
import msa.restaurant.service.member.MemberService;
import msa.restaurant.sqs.SendingMessageConverter;
import msa.restaurant.service.store.StoreService;
@@ -26,16 +27,19 @@ public class StoreController {
private final SendingMessageConverter sendingMessageConverter;
private final SqsService sqsService;
- public StoreController(StoreService storeService, SendingMessageConverter sendingMessageConverter, MemberService memberService, SqsService sqsService) {
+ public StoreController(StoreService storeService,
+ SendingMessageConverter sendingMessageConverter,
+ MemberService memberService,
+ SqsService sqsService) {
this.storeService = storeService;
this.sendingMessageConverter = sendingMessageConverter;
this.sqsService = sqsService;
}
@GetMapping
@ResponseStatus(HttpStatus.OK)
- public List<StorePartResponseDto> storeList (
- @RequestAttribute("cognitoUsername") String managerId) {
+ public List<StorePartResponseDto> storeList(
+ @RequestAttribute("cognitoUsername") String managerId) {
List<Store> storeList = storeService.getStoreList(managerId);
List<StorePartResponseDto> storeListDto = new ArrayList<>();
storeList.forEach(store -> {
@@ -46,12 +50,12 @@ public List<StorePartResponseDto> storeList (
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
- public void createStore (@RequestAttribute("cognitoUsername") String managerId,
- @RequestBody StoreRequestDto data) {
+ public void createStore(@RequestAttribute("cognitoUsername") String managerId,
+ @RequestBody StoreRequestDto data) {
String storeId = storeService.createStore(data, managerId);
Optional<Store> storeOptional = storeService.getStore(storeId);
- if (storeOptional.isEmpty()){
- throw new RuntimeException("Store creation failed.");
+ if (storeOptional.isEmpty()) {
+ throw new StoreCreationFailedException(storeId);
}
Store store = storeOptional.get();
StoreSqsDto storeSqsDto = new StoreSqsDto(store);
@@ -62,16 +66,16 @@ public void createStore (@RequestAttribute("cognitoUsername") String managerId,
@GetMapping("/{storeId}")
@ResponseStatus(HttpStatus.OK)
- public StoreResponseDto storeInfo (@RequestAttribute("cognitoUsername") String managerId,
- @RequestAttribute("store") Store store) {
+ public StoreResponseDto storeInfo(@RequestAttribute("cognitoUsername") String managerId,
+ @RequestAttribute("store") Store store) {
return new StoreResponseDto(store);
}
@PutMapping("/{storeId}")
@ResponseStatus(HttpStatus.OK)
public void updateStore(@RequestAttribute("cognitoUsername") String managerId,
@PathVariable String storeId,
- @RequestBody StoreRequestDto data) {
+ @RequestBody StoreRequestDto data) {
storeService.updateStore(storeId, data);
Optional<Store> storeOptional = storeService.getStore(storeId);
Store store = storeOptional.get();
@@ -85,9 +89,9 @@ public void updateStore(@RequestAttribute("cognitoUsername") String managerId,
@ResponseStatus(HttpStatus.CREATED)
public void changeStoreStatus(@RequestAttribute("cognitoUsername") String managerId,
@PathVariable String storeId,
- @RequestBody boolean open){
+ @RequestBody boolean open) {
String messageToChangeStatus;
- if (open){
+ if (open) {
storeService.openStore(storeId);
messageToChangeStatus = sendingMessageConverter.createMessageToOpenStore(storeId);
} else {
@@ -101,11 +105,12 @@ public void changeStoreStatus(@RequestAttribute("cognitoUsername") String manage
@DeleteMapping("/{storeId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteStore(@RequestAttribute("cognitoUsername") String managerId,
- @PathVariable String storeId) {
+ @PathVariable String storeId) {
storeService.deleteStore(storeId);
String messageToDeleteStore = sendingMessageConverter.createMessageToDeleteStore(storeId);
sqsService.sendToCustomer(messageToDeleteStore);
sqsService.sendToRider(messageToDeleteStore);
}
+
}
| Java | (C) ์ฌ๊ธฐ๋ StoreCreationFailedException์ ๋์ง์ง๋ง, storeService.createStore ๋ด๋ถ์์ exception ์ฒ๋ฆฌ๋ฅผ ํ๊ณ ๋์ค๋ ๊ฒ์ด ๋ ๋ฐ๋์งํด๋ณด์
๋๋ค. |
@@ -18,39 +18,30 @@ public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
- public void createOrder(Order order){
+ public void createOrder(Order order) {
orderRepository.createOrder(order);
}
- public List<Order> getOrderList(String storeId){
+ public List<Order> getOrderList(String storeId) {
return orderRepository.readOrderList(storeId);
}
- public Optional<Order> getOrder(String orderId){
+ public Optional<Order> getOrder(String orderId) {
return orderRepository.readOrder(orderId);
}
- public OrderStatus changeOrderStatusToOrderAccept(String orderId, OrderStatus orderStatus){
- if(orderStatus.equals(OrderStatus.ORDER_REQUEST)){
- return orderRepository.updateOrderStatus(orderId, OrderStatus.ORDER_ACCEPT);
- } else {
- throw new IllegalStateException("The current order status is not changeable");
- }
- }
+ public OrderStatus changeOrderStatus(Order order, OrderStatus status, OrderStatusUpdatePolicy orderStatusUpdatePolicy) {
+ orderStatusUpdatePolicy.checkStatusUpdatable(status);
- public OrderStatus changeOrderStatusToFoodReady(String orderId, OrderStatus orderStatus){
- if (orderStatus.equals(OrderStatus.RIDER_ASSIGNED)) {
- return orderRepository.updateOrderStatus(orderId, OrderStatus.FOOD_READY);
- } else {
- throw new IllegalStateException("The current order status is not changeable");
- }
+ return orderRepository.updateOrderStatus(order.getOrderId(), status);
}
- public void assignRiderToOrder(String orderId, OrderStatus orderStatus, RiderPartDto riderPartDto){
+ public void assignRiderToOrder(String orderId, OrderStatus orderStatus, RiderPartDto riderPartDto) {
orderRepository.updateRiderInfo(orderId, orderStatus, riderPartDto);
}
- public void changeOrderStatusFromOtherServer(String orderId, OrderStatus orderStatus){
+ public void changeOrderStatusFromOtherServer(String orderId, OrderStatus orderStatus) {
orderRepository.updateOrderStatus(orderId, orderStatus);
}
+
} | Java | (C) changeOrderStatusTo~ ๋ฉ์๋์ ๋ด์ฉ์ด ๋๋ถ๋ถ ์ค๋ณต๋์ด์, ํด๋น ๋ฉ์๋๋ค์ changeOrderStatus๋ผ๋ ๋ฉ์๋๋ก ํตํฉํ์์ต๋๋ค.
์ฌ๊ธฐ์ ์๋ก์ด ํด๋์ค์ธ OrderStatusUpdatePolicy๊ฐ ๋ง๋ค์ด์ก๋๋ฐ, ~Policy๋ผ๋ ๋ช
์นญ์ ์๋น์ค ์ ์ฑ
์ ํํํ๊ธฐ ์ํด ๋ฒ์ฉ์ ์ผ๋ก ์ฌ์ฉํ๋ ํด๋์ค ๋ค์ด๋ฐ ์ค ํ๋์
๋๋ค.
delivery-application์๋ ์๋น์ค ์ด์์ ํ์ํ ์ฌ๋ฌ๊ฐ์ง ์ ์ฑ
๋ค์ ๊ฐ๊ณ ์์ํ
๋ฐ์, ์ด๋ฐ ์ ์ฑ
๋ค์ ํด๋์ค๋ก ๋๋ฌ๋ด์ง ์๊ณ ๋ฉ์๋๋ ๋ก์ง์ผ๋ก๋ง ๋ํ๋ด๋ฉด ์ ์ฑ
๋ค์ด ๋ถ์ฐ๋๊ณ , ์์์ ์ผ๋ก ํํ๋์ด์ ์ฐ๋ฆฌ ์๋น์ค๊ฐ ์ด๋ค ์ ์ฑ
๋ค์ ๊ฐ๊ณ ์๋์ง ๋ช
ํํ ํ์
ํ๊ธฐ ์ด๋ ต์ต๋๋ค. (์ด๋ ๊ฒ ๋๋ฉด ์๊ฐ์ด ์ง๋๋ฉด์ ์ฝ๋๊ฐ ์๋น์ค ์ ์ฑ
์ ๋ฐ๋ผ๊ฐ์ง ๋ชปํ๋ ์ํฉ๋ ์ข
์ข
์๊น๋๋ค)
DDD๋ฅผ ์์ง ์ฝ์ด๋ณด์ ์ง๋ ๋ชจ๋ฅด๊ฒ ์ง๋ง, [์์์ ์ธ ๊ฐ๋
์ ๋ช
ํํ๊ฒ](https://dhsim86.github.io/programming/2019/05/16/domain_driven_design_09-post.html)๋ผ๋ chapter๊ฐ ์๋๋ฐ ์๊ฐ๋์ค ๋ ๋งํฌ ํ๋ฒ ์ฝ์ด๋ณด์๊ณ , ํ์ฌ ์ฐ๋ฆฌ ์๋น์ค์ ์ฌ๊ธฐ์ ๊ธฐ ์จ์ด์๋ ์ ์ฑ
๋ค์ ์ด๋ค ๊ฒ๋ค์ด ์์์ง ์ฐพ์๋ณด์
์ ๋ช
ํํ๊ฒ ํด๋์ค๋ก ๋ํ๋ด๋ณด์ ๋ค๋ฉด ๋ง์ ๋์์ด ๋์ค ๊ฒ ๊ฐ์ต๋๋ค :) |
@@ -0,0 +1,13 @@
+package msa.customer.exception;
+
+public class DeliveryCustomerException extends RuntimeException {
+
+ public DeliveryCustomerException(String message) {
+ super(message);
+ }
+
+ public DeliveryCustomerException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+} | Java | ์ฐธ๊ณ ํ๊ฒ ์ต๋๋ค. |
@@ -3,21 +3,17 @@
<mapper namespace="com.midnear.midnearshopping.mapper.coupon_point.PointMapper">
<insert id="grantPoints" useGeneratedKeys="true" keyProperty="pointId">
INSERT INTO point (
- amount, reason
+ amount, reason, review_id, user_id, grant_date
) VALUES (
- #{amount}, #{reason}
+ #{amount}, #{reason}, #{reviewId}, #{userId}, NOW()
)
</insert>
- <insert id="setReviewPointAmount">
- INSERT INTO review_point (
- text_review, photo_review
- ) VALUES (
- #{textReview}, #{photoReview}
- )
- </insert>
- <delete id="deletePreviousData">
- delete from review_point
- </delete>
+ <select id="getProductInfoByReviewId" resultType="java.lang.String">
+ select concat(op.product_name, ' _ ', op.color)
+ from reviews r
+ join order_products op on r.order_product_id = op.order_product_id
+ where review_id = #{reviewId}
+ </select>
<!--์๋ ํ์ผ ๋ฐ๋ก ๋บด๊ธฐ ์ ๋งคํด์ ์ฌ๊ธฐ์ ๋ฃ์-->
<select id="getPointList">
select | Unknown | ๊ฐ์ฌํฉ๋๋ค.. |
@@ -0,0 +1,58 @@
+package christmas.domain;
+
+import java.time.DayOfWeek;
+import java.time.LocalDate;
+
+public class Date {
+ public static final String DATE_RE_READ_REQUEST_MESSAGE = "์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final LocalDate CHRISTMAS_DAY = LocalDate.of(2023, 12, 25);
+ private static final int YEAR = 2023;
+ private static final int MONTH = 12;
+ private static final int DATE_MIN_NUMBER = 1;
+ private static final int DATE_MAX_NUMBER = 31;
+
+ private final int dayNumber;
+
+ public Date(int dayNumber) {
+ validateRange(dayNumber);
+ this.dayNumber = dayNumber;
+ }
+
+ private void validateRange(int dayNumber) {
+ if ((dayNumber < DATE_MIN_NUMBER) || (dayNumber > DATE_MAX_NUMBER)) {
+ throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE);
+ }
+ }
+
+ public boolean isDDayDiscountActive() {
+ LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber);
+
+ return localDate.isBefore(CHRISTMAS_DAY.plusDays(1));
+ }
+
+ public boolean isWeekdayDiscountActive() {
+ LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber);
+ DayOfWeek day = localDate.getDayOfWeek();
+
+ return (day.equals(DayOfWeek.MONDAY) || day.equals(DayOfWeek.TUESDAY) || day.equals(DayOfWeek.WEDNESDAY)
+ || day.equals(DayOfWeek.THURSDAY) || day.equals(DayOfWeek.SUNDAY));
+ }
+
+ public boolean isWeekendDiscountActive() {
+ LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber);
+ DayOfWeek day = localDate.getDayOfWeek();
+
+ return (day.equals(DayOfWeek.FRIDAY) || day.equals(DayOfWeek.SATURDAY));
+ }
+
+ public boolean isSpecialDiscountActive() {
+ LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber);
+ DayOfWeek day = localDate.getDayOfWeek();
+
+ return (day.equals(DayOfWeek.SUNDAY) || localDate.equals(CHRISTMAS_DAY));
+ }
+
+ public int getDayNumber() {
+ return dayNumber;
+ }
+}
\ No newline at end of file | Java | (์ด๋ฐ ๋ฐฉ๋ฒ๋ ์์ด์!)
```suggestion
try {
LocalDate.of(YEAR, MONTH, dayNumber);
} catch (NumberFormatException exception) {
throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE);
}
```
์ด๋ฐ์์ผ๋ก LocalDate๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์์ต๋๋ค! |
@@ -0,0 +1,100 @@
+package christmas.domain;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class EventProcess {
+ private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT = 120000;
+ private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT = 10000;
+ private static final int D_DAY_DISCOUNT_AMOUNT_PER_DAY = 100;
+ private static final int D_DAY_DISCOUNT_DEFAULT_AMOUNT = 1000;
+ private static final int FIRST_DAY_NUMBER = 1;
+ private static final int WEEKDAY_DESSERT_DISCOUNT_AMOUNT = 2023;
+ private static final int WEEKEND_MAIN_DISCOUNT_AMOUNT = 2023;
+ private static final int SPECIAL_DISCOUNT_AMOUNT = 1000;
+
+ public EventResult takeAllBenefit(Date date, Order order) {
+ Map<BenefitType, Integer> allBenefit = new HashMap<>();
+ allBenefit.put(BenefitType.GIFT, takeGift(order));
+ allBenefit.put(BenefitType.D_DAY, takeDDayDiscount(date));
+ allBenefit.put(BenefitType.WEEKDAY, takeWeekdayDiscount(date, order));
+ allBenefit.put(BenefitType.WEEKEND, takeWeekendDiscount(date, order));
+ allBenefit.put(BenefitType.SPECIAL, takeSpecialDiscount(date));
+
+ cancelAllBenefitIfMeetCondition(order, allBenefit);
+
+ return new EventResult(allBenefit);
+ }
+
+ private int takeGift(Order order) {
+ if (order.calculateTotalOrderPrice() >= MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT) {
+ return Menu.CHAMPAGNE.getPrice();
+ }
+
+ return 0;
+ }
+
+ private int takeDDayDiscount(Date date) {
+ if (date.isDDayDiscountActive()) {
+ return D_DAY_DISCOUNT_DEFAULT_AMOUNT
+ + (calculateDifferenceBetweenFirstDay(date) * D_DAY_DISCOUNT_AMOUNT_PER_DAY);
+ }
+
+ return 0;
+ }
+
+ private int calculateDifferenceBetweenFirstDay(Date date) {
+ return date.getDayNumber() - FIRST_DAY_NUMBER;
+ }
+
+ private int takeWeekdayDiscount(Date date, Order order) {
+ int discount = 0;
+
+ for (OrderedMenu orderedMenu : order.getOrder()) {
+ Menu menu = Menu.decideMenu(orderedMenu.getMenuName());
+ if (date.isWeekdayDiscountActive() && MenuType.decideMenuType(menu) == MenuType.DESSERT) {
+ discount += orderedMenu.getQuantity() * WEEKDAY_DESSERT_DISCOUNT_AMOUNT;
+ }
+ }
+
+ return discount;
+ }
+
+ private int takeWeekendDiscount(Date date, Order order) {
+ int discount = 0;
+
+ for (OrderedMenu orderedMenu : order.getOrder()) {
+ Menu menu = Menu.decideMenu(orderedMenu.getMenuName());
+ if (date.isWeekendDiscountActive() && MenuType.decideMenuType(menu) == MenuType.MAIN) {
+ discount += orderedMenu.getQuantity() * WEEKEND_MAIN_DISCOUNT_AMOUNT;
+ }
+ }
+
+ return discount;
+ }
+
+ private int takeSpecialDiscount(Date date) {
+ if (date.isSpecialDiscountActive()) {
+ return SPECIAL_DISCOUNT_AMOUNT;
+ }
+
+ return 0;
+ }
+
+ private Map<BenefitType, Integer> cancelAllBenefitIfMeetCondition(Order order,
+ Map<BenefitType, Integer> allBenefit) {
+ if (isInsufficientTotalOrderPriceForEvent(order)) {
+ allBenefit.replace(BenefitType.GIFT, 0);
+ allBenefit.replace(BenefitType.D_DAY, 0);
+ allBenefit.replace(BenefitType.WEEKDAY, 0);
+ allBenefit.replace(BenefitType.WEEKEND, 0);
+ allBenefit.replace(BenefitType.SPECIAL, 0);
+ }
+
+ return allBenefit;
+ }
+
+ private boolean isInsufficientTotalOrderPriceForEvent(Order order) {
+ return order.calculateTotalOrderPrice() < MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT;
+ }
+}
\ No newline at end of file | Java | (๊ฐ์ธ์ ์ธ ์๊ฐ์
๋๋ค!)
ํ ์ธ์ ์ ์ฉํ๊ณ ์กฐ๊ฑด์ ๋ง์ง ์์ผ๋ฉด ๋๋๋ฆฌ๋ ๋ฐฉ๋ฒ๋ณด๋ค๋ ์ฒ์๋ถํฐ ์กฐ๊ฑด์ ๋ง์กฑํ์ง ์์ผ๋ฉด ํ ์ธ์ ์ ์ฉํ์ง ์๋ ๊ฒ๋ ํ๋์ ๋ฐฉ๋ฒ์ผ ๊ฒ ๊ฐ์์!
```suggestion
if (this.isInsufficientTotalOrderPriceForEvent(order)) {
return EnumSet.allOf(BenefitType.class)
.stream()
.collect(collectingAndThen(
toMap(Function.identity(), (benefit) -> 0),
EventResult::new)
);
}
```
์ ์ฝ๋์์ stream์ ํ์ฉํ๋๋ฐ BenefitType์ ๋ชจ๋ ์์๋ฅผ ๊ฐ์ ธ์จ๋ค์ 0์ผ๋ก ์ด๊ธฐํํ๊ณ ๋์จ ๊ฒฐ๊ณผ๋ฌผ์ EventResult๋ก ๋ํํ๋ ์ฝ๋์
๋๋ค :) |
@@ -0,0 +1,100 @@
+package christmas.domain;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class EventProcess {
+ private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT = 120000;
+ private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT = 10000;
+ private static final int D_DAY_DISCOUNT_AMOUNT_PER_DAY = 100;
+ private static final int D_DAY_DISCOUNT_DEFAULT_AMOUNT = 1000;
+ private static final int FIRST_DAY_NUMBER = 1;
+ private static final int WEEKDAY_DESSERT_DISCOUNT_AMOUNT = 2023;
+ private static final int WEEKEND_MAIN_DISCOUNT_AMOUNT = 2023;
+ private static final int SPECIAL_DISCOUNT_AMOUNT = 1000;
+
+ public EventResult takeAllBenefit(Date date, Order order) {
+ Map<BenefitType, Integer> allBenefit = new HashMap<>();
+ allBenefit.put(BenefitType.GIFT, takeGift(order));
+ allBenefit.put(BenefitType.D_DAY, takeDDayDiscount(date));
+ allBenefit.put(BenefitType.WEEKDAY, takeWeekdayDiscount(date, order));
+ allBenefit.put(BenefitType.WEEKEND, takeWeekendDiscount(date, order));
+ allBenefit.put(BenefitType.SPECIAL, takeSpecialDiscount(date));
+
+ cancelAllBenefitIfMeetCondition(order, allBenefit);
+
+ return new EventResult(allBenefit);
+ }
+
+ private int takeGift(Order order) {
+ if (order.calculateTotalOrderPrice() >= MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT) {
+ return Menu.CHAMPAGNE.getPrice();
+ }
+
+ return 0;
+ }
+
+ private int takeDDayDiscount(Date date) {
+ if (date.isDDayDiscountActive()) {
+ return D_DAY_DISCOUNT_DEFAULT_AMOUNT
+ + (calculateDifferenceBetweenFirstDay(date) * D_DAY_DISCOUNT_AMOUNT_PER_DAY);
+ }
+
+ return 0;
+ }
+
+ private int calculateDifferenceBetweenFirstDay(Date date) {
+ return date.getDayNumber() - FIRST_DAY_NUMBER;
+ }
+
+ private int takeWeekdayDiscount(Date date, Order order) {
+ int discount = 0;
+
+ for (OrderedMenu orderedMenu : order.getOrder()) {
+ Menu menu = Menu.decideMenu(orderedMenu.getMenuName());
+ if (date.isWeekdayDiscountActive() && MenuType.decideMenuType(menu) == MenuType.DESSERT) {
+ discount += orderedMenu.getQuantity() * WEEKDAY_DESSERT_DISCOUNT_AMOUNT;
+ }
+ }
+
+ return discount;
+ }
+
+ private int takeWeekendDiscount(Date date, Order order) {
+ int discount = 0;
+
+ for (OrderedMenu orderedMenu : order.getOrder()) {
+ Menu menu = Menu.decideMenu(orderedMenu.getMenuName());
+ if (date.isWeekendDiscountActive() && MenuType.decideMenuType(menu) == MenuType.MAIN) {
+ discount += orderedMenu.getQuantity() * WEEKEND_MAIN_DISCOUNT_AMOUNT;
+ }
+ }
+
+ return discount;
+ }
+
+ private int takeSpecialDiscount(Date date) {
+ if (date.isSpecialDiscountActive()) {
+ return SPECIAL_DISCOUNT_AMOUNT;
+ }
+
+ return 0;
+ }
+
+ private Map<BenefitType, Integer> cancelAllBenefitIfMeetCondition(Order order,
+ Map<BenefitType, Integer> allBenefit) {
+ if (isInsufficientTotalOrderPriceForEvent(order)) {
+ allBenefit.replace(BenefitType.GIFT, 0);
+ allBenefit.replace(BenefitType.D_DAY, 0);
+ allBenefit.replace(BenefitType.WEEKDAY, 0);
+ allBenefit.replace(BenefitType.WEEKEND, 0);
+ allBenefit.replace(BenefitType.SPECIAL, 0);
+ }
+
+ return allBenefit;
+ }
+
+ private boolean isInsufficientTotalOrderPriceForEvent(Order order) {
+ return order.calculateTotalOrderPrice() < MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT;
+ }
+}
\ No newline at end of file | Java | (๊ฐ์ธ์ ์ธ ์๊ฐ์
๋๋ค!)
```suggestion
return new EventResult(new HashMap<>() {{
this.put(BenefitType.GIFT, takeGift(order));
this.put(BenefitType.D_DAY, takeDDayDiscount(date));
this.put(BenefitType.WEEKDAY, takeWeekdayDiscount(date, order));
this.put(BenefitType.WEEKEND, takeWeekendDiscount(date, order));
this.put(BenefitType.SPECIAL, takeSpecialDiscount(date));
}});
```
์ด๋ฐ์์ผ๋ก ์์ฑ๊ณผ ๋์์ HashMap์ ์์๋ฅผ ์ด๊ธฐํ ํ๋ ๋ฐฉ๋ฒ๋ ์์ด์ :) |
@@ -0,0 +1,100 @@
+package christmas.domain;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class EventProcess {
+ private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT = 120000;
+ private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT = 10000;
+ private static final int D_DAY_DISCOUNT_AMOUNT_PER_DAY = 100;
+ private static final int D_DAY_DISCOUNT_DEFAULT_AMOUNT = 1000;
+ private static final int FIRST_DAY_NUMBER = 1;
+ private static final int WEEKDAY_DESSERT_DISCOUNT_AMOUNT = 2023;
+ private static final int WEEKEND_MAIN_DISCOUNT_AMOUNT = 2023;
+ private static final int SPECIAL_DISCOUNT_AMOUNT = 1000;
+
+ public EventResult takeAllBenefit(Date date, Order order) {
+ Map<BenefitType, Integer> allBenefit = new HashMap<>();
+ allBenefit.put(BenefitType.GIFT, takeGift(order));
+ allBenefit.put(BenefitType.D_DAY, takeDDayDiscount(date));
+ allBenefit.put(BenefitType.WEEKDAY, takeWeekdayDiscount(date, order));
+ allBenefit.put(BenefitType.WEEKEND, takeWeekendDiscount(date, order));
+ allBenefit.put(BenefitType.SPECIAL, takeSpecialDiscount(date));
+
+ cancelAllBenefitIfMeetCondition(order, allBenefit);
+
+ return new EventResult(allBenefit);
+ }
+
+ private int takeGift(Order order) {
+ if (order.calculateTotalOrderPrice() >= MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT) {
+ return Menu.CHAMPAGNE.getPrice();
+ }
+
+ return 0;
+ }
+
+ private int takeDDayDiscount(Date date) {
+ if (date.isDDayDiscountActive()) {
+ return D_DAY_DISCOUNT_DEFAULT_AMOUNT
+ + (calculateDifferenceBetweenFirstDay(date) * D_DAY_DISCOUNT_AMOUNT_PER_DAY);
+ }
+
+ return 0;
+ }
+
+ private int calculateDifferenceBetweenFirstDay(Date date) {
+ return date.getDayNumber() - FIRST_DAY_NUMBER;
+ }
+
+ private int takeWeekdayDiscount(Date date, Order order) {
+ int discount = 0;
+
+ for (OrderedMenu orderedMenu : order.getOrder()) {
+ Menu menu = Menu.decideMenu(orderedMenu.getMenuName());
+ if (date.isWeekdayDiscountActive() && MenuType.decideMenuType(menu) == MenuType.DESSERT) {
+ discount += orderedMenu.getQuantity() * WEEKDAY_DESSERT_DISCOUNT_AMOUNT;
+ }
+ }
+
+ return discount;
+ }
+
+ private int takeWeekendDiscount(Date date, Order order) {
+ int discount = 0;
+
+ for (OrderedMenu orderedMenu : order.getOrder()) {
+ Menu menu = Menu.decideMenu(orderedMenu.getMenuName());
+ if (date.isWeekendDiscountActive() && MenuType.decideMenuType(menu) == MenuType.MAIN) {
+ discount += orderedMenu.getQuantity() * WEEKEND_MAIN_DISCOUNT_AMOUNT;
+ }
+ }
+
+ return discount;
+ }
+
+ private int takeSpecialDiscount(Date date) {
+ if (date.isSpecialDiscountActive()) {
+ return SPECIAL_DISCOUNT_AMOUNT;
+ }
+
+ return 0;
+ }
+
+ private Map<BenefitType, Integer> cancelAllBenefitIfMeetCondition(Order order,
+ Map<BenefitType, Integer> allBenefit) {
+ if (isInsufficientTotalOrderPriceForEvent(order)) {
+ allBenefit.replace(BenefitType.GIFT, 0);
+ allBenefit.replace(BenefitType.D_DAY, 0);
+ allBenefit.replace(BenefitType.WEEKDAY, 0);
+ allBenefit.replace(BenefitType.WEEKEND, 0);
+ allBenefit.replace(BenefitType.SPECIAL, 0);
+ }
+
+ return allBenefit;
+ }
+
+ private boolean isInsufficientTotalOrderPriceForEvent(Order order) {
+ return order.calculateTotalOrderPrice() < MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT;
+ }
+}
\ No newline at end of file | Java | (๊ฐ์ธ์ ์ธ ์๊ฐ์
๋๋ค!)
ํ ์ธ ๊ธ์ก์ ๊ณ์ฐํ๊ธฐ ์ ์ ์ฃผ๋ง์ด๋ฉด 0์์ ๋ฐํํ๋ ๋ฐฉ๋ฒ๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์์!
```suggestion
if (date.isWeekendDiscountActive()) {
return 0;
}
return order.getOrder()
.stream()
.filter(orderedMenu -> DESSERT.equals(MenuType.decideMenuType(Menu.decideMenu(orderedMenu.getMenuName()))))
.mapToInt(orderMenu -> orderMenu.getQuantity() * WEEKDAY_DESSERT_DISCOUNT_AMOUNT)
.sum();
``` |
@@ -0,0 +1,85 @@
+package christmas.domain;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+public class EventResult {
+ private static final String NON_BADGE = "์์";
+ private static final String STAR_BADGE = "๋ณ";
+ private static final String TREE_BADGE = "ํธ๋ฆฌ";
+ private static final String SANTA_BADGE = "์ฐํ";
+ private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000;
+ private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000;
+ private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000;
+ private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000;
+ private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000;
+
+ private final Map<BenefitType, Integer> allBenefit;
+
+ public EventResult(Map<BenefitType, Integer> allBenefit) {
+ this.allBenefit = allBenefit;
+ }
+
+ public int calculateTotalBenefitAmount() {
+ int totalBenefitAmount = 0;
+ List<BenefitType> benefitTypes = List.of(BenefitType.values());
+
+ for (BenefitType benefitType : benefitTypes) {
+ totalBenefitAmount += allBenefit.get(benefitType);
+ }
+
+ return totalBenefitAmount;
+ }
+
+ public int calculateTotalDiscountAmount() {
+ int totalDiscountAmount = 0;
+ List<BenefitType> benefitTypes = List.of(BenefitType.values());
+
+ for (BenefitType benefitType : benefitTypes) {
+ totalDiscountAmount += allBenefit.get(benefitType);
+ }
+ if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) {
+ totalDiscountAmount -= Menu.CHAMPAGNE.getPrice();
+ }
+
+ return totalDiscountAmount;
+ }
+
+ public boolean isReceivedGiftBenefit() {
+ return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice();
+ }
+
+ public List<BenefitType> checkWhichBenefitExist() {
+ List<BenefitType> existingBenefit = new ArrayList<>();
+ BenefitType[] benefitTypes = BenefitType.values();
+
+ for (BenefitType benefitType : benefitTypes) {
+ if (allBenefit.get(benefitType) != 0) {
+ existingBenefit.add(benefitType);
+ }
+ }
+
+ return existingBenefit;
+ }
+
+ public String decideEventBadge() {
+ int totalBenefitAmount = calculateTotalBenefitAmount();
+
+ if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) {
+ return STAR_BADGE;
+ }
+ if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) {
+ return TREE_BADGE;
+ }
+ if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) {
+ return SANTA_BADGE;
+ }
+ return NON_BADGE;
+ }
+
+ public Map<BenefitType, Integer> getAllBenefit() {
+ return Collections.unmodifiableMap(allBenefit);
+ }
+}
\ No newline at end of file | Java | (์ถ์ฒ๋๋ ค์!)
EventResult์ badge๊ด๋ จ ์ฝ๋๋ enum์ผ๋ก ๋ถ๋ฆฌํ๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,52 @@
+package christmas.ui;
+
+import static christmas.domain.Date.DATE_RE_READ_REQUEST_MESSAGE;
+import static christmas.domain.OrderedMenu.ORDER_RE_READ_REQUEST_MESSAGE;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Converter {
+ private static final String ORDER_DELIMITER = ",";
+ private static final String NAME_AND_AMOUNT_DELIMITER = "-";
+
+ public static int convertDateNumeric(String Input) {
+ try {
+ return Integer.parseInt(Input);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE);
+ }
+ }
+
+ public static List<String> separateOrder(String order) {
+ return new ArrayList<>(List.of(order.split(ORDER_DELIMITER)));
+ }
+
+ public static List<String> separateNameAndAmount(String orderedMenu) {
+ validateAppropriateDelimiter(orderedMenu);
+ List<String> separatedNameAndAmount = new ArrayList<>(List.of(orderedMenu.split(NAME_AND_AMOUNT_DELIMITER)));
+ validateAppropriateForm(separatedNameAndAmount);
+
+ return separatedNameAndAmount;
+ }
+
+ private static void validateAppropriateDelimiter(String orderedMenu) {
+ if (!orderedMenu.contains(NAME_AND_AMOUNT_DELIMITER)) {
+ throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE);
+ }
+ }
+
+ private static void validateAppropriateForm(List<String> separatedNameAndAmount) {
+ if (separatedNameAndAmount.size() != 2) {
+ throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE);
+ }
+ }
+
+ public static int convertAmountNumeric(String input) {
+ try {
+ return Integer.parseInt(input);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE);
+ }
+ }
+}
\ No newline at end of file | Java | (์ด๋ ๊ฒ๋ ๊ฐ๋ฅํด์!)
ArrayList๋ก ๋ค์ ๊ฐ์ธ์ง ์์๋ ์ถฉ๋ถํ ๊ฒ ๊ฐ์์ :)
```suggestion
return List.of(order.split(ORDER_DELIMITER));
``` |
@@ -0,0 +1,63 @@
+package christmas.ui;
+
+import camp.nextstep.edu.missionutils.Console;
+import christmas.domain.Date;
+import christmas.domain.Order;
+import christmas.domain.OrderedMenu;
+import java.util.ArrayList;
+import java.util.List;
+
+public class InputView {
+ private static final String OPENING_MESSAGE = "์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น 12์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค.";
+ private static final String DATE_REQUEST_MESSAGE = "12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)";
+ private static final String ORDER_REQUEST_MESSAGE
+ = "์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)";
+
+ public static void notifyProgramStarted() {
+ System.out.println(OPENING_MESSAGE);
+ }
+
+ public static Date inputDate() {
+ try {
+ return readDate();
+ } catch (IllegalArgumentException e) {
+ OutputView.printErrorMessage(e);
+ return inputDate();
+ }
+ }
+
+ private static Date readDate() {
+ System.out.println(DATE_REQUEST_MESSAGE);
+
+ String input = Console.readLine();
+ int dayNumber = Converter.convertDateNumeric(input);
+
+ return new Date(dayNumber);
+ }
+
+ public static Order inputOrder() {
+ try {
+ return new Order(readOrder());
+ } catch (IllegalArgumentException e) {
+ OutputView.printErrorMessage(e);
+ return inputOrder();
+ }
+ }
+
+ private static List<OrderedMenu> readOrder() {
+ System.out.println(ORDER_REQUEST_MESSAGE);
+
+ String input = Console.readLine();
+ List<String> separatedOrder = Converter.separateOrder(input);
+ List<OrderedMenu> order = new ArrayList<>();
+
+ for (String orderedMenu : separatedOrder) {
+ List<String> separatedNameAndAmount = Converter.separateNameAndAmount(orderedMenu);
+ String name = separatedNameAndAmount.get(0);
+ int amount = Converter.convertAmountNumeric(separatedNameAndAmount.get(1));
+ order.add(new OrderedMenu(name, amount));
+ }
+
+ return order;
+ }
+}
\ No newline at end of file | Java | (์ด๋ ๊ฒ ๋ฐ๊ฟ ์ ์์ด์!)
```suggestion
private static List<OrderedMenu> readOrder() {
System.out.println(ORDER_REQUEST_MESSAGE);
String input = Console.readLine();
List<String> separatedOrder = Converter.separateOrder(input);
return separatedOrder.stream()
.map(InputView::convertNameToOrderedMenu)
.toList();
}
private static OrderedMenu convertNameToOrderedMenu(String orderMenu) {
List<String> separatedNameAndAmount = Converter.separateNameAndAmount(orderMenu);
String name = separatedNameAndAmount.get(0);
int amount = Converter.convertAmountNumeric(separatedNameAndAmount.get(1));
return new OrderedMenu(name, amount);
}
``` |
@@ -0,0 +1,60 @@
+package christmas.domain;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+class EventProcessTest {
+ @DisplayName("์ด๋ฒคํธ์ ์ถฉ๋ถํ ์ ์ฒด ์ฃผ๋ฌธ ๊ธ์ก์ผ๋ก ์ ์ฒด ํํ ๋์ถ")
+ @Test
+ void takeAllBenefitWithSufficientTotalOrderPrice() {
+ Date dateInput = new Date(3);
+ Order orderInput = new Order(setUpSufficientTotalOrderPriceOrder());
+ EventProcess eventProcess = new EventProcess();
+ EventResult eventResult = eventProcess.takeAllBenefit(dateInput, orderInput);
+ Map<BenefitType, Integer> result = eventResult.getAllBenefit();
+
+ assertThat(result.get(BenefitType.GIFT)).isEqualTo(25000);
+ assertThat(result.get(BenefitType.D_DAY)).isEqualTo(1200);
+ assertThat(result.get(BenefitType.WEEKDAY)).isEqualTo(2023);
+ assertThat(result.get(BenefitType.WEEKEND)).isEqualTo(0);
+ assertThat(result.get(BenefitType.SPECIAL)).isEqualTo(1000);
+ }
+
+ private List<OrderedMenu> setUpSufficientTotalOrderPriceOrder() {
+ List<OrderedMenu> orderedMenus = new ArrayList<>();
+ orderedMenus.add(new OrderedMenu("ํด์ฐ๋ฌผํ์คํ", 2));
+ orderedMenus.add(new OrderedMenu("๋ ๋์์ธ", 1));
+ orderedMenus.add(new OrderedMenu("์ด์ฝ์ผ์ดํฌ", 1));
+
+ return orderedMenus;
+ }
+
+ @DisplayName("์ด๋ฒคํธ์ ์ถฉ๋ถํ์ง ์์ ์ ์ฒด ์ฃผ๋ฌธ ๊ธ์ก์ผ๋ก ์ ์ฒด ํํ ๋์ถ")
+ @Test
+ void takeAllBenefitWithInSufficientTotalOrderPrice() {
+ Date dateInput = new Date(26);
+ Order orderInput = new Order(setUpInSufficientTotalOrderPriceOrder());
+ EventProcess eventProcess = new EventProcess();
+ EventResult eventResult = eventProcess.takeAllBenefit(dateInput, orderInput);
+ Map<BenefitType, Integer> result = eventResult.getAllBenefit();
+
+ assertThat(result.get(BenefitType.GIFT)).isEqualTo(0);
+ assertThat(result.get(BenefitType.D_DAY)).isEqualTo(0);
+ assertThat(result.get(BenefitType.WEEKDAY)).isEqualTo(0);
+ assertThat(result.get(BenefitType.WEEKEND)).isEqualTo(0);
+ assertThat(result.get(BenefitType.SPECIAL)).isEqualTo(0);
+ }
+
+ private List<OrderedMenu> setUpInSufficientTotalOrderPriceOrder() {
+ List<OrderedMenu> orderedMenus = new ArrayList<>();
+ orderedMenus.add(new OrderedMenu("ํํ์ค", 1));
+ orderedMenus.add(new OrderedMenu("์ ๋ก์ฝ๋ผ", 1));
+
+ return orderedMenus;
+ }
+}
\ No newline at end of file | Java | (์ถ์ฒ๋๋ ค์)
assertThat๋ง ์ฌ์ฉํ๋ฉด 21๋ฒ์งธ ์ค์์ ์คํจํ์ ๋ ๋ฐ์ 22~25๋ฒ ๋ผ์ธ๊น์ง ๊ฒ์ฆํ์ง ์์ต๋๋ค!
๋ฐ๋ฉด assertAll์ ์ฌ์ฉํ๋ฉด ๋ชจ๋ assertThat์ ์คํ์ํค๊ธฐ ๋๋ฌธ์ ๋ฌด์์ด ์คํจํ๋์ง ํ์คํ๊ฒ ์ฐพ์ ์ ์๋ ์ฅ์ ์ด ์์ต๋๋ค!
```suggestion
assertAll(
() -> assertThat(result.get(BenefitType.GIFT)).isEqualTo(25000),
() -> assertThat(result.get(BenefitType.D_DAY)).isEqualTo(1200),
() -> assertThat(result.get(BenefitType.WEEKDAY)).isEqualTo(2023),
() -> assertThat(result.get(BenefitType.WEEKEND)).isEqualTo(0),
() -> assertThat(result.get(BenefitType.SPECIAL)).isEqualTo(1000)
);
``` |
@@ -0,0 +1,23 @@
+package christmas.domain;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+class MenuTest {
+ @DisplayName("์ฃผ์ด์ง ์ด๋ฆ์ ํ ๋๋ก ๋ฉ๋ด๋ฅผ ํ๋จํ ์ ์๋ค.")
+ @Test
+ void decideMenu() {
+ String inputTapas = "ํํ์ค";
+ String inputSeafoodPasta = "ํด์ฐ๋ฌผํ์คํ";
+ String inputRedWine = "๋ ๋์์ธ";
+ Menu inputTapasResult = Menu.decideMenu(inputTapas);
+ Menu inputSeafoodPastaResult = Menu.decideMenu(inputSeafoodPasta);
+ Menu inputRedWineResult = Menu.decideMenu(inputRedWine);
+
+ assertThat(inputTapasResult).isEqualTo(Menu.TAPAS);
+ assertThat(inputSeafoodPastaResult).isEqualTo(Menu.SEAFOOD_PASTA);
+ assertThat(inputRedWineResult).isEqualTo(Menu.RED_WINE);
+ }
+}
\ No newline at end of file | Java | (๊ฐ์ธ์ ์ธ ์๊ฐ์
๋๋ค!)
ํ
์คํธ์ฝ๋๋ฅผ ๋๋ฌ๋ณด๋ ๋๋ถ๋ถ ํดํผ์ผ์ด์ค๋ง ์๋๋ผ๊ตฌ์!
์คํจ์ ๋ํ ๊ฒ์ฆ ํ
์คํธ ์ฝ๋๋ ์ฐ์ตํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,58 @@
+package christmas.domain;
+
+import java.time.DayOfWeek;
+import java.time.LocalDate;
+
+public class Date {
+ public static final String DATE_RE_READ_REQUEST_MESSAGE = "์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final LocalDate CHRISTMAS_DAY = LocalDate.of(2023, 12, 25);
+ private static final int YEAR = 2023;
+ private static final int MONTH = 12;
+ private static final int DATE_MIN_NUMBER = 1;
+ private static final int DATE_MAX_NUMBER = 31;
+
+ private final int dayNumber;
+
+ public Date(int dayNumber) {
+ validateRange(dayNumber);
+ this.dayNumber = dayNumber;
+ }
+
+ private void validateRange(int dayNumber) {
+ if ((dayNumber < DATE_MIN_NUMBER) || (dayNumber > DATE_MAX_NUMBER)) {
+ throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE);
+ }
+ }
+
+ public boolean isDDayDiscountActive() {
+ LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber);
+
+ return localDate.isBefore(CHRISTMAS_DAY.plusDays(1));
+ }
+
+ public boolean isWeekdayDiscountActive() {
+ LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber);
+ DayOfWeek day = localDate.getDayOfWeek();
+
+ return (day.equals(DayOfWeek.MONDAY) || day.equals(DayOfWeek.TUESDAY) || day.equals(DayOfWeek.WEDNESDAY)
+ || day.equals(DayOfWeek.THURSDAY) || day.equals(DayOfWeek.SUNDAY));
+ }
+
+ public boolean isWeekendDiscountActive() {
+ LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber);
+ DayOfWeek day = localDate.getDayOfWeek();
+
+ return (day.equals(DayOfWeek.FRIDAY) || day.equals(DayOfWeek.SATURDAY));
+ }
+
+ public boolean isSpecialDiscountActive() {
+ LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber);
+ DayOfWeek day = localDate.getDayOfWeek();
+
+ return (day.equals(DayOfWeek.SUNDAY) || localDate.equals(CHRISTMAS_DAY));
+ }
+
+ public int getDayNumber() {
+ return dayNumber;
+ }
+}
\ No newline at end of file | Java | ์ค,, ์ด๋ฒ์ LocalDate๋ฅผ ์ฒ์ ์ฌ์ฉํด๋ด์ ์ต์ํ์ง ์์๋๋ฐ์ ์ด๋ฐ ๋ฐฉ๋ฒ๋ ์์๊ตฐ์,, ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,100 @@
+package christmas.domain;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class EventProcess {
+ private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT = 120000;
+ private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT = 10000;
+ private static final int D_DAY_DISCOUNT_AMOUNT_PER_DAY = 100;
+ private static final int D_DAY_DISCOUNT_DEFAULT_AMOUNT = 1000;
+ private static final int FIRST_DAY_NUMBER = 1;
+ private static final int WEEKDAY_DESSERT_DISCOUNT_AMOUNT = 2023;
+ private static final int WEEKEND_MAIN_DISCOUNT_AMOUNT = 2023;
+ private static final int SPECIAL_DISCOUNT_AMOUNT = 1000;
+
+ public EventResult takeAllBenefit(Date date, Order order) {
+ Map<BenefitType, Integer> allBenefit = new HashMap<>();
+ allBenefit.put(BenefitType.GIFT, takeGift(order));
+ allBenefit.put(BenefitType.D_DAY, takeDDayDiscount(date));
+ allBenefit.put(BenefitType.WEEKDAY, takeWeekdayDiscount(date, order));
+ allBenefit.put(BenefitType.WEEKEND, takeWeekendDiscount(date, order));
+ allBenefit.put(BenefitType.SPECIAL, takeSpecialDiscount(date));
+
+ cancelAllBenefitIfMeetCondition(order, allBenefit);
+
+ return new EventResult(allBenefit);
+ }
+
+ private int takeGift(Order order) {
+ if (order.calculateTotalOrderPrice() >= MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT) {
+ return Menu.CHAMPAGNE.getPrice();
+ }
+
+ return 0;
+ }
+
+ private int takeDDayDiscount(Date date) {
+ if (date.isDDayDiscountActive()) {
+ return D_DAY_DISCOUNT_DEFAULT_AMOUNT
+ + (calculateDifferenceBetweenFirstDay(date) * D_DAY_DISCOUNT_AMOUNT_PER_DAY);
+ }
+
+ return 0;
+ }
+
+ private int calculateDifferenceBetweenFirstDay(Date date) {
+ return date.getDayNumber() - FIRST_DAY_NUMBER;
+ }
+
+ private int takeWeekdayDiscount(Date date, Order order) {
+ int discount = 0;
+
+ for (OrderedMenu orderedMenu : order.getOrder()) {
+ Menu menu = Menu.decideMenu(orderedMenu.getMenuName());
+ if (date.isWeekdayDiscountActive() && MenuType.decideMenuType(menu) == MenuType.DESSERT) {
+ discount += orderedMenu.getQuantity() * WEEKDAY_DESSERT_DISCOUNT_AMOUNT;
+ }
+ }
+
+ return discount;
+ }
+
+ private int takeWeekendDiscount(Date date, Order order) {
+ int discount = 0;
+
+ for (OrderedMenu orderedMenu : order.getOrder()) {
+ Menu menu = Menu.decideMenu(orderedMenu.getMenuName());
+ if (date.isWeekendDiscountActive() && MenuType.decideMenuType(menu) == MenuType.MAIN) {
+ discount += orderedMenu.getQuantity() * WEEKEND_MAIN_DISCOUNT_AMOUNT;
+ }
+ }
+
+ return discount;
+ }
+
+ private int takeSpecialDiscount(Date date) {
+ if (date.isSpecialDiscountActive()) {
+ return SPECIAL_DISCOUNT_AMOUNT;
+ }
+
+ return 0;
+ }
+
+ private Map<BenefitType, Integer> cancelAllBenefitIfMeetCondition(Order order,
+ Map<BenefitType, Integer> allBenefit) {
+ if (isInsufficientTotalOrderPriceForEvent(order)) {
+ allBenefit.replace(BenefitType.GIFT, 0);
+ allBenefit.replace(BenefitType.D_DAY, 0);
+ allBenefit.replace(BenefitType.WEEKDAY, 0);
+ allBenefit.replace(BenefitType.WEEKEND, 0);
+ allBenefit.replace(BenefitType.SPECIAL, 0);
+ }
+
+ return allBenefit;
+ }
+
+ private boolean isInsufficientTotalOrderPriceForEvent(Order order) {
+ return order.calculateTotalOrderPrice() < MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT;
+ }
+}
\ No newline at end of file | Java | ๋ฏธ์
๋์์ ์๊ฐ์ด ์ด๋ฐํ์ด์ stream๊ณต๋ถ๋ฅผ ํ์ง ๋ชปํ๋๋ฐ์, ์์ผ๋ก ๊ณต๋ถํด์ ์ถ์ฒํด์ฃผ์ ๋ฐฉ๋ฒ ์ดํดํด๋ณด๋๋ก ํ ๊ฒ์ ๊ฐ์ฌํฉ๋๋ค!! |
@@ -0,0 +1,100 @@
+package christmas.domain;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class EventProcess {
+ private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT = 120000;
+ private static final int MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT = 10000;
+ private static final int D_DAY_DISCOUNT_AMOUNT_PER_DAY = 100;
+ private static final int D_DAY_DISCOUNT_DEFAULT_AMOUNT = 1000;
+ private static final int FIRST_DAY_NUMBER = 1;
+ private static final int WEEKDAY_DESSERT_DISCOUNT_AMOUNT = 2023;
+ private static final int WEEKEND_MAIN_DISCOUNT_AMOUNT = 2023;
+ private static final int SPECIAL_DISCOUNT_AMOUNT = 1000;
+
+ public EventResult takeAllBenefit(Date date, Order order) {
+ Map<BenefitType, Integer> allBenefit = new HashMap<>();
+ allBenefit.put(BenefitType.GIFT, takeGift(order));
+ allBenefit.put(BenefitType.D_DAY, takeDDayDiscount(date));
+ allBenefit.put(BenefitType.WEEKDAY, takeWeekdayDiscount(date, order));
+ allBenefit.put(BenefitType.WEEKEND, takeWeekendDiscount(date, order));
+ allBenefit.put(BenefitType.SPECIAL, takeSpecialDiscount(date));
+
+ cancelAllBenefitIfMeetCondition(order, allBenefit);
+
+ return new EventResult(allBenefit);
+ }
+
+ private int takeGift(Order order) {
+ if (order.calculateTotalOrderPrice() >= MINIMUM_TOTAL_ORDER_PRICE_FOR_GIFT) {
+ return Menu.CHAMPAGNE.getPrice();
+ }
+
+ return 0;
+ }
+
+ private int takeDDayDiscount(Date date) {
+ if (date.isDDayDiscountActive()) {
+ return D_DAY_DISCOUNT_DEFAULT_AMOUNT
+ + (calculateDifferenceBetweenFirstDay(date) * D_DAY_DISCOUNT_AMOUNT_PER_DAY);
+ }
+
+ return 0;
+ }
+
+ private int calculateDifferenceBetweenFirstDay(Date date) {
+ return date.getDayNumber() - FIRST_DAY_NUMBER;
+ }
+
+ private int takeWeekdayDiscount(Date date, Order order) {
+ int discount = 0;
+
+ for (OrderedMenu orderedMenu : order.getOrder()) {
+ Menu menu = Menu.decideMenu(orderedMenu.getMenuName());
+ if (date.isWeekdayDiscountActive() && MenuType.decideMenuType(menu) == MenuType.DESSERT) {
+ discount += orderedMenu.getQuantity() * WEEKDAY_DESSERT_DISCOUNT_AMOUNT;
+ }
+ }
+
+ return discount;
+ }
+
+ private int takeWeekendDiscount(Date date, Order order) {
+ int discount = 0;
+
+ for (OrderedMenu orderedMenu : order.getOrder()) {
+ Menu menu = Menu.decideMenu(orderedMenu.getMenuName());
+ if (date.isWeekendDiscountActive() && MenuType.decideMenuType(menu) == MenuType.MAIN) {
+ discount += orderedMenu.getQuantity() * WEEKEND_MAIN_DISCOUNT_AMOUNT;
+ }
+ }
+
+ return discount;
+ }
+
+ private int takeSpecialDiscount(Date date) {
+ if (date.isSpecialDiscountActive()) {
+ return SPECIAL_DISCOUNT_AMOUNT;
+ }
+
+ return 0;
+ }
+
+ private Map<BenefitType, Integer> cancelAllBenefitIfMeetCondition(Order order,
+ Map<BenefitType, Integer> allBenefit) {
+ if (isInsufficientTotalOrderPriceForEvent(order)) {
+ allBenefit.replace(BenefitType.GIFT, 0);
+ allBenefit.replace(BenefitType.D_DAY, 0);
+ allBenefit.replace(BenefitType.WEEKDAY, 0);
+ allBenefit.replace(BenefitType.WEEKEND, 0);
+ allBenefit.replace(BenefitType.SPECIAL, 0);
+ }
+
+ return allBenefit;
+ }
+
+ private boolean isInsufficientTotalOrderPriceForEvent(Order order) {
+ return order.calculateTotalOrderPrice() < MINIMUM_TOTAL_ORDER_PRICE_FOR_ALL_EVENT;
+ }
+}
\ No newline at end of file | Java | ํจ์ฌ ๊น๋ํ๋ค์,, ๊ฐ์ฌํฉ๋๋ค!! |
@@ -0,0 +1,85 @@
+package christmas.domain;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+public class EventResult {
+ private static final String NON_BADGE = "์์";
+ private static final String STAR_BADGE = "๋ณ";
+ private static final String TREE_BADGE = "ํธ๋ฆฌ";
+ private static final String SANTA_BADGE = "์ฐํ";
+ private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000;
+ private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000;
+ private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000;
+ private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000;
+ private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000;
+
+ private final Map<BenefitType, Integer> allBenefit;
+
+ public EventResult(Map<BenefitType, Integer> allBenefit) {
+ this.allBenefit = allBenefit;
+ }
+
+ public int calculateTotalBenefitAmount() {
+ int totalBenefitAmount = 0;
+ List<BenefitType> benefitTypes = List.of(BenefitType.values());
+
+ for (BenefitType benefitType : benefitTypes) {
+ totalBenefitAmount += allBenefit.get(benefitType);
+ }
+
+ return totalBenefitAmount;
+ }
+
+ public int calculateTotalDiscountAmount() {
+ int totalDiscountAmount = 0;
+ List<BenefitType> benefitTypes = List.of(BenefitType.values());
+
+ for (BenefitType benefitType : benefitTypes) {
+ totalDiscountAmount += allBenefit.get(benefitType);
+ }
+ if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) {
+ totalDiscountAmount -= Menu.CHAMPAGNE.getPrice();
+ }
+
+ return totalDiscountAmount;
+ }
+
+ public boolean isReceivedGiftBenefit() {
+ return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice();
+ }
+
+ public List<BenefitType> checkWhichBenefitExist() {
+ List<BenefitType> existingBenefit = new ArrayList<>();
+ BenefitType[] benefitTypes = BenefitType.values();
+
+ for (BenefitType benefitType : benefitTypes) {
+ if (allBenefit.get(benefitType) != 0) {
+ existingBenefit.add(benefitType);
+ }
+ }
+
+ return existingBenefit;
+ }
+
+ public String decideEventBadge() {
+ int totalBenefitAmount = calculateTotalBenefitAmount();
+
+ if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) {
+ return STAR_BADGE;
+ }
+ if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) {
+ return TREE_BADGE;
+ }
+ if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) {
+ return SANTA_BADGE;
+ }
+ return NON_BADGE;
+ }
+
+ public Map<BenefitType, Integer> getAllBenefit() {
+ return Collections.unmodifiableMap(allBenefit);
+ }
+}
\ No newline at end of file | Java | ์ถ๋ ฅ ํค๋๋ค์ enum์ผ๋ก ๊ด๋ฆฌํ ์ง badge๋ฅผ enum์ผ๋ก ๊ด๋ฆฌํ ์ง ๊ณ ๋ฏผํ๋ค๊ฐ ์ถ๋ ฅ ํค๋๋ค๋ง enum์ผ๋ก ๊ด๋ฆฌํ๋๋ฐ์, ๋ฆฌ๋ทฐ ๋จ๊ฒจ๋๋ฆฐ ๊ฒ์ ๋ต๋ณ ์ฃผ์ ํ๋ฒ๋ง ์ฌ์ฉํ๋ ๋ฌธ์์ด์ ๊ตณ์ด enum์ผ๋ก ๊ด๋ฆฌ ํ์ง ์์๋ ๋ ๊ฒ ๊ฐ๋ค๋ ๋ง์์ ๊ณต๊ณต๊ฐํด์ ๋ฐ๋๋ก badge๋ ๊ผญ enum์ผ๋ก ํ๋ ๊ฒ์ด ์ข์์ ๊ฒ์ด๋ผ๋ ์๊ฐ๋ ๋๋ค์. ๊ฐ์ฌํด์ |
@@ -0,0 +1,60 @@
+package christmas.domain;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+class EventProcessTest {
+ @DisplayName("์ด๋ฒคํธ์ ์ถฉ๋ถํ ์ ์ฒด ์ฃผ๋ฌธ ๊ธ์ก์ผ๋ก ์ ์ฒด ํํ ๋์ถ")
+ @Test
+ void takeAllBenefitWithSufficientTotalOrderPrice() {
+ Date dateInput = new Date(3);
+ Order orderInput = new Order(setUpSufficientTotalOrderPriceOrder());
+ EventProcess eventProcess = new EventProcess();
+ EventResult eventResult = eventProcess.takeAllBenefit(dateInput, orderInput);
+ Map<BenefitType, Integer> result = eventResult.getAllBenefit();
+
+ assertThat(result.get(BenefitType.GIFT)).isEqualTo(25000);
+ assertThat(result.get(BenefitType.D_DAY)).isEqualTo(1200);
+ assertThat(result.get(BenefitType.WEEKDAY)).isEqualTo(2023);
+ assertThat(result.get(BenefitType.WEEKEND)).isEqualTo(0);
+ assertThat(result.get(BenefitType.SPECIAL)).isEqualTo(1000);
+ }
+
+ private List<OrderedMenu> setUpSufficientTotalOrderPriceOrder() {
+ List<OrderedMenu> orderedMenus = new ArrayList<>();
+ orderedMenus.add(new OrderedMenu("ํด์ฐ๋ฌผํ์คํ", 2));
+ orderedMenus.add(new OrderedMenu("๋ ๋์์ธ", 1));
+ orderedMenus.add(new OrderedMenu("์ด์ฝ์ผ์ดํฌ", 1));
+
+ return orderedMenus;
+ }
+
+ @DisplayName("์ด๋ฒคํธ์ ์ถฉ๋ถํ์ง ์์ ์ ์ฒด ์ฃผ๋ฌธ ๊ธ์ก์ผ๋ก ์ ์ฒด ํํ ๋์ถ")
+ @Test
+ void takeAllBenefitWithInSufficientTotalOrderPrice() {
+ Date dateInput = new Date(26);
+ Order orderInput = new Order(setUpInSufficientTotalOrderPriceOrder());
+ EventProcess eventProcess = new EventProcess();
+ EventResult eventResult = eventProcess.takeAllBenefit(dateInput, orderInput);
+ Map<BenefitType, Integer> result = eventResult.getAllBenefit();
+
+ assertThat(result.get(BenefitType.GIFT)).isEqualTo(0);
+ assertThat(result.get(BenefitType.D_DAY)).isEqualTo(0);
+ assertThat(result.get(BenefitType.WEEKDAY)).isEqualTo(0);
+ assertThat(result.get(BenefitType.WEEKEND)).isEqualTo(0);
+ assertThat(result.get(BenefitType.SPECIAL)).isEqualTo(0);
+ }
+
+ private List<OrderedMenu> setUpInSufficientTotalOrderPriceOrder() {
+ List<OrderedMenu> orderedMenus = new ArrayList<>();
+ orderedMenus.add(new OrderedMenu("ํํ์ค", 1));
+ orderedMenus.add(new OrderedMenu("์ ๋ก์ฝ๋ผ", 1));
+
+ return orderedMenus;
+ }
+}
\ No newline at end of file | Java | ์,, ํ์คํ๊ฒ ํ
์คํธ ํ ์ ์๊ฒ ๋ค์ ๊ฐ์ฌํฉ๋๋ค!! |
@@ -0,0 +1,23 @@
+package christmas.domain;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+class MenuTest {
+ @DisplayName("์ฃผ์ด์ง ์ด๋ฆ์ ํ ๋๋ก ๋ฉ๋ด๋ฅผ ํ๋จํ ์ ์๋ค.")
+ @Test
+ void decideMenu() {
+ String inputTapas = "ํํ์ค";
+ String inputSeafoodPasta = "ํด์ฐ๋ฌผํ์คํ";
+ String inputRedWine = "๋ ๋์์ธ";
+ Menu inputTapasResult = Menu.decideMenu(inputTapas);
+ Menu inputSeafoodPastaResult = Menu.decideMenu(inputSeafoodPasta);
+ Menu inputRedWineResult = Menu.decideMenu(inputRedWine);
+
+ assertThat(inputTapasResult).isEqualTo(Menu.TAPAS);
+ assertThat(inputSeafoodPastaResult).isEqualTo(Menu.SEAFOOD_PASTA);
+ assertThat(inputRedWineResult).isEqualTo(Menu.RED_WINE);
+ }
+}
\ No newline at end of file | Java | ๋ง์์ ๊ฐ์ธ์ ์ผ๋ก ๊ฐ์ฅ ์์ฌ์ด ๋ถ๋ถ์ธ๋ฐ์,, ๊ตฌํํ๊ณ ๋ฆฌํฉํฐ๋ง์ ํ๋ ๋ง์ด ํด์ ์๊ฐ์ด ๋งค์ฐ ์ด๋ฐํ๋ค๋,, ใ
ใ
|
@@ -0,0 +1,39 @@
+package christmas.domain;
+
+import java.util.Arrays;
+import java.util.List;
+
+public enum MenuType {
+ APPETIZER("์ ํผํ์ด์ ", Arrays.asList(Menu.MUSHROOM_SOUP, Menu.TAPAS, Menu.CAESAR_SALAD)),
+ MAIN("๋ฉ์ธ", Arrays.asList(Menu.T_BONE_STAKE, Menu.BARBECUE_LIP, Menu.SEAFOOD_PASTA, Menu.CHRISTMAS_PASTA)),
+ DESSERT("๋์ ํธ", Arrays.asList(Menu.CHOCOLATE_CAKE, Menu.ICE_CREAM)),
+ BEVERAGE("์๋ฃ", Arrays.asList(Menu.ZERO_COKE, Menu.RED_WINE, Menu.CHAMPAGNE));
+
+ private final String name;
+ private final List<Menu> menus;
+
+ MenuType(String name, List<Menu> menus) {
+ this.name = name;
+ this.menus = menus;
+ }
+
+ public static MenuType decideMenuType(Menu menu) {
+ MenuType[] menuTypes = values();
+
+ for (MenuType menuType : menuTypes) {
+ if (menuType.getMenus().contains(menu)) {
+ return menuType;
+ }
+ }
+
+ return null;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public List<Menu> getMenus() {
+ return menus;
+ }
+}
\ No newline at end of file | Java | ์ ํํ
๋จ๊ฒจ์ฃผ์
จ๋ ์ฝ๋ ๋ฆฌ๋ทฐ์์ ์ธ๊ธํ์ ํจํด์ด๋ค์! ์ข์ ๊ณต๋ถ๊ฐ ๋ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,21 @@
+package christmas.domain;
+
+public enum OrderResultType {
+ ORDER_MENU("<์ฃผ๋ฌธ ๋ฉ๋ด>"),
+ TOTAL_ORDER_PRICE("<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>"),
+ GIFT_MENU("<์ฆ์ ๋ฉ๋ด>"),
+ BENEFIT_STATISTICS("<ํํ ๋ด์ญ>"),
+ TOTAL_BENEFIT_AMOUNT("<์ดํํ ๊ธ์ก>"),
+ ESTIMATED_PAYMENT("<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>"),
+ EVENT_BADGE("<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>");
+
+ private final String name;
+
+ OrderResultType(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+} | Java | ๐ค `OrderResultType`์ ์ ๋ชฉ์ ๊ทธ๋ ๋ค ์ณ๋, < > ๊ฐ์ character ๋ค์ View์ ์ฑ
์์ ๊ฐ๊น์ ๋ณด์ฌ์.
View์์`"<%s>"` ์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ์ฑ
์์ ๋ถ๋ฆฌํ๊ณ formatting์ ์ด์ฉํด ์ถ๋ ฅํ๋ ๋ฐฉ๋ฒ์ด ๋ ๋ณ๊ฒฝ์ ๊ฐํ ์ค๊ณ ์๋๊น์? |
@@ -0,0 +1,45 @@
+package christmas.domain;
+
+public class OrderedMenu {
+ public static final String ORDER_RE_READ_REQUEST_MESSAGE = "์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final int AMOUNT_MINIMUM_QUANTITY = 1;
+
+ private final String menuName;
+ private final int quantity;
+
+ public OrderedMenu(String menuName, int quantity) {
+ validateOrderNameExistInMenu(menuName);
+ validateOrderAmountQuantity(quantity);
+
+ this.menuName = menuName;
+ this.quantity = quantity;
+ }
+
+ private void validateOrderNameExistInMenu(String name) {
+ if (Menu.decideMenu(name) == Menu.NONE) {
+ throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE);
+ }
+ }
+
+ private void validateOrderAmountQuantity(int amount) {
+ if (amount < AMOUNT_MINIMUM_QUANTITY) {
+ throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE);
+ }
+ }
+
+ public boolean isBeverage() {
+ return MenuType.decideMenuType(Menu.decideMenu(menuName)) == MenuType.BEVERAGE;
+ }
+
+ public int calculatePrice() {
+ return Menu.decideMenu(menuName).getPrice() * quantity;
+ }
+
+ public String getMenuName() {
+ return menuName;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+}
\ No newline at end of file | Java | `Menu` ์ ์ํ๋ ๊ฒ์ผ๋ก ๊ฒ์ฆ์ด ๋์๋ค๋ฉด, field๋ฅผ `String`์ด ์๋ `Menu`๋ก ์ ์ฅํ๋๊ฒ ๋ ์ข์์ ๊ฒ ๊ฐ์์.
enum ์ฌ์ฉ์ ์๋ฏธ๊ฐ ์ฝ๊ฐ ํด์๋์ง ์์๋ ์ถ๋ค์! |
@@ -0,0 +1,52 @@
+package christmas.ui;
+
+import static christmas.domain.Date.DATE_RE_READ_REQUEST_MESSAGE;
+import static christmas.domain.OrderedMenu.ORDER_RE_READ_REQUEST_MESSAGE;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Converter {
+ private static final String ORDER_DELIMITER = ",";
+ private static final String NAME_AND_AMOUNT_DELIMITER = "-";
+
+ public static int convertDateNumeric(String Input) {
+ try {
+ return Integer.parseInt(Input);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE);
+ }
+ }
+
+ public static List<String> separateOrder(String order) {
+ return new ArrayList<>(List.of(order.split(ORDER_DELIMITER)));
+ }
+
+ public static List<String> separateNameAndAmount(String orderedMenu) {
+ validateAppropriateDelimiter(orderedMenu);
+ List<String> separatedNameAndAmount = new ArrayList<>(List.of(orderedMenu.split(NAME_AND_AMOUNT_DELIMITER)));
+ validateAppropriateForm(separatedNameAndAmount);
+
+ return separatedNameAndAmount;
+ }
+
+ private static void validateAppropriateDelimiter(String orderedMenu) {
+ if (!orderedMenu.contains(NAME_AND_AMOUNT_DELIMITER)) {
+ throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE);
+ }
+ }
+
+ private static void validateAppropriateForm(List<String> separatedNameAndAmount) {
+ if (separatedNameAndAmount.size() != 2) {
+ throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE);
+ }
+ }
+
+ public static int convertAmountNumeric(String input) {
+ try {
+ return Integer.parseInt(input);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE);
+ }
+ }
+}
\ No newline at end of file | Java | String์ ์ค๋ธ์ ํธ๋ก ๋ฐ๊พธ๋ ํด๋์ค๋ `Parser`๋ผ๊ณ ์ด๋ฆ ์ง๋ ๊ฒ์ด ๋ convention์ ๊ฐ๊น์ธ ๊ฒ ๊ฐ์๋ฐ, ์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,63 @@
+package christmas.ui;
+
+import camp.nextstep.edu.missionutils.Console;
+import christmas.domain.Date;
+import christmas.domain.Order;
+import christmas.domain.OrderedMenu;
+import java.util.ArrayList;
+import java.util.List;
+
+public class InputView {
+ private static final String OPENING_MESSAGE = "์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น 12์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค.";
+ private static final String DATE_REQUEST_MESSAGE = "12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)";
+ private static final String ORDER_REQUEST_MESSAGE
+ = "์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)";
+
+ public static void notifyProgramStarted() {
+ System.out.println(OPENING_MESSAGE);
+ }
+
+ public static Date inputDate() {
+ try {
+ return readDate();
+ } catch (IllegalArgumentException e) {
+ OutputView.printErrorMessage(e);
+ return inputDate();
+ }
+ }
+
+ private static Date readDate() {
+ System.out.println(DATE_REQUEST_MESSAGE);
+
+ String input = Console.readLine();
+ int dayNumber = Converter.convertDateNumeric(input);
+
+ return new Date(dayNumber);
+ }
+
+ public static Order inputOrder() {
+ try {
+ return new Order(readOrder());
+ } catch (IllegalArgumentException e) {
+ OutputView.printErrorMessage(e);
+ return inputOrder();
+ }
+ }
+
+ private static List<OrderedMenu> readOrder() {
+ System.out.println(ORDER_REQUEST_MESSAGE);
+
+ String input = Console.readLine();
+ List<String> separatedOrder = Converter.separateOrder(input);
+ List<OrderedMenu> order = new ArrayList<>();
+
+ for (String orderedMenu : separatedOrder) {
+ List<String> separatedNameAndAmount = Converter.separateNameAndAmount(orderedMenu);
+ String name = separatedNameAndAmount.get(0);
+ int amount = Converter.convertAmountNumeric(separatedNameAndAmount.get(1));
+ order.add(new OrderedMenu(name, amount));
+ }
+
+ return order;
+ }
+}
\ No newline at end of file | Java | Supplier์ ํํ๋ก handling ํ๋ ๋ฐฉ๋ฒ๋ ์์ด์. inputDate์ inputOrder์ ํํ๊ฐ ๊ต์ฅํ ๋ฎ์์์ด์, ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๋ ๋ฐฉ๋ฒ์ผ๋ก ์ ์ฉ ๊ฐ๋ฅํ ๊ฒ ๊ฐ์ผ๋ ์ด ์๋ฃ ํ๋ฒ ์ฝ์ด๋ณด์๊ธธ ์ถ์ฒํฉ๋๋ค.
https://hwan33.tistory.com/17 |
@@ -0,0 +1,85 @@
+package christmas.domain;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+public class EventResult {
+ private static final String NON_BADGE = "์์";
+ private static final String STAR_BADGE = "๋ณ";
+ private static final String TREE_BADGE = "ํธ๋ฆฌ";
+ private static final String SANTA_BADGE = "์ฐํ";
+ private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000;
+ private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000;
+ private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000;
+ private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000;
+ private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000;
+
+ private final Map<BenefitType, Integer> allBenefit;
+
+ public EventResult(Map<BenefitType, Integer> allBenefit) {
+ this.allBenefit = allBenefit;
+ }
+
+ public int calculateTotalBenefitAmount() {
+ int totalBenefitAmount = 0;
+ List<BenefitType> benefitTypes = List.of(BenefitType.values());
+
+ for (BenefitType benefitType : benefitTypes) {
+ totalBenefitAmount += allBenefit.get(benefitType);
+ }
+
+ return totalBenefitAmount;
+ }
+
+ public int calculateTotalDiscountAmount() {
+ int totalDiscountAmount = 0;
+ List<BenefitType> benefitTypes = List.of(BenefitType.values());
+
+ for (BenefitType benefitType : benefitTypes) {
+ totalDiscountAmount += allBenefit.get(benefitType);
+ }
+ if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) {
+ totalDiscountAmount -= Menu.CHAMPAGNE.getPrice();
+ }
+
+ return totalDiscountAmount;
+ }
+
+ public boolean isReceivedGiftBenefit() {
+ return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice();
+ }
+
+ public List<BenefitType> checkWhichBenefitExist() {
+ List<BenefitType> existingBenefit = new ArrayList<>();
+ BenefitType[] benefitTypes = BenefitType.values();
+
+ for (BenefitType benefitType : benefitTypes) {
+ if (allBenefit.get(benefitType) != 0) {
+ existingBenefit.add(benefitType);
+ }
+ }
+
+ return existingBenefit;
+ }
+
+ public String decideEventBadge() {
+ int totalBenefitAmount = calculateTotalBenefitAmount();
+
+ if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) {
+ return STAR_BADGE;
+ }
+ if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) {
+ return TREE_BADGE;
+ }
+ if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) {
+ return SANTA_BADGE;
+ }
+ return NON_BADGE;
+ }
+
+ public Map<BenefitType, Integer> getAllBenefit() {
+ return Collections.unmodifiableMap(allBenefit);
+ }
+}
\ No newline at end of file | Java | Early return์ ์ฌ์ฉํ์
จ๋๋ฐ, ๊ตณ์ด ๋ฒ์๋ฅผ max, min์ ์ด์ฉํด ์ผ์ผ์ด ์ค๋ช
ํ์ง ์์๋ ๋ ๊ฒ ๊ฐ์์.
๊ผญ maximum_amount๊ฐ ํ์ํ์๊น์?
(๊ทธ๋ฆฌ๊ณ ๋ณ์๋ช
์ maximum์ด๋ผ๊ณ ๋ณด๊ธฐ์ ํด๋น ๋ถ๋ถ์ ํฌํจํ์ง ์์์ ํผ๋์ด ์์ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
์ฐจ๋ผ๋ฆฌ `STAR_BADGE_MAXIMUM_AMOUNT = 9999`์ ๊ฐ์ ๋ฐฉ์์ด์๋ค๋ฉด...)
```suggestion
if (totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) {
return STAR_BADGE;
}
if (totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) {
return TREE_BADGE;
}
if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) {
return SANTA_BADGE;
}
return NON_BADGE;
``` |
@@ -0,0 +1,85 @@
+package christmas.domain;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+public class EventResult {
+ private static final String NON_BADGE = "์์";
+ private static final String STAR_BADGE = "๋ณ";
+ private static final String TREE_BADGE = "ํธ๋ฆฌ";
+ private static final String SANTA_BADGE = "์ฐํ";
+ private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000;
+ private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000;
+ private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000;
+ private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000;
+ private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000;
+
+ private final Map<BenefitType, Integer> allBenefit;
+
+ public EventResult(Map<BenefitType, Integer> allBenefit) {
+ this.allBenefit = allBenefit;
+ }
+
+ public int calculateTotalBenefitAmount() {
+ int totalBenefitAmount = 0;
+ List<BenefitType> benefitTypes = List.of(BenefitType.values());
+
+ for (BenefitType benefitType : benefitTypes) {
+ totalBenefitAmount += allBenefit.get(benefitType);
+ }
+
+ return totalBenefitAmount;
+ }
+
+ public int calculateTotalDiscountAmount() {
+ int totalDiscountAmount = 0;
+ List<BenefitType> benefitTypes = List.of(BenefitType.values());
+
+ for (BenefitType benefitType : benefitTypes) {
+ totalDiscountAmount += allBenefit.get(benefitType);
+ }
+ if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) {
+ totalDiscountAmount -= Menu.CHAMPAGNE.getPrice();
+ }
+
+ return totalDiscountAmount;
+ }
+
+ public boolean isReceivedGiftBenefit() {
+ return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice();
+ }
+
+ public List<BenefitType> checkWhichBenefitExist() {
+ List<BenefitType> existingBenefit = new ArrayList<>();
+ BenefitType[] benefitTypes = BenefitType.values();
+
+ for (BenefitType benefitType : benefitTypes) {
+ if (allBenefit.get(benefitType) != 0) {
+ existingBenefit.add(benefitType);
+ }
+ }
+
+ return existingBenefit;
+ }
+
+ public String decideEventBadge() {
+ int totalBenefitAmount = calculateTotalBenefitAmount();
+
+ if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) {
+ return STAR_BADGE;
+ }
+ if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) {
+ return TREE_BADGE;
+ }
+ if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) {
+ return SANTA_BADGE;
+ }
+ return NON_BADGE;
+ }
+
+ public Map<BenefitType, Integer> getAllBenefit() {
+ return Collections.unmodifiableMap(allBenefit);
+ }
+}
\ No newline at end of file | Java | ์ถ๊ฐ) NON_BADGE๋ ๋ณ์๋ช
ํต์ผ์ฑ์ ์ํด NONE_BADGE๋ก ์์ฑํ์
จ๋ค๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.