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
![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-11-17 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 10 16 39](https://github.com/parksangchu/java-christmas-6-parksangchu/assets/112257466/f75ebc13-0ec4-4cf1-976c-766f1db54275) ์ƒ์ถ”๋‹˜๊ป˜์„œ ๋ฆฌ๋ทฐ ์ฃผ์…จ๋˜ ๋‚ด์šฉ์— ๋Œ€ํ•ด์„œ, ํ•œ๋ฒˆ ์—ฌ์ญ™๊ณ  ์‹ถ์€ ๋ถ€๋ถ„์ด ์ƒ๊ฒจ ์—ฌ๊ธฐ์—๋„ ๊ธ€์„ ๋‚จ๊ฒจ๋ด…๋‹ˆ๋‹ค. 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 ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค >_<