code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,68 @@
+import { ERROR_MESSAGES } from '../constants/messages.js';
+import { SYMBOLS } from '../constants/system.js';
+import validationErrorHandler from '../errors/index.js';
+import { isValidMenuName } from '../validators/index.js';
+import { isOnlyBeverage } from '../validators/is-valid-menu/name.js';
+import {
+ isValidEachMenuQuantity,
+ isValidTotalMenuQuantity,
+} from '../validators/is-valid-menu/quantity.js';
+
+class Order {
+ #orderList = new Set();
+
+ constructor(orderInput) {
+ this.#validate(orderInput);
+ this.#orderList = this.#parse(orderInput);
+ }
+
+ /**
+ * ์ฌ์ฉ์ ์
๋ ฅํ ์ฃผ๋ฌธ ๋ฉ๋ด์ ๋ฉ๋ด ๊ฐ์๋ฅผ ํ์ฑํ์ฌ ๋ฐฐ์ด๋ก ๋ฐํ
+ * @param {string} orderInput - ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๋ชจ๋ ์ฃผ๋ฌธ ๋ฉ๋ด์ ๋ฉ๋ด ๊ฐ์
+ * @returns {{ key: string, value: string }[]} - ์ฃผ๋ฌธ์ด ํ์ฑ๋ ๋ฐฐ์ด
+ */
+ #parse(orderInput) {
+ this.#splitOrder(orderInput);
+
+ return [...this.#orderList];
+ }
+
+ /**
+ * orderInput ๋ด ๊ฐ๋ณ ์ฃผ๋ฌธ๋ค์ ํ์ฑ
+ * @param {string} orderInput - ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๋ชจ๋ ์ฃผ๋ฌธ ๋ฉ๋ด์ ๋ฉ๋ด ๊ฐ์
+ * @returns {Set<{ menu: string, quantity: number }>} - ์ฃผ๋ฌธ์ด ํ์ฑ๋ Set
+ */
+ #splitOrder(orderInput) {
+ return orderInput.split(SYMBOLS.comma).map(order => this.#splitMenu(order));
+ }
+
+ /**
+ * ์ฃผ๋ฌธ ๋ฉ๋ด๋ช
๊ณผ ๊ฐ์๋ฅผ orderList Set์ ์ถ๊ฐ
+ * @param {string} order ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ํน์ ์ฃผ๋ฌธ ๋ฉ๋ด์ ๋ฉ๋ด ๊ฐ์
+ */
+ #splitMenu(order) {
+ const [menu, quantity] = order.split(SYMBOLS.dash);
+
+ this.#orderList.add({ menu: menu.trim(), quantity: Number(quantity) });
+ }
+
+ #validate(orderInput) {
+ if (!isValidMenuName(orderInput) || !isValidEachMenuQuantity(orderInput)) {
+ validationErrorHandler(ERROR_MESSAGES.invalidMenu);
+ }
+
+ if (!isValidTotalMenuQuantity(orderInput)) {
+ validationErrorHandler(ERROR_MESSAGES.exceedQuantity);
+ }
+
+ if (isOnlyBeverage(orderInput)) {
+ validationErrorHandler(ERROR_MESSAGES.onlyBeverage);
+ }
+ }
+
+ getOrderList() {
+ return this.#orderList;
+ }
+}
+
+export default Order; | JavaScript | ์ ์ฝ๋๋ ์ค์ค๋ก ๋ค์ ๋์กํ ์ฝ๋๋ผ๊ณ ์๊ฐ์ด ๋๋๋ฐ, ๋คํ์
๋๋ค ํํ |
@@ -0,0 +1,15 @@
+import { UNITS } from '../constants/system.js';
+
+/**
+ * number type์ ๊ฐ๊ฒฉ์ ์
๋ ฅ๋ฐ์ ๊ฐ๊ฒฉ์ ๋ณํ ํ '์'์ ํฌํจํ string type์ผ๋ก ๋ณํ
+ * 20000์ด๋ผ๋ ๋งค๊ฐ๋ณ์๋ฅผ ํตํด '20,000์'์ ๋ฐํ
+ * @param {number} price ๊ฐ๊ฒฉ
+ * @returns {string}
+ */
+const priceFormatter = price => {
+ const formattedPrice = new Intl.NumberFormat().format(price);
+
+ return `${formattedPrice}${UNITS.won}`;
+};
+
+export default priceFormatter; | JavaScript | ์ ๋ ์ด ๋ถ๋ถ์ [Number.toLocaleString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString)์ ์ ์ฉํ๋๋ฐ, ์ซ์ ํ์๊ณผ ๊ด๋ จ๋ ๋ถ๋ถ์ Intl ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋๊ฒ ๋ ์ ํํ ๊ฒ ๊ฐ๋ค์ ๐ (๋ด๋ถ์ ์ผ๋ก ๊ฐ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฑฐ ๊ฐ์์ ใ
ใ
) |
@@ -0,0 +1,24 @@
+const MENUS = Object.freeze({
+ appetizer: {
+ ์์ก์ด์ํ: 6_000,
+ ํํ์ค: 5_500,
+ ์์ ์๋ฌ๋: 8_000,
+ },
+ main: {
+ ํฐ๋ณธ์คํ
์ดํฌ: 55_000,
+ ๋ฐ๋นํ๋ฆฝ: 54_000,
+ ํด์ฐ๋ฌผํ์คํ: 35_000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25_000,
+ },
+ dessert: {
+ ์ด์ฝ์ผ์ดํฌ: 15_000,
+ ์์ด์คํฌ๋ฆผ: 5_000,
+ },
+ beverage: {
+ ์ ๋ก์ฝ๋ผ: 3_000,
+ ๋ ๋์์ธ: 60_000,
+ ์ดํ์ธ: 25_000,
+ },
+});
+
+export default MENUS; | JavaScript | ํน์ menu๋ data ํด๋์ ๋ฃ๋ ๋ฐฉํฅ๋ ์๊ฐํด ๋ณด์
จ์๊น์? ์ ๊ฐ์ ๊ฒฝ์ฐ์ ์์๋ก ๋ฌถ๊ธฐ์ ๊ฑธ๋ฆฌ๋ ๋ถ๋ถ๋ค์ด ์์๋ ๊ฒ ๊ฐ์์์! ๊ฐ์ธ์ ์ผ๋ก ๊ถ๊ธํด์ ์ฌ์ญค๋ด
๋๋ค! ๐ |
@@ -1,5 +1,15 @@
+import EventController from './controller/index.js';
+
class App {
- async run() {}
+ #controller;
+
+ constructor() {
+ this.#controller = new EventController();
+ }
+
+ async run() {
+ await this.#controller.startEvent();
+ }
}
export default App; | JavaScript | App์ด ์ ๋ง ๊น๋ํ๋ค์..! ๋ค์ ์จ๊ฒจ์ง ์ฝ๋๋ค ๋ณด๊ณ ๋ง์ด ๋ฐฐ์๊ฐ๋๋ค! ๐โโ๏ธ |
@@ -0,0 +1,52 @@
+import { PROMOTION_TITLES, TIME, UNITS } from './system.js';
+
+export const DISCOUNT_PRICES = Object.freeze({
+ dDay: 1_000,
+ dDayInc: 100,
+ week: 2_023,
+ special: 1_000,
+});
+
+export const BADGES = Object.freeze({
+ star: { title: '๋ณ', price: 5_000 },
+ tree: { title: 'ํธ๋ฆฌ', price: 10_000 },
+ santa: { title: '์ฐํ', price: 20_000 },
+});
+
+export const TITLES = Object.freeze({
+ dDay: `${PROMOTION_TITLES[TIME.month]} ๋๋ฐ์ด ํ ์ธ`,
+ weekday: 'ํ์ผ ํ ์ธ',
+ weekend: '์ฃผ๋ง ํ ์ธ',
+ special: 'ํน๋ณ ํ ์ธ',
+ giveaway: '์ฆ์ ์ด๋ฒคํธ',
+});
+
+export const GIVEAWAYS = Object.freeze({
+ giveaway: '์ดํ์ธ',
+ giveawayUnit: 1,
+ giveawayPrice: 120_000,
+});
+
+export const ORDER = Object.freeze({
+ minPrice: 10_000,
+ minQuantity: 1,
+ maxQuantity: 20,
+});
+
+export const DATES = Object.freeze({
+ startDate: 1,
+ endDate: 31,
+ christmas: 25,
+ specialDayIndex: 0,
+ weekendIndex: 5,
+});
+
+export const BENEFITS = Object.freeze({
+ menu: '<์ฃผ๋ฌธ ๋ฉ๋ด>',
+ preTotalPrice: '<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>',
+ giveaway: '<์ฆ์ ๋ฉ๋ด>',
+ benefitList: '<ํํ ๋ด์ญ>',
+ totalBenefitPrice: '<์ดํํ ๊ธ์ก>',
+ totalPrice: '<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>',
+ badge: `<${TIME.month}${UNITS.month} ์ด๋ฒคํธ ๋ฐฐ์ง>`,
+}); | JavaScript | `dDayInc`๋ณด๋ค๋ ์ข๋ ๊ตฌ์ฒด์ ์ธ ์์๋ช
์ ์ด๋จ๊น์? |
@@ -0,0 +1,52 @@
+import { PROMOTION_TITLES, TIME, UNITS } from './system.js';
+
+export const DISCOUNT_PRICES = Object.freeze({
+ dDay: 1_000,
+ dDayInc: 100,
+ week: 2_023,
+ special: 1_000,
+});
+
+export const BADGES = Object.freeze({
+ star: { title: '๋ณ', price: 5_000 },
+ tree: { title: 'ํธ๋ฆฌ', price: 10_000 },
+ santa: { title: '์ฐํ', price: 20_000 },
+});
+
+export const TITLES = Object.freeze({
+ dDay: `${PROMOTION_TITLES[TIME.month]} ๋๋ฐ์ด ํ ์ธ`,
+ weekday: 'ํ์ผ ํ ์ธ',
+ weekend: '์ฃผ๋ง ํ ์ธ',
+ special: 'ํน๋ณ ํ ์ธ',
+ giveaway: '์ฆ์ ์ด๋ฒคํธ',
+});
+
+export const GIVEAWAYS = Object.freeze({
+ giveaway: '์ดํ์ธ',
+ giveawayUnit: 1,
+ giveawayPrice: 120_000,
+});
+
+export const ORDER = Object.freeze({
+ minPrice: 10_000,
+ minQuantity: 1,
+ maxQuantity: 20,
+});
+
+export const DATES = Object.freeze({
+ startDate: 1,
+ endDate: 31,
+ christmas: 25,
+ specialDayIndex: 0,
+ weekendIndex: 5,
+});
+
+export const BENEFITS = Object.freeze({
+ menu: '<์ฃผ๋ฌธ ๋ฉ๋ด>',
+ preTotalPrice: '<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>',
+ giveaway: '<์ฆ์ ๋ฉ๋ด>',
+ benefitList: '<ํํ ๋ด์ญ>',
+ totalBenefitPrice: '<์ดํํ ๊ธ์ก>',
+ totalPrice: '<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>',
+ badge: `<${TIME.month}${UNITS.month} ์ด๋ฒคํธ ๋ฐฐ์ง>`,
+}); | JavaScript | ์ํด ์ด๋ฒคํธ๊น์ง๋ ์๊ฐ์ํด๋ดค๋๋ฐ ๊ผผ๊ผผํ์ญ๋๋ค!๐ |
@@ -0,0 +1,15 @@
+import { UNITS } from '../constants/system.js';
+
+/**
+ * number type์ ๊ฐ๊ฒฉ์ ์
๋ ฅ๋ฐ์ ๊ฐ๊ฒฉ์ ๋ณํ ํ '์'์ ํฌํจํ string type์ผ๋ก ๋ณํ
+ * 20000์ด๋ผ๋ ๋งค๊ฐ๋ณ์๋ฅผ ํตํด '20,000์'์ ๋ฐํ
+ * @param {number} price ๊ฐ๊ฒฉ
+ * @returns {string}
+ */
+const priceFormatter = price => {
+ const formattedPrice = new Intl.NumberFormat().format(price);
+
+ return `${formattedPrice}${UNITS.won}`;
+};
+
+export default priceFormatter; | JavaScript | ๋ง์ต๋๋ค! ์ํ๋ ๋ง์์ฒ๋ผ ๊ฒฐ๊ตญ `toLocaleString()`์ด ๊ถ๊ทน์ ์ผ๋ก `Intl.NumberFormat` API๋ฅผ ์ฌ์ฉํ๋๋ผ๊ตฌ์ :)
์ด๋ป๊ฒ๋ณด๋ฉด ์ข๋ ์์ฌ์ด ์ฌ์ฉ์ ์ํ `toLocaleString()` ๋ฉ์๋ ์ฌ์ฉ์ด ๋ ๋ชฉ์ ์ ๋ถํฉํ ์๋ ์๋ค๊ณ ์๊ฐ์ด ๋๋ค์ :) |
@@ -0,0 +1,36 @@
+import EventCalendar from '../../src/domain/EventCalendar';
+import { CHRISTMAS_D_DAY, GIFT, SPECIAL } from '../../src/domain/Events';
+
+import {
+ EVENT_MONTH,
+ EVENT_NAMES,
+ EVENT_YEAR,
+} from '../../src/constants/event';
+import { MAIN_COURSES } from '../../src/constants/menus';
+
+describe('EventCalendar ํ
์คํธ', () => {
+ test('getEventBenefit ๊ธฐ๋ฅ ํ
์คํธ', () => {
+ // given
+ const YEAR = EVENT_YEAR;
+ const MONTH = EVENT_MONTH;
+ const DATE = 25;
+ const TOTAL_PRICE = 130_000;
+ const ORDER_CATEGORIES = [MAIN_COURSES];
+
+ const RESULT = {
+ [EVENT_NAMES.CHRISTMAS_D_DAY]: CHRISTMAS_D_DAY.getEvent().getBenefit,
+ [EVENT_NAMES.SPECIAL]: SPECIAL.getEvent().getBenefit,
+ [EVENT_NAMES.GIFT]: GIFT.getEvent().getBenefit,
+ };
+
+ // when
+ const eventCalendar = new EventCalendar(YEAR, MONTH, DATE);
+ eventCalendar.setAvailableEvents({
+ totalPrice: TOTAL_PRICE,
+ orderCategories: ORDER_CATEGORIES,
+ });
+
+ //then
+ expect(eventCalendar.availableEvents).toEqual(RESULT);
+ });
+}); | JavaScript | ํ
์คํธ ์ฝ๋์์ ์ค์ ์ฝ๋์์ ์ฌ์ฉ๋๋ ์์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ์ข์ง ์์ ๋ณด์
๋๋ค. |
@@ -0,0 +1,117 @@
+import { EVENT_NAMES } from '../../src/constants/event';
+import EventPlanner from '../../src/services/EventPlanner';
+
+const ORDERS = [
+ { menu: '์์ก์ด์ํ', amount: 1 },
+ { menu: 'ํํ์ค', amount: 1 },
+ { menu: 'ํฐ๋ณธ์คํ
์ดํฌ', amount: 2 },
+ { menu: '์ด์ฝ์ผ์ดํฌ', amount: 1 },
+ { menu: '์์ด์คํฌ๋ฆผ', amount: 1 },
+];
+
+async function getEventPlanner(orders) {
+ // given
+ const VISIT_DATE = 25;
+
+ // when
+ const eventPlanner = new EventPlanner(VISIT_DATE);
+ await eventPlanner.generateReceipt(orders);
+ await eventPlanner.generateBenefits();
+
+ return eventPlanner;
+}
+
+describe('EventPlanner ๊ธฐ๋ฅ ํ
์คํธ', () => {
+ test.each([32, 3.1])('validate - ์ ์ ์๋', async (input) => {
+ expect(() => new EventPlanner(input)).toThrow('[ERROR]');
+ });
+});
+
+describe('EventPlanner ํํ ์กฐํ ํ
์คํธ', () => {
+ test('์ ๊ทผ์ ํ๋กํผํฐ ํ
์คํธ - gift', async () => {
+ const gift = {
+ menu: '์ดํ์ธ',
+ amount: 1,
+ price: 25_000,
+ };
+
+ const eventPlanner = await getEventPlanner(ORDERS);
+
+ expect(eventPlanner.gift).toEqual(gift);
+ });
+
+ test('์ ๊ทผ์ ํ๋กํผํฐ ํ
์คํธ - benefits', async () => {
+ const benefits = {
+ [EVENT_NAMES.CHRISTMAS_D_DAY]: { amount: 1, price: 3400 },
+ [EVENT_NAMES.SPECIAL]: { amount: 1, price: 1000 },
+ [EVENT_NAMES.WEEKDAY]: { amount: 2, price: 2023 },
+ [EVENT_NAMES.GIFT]: {
+ menu: '์ดํ์ธ',
+ amount: 1,
+ price: 25_000,
+ },
+ };
+
+ const eventPlanner = await getEventPlanner(ORDERS);
+
+ expect(eventPlanner.benefits).toEqual(benefits);
+ });
+
+ test('์ ๊ทผ์ ํ๋กํผํฐ ํ
์คํธ - totalBenefitMoney', async () => {
+ const totalBenefitMoney = 33_446;
+
+ const eventPlanner = await getEventPlanner(ORDERS);
+
+ expect(eventPlanner.totalBenefitMoney).toBe(totalBenefitMoney);
+ });
+
+ test('์ ๊ทผ์ ํ๋กํผํฐ ํ
์คํธ - payment', async () => {
+ const originalPrice = 141_500;
+ const discountAmount = 8_446;
+
+ const eventPlanner = await getEventPlanner(ORDERS);
+
+ expect(eventPlanner.payment).toBe(originalPrice - discountAmount);
+ });
+
+ test.each([
+ {
+ orders: [
+ { menu: '์์ก์ด์ํ', amount: 1 },
+ { menu: 'ํํ์ค', amount: 1 },
+ { menu: 'ํฐ๋ณธ์คํ
์ดํฌ', amount: 2 },
+ { menu: '์ด์ฝ์ผ์ดํฌ', amount: 1 },
+ { menu: '์์ด์คํฌ๋ฆผ', amount: 1 },
+ ],
+ badge: '์ฐํ',
+ },
+ {
+ orders: [
+ { menu: '์์ก์ด์ํ', amount: 1 },
+ { menu: 'ํํ์ค', amount: 1 },
+ { menu: 'ํฐ๋ณธ์คํ
์ดํฌ', amount: 1 },
+ { menu: '์์ด์คํฌ๋ฆผ', amount: 4 },
+ ],
+ badge: 'ํธ๋ฆฌ',
+ },
+ {
+ orders: [
+ { menu: '์์ก์ด์ํ', amount: 1 },
+ { menu: 'ํํ์ค', amount: 1 },
+ { menu: '์์ด์คํฌ๋ฆผ', amount: 1 },
+ ],
+ badge: '๋ณ',
+ },
+ {
+ orders: [
+ { menu: '์์ก์ด์ํ', amount: 1 },
+ { menu: 'ํํ์ค', amount: 1 },
+ ],
+ badge: '์์',
+ },
+ ])('์ ๊ทผ์ ํ๋กํผํฐ ํ
์คํธ - badge', async ({ orders, badge }) => {
+ const eventPlanner = await getEventPlanner(orders);
+
+ expect(eventPlanner.badge).toBe(badge);
+ });
+}); | JavaScript | "\_"์๋กญ๊ฒ ๋ฐฐ์๊ฐ๋๋ค! |
@@ -0,0 +1,117 @@
+import { EVENT_NAMES } from '../constants/event.js';
+import { DESSERTS, MAIN_COURSES } from '../constants/menus.js';
+
+export const CHRISTMAS_D_DAY = {
+ getBenefit({ date }) {
+ const baseAmount = 1000;
+ const weightAmount = 100;
+
+ return {
+ amount: 1,
+ price: baseAmount + weightAmount * (date - 1),
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.CHRISTMAS_D_DAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isChristmasPeriod }) {
+ return isChristmasPeriod === true;
+ },
+};
+
+export const WEEKDAY = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[DESSERTS],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKDAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === false && orderCategories.includes(DESSERTS);
+ },
+};
+
+export const WEEKEND = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[MAIN_COURSES],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKEND,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === true && orderCategories.includes(MAIN_COURSES);
+ },
+};
+
+export const SPECIAL = {
+ getBenefit() {
+ const discountAmount = 1_000;
+ return {
+ amount: 1,
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.SPECIAL,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isSpecialDate }) {
+ return isSpecialDate === true;
+ },
+};
+
+export const GIFT = {
+ getBenefit() {
+ return {
+ menu: '์ดํ์ธ',
+ amount: 1,
+ price: 25_000,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.GIFT,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ totalPrice }) {
+ return totalPrice >= 120_000;
+ },
+};
+
+export const ALL_EVENTS = Object.freeze({
+ CHRISTMAS_D_DAY,
+ WEEKDAY,
+ WEEKEND,
+ SPECIAL,
+ GIFT,
+}); | JavaScript | isWeekend === true๋ ๋ถํ์ํด ๋ณด์ด๋๋ฐ, ์ฌ์ฉํ์ ์ด์ ๊ฐ ์์ผ์ ๊ฐ์? |
@@ -0,0 +1,117 @@
+import { EVENT_NAMES } from '../constants/event.js';
+import { DESSERTS, MAIN_COURSES } from '../constants/menus.js';
+
+export const CHRISTMAS_D_DAY = {
+ getBenefit({ date }) {
+ const baseAmount = 1000;
+ const weightAmount = 100;
+
+ return {
+ amount: 1,
+ price: baseAmount + weightAmount * (date - 1),
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.CHRISTMAS_D_DAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isChristmasPeriod }) {
+ return isChristmasPeriod === true;
+ },
+};
+
+export const WEEKDAY = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[DESSERTS],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKDAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === false && orderCategories.includes(DESSERTS);
+ },
+};
+
+export const WEEKEND = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[MAIN_COURSES],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKEND,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === true && orderCategories.includes(MAIN_COURSES);
+ },
+};
+
+export const SPECIAL = {
+ getBenefit() {
+ const discountAmount = 1_000;
+ return {
+ amount: 1,
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.SPECIAL,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isSpecialDate }) {
+ return isSpecialDate === true;
+ },
+};
+
+export const GIFT = {
+ getBenefit() {
+ return {
+ menu: '์ดํ์ธ',
+ amount: 1,
+ price: 25_000,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.GIFT,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ totalPrice }) {
+ return totalPrice >= 120_000;
+ },
+};
+
+export const ALL_EVENTS = Object.freeze({
+ CHRISTMAS_D_DAY,
+ WEEKDAY,
+ WEEKEND,
+ SPECIAL,
+ GIFT,
+}); | JavaScript | ๋งค์ง ๋๋ฒ๋ฅผ ์ฌ์ฉํ์๋ ๊ฒ ๊ฐ์ผ์ ๋ฐ์, ํน์ ๋ณธ์ธ๋ง์ ๊ธฐ์ค์ด ์์ผ์ค๊น์? ์ด๊ณณ์์๋ง ๋ฐ๋ก ๋ถ๋ฆฌ๋ฅผ ํ์ง ์์ผ์ ๊ฒ ๊ฐ์์์. |
@@ -0,0 +1,47 @@
+import { Console } from '@woowacourse/mission-utils';
+import MESSAGES from '../constants/messages.js';
+
+const OutputView = {
+ printResult(msgObj, result) {
+ const { title, printMsg } = msgObj;
+
+ Console.print(title);
+ Console.print(printMsg(result));
+ },
+
+ printStart() {
+ Console.print(MESSAGES.outputs.sayHi);
+ },
+
+ printMenus({ visitDate, menus }) {
+ Console.print(MESSAGES.outputs.eventPreview(visitDate));
+
+ this.printResult(MESSAGES.outputs.menus, menus);
+ },
+
+ printOriginalPrice(price) {
+ this.printResult(MESSAGES.outputs.originalPrice, price);
+ },
+
+ printGift(gift) {
+ this.printResult(MESSAGES.outputs.gift, gift);
+ },
+
+ printBenefits(benefits) {
+ this.printResult(MESSAGES.outputs.benefits, benefits);
+ },
+
+ printTotalBenefitMoney(money) {
+ this.printResult(MESSAGES.outputs.totalBenefitMoney, money);
+ },
+
+ printPayment(money) {
+ this.printResult(MESSAGES.outputs.payment, money);
+ },
+
+ printBadge(badge) {
+ this.printResult(MESSAGES.outputs.badge, badge);
+ },
+};
+
+export default OutputView; | JavaScript | ์ด๋ฐ์์ผ๋ก ๋ณด๋ด๋ฉด ์กฐ๊ธ ๋ ๊น๋ํด์ง๋๊ตฐ์...!!! |
@@ -0,0 +1,94 @@
+import OrderItem from './OrderItem.js';
+
+import MESSAGES from '../constants/messages.js';
+
+import throwError from '../utils/throwError.js';
+import {
+ hasDuplicatedMenu,
+ hasOnlyBeverages,
+ isTotalAmountOfMenusValid,
+} from '../utils/validators.js';
+
+class Receipt {
+ #orders;
+
+ #orderItems;
+
+ constructor(orders) {
+ this.#validate(orders);
+ this.#orders = orders;
+ this.#orderItems = [];
+ }
+
+ #validate(orders) {
+ if (!isTotalAmountOfMenusValid(orders))
+ throwError(MESSAGES.errors.invalidOrders);
+
+ if (hasDuplicatedMenu(orders)) throwError(MESSAGES.errors.invalidOrders);
+
+ if (hasOnlyBeverages(orders)) throwError(MESSAGES.errors.invalidOrders);
+ }
+
+ generateOrders() {
+ this.#orderItems = this.#orders.map(
+ ({ menu, amount }) => new OrderItem(menu, amount),
+ );
+ }
+
+ get totalPrice() {
+ return this.#orderItems.reduce(
+ (totalPrice, orderItem) => totalPrice + orderItem.totalPrice,
+ 0,
+ );
+ }
+
+ get receipt() {
+ const receipt = {};
+
+ this.#orderItems.forEach((orderItem) => {
+ const { category, menu, amount } = orderItem.item;
+
+ if (!Array.isArray(receipt[category])) {
+ receipt[category] = [];
+ }
+
+ receipt[category].push({ menu, amount });
+ });
+
+ return receipt;
+ }
+
+ get orderCategories() {
+ const categories = new Set();
+
+ this.#orderItems.forEach((orderItem) => {
+ const { category } = orderItem.item;
+ categories.add(category);
+ });
+
+ return [...categories];
+ }
+
+ get orderCntByCategory() {
+ const orderCnt = {};
+
+ this.#orderItems.forEach((orderItem) => {
+ const { category, amount } = orderItem.item;
+
+ if (!orderCnt[category]) orderCnt[category] = 0;
+
+ orderCnt[category] += amount;
+ });
+
+ return orderCnt;
+ }
+
+ get menus() {
+ return this.#orderItems.map((orderItem) => {
+ const { menu, amount } = orderItem.item;
+ return { menu, amount };
+ });
+ }
+}
+
+export default Receipt; | JavaScript | get์ ์ฌ์ฉ๋ฒ ๋ฐฐ์ฐ๊ณ ๊ฐ๋๋ค...! |
@@ -0,0 +1,46 @@
+import MESSAGES from '../constants/messages.js';
+import { CATEGORIES, MENUS } from '../constants/menus.js';
+
+import { getValueOfField } from '../utils/object.js';
+import throwError from '../utils/throwError.js';
+import { isMenuExists } from '../utils/validators.js';
+
+class OrderItem {
+ #category;
+
+ #menu;
+
+ #amount;
+
+ constructor(menu, amount) {
+ this.#validate(menu);
+ this.#setCategory(menu);
+ this.#menu = menu;
+ this.#amount = amount;
+ }
+
+ #validate(menu) {
+ if (!isMenuExists(menu)) throwError(MESSAGES.errors.invalidMenu);
+ }
+
+ #setCategory(menu) {
+ this.#category = CATEGORIES.find((category) =>
+ getValueOfField(MENUS, `${category}.${menu}`),
+ );
+ }
+
+ get totalPrice() {
+ const price = getValueOfField(MENUS, `${this.#category}.${this.#menu}`);
+ return price * this.#amount;
+ }
+
+ get item() {
+ return {
+ category: this.#category,
+ menu: this.#menu,
+ amount: this.#amount,
+ };
+ }
+}
+
+export default OrderItem; | JavaScript | get set ํจ์๋ฅผ ์ด๋ ๊ฒ ์ง์ ํ ์ ์๋ค๋ ๊ฑธ ์ฒ์ ์๊ฒ๋์๋ค์! |
@@ -0,0 +1,38 @@
+/**
+ * ๋ ๊ฐ์ฒด๊ฐ ๊ฐ์ ๊ฐ์ ๊ฐ์ง๊ณ ์๋์ง ์๊ฒ ๋น๊ต
+ * @param {object} obj1
+ * @param {object} obj2
+ */
+export const shallowEqual = (objA, objB) => {
+ let result = true;
+
+ // ๋ ๊ฐ์ฒด์ key ๊ฐ์ ๋น๊ต
+ const keysA = Object.keys(objA);
+ const keysB = Object.keys(objB);
+ if (keysA.length !== keysB.length) result = false;
+
+ // ๋ ๊ฐ์ฒด์ value ๊ฐ ๋น๊ต
+ keysA.forEach((key) => {
+ if (objA[key] !== objB[key]) result = false;
+ });
+
+ return result;
+};
+
+/**
+ * ์ ํ๊ธฐ๋ฒ(.)์ ์ฌ์ฉํด์ ์ค์ฒฉ ๊ฐ์ฒด์ ๊ฐ์ ์ฐพ๋ ํจ์
+ * ์ฐธ๊ณ : https://elvanov.com/2286
+ * @param {object} obj
+ * @param {string} field
+ */
+export const getValueOfField = (obj, field) => {
+ if (!field) {
+ return null;
+ }
+
+ const keys = field.split('.');
+
+ return keys.reduce((curObj, curKey) => (curObj ? curObj[curKey] : null), obj);
+};
+
+export const isEmptyObject = (obj) => JSON.stringify(obj) === '{}'; | JavaScript | ๋ด์ฉ ์ ๋ดค์ต๋๋ค ใ
ใ
|
@@ -0,0 +1,36 @@
+import EventCalendar from '../../src/domain/EventCalendar';
+import { CHRISTMAS_D_DAY, GIFT, SPECIAL } from '../../src/domain/Events';
+
+import {
+ EVENT_MONTH,
+ EVENT_NAMES,
+ EVENT_YEAR,
+} from '../../src/constants/event';
+import { MAIN_COURSES } from '../../src/constants/menus';
+
+describe('EventCalendar ํ
์คํธ', () => {
+ test('getEventBenefit ๊ธฐ๋ฅ ํ
์คํธ', () => {
+ // given
+ const YEAR = EVENT_YEAR;
+ const MONTH = EVENT_MONTH;
+ const DATE = 25;
+ const TOTAL_PRICE = 130_000;
+ const ORDER_CATEGORIES = [MAIN_COURSES];
+
+ const RESULT = {
+ [EVENT_NAMES.CHRISTMAS_D_DAY]: CHRISTMAS_D_DAY.getEvent().getBenefit,
+ [EVENT_NAMES.SPECIAL]: SPECIAL.getEvent().getBenefit,
+ [EVENT_NAMES.GIFT]: GIFT.getEvent().getBenefit,
+ };
+
+ // when
+ const eventCalendar = new EventCalendar(YEAR, MONTH, DATE);
+ eventCalendar.setAvailableEvents({
+ totalPrice: TOTAL_PRICE,
+ orderCategories: ORDER_CATEGORIES,
+ });
+
+ //then
+ expect(eventCalendar.availableEvents).toEqual(RESULT);
+ });
+}); | JavaScript | ํ
์คํธ ์ฝ๋์ ์์กด์ฑ์ ์ค์ฌ์ผ ํ๊ธฐ ๋๋ฌธ์ผ๊น์? ๊ทธ๋ ๊ฒ ์๊ฐํ์๋ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค..! |
@@ -0,0 +1,117 @@
+import { EVENT_NAMES } from '../constants/event.js';
+import { DESSERTS, MAIN_COURSES } from '../constants/menus.js';
+
+export const CHRISTMAS_D_DAY = {
+ getBenefit({ date }) {
+ const baseAmount = 1000;
+ const weightAmount = 100;
+
+ return {
+ amount: 1,
+ price: baseAmount + weightAmount * (date - 1),
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.CHRISTMAS_D_DAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isChristmasPeriod }) {
+ return isChristmasPeriod === true;
+ },
+};
+
+export const WEEKDAY = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[DESSERTS],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKDAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === false && orderCategories.includes(DESSERTS);
+ },
+};
+
+export const WEEKEND = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[MAIN_COURSES],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKEND,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === true && orderCategories.includes(MAIN_COURSES);
+ },
+};
+
+export const SPECIAL = {
+ getBenefit() {
+ const discountAmount = 1_000;
+ return {
+ amount: 1,
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.SPECIAL,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isSpecialDate }) {
+ return isSpecialDate === true;
+ },
+};
+
+export const GIFT = {
+ getBenefit() {
+ return {
+ menu: '์ดํ์ธ',
+ amount: 1,
+ price: 25_000,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.GIFT,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ totalPrice }) {
+ return totalPrice >= 120_000;
+ },
+};
+
+export const ALL_EVENTS = Object.freeze({
+ CHRISTMAS_D_DAY,
+ WEEKDAY,
+ WEEKEND,
+ SPECIAL,
+ GIFT,
+}); | JavaScript | ๋ฌผ๋ก Truthy๋ฅผ ์ฌ์ฉํ ์๋ ์๋ ์ํฉ์ด์ง๋ง, ์ค์ ๋ก ์ด์ํ๋ ์๋น์ค๋ผ๋ฉด ์ํ๊ฐ ์ด์ํ๊ฑฐ๋ ์๋ฒ์์ ์๋ชป๋ ๊ฐ์ด ๋ ์์์ ๋ ์ก์ ์ ์๋ ์ฝ๋์ฌ์ผ ํ๋ค๊ณ ์๊ฐํ์ต๋๋ค! ๊ทธ๋์ ํ์
๊ณผ ๊ฐ์ ์ ํํ ๊ฒ์ฌํ๋ ๋ฐฉ์์ผ๋ก ์์ฑํ์ด์. |
@@ -0,0 +1,117 @@
+import { EVENT_NAMES } from '../constants/event.js';
+import { DESSERTS, MAIN_COURSES } from '../constants/menus.js';
+
+export const CHRISTMAS_D_DAY = {
+ getBenefit({ date }) {
+ const baseAmount = 1000;
+ const weightAmount = 100;
+
+ return {
+ amount: 1,
+ price: baseAmount + weightAmount * (date - 1),
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.CHRISTMAS_D_DAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isChristmasPeriod }) {
+ return isChristmasPeriod === true;
+ },
+};
+
+export const WEEKDAY = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[DESSERTS],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKDAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === false && orderCategories.includes(DESSERTS);
+ },
+};
+
+export const WEEKEND = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[MAIN_COURSES],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKEND,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === true && orderCategories.includes(MAIN_COURSES);
+ },
+};
+
+export const SPECIAL = {
+ getBenefit() {
+ const discountAmount = 1_000;
+ return {
+ amount: 1,
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.SPECIAL,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isSpecialDate }) {
+ return isSpecialDate === true;
+ },
+};
+
+export const GIFT = {
+ getBenefit() {
+ return {
+ menu: '์ดํ์ธ',
+ amount: 1,
+ price: 25_000,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.GIFT,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ totalPrice }) {
+ return totalPrice >= 120_000;
+ },
+};
+
+export const ALL_EVENTS = Object.freeze({
+ CHRISTMAS_D_DAY,
+ WEEKDAY,
+ WEEKEND,
+ SPECIAL,
+ GIFT,
+}); | JavaScript | ๋น์ฆ๋์ค์ ๊ด๋ จํ ์ค์ํ ๋ก์ง์ด๊ธฐ ๋๋ฌธ์ ๊ทธ ์ ๋ณด๊ฐ ๋น ๋ฅด๊ฒ ๋ณด์ฌ์ผ ํ๋ฉด์ ๋ค๋ฅธ ๊ณณ์์ ์ฌ์ฌ์ฉ๋์ง ์๋ ์๋ ์์๋ก ๋นผ์ง ์์์ต๋๋ค.
๋ง์ฝ '์ฆ์ ํ ์๋ น ์ต์ ์ฃผ๋ฌธ ๊ธ์ก'์ด๋ผ๋ ์ด๋ฆ์ ์์๋ฅผ ๋ง๋ ๋ค๋ฉด ๊ทธ ์๋ฏธ๋ ๋ด๊ณ ์๊ฒ ์ง๋ง, ๊ทธ๊ฒ ์ผ๋ง์ธ์ง ์๊ธฐ ์ํด์๋ ๋ค๋ฅธ ํ์ผ์ ์ด์ด ๋ณด๊ฑฐ๋ ์์ ์ ์ด๋ํด์ผ ํ๋ ๋ถํธํจ์ด ์๊ธธ ๊ฑฐ๋ผ ์๊ฐํ์ต๋๋ค. :)
๋น์ทํ ๊ฒฐ์ ์ ๋ด๋ฆฐ ์ฌ๋ก๋ก EventPlanner์ getBadge ํจ์๊ฐ ์์ต๋๋ค.๐ |
@@ -0,0 +1,117 @@
+import { EVENT_NAMES } from '../constants/event.js';
+import { DESSERTS, MAIN_COURSES } from '../constants/menus.js';
+
+export const CHRISTMAS_D_DAY = {
+ getBenefit({ date }) {
+ const baseAmount = 1000;
+ const weightAmount = 100;
+
+ return {
+ amount: 1,
+ price: baseAmount + weightAmount * (date - 1),
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.CHRISTMAS_D_DAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isChristmasPeriod }) {
+ return isChristmasPeriod === true;
+ },
+};
+
+export const WEEKDAY = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[DESSERTS],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKDAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === false && orderCategories.includes(DESSERTS);
+ },
+};
+
+export const WEEKEND = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[MAIN_COURSES],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKEND,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === true && orderCategories.includes(MAIN_COURSES);
+ },
+};
+
+export const SPECIAL = {
+ getBenefit() {
+ const discountAmount = 1_000;
+ return {
+ amount: 1,
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.SPECIAL,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isSpecialDate }) {
+ return isSpecialDate === true;
+ },
+};
+
+export const GIFT = {
+ getBenefit() {
+ return {
+ menu: '์ดํ์ธ',
+ amount: 1,
+ price: 25_000,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.GIFT,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ totalPrice }) {
+ return totalPrice >= 120_000;
+ },
+};
+
+export const ALL_EVENTS = Object.freeze({
+ CHRISTMAS_D_DAY,
+ WEEKDAY,
+ WEEKEND,
+ SPECIAL,
+ GIFT,
+}); | JavaScript | ์ํ ๊ทธ๋ ๊ตฐ์. ๊ทธ๋ ๋ค๋ฉด ์ด๋ ๊ฒ๋ ์ด๋ ์ ๊ฐ์?
```
isEventAvailable({ totalPrice }) {
const evenAvaliableAmount = 120_000;
return totalPrice >= evenAvaliableAmount;
```
๋ถํ์ํ ํ ์ค์ด๋ผ๊ณ ์๊ฐํ์ค ์ ์์ง๋ง, ๋ง์ํ์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ฉด์๋ ์๋ฏธ๋ฅผ ๋ด์ ์ ์๋ค๊ณ ์๊ฐํ์ต๋๋ค! |
@@ -0,0 +1,36 @@
+import EventCalendar from '../../src/domain/EventCalendar';
+import { CHRISTMAS_D_DAY, GIFT, SPECIAL } from '../../src/domain/Events';
+
+import {
+ EVENT_MONTH,
+ EVENT_NAMES,
+ EVENT_YEAR,
+} from '../../src/constants/event';
+import { MAIN_COURSES } from '../../src/constants/menus';
+
+describe('EventCalendar ํ
์คํธ', () => {
+ test('getEventBenefit ๊ธฐ๋ฅ ํ
์คํธ', () => {
+ // given
+ const YEAR = EVENT_YEAR;
+ const MONTH = EVENT_MONTH;
+ const DATE = 25;
+ const TOTAL_PRICE = 130_000;
+ const ORDER_CATEGORIES = [MAIN_COURSES];
+
+ const RESULT = {
+ [EVENT_NAMES.CHRISTMAS_D_DAY]: CHRISTMAS_D_DAY.getEvent().getBenefit,
+ [EVENT_NAMES.SPECIAL]: SPECIAL.getEvent().getBenefit,
+ [EVENT_NAMES.GIFT]: GIFT.getEvent().getBenefit,
+ };
+
+ // when
+ const eventCalendar = new EventCalendar(YEAR, MONTH, DATE);
+ eventCalendar.setAvailableEvents({
+ totalPrice: TOTAL_PRICE,
+ orderCategories: ORDER_CATEGORIES,
+ });
+
+ //then
+ expect(eventCalendar.availableEvents).toEqual(RESULT);
+ });
+}); | JavaScript | ๋ค ์์กด์ฑ ๋ฌธ์ ๋ผ๊ณ ์๊ฐํฉ๋๋ค.
์ ๋ ์์ ์์ฒด๋ ํ๋์ ๋ชจ๋์ด๋ผ๊ณ ์๊ฐํ๋๋ฐ์,
ํ๋์ ๋ชจ๋์ด ์ค์ ์ฝ๋์, ํ
์คํธ ์ฝ๋ ๋ ๊ตฐ๋ฐ ๋ชจ๋ ์์กดํ๋ ๊ฒ์ ์ข์๋ณด์ด์ง ์๊ธฐ ๋๋ฌธ์ด๊ธฐ๋ ํ๊ณ ,
ํนํ๋ ํ
์คํธ๋ ๋
๋ฆฝ์ ์ด์ด์ผ ํ๊ธฐ ๋๋ฌธ์ด๊ธฐ๋ ํฉ๋๋ค! |
@@ -0,0 +1,83 @@
+import deepFreeze from '../utils/deepFreeze.js';
+import { EVENT_MONTH } from './event.js';
+
+const MESSAGES = deepFreeze({
+ // InputView
+ inputs: {
+ getVisitDate: `${EVENT_MONTH}์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n`,
+ getOrders:
+ '์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n',
+ },
+
+ // OutputView
+ outputs: {
+ sayHi: `์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น ${EVENT_MONTH}์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค.`,
+
+ eventPreview: (date) =>
+ `${EVENT_MONTH}์ ${date}์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n`,
+
+ menus: {
+ title: '<์ฃผ๋ฌธ ๋ฉ๋ด>',
+ printMsg: (menus) =>
+ menus.reduce(
+ (acc, { menu, amount }) => `${acc}${menu} ${amount}๊ฐ\n`,
+ '',
+ ),
+ },
+
+ originalPrice: {
+ title: '<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>',
+ printMsg: (price) => `${price.toLocaleString()}์\n`,
+ },
+
+ gift: {
+ title: '<์ฆ์ ๋ฉ๋ด>',
+ printMsg: (gift) => {
+ if (!gift) return '์์\n';
+
+ return `${gift.menu} ${gift.amount}๊ฐ\n`;
+ },
+ },
+
+ benefits: {
+ title: '<ํํ ๋ด์ญ>',
+ printMsg: (benefits) => {
+ if (!benefits) return '์์\n';
+
+ return Object.entries(benefits).reduce(
+ (acc, [eventName, { amount, price }]) =>
+ `${acc}${eventName}: -${(amount * price).toLocaleString()}์\n`,
+ '',
+ );
+ },
+ },
+
+ totalBenefitMoney: {
+ title: '<์ดํํ ๊ธ์ก>',
+ printMsg: (money) => {
+ if (!money) return '0์\n';
+
+ return `-${money.toLocaleString()}์\n`;
+ },
+ },
+
+ payment: {
+ title: '<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>',
+ printMsg: (money) => `${money.toLocaleString()}์\n`,
+ },
+
+ badge: {
+ title: '<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>',
+ printMsg: (badge) => `${badge}`,
+ },
+ },
+
+ // Error
+ errors: {
+ prefix: '[ERROR]',
+ invalidDate: '์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ invalidOrders: '์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ },
+});
+
+export default MESSAGES; | JavaScript | EVENT_MONTH๋ ์์๋ณด๋ค๋ 12์๋ก ๋ฐ๋ก ๋ฃ๋ ๊ฒ๋ ๊ณ ๋ คํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์์
์ด ๋ถ๋ถ์ ๋ํด์ ๋ง์ด ๊ณ ๋ฏผํ์๋๋ฐ, ํด๋น ํฌ๋ฆฌ์ค๋ง์ค ์ด๋ฒคํธ๋ ๋ด๋
์๋ ๊ฐ์ต๋ ์ ์์ผ๋๊น ๋
๋๋ ๋ฐ๋๋๋ผ๋, ํฌ๋ฆฌ์ค๋ง์ค๋๊น 12์์ ์๋ณํ๊ฒ ๋ค ์ถ์์ต๋๋ค! |
@@ -0,0 +1,83 @@
+import deepFreeze from '../utils/deepFreeze.js';
+import { EVENT_MONTH } from './event.js';
+
+const MESSAGES = deepFreeze({
+ // InputView
+ inputs: {
+ getVisitDate: `${EVENT_MONTH}์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n`,
+ getOrders:
+ '์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n',
+ },
+
+ // OutputView
+ outputs: {
+ sayHi: `์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น ${EVENT_MONTH}์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค.`,
+
+ eventPreview: (date) =>
+ `${EVENT_MONTH}์ ${date}์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n`,
+
+ menus: {
+ title: '<์ฃผ๋ฌธ ๋ฉ๋ด>',
+ printMsg: (menus) =>
+ menus.reduce(
+ (acc, { menu, amount }) => `${acc}${menu} ${amount}๊ฐ\n`,
+ '',
+ ),
+ },
+
+ originalPrice: {
+ title: '<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>',
+ printMsg: (price) => `${price.toLocaleString()}์\n`,
+ },
+
+ gift: {
+ title: '<์ฆ์ ๋ฉ๋ด>',
+ printMsg: (gift) => {
+ if (!gift) return '์์\n';
+
+ return `${gift.menu} ${gift.amount}๊ฐ\n`;
+ },
+ },
+
+ benefits: {
+ title: '<ํํ ๋ด์ญ>',
+ printMsg: (benefits) => {
+ if (!benefits) return '์์\n';
+
+ return Object.entries(benefits).reduce(
+ (acc, [eventName, { amount, price }]) =>
+ `${acc}${eventName}: -${(amount * price).toLocaleString()}์\n`,
+ '',
+ );
+ },
+ },
+
+ totalBenefitMoney: {
+ title: '<์ดํํ ๊ธ์ก>',
+ printMsg: (money) => {
+ if (!money) return '0์\n';
+
+ return `-${money.toLocaleString()}์\n`;
+ },
+ },
+
+ payment: {
+ title: '<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>',
+ printMsg: (money) => `${money.toLocaleString()}์\n`,
+ },
+
+ badge: {
+ title: '<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>',
+ printMsg: (badge) => `${badge}`,
+ },
+ },
+
+ // Error
+ errors: {
+ prefix: '[ERROR]',
+ invalidDate: '์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ invalidOrders: '์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ },
+});
+
+export default MESSAGES; | JavaScript | toLocaleString ๋ถ๋ถ์ด ๊ฐ๊ฒฉ์ ๋ฐํํด์ค๋๋ ๋ค ์ฐ์ด๋๋ฐ, ๋ฐ๋ก ๋ชจ๋ํ ์ํค๋ ๊ฒ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,83 @@
+import deepFreeze from '../utils/deepFreeze.js';
+import { EVENT_MONTH } from './event.js';
+
+const MESSAGES = deepFreeze({
+ // InputView
+ inputs: {
+ getVisitDate: `${EVENT_MONTH}์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n`,
+ getOrders:
+ '์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n',
+ },
+
+ // OutputView
+ outputs: {
+ sayHi: `์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น ${EVENT_MONTH}์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค.`,
+
+ eventPreview: (date) =>
+ `${EVENT_MONTH}์ ${date}์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n`,
+
+ menus: {
+ title: '<์ฃผ๋ฌธ ๋ฉ๋ด>',
+ printMsg: (menus) =>
+ menus.reduce(
+ (acc, { menu, amount }) => `${acc}${menu} ${amount}๊ฐ\n`,
+ '',
+ ),
+ },
+
+ originalPrice: {
+ title: '<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>',
+ printMsg: (price) => `${price.toLocaleString()}์\n`,
+ },
+
+ gift: {
+ title: '<์ฆ์ ๋ฉ๋ด>',
+ printMsg: (gift) => {
+ if (!gift) return '์์\n';
+
+ return `${gift.menu} ${gift.amount}๊ฐ\n`;
+ },
+ },
+
+ benefits: {
+ title: '<ํํ ๋ด์ญ>',
+ printMsg: (benefits) => {
+ if (!benefits) return '์์\n';
+
+ return Object.entries(benefits).reduce(
+ (acc, [eventName, { amount, price }]) =>
+ `${acc}${eventName}: -${(amount * price).toLocaleString()}์\n`,
+ '',
+ );
+ },
+ },
+
+ totalBenefitMoney: {
+ title: '<์ดํํ ๊ธ์ก>',
+ printMsg: (money) => {
+ if (!money) return '0์\n';
+
+ return `-${money.toLocaleString()}์\n`;
+ },
+ },
+
+ payment: {
+ title: '<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>',
+ printMsg: (money) => `${money.toLocaleString()}์\n`,
+ },
+
+ badge: {
+ title: '<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>',
+ printMsg: (badge) => `${badge}`,
+ },
+ },
+
+ // Error
+ errors: {
+ prefix: '[ERROR]',
+ invalidDate: '์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ invalidOrders: '์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ },
+});
+
+export default MESSAGES; | JavaScript | ๋ฐ์์๋ `totalBenefitMoney`๋ก ์์ธํ๊ฒ ๋ฉ์๋๋ช
์ ์ง์ผ์
์ ์ด๊ฒ๋ `benefits`๋ณด๋ค๋ `benefitsDetail`๋ก ์ข๋ ๊ตฌ์ฒด์ ์ธ๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,83 @@
+import deepFreeze from '../utils/deepFreeze.js';
+import { EVENT_MONTH } from './event.js';
+
+const MESSAGES = deepFreeze({
+ // InputView
+ inputs: {
+ getVisitDate: `${EVENT_MONTH}์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n`,
+ getOrders:
+ '์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n',
+ },
+
+ // OutputView
+ outputs: {
+ sayHi: `์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น ${EVENT_MONTH}์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค.`,
+
+ eventPreview: (date) =>
+ `${EVENT_MONTH}์ ${date}์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n`,
+
+ menus: {
+ title: '<์ฃผ๋ฌธ ๋ฉ๋ด>',
+ printMsg: (menus) =>
+ menus.reduce(
+ (acc, { menu, amount }) => `${acc}${menu} ${amount}๊ฐ\n`,
+ '',
+ ),
+ },
+
+ originalPrice: {
+ title: '<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>',
+ printMsg: (price) => `${price.toLocaleString()}์\n`,
+ },
+
+ gift: {
+ title: '<์ฆ์ ๋ฉ๋ด>',
+ printMsg: (gift) => {
+ if (!gift) return '์์\n';
+
+ return `${gift.menu} ${gift.amount}๊ฐ\n`;
+ },
+ },
+
+ benefits: {
+ title: '<ํํ ๋ด์ญ>',
+ printMsg: (benefits) => {
+ if (!benefits) return '์์\n';
+
+ return Object.entries(benefits).reduce(
+ (acc, [eventName, { amount, price }]) =>
+ `${acc}${eventName}: -${(amount * price).toLocaleString()}์\n`,
+ '',
+ );
+ },
+ },
+
+ totalBenefitMoney: {
+ title: '<์ดํํ ๊ธ์ก>',
+ printMsg: (money) => {
+ if (!money) return '0์\n';
+
+ return `-${money.toLocaleString()}์\n`;
+ },
+ },
+
+ payment: {
+ title: '<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>',
+ printMsg: (money) => `${money.toLocaleString()}์\n`,
+ },
+
+ badge: {
+ title: '<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>',
+ printMsg: (badge) => `${badge}`,
+ },
+ },
+
+ // Error
+ errors: {
+ prefix: '[ERROR]',
+ invalidDate: '์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ invalidOrders: '์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ },
+});
+
+export default MESSAGES; | JavaScript | ์ ์์์๋ ์์๊ฐ์ ๊ฐ์ ธ์์๋๋ฐ, ์ด๊ฑด ํ๋์ฝ๋ฉ๋์ด์์ต๋๋ค! ํ๋๋ก ํต์ผํ๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,63 @@
+import { FRIDAY, SATURDAY, SPECIAL_DATES } from '../constants/calendar.js';
+import { EVENT_PERIOD } from '../constants/event.js';
+import { ALL_EVENTS } from './Events.js';
+
+class EventCalendar {
+ #year;
+
+ #month;
+
+ #date;
+
+ #availableEvents;
+
+ constructor(year, month, date) {
+ this.#year = year;
+ this.#month = month;
+ this.#date = date;
+ this.#availableEvents = {};
+ }
+
+ async setAvailableEvents({ totalPrice, orderCategories }) {
+ const state = this.#getState(totalPrice, orderCategories);
+
+ await Object.values(ALL_EVENTS).forEach((event) => {
+ const { name, getBenefit } = event.getEvent();
+
+ if (event.isEventAvailable(state))
+ this.#availableEvents[name] = getBenefit;
+ });
+ }
+
+ #getState(totalPrice, orderCategories) {
+ return {
+ isWeekend: this.#isWeekend(),
+ isSpecialDate: this.#isSpecialDate(),
+ isChristmasPeriod: this.#isChristmasPeriod(),
+ totalPrice,
+ orderCategories,
+ };
+ }
+
+ #isWeekend() {
+ const dayOfWeek = new Date(
+ `${this.#year}-${this.#month}-${this.#date}`,
+ ).getDay();
+ return dayOfWeek === FRIDAY || dayOfWeek === SATURDAY;
+ }
+
+ #isSpecialDate() {
+ return SPECIAL_DATES.includes(this.#date);
+ }
+
+ #isChristmasPeriod() {
+ const { startDate, endDate } = EVENT_PERIOD;
+ return this.#date >= startDate && this.#date <= endDate;
+ }
+
+ get availableEvents() {
+ return this.#availableEvents;
+ }
+}
+
+export default EventCalendar; | JavaScript | `Period` ๊ผผ๊ผผํ์ญ๋๋ค! ๐๐๐ |
@@ -0,0 +1,63 @@
+import { FRIDAY, SATURDAY, SPECIAL_DATES } from '../constants/calendar.js';
+import { EVENT_PERIOD } from '../constants/event.js';
+import { ALL_EVENTS } from './Events.js';
+
+class EventCalendar {
+ #year;
+
+ #month;
+
+ #date;
+
+ #availableEvents;
+
+ constructor(year, month, date) {
+ this.#year = year;
+ this.#month = month;
+ this.#date = date;
+ this.#availableEvents = {};
+ }
+
+ async setAvailableEvents({ totalPrice, orderCategories }) {
+ const state = this.#getState(totalPrice, orderCategories);
+
+ await Object.values(ALL_EVENTS).forEach((event) => {
+ const { name, getBenefit } = event.getEvent();
+
+ if (event.isEventAvailable(state))
+ this.#availableEvents[name] = getBenefit;
+ });
+ }
+
+ #getState(totalPrice, orderCategories) {
+ return {
+ isWeekend: this.#isWeekend(),
+ isSpecialDate: this.#isSpecialDate(),
+ isChristmasPeriod: this.#isChristmasPeriod(),
+ totalPrice,
+ orderCategories,
+ };
+ }
+
+ #isWeekend() {
+ const dayOfWeek = new Date(
+ `${this.#year}-${this.#month}-${this.#date}`,
+ ).getDay();
+ return dayOfWeek === FRIDAY || dayOfWeek === SATURDAY;
+ }
+
+ #isSpecialDate() {
+ return SPECIAL_DATES.includes(this.#date);
+ }
+
+ #isChristmasPeriod() {
+ const { startDate, endDate } = EVENT_PERIOD;
+ return this.#date >= startDate && this.#date <= endDate;
+ }
+
+ get availableEvents() {
+ return this.#availableEvents;
+ }
+}
+
+export default EventCalendar; | JavaScript | new Date(2023,11,25)๋ฑ์ผ๋ก๋ ๋ฃ์ ์ ์์ต๋๋ค! ๊ทธ๋ฐ๋ฐ month์ ๊ฒฝ์ฐ 0์ผ๋ก ์์ํด์ 1๋นผ์ค์ผ ํ๋๋ผ๊ตฌ์... |
@@ -0,0 +1,63 @@
+import { FRIDAY, SATURDAY, SPECIAL_DATES } from '../constants/calendar.js';
+import { EVENT_PERIOD } from '../constants/event.js';
+import { ALL_EVENTS } from './Events.js';
+
+class EventCalendar {
+ #year;
+
+ #month;
+
+ #date;
+
+ #availableEvents;
+
+ constructor(year, month, date) {
+ this.#year = year;
+ this.#month = month;
+ this.#date = date;
+ this.#availableEvents = {};
+ }
+
+ async setAvailableEvents({ totalPrice, orderCategories }) {
+ const state = this.#getState(totalPrice, orderCategories);
+
+ await Object.values(ALL_EVENTS).forEach((event) => {
+ const { name, getBenefit } = event.getEvent();
+
+ if (event.isEventAvailable(state))
+ this.#availableEvents[name] = getBenefit;
+ });
+ }
+
+ #getState(totalPrice, orderCategories) {
+ return {
+ isWeekend: this.#isWeekend(),
+ isSpecialDate: this.#isSpecialDate(),
+ isChristmasPeriod: this.#isChristmasPeriod(),
+ totalPrice,
+ orderCategories,
+ };
+ }
+
+ #isWeekend() {
+ const dayOfWeek = new Date(
+ `${this.#year}-${this.#month}-${this.#date}`,
+ ).getDay();
+ return dayOfWeek === FRIDAY || dayOfWeek === SATURDAY;
+ }
+
+ #isSpecialDate() {
+ return SPECIAL_DATES.includes(this.#date);
+ }
+
+ #isChristmasPeriod() {
+ const { startDate, endDate } = EVENT_PERIOD;
+ return this.#date >= startDate && this.#date <= endDate;
+ }
+
+ get availableEvents() {
+ return this.#availableEvents;
+ }
+}
+
+export default EventCalendar; | JavaScript | ์๋ ๋ฐํ๋๋ 0,1,2,~ ๋์ FRIDAY๋ก ๋ฐ๊พธ๋๊น ํจ์ฌ ๊ฐ๋
์ฑ ์ข์์ง๋ค์! ๋ฐฐ์๊ฐ๋๋ค!๐๐๐ |
@@ -0,0 +1,117 @@
+import { EVENT_NAMES } from '../constants/event.js';
+import { DESSERTS, MAIN_COURSES } from '../constants/menus.js';
+
+export const CHRISTMAS_D_DAY = {
+ getBenefit({ date }) {
+ const baseAmount = 1000;
+ const weightAmount = 100;
+
+ return {
+ amount: 1,
+ price: baseAmount + weightAmount * (date - 1),
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.CHRISTMAS_D_DAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isChristmasPeriod }) {
+ return isChristmasPeriod === true;
+ },
+};
+
+export const WEEKDAY = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[DESSERTS],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKDAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === false && orderCategories.includes(DESSERTS);
+ },
+};
+
+export const WEEKEND = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[MAIN_COURSES],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKEND,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === true && orderCategories.includes(MAIN_COURSES);
+ },
+};
+
+export const SPECIAL = {
+ getBenefit() {
+ const discountAmount = 1_000;
+ return {
+ amount: 1,
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.SPECIAL,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isSpecialDate }) {
+ return isSpecialDate === true;
+ },
+};
+
+export const GIFT = {
+ getBenefit() {
+ return {
+ menu: '์ดํ์ธ',
+ amount: 1,
+ price: 25_000,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.GIFT,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ totalPrice }) {
+ return totalPrice >= 120_000;
+ },
+};
+
+export const ALL_EVENTS = Object.freeze({
+ CHRISTMAS_D_DAY,
+ WEEKDAY,
+ WEEKEND,
+ SPECIAL,
+ GIFT,
+}); | JavaScript | 1_000, 1000 ์ค์ ํ๋๋ก ํต์ผํ๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,94 @@
+import OrderItem from './OrderItem.js';
+
+import MESSAGES from '../constants/messages.js';
+
+import throwError from '../utils/throwError.js';
+import {
+ hasDuplicatedMenu,
+ hasOnlyBeverages,
+ isTotalAmountOfMenusValid,
+} from '../utils/validators.js';
+
+class Receipt {
+ #orders;
+
+ #orderItems;
+
+ constructor(orders) {
+ this.#validate(orders);
+ this.#orders = orders;
+ this.#orderItems = [];
+ }
+
+ #validate(orders) {
+ if (!isTotalAmountOfMenusValid(orders))
+ throwError(MESSAGES.errors.invalidOrders);
+
+ if (hasDuplicatedMenu(orders)) throwError(MESSAGES.errors.invalidOrders);
+
+ if (hasOnlyBeverages(orders)) throwError(MESSAGES.errors.invalidOrders);
+ }
+
+ generateOrders() {
+ this.#orderItems = this.#orders.map(
+ ({ menu, amount }) => new OrderItem(menu, amount),
+ );
+ }
+
+ get totalPrice() {
+ return this.#orderItems.reduce(
+ (totalPrice, orderItem) => totalPrice + orderItem.totalPrice,
+ 0,
+ );
+ }
+
+ get receipt() {
+ const receipt = {};
+
+ this.#orderItems.forEach((orderItem) => {
+ const { category, menu, amount } = orderItem.item;
+
+ if (!Array.isArray(receipt[category])) {
+ receipt[category] = [];
+ }
+
+ receipt[category].push({ menu, amount });
+ });
+
+ return receipt;
+ }
+
+ get orderCategories() {
+ const categories = new Set();
+
+ this.#orderItems.forEach((orderItem) => {
+ const { category } = orderItem.item;
+ categories.add(category);
+ });
+
+ return [...categories];
+ }
+
+ get orderCntByCategory() {
+ const orderCnt = {};
+
+ this.#orderItems.forEach((orderItem) => {
+ const { category, amount } = orderItem.item;
+
+ if (!orderCnt[category]) orderCnt[category] = 0;
+
+ orderCnt[category] += amount;
+ });
+
+ return orderCnt;
+ }
+
+ get menus() {
+ return this.#orderItems.map((orderItem) => {
+ const { menu, amount } = orderItem.item;
+ return { menu, amount };
+ });
+ }
+}
+
+export default Receipt; | JavaScript | ํน์ ์ฌ๊ธฐ ๊ณต๋ฐฑ์ด ์๋ ์ด์ ๊ฐ ์์๊น์?? |
@@ -0,0 +1,94 @@
+import OrderItem from './OrderItem.js';
+
+import MESSAGES from '../constants/messages.js';
+
+import throwError from '../utils/throwError.js';
+import {
+ hasDuplicatedMenu,
+ hasOnlyBeverages,
+ isTotalAmountOfMenusValid,
+} from '../utils/validators.js';
+
+class Receipt {
+ #orders;
+
+ #orderItems;
+
+ constructor(orders) {
+ this.#validate(orders);
+ this.#orders = orders;
+ this.#orderItems = [];
+ }
+
+ #validate(orders) {
+ if (!isTotalAmountOfMenusValid(orders))
+ throwError(MESSAGES.errors.invalidOrders);
+
+ if (hasDuplicatedMenu(orders)) throwError(MESSAGES.errors.invalidOrders);
+
+ if (hasOnlyBeverages(orders)) throwError(MESSAGES.errors.invalidOrders);
+ }
+
+ generateOrders() {
+ this.#orderItems = this.#orders.map(
+ ({ menu, amount }) => new OrderItem(menu, amount),
+ );
+ }
+
+ get totalPrice() {
+ return this.#orderItems.reduce(
+ (totalPrice, orderItem) => totalPrice + orderItem.totalPrice,
+ 0,
+ );
+ }
+
+ get receipt() {
+ const receipt = {};
+
+ this.#orderItems.forEach((orderItem) => {
+ const { category, menu, amount } = orderItem.item;
+
+ if (!Array.isArray(receipt[category])) {
+ receipt[category] = [];
+ }
+
+ receipt[category].push({ menu, amount });
+ });
+
+ return receipt;
+ }
+
+ get orderCategories() {
+ const categories = new Set();
+
+ this.#orderItems.forEach((orderItem) => {
+ const { category } = orderItem.item;
+ categories.add(category);
+ });
+
+ return [...categories];
+ }
+
+ get orderCntByCategory() {
+ const orderCnt = {};
+
+ this.#orderItems.forEach((orderItem) => {
+ const { category, amount } = orderItem.item;
+
+ if (!orderCnt[category]) orderCnt[category] = 0;
+
+ orderCnt[category] += amount;
+ });
+
+ return orderCnt;
+ }
+
+ get menus() {
+ return this.#orderItems.map((orderItem) => {
+ const { menu, amount } = orderItem.item;
+ return { menu, amount };
+ });
+ }
+}
+
+export default Receipt; | JavaScript | ๊ตฌ์กฐ๋ถํด ํ ๋น ์ ์ฐ์ญ๋๋น! ๐ |
@@ -0,0 +1,144 @@
+import EventCalendar from '../domain/EventCalendar.js';
+import Receipt from '../domain/Receipt.js';
+
+import {
+ EVENT_MONTH,
+ EVENT_NAMES,
+ EVENT_PERIOD,
+ EVENT_YEAR,
+} from '../constants/event.js';
+import MESSAGES from '../constants/messages.js';
+
+import { isInteger, isNumberInRange } from '../utils/validators.js';
+import throwError from '../utils/throwError.js';
+import { isEmptyObject } from '../utils/object.js';
+
+class EventPlanner {
+ #visitDate;
+
+ #eventCalendar;
+
+ #receipt;
+
+ #benefits;
+
+ constructor(date) {
+ this.#validate(date);
+ this.#visitDate = date;
+ this.#eventCalendar = new EventCalendar(EVENT_YEAR, EVENT_MONTH, date);
+ this.#benefits = {};
+ }
+
+ #validate(date) {
+ const { startDate, endDate } = EVENT_PERIOD;
+
+ if (!isNumberInRange(startDate, endDate, date))
+ throwError(MESSAGES.errors.invalidDate);
+
+ if (!isInteger(date)) throwError(MESSAGES.errors.invalidDate);
+ }
+
+ generateReceipt(orders) {
+ this.#receipt = new Receipt(orders);
+ this.#receipt.generateOrders(orders);
+ }
+
+ generateBenefits() {
+ const { totalPrice, orderCategories } = this.#receipt;
+
+ this.#eventCalendar.setAvailableEvents({
+ totalPrice,
+ orderCategories,
+ });
+
+ this.#setBenefits();
+ }
+
+ #setBenefits() {
+ if (this.originalPrice < 10_000) return;
+
+ const conditions = {
+ orderCntByCategory: this.#receipt.orderCntByCategory,
+ date: this.#visitDate,
+ };
+
+ Object.entries(this.#eventCalendar.availableEvents).forEach(
+ ([eventName, getBenefit]) => {
+ this.#benefits[eventName] = getBenefit(conditions);
+ },
+ );
+ }
+
+ #getBadge() {
+ const money = this.totalBenefitMoney;
+
+ if (money >= 20_000) return '์ฐํ';
+
+ if (money >= 10_000) return 'ํธ๋ฆฌ';
+
+ if (money >= 5000) return '๋ณ';
+
+ return '์์';
+ }
+
+ get visitDate() {
+ return this.#visitDate;
+ }
+
+ // ์ฃผ๋ฌธ ๋ฉ๋ด
+ get menus() {
+ return this.#receipt.menus;
+ }
+
+ // ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก
+ get originalPrice() {
+ return this.#receipt.totalPrice;
+ }
+
+ // ์ฆ์ ๋ฉ๋ด
+ get gift() {
+ if (isEmptyObject(this.#benefits)) return null;
+
+ return this.#benefits[EVENT_NAMES.GIFT];
+ }
+
+ // ํํ ๋ด์ญ
+ get benefits() {
+ if (isEmptyObject(this.#benefits)) return null;
+
+ return this.#benefits;
+ }
+
+ // ์ด ํํ ๊ธ์ก
+ get totalBenefitMoney() {
+ if (isEmptyObject(this.#benefits)) return 0;
+
+ return Object.values(this.#benefits).reduce(
+ (totalBenefitMoney, { amount, price }) =>
+ totalBenefitMoney + amount * price,
+ 0,
+ );
+ }
+
+ // ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก
+ get payment() {
+ if (isEmptyObject(this.#benefits)) return this.originalPrice;
+
+ const discountAmount = Object.entries(this.#benefits).reduce(
+ (payment, [eventName, { amount, price }]) => {
+ if (eventName !== EVENT_NAMES.GIFT) return payment + amount * price;
+ return payment;
+ },
+ 0,
+ );
+
+ return this.originalPrice - discountAmount;
+ }
+
+ // 12์ ์ด๋ฒคํธ ๋ฐฐ์ง
+ get badge() {
+ return this.#getBadge();
+ }
+}
+
+export default EventPlanner; | JavaScript | ํด๋น ๋ถ๋ถ์ ๋ฐ๋ก ๊ฐ์ฒด๋ก ๊ด๋ฆฌํด๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,11 @@
+const deepFreeze = (object) => {
+ Object.keys(object).forEach((prop) => {
+ if (typeof object[prop] === 'object' && !Object.isFrozen(object[prop])) {
+ deepFreeze(object[prop]);
+ }
+ });
+
+ return Object.freeze(object);
+};
+
+export default deepFreeze; | JavaScript | deepFreeze ๊ผผ๊ผผํ์ญ๋๋ค~ ๐๐๐ |
@@ -0,0 +1,36 @@
+import EventCalendar from '../../src/domain/EventCalendar';
+import { CHRISTMAS_D_DAY, GIFT, SPECIAL } from '../../src/domain/Events';
+
+import {
+ EVENT_MONTH,
+ EVENT_NAMES,
+ EVENT_YEAR,
+} from '../../src/constants/event';
+import { MAIN_COURSES } from '../../src/constants/menus';
+
+describe('EventCalendar ํ
์คํธ', () => {
+ test('getEventBenefit ๊ธฐ๋ฅ ํ
์คํธ', () => {
+ // given
+ const YEAR = EVENT_YEAR;
+ const MONTH = EVENT_MONTH;
+ const DATE = 25;
+ const TOTAL_PRICE = 130_000;
+ const ORDER_CATEGORIES = [MAIN_COURSES];
+
+ const RESULT = {
+ [EVENT_NAMES.CHRISTMAS_D_DAY]: CHRISTMAS_D_DAY.getEvent().getBenefit,
+ [EVENT_NAMES.SPECIAL]: SPECIAL.getEvent().getBenefit,
+ [EVENT_NAMES.GIFT]: GIFT.getEvent().getBenefit,
+ };
+
+ // when
+ const eventCalendar = new EventCalendar(YEAR, MONTH, DATE);
+ eventCalendar.setAvailableEvents({
+ totalPrice: TOTAL_PRICE,
+ orderCategories: ORDER_CATEGORIES,
+ });
+
+ //then
+ expect(eventCalendar.availableEvents).toEqual(RESULT);
+ });
+}); | JavaScript | ์ค์ ๋ก ๋ชจ๋ํ๋ ์์์ ๊ตฌ์กฐ๋ ๊ฐ์ด ๋ฐ๋๋ฉด ํ
์คํธ์ ๊ฒฐ๊ณผ๊ฐ ๋ฐ๋๋ ๊ฑธ ๊ฒฝํํ๊ณ ์๋ฌธ์ ํ๊ธด ํ๋๋ฐ, ๋
๋ฆฝ์ฑ์ ์งํค์ง ์์ ํ๋์ด์๊ตฐ์! ๋๋ถ์ ๊นจ๋ฌ์์ต๋๋ค. ์ข์ ํผ๋๋ฐฑ ๊ฐ์ฌํด์.๐ |
@@ -0,0 +1,117 @@
+import { EVENT_NAMES } from '../constants/event.js';
+import { DESSERTS, MAIN_COURSES } from '../constants/menus.js';
+
+export const CHRISTMAS_D_DAY = {
+ getBenefit({ date }) {
+ const baseAmount = 1000;
+ const weightAmount = 100;
+
+ return {
+ amount: 1,
+ price: baseAmount + weightAmount * (date - 1),
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.CHRISTMAS_D_DAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isChristmasPeriod }) {
+ return isChristmasPeriod === true;
+ },
+};
+
+export const WEEKDAY = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[DESSERTS],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKDAY,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === false && orderCategories.includes(DESSERTS);
+ },
+};
+
+export const WEEKEND = {
+ getBenefit({ orderCntByCategory }) {
+ const discountAmount = 2_023;
+ return {
+ amount: orderCntByCategory[MAIN_COURSES],
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.WEEKEND,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isWeekend, orderCategories }) {
+ return isWeekend === true && orderCategories.includes(MAIN_COURSES);
+ },
+};
+
+export const SPECIAL = {
+ getBenefit() {
+ const discountAmount = 1_000;
+ return {
+ amount: 1,
+ price: discountAmount,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.SPECIAL,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ isSpecialDate }) {
+ return isSpecialDate === true;
+ },
+};
+
+export const GIFT = {
+ getBenefit() {
+ return {
+ menu: '์ดํ์ธ',
+ amount: 1,
+ price: 25_000,
+ };
+ },
+
+ getEvent() {
+ return {
+ name: EVENT_NAMES.GIFT,
+ getBenefit: this.getBenefit,
+ };
+ },
+
+ isEventAvailable({ totalPrice }) {
+ return totalPrice >= 120_000;
+ },
+};
+
+export const ALL_EVENTS = Object.freeze({
+ CHRISTMAS_D_DAY,
+ WEEKDAY,
+ WEEKEND,
+ SPECIAL,
+ GIFT,
+}); | JavaScript | ์ข์ ๋ฐฉ๋ฒ์ด๋ค์! ๊ฐ์ฌํฉ๋๋ค. :) |
@@ -0,0 +1,83 @@
+import deepFreeze from '../utils/deepFreeze.js';
+import { EVENT_MONTH } from './event.js';
+
+const MESSAGES = deepFreeze({
+ // InputView
+ inputs: {
+ getVisitDate: `${EVENT_MONTH}์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n`,
+ getOrders:
+ '์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n',
+ },
+
+ // OutputView
+ outputs: {
+ sayHi: `์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น ${EVENT_MONTH}์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค.`,
+
+ eventPreview: (date) =>
+ `${EVENT_MONTH}์ ${date}์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n`,
+
+ menus: {
+ title: '<์ฃผ๋ฌธ ๋ฉ๋ด>',
+ printMsg: (menus) =>
+ menus.reduce(
+ (acc, { menu, amount }) => `${acc}${menu} ${amount}๊ฐ\n`,
+ '',
+ ),
+ },
+
+ originalPrice: {
+ title: '<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>',
+ printMsg: (price) => `${price.toLocaleString()}์\n`,
+ },
+
+ gift: {
+ title: '<์ฆ์ ๋ฉ๋ด>',
+ printMsg: (gift) => {
+ if (!gift) return '์์\n';
+
+ return `${gift.menu} ${gift.amount}๊ฐ\n`;
+ },
+ },
+
+ benefits: {
+ title: '<ํํ ๋ด์ญ>',
+ printMsg: (benefits) => {
+ if (!benefits) return '์์\n';
+
+ return Object.entries(benefits).reduce(
+ (acc, [eventName, { amount, price }]) =>
+ `${acc}${eventName}: -${(amount * price).toLocaleString()}์\n`,
+ '',
+ );
+ },
+ },
+
+ totalBenefitMoney: {
+ title: '<์ดํํ ๊ธ์ก>',
+ printMsg: (money) => {
+ if (!money) return '0์\n';
+
+ return `-${money.toLocaleString()}์\n`;
+ },
+ },
+
+ payment: {
+ title: '<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>',
+ printMsg: (money) => `${money.toLocaleString()}์\n`,
+ },
+
+ badge: {
+ title: '<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>',
+ printMsg: (badge) => `${badge}`,
+ },
+ },
+
+ // Error
+ errors: {
+ prefix: '[ERROR]',
+ invalidDate: '์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ invalidOrders: '์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ },
+});
+
+export default MESSAGES; | JavaScript | ์ ๋ ๊ณ ๋ฏผํ๋ ๋ถ๋ถ์ธ๋ฐ, ๋ค๋ฅธ ๋ฌ์ ์ด๋ฒคํธ๋ฅผ ๊ฐ์ตํ๋ค๋ฉด ์์๋ก ๋นผ๋ ๊ฒ ์ข์ ๊ฒ ๊ฐ์ ์ด๋ ๊ฒ ํ์ต๋๋ค! |
@@ -0,0 +1,83 @@
+import deepFreeze from '../utils/deepFreeze.js';
+import { EVENT_MONTH } from './event.js';
+
+const MESSAGES = deepFreeze({
+ // InputView
+ inputs: {
+ getVisitDate: `${EVENT_MONTH}์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n`,
+ getOrders:
+ '์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n',
+ },
+
+ // OutputView
+ outputs: {
+ sayHi: `์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น ${EVENT_MONTH}์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค.`,
+
+ eventPreview: (date) =>
+ `${EVENT_MONTH}์ ${date}์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n`,
+
+ menus: {
+ title: '<์ฃผ๋ฌธ ๋ฉ๋ด>',
+ printMsg: (menus) =>
+ menus.reduce(
+ (acc, { menu, amount }) => `${acc}${menu} ${amount}๊ฐ\n`,
+ '',
+ ),
+ },
+
+ originalPrice: {
+ title: '<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>',
+ printMsg: (price) => `${price.toLocaleString()}์\n`,
+ },
+
+ gift: {
+ title: '<์ฆ์ ๋ฉ๋ด>',
+ printMsg: (gift) => {
+ if (!gift) return '์์\n';
+
+ return `${gift.menu} ${gift.amount}๊ฐ\n`;
+ },
+ },
+
+ benefits: {
+ title: '<ํํ ๋ด์ญ>',
+ printMsg: (benefits) => {
+ if (!benefits) return '์์\n';
+
+ return Object.entries(benefits).reduce(
+ (acc, [eventName, { amount, price }]) =>
+ `${acc}${eventName}: -${(amount * price).toLocaleString()}์\n`,
+ '',
+ );
+ },
+ },
+
+ totalBenefitMoney: {
+ title: '<์ดํํ ๊ธ์ก>',
+ printMsg: (money) => {
+ if (!money) return '0์\n';
+
+ return `-${money.toLocaleString()}์\n`;
+ },
+ },
+
+ payment: {
+ title: '<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>',
+ printMsg: (money) => `${money.toLocaleString()}์\n`,
+ },
+
+ badge: {
+ title: '<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>',
+ printMsg: (badge) => `${badge}`,
+ },
+ },
+
+ // Error
+ errors: {
+ prefix: '[ERROR]',
+ invalidDate: '์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ invalidOrders: '์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ },
+});
+
+export default MESSAGES; | JavaScript | ์, ๋์น ๋ถ๋ถ์ด๋ค์! ์ง์ด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค.๐ |
@@ -0,0 +1,63 @@
+import { FRIDAY, SATURDAY, SPECIAL_DATES } from '../constants/calendar.js';
+import { EVENT_PERIOD } from '../constants/event.js';
+import { ALL_EVENTS } from './Events.js';
+
+class EventCalendar {
+ #year;
+
+ #month;
+
+ #date;
+
+ #availableEvents;
+
+ constructor(year, month, date) {
+ this.#year = year;
+ this.#month = month;
+ this.#date = date;
+ this.#availableEvents = {};
+ }
+
+ async setAvailableEvents({ totalPrice, orderCategories }) {
+ const state = this.#getState(totalPrice, orderCategories);
+
+ await Object.values(ALL_EVENTS).forEach((event) => {
+ const { name, getBenefit } = event.getEvent();
+
+ if (event.isEventAvailable(state))
+ this.#availableEvents[name] = getBenefit;
+ });
+ }
+
+ #getState(totalPrice, orderCategories) {
+ return {
+ isWeekend: this.#isWeekend(),
+ isSpecialDate: this.#isSpecialDate(),
+ isChristmasPeriod: this.#isChristmasPeriod(),
+ totalPrice,
+ orderCategories,
+ };
+ }
+
+ #isWeekend() {
+ const dayOfWeek = new Date(
+ `${this.#year}-${this.#month}-${this.#date}`,
+ ).getDay();
+ return dayOfWeek === FRIDAY || dayOfWeek === SATURDAY;
+ }
+
+ #isSpecialDate() {
+ return SPECIAL_DATES.includes(this.#date);
+ }
+
+ #isChristmasPeriod() {
+ const { startDate, endDate } = EVENT_PERIOD;
+ return this.#date >= startDate && this.#date <= endDate;
+ }
+
+ get availableEvents() {
+ return this.#availableEvents;
+ }
+}
+
+export default EventCalendar; | JavaScript | ๋ง์์! ์ค์๋ฅผ ์ค์ด๊ธฐ ์ํด์ ์์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ํ์ต๋๋ค. :) |
@@ -0,0 +1,56 @@
+package christmas.domain;
+
+
+import static christmas.domain.Badge.NONE;
+import static christmas.domain.Badge.SANTA;
+import static christmas.domain.Badge.STAR;
+import static christmas.domain.Badge.TREE;
+
+import java.util.Map;
+
+public class Benefits {
+ private final Map<Event, Integer> benefits;
+
+ public Benefits(Map<Event, Integer> benefits) {
+ this.benefits = benefits;
+ }
+
+ public int calculateTotalBenefitAmount() {
+ return benefits.values()
+ .stream()
+ .mapToInt(amount -> amount)
+ .sum();
+ }
+
+ public Badge toEventBadge(int totalBenefit) {
+ if (isStarBadge(totalBenefit)) {
+ return STAR;
+ }
+ if (isTreeBadge(totalBenefit)) {
+ return TREE;
+ }
+ if (isSantaBadge(totalBenefit)) {
+ return SANTA;
+ }
+ return NONE;
+ }
+
+ private boolean isStarBadge(int totalBenefit) {
+ return totalBenefit >= STAR.getAmount() && totalBenefit < TREE.getAmount();
+ }
+
+ private boolean isTreeBadge(int totalBenefit) {
+ return totalBenefit >= TREE.getAmount() && totalBenefit < SANTA.getAmount();
+ }
+
+ private boolean isSantaBadge(int totalBenefit) {
+ return totalBenefit >= SANTA.getAmount();
+ }
+
+ public Map<Event, Integer> getBenefits() {
+ return benefits;
+ }
+}
+
+
+ | Java | is{BadgeType}Badge ํจ์๋ฅผ ์ฌ์ฉํด์ ํ๋์ฉ ๋น๊ตํ๋ ํจ์๋ฅผ ๋ง๋ค๊ฒ ๋๋ฉด ๋ฐฐ์ง๊ฐ ์๊ธฐ๊ฑฐ๋ ์ญ์ ํ ๋๋ง๋ค ๋ฉ์๋ ๊ด๋ฆฌ๋ฅผ ๊ณ์ ํด์ค์ผํ๋ค๋ ๋ฒ๊ฑฐ๋ก์์ด ์๊ธธ ์ ์์ ๊ฑฐ ๊ฐ์์!! |
@@ -0,0 +1,48 @@
+package christmas.domain;
+
+public enum Menu {
+ MUSHROOM_SOUP("์์ก์ด์ํ", 6_000, "์ ํผํ์ด์ "),
+ TAPAS("ํํ์ค", 5_500, "์ ํผํ์ด์ "),
+ CAESAR_SALAD("์์ ์๋ฌ๋", 8_000, "์ ํผํ์ด์ "),
+ T_BONE_STEAK("ํฐ๋ณธ์คํ
์ดํฌ", 55_000, "๋ฉ์ธ"),
+ BBQ_RIBS("๋ฐ๋นํ๋ฆฝ", 54_000, "๋ฉ์ธ"),
+ SEAFOOD_PASTA("ํด์ฐ๋ฌผํ์คํ", 35_000, "๋ฉ์ธ"),
+ CHRISTMAS_PASTA("ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25_000, "๋ฉ์ธ"),
+ CHOCOLATE_CAKE("์ด์ฝ์ผ์ดํฌ", 15_000, "๋์ ํธ"),
+ ICE_CREAM("์์ด์คํฌ๋ฆผ", 5_000, "๋์ ํธ"),
+ ZERO_COKE("์ ๋ก์ฝ๋ผ", 3_000, "์๋ฃ"),
+ RED_WINE("๋ ๋์์ธ", 60_000, "์๋ฃ"),
+ CHAMPAGNE("์ดํ์ธ", 25_000, "์๋ฃ"),
+ NONE("์์", 0, "์์");
+
+ private final String menuName;
+ private final int price;
+ private final String type;
+
+ Menu(String menuName, int price, String type) {
+ this.menuName = menuName;
+ this.price = price;
+ this.type = type;
+ }
+
+ public static Menu from(String menuName) {
+ for (Menu menu : values()) {
+ if (menu.getMenuName().equals(menuName)) {
+ return menu;
+ }
+ }
+ return NONE;
+ }
+
+ public String getMenuName() {
+ return menuName;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public String getType() {
+ return type;
+ }
+} | Java | ์์ Type์ ๊ฒฝ์ฐ ๊ณตํต๋๋ ํญ๋ชฉ (์ํผํ์ด์ /๋ฉ์ธ/๋์ ํธ/์๋ฃ)์ ๋ํด์ ๋ณ๋๋ก ๊ด๋ฆฌํด๋ ์ข์๊ฑฐ ๊ฐ์์ |
@@ -0,0 +1,76 @@
+package christmas.controller;
+
+import christmas.domain.Badge;
+import christmas.domain.Benefits;
+import christmas.domain.Convertor;
+import christmas.domain.Date;
+import christmas.domain.EventManager;
+import christmas.domain.Orders;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+public class EventController {
+ public void start() {
+ OutputView.printWelcome();
+ Date date = createDate();
+ Orders orders = createOrders();
+ int totalOrderAmount = createTotalOrderAmount(orders);
+ OutputView.printGift(orders.hasGift());
+ Benefits benefits = createBenefits(orders, date);
+ int totalBenefitAmount = createTotalBenefitAmount(benefits);
+ createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount);
+ createBadge(benefits, totalBenefitAmount);
+ }
+
+ private Date createDate() {
+ while (true) {
+ try {
+ Date date = new Date(InputView.readDate());
+ OutputView.printPreview(date);
+ return date;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private Orders createOrders() {
+ while (true) {
+ try {
+ Orders orders = Convertor.toOrders(InputView.readOrder());
+ OutputView.printOrders(orders);
+ return orders;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private int createTotalOrderAmount(Orders orders) {
+ int totalOrderAmount = orders.calculateTotalAmount();
+ OutputView.printTotalOrderAmount(totalOrderAmount);
+ return totalOrderAmount;
+ }
+
+ private Benefits createBenefits(Orders orders, Date date) {
+ Benefits benefits = EventManager.toBenefits(orders, date);
+ OutputView.printBenefitDetail(benefits);
+ return benefits;
+ }
+
+ private int createTotalBenefitAmount(Benefits benefits) {
+ int totalBenefitAmount = benefits.calculateTotalBenefitAmount();
+ OutputView.printTotalBenefitAmount(totalBenefitAmount);
+ return totalBenefitAmount;
+ }
+
+ private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) {
+ int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount);
+ OutputView.printPaymentAmount(paymentAmount);
+ }
+
+ private void createBadge(Benefits benefits, int totalBenefitAmount) {
+ Badge badge = benefits.toEventBadge(totalBenefitAmount);
+ OutputView.printEventBadge(badge);
+ }
+} | Java | ์
๋ ฅ ๊ฒ์ฆ์ ํ ์ฌ์
๋ ฅ ๋ฐ๋๊ฒ์ inputview์์ ์งํํ๋๊ฒ์ ๋ํด ์ด๋ป๊ฒ ์๊ฐํ์๋์???
๊ตฌํ ํ๋ฉด์ ๊ณ ๋ฏผํ์๋๋ฐ ์ด๋ ๋ฐฉํฅ์ผ๋ก ๊ตฌํํด์ผ ๋ ๋์์ง ์ ๋ชจ๋ฅด๊ฒ ๋๋ผ๊ตฌ์ |
@@ -0,0 +1,76 @@
+package christmas.controller;
+
+import christmas.domain.Badge;
+import christmas.domain.Benefits;
+import christmas.domain.Convertor;
+import christmas.domain.Date;
+import christmas.domain.EventManager;
+import christmas.domain.Orders;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+public class EventController {
+ public void start() {
+ OutputView.printWelcome();
+ Date date = createDate();
+ Orders orders = createOrders();
+ int totalOrderAmount = createTotalOrderAmount(orders);
+ OutputView.printGift(orders.hasGift());
+ Benefits benefits = createBenefits(orders, date);
+ int totalBenefitAmount = createTotalBenefitAmount(benefits);
+ createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount);
+ createBadge(benefits, totalBenefitAmount);
+ }
+
+ private Date createDate() {
+ while (true) {
+ try {
+ Date date = new Date(InputView.readDate());
+ OutputView.printPreview(date);
+ return date;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private Orders createOrders() {
+ while (true) {
+ try {
+ Orders orders = Convertor.toOrders(InputView.readOrder());
+ OutputView.printOrders(orders);
+ return orders;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private int createTotalOrderAmount(Orders orders) {
+ int totalOrderAmount = orders.calculateTotalAmount();
+ OutputView.printTotalOrderAmount(totalOrderAmount);
+ return totalOrderAmount;
+ }
+
+ private Benefits createBenefits(Orders orders, Date date) {
+ Benefits benefits = EventManager.toBenefits(orders, date);
+ OutputView.printBenefitDetail(benefits);
+ return benefits;
+ }
+
+ private int createTotalBenefitAmount(Benefits benefits) {
+ int totalBenefitAmount = benefits.calculateTotalBenefitAmount();
+ OutputView.printTotalBenefitAmount(totalBenefitAmount);
+ return totalBenefitAmount;
+ }
+
+ private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) {
+ int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount);
+ OutputView.printPaymentAmount(paymentAmount);
+ }
+
+ private void createBadge(Benefits benefits, int totalBenefitAmount) {
+ Badge badge = benefits.toEventBadge(totalBenefitAmount);
+ OutputView.printEventBadge(badge);
+ }
+} | Java | ๋ค๋ค ์์ธ๋ฅผ e ๋ผ๊ณ ํ๊ธฐํ์ง๋ง ๊ทธ๋๋ error ๋ผ๊ณ ์กฐ๊ธ ๋ ์์ธํ๊ฒ ํํํ๋ ๊ฒ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
์ฌ์ค ์ ๋ ์ค๋ผ๊ณ ํ์๋๋ฐ ์ด๋ค ๋ถ์ด ๋ฆฌ๋ทฐ๋ ์ด๋ฌํ ๋ถ๋ถ์ ์ธ๊ธํ ๊ฒ์ ๋ณธ ์ ์ด ์์ด์์ |
@@ -0,0 +1,48 @@
+package christmas.domain;
+
+public enum Menu {
+ MUSHROOM_SOUP("์์ก์ด์ํ", 6_000, "์ ํผํ์ด์ "),
+ TAPAS("ํํ์ค", 5_500, "์ ํผํ์ด์ "),
+ CAESAR_SALAD("์์ ์๋ฌ๋", 8_000, "์ ํผํ์ด์ "),
+ T_BONE_STEAK("ํฐ๋ณธ์คํ
์ดํฌ", 55_000, "๋ฉ์ธ"),
+ BBQ_RIBS("๋ฐ๋นํ๋ฆฝ", 54_000, "๋ฉ์ธ"),
+ SEAFOOD_PASTA("ํด์ฐ๋ฌผํ์คํ", 35_000, "๋ฉ์ธ"),
+ CHRISTMAS_PASTA("ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25_000, "๋ฉ์ธ"),
+ CHOCOLATE_CAKE("์ด์ฝ์ผ์ดํฌ", 15_000, "๋์ ํธ"),
+ ICE_CREAM("์์ด์คํฌ๋ฆผ", 5_000, "๋์ ํธ"),
+ ZERO_COKE("์ ๋ก์ฝ๋ผ", 3_000, "์๋ฃ"),
+ RED_WINE("๋ ๋์์ธ", 60_000, "์๋ฃ"),
+ CHAMPAGNE("์ดํ์ธ", 25_000, "์๋ฃ"),
+ NONE("์์", 0, "์์");
+
+ private final String menuName;
+ private final int price;
+ private final String type;
+
+ Menu(String menuName, int price, String type) {
+ this.menuName = menuName;
+ this.price = price;
+ this.type = type;
+ }
+
+ public static Menu from(String menuName) {
+ for (Menu menu : values()) {
+ if (menu.getMenuName().equals(menuName)) {
+ return menu;
+ }
+ }
+ return NONE;
+ }
+
+ public String getMenuName() {
+ return menuName;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public String getType() {
+ return type;
+ }
+} | Java | Stream์ ์ด์ฉํด ๋ฉ๋ด๊ฐ ํฌํจ๋ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด contains๋ก ํ์ธํ๋ ๊ฒ ์กฐ๊ธ ๋ ๊ฐ๊ฒฐํด ๋ณด์ผ์๋ ์์ ๊ฒ ๊ฐ์๋ฐ ์ด๋ป๊ฒ ์๊ฐํ์๋์?? |
@@ -0,0 +1,17 @@
+package christmas.domain;
+
+public enum Error {
+ INVALID_DATE("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ INVALID_ORDER("[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ INVALID_COUNT("[ERROR] ๋ฉ๋ด๋ ํ ๋ฒ์ ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์"),
+ INVALID_TYPE("[ERROR] ์๋ฃ๋ง ์ฃผ๋ฌธ ์, ์ฃผ๋ฌธํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์");
+ private final String message;
+
+ Error(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+} | Java | [Error] ์ ๊ณตํต๋ถ๋ถ์ด๋ค๋ณด๋ ๋ฐ๋ก ๋นผ๋์ด์ ๋ถ๋ฌ์ฌ๋ this message = " [ error ]"+ message ๋ก ํด๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์๋ฐ ์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,76 @@
+package christmas.controller;
+
+import christmas.domain.Badge;
+import christmas.domain.Benefits;
+import christmas.domain.Convertor;
+import christmas.domain.Date;
+import christmas.domain.EventManager;
+import christmas.domain.Orders;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+public class EventController {
+ public void start() {
+ OutputView.printWelcome();
+ Date date = createDate();
+ Orders orders = createOrders();
+ int totalOrderAmount = createTotalOrderAmount(orders);
+ OutputView.printGift(orders.hasGift());
+ Benefits benefits = createBenefits(orders, date);
+ int totalBenefitAmount = createTotalBenefitAmount(benefits);
+ createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount);
+ createBadge(benefits, totalBenefitAmount);
+ }
+
+ private Date createDate() {
+ while (true) {
+ try {
+ Date date = new Date(InputView.readDate());
+ OutputView.printPreview(date);
+ return date;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private Orders createOrders() {
+ while (true) {
+ try {
+ Orders orders = Convertor.toOrders(InputView.readOrder());
+ OutputView.printOrders(orders);
+ return orders;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private int createTotalOrderAmount(Orders orders) {
+ int totalOrderAmount = orders.calculateTotalAmount();
+ OutputView.printTotalOrderAmount(totalOrderAmount);
+ return totalOrderAmount;
+ }
+
+ private Benefits createBenefits(Orders orders, Date date) {
+ Benefits benefits = EventManager.toBenefits(orders, date);
+ OutputView.printBenefitDetail(benefits);
+ return benefits;
+ }
+
+ private int createTotalBenefitAmount(Benefits benefits) {
+ int totalBenefitAmount = benefits.calculateTotalBenefitAmount();
+ OutputView.printTotalBenefitAmount(totalBenefitAmount);
+ return totalBenefitAmount;
+ }
+
+ private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) {
+ int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount);
+ OutputView.printPaymentAmount(paymentAmount);
+ }
+
+ private void createBadge(Benefits benefits, int totalBenefitAmount) {
+ Badge badge = benefits.toEventBadge(totalBenefitAmount);
+ OutputView.printEventBadge(badge);
+ }
+} | Java | ์ ์ฒด์ ์ผ๋ก create๋ผ๋ ๋ฉ์๋๋ช
์ ์ฌ์ฉํ๋๋ฐ create๊ฐ ๊ฐ์ฒด ์์ฑ์ ๋๋์ ์ฃผ๋๊ฑฐ๊ฐ์์ ๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ๋ฉ์๋๋ช
์ ์์ฑํด๋ณด๋๊ฑด ์ด๋จ๊น์ ? |
@@ -0,0 +1,43 @@
+package christmas.domain;
+
+import static christmas.domain.Error.INVALID_ORDER;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+public class Convertor {
+ private static final String ORDERS_DELIMITER = ",";
+ private static final String ORDER_DELIMITER = "-";
+ private static final int MENU_INDEX = 0;
+ private static final int COUNT_INDEX = 1;
+ private static final String REGEXP_PATTERN_ORDER = "([๊ฐ-ํฃ]+)(-)(\\d+)";
+
+ public static Orders toOrders(String input) {
+ List<Order> orders = Arrays.stream(input.split(ORDERS_DELIMITER))
+ .map(String::trim)
+ .map(Convertor::toOrder)
+ .collect(Collectors.toList());
+ return new Orders(orders);
+ }
+
+ private static Order toOrder(String input) {
+ validatePattern(input);
+ String[] menus = input.split(ORDER_DELIMITER);
+ String menuName = menus[MENU_INDEX];
+ int count = Integer.parseInt(menus[COUNT_INDEX]);
+ return new Order(menuName, count);
+ }
+
+ private static void validatePattern(String input) {
+ if (!isValidPattern(input)) {
+ throw new IllegalArgumentException(INVALID_ORDER.getMessage());
+ }
+ }
+
+ private static boolean isValidPattern(String input) {
+ return Pattern.matches(REGEXP_PATTERN_ORDER, input);
+ }
+
+} | Java | ์ ์ ๋ฉ์๋๋ก ์ฌ์ฉํ๋ ๊ธฐ์ค์ด ๊ถ๊ธํฉ๋๋ค. ํ๋ฆฌ์ฝ์ค ๊ธฐ๊ฐ๋์ ๋ฏธ์
ํ๋ฉด์ ์จ๋ณด๊ธฐ๋ ํ๋๋ฐ ์ด๋จ๋ ์จ์ผํ ์ง ์ ๋ชจ๋ฅด๊ฒ ์ด์์ |
@@ -0,0 +1,24 @@
+package christmas.domain;
+
+public enum Badge {
+ STAR("๋ณ", 5_000),
+ TREE("ํธ๋ฆฌ", 10_000),
+ SANTA("์ฐํ", 20_000),
+ NONE("์์", 0);
+
+ private final String type;
+ private final int amount;
+
+ Badge(String type, int amount) {
+ this.type = type;
+ this.amount = amount;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public int getAmount() {
+ return amount;
+ }
+} | Java | private์ ์ง์ ํด ์ฃผ์ง ์์ ์ด์ ๊ฐ ๋ฐ๋ก ์๋์? |
@@ -0,0 +1,56 @@
+package christmas.domain;
+
+
+import static christmas.domain.Badge.NONE;
+import static christmas.domain.Badge.SANTA;
+import static christmas.domain.Badge.STAR;
+import static christmas.domain.Badge.TREE;
+
+import java.util.Map;
+
+public class Benefits {
+ private final Map<Event, Integer> benefits;
+
+ public Benefits(Map<Event, Integer> benefits) {
+ this.benefits = benefits;
+ }
+
+ public int calculateTotalBenefitAmount() {
+ return benefits.values()
+ .stream()
+ .mapToInt(amount -> amount)
+ .sum();
+ }
+
+ public Badge toEventBadge(int totalBenefit) {
+ if (isStarBadge(totalBenefit)) {
+ return STAR;
+ }
+ if (isTreeBadge(totalBenefit)) {
+ return TREE;
+ }
+ if (isSantaBadge(totalBenefit)) {
+ return SANTA;
+ }
+ return NONE;
+ }
+
+ private boolean isStarBadge(int totalBenefit) {
+ return totalBenefit >= STAR.getAmount() && totalBenefit < TREE.getAmount();
+ }
+
+ private boolean isTreeBadge(int totalBenefit) {
+ return totalBenefit >= TREE.getAmount() && totalBenefit < SANTA.getAmount();
+ }
+
+ private boolean isSantaBadge(int totalBenefit) {
+ return totalBenefit >= SANTA.getAmount();
+ }
+
+ public Map<Event, Integer> getBenefits() {
+ return benefits;
+ }
+}
+
+
+ | Java | ๋์ค ๊ฐํ์ด ๋ค์ด๊ฐ๊ฑฐ ๊ฐ์์ !!
๋ค์๋ฒ์ ์์ํ ์ปจ๋ฒค์
๋ ์ฑ๊ธฐ๋ฉด ์ข์๊ฑฐ๊ฐ์์ |
@@ -0,0 +1,51 @@
+package christmas.domain;
+
+import static christmas.domain.Error.INVALID_DATE;
+import static java.time.DayOfWeek.FRIDAY;
+import static java.time.DayOfWeek.SATURDAY;
+import static java.time.DayOfWeek.SUNDAY;
+
+import java.time.DayOfWeek;
+import java.time.LocalDate;
+
+public class Date {
+ private static final int MIN_DATE = 1;
+ private static final int MAX_DATE = 31;
+ private static final int THIS_YEAR = 2023;
+ private static final int THIS_MONTH = 12;
+ private static final int CHRISTMAS_DATE = 25;
+ private final int value;
+
+ public Date(int value) {
+ validateRange(value);
+ this.value = value;
+ }
+
+ public boolean isDayBeforeChristmas() {
+ return value <= CHRISTMAS_DATE;
+ }
+
+ public boolean isWeekend() {
+ DayOfWeek dayOfWeek = createDayOfWeek();
+ return dayOfWeek == FRIDAY || dayOfWeek == SATURDAY;
+ }
+
+ public boolean isStarDay() {
+ return createDayOfWeek() == SUNDAY || value == CHRISTMAS_DATE;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ private DayOfWeek createDayOfWeek() {
+ LocalDate localDate = LocalDate.of(THIS_YEAR, THIS_MONTH, value);
+ return localDate.getDayOfWeek();
+ }
+
+ private void validateRange(int value) {
+ if (value > MAX_DATE || value < MIN_DATE) {
+ throw new IllegalArgumentException(INVALID_DATE.getMessage());
+ }
+ }
+}
\ No newline at end of file | Java | Date๋ผ๋ ํด๋์ค๊ฐ validateRange๋ ๊ทธ๋ ๊ณ 12์์๋ง ํ์ ๋๊ฑฐ ๊ฐ์๋ฐ Date๋ผ๊ณ class๋ช
์ ์ง๋๊ฑด ๋๋ฌด ํฌ๊ด์ ์ด๊ฒ ๋๊ปด์ ธ์ ์กฐ๊ธ ๋ ๊ตฌ์ฒด์ ์ธ ๋ค๋ฅธ์ด๋ฆ์ ์ฌ์ฉํ๋ค๋ฉด ์ด๋จ๊น ์ถ์ต๋๋ค |
@@ -0,0 +1,48 @@
+package christmas.domain;
+
+public enum Menu {
+ MUSHROOM_SOUP("์์ก์ด์ํ", 6_000, "์ ํผํ์ด์ "),
+ TAPAS("ํํ์ค", 5_500, "์ ํผํ์ด์ "),
+ CAESAR_SALAD("์์ ์๋ฌ๋", 8_000, "์ ํผํ์ด์ "),
+ T_BONE_STEAK("ํฐ๋ณธ์คํ
์ดํฌ", 55_000, "๋ฉ์ธ"),
+ BBQ_RIBS("๋ฐ๋นํ๋ฆฝ", 54_000, "๋ฉ์ธ"),
+ SEAFOOD_PASTA("ํด์ฐ๋ฌผํ์คํ", 35_000, "๋ฉ์ธ"),
+ CHRISTMAS_PASTA("ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25_000, "๋ฉ์ธ"),
+ CHOCOLATE_CAKE("์ด์ฝ์ผ์ดํฌ", 15_000, "๋์ ํธ"),
+ ICE_CREAM("์์ด์คํฌ๋ฆผ", 5_000, "๋์ ํธ"),
+ ZERO_COKE("์ ๋ก์ฝ๋ผ", 3_000, "์๋ฃ"),
+ RED_WINE("๋ ๋์์ธ", 60_000, "์๋ฃ"),
+ CHAMPAGNE("์ดํ์ธ", 25_000, "์๋ฃ"),
+ NONE("์์", 0, "์์");
+
+ private final String menuName;
+ private final int price;
+ private final String type;
+
+ Menu(String menuName, int price, String type) {
+ this.menuName = menuName;
+ this.price = price;
+ this.type = type;
+ }
+
+ public static Menu from(String menuName) {
+ for (Menu menu : values()) {
+ if (menu.getMenuName().equals(menuName)) {
+ return menu;
+ }
+ }
+ return NONE;
+ }
+
+ public String getMenuName() {
+ return menuName;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public String getType() {
+ return type;
+ }
+} | Java | ์ฌ๊ธฐ๋ ์ ๊ทผ ์ง์ ์๊ฐ ๋ฐ๋ก ์ค์ ๋์ง ์์ ์ด์ ๊ฐ ์์๊น์ ? |
@@ -0,0 +1,92 @@
+package christmas.domain;
+
+import static christmas.domain.Error.INVALID_ORDER;
+import static christmas.domain.Menu.NONE;
+
+import java.util.Objects;
+
+public class Order {
+ private static final int MIN_ORDER_COUNT = 1;
+ private final String menuName;
+ private final int count;
+
+ public Order(String menuName, int count) {
+ validateMenuName(menuName);
+ validateCount(count);
+ this.menuName = menuName;
+ this.count = count;
+ }
+
+ public int calculateByMenu() {
+ Menu menu = Menu.from(menuName);
+ return menu.getPrice() * count;
+ }
+
+
+ public boolean isBeverage() {
+ Menu menu = Menu.from(menuName);
+ return menu.getType().equals("์๋ฃ");
+ }
+
+ public int countDessert() {
+ if (isDessert()) {
+ return count;
+ }
+ return 0;
+ }
+
+ private boolean isDessert() {
+ Menu menu = Menu.from(menuName);
+ return menu.getType().equals("๋์ ํธ");
+ }
+
+ public int countMain() {
+ if (isMain()) {
+ return count;
+ }
+ return 0;
+ }
+
+
+ private boolean isMain() {
+ Menu menu = Menu.from(menuName);
+ return menu.getType().equals("๋ฉ์ธ");
+ }
+
+ public String getMenuName() {
+ return menuName;
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o instanceof Order order) {
+ return menuName.equals(order.menuName);
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(menuName);
+ }
+
+ private void validateMenuName(String menu) {
+ if (isInvalidMenu(menu)) {
+ throw new IllegalArgumentException(INVALID_ORDER.getMessage());
+ }
+ }
+
+ private boolean isInvalidMenu(String menu) {
+ return Menu.from(menu) == NONE;
+ }
+
+ private void validateCount(int count) {
+ if (count < MIN_ORDER_COUNT) {
+ throw new IllegalArgumentException(INVALID_ORDER.getMessage());
+ }
+ }
+} | Java | ์๊น Type์ ๋ณ๋๋ก Enum ๊ฐ์ ๊ฐ์ฒด๋ก ๊ด๋ฆฌ ํ๋๋ผ๋ฉด "์๋ฃ"-> ์ ๊ฐ์ stringํ์ผ๋ก ๋น๊ตํ์ง ์์๋ ๋์ ๊ฑฐ ๊ฐ์์ !! ๋ง์ฝ Type์ ์ด๋ฆ์ด ๋ฐ๋๊ฒ ๋์๋ ํ๋ํ๋ ๋ค์ ์ฝ๋๋ฅผ ๋ฐ๊พธ๋ ์๊ณ ๋ฅผ ๋ ์ ์์์๊ฑฐ ๊ฐ์ต๋๋ค:D |
@@ -0,0 +1,92 @@
+package christmas.domain;
+
+import static christmas.domain.Error.INVALID_ORDER;
+import static christmas.domain.Menu.NONE;
+
+import java.util.Objects;
+
+public class Order {
+ private static final int MIN_ORDER_COUNT = 1;
+ private final String menuName;
+ private final int count;
+
+ public Order(String menuName, int count) {
+ validateMenuName(menuName);
+ validateCount(count);
+ this.menuName = menuName;
+ this.count = count;
+ }
+
+ public int calculateByMenu() {
+ Menu menu = Menu.from(menuName);
+ return menu.getPrice() * count;
+ }
+
+
+ public boolean isBeverage() {
+ Menu menu = Menu.from(menuName);
+ return menu.getType().equals("์๋ฃ");
+ }
+
+ public int countDessert() {
+ if (isDessert()) {
+ return count;
+ }
+ return 0;
+ }
+
+ private boolean isDessert() {
+ Menu menu = Menu.from(menuName);
+ return menu.getType().equals("๋์ ํธ");
+ }
+
+ public int countMain() {
+ if (isMain()) {
+ return count;
+ }
+ return 0;
+ }
+
+
+ private boolean isMain() {
+ Menu menu = Menu.from(menuName);
+ return menu.getType().equals("๋ฉ์ธ");
+ }
+
+ public String getMenuName() {
+ return menuName;
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o instanceof Order order) {
+ return menuName.equals(order.menuName);
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(menuName);
+ }
+
+ private void validateMenuName(String menu) {
+ if (isInvalidMenu(menu)) {
+ throw new IllegalArgumentException(INVALID_ORDER.getMessage());
+ }
+ }
+
+ private boolean isInvalidMenu(String menu) {
+ return Menu.from(menu) == NONE;
+ }
+
+ private void validateCount(int count) {
+ if (count < MIN_ORDER_COUNT) {
+ throw new IllegalArgumentException(INVALID_ORDER.getMessage());
+ }
+ }
+} | Java | hashCode๋ฅผ ์ฌ์ฉํ์
จ๋ค์ !!!!
ํน์ hashCode๋ฅผ ์ฌ์ฉํ ์ด์ ๋ฅผ ์ฌ์ญค๋ด๋ ๋ ๊น์?๐ |
@@ -0,0 +1,106 @@
+package christmas.domain;
+
+import static christmas.domain.Error.INVALID_COUNT;
+import static christmas.domain.Error.INVALID_ORDER;
+import static christmas.domain.Error.INVALID_TYPE;
+
+import java.util.List;
+
+public class Orders {
+ private static final int MAX_TOTAL_ORDER_COUNT = 20;
+ private static final int LOWER_LIMIT_AMOUNT_FOR_BENEFIT = 10_000;
+ private static final int LOWER_LIMIT_AMOUNT_FOR_GIFT = 120_000;
+
+ private final List<Order> orders;
+
+
+ public Orders(List<Order> orders) {
+ validateDuplicated(orders);
+ validateTotalCount(orders);
+ validateType(orders);
+ this.orders = orders;
+ }
+
+ public int calculatePaymentAmount(int totalAmount, int totalBenefit) {
+ int paymentAmount = totalAmount - totalBenefit;
+ if (hasGift()) {
+ paymentAmount += Event.GIFT.getInitPrice();
+ paymentAmount = minusValueToZero(paymentAmount);
+ return paymentAmount;
+ }
+ paymentAmount = minusValueToZero(paymentAmount);
+ return paymentAmount;
+ }
+
+ private int minusValueToZero(int paymentAmount) {
+ return Math.max(paymentAmount, 0);
+ }
+
+ public int calculateTotalAmount() {
+ return orders.stream()
+ .mapToInt(Order::calculateByMenu)
+ .sum();
+ }
+
+ public boolean isEligible() {
+ return calculateTotalAmount() > LOWER_LIMIT_AMOUNT_FOR_BENEFIT;
+ }
+
+ public boolean hasGift() {
+ return calculateTotalAmount() >= LOWER_LIMIT_AMOUNT_FOR_GIFT;
+ }
+
+
+ public int countDesserts() {
+ return orders.stream()
+ .mapToInt(Order::countDessert)
+ .sum();
+ }
+
+ public int countMains() {
+ return orders.stream()
+ .mapToInt(Order::countMain)
+ .sum();
+ }
+
+ public List<Order> getOrders() {
+ return orders;
+ }
+
+ private void validateDuplicated(List<Order> orders) {
+ if (isDuplicated(orders)) {
+ throw new IllegalArgumentException(INVALID_ORDER.getMessage());
+ }
+ }
+
+ private boolean isDuplicated(List<Order> orders) {
+ return orders.stream()
+ .distinct()
+ .count() != orders.size();
+ }
+
+ private void validateTotalCount(List<Order> orders) {
+ if (calculateTotalCount(orders) > MAX_TOTAL_ORDER_COUNT) {
+ throw new IllegalArgumentException(INVALID_COUNT.getMessage());
+ }
+ }
+
+ private int calculateTotalCount(List<Order> orders) {
+ return orders.stream()
+ .mapToInt(Order::getCount)
+ .sum();
+ }
+
+ private void validateType(List<Order> orders) {
+ if (isInvalidType(orders)) {
+ throw new IllegalArgumentException(INVALID_TYPE.getMessage());
+ }
+ }
+
+ private boolean isInvalidType(List<Order> orders) {
+ return orders.stream()
+ .allMatch(Order::isBeverage);
+ }
+
+
+} | Java | Benefit๊ณผ gif์ limit ๊ธ์ก๋ฅผ Order์์ ์ ํด์ฃผ๋๊ฒ๋ณด๋ค Benefit์ ์ฑ
์์ ๋ฌผ๋ ค์ ๊ฒ์ฆํ๋๊ฑด ์ด๋ ์๊น์? |
@@ -0,0 +1,25 @@
+package christmas.view;
+
+enum Notice {
+ WELCOME("์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น 12์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค."),
+ ASK_DATE_OF_VISIT("12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)"),
+ ASK_MENU_AND_COUNT("์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)"),
+ PREVIEW("12์ %s์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!%n"),
+ ORDER_MENU("\n<์ฃผ๋ฌธ ๋ฉ๋ด>"),
+ TOTAL_ORDER_AMOUNT("\n<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>"),
+ GIFT_MENU("\n<์ฆ์ ๋ฉ๋ด>"),
+ BENEFIT_LIST("\n<ํํ ๋ด์ญ>"),
+ TOTAL_BENEFIT_AMOUNT("\n<์ดํํ ๊ธ์ก>"),
+ TOTAL_PAYMENT_AMOUNT("\n<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>"),
+ EVENT_BADGE("\n<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>");
+
+ private final String message;
+
+ Notice(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+} | Java | %s์ผ ์ฌ์ฉํ์ ๊ฒ์ฒ๋ผ 12์๋ ๋์ ์ผ๋ก ์ฒ๋ฆฌํด๋ณผ ์ ์์๊ฑฐ ๊ฐ์์ :) ๐ |
@@ -0,0 +1,38 @@
+package christmas.domain;
+
+import static christmas.domain.Badge.NONE;
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+class BenefitsTest {
+ Benefits benefits;
+
+ @Test
+ @DisplayName("ํํ์ ์ดํฉ ๊ณ์ฐ")
+ void calculateTotalBenefit() {
+ int totalBenefit = benefits.calculateTotalBenefitAmount();
+ assertThat(totalBenefit).isEqualTo(31246);
+ }
+
+ @ParameterizedTest
+ @DisplayName("์ด ํํ ๊ธ์ก์ ๋ฐ๋ฅธ ๋ฑ์ง ์ง๊ธ")
+ @CsvSource(value = {"5500,๋ณ", "10500,ํธ๋ฆฌ", "21000,์ฐํ"})
+ void toEventBadge(int input, String expect) {
+ Badge badge = benefits.toEventBadge(input);
+ if (badge != NONE) {
+ assertThat(badge.getType()).isEqualTo(expect);
+ }
+ }
+
+ @BeforeEach
+ void setUp() {
+ Date date = new Date(3);
+ Orders orders = Convertor.toOrders("ํฐ๋ณธ์คํ
์ดํฌ-1,๋ฐ๋นํ๋ฆฝ-1,์ด์ฝ์ผ์ดํฌ-2,์ ๋ก์ฝ๋ผ-1");
+ benefits = EventManager.toBenefits(orders, date);
+ }
+}
\ No newline at end of file | Java | CsvSouce๊ฐ ์์๊ตฐ์ ?!!! ์ ๋ ์ด๊ฑธ ๋ชฐ๋ผ์ ํ๊ฐ์ง ๋ณ์๋ก๋ง ํ
์คํธ ์ฝ๋๋ฅผ ์์ฑํ๋๋ผ ์ ๋จน์ ๋ถ๋ถ์ด ์์๋๋ฐ ,, ๋ฐฐ์๊ฐ๋๋ค :O ์ ์ฉํ๊ฒ ์ธ ์ ์์๊ฑฐ๊ฐ์์ |
@@ -0,0 +1,38 @@
+package christmas.domain;
+
+import static christmas.domain.Badge.NONE;
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+class BenefitsTest {
+ Benefits benefits;
+
+ @Test
+ @DisplayName("ํํ์ ์ดํฉ ๊ณ์ฐ")
+ void calculateTotalBenefit() {
+ int totalBenefit = benefits.calculateTotalBenefitAmount();
+ assertThat(totalBenefit).isEqualTo(31246);
+ }
+
+ @ParameterizedTest
+ @DisplayName("์ด ํํ ๊ธ์ก์ ๋ฐ๋ฅธ ๋ฑ์ง ์ง๊ธ")
+ @CsvSource(value = {"5500,๋ณ", "10500,ํธ๋ฆฌ", "21000,์ฐํ"})
+ void toEventBadge(int input, String expect) {
+ Badge badge = benefits.toEventBadge(input);
+ if (badge != NONE) {
+ assertThat(badge.getType()).isEqualTo(expect);
+ }
+ }
+
+ @BeforeEach
+ void setUp() {
+ Date date = new Date(3);
+ Orders orders = Convertor.toOrders("ํฐ๋ณธ์คํ
์ดํฌ-1,๋ฐ๋นํ๋ฆฝ-1,์ด์ฝ์ผ์ดํฌ-2,์ ๋ก์ฝ๋ผ-1");
+ benefits = EventManager.toBenefits(orders, date);
+ }
+}
\ No newline at end of file | Java | setUp ํจ์์ ๊ฒฝ์ฐ ๊ฐ๋
์ฑ(?)์ ์ํด ์๋จ์ ์์น์ํค๋ฉด ๋ ์ข์๊ฑฐ๊ฐ์์ |
@@ -0,0 +1,70 @@
+# ๊ธฐ๋ฅ ๊ตฌํ ๋ชฉ๋ก
+
+## view
+
+- [x] **InputView**
+ - ๋ฐฉ๋ฌธ ๋ ์ง๋ฅผ ์
๋ ฅ ๋ฐ์ ์ ์๋ก ๋ฐํํ๋ค.
+ - ์ซ์ ์ด์ธ์ ๊ฐ์ ์
๋ ฅ์ ์์ธ๋ฅผ ๋ฐ์ ์ํจ๋ค.
+ - ์ฃผ๋ฌธ ๋ด์ฉ์ ์
๋ ฅ ๋ฐ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ค.
+- [x] **Notice**
+ - InputView์ OutputView์์ ์ฌ์ฉํ๋ ๋ฉ์์ง๋ฅผ ์ ์ฅํ๋ค.
+- [x] **OutputView**
+ - ์๋ฌ ๋ฐ์์ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+ - ์ธ์ฌ๋ง์ ์ถ๋ ฅํ๋ค.
+ - ํํ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ๋ด์ฉ์ ์ถ๋ ฅํ๋ค.
+ - ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ์ถ๋ ฅ ํ์์ ๋ง๊ฒ ๊ฐ๊ณตํ๋ค.
+
+## domain
+
+- [x] **Date**
+ - ์
๋ ฅ ๋ฐ์ ๋ ์ง๋ฅผ ์ ์ฅํ๋ค.
+ - 1-31 ์ด์ธ์ ๋ ์ง๋ฅผ ๋ฐ์ผ๋ฉด ์์ธ๋ฅผ ๋ฐ์ ์ํจ๋ค.
+ - 25์ผ ์ ์ธ์ง ํ์ธํ๋ค.
+ - ์ฃผ๋ง์ธ์ง ํ์ธํ๋ค.
+ - ์ด๋ฒคํธ ๋ฌ๋ ฅ์ ๋ณ์ด ์๋์ง ํ์ธํ๋ค.
+- [x] **Convertor**
+ - ์ฃผ๋ฌธ ๋ด์ฉ์ด ๋ด๊ธด ๋ฌธ์์ด์ Order ๊ฐ์ฒด๋ก ๊ตฌ์ฑ๋ Orders ๊ฐ์ฒด๋ก ๋ณํํ๋ค.
+ - ์ฝค๋ง(,) ๋จ์๋ก Order ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
+ - ๋ฉ๋ด์ ์ฃผ๋ฌธ๋์ Order ๊ฐ์ฒด์ ์ ์ฅํ๋ค.
+ - ์์์ ๋ค๋ฅธ ํ์์ ์ฃผ๋ฌธ ๋ด์ฉ์ ๋ฐ์ผ๋ฉด ์์ธ๋ฅผ ๋ฐ์์ํจ๋ค.
+- [x] **Order**
+ - ์
๋ ฅ ๋ฐ์ ๋ฉ๋ด๊ฐ ๋ฉ๋ดํ์ ์์ผ๋ฉด ์์ธ๋ฅผ ๋ฐ์์ํจ๋ค.
+ - ๋ฉ๋ด๋ณ ์ฃผ๋ฌธ๋์ด 1๊ฐ๋ณด๋ค ์์ผ๋ฉด ์์ธ๋ฅผ ๋ฐ์์ํจ๋ค.
+ - ์ฃผ๋ฌธ๋ณ ๊ธ์ก์ ๊ณ์ฐํ๋ค.
+ - ๋ฉ๋ด์ ์ข
๋ฅ๋ฅผ ํ์ธํ๋ค.
+ - ๋ฉ๋ด ์ข
๋ฅ๋ณ ๊ฐฏ์๋ฅผ ์ผ๋ค.
+- [x] **Orders**
+ - ๋ฉ๋ด์ ์ค๋ณต์ฌ๋ถ๋ฅผ ํ์ธํ์ฌ ์์ธ๋ฅผ ๋ฐ์์ํจ๋ค.
+ - ์ด ์ฃผ๋ฌธ๋์ด 20๊ฐ๊ฐ ๋์ผ๋ฉด ์์ธ๋ฅผ ๋ฐ์์ํจ๋ค.
+ - ์๋ฃ๋ง ์ฃผ๋ฌธํ๋ฉด ์์ธ๋ฅผ ๋ฐ์์ํจ๋ค.
+ - ์ด ์ฃผ๋ฌธ๊ธ์ก์ ๊ณ์ฐํ๋ค.
+ - ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ๊ณ์ฐํ๋ค.
+ - ๊ฒฐ์ ๊ธ์ก์ด ์์์ผ ๊ฒฝ์ฐ 0์ ๋ฐํํ๋ค.
+ - ์ด๋ฒคํธ ์ฐธ์ฌ ๊ฐ๋ฅ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
+ - ์ฌ์ํ ์ฆ์ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
+ - ๋ฉ๋ด ์ข
๋ฅ๋ณ ์ฃผ๋ฌธ๋์ ๊ณ์ฐํ๋ค.
+- [x] **EventManager**
+ - ๊ฐ ์ด๋ฒคํธ๋ณ ์ ์ฉ์ฌ๋ถ๋ฅผ ํ์ธํ๊ณ ํํ ๊ธ์ก์ ๊ณ์ฐํ๋ค.
+ - ํํ ๋ด์ญ์ ๋ด์ Benefits ๊ฐ์ฒด๋ฅผ ๋ง๋ ๋ค.
+- [x] **Benefits**
+ - ์ด ํํ ๊ธ์ก์ ๊ณ์ฐํ๋ค.
+ - ํํ ๊ธ์ก์ ๋ฐ๋ฅธ ๋ฑ์ง ๊ฐ์ฒด๋ฅผ ๋ง๋ ๋ค.
+- [x] **Menu**
+ - ๋ฉ๋ด ์ด๋ฆ, ๊ฐ๊ฒฉ, ์ข
๋ฅ๋ฅผ ์ ์ฅํ๋ค.
+ - ๋ฉ๋ด ์ด๋ฆ์ ๋ฐ์ ์ผ์นํ๋ ๋ฉ๋ด ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
+ - ์ผ์นํ๋ ๋ฉ๋ด๊ฐ ์๋ค๋ฉด NONE์ ๋ฐํํ๋ค.
+- [x] **Event**
+ - ์ด๋ฒคํธ ์ด๋ฆ, ์ด๊ธฐ ์ ์ฉ ๊ธ์ก, ๋จ์๋น ์ ์ฉ ๊ธ์ก์ ์ ์ฅํ๋ค.
+- [x] **Badge**
+ - ๋ฑ์ง ์ข
๋ฅ, ๊ธฐ์ค ๊ธ์ก์ ์ ์ฅํ๋ค.
+- [x] **Error**
+ - ์๋ฌ ๋ฉ์์ง ์ถ๋ ฅ์ ์ฌ์ฉ๋๋ ๋ด์ฉ์ ์ ์ฅํ๋ค.
+
+## controller
+
+- [x] **EventController**
+ - ์์ธ ๋ฐ์์ ํด๋น ๋ถ๋ถ์ ๋ฐ๋ณต์ํจ๋ค.
+ - view์ ๋ด์ฉ์ domain์ ์ ๋ฌํ๋ค.
+ - domain์ ๋ด์ฉ์ view์ ์ ๋ฌํ๋ค.
+
+ | Unknown | ์ฝ๋๋ฆฌ๋ทฐ์ ์์, ๊ฐ์ฒด ๊ธฐ๋ฅ๋ณ๋ก ๋ถ๋ฆฌ๋์ด ์์ด ์ ์ฒด ๋น์ฆ๋์ค ๋ก์ง ๋ฐ ๊ฐ ๊ฐ์ฒด์ ์ญํ ์ ํ์
ํ๊ธฐ ํธ๋ฆฌํ์ด์ ๐ |
@@ -0,0 +1,76 @@
+package christmas.controller;
+
+import christmas.domain.Badge;
+import christmas.domain.Benefits;
+import christmas.domain.Convertor;
+import christmas.domain.Date;
+import christmas.domain.EventManager;
+import christmas.domain.Orders;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+public class EventController {
+ public void start() {
+ OutputView.printWelcome();
+ Date date = createDate();
+ Orders orders = createOrders();
+ int totalOrderAmount = createTotalOrderAmount(orders);
+ OutputView.printGift(orders.hasGift());
+ Benefits benefits = createBenefits(orders, date);
+ int totalBenefitAmount = createTotalBenefitAmount(benefits);
+ createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount);
+ createBadge(benefits, totalBenefitAmount);
+ }
+
+ private Date createDate() {
+ while (true) {
+ try {
+ Date date = new Date(InputView.readDate());
+ OutputView.printPreview(date);
+ return date;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private Orders createOrders() {
+ while (true) {
+ try {
+ Orders orders = Convertor.toOrders(InputView.readOrder());
+ OutputView.printOrders(orders);
+ return orders;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private int createTotalOrderAmount(Orders orders) {
+ int totalOrderAmount = orders.calculateTotalAmount();
+ OutputView.printTotalOrderAmount(totalOrderAmount);
+ return totalOrderAmount;
+ }
+
+ private Benefits createBenefits(Orders orders, Date date) {
+ Benefits benefits = EventManager.toBenefits(orders, date);
+ OutputView.printBenefitDetail(benefits);
+ return benefits;
+ }
+
+ private int createTotalBenefitAmount(Benefits benefits) {
+ int totalBenefitAmount = benefits.calculateTotalBenefitAmount();
+ OutputView.printTotalBenefitAmount(totalBenefitAmount);
+ return totalBenefitAmount;
+ }
+
+ private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) {
+ int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount);
+ OutputView.printPaymentAmount(paymentAmount);
+ }
+
+ private void createBadge(Benefits benefits, int totalBenefitAmount) {
+ Badge badge = benefits.toEventBadge(totalBenefitAmount);
+ OutputView.printEventBadge(badge);
+ }
+} | Java | ๊ฐํ๋ ํ๋์ ์ปจ๋ฒค์
์ด๋ค!
์กฐ๊ธ ๋ ๊ธฐ๋ฅ๋ณ๋ก ๋ถ๋ฆฌํด์ ๊ฐํ์ผ๋ก ๊ตฌ๋ถํ๋ค๋ฉด ๋ ์ฝ๊ธฐ ํธํ ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,76 @@
+package christmas.controller;
+
+import christmas.domain.Badge;
+import christmas.domain.Benefits;
+import christmas.domain.Convertor;
+import christmas.domain.Date;
+import christmas.domain.EventManager;
+import christmas.domain.Orders;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+public class EventController {
+ public void start() {
+ OutputView.printWelcome();
+ Date date = createDate();
+ Orders orders = createOrders();
+ int totalOrderAmount = createTotalOrderAmount(orders);
+ OutputView.printGift(orders.hasGift());
+ Benefits benefits = createBenefits(orders, date);
+ int totalBenefitAmount = createTotalBenefitAmount(benefits);
+ createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount);
+ createBadge(benefits, totalBenefitAmount);
+ }
+
+ private Date createDate() {
+ while (true) {
+ try {
+ Date date = new Date(InputView.readDate());
+ OutputView.printPreview(date);
+ return date;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private Orders createOrders() {
+ while (true) {
+ try {
+ Orders orders = Convertor.toOrders(InputView.readOrder());
+ OutputView.printOrders(orders);
+ return orders;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private int createTotalOrderAmount(Orders orders) {
+ int totalOrderAmount = orders.calculateTotalAmount();
+ OutputView.printTotalOrderAmount(totalOrderAmount);
+ return totalOrderAmount;
+ }
+
+ private Benefits createBenefits(Orders orders, Date date) {
+ Benefits benefits = EventManager.toBenefits(orders, date);
+ OutputView.printBenefitDetail(benefits);
+ return benefits;
+ }
+
+ private int createTotalBenefitAmount(Benefits benefits) {
+ int totalBenefitAmount = benefits.calculateTotalBenefitAmount();
+ OutputView.printTotalBenefitAmount(totalBenefitAmount);
+ return totalBenefitAmount;
+ }
+
+ private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) {
+ int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount);
+ OutputView.printPaymentAmount(paymentAmount);
+ }
+
+ private void createBadge(Benefits benefits, int totalBenefitAmount) {
+ Badge badge = benefits.toEventBadge(totalBenefitAmount);
+ OutputView.printEventBadge(badge);
+ }
+} | Java | 
์์ถ๋๊ป์ ๋ฆฌ๋ทฐ ์ฃผ์
จ๋ ๋ด์ฉ์ ๋ํด์, ํ๋ฒ ์ฌ์ญ๊ณ ์ถ์ ๋ถ๋ถ์ด ์๊ฒจ ์ฌ๊ธฐ์๋ ๊ธ์ ๋จ๊ฒจ๋ด
๋๋ค.
1. ์ปจํธ๋กค๋ฌ๊ฐ ์ ์ฒด ๋น์ฆ๋์ค ๋ก์ง์ ๊ดํ ํ๊ณ ์์ด์.
`์ปจํธ๋กค๋ฌ Layer ๋จ์ํ
์คํธ = ํตํฉํ
์คํธ`์ ์ญํ ์ ์ํํ ๊ฒ ๊ฐ์๋ฐ, ์ปจํธ๋กค๋ฌ์ ๋ถ๋ฆฌ๊ฐ ํ์ํ์ง ์์๊น์?
1-1. ์ปจํธ๋กค๋ฌ์ ๋ถ๋ฆฌ๊ฐ ํ์ํ๋ค๊ณ ํ๋จ๋๋ฉด, EventController(Main์ ๊ธฐ๋ฅ)๊ฐ ๋ค๋ฅธ ์ปจํธ๋กค๋ฌ๋ฅผ ์์กดํ๊ฒ ๋๋๋ฐ
์ปจํธ๋กค๋ฌ๋ View์ Domain์ ์ฐ๊ฒฐํ๋ ์ญํ ์ธ๋ฐ, ์ปจํธ๋กค๋ฌ๊ฐ ํธ์ถ์ด ์ฆ์์ง ๊ฒ ๊ฐ์์.
1-2. EventController์์ ๋ชจ๋ ๋ก์ง์ ํธ์ถํ๋ ๊ฒ๊ณผ, Application.main์์ ๋ชจ๋ ๋ก์ง์ ํธ์ถํ๋ ๊ฒ
์ด๋ค ์ฐจ์ด๊ฐ ์์๊น์? |
@@ -0,0 +1,56 @@
+package christmas.domain;
+
+
+import static christmas.domain.Badge.NONE;
+import static christmas.domain.Badge.SANTA;
+import static christmas.domain.Badge.STAR;
+import static christmas.domain.Badge.TREE;
+
+import java.util.Map;
+
+public class Benefits {
+ private final Map<Event, Integer> benefits;
+
+ public Benefits(Map<Event, Integer> benefits) {
+ this.benefits = benefits;
+ }
+
+ public int calculateTotalBenefitAmount() {
+ return benefits.values()
+ .stream()
+ .mapToInt(amount -> amount)
+ .sum();
+ }
+
+ public Badge toEventBadge(int totalBenefit) {
+ if (isStarBadge(totalBenefit)) {
+ return STAR;
+ }
+ if (isTreeBadge(totalBenefit)) {
+ return TREE;
+ }
+ if (isSantaBadge(totalBenefit)) {
+ return SANTA;
+ }
+ return NONE;
+ }
+
+ private boolean isStarBadge(int totalBenefit) {
+ return totalBenefit >= STAR.getAmount() && totalBenefit < TREE.getAmount();
+ }
+
+ private boolean isTreeBadge(int totalBenefit) {
+ return totalBenefit >= TREE.getAmount() && totalBenefit < SANTA.getAmount();
+ }
+
+ private boolean isSantaBadge(int totalBenefit) {
+ return totalBenefit >= SANTA.getAmount();
+ }
+
+ public Map<Event, Integer> getBenefits() {
+ return benefits;
+ }
+}
+
+
+ | Java | ๋ง์ฝ ๋ฑ์ง๊ฐ 100๊ฐ๋ผ๋ฉด?, 1000๊ฐ๋ผ๋ฉด?
๋ฑ์ง์ ๊ฐฏ์๊ฐ ๋ง์์ง๋ ์ํฉ์์ ๋ชจ๋ ๋ก์ง์ ํ๋์ฝ๋ฉ ํด์ผํ๋ ์ํฉ์ด ์๊ธธ ๊ฒ ๊ฐ์์.
์ด ๋ถ๋ถ๋, ํ๋์ฝ๋ฉ์ด ์๋๋ผ, ๋ฑ์ง์ ์์ฑ์ ๋ฐ๋ผ ๊ตฌ๋ถํ ์ ์๋๋ก ๋ฐฉ๋ฒ์ ๋ชจ์ํด๋ณด์๋ฉด, ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,51 @@
+package christmas.domain;
+
+import static christmas.domain.Error.INVALID_DATE;
+import static java.time.DayOfWeek.FRIDAY;
+import static java.time.DayOfWeek.SATURDAY;
+import static java.time.DayOfWeek.SUNDAY;
+
+import java.time.DayOfWeek;
+import java.time.LocalDate;
+
+public class Date {
+ private static final int MIN_DATE = 1;
+ private static final int MAX_DATE = 31;
+ private static final int THIS_YEAR = 2023;
+ private static final int THIS_MONTH = 12;
+ private static final int CHRISTMAS_DATE = 25;
+ private final int value;
+
+ public Date(int value) {
+ validateRange(value);
+ this.value = value;
+ }
+
+ public boolean isDayBeforeChristmas() {
+ return value <= CHRISTMAS_DATE;
+ }
+
+ public boolean isWeekend() {
+ DayOfWeek dayOfWeek = createDayOfWeek();
+ return dayOfWeek == FRIDAY || dayOfWeek == SATURDAY;
+ }
+
+ public boolean isStarDay() {
+ return createDayOfWeek() == SUNDAY || value == CHRISTMAS_DATE;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ private DayOfWeek createDayOfWeek() {
+ LocalDate localDate = LocalDate.of(THIS_YEAR, THIS_MONTH, value);
+ return localDate.getDayOfWeek();
+ }
+
+ private void validateRange(int value) {
+ if (value > MAX_DATE || value < MIN_DATE) {
+ throw new IllegalArgumentException(INVALID_DATE.getMessage());
+ }
+ }
+}
\ No newline at end of file | Java | ํด๋น ์ด๋ฒคํธ๋ 1์์๋ ์ ๋ฌผ์ ์ฆ์ ํ๋ฉด์, `์ฌ์ฌ์ฉ ์์ `์์ ์์ฐ์ค์ ์์ํ์ด์.
int ํ์
ํ๋์ฝ๋ฉ์ด ์๋๋ผ, LocalDate๋ฅผ ํ์ฉํด 2023๋
12์๊ณผ 2024๋
1์์ ๋ผ์๋ฃ์ด์ ๋ฐ๋ก ์ฌ์ฉํ ์ ์๋๋ก ์ค๊ณํ๋ฉด, ๋ ์ ์ฐํ ์ค๊ณ๊ฐ ๋์์ ๊ฒ์ด๋ผ ์๊ฐ๋์ด์! |
@@ -0,0 +1,33 @@
+package christmas.domain;
+
+import static christmas.domain.Menu.CHAMPAGNE;
+
+public enum Event {
+ CHRISTMAS_D_DAY("ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ", 1_000, 100),
+ WEEKDAY("ํ์ผ ํ ์ธ", 0, 2_023),
+ WEEKEND("์ฃผ๋ง ํ ์ธ", 0, 2_023),
+ SPECIAL("ํน๋ณ ํ ์ธ", 1_000, 0),
+ GIFT("์ฆ์ ์ด๋ฒคํธ", CHAMPAGNE.getPrice(), 0);
+
+ private final String eventName;
+ private final int initPrice;
+ private final int unitPrice;
+
+ Event(String eventName, int initPrice, int unitPrice) {
+ this.eventName = eventName;
+ this.initPrice = initPrice;
+ this.unitPrice = unitPrice;
+ }
+
+ public String getEventName() {
+ return eventName;
+ }
+
+ public int getInitPrice() {
+ return initPrice;
+ }
+
+ public int getUnitPrice() {
+ return unitPrice;
+ }
+} | Java | `initPrice` ์ง๊ด์ ์ผ๋ก ๋ณ์๋ช
๋ง ๋ดค์ ๋, ์ด๋ค int๊ฐ์ ๋ด๋์ง ๋ชจํธํ ๊ฐ์ด ์์ด์ |
@@ -0,0 +1,60 @@
+package christmas.domain;
+
+import static christmas.domain.Event.CHRISTMAS_D_DAY;
+import static christmas.domain.Event.GIFT;
+import static christmas.domain.Event.SPECIAL;
+import static christmas.domain.Event.WEEKDAY;
+import static christmas.domain.Event.WEEKEND;
+
+import java.util.EnumMap;
+import java.util.Map;
+
+public class EventManager {
+
+ public static Benefits toBenefits(Orders orders, Date date) {
+ Map<Event, Integer> benefits = new EnumMap<>(Event.class);
+ if (orders.isEligible()) {
+ benefits.put(CHRISTMAS_D_DAY, calculateDDayDiscount(date));
+ benefits.put(WEEKDAY, calculateWeekdayDiscount(orders, date));
+ benefits.put(WEEKEND, calculateWeekendDiscount(orders, date));
+ benefits.put(SPECIAL, calculateSpecialDiscount(date));
+ benefits.put(GIFT, calculateGiftAmount(orders));
+ }
+ return new Benefits(benefits);
+ }
+
+ private static int calculateDDayDiscount(Date date) {
+ if (date.isDayBeforeChristmas()) {
+ return CHRISTMAS_D_DAY.getInitPrice() + ((date.getValue() - 1) * CHRISTMAS_D_DAY.getUnitPrice());
+ }
+ return 0;
+ }
+
+ private static int calculateWeekdayDiscount(Orders orders, Date date) {
+ if (!date.isWeekend()) {
+ return orders.countDesserts() * WEEKDAY.getUnitPrice();
+ }
+ return 0;
+ }
+
+ private static int calculateWeekendDiscount(Orders orders, Date date) {
+ if (date.isWeekend()) {
+ return orders.countMains() * WEEKEND.getUnitPrice();
+ }
+ return 0;
+ }
+
+ private static int calculateSpecialDiscount(Date date) {
+ if (date.isStarDay()) {
+ return SPECIAL.getInitPrice();
+ }
+ return 0;
+ }
+
+ private static int calculateGiftAmount(Orders orders) {
+ if (orders.hasGift()) {
+ return GIFT.getInitPrice();
+ }
+ return 0;
+ }
+} | Java | ์ค๋ ์ผ์ * 100 + 900 ์ผ๋ก ๊ฐ๋จํ๊ฒ ํํํ ์ ์๋ต๋๋ค >_< |
@@ -0,0 +1,25 @@
+package christmas.view;
+
+enum Notice {
+ WELCOME("์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น 12์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค."),
+ ASK_DATE_OF_VISIT("12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)"),
+ ASK_MENU_AND_COUNT("์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)"),
+ PREVIEW("12์ %s์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!%n"),
+ ORDER_MENU("\n<์ฃผ๋ฌธ ๋ฉ๋ด>"),
+ TOTAL_ORDER_AMOUNT("\n<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>"),
+ GIFT_MENU("\n<์ฆ์ ๋ฉ๋ด>"),
+ BENEFIT_LIST("\n<ํํ ๋ด์ญ>"),
+ TOTAL_BENEFIT_AMOUNT("\n<์ดํํ ๊ธ์ก>"),
+ TOTAL_PAYMENT_AMOUNT("\n<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>"),
+ EVENT_BADGE("\n<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>");
+
+ private final String message;
+
+ Notice(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+} | Java | ์ ๋ @ddrongy๋์ ์๊ฒฌ์ ๋์ํฉ๋๋ค!,๊ทธ๋ ๋ค๋ฉด 1์๋ก ๋ฐ๋์์๋, (2024๋
1์)
System Constraint๋ ์๋์ผ๋ก ๋ฐ๋๋๋ก(LocalDate..?) ์ค์ ํ๋ ๋ฐฉํฅ์ผ๋ก ์์ ๋ ํ์ํ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,76 @@
+package christmas.controller;
+
+import christmas.domain.Badge;
+import christmas.domain.Benefits;
+import christmas.domain.Convertor;
+import christmas.domain.Date;
+import christmas.domain.EventManager;
+import christmas.domain.Orders;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+public class EventController {
+ public void start() {
+ OutputView.printWelcome();
+ Date date = createDate();
+ Orders orders = createOrders();
+ int totalOrderAmount = createTotalOrderAmount(orders);
+ OutputView.printGift(orders.hasGift());
+ Benefits benefits = createBenefits(orders, date);
+ int totalBenefitAmount = createTotalBenefitAmount(benefits);
+ createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount);
+ createBadge(benefits, totalBenefitAmount);
+ }
+
+ private Date createDate() {
+ while (true) {
+ try {
+ Date date = new Date(InputView.readDate());
+ OutputView.printPreview(date);
+ return date;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private Orders createOrders() {
+ while (true) {
+ try {
+ Orders orders = Convertor.toOrders(InputView.readOrder());
+ OutputView.printOrders(orders);
+ return orders;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private int createTotalOrderAmount(Orders orders) {
+ int totalOrderAmount = orders.calculateTotalAmount();
+ OutputView.printTotalOrderAmount(totalOrderAmount);
+ return totalOrderAmount;
+ }
+
+ private Benefits createBenefits(Orders orders, Date date) {
+ Benefits benefits = EventManager.toBenefits(orders, date);
+ OutputView.printBenefitDetail(benefits);
+ return benefits;
+ }
+
+ private int createTotalBenefitAmount(Benefits benefits) {
+ int totalBenefitAmount = benefits.calculateTotalBenefitAmount();
+ OutputView.printTotalBenefitAmount(totalBenefitAmount);
+ return totalBenefitAmount;
+ }
+
+ private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) {
+ int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount);
+ OutputView.printPaymentAmount(paymentAmount);
+ }
+
+ private void createBadge(Benefits benefits, int totalBenefitAmount) {
+ Badge badge = benefits.toEventBadge(totalBenefitAmount);
+ OutputView.printEventBadge(badge);
+ }
+} | Java | ๋ชจ๋ ๊ฒ์ฆ์ InputView์์ ๋ฐ๋๋ค๋ฉด ์น์ฐ๋ ๋ง์๋๋ก ํ๋๊ฒ ๋ง๋ค๊ณ ์๊ฐํฉ๋๋ค! ํ์ง๋ง ๋ ์ง๊ฐ 1 - 31์ผ ์ค ํ๋์ฌ์ผ ํ๋ค๋ ๊ฒ์ ๋ํ ๊ฒ์ฆ์ VIew๊ฐ ์๋๋ผ Domain ์์ ์ด๋ค์ง๊ธฐ ๋๋ฌธ์ ๊ฒฐ๊ณผ์ ์ผ๋ก View์ Domain ์์ชฝ์์ ๊ฒ์ฆ์ด ์ด๋ค์ ธ์ผ ํฉ๋๋ค. ๋ฐ๋ผ์ ์์ชฝ์ ํต๋ก ์ญํ ์ ํ๋ controller์์ ์งํํ๋ ๊ฒ์ด ๋ง๋ค๊ณ ํ๋จํ์ต๋๋ค~ |
@@ -0,0 +1,76 @@
+package christmas.controller;
+
+import christmas.domain.Badge;
+import christmas.domain.Benefits;
+import christmas.domain.Convertor;
+import christmas.domain.Date;
+import christmas.domain.EventManager;
+import christmas.domain.Orders;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+public class EventController {
+ public void start() {
+ OutputView.printWelcome();
+ Date date = createDate();
+ Orders orders = createOrders();
+ int totalOrderAmount = createTotalOrderAmount(orders);
+ OutputView.printGift(orders.hasGift());
+ Benefits benefits = createBenefits(orders, date);
+ int totalBenefitAmount = createTotalBenefitAmount(benefits);
+ createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount);
+ createBadge(benefits, totalBenefitAmount);
+ }
+
+ private Date createDate() {
+ while (true) {
+ try {
+ Date date = new Date(InputView.readDate());
+ OutputView.printPreview(date);
+ return date;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private Orders createOrders() {
+ while (true) {
+ try {
+ Orders orders = Convertor.toOrders(InputView.readOrder());
+ OutputView.printOrders(orders);
+ return orders;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private int createTotalOrderAmount(Orders orders) {
+ int totalOrderAmount = orders.calculateTotalAmount();
+ OutputView.printTotalOrderAmount(totalOrderAmount);
+ return totalOrderAmount;
+ }
+
+ private Benefits createBenefits(Orders orders, Date date) {
+ Benefits benefits = EventManager.toBenefits(orders, date);
+ OutputView.printBenefitDetail(benefits);
+ return benefits;
+ }
+
+ private int createTotalBenefitAmount(Benefits benefits) {
+ int totalBenefitAmount = benefits.calculateTotalBenefitAmount();
+ OutputView.printTotalBenefitAmount(totalBenefitAmount);
+ return totalBenefitAmount;
+ }
+
+ private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) {
+ int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount);
+ OutputView.printPaymentAmount(paymentAmount);
+ }
+
+ private void createBadge(Benefits benefits, int totalBenefitAmount) {
+ Badge badge = benefits.toEventBadge(totalBenefitAmount);
+ OutputView.printEventBadge(badge);
+ }
+} | Java | e๋ผ๊ณ ์ฌ์ฉํ๋ ๊ฒ์ ์๋ฐ๋ฅผ ์ฌ์ฉํ๋ ์ฌ๋๋ค๋ผ๋ฆฌ์ ์ฝ์์ธ๊ฑฐ ๊ฐ์๋ฐ ์ด๋ถ๋ถ์ ํ๋ฒ ๋ํ๋ฅผ ๋๋ ๋ณด๋ฉด ์ข์๊ฑฐ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,76 @@
+package christmas.controller;
+
+import christmas.domain.Badge;
+import christmas.domain.Benefits;
+import christmas.domain.Convertor;
+import christmas.domain.Date;
+import christmas.domain.EventManager;
+import christmas.domain.Orders;
+import christmas.view.InputView;
+import christmas.view.OutputView;
+
+public class EventController {
+ public void start() {
+ OutputView.printWelcome();
+ Date date = createDate();
+ Orders orders = createOrders();
+ int totalOrderAmount = createTotalOrderAmount(orders);
+ OutputView.printGift(orders.hasGift());
+ Benefits benefits = createBenefits(orders, date);
+ int totalBenefitAmount = createTotalBenefitAmount(benefits);
+ createPaymentAmount(orders, totalOrderAmount, totalBenefitAmount);
+ createBadge(benefits, totalBenefitAmount);
+ }
+
+ private Date createDate() {
+ while (true) {
+ try {
+ Date date = new Date(InputView.readDate());
+ OutputView.printPreview(date);
+ return date;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private Orders createOrders() {
+ while (true) {
+ try {
+ Orders orders = Convertor.toOrders(InputView.readOrder());
+ OutputView.printOrders(orders);
+ return orders;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e);
+ }
+ }
+ }
+
+ private int createTotalOrderAmount(Orders orders) {
+ int totalOrderAmount = orders.calculateTotalAmount();
+ OutputView.printTotalOrderAmount(totalOrderAmount);
+ return totalOrderAmount;
+ }
+
+ private Benefits createBenefits(Orders orders, Date date) {
+ Benefits benefits = EventManager.toBenefits(orders, date);
+ OutputView.printBenefitDetail(benefits);
+ return benefits;
+ }
+
+ private int createTotalBenefitAmount(Benefits benefits) {
+ int totalBenefitAmount = benefits.calculateTotalBenefitAmount();
+ OutputView.printTotalBenefitAmount(totalBenefitAmount);
+ return totalBenefitAmount;
+ }
+
+ private void createPaymentAmount(Orders orders, int totalOrderAmount, int totalBenefitAmount) {
+ int paymentAmount = orders.calculatePaymentAmount(totalOrderAmount, totalBenefitAmount);
+ OutputView.printPaymentAmount(paymentAmount);
+ }
+
+ private void createBadge(Benefits benefits, int totalBenefitAmount) {
+ Badge badge = benefits.toEventBadge(totalBenefitAmount);
+ OutputView.printEventBadge(badge);
+ }
+} | Java | > ์ ์ฒด์ ์ผ๋ก create๋ผ๋ ๋ฉ์๋๋ช
์ ์ฌ์ฉํ๋๋ฐ create๊ฐ ๊ฐ์ฒด ์์ฑ์ ๋๋์ ์ฃผ๋๊ฑฐ๊ฐ์์ ๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ๋ฉ์๋๋ช
์ ์์ฑํด๋ณด๋๊ฑด ์ด๋จ๊น์ ?
์ค์ ๋ก ํด๋น ๋ฉ์๋๊ฐ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ๋ฆฌํดํ๋ ๊ธฐ๋ฅ์ ํ๊ณ ์์ด์ create ๋ผ๊ณ ๋ช
๋ช
ํ๋๋ฐ ํน์ ๋ค๋ฅธ ์ข์ ์ด๋ฆ์ด ์์๊น์~? |
@@ -0,0 +1,24 @@
+package christmas.domain;
+
+public enum Badge {
+ STAR("๋ณ", 5_000),
+ TREE("ํธ๋ฆฌ", 10_000),
+ SANTA("์ฐํ", 20_000),
+ NONE("์์", 0);
+
+ private final String type;
+ private final int amount;
+
+ Badge(String type, int amount) {
+ this.type = type;
+ this.amount = amount;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public int getAmount() {
+ return amount;
+ }
+} | Java | ์ธํ
๋ฆฌ์ ์ด ์๋ ์์ฑ๊ธฐ๋ฅ์ผ๋ก ๋ง๋ ๊ฑด๋ฐ ์ด ๋ถ๋ถ์ ๋ํด์๋ ์๊ฐํด๋ณด์ง ๋ชปํ๋ค์ ใ
ใ
private๋ก ์ง์ ํ๋๊ฒ ๋ง๋๊ฑฐ ๊ฐ์ต๋๋ค! |
@@ -41,7 +41,7 @@ public Object checkReviewGroupAccess(ProceedingJoinPoint joinPoint,
private boolean canMemberAccess(ReviewGroup reviewGroup, HttpServletRequest request) {
GitHubMember gitHubMember = sessionManager.getGitHubMember(request);
- return gitHubMember != null && reviewGroup.getMemberId() == gitHubMember.getMemberId();
+ return gitHubMember != null && reviewGroup.isMadeByMember(gitHubMember.getMemberId());
}
private boolean canGuestAccess(ReviewGroup reviewGroup, HttpServletRequest request) { | Java | **๋ฌธ์ ๊ฐ ๋๋ ๋ถ๋ถ์ ์ด ๋ถ๋ถ์ด์์ต๋๋ค.**
ํ์์ด ์ ๊ทผํ๋ ค๋ ๋ฆฌ๋ทฐ ๊ทธ๋ฃน์ด "๋นํ์์ด ๋ง๋ ๋ฆฌ๋ทฐ๊ทธ๋ฃน"์ธ ๊ฒฝ์ฐ,
reviewGroup.getMemberId() ๊ฐ null ์ด๊ธฐ ๋๋ฌธ์
long์ธ gitHubMember.getMemberId() ์์ == ์ฐ์ฐ์ ๋ชปํ๋ฏ๋ก NPE๊ฐ ๋ฐ์ํฉ๋๋ค.
์ด๋ฐ ์ผ์ด ์๊ธฐ์ง ์๊ฒํ๋ ค๋ฉด 1์ฐจ์์ ์ผ๋ก
`gitHubMember != null && (reviewGroup.getMemberId() != null && reviewGroup.getMemberId() == gitHubMember.getMemberId())` ๋ฅผ ํ ์๋ ์์ง๋ง, ๋๋ฌด ๊ฐ๋
์ฑ์ด ๋จ์ด์ ธ์ ํธ์ ๋ฉ์๋ isMadeByMember๋ฅผ ์ถ๊ฐํด์คฌ์ต๋๋ค. |
@@ -57,6 +57,13 @@ void setUp() {
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
}
+ @Test
+ void ์กด์ฌํ์ง_์๋_๋ฆฌ๋ทฐ์_์ ๊ทผํ๋ฉด_NotFound_์์ธ๊ฐ_๋ฐ์ํ๋ค() {
+ // when & then
+ assertThatCode(() -> aopTestClass.testReviewMethod(1L))
+ .isInstanceOf(ReviewNotFoundException.class);
+ }
+
@Nested
class ์ฑ๊ณต์ ์ผ๋ก_๋ฆฌ๋ทฐ์_์ ๊ทผํ _์_์๋ค {
@@ -90,7 +97,7 @@ class ์ฑ๊ณต์ ์ผ๋ก_๋ฆฌ๋ทฐ์_์ ๊ทผํ _์_์๋ค {
}
@Test
- void ๋นํ์์_์์ ์ด_๋ง๋ _๋ฆฌ๋ทฐ_๊ทธ๋ฃน์_์์ฑ๋_๋ฆฌ๋ทฐ์_์ ๊ทผํ _์_์๋ค() {
+ void ๋ฆฌ๋ทฐ_๊ทธ๋ฃน์_์ธ์ฆํ_๋นํ์์_๋ฆฌ๋ทฐ_๊ทธ๋ฃน์_์์ฑ๋_๋ฆฌ๋ทฐ์_์ ๊ทผํ _์_์๋ค() {
// given
ReviewGroup reviewGroup = reviewGroupRepository.save(๋นํ์_๋ฆฌ๋ทฐ_๊ทธ๋ฃน());
Review review = reviewRepository.save(๋นํ์_์์ฑ_๋ฆฌ๋ทฐ(1L, reviewGroup.getId(), List.of()));
@@ -102,13 +109,6 @@ class ์ฑ๊ณต์ ์ผ๋ก_๋ฆฌ๋ทฐ์_์ ๊ทผํ _์_์๋ค {
}
}
- @Test
- void ์กด์ฌํ์ง_์๋_๋ฆฌ๋ทฐ์_์ ๊ทผํ๋ฉด_NotFound_์์ธ๊ฐ_๋ฐ์ํ๋ค() {
- // when & then
- assertThatCode(() -> aopTestClass.testReviewMethod(1L))
- .isInstanceOf(ReviewNotFoundException.class);
- }
-
@Nested
class ์ ํจํ์ง_์์_์ธ์
์ผ๋ก_์ ๊ทผํ๋ฉด_์์ธ๊ฐ_๋ฐ์ํ๋ค {
| Java | ์์ํ๊ฒ ํ
์คํธ ์ฝ๋ ๋ฆฌํฉํฐ๋งํ์ต๋๋ค.
์๊ฐํด๋ณด๋ฉด ๋นํ์์ ์๊ธฐ๊ฐ ๋ง๋ค์ง ์์๋๋ผ๋ ๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ๋ฉด ํต๊ณผ๋๋๊ฑฐ๋ผ "๋ฆฌ๋ทฐ ๊ทธ๋ฃน์ ์ธ์ฆํ" ์ผ๋ก ๋ช
ํํ ์๋ฏธ๋ฅผ ์ ๋ฌํ๋ ค ํ์๋๋ค~ |
@@ -0,0 +1,37 @@
+import CONSTANTS from '../constants/constants.js';
+import ERROR from '../constants/error.js';
+
+class NumbersValidator {
+ static validateNumbers(numbers) {
+ const validators = [
+ this.#validateEmpty,
+ this.#validateNegative,
+ this.#validateNaN,
+ this.#validateLength,
+ this.#validateDuplicated,
+ ];
+ validators.forEach(validator => validator(numbers));
+ }
+
+ static #validateLength(numbers) {
+ if (numbers.length !== CONSTANTS.number.numberSize) throw new Error(ERROR.numbers.length);
+ }
+
+ static #validateNaN(numbers) {
+ if (Number.isNaN(Number(numbers))) throw new Error(ERROR.numbers.notANumber);
+ }
+
+ static #validateNegative(numbers) {
+ if (Number(numbers) < CONSTANTS.number.zero) throw new Error(ERROR.numbers.negative);
+ }
+
+ static #validateDuplicated(numbers) {
+ if (numbers.length !== new Set(numbers).size) throw new Error(ERROR.numbers.duplicated);
+ }
+
+ static #validateEmpty(numbers) {
+ if (numbers.length === CONSTANTS.number.zero) throw new Error(ERROR.numbers.empty);
+ }
+}
+
+export default NumbersValidator; | JavaScript | ๊ฐ ๋งค์๋๋ค์ ๋ฐ๋ก ํธ์ถ์ํค์ง ์๊ณ ๋ฐฐ์ด์ ๋ง๋ค์ด์ forEach ๋ฉ์๋๋ก ํ๋์ฉ ํธ์ถ์ํจ ์ด์ ๊ฐ ๊ถ๊ธํด์! |
@@ -0,0 +1,20 @@
+import { Console } from '@woowacourse/mission-utils';
+import MESSAGE from '../constants/message.js';
+import NumbersValidator from '../validators/NumbersValidator.js';
+import RestartValidator from '../validators/RestartValidator.js';
+
+const InputView = {
+ async readNumbers() {
+ const numbers = await Console.readLineAsync(MESSAGE.read.numbers);
+ NumbersValidator.validateNumbers(numbers);
+ return [...numbers].map(Number);
+ },
+
+ async readRestart() {
+ const restart = await Console.readLineAsync(MESSAGE.read.restart);
+ RestartValidator.validateRestart(restart);
+ return Number(restart);
+ },
+};
+
+export default InputView; | JavaScript | ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ์ถ๋ ฅํ๋ view์ ์ญํ ์ ์๊ฐํ์ ๋ ์ ํจ์ฑ ๊ฒ์ฌ๋จ๊ณ๊ฐ view์ ์๋๊ฒ ๋ง๋์ง ๊ถ๊ธ์ฆ์ด ๋๋๋ฐ ์ด๋ ๊ฒ ์์ฑํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋น :) |
@@ -0,0 +1,42 @@
+import CONSTANTS from '../constants/constants.js';
+import MESSAGE from '../constants/message.js';
+
+class Hint {
+ #numbers;
+
+ #computerNumbers;
+
+ constructor(numbers, computerNumbers) {
+ this.#numbers = numbers;
+ this.#computerNumbers = computerNumbers;
+ }
+
+ calculateStrikeCount() {
+ return this.#numbers.reduce(
+ (count, digit, index) => (digit === this.#computerNumbers[index] ? count + 1 : count),
+ 0,
+ );
+ }
+
+ calculateBallCount(strikeCount) {
+ return (
+ this.#numbers.reduce(
+ (count, digit) => (this.#computerNumbers.includes(digit) ? count + 1 : count),
+ 0,
+ ) - strikeCount
+ );
+ }
+
+ generateHintMessage(strikeCount, ballCount) {
+ const hintMessage = [];
+ if (ballCount !== CONSTANTS.number.zero) hintMessage.push(`${ballCount}${MESSAGE.print.ball}`);
+ if (strikeCount !== CONSTANTS.number.zero)
+ hintMessage.push(`${strikeCount}${MESSAGE.print.strike}`);
+ if (ballCount === CONSTANTS.number.zero && strikeCount === CONSTANTS.number.zero)
+ hintMessage.push(MESSAGE.print.nothing);
+
+ return hintMessage;
+ }
+}
+
+export default Hint; | JavaScript | ์ฒ์์ Hint ํด๋์ค๋ช
์ ์๋ฌธ์ด ๋ค์๋๋ฐ ๊ฒ์์ด๋๊น ์ผ๋ง๋ ๋ง์ท๋์ง ์๋ ค์ฃผ๋ ์ญํ ์ด๋ผ ์ ์ ํ ์ ์๊ฒ ๋ค์! ๐๐ป |
@@ -0,0 +1,20 @@
+import { Console } from '@woowacourse/mission-utils';
+import MESSAGE from '../constants/message.js';
+import NumbersValidator from '../validators/NumbersValidator.js';
+import RestartValidator from '../validators/RestartValidator.js';
+
+const InputView = {
+ async readNumbers() {
+ const numbers = await Console.readLineAsync(MESSAGE.read.numbers);
+ NumbersValidator.validateNumbers(numbers);
+ return [...numbers].map(Number);
+ },
+
+ async readRestart() {
+ const restart = await Console.readLineAsync(MESSAGE.read.restart);
+ RestartValidator.validateRestart(restart);
+ return Number(restart);
+ },
+};
+
+export default InputView; | JavaScript | ์ด ๋ถ๋ถ์ ๋ํด์ ์ ๋ ๊ณ ๋ฏผ์ ๋ง์ด ํด๋ดค๋๋ฐ ํ๋ก ํธ(InputVIew)์ ๋ฐฑ์๋(model)์ ์ญํ ์ด ์๋ค๊ณ ๊ฐ์ ํ์ ๋, view์ model ๋ ๋ค ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ์งํํ๋ ๊ฒ์ด ๋ง๋ค๊ณ ํ๋จํ์ฌ ์์ฑํ๊ฒ ๋์์ต๋๋ค. |
@@ -0,0 +1,37 @@
+import CONSTANTS from '../constants/constants.js';
+import ERROR from '../constants/error.js';
+
+class NumbersValidator {
+ static validateNumbers(numbers) {
+ const validators = [
+ this.#validateEmpty,
+ this.#validateNegative,
+ this.#validateNaN,
+ this.#validateLength,
+ this.#validateDuplicated,
+ ];
+ validators.forEach(validator => validator(numbers));
+ }
+
+ static #validateLength(numbers) {
+ if (numbers.length !== CONSTANTS.number.numberSize) throw new Error(ERROR.numbers.length);
+ }
+
+ static #validateNaN(numbers) {
+ if (Number.isNaN(Number(numbers))) throw new Error(ERROR.numbers.notANumber);
+ }
+
+ static #validateNegative(numbers) {
+ if (Number(numbers) < CONSTANTS.number.zero) throw new Error(ERROR.numbers.negative);
+ }
+
+ static #validateDuplicated(numbers) {
+ if (numbers.length !== new Set(numbers).size) throw new Error(ERROR.numbers.duplicated);
+ }
+
+ static #validateEmpty(numbers) {
+ if (numbers.length === CONSTANTS.number.zero) throw new Error(ERROR.numbers.empty);
+ }
+}
+
+export default NumbersValidator; | JavaScript | ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ๋ชจ๋ ์ํํ๊ธฐ ์ํด์ ํ๋์ ๋ฉ์๋๋ฅผ ํธ์ถํจ์ผ๋ก์ ๊ฐ ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ์ํ๋๋๋ก ์์ฑํด๋ณด์์ต๋๋ค! |
@@ -0,0 +1,38 @@
+# ๊ธฐ๋ฅ ๊ตฌํ ์ฒดํฌ๋ฆฌ์คํธ
+
+## ๊ธฐ๋ฅ ๋ชฉ๋ก
+
+### ์ฌ์ฉ์์ ์
๋ ฅ
+
+- [x] ์ธ ์๋ฆฌ ์ซ์๋ฅผ ์
๋ ฅํ๋ค.
+ - [x] ์ซ์๊ฐ ์๋ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ (1 ~ 9) - ์ ๊ท ํํ์
+ - [x] ์ซ์๊ฐ 3๊ฐ๊ฐ ์๋ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ
+ - [x] ์ค๋ณต๋๋ ์ซ์๊ฐ ์กด์ฌํ๋ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ
+
+### ์ปดํจํฐ์ ์ ์์ฑ
+
+- [x] 3๊ฐ์ ์ซ์ ์์ฑ (์ง์ ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ด์ฉ)
+ - [x] ์ค๋ณต๋ ์ซ์๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ ์ฌ์์ฑ
+
+### ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ์์ ์ปดํจํฐ๊ฐ ์์ฑํ ์ ๋น๊ต
+
+- [x] ๊ฐ ์๋ฆฌ ์ซ์ ๋น๊ตํ๊ธฐ
+ - [x] ์ซ์๊ฐ ๋๊ฐ๋ค๋ฉด ์คํธ๋ผ์ดํฌ +1
+ - [x] ๋ค๋ฅธ ์์น์ ์๋ค๋ฉด ๋ณผ +1
+
+### ๊ฒฐ๊ณผ
+
+- [x] ๋ณผ, ์คํธ๋ผ์ดํฌ, ๋ซ์ฑ ์ถ๋ ฅ
+- [x] 3์คํธ๋ผ์ดํฌ์ธ ๊ฒฝ์ฐ ๊ฒ์ ์ข
๋ฃ
+
+### ์ฌ์์ ํ์ธ
+
+- [x] ์ฌ์์ ์
๋ ฅ ์ซ์๋ฅผ ์
๋ ฅํ๋ค.
+ - [x] ์ซ์๊ฐ ์๋ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ
+ - [x] 1(์ฌ์์), 2(์ข
๋ฃ)๊ฐ ์๋ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ
+- [x] 1์ ์
๋ ฅ๋ฐ์ ๊ฒฝ์ฐ ์ฌ์์
+- [x] 2๋ฅผ ์
๋ ฅ๋ฐ์ ๊ฒฝ์ฐ ์ข
๋ฃ
+
+## ๋ชฉํ
+
+1. ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด์ค๋ฝ๊ฒ ์ฌ์ฉํ์!!!
\ No newline at end of file | Unknown | ๋ฆฌ๋ทฐ ์์์ ์์ ๋ฆฌ๋๋ฏธ์ ๊ฐ ํด๋์ค๋ณ ์ญํ ์ ๋ํด ๊ธฐ์ฌํด์ฃผ์๋ฉด ์ฝ๋ ํ์
์ ๋ ๋์์ด ๋ ๊ฑฐ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,58 @@
+package baseball.controller;
+
+import baseball.domain.Computer;
+import baseball.domain.Referee;
+import baseball.domain.Result;
+import baseball.util.GameProgress;
+import baseball.view.InputView;
+import baseball.view.OutputView;
+import java.util.List;
+
+public class GameController {
+
+ private final Computer computer;
+ private final Referee referee;
+ private GameProgress gameProgress;
+
+ public GameController() {
+ this.computer = new Computer();
+ this.referee = new Referee();
+ this.gameProgress = GameProgress.START;
+ }
+
+ public void play() {
+ playOrEnd();
+ }
+
+ private void playOrEnd() {
+ OutputView.printStart();
+ while (!GameProgress.isQuit(gameProgress)) {
+ List<Integer> computerNumbers = computer.generateRandomNumbers();
+ playUntilThreeStrikes(computerNumbers);
+ selectRetry();
+ }
+ }
+
+ private void selectRetry() {
+ OutputView.printRetryOrEnd();
+ gameProgress = GameProgress.judgeEnd(InputView.readNumber());
+ }
+
+ private void playUntilThreeStrikes(List<Integer> computerNumbers) {
+ while (!GameProgress.isQuit(gameProgress)) {
+ OutputView.printInputNumbers();
+ Result result = referee.judge(InputView.readNumbers(), computerNumbers);
+ OutputView.printResult(result.announceResult());
+ judgeRetry(result);
+ }
+ }
+
+ private void judgeRetry(Result result) {
+ if (!result.isThreeStrikes()) {
+ gameProgress = GameProgress.RETRY;
+ return;
+ }
+ OutputView.printGameEnd();
+ gameProgress = GameProgress.QUIT;
+ }
+} | Java | ํ๋ ์ด ๋ฉ์๋๊ฐ ๋จ์ํ playOrEnd๋ฅผ ํธ์ถํ๊ณ ์๋๋ฐ play๋ playOrEnd์ค ํ๋๋ฅผ ์์ ๊ณ ํฉ์ณ๋ ๋ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,58 @@
+package baseball.controller;
+
+import baseball.domain.Computer;
+import baseball.domain.Referee;
+import baseball.domain.Result;
+import baseball.util.GameProgress;
+import baseball.view.InputView;
+import baseball.view.OutputView;
+import java.util.List;
+
+public class GameController {
+
+ private final Computer computer;
+ private final Referee referee;
+ private GameProgress gameProgress;
+
+ public GameController() {
+ this.computer = new Computer();
+ this.referee = new Referee();
+ this.gameProgress = GameProgress.START;
+ }
+
+ public void play() {
+ playOrEnd();
+ }
+
+ private void playOrEnd() {
+ OutputView.printStart();
+ while (!GameProgress.isQuit(gameProgress)) {
+ List<Integer> computerNumbers = computer.generateRandomNumbers();
+ playUntilThreeStrikes(computerNumbers);
+ selectRetry();
+ }
+ }
+
+ private void selectRetry() {
+ OutputView.printRetryOrEnd();
+ gameProgress = GameProgress.judgeEnd(InputView.readNumber());
+ }
+
+ private void playUntilThreeStrikes(List<Integer> computerNumbers) {
+ while (!GameProgress.isQuit(gameProgress)) {
+ OutputView.printInputNumbers();
+ Result result = referee.judge(InputView.readNumbers(), computerNumbers);
+ OutputView.printResult(result.announceResult());
+ judgeRetry(result);
+ }
+ }
+
+ private void judgeRetry(Result result) {
+ if (!result.isThreeStrikes()) {
+ gameProgress = GameProgress.RETRY;
+ return;
+ }
+ OutputView.printGameEnd();
+ gameProgress = GameProgress.QUIT;
+ }
+} | Java | while์ ์กฐ๊ฑด๋ฌธ์ ๊ธ์ ๋ฌธ์ผ๋ก ํ๋ ๊ฒ์ด ๊ฐ๋
์ฑ์ ๋ ๋์์ด ๋๋ค๊ณ ํ๋๋ผ๊ตฌ์.
```suggestion
while (GameProgress.isRetry(gameProgress)) {
```
์ด๋ ๊ฒ ๋ฐ๊ฟ๋ณด์๋ ๊ฒ๋ ํ๋ฒ ์ ์๋๋ ค๋ด
๋๋ค! |
@@ -0,0 +1,23 @@
+package baseball.domain;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import java.util.ArrayList;
+import java.util.List;
+
+public class Computer {
+
+ private static final int NUMBERS_LENGTH = 3;
+ private static final int START_RANGE_NUMBER = 1;
+ private static final int END_RANGE_NUMBER = 9;
+
+ public List<Integer> generateRandomNumbers() {
+ List<Integer> computer = new ArrayList<>();
+ while (computer.size() < NUMBERS_LENGTH) {
+ int randomNumber = Randoms.pickNumberInRange(START_RANGE_NUMBER, END_RANGE_NUMBER);
+ if (!computer.contains(randomNumber)) {
+ computer.add(randomNumber);
+ }
+ }
+ return computer;
+ }
+} | Java | ์ ์ ๋ฉ์๋๋ฅผ ์ ์ธํ์ ๊ธฐ์ค์ ์ดํด๋ณด๋ฉด Computer๋ ์ ํธ ๊ธฐ๋ฅ + ์ํ๊ฐ์ ๊ฐ์ง์ง ์๊ธฐ ๋๋ฌธ์ ์ ์ ๋ฉ์๋๋ก ์ ์ธํด๋ ๋๋ค๊ณ ์๊ฐํ๋๋ฐ ์ด๋ ์ ๊ฐ์? |
@@ -0,0 +1,17 @@
+package baseball.domain;
+
+import java.util.List;
+
+public class Referee {
+
+ public Result judge(List<Integer> userNumbers, List<Integer> computerNumbers) {
+ Result result = new Result();
+ for (int i = 0; i < userNumbers.size(); i++) {
+ if (result.isStrike(userNumbers, computerNumbers, i)) {
+ continue;
+ }
+ result.countBall(userNumbers, computerNumbers, i);
+ }
+ return result;
+ }
+} | Java | ์ปดํจํฐ์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ ํผ๋ฆฌ๋ ์ ์ ๋ฉ์๋๋ก ์ ์ธํด๋ ๊ด์ฐฎ์ง ์์๊น ํ๋ ์๊ฐ์
๋๋ค! |
@@ -0,0 +1,19 @@
+package baseball.util;
+
+public enum GameProgress {
+
+ START, RETRY, QUIT;
+
+ private static final int RETRY_NUMBER = 1;
+
+ public static boolean isQuit(GameProgress gameProgress) {
+ return gameProgress == QUIT;
+ }
+
+ public static GameProgress judgeEnd(int number) {
+ if (number == RETRY_NUMBER) {
+ return START;
+ }
+ return QUIT;
+ }
+} | Java | ํ๋ก๊ทธ๋จ์ ํ์ฅ์ฑ์ ๊ณ ๋ คํ์
์ enum์ผ๋ก ๋ง๋์ ๊ฑฐ ๋ง์ผ์ค๊น์? ์ ๋ ์ฒ์์ ์ข
๋ฃ ๋ฉ๋ด๋ฅผ enum์ผ๋ก ๋ง๋๋ ค๊ณ ํ๋ค๊ฐ ๊ฒฝ์ฐ์ ์๊ฐ 2๊ฐ์ง์ฌ์ class๋ก ๋ง๋ค์์ต๋๋ค. ๋ง์ฝ ๊ฒฝ์ฐ์ ์๊ฐ ๋ ๋ง์๋ค๋ฉด ์ ๋ enum์ผ๋ก ๋ง๋ค์์ ๊ฒ ๊ฐ๋ค์! |
@@ -0,0 +1,19 @@
+package baseball.util;
+
+public enum GameProgress {
+
+ START, RETRY, QUIT;
+
+ private static final int RETRY_NUMBER = 1;
+
+ public static boolean isQuit(GameProgress gameProgress) {
+ return gameProgress == QUIT;
+ }
+
+ public static GameProgress judgeEnd(int number) {
+ if (number == RETRY_NUMBER) {
+ return START;
+ }
+ return QUIT;
+ }
+} | Java | ๋ฐ๋ก ์์๋ฅผ ์ ์ธํ์๊ธฐ ๋ณด๋ค RETRY(1), QUIT(2); ์ด๋ฐ ์์ผ๋ก ๊ฐ์ ์ง์ ํด์ฃผ์๋๊ฑด ์ด๋ ์ ์ง ์ ์๋๋ ค๋ด
๋๋ค! |
@@ -0,0 +1,34 @@
+package baseball.view;
+
+import baseball.util.Validator;
+import camp.nextstep.edu.missionutils.Console;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public class InputView {
+
+ public static final String DELIMITER = "";
+
+ public static List<Integer> readNumbers() {
+ String input = input();
+ Validator.validateNumbersPattern(input);
+
+ List<Integer> numbers = Stream.of(input.split(DELIMITER))
+ .map(Integer::parseInt)
+ .distinct()
+ .collect(Collectors.toList());
+ Validator.validateDuplication(numbers);
+ return numbers;
+ }
+
+ public static int readNumber() {
+ String input = input();
+ Validator.validateNumberPattern(input);
+ return Integer.parseInt(input);
+ }
+
+ private static String input() {
+ return Console.readLine();
+ }
+} | Java | view๊ฐ inputd์ domain์์ ์ฌ์ฉ๋ ํํ๋ก ๊ฐ๊ณตํด์ ๋๊ฒจ์ฃผ๋ ๊ฒ์ด ๋ง๋ ๊ฑธ๊น์??? ์ ๋ ํ๋ฆฌ์ฝ์ค๋ฅผ ํ๋ฉด์ ํญ์ ์๊ตฌ์ฌ ๊ฐ์ ธ์๋ ๋ถ๋ถ์ด๋ผ์ ๋ ์ด์ฑ์นด ๋ค์ ๋ง๋ค๊ธฐ์์๋ ์๋ํ์ ๋ํ String ๊ฐ์ผ๋ก๋ง ๋๊ฒจ์คฌ์๋๋ฐ์. ์ด ๋ถ๋ถ ๋ชฉ์์ผ์ ๊ผญ ์๊ธฐ ๋๋ ๋ดค์ผ๋ฉด ์ข๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,65 @@
+package baseball.domain;
+
+import java.util.List;
+
+public class Result {
+
+ private static final int DEFAULT_NUMBER = 0;
+ private static final int END_NUMBER = 3;
+
+ private int ball;
+ private int strike;
+
+ public Result() {
+ this.ball = DEFAULT_NUMBER;
+ this.strike = DEFAULT_NUMBER;
+ }
+
+ public boolean isStrike(List<Integer> userNumbers, List<Integer> computerNumbers, int index) {
+ if (userNumbers.get(index).equals(computerNumbers.get(index))) {
+ strike++;
+ return true;
+ }
+ return false;
+ }
+
+ public void countBall(List<Integer> userNumbers, List<Integer> computerNumbers, int index) {
+ if (computerNumbers.contains(userNumbers.get(index))) {
+ ball++;
+ }
+ }
+
+ public boolean isThreeStrikes() {
+ return strike == END_NUMBER;
+ }
+
+ public String announceResult() {
+ if (ball == DEFAULT_NUMBER && strike == DEFAULT_NUMBER) {
+ return Message.NOTHING.getMessage();
+ }
+ if (ball == DEFAULT_NUMBER) {
+ return String.format(Message.STRIKE.getMessage(), strike);
+ }
+ if (strike == DEFAULT_NUMBER) {
+ return String.format(Message.BALL.getMessage(), ball);
+ }
+ return String.format(Message.BALL_AND_STRIKE.getMessage(), ball, strike);
+ }
+
+ private enum Message {
+ NOTHING("๋ซ์ฑ"),
+ BALL("%d๋ณผ"),
+ STRIKE("%d์คํธ๋ผ์ดํฌ"),
+ BALL_AND_STRIKE("%d๋ณผ %d์คํธ๋ผ์ดํฌ");
+
+ private final String message;
+
+ Message(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+ }
+} | Java | ํด๋น ์ถ๋ ฅ์ ๋ํ ์ฑ
์์ OutputView์ ๋๊ฒจ์ฃผ์๋๊ฑด ์ด๋ ์ ๊ฐ์? |
@@ -0,0 +1,32 @@
+package baseball.util;
+
+import java.util.List;
+import java.util.regex.Pattern;
+
+public class Validator {
+
+ private static final String DUPLICATION_MESSAGE = "์ค๋ณต๋ ์ซ์๊ฐ ์กด์ฌํฉ๋๋ค. ๋ค์ ์
๋ ฅํด์ฃผ์ธ์.";
+ private static final String NUMBERS_PATTERN_MESSAGE = "1๋ถํฐ 9๊น์ง์ ์ซ์ ์ค 3๊ฐ์ง ์ซ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.";
+ private static final String NUMBER_PATTERN_MESSAGE = "์ฌ์์์ ์ํ๋ฉด 1, ์ข
๋ฃ๋ฅผ ์ํ๋ฉด 2๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.";
+ private static final Pattern REGEX_NUMBERS = Pattern.compile("^[1-9]{3}$");
+ private static final Pattern REGEX_NUMBER = Pattern.compile("^[1-2]{1}$");
+ private static final int NUMBERS_LENGTH = 3;
+
+ public static void validateDuplication(List<Integer> input) {
+ if (input.size() < NUMBERS_LENGTH) {
+ throw new IllegalArgumentException(DUPLICATION_MESSAGE);
+ }
+ }
+
+ public static void validateNumbersPattern(String input) {
+ if (!REGEX_NUMBERS.matcher(input).matches()) {
+ throw new IllegalArgumentException(NUMBERS_PATTERN_MESSAGE);
+ }
+ }
+
+ public static void validateNumberPattern(String input) {
+ if (!REGEX_NUMBER.matcher(input).matches()) {
+ throw new IllegalArgumentException(NUMBER_PATTERN_MESSAGE);
+ }
+ }
+} | Java | ์ ๊ท์ ํํ์ด ๋งค์ฐ ๊น๋ํ๋ค์! ๋ฐฐ์ฐ๊ณ ๊ฐ๋๋ค. |
@@ -0,0 +1,58 @@
+package baseball.controller;
+
+import baseball.domain.Computer;
+import baseball.domain.Referee;
+import baseball.domain.Result;
+import baseball.util.GameProgress;
+import baseball.view.InputView;
+import baseball.view.OutputView;
+import java.util.List;
+
+public class GameController {
+
+ private final Computer computer;
+ private final Referee referee;
+ private GameProgress gameProgress;
+
+ public GameController() {
+ this.computer = new Computer();
+ this.referee = new Referee();
+ this.gameProgress = GameProgress.START;
+ }
+
+ public void play() {
+ playOrEnd();
+ }
+
+ private void playOrEnd() {
+ OutputView.printStart();
+ while (!GameProgress.isQuit(gameProgress)) {
+ List<Integer> computerNumbers = computer.generateRandomNumbers();
+ playUntilThreeStrikes(computerNumbers);
+ selectRetry();
+ }
+ }
+
+ private void selectRetry() {
+ OutputView.printRetryOrEnd();
+ gameProgress = GameProgress.judgeEnd(InputView.readNumber());
+ }
+
+ private void playUntilThreeStrikes(List<Integer> computerNumbers) {
+ while (!GameProgress.isQuit(gameProgress)) {
+ OutputView.printInputNumbers();
+ Result result = referee.judge(InputView.readNumbers(), computerNumbers);
+ OutputView.printResult(result.announceResult());
+ judgeRetry(result);
+ }
+ }
+
+ private void judgeRetry(Result result) {
+ if (!result.isThreeStrikes()) {
+ gameProgress = GameProgress.RETRY;
+ return;
+ }
+ OutputView.printGameEnd();
+ gameProgress = GameProgress.QUIT;
+ }
+} | Java | ํจ์๋ช
์ ThreeStrike์ฒ๋ผ ์ฐ์น ์กฐ๊ฑด์ ๋ํด ์ง์ ์ ์ผ๋ก ๋ํ๋ด๋ ๊ฒ๋ณด๋ค playUntilWin ๊ฐ์ ๋ฐฉ์์ผ๋ก ์์ฑํ๋๊ฑด ์ด๋จ๊น์?? |
@@ -0,0 +1,65 @@
+package baseball.domain;
+
+import java.util.List;
+
+public class Result {
+
+ private static final int DEFAULT_NUMBER = 0;
+ private static final int END_NUMBER = 3;
+
+ private int ball;
+ private int strike;
+
+ public Result() {
+ this.ball = DEFAULT_NUMBER;
+ this.strike = DEFAULT_NUMBER;
+ }
+
+ public boolean isStrike(List<Integer> userNumbers, List<Integer> computerNumbers, int index) {
+ if (userNumbers.get(index).equals(computerNumbers.get(index))) {
+ strike++;
+ return true;
+ }
+ return false;
+ }
+
+ public void countBall(List<Integer> userNumbers, List<Integer> computerNumbers, int index) {
+ if (computerNumbers.contains(userNumbers.get(index))) {
+ ball++;
+ }
+ }
+
+ public boolean isThreeStrikes() {
+ return strike == END_NUMBER;
+ }
+
+ public String announceResult() {
+ if (ball == DEFAULT_NUMBER && strike == DEFAULT_NUMBER) {
+ return Message.NOTHING.getMessage();
+ }
+ if (ball == DEFAULT_NUMBER) {
+ return String.format(Message.STRIKE.getMessage(), strike);
+ }
+ if (strike == DEFAULT_NUMBER) {
+ return String.format(Message.BALL.getMessage(), ball);
+ }
+ return String.format(Message.BALL_AND_STRIKE.getMessage(), ball, strike);
+ }
+
+ private enum Message {
+ NOTHING("๋ซ์ฑ"),
+ BALL("%d๋ณผ"),
+ STRIKE("%d์คํธ๋ผ์ดํฌ"),
+ BALL_AND_STRIKE("%d๋ณผ %d์คํธ๋ผ์ดํฌ");
+
+ private final String message;
+
+ Message(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+ }
+} | Java | isStrike์ ํต์ผํ์ฌ isBall๋ก ์ํ ์ด์ ๊ฐ ๋ฐ๋ก ์๋์ !? |
@@ -0,0 +1,65 @@
+package baseball.domain;
+
+import java.util.List;
+
+public class Result {
+
+ private static final int DEFAULT_NUMBER = 0;
+ private static final int END_NUMBER = 3;
+
+ private int ball;
+ private int strike;
+
+ public Result() {
+ this.ball = DEFAULT_NUMBER;
+ this.strike = DEFAULT_NUMBER;
+ }
+
+ public boolean isStrike(List<Integer> userNumbers, List<Integer> computerNumbers, int index) {
+ if (userNumbers.get(index).equals(computerNumbers.get(index))) {
+ strike++;
+ return true;
+ }
+ return false;
+ }
+
+ public void countBall(List<Integer> userNumbers, List<Integer> computerNumbers, int index) {
+ if (computerNumbers.contains(userNumbers.get(index))) {
+ ball++;
+ }
+ }
+
+ public boolean isThreeStrikes() {
+ return strike == END_NUMBER;
+ }
+
+ public String announceResult() {
+ if (ball == DEFAULT_NUMBER && strike == DEFAULT_NUMBER) {
+ return Message.NOTHING.getMessage();
+ }
+ if (ball == DEFAULT_NUMBER) {
+ return String.format(Message.STRIKE.getMessage(), strike);
+ }
+ if (strike == DEFAULT_NUMBER) {
+ return String.format(Message.BALL.getMessage(), ball);
+ }
+ return String.format(Message.BALL_AND_STRIKE.getMessage(), ball, strike);
+ }
+
+ private enum Message {
+ NOTHING("๋ซ์ฑ"),
+ BALL("%d๋ณผ"),
+ STRIKE("%d์คํธ๋ผ์ดํฌ"),
+ BALL_AND_STRIKE("%d๋ณผ %d์คํธ๋ผ์ดํฌ");
+
+ private final String message;
+
+ Message(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+ }
+} | Java | ์ค .. ์ด๋ฐฉ์์ผ๋ก ํ๋๊ฑฐ ์ข์๊ฑฐ ๊ฐ๋ค์
๊ทผ๋ฐ ์๋ ์กฐ๊ฑด์๋ else if ๋ก ๋ํ๋ด๋ ์ข์๊ฑฐ๊ฐ์์ ! |
@@ -0,0 +1,73 @@
+package com.eureka.spartaonetoone.review.application;
+
+import com.eureka.spartaonetoone.review.application.dtos.request.ReviewRequestDto;
+import com.eureka.spartaonetoone.review.application.dtos.response.ReviewResponseDto;
+import com.eureka.spartaonetoone.review.application.exception.ReviewException;
+import com.eureka.spartaonetoone.review.domain.Review;
+import com.eureka.spartaonetoone.review.domain.repository.ReviewRepository;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.data.domain.Pageable;
+import org.springframework.stereotype.Service;
+import org.springframework.validation.annotation.Validated;
+
+import java.util.List;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import jakarta.validation.Valid;
+
+@Service
+@Validated
+public class ReviewService {
+
+ private final ReviewRepository reviewRepository;
+
+ public ReviewService(ReviewRepository reviewRepository) {
+ this.reviewRepository = reviewRepository;
+ }
+
+ // ๋ฆฌ๋ทฐ ๋ฑ๋ก - DTO๋ฅผ ์ํฐํฐ๋ก ๋ณํํ ํ ์ ์ฅํ๊ณ , ์ ์ฅ๋ ์ํฐํฐ๋ฅผ DTO๋ก ๋ณํํ์ฌ ๋ฐํ
+ public ReviewResponseDto createReview(@Valid ReviewRequestDto dto) {
+ Review review = dto.createReview();
+ Review savedReview = reviewRepository.save(review);
+ return ReviewResponseDto.from(savedReview);
+ }
+
+ // ํน์ ๋ฆฌ๋ทฐ ์กฐํ - ์ฃผ์ด์ง reviewId๋ก Review ์ํฐํฐ๋ฅผ ์กฐํํ๋ฉฐ, ์กด์ฌํ์ง ์์ผ๋ฉด CustomException์ ๋ฐ์
+ public ReviewResponseDto getReviewById(UUID reviewId) {
+ Review review = reviewRepository.findById(reviewId)
+ .orElseThrow(ReviewException.ReviewNotFoundException::new);
+ return ReviewResponseDto.from(review);
+ }
+
+ // ํน์ ์ฃผ๋ฌธ(orderId)์ ์ํ ๋ฆฌ๋ทฐ ๋ชฉ๋ก ์กฐํ
+ // ReviewRepository์ findByOrderId()๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ์ฃผ๋ฌธ์ ๋ชจ๋ ๋ฆฌ๋ทฐ๋ฅผ ์กฐํ
+ // ๋ฆฌ์คํธ๋ฅผ Pageable๋ก ๊ฐ์ธ์ ๋ฐํ
+ public Page<ReviewResponseDto> getReviewsByOrderId(UUID orderId, Pageable pageable) {
+ List<Review> reviews = reviewRepository.findByOrderId(orderId);
+ List<ReviewResponseDto> dtoList = reviews.stream()
+ .map(ReviewResponseDto::from)
+ .collect(Collectors.toList());
+ return new PageImpl<>(dtoList, pageable, dtoList.size());
+ }
+
+ // ๋ฆฌ๋ทฐ ์์ - ํน์ reviewId์ ํด๋นํ๋ Review ์ํฐํฐ๋ฅผ ์กฐํํ ํ, DTO์ ๊ฐ์ผ๋ก ์
๋ฐ์ดํธํ๊ณ ์ ์ฅ
+ public ReviewResponseDto updateReview(UUID reviewId, @Valid ReviewRequestDto dto) {
+ Review review = reviewRepository.findById(reviewId)
+ .orElseThrow(ReviewException.ReviewNotFoundException::new);
+ review.update(dto.getContent(), dto.getRating(), dto.getImage());
+ Review updatedReview = reviewRepository.save(review);
+ return ReviewResponseDto.from(updatedReview);
+ }
+
+
+ // ๋ฆฌ๋ทฐ ์ญ์ - ํน์ reviewId๋ก Review ์ํฐํฐ๋ฅผ ์กฐํํ ํ, markDeleted()๋ฅผ ํธ์ถ
+ public void deleteReview(UUID reviewId) {
+ Review review = reviewRepository.findById(reviewId)
+ .orElseThrow(ReviewException.ReviewNotFoundException::new);
+ review.markDeleted();
+ reviewRepository.save(review);
+ }
+}
\ No newline at end of file | Java | `@Transactional`์ด ๋น ์ง ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,73 @@
+package com.eureka.spartaonetoone.review.application;
+
+import com.eureka.spartaonetoone.review.application.dtos.request.ReviewRequestDto;
+import com.eureka.spartaonetoone.review.application.dtos.response.ReviewResponseDto;
+import com.eureka.spartaonetoone.review.application.exception.ReviewException;
+import com.eureka.spartaonetoone.review.domain.Review;
+import com.eureka.spartaonetoone.review.domain.repository.ReviewRepository;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.data.domain.Pageable;
+import org.springframework.stereotype.Service;
+import org.springframework.validation.annotation.Validated;
+
+import java.util.List;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import jakarta.validation.Valid;
+
+@Service
+@Validated
+public class ReviewService {
+
+ private final ReviewRepository reviewRepository;
+
+ public ReviewService(ReviewRepository reviewRepository) {
+ this.reviewRepository = reviewRepository;
+ }
+
+ // ๋ฆฌ๋ทฐ ๋ฑ๋ก - DTO๋ฅผ ์ํฐํฐ๋ก ๋ณํํ ํ ์ ์ฅํ๊ณ , ์ ์ฅ๋ ์ํฐํฐ๋ฅผ DTO๋ก ๋ณํํ์ฌ ๋ฐํ
+ public ReviewResponseDto createReview(@Valid ReviewRequestDto dto) {
+ Review review = dto.createReview();
+ Review savedReview = reviewRepository.save(review);
+ return ReviewResponseDto.from(savedReview);
+ }
+
+ // ํน์ ๋ฆฌ๋ทฐ ์กฐํ - ์ฃผ์ด์ง reviewId๋ก Review ์ํฐํฐ๋ฅผ ์กฐํํ๋ฉฐ, ์กด์ฌํ์ง ์์ผ๋ฉด CustomException์ ๋ฐ์
+ public ReviewResponseDto getReviewById(UUID reviewId) {
+ Review review = reviewRepository.findById(reviewId)
+ .orElseThrow(ReviewException.ReviewNotFoundException::new);
+ return ReviewResponseDto.from(review);
+ }
+
+ // ํน์ ์ฃผ๋ฌธ(orderId)์ ์ํ ๋ฆฌ๋ทฐ ๋ชฉ๋ก ์กฐํ
+ // ReviewRepository์ findByOrderId()๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ์ฃผ๋ฌธ์ ๋ชจ๋ ๋ฆฌ๋ทฐ๋ฅผ ์กฐํ
+ // ๋ฆฌ์คํธ๋ฅผ Pageable๋ก ๊ฐ์ธ์ ๋ฐํ
+ public Page<ReviewResponseDto> getReviewsByOrderId(UUID orderId, Pageable pageable) {
+ List<Review> reviews = reviewRepository.findByOrderId(orderId);
+ List<ReviewResponseDto> dtoList = reviews.stream()
+ .map(ReviewResponseDto::from)
+ .collect(Collectors.toList());
+ return new PageImpl<>(dtoList, pageable, dtoList.size());
+ }
+
+ // ๋ฆฌ๋ทฐ ์์ - ํน์ reviewId์ ํด๋นํ๋ Review ์ํฐํฐ๋ฅผ ์กฐํํ ํ, DTO์ ๊ฐ์ผ๋ก ์
๋ฐ์ดํธํ๊ณ ์ ์ฅ
+ public ReviewResponseDto updateReview(UUID reviewId, @Valid ReviewRequestDto dto) {
+ Review review = reviewRepository.findById(reviewId)
+ .orElseThrow(ReviewException.ReviewNotFoundException::new);
+ review.update(dto.getContent(), dto.getRating(), dto.getImage());
+ Review updatedReview = reviewRepository.save(review);
+ return ReviewResponseDto.from(updatedReview);
+ }
+
+
+ // ๋ฆฌ๋ทฐ ์ญ์ - ํน์ reviewId๋ก Review ์ํฐํฐ๋ฅผ ์กฐํํ ํ, markDeleted()๋ฅผ ํธ์ถ
+ public void deleteReview(UUID reviewId) {
+ Review review = reviewRepository.findById(reviewId)
+ .orElseThrow(ReviewException.ReviewNotFoundException::new);
+ review.markDeleted();
+ reviewRepository.save(review);
+ }
+}
\ No newline at end of file | Java | ์ด ๋ถ๋ถ์ด ์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ์๋ง `Review.createReview(dto)`๊ฐ ๋ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,59 @@
+package com.eureka.spartaonetoone.review.presentation;
+
+import com.eureka.spartaonetoone.review.application.ReviewService;
+import com.eureka.spartaonetoone.review.application.dtos.request.ReviewRequestDto;
+import com.eureka.spartaonetoone.review.application.dtos.response.ReviewResponseDto;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.UUID;
+
+@RestController
+@RequestMapping("/api/v1/reviews")
+public class ReviewController {
+
+ private final ReviewService reviewService;
+
+ public ReviewController(ReviewService reviewService) {
+ this.reviewService = reviewService;
+ }
+
+ // ๋ฆฌ๋ทฐ ์์ฑ
+ @PostMapping
+ public ResponseEntity<ReviewResponseDto> createReview(@RequestBody ReviewRequestDto reviewRequestDto) {
+ ReviewResponseDto responseDto = reviewService.createReview(reviewRequestDto);
+ return ResponseEntity.ok(responseDto);
+ }
+
+ // ํน์ ๋ฆฌ๋ทฐ ์กฐํ
+ @GetMapping("/{review_id}")
+ public ResponseEntity<ReviewResponseDto> getReview(@PathVariable("review_id") UUID reviewId) {
+ ReviewResponseDto responseDto = reviewService.getReviewById(reviewId);
+ return ResponseEntity.ok(responseDto);
+ }
+
+ // ํน์ ์ฃผ๋ฌธ(orderId)์ ์ํ ๋ฆฌ๋ทฐ ์ ์ฒด ๋ชฉ๋ก ์กฐํ
+ @GetMapping
+ public ResponseEntity<Page<ReviewResponseDto>> getReviewsByOrderId(@RequestParam("order_id") UUID orderId, Pageable pageable) {
+ Page<ReviewResponseDto> responseDtos = reviewService.getReviewsByOrderId(orderId, pageable);
+ return ResponseEntity.ok(responseDtos);
+ }
+
+ // ๋ฆฌ๋ทฐ ์์
+ @PutMapping("/{review_id}")
+ public ResponseEntity<ReviewResponseDto> updateReview(@PathVariable("review_id") UUID reviewId,
+ @RequestBody ReviewRequestDto reviewRequestDto) {
+ ReviewResponseDto responseDto = reviewService.updateReview(reviewId, reviewRequestDto);
+ return ResponseEntity.ok(responseDto);
+ }
+
+ // ๋ฆฌ๋ทฐ ์ญ์
+ @DeleteMapping("/{review_id}")
+ public ResponseEntity<Void> deleteReview(@PathVariable("review_id") UUID reviewId) {
+ reviewService.deleteReview(reviewId);
+ return ResponseEntity.noContent().build();
+ }
+} | Java | dto ์์ `@Valid` ์ถ๊ฐํด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,59 @@
+package com.eureka.spartaonetoone.review.presentation;
+
+import com.eureka.spartaonetoone.review.application.ReviewService;
+import com.eureka.spartaonetoone.review.application.dtos.request.ReviewRequestDto;
+import com.eureka.spartaonetoone.review.application.dtos.response.ReviewResponseDto;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.UUID;
+
+@RestController
+@RequestMapping("/api/v1/reviews")
+public class ReviewController {
+
+ private final ReviewService reviewService;
+
+ public ReviewController(ReviewService reviewService) {
+ this.reviewService = reviewService;
+ }
+
+ // ๋ฆฌ๋ทฐ ์์ฑ
+ @PostMapping
+ public ResponseEntity<ReviewResponseDto> createReview(@RequestBody ReviewRequestDto reviewRequestDto) {
+ ReviewResponseDto responseDto = reviewService.createReview(reviewRequestDto);
+ return ResponseEntity.ok(responseDto);
+ }
+
+ // ํน์ ๋ฆฌ๋ทฐ ์กฐํ
+ @GetMapping("/{review_id}")
+ public ResponseEntity<ReviewResponseDto> getReview(@PathVariable("review_id") UUID reviewId) {
+ ReviewResponseDto responseDto = reviewService.getReviewById(reviewId);
+ return ResponseEntity.ok(responseDto);
+ }
+
+ // ํน์ ์ฃผ๋ฌธ(orderId)์ ์ํ ๋ฆฌ๋ทฐ ์ ์ฒด ๋ชฉ๋ก ์กฐํ
+ @GetMapping
+ public ResponseEntity<Page<ReviewResponseDto>> getReviewsByOrderId(@RequestParam("order_id") UUID orderId, Pageable pageable) {
+ Page<ReviewResponseDto> responseDtos = reviewService.getReviewsByOrderId(orderId, pageable);
+ return ResponseEntity.ok(responseDtos);
+ }
+
+ // ๋ฆฌ๋ทฐ ์์
+ @PutMapping("/{review_id}")
+ public ResponseEntity<ReviewResponseDto> updateReview(@PathVariable("review_id") UUID reviewId,
+ @RequestBody ReviewRequestDto reviewRequestDto) {
+ ReviewResponseDto responseDto = reviewService.updateReview(reviewId, reviewRequestDto);
+ return ResponseEntity.ok(responseDto);
+ }
+
+ // ๋ฆฌ๋ทฐ ์ญ์
+ @DeleteMapping("/{review_id}")
+ public ResponseEntity<Void> deleteReview(@PathVariable("review_id") UUID reviewId) {
+ reviewService.deleteReview(reviewId);
+ return ResponseEntity.noContent().build();
+ }
+} | Java | ์ฌ๊ธฐ๋ dto ์์ `@Valid` ์ถ๊ฐํด์ฃผ์๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,62 @@
+package com.eureka.spartaonetoone.store.presentation;
+
+import com.eureka.spartaonetoone.store.application.StoreService;
+import com.eureka.spartaonetoone.store.application.dtos.request.StoreRequestDto; //
+import com.eureka.spartaonetoone.store.application.dtos.response.StoreResponseDto;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.UUID;
+
+@RestController
+@RequestMapping("/api/v1/stores")
+public class StoreController {
+
+ private final StoreService storeService;
+
+ // ์์ฑ์ ์ฃผ์
+ public StoreController(StoreService storeService) {
+ this.storeService = storeService;
+ }
+
+ // ๊ฐ๊ฒ ๋ฑ๋ก
+ @PostMapping
+ public ResponseEntity<StoreResponseDto> createStore(@RequestBody StoreRequestDto storeRequestDto) { //
+ StoreResponseDto responseDto = storeService.createStore(storeRequestDto);
+ return ResponseEntity.ok(responseDto);
+ }
+
+ // ํน์ ๊ฐ๊ฒ ์กฐํ
+ @GetMapping("/{store_id}")
+ public ResponseEntity<StoreResponseDto> getStore(@PathVariable(name = "store_id") UUID storeId) {
+ StoreResponseDto responseDto = storeService.getStoreById(storeId);
+ return ResponseEntity.ok(responseDto);
+ } // getStore(@PathVariable(name = "id") UUID id) { // ์ด๋ ๊ฒ ์์ - ์คํ๋ง์ด ์ธ์ํ ์ ์๋๋ก / requesstํจ๋ด? ์ฐพ์๋ณด๊ธฐ / ๋งํฌ ์ฐธ๊ณ
+ // https://teamsparta.notion.site/RequestParam-PathVariable-Autowired-335787e51fac41418598434fa7f7ed51
+
+
+ // ์ ์ฒด ๊ฐ๊ฒ ๋ชฉ๋ก ์กฐํ
+ @GetMapping
+ public ResponseEntity<Page<StoreResponseDto>> getAllStores(Pageable pageable) {
+ Page<StoreResponseDto> responseDtos = storeService.getAllStores(pageable);
+ return ResponseEntity.ok(responseDtos);
+ }
+
+ // ๊ฐ๊ฒ ์์
+ @PutMapping("/{store_id}")
+ public ResponseEntity<StoreResponseDto> updateStore(@PathVariable(name = "store_id") UUID storeId,
+ @RequestBody StoreRequestDto storeRequestDto) {
+ StoreResponseDto responseDto = storeService.updateStore(storeId, storeRequestDto);
+ return ResponseEntity.ok(responseDto);
+ }
+
+ // ๊ฐ๊ฒ ์ญ์
+ @DeleteMapping("/{store_id}")
+ public ResponseEntity<Void> deleteStore(@PathVariable(name = "store_id") UUID storeId) {
+ storeService.deleteStore(storeId);
+ return ResponseEntity.noContent().build();
+ }
+} | Java | ์ฃผ์ ๋ถ๋ถ๋ง ๋์ค์ ์ ๊ฑฐํด์ฃผ์
๋ ๋ ๊ฒ ๊ฐ์์ |
@@ -29,7 +29,7 @@ public void initialize() {
beanFactory.initialize();
}
- public Collection<Object> controllers() {
+ public Collection<Object> getControllers() {
return beanFactory.beansAnnotatedWith(Controller.class);
}
} | Java | ํผ๋๋ฐฑ ๋ฐ์ ์ ํด์ฃผ์
จ๋ค์ ๐ |
@@ -1,6 +1,5 @@
package next.controller;
-import core.annotation.Inject;
import core.annotation.web.Controller;
import core.annotation.web.RequestMapping;
import core.annotation.web.RequestMethod;
@@ -22,14 +21,8 @@
public class ApiQnaController extends AbstractNewController {
private static final Logger logger = LoggerFactory.getLogger( ApiQnaController.class );
- private final QuestionDao questionDao;
- private final AnswerDao answerDao;
-
- @Inject
- public ApiQnaController(QuestionDao questionDao, AnswerDao answerDao) {
- this.questionDao = questionDao;
- this.answerDao = answerDao;
- }
+ private final QuestionDao questionDao = QuestionDao.getInstance();
+ private final AnswerDao answerDao = AnswerDao.getInstance();
@RequestMapping(value = "/api/qna/list", method = RequestMethod.GET)
public ModelAndView questions(HttpServletRequest req, HttpServletResponse resp) throws Exception { | Java | BeanFactory, getInstantiateSingletons NPE ๋ฐ์ ๋ฌธ์ ๋ก getInstance() ํธ์ถ๋ก ์์ ํ์ ๊ฑฐ ๋ง์๊น์?
์์ธ์ด ๋ฌด์์ด์๋์?
์ด์ ์ฝ๋(์์ฑ์ inject) ๋ฐฉ์์ผ๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์.
MyConfiguration ํด๋์ค์์ ๋ฑ๋กํ DataSource๋ ๋น์ผ๋ก ๋ฑ๋กํ์ผ๋ JdbcTemplate์์ ์ฌ์ฉํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ๊ตฌ์! |
@@ -0,0 +1,86 @@
+package nextstep.subway.application;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import nextstep.subway.domain.Line;
+import nextstep.subway.domain.LineRepository;
+import nextstep.subway.domain.Station;
+import nextstep.subway.domain.StationRepository;
+import nextstep.subway.dto.LineCreateRequest;
+import nextstep.subway.dto.LineResponse;
+import nextstep.subway.dto.LineUpdateRequest;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@Transactional(readOnly = true)
+public class LineService {
+ private final LineRepository lineRepository;
+
+ private final StationRepository stationRepository;
+
+ public LineService(LineRepository lineRepository, StationRepository stationRepository) {
+ this.lineRepository = lineRepository;
+ this.stationRepository = stationRepository;
+ }
+
+ @Transactional
+ public LineResponse saveLine(LineCreateRequest lineCreateRequest) {
+ Line persistLine = lineRepository.save(lineCreateRequest.toLine());
+ addStations(persistLine, lineCreateRequest);
+ return LineResponse.of(persistLine);
+ }
+
+ public LineResponse findLine(Long id) {
+ Optional<Line> line = lineRepository.findById(id);
+ return LineResponse.of(line.get());
+ }
+
+ public List<LineResponse> findAllLines() {
+ return lineRepository.findAll().stream()
+ .map(LineResponse::of)
+ .collect(Collectors.toList());
+ }
+
+ @Transactional
+ public LineResponse updateLine(Long id, LineUpdateRequest lineUpdateRequest) {
+ Line line = lineRepository.findById(id).get();
+ line.updateLine(lineUpdateRequest);
+ return LineResponse.of(line);
+ }
+
+ @Transactional
+ public void deleteLineById(Long id) {
+ Optional<Line> line = lineRepository.findById(id);
+ if (line.isPresent()) {
+ line.get().clearRelatedLines();
+ lineRepository.delete(line.get());
+ }
+ }
+
+ private void addStations(Line persistLine, LineCreateRequest lineCreateRequest) {
+ addUpLine(persistLine, lineCreateRequest);
+ addDownLine(persistLine, lineCreateRequest);
+ }
+
+ private void addUpLine(Line line, LineCreateRequest lineCreateRequest) {
+ if (lineCreateRequest.hasUpStationsId()) {
+ Station station = getStation(lineCreateRequest.getUpStationId());
+ line.addStation(station);
+ }
+ }
+
+ private void addDownLine(Line line, LineCreateRequest lineCreateRequest) {
+ if (lineCreateRequest.hasDownStationsId()) {
+ Station station = getStation(lineCreateRequest.getDownStationId());
+ line.addStation(station);
+ }
+ }
+
+ private Station getStation(Long stationId) {
+ return stationRepository.findById(stationId)
+ .orElseThrow(() -> new NoSuchElementException("id(" + stationId + ")์ ํด๋นํ๋ ์ญ์ด ์์ต๋๋ค."));
+ }
+} | Java | deleteById ๊ตฌํ์ฒด๋ฅผ ๋ณด์๋ฉด ์กฐํ ํ์ ๊ฐ์ด ์์ ๊ฒฝ์ฐ์๋ง ์ญ์ ํ๊ณ ์์ต๋๋ค.
์ด๋ฐ ๋ถ๋ถ๋ค์ ์ ํ๋ฆฌ์ผ์ด์
์์ ์ง์ ์์ฑํ๊ธฐ๋ณด๋ค๋ Infrastructre layer์์ ๋ด๋นํ๋ ๊ฒ์ด ์ข๋ค๊ณ ์๊ฐํ๋๋ฐ
์ด๋ป๊ฒ ์๊ฐํ์๋์?
```java
@Transactional
@Override
public void deleteById(ID id) {
Assert.notNull(id, ID_MUST_NOT_BE_NULL);
delete(findById(id).orElseThrow(() -> new EmptyResultDataAccessException(
String.format("No %s entity with id %s exists!", entityInformation.getJavaType(), id), 1)));
} |
@@ -0,0 +1,86 @@
+package nextstep.subway.application;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import nextstep.subway.domain.Line;
+import nextstep.subway.domain.LineRepository;
+import nextstep.subway.domain.Station;
+import nextstep.subway.domain.StationRepository;
+import nextstep.subway.dto.LineCreateRequest;
+import nextstep.subway.dto.LineResponse;
+import nextstep.subway.dto.LineUpdateRequest;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@Transactional(readOnly = true)
+public class LineService {
+ private final LineRepository lineRepository;
+
+ private final StationRepository stationRepository;
+
+ public LineService(LineRepository lineRepository, StationRepository stationRepository) {
+ this.lineRepository = lineRepository;
+ this.stationRepository = stationRepository;
+ }
+
+ @Transactional
+ public LineResponse saveLine(LineCreateRequest lineCreateRequest) {
+ Line persistLine = lineRepository.save(lineCreateRequest.toLine());
+ addStations(persistLine, lineCreateRequest);
+ return LineResponse.of(persistLine);
+ }
+
+ public LineResponse findLine(Long id) {
+ Optional<Line> line = lineRepository.findById(id);
+ return LineResponse.of(line.get());
+ }
+
+ public List<LineResponse> findAllLines() {
+ return lineRepository.findAll().stream()
+ .map(LineResponse::of)
+ .collect(Collectors.toList());
+ }
+
+ @Transactional
+ public LineResponse updateLine(Long id, LineUpdateRequest lineUpdateRequest) {
+ Line line = lineRepository.findById(id).get();
+ line.updateLine(lineUpdateRequest);
+ return LineResponse.of(line);
+ }
+
+ @Transactional
+ public void deleteLineById(Long id) {
+ Optional<Line> line = lineRepository.findById(id);
+ if (line.isPresent()) {
+ line.get().clearRelatedLines();
+ lineRepository.delete(line.get());
+ }
+ }
+
+ private void addStations(Line persistLine, LineCreateRequest lineCreateRequest) {
+ addUpLine(persistLine, lineCreateRequest);
+ addDownLine(persistLine, lineCreateRequest);
+ }
+
+ private void addUpLine(Line line, LineCreateRequest lineCreateRequest) {
+ if (lineCreateRequest.hasUpStationsId()) {
+ Station station = getStation(lineCreateRequest.getUpStationId());
+ line.addStation(station);
+ }
+ }
+
+ private void addDownLine(Line line, LineCreateRequest lineCreateRequest) {
+ if (lineCreateRequest.hasDownStationsId()) {
+ Station station = getStation(lineCreateRequest.getDownStationId());
+ line.addStation(station);
+ }
+ }
+
+ private Station getStation(Long stationId) {
+ return stationRepository.findById(stationId)
+ .orElseThrow(() -> new NoSuchElementException("id(" + stationId + ")์ ํด๋นํ๋ ์ญ์ด ์์ต๋๋ค."));
+ }
+} | Java | ๋ฉ์๋ ๋ณ์๋ก ํ ๋นํ์ ์ด์ ๊ฐ ์์ผ์ ๊ฐ์?
``` java
findAll().stream()
.map(LineResponse::of)
.collect(Collectors.toList());
``` |
@@ -1,16 +1,35 @@
package nextstep.subway.domain;
-import javax.persistence.*;
+import java.util.Objects;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
@Entity
public class Station extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
+
@Column(unique = true)
private String name;
- public Station() {
+ @ManyToOne
+ @JoinColumn(name = "line_id")
+ private Line line;
+
+ private Integer distance;
+
+ protected Station() {
+ }
+
+ Station(Long id, String name) {
+ this.id = id;
+ this.name = name;
}
public Station(String name) {
@@ -24,4 +43,61 @@ public Long getId() {
public String getName() {
return name;
}
+
+ public Integer getDistance() {
+ return distance;
+ }
+
+ public Line getLine() {
+ return line;
+ }
+
+ public void setLine(Line line) {
+ if (Objects.nonNull(this.line)) {
+ this.line.getStations().removeStation(this);
+ }
+ this.line = line;
+ if (!line.getStations().containsStation(this)) {
+ line.getStations().addStation(this);
+ }
+ }
+
+ public void clearLine() {
+ this.line = null;
+ }
+
+ public void clearRelatedStation() {
+ if (line != null) {
+ this.line.getStations().removeStation(this);
+ }
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ Station station = (Station) o;
+
+ return id.equals(station.id);
+ }
+
+ @Override
+ public int hashCode() {
+ return id.hashCode();
+ }
+
+ @Override
+ public String toString() {
+ return "Station{" +
+ "id=" + id +
+ ", name='" + name + '\'' +
+ ", line=" + line +
+ ", distance=" + distance +
+ '}';
+ }
} | Java | ์ญ์ด Line์ ๊ฐ์ง๋๊ฒ ๋ง์๊น์?
ํ๋์ ์ญ์ด ์ฌ๋ฌ ๋
ธ์ ์ ์ํ๋ฉด ์ด๋ป๊ฒ ๋๋์.
์๋ฅผ ๋ค์ด ๊ฐ๋จ์ญ์ 2ํธ์ ๊ณผ ์ ๋ถ๋น์ ๋๊ฐ์ง๊ฐ ์์ด์. |
@@ -0,0 +1,156 @@
+package nextstep.subway.acceptance;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import io.restassured.RestAssured;
+import io.restassured.response.ExtractableResponse;
+import io.restassured.response.Response;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.http.HttpStatus;
+import org.springframework.test.context.jdbc.Sql;
+
+@Sql("classpath:testdb/truncate.sql")
+@DisplayName("์งํ์ฒ ๋
ธ์ ๊ด๋ จ ๊ธฐ๋ฅ")
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+class LineAcceptanceTest extends AcceptanceTest {
+ private final static String API_URL_LINES = "/lines";
+ private final static String COLOR_RED = "bg-red-400";
+ private final static String LINE_NUMBER_1 = "1ํธ์ ";
+ private final static String LINE_NUMBER_2 = "2ํธ์ ";
+ @LocalServerPort
+ int port;
+
+ @BeforeEach
+ public void setUp() {
+ if (RestAssured.port == RestAssured.UNDEFINED_PORT) {
+ RestAssured.port = port;
+ }
+ }
+
+
+ /**
+ * When ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๋ฉด
+ * Then ์งํ์ฒ ๋
ธ์ ๋ชฉ๋ก ์กฐํ ์ ์์ฑํ ๋
ธ์ ์ ์ฐพ์ ์ ์๋ค
+ */
+ @DisplayName("์งํ์ฒ ๋
ธ์ ์์ฑ")
+ @Test
+ void createLine() {
+ // when
+ registerLine(LINE_NUMBER_2);
+
+ // then
+ List<String> lineNames = findLines().jsonPath().getList("name", String.class);
+ assertThat(lineNames).contains(LINE_NUMBER_2);
+
+ }
+
+ /**
+ * Given 2๊ฐ์ ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๊ณ
+ * When ์งํ์ฒ ๋
ธ์ ๋ชฉ๋ก์ ์กฐํํ๋ฉด
+ * Then ์งํ์ฒ ๋
ธ์ ๋ชฉ๋ก ์กฐํ ์ 2๊ฐ์ ๋
ธ์ ์ ์กฐํํ ์ ์๋ค.
+ */
+ @DisplayName("์งํ์ฒ ๋
ธ์ ๋ชฉ๋ก ์กฐํ")
+ @Test
+ void getLines() {
+ // given
+ registerLine(LINE_NUMBER_1);
+ registerLine(LINE_NUMBER_2);
+
+ // when
+ List<String> lineNames = findLines().jsonPath().getList("name", String.class);
+
+ // then
+ assertThat(lineNames).contains(LINE_NUMBER_1, LINE_NUMBER_2);
+ }
+
+ /**
+ * Given ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๊ณ
+ * When ์์ฑํ ์งํ์ฒ ๋
ธ์ ์ ์กฐํํ๋ฉด
+ * Then ์์ฑํ ์งํ์ฒ ๋
ธ์ ์ ์ ๋ณด๋ฅผ ์๋ต๋ฐ์ ์ ์๋ค.
+ */
+ @DisplayName("์งํ์ฒ ๋
ธ์ ์กฐํ")
+ @Test
+ void getLine() {
+ // given
+ String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id");
+
+ // when
+ ExtractableResponse<Response> response = findLine(lineId);
+
+ // then
+ assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
+ }
+
+ /**
+ * Given ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๊ณ
+ * When ์์ฑํ ์งํ์ฒ ๋
ธ์ ์ ์์ ํ๋ฉด
+ * Then ํด๋น ์งํ์ฒ ๋
ธ์ ์ ๋ณด๋ ์์ ๋๋ค
+ */
+
+ @DisplayName("์งํ์ฒ ๋
ธ์ ์์ ")
+ @Test
+ void updateLine() {
+ // given
+ String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id");
+
+ // when
+ modifyLine(lineId, COLOR_RED);
+
+ // then
+ String actual = findLine(lineId).jsonPath().getString("color");
+ assertThat(actual).isEqualTo(COLOR_RED);
+ }
+
+ /**
+ * Given ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๊ณ
+ * When ์์ฑํ ์งํ์ฒ ๋
ธ์ ์ ์ญ์ ํ๋ฉด
+ * Then ํด๋น ์งํ์ฒ ๋
ธ์ ์ ๋ณด๋ ์ญ์ ๋๋ค
+ */
+ @DisplayName("์งํ์ฒ ๋
ธ์ ์ญ์ ")
+ @Test
+ void deleteLine() {
+ // given
+ String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id");
+
+ // when
+ removeLine(lineId);
+ List<String> lineNames = findLines().jsonPath().getList("name", String.class);
+
+ // then
+ assertThat(lineNames.isEmpty() || !lineNames.contains(LINE_NUMBER_2)).isTrue();
+ }
+
+ private ExtractableResponse<Response> registerLine(String lineName) {
+ Map<String, Object> lineMap = new HashMap<>();
+ lineMap.put("name", lineName);
+ lineMap.put("color", "red");
+ lineMap.put("distance", 2);
+ return sendPost(lineMap, API_URL_LINES);
+ }
+
+ private ExtractableResponse<Response> findLines() {
+ return sendGet(API_URL_LINES);
+ }
+
+ private ExtractableResponse<Response> findLine(String lineId) {
+ return sendGet(API_URL_LINES + "/{id}", lineId);
+ }
+
+ private ExtractableResponse<Response> modifyLine(String lineId, String color) {
+ Map<String, Object> lineMap = new HashMap<>();
+ lineMap.put("name", "redred");
+ lineMap.put("color", color);
+ return sendPut(lineMap, API_URL_LINES + "/{id}", lineId);
+ }
+
+ private ExtractableResponse<Response> removeLine(String lineId) {
+ return sendDelete(API_URL_LINES + "/{id}", lineId);
+ }
+} | Java | ๋
ธ์ ์์ฑ ์ ์ํ์ข
์ ์ญ๊ณผ ํํ์ข
์ ์ญ์ ๋ฑ๋กํฉ๋๋ค.
๋ฐ๋ผ์ ์ด๋ฒ ๋จ๊ณ์์๋ ์งํ์ฒ ๋
ธ์ ์ ์ญ์ ๋งตํํ๋ ๊ธฐ๋ฅ์ ์์ง ์์ง๋ง ๋
ธ์ ์กฐํ์ ํฌํจ๋ ์ญ ๋ชฉ๋ก์ด ํจ๊ป ์๋ต๋ฉ๋๋ค.
statusCode ์ฝ๋ ๋ฟ๋ง ์๋๋ผ ์ค์ ์๋ต์๋ํด์ ํ
์คํธ ํ ์ ์์ผ๋ฉด ์ข์๊ฒ ๊ฐ์์. |
@@ -0,0 +1,156 @@
+package nextstep.subway.acceptance;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import io.restassured.RestAssured;
+import io.restassured.response.ExtractableResponse;
+import io.restassured.response.Response;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.http.HttpStatus;
+import org.springframework.test.context.jdbc.Sql;
+
+@Sql("classpath:testdb/truncate.sql")
+@DisplayName("์งํ์ฒ ๋
ธ์ ๊ด๋ จ ๊ธฐ๋ฅ")
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+class LineAcceptanceTest extends AcceptanceTest {
+ private final static String API_URL_LINES = "/lines";
+ private final static String COLOR_RED = "bg-red-400";
+ private final static String LINE_NUMBER_1 = "1ํธ์ ";
+ private final static String LINE_NUMBER_2 = "2ํธ์ ";
+ @LocalServerPort
+ int port;
+
+ @BeforeEach
+ public void setUp() {
+ if (RestAssured.port == RestAssured.UNDEFINED_PORT) {
+ RestAssured.port = port;
+ }
+ }
+
+
+ /**
+ * When ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๋ฉด
+ * Then ์งํ์ฒ ๋
ธ์ ๋ชฉ๋ก ์กฐํ ์ ์์ฑํ ๋
ธ์ ์ ์ฐพ์ ์ ์๋ค
+ */
+ @DisplayName("์งํ์ฒ ๋
ธ์ ์์ฑ")
+ @Test
+ void createLine() {
+ // when
+ registerLine(LINE_NUMBER_2);
+
+ // then
+ List<String> lineNames = findLines().jsonPath().getList("name", String.class);
+ assertThat(lineNames).contains(LINE_NUMBER_2);
+
+ }
+
+ /**
+ * Given 2๊ฐ์ ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๊ณ
+ * When ์งํ์ฒ ๋
ธ์ ๋ชฉ๋ก์ ์กฐํํ๋ฉด
+ * Then ์งํ์ฒ ๋
ธ์ ๋ชฉ๋ก ์กฐํ ์ 2๊ฐ์ ๋
ธ์ ์ ์กฐํํ ์ ์๋ค.
+ */
+ @DisplayName("์งํ์ฒ ๋
ธ์ ๋ชฉ๋ก ์กฐํ")
+ @Test
+ void getLines() {
+ // given
+ registerLine(LINE_NUMBER_1);
+ registerLine(LINE_NUMBER_2);
+
+ // when
+ List<String> lineNames = findLines().jsonPath().getList("name", String.class);
+
+ // then
+ assertThat(lineNames).contains(LINE_NUMBER_1, LINE_NUMBER_2);
+ }
+
+ /**
+ * Given ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๊ณ
+ * When ์์ฑํ ์งํ์ฒ ๋
ธ์ ์ ์กฐํํ๋ฉด
+ * Then ์์ฑํ ์งํ์ฒ ๋
ธ์ ์ ์ ๋ณด๋ฅผ ์๋ต๋ฐ์ ์ ์๋ค.
+ */
+ @DisplayName("์งํ์ฒ ๋
ธ์ ์กฐํ")
+ @Test
+ void getLine() {
+ // given
+ String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id");
+
+ // when
+ ExtractableResponse<Response> response = findLine(lineId);
+
+ // then
+ assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
+ }
+
+ /**
+ * Given ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๊ณ
+ * When ์์ฑํ ์งํ์ฒ ๋
ธ์ ์ ์์ ํ๋ฉด
+ * Then ํด๋น ์งํ์ฒ ๋
ธ์ ์ ๋ณด๋ ์์ ๋๋ค
+ */
+
+ @DisplayName("์งํ์ฒ ๋
ธ์ ์์ ")
+ @Test
+ void updateLine() {
+ // given
+ String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id");
+
+ // when
+ modifyLine(lineId, COLOR_RED);
+
+ // then
+ String actual = findLine(lineId).jsonPath().getString("color");
+ assertThat(actual).isEqualTo(COLOR_RED);
+ }
+
+ /**
+ * Given ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๊ณ
+ * When ์์ฑํ ์งํ์ฒ ๋
ธ์ ์ ์ญ์ ํ๋ฉด
+ * Then ํด๋น ์งํ์ฒ ๋
ธ์ ์ ๋ณด๋ ์ญ์ ๋๋ค
+ */
+ @DisplayName("์งํ์ฒ ๋
ธ์ ์ญ์ ")
+ @Test
+ void deleteLine() {
+ // given
+ String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id");
+
+ // when
+ removeLine(lineId);
+ List<String> lineNames = findLines().jsonPath().getList("name", String.class);
+
+ // then
+ assertThat(lineNames.isEmpty() || !lineNames.contains(LINE_NUMBER_2)).isTrue();
+ }
+
+ private ExtractableResponse<Response> registerLine(String lineName) {
+ Map<String, Object> lineMap = new HashMap<>();
+ lineMap.put("name", lineName);
+ lineMap.put("color", "red");
+ lineMap.put("distance", 2);
+ return sendPost(lineMap, API_URL_LINES);
+ }
+
+ private ExtractableResponse<Response> findLines() {
+ return sendGet(API_URL_LINES);
+ }
+
+ private ExtractableResponse<Response> findLine(String lineId) {
+ return sendGet(API_URL_LINES + "/{id}", lineId);
+ }
+
+ private ExtractableResponse<Response> modifyLine(String lineId, String color) {
+ Map<String, Object> lineMap = new HashMap<>();
+ lineMap.put("name", "redred");
+ lineMap.put("color", color);
+ return sendPut(lineMap, API_URL_LINES + "/{id}", lineId);
+ }
+
+ private ExtractableResponse<Response> removeLine(String lineId) {
+ return sendDelete(API_URL_LINES + "/{id}", lineId);
+ }
+} | Java | ์ฑํ ํฝ์ค์ฒ ๋ฐ ๋ฐ์ดํฐ ์ง์ ์ญ์ ํด์ฃผ์
จ์ด์.
๋ค๋ง ์์ฑํด์ผ ํ ๋ฐ์ดํฐ๊ฐ ๋ง๊ฑฐ๋, ์ฐ๊ด ๊ด๊ณ ๋งตํ์ด ์์ผ๋ฉด ์ด๋ป๊ฒ ๋ ์ง ๊ณ ๋ฏผํด๋ณด์๋ฉด ์ข์ ๊ฒ๊ฐ์์. |
@@ -0,0 +1,156 @@
+package nextstep.subway.acceptance;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import io.restassured.RestAssured;
+import io.restassured.response.ExtractableResponse;
+import io.restassured.response.Response;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.http.HttpStatus;
+import org.springframework.test.context.jdbc.Sql;
+
+@Sql("classpath:testdb/truncate.sql")
+@DisplayName("์งํ์ฒ ๋
ธ์ ๊ด๋ จ ๊ธฐ๋ฅ")
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+class LineAcceptanceTest extends AcceptanceTest {
+ private final static String API_URL_LINES = "/lines";
+ private final static String COLOR_RED = "bg-red-400";
+ private final static String LINE_NUMBER_1 = "1ํธ์ ";
+ private final static String LINE_NUMBER_2 = "2ํธ์ ";
+ @LocalServerPort
+ int port;
+
+ @BeforeEach
+ public void setUp() {
+ if (RestAssured.port == RestAssured.UNDEFINED_PORT) {
+ RestAssured.port = port;
+ }
+ }
+
+
+ /**
+ * When ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๋ฉด
+ * Then ์งํ์ฒ ๋
ธ์ ๋ชฉ๋ก ์กฐํ ์ ์์ฑํ ๋
ธ์ ์ ์ฐพ์ ์ ์๋ค
+ */
+ @DisplayName("์งํ์ฒ ๋
ธ์ ์์ฑ")
+ @Test
+ void createLine() {
+ // when
+ registerLine(LINE_NUMBER_2);
+
+ // then
+ List<String> lineNames = findLines().jsonPath().getList("name", String.class);
+ assertThat(lineNames).contains(LINE_NUMBER_2);
+
+ }
+
+ /**
+ * Given 2๊ฐ์ ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๊ณ
+ * When ์งํ์ฒ ๋
ธ์ ๋ชฉ๋ก์ ์กฐํํ๋ฉด
+ * Then ์งํ์ฒ ๋
ธ์ ๋ชฉ๋ก ์กฐํ ์ 2๊ฐ์ ๋
ธ์ ์ ์กฐํํ ์ ์๋ค.
+ */
+ @DisplayName("์งํ์ฒ ๋
ธ์ ๋ชฉ๋ก ์กฐํ")
+ @Test
+ void getLines() {
+ // given
+ registerLine(LINE_NUMBER_1);
+ registerLine(LINE_NUMBER_2);
+
+ // when
+ List<String> lineNames = findLines().jsonPath().getList("name", String.class);
+
+ // then
+ assertThat(lineNames).contains(LINE_NUMBER_1, LINE_NUMBER_2);
+ }
+
+ /**
+ * Given ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๊ณ
+ * When ์์ฑํ ์งํ์ฒ ๋
ธ์ ์ ์กฐํํ๋ฉด
+ * Then ์์ฑํ ์งํ์ฒ ๋
ธ์ ์ ์ ๋ณด๋ฅผ ์๋ต๋ฐ์ ์ ์๋ค.
+ */
+ @DisplayName("์งํ์ฒ ๋
ธ์ ์กฐํ")
+ @Test
+ void getLine() {
+ // given
+ String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id");
+
+ // when
+ ExtractableResponse<Response> response = findLine(lineId);
+
+ // then
+ assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
+ }
+
+ /**
+ * Given ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๊ณ
+ * When ์์ฑํ ์งํ์ฒ ๋
ธ์ ์ ์์ ํ๋ฉด
+ * Then ํด๋น ์งํ์ฒ ๋
ธ์ ์ ๋ณด๋ ์์ ๋๋ค
+ */
+
+ @DisplayName("์งํ์ฒ ๋
ธ์ ์์ ")
+ @Test
+ void updateLine() {
+ // given
+ String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id");
+
+ // when
+ modifyLine(lineId, COLOR_RED);
+
+ // then
+ String actual = findLine(lineId).jsonPath().getString("color");
+ assertThat(actual).isEqualTo(COLOR_RED);
+ }
+
+ /**
+ * Given ์งํ์ฒ ๋
ธ์ ์ ์์ฑํ๊ณ
+ * When ์์ฑํ ์งํ์ฒ ๋
ธ์ ์ ์ญ์ ํ๋ฉด
+ * Then ํด๋น ์งํ์ฒ ๋
ธ์ ์ ๋ณด๋ ์ญ์ ๋๋ค
+ */
+ @DisplayName("์งํ์ฒ ๋
ธ์ ์ญ์ ")
+ @Test
+ void deleteLine() {
+ // given
+ String lineId = registerLine(LINE_NUMBER_2).jsonPath().getString("id");
+
+ // when
+ removeLine(lineId);
+ List<String> lineNames = findLines().jsonPath().getList("name", String.class);
+
+ // then
+ assertThat(lineNames.isEmpty() || !lineNames.contains(LINE_NUMBER_2)).isTrue();
+ }
+
+ private ExtractableResponse<Response> registerLine(String lineName) {
+ Map<String, Object> lineMap = new HashMap<>();
+ lineMap.put("name", lineName);
+ lineMap.put("color", "red");
+ lineMap.put("distance", 2);
+ return sendPost(lineMap, API_URL_LINES);
+ }
+
+ private ExtractableResponse<Response> findLines() {
+ return sendGet(API_URL_LINES);
+ }
+
+ private ExtractableResponse<Response> findLine(String lineId) {
+ return sendGet(API_URL_LINES + "/{id}", lineId);
+ }
+
+ private ExtractableResponse<Response> modifyLine(String lineId, String color) {
+ Map<String, Object> lineMap = new HashMap<>();
+ lineMap.put("name", "redred");
+ lineMap.put("color", color);
+ return sendPut(lineMap, API_URL_LINES + "/{id}", lineId);
+ }
+
+ private ExtractableResponse<Response> removeLine(String lineId) {
+ return sendDelete(API_URL_LINES + "/{id}", lineId);
+ }
+} | Java | ๋
ธ์ ์์ฑ ์ ์ํ์ข
์ ์ญ๊ณผ ํํ์ข
์ ์ญ์ ๋ฑ๋กํฉ๋๋ค.
ํด๋น ์๊ตฌ์ฌํญ์ด ๊ตฌํ๋์ง ์์ ๊ฒ ๊ฐ์์. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.