code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,16 @@ +package racingcar.constants; + +public enum GameConstants { + CAR_NAME_LENGTH_MIN(1), + CAR_NAME_LENGTH_MAX(5); + + private final int number; + + GameConstants(int number) { + this.number = number; + } + + public int getNumber() { + return number; + } +}
Java
GameRule ๋„๋ฉ”์ธ์ด ๋งŒ๋“ค์–ด ์ ธ์„œ, GameConstants์™€ ์ค‘๋ณต๋œ ์˜๋ฏธ๋ฅผ ๋‹ด๋Š”๊ฑฐ ๊ฐ™์€๋ฐ ํ•ด๋‹น ํด๋ž˜์Šค์— ๋Œ€ํ•ด ๊ณ ๋ฏผ์ด ์ข€๋” ํ•„์š”ํ•ด๋ณด์—ฌ์šฉ
@@ -0,0 +1,30 @@ +package racingcar.domain; + +public class RacingCar { + private final CarName carName; + private int moveCount; + + public RacingCar(String name) { + this.carName = new CarName(name); + moveCount = 0; + } + + public int getMoveCount() { + return moveCount; + } + + public String getCarName() { + return carName.getName(); + } + + public void attemptToMove(GameRule gameRule) { + if (gameRule.canMove()) { + move(); + } + } + + private void move() { + moveCount++; + } + +}
Java
### ํ˜„์žฌ ์ˆ˜์ •์‚ฌํ•ญ - GameRule ๋„๋ฉ”์ธ์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ RandomNumber์™€ TrialCount ์ถ”๊ฐ€ - RandomNumber๋Š” ์ž๋™์ฐจ์˜ ์ „์ง„์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•  ๋•Œ๋งŒ ํ•„์š”ํ•œ๋ฐ, ์ „์ง„์—ฌ๋ถ€ ํŒ๋‹จ๋กœ์ง์ธ GameRule์— ์žˆ์œผ๋ฏ€๋กœ RandomNumber๋ฅผ GameRule์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ์ถ”๊ฐ€ํ•จ - GameRule ๊ฐ์ฒด๋ฅผ RacingCar์˜ ๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  Controller์—์„œ ์ƒ์„ฑํ•˜๋„๋ก ์ˆ˜์ • - Controller์—์„œ RacingCars์— GameRule๊ฐ์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ - GameRandomNumber์˜ getRandomNumber() ๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ ๋ฐ”๋กœ ๋žœ๋ค์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ˆ˜์ • - ๋žœ๋ค์ˆซ์ž๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด RandomNumber ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ , GameRule์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” RandomNumber ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๋žœ๋ค์ˆซ์ž ๊ฐ’๋งŒ ๋ฐ˜ํ™˜๋ฐ›๋„๋ก ํ•จ ### Issue 1 : GameRule ๋„๋ฉ”์ธ๊ณผ ๋‹ค๋ฅธ ๋„๋ฉ”์ธ ๊ฐ„์˜ ์˜์กด๊ด€๊ณ„ ์„ค์ •์˜ ์–ด๋ ค์›€ - RacingCars์˜ ๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ ๊ฐ ์ž๋™์ฐจ ๊ฐ์ฒด๋งˆ๋‹ค ์ „์ง„์—ฌ๋ถ€ํŒ๋‹จ์ด ํ•„์š”ํ•œ๋ฐ, GameRule ๊ฐ์ฒด๋ฅผ RacingCar์—๊ฒŒ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š๊ณ  ์™ธ๋ถ€์—์„œ ํŒ๋‹จ ํ›„ RacingCar๋ฅผ ์ „์ง„์‹œํ‚ฌ ๋ฐฉ๋ฒ•์„ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค..! ### Issue 2 : GameRandomNumber์—์„œ randomNumber๋ผ๋Š” ๋ฉค๋ฒ„๋ณ€์ˆ˜์˜ ํ•„์š”์„ฑ - getRandomNumber()์—์„œ ๋ฐ”๋กœ ๋žœ๋ค์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š”๋ฐ, ๊ทธ๋Ÿผ randomNumber ๋ฉค๋ฒ„๋ณ€์ˆ˜๊ฐ€ ์—†์–ด๋„ ๋˜๋Š” ๊ฒƒ์ผ๊นŒ์š”? ### Issue 3 : GameConstants ํด๋ž˜์Šค - ๊ฒŒ์ž„์ƒ์ˆ˜ 5๊ฐœ ์ค‘, 3๊ฐœ๋Š” ํ•œ ๊ณณ์—์„œ๋งŒ ํ˜ธ์ถœํ•˜๊ณ  ๋‚˜๋จธ์ง€ 2๊ฐœ๋Š” ๋‘ ๊ตฐ๋ฐ์—์„œ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์ด ์ƒ์ˆ˜๋“ค์„ ์–ด๋–ป๊ฒŒ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ํšจ๊ณผ์ ์ผ์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! ์•„๋ž˜๋Š” ์ œ๊ฐ€ ์ƒ๊ฐํ•ด ๋ณธ ๋ฐฉ๋ฒ•๋“ค์ž…๋‹ˆ๋‹ค. - ๋ชจ๋“  ์ƒ์ˆ˜๋ฅผ Enum ํด๋ž˜์Šค๋กœ ๋ชจ์•„์„œ ๊ด€๋ฆฌ - ํ•œ ๊ณณ์—์„œ ํ˜ธ์ถœํ•˜๋Š” ์ƒ์ˆ˜๋Š” ๊ฐ๊ฐ ์‚ฌ์šฉ๋˜๋Š” ๋„๋ฉ”์ธ์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ์„ ์–ธ, ๋‚˜๋จธ์ง€๋Š” Enumํด๋ž˜์Šค๋กœ ๊ด€๋ฆฌ - ๋ชจ๋“  ์ƒ์ˆ˜๋ฅผ ๊ฐ๊ฐ ์‚ฌ์šฉ๋˜๋Š” ๋„๋ฉ”์ธ์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ์„ ์–ธ - ์—ฌ๋Ÿฌ ๊ณณ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ƒ์ˆ˜์˜ ๊ฒฝ์šฐ, ํŠน์ • ๋„๋ฉ”์ธ์— ์ •์  ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•˜๊ณ  ๋‹ค๋ฅธ ๋„๋ฉ”์ธ์—์„œ ํ˜ธ์ถœ --- ๊ฐ ๋„๋ฉ”์ธ์˜ ์—ญํ• ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒƒ๊ณผ ๋„๋ฉ”์ธ ๊ฐ„ ์˜์กด๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์ด ์–ด๋ ต๊ณ  ๊ฐœ์ธ์ ์œผ๋กœ ์•ฝํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ „์ฒด์ ์œผ๋กœ ์ œ๊ฐ€ ํ˜ผ๋ž€์Šค๋Ÿฌ์šด ๋ถ€๋ถ„์ด ๋งŽ์•„์„œ ์ฝ”๋“œ๋Š” ๋‹ฌ๋ผ์ง„ ์ ์ด ๋งŽ์ด ์—†๊ณ , ์ด์Šˆ๋“ค ์œ„์— ์ ์–ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋ทฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค!
@@ -0,0 +1,24 @@ +package com.example.chatgptcodereviewtest.controller; + +import com.example.chatgptcodereviewtest.service.TestService2; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +/** + * ์„ค๋ช…: + * + * @author ์ตœํ˜„๋ฒ”(Jayce) / hb.choi@dreamus.io + * @since 2023/03/23 + */ +@RestController +@RequestMapping("/test2") +@RequiredArgsConstructor +public class TestController2 { + + private final TestService2 testService2; + + @GetMapping + public String getText(@RequestParam(value = "label", required = false) String label) { + return testService2.getUppercaseTestLabel(label); + } +} \ No newline at end of file
Java
์œ„ ์ฝ”๋“œ๋Š” `TestController2`๋ผ๋Š” REST API ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ๊ฐœ์„  ์ œ์•ˆ: 1. Javadoc(์ฃผ์„) ์ž‘์„ฑ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”์†Œ๋“œ์™€ ํด๋ž˜์Šค์˜ ์„ค๋ช…์„ ๊ตฌ์ฒด์ ์œผ๋กœ ๋‹ฌ์•„์ฃผ์–ด ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. ๋งˆ์ง€๋ง‰ ์ค„์— ๊ฐœํ–‰ ๋ฌธ์ž์—ด์ด ์—†์Šต๋‹ˆ๋‹ค. ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ˆ˜์ • ๋ฐ ํŽธ์ง‘ํ•  ๋•Œ ๊นƒ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ๊ฐœํ–‰ ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. 3. `@RequestParam` ์–ด๋…ธํ…Œ์ด์…˜์˜ required ์†์„ฑ๊ฐ’์€ false๋กœ ๋˜์–ด ์žˆ์–ด, ํ•„์ˆ˜๋กœ ๋“ค์–ด์˜ฌ ๊ฐ’์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ํ•ด๋‹น ํŒŒ๋ผ๋ฏธํ„ฐ ๋ณ€์ˆ˜์— ๋„ ์ฒดํฌ๋‚˜ ๋””ํดํŠธ๊ฐ’ ๋“ฑ์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฒ„๊ทธ ์œ„ํ—˜์ด ์—†์–ด ๋ณด์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,17 @@ +package com.example.chatgptcodereviewtest.service; + +import org.springframework.stereotype.Service; + +/** + * ์„ค๋ช…: + * + * @author ์ตœํ˜„๋ฒ”(Jayce) / hb.choi@dreamus.io + * @since 2023/03/23 + */ +@Service +public class TestService2 { + + public String getUppercaseTestLabel(String label) { + return label.toUpperCase(); + } +}
Java
์ด ์ฝ”๋“œ๋Š” ๋ด‰์‡„์ ์ธ ๋ฌธ์ œ๋‚˜ ๋ฒ„๊ทธ๊ฐ€ ์—†๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹จ์ˆœํ•˜๊ฒŒ ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, Spring์˜ Service ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ Bean์œผ๋กœ ๋“ฑ๋ก๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜๋ช…๊ณผ ๋ฉ”์„œ๋“œ๋ช…์˜ ์ง๊ด€์„ฑ ์ธก๋ฉด์—์„œ ๊ฐœ์„ ํ•  ์ ์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. 'label'์€ ๊ตฌ์ฒด์ ์ธ ์˜๋ฏธ๋ฅผ ์•Œ๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๋” ๊ตฌ์ฒด์ ์ธ ๋ช…์นญ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ํ–ฅ์ƒ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,35 @@ +package nextstep.subway.applicaion; + +import java.util.List; +import nextstep.subway.applicaion.dto.PathResponse; +import nextstep.subway.domain.Line; +import nextstep.subway.domain.LineRepository; +import nextstep.subway.domain.Path; +import nextstep.subway.domain.PathFinder; +import nextstep.subway.domain.Station; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class PathService { + private final LineRepository lineRepository; + private final StationService stationService; + + public PathService(LineRepository lineRepository, + StationService stationService) { + this.lineRepository = lineRepository; + this.stationService = stationService; + } + + public PathResponse searchPath(Long sourceId, Long targetId) { + Station source = stationService.findById(sourceId); + Station target = stationService.findById(targetId); + List<Line> lines = lineRepository.findAll(); + + PathFinder pathFinder = PathFinder.from(lines); + Path shortestPath = pathFinder.searchPath(source, target); + return PathResponse.from(shortestPath); + } + +}
Java
๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ธฐ์œ„ํ•ด lines๋ผ๋Š” ์ •๋ณด๊ฐ€ ์žˆ๊ณ , ์ด ์ •๋ณด๋ฅผ ์ด์šฉํ•ด ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š”๋ฐ ์ด ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ์ฑ…์ž„์„ PathFinder๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ๊ทธ ๊ฒฐ๊ณผ ์ •๋ณด๋Š” Path๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋„ค์š”! ๊ฐ์ฒด์ง€ํ–ฅ์—์„œ ๊ฐ์ฒด๋Š” ์ƒํƒœ์™€ ํ–‰์œ„๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ์š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ ์ƒํƒœ์ธ lines์™€ ํ–‰์œ„์ธ PathFinder๋ฅผ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ ๋„์ถœํ•ด๋ณผ ์ˆ˜๋Š” ์—†์„๊นŒ์š”? ๋งŒ์•ฝ์— ์ด๋ ‡๊ฒŒ ๋ฆฌํŒฉํ„ฐ๋ง ํ•  ๊ฒฝ์šฐ ๊ธฐ์กด์— ๋งŒ๋“ค์–ด ๋‘์—ˆ๋˜ ํ…Œ์ŠคํŠธ๋ฅผ ํ™œ์šฉํ•ด์„œ ํ…Œ์ŠคํŠธ์˜ ๊ฒ€์ฆ์ด๋ผ๋Š” ๋ณดํ˜ธ ์†์— ๋ฆฌํŒฉํ„ฐ๋ง์„ ํ•ด๋ณด์‹œ๋Š” ๊ฒƒ์„ ๊ถŒํ•ด๋“œ๋ฆฝ๋‹ˆ๋‹ค!
@@ -63,19 +63,19 @@ public void update(Station newUpStation, int minusDistance) { } public Section merge(Section section) { - if (!isDownStation(section.upStation)) { + if (!hasDownStationAs(section.upStation)) { throw new IllegalArgumentException("ํ•ฉ์น˜๋ ค๋Š” ๊ตฌ๊ฐ„์˜ ์ƒํ–‰์—ญ์ด ํ•˜ํ–‰์—ญ๊ณผ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค."); } return Section.of(line, upStation, section.downStation, distance + section.distance); } - public boolean isUpStation(Station station) { + public boolean hasUpStationAs(Station station) { return upStation.equals(station); } - public boolean isDownStation(Station station) { + public boolean hasDownStationAs(Station station) { return downStation.equals(station); }
Java
๋ณ€๊ฒฝ๋œ ๋„ค์ด๋ฐ์ด ์˜๋ฏธ๋ฅผ ์กฐ๊ธˆ ๋” ์ž˜ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ‘
@@ -1,6 +1,7 @@ package nextstep.subway.domain; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; @@ -34,23 +35,30 @@ public void add(Section section) { public void remove(Station station) { validationRemoveStation(station); - Optional<Section> firstSection = sections.stream() - .filter(s -> s.isDownStation(station)) - .findAny(); - - Optional<Section> secondSection = sections.stream() - .filter(s -> s.isUpStation(station)) - .findAny(); + Optional<Section> sectionHasUpStation = findSectionHasUpStationAs(station); + Optional<Section> sectionHasDownStation = findSectionHasDownStationAs(station); - firstSection.ifPresent(section -> sections.remove(section)); - secondSection.ifPresent(section -> sections.remove(section)); + sectionHasUpStation.ifPresent(section -> sections.remove(section)); + sectionHasDownStation.ifPresent(section -> sections.remove(section)); - if (firstSection.isPresent() && secondSection.isPresent()) { - mergeExistingSections(firstSection.get(), secondSection.get()); + if (sectionHasUpStation.isPresent() && sectionHasDownStation.isPresent()) { + mergeExistingSections(sectionHasUpStation.get(), sectionHasDownStation.get()); } } + private Optional<Section> findSectionHasDownStationAs(Station station) { + return sections.stream() + .filter(s -> s.hasUpStationAs(station)) + .findAny(); + } + + private Optional<Section> findSectionHasUpStationAs(Station station) { + return sections.stream() + .filter(s -> s.hasDownStationAs(station)) + .findAny(); + } + private List<Station> getStations() { List<Station> stations = new ArrayList<>(); stations.add(sections.get(0).getUpStation()); @@ -152,4 +160,7 @@ private void mergeExistingSections(Section firstSection, Section secondSection) sections.add(firstSection.merge(secondSection)); } + public List<Section> getSectionList() { + return Collections.unmodifiableList(sections); + } }
Java
์ ์ ˆํ•œ ์‚ฌ์ด์ฆˆ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ๋‚˜๋ˆ„์–ด ์ฃผ์…จ๋„ค์š” ๐Ÿ‘
@@ -0,0 +1,48 @@ +package nextstep.subway.unit; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import nextstep.subway.domain.Line; +import nextstep.subway.domain.Station; +import org.springframework.test.util.ReflectionTestUtils; + +public class PathFixture { + /** (10) + * ๊ต๋Œ€์—ญ --- *2ํ˜ธ์„ * --- ๊ฐ•๋‚จ์—ญ + * | | + * *3ํ˜ธ์„ * (2) *์‹ ๋ถ„๋‹น์„ * (5) + * | | + * ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ --- *3ํ˜ธ์„ * --- ์–‘์žฌ + (3) **/ + + + public static Station ๊ฐ•๋‚จ์—ญ = new Station("๊ฐ•๋‚จ์—ญ"); + public static Station ๊ต๋Œ€์—ญ = new Station("๊ต๋Œ€์—ญ"); + public static Station ์–‘์žฌ์—ญ = new Station("์–‘์žฌ์—ญ"); + public static Station ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ = new Station("๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ"); + + public static int ๊ต๋Œ€์—ญ_๊ฐ•๋‚จ์—ญ_๊ฑฐ๋ฆฌ = 10; + public static int ๊ฐ•๋‚จ์—ญ_์–‘์žฌ์—ญ_๊ฑฐ๋ฆฌ = 5; + public static int ๊ต๋Œ€์—ญ_๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ_๊ฑฐ๋ฆฌ = 2; + public static int ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ_์–‘์žฌ์—ญ_๊ฑฐ๋ฆฌ = 3; + + public static Line ์ดํ˜ธ์„  = Line.of("2ํ˜ธ์„ ", "bg-green-600", ๊ต๋Œ€์—ญ, ๊ฐ•๋‚จ์—ญ, ๊ต๋Œ€์—ญ_๊ฐ•๋‚จ์—ญ_๊ฑฐ๋ฆฌ); + public static Line ์‚ผํ˜ธ์„  = Line.of("3ํ˜ธ์„ ", "bg-orange-500", ๊ต๋Œ€์—ญ, ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, ๊ต๋Œ€์—ญ_๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ_๊ฑฐ๋ฆฌ); + public static Line ์‹ ๋ถ„๋‹น์„  = Line.of("์‹ ๋ถ„๋‹น์„ ", "bg-red-500", ๊ฐ•๋‚จ์—ญ, ์–‘์žฌ์—ญ, ๊ฐ•๋‚จ์—ญ_์–‘์žฌ์—ญ_๊ฑฐ๋ฆฌ); + + public static List<Line> ๋…ธ์„ _๋ชฉ๋ก = new ArrayList<>(); + + static { + ์‚ผํ˜ธ์„ .addSection(๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, ์–‘์žฌ์—ญ, ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ_์–‘์žฌ์—ญ_๊ฑฐ๋ฆฌ); + + ReflectionTestUtils.setField(๊ฐ•๋‚จ์—ญ, "id", 1L); + ReflectionTestUtils.setField(๊ต๋Œ€์—ญ, "id", 2L); + ReflectionTestUtils.setField(์–‘์žฌ์—ญ, "id", 3L); + ReflectionTestUtils.setField(๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, "id", 4L); + + ๋…ธ์„ _๋ชฉ๋ก.addAll(Arrays.asList(์ดํ˜ธ์„ , ์‚ผํ˜ธ์„ , ์‹ ๋ถ„๋‹น์„ )); + } + + +}
Java
ํ”ฝ์Šค์ณ ๋ถ„๋ฆฌ๋ฅผ ํ†ตํ•ด ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๋‚ด ํšจ๊ณผ์ ์ธ ์ค‘๋ณต ์ œ๊ฑฐ ๐Ÿ‘
@@ -0,0 +1,120 @@ +package nextstep.subway.acceptance; + +import static nextstep.subway.acceptance.LineSteps.์ง€ํ•˜์ฒ _๋…ธ์„ _์ƒ์„ฑ_์š”์ฒญ; +import static nextstep.subway.acceptance.LineSteps.์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ _๊ตฌ๊ฐ„_์ƒ์„ฑ_์š”์ฒญ; +import static nextstep.subway.acceptance.PathSteps.๊ฒฝ๋กœ_์กฐํšŒ_์š”์ฒญ; +import static nextstep.subway.acceptance.StationSteps.์ง€ํ•˜์ฒ ์—ญ_์ƒ์„ฑ_์š”์ฒญ; +import static org.assertj.core.api.Assertions.assertThat; + +import io.restassured.response.ExtractableResponse; +import io.restassured.response.Response; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpStatus; + +@DisplayName("์ง€ํ•˜์ฒ  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰") +public class PathAcceptanceTest extends AcceptanceTest { + private Long ๊ต๋Œ€์—ญ; + private Long ๊ฐ•๋‚จ์—ญ; + private Long ์–‘์žฌ์—ญ; + private Long ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ; + private Long ์ฒญ๋Ÿ‰๋ฆฌ์—ญ; + private Long ํšŒ๊ธฐ์—ญ; + + private Long ์ผํ˜ธ์„ ; + private Long ์ดํ˜ธ์„ ; + private Long ์‹ ๋ถ„๋‹น์„ ; + private Long ์‚ผํ˜ธ์„ ; + + private int ๊ต๋Œ€์—ญ_๊ฐ•๋‚จ์—ญ_๊ฑฐ๋ฆฌ = 10; + private int ๊ฐ•๋‚จ์—ญ_์–‘์žฌ์—ญ_๊ฑฐ๋ฆฌ = 5; + private int ๊ต๋Œ€์—ญ_๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ_๊ฑฐ๋ฆฌ = 2; + private int ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ_์–‘์žฌ์—ญ_๊ฑฐ๋ฆฌ = 3; + private int ์ฒญ๋Ÿ‰๋ฆฌ์—ญ_ํšŒ๊ธฐ์—ญ_๊ฑฐ๋ฆฌ = 7; + + /** (10) + * ๊ต๋Œ€์—ญ --- *2ํ˜ธ์„ * --- ๊ฐ•๋‚จ์—ญ + * | | + * *3ํ˜ธ์„ * (2) *์‹ ๋ถ„๋‹น์„ * (5) + * | | + * ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ --- *3ํ˜ธ์„ * --- ์–‘์žฌ + (3) **/ + + @BeforeEach + public void setUp() { + super.setUp(); + + ๊ต๋Œ€์—ญ = ์ง€ํ•˜์ฒ ์—ญ_์ƒ์„ฑ_์š”์ฒญ("๊ต๋Œ€์—ญ").jsonPath().getLong("id"); + ๊ฐ•๋‚จ์—ญ = ์ง€ํ•˜์ฒ ์—ญ_์ƒ์„ฑ_์š”์ฒญ("๊ฐ•๋‚จ์—ญ").jsonPath().getLong("id"); + ์–‘์žฌ์—ญ = ์ง€ํ•˜์ฒ ์—ญ_์ƒ์„ฑ_์š”์ฒญ("์–‘์žฌ์—ญ").jsonPath().getLong("id"); + ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ = ์ง€ํ•˜์ฒ ์—ญ_์ƒ์„ฑ_์š”์ฒญ("๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ").jsonPath().getLong("id"); + ์ฒญ๋Ÿ‰๋ฆฌ์—ญ = ์ง€ํ•˜์ฒ ์—ญ_์ƒ์„ฑ_์š”์ฒญ("์ฒญ๋Ÿ‰๋ฆฌ์—ญ").jsonPath().getLong("id"); + ํšŒ๊ธฐ์—ญ = ์ง€ํ•˜์ฒ ์—ญ_์ƒ์„ฑ_์š”์ฒญ("ํšŒ๊ธฐ์—ญ").jsonPath().getLong("id"); + + ์ผํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _์ƒ์„ฑ_์š”์ฒญ("1ํ˜ธ์„ ", "blue", ์ฒญ๋Ÿ‰๋ฆฌ์—ญ, ํšŒ๊ธฐ์—ญ, ์ฒญ๋Ÿ‰๋ฆฌ์—ญ_ํšŒ๊ธฐ์—ญ_๊ฑฐ๋ฆฌ).jsonPath().getLong("id"); + ์ดํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _์ƒ์„ฑ_์š”์ฒญ("2ํ˜ธ์„ ", "green", ๊ต๋Œ€์—ญ, ๊ฐ•๋‚จ์—ญ, ๊ต๋Œ€์—ญ_๊ฐ•๋‚จ์—ญ_๊ฑฐ๋ฆฌ).jsonPath().getLong("id"); + ์‹ ๋ถ„๋‹น์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _์ƒ์„ฑ_์š”์ฒญ("์‹ ๋ถ„๋‹น์„ ", "red", ๊ฐ•๋‚จ์—ญ, ์–‘์žฌ์—ญ, ๊ฐ•๋‚จ์—ญ_์–‘์žฌ์—ญ_๊ฑฐ๋ฆฌ).jsonPath().getLong("id"); + ์‚ผํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _์ƒ์„ฑ_์š”์ฒญ("3ํ˜ธ์„ ", "orange", ๊ต๋Œ€์—ญ, ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, ๊ต๋Œ€์—ญ_๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ_๊ฑฐ๋ฆฌ).jsonPath().getLong("id"); + + ์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ _๊ตฌ๊ฐ„_์ƒ์„ฑ_์š”์ฒญ(์‚ผํ˜ธ์„ , createSectionCreateParams(๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, ์–‘์žฌ์—ญ, ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ_์–‘์žฌ์—ญ_๊ฑฐ๋ฆฌ)); + } + + /** + * Given ์ง€ํ•˜์ฒ  ๋…ธ์„  (2ํ˜ธ์„ , 3ํ˜ธ์„ , ์‹ ๋ถ„๋‹น์„ ) ์„ ์ƒ์„ฑํ•˜๊ณ  ์—ญ, ๊ตฌ๊ฐ„์„ ์ƒ์„ฑํ•œ๋‹ค. + * When ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ์—ญ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์กฐํšŒํ•˜๋ฉด + * Then ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์กฐํšŒ์— ์„ฑ๊ณตํ•œ๋‹ค. + */ + @DisplayName("์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์กฐํšŒํ•˜๊ธฐ") + @Test + void searchShortestPath() { + // when + ExtractableResponse<Response> response = ๊ฒฝ๋กœ_์กฐํšŒ_์š”์ฒญ(๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, ๊ฐ•๋‚จ์—ญ); + + // then + assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); + assertThat(response.jsonPath().getList("stations.id", Long.class)).containsExactly(๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, ์–‘์žฌ์—ญ, ๊ฐ•๋‚จ์—ญ); + assertThat(response.jsonPath().getInt("distance")).isEqualTo(๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ_์–‘์žฌ์—ญ_๊ฑฐ๋ฆฌ + ๊ฐ•๋‚จ์—ญ_์–‘์žฌ์—ญ_๊ฑฐ๋ฆฌ); + } + + /** + * Given ์ง€ํ•˜์ฒ  ๋…ธ์„  (2ํ˜ธ์„ , 3ํ˜ธ์„ , ์‹ ๋ถ„๋‹น์„ ) ์„ ์ƒ์„ฑํ•˜๊ณ  ์—ญ, ๊ตฌ๊ฐ„์„ ์ƒ์„ฑํ•œ๋‹ค. + * ๊ธฐ์กด ๋…ธ์„ ๊ณผ ์—ฐ๊ฒฐ๋˜์ง€ ์•Š๋Š” ์ƒˆ๋กœ์šด ๋…ธ์„  (1ํ˜ธ์„ )์„ ์ƒ์„ฑํ•œ๋‹ค. + * When ์—ฐ๊ฒฐ ๋˜์ง€ ์•Š์€ ๋‘ ์—ญ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์กฐํšŒ๋ฅผ ์š”์ฒญ ํ•˜๋ฉด + * Then ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์กฐํšŒ์— ์‹คํŒจํ•œ๋‹ค. + */ + @DisplayName("์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์กฐํšŒํ•˜๊ธฐ - ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์€ ์—ญ์„ ์กฐํšŒ ํ•  ๊ฒฝ์šฐ") + @Test + void searchShortestPathDoesNotExistPath() { + // when + ExtractableResponse<Response> response = ๊ฒฝ๋กœ_์กฐํšŒ_์š”์ฒญ(๊ฐ•๋‚จ์—ญ, ์ฒญ๋Ÿ‰๋ฆฌ์—ญ); + + // then + assertThat(response.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());; + } + + /** + * Given ์ง€ํ•˜์ฒ  ๋…ธ์„  (2ํ˜ธ์„ , 3ํ˜ธ์„ , ์‹ ๋ถ„๋‹น์„ ) ์„ ์ƒ์„ฑํ•˜๊ณ  ์—ญ, ๊ตฌ๊ฐ„์„ ์ƒ์„ฑํ•œ๋‹ค. + * When ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ์ด ๋™์ผํ•œ๋ฐ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์กฐํšŒ๋ฅผ ์š”์ฒญํ•˜๋ฉด + * Then ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์กฐํšŒ์— ์‹คํŒจํ•œ๋‹ค. + */ + @DisplayName("์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์กฐํšŒํ•˜๊ธฐ - ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ์ด ๋™์ผํ•œ ๊ฒฝ์šฐ") + @Test + void searchShortestPathSourceEqualsTarget() { + // when + ExtractableResponse<Response> response = ๊ฒฝ๋กœ_์กฐํšŒ_์š”์ฒญ(๊ฐ•๋‚จ์—ญ, ๊ฐ•๋‚จ์—ญ); + + // then + assertThat(response.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());; + } + + private Map<String, String> createSectionCreateParams(Long upStationId, Long downStationId, int distance) { + Map<String, String> params = new HashMap<>(); + params.put("upStationId", upStationId + ""); + params.put("downStationId", downStationId + ""); + params.put("distance", distance + ""); + return params; + } +}
Java
๋‹จ์œ„ ํ…Œ์ŠคํŠธ์˜ ํ”ฝ์Šค์ณ ๊ด€๋ฆฌ ๋ฐฉ๋ฒ•์ฒ˜๋Ÿผ ํ•ด๋‹น ๊ฐ์ฒด๋“ค์˜ ์žฌ์‚ฌ์šฉ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ์šฉ ํ”ฝ์Šค์ณ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ๋„ ์žˆ๊ฒ ๋„ค์š”!
@@ -0,0 +1,87 @@ +const express = require('express') +const http = require('http'); +const path = require('path'); + +const bodyParser = require('body-parser'); +const static = require('serve-static'); +const multer = require("multer") + +const app = express(); + +app.set('port', process.env.PORT || 3000); + +app.use(bodyParser.urlencoded({ extended: false })) +app.use(bodyParser.json()) + +app.use('/public', static(path.join(__dirname, 'public'))); +app.use('/uploads', static(path.join(__dirname, 'uploads'))) + +const upload = multer({ + storage: multer.diskStorage({ + destination: function (req, file, cb) { + cb(null, './uploads/'); + }, + filename: function (req, file, cb) { + cb(null, file.originalname); + } + }), +}).single('userfile'); + +app.post("/save", function (req, res) { + try { + upload(req, res, function (err) { + var name = req.body.name; + var date = req.body.date; + var content = req.body.content; + var filename = req.file.filename; + console.log(filename); + res.writeHead(200, { 'Content-Type': 'text/html;charset=utf8' }); + res.write(` + <head> + <meta charset="UTF-8"> + <link rel="stylesheet" href="/public/stylesheet/momo.css"> + <title>Document</title> + </head> + <form method="GET" action="/" id="re-form"> + <table class="header-table"> + <thead> + <th class="table-head">๋‚˜์˜ ๋ฉ”๋ชจ</th> + </thead> + <tbody class="table-body"> + <tr> + <td>${name},${date},${content}๋ฉ”๋ชจ๊ฐ€ ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.</td> + </tr> + <tr> + <td colspan="2" class="upload-display"> + <div class="upload-thumb-wrap"><img class="upload-thumb" src="/uploads/${filename}"></div> + </td> + </tr> + </tbody> + </table> + <table class="footer-table"> + <tbody> + <tr><td id="re-layor"><button id="redirect" form = "re-form" type="submit" >๋‹ค์‹œ์ž‘์„ฑ</button></td></tr> + </tbody> + </table> + </form> + `); + res.end(); + }); + + } catch (err) { + console.dir(err.stack); + + res.writeHead(400, { 'Content-Type': 'text/html;charset=utf8' }); + res.write('<div><p>๋ฉ”๋ชจ ์ €์žฅ ์‹œ ์—๋Ÿฌ ๋ฐœ์ƒ</p></div>'); + res.end(); + } + +}); + +app.get('/', function (req, res) { + res.sendFile(__dirname + "/view/memo/memo.html"); +}); + +http.createServer(app).listen(app.get('port'), function () { + console.log("์„œ๋ฒ„์‹œ์ž‘"); +}); \ No newline at end of file
JavaScript
String์ด ์•„๋‹Œ html์ด๋‚˜ ejs ํŒŒ์ผ๋กœ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. (Feat. ์ฟ ๋งˆ์จฉ) save.ejs๋กœ ๋ถ„๋ฆฌํ•ด ์ฃผ์„ธ์š”.
@@ -0,0 +1,87 @@ +const express = require('express') +const http = require('http'); +const path = require('path'); + +const bodyParser = require('body-parser'); +const static = require('serve-static'); +const multer = require("multer") + +const app = express(); + +app.set('port', process.env.PORT || 3000); + +app.use(bodyParser.urlencoded({ extended: false })) +app.use(bodyParser.json()) + +app.use('/public', static(path.join(__dirname, 'public'))); +app.use('/uploads', static(path.join(__dirname, 'uploads'))) + +const upload = multer({ + storage: multer.diskStorage({ + destination: function (req, file, cb) { + cb(null, './uploads/'); + }, + filename: function (req, file, cb) { + cb(null, file.originalname); + } + }), +}).single('userfile'); + +app.post("/save", function (req, res) { + try { + upload(req, res, function (err) { + var name = req.body.name; + var date = req.body.date; + var content = req.body.content; + var filename = req.file.filename; + console.log(filename); + res.writeHead(200, { 'Content-Type': 'text/html;charset=utf8' }); + res.write(` + <head> + <meta charset="UTF-8"> + <link rel="stylesheet" href="/public/stylesheet/momo.css"> + <title>Document</title> + </head> + <form method="GET" action="/" id="re-form"> + <table class="header-table"> + <thead> + <th class="table-head">๋‚˜์˜ ๋ฉ”๋ชจ</th> + </thead> + <tbody class="table-body"> + <tr> + <td>${name},${date},${content}๋ฉ”๋ชจ๊ฐ€ ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.</td> + </tr> + <tr> + <td colspan="2" class="upload-display"> + <div class="upload-thumb-wrap"><img class="upload-thumb" src="/uploads/${filename}"></div> + </td> + </tr> + </tbody> + </table> + <table class="footer-table"> + <tbody> + <tr><td id="re-layor"><button id="redirect" form = "re-form" type="submit" >๋‹ค์‹œ์ž‘์„ฑ</button></td></tr> + </tbody> + </table> + </form> + `); + res.end(); + }); + + } catch (err) { + console.dir(err.stack); + + res.writeHead(400, { 'Content-Type': 'text/html;charset=utf8' }); + res.write('<div><p>๋ฉ”๋ชจ ์ €์žฅ ์‹œ ์—๋Ÿฌ ๋ฐœ์ƒ</p></div>'); + res.end(); + } + +}); + +app.get('/', function (req, res) { + res.sendFile(__dirname + "/view/memo/memo.html"); +}); + +http.createServer(app).listen(app.get('port'), function () { + console.log("์„œ๋ฒ„์‹œ์ž‘"); +}); \ No newline at end of file
JavaScript
๋นˆ ์ค„๊ณผ ๋“ค์—ฌ์“ฐ๊ธฐ ์ •๋ฆฌํ•ด ์ฃผ์„ธ์š”.
@@ -1,7 +1,10 @@ package christmas; +import christmas.controller.MainController; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + MainController mainController = new MainController(); + mainController.run(); } }
Java
๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ new ์˜ˆ์•ฝ์–ด๋ณด๋‹ค ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์‹œ๋ฉด ์–ด๋–จ๊นŒ์š”. ๊ฐ€๋…์„ฑ์ด๋‚˜ ๊ฐ์ฒด ์ƒ์„ฑ ์ˆ˜ ์ œํ•œ ๊ฐ™์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ด์ ์ด ์žˆ์–ด์š”. https://tecoble.techcourse.co.kr/post/2020-05-26-static-factory-method/
@@ -0,0 +1,47 @@ +package christmas.controller; + +import java.util.Map; + +import christmas.view.*; +import christmas.model.*; + +public class MainController { + private OrderMenu orderMenu; + + public MainController() { + orderMenu = new OrderMenu(); + } + + public void run() { + orderMenu.setOrderDate(InputView.readDate()); + setReadOrder(); + + OutputView.printPreview(orderMenu.getOrderDate()); + OutputView.printOrder(orderMenu.getUserOrder()); + int totalPrice = orderMenu.getTotalPrice(); + OutputView.printTotalPrice(totalPrice); + OutputView.printPresent(totalPrice); + benefitControl(totalPrice); + } + + private void benefitControl(int totalPrice) { + Benefit benefit = new Benefit(orderMenu.getOrderDate(), orderMenu.getCategoryCount(), totalPrice); + OutputView.printBenefits(benefit.getBenefits()); + + int totalBenefitPrice = benefit.getTotalBenefitPrice(); + OutputView.printTotalBenefitPrice(totalBenefitPrice); + OutputView.printTotalPayment(totalPrice - totalBenefitPrice + benefit.getPresentPrice()); + OutputView.printEventBadge(totalBenefitPrice); + } + + private void setReadOrder() { + Map<String, Integer> orderMenu; + try { + orderMenu = InputView.readOrder(); + this.orderMenu.setUserOrder(orderMenu); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + setReadOrder(); + } + } +}
Java
OutputView๋‚˜ InputView์˜ ์ž…์ถœ๋ ฅ ๊ธฐ๋Šฅ๋“ค์„ ๋ชจ๋‘ ์Šคํƒœํ‹ฑ ๋ฉ”์„œ๋“œ๋กœ ๊ตฌํ˜„ํ•˜์…จ๋„ค์š”. ๋ณดํ†ต์€ ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ ํ›„์— ์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ ์Šคํƒœํ‹ฑ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”?
@@ -0,0 +1,97 @@ +package christmas.util; + +import java.util.Map; +import java.util.List; + +public class Validator { + private final static char MIN_DIGIT = '0'; + private final static char MAX_DIGIT = '9'; + private final static char CHAR_HYPHEN = '-'; + private final static char CHAR_COMMA = ','; + private final static int MIN_DAY = 1; + private final static int MAX_DAY = 31; + private final static int IS_TWO = 2; + + public static void validateDate(String input) { + if (input.isEmpty()) { + throw new IllegalArgumentException(ErrorMessage.DATE_ERROR.get()); + } + for (int i = Constant.ZERO.get(); i < input.length(); ++i) { + if (input.charAt(i) < MIN_DIGIT || input.charAt(i) > MAX_DIGIT) { + throw new IllegalArgumentException(ErrorMessage.DATE_ERROR.get()); + } + } + int date = Integer.parseInt(input); + if (date < MIN_DAY || date > MAX_DAY) { + throw new IllegalArgumentException(ErrorMessage.DATE_ERROR.get()); + } + } + + public static void validateOrder(String input) { + // splitํ•  ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰์˜ ๋นˆ ๋ฌธ์ž์—ด์„ ์‚ฌ๋ผ์ง€๋ฏ€๋กœ ๋ณ„๋„๋กœ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ + if (input.isEmpty() || input.charAt(input.length() - Constant.ONE.get()) == CHAR_COMMA) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + String[] splittedComma = input.split(Message.COMMA.get()); + if (splittedComma.length == Constant.ZERO.get()) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + for (String string : splittedComma) { + if (validateFormat(string)) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + } + } + + public static boolean validateMenu(Map<String, Integer> userOrder, List<Map<String, Integer>> menu) { + int[] countCategory = new int[Constant.CATEGORY_COUNT.get()]; + for (Map.Entry<String, Integer> entry : userOrder.entrySet()) { + if (entry.getValue() <= Constant.ZERO.get()) { + return true; + } + int category = Utility.getCategory(entry.getKey(), menu); + if (category == Constant.NOT_IN_MENU.get()) { + return true; + } + countCategory[category] += entry.getValue(); + } + if (validateCount(countCategory)) { + return true; + } + return false; + } + + private static boolean validateCount(int[] countCategory) { + // ์Œ๋ฃŒ๋งŒ ์‹œํ‚จ ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ + if (countCategory[Constant.EPPETIZER.get()] == Constant.ZERO.get() + && countCategory[Constant.MAINMENU.get()] == Constant.ZERO.get() + && countCategory[Constant.DESSERT.get()] == Constant.ZERO.get()) { + return true; + } + int totalCount = Constant.ZERO.get(); + for (int count : countCategory) { + totalCount += count; + } + if (totalCount > Constant.MAX_MENU_COUNT.get()) { + return true; + } + return false; + } + + private static boolean validateFormat(String input) { + if (input.isEmpty() || input.charAt(input.length() - Constant.ONE.get()) == CHAR_HYPHEN) { + return true; + } + String[] splittedHyphen = input.split(Message.HYPHEN.get()); + if (splittedHyphen.length != IS_TWO) { + return true; + } + for (int i = Constant.ZERO.get(); i < splittedHyphen[Constant.VALUE_INDEX.get()].length(); ++i) { + if (splittedHyphen[Constant.VALUE_INDEX.get()].charAt(i) < MIN_DIGIT + || splittedHyphen[Constant.VALUE_INDEX.get()].charAt(i) > MAX_DIGIT) { + return true; + } + } + return false; + } +}
Java
์ €๋„ ์ฑ™๊ธฐ์ง€ ๋ชปํ•œ ๋ถ€๋ถ„์ด๊ธดํ•ฉ๋‹ˆ๋‹ค๋งŒ ํ•ด๋‹น ๋‚ด์šฉ ๊ณต์œ ํ•ด๋“œ๋ฆฌ์ž๋ฉด, '์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธ'๊ณผ ๊ฐ™์€ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋Š” ๋ฌธ์ž์—ด ํŒŒ์‹ฑ ์˜ˆ์™ธ๊ฐ€ ์•„๋‹Œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์˜ˆ์™ธ์ฒ˜๋ฆฌ์ด๋ฏ€๋กœ ๊ฐ์ฒด์ƒ์„ฑ์—์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋ฉด ์—ญํ• ๋ถ„๋ฆฌ ์›์น™์— ํ›จ์”ฌ ๋” ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค~!
@@ -1,7 +1,10 @@ package christmas; +import christmas.controller.MainController; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + MainController mainController = new MainController(); + mainController.run(); } }
Java
์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ๋˜ ๊ฐœ๋…์ธ๋ฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋ฆฌํŒฉํ† ๋งํ•  ๋•Œ ์ ์šฉํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š”
@@ -0,0 +1,39 @@ +package christmas.util; + +public enum Constant { + NOT_IN_MENU(-1), + EPPETIZER(0), + MAINMENU(1), + DESSERT(2), + BEVERAGE(3), + CATEGORY_COUNT(4), + ZERO(0), + ONE(1), + THOUSAND(1000), + HUNDRED(100), + CHRISTMAS_DAY(25), + MIN_BENEFIT_PRICE(10000), + PRESENT_COST(120000), + CHAMPAIGN_PRICE(25000), + WEEKDAYS(7), + FRIDAY(1), + SATURDAY(2), + SUNDAY(3), + EVENT_PRICE(2023), + SANTA(20000), + TREE(10000), + STAR(5000), + MAX_MENU_COUNT(20), + KEY_INDEX(0), + VALUE_INDEX(1); + + private final int value; + + Constant(int value) { + this.value = value; + } + + public int get() { + return this.value; + } +}
Java
Enum์„ ๋ฉ”๋‰ด์ •๋ณด ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์šฉ์ฒ˜๊ฐ€ ์ž˜ ๋งž์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฉ”๋‰ด์— ํ•ด๋‹นํ•˜๋Š” ํ•œ๊ธ€์ด๋ฆ„(String)๊ณผ ๊ฐ€๊ฒฉ(Integer)๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์ง€์š”.
@@ -0,0 +1,68 @@ +package christmas.model; + +import christmas.util.Constant; +import christmas.util.ErrorMessage; +import christmas.util.Utility; +import christmas.util.Validator; + +import java.util.ArrayList; +import java.util.Map; +import java.util.List; + +public class OrderMenu { + private final List<Map<String, Integer>> menu; + + private int orderDate; + private Map<String, Integer> userOrder; + + public OrderMenu() { + menu = new ArrayList<>(List.of( + // EPPETIZER 0 + Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "ํƒ€ํŒŒ์Šค", 5500, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + // MAINMENU 1 + Map.of("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "๋ฐ”๋น„ํ๋ฆฝ", 54000, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + // DESSERT 2 + Map.of("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "์•„์ด์Šคํฌ๋ฆผ", 5000), + // BEVERAGE 3 + Map.of("์ œ๋กœ์ฝœ๋ผ", 3000, "๋ ˆ๋“œ์™€์ธ", 60000, "์ƒดํŽ˜์ธ", 25000) + )); + } + + public int getOrderDate() { + return this.orderDate; + } + + public Map<String, Integer> getUserOrder() { + return this.userOrder; + } + + public int getTotalPrice() { + int totalPrice = Constant.ZERO.get(); + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + int price = menu.get(category).get(entry.getKey()); + totalPrice += price * entry.getValue(); + } + return totalPrice; + } + + public int[] getCategoryCount() { + int[] categoryCount = new int[Constant.CATEGORY_COUNT.get()]; + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + categoryCount[category] += entry.getValue(); + } + return categoryCount; + } + + public void setOrderDate(int date) { + this.orderDate = date; + } + + public void setUserOrder(Map<String, Integer> userOrder) { + if (Validator.validateMenu(userOrder, this.menu)) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + this.userOrder = userOrder; + } +}
Java
Map.of๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋‚˜ ํŽธํ•ด๋ณด์ด๋„ค์š”! ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค! ์–ด๋””๊ฐ€์„œ ์จ๋จน์–ด ๋ด์•ผ๊ฒ ์–ด์š”!
@@ -0,0 +1,47 @@ +package christmas.controller; + +import java.util.Map; + +import christmas.view.*; +import christmas.model.*; + +public class MainController { + private OrderMenu orderMenu; + + public MainController() { + orderMenu = new OrderMenu(); + } + + public void run() { + orderMenu.setOrderDate(InputView.readDate()); + setReadOrder(); + + OutputView.printPreview(orderMenu.getOrderDate()); + OutputView.printOrder(orderMenu.getUserOrder()); + int totalPrice = orderMenu.getTotalPrice(); + OutputView.printTotalPrice(totalPrice); + OutputView.printPresent(totalPrice); + benefitControl(totalPrice); + } + + private void benefitControl(int totalPrice) { + Benefit benefit = new Benefit(orderMenu.getOrderDate(), orderMenu.getCategoryCount(), totalPrice); + OutputView.printBenefits(benefit.getBenefits()); + + int totalBenefitPrice = benefit.getTotalBenefitPrice(); + OutputView.printTotalBenefitPrice(totalBenefitPrice); + OutputView.printTotalPayment(totalPrice - totalBenefitPrice + benefit.getPresentPrice()); + OutputView.printEventBadge(totalBenefitPrice); + } + + private void setReadOrder() { + Map<String, Integer> orderMenu; + try { + orderMenu = InputView.readOrder(); + this.orderMenu.setUserOrder(orderMenu); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + setReadOrder(); + } + } +}
Java
MainController๊ฐ€ ํฌ๊ฒŒ ์ปจํŠธ๋กค(๊ฐ์ฒด๋‚ด ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ • ๋ฐ ์ €์žฅ)ํ•˜๋Š” ๋ชจ๋ธ์€ orderMenu์ธ ๊ฒƒ ๊ฐ™์•„๋ณด์ž…๋‹ˆ๋‹ค. ์ฆ‰ ํด๋ž˜์Šค ์ด๋ฆ„์ด orderController์ด๋ฉด Model๊ณผ Controller๊ฐ„์˜ ์—ญํ•  ์˜์กด์„ฑ์— ๋” ๋งž๋Š” ์ž‘๋ช…์ผ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,79 @@ +package christmas.model; + +import christmas.util.Constant; +import christmas.util.Message; + +import java.util.HashMap; +import java.util.Map; + +public class Benefit { + private int orderDate; + private int[] categoryCount; + Map<String, Integer> benefits; + + public Benefit(int orderDate, int[] categoryCount, int totalPrice) { + this.orderDate = orderDate; + this.categoryCount = categoryCount; + benefits = new HashMap<>(); + if (totalPrice < Constant.MIN_BENEFIT_PRICE.get()) { + return; + } + christmasBenefit(); + weekBenefit(); + starBenefit(); + if (totalPrice >= Constant.PRESENT_COST.get()) { + presentBenefit(); + } + } + + public Map<String, Integer> getBenefits() { + return this.benefits; + } + + public int getTotalBenefitPrice() { + int totalBenefitPrice = Constant.ZERO.get(); + for (Map.Entry<String, Integer> entry : this.benefits.entrySet()) { + int benefit = entry.getValue(); + totalBenefitPrice += benefit; + } + return totalBenefitPrice; + } + + public int getPresentPrice() { + if (this.benefits.containsKey(Message.PRESENT_BENEFIT.get())) { + return Constant.CHAMPAIGN_PRICE.get(); + } + return Constant.ZERO.get(); + } + + private void christmasBenefit() { + if (orderDate > Constant.CHRISTMAS_DAY.get()) { + return; + } + int benefit = Constant.THOUSAND.get() + (orderDate - Constant.ONE.get()) * Constant.HUNDRED.get(); + benefits.put(Message.D_DAY_BENEFIT.get(), benefit); + } + + private void weekBenefit() { + if (orderDate % Constant.WEEKDAYS.get() == Constant.FRIDAY.get() + || orderDate % Constant.WEEKDAYS.get() == Constant.SATURDAY.get()) { + int benefit = Constant.EVENT_PRICE.get() * categoryCount[Constant.MAINMENU.get()]; + benefits.put(Message.WEEKEND_BENEFIT.get(), benefit); + return; + } + int benefit = Constant.EVENT_PRICE.get() * categoryCount[Constant.DESSERT.get()]; + benefits.put(Message.WEEKDAY_BENEFIT.get(), benefit); + } + + private void starBenefit() { + if (orderDate != Constant.CHRISTMAS_DAY.get() + && orderDate % Constant.WEEKDAYS.get() != Constant.SUNDAY.get()) { + return; + } + benefits.put(Message.STAR_BENEFIT.get(), Constant.THOUSAND.get()); + } + + private void presentBenefit() { + benefits.put(Message.PRESENT_BENEFIT.get(), Constant.CHAMPAIGN_PRICE.get()); + } +}
Java
์ „์ฒด์ ์œผ๋กœ ํ•จ์ˆ˜๋ถ„๋ฆฌ๊ฐ€ ์ž˜ ๋ผ์žˆ์–ด์„œ ํ•จ์ˆ˜๋‹น ๋ผ์ธ์ด ์งง์€ ํŽธ์ด๋„ค์š”! ๊ต‰์žฅํžˆ ๊ฐ€๋…์„ฑ ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,39 @@ +package christmas.util; + +public enum Constant { + NOT_IN_MENU(-1), + EPPETIZER(0), + MAINMENU(1), + DESSERT(2), + BEVERAGE(3), + CATEGORY_COUNT(4), + ZERO(0), + ONE(1), + THOUSAND(1000), + HUNDRED(100), + CHRISTMAS_DAY(25), + MIN_BENEFIT_PRICE(10000), + PRESENT_COST(120000), + CHAMPAIGN_PRICE(25000), + WEEKDAYS(7), + FRIDAY(1), + SATURDAY(2), + SUNDAY(3), + EVENT_PRICE(2023), + SANTA(20000), + TREE(10000), + STAR(5000), + MAX_MENU_COUNT(20), + KEY_INDEX(0), + VALUE_INDEX(1); + + private final int value; + + Constant(int value) { + this.value = value; + } + + public int get() { + return this.value; + } +}
Java
์ƒ์ˆ˜๋“ค์„ enum์œผ๋กœ ๊ด€๋ฆฌํ•˜์‹œ๋Š” ๊ฒƒ์€ ์ข‹์€ ํฌ์ธํŠธ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ constant์— ๋ชจ๋“  ์ƒ์ˆ˜๋ฅผ ๋‹ค ๋„ฃ์–ด๋ฒ„๋ฆฌ์‹œ๋ฉด ๊ฐœ๋ณ„ ์ƒ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•ด์•ผ ํ•  ๋•Œ ์ฐพ๊ธฐ ์–ด๋ ค์›Œ์งˆ ๊ฒƒ ๊ฐ™์•„์š”. enum์„ ์ข…๋ฅ˜๋ณ„๋กœ, ๋ฌธ๋งฅ๋ณ„๋กœ ๋ถ„๋ฆฌํ•˜์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœ ๋“œ๋ ค์š”.
@@ -0,0 +1,79 @@ +package christmas.model; + +import christmas.util.Constant; +import christmas.util.Message; + +import java.util.HashMap; +import java.util.Map; + +public class Benefit { + private int orderDate; + private int[] categoryCount; + Map<String, Integer> benefits; + + public Benefit(int orderDate, int[] categoryCount, int totalPrice) { + this.orderDate = orderDate; + this.categoryCount = categoryCount; + benefits = new HashMap<>(); + if (totalPrice < Constant.MIN_BENEFIT_PRICE.get()) { + return; + } + christmasBenefit(); + weekBenefit(); + starBenefit(); + if (totalPrice >= Constant.PRESENT_COST.get()) { + presentBenefit(); + } + } + + public Map<String, Integer> getBenefits() { + return this.benefits; + } + + public int getTotalBenefitPrice() { + int totalBenefitPrice = Constant.ZERO.get(); + for (Map.Entry<String, Integer> entry : this.benefits.entrySet()) { + int benefit = entry.getValue(); + totalBenefitPrice += benefit; + } + return totalBenefitPrice; + } + + public int getPresentPrice() { + if (this.benefits.containsKey(Message.PRESENT_BENEFIT.get())) { + return Constant.CHAMPAIGN_PRICE.get(); + } + return Constant.ZERO.get(); + } + + private void christmasBenefit() { + if (orderDate > Constant.CHRISTMAS_DAY.get()) { + return; + } + int benefit = Constant.THOUSAND.get() + (orderDate - Constant.ONE.get()) * Constant.HUNDRED.get(); + benefits.put(Message.D_DAY_BENEFIT.get(), benefit); + } + + private void weekBenefit() { + if (orderDate % Constant.WEEKDAYS.get() == Constant.FRIDAY.get() + || orderDate % Constant.WEEKDAYS.get() == Constant.SATURDAY.get()) { + int benefit = Constant.EVENT_PRICE.get() * categoryCount[Constant.MAINMENU.get()]; + benefits.put(Message.WEEKEND_BENEFIT.get(), benefit); + return; + } + int benefit = Constant.EVENT_PRICE.get() * categoryCount[Constant.DESSERT.get()]; + benefits.put(Message.WEEKDAY_BENEFIT.get(), benefit); + } + + private void starBenefit() { + if (orderDate != Constant.CHRISTMAS_DAY.get() + && orderDate % Constant.WEEKDAYS.get() != Constant.SUNDAY.get()) { + return; + } + benefits.put(Message.STAR_BENEFIT.get(), Constant.THOUSAND.get()); + } + + private void presentBenefit() { + benefits.put(Message.PRESENT_BENEFIT.get(), Constant.CHAMPAIGN_PRICE.get()); + } +}
Java
์‹ค์ œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฐฐ์—ด ๋ณด๋‹ค๋Š” ๋ฆฌ์ŠคํŠธ๋‚˜, ๋งต, ์…‹ ๋“ฑ ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ด์š”. ๊ทธ ์ด์œ ๋Š” ์ง€๋‚œ ์ฃผ์ฐจ ํ”ผ๋“œ๋ฐฑ์„ ๋ณด๋ฉด ๋ฐฐ์—ด๋ณด๋‹ค ์ข€ ๋” ํŽธ๋ฆฌํ•œ ์—ฌ๋Ÿฌ API๋“ค์„ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ์š”. ์ด์™ธ์—๋„ ์ปฌ๋ ‰์…˜์ด ์„ ํ˜ธ๋˜๋Š” ์ด์œ ๋Š” ์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ” ๋“ฑ์—์„œ๋„ ์–ธ๊ธ‰๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. https://incheol-jung.gitbook.io/docs/study/effective-java/undefined-3/2020-03-20-effective-28item
@@ -0,0 +1,68 @@ +package christmas.model; + +import christmas.util.Constant; +import christmas.util.ErrorMessage; +import christmas.util.Utility; +import christmas.util.Validator; + +import java.util.ArrayList; +import java.util.Map; +import java.util.List; + +public class OrderMenu { + private final List<Map<String, Integer>> menu; + + private int orderDate; + private Map<String, Integer> userOrder; + + public OrderMenu() { + menu = new ArrayList<>(List.of( + // EPPETIZER 0 + Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "ํƒ€ํŒŒ์Šค", 5500, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + // MAINMENU 1 + Map.of("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "๋ฐ”๋น„ํ๋ฆฝ", 54000, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + // DESSERT 2 + Map.of("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "์•„์ด์Šคํฌ๋ฆผ", 5000), + // BEVERAGE 3 + Map.of("์ œ๋กœ์ฝœ๋ผ", 3000, "๋ ˆ๋“œ์™€์ธ", 60000, "์ƒดํŽ˜์ธ", 25000) + )); + } + + public int getOrderDate() { + return this.orderDate; + } + + public Map<String, Integer> getUserOrder() { + return this.userOrder; + } + + public int getTotalPrice() { + int totalPrice = Constant.ZERO.get(); + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + int price = menu.get(category).get(entry.getKey()); + totalPrice += price * entry.getValue(); + } + return totalPrice; + } + + public int[] getCategoryCount() { + int[] categoryCount = new int[Constant.CATEGORY_COUNT.get()]; + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + categoryCount[category] += entry.getValue(); + } + return categoryCount; + } + + public void setOrderDate(int date) { + this.orderDate = date; + } + + public void setUserOrder(Map<String, Integer> userOrder) { + if (Validator.validateMenu(userOrder, this.menu)) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + this.userOrder = userOrder; + } +}
Java
๋ฉ”๋‰ด ์ด๋ฆ„๊ณผ ๊ฐ€๊ฒฉ์˜ ๊ฒฝ์šฐ ํ•˜๋“œ ์ฝ”๋”ฉํ•˜๊ธฐ๋ณด๋‹ค enum ๋“ฑ์œผ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,97 @@ +package christmas.util; + +import java.util.Map; +import java.util.List; + +public class Validator { + private final static char MIN_DIGIT = '0'; + private final static char MAX_DIGIT = '9'; + private final static char CHAR_HYPHEN = '-'; + private final static char CHAR_COMMA = ','; + private final static int MIN_DAY = 1; + private final static int MAX_DAY = 31; + private final static int IS_TWO = 2; + + public static void validateDate(String input) { + if (input.isEmpty()) { + throw new IllegalArgumentException(ErrorMessage.DATE_ERROR.get()); + } + for (int i = Constant.ZERO.get(); i < input.length(); ++i) { + if (input.charAt(i) < MIN_DIGIT || input.charAt(i) > MAX_DIGIT) { + throw new IllegalArgumentException(ErrorMessage.DATE_ERROR.get()); + } + } + int date = Integer.parseInt(input); + if (date < MIN_DAY || date > MAX_DAY) { + throw new IllegalArgumentException(ErrorMessage.DATE_ERROR.get()); + } + } + + public static void validateOrder(String input) { + // splitํ•  ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰์˜ ๋นˆ ๋ฌธ์ž์—ด์„ ์‚ฌ๋ผ์ง€๋ฏ€๋กœ ๋ณ„๋„๋กœ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ + if (input.isEmpty() || input.charAt(input.length() - Constant.ONE.get()) == CHAR_COMMA) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + String[] splittedComma = input.split(Message.COMMA.get()); + if (splittedComma.length == Constant.ZERO.get()) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + for (String string : splittedComma) { + if (validateFormat(string)) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + } + } + + public static boolean validateMenu(Map<String, Integer> userOrder, List<Map<String, Integer>> menu) { + int[] countCategory = new int[Constant.CATEGORY_COUNT.get()]; + for (Map.Entry<String, Integer> entry : userOrder.entrySet()) { + if (entry.getValue() <= Constant.ZERO.get()) { + return true; + } + int category = Utility.getCategory(entry.getKey(), menu); + if (category == Constant.NOT_IN_MENU.get()) { + return true; + } + countCategory[category] += entry.getValue(); + } + if (validateCount(countCategory)) { + return true; + } + return false; + } + + private static boolean validateCount(int[] countCategory) { + // ์Œ๋ฃŒ๋งŒ ์‹œํ‚จ ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ + if (countCategory[Constant.EPPETIZER.get()] == Constant.ZERO.get() + && countCategory[Constant.MAINMENU.get()] == Constant.ZERO.get() + && countCategory[Constant.DESSERT.get()] == Constant.ZERO.get()) { + return true; + } + int totalCount = Constant.ZERO.get(); + for (int count : countCategory) { + totalCount += count; + } + if (totalCount > Constant.MAX_MENU_COUNT.get()) { + return true; + } + return false; + } + + private static boolean validateFormat(String input) { + if (input.isEmpty() || input.charAt(input.length() - Constant.ONE.get()) == CHAR_HYPHEN) { + return true; + } + String[] splittedHyphen = input.split(Message.HYPHEN.get()); + if (splittedHyphen.length != IS_TWO) { + return true; + } + for (int i = Constant.ZERO.get(); i < splittedHyphen[Constant.VALUE_INDEX.get()].length(); ++i) { + if (splittedHyphen[Constant.VALUE_INDEX.get()].charAt(i) < MIN_DIGIT + || splittedHyphen[Constant.VALUE_INDEX.get()].charAt(i) > MAX_DIGIT) { + return true; + } + } + return false; + } +}
Java
์ด๋ ‡๊ฒŒ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๋กœ์ง์„ ํ•œ ๊ตฐ๋ฐ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์ด์ง€๋งŒ ๋„๋ฉ”์ธ์—์„œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌํ•˜๊ฒŒ ๋˜๋ฉด ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๋กœ์ง๋“ค์ด ํฉ์–ด์ง€๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์–ด์„œ ๋กœ์ง์„ ๋”ฐ๋ผ๊ฐ€๊ธฐ ์–ด๋ ค์šด๋ฐ ์ด๋ ‡๊ฒŒ ๋ชจ์•„๋‘๋ฉด ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๋ฉ”์„œ๋“œ๋“ค๋งŒ ํ•œ๋ˆˆ์— ๋ณผ ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,96 @@ +package christmas.view; + +import christmas.util.Constant; +import christmas.util.Message; +import java.util.Map; + +import java.text.DecimalFormat; + +public class OutputView { + public static void printPreview(int orderDate) { + System.out.println("12์›” " + orderDate + "์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!"); + System.out.println(); + } + + public static void printOrder(Map<String, Integer> userOrder) { + System.out.println("<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"); + for (Map.Entry<String, Integer> entry : userOrder.entrySet()) { + System.out.println(entry.getKey() + Message.SPACE.get() + entry.getValue() + Message.COUNT.get()); + } + System.out.println(); + } + + public static void printTotalPrice(int totalPrice) { + DecimalFormat decimalFormat = new DecimalFormat(Message.PRICE_FORMAT.get()); + + System.out.println("<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>"); + String formattedNumber = decimalFormat.format(totalPrice); + System.out.println(formattedNumber + Message.WON.get()); + System.out.println(); + } + + public static void printPresent(int totalPrice) { + System.out.println("<์ฆ์ • ๋ฉ”๋‰ด>"); + if (totalPrice < Constant.PRESENT_COST.get()) { + System.out.println(Message.NOTHING.get()); + System.out.println(); + return; + } + System.out.println(Message.ONE_CHAMPAIGN.get()); + System.out.println(); + } + + public static void printBenefits(Map<String, Integer> benefits) { + System.out.println("<ํ˜œํƒ ๋‚ด์—ญ>"); + if (benefits.isEmpty()) { + System.out.println(Message.NOTHING.get()); + System.out.println(); + return; + } + DecimalFormat decimalFormat = new DecimalFormat(Message.PRICE_FORMAT.get()); + for (Map.Entry<String, Integer> entry : benefits.entrySet()) { + System.out.print(entry.getKey() + ": -"); + String fomattedNumber = decimalFormat.format(entry.getValue()); + System.out.println(fomattedNumber + Message.WON.get()); + } + System.out.println(); + } + + public static void printTotalBenefitPrice(int totalBenefitPrice) { + System.out.println("<์ดํ˜œํƒ ๊ธˆ์•ก>"); + if (totalBenefitPrice == Constant.ZERO.get()) { + System.out.println(Message.NOTHING.get()); + System.out.println(); + return; + } + DecimalFormat decimalFormat = new DecimalFormat(Message.PRICE_FORMAT.get()); + String formattedNumber = decimalFormat.format(totalBenefitPrice); + System.out.println(Message.HYPHEN.get() + formattedNumber + Message.WON.get()); + System.out.println(); + } + + public static void printTotalPayment(int totalPayment) { + System.out.println("<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>"); + DecimalFormat decimalFormat = new DecimalFormat(Message.PRICE_FORMAT.get()); + String formattedNumber = decimalFormat.format(totalPayment); + System.out.println(formattedNumber + Message.WON.get()); + System.out.println(); + } + + public static void printEventBadge(int totalBenefitPrice) { + System.out.println("<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>"); + if (totalBenefitPrice >= Constant.SANTA.get()) { + System.out.println("์‚ฐํƒ€"); + return; + } + if (totalBenefitPrice >= Constant.TREE.get()) { + System.out.println("ํŠธ๋ฆฌ"); + return; + } + if (totalBenefitPrice >= Constant.STAR.get()) { + System.out.println("๋ณ„"); + return; + } + System.out.println(Message.NOTHING.get()); + } +}
Java
์ถœ๋ ฅ ๋ทฐ ๋ฉ”์„œ๋“œ์— ์ฃผ๋ฌธ์ด๋‚˜ ํ• ์ธ ์ „ ์ฃผ๋ฌธ๊ธˆ์•ก์„ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌ ํ›„ ํ˜œํƒ์ด๋‚˜ ์ฆ์ • ๋ฉ”๋‰ด๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ถ„๊ธฐ ๋กœ์ง์ด ๋‹ด๊ฒจ ์žˆ๋„ค์š”. ์ถœ๋ ฅ ๋ทฐ๋Š” ์ถœ๋ ฅ๋งŒ์— ๋Œ€ํ•œ ์ฑ…์ž„์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ˜œํƒ์ด๋‚˜ ์ฆ์ •๋ฉ”๋‰ด๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์ฑ…์ž„์€ ๊ฐ๊ฐ์˜ ๋„๋ฉ”์ธ ๊ฐ์ฒด๋กœ ์ด๋™ํ•˜์‹œ๋Š” ๊ฒŒ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,135 @@ +package christmas; + +import christmas.model.OrderMenu; +import christmas.util.Validator; + +import java.lang.reflect.Array; +import java.util.Map; +import java.util.HashMap; +import java.util.ArrayList; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThat; + +public class ValidatorTest { + private List<Map<String, Integer>> menu = new ArrayList<>(List.of( + Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "ํƒ€ํŒŒ์Šค", 5500, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + Map.of("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "๋ฐ”๋น„ํ๋ฆฝ", 54000, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + Map.of("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "์•„์ด์Šคํฌ๋ฆผ", 5000), + Map.of("์ œ๋กœ์ฝœ๋ผ", 3000, "๋ ˆ๋“œ์™€์ธ", 60000, "์ƒดํŽ˜์ธ", 25000) + )); + + @DisplayName("์ž…๋ ฅ๋œ ๋‚ ์งœ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isDateNoneTest() { + assertThatThrownBy(() -> Validator.validateDate("")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("์ž…๋ ฅ๋œ ๋‚ ์งœ๊ฐ€ ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isDateInvalidTest() { + assertThatThrownBy(() -> Validator.validateDate("1a")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("์ž…๋ ฅ๋œ ๋‚ ์งœ๊ฐ€ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void dateRangeTest() { + assertThatThrownBy(() -> Validator.validateDate("123")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("์ž…๋ ฅ๋œ ์ฃผ๋ฌธ์ด ๋น„์–ด์žˆ์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isOrderNoneTest() { + assertThatThrownBy(() -> Validator.validateOrder("")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("์ž…๋ ฅ๋œ ์ฃผ๋ฌธ ์ค‘ ๋นˆ ๋ฌธ์ž์—ด์ด ์žˆ์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isParsedNoneTest() { + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder(",ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,,๋ ˆ๋“œ์™€์ธ-1")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder(",,,")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("์ฃผ๋ฌธ ํฌ๋งท์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isFormatWrongTest() { + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1-1,์ œ๋กœ์ฝœ๋ผ-1")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1,์ œ๋กœ์ฝœ๋ผ-")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1,---")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("๊ฐœ์ˆ˜์— ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isNotNumberTest() { + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-๋ ˆ๋“œ์™€์ธ")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1,์ œ๋กœ์ฝœ๋ผ-๋ ˆ๋“œ์™€์ธ")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1,์ œ๋กœ์ฝœ๋ผ-2๋ ˆ๋“œ์™€์ธ")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("๋ฉ”๋‰ดํŒ์— ์—†๋Š” ๋ฉ”๋‰ด๋ฅผ ์ฃผ๋ฌธํ•˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isOrderInMenu() { + Map<String, Integer> order1 = Map.of("๋˜ ์–Œ๊ฟ", 1); + Map<String, Integer> order2 = Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 2, "์—ฐ์ €์œก์ฐœ", 1); + Map<String, Integer> order3 = Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 1, "์•„์ด์Šคํฌ๋ฆผ", 1); + + assertThat(Validator.validateMenu(order1, menu)).isEqualTo(true); + assertThat(Validator.validateMenu(order2, menu)).isEqualTo(true); + assertThat(Validator.validateMenu(order3, menu)).isEqualTo(false); + } + + @DisplayName("0๊ฐœ ์ดํ•˜์ธ ๊ฐœ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isCountValid() { + Map<String, Integer> order1 = Map.of("์•„์ด์Šคํฌ๋ฆผ", 0); + Map<String, Integer> order2 = Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 1, "์•„์ด์Šคํฌ๋ฆผ", 0); + + assertThat(Validator.validateMenu(order1, menu)).isEqualTo(true); + assertThat(Validator.validateMenu(order2, menu)).isEqualTo(true); + } + + @DisplayName("20๊ฐœ๊ฐ€ ๋„˜์–ด๊ฐ€๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isUnderTwenty() { + Map<String, Integer> order3 = Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 21); + Map<String, Integer> order4 = Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 10, "์•„์ด์Šคํฌ๋ฆผ", 11); + + assertThat(Validator.validateMenu(order3, menu)).isEqualTo(true); + assertThat(Validator.validateMenu(order4, menu)).isEqualTo(true); + } + + @DisplayName("์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isOnlyBeverage() { + Map<String, Integer> order3 = Map.of("์ œ๋กœ์ฝœ๋ผ", 1); + Map<String, Integer> order4 = Map.of("์ œ๋กœ์ฝœ๋ผ", 3, "๋ ˆ๋“œ์™€์ธ", 1); + + assertThat(Validator.validateMenu(order3, menu)).isEqualTo(true); + assertThat(Validator.validateMenu(order4, menu)).isEqualTo(true); + } +}
Java
ํ•˜๋‚˜์˜ ํ…Œ์ŠคํŠธ๋Š” ํ•˜๋‚˜์˜ ๊ธฐ๋Šฅ ํ˜น์€ ์ฑ…์ž„๋งŒ์„ ํ…Œ์ŠคํŠธ ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”. ์ผ๋ฐ˜์ ์œผ๋กœ ํ•˜๋‚˜์˜ ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ์—์„œ ๋ฌด์—‡์„ ํ…Œ์ŠคํŠธํ•˜๋ ค๋Š”์ง€ ์ž˜ ๊ธฐ์ˆ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜์˜ ํ…Œ์ŠคํŠธ์— ํ•˜๋‚˜์˜ ๋‹จ์–ธ๋ฌธ์„ ์“ฐ๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,39 @@ +package christmas.util; + +public enum Constant { + NOT_IN_MENU(-1), + EPPETIZER(0), + MAINMENU(1), + DESSERT(2), + BEVERAGE(3), + CATEGORY_COUNT(4), + ZERO(0), + ONE(1), + THOUSAND(1000), + HUNDRED(100), + CHRISTMAS_DAY(25), + MIN_BENEFIT_PRICE(10000), + PRESENT_COST(120000), + CHAMPAIGN_PRICE(25000), + WEEKDAYS(7), + FRIDAY(1), + SATURDAY(2), + SUNDAY(3), + EVENT_PRICE(2023), + SANTA(20000), + TREE(10000), + STAR(5000), + MAX_MENU_COUNT(20), + KEY_INDEX(0), + VALUE_INDEX(1); + + private final int value; + + Constant(int value) { + this.value = value; + } + + public int get() { + return this.value; + } +}
Java
์ƒ์ˆ˜๋ฅผ ํ•˜๋‚˜์˜ enum์— ๊ด€๋ฆฌํ•˜์‹ ๊ฒƒ ๊ฐ™์€๋ฐ, enum์„ ์ข€ ๋ถ„๋ฆฌํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? ๊ด€๋ จ์„ฑ์ด ์—†๋А ์ƒ์ˆ˜๋“ค๋ผ๋ฆฌ ํ•œ๊ตฐ๋ฐ ๋ชฐ๋ ค์žˆ๋‹ค๋ณด๋‹ˆ ์–ด์ƒ‰ํ•˜๊ฒŒ ๋А๊ปด์ง‘๋‹ˆ๋‹ค
@@ -0,0 +1,68 @@ +package christmas.model; + +import christmas.util.Constant; +import christmas.util.ErrorMessage; +import christmas.util.Utility; +import christmas.util.Validator; + +import java.util.ArrayList; +import java.util.Map; +import java.util.List; + +public class OrderMenu { + private final List<Map<String, Integer>> menu; + + private int orderDate; + private Map<String, Integer> userOrder; + + public OrderMenu() { + menu = new ArrayList<>(List.of( + // EPPETIZER 0 + Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "ํƒ€ํŒŒ์Šค", 5500, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + // MAINMENU 1 + Map.of("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "๋ฐ”๋น„ํ๋ฆฝ", 54000, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + // DESSERT 2 + Map.of("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "์•„์ด์Šคํฌ๋ฆผ", 5000), + // BEVERAGE 3 + Map.of("์ œ๋กœ์ฝœ๋ผ", 3000, "๋ ˆ๋“œ์™€์ธ", 60000, "์ƒดํŽ˜์ธ", 25000) + )); + } + + public int getOrderDate() { + return this.orderDate; + } + + public Map<String, Integer> getUserOrder() { + return this.userOrder; + } + + public int getTotalPrice() { + int totalPrice = Constant.ZERO.get(); + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + int price = menu.get(category).get(entry.getKey()); + totalPrice += price * entry.getValue(); + } + return totalPrice; + } + + public int[] getCategoryCount() { + int[] categoryCount = new int[Constant.CATEGORY_COUNT.get()]; + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + categoryCount[category] += entry.getValue(); + } + return categoryCount; + } + + public void setOrderDate(int date) { + this.orderDate = date; + } + + public void setUserOrder(Map<String, Integer> userOrder) { + if (Validator.validateMenu(userOrder, this.menu)) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + this.userOrder = userOrder; + } +}
Java
menu๋ฅผ list์— ๊ด€๋ฆฌํ•˜์…จ๊ตฐ์š” ์ •๋‹ต์€ ์—†๋Š”๊ฒƒ ๊ฐ™๋‹ค๋งŒ ์šฐํ…Œ์ฝ”์—์„œ enum์„ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ๊ถŒ์žฅํ–ˆ์œผ๋‹ˆ ์ด๋Ÿฐ ๋ฉ”๋‰ด๋“ค์€ enum์—์„œ ๊ด€๋ฆฌํ•˜๋ฉด ์ข€ ๋” ์ข‹์ง€ ์•Š์„๊นŒ ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค!
@@ -0,0 +1,68 @@ +package christmas.model; + +import christmas.util.Constant; +import christmas.util.ErrorMessage; +import christmas.util.Utility; +import christmas.util.Validator; + +import java.util.ArrayList; +import java.util.Map; +import java.util.List; + +public class OrderMenu { + private final List<Map<String, Integer>> menu; + + private int orderDate; + private Map<String, Integer> userOrder; + + public OrderMenu() { + menu = new ArrayList<>(List.of( + // EPPETIZER 0 + Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "ํƒ€ํŒŒ์Šค", 5500, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + // MAINMENU 1 + Map.of("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "๋ฐ”๋น„ํ๋ฆฝ", 54000, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + // DESSERT 2 + Map.of("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "์•„์ด์Šคํฌ๋ฆผ", 5000), + // BEVERAGE 3 + Map.of("์ œ๋กœ์ฝœ๋ผ", 3000, "๋ ˆ๋“œ์™€์ธ", 60000, "์ƒดํŽ˜์ธ", 25000) + )); + } + + public int getOrderDate() { + return this.orderDate; + } + + public Map<String, Integer> getUserOrder() { + return this.userOrder; + } + + public int getTotalPrice() { + int totalPrice = Constant.ZERO.get(); + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + int price = menu.get(category).get(entry.getKey()); + totalPrice += price * entry.getValue(); + } + return totalPrice; + } + + public int[] getCategoryCount() { + int[] categoryCount = new int[Constant.CATEGORY_COUNT.get()]; + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + categoryCount[category] += entry.getValue(); + } + return categoryCount; + } + + public void setOrderDate(int date) { + this.orderDate = date; + } + + public void setUserOrder(Map<String, Integer> userOrder) { + if (Validator.validateMenu(userOrder, this.menu)) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + this.userOrder = userOrder; + } +}
Java
ํด๋ž˜์Šค๋ช…์€ OrderMenu์ธ๋ฐ orderDate๋ฅผ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜๋Š”๊ฒŒ ์กฐ๊ธˆ ์–ด์ƒ‰ํ•˜๊ฒŒ ๋А๊ปด์ง‘๋‹ˆ๋‹ค. ํด๋ž˜์Šค ๋ถ„๋ฆฌ๋ฅผ ํ†ตํ•ด orderDate์™€ ๊ด€๋ จํ•œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ฑฐ๋‚˜, OrderMenu์˜ ํด๋ž˜์Šค๋ช…์ด ๋ฉ”๋‰ด์™€ ๋‚ ์งœ๋ฅผ ๋‹ค ํฌํ•จํ•˜๊ฒŒ๋” ์ƒ๊ฐ์ด ๋“ค๊ฒŒ๋” ๋ณ€๊ฒฝํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,47 @@ +package christmas.controller; + +import java.util.Map; + +import christmas.view.*; +import christmas.model.*; + +public class MainController { + private OrderMenu orderMenu; + + public MainController() { + orderMenu = new OrderMenu(); + } + + public void run() { + orderMenu.setOrderDate(InputView.readDate()); + setReadOrder(); + + OutputView.printPreview(orderMenu.getOrderDate()); + OutputView.printOrder(orderMenu.getUserOrder()); + int totalPrice = orderMenu.getTotalPrice(); + OutputView.printTotalPrice(totalPrice); + OutputView.printPresent(totalPrice); + benefitControl(totalPrice); + } + + private void benefitControl(int totalPrice) { + Benefit benefit = new Benefit(orderMenu.getOrderDate(), orderMenu.getCategoryCount(), totalPrice); + OutputView.printBenefits(benefit.getBenefits()); + + int totalBenefitPrice = benefit.getTotalBenefitPrice(); + OutputView.printTotalBenefitPrice(totalBenefitPrice); + OutputView.printTotalPayment(totalPrice - totalBenefitPrice + benefit.getPresentPrice()); + OutputView.printEventBadge(totalBenefitPrice); + } + + private void setReadOrder() { + Map<String, Integer> orderMenu; + try { + orderMenu = InputView.readOrder(); + this.orderMenu.setUserOrder(orderMenu); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + setReadOrder(); + } + } +}
Java
์‚ฌ์‹ค ์ œ๊ฐ€ ์—„์ฒญ ์˜ค๋ž˜ ์ฐพ์•„๋ณด๊ณ  ๊ณ ๋ฏผํ•œ ๋ถ€๋ถ„์ด๊ธด ํ•œ๋ฐ ๋ณดํ†ต ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์€ ์ž˜ ๋ชฐ๋ผ์„œ ์ œ๊ฐ€ ์ƒ๊ฐํ•œ ๋Œ€๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ์ธ์ ์œผ๋กœ ์ƒ๊ฐํ•œ View ํด๋ž˜์Šค๋Š” ์ƒํƒœ๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š๊ณ  ์ธ์Šคํ„ด์Šคํ™”๋ฅผ ํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์Šคํƒœํ‹ฑ ํด๋ž˜์Šค๋‚˜ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๋ดค์—ˆ๋Š”๋ฐ, ๊ณผ์ œ์—์„œ View๋ฅผ ์ƒ์†ํ•  ์ผ์€ ์—†์„ ๊ฒƒ ๊ฐ™์•„์„œ ์Šคํƒœํ‹ฑ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฑธ๊นŒ์š”?
@@ -0,0 +1,39 @@ +package christmas.util; + +public enum Constant { + NOT_IN_MENU(-1), + EPPETIZER(0), + MAINMENU(1), + DESSERT(2), + BEVERAGE(3), + CATEGORY_COUNT(4), + ZERO(0), + ONE(1), + THOUSAND(1000), + HUNDRED(100), + CHRISTMAS_DAY(25), + MIN_BENEFIT_PRICE(10000), + PRESENT_COST(120000), + CHAMPAIGN_PRICE(25000), + WEEKDAYS(7), + FRIDAY(1), + SATURDAY(2), + SUNDAY(3), + EVENT_PRICE(2023), + SANTA(20000), + TREE(10000), + STAR(5000), + MAX_MENU_COUNT(20), + KEY_INDEX(0), + VALUE_INDEX(1); + + private final int value; + + Constant(int value) { + this.value = value; + } + + public int get() { + return this.value; + } +}
Java
enum์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์ž˜ ๋ชฐ๋ž๋˜ ๊ฒƒ ๊ฐ™์•„์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,47 @@ +package christmas.controller; + +import java.util.Map; + +import christmas.view.*; +import christmas.model.*; + +public class MainController { + private OrderMenu orderMenu; + + public MainController() { + orderMenu = new OrderMenu(); + } + + public void run() { + orderMenu.setOrderDate(InputView.readDate()); + setReadOrder(); + + OutputView.printPreview(orderMenu.getOrderDate()); + OutputView.printOrder(orderMenu.getUserOrder()); + int totalPrice = orderMenu.getTotalPrice(); + OutputView.printTotalPrice(totalPrice); + OutputView.printPresent(totalPrice); + benefitControl(totalPrice); + } + + private void benefitControl(int totalPrice) { + Benefit benefit = new Benefit(orderMenu.getOrderDate(), orderMenu.getCategoryCount(), totalPrice); + OutputView.printBenefits(benefit.getBenefits()); + + int totalBenefitPrice = benefit.getTotalBenefitPrice(); + OutputView.printTotalBenefitPrice(totalBenefitPrice); + OutputView.printTotalPayment(totalPrice - totalBenefitPrice + benefit.getPresentPrice()); + OutputView.printEventBadge(totalBenefitPrice); + } + + private void setReadOrder() { + Map<String, Integer> orderMenu; + try { + orderMenu = InputView.readOrder(); + this.orderMenu.setUserOrder(orderMenu); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + setReadOrder(); + } + } +}
Java
๋‹ค์‹œ ๋ณด๋‹ˆ๊นŒ MainController๊ฐ€ ์ ์ ˆํ•œ ์ด๋ฆ„์ด ์•„๋‹Œ ๊ฒƒ ๊ฐ™๋„ค์š”, ๋ณดํ†ต ๋ชจ๋ธ๊ณผ ์ปจํŠธ๋กค๋Ÿฌ์˜ ๊ด€๊ณ„๊ฐ€ 1๋Œ€ 1๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค๊ณ  ๋ณด๋ฉด ๋˜๋Š” ๊ฑธ๊นŒ์š”?
@@ -0,0 +1,39 @@ +package christmas.util; + +public enum Constant { + NOT_IN_MENU(-1), + EPPETIZER(0), + MAINMENU(1), + DESSERT(2), + BEVERAGE(3), + CATEGORY_COUNT(4), + ZERO(0), + ONE(1), + THOUSAND(1000), + HUNDRED(100), + CHRISTMAS_DAY(25), + MIN_BENEFIT_PRICE(10000), + PRESENT_COST(120000), + CHAMPAIGN_PRICE(25000), + WEEKDAYS(7), + FRIDAY(1), + SATURDAY(2), + SUNDAY(3), + EVENT_PRICE(2023), + SANTA(20000), + TREE(10000), + STAR(5000), + MAX_MENU_COUNT(20), + KEY_INDEX(0), + VALUE_INDEX(1); + + private final int value; + + Constant(int value) { + this.value = value; + } + + public int get() { + return this.value; + } +}
Java
์ƒ์ˆ˜์ฒ˜๋ฆฌ์—๋งŒ ๊ธ‰๊ธ‰ํ•ด์„œ ๋ถ„๋ฆฌํ•˜์ง€ ๋ชปํ–ˆ๋Š”๋ฐ ์ง€๊ธˆ ๋ณด๋‹ˆ ๊ฐ€๋…์„ฑ ์ธก๋ฉด์—์„œ๋„ ์ข‹์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™๋„ค์š”. ์ด ๋ถ€๋ถ„์€ ๊ผญ ์ˆ˜์ •ํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,79 @@ +package christmas.model; + +import christmas.util.Constant; +import christmas.util.Message; + +import java.util.HashMap; +import java.util.Map; + +public class Benefit { + private int orderDate; + private int[] categoryCount; + Map<String, Integer> benefits; + + public Benefit(int orderDate, int[] categoryCount, int totalPrice) { + this.orderDate = orderDate; + this.categoryCount = categoryCount; + benefits = new HashMap<>(); + if (totalPrice < Constant.MIN_BENEFIT_PRICE.get()) { + return; + } + christmasBenefit(); + weekBenefit(); + starBenefit(); + if (totalPrice >= Constant.PRESENT_COST.get()) { + presentBenefit(); + } + } + + public Map<String, Integer> getBenefits() { + return this.benefits; + } + + public int getTotalBenefitPrice() { + int totalBenefitPrice = Constant.ZERO.get(); + for (Map.Entry<String, Integer> entry : this.benefits.entrySet()) { + int benefit = entry.getValue(); + totalBenefitPrice += benefit; + } + return totalBenefitPrice; + } + + public int getPresentPrice() { + if (this.benefits.containsKey(Message.PRESENT_BENEFIT.get())) { + return Constant.CHAMPAIGN_PRICE.get(); + } + return Constant.ZERO.get(); + } + + private void christmasBenefit() { + if (orderDate > Constant.CHRISTMAS_DAY.get()) { + return; + } + int benefit = Constant.THOUSAND.get() + (orderDate - Constant.ONE.get()) * Constant.HUNDRED.get(); + benefits.put(Message.D_DAY_BENEFIT.get(), benefit); + } + + private void weekBenefit() { + if (orderDate % Constant.WEEKDAYS.get() == Constant.FRIDAY.get() + || orderDate % Constant.WEEKDAYS.get() == Constant.SATURDAY.get()) { + int benefit = Constant.EVENT_PRICE.get() * categoryCount[Constant.MAINMENU.get()]; + benefits.put(Message.WEEKEND_BENEFIT.get(), benefit); + return; + } + int benefit = Constant.EVENT_PRICE.get() * categoryCount[Constant.DESSERT.get()]; + benefits.put(Message.WEEKDAY_BENEFIT.get(), benefit); + } + + private void starBenefit() { + if (orderDate != Constant.CHRISTMAS_DAY.get() + && orderDate % Constant.WEEKDAYS.get() != Constant.SUNDAY.get()) { + return; + } + benefits.put(Message.STAR_BENEFIT.get(), Constant.THOUSAND.get()); + } + + private void presentBenefit() { + benefits.put(Message.PRESENT_BENEFIT.get(), Constant.CHAMPAIGN_PRICE.get()); + } +}
Java
์œ„ ์ฝ”๋“œ์—์„œ๋Š” ๋ฐฐ์—ด๋งŒ์œผ๋กœ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•ด์„œ ๋ฐฐ์—ด๋กœ ์ฒ˜๋ฆฌํ–ˆ๋Š”๋ฐ ์˜ฌ๋ ค์ฃผ์‹  ๋งํฌ๋ฅผ ๋ณด๋‹ˆ๊นŒ ์ปฌ๋ ‰์…˜ ArrayList์˜ ์žฅ์ ์ด ๊ฐ€๋ณ€ ๊ธธ์ด๋งŒ์€ ์•„๋‹ˆ์—ˆ๊ตฐ์š”. ๋งŽ์ด ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค!
@@ -0,0 +1,68 @@ +package christmas.model; + +import christmas.util.Constant; +import christmas.util.ErrorMessage; +import christmas.util.Utility; +import christmas.util.Validator; + +import java.util.ArrayList; +import java.util.Map; +import java.util.List; + +public class OrderMenu { + private final List<Map<String, Integer>> menu; + + private int orderDate; + private Map<String, Integer> userOrder; + + public OrderMenu() { + menu = new ArrayList<>(List.of( + // EPPETIZER 0 + Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "ํƒ€ํŒŒ์Šค", 5500, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + // MAINMENU 1 + Map.of("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "๋ฐ”๋น„ํ๋ฆฝ", 54000, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + // DESSERT 2 + Map.of("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "์•„์ด์Šคํฌ๋ฆผ", 5000), + // BEVERAGE 3 + Map.of("์ œ๋กœ์ฝœ๋ผ", 3000, "๋ ˆ๋“œ์™€์ธ", 60000, "์ƒดํŽ˜์ธ", 25000) + )); + } + + public int getOrderDate() { + return this.orderDate; + } + + public Map<String, Integer> getUserOrder() { + return this.userOrder; + } + + public int getTotalPrice() { + int totalPrice = Constant.ZERO.get(); + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + int price = menu.get(category).get(entry.getKey()); + totalPrice += price * entry.getValue(); + } + return totalPrice; + } + + public int[] getCategoryCount() { + int[] categoryCount = new int[Constant.CATEGORY_COUNT.get()]; + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + categoryCount[category] += entry.getValue(); + } + return categoryCount; + } + + public void setOrderDate(int date) { + this.orderDate = date; + } + + public void setUserOrder(Map<String, Integer> userOrder) { + if (Validator.validateMenu(userOrder, this.menu)) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + this.userOrder = userOrder; + } +}
Java
์ด ์ ์€ ์ œ๊ฐ€ ์•„์˜ˆ ๋ชฐ๋ž๋˜ ๋ถ€๋ถ„์ด์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋“œ์ฝ”๋”ฉํ•˜๋ฉด์„œ๋„ ์ข‹์€ ๋ฐฉ์‹์ด ์•„๋‹ˆ๋ผ๊ณ  ๋А๊ผˆ๋Š”๋ฐ enum์„ ์‚ฌ์šฉํ•˜๋ฉด ๋์—ˆ๋„ค์š”...
@@ -0,0 +1,96 @@ +package christmas.view; + +import christmas.util.Constant; +import christmas.util.Message; +import java.util.Map; + +import java.text.DecimalFormat; + +public class OutputView { + public static void printPreview(int orderDate) { + System.out.println("12์›” " + orderDate + "์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!"); + System.out.println(); + } + + public static void printOrder(Map<String, Integer> userOrder) { + System.out.println("<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"); + for (Map.Entry<String, Integer> entry : userOrder.entrySet()) { + System.out.println(entry.getKey() + Message.SPACE.get() + entry.getValue() + Message.COUNT.get()); + } + System.out.println(); + } + + public static void printTotalPrice(int totalPrice) { + DecimalFormat decimalFormat = new DecimalFormat(Message.PRICE_FORMAT.get()); + + System.out.println("<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>"); + String formattedNumber = decimalFormat.format(totalPrice); + System.out.println(formattedNumber + Message.WON.get()); + System.out.println(); + } + + public static void printPresent(int totalPrice) { + System.out.println("<์ฆ์ • ๋ฉ”๋‰ด>"); + if (totalPrice < Constant.PRESENT_COST.get()) { + System.out.println(Message.NOTHING.get()); + System.out.println(); + return; + } + System.out.println(Message.ONE_CHAMPAIGN.get()); + System.out.println(); + } + + public static void printBenefits(Map<String, Integer> benefits) { + System.out.println("<ํ˜œํƒ ๋‚ด์—ญ>"); + if (benefits.isEmpty()) { + System.out.println(Message.NOTHING.get()); + System.out.println(); + return; + } + DecimalFormat decimalFormat = new DecimalFormat(Message.PRICE_FORMAT.get()); + for (Map.Entry<String, Integer> entry : benefits.entrySet()) { + System.out.print(entry.getKey() + ": -"); + String fomattedNumber = decimalFormat.format(entry.getValue()); + System.out.println(fomattedNumber + Message.WON.get()); + } + System.out.println(); + } + + public static void printTotalBenefitPrice(int totalBenefitPrice) { + System.out.println("<์ดํ˜œํƒ ๊ธˆ์•ก>"); + if (totalBenefitPrice == Constant.ZERO.get()) { + System.out.println(Message.NOTHING.get()); + System.out.println(); + return; + } + DecimalFormat decimalFormat = new DecimalFormat(Message.PRICE_FORMAT.get()); + String formattedNumber = decimalFormat.format(totalBenefitPrice); + System.out.println(Message.HYPHEN.get() + formattedNumber + Message.WON.get()); + System.out.println(); + } + + public static void printTotalPayment(int totalPayment) { + System.out.println("<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>"); + DecimalFormat decimalFormat = new DecimalFormat(Message.PRICE_FORMAT.get()); + String formattedNumber = decimalFormat.format(totalPayment); + System.out.println(formattedNumber + Message.WON.get()); + System.out.println(); + } + + public static void printEventBadge(int totalBenefitPrice) { + System.out.println("<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>"); + if (totalBenefitPrice >= Constant.SANTA.get()) { + System.out.println("์‚ฐํƒ€"); + return; + } + if (totalBenefitPrice >= Constant.TREE.get()) { + System.out.println("ํŠธ๋ฆฌ"); + return; + } + if (totalBenefitPrice >= Constant.STAR.get()) { + System.out.println("๋ณ„"); + return; + } + System.out.println(Message.NOTHING.get()); + } +}
Java
mvc์˜ ์—ญํ•  ๋ถ„๋ฆฌ์— ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ๊ทธ๋Ÿฐ์ง€ View๊ฐ€ ์ฒ˜๋ฆฌํ•ด๋„ ๊ดœ์ฐฎ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๋‹ค์‹œ ๋ณด๋‹ˆ๊นŒ ๋ง์”€ํ•˜์‹  ๋ถ€๋ถ„์ด ๋งž๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,135 @@ +package christmas; + +import christmas.model.OrderMenu; +import christmas.util.Validator; + +import java.lang.reflect.Array; +import java.util.Map; +import java.util.HashMap; +import java.util.ArrayList; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThat; + +public class ValidatorTest { + private List<Map<String, Integer>> menu = new ArrayList<>(List.of( + Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "ํƒ€ํŒŒ์Šค", 5500, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + Map.of("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "๋ฐ”๋น„ํ๋ฆฝ", 54000, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + Map.of("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "์•„์ด์Šคํฌ๋ฆผ", 5000), + Map.of("์ œ๋กœ์ฝœ๋ผ", 3000, "๋ ˆ๋“œ์™€์ธ", 60000, "์ƒดํŽ˜์ธ", 25000) + )); + + @DisplayName("์ž…๋ ฅ๋œ ๋‚ ์งœ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isDateNoneTest() { + assertThatThrownBy(() -> Validator.validateDate("")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("์ž…๋ ฅ๋œ ๋‚ ์งœ๊ฐ€ ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isDateInvalidTest() { + assertThatThrownBy(() -> Validator.validateDate("1a")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("์ž…๋ ฅ๋œ ๋‚ ์งœ๊ฐ€ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void dateRangeTest() { + assertThatThrownBy(() -> Validator.validateDate("123")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("์ž…๋ ฅ๋œ ์ฃผ๋ฌธ์ด ๋น„์–ด์žˆ์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isOrderNoneTest() { + assertThatThrownBy(() -> Validator.validateOrder("")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("์ž…๋ ฅ๋œ ์ฃผ๋ฌธ ์ค‘ ๋นˆ ๋ฌธ์ž์—ด์ด ์žˆ์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isParsedNoneTest() { + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder(",ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,,๋ ˆ๋“œ์™€์ธ-1")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder(",,,")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("์ฃผ๋ฌธ ํฌ๋งท์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isFormatWrongTest() { + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1-1,์ œ๋กœ์ฝœ๋ผ-1")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1,์ œ๋กœ์ฝœ๋ผ-")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1,---")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("๊ฐœ์ˆ˜์— ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isNotNumberTest() { + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-๋ ˆ๋“œ์™€์ธ")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1,์ œ๋กœ์ฝœ๋ผ-๋ ˆ๋“œ์™€์ธ")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> Validator.validateOrder("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1,์ œ๋กœ์ฝœ๋ผ-2๋ ˆ๋“œ์™€์ธ")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("๋ฉ”๋‰ดํŒ์— ์—†๋Š” ๋ฉ”๋‰ด๋ฅผ ์ฃผ๋ฌธํ•˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isOrderInMenu() { + Map<String, Integer> order1 = Map.of("๋˜ ์–Œ๊ฟ", 1); + Map<String, Integer> order2 = Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 2, "์—ฐ์ €์œก์ฐœ", 1); + Map<String, Integer> order3 = Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 1, "์•„์ด์Šคํฌ๋ฆผ", 1); + + assertThat(Validator.validateMenu(order1, menu)).isEqualTo(true); + assertThat(Validator.validateMenu(order2, menu)).isEqualTo(true); + assertThat(Validator.validateMenu(order3, menu)).isEqualTo(false); + } + + @DisplayName("0๊ฐœ ์ดํ•˜์ธ ๊ฐœ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isCountValid() { + Map<String, Integer> order1 = Map.of("์•„์ด์Šคํฌ๋ฆผ", 0); + Map<String, Integer> order2 = Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 1, "์•„์ด์Šคํฌ๋ฆผ", 0); + + assertThat(Validator.validateMenu(order1, menu)).isEqualTo(true); + assertThat(Validator.validateMenu(order2, menu)).isEqualTo(true); + } + + @DisplayName("20๊ฐœ๊ฐ€ ๋„˜์–ด๊ฐ€๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isUnderTwenty() { + Map<String, Integer> order3 = Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 21); + Map<String, Integer> order4 = Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 10, "์•„์ด์Šคํฌ๋ฆผ", 11); + + assertThat(Validator.validateMenu(order3, menu)).isEqualTo(true); + assertThat(Validator.validateMenu(order4, menu)).isEqualTo(true); + } + + @DisplayName("์Œ๋ฃŒ๋งŒ ์ฃผ๋ฌธํ•˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void isOnlyBeverage() { + Map<String, Integer> order3 = Map.of("์ œ๋กœ์ฝœ๋ผ", 1); + Map<String, Integer> order4 = Map.of("์ œ๋กœ์ฝœ๋ผ", 3, "๋ ˆ๋“œ์™€์ธ", 1); + + assertThat(Validator.validateMenu(order3, menu)).isEqualTo(true); + assertThat(Validator.validateMenu(order4, menu)).isEqualTo(true); + } +}
Java
ํ…Œ์ŠคํŠธ๋„ ๋” ์‹ ๊ฒฝ์จ์•ผ๊ฒ ๊ตฐ์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,39 @@ +package christmas.util; + +public enum Constant { + NOT_IN_MENU(-1), + EPPETIZER(0), + MAINMENU(1), + DESSERT(2), + BEVERAGE(3), + CATEGORY_COUNT(4), + ZERO(0), + ONE(1), + THOUSAND(1000), + HUNDRED(100), + CHRISTMAS_DAY(25), + MIN_BENEFIT_PRICE(10000), + PRESENT_COST(120000), + CHAMPAIGN_PRICE(25000), + WEEKDAYS(7), + FRIDAY(1), + SATURDAY(2), + SUNDAY(3), + EVENT_PRICE(2023), + SANTA(20000), + TREE(10000), + STAR(5000), + MAX_MENU_COUNT(20), + KEY_INDEX(0), + VALUE_INDEX(1); + + private final int value; + + Constant(int value) { + this.value = value; + } + + public int get() { + return this.value; + } +}
Java
์ƒ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค๋Š” ๊ฒƒ์—๋งŒ ์‹ ๊ฒฝ์“ฐ๋‹ค ๋ณด๋‹ˆ ๋ฏธ์ฒ˜ ์ƒ๊ฐ์„ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ํ™•์ธํ•ด๋ณด๋‹ˆ ๊ฐ€๋…์„ฑ ์ธก๋ฉด์—์„œ๋„ ๋งŽ์ด ๋ถˆํŽธํ•œ ๊ฒƒ ๊ฐ™์•„์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,68 @@ +package christmas.model; + +import christmas.util.Constant; +import christmas.util.ErrorMessage; +import christmas.util.Utility; +import christmas.util.Validator; + +import java.util.ArrayList; +import java.util.Map; +import java.util.List; + +public class OrderMenu { + private final List<Map<String, Integer>> menu; + + private int orderDate; + private Map<String, Integer> userOrder; + + public OrderMenu() { + menu = new ArrayList<>(List.of( + // EPPETIZER 0 + Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "ํƒ€ํŒŒ์Šค", 5500, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + // MAINMENU 1 + Map.of("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "๋ฐ”๋น„ํ๋ฆฝ", 54000, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + // DESSERT 2 + Map.of("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "์•„์ด์Šคํฌ๋ฆผ", 5000), + // BEVERAGE 3 + Map.of("์ œ๋กœ์ฝœ๋ผ", 3000, "๋ ˆ๋“œ์™€์ธ", 60000, "์ƒดํŽ˜์ธ", 25000) + )); + } + + public int getOrderDate() { + return this.orderDate; + } + + public Map<String, Integer> getUserOrder() { + return this.userOrder; + } + + public int getTotalPrice() { + int totalPrice = Constant.ZERO.get(); + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + int price = menu.get(category).get(entry.getKey()); + totalPrice += price * entry.getValue(); + } + return totalPrice; + } + + public int[] getCategoryCount() { + int[] categoryCount = new int[Constant.CATEGORY_COUNT.get()]; + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + categoryCount[category] += entry.getValue(); + } + return categoryCount; + } + + public void setOrderDate(int date) { + this.orderDate = date; + } + + public void setUserOrder(Map<String, Integer> userOrder) { + if (Validator.validateMenu(userOrder, this.menu)) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + this.userOrder = userOrder; + } +}
Java
์ €๋„ ์ด ๋ถ€๋ถ„์„ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ ์ข‹์€ ๋ฐฉ์‹์ด ์•„๋‹ˆ๋ผ๊ณ  ๋А๊ผˆ๋Š”๋ฐ, enum์„ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹จ ์ƒ๊ฐ์€ ๋ชปํ–ˆ๋„ค์š”. ํ•ด์šด๋‹˜ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  enum์„ ์ž˜ ์ฒ˜๋ฆฌํ•˜์…จ๋‹ค๊ณ  ๋А๊ผˆ๋Š”๋ฐ ์ฐธ๊ณ ํ•ด์„œ ์ ์šฉํ•ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,68 @@ +package christmas.model; + +import christmas.util.Constant; +import christmas.util.ErrorMessage; +import christmas.util.Utility; +import christmas.util.Validator; + +import java.util.ArrayList; +import java.util.Map; +import java.util.List; + +public class OrderMenu { + private final List<Map<String, Integer>> menu; + + private int orderDate; + private Map<String, Integer> userOrder; + + public OrderMenu() { + menu = new ArrayList<>(List.of( + // EPPETIZER 0 + Map.of("์–‘์†ก์ด์ˆ˜ํ”„", 6000, "ํƒ€ํŒŒ์Šค", 5500, "์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8000), + // MAINMENU 1 + Map.of("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000, "๋ฐ”๋น„ํ๋ฆฝ", 54000, "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000, "ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000), + // DESSERT 2 + Map.of("์ดˆ์ฝ”์ผ€์ดํฌ", 15000, "์•„์ด์Šคํฌ๋ฆผ", 5000), + // BEVERAGE 3 + Map.of("์ œ๋กœ์ฝœ๋ผ", 3000, "๋ ˆ๋“œ์™€์ธ", 60000, "์ƒดํŽ˜์ธ", 25000) + )); + } + + public int getOrderDate() { + return this.orderDate; + } + + public Map<String, Integer> getUserOrder() { + return this.userOrder; + } + + public int getTotalPrice() { + int totalPrice = Constant.ZERO.get(); + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + int price = menu.get(category).get(entry.getKey()); + totalPrice += price * entry.getValue(); + } + return totalPrice; + } + + public int[] getCategoryCount() { + int[] categoryCount = new int[Constant.CATEGORY_COUNT.get()]; + for (Map.Entry<String, Integer> entry : this.userOrder.entrySet()) { + int category = Utility.getCategory(entry.getKey(), menu); + categoryCount[category] += entry.getValue(); + } + return categoryCount; + } + + public void setOrderDate(int date) { + this.orderDate = date; + } + + public void setUserOrder(Map<String, Integer> userOrder) { + if (Validator.validateMenu(userOrder, this.menu)) { + throw new IllegalArgumentException(ErrorMessage.ORDER_ERROR.get()); + } + this.userOrder = userOrder; + } +}
Java
๋ง์”€ํ•ด์ฃผ์‹  ๋ถ€๋ถ„ ๋‹ค์‹œ ๋ณด๋‹ˆ๊นŒ ํด๋ž˜์Šค ์ด๋ฆ„์ด ๋‚ด์šฉ์„ ๋‹ค ํฌ๊ด„ํ•˜์ง€ ๋ชปํ•ด ์–ด์ƒ‰ํ•˜๊ฒŒ ๋А๊ปด์ง€๋„ค์š”. ์ฃผ๋ฌธํ•œ ๋ถ€๋ถ„์„ ๋‹ค ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋‚ ์งœ๊ฐ€ ํ•„์š”ํ•œ๋ฐ ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•ด๋ณด๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,47 @@ +package christmas.controller; + +import java.util.Map; + +import christmas.view.*; +import christmas.model.*; + +public class MainController { + private OrderMenu orderMenu; + + public MainController() { + orderMenu = new OrderMenu(); + } + + public void run() { + orderMenu.setOrderDate(InputView.readDate()); + setReadOrder(); + + OutputView.printPreview(orderMenu.getOrderDate()); + OutputView.printOrder(orderMenu.getUserOrder()); + int totalPrice = orderMenu.getTotalPrice(); + OutputView.printTotalPrice(totalPrice); + OutputView.printPresent(totalPrice); + benefitControl(totalPrice); + } + + private void benefitControl(int totalPrice) { + Benefit benefit = new Benefit(orderMenu.getOrderDate(), orderMenu.getCategoryCount(), totalPrice); + OutputView.printBenefits(benefit.getBenefits()); + + int totalBenefitPrice = benefit.getTotalBenefitPrice(); + OutputView.printTotalBenefitPrice(totalBenefitPrice); + OutputView.printTotalPayment(totalPrice - totalBenefitPrice + benefit.getPresentPrice()); + OutputView.printEventBadge(totalBenefitPrice); + } + + private void setReadOrder() { + Map<String, Integer> orderMenu; + try { + orderMenu = InputView.readOrder(); + this.orderMenu.setUserOrder(orderMenu); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + setReadOrder(); + } + } +}
Java
MVC ๊ฐ๊ฐ์€ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ๋Š” ์š”์†Œ๋‹ค ๋ณด๋‹ˆ๊นŒ ๊ฐ๊ฐ์„ ๋ถ„๋ฆฌ๋œ ๊ฐ์ฒด๋กœ ๋ณด๊ณ  ์ธ์Šคํ„ด์Šคํ™”ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”. ๋ฌผ๋ก  ์ด๊ฒŒ ์ •๋‹ต์ด๋ผ๋Š” ๊ฑด ์•„๋‹™๋‹ˆ๋‹ค. ๋ง์”€ํ•˜์‹ ๋Œ€๋กœ ์ƒํƒœ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š์œผ๋‹ˆ ์Šคํƒœํ‹ฑ ๋ฉ”์„œ๋“œ๋กœ ๋งŒ๋“œ์‹  ๊ฒƒ๋„ ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ, ์Šคํƒœํ‹ฑ ๋ฉ”์„œ๋“œ๋งŒ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ฌธ์ œ๋ฅผ ์ฐพ์•„๋ดค๋Š”๋ฐ, ๋งŽ์€ ๊ฒฝ์šฐ ๋ทฐ์—์„œ ์ƒํƒœ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์œผ๋ฉฐ, ์Šคํƒœํ‹ฑ ๋ฉ”์„œ๋“œ์˜ ๊ฒฝ์šฐ ํ…Œ์ŠคํŠธ ์‹œ ๋ชฉ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ธฐ ์–ด๋ ต๊ณ  ์ƒ์†์ด๋‚˜ ๋‹คํ˜•์„ฑ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ค์›Œ์„œ ํ™•์žฅ์„ฑ์ด๋‚˜ ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์ง„๋‹ค๊ณ  ํ•˜๋„ค์š”.
@@ -1 +1,127 @@ # java-chess ๊ฒŒ์ž„ + +## ๊ตฌํ˜„ํ•ด์•ผํ•  ๋ชฉ๋ก + +### ์ž…๋ ฅ + +- [x] ๋ช…๋ น์–ด ์ž…๋ ฅ + - [x] ์˜ˆ์™ธ: `null` ์ž…๋ ฅ + +### ์ถœ๋ ฅ + +- [x] ์‹œ์ž‘ ์•ˆ๋‚ด ๋ฌธ๊ตฌ ์ถœ๋ ฅ +- [x] ์ฒด์ŠคํŒ ์Šค๋ƒ…์ƒท ์ถœ๋ ฅ +- [x] ์ฐจ๋ก€ ์ถœ๋ ฅ +- [x] ์ ์ˆ˜ ์ถœ๋ ฅ +- [x] ์Šน์ž ์ถœ๋ ฅ + +### ์„œ๋น„์Šค + +#### `ChessService` + +- [x] `Command`์— ๋”ฐ๋ผ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰ +- [x] ๊ฒŒ์ž„ ํ˜„์žฌ ์ƒํ™ฉ DTO + +### ๋„๋ฉ”์ธ + +#### `Operation` + +- [x] ๋ช…๋ น์–ด ๊ฒ€์ฆ + - [x] start: ๊ฒŒ์ž„ ์‹คํ–‰ + - [x] status: ํŒ€๋ณ„ ์ ์ˆ˜ + - [x] move: ๊ธฐ๋ฌผ ์ด๋™ + - [x] end: ๊ฐ•์ œ ์ข…๋ฃŒ + +#### `Board` + +- [x] ๊ธฐ๋ฌผ ์ด๋™ + - [x] ์˜ˆ์™ธ + - [x] ์›€์ง์ผ ๊ธฐ๋ฌผ์ด ์ž์‹  ํŒ€ ์†Œ์œ ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ + - [x] `source`์™€ `target`์ด ๊ฐ™์€ ์œ„์น˜์ธ ๊ฒฝ์šฐ + - [x] ๋ณธ์ธ์˜ ๊ธฐ๋ฌผ์„ ๊ณต๊ฒฉํ•˜๋Š” ๊ฒฝ์šฐ + - [x] ์ด๋™ ๊ฒฝ๋กœ๊ฐ€ ๋‹ค๋ฅธ ๊ธฐ๋ฌผ์— ๊ฐ€๋กœ๋ง‰ํžŒ ๊ฒฝ์šฐ + - [x] `King`: `target`์ด ์ ์ด ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ์œ„์น˜์ธ ๊ฒฝ์šฐ + - [x] `Pawn`: `target`์— ์ ์ด ์—†๋Š”๋ฐ ๋Œ€๊ฐ์„ ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ + - [x] ์ž์‹ ์˜ ๊ธฐ๋ฌผ ์ด๋™ + - [x] ๊ณต๊ฒฉ์ธ ๊ฒฝ์šฐ ์  ๊ธฐ๋ฌผ ์ œ๊ฑฐ + - [x] ์ฐจ๋ก€ ๋ณ€๊ฒฝ +- [x] ํ˜„์žฌ ์ฐจ๋ก€ ๊ด€๋ฆฌ +- [x] ์œ„์น˜ ์ •๋ณด + - [x] ํŠน์ • ์œ„์น˜๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ํ™•์ธ + - [x] ํŠน์ • ์œ„์น˜์— ์žˆ๋Š” ๊ธฐ๋ฌผ ํ™•์ธ +- [x] ๊ฐ ํŒ€ ์ ์ˆ˜ ๊ณ„์‚ฐ +- [x] ์Šน์ž ํ™•์ธ + - [x] ์˜ˆ์™ธ: ๋‘ `King`์ด ๋ชจ๋‘ ์‚ด์•„ ์žˆ๋Š” ๊ฒฝ์šฐ + +#### `Team` + +- [x] ๊ธฐ๋ฌผ ์ด๋™ +- [x] ๊ธฐ๋ฌผ ์ด๋™ ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ํ˜„์œ„์น˜์—์„œ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ์ ์—๊ฒŒ ๊ณต๊ฒฉ๋ฐ›์€ ๊ฒฝ์šฐ ๊ธฐ๋ฌผ ์ œ๊ฑฐ +- [x] ๊ธฐ๋ฌผ ๊ฒ€์ƒ‰ +- [x] ์ ์ˆ˜ ๊ณ„์‚ฐ + +#### `Piece` + +- [x] ์ด๋™ ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ํ˜„์œ„์น˜์—์„œ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ์ƒ‰์ƒ ํ™•์ธ +- [x] ํƒ€์ž… ํ™•์ธ + - [x] `Pawn` + - [x] `King` + +#### `MovePattern` + +- [x] ๊ฐ ๊ธฐ๋ฌผ ํƒ€์ž…์— ๋งž๋Š” ํŒจํ„ด ๊ฒ€์ƒ‰ + - [x] `Pawn`์€ ์ƒ‰์ƒ์— ๋งž๋Š” ํŒจํ„ด ๊ฒ€์ƒ‰ +- [x] ๋ณธ์ธ ํŒจํ„ด ์ค‘ `gap`์— ๋งž๋Š” `MoveUnit` ๊ฒ€์ƒ‰ +- [x] ๋ณธ์ธ ํŒจํ„ด์œผ๋กœ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ + +#### `MoveLimit` + +- [x] ํ•œ ์นธ๋งŒ ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ + +#### `MoveUnits` + +- [x] ๋ณธ์ธ ๋ฆฌ์ŠคํŠธ์—์„œ ๋งค์นญ๋˜๋Š” `MoveUnit` ๊ฒ€์ƒ‰ + - [x] ์˜ˆ์™ธ: ๋งค์นญ๋˜๋Š” `MoveUnit`์ด ์—†๋Š” ๊ฒฝ์šฐ +- [x] ์ด๋™ ๋‹จ์œ„ ๋ณ„๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ + +#### `MoveUnit` + +- [x] `MoveUnit`์˜ ์ขŒํ‘œ๊ฐ€ `gap`์— ๋งค์นญ๋˜๋Š”์ง€ ํ™•์ธ + +#### `Position` + +- [x] 64๊ฐœ ์œ„์น˜ ์บ์‹ฑ ๋ฐ ๊ฒ€์ƒ‰ +- [x] `Gap` ๊ณ„์‚ฐ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ํƒ€๊ฒŸ๊นŒ์ง€ ์ด๋™ํ•  ๋•Œ ์ง€๋‚˜๊ฐ€๋Š” ๋ชจ๋“  ์œ„์น˜ ๊ฒ€์ƒ‰ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ ์ฒด์ŠคํŒ์˜ ๋ชจ๋“  ์œ„์น˜ ๊ฒ€์ƒ‰ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ํ•œ ๋ฒˆ ๋” ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ์ด๋™ + +## ์ด๋™ ๊ทœ์น™ + +- [x] ํฐ (1 or 0.5) + - [x] ์  ๋ฐฉํ–ฅ ์ง์„  1์นธ ์ด๋™ + - [x] ์ฒ˜์Œ ์ด๋™ ์‹œ์—๋Š” 2์นธ ์ด๋™ ๊ฐ€๋Šฅ + - [x] ๊ณต๊ฒฉ ์‹œ์—๋Š” ์  ๋ฐฉํ–ฅ ์ขŒ, ์šฐ ๋Œ€๊ฐ์„  1์นธ ์ด๋™ + +- [x] ๋ฃฉ (5) + - [x] ๋ชจ๋“  ์ง์„  ๋ฐฉํ–ฅ์œผ๋กœ ์›ํ•˜๋Š” ๋งŒํผ ์ด๋™ + +- [x] ๋‚˜์ดํŠธ (2.5) + - [x] ๋ชจ๋“  ์ง์„  ๋ฐฉํ–ฅ 1์นธ + ์ด๋™ํ•œ ์ง์„  ๋ฐฉํ–ฅ์˜ ์ขŒ, ์šฐ ๋Œ€๊ฐ์„  1์นธ์œผ๋กœ ์ด๋™ + - [x] ์ง„ํ–‰ ๋ฐฉํ–ฅ์ด ๊ฐ€๋กœ๋ง‰ํ˜€๋„ ์ , ์•„๊ตฐ ์ƒ๊ด€์—†์ด ๋›ฐ์–ด๋„˜์„ ์ˆ˜ ์žˆ์Œ + +- [x] ๋น„์ˆ (3) + - [x] ๋ชจ๋“  ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์œผ๋กœ ์›ํ•˜๋Š” ๋งŒํผ ์ด๋™ + +- [x] ํ€ธ (9) + - [x] ๋ชจ๋“  ๋ฐฉํ–ฅ์œผ๋กœ ์›ํ•˜๋Š” ๋งŒํผ ์ด๋™ + +- [x] ํ‚น + - [x] ๋ชจ๋“  ๋ฐฉํ–ฅ 1์นธ ์ด๋™ + - [x] ์ƒ๋Œ€์˜ ๊ณต๊ฒฉ ๋ฒ”์œ„๋กœ๋Š” ์ด๋™ ๋ถˆ๊ฐ€๋Šฅ +
Unknown
๊ฐ ๊ธฐ๋ฌผ ๋’ค์— ๋‚˜์˜จ ์ˆซ์ž๊ฐ€ ์ ์ˆ˜์ธ ๊ฒƒ์œผ๋กœ ๋ณด์ด๋Š”๋ฐ, ์ ์ˆ˜ ๊ณ„์‚ฐ์— ๋Œ€ํ•œ ๊ฒƒ๋„ ๊ธฐ๋Šฅ ๋ชฉ๋ก์œผ๋กœ ๋นผ๋ฉด ์–ด๋–จ๊นŒ์š”? ํ•ด๋‹น ์ฝ”๋“œ๋งŒ ๋ณด๊ณ  ์ดํ•ดํ•˜๋Š” ์‚ฌ๋žŒ์—๊ฒŒ๋Š” ์–ด๋–ค ์ˆซ์ž๋ฅผ ์˜๋ฏธํ•˜๋Š”์ง€ ์•Œ๊ธฐ๊ฐ€ ์–ด๋ ค์šธ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -1 +1,127 @@ # java-chess ๊ฒŒ์ž„ + +## ๊ตฌํ˜„ํ•ด์•ผํ•  ๋ชฉ๋ก + +### ์ž…๋ ฅ + +- [x] ๋ช…๋ น์–ด ์ž…๋ ฅ + - [x] ์˜ˆ์™ธ: `null` ์ž…๋ ฅ + +### ์ถœ๋ ฅ + +- [x] ์‹œ์ž‘ ์•ˆ๋‚ด ๋ฌธ๊ตฌ ์ถœ๋ ฅ +- [x] ์ฒด์ŠคํŒ ์Šค๋ƒ…์ƒท ์ถœ๋ ฅ +- [x] ์ฐจ๋ก€ ์ถœ๋ ฅ +- [x] ์ ์ˆ˜ ์ถœ๋ ฅ +- [x] ์Šน์ž ์ถœ๋ ฅ + +### ์„œ๋น„์Šค + +#### `ChessService` + +- [x] `Command`์— ๋”ฐ๋ผ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰ +- [x] ๊ฒŒ์ž„ ํ˜„์žฌ ์ƒํ™ฉ DTO + +### ๋„๋ฉ”์ธ + +#### `Operation` + +- [x] ๋ช…๋ น์–ด ๊ฒ€์ฆ + - [x] start: ๊ฒŒ์ž„ ์‹คํ–‰ + - [x] status: ํŒ€๋ณ„ ์ ์ˆ˜ + - [x] move: ๊ธฐ๋ฌผ ์ด๋™ + - [x] end: ๊ฐ•์ œ ์ข…๋ฃŒ + +#### `Board` + +- [x] ๊ธฐ๋ฌผ ์ด๋™ + - [x] ์˜ˆ์™ธ + - [x] ์›€์ง์ผ ๊ธฐ๋ฌผ์ด ์ž์‹  ํŒ€ ์†Œ์œ ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ + - [x] `source`์™€ `target`์ด ๊ฐ™์€ ์œ„์น˜์ธ ๊ฒฝ์šฐ + - [x] ๋ณธ์ธ์˜ ๊ธฐ๋ฌผ์„ ๊ณต๊ฒฉํ•˜๋Š” ๊ฒฝ์šฐ + - [x] ์ด๋™ ๊ฒฝ๋กœ๊ฐ€ ๋‹ค๋ฅธ ๊ธฐ๋ฌผ์— ๊ฐ€๋กœ๋ง‰ํžŒ ๊ฒฝ์šฐ + - [x] `King`: `target`์ด ์ ์ด ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ์œ„์น˜์ธ ๊ฒฝ์šฐ + - [x] `Pawn`: `target`์— ์ ์ด ์—†๋Š”๋ฐ ๋Œ€๊ฐ์„ ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ + - [x] ์ž์‹ ์˜ ๊ธฐ๋ฌผ ์ด๋™ + - [x] ๊ณต๊ฒฉ์ธ ๊ฒฝ์šฐ ์  ๊ธฐ๋ฌผ ์ œ๊ฑฐ + - [x] ์ฐจ๋ก€ ๋ณ€๊ฒฝ +- [x] ํ˜„์žฌ ์ฐจ๋ก€ ๊ด€๋ฆฌ +- [x] ์œ„์น˜ ์ •๋ณด + - [x] ํŠน์ • ์œ„์น˜๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ํ™•์ธ + - [x] ํŠน์ • ์œ„์น˜์— ์žˆ๋Š” ๊ธฐ๋ฌผ ํ™•์ธ +- [x] ๊ฐ ํŒ€ ์ ์ˆ˜ ๊ณ„์‚ฐ +- [x] ์Šน์ž ํ™•์ธ + - [x] ์˜ˆ์™ธ: ๋‘ `King`์ด ๋ชจ๋‘ ์‚ด์•„ ์žˆ๋Š” ๊ฒฝ์šฐ + +#### `Team` + +- [x] ๊ธฐ๋ฌผ ์ด๋™ +- [x] ๊ธฐ๋ฌผ ์ด๋™ ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ํ˜„์œ„์น˜์—์„œ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ์ ์—๊ฒŒ ๊ณต๊ฒฉ๋ฐ›์€ ๊ฒฝ์šฐ ๊ธฐ๋ฌผ ์ œ๊ฑฐ +- [x] ๊ธฐ๋ฌผ ๊ฒ€์ƒ‰ +- [x] ์ ์ˆ˜ ๊ณ„์‚ฐ + +#### `Piece` + +- [x] ์ด๋™ ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ํ˜„์œ„์น˜์—์„œ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ์ƒ‰์ƒ ํ™•์ธ +- [x] ํƒ€์ž… ํ™•์ธ + - [x] `Pawn` + - [x] `King` + +#### `MovePattern` + +- [x] ๊ฐ ๊ธฐ๋ฌผ ํƒ€์ž…์— ๋งž๋Š” ํŒจํ„ด ๊ฒ€์ƒ‰ + - [x] `Pawn`์€ ์ƒ‰์ƒ์— ๋งž๋Š” ํŒจํ„ด ๊ฒ€์ƒ‰ +- [x] ๋ณธ์ธ ํŒจํ„ด ์ค‘ `gap`์— ๋งž๋Š” `MoveUnit` ๊ฒ€์ƒ‰ +- [x] ๋ณธ์ธ ํŒจํ„ด์œผ๋กœ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ + +#### `MoveLimit` + +- [x] ํ•œ ์นธ๋งŒ ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ + +#### `MoveUnits` + +- [x] ๋ณธ์ธ ๋ฆฌ์ŠคํŠธ์—์„œ ๋งค์นญ๋˜๋Š” `MoveUnit` ๊ฒ€์ƒ‰ + - [x] ์˜ˆ์™ธ: ๋งค์นญ๋˜๋Š” `MoveUnit`์ด ์—†๋Š” ๊ฒฝ์šฐ +- [x] ์ด๋™ ๋‹จ์œ„ ๋ณ„๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ + +#### `MoveUnit` + +- [x] `MoveUnit`์˜ ์ขŒํ‘œ๊ฐ€ `gap`์— ๋งค์นญ๋˜๋Š”์ง€ ํ™•์ธ + +#### `Position` + +- [x] 64๊ฐœ ์œ„์น˜ ์บ์‹ฑ ๋ฐ ๊ฒ€์ƒ‰ +- [x] `Gap` ๊ณ„์‚ฐ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ํƒ€๊ฒŸ๊นŒ์ง€ ์ด๋™ํ•  ๋•Œ ์ง€๋‚˜๊ฐ€๋Š” ๋ชจ๋“  ์œ„์น˜ ๊ฒ€์ƒ‰ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ ์ฒด์ŠคํŒ์˜ ๋ชจ๋“  ์œ„์น˜ ๊ฒ€์ƒ‰ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ํ•œ ๋ฒˆ ๋” ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ์ด๋™ + +## ์ด๋™ ๊ทœ์น™ + +- [x] ํฐ (1 or 0.5) + - [x] ์  ๋ฐฉํ–ฅ ์ง์„  1์นธ ์ด๋™ + - [x] ์ฒ˜์Œ ์ด๋™ ์‹œ์—๋Š” 2์นธ ์ด๋™ ๊ฐ€๋Šฅ + - [x] ๊ณต๊ฒฉ ์‹œ์—๋Š” ์  ๋ฐฉํ–ฅ ์ขŒ, ์šฐ ๋Œ€๊ฐ์„  1์นธ ์ด๋™ + +- [x] ๋ฃฉ (5) + - [x] ๋ชจ๋“  ์ง์„  ๋ฐฉํ–ฅ์œผ๋กœ ์›ํ•˜๋Š” ๋งŒํผ ์ด๋™ + +- [x] ๋‚˜์ดํŠธ (2.5) + - [x] ๋ชจ๋“  ์ง์„  ๋ฐฉํ–ฅ 1์นธ + ์ด๋™ํ•œ ์ง์„  ๋ฐฉํ–ฅ์˜ ์ขŒ, ์šฐ ๋Œ€๊ฐ์„  1์นธ์œผ๋กœ ์ด๋™ + - [x] ์ง„ํ–‰ ๋ฐฉํ–ฅ์ด ๊ฐ€๋กœ๋ง‰ํ˜€๋„ ์ , ์•„๊ตฐ ์ƒ๊ด€์—†์ด ๋›ฐ์–ด๋„˜์„ ์ˆ˜ ์žˆ์Œ + +- [x] ๋น„์ˆ (3) + - [x] ๋ชจ๋“  ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์œผ๋กœ ์›ํ•˜๋Š” ๋งŒํผ ์ด๋™ + +- [x] ํ€ธ (9) + - [x] ๋ชจ๋“  ๋ฐฉํ–ฅ์œผ๋กœ ์›ํ•˜๋Š” ๋งŒํผ ์ด๋™ + +- [x] ํ‚น + - [x] ๋ชจ๋“  ๋ฐฉํ–ฅ 1์นธ ์ด๋™ + - [x] ์ƒ๋Œ€์˜ ๊ณต๊ฒฉ ๋ฒ”์œ„๋กœ๋Š” ์ด๋™ ๋ถˆ๊ฐ€๋Šฅ +
Unknown
ํ€ธ์˜ ์ด๋™๋ฐฉํ–ฅ์€ ์ƒํ•˜์ขŒ์šฐ๋Œ€๊ฐ์„  ๋ชจ๋‘ ์›ํ•˜๋Š”๋งŒํผ ์ด๋™์ธ ๊ฒƒ์œผ๋กœ ์•Œ๊ณ  ์žˆ๋Š”๋ฐ, ์ƒํ•˜์ขŒ์šฐ๋Š” 1์นธ์”ฉ๋ฐ–์— ์ด๋™ํ•˜์ง€ ๋ชปํ•˜๋Š”๊ฑด๊ฐ€์š”? ํ€ธ์˜ ์ด๋™ ๊ทœ์น™์„ ํ•œ ๋ฒˆ ๋‹ค์‹œ ๋ด์ฃผ์‹œ๊ฒ ์–ด์š”?
@@ -1 +1,127 @@ # java-chess ๊ฒŒ์ž„ + +## ๊ตฌํ˜„ํ•ด์•ผํ•  ๋ชฉ๋ก + +### ์ž…๋ ฅ + +- [x] ๋ช…๋ น์–ด ์ž…๋ ฅ + - [x] ์˜ˆ์™ธ: `null` ์ž…๋ ฅ + +### ์ถœ๋ ฅ + +- [x] ์‹œ์ž‘ ์•ˆ๋‚ด ๋ฌธ๊ตฌ ์ถœ๋ ฅ +- [x] ์ฒด์ŠคํŒ ์Šค๋ƒ…์ƒท ์ถœ๋ ฅ +- [x] ์ฐจ๋ก€ ์ถœ๋ ฅ +- [x] ์ ์ˆ˜ ์ถœ๋ ฅ +- [x] ์Šน์ž ์ถœ๋ ฅ + +### ์„œ๋น„์Šค + +#### `ChessService` + +- [x] `Command`์— ๋”ฐ๋ผ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰ +- [x] ๊ฒŒ์ž„ ํ˜„์žฌ ์ƒํ™ฉ DTO + +### ๋„๋ฉ”์ธ + +#### `Operation` + +- [x] ๋ช…๋ น์–ด ๊ฒ€์ฆ + - [x] start: ๊ฒŒ์ž„ ์‹คํ–‰ + - [x] status: ํŒ€๋ณ„ ์ ์ˆ˜ + - [x] move: ๊ธฐ๋ฌผ ์ด๋™ + - [x] end: ๊ฐ•์ œ ์ข…๋ฃŒ + +#### `Board` + +- [x] ๊ธฐ๋ฌผ ์ด๋™ + - [x] ์˜ˆ์™ธ + - [x] ์›€์ง์ผ ๊ธฐ๋ฌผ์ด ์ž์‹  ํŒ€ ์†Œ์œ ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ + - [x] `source`์™€ `target`์ด ๊ฐ™์€ ์œ„์น˜์ธ ๊ฒฝ์šฐ + - [x] ๋ณธ์ธ์˜ ๊ธฐ๋ฌผ์„ ๊ณต๊ฒฉํ•˜๋Š” ๊ฒฝ์šฐ + - [x] ์ด๋™ ๊ฒฝ๋กœ๊ฐ€ ๋‹ค๋ฅธ ๊ธฐ๋ฌผ์— ๊ฐ€๋กœ๋ง‰ํžŒ ๊ฒฝ์šฐ + - [x] `King`: `target`์ด ์ ์ด ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ์œ„์น˜์ธ ๊ฒฝ์šฐ + - [x] `Pawn`: `target`์— ์ ์ด ์—†๋Š”๋ฐ ๋Œ€๊ฐ์„ ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ + - [x] ์ž์‹ ์˜ ๊ธฐ๋ฌผ ์ด๋™ + - [x] ๊ณต๊ฒฉ์ธ ๊ฒฝ์šฐ ์  ๊ธฐ๋ฌผ ์ œ๊ฑฐ + - [x] ์ฐจ๋ก€ ๋ณ€๊ฒฝ +- [x] ํ˜„์žฌ ์ฐจ๋ก€ ๊ด€๋ฆฌ +- [x] ์œ„์น˜ ์ •๋ณด + - [x] ํŠน์ • ์œ„์น˜๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ํ™•์ธ + - [x] ํŠน์ • ์œ„์น˜์— ์žˆ๋Š” ๊ธฐ๋ฌผ ํ™•์ธ +- [x] ๊ฐ ํŒ€ ์ ์ˆ˜ ๊ณ„์‚ฐ +- [x] ์Šน์ž ํ™•์ธ + - [x] ์˜ˆ์™ธ: ๋‘ `King`์ด ๋ชจ๋‘ ์‚ด์•„ ์žˆ๋Š” ๊ฒฝ์šฐ + +#### `Team` + +- [x] ๊ธฐ๋ฌผ ์ด๋™ +- [x] ๊ธฐ๋ฌผ ์ด๋™ ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ํ˜„์œ„์น˜์—์„œ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ์ ์—๊ฒŒ ๊ณต๊ฒฉ๋ฐ›์€ ๊ฒฝ์šฐ ๊ธฐ๋ฌผ ์ œ๊ฑฐ +- [x] ๊ธฐ๋ฌผ ๊ฒ€์ƒ‰ +- [x] ์ ์ˆ˜ ๊ณ„์‚ฐ + +#### `Piece` + +- [x] ์ด๋™ ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ํ˜„์œ„์น˜์—์„œ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ์ƒ‰์ƒ ํ™•์ธ +- [x] ํƒ€์ž… ํ™•์ธ + - [x] `Pawn` + - [x] `King` + +#### `MovePattern` + +- [x] ๊ฐ ๊ธฐ๋ฌผ ํƒ€์ž…์— ๋งž๋Š” ํŒจํ„ด ๊ฒ€์ƒ‰ + - [x] `Pawn`์€ ์ƒ‰์ƒ์— ๋งž๋Š” ํŒจํ„ด ๊ฒ€์ƒ‰ +- [x] ๋ณธ์ธ ํŒจํ„ด ์ค‘ `gap`์— ๋งž๋Š” `MoveUnit` ๊ฒ€์ƒ‰ +- [x] ๋ณธ์ธ ํŒจํ„ด์œผ๋กœ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ + +#### `MoveLimit` + +- [x] ํ•œ ์นธ๋งŒ ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ + +#### `MoveUnits` + +- [x] ๋ณธ์ธ ๋ฆฌ์ŠคํŠธ์—์„œ ๋งค์นญ๋˜๋Š” `MoveUnit` ๊ฒ€์ƒ‰ + - [x] ์˜ˆ์™ธ: ๋งค์นญ๋˜๋Š” `MoveUnit`์ด ์—†๋Š” ๊ฒฝ์šฐ +- [x] ์ด๋™ ๋‹จ์œ„ ๋ณ„๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ + +#### `MoveUnit` + +- [x] `MoveUnit`์˜ ์ขŒํ‘œ๊ฐ€ `gap`์— ๋งค์นญ๋˜๋Š”์ง€ ํ™•์ธ + +#### `Position` + +- [x] 64๊ฐœ ์œ„์น˜ ์บ์‹ฑ ๋ฐ ๊ฒ€์ƒ‰ +- [x] `Gap` ๊ณ„์‚ฐ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ํƒ€๊ฒŸ๊นŒ์ง€ ์ด๋™ํ•  ๋•Œ ์ง€๋‚˜๊ฐ€๋Š” ๋ชจ๋“  ์œ„์น˜ ๊ฒ€์ƒ‰ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ ์ฒด์ŠคํŒ์˜ ๋ชจ๋“  ์œ„์น˜ ๊ฒ€์ƒ‰ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ํ•œ ๋ฒˆ ๋” ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ์ด๋™ + +## ์ด๋™ ๊ทœ์น™ + +- [x] ํฐ (1 or 0.5) + - [x] ์  ๋ฐฉํ–ฅ ์ง์„  1์นธ ์ด๋™ + - [x] ์ฒ˜์Œ ์ด๋™ ์‹œ์—๋Š” 2์นธ ์ด๋™ ๊ฐ€๋Šฅ + - [x] ๊ณต๊ฒฉ ์‹œ์—๋Š” ์  ๋ฐฉํ–ฅ ์ขŒ, ์šฐ ๋Œ€๊ฐ์„  1์นธ ์ด๋™ + +- [x] ๋ฃฉ (5) + - [x] ๋ชจ๋“  ์ง์„  ๋ฐฉํ–ฅ์œผ๋กœ ์›ํ•˜๋Š” ๋งŒํผ ์ด๋™ + +- [x] ๋‚˜์ดํŠธ (2.5) + - [x] ๋ชจ๋“  ์ง์„  ๋ฐฉํ–ฅ 1์นธ + ์ด๋™ํ•œ ์ง์„  ๋ฐฉํ–ฅ์˜ ์ขŒ, ์šฐ ๋Œ€๊ฐ์„  1์นธ์œผ๋กœ ์ด๋™ + - [x] ์ง„ํ–‰ ๋ฐฉํ–ฅ์ด ๊ฐ€๋กœ๋ง‰ํ˜€๋„ ์ , ์•„๊ตฐ ์ƒ๊ด€์—†์ด ๋›ฐ์–ด๋„˜์„ ์ˆ˜ ์žˆ์Œ + +- [x] ๋น„์ˆ (3) + - [x] ๋ชจ๋“  ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์œผ๋กœ ์›ํ•˜๋Š” ๋งŒํผ ์ด๋™ + +- [x] ํ€ธ (9) + - [x] ๋ชจ๋“  ๋ฐฉํ–ฅ์œผ๋กœ ์›ํ•˜๋Š” ๋งŒํผ ์ด๋™ + +- [x] ํ‚น + - [x] ๋ชจ๋“  ๋ฐฉํ–ฅ 1์นธ ์ด๋™ + - [x] ์ƒ๋Œ€์˜ ๊ณต๊ฒฉ ๋ฒ”์œ„๋กœ๋Š” ์ด๋™ ๋ถˆ๊ฐ€๋Šฅ +
Unknown
๋„ต! Player ๊ธฐ๋Šฅ๋ชฉ๋ก์— ์ ์ˆ˜ ๊ณ„์‚ฐ๋„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค
@@ -1 +1,127 @@ # java-chess ๊ฒŒ์ž„ + +## ๊ตฌํ˜„ํ•ด์•ผํ•  ๋ชฉ๋ก + +### ์ž…๋ ฅ + +- [x] ๋ช…๋ น์–ด ์ž…๋ ฅ + - [x] ์˜ˆ์™ธ: `null` ์ž…๋ ฅ + +### ์ถœ๋ ฅ + +- [x] ์‹œ์ž‘ ์•ˆ๋‚ด ๋ฌธ๊ตฌ ์ถœ๋ ฅ +- [x] ์ฒด์ŠคํŒ ์Šค๋ƒ…์ƒท ์ถœ๋ ฅ +- [x] ์ฐจ๋ก€ ์ถœ๋ ฅ +- [x] ์ ์ˆ˜ ์ถœ๋ ฅ +- [x] ์Šน์ž ์ถœ๋ ฅ + +### ์„œ๋น„์Šค + +#### `ChessService` + +- [x] `Command`์— ๋”ฐ๋ผ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰ +- [x] ๊ฒŒ์ž„ ํ˜„์žฌ ์ƒํ™ฉ DTO + +### ๋„๋ฉ”์ธ + +#### `Operation` + +- [x] ๋ช…๋ น์–ด ๊ฒ€์ฆ + - [x] start: ๊ฒŒ์ž„ ์‹คํ–‰ + - [x] status: ํŒ€๋ณ„ ์ ์ˆ˜ + - [x] move: ๊ธฐ๋ฌผ ์ด๋™ + - [x] end: ๊ฐ•์ œ ์ข…๋ฃŒ + +#### `Board` + +- [x] ๊ธฐ๋ฌผ ์ด๋™ + - [x] ์˜ˆ์™ธ + - [x] ์›€์ง์ผ ๊ธฐ๋ฌผ์ด ์ž์‹  ํŒ€ ์†Œ์œ ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ + - [x] `source`์™€ `target`์ด ๊ฐ™์€ ์œ„์น˜์ธ ๊ฒฝ์šฐ + - [x] ๋ณธ์ธ์˜ ๊ธฐ๋ฌผ์„ ๊ณต๊ฒฉํ•˜๋Š” ๊ฒฝ์šฐ + - [x] ์ด๋™ ๊ฒฝ๋กœ๊ฐ€ ๋‹ค๋ฅธ ๊ธฐ๋ฌผ์— ๊ฐ€๋กœ๋ง‰ํžŒ ๊ฒฝ์šฐ + - [x] `King`: `target`์ด ์ ์ด ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ์œ„์น˜์ธ ๊ฒฝ์šฐ + - [x] `Pawn`: `target`์— ์ ์ด ์—†๋Š”๋ฐ ๋Œ€๊ฐ์„ ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ + - [x] ์ž์‹ ์˜ ๊ธฐ๋ฌผ ์ด๋™ + - [x] ๊ณต๊ฒฉ์ธ ๊ฒฝ์šฐ ์  ๊ธฐ๋ฌผ ์ œ๊ฑฐ + - [x] ์ฐจ๋ก€ ๋ณ€๊ฒฝ +- [x] ํ˜„์žฌ ์ฐจ๋ก€ ๊ด€๋ฆฌ +- [x] ์œ„์น˜ ์ •๋ณด + - [x] ํŠน์ • ์œ„์น˜๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ํ™•์ธ + - [x] ํŠน์ • ์œ„์น˜์— ์žˆ๋Š” ๊ธฐ๋ฌผ ํ™•์ธ +- [x] ๊ฐ ํŒ€ ์ ์ˆ˜ ๊ณ„์‚ฐ +- [x] ์Šน์ž ํ™•์ธ + - [x] ์˜ˆ์™ธ: ๋‘ `King`์ด ๋ชจ๋‘ ์‚ด์•„ ์žˆ๋Š” ๊ฒฝ์šฐ + +#### `Team` + +- [x] ๊ธฐ๋ฌผ ์ด๋™ +- [x] ๊ธฐ๋ฌผ ์ด๋™ ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ํ˜„์œ„์น˜์—์„œ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ์ ์—๊ฒŒ ๊ณต๊ฒฉ๋ฐ›์€ ๊ฒฝ์šฐ ๊ธฐ๋ฌผ ์ œ๊ฑฐ +- [x] ๊ธฐ๋ฌผ ๊ฒ€์ƒ‰ +- [x] ์ ์ˆ˜ ๊ณ„์‚ฐ + +#### `Piece` + +- [x] ์ด๋™ ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ํ˜„์œ„์น˜์—์„œ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ +- [x] ์ƒ‰์ƒ ํ™•์ธ +- [x] ํƒ€์ž… ํ™•์ธ + - [x] `Pawn` + - [x] `King` + +#### `MovePattern` + +- [x] ๊ฐ ๊ธฐ๋ฌผ ํƒ€์ž…์— ๋งž๋Š” ํŒจํ„ด ๊ฒ€์ƒ‰ + - [x] `Pawn`์€ ์ƒ‰์ƒ์— ๋งž๋Š” ํŒจํ„ด ๊ฒ€์ƒ‰ +- [x] ๋ณธ์ธ ํŒจํ„ด ์ค‘ `gap`์— ๋งž๋Š” `MoveUnit` ๊ฒ€์ƒ‰ +- [x] ๋ณธ์ธ ํŒจํ„ด์œผ๋กœ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ + +#### `MoveLimit` + +- [x] ํ•œ ์นธ๋งŒ ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ + +#### `MoveUnits` + +- [x] ๋ณธ์ธ ๋ฆฌ์ŠคํŠธ์—์„œ ๋งค์นญ๋˜๋Š” `MoveUnit` ๊ฒ€์ƒ‰ + - [x] ์˜ˆ์™ธ: ๋งค์นญ๋˜๋Š” `MoveUnit`์ด ์—†๋Š” ๊ฒฝ์šฐ +- [x] ์ด๋™ ๋‹จ์œ„ ๋ณ„๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ ๊ฒ€์ƒ‰ + +#### `MoveUnit` + +- [x] `MoveUnit`์˜ ์ขŒํ‘œ๊ฐ€ `gap`์— ๋งค์นญ๋˜๋Š”์ง€ ํ™•์ธ + +#### `Position` + +- [x] 64๊ฐœ ์œ„์น˜ ์บ์‹ฑ ๋ฐ ๊ฒ€์ƒ‰ +- [x] `Gap` ๊ณ„์‚ฐ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ํƒ€๊ฒŸ๊นŒ์ง€ ์ด๋™ํ•  ๋•Œ ์ง€๋‚˜๊ฐ€๋Š” ๋ชจ๋“  ์œ„์น˜ ๊ฒ€์ƒ‰ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ ์ฒด์ŠคํŒ์˜ ๋ชจ๋“  ์œ„์น˜ ๊ฒ€์ƒ‰ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ํ•œ ๋ฒˆ ๋” ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ +- [x] ์ด๋™ ๋‹จ์œ„๋กœ ์ด๋™ + +## ์ด๋™ ๊ทœ์น™ + +- [x] ํฐ (1 or 0.5) + - [x] ์  ๋ฐฉํ–ฅ ์ง์„  1์นธ ์ด๋™ + - [x] ์ฒ˜์Œ ์ด๋™ ์‹œ์—๋Š” 2์นธ ์ด๋™ ๊ฐ€๋Šฅ + - [x] ๊ณต๊ฒฉ ์‹œ์—๋Š” ์  ๋ฐฉํ–ฅ ์ขŒ, ์šฐ ๋Œ€๊ฐ์„  1์นธ ์ด๋™ + +- [x] ๋ฃฉ (5) + - [x] ๋ชจ๋“  ์ง์„  ๋ฐฉํ–ฅ์œผ๋กœ ์›ํ•˜๋Š” ๋งŒํผ ์ด๋™ + +- [x] ๋‚˜์ดํŠธ (2.5) + - [x] ๋ชจ๋“  ์ง์„  ๋ฐฉํ–ฅ 1์นธ + ์ด๋™ํ•œ ์ง์„  ๋ฐฉํ–ฅ์˜ ์ขŒ, ์šฐ ๋Œ€๊ฐ์„  1์นธ์œผ๋กœ ์ด๋™ + - [x] ์ง„ํ–‰ ๋ฐฉํ–ฅ์ด ๊ฐ€๋กœ๋ง‰ํ˜€๋„ ์ , ์•„๊ตฐ ์ƒ๊ด€์—†์ด ๋›ฐ์–ด๋„˜์„ ์ˆ˜ ์žˆ์Œ + +- [x] ๋น„์ˆ (3) + - [x] ๋ชจ๋“  ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์œผ๋กœ ์›ํ•˜๋Š” ๋งŒํผ ์ด๋™ + +- [x] ํ€ธ (9) + - [x] ๋ชจ๋“  ๋ฐฉํ–ฅ์œผ๋กœ ์›ํ•˜๋Š” ๋งŒํผ ์ด๋™ + +- [x] ํ‚น + - [x] ๋ชจ๋“  ๋ฐฉํ–ฅ 1์นธ ์ด๋™ + - [x] ์ƒ๋Œ€์˜ ๊ณต๊ฒฉ ๋ฒ”์œ„๋กœ๋Š” ์ด๋™ ๋ถˆ๊ฐ€๋Šฅ +
Unknown
๋„ต ํ€ธ ์ด๋™ ๊ทœ์น™์„ ์™„์ „ํžˆ ์ฐฉ๊ฐํ•ด๋ฒ„๋ ธ์–ด์š”ใ… ใ…  ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,42 @@ +import Customer from '../src/models/Customer'; +import Order from '../src/models/Order'; + +describe('Customer ํด๋ž˜์Šค', () => { + let customer; + + beforeEach(() => { + customer = new Customer(); + }); + + test('์ฃผ๋ฌธ์ด ์—†์„ ๋•Œ, ๋ฑƒ์ง€ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋Š” null์ด ๋œ๋‹ค.', () => { + customer.calculateBadge(); + expect(customer.getBadge()).toBeNull(); + }); + + test('์ ์ ˆํ•œ ํ• ์ธ ๊ธˆ์•ก์— ๋”ฐ๋ผ ์˜ฌ๋ฐ”๋ฅธ ๋ฑƒ์ง€๊ฐ€ ํ• ๋‹น๋œ๋‹ค.', () => { + const order = new Order(); + order.setDiscountAmount(10000); + customer.addOrder(order); + customer.calculateBadge(); + expect(customer.getBadge()).toBe('ํŠธ๋ฆฌ'); + }); + + test('์—ฌ๋Ÿฌ ์ฃผ๋ฌธ์„ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ, ์ด ํ• ์ธ ๊ธˆ์•ก์— ๋”ฐ๋ฅธ ๋ฑƒ์ง€๊ฐ€ ํ• ๋‹น๋œ๋‹ค.', () => { + const order1 = new Order(); + order1.setDiscountAmount(3000); + const order2 = new Order(); + order2.setDiscountAmount(2000); + customer.addOrder(order1); + customer.addOrder(order2); + customer.calculateBadge(); + expect(customer.getBadge()).toBe('๋ณ„'); + }); + + test('ํ• ์ธ ๊ธˆ์•ก์ด 5000์› ์ดํ•˜์ผ ๋•Œ ๋ฑƒ์ง€๋Š” null๋กœ ํ‘œ์‹œ๋œ๋‹ค.', () => { + const order = new Order(); + order.setDiscountAmount(4000); + customer.addOrder(order); + customer.calculateBadge(); + expect(customer.getBadge()).toBeNull(); + }); +});
JavaScript
์ง€๊ธˆ์€ `ํŠธ๋ฆฌ`์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งŒ ์žˆ๋Š”๋ฐ, `๋ณ„`๊ณผ `์‚ฐํƒ€` ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊นŒ์ง€ ๋ชจ๋‘ ํฌํ•จํ•˜๋ฉด ํ…Œ์ŠคํŠธ ์‹ ๋ขฐ๋„๊ฐ€ ๋†’์•„์งˆ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,48 @@ +import Menu from '../src/models/Menu'; +import { MenuConstants } from '../src/constants/MenuConstants'; + +describe('Menu ํด๋ž˜์Šค', () => { + let menu; + + beforeEach(() => { + menu = new Menu(); + }); + + test('๋ฉ”๋‰ด ํ•ญ๋ชฉ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ดˆ๊ธฐํ™”๋˜์–ด์•ผ ํ•œ๋‹ค.', () => { + expect(menu.getItem('ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ')).toEqual({ + name: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', + price: 55000, + category: 'MAINS', + }); + expect(menu.getItem('์‹œ์ €์ƒ๋Ÿฌ๋“œ')).toEqual({ + name: '์‹œ์ €์ƒ๋Ÿฌ๋“œ', + price: 8000, + category: 'APPETIZERS', + }); + }); + + test('์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฉ”๋‰ด ํ•ญ๋ชฉ์— ๋Œ€ํ•ด null์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.', () => { + expect(menu.getItem('์–‘์†ก์ด์Šคํ”„')).toBeNull(); + expect(menu.getItem('1234')).toBeNull(); + expect(menu.getItem('์Šคํ…Œ์ดํฌ@')).toBeNull(); + }); + + test('๋ชจ๋“  ๋ฉ”๋‰ด ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋กœ๋“œ๋˜์–ด์•ผ ํ•œ๋‹ค.', () => { + Object.keys(MenuConstants).forEach((category) => { + Object.keys(MenuConstants[category]).forEach((itemName) => { + const menuItem = menu.getItem(itemName); + expect(menuItem).not.toBeNull(); + expect(menuItem.category).toBe(category); + }); + }); + }); + + test('๊ฐ ๋ฉ”๋‰ด ํ•ญ๋ชฉ์˜ ๊ฐ€๊ฒฉ์ด ์ •ํ™•ํ•ด์•ผ ํ•จ', () => { + Object.keys(MenuConstants).forEach((category) => { + Object.entries(MenuConstants[category]).forEach(([itemName, price]) => { + const menuItem = menu.getItem(itemName); + expect(menuItem.price).toBe(price); + }); + }); + }); +});
JavaScript
์—ฌ๊ธฐ ๋‘ ํ…Œ์ŠคํŠธ ๋ชจ๋‘ ์—ฌ๋Ÿฌ ๊ฐœ์˜ `expect()`๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ์š”. `expect()`ํ•˜๋Š” ๋ถ€๋ถ„์˜ ๊ตฌ์กฐ๊ฐ€ ๋น„์Šทํ•˜๋‹ˆ๊นŒ "๋ฉ”๋‰ด ํ•ญ๋ชฉ ์ดˆ๊ธฐํ™” ํ…Œ์ŠคํŠธ"์™€ "์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฉ”๋‰ด ํ•ญ๋ชฉ ํ…Œ์ŠคํŠธ"๋ฅผ `test.each()`๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”?. ํ”„๋กœ๋•์…˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์ค‘๋ณต๋˜๋Š” ๋ถ€๋ถ„์„ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฆฌํŒฉํ„ฐ๋ง์ด ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,73 @@ +import OrderValidator from '../src/validators/OrderValidator'; +import Menu from '../src/models/Menu'; +import { ValidatorConstants } from '../src/validators/constants/ValidatorConstants'; + +describe('OrderValidator ํด๋ž˜์Šค', () => { + let mockMenu; + + beforeEach(() => { + mockMenu = new Menu(); + }); + + describe('validateOrderItems ๋ฉ”์„œ๋“œ', () => { + test('์œ ํšจํ•œ ์ฃผ๋ฌธ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค', () => { + const orderItems = [{ name: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', quantity: 1 }]; + expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).not.toThrow(); + }); + + test('์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฉ”๋‰ด ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ด์•ผ ํ•œ๋‹ค', () => { + const orderItems = [{ name: '๋ฏธ์ง€์˜๋ฉ”๋‰ด', quantity: 1 }]; + expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).toThrow( + new Error(ValidatorConstants.INVALID_ORDER_ERROR), + ); + }); + + test('์Œ๋ฃŒ๋งŒ ํฌํ•จ๋œ ์ฃผ๋ฌธ ์‹œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ด์•ผ ํ•œ๋‹ค', () => { + const orderItems = [{ name: '์ œ๋กœ์ฝœ๋ผ', quantity: 2 }]; + expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).toThrow( + new Error(ValidatorConstants.ONLY_BEVERAGE_ERROR), + ); + }); + + test('์ฃผ๋ฌธ ํ•ญ๋ชฉ์˜ ์ด ์ˆ˜๋Ÿ‰์ด 20๊ฐœ๋ฅผ ์ดˆ๊ณผํ•˜๋Š” ๊ฒฝ์šฐ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ด์•ผ ํ•œ๋‹ค', () => { + const orderItems = new Array(21).fill({ name: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', quantity: 1 }); + expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).toThrow( + new Error(ValidatorConstants.INVALID_ORDER_ERROR), + ); + }); + test('์ฃผ๋ฌธ ํ•ญ๋ชฉ์˜ ์ด ์ˆ˜๋Ÿ‰์ด 20๊ฐœ ์ดํ•˜์ธ ๊ฒฝ์šฐ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค', () => { + const orderItems = new Array(20).fill({ name: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', quantity: 1 }); + expect(() => OrderValidator.validateTotalQuantity(orderItems)).not.toThrow(); + }); + + test('์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์ด 0 ์ดํ•˜์ธ ๊ฒฝ์šฐ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ด์•ผ ํ•œ๋‹ค', () => { + const orderItems = [{ name: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', quantity: 0 }]; + expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).toThrow( + new Error(ValidatorConstants.INVALID_ORDER_ERROR), + ); + }); + + test('์ˆ˜๋Ÿ‰์ด 1 ์ด์ƒ์ธ ๊ฒฝ์šฐ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค', () => { + const item = { name: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', quantity: 1 }; + expect(() => OrderValidator.validateQuantity(item)).not.toThrow(); + }); + + test('์ค‘๋ณต๋œ ๋ฉ”๋‰ด ํ•ญ๋ชฉ์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ด์•ผ ํ•œ๋‹ค', () => { + const orderItems = [ + { name: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', quantity: 1 }, + { name: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', quantity: 1 }, + ]; + expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).toThrow( + new Error(ValidatorConstants.INVALID_ORDER_ERROR), + ); + }); + + test('์Œ์‹๊ณผ ์Œ๋ฃŒ๊ฐ€ ํ˜ผํ•ฉ๋œ ์œ ํšจํ•œ ์ฃผ๋ฌธ์€ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค', () => { + const orderItems = [ + { name: 'ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', quantity: 1 }, + { name: '์ œ๋กœ์ฝœ๋ผ', quantity: 2 }, + ]; + expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).not.toThrow(); + }); + }); +});
JavaScript
`Validator` ํ…Œ์ŠคํŠธ ํŒŒ์ผ์—์„œ๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์—๋Ÿฌ์˜ ์œ ํ˜•๊ณผ ๋ฉ”์‹œ์ง€ ํฌํ•จ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•˜๊ณ  ์žˆ๋Š”๋ฐ์š”. ๋‹ค๋ฅธ ๊ณณ์˜ ์˜ˆ์™ธ ํ…Œ์ŠคํŠธ์—์„œ๋Š” ๋ฉ”์‹œ์ง€ ๊ฒ€์‚ฌ ์—†์ด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”์ง€๋งŒ(`toThrow()`) ๊ฒ€์‚ฌํ•˜๋Š” ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,56 @@ +import InputView from '../views/InputView'; +import OutputView from '../views/OutputView'; +import OrderService from '../services/OrderService'; +import DiscountService from '../services/DiscountService'; +import Customer from '../models/Customer'; +import { DateFormatter } from '../utils/DateFormatter'; + +export default class EventController { + #orderService; + + #customer; + + #discountService; + + constructor() { + this.#orderService = new OrderService(); + this.#customer = new Customer(); + this.#discountService = new DiscountService(); + } + + async start() { + OutputView.printWelcomeMessage(); + await this.handleOrderProcess(); + } + + async handleOrderProcess() { + try { + const inputDate = await this.getDateFromUser(); + await this.handleMenuOrderProcess(inputDate); + } catch (error) { + OutputView.printErrorMessage(error.message); + await this.handleOrderProcess(); + } + } + + async getDateFromUser() { + const day = await InputView.readDate(); + return DateFormatter.createDateFromDay(day); + } + + async handleMenuOrderProcess(inputDate) { + try { + const inputMenuItems = await InputView.readMenu(); + const order = this.#orderService.createOrder(inputDate, inputMenuItems); + order.applyDiscounts(this.#discountService, inputDate); + this.#customer.addOrder(order); + this.#customer.calculateBadge(); + OutputView.printDay(inputDate); + OutputView.printOrderDetails(order); + OutputView.printBadge(this.#customer); + } catch (error) { + OutputView.printErrorMessage(error.message); + await this.handleMenuOrderProcess(inputDate); + } + } +}
JavaScript
ํ•จ์ˆ˜ ๋ผ์ธ ์ˆ˜๊ฐ€ ๋”ฑ 15๋ผ์ธ์ธ ๊ฒƒ ๊ฐ™๋„ค์š”. ๋งŒ์•ฝ ์–ด๋–ค ์ถœ๋ ฅ์ด ํ•˜๋‚˜ ์ถ”๊ฐ€๋œ๋‹ค๋ฉด ์—ฌ๊ธฐ์— ์ถ”๊ฐ€ํ•ด์•ผ ํ• ํ…๋ฐ, ๊ทธ๋Ÿฌ๋ฉด ํ•จ์ˆ˜ ๋ผ์ธ ์ˆ˜๊ฐ€ 15๋ผ์ธ์„ ๋„˜๊ธธ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”. ํ•จ์ˆ˜ ๋ผ์ธ ์ˆ˜ ์ œํ•œ์ด ์žˆ๋Š” ๊ฒƒ์€ 15๋ผ์ธ์ด๋ผ๋Š” ๊ฒŒ ์ค‘์š”ํ•ด์„œ๋ผ๊ธฐ๋ณด๋‹ค ๊ทธ๋งŒํผ **ํ•จ์ˆ˜๋ฅผ ์ž˜ ๋ถ„๋ฆฌํ•˜์ž** ๋Š” ์˜๋ฏธ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ•จ์ˆ˜์˜ ๋ผ์ธ ์ˆ˜๊ฐ€ ๊ฐ„๋‹น๊ฐ„๋‹นํ•˜๋‹ค๋Š” ๊ฒƒ์€, ๋ฆฌํŒฉํ„ฐ๋ง์˜ ์‹ ํ˜ธ๊ฐ€ ์•„๋‹๊นŒ ์‹ถ์–ด์š”๐Ÿค” ์ด ๋ฉ”์„œ๋“œ๋Š” ๋™์‹œ์— ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ผ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š”๋ฐ์š”(์ž…๋ ฅ ๋ฐ›๊ธฐ, ์ฃผ๋ฌธ ์ƒ์„ฑ, ๊ฐ์ข… ํ• ์ธ ์ ์šฉ, ์ถœ๋ ฅ, ์ž…๋ ฅ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฐ ์žฌ์ž…๋ ฅ ๋ฐ›๊ธฐ ๋“ฑ). ๋ฉ”์„œ๋“œ๊ฐ€ ํ•˜๋‚˜์˜ ์—ญํ• ๋งŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,30 @@ +export default class Customer { + #orders; + + #badge; + + static BADGE_CRITERIA = [ + { name: '์‚ฐํƒ€', amount: 20000 }, + { name: 'ํŠธ๋ฆฌ', amount: 10000 }, + { name: '๋ณ„', amount: 5000 }, + ]; + + constructor() { + this.#orders = []; + this.#badge = null; + } + + addOrder(order) { + this.#orders.push(order); + } + + calculateBadge() { + const totalDiscount = this.#orders.reduce((sum, order) => sum + order.getDiscountAmount(), 0); + const badge = Customer.BADGE_CRITERIA.find((badgeName) => totalDiscount >= badgeName.amount); + this.#badge = badge ? badge.name : null; + } + + getBadge() { + return this.#badge; + } +}
JavaScript
์ƒ์ˆ˜๋ฅผ ํŒŒ์ผ๋กœ ๋งŒ๋“ ๋‹ค๋ฉด ์ˆ˜์ •์ด ํ•„์š”ํ•  ๋•Œ ์ฐพ๊ธฐ๊ฐ€ ์ˆ˜์›”ํ•˜๊ณ , ๋ฐฐ์ง€ ์ •๋ณด๊ฐ€ ๋Š˜์–ด๋‚˜๊ฑฐ๋‚˜ ์ค„์–ด๋“ค์–ด๋„ ๊ด€๋ฆฌํ•˜๊ธฐ ํŽธ๋ฆฌํ•  ๊ฒƒ ๊ฐ™์€๋ฐ์š”. ํŒŒ์ผ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ์—ฌ๊ธฐ์— ์ž‘์„ฑํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ๋‚˜์š”?
@@ -0,0 +1,81 @@ +import { OrderConstants } from './constants/OrderConstants'; + +export default class Order { + #menuItems; + + #totalAmount; + + #discountAmount; + + #gift; + + #discountDetails; + + constructor() { + this.#menuItems = new Map(); + this.#totalAmount = 0; + this.#discountAmount = 0; + this.#gift = null; + this.#discountDetails = {}; + } + + applyDiscounts(discountService, date) { + const discountResults = discountService.applyDiscounts(this, date); + this.setDiscountDetails(discountResults); + this.checkForGift(); + } + + addMenuItem(menuItem, quantity) { + this.#menuItems.set(menuItem.name, { ...menuItem, quantity }); + this.#totalAmount += menuItem.price * quantity; + } + + addDiscountDetail(discountName, discountAmount) { + this.#discountDetails[discountName] = + (this.#discountDetails[discountName] || 0) + discountAmount; + this.#discountAmount += discountAmount; + } + + setDiscountDetails(discountDetails) { + this.#discountDetails = { ...discountDetails }; + this.#discountAmount = Object.values(this.#discountDetails).reduce( + (sum, amount) => sum + amount, + 0, + ); + } + + checkForGift() { + if (this.#totalAmount >= OrderConstants.GIFT_THRESHOLD_AMOUNT) { + this.#gift = `${OrderConstants.GIFT_NAME} ${OrderConstants.GIFT_QUANTITY}๊ฐœ`; + } + } + + getDiscountDetails() { + return this.#discountDetails; + } + + getTotalAmount() { + return this.#totalAmount; + } + + getFinalAmount() { + const giftDiscount = this.#discountDetails[OrderConstants.GIFT_EVENT_TITLE] || 0; + return this.#totalAmount - (this.#discountAmount - giftDiscount); + } + + setDiscountAmount(discountAmount) { + this.#discountAmount = discountAmount; + } + + getDiscountAmount() { + return this.#discountAmount; + } + + getItems() { + return Array.from(this.#menuItems.values()); + } + + getGift() { + return this.#gift; + } +}
JavaScript
์ €๋Š” `Model`์€ ์ฆ์ • ๋ฉ”๋‰ด์— ๋Œ€ํ•œ ์ •๋ณด(๋ฐ์ดํ„ฐ)๋งŒ ์ €์žฅํ•˜๊ณ , ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  '`์ฆ์ •๋ฉ”๋‰ด` `๊ฐœ์ˆ˜`๊ฐœ'์™€ ๊ฐ™์ด ํฌ๋งทํŒ…ํ•˜๋Š” ๊ฒƒ์€ `View`์˜ ์—ญํ• ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์š”. ๋งŒ์•ฝ ์ฆ์ • ๋ฉ”๋‰ด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ๋‹ค๋ฅธ ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ๋ทฐ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ๊ธด๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ์ง€๊ธˆ ์ฝ”๋“œ๋ฐฉ์‹๋Œ€๋กœ๋ผ๋ฉด ๊ฐ ๋ทฐ์—์„œ ๋‚˜ํƒ€๋‚ผ ์ถœ๋ ฅ ํ˜•์‹์„ `Model`์—์„œ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฑด `Model`์ด `View`์— ์˜์กดํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ ํ˜•์‹์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ `Model`์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋  ํ…Œ๋‹ˆ๊นŒ์š”. ๋˜ `Model`์˜ ์—ญํ• ์„ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒƒ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ €๋Š” ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ถœ๋ ฅ์„ ๋‹ด๋‹นํ•˜๋Š” `View` ์ชฝ์œผ๋กœ ์˜ฎ๊ธฐ๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์€๋ฐ, ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,6 @@ +export const OrderConstants = { + GIFT_THRESHOLD_AMOUNT: 120000, + GIFT_EVENT_TITLE: '์ฆ์ • ์ด๋ฒคํŠธ', + GIFT_NAME: '์ƒดํŽ˜์ธ', + GIFT_QUANTITY: 1, +};
JavaScript
์ง€๊ธˆ์€ ์ฆ์ • ์ด๋ฒคํŠธ๊ฐ€ ์ƒดํŽ˜์ธ 1๊ฐœ๋งŒ ์ฃผ๊ณ  ์žˆ๋Š”๋ฐ์š”. ๋งŒ์•ฝ ์ด ์ด๋ฒคํŠธ๊ฐ€ ํ™•์žฅ๋œ๋‹ค๋ฉด, ๋ฐฐ์ง€ ์ด๋ฒคํŠธ์ฒ˜๋Ÿผ ๊ธˆ์•ก์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์„ ๋ฌผ์„ ์ฆ์ •ํ•˜๊ฒŒ ๋  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”. Strategy ํŒจํ„ด์„ ์„ ํƒํ•˜์‹  ์ด์œ ๋„ ์œ ์ง€๋ณด์ˆ˜์™€ ๋ณ€๊ฒฝ์— ์šฉ์ดํ•ด์„œ๋ผ๊ณ  ์ƒ๊ฐ๋˜๋Š”๋ฐ์š”. ๊ทธ๋งŒํผ ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ์—๋„ ํ™•์žฅ์„ฑ์„ ์ฑ™๊ธฐ๋ฉด ๋” ์ข‹์ง€ ์•Š์•˜์„๊นŒ ์ƒ๊ฐํ•ด๋ด…๋‹ˆ๋‹ค!
@@ -0,0 +1,46 @@ +import ChristmasDiscountStrategy from './strategies/ChristmasDiscountStrategy'; +import WeekdayDiscountStrategy from './strategies/WeekdayDiscountStrategy'; +import WeekendDiscountStrategy from './strategies/WeekendDiscountStrategy'; +import SpecialDiscountStrategy from './strategies/SpecialDiscountStrategy'; +import GiftChampagneStrategy from './strategies/GiftChampagneStrategy'; + +export default class DiscountService { + #strategies; + + constructor() { + this.#strategies = { + christmas: new ChristmasDiscountStrategy(), + special: new SpecialDiscountStrategy(), + weekday: new WeekdayDiscountStrategy(), + weekend: new WeekendDiscountStrategy(), + giftChampagne: new GiftChampagneStrategy(), + }; + } + + applyDiscounts(order, date) { + const MINIMUM_AMOUNT_FOR_DISCOUNTS = 10000; + if (order.getTotalAmount() >= MINIMUM_AMOUNT_FOR_DISCOUNTS) { + const discountResults = this.calculateTotalDiscounts(order, date); + const totalDiscountAmount = this.calculateTotalDiscountAmount(discountResults); + order.setDiscountAmount(totalDiscountAmount); + return discountResults; + } + return {}; + } + + calculateTotalDiscounts(order, date) { + const discountResults = {}; + Object.keys(this.#strategies).forEach((strategyKey) => { + const strategy = this.#strategies[strategyKey]; + const discountResult = strategy.applyDiscount(order, date); + if (discountResult.amount > 0) { + discountResults[discountResult.name] = discountResult.amount; + } + }); + return discountResults; + } + + calculateTotalDiscountAmount(discountResults) { + return Object.values(discountResults).reduce((sum, amount) => sum + amount, 0); + } +}
JavaScript
์ด ๋ถ€๋ถ„์„ ์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”! ```javascript Object.values(this.#strategies).forEach((strategy) => { const discountResult = strategy.applyDiscount(order, date); // ... }); ```
@@ -0,0 +1,5 @@ +export default class IDiscountStrategy { + applyDiscount(order, date) { + throw new Error('[ERROR] ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ applyDiscount ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.'); + } +}
JavaScript
ํŒŒ์ผ๋ช…๊ณผ ํด๋ž˜์Šค๋ช… ๋งจ ์•ž์— `I`๊ฐ€ ๋ถ™์–ด ์žˆ๋Š”๋ฐ, ํ˜น์‹œ ์˜๋„ํ•˜์‹  ๊ฑด๊ฐ€์š”? ์˜๋„ํ•˜์‹  ๊ฑฐ๋ผ๋ฉด ์–ด๋–ค ์˜๋ฏธ์ธ์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,16 @@ +import IDiscountStrategy from './IDiscountStrategy'; +import { DiscountStrategyConstants } from './constants/DiscountStrategyConstants'; + +export default class SpecialDiscountStrategy extends IDiscountStrategy { + applyDiscount(order, date) { + const dayOfWeek = date.getDay(); + const isSunday = dayOfWeek === 0; + const isChristmas = date.getDate() === 25; + + if (isSunday || isChristmas) { + return { name: DiscountStrategyConstants.SPECIAL_DISCOUNT, amount: 1000 }; + } + + return { name: DiscountStrategyConstants.SPECIAL_DISCOUNT, amount: 0 }; + } +}
JavaScript
ํŠน๋ณ„ ํ• ์ธ์ด ๋ชจ๋‘ ์ผ์š”์ผ (+25์ผ)์ด๋ผ๋Š” ์ ์„ ์ด์šฉํ•˜์…จ๋„ค์š”! ์ €๋Š” ๋ณ„ ํ‘œ์‹œ๊ฐ€ ๋œ ๋‚ ์งœ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ƒ์ˆ˜ ๋ฐฐ์—ด์— ๋‹ด์•„์„œ ๊ด€๋ฆฌํ–ˆ๋Š”๋ฐ์š”. ์˜ฎ๊ฒจ ์ ์„ ๋•Œ ์˜คํƒ€๋ผ๋„ ๋‚œ๋‹ค๋ฉด ํฐ์ผ์ด์ง€๋งŒ๐Ÿ˜…, ์ด๋ ‡๊ฒŒ ํ•œ ์ด์œ ๋Š” ํŠน๋ณ„ ํ• ์ธ ๋‚ ์งœ๊ฐ€ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์š”๊ตฌ์‚ฌํ•ญ์—์„œ "ํŠน๋ณ„ ํ• ์ธ"์„ ์„ค๋ช…ํ•  ๋•Œ, '๋‹ฌ๋ ฅ์— ๋ณ„์ด ํ‘œ์‹œ๋œ ๋‚ ์งœ'๋ผ๊ณ  ์„ค๋ช…ํ•˜๋”๋ผ๊ตฌ์š”. ๋งŒ์•ฝ '๋ชจ๋“  ์ผ์š”์ผ๊ณผ ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋‹น์ผ'์ฒ˜๋Ÿผ ์„ค๋ช…ํ–ˆ๋‹ค๋ฉด ์ €๋„ ์ด๋Ÿฐ ์ฝ”๋“œ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”. ๊ตฌํ˜„ ๋ฐฉ์‹์— ์ •๋‹ต์€ ์—†์ง€๋งŒ ์ œ ์ƒ๊ฐ์„ ํ•œ ๋ฒˆ ๊ณต์œ ๋“œ๋ฆฌ๊ณ  ์‹ถ์—ˆ์–ด์š”๐Ÿ˜„
@@ -0,0 +1,19 @@ +import IDiscountStrategy from './IDiscountStrategy'; +import { DiscountStrategyConstants } from './constants/DiscountStrategyConstants'; + +export default class WeekdayDiscountStrategy extends IDiscountStrategy { + applyDiscount(order, date) { + const dayOfWeek = date.getDay(); + const isWeekday = dayOfWeek >= 0 && dayOfWeek <= 4; + + if (isWeekday) { + const DESSERT_DISCOUNT = 2023; + const discountAmount = order + .getItems() + .filter((item) => item.category === 'DESSERTS') + .reduce((total, item) => total + item.quantity * DESSERT_DISCOUNT, 0); + return { name: DiscountStrategyConstants.WEEKDAY_DISCOUNT, amount: discountAmount }; + } + return { name: DiscountStrategyConstants.WEEKDAY_DISCOUNT, amount: 0 }; + } +}
JavaScript
`0`, `4`์™€ ๊ฐ™์€ ์ˆซ์ž๋ฅผ `SUNDAY`, `THURSDAY`์ฒ˜๋Ÿผ ์ƒ์ˆ˜ํ™”ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ ๊ฐ€๋…์„ฑ์„ ๋” ์ฑ™๊ธธ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,36 @@ +import { ValidatorConstants } from './constants/ValidatorConstants'; + +export const InputValidator = { + Day: { + validateDay(input) { + const dayPattern = /^\d+$/; + if (!dayPattern.test(input)) { + throw new Error(ValidatorConstants.INVALID_DATE_ERROR); + } + const day = parseInt(input, 10); + if (day < 1 || day > 31) { + throw new Error(ValidatorConstants.INVALID_DATE_ERROR); + } + + return day; + }, + }, + + Menu: { + validateMenu(input) { + const menuSelections = input.split(','); + const selectionPattern = /^[๊ฐ€-ํžฃ]+-\d+$/; + const validSelections = menuSelections.map((selection) => { + const trimmedSelection = selection.trim(); + if (!selectionPattern.test(trimmedSelection)) { + throw new Error(ValidatorConstants.INVALID_ORDER_ERROR); + } + const [name, quantity] = trimmedSelection.split('-'); + + return { name, quantity: parseInt(quantity, 10) }; + }); + + return validSelections; + }, + }, +};
JavaScript
๊ฐ๊ฐ์˜ validation(์ •๊ทœํ‘œํ˜„์‹ ํŒจํ„ด & ๋ฒ”์œ„)์„ ๋‘ ๊ฐœ์˜ ํ•จ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•ด ์„ ์–ธํ•˜๊ณ  ํ˜ธ์ถœํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”? ์˜๋„ํ•ด์„œ ํ•œ๊บผ๋ฒˆ์— ์ž‘์„ฑํ•˜์‹  ๊ฒƒ ๊ฐ™์€๋ฐ, ๋งŒ์•ฝ ๋งž๋‹ค๋ฉด ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”!
@@ -0,0 +1,36 @@ +import { ValidatorConstants } from './constants/ValidatorConstants'; + +export const InputValidator = { + Day: { + validateDay(input) { + const dayPattern = /^\d+$/; + if (!dayPattern.test(input)) { + throw new Error(ValidatorConstants.INVALID_DATE_ERROR); + } + const day = parseInt(input, 10); + if (day < 1 || day > 31) { + throw new Error(ValidatorConstants.INVALID_DATE_ERROR); + } + + return day; + }, + }, + + Menu: { + validateMenu(input) { + const menuSelections = input.split(','); + const selectionPattern = /^[๊ฐ€-ํžฃ]+-\d+$/; + const validSelections = menuSelections.map((selection) => { + const trimmedSelection = selection.trim(); + if (!selectionPattern.test(trimmedSelection)) { + throw new Error(ValidatorConstants.INVALID_ORDER_ERROR); + } + const [name, quantity] = trimmedSelection.split('-'); + + return { name, quantity: parseInt(quantity, 10) }; + }); + + return validSelections; + }, + }, +};
JavaScript
์ด ๋กœ์ง์ด๋ผ๋ฉด ์ด๋Ÿฐ ์ž…๋ ฅ์„ ๋„ฃ์–ด๋„ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•  ๊ฒƒ ๊ฐ™์•„์š”. ํ˜น์‹œ ๋‹ค๋ฅธ ๋ถ€๋ถ„์—์„œ ๊ฒ€์‚ฌํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์ œ๊ฐ€ ๋†“์นœ ๊ฒƒ์ผ๊นŒ์š”? ``` ๋ฐ”๋น„ํ๋ฆฝ-1,,,,,,,ํƒ€ํŒŒ์Šค-2 ๋ฐ”๋น„ํ๋ฆฝ-1,,,ํƒ€ํŒŒ์Šค-2,, ```
@@ -0,0 +1,123 @@ +import { FOOD_MENU } from '../constants/FoodMenu.js'; +import { + EVENT_CONST, + DAY_OF_WEEK, + UTILS_CONST, +} from '../constants/Constants.js'; + +class PlannerUtils { + #userOrder; + + #userDate; + + #userDay; + + #userEvent; + + constructor(userOrder, userDate) { + this.#userOrder = userOrder; + this.#userDate = userDate; + this.#userDay = new Date(UTILS_CONST.userDay(this.#userDate)).getDay(); + this.#userEvent = { ...EVENT_CONST }; + } + + getTotalAmount() { + const MENU_COST = {}; + Object.values(FOOD_MENU).forEach((course) => { + Object.values(course).forEach((menu) => { + MENU_COST[menu[0]] = menu[1]; + }); + }); + return this.#sumAmount(MENU_COST); + } + + #sumAmount(MENU_COST) { + const totalAmount = []; + this.#userOrder.forEach((order) => { + totalAmount.push(MENU_COST[order[0]] * Number(order[1])); + }); + return totalAmount.reduce((total, cost) => total + cost); + } + + benefitCheck() { + if (this.getTotalAmount() >= UTILS_CONST.minGetBenefit) { + this.#userEvent.christmas = this.#christmasCheck(); + this.#userEvent.weekDay = this.#weekDaysCheck(); + this.#userEvent.weekendDay = this.#weekendDayCheck(); + this.#userEvent.specialDay = this.#specialDayCheck(); + this.#userEvent.benefitEvent = this.#giftCheck(); + } + return this.#userEvent; + } + + #christmasCheck() { + if (this.#userDate > UTILS_CONST.christmasEventEnd) { + return UTILS_CONST.noBenefit; + } + return UTILS_CONST.christmasBenefit(this.#userDate); + } + + #weekDaysCheck() { + if (DAY_OF_WEEK.weekDay.includes(this.#userDay)) { + return ( + this.#checkEventMenu(UTILS_CONST.weekDayEvent) * UTILS_CONST.discount + ); + } + return UTILS_CONST.noBenefit; + } + + #weekendDayCheck() { + if (DAY_OF_WEEK.weekendDay.includes(this.#userDay)) { + return ( + this.#checkEventMenu(UTILS_CONST.weekendEvent) * UTILS_CONST.discount + ); + } + return UTILS_CONST.noBenefit; + } + + #specialDayCheck() { + if (DAY_OF_WEEK.specialDay.includes(this.#userDay)) { + return UTILS_CONST.specialDiscount; + } + if (DAY_OF_WEEK.specialDay.includes(this.#userDate)) { + return UTILS_CONST.specialDiscount; + } + return UTILS_CONST.noBenefit; + } + + #giftCheck() { + if (this.getTotalAmount() > UTILS_CONST.minGetGift) { + return UTILS_CONST.giftCost; + } + return UTILS_CONST.noBenefit; + } + + #checkEventMenu(event) { + const numberOfMenu = [0]; + const eventMenu = Object.values(FOOD_MENU[event]).map((menu) => menu[0]); + this.#userOrder.forEach((menu) => { + if (eventMenu.includes(menu[0])) { + numberOfMenu.push(Number(menu[1])); + } + }); + return numberOfMenu.reduce((total, amount) => total + amount); + } + + calcBenefitAmount() { + const totalBenefit = Object.values(this.#userEvent).reduce( + (total, amount) => total + amount, + ); + return totalBenefit; + } + + calcTotalPayment() { + if (this.#userEvent.benefitEvent !== 0) { + return ( + this.getTotalAmount() - this.calcBenefitAmount() + UTILS_CONST.giftCost + ); + } + return this.getTotalAmount() - this.calcBenefitAmount(); + } +} + +export default PlannerUtils;
JavaScript
์ด๊ฑด ์ •๋ง ์ €์˜ ๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค๋งŒ, ํ•˜๋‚˜์˜ ํด๋ž˜์Šค์—์„œ ๋„ˆ๋ฌด ๋งŽ์€ ์ผ์„ ๋‹ด๋‹นํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค..!
@@ -0,0 +1,32 @@ +import { ERROR_MSG } from '../../constants/PlannerMsg.js'; + +class DateValidate { + #regax; + + #date; + + constructor(regax, date) { + this.#regax = regax; + this.#date = date; + } + + regaxCheck() { + if (this.#regax.test(this.#date)) { + throw new Error(ERROR_MSG.dateError); + } + } + + safeCheck() { + if (!Number.isSafeInteger(Number(this.#date))) { + throw new Error(ERROR_MSG.dateError); + } + } + + rangeCheck() { + if (Number(this.#date) <= 0 || Number(this.#date) > 31) { + throw new Error(ERROR_MSG.dateError); + } + } +} + +export default DateValidate;
JavaScript
์ด๊ฑด ์ง„์งœ ์‚ฌ์†Œํ•œ ๊ฑฐ๊ธดํ•œ๋ฐ, ๋ณ€์ˆ˜ ์ค‘์— #regax ๊ฐ€ ์žˆ๋Š”๋ฐ ์ •๊ทœํ‘œํ˜„์‹์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ, ํ˜น์‹œ ๋งž๋‹ค๋ฉด regular expression => regex ๊ฐ€ ์ข‹์ง€ ์•Š์„๊นŒ! ์ƒ๊ฐํ•ด๋ดค์Šต๋‹ˆ๋‹ค! ์•„๋งˆ ์˜คํƒ€์ด์‹  ๊ฑฐ ๊ฐ™๊ธฐ๋‘ํ•˜๊ตฌ..!?
@@ -0,0 +1,32 @@ +import { ERROR_MSG } from '../../constants/PlannerMsg.js'; + +class DateValidate { + #regax; + + #date; + + constructor(regax, date) { + this.#regax = regax; + this.#date = date; + } + + regaxCheck() { + if (this.#regax.test(this.#date)) { + throw new Error(ERROR_MSG.dateError); + } + } + + safeCheck() { + if (!Number.isSafeInteger(Number(this.#date))) { + throw new Error(ERROR_MSG.dateError); + } + } + + rangeCheck() { + if (Number(this.#date) <= 0 || Number(this.#date) > 31) { + throw new Error(ERROR_MSG.dateError); + } + } +} + +export default DateValidate;
JavaScript
Number.isInteger ๋งŒ ์•Œ์•˜์ง€, Number.isSafeInteger ๋ผ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” ์ค„์€ ์ •๋ง ๋ชฐ๋ž๋Š”๋ฐ, ๋•๋ถ„์— ์ด๋ ‡๊ฒŒ ๋˜ ํ•˜๋‚˜ ์•Œ์•„๊ฐ€์š”!
@@ -0,0 +1,32 @@ +import { ERROR_MSG } from '../../constants/PlannerMsg.js'; + +class DateValidate { + #regax; + + #date; + + constructor(regax, date) { + this.#regax = regax; + this.#date = date; + } + + regaxCheck() { + if (this.#regax.test(this.#date)) { + throw new Error(ERROR_MSG.dateError); + } + } + + safeCheck() { + if (!Number.isSafeInteger(Number(this.#date))) { + throw new Error(ERROR_MSG.dateError); + } + } + + rangeCheck() { + if (Number(this.#date) <= 0 || Number(this.#date) > 31) { + throw new Error(ERROR_MSG.dateError); + } + } +} + +export default DateValidate;
JavaScript
rangeCheck๋ฉ”์„œ๋“œ์—์„œ ๋‚ ์งœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž 0, 31๋„ ์ƒ์ˆ˜์ฒ˜๋ฆฌํ•ด์„œ ๊ฐ€์ ธ์˜ค๋ฉด ๋‚˜์ค‘์— ๋ณ€๊ฒฝํ•  ๋•Œ ๋” ํŽธ๋ฆฌํ•˜์ง€ ์•Š์„๊นŒ ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,125 @@ +import { ERROR_MSG } from '../../constants/PlannerMsg.js'; +import { FOOD_MENU } from '../../constants/FoodMenu.js'; + +class MenuValidate { + #canOrder; + + #userMenu; + + #menu; + + constructor(menu, canOrder = [], userMenu = []) { + this.#menu = menu; + this.#canOrder = canOrder; + this.#userMenu = userMenu; + } + + menuCheck() { + this.#findingMenu(this.#canOrder, this.#userMenu, this.#menu); + this.#userMenu.forEach((order) => { + if (!this.#canOrder.includes(order)) { + throw new Error(ERROR_MSG.notInMenu); + } + }); + } + + #findingMenu() { + Object.keys(FOOD_MENU).map((course) => + this.#makeCanOrder(course, this.#canOrder), + ); + + this.#menu.forEach((order) => this.#userMenu.push(order.split('-')[0])); + } + + #makeCanOrder(course) { + Object.values(FOOD_MENU[`${course}`]).map((food) => + this.#canOrder.push(food[0]), + ); + } + + menuAmountCheck(regax) { + const totalMenuAmount = []; + this.#menu.forEach((order) => { + this.#amountCheck(order.split('-')[1], regax); + totalMenuAmount.push(Number(order.split('-')[1])); + }); + + this.#totalAmountCheck(totalMenuAmount); + } + + #amountCheck(amount, regax) { + if (regax.test(Number(amount))) { + throw new Error(ERROR_MSG.notInMenu); + } + if (Number(amount) <= 0) { + throw new Error(ERROR_MSG.notInMenu); + } + + if (!Number.isSafeInteger(Number(amount))) { + throw new Error(ERROR_MSG.notInMenu); + } + } + + #totalAmountCheck(total) { + const checkTotal = total.reduce((sum, amount) => sum + amount); + if (checkTotal > 20) { + throw new Error(ERROR_MSG.totalError); + } + } + + formCheck() { + const form = /^[๊ฐ€-ํžฃ]+-[0-9]+$/; + this.#menu.forEach((order) => this.#formRegaxCheck(order, form)); + } + + #formRegaxCheck(order, form) { + if (!form.test(order)) { + throw new Error(ERROR_MSG.notInMenu); + } + } + + duplicateCheck() { + if (this.#userMenu.length !== new Set(this.#userMenu).size) { + throw new Error(ERROR_MSG.notInMenu); + } + } + + drinkCheck() { + const menuObject = this.#makingMenuObject(); + if (this.#objectCheck(menuObject)) { + throw new Error(ERROR_MSG.drinkError); + } + } + + #makingMenuObject() { + const menuObject = {}; + Object.keys(FOOD_MENU).forEach((course) => { + menuObject[course] = this.#userMenuCourses(course); + }); + return menuObject; + } + + #userMenuCourses(course) { + const includeCourse = []; + Object.values(FOOD_MENU[course]).forEach((eachMenu) => { + if (this.#menuIncludes(eachMenu)) { + includeCourse.push(eachMenu[0]); + } + }); + return includeCourse.length; + } + + #menuIncludes(eachMenu) { + return this.#userMenu.includes(eachMenu[0]); + } + + #objectCheck(menuObject) { + const { appetizer, main, dessert, drink } = menuObject; + if (appetizer === 0 && main === 0 && dessert === 0 && drink !== 0) { + return true; + } + return false; + } +} + +export default MenuValidate;
JavaScript
๋ฐฐ์—ด์„ ์ž์ฃผ ์‚ฌ์šฉํ•˜์‹  ๊ฒƒ ๊ฐ™์•„์š”! ์ €๋„ ์ด๋ฒˆ์— ๋ฏธ์…˜ํ•˜๋ฉด์„œ Array ๋ž‘ Map ์ค‘์—์„œ ์–ด๋–ค ๊ฑธ ์‚ฌ์šฉํ•  ์ง€ ๊ณ ๋ฏผํ–ˆ์—ˆ๋Š”๋ฐ์š”, ์ €ํ•œํ…Œ๋Š” ์‚ฌ์‹ค Map ์ด ์ต์ˆ™ํ•œ ๊ฐœ๋…์€ ์•„๋‹ˆ์—ฌ์„œ ๋ฐฑ์—”๋“œ๋ถ„๋“คํ•œํ…Œ ๋ฌผ์–ด๋ดค๋Š”๋ฐ ๋ชจ๋‘ ๋ฐฐ์—ด๊ณผ ๋งต ๋‘˜ ์ค‘์— ๊ณจ๋ผ์•ผํ•œ๋‹ค๋ฉด ๋งต์ด ๋” ํšจ์œจ์ ์ด๋ผ๊ณ  ํ•˜์‹œ๋”๋ผ๊ตฌ์š”! ๋ฐฐ์—ด๋„ index๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ณ , Map ๋„ key๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ, ๋ฐฐ์—ด์€ ํ•ด๋‹นindex๊นŒ์ง€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์•ผํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๊ตฌ์š”. ๋ฐ˜๋ฉด์— ๋งต์€ ํ•œ๋ฒˆ์— ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(1) ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,132 @@ +/* eslint-disable max-lines-per-function */ +import PlannerUtils from '../../src/domain/utils/PlannerUtills.js'; +import PlannerData from '../../src/domain/model/PlannerData.js'; + +describe('์œ ํ‹ธ ํ…Œ์ŠคํŠธ', () => { + const testCases = [ + { menu: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-5'], day: 25, expected: 275000 }, + { + menu: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-5', '์ œ๋กœ์ฝœ๋ผ-5', '์•„์ด์Šคํฌ๋ฆผ-5'], + day: 1, + expected: 275000 + 15000 + 25000, + }, + ]; + + testCases.forEach((testCase) => { + test(`์ด ๊ธˆ์•ก ํ…Œ์ŠคํŠธ : ${testCase.menu}`, () => { + const PLANNER_DATA = new PlannerData(); + PLANNER_DATA.updateDate(testCase.day); + PLANNER_DATA.updateFood(testCase.menu); + const plannerUtil = new PlannerUtils( + PLANNER_DATA.getUserOrder(), + PLANNER_DATA.getDate(), + ); + expect(plannerUtil.getTotalAmount()).toBe(testCase.expected); + }); + }); +}); + +describe('์œ ํ‹ธ ํ…Œ์ŠคํŠธ', () => { + const expectedObj = { + christmas: 3400, + weekDay: 4046, + weekendDay: 0, + specialDay: 1000, + benefitEvent: 25000, + }; + + const testCases = [ + { + menu: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-5', '์ดˆ์ฝ”์ผ€์ดํฌ-2'], + day: 25, + expected: expectedObj, + }, + ]; + + testCases.forEach((testCase) => { + test(`ํ˜œํƒ ํ…Œ์ŠคํŠธ : ${testCase.menu}`, () => { + const PLANNER_DATA = new PlannerData(); + PLANNER_DATA.updateDate(testCase.day); + PLANNER_DATA.updateFood(testCase.menu); + const plannerUtil = new PlannerUtils( + PLANNER_DATA.getUserOrder(), + PLANNER_DATA.getDate(), + ); + expect(plannerUtil.benefitCheck()).toStrictEqual(testCase.expected); + }); + }); +}); + +describe('์œ ํ‹ธ ํ…Œ์ŠคํŠธ', () => { + const testCases = [ + { + menu: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-5', '์ดˆ์ฝ”์ผ€์ดํฌ-2'], + day: '25', + expected: 3400 + 4046 + 1000 + 25000, + }, + { + menu: ['์•„์ด์Šคํฌ๋ฆผ-5', '์ดˆ์ฝ”์ผ€์ดํฌ-2'], + day: '21', + expected: 3000 + 14161, + }, + ]; + + testCases.forEach((testCase) => { + test(`์ด ํ˜œํƒ๊ธˆ์•ก ํ…Œ์ŠคํŠธ : ${testCase.day}์ผ`, () => { + const PLANNER_DATA = new PlannerData(); + PLANNER_DATA.updateDate(testCase.day); + PLANNER_DATA.updateFood(testCase.menu); + const plannerUtil = new PlannerUtils( + PLANNER_DATA.getUserOrder(), + PLANNER_DATA.getDate(), + ); + plannerUtil.benefitCheck(); + expect(plannerUtil.calcBenefitAmount()).toBe(testCase.expected); + }); + }); +}); + +describe('์œ ํ‹ธ ํ…Œ์ŠคํŠธ', () => { + const testCases = [ + { + menu: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-5', '์ดˆ์ฝ”์ผ€์ดํฌ-2'], + day: '25', + discount: 3400 + 4046 + 1000, + }, + { + menu: ['์•„์ด์Šคํฌ๋ฆผ-5', '์ดˆ์ฝ”์ผ€์ดํฌ-2'], + day: '21', + discount: 3000 + 14161, + }, + { + menu: ['ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€-1', '๋ ˆ๋“œ์™€์ธ-1', '์ดˆ์ฝ”์ผ€์ดํฌ-1'], + day: '12', + discount: 2100 + 2023, + }, + { + menu: ['ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€-1', '๋ ˆ๋“œ์™€์ธ-1', '์ดˆ์ฝ”์ผ€์ดํฌ-1'], + day: '26', + discount: 2023, + }, + { + menu: ['ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€-1', '๋ ˆ๋“œ์™€์ธ-1'], + day: '26', + discount: 0, + }, + ]; + + testCases.forEach((testCase) => { + test(`ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก : ${testCase.day}์ผ`, () => { + const PLANNER_DATA = new PlannerData(); + PLANNER_DATA.updateDate(testCase.day); + PLANNER_DATA.updateFood(testCase.menu); + const plannerUtil = new PlannerUtils( + PLANNER_DATA.getUserOrder(), + PLANNER_DATA.getDate(), + ); + plannerUtil.benefitCheck(); + const expected = plannerUtil.getTotalAmount() - testCase.discount; + expect(plannerUtil.calcTotalPayment()).toBe(expected); + }); + }); +});
JavaScript
์ €๋Š” ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋ฅผ ์งœ๋Š”๊ฒŒ ๋„ˆ๋ฌด ์–ด๋ ค์› ์–ด์„œ,, ๋งŽ์€ ์ฐธ๊ณ ๊ฐ€ ๋ผ์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,123 @@ +import { FOOD_MENU } from '../constants/FoodMenu.js'; +import { + EVENT_CONST, + DAY_OF_WEEK, + UTILS_CONST, +} from '../constants/Constants.js'; + +class PlannerUtils { + #userOrder; + + #userDate; + + #userDay; + + #userEvent; + + constructor(userOrder, userDate) { + this.#userOrder = userOrder; + this.#userDate = userDate; + this.#userDay = new Date(UTILS_CONST.userDay(this.#userDate)).getDay(); + this.#userEvent = { ...EVENT_CONST }; + } + + getTotalAmount() { + const MENU_COST = {}; + Object.values(FOOD_MENU).forEach((course) => { + Object.values(course).forEach((menu) => { + MENU_COST[menu[0]] = menu[1]; + }); + }); + return this.#sumAmount(MENU_COST); + } + + #sumAmount(MENU_COST) { + const totalAmount = []; + this.#userOrder.forEach((order) => { + totalAmount.push(MENU_COST[order[0]] * Number(order[1])); + }); + return totalAmount.reduce((total, cost) => total + cost); + } + + benefitCheck() { + if (this.getTotalAmount() >= UTILS_CONST.minGetBenefit) { + this.#userEvent.christmas = this.#christmasCheck(); + this.#userEvent.weekDay = this.#weekDaysCheck(); + this.#userEvent.weekendDay = this.#weekendDayCheck(); + this.#userEvent.specialDay = this.#specialDayCheck(); + this.#userEvent.benefitEvent = this.#giftCheck(); + } + return this.#userEvent; + } + + #christmasCheck() { + if (this.#userDate > UTILS_CONST.christmasEventEnd) { + return UTILS_CONST.noBenefit; + } + return UTILS_CONST.christmasBenefit(this.#userDate); + } + + #weekDaysCheck() { + if (DAY_OF_WEEK.weekDay.includes(this.#userDay)) { + return ( + this.#checkEventMenu(UTILS_CONST.weekDayEvent) * UTILS_CONST.discount + ); + } + return UTILS_CONST.noBenefit; + } + + #weekendDayCheck() { + if (DAY_OF_WEEK.weekendDay.includes(this.#userDay)) { + return ( + this.#checkEventMenu(UTILS_CONST.weekendEvent) * UTILS_CONST.discount + ); + } + return UTILS_CONST.noBenefit; + } + + #specialDayCheck() { + if (DAY_OF_WEEK.specialDay.includes(this.#userDay)) { + return UTILS_CONST.specialDiscount; + } + if (DAY_OF_WEEK.specialDay.includes(this.#userDate)) { + return UTILS_CONST.specialDiscount; + } + return UTILS_CONST.noBenefit; + } + + #giftCheck() { + if (this.getTotalAmount() > UTILS_CONST.minGetGift) { + return UTILS_CONST.giftCost; + } + return UTILS_CONST.noBenefit; + } + + #checkEventMenu(event) { + const numberOfMenu = [0]; + const eventMenu = Object.values(FOOD_MENU[event]).map((menu) => menu[0]); + this.#userOrder.forEach((menu) => { + if (eventMenu.includes(menu[0])) { + numberOfMenu.push(Number(menu[1])); + } + }); + return numberOfMenu.reduce((total, amount) => total + amount); + } + + calcBenefitAmount() { + const totalBenefit = Object.values(this.#userEvent).reduce( + (total, amount) => total + amount, + ); + return totalBenefit; + } + + calcTotalPayment() { + if (this.#userEvent.benefitEvent !== 0) { + return ( + this.getTotalAmount() - this.calcBenefitAmount() + UTILS_CONST.giftCost + ); + } + return this.getTotalAmount() - this.calcBenefitAmount(); + } +} + +export default PlannerUtils;
JavaScript
์ข‹์€ ์˜๊ฒฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! `์—ฌ๊ธฐ์—” ํŒŒ์ผ์ด ๋”ฐ๋กœ ์•ˆ ๋ณด์ด๋Š”๋ฐ, ์ „์— PR ๋กœ ๋ดค์—ˆ์„ ๋•Œ, App.js ์—์„œ #PLANNER ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜์…จ๋Š”๋ฐ, ํ˜น์‹œ ๋Œ€๋ฌธ์ž๋กœ ใ……ํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ ๊ฐ€์š”๏ผŸ๏ผŸ` ๊ฐœ์ธ์ ์œผ๋กœ ์ƒ์„ฑ์ž์—์„œ ์ •์˜๋˜๋Š” ํด๋ž˜์Šค๋Š” ๊ตฌ๋ถ„์„ ๋ช…ํ™•ํžˆ ํ•˜๊ธฐ์œ„ํ•ด ๋Œ€๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,32 @@ +import { ERROR_MSG } from '../../constants/PlannerMsg.js'; + +class DateValidate { + #regax; + + #date; + + constructor(regax, date) { + this.#regax = regax; + this.#date = date; + } + + regaxCheck() { + if (this.#regax.test(this.#date)) { + throw new Error(ERROR_MSG.dateError); + } + } + + safeCheck() { + if (!Number.isSafeInteger(Number(this.#date))) { + throw new Error(ERROR_MSG.dateError); + } + } + + rangeCheck() { + if (Number(this.#date) <= 0 || Number(this.#date) > 31) { + throw new Error(ERROR_MSG.dateError); + } + } +} + +export default DateValidate;
JavaScript
์•— MVCํŒจํ„ด ์•ˆ์—์„œ๋งŒ ์ƒ์ˆ˜ํ™”๋ฅผ ์ƒ๊ฐํ–ˆ์ง€ ์ด๋ถ€๋ถ„์„ ๊นœ๋นกํ–ˆ๋„ค์š”..! ์ข‹์€ ์˜๊ฒฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,28 @@ +import DateValidate from './validator/DateValidate.js'; +import MenuValidate from './validator/MenuValidate.js'; + +class InputValidator { + #REGAX; + + constructor() { + this.#REGAX = /\s|[!@#$%^&*(),?":{}|<>]|[a-zA-Z]/; + } + + async dateValidate(date) { + const dateValidator = new DateValidate(this.#REGAX, date); + dateValidator.regaxCheck(); + dateValidator.safeCheck(); + dateValidator.rangeCheck(); + } + + async menuValidate(menu) { + const menuValidator = new MenuValidate(menu); + menuValidator.menuCheck(); + menuValidator.menuAmountCheck(this.#REGAX); + menuValidator.formCheck(); + menuValidator.duplicateCheck(); + menuValidator.drinkCheck(); + } +} + +export default InputValidator;
JavaScript
์ •๊ทœ์‹์œผ๋กœ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๋กœ์ง์„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ํ–ˆ์œผ๋ฉด ์ข‹์•˜์„ ๊ฑฐ๋ž€ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›์€ ์ž…์žฅ์œผ๋กœ์„œ.. ์ •๊ทœ์‹ ์‚ฌ์šฉ์„ ๊น”๋”ํ•˜์‹  ์  ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค ๐Ÿ™Œ๐Ÿป
@@ -0,0 +1,34 @@ +export const EVENT_CONST = { + christmas: 0, + weekDay: 0, + weekendDay: 0, + specialDay: 0, + benefitEvent: 0, +}; + +export const DAY_OF_WEEK = { + sun: 0, + mon: 1, + tue: 2, + wed: 3, + thu: 4, + fri: 5, + sat: 6, + weekDay: [0, 1, 2, 3, 4], + weekendDay: [5, 6], + specialDay: [0, 25], +}; + +export const UTILS_CONST = { + userDay: (userDate) => `2023-12-${userDate}`, + christmasBenefit: (userDate) => 1000 + (userDate - 1) * 100, + minGetBenefit: 10000, + christmasEventEnd: 25, + noBenefit: 0, + discount: 2023, + weekDayEvent: 'dessert', + weekendEvent: 'main', + specialDiscount: 1000, + minGetGift: 120000, + giftCost: 25000, +};
JavaScript
์ด๋ฏธ ์ƒ์ˆ˜ ์„ ์–ธ์œผ๋กœ const ๋ผ๋Š” ๋‹จ์–ด๋Š” ๋‚ดํฌํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์š”! UTILS, EVENT ๋กœ ์„ ์–ธํ•˜๊ณ  ๋‚ด๋ถ€ ์ƒ์ˆ˜๋“ค์˜ ๋ช…์นญ์„ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ์ž‘์„ฑํ•˜์‹œ๋Š” ๋ฐฉ๋ฒ•๋„ ์ข‹์•˜์„๊ฑฐ๋ผ๊ตฌ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž
@@ -0,0 +1,96 @@ +import OutputView from '../views/OutputView.js'; +import InputView from '../views/InputView.js'; +import PlannerData from '../model/PlannerData.js'; +import PlannerUtils from '../utils/PlannerUtills.js'; +import { STATUS_MSG } from '../constants/PlannerMsg.js'; + +class PlannerController { + constructor() { + this.PLANNER_DATA = new PlannerData(); + } + + async plannerStart() { + OutputView.printStatusMsg(STATUS_MSG.welcomeMsg); + await this.#inputVisitDay(); + } + + async #inputVisitDay() { + try { + const visitDate = await InputView.readDate(); + this.PLANNER_DATA.updateDate(visitDate); + return this.#orderMenu(); + } catch (error) { + OutputView.printError(error.message); + return this.#inputVisitDay(); + } + } + + async #orderMenu() { + try { + const foodAndAmount = await InputView.readMenu(); + this.PLANNER_DATA.updateFood(foodAndAmount); + return this.#showPlanner(); + } catch (error) { + OutputView.printError(error.message); + return this.#orderMenu(); + } + } + + #showPlanner() { + OutputView.printStatusMsg( + STATUS_MSG.showPlanner(this.PLANNER_DATA.getDate()), + ); + this.#showOrderMenu(); + } + + #showOrderMenu() { + OutputView.printStatusMsg(STATUS_MSG.orderMenu); + OutputView.printOrderMenu(this.PLANNER_DATA.getUserOrder()); + this.#totalOrderAmount(); + } + + #totalOrderAmount() { + OutputView.printStatusMsg(STATUS_MSG.totalAmount); + const plannerUtils = new PlannerUtils( + this.PLANNER_DATA.getUserOrder(), + this.PLANNER_DATA.getDate(), + ); + OutputView.printTotalAmount(plannerUtils.getTotalAmount()); + this.#giftCheck(plannerUtils); + } + + #giftCheck(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.giftMenu); + OutputView.printGift(plannerUtils.getTotalAmount()); + this.#benefitCheck(plannerUtils); + } + + #benefitCheck(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.userBenefit); + this.#showBenefits(plannerUtils.benefitCheck(), plannerUtils); + } + + #showBenefits(benefitObject, plannerUtils) { + OutputView.printBenefits(benefitObject); + this.#showTotalBenefit(plannerUtils); + } + + #showTotalBenefit(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.totalBenefit); + OutputView.printTotalBenefit(plannerUtils.calcBenefitAmount()); + this.#showPayment(plannerUtils); + } + + #showPayment(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.payment); + OutputView.printPayment(plannerUtils.calcTotalPayment()); + this.#userEventBadge(plannerUtils); + } + + #userEventBadge(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.eventBadge); + OutputView.printEventBadge(plannerUtils.calcBenefitAmount()); + } +} + +export default PlannerController;
JavaScript
์ธํ’‹์„ ๋ฐ›๋Š” ๊ตฌ์กฐ๊ฐ€ ์œ ์‚ฌํ•˜๋‹ˆ, ์ธํ’‹ ํ•ธ๋“ค๋ง๊ณผ ํŠธ๋ผ์ด ์บ์น˜ ์‚ฌ์šฉ์˜ ์ค‘๋ณต์„ ํ•˜๋‚˜์˜ ๋ฉ”์†Œ๋“œ๋กœ ๋”ฐ๋กœ ์„ ์–ธํ•˜๊ณ  ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋”๋ผ๊ตฌ์š” ์ถ”์ฒœ๋“œ๋ฆฌ๊ตฌ ๊ฐ‘๋‹ˆ๋‹ค ๐Ÿ™Œ๐Ÿป
@@ -0,0 +1,21 @@ +import { Console } from '@woowacourse/mission-utils'; +import { INPUT_MSG } from '../constants/PlannerMsg.js'; +import InputValidator from '../utils/InputValidator.js'; + +const InputView = { + INPUT_VAL: new InputValidator(), + + async readDate() { + const userDate = await Console.readLineAsync(INPUT_MSG.inputVisitDay); + await this.INPUT_VAL.dateValidate(userDate); + return userDate; + }, + + async readMenu() { + const userMenu = await Console.readLineAsync(INPUT_MSG.orderMenu); + await this.INPUT_VAL.menuValidate(userMenu.split(',')); + return userMenu.split(','); + }, +}; + +export default InputView;
JavaScript
์ธํ’‹ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์— ๋Œ€ํ•œ ํด๋ž˜์Šค๋ฅผ ๋”ฐ๋กœ ์„ ์–ธํ•˜์…”์„œ ๋ทฐ์—์„œ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์ด ๋˜ ์ƒˆ๋กญ๋„ค์š” !! ์ €๋Š” ์ธํ’‹ ๋ฐ›๋Š” ๋‚ด๋ถ€์—์„œ ๊ตฌํ˜„ํ•˜๊ณ , ๋ถ„๋ฆฌํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ƒ๊ฐ๋งŒ ํ–ˆ์—ˆ๋Š”๋ฐ ์ด ๋ฐฉ์‹๋„ ํšจ์œจ์ ์ธ๊ฑฐ ๊ฐ™์•„์š” โ˜บ๏ธ
@@ -0,0 +1,96 @@ +import OutputView from '../views/OutputView.js'; +import InputView from '../views/InputView.js'; +import PlannerData from '../model/PlannerData.js'; +import PlannerUtils from '../utils/PlannerUtills.js'; +import { STATUS_MSG } from '../constants/PlannerMsg.js'; + +class PlannerController { + constructor() { + this.PLANNER_DATA = new PlannerData(); + } + + async plannerStart() { + OutputView.printStatusMsg(STATUS_MSG.welcomeMsg); + await this.#inputVisitDay(); + } + + async #inputVisitDay() { + try { + const visitDate = await InputView.readDate(); + this.PLANNER_DATA.updateDate(visitDate); + return this.#orderMenu(); + } catch (error) { + OutputView.printError(error.message); + return this.#inputVisitDay(); + } + } + + async #orderMenu() { + try { + const foodAndAmount = await InputView.readMenu(); + this.PLANNER_DATA.updateFood(foodAndAmount); + return this.#showPlanner(); + } catch (error) { + OutputView.printError(error.message); + return this.#orderMenu(); + } + } + + #showPlanner() { + OutputView.printStatusMsg( + STATUS_MSG.showPlanner(this.PLANNER_DATA.getDate()), + ); + this.#showOrderMenu(); + } + + #showOrderMenu() { + OutputView.printStatusMsg(STATUS_MSG.orderMenu); + OutputView.printOrderMenu(this.PLANNER_DATA.getUserOrder()); + this.#totalOrderAmount(); + } + + #totalOrderAmount() { + OutputView.printStatusMsg(STATUS_MSG.totalAmount); + const plannerUtils = new PlannerUtils( + this.PLANNER_DATA.getUserOrder(), + this.PLANNER_DATA.getDate(), + ); + OutputView.printTotalAmount(plannerUtils.getTotalAmount()); + this.#giftCheck(plannerUtils); + } + + #giftCheck(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.giftMenu); + OutputView.printGift(plannerUtils.getTotalAmount()); + this.#benefitCheck(plannerUtils); + } + + #benefitCheck(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.userBenefit); + this.#showBenefits(plannerUtils.benefitCheck(), plannerUtils); + } + + #showBenefits(benefitObject, plannerUtils) { + OutputView.printBenefits(benefitObject); + this.#showTotalBenefit(plannerUtils); + } + + #showTotalBenefit(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.totalBenefit); + OutputView.printTotalBenefit(plannerUtils.calcBenefitAmount()); + this.#showPayment(plannerUtils); + } + + #showPayment(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.payment); + OutputView.printPayment(plannerUtils.calcTotalPayment()); + this.#userEventBadge(plannerUtils); + } + + #userEventBadge(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.eventBadge); + OutputView.printEventBadge(plannerUtils.calcBenefitAmount()); + } +} + +export default PlannerController;
JavaScript
์—ฌ๊ฒฝ๋‹˜ ์ฝ”๋“œ๋ณด๊ณ  ์ €๋„ ์—ญ์‹œ ์ค„์—ฌ์•ผ๊ฒ ๋‹ค ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค! ์ข‹์€ ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,96 @@ +import OutputView from '../views/OutputView.js'; +import InputView from '../views/InputView.js'; +import PlannerData from '../model/PlannerData.js'; +import PlannerUtils from '../utils/PlannerUtills.js'; +import { STATUS_MSG } from '../constants/PlannerMsg.js'; + +class PlannerController { + constructor() { + this.PLANNER_DATA = new PlannerData(); + } + + async plannerStart() { + OutputView.printStatusMsg(STATUS_MSG.welcomeMsg); + await this.#inputVisitDay(); + } + + async #inputVisitDay() { + try { + const visitDate = await InputView.readDate(); + this.PLANNER_DATA.updateDate(visitDate); + return this.#orderMenu(); + } catch (error) { + OutputView.printError(error.message); + return this.#inputVisitDay(); + } + } + + async #orderMenu() { + try { + const foodAndAmount = await InputView.readMenu(); + this.PLANNER_DATA.updateFood(foodAndAmount); + return this.#showPlanner(); + } catch (error) { + OutputView.printError(error.message); + return this.#orderMenu(); + } + } + + #showPlanner() { + OutputView.printStatusMsg( + STATUS_MSG.showPlanner(this.PLANNER_DATA.getDate()), + ); + this.#showOrderMenu(); + } + + #showOrderMenu() { + OutputView.printStatusMsg(STATUS_MSG.orderMenu); + OutputView.printOrderMenu(this.PLANNER_DATA.getUserOrder()); + this.#totalOrderAmount(); + } + + #totalOrderAmount() { + OutputView.printStatusMsg(STATUS_MSG.totalAmount); + const plannerUtils = new PlannerUtils( + this.PLANNER_DATA.getUserOrder(), + this.PLANNER_DATA.getDate(), + ); + OutputView.printTotalAmount(plannerUtils.getTotalAmount()); + this.#giftCheck(plannerUtils); + } + + #giftCheck(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.giftMenu); + OutputView.printGift(plannerUtils.getTotalAmount()); + this.#benefitCheck(plannerUtils); + } + + #benefitCheck(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.userBenefit); + this.#showBenefits(plannerUtils.benefitCheck(), plannerUtils); + } + + #showBenefits(benefitObject, plannerUtils) { + OutputView.printBenefits(benefitObject); + this.#showTotalBenefit(plannerUtils); + } + + #showTotalBenefit(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.totalBenefit); + OutputView.printTotalBenefit(plannerUtils.calcBenefitAmount()); + this.#showPayment(plannerUtils); + } + + #showPayment(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.payment); + OutputView.printPayment(plannerUtils.calcTotalPayment()); + this.#userEventBadge(plannerUtils); + } + + #userEventBadge(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.eventBadge); + OutputView.printEventBadge(plannerUtils.calcBenefitAmount()); + } +} + +export default PlannerController;
JavaScript
์ €๋„ ์ €๋ฒˆ์—๋Š” catch๋ถ€๋ฌธ์— ๋‹ค์‹œ inputVisitDay๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์žฌ๊ท€์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์–ด ํ…Œ์ŠคํŠธ๊ฐ€ ํ†ต๊ณผ๊ฐ€ ์•ˆ ๋๋Š”๋ฐ @JonghyunLEE12 ๋‹˜์€ inputVisitDay๋ฅผ ๋ฐ”๋กœ returnํ•˜๋Š”๊ฑธ๋กœ ํ•ด๊ฒฐํ•˜์…จ๋„ค์š”. ์ด๋ฒˆ์— ์ €๋Š” while(true) ๊ตฌ๋ฌธ ์•ˆ์—์„œ validํ•œ ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์œผ๋ฉด while์„ ๋‚˜๊ฐ€๋Š” ๊ฒƒ์œผ๋กœ ์ž‘์„ฑํ•ด๋ดค๋Š”๋ฐ ๊ฐœ์ธ์ ์œผ๋กœ ๋” ๊น”๋”ํ•œ ์ฒ˜๋ฆฌ๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“ค๋”๋ผ๊ณ ์š” ํ•œ๋ฒˆ ์ฐธ๊ณ ํ•ด๋ณด์…”๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,125 @@ +import { ERROR_MSG } from '../../constants/PlannerMsg.js'; +import { FOOD_MENU } from '../../constants/FoodMenu.js'; + +class MenuValidate { + #canOrder; + + #userMenu; + + #menu; + + constructor(menu, canOrder = [], userMenu = []) { + this.#menu = menu; + this.#canOrder = canOrder; + this.#userMenu = userMenu; + } + + menuCheck() { + this.#findingMenu(this.#canOrder, this.#userMenu, this.#menu); + this.#userMenu.forEach((order) => { + if (!this.#canOrder.includes(order)) { + throw new Error(ERROR_MSG.notInMenu); + } + }); + } + + #findingMenu() { + Object.keys(FOOD_MENU).map((course) => + this.#makeCanOrder(course, this.#canOrder), + ); + + this.#menu.forEach((order) => this.#userMenu.push(order.split('-')[0])); + } + + #makeCanOrder(course) { + Object.values(FOOD_MENU[`${course}`]).map((food) => + this.#canOrder.push(food[0]), + ); + } + + menuAmountCheck(regax) { + const totalMenuAmount = []; + this.#menu.forEach((order) => { + this.#amountCheck(order.split('-')[1], regax); + totalMenuAmount.push(Number(order.split('-')[1])); + }); + + this.#totalAmountCheck(totalMenuAmount); + } + + #amountCheck(amount, regax) { + if (regax.test(Number(amount))) { + throw new Error(ERROR_MSG.notInMenu); + } + if (Number(amount) <= 0) { + throw new Error(ERROR_MSG.notInMenu); + } + + if (!Number.isSafeInteger(Number(amount))) { + throw new Error(ERROR_MSG.notInMenu); + } + } + + #totalAmountCheck(total) { + const checkTotal = total.reduce((sum, amount) => sum + amount); + if (checkTotal > 20) { + throw new Error(ERROR_MSG.totalError); + } + } + + formCheck() { + const form = /^[๊ฐ€-ํžฃ]+-[0-9]+$/; + this.#menu.forEach((order) => this.#formRegaxCheck(order, form)); + } + + #formRegaxCheck(order, form) { + if (!form.test(order)) { + throw new Error(ERROR_MSG.notInMenu); + } + } + + duplicateCheck() { + if (this.#userMenu.length !== new Set(this.#userMenu).size) { + throw new Error(ERROR_MSG.notInMenu); + } + } + + drinkCheck() { + const menuObject = this.#makingMenuObject(); + if (this.#objectCheck(menuObject)) { + throw new Error(ERROR_MSG.drinkError); + } + } + + #makingMenuObject() { + const menuObject = {}; + Object.keys(FOOD_MENU).forEach((course) => { + menuObject[course] = this.#userMenuCourses(course); + }); + return menuObject; + } + + #userMenuCourses(course) { + const includeCourse = []; + Object.values(FOOD_MENU[course]).forEach((eachMenu) => { + if (this.#menuIncludes(eachMenu)) { + includeCourse.push(eachMenu[0]); + } + }); + return includeCourse.length; + } + + #menuIncludes(eachMenu) { + return this.#userMenu.includes(eachMenu[0]); + } + + #objectCheck(menuObject) { + const { appetizer, main, dessert, drink } = menuObject; + if (appetizer === 0 && main === 0 && dessert === 0 && drink !== 0) { + return true; + } + return false; + } +} + +export default MenuValidate;
JavaScript
๋ฉ”๋‰ด ์ •๊ทœํ‘œํ˜„์‹์€ ์ž˜ ์ž‘์„ฑํ•˜์…จ๋Š”๋ฐ ๋ฉ”๋‰ด๊ฐฏ์ˆ˜๋Š” ์ฒซ ์ˆซ์ž๋กœ 0์ด ๋“ค์–ด๊ฐ€๋Š”๊ฑธ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด [0-9]+๋Š” ๋ณด๋‹ค [1-9]\d*๊ฐ€ ๋” ์ ์ ˆํ•˜์ง€ ์•Š์„๊นŒ ์‹ถ์Šต๋‹ˆ๋‹ค. (์ฒซ ์ˆซ์ž๋Š” 1์—์„œ9๋งŒ ๊ฐ€๋Šฅํ•˜๊ณ  ๋’ท ์ˆซ์ž๋Š” ๋ญ ๋“  0๊ฐœ ์ด์ƒ ์ด๋Ÿฐ์‹์œผ๋กœ์š”)
@@ -0,0 +1,60 @@ +/* eslint-disable max-lines-per-function */ +import InputValidator from '../../src/domain/utils/InputValidator.js'; + +describe('๋‚ ์งœ ํ…Œ์ŠคํŠธ', () => { + const userInput = new InputValidator(); + const testCases = [ + { date: 'a', expectedError: '[ERROR]' }, + { date: ' ', expectedError: '[ERROR]' }, + { date: '์ด์‹ญ์˜ค์ผ', expectedError: '[ERROR]' }, + { date: '32', expectedError: '[ERROR]' }, + { date: '0', expectedError: '[ERROR]' }, + { date: '25.5', expectedError: '[ERROR]' }, + { date: '-1', expectedError: '[ERROR]' }, + { date: ' 12', expectedError: '[ERROR]' }, + { date: '1 2', expectedError: '[ERROR]' }, + { date: '12 ', expectedError: '[ERROR]' }, + ]; + + testCases.forEach((testCase) => { + test(`๋‚ ์งœ ํ…Œ์ŠคํŠธ, ๋‚ ์งœ: ${testCase.date}`, async () => { + await expect(userInput.dateValidate(testCase.date)).rejects.toThrow( + testCases.expectedError, + ); + }); + }); +}); + +describe('๋ฉ”๋‰ด ํ…Œ์ŠคํŠธ', () => { + const menuValidate = new InputValidator(); + const testCases = [ + { menu: ['์งฌ๋ฝ•-1'], expectedError: '[ERROR]' }, + { menu: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-0'], expectedError: '[ERROR]' }, + { menu: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-1.5'], expectedError: '[ERROR]' }, + { menu: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-a'], expectedError: '[ERROR]' }, + { menu: ['์ œ๋กœ์ฝœ๋ผ1', '๋ ˆ๋“œ์™€์ธ1'], expectedError: '[ERROR]' }, + { menu: ['๊น€๋ฐฅ-1', '๋–ก๋ณถ์ด-1'], expectedError: '[ERROR]' }, + { menu: ['์ œ๋กœ์ฝœ๋ผ-์ผ', '๋ ˆ๋“œ์™€์ธ-์ด'], expectedError: '[ERROR]' }, + { menu: ['์ œ๋กœ์ฝœ๋ผ-a', '๋ ˆ๋“œ์™€์ธ-b'], expectedError: '[ERROR]' }, + { menu: ['ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2', 'ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-1'], expectedError: '[ERROR]' }, + { menu: ['์ œ๋กœ์ฝœ๋ผ-1'], expectedError: '[ERROR]' }, + { menu: ['๋ ˆ๋“œ์™€์ธ-1', '์ œ๋กœ์ฝœ๋ผ-1'], expectedError: '[ERROR]' }, + { menu: ['์‹œ์ €์ƒ๋Ÿฌ๋“œ- 1', '์ œ๋กœ์ฝœ๋ผ- 1'], expectedError: '[ERROR]' }, + { + menu: ['๋ ˆ๋“œ์™€์ธ-2', '์ œ๋กœ์ฝœ๋ผ-2', '์ƒดํŽ˜์ธ-2'], + expectedError: '[ERROR]', + }, + { + menu: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ-10', '๋ ˆ๋“œ์™€์ธ-20', '์ œ๋กœ์ฝœ๋ผ-20'], + expectedError: '[ERROR]', + }, + ]; + + testCases.forEach((testCase) => { + test(`๋ฉ”๋‰ด ํ…Œ์ŠคํŠธ, ๋ฉ”๋‰ด: ${testCase.menu}`, async () => { + await expect(menuValidate.menuValidate(testCase.menu)).rejects.toThrow( + testCase.expectedError, + ); + }); + }); +});
JavaScript
์ด์‹ญ์˜ค์ผใ…‹ใ…‹ ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋ฅผ ์ •๋ง ์ž์„ธํ•˜๊ฒŒ ์งœ์…จ๋„ค์š” ๋งŽ์ด ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค!
@@ -0,0 +1,32 @@ +import { ERROR_MSG } from '../../constants/PlannerMsg.js'; + +class DateValidate { + #regax; + + #date; + + constructor(regax, date) { + this.#regax = regax; + this.#date = date; + } + + regaxCheck() { + if (this.#regax.test(this.#date)) { + throw new Error(ERROR_MSG.dateError); + } + } + + safeCheck() { + if (!Number.isSafeInteger(Number(this.#date))) { + throw new Error(ERROR_MSG.dateError); + } + } + + rangeCheck() { + if (Number(this.#date) <= 0 || Number(this.#date) > 31) { + throw new Error(ERROR_MSG.dateError); + } + } +} + +export default DateValidate;
JavaScript
๊ฐœ์ธ์ ์œผ๋กœ this.#regax๋ฅผ ํ•„๋“œ๋กœ ๋‘์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค. ์ƒ์ˆ˜ํ™”ํ•ด์„œ ๋ฐ”๋กœ ๊ฐ€์ ธ์™€ ์“ฐ์‹œ์ง€์•Š๊ณ  new DateValidate()ํ• ๋•Œ regex๋ฅผ ์ „๋‹ฌํ•˜์‹  ๋‹ค๋ฅธ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,125 @@ +import { ERROR_MSG } from '../../constants/PlannerMsg.js'; +import { FOOD_MENU } from '../../constants/FoodMenu.js'; + +class MenuValidate { + #canOrder; + + #userMenu; + + #menu; + + constructor(menu, canOrder = [], userMenu = []) { + this.#menu = menu; + this.#canOrder = canOrder; + this.#userMenu = userMenu; + } + + menuCheck() { + this.#findingMenu(this.#canOrder, this.#userMenu, this.#menu); + this.#userMenu.forEach((order) => { + if (!this.#canOrder.includes(order)) { + throw new Error(ERROR_MSG.notInMenu); + } + }); + } + + #findingMenu() { + Object.keys(FOOD_MENU).map((course) => + this.#makeCanOrder(course, this.#canOrder), + ); + + this.#menu.forEach((order) => this.#userMenu.push(order.split('-')[0])); + } + + #makeCanOrder(course) { + Object.values(FOOD_MENU[`${course}`]).map((food) => + this.#canOrder.push(food[0]), + ); + } + + menuAmountCheck(regax) { + const totalMenuAmount = []; + this.#menu.forEach((order) => { + this.#amountCheck(order.split('-')[1], regax); + totalMenuAmount.push(Number(order.split('-')[1])); + }); + + this.#totalAmountCheck(totalMenuAmount); + } + + #amountCheck(amount, regax) { + if (regax.test(Number(amount))) { + throw new Error(ERROR_MSG.notInMenu); + } + if (Number(amount) <= 0) { + throw new Error(ERROR_MSG.notInMenu); + } + + if (!Number.isSafeInteger(Number(amount))) { + throw new Error(ERROR_MSG.notInMenu); + } + } + + #totalAmountCheck(total) { + const checkTotal = total.reduce((sum, amount) => sum + amount); + if (checkTotal > 20) { + throw new Error(ERROR_MSG.totalError); + } + } + + formCheck() { + const form = /^[๊ฐ€-ํžฃ]+-[0-9]+$/; + this.#menu.forEach((order) => this.#formRegaxCheck(order, form)); + } + + #formRegaxCheck(order, form) { + if (!form.test(order)) { + throw new Error(ERROR_MSG.notInMenu); + } + } + + duplicateCheck() { + if (this.#userMenu.length !== new Set(this.#userMenu).size) { + throw new Error(ERROR_MSG.notInMenu); + } + } + + drinkCheck() { + const menuObject = this.#makingMenuObject(); + if (this.#objectCheck(menuObject)) { + throw new Error(ERROR_MSG.drinkError); + } + } + + #makingMenuObject() { + const menuObject = {}; + Object.keys(FOOD_MENU).forEach((course) => { + menuObject[course] = this.#userMenuCourses(course); + }); + return menuObject; + } + + #userMenuCourses(course) { + const includeCourse = []; + Object.values(FOOD_MENU[course]).forEach((eachMenu) => { + if (this.#menuIncludes(eachMenu)) { + includeCourse.push(eachMenu[0]); + } + }); + return includeCourse.length; + } + + #menuIncludes(eachMenu) { + return this.#userMenu.includes(eachMenu[0]); + } + + #objectCheck(menuObject) { + const { appetizer, main, dessert, drink } = menuObject; + if (appetizer === 0 && main === 0 && dessert === 0 && drink !== 0) { + return true; + } + return false; + } +} + +export default MenuValidate;
JavaScript
์•— ์ด๋ถ€๋ถ„์€ ์ œ๊ฐ€ ์ œ์ถœํ•˜๊ณ ๋‚˜์„œ ๊นจ๋‹ฌ์•˜๋„ค์š”ใ…œใ…œ ์ €๋„ ์ฐธ ์•„์‰ฝ์Šต๋‹ˆ๋‹ค. ์ข‹์€ ์˜๊ฒฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,32 @@ +import { ERROR_MSG } from '../../constants/PlannerMsg.js'; + +class DateValidate { + #regax; + + #date; + + constructor(regax, date) { + this.#regax = regax; + this.#date = date; + } + + regaxCheck() { + if (this.#regax.test(this.#date)) { + throw new Error(ERROR_MSG.dateError); + } + } + + safeCheck() { + if (!Number.isSafeInteger(Number(this.#date))) { + throw new Error(ERROR_MSG.dateError); + } + } + + rangeCheck() { + if (Number(this.#date) <= 0 || Number(this.#date) > 31) { + throw new Error(ERROR_MSG.dateError); + } + } +} + +export default DateValidate;
JavaScript
InputValidate ํด๋ž˜์Šค์•ˆ์— Date ์™€ Menu ์— ๋Œ€ํ•ด ๋‘๊ฐ€์ง€ ๊ฒ€์‚ฌ๋ฅผ ์ •์˜ํ•ด์คฌ์Šต๋‹ˆ๋‹ค! ์šฐ์„  ์ „์ฒด์ ์ธ ์ •๊ทœํ‘œํ˜„์‹์„ ์ƒ์œ„์—์„œ ์ •์˜ํ•˜๊ณ  ํ•˜์œ„ ๊ฐ์ฒด๋“ค์—๊ฒŒ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ์š”. MenuValidate ์•ˆ์—๋Š” form ์ด๋ผ๋Š” ์ •๊ทœํ‘œํ˜„์‹์ด ๋˜ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ ๊ฐ์ฒด ์•ˆ์— ๋‹ค๋ฅธ ์ •๊ทœํ‘œํ˜„์‹์ด ์กด์žฌํ•˜๋Š”๊ฒƒ ๋ณด๋‹ค๋Š” ๋”ฐ๋กœ ์ •์˜๋ฅผ ํ•ด์ฃผ๋Š”๊ฒŒ ๋” ์˜ˆ๋ป๋ณด์—ฌ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,96 @@ +import OutputView from '../views/OutputView.js'; +import InputView from '../views/InputView.js'; +import PlannerData from '../model/PlannerData.js'; +import PlannerUtils from '../utils/PlannerUtills.js'; +import { STATUS_MSG } from '../constants/PlannerMsg.js'; + +class PlannerController { + constructor() { + this.PLANNER_DATA = new PlannerData(); + } + + async plannerStart() { + OutputView.printStatusMsg(STATUS_MSG.welcomeMsg); + await this.#inputVisitDay(); + } + + async #inputVisitDay() { + try { + const visitDate = await InputView.readDate(); + this.PLANNER_DATA.updateDate(visitDate); + return this.#orderMenu(); + } catch (error) { + OutputView.printError(error.message); + return this.#inputVisitDay(); + } + } + + async #orderMenu() { + try { + const foodAndAmount = await InputView.readMenu(); + this.PLANNER_DATA.updateFood(foodAndAmount); + return this.#showPlanner(); + } catch (error) { + OutputView.printError(error.message); + return this.#orderMenu(); + } + } + + #showPlanner() { + OutputView.printStatusMsg( + STATUS_MSG.showPlanner(this.PLANNER_DATA.getDate()), + ); + this.#showOrderMenu(); + } + + #showOrderMenu() { + OutputView.printStatusMsg(STATUS_MSG.orderMenu); + OutputView.printOrderMenu(this.PLANNER_DATA.getUserOrder()); + this.#totalOrderAmount(); + } + + #totalOrderAmount() { + OutputView.printStatusMsg(STATUS_MSG.totalAmount); + const plannerUtils = new PlannerUtils( + this.PLANNER_DATA.getUserOrder(), + this.PLANNER_DATA.getDate(), + ); + OutputView.printTotalAmount(plannerUtils.getTotalAmount()); + this.#giftCheck(plannerUtils); + } + + #giftCheck(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.giftMenu); + OutputView.printGift(plannerUtils.getTotalAmount()); + this.#benefitCheck(plannerUtils); + } + + #benefitCheck(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.userBenefit); + this.#showBenefits(plannerUtils.benefitCheck(), plannerUtils); + } + + #showBenefits(benefitObject, plannerUtils) { + OutputView.printBenefits(benefitObject); + this.#showTotalBenefit(plannerUtils); + } + + #showTotalBenefit(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.totalBenefit); + OutputView.printTotalBenefit(plannerUtils.calcBenefitAmount()); + this.#showPayment(plannerUtils); + } + + #showPayment(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.payment); + OutputView.printPayment(plannerUtils.calcTotalPayment()); + this.#userEventBadge(plannerUtils); + } + + #userEventBadge(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.eventBadge); + OutputView.printEventBadge(plannerUtils.calcBenefitAmount()); + } +} + +export default PlannerController;
JavaScript
์ฒ˜์Œ ์‹œ์ž‘์—์„œ๋ถ€ํ„ฐ ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ๋๋‚˜๋ฉด ๋‹ค์Œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์‹์œผ๋กœ ๋งˆ๋ฌด๋ฆฌ๋ฅผ ํ•˜์—ฌ controller์ชฝ์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”๋ฐ ๊ทธ๋ ‡๊ฒŒ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋ฅผ ์—ฐ๊ฒฐ์„ ์ง€์œผ์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”?
@@ -0,0 +1,21 @@ +import { Console } from '@woowacourse/mission-utils'; +import { INPUT_MSG } from '../constants/PlannerMsg.js'; +import InputValidator from '../utils/InputValidator.js'; + +const InputView = { + INPUT_VAL: new InputValidator(), + + async readDate() { + const userDate = await Console.readLineAsync(INPUT_MSG.inputVisitDay); + await this.INPUT_VAL.dateValidate(userDate); + return userDate; + }, + + async readMenu() { + const userMenu = await Console.readLineAsync(INPUT_MSG.orderMenu); + await this.INPUT_VAL.menuValidate(userMenu.split(',')); + return userMenu.split(','); + }, +}; + +export default InputView;
JavaScript
๋‚ ์งœ๋ฅผ ๋ฐ›์•„์˜ค์ž๋งˆ์ž ๋ฐ”๋กœ validate๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๊ดœ์ฐฎ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. controller๋กœ ๋ฐ›์•„์˜ค๋Š” ๊ฒƒ๋ณด๋‹ค ์ด ํŽธ์ด ํ›จ์”ฌ ๋ณด๊ธฐ ์ข‹์€ ๊ฒƒ ๊ฐ™๋„ค์š”
@@ -0,0 +1,28 @@ +import DateValidate from './validator/DateValidate.js'; +import MenuValidate from './validator/MenuValidate.js'; + +class InputValidator { + #REGAX; + + constructor() { + this.#REGAX = /\s|[!@#$%^&*(),?":{}|<>]|[a-zA-Z]/; + } + + async dateValidate(date) { + const dateValidator = new DateValidate(this.#REGAX, date); + dateValidator.regaxCheck(); + dateValidator.safeCheck(); + dateValidator.rangeCheck(); + } + + async menuValidate(menu) { + const menuValidator = new MenuValidate(menu); + menuValidator.menuCheck(); + menuValidator.menuAmountCheck(this.#REGAX); + menuValidator.formCheck(); + menuValidator.duplicateCheck(); + menuValidator.drinkCheck(); + } +} + +export default InputValidator;
JavaScript
๋ฐ›์•„์˜จ ๊ฐ’์„ ๊ฒ€์‚ฌํ•  ๋•Œ ํ•œ๋ฒˆ ๋ณด๋‚ด์„œ ์ •๊ทœ์‹ ํ•ด์„œ ๊ทธ๊ฒƒ์„ ๋‹ค์‹œ ๊ฒ€์‚ฌํ•˜๋Š” ์‹์œผ๋กœ ํ•˜์‹ ๊ฒƒ ๊ฐ™์€๋ฐ ์ด๋ ‡๊ฒŒ ์ •๊ทœ์‹์„ ํ•œ ๋‹จ๊ณ„๋กœ ๋”ฐ๋กœ ๋นผ์‹  ์ด์œ ๊ฐ€ ์žˆ์„ ์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,96 @@ +import OutputView from '../views/OutputView.js'; +import InputView from '../views/InputView.js'; +import PlannerData from '../model/PlannerData.js'; +import PlannerUtils from '../utils/PlannerUtills.js'; +import { STATUS_MSG } from '../constants/PlannerMsg.js'; + +class PlannerController { + constructor() { + this.PLANNER_DATA = new PlannerData(); + } + + async plannerStart() { + OutputView.printStatusMsg(STATUS_MSG.welcomeMsg); + await this.#inputVisitDay(); + } + + async #inputVisitDay() { + try { + const visitDate = await InputView.readDate(); + this.PLANNER_DATA.updateDate(visitDate); + return this.#orderMenu(); + } catch (error) { + OutputView.printError(error.message); + return this.#inputVisitDay(); + } + } + + async #orderMenu() { + try { + const foodAndAmount = await InputView.readMenu(); + this.PLANNER_DATA.updateFood(foodAndAmount); + return this.#showPlanner(); + } catch (error) { + OutputView.printError(error.message); + return this.#orderMenu(); + } + } + + #showPlanner() { + OutputView.printStatusMsg( + STATUS_MSG.showPlanner(this.PLANNER_DATA.getDate()), + ); + this.#showOrderMenu(); + } + + #showOrderMenu() { + OutputView.printStatusMsg(STATUS_MSG.orderMenu); + OutputView.printOrderMenu(this.PLANNER_DATA.getUserOrder()); + this.#totalOrderAmount(); + } + + #totalOrderAmount() { + OutputView.printStatusMsg(STATUS_MSG.totalAmount); + const plannerUtils = new PlannerUtils( + this.PLANNER_DATA.getUserOrder(), + this.PLANNER_DATA.getDate(), + ); + OutputView.printTotalAmount(plannerUtils.getTotalAmount()); + this.#giftCheck(plannerUtils); + } + + #giftCheck(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.giftMenu); + OutputView.printGift(plannerUtils.getTotalAmount()); + this.#benefitCheck(plannerUtils); + } + + #benefitCheck(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.userBenefit); + this.#showBenefits(plannerUtils.benefitCheck(), plannerUtils); + } + + #showBenefits(benefitObject, plannerUtils) { + OutputView.printBenefits(benefitObject); + this.#showTotalBenefit(plannerUtils); + } + + #showTotalBenefit(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.totalBenefit); + OutputView.printTotalBenefit(plannerUtils.calcBenefitAmount()); + this.#showPayment(plannerUtils); + } + + #showPayment(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.payment); + OutputView.printPayment(plannerUtils.calcTotalPayment()); + this.#userEventBadge(plannerUtils); + } + + #userEventBadge(plannerUtils) { + OutputView.printStatusMsg(STATUS_MSG.eventBadge); + OutputView.printEventBadge(plannerUtils.calcBenefitAmount()); + } +} + +export default PlannerController;
JavaScript
Controller์—๋Š” ์ด๋ฒคํŠธ ํ”Œ๋ ˆ๋„ˆ์˜ ์ž…๋ ฅ,์ถœ๋ ฅ๋“ฑ์˜ ์ˆœ์„œ๋ฅผ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ˆœ์„œ๋“ค์„ ํ•˜๋‚˜์˜ ๋ฉ”์†Œ๋“œ๋กœ ์ •์˜ํ–ˆ๊ณ , ์ด๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,28 @@ +import DateValidate from './validator/DateValidate.js'; +import MenuValidate from './validator/MenuValidate.js'; + +class InputValidator { + #REGAX; + + constructor() { + this.#REGAX = /\s|[!@#$%^&*(),?":{}|<>]|[a-zA-Z]/; + } + + async dateValidate(date) { + const dateValidator = new DateValidate(this.#REGAX, date); + dateValidator.regaxCheck(); + dateValidator.safeCheck(); + dateValidator.rangeCheck(); + } + + async menuValidate(menu) { + const menuValidator = new MenuValidate(menu); + menuValidator.menuCheck(); + menuValidator.menuAmountCheck(this.#REGAX); + menuValidator.formCheck(); + menuValidator.duplicateCheck(); + menuValidator.drinkCheck(); + } +} + +export default InputValidator;
JavaScript
์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋Š” InputValidate ์•ˆ์—์„œ, MenuValidate, DateValidate ์˜ ๊ฐ์ฒด๋“ค๋กœ ๋‚˜๋‰˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ƒ์œ„์—์„œ ์ •๊ทœ์‹์„ ์ •์˜ํ•ด์ฃผ๊ณ , ์ •๊ทœ์‹์„ ์จ์•ผํ•˜๋Š” ๊ฒ€์‚ฌ ํ•„์š”์‹œ, ํ•˜์œ„ ๊ฐ์ฒด๋“ค์—๊ฒŒ ๋‚ด๋ ค์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,21 @@ +/* eslint-disable max-lines-per-function */ +import { OUTPUT_MSG } from '../../src/domain/constants/PlannerMsg.js'; + +describe('๋ฐฐ์ง€ ํ…Œ์ŠคํŠธ', () => { + const testCases = [ + { amount: 1000, expected: '์—†์Œ' }, + { amount: 4900, expected: '์—†์Œ' }, + { amount: 5000, expected: '๋ณ„' }, + { amount: 9900, expected: '๋ณ„' }, + { amount: 10000, expected: 'ํŠธ๋ฆฌ' }, + { amount: 19999, expected: 'ํŠธ๋ฆฌ' }, + { amount: 20000, expected: '์‚ฐํƒ€' }, + { amount: 50000, expected: '์‚ฐํƒ€' }, + ]; + + testCases.forEach((testCase) => { + test(`๋ฐฐ์ง€ ํ…Œ์ŠคํŠธ : ${testCase.amount}`, () => { + expect(OUTPUT_MSG.userBadge(testCase.amount)).toBe(testCase.expected); + }); + }); +});
JavaScript
์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ตฐ์š”!
@@ -0,0 +1,37 @@ +/** + * +<์• ํ”ผํƒ€์ด์ €> +์–‘์†ก์ด์ˆ˜ํ”„(6,000), ํƒ€ํŒŒ์Šค(5,500), ์‹œ์ €์ƒ๋Ÿฌ๋“œ(8,000) + +<๋ฉ”์ธ> +ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ(55,000), ๋ฐ”๋น„ํ๋ฆฝ(54,000), ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€(35,000), ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€(25,000) + +<๋””์ €ํŠธ> +์ดˆ์ฝ”์ผ€์ดํฌ(15,000), ์•„์ด์Šคํฌ๋ฆผ(5,000) + +<์Œ๋ฃŒ> +์ œ๋กœ์ฝœ๋ผ(3,000), ๋ ˆ๋“œ์™€์ธ(60,000), ์ƒดํŽ˜์ธ(25,000) + */ + +export const FOOD_MENU = { + appetizer: { + soup: ['์–‘์†ก์ด์ˆ˜ํ”„', 6000], + tapas: ['ํƒ€ํŒŒ์Šค', 5500], + salad: ['์‹œ์ €์ƒ๋Ÿฌ๋“œ', 8000], + }, + main: { + stake: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', 55000], + barbeque: ['๋ฐ”๋น„ํ๋ฆฝ', 54000], + seafood: ['ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€', 35000], + christmas: ['ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€', 25000], + }, + dessert: { + cake: ['์ดˆ์ฝ”์ผ€์ดํฌ', 15000], + iceCream: ['์•„์ด์Šคํฌ๋ฆผ', 5000], + }, + drink: { + coke: ['์ œ๋กœ์ฝœ๋ผ', 3000], + wine: ['๋ ˆ๋“œ์™€์ธ', 60000], + champane: ['์ƒดํŽ˜์ธ', 25000], + }, +};
JavaScript
FOOD_MENU ๊ฐ์ฒด๋ฅผ ๊นŠ์€ ๋™๊ฒฐํ•˜๋Š” ๊ฑด ์–ด๋– ์‹ ๊ฐ€์š”?
@@ -0,0 +1,37 @@ +/** + * +<์• ํ”ผํƒ€์ด์ €> +์–‘์†ก์ด์ˆ˜ํ”„(6,000), ํƒ€ํŒŒ์Šค(5,500), ์‹œ์ €์ƒ๋Ÿฌ๋“œ(8,000) + +<๋ฉ”์ธ> +ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ(55,000), ๋ฐ”๋น„ํ๋ฆฝ(54,000), ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€(35,000), ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€(25,000) + +<๋””์ €ํŠธ> +์ดˆ์ฝ”์ผ€์ดํฌ(15,000), ์•„์ด์Šคํฌ๋ฆผ(5,000) + +<์Œ๋ฃŒ> +์ œ๋กœ์ฝœ๋ผ(3,000), ๋ ˆ๋“œ์™€์ธ(60,000), ์ƒดํŽ˜์ธ(25,000) + */ + +export const FOOD_MENU = { + appetizer: { + soup: ['์–‘์†ก์ด์ˆ˜ํ”„', 6000], + tapas: ['ํƒ€ํŒŒ์Šค', 5500], + salad: ['์‹œ์ €์ƒ๋Ÿฌ๋“œ', 8000], + }, + main: { + stake: ['ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ', 55000], + barbeque: ['๋ฐ”๋น„ํ๋ฆฝ', 54000], + seafood: ['ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€', 35000], + christmas: ['ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€', 25000], + }, + dessert: { + cake: ['์ดˆ์ฝ”์ผ€์ดํฌ', 15000], + iceCream: ['์•„์ด์Šคํฌ๋ฆผ', 5000], + }, + drink: { + coke: ['์ œ๋กœ์ฝœ๋ผ', 3000], + wine: ['๋ ˆ๋“œ์™€์ธ', 60000], + champane: ['์ƒดํŽ˜์ธ', 25000], + }, +};
JavaScript
์ข‹์€ ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! @joywhy pr ๋งํฌ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๋งž๋ฆฌ๋ทฐ ๊ฐˆ๊ฒŒ์š”!!
@@ -0,0 +1,37 @@ +package christmas.service; + +import christmas.domain.Order; +import christmas.domain.constant.Message; +import christmas.domain.constant.dish.Appetizer; +import christmas.domain.constant.dish.Beverage; +import christmas.domain.constant.dish.Dessert; +import christmas.domain.constant.dish.MainDish; +import christmas.domain.constant.dish.Orderable; +import java.util.Map; +import java.util.Objects; + +public class OrderMaker { + public Order make(Map<String, Integer> parsedOrder) { + Order order = new Order(); + parsedOrder.forEach((dishLabel, count) -> order.addMenu(findDish(dishLabel), count)); + order.validate(); + return order; + } + + private Orderable findDish(String input) { + if (Objects.nonNull(Appetizer.valueOfLabel(input))) { + return Appetizer.valueOfLabel(input); + } + if (Objects.nonNull(Beverage.valueOfLabel(input))) { + return Beverage.valueOfLabel(input); + } + if (Objects.nonNull(Dessert.valueOfLabel(input))) { + return Dessert.valueOfLabel(input); + } + if (Objects.nonNull(MainDish.valueOfLabel(input))) { + return MainDish.valueOfLabel(input); + } + throw new IllegalArgumentException(Message.INVALID_ORDER.getContent()); + } + +}
Java
๋ฉ”๋‰ด enum ํƒ€์ž…์ด ์žˆ์—ˆ์œผ๋ฉด ํ•œ ๋ฒˆ์— ์ฐพ์„ ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,38 @@ +package christmas.domain.constant.dish; + +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public enum MainDish implements Orderable { + T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55000), + BARBEQUE_RIBS("๋ฐ”๋น„ํ๋ฆฝ", 54000), + SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35000), + CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25000); + + private static final Map<String, MainDish> BY_LABEL = + Stream.of(MainDish.values()).collect(Collectors.toMap(MainDish::getLabel, e -> e)); + + private final String label; + private final int price; + + MainDish(String label, int price) { + this.label = label; + this.price = price; + } + + @Override + public String getLabel() { + return label; + } + + @Override + public int getPrice() { + return price; + } + + public static MainDish valueOfLabel(String label) { + return BY_LABEL.get(label); + } + +}
Java
static์œผ๋กœ Map์„ ์ƒ์„ฑํ•ด๋‘” ๊ฑฐ ์ข‹์€ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ BY_LABEL ์ด๋ฆ„์„ ๋Œ€๋ฌธ์ž๋กœ ํ•˜๋Š” ๊ฒƒ์ด ์ปจ๋ฒค์…˜์ธ์ง€ ์ €๋„ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋”๋ผ๊ณ ์š”. ์ƒ์ˆ˜๋Š” ์•„๋‹Œ๋ฐ static final์ด๊ธฐ๋Š” ํ•˜๊ณ  ๋ญ๊ฐ€ ๋งž์„๊นŒ์š”?
@@ -0,0 +1,31 @@ +package christmas.service; + +import christmas.domain.DecemberDate; +import christmas.domain.constant.Discount; +import christmas.domain.constant.dish.Orderable; +import christmas.domain.dto.BenefitDto; +import christmas.service.discountcalculator.SpecialDiscountCalculator; +import christmas.service.discountcalculator.WeekDiscountCalculator; +import christmas.service.discountcalculator.XmasDdayDiscountCalculator; +import java.util.Map; + +public class DiscountManager { + public BenefitDto applyDiscount(Map<Orderable, Integer> menus, DecemberDate visitDate) { + int totalCost = getTotalCost(menus); + BenefitDto benefitDto = new BenefitDto(menus, totalCost); + if (totalCost < Discount.DISCOUNT_APPLY_LOWER_BOUND.getValue()) { + return benefitDto; + } + XmasDdayDiscountCalculator.apply(benefitDto, visitDate); + WeekDiscountCalculator.apply(menus, benefitDto, visitDate); + SpecialDiscountCalculator.apply(benefitDto, visitDate); + return benefitDto; + } + + private int getTotalCost(Map<Orderable, Integer> menus) { + return menus.entrySet().stream() + .mapToInt(entry -> entry.getKey().getPrice() * entry.getValue()) + .sum(); + } + +}
Java
menus๋ณด๋‹ค Order๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ๊ฒƒ์ด ๋” ๊ฐ์ฒด์ง€ํ–ฅ์— ๋งž๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Order ๋‚ด๋ถ€์— getTotalCost ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด ์–ด๋–จ๊นŒ์š”? ๊ทธ๋Ÿฌ๋ฉด ์ž์‹ ์˜ ๋ฐ์ดํ„ฐ๋กœ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,21 @@ +package christmas.service; + +import christmas.domain.constant.Benefit; +import christmas.domain.dto.BenefitDto; + +public class PresentationManager { + private static final int PRESENTATION_THRESHOLD = 120000; + private static final int PRESENTATION_PRICE = 25000; + + private PresentationManager() { + + } + + public static void present(BenefitDto benefitDto) { + if (benefitDto.getTotalCost() >= PRESENTATION_THRESHOLD) { + benefitDto.addBenefit(Benefit.PRESENTATION, PRESENTATION_PRICE); + benefitDto.presentChampagne(); + } + } + +}
Java
120_000 ์ฒ˜๋Ÿผ _ ์„ ๋ถ™์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,21 @@ +package christmas.service; + +import christmas.domain.constant.Benefit; +import christmas.domain.dto.BenefitDto; + +public class PresentationManager { + private static final int PRESENTATION_THRESHOLD = 120000; + private static final int PRESENTATION_PRICE = 25000; + + private PresentationManager() { + + } + + public static void present(BenefitDto benefitDto) { + if (benefitDto.getTotalCost() >= PRESENTATION_THRESHOLD) { + benefitDto.addBenefit(Benefit.PRESENTATION, PRESENTATION_PRICE); + benefitDto.presentChampagne(); + } + } + +}
Java
private์œผ๋กœ ์ƒ์„ฑ์„ ๋ง‰์•„์ค€๋А ๊ฒƒ ์ข‹์Šต๋‹ˆ๋‹ค! ๋‹ค๋งŒ ๋‚ด์šฉ์ด ์—†์œผ๋ฉด ๋นˆ์ค„๋„ ์—†๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,24 @@ +package christmas.service.discountcalculator; + +import static christmas.domain.constant.Discount.DATE_OF_CHRISTMAS; +import static christmas.domain.constant.Discount.ONE_WEEK; +import static christmas.domain.constant.Discount.SPECIAL_DISCOUNT_AMOUNT; +import static christmas.domain.constant.Discount.THREE; + +import christmas.domain.DecemberDate; +import christmas.domain.constant.Benefit; +import christmas.domain.dto.BenefitDto; + +public class SpecialDiscountCalculator { + private SpecialDiscountCalculator() { + + } + + public static void apply(BenefitDto benefitDto, DecemberDate visitDate) { + if (visitDate.date() == DATE_OF_CHRISTMAS.getValue() + || visitDate.date() % ONE_WEEK.getValue() == THREE.getValue()) { + benefitDto.addBenefit(Benefit.SPECIAL_DISCOUNT, SPECIAL_DISCOUNT_AMOUNT.getValue()); + } + } + +}
Java
๊ธด ์กฐ๊ฑด์‹์€ ๋ฉ”์†Œ๋“œ๋กœ ๋ฝ‘๋Š” ๊ฒƒ์ด ๊ฐ€๋…์„ฑ์— ์ข‹์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,70 @@ +package christmas.service.discountcalculator; + +import static christmas.domain.constant.Discount.ONE; +import static christmas.domain.constant.Discount.ONE_WEEK; +import static christmas.domain.constant.Discount.TWO; +import static christmas.domain.constant.Discount.WEEK_DISCOUNT_UNIT; +import static christmas.domain.constant.Discount.ZERO; + +import christmas.domain.DecemberDate; +import christmas.domain.constant.Benefit; +import christmas.domain.constant.dish.Dessert; +import christmas.domain.constant.dish.MainDish; +import christmas.domain.constant.dish.Orderable; +import christmas.domain.dto.BenefitDto; +import java.util.Map; +import java.util.Map.Entry; + +public class WeekDiscountCalculator { + private WeekDiscountCalculator() { + + } + + public static void apply(Map<Orderable, Integer> menus, BenefitDto benefitDto, DecemberDate visitDate) { + if (isWeekend(visitDate)) { + applyWeekendDiscount(menus, benefitDto); + return; + } + applyWeekdayDiscount(menus, benefitDto); + } + + private static void applyWeekendDiscount(Map<Orderable, Integer> menus, BenefitDto benefitDto) { + if (getMainDishCount(menus) == ZERO.getValue()) { + return; + } + benefitDto.addBenefit(Benefit.WEEK_END_DISCOUNT, getMainDishCount(menus) * WEEK_DISCOUNT_UNIT.getValue()); + } + + private static void applyWeekdayDiscount(Map<Orderable, Integer> menus, BenefitDto benefitDto) { + if (getDessertCount(menus) == ZERO.getValue()) { + return; + } + benefitDto.addBenefit(Benefit.WEEK_DAY_DISCOUNT, getDessertCount(menus) * WEEK_DISCOUNT_UNIT.getValue()); + } + + private static int getDessertCount(Map<Orderable, Integer> menus) { + return countDishesOfCertainCategory(menus, Dessert.class); + } + + private static int getMainDishCount(Map<Orderable, Integer> menus) { + return countDishesOfCertainCategory(menus, MainDish.class); + } + + private static int countDishesOfCertainCategory(Map<Orderable, Integer> menus, + Class<? extends Orderable> category) { + int count = ZERO.getValue(); + for (Entry<Orderable, Integer> entry : menus.entrySet()) { + if (entry.getKey().getClass() == category) { + count += entry.getValue(); + } + } + return count; + } + + private static boolean isWeekend(DecemberDate visitDate) { + int dateNumber = visitDate.date(); + return dateNumber % ONE_WEEK.getValue() == ONE.getValue() + || dateNumber % ONE_WEEK.getValue() == TWO.getValue(); + } + +}
Java
dto์— addํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด Dto์˜ ์˜์กด์„ฑ์„ ๋–จ์–ด๋œจ๋ฆฌ๊ณ  ๋‹ค๋ฅธ Dto์—๋„ ์ „๋‹ฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,8 @@ +public class Car { + public String name; + public int position; + + public int run() { + return this.position++; + } +} \ No newline at end of file
Java
addPosition ๋„ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์— ๋Œ€ํ•œ ์„ค๋ช…์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™์ง€๋งŒ, run ์ด๋ผ๋Š” ํ–‰์œ„๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋‹จ์–ด๋กœ ๋ฉ”์„œ๋“œ๋ช…์„ ์ง€์–ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? ์ถ”ํ›„์— ์ž๋™์ฐจ ๊ธฐ๋ณธ ์ด๋™ ์กฐ๊ฑด์ด +2 ๋กœ ๋ฐ”๋€๋‹ค๊ณ  ๊ฐ€์ • ํ–ˆ์„ ๋•Œ addPosition ์ด๋ฉด ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ž…์žฅ์—์„œ position+1 ์ผ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“œ๋Š”๋ฐ run ์œผ๋กœ ์ถ”์ƒํ™” ๋˜์–ด์žˆ์œผ๋ฉด +2 ๋ฅผ ํ•˜๋“  +1 ์„ ํ•˜๋“  ์ž๋™์ฐจ๊ฐ€ ์ด๋™ํ•œ๋‹ค๋Š” ํ–‰์œ„๋ฅผ ๋‚˜ํƒ€๋‚ด์„œ ํ˜ผ๋™์ด ์—†์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ~
@@ -0,0 +1,8 @@ +public class Car { + public String name; + public int position; + + public int run() { + return this.position++; + } +} \ No newline at end of file
Java
๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” View ์˜ ์—ญํ• ์€ ResultView ์— ์œ„์ž„ํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,8 @@ +public class Car { + public String name; + public int position; + + public int run() { + return this.position++; + } +} \ No newline at end of file
Java
์ถ”ํ›„์— ์ฝ˜์†”์ด ์•„๋‹Œ GUI ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ํ™•์žฅ๋œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ Car ์ด๋ผ๋Š” pure ํ•ด์•ผํ•˜๋Š” ๋ชจ๋ธ ํด๋ž˜์Šค๊ฐ€ ๋”๋Ÿฝํ˜€์ง€์ง€ ์•Š์„๊นŒ์š”~?
@@ -0,0 +1,22 @@ +import java.util.ArrayList; +import java.util.List; + +public class GameWinner { + public List<String> findWinner(List<Car> cars) { + int max = cars.get(0).position; + for (int i = 1; i < cars.size(); i++) { + max = Math.max(max, cars.get(i).position); + } + List<String> winnerList = new ArrayList<>(); + for (Car car : cars) { + addWinnerList(car, max, winnerList); + } + return winnerList; + } + + private void addWinnerList(Car car, int max, List<String> winnerList) { + if (car.position == max) { + winnerList.add(car.name); + } + } +}
Java
์กฐ์Šˆ์•„ ๋ธ”๋กœํฌ์˜ effective java ์—๋Š” '๊ฐ์ฒด๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•ด ์ฐธ์กฐํ•˜๋ผ.' ๋ผ๋Š” ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ์ฝ์–ด๋ณด์‹œ๊ณ  ๊ฐœ์„ ํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” ~ jaehun2841.github.io/2019/03/01/effective-java-item64/#%EC%9C%A0%EC%97%B0%ED%95%9C-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%A8%EC%9D%84-%EC%83%9D%EC%84%B1%ED%95%98%EB%8A%94-%EC%9D%B8%ED%84%B0%ED%8E%98%EC%9D%B4%EC%8A%A4-%ED%83%80%EC%9E%85-%EB%B3%80%EC%88%98
@@ -0,0 +1,22 @@ +import java.util.ArrayList; +import java.util.List; + +public class GameWinner { + public List<String> findWinner(List<Car> cars) { + int max = cars.get(0).position; + for (int i = 1; i < cars.size(); i++) { + max = Math.max(max, cars.get(i).position); + } + List<String> winnerList = new ArrayList<>(); + for (Car car : cars) { + addWinnerList(car, max, winnerList); + } + return winnerList; + } + + private void addWinnerList(Car car, int max, List<String> winnerList) { + if (car.position == max) { + winnerList.add(car.name); + } + } +}
Java
๋ฐฐ์—ด ์‚ฌ์šฉ์„ ์ง€์–‘ํ•˜๊ณ  List ๋กœ ์ตœ๋Œ€ํ•œ ๋„˜๊ฒจ๋ฐ›์œผ๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,18 @@ +import java.util.List; + +public class Main { + public static void main(String[] args) { + InputView inputView = new InputView(); + RacingGame racingGame = new RacingGame(); + GameWinner gameWinner = new GameWinner(); + ResultView resultView = new ResultView(); + + List<Car> cars = inputView.registerCar(inputView.inputCarName()); + int time = inputView.inputTime(); + for (int i = 0; i < time; i++) { + racingGame.raceByTime(cars); + resultView.showResultByTime(cars); + } + resultView.showFinalResult(gameWinner.findWinner(cars)); + } +}
Java
Main ์— ์ฝ˜์†” ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์—†์• ๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”~?
@@ -0,0 +1,29 @@ +import java.util.Random; +import java.util.List; + +public class RacingGame { + private static final int RANDOM_RANGE = 10; + private static final int MIN_MOVE_STANDARD = 4; + + public void raceByTime(List<Car> cars) { + for (Car car : cars) { + int randomValue = giveRandomValue(); + move(car, randomValue); + } + } + + private int giveRandomValue() { + Random random = new Random(); + return random.nextInt(RANDOM_RANGE); + } + + private void move(Car car, int randomValue) { + if (canMove(randomValue)) { + car.run(); + } + } + + private boolean canMove(int number) { + return number >= MIN_MOVE_STANDARD; + } +} \ No newline at end of file
Java
์ ‘๊ทผ์ œ์–ด์ž๋ฅผ ์ƒ๋žตํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? ์ƒ๋žตํ•˜๋ฉด ์–ด๋–ค ์ ‘๊ทผ์ œ์–ด์ž๊ฐ€ ๋ถ™์„๊นŒ์š”?
@@ -0,0 +1,8 @@ +public class Car { + public String name; + public int position; + + public int run() { + return this.position++; + } +} \ No newline at end of file
Java
๋ฉ”์„œ๋“œ๋ช…์„ ์ง€์„ ๋•Œ ์กฐ๊ฑด ๋ณ€๊ฒฝ์˜ ๊ฐ€๋Šฅ์„ฑ์„ ๊ผญ ๊ณ ๋ คํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! ๋‹จ๋ฒˆ์— ์ดํ•ด๋˜๋Š” ์ข‹์€ ์˜ˆ์‹œ ์ œ์‹œํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,8 @@ +public class Car { + public String name; + public int position; + + public int run() { + return this.position++; + } +} \ No newline at end of file
Java
GUI ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ํ™•์žฅ๋  ๊ฒฝ์šฐ๋„ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค๋Š” ์  ๋ช…์‹ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!