code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,58 @@ +package com.subject.board.comment; + +import com.subject.board.article.ArticleRepository; +import com.subject.board.entity.ArticleEntity; +import com.subject.board.entity.CommentEntity; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class CommentService { + private final ArticleRepository articleRepository; + private final CommentRepository commentRepository; + + // Create + public void create( + String content, + String password, + Long articleId) { + Optional<ArticleEntity> article = articleRepository.findById(articleId); + + CommentEntity comment = new CommentEntity(); + comment.setContent(content); + comment.setPassword(password); + comment.setArticle(article.orElse(null)); + commentRepository.save(comment); + } + + // Read + // ๊ฒŒ์‹œ๊ธ€๋ณ„ ๋Œ“๊ธ€ ์กฐํšŒ + public List<CommentEntity> findByArticleId(Long articleId) { + return commentRepository.findByArticleId(articleId); + } + + public CommentEntity readOne(Long commentId) { + return commentRepository.findById(commentId) + .orElse(null); + } + + // Delete + public void delete(Long commentId) { + commentRepository.deleteById(commentId); + } + + // ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ + public Boolean checkPassword( + Long commentId, + String inputPassword + ) { + String passwrod = readOne(commentId).getPassword(); + if (passwrod.equals(inputPassword)) { + return true; + } else return false; + } +}
Java
password ์˜คํƒ€์žˆ์Šต๋‹ˆ๋‹ค. return password.equals(inputPassword); ๋กœ ์ค„์ผ์ˆ˜ ์žˆ์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,41 @@ +INSERT INTO board_entity (board_name) +VALUES + ('์ž์œ  ๊ฒŒ์‹œํŒ'), + ('๊ฐœ๋ฐœ ๊ฒŒ์‹œํŒ'), + ('์ผ์ƒ ๊ฒŒ์‹œํŒ'), + ('์‚ฌ๊ฑด์‚ฌ๊ณ  ๊ฒŒ์‹œํŒ'); + +INSERT INTO article_entity (title, content, password, board_id) +VALUES + ('์ˆ™๋Œ€์ž…๊ตฌ์—ญ ๋ง›์ง‘ ์ถ”์ฒœ', '์ˆ™๋Œ€์ž…๊ตฌ์—ญ ๊ทผ์ฒ˜์— ์žˆ๋Š” ํ‰ํ™”๋‚จ์˜ ์‹๋‹น์ด ๋ง›์žˆ์–ด์š”!', '1234', 1), + ('์—ฐ์•  ํ”„๋กœ๊ทธ๋žจ์„ ๋ณด๋ฉด์„œ..', '๊ฐœ๋ฐœ์ž๋“ค์„ ์œ„ํ•œ ์˜ˆ๋Šฅ ํ”„๋กœ๊ทธ๋žจ์ด ์žˆ์—ˆ์œผ๋ฉด ์ข‹๊ฒ ๋‹ค.. ์•„ ๊ทผ๋ฐ ๊ฐœ๋ฐœ์ž๋“ค์ด ์นด๋ฉ”๋ผ ์•ž์— ์„ค๋ฆฌ๊ฐ€ ์—†์ง€ ใ…‹ใ…‹ใ…‹ใ…‹', '1234', 1), + ('GPT 3.5๋ฅผ ์“ฐ๋ฉด์„œ..', '๋‹Œ ๊ทธ๋ƒฅ ์ฝ”๋”ฉ ํ•˜์ง€ ๋ง์•„๋ผ ์† ํ„ฐ์ง„๋‹ค.', '1234', 2), + ('JS์™€ Java๋ฅผ ๊ณต๋ถ€ํ•˜๋‹ˆ๊นŒ', '๋„ˆ๋ฌด ํ—ท๊ฐˆ๋ ค์š” ใ…‹ใ…‹ใ…‹ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋„ฃ์„ ๋•Œ ์ž๊พธ ํƒ€์ž…์„ ์•ˆ์”€;;', '1234', 2), + ('๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ style ์ฝ”๋“œ ๋ฆฌ๋ทฐ', '์˜ค๋Š˜์€ ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ์•Œ์•„๋ณผ๊ฒŒ์š”! ์ €๋„ ์ฐธ ๊ถ๊ธˆํ•˜๋„ค์š”!', '1234', 2), + ('์š”์ฆ˜ ๊ทผํ™ฉ', '๋ฐฅ ๋จน์–ด ์ฝ”๋”ฉ ํ•ด ๋˜ ๋ฐฅ ๋จน์–ด ์ฝ”๋”ฉ ํ•ด ์œ ํŠœ๋ธŒ ๋ด ๋˜ ์ฝ”๋”ฉ ํ•ด', '1234', 3), + ('์š”์ฆ˜ ๊ทผํ™ฉ', '๋ฐฅ๋ฐฅ๋ฐฅ๋ฐฅ ์˜ค๋Š˜ ์ €๋…์€ ์ œ์œก๋ณถ์Œ~~', '1234', 3), + ('์‚ฌ๊ฑด์‚ฌ๊ณ ? ๊ทธ๋Ÿฐ๊ฑด ๋‚ด ์‚ฌ์ „์— ์—†๋‹ค.', '์™œ๋ƒํ•˜๋ฉด ์ฝ”๋”ฉ์„ ํ•œ ์ค„๋„ ์•ˆ์น˜๊ธฐ ๋•Œ๋ฌธ์ด์ง€ ํ•˜ํ•˜ํ•˜!', '1234', 4), + ('์•ผ ์—๋Ÿฌ ๋œจ๋Š” ๋†ˆ๋“ค ๋ด๋ผ ใ…‹ใ…‹ใ…‹', '์—๋Ÿฌ? ์•„๋‹ˆ ์ปดํ“จํ„ฐ ๋„ˆ๊ฐ€ ํ‹€๋ฆฌ๊ณ  ๋‚ด๊ฐ€ ๋งž์–ด', '1234', 4); + +INSERT INTO comment_entity(content, password, article_id) +VALUES + ('ํ‰ํ™”๋‚จ์˜ ๊ฑฐ๊ธฐ ๋ง›์žˆ์–ด์š”!', '1234', 1), + ('์ธํ…Œ๋ฆฌ์–ด๊ฐ€ ์ด๋ป์š”!', '1234', 1), + ('๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ์˜ˆ๋Šฅ์ด๋ผ๋‹ˆ.. ์•„์•„ ๊ฟˆ์„ ๊พธ์—ˆ์Šต๋‹ˆ๋‹ค..', '1234', 2), + ('์•„์ฃผ ํ–‰๋ณตํ•œ ๊ฟˆ์ด์—ˆ์Šต๋‹ˆ๋‹ค..', '1234', 2), + ('๊ทผ๋ฐ ์™œ ์šฐ๋Š” ๊ฒƒ์ด๋ƒ', '1234', 2), + ('๊ทธ๊ฑด ์ด๋ค„์งˆ ์ˆ˜ ์—†๋Š” ๊ฟˆ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.', '1234', 2), + ('GPT๋Š” ์˜คํƒ€ ์žก๊ธฐ ์ตœ๊ณ ๋ผ๊ณ  ใ…‹ใ…‹ใ…‹ใ…‹', '1234', 3), + ('๋ฐฑ์—”๋“œ์˜ ๊ทผ๋ณธ์€ Java์ด์ง€ ใ…‹ใ…‹ใ…‹ใ…‹', '1234', 4), + ('js๋Š” ๊ทผ๋ณธ์ด ์—†๋‹ค ์•„์ž…๋‹ˆ๊บผ ๊ทผ๋ณธ์ด! ', '1234', 4), + ('์ด์™• ์ด๋ ‡๊ฒŒ ๋œ๊ฑฐ ์ฝ”ํ‹€๋ฆฐ์œผ๋กœ ๊ฐ„๋‹ค.', '1234', 4), + ('์‹คํŒจํ•˜๋ฉด ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์„ฑ๊ณตํ•˜๋ฉด ๋™์ ์–ธ์–ด ์ตœ๊ณ ์•„๋‹™๋‹ˆ๊นŒ!.', '1234', 4), + ('ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋„ Java๋ž‘ ํƒ€์ž… ์“ฐ๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฆ„ ใ…‹ใ…‹ใ…‹ใ…‹ ์ˆ˜๊ณ ์š”.', '1234', 4), + ('๋น„๋ฐ€ ๋Œ“๊ธ€์ž…๋‹ˆ๋‹ค.', '1234', 5), + ('๋น„๋ฐ€ ๋Œ“๊ธ€์ž…๋‹ˆ๋‹ค.', '1234', 5), + ('๊ฐœ๋ฐœ ๊ณต๋ถ€ํ•˜๋ฉด ์›๋ž˜ ๋ฐ˜์ฏค ๋ฏธ์ณ๊ฐ€๋‚˜์š”?', '1234', 5), + ('์ผ์ƒ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํผ ๋ฏธ์ณค๋‹ค', '1234', 6), + ('๋„ˆ ์•„๋ž˜ ๊ธ€ ์“ด ๋†ˆ์ด์ง€?', '1234', 7), + ('๊ฐœ๋ฐœ ๊ณต๋ถ€ํ•˜๋ฉด ์›๋ž˜ ๋ฐ˜์ฏค ๋ฏธ์น˜๋‚˜์š”?', '1234', 7), + ('๊ทธ๋Ÿฐ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋„ค ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹', '1234', 8), + ('404 Not Found๋ผ๊ณ ? ๊ทธ๋Ÿผ ์ฐพ์–ด!', '1234', 9);
Unknown
๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ์–ด์„œ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์ข‹์•˜์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,147 @@ +package com.subject.board.article; + +import com.subject.board.board.BoardService; +import com.subject.board.comment.CommentService; +import com.subject.board.entity.ArticleEntity; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; + +import java.util.ArrayList; +import java.util.List; + + +@Controller +@RequestMapping("/article") +@RequiredArgsConstructor +public class ArticleController { + private final ArticleService articleService; + private final BoardService boardService; + private final CommentService commentService; + + // Create + // ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ฑ view๋กœ ์ด๋™ + @GetMapping("/create") + public String createPage() { + return "article/create"; + } + + // create ๋กœ์ง + @PostMapping("/create") + public String create( + @RequestParam("board-id") Long boardId, + @RequestParam("title") String title, + @RequestParam("content") String content, + @RequestParam("password") String password + ) { + articleService.create(boardId, title, content, password); + return "redirect:/article"; + } + + // Read + // ์ „์ฒด ๋ณด๊ธฐ(= ์ „์ฒด ๊ฒŒ์‹œํŒ) + @GetMapping + public String readAll(Model model) { + model.addAttribute("articles", articleService.readAll()); + return "home"; + } + + // ์ƒ์„ธ ๋ณด๊ธฐ + @GetMapping("/{articleId}") + public String readOne( + @PathVariable("articleId") Long articleId, + Model model + ) { + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("comments", commentService.findByArticleId(articleId)); + return "article/read"; + } + + // Update + // ๋น„๋ฐ€๋ฒˆํ˜ธ view + @GetMapping("/{articleId}/password-view/update") + public String passwordViewUpdate( + @PathVariable("articleId") Long articleId, + Model model + ) { + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("type", "article"); + model.addAttribute("method", "update"); + return "password"; + } + + // ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ ํ›„ -> update view๋กœ ์ด๋™ + @PostMapping("/{articleId}/passwordCheck/update") + public String checkPasswordUpdate( + @PathVariable("articleId") Long articleId, + @RequestParam("input-password") String inputPassword, + RedirectAttributes redirectAttributes, + Model model + ) { + if (articleService.checkPassword(articleId, inputPassword)) { + // ๋น„๋ฐ€๋ฒˆํ˜ธ ์ผ์น˜ + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("boards", boardService.readAll()); + return "article/update"; + } else { + // ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ถˆ์ผ์น˜ + redirectAttributes.addFlashAttribute("error", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + return "redirect:/article/" + articleId + "/password-view/update"; // ๋‹ค์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ + } + } + + // update ์‹คํ–‰ + @PostMapping("/{articleId}/update") + public String update( + @PathVariable("articleId") Long articleId, + @RequestParam("board-id") Long boardId, + @RequestParam("title") String title, + @RequestParam("content") String content + ) { + articleService.update(articleId, boardId, title, content); + return String.format("redirect:/article/%d", articleId); + } + + // Delete + // ๋น„๋ฐ€๋ฒˆํ˜ธ view + @GetMapping("/{articleId}/password-view/delete") + public String passwordViewDelete( + @PathVariable("articleId") Long articleId, + Model model + ) { + model.addAttribute("article", articleService.readOne(articleId)); + model.addAttribute("type", "article"); + model.addAttribute("method", "delete"); + return "password"; + } + + // ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜๋ฉด ์‚ญ์ œ, ํ‹€๋ฆฌ๋ฉด ๊ฒฝ๊ณ ์ฐฝ + @PostMapping("/{articleId}/passwordCheck/delete") + public String checkPasswordDelete( + @PathVariable("articleId") Long articleId, + @RequestParam("input-password") String inputPassword, + RedirectAttributes redirectAttributes, + Model model + ) { + if (articleService.checkPassword(articleId, inputPassword)) { + articleService.delete(articleId); + return "redirect:/article"; + } else { + redirectAttributes.addFlashAttribute("error", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + return "redirect:/article/" + articleId + "/password-view/delete"; // ๋‹ค์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ + } + } + + // Search + @PostMapping("/search") + public String searchArticle( + @RequestParam("category") String category, + @RequestParam("search") String search, + Model model + ) { + model.addAttribute("articles", articleService.search(category, search)); + return "article/searchArticle"; + } +}
Java
ArticleEntity๊ฐ€ ์ด๋ฏธ Comment ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด comments๋ฅผ ๋ชจ๋ธ๋กœ ๋”ฐ๋กœ ๋„˜๊ธฐ์ง€ ์•Š์•„๋„ ๋ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,54 @@ +package christmas.constant; + +public final class CommentConstants { + public static final String EVENT_INFORMATION_MESSAGE = """ + ์šฐํ…Œ์ฝ” ์‹๋‹น์˜ ์ตœ๋Œ€ ์ด๋ฒคํŠธ! 12์›” ์ด๋ฒคํŠธ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค! + ์ตœ๋Œ€ 3๋ฒˆ์˜ ์ค‘๋ณต ํ• ์ธ๊ณผ ์šฐํ…Œ์ฝ”๊ฐ€ ์ค€๋น„ํ•œ ๋‘ ๊ฐœ์˜ ๊นœ์ง ์„ ๋ฌผ์„ ๋ฐ›์•„ ๊ฐ€์„ธ์š”! + * ๋ชจ๋“  ์ด๋ฒคํŠธ๋Š” ํƒ€ ์ด๋ฒคํŠธ์™€ ์ค‘๋ณต ์ ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. + + ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด๋ฅผ ๊ฐ™์ด ์„ธ์–ด๋ณด์•„์š”. + ์ตœ๋Œ€ 3400์› ํ• ์ธ! 1์ผ 1000์›์œผ๋กœ ์‹œ์ž‘ํ•˜์—ฌ 25์ผ๊นŒ์ง€ 100์›์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ํ• ์ธ! + ํฌ๋ฆฌ์Šค๋งˆ์Šค์˜ ์„ ๋ฌผ์„ ๋А๊ปด๋ณด์„ธ์š”! + * 25์ผ๊นŒ์ง€ + + ์ง€๋‚˜๊ฐ€๋Š” 2023๋…„์„ ์œ„ํ•œ, 2,023์› ํ• ์ธ ์ด๋ฒคํŠธ! + ๊ธˆ์š”์ผ, ํ† ์š”์ผ์—๋Š” ๋ฉ”์ธ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์›์„ ํ• ์ธํ•ฉ๋‹ˆ๋‹ค! + ๋‹ค๋ฅธ ๋‚  ๋ฐฉ๋ฌธํ•ด๋„ ์„ญ์„ญํ•ดํ•˜์ง€ ๋งˆ์„ธ์š”. + ์ผ์š”์ผ๋ถ€ํ„ฐ ๋ชฉ์š”์ผ๊นŒ์ง€๋Š” ๋‹ฌ์ฝคํ•œ ๋””์ €ํŠธ๋ฅผ 1๊ฐœ๋‹น 2,023์›์˜ ํ˜œํƒ์„ ๋ฐ›์•„๋ณด์„ธ์š”. + + ๋ณ„์ด ๋œจ๋Š” ๋‚ ์—๋Š” ํ–‰์šด์ด ์ฐพ์•„์˜ฌ ๊ฑฐ์•ผ + ๋‹ฌ๋ ฅ์— ๋ณ„์ด ๋œฌ ๋‚ ์—๋Š” 1,000์›์˜ ์ถ”๊ฐ€ํ• ์ธ์ด ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค! + + ํŠน๋ณ„ํ•œ ๋‚ ์„ ๋” ํŠน๋ณ„ํ•˜๊ฒŒ, ์ƒดํŽ˜์ธ๊ณผ ํ•จ๊ป˜ํ•˜๋Š” ์šฐ์•„ํ•œ ์‹œ๊ฐ„ + ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก 12๋งŒ ์› ์ด์ƒ ์‹œ ์ƒดํŽ˜์ธ์„ ์ œ๊ณตํ•ด ๋“œ๋ฆฝ๋‹ˆ๋‹ค. + * ํ• ์ธ ์ „ ๊ธˆ์•ก ๊ธฐ์ค€ + + 1์„ 2์กฐ, ํ˜œํƒ๋„ ๋ฐ›๊ณ , ์ƒˆํ•ด์„ ๋ฌผ๋„ ๋ฐ›์ž! + ํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ผ ๋ฐ›๋Š” ์ด๋ฒคํŠธ ๋ฐฐ์ง€๋กœ 1์›” ์ƒˆํ•ด ์„ ๋ฌผ ๋ฐ›์•„ ๊ฐ€์„ธ์š”! ์„ ๋ฌผ์€ ์ƒˆํ•ด์ด๋ฒคํŠธ์— ๊ณต๊ฐœ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. + * 5์ฒœ ์› ์ด์ƒ : ๋ณ„, 1๋งŒ ์› ์ด์ƒ : ํŠธ๋ฆฌ, 2๋งŒ ์› ์ด์ƒ : ์‚ฐํƒ€ + + * ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก 10,000์› ์ด์ƒ๋ถ€ํ„ฐ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. + * ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ์‹œ, ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. + * ๋ฉ”๋‰ด๋Š” ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 20๊ฐœ๊นŒ์ง€๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + ------------------------ ์šฐํ…Œ์ฝ” ์‹๋‹น ๋ฉ”๋‰ด ------------------------ + + <์• ํ”ผํƒ€์ด์ €> + ์–‘์†ก์ด์ˆ˜ํ”„(6,000), ํƒ€ํŒŒ์Šค(5,500), ์‹œ์ €์ƒ๋Ÿฌ๋“œ(8,000) + + <๋ฉ”์ธ> + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ(55,000), ๋ฐ”๋น„ํ๋ฆฝ(54,000), ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€(35,000), ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€(25,000) + + <๋””์ €ํŠธ> + ์ดˆ์ฝ”์ผ€์ดํฌ(15,000), ์•„์ด์Šคํฌ๋ฆผ(5,000) + + <์Œ๋ฃŒ> + ์ œ๋กœ์ฝœ๋ผ(3,000), ๋ ˆ๋“œ์™€์ธ(60,000), ์ƒดํŽ˜์ธ(25,000) + + --------------------------------------------------------------- + ์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค. + """; + + private CommentConstants() { + } +} \ No newline at end of file
Java
์—‡ ์ด๊ฑด ํ˜น์‹œ ์‚ฌ์šฉ๋˜๋Š” ์•Š๋Š” ๋ฉ”์‹œ์ง€์ผ๊นŒ์š”!?
@@ -0,0 +1,11 @@ +package christmas.domain.discount; + +import christmas.constant.DiscountPolicyName; +import christmas.service.dto.OrderDto; + +public interface DiscountPolicy { + + int discount(final OrderDto order); + + DiscountPolicyName getDiscountPolicyName(); +} \ No newline at end of file
Java
์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์ž˜ ์‚ฌ์šฉํ•˜์‹  ๊ฑฐ ๊ฐ™์•„์š”! ๊ฐ ํ• ์ธ์— ๋Œ€ํ•ด ํ™•์žฅ์ด๋‚˜ ๋ณ€๊ฒฝ์ด ์‰ฌ์šธ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,52 @@ +package christmas.domain.menu; + +import java.util.Arrays; + +public enum Menu { + + MUSHROOM_SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6_000, MenuCategory.APPETIZER), + TAPAS("ํƒ€ํŒŒ์Šค", 5_500, MenuCategory.APPETIZER), + CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000, MenuCategory.APPETIZER), + + T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000, MenuCategory.MAIN), + BBQ_RIB("๋ฐ”๋น„ํ๋ฆฝ", 54_000, MenuCategory.MAIN), + SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000, MenuCategory.MAIN), + CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000, MenuCategory.MAIN), + + CHOCOLATE_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15_000, MenuCategory.DESSERT), + ICE_CREAM("์•„์ด์Šคํฌ๋ฆผ", 5_000, MenuCategory.DESSERT), + + ZERO_COLA("์ œ๋กœ์ฝœ๋ผ", 3_000, MenuCategory.DRINK), + RED_WINE("๋ ˆ๋“œ์™€์ธ", 60_000, MenuCategory.DRINK), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, MenuCategory.DRINK); + + private static final String INVALID_ORDER_MESSAGE = "์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private final String name; + private final int price; + private final MenuCategory menuCategory; + + Menu(final String name, final int price, final MenuCategory menuCategory) { + this.name = name; + this.price = price; + this.menuCategory = menuCategory; + } + + public static Menu findByName(final String name) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(name)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(INVALID_ORDER_MESSAGE)); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public MenuCategory getMenuCategory() { + return menuCategory; + } +} \ No newline at end of file
Java
์š” ์ฝ”๋“œ๋Š” ์ €๋ž‘ ๊ฑฐ์˜ ๋˜‘๊ฐ™๋„ค์š” ใ…Žใ…Ž ์ด๋ฆ„๋„ ๋˜‘๊ฐ™์€๋ฐ ์‹ ๊ธฐํ•˜๋„ค์š” ใ…‹ใ…‹ใ…‹ ๋ฉ”๋‰ด์— ๋Œ€ํ•œ ์ถ”๊ฐ€์ ์ธ ๋กœ์ง์ด ์—†์–ด์„œ ์ด๋ ‡๊ฒŒ ๋ฉ”๋‰ด๋ฅผ ๋ชจ์•„๋†“๋Š” ๊ฒŒ ๋” ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,77 @@ +package christmas.domain.discount; + +import christmas.constant.DiscountConstants; +import christmas.constant.DiscountPolicyName; +import christmas.domain.menu.Menu; +import christmas.domain.menu.MenuCategory; +import christmas.service.dto.OrderDto; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.Map; +import java.util.Map.Entry; + + +public class WeekendDiscountPolicy implements DiscountPolicy { + private static final int WEEKEND_DISCOUNT_AMOUNT = 2023; + + @Override + public int discount(final OrderDto order) { + LocalDate orderDate = order.getDate(); + if (isWithinDiscountPeriod(orderDate) && isWeekend(orderDate)) { + return calculateTotalDiscount(order.getMenus()); + } + return DiscountConstants.NO_DISCOUNT; + } + + @Override + public DiscountPolicyName getDiscountPolicyName() { + return DiscountPolicyName.WEEKEND_DISCOUNT; + } + + private int calculateTotalDiscount(Map<Menu, Integer> menus) { + int totalDiscount = 0; + for (Entry<Menu, Integer> menu : menus.entrySet()) { + totalDiscount += calculateDiscountForMenu(menu); + } + return totalDiscount; + } + + private int calculateDiscountForMenu(Entry<Menu, Integer> menu) { + if (isMainCategory(menu.getKey())) { + return calculateMenuDiscount(menu); + } + return 0; + } + + private boolean isMainCategory(Menu menu) { + return menu.getMenuCategory() == MenuCategory.MAIN; + } + + private int calculateMenuDiscount(Map.Entry<Menu, Integer> menu) { + return WEEKEND_DISCOUNT_AMOUNT * menu.getValue(); + } + + private boolean isWithinDiscountPeriod(LocalDate date) { + return isNotBeforeStartDate(date) && isNotAfterEndDate(date); + } + + private boolean isNotBeforeStartDate(LocalDate date) { + return !date.isBefore(DiscountConstants.START_DAY_OF_MONTH); + } + + private boolean isNotAfterEndDate(LocalDate date) { + return !date.isAfter(DiscountConstants.END_DAY_OF_MONTH); + } + + private boolean isWeekend(LocalDate date) { + return isFriday(date) || isSaturday(date); + } + + private boolean isFriday(LocalDate date) { + return date.getDayOfWeek() == DayOfWeek.FRIDAY; + } + + private boolean isSaturday(LocalDate date) { + return date.getDayOfWeek() == DayOfWeek.SATURDAY; + } +} \ No newline at end of file
Java
DayOfWeek ์‚ฌ์šฉ๋ฒ• ์ €๋งŒ ๋ชฐ๋ž๊ตฐ์š”.. ๋˜๊ฒŒ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฑฐ ๊ฐ™์•„์š”! ๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,74 @@ +package christmas.domain; + +import christmas.constant.DiscountConstants; +import christmas.constant.DiscountPolicyName; +import christmas.domain.discount.ChristmasDailyDiscountPolicy; +import christmas.domain.discount.DiscountPolicy; +import christmas.domain.discount.GiftEventPolicy; +import christmas.domain.discount.SpecialDiscountPolicy; +import christmas.domain.discount.WeekdayDiscountPolicy; +import christmas.domain.discount.WeekendDiscountPolicy; +import christmas.domain.menu.Menu; +import christmas.service.dto.OrderDto; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; + +public class ChristmasEvent { + private static final int MINIMUM_AMOUNT_FOR_DISCOUNT = 10000; + private static final int MINIMUM_AMOUNT_FOR_GIFT = 120000; + private final List<DiscountPolicy> discountPolicies; + private final GiftEventPolicy giftEventPolicy; + private final Map<Menu, Integer> giftMenu; + + public ChristmasEvent() { + this.discountPolicies = List.of( + new ChristmasDailyDiscountPolicy(), + new WeekdayDiscountPolicy(), + new WeekendDiscountPolicy(), + new SpecialDiscountPolicy()); + this.giftEventPolicy = new GiftEventPolicy(); + this.giftMenu = new EnumMap<>(Menu.class); + } + + public Map<DiscountPolicyName, Integer> calculateBenefitDetails(final OrderDto order) { + Map<DiscountPolicyName, Integer> benefitDetail = new EnumMap<>(DiscountPolicyName.class); + if (order.getTotalPrice() >= MINIMUM_AMOUNT_FOR_DISCOUNT) { + benefitDetail = applyDiscountPolicies(order); + addGift(order, benefitDetail); + } + return benefitDetail; + } + + public int calculateTotalDiscount(final OrderDto order) { + Map<DiscountPolicyName, Integer> discountDetail = calculateBenefitDetails(order); + return discountDetail.values() + .stream() + .mapToInt(Integer::intValue) + .sum(); + } + + private void addGift(final OrderDto order, final Map<DiscountPolicyName, Integer> benefitDetail) { + if (order.getTotalPrice() >= MINIMUM_AMOUNT_FOR_GIFT) { + int giftPrice = giftEventPolicy.discountGiftPrice(order); + benefitDetail.put(DiscountPolicyName.GIFT_EVENT, giftPrice); + giftMenu.put(Menu.CHAMPAGNE, 1); + } + } + + private Map<DiscountPolicyName, Integer> applyDiscountPolicies(final OrderDto order) { + Map<DiscountPolicyName, Integer> discountDetail = new EnumMap<>(DiscountPolicyName.class); + for (DiscountPolicy discountPolicy : discountPolicies) { + int discountAmount = discountPolicy.discount(order); + if (discountAmount != DiscountConstants.NO_DISCOUNT) { + discountDetail.put(discountPolicy.getDiscountPolicyName(), discountAmount); + } + } + return discountDetail; + } + + public Map<Menu, Integer> getGiftMenu(final OrderDto order) { + calculateBenefitDetails(order); + return giftMenu; + } +} \ No newline at end of file
Java
์ฆ์ • ๋ฉ”๋‰ด์— ๋Œ€ํ•ด์„œ ํ™•์žฅ์„ฑ์ด ๋ณด์—ฌ์„œ ์ข‹์€ ๊ฑฐ ๊ฐ™์•„์š”! ๐Ÿ‘
@@ -0,0 +1,115 @@ +package christmas.controller; + +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import christmas.model.strategy.WootecoBadgeStrategy; +import christmas.model.strategy.WootecoDiscountStrategy; +import christmas.service.PromotionService; +import christmas.util.Parser; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.EnumMap; +import java.util.Map; +import java.util.function.Supplier; + +public class PromotionController { + private final PromotionService promotion; + private final DiscountStrategy discountStrategy = new WootecoDiscountStrategy(); + private final BadgeStrategy badgeStrategy = new WootecoBadgeStrategy(); + + public PromotionController() { + this.promotion = new PromotionService(discountStrategy, badgeStrategy); + } + + public void run() { + InputView.printGreetingMessage(); + Date date = getDate(); + MenuCount menuCount = getMenu(); + displayEventGuideMessage(date); + displayMenuAndOrderAmount(menuCount); + displayPromotionResult(menuCount, date); + } + + private Date getDate() { + return executeWithExceptionHandle(() -> { + int inputDate = InputView.readDate(); + return Date.of(inputDate); + }); + } + + private MenuCount getMenu() { + return executeWithExceptionHandle(() -> { + String input = InputView.readMenu(); + Map<String, Integer> parsedMenu = Parser.parseMenuCount(input); + return new MenuCount(parsedMenu); + }); + } + + private void displayMenuAndOrderAmount(MenuCount menuCount) { + displayMenuInfo(menuCount); + displayTotalOrderAmount(menuCount); + } + + private void displayPromotionResult(MenuCount menuCount, Date date) { + displayGiftEvent(menuCount); + displayPromotionEvent(menuCount, date); + displayTotalPromotionAmount(menuCount, date); + displayExpectedPaymentAmount(menuCount, date); + displayEventBadge(menuCount, date); + } + + private void displayEventGuideMessage(Date date) { + OutputView.printEventGuideMessage(date.getValue()); + } + + + private void displayMenuInfo(MenuCount menuCount) { + OutputView.printOrderedMenu(menuCount); + } + + private void displayTotalOrderAmount(MenuCount menuCount) { + OutputView.printTotalOrderAmount(menuCount.calculateTotalAmount()); + } + + private void displayGiftEvent(MenuCount menuCount) { + boolean canGiveGift = menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount(); + OutputView.printGiftEvent(canGiveGift); + } + + private void displayPromotionEvent(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = promotion.calculatePromotionStatus(menuCount, date); + OutputView.printPromotionStatus(promotionStatus); + } + + private void displayTotalPromotionAmount(MenuCount menuCount, Date date) { + int totalPromotionAmount = promotion.calculatePromotionAmount(menuCount, date); + OutputView.printPromotionAmount(totalPromotionAmount); + } + + private void displayExpectedPaymentAmount(MenuCount menuCount, Date date) { + int expectedPayment = + menuCount.calculateTotalAmount() - promotion.calaulateTotalDiscountAmount(menuCount, date); + OutputView.printExpectedPaymentAmount(expectedPayment); + } + + private void displayEventBadge(MenuCount menuCount, Date date) { + String badgeName = promotion.calculateBadgeStatus(menuCount, date); + OutputView.printEventBadge(badgeName); + } + + private static <T> T executeWithExceptionHandle(final Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + +}
Java
์ปจํŠธ๋กค๋Ÿฌ๋Š” ํ๋ฆ„์„ ์ œ์–ดํ•˜๋Š” ์ฑ…์ž„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ ์ ์šฉ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ์ฑ…์ž„์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,115 @@ +package christmas.controller; + +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import christmas.model.strategy.WootecoBadgeStrategy; +import christmas.model.strategy.WootecoDiscountStrategy; +import christmas.service.PromotionService; +import christmas.util.Parser; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.EnumMap; +import java.util.Map; +import java.util.function.Supplier; + +public class PromotionController { + private final PromotionService promotion; + private final DiscountStrategy discountStrategy = new WootecoDiscountStrategy(); + private final BadgeStrategy badgeStrategy = new WootecoBadgeStrategy(); + + public PromotionController() { + this.promotion = new PromotionService(discountStrategy, badgeStrategy); + } + + public void run() { + InputView.printGreetingMessage(); + Date date = getDate(); + MenuCount menuCount = getMenu(); + displayEventGuideMessage(date); + displayMenuAndOrderAmount(menuCount); + displayPromotionResult(menuCount, date); + } + + private Date getDate() { + return executeWithExceptionHandle(() -> { + int inputDate = InputView.readDate(); + return Date.of(inputDate); + }); + } + + private MenuCount getMenu() { + return executeWithExceptionHandle(() -> { + String input = InputView.readMenu(); + Map<String, Integer> parsedMenu = Parser.parseMenuCount(input); + return new MenuCount(parsedMenu); + }); + } + + private void displayMenuAndOrderAmount(MenuCount menuCount) { + displayMenuInfo(menuCount); + displayTotalOrderAmount(menuCount); + } + + private void displayPromotionResult(MenuCount menuCount, Date date) { + displayGiftEvent(menuCount); + displayPromotionEvent(menuCount, date); + displayTotalPromotionAmount(menuCount, date); + displayExpectedPaymentAmount(menuCount, date); + displayEventBadge(menuCount, date); + } + + private void displayEventGuideMessage(Date date) { + OutputView.printEventGuideMessage(date.getValue()); + } + + + private void displayMenuInfo(MenuCount menuCount) { + OutputView.printOrderedMenu(menuCount); + } + + private void displayTotalOrderAmount(MenuCount menuCount) { + OutputView.printTotalOrderAmount(menuCount.calculateTotalAmount()); + } + + private void displayGiftEvent(MenuCount menuCount) { + boolean canGiveGift = menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount(); + OutputView.printGiftEvent(canGiveGift); + } + + private void displayPromotionEvent(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = promotion.calculatePromotionStatus(menuCount, date); + OutputView.printPromotionStatus(promotionStatus); + } + + private void displayTotalPromotionAmount(MenuCount menuCount, Date date) { + int totalPromotionAmount = promotion.calculatePromotionAmount(menuCount, date); + OutputView.printPromotionAmount(totalPromotionAmount); + } + + private void displayExpectedPaymentAmount(MenuCount menuCount, Date date) { + int expectedPayment = + menuCount.calculateTotalAmount() - promotion.calaulateTotalDiscountAmount(menuCount, date); + OutputView.printExpectedPaymentAmount(expectedPayment); + } + + private void displayEventBadge(MenuCount menuCount, Date date) { + String badgeName = promotion.calculateBadgeStatus(menuCount, date); + OutputView.printEventBadge(badgeName); + } + + private static <T> T executeWithExceptionHandle(final Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + +}
Java
`๋‚ ์งœ๋‚˜ ๋ฉ”๋‰ด๋ฅผ ์–ป๋Š”๋‹ค`๋ณด๋‹ค `์œ ํšจํ•œ ๊ฐ’์„ ์–ป์„๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค`๋ผ๋Š” ์ข€ ๋” ๋ช…ํ™•ํ•œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”? e.g.)`retryInputForValidVisitDate`, `returyInputForValidOrders`
@@ -0,0 +1,70 @@ +package christmas.service; + +import static christmas.model.constant.DiscountConstant.MIN_DISCOUNT_SERVICE; +import static christmas.model.constant.DiscountConstant.NO_DISCOUNT; +import static christmas.model.constant.PromotionConstant.CHRISTMAS_DISCOUNT; +import static christmas.model.constant.PromotionConstant.GIFT_EVENT; +import static christmas.model.constant.PromotionConstant.SPECIAL_DISCOUNT; +import static christmas.model.constant.PromotionConstant.WEEKDAY_DISCOUNT; +import static christmas.model.constant.PromotionConstant.WEEKEND_DISCOUNT; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.EventBadge; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import java.util.EnumMap; + +public class PromotionService { + private final DiscountStrategy discountStrategy; + private final BadgeStrategy badgeStrategy; + + + public PromotionService(DiscountStrategy discountStrategy, BadgeStrategy badgeStrategy) { + this.discountStrategy = discountStrategy; + this.badgeStrategy = badgeStrategy; + } + + public boolean canGetDiscount(MenuCount menuCount) { + return menuCount.calculateTotalAmount() >= MIN_DISCOUNT_SERVICE.getAmount(); + } + + public int calculatePromotionAmount(MenuCount menuCount, Date date) { + if (canGetDiscount(menuCount)) { + return calaulateTotalDiscountAmount(menuCount, date) + + discountStrategy.giftEventDiscount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + public int calaulateTotalDiscountAmount(MenuCount menuCount, Date date) { + if (canGetDiscount(menuCount)) { + return discountStrategy.christmasDiscount(date) + + discountStrategy.weekdayDiscount(menuCount, date) + + discountStrategy.specialDayDiscount(date) + + discountStrategy.weekendDiscount(menuCount, date); + } + return NO_DISCOUNT.getAmount(); + } + + public EnumMap<PromotionConstant, Integer> calculatePromotionStatus(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = new EnumMap<>(PromotionConstant.class); + if (canGetDiscount(menuCount)) { + promotionStatus.put(CHRISTMAS_DISCOUNT, discountStrategy.christmasDiscount(date)); + promotionStatus.put(WEEKDAY_DISCOUNT, discountStrategy.weekdayDiscount(menuCount, date)); + promotionStatus.put(WEEKEND_DISCOUNT, discountStrategy.weekendDiscount(menuCount, date)); + promotionStatus.put(SPECIAL_DISCOUNT, discountStrategy.specialDayDiscount(date)); + promotionStatus.put(GIFT_EVENT, discountStrategy.giftEventDiscount(menuCount)); + return promotionStatus; + } + return promotionStatus; + } + + public String calculateBadgeStatus(MenuCount menuCount, Date date) { + int promotionAmount = calculatePromotionAmount(menuCount, date); + EventBadge badge = badgeStrategy.calculateBadgeGrade(promotionAmount); + return badge.getName(); + } + +}
Java
์ด๋ฒคํŠธ๊ฐ€ ์‹ ๊ทœ๋กœ ๋„์ž…๋˜๊ฑฐ๋‚˜ ์‚ญ์ œ๋˜๋Š” ๊ฒฝ์šฐ ์„œ๋น„์Šค์— ์žˆ๋Š” ๊ณ„์‚ฐ ๊ธฐ๋Šฅ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์˜์กด๋„๋ฅผ ๋‚ฎ์ถ”๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,15 @@ +package christmas.model.strategy; + +import christmas.model.constant.EventBadge; + +public class WootecoBadgeStrategy implements BadgeStrategy { + @Override + public EventBadge calculateBadgeGrade(int promotionAmount) { + for (EventBadge badge : EventBadge.values()){ + if (promotionAmount >= badge.getBaseAmount()){ + return badge; + } + } + return EventBadge.NOTHING; + } +}
Java
ํ˜„์žฌ๋Š” EventBadge๊ฐ€ ๊ธฐ์ค€ ๊ธˆ์•ก์ด ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ์ง€๋งŒ ๋งŒ์ผ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์ˆœ์„œ๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ๋ฑƒ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๊ณ ๋ คํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,115 @@ +package christmas.controller; + +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import christmas.model.strategy.WootecoBadgeStrategy; +import christmas.model.strategy.WootecoDiscountStrategy; +import christmas.service.PromotionService; +import christmas.util.Parser; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.EnumMap; +import java.util.Map; +import java.util.function.Supplier; + +public class PromotionController { + private final PromotionService promotion; + private final DiscountStrategy discountStrategy = new WootecoDiscountStrategy(); + private final BadgeStrategy badgeStrategy = new WootecoBadgeStrategy(); + + public PromotionController() { + this.promotion = new PromotionService(discountStrategy, badgeStrategy); + } + + public void run() { + InputView.printGreetingMessage(); + Date date = getDate(); + MenuCount menuCount = getMenu(); + displayEventGuideMessage(date); + displayMenuAndOrderAmount(menuCount); + displayPromotionResult(menuCount, date); + } + + private Date getDate() { + return executeWithExceptionHandle(() -> { + int inputDate = InputView.readDate(); + return Date.of(inputDate); + }); + } + + private MenuCount getMenu() { + return executeWithExceptionHandle(() -> { + String input = InputView.readMenu(); + Map<String, Integer> parsedMenu = Parser.parseMenuCount(input); + return new MenuCount(parsedMenu); + }); + } + + private void displayMenuAndOrderAmount(MenuCount menuCount) { + displayMenuInfo(menuCount); + displayTotalOrderAmount(menuCount); + } + + private void displayPromotionResult(MenuCount menuCount, Date date) { + displayGiftEvent(menuCount); + displayPromotionEvent(menuCount, date); + displayTotalPromotionAmount(menuCount, date); + displayExpectedPaymentAmount(menuCount, date); + displayEventBadge(menuCount, date); + } + + private void displayEventGuideMessage(Date date) { + OutputView.printEventGuideMessage(date.getValue()); + } + + + private void displayMenuInfo(MenuCount menuCount) { + OutputView.printOrderedMenu(menuCount); + } + + private void displayTotalOrderAmount(MenuCount menuCount) { + OutputView.printTotalOrderAmount(menuCount.calculateTotalAmount()); + } + + private void displayGiftEvent(MenuCount menuCount) { + boolean canGiveGift = menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount(); + OutputView.printGiftEvent(canGiveGift); + } + + private void displayPromotionEvent(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = promotion.calculatePromotionStatus(menuCount, date); + OutputView.printPromotionStatus(promotionStatus); + } + + private void displayTotalPromotionAmount(MenuCount menuCount, Date date) { + int totalPromotionAmount = promotion.calculatePromotionAmount(menuCount, date); + OutputView.printPromotionAmount(totalPromotionAmount); + } + + private void displayExpectedPaymentAmount(MenuCount menuCount, Date date) { + int expectedPayment = + menuCount.calculateTotalAmount() - promotion.calaulateTotalDiscountAmount(menuCount, date); + OutputView.printExpectedPaymentAmount(expectedPayment); + } + + private void displayEventBadge(MenuCount menuCount, Date date) { + String badgeName = promotion.calculateBadgeStatus(menuCount, date); + OutputView.printEventBadge(badgeName); + } + + private static <T> T executeWithExceptionHandle(final Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + +}
Java
ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ž˜ ํ™œ์šฉํ•˜์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ข‹์€ ์ง€์‹์„ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค.
@@ -0,0 +1,115 @@ +package christmas.controller; + +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import christmas.model.strategy.WootecoBadgeStrategy; +import christmas.model.strategy.WootecoDiscountStrategy; +import christmas.service.PromotionService; +import christmas.util.Parser; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.EnumMap; +import java.util.Map; +import java.util.function.Supplier; + +public class PromotionController { + private final PromotionService promotion; + private final DiscountStrategy discountStrategy = new WootecoDiscountStrategy(); + private final BadgeStrategy badgeStrategy = new WootecoBadgeStrategy(); + + public PromotionController() { + this.promotion = new PromotionService(discountStrategy, badgeStrategy); + } + + public void run() { + InputView.printGreetingMessage(); + Date date = getDate(); + MenuCount menuCount = getMenu(); + displayEventGuideMessage(date); + displayMenuAndOrderAmount(menuCount); + displayPromotionResult(menuCount, date); + } + + private Date getDate() { + return executeWithExceptionHandle(() -> { + int inputDate = InputView.readDate(); + return Date.of(inputDate); + }); + } + + private MenuCount getMenu() { + return executeWithExceptionHandle(() -> { + String input = InputView.readMenu(); + Map<String, Integer> parsedMenu = Parser.parseMenuCount(input); + return new MenuCount(parsedMenu); + }); + } + + private void displayMenuAndOrderAmount(MenuCount menuCount) { + displayMenuInfo(menuCount); + displayTotalOrderAmount(menuCount); + } + + private void displayPromotionResult(MenuCount menuCount, Date date) { + displayGiftEvent(menuCount); + displayPromotionEvent(menuCount, date); + displayTotalPromotionAmount(menuCount, date); + displayExpectedPaymentAmount(menuCount, date); + displayEventBadge(menuCount, date); + } + + private void displayEventGuideMessage(Date date) { + OutputView.printEventGuideMessage(date.getValue()); + } + + + private void displayMenuInfo(MenuCount menuCount) { + OutputView.printOrderedMenu(menuCount); + } + + private void displayTotalOrderAmount(MenuCount menuCount) { + OutputView.printTotalOrderAmount(menuCount.calculateTotalAmount()); + } + + private void displayGiftEvent(MenuCount menuCount) { + boolean canGiveGift = menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount(); + OutputView.printGiftEvent(canGiveGift); + } + + private void displayPromotionEvent(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = promotion.calculatePromotionStatus(menuCount, date); + OutputView.printPromotionStatus(promotionStatus); + } + + private void displayTotalPromotionAmount(MenuCount menuCount, Date date) { + int totalPromotionAmount = promotion.calculatePromotionAmount(menuCount, date); + OutputView.printPromotionAmount(totalPromotionAmount); + } + + private void displayExpectedPaymentAmount(MenuCount menuCount, Date date) { + int expectedPayment = + menuCount.calculateTotalAmount() - promotion.calaulateTotalDiscountAmount(menuCount, date); + OutputView.printExpectedPaymentAmount(expectedPayment); + } + + private void displayEventBadge(MenuCount menuCount, Date date) { + String badgeName = promotion.calculateBadgeStatus(menuCount, date); + OutputView.printEventBadge(badgeName); + } + + private static <T> T executeWithExceptionHandle(final Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + +}
Java
ํ•„๋“œ ์ฃผ์ž…๋ณด๋‹ค, ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ํ™œ์šฉํ•ด๋ณด๋ฉด ๋” ์œ ์—ฐํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,115 @@ +package christmas.controller; + +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import christmas.model.strategy.WootecoBadgeStrategy; +import christmas.model.strategy.WootecoDiscountStrategy; +import christmas.service.PromotionService; +import christmas.util.Parser; +import christmas.view.InputView; +import christmas.view.OutputView; +import java.util.EnumMap; +import java.util.Map; +import java.util.function.Supplier; + +public class PromotionController { + private final PromotionService promotion; + private final DiscountStrategy discountStrategy = new WootecoDiscountStrategy(); + private final BadgeStrategy badgeStrategy = new WootecoBadgeStrategy(); + + public PromotionController() { + this.promotion = new PromotionService(discountStrategy, badgeStrategy); + } + + public void run() { + InputView.printGreetingMessage(); + Date date = getDate(); + MenuCount menuCount = getMenu(); + displayEventGuideMessage(date); + displayMenuAndOrderAmount(menuCount); + displayPromotionResult(menuCount, date); + } + + private Date getDate() { + return executeWithExceptionHandle(() -> { + int inputDate = InputView.readDate(); + return Date.of(inputDate); + }); + } + + private MenuCount getMenu() { + return executeWithExceptionHandle(() -> { + String input = InputView.readMenu(); + Map<String, Integer> parsedMenu = Parser.parseMenuCount(input); + return new MenuCount(parsedMenu); + }); + } + + private void displayMenuAndOrderAmount(MenuCount menuCount) { + displayMenuInfo(menuCount); + displayTotalOrderAmount(menuCount); + } + + private void displayPromotionResult(MenuCount menuCount, Date date) { + displayGiftEvent(menuCount); + displayPromotionEvent(menuCount, date); + displayTotalPromotionAmount(menuCount, date); + displayExpectedPaymentAmount(menuCount, date); + displayEventBadge(menuCount, date); + } + + private void displayEventGuideMessage(Date date) { + OutputView.printEventGuideMessage(date.getValue()); + } + + + private void displayMenuInfo(MenuCount menuCount) { + OutputView.printOrderedMenu(menuCount); + } + + private void displayTotalOrderAmount(MenuCount menuCount) { + OutputView.printTotalOrderAmount(menuCount.calculateTotalAmount()); + } + + private void displayGiftEvent(MenuCount menuCount) { + boolean canGiveGift = menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount(); + OutputView.printGiftEvent(canGiveGift); + } + + private void displayPromotionEvent(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = promotion.calculatePromotionStatus(menuCount, date); + OutputView.printPromotionStatus(promotionStatus); + } + + private void displayTotalPromotionAmount(MenuCount menuCount, Date date) { + int totalPromotionAmount = promotion.calculatePromotionAmount(menuCount, date); + OutputView.printPromotionAmount(totalPromotionAmount); + } + + private void displayExpectedPaymentAmount(MenuCount menuCount, Date date) { + int expectedPayment = + menuCount.calculateTotalAmount() - promotion.calaulateTotalDiscountAmount(menuCount, date); + OutputView.printExpectedPaymentAmount(expectedPayment); + } + + private void displayEventBadge(MenuCount menuCount, Date date) { + String badgeName = promotion.calculateBadgeStatus(menuCount, date); + OutputView.printEventBadge(badgeName); + } + + private static <T> T executeWithExceptionHandle(final Supplier<T> supplier) { + while (true) { + try { + return supplier.get(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + +}
Java
ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ static์œผ๋กœ ์ž‘์„ฑํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? static์ด ์•„๋‹ˆ์–ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,29 @@ +package christmas.view; + +import camp.nextstep.edu.missionutils.Console; +import christmas.util.Parser; +import christmas.validator.MenuValidator; + +public class InputView { + + public static void printGreetingMessage() { + System.out.println(ViewMessage.GREETING.getMessage()); + } + + + public static int readDate() { + System.out.println(ViewMessage.DATE.getMessage()); + + String input = Console.readLine(); + return Parser.parseDate(input); + } + + + public static String readMenu() { + System.out.println(ViewMessage.MENU.getMessage()); + String input = Console.readLine(); + MenuValidator.validateMenuInputFormat(input); + return input; + } + +}
Java
static ๋ฉ”์†Œ๋“œ๋ฐ–์— ์—†๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ, ์ธ์Šคํ„ด์Šคํ™”๊ฐ€ ํ•„์š” ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ฑ์ž๋ฅผ private์œผ๋กœ ํ•˜์—ฌ ์ธ์Šคํ„ด์Šคํ™”๋ฅผ ๋ง‰์•„์ฃผ๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค https://rules.sonarsource.com/java/RSPEC-1118/
@@ -0,0 +1,44 @@ +package christmas.model.constant; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public enum DateConstant { + MIN_DATE(1), + MAX_DATE(31), + XMAS(25); + + public static final Set<Integer> WEEKDAYS = new HashSet<>(); + public static final Set<Integer> WEEKENDS = new HashSet<>(); + public static final Set<Integer> SPECIAL_DAYS = new HashSet<>(Arrays.asList( + 3, 10, 17, 24, 25, 31 + )); + + static { + LocalDate start = LocalDate.of(2023, 12, MIN_DATE.date); + LocalDate end = LocalDate.of(2023, 12, MAX_DATE.date); + + for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) { + DayOfWeek day = date.getDayOfWeek(); + if (day == DayOfWeek.SATURDAY || day == DayOfWeek.FRIDAY) { + WEEKENDS.add(date.getDayOfMonth()); + continue; + } + WEEKDAYS.add(date.getDayOfMonth()); + } + } + + private final int date; + + DateConstant(int date) { + this.date = date; + } + + public int getDate() { + return date; + } + +}
Java
```suggestion public static final Set<Integer> SPECIAL_DAYS = Set.of(3, 10, 17, 24, 25, 31); ``` `Set.of` ๋ฅผ ํ™œ์šฉํ•ด์„œ ๋” ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,44 @@ +package christmas.model.constant; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public enum DateConstant { + MIN_DATE(1), + MAX_DATE(31), + XMAS(25); + + public static final Set<Integer> WEEKDAYS = new HashSet<>(); + public static final Set<Integer> WEEKENDS = new HashSet<>(); + public static final Set<Integer> SPECIAL_DAYS = new HashSet<>(Arrays.asList( + 3, 10, 17, 24, 25, 31 + )); + + static { + LocalDate start = LocalDate.of(2023, 12, MIN_DATE.date); + LocalDate end = LocalDate.of(2023, 12, MAX_DATE.date); + + for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) { + DayOfWeek day = date.getDayOfWeek(); + if (day == DayOfWeek.SATURDAY || day == DayOfWeek.FRIDAY) { + WEEKENDS.add(date.getDayOfMonth()); + continue; + } + WEEKDAYS.add(date.getDayOfMonth()); + } + } + + private final int date; + + DateConstant(int date) { + this.date = date; + } + + public int getDate() { + return date; + } + +}
Java
SPECIAL_DAYS๊ฐ€ public์œผ๋กœ ์—ด๋ ค์žˆ์œผ๋ฉด์„œ ๋ถˆ๋ณ€์ด ์•„๋‹ˆ๋ฏ€๋กœ (HashMap) ์™ธ๋ถ€์—์„œ ๊ฐ’์„ ์ˆ˜์ •ํ•˜๋Š” ํ•ดํ‚น์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค! ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค ```java DateConstant.SPECIAL_DAYS.add(-1); ```
@@ -0,0 +1,40 @@ +package christmas.exception; + +public class CustomException extends IllegalArgumentException { + private static final String MENU_ERROR_MESSAGE = "[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String DATE_ERROR_MESSAGE = "[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + public CustomException(String s) { + super(s); + } + + @Override + public synchronized Throwable fillInStackTrace(){ + return this; + } + + public static final CustomException DATE_NOT_INTEGER_EXCEPTION = + new CustomException(DATE_ERROR_MESSAGE); + + public static final CustomException DATE_RANGE_EXCEPTION = + new CustomException(DATE_ERROR_MESSAGE); + + public static final CustomException DUPLICATE_MENU_EXCEPTION = + new CustomException(MENU_ERROR_MESSAGE); + + public static final CustomException MENU_COUNT_ZERO_EXCEPTION = + new CustomException(MENU_ERROR_MESSAGE); + + public static final CustomException MENU_FORMAT_EXCEPTION = + new CustomException(MENU_ERROR_MESSAGE); + + public static final CustomException MENU_NOT_FOUND_EXCEPTION = + new CustomException(MENU_ERROR_MESSAGE); + + public static final CustomException ONLY_DRINK_EXCEPTION = + new CustomException(MENU_ERROR_MESSAGE); + + public static final CustomException MENU_AMOUNT_OVER_LIMIT_EXCEPTION = + new CustomException(MENU_ERROR_MESSAGE); + + +}
Java
์ปค์Šคํ…€ ์ต์…‰์…˜์ด๋ผ๋Š” ์ด๋ฆ„๋ณด๋‹ค, ์กฐ๊ธˆ ๋” ์˜ˆ์™ธ์˜ ์ด์œ  ๋“ฑ์˜ ์˜๋ฏธ๊ฐ€ ๋“œ๋Ÿฌ๋‚˜๋Š” ์ด๋ฆ„์ด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,56 @@ +package christmas.model.constant; + +import java.util.Arrays; + +public enum Menu { + SOUP("์• ํ”ผํƒ€์ด์ €", "์–‘์†ก์ด์ˆ˜ํ”„", 6000), + TAPAS("์• ํ”ผํƒ€์ด์ €", "ํƒ€ํŒŒ์Šค", 5500), + SALAD("์• ํ”ผํƒ€์ด์ €", "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + STEAK("๋ฉ”์ธ", "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000), + BBQ("๋ฉ”์ธ", "๋ฐ”๋น„ํ๋ฆฝ", 54000), + SEAFOOD_PASTA("๋ฉ”์ธ", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000), + XMAS_PASTA("๋ฉ”์ธ", "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + CHOCO_CAKE("๋””์ €ํŠธ", "์ดˆ์ฝ”์ผ€์ดํฌ", 15000), + ICECREAM("๋””์ €ํŠธ", "์•„์ด์Šคํฌ๋ฆผ", 5000), + ZEROCOKE("์Œ๋ฃŒ", "์ œ๋กœ์ฝœ๋ผ", 3000), + REDWINE("์Œ๋ฃŒ", "๋ ˆ๋“œ์™€์ธ", 60000), + CHAMPAGNE("์Œ๋ฃŒ", "์ƒดํŽ˜์ธ", 25000); + + + private final String category; + private final String name; + private final int price; + + Menu(String category, String name, int price) { + this.category = category; + this.name = name; + this.price = price; + } + + public static Menu of(String name) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(name)) + .findFirst() + .orElse(null); + } + + public boolean isMainCategory() { + return "๋ฉ”์ธ".equals(this.category); + } + + public boolean isDessertCategory() { + return "๋””์ €ํŠธ".equals(this.category); + } + + public String getCategory() { + return category; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } +}
Java
String ๋ฆฌํ„ฐ๋Ÿด๋กœ "๋ฉ”์ธ"๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ฉ”๋‰ด์˜ ์นดํ…Œ๊ณ ๋ฆฌ๋„ enum์œผ๋กœ ๊ด€๋ฆฌํ•˜๋ฉด ์กฐ๊ธˆ ๋” ์šฉ์ดํ•˜์ง€ ์•Š์„๊นŒ์š”?
@@ -0,0 +1,52 @@ +package christmas.model; + +import static christmas.exception.CustomException.DATE_RANGE_EXCEPTION; +import static christmas.model.constant.DateConstant.SPECIAL_DAYS; +import static christmas.model.constant.DateConstant.WEEKDAYS; +import static christmas.model.constant.DateConstant.WEEKENDS; +import static christmas.model.constant.DateConstant.XMAS; + +import christmas.model.constant.DateConstant; + +public class Date { + private final int MIN_DATE = DateConstant.MIN_DATE.getDate(); + private final int MAX_DATE = DateConstant.MAX_DATE.getDate(); + private final int date; + + private Date(int date) { + validateDateRange(date); + this.date = date; + } + + public static Date of(int date) { + return new Date(date); + } + + public int getValue() { + return date; + } + + private void validateDateRange(int date) { + if (date < MIN_DATE || date > MAX_DATE) { + throw DATE_RANGE_EXCEPTION; + } + } + + public boolean isWeekday() { + return WEEKDAYS.contains(date); + } + + public boolean isWeekend() { + return WEEKENDS.contains(date); + } + + public boolean isBeforeXmas() { + return XMAS.getDate() >= date; + } + + public boolean isSpecialDay() { + return SPECIAL_DAYS.contains(date); + } + + +}
Java
์ž๋ฐ”์˜ ๋นŒํŠธ์ธ ๊ฐ์ฒด์ธ [Date](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html)์™€ ํ˜ผ๋™์˜ ์—ฌ์ง€๊ฐ€ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค..! (์ผ๋‹จ ์ „ ํ—ท๊ฐˆ๋ ธ์–ด์š” ๐Ÿ˜ญ)
@@ -0,0 +1,22 @@ +package christmas.model.constant; + +public enum PromotionConstant { + + CHRISTMAS_DISCOUNT("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"), + WEEKDAY_DISCOUNT("ํ‰์ผ ํ• ์ธ"), + WEEKEND_DISCOUNT("์ฃผ๋ง ํ• ์ธ"), + SPECIAL_DISCOUNT("ํŠน๋ณ„ ํ• ์ธ"), + GIFT_EVENT("์ฆ์ • ์ด๋ฒคํŠธ"); + + private String name; + + PromotionConstant(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + +}
Java
private final๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,75 @@ +package christmas.model.strategy; + +import static christmas.model.constant.DiscountConstant.CHRISTMAS_START_DISCOUNT; +import static christmas.model.constant.DiscountConstant.DISCOUNT_INCREMENT; +import static christmas.model.constant.DiscountConstant.FIXED_DISCOUNT; +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; +import static christmas.model.constant.DiscountConstant.NO_DISCOUNT; +import static christmas.model.constant.DiscountConstant.SPECIAL_DAY_DISCOUNT; + +import christmas.model.Date; +import christmas.model.constant.Menu; +import christmas.model.MenuCount; + +public class WootecoDiscountStrategy implements DiscountStrategy { + + @Override + public int christmasDiscount(Date date) { + if (date.isBeforeXmas()) { + return (CHRISTMAS_START_DISCOUNT.getAmount() + (date.getValue() - 1) + * DISCOUNT_INCREMENT.getAmount()); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int weekendDiscount(MenuCount menuCount, Date date) { + if (date.isWeekend()) { + return calculateWeekendDiscountAmount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int weekdayDiscount(MenuCount menuCount, Date date) { + if (date.isWeekday()) { + return calculateWeekdayDiscountAmount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int specialDayDiscount(Date date) { + if (date.isSpecialDay()) { + return SPECIAL_DAY_DISCOUNT.getAmount(); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int giftEventDiscount(MenuCount menuCount) { + if (menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount()) { + return Menu.CHAMPAGNE.getPrice(); + } + return NO_DISCOUNT.getAmount(); + } + + + private int calculateWeekendDiscountAmount(MenuCount menuCount) { + return menuCount.getValue() + .entrySet() + .stream() + .filter(entry -> entry.getKey().isMainCategory()) + .mapToInt(entry -> FIXED_DISCOUNT.getAmount() * entry.getValue()) + .sum(); + } + + private int calculateWeekdayDiscountAmount(MenuCount menuCount) { + return menuCount.getValue() + .entrySet() + .stream() + .filter(entry -> entry.getKey().isDessertCategory()) + .mapToInt(entry -> FIXED_DISCOUNT.getAmount() * entry.getValue()) + .sum(); + } +}
Java
์—ฌ๋Ÿฌ ํ• ์ธ ์ „๋žต์„ ํ•œ ํด๋ž˜์Šค์— ๋ชฐ์•„๋„ฃ๊ธฐ๋ณด๋‹ค๋Š” ๊ฐ๊ฐ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š” + ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋ ‡๊ฒŒ ๋‚˜๋ˆ„์–ด์„œ, ์šฐํ…Œ์ฝ” ํ• ์ธ ์ „๋žต์ด๋ผ๋Š” ์ด๋ฆ„๋ณด๋‹ค๋Š” ๋ฌด์Šจ ํ• ์ธ ์ „๋žต์ธ์ง€์— ๋Œ€ํ•œ ์˜๋ฏธ๊ฐ€ ๋“œ๋Ÿฌ๋‚˜๋Š” ์ด๋ฆ„์ด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,75 @@ +package christmas.model.strategy; + +import static christmas.model.constant.DiscountConstant.CHRISTMAS_START_DISCOUNT; +import static christmas.model.constant.DiscountConstant.DISCOUNT_INCREMENT; +import static christmas.model.constant.DiscountConstant.FIXED_DISCOUNT; +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; +import static christmas.model.constant.DiscountConstant.NO_DISCOUNT; +import static christmas.model.constant.DiscountConstant.SPECIAL_DAY_DISCOUNT; + +import christmas.model.Date; +import christmas.model.constant.Menu; +import christmas.model.MenuCount; + +public class WootecoDiscountStrategy implements DiscountStrategy { + + @Override + public int christmasDiscount(Date date) { + if (date.isBeforeXmas()) { + return (CHRISTMAS_START_DISCOUNT.getAmount() + (date.getValue() - 1) + * DISCOUNT_INCREMENT.getAmount()); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int weekendDiscount(MenuCount menuCount, Date date) { + if (date.isWeekend()) { + return calculateWeekendDiscountAmount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int weekdayDiscount(MenuCount menuCount, Date date) { + if (date.isWeekday()) { + return calculateWeekdayDiscountAmount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int specialDayDiscount(Date date) { + if (date.isSpecialDay()) { + return SPECIAL_DAY_DISCOUNT.getAmount(); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int giftEventDiscount(MenuCount menuCount) { + if (menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount()) { + return Menu.CHAMPAGNE.getPrice(); + } + return NO_DISCOUNT.getAmount(); + } + + + private int calculateWeekendDiscountAmount(MenuCount menuCount) { + return menuCount.getValue() + .entrySet() + .stream() + .filter(entry -> entry.getKey().isMainCategory()) + .mapToInt(entry -> FIXED_DISCOUNT.getAmount() * entry.getValue()) + .sum(); + } + + private int calculateWeekdayDiscountAmount(MenuCount menuCount) { + return menuCount.getValue() + .entrySet() + .stream() + .filter(entry -> entry.getKey().isDessertCategory()) + .mapToInt(entry -> FIXED_DISCOUNT.getAmount() * entry.getValue()) + .sum(); + } +}
Java
ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ• ์ธ ์ „๋žต์—์„œ ๋ณ€๋™์ด ์ƒ๊ธฐ๊ฒŒ ๋œ๋‹ค๋ฉด (์˜ˆ: ์‹œ์ž‘ํ• ์ธ์•ก์ด 1000์›์—์„œ 2000์›์œผ๋กœ ๋ณ€๊ฒฝ), ํ•ด๋‹น ๋ณ€๊ฒฝ์‚ฌํ•ญ์— ๋Œ€ํ•œ ์—ฌํŒŒ๊ฐ€ ํ• ์ธ ์ „๋žต๋งŒ ์—ฌํŒŒ๋ฅผ ๋ฏธ์ณ์•ผ ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ enum์ธ DiscountConstant์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๊ฒŒ ๋ผ์š”. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฃผ๋ง ํ• ์ธ ์ „๋žต์—๋„ ๋ณ€๋™์ด ์ƒ๊ธด๋‹ค๋ฉด DiscountConstant์ด ๋ฐ”๋€Œ์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ด๋Š” `ํด๋ž˜์Šค๊ฐ€ ๋ณ€๊ฒฝ๋  ์ด์œ ๋Š” ํ•œ ๊ฐ€์ง€์—ฌ์•ผ ํ•œ๋‹ค`๋ผ๋Š” SRP์›์น™์„ ์œ„๋ฐ˜ํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,75 @@ +package christmas.model.strategy; + +import static christmas.model.constant.DiscountConstant.CHRISTMAS_START_DISCOUNT; +import static christmas.model.constant.DiscountConstant.DISCOUNT_INCREMENT; +import static christmas.model.constant.DiscountConstant.FIXED_DISCOUNT; +import static christmas.model.constant.DiscountConstant.GIFT_EVENT_THRESHOLD; +import static christmas.model.constant.DiscountConstant.NO_DISCOUNT; +import static christmas.model.constant.DiscountConstant.SPECIAL_DAY_DISCOUNT; + +import christmas.model.Date; +import christmas.model.constant.Menu; +import christmas.model.MenuCount; + +public class WootecoDiscountStrategy implements DiscountStrategy { + + @Override + public int christmasDiscount(Date date) { + if (date.isBeforeXmas()) { + return (CHRISTMAS_START_DISCOUNT.getAmount() + (date.getValue() - 1) + * DISCOUNT_INCREMENT.getAmount()); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int weekendDiscount(MenuCount menuCount, Date date) { + if (date.isWeekend()) { + return calculateWeekendDiscountAmount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int weekdayDiscount(MenuCount menuCount, Date date) { + if (date.isWeekday()) { + return calculateWeekdayDiscountAmount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int specialDayDiscount(Date date) { + if (date.isSpecialDay()) { + return SPECIAL_DAY_DISCOUNT.getAmount(); + } + return NO_DISCOUNT.getAmount(); + } + + @Override + public int giftEventDiscount(MenuCount menuCount) { + if (menuCount.calculateTotalAmount() >= GIFT_EVENT_THRESHOLD.getAmount()) { + return Menu.CHAMPAGNE.getPrice(); + } + return NO_DISCOUNT.getAmount(); + } + + + private int calculateWeekendDiscountAmount(MenuCount menuCount) { + return menuCount.getValue() + .entrySet() + .stream() + .filter(entry -> entry.getKey().isMainCategory()) + .mapToInt(entry -> FIXED_DISCOUNT.getAmount() * entry.getValue()) + .sum(); + } + + private int calculateWeekdayDiscountAmount(MenuCount menuCount) { + return menuCount.getValue() + .entrySet() + .stream() + .filter(entry -> entry.getKey().isDessertCategory()) + .mapToInt(entry -> FIXED_DISCOUNT.getAmount() * entry.getValue()) + .sum(); + } +}
Java
๊ทธ๋Ÿฐ ์ด์œ ์—์„œ, CHRISTMAS_START_DISCOUNT๋Š” enum์œผ๋กœ publicํ•˜๊ฒŒ ์•„๋ฌด ํด๋ž˜์Šค์—์„œ๋‚˜ ๊ณต๊ฐœ๋  ํ•„์š”๊ฐ€ ์—†์ด, ํ•ด๋‹น ํ• ์ธ ์ •์ฑ… ํด๋ž˜์Šค์—์„œ `private static final`๊ณผ ๊ฐ™์ด ๊ด€๋ฆฌํ•˜๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” (ํ• ์ธ ์ •์ฑ…์ด ๋ฐ”๋€Œ๋”๋ผ๋„ ํ• ์ธ ์ •์ฑ… ํด๋ž˜์Šค๋งŒ ์˜ํ–ฅ์„ ๋ฐ›๊ฒŒ ๋˜๊ฒ ์ฃ )
@@ -0,0 +1,70 @@ +package christmas.service; + +import static christmas.model.constant.DiscountConstant.MIN_DISCOUNT_SERVICE; +import static christmas.model.constant.DiscountConstant.NO_DISCOUNT; +import static christmas.model.constant.PromotionConstant.CHRISTMAS_DISCOUNT; +import static christmas.model.constant.PromotionConstant.GIFT_EVENT; +import static christmas.model.constant.PromotionConstant.SPECIAL_DISCOUNT; +import static christmas.model.constant.PromotionConstant.WEEKDAY_DISCOUNT; +import static christmas.model.constant.PromotionConstant.WEEKEND_DISCOUNT; + +import christmas.model.Date; +import christmas.model.MenuCount; +import christmas.model.constant.EventBadge; +import christmas.model.constant.PromotionConstant; +import christmas.model.strategy.BadgeStrategy; +import christmas.model.strategy.DiscountStrategy; +import java.util.EnumMap; + +public class PromotionService { + private final DiscountStrategy discountStrategy; + private final BadgeStrategy badgeStrategy; + + + public PromotionService(DiscountStrategy discountStrategy, BadgeStrategy badgeStrategy) { + this.discountStrategy = discountStrategy; + this.badgeStrategy = badgeStrategy; + } + + public boolean canGetDiscount(MenuCount menuCount) { + return menuCount.calculateTotalAmount() >= MIN_DISCOUNT_SERVICE.getAmount(); + } + + public int calculatePromotionAmount(MenuCount menuCount, Date date) { + if (canGetDiscount(menuCount)) { + return calaulateTotalDiscountAmount(menuCount, date) + + discountStrategy.giftEventDiscount(menuCount); + } + return NO_DISCOUNT.getAmount(); + } + + public int calaulateTotalDiscountAmount(MenuCount menuCount, Date date) { + if (canGetDiscount(menuCount)) { + return discountStrategy.christmasDiscount(date) + + discountStrategy.weekdayDiscount(menuCount, date) + + discountStrategy.specialDayDiscount(date) + + discountStrategy.weekendDiscount(menuCount, date); + } + return NO_DISCOUNT.getAmount(); + } + + public EnumMap<PromotionConstant, Integer> calculatePromotionStatus(MenuCount menuCount, Date date) { + EnumMap<PromotionConstant, Integer> promotionStatus = new EnumMap<>(PromotionConstant.class); + if (canGetDiscount(menuCount)) { + promotionStatus.put(CHRISTMAS_DISCOUNT, discountStrategy.christmasDiscount(date)); + promotionStatus.put(WEEKDAY_DISCOUNT, discountStrategy.weekdayDiscount(menuCount, date)); + promotionStatus.put(WEEKEND_DISCOUNT, discountStrategy.weekendDiscount(menuCount, date)); + promotionStatus.put(SPECIAL_DISCOUNT, discountStrategy.specialDayDiscount(date)); + promotionStatus.put(GIFT_EVENT, discountStrategy.giftEventDiscount(menuCount)); + return promotionStatus; + } + return promotionStatus; + } + + public String calculateBadgeStatus(MenuCount menuCount, Date date) { + int promotionAmount = calculatePromotionAmount(menuCount, date); + EventBadge badge = badgeStrategy.calculateBadgeGrade(promotionAmount); + return badge.getName(); + } + +}
Java
early return์„ ์‚ฌ์šฉํ•˜๋ฉด ์กฐ๊ธˆ ๋” ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•ด์งˆ ๊ฒƒ ๊ฐ™์•„์š” ```java if(!canGetDiscount(menuCount) { return Collections.EMPTY_MAP; } EnumMap<PromotionConstant, Integer> promotionStatus = new EnumMap<>(PromotionConstant.class); promotionStatus.put(CHRISTMAS_DISCOUNT, discountStrategy.christmasDiscount(date)); promotionStatus.put(WEEKDAY_DISCOUNT, discountStrategy.weekdayDiscount(menuCount, date)); promotionStatus.put(WEEKEND_DISCOUNT, discountStrategy.weekendDiscount(menuCount, date)); promotionStatus.put(SPECIAL_DISCOUNT, discountStrategy.specialDayDiscount(date)); promotionStatus.put(GIFT_EVENT, discountStrategy.giftEventDiscount(menuCount)); return promotionStatus; ``` ์ด๋ ‡๊ฒŒ ๋ฆฌํŒฉํ† ๋ง์„ ํ–ˆ๋”๋‹ˆ enummap์„ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ private ๋ฉ”์†Œ๋“œ๋กœ ์ถ”์ถœํ•ด์•ผ๊ฒ ๋‹ค๋Š” ๋А๋‚Œ๋„ ์˜ค๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค (if๋ฌธ์œผ๋กœ ํ• ์ธ ํ™•์ธํ•˜๋Š” ๋กœ์ง, map ๋งŒ๋“œ๋Š” ๋กœ์ง๊นŒ์ง€ ์ด 2๊ฐœ์˜ ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ์œผ๋‹ˆ๊น์š”)
@@ -0,0 +1,52 @@ +package christmas.model; + +import static christmas.exception.CustomException.DATE_RANGE_EXCEPTION; +import static christmas.model.constant.DateConstant.SPECIAL_DAYS; +import static christmas.model.constant.DateConstant.WEEKDAYS; +import static christmas.model.constant.DateConstant.WEEKENDS; +import static christmas.model.constant.DateConstant.XMAS; + +import christmas.model.constant.DateConstant; + +public class Date { + private final int MIN_DATE = DateConstant.MIN_DATE.getDate(); + private final int MAX_DATE = DateConstant.MAX_DATE.getDate(); + private final int date; + + private Date(int date) { + validateDateRange(date); + this.date = date; + } + + public static Date of(int date) { + return new Date(date); + } + + public int getValue() { + return date; + } + + private void validateDateRange(int date) { + if (date < MIN_DATE || date > MAX_DATE) { + throw DATE_RANGE_EXCEPTION; + } + } + + public boolean isWeekday() { + return WEEKDAYS.contains(date); + } + + public boolean isWeekend() { + return WEEKENDS.contains(date); + } + + public boolean isBeforeXmas() { + return XMAS.getDate() >= date; + } + + public boolean isSpecialDay() { + return SPECIAL_DAYS.contains(date); + } + + +}
Java
์ฃผ๋ง์ธ์ง€ ํ‰์ผ์ธ์ง€๋ฅผ ์ฒดํฌํ•˜๋Š” ๋กœ์ง์„ `DateConstant`์— ๋ชฐ๊ธฐ๋ณด๋‹ค, Date ํด๋ž˜์Šค์—์„œ ์ฒ˜๋ฆฌํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”? ์ƒ์ˆ˜ ํด๋ž˜์Šค์— ๋กœ์ง์ด ๋“ค์–ด์žˆ๋Š” ๊ฒƒ์€ ์ด์ƒํ•œ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด์š”
@@ -0,0 +1,44 @@ +package christmas.model.constant; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public enum DateConstant { + MIN_DATE(1), + MAX_DATE(31), + XMAS(25); + + public static final Set<Integer> WEEKDAYS = new HashSet<>(); + public static final Set<Integer> WEEKENDS = new HashSet<>(); + public static final Set<Integer> SPECIAL_DAYS = new HashSet<>(Arrays.asList( + 3, 10, 17, 24, 25, 31 + )); + + static { + LocalDate start = LocalDate.of(2023, 12, MIN_DATE.date); + LocalDate end = LocalDate.of(2023, 12, MAX_DATE.date); + + for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) { + DayOfWeek day = date.getDayOfWeek(); + if (day == DayOfWeek.SATURDAY || day == DayOfWeek.FRIDAY) { + WEEKENDS.add(date.getDayOfMonth()); + continue; + } + WEEKDAYS.add(date.getDayOfMonth()); + } + } + + private final int date; + + DateConstant(int date) { + this.date = date; + } + + public int getDate() { + return date; + } + +}
Java
๋ฆฌํ„ฐ๋Ÿด ๊ฐ’๋„ ์ปจ๋ฒค์…˜์— ๋งž๊ฒŒ ์ƒ์ˆ˜ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,56 @@ +package christmas.model.constant; + +import java.util.Arrays; + +public enum Menu { + SOUP("์• ํ”ผํƒ€์ด์ €", "์–‘์†ก์ด์ˆ˜ํ”„", 6000), + TAPAS("์• ํ”ผํƒ€์ด์ €", "ํƒ€ํŒŒ์Šค", 5500), + SALAD("์• ํ”ผํƒ€์ด์ €", "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + STEAK("๋ฉ”์ธ", "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000), + BBQ("๋ฉ”์ธ", "๋ฐ”๋น„ํ๋ฆฝ", 54000), + SEAFOOD_PASTA("๋ฉ”์ธ", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000), + XMAS_PASTA("๋ฉ”์ธ", "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + CHOCO_CAKE("๋””์ €ํŠธ", "์ดˆ์ฝ”์ผ€์ดํฌ", 15000), + ICECREAM("๋””์ €ํŠธ", "์•„์ด์Šคํฌ๋ฆผ", 5000), + ZEROCOKE("์Œ๋ฃŒ", "์ œ๋กœ์ฝœ๋ผ", 3000), + REDWINE("์Œ๋ฃŒ", "๋ ˆ๋“œ์™€์ธ", 60000), + CHAMPAGNE("์Œ๋ฃŒ", "์ƒดํŽ˜์ธ", 25000); + + + private final String category; + private final String name; + private final int price; + + Menu(String category, String name, int price) { + this.category = category; + this.name = name; + this.price = price; + } + + public static Menu of(String name) { + return Arrays.stream(Menu.values()) + .filter(menu -> menu.getName().equals(name)) + .findFirst() + .orElse(null); + } + + public boolean isMainCategory() { + return "๋ฉ”์ธ".equals(this.category); + } + + public boolean isDessertCategory() { + return "๋””์ €ํŠธ".equals(this.category); + } + + public String getCategory() { + return category; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } +}
Java
์Œ์‹์˜ ์ข…๋ฅ˜๋„ Enum์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ํŠน์ • ๋ฉ”๋‰ด ์ข…๋ฅ˜์— ํ•ด๋‹น๋˜๋Š” ์Œ์‹์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋กœ์ง์„ ์งค ๋•Œ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”:)
@@ -0,0 +1,52 @@ +package christmas.model; + +import static christmas.exception.CustomException.DATE_RANGE_EXCEPTION; +import static christmas.model.constant.DateConstant.SPECIAL_DAYS; +import static christmas.model.constant.DateConstant.WEEKDAYS; +import static christmas.model.constant.DateConstant.WEEKENDS; +import static christmas.model.constant.DateConstant.XMAS; + +import christmas.model.constant.DateConstant; + +public class Date { + private final int MIN_DATE = DateConstant.MIN_DATE.getDate(); + private final int MAX_DATE = DateConstant.MAX_DATE.getDate(); + private final int date; + + private Date(int date) { + validateDateRange(date); + this.date = date; + } + + public static Date of(int date) { + return new Date(date); + } + + public int getValue() { + return date; + } + + private void validateDateRange(int date) { + if (date < MIN_DATE || date > MAX_DATE) { + throw DATE_RANGE_EXCEPTION; + } + } + + public boolean isWeekday() { + return WEEKDAYS.contains(date); + } + + public boolean isWeekend() { + return WEEKENDS.contains(date); + } + + public boolean isBeforeXmas() { + return XMAS.getDate() >= date; + } + + public boolean isSpecialDay() { + return SPECIAL_DAYS.contains(date); + } + + +}
Java
๋นŒํŠธ์ธ ๊ฐ์ฒด Date์™€ ํ˜ผ๋™๋  ์ˆ˜ ์žˆ์–ด์„œ ๋‹ค๋ฅธ ๋„ค์ด๋ฐ์„ ๋ฐ˜์˜ํ•˜๋Š”๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -21,7 +21,9 @@ <body> <div class="create-account"> <h1 class="heading xl3">Create <br />Account</h1> - <input type="file" id="file_1" class="a11y-hidden" ><label for="file_1" class="input-file"></label> + <div class="file-wrapper"> + <input type="file" id="file_1" class="a11y-hidden" ><label for="file_1" class="input-file"></label> + </div> <div class="account-info"> <input type="email" class="input md" placeholder="Email"> <div class="password"> @@ -43,7 +45,6 @@ <h1 class="heading xl3">Create <br />Account</h1> <button class="btn lg3 blue radius-16 btn-done" onclick="goCreate()">Done</button> <p class="text md3 btn-cancel" onclick="goCancel()">Cancel</p> </div> - <!-- <script type="module" src="./js/common.js" defer></script> --> <script defer> //์ƒˆ๋กœ์šด ํŽ˜์ด์ง€ ์ด๋™ํ•˜๊ธฐ const goCreate = () => {
Unknown
๊ฐœ์ธ์ฐจ๊ฐ€ ์žˆ๊ฒ ์ง€๋งŒ, ์ „ ๊ฐœ์ธ์ ์œผ๋กœ ์ด๋ฒคํŠธ๋Š” js์—์„œ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•˜๋Š”ํŽธ์ž…๋‹ˆ๋‹ค.. react๋ฉด ์ปดํผ๋„ŒํŠธ๋กœ ๊ด€๋ฆฌ๋˜๊ธฐ๋•Œ๋ฌธ์— ๋ฌด๊ด€ํ•˜์ง€๋งŒ.. html์—์„  ๋‚˜์ค‘์—” ๋ชจ๋“ ํŽ˜์ด์ง€ ์ฐพ์•„๊ฐ€๋ฉด์„œ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์ œ๊ฑฐ ๋˜๋Š” ์ถ”๊ฐ€ํ•ด์•ผ๋  ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ฑฐ๋“ ์š”.
@@ -21,7 +21,9 @@ <body> <div class="create-account"> <h1 class="heading xl3">Create <br />Account</h1> - <input type="file" id="file_1" class="a11y-hidden" ><label for="file_1" class="input-file"></label> + <div class="file-wrapper"> + <input type="file" id="file_1" class="a11y-hidden" ><label for="file_1" class="input-file"></label> + </div> <div class="account-info"> <input type="email" class="input md" placeholder="Email"> <div class="password"> @@ -43,7 +45,6 @@ <h1 class="heading xl3">Create <br />Account</h1> <button class="btn lg3 blue radius-16 btn-done" onclick="goCreate()">Done</button> <p class="text md3 btn-cancel" onclick="goCancel()">Cancel</p> </div> - <!-- <script type="module" src="./js/common.js" defer></script> --> <script defer> //์ƒˆ๋กœ์šด ํŽ˜์ด์ง€ ์ด๋™ํ•˜๊ธฐ const goCreate = () => {
Unknown
๋ชจ๋“  ์˜จ๋ผ์ธ์„œ์‹์—๋Š” title๋˜๋Š” label์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
@@ -21,7 +21,9 @@ <body> <div class="create-account"> <h1 class="heading xl3">Create <br />Account</h1> - <input type="file" id="file_1" class="a11y-hidden" ><label for="file_1" class="input-file"></label> + <div class="file-wrapper"> + <input type="file" id="file_1" class="a11y-hidden" ><label for="file_1" class="input-file"></label> + </div> <div class="account-info"> <input type="email" class="input md" placeholder="Email"> <div class="password"> @@ -43,7 +45,6 @@ <h1 class="heading xl3">Create <br />Account</h1> <button class="btn lg3 blue radius-16 btn-done" onclick="goCreate()">Done</button> <p class="text md3 btn-cancel" onclick="goCancel()">Cancel</p> </div> - <!-- <script type="module" src="./js/common.js" defer></script> --> <script defer> //์ƒˆ๋กœ์šด ํŽ˜์ด์ง€ ์ด๋™ํ•˜๊ธฐ const goCreate = () => {
Unknown
์Šคํฌ๋ฆฝํŠธ๋Š” ์™ธ๋ถ€ํŒŒ์ผ๋กœ.. alert๋ณด๋‹จ dialog๋ฅผ ์ด์šฉํ•ด์„œ ๊ฒฝ๊ณ ์ฐฝ์„ ๊พธ๋ฉฐ๋ณด๋Š”๊ฒƒ๋„ ๋„์›€๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋Šฅ๋ณ„๋กœ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•˜์…”์„œ moduleํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์‹œ๋„ํ•ด๋ณด์„ธ์š”~
@@ -1,11 +1,32 @@ -import { useState } from "react"; +import '@styles/App.css'; +import '@styles/reset.css'; +import '@styles/global.css'; +import { Header, Layout, PageRequestError } from '@components/index'; +import { lazy, Suspense } from 'react'; +import { ErrorBoundary } from 'react-error-boundary'; + +import { CartItemsContext } from './contexts'; +import { useCartItemIds } from './hooks'; +import ProductListPageSkeleton from './pages/ProductListPage/Skeleton/ProductListPageSkeleton/index.'; + +const ProductListPage = lazy(() => import('@pages/ProductListPage')); function App() { - const [count, setCount] = useState(0); + const { cartItemIds, refreshCartItemIds, error: cartItemsFetchError } = useCartItemIds(); + const cartItemsLength = cartItemIds ? Array.from(cartItemIds).length : 0; return ( <> - <h1>React Shopping Products</h1> + <Header cartItemsLength={cartItemsLength} cartItemsFetchError={cartItemsFetchError} /> + <Layout> + <ErrorBoundary FallbackComponent={({ error }) => <PageRequestError error={error} />}> + <CartItemsContext.Provider value={{ cartItemIds, refreshCartItemIds }}> + <Suspense fallback={<ProductListPageSkeleton />}> + <ProductListPage /> + </Suspense> + </CartItemsContext.Provider> + </ErrorBoundary> + </Layout> </> ); }
Unknown
์ƒํ’ˆ์˜ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋‹ด๊ธฐ/๋นผ๊ธฐ ํด๋ฆญ ์‹œ api์š”์ฒญ ํ›„ ๊ฒฐ๊ณผ์— ๋งž์ถ”์–ด Header ์† ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์•„์ด์ฝ˜์— ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก ์ˆ˜๋Ÿ‰์ด ์—…๋ฐ์ดํŠธ ๋˜์–ด์•ผํ•ด์„œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋‹ด๊ธฐ/๋นผ๊ธฐ + ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•ด์ฃผ๋Š” handleCartActionํ•จ์ˆ˜๋ฅผ context api๋กœ ๋‚ด๋ ค์ฃผ๊ณ  ์žˆ์–ด์š”. ํฌ๋ฃจ๋Š” ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•˜์…จ๋‚˜์š”?
@@ -1,11 +1,32 @@ -import { useState } from "react"; +import '@styles/App.css'; +import '@styles/reset.css'; +import '@styles/global.css'; +import { Header, Layout, PageRequestError } from '@components/index'; +import { lazy, Suspense } from 'react'; +import { ErrorBoundary } from 'react-error-boundary'; + +import { CartItemsContext } from './contexts'; +import { useCartItemIds } from './hooks'; +import ProductListPageSkeleton from './pages/ProductListPage/Skeleton/ProductListPageSkeleton/index.'; + +const ProductListPage = lazy(() => import('@pages/ProductListPage')); function App() { - const [count, setCount] = useState(0); + const { cartItemIds, refreshCartItemIds, error: cartItemsFetchError } = useCartItemIds(); + const cartItemsLength = cartItemIds ? Array.from(cartItemIds).length : 0; return ( <> - <h1>React Shopping Products</h1> + <Header cartItemsLength={cartItemsLength} cartItemsFetchError={cartItemsFetchError} /> + <Layout> + <ErrorBoundary FallbackComponent={({ error }) => <PageRequestError error={error} />}> + <CartItemsContext.Provider value={{ cartItemIds, refreshCartItemIds }}> + <Suspense fallback={<ProductListPageSkeleton />}> + <ProductListPage /> + </Suspense> + </CartItemsContext.Provider> + </ErrorBoundary> + </Layout> </> ); }
Unknown
์ €ํฌ๋„ ์žฅ๋ฐ”๊ตฌ๋‹ˆ api์ธ cart-tiems์˜ ๋ฐ์ดํ„ฐ๋Š” context๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,39 @@ +import { Filtering, Product, ApiResponse } from '@appTypes/index'; +import { LOAD_MORE_PRODUCTS_AMOUNT } from '@constants/index'; +import { fetchWithToken } from '@utils/index'; + +import { END_POINTS } from './endPoints'; + +interface FetchProductParameter { + filtering: Filtering; + page?: number; +} + +const getSearchParams = ({ filtering, page }: FetchProductParameter): URLSearchParams => { + const searchParams = new URLSearchParams(); + + if (filtering.category !== '') { + searchParams.append('category', filtering.category); + } + + searchParams.append('sort', filtering.sort); + searchParams.append('page', String(page)); + + if (page !== 0) { + searchParams.append('size', LOAD_MORE_PRODUCTS_AMOUNT.toString()); + } + + return searchParams; +}; + +export async function fetchProduct(params: FetchProductParameter): Promise<{ products: Product[]; isLast: boolean }> { + const searchParams = '?' + getSearchParams({ ...params, page: params.page ?? 0 }).toString(); + + const data = await fetchWithToken({ + url: END_POINTS.products + searchParams, + method: 'GET', + }); + const result = (await data.json()) as ApiResponse<Product[]>; + + return { products: result.content, isLast: result.last }; +}
TypeScript
page์˜ ํƒ€์ž…์ด number์—ฌ์„œ string์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ์‹  ๊ฒƒ ๊ฐ™์€๋ฐ, ์ œ๊ฐ€ ์•Œ๊ธฐ๋กœ๋Š” ๊ทธ๋ƒฅ ์ˆซ์ž๋ฅผ ๋„ฃ์–ด๋„ ๋œ๋‹ค๊ณ  ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค..! ํ˜น์‹œ string์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋„ฃ์€ ๊ฒƒ์— ๋Œ€ํ•ด ์˜๋ฏธ๊ฐ€ ์žˆ๋Š” ๊ฑธ ๊นŒ์š”?? (๊ทผ๋ฐ ํ™•์‹คํ•˜์ง„ ์•Š์•„์šง.. ํ‹€๋ฆฌ๋จผ ์Šค๋ฃจ ํ•ด์ฃผ์‹ญ์‡ผ..) https://developer.mozilla.org/ko/docs/Web/API/URLSearchParams/get
@@ -0,0 +1,15 @@ +export type Category = '' | 'fashion' | 'beverage' | 'electronics' | 'kitchen' | 'fitness' | 'books'; +export type PriceSort = 'price,asc' | 'price,desc'; + +export interface Product { + id: number; + name: string; + price: number; + imageUrl: string; + category: Category; +} + +export interface Filtering { + category: Category; + sort: PriceSort; +}
TypeScript
๋นˆ ๋ฌธ์ž์—ด์ด **์ „์ฒด** ๋•Œ๋ฌธ์— ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ด๋Š”๋ฐ, Category๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ณณ์—์„œ `Category | undefined`์™€ ๊ฐ™์€ ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ ๋นˆ ๋ฌธ์ž์—ด๋กœ ๋„ฃ์–ด์ค€ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”??
@@ -0,0 +1,32 @@ +/** + * ์„œ๋ฒ„ ์‘๋‹ต์˜ ๊ณตํ†ต ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค + * @template T - response์˜ contentํƒ€์ž… + */ +export interface ApiResponse<T> { + totalPages: number; + totalElements: number; + sort: { + sorted: boolean; + unsorted: boolean; + empty: boolean; + }; + first: boolean; + last: boolean; + pageable: { + sort: { + sorted: boolean; + unsorted: boolean; + empty: boolean; + }; + pageNumber: number; + pageSize: number; + paged: boolean; + unpaged: boolean; + offset: number; + }; + number: number; + numberOfElements: number; + size: number; + content: T; + empty: boolean; +}
TypeScript
์˜ค.. ์ด๋ ‡๊ฒŒ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ์ข‹๋„ค์š”~!
@@ -0,0 +1,30 @@ +import { DropdownOption } from '@appTypes/dropdown'; + +import style from './style.module.css'; + +interface DropdownProps extends Omit<React.HTMLProps<HTMLSelectElement>, 'onChange'> { + options: DropdownOption[]; + onChange: (event: React.ChangeEvent<HTMLSelectElement>) => void; + label: string; +} + +function Dropdown({ options, label, onChange, name, ...rest }: DropdownProps) { + const selectId = `select-${label}`; + + return ( + <> + <label className="scr-only" htmlFor={selectId}> + {label} + </label> + <select id={selectId} name={name} className={style.select} onChange={onChange} {...rest}> + {options.map((item) => ( + <option key={item.value} value={item.value}> + {item.label} + </option> + ))} + </select> + </> + ); +} + +export default Dropdown;
Unknown
ํ•˜๋ฃจ๊ฐ€ ๋„ˆ๋ฌด ์ข‹์•„ ํ•  ๊ฒƒ ๊ฐ™์€!
@@ -0,0 +1,15 @@ +export type Category = '' | 'fashion' | 'beverage' | 'electronics' | 'kitchen' | 'fitness' | 'books'; +export type PriceSort = 'price,asc' | 'price,desc'; + +export interface Product { + id: number; + name: string; + price: number; + imageUrl: string; + category: Category; +} + +export interface Filtering { + category: Category; + sort: PriceSort; +}
TypeScript
api ์š”์ฒญ ๋ณด๋‚ด๋Š” ๊ฒƒ๊ณผ ํƒ€์ž…์„ ๋งž์ถ”์—ˆ์–ด์š”. ์ง€๊ธˆ๋ณด๋‹ˆ, ''๋ฅผ ALL์ด๋ผ๋Š” ์ƒ์ˆ˜๋ฅผ ๋ฐ›๋“œ๋Š” ๊ฒƒ์ด ์„ ์–ธ์ ์œผ๋กœ ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š”.
@@ -0,0 +1,22 @@ +package christmas.constant; + +public enum ResultMessage { + BENEFIT_PREVIEW("12์›” 3์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!"), + ORDER_MENU("\n<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"), + ALL_ORDER_PRICE_WITHOUT_DISCOUNT("\n<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>"), + GIVE_MENU("\n<์ฆ์ • ๋ฉ”๋‰ด>"), + BENEFIT("\n<ํ˜œํƒ ๋‚ด์—ญ>"), + ALL_BENEFIT_AMOUNT("\n<์ดํ˜œํƒ ๊ธˆ์•ก>"), + PAYMENT_PRICE("\n<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>"), + BADGE("\n<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>"); + + private String message; + + ResultMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +}
Java
๊ฐœ์ธ์ ์œผ๋กœ ๋ฌธ์ž์—ด์— ๊ฐœํ–‰์ด ๋“ค์–ด๊ฐ€ ์žˆ์œผ๋ฉด ๋‚˜์ค‘์— ๋””๋ฒ„๊น…์ด ์‰ฝ์ง€ ์•Š๋”๊ตฐ์š”..
@@ -0,0 +1,126 @@ +package christmas.controller; + +import christmas.domain.customer.CustomerInfo; +import christmas.domain.customer.VisitDate; +import christmas.domain.order.Orders; +import christmas.service.EventService; +import christmas.service.RestaurantService; +import christmas.view.InputView; +import christmas.view.OutputView; + +import java.util.Collections; + +public class RestaurantController { + private final InputView inputView; + private final OutputView outputView; + private final RestaurantService restaurantService; + private EventService eventService; + + private RestaurantController(InputView inputView, OutputView outputView, RestaurantService restaurantService) { + this.inputView = inputView; + this.outputView = outputView; + this.restaurantService = restaurantService; + } + + public static RestaurantController of(InputView inputView, OutputView outputView, RestaurantService restaurantService) { + return new RestaurantController(inputView, outputView, restaurantService); + } + + public void run() { + inputView.printInitialMessage(); + createCustomerInfo(); + eventService = EventService.of( + getCustomerInfoFromRestaurantService() + ); + applyDiscount(); + printApplyEvent(); + } + + public void createCustomerInfo() { + restaurantService.createCustomerInfo(createVisitDate(), createOrdersFromMenuAndNum()); + } + + private VisitDate createVisitDate() { + while (true) { + try { + return restaurantService.createVisitDate(Integer.parseInt(inputView.readVisitDate())); + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e); + } + } + } + + private Orders createOrdersFromMenuAndNum() { + while (true) { + try { + String inputMenuAndNum = inputView.readMenuAndNum(); + return restaurantService.processCreateOrders(inputMenuAndNum); + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e); + } + } + } + + private CustomerInfo getCustomerInfoFromRestaurantService() { + return restaurantService.getCustomerInfo(); + } + + public void applyDiscount() { + eventService.applyDiscount(); + } + + public void printApplyEvent() { + printPreviewMessage(); + printOrders(); + printTotalOrderAmountWithoutDiscount(); + printGiveawayMenu(); + printBenefits(); + printTotalBenefitAmount(); + printExpectedPaymentAmount(); + printEventBadge(); + } + + private void printPreviewMessage() { + outputView.printBenefitPreview(); + } + + private void printOrders() { + outputView.printOrders(eventService.getOrderList()); + } + + private void printTotalOrderAmountWithoutDiscount() { + outputView.printTotalOrderAmountWithoutDiscount(eventService.getTotalOrderAmount()); + } + + private void printGiveawayMenu() { + outputView.printGiveawayMenu(eventService.getGiveaway()); + } + + private void printTotalBenefitAmount() { + outputView.printTotalBenefitAmount(eventService.getTotalBenefitAmount()); + } + + private void printBenefits() { + if (eventService.getBenefits().size() == 0) { + outputView.printBenefits(Collections.emptyMap()); + return; + } + outputView.printBenefits(eventService.getBenefits()); + } + + private void printExpectedPaymentAmount() { + outputView.printExpectedPaymentAmount( + expectedPaymentAmount( + eventService.getTotalOrderAmount(), + eventService.getTotalDiscountAmount() + )); + } + + private int expectedPaymentAmount(int orderAmount, int benefitAmount) { + return orderAmount + benefitAmount; + } + + private void printEventBadge() { + outputView.printEventBadge(eventService.getBadge()); + } +}
Java
์ถœ๋ ฅ๋ถ€๋ฌธ์ด ์„œ๋น„์Šค์™€ ๊ฒฐํ•ฉ๋˜์–ด ์žˆ๋Š”๊ฒŒ ๋งŽ์€ ๊ฒƒ ๊ฐ™์€๋ฐ, ์•„์˜ˆ ์„œ๋น„์Šค ์•ˆ์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค!
@@ -0,0 +1,55 @@ +package christmas.domain.customer; + +import christmas.constant.NumConstant; +import christmas.validator.DateValidator; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +public class VisitDate { + private final int visitDate; + + private VisitDate(int visitDate) { + DateValidator.validateVisitDateBound(visitDate); + this.visitDate = visitDate; + } + + public static VisitDate from(int visitDate) { + return new VisitDate(visitDate); + } + + public DayOfWeek getDayOfWeek() { + LocalDate date = LocalDate.of( + NumConstant.THIS_YEAR.getValue(), + NumConstant.THIS_MONTH.getValue(), + visitDate + ); + return date.getDayOfWeek(); + } + + public boolean isVisitDateWeekend() { + DayOfWeek dayOfWeek = getDayOfWeek(); + if (dayOfWeek == DayOfWeek.FRIDAY || dayOfWeek == DayOfWeek.SATURDAY) { + return true; + } + return false; + } + + public boolean isOverChristmas() { + if(visitDate > NumConstant.CHRISTMAS_DAY.getValue()) { + return true; + } + return false; + } + + public int calculateDdayEventApplyDay() { + return visitDate - 1; + } + + public boolean isSpecialDay() { + if (visitDate == NumConstant.CHRISTMAS_DAY.getValue() || getDayOfWeek() == DayOfWeek.SUNDAY) { + return true; + } + return false; + } +}
Java
์–ด๋–ค ๊ฑธ ์˜๋„ํ•˜์…จ๋Š”์ง€ ์—ฌ์ญค๋ด๋„ ๋ ๊นŒ์š”?
@@ -0,0 +1,68 @@ +package christmas.service; + +import christmas.constant.Badge; +import christmas.domain.customer.CustomerInfo; +import christmas.domain.event.EventStatus; +import christmas.handler.EventHandler; + +import java.util.Collections; +import java.util.Map; + +public class EventService { + private final EventHandler eventHandler; + private final CustomerInfo customerInfo; + + private EventService(CustomerInfo customerInfo) { + eventHandler = EventHandler.from(EventStatus.createInstance()); + this.customerInfo = customerInfo; + } + + public static EventService of(CustomerInfo customerInfo) { + return new EventService(customerInfo); + } + + public void applyDiscount() { + eventHandler.applyDiscount(customerInfo); + } + + public int getTotalBenefitAmount() { + if (customerInfo.isOverMinAmount()) { + return eventHandler.getTotalBenefitAmount(); + } + return 0; + } + + public Map<String, Integer> getBenefits() { + if (customerInfo.isOverMinAmount()) { + return eventHandler.getBenefits(); + } + return Collections.emptyMap(); + } + + public Map<String, Integer> getOrderList() { + return customerInfo.getOrderList(); + } + + public String getGiveaway() { + if (customerInfo.isApplyGiveawayEvent()) { + return "์ƒดํŽ˜์ธ 1๊ฐœ"; + } + return "์—†์Œ"; + } + + public int getTotalOrderAmount() { + return customerInfo.getTotalOrderAmount(); + } + + public int getTotalDiscountAmount() { + if (customerInfo.isOverMinAmount()) { + return eventHandler.getTotalDiscountAmount(); + } + return 0; + } + + public String getBadge() { + return Badge.getBadge(eventHandler.getTotalBenefitAmount()); + } + +}
Java
๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ๋ณด๋‹ค ์ฆ์ •ํ’ˆ์— ๋Œ€ํ•œ ๊ฐ์ฒด๊ฐ€ ์žˆ์–ด์„œ ๊ทธ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,31 @@ +package christmas.constant; + +public enum Badge { + MISS("์—†์Œ"), + STAR("๋ณ„"), + TREE("ํŠธ๋ฆฌ"), + SANTA("์‚ฐํƒ€"); + private String name; + + Badge(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public static String getBadge(int totalBenefitAmount) { + totalBenefitAmount = Math.abs(totalBenefitAmount); + if(totalBenefitAmount >= NumConstant.STAR_BADGE_AMOUNT.getValue() && totalBenefitAmount < NumConstant.TREE_BADGE_AMOUNT.getValue()) { + return STAR.name; + } + if(totalBenefitAmount >= NumConstant.TREE_BADGE_AMOUNT.getValue() && totalBenefitAmount < NumConstant.SANTA_BADGE_AMOUNT.getValue()) { + return TREE.name; + } + if(totalBenefitAmount >= NumConstant.SANTA_BADGE_AMOUNT.getValue()) { + return SANTA.name; + } + return MISS.name; + } +}
Java
์ €๋„ ๋˜‘๊ฐ™์ด ๋ฐ›์•˜๋˜ ํ”ผ๋“œ๋ฐฑ์ธ๋ฐ, ``` SANTA("์‚ฐํƒ€, 25_000"); public static String getBadge(final int totalBenefitAmount) { for(Badge b : Badge.values()){ if(totalBenefitAmount>=b.getValue()){ return b.name; } } return "์—†์Œ"; } ``` ๋กœ ๊ณ ์ณ๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? ๋” ๊ฐ„๋‹จํ•ด ์งˆ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,126 @@ +package christmas.controller; + +import christmas.domain.customer.CustomerInfo; +import christmas.domain.customer.VisitDate; +import christmas.domain.order.Orders; +import christmas.service.EventService; +import christmas.service.RestaurantService; +import christmas.view.InputView; +import christmas.view.OutputView; + +import java.util.Collections; + +public class RestaurantController { + private final InputView inputView; + private final OutputView outputView; + private final RestaurantService restaurantService; + private EventService eventService; + + private RestaurantController(InputView inputView, OutputView outputView, RestaurantService restaurantService) { + this.inputView = inputView; + this.outputView = outputView; + this.restaurantService = restaurantService; + } + + public static RestaurantController of(InputView inputView, OutputView outputView, RestaurantService restaurantService) { + return new RestaurantController(inputView, outputView, restaurantService); + } + + public void run() { + inputView.printInitialMessage(); + createCustomerInfo(); + eventService = EventService.of( + getCustomerInfoFromRestaurantService() + ); + applyDiscount(); + printApplyEvent(); + } + + public void createCustomerInfo() { + restaurantService.createCustomerInfo(createVisitDate(), createOrdersFromMenuAndNum()); + } + + private VisitDate createVisitDate() { + while (true) { + try { + return restaurantService.createVisitDate(Integer.parseInt(inputView.readVisitDate())); + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e); + } + } + } + + private Orders createOrdersFromMenuAndNum() { + while (true) { + try { + String inputMenuAndNum = inputView.readMenuAndNum(); + return restaurantService.processCreateOrders(inputMenuAndNum); + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e); + } + } + } + + private CustomerInfo getCustomerInfoFromRestaurantService() { + return restaurantService.getCustomerInfo(); + } + + public void applyDiscount() { + eventService.applyDiscount(); + } + + public void printApplyEvent() { + printPreviewMessage(); + printOrders(); + printTotalOrderAmountWithoutDiscount(); + printGiveawayMenu(); + printBenefits(); + printTotalBenefitAmount(); + printExpectedPaymentAmount(); + printEventBadge(); + } + + private void printPreviewMessage() { + outputView.printBenefitPreview(); + } + + private void printOrders() { + outputView.printOrders(eventService.getOrderList()); + } + + private void printTotalOrderAmountWithoutDiscount() { + outputView.printTotalOrderAmountWithoutDiscount(eventService.getTotalOrderAmount()); + } + + private void printGiveawayMenu() { + outputView.printGiveawayMenu(eventService.getGiveaway()); + } + + private void printTotalBenefitAmount() { + outputView.printTotalBenefitAmount(eventService.getTotalBenefitAmount()); + } + + private void printBenefits() { + if (eventService.getBenefits().size() == 0) { + outputView.printBenefits(Collections.emptyMap()); + return; + } + outputView.printBenefits(eventService.getBenefits()); + } + + private void printExpectedPaymentAmount() { + outputView.printExpectedPaymentAmount( + expectedPaymentAmount( + eventService.getTotalOrderAmount(), + eventService.getTotalDiscountAmount() + )); + } + + private int expectedPaymentAmount(int orderAmount, int benefitAmount) { + return orderAmount + benefitAmount; + } + + private void printEventBadge() { + outputView.printEventBadge(eventService.getBadge()); + } +}
Java
์™€! ์ธ์Šคํ„ด์Šค ํ•„๋“œ๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์ž˜ ์ค„์ธ๊ฒƒ ๊ฐ™์•„์„œ ์—„์ฒญ ์ข‹๋„ค์š”!!
@@ -0,0 +1,126 @@ +package christmas.controller; + +import christmas.domain.customer.CustomerInfo; +import christmas.domain.customer.VisitDate; +import christmas.domain.order.Orders; +import christmas.service.EventService; +import christmas.service.RestaurantService; +import christmas.view.InputView; +import christmas.view.OutputView; + +import java.util.Collections; + +public class RestaurantController { + private final InputView inputView; + private final OutputView outputView; + private final RestaurantService restaurantService; + private EventService eventService; + + private RestaurantController(InputView inputView, OutputView outputView, RestaurantService restaurantService) { + this.inputView = inputView; + this.outputView = outputView; + this.restaurantService = restaurantService; + } + + public static RestaurantController of(InputView inputView, OutputView outputView, RestaurantService restaurantService) { + return new RestaurantController(inputView, outputView, restaurantService); + } + + public void run() { + inputView.printInitialMessage(); + createCustomerInfo(); + eventService = EventService.of( + getCustomerInfoFromRestaurantService() + ); + applyDiscount(); + printApplyEvent(); + } + + public void createCustomerInfo() { + restaurantService.createCustomerInfo(createVisitDate(), createOrdersFromMenuAndNum()); + } + + private VisitDate createVisitDate() { + while (true) { + try { + return restaurantService.createVisitDate(Integer.parseInt(inputView.readVisitDate())); + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e); + } + } + } + + private Orders createOrdersFromMenuAndNum() { + while (true) { + try { + String inputMenuAndNum = inputView.readMenuAndNum(); + return restaurantService.processCreateOrders(inputMenuAndNum); + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e); + } + } + } + + private CustomerInfo getCustomerInfoFromRestaurantService() { + return restaurantService.getCustomerInfo(); + } + + public void applyDiscount() { + eventService.applyDiscount(); + } + + public void printApplyEvent() { + printPreviewMessage(); + printOrders(); + printTotalOrderAmountWithoutDiscount(); + printGiveawayMenu(); + printBenefits(); + printTotalBenefitAmount(); + printExpectedPaymentAmount(); + printEventBadge(); + } + + private void printPreviewMessage() { + outputView.printBenefitPreview(); + } + + private void printOrders() { + outputView.printOrders(eventService.getOrderList()); + } + + private void printTotalOrderAmountWithoutDiscount() { + outputView.printTotalOrderAmountWithoutDiscount(eventService.getTotalOrderAmount()); + } + + private void printGiveawayMenu() { + outputView.printGiveawayMenu(eventService.getGiveaway()); + } + + private void printTotalBenefitAmount() { + outputView.printTotalBenefitAmount(eventService.getTotalBenefitAmount()); + } + + private void printBenefits() { + if (eventService.getBenefits().size() == 0) { + outputView.printBenefits(Collections.emptyMap()); + return; + } + outputView.printBenefits(eventService.getBenefits()); + } + + private void printExpectedPaymentAmount() { + outputView.printExpectedPaymentAmount( + expectedPaymentAmount( + eventService.getTotalOrderAmount(), + eventService.getTotalDiscountAmount() + )); + } + + private int expectedPaymentAmount(int orderAmount, int benefitAmount) { + return orderAmount + benefitAmount; + } + + private void printEventBadge() { + outputView.printEventBadge(eventService.getBadge()); + } +}
Java
์ด ๋ถ€๋ถ„์€ - ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์•„์˜จ๋‹ค - ์„œ๋น„์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค ๋ผ๋Š” ๋กœ์ง์„ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ์–ด ๋ถ„๋ฆฌ๊ฐ€ ํ•„์šฉํ•ด ๋ณด์ž…๋‹ˆ๋‹ค!
@@ -0,0 +1,63 @@ +package christmas.domain.customer; + +import christmas.constant.MenuType; +import christmas.constant.NumConstant; +import christmas.domain.order.Orders; + +import java.util.Map; + +public class CustomerInfo { + private final VisitDate visitDate; + private final Orders orders; + + private CustomerInfo(VisitDate visitDate, Orders orders) { + this.visitDate = visitDate; + this.orders = orders; + } + + public static CustomerInfo of(VisitDate visitDate, Orders orders) { + return new CustomerInfo(visitDate, orders); + } + + public boolean isOverMinAmount() { + if (getTotalOrderAmount() > NumConstant.MIN_EVENT_APPLY_AMOUNT.getValue()) { + return true; + } + return false; + } + + public boolean isVisitDateWeekend() { + return visitDate.isVisitDateWeekend(); + } + + public boolean isOverChristmas() { + return visitDate.isOverChristmas(); + } + + public int getTotalOrderAmount() { + return orders.getTotalOrderAmount(); + } + + public int getDdayEventApplyDay() { + return visitDate.calculateDdayEventApplyDay(); + } + + public int getOrderNumByMenuType(MenuType menuType) { + return orders.getOrderNumByMenuType(menuType); + } + + public boolean isApplyGiveawayEvent() { + if (orders.getTotalOrderAmount() >= NumConstant.MIN_GIVEAWAY_AMOUNT.getValue()) { + return true; + } + return false; + } + + public boolean isSpecialDay() { + return visitDate.isSpecialDay(); + } + + public Map<String, Integer> getOrderList() { + return orders.getOrderList(); + } +}
Java
์ฝ”๋“œ ์ปจ๋ฒค์…˜ ์ˆœ์„œ์— ๋งž์ถฐ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง๋ณด๋‹ค getter๋ฅผ ์•„๋ž˜์— ๋‘๋Š” ๊ฒƒ์ด ๊น”๋”ํ•ด๋ณด์ž…๋‹ˆ๋‹ค!
@@ -0,0 +1,38 @@ +package christmas.domain.order; + +import christmas.constant.MenuType; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Orders { + private final List<Order> orders; + + private Orders(List<Order> orders) { + this.orders = orders; + } + + public static Orders from(List<Order> orders) { + return new Orders(orders); + } + + public int getTotalOrderAmount() { + return orders.stream() + .map(Order::getOrderAmount) + .reduce(0, Integer::sum); + } + + public int getOrderNumByMenuType(MenuType menuType) { + return orders.stream() + .filter(order -> order.isMatchMenuType(menuType)) + .mapToInt(Order::getMenuNum) + .sum(); + } + + public Map<String, Integer> getOrderList() { + return orders.stream() + .collect(Collectors.toMap(Order::getMenuName, Order::getMenuNum)); + } +}
Java
list๋ผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์„ ๊ทธ๋Œ€๋กœ๋„ค์ด๋ฐ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ง€์–‘ํ•˜๋ผ๊ณ  ํ”ผ๋“œ๋ฐฑ์— ๋‚˜์™€์žˆ์—ˆ์Šต๋‹ˆ๋‹ค! ๋˜, getter๋กœ list์˜๋ฐ˜ํ™˜์‹œ์— list์˜ ๋ณ€๊ฒฝ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์œผ๋ฏ€๋กœ Collections.unmodifi...๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ ์ ˆํ•ด๋ณด์ด๋„ค์š”!
@@ -0,0 +1,48 @@ +package christmas.domain.menu; + +import christmas.constant.MenuType; + +import java.util.*; + +public class Menus { + private final List<Menu> menus; + private static Menus instance; + + private Menus(List<Menu> menus) { + this.menus = menus; + } + + public static Menus getInstance() { + if(instance == null) { + instance = new Menus(initialMenus()); + } + return instance; + } + + private static List<Menu> initialMenus() { + List<Menu> initialMenus = new ArrayList<>(); + + initialMenus.add(Menu.of("์–‘์†ก์ด์ˆ˜ํ”„", 6000, MenuType.APPETIZER)); + initialMenus.add(Menu.of("ํƒ€ํŒŒ์Šค", 5500, MenuType.APPETIZER)); + initialMenus.add(Menu.of("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000, MenuType.APPETIZER)); + initialMenus.add(Menu.of("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, MenuType.MAIN)); + initialMenus.add(Menu.of("๋ฐ”๋น„ํ๋ฆฝ", 54000, MenuType.MAIN)); + initialMenus.add(Menu.of("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, MenuType.MAIN)); + initialMenus.add(Menu.of("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000, MenuType.MAIN)); + initialMenus.add(Menu.of("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, MenuType.DESSERT)); + initialMenus.add(Menu.of("์•„์ด์Šคํฌ๋ฆผ", 5000, MenuType.DESSERT)); + initialMenus.add(Menu.of("์ œ๋กœ์ฝœ๋ผ", 3000, MenuType.DRINK)); + initialMenus.add(Menu.of("๋ ˆ๋“œ์™€์ธ", 60000, MenuType.DRINK)); + initialMenus.add(Menu.of("์ƒดํŽ˜์ธ", 25000, MenuType.DRINK)); + + return initialMenus; + } + + public Optional<Menu> getMenuByMenuName(String menuName) { + return menus.stream() + .filter(menu -> menu.getMenuName().equals(menuName)) + .findFirst(); + + } + +}
Java
์ด๋ ‡๊ฒŒ ์‹ฑ๊ธ€ํ†ค ๋ฐฉ์‹์œผ๋กœ ์„ ์–ธํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? enum์„ ์‚ฌ์šฉํ•ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,22 @@ +package christmas.constant; + +public enum ResultMessage { + BENEFIT_PREVIEW("12์›” 3์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!"), + ORDER_MENU("\n<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"), + ALL_ORDER_PRICE_WITHOUT_DISCOUNT("\n<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>"), + GIVE_MENU("\n<์ฆ์ • ๋ฉ”๋‰ด>"), + BENEFIT("\n<ํ˜œํƒ ๋‚ด์—ญ>"), + ALL_BENEFIT_AMOUNT("\n<์ดํ˜œํƒ ๊ธˆ์•ก>"), + PAYMENT_PRICE("\n<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>"), + BADGE("\n<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>"); + + private String message; + + ResultMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +}
Java
์•—... ๊ทธ๋Ÿฐ ๋ฌธ์ œ๊ฐ€ ์ข€ ์žˆ์—ˆ๊ตฐ์š” ๐Ÿ˜ฑ
@@ -0,0 +1,126 @@ +package christmas.controller; + +import christmas.domain.customer.CustomerInfo; +import christmas.domain.customer.VisitDate; +import christmas.domain.order.Orders; +import christmas.service.EventService; +import christmas.service.RestaurantService; +import christmas.view.InputView; +import christmas.view.OutputView; + +import java.util.Collections; + +public class RestaurantController { + private final InputView inputView; + private final OutputView outputView; + private final RestaurantService restaurantService; + private EventService eventService; + + private RestaurantController(InputView inputView, OutputView outputView, RestaurantService restaurantService) { + this.inputView = inputView; + this.outputView = outputView; + this.restaurantService = restaurantService; + } + + public static RestaurantController of(InputView inputView, OutputView outputView, RestaurantService restaurantService) { + return new RestaurantController(inputView, outputView, restaurantService); + } + + public void run() { + inputView.printInitialMessage(); + createCustomerInfo(); + eventService = EventService.of( + getCustomerInfoFromRestaurantService() + ); + applyDiscount(); + printApplyEvent(); + } + + public void createCustomerInfo() { + restaurantService.createCustomerInfo(createVisitDate(), createOrdersFromMenuAndNum()); + } + + private VisitDate createVisitDate() { + while (true) { + try { + return restaurantService.createVisitDate(Integer.parseInt(inputView.readVisitDate())); + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e); + } + } + } + + private Orders createOrdersFromMenuAndNum() { + while (true) { + try { + String inputMenuAndNum = inputView.readMenuAndNum(); + return restaurantService.processCreateOrders(inputMenuAndNum); + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e); + } + } + } + + private CustomerInfo getCustomerInfoFromRestaurantService() { + return restaurantService.getCustomerInfo(); + } + + public void applyDiscount() { + eventService.applyDiscount(); + } + + public void printApplyEvent() { + printPreviewMessage(); + printOrders(); + printTotalOrderAmountWithoutDiscount(); + printGiveawayMenu(); + printBenefits(); + printTotalBenefitAmount(); + printExpectedPaymentAmount(); + printEventBadge(); + } + + private void printPreviewMessage() { + outputView.printBenefitPreview(); + } + + private void printOrders() { + outputView.printOrders(eventService.getOrderList()); + } + + private void printTotalOrderAmountWithoutDiscount() { + outputView.printTotalOrderAmountWithoutDiscount(eventService.getTotalOrderAmount()); + } + + private void printGiveawayMenu() { + outputView.printGiveawayMenu(eventService.getGiveaway()); + } + + private void printTotalBenefitAmount() { + outputView.printTotalBenefitAmount(eventService.getTotalBenefitAmount()); + } + + private void printBenefits() { + if (eventService.getBenefits().size() == 0) { + outputView.printBenefits(Collections.emptyMap()); + return; + } + outputView.printBenefits(eventService.getBenefits()); + } + + private void printExpectedPaymentAmount() { + outputView.printExpectedPaymentAmount( + expectedPaymentAmount( + eventService.getTotalOrderAmount(), + eventService.getTotalDiscountAmount() + )); + } + + private int expectedPaymentAmount(int orderAmount, int benefitAmount) { + return orderAmount + benefitAmount; + } + + private void printEventBadge() { + outputView.printEventBadge(eventService.getBadge()); + } +}
Java
ํ”„๋กœ๊ทธ๋žจ์˜ ์ „์ฒด ํ๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฉ”์„œ๋“œ์ธ๋ฐ ์–ด๋–ค ์‹์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋Š”๊ฒŒ ์ข‹์„๊นŒ์š”....? ์ €๋Š” ์ผ๋‹จ ํ”„๋กœ๊ทธ๋žจ์ด ๋Œ์•„๊ฐ€๊ธด ํ•ด์•ผํ•˜๋‹ˆ ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ์—์„  ํ”„๋กœ๊ทธ๋žจ ์ „์ฒด์˜ ํ๋ฆ„์— ๋Œ€ํ•ด ๋‹ค๋ค„์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ฑฐ๋“ ์š”! ์ง€๊ธˆ ๋ณด๋‹ˆ EventService๊ฐ์ฒด์˜ ์ƒ์„ฑ์„ ๋ฉ”์„œ๋“œ๋กœ ๋”ฐ๋กœ ๋นผ๋Š”๊ฒƒ๋„ ์ข‹์•˜์„๊ฑฐ๊ฐ™๋„ค์š”
@@ -0,0 +1,126 @@ +package christmas.controller; + +import christmas.domain.customer.CustomerInfo; +import christmas.domain.customer.VisitDate; +import christmas.domain.order.Orders; +import christmas.service.EventService; +import christmas.service.RestaurantService; +import christmas.view.InputView; +import christmas.view.OutputView; + +import java.util.Collections; + +public class RestaurantController { + private final InputView inputView; + private final OutputView outputView; + private final RestaurantService restaurantService; + private EventService eventService; + + private RestaurantController(InputView inputView, OutputView outputView, RestaurantService restaurantService) { + this.inputView = inputView; + this.outputView = outputView; + this.restaurantService = restaurantService; + } + + public static RestaurantController of(InputView inputView, OutputView outputView, RestaurantService restaurantService) { + return new RestaurantController(inputView, outputView, restaurantService); + } + + public void run() { + inputView.printInitialMessage(); + createCustomerInfo(); + eventService = EventService.of( + getCustomerInfoFromRestaurantService() + ); + applyDiscount(); + printApplyEvent(); + } + + public void createCustomerInfo() { + restaurantService.createCustomerInfo(createVisitDate(), createOrdersFromMenuAndNum()); + } + + private VisitDate createVisitDate() { + while (true) { + try { + return restaurantService.createVisitDate(Integer.parseInt(inputView.readVisitDate())); + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e); + } + } + } + + private Orders createOrdersFromMenuAndNum() { + while (true) { + try { + String inputMenuAndNum = inputView.readMenuAndNum(); + return restaurantService.processCreateOrders(inputMenuAndNum); + } catch (IllegalArgumentException e) { + outputView.printErrorMessage(e); + } + } + } + + private CustomerInfo getCustomerInfoFromRestaurantService() { + return restaurantService.getCustomerInfo(); + } + + public void applyDiscount() { + eventService.applyDiscount(); + } + + public void printApplyEvent() { + printPreviewMessage(); + printOrders(); + printTotalOrderAmountWithoutDiscount(); + printGiveawayMenu(); + printBenefits(); + printTotalBenefitAmount(); + printExpectedPaymentAmount(); + printEventBadge(); + } + + private void printPreviewMessage() { + outputView.printBenefitPreview(); + } + + private void printOrders() { + outputView.printOrders(eventService.getOrderList()); + } + + private void printTotalOrderAmountWithoutDiscount() { + outputView.printTotalOrderAmountWithoutDiscount(eventService.getTotalOrderAmount()); + } + + private void printGiveawayMenu() { + outputView.printGiveawayMenu(eventService.getGiveaway()); + } + + private void printTotalBenefitAmount() { + outputView.printTotalBenefitAmount(eventService.getTotalBenefitAmount()); + } + + private void printBenefits() { + if (eventService.getBenefits().size() == 0) { + outputView.printBenefits(Collections.emptyMap()); + return; + } + outputView.printBenefits(eventService.getBenefits()); + } + + private void printExpectedPaymentAmount() { + outputView.printExpectedPaymentAmount( + expectedPaymentAmount( + eventService.getTotalOrderAmount(), + eventService.getTotalDiscountAmount() + )); + } + + private int expectedPaymentAmount(int orderAmount, int benefitAmount) { + return orderAmount + benefitAmount; + } + + private void printEventBadge() { + outputView.printEventBadge(eventService.getBadge()); + } +}
Java
์ œ ํ”„๋กœ๊ทธ๋žจ ๊ตฌ์กฐ์˜ ๊ฒฝ์šฐ view->controller -> service ์ธ ๊ฒฝ์šฐ๋ผ controller์—์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ๊ธด ํ–ˆ์Šต๋‹ˆ๋‹ค <- <-
@@ -0,0 +1,55 @@ +package christmas.domain.customer; + +import christmas.constant.NumConstant; +import christmas.validator.DateValidator; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +public class VisitDate { + private final int visitDate; + + private VisitDate(int visitDate) { + DateValidator.validateVisitDateBound(visitDate); + this.visitDate = visitDate; + } + + public static VisitDate from(int visitDate) { + return new VisitDate(visitDate); + } + + public DayOfWeek getDayOfWeek() { + LocalDate date = LocalDate.of( + NumConstant.THIS_YEAR.getValue(), + NumConstant.THIS_MONTH.getValue(), + visitDate + ); + return date.getDayOfWeek(); + } + + public boolean isVisitDateWeekend() { + DayOfWeek dayOfWeek = getDayOfWeek(); + if (dayOfWeek == DayOfWeek.FRIDAY || dayOfWeek == DayOfWeek.SATURDAY) { + return true; + } + return false; + } + + public boolean isOverChristmas() { + if(visitDate > NumConstant.CHRISTMAS_DAY.getValue()) { + return true; + } + return false; + } + + public int calculateDdayEventApplyDay() { + return visitDate - 1; + } + + public boolean isSpecialDay() { + if (visitDate == NumConstant.CHRISTMAS_DAY.getValue() || getDayOfWeek() == DayOfWeek.SUNDAY) { + return true; + } + return false; + } +}
Java
Dday์ด๋ฒคํŠธ๊ฐ€ ์ตœ์†Œ 1000์›์œผ๋กœ ์‹œ์ž‘ํ•˜๊ณ  1์ผ ์ดํ›„๋ถ€ํ„ฐ 100์›์”ฉ ๋ถ™๊ฒŒ๋˜๋Š”๋ฐ 100์›์”ฉ ๋ถ™๊ฒŒ๋˜๋Š” ๋‚ ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค! 1์ด ๋ฐฉ๋ฌธ ๋‚ ์งœ์ธ ๊ฒฝ์šฐ 0์„ returnํ•˜๋ฏ€๋กœ 100์› ์ ์šฉ์ด ์•ˆ๋˜๋Š” ๊ฒ๋‹ˆ๋‹ค!
@@ -0,0 +1,68 @@ +package christmas.service; + +import christmas.constant.Badge; +import christmas.domain.customer.CustomerInfo; +import christmas.domain.event.EventStatus; +import christmas.handler.EventHandler; + +import java.util.Collections; +import java.util.Map; + +public class EventService { + private final EventHandler eventHandler; + private final CustomerInfo customerInfo; + + private EventService(CustomerInfo customerInfo) { + eventHandler = EventHandler.from(EventStatus.createInstance()); + this.customerInfo = customerInfo; + } + + public static EventService of(CustomerInfo customerInfo) { + return new EventService(customerInfo); + } + + public void applyDiscount() { + eventHandler.applyDiscount(customerInfo); + } + + public int getTotalBenefitAmount() { + if (customerInfo.isOverMinAmount()) { + return eventHandler.getTotalBenefitAmount(); + } + return 0; + } + + public Map<String, Integer> getBenefits() { + if (customerInfo.isOverMinAmount()) { + return eventHandler.getBenefits(); + } + return Collections.emptyMap(); + } + + public Map<String, Integer> getOrderList() { + return customerInfo.getOrderList(); + } + + public String getGiveaway() { + if (customerInfo.isApplyGiveawayEvent()) { + return "์ƒดํŽ˜์ธ 1๊ฐœ"; + } + return "์—†์Œ"; + } + + public int getTotalOrderAmount() { + return customerInfo.getTotalOrderAmount(); + } + + public int getTotalDiscountAmount() { + if (customerInfo.isOverMinAmount()) { + return eventHandler.getTotalDiscountAmount(); + } + return 0; + } + + public String getBadge() { + return Badge.getBadge(eventHandler.getTotalBenefitAmount()); + } + +}
Java
๋ฆฌํŒฉํ† ๋ง ๋Œ€์ƒ์ด์—ˆ๋Š”๋ฐ ์ •์‹ ์—†์ด ๊ฐœ๋ฐœํ•˜๋‹ค๋ณด๋‹ˆ ๋†“์นœ ๋ถ€๋ถ„์ด๋„ค์š” ์ง€์  ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,31 @@ +package christmas.constant; + +public enum Badge { + MISS("์—†์Œ"), + STAR("๋ณ„"), + TREE("ํŠธ๋ฆฌ"), + SANTA("์‚ฐํƒ€"); + private String name; + + Badge(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public static String getBadge(int totalBenefitAmount) { + totalBenefitAmount = Math.abs(totalBenefitAmount); + if(totalBenefitAmount >= NumConstant.STAR_BADGE_AMOUNT.getValue() && totalBenefitAmount < NumConstant.TREE_BADGE_AMOUNT.getValue()) { + return STAR.name; + } + if(totalBenefitAmount >= NumConstant.TREE_BADGE_AMOUNT.getValue() && totalBenefitAmount < NumConstant.SANTA_BADGE_AMOUNT.getValue()) { + return TREE.name; + } + if(totalBenefitAmount >= NumConstant.SANTA_BADGE_AMOUNT.getValue()) { + return SANTA.name; + } + return MISS.name; + } +}
Java
์˜ค ์ €๋„ ์ด ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ ๋‚˜์„œ ๊น”๋”ํ•˜๋‹ค๊ณ  ๋А๊ผˆ์—ˆ๋Š”๋ฐ.. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!
@@ -0,0 +1,38 @@ +package christmas.domain.order; + +import christmas.constant.MenuType; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Orders { + private final List<Order> orders; + + private Orders(List<Order> orders) { + this.orders = orders; + } + + public static Orders from(List<Order> orders) { + return new Orders(orders); + } + + public int getTotalOrderAmount() { + return orders.stream() + .map(Order::getOrderAmount) + .reduce(0, Integer::sum); + } + + public int getOrderNumByMenuType(MenuType menuType) { + return orders.stream() + .filter(order -> order.isMatchMenuType(menuType)) + .mapToInt(Order::getMenuNum) + .sum(); + } + + public Map<String, Integer> getOrderList() { + return orders.stream() + .collect(Collectors.toMap(Order::getMenuName, Order::getMenuNum)); + } +}
Java
๋ฉ”์„œ๋“œ ๋„ค์ด๋ฐ์—์„œ ์—ฌ๋Ÿฌ ์ฃผ๋ฌธ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ข‹์€ ์ด๋ฆ„์ด ๋ญ๊ฐ€ ์žˆ์„๊นŒ ํ•˜๋‹ค๊ฐ€ ๋‚˜์˜จ๊ฒŒ ์ด๊ฑด๋ฐ ๋„ค์ด๋ฐ ์–ด๋ ต๋„ค์š” ใ… ใ… 
@@ -0,0 +1,48 @@ +package christmas.domain.menu; + +import christmas.constant.MenuType; + +import java.util.*; + +public class Menus { + private final List<Menu> menus; + private static Menus instance; + + private Menus(List<Menu> menus) { + this.menus = menus; + } + + public static Menus getInstance() { + if(instance == null) { + instance = new Menus(initialMenus()); + } + return instance; + } + + private static List<Menu> initialMenus() { + List<Menu> initialMenus = new ArrayList<>(); + + initialMenus.add(Menu.of("์–‘์†ก์ด์ˆ˜ํ”„", 6000, MenuType.APPETIZER)); + initialMenus.add(Menu.of("ํƒ€ํŒŒ์Šค", 5500, MenuType.APPETIZER)); + initialMenus.add(Menu.of("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000, MenuType.APPETIZER)); + initialMenus.add(Menu.of("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, MenuType.MAIN)); + initialMenus.add(Menu.of("๋ฐ”๋น„ํ๋ฆฝ", 54000, MenuType.MAIN)); + initialMenus.add(Menu.of("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, MenuType.MAIN)); + initialMenus.add(Menu.of("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000, MenuType.MAIN)); + initialMenus.add(Menu.of("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, MenuType.DESSERT)); + initialMenus.add(Menu.of("์•„์ด์Šคํฌ๋ฆผ", 5000, MenuType.DESSERT)); + initialMenus.add(Menu.of("์ œ๋กœ์ฝœ๋ผ", 3000, MenuType.DRINK)); + initialMenus.add(Menu.of("๋ ˆ๋“œ์™€์ธ", 60000, MenuType.DRINK)); + initialMenus.add(Menu.of("์ƒดํŽ˜์ธ", 25000, MenuType.DRINK)); + + return initialMenus; + } + + public Optional<Menu> getMenuByMenuName(String menuName) { + return menus.stream() + .filter(menu -> menu.getMenuName().equals(menuName)) + .findFirst(); + + } + +}
Java
Menus๋Š” ์‰ฝ๊ฒŒ๋งํ•ด ๋ฉ”๋‰ดํŒ์œผ๋กœ ์‚ฌ์šฉ๋œ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค! ๋Ÿฐํƒ€์ž„ ์‹œ์ ์— ๋ณ€๊ฒฝ๋ ์ผ์ด ์—†์„๋“ฏํ•˜์—ฌ ์‹ฑ๊ธ€ํ†ค ๋ฐฉ์‹์œผ๋กœ ์„ ์–ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,102 @@ +## ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ”„๋กœ๋ชจ์…˜ + +์ด๋ฒคํŠธ ๊ธฐ๊ฐ„: 2023.12.1 ~ 2023.12.31 + +- ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ + - ์ด๋ฒคํŠธ ๊ธฐ๊ฐ„: 2023.12.1 ~ 2023.12.25 + - 1,000์›์œผ๋กœ ์‹œ์ž‘ํ•˜์—ฌ ํฌ๋ฆฌ์Šค๋งˆ์Šค๊ฐ€ ๋‹ค๊ฐ€์˜ฌ์ˆ˜๋ก ๋‚ ๋งˆ๋‹ค ํ• ์ธ ๊ธˆ์•ก์ด 100์›์”ฉ ์ฆ๊ฐ€ + - ์ด์ฃผ๋ฌธ ๊ธˆ์•ก์—์„œ ํ•ด๋‹น ๊ธˆ์•ก๋งŒํผ ํ• ์ธ + - (e.g. ์‹œ์ž‘์ผ์ธ 12์›” 1์ผ์— 1,000์›, 2์ผ์— 1,100์›, ..., 25์ผ์—” 3,400์› ํ• ์ธ) +- ํ‰์ผ ํ• ์ธ(์ผ์š”์ผ~๋ชฉ์š”์ผ): ํ‰์ผ์—๋Š” ๋””์ €ํŠธ ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์› ํ• ์ธ +- ์ฃผ๋ง ํ• ์ธ(๊ธˆ์š”์ผ, ํ† ์š”์ผ): ์ฃผ๋ง์—๋Š” ๋ฉ”์ธ ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์› ํ• ์ธ +- ํŠน๋ณ„ ํ• ์ธ: ์ด๋ฒคํŠธ ๋‹ฌ๋ ฅ์— ๋ณ„์ด ์žˆ์œผ๋ฉด ์ด์ฃผ๋ฌธ ๊ธˆ์•ก์—์„œ 1,000์› ํ• ์ธ +- ์ฆ์ • ์ด๋ฒคํŠธ: ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก์ด 12๋งŒ ์› ์ด์ƒ์ผ ๋•Œ, ์ƒดํŽ˜์ธ 1๊ฐœ ์ฆ์ • + + +``` +<์• ํ”ผํƒ€์ด์ €> +์–‘์†ก์ด์ˆ˜ํ”„(6,000), ํƒ€ํŒŒ์Šค(5,500), ์‹œ์ €์ƒ๋Ÿฌ๋“œ(8,000) + +<๋ฉ”์ธ> +ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ(55,000), ๋ฐ”๋น„ํ๋ฆฝ(54,000), ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€(35,000), ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€(25,000) + +<๋””์ €ํŠธ> +์ดˆ์ฝ”์ผ€์ดํฌ(15,000), ์•„์ด์Šคํฌ๋ฆผ(5,000) + +<์Œ๋ฃŒ> +์ œ๋กœ์ฝœ๋ผ(3,000), ๋ ˆ๋“œ์™€์ธ(60,000), ์ƒดํŽ˜์ธ(25,000) +``` + + +## ๊ธฐ๋Šฅ ๋ชฉ๋ก +- [x] ๋ฐฉ๋ฌธ ๋‚ ์งœ๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š” ๊ธฐ๋Šฅ `getDate()` + - ์ˆซ์ž๋งŒ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. +- [x] ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š” ๊ธฐ๋Šฅ (์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ํ•  ์ˆ˜ ์—†์Œ, ์ตœ๋Œ€ 20๊ฐœ๊นŒ์ง€ ์ฃผ๋ฌธ) `getOrder()` + - e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1 +- [x] ์ฃผ๋ฌธ ๋ฉ”๋‰ด๋ฅผ ์ถœ๋ ฅ ํ•˜๋Š” ๊ธฐ๋Šฅ `printMenu(List<String> menus)` +- [x] ํ• ์ธ ์ „ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐ ํ•˜๋Š” ๊ธฐ๋Šฅ `calculateTotalOrderAmount()` +- [x] ํ• ์ธ ์ „ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก ์ถœ๋ ฅ ํ•˜๋Š” ๊ธฐ๋Šฅ `printTotalOrderAmount()` +- [x] ํ• ์ธ ์ „ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์ด 120,000์› ์ด์ƒ์‹œ ์ƒดํŽ˜์ธ์„ ์ฆ์ •ํ•˜๋Š” ๊ธฐ๋Šฅ `calculateGiftMenu(int totalOrderAmount, int totalDiscount)` + - 120,000์› ๋ฏธ๋งŒ์‹œ "์—†์Œ" ์ถœ๋ ฅ +- [x] ํฌ๋ฆฌ์Šค ๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ์„ ๊ณ„์‚ฐ ํ•˜๋Š” ๊ธฐ๋Šฅ `ddayDiscount(int date)` + - 1์ผ ~ 25์ผ ํฌ๋ฆฌ์Šค๋งˆ์Šค์™€ ๋‹ค๊ฐ€์˜ฌ์ˆ˜๋ก 1,000์› ์‹œ์ž‘ 100์œˆ์”ฉ ์ฆ๊ฐ€ +- [x] ํ‰์ผ ํ• ์ธ์„ ๊ณ„์‚ฐ ํ•˜๋Š” ๊ธฐ๋Šฅ (์ฃผ๋ง ์ œ์™ธ) `weekDayDiscount(int date, List<String> menus)` + - ๋””์ €ํŠธ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์› ํ• ์ธ +- [x] ์ฃผ๋ง ํ• ์ธ์„ ๊ณ„์‚ฐ ํ•˜๋Š” ๊ธฐ๋Šฅ (1,2,8,9,15,16,22,23,29,30) `weekendDayDiscount(int date, List<String> menus)` + - ๋ฉ”์ธ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์› ํ• ์ธ +- [x] ํŠน๋ณ„ ํ• ์ธ์ด ๊ฐ€๋Šฅํ•œ ๋‚  ์ธ์ง€ ํ™•์ธ ํ•˜๋Š” ๊ธฐ๋Šฅ (3,10,17,24,25,31) `starDayDiscount(int date)` + - 1,000์› ํ• ์ธ +- [x] ํ˜œํƒ ๋‚ด์—ญ์„ ์ถœ๋ ฅ ํ•˜๋Š” ๊ธฐ๋Šฅ `printDiscountAmount(int date, List<String> menus, int totalOrderAmount)` + - ๋ฐ›์€ ํ˜œํƒ์ด ์—†๋‹ค๋ฉด "์—†์Œ" ์ถœ๋ ฅ +- [x] ์ด ํ˜œํƒ ๊ธˆ์•ก์„ ๊ณ„์‚ฐ ํ•˜๋Š” ๊ธฐ๋Šฅ (ํ• ์ธ ๊ธˆ์•ก์˜ ํ•ฉ๊ณ„ + ์ฆ์ • ์ƒดํŽ˜์ธ์˜ ๊ฐ€๊ฒฉ) `calculateTotalDiscount(int date)` + - ํ˜œํƒ ๊ธˆ์•ก์ด ์—†์„ ์‹œ "0์›" ์ถœ๋ ฅ +- [x] ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก ๊ณ„์‚ฐ ํ•˜๋Š” ๊ธฐ๋Šฅ(ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก - ํ• ์ธ ๊ธˆ์•ก) `calculatePayment(int totalOrderAmount, int totalDiscount)` +- [x] ์ด ํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ผ ์ด๋ฒคํŠธ ๋ฐฐ์ง€๋ฅผ ์ถœ๋ ฅ ํ•˜๋Š” ๊ธฐ๋Šฅ `calculateBadge(int totalBenefits)` + - 5์ฒœ ์› ๋ฏธ๋งŒ: ์—†์Œ + - 5์ฒœ ์› ์ด์ƒ: ๋ณ„ + - 1๋งŒ ์› ์ด์ƒ: ํŠธ๋ฆฌ + - 2๋งŒ ์› ์ด์ƒ: ์‚ฐํƒ€ + + +## ํŒŒ์ผ ๋ชฉ๋ก +#### `EventPlanner.java` : ์ด๋ฒคํŠธ๋ฅผ ๊ณ„ํšํ•˜๊ณ  ์‹คํ–‰ํ•˜๋Š” ํด๋ž˜์Šค +#### `EventProcessor.java` : ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค +#### `NoEventProcessor.java` : ์ด๋ฒคํŠธ๊ฐ€ ์—†์„ ๋•Œ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค +#### `Badge.java` : ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ์—ด๊ฑฐํ˜• ํด๋ž˜์Šค +#### `DateDiscount.java` : ๋‚ ์งœ์— ๋”ฐ๋ฅธ ํ• ์ธ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค +#### `Menu.java` : ์Œ์‹ ๋ฉ”๋‰ด ์—ด๊ฑฐํ˜• ํด๋ž˜์Šค +#### `Order.java` : ์ฃผ๋ฌธ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค +#### `OrderItem.java` : ์ฃผ๋ฌธ ํ•ญ๋ชฉ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค +#### `StarDiscount.java` : ๋ณ„์ด ํ‘œ์‹œ๋œ ๋‚ ์— ๋Œ€ํ•œ ํ• ์ธ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค +#### `TotalDiscountCalculator.java` : ์ด ํ• ์ธ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค +#### `WeekendWeekdayDiscount.java` : ์ฃผ๋ง๊ณผ ํ‰์ผ์— ๋Œ€ํ•œ ํ• ์ธ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค +#### `ErrorMessage.java` : ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์—ด๊ฑฐํ˜• ํด๋ž˜์Šค +#### `OrderValidator.java` : ์ฃผ๋ฌธ์˜ ์œ ํšจ์„ฑ์„ ๊ฒ€์ฆํ•˜๋Š” ํด๋ž˜์Šค +#### `DiscountOutputPrinter.java` : ํ• ์ธ ๋‚ด์—ญ์„ ์ถœ๋ ฅํ•˜๋Š” ํด๋ž˜์Šค +#### `InputView.java` : ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ฐ›๋Š” ํด๋ž˜์Šค +#### `OrderOutputPrinter.java` : ์ฃผ๋ฌธ ๋‚ด์—ญ์„ ์ถœ๋ ฅํ•˜๋Š” ํด๋ž˜์Šค +#### `OutputView.java` : ์ถœ๋ ฅ ๊ด€๋ จ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค +#### `Application.java` : ํ”„๋กœ๊ทธ๋žจ์˜ ์‹œ์ž‘์ ์ด ๋˜๋Š” ํด๋ž˜์Šค + + +## ํ”„๋กœ๊ทธ๋žจ ์‚ฌ์šฉ๋ฒ• +1. Application ํด๋ž˜์Šค๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. +2. ํ”„๋กœ๊ทธ๋žจ์€ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐฉ๋ฌธ ๋‚ ์งœ์™€ ์ฃผ๋ฌธ์„ ์ž…๋ ฅํ•˜๋„๋ก ์•ˆ๋‚ดํ•ฉ๋‹ˆ๋‹ค. +3. ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์ˆซ์ž๋กœ ์ž…๋ ฅํ•˜๋ฉฐ, ์œ ํšจํ•œ ๋‚ ์งœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค (1 ~ 31). +4. ์ฃผ๋ฌธ์€ ๋ฉ”๋‰ด์™€ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•˜๋Š” ํ˜•์‹์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค (e.g., ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1). +5. ํ”„๋กœ๊ทธ๋žจ์€ ์ž…๋ ฅ๋œ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฒคํŠธ๋ฅผ ๊ณ„ํšํ•˜๊ณ  ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. +6. ํ• ์ธ ๋‚ด์—ญ, ์ฃผ๋ฌธ ๋ฉ”๋‰ด, ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก, ์ด ํ˜œํƒ ๊ธˆ์•ก, ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก, ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ๋“ฑ์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. + + +## ๊ธฐ๋Šฅ ์š”๊ตฌ์‚ฌํ•ญ +- ์ด๋ฒคํŠธ ๊ธฐ๊ฐ„: 2023.12.1 ~ 2023.12.31 +- ์ด์ฃผ๋ฌธ ๊ธˆ์•ก 10,000 ์ด์ƒ๋ถ€ํ„ฐ ์ด๋ฒคํŠธ ์ ์šฉ +- ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ์‹œ, ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†๋‹ค. +- ๋ฉ”๋‰ด๋Š” ํ•œ๋ฒˆ์— ์ตœ๋Œ€ 20๊ฐœ๊นŒ์ง€๋งŒ ์ฃผ๋ฌธ ํ•  ์ˆ˜ ์žˆ๋‹ค. +- ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ : 1,000์›์œผ๋กœ ์‹œ์ž‘ํ•˜์—ฌ ํฌ๋ฆฌ์Šค๋งˆ์Šค๊ฐ€ ๋‹ค๊ฐ€์˜ฌ์ˆ˜๋ก ๋‚ ๋งˆ๋‹ค ํ• ์ธ ๊ธˆ์•ก์ด 100์›์”ฉ ์ฆ๊ฐ€ (2023.12.1 ~ 2023.12.31) +- ํ‰์ผ ํ• ์ธ(์ผ์š”์ผ~๋ชฉ์š”์ผ): ํ‰์ผ์—๋Š” ๋””์ €ํŠธ ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์› ํ• ์ธ +- ์ฃผ๋ง ํ• ์ธ(๊ธˆ์š”์ผ, ํ† ์š”์ผ): ์ฃผ๋ง์—๋Š” ๋ฉ”์ธ ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์› ํ• ์ธ +- ํŠน๋ณ„ ํ• ์ธ: ์ด๋ฒคํŠธ ๋‹ฌ๋ ฅ์— ๋ณ„์ด ์žˆ์œผ๋ฉด ์ด์ฃผ๋ฌธ ๊ธˆ์•ก์—์„œ 1,000์› ํ• ์ธ +- ์ฆ์ • ์ด๋ฒคํŠธ: ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก์ด 12๋งŒ ์› ์ด์ƒ์ผ ๋•Œ, ์ƒดํŽ˜์ธ 1๊ฐœ ์ฆ์ • (25,000) +- ์ด์ฃผ๋ฌธ ๊ธˆ์•ก์—์„œ ํ•ด๋‹น ๊ธˆ์•ก๋งŒํผ ํ• ์ธ +- ์ดํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ด๋ฒคํŠธ ๋ฐฐ์ง€๋ฅผ ๋ถ€์—ฌ (๋ณ„, ํŠธ๋ฆฌ, ์‚ฐํƒ€)
Unknown
๊ธฐ๋Šฅ ๋ช…์„ธ ๊ผผ๊ผผํ•˜๊ฒŒ ์ž‘์„ฑํ•ด์ฃผ์…จ๋„ค์š” ๐Ÿ‘๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,32 @@ +package christmas.controller; + +import christmas.model.Order; +import christmas.view.OrderOutputPrinter; +import christmas.view.OutputView; + +public class EventPlanner { + private static final int MIN_TOTAL_ORDER_AMOUNT_FOR_EVENTS = 10000; + + public static void planEvent(Order order, int date) { + int totalOrderAmount = order.calculateTotalOrderAmount(); + + printOrderDetails(order, totalOrderAmount); + + processEventBasedOnTotalAmount(order, date, totalOrderAmount); + } + + private static void printOrderDetails(Order order, int totalOrderAmount) { + OrderOutputPrinter.printMenu(order.getMenus()); + OutputView.printTotalOrderAmount(totalOrderAmount); + } + + private static void processEventBasedOnTotalAmount(Order order, int date, int totalOrderAmount) { + if (totalOrderAmount >= MIN_TOTAL_ORDER_AMOUNT_FOR_EVENTS) { + EventProcessor.processEvent(order, date, totalOrderAmount); + } + + if (totalOrderAmount < MIN_TOTAL_ORDER_AMOUNT_FOR_EVENTS) { + NoEventProcessor.processNoEvent(order, date, totalOrderAmount); + } + } +}
Java
ํ•ด๋‹น ๋ฉ”์„œ๋“œ ๋ฐ EventProcessor๋“ค์€ controller๊ฐ€ ์•„๋‹Œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— ์žˆ์–ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. controller์˜ ์—ญํ• ์€ view์˜ ๊ฐ’์„ model์—๊ฒŒ ๊ฑด๋„ค์ฃผ๊ฑฐ๋‚˜, model์˜ ๊ฐ’์„ view์—๊ฒŒ ๊ฑด๋„ค์ฃผ๋Š” ์—ญํ• ๋กœ ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,43 @@ +package christmas.controller; + +import christmas.model.Order; +import christmas.model.OrderItem; +import christmas.model.TotalDiscountCalculator; +import christmas.view.DiscountOutputPrinter; +import christmas.view.OrderOutputPrinter; +import christmas.view.OutputView; +import java.util.Set; + +public class EventProcessor { + public static void processEvent(Order order, int date, int totalOrderAmount) { + int totalDiscount = calculateTotalDiscount(order, date); + int totalBenefits = TotalDiscountCalculator.calculateGiftMenu(totalOrderAmount, totalDiscount); + int totalPayment = TotalDiscountCalculator.calculatePayment(totalOrderAmount, totalDiscount); + String giftMenu = OrderOutputPrinter.printGiftMenuMessage(totalOrderAmount); + + OutputView.printGiftMenuPhrase(giftMenu); + printDiscountAmount(date, order.getMenus(), totalOrderAmount); + printBenefits(totalBenefits); + printPayment(totalPayment); + + String badge = TotalDiscountCalculator.calculateBadge(totalOrderAmount, totalDiscount); + OutputView.printBadge(badge); + } + + public static int calculateTotalDiscount(Order order, int date) { + return order.calculateTotalDiscount(date); + } + + public static void printDiscountAmount(int date, Set<OrderItem> menus, int totalOrderAmount) { + DiscountOutputPrinter.printDiscountAmount(date, menus, totalOrderAmount); + } + + public static void printBenefits(int totalBenefits) { + DiscountOutputPrinter.printBenefits(totalBenefits); + } + + public static void printPayment(int totalPayment) { + OutputView.printPayment(totalPayment); + } + +}
Java
ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋“ค ์ •๋ง ๋ถ„๋ฆฌํ•  ํ•„์š”๊ฐ€ ์žˆ์—ˆ์„๊นŒ์š”?
@@ -0,0 +1,19 @@ +package christmas.controller; + +import christmas.model.Badge; +import christmas.model.Order; +import christmas.view.OutputView; + +public class NoEventProcessor { + public static void processNoEvent(Order order, int date, int totalOrderAmount) { + int totalDiscount = 0; + int totalBenefits = 0; + int totalPayment = totalOrderAmount - totalDiscount; + + EventProcessor.printDiscountAmount(date, order.getMenus(), totalDiscount); + EventProcessor.printBenefits(totalBenefits); + EventProcessor.printPayment(totalPayment); + + OutputView.printBadge(Badge.NONE.getName()); + } +}
Java
ํ•ด๋‹น ๊ฐ’ ์—ญ์‹œ model์— ์˜ํ•ด ๊ฒฐ์ •๋˜์–ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋ฐ”๊พผ๋‹ค๋ฉด Processor ๊ฐ์ฒด๋“ค์ด ํ•„์š”๊ฐ€ ์—†์–ด์ง€๊ฒ ๋„ค์š”
@@ -0,0 +1,53 @@ +package christmas.model; + +import java.util.Arrays; + +public enum Menu { + ์–‘์†ก์ด์ˆ˜ํ”„(6_000), + ํƒ€ํŒŒ์Šค(5_500), + ์‹œ์ €์ƒ๋Ÿฌ๋“œ(8_000), + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ(55_000), + ๋ฐ”๋น„ํ๋ฆฝ(54_000), + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€(35_000), + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€(25_000), + ์ดˆ์ฝ”์ผ€์ดํฌ(15_000), + ์•„์ด์Šคํฌ๋ฆผ(5_000), + ์ œ๋กœ์ฝœ๋ผ(3_000), + ๋ ˆ๋“œ์™€์ธ(60_000), + ์ƒดํŽ˜์ธ(25_000); + + private final int price; + + Menu(int price) { + this.price = price; + } + + public static int getChampagnePrice() { + return ์ƒดํŽ˜์ธ.getPrice(); + } + + public static int getPriceByMenuName(String menuName) { + return Menu.valueOf(menuName).getPrice(); + } + + public static boolean containsMenu(String menuName) { + return Arrays.stream(values()).anyMatch(menu -> menu.name().equals(menuName)); + } + + public int getPrice() { + return price; + } + + public boolean containsBeverageMenu(String menuName) { + return Arrays.asList(์ œ๋กœ์ฝœ๋ผ.name(), ๋ ˆ๋“œ์™€์ธ.name(), ์ƒดํŽ˜์ธ.name()).contains(menuName); + } + + public boolean containsMainMenu(String menuName) { + return Arrays.asList(ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ.name(), ๋ฐ”๋น„ํ๋ฆฝ.name(), ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€.name(), ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€.name()).contains(menuName); + } + + public boolean containsDessertMenu(String menuName) { + return Arrays.asList(์ดˆ์ฝ”์ผ€์ดํฌ.name(), ์•„์ด์Šคํฌ๋ฆผ.name()).contains(menuName); + } + +}
Java
[๊ฐ์ฒด์ง€ํ–ฅ์ƒํ™œ์ฒด์กฐ ์›์น™4. ํ•œ ์ค„์— ์ ์„ ํ•˜๋‚˜๋งŒ ์ฐ๋Š”๋‹ค.](https://hudi.blog/thoughtworks-anthology-object-calisthenics/)์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ๊นŒ์š”?
@@ -0,0 +1,57 @@ +package christmas.model; + +import christmas.view.OutputView; +import java.util.HashSet; +import java.util.Set; + +public class Order { + private static final int MENU_NAME_INDEX = 0; + private static final int QUANTITY_INDEX = 1; + + private final Set<OrderItem> menus; + + + private Order(Set<OrderItem> menus) { + this.menus = menus; + } + + public static Order createOrder(int date, String orderInput) { + Set<OrderItem> menus = createOrderItems(orderInput); + OutputView.printOrderPreview(date); + return new Order(menus); + } + + private static Set<OrderItem> createOrderItems(String orderInput) { + Set<OrderItem> menus = new HashSet<>(); + String[] orderItems = orderInput.split(","); + for (String orderItem : orderItems) { + String[] menuInfo = getMenuInfo(orderItem.trim()); + String menuName = menuInfo[MENU_NAME_INDEX]; + int quantity = Integer.parseInt(menuInfo[QUANTITY_INDEX]); + menus.add(new OrderItem(menuName, quantity)); + } + return menus; + } + + private static String[] getMenuInfo(String menu) { + return menu.split("-"); + } + + public Set<OrderItem> getMenus() { + return menus; + } + + public int calculateTotalOrderAmount() { + int totalOrderAmount = 0; + for (OrderItem orderItem : menus) { + totalOrderAmount += orderItem.calculateMenuAmount(); + } + return totalOrderAmount; + } + + public int calculateTotalDiscount(int date) { + int dateDiscount = TotalDiscountCalculator.calculateDateDiscount(date, menus); + int starDiscount = TotalDiscountCalculator.calculateStarDiscount(date); + return dateDiscount + starDiscount; + } +}
Java
MVC ํŒจํ„ด์€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง(model)๊ณผ ๋ทฐ ๋กœ์ง์„ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. model ๋‚ด๋ถ€์—์„œ view๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค๋ฉด mvc ํŒจํ„ด์„ ์ž˜๋ชป ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์‹ ํ˜ธ์ž…๋‹ˆ๋‹ค
@@ -0,0 +1,57 @@ +package christmas.model; + +import christmas.view.OutputView; +import java.util.HashSet; +import java.util.Set; + +public class Order { + private static final int MENU_NAME_INDEX = 0; + private static final int QUANTITY_INDEX = 1; + + private final Set<OrderItem> menus; + + + private Order(Set<OrderItem> menus) { + this.menus = menus; + } + + public static Order createOrder(int date, String orderInput) { + Set<OrderItem> menus = createOrderItems(orderInput); + OutputView.printOrderPreview(date); + return new Order(menus); + } + + private static Set<OrderItem> createOrderItems(String orderInput) { + Set<OrderItem> menus = new HashSet<>(); + String[] orderItems = orderInput.split(","); + for (String orderItem : orderItems) { + String[] menuInfo = getMenuInfo(orderItem.trim()); + String menuName = menuInfo[MENU_NAME_INDEX]; + int quantity = Integer.parseInt(menuInfo[QUANTITY_INDEX]); + menus.add(new OrderItem(menuName, quantity)); + } + return menus; + } + + private static String[] getMenuInfo(String menu) { + return menu.split("-"); + } + + public Set<OrderItem> getMenus() { + return menus; + } + + public int calculateTotalOrderAmount() { + int totalOrderAmount = 0; + for (OrderItem orderItem : menus) { + totalOrderAmount += orderItem.calculateMenuAmount(); + } + return totalOrderAmount; + } + + public int calculateTotalDiscount(int date) { + int dateDiscount = TotalDiscountCalculator.calculateDateDiscount(date, menus); + int starDiscount = TotalDiscountCalculator.calculateStarDiscount(date); + return dateDiscount + starDiscount; + } +}
Java
๋งค์ง๋„˜๋ฒ„ ์ƒ์ˆ˜ํ™” ๐Ÿ‘๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,57 @@ +package christmas.model; + +import christmas.view.OutputView; +import java.util.HashSet; +import java.util.Set; + +public class Order { + private static final int MENU_NAME_INDEX = 0; + private static final int QUANTITY_INDEX = 1; + + private final Set<OrderItem> menus; + + + private Order(Set<OrderItem> menus) { + this.menus = menus; + } + + public static Order createOrder(int date, String orderInput) { + Set<OrderItem> menus = createOrderItems(orderInput); + OutputView.printOrderPreview(date); + return new Order(menus); + } + + private static Set<OrderItem> createOrderItems(String orderInput) { + Set<OrderItem> menus = new HashSet<>(); + String[] orderItems = orderInput.split(","); + for (String orderItem : orderItems) { + String[] menuInfo = getMenuInfo(orderItem.trim()); + String menuName = menuInfo[MENU_NAME_INDEX]; + int quantity = Integer.parseInt(menuInfo[QUANTITY_INDEX]); + menus.add(new OrderItem(menuName, quantity)); + } + return menus; + } + + private static String[] getMenuInfo(String menu) { + return menu.split("-"); + } + + public Set<OrderItem> getMenus() { + return menus; + } + + public int calculateTotalOrderAmount() { + int totalOrderAmount = 0; + for (OrderItem orderItem : menus) { + totalOrderAmount += orderItem.calculateMenuAmount(); + } + return totalOrderAmount; + } + + public int calculateTotalDiscount(int date) { + int dateDiscount = TotalDiscountCalculator.calculateDateDiscount(date, menus); + int starDiscount = TotalDiscountCalculator.calculateStarDiscount(date); + return dateDiscount + starDiscount; + } +}
Java
`"-"` ํ•ด๋‹น ๊ฐ’๋„ ์ƒ์ˆ˜ํ™” ํ•ด์ฃผ๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,23 @@ +package christmas.model; + +public class OrderItem { + private final String menuName; + private final int quantity; + + public OrderItem(String menuName, int quantity) { + this.menuName = menuName; + this.quantity = quantity; + } + + public String getMenuName() { + return menuName; + } + + public int getQuantity() { + return quantity; + } + + public int calculateMenuAmount() { + return quantity * Menu.getPriceByMenuName(menuName); + } +}
Java
๋ณดํ†ต getter ๋ฉ”์„œ๋“œ๋Š” ์ž๋ฐ” ์ปจ๋ฒค์…˜์ƒ ๊ฐ€์žฅ ์•„๋ž˜์ชฝ์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,48 @@ +package christmas.model; + +import java.util.Set; + +public class TotalDiscountCalculator { + + private static final int GIFT_MENU_ORDER_AMOUNT_THRESHOLD = 120000; + private static final int DEFAULT_DISCOUNT = 0; + + public static int calculateDateDiscount(int date, Set<OrderItem> menus) { + int dateDiscount = DateDiscount.ddayDiscount(date); + int weekDayDiscount = WeekendWeekdayDiscount.weekDayDiscount(date, menus); + int weekendDayDiscount = WeekendWeekdayDiscount.weekendDayDiscount(date, menus); + return dateDiscount + weekDayDiscount + weekendDayDiscount; + } + + public static int calculateStarDiscount(int date) { + return StarDiscount.starDayDiscount(date); + } + + public static int calculateGiftMenu(int totalOrderAmount, int totalDiscount) { + int totalBenefits = totalDiscount; + if (totalOrderAmount >= GIFT_MENU_ORDER_AMOUNT_THRESHOLD) { + totalBenefits += getGiftMenuPrice(); + } + return totalBenefits; + } + + public static int printGiftMenuPrice(int totalOrderAmount) { + if (totalOrderAmount < GIFT_MENU_ORDER_AMOUNT_THRESHOLD) { + return DEFAULT_DISCOUNT; + } + return getGiftMenuPrice(); + } + + public static String calculateBadge(int totalOrderAmount, int calculateTotalDiscount) { + int totalBenefits = calculateGiftMenu(totalOrderAmount, calculateTotalDiscount); + return Badge.calculateBadge(totalBenefits); + } + + public static int calculatePayment(int totalOrderAmount, int totalDiscount) { + return totalOrderAmount - totalDiscount; + } + + private static int getGiftMenuPrice() { + return Menu.getChampagnePrice(); + } +}
Java
Discount ๊ด€๋ จ ๊ฐ์ฒด๋“ค์ด ๋ชจ๋‘ ์œ ํ‹ธ์„ฑ ํด๋ž˜์Šค๋„ค์š”. ์ง€๋‚˜์น˜๊ฒŒ ๋‚จ์šฉ๋œ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค. ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์“ฐ๋ฉด static ๋ฉ”์„œ๋“œ ์—†์ด ์ข€ ๋” ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ• ๊นŒ์š”?
@@ -0,0 +1,139 @@ +package christmas.validator; + +import christmas.model.Menu; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.function.Predicate; + +public class OrderValidator { + + private static final int MAX_MENU_COUNT = 20; + private static final String MENU_REGEX = "^[๊ฐ€-ํžฃ]+-[1-9]\\d*$"; + private static final String ENGLISH_REGEX = ".*[a-zA-Z]+.*"; + + public static String validateAndGetOrder(String order) { + validateOrder(order); + return order; + } + + private static void validateOrder(String order) { + validateOrderCommaFormat(order); + validateInvalidMenuInOrder(order); + validateOrderFormat(order); + validateNoEnglishCharacters(order); + validateNoBeverageOnlyOrder(order); + validateMenusExist(order); + } + + private static void validateNoBeverageOnlyOrder(String order) { + if (containsBeverageOnly(order)) { + throw new IllegalArgumentException(ErrorMessage.BEVERAGE_ONLY_ORDER.getMessage()); + } + } + + private static void validateInvalidMenuInOrder(String order) { + if (isInvalidMenuInOrder(order)) { + throw new IllegalArgumentException(ErrorMessage.INVALID_ORDER.getMessage()); + } + } + + private static boolean isInvalidMenuInOrder(String order) { + return allMatch(order, menuInfo -> { + String menuName = menuInfo.split("-")[0].trim(); + return !Menu.containsMenu(menuName); + }); + } + + private static boolean containsBeverageOnly(String order) { + return allMatch(order, menuInfo -> isBeverageMenu(menuInfo.split("-")[0].trim())); + } + + private static boolean isBeverageMenu(String menuName) { + return Menu.valueOf(menuName).containsBeverageMenu(menuName); + } + + private static void validateOrderFormat(String order) { + if (!allMatch(order, OrderValidator::isValidMenuEntryFormat)) { + throw new IllegalArgumentException(ErrorMessage.INVALID_ORDER.getMessage()); + } + } + + private static void validateOrderCommaFormat(String order) { + if (order.startsWith(",") || order.endsWith(",")) { + throw new IllegalArgumentException(ErrorMessage.INVALID_ORDER.getMessage()); + } + } + + private static boolean allMatch(String order, Predicate<String> predicate) { + return Arrays.stream(order.split(",")).allMatch(predicate); + } + + private static boolean isValidMenuEntryFormat(String menuEntry) { + return menuEntry.matches(MENU_REGEX); + } + + private static void validateNoEnglishCharacters(String order) { + if (containsEnglish(order)) { + throw new IllegalArgumentException(ErrorMessage.INVALID_ORDER.getMessage()); + } + } + + private static boolean containsEnglish(String order) { + return order.matches(ENGLISH_REGEX); + } + + private static void validateMenusExist(String order) { + Set<String> uniqueMenus = checkForDuplicateMenus(order); + validateTotalMenuCount(order, uniqueMenus.size()); + } + + private static Set<String> checkForDuplicateMenus(String order) { + Set<String> uniqueMenus = new HashSet<>(); + + allMatch(order, menuInfo -> { + String menuName = menuInfo.split("-")[0].trim(); + if (!uniqueMenus.add(menuName)) { + throw new IllegalArgumentException(ErrorMessage.INVALID_ORDER.getMessage()); + } + return true; + }); + + return uniqueMenus; + } + + private static void validateTotalMenuCount(String order, int uniqueMenuCount) { + int totalMenuCount = Arrays.stream(order.split(",")) + .mapToInt(OrderValidator::validateAndGetQuantity) + .sum(); + + if (totalMenuCount > MAX_MENU_COUNT || uniqueMenuCount > MAX_MENU_COUNT) { + throw new IllegalArgumentException(ErrorMessage.MAX_MENU_EXCEEDED.getMessage()); + } + } + + private static int validateAndGetQuantity(String menuInfo) { + String[] menuDetails = menuInfo.split("-"); + validateMenu(menuDetails); + return Integer.parseInt(menuDetails[1].trim()); + } + + private static void validateMenu(String[] menuDetails) { + String menuName = menuDetails[0].trim(); + if (!isValidMenu(menuName)) { + throw new IllegalArgumentException(ErrorMessage.INVALID_ORDER.getMessage()); + } + validateQuantity(menuDetails[1].trim()); + } + + private static boolean isValidMenu(String menuName) { + return Arrays.stream(Menu.values()).anyMatch(menu -> menu.name().equals(menuName)); + } + + private static void validateQuantity(String quantityStr) { + int quantity = Integer.parseInt(quantityStr); + if (quantity < 1) { + throw new IllegalArgumentException(ErrorMessage.INVALID_ORDER.getMessage()); + } + } +}
Java
Validator๋“ค ์•ˆ์— View์— ์žˆ์–ด์•ผํ•  ๋กœ์ง๋“ค๊ณผ model์— ์žˆ์–ด์•ผํ•  ๋กœ์ง๋“ค์ด ํ˜ผ์žฌ๋˜์–ด ์žˆ๋„ค์š”. View์—์„œ ์‚ฌ์šฉํ•  Validator์™€ model์— ์‚ฌ์šฉํ•  Validator๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ฐ๊ฐ์˜ ํŒจํ‚ค์ง€์— ์œ„์น˜์‹œ์ผœ๋ณผ๊นŒ์š”?
@@ -0,0 +1,28 @@ +package christmas.view; + +import christmas.model.OrderItem; +import java.util.Set; + +public class OrderOutputPrinter { + private static final String GIFT_MENU_MESSAGE = "์ƒดํŽ˜์ธ 1๊ฐœ"; + private static final String NONE_BENEFIT_MESSAGE = "์—†์Œ"; + + public static void printMenu(Set<OrderItem> menus) { + OutputView.printMessage("<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"); + menus.forEach(OrderOutputPrinter::printMenuItem); + OutputView.printEmptyLine(); + } + + private static void printMenuItem(OrderItem orderItem) { + String menuName = orderItem.getMenuName(); + int quantity = orderItem.getQuantity(); + OutputView.printMessage(menuName + " " + quantity + "๊ฐœ"); + } + + public static String printGiftMenuMessage(int totalOrderAmount) { + if (totalOrderAmount >= 120000) { + return GIFT_MENU_MESSAGE; + } + return NONE_BENEFIT_MESSAGE; + } +}
Java
์ฃผ๋ฌธ๊ธˆ์•ก์ด 120000์„ ๋„˜์œผ๋ฉด ์ƒดํŽ˜์ธ์„ ์ฆ์ •ํ•œ๋‹ค๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์•„๋‹๊นŒ์š”?
@@ -0,0 +1,102 @@ +## ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ”„๋กœ๋ชจ์…˜ + +์ด๋ฒคํŠธ ๊ธฐ๊ฐ„: 2023.12.1 ~ 2023.12.31 + +- ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ + - ์ด๋ฒคํŠธ ๊ธฐ๊ฐ„: 2023.12.1 ~ 2023.12.25 + - 1,000์›์œผ๋กœ ์‹œ์ž‘ํ•˜์—ฌ ํฌ๋ฆฌ์Šค๋งˆ์Šค๊ฐ€ ๋‹ค๊ฐ€์˜ฌ์ˆ˜๋ก ๋‚ ๋งˆ๋‹ค ํ• ์ธ ๊ธˆ์•ก์ด 100์›์”ฉ ์ฆ๊ฐ€ + - ์ด์ฃผ๋ฌธ ๊ธˆ์•ก์—์„œ ํ•ด๋‹น ๊ธˆ์•ก๋งŒํผ ํ• ์ธ + - (e.g. ์‹œ์ž‘์ผ์ธ 12์›” 1์ผ์— 1,000์›, 2์ผ์— 1,100์›, ..., 25์ผ์—” 3,400์› ํ• ์ธ) +- ํ‰์ผ ํ• ์ธ(์ผ์š”์ผ~๋ชฉ์š”์ผ): ํ‰์ผ์—๋Š” ๋””์ €ํŠธ ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์› ํ• ์ธ +- ์ฃผ๋ง ํ• ์ธ(๊ธˆ์š”์ผ, ํ† ์š”์ผ): ์ฃผ๋ง์—๋Š” ๋ฉ”์ธ ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์› ํ• ์ธ +- ํŠน๋ณ„ ํ• ์ธ: ์ด๋ฒคํŠธ ๋‹ฌ๋ ฅ์— ๋ณ„์ด ์žˆ์œผ๋ฉด ์ด์ฃผ๋ฌธ ๊ธˆ์•ก์—์„œ 1,000์› ํ• ์ธ +- ์ฆ์ • ์ด๋ฒคํŠธ: ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก์ด 12๋งŒ ์› ์ด์ƒ์ผ ๋•Œ, ์ƒดํŽ˜์ธ 1๊ฐœ ์ฆ์ • + + +``` +<์• ํ”ผํƒ€์ด์ €> +์–‘์†ก์ด์ˆ˜ํ”„(6,000), ํƒ€ํŒŒ์Šค(5,500), ์‹œ์ €์ƒ๋Ÿฌ๋“œ(8,000) + +<๋ฉ”์ธ> +ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ(55,000), ๋ฐ”๋น„ํ๋ฆฝ(54,000), ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€(35,000), ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€(25,000) + +<๋””์ €ํŠธ> +์ดˆ์ฝ”์ผ€์ดํฌ(15,000), ์•„์ด์Šคํฌ๋ฆผ(5,000) + +<์Œ๋ฃŒ> +์ œ๋กœ์ฝœ๋ผ(3,000), ๋ ˆ๋“œ์™€์ธ(60,000), ์ƒดํŽ˜์ธ(25,000) +``` + + +## ๊ธฐ๋Šฅ ๋ชฉ๋ก +- [x] ๋ฐฉ๋ฌธ ๋‚ ์งœ๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š” ๊ธฐ๋Šฅ `getDate()` + - ์ˆซ์ž๋งŒ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. +- [x] ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š” ๊ธฐ๋Šฅ (์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ ํ•  ์ˆ˜ ์—†์Œ, ์ตœ๋Œ€ 20๊ฐœ๊นŒ์ง€ ์ฃผ๋ฌธ) `getOrder()` + - e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1 +- [x] ์ฃผ๋ฌธ ๋ฉ”๋‰ด๋ฅผ ์ถœ๋ ฅ ํ•˜๋Š” ๊ธฐ๋Šฅ `printMenu(List<String> menus)` +- [x] ํ• ์ธ ์ „ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์„ ๊ณ„์‚ฐ ํ•˜๋Š” ๊ธฐ๋Šฅ `calculateTotalOrderAmount()` +- [x] ํ• ์ธ ์ „ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก ์ถœ๋ ฅ ํ•˜๋Š” ๊ธฐ๋Šฅ `printTotalOrderAmount()` +- [x] ํ• ์ธ ์ „ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์ด 120,000์› ์ด์ƒ์‹œ ์ƒดํŽ˜์ธ์„ ์ฆ์ •ํ•˜๋Š” ๊ธฐ๋Šฅ `calculateGiftMenu(int totalOrderAmount, int totalDiscount)` + - 120,000์› ๋ฏธ๋งŒ์‹œ "์—†์Œ" ์ถœ๋ ฅ +- [x] ํฌ๋ฆฌ์Šค ๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ์„ ๊ณ„์‚ฐ ํ•˜๋Š” ๊ธฐ๋Šฅ `ddayDiscount(int date)` + - 1์ผ ~ 25์ผ ํฌ๋ฆฌ์Šค๋งˆ์Šค์™€ ๋‹ค๊ฐ€์˜ฌ์ˆ˜๋ก 1,000์› ์‹œ์ž‘ 100์œˆ์”ฉ ์ฆ๊ฐ€ +- [x] ํ‰์ผ ํ• ์ธ์„ ๊ณ„์‚ฐ ํ•˜๋Š” ๊ธฐ๋Šฅ (์ฃผ๋ง ์ œ์™ธ) `weekDayDiscount(int date, List<String> menus)` + - ๋””์ €ํŠธ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์› ํ• ์ธ +- [x] ์ฃผ๋ง ํ• ์ธ์„ ๊ณ„์‚ฐ ํ•˜๋Š” ๊ธฐ๋Šฅ (1,2,8,9,15,16,22,23,29,30) `weekendDayDiscount(int date, List<String> menus)` + - ๋ฉ”์ธ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์› ํ• ์ธ +- [x] ํŠน๋ณ„ ํ• ์ธ์ด ๊ฐ€๋Šฅํ•œ ๋‚  ์ธ์ง€ ํ™•์ธ ํ•˜๋Š” ๊ธฐ๋Šฅ (3,10,17,24,25,31) `starDayDiscount(int date)` + - 1,000์› ํ• ์ธ +- [x] ํ˜œํƒ ๋‚ด์—ญ์„ ์ถœ๋ ฅ ํ•˜๋Š” ๊ธฐ๋Šฅ `printDiscountAmount(int date, List<String> menus, int totalOrderAmount)` + - ๋ฐ›์€ ํ˜œํƒ์ด ์—†๋‹ค๋ฉด "์—†์Œ" ์ถœ๋ ฅ +- [x] ์ด ํ˜œํƒ ๊ธˆ์•ก์„ ๊ณ„์‚ฐ ํ•˜๋Š” ๊ธฐ๋Šฅ (ํ• ์ธ ๊ธˆ์•ก์˜ ํ•ฉ๊ณ„ + ์ฆ์ • ์ƒดํŽ˜์ธ์˜ ๊ฐ€๊ฒฉ) `calculateTotalDiscount(int date)` + - ํ˜œํƒ ๊ธˆ์•ก์ด ์—†์„ ์‹œ "0์›" ์ถœ๋ ฅ +- [x] ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก ๊ณ„์‚ฐ ํ•˜๋Š” ๊ธฐ๋Šฅ(ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก - ํ• ์ธ ๊ธˆ์•ก) `calculatePayment(int totalOrderAmount, int totalDiscount)` +- [x] ์ด ํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ผ ์ด๋ฒคํŠธ ๋ฐฐ์ง€๋ฅผ ์ถœ๋ ฅ ํ•˜๋Š” ๊ธฐ๋Šฅ `calculateBadge(int totalBenefits)` + - 5์ฒœ ์› ๋ฏธ๋งŒ: ์—†์Œ + - 5์ฒœ ์› ์ด์ƒ: ๋ณ„ + - 1๋งŒ ์› ์ด์ƒ: ํŠธ๋ฆฌ + - 2๋งŒ ์› ์ด์ƒ: ์‚ฐํƒ€ + + +## ํŒŒ์ผ ๋ชฉ๋ก +#### `EventPlanner.java` : ์ด๋ฒคํŠธ๋ฅผ ๊ณ„ํšํ•˜๊ณ  ์‹คํ–‰ํ•˜๋Š” ํด๋ž˜์Šค +#### `EventProcessor.java` : ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค +#### `NoEventProcessor.java` : ์ด๋ฒคํŠธ๊ฐ€ ์—†์„ ๋•Œ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค +#### `Badge.java` : ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ์—ด๊ฑฐํ˜• ํด๋ž˜์Šค +#### `DateDiscount.java` : ๋‚ ์งœ์— ๋”ฐ๋ฅธ ํ• ์ธ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค +#### `Menu.java` : ์Œ์‹ ๋ฉ”๋‰ด ์—ด๊ฑฐํ˜• ํด๋ž˜์Šค +#### `Order.java` : ์ฃผ๋ฌธ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค +#### `OrderItem.java` : ์ฃผ๋ฌธ ํ•ญ๋ชฉ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค +#### `StarDiscount.java` : ๋ณ„์ด ํ‘œ์‹œ๋œ ๋‚ ์— ๋Œ€ํ•œ ํ• ์ธ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค +#### `TotalDiscountCalculator.java` : ์ด ํ• ์ธ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค +#### `WeekendWeekdayDiscount.java` : ์ฃผ๋ง๊ณผ ํ‰์ผ์— ๋Œ€ํ•œ ํ• ์ธ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค +#### `ErrorMessage.java` : ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์—ด๊ฑฐํ˜• ํด๋ž˜์Šค +#### `OrderValidator.java` : ์ฃผ๋ฌธ์˜ ์œ ํšจ์„ฑ์„ ๊ฒ€์ฆํ•˜๋Š” ํด๋ž˜์Šค +#### `DiscountOutputPrinter.java` : ํ• ์ธ ๋‚ด์—ญ์„ ์ถœ๋ ฅํ•˜๋Š” ํด๋ž˜์Šค +#### `InputView.java` : ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ฐ›๋Š” ํด๋ž˜์Šค +#### `OrderOutputPrinter.java` : ์ฃผ๋ฌธ ๋‚ด์—ญ์„ ์ถœ๋ ฅํ•˜๋Š” ํด๋ž˜์Šค +#### `OutputView.java` : ์ถœ๋ ฅ ๊ด€๋ จ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค +#### `Application.java` : ํ”„๋กœ๊ทธ๋žจ์˜ ์‹œ์ž‘์ ์ด ๋˜๋Š” ํด๋ž˜์Šค + + +## ํ”„๋กœ๊ทธ๋žจ ์‚ฌ์šฉ๋ฒ• +1. Application ํด๋ž˜์Šค๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. +2. ํ”„๋กœ๊ทธ๋žจ์€ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐฉ๋ฌธ ๋‚ ์งœ์™€ ์ฃผ๋ฌธ์„ ์ž…๋ ฅํ•˜๋„๋ก ์•ˆ๋‚ดํ•ฉ๋‹ˆ๋‹ค. +3. ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์ˆซ์ž๋กœ ์ž…๋ ฅํ•˜๋ฉฐ, ์œ ํšจํ•œ ๋‚ ์งœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค (1 ~ 31). +4. ์ฃผ๋ฌธ์€ ๋ฉ”๋‰ด์™€ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•˜๋Š” ํ˜•์‹์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค (e.g., ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1). +5. ํ”„๋กœ๊ทธ๋žจ์€ ์ž…๋ ฅ๋œ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฒคํŠธ๋ฅผ ๊ณ„ํšํ•˜๊ณ  ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. +6. ํ• ์ธ ๋‚ด์—ญ, ์ฃผ๋ฌธ ๋ฉ”๋‰ด, ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก, ์ด ํ˜œํƒ ๊ธˆ์•ก, ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก, ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ๋“ฑ์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. + + +## ๊ธฐ๋Šฅ ์š”๊ตฌ์‚ฌํ•ญ +- ์ด๋ฒคํŠธ ๊ธฐ๊ฐ„: 2023.12.1 ~ 2023.12.31 +- ์ด์ฃผ๋ฌธ ๊ธˆ์•ก 10,000 ์ด์ƒ๋ถ€ํ„ฐ ์ด๋ฒคํŠธ ์ ์šฉ +- ์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ์‹œ, ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†๋‹ค. +- ๋ฉ”๋‰ด๋Š” ํ•œ๋ฒˆ์— ์ตœ๋Œ€ 20๊ฐœ๊นŒ์ง€๋งŒ ์ฃผ๋ฌธ ํ•  ์ˆ˜ ์žˆ๋‹ค. +- ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ : 1,000์›์œผ๋กœ ์‹œ์ž‘ํ•˜์—ฌ ํฌ๋ฆฌ์Šค๋งˆ์Šค๊ฐ€ ๋‹ค๊ฐ€์˜ฌ์ˆ˜๋ก ๋‚ ๋งˆ๋‹ค ํ• ์ธ ๊ธˆ์•ก์ด 100์›์”ฉ ์ฆ๊ฐ€ (2023.12.1 ~ 2023.12.31) +- ํ‰์ผ ํ• ์ธ(์ผ์š”์ผ~๋ชฉ์š”์ผ): ํ‰์ผ์—๋Š” ๋””์ €ํŠธ ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์› ํ• ์ธ +- ์ฃผ๋ง ํ• ์ธ(๊ธˆ์š”์ผ, ํ† ์š”์ผ): ์ฃผ๋ง์—๋Š” ๋ฉ”์ธ ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2,023์› ํ• ์ธ +- ํŠน๋ณ„ ํ• ์ธ: ์ด๋ฒคํŠธ ๋‹ฌ๋ ฅ์— ๋ณ„์ด ์žˆ์œผ๋ฉด ์ด์ฃผ๋ฌธ ๊ธˆ์•ก์—์„œ 1,000์› ํ• ์ธ +- ์ฆ์ • ์ด๋ฒคํŠธ: ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก์ด 12๋งŒ ์› ์ด์ƒ์ผ ๋•Œ, ์ƒดํŽ˜์ธ 1๊ฐœ ์ฆ์ • (25,000) +- ์ด์ฃผ๋ฌธ ๊ธˆ์•ก์—์„œ ํ•ด๋‹น ๊ธˆ์•ก๋งŒํผ ํ• ์ธ +- ์ดํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ด๋ฒคํŠธ ๋ฐฐ์ง€๋ฅผ ๋ถ€์—ฌ (๋ณ„, ํŠธ๋ฆฌ, ์‚ฐํƒ€)
Unknown
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,23 @@ +package christmas.model; + +public class OrderItem { + private final String menuName; + private final int quantity; + + public OrderItem(String menuName, int quantity) { + this.menuName = menuName; + this.quantity = quantity; + } + + public String getMenuName() { + return menuName; + } + + public int getQuantity() { + return quantity; + } + + public int calculateMenuAmount() { + return quantity * Menu.getPriceByMenuName(menuName); + } +}
Java
ํ™•์ธํ•œ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ ๋†“์นœ ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋„ค์š” ใ… ใ…  ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,57 @@ +package christmas.model; + +import christmas.view.OutputView; +import java.util.HashSet; +import java.util.Set; + +public class Order { + private static final int MENU_NAME_INDEX = 0; + private static final int QUANTITY_INDEX = 1; + + private final Set<OrderItem> menus; + + + private Order(Set<OrderItem> menus) { + this.menus = menus; + } + + public static Order createOrder(int date, String orderInput) { + Set<OrderItem> menus = createOrderItems(orderInput); + OutputView.printOrderPreview(date); + return new Order(menus); + } + + private static Set<OrderItem> createOrderItems(String orderInput) { + Set<OrderItem> menus = new HashSet<>(); + String[] orderItems = orderInput.split(","); + for (String orderItem : orderItems) { + String[] menuInfo = getMenuInfo(orderItem.trim()); + String menuName = menuInfo[MENU_NAME_INDEX]; + int quantity = Integer.parseInt(menuInfo[QUANTITY_INDEX]); + menus.add(new OrderItem(menuName, quantity)); + } + return menus; + } + + private static String[] getMenuInfo(String menu) { + return menu.split("-"); + } + + public Set<OrderItem> getMenus() { + return menus; + } + + public int calculateTotalOrderAmount() { + int totalOrderAmount = 0; + for (OrderItem orderItem : menus) { + totalOrderAmount += orderItem.calculateMenuAmount(); + } + return totalOrderAmount; + } + + public int calculateTotalDiscount(int date) { + int dateDiscount = TotalDiscountCalculator.calculateDateDiscount(date, menus); + int starDiscount = TotalDiscountCalculator.calculateStarDiscount(date); + return dateDiscount + starDiscount; + } +}
Java
์ „์— ์ƒ์ˆ˜ํ™” ๊ณต๋ถ€์ค‘ ๋ธ”๋กœ๊ทธ ๊ธ€์—์„œ ","(comma)๋ฅผ ์ƒ์ˆ˜ํ™” ํ•˜๋Š”๊ฒŒ ์˜๋ฏธ์—†๋Š” ์ƒ์ˆ˜ํ™”๋ผ๋Š” ๊ธ€์„ ๋ณธ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. "-" ํ•ด๋‹น ๊ฐ’๋„ ์ƒ์ˆ˜ํ™” ํ•ด์ฃผ๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ๋ง์”€ํ•ด ์ฃผ์…จ๋Š”๋ฐ ํ˜น์‹œ ","(comma)์™€ ๋‹ค๋ฅธ ์ด์œ ๊ฐ€ ์žˆ๋Š”์ง€ ์—ฌ์ญค๋ณด๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,48 @@ +package christmas.model; + +import java.util.Set; + +public class TotalDiscountCalculator { + + private static final int GIFT_MENU_ORDER_AMOUNT_THRESHOLD = 120000; + private static final int DEFAULT_DISCOUNT = 0; + + public static int calculateDateDiscount(int date, Set<OrderItem> menus) { + int dateDiscount = DateDiscount.ddayDiscount(date); + int weekDayDiscount = WeekendWeekdayDiscount.weekDayDiscount(date, menus); + int weekendDayDiscount = WeekendWeekdayDiscount.weekendDayDiscount(date, menus); + return dateDiscount + weekDayDiscount + weekendDayDiscount; + } + + public static int calculateStarDiscount(int date) { + return StarDiscount.starDayDiscount(date); + } + + public static int calculateGiftMenu(int totalOrderAmount, int totalDiscount) { + int totalBenefits = totalDiscount; + if (totalOrderAmount >= GIFT_MENU_ORDER_AMOUNT_THRESHOLD) { + totalBenefits += getGiftMenuPrice(); + } + return totalBenefits; + } + + public static int printGiftMenuPrice(int totalOrderAmount) { + if (totalOrderAmount < GIFT_MENU_ORDER_AMOUNT_THRESHOLD) { + return DEFAULT_DISCOUNT; + } + return getGiftMenuPrice(); + } + + public static String calculateBadge(int totalOrderAmount, int calculateTotalDiscount) { + int totalBenefits = calculateGiftMenu(totalOrderAmount, calculateTotalDiscount); + return Badge.calculateBadge(totalBenefits); + } + + public static int calculatePayment(int totalOrderAmount, int totalDiscount) { + return totalOrderAmount - totalDiscount; + } + + private static int getGiftMenuPrice() { + return Menu.getChampagnePrice(); + } +}
Java
TotalDiscountCalculator ํด๋ž˜์Šค๋ฅผ ๊ฐ์ฒด๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ํ•„์š”ํ•œ Discount ๊ณ„์‚ฐ ๋กœ์ง์„ ์—ฌ๋Ÿฌ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋กœ ๋ถ„ํ• ํ•œ๋‹ค๋ฉด static ๋ฉ”์„œ๋“œ ์—†์ด ์ข€ ๋” ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์„๊ฒƒ ๊ฐ™์€๋ฐ ์ œ๊ฐ€ ์ƒ๊ฐํ•œ ๋ฐฉ๋ฒ•์ด ๋งž์„์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,48 @@ +package christmas.model; + +import java.util.Set; + +public class TotalDiscountCalculator { + + private static final int GIFT_MENU_ORDER_AMOUNT_THRESHOLD = 120000; + private static final int DEFAULT_DISCOUNT = 0; + + public static int calculateDateDiscount(int date, Set<OrderItem> menus) { + int dateDiscount = DateDiscount.ddayDiscount(date); + int weekDayDiscount = WeekendWeekdayDiscount.weekDayDiscount(date, menus); + int weekendDayDiscount = WeekendWeekdayDiscount.weekendDayDiscount(date, menus); + return dateDiscount + weekDayDiscount + weekendDayDiscount; + } + + public static int calculateStarDiscount(int date) { + return StarDiscount.starDayDiscount(date); + } + + public static int calculateGiftMenu(int totalOrderAmount, int totalDiscount) { + int totalBenefits = totalDiscount; + if (totalOrderAmount >= GIFT_MENU_ORDER_AMOUNT_THRESHOLD) { + totalBenefits += getGiftMenuPrice(); + } + return totalBenefits; + } + + public static int printGiftMenuPrice(int totalOrderAmount) { + if (totalOrderAmount < GIFT_MENU_ORDER_AMOUNT_THRESHOLD) { + return DEFAULT_DISCOUNT; + } + return getGiftMenuPrice(); + } + + public static String calculateBadge(int totalOrderAmount, int calculateTotalDiscount) { + int totalBenefits = calculateGiftMenu(totalOrderAmount, calculateTotalDiscount); + return Badge.calculateBadge(totalBenefits); + } + + public static int calculatePayment(int totalOrderAmount, int totalDiscount) { + return totalOrderAmount - totalDiscount; + } + + private static int getGiftMenuPrice() { + return Menu.getChampagnePrice(); + } +}
Java
์ œ๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ž˜๋ชป ์ž‘์„ฑํ–ˆ๋„ค์š”.. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,57 @@ +package christmas.model; + +import christmas.view.OutputView; +import java.util.HashSet; +import java.util.Set; + +public class Order { + private static final int MENU_NAME_INDEX = 0; + private static final int QUANTITY_INDEX = 1; + + private final Set<OrderItem> menus; + + + private Order(Set<OrderItem> menus) { + this.menus = menus; + } + + public static Order createOrder(int date, String orderInput) { + Set<OrderItem> menus = createOrderItems(orderInput); + OutputView.printOrderPreview(date); + return new Order(menus); + } + + private static Set<OrderItem> createOrderItems(String orderInput) { + Set<OrderItem> menus = new HashSet<>(); + String[] orderItems = orderInput.split(","); + for (String orderItem : orderItems) { + String[] menuInfo = getMenuInfo(orderItem.trim()); + String menuName = menuInfo[MENU_NAME_INDEX]; + int quantity = Integer.parseInt(menuInfo[QUANTITY_INDEX]); + menus.add(new OrderItem(menuName, quantity)); + } + return menus; + } + + private static String[] getMenuInfo(String menu) { + return menu.split("-"); + } + + public Set<OrderItem> getMenus() { + return menus; + } + + public int calculateTotalOrderAmount() { + int totalOrderAmount = 0; + for (OrderItem orderItem : menus) { + totalOrderAmount += orderItem.calculateMenuAmount(); + } + return totalOrderAmount; + } + + public int calculateTotalDiscount(int date) { + int dateDiscount = TotalDiscountCalculator.calculateDateDiscount(date, menus); + int starDiscount = TotalDiscountCalculator.calculateStarDiscount(date); + return dateDiscount + starDiscount; + } +}
Java
์ƒ์ˆ˜ํ™”๋ฅผ ํ•˜๋Š” ๋ชฉ์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”? ๊ฐ€์žฅ ํฐ ์ด์œ ๋Š” ํ•ด๋‹น ๊ฐ’์ด ์ฝ”๋“œ์˜ ๋งฅ๋ฝ์ƒ `์–ด๋–ค ์—ญํ• `์„ ํ•˜๋Š”์ง€ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ์ ์ ˆํ•œ ์ด๋ฆ„์„ ๋ถ™์ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. `","`๋ฅผ `comma`๋ผ๊ณ  ๋„ค์ด๋ฐํ•˜๋Š” ๊ฒƒ์€ ์™œ ์˜๋ฏธ ์—†๋Š” ์ƒ์ˆ˜ํ™”์ผ๊นŒ์š”? 1. comma๋ผ๋Š” ์ด๋ฆ„์€ ํ•ด๋‹น ๋ฌธ์žฅ ๋ถ€ํ˜ธ์˜ ์ด๋ฆ„์ด์ง€, ์ฝ”๋“œ์—์„œ ํ•ด๋‹น ๋ฌธ์žฅ ๋ถ€ํ˜ธ์˜ ์—ญํ• ์— ๋Œ€ํ•ด ํ‘œํ˜„ํ•œ ์ด๋ฆ„์ด ์•„๋‹™๋‹ˆ๋‹ค. 2. menu๋ผ๋Š” String์˜ ํ˜•์‹์ด ๋ฐ”๋€Œ์–ด ๋ฉ”๋‰ด ์ด๋ฆ„๊ณผ ์ˆ˜๋Ÿ‰์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ตฌ๋ถ„์ž๊ฐ€ `","`์—์„œ `"-"`๋กœ ๋ณ€ํ™”ํ•œ๋‹ค๋ฉด ์ƒ์ˆ˜์˜ ๋„ค์ด๋ฐ๋„ `comma`์—์„œ `hyphen`์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋ถ„์ž๊ฐ€ ๋ฐ”๋€”๋•Œ๋งˆ๋‹ค ์ƒ์ˆ˜์˜ ์ด๋ฆ„์„ ๋ฐ”๊พธ๊ณ  ํ•ด๋‹น ์ƒ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ๊ณณ์—์„œ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผํ•œ๋‹ค๋ฉด ์ƒ์ˆ˜ํ™”๋ฅผ ํ•˜๋Š” ๋ชฉ์ ์„ ๋‹ฌ์„ฑํ–ˆ๋‹ค๊ณ  ๋ณด๊ธฐ ํž˜๋“ค๊ฒ ์ฃ . ๊ทธ๋Ÿฐ ์ด์œ ์—์„œ ","(comma)๋ฅผ ์ƒ์ˆ˜ํ™” ํ•˜๋Š”๊ฒŒ ์˜๋ฏธ์—†๋Š” ์ƒ์ˆ˜ํ™”๋ผ๊ณ  ํ‘œํ˜„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์˜๋ฏธ๋Š” ๋งค์ง ๋ฆฌํ„ฐ๋Ÿด์„ ์ƒ์ˆ˜ํ™”ํ•˜์ง€ ๋ง๋ผ๋Š” ์˜๋ฏธ๊ฐ€ ์•„๋‹Œ, ์œ ์˜๋ฏธํ•œ ๋„ค์ด๋ฐ์œผ๋กœ ์ƒ์ˆ˜ํ™”ํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,48 @@ +package christmas.model; + +import java.util.Set; + +public class TotalDiscountCalculator { + + private static final int GIFT_MENU_ORDER_AMOUNT_THRESHOLD = 120000; + private static final int DEFAULT_DISCOUNT = 0; + + public static int calculateDateDiscount(int date, Set<OrderItem> menus) { + int dateDiscount = DateDiscount.ddayDiscount(date); + int weekDayDiscount = WeekendWeekdayDiscount.weekDayDiscount(date, menus); + int weekendDayDiscount = WeekendWeekdayDiscount.weekendDayDiscount(date, menus); + return dateDiscount + weekDayDiscount + weekendDayDiscount; + } + + public static int calculateStarDiscount(int date) { + return StarDiscount.starDayDiscount(date); + } + + public static int calculateGiftMenu(int totalOrderAmount, int totalDiscount) { + int totalBenefits = totalDiscount; + if (totalOrderAmount >= GIFT_MENU_ORDER_AMOUNT_THRESHOLD) { + totalBenefits += getGiftMenuPrice(); + } + return totalBenefits; + } + + public static int printGiftMenuPrice(int totalOrderAmount) { + if (totalOrderAmount < GIFT_MENU_ORDER_AMOUNT_THRESHOLD) { + return DEFAULT_DISCOUNT; + } + return getGiftMenuPrice(); + } + + public static String calculateBadge(int totalOrderAmount, int calculateTotalDiscount) { + int totalBenefits = calculateGiftMenu(totalOrderAmount, calculateTotalDiscount); + return Badge.calculateBadge(totalBenefits); + } + + public static int calculatePayment(int totalOrderAmount, int totalDiscount) { + return totalOrderAmount - totalDiscount; + } + + private static int getGiftMenuPrice() { + return Menu.getChampagnePrice(); + } +}
Java
๊ฐ์ฒด์ง€ํ–ฅ์—์„œ ๊ฐ์ฒด๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”? ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ์ฒด๋Š” `์ƒํƒœ(State)` ์™€ `ํ–‰๋™(Behavior)` ์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ์ ์œผ๋กœ๋Š” ์•Œ๊ณ  ์žˆ๋Š” ๊ฒƒ๋“ค(์ƒํƒœ)๋ฅผ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ด๊ณ , ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค(ํ–‰๋™)์„ ๋ฉ”์„œ๋“œ๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๊ฒ ์ฃ . ์ด๋Ÿฌํ•œ ๊ด€์ ์—์„œ ์ „์ฒด์ ์ธ ์„ค๊ณ„๋ฅผ ์žฌ๊ฒ€ํ† ํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž
@@ -0,0 +1,77 @@ +export const validInputDates = ["0", "1", "31", "32", "-1", "-31"]; + +export const invalidInputDates = [ + "-0", + "2.5", + "foo", + "bar", + "๐Ÿ˜Š", + "12.0", + "12.000000000000004", + "12345678910987654321", +]; + +export const validDomainDates = [1, 2, 11, 15, 23, 31]; + +export const invalidDomainDates = [32, 0]; + +export const validInputMenus = [ + "-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "์–‘์†ก์ด์ˆ˜ํ”„-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-0", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-30", + "๋ ˆ๋“œ์™€์ธ-1", + "๋ ˆ๋“œ์™€์ธ-1,๋ธ”๋ฃจ์™€์ธ-1,ํผํ”Œ์™€์ธ-1,๊ทธ๋ฆฐ์™€์ธ-1", + "foo-1", +]; + +export const invalidInputMenus = [ + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ1", + "์–‘์†ก์ด์ˆ˜ํ”„-1,ํ‹ฐ๋ณธ-์Šคํ…Œ์ดํฌ-1", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ--1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-foo", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.0", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.000000000000004", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,,์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,-,์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", +]; + +export const valiidDomainMenus = [ + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1"]], + [ + [ + "์–‘์†ก์ด์ˆ˜ํ”„-3", + "ํƒ€ํŒŒ์Šค-2", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-3", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2", + "์•„์ด์Šคํฌ๋ฆผ-3", + "์ œ๋กœ์ฝœ๋ผ-6", + ], + ], +]; + +export const invalidDomainMenus = [ + [["-1"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", "-1", "์‹œ์ €์ƒ๋Ÿฌ๋“œ-1"]], + [["foo-1"]], + [["ํ‹ฐ๋ณธ์Šคํƒœ์ดํฌ-1"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-0"]], + [["์ƒดํŽ˜์ธ-1", "์ œ๋กœ์ฝœ๋ผ-2", "๋ ˆ๋“œ์™€์ธ-1"]], + [["์ œ๋กœ์ฝœ๋ผ-15"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-10", "ํƒ€ํŒŒ์Šค-10", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1"]], + [ + [ + "์–‘์†ก์ด์ˆ˜ํ”„-3", + "ํƒ€ํŒŒ์Šค-2", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-5", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-3", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2", + "์•„์ด์Šคํฌ๋ฆผ-5", + "์ œ๋กœ์ฝœ๋ผ-1", + ], + ], +];
JavaScript
ํ…Œ์ŠคํŠธ์—์„œ ์“ฐ์ด๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ ํ•œ ๊ณณ์— ๋ชจ์•„๋†“์œผ์…จ๋„ค์š”. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ๋งŽ์•„์ง€๋ฉด ๋งŽ์•„์งˆ์ˆ˜๋ก ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž์ฒด๊ฐ€ ๊ธธ์–ด์ง€๋Š”๋ฐ, ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒ ์–ด์š”! ๊ทธ๋Ÿฐ๋ฐ ์ด ๋ฐฉ์‹์„ ์ฑ„ํƒํ•˜๋ฉด ํ˜„์žฌ ํ…Œ์ŠคํŠธํ•˜๋Š” ์ผ€์ด์Šค๊ฐ€ ๋ฌด์—‡์ธ์ง€ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์— ์ง์ ‘์ ์œผ๋กœ ๋“œ๋Ÿฌ๋‚˜์ง€ ์•Š์•„์„œ, ์–ด๋–ค ๊ฐ’์ด ์ž…๋ ฅ๋˜๊ณ  ๊ทธ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ ๊ฐ’์€ ๋ฌด์—‡์ธ์ง€ ์•Œ๊ธฐ๊ฐ€ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™์•„์š”. ๋˜ ํ…Œ์ŠคํŠธ ๋‚ด์šฉ์ด ๋ฐ”๋€๋‹ค๋ฉด, ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ํŒŒ์ผ๋กœ ๋„˜์–ด๊ฐ€์„œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์€๋ฐ, ๊ด€๋ฆฌํ•˜๊ธฐ ์–ด๋ ต์ง€ ์•Š์„๊นŒ ์‹ถ์–ด์š”. ์–ด๋–ค ์ด์œ ์—์„œ ์ด ๋ฐฉ๋ฒ•์„ ์ฑ„ํƒํ•˜์…จ๋Š”์ง€๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,17 @@ +import { EOL as LINE_SEPARATOR } from "os"; + +export const COMMON_STRINGS = { + LINE_SEPARATOR, + EMPTY: "", + WHITE_SPACE: " ", + COLON: ":", + DASH: "-", + COMMA: ",", +}; + +export const CATEGORY_DICT = { + appetizer: "appetizer", + main: "main", + dessert: "dessert", + drink: "drink", +};
JavaScript
์ด ๋ถ€๋ถ„์„ ๋‹ค๋ฅธ common ํŒŒ์ผ์— ์ž‘์„ฑํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ๋‚˜์š”? domain์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ, domain์— ์ง์ ‘ ์ž‘์„ฑํ•œ๋‹ค๊ฑฐ๋‚˜, 'category'๋ผ๋Š” ์ƒ์ˆ˜ ํŒŒ์ผ์„ ๋”ฐ๋กœ ๋งŒ๋“ ๋‹ค๊ฑฐ๋‚˜ ํ•˜์ง€ ์•Š๊ณ  ์—ฌ๊ธฐ์— ์ž‘์„ฑํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”. Enum๊ฐ™์€ ๊ฐœ๋…์ด๋ผ ๋”ฐ๋กœ ๋นผ์‹  ๊ฑธ๊นŒ์š”?
@@ -0,0 +1,5 @@ +export { default as DOMAIN } from "./domain.js"; +export { default as INPUT } from "./input.js"; +export { default as OUTPUT } from "./output.js"; +export { default as ERROR } from "./error.js"; +export { default as VALIDATION } from "./validation.js";
JavaScript
์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•œ ํŒŒ์ผ์—์„œ๋„ `export default`๋ฅผ ํ•ด์ฃผ๊ณ  ์žˆ๋Š”๋ฐ, ๊ทธ๋Ÿฐ ์ƒ์ˆ˜ ํŒŒ์ผ๋“ค์„ ๊ฐ์‹ธ๋Š” ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”. ์–ด๋–ค ์ด์  ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ๋ฐฉ์‹์„ ์ฑ„ํƒํ•˜์‹  ๊ฑด๊ฐ€์š”?
@@ -0,0 +1,27 @@ +import { ItemCalc } from "../utils/Calc/index.js"; +import { DOMAIN } from "../Constants/index.js"; + +class BasicCalculator { + /** @param {MainOrder} mainOrder */ + static originalTotal(mainOrder) { + const nestedMenus = mainOrder.getMenus(); + const result = Object.values(nestedMenus) + .map((menus) => BasicCalculator.calcMenus(menus)); + return ItemCalc.sumArray(result); + } + + /** @param {{[menu: string]: number}} menus */ + static calcMenus(menus) { + const result = Object.entries(menus) + .map(([k, v]) => DOMAIN.MENU_DICT[k].price * v); + return ItemCalc.sumArray(result); + } + + /** @param {MainOrder} mainOrder */ + static calcEventFlag(mainOrder) { + const originalTotal = BasicCalculator.originalTotal(mainOrder); + return Boolean(DOMAIN.EVENT_THRESHOLD_INCLUSIVE <= originalTotal); + } +} + +export default BasicCalculator;
JavaScript
`key`, `value`์—๋„ ์ด๋ฆ„์„ ๋ถ™์ด๋ฉด ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šธ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,27 @@ +import { ItemCalc } from "../utils/Calc/index.js"; +import { DOMAIN } from "../Constants/index.js"; + +class BasicCalculator { + /** @param {MainOrder} mainOrder */ + static originalTotal(mainOrder) { + const nestedMenus = mainOrder.getMenus(); + const result = Object.values(nestedMenus) + .map((menus) => BasicCalculator.calcMenus(menus)); + return ItemCalc.sumArray(result); + } + + /** @param {{[menu: string]: number}} menus */ + static calcMenus(menus) { + const result = Object.entries(menus) + .map(([k, v]) => DOMAIN.MENU_DICT[k].price * v); + return ItemCalc.sumArray(result); + } + + /** @param {MainOrder} mainOrder */ + static calcEventFlag(mainOrder) { + const originalTotal = BasicCalculator.originalTotal(mainOrder); + return Boolean(DOMAIN.EVENT_THRESHOLD_INCLUSIVE <= originalTotal); + } +} + +export default BasicCalculator;
JavaScript
`Boolean()`์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์€๋ฐ ์ œ๊ฑฐํ•ด๋„ ๋˜์ง€ ์•Š์„๊นŒ์š”?
@@ -0,0 +1,75 @@ +// eslint-disable-next-line no-unused-vars +import MainOrder from "./MainOrder.js"; +import BasicCalculator from "./BasicCalculator.js"; +import { DayCalc, ItemCalc } from "../utils/Calc/index.js"; +import { DOMAIN } from "../Constants/index.js"; + +class DiscountCalculator { + // ์ƒ์† ๋ฉ”์„œ๋“œ(๋…ผ๋ฆฌ์ ) + /** @param {MainOrder} mainOrder */ + static #calcEventFlag(mainOrder) { + return BasicCalculator.calcEventFlag(mainOrder); + } + + // ๊ณ ์œ  ๋ฉ”์„œ๋“œ + /** @param {MainOrder} mainOrder */ + static discountInfo(mainOrder) { + const christmas = DiscountCalculator.christmasDiscount(mainOrder); + const weekday = DiscountCalculator.weekdayDiscount(mainOrder); + const weekend = DiscountCalculator.weekendDiscount(mainOrder); + const special = DiscountCalculator.specialDiscount(mainOrder); + + return { + christmas, weekday, weekend, special, + }; + } + + /** @param {MainOrder} mainOrder */ + static weekdayDiscount(mainOrder) { + const eventFlag = DiscountCalculator.#calcEventFlag(mainOrder); + if (!eventFlag) return 0; + + const day = mainOrder.getDate(); + if (DayCalc.isWeekend(day)) return 0; + const { dessert } = mainOrder.getMenus(); + const quantity = ItemCalc.sumObjValue(dessert); + return quantity * DOMAIN.WEEKDAY_DISCOUNT_PER_MENUS; + } + + /** @param {MainOrder} mainOrder */ + static weekendDiscount(mainOrder) { + const eventFlag = DiscountCalculator.#calcEventFlag(mainOrder); + if (!eventFlag) return 0; + + const day = mainOrder.getDate(); + if (!DayCalc.isWeekend(day)) return 0; + const { main } = mainOrder.getMenus(); + const quantity = ItemCalc.sumObjValue(main); + return quantity * DOMAIN.WEEKEND_DISCOUNT_PER_MENUS; + } + + /** @param {MainOrder} mainOrder */ + static specialDiscount(mainOrder) { + const eventFlag = DiscountCalculator.#calcEventFlag(mainOrder); + if (!eventFlag) return 0; + + const day = mainOrder.getDate(); + if (!DayCalc.isSpecialDay(day)) return 0; + return DOMAIN.SPECIAL_DAY_DISCOUNT; + } + + /** @param {MainOrder} mainOrder */ + static christmasDiscount(mainOrder) { + const eventFlag = DiscountCalculator.#calcEventFlag(mainOrder); + if (!eventFlag) return 0; + + const day = mainOrder.getDate(); + const [start, end] = [DOMAIN.D_DAY_START_INCLUSIVE, DOMAIN.D_DAY_END_INCLUSIVE]; + + if (end < day) return 0; + const multiplier = DayCalc.eventMoneyMultiplier(start, end, day); + return DOMAIN.D_DAY_DEFAULT_DISCOUNT + multiplier * DOMAIN.D_DAY_BONUS_DISCOUNT; + } +} + +export default DiscountCalculator;
JavaScript
`eventFlag`๋ฅผ ํ™•์ธํ•˜๋Š” ๋ถ€๋ถ„์ด ๋‹ค๋ฅธ ๊ณ„์‚ฐ ๋ฉ”์„œ๋“œ์—์„œ๋„ ์ค‘๋ณตํ•˜์—ฌ ์ˆ˜ํ–‰๋˜๊ณ  ์žˆ๋Š”๋ฐ, ์ด๊ฑธ ๋ฐ”๊นฅ์œผ๋กœ ๋นผ๋‚ด์„œ ์žฌ์‚ฌ์šฉํ•˜๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”? ์•„๋‹ˆ๋ฉด `eventFlag`๋ฅผ ํ™•์ธํ•˜๋Š” ์œ„์น˜๋ฅผ ๋‹ค๋ฅธ ๊ณณ์— ๋‘˜ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”. ๊ฐ๊ฐ์˜ ๋ฉ”์„œ๋“œ ์•ˆ์— ๋„ฃ์œผ์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค. ```javascript calculate(detailFunction) { const eventFlag = DiscountCalculator.#calcEventFlag(mainOrder); if (!eventFlag) return 0; detailFunction(); } weekdayDiscount(mainOrder) { const day = mainOrder.getDate(); if (DayCalc.isWeekend(day)) return 0; const { dessert } = mainOrder.getMenus(); const quantity = ItemCalc.sumObjValue(dessert); return quantity * DOMAIN.WEEKDAY_DISCOUNT_PER_MENUS; } calculate(() => weekdayDscount(mainOrder)); ```
@@ -0,0 +1,34 @@ +import { DomainValidator } from "../utils/Validator/index.js"; +import { DomainParser } from "../utils/Parser/index.js"; + +class MainOrder { + /** @type {number} */ + #date; + + /** @type {{appetizer: {}, main: {}, dessert:{}, drink: {}}} */ + #menus; + + /** @param {number} date */ + constructor(date) { + DomainValidator.date(date); + this.#date = date; + } + + getDate() { + return this.#date; + } + + /** @param {[string]} rawOrder */ + setMenus(rawOrder) { + if (this.#menus) throw new Error(); + DomainValidator.menus(rawOrder); + const parsedMenu = DomainParser.menus(rawOrder); + this.#menus = parsedMenu; + } + + getMenus() { + return this.#menus; + } +} + +export default MainOrder;
JavaScript
๋ฉ”๋‰ด๋ฅผ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ €์žฅํ•˜๋Š” ๊ฒŒ ํŽธ๋ฆฌํ•˜๊ณ  ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ‘
@@ -0,0 +1,77 @@ +export const validInputDates = ["0", "1", "31", "32", "-1", "-31"]; + +export const invalidInputDates = [ + "-0", + "2.5", + "foo", + "bar", + "๐Ÿ˜Š", + "12.0", + "12.000000000000004", + "12345678910987654321", +]; + +export const validDomainDates = [1, 2, 11, 15, 23, 31]; + +export const invalidDomainDates = [32, 0]; + +export const validInputMenus = [ + "-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "์–‘์†ก์ด์ˆ˜ํ”„-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-0", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-30", + "๋ ˆ๋“œ์™€์ธ-1", + "๋ ˆ๋“œ์™€์ธ-1,๋ธ”๋ฃจ์™€์ธ-1,ํผํ”Œ์™€์ธ-1,๊ทธ๋ฆฐ์™€์ธ-1", + "foo-1", +]; + +export const invalidInputMenus = [ + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ1", + "์–‘์†ก์ด์ˆ˜ํ”„-1,ํ‹ฐ๋ณธ-์Šคํ…Œ์ดํฌ-1", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ--1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-foo", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.0", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.000000000000004", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,,์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,-,์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", +]; + +export const valiidDomainMenus = [ + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1"]], + [ + [ + "์–‘์†ก์ด์ˆ˜ํ”„-3", + "ํƒ€ํŒŒ์Šค-2", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-3", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2", + "์•„์ด์Šคํฌ๋ฆผ-3", + "์ œ๋กœ์ฝœ๋ผ-6", + ], + ], +]; + +export const invalidDomainMenus = [ + [["-1"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", "-1", "์‹œ์ €์ƒ๋Ÿฌ๋“œ-1"]], + [["foo-1"]], + [["ํ‹ฐ๋ณธ์Šคํƒœ์ดํฌ-1"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-0"]], + [["์ƒดํŽ˜์ธ-1", "์ œ๋กœ์ฝœ๋ผ-2", "๋ ˆ๋“œ์™€์ธ-1"]], + [["์ œ๋กœ์ฝœ๋ผ-15"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-10", "ํƒ€ํŒŒ์Šค-10", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1"]], + [ + [ + "์–‘์†ก์ด์ˆ˜ํ”„-3", + "ํƒ€ํŒŒ์Šค-2", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-5", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-3", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2", + "์•„์ด์Šคํฌ๋ฆผ-5", + "์ œ๋กœ์ฝœ๋ผ-1", + ], + ], +];
JavaScript
์•— ํŠน๋ณ„ํžˆ ๋ฆฌ๋ทฐ๋ฅผ ๋ง‰ ๋ฐ›์•„์•ผ๊ฒ ๋‹ค๋Š” ๋งˆ์Œ์œผ๋กœ ๋ฆฌ๋ทฐ๋ฅผ ๋‚จ๊ธด ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ์™€์ฃผ์…”์„œ ๋„ˆ๋ฌด ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  `static`๋งŒ ์“ฐ๋Š” ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด์„œ ๋ง์”€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ €๋Š” ์ด๋ฒˆ ์šฐํ…Œ์ฝ”๊ฐ€ ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๋Š” ๊ฒƒ์ด ์ฒ˜์Œ์ธ๋ฐ์š”... ๋ฏธ์…˜ ์œ ํ‹ธ์˜ ๊ตฌ์กฐ์—์„œ `static`์„ ์“ฐ๋Š” ๊ฒƒ์„ ๋ณด๊ณ  ์ผ๋‹จ์€ `static` ๋ฉ”์„œ๋“œ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ์“ฐ๊ฒŒ ๋˜์—ˆ์–ด์š”. ์™œ ๊ฐ์ฒด๋ณด๋‹ค ํด๋ž˜์Šค๋ฅผ ์„ ํ˜ธํ•˜๋Š”์ง€ ๋ฌผ์–ด๋ณด์‹ ๋‹ค๋ฉด... ์ผ๋‹จ์€ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์•„๋ฌด๋ž˜๋„ ๋Œ€๋ฌธ์ž๋ฅผ ์“ฐ๊ณ  ์‹ถ๊ธฐ๋„ ํ•˜๊ณ (ํด๋ž˜์Šค, ์ƒ์„ฑ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ๋Œ€๋ฌธ์ž๋ฅผ ์“ฐ๋Š” ๊ฑด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ์ปจ๋ฒค์…˜์— ์œ„๋ฐฐ๋˜๋‹ˆ๊นŒ), ๋˜ ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ์— ์•„๋ฌด๋ž˜๋„ ๋Ÿฐํƒ€์ž„์—์„œ ์กฐ์ž‘๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด์š”. ํด๋ž˜์Šค๋ผ๊ณ  ๋” ์•ˆ์ „ํ•œ ๊ฑด ์•„๋‹ˆ๊ฒ ์ง€๋งŒ... ์•„๋ฌด๋ž˜๋„ ๊ทธ๋ƒฅ ์šฐํ…Œ์ฝ”์—์„œ ์“ฐ๋‹ˆ๊นŒ ์ด์œ ๊ฐ€ ์žˆ๊ฒ ๊ฑฐ๋‹ˆ ํ•œ ๋ถ€๋ถ„์ด ํฝ๋‹ˆ๋‹ค. ๋ง๋ถ™์—ฌ์„œ `static` ๋ฉ”์„œ๋“œ๋ฅผ ์“ฐ๋Š” ์ด์œ ๋Š”.. ์•„๋ฌด๋ž˜๋„ ์ธ์Šคํ„ด์Šค๋ฅผ ์•ˆ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋Š” ์ƒ๊ฐ์ด ํฐ ๊ฒƒ ๊ฐ™๋„ค์š”. ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ์‹œํ‚ค๊ณ  '๊ฒฐ๊ณผ'๋ฅผ ์ค‘๊ฐ„ ์ƒํƒœ๋กœ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ์—, ์ฃผ๋ฌธ์ด ๋ฐ”๋€Œ๊ฑฐ๋‚˜ ๋ฐฉ๋ฌธ ์ผ์ •์„ ๋ฐ”๊พผ๋‹ค๊ฑฐ๋‚˜ ํ•˜๋Š” ์กฐ์ž‘์„ ํ–ˆ์„ ๊ฒฝ์šฐ์— ์ค‘๊ฐ„ ๊ฒฐ๊ณผ๊นŒ์ง€ ๋ฐ”๊พธ์–ด์•ผ ํ•˜๋Š”๋ฐ ๊ทธ๊ฒŒ ์กฐ๊ธˆ ๋ฒˆ๊ฑฐ๋กœ์šธ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์— ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ–์ง€ ์•Š๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŽ์ด ๋งŒ๋“  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ถฉ๋ถ„ํ•œ ๋Œ€๋‹ต์€ ์•„๋‹ˆ๊ฒ ์ง€๋งŒ ๋Œ€์ถฉ์€ ์ด๋ ‡๋„ค์š” ๐Ÿ˜Š
@@ -0,0 +1,77 @@ +export const validInputDates = ["0", "1", "31", "32", "-1", "-31"]; + +export const invalidInputDates = [ + "-0", + "2.5", + "foo", + "bar", + "๐Ÿ˜Š", + "12.0", + "12.000000000000004", + "12345678910987654321", +]; + +export const validDomainDates = [1, 2, 11, 15, 23, 31]; + +export const invalidDomainDates = [32, 0]; + +export const validInputMenus = [ + "-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "์–‘์†ก์ด์ˆ˜ํ”„-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-0", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-30", + "๋ ˆ๋“œ์™€์ธ-1", + "๋ ˆ๋“œ์™€์ธ-1,๋ธ”๋ฃจ์™€์ธ-1,ํผํ”Œ์™€์ธ-1,๊ทธ๋ฆฐ์™€์ธ-1", + "foo-1", +]; + +export const invalidInputMenus = [ + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ1", + "์–‘์†ก์ด์ˆ˜ํ”„-1,ํ‹ฐ๋ณธ-์Šคํ…Œ์ดํฌ-1", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ--1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-foo", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.0", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.000000000000004", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,,์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,-,์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", +]; + +export const valiidDomainMenus = [ + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1"]], + [ + [ + "์–‘์†ก์ด์ˆ˜ํ”„-3", + "ํƒ€ํŒŒ์Šค-2", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-3", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2", + "์•„์ด์Šคํฌ๋ฆผ-3", + "์ œ๋กœ์ฝœ๋ผ-6", + ], + ], +]; + +export const invalidDomainMenus = [ + [["-1"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", "-1", "์‹œ์ €์ƒ๋Ÿฌ๋“œ-1"]], + [["foo-1"]], + [["ํ‹ฐ๋ณธ์Šคํƒœ์ดํฌ-1"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-0"]], + [["์ƒดํŽ˜์ธ-1", "์ œ๋กœ์ฝœ๋ผ-2", "๋ ˆ๋“œ์™€์ธ-1"]], + [["์ œ๋กœ์ฝœ๋ผ-15"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-10", "ํƒ€ํŒŒ์Šค-10", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1"]], + [ + [ + "์–‘์†ก์ด์ˆ˜ํ”„-3", + "ํƒ€ํŒŒ์Šค-2", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-5", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-3", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2", + "์•„์ด์Šคํฌ๋ฆผ-5", + "์ œ๋กœ์ฝœ๋ผ-1", + ], + ], +];
JavaScript
> ํ…Œ์ŠคํŠธ์—์„œ ์“ฐ์ด๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ ํ•œ ๊ณณ์— ๋ชจ์•„๋†“์œผ์…จ๋„ค์š”. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ๋งŽ์•„์ง€๋ฉด ๋งŽ์•„์งˆ์ˆ˜๋ก ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž์ฒด๊ฐ€ ๊ธธ์–ด์ง€๋Š”๋ฐ, ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒ ์–ด์š”! > > ๊ทธ๋Ÿฐ๋ฐ ์ด ๋ฐฉ์‹์„ ์ฑ„ํƒํ•˜๋ฉด ํ˜„์žฌ ํ…Œ์ŠคํŠธํ•˜๋Š” ์ผ€์ด์Šค๊ฐ€ ๋ฌด์—‡์ธ์ง€ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์— ์ง์ ‘์ ์œผ๋กœ ๋“œ๋Ÿฌ๋‚˜์ง€ ์•Š์•„์„œ, ์–ด๋–ค ๊ฐ’์ด ์ž…๋ ฅ๋˜๊ณ  ๊ทธ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ ๊ฐ’์€ ๋ฌด์—‡์ธ์ง€ ์•Œ๊ธฐ๊ฐ€ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™์•„์š”. ๋˜ ํ…Œ์ŠคํŠธ ๋‚ด์šฉ์ด ๋ฐ”๋€๋‹ค๋ฉด, ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ํŒŒ์ผ๋กœ ๋„˜์–ด๊ฐ€์„œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์€๋ฐ, ๊ด€๋ฆฌํ•˜๊ธฐ ์–ด๋ ต์ง€ ์•Š์„๊นŒ ์‹ถ์–ด์š”. > > ์–ด๋–ค ์ด์œ ์—์„œ ์ด ๋ฐฉ๋ฒ•์„ ์ฑ„ํƒํ•˜์…จ๋Š”์ง€๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฒฝ์šฐ์—๋Š” ์‚ฌ์‹ค ์ €๋„ 3์ฃผ์ฐจ๊นŒ์ง„ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์„ ์“ฐ์ง€๋Š” ์•Š์•˜๋Š”๋ฐ์š”.. 4์ฃผ์ฐจ์—๋Š” ํ•˜๋‚˜์˜ ์ผ€์ด์Šค๊ฐ€ ์—ฌ๋Ÿฌ ์‹œ๋‚˜๋ฆฌ์˜ค(ํ‰์ผ, ์ฃผ๋ง, ํŠน๋ณ„ ๋“ฑ)์— ์ ์šฉ๋˜์–ด์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋”๋ผ๊ณ ์š”. ์ด๋Ÿฐ ๊ฒฝ์šฐ์— ๊ตณ์ด ๋ฉ”๋‰ด๋ฅผ ๋งค๋ฒˆ ์“ฐ๊ณ  ์˜ˆ์ƒ๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์“ฐ๋Š” ๊ฒŒ ์ฐธ ๋ถˆํŽธํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ ๊ฒฐ๊ณผ๊ฐ’์ด์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด ๋‹ค์Œ์—๋Š” ์ค‘๊ฐ„ ์ธ์ž๊ฐ€ ๋˜๊ธฐ๋„ ํ•˜๊ณ ์š”. ๊ทธ๋ฆฌ๊ณ  ์‚ฌ์‹ค ์ฒ˜์Œ์— ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์งœ๊ณ  ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ฐœ๋ฐœ์„ ํ•ด๋ดค๋Š”๋ฐ์š”. ์˜ˆ์ƒํ–ˆ๋˜ ์ค‘๊ฐ„ ์ธ์ž๋“ค์ด ์ž๊พธ ๋ฐ”๋€Œ๋”๋ผ๊ณ ์š”. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์‚ฌ์‹ค ์ฒ˜์Œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์งค ๋‹น์‹œ์—, `์ด ํ˜œํƒ ๊ธˆ์•ก`์„ ๊ตฌํ•˜๋Š” ํ…Œ์ŠคํŠธ์˜ ์ธ์ž๋Š” `์ด ํ• ์ธ ๊ธˆ์•ก`๊ณผ `reward` ๊ฐ์ฒด์˜€๋Š”๋ฐ์š”... ์ค‘๊ฐ„์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋ฐ”๋€Œ๋‹ˆ๊นŒ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ „์ฒด๋ฅผ ์—Ž์–ด์•ผ ํ•˜๋”๋ผ๊ณ ์š”. ๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ํ…Œ์ŠคํŠธ ๋˜๋Š” ์ค‘๊ฐ„ ์ธ์ž๋ฅผ ์ƒ๊ฐํ•˜์ง€ ๋ง๊ณ  ์ž…๋ ฅ๊ณผ ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ์ค‘๊ฐ„, ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ๊ฐ์ฒด์— ๋ชฐ์•„์„œ ๊ด€๋ฆฌํ•˜๊ณ  ํ…Œ์ŠคํŠธ๊ฐ€ ๋ฐ”๋€” ๋•Œ ์ค‘๊ฐ„ ์ค‘๊ฐ„ ์ธ์ž๋ฅผ ๋ฐ”๊พธ์ž๋Š” ์ƒ๊ฐ์œผ๋กœ ์ด๋Ÿฐ ๊ตฌํ˜„์ด ๋˜๊ฒŒ ๋˜์—ˆ์–ด์š”. ๋งŒ์•ฝ ์œ ์ง€ ๋ณด์ˆ˜์˜ ๊ด€์ ์—์„œ ์ƒ๊ฐํ•ด๋ณธ๋‹ค๋ฉด ์ค‘๊ฐ„ ์ธ์ž๋ฅผ ์ข€ ์ˆ˜์ •ํ•˜๊ณ , jest ์˜ต์…˜์— --verbose๋ฅผ ๋„ฃ์–ด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๋Š” ์ค‘๊ฐ„์— ๋ฌด์—‡์ด ํ…Œ์ŠคํŠธ๋˜๋Š”์ง€ ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๋„ค์š”. ์Œ... ์‚ฌ์‹ค ๊ฐœ๋ฐœ ์ค‘๊ฐ„๊นŒ์ง€๋Š” ์ €ํ•œํ…Œ๋Š” ๊ต‰์žฅํžˆ ์œ ์ง€ ๋ณด์ˆ˜์— ํŽธ๋ฆฌํ•œ ์ฝ”๋“œ์˜€๋Š”๋ฐ์š”(ํ…Œ์ŠคํŠธ ์ธ์ž๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ ์ ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ์ ‘๊ทผํ•˜๋Š” ํ•„๋“œ๋งŒ ๋ฐ”๊พธ๋ฉด ๋˜๋‹ˆ๊นŒ์š”) ๋‹ค ์™„์„ฑํ•˜๊ณ  ๋ณด๋‹ˆ ๋‹ค์Œ ์‚ฌ๋žŒ์—๊ฒŒ๋Š” ์ƒ๋‹นํžˆ ๋ถˆ์นœ์ ˆํ•œ ์ฝ”๋“œ๊ฐ€ ๋˜์—ˆ๋„ค์š”. ์–ด๋–ป๊ฒŒ ํ•˜๋Š” ๊ฒŒ ์ข‹์„์ง€ ์ €๋„ ๊ณ ๋ฏผ์ด ๋ฉ๋‹ˆ๋‹ค. uraflower๋‹˜๊ป˜์„œ๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,17 @@ +import { EOL as LINE_SEPARATOR } from "os"; + +export const COMMON_STRINGS = { + LINE_SEPARATOR, + EMPTY: "", + WHITE_SPACE: " ", + COLON: ":", + DASH: "-", + COMMA: ",", +}; + +export const CATEGORY_DICT = { + appetizer: "appetizer", + main: "main", + dessert: "dessert", + drink: "drink", +};
JavaScript
์‚ฌ์‹ค validation์—์„œ๋„ ์‚ฌ์šฉ๋˜๊ธฐ์— domain์—์„œ ์“ฐ๋˜ ๊ฒƒ์„ ๋”ฐ๋กœ ๋นผ๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค. ํ•œํŽธ์œผ๋กœ๋Š” ์ด ๋…€์„์ด ๋„๋ฉ”์ธ์  ์„ฑ๊ฒฉ์„ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒƒ์€ ๋ถ„๋ช…ํ•˜์ง€๋งŒ ๋˜ ๊ทธ ์ž์ฒด๋กœ๋Š” ๋„๋ฉ”์ธ์ด ์•„๋‹ˆ๊ธฐ๋„ ํ•ด์„œ(์ด๋ฆ„์ด ๋„๋ฉ”์ธ ์ง€์‹์ด๋ผ๊ธฐ์—๋Š” ์• ๋งคํ•˜๋‹ˆ๊นŒ์š”) ๋”ฐ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ณ  ์‹ถ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋” ๋“ค๊ธฐ๋„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  common์œผ๋กœ ๋บ€ ๊ฒƒ์ด ์ข‹์€ ์ผ์ด์—ˆ๋Š”์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ์š”. Enum๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์ œ๊ฐ€ ์‚ฌ์‹ค ์ž˜ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ, ์•Œ์•„๋ณด๊ณ  ๋ง๋ถ™์—ฌ์•ผ ํ•  ๋‚ด์šฉ์ด ์žˆ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค๋ฉด ๋” ๋‚จ๊ธฐ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,5 @@ +export { default as DOMAIN } from "./domain.js"; +export { default as INPUT } from "./input.js"; +export { default as OUTPUT } from "./output.js"; +export { default as ERROR } from "./error.js"; +export { default as VALIDATION } from "./validation.js";
JavaScript
์‹ค์ œ๋กœ ๊ทธ๋Ÿด ์ผ์€ ๊ฑฐ์˜ ์—†๊ฒ ์ง€๋งŒ `VALIDATION`, `DOMAIN`, `INPUT`, ๊ทธ๋ฆฌ๊ณ  `OUTPUT`์„ ๋™์‹œ์— ์ž„ํฌํŠธํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์„ ๊ณ ๋ คํ•œ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ```javascript import DOMAIN from "../Constants/domain.js"; import VALIDATION from "../Constants/validation.js"; import INPUT from "../Constants/input.js"; import OUTPUT from "../Constants/output.js"; ``` ์œ„์™€ ๊ฐ™์ด ์ƒ์ˆ˜ `import`์— 4์ค„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ ์ด ๋ณ„๋กœ ๋งˆ์Œ์— ๋“ค์ง€ ์•Š์•„์„œ `index.js`๋ฅผ ํ†ตํ•ด ๋‹ค์‹œ ๋‚ด๋ณด๋‚ด๋Š” ๊ตฌ์กฐ๋ฅผ ์ทจํ•œ ๊ฒƒ์ด๊ณ , ํŠน๋ณ„ํžˆ ์–ด๋–ค ์ด์œ ๋‚˜ ์ด์ ์ด ํฌ์ง„ ์•Š์•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,27 @@ +import { ItemCalc } from "../utils/Calc/index.js"; +import { DOMAIN } from "../Constants/index.js"; + +class BasicCalculator { + /** @param {MainOrder} mainOrder */ + static originalTotal(mainOrder) { + const nestedMenus = mainOrder.getMenus(); + const result = Object.values(nestedMenus) + .map((menus) => BasicCalculator.calcMenus(menus)); + return ItemCalc.sumArray(result); + } + + /** @param {{[menu: string]: number}} menus */ + static calcMenus(menus) { + const result = Object.entries(menus) + .map(([k, v]) => DOMAIN.MENU_DICT[k].price * v); + return ItemCalc.sumArray(result); + } + + /** @param {MainOrder} mainOrder */ + static calcEventFlag(mainOrder) { + const originalTotal = BasicCalculator.originalTotal(mainOrder); + return Boolean(DOMAIN.EVENT_THRESHOLD_INCLUSIVE <= originalTotal); + } +} + +export default BasicCalculator;
JavaScript
์‚ฌ์‹ค ์ด ๋…€์„์€ ์‚ฌ์‹ค `ItemCalc`๋ผ๊ณ  ๋”ฐ๋กœ ํ™•์‹คํžˆ ์œ ํ‹ธ์ ์ธ ์„ฑ๊ฒฉ์ธ ๊ณ„์‚ฐ ํ•จ์ˆ˜๋“ค์„ ๋นผ๋†“์€ ๋ชจ๋“ˆ์— ์žˆ์—ˆ๋Š”๋ฐ, ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์„ ๋ณธ๋‹ค๋Š” ์ ์ด ํ™•์‹คํžˆ ๋„๋ฉ”์ธ์ด๋ผ ์ด์ชฝ์œผ๋กœ ์ด์‚ฌ๋ฅผ ์˜ค๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ฎ๊ธฐ๋Š” ๊ณผ์ •์—์„œ ์กฐ๊ธˆ ๋ถ€์ฃผ์˜ํ–ˆ๋‚˜๋ด์š”. ๋ง์”€ํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,27 @@ +import { ItemCalc } from "../utils/Calc/index.js"; +import { DOMAIN } from "../Constants/index.js"; + +class BasicCalculator { + /** @param {MainOrder} mainOrder */ + static originalTotal(mainOrder) { + const nestedMenus = mainOrder.getMenus(); + const result = Object.values(nestedMenus) + .map((menus) => BasicCalculator.calcMenus(menus)); + return ItemCalc.sumArray(result); + } + + /** @param {{[menu: string]: number}} menus */ + static calcMenus(menus) { + const result = Object.entries(menus) + .map(([k, v]) => DOMAIN.MENU_DICT[k].price * v); + return ItemCalc.sumArray(result); + } + + /** @param {MainOrder} mainOrder */ + static calcEventFlag(mainOrder) { + const originalTotal = BasicCalculator.originalTotal(mainOrder); + return Boolean(DOMAIN.EVENT_THRESHOLD_INCLUSIVE <= originalTotal); + } +} + +export default BasicCalculator;
JavaScript
์‚ฌ์‹ค ์ €๋Š” ์ด๋Ÿฐ ๊ฒฝ์šฐ ๊ทธ๋ƒฅ ๊ฐœ์ธ์ ์ธ ์ด์œ ๋กœ `Boolean()`์„ ์“ฐ๋Š” ๊ฒƒ์„ ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค. ์ƒ๊ฐํ•ด๋ณด๋ฉด ์ค‘๊ฐ„์— ์•ˆ ๋„ฃ์€ ๊ฒƒ๋„ ๋งŽ์ด ์žˆ๊ธด ํ•  ํ…๋ฐ, ์‹์œผ๋กœ ๋ฆฌํ„ดํ•˜๊ธฐ๋ณด๋‹ค๋Š” ๊ฐ’์œผ๋กœ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์ด ๋ญ”๊ฐ€ ์ง๊ด€์ ์œผ๋กœ ๋งž๋Š” ๊ฒƒ ๊ฐ™๋‹ค๋Š” ๋А๋‚Œ์ด ๋“ ๋‹ฌ๊นŒ์š”. ๊ทธ๋ƒฅ ๊ฐœ์ธ์ ์œผ๋กœ ์•ˆ์ •๊ฐ์ด ๋“œ๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŠน์„ฑ ์ƒ, ์ •์˜๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜์—ฌ๋„ ์–ด๋–ป๊ฒŒ๋“  ๋น„๊ตํ•ด์„œ ๋ฐ˜ํ™˜ํ•ด์ค„ ๊ฑฐ๋‹ˆ๊นŒ ๋ถˆ์•ˆํ•œ ๋งˆ์Œ์ด ๋“œ๋Š” ๊ฑธ๊นŒ์š”? ์•„๋ฌด๋ž˜๋„ ๊ทธ๋ ‡๊ฒŒ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,75 @@ +// eslint-disable-next-line no-unused-vars +import MainOrder from "./MainOrder.js"; +import BasicCalculator from "./BasicCalculator.js"; +import { DayCalc, ItemCalc } from "../utils/Calc/index.js"; +import { DOMAIN } from "../Constants/index.js"; + +class DiscountCalculator { + // ์ƒ์† ๋ฉ”์„œ๋“œ(๋…ผ๋ฆฌ์ ) + /** @param {MainOrder} mainOrder */ + static #calcEventFlag(mainOrder) { + return BasicCalculator.calcEventFlag(mainOrder); + } + + // ๊ณ ์œ  ๋ฉ”์„œ๋“œ + /** @param {MainOrder} mainOrder */ + static discountInfo(mainOrder) { + const christmas = DiscountCalculator.christmasDiscount(mainOrder); + const weekday = DiscountCalculator.weekdayDiscount(mainOrder); + const weekend = DiscountCalculator.weekendDiscount(mainOrder); + const special = DiscountCalculator.specialDiscount(mainOrder); + + return { + christmas, weekday, weekend, special, + }; + } + + /** @param {MainOrder} mainOrder */ + static weekdayDiscount(mainOrder) { + const eventFlag = DiscountCalculator.#calcEventFlag(mainOrder); + if (!eventFlag) return 0; + + const day = mainOrder.getDate(); + if (DayCalc.isWeekend(day)) return 0; + const { dessert } = mainOrder.getMenus(); + const quantity = ItemCalc.sumObjValue(dessert); + return quantity * DOMAIN.WEEKDAY_DISCOUNT_PER_MENUS; + } + + /** @param {MainOrder} mainOrder */ + static weekendDiscount(mainOrder) { + const eventFlag = DiscountCalculator.#calcEventFlag(mainOrder); + if (!eventFlag) return 0; + + const day = mainOrder.getDate(); + if (!DayCalc.isWeekend(day)) return 0; + const { main } = mainOrder.getMenus(); + const quantity = ItemCalc.sumObjValue(main); + return quantity * DOMAIN.WEEKEND_DISCOUNT_PER_MENUS; + } + + /** @param {MainOrder} mainOrder */ + static specialDiscount(mainOrder) { + const eventFlag = DiscountCalculator.#calcEventFlag(mainOrder); + if (!eventFlag) return 0; + + const day = mainOrder.getDate(); + if (!DayCalc.isSpecialDay(day)) return 0; + return DOMAIN.SPECIAL_DAY_DISCOUNT; + } + + /** @param {MainOrder} mainOrder */ + static christmasDiscount(mainOrder) { + const eventFlag = DiscountCalculator.#calcEventFlag(mainOrder); + if (!eventFlag) return 0; + + const day = mainOrder.getDate(); + const [start, end] = [DOMAIN.D_DAY_START_INCLUSIVE, DOMAIN.D_DAY_END_INCLUSIVE]; + + if (end < day) return 0; + const multiplier = DayCalc.eventMoneyMultiplier(start, end, day); + return DOMAIN.D_DAY_DEFAULT_DISCOUNT + multiplier * DOMAIN.D_DAY_BONUS_DISCOUNT; + } +} + +export default DiscountCalculator;
JavaScript
์ด ๋ถ€๋ถ„๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์›๋ž˜ ๋”ฐ๋กœ ๋ถ„๋ฆฌ๋˜์–ด์žˆ์—ˆ๋Š”๋ฐ์š”. ํ• ์ธ์ด ์•„๋‹ˆ๋ผ ์ฆ์ •ํ’ˆ ๋•Œ๋ฌธ์— ๊ทธ๋ƒฅ eventFlag๋กœ ๋น ์ง€๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. `reward`์˜ ๊ฒฝ์šฐ ๋ฐ˜ํ™˜๊ฐ’์ด 0์ด ์•„๋‹ˆ๋ผ {}์ธ๋ฐ ์ด๋Ÿด ๊ฒฝ์šฐ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๋‘˜๋กœ ๊ฐˆ๋ฆฌ๊ฒŒ ๋˜๊ณ , ์ด ๊ฒฝ์šฐ ์œ ์ง€ ๋ณด์ˆ˜์— ์žˆ์–ด์„œ๋„ ๊ทธ๋ ‡๊ณ  ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ฉ”์„œ๋“œ๋ฅผ ๋Š˜๋ฆฌ๊ณ  ์‹ถ์ง€๋„ ์•Š๋‹ค๋Š” ์ƒ๊ฐ์— ๊ทธ๋ƒฅ `eventFlag`๋ฅผ ์œ ์ง€ํ•˜๊ฒŒ ๋˜์—ˆ๋„ค์š”. ๊ทธ๋Ÿฐ๋ฐ ๋ง์”€์„ ๋“ฃ๊ณ  ๋‚˜๋‹ˆ... ์—ฌ์ „ํžˆ ๋ถ„๋ฆฌ๊ฐ€ ๋˜๋„ค์š”. ์ธ์ž๋กœ true์ผ ๊ฒฝ์šฐ ๋ฐ˜ํ™˜ํ•  ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋˜๋‹ˆ๊นŒ์š”. ์•„๋ฌด๋ž˜๋„.. ์ œ๊ฐ€ ๋œ ์ƒ๊ฐํ•œ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,34 @@ +import { DomainValidator } from "../utils/Validator/index.js"; +import { DomainParser } from "../utils/Parser/index.js"; + +class MainOrder { + /** @type {number} */ + #date; + + /** @type {{appetizer: {}, main: {}, dessert:{}, drink: {}}} */ + #menus; + + /** @param {number} date */ + constructor(date) { + DomainValidator.date(date); + this.#date = date; + } + + getDate() { + return this.#date; + } + + /** @param {[string]} rawOrder */ + setMenus(rawOrder) { + if (this.#menus) throw new Error(); + DomainValidator.menus(rawOrder); + const parsedMenu = DomainParser.menus(rawOrder); + this.#menus = parsedMenu; + } + + getMenus() { + return this.#menus; + } +} + +export default MainOrder;
JavaScript
๋Œ€์‹  ์ค‘์ฒฉ์ด ๋ฐœ์ƒํ•ด์„œ ๊ทธ๋ƒฅ ์ˆœํšŒํ•ด์„œ๋Š” ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ด ๋‹จ์ ์ด๋ž๋‹ˆ๋‹ค๐Ÿ˜ž
@@ -0,0 +1,77 @@ +export const validInputDates = ["0", "1", "31", "32", "-1", "-31"]; + +export const invalidInputDates = [ + "-0", + "2.5", + "foo", + "bar", + "๐Ÿ˜Š", + "12.0", + "12.000000000000004", + "12345678910987654321", +]; + +export const validDomainDates = [1, 2, 11, 15, 23, 31]; + +export const invalidDomainDates = [32, 0]; + +export const validInputMenus = [ + "-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "์–‘์†ก์ด์ˆ˜ํ”„-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-0", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-30", + "๋ ˆ๋“œ์™€์ธ-1", + "๋ ˆ๋“œ์™€์ธ-1,๋ธ”๋ฃจ์™€์ธ-1,ํผํ”Œ์™€์ธ-1,๊ทธ๋ฆฐ์™€์ธ-1", + "foo-1", +]; + +export const invalidInputMenus = [ + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ1", + "์–‘์†ก์ด์ˆ˜ํ”„-1,ํ‹ฐ๋ณธ-์Šคํ…Œ์ดํฌ-1", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ--1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-foo", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.0", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.000000000000004", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,,์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,-,์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", +]; + +export const valiidDomainMenus = [ + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1"]], + [ + [ + "์–‘์†ก์ด์ˆ˜ํ”„-3", + "ํƒ€ํŒŒ์Šค-2", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-3", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2", + "์•„์ด์Šคํฌ๋ฆผ-3", + "์ œ๋กœ์ฝœ๋ผ-6", + ], + ], +]; + +export const invalidDomainMenus = [ + [["-1"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", "-1", "์‹œ์ €์ƒ๋Ÿฌ๋“œ-1"]], + [["foo-1"]], + [["ํ‹ฐ๋ณธ์Šคํƒœ์ดํฌ-1"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-0"]], + [["์ƒดํŽ˜์ธ-1", "์ œ๋กœ์ฝœ๋ผ-2", "๋ ˆ๋“œ์™€์ธ-1"]], + [["์ œ๋กœ์ฝœ๋ผ-15"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-10", "ํƒ€ํŒŒ์Šค-10", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1"]], + [ + [ + "์–‘์†ก์ด์ˆ˜ํ”„-3", + "ํƒ€ํŒŒ์Šค-2", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-5", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-3", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2", + "์•„์ด์Šคํฌ๋ฆผ-5", + "์ œ๋กœ์ฝœ๋ผ-1", + ], + ], +];
JavaScript
`Jest`์— `--verbose`๋ผ๋Š” ์˜ต์…˜์ด ์žˆ๋Š”์ง€ ๋ชฐ๋ž๋„ค์š”. ๊ทธ๋Ÿฐ๋ฐ ์ฐพ์•„๋ณด๋‹ˆ `--verbose`๋Š” ํ…Œ์ŠคํŠธ์— ์‚ฌ์šฉ๋œ ๊ฐ’๋“ค์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ์˜ต์…˜์ธ ๊ฒƒ ๊ฐ™์•„์š”. ํ˜น์‹œ ์ œ๊ฐ€ ์ž˜๋ชป ์ดํ•ดํ•œ ๊ฒƒ์ด๋ผ๋ฉด ์•Œ๋ ค์ฃผ์„ธ์š”! --- ### ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์˜ ๋ชฉ์  ์ œ๊ฐ€ ๋ฆฌ๋ทฐ๋“œ๋ฆฐ ์˜๋„๋Š” "์–ด๋–ค ๊ฐ’์œผ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋Š”์ง€๋ฅผ **ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๋‚ด๋ถ€** ์—์„œ ํ™•์ธํ•˜๊ณ  ๊ด€๋ฆฌํ•ด์•ผ ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ์‰ฝ์ง€ ์•Š์„๊นŒ?"๋ผ๋Š” ๋œป์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ง€๊ธˆ์ฒ˜๋Ÿผ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์™€ ํ…Œ์ŠคํŠธ์— ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’๋“ค์„ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์ €์žฅํ•ด๋„ ๋งˆ์šฐ์Šค ์˜ฌ๋ฆฌ๋ฉด ๊ฐ’์ด ๋‹ค ๋‚˜์˜ค๊ธด ํ•˜์ง€๋งŒ, ์ฝ”๋“œ๋ฅผ ๋”ฑ ๋ดค์„ ๋•Œ ์–ด๋–ค ๊ฐ’์ด ์˜ˆ์™ธ๋ฅผ ์ผ์œผํ‚ค๋Š”์ง€, ๋˜ ์ •์ƒ์ธ์ง€, ์–ด๋–ค ์ผ€์ด์Šค๋“ค์ด ์–ด๋–ค ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ๋” ๋ฌธ์„œ ์—ญํ• ๋„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒŒ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•ด์š”. ๊ทธ๋ž˜์„œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๋‚ด๋ถ€์—์„œ ๊ฐ’๋“ค์ด ๊ด€๋ฆฌ๋˜์–ด์•ผ ํ•˜์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•ด๋ด…๋‹ˆ๋‹ค. ### ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์˜ ์žฅ๋‹จ์  ๋ง์”€ํ•˜์‹  ๊ฒƒ์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ๊ตฐ๋ฐ์—์„œ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’๋“ค์ด ์žˆ์„ ๋•Œ, ํ”„๋กœ๋•์…˜ ์ฝ”๋“œ์—์„œ ์ถ”์ถœํ•˜๋“ฏ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์—์„œ๋„ ์ถ”์ถœํ•ด์•ผ ํ•˜๋Š”์ง€ ๊ณ ๋ฏผ์ด ๋˜๊ธด ํ•˜๋„ค์š”๐Ÿค”. ๋™์ผํ•œ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ํ…Œ์ŠคํŠธ๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ์ถ”์ถœํ•˜๋Š” ๊ฒŒ ๋” ํŽธํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๊ตฌ์š”. ๊ทธ๋Ÿฐ๋ฐ A,B,C ... ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ๊ฐ€ "Z" ๋ผ๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, A ํ…Œ์ŠคํŠธ๊ฐ€ ๋ชจ์ข…์˜ ์ด์œ ๋กœ ํ•ด๋‹น ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ ๋”ฑ ํ•˜๋‚˜๋งŒ ๋นผ๊ณ  ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค๊ณ  ํ•˜๋ฉด ๊ณจ์น˜์•„ํŒŒ์งˆ ๊ฒƒ ๊ฐ™์•„์š”. ์˜ˆ๋ฅผ ๋“ค๋ฉด "Z" ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์–‘์˜ ์ •์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฐ’๋“ค์˜ ๋ชจ์Œ์ผ ๋•Œ, "Z"์—๋Š” ์†Œ์ˆ˜ ๊ฐ’์ด ์žˆ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ์š”. ๊ทธ๋Ÿผ A,B,C ... ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ๋Š” "Z"๋ฅผ ๋ฝ‘์•„์„œ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ A ๋ผ๋Š” ๊ธฐ๋Šฅ์ด ๋ชจ์ข…์˜ ์ด์œ ๋กœ ์ธํ•ด ์†Œ์ˆ˜๊ฐ€ ๋“ค์–ด์™€๋„ ๋˜๋„๋ก ๋ณ€๊ฒฝ๋œ๋‹ค๋ฉด, "Z" ์ผ€์ด์Šค๋ฅผ ์ง€๊ธˆ์ฒ˜๋Ÿผ ์™ ๋ฝ‘์•„๋‹ค ์“ธ ์ˆ˜ ์—†์„ ๊ฒƒ ๊ฐ™์•„์š”. --- ์ €๋„ ๋ฌด์—‡์ด ์ •๋‹ต์ธ์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค๐Ÿ˜…. ์ €ํฌ๊ฐ€ ์ž‘์€ ๊ทœ๋ชจ์˜ ํ”„๋กœ์ ํŠธ์—๋งŒ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ ์šฉํ•ด๋ด์„œ ํ—ท๊ฐˆ๋ คํ•˜๋Š” ๊ฑธ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์ƒ๊ฐ๋„ ๋“ค๊ตฌ์š”. ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์— ๊ด€ํ•œ ์ฑ…์„ ์ฝ์–ด๋ณด๊ฑฐ๋‚˜ ์ ์  ํฐ ๊ทœ๋ชจ์— ์ ์šฉ์‹œ์ผœ๋ณธ๋‹ค๋ฉด ์ข‹์€ ๋ฐฉ์‹์— ๋Œ€ํ•œ ํ™•์‹ ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,77 @@ +export const validInputDates = ["0", "1", "31", "32", "-1", "-31"]; + +export const invalidInputDates = [ + "-0", + "2.5", + "foo", + "bar", + "๐Ÿ˜Š", + "12.0", + "12.000000000000004", + "12345678910987654321", +]; + +export const validDomainDates = [1, 2, 11, 15, 23, 31]; + +export const invalidDomainDates = [32, 0]; + +export const validInputMenus = [ + "-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "์–‘์†ก์ด์ˆ˜ํ”„-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-0", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-30", + "๋ ˆ๋“œ์™€์ธ-1", + "๋ ˆ๋“œ์™€์ธ-1,๋ธ”๋ฃจ์™€์ธ-1,ํผํ”Œ์™€์ธ-1,๊ทธ๋ฆฐ์™€์ธ-1", + "foo-1", +]; + +export const invalidInputMenus = [ + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ1", + "์–‘์†ก์ด์ˆ˜ํ”„-1,ํ‹ฐ๋ณธ-์Šคํ…Œ์ดํฌ-1", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ--1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-foo", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.0", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.000000000000004", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,,์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1,-,์‹œ์ €์ƒ๋Ÿฌ๋“œ-1", +]; + +export const valiidDomainMenus = [ + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1"]], + [ + [ + "์–‘์†ก์ด์ˆ˜ํ”„-3", + "ํƒ€ํŒŒ์Šค-2", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-3", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2", + "์•„์ด์Šคํฌ๋ฆผ-3", + "์ œ๋กœ์ฝœ๋ผ-6", + ], + ], +]; + +export const invalidDomainMenus = [ + [["-1"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1", "-1", "์‹œ์ €์ƒ๋Ÿฌ๋“œ-1"]], + [["foo-1"]], + [["ํ‹ฐ๋ณธ์Šคํƒœ์ดํฌ-1"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-0"]], + [["์ƒดํŽ˜์ธ-1", "์ œ๋กœ์ฝœ๋ผ-2", "๋ ˆ๋“œ์™€์ธ-1"]], + [["์ œ๋กœ์ฝœ๋ผ-15"]], + [["ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-10", "ํƒ€ํŒŒ์Šค-10", "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1"]], + [ + [ + "์–‘์†ก์ด์ˆ˜ํ”„-3", + "ํƒ€ํŒŒ์Šค-2", + "์‹œ์ €์ƒ๋Ÿฌ๋“œ-5", + "ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-3", + "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2", + "์•„์ด์Šคํฌ๋ฆผ-5", + "์ œ๋กœ์ฝœ๋ผ-1", + ], + ], +];
JavaScript
์Œ ์ด ๋ถ€๋ถ„์€ ์ œ๊ฐ€ ์˜คํ•ดํ•˜๊ธฐ๋„ ํ–ˆ๊ณ , ๋˜ ์„ค๋ช…์ด ์ข€ ๋ถˆ์ถฉ๋ถ„ํ–ˆ๋Š”๋ฐ์š”... ์šฐ์„  verbose๋ฅผ ์“ฐ๊ณ  ์ธ์ž์— ๋„˜๊ธธ ๋•Œ, ์ „์ฒด๋ฅผ ๋„˜๊ธฐ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ํŠน์ • ํ•„๋“œ๋ฅผ ํŠน์ •ํ•ด์„œ ๋„˜๊ธฐ๊ฒŒ ๋œ๋‹ค๋ฉด ํ…Œ์ŠคํŠธ๋ฅผ '๋Œ๋ ธ์„ ๋•Œ' ์–ด๋–ค ๊ฐ’์ด ํ…Œ์ŠคํŠธ๊ฐ€ ๋˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋ž€ ๋ง์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ด๋ ‡๊ฒŒ ํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ 'ํ…Œ์ŠคํŠธ ์ฝ”๋“œ'์—๋Š” ์ง์ ‘์ ์œผ๋กœ ์–ด๋–ค ๊ฐ’์ด ๋“ค์–ด์˜ค๋Š”์ง€ ์•Œ ์ˆ˜๋Š” ์—†์ง€๋งŒ์š”. ๋ง์”€ํ•ด์ฃผ์‹  ๋ฌธ์ œ๊ฐ€ ์–ด๋А ์ •๋„ ํ•ด์†Œ๊ฐ€ ๋˜๋ฆฌ๋ผ๋Š” ์ƒ๊ฐ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  `๋ฌธ์„œ`์˜ ์—ญํ• ์„ ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์ด ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋Š” ์–ด๋–ค ๋ถ€๋ถ„์—์„œ ๊ธฐ๋Šฅ์„ ๋ชปํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์‚ฌ์‹ค์ž…๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ๊น”๋”ํ•˜๊ฒŒ ์ธ์ •ํ•  ์ˆ˜๋ฐ–์— ์—†๊ฒ ๋„ค์š”. ํ•˜์ง€๋งŒ ์ผ€์ด์Šค๊ฐ€ ํŠน์ˆ˜ํ•˜๊ฒŒ ๋ฐ”๋€Œ๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋ชฐ์•„์„œ ๊ด€๋ฆฌํ•˜๋Š” ํŽธ์ด ํ™•์‹คํžˆ ํŽธ๋ฆฌํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ `ํ‰์ผ ํ• ์ธ`์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ํ’ˆ๋ชฉ์— ์–ด๋–ค ์ด์œ ์—์„œ์ธ์ง€ `์ œ๋กœ์ฝœ๋ผ`๊ฐ€ ์ถ”๊ฐ€ ๋˜์—ˆ๋‹ค๊ณ  ํ•˜๋ฉด, (๋„˜๊ธฐ๋Š” ์ธ์ž์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒ ์ง€๋งŒ) ํ†ต์ƒ์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” 1. ํ‰์ผ ํ• ์ธ ๊ณ„์‚ฐ ํ…Œ์ŠคํŠธ, 2. ์ „์ฒด ํ• ์ธ ๊ณ„์‚ฐ ํ…Œ์ŠคํŠธ, 3. ํ˜œํƒ ๊ณ„์‚ฐ ํ…Œ์ŠคํŠธ, 4. ๋ฐฐ์ง€ ํ…Œ์ŠคํŠธ๊ฐ€ ์˜ํ–ฅ์„ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ด ๋„ค ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ฐพ์•„์„œ ์ œ๋กœ ์ฝœ๋ผ ๊ด€๋ จ ์ผ€์ด์Šค๋ฅผ ๋‹ค ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š” ๋ฐ˜๋ฉด, ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋ชฐ์•„์„œ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋˜๋ฉด `์ œ๋กœ ์ฝœ๋ผ` ์ผ€์ด์Šค๋ฅผ ์ฐพ์€ ๋‹ค์Œ ์ € 4๊ฐœ์˜ ๋‚ด์šฉ์„ ๊ณ ์ณ์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์“ฑ ๋นผ๋Š” ๊ฒƒ์ด ํž˜๋“ค๋‹ค๋Š” ๊ฒƒ์ด ๋ฌธ์ œ๋ผ๋ฉด ์•„๋งˆ๋„ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ํ—ˆ์šฉ๋˜๋Š” ์‹œ๋‚˜๋ฆฌ์˜ค๋‚˜ ์ˆ˜์ค€์„ ํ•„๋“œ๋กœ ์ •ํ•ด์ค„ ์ˆ˜๋„ ์žˆ๊ฒ ์ฃ ? ๊ทธ๋Ÿฌ๋ฉด A์—์„œ ํ…Œ์ŠคํŠธํ•  ๋•Œ Z์—์„œ `inputLevel`์„ ์ œ์™ธํ•˜๊ณ  ํ•„ํ„ฐํ•˜๊ฑฐ๋‚˜ `domainLevel`์ธ ๊ฒƒ๋“ค๋งŒ ํ•„ํ„ฐ๋ฅผ ํ•ด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋‚˜๋ฆ„์˜ ๋ฐฉ๋ฒ•์ด ๋  ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ด๋Ÿฐ ๋ฌธ์ œ ๋•Œ๋ฌธ์— ์ง€๊ธˆ๋„ `inputCase`์™€ `validCase` ๊ทธ๋ฆฌ๊ณ  `tryErrorCase`๋ผ๋Š” 3๊ฐ€์ง€ ๋ณ„๋„์˜ ํŒŒ์ผ์„ ํ†ต์ผ๋œ ํ˜•์‹ ์—†์ด ์šด์šฉํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ์š”. ๋ง์”€ํ•ด์ฃผ์‹  ๋ถ€๋ถ„์„ ์ƒ๊ฐํ•˜๊ณ  ๋ณด๋‹ˆ ์กฐ๊ธˆ ๋” ํ†ต์ผ์„ฑ ์žˆ๋Š” ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด์„œ๋„ ๋˜ ์ผ€์ด์Šค๋ฅผ ๋นผ์„œ ์“ฐ๊ธฐ ์–ด๋ ค์šด ์ƒํ™ฉ๋“ค์ด ๋‚˜๋‰˜๋Š” ๋ถ€๋ถ„๋„ ์กฐ๊ธˆ์€ ํ•ด๊ฒฐํ•  ๊ธธ์ด ๋ณด์ด๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์ด ๊ทธ๋ฆฌ ๊น”๋”ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ๋ง์ด์ฃ . ๊ทธ๋ž˜๋„ ๋ง์”€ํ•ด์ฃผ์‹  ๋ถ€๋ถ„์ด ๋งŽ์€ ๋„์›€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ํ˜„์žฅ์—์„œ ํ•ด๋‚˜๊ฐ€๋ฉด์„œ ๋ฌด์—‡์ด ์–ด๋–ค ์ƒํ™ฉ์—์„œ ๋” ์ข‹์€์ง€, ๋˜ ์–ด๋–ค ์žฅ์ ๊ณผ ๋‹จ์ ์ด ์žˆ๋Š”์ง€ ๋” ๋ฐฐ์šฐ๊ฒŒ ๋˜๊ฒ ์ฃ . ๋•๋ถ„์— ๊ทธ๋Ÿฐ ๋ฐฐ์›€์˜ ์‹œ๊ฐ„์ด ์™”์„ ๋•Œ ์กฐ๊ธˆ ๋” ์—ด๋ฆฐ ์‚ฌ๋žŒ์ด ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,118 @@ +import React, { useEffect, useState } from "react"; +import { Link, useNavigate } from "react-router-dom"; +import { signin } from "../../api"; +import styled from "styled-components"; + +function SignIn() { + const navigate = useNavigate(); + const [auth, setAuth] = useState({ + email: "", + password: "", + }); + const [isValid, setValid] = useState(false); + const [errorMessage, setError] = useState(""); + + const handleAuth = (e) => { + const { name, value } = e.target; + setValid(true); + setAuth((prev) => ({ + ...prev, + [name]: value, + })); + }; + + const handleSignIn = async (e) => { + e.preventDefault(); + setValid(false); + if (!auth?.email || !auth.email.includes("@")) { + setError("์ด๋ฉ”์ผ ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + } else if (!auth?.password || auth.password.length < 8) { + setError("๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์ตœ์†Œ 8์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } else { + setError(""); + setValid(true); + const isSign = await signin(auth); + if (isSign) return navigate("/todo"); + } + }; + + useEffect(() => { + const token = localStorage.getItem("token"); + if (token) { + return navigate("/todo"); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + return ( + <Container> + <h1>SIGN IN</h1> + <form onSubmit={handleSignIn}> + <input + data-testid="email-input" + type="text" + name="email" + onChange={handleAuth} + /> + <input + data-testid="password-input" + type="password" + name="password" + onChange={handleAuth} + /> + <button data-testid="signin-button" disabled={!isValid}> + ๋กœ๊ทธ์ธ + </button> + </form> + {!isValid && <span>{errorMessage}</span>} + <StyledLink to="/signup">ํšŒ์›๊ฐ€์ž…</StyledLink> + </Container> + ); +} + +export default SignIn; + +const Container = styled.div` + display: flex; + flex-direction: column; + align-items: center; + form { + display: flex; + flex-direction: column; + margin-bottom: 1rem; + width: 300px; + input { + padding: 0.5rem; + font-size: 1rem; + border: 1px solid #ccc; + border-radius: 4px; + margin-bottom: 8px; + } + button { + padding: 0.5rem 1rem; + font-size: 1rem; + background-color: black; + color: #fff; + border: none; + border-radius: 4px; + cursor: pointer; + :disabled { + color: gray; + cursor: default; + } + } + } + span { + color: red; + } +`; + +const StyledLink = styled(Link)` + box-sizing: border-box; + display: block; + padding: 4px 8px; + margin: 0 auto; + text-align: center; + color: white; + text-decoration: none; +`;
JavaScript
placeholder๋กœ ํ˜•์‹์„ ์•Œ๋ ค์ฃผ๋ฉด ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์ด ์ข€ ๋” ์ข‹์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค!_! `@`๋ฅผ ํฌํ•จํ•ด์•ผ ํ•œ๋‹ค๊ฑฐ๋‚˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” 8์ž ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค๊ฑฐ๋‚˜..!
@@ -0,0 +1,43 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8" /> + <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> + <meta name="viewport" content="width=device-width, initial-scale=1" /> + <meta name="theme-color" content="#000000" /> + <meta + name="description" + content="Web site created using create-react-app" + /> + <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> + <!-- + manifest.json provides metadata used when your web app is installed on a + user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ + --> + <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> + <!-- + Notice the use of %PUBLIC_URL% in the tags above. + It will be replaced with the URL of the `public` folder during the build. + Only files inside the `public` folder can be referenced from the HTML. + + Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will + work correctly both with client-side routing and a non-root public URL. + Learn how to configure a non-root public URL by running `npm run build`. + --> + <title>React App</title> + </head> + <body> + <noscript>You need to enable JavaScript to run this app.</noscript> + <div id="root"></div> + <!-- + This HTML file is a template. + If you open it directly in the browser, you will see an empty page. + + You can add webfonts, meta tags, or analytics to this file. + The build step will place the bundled scripts into the <body> tag. + + To begin the development, run `npm start` or `yarn start`. + To create a production bundle, use `npm run build` or `yarn build`. + --> + </body> +</html>
Unknown
์•„์ด์ฝ˜์ด ํฌํ•จ๋œ ํƒœ๊ทธ๋Š” ํˆฌ๋‘์•ฑ๊ณผ ํฐ ์ƒ๊ด€์ด ์—†๋‹ค๊ณ ์ƒ๊ฐํ•˜๋Š”๋ฐ, ์ œ๊ฑฐํ•ด๋„ ์ข‹์ง€์•Š์„๊นŒ ์‹ถ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,34 @@ +import { Navigate, createBrowserRouter } from "react-router-dom"; +import App from "./App"; +import SignIn from "./Page/Auth/SignIn"; +import SignUp from "./Page/Auth/SignUp"; +import ToDoList from "./Page/ToDos/ToDoList"; + +const routes = [ + { + path: "", + element: <App />, + children: [ + { + path: "/", + element: <Navigate to="/signin" />, + }, + { + path: "/signin", + element: <SignIn />, + }, + { + path: "/signup", + element: <SignUp />, + }, + { + path: "/todo", + element: <ToDoList />, + }, + ], + }, +]; + +const router = createBrowserRouter(routes); + +export default router;
JavaScript
์˜ค ๋ผ์šฐํ„ฐ๋ฅผ ์ค‘์ฒฉํ•ด์„œ ์ž‘์„ฑํ• ์ˆ˜๋„ ์žˆ๊ตฐ์š”..!
@@ -0,0 +1,118 @@ +import React, { useEffect, useState } from "react"; +import { Link, useNavigate } from "react-router-dom"; +import { signin } from "../../api"; +import styled from "styled-components"; + +function SignIn() { + const navigate = useNavigate(); + const [auth, setAuth] = useState({ + email: "", + password: "", + }); + const [isValid, setValid] = useState(false); + const [errorMessage, setError] = useState(""); + + const handleAuth = (e) => { + const { name, value } = e.target; + setValid(true); + setAuth((prev) => ({ + ...prev, + [name]: value, + })); + }; + + const handleSignIn = async (e) => { + e.preventDefault(); + setValid(false); + if (!auth?.email || !auth.email.includes("@")) { + setError("์ด๋ฉ”์ผ ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + } else if (!auth?.password || auth.password.length < 8) { + setError("๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์ตœ์†Œ 8์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } else { + setError(""); + setValid(true); + const isSign = await signin(auth); + if (isSign) return navigate("/todo"); + } + }; + + useEffect(() => { + const token = localStorage.getItem("token"); + if (token) { + return navigate("/todo"); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + return ( + <Container> + <h1>SIGN IN</h1> + <form onSubmit={handleSignIn}> + <input + data-testid="email-input" + type="text" + name="email" + onChange={handleAuth} + /> + <input + data-testid="password-input" + type="password" + name="password" + onChange={handleAuth} + /> + <button data-testid="signin-button" disabled={!isValid}> + ๋กœ๊ทธ์ธ + </button> + </form> + {!isValid && <span>{errorMessage}</span>} + <StyledLink to="/signup">ํšŒ์›๊ฐ€์ž…</StyledLink> + </Container> + ); +} + +export default SignIn; + +const Container = styled.div` + display: flex; + flex-direction: column; + align-items: center; + form { + display: flex; + flex-direction: column; + margin-bottom: 1rem; + width: 300px; + input { + padding: 0.5rem; + font-size: 1rem; + border: 1px solid #ccc; + border-radius: 4px; + margin-bottom: 8px; + } + button { + padding: 0.5rem 1rem; + font-size: 1rem; + background-color: black; + color: #fff; + border: none; + border-radius: 4px; + cursor: pointer; + :disabled { + color: gray; + cursor: default; + } + } + } + span { + color: red; + } +`; + +const StyledLink = styled(Link)` + box-sizing: border-box; + display: block; + padding: 4px 8px; + margin: 0 auto; + text-align: center; + color: white; + text-decoration: none; +`;
JavaScript
์ด๋ฉ”์ผ, ๋น„๋ฐ€๋ฒˆํ˜ธ input ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ๊ณต์œ ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ž˜ ์ถ”์ƒํ™”๋œ ๊ธฐ๋Šฅ์ธ ๊ฒƒ ๊ฐ™์•„์š”! ์—‡ ๊ทผ๋ฐ ์ธํ’‹์ด ๋“ค์–ด์˜ค๋ฉด ํ•ญ์ƒ valid ์ƒํƒœ๊ฐ€ true๋กœ ์„ธํŒ…๋˜๋Š”๋ฐ ๊ฒ€์‚ฌํ•œ ํ›„์— valid์ฒดํฌํ•˜๋Š” ๊ฒŒ ๋กœ์ง์ƒ ๋งž์ง€์•Š๋‚˜์š”..?! ๐Ÿ‘€
@@ -0,0 +1,118 @@ +import React, { useEffect, useState } from "react"; +import { Link, useNavigate } from "react-router-dom"; +import { signin } from "../../api"; +import styled from "styled-components"; + +function SignIn() { + const navigate = useNavigate(); + const [auth, setAuth] = useState({ + email: "", + password: "", + }); + const [isValid, setValid] = useState(false); + const [errorMessage, setError] = useState(""); + + const handleAuth = (e) => { + const { name, value } = e.target; + setValid(true); + setAuth((prev) => ({ + ...prev, + [name]: value, + })); + }; + + const handleSignIn = async (e) => { + e.preventDefault(); + setValid(false); + if (!auth?.email || !auth.email.includes("@")) { + setError("์ด๋ฉ”์ผ ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + } else if (!auth?.password || auth.password.length < 8) { + setError("๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์ตœ์†Œ 8์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } else { + setError(""); + setValid(true); + const isSign = await signin(auth); + if (isSign) return navigate("/todo"); + } + }; + + useEffect(() => { + const token = localStorage.getItem("token"); + if (token) { + return navigate("/todo"); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + return ( + <Container> + <h1>SIGN IN</h1> + <form onSubmit={handleSignIn}> + <input + data-testid="email-input" + type="text" + name="email" + onChange={handleAuth} + /> + <input + data-testid="password-input" + type="password" + name="password" + onChange={handleAuth} + /> + <button data-testid="signin-button" disabled={!isValid}> + ๋กœ๊ทธ์ธ + </button> + </form> + {!isValid && <span>{errorMessage}</span>} + <StyledLink to="/signup">ํšŒ์›๊ฐ€์ž…</StyledLink> + </Container> + ); +} + +export default SignIn; + +const Container = styled.div` + display: flex; + flex-direction: column; + align-items: center; + form { + display: flex; + flex-direction: column; + margin-bottom: 1rem; + width: 300px; + input { + padding: 0.5rem; + font-size: 1rem; + border: 1px solid #ccc; + border-radius: 4px; + margin-bottom: 8px; + } + button { + padding: 0.5rem 1rem; + font-size: 1rem; + background-color: black; + color: #fff; + border: none; + border-radius: 4px; + cursor: pointer; + :disabled { + color: gray; + cursor: default; + } + } + } + span { + color: red; + } +`; + +const StyledLink = styled(Link)` + box-sizing: border-box; + display: block; + padding: 4px 8px; + margin: 0 auto; + text-align: center; + color: white; + text-decoration: none; +`;
JavaScript
๊ฐ™์€์–˜๊ธฐ๋ผ ๋‹ต๊ธ€ ๋‹ต๋‹ˆ๋‹ค! handleAuth์—์„œ๋Š” setValid(true)๋งŒ ํ•  ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ˜„์žฌ ์ƒํƒœ๊ฐ€ validํ•œ์ง€ ํŒ๋‹จํ•˜๋Š” ๋กœ์ง์ด ์ถ”๊ฐ€์ ์œผ๋กœ ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,118 @@ +import React, { useEffect, useState } from "react"; +import { Link, useNavigate } from "react-router-dom"; +import { signin } from "../../api"; +import styled from "styled-components"; + +function SignIn() { + const navigate = useNavigate(); + const [auth, setAuth] = useState({ + email: "", + password: "", + }); + const [isValid, setValid] = useState(false); + const [errorMessage, setError] = useState(""); + + const handleAuth = (e) => { + const { name, value } = e.target; + setValid(true); + setAuth((prev) => ({ + ...prev, + [name]: value, + })); + }; + + const handleSignIn = async (e) => { + e.preventDefault(); + setValid(false); + if (!auth?.email || !auth.email.includes("@")) { + setError("์ด๋ฉ”์ผ ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + } else if (!auth?.password || auth.password.length < 8) { + setError("๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์ตœ์†Œ 8์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } else { + setError(""); + setValid(true); + const isSign = await signin(auth); + if (isSign) return navigate("/todo"); + } + }; + + useEffect(() => { + const token = localStorage.getItem("token"); + if (token) { + return navigate("/todo"); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + return ( + <Container> + <h1>SIGN IN</h1> + <form onSubmit={handleSignIn}> + <input + data-testid="email-input" + type="text" + name="email" + onChange={handleAuth} + /> + <input + data-testid="password-input" + type="password" + name="password" + onChange={handleAuth} + /> + <button data-testid="signin-button" disabled={!isValid}> + ๋กœ๊ทธ์ธ + </button> + </form> + {!isValid && <span>{errorMessage}</span>} + <StyledLink to="/signup">ํšŒ์›๊ฐ€์ž…</StyledLink> + </Container> + ); +} + +export default SignIn; + +const Container = styled.div` + display: flex; + flex-direction: column; + align-items: center; + form { + display: flex; + flex-direction: column; + margin-bottom: 1rem; + width: 300px; + input { + padding: 0.5rem; + font-size: 1rem; + border: 1px solid #ccc; + border-radius: 4px; + margin-bottom: 8px; + } + button { + padding: 0.5rem 1rem; + font-size: 1rem; + background-color: black; + color: #fff; + border: none; + border-radius: 4px; + cursor: pointer; + :disabled { + color: gray; + cursor: default; + } + } + } + span { + color: red; + } +`; + +const StyledLink = styled(Link)` + box-sizing: border-box; + display: block; + padding: 4px 8px; + margin: 0 auto; + text-align: center; + color: white; + text-decoration: none; +`;
JavaScript
์œ„์—์„œ๋„ ๋งํ–ˆ๋“ฏ์ด ๊ณผ์ œ์˜ ๊ตฌํ˜„ ์‚ฌํ•ญ์€ `์ž…๋ ฅ๋œ ์ด๋ฉ”์ผ๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•œ๋‹ค๋ฉด button์— disabled ์†์„ฑ์„ ๋ถ€์—ฌํ•ด์ฃผ์„ธ์š”` ์ฆ‰ submit์ด์ „์— ์œ ํšจ์„ฑ๊ฒ€์‚ฌ๋ฅผ ์ฒดํฌ ํ•˜๊ณ  disabled ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌ ํ•ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. submit ์ œ์ถœ์‹œ์— ๊ฒ€์‚ฌํ•˜๋ฉด ์•ˆ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค(๊ตฌํ˜„์‚ฌํ•ญ๋Œ€๋กœ๋ผ๋ฉด)