code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | reduce๋ฅผ ์ฌ์ฉํ๋ฉด ๊ตณ์ด ์กฐ๊ฑด๋ฌธ์ ๊ฑธ์ง ์์๋ Menu ๊ฐ๊ฒฉ์ ๊ณ์ฐํ ์ ์์ต๋๋ค.
hasOwnProperty ๋ฉ์๋ ๋ด์์ ์์ฒด์ ์ผ๋ก ํด๋น๊ฐ์ด ์์ ์ 0์ ๋ฐํํ๋๋ก ์ค์ ํ๋ค๋ฉด
์๋ reduce ๋ฉ์๋์์ ์ผํญ์ฐ์ฐ์ ๋ถ๋ถ๋ ์ ๊ฑฐํ ์ ์๊ฒ ๋ค์
```js
MENUS.reduce((acc, menu) => {
const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
return acc + (MENUS_PRICE.hasOwnProperty(MENU_NAME) ? MENUS_PRICE[MENU_NAME] * QUANTITY : 0);
}, 0);
``` |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | ์
๋ ฅํ ๋ ์ง๋งํผ 100์์ฉ ์ถ๊ฐ๋ก ๋ง์ด๋์ค ๊ฐ๊ฒฉ์ ํฉ์ฐํ์ฌ ์ต์ข
ํฉ์ฐ ํ ์ธ๊ธ์ก์ ๊ณ์ฐ์ฒ๋ฆฌํ๊ณ ์์ต๋๋ค.
for๋ฌธ์ ์ฌ์ฉํ์ง ์๊ณ ๋ ์
๋ ฅ ๋ ์ง๋ฅผ ์ด์ฉํด์ ์์์ผ๋ก ํด๋น ์ผ์์ ์ ์ฉํ ์ธ ๊ธ์ก์ ๊ณ์ฐํ ์ ์์ต๋๋ค. ๊ณ ๋ฏผํด๋ณด์๊ณ ์์์ ํ์ฉํด ๋ณด์๋ฉด ์ข์ ๊ฑฐ ๊ฐ์์ (๊ฐ๋จํ ์์๋ค์ ์๊ณ ์์ผ๋ฉด ์๊ฐ๋ณด๋ค ์ฝ๋์์ฑ์ ์์ํ๊ฒ ๋์์ค๋๋ค)
๊ทธ๋ฆฌ๊ณ ๊ฐ๊ธ์ for๋ฌธ์ ์์ฐ๊ณ ํด๊ฒฐํด๋ณด๋ ค๊ณ ํด๋ณด์ธ์. ์ ๋ ์ด ๋ฐฉ์์ผ๋ก ์ฝ๋๋ฅผ ๋ง๋ค๋ค๋ณด๋ ์๋ก์ด ๋ฐฉ๋ฒ๋ค์ ์๊พธ ์ฐพ๊ฒ๋๊ณ ๊ทธ ์์์ ๋ฐฐ์ฐ๋ ๊ฒ๋ค์ด ์ ๋ง ๋ง๋๋ผ๊ณ ์ |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | ์ํฉ๋ง๋ค ๋ค๋ฅด์ง๋ง ์์ ์์๋ฅผ ๋ถ์ฌ์ ์์๋ก ๋ง๋ค ๋ ์ง๊ด์ ์ด๊ณ ๋ช
ํํ ํํํ๊ธฐ ์ํด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ ํธํ ์ ์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ else if์ ๋์ด๋๋ ์กฐ๊ฑด๋ฌธ์ด ๊ธธ์ด์ง ์๋ก ๊ฐ๋
์ฑ์ ํด์น๊ธฐ์ ์ง์ํ๋ ํธ์ด ์ข๋ค๊ณ ํฉ๋๋ค.
์๋์ฒ๋ผ ์์ ํด๋ณด์๋ ๊ฑด ์ด๋จ๊น์?
```js
const TOTAL_AFTER = Math.abs(receivedTotalBenefitPrice())
if (TOTAL_AFTER >= 20000) return "์ฐํ";
if (TOTAL_AFTER >= 10000) return "ํธ๋ฆฌ";
if (TOTAL_AFTER >= 5000) return "๋ณ";
return "์์";
``` |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | for..of๋ javascript airbnb์์ ์ถ์ฒํ์ง ์๋ ๋ฐฉ๋ฒ์ด๋ผ๊ณ ํฉ๋๋ค !
forEach()๋ map()์ ์จ๋ณด์๋๊ฑด ์ด๋จ๊น์?
ํด๋น ํจ์๋ for..of์ if๋ฌธ์ ์ฌ์ฉํ๋๊ฑฐ๋ `filter()`๊ฐ ์ ํฉํด๋ณด์
๋๋ค !
์ฐธ๊ณ : [Javascript Airbnb 11.1](https://github.com/tipjs/javascript-style-guide#11.1) |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | has๋ฅผ ์ฌ์ฉํ๋ฉด ๋ํ
์ผํ๊ฒ ์ค๋ณต ์ฒ๋ฆฌํ ์ ์๊ตฐ์...!
์ ๋ ๋ฉ๋ด๋ฅผ ๋ถ๋ฆฌํด์ setํจ์๋ฅผ ์ฌ์ฉํ์๋๋ฐ ์ด๋ฐ ๋ฐฉ๋ฒ๋ ์๋ค๋ ๊ฒ์ ๋ฐฐ์๊ฐ๋๋ค !! |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | ์๋๋ฉด ๊ฐ์ฒด๋ก ์ ์ธํ์ ๋ค์์ find๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์ต๋๋ค!
๊ธ์ก ์กฐ๊ฑด์ด 3๊ฐ๋๊น ๊ด์ฐฎ์ ๋ณด์
๋๋ค ! |
@@ -1,7 +1,27 @@
-export default InputView = {
- async readDate() {
- const input = await Console.readLineAsync("12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)");
- // ...
- }
- // ...
-}
+import { MissionUtils } from "@woowacourse/mission-utils";
+import ChristmasDomain from "./ChristmasPromotion.js";
+import Order from "./Order.js";
+
+export const christmasInstance = new ChristmasDomain();
+
+const InputView = {
+ async readDate() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n"
+ );
+ const DATE = parseInt(input, 10);
+ christmasInstance.startDateValidate(DATE);
+ },
+
+ async readOrderMenu() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n"
+ );
+ new Order(input);
+ const MENUES = input.split(",");
+ christmasInstance.startMenuValidate(MENUES);
+ },
+ // ...
+};
+
+export default InputView; | JavaScript | ํด๋น ๋ฌธ๊ตฌ๋ ๋ณํ์ง ์๋ ์์๊ฐ์ด๋ ๋ณํ์ง ์์ ์์๊ฐ๋ค์ ๋ฐ๋ก constant๋ฅผ ๋ง๋ค์ด์ ํ ๋ฒ์ ์ ๋ฆฌํ๋ ๊ฒ๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์ต๋๋ค ! |
@@ -1,7 +1,27 @@
-export default InputView = {
- async readDate() {
- const input = await Console.readLineAsync("12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)");
- // ...
- }
- // ...
-}
+import { MissionUtils } from "@woowacourse/mission-utils";
+import ChristmasDomain from "./ChristmasPromotion.js";
+import Order from "./Order.js";
+
+export const christmasInstance = new ChristmasDomain();
+
+const InputView = {
+ async readDate() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n"
+ );
+ const DATE = parseInt(input, 10);
+ christmasInstance.startDateValidate(DATE);
+ },
+
+ async readOrderMenu() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n"
+ );
+ new Order(input);
+ const MENUES = input.split(",");
+ christmasInstance.startMenuValidate(MENUES);
+ },
+ // ...
+};
+
+export default InputView; | JavaScript | ํด๋น ๋ฏธ์
์ ์ฐํ
์ฝ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ Random์ ์ฌ์ฉํ์ง ์์ผ๋ `Console`๋ก๋ง ์ ์ธํ์๋ฉด ์ฝ๋๊ฐ ์กฐ๊ธ ๋ ๊น๋ํด ์ง ๊ฒ ๊ฐ์์ ! |
@@ -0,0 +1,24 @@
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+class Order {
+ #order;
+
+ constructor(order) {
+ this.#order = Array.isArray(order) ? order : [order];
+ this.#orderQuantityValidate();
+ }
+
+ #orderQuantityValidate() {
+ this.#order.forEach((menu) => {
+ const { QUANTITY } = menuAndQuantity(menu);
+
+ if (QUANTITY > 20) {
+ throw new Error(
+ `[ERROR] ๋ฉ๋ด๋ ํ ๋ฒ์ ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.`
+ );
+ }
+ });
+ }
+}
+
+export default Order; | JavaScript | forEach()๋ก ์ ์ธํ์ ์ด์ ๋ฅผ ์ ์ ์์๊น์?? |
@@ -1,7 +1,110 @@
-export default OutputView = {
- printMenu() {
- Console.print("<์ฃผ๋ฌธ ๋ฉ๋ด>");
- // ...
+import { MissionUtils } from "@woowacourse/mission-utils";
+import { christmasInstance } from "./InputView.js";
+import {
+ ChampagnePromotionAvailable,
+ receivedChampagnePromotion,
+ receivedD_dayPromotion,
+ receivedSpecialPromotion,
+ receivedTotalBenefitPrice,
+ receivedWeekDayPromotion,
+ receivedWeekendPromotion,
+ sendBadge,
+ toTalPriceLogic,
+ totalPriceAfterDiscount,
+} from "./DomainLogic.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const OutputView = {
+ printIntroduction() {
+ MissionUtils.Console.print(
+ "์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น 12์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค."
+ );
+ },
+
+ printBenefitIntroduction() {
+ MissionUtils.Console.print(
+ "12์ 3์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n"
+ );
+ },
+
+ printMenu() {
+ MissionUtils.Console.print("<์ฃผ๋ฌธ ๋ฉ๋ด>");
+ const ORDERED_MENUS = christmasInstance.getMenus();
+ ORDERED_MENUS.map(function (eachMenu) {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(eachMenu);
+ MissionUtils.Console.print(`${MENU_NAME} ${QUANTITY}๊ฐ`);
+ });
+ },
+
+ printTotalPrice() {
+ MissionUtils.Console.print("\n<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>");
+ const TOTAL_PRICE = toTalPriceLogic().toLocaleString();
+ MissionUtils.Console.print(`${TOTAL_PRICE}์`);
+ },
+
+ printChampagnePromotion() {
+ MissionUtils.Console.print("\n<์ฆ์ ๋ฉ๋ด>");
+ const CHAMPAGNEPROMOTION_AVAILABLE = ChampagnePromotionAvailable();
+ if (CHAMPAGNEPROMOTION_AVAILABLE === true) {
+ MissionUtils.Console.print("์ดํ์ธ 1๊ฐ");
+ } else MissionUtils.Console.print("์์");
+ },
+
+ printReceivedPromotion() {
+ MissionUtils.Console.print("\n<ํํ ๋ด์ญ>");
+ const DDAY_AVAILABLE = receivedD_dayPromotion().toLocaleString();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion().toLocaleString();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion().toLocaleString();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion().toLocaleString();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion().toLocaleString();
+
+ let anyPromotionApplied = false;
+
+ if (DDAY_AVAILABLE !== "0") {
+ MissionUtils.Console.print(
+ `ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: -${DDAY_AVAILABLE}์`
+ );
+ anyPromotionApplied = true;
}
- // ...
-}
+
+ if (WEEKDAY_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`ํ์ผ ํ ์ธ: -${WEEKDAY_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (WEEKEND_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`์ฃผ๋ง ํ ์ธ: -${WEEKEND_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (SPECIAL_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`ํน๋ณ ํ ์ธ: -${SPECIAL_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (CHAMPAGNE_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`์ฆ์ ์ด๋ฒคํธ: -${CHAMPAGNE_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (!anyPromotionApplied) {
+ MissionUtils.Console.print("์์");
+ }
+ },
+
+ printReceivedTotalBenefitPrice() {
+ MissionUtils.Console.print("\n<์ดํํ ๊ธ์ก>");
+ const TOTAL_BENEFITPRICE = receivedTotalBenefitPrice().toLocaleString();
+ MissionUtils.Console.print(`${TOTAL_BENEFITPRICE}์`);
+ },
+
+ printTotalPriceAfterDiscount() {
+ MissionUtils.Console.print("\n<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>");
+ const TOTAL_AFTER = totalPriceAfterDiscount().toLocaleString();
+ MissionUtils.Console.print(`${TOTAL_AFTER}์`);
+ },
+
+ printEventBadge() {
+ MissionUtils.Console.print("\n<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>");
+ const GET_BADGE = sendBadge();
+ MissionUtils.Console.print(`${GET_BADGE}`);
+ },
+};
+
+export default OutputView; | JavaScript | ํด๋น ํํ ๋ด์ญ์ ๋ฐฐ์ด๋ก ์
๋ ฅ๋ฐ์์ forEach()ํจ์๋ฅผ ์จ์ ์ถ๋ ฅํ๋ฉด ์กฐ๊ธ ๋ ๊น๋ํ ๊ฒ ๊ฐ์ต๋๋ค !
0์์ด๊ฑฐ๋ ํด๋นํ์ง ์์ผ๋ฉด undefined๋ก ์
๋ ฅ๋ฐ๊ณ filter()๋ก ์ ๊ฑฐํ๋ฉด ์ฝ๋ ๊ธธ์ด๊ฐ ํ ์ค์ด๋ค ๊ฒ ๊ฐ์ต๋๋ค ! |
@@ -1,5 +1,29 @@
+import { MissionUtils } from "@woowacourse/mission-utils";
+import InputView from "./InputView.js";
+import OutputView from "./OutputView.js";
+
class App {
- async run() {}
+ async run() {
+ await christmasPromotionProcess();
+ }
+}
+
+async function christmasPromotionProcess() {
+ try {
+ OutputView.printIntroduction();
+ await InputView.readDate();
+ await InputView.readOrderMenu();
+ OutputView.printBenefitIntroduction();
+ OutputView.printMenu();
+ OutputView.printTotalPrice();
+ OutputView.printChampagnePromotion();
+ OutputView.printReceivedPromotion();
+ OutputView.printReceivedTotalBenefitPrice();
+ OutputView.printTotalPriceAfterDiscount();
+ OutputView.printEventBadge();
+ } catch (error) {
+ MissionUtils.Console.print(error.message);
+ }
}
export default App; | JavaScript | ํธ์ถ ๋ฉ์๋๊ฐ ๋ง์ ๊ธธ์ด์ง ๊ฑธ ์ ๋ฆฌํ์ง ์์์๋๋ฐ printResult () ๋ฉ์๋๋ฅผ ์์ฑํ๋ ๋ฐฉ์ ์ข์์! |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | ์ง๊ธ๊น์ง ์์๋ผ๋ ๊ฐ๋
์ ์์์ง๋ง ์์๋ฅผ ์ ์ฌ์ฉํ๋์ง์ ๊ณ ๋ฏผ์ด ์์๋ ๊ฒ ๊ฐ์์. ๐ฅฒ๐ |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | ๋ฌด์์ ํด๊ฒฐํด๋ณด๊ณ ๋ณธ๋ค ์คํ์ผ์ด์๋๋ฐ, ์ ๋ง ํด๊ฒฐํ๊ณ ๋์ด์๋ค์. ์์ฑํ ๋๋ถํฐ ์ํฉ์ ๋ง๋ ํจ์จ์ ์ธ ๋ฉ์๋๋ฅผ ํํ๋ ๊ฒ์ด ์ข๊ฒ ์ง๋ง, ํ๊ณ ๋์ ํ์ธํ์ ๋ '์ฌ๊ธฐ์ ๋ง๋ ๋ ์ข์ ๋ฉ์๋๊ฐ ์๋?' ๊ณ ๋ฏผํด ๋ด์ผ๊ฒ ์ด์! |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | ๋งค์ง๋๋ฒ๋ฅผ ์ ๊ฐ ์ฌ์ฉํ๋ ๊ฑฐ๊ตฐ์! ์๋ฏธ์๊ฒ ์ฝ๋ ๊ธธ์ด๊ฐ ๊ธธ์ด์ง ๊ฒ ๊ฐ์์.
๋, ์์ฑ์ ํจ์ ์ฌ์ฉ(๊ฐ์ฒด...!)์ ์ ๊ทน์ ์ผ๋ก ์์ผ๋ก ์ฌ์ฉํด์ผ ๊ฒ ์ด์. |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | ์ ๊ฐ ํ๋์ฝ๋ฉ์ ์ ๋๋ก ํ๋ค์.
```js
let weekday = true;
const date = new Date(`2023-12-${์ ์ฅ๋ ๊ฐ}`);
if (date.getDay() === 5 || date.getDay() === 6) {
weekday = false;
} else {
weekday = true;
}
```
์ด๋ฐ ์์ผ๋ก ์๊ฐ์ ๋ฐ๊ฟ๋ณผ๊ฒ์! |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | `forEach`์ `if๋ฌธ`์ ์ฌ์ฉํ์ง ์๊ณ ๋ Menu ๊ฐ๊ฒฉ์ ๊ณ์ฐํ ์ ์๋ค์! |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | ```js
if (available) {
minusPrice += (DATE -1) * 100;
} else {
minusPrice = 0;
}
```
Songhynseop ๋ฆฌ๋ทฐ๋ก ์๋ ค์ฃผ์ ๊ฒ์ ๋ณด๊ณ ๊ณ ์ณ๋ณด์์ด์. ์์์ผ๋ก ๊ฐ๋จํ๊ฒ ํด๊ฒฐํ ์ ์๋ค์.
'ํน์ ๋ฌธ๋ฒ์ ์ฌ์ฉํ ๋ ๊ผญ ํ์ํ ๊ฐ? ๋ ๊ฐ๋จํ๊ฒ ํ ์ ์๋' ์๊ฐํด ๋ณผ๊ฒ์! |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | Songhyunseop ๋๋ถ์ ์ ๋๊ฐ์ ๋ฐํํด์ฃผ๋ `Math.abs()`์ ์ฌ์ฉ์ ์ ์ ์์์ด์. ์ด๋ฏธ ์ ๊ณตํ๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ์ข๋ค๊ณ ํผ๋๋ฐฑ์์ ๋ณด์๋๋ฐ ์ด ๋ป์ธ ๊ฒ ๊ฐ์์. else if๋ฌธ ๋์ if์์ return ์ ์ฉ ์์ ๋ณด์ฌ์ฃผ์ ๊ฑฐ ๊ฐ์ฌํด์.
hyurim ๋ฆฌ๋ทฐ์ ๋ฐ๋ฅด๋ฉด ๋ ๊ฐ์ฒด์งํฅ์ ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ด์ ์ข์์.
```JS
const BADGE_THRESHOLDS = { ์ฐํ: 20000, ํธ๋ฆฌ: 10000, ๋ณ: 5000 };
export function sendBadge() {
const TOTAL_AFTER = Math.abs(receivedTotalBenefitPrice());
for (const BADGE of Object.keys(BADGE_THRESHOLDS)) {
if (TOTAL_AFTER >= BADGE_THRESHOLDS[BADGE]) {
return BADGE;
}
}
return "์์";
}
```
์ ์ง ๋ณด์์๋ ํธํ ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,78 @@
+import {
+ ChampagnePromotionAvailable,
+ receivedD_dayPromotion,
+ toTalPriceLogic,
+} from "../src/DomainLogic";
+import menuAndQuantity from "../src/utils/menuAndQuantity";
+
+jest.mock("../src/InputView", () => ({
+ christmasInstance: {
+ getMenus: jest.fn(),
+ getDate: jest.fn(),
+ },
+}));
+
+jest.mock("../src/utils/menuAndQuantity", () => ({
+ __esModule: true,
+ default: jest.fn(),
+}));
+
+jest.mock("../src/DomainLogic", () => ({
+ ...jest.requireActual("../src/DomainLogic"),
+ toTalPriceLogic: jest.fn(),
+}));
+
+describe("DomainLogic ๊ธฐ๋ฅ ํ
์คํธ", () => {
+ test("ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก", () => {
+ const mockedMenus = ["์์ก์ด์ํ-2", "์์ก์ด์ํ-1", "ํฐ๋ณธ์คํ
์ดํฌ-3"];
+ require("../src/InputView").christmasInstance.getMenus.mockReturnValue(
+ mockedMenus
+ );
+
+ menuAndQuantity
+ .mockReturnValueOnce({ MENU_NAME: "์์ก์ด์ํ", QUANTITY: 2 })
+ .mockReturnValueOnce({ MENU_NAME: "์ด์ฝ์ผ์ดํฌ", QUANTITY: 1 })
+ .mockReturnValueOnce({ MENU_NAME: "ํฐ๋ณธ์คํ
์ดํฌ", QUANTITY: 3 });
+
+ const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ };
+
+ toTalPriceLogic.mockReturnValueOnce(192000);
+
+ const result = toTalPriceLogic(MENUS_PRICE);
+ const expectedTotalPrice = 2 * 6000 + 1 * 15000 + 3 * 55000;
+
+ expect(result).toBe(expectedTotalPrice);
+ });
+
+ test("์ฆ์ ๋ฉ๋ด", () => {
+ const mockedMenus = ["์์ด์คํฌ๋ฆผ-2", "์ด์ฝ์ผ์ดํฌ-1"];
+ require("../src/InputView").christmasInstance.getMenus.mockReturnValue(
+ mockedMenus
+ );
+
+ toTalPriceLogic.mockReturnValueOnce(25000);
+
+ const result = ChampagnePromotionAvailable();
+ expect(result).toBe(false);
+ });
+
+ test("ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ", () => {
+ require("../src/InputView").christmasInstance.getDate.mockReturnValue(25);
+
+ const result = receivedD_dayPromotion();
+
+ expect(result).toBe(3400);
+ });
+
+ test("ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ ์์ธ", () => {
+ require("../src/InputView").christmasInstance.getDate.mockReturnValue(26);
+
+ const result = receivedD_dayPromotion();
+
+ expect(result).toBe(0);
+ });
+}); | JavaScript | christmasInstance๋ฅผ ์ ์ธํ๋ฉด ํจ์๋ผ mock๋ฅผ ์ ์ฉํ๋ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | ````js
menus.forEach((menu) => { . . .
````
์ด๋ฐ ์์ผ๋ก ์ฝ๋๋ฅผ ๊ณ ์น๊ฒ ์ต๋๋ค. `for..of`๋ ์ฌ์ฉ์ ์ง์ํ๋๋ก ํ๊ฒ ์ต๋๋ค! |
@@ -1,7 +1,27 @@
-export default InputView = {
- async readDate() {
- const input = await Console.readLineAsync("12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)");
- // ...
- }
- // ...
-}
+import { MissionUtils } from "@woowacourse/mission-utils";
+import ChristmasDomain from "./ChristmasPromotion.js";
+import Order from "./Order.js";
+
+export const christmasInstance = new ChristmasDomain();
+
+const InputView = {
+ async readDate() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n"
+ );
+ const DATE = parseInt(input, 10);
+ christmasInstance.startDateValidate(DATE);
+ },
+
+ async readOrderMenu() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n"
+ );
+ new Order(input);
+ const MENUES = input.split(",");
+ christmasInstance.startMenuValidate(MENUES);
+ },
+ // ...
+};
+
+export default InputView; | JavaScript | ์คํธ!
```js
import { Console } from "@woowacourse/mission-utils";
```
import ํด์ค๋ ๊ฒ๋ ์ ์ธํ๋ค๊ณ ํํํ๋ ๊ฒ๋ ์ฒ์ ์์์ด์! ๊ฐ์ฌํฉ๋๋ค. |
@@ -1,7 +1,27 @@
-export default InputView = {
- async readDate() {
- const input = await Console.readLineAsync("12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)");
- // ...
- }
- // ...
-}
+import { MissionUtils } from "@woowacourse/mission-utils";
+import ChristmasDomain from "./ChristmasPromotion.js";
+import Order from "./Order.js";
+
+export const christmasInstance = new ChristmasDomain();
+
+const InputView = {
+ async readDate() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n"
+ );
+ const DATE = parseInt(input, 10);
+ christmasInstance.startDateValidate(DATE);
+ },
+
+ async readOrderMenu() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n"
+ );
+ new Order(input);
+ const MENUES = input.split(",");
+ christmasInstance.startMenuValidate(MENUES);
+ },
+ // ...
+};
+
+export default InputView; | JavaScript | ๋ค ๋ณํ์ง ์๋ ๊ฒ์ ์์๋ก ๋ฐ๊ฟ์ผ ์ข๋ค์. ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,24 @@
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+class Order {
+ #order;
+
+ constructor(order) {
+ this.#order = Array.isArray(order) ? order : [order];
+ this.#orderQuantityValidate();
+ }
+
+ #orderQuantityValidate() {
+ this.#order.forEach((menu) => {
+ const { QUANTITY } = menuAndQuantity(menu);
+
+ if (QUANTITY > 20) {
+ throw new Error(
+ `[ERROR] ๋ฉ๋ด๋ ํ ๋ฒ์ ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.`
+ );
+ }
+ });
+ }
+}
+
+export default Order; | JavaScript | ```js
export default function menuAndQuantity(menu) {
const [MENU_NAME, NUMBER] = menu.split("-");
const QUANTITY = parseInt(NUMBER, 10);
return { MENU_NAME, QUANTITY };
}
```
**์ด `menuAndQuantity(menu)` ํจ์๋ฅผ ์ฌ์ฉํด์ ์๋ ํ์
**ํ์ต๋๋ค.
๊ทธ๋ฌ๊ธฐ ์ํด์๋ menu (menu-quantity)๊ฐ ํ๋์ฉ ์ ๋ฌํด์ผ ํด์ `forEach()`๋ฅผ ์ฌ์ฉํ์ต๋๋ค. |
@@ -1,7 +1,110 @@
-export default OutputView = {
- printMenu() {
- Console.print("<์ฃผ๋ฌธ ๋ฉ๋ด>");
- // ...
+import { MissionUtils } from "@woowacourse/mission-utils";
+import { christmasInstance } from "./InputView.js";
+import {
+ ChampagnePromotionAvailable,
+ receivedChampagnePromotion,
+ receivedD_dayPromotion,
+ receivedSpecialPromotion,
+ receivedTotalBenefitPrice,
+ receivedWeekDayPromotion,
+ receivedWeekendPromotion,
+ sendBadge,
+ toTalPriceLogic,
+ totalPriceAfterDiscount,
+} from "./DomainLogic.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const OutputView = {
+ printIntroduction() {
+ MissionUtils.Console.print(
+ "์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น 12์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค."
+ );
+ },
+
+ printBenefitIntroduction() {
+ MissionUtils.Console.print(
+ "12์ 3์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n"
+ );
+ },
+
+ printMenu() {
+ MissionUtils.Console.print("<์ฃผ๋ฌธ ๋ฉ๋ด>");
+ const ORDERED_MENUS = christmasInstance.getMenus();
+ ORDERED_MENUS.map(function (eachMenu) {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(eachMenu);
+ MissionUtils.Console.print(`${MENU_NAME} ${QUANTITY}๊ฐ`);
+ });
+ },
+
+ printTotalPrice() {
+ MissionUtils.Console.print("\n<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>");
+ const TOTAL_PRICE = toTalPriceLogic().toLocaleString();
+ MissionUtils.Console.print(`${TOTAL_PRICE}์`);
+ },
+
+ printChampagnePromotion() {
+ MissionUtils.Console.print("\n<์ฆ์ ๋ฉ๋ด>");
+ const CHAMPAGNEPROMOTION_AVAILABLE = ChampagnePromotionAvailable();
+ if (CHAMPAGNEPROMOTION_AVAILABLE === true) {
+ MissionUtils.Console.print("์ดํ์ธ 1๊ฐ");
+ } else MissionUtils.Console.print("์์");
+ },
+
+ printReceivedPromotion() {
+ MissionUtils.Console.print("\n<ํํ ๋ด์ญ>");
+ const DDAY_AVAILABLE = receivedD_dayPromotion().toLocaleString();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion().toLocaleString();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion().toLocaleString();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion().toLocaleString();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion().toLocaleString();
+
+ let anyPromotionApplied = false;
+
+ if (DDAY_AVAILABLE !== "0") {
+ MissionUtils.Console.print(
+ `ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: -${DDAY_AVAILABLE}์`
+ );
+ anyPromotionApplied = true;
}
- // ...
-}
+
+ if (WEEKDAY_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`ํ์ผ ํ ์ธ: -${WEEKDAY_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (WEEKEND_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`์ฃผ๋ง ํ ์ธ: -${WEEKEND_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (SPECIAL_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`ํน๋ณ ํ ์ธ: -${SPECIAL_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (CHAMPAGNE_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`์ฆ์ ์ด๋ฒคํธ: -${CHAMPAGNE_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (!anyPromotionApplied) {
+ MissionUtils.Console.print("์์");
+ }
+ },
+
+ printReceivedTotalBenefitPrice() {
+ MissionUtils.Console.print("\n<์ดํํ ๊ธ์ก>");
+ const TOTAL_BENEFITPRICE = receivedTotalBenefitPrice().toLocaleString();
+ MissionUtils.Console.print(`${TOTAL_BENEFITPRICE}์`);
+ },
+
+ printTotalPriceAfterDiscount() {
+ MissionUtils.Console.print("\n<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>");
+ const TOTAL_AFTER = totalPriceAfterDiscount().toLocaleString();
+ MissionUtils.Console.print(`${TOTAL_AFTER}์`);
+ },
+
+ printEventBadge() {
+ MissionUtils.Console.print("\n<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>");
+ const GET_BADGE = sendBadge();
+ MissionUtils.Console.print(`${GET_BADGE}`);
+ },
+};
+
+export default OutputView; | JavaScript | return ๊ฐ์ด 0, ๊ณตํต๋ถ๋ถ์ ํ์ฉํ๋ฉด ์ ๋ง ๊น๋ํ ์ฝ๋๊ฐ ๋๊ฒ ๋ค์!
๋ฐ๋ก ๊ณ ์น๊ธฐ์๋ ์ข ์ด๋ ค์์ ๋ ์๊ฐํด๋ณด๊ณ ์ฝ๋๋ฅผ ์์ ํด ๋ณด๊ฒ ์ต๋๋ค! ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,5 @@
+package store.constants;
+
+public class MembershipConstants {
+ public static final double DISCOUNT_RATE = 0.30;
+} | Java | ์ด ๋ถ๋ถ์ ํด๋์ค๋ฅผ ์์ฑํ ์ ์๊ฒ๋ ์์ฑ์๋ฅผ private์ผ๋ก ๋ง๋ค๋ฉด ์ข์ ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,18 @@
+package store.constants;
+
+public class ReceiptConstants {
+ public static final String ORDER_DETAIL_FORMAT = "%-10s %6s %12s";
+ public static final String PROMOTION_DETAIL_FORMAT = "%-10s %6s";
+ public static final String TOTAL_DETAIL_FORMAT = "%-10s %18s";
+ public static final String LINE_SEPARATOR = "====================================";
+ public static final String PROMOTION_HEADER = "=============์ฆ\t\t์ ===============";
+ public static final String RECEIPT_HEADER = "==============W ํธ์์ ================";
+ public static final String PRODUCT_HEADER = "%-10s %6s %12s";
+ public static final String PRODUCT_NAME = "์ํ๋ช
";
+ public static final String QUANTITY = "์๋";
+ public static final String PRICE = "๊ธ์ก";
+ public static final String TOTAL_PURCHASE_AMOUNT = "์ด๊ตฌ๋งค์ก";
+ public static final String EVENT_DISCOUNT = "ํ์ฌํ ์ธ";
+ public static final String MEMBERSHIP_DISCOUNT = "๋ฉค๋ฒ์ญํ ์ธ";
+ public static final String FINAL_AMOUNT = "๋ด์ค๋";
+} | Java | Enum์ ์ฌ์ฉํ์ง ์์ผ์ ์ด์ ๊ฐ ์์๊น์?
String.format์ ์ฌ์ฉํ์ค ๋ Enum ๋ด์ ๋ณํํด์ ๋ฐํํ๋ฉด OuptView์์๋ ๋ ๊น๋ํ๊ฒ ๋ก์ง์ ์ฒ๋ฆฌํ ์ ์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,38 @@
+package store.controller;
+
+import java.util.List;
+import java.util.Map;
+import store.domain.Product;
+import store.domain.Promotion;
+import store.dto.StoreDto;
+import store.dto.StoreInitializationDto;
+import store.parser.ProductParser;
+import store.parser.PromotionParser;
+import store.service.FileReaderService;
+
+public class FileReaderController {
+ private final FileReaderService fileReaderService;
+ private final PromotionParser promotionParser;
+ private final ProductParser productParser;
+
+ public FileReaderController() {
+ this.fileReaderService = new FileReaderService();
+ this.productParser = new ProductParser();
+ this.promotionParser = new PromotionParser();
+ }
+
+ public StoreDto runFileData() {
+ return initialize();
+ }
+
+ public StoreDto initialize() {
+ StoreInitializationDto storeInitializationDto = fileReaderService.initializeStoreData();
+ return parseStoreData(storeInitializationDto);
+ }
+
+ public StoreDto parseStoreData(StoreInitializationDto storeInitializationDto) {
+ List<Promotion> promotions = promotionParser.parse(storeInitializationDto.promotionDtos());
+ Map<String, Product> products = productParser.parse(storeInitializationDto.productDtos(), promotions);
+ return new StoreDto(products, promotions);
+ }
+} | Java | ์ด๋ฐ ๋ถ๋ถ์ private๋ฅผ ํตํด ์บก์ํํ๋ ๊ฒ์ด ์ด๋จ๊น์? |
@@ -0,0 +1,23 @@
+package store.controller;
+
+import store.domain.Store;
+import store.dto.StoreDto;
+import store.service.StoreService;
+
+public class StoreController {
+
+ private final StoreService storeService;
+
+ public StoreController() {
+ this.storeService = new StoreService();
+ }
+
+ public void run(StoreDto storeDto) {
+ Store store = createStore(storeDto);
+ storeService.processOrder(store);
+ }
+
+ private Store createStore(StoreDto storeDto) {
+ return new Store(storeDto);
+ }
+} | Java | ์ธ๋ถ์์ ์์กด์ฑ ์ฃผ์
ํ๋ ๋ฐฉ์์ ์ฌ์ฉํ์ง ์์ผ์ ์ด์ ๊ฐ ์์๊น์? |
@@ -0,0 +1,35 @@
+package store.domain;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+public class Cart {
+ private final Map<String, CartItem> cartItems;
+
+ public Cart() {
+ this.cartItems = new LinkedHashMap<>();
+ }
+
+ public void addItem(List<CartItem> cartItems) {
+ for (CartItem cartItem : cartItems) {
+ this.cartItems.put(cartItem.getProduct().getName(), cartItem);
+ }
+ }
+
+ public List<CartItem> getAllItemsInCart() {
+ return cartItems.values().stream().toList();
+ }
+
+ public int getTotalFreeItemQuantity() {
+ return cartItems.values().stream().mapToInt(CartItem::getFreeQuantity).sum();
+ }
+
+ public int getTotalFreeItemPrice() {
+ return cartItems.values().stream().mapToInt(CartItem::getTotalFreePrice).sum();
+ }
+
+ public int getTotalItemPrice() {
+ return cartItems.values().stream().mapToInt(CartItem::totalPrice).sum();
+ }
+} | Java | Stream API๋ ๊ฐ๋
์ฑ์ ์ํด ์ํฐ๋ฅผ ์ฌ์ฉํ๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,56 @@
+package store.domain;
+
+import static store.message.ErrorMessage.INSUFFICIENT_STOCK_ERROR;
+
+import store.validation.CartItemValidator;
+
+public class CartItem {
+ private final Product product;
+ private int quantity;
+ private int freeQuantity;
+
+ public CartItem(Product product, int quantity) {
+ CartItemValidator.validateCartItem(quantity);
+ this.product = product;
+ this.quantity = quantity;
+ }
+
+ public void increaseQuantity(int updateQuantity) {
+ this.freeQuantity += updateQuantity;
+ }
+
+ public void decreaseQuantity(int updateQuantity) {
+ this.quantity -= updateQuantity;
+ if (quantity <= 0) {
+ throw new IllegalArgumentException(INSUFFICIENT_STOCK_ERROR.getMessage());
+ }
+ }
+
+ public String getProductName() {
+ return product.getName();
+ }
+
+ public int totalPrice() {
+ return product.getPrice() * getTotalQuantity();
+ }
+
+ public int getTotalFreePrice() {
+ return product.getPrice() * freeQuantity;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public int getTotalQuantity() {
+ return quantity + freeQuantity;
+ }
+
+ public Product getProduct() {
+ return product;
+ }
+
+ public int getFreeQuantity() {
+ return freeQuantity;
+ }
+} | Java | ๋๋ฏธํฐ ๋ฒ์น์ ์ค์ํ๊ธฐ ์ํด ์์ฑํ์ ๋ฉ์๋๋ก ๋ณด์
๋๋ค. ์ ๋ ์ด๋ฐ ๋ฐฉ์์ผ๋ก ์งํํ๋๋ฐ์, 3์ฃผ์ฐจ ํผ๋๋ฐฑ๊ณผ ์๋ฐ ์คํ์ผ ๊ฐ์ด๋์์ Getter๋ฅผ ์ฌ์ฉํ๊ธฐ๋ณด๋ค ๊ฐ์ฒด ๋ด์์ ์ฒ๋ฆฌํ๋ ๋ฐฉ์์ด ๋ ์ข๋ค๋ ๋ด์ฉ์ด ์์์ต๋๋ค.
์ด ๋ถ๋ถ์ ์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,136 @@
+package store.domain;
+
+import static store.constants.ProductConstants.OUT_OF_STOCK;
+import static store.constants.ProductConstants.PRODUCT_DESCRIPTION_PREFIX;
+import static store.constants.ProductConstants.UNIT_QUANTITY;
+import static store.constants.ProductConstants.UNIT_WON;
+import static store.message.ErrorMessage.NOT_FOUND_PROMOTION;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+import java.util.Optional;
+import store.dto.ProductDto;
+import store.validation.ProductValidator;
+
+public class Product {
+ private String name;
+ private int price;
+ private int quantity;
+ private Optional<Promotion> promotion;
+
+ private Product() {
+ }
+
+ public Product(ProductDto productDto, Optional<Promotion> promotion) {
+ ProductValidator.validate(productDto);
+ this.name = productDto.name();
+ this.price = Integer.parseInt(productDto.price());
+ this.quantity = Integer.parseInt(productDto.quantity());
+ this.promotion = promotion;
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ ์ ์ฉ ์ฌ๋ถ ๊ฒ์ฆ
+ */
+ public boolean isEligibleForStandardPromotion(int orderedQuantity) {
+ if (promotion.isEmpty()) {
+ return false;
+ }
+ Promotion promotionInfo = promotion.get();
+ return orderedQuantity <= quantity
+ && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity;
+ }
+
+ /**
+ * ๋ณด๋์ค ์ํ์ด ์ ์ฉ ์ฌ๋ถ ๊ฒ์ฆ
+ */
+ public boolean isEligibleForBonusProduct(int orderedQuantity) {
+ if (promotion.isEmpty()) {
+ return false;
+ }
+ Promotion promotionInfo = promotion.get();
+ return getRemainingItemsForPromotion(orderedQuantity, promotionInfo) >= promotionInfo.getBuy()
+ && orderedQuantity + promotionInfo.getGet() <= quantity;
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋จ์ ์๋์ ๊ณ์ฐ
+ */
+ public int getRemainingItemsForPromotion(int orderedQuantity, Promotion promotionInfo) {
+ return orderedQuantity % promotionInfo.getTotalRequiredQuantity();
+ }
+
+ /**
+ * ๋ณด๋์ค ์ํ์ ์๋์ ๊ณ์ฐ
+ */
+ public int calculateBonusQuantity(int orderedQuantity, Promotion promotion) {
+ return (orderedQuantity / promotion.getTotalRequiredQuantity()) * promotion.getGet();
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์ ์ฉ๋ ํ, ์ค์ ์ฃผ๋ฌธ ๊ฐ๋ฅํ ์๋์ ๊ณ์ฐ
+ */
+ public int calculateQuantityAfterPromotion(int orderedQuantity) {
+ Promotion promotionInfo = getPromotionOrElseThrow();
+ int requiredQuantity = promotionInfo.getTotalRequiredQuantity();
+ return (orderedQuantity - quantity) + (quantity % requiredQuantity);
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ ๋ฐ์
+ */
+ public Promotion getPromotionOrElseThrow() {
+ return promotion.orElseThrow(() -> new IllegalArgumentException(NOT_FOUND_PROMOTION.getMessage()));
+ }
+
+ /**
+ * ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ์ฃผ์ฒด์ด๊ธฐ ๋๋ฌธ์ ์ํ ๋ฆฌ์คํธ๋ฅผ ํฌ๋งท์ ํฉ๋๋ค.
+ */
+ @Override
+ public String toString() {
+ return PRODUCT_DESCRIPTION_PREFIX
+ + name + " "
+ + getFormatKoreanLocale(price) + UNIT_WON
+ + getFormattedQuantity()
+ + getFormattedPromotion();
+ }
+
+ public String getFormattedQuantity() {
+ if (quantity < 1) {
+ return OUT_OF_STOCK;
+ }
+ return getFormatKoreanLocale(quantity) + UNIT_QUANTITY;
+ }
+
+ public String getFormatKoreanLocale(int number) {
+ return NumberFormat.getInstance(Locale.KOREA).format(number);
+ }
+
+ public String getFormattedPromotion() {
+ return promotion.map(Promotion::getName).orElse("");
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public Optional<Promotion> getPromotion() {
+ return promotion;
+ }
+
+ public boolean isPromotionalProduct() {
+ return promotion.isPresent();
+ }
+
+ public void decreaseQuantity(int orderQuantity) {
+ this.quantity -= orderQuantity;
+ }
+} | Java | 1์ฃผ์ฐจ ํผ๋๋ฐฑ์์, ์ฃผ์์ ์ฌ์ฉํ๊ธฐ ๋ณด๋ค๋ ๋ฉ์๋๋ช
์ ํตํด ์๋๋ฅผ ํ์
ํ๊ฒ ํ๋ ๊ฒ์ด ๋ ์ข๋ค๋ ๋ด์ฉ์ด ์์์ต๋๋ค. ์ด ๋ถ๋ถ์ ๋ฐ๋ก ์ฃผ์์ ํ์ํ ์ด์ ๊ฐ ์์๊น์? |
@@ -0,0 +1,55 @@
+package store.domain;
+
+import java.time.LocalDateTime;
+import store.dto.PromotionDto;
+import store.util.DateUtil;
+import store.validation.PromotionValidator;
+
+public class Promotion {
+
+ private String name; //ํ๋ก๋ชจ์
๋ช
+ private int buy; // ๊ตฌ๋งค์กฐ๊ฑด
+ private int get; //์ฆ์ ์๋
+ private LocalDateTime startDate;
+ private LocalDateTime endDate;
+
+ private Promotion() {
+ }
+
+ public Promotion(PromotionDto promotionDto) {
+ PromotionValidator.validate(promotionDto);
+ this.name = promotionDto.name();
+ this.buy = Integer.parseInt(promotionDto.buy());
+ this.get = Integer.parseInt(promotionDto.get());
+ this.startDate = DateUtil.dateParse(promotionDto.startDate());
+ this.endDate = DateUtil.dateParse(promotionDto.endDate());
+ }
+
+ public boolean isPromotionName(String productPromotionName) {
+ return this.name.equals(productPromotionName);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getTotalRequiredQuantity() {
+ return buy + get;
+ }
+
+ public int getBuy() {
+ return buy;
+ }
+
+ public int getGet() {
+ return get;
+ }
+
+ public LocalDateTime getStartDate() {
+ return startDate;
+ }
+
+ public LocalDateTime getEndDate() {
+ return endDate;
+ }
+} | Java | ์๊ฒ๋ ์ฃผ์๋ณด๋ค๋ ๋ณ์๋ช
์ ์ ์ง๋๊ฒ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,55 @@
+package store.domain;
+
+import java.time.LocalDateTime;
+import store.dto.PromotionDto;
+import store.util.DateUtil;
+import store.validation.PromotionValidator;
+
+public class Promotion {
+
+ private String name; //ํ๋ก๋ชจ์
๋ช
+ private int buy; // ๊ตฌ๋งค์กฐ๊ฑด
+ private int get; //์ฆ์ ์๋
+ private LocalDateTime startDate;
+ private LocalDateTime endDate;
+
+ private Promotion() {
+ }
+
+ public Promotion(PromotionDto promotionDto) {
+ PromotionValidator.validate(promotionDto);
+ this.name = promotionDto.name();
+ this.buy = Integer.parseInt(promotionDto.buy());
+ this.get = Integer.parseInt(promotionDto.get());
+ this.startDate = DateUtil.dateParse(promotionDto.startDate());
+ this.endDate = DateUtil.dateParse(promotionDto.endDate());
+ }
+
+ public boolean isPromotionName(String productPromotionName) {
+ return this.name.equals(productPromotionName);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getTotalRequiredQuantity() {
+ return buy + get;
+ }
+
+ public int getBuy() {
+ return buy;
+ }
+
+ public int getGet() {
+ return get;
+ }
+
+ public LocalDateTime getStartDate() {
+ return startDate;
+ }
+
+ public LocalDateTime getEndDate() {
+ return endDate;
+ }
+} | Java | getGet() ๋ฉ์๋๋ช
์ ๋ณด์๋ฉด ๊ฐ๋
์ฑ์ด ๋งค์ฐ ๋จ์ด์ง๋ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,152 @@
+package store.domain;
+
+import static store.constants.ReceiptConstants.EVENT_DISCOUNT;
+import static store.constants.ReceiptConstants.FINAL_AMOUNT;
+import static store.constants.ReceiptConstants.LINE_SEPARATOR;
+import static store.constants.ReceiptConstants.MEMBERSHIP_DISCOUNT;
+import static store.constants.ReceiptConstants.ORDER_DETAIL_FORMAT;
+import static store.constants.ReceiptConstants.PRICE;
+import static store.constants.ReceiptConstants.PRODUCT_HEADER;
+import static store.constants.ReceiptConstants.PRODUCT_NAME;
+import static store.constants.ReceiptConstants.PROMOTION_DETAIL_FORMAT;
+import static store.constants.ReceiptConstants.PROMOTION_HEADER;
+import static store.constants.ReceiptConstants.QUANTITY;
+import static store.constants.ReceiptConstants.RECEIPT_HEADER;
+import static store.constants.ReceiptConstants.TOTAL_DETAIL_FORMAT;
+import static store.constants.ReceiptConstants.TOTAL_PURCHASE_AMOUNT;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+public class Receipt {
+ private final Store store;
+ private final Cart cart;
+ private final Membership membership;
+
+ public Receipt(Store store, Cart cart, Membership membership) {
+ this.store = store;
+ this.cart = cart;
+ this.membership = membership;
+ }
+
+ @Override
+ public String toString() {
+ return buildReceiptContent();
+ }
+
+ public boolean hasFreeItems() {
+ return cart.getTotalFreeItemQuantity() > 0;
+ }
+
+ /**
+ * ์ฅ๋ฐ๊ตฌ๋ ๋ฐ ๋ฉค๋ฒ์ญ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์์์ฆ ๋ด์ฉ์ ๋น๋ํ๋ ๋ฉ์๋์
๋๋ค.
+ * <p>
+ * ์ถ๊ฐ๋ก ์ฆ์ ๋ ์ํ์ด ์๋ค๋ฉด ์ํ๋ด์ญ ์ ๋ณด๋ ์ถ๋ ฅํ์ง ์์ต๋๋ค.
+ */
+ public String buildReceiptContent() {
+ StringBuilder result = new StringBuilder();
+ result.append(getReceiptHeader());
+ result.append(toStringOrderDetail());
+ if (hasFreeItems()) {
+ result.append(toStringRemainingItemsForPromotion());
+ }
+ result.append(toStringTotal());
+ return result.toString();
+ }
+
+ /**
+ * ์์์ฆ ํค๋๋ฅผ ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String getReceiptHeader() {
+ StringBuilder header = new StringBuilder();
+ header.append(RECEIPT_HEADER).append("\n");
+ header.append(String.format(PRODUCT_HEADER, PRODUCT_NAME, QUANTITY, PRICE)).append("\n");
+ return header.toString();
+ }
+
+ /**
+ * ์ฃผ๋ฌธ ๋ด์ญ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String toStringOrderDetail() {
+ StringBuilder orderDetail = new StringBuilder();
+
+ for (CartItem cartItem : cart.getAllItemsInCart()) {
+ orderDetail.append(String.format(ORDER_DETAIL_FORMAT, cartItem.getProductName(),
+ numberFormat(cartItem.getQuantity()), numberFormat(cartItem.totalPrice())))
+ .append("\n");
+ }
+ return orderDetail.toString();
+ }
+
+ /**
+ * ์ฆ์ ์ํ ๋ด์ญ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String toStringRemainingItemsForPromotion() {
+ StringBuilder remainingitems = new StringBuilder();
+ remainingitems.append(PROMOTION_HEADER).append("\n");
+
+ for (CartItem cartItem : cart.getAllItemsInCart()) {
+ if (cartItem.getFreeQuantity() > 0) {
+ remainingitems.append(String.format(PROMOTION_DETAIL_FORMAT, cartItem.getProductName(),
+ numberFormat(cartItem.getFreeQuantity())))
+ .append("\n");
+ }
+ }
+ return remainingitems.toString();
+ }
+
+ /**
+ * ์ด ๊ธ์ก ๋ด์ญ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String toStringTotal() {
+ StringBuilder total = new StringBuilder();
+ total.append(LINE_SEPARATOR).append("\n");
+
+ total.append(formatTotalItemPrice())
+ .append("\n");
+ total.append(formatEventDiscount())
+ .append("\n");
+ total.append(formatMembershipDiscount())
+ .append("\n");
+ total.append(formatFinalAmount())
+ .append("\n");
+
+ return total.toString();
+ }
+
+ /**
+ * ์ด ๊ตฌ๋งค ๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatTotalItemPrice() {
+ return String.format(TOTAL_DETAIL_FORMAT, TOTAL_PURCHASE_AMOUNT, numberFormat(cart.getTotalItemPrice()));
+ }
+
+ /**
+ * ์ด๋ฒคํธ ํ ์ธ ๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatEventDiscount() {
+ return String.format(TOTAL_DETAIL_FORMAT, EVENT_DISCOUNT, numberFormat(cart.getTotalFreeItemPrice()));
+ }
+
+ /**
+ * ๋ฉค๋ฒ์ญ ํ ์ธ ๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatMembershipDiscount() {
+ return String.format(TOTAL_DETAIL_FORMAT, MEMBERSHIP_DISCOUNT, numberFormat(membership.getMembershipPrice()));
+ }
+
+ /**
+ * ์ต์ข
๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatFinalAmount() {
+ int finalAmount = cart.getTotalItemPrice() - cart.getTotalFreeItemPrice() - membership.getMembershipPrice();
+ return String.format(TOTAL_DETAIL_FORMAT, FINAL_AMOUNT, numberFormat(finalAmount));
+ }
+
+ /**
+ * ์ซ์๋ฅผ ํ๊ตญ์ ์ซ์ ํฌ๋งท์ ๋ง๊ฒ ํ์ํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String numberFormat(int number) {
+ return NumberFormat.getInstance(Locale.KOREA).format(number);
+ }
+} | Java | ์ฌ๊ธฐ๋ ์ ๋น๋ผ์ธ์ ์ถ๊ฐํ์ง ์์ผ์
จ๋์? ์ด์ ์ฝ๋์๋ ์ผ๊ด์ฑ ์๊ฒ ์ถ๊ฐํ ๊ฒ ๊ฐ์์์! |
@@ -0,0 +1,152 @@
+package store.domain;
+
+import static store.constants.ReceiptConstants.EVENT_DISCOUNT;
+import static store.constants.ReceiptConstants.FINAL_AMOUNT;
+import static store.constants.ReceiptConstants.LINE_SEPARATOR;
+import static store.constants.ReceiptConstants.MEMBERSHIP_DISCOUNT;
+import static store.constants.ReceiptConstants.ORDER_DETAIL_FORMAT;
+import static store.constants.ReceiptConstants.PRICE;
+import static store.constants.ReceiptConstants.PRODUCT_HEADER;
+import static store.constants.ReceiptConstants.PRODUCT_NAME;
+import static store.constants.ReceiptConstants.PROMOTION_DETAIL_FORMAT;
+import static store.constants.ReceiptConstants.PROMOTION_HEADER;
+import static store.constants.ReceiptConstants.QUANTITY;
+import static store.constants.ReceiptConstants.RECEIPT_HEADER;
+import static store.constants.ReceiptConstants.TOTAL_DETAIL_FORMAT;
+import static store.constants.ReceiptConstants.TOTAL_PURCHASE_AMOUNT;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+public class Receipt {
+ private final Store store;
+ private final Cart cart;
+ private final Membership membership;
+
+ public Receipt(Store store, Cart cart, Membership membership) {
+ this.store = store;
+ this.cart = cart;
+ this.membership = membership;
+ }
+
+ @Override
+ public String toString() {
+ return buildReceiptContent();
+ }
+
+ public boolean hasFreeItems() {
+ return cart.getTotalFreeItemQuantity() > 0;
+ }
+
+ /**
+ * ์ฅ๋ฐ๊ตฌ๋ ๋ฐ ๋ฉค๋ฒ์ญ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์์์ฆ ๋ด์ฉ์ ๋น๋ํ๋ ๋ฉ์๋์
๋๋ค.
+ * <p>
+ * ์ถ๊ฐ๋ก ์ฆ์ ๋ ์ํ์ด ์๋ค๋ฉด ์ํ๋ด์ญ ์ ๋ณด๋ ์ถ๋ ฅํ์ง ์์ต๋๋ค.
+ */
+ public String buildReceiptContent() {
+ StringBuilder result = new StringBuilder();
+ result.append(getReceiptHeader());
+ result.append(toStringOrderDetail());
+ if (hasFreeItems()) {
+ result.append(toStringRemainingItemsForPromotion());
+ }
+ result.append(toStringTotal());
+ return result.toString();
+ }
+
+ /**
+ * ์์์ฆ ํค๋๋ฅผ ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String getReceiptHeader() {
+ StringBuilder header = new StringBuilder();
+ header.append(RECEIPT_HEADER).append("\n");
+ header.append(String.format(PRODUCT_HEADER, PRODUCT_NAME, QUANTITY, PRICE)).append("\n");
+ return header.toString();
+ }
+
+ /**
+ * ์ฃผ๋ฌธ ๋ด์ญ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String toStringOrderDetail() {
+ StringBuilder orderDetail = new StringBuilder();
+
+ for (CartItem cartItem : cart.getAllItemsInCart()) {
+ orderDetail.append(String.format(ORDER_DETAIL_FORMAT, cartItem.getProductName(),
+ numberFormat(cartItem.getQuantity()), numberFormat(cartItem.totalPrice())))
+ .append("\n");
+ }
+ return orderDetail.toString();
+ }
+
+ /**
+ * ์ฆ์ ์ํ ๋ด์ญ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String toStringRemainingItemsForPromotion() {
+ StringBuilder remainingitems = new StringBuilder();
+ remainingitems.append(PROMOTION_HEADER).append("\n");
+
+ for (CartItem cartItem : cart.getAllItemsInCart()) {
+ if (cartItem.getFreeQuantity() > 0) {
+ remainingitems.append(String.format(PROMOTION_DETAIL_FORMAT, cartItem.getProductName(),
+ numberFormat(cartItem.getFreeQuantity())))
+ .append("\n");
+ }
+ }
+ return remainingitems.toString();
+ }
+
+ /**
+ * ์ด ๊ธ์ก ๋ด์ญ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String toStringTotal() {
+ StringBuilder total = new StringBuilder();
+ total.append(LINE_SEPARATOR).append("\n");
+
+ total.append(formatTotalItemPrice())
+ .append("\n");
+ total.append(formatEventDiscount())
+ .append("\n");
+ total.append(formatMembershipDiscount())
+ .append("\n");
+ total.append(formatFinalAmount())
+ .append("\n");
+
+ return total.toString();
+ }
+
+ /**
+ * ์ด ๊ตฌ๋งค ๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatTotalItemPrice() {
+ return String.format(TOTAL_DETAIL_FORMAT, TOTAL_PURCHASE_AMOUNT, numberFormat(cart.getTotalItemPrice()));
+ }
+
+ /**
+ * ์ด๋ฒคํธ ํ ์ธ ๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatEventDiscount() {
+ return String.format(TOTAL_DETAIL_FORMAT, EVENT_DISCOUNT, numberFormat(cart.getTotalFreeItemPrice()));
+ }
+
+ /**
+ * ๋ฉค๋ฒ์ญ ํ ์ธ ๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatMembershipDiscount() {
+ return String.format(TOTAL_DETAIL_FORMAT, MEMBERSHIP_DISCOUNT, numberFormat(membership.getMembershipPrice()));
+ }
+
+ /**
+ * ์ต์ข
๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatFinalAmount() {
+ int finalAmount = cart.getTotalItemPrice() - cart.getTotalFreeItemPrice() - membership.getMembershipPrice();
+ return String.format(TOTAL_DETAIL_FORMAT, FINAL_AMOUNT, numberFormat(finalAmount));
+ }
+
+ /**
+ * ์ซ์๋ฅผ ํ๊ตญ์ ์ซ์ ํฌ๋งท์ ๋ง๊ฒ ํ์ํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String numberFormat(int number) {
+ return NumberFormat.getInstance(Locale.KOREA).format(number);
+ }
+} | Java | ๊ฐํ ๋ฌธ์๋ ์์ํํ๋ฉด ์ด๋ ์ ๊ฐ์? |
@@ -0,0 +1,68 @@
+package store.parser;
+
+import static store.message.ErrorMessage.INVALID_DATA_FORMAT;
+
+import java.util.List;
+import store.dto.ProductDto;
+import store.dto.PromotionDto;
+
+public class FileReaderParser {
+
+ private static final String COMMA_DELIMITER = ",";
+ private static final String NULL_PROMOTION = "null";
+ private static final int EXPECTED_PRODUCT_LENGTH = 4;
+ private static final int EXPECTED_PROMOTION_LENGTH = 5;
+ private static final int HEADER_SKIP_COUNT = 1;
+
+ public List<ProductDto> parseProduct(List<String> productData) {
+ List<String> productRows = removeHeader(productData);
+ return productRows.stream()
+ .map(this::parseProductRow)
+ .toList();
+ }
+
+ public List<PromotionDto> parsePromotion(List<String> promotionData) {
+ List<String> promotionRows = removeHeader(promotionData);
+ return promotionRows.stream()
+ .map(this::parsePromotionRow)
+ .toList();
+ }
+
+ public ProductDto parseProductRow(String row) {
+ String[] split = row.split(COMMA_DELIMITER);
+ validateDataLength(split, EXPECTED_PRODUCT_LENGTH);
+ return createProductDto(split);
+ }
+
+ public PromotionDto parsePromotionRow(String row) {
+ String[] split = row.split(COMMA_DELIMITER);
+ validateDataLength(split, EXPECTED_PROMOTION_LENGTH);
+ return createPromotionDto(split);
+ }
+
+ public void validateDataLength(String[] split, int expectedLength) {
+ if (split.length != expectedLength) {
+ throw new IllegalStateException(INVALID_DATA_FORMAT.getMessage());
+ }
+ }
+
+ public ProductDto createProductDto(String[] split) {
+ return ProductDto.toProductDto(split[0], split[1], split[2], normalizePromotion(split[3]));
+ }
+
+ public PromotionDto createPromotionDto(String[] split) {
+ return new PromotionDto(split[0], split[1], split[2], split[3], split[4]);
+ }
+
+ public String normalizePromotion(String promotion) {
+ if (NULL_PROMOTION.equals(promotion)) {
+ return null;
+ }
+ return promotion;
+ }
+
+ public List<String> removeHeader(List<String> rows) {
+ return rows.stream().skip(HEADER_SKIP_COUNT).toList();
+ }
+
+} | Java | ํ์ ๊ฐ์ ๊ฒฝ์ฐ๋ ์ ํธ ํด๋์ค๋ก ๋ฐ๋ก ์์ฑํ์ง ์์ผ์ ์ด์ ๊ฐ ์์๊น์? |
@@ -0,0 +1,21 @@
+package store;
+
+import store.controller.FileReaderController;
+import store.controller.StoreController;
+import store.domain.Store;
+import store.dto.StoreDto;
+
+public class FrontController {
+ private final FileReaderController fileReaderController;
+ private final StoreController storeController;
+
+ public FrontController() {
+ this.fileReaderController = new FileReaderController();
+ this.storeController = new StoreController();
+ }
+
+ public void run() {
+ StoreDto storeDto = fileReaderController.runFileData();
+ storeController.run(storeDto);
+ }
+} | Java | frontController๋ฅผ ์ด์ฉํด์ ํ์ผ ๋จผ์ ์
๋ ฅ์ ๋ฐ์ผ์
จ๊ตฐ์ ์ข์๊ฒ ๊ฐ์์! ๐ |
@@ -0,0 +1,136 @@
+package store.domain;
+
+import static store.constants.ProductConstants.OUT_OF_STOCK;
+import static store.constants.ProductConstants.PRODUCT_DESCRIPTION_PREFIX;
+import static store.constants.ProductConstants.UNIT_QUANTITY;
+import static store.constants.ProductConstants.UNIT_WON;
+import static store.message.ErrorMessage.NOT_FOUND_PROMOTION;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+import java.util.Optional;
+import store.dto.ProductDto;
+import store.validation.ProductValidator;
+
+public class Product {
+ private String name;
+ private int price;
+ private int quantity;
+ private Optional<Promotion> promotion;
+
+ private Product() {
+ }
+
+ public Product(ProductDto productDto, Optional<Promotion> promotion) {
+ ProductValidator.validate(productDto);
+ this.name = productDto.name();
+ this.price = Integer.parseInt(productDto.price());
+ this.quantity = Integer.parseInt(productDto.quantity());
+ this.promotion = promotion;
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ ์ ์ฉ ์ฌ๋ถ ๊ฒ์ฆ
+ */
+ public boolean isEligibleForStandardPromotion(int orderedQuantity) {
+ if (promotion.isEmpty()) {
+ return false;
+ }
+ Promotion promotionInfo = promotion.get();
+ return orderedQuantity <= quantity
+ && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity;
+ }
+
+ /**
+ * ๋ณด๋์ค ์ํ์ด ์ ์ฉ ์ฌ๋ถ ๊ฒ์ฆ
+ */
+ public boolean isEligibleForBonusProduct(int orderedQuantity) {
+ if (promotion.isEmpty()) {
+ return false;
+ }
+ Promotion promotionInfo = promotion.get();
+ return getRemainingItemsForPromotion(orderedQuantity, promotionInfo) >= promotionInfo.getBuy()
+ && orderedQuantity + promotionInfo.getGet() <= quantity;
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋จ์ ์๋์ ๊ณ์ฐ
+ */
+ public int getRemainingItemsForPromotion(int orderedQuantity, Promotion promotionInfo) {
+ return orderedQuantity % promotionInfo.getTotalRequiredQuantity();
+ }
+
+ /**
+ * ๋ณด๋์ค ์ํ์ ์๋์ ๊ณ์ฐ
+ */
+ public int calculateBonusQuantity(int orderedQuantity, Promotion promotion) {
+ return (orderedQuantity / promotion.getTotalRequiredQuantity()) * promotion.getGet();
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์ ์ฉ๋ ํ, ์ค์ ์ฃผ๋ฌธ ๊ฐ๋ฅํ ์๋์ ๊ณ์ฐ
+ */
+ public int calculateQuantityAfterPromotion(int orderedQuantity) {
+ Promotion promotionInfo = getPromotionOrElseThrow();
+ int requiredQuantity = promotionInfo.getTotalRequiredQuantity();
+ return (orderedQuantity - quantity) + (quantity % requiredQuantity);
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ ๋ฐ์
+ */
+ public Promotion getPromotionOrElseThrow() {
+ return promotion.orElseThrow(() -> new IllegalArgumentException(NOT_FOUND_PROMOTION.getMessage()));
+ }
+
+ /**
+ * ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ์ฃผ์ฒด์ด๊ธฐ ๋๋ฌธ์ ์ํ ๋ฆฌ์คํธ๋ฅผ ํฌ๋งท์ ํฉ๋๋ค.
+ */
+ @Override
+ public String toString() {
+ return PRODUCT_DESCRIPTION_PREFIX
+ + name + " "
+ + getFormatKoreanLocale(price) + UNIT_WON
+ + getFormattedQuantity()
+ + getFormattedPromotion();
+ }
+
+ public String getFormattedQuantity() {
+ if (quantity < 1) {
+ return OUT_OF_STOCK;
+ }
+ return getFormatKoreanLocale(quantity) + UNIT_QUANTITY;
+ }
+
+ public String getFormatKoreanLocale(int number) {
+ return NumberFormat.getInstance(Locale.KOREA).format(number);
+ }
+
+ public String getFormattedPromotion() {
+ return promotion.map(Promotion::getName).orElse("");
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public Optional<Promotion> getPromotion() {
+ return promotion;
+ }
+
+ public boolean isPromotionalProduct() {
+ return promotion.isPresent();
+ }
+
+ public void decreaseQuantity(int orderQuantity) {
+ this.quantity -= orderQuantity;
+ }
+} | Java | ์ด๋ฆ์ ๋ณ๊ฒฝ ๊ฐ๋ฅ์ฑ์ด ์๊ธฐ๋๋ฌธ์ final๋ก ์ ์ธํด์ฃผ๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,136 @@
+package store.domain;
+
+import static store.constants.ProductConstants.OUT_OF_STOCK;
+import static store.constants.ProductConstants.PRODUCT_DESCRIPTION_PREFIX;
+import static store.constants.ProductConstants.UNIT_QUANTITY;
+import static store.constants.ProductConstants.UNIT_WON;
+import static store.message.ErrorMessage.NOT_FOUND_PROMOTION;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+import java.util.Optional;
+import store.dto.ProductDto;
+import store.validation.ProductValidator;
+
+public class Product {
+ private String name;
+ private int price;
+ private int quantity;
+ private Optional<Promotion> promotion;
+
+ private Product() {
+ }
+
+ public Product(ProductDto productDto, Optional<Promotion> promotion) {
+ ProductValidator.validate(productDto);
+ this.name = productDto.name();
+ this.price = Integer.parseInt(productDto.price());
+ this.quantity = Integer.parseInt(productDto.quantity());
+ this.promotion = promotion;
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ ์ ์ฉ ์ฌ๋ถ ๊ฒ์ฆ
+ */
+ public boolean isEligibleForStandardPromotion(int orderedQuantity) {
+ if (promotion.isEmpty()) {
+ return false;
+ }
+ Promotion promotionInfo = promotion.get();
+ return orderedQuantity <= quantity
+ && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity;
+ }
+
+ /**
+ * ๋ณด๋์ค ์ํ์ด ์ ์ฉ ์ฌ๋ถ ๊ฒ์ฆ
+ */
+ public boolean isEligibleForBonusProduct(int orderedQuantity) {
+ if (promotion.isEmpty()) {
+ return false;
+ }
+ Promotion promotionInfo = promotion.get();
+ return getRemainingItemsForPromotion(orderedQuantity, promotionInfo) >= promotionInfo.getBuy()
+ && orderedQuantity + promotionInfo.getGet() <= quantity;
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋จ์ ์๋์ ๊ณ์ฐ
+ */
+ public int getRemainingItemsForPromotion(int orderedQuantity, Promotion promotionInfo) {
+ return orderedQuantity % promotionInfo.getTotalRequiredQuantity();
+ }
+
+ /**
+ * ๋ณด๋์ค ์ํ์ ์๋์ ๊ณ์ฐ
+ */
+ public int calculateBonusQuantity(int orderedQuantity, Promotion promotion) {
+ return (orderedQuantity / promotion.getTotalRequiredQuantity()) * promotion.getGet();
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์ ์ฉ๋ ํ, ์ค์ ์ฃผ๋ฌธ ๊ฐ๋ฅํ ์๋์ ๊ณ์ฐ
+ */
+ public int calculateQuantityAfterPromotion(int orderedQuantity) {
+ Promotion promotionInfo = getPromotionOrElseThrow();
+ int requiredQuantity = promotionInfo.getTotalRequiredQuantity();
+ return (orderedQuantity - quantity) + (quantity % requiredQuantity);
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ ๋ฐ์
+ */
+ public Promotion getPromotionOrElseThrow() {
+ return promotion.orElseThrow(() -> new IllegalArgumentException(NOT_FOUND_PROMOTION.getMessage()));
+ }
+
+ /**
+ * ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ์ฃผ์ฒด์ด๊ธฐ ๋๋ฌธ์ ์ํ ๋ฆฌ์คํธ๋ฅผ ํฌ๋งท์ ํฉ๋๋ค.
+ */
+ @Override
+ public String toString() {
+ return PRODUCT_DESCRIPTION_PREFIX
+ + name + " "
+ + getFormatKoreanLocale(price) + UNIT_WON
+ + getFormattedQuantity()
+ + getFormattedPromotion();
+ }
+
+ public String getFormattedQuantity() {
+ if (quantity < 1) {
+ return OUT_OF_STOCK;
+ }
+ return getFormatKoreanLocale(quantity) + UNIT_QUANTITY;
+ }
+
+ public String getFormatKoreanLocale(int number) {
+ return NumberFormat.getInstance(Locale.KOREA).format(number);
+ }
+
+ public String getFormattedPromotion() {
+ return promotion.map(Promotion::getName).orElse("");
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public Optional<Promotion> getPromotion() {
+ return promotion;
+ }
+
+ public boolean isPromotionalProduct() {
+ return promotion.isPresent();
+ }
+
+ public void decreaseQuantity(int orderQuantity) {
+ this.quantity -= orderQuantity;
+ }
+} | Java | Optional ์ข๋ค์ ๐ |
@@ -0,0 +1,136 @@
+package store.domain;
+
+import static store.constants.ProductConstants.OUT_OF_STOCK;
+import static store.constants.ProductConstants.PRODUCT_DESCRIPTION_PREFIX;
+import static store.constants.ProductConstants.UNIT_QUANTITY;
+import static store.constants.ProductConstants.UNIT_WON;
+import static store.message.ErrorMessage.NOT_FOUND_PROMOTION;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+import java.util.Optional;
+import store.dto.ProductDto;
+import store.validation.ProductValidator;
+
+public class Product {
+ private String name;
+ private int price;
+ private int quantity;
+ private Optional<Promotion> promotion;
+
+ private Product() {
+ }
+
+ public Product(ProductDto productDto, Optional<Promotion> promotion) {
+ ProductValidator.validate(productDto);
+ this.name = productDto.name();
+ this.price = Integer.parseInt(productDto.price());
+ this.quantity = Integer.parseInt(productDto.quantity());
+ this.promotion = promotion;
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ ์ ์ฉ ์ฌ๋ถ ๊ฒ์ฆ
+ */
+ public boolean isEligibleForStandardPromotion(int orderedQuantity) {
+ if (promotion.isEmpty()) {
+ return false;
+ }
+ Promotion promotionInfo = promotion.get();
+ return orderedQuantity <= quantity
+ && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity;
+ }
+
+ /**
+ * ๋ณด๋์ค ์ํ์ด ์ ์ฉ ์ฌ๋ถ ๊ฒ์ฆ
+ */
+ public boolean isEligibleForBonusProduct(int orderedQuantity) {
+ if (promotion.isEmpty()) {
+ return false;
+ }
+ Promotion promotionInfo = promotion.get();
+ return getRemainingItemsForPromotion(orderedQuantity, promotionInfo) >= promotionInfo.getBuy()
+ && orderedQuantity + promotionInfo.getGet() <= quantity;
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋จ์ ์๋์ ๊ณ์ฐ
+ */
+ public int getRemainingItemsForPromotion(int orderedQuantity, Promotion promotionInfo) {
+ return orderedQuantity % promotionInfo.getTotalRequiredQuantity();
+ }
+
+ /**
+ * ๋ณด๋์ค ์ํ์ ์๋์ ๊ณ์ฐ
+ */
+ public int calculateBonusQuantity(int orderedQuantity, Promotion promotion) {
+ return (orderedQuantity / promotion.getTotalRequiredQuantity()) * promotion.getGet();
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์ ์ฉ๋ ํ, ์ค์ ์ฃผ๋ฌธ ๊ฐ๋ฅํ ์๋์ ๊ณ์ฐ
+ */
+ public int calculateQuantityAfterPromotion(int orderedQuantity) {
+ Promotion promotionInfo = getPromotionOrElseThrow();
+ int requiredQuantity = promotionInfo.getTotalRequiredQuantity();
+ return (orderedQuantity - quantity) + (quantity % requiredQuantity);
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ ๋ฐ์
+ */
+ public Promotion getPromotionOrElseThrow() {
+ return promotion.orElseThrow(() -> new IllegalArgumentException(NOT_FOUND_PROMOTION.getMessage()));
+ }
+
+ /**
+ * ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ์ฃผ์ฒด์ด๊ธฐ ๋๋ฌธ์ ์ํ ๋ฆฌ์คํธ๋ฅผ ํฌ๋งท์ ํฉ๋๋ค.
+ */
+ @Override
+ public String toString() {
+ return PRODUCT_DESCRIPTION_PREFIX
+ + name + " "
+ + getFormatKoreanLocale(price) + UNIT_WON
+ + getFormattedQuantity()
+ + getFormattedPromotion();
+ }
+
+ public String getFormattedQuantity() {
+ if (quantity < 1) {
+ return OUT_OF_STOCK;
+ }
+ return getFormatKoreanLocale(quantity) + UNIT_QUANTITY;
+ }
+
+ public String getFormatKoreanLocale(int number) {
+ return NumberFormat.getInstance(Locale.KOREA).format(number);
+ }
+
+ public String getFormattedPromotion() {
+ return promotion.map(Promotion::getName).orElse("");
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public Optional<Promotion> getPromotion() {
+ return promotion;
+ }
+
+ public boolean isPromotionalProduct() {
+ return promotion.isPresent();
+ }
+
+ public void decreaseQuantity(int orderQuantity) {
+ this.quantity -= orderQuantity;
+ }
+} | Java | Model์์ DTO๋ฅผ ์์กดํ๋ฉด View์ ์์กดํ๋๊ฒ๊ณผ ๋น์ทํ๋ค๊ณ ์๊ฐํฉ๋๋ค.
DTO์ ๋ณ๊ฒฝ์ Model์ ์ฝ๋๊ฐ ๋ณ๊ฒฝ๋์ด์ผํด์!
Service์์ DTO๊ด๋ จ๋ ๋ก์ง์ ์ ์ฒ๋ฆฌ ํด์ฃผ๋ฉด ์ข์ ๊ฒ ๊ฐ์์๐ |
@@ -0,0 +1,136 @@
+package store.domain;
+
+import static store.constants.ProductConstants.OUT_OF_STOCK;
+import static store.constants.ProductConstants.PRODUCT_DESCRIPTION_PREFIX;
+import static store.constants.ProductConstants.UNIT_QUANTITY;
+import static store.constants.ProductConstants.UNIT_WON;
+import static store.message.ErrorMessage.NOT_FOUND_PROMOTION;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+import java.util.Optional;
+import store.dto.ProductDto;
+import store.validation.ProductValidator;
+
+public class Product {
+ private String name;
+ private int price;
+ private int quantity;
+ private Optional<Promotion> promotion;
+
+ private Product() {
+ }
+
+ public Product(ProductDto productDto, Optional<Promotion> promotion) {
+ ProductValidator.validate(productDto);
+ this.name = productDto.name();
+ this.price = Integer.parseInt(productDto.price());
+ this.quantity = Integer.parseInt(productDto.quantity());
+ this.promotion = promotion;
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ ์ ์ฉ ์ฌ๋ถ ๊ฒ์ฆ
+ */
+ public boolean isEligibleForStandardPromotion(int orderedQuantity) {
+ if (promotion.isEmpty()) {
+ return false;
+ }
+ Promotion promotionInfo = promotion.get();
+ return orderedQuantity <= quantity
+ && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity;
+ }
+
+ /**
+ * ๋ณด๋์ค ์ํ์ด ์ ์ฉ ์ฌ๋ถ ๊ฒ์ฆ
+ */
+ public boolean isEligibleForBonusProduct(int orderedQuantity) {
+ if (promotion.isEmpty()) {
+ return false;
+ }
+ Promotion promotionInfo = promotion.get();
+ return getRemainingItemsForPromotion(orderedQuantity, promotionInfo) >= promotionInfo.getBuy()
+ && orderedQuantity + promotionInfo.getGet() <= quantity;
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋จ์ ์๋์ ๊ณ์ฐ
+ */
+ public int getRemainingItemsForPromotion(int orderedQuantity, Promotion promotionInfo) {
+ return orderedQuantity % promotionInfo.getTotalRequiredQuantity();
+ }
+
+ /**
+ * ๋ณด๋์ค ์ํ์ ์๋์ ๊ณ์ฐ
+ */
+ public int calculateBonusQuantity(int orderedQuantity, Promotion promotion) {
+ return (orderedQuantity / promotion.getTotalRequiredQuantity()) * promotion.getGet();
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์ ์ฉ๋ ํ, ์ค์ ์ฃผ๋ฌธ ๊ฐ๋ฅํ ์๋์ ๊ณ์ฐ
+ */
+ public int calculateQuantityAfterPromotion(int orderedQuantity) {
+ Promotion promotionInfo = getPromotionOrElseThrow();
+ int requiredQuantity = promotionInfo.getTotalRequiredQuantity();
+ return (orderedQuantity - quantity) + (quantity % requiredQuantity);
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ ๋ฐ์
+ */
+ public Promotion getPromotionOrElseThrow() {
+ return promotion.orElseThrow(() -> new IllegalArgumentException(NOT_FOUND_PROMOTION.getMessage()));
+ }
+
+ /**
+ * ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ์ฃผ์ฒด์ด๊ธฐ ๋๋ฌธ์ ์ํ ๋ฆฌ์คํธ๋ฅผ ํฌ๋งท์ ํฉ๋๋ค.
+ */
+ @Override
+ public String toString() {
+ return PRODUCT_DESCRIPTION_PREFIX
+ + name + " "
+ + getFormatKoreanLocale(price) + UNIT_WON
+ + getFormattedQuantity()
+ + getFormattedPromotion();
+ }
+
+ public String getFormattedQuantity() {
+ if (quantity < 1) {
+ return OUT_OF_STOCK;
+ }
+ return getFormatKoreanLocale(quantity) + UNIT_QUANTITY;
+ }
+
+ public String getFormatKoreanLocale(int number) {
+ return NumberFormat.getInstance(Locale.KOREA).format(number);
+ }
+
+ public String getFormattedPromotion() {
+ return promotion.map(Promotion::getName).orElse("");
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public Optional<Promotion> getPromotion() {
+ return promotion;
+ }
+
+ public boolean isPromotionalProduct() {
+ return promotion.isPresent();
+ }
+
+ public void decreaseQuantity(int orderQuantity) {
+ this.quantity -= orderQuantity;
+ }
+} | Java | Optional์ ๊ธฐ๋ฅ์ ํ์ฉํด๋ณด๋ฉด ์ด๋จ๊น์!?
```suggestion
public boolean isEligibleForStandardPromotion(int orderedQuantity) {
return promotion.map(promotionInfo ->
orderedQuantity <= quantity &&
getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity
).orElse(false);
}
``` |
@@ -0,0 +1,136 @@
+package store.domain;
+
+import static store.constants.ProductConstants.OUT_OF_STOCK;
+import static store.constants.ProductConstants.PRODUCT_DESCRIPTION_PREFIX;
+import static store.constants.ProductConstants.UNIT_QUANTITY;
+import static store.constants.ProductConstants.UNIT_WON;
+import static store.message.ErrorMessage.NOT_FOUND_PROMOTION;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+import java.util.Optional;
+import store.dto.ProductDto;
+import store.validation.ProductValidator;
+
+public class Product {
+ private String name;
+ private int price;
+ private int quantity;
+ private Optional<Promotion> promotion;
+
+ private Product() {
+ }
+
+ public Product(ProductDto productDto, Optional<Promotion> promotion) {
+ ProductValidator.validate(productDto);
+ this.name = productDto.name();
+ this.price = Integer.parseInt(productDto.price());
+ this.quantity = Integer.parseInt(productDto.quantity());
+ this.promotion = promotion;
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ ์ ์ฉ ์ฌ๋ถ ๊ฒ์ฆ
+ */
+ public boolean isEligibleForStandardPromotion(int orderedQuantity) {
+ if (promotion.isEmpty()) {
+ return false;
+ }
+ Promotion promotionInfo = promotion.get();
+ return orderedQuantity <= quantity
+ && getRemainingItemsForPromotion(orderedQuantity, promotionInfo) + promotionInfo.getGet() <= quantity;
+ }
+
+ /**
+ * ๋ณด๋์ค ์ํ์ด ์ ์ฉ ์ฌ๋ถ ๊ฒ์ฆ
+ */
+ public boolean isEligibleForBonusProduct(int orderedQuantity) {
+ if (promotion.isEmpty()) {
+ return false;
+ }
+ Promotion promotionInfo = promotion.get();
+ return getRemainingItemsForPromotion(orderedQuantity, promotionInfo) >= promotionInfo.getBuy()
+ && orderedQuantity + promotionInfo.getGet() <= quantity;
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋จ์ ์๋์ ๊ณ์ฐ
+ */
+ public int getRemainingItemsForPromotion(int orderedQuantity, Promotion promotionInfo) {
+ return orderedQuantity % promotionInfo.getTotalRequiredQuantity();
+ }
+
+ /**
+ * ๋ณด๋์ค ์ํ์ ์๋์ ๊ณ์ฐ
+ */
+ public int calculateBonusQuantity(int orderedQuantity, Promotion promotion) {
+ return (orderedQuantity / promotion.getTotalRequiredQuantity()) * promotion.getGet();
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์ ์ฉ๋ ํ, ์ค์ ์ฃผ๋ฌธ ๊ฐ๋ฅํ ์๋์ ๊ณ์ฐ
+ */
+ public int calculateQuantityAfterPromotion(int orderedQuantity) {
+ Promotion promotionInfo = getPromotionOrElseThrow();
+ int requiredQuantity = promotionInfo.getTotalRequiredQuantity();
+ return (orderedQuantity - quantity) + (quantity % requiredQuantity);
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ ๋ฐ์
+ */
+ public Promotion getPromotionOrElseThrow() {
+ return promotion.orElseThrow(() -> new IllegalArgumentException(NOT_FOUND_PROMOTION.getMessage()));
+ }
+
+ /**
+ * ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ์ฃผ์ฒด์ด๊ธฐ ๋๋ฌธ์ ์ํ ๋ฆฌ์คํธ๋ฅผ ํฌ๋งท์ ํฉ๋๋ค.
+ */
+ @Override
+ public String toString() {
+ return PRODUCT_DESCRIPTION_PREFIX
+ + name + " "
+ + getFormatKoreanLocale(price) + UNIT_WON
+ + getFormattedQuantity()
+ + getFormattedPromotion();
+ }
+
+ public String getFormattedQuantity() {
+ if (quantity < 1) {
+ return OUT_OF_STOCK;
+ }
+ return getFormatKoreanLocale(quantity) + UNIT_QUANTITY;
+ }
+
+ public String getFormatKoreanLocale(int number) {
+ return NumberFormat.getInstance(Locale.KOREA).format(number);
+ }
+
+ public String getFormattedPromotion() {
+ return promotion.map(Promotion::getName).orElse("");
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public Optional<Promotion> getPromotion() {
+ return promotion;
+ }
+
+ public boolean isPromotionalProduct() {
+ return promotion.isPresent();
+ }
+
+ public void decreaseQuantity(int orderQuantity) {
+ this.quantity -= orderQuantity;
+ }
+} | Java | ๊ผผ๊ผผํจ์ด ๋ณด์ด๋ค์ ๐ |
@@ -0,0 +1,152 @@
+package store.domain;
+
+import static store.constants.ReceiptConstants.EVENT_DISCOUNT;
+import static store.constants.ReceiptConstants.FINAL_AMOUNT;
+import static store.constants.ReceiptConstants.LINE_SEPARATOR;
+import static store.constants.ReceiptConstants.MEMBERSHIP_DISCOUNT;
+import static store.constants.ReceiptConstants.ORDER_DETAIL_FORMAT;
+import static store.constants.ReceiptConstants.PRICE;
+import static store.constants.ReceiptConstants.PRODUCT_HEADER;
+import static store.constants.ReceiptConstants.PRODUCT_NAME;
+import static store.constants.ReceiptConstants.PROMOTION_DETAIL_FORMAT;
+import static store.constants.ReceiptConstants.PROMOTION_HEADER;
+import static store.constants.ReceiptConstants.QUANTITY;
+import static store.constants.ReceiptConstants.RECEIPT_HEADER;
+import static store.constants.ReceiptConstants.TOTAL_DETAIL_FORMAT;
+import static store.constants.ReceiptConstants.TOTAL_PURCHASE_AMOUNT;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+public class Receipt {
+ private final Store store;
+ private final Cart cart;
+ private final Membership membership;
+
+ public Receipt(Store store, Cart cart, Membership membership) {
+ this.store = store;
+ this.cart = cart;
+ this.membership = membership;
+ }
+
+ @Override
+ public String toString() {
+ return buildReceiptContent();
+ }
+
+ public boolean hasFreeItems() {
+ return cart.getTotalFreeItemQuantity() > 0;
+ }
+
+ /**
+ * ์ฅ๋ฐ๊ตฌ๋ ๋ฐ ๋ฉค๋ฒ์ญ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์์์ฆ ๋ด์ฉ์ ๋น๋ํ๋ ๋ฉ์๋์
๋๋ค.
+ * <p>
+ * ์ถ๊ฐ๋ก ์ฆ์ ๋ ์ํ์ด ์๋ค๋ฉด ์ํ๋ด์ญ ์ ๋ณด๋ ์ถ๋ ฅํ์ง ์์ต๋๋ค.
+ */
+ public String buildReceiptContent() {
+ StringBuilder result = new StringBuilder();
+ result.append(getReceiptHeader());
+ result.append(toStringOrderDetail());
+ if (hasFreeItems()) {
+ result.append(toStringRemainingItemsForPromotion());
+ }
+ result.append(toStringTotal());
+ return result.toString();
+ }
+
+ /**
+ * ์์์ฆ ํค๋๋ฅผ ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String getReceiptHeader() {
+ StringBuilder header = new StringBuilder();
+ header.append(RECEIPT_HEADER).append("\n");
+ header.append(String.format(PRODUCT_HEADER, PRODUCT_NAME, QUANTITY, PRICE)).append("\n");
+ return header.toString();
+ }
+
+ /**
+ * ์ฃผ๋ฌธ ๋ด์ญ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String toStringOrderDetail() {
+ StringBuilder orderDetail = new StringBuilder();
+
+ for (CartItem cartItem : cart.getAllItemsInCart()) {
+ orderDetail.append(String.format(ORDER_DETAIL_FORMAT, cartItem.getProductName(),
+ numberFormat(cartItem.getQuantity()), numberFormat(cartItem.totalPrice())))
+ .append("\n");
+ }
+ return orderDetail.toString();
+ }
+
+ /**
+ * ์ฆ์ ์ํ ๋ด์ญ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String toStringRemainingItemsForPromotion() {
+ StringBuilder remainingitems = new StringBuilder();
+ remainingitems.append(PROMOTION_HEADER).append("\n");
+
+ for (CartItem cartItem : cart.getAllItemsInCart()) {
+ if (cartItem.getFreeQuantity() > 0) {
+ remainingitems.append(String.format(PROMOTION_DETAIL_FORMAT, cartItem.getProductName(),
+ numberFormat(cartItem.getFreeQuantity())))
+ .append("\n");
+ }
+ }
+ return remainingitems.toString();
+ }
+
+ /**
+ * ์ด ๊ธ์ก ๋ด์ญ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String toStringTotal() {
+ StringBuilder total = new StringBuilder();
+ total.append(LINE_SEPARATOR).append("\n");
+
+ total.append(formatTotalItemPrice())
+ .append("\n");
+ total.append(formatEventDiscount())
+ .append("\n");
+ total.append(formatMembershipDiscount())
+ .append("\n");
+ total.append(formatFinalAmount())
+ .append("\n");
+
+ return total.toString();
+ }
+
+ /**
+ * ์ด ๊ตฌ๋งค ๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatTotalItemPrice() {
+ return String.format(TOTAL_DETAIL_FORMAT, TOTAL_PURCHASE_AMOUNT, numberFormat(cart.getTotalItemPrice()));
+ }
+
+ /**
+ * ์ด๋ฒคํธ ํ ์ธ ๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatEventDiscount() {
+ return String.format(TOTAL_DETAIL_FORMAT, EVENT_DISCOUNT, numberFormat(cart.getTotalFreeItemPrice()));
+ }
+
+ /**
+ * ๋ฉค๋ฒ์ญ ํ ์ธ ๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatMembershipDiscount() {
+ return String.format(TOTAL_DETAIL_FORMAT, MEMBERSHIP_DISCOUNT, numberFormat(membership.getMembershipPrice()));
+ }
+
+ /**
+ * ์ต์ข
๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatFinalAmount() {
+ int finalAmount = cart.getTotalItemPrice() - cart.getTotalFreeItemPrice() - membership.getMembershipPrice();
+ return String.format(TOTAL_DETAIL_FORMAT, FINAL_AMOUNT, numberFormat(finalAmount));
+ }
+
+ /**
+ * ์ซ์๋ฅผ ํ๊ตญ์ ์ซ์ ํฌ๋งท์ ๋ง๊ฒ ํ์ํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String numberFormat(int number) {
+ return NumberFormat.getInstance(Locale.KOREA).format(number);
+ }
+} | Java | Model๋ ์ด์ด์์ View์ ๋ด์ฉ์ ๊ฐ๊ณตํ๊ธฐ ๋ณด๋จ, Controller or Service์์ ์งํํด์ฃผ๋๊ฑด ์ด๋จ๊น์?
์ง๊ธ์ View๊ฐ ๋ณ๊ฒฝ๋๋ฉด Model๋ ๋ณ๊ฒฝ๋์ด์ผ ํ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,152 @@
+package store.domain;
+
+import static store.constants.ReceiptConstants.EVENT_DISCOUNT;
+import static store.constants.ReceiptConstants.FINAL_AMOUNT;
+import static store.constants.ReceiptConstants.LINE_SEPARATOR;
+import static store.constants.ReceiptConstants.MEMBERSHIP_DISCOUNT;
+import static store.constants.ReceiptConstants.ORDER_DETAIL_FORMAT;
+import static store.constants.ReceiptConstants.PRICE;
+import static store.constants.ReceiptConstants.PRODUCT_HEADER;
+import static store.constants.ReceiptConstants.PRODUCT_NAME;
+import static store.constants.ReceiptConstants.PROMOTION_DETAIL_FORMAT;
+import static store.constants.ReceiptConstants.PROMOTION_HEADER;
+import static store.constants.ReceiptConstants.QUANTITY;
+import static store.constants.ReceiptConstants.RECEIPT_HEADER;
+import static store.constants.ReceiptConstants.TOTAL_DETAIL_FORMAT;
+import static store.constants.ReceiptConstants.TOTAL_PURCHASE_AMOUNT;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+public class Receipt {
+ private final Store store;
+ private final Cart cart;
+ private final Membership membership;
+
+ public Receipt(Store store, Cart cart, Membership membership) {
+ this.store = store;
+ this.cart = cart;
+ this.membership = membership;
+ }
+
+ @Override
+ public String toString() {
+ return buildReceiptContent();
+ }
+
+ public boolean hasFreeItems() {
+ return cart.getTotalFreeItemQuantity() > 0;
+ }
+
+ /**
+ * ์ฅ๋ฐ๊ตฌ๋ ๋ฐ ๋ฉค๋ฒ์ญ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์์์ฆ ๋ด์ฉ์ ๋น๋ํ๋ ๋ฉ์๋์
๋๋ค.
+ * <p>
+ * ์ถ๊ฐ๋ก ์ฆ์ ๋ ์ํ์ด ์๋ค๋ฉด ์ํ๋ด์ญ ์ ๋ณด๋ ์ถ๋ ฅํ์ง ์์ต๋๋ค.
+ */
+ public String buildReceiptContent() {
+ StringBuilder result = new StringBuilder();
+ result.append(getReceiptHeader());
+ result.append(toStringOrderDetail());
+ if (hasFreeItems()) {
+ result.append(toStringRemainingItemsForPromotion());
+ }
+ result.append(toStringTotal());
+ return result.toString();
+ }
+
+ /**
+ * ์์์ฆ ํค๋๋ฅผ ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String getReceiptHeader() {
+ StringBuilder header = new StringBuilder();
+ header.append(RECEIPT_HEADER).append("\n");
+ header.append(String.format(PRODUCT_HEADER, PRODUCT_NAME, QUANTITY, PRICE)).append("\n");
+ return header.toString();
+ }
+
+ /**
+ * ์ฃผ๋ฌธ ๋ด์ญ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String toStringOrderDetail() {
+ StringBuilder orderDetail = new StringBuilder();
+
+ for (CartItem cartItem : cart.getAllItemsInCart()) {
+ orderDetail.append(String.format(ORDER_DETAIL_FORMAT, cartItem.getProductName(),
+ numberFormat(cartItem.getQuantity()), numberFormat(cartItem.totalPrice())))
+ .append("\n");
+ }
+ return orderDetail.toString();
+ }
+
+ /**
+ * ์ฆ์ ์ํ ๋ด์ญ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String toStringRemainingItemsForPromotion() {
+ StringBuilder remainingitems = new StringBuilder();
+ remainingitems.append(PROMOTION_HEADER).append("\n");
+
+ for (CartItem cartItem : cart.getAllItemsInCart()) {
+ if (cartItem.getFreeQuantity() > 0) {
+ remainingitems.append(String.format(PROMOTION_DETAIL_FORMAT, cartItem.getProductName(),
+ numberFormat(cartItem.getFreeQuantity())))
+ .append("\n");
+ }
+ }
+ return remainingitems.toString();
+ }
+
+ /**
+ * ์ด ๊ธ์ก ๋ด์ญ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String toStringTotal() {
+ StringBuilder total = new StringBuilder();
+ total.append(LINE_SEPARATOR).append("\n");
+
+ total.append(formatTotalItemPrice())
+ .append("\n");
+ total.append(formatEventDiscount())
+ .append("\n");
+ total.append(formatMembershipDiscount())
+ .append("\n");
+ total.append(formatFinalAmount())
+ .append("\n");
+
+ return total.toString();
+ }
+
+ /**
+ * ์ด ๊ตฌ๋งค ๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatTotalItemPrice() {
+ return String.format(TOTAL_DETAIL_FORMAT, TOTAL_PURCHASE_AMOUNT, numberFormat(cart.getTotalItemPrice()));
+ }
+
+ /**
+ * ์ด๋ฒคํธ ํ ์ธ ๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatEventDiscount() {
+ return String.format(TOTAL_DETAIL_FORMAT, EVENT_DISCOUNT, numberFormat(cart.getTotalFreeItemPrice()));
+ }
+
+ /**
+ * ๋ฉค๋ฒ์ญ ํ ์ธ ๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatMembershipDiscount() {
+ return String.format(TOTAL_DETAIL_FORMAT, MEMBERSHIP_DISCOUNT, numberFormat(membership.getMembershipPrice()));
+ }
+
+ /**
+ * ์ต์ข
๊ธ์ก์ ํฌ๋งทํ
ํ์ฌ ๋ฐํ
+ */
+ public String formatFinalAmount() {
+ int finalAmount = cart.getTotalItemPrice() - cart.getTotalFreeItemPrice() - membership.getMembershipPrice();
+ return String.format(TOTAL_DETAIL_FORMAT, FINAL_AMOUNT, numberFormat(finalAmount));
+ }
+
+ /**
+ * ์ซ์๋ฅผ ํ๊ตญ์ ์ซ์ ํฌ๋งท์ ๋ง๊ฒ ํ์ํํ๋ ๋ฉ์๋์
๋๋ค.
+ */
+ public String numberFormat(int number) {
+ return NumberFormat.getInstance(Locale.KOREA).format(number);
+ }
+} | Java | ํจ์ํ ์ธํฐํ์ด์ค๋ depth๊ฐ ์๋ค๊ณ ์๊ฐํฉ๋๋ค!
๋ชจ๋ํ๋ฅผ ํด๋ณด๋๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,68 @@
+package store.parser;
+
+import static store.message.ErrorMessage.INVALID_DATA_FORMAT;
+
+import java.util.List;
+import store.dto.ProductDto;
+import store.dto.PromotionDto;
+
+public class FileReaderParser {
+
+ private static final String COMMA_DELIMITER = ",";
+ private static final String NULL_PROMOTION = "null";
+ private static final int EXPECTED_PRODUCT_LENGTH = 4;
+ private static final int EXPECTED_PROMOTION_LENGTH = 5;
+ private static final int HEADER_SKIP_COUNT = 1;
+
+ public List<ProductDto> parseProduct(List<String> productData) {
+ List<String> productRows = removeHeader(productData);
+ return productRows.stream()
+ .map(this::parseProductRow)
+ .toList();
+ }
+
+ public List<PromotionDto> parsePromotion(List<String> promotionData) {
+ List<String> promotionRows = removeHeader(promotionData);
+ return promotionRows.stream()
+ .map(this::parsePromotionRow)
+ .toList();
+ }
+
+ public ProductDto parseProductRow(String row) {
+ String[] split = row.split(COMMA_DELIMITER);
+ validateDataLength(split, EXPECTED_PRODUCT_LENGTH);
+ return createProductDto(split);
+ }
+
+ public PromotionDto parsePromotionRow(String row) {
+ String[] split = row.split(COMMA_DELIMITER);
+ validateDataLength(split, EXPECTED_PROMOTION_LENGTH);
+ return createPromotionDto(split);
+ }
+
+ public void validateDataLength(String[] split, int expectedLength) {
+ if (split.length != expectedLength) {
+ throw new IllegalStateException(INVALID_DATA_FORMAT.getMessage());
+ }
+ }
+
+ public ProductDto createProductDto(String[] split) {
+ return ProductDto.toProductDto(split[0], split[1], split[2], normalizePromotion(split[3]));
+ }
+
+ public PromotionDto createPromotionDto(String[] split) {
+ return new PromotionDto(split[0], split[1], split[2], split[3], split[4]);
+ }
+
+ public String normalizePromotion(String promotion) {
+ if (NULL_PROMOTION.equals(promotion)) {
+ return null;
+ }
+ return promotion;
+ }
+
+ public List<String> removeHeader(List<String> rows) {
+ return rows.stream().skip(HEADER_SKIP_COUNT).toList();
+ }
+
+} | Java | ๋งค์ง ๋๋ฒ๋ฅผ ์์ํ ํด์ฃผ๋ฉด ์ข์๊ฒ๊ฐ๋ค์๐ |
@@ -0,0 +1,153 @@
+package store.parser;
+
+import static store.message.ErrorMessage.INVALID_INPUT_FORMAT_ERROR;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import store.dto.CartItemDto;
+
+public class InputParser {
+ private static final String ITEM_SEPARATOR_REGEX = "],\\[";
+ private static final char OPEN_BRACKET = '[';
+ private static final char CLOSE_BRACKET = ']';
+ private static final String OPEN_BRACKET_STR = "[";
+ private static final String CLOSE_BRACKET_STR = "]";
+ private static final String NESTED_BRACKET_ERROR_REGEX = "\\[\\[";
+ private static final String NESTED_BRACKET_CLOSE_ERROR_REGEX = "]]";
+ private static final String ITEM_FORMAT_REGEX = "[^\\-]+-\\d+";
+ private static final String QUANTITY_SEPARATOR = "-";
+ private static final int MIN_ORDER_ITEMS_INPUT_LENGTH = 5;
+
+ private static final char YES = 'Y';
+ private static final char NO = 'N';
+ private static final int YES_OR_NO_INPUT_LENGTH = 1;
+
+ public List<CartItemDto> parseOrderItems(String input) {
+ validateOrderItems(input);
+ String[] items = splitItems(input);
+ List<CartItemDto> cartItemDtos = new ArrayList<>();
+ for (String item : items) {
+ cartItemDtos.add(convertToOrderItemDtos(item));
+ }
+ validateDuplicateItemName(cartItemDtos);
+ return cartItemDtos;
+ }
+
+ public boolean parseYesOrNo(String input) {
+ validateYesOrNo(input);
+ return input.charAt(YES_OR_NO_INPUT_LENGTH - 1) == YES;
+ }
+
+ public void validateOrderItems(String input) {
+ validateInputLength(input);
+ validateBrackets(input);
+ }
+
+ public void validateYesOrNo(String input) {
+ validateEmpty(input);
+ validateYNLength(input);
+ validateUppercase(input);
+ validateYN(input);
+ }
+
+ private void validateUppercase(String input) {
+ if (!Character.isUpperCase(input.charAt(0))) {
+ exception();
+ }
+ }
+
+ private void validateDuplicateItemName(List<CartItemDto> cartItemDtos) {
+ Set<String> productNames = new HashSet<>();
+ for (CartItemDto cartItemDto : cartItemDtos) {
+ if (!productNames.add(cartItemDto.productName())) {
+ exception();
+ }
+ }
+ }
+
+ private void validateYN(String input) {
+ char YN = input.charAt(YES_OR_NO_INPUT_LENGTH - 1);
+ if (YN == YES || YN == NO) {
+ return;
+ }
+ exception();
+ }
+
+ private String[] splitItems(String input) {
+ String cleanInput = input.substring(1, input.length() - 1);
+ return cleanInput.split(ITEM_SEPARATOR_REGEX);
+ }
+
+ private void validateInputLength(String input) {
+ if (input.length() < MIN_ORDER_ITEMS_INPUT_LENGTH) {
+ exception();
+ }
+ }
+
+ private void validateEmpty(String input) {
+ if (input == null || input.isEmpty()) {
+ exception();
+ }
+ }
+
+ private void validateYNLength(String input) {
+ if (input.length() != YES_OR_NO_INPUT_LENGTH) {
+ exception();
+ }
+ }
+
+ private void validateBrackets(String input) {
+ checkBracketsBalance(input);
+ checkNestedBrackets(input);
+ checkBracketStructure(input);
+ }
+
+ private void checkBracketsBalance(String input) {
+ int openBrackets = countOpenBrackets(input);
+ if (openBrackets != 0) {
+ exception();
+ }
+ }
+
+ private int countOpenBrackets(String input) {
+ int openBrackets = 0;
+ for (char c : input.toCharArray()) {
+ if (c == OPEN_BRACKET) {
+ openBrackets++;
+ }
+ if (c == CLOSE_BRACKET) {
+ openBrackets--;
+ }
+ }
+ return openBrackets;
+ }
+
+
+ private void checkNestedBrackets(String input) {
+ if (input.contains(NESTED_BRACKET_ERROR_REGEX) ||
+ input.contains(NESTED_BRACKET_CLOSE_ERROR_REGEX)) {
+ exception();
+ }
+ }
+
+ private void checkBracketStructure(String input) {
+ if (!(input.startsWith(OPEN_BRACKET_STR) && input.endsWith(
+ CLOSE_BRACKET_STR))) {
+ exception();
+ }
+ }
+
+ private CartItemDto convertToOrderItemDtos(String item) {
+ if (!item.matches(ITEM_FORMAT_REGEX)) {
+ exception();
+ }
+ String[] itemParts = item.split(QUANTITY_SEPARATOR);
+ return new CartItemDto(itemParts[0], Integer.parseInt(itemParts[1]));
+ }
+
+ private void exception() {
+ throw new IllegalArgumentException(INVALID_INPUT_FORMAT_ERROR.getMessage());
+ }
+} | Java | confirm๊ณผ ๊ฐ์ ๋ณ์๋ช
์ ์ด๋จ๊น์? |
@@ -0,0 +1,128 @@
+package store.parser;
+
+import static store.constants.PromotionConstants.NO_PROMOTION_SUFFIX;
+import static store.constants.PromotionConstants.PROMOTION_SUFFIX;
+import static store.message.ErrorMessage.EMPTY_DATA;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+import store.domain.Product;
+import store.domain.Promotion;
+import store.dto.ProductDto;
+import store.util.PromotionUtil;
+
+public class ProductParser {
+
+ private final PromotionUtil promotionUtil;
+
+ public ProductParser() {
+ this.promotionUtil = new PromotionUtil();
+ }
+
+ public Map<String, Product> parse(List<ProductDto> productDtos, List<Promotion> availablePromotions) {
+ validateProductDtos(productDtos);
+ return groupAndConvertProductDtos(productDtos, availablePromotions);
+ }
+
+ private Map<String, Product> groupAndConvertProductDtos(List<ProductDto> productDtos,
+ List<Promotion> availablePromotions) {
+ Map<String, List<ProductDto>> groupedProductDtos = groupProductDtosByProductName(productDtos);
+ applyDefaultPromotionForSingleProduct(groupedProductDtos);
+ return convertGroupedProductDtosToProducts(groupedProductDtos, availablePromotions);
+ }
+
+ /**
+ * ์ฃผ์ด์ง ๊ทธ๋ฃนํ๋ ์ ํ DTO ๋ชฉ๋ก์ ๊ธฐ๋ฐ์ผ๋ก, ํ๋ก๋ชจ์
์ ๊ณ ๋ คํ์ฌ ์ ํ ๋งต์ ์์ฑํฉ๋๋ค.
+ */
+ private Map<String, Product> convertGroupedProductDtosToProducts(Map<String, List<ProductDto>> groupedProductDtos,
+ List<Promotion> availablePromotions) {
+ Map<String, Product> productMap = new LinkedHashMap<>();
+ for (Entry<String, List<ProductDto>> entry : groupedProductDtos.entrySet()) {
+ for (ProductDto productDto : entry.getValue()) {
+ if (addProductWithoutPromotion(availablePromotions, entry, productDto, productMap)) {
+ continue;
+ }
+ addProductWithPromotion(availablePromotions, entry, productDto, productMap);
+ }
+ }
+ return productMap;
+ }
+
+ /**
+ * ์ฃผ์ด์ง ํ๋ก๋ชจ์
์ ์ ์ฉํ์ฌ ์ ํ์ ์์ฑํ ํ, ์ ํ ๋งต์ ์ถ๊ฐํฉ๋๋ค.
+ */
+ private void addProductWithPromotion(List<Promotion> availablePromotions, Entry<String, List<ProductDto>> entry,
+ ProductDto productDto, Map<String, Product> productMap) {
+ productMap.put(entry.getKey() + PROMOTION_SUFFIX,
+ createProductFromDto(productDto, availablePromotions));
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์๋ ์ ํ์ ์ ํ ๋งต์ ์ถ๊ฐํฉ๋๋ค. ์ ํ DTO์ ํ๋ก๋ชจ์
ํ๋๊ฐ null์ธ ๊ฒฝ์ฐ์๋ง ํธ์ถ๋ฉ๋๋ค.
+ */
+ private boolean addProductWithoutPromotion(List<Promotion> availablePromotions,
+ Entry<String, List<ProductDto>> entry,
+ ProductDto productDto, Map<String, Product> productMap) {
+ if (productDto.promotion() == null) {
+ productMap.put(entry.getKey() + NO_PROMOTION_SUFFIX,
+ createProductFromDto(productDto, availablePromotions));
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * ์ฃผ์ด์ง ์ ํ DTO์ ์ฌ์ฉ ๊ฐ๋ฅํ ํ๋ก๋ชจ์
๋ชฉ๋ก์ ๊ธฐ๋ฐ์ผ๋ก Product ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
+ *
+ * @param productDto ๋ณํํ ์ ํ DTO
+ * @param availablePromotions ์ฌ์ฉ ๊ฐ๋ฅํ ํ๋ก๋ชจ์
๋ชฉ๋ก
+ * @return ์์ฑ๋ Product ๊ฐ์ฒด
+ */
+ private Product createProductFromDto(ProductDto productDto, List<Promotion> availablePromotions) {
+ Promotion promotion = promotionUtil.findMatchingPromotion(productDto.promotion(), availablePromotions);
+ return new Product(productDto, Optional.ofNullable(promotion));
+ }
+
+ /**
+ * ๊ทธ๋ฃนํ๋ ์ ํ DTO ๋ชฉ๋ก์์ ํ๋ก๋ชจ์
์ด ์๋ ๋จ์ผ ์ ํ์ ๊ธฐ๋ณธ ํ๋ก๋ชจ์
์ ์ ์ฉํฉ๋๋ค.
+ *
+ * @param groupedProductDtos ์ ํ ์ด๋ฆ์ ๊ธฐ์ค์ผ๋ก ๊ทธ๋ฃนํ๋ ์ ํ DTO ๋งต
+ */
+ private void applyDefaultPromotionForSingleProduct(Map<String, List<ProductDto>> groupedProductDtos) {
+ for (List<ProductDto> productDtos : groupedProductDtos.values()) {
+ if (isSingleProductWithPromotion(productDtos)) {
+ productDtos.add(ProductDto.toGeneralProductDto(productDtos.getFirst()));
+ }
+ }
+ }
+
+ /**
+ * ์ฃผ์ด์ง ์ ํ DTO ๋ฆฌ์คํธ๊ฐ ๋จ์ผ ์ ํ์ด๊ณ , ํด๋น ์ ํ์ ํ๋ก๋ชจ์
์ด ์๋์ง ํ์ธํฉ๋๋ค.
+ */
+ private boolean isSingleProductWithPromotion(List<ProductDto> productDtos) {
+ return productDtos.size() == 1 && productDtos.getFirst().promotion() != null;
+ }
+
+ /**
+ * ์ ํ DTO ๋ชฉ๋ก์ ์ ํ ์ด๋ฆ์ ๊ธฐ์ค์ผ๋ก ๊ทธ๋ฃนํํ์ฌ ๋ฐํํฉ๋๋ค.
+ */
+ private Map<String, List<ProductDto>> groupProductDtosByProductName(List<ProductDto> productDtos) {
+ Map<String, List<ProductDto>> groupedProductDtos = new LinkedHashMap<>();
+ for (ProductDto productDto : productDtos) {
+ groupedProductDtos.computeIfAbsent(productDto.name(), k -> new ArrayList<>())
+ .add(productDto);
+ }
+ return groupedProductDtos;
+ }
+
+ private void validateProductDtos(List<ProductDto> productDtos) {
+ if (productDtos.isEmpty()) {
+ throw new IllegalArgumentException(EMPTY_DATA.getMessage());
+ }
+ }
+
+} | Java | depth๊ฐ ๋ค์ ๊น์ ๊ฒ ๊ฐ์์!
๋ชจ๋ํ๋ฅผ ํด๋ณด๋๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,112 @@
+package store.custom.service.editor;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class ProductsEditor {
+ // ์ฌ๊ณ ๋ชฉ๋ก ์ด๊ธฐ ์ค์ : ๋ชฉ๋ก์ ํ๋ก๋ชจ์
์ํ๋ง ์๋ ๊ฒฝ์ฐ ์ผ๋ฐ ์ํ ์ถ๊ฐ
+ public static Products inspectProductCatalog(Products originalCatalog) {
+ List<Product> resultCatalog = new ArrayList<>();
+ int currentIndex;
+ for (currentIndex = 0; currentIndex < originalCatalog.getProductsSize() - 1; currentIndex++) {
+ currentIndex += inspectProduct(originalCatalog, resultCatalog, currentIndex);
+ }
+ addLastProduct(originalCatalog, resultCatalog, currentIndex);
+
+ return new Products(resultCatalog);
+ }
+
+ private static int inspectProduct(Products original, List<Product> result, int currentIndex) {
+ Product currentProduct = original.getProductByIndex(currentIndex);
+ Product nextProduct = original.getProductByIndex(currentIndex + 1);
+
+ if (hasSameNameAndPrice(result, currentProduct, nextProduct)) {
+ return 1; // ๋๊ฐ์ ์ํ์ ๊ฒ์ฌ์๋ฃํ์ผ๋ฏ๋ก index ์ ๊ฐ 1 ์ฆ๊ฐ
+ }
+
+ return addProductConsideringPromotion(currentProduct, result);
+ }
+
+ private static boolean hasSameNameAndPrice(List<Product> result, Product currentProduct, Product nextProduct) {
+ if (currentProduct.getName().equals(nextProduct.getName())
+ && currentProduct.getPrice() == nextProduct.getPrice()) {
+ result.add(currentProduct);
+ result.add(nextProduct);
+ return true;
+ }
+ return false;
+ }
+
+ private static int addProductConsideringPromotion(Product currentProduct, List<Product> result) {
+ result.add(currentProduct);
+
+ if (currentProduct.getPromotion() != null) {
+ result.add(createZeroStockProduct(currentProduct)); // ์ฌ๊ณ ๊ฐ 0์ธ ์ ํ ์ถ๊ฐ
+ }
+ return 0;
+ }
+
+ private static Product createZeroStockProduct(Product product) {
+ return new Product(product.getName(), product.getPrice(), 0, null);
+ }
+
+ private static void addLastProduct(Products originalCatalog, List<Product> result, int currentIndex) {
+ if (currentIndex == originalCatalog.getProductsSize() - 1) {
+ Product lastProduct = originalCatalog.getProductByIndex(originalCatalog.getProductsSize() - 1);
+ addProductConsideringPromotion(lastProduct, result);
+ }
+ }
+
+ // ์ฃผ๋ฌธ ์๋์ ๋ฐ๋ฅธ ์ฌ๊ณ ์กฐ์
+ public static void adjustInventoryForOrders(OrderSheet orderSheet, Products productCatalog) {
+ for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) {
+ processOrderedProduct(orderedProduct, productCatalog);
+ }
+ }
+
+ private static void processOrderedProduct(OrderedProduct orderedProduct, Products productCatalog) {
+ int remainQuantity = orderedProduct.getQuantity();
+ for (Product product : productCatalog.getProducts()) {
+ if (remainQuantity == 0) {
+ break;
+ }
+ remainQuantity = updateProductQuantity(orderedProduct, product, remainQuantity);
+ }
+ }
+
+ private static int updateProductQuantity(OrderedProduct orderedProduct, Product product, int remainQuantity) {
+ if (product.getName().equals(orderedProduct.getName())) {
+ if (product.getPromotion() != null) {
+ return calculateRemainingQuantityWithPromotion(product, remainQuantity);
+ }
+ return calculateRemainingQuantityWithoutPromotion(product, remainQuantity);
+ }
+ return remainQuantity;
+ }
+
+ private static int calculateRemainingQuantityWithPromotion(Product product, int remainQuantity) {
+ int productQuantity = product.getQuantity();
+
+ if (remainQuantity >= productQuantity) {
+ product.setQuantity(0);
+ return remainQuantity - productQuantity;
+ }
+ product.setQuantity(productQuantity - remainQuantity);
+ return 0;
+ }
+
+ private static int calculateRemainingQuantityWithoutPromotion(Product product, int remainQuantity) {
+ int productQuantity = product.getQuantity();
+
+ if (remainQuantity == productQuantity) {
+ product.setQuantity(0);
+ return 0;
+ }
+ product.setQuantity(productQuantity - remainQuantity);
+ return 0;
+ }
+}
\ No newline at end of file | Java | [์ง๋ฌธ] `OrderSheetEditor`์ ๊ฒฝ์ฐ์๋ ๊ฐ์ฒด๋ฅผ ์ง์ ์์ฑํ์ฌ ์ฌ์ฉํ๋ ๋ฐฉ์์ ์ ํํ์
จ๋๋ฐ, `ProductsEditor`๋ ์ ํธ๋ฆฌํฐ ํด๋์ค์ ๊ฐ์ ํํ๋ก ์์ฑํ ์ด์ ๊ฐ ์์๊น์? |
@@ -0,0 +1,35 @@
+package store.custom.model.product;
+
+public class Product {
+ private final String name;
+ private final int price;
+ private int quantity;
+ private final String promotion;
+
+ public Product(String name, int price, int quantity, String promotion) {
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotion = promotion;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+}
\ No newline at end of file | Java | `product`์ `promotion` ํ๋๊ฐ `null` ๊ฐ์ ๊ฐ์ง ์ ์์ด Editor ๋ก์ง๋ค์์ `null` ์ ๋ํ ์ฒ๋ฆฌ๊ฐ ํ์ํฉ๋๋ค.
`Optional`์ ์ฌ์ฉํ์ฌ `promotion` ํ๋๊ฐ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ๋ฅผ ๋ช
์์ ์ผ๋ก ์ฒ๋ฆฌํ๊ฑฐ๋, "" ์ ๊ฐ์ ๊ธฐ๋ณธ ๊ฐ์ ๊ฐ์ง๋๋ก ํ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,21 @@
+package store.custom.service.filehandler;
+
+import static store.custom.validator.CustomErrorMessages.FILE_READING_FAIL;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import store.custom.validator.Validator;
+
+public class FileReader {
+ public static List<String> run(String filePath) {
+ Validator.validateFilePath(filePath);
+
+ try {
+ return Files.readAllLines(Path.of(filePath));
+ } catch (IOException e) {
+ throw new RuntimeException(FILE_READING_FAIL + e);
+ }
+ }
+}
\ No newline at end of file | Java | readAllLines๋ผ๋ ๋ฉ์๋๊ฐ ์์๋ค์! ์ ๋ while๋ฌธ์ผ๋ก ํ์ผ์ ๋๊น์ง ์ฝ์ด์ค๋๋ก ํ๋๋ฐ, ์ฌ์ฉํ์ ๋ฐฉ๋ฒ์ด ์ฐธ ๊ฐ๋จํ ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,112 @@
+package store.custom.service.editor;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class ProductsEditor {
+ // ์ฌ๊ณ ๋ชฉ๋ก ์ด๊ธฐ ์ค์ : ๋ชฉ๋ก์ ํ๋ก๋ชจ์
์ํ๋ง ์๋ ๊ฒฝ์ฐ ์ผ๋ฐ ์ํ ์ถ๊ฐ
+ public static Products inspectProductCatalog(Products originalCatalog) {
+ List<Product> resultCatalog = new ArrayList<>();
+ int currentIndex;
+ for (currentIndex = 0; currentIndex < originalCatalog.getProductsSize() - 1; currentIndex++) {
+ currentIndex += inspectProduct(originalCatalog, resultCatalog, currentIndex);
+ }
+ addLastProduct(originalCatalog, resultCatalog, currentIndex);
+
+ return new Products(resultCatalog);
+ }
+
+ private static int inspectProduct(Products original, List<Product> result, int currentIndex) {
+ Product currentProduct = original.getProductByIndex(currentIndex);
+ Product nextProduct = original.getProductByIndex(currentIndex + 1);
+
+ if (hasSameNameAndPrice(result, currentProduct, nextProduct)) {
+ return 1; // ๋๊ฐ์ ์ํ์ ๊ฒ์ฌ์๋ฃํ์ผ๋ฏ๋ก index ์ ๊ฐ 1 ์ฆ๊ฐ
+ }
+
+ return addProductConsideringPromotion(currentProduct, result);
+ }
+
+ private static boolean hasSameNameAndPrice(List<Product> result, Product currentProduct, Product nextProduct) {
+ if (currentProduct.getName().equals(nextProduct.getName())
+ && currentProduct.getPrice() == nextProduct.getPrice()) {
+ result.add(currentProduct);
+ result.add(nextProduct);
+ return true;
+ }
+ return false;
+ }
+
+ private static int addProductConsideringPromotion(Product currentProduct, List<Product> result) {
+ result.add(currentProduct);
+
+ if (currentProduct.getPromotion() != null) {
+ result.add(createZeroStockProduct(currentProduct)); // ์ฌ๊ณ ๊ฐ 0์ธ ์ ํ ์ถ๊ฐ
+ }
+ return 0;
+ }
+
+ private static Product createZeroStockProduct(Product product) {
+ return new Product(product.getName(), product.getPrice(), 0, null);
+ }
+
+ private static void addLastProduct(Products originalCatalog, List<Product> result, int currentIndex) {
+ if (currentIndex == originalCatalog.getProductsSize() - 1) {
+ Product lastProduct = originalCatalog.getProductByIndex(originalCatalog.getProductsSize() - 1);
+ addProductConsideringPromotion(lastProduct, result);
+ }
+ }
+
+ // ์ฃผ๋ฌธ ์๋์ ๋ฐ๋ฅธ ์ฌ๊ณ ์กฐ์
+ public static void adjustInventoryForOrders(OrderSheet orderSheet, Products productCatalog) {
+ for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) {
+ processOrderedProduct(orderedProduct, productCatalog);
+ }
+ }
+
+ private static void processOrderedProduct(OrderedProduct orderedProduct, Products productCatalog) {
+ int remainQuantity = orderedProduct.getQuantity();
+ for (Product product : productCatalog.getProducts()) {
+ if (remainQuantity == 0) {
+ break;
+ }
+ remainQuantity = updateProductQuantity(orderedProduct, product, remainQuantity);
+ }
+ }
+
+ private static int updateProductQuantity(OrderedProduct orderedProduct, Product product, int remainQuantity) {
+ if (product.getName().equals(orderedProduct.getName())) {
+ if (product.getPromotion() != null) {
+ return calculateRemainingQuantityWithPromotion(product, remainQuantity);
+ }
+ return calculateRemainingQuantityWithoutPromotion(product, remainQuantity);
+ }
+ return remainQuantity;
+ }
+
+ private static int calculateRemainingQuantityWithPromotion(Product product, int remainQuantity) {
+ int productQuantity = product.getQuantity();
+
+ if (remainQuantity >= productQuantity) {
+ product.setQuantity(0);
+ return remainQuantity - productQuantity;
+ }
+ product.setQuantity(productQuantity - remainQuantity);
+ return 0;
+ }
+
+ private static int calculateRemainingQuantityWithoutPromotion(Product product, int remainQuantity) {
+ int productQuantity = product.getQuantity();
+
+ if (remainQuantity == productQuantity) {
+ product.setQuantity(0);
+ return 0;
+ }
+ product.setQuantity(productQuantity - remainQuantity);
+ return 0;
+ }
+}
\ No newline at end of file | Java | ์ ๋ ํ๋ก๋ชจ์
์ํ๋ง ์๋ ๊ฒฝ์ฐ ์ผ๋ฐ ์ํ์ ์ถ๊ฐํ๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํ์ง ๋ชปํ๋๋ฐ, ๊ทธ๋์ ๊ทธ๋ฐ์ง ๋ ์ธ์์ ์ด์์...๐
๊ผผ๊ผผํ ๋ก์ง์์ ๋ง์ด ๋ฐฐ์๋๋ค. |
@@ -0,0 +1,105 @@
+package store.custom.validator;
+
+import static store.custom.constants.RegexConstants.PRODUCT_ORDER_REGEX;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+import static store.custom.validator.CustomErrorMessages.INVALID_FILE_PATH;
+import static store.custom.validator.CustomErrorMessages.INVALID_INPUT;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class Validator {
+ // ํ์ผ ๋ฆฌ๋๊ธฐ ๊ด๋ จ ์ ํจ์ฑ ๊ฒ์ฌ
+ public static void validateFilePath(String filePath) {
+ if (!Files.exists(Path.of(filePath))) {
+ throw new IllegalArgumentException(INVALID_FILE_PATH + filePath);
+ }
+ }
+
+ // ์
๋ ฅ ๊ด๋ จ ์ ํจ์ฑ ๊ฒ์ฌ (๊ณตํต)
+ public static void validateEmptyInput(String input) {
+ checkNullInput(input);
+ checkEmptyInput(input);
+ checkWhitespaceOnlyInput(input);
+ }
+
+ private static void checkNullInput(String input) {
+ if (input == null) {
+ throw new IllegalArgumentException(INVALID_INPUT);
+ }
+ }
+
+ private static void checkEmptyInput(String input) {
+ if (input.isEmpty()) {
+ throw new IllegalArgumentException(INVALID_INPUT);
+ }
+ }
+
+ private static void checkWhitespaceOnlyInput(String input) {
+ if (input.trim().isEmpty()) {
+ throw new IllegalArgumentException(INVALID_INPUT);
+ }
+ }
+
+ // ์ฃผ๋ฌธ ์
๋ ฅ ๊ด๋ จ ์ ํจ์ฑ ๊ฒ์ฌ
+ public static void validateOrderForm(List<String> orderForms) {
+ for (String orderForm : orderForms) {
+ if (!orderForm.matches(PRODUCT_ORDER_REGEX)) {
+ throw new IllegalArgumentException(CustomErrorMessages.INVALID_ORDER_FORMAT);
+ }
+ }
+ }
+
+ public static void validateOrderSheet(Products products, OrderSheet orderSheet) {
+ validateOrderedProductsName(products, orderSheet);
+ validateOrderedProductsQuantity(products, orderSheet);
+ }
+
+ public static void validateOrderedProductsName(Products products, OrderSheet orderSheet) {
+ for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) {
+ if (!isProductNameMatched(products, orderedProduct)) { // ์ ํ์ด ์์ผ๋ฉด
+ throw new IllegalArgumentException(CustomErrorMessages.NON_EXISTENT_PRODUCT);
+ }
+ }
+ }
+
+ private static boolean isProductNameMatched(Products products, OrderedProduct orderedProduct) {
+ for (Product product : products.getProducts()) {
+ if (product.getName().equals(orderedProduct.getName())) {
+ return true; // ์ ํ์ด ์กด์ฌํ ๋ true ๋ฐํ
+ }
+ }
+ return false; // ์ ํ์ด ์กด์ฌํ์ง์์ ๋ false ๋ฐํ
+ }
+
+ public static void validateOrderedProductsQuantity(Products products, OrderSheet orderSheet) {
+ for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) {
+ if (orderedProduct.getQuantity() > calculateProductTotalQuantity(products, orderedProduct)) {
+ throw new IllegalArgumentException(CustomErrorMessages.INSUFFICIENT_STOCK);
+ }
+ }
+ }
+
+ private static int calculateProductTotalQuantity(Products products, OrderedProduct orderedProduct) {
+ int totalQuantity = 0;
+ for (Product product : products.getProducts()) {
+ if (product.getName().equals(orderedProduct.getName())) {
+ totalQuantity += product.getQuantity();
+ }
+ }
+ return totalQuantity;
+ }
+
+ // ์๋ต ์
๋ ฅ ๊ด๋ จ ์ ํจ์ฑ ๊ฒ์ฌ
+ public static void validateYesOrNoInput(String response) {
+ if (!response.equals(RESPONSE_YES) && !response.equals(RESPONSE_NO)) {
+ throw new IllegalArgumentException(INVALID_INPUT);
+ }
+ }
+}
\ No newline at end of file | Java | ์ ๊ท ํํ์์ ์ฌ์ฉํ๋ ์ ๋ง ๊ฐ๋จํ๊ฒ ์ฒ๋ฆฌ๋๋ค์!! ๋งค๋ฒ ๋๋ผ๋ ๊ฑฐ์ง๋ง ํจ์จ์ ์ธ ๋ฐฉ๋ฒ์ ์ ์ฌ์ฉํ์๋ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,99 @@
+package store.custom.service.maker;
+
+import static store.custom.constants.NumberConstants.NOT_FOUND;
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.custom.model.PromotionInfo;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.PromotionResult.PromotionResults;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class PromotionResultMaker {
+ public PromotionResults createPromotionResults(Products products, OrderSheet orderSheet) {
+ List<PromotionResult> results = new ArrayList<>();
+
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ PromotionInfo promotionInfo = createPromotionInfo(products.getProducts(), orderProduct);
+ PromotionResult result = calculateResult(orderProduct, promotionInfo);
+ results.add(result);
+ }
+ return new PromotionResults(results);
+ }
+
+ // ์ฃผ๋ฌธ ์๋์ ๋ฐ๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ํ์ ์์ฑ
+ private PromotionInfo createPromotionInfo(List<Product> products, OrderedProduct orderProduct) {
+ int promotionQuantity = quantityWithPromotion(products, orderProduct.getName());
+ int orderQuantity = orderProduct.getQuantity();
+ if (isPromotionNull(orderProduct)) {
+ return new PromotionInfo(promotionQuantity, NOT_FOUND, orderQuantity, NOT_FOUND, NOT_FOUND);
+ }
+ int promotionConditions = orderProduct.getBuy() + orderProduct.getGet();
+ return new PromotionInfo(promotionQuantity, promotionConditions, orderQuantity,
+ orderQuantity / promotionConditions, orderQuantity % promotionConditions);
+ }
+
+ private int quantityWithPromotion(List<Product> products, String productName) {
+ int quantity = 0;
+ for (Product product : products) {
+ if (product.getName().equals(productName) && product.getPromotion() != null) {
+ quantity = product.getQuantity();
+ }
+ }
+ return quantity;
+ }
+
+ private boolean isPromotionNull(OrderedProduct orderProduct) {
+ String promotion = orderProduct.getPromotion();
+ return (promotion == null || promotion.equals(BEFORE_PROMOTION_START) || promotion.equals(AFTER_PROMOTION_END));
+ }
+
+ // ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๊ณ์ฐ
+ private PromotionResult calculateResult(OrderedProduct orderProduct, PromotionInfo promotionInfo) {
+ if (promotionInfo.getRemainder() == NOT_FOUND) { // ํ๋ก๋ชจ์
์ด ์๋ ๊ฒฝ์ฐ
+ return new PromotionResult(NOT_FOUND, NOT_FOUND, NOT_FOUND);
+ }
+ return hasPromotion(orderProduct, promotionInfo);
+ }
+
+ private PromotionResult hasPromotion(OrderedProduct orderProduct, PromotionInfo promotionInfo) {
+ if (promotionInfo.getRemainder() == 0) {
+ return applyFullPromotionConditions(promotionInfo);
+ }
+ if (promotionInfo.getRemainder() < orderProduct.getBuy()) {
+ return applyPartialPromotionConditions(promotionInfo);
+ }
+ return applyAdditionalPromotionConditions(promotionInfo);
+ }
+
+ private PromotionResult applyFullPromotionConditions(PromotionInfo promotionInfo) {
+ if (promotionInfo.getOrderQuantity() > promotionInfo.getPromotionQuantity()) {
+ int validPromotionCount = promotionInfo.getPromotionQuantity() / promotionInfo.getConditions();
+ int excludedCount = (promotionInfo.getQuotient() - validPromotionCount) * promotionInfo.getConditions();
+ return new PromotionResult(validPromotionCount, 0, excludedCount);
+ }
+ // if (promotionInfo.getOrderQuantity() <= promotionInfo.getPromotionQuantity())
+ return new PromotionResult(promotionInfo.getQuotient(), 0, 0);
+ }
+
+ private PromotionResult applyPartialPromotionConditions(PromotionInfo promotionInfo) {
+ if (promotionInfo.getConditions() * promotionInfo.getQuotient() > promotionInfo.getPromotionQuantity()) {
+ int validPromotionCount = promotionInfo.getPromotionQuantity() / promotionInfo.getConditions();
+ int excludedCount = (promotionInfo.getQuotient() - validPromotionCount) * promotionInfo.getConditions();
+ return new PromotionResult(validPromotionCount, 0, excludedCount + promotionInfo.getRemainder());
+ }
+ return new PromotionResult(promotionInfo.getQuotient(), 0, promotionInfo.getRemainder());
+ }
+
+ private PromotionResult applyAdditionalPromotionConditions(PromotionInfo promotionInfo) {
+ if (promotionInfo.getConditions() * (promotionInfo.getQuotient() + 1) > promotionInfo.getPromotionQuantity()) {
+ applyPartialPromotionConditions(promotionInfo);
+ }
+ return new PromotionResult(promotionInfo.getQuotient(), 1, 0);
+ }
+}
\ No newline at end of file | Java | ์ ๋ฉ์๋๋ช
์ฌ๊ธฐ๋์...! |
@@ -0,0 +1,153 @@
+package store.custom.service.editor;
+
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDate;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotion;
+import store.custom.model.promotion.Promotions;
+
+public class OrderSheetEditor {
+ // ์ฃผ๋ฌธ์ ์ด๊ธฐ ์ค์ : ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ ๋ณด ์ถ๊ฐ
+ public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) {
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ถ๊ฐ
+ setPromotionDetails(orderProduct, promotions); // ํ๋ก๋ชจ์
๊ด๋ จ ์ ๋ณด ์ถ๊ฐ
+ }
+ }
+
+ private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) {
+ Product product = findOrderedProduct(orderProduct, productCatalog);
+ if (product != null) {
+ orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity());
+ setPromotionIfExist(orderProduct, product);
+ }
+ }
+
+ private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) {
+ for (Product product : productCatalog.getProducts()) {
+ if (product.getName().equals(orderProduct.getName())) {
+ return product;
+ }
+ }
+ return null;
+ }
+
+ private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
+ if (product.getPromotion() != null) {
+ orderProduct.setPromotion(product.getPromotion());
+ }
+ }
+
+ private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) {
+ if (orderProduct.getPromotion() != null) {
+ LocalDate today = DateTimes.now().toLocalDate();
+ Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions);
+ if (promotion != null) {
+ applyPromotionDates(orderProduct, today, promotion);
+ applyPromotionBuyAndGet(orderProduct, promotion);
+ }
+ }
+ }
+
+ private Promotion findPromotion(String promotionName, Promotions promotions) {
+ for (Promotion promotion : promotions.getPromotions()) {
+ if (promotion.getName().equals(promotionName)) {
+ return promotion;
+ }
+ }
+ return null;
+ }
+
+ private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) {
+ if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) {
+ orderProduct.setPromotion(BEFORE_PROMOTION_START);
+ }
+
+ if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) {
+ orderProduct.setPromotion(AFTER_PROMOTION_END);
+ }
+ }
+
+ private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) {
+ String orderProductPromotion = orderProduct.getPromotion();
+
+ if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START)
+ && !orderProductPromotion.equals(AFTER_PROMOTION_END)) {
+ orderProduct.setBuy(promotion.getBuy());
+ orderProduct.setGet(promotion.getGet());
+ }
+ }
+
+ // ์ฃผ๋ฌธ์ ์์ : ์ฃผ๋ฌธ ์๋์ ๋ค๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๋ฐ์
+ public void applyResponseForNoPromotion(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processNonPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์๋ ๊ฐ์
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ๊ฐ์์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ }
+
+ public void applyResponseForFreeProduct(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processAdditionalPromotionApplied(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processAdditionalPromotionNotApplied(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์๋ ์ฆ๊ฐ
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ์ฆ๊ฐ์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1));
+ }
+
+ private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+}
\ No newline at end of file | Java | OrderSheetEditor ํด๋์ค ๋ถ๋ถ์ ์ฐธ๊ณ ํ๋ฉด ์ ๊ฐ ์ฌ์
๋ ฅ๊ณผ ๊ด๋ จํด ๊ณ ๋ฏผํ๋ ๋ถ๋ถ์ด ์ผ๋ถ ํด๊ฒฐ๋ ๊ฒ ๊ฐ์์.
์ถ๊ฐ๋ก, ์ ์ฒด์ ์ผ๋ก ์ ์ ์ ์ฝํ๋ ์ฝ๋์ ๋ก์ง์ ๊ฐํํ๊ณ ๊ฐ๋๋ค. |
@@ -0,0 +1,35 @@
+package store.custom.model.product;
+
+public class Product {
+ private final String name;
+ private final int price;
+ private int quantity;
+ private final String promotion;
+
+ public Product(String name, int price, int quantity, String promotion) {
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotion = promotion;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+}
\ No newline at end of file | Java | promotion ํ๋๋ ํ๋ก๋ชจ์
๊ฐ์ฒด๋ฅผ ์ง์ ์ฐธ์กฐํ๋๋ก ํ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,153 @@
+package store.custom.service.editor;
+
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDate;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotion;
+import store.custom.model.promotion.Promotions;
+
+public class OrderSheetEditor {
+ // ์ฃผ๋ฌธ์ ์ด๊ธฐ ์ค์ : ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ ๋ณด ์ถ๊ฐ
+ public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) {
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ถ๊ฐ
+ setPromotionDetails(orderProduct, promotions); // ํ๋ก๋ชจ์
๊ด๋ จ ์ ๋ณด ์ถ๊ฐ
+ }
+ }
+
+ private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) {
+ Product product = findOrderedProduct(orderProduct, productCatalog);
+ if (product != null) {
+ orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity());
+ setPromotionIfExist(orderProduct, product);
+ }
+ }
+
+ private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) {
+ for (Product product : productCatalog.getProducts()) {
+ if (product.getName().equals(orderProduct.getName())) {
+ return product;
+ }
+ }
+ return null;
+ }
+
+ private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
+ if (product.getPromotion() != null) {
+ orderProduct.setPromotion(product.getPromotion());
+ }
+ }
+
+ private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) {
+ if (orderProduct.getPromotion() != null) {
+ LocalDate today = DateTimes.now().toLocalDate();
+ Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions);
+ if (promotion != null) {
+ applyPromotionDates(orderProduct, today, promotion);
+ applyPromotionBuyAndGet(orderProduct, promotion);
+ }
+ }
+ }
+
+ private Promotion findPromotion(String promotionName, Promotions promotions) {
+ for (Promotion promotion : promotions.getPromotions()) {
+ if (promotion.getName().equals(promotionName)) {
+ return promotion;
+ }
+ }
+ return null;
+ }
+
+ private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) {
+ if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) {
+ orderProduct.setPromotion(BEFORE_PROMOTION_START);
+ }
+
+ if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) {
+ orderProduct.setPromotion(AFTER_PROMOTION_END);
+ }
+ }
+
+ private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) {
+ String orderProductPromotion = orderProduct.getPromotion();
+
+ if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START)
+ && !orderProductPromotion.equals(AFTER_PROMOTION_END)) {
+ orderProduct.setBuy(promotion.getBuy());
+ orderProduct.setGet(promotion.getGet());
+ }
+ }
+
+ // ์ฃผ๋ฌธ์ ์์ : ์ฃผ๋ฌธ ์๋์ ๋ค๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๋ฐ์
+ public void applyResponseForNoPromotion(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processNonPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์๋ ๊ฐ์
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ๊ฐ์์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ }
+
+ public void applyResponseForFreeProduct(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processAdditionalPromotionApplied(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processAdditionalPromotionNotApplied(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์๋ ์ฆ๊ฐ
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ์ฆ๊ฐ์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1));
+ }
+
+ private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+}
\ No newline at end of file | Java | OrderSheetEditor์์ ๋ง์ ์ญํ ์ ํ๊ณ ์๋๋ฏ ํฉ๋๋ค ์ฃผ๋ฌธ์ ํธ์ง, ํ๋ก๋ชจ์
์ ์ฉ, ์ฌ๊ณ ๊ด๋ฆฌ ๋ฑ์ ๊ธฐ๋ฅ์ ๊ฐ๊ฐ ๋ถ๋ฆฌํ๋ ๊ฒ ์ด๋จ๊น์? ์ฌ์ค ์ด๋ฐ ๋ถ๋ถ์์ ์ ๋ ๊ณ ๋ฏผ์ด ๋ง์ ํฐ๋ผ ๋ผ์ ค๋์ ๋ค๋ฅธ ์๊ฒฌ์ด ์๋ค๋ฉด ๋ฃ๊ณ ์ถ์ต๋๋ค! |
@@ -0,0 +1,63 @@
+package store.custom.model.order;
+
+public class OrderedProduct {
+ private final String name;
+ private int quantity;
+ private int totalPrice;
+ private String promotion;
+ private int buy;
+ private int get;
+
+ public OrderedProduct(String name, int quantity, int totalPrice, String promotion, int buy, int get) {
+ this.name = name;
+ this.quantity = quantity;
+ this.totalPrice = totalPrice;
+ this.promotion = promotion;
+ this.buy = buy;
+ this.get = get;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public int getTotalPrice() {
+ return totalPrice;
+ }
+
+ public void setTotalPrice(int totalPrice) {
+ this.totalPrice = totalPrice;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+
+ public void setPromotion(String promotion) {
+ this.promotion = promotion;
+ }
+
+ public int getBuy() {
+ return buy;
+ }
+
+ public void setBuy(int buy) {
+ this.buy = buy;
+ }
+
+ public int getGet() {
+ return get;
+ }
+
+ public void setGet(int get) {
+ this.get = get;
+ }
+}
\ No newline at end of file | Java | setter๋ฅผ ์ด์ด๋๋๊ฒ์ ์ข์ง ์๋ค๊ณ ๋ฐฐ์ ๋ ๊ฒ ๊ฐ์ต๋๋ค! ์ฌ๊ณ ๋ฅผ ์ฐจ๊ฐํ๋ค๋๊ฐ ๋ค๋ฅธ ๊ฐ๋ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ํ ๋นํ๋ค๋๊ฐ ํ๋ ๋ก์ง์ ๋ฐ๋ก ๋ง๋ค์ด์ ์ฒ๋ฆฌํ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,58 @@
+package store.custom.service.parser;
+
+import static store.custom.constants.RegexConstants.SINGLE_COMMA;
+import static store.custom.constants.StringConstants.NO_PROMOTION;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class ProductParser {
+ public static Products run(List<String> lines) {
+ List<Product> productCatalog = new ArrayList<>();
+
+ if (lines != null) {
+ parseProductLines(lines, productCatalog);
+ }
+
+ return new Products(productCatalog);
+ }
+
+ private static void parseProductLines(List<String> lines, List<Product> productCatalog) {
+ for (int currentLine = 1; currentLine < lines.size(); currentLine++) {
+ List<String> currentLineParts = List.of(lines.get(currentLine).split(SINGLE_COMMA));
+ Product product = createProduct(currentLineParts);
+ productCatalog.add(product);
+ }
+ }
+
+ private static Product createProduct(List<String> parts) {
+ String name = extractProductName(parts);
+ int price = extractProductPrice(parts);
+ int quantity = extractProductQuantity(parts);
+ String promotion = extractProductPromotion(parts);
+
+ return new Product(name, price, quantity, promotion);
+ }
+
+ private static String extractProductName(List<String> parts) {
+ return parts.get(0).trim();
+ }
+
+ private static int extractProductPrice(List<String> parts) {
+ return Integer.parseInt(parts.get(1).trim());
+ }
+
+ private static int extractProductQuantity(List<String> parts) {
+ return Integer.parseInt(parts.get(2).trim());
+ }
+
+ private static String extractProductPromotion(List<String> parts) {
+ String promotion = parts.get(3).trim();
+ if (promotion.equals(NO_PROMOTION)) {
+ return null;
+ }
+ return promotion;
+ }
+}
\ No newline at end of file | Java | ๋ณ๊ฑฐ ์๋ ๋ถ๋ถ์ด๊ธด ํ๋ฐ, ์ธ๋ฑ์ค๋ฅผ ํ๋์ฝ๋ฉํ๋ ๊ฒ ๋ณด๋ค ์์๋ก ๋นผ์ nameIndex, priceIndex ์ฒ๋ผ ๋์์ผ๋ฉด ๋ ์ข์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,153 @@
+package store.custom.service.editor;
+
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDate;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotion;
+import store.custom.model.promotion.Promotions;
+
+public class OrderSheetEditor {
+ // ์ฃผ๋ฌธ์ ์ด๊ธฐ ์ค์ : ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ ๋ณด ์ถ๊ฐ
+ public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) {
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ถ๊ฐ
+ setPromotionDetails(orderProduct, promotions); // ํ๋ก๋ชจ์
๊ด๋ จ ์ ๋ณด ์ถ๊ฐ
+ }
+ }
+
+ private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) {
+ Product product = findOrderedProduct(orderProduct, productCatalog);
+ if (product != null) {
+ orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity());
+ setPromotionIfExist(orderProduct, product);
+ }
+ }
+
+ private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) {
+ for (Product product : productCatalog.getProducts()) {
+ if (product.getName().equals(orderProduct.getName())) {
+ return product;
+ }
+ }
+ return null;
+ }
+
+ private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
+ if (product.getPromotion() != null) {
+ orderProduct.setPromotion(product.getPromotion());
+ }
+ }
+
+ private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) {
+ if (orderProduct.getPromotion() != null) {
+ LocalDate today = DateTimes.now().toLocalDate();
+ Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions);
+ if (promotion != null) {
+ applyPromotionDates(orderProduct, today, promotion);
+ applyPromotionBuyAndGet(orderProduct, promotion);
+ }
+ }
+ }
+
+ private Promotion findPromotion(String promotionName, Promotions promotions) {
+ for (Promotion promotion : promotions.getPromotions()) {
+ if (promotion.getName().equals(promotionName)) {
+ return promotion;
+ }
+ }
+ return null;
+ }
+
+ private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) {
+ if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) {
+ orderProduct.setPromotion(BEFORE_PROMOTION_START);
+ }
+
+ if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) {
+ orderProduct.setPromotion(AFTER_PROMOTION_END);
+ }
+ }
+
+ private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) {
+ String orderProductPromotion = orderProduct.getPromotion();
+
+ if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START)
+ && !orderProductPromotion.equals(AFTER_PROMOTION_END)) {
+ orderProduct.setBuy(promotion.getBuy());
+ orderProduct.setGet(promotion.getGet());
+ }
+ }
+
+ // ์ฃผ๋ฌธ์ ์์ : ์ฃผ๋ฌธ ์๋์ ๋ค๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๋ฐ์
+ public void applyResponseForNoPromotion(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processNonPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์๋ ๊ฐ์
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ๊ฐ์์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ }
+
+ public void applyResponseForFreeProduct(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processAdditionalPromotionApplied(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processAdditionalPromotionNotApplied(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์๋ ์ฆ๊ฐ
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ์ฆ๊ฐ์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1));
+ }
+
+ private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+}
\ No newline at end of file | Java | OrderSheetEditor์ ์ญํ ์ด ๋ง๋ค๋ ์ ์์ ์ ๋ ๋น์ทํ ์๊ฐ์
๋๋ค!
+ **3์ฃผ์ฐจ ํผ๋๋ฐฑ ์ค ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด๋ต๊ฒ ์ฌ์ฉํ๋ค** ํผ๋๋ฐฑ์ด ๋ ์ฌ๋์ด์. ์ผ๋ถ ๋ก์ง ์ค์์๋ ๋ชจ๋ธ ๋ด๋ถ์์ ์ฑ
์์ ๊ฐ์ ธ๊ฐ ์ ์๋ ๊ฒ๋ค๋ ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
๋ํ์ ์ผ๋ก ์๋ ๋ฉ์๋๊ฐ ๊ทธ๋ฐ ๊ฒ ๊ฐ์์!
```
private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
if (product.getPromotion() != null) {
orderProduct.setPromotion(product.getPromotion());
}
}
```
product.getPromotion()์ด String์ด๊ธฐ ๋๋ฌธ์ orderProduct ๋ด๋ถ์์ ์ฒ๋ฆฌํ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
์ ์ฒ๋ผ ๊ฐ ๋๋ฉ์ธ์์ ์ฒ๋ฆฌ๊ฐ๋ฅํ ๋ก์ง๋ค์ ์์์์ง ์๊ฐํด๋ณด๋ฉด ์ด๋จ๊น ์ถ์ต๋๋ค. |
@@ -0,0 +1,63 @@
+package store.custom.model.order;
+
+public class OrderedProduct {
+ private final String name;
+ private int quantity;
+ private int totalPrice;
+ private String promotion;
+ private int buy;
+ private int get;
+
+ public OrderedProduct(String name, int quantity, int totalPrice, String promotion, int buy, int get) {
+ this.name = name;
+ this.quantity = quantity;
+ this.totalPrice = totalPrice;
+ this.promotion = promotion;
+ this.buy = buy;
+ this.get = get;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public int getTotalPrice() {
+ return totalPrice;
+ }
+
+ public void setTotalPrice(int totalPrice) {
+ this.totalPrice = totalPrice;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+
+ public void setPromotion(String promotion) {
+ this.promotion = promotion;
+ }
+
+ public int getBuy() {
+ return buy;
+ }
+
+ public void setBuy(int buy) {
+ this.buy = buy;
+ }
+
+ public int getGet() {
+ return get;
+ }
+
+ public void setGet(int get) {
+ this.get = get;
+ }
+}
\ No newline at end of file | Java | ์์ ์ธ๊ธ๋๋ฆฐ 3์ฃผ์ฐจ ํผ๋๋ฐฑ ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด๋ต๊ฒ ์ฌ์ฉํ๊ธฐ์ ์ฐ๊ด๋ ๋ฆฌ๋ทฐ๊ฐ ์๋๊น ์ถ๋ค์!
3์ฃผ์ฐจ ํผ๋๋ฐฑ ์ค ๋ฐ์ท
>Lotto์์ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ด์ง(get) ๋ง๊ณ ๋ฉ์์ง๋ฅผ ๋์ง๋๋ก ๊ตฌ์กฐ๋ฅผ ๋ฐ๊ฟ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๋ ๊ฐ์ฒด๊ฐ ์ผํ๋๋ก ํ๋ค. ์ด์ฒ๋ผ Lotto ๊ฐ์ฒด์์ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ด(get) ์ฌ์ฉํ๊ธฐ๋ณด๋ค๋, ๋ฐ์ดํฐ๊ฐ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด๊ฐ ์ค์ค๋ก ์ฒ๋ฆฌํ ์ ์๋๋ก ๊ตฌ์กฐ๋ฅผ ๋ณ๊ฒฝํด์ผ ํ๋ค. ์๋์ ๊ฐ์ด ๋ฐ์ดํฐ๋ฅผ ์ธ๋ถ์์ ๊ฐ์ ธ์(get) ์ฒ๋ฆฌํ์ง ๋ง๊ณ , ๊ฐ์ฒด๊ฐ ์์ ์ ๋ฐ์ดํฐ๋ฅผ ์ค์ค๋ก ์ฒ๋ฆฌํ๋๋ก ๋ฉ์์ง๋ฅผ ๋์ง๊ฒ ํ๋ค. |
@@ -0,0 +1,190 @@
+package store.custom.controller;
+
+import static store.custom.constants.StringConstants.PRODUCTS_FILE_PATH;
+import static store.custom.constants.StringConstants.PROMOTIONS_FILE_PATH;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import java.util.List;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.PromotionResult.PromotionResults;
+import store.custom.model.ReceiptDetails;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotions;
+import store.custom.service.editor.OrderSheetEditor;
+import store.custom.service.editor.ProductsEditor;
+import store.custom.service.filehandler.FileReader;
+import store.custom.service.maker.PromotionResultMaker;
+import store.custom.service.maker.ReceiptDetailsMaker;
+import store.custom.service.parser.OrderParser;
+import store.custom.service.parser.ProductParser;
+import store.custom.service.parser.PromotionParser;
+import store.custom.service.parser.ResponseParser;
+import store.custom.view.InputView;
+import store.custom.view.OutputView;
+
+public class StoreController {
+ private final OrderSheetEditor orderSheetEditor;
+ private final PromotionResultMaker promotionResultsMaker;
+ private final ReceiptDetailsMaker receiptDetailsMaker;
+ private final OrderParser orderParser;
+ private final ResponseParser responseParser;
+
+ private final InputView inputView;
+ private final OutputView outputView;
+
+ public StoreController(InputView inputView, OutputView outputView) {
+ this.orderSheetEditor = new OrderSheetEditor();
+ this.promotionResultsMaker = new PromotionResultMaker();
+ this.receiptDetailsMaker = new ReceiptDetailsMaker();
+ this.orderParser = new OrderParser();
+ this.responseParser = new ResponseParser();
+
+ this.inputView = inputView;
+ this.outputView = outputView;
+ }
+
+ public void start() {
+ Products productCatalog = setUpProductCatalog();
+ Promotions promotionCatalog = setUpPromotionCatalog();
+
+ String repeat = RESPONSE_YES;
+ while (RESPONSE_YES.equals(repeat)) {
+ outputView.displayInventoryStatus(productCatalog);
+ repeat = handleStoreOrder(productCatalog, promotionCatalog);
+ }
+ }
+
+ // ํธ์์ ํ๋ก๊ทธ๋จ ์ด๊ธฐ ์
์
๋ฉ์๋
+ private Products setUpProductCatalog() {
+ List<String> productsLines = FileReader.run(PRODUCTS_FILE_PATH);
+ Products productCatalog = ProductParser.run(productsLines);
+ return ProductsEditor.inspectProductCatalog(productCatalog);
+ }
+
+ private Promotions setUpPromotionCatalog() {
+ List<String> promotionLines = FileReader.run(PROMOTIONS_FILE_PATH);
+ return PromotionParser.run(promotionLines);
+ }
+
+ // ์ฃผ๋ฌธ์์ฒญ ์ฒ๋ฆฌ ๋ฉ์๋
+ private String handleStoreOrder(Products productCatalog, Promotions promotionCatalog) {
+ OrderSheet orderSheet = handleOrderSheet(productCatalog, promotionCatalog);
+ ProductsEditor.adjustInventoryForOrders(orderSheet, productCatalog); // ์ฃผ๋ฌธ์์ ๋ง์ถฐ ์ฌ๊ณ ๊ด๋ฆฌ
+ ReceiptDetails receiptDetails = receiptDetailsMaker.run(orderSheet, inputResponseForMembership());
+
+ outputView.displayReceipt(orderSheet, receiptDetails); // ๋ ์ํผ ์ถ๋ ฅ
+ return inputResponseForAdditionalPurchase();
+ }
+
+ // ์ฃผ๋ฌธ์ ์ฒ๋ฆฌ ๋ฉ์๋
+ private OrderSheet handleOrderSheet(Products productCatalog, Promotions promotionCatalog) {
+ OrderSheet orderSheet = inputOrderRequest(productCatalog);
+ orderSheetEditor.addPromotionInfo(productCatalog, promotionCatalog, orderSheet);
+
+ PromotionResults promotionResults =
+ promotionResultsMaker.createPromotionResults(productCatalog, orderSheet); // ํ๋ก๋ชจ์
๊ฒฐ๊ณผ์ง ๋ง๋ค๊ธฐ
+ handlePromotionResults(orderSheet, promotionResults); // ํ๋ก๋ชจ์
๊ฒฐ๊ณผ์ง๋ฅผ ์ฃผ๋ฌธ์์ ๋ฐ์
+
+ return orderSheet;
+ }
+
+ // ํ๋ก๋ชจ์
ํ์ฌ ์ ์ฉ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ ๋ฉ์๋
+ private void handlePromotionResults(OrderSheet orderSheet, PromotionResults promotionResults) {
+ for (int index = 0; index < promotionResults.getPromotionResultCount(); index++) {
+ PromotionResult promotionResult = promotionResults.getPromotionResultByIndex(index);
+ OrderedProduct orderedProduct = orderSheet.getOrderSheetByIndex(index);
+
+ handleExcludedPromotionProduct(orderedProduct, promotionResult);
+ handleAdditionalFreebie(orderedProduct, promotionResult);
+ handlePromotionProduct(orderedProduct, promotionResult);
+ }
+ }
+
+ private void handleExcludedPromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ String orderedProductName = orderedProduct.getName();
+ int nonPromotionProduct = promotionResult.getNonPromotionProductCount();
+
+ if (nonPromotionProduct > 0) {
+ String responseForNoPromotion = inputResponseForNoPromotion(orderedProductName, nonPromotionProduct);
+ orderSheetEditor.applyResponseForNoPromotion(responseForNoPromotion, promotionResult, orderedProduct);
+ }
+ }
+
+ private void handleAdditionalFreebie(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ String orderedProductName = orderedProduct.getName();
+ int extraPromotionCount = promotionResult.getExtraPromotionCount();
+
+ if (extraPromotionCount > 0) {
+ int additionalFreebie = orderedProduct.getGet();
+ String responseForFreeProduct = inputResponseForFreebie(orderedProductName, additionalFreebie);
+ orderSheetEditor.applyResponseForFreeProduct(responseForFreeProduct, promotionResult, orderedProduct);
+ }
+ }
+
+ private void handlePromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int nonPromotionProduct = promotionResult.getNonPromotionProductCount();
+ int extraPromotionCount = promotionResult.getExtraPromotionCount();
+
+ if (extraPromotionCount == 0 && nonPromotionProduct == 0) {
+ orderSheetEditor.computeTotalWithPromotion(orderedProduct, promotionResult);
+ }
+ }
+
+ // ์ฌ์ฉ์ ์
๋ ฅ ๊ด๋ จ ๋ฉ์๋ (Error ์ ์ฌ์
๋ ฅ)
+ private OrderSheet inputOrderRequest(Products productCatalog) {
+ while (true) {
+ try {
+ String orderRequest = inputView.askForProductsToPurchase();
+ return orderParser.run(orderRequest, productCatalog);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForNoPromotion(String name, int noPromotionProductCount) {
+ while (true) {
+ try {
+ String response = inputView.askForProductWithoutPromotion(name, noPromotionProductCount);
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForFreebie(String name, int additionalFreeProduct) {
+ while (true) {
+ try {
+ String response = inputView.askForFreebie(name, additionalFreeProduct);
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForMembership() {
+ while (true) {
+ try {
+ String response = inputView.askForMembershipDiscount();
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForAdditionalPurchase() {
+ while (true) {
+ try {
+ String response = inputView.askForAdditionalPurchase();
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+}
\ No newline at end of file | Java | while(true)์ try-catch๊ฐ ๋ฐ๋ณต๋๋๋ฐ, ๋ฐํ๊ฐ์ด ๊ฐ์ ๊ฒ ๊ฐ์์์! ํธ๋ค๋ฌ๋ก ๋นผ์ ์ฒ๋ฆฌํ๋ฉด ๋ ์ ์ฐํ ์ฝ๋๊ฐ ๋์ง ์์๊น ์ถ์ต๋๋ค |
@@ -0,0 +1,63 @@
+package store.custom.model.order;
+
+public class OrderedProduct {
+ private final String name;
+ private int quantity;
+ private int totalPrice;
+ private String promotion;
+ private int buy;
+ private int get;
+
+ public OrderedProduct(String name, int quantity, int totalPrice, String promotion, int buy, int get) {
+ this.name = name;
+ this.quantity = quantity;
+ this.totalPrice = totalPrice;
+ this.promotion = promotion;
+ this.buy = buy;
+ this.get = get;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public int getTotalPrice() {
+ return totalPrice;
+ }
+
+ public void setTotalPrice(int totalPrice) {
+ this.totalPrice = totalPrice;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+
+ public void setPromotion(String promotion) {
+ this.promotion = promotion;
+ }
+
+ public int getBuy() {
+ return buy;
+ }
+
+ public void setBuy(int buy) {
+ this.buy = buy;
+ }
+
+ public int getGet() {
+ return get;
+ }
+
+ public void setGet(int get) {
+ this.get = get;
+ }
+}
\ No newline at end of file | Java | ๋์ํฉ๋๋ค! ๊ฐ์ฒด๊ฐ ์๋์ ์ธ ๊ฒ์ด ์๋, '๋ฅ๋์ '์ธ ์ธ์คํด์ค๋ผ๋ ๊ฒ์ ๊ณ ๋ คํ์๋ฉด ๋ ์ข์ ์ฝ๋๊ฐ ๋ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,35 @@
+package store.custom.model.product;
+
+public class Product {
+ private final String name;
+ private final int price;
+ private int quantity;
+ private final String promotion;
+
+ public Product(String name, int price, int quantity, String promotion) {
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotion = promotion;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+}
\ No newline at end of file | Java | ๋์ํฉ๋๋ค! |
@@ -0,0 +1,153 @@
+package store.custom.service.editor;
+
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDate;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotion;
+import store.custom.model.promotion.Promotions;
+
+public class OrderSheetEditor {
+ // ์ฃผ๋ฌธ์ ์ด๊ธฐ ์ค์ : ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ ๋ณด ์ถ๊ฐ
+ public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) {
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ถ๊ฐ
+ setPromotionDetails(orderProduct, promotions); // ํ๋ก๋ชจ์
๊ด๋ จ ์ ๋ณด ์ถ๊ฐ
+ }
+ }
+
+ private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) {
+ Product product = findOrderedProduct(orderProduct, productCatalog);
+ if (product != null) {
+ orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity());
+ setPromotionIfExist(orderProduct, product);
+ }
+ }
+
+ private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) {
+ for (Product product : productCatalog.getProducts()) {
+ if (product.getName().equals(orderProduct.getName())) {
+ return product;
+ }
+ }
+ return null;
+ }
+
+ private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
+ if (product.getPromotion() != null) {
+ orderProduct.setPromotion(product.getPromotion());
+ }
+ }
+
+ private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) {
+ if (orderProduct.getPromotion() != null) {
+ LocalDate today = DateTimes.now().toLocalDate();
+ Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions);
+ if (promotion != null) {
+ applyPromotionDates(orderProduct, today, promotion);
+ applyPromotionBuyAndGet(orderProduct, promotion);
+ }
+ }
+ }
+
+ private Promotion findPromotion(String promotionName, Promotions promotions) {
+ for (Promotion promotion : promotions.getPromotions()) {
+ if (promotion.getName().equals(promotionName)) {
+ return promotion;
+ }
+ }
+ return null;
+ }
+
+ private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) {
+ if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) {
+ orderProduct.setPromotion(BEFORE_PROMOTION_START);
+ }
+
+ if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) {
+ orderProduct.setPromotion(AFTER_PROMOTION_END);
+ }
+ }
+
+ private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) {
+ String orderProductPromotion = orderProduct.getPromotion();
+
+ if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START)
+ && !orderProductPromotion.equals(AFTER_PROMOTION_END)) {
+ orderProduct.setBuy(promotion.getBuy());
+ orderProduct.setGet(promotion.getGet());
+ }
+ }
+
+ // ์ฃผ๋ฌธ์ ์์ : ์ฃผ๋ฌธ ์๋์ ๋ค๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๋ฐ์
+ public void applyResponseForNoPromotion(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processNonPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์๋ ๊ฐ์
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ๊ฐ์์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ }
+
+ public void applyResponseForFreeProduct(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processAdditionalPromotionApplied(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processAdditionalPromotionNotApplied(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์๋ ์ฆ๊ฐ
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ์ฆ๊ฐ์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1));
+ }
+
+ private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+}
\ No newline at end of file | Java | ๋์ํฉ๋๋ค! ํ์ฌ ํด๋์ค์์ ์ญํ ์ ๋ง์ด ๊ฐ๊ณ ์์ด์ ๊ฐ์ฒด ๋ด๋ถ์์ ์ฒ๋ฆฌํ๊ฑฐ๋, ๋ถ๋ฆฌํ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,190 @@
+package store.custom.controller;
+
+import static store.custom.constants.StringConstants.PRODUCTS_FILE_PATH;
+import static store.custom.constants.StringConstants.PROMOTIONS_FILE_PATH;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import java.util.List;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.PromotionResult.PromotionResults;
+import store.custom.model.ReceiptDetails;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotions;
+import store.custom.service.editor.OrderSheetEditor;
+import store.custom.service.editor.ProductsEditor;
+import store.custom.service.filehandler.FileReader;
+import store.custom.service.maker.PromotionResultMaker;
+import store.custom.service.maker.ReceiptDetailsMaker;
+import store.custom.service.parser.OrderParser;
+import store.custom.service.parser.ProductParser;
+import store.custom.service.parser.PromotionParser;
+import store.custom.service.parser.ResponseParser;
+import store.custom.view.InputView;
+import store.custom.view.OutputView;
+
+public class StoreController {
+ private final OrderSheetEditor orderSheetEditor;
+ private final PromotionResultMaker promotionResultsMaker;
+ private final ReceiptDetailsMaker receiptDetailsMaker;
+ private final OrderParser orderParser;
+ private final ResponseParser responseParser;
+
+ private final InputView inputView;
+ private final OutputView outputView;
+
+ public StoreController(InputView inputView, OutputView outputView) {
+ this.orderSheetEditor = new OrderSheetEditor();
+ this.promotionResultsMaker = new PromotionResultMaker();
+ this.receiptDetailsMaker = new ReceiptDetailsMaker();
+ this.orderParser = new OrderParser();
+ this.responseParser = new ResponseParser();
+
+ this.inputView = inputView;
+ this.outputView = outputView;
+ }
+
+ public void start() {
+ Products productCatalog = setUpProductCatalog();
+ Promotions promotionCatalog = setUpPromotionCatalog();
+
+ String repeat = RESPONSE_YES;
+ while (RESPONSE_YES.equals(repeat)) {
+ outputView.displayInventoryStatus(productCatalog);
+ repeat = handleStoreOrder(productCatalog, promotionCatalog);
+ }
+ }
+
+ // ํธ์์ ํ๋ก๊ทธ๋จ ์ด๊ธฐ ์
์
๋ฉ์๋
+ private Products setUpProductCatalog() {
+ List<String> productsLines = FileReader.run(PRODUCTS_FILE_PATH);
+ Products productCatalog = ProductParser.run(productsLines);
+ return ProductsEditor.inspectProductCatalog(productCatalog);
+ }
+
+ private Promotions setUpPromotionCatalog() {
+ List<String> promotionLines = FileReader.run(PROMOTIONS_FILE_PATH);
+ return PromotionParser.run(promotionLines);
+ }
+
+ // ์ฃผ๋ฌธ์์ฒญ ์ฒ๋ฆฌ ๋ฉ์๋
+ private String handleStoreOrder(Products productCatalog, Promotions promotionCatalog) {
+ OrderSheet orderSheet = handleOrderSheet(productCatalog, promotionCatalog);
+ ProductsEditor.adjustInventoryForOrders(orderSheet, productCatalog); // ์ฃผ๋ฌธ์์ ๋ง์ถฐ ์ฌ๊ณ ๊ด๋ฆฌ
+ ReceiptDetails receiptDetails = receiptDetailsMaker.run(orderSheet, inputResponseForMembership());
+
+ outputView.displayReceipt(orderSheet, receiptDetails); // ๋ ์ํผ ์ถ๋ ฅ
+ return inputResponseForAdditionalPurchase();
+ }
+
+ // ์ฃผ๋ฌธ์ ์ฒ๋ฆฌ ๋ฉ์๋
+ private OrderSheet handleOrderSheet(Products productCatalog, Promotions promotionCatalog) {
+ OrderSheet orderSheet = inputOrderRequest(productCatalog);
+ orderSheetEditor.addPromotionInfo(productCatalog, promotionCatalog, orderSheet);
+
+ PromotionResults promotionResults =
+ promotionResultsMaker.createPromotionResults(productCatalog, orderSheet); // ํ๋ก๋ชจ์
๊ฒฐ๊ณผ์ง ๋ง๋ค๊ธฐ
+ handlePromotionResults(orderSheet, promotionResults); // ํ๋ก๋ชจ์
๊ฒฐ๊ณผ์ง๋ฅผ ์ฃผ๋ฌธ์์ ๋ฐ์
+
+ return orderSheet;
+ }
+
+ // ํ๋ก๋ชจ์
ํ์ฌ ์ ์ฉ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ ๋ฉ์๋
+ private void handlePromotionResults(OrderSheet orderSheet, PromotionResults promotionResults) {
+ for (int index = 0; index < promotionResults.getPromotionResultCount(); index++) {
+ PromotionResult promotionResult = promotionResults.getPromotionResultByIndex(index);
+ OrderedProduct orderedProduct = orderSheet.getOrderSheetByIndex(index);
+
+ handleExcludedPromotionProduct(orderedProduct, promotionResult);
+ handleAdditionalFreebie(orderedProduct, promotionResult);
+ handlePromotionProduct(orderedProduct, promotionResult);
+ }
+ }
+
+ private void handleExcludedPromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ String orderedProductName = orderedProduct.getName();
+ int nonPromotionProduct = promotionResult.getNonPromotionProductCount();
+
+ if (nonPromotionProduct > 0) {
+ String responseForNoPromotion = inputResponseForNoPromotion(orderedProductName, nonPromotionProduct);
+ orderSheetEditor.applyResponseForNoPromotion(responseForNoPromotion, promotionResult, orderedProduct);
+ }
+ }
+
+ private void handleAdditionalFreebie(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ String orderedProductName = orderedProduct.getName();
+ int extraPromotionCount = promotionResult.getExtraPromotionCount();
+
+ if (extraPromotionCount > 0) {
+ int additionalFreebie = orderedProduct.getGet();
+ String responseForFreeProduct = inputResponseForFreebie(orderedProductName, additionalFreebie);
+ orderSheetEditor.applyResponseForFreeProduct(responseForFreeProduct, promotionResult, orderedProduct);
+ }
+ }
+
+ private void handlePromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int nonPromotionProduct = promotionResult.getNonPromotionProductCount();
+ int extraPromotionCount = promotionResult.getExtraPromotionCount();
+
+ if (extraPromotionCount == 0 && nonPromotionProduct == 0) {
+ orderSheetEditor.computeTotalWithPromotion(orderedProduct, promotionResult);
+ }
+ }
+
+ // ์ฌ์ฉ์ ์
๋ ฅ ๊ด๋ จ ๋ฉ์๋ (Error ์ ์ฌ์
๋ ฅ)
+ private OrderSheet inputOrderRequest(Products productCatalog) {
+ while (true) {
+ try {
+ String orderRequest = inputView.askForProductsToPurchase();
+ return orderParser.run(orderRequest, productCatalog);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForNoPromotion(String name, int noPromotionProductCount) {
+ while (true) {
+ try {
+ String response = inputView.askForProductWithoutPromotion(name, noPromotionProductCount);
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForFreebie(String name, int additionalFreeProduct) {
+ while (true) {
+ try {
+ String response = inputView.askForFreebie(name, additionalFreeProduct);
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForMembership() {
+ while (true) {
+ try {
+ String response = inputView.askForMembershipDiscount();
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForAdditionalPurchase() {
+ while (true) {
+ try {
+ String response = inputView.askForAdditionalPurchase();
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+}
\ No newline at end of file | Java | ์ธ๋ฑ์ค๋ก ์ ๊ทผํ๊ธฐ ๋ณด๋ค๋,,, ์์๋ก ์ ๊ทผํด๋ ์ข์ง ์์์๊น์? ๋ก์ง์์ผ๋ก๋ ๊ฐ ์์๊ฐ ์์ผ๋ก ๋ค์ด์ค์ง๋ง ๊ทธ๊ฑธ ์ดํดํ๋ ค๋ฉด ์ฝ๋๋ฅผ ๊ณ์ ๋ฐ๋ผ๊ฐ์ผํ๋๊น,,,
๋ ๊ฐ์ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ ๊ทผํด์ผํ๋ค๋๊ฒ ๋ฌธ์ ๊ธด ํ๋ฐ, ์ด์ฐจํผ ๋ค์๋ ๋ ๊ฐ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ณ์ ์ฐ๊ณ , ๋ ๋ฐ๋ณต์ํค๋ ๊ฒ ๊ฐ์์promotionResults๋ฅผ ๋ฝ์ ๋ orderSheet์ ์์์ ๋ด์ฉ์ ํฌํจํ๋ ๋ฌด์ธ๊ฐ๋ก ๋งคํ์ํค๊ฑฐ๋ ํ ์๋ ์์ง ์์๊น์? |
@@ -0,0 +1,58 @@
+package store.custom.service.parser;
+
+import static store.custom.constants.RegexConstants.SINGLE_COMMA;
+import static store.custom.constants.StringConstants.NO_PROMOTION;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class ProductParser {
+ public static Products run(List<String> lines) {
+ List<Product> productCatalog = new ArrayList<>();
+
+ if (lines != null) {
+ parseProductLines(lines, productCatalog);
+ }
+
+ return new Products(productCatalog);
+ }
+
+ private static void parseProductLines(List<String> lines, List<Product> productCatalog) {
+ for (int currentLine = 1; currentLine < lines.size(); currentLine++) {
+ List<String> currentLineParts = List.of(lines.get(currentLine).split(SINGLE_COMMA));
+ Product product = createProduct(currentLineParts);
+ productCatalog.add(product);
+ }
+ }
+
+ private static Product createProduct(List<String> parts) {
+ String name = extractProductName(parts);
+ int price = extractProductPrice(parts);
+ int quantity = extractProductQuantity(parts);
+ String promotion = extractProductPromotion(parts);
+
+ return new Product(name, price, quantity, promotion);
+ }
+
+ private static String extractProductName(List<String> parts) {
+ return parts.get(0).trim();
+ }
+
+ private static int extractProductPrice(List<String> parts) {
+ return Integer.parseInt(parts.get(1).trim());
+ }
+
+ private static int extractProductQuantity(List<String> parts) {
+ return Integer.parseInt(parts.get(2).trim());
+ }
+
+ private static String extractProductPromotion(List<String> parts) {
+ String promotion = parts.get(3).trim();
+ if (promotion.equals(NO_PROMOTION)) {
+ return null;
+ }
+ return promotion;
+ }
+}
\ No newline at end of file | Java | ์๊ฒ๋ ์ธ๋ฑ์ค ์ ๊ทผ๋ณด๋ค ์์ ์ ๊ทผ์ผ๋ก ๋ฐ๊พธ๋๊ฒ ๋ ์ง๊ด์ ์ด๊ณ ํจ์จ์ ์ผ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,112 @@
+package store.custom.service.editor;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class ProductsEditor {
+ // ์ฌ๊ณ ๋ชฉ๋ก ์ด๊ธฐ ์ค์ : ๋ชฉ๋ก์ ํ๋ก๋ชจ์
์ํ๋ง ์๋ ๊ฒฝ์ฐ ์ผ๋ฐ ์ํ ์ถ๊ฐ
+ public static Products inspectProductCatalog(Products originalCatalog) {
+ List<Product> resultCatalog = new ArrayList<>();
+ int currentIndex;
+ for (currentIndex = 0; currentIndex < originalCatalog.getProductsSize() - 1; currentIndex++) {
+ currentIndex += inspectProduct(originalCatalog, resultCatalog, currentIndex);
+ }
+ addLastProduct(originalCatalog, resultCatalog, currentIndex);
+
+ return new Products(resultCatalog);
+ }
+
+ private static int inspectProduct(Products original, List<Product> result, int currentIndex) {
+ Product currentProduct = original.getProductByIndex(currentIndex);
+ Product nextProduct = original.getProductByIndex(currentIndex + 1);
+
+ if (hasSameNameAndPrice(result, currentProduct, nextProduct)) {
+ return 1; // ๋๊ฐ์ ์ํ์ ๊ฒ์ฌ์๋ฃํ์ผ๋ฏ๋ก index ์ ๊ฐ 1 ์ฆ๊ฐ
+ }
+
+ return addProductConsideringPromotion(currentProduct, result);
+ }
+
+ private static boolean hasSameNameAndPrice(List<Product> result, Product currentProduct, Product nextProduct) {
+ if (currentProduct.getName().equals(nextProduct.getName())
+ && currentProduct.getPrice() == nextProduct.getPrice()) {
+ result.add(currentProduct);
+ result.add(nextProduct);
+ return true;
+ }
+ return false;
+ }
+
+ private static int addProductConsideringPromotion(Product currentProduct, List<Product> result) {
+ result.add(currentProduct);
+
+ if (currentProduct.getPromotion() != null) {
+ result.add(createZeroStockProduct(currentProduct)); // ์ฌ๊ณ ๊ฐ 0์ธ ์ ํ ์ถ๊ฐ
+ }
+ return 0;
+ }
+
+ private static Product createZeroStockProduct(Product product) {
+ return new Product(product.getName(), product.getPrice(), 0, null);
+ }
+
+ private static void addLastProduct(Products originalCatalog, List<Product> result, int currentIndex) {
+ if (currentIndex == originalCatalog.getProductsSize() - 1) {
+ Product lastProduct = originalCatalog.getProductByIndex(originalCatalog.getProductsSize() - 1);
+ addProductConsideringPromotion(lastProduct, result);
+ }
+ }
+
+ // ์ฃผ๋ฌธ ์๋์ ๋ฐ๋ฅธ ์ฌ๊ณ ์กฐ์
+ public static void adjustInventoryForOrders(OrderSheet orderSheet, Products productCatalog) {
+ for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) {
+ processOrderedProduct(orderedProduct, productCatalog);
+ }
+ }
+
+ private static void processOrderedProduct(OrderedProduct orderedProduct, Products productCatalog) {
+ int remainQuantity = orderedProduct.getQuantity();
+ for (Product product : productCatalog.getProducts()) {
+ if (remainQuantity == 0) {
+ break;
+ }
+ remainQuantity = updateProductQuantity(orderedProduct, product, remainQuantity);
+ }
+ }
+
+ private static int updateProductQuantity(OrderedProduct orderedProduct, Product product, int remainQuantity) {
+ if (product.getName().equals(orderedProduct.getName())) {
+ if (product.getPromotion() != null) {
+ return calculateRemainingQuantityWithPromotion(product, remainQuantity);
+ }
+ return calculateRemainingQuantityWithoutPromotion(product, remainQuantity);
+ }
+ return remainQuantity;
+ }
+
+ private static int calculateRemainingQuantityWithPromotion(Product product, int remainQuantity) {
+ int productQuantity = product.getQuantity();
+
+ if (remainQuantity >= productQuantity) {
+ product.setQuantity(0);
+ return remainQuantity - productQuantity;
+ }
+ product.setQuantity(productQuantity - remainQuantity);
+ return 0;
+ }
+
+ private static int calculateRemainingQuantityWithoutPromotion(Product product, int remainQuantity) {
+ int productQuantity = product.getQuantity();
+
+ if (remainQuantity == productQuantity) {
+ product.setQuantity(0);
+ return 0;
+ }
+ product.setQuantity(productQuantity - remainQuantity);
+ return 0;
+ }
+}
\ No newline at end of file | Java | ์ข ๋ ๋ช
๋ฃํ ์ฝ๋ ํ๋ฆ์ด ์์๊น์? ์... ArrayList์ฌ์ ๋ ๋นํจ์จ์ ์ผ ์๋ ์์ง๋ง promotion์ด ์๋ ์ํ๋ค๋ง ๋ฐ๋ณต์ํค๋ฉด์ ์ผ๋ฐ ์ฌ๊ณ ๋ฅผ ๊ฒ์ํ๊ณ ์์ผ๋ฉด ์์ฑํ๋...?๊ฒ๋ ์ฌ๋ฐ์ง ์์๊น์? |
@@ -0,0 +1,29 @@
+package store.custom.model.product;
+
+import static store.custom.validator.CustomErrorMessages.INVALID_INDEX;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Products {
+ private final List<Product> productCatalog;
+
+ public Products(List<Product> productCatalog) {
+ this.productCatalog = new ArrayList<>(productCatalog);
+ }
+
+ public List<Product> getProducts() {
+ return productCatalog;
+ }
+
+ public int getProductsSize() {
+ return productCatalog.size();
+ }
+
+ public Product getProductByIndex(int index) {
+ if (index < 0 || index >= productCatalog.size()) {
+ throw new IndexOutOfBoundsException(INVALID_INDEX + index);
+ }
+ return productCatalog.get(index);
+ }
+}
\ No newline at end of file | Java | ์ ๊ฐ ์ผ๊ธ์ปฌ๋ ์
์ ์ ๋ชจ๋ฅด๊ธด ํ์ง๋ง,,, ์ ๊ฐ ์ดํดํ ๋ฐ๋ก๋ ์ข ๋ ๋ฉฑํํ๊ฒ ์บก์ํํ๋๊ฒ ์ฅ์ ์ธ ๊ฒ์ผ๋ก ์ดํดํ๊ณ ์์ด์! ๊ทผ๋ฐ ์ผ๊ธ ์ปฌ๋์
์ ๊ตฌํ์ด ๋ด๋ถ์ ์์๋ฅผ ์ธ๋ถ๋ก ๋ฐ์ถ์ํค๋ ๊ฒ๋ง ์๋๊ฒ ์คํ๋ ค ์บก์ํ, ์๋ํ๋ฅผ ํด์น ์ ์์ง ์์๊น์? ์ธ๋ถ์์ product๋ products์์ ํ์ฉํ๋ ์ฌ๋ฌ ๋ฉ์๋๋ค ์ญ์ ๋ชจ๋ธ์ ์ฑ
์ ์๋์ ์์ผ๋๊น ๋ด๋ถ๋ก ํฌํจ์ํค๋ ๊ฒ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,190 @@
+package store.custom.controller;
+
+import static store.custom.constants.StringConstants.PRODUCTS_FILE_PATH;
+import static store.custom.constants.StringConstants.PROMOTIONS_FILE_PATH;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import java.util.List;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.PromotionResult.PromotionResults;
+import store.custom.model.ReceiptDetails;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotions;
+import store.custom.service.editor.OrderSheetEditor;
+import store.custom.service.editor.ProductsEditor;
+import store.custom.service.filehandler.FileReader;
+import store.custom.service.maker.PromotionResultMaker;
+import store.custom.service.maker.ReceiptDetailsMaker;
+import store.custom.service.parser.OrderParser;
+import store.custom.service.parser.ProductParser;
+import store.custom.service.parser.PromotionParser;
+import store.custom.service.parser.ResponseParser;
+import store.custom.view.InputView;
+import store.custom.view.OutputView;
+
+public class StoreController {
+ private final OrderSheetEditor orderSheetEditor;
+ private final PromotionResultMaker promotionResultsMaker;
+ private final ReceiptDetailsMaker receiptDetailsMaker;
+ private final OrderParser orderParser;
+ private final ResponseParser responseParser;
+
+ private final InputView inputView;
+ private final OutputView outputView;
+
+ public StoreController(InputView inputView, OutputView outputView) {
+ this.orderSheetEditor = new OrderSheetEditor();
+ this.promotionResultsMaker = new PromotionResultMaker();
+ this.receiptDetailsMaker = new ReceiptDetailsMaker();
+ this.orderParser = new OrderParser();
+ this.responseParser = new ResponseParser();
+
+ this.inputView = inputView;
+ this.outputView = outputView;
+ }
+
+ public void start() {
+ Products productCatalog = setUpProductCatalog();
+ Promotions promotionCatalog = setUpPromotionCatalog();
+
+ String repeat = RESPONSE_YES;
+ while (RESPONSE_YES.equals(repeat)) {
+ outputView.displayInventoryStatus(productCatalog);
+ repeat = handleStoreOrder(productCatalog, promotionCatalog);
+ }
+ }
+
+ // ํธ์์ ํ๋ก๊ทธ๋จ ์ด๊ธฐ ์
์
๋ฉ์๋
+ private Products setUpProductCatalog() {
+ List<String> productsLines = FileReader.run(PRODUCTS_FILE_PATH);
+ Products productCatalog = ProductParser.run(productsLines);
+ return ProductsEditor.inspectProductCatalog(productCatalog);
+ }
+
+ private Promotions setUpPromotionCatalog() {
+ List<String> promotionLines = FileReader.run(PROMOTIONS_FILE_PATH);
+ return PromotionParser.run(promotionLines);
+ }
+
+ // ์ฃผ๋ฌธ์์ฒญ ์ฒ๋ฆฌ ๋ฉ์๋
+ private String handleStoreOrder(Products productCatalog, Promotions promotionCatalog) {
+ OrderSheet orderSheet = handleOrderSheet(productCatalog, promotionCatalog);
+ ProductsEditor.adjustInventoryForOrders(orderSheet, productCatalog); // ์ฃผ๋ฌธ์์ ๋ง์ถฐ ์ฌ๊ณ ๊ด๋ฆฌ
+ ReceiptDetails receiptDetails = receiptDetailsMaker.run(orderSheet, inputResponseForMembership());
+
+ outputView.displayReceipt(orderSheet, receiptDetails); // ๋ ์ํผ ์ถ๋ ฅ
+ return inputResponseForAdditionalPurchase();
+ }
+
+ // ์ฃผ๋ฌธ์ ์ฒ๋ฆฌ ๋ฉ์๋
+ private OrderSheet handleOrderSheet(Products productCatalog, Promotions promotionCatalog) {
+ OrderSheet orderSheet = inputOrderRequest(productCatalog);
+ orderSheetEditor.addPromotionInfo(productCatalog, promotionCatalog, orderSheet);
+
+ PromotionResults promotionResults =
+ promotionResultsMaker.createPromotionResults(productCatalog, orderSheet); // ํ๋ก๋ชจ์
๊ฒฐ๊ณผ์ง ๋ง๋ค๊ธฐ
+ handlePromotionResults(orderSheet, promotionResults); // ํ๋ก๋ชจ์
๊ฒฐ๊ณผ์ง๋ฅผ ์ฃผ๋ฌธ์์ ๋ฐ์
+
+ return orderSheet;
+ }
+
+ // ํ๋ก๋ชจ์
ํ์ฌ ์ ์ฉ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ ๋ฉ์๋
+ private void handlePromotionResults(OrderSheet orderSheet, PromotionResults promotionResults) {
+ for (int index = 0; index < promotionResults.getPromotionResultCount(); index++) {
+ PromotionResult promotionResult = promotionResults.getPromotionResultByIndex(index);
+ OrderedProduct orderedProduct = orderSheet.getOrderSheetByIndex(index);
+
+ handleExcludedPromotionProduct(orderedProduct, promotionResult);
+ handleAdditionalFreebie(orderedProduct, promotionResult);
+ handlePromotionProduct(orderedProduct, promotionResult);
+ }
+ }
+
+ private void handleExcludedPromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ String orderedProductName = orderedProduct.getName();
+ int nonPromotionProduct = promotionResult.getNonPromotionProductCount();
+
+ if (nonPromotionProduct > 0) {
+ String responseForNoPromotion = inputResponseForNoPromotion(orderedProductName, nonPromotionProduct);
+ orderSheetEditor.applyResponseForNoPromotion(responseForNoPromotion, promotionResult, orderedProduct);
+ }
+ }
+
+ private void handleAdditionalFreebie(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ String orderedProductName = orderedProduct.getName();
+ int extraPromotionCount = promotionResult.getExtraPromotionCount();
+
+ if (extraPromotionCount > 0) {
+ int additionalFreebie = orderedProduct.getGet();
+ String responseForFreeProduct = inputResponseForFreebie(orderedProductName, additionalFreebie);
+ orderSheetEditor.applyResponseForFreeProduct(responseForFreeProduct, promotionResult, orderedProduct);
+ }
+ }
+
+ private void handlePromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int nonPromotionProduct = promotionResult.getNonPromotionProductCount();
+ int extraPromotionCount = promotionResult.getExtraPromotionCount();
+
+ if (extraPromotionCount == 0 && nonPromotionProduct == 0) {
+ orderSheetEditor.computeTotalWithPromotion(orderedProduct, promotionResult);
+ }
+ }
+
+ // ์ฌ์ฉ์ ์
๋ ฅ ๊ด๋ จ ๋ฉ์๋ (Error ์ ์ฌ์
๋ ฅ)
+ private OrderSheet inputOrderRequest(Products productCatalog) {
+ while (true) {
+ try {
+ String orderRequest = inputView.askForProductsToPurchase();
+ return orderParser.run(orderRequest, productCatalog);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForNoPromotion(String name, int noPromotionProductCount) {
+ while (true) {
+ try {
+ String response = inputView.askForProductWithoutPromotion(name, noPromotionProductCount);
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForFreebie(String name, int additionalFreeProduct) {
+ while (true) {
+ try {
+ String response = inputView.askForFreebie(name, additionalFreeProduct);
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForMembership() {
+ while (true) {
+ try {
+ String response = inputView.askForMembershipDiscount();
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForAdditionalPurchase() {
+ while (true) {
+ try {
+ String response = inputView.askForAdditionalPurchase();
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+}
\ No newline at end of file | Java | ํธ๋ค๋ฌ ์๊ฐ์ ๋ชปํ๋ค์! ์ข์ ๋ฐฉ๋ฒ ์๋ ค์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,63 @@
+package store.custom.model.order;
+
+public class OrderedProduct {
+ private final String name;
+ private int quantity;
+ private int totalPrice;
+ private String promotion;
+ private int buy;
+ private int get;
+
+ public OrderedProduct(String name, int quantity, int totalPrice, String promotion, int buy, int get) {
+ this.name = name;
+ this.quantity = quantity;
+ this.totalPrice = totalPrice;
+ this.promotion = promotion;
+ this.buy = buy;
+ this.get = get;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public int getTotalPrice() {
+ return totalPrice;
+ }
+
+ public void setTotalPrice(int totalPrice) {
+ this.totalPrice = totalPrice;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+
+ public void setPromotion(String promotion) {
+ this.promotion = promotion;
+ }
+
+ public int getBuy() {
+ return buy;
+ }
+
+ public void setBuy(int buy) {
+ this.buy = buy;
+ }
+
+ public int getGet() {
+ return get;
+ }
+
+ public void setGet(int get) {
+ this.get = get;
+ }
+}
\ No newline at end of file | Java | ๊ฐ์ฒด์ ๋ํ ๊ณต๋ถ๋ฅผ ๋ ํด๋ด์ผ๊ฒ ์ต๋๋ค! ์๋ ค์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,35 @@
+package store.custom.model.product;
+
+public class Product {
+ private final String name;
+ private final int price;
+ private int quantity;
+ private final String promotion;
+
+ public Product(String name, int price, int quantity, String promotion) {
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotion = promotion;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+}
\ No newline at end of file | Java | ๊ทธ ์๊ฐ์ ๋ชปํด๋ดค๋ค์! ํ์คํ ํ๋ก๋ชจ์
๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ๋ค์! |
@@ -0,0 +1,153 @@
+package store.custom.service.editor;
+
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDate;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotion;
+import store.custom.model.promotion.Promotions;
+
+public class OrderSheetEditor {
+ // ์ฃผ๋ฌธ์ ์ด๊ธฐ ์ค์ : ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ ๋ณด ์ถ๊ฐ
+ public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) {
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ถ๊ฐ
+ setPromotionDetails(orderProduct, promotions); // ํ๋ก๋ชจ์
๊ด๋ จ ์ ๋ณด ์ถ๊ฐ
+ }
+ }
+
+ private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) {
+ Product product = findOrderedProduct(orderProduct, productCatalog);
+ if (product != null) {
+ orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity());
+ setPromotionIfExist(orderProduct, product);
+ }
+ }
+
+ private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) {
+ for (Product product : productCatalog.getProducts()) {
+ if (product.getName().equals(orderProduct.getName())) {
+ return product;
+ }
+ }
+ return null;
+ }
+
+ private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
+ if (product.getPromotion() != null) {
+ orderProduct.setPromotion(product.getPromotion());
+ }
+ }
+
+ private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) {
+ if (orderProduct.getPromotion() != null) {
+ LocalDate today = DateTimes.now().toLocalDate();
+ Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions);
+ if (promotion != null) {
+ applyPromotionDates(orderProduct, today, promotion);
+ applyPromotionBuyAndGet(orderProduct, promotion);
+ }
+ }
+ }
+
+ private Promotion findPromotion(String promotionName, Promotions promotions) {
+ for (Promotion promotion : promotions.getPromotions()) {
+ if (promotion.getName().equals(promotionName)) {
+ return promotion;
+ }
+ }
+ return null;
+ }
+
+ private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) {
+ if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) {
+ orderProduct.setPromotion(BEFORE_PROMOTION_START);
+ }
+
+ if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) {
+ orderProduct.setPromotion(AFTER_PROMOTION_END);
+ }
+ }
+
+ private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) {
+ String orderProductPromotion = orderProduct.getPromotion();
+
+ if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START)
+ && !orderProductPromotion.equals(AFTER_PROMOTION_END)) {
+ orderProduct.setBuy(promotion.getBuy());
+ orderProduct.setGet(promotion.getGet());
+ }
+ }
+
+ // ์ฃผ๋ฌธ์ ์์ : ์ฃผ๋ฌธ ์๋์ ๋ค๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๋ฐ์
+ public void applyResponseForNoPromotion(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processNonPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์๋ ๊ฐ์
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ๊ฐ์์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ }
+
+ public void applyResponseForFreeProduct(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processAdditionalPromotionApplied(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processAdditionalPromotionNotApplied(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์๋ ์ฆ๊ฐ
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ์ฆ๊ฐ์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1));
+ }
+
+ private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+}
\ No newline at end of file | Java | OrdersheetEditor์ ์ญํ ์ ์ด๋ฏธ ์์ฑ๋ ์ฃผ๋ฌธ์์ ๋ณ๊ฒฝ(์ฃผ๋ฌธ์์ ์ด๊ธฐ ์ค์ , ํ๋ก๋ชจ์
๋ด์ฉ ์ ์ฉ)์ผ๋ก ์๊ฐํ๊ณ ์์ฑํ์๋๋ฐ, ๊ฐ์ฒด๊ฐ ๊ฐ์ง ์ ์๋ ์ญํ ์ ๋ฒ์๋ฅผ ์ ํ์
ํ์ง ๋ชปํด ๊ธฐ๋ณธ์ ์ธ ๊ฒ์ ์ ์ธํ๊ณ editor์ ๋ชจ๋ ์์ฑํ์์ต๋๋ค. ๋ชจ๋ธ ๋ด๋ถ์์ ์ฑ
์์ ๊ฐ์ ธ๊ฐ ์ ์๋ ๋ถ๋ถ์ ๊ณ ๋ฏผํด ๋ณด์์ผ๊ฒ ์ต๋๋ค. ์ข์ ์๊ฒฌ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,153 @@
+package store.custom.service.editor;
+
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDate;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotion;
+import store.custom.model.promotion.Promotions;
+
+public class OrderSheetEditor {
+ // ์ฃผ๋ฌธ์ ์ด๊ธฐ ์ค์ : ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ ๋ณด ์ถ๊ฐ
+ public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) {
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ถ๊ฐ
+ setPromotionDetails(orderProduct, promotions); // ํ๋ก๋ชจ์
๊ด๋ จ ์ ๋ณด ์ถ๊ฐ
+ }
+ }
+
+ private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) {
+ Product product = findOrderedProduct(orderProduct, productCatalog);
+ if (product != null) {
+ orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity());
+ setPromotionIfExist(orderProduct, product);
+ }
+ }
+
+ private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) {
+ for (Product product : productCatalog.getProducts()) {
+ if (product.getName().equals(orderProduct.getName())) {
+ return product;
+ }
+ }
+ return null;
+ }
+
+ private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
+ if (product.getPromotion() != null) {
+ orderProduct.setPromotion(product.getPromotion());
+ }
+ }
+
+ private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) {
+ if (orderProduct.getPromotion() != null) {
+ LocalDate today = DateTimes.now().toLocalDate();
+ Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions);
+ if (promotion != null) {
+ applyPromotionDates(orderProduct, today, promotion);
+ applyPromotionBuyAndGet(orderProduct, promotion);
+ }
+ }
+ }
+
+ private Promotion findPromotion(String promotionName, Promotions promotions) {
+ for (Promotion promotion : promotions.getPromotions()) {
+ if (promotion.getName().equals(promotionName)) {
+ return promotion;
+ }
+ }
+ return null;
+ }
+
+ private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) {
+ if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) {
+ orderProduct.setPromotion(BEFORE_PROMOTION_START);
+ }
+
+ if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) {
+ orderProduct.setPromotion(AFTER_PROMOTION_END);
+ }
+ }
+
+ private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) {
+ String orderProductPromotion = orderProduct.getPromotion();
+
+ if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START)
+ && !orderProductPromotion.equals(AFTER_PROMOTION_END)) {
+ orderProduct.setBuy(promotion.getBuy());
+ orderProduct.setGet(promotion.getGet());
+ }
+ }
+
+ // ์ฃผ๋ฌธ์ ์์ : ์ฃผ๋ฌธ ์๋์ ๋ค๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๋ฐ์
+ public void applyResponseForNoPromotion(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processNonPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์๋ ๊ฐ์
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ๊ฐ์์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ }
+
+ public void applyResponseForFreeProduct(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processAdditionalPromotionApplied(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processAdditionalPromotionNotApplied(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์๋ ์ฆ๊ฐ
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ์ฆ๊ฐ์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1));
+ }
+
+ private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+}
\ No newline at end of file | Java | ์นญ์ฐฌํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,369 @@
+# 4์ฃผ ์ฐจ - ํธ์์
+
+---
+## ๊ธฐ๋ฅ ์๊ตฌ์ฌํญ
+
+---
+
+๊ตฌ๋งค์์ ํ ์ธ ํํ๊ณผ ์ฌ๊ณ ์ํฉ์ ๊ณ ๋ คํ์ฌ ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ๊ณ์ฐํ๊ณ ์๋ดํ๋ ๊ฒฐ์ ์์คํ
์ ๊ตฌํํ๋ค.
+
+* ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ์ํ์ ๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ธฐ๋ฐ์ผ๋ก ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ๊ณ์ฐํ๋ค.
+ * ์ด๊ตฌ๋งค์ก์ ์ํ๋ณ ๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ณฑํ์ฌ ๊ณ์ฐํ๋ฉฐ, ํ๋ก๋ชจ์
๋ฐ ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฑ
์ ๋ฐ์ํ์ฌ ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ์ฐ์ถํ๋ค.
+* ๊ตฌ๋งค ๋ด์ญ๊ณผ ์ฐ์ถํ ๊ธ์ก ์ ๋ณด๋ฅผ ์์์ฆ์ผ๋ก ์ถ๋ ฅํ๋ค.
+* ์์์ฆ ์ถ๋ ฅ ํ ์ถ๊ฐ ๊ตฌ๋งค๋ฅผ ์งํํ ์ง ๋๋ ์ข
๋ฃํ ์ง๋ฅผ ์ ํํ ์ ์๋ค.
+* ์ฌ์ฉ์๊ฐ ์๋ชป๋ ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ ``IllegalArgumentException``๋ฅผ ๋ฐ์์ํค๊ณ , "[ERROR]"๋ก ์์ํ๋ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ ํ ๊ทธ ๋ถ๋ถ๋ถํฐ ์
๋ ฅ์ ๋ค์ ๋ฐ๋๋ค.
+ * ``Exception``์ด ์๋ ``IllegalArgumentException``, ``IllegalStateException`` ๋ฑ๊ณผ ๊ฐ์ ๋ช
ํํ ์ ํ์ ์ฒ๋ฆฌํ๋ค.
+
+
+**์ฌ๊ณ ๊ด๋ฆฌ**
+* ๊ฐ ์ํ์ ์ฌ๊ณ ์๋์ ๊ณ ๋ คํ์ฌ ๊ฒฐ์ ๊ฐ๋ฅ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
+* ๊ณ ๊ฐ์ด ์ํ์ ๊ตฌ๋งคํ ๋๋ง๋ค, ๊ฒฐ์ ๋ ์๋๋งํผ ํด๋น ์ํ์ ์ฌ๊ณ ์์ ์ฐจ๊ฐํ์ฌ ์๋์ ๊ด๋ฆฌํ๋ค.
+* ์ฌ๊ณ ๋ฅผ ์ฐจ๊ฐํจ์ผ๋ก์จ ์์คํ
์ ์ต์ ์ฌ๊ณ ์ํ๋ฅผ ์ ์งํ๋ฉฐ, ๋ค์ ๊ณ ๊ฐ์ด ๊ตฌ๋งคํ ๋ ์ ํํ ์ฌ๊ณ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ค.
+
+
+**ํ๋ก๋ชจ์
ํ ์ธ**
+* ์ค๋ ๋ ์ง๊ฐ ํ๋ก๋ชจ์
๊ธฐ๊ฐ ๋ด์ ํฌํจ๋ ๊ฒฝ์ฐ์๋ง ํ ์ธ์ ์ ์ฉํ๋ค.
+* ํ๋ก๋ชจ์
์ N๊ฐ ๊ตฌ๋งค ์ 1๊ฐ ๋ฌด๋ฃ ์ฆ์ (Buy N Get 1 Free)์ ํํ๋ก ์งํ๋๋ค.
+* 1+1 ๋๋ 2+1 ํ๋ก๋ชจ์
์ด ๊ฐ๊ฐ ์ง์ ๋ ์ํ์ ์ ์ฉ๋๋ฉฐ, ๋์ผ ์ํ์ ์ฌ๋ฌ ํ๋ก๋ชจ์
์ด ์ ์ฉ๋์ง ์๋๋ค.
+* ํ๋ก๋ชจ์
ํํ์ ํ๋ก๋ชจ์
์ฌ๊ณ ๋ด์์๋ง ์ ์ฉํ ์ ์๋ค.
+* ํ๋ก๋ชจ์
๊ธฐ๊ฐ ์ค์ด๋ผ๋ฉด ํ๋ก๋ชจ์
์ฌ๊ณ ๋ฅผ ์ฐ์ ์ ์ผ๋ก ์ฐจ๊ฐํ๋ฉฐ, ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ ๊ฒฝ์ฐ์๋ ์ผ๋ฐ ์ฌ๊ณ ๋ฅผ ์ฌ์ฉํ๋ค.
+* ํ๋ก๋ชจ์
์ ์ฉ์ด ๊ฐ๋ฅํ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋ณด๋ค ์ ๊ฒ ๊ฐ์ ธ์จ ๊ฒฝ์ฐ, ํ์ํ ์๋์ ์ถ๊ฐ๋ก ๊ฐ์ ธ์ค๋ฉด ํํ์ ๋ฐ์ ์ ์์์ ์๋ดํ๋ค.
+* ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ์ฌ ์ผ๋ถ ์๋์ ํ๋ก๋ชจ์
ํํ ์์ด ๊ฒฐ์ ํด์ผ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ๊ฒ ๋จ์ ์๋ดํ๋ค.
+
+
+**๋ฉค๋ฒ์ญ ํ ์ธ**
+* ๋ฉค๋ฒ์ญ ํ์์ ํ๋ก๋ชจ์
๋ฏธ์ ์ฉ ๊ธ์ก์ 30%๋ฅผ ํ ์ธ๋ฐ๋๋ค.
+* ํ๋ก๋ชจ์
์ ์ฉ ํ ๋จ์ ๊ธ์ก์ ๋ํด ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ ์ฉํ๋ค.
+* ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ต๋ ํ๋๋ 8,000์์ด๋ค.
+
+
+**์์์ฆ ์ถ๋ ฅ**
+* ์์์ฆ์ ๊ณ ๊ฐ์ ๊ตฌ๋งค ๋ด์ญ๊ณผ ํ ์ธ์ ์์ฝํ์ฌ ์ถ๋ ฅํ๋ค.
+* ์์์ฆ ํญ๋ชฉ์ ์๋์ ๊ฐ๋ค.
+ * ๊ตฌ๋งค ์ํ ๋ด์ญ: ๊ตฌ๋งคํ ์ํ๋ช
, ์๋, ๊ฐ๊ฒฉ
+ * ์ฆ์ ์ํ ๋ด์ญ: ํ๋ก๋ชจ์
์ ๋ฐ๋ผ ๋ฌด๋ฃ๋ก ์ ๊ณต๋ ์ฆ์ ์ํ์ ๋ชฉ๋ก
+ * ๊ธ์ก ์ ๋ณด
+ * ์ด๊ตฌ๋งค์ก: ๊ตฌ๋งคํ ์ํ์ ์ด ์๋๊ณผ ์ด ๊ธ์ก
+ * ํ์ฌํ ์ธ: ํ๋ก๋ชจ์
์ ์ํด ํ ์ธ๋ ๊ธ์ก
+ * ๋ฉค๋ฒ์ญํ ์ธ: ๋ฉค๋ฒ์ญ์ ์ํด ์ถ๊ฐ๋ก ํ ์ธ๋ ๊ธ์ก
+ * ๋ด์ค๋: ์ต์ข
๊ฒฐ์ ๊ธ์ก
+* ์์์ฆ์ ๊ตฌ์ฑ ์์๋ฅผ ๋ณด๊ธฐ ์ข๊ฒ ์ ๋ ฌํ์ฌ ๊ณ ๊ฐ์ด ์ฝ๊ฒ ๊ธ์ก๊ณผ ์๋์ ํ์ธํ ์ ์๊ฒ ํ๋ค.
+
+
+### **์
์ถ๋ ฅ ์๊ตฌ ์ฌํญ**
+
+**์
๋ ฅ**
+* ๊ตฌํ์ ํ์ํ ์ํ ๋ชฉ๋ก๊ณผ ํ์ฌ ๋ชฉ๋ก์ ํ์ผ ์
์ถ๋ ฅ์ ํตํด ๋ถ๋ฌ์จ๋ค.
+ * src/main/resources/products.md๊ณผ src/main/resources/promotions.md ํ์ผ์ ์ด์ฉํ๋ค.
+ * ๋ ํ์ผ ๋ชจ๋ ๋ด์ฉ์ ํ์์ ์ ์งํ๋ค๋ฉด ๊ฐ์ ์์ ํ ์ ์๋ค.
+* ๊ตฌ๋งคํ ์ํ๊ณผ ์๋์ ์
๋ ฅ ๋ฐ๋๋ค. ์ํ๋ช
, ์๋์ ํ์ดํ(-)์ผ๋ก, ๊ฐ๋ณ ์ํ์ ๋๊ดํธ([])๋ก ๋ฌถ์ด ์ผํ(,)๋ก ๊ตฌ๋ถํ๋ค.
+~~~
+[์ฝ๋ผ-10],[์ฌ์ด๋ค-3]
+~~~
+
+* ํ๋ก๋ชจ์
์ ์ฉ์ด ๊ฐ๋ฅํ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋ณด๋ค ์ ๊ฒ ๊ฐ์ ธ์จ ๊ฒฝ์ฐ, ๊ทธ ์๋๋งํผ ์ถ๊ฐ ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+ * Y: ์ฆ์ ๋ฐ์ ์ ์๋ ์ํ์ ์ถ๊ฐํ๋ค.
+ * N: ์ฆ์ ๋ฐ์ ์ ์๋ ์ํ์ ์ถ๊ฐํ์ง ์๋๋ค.
+~~~
+Y
+~~~
+
+* ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ์ฌ ์ผ๋ถ ์๋์ ํ๋ก๋ชจ์
ํํ ์์ด ๊ฒฐ์ ํด์ผ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ ์ง ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+ * Y: ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ๋ค.
+ * N: ์ ๊ฐ๋ก ๊ฒฐ์ ํด์ผํ๋ ์๋๋งํผ ์ ์ธํ ํ ๊ฒฐ์ ๋ฅผ ์งํํ๋ค.
+~~~
+Y
+~~~
+
+* ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ ์ฌ๋ถ๋ฅผ ์
๋ ฅ ๋ฐ๋๋ค.
+ * Y: ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ ์ฉํ๋ค.
+ * N: ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ ์ฉํ์ง ์๋๋ค.
+~~~
+Y
+~~~
+
+* ์ถ๊ฐ ๊ตฌ๋งค ์ฌ๋ถ๋ฅผ ์
๋ ฅ ๋ฐ๋๋ค.
+ * Y: ์ฌ๊ณ ๊ฐ ์
๋ฐ์ดํธ๋ ์ํ ๋ชฉ๋ก์ ํ์ธ ํ ์ถ๊ฐ๋ก ๊ตฌ๋งค๋ฅผ ์งํํ๋ค.
+ * N: ๊ตฌ๋งค๋ฅผ ์ข
๋ฃํ๋ค.
+~~~
+Y
+~~~
+
+**์ถ๋ ฅ**
+* ํ์ ์ธ์ฌ์ ํจ๊ป ์ํ๋ช
, ๊ฐ๊ฒฉ, ํ๋ก๋ชจ์
์ด๋ฆ, ์ฌ๊ณ ๋ฅผ ์๋ดํ๋ค. ๋ง์ฝ ์ฌ๊ณ ๊ฐ 0๊ฐ๋ผ๋ฉด ์ฌ๊ณ ์์์ ์ถ๋ ฅํ๋ค.
+~~~
+์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค.
+ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค.
+
+- ์ฝ๋ผ 1,000์ 10๊ฐ ํ์ฐ2+1
+- ์ฝ๋ผ 1,000์ 10๊ฐ
+- ์ฌ์ด๋ค 1,000์ 8๊ฐ ํ์ฐ2+1
+- ์ฌ์ด๋ค 1,000์ 7๊ฐ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ 9๊ฐ MD์ถ์ฒ์ํ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ ์ฌ๊ณ ์์
+- ํ์ฐ์ 1,200์ 5๊ฐ ํ์ฐ2+1
+- ํ์ฐ์ 1,200์ ์ฌ๊ณ ์์
+- ๋ฌผ 500์ 10๊ฐ
+- ๋นํ๋ฏผ์ํฐ 1,500์ 6๊ฐ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ ๋ฐ์งํ ์ธ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ MD์ถ์ฒ์ํ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ
+- ์๋์ง๋ฐ 2,000์ 5๊ฐ
+- ์ ์๋์๋ฝ 6,400์ 8๊ฐ
+- ์ปต๋ผ๋ฉด 1,700์ 1๊ฐ MD์ถ์ฒ์ํ
+- ์ปต๋ผ๋ฉด 1,700์ 10๊ฐ
+
+๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])
+~~~
+
+* ํ๋ก๋ชจ์
์ ์ฉ์ด ๊ฐ๋ฅํ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋งํผ ๊ฐ์ ธ์ค์ง ์์์ ๊ฒฝ์ฐ, ํํ์ ๋ํ ์๋ด ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+~~~
+ํ์ฌ {์ํ๋ช
}์(๋) 1๊ฐ๋ฅผ ๋ฌด๋ฃ๋ก ๋ ๋ฐ์ ์ ์์ต๋๋ค. ์ถ๊ฐํ์๊ฒ ์ต๋๊น? (Y/N)
+~~~
+
+* ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ์ฌ ์ผ๋ถ ์๋์ ํ๋ก๋ชจ์
ํํ ์์ด ๊ฒฐ์ ํด์ผ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ ์ง ์ฌ๋ถ์ ๋ํ ์๋ด ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+~~~
+ํ์ฌ {์ํ๋ช
} {์๋}๊ฐ๋ ํ๋ก๋ชจ์
ํ ์ธ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. ๊ทธ๋๋ ๊ตฌ๋งคํ์๊ฒ ์ต๋๊น? (Y/N)
+~~~
+
+* ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ ์ฌ๋ถ๋ฅผ ํ์ธํ๊ธฐ ์ํด ์๋ด ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๋ค.
+~~~
+๋ฉค๋ฒ์ญ ํ ์ธ์ ๋ฐ์ผ์๊ฒ ์ต๋๊น? (Y/N)
+~~~
+
+* ๊ตฌ๋งค ์ํ ๋ด์ญ, ์ฆ์ ์ํ ๋ด์ญ, ๊ธ์ก ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ค.
+~~~
+===========W ํธ์์ =============
+์ํ๋ช
์๋ ๊ธ์ก
+์ฝ๋ผ 3 3,000
+์๋์ง๋ฐ 5 10,000
+===========์ฆ ์ =============
+์ฝ๋ผ 1
+==============================
+์ด๊ตฌ๋งค์ก 8 13,000
+ํ์ฌํ ์ธ -1,000
+๋ฉค๋ฒ์ญํ ์ธ -3,000
+๋ด์ค๋ 9,000
+~~~
+
+* ์ถ๊ฐ ๊ตฌ๋งค ์ฌ๋ถ๋ฅผ ํ์ธํ๊ธฐ ์ํด ์๋ด ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๋ค.
+~~~
+๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)
+~~~
+
+* ์ฌ์ฉ์๊ฐ ์๋ชป๋ ๊ฐ์ ์
๋ ฅํ์ ๋, "[ERROR]"๋ก ์์ํ๋ ์ค๋ฅ ๋ฉ์์ง์ ํจ๊ป ์ํฉ์ ๋ง๋ ์๋ด๋ฅผ ์ถ๋ ฅํ๋ค.
+ * ๊ตฌ๋งคํ ์ํ๊ณผ ์๋ ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ ๊ฒฝ์ฐ: ```[ERROR] ์ฌ๋ฐ๋ฅด์ง ์์ ํ์์ผ๋ก ์
๋ ฅํ์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.```
+ * ์กด์ฌํ์ง ์๋ ์ํ์ ์
๋ ฅํ ๊ฒฝ์ฐ: ```[ERROR] ์กด์ฌํ์ง ์๋ ์ํ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.```
+ * ๊ตฌ๋งค ์๋์ด ์ฌ๊ณ ์๋์ ์ด๊ณผํ ๊ฒฝ์ฐ: ```[ERROR] ์ฌ๊ณ ์๋์ ์ด๊ณผํ์ฌ ๊ตฌ๋งคํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.```
+ * ๊ธฐํ ์๋ชป๋ ์
๋ ฅ์ ๊ฒฝ์ฐ: ```[ERROR] ์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.```
+
+
+**์คํ ๊ฒฐ๊ณผ ์์**
+~~~
+์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค.
+ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค.
+
+- ์ฝ๋ผ 1,000์ 10๊ฐ ํ์ฐ2+1
+- ์ฝ๋ผ 1,000์ 10๊ฐ
+- ์ฌ์ด๋ค 1,000์ 8๊ฐ ํ์ฐ2+1
+- ์ฌ์ด๋ค 1,000์ 7๊ฐ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ 9๊ฐ MD์ถ์ฒ์ํ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ ์ฌ๊ณ ์์
+- ํ์ฐ์ 1,200์ 5๊ฐ ํ์ฐ2+1
+- ํ์ฐ์ 1,200์ ์ฌ๊ณ ์์
+- ๋ฌผ 500์ 10๊ฐ
+- ๋นํ๋ฏผ์ํฐ 1,500์ 6๊ฐ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ ๋ฐ์งํ ์ธ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ MD์ถ์ฒ์ํ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ
+- ์๋์ง๋ฐ 2,000์ 5๊ฐ
+- ์ ์๋์๋ฝ 6,400์ 8๊ฐ
+- ์ปต๋ผ๋ฉด 1,700์ 1๊ฐ MD์ถ์ฒ์ํ
+- ์ปต๋ผ๋ฉด 1,700์ 10๊ฐ
+
+๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])
+[์ฝ๋ผ-3],[์๋์ง๋ฐ-5]
+
+๋ฉค๋ฒ์ญ ํ ์ธ์ ๋ฐ์ผ์๊ฒ ์ต๋๊น? (Y/N)
+Y
+
+===========W ํธ์์ =============
+์ํ๋ช
์๋ ๊ธ์ก
+์ฝ๋ผ 3 3,000
+์๋์ง๋ฐ 5 10,000
+===========์ฆ ์ =============
+์ฝ๋ผ 1
+==============================
+์ด๊ตฌ๋งค์ก 8 13,000
+ํ์ฌํ ์ธ -1,000
+๋ฉค๋ฒ์ญํ ์ธ -3,000
+๋ด์ค๋ 9,000
+
+๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)
+Y
+
+์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค.
+ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค.
+
+- ์ฝ๋ผ 1,000์ 7๊ฐ ํ์ฐ2+1
+- ์ฝ๋ผ 1,000์ 10๊ฐ
+- ์ฌ์ด๋ค 1,000์ 8๊ฐ ํ์ฐ2+1
+- ์ฌ์ด๋ค 1,000์ 7๊ฐ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ 9๊ฐ MD์ถ์ฒ์ํ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ ์ฌ๊ณ ์์
+- ํ์ฐ์ 1,200์ 5๊ฐ ํ์ฐ2+1
+- ํ์ฐ์ 1,200์ ์ฌ๊ณ ์์
+- ๋ฌผ 500์ 10๊ฐ
+- ๋นํ๋ฏผ์ํฐ 1,500์ 6๊ฐ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ ๋ฐ์งํ ์ธ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ MD์ถ์ฒ์ํ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ
+- ์๋์ง๋ฐ 2,000์ ์ฌ๊ณ ์์
+- ์ ์๋์๋ฝ 6,400์ 8๊ฐ
+- ์ปต๋ผ๋ฉด 1,700์ 1๊ฐ MD์ถ์ฒ์ํ
+- ์ปต๋ผ๋ฉด 1,700์ 10๊ฐ
+
+๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])
+[์ฝ๋ผ-10]
+
+ํ์ฌ ์ฝ๋ผ 4๊ฐ๋ ํ๋ก๋ชจ์
ํ ์ธ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. ๊ทธ๋๋ ๊ตฌ๋งคํ์๊ฒ ์ต๋๊น? (Y/N)
+Y
+
+๋ฉค๋ฒ์ญ ํ ์ธ์ ๋ฐ์ผ์๊ฒ ์ต๋๊น? (Y/N)
+N
+
+===========W ํธ์์ =============
+์ํ๋ช
์๋ ๊ธ์ก
+์ฝ๋ผ 10 10,000
+===========์ฆ ์ =============
+์ฝ๋ผ 2
+==============================
+์ด๊ตฌ๋งค์ก 10 10,000
+ํ์ฌํ ์ธ -2,000
+๋ฉค๋ฒ์ญํ ์ธ -0
+๋ด์ค๋ 8,000
+
+๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)
+Y
+
+์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค.
+ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค.
+
+- ์ฝ๋ผ 1,000์ ์ฌ๊ณ ์์ ํ์ฐ2+1
+- ์ฝ๋ผ 1,000์ 7๊ฐ
+- ์ฌ์ด๋ค 1,000์ 8๊ฐ ํ์ฐ2+1
+- ์ฌ์ด๋ค 1,000์ 7๊ฐ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ 9๊ฐ MD์ถ์ฒ์ํ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ ์ฌ๊ณ ์์
+- ํ์ฐ์ 1,200์ 5๊ฐ ํ์ฐ2+1
+- ํ์ฐ์ 1,200์ ์ฌ๊ณ ์์
+- ๋ฌผ 500์ 10๊ฐ
+- ๋นํ๋ฏผ์ํฐ 1,500์ 6๊ฐ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ ๋ฐ์งํ ์ธ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ MD์ถ์ฒ์ํ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ
+- ์๋์ง๋ฐ 2,000์ ์ฌ๊ณ ์์
+- ์ ์๋์๋ฝ 6,400์ 8๊ฐ
+- ์ปต๋ผ๋ฉด 1,700์ 1๊ฐ MD์ถ์ฒ์ํ
+- ์ปต๋ผ๋ฉด 1,700์ 10๊ฐ
+
+๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])
+[์ค๋ ์ง์ฃผ์ค-1]
+
+ํ์ฌ ์ค๋ ์ง์ฃผ์ค์(๋) 1๊ฐ๋ฅผ ๋ฌด๋ฃ๋ก ๋ ๋ฐ์ ์ ์์ต๋๋ค. ์ถ๊ฐํ์๊ฒ ์ต๋๊น? (Y/N)
+Y
+
+๋ฉค๋ฒ์ญ ํ ์ธ์ ๋ฐ์ผ์๊ฒ ์ต๋๊น? (Y/N)
+Y
+
+===========W ํธ์์ =============
+์ํ๋ช
์๋ ๊ธ์ก
+์ค๋ ์ง์ฃผ์ค 2 3,600
+===========์ฆ ์ =============
+์ค๋ ์ง์ฃผ์ค 1
+==============================
+์ด๊ตฌ๋งค์ก 2 3,600
+ํ์ฌํ ์ธ -1,800
+๋ฉค๋ฒ์ญํ ์ธ -0
+๋ด์ค๋ 1,800
+
+๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)
+N
+~~~
+
+
+## ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ ์ฌํญ 1
+
+----
+* JDK 21 ๋ฒ์ ์์ ์คํ ๊ฐ๋ฅํด์ผ ํ๋ค.
+* ํ๋ก๊ทธ๋จ ์คํ์ ์์์ ์ Application์ main()์ด๋ค.
+* build.gradle ํ์ผ์ ๋ณ๊ฒฝํ ์ ์์ผ๋ฉฐ, ์ ๊ณต๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ด์ธ์ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ฌ์ฉํ์ง ์๋๋ค.
+* ํ๋ก๊ทธ๋จ ์ข
๋ฃ ์ System.exit()๋ฅผ ํธ์ถํ์ง ์๋๋ค.
+* ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ ์ฌํญ์์ ๋ฌ๋ฆฌ ๋ช
์ํ์ง ์๋ ํ ํ์ผ, ํจํค์ง ๋ฑ์ ์ด๋ฆ์ ๋ฐ๊พธ๊ฑฐ๋ ์ด๋ํ์ง ์๋๋ค.
+* ์๋ฐ ์ฝ๋ ์ปจ๋ฒค์
์ ์งํค๋ฉด์ ํ๋ก๊ทธ๋๋ฐํ๋ค.
+ * ๊ธฐ๋ณธ์ ์ผ๋ก Java Style Guide๋ฅผ ์์น์ผ๋ก ํ๋ค.
+
+
+## ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ ์ฌํญ 2
+
+----
+* indent(์ธ๋ดํธ, ๋ค์ฌ์ฐ๊ธฐ) depth๋ฅผ 3์ด ๋์ง ์๋๋ก ๊ตฌํํ๋ค. 2๊น์ง๋ง ํ์ฉํ๋ค.
+ * ์๋ฅผ ๋ค์ด while๋ฌธ ์์ if๋ฌธ์ด ์์ผ๋ฉด ๋ค์ฌ์ฐ๊ธฐ๋ 2์ด๋ค.
+ * ํํธ: indent(์ธ๋ดํธ, ๋ค์ฌ์ฐ๊ธฐ) depth๋ฅผ ์ค์ด๋ ์ข์ ๋ฐฉ๋ฒ์ ํจ์(๋๋ ๋ฉ์๋)๋ฅผ ๋ถ๋ฆฌํ๋ฉด ๋๋ค.
+* 3ํญ ์ฐ์ฐ์๋ฅผ ์ฐ์ง ์๋๋ค.
+* ํจ์(๋๋ ๋ฉ์๋)๊ฐ ํ ๊ฐ์ง ์ผ๋ง ํ๋๋ก ์ต๋ํ ์๊ฒ ๋ง๋ค์ด๋ผ.
+* JUnit 5์ AssertJ๋ฅผ ์ด์ฉํ์ฌ ์ ๋ฆฌํ ๊ธฐ๋ฅ ๋ชฉ๋ก์ด ์ ์์ ์ผ๋ก ์๋ํ๋์ง ํ
์คํธ ์ฝ๋๋ก ํ์ธํ๋ค.
+ * ํ
์คํธ ๋๊ตฌ ์ฌ์ฉ๋ฒ์ด ์ต์ํ์ง ์๋ค๋ฉด ์๋ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ํ์ตํ ํ ํ
์คํธ๋ฅผ ๊ตฌํํ๋ค.
+ * [JUnit 5 User Guide](https://junit.org/junit5/docs/current/user-guide/)
+ * [AssertJ User Guide](https://assertj.github.io/doc/)
+ * [AssertJ Exception Assertions](https://www.baeldung.com/assertj-exception-assertion)
+ * [Guide to JUnit 5 Parameterized Tests](https://www.baeldung.com/parameterized-tests-junit-5)
+
+
+## ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ ์ฌํญ 3
+
+---
+* else ์์ฝ์ด๋ฅผ ์ฐ์ง ์๋๋ค.
+ * else๋ฅผ ์ฐ์ง ๋ง๋ผ๊ณ ํ๋ switch/case๋ก ๊ตฌํํ๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ฐ switch/case๋ ํ์ฉํ์ง ์๋๋ค.
+ * ํํธ: if ์กฐ๊ฑด์ ์์ ๊ฐ์ returnํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ๋ฉด else๋ฅผ ์ฌ์ฉํ์ง ์์๋ ๋๋ค.
+* Java Enum์ ์ ์ฉํ์ฌ ํ๋ก๊ทธ๋จ์ ๊ตฌํํ๋ค.
+* ๊ตฌํํ ๊ธฐ๋ฅ์ ๋ํ ๋จ์ ํ
์คํธ๋ฅผ ์์ฑํ๋ค. ๋จ, UI(System.out, System.in, Scanner) ๋ก์ง์ ์ ์ธํ๋ค.
+
+
+## ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ ์ฌํญ 4
+
+----
+* ํจ์(๋๋ ๋ฉ์๋)์ ๊ธธ์ด๊ฐ 10๋ผ์ธ์ ๋์ด๊ฐ์ง ์๋๋ก ๊ตฌํํ๋ค.
+ * ํจ์(๋๋ ๋ฉ์๋)๊ฐ ํ ๊ฐ์ง ์ผ๋ง ์ ํ๋๋ก ๊ตฌํํ๋ค.
+* ์
์ถ๋ ฅ์ ๋ด๋นํ๋ ํด๋์ค๋ฅผ ๋ณ๋๋ก ๊ตฌํํ๋ค.
+ * ์๋ ``InputView``, ``OutputView`` ํด๋์ค๋ฅผ ์ฐธ๊ณ ํ์ฌ ์
์ถ๋ ฅ ํด๋์ค๋ฅผ ๊ตฌํํ๋ค.
+ * ํด๋์ค ์ด๋ฆ, ๋ฉ์๋ ๋ฐํ ์ ํ, ์๊ทธ๋์ฒ ๋ฑ์ ์์ ๋กญ๊ฒ ์์ ํ ์ ์๋ค.
+~~~
+public class InputView {
+ public String readItem() {
+ System.out.println("๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])");
+ String input = Console.readLine();
+ // ...
+ }
+ // ...
+}
+~~~
+~~~
+public class OutputView {
+ public void printProducts() {
+ System.out.println("- ์ฝ๋ผ 1,000์ 10๊ฐ ํ์ฐ2+1");
+ // ...
+ }
+ // ...
+}
+~~~
+
+
+## ๋ผ์ด๋ธ๋ฌ๋ฆฌ
+* ``camp.nextstep.edu.missionutils``์์ ์ ๊ณตํ๋ ``DateTimes`` ๋ฐ ``Console`` API๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํด์ผ ํ๋ค.
+ * ํ์ฌ ๋ ์ง์ ์๊ฐ์ ๊ฐ์ ธ์ค๋ ค๋ฉด ``camp.nextstep.edu.missionutils.DateTimes``์ ``now()``๋ฅผ ํ์ฉํ๋ค.
+ * ์ฌ์ฉ์๊ฐ ์
๋ ฅํ๋ ๊ฐ์ ``camp.nextstep.edu.missionutils.Console``์ ``readLine()``์ ํ์ฉํ๋ค. | Unknown | ์ด๋ฒ ๋ฏธ์
์ด ์ ๋ง ๋ณต์กํด์ ์ด๋ ค์ ๋๋ฐ ์ ๋ ์ค๊ณ๋ฅผ ์งํํ ๋ ํ๋ก์ฐ ์ฐจํธ์ ๋น์ทํ๊ฒ ๋์ ๊ณผ์ ์ ์ ๋ฆฌํ๊ณ ๊ฐ๋ฐ์ ์งํํ๋๊ฒ ๊ฐ์๋ฐ ๋ฌธ์ํ๋๋ ๋น์ทํ๊ฒ ์งํํ ๊ฒ ๋ณด๊ณ ๋๋๋ค์ |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | ๋ค๋ฅธ ์ฌ๋๋ค์ ์ฝ๋๋ฅผ ๋ชฐ๋๋ชฐ๋ ๋ดค์๋ ์ด๋ฐ์ฉ์ผ๋ก ์ฒ๋ฆฌํด์ ๋ณ์๋ฅผ ์ค์ด๋ ๋ฐฉ๋ฒ๋ ์๋๊ตฐ์. ์๊ฐ์ ๋ฐ๋ผ ์ข์์๋ ์๊ณ ์์ข๋ค๊ณ ์๊ฐํ ์๋ ์์ง๋ง ์ฝ๋๋ณด๋ค๊ฐ ์๊ฐ๋์ ๋ฃ์ด๋๊ฒ์
```suggestion
do {
showInventory(products);
BuyProducts buyProducts = buyProducts(promotions, products);
CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
showReceipt(customerReceipt);
} while (inputView.continueShopping()
.equals(YES))
``` |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | ๋ค์ .equals(YES)๋ผ๊ณ ํ๋ ๊ฒฝ์ฐ ํ๋จ์ controller์์ ์งํํ๋ ๊ฒ๋ณด๋ค๋ view์์ YES๋ฉด true๋ฅผ ๋ฃ์ด controller์ ์ฑ
์์ ์ค์ฌ์ฃผ๋๊ฒ์ ๋ํด์๋ ์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | FileReader๋ฅผ ์์ฑํ๋ ๊ฒ๋ณด๋ค ์์กด์ฑ ์ฃผ์
์ ํตํด ์ฌ์ฉํ์๋ฉด ์ถํ ํ๋ก๋ชจ์
์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ด ๋ฐ๋์ด๋ ์ฝ๊ฒ ์์ ์ด ๊ฐ๋ฅํด์ง ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,41 @@
+package store.domain;
+
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.vo.PromotionType;
+
+public class Promotions {
+
+ private static final int BUY_INDEX = 1;
+ private static final int FREE_GET_INDEX = 2;
+ private static final int START_DATE_INDEX = 3;
+
+ private final List<Promotion> promotions;
+
+ public Promotions(List<List<String>> contents, LocalDateTime currentTime) {
+ this.promotions = makePromotions(contents, currentTime);
+ }
+
+ public Promotion getPromotionByType(PromotionType type) {
+ return promotions.stream()
+ .filter(promotion -> promotion.getPromotionType().equals(type.getPromotionType()))
+ .findFirst()
+ .orElse(Promotion.noPromotion());
+ }
+
+ private List<Promotion> makePromotions(List<List<String>> contents, LocalDateTime currentTime) {
+ List<Promotion> promotions = new ArrayList<>();
+ makePromotion(contents, currentTime, promotions);
+ return promotions;
+ }
+
+ private void makePromotion(List<List<String>> contents, LocalDateTime currentTime, List<Promotion> promotions) {
+ for (List<String> content : contents) {
+ Promotion promotion = Promotion.from(
+ PromotionType.from(content.getFirst()), currentTime, content.get(BUY_INDEX), content.get(FREE_GET_INDEX), content.get(START_DATE_INDEX), content.getLast()
+ );
+ promotions.add(promotion);
+ }
+ }
+} | Java | List<List<String>>์ผ๋ก ์ ๋ณด๋ฅผ ๋ฝ์๋ด๋ ๊ฒ์ ๋๋ฉ์ธ์ ์ญํ ์ด ์๋ view์ ์ญํ ์ ๋ ๊ฐ๊น์ด ๊ฒ ๊ฐ์ต๋๋ค. View์์ ์ด๋์ ๋์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณตํ ํ DTO๋ฅผ ํตํด product๋ฅผ ์์ฑํ๋ ๊ฒ์ ์ด๋จ๊น์? |
@@ -0,0 +1,60 @@
+package store.domain;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import store.domain.vo.ProductQuantity;
+import store.domain.vo.PromotionType;
+
+public class Promotion {
+
+ private final PromotionType promotionType;
+ private final ProductQuantity buyQuantity;
+ private final ProductQuantity getQuantity;
+ private final boolean isPromotion;
+
+ public static Promotion from(PromotionType promotionType, LocalDateTime currentDate, String buyQuantity, String getQuantity,
+ String startDate, String endDate) {
+
+ return new Promotion(promotionType, ProductQuantity.from(buyQuantity), ProductQuantity.from(getQuantity), isPromotion(currentDate, startDate, endDate));
+ }
+
+ public static Promotion noPromotion() {
+ return new Promotion(PromotionType.none(), ProductQuantity.none(), ProductQuantity.none(), false);
+ }
+
+ private Promotion(PromotionType promotionType, ProductQuantity buyQuantity, ProductQuantity getQuantity,
+ boolean isPromotion) {
+ this.promotionType = promotionType;
+ this.buyQuantity = buyQuantity;
+ this.getQuantity = getQuantity;
+ this.isPromotion = isPromotion;
+ }
+
+ public String getPromotionType() {
+ return promotionType.getPromotionType();
+ }
+
+ public boolean isPromotion() {
+ return isPromotion;
+ }
+
+ public int getBuyQuantity() {
+ return buyQuantity.getQuantity();
+ }
+
+ public int getGettableQuantity() {
+ return getQuantity.getQuantity();
+ }
+
+ public int getOnePromotionCycle() {
+ return buyQuantity.getQuantity() + getQuantity.getQuantity();
+ }
+
+ private static boolean isPromotion(LocalDateTime currentDate, String startDate, String endDate) {
+ LocalDateTime start = LocalDate.parse(startDate).atStartOfDay();
+ LocalDateTime end = LocalDate.parse(endDate).atTime(LocalTime.MAX);
+
+ return currentDate.isAfter(start) && currentDate.isBefore(end);
+ }
+} | Java | ํ๋ก๋ชจ์
์์ฑ ๊ณผ์ ์ ๋ณด๋ฉด ํ์ฌ ์๊ฐ์ ๋ง์ถฐ ํ๋ก๋ชจ์
์ ์ฉ์ ์ ํ๋๋ฐ ์ฌ๊ณ ๋ฅผ ๋ฃ๋ ์๊ฐ๋์ ๋ฌผํ์ ์ฌ๋ ์๊ฐ์ด ๋ค๋ฅธ ๊ฒฝ์ฐ ๋ฌธ์ ๊ฐ ์๊ธธ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | ์ด๋ ๊ฒ ์ฌ๋ฌ๋ฒ ํธ์ถ์ด ๋๋ค๋ฉด IO์์
์ด ๋์ด๋๊ฒ ๋๋ฉด์ ์ฑ๋ฅ์ ์ผ๋ก ์์ฌ์์ด ์๊ธธ ์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค. List๋ฅผ ํตํด ํ๋ฒ์ ๋ณด๋ธ ํ IO์์
์ ํตํด ์ฒ๋ฆฌํ์๋๊ฒ์ ์ด๋ค๊ฐ์? |
@@ -0,0 +1,76 @@
+package store.view;
+
+import java.util.List;
+import store.view.dto.response.BuyProductResponse;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.response.FreeProductResponse;
+import store.view.dto.response.ProductCalculateResponse;
+
+public class OutputView {
+
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+
+ public void welcomeStore() {
+ System.out.println(LINE_SEPARATOR + "์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค." + LINE_SEPARATOR + "ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค." + LINE_SEPARATOR);
+ }
+
+ public void showInventory(String name, int price, String quantity, String promotionType) {
+ System.out.printf(
+ "- %s %,d์ %s %s"
+ + LINE_SEPARATOR,
+ name, price, quantity, promotionType
+ );
+ }
+
+ public void getCustomerProducts() {
+ System.out.println(LINE_SEPARATOR + "๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])");
+ }
+
+ public void guideBuyProducts() {
+ System.out.printf(
+ LINE_SEPARATOR +
+ "==============W ํธ์์ ================"
+ + LINE_SEPARATOR
+ + "%-10s %9s %9s%n"
+ , "์ํ๋ช
", "์๋" ,"๊ธ์ก"
+ );
+ }
+
+ public void showBuyProducts(List<BuyProductResponse> buyProducts) {
+ for (BuyProductResponse product : buyProducts) {
+ System.out.printf(
+ "%-10s %9d %,14d" + LINE_SEPARATOR,
+ product.name(), product.quantity(), product.totalPrice()
+ );
+ }
+ }
+
+ public void guideFreeGetQuantity() {
+ System.out.println("=============์ฆ\t\t์ ===============");
+ }
+
+ public void showFreeQuantity(List<FreeProductResponse> freeProductResponses) {
+ for (FreeProductResponse freeProductResponse : freeProductResponses) {
+ System.out.printf(
+ "%-10s %9d" + LINE_SEPARATOR,
+ freeProductResponse.name(), freeProductResponse.quantity()
+ );
+ }
+ }
+
+ public void showCalculateResult(ProductCalculateResponse calculateInfo) {
+ System.out.printf(
+ "====================================" + LINE_SEPARATOR
+ + "์ด๊ตฌ๋งค์ก\t\t\t\t%d\t\t %,d" + LINE_SEPARATOR
+ + "ํ์ฌํ ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR
+ + "๋ฉค๋ฒ์ญํ ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR
+ + "๋ด์ค๋\t\t\t\t\t\t %,d" + LINE_SEPARATOR
+ , calculateInfo.buyCounts(), calculateInfo.buyProductsPrice(),
+ calculateInfo.promotionPrice(), calculateInfo.memberShip(), calculateInfo.totalPrice()
+ );
+ }
+
+ public void showError(ErrorResponse errorResponse) {
+ System.out.println(errorResponse.message());
+ }
+} | Java | format์ ์์๋ฅผ ํตํด ์ฒ๋ฆฌํ๋ ๊ฒ์ ์ด๋ค๊ฐ์?
๋ง์ฝ ์ถ๋ ฅ ํ์์ด ์ข ๋ฌ๋ผ์ง๋ค๋ฉด ์์ ์ด ๋์ฑ ๋ฒ๊ฑฐ๋ก์ธ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,76 @@
+package store.view;
+
+import java.util.List;
+import store.view.dto.response.BuyProductResponse;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.response.FreeProductResponse;
+import store.view.dto.response.ProductCalculateResponse;
+
+public class OutputView {
+
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+
+ public void welcomeStore() {
+ System.out.println(LINE_SEPARATOR + "์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค." + LINE_SEPARATOR + "ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค." + LINE_SEPARATOR);
+ }
+
+ public void showInventory(String name, int price, String quantity, String promotionType) {
+ System.out.printf(
+ "- %s %,d์ %s %s"
+ + LINE_SEPARATOR,
+ name, price, quantity, promotionType
+ );
+ }
+
+ public void getCustomerProducts() {
+ System.out.println(LINE_SEPARATOR + "๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])");
+ }
+
+ public void guideBuyProducts() {
+ System.out.printf(
+ LINE_SEPARATOR +
+ "==============W ํธ์์ ================"
+ + LINE_SEPARATOR
+ + "%-10s %9s %9s%n"
+ , "์ํ๋ช
", "์๋" ,"๊ธ์ก"
+ );
+ }
+
+ public void showBuyProducts(List<BuyProductResponse> buyProducts) {
+ for (BuyProductResponse product : buyProducts) {
+ System.out.printf(
+ "%-10s %9d %,14d" + LINE_SEPARATOR,
+ product.name(), product.quantity(), product.totalPrice()
+ );
+ }
+ }
+
+ public void guideFreeGetQuantity() {
+ System.out.println("=============์ฆ\t\t์ ===============");
+ }
+
+ public void showFreeQuantity(List<FreeProductResponse> freeProductResponses) {
+ for (FreeProductResponse freeProductResponse : freeProductResponses) {
+ System.out.printf(
+ "%-10s %9d" + LINE_SEPARATOR,
+ freeProductResponse.name(), freeProductResponse.quantity()
+ );
+ }
+ }
+
+ public void showCalculateResult(ProductCalculateResponse calculateInfo) {
+ System.out.printf(
+ "====================================" + LINE_SEPARATOR
+ + "์ด๊ตฌ๋งค์ก\t\t\t\t%d\t\t %,d" + LINE_SEPARATOR
+ + "ํ์ฌํ ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR
+ + "๋ฉค๋ฒ์ญํ ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR
+ + "๋ด์ค๋\t\t\t\t\t\t %,d" + LINE_SEPARATOR
+ , calculateInfo.buyCounts(), calculateInfo.buyProductsPrice(),
+ calculateInfo.promotionPrice(), calculateInfo.memberShip(), calculateInfo.totalPrice()
+ );
+ }
+
+ public void showError(ErrorResponse errorResponse) {
+ System.out.println(errorResponse.message());
+ }
+} | Java | System.lineSeparator๋ฅผ ํตํ ์ค๋ฐ๊ฟ ์ฒ๋ฆฌ ๊ต์ฅํ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,46 @@
+package store.domain.service;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProducts;
+import store.domain.Product;
+import store.domain.Products;
+import store.view.dto.response.PromotionResponse;
+
+public class PromotionChecker {
+
+ private static final int ZERO = 0;
+
+ public List<PromotionResponse> checkPromotion(Products products, BuyProducts buyProducts) {
+ List<PromotionResponse> promotionResponses = new ArrayList<>();
+ for (Product buyProduct : buyProducts.products()) {
+ checkPromotionCase(products, buyProduct, promotionResponses);
+ }
+ return promotionResponses;
+ }
+
+ private void checkPromotionCase(Products products, Product buyProduct, List<PromotionResponse> promotionResponses) {
+ if (buyProduct.isPromotion()) {
+ Product promotionProduct = products.getPromotionProducts(buyProduct.getName()).getFirst();
+ checkNoPromotion(buyProduct, promotionProduct, promotionResponses);
+ checkFreePromotion(buyProduct, promotionResponses);
+ }
+ }
+
+ private void checkFreePromotion(Product buyProduct, List<PromotionResponse> promotionResponses) {
+ if (buyProduct.getQuantity() % buyProduct.getOnePromotionCycle() == buyProduct.getPromotionBuyQuantity()) {
+ promotionResponses.add(
+ new PromotionResponse(buyProduct.getName(), false, ZERO, true, buyProduct.getPromotionGettableQuantity())
+ );
+ }
+ }
+
+ private void checkNoPromotion(Product buyProduct, Product promotionProduct, List<PromotionResponse> promotionResponses) {
+ if (buyProduct.getQuantity() > promotionProduct.getQuantity()) {
+ int noPromotionQuantity = buyProduct.getQuantity() - promotionProduct.getOnePromotionCycle() * promotionProduct.getPromotionQuantity();
+ promotionResponses.add(
+ new PromotionResponse(buyProduct.getName(), true, noPromotionQuantity, false, ZERO)
+ );
+ }
+ }
+} | Java | ํ๋ก๋ชจ์
์ ํ์ธํ๋ ๋ถ๋ถ์ ๋ฐ๋ก ๋ฝ์ ์ด์ ๊ฐ ์์๊น์? |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | false์ isFreePromotion์ enum์ ํตํ ์ํ๋ฅผ ๋ํ๋ด๋ ์ด๊ฑฐํ์ผ๋ก ํ๋ ๊ฒ์ ์ด๋จ๊น์? |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | ์ด ๋ถ๋ถ์ outputView์์ ์์๋ฅผ ๊ด๋ฆฌํ๋ ๊ฒ์ด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | while์กฐ๊ฑด ์์ true๋ฅผ ๋ฃ๋ ๋ฐฉ์์ ๋งค์ฐ ์ข์ง ๋ชปํ ๋ฐฉ์์ด๋ผ๊ณ ์๊ณ ์์ต๋๋ค. ๋ฐ๋ก boolean๊ฐ์ ์ฌ์ฉํด์ ์กฐ๊ฑด์ ๋ฃ๋ ๊ฒ์ด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,63 @@
+package store.domain;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class BuyProductParser {
+
+ private static final int NAME_INDEX = 1;
+ private static final int PRICE_INDEX = 2;
+ private static final String INPUT_DELIMITER = ",";
+ private static final String ORDER_FORM = "\\[(.+)-(.+)]";
+
+ private final Map<String, String> products;
+
+ public BuyProductParser(String buyProduct) {
+ this.products = makeProductsForm(buyProduct);
+ }
+
+ public Map<String, String> getProducts() {
+ return Collections.unmodifiableMap(products);
+ }
+
+ private Map<String, String> makeProductsForm(String buyProducts) {
+ Map<String, String> products = new LinkedHashMap<>();
+ parseProducts(buyProducts, products);
+ return products;
+ }
+
+ private void parseProducts(String buyProducts, Map<String, String> products) {
+ Pattern pattern = Pattern.compile(ORDER_FORM);
+ parseByRest(buyProducts).forEach(product -> {
+ validateFrom(product);
+ Matcher matcher = pattern.matcher(product);
+ validateMatcher(matcher);
+ products.put(matcher.group(NAME_INDEX).trim(), matcher.group(PRICE_INDEX).trim());
+ });
+ }
+
+ private void validateMatcher(Matcher matcher) {
+ if (!matcher.matches()) {
+ throw new ProductException(ProductErrorCode.WRONG_INPUT);
+ }
+ }
+
+ private List<String> parseByRest(String buyProduct) {
+ if (buyProduct.contains(INPUT_DELIMITER)) {
+ return List.of(buyProduct.split(INPUT_DELIMITER));
+ }
+ return List.of(buyProduct);
+ }
+
+ private void validateFrom(String buyProducts) {
+ if (!buyProducts.matches(ORDER_FORM)) {
+ throw new ProductException(ProductErrorCode.WRONG_INPUT);
+ }
+ }
+} | Java | Hash์ ๊ฒฝ์ฐ ์ฌ๊ธฐ์ ์ธ ์๊ฐ๋ณต์ก๋๋ก ์ข์ง๋ง ํน์ํ ์ํฉ์์๋ Hash์ ๊ฒฝ์ฐ ๊ต์ฅํ ๋๋ ค์ง ์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค. ๋ฐ๋ผ์ Hash๋ณด๋จ ๋๋ฆฌ์ง๋ง ์์ ์ ์ธ ์ฑ๋ฅ์ ๊ฐ์ง๋ TreeMap๋ ์ถ์ฒ๋๋ฆฝ๋๋ค. ๊ทผ๋ฐ TreeMap์ ์ฌ์ฉํ๋ฉด ์์์ ๋ค๋ฅด๊ฒ ์ ๋ ฌ๋๋ค๋ ๋จ์ ์ด ์์ต๋๋ค. |
@@ -0,0 +1,168 @@
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+~~~
+๊ตฌํ์ ํ์ํ ์ํ ๋ชฉ๋ก๊ณผ ํ์ฌ ๋ชฉ๋ก์ ํ์ผ ์
์ถ๋ ฅ์ ํตํด ๋ถ๋ฌ์จ๋ค.
+src/main/resources/products.md๊ณผ
+src/main/resources/promotions.md ํ์ผ์ ์ด์ฉํ๋ค.
+~~~
+* ๋งํฌ๋ค์ด ํ์ผ์ ์๋ ๋ด์ฉ์ ์ด๋ป๊ฒ ๋ถ๋ฌ์ ์ฝ๋์ ์ ์ฉ์ํฌ ์ ์์๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* BufferedReader ๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ ๋ด์ฉ์ ํ ์ค์ฉ ์
๋ ฅ๋ฐ๋ ํ์์ผ๋ก ํ๋ค.
+* ๋ฐ์์จ ์ค์ ์ผํ(,)๋ก ๊ตฌ๋ถํ์ฌ ๋ถ๋ฆฌํ๋ค
+* ๋ถ๋ฆฌํ ๋ด์ฉ์ ์๋ง๊ฒ ์์๊ฐ ํฌ์ฅ ํด๋์ค์ ํ ๋นํ๋ค.
+* ํ ๋น๋ ๋, ๋น ๊ฐ์ด๋ ํ์์ด ๋ง์ง ์์ผ๋ฉด ์์ธ๋ฅผ ๋ฐ์์ํจ๋ค.
+* ๋ํ, ํ
์คํธ ํด์ผ ํ ๊ฒฝ์ฐ ํ์ผ ๊ฒฝ๋ก๋ฅผ ํด๋น ๊ฐ์ฒด ๋ด๋ถ์ ๊ฒฐ์ ํ๋ฉด ๋ค๋ฅธ ํ์ผ๋ก ํ
์คํธ ํ์ง ๋ชปํ๋ค.
+* ๋ฐ๋ผ์, ํ์ผ ๊ฒฝ๋ก๋ฅผ ์ฃผ์
๋ฐ๋ ํ์์ผ๋ก ์์ฑํ์ฌ ํ
์คํธ๋ฅผ ์งํํ ์ ์๋๋ก ํ๋ค.
+* ๊ทธ๋ฆฌ๊ณ ํ
์คํธ ํ์ผ ์ค์ ์, ๋งํฌ๋ค์ด์ ๋น ์ค์ด ๋ค์ด๊ฐ์ง ์๋๋ก ์ฃผ์ํ๋ค. -> ๋น ์ค์ด ๋ค์ด๊ฐ๋ฉด ๋ฒํผ๋ฆฌ๋๊ฐ ๊ณต๋ฐฑ์ ์ฝ์ด๋ฒ๋ฆฐ๋ค.
+
+
+* ๋ํ, ๋ชจ๋ ํ์ผ์ ๋ด์ฉ์ ์ ์ฅํ๋ค. ํ๋ก๊ทธ๋จ ์๊ตฌ ์ฌํญ์ ๋ฐ๋ผ, ํ์ผ ์
์ถ๋ ฅ์ ํตํด ํ๋ก๊ทธ๋จ์ ๊ตฌํํ๋ค. -> ํ์ผ์ ์๋ ๋ด์ฉ์ enum์ผ๋ก ๋ง๋ค์ง ์์ ๊ฒ.
+
+----
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ๊ฐ์ฒด๋ฅผ ์ด๋ค์์ผ๋ก ๋๋ ์ผ ํ ๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* ์
๋ ฅ ํ์์ด ``[์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1]`` ์ด๋ฐ์์ผ๋ก ๋ค์ด์ด. ์ฆ, ์ค์ํ๊ฑด ์ํ ์ด๋ฆ๊ณผ ์๋
+* ๊ฐ๊ฒฉ์ ๊ทธ๋ ๊ทธ๋ ๋ํด์ฃผ๋ฉด ๋๊ธฐ ๋๋ฌธ์, ์ํ์ด๋ฆ๊ณผ ๊ฐ๊ฒฉ์ enum ์ผ๋ก ์์ฑ
+* ์ํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ ์ด๋ฆ๊ณผ ์๋์ ํ๋๋ก ๊ฐ์ง๋๋ก ํจ
+* ๊ทธ๋ฆฌ๊ณ ํ๋ก๋ชจ์
์ ๊ฒฝ์ฐ๋ Map ์๋ฃ๊ตฌ์กฐ ํํ๋ฅผ ์ฌ์ฉํด์ ``Map<์ํ, ํ๋ก๋ชจ์
>`` ํํ๋ก ๋ํ๋
+* ์ด๋ฐ ํ์์ผ๋ก ํ๋ฉด ์ด๋ค ์ํ์ด ์ด๋ค ํ๋ก๋ชจ์
์ธ์ง ์ ๋ํ๋ผ ์ ์์ ๊ฒ ๊ฐ์.
+* ๋์ค์ ํ๋ก๋ชจ์
์ ๋ณด๋ง ๊ฐ์ง๋ ๊ฐ์ฒด๋ ๋ฐ๋ก ๋ง๋ค์ด์ ๊ตฌ์
ํ ๋, ๋น๊ตํ๋๋ก ํ๋ฉด ๋จ.
+
+
+
+* ``Map<์ํ, ํ๋ก๋ชจ์
>`` ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์์์ ์๊ฒ ๋.
+* ์ํ ํ๋์ ์์๊ฐ ํฌ์ฅํ ๊ฐ์ฒด๋ฅผ ์ฌ๋ฌ๊ฐ ๊ฐ์ ธ๋ค ์จ๋ ๋๋ค๋ ๊ฒ์ ์์์.
+* ๋ฐ๋ผ์, ``List<์ํ>`` ํ์์ผ๋ก ํ๋๋ฅผ ๊ตฌ์ฑ
+----
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ์์๊ฐ ํฌ์ฅ ๊ฐ์ฒด์ ์๋ ๊ฐ์ ๋ฐํํ๋ ค ํ๋, ``Product.name().name()`` ํ์์ ํตํด ๋ฐํ๋๋ค. ์ค๋ณต์ ์ค์ด๊ณ ์ถ์.
+
+**ํด๊ฒฐ ๊ณผ์ **
+* ๊ฐ์ฒด๋ฅผ ๋ฐ๋ ์
์ฅ์์ ์ด๋ค ๋ฐฉ์์ผ๋ก ๊ฐ์ฒด๊ฐ ์ ๋ฌ๋๋์ง ์ ํํ ์์์ผ ํ ๊น?
+* ํด๋น๋ ๊ฐ๋ง ์ ๋ฐ์์ ๊ตฌํ๋๋ฉด ๋๋ค.
+* ๋ฐ๋ผ์, ``Product.name()`` ๋ง ํ์ ๋, ๊ฐ์ด ๋ฐํ๋๋๋ก ํ๋ค.
+
+----
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ์กด์ฌํ์ง ์๋ ์ํ์ ๊ฒฝ์ฐ๋ฅผ ์ด๋ค์์ผ๋ก ์ฒ๋ฆฌํ ์ ์์๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* ์
๋ ฅ๋ฐ์ ํ๋ชฉ๋ค์ Products ํด๋์ค ํน์ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ๋์ง๋ค
+* ์ด๋, ์ด๋ป๊ฒ ํด๋น ํญ๋ชฉ์ด ๋ค์ด์๋์ง ๊ฒ์ฆํ ์ ์์๊น?
+* ์ํ๋ช
์ด ๊ฐ์ ๊ฒฝ์ฐ ๋ค์ด์๋ค๊ณ ํ๋จํด์ผ ํ๋ค.
+* ์ํ๋ช
์ด ๊ฐ์ ๊ฒฝ์ฐ, ์ธ์คํด์ค ์์ฒด๊ฐ ๊ฐ๋ค๊ณ ํ๋จํ๋ฉด ๋ก์ง์ด ๋ ๊ฐํธํด์ง ๊ฒ ๊ฐ๋ค.
+* ๋ฐ๋ผ์, Product ํด๋์ค์ equals์ hashcode ๋ฉ์๋๋ฅผ name์ ๊ดํด์๋ง ์์ฑํด์ผ ํ๋ค.
+* ProductName ํด๋์ค์๋ equals์ hashcode ๋ฉ์๋๋ฅผ ์์ฑํด์ผ ํ๋ค.
+
+----
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ๊ตฌ๋งค ์๋์ด ์ฌ๊ณ ์๋์ ์ด๊ณผํ ๊ฒฝ์ฐ๋ฅผ ์ด๋ค ์์ผ๋ก ์ฒ๋ฆฌํ ์ ์์๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* ๋จผ์ , ํด๋น ์ํ์ ์ด๋ฆ๊ณผ ์ผ์นํ๋ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค.
+* ๊ฐ์ ธ์จ ๋ชจ๋ ์ํ์ ์ฌ๊ณ ๋ฅผ ํฉํ์ฌ ๋น๊ตํ๋ค.
+
+
+* ์๋ ์๋ Products ํด๋์ค์ ์ฌ๊ณ ๊ฒ์ฆ ์ฑ
์์ ๋ถ์ฌํ๋, ๋ก์ง์ด ๋๋ฌด ๋ง์์ง๊ณ ๋ง์ ์ฑ
์์ ์ง๊ฒ ๋๋ค.
+* ๋ฐ๋ผ์, ์ฌ๊ณ ๊ฒ์ฆํ๋ ํด๋์ค๋ฅผ ๋ฐ๋ก ์์ฑํ๋ค.
+
+---
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ์ํ ์
๋ ฅ ์, ๊ธฐํ ์๋ชป๋ ๊ฒฝ์ฐ๋ฅผ ์ด๋ป๊ฒ ๊ฒ์ฆํ ์ ์์๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* ๊ธฐํ ์๋ชป๋ ๊ฒฝ์ฐ๋ฅผ ๊ฒ์ฆํ๋ ค๋ฉด [์ํ-๊ฐ์] ํด๋น ํ์ ์์ฒด๊ฐ ์๋์ด์ผ ํ๋ค.
+* ๋ฐ๋ผ์ ์ ๊ท์์ ํตํด ๊ฒ์ฆํ๋ค.
+1. ```\\[``` : ```[```๋ก ์์ํ๋์ง
+2. ```(.+)``` : ์๋ฌด ๋ฌธ์๊ฐ ํ๋ ์ด์ ์๋์ง
+3. ``-`` : ``-``๋ก ๋๋๋์ง
+4. ```]``` : ``]`` ๋ก ๋๋๋์ง
+
+
+* ์ต์ข
: ``\\[(.+)-(.+)]``
+
+-----
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ์ํ์ ํ๋ก๋ชจ์
์ ๋ฌด๋ฅผ ๋ฐ์ ธ ์ด๋ป๊ฒ ๊ตฌ๋งคํ ๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+1. ํด๋น ์ด๋ฆ์ผ๋ก ํ๋ก๋ชจ์
์ํ์ด ์๋์ง ํ๋ณํ๋ค
+2. ํ๋ก๋ชจ์
์ํ์ด ์์ ๊ฒฝ์ฐ
+ * ํ๋ก๋ชจ์
๊ฐ์๊ฐ ๊ตฌ๋งคํ ๊ฐ์๋ณด๋ค ๋ง์์ง ํ๋ณํ๋ค
+ * ํ๋ก๋ชจ์
๊ฐ์ >= ๊ตฌ๋งคํ ๊ฐ์ : ํ๋ก๋ชจ์
์ฌ๊ณ ๋ฅผ ์ฐจ๊ฐํ๋ค
+ * ํ๋ก๋ชจ์
์ ์ฉ์ด ๊ฐ๋ฅํ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋งํผ ๊ฐ์ ธ์ค์ง ์์์ ๊ฒฝ์ฐ, ํํ์ ๋ํ ์๋ด ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+ * ํ๋ก๋ชจ์
๊ฐ์ < ๊ตฌ๋งคํ ๊ฐ์
+ * ํ๋ก๋ชจ์
์ด ์ผ๋ง๋ ์ ์ฉ๋๋์ง ํ์
ํ๊ณ , ์ผ๋ง๋ ์ ์ฉ ์๋๋์ง ํ์
ํ๋ค
+ * ํ๋ก๋ชจ์
์ฌ๊ณ ์์ ์ ์ฉ๋๋ ๋งํผ ์ฐจ๊ฐํ๊ณ , ์ ์ฉ ์๋๋ ๊ฐ์๋ฅผ ํ๋ก๋ชจ์
์ฌ๊ณ ์ ์ผ๋ฐ ์ฌ๊ณ ์์ ์ฐจ๊ฐํ๋ค.
+ 
+ * ํด๋น ์ด๋ฏธ์ง๋ฅผ ์ฐธ๊ณ ํ์ฌ ๊ณ์ฐ ๋ก์ง์ ๊ตฌํํ๋ค
+ * ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ์ฌ ์ผ๋ถ ์๋์ ํ๋ก๋ชจ์
ํํ ์์ด ๊ฒฐ์ ํด์ผ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ ์ง ์ฌ๋ถ์ ๋ํ ์๋ด ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+
+
+
+
+----
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ๊ตฌ๋งคํ ์ํ์์, ๊ตฌ๋งคํ ๊ฐ์์ ๋ฐ๋ฅธ ํ๋ก๋ชจ์
๊ฐ์๋ฅผ ๊ตฌํด์ผ ํ์ง๋ง, ํ์ฌ ๊ตฌ๋งคํ ๋ชจ๋ ์ํ์ promotion์ noPromotion์ ํ ๋นํจ
+
+**ํด๊ฒฐ ๊ณผ์ **
+* enum ์ ์ ์ธํ์ฌ ``์ด๋ฆ - ์ด๋ค ํ๋ก๋ชจ์
์ธ์ง `` ํ์์ผ๋ก ์์๋ค์ ์ ํด์ค
+* static ๋ฉ์๋๋ก ์ด๋ฆ์ ๋ฐ๋ผ ์ด๋ค ํ๋ก๋ชจ์
๊ฐ์ ๊ฐ์ ธ์ฌ์ง ์ ํด์ค
+* ์ด๋ ๊ฒ ํ๋ฉด ๊ตฌ๋งคํ ์ํ์ ์์ฑํ ๋, Promotion ๊ฐ๋ ์ง์ ํด์ค ์ ์๋ค.
+
+----
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* Promotion์ ํ๋๋ก ๊ฐ์ง๊ณ ์๋, Product ํด๋์ค์ ํ๋ก๋ชจ์
๊ณ์ฐ ๋ก์ง์ ์์ฑํ๋ ๊ฒ์ด ๋ง์๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* Product ํด๋์ค์ Promotion ๊ณ์ฐ ๋ก์ง์ ์์ฑํ๋, ๋๋ฌด ๋ง์ ์ฑ
์์ ๊ฐ์ง๊ณ ์๋ ๊ฒ ๊ฐ๋ค.
+* ์์ง๋ ํฅ์์ ์ํด Product ํด๋์ค์ ์ํ - ํ๋ก๋ชจ์
๊ด๋ จ ํ๋๋ฅผ ๋์์ง๋ง ๊ณ์ฐ ์ฑ
์๊น์ง ๊ฐ์ง๋ ๊ฒ์ ๋นํจ์จ์ ์.
+* ๋ฐ๋ผ์ Promotion ๊ด๋ จ ๊ณ์ฐ์ ์งํํ๋ PromotionService ํด๋์ค๋ฅผ ๋ง๋ ๋ค.
+
+----
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ์์์ฆ์ ์ถ๋ ฅํ๊ธฐ ์ํด, ์์์ฆ ๊ฐ์ฒด์ ์ด๋ค ์ ๋ณด๋ฅผ ๋ด์์ผ ํ ๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* ์์์ฆ์ ๊ตฌ๋งคํ ์ํ์ ์ ๋ณด, ์ฆ์ ์๋, ์ด๊ตฌ๋งค์ก, ํ์ฌํ ์ธ, ๋ฉค๋ฒ์ญ, ๋ด์ค ๋ ์ ๋ณด๊ฐ ์์ด์ผ ํ๋ค.
+* ํ์ฌ ํ๋ก๋ชจ์
์ ๊ดํ ์ ๋ณด๋ง ์์์ฆ์ ๋ฑ๋กํ ๊ฒ์ด๊ธฐ์ ๋ฉฅ๋ฒ์ญ ๊ด๋ จ ์ ๋ณด๋ ์ ์ธํ๊ณ ์งํํ๋ค.
+* Receipt ๊ฐ์ฒด์ ๊ตฌ๋งค ์ํ ์ ๋ณด, ์ฆ์ ์๋ ์ ๋ณด๋ง ๊ฐ์ ธ์์ ๊ณ์ฐํ๋ฉด ๋๋ค.
+ * ํ์ฌ ํ ์ธ ์ ๋ณด์ ๊ฒฝ์ฐ, ํด๋น ์ํ์ ์ฆ์ ์๋์ ํ์
ํด ๊ณ์ฐํ๋ฉด ๋๋ค.
+ * ์ด ๊ตฌ๋งค์ก์ ๊ฒฝ์ฐ, ํด๋น ์ํ์ ๊ตฌ๋งคํ ๊ฐ์์ ๋ฐ๋ผ ๊ณ์ฐํ๋ฉด ๋๋ค.
+ * ๋ด์ค ๋ ์ ๋ณด์ ๊ฒฝ์ฐ, ์ด ๊ตฌ๋งค์ก์์ ํ ์ธ ๋ค์ด๊ฐ ๊ฐ๊ฒฉ์ ๋นผ๋ฉด ๋๋ค.
+* ๊ตฌ๋งค ์ํ ์ ๋ณด์ ์ฆ์ ์๋์ ๋งค์นญํด์ผ ํ๊ธฐ ๋๋ฌธ์ List ํํ ๋ณด๋ค๋, Map ์๋ฃ๊ตฌ์กฐ ํํ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์๋ณด์ธ๋ค.
+
+
+* ์์์ฆ์ ์ ๋ณด๋ฅผ ๋ด์๋๋ฐ, ์์ง ๋ฉค๋ฒ์ญ์ ํฉ์น์ง ์์์.
+* ๋ฉค๋ฒ์ญ์ ํฉ์ณ์ ์ ๋ณด๋ฅผ ๋ณด์ฌ์ฃผ๋ ๊ฒ์ CustomerReceipt ํด๋์ค๋ฅผ ํ์ ์งํ (ํด๋น ํด๋์ค๋ ์ ๋ณด๋ฅผ ์ ๊ณตํด์ฃผ๋ ์ญํ ๋ง ํ๋๋ก)
+
+---- | Unknown | README์ ๋ชจ๋ ์์ฑํ์ง ์๊ณ , ์ฌ๋ฌ๊ฐ๋ก ๋ถ๋ฆฌํ์ ์ด์ ๊ฐ ์์๊น์?? |
@@ -1,7 +1,19 @@
package store;
+import store.controller.StoreController;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.view.InputView;
+import store.view.OutputView;
+
public class Application {
public static void main(String[] args) {
// TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ OutputView outputView = new OutputView();
+ InputView inputView = new InputView();
+ MemberShipCalculator memberShipCalculator = new MemberShipCalculator();
+ PromotionCalculator promotionCalculateService = new PromotionCalculator();
+ StoreController storeController = new StoreController(memberShipCalculator, promotionCalculateService, outputView, inputView);
+ storeController.run();
}
} | Java | controller ๋ด๋ถ์์ ์์ฑ์ ์ฃผ์
์ผ๋ก ์ด๋ฏธ ๋ช
์๋ฅผ ํด๋์๋๋ฐ, ๊ฐ์ฒด ์์ฑ์ ํ๋ฒ ๋ ํ์ ์ด์ ๊ฐ ๊ถ๊ธํด์!
InputView์ OutputView๋ static์ผ๋ก ํธ์ถํ๊ณ ์๋์ฒ๋ผ ํ์๋ฉด ์ด๋จ๊น์??
```suggestion
StoreController storeController = new StoreController(new MemberShipCalculator(), new PromotionCalculateService());
``` |
@@ -0,0 +1,79 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map.Entry;
+import store.domain.vo.MemberShip;
+import store.domain.vo.ProductQuantity;
+import store.view.dto.response.BuyProductResponse;
+import store.view.dto.response.FreeProductResponse;
+import store.view.dto.response.ProductCalculateResponse;
+
+public class CustomerReceipt {
+
+ private final Receipt receipt;
+ private final MemberShip memberShipDiscount;
+
+ public CustomerReceipt(Receipt receipt, MemberShip memberShipDiscount) {
+ this.receipt = receipt;
+ this.memberShipDiscount = memberShipDiscount;
+ }
+
+ public List<BuyProductResponse> showBuyProducts() {
+ List<BuyProductResponse> products = new ArrayList<>();
+ for (Entry<Product, ProductQuantity> entry : receipt.getReceipt().entrySet()) {
+ products.add(new BuyProductResponse(entry.getKey().getName(), entry.getKey().getQuantity(),
+ entry.getKey().getQuantity() * entry.getKey().getPrice()));
+ }
+ return Collections.unmodifiableList(products);
+ }
+
+ public List<FreeProductResponse> showFreeProductInfo() {
+ List<FreeProductResponse> freeProductResponses = new ArrayList<>();
+ for (Entry<Product, ProductQuantity> entry : receipt.getReceipt().entrySet()) {
+ freeProductResponses.add(
+ new FreeProductResponse(entry.getKey().getName(), entry.getValue().getQuantity())
+ );
+ }
+ return Collections.unmodifiableList(freeProductResponses);
+ }
+
+ public ProductCalculateResponse showCalculateInfo() {
+ return new ProductCalculateResponse(
+ productsAllQuantity(),
+ productsAllPrice(),
+ promotionPrice(),
+ memberShipDiscount.getDisCountPrice(),
+ totalPrice()
+ );
+ }
+
+ private int totalPrice() {
+ return productsAllPrice() - promotionPrice() - memberShipDiscount.getDisCountPrice();
+ }
+
+ private int productsAllPrice() {
+ return receipt.getReceipt()
+ .keySet()
+ .stream()
+ .mapToInt(product -> product.getPrice() * product.getQuantity())
+ .sum();
+ }
+
+ private int promotionPrice() {
+ return receipt.getReceipt()
+ .entrySet()
+ .stream()
+ .mapToInt(entry -> entry.getKey().getPrice() * entry.getValue().getQuantity())
+ .sum();
+ }
+
+ private int productsAllQuantity() {
+ return receipt.getReceipt()
+ .keySet()
+ .stream()
+ .mapToInt(Product::getQuantity)
+ .sum();
+ }
+} | Java | ๋ฉ์๋๋ช
์ด ๋ชจ๋ ์ง๊ด์ ์ด์ด์ ์ข๋ค์๐ |
@@ -0,0 +1,54 @@
+package store.domain;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class FileReader {
+
+ private static final String CONTENT_SPLIT_DELIMITER = ",";
+
+ private final List<List<String>> fileContents;
+
+ public FileReader(String filePath) {
+ this.fileContents = makeFileContents(filePath);
+ }
+
+ public List<List<String>> fileContents() {
+ return Collections.unmodifiableList(fileContents);
+ }
+
+ private List<List<String>> makeFileContents(String filePath) {
+ List<List<String>> fileContents = new ArrayList<>();
+
+ try (BufferedReader fileReader = new BufferedReader(new java.io.FileReader(filePath))) {
+ skipOneLine(fileReader);
+ makeFileContent(fileReader, fileContents);
+ } catch (IOException e) {
+ throw new ProductException(ProductErrorCode.NOT_FOUND_FILE_PATH);
+ }
+
+ return fileContents;
+ }
+
+ private void makeFileContent(BufferedReader fileReader, List<List<String>> fileContents) throws IOException {
+ String line;
+
+ while ((line = fileReader.readLine()) != null) {
+ List<String> contentInfo = splitProducts(line);
+ fileContents.add(contentInfo);
+ }
+ }
+
+ private void skipOneLine(BufferedReader fileReader) throws IOException {
+ fileReader.readLine();
+ }
+
+ private List<String> splitProducts(String productInformation) {
+ return List.of(productInformation.split(CONTENT_SPLIT_DELIMITER));
+ }
+} | Java | List<List<String>>์ผ๋ก ๊ตฌํํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,45 @@
+package store.domain;
+
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public enum ProductInfo {
+
+ COKE("์ฝ๋ผ", "1000"),
+ CIDER("์ฌ์ด๋ค", "1000"),
+ ORANGE_JUICE("์ค๋ ์ง์ฃผ์ค", "1800"),
+ SPARKLING_WATER("ํ์ฐ์", "1200"),
+ WATER("๋ฌผ", "500"),
+ VITAMIN_WATER("๋นํ๋ฏผ์ํฐ", "1500"),
+ POTATO_CHIPS("๊ฐ์์นฉ", "1500"),
+ CHOCOLATE_BAR("์ด์ฝ๋ฐ", "1200"),
+ ENERGY_BAR("์๋์ง๋ฐ", "2000"),
+ MEAL_BOX("์ ์๋์๋ฝ", "6400"),
+ CUP_NOODLES("์ปต๋ผ๋ฉด", "1700");
+
+ private final String name;
+ private final String price;
+
+ ProductInfo(String name, String price) {
+ this.name = name;
+ this.price = price;
+ }
+
+ public static String findPriceByName(String name) {
+ for (ProductInfo product : ProductInfo.values()) {
+ if (product.getName().equals(name)) {
+ return product.getPrice();
+ }
+ }
+
+ throw new ProductException(ProductErrorCode.NOT_FOUND_PRODUCT_NAME);
+ }
+
+ private String getName() {
+ return name;
+ }
+
+ private String getPrice() {
+ return price;
+ }
+} | Java | ํด๋น ๋ถ๋ถ์ ํ์ผ ๋ด์ ์๋ ๋ด์ฉ์ ํ์ฉํด์ ๊ตฌํํด์ผํ์ง ์๋์ฉ?? ๋ฐ๋ก ๋ช
์ํด๋์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,76 @@
+package store.view;
+
+import java.util.List;
+import store.view.dto.response.BuyProductResponse;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.response.FreeProductResponse;
+import store.view.dto.response.ProductCalculateResponse;
+
+public class OutputView {
+
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+
+ public void welcomeStore() {
+ System.out.println(LINE_SEPARATOR + "์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค." + LINE_SEPARATOR + "ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค." + LINE_SEPARATOR);
+ }
+
+ public void showInventory(String name, int price, String quantity, String promotionType) {
+ System.out.printf(
+ "- %s %,d์ %s %s"
+ + LINE_SEPARATOR,
+ name, price, quantity, promotionType
+ );
+ }
+
+ public void getCustomerProducts() {
+ System.out.println(LINE_SEPARATOR + "๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])");
+ }
+
+ public void guideBuyProducts() {
+ System.out.printf(
+ LINE_SEPARATOR +
+ "==============W ํธ์์ ================"
+ + LINE_SEPARATOR
+ + "%-10s %9s %9s%n"
+ , "์ํ๋ช
", "์๋" ,"๊ธ์ก"
+ );
+ }
+
+ public void showBuyProducts(List<BuyProductResponse> buyProducts) {
+ for (BuyProductResponse product : buyProducts) {
+ System.out.printf(
+ "%-10s %9d %,14d" + LINE_SEPARATOR,
+ product.name(), product.quantity(), product.totalPrice()
+ );
+ }
+ }
+
+ public void guideFreeGetQuantity() {
+ System.out.println("=============์ฆ\t\t์ ===============");
+ }
+
+ public void showFreeQuantity(List<FreeProductResponse> freeProductResponses) {
+ for (FreeProductResponse freeProductResponse : freeProductResponses) {
+ System.out.printf(
+ "%-10s %9d" + LINE_SEPARATOR,
+ freeProductResponse.name(), freeProductResponse.quantity()
+ );
+ }
+ }
+
+ public void showCalculateResult(ProductCalculateResponse calculateInfo) {
+ System.out.printf(
+ "====================================" + LINE_SEPARATOR
+ + "์ด๊ตฌ๋งค์ก\t\t\t\t%d\t\t %,d" + LINE_SEPARATOR
+ + "ํ์ฌํ ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR
+ + "๋ฉค๋ฒ์ญํ ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR
+ + "๋ด์ค๋\t\t\t\t\t\t %,d" + LINE_SEPARATOR
+ , calculateInfo.buyCounts(), calculateInfo.buyProductsPrice(),
+ calculateInfo.promotionPrice(), calculateInfo.memberShip(), calculateInfo.totalPrice()
+ );
+ }
+
+ public void showError(ErrorResponse errorResponse) {
+ System.out.println(errorResponse.message());
+ }
+} | Java | System.lineSeparator()๋ฅผ ํ์ฉํ ์ ์๊ตฐ์!! ๋ฐฐ์๊ฐ๋๋ค:) |
@@ -0,0 +1,6 @@
+package store.view.dto.request;
+
+public record MemberShipRequest(
+ boolean isMemberShip
+) {
+} | Java | ๋ถ๋ณ์์ ๋ช
์ํ ์ ์๋ record ์ฌ์ฉ ์ข๋ค์!!๐ |
@@ -0,0 +1,63 @@
+package store.domain;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class BuyProductParser {
+
+ private static final int NAME_INDEX = 1;
+ private static final int PRICE_INDEX = 2;
+ private static final String INPUT_DELIMITER = ",";
+ private static final String ORDER_FORM = "\\[(.+)-(.+)]";
+
+ private final Map<String, String> products;
+
+ public BuyProductParser(String buyProduct) {
+ this.products = makeProductsForm(buyProduct);
+ }
+
+ public Map<String, String> getProducts() {
+ return Collections.unmodifiableMap(products);
+ }
+
+ private Map<String, String> makeProductsForm(String buyProducts) {
+ Map<String, String> products = new LinkedHashMap<>();
+ parseProducts(buyProducts, products);
+ return products;
+ }
+
+ private void parseProducts(String buyProducts, Map<String, String> products) {
+ Pattern pattern = Pattern.compile(ORDER_FORM);
+ parseByRest(buyProducts).forEach(product -> {
+ validateFrom(product);
+ Matcher matcher = pattern.matcher(product);
+ validateMatcher(matcher);
+ products.put(matcher.group(NAME_INDEX).trim(), matcher.group(PRICE_INDEX).trim());
+ });
+ }
+
+ private void validateMatcher(Matcher matcher) {
+ if (!matcher.matches()) {
+ throw new ProductException(ProductErrorCode.WRONG_INPUT);
+ }
+ }
+
+ private List<String> parseByRest(String buyProduct) {
+ if (buyProduct.contains(INPUT_DELIMITER)) {
+ return List.of(buyProduct.split(INPUT_DELIMITER));
+ }
+ return List.of(buyProduct);
+ }
+
+ private void validateFrom(String buyProducts) {
+ if (!buyProducts.matches(ORDER_FORM)) {
+ throw new ProductException(ProductErrorCode.WRONG_INPUT);
+ }
+ }
+} | Java | parse ํด๋์ค๋ domain๋ณด๋ค util ํจํค์ง ๋ด์ ์๋๊ฒ ์ ํฉํ๋ค๊ณ ์๊ฐํ๋๋ฐ, ์ํ๋์ ์๊ฒฌ์ ์ด๋ ์ ๊ฐ์? |
@@ -0,0 +1,54 @@
+package store.domain;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class FileReader {
+
+ private static final String CONTENT_SPLIT_DELIMITER = ",";
+
+ private final List<List<String>> fileContents;
+
+ public FileReader(String filePath) {
+ this.fileContents = makeFileContents(filePath);
+ }
+
+ public List<List<String>> fileContents() {
+ return Collections.unmodifiableList(fileContents);
+ }
+
+ private List<List<String>> makeFileContents(String filePath) {
+ List<List<String>> fileContents = new ArrayList<>();
+
+ try (BufferedReader fileReader = new BufferedReader(new java.io.FileReader(filePath))) {
+ skipOneLine(fileReader);
+ makeFileContent(fileReader, fileContents);
+ } catch (IOException e) {
+ throw new ProductException(ProductErrorCode.NOT_FOUND_FILE_PATH);
+ }
+
+ return fileContents;
+ }
+
+ private void makeFileContent(BufferedReader fileReader, List<List<String>> fileContents) throws IOException {
+ String line;
+
+ while ((line = fileReader.readLine()) != null) {
+ List<String> contentInfo = splitProducts(line);
+ fileContents.add(contentInfo);
+ }
+ }
+
+ private void skipOneLine(BufferedReader fileReader) throws IOException {
+ fileReader.readLine();
+ }
+
+ private List<String> splitProducts(String productInformation) {
+ return List.of(productInformation.split(CONTENT_SPLIT_DELIMITER));
+ }
+} | Java | FileReader๋ util ํจํค์ง ๋ด์ ์๋๊ฒ ์ ํฉํ๋ค๊ณ ์๊ฐํฉ๋๋ค! |
@@ -0,0 +1,86 @@
+package store.domain;
+
+import java.util.Objects;
+import store.domain.vo.ProductName;
+import store.domain.vo.ProductPrice;
+import store.domain.vo.ProductQuantity;
+
+public class Product {
+
+ private final ProductName name;
+ private final ProductPrice price;
+ private final ProductQuantity quantity;
+ private final Promotion promotion;
+
+ public Product(String name, String price, String quantity, Promotion promotion) {
+ this.name = ProductName.from(name);
+ this.price = ProductPrice.from(price);
+ this.quantity = ProductQuantity.from(quantity);
+ this.promotion = promotion;
+ }
+
+ public String getName() {
+ return name.getName();
+ }
+
+ public int getPrice() {
+ return price.getPrice();
+ }
+
+ public int getQuantity() {
+ return quantity.getQuantity();
+ }
+
+ public String showQuantity() {
+ return quantity.showQuantity();
+ }
+
+ public String getPromotionType() {
+ return promotion.getPromotionType();
+ }
+
+ public boolean isPromotion() {
+ return promotion.isPromotion();
+ }
+
+ public int getPromotionBuyQuantity() {
+ return promotion.getBuyQuantity();
+ }
+
+ public int getPromotionGettableQuantity() {
+ return promotion.getGettableQuantity();
+ }
+
+ public int getPromotionQuantity() {
+ return (quantity.getQuantity() / getOnePromotionCycle()) * promotion.getGettableQuantity();
+ }
+
+ public int getOnePromotionCycle() {
+ return promotion.getOnePromotionCycle();
+ }
+
+ public void buyProduct(int buyQuantity) {
+ quantity.minusQuantity(buyQuantity);
+ }
+
+ public void freeProduct(int addQuantity) {
+ quantity.addQuantity(addQuantity);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ Product that = (Product) o;
+ return name.equals(that.name);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name);
+ }
+} | Java | ๋๋ฉ์ธ ํจํค์ง ๋ด์ ํด๋์ค๊ฐ ๋ง์ ๊ฒ ๊ฐ์์!
product์ promotion ๋ฑ์ ํจํค์ง๋ฅผ ํ๋ฒ ๋ ๋ง๋ค์ด์ ๋ถ๋ฆฌํด์ฃผ์๋ฉด ๋๋ฉ์ธ์ ์ญํ ์ด ๋ช
ํํ ๋ณด์ผ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,63 @@
+package store.domain;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class BuyProductParser {
+
+ private static final int NAME_INDEX = 1;
+ private static final int PRICE_INDEX = 2;
+ private static final String INPUT_DELIMITER = ",";
+ private static final String ORDER_FORM = "\\[(.+)-(.+)]";
+
+ private final Map<String, String> products;
+
+ public BuyProductParser(String buyProduct) {
+ this.products = makeProductsForm(buyProduct);
+ }
+
+ public Map<String, String> getProducts() {
+ return Collections.unmodifiableMap(products);
+ }
+
+ private Map<String, String> makeProductsForm(String buyProducts) {
+ Map<String, String> products = new LinkedHashMap<>();
+ parseProducts(buyProducts, products);
+ return products;
+ }
+
+ private void parseProducts(String buyProducts, Map<String, String> products) {
+ Pattern pattern = Pattern.compile(ORDER_FORM);
+ parseByRest(buyProducts).forEach(product -> {
+ validateFrom(product);
+ Matcher matcher = pattern.matcher(product);
+ validateMatcher(matcher);
+ products.put(matcher.group(NAME_INDEX).trim(), matcher.group(PRICE_INDEX).trim());
+ });
+ }
+
+ private void validateMatcher(Matcher matcher) {
+ if (!matcher.matches()) {
+ throw new ProductException(ProductErrorCode.WRONG_INPUT);
+ }
+ }
+
+ private List<String> parseByRest(String buyProduct) {
+ if (buyProduct.contains(INPUT_DELIMITER)) {
+ return List.of(buyProduct.split(INPUT_DELIMITER));
+ }
+ return List.of(buyProduct);
+ }
+
+ private void validateFrom(String buyProducts) {
+ if (!buyProducts.matches(ORDER_FORM)) {
+ throw new ProductException(ProductErrorCode.WRONG_INPUT);
+ }
+ }
+} | Java | private ๋ฉ์๋๋ก ๋ฉ์๋๋ฅผ ๋ถ๋ฆฌํ๋ ๊ฒ์ ์ด๋จ๊น์? |
@@ -0,0 +1,56 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class BuyProducts {
+
+ private final List<Product> products;
+
+ public BuyProducts(Map<String, String> splitProducts, Promotions promotions, Products products) {
+ this.products = createBuyProducts(splitProducts, promotions);
+ validateExistence(products);
+ validateQuantity(products);
+ }
+
+ public List<Product> products() {
+ return Collections.unmodifiableList(products);
+ }
+
+ private List<Product> createBuyProducts(Map<String, String> splitProducts, Promotions promotions) {
+ List<Product> products = new ArrayList<>();
+
+ for (Entry<String, String> entry : splitProducts.entrySet()) {
+ Product product = new Product(
+ entry.getKey(),
+ ProductInfo.findPriceByName(entry.getKey()),
+ entry.getValue(),
+ promotions.getPromotionByType(PromotionInfo.findPromotionByName(entry.getKey())));
+ products.add(product);
+ }
+
+ return products;
+ }
+
+ private void validateExistence(Products inventoryProducts) {
+ for (Product buyProduct : products) {
+ if (!inventoryProducts.getProducts().contains(buyProduct)) {
+ throw new ProductException(ProductErrorCode.NOT_FOUND_PRODUCT_NAME);
+ }
+ }
+ }
+
+ private void validateQuantity(Products inventoryProducts) {
+ for (Product buyProduct : products) {
+ int availableQuantity = inventoryProducts.countProductsByName(buyProduct.getName());
+ if (availableQuantity < buyProduct.getQuantity()) {
+ throw new ProductException(ProductErrorCode.OVER_QUANTITY);
+ }
+ }
+ }
+} | Java | ๋ฐํํด์ค๋ ๋ฐ์ดํฐ์ ์์ ์ฑ์ ์งํค๊ธฐ ์ํด ์ฌ์ฉํ์
จ๊ตฐ์ ๋ฐฐ์๊ฐ๋๋ค |
@@ -0,0 +1,56 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class BuyProducts {
+
+ private final List<Product> products;
+
+ public BuyProducts(Map<String, String> splitProducts, Promotions promotions, Products products) {
+ this.products = createBuyProducts(splitProducts, promotions);
+ validateExistence(products);
+ validateQuantity(products);
+ }
+
+ public List<Product> products() {
+ return Collections.unmodifiableList(products);
+ }
+
+ private List<Product> createBuyProducts(Map<String, String> splitProducts, Promotions promotions) {
+ List<Product> products = new ArrayList<>();
+
+ for (Entry<String, String> entry : splitProducts.entrySet()) {
+ Product product = new Product(
+ entry.getKey(),
+ ProductInfo.findPriceByName(entry.getKey()),
+ entry.getValue(),
+ promotions.getPromotionByType(PromotionInfo.findPromotionByName(entry.getKey())));
+ products.add(product);
+ }
+
+ return products;
+ }
+
+ private void validateExistence(Products inventoryProducts) {
+ for (Product buyProduct : products) {
+ if (!inventoryProducts.getProducts().contains(buyProduct)) {
+ throw new ProductException(ProductErrorCode.NOT_FOUND_PRODUCT_NAME);
+ }
+ }
+ }
+
+ private void validateQuantity(Products inventoryProducts) {
+ for (Product buyProduct : products) {
+ int availableQuantity = inventoryProducts.countProductsByName(buyProduct.getName());
+ if (availableQuantity < buyProduct.getQuantity()) {
+ throw new ProductException(ProductErrorCode.OVER_QUANTITY);
+ }
+ }
+ }
+} | Java | ์ ๋ entry๋ฅผ ์ฝํ
๋ฅผ ๋ณผ ๋ ๋ณ๋์ ํด๋์ค ์์ฑ์ด ํ์์๊ณ ํธํด ๋ง์ด ์ฌ์ฉํ๋๋ฐ ์ด๋ ๊ฐ๋
์ฑ ๋ฉด์์ ๋ง์ด ๋จ์ด์ง๋ค๊ณ ๋๋๋๋ค ์ด๋ณด๋ค๋ ๋ฐ๋ก DTO๋ฅผ ๋ง๋๋๊ฒ ๋์๊ฒ ๊ฐ์ต๋๋ค |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.