code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,15 @@
+package christmas.controller;
+
+import christmas.domain.Order;
+import christmas.domain.Reservation;
+import christmas.domain.Result;
+
+public class EventPlannerController {
+ public static void run() {
+ Reservation reservation = ReservationController.inputReservation();
+ Order order = OrderController.inputOrder();
+ Result result = ResultController.createResult(order, reservation);
+ ResultController.printResult(result);
+
+ }
+} | Java | static ๋ฉ์๋๋ง ์๋ ํด๋์ค๋ผ๋ฉด ์ธ์คํด์ค๋ฅพ ๋ง๋ค ํ์๊ฐ ์์ํ
๋ฐ์, ์ด๋ฅผ ์ด๋ป๊ฒ ๋ฐฉ์งํ ์ ์์๊น์? |
@@ -0,0 +1,117 @@
+package christmas.view;
+
+import static christmas.view.messages.PrintMessage.*;
+
+import christmas.domain.Menu;
+import christmas.domain.Order;
+import christmas.domain.Result;
+import christmas.view.messages.PrintMessage;
+import java.util.Map;
+
+public class OutputView {
+ private static final int TRUE = 1;
+ private static final int FALSE = 0;
+ private static final int CHAMPAGNE = 25000;
+ public static void println(Object data) {
+ System.out.println(data);
+
+ }
+ public static void printMessage(final PrintMessage message) {
+ System.out.println(message.getMessage());
+ }
+
+ public static void printOrder(Order order) {
+ Map<Menu, Integer> orderedItems = order.getOrderedItems();
+ printMessage(OUTPUT_ORDER);
+ for (Map.Entry<Menu, Integer> entry : orderedItems.entrySet()) {
+ System.out.println(entry.getKey() + " " + entry.getValue() + COUNT.getMessage());
+ }
+ }
+
+ public static void printAllEvents(Result result) {
+ printTotalBeforeDiscount(result);
+ printServiceMenu(result);
+ printBenefit(result);
+ printWeekday(result);
+ printWeekend(result);
+ printSpecialDay(result);
+ printAllBenefit(result);
+ printTotalAfterBenefit(result);
+ printEventBadge(result);
+ }
+
+ public static void printIntroduce(int date) {
+ System.out.println(PREVIEW.getMessage(date));
+ System.out.println();
+ }
+
+ public static void printTotalBeforeDiscount(Result result) {
+ System.out.println(OUTPUT_TOTAL_BEFORE_DISCOUNT.getMessage());
+ System.out.println(result.getTotalBeforeDiscount() + "" + MONEY.getMessage());
+ System.out.println();
+ }
+
+
+ public static void printServiceMenu(Result result) {
+ System.out.println(OUTPUT_GIFT_MENU.getMessage());
+ if (result.getServiceMenu() == TRUE) {
+ System.out.println(SERVICE_MENU.getMessage());
+ }
+ if (result.getServiceMenu() == FALSE) {
+ System.out.println(NONE.getMessage());
+ }
+ System.out.println();
+ }
+
+ public static void printBenefit(Result result) {
+ System.out.println(OUTPUT_BENEFIT.getMessage());
+ if (result.getdDayDiscount() == FALSE) {
+ System.out.println(NONE.getMessage());
+ }
+ if (result.getdDayDiscount() != FALSE) {
+ System.out.println(CHRISTMAS_DISCOUNT.getMessage() + result.getdDayDiscount() + "" + MONEY.getMessage());
+ }
+ }
+
+ public static void printWeekday(Result result) {
+ if (result.getWeekdayDiscount() != FALSE) {
+ System.out.println(WEEKDAY_DISCOUNT.getMessage() + result.getWeekdayDiscount() + MONEY.getMessage());
+ }
+ }
+
+ public static void printWeekend(Result result) {
+ if (result.getWeekendDiscount() != FALSE) {
+ System.out.println(WEEKEND_DISCOUNT.getMessage() + result.getWeekendDiscount() + MONEY.getMessage());
+ }
+ }
+
+ public static void printSpecialDay(Result result) {
+ if (result.getServiceMenu() == TRUE) {
+ System.out.println(SPECIAL_DISCOUNT.getMessage() + result.getSpecialDiscount() + MONEY.getMessage());
+ System.out.println(SERVICE_DISCOUNT.getMessage() + CHAMPAGNE + MONEY.getMessage());
+ }
+ System.out.println();
+ }
+
+ public static void printAllBenefit(Result result) {
+ System.out.println(BENEFIT_ALL_DISCOUNT.getMessage());
+ if (result.getTotalBenefit() != FALSE) {
+ System.out.println("-" + result.getTotalBenefit() + MONEY.getMessage());
+ }
+ if (result.getTotalBenefit() == FALSE) {
+ System.out.println(result.getTotalBenefit() + MONEY.getMessage());
+ }
+ System.out.println();
+ }
+
+ public static void printTotalAfterBenefit(Result result) {
+ System.out.println(AFTER_BENEFIT_DISCOUNT.getMessage());
+ System.out.println(result.getTotalAfterBenefit() + MONEY.getMessage());
+ System.out.println();
+ }
+
+ public static void printEventBadge(Result result) {
+ System.out.println(BADGE_DISCOUNT.getMessage());
+ System.out.println(result.getBadge());
+ }
+} | Java | ๋ถํ์ํ ๊ฐํ์ ์ง์์ฃผ์ธ์..! |
@@ -0,0 +1,36 @@
+package com.doubleowner.revibe.global.common.service;
+
+import com.doubleowner.revibe.global.exception.CommonException;
+import com.doubleowner.revibe.global.exception.errorCode.ErrorCode;
+import com.doubleowner.revibe.global.util.S3Uploader;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+
+@Service
+@RequiredArgsConstructor
+public class ImageService {
+ private final S3Uploader s3Uploader;
+
+ public String UploadImage(String image, MultipartFile file) {
+ try {
+ if (image != null) {
+ s3Uploader.deleteImage(image);
+ }
+ return s3Uploader.upload(file);
+ } catch (IOException e) {
+ throw new CommonException(ErrorCode.FAILED_UPLOAD_IMAGE);
+ }
+ }
+
+ public void deleteImage(String image) {
+ try {
+ s3Uploader.deleteImage(image);
+ } catch (IOException e) {
+ throw new CommonException(ErrorCode.FAILED_DELETE_IMAGE);
+ }
+ }
+
+} | Java | _:hammer_and_wrench: Refactor suggestion_
**ํ์ผ ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ํ์ํฉ๋๋ค**
`file` ํ๋ผ๋ฏธํฐ์ ๋ํ null ์ฒดํฌ์ ํ์ผ ํฌ๊ธฐ, ํ์
๋ฑ์ ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ๋๋ฝ๋์ด ์์ต๋๋ค. ์๋ชป๋ ์
๋ ฅ์ผ๋ก ์ธํ ์์ธ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ๊ฒ์ฆ ๋ก์ง์ด ํ์ํฉ๋๋ค.
๋ค์๊ณผ ๊ฐ์ ๊ฒ์ฆ ๋ก์ง ์ถ๊ฐ๋ฅผ ์ ์ํฉ๋๋ค:
```diff
public String UploadImage(String image, MultipartFile file) {
+ if (file == null || file.isEmpty()) {
+ throw new CommonException(ErrorCode.INVALID_FILE);
+ }
+ validateFileSize(file);
+ validateFileType(file);
try {
```
์ถ๊ฐ๋ก ๋ค์๊ณผ ๊ฐ์ ๊ฒ์ฆ ๋ฉ์๋ ๊ตฌํ์ด ํ์ํฉ๋๋ค:
```java
private void validateFileSize(MultipartFile file) {
long maxSize = 5 * 1024 * 1024; // 5MB
if (file.getSize() > maxSize) {
throw new CommonException(ErrorCode.FILE_TOO_LARGE);
}
}
private void validateFileType(MultipartFile file) {
String contentType = file.getContentType();
if (contentType == null || !contentType.startsWith("image/")) {
throw new CommonException(ErrorCode.INVALID_FILE_TYPE);
}
}
```
<!-- This is an auto-generated comment by CodeRabbit --> |
@@ -0,0 +1,36 @@
+package com.doubleowner.revibe.global.common.service;
+
+import com.doubleowner.revibe.global.exception.CommonException;
+import com.doubleowner.revibe.global.exception.errorCode.ErrorCode;
+import com.doubleowner.revibe.global.util.S3Uploader;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+
+@Service
+@RequiredArgsConstructor
+public class ImageService {
+ private final S3Uploader s3Uploader;
+
+ public String UploadImage(String image, MultipartFile file) {
+ try {
+ if (image != null) {
+ s3Uploader.deleteImage(image);
+ }
+ return s3Uploader.upload(file);
+ } catch (IOException e) {
+ throw new CommonException(ErrorCode.FAILED_UPLOAD_IMAGE);
+ }
+ }
+
+ public void deleteImage(String image) {
+ try {
+ s3Uploader.deleteImage(image);
+ } catch (IOException e) {
+ throw new CommonException(ErrorCode.FAILED_DELETE_IMAGE);
+ }
+ }
+
+} | Java | _:hammer_and_wrench: Refactor suggestion_
**๋ฉ์๋ ์ด๋ฆ์ด Java ๋ค์ด๋ฐ ์ปจ๋ฒค์
์ ์๋ฐํฉ๋๋ค**
`UploadImage` ๋ฉ์๋ ์ด๋ฆ์ด ์นด๋ฉ ์ผ์ด์ค ๊ท์น์ ๋ฐ๋ฅด์ง ์์ต๋๋ค. Java ๋ฉ์๋ ์ด๋ฆ์ ์๋ฌธ์๋ก ์์ํด์ผ ํฉ๋๋ค.
๋ค์๊ณผ ๊ฐ์ด ์์ ํ๋ ๊ฒ์ ์ ์ํฉ๋๋ค:
```diff
- public String UploadImage(String image, MultipartFile file) {
+ public String uploadImage(String image, MultipartFile file) {
```
<!-- suggestion_start -->
<details>
<summary>๐ Committable suggestion</summary>
> โผ๏ธ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
`````suggestion
public String uploadImage(String image, MultipartFile file) {
`````
</details>
<!-- suggestion_end -->
<!-- This is an auto-generated comment by CodeRabbit --> |
@@ -0,0 +1,36 @@
+package com.doubleowner.revibe.global.common.service;
+
+import com.doubleowner.revibe.global.exception.CommonException;
+import com.doubleowner.revibe.global.exception.errorCode.ErrorCode;
+import com.doubleowner.revibe.global.util.S3Uploader;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+
+@Service
+@RequiredArgsConstructor
+public class ImageService {
+ private final S3Uploader s3Uploader;
+
+ public String UploadImage(String image, MultipartFile file) {
+ try {
+ if (image != null) {
+ s3Uploader.deleteImage(image);
+ }
+ return s3Uploader.upload(file);
+ } catch (IOException e) {
+ throw new CommonException(ErrorCode.FAILED_UPLOAD_IMAGE);
+ }
+ }
+
+ public void deleteImage(String image) {
+ try {
+ s3Uploader.deleteImage(image);
+ } catch (IOException e) {
+ throw new CommonException(ErrorCode.FAILED_DELETE_IMAGE);
+ }
+ }
+
+} | Java | _:hammer_and_wrench: Refactor suggestion_
**์ด๋ฏธ์ง ๊ฒฝ๋ก ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ํ์ํฉ๋๋ค**
`image` ํ๋ผ๋ฏธํฐ์ ๋ํ null ์ฒดํฌ์ ๊ฒฝ๋ก ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ๋๋ฝ๋์ด ์์ต๋๋ค. ์๋ชป๋ ์ด๋ฏธ์ง ๊ฒฝ๋ก๋ก ์ธํ ๋ถํ์ํ S3 ํธ์ถ์ ๋ฐฉ์งํด์ผ ํฉ๋๋ค.
๋ค์๊ณผ ๊ฐ์ด ์์ ํ๋ ๊ฒ์ ์ ์ํฉ๋๋ค:
```diff
public void deleteImage(String image) {
+ if (image == null || image.trim().isEmpty()) {
+ return;
+ }
try {
s3Uploader.deleteImage(image);
} catch (IOException e) {
```
<!-- suggestion_start -->
<details>
<summary>๐ Committable suggestion</summary>
> โผ๏ธ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
`````suggestion
public void deleteImage(String image) {
if (image == null || image.trim().isEmpty()) {
return;
}
try {
s3Uploader.deleteImage(image);
} catch (IOException e) {
throw new CommonException(ErrorCode.FAILED_DELETE_IMAGE);
}
}
`````
</details>
<!-- suggestion_end -->
<!-- This is an auto-generated comment by CodeRabbit --> |
@@ -2,25 +2,25 @@
import com.doubleowner.revibe.domain.execution.entity.Execution;
import com.doubleowner.revibe.domain.execution.repository.ExecutionRepository;
+import com.doubleowner.revibe.domain.point.PointService;
import com.doubleowner.revibe.domain.review.dto.ReviewRequestDto;
import com.doubleowner.revibe.domain.review.dto.ReviewResponseDto;
import com.doubleowner.revibe.domain.review.dto.UpdateReviewRequestDto;
import com.doubleowner.revibe.domain.review.entity.Review;
import com.doubleowner.revibe.domain.review.repository.ReviewRepository;
import com.doubleowner.revibe.domain.user.entity.User;
-import com.doubleowner.revibe.domain.user.repository.UserRepository;
+import com.doubleowner.revibe.global.common.service.ImageService;
import com.doubleowner.revibe.global.config.auth.UserDetailsImpl;
import com.doubleowner.revibe.global.exception.CommonException;
import com.doubleowner.revibe.global.exception.errorCode.ErrorCode;
-import com.doubleowner.revibe.global.util.S3Uploader;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
-import java.io.IOException;
import java.util.List;
@@ -29,26 +29,21 @@
public class ReviewService {
private final ReviewRepository reviewRepository;
private final ExecutionRepository executionRepository;
- private final UserRepository userRepository;
- private final S3Uploader s3Uploader;
+ private final PointService pointService;
+ private final ImageService imageService;
- private static final int TEXT_ONLY_POINT = 100;
- private static final int TEXT_IMAGE_POINT = 500;
- private static final int DIFFERENCE_POINT = 400;
@Transactional
public ReviewResponseDto write(ReviewRequestDto reviewRequestDto, User user) {
-
Execution execution = executionRepository.findExecutionById(reviewRequestDto.getPaymentId(), user.getEmail())
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_VALUE, "๋ด์ญ์ ์ฐพ์ ์ ์์ต๋๋ค"));
+
String image = null;
- int point = TEXT_ONLY_POINT;
- if (reviewRequestDto.getImage() != null) {
- image = uploadImage(reviewRequestDto.getImage());
- point = TEXT_IMAGE_POINT;
- }
+ if (hasImage(reviewRequestDto.getImage())) {
+ image = imageService.UploadImage(image, reviewRequestDto.getImage());
+ }
Review review = Review.builder()
.starRate(reviewRequestDto.getStarRate())
@@ -60,12 +55,11 @@ public ReviewResponseDto write(ReviewRequestDto reviewRequestDto, User user) {
.user(user)
.build();
- Review save = reviewRepository.save(review);
- user.addPoint(point);
- userRepository.save(user);
+ Review savedReview = reviewRepository.save(review);
+ pointService.addReviewPoint(user, image);
- return toDto(save);
+ return toDto(savedReview);
}
@@ -76,49 +70,42 @@ public List<ReviewResponseDto> findReview(User user) {
return reviewsByUserId.stream().map(this::toDto).toList();
}
+ @Transactional(readOnly = true)
+ public List<ReviewResponseDto> findItemReviews(Long itemId, int page, int size) {
+ Pageable pageable = PageRequest.of(page - 1, size);
+ Slice<Review> reviews = reviewRepository.findReviewsByItemId(itemId, pageable);
+ return reviews.stream().map(this::toDto).toList();
+ }
+
@Transactional
public void updateReview(Long id, UserDetailsImpl userDetails, UpdateReviewRequestDto updateReviewRequestDto) {
- Review review = reviewRepository.findMyReview(id, userDetails.getUser().getId());
+ Review review = getMyReview(id, userDetails.getUser());
// ์ด๋ฏธ์ง ๊ด๋ จ ์ฒ๋ฆฌ
- handlePoint(review, updateReviewRequestDto.getImage());
-
- // ๋๋จธ์ง ํ๋ ์
๋ฐ์ดํธ
- review.update(updateReviewRequestDto);
+ String image = null;
+ if (hasImage(updateReviewRequestDto.getImage())) {
+ image = imageService.UploadImage(review.getReviewImage(), updateReviewRequestDto.getImage());
+ }
+ pointService.handlePoint(userDetails.getUser(), review.getReviewImage(), updateReviewRequestDto.getImage());
+ // ํ๋ ์
๋ฐ์ดํธ
+ review.update(updateReviewRequestDto, image);
}
@Transactional
public void deleteReview(Long id, User user) {
- Review review = reviewRepository.findMyReview(id, user.getId());
- if (review.getReviewImage() != null) {
- review.getUser().minusPoint(TEXT_IMAGE_POINT);
- } else {
- review.getUser().minusPoint(TEXT_ONLY_POINT);
- }
+ Review review = getMyReview(id, user);
+ pointService.deletePoint(user, review.getReviewImage());
reviewRepository.delete(review);
}
- private void handlePoint(Review review, MultipartFile newImage) {
- try {
- if (newImage != null) {
- if (review.getReviewImage() == null) {
- review.getUser().addPoint(DIFFERENCE_POINT);
- }
- // ์ ์ด๋ฏธ์ง ์
๋ก๋
- reUploadImage(review, newImage);
-
- } else {
- if (review.getReviewImage() != null) {
- s3Uploader.deleteImage(review.getReviewImage());
- review.deleteImage();
- review.getUser().minusPoint(DIFFERENCE_POINT);
- }
- }
- } catch (IOException e) {
- throw new CommonException(ErrorCode.FAILED_UPLOAD_IMAGE);
- }
+ private Review getMyReview(Long id, User user) {
+ return reviewRepository.findMyReview(id, user.getId());
+ }
+
+ private static boolean hasImage(MultipartFile multipartFile) {
+ return multipartFile != null && !multipartFile.isEmpty();
}
/**
@@ -138,48 +125,4 @@ private ReviewResponseDto toDto(Review review) {
.build();
}
- /**
- * ์ด๋ฏธ์ง ์
๋ก๋ ๋ฉ์๋
- *
- * @param file
- * @return
- */
- private String uploadImage(MultipartFile file) {
-
- try {
- return s3Uploader.upload(file);
-
- } catch (IOException e) {
- throw new CommonException(ErrorCode.FAILED_UPLOAD_IMAGE);
- }
- }
-
- /**
- * ์ด๋ฏธ์ง ์ฌ์
๋ก๋ ๋ฉ์๋ (๊ธฐ์กด ์ด๋ฏธ์ง ์ญ์ ํ ์
๋ก๋)
- *
- * @param review
- * @param file
- */
- private void reUploadImage(Review review, MultipartFile file) {
-
- try {
- if (review.getReviewImage() != null) {
- s3Uploader.deleteImage(review.getReviewImage());
- }
-
- String imageUrl = uploadImage(file);
- review.update(imageUrl);
-
- } catch (IOException e) {
- throw new CommonException(ErrorCode.FAILED_UPLOAD_IMAGE);
- }
-
- }
-
- @Transactional(readOnly = true)
- public List<ReviewResponseDto> findItemReviews(Long itemId, int page, int size) {
- Pageable pageable = PageRequest.of(page - 1, size);
- List<Review> reviews = reviewRepository.findReviewsByItemId(itemId, pageable).stream().toList();
- return reviews.stream().map(this::toDto).toList();
- }
} | Java | _:hammer_and_wrench: Refactor suggestion_
**๋ฆฌ๋ทฐ ์ญ์ ์ ์ด๋ฏธ์ง ์ ๊ฑฐ ๊ณ ๋ ค**
ํฌ์ธํธ ์ฐจ๊ฐ ํ ๋ฆฌ๋ทฐ๋ฅผ ์ญ์ ํ์ง๋ง, ์ค์ ์ด๋ฏธ์ง ํ์ผ ์ญ์ ๋ก์ง์ ๋๋ฝ๋ ๊ฒ์ผ๋ก ๋ณด์
๋๋ค. ๋ถํ์ํด์ง ์ด๋ฏธ์ง๋ฅผ ์ ๋ฆฌํ๋ ค๋ฉด `imageService.deleteImage(...)` ํธ์ถ๋ ๊ณ ๋ คํด์ฃผ์ธ์.
๋ค์๊ณผ ๊ฐ์ ์์ ์์๋ฅผ ์ฐธ๊ณ ํด ๋ณด์ค ์ ์์ต๋๋ค:
```diff
98 Review review = getMyReview(id, user);
99 pointService.deletePoint(user, review.getReviewImage());
+ imageService.deleteImage(review.getReviewImage());
reviewRepository.delete(review);
```
<!-- suggestion_start -->
<details>
<summary>๐ Committable suggestion</summary>
> โผ๏ธ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
`````suggestion
Review review = getMyReview(id, user);
pointService.deletePoint(user, review.getReviewImage());
imageService.deleteImage(review.getReviewImage());
reviewRepository.delete(review);
`````
</details>
<!-- suggestion_end -->
<!-- This is an auto-generated comment by CodeRabbit --> |
@@ -47,18 +47,11 @@ public class Review extends BaseTimeEntity {
private User user;
- public void update(UpdateReviewRequestDto updateReviewRequestDto) {
+ public void update(UpdateReviewRequestDto updateReviewRequestDto, String image) {
this.starRate = updateReviewRequestDto.getStarRate();
this.title = updateReviewRequestDto.getTitle();
this.content = updateReviewRequestDto.getContent();
+ this.reviewImage = image;
}
-
- public void update(String reviewImage) {
- this.reviewImage = reviewImage;
- }
-
- public void deleteImage() {
- this.reviewImage = null;
- }
} | Java | _:hammer_and_wrench: Refactor suggestion_
**์ด๋ฏธ์ง ์
๋ฐ์ดํธ ๋ก์ง ํตํฉ์ ๋ํ ํผ๋๋ฐฑ**
์ด๋ฏธ์ง ์
๋ฐ์ดํธ ๋ก์ง์ ํ๋์ ๋ฉ์๋๋ก ํตํฉํ ๊ฒ์ ์ข์ ์ ๊ทผ์
๋๋ค. ํ์ง๋ง ๋ช ๊ฐ์ง ๊ฐ์ ์ฌํญ์ ์ ์๋๋ฆฝ๋๋ค:
1. ์
๋ ฅ๊ฐ ๊ฒ์ฆ์ด ๋๋ฝ๋์ด ์์ต๋๋ค.
2. ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋ก์ง์ด ๋๋ฉ์ธ ๋ชจ๋ธ์ ์ง์ ๋
ธ์ถ๋์ด ์์ต๋๋ค.
๋ค์๊ณผ ๊ฐ์ด ๊ฐ์ ํด๋ณด์๋ ๊ฒ์ ์ด๋จ๊น์:
```diff
public void update(UpdateReviewRequestDto updateReviewRequestDto, String image) {
+ validateUpdateRequest(updateReviewRequestDto);
this.starRate = updateReviewRequestDto.getStarRate();
this.title = updateReviewRequestDto.getTitle();
this.content = updateReviewRequestDto.getContent();
this.reviewImage = image;
}
+private void validateUpdateRequest(UpdateReviewRequestDto dto) {
+ if (dto.getStarRate() == null || dto.getStarRate() < 1 || dto.getStarRate() > 5) {
+ throw new IllegalArgumentException("๋ณ์ ์ 1-5 ์ฌ์ด์ ๊ฐ์ด์ด์ผ ํฉ๋๋ค.");
+ }
+ if (dto.getTitle() == null || dto.getTitle().trim().isEmpty()) {
+ throw new IllegalArgumentException("์ ๋ชฉ์ ํ์ ์
๋ ฅ๊ฐ์
๋๋ค.");
+ }
+ if (dto.getContent() == null || dto.getContent().trim().isEmpty()) {
+ throw new IllegalArgumentException("๋ด์ฉ์ ํ์ ์
๋ ฅ๊ฐ์
๋๋ค.");
+ }
+}
```
<!-- suggestion_start -->
<details>
<summary>๐ Committable suggestion</summary>
> โผ๏ธ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
`````suggestion
public void update(UpdateReviewRequestDto updateReviewRequestDto, String image) {
validateUpdateRequest(updateReviewRequestDto);
this.starRate = updateReviewRequestDto.getStarRate();
this.title = updateReviewRequestDto.getTitle();
this.content = updateReviewRequestDto.getContent();
this.reviewImage = image;
}
private void validateUpdateRequest(UpdateReviewRequestDto dto) {
if (dto.getStarRate() == null || dto.getStarRate() < 1 || dto.getStarRate() > 5) {
throw new IllegalArgumentException("๋ณ์ ์ 1-5 ์ฌ์ด์ ๊ฐ์ด์ด์ผ ํฉ๋๋ค.");
}
if (dto.getTitle() == null || dto.getTitle().trim().isEmpty()) {
throw new IllegalArgumentException("์ ๋ชฉ์ ํ์ ์
๋ ฅ๊ฐ์
๋๋ค.");
}
if (dto.getContent() == null || dto.getContent().trim().isEmpty()) {
throw new IllegalArgumentException("๋ด์ฉ์ ํ์ ์
๋ ฅ๊ฐ์
๋๋ค.");
}
}
`````
</details>
<!-- suggestion_end -->
<!-- This is an auto-generated comment by CodeRabbit --> |
@@ -0,0 +1,52 @@
+package com.doubleowner.revibe.domain.point;
+
+import com.doubleowner.revibe.domain.user.entity.User;
+import com.doubleowner.revibe.domain.user.repository.UserRepository;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
+
+@Service
+@RequiredArgsConstructor
+public class PointService {
+ private final UserRepository userRepository;
+
+ @Transactional
+ public void addReviewPoint(User user, String image) {
+ int point = PointPolicy.TEXT_ONLY_POINT.getPoint();
+ if (image != null) {
+ point = PointPolicy.TEXT_IMAGE_POINT.getPoint();
+ }
+ User findUser = userRepository.findByEmailOrElseThrow(user.getEmail());
+ findUser.addPoint(point);
+
+
+ }
+
+ @Transactional
+ public void handlePoint(User user, String preImage, MultipartFile afterImage) {
+
+ int pointChange = 0;
+ if (preImage != null && afterImage == null) {
+ pointChange = PointPolicy.TEXT_ONLY_POINT.getPoint() - PointPolicy.TEXT_IMAGE_POINT.getPoint();
+ } else if (preImage == null && afterImage != null) {
+ pointChange = PointPolicy.TEXT_IMAGE_POINT.getPoint() - PointPolicy.TEXT_ONLY_POINT.getPoint();
+ }
+ if (pointChange != 0) {
+ User findUser = userRepository.findByEmailOrElseThrow(user.getEmail());
+ findUser.addPoint(pointChange);
+ }
+ }
+
+ @Transactional
+ public void deletePoint(User user, String image) {
+ int point = (image != null)
+ ? PointPolicy.TEXT_IMAGE_POINT.getPoint()
+ : PointPolicy.TEXT_ONLY_POINT.getPoint();
+
+ User findUser = userRepository.findByEmailOrElseThrow(user.getEmail());
+ findUser.addPoint(-point); // ํฌ์ธํธ ์ฐจ๊ฐ
+ }
+}
+ | Java | _:hammer_and_wrench: Refactor suggestion_
**ํฌ์ธํธ ์ ๋ฆฝ ๋ก์ง ๊ฐ์ ํ์**
ํฌ์ธํธ ์ ๋ฆฝ ๋ก์ง์ ๋ํ ๊ฐ์ ์ฌํญ์ ์ ์๋๋ฆฝ๋๋ค:
1. null ์ฒดํฌ๊ฐ ๋ถ์ถฉ๋ถํฉ๋๋ค.
2. ์ฌ์ฉ์ ์กฐํ์ ํฌ์ธํธ ์ ๋ฆฝ์ด ๋ถ๋ฆฌ๋์ด ์์ง ์์ต๋๋ค.
๋ค์๊ณผ ๊ฐ์ด ๊ฐ์ ํด๋ณด์๋ ๊ฒ์ ์ด๋จ๊น์:
```diff
@Transactional
public void addReviewPoint(User user, String image) {
+ if (user == null) {
+ throw new IllegalArgumentException("์ฌ์ฉ์ ์ ๋ณด๊ฐ ์์ต๋๋ค.");
+ }
int point = PointPolicy.TEXT_ONLY_POINT.getPoint();
if (image != null) {
point = PointPolicy.TEXT_IMAGE_POINT.getPoint();
}
User findUser = userRepository.findByEmailOrElseThrow(user.getEmail());
findUser.addPoint(point);
-
}
```
<!-- suggestion_start -->
<details>
<summary>๐ Committable suggestion</summary>
> โผ๏ธ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
`````suggestion
@Transactional
public void addReviewPoint(User user, String image) {
if (user == null) {
throw new IllegalArgumentException("์ฌ์ฉ์ ์ ๋ณด๊ฐ ์์ต๋๋ค.");
}
int point = PointPolicy.TEXT_ONLY_POINT.getPoint();
if (image != null) {
point = PointPolicy.TEXT_IMAGE_POINT.getPoint();
}
User findUser = userRepository.findByEmailOrElseThrow(user.getEmail());
findUser.addPoint(point);
}
`````
</details>
<!-- suggestion_end -->
<!-- This is an auto-generated comment by CodeRabbit --> |
@@ -0,0 +1,52 @@
+package com.doubleowner.revibe.domain.point;
+
+import com.doubleowner.revibe.domain.user.entity.User;
+import com.doubleowner.revibe.domain.user.repository.UserRepository;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
+
+@Service
+@RequiredArgsConstructor
+public class PointService {
+ private final UserRepository userRepository;
+
+ @Transactional
+ public void addReviewPoint(User user, String image) {
+ int point = PointPolicy.TEXT_ONLY_POINT.getPoint();
+ if (image != null) {
+ point = PointPolicy.TEXT_IMAGE_POINT.getPoint();
+ }
+ User findUser = userRepository.findByEmailOrElseThrow(user.getEmail());
+ findUser.addPoint(point);
+
+
+ }
+
+ @Transactional
+ public void handlePoint(User user, String preImage, MultipartFile afterImage) {
+
+ int pointChange = 0;
+ if (preImage != null && afterImage == null) {
+ pointChange = PointPolicy.TEXT_ONLY_POINT.getPoint() - PointPolicy.TEXT_IMAGE_POINT.getPoint();
+ } else if (preImage == null && afterImage != null) {
+ pointChange = PointPolicy.TEXT_IMAGE_POINT.getPoint() - PointPolicy.TEXT_ONLY_POINT.getPoint();
+ }
+ if (pointChange != 0) {
+ User findUser = userRepository.findByEmailOrElseThrow(user.getEmail());
+ findUser.addPoint(pointChange);
+ }
+ }
+
+ @Transactional
+ public void deletePoint(User user, String image) {
+ int point = (image != null)
+ ? PointPolicy.TEXT_IMAGE_POINT.getPoint()
+ : PointPolicy.TEXT_ONLY_POINT.getPoint();
+
+ User findUser = userRepository.findByEmailOrElseThrow(user.getEmail());
+ findUser.addPoint(-point); // ํฌ์ธํธ ์ฐจ๊ฐ
+ }
+}
+ | Java | _:hammer_and_wrench: Refactor suggestion_
**ํฌ์ธํธ ์ฐจ๊ฐ ๋ก์ง ๊ฐ์ ํ์**
ํฌ์ธํธ ์ฐจ๊ฐ ๋ก์ง์ ๋ํ ๊ฐ์ ์ฌํญ์
๋๋ค:
1. ์ญ์ ์ ํฌ์ธํธ๊ฐ ์์๊ฐ ๋๋ ๊ฒฝ์ฐ์ ๋ํ ์ฒ๋ฆฌ๊ฐ ํ์ํฉ๋๋ค.
2. ์ฝ๋ ๊ฐ๋
์ฑ ๊ฐ์ ์ด ํ์ํฉ๋๋ค.
๋ค์๊ณผ ๊ฐ์ด ๊ฐ์ ํด๋ณด์๋ ๊ฒ์ ์ด๋จ๊น์:
```diff
@Transactional
public void deletePoint(User user, String image) {
+ if (user == null) {
+ throw new IllegalArgumentException("์ฌ์ฉ์ ์ ๋ณด๊ฐ ์์ต๋๋ค.");
+ }
- int point = (image != null)
- ? PointPolicy.TEXT_IMAGE_POINT.getPoint()
- : PointPolicy.TEXT_ONLY_POINT.getPoint();
+ int pointToDeduct = calculatePointToDeduct(image);
User findUser = userRepository.findByEmailOrElseThrow(user.getEmail());
- findUser.addPoint(-point); // ํฌ์ธํธ ์ฐจ๊ฐ
+ if (findUser.getPoint() < pointToDeduct) {
+ throw new IllegalStateException("์ฐจ๊ฐํ ํฌ์ธํธ๊ฐ ๋ณด์ ํฌ์ธํธ๋ณด๋ค ๋ง์ต๋๋ค.");
+ }
+ findUser.addPoint(-pointToDeduct);
}
+private int calculatePointToDeduct(String image) {
+ return image != null
+ ? PointPolicy.TEXT_IMAGE_POINT.getPoint()
+ : PointPolicy.TEXT_ONLY_POINT.getPoint();
+}
```
<!-- suggestion_start -->
<details>
<summary>๐ Committable suggestion</summary>
> โผ๏ธ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
`````suggestion
@Transactional
public void deletePoint(User user, String image) {
if (user == null) {
throw new IllegalArgumentException("์ฌ์ฉ์ ์ ๋ณด๊ฐ ์์ต๋๋ค.");
}
int pointToDeduct = calculatePointToDeduct(image);
User findUser = userRepository.findByEmailOrElseThrow(user.getEmail());
if (findUser.getPoint() < pointToDeduct) {
throw new IllegalStateException("์ฐจ๊ฐํ ํฌ์ธํธ๊ฐ ๋ณด์ ํฌ์ธํธ๋ณด๋ค ๋ง์ต๋๋ค.");
}
findUser.addPoint(-pointToDeduct);
}
private int calculatePointToDeduct(String image) {
return image != null
? PointPolicy.TEXT_IMAGE_POINT.getPoint()
: PointPolicy.TEXT_ONLY_POINT.getPoint();
}
`````
</details>
<!-- suggestion_end -->
<!-- This is an auto-generated comment by CodeRabbit --> |
@@ -1,6 +1,9 @@
package bridge;
+import bridge.domain.constant.MoveCommand;
import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
/**
* ๋ค๋ฆฌ์ ๊ธธ์ด๋ฅผ ์
๋ ฅ ๋ฐ์์ ๋ค๋ฆฌ๋ฅผ ์์ฑํด์ฃผ๋ ์ญํ ์ ํ๋ค.
@@ -18,6 +21,10 @@ public BridgeMaker(BridgeNumberGenerator bridgeNumberGenerator) {
* @return ์
๋ ฅ๋ฐ์ ๊ธธ์ด์ ํด๋นํ๋ ๋ค๋ฆฌ ๋ชจ์. ์ ์นธ์ด๋ฉด "U", ์๋ ์นธ์ด๋ฉด "D"๋ก ํํํด์ผ ํ๋ค.
*/
public List<String> makeBridge(int size) {
- return null;
+ return IntStream
+ .generate(bridgeNumberGenerator::generate)
+ .limit(size)
+ .mapToObj(MoveCommand::convertCommand)
+ .collect(Collectors.toList());
}
} | Java | ์ด ๋ถ๋ถ ๊ฐ์ฒด์งํฅ์ ์ผ๋ก ์ค๊ณ๋ฅผ ์ํ์
จ๋ค์!! ๊ฐ๋
์ฑ ์์ฃผ ๊ตฟ์
๋๋ค |
@@ -0,0 +1,95 @@
+package bridge.controller;
+
+import bridge.domain.model.Bridge;
+import bridge.domain.model.BridgeGame;
+import bridge.domain.constant.MoveCommand;
+import bridge.domain.constant.RetryCommand;
+import bridge.domain.constant.Status;
+import bridge.view.InputView;
+import bridge.view.OutputView;
+
+public class BridgeController {
+
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final BridgeGame bridgeGame;
+
+ public BridgeController() {
+ this.inputView = new InputView();
+ this.outputView = new OutputView();
+ this.bridgeGame = new BridgeGame();
+ }
+
+ public void crossBridge() {
+ outputView.printStartMessage();
+ Bridge bridge = createBridge();
+ startCrossingTheBridge(bridge);
+ }
+
+ private void startCrossingTheBridge(Bridge bridge) {
+ int count = 1;
+ Status status;
+ while (true) {
+ status = startGame(bridge);
+ if (status == Status.FAIL) {
+ if (restartGame() == RetryCommand.RETRY) {
+ count++;
+ continue;
+ }
+ }
+ break;
+ }
+ outputView.printResult(count, status, bridgeGame);
+ }
+
+ private RetryCommand restartGame() {
+ outputView.printRestart();
+ while (true) {
+ try {
+ String command = inputView.readGameCommand();
+ return RetryCommand.from(command);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e);
+ }
+ }
+ }
+
+ private Status startGame(Bridge bridge) {
+ Status status = Status.SUCCESS;
+ for (int idx = 0; idx < bridge.getSize(); idx++) {
+ MoveCommand moveCommand = getMoveCommand();
+ status = bridgeGame.move(idx, moveCommand, bridge);
+ outputView.printMap(bridgeGame);
+ if (bridgeGame.retry(status)) {
+ break;
+ }
+ }
+ return status;
+ }
+
+ private Bridge createBridge() {
+ outputView.printInputSize();
+ while (true) {
+ try {
+ int bridgeSize = inputView.readBridgeSize();
+ Bridge bridge = Bridge.createBridge(bridgeSize);
+ bridge.setBridge(bridgeSize);
+ return bridge;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e);
+ }
+ }
+ }
+
+ private MoveCommand getMoveCommand() {
+ outputView.printInputMoveCommand();
+ while (true) {
+ try {
+ String command = inputView.readMoving();
+ return MoveCommand.from(command);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e);
+ }
+ }
+ }
+} | Java | depth๊ฐ 3์ด๋ค์ ใ
ใ
ํจ์๋ฅผ ๋ถ๋ฆฌํ์
์ผํ ๊ฑฐ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,41 @@
+package bridge.domain.model;
+
+import bridge.domain.constant.MoveCommand;
+import bridge.domain.constant.Status;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class GameDashboard {
+
+ private final List<Status> upResult = new ArrayList<>();
+ private final List<Status> downResult = new ArrayList<>();
+
+ public void updateResult(Status status, MoveCommand moveCommand) {
+ if (status == Status.SUCCESS) {
+ checkResult(status, moveCommand);
+ }
+ if (status == Status.FAIL) {
+ checkResult(status, moveCommand);
+ }
+ }
+
+ private void checkResult(Status status, MoveCommand moveCommand) {
+ if (moveCommand == MoveCommand.UP) {
+ upResult.add(status);
+ downResult.add(Status.SPACE);
+ }
+ if (moveCommand == MoveCommand.DOWN) {
+ upResult.add(Status.SPACE);
+ downResult.add(status);
+ }
+ }
+
+ public List<Status> getUpResult() {
+ return Collections.unmodifiableList(upResult);
+ }
+
+ public List<Status> getDownResult() {
+ return Collections.unmodifiableList(downResult);
+ }
+} | Java | ์ด ๋ถ๋ถ ์ข๋ค์! ์ ๋ Map ์ ์ธ๋ถ๋ก ๋บ์ง ๋ง์ง ๊ณ ๋ฏผํ๋ค๊ฐ ์๋บ๋๋ฐ ๋บ ํ์ Player๊ฐ ๊ฐ์ง๊ณ ์๋ ๊ธฐ๋ฅ๋ค์ ์ฎ๊ฒจ์ฃผ๋๊ฒ ๋ ์ข์์๊ฑฐ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,95 @@
+package bridge.controller;
+
+import bridge.domain.model.Bridge;
+import bridge.domain.model.BridgeGame;
+import bridge.domain.constant.MoveCommand;
+import bridge.domain.constant.RetryCommand;
+import bridge.domain.constant.Status;
+import bridge.view.InputView;
+import bridge.view.OutputView;
+
+public class BridgeController {
+
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final BridgeGame bridgeGame;
+
+ public BridgeController() {
+ this.inputView = new InputView();
+ this.outputView = new OutputView();
+ this.bridgeGame = new BridgeGame();
+ }
+
+ public void crossBridge() {
+ outputView.printStartMessage();
+ Bridge bridge = createBridge();
+ startCrossingTheBridge(bridge);
+ }
+
+ private void startCrossingTheBridge(Bridge bridge) {
+ int count = 1;
+ Status status;
+ while (true) {
+ status = startGame(bridge);
+ if (status == Status.FAIL) {
+ if (restartGame() == RetryCommand.RETRY) {
+ count++;
+ continue;
+ }
+ }
+ break;
+ }
+ outputView.printResult(count, status, bridgeGame);
+ }
+
+ private RetryCommand restartGame() {
+ outputView.printRestart();
+ while (true) {
+ try {
+ String command = inputView.readGameCommand();
+ return RetryCommand.from(command);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e);
+ }
+ }
+ }
+
+ private Status startGame(Bridge bridge) {
+ Status status = Status.SUCCESS;
+ for (int idx = 0; idx < bridge.getSize(); idx++) {
+ MoveCommand moveCommand = getMoveCommand();
+ status = bridgeGame.move(idx, moveCommand, bridge);
+ outputView.printMap(bridgeGame);
+ if (bridgeGame.retry(status)) {
+ break;
+ }
+ }
+ return status;
+ }
+
+ private Bridge createBridge() {
+ outputView.printInputSize();
+ while (true) {
+ try {
+ int bridgeSize = inputView.readBridgeSize();
+ Bridge bridge = Bridge.createBridge(bridgeSize);
+ bridge.setBridge(bridgeSize);
+ return bridge;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e);
+ }
+ }
+ }
+
+ private MoveCommand getMoveCommand() {
+ outputView.printInputMoveCommand();
+ while (true) {
+ try {
+ String command = inputView.readMoving();
+ return MoveCommand.from(command);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e);
+ }
+ }
+ }
+} | Java | ์ด ๋ถ๋ถ์ BridgeGame์์ ์งํํ๋ ๋ฐฉ๋ฒ์ ์์์๊น์??? |
@@ -0,0 +1,64 @@
+package bridge.view;
+
+import static bridge.view.ViewMessage.*;
+
+import bridge.domain.model.BridgeGame;
+import bridge.domain.model.GameDashboard;
+import bridge.domain.constant.Status;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * ์ฌ์ฉ์์๊ฒ ๊ฒ์ ์งํ ์ํฉ๊ณผ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ์ญํ ์ ํ๋ค.
+ */
+public class OutputView {
+
+ public void printError(IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ }
+
+ public void printStartMessage() {
+ System.out.println(START_MESSAGE.getMessage());
+ }
+
+ public void printInputSize() {
+ System.out.println(INPUT_BRIDGE_SIZE_MESSAGE.getMessage());
+ }
+
+ public void printInputMoveCommand() {
+ System.out.println(INPUT_MOVE_COMMAND_MESSAGE.getMessage());
+ }
+
+ public void printRestart() {
+ System.out.println(INPUT_RETRY_COMMAND_MESSAGE.getMessage());
+ }
+
+ /**
+ * ํ์ฌ๊น์ง ์ด๋ํ ๋ค๋ฆฌ์ ์ํ๋ฅผ ์ ํด์ง ํ์์ ๋ง์ถฐ ์ถ๋ ฅํ๋ค.
+ * <p>
+ * ์ถ๋ ฅ์ ์ํด ํ์ํ ๋ฉ์๋์ ์ธ์(parameter)๋ ์์ ๋กญ๊ฒ ์ถ๊ฐํ๊ฑฐ๋ ๋ณ๊ฒฝํ ์ ์๋ค.
+ */
+ public void printMap(BridgeGame bridgeGame) {
+ GameDashboard dashboard = bridgeGame.getGameDashboard();
+ System.out.printf(RESULT_FORMAT.getMessage(), convertToString(dashboard.getUpResult()));
+ System.out.printf(RESULT_FORMAT.getMessage(), convertToString(dashboard.getDownResult()));
+ }
+
+ /**
+ * ๊ฒ์์ ์ต์ข
๊ฒฐ๊ณผ๋ฅผ ์ ํด์ง ํ์์ ๋ง์ถฐ ์ถ๋ ฅํ๋ค.
+ * <p>
+ * ์ถ๋ ฅ์ ์ํด ํ์ํ ๋ฉ์๋์ ์ธ์(parameter)๋ ์์ ๋กญ๊ฒ ์ถ๊ฐํ๊ฑฐ๋ ๋ณ๊ฒฝํ ์ ์๋ค.
+ */
+ public void printResult(int count, Status status, BridgeGame bridgeGame) {
+ System.out.println(RESULT_MESSAGE.getMessage());
+ printMap(bridgeGame);
+ System.out.printf(SUCCESS_OR_FAIL.getMessage(), status.getMessage());
+ System.out.printf(TOTAL_ATTEMPTS_COUNT.getMessage(), count);
+ }
+
+ private String convertToString(List<Status> result) {
+ List<String> convertResult = result.stream().map(Status::getStatus)
+ .collect(Collectors.toList());
+ return String.join(DELIMITER.getMessage(), convertResult);
+ }
+} | Java | ๋งค๊ฐ๊ฐ์ BridgeGame์ด ์๋ GameDashBoard๋ก ๋ฐ์์ผ๋ฉด ์ด๋ ์๊น์??? |
@@ -0,0 +1,23 @@
+package bridge.view;
+
+public enum ViewMessage {
+ START_MESSAGE("๋ค๋ฆฌ ๊ฑด๋๊ธฐ ๊ฒ์์ ์์ํฉ๋๋ค."),
+ INPUT_BRIDGE_SIZE_MESSAGE("๋ค๋ฆฌ์ ๊ธธ์ด๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."),
+ INPUT_MOVE_COMMAND_MESSAGE("์ด๋ํ ์นธ์ ์ ํํด์ฃผ์ธ์. (์: U, ์๋: D)"),
+ INPUT_RETRY_COMMAND_MESSAGE("๊ฒ์์ ๋ค์ ์๋ํ ์ง ์ฌ๋ถ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์. (์ฌ์๋: R, ์ข
๋ฃ: Q)"),
+ RESULT_MESSAGE("์ต์ข
๊ฒ์ ๊ฒฐ๊ณผ"),
+ SUCCESS_OR_FAIL("๊ฒ์ ์ฑ๊ณต ์ฌ๋ถ: %s%n"),
+ TOTAL_ATTEMPTS_COUNT("์ด ์๋ํ ํ์: %d%n"),
+ DELIMITER(" | "),
+ RESULT_FORMAT("[ %s ]%n");
+
+ private final String message;
+
+ ViewMessage(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+} | Java | StringJoiner ๋ฅผ ์ฐ๋ฉด ํด๋น ๋ถ๋ถ์ ํ๋ฒ์ ํด๊ฒฐํ ์ ์๋๋ผ๊ตฌ์! |
@@ -0,0 +1,21 @@
+import { EOL as LINE_SEPARATOR } from 'os';
+import { TIME, UNITS } from './system.js';
+
+export const OUTPUT_MESSAGES = Object.freeze({
+ intro: `์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น ${TIME.month}${UNITS.month} ์ด๋ฒคํธ ํ๋๋์
๋๋ค.`,
+ outro: `์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!`,
+});
+
+export const INPUT_MESSAGES = Object.freeze({
+ date: `${TIME.month}${UNITS.month} ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)${LINE_SEPARATOR}`,
+ order: `์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)${LINE_SEPARATOR}`,
+});
+
+export const ERROR_MESSAGES = Object.freeze({
+ prefix: '[ERROR]',
+ invalidDate: '์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ invalidMenu: '์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ exceedQuantity:
+ '๋ฉ๋ด๋ ํ ๋ฒ์ ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ onlyBeverage: '์๋ฃ๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+}); | JavaScript | ์ด๋ฒคํธ๋ฅผ ์งํํ๋ ๋ฌ์ด ๋ฐ๋๋ ๊ฒ์ ๊ณ ๋ คํด `TIME.month` ์์๋ฅผ ๋ฐ๋ก ์์ฑํ ์ ์ด ์ธ์ ๊น์ด์!
`12์ ์ด๋ฒคํธ ์ฐธ์ฌ ๊ณ ๊ฐ์ 5%๊ฐ ๋ด๋
1์ ์ํด ์ด๋ฒคํธ์ ์ฌ์ฐธ์ฌํ๋ ๊ฒ`
์ด๋ฉ์ผ์ ์ด๋ฌํ ๋ฌธ์ฅ์ด ์ ๋ ๋์ ๋ฐํ๊ธด ํ๋๋ฐ ๊ทธ๋ฅ ๊ธฐ๋ฅ ๊ตฌํ์ ์ง์คํ๋๋ผ ์์ด๋ฒ๋ ธ๊ฑฐ๋ ์..
์ ๋ง ์ฌ์ธํ๊ฒ ๊ณ ๋ฏผํ์๋ฉด์ ๊ตฌํํ์ ๊ฒ ๊ฐ์ต๋๋ค! ๊ฐํ๋ง ๋์ค๋ค์ ๐ฎ |
@@ -0,0 +1,28 @@
+export const TIME = Object.freeze({
+ year: '2023',
+ month: '12',
+ dateFormat: `2023-12-`,
+});
+
+export const PROMOTION_TITLES = Object.freeze({
+ 1: '์ํด',
+ 12: 'ํฌ๋ฆฌ์ค๋ง์ค',
+});
+
+export const SYMBOLS = Object.freeze({
+ comma: ',',
+ dash: '-',
+ colon: ':',
+ blank: '',
+});
+
+export const UNITS = Object.freeze({
+ quantity: '๊ฐ',
+ won: '์',
+ month: '์',
+ year: '๋
',
+});
+
+export const NONE = '์์';
+
+export const FILE_PATH = 'src/data/dec-promotion.json'; | JavaScript | ํ์ฅ์ ๊ณ ๋ คํ์
จ๊ตฐ์! ๋ฐฐ์ฐ๊ณ ๊ฐ๋๋ค ๐
์ค์ ํ์ฌ์๋ค๋ฉด ์ด๋ฒคํธ ๊ธฐํ์๊ฐ ๋๋ฌด ์ข์ํ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,28 @@
+export const TIME = Object.freeze({
+ year: '2023',
+ month: '12',
+ dateFormat: `2023-12-`,
+});
+
+export const PROMOTION_TITLES = Object.freeze({
+ 1: '์ํด',
+ 12: 'ํฌ๋ฆฌ์ค๋ง์ค',
+});
+
+export const SYMBOLS = Object.freeze({
+ comma: ',',
+ dash: '-',
+ colon: ':',
+ blank: '',
+});
+
+export const UNITS = Object.freeze({
+ quantity: '๊ฐ',
+ won: '์',
+ month: '์',
+ year: '๋
',
+});
+
+export const NONE = '์์';
+
+export const FILE_PATH = 'src/data/dec-promotion.json'; | JavaScript | ๊ทธ๋ฅ ๊ฐํ๋ง ๋์ค๋ค์... ํ ๋ง์ด ์๋ค์... ์ต์ข
์ฝํ
์ด๋ฐ๋ถ์ด ๊ฐ์๋๊ตฌ๋... |
@@ -0,0 +1,87 @@
+import VisitDate from '../domain/VisitDate.js';
+import Order from '../domain/Order.js';
+import InputView from '../views/InputView.js';
+import OutputView from '../views/OutputView.js';
+import EventPlanner from '../domain/EventPlanner.js';
+import Save from '../domain/Save.js';
+
+class EventController {
+ #inputView;
+
+ #outputView;
+
+ #dayIndex;
+
+ #eventPlanner;
+
+ constructor() {
+ this.#inputView = InputView;
+ this.#outputView = OutputView;
+ }
+
+ async startEvent() {
+ this.#outputView.printIntro();
+
+ const visitDate = await this.#requestVisitDate();
+ const orderList = await this.#requestOrderList();
+
+ this.#eventPlanner = this.#showEventPlanner(visitDate, orderList);
+ this.#saveResult(this.#eventPlanner);
+ }
+
+ /**
+ * ๋ฐฉ๋ฌธ ๋ ์ง๋ฅผ ์
๋ ฅ ๋ฐ์ ๋ฐํ, ์์ผ ์ธ๋ฑ์ค ๋ฉค๋ฒ ๋ณ์ ํ ๋น
+ * @returns {number} - ์์ผ ์ธ๋ฑ์ค
+ */
+ async #requestVisitDate() {
+ try {
+ const visitDate = await this.#inputView.readDate();
+ this.#dayIndex = new VisitDate(visitDate).getDayIndex();
+
+ return visitDate;
+ } catch ({ message }) {
+ this.#outputView.print(message);
+
+ return this.#requestVisitDate();
+ }
+ }
+
+ /**
+ * ์ฃผ๋ฌธํ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์
๋ ฅ ๋ฐ์ ๊ฐ๊ณตํ ์ฃผ๋ฌธ ๋ฉ๋ด๋ฅผ ๋ฆฌํด
+ * @returns {Set<{ menu: string, quantity: number }>}
+ */
+ async #requestOrderList() {
+ try {
+ const orderInput = await this.#inputView.readOrder();
+
+ return new Order(orderInput).getOrderList();
+ } catch ({ message }) {
+ this.#outputView.print(message);
+
+ return this.#requestOrderList();
+ }
+ }
+
+ /**
+ * ์ ์ ์ ์
๋ ฅ ๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ์ด๋ฒคํธ ํ๋๋๋ฅผ ๊ตฌ์ฑํ๊ณ ์ถ๋ ฅ
+ * @param {number} visitDate - ์์ผ ์ธ๋ฑ์ค
+ * @param {Set<{ menu: string, quantity: number }>} orderList - ์ฃผ๋ฌธ ๋ฉ๋ด์ ์๋์ ๋ํ๋ด๋ Set ๊ฐ์ฒด
+ * @returns
+ */
+ #showEventPlanner(visitDate, orderList) {
+ this.#outputView.printOutro(visitDate);
+ this.#eventPlanner = new EventPlanner(visitDate, this.#dayIndex, orderList);
+ this.#outputView.printPlanner(orderList, this.#eventPlanner);
+
+ return this.#eventPlanner;
+ }
+
+ /**
+ * ์ด๋ฒคํธ ํ๋๋์ ์ฃผ์ ๋ฐ์ดํฐ๋ฅผ Save ํด๋์ค์ ๋๊ฒจ ์ ์ฅ
+ */
+ #saveResult() {
+ new Save(this.#eventPlanner);
+ }
+}
+
+export default EventController; | JavaScript | ์๋ฌ๋ฉ์์ง ์ถ๋ ฅ ๊ธฐ๋ฅ์ `outputView`๊ฐ ์ฑ
์์ ๋งก๋๋ก ํ๊ตฐ์! ๋ฆฌํฉํ ๋ง ํ ๋ ์ ๋ ๋ฐ์ํด๋ด์ผ๊ฒ ์ต๋๋ค ๐ |
@@ -0,0 +1,32 @@
+import { ERROR_MESSAGES } from '../constants/messages.js';
+import { TIME } from '../constants/system.js';
+import validationErrorHandler from '../errors/index.js';
+import { isPositiveInteger, isValidDate } from '../validators/index.js';
+
+class VisitDate {
+ #visitDate;
+
+ constructor(date) {
+ this.#validateDate(date);
+ this.#visitDate = date;
+ }
+
+ #validateDate(date) {
+ if (!isPositiveInteger(date) || !isValidDate(date)) {
+ validationErrorHandler(ERROR_MESSAGES.invalidDate);
+ }
+ }
+
+ getDayIndex() {
+ return this.#calculateDayIndex();
+ }
+
+ // ์ด๋ฒคํธ ์ฐ-์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ฅผ ๊ณ์ฐํ์ฌ ์์ผ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ
+ #calculateDayIndex() {
+ const date = new Date(`${TIME.dateFormat}${this.#visitDate}`);
+
+ return date.getDay();
+ }
+}
+
+export default VisitDate; | JavaScript | ๊ธฐ๋ณธ ์ ๊ณต๋๋ `Date` ๊ฐ์ฒด๋ฅผ ํ์ฉํ ์๊ฐ์ ๋ชปํ๋ค์!
์ด๋ ๊ฒ ์ ๋ฐํ ๋ฐฉ๋ฒ๋ ์๋ค๋ ๊ฒ์ ์์๊ฐ๋๋ค ๐ |
@@ -0,0 +1,9 @@
+import { ERROR_MESSAGES } from '../constants/messages.js';
+
+class AppError extends Error {
+ constructor(message) {
+ super(`${ERROR_MESSAGES.prefix} ${message}`);
+ }
+}
+
+export default AppError; | JavaScript | ์๋ฌ๋ฅผ ์์ํ์
จ๊ตฐ์! ์ ๋ง ์ฒด๊ณ์ ์ธ ์ฝ๋์ธ ๊ฒ ๊ฐ์์!
`Error`๋ฅผ ์์ํด `AppError` ํด๋์ค๋ฅผ ๋ฐ๋ก ๋ง๋์๊ฒ ๋ ๊ณ๊ธฐ๋ฅผ ์ฌ์ญค๋ด๋ ๋ ๊น์? |
@@ -0,0 +1,12 @@
+import MENUS from '../constants/menus.js';
+
+/**
+ * ๋ฉ๋ด ์ด๋ฆ์ ์
๋ ฅํ๋ฉด ์นดํ
๊ณ ๋ฆฌ ๋ด ๋ฉ๋ด์ ๊ฐ๊ฒฉ ๊ฐ์ ๋ฐํ
+ * @param {string} menu - ๋ฉ๋ด ์ด๋ฆ
+ * @returns {number | undefined}
+ */
+const menuPriceFinder = menu => {
+ return Object.values(MENUS).find(category => menu in category)[menu];
+};
+
+export default menuPriceFinder; | JavaScript | ๊ธฐ๋ฅ์ ๊ตฌํํ๋ค๋ณด๋ฉด ๋ฉ๋ด์ ๊ฐ๊ฒฉ์ ์์ฃผ ๊ฐ์ ธ๋ค ์ฐ๊ฒ ๋๋๋ฐ utils ํด๋์ ๋ฐ๋ก ํจ์๋ฅผ ๊ตฌํํด๋๋ ์ข์ ๊ฒ ๊ฐ์์!
๋ฐฐ์๊ฐ๋๋ค ๐ |
@@ -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 | `Intl.NumberFormat` ๋ฅผ ์ฒ์ ์ ํ๋๋ฐ ์ด๋ ๊ฒ ๊ฐ๊ฒฉ์ ํฌ๋งทํ๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ด ์์๊ตฐ์!
์ ๋ ์ ๊ทํํ์์ผ๋ก ๊ตฌํํ๋๋ฐ ์๋ก์ด ๋ฐฉ๋ฒ์ ์์๊ฐ๊ฒ ๋ผ์ ์ข์์! |
@@ -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,57 @@
+import { GIVEAWAYS, TITLES } from '../constants/events.js';
+import { NONE } from '../constants/system.js';
+import menuPriceFinder from '../utils/menuPriceFinder.js';
+
+class Benefit {
+ #isFitGiveaway;
+
+ #benefitList;
+
+ #totalPrice;
+
+ constructor(discountList, isFitGiveaway) {
+ this.#isFitGiveaway = isFitGiveaway;
+ this.#benefitList = this.#setBenefitList(discountList);
+ this.#totalPrice = this.#calculateTotalPrice();
+ }
+
+ /**
+ * ์ฆ์ ์ด๋ฒคํธ ๊ธฐ์ค ์ด์์ด๋ฉด ํํ ๋ด์ญ์ ์ฆ์ ๋ฉ๋ด๋ฅผ ์ถ๊ฐํ๊ณ , ๋ฏธ๋ง์ด๋ฉด '์์'์ ์ถ๊ฐ
+ * @param {Array<{ title: string, price: number }>} benefitList - ํํ ๋ฆฌ์คํธ
+ * @returns
+ */
+ #setBenefitList(discountList) {
+ if (this.#isFitGiveaway) {
+ return [...discountList, this.#createGiveawayBenefit()];
+ }
+
+ return discountList;
+ }
+
+ #createGiveawayBenefit() {
+ return {
+ title: TITLES.giveaway,
+ price: menuPriceFinder(GIVEAWAYS.giveaway),
+ };
+ }
+
+ /**
+ * ํํ ๋ด์ญ์ด '์์'์ด๋ฉด ์ด ๊ธ์ก 0์ ๋ฐํ, ํํ ๋ด์ญ์ด ์์ผ๋ฉด ์ด ๊ธ์ก์ ๋์ฐํ์ฌ ๋ฐํ
+ * @returns
+ */
+ #calculateTotalPrice() {
+ if (this.#benefitList === NONE) return 0;
+
+ return this.#benefitList.reduce((acc, benefit) => acc + benefit.price, 0);
+ }
+
+ getList() {
+ return this.#benefitList;
+ }
+
+ getTotalPrice() {
+ return this.#totalPrice;
+ }
+}
+
+export default Benefit; | JavaScript | 3์ฃผ์ฐจ ๊ณตํต ํผ๋๋ฐฑ์ `ํ๋์ ์๋ฅผ ์ค์ด๊ธฐ ์ํด ๋
ธ๋ ฅํ๋ค`๋ผ๋ ๋ง์ด ์์์ฃ !
`#benefitList`์ `#totalPrice`๋
์์ฑ์์ ์ธ์๋ก ์ ๋ฌ๋ `discountList`, `isFitGiveaway`์ผ๋ก ๋ง๋ค ์ ์๊ธฐ ๋๋ฌธ์
๊ผญ ํ๋๋ก ์ ์ธํด์ผ ํ๋ ๊ฑด์ง์ ๋ํ ์๋ฌธ์ด ์๊น๋๋ค!
ํ๋์ ๊ฐ์๋ฅผ ์ต๋ํ ์ค์ด๋ ๊ฒ ์ ๋ง ์ณ์ ๊ฒ์ธ์ง ๊ณ ๋ฏผ์ด ๋ง์์ง๋ค์
ํฌํ๋ ์๊ฐ์ ์ด๋ ์ ๊ฐ์?! |
@@ -0,0 +1,106 @@
+import { BADGES, GIVEAWAYS } from '../constants/events.js';
+import { NONE } from '../constants/system.js';
+import menuPriceFinder from '../utils/menuPriceFinder.js';
+import Benefit from './Benefit.js';
+import Discount from './Discount.js';
+
+class EventPlanner {
+ #benefit;
+
+ #preTotalPrice;
+
+ #totalBenefitPrice;
+
+ constructor(visitDate, dayIndex, orderList) {
+ this.discount = new Discount(visitDate, dayIndex, orderList);
+ this.#preTotalPrice = this.#setPreTotalPrice(orderList);
+ this.isFitGiveaway = this.#checkIsFitGiveAway();
+ this.#benefit = new Benefit(
+ this.discount.getDiscounts(this.#preTotalPrice),
+ this.isFitGiveaway,
+ );
+ }
+
+ /**
+ * ์ฃผ๋ฌธ ๋ด์ญ์ ์ํํ๋ฉฐ ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก์ ๋ฐํ
+ * @param {Array<{ menu: string, quantity: number }>} orderList - ์ฃผ๋ฌธ ๋ด์ญ
+ * @returns
+ */
+ #setPreTotalPrice(orderList) {
+ return orderList.reduce(
+ (acc, order) => this.#calculateTotalPrice(acc, order),
+ 0,
+ );
+ }
+
+ /**
+ * ํน์ ๋ฉ๋ด์ ๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ณฑํ์ฌ ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก์ ๊ณ์ฐ
+ * @param {number} acc - ๋์ ๊ธ์ก
+ * @param {{ menu: string, quantity: number }} order - ํน์ ์ฃผ๋ฌธ ๋ด์ญ
+ * @returns
+ */
+ #calculateTotalPrice(acc, order) {
+ const price = menuPriceFinder(order.menu);
+
+ return acc + price * order.quantity;
+ }
+
+ getPreTotalPrice() {
+ return this.#preTotalPrice;
+ }
+
+ // ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก๊ณผ ์ฆ์ ์ด๋ฒคํธ ๊ธฐ์ค ๊ธ์ก์ ๋น๊ตํ์ฌ boolean ๋ฐํ
+ #checkIsFitGiveAway() {
+ return this.#preTotalPrice >= GIVEAWAYS.giveawayPrice;
+ }
+
+ // ์ฆ์ ์ด๋ฒคํธ ๊ธฐ์ค ๊ธ์ก ์ด์์ด๋ฉด ์ฆ์ ์ด๋ฒคํธ ์ํ ๋ฐํ, ๋ฏธ๋ง์ด๋ฉด '์์' ๋ฐํ
+ getGiveaway() {
+ return this.isFitGiveaway ? [this.#createGiveaway()] : NONE;
+ }
+
+ #createGiveaway() {
+ return { menu: GIVEAWAYS.giveaway, quantity: GIVEAWAYS.giveawayUnit };
+ }
+
+ getBenefitList() {
+ const benefitList = this.#benefit.getList();
+
+ return benefitList.length ? benefitList : NONE;
+ }
+
+ getTotalBenefitPrice() {
+ this.#totalBenefitPrice = this.#benefit.getTotalPrice();
+
+ return this.#totalBenefitPrice;
+ }
+
+ /**
+ * ์ด ํ ์ธ ๊ธ์ก์ด ์กด์ฌํ๋ฉด ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก ๋ฐํ, ๊ทธ๋ ์ง ์๋ค๋ฉด ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก ๋ฐํ
+ * @returns
+ */
+ getTotalPrice() {
+ const totalDiscountPrice = this.discount.getTotalDiscountPrice();
+
+ return this.#preTotalPrice - totalDiscountPrice;
+ }
+
+ /**
+ * ์ดํํ ๊ธ์ก์ ๋ฐ๋ฅธ ์ด๋ฒคํธ ๋ฐฐ์ง ๋ฐํ
+ * @returns
+ */
+ getBadge() {
+ switch (true) {
+ case this.#totalBenefitPrice > BADGES.santa.price:
+ return BADGES.santa.title;
+ case this.#totalBenefitPrice > BADGES.tree.price:
+ return BADGES.tree.title;
+ case this.#totalBenefitPrice > BADGES.star.price:
+ return BADGES.star.title;
+ default:
+ return NONE;
+ }
+ }
+}
+
+export default EventPlanner; | JavaScript | `switch`๋ฌธ์ ์ฌ์ฉํ๋ ์ ๋ง ๊น๋ํ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,43 @@
+import fs from 'fs';
+import { FILE_PATH } from '../constants/system.js';
+
+class Save {
+ #eventPlanner;
+
+ #filePath;
+
+ constructor(eventPlanner) {
+ this.#eventPlanner = eventPlanner;
+ this.#filePath = FILE_PATH;
+ this.#writeData();
+ }
+
+ // ๊ธฐ์กด JSON ๋ฐ์ดํฐ์ ์ ๊ท ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๊ณ ์ ์ฅ
+ #writeData() {
+ const readData = this.#readData();
+ const userData = {
+ user: readData.length,
+ badge: this.#eventPlanner.getBadge(),
+ total: this.#eventPlanner.getPreTotalPrice(),
+ benefit: this.#eventPlanner.getTotalBenefitPrice(),
+ payment: this.#eventPlanner.getTotalPrice(),
+ };
+ readData.push(userData);
+ const jsonData = JSON.stringify(readData, null, 2);
+
+ fs.writeFileSync(this.#filePath, jsonData, 'utf-8');
+ }
+
+ // ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ ํ์ฑํ์ฌ ๋ฐํ, ๊ธฐ์กด์ ๋ฐ์ดํฐ๊ฐ ์๋ค๋ฉด ๋น ๋ฐฐ์ด ์ถ๊ฐ
+ #readData() {
+ try {
+ const data = fs.readFileSync(this.#filePath, 'utf-8');
+
+ return JSON.parse(data);
+ } catch (error) {
+ return [];
+ }
+ }
+}
+
+export default Save; | JavaScript | ๋ฐฉ๋ฌธํ ์๋๋ค์ ๋ํ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๊ธฐ๋ฅ์ ์๊ฐ ๋ชป ํ๋๋ฐ
๋๋ฌด ์ข์ ์์ด๋์ด์
๋๋ค โจ |
@@ -0,0 +1,21 @@
+import { EOL as LINE_SEPARATOR } from 'os';
+import { TIME, UNITS } from './system.js';
+
+export const OUTPUT_MESSAGES = Object.freeze({
+ intro: `์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น ${TIME.month}${UNITS.month} ์ด๋ฒคํธ ํ๋๋์
๋๋ค.`,
+ outro: `์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!`,
+});
+
+export const INPUT_MESSAGES = Object.freeze({
+ date: `${TIME.month}${UNITS.month} ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)${LINE_SEPARATOR}`,
+ order: `์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)${LINE_SEPARATOR}`,
+});
+
+export const ERROR_MESSAGES = Object.freeze({
+ prefix: '[ERROR]',
+ invalidDate: '์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ invalidMenu: '์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ exceedQuantity:
+ '๋ฉ๋ด๋ ํ ๋ฒ์ ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+ onlyBeverage: '์๋ฃ๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.',
+}); | JavaScript | ์ด๋ฒ ๋ฏธ์
์์๋ ํ์ฅ์ฑ์ ์ ๊ฒฝ์ฐ๊ณ ์ ๋
ธ๋ ฅํ๋๋ฐ ์์๋ด์ฃผ์
์ ๊ฐ์ฌํ ๋ฐ๋ฆ์
๋๋ค! ๐ |
@@ -0,0 +1,32 @@
+import { ERROR_MESSAGES } from '../constants/messages.js';
+import { TIME } from '../constants/system.js';
+import validationErrorHandler from '../errors/index.js';
+import { isPositiveInteger, isValidDate } from '../validators/index.js';
+
+class VisitDate {
+ #visitDate;
+
+ constructor(date) {
+ this.#validateDate(date);
+ this.#visitDate = date;
+ }
+
+ #validateDate(date) {
+ if (!isPositiveInteger(date) || !isValidDate(date)) {
+ validationErrorHandler(ERROR_MESSAGES.invalidDate);
+ }
+ }
+
+ getDayIndex() {
+ return this.#calculateDayIndex();
+ }
+
+ // ์ด๋ฒคํธ ์ฐ-์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ฅผ ๊ณ์ฐํ์ฌ ์์ผ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ
+ #calculateDayIndex() {
+ const date = new Date(`${TIME.dateFormat}${this.#visitDate}`);
+
+ return date.getDay();
+ }
+}
+
+export default VisitDate; | JavaScript | ์๋ง ๋ ์ข์ ๋ฐฉ๋ฒ์ด ์์๊ฒ๋๋ค! :) ์ ๋ `Date` ๊ฐ์ฒด๋ฅผ ์ ํ์ฉํ์ง ๋ชปํ ๊ฒ ๊ฐ์์ ๋ ๊ณต๋ถํด๋ณด๋ ค๊ตฌ์! |
@@ -0,0 +1,9 @@
+import { ERROR_MESSAGES } from '../constants/messages.js';
+
+class AppError extends Error {
+ constructor(message) {
+ super(`${ERROR_MESSAGES.prefix} ${message}`);
+ }
+}
+
+export default AppError; | JavaScript | ์๋ฌด๋๋ `[ERROR]` ๋ผ๋ prefix๋ฅผ ํตํด ์๋ฌ ๋ฉ์์ง๋ฅผ ์ ์ํ๋ ๊ณผ์ ์์ ๋ง๋ค๊ฒ ๋์๋๋ฐ์, ์ง์ ์๋ฌ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฉด ์ถํ์ ๋ ์ฝ๊ฒ ํ์ฅํ ์ ์๋ ์ฅ์ ์ด ์์ง ์์๊น๋ผ๋ ์๊ฐ์ด์์ต๋๋ค! |
@@ -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 | `Intl.NumberFormat` ์์ฑ์๋ฅผ ํตํด ๋ค์ํ ํตํ ์์์ ๋ณํํ ์ ์๋๋ผ๊ตฌ์!
[Intl.NumberFormat](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat)
๋์์ด ๋ ๊น์ถ์ด ๋งํฌ ์ฒจ๋ถํด๋์์ต๋๋ค :) |
@@ -0,0 +1,57 @@
+import { GIVEAWAYS, TITLES } from '../constants/events.js';
+import { NONE } from '../constants/system.js';
+import menuPriceFinder from '../utils/menuPriceFinder.js';
+
+class Benefit {
+ #isFitGiveaway;
+
+ #benefitList;
+
+ #totalPrice;
+
+ constructor(discountList, isFitGiveaway) {
+ this.#isFitGiveaway = isFitGiveaway;
+ this.#benefitList = this.#setBenefitList(discountList);
+ this.#totalPrice = this.#calculateTotalPrice();
+ }
+
+ /**
+ * ์ฆ์ ์ด๋ฒคํธ ๊ธฐ์ค ์ด์์ด๋ฉด ํํ ๋ด์ญ์ ์ฆ์ ๋ฉ๋ด๋ฅผ ์ถ๊ฐํ๊ณ , ๋ฏธ๋ง์ด๋ฉด '์์'์ ์ถ๊ฐ
+ * @param {Array<{ title: string, price: number }>} benefitList - ํํ ๋ฆฌ์คํธ
+ * @returns
+ */
+ #setBenefitList(discountList) {
+ if (this.#isFitGiveaway) {
+ return [...discountList, this.#createGiveawayBenefit()];
+ }
+
+ return discountList;
+ }
+
+ #createGiveawayBenefit() {
+ return {
+ title: TITLES.giveaway,
+ price: menuPriceFinder(GIVEAWAYS.giveaway),
+ };
+ }
+
+ /**
+ * ํํ ๋ด์ญ์ด '์์'์ด๋ฉด ์ด ๊ธ์ก 0์ ๋ฐํ, ํํ ๋ด์ญ์ด ์์ผ๋ฉด ์ด ๊ธ์ก์ ๋์ฐํ์ฌ ๋ฐํ
+ * @returns
+ */
+ #calculateTotalPrice() {
+ if (this.#benefitList === NONE) return 0;
+
+ return this.#benefitList.reduce((acc, benefit) => acc + benefit.price, 0);
+ }
+
+ getList() {
+ return this.#benefitList;
+ }
+
+ getTotalPrice() {
+ return this.#totalPrice;
+ }
+}
+
+export default Benefit; | JavaScript | ์ ๋ ์ฐ์ ๋ชจ๋ ๋ฉ์๋์ ํ๋๋ฅผ `private`์ผ๋ก ์ง์ ํ๊ณ ๋์, ์ดํ์ `public`์ผ๋ก ์ ๊ทผ์ ํด์ผ๋ง ํ ๋ `public`์ผ๋ก ์ ํํ์์ต๋๋ค!
์๋ง ์ด ๊ณผ์ ์์๋ ํด๋์ค ๋ด ๋ฉ์๋๊ฐ `benefitList`์ `totalPrice`๋ฅผ ์ฌ์ฉํจ์ ์์ด์ `public`์ผ๋ก ์ ํํ์ง ์์๋ ๋ ๊ฒ ๊ฐ์์ ๊ทธ๋๋ก ๋์์ต๋๋ค :)
์ ๋ ์ฌ์ค ๊ณตํต ํผ๋๋ฐฑ์ ๋ฐ๊ณ ๋์ ํ๋์ ๊ฐ์๋ฅผ ์ต๋ํ ์ค์ฌ์ผํ๋ ๊ฒ๊ณผ, ์ต๋ํ private ํ๋๋ก ์บก์ํ๋ฅผ ๊ตฌํํ๋ ๊ฒ ์ฌ์ด์์์ ์ค๊ฐ์ ์ ๋ชป์ฐพ๊ฒ ๋๋ผ๊ตฌ์!
์ข์ ํผ๋๋ฐฑ ๊ฐ์ฌ๋๋ฆฝ๋๋ค! `benefitList`์ `totalPrice`๋ฅผ ๊ตณ์ด private ํ๋๋ก ์ ์ธํด์ผํ๋ ๊ทผ๊ฑฐ๊ฐ ์์ผ๋ฉด ์ค์ด๋๊ฒ๋ ๋ง๋ค๊ณ ์๊ฐ์ด ๋ญ๋๋ค. |
@@ -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,14 @@
+package step1;
+
+public class Print {
+
+ private int calculateResult;
+
+ public Print(int calculateResult) {
+ this.calculateResult = calculateResult;
+ }
+
+ public void printResult() {
+ System.out.println("calculateResult = " + calculateResult);
+ }
+} | Java | Print๊ฐ ํ๋์ ๊ณ์ฐ๊ฒฐ๊ณผ๋ฅผ ์ํ๋ก ๊ฐ์ง๋๋ก ๊ตฌํํ์
จ๋ค์!
๋ง์ฝ Print๋ฅผ ํตํด ๋ ๋ค๋ฅธ ๊ณ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํด์ผํ๋ค๋ฉด ์ด๋ป๊ฒ ๋ ๊น์? Print๋ผ๋ ํด๋์ค์ ์ฑ
์์ ๋ง๋ ์ญํ ์ ํ๊ณ ์๋ค๊ณ ๋ณผ ์ ์์๊น์? |
@@ -0,0 +1,50 @@
+package step1;
+
+import java.util.List;
+
+public class Calculator {
+
+ private CalculatorResource calculatorResource;
+
+ public Calculator(String stringCalculatorSentence) {
+ this.calculatorResource = new CalculatorResource(stringCalculatorSentence);
+ }
+
+ public int calculate() {
+
+ List<Integer> numberList = calculatorResource.getNumberArray();
+ List<String> operatorList = calculatorResource.getOperatorArray();
+
+ int operatorListIndex = 0;
+ int result = 0;
+
+ for (int i = 1; i < numberList.size(); i++) {
+
+ result = calcualteInit(numberList, result, i);
+
+ result = getResult(numberList, operatorList, operatorListIndex, result, i);
+ operatorListIndex++;
+ }
+ return result;
+ }
+
+ private int calcualteInit(List<Integer> numberList, int result, int i) {
+ if (i == 1) {
+ result = (numberList.get(i - 1));
+ }
+ return result;
+ }
+
+ private int getResult(List<Integer> numberList, List<String> operatorList, int operatorListIndex, int result, int i) {
+ if (operatorList.get(operatorListIndex).equals("+")) {
+ result = result + (numberList.get(i));
+ } else if (operatorList.get(operatorListIndex).equals("-")) {
+ result = result - (numberList.get(i));
+ } else if (operatorList.get(operatorListIndex).equals("*")) {
+ result = result * (numberList.get(i));
+ } else if (operatorList.get(operatorListIndex).equals("/")) {
+ result = result / (numberList.get(i));
+ }
+ return result;
+ }
+} | Java | java์ Enum ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฉด ์ํ์ ํ์๋ฅผ ํ๊ณณ์์ ๊ด๋ฆฌํ ์ ์๋๋ฐ์!
๋งํฌ๋ฅผ ๋ณด์๊ณ ์ฐธ๊ณ ํ์
์ enum ํด๋์ค๋ฅผ ์ฌ์ฉํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค :)
https://techblog.woowahan.com/2527/ |
@@ -0,0 +1,50 @@
+package step1;
+
+import java.util.List;
+
+public class Calculator {
+
+ private CalculatorResource calculatorResource;
+
+ public Calculator(String stringCalculatorSentence) {
+ this.calculatorResource = new CalculatorResource(stringCalculatorSentence);
+ }
+
+ public int calculate() {
+
+ List<Integer> numberList = calculatorResource.getNumberArray();
+ List<String> operatorList = calculatorResource.getOperatorArray();
+
+ int operatorListIndex = 0;
+ int result = 0;
+
+ for (int i = 1; i < numberList.size(); i++) {
+
+ result = calcualteInit(numberList, result, i);
+
+ result = getResult(numberList, operatorList, operatorListIndex, result, i);
+ operatorListIndex++;
+ }
+ return result;
+ }
+
+ private int calcualteInit(List<Integer> numberList, int result, int i) {
+ if (i == 1) {
+ result = (numberList.get(i - 1));
+ }
+ return result;
+ }
+
+ private int getResult(List<Integer> numberList, List<String> operatorList, int operatorListIndex, int result, int i) {
+ if (operatorList.get(operatorListIndex).equals("+")) {
+ result = result + (numberList.get(i));
+ } else if (operatorList.get(operatorListIndex).equals("-")) {
+ result = result - (numberList.get(i));
+ } else if (operatorList.get(operatorListIndex).equals("*")) {
+ result = result * (numberList.get(i));
+ } else if (operatorList.get(operatorListIndex).equals("/")) {
+ result = result / (numberList.get(i));
+ }
+ return result;
+ }
+} | Java | ๋ฉ์๋์ ํ๋ผ๋ฏธํฐ์ ๊ฐ์๋ฅผ 2๊ฐ ์ดํ๋ก ๋ฆฌํฉํฐ๋งํด๋ณด๋๊ฑด ์ด๋จ๊น์?
ํด๋ฆฐ ์ฝ๋ ์ฑ
์์๋ ๋ฉ์๋์ ํ๋ผ๋ฏธํฐ ๊ฐ์๋ 0~2๊ฐ๊ฐ ์ ์ ํ๋ฉฐ 4๊ฐ ์ด์์ ๋ฌด์กฐ๊ฑด์ ์ผ๋ก ํผํด์ผํ๋ค๊ณ ์ด์ผ๊ธฐํฉ๋๋ค.
ํ๋ผ๋ฏธํฐ์ ๊ฐ์๊ฐ ๋ง์ ์๋ก ํ๋์ ๋ฉ์๋์์ ํ๋ ์ผ์ด ๋ง์์ง ์ ์๊ณ ์ฝ๋๋ฅผ ์ฝ๊ธฐ ์ด๋ ค์์ง๊ธฐ ๋๋ฌธ์
๋๋ค :) |
@@ -0,0 +1,50 @@
+package step1;
+
+import java.util.List;
+
+public class Calculator {
+
+ private CalculatorResource calculatorResource;
+
+ public Calculator(String stringCalculatorSentence) {
+ this.calculatorResource = new CalculatorResource(stringCalculatorSentence);
+ }
+
+ public int calculate() {
+
+ List<Integer> numberList = calculatorResource.getNumberArray();
+ List<String> operatorList = calculatorResource.getOperatorArray();
+
+ int operatorListIndex = 0;
+ int result = 0;
+
+ for (int i = 1; i < numberList.size(); i++) {
+
+ result = calcualteInit(numberList, result, i);
+
+ result = getResult(numberList, operatorList, operatorListIndex, result, i);
+ operatorListIndex++;
+ }
+ return result;
+ }
+
+ private int calcualteInit(List<Integer> numberList, int result, int i) {
+ if (i == 1) {
+ result = (numberList.get(i - 1));
+ }
+ return result;
+ }
+
+ private int getResult(List<Integer> numberList, List<String> operatorList, int operatorListIndex, int result, int i) {
+ if (operatorList.get(operatorListIndex).equals("+")) {
+ result = result + (numberList.get(i));
+ } else if (operatorList.get(operatorListIndex).equals("-")) {
+ result = result - (numberList.get(i));
+ } else if (operatorList.get(operatorListIndex).equals("*")) {
+ result = result * (numberList.get(i));
+ } else if (operatorList.get(operatorListIndex).equals("/")) {
+ result = result / (numberList.get(i));
+ }
+ return result;
+ }
+} | Java | `๊ณ์ฐ๊ธฐ`๊ฐ ํ๋์ `๊ณ์ฐ๊ธฐ ์์`์ ์ํ๋ก ๊ฐ์ง๋๋ก ๊ตฌํํ์
จ๋ค์ !
๋ง์ฝ ๊ณ์ฐ๊ธฐ๊ฐ ๋ ๋ค๋ฅธ ์์์ ๋ํ ๊ณ์ฐ์ ํด์ผํ๋ ์ํฉ์ด๋ผ๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น์?
ํ์ฌ ์ํ์์๋ ํ๋์ ๊ณ์ฐ๊ธฐ ๊ฐ์ฒด๊ฐ ํ๋์ ์์๋ง์ ๊ณ์ฐํ ์ ์๋๋ฐ์.
์ฌ๋ฌ ๊ฐ์ ์์์ ๋ํด ๊ณ์ฐํ ์ ์๋๋ก ๊ฐ์ ํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค :) |
@@ -0,0 +1,64 @@
+package step1;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class CalculatorResource {
+
+ private String sentence;
+ private String[] sentenceArray;
+ private List<Integer> numberArray;
+ private List<String> operatorArray;
+
+ public List<Integer> getNumberArray() {
+ return numberArray;
+ }
+
+ public List<String> getOperatorArray() {
+ return operatorArray;
+ }
+
+ public CalculatorResource(String sentence) {
+ this.sentence = sentence;
+ splitSentence();
+ creatNumberList();
+ creatOperatorList();
+ }
+
+ private void splitSentence() {
+ this.sentenceArray = this.sentence.split(" ");
+ }
+
+ public void creatNumberList() {
+ this.numberArray = new ArrayList<>();
+
+ for (String s : sentenceArray) {
+ char numberCheck = s.charAt(0);
+ validateNumber(numberArray, s, numberCheck);
+ }
+ this.numberArray = numberArray;
+ }
+
+ private void validateNumber(List<Integer> numberArray, String s, char numberCheck) {
+ if (numberCheck > 48 && numberCheck < 58) {
+ this.numberArray.add(Integer.parseInt(s));
+ }
+ }
+
+ public void creatOperatorList() {
+ this.operatorArray = new ArrayList<>();
+
+ for (String s : sentenceArray) {
+ char operatorCheck = s.charAt(0);
+ validateOperator(operatorArray, s, operatorCheck);
+ }
+ }
+
+ private void validateOperator(List<String> operatorArray, String s, char operatorCheck) {
+ if (operatorCheck < 48 || operatorCheck > 58) {
+ this.operatorArray.add(s);
+ }
+ }
+
+
+} | Java | CalculatorResource ํด๋์ค ๋ค์ด๋ฐ์ ์ถ์ํ๊ฐ ๋ง์ด ๋์ด ์๋ ํด๋์ค ๋ช
์
๋๋ค.
ํด๋์ค ๋ด๋ถ๋ฅผ ๋ณด์ง ์์ผ๋ฉด ์ด๋ค ์ํ๋ฅผ ๊ฐ์ง๊ณ ์๋์ง ์ ์ถํ๊ธฐ ํ๋ญ๋๋ค. ์กฐ๊ธ ๋ ๊ตฌ์ฒด์ ์ธ ํด๋์ค ๋ช
์ ๋ง๋ค์ด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์ :) |
@@ -0,0 +1,64 @@
+package step1;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class CalculatorResource {
+
+ private String sentence;
+ private String[] sentenceArray;
+ private List<Integer> numberArray;
+ private List<String> operatorArray;
+
+ public List<Integer> getNumberArray() {
+ return numberArray;
+ }
+
+ public List<String> getOperatorArray() {
+ return operatorArray;
+ }
+
+ public CalculatorResource(String sentence) {
+ this.sentence = sentence;
+ splitSentence();
+ creatNumberList();
+ creatOperatorList();
+ }
+
+ private void splitSentence() {
+ this.sentenceArray = this.sentence.split(" ");
+ }
+
+ public void creatNumberList() {
+ this.numberArray = new ArrayList<>();
+
+ for (String s : sentenceArray) {
+ char numberCheck = s.charAt(0);
+ validateNumber(numberArray, s, numberCheck);
+ }
+ this.numberArray = numberArray;
+ }
+
+ private void validateNumber(List<Integer> numberArray, String s, char numberCheck) {
+ if (numberCheck > 48 && numberCheck < 58) {
+ this.numberArray.add(Integer.parseInt(s));
+ }
+ }
+
+ public void creatOperatorList() {
+ this.operatorArray = new ArrayList<>();
+
+ for (String s : sentenceArray) {
+ char operatorCheck = s.charAt(0);
+ validateOperator(operatorArray, s, operatorCheck);
+ }
+ }
+
+ private void validateOperator(List<String> operatorArray, String s, char operatorCheck) {
+ if (operatorCheck < 48 || operatorCheck > 58) {
+ this.operatorArray.add(s);
+ }
+ }
+
+
+} | Java | ๊ฐ์ฒด๊ฐ ์์ฑ๋ ์ดํ์ ๋ ๋ณ์๋ ์ฌ์ฉ๋์ง ์๊ณ ์๋ค์! ๐ค
๊ทธ๋ ๋ค๋ฉด ๊ณ์ฐ๊ธฐ ์์ํด๋์ค์์ sentence์ sentenceArray๋ฅผ ์ํ๋ก ๊ฐ์ง๊ณ ์์ ํ์๊ฐ ์์๊น์? |
@@ -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 ์ผ๋ก ๊ฐ๋จํ๊ฒ ํํํ ์ ์๋ต๋๋ค >_< |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.