code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,68 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; +import java.util.stream.Stream; + +public class InputView { + private static final int MAX_LENGTH = 6; + private static final Scanner scanner = new Scanner(System.in); + + public static List<Car> inputCarName() { + String name = scanner.nextLine(); + String[] splittedInputCarName = splitInputCarName(name); + String[] trimmedInputCarName = trimInputCarName(splittedInputCarName); + + try { + processStreamAboutCheckName(trimmedInputCarName); + } catch(IllegalArgumentException e) { + System.out.println(e.getMessage()); + inputCarName(); + } + + List<Car> cars = new ArrayList<>(); + for(int i = 0; i< splittedInputCarName.length; i++) { + cars.add(new Car(trimmedInputCarName[i])); + } + return cars; + } + + private static String[] splitInputCarName(String name) { + return name.split(","); + } + + private static String[] trimInputCarName(String[] name) { + for(int i = 0; i<name.length; i++) { + name[i] = name[i].trim(); + } + return name; + } + + private static void checkNameLength(String name) { + if(name.length() >= MAX_LENGTH) { + throw new IllegalArgumentException("์ด๋ฆ„์€ 5์ž ์ดํ•˜๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."); + } + } + + private static void processStreamAboutCheckName(String[] trimmedInputCarName) { + Stream<String> stream = Arrays.stream(trimmedInputCarName); + stream.forEach(e -> checkNameLength(e)); + } + + public static int inputTrial() { + return scanner.nextInt(); + } + + public static void inputCarNamesMessage() { + System.out.println("๊ฒฝ์ฃผํ•  ์ž๋™์ฐจ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.(์ด๋ฆ„์€ ์‰ผํ‘œ(,) ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„)"); + } + + public static void inputCountMessage() { + System.out.println("์‹œ๋„ํ•  ํšŸ์ˆ˜๋Š” ๋ช‡ํšŒ์ธ๊ฐ€์š”?"); + } +} + + + + +
Java
๋ฉ”์„œ๋“œ ์ฒด์ด๋‹์„ ์ ์šฉํ•ด์„œ Arrays.stream(trimmedInputCarName).forEach(~~) ๋กœ ์ด์–ด ๋ถ™์ด์‹œ๋ฉด ์–ด๋–จ๊นŒ์š”
@@ -0,0 +1,136 @@ +import java.util.Random; +import java.util.ArrayList; + +public class RacingGame { + static final int MAX_LENGTH = 6; + static final int CAN_GO_NUMBER = 4; + static final int ORIGINAL_POSITION = 0; + + public String[] nameSet(String inputNames) { + String[] names = inputNames.split(","); + for (int i = 0; i < names.length; i++) { + names = nameLengthCheck(names, i); + } + return names; + } + + public ArrayList<Car> registerCarNames(ArrayList<Car> cars, String[] nameSet) { + for (int i = 0; i < nameSet.length; i++) { + cars.add(new Car(nameSet[i], ORIGINAL_POSITION)); + } + return cars; + } + + public String[] nameLengthCheck(String[] names, int i) { + if (names[i].length() >= MAX_LENGTH) { + String inputNames = InputView.overNames(); + names = inputNames.split(","); + } + return names; + } + + public String getInputNames(String names) { + String[] carNames = nameSet(names); + String cars = ""; + for (int i = 0; i < carNames.length; i++) { + cars += carNames[i] + ""; + } + return cars; + } + + public static int randomValue() { + Random random = new Random(); + return random.nextInt(10); + } + + public static ArrayList<Car> valueCheck(ArrayList<Car> cars, int randomValue, int i) { + if (randomValue >= CAN_GO_NUMBER) { + cars.get(i).move(); + } + return cars; + } + + public static ArrayList<Car> randomToNumber(ArrayList<Car> cars) { + for (int i = 0; i < cars.size(); i++) { + valueCheck(cars, randomValue(), i); + } + return cars; + } + + public static Car winnerJudge(ArrayList<Car> cars, int max, int i, String winner) { + if (max < cars.get(i).getPosition()) { + max = cars.get(i).getPosition(); + winner = cars.get(i).getName(); + } + Car passInfo = new Car(winner, max); + return passInfo; + } + + public static int sameScoreCount(int cnt, int max, ArrayList<Car> cars, int i) { + if (max == cars.get(i).getPosition()) { + cnt += 1; + } + return cnt; + } + + public static ArrayList<String> winnersNameSet(ArrayList<String> winners, int max, ArrayList<Car> cars) { + for (int i = 0; i < cars.size(); i++) { + winners = sameScoreWinners(max, cars, winners, i); + } + return winners; + } + + public static ArrayList<String> sameScoreWinners(int max, ArrayList<Car> cars, ArrayList<String> winners, int i) { + if (max == cars.get(i).getPosition()) { + winners.add(cars.get(i).getName()); + } + return winners; + } + + public static ArrayList<String> winnerNameReturn(int cnt, String winner) { + ArrayList<String> winners = new ArrayList<>(); + if (cnt == 0) { + winners.add(winner); + } + return winners; + } + + public static ArrayList<String> winnersNameReturn(int cnt, int max, ArrayList<Car> cars) { + ArrayList<String> winners = new ArrayList<>(cnt); + winners = winnersNameSet(winners, max, cars); + return winners; + } + + public static ArrayList<String> findWinner(ArrayList<Car> cars) { + int max = cars.get(0).getPosition(); + String winner = cars.get(0).getName(); + for (int i = 1; i < cars.size(); i++) { + max = winnerJudge(cars, max, i, winner).getPosition(); + winner = winnerJudge(cars, max, i, winner).getName(); + } + int cnt = 0; + for (int i = 0; i < cars.size(); i++) { + cnt = sameScoreCount(cnt, max, cars, i); + } + ArrayList<String> winners; + winners = winnerNameReturn(cnt, winner); + if (cnt > 0) { + winners = winnersNameReturn(cnt, max, cars); + } + return winners; + } + + public String getWinner(ArrayList<Car> cars, int[] randomNumbers) { + ResultView v = new ResultView(); + + for (int i = 0; i < cars.size(); i++) { + cars = valueCheck(cars, randomNumbers[i], i); + } + ArrayList<String> winners = findWinner(cars); + String winner = ""; + for (int i = 0; i < winners.size(); i++) { + winner += winners.get(i) + " "; + } + return winner; + } +}
Java
๋ฉ”์„œ๋“œ๋“ค์ด static ์ธ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,136 @@ +import java.util.Random; +import java.util.ArrayList; + +public class RacingGame { + static final int MAX_LENGTH = 6; + static final int CAN_GO_NUMBER = 4; + static final int ORIGINAL_POSITION = 0; + + public String[] nameSet(String inputNames) { + String[] names = inputNames.split(","); + for (int i = 0; i < names.length; i++) { + names = nameLengthCheck(names, i); + } + return names; + } + + public ArrayList<Car> registerCarNames(ArrayList<Car> cars, String[] nameSet) { + for (int i = 0; i < nameSet.length; i++) { + cars.add(new Car(nameSet[i], ORIGINAL_POSITION)); + } + return cars; + } + + public String[] nameLengthCheck(String[] names, int i) { + if (names[i].length() >= MAX_LENGTH) { + String inputNames = InputView.overNames(); + names = inputNames.split(","); + } + return names; + } + + public String getInputNames(String names) { + String[] carNames = nameSet(names); + String cars = ""; + for (int i = 0; i < carNames.length; i++) { + cars += carNames[i] + ""; + } + return cars; + } + + public static int randomValue() { + Random random = new Random(); + return random.nextInt(10); + } + + public static ArrayList<Car> valueCheck(ArrayList<Car> cars, int randomValue, int i) { + if (randomValue >= CAN_GO_NUMBER) { + cars.get(i).move(); + } + return cars; + } + + public static ArrayList<Car> randomToNumber(ArrayList<Car> cars) { + for (int i = 0; i < cars.size(); i++) { + valueCheck(cars, randomValue(), i); + } + return cars; + } + + public static Car winnerJudge(ArrayList<Car> cars, int max, int i, String winner) { + if (max < cars.get(i).getPosition()) { + max = cars.get(i).getPosition(); + winner = cars.get(i).getName(); + } + Car passInfo = new Car(winner, max); + return passInfo; + } + + public static int sameScoreCount(int cnt, int max, ArrayList<Car> cars, int i) { + if (max == cars.get(i).getPosition()) { + cnt += 1; + } + return cnt; + } + + public static ArrayList<String> winnersNameSet(ArrayList<String> winners, int max, ArrayList<Car> cars) { + for (int i = 0; i < cars.size(); i++) { + winners = sameScoreWinners(max, cars, winners, i); + } + return winners; + } + + public static ArrayList<String> sameScoreWinners(int max, ArrayList<Car> cars, ArrayList<String> winners, int i) { + if (max == cars.get(i).getPosition()) { + winners.add(cars.get(i).getName()); + } + return winners; + } + + public static ArrayList<String> winnerNameReturn(int cnt, String winner) { + ArrayList<String> winners = new ArrayList<>(); + if (cnt == 0) { + winners.add(winner); + } + return winners; + } + + public static ArrayList<String> winnersNameReturn(int cnt, int max, ArrayList<Car> cars) { + ArrayList<String> winners = new ArrayList<>(cnt); + winners = winnersNameSet(winners, max, cars); + return winners; + } + + public static ArrayList<String> findWinner(ArrayList<Car> cars) { + int max = cars.get(0).getPosition(); + String winner = cars.get(0).getName(); + for (int i = 1; i < cars.size(); i++) { + max = winnerJudge(cars, max, i, winner).getPosition(); + winner = winnerJudge(cars, max, i, winner).getName(); + } + int cnt = 0; + for (int i = 0; i < cars.size(); i++) { + cnt = sameScoreCount(cnt, max, cars, i); + } + ArrayList<String> winners; + winners = winnerNameReturn(cnt, winner); + if (cnt > 0) { + winners = winnersNameReturn(cnt, max, cars); + } + return winners; + } + + public String getWinner(ArrayList<Car> cars, int[] randomNumbers) { + ResultView v = new ResultView(); + + for (int i = 0; i < cars.size(); i++) { + cars = valueCheck(cars, randomNumbers[i], i); + } + ArrayList<String> winners = findWinner(cars); + String winner = ""; + for (int i = 0; i < winners.size(); i++) { + winner += winners.get(i) + " "; + } + return winner; + } +}
Java
๋ชจ๋“  ๋ฉ”์„œ๋“œ๊ฐ€ public ์œผ๋กœ ๊ณต๊ฐœ๋˜์–ด์žˆ๋Š” ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,136 @@ +import java.util.Random; +import java.util.ArrayList; + +public class RacingGame { + static final int MAX_LENGTH = 6; + static final int CAN_GO_NUMBER = 4; + static final int ORIGINAL_POSITION = 0; + + public String[] nameSet(String inputNames) { + String[] names = inputNames.split(","); + for (int i = 0; i < names.length; i++) { + names = nameLengthCheck(names, i); + } + return names; + } + + public ArrayList<Car> registerCarNames(ArrayList<Car> cars, String[] nameSet) { + for (int i = 0; i < nameSet.length; i++) { + cars.add(new Car(nameSet[i], ORIGINAL_POSITION)); + } + return cars; + } + + public String[] nameLengthCheck(String[] names, int i) { + if (names[i].length() >= MAX_LENGTH) { + String inputNames = InputView.overNames(); + names = inputNames.split(","); + } + return names; + } + + public String getInputNames(String names) { + String[] carNames = nameSet(names); + String cars = ""; + for (int i = 0; i < carNames.length; i++) { + cars += carNames[i] + ""; + } + return cars; + } + + public static int randomValue() { + Random random = new Random(); + return random.nextInt(10); + } + + public static ArrayList<Car> valueCheck(ArrayList<Car> cars, int randomValue, int i) { + if (randomValue >= CAN_GO_NUMBER) { + cars.get(i).move(); + } + return cars; + } + + public static ArrayList<Car> randomToNumber(ArrayList<Car> cars) { + for (int i = 0; i < cars.size(); i++) { + valueCheck(cars, randomValue(), i); + } + return cars; + } + + public static Car winnerJudge(ArrayList<Car> cars, int max, int i, String winner) { + if (max < cars.get(i).getPosition()) { + max = cars.get(i).getPosition(); + winner = cars.get(i).getName(); + } + Car passInfo = new Car(winner, max); + return passInfo; + } + + public static int sameScoreCount(int cnt, int max, ArrayList<Car> cars, int i) { + if (max == cars.get(i).getPosition()) { + cnt += 1; + } + return cnt; + } + + public static ArrayList<String> winnersNameSet(ArrayList<String> winners, int max, ArrayList<Car> cars) { + for (int i = 0; i < cars.size(); i++) { + winners = sameScoreWinners(max, cars, winners, i); + } + return winners; + } + + public static ArrayList<String> sameScoreWinners(int max, ArrayList<Car> cars, ArrayList<String> winners, int i) { + if (max == cars.get(i).getPosition()) { + winners.add(cars.get(i).getName()); + } + return winners; + } + + public static ArrayList<String> winnerNameReturn(int cnt, String winner) { + ArrayList<String> winners = new ArrayList<>(); + if (cnt == 0) { + winners.add(winner); + } + return winners; + } + + public static ArrayList<String> winnersNameReturn(int cnt, int max, ArrayList<Car> cars) { + ArrayList<String> winners = new ArrayList<>(cnt); + winners = winnersNameSet(winners, max, cars); + return winners; + } + + public static ArrayList<String> findWinner(ArrayList<Car> cars) { + int max = cars.get(0).getPosition(); + String winner = cars.get(0).getName(); + for (int i = 1; i < cars.size(); i++) { + max = winnerJudge(cars, max, i, winner).getPosition(); + winner = winnerJudge(cars, max, i, winner).getName(); + } + int cnt = 0; + for (int i = 0; i < cars.size(); i++) { + cnt = sameScoreCount(cnt, max, cars, i); + } + ArrayList<String> winners; + winners = winnerNameReturn(cnt, winner); + if (cnt > 0) { + winners = winnersNameReturn(cnt, max, cars); + } + return winners; + } + + public String getWinner(ArrayList<Car> cars, int[] randomNumbers) { + ResultView v = new ResultView(); + + for (int i = 0; i < cars.size(); i++) { + cars = valueCheck(cars, randomNumbers[i], i); + } + ArrayList<String> winners = findWinner(cars); + String winner = ""; + for (int i = 0; i < winners.size(); i++) { + winner += winners.get(i) + " "; + } + return winner; + } +}
Java
RacingGame ์—์„œ Cars ์— ๋Œ€ํ•œ ์ƒํƒœ๋ฅผ ๋“ค๊ณ  ์žˆ์œผ๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,12 @@ +import java.util.ArrayList; + +public class Main { + public static void main(String[] args) { + RacingGame racing = new RacingGame(); + ArrayList<Car> cars = new ArrayList<>(); + cars = racing.registerCarNames(cars, racing.nameSet(InputView.setNames())); + int number = InputView.inputNumber(); + ResultView result = new ResultView(); + result.resultRacing(number, cars); + } +}
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,136 @@ +import java.util.Random; +import java.util.ArrayList; + +public class RacingGame { + static final int MAX_LENGTH = 6; + static final int CAN_GO_NUMBER = 4; + static final int ORIGINAL_POSITION = 0; + + public String[] nameSet(String inputNames) { + String[] names = inputNames.split(","); + for (int i = 0; i < names.length; i++) { + names = nameLengthCheck(names, i); + } + return names; + } + + public ArrayList<Car> registerCarNames(ArrayList<Car> cars, String[] nameSet) { + for (int i = 0; i < nameSet.length; i++) { + cars.add(new Car(nameSet[i], ORIGINAL_POSITION)); + } + return cars; + } + + public String[] nameLengthCheck(String[] names, int i) { + if (names[i].length() >= MAX_LENGTH) { + String inputNames = InputView.overNames(); + names = inputNames.split(","); + } + return names; + } + + public String getInputNames(String names) { + String[] carNames = nameSet(names); + String cars = ""; + for (int i = 0; i < carNames.length; i++) { + cars += carNames[i] + ""; + } + return cars; + } + + public static int randomValue() { + Random random = new Random(); + return random.nextInt(10); + } + + public static ArrayList<Car> valueCheck(ArrayList<Car> cars, int randomValue, int i) { + if (randomValue >= CAN_GO_NUMBER) { + cars.get(i).move(); + } + return cars; + } + + public static ArrayList<Car> randomToNumber(ArrayList<Car> cars) { + for (int i = 0; i < cars.size(); i++) { + valueCheck(cars, randomValue(), i); + } + return cars; + } + + public static Car winnerJudge(ArrayList<Car> cars, int max, int i, String winner) { + if (max < cars.get(i).getPosition()) { + max = cars.get(i).getPosition(); + winner = cars.get(i).getName(); + } + Car passInfo = new Car(winner, max); + return passInfo; + } + + public static int sameScoreCount(int cnt, int max, ArrayList<Car> cars, int i) { + if (max == cars.get(i).getPosition()) { + cnt += 1; + } + return cnt; + } + + public static ArrayList<String> winnersNameSet(ArrayList<String> winners, int max, ArrayList<Car> cars) { + for (int i = 0; i < cars.size(); i++) { + winners = sameScoreWinners(max, cars, winners, i); + } + return winners; + } + + public static ArrayList<String> sameScoreWinners(int max, ArrayList<Car> cars, ArrayList<String> winners, int i) { + if (max == cars.get(i).getPosition()) { + winners.add(cars.get(i).getName()); + } + return winners; + } + + public static ArrayList<String> winnerNameReturn(int cnt, String winner) { + ArrayList<String> winners = new ArrayList<>(); + if (cnt == 0) { + winners.add(winner); + } + return winners; + } + + public static ArrayList<String> winnersNameReturn(int cnt, int max, ArrayList<Car> cars) { + ArrayList<String> winners = new ArrayList<>(cnt); + winners = winnersNameSet(winners, max, cars); + return winners; + } + + public static ArrayList<String> findWinner(ArrayList<Car> cars) { + int max = cars.get(0).getPosition(); + String winner = cars.get(0).getName(); + for (int i = 1; i < cars.size(); i++) { + max = winnerJudge(cars, max, i, winner).getPosition(); + winner = winnerJudge(cars, max, i, winner).getName(); + } + int cnt = 0; + for (int i = 0; i < cars.size(); i++) { + cnt = sameScoreCount(cnt, max, cars, i); + } + ArrayList<String> winners; + winners = winnerNameReturn(cnt, winner); + if (cnt > 0) { + winners = winnersNameReturn(cnt, max, cars); + } + return winners; + } + + public String getWinner(ArrayList<Car> cars, int[] randomNumbers) { + ResultView v = new ResultView(); + + for (int i = 0; i < cars.size(); i++) { + cars = valueCheck(cars, randomNumbers[i], i); + } + ArrayList<String> winners = findWinner(cars); + String winner = ""; + for (int i = 0; i < winners.size(); i++) { + winner += winners.get(i) + " "; + } + return winner; + } +}
Java
๋ฉ”์„œ๋“œ๋ช…์ด ๋Œ€๋ถ€๋ถ„ ๋ช…์‚ฌ์ธ ๊ฒƒ ๊ฐ™์€๋ฐ ๋™์‚ฌํ˜•์œผ๋กœ ๋ฐ”๊ฟ”๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”~ ๊ทธ๋ฆฌ๊ณ  cnt ๊ฐ™์€ ๊ฒฝ์šฐ์—๋„ ํ’€๋„ค์ž„์œผ๋กœ ์ ์–ด์ฃผ๋ฉด ์Šคํ„ฐ๋”” ๊ทœ์น™์ƒ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,136 @@ +import java.util.Random; +import java.util.ArrayList; + +public class RacingGame { + static final int MAX_LENGTH = 6; + static final int CAN_GO_NUMBER = 4; + static final int ORIGINAL_POSITION = 0; + + public String[] nameSet(String inputNames) { + String[] names = inputNames.split(","); + for (int i = 0; i < names.length; i++) { + names = nameLengthCheck(names, i); + } + return names; + } + + public ArrayList<Car> registerCarNames(ArrayList<Car> cars, String[] nameSet) { + for (int i = 0; i < nameSet.length; i++) { + cars.add(new Car(nameSet[i], ORIGINAL_POSITION)); + } + return cars; + } + + public String[] nameLengthCheck(String[] names, int i) { + if (names[i].length() >= MAX_LENGTH) { + String inputNames = InputView.overNames(); + names = inputNames.split(","); + } + return names; + } + + public String getInputNames(String names) { + String[] carNames = nameSet(names); + String cars = ""; + for (int i = 0; i < carNames.length; i++) { + cars += carNames[i] + ""; + } + return cars; + } + + public static int randomValue() { + Random random = new Random(); + return random.nextInt(10); + } + + public static ArrayList<Car> valueCheck(ArrayList<Car> cars, int randomValue, int i) { + if (randomValue >= CAN_GO_NUMBER) { + cars.get(i).move(); + } + return cars; + } + + public static ArrayList<Car> randomToNumber(ArrayList<Car> cars) { + for (int i = 0; i < cars.size(); i++) { + valueCheck(cars, randomValue(), i); + } + return cars; + } + + public static Car winnerJudge(ArrayList<Car> cars, int max, int i, String winner) { + if (max < cars.get(i).getPosition()) { + max = cars.get(i).getPosition(); + winner = cars.get(i).getName(); + } + Car passInfo = new Car(winner, max); + return passInfo; + } + + public static int sameScoreCount(int cnt, int max, ArrayList<Car> cars, int i) { + if (max == cars.get(i).getPosition()) { + cnt += 1; + } + return cnt; + } + + public static ArrayList<String> winnersNameSet(ArrayList<String> winners, int max, ArrayList<Car> cars) { + for (int i = 0; i < cars.size(); i++) { + winners = sameScoreWinners(max, cars, winners, i); + } + return winners; + } + + public static ArrayList<String> sameScoreWinners(int max, ArrayList<Car> cars, ArrayList<String> winners, int i) { + if (max == cars.get(i).getPosition()) { + winners.add(cars.get(i).getName()); + } + return winners; + } + + public static ArrayList<String> winnerNameReturn(int cnt, String winner) { + ArrayList<String> winners = new ArrayList<>(); + if (cnt == 0) { + winners.add(winner); + } + return winners; + } + + public static ArrayList<String> winnersNameReturn(int cnt, int max, ArrayList<Car> cars) { + ArrayList<String> winners = new ArrayList<>(cnt); + winners = winnersNameSet(winners, max, cars); + return winners; + } + + public static ArrayList<String> findWinner(ArrayList<Car> cars) { + int max = cars.get(0).getPosition(); + String winner = cars.get(0).getName(); + for (int i = 1; i < cars.size(); i++) { + max = winnerJudge(cars, max, i, winner).getPosition(); + winner = winnerJudge(cars, max, i, winner).getName(); + } + int cnt = 0; + for (int i = 0; i < cars.size(); i++) { + cnt = sameScoreCount(cnt, max, cars, i); + } + ArrayList<String> winners; + winners = winnerNameReturn(cnt, winner); + if (cnt > 0) { + winners = winnersNameReturn(cnt, max, cars); + } + return winners; + } + + public String getWinner(ArrayList<Car> cars, int[] randomNumbers) { + ResultView v = new ResultView(); + + for (int i = 0; i < cars.size(); i++) { + cars = valueCheck(cars, randomNumbers[i], i); + } + ArrayList<String> winners = findWinner(cars); + String winner = ""; + for (int i = 0; i < winners.size(); i++) { + winner += winners.get(i) + " "; + } + return winner; + } +}
Java
ํ•œ ํด๋ž˜์Šค์—์„œ ์ฑ…์ž„์„ ๋งŽ์ด ๋“ค๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค ๊ธธ์ด๊ฐ€ ๋Š˜์–ด๋‚œ ๊ฒƒ ๊ฐ™์€๋ฐ์š”, winner ์— ๋Œ€ํ•œ ๋กœ์ง์„ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์œ„์ž„ํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,39 @@ +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.ArrayList; + +public class RacingGameTest { + RacingGame r; + ResultView v; + + @Before + public void setUp() { + r = new RacingGame(); + v = new ResultView(); + } + + @Test + public void carNamesInputTest() { + String input = "์•„์†ก, ์œˆ๋„์šฐ, ๊ตฌ๋ฆ„, ๊ทธ๋ฆฌ์ฆˆ, ํ‚ค์บฃ"; + assertEquals("์•„์†ก ์œˆ๋„์šฐ ๊ตฌ๋ฆ„ ๊ทธ๋ฆฌ์ฆˆ ํ‚ค์บฃ", r.getInputNames(input)); + } + + @Test + public void winnerTest() { + ArrayList<Car> cars = new ArrayList<>(); + String input = "์•„์†ก, ์œˆ๋„์šฐ, ๊ตฌ๋ฆ„, ๊ทธ๋ฆฌ์ฆˆ, ํ‚ค์บฃ"; + int number = 1; + int[] randomNumbers = {1, 3, 5, 8, 2}; + cars = r.registerCarNames(cars, r.nameSet(input)); + assertEquals(" ๊ตฌ๋ฆ„ ๊ทธ๋ฆฌ์ฆˆ ", r.getWinner(cars, randomNumbers)); + } + + @After + public void tearDown() { + r = null; + } +} \ No newline at end of file
Java
์Šคํ„ฐ๋”” ๊ทœ์น™ : ๋ณ€์ˆ˜๋ช…์„ ์ค„์—ฌ์“ฐ์ง€ ์•Š๋Š”๋‹ค
@@ -0,0 +1,40 @@ +import java.util.ArrayList; + +public class ResultView { + public static void resultRacing(int number, ArrayList<Car> cars) { + System.out.println("์‹คํ–‰ ๊ฒฐ๊ณผ"); + for (int i = 0; i < number; i++) { + RacingGame.randomToNumber(cars); + resultRacing(cars); + System.out.println(""); + } + ArrayList<String> winners = RacingGame.findWinner(cars); + finalResultView(winners); + } + + public static void resultRacing(ArrayList<Car> cars) { + for (int i = 0; i < cars.size(); i++) { + System.out.println(cars.get(i).getName() + ":" + racingView(cars.get(i).getPosition())); + } + } + + public static String racingView(int n) { + String car = ""; + for (int i = 0; i < n; i++) { + car += "-"; + } + return car; + } + + public static void finalResultView(ArrayList<String> winners) { + if (winners.size() == 1) { + System.out.println(winners.get(0) + "๊ฐ€ ์ตœ์ข… ์šฐ์Šนํ–ˆ์Šต๋‹ˆ๋‹ค."); + } + if (winners.size() > 1) { + for (int i = 0; i < winners.size() - 1; i++) { + System.out.print(winners.get(i) + ","); + } + System.out.println(winners.get(winners.size() - 1) + "๊ฐ€ ์ตœ์ข… ์šฐ์Šนํ–ˆ์Šต๋‹ˆ๋‹ค."); + } + } +}
Java
ResultView ๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ๋ฆ„์ด๋‚˜ ๋กœ์ง์„ ์•Œ ํ•„์š” ์—†์ด ๊ทธ๋ƒฅ ๋“ค์–ด์˜จ input ์— ๋Œ€ํ•ด์„œ ์ถœ๋ ฅ๋งŒ ํ•ด์ฃผ๋Š” ์ •๋„์˜ ์—ญํ• ์„ ํ•˜๋„๋ก ๋ณ€๊ฒฝํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -5,6 +5,7 @@ import com.project.Project.domain.interaction.ReviewLike; import com.project.Project.domain.member.Member; import com.project.Project.domain.member.RecentMapLocation; +import com.project.Project.domain.review.Review; import java.util.List; import java.util.Optional; @@ -21,4 +22,9 @@ public interface MemberService { public List<ReviewLike> getReviewLikeList(Member member); Long delete(Member member); + + + + + }
Java
์ด ๋ฉ”์†Œ๋“œ๋ฅผ ReviewService๋กœ ์˜ฎ๊ธฐ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๊ฐ€๋งŒ๋ณด๋‹ˆ ์ด์ „์— ์ž‘์„ฑํ–ˆ๋˜ ์œ„์˜ getReviewLikeList๋„ ์ž˜๋ชป ์œ„์น˜ํ•ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”
@@ -7,6 +7,7 @@ import com.project.Project.domain.interaction.ReviewLike; import com.project.Project.domain.member.Member; import com.project.Project.domain.member.RecentMapLocation; +import com.project.Project.domain.review.Review; import com.project.Project.repository.interaction.FavoriteRepository; import com.project.Project.repository.interaction.ReviewLikeRepository; import com.project.Project.repository.member.MemberCustomRepository; @@ -79,4 +80,6 @@ public Integer getReviewCnt(Member member) { public List<ReviewLike> getReviewLikeList(Member member) { return reviewLikeRepository.findByReviewLikeStatusAndMember(ReviewLikeStatus.LIKED, member); } + + }
Java
๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ReviewServiceImpl์ชฝ์œผ๋กœ ์˜ฎ๊ธฐ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -2,6 +2,7 @@ import com.project.Project.domain.member.Member; import com.project.Project.domain.review.Review; +import com.querydsl.core.types.dsl.BooleanExpression; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; @@ -40,4 +41,6 @@ public interface ReviewRepository extends JpaRepository<Review, Long> { @Query("select review from Review review where review.author.id = :memberId and review.id = :reviewId") Optional<Review> findReviewByAuthorAndReview(Long memberId, Long reviewId); + + List<Review> findReviewsByAuthor(Member member); }
Java
์ด ๋ฉ”์†Œ๋“œ๋Š” deleted ํ•„๋“œ ์ œ์™ธํ•˜๋Š” ๊ฒƒ ๋ฐ˜์˜ํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -3,21 +3,17 @@ <mapper namespace="com.midnear.midnearshopping.mapper.coupon_point.PointMapper"> <insert id="grantPoints" useGeneratedKeys="true" keyProperty="pointId"> INSERT INTO point ( - amount, reason + amount, reason, review_id, user_id, grant_date ) VALUES ( - #{amount}, #{reason} + #{amount}, #{reason}, #{reviewId}, #{userId}, NOW() ) </insert> - <insert id="setReviewPointAmount"> - INSERT INTO review_point ( - text_review, photo_review - ) VALUES ( - #{textReview}, #{photoReview} - ) - </insert> - <delete id="deletePreviousData"> - delete from review_point - </delete> + <select id="getProductInfoByReviewId" resultType="java.lang.String"> + select concat(op.product_name, ' _ ', op.color) + from reviews r + join order_products op on r.order_product_id = op.order_product_id + where review_id = #{reviewId} + </select> <!--์–˜๋Š” ํŒŒ์ผ ๋”ฐ๋กœ ๋บด๊ธฐ ์• ๋งคํ•ด์„œ ์—ฌ๊ธฐ์— ๋„ฃ์Œ--> <select id="getPointList"> select
Unknown
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค..
@@ -0,0 +1,40 @@ +package com.mybox.domain.storage; + +import com.mybox.domain.common.BaseTimeEntity; +import java.time.LocalDateTime; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import lombok.Getter; +import org.springframework.data.annotation.LastModifiedDate; + +@Getter +@Entity +public class StorageObjectMetaData extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + private StorageObjectType type; + private String extension; + private Long size; + private String path; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "namespace_id") + private Namespace namespace; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "parent_id") + private StorageObjectMetaData parentDirectory; + + @LastModifiedDate + private LocalDateTime lastModifiedAt; + +}
Java
`@Enumerated` ๋ถ™์—ฌ์ฃผ์‹ค๊บผ์ฃ ? ๐Ÿ˜Ž
@@ -0,0 +1,40 @@ +package com.mybox.domain.storage; + +import com.mybox.domain.common.BaseTimeEntity; +import java.time.LocalDateTime; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import lombok.Getter; +import org.springframework.data.annotation.LastModifiedDate; + +@Getter +@Entity +public class StorageObjectMetaData extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + private StorageObjectType type; + private String extension; + private Long size; + private String path; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "namespace_id") + private Namespace namespace; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "parent_id") + private StorageObjectMetaData parentDirectory; + + @LastModifiedDate + private LocalDateTime lastModifiedAt; + +}
Java
์ด๊ฑธ ๋†“์ณค๋„ค์š”;;ใ…Žใ…Ž
@@ -0,0 +1,40 @@ +package com.mybox.domain.storage; + +import com.mybox.domain.common.BaseTimeEntity; +import java.time.LocalDateTime; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import lombok.Getter; +import org.springframework.data.annotation.LastModifiedDate; + +@Getter +@Entity +public class StorageObjectMetaData extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + private StorageObjectType type; + private String extension; + private Long size; + private String path; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "namespace_id") + private Namespace namespace; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "parent_id") + private StorageObjectMetaData parentDirectory; + + @LastModifiedDate + private LocalDateTime lastModifiedAt; + +}
Java
๊ฒจ์šฐ ํ•˜๋‚˜ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค๐Ÿ™
@@ -0,0 +1,40 @@ +package com.mybox.domain.storage; + +import com.mybox.domain.common.BaseTimeEntity; +import java.time.LocalDateTime; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import lombok.Getter; +import org.springframework.data.annotation.LastModifiedDate; + +@Getter +@Entity +public class StorageObjectMetaData extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + private StorageObjectType type; + private String extension; + private Long size; + private String path; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "namespace_id") + private Namespace namespace; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "parent_id") + private StorageObjectMetaData parentDirectory; + + @LastModifiedDate + private LocalDateTime lastModifiedAt; + +}
Java
extension์€ ์–ด๋–ค ๋ณ€์ˆ˜ ์ธ๊ฐ€์š”..?! ์ €๋„ ํด๋”, ํŒŒ์ผ ๊ด€๋ จ ์—”ํ‹ฐํ‹ฐ ์„ค๊ณ„ ์ค‘์ธ๋ฐ ์นผ๋Ÿผ ์„ค์ •์„ ์ •๋ง ์ž˜ํ•˜์‹  ๊ฒƒ ๊ฐ™์•„์š” ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค!! ๐Ÿ‘
@@ -0,0 +1,40 @@ +package com.mybox.domain.storage; + +import com.mybox.domain.common.BaseTimeEntity; +import java.time.LocalDateTime; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import lombok.Getter; +import org.springframework.data.annotation.LastModifiedDate; + +@Getter +@Entity +public class StorageObjectMetaData extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + private StorageObjectType type; + private String extension; + private Long size; + private String path; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "namespace_id") + private Namespace namespace; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "parent_id") + private StorageObjectMetaData parentDirectory; + + @LastModifiedDate + private LocalDateTime lastModifiedAt; + +}
Java
ํŒŒ์ผ ํ™•์žฅ์ž์ž…๋‹ˆ๋‹ค!
@@ -0,0 +1,29 @@ +import { Console } from '@woowacourse/mission-utils'; +import parseInput from './parseInput'; + +const inputHandler = { + async dateHandler(userInput, validate) { + try { + const input = await Console.readLineAsync(userInput); + validate(Number(input)); + return Number(input); + } catch (e) { + Console.print(e.message); + return this.dateHandler(userInput, validate); + } + }, + + async orderHandler(userInput, validate) { + try { + const input = await Console.readLineAsync(userInput); + const menu = parseInput(input); + validate(menu); + return Object.fromEntries(menu); + } catch (e) { + Console.print(e.message); + return this.orderHandler(userInput, validate); + } + }, +}; + +export default inputHandler;
JavaScript
์•„์˜ˆ ์ž…๋ ฅ ๋ฐ›๊ณ ๋‚˜์„œ ๋ฐ”๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์—ˆ๋„ค์š” ์ด ๋ฐฉ๋ฒ•์€ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ใ…Ž.. ์ถ”๊ฐ€๋กœ trim,split,map์„ ๊ฒฐํ•ฉํ•ด์„œ ์“ฐ๋Š” ๊ฒƒ๋ณด๋‹ค ์ •๊ทœํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,58 @@ +import { DATE, DISCOUNT, INFO, MENU, MENU_KIND } from '../Util/constants'; + +class Discount { + calculateDiscount(date, menu) { + const discount = {}; + const addDisCount = (key, value) => { + if (value !== 0) discount[key] = value; + }; + + addDisCount(DISCOUNT.CHRISTMAS, this.isPeriod(date)); + addDisCount(DISCOUNT.WEEK, this.isWeekday(date, menu)); + addDisCount(DISCOUNT.WEEKEND, this.isWeekend(date, menu)); + addDisCount(DISCOUNT.SPECIAL, this.isSpecialDay(date)); + return discount; + } + + isPeriod(date) { + let periodDiscount = 0; + if (date >= DATE.EVENT_START && date <= DATE.EVENT_END) { + periodDiscount += + -DATE.PERIOD_DISCOUNT + (date - 1) * -DATE.PER_DAY_DISCOUNT; + } + return periodDiscount; + } + + isWeekday(date, menu) { + let weekDisCount = 0; + if (!(date % 7 === 1 || date % 7 === 2)) { + let count = 0; + Object.keys(menu).forEach(name => { + if (MENU[name].kind === MENU_KIND.DESSERT) count += menu[name]; + }); + weekDisCount += -INFO.WEEK_DISCOUNT * count; + } + return weekDisCount; + } + + isWeekend(date, menu) { + let weekendDisCount = 0; + if (date % 7 === 1 || date % 7 === 2) { + let count = 0; + Object.keys(menu).forEach(name => { + if (MENU[name].kind === MENU_KIND.MAIN) count += menu[name]; + }); + weekendDisCount += -INFO.WEEK_DISCOUNT * count; + } + return weekendDisCount; + } + + isSpecialDay(date) { + let specialDayDiscount = 0; + if (DATE.SPECIAL_DATE.includes(date)) + specialDayDiscount += -DATE.SPECIAL_DISCOUNT; + return specialDayDiscount; + } +} + +export default Discount;
JavaScript
ํ˜น์‹œ new Date() ๋ง๊ณ  ๋‚˜๋จธ์ง€ ๊ฐ’์„ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”??
@@ -0,0 +1,60 @@ +import { ERROR_MESSAGE, MENU, MENU_KIND } from './constants'; + +const validateDate = date => { + if (!(date >= 1 && date <= 31)) { + throw new Error(ERROR_MESSAGE.INVALID_DATE); + } + if (typeof date !== 'number' || !Number.isInteger(date)) { + throw new Error(ERROR_MESSAGE.INVALID_DATE); + } +}; + +const validateName = menu => { + const checkedName = new Set(); + menu.forEach(([name]) => { + if (!Object.keys(MENU).includes(name)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } + checkedName.add(name); + }); +}; + +const validateDuplicate = menu => { + const checkedName = new Set(); + menu.forEach(([name]) => { + if (checkedName.has(name)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } + checkedName.add(name); + }); +}; + +const validateOnlyDrink = menu => { + const types = new Set(menu.map(([name]) => MENU[name].type)); + if (types.size === 1 && types.has(MENU_KIND.DRINK)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } +}; + +const validateCount = menu => { + if (!menu.every(([, count]) => count >= 1)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } +}; + +const validateTotalCount = menu => { + const total = menu.reduce((acc, [, count]) => acc + count, 0); + if (total > 20) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } +}; + +const validateOrder = order => { + validateName(order); + validateDuplicate(order); + validateOnlyDrink(order); + validateCount(order); + validateTotalCount(order); +}; + +export { validateDate, validateOrder };
JavaScript
ํ•ด๋‹น ๋ถ€๋ถ„๋„ ์ •๊ทœํ‘œํ˜„์‹์„ ์“ฐ๋ฉด if๋ฅผ ํ•˜๋‚˜๋งŒ ์จ๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค !
@@ -0,0 +1,29 @@ +import { Console } from '@woowacourse/mission-utils'; +import parseInput from './parseInput'; + +const inputHandler = { + async dateHandler(userInput, validate) { + try { + const input = await Console.readLineAsync(userInput); + validate(Number(input)); + return Number(input); + } catch (e) { + Console.print(e.message); + return this.dateHandler(userInput, validate); + } + }, + + async orderHandler(userInput, validate) { + try { + const input = await Console.readLineAsync(userInput); + const menu = parseInput(input); + validate(menu); + return Object.fromEntries(menu); + } catch (e) { + Console.print(e.message); + return this.orderHandler(userInput, validate); + } + }, +}; + +export default inputHandler;
JavaScript
์ •๊ทœ ํ‘œํ˜„์‹์„ ํ™œ์šฉํ–ˆ์œผ๋ฉด ๊ตณ์ด ํŒŒ์‹ฑํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜์ง€ ์•Š์•˜์„ ์ˆ˜๋„ ์žˆ๊ฒ ๋„ค์š” ์ข‹์€ ํ”ผ๋“œ๋ฐฑ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž๐Ÿคฃ
@@ -0,0 +1,58 @@ +import { DATE, DISCOUNT, INFO, MENU, MENU_KIND } from '../Util/constants'; + +class Discount { + calculateDiscount(date, menu) { + const discount = {}; + const addDisCount = (key, value) => { + if (value !== 0) discount[key] = value; + }; + + addDisCount(DISCOUNT.CHRISTMAS, this.isPeriod(date)); + addDisCount(DISCOUNT.WEEK, this.isWeekday(date, menu)); + addDisCount(DISCOUNT.WEEKEND, this.isWeekend(date, menu)); + addDisCount(DISCOUNT.SPECIAL, this.isSpecialDay(date)); + return discount; + } + + isPeriod(date) { + let periodDiscount = 0; + if (date >= DATE.EVENT_START && date <= DATE.EVENT_END) { + periodDiscount += + -DATE.PERIOD_DISCOUNT + (date - 1) * -DATE.PER_DAY_DISCOUNT; + } + return periodDiscount; + } + + isWeekday(date, menu) { + let weekDisCount = 0; + if (!(date % 7 === 1 || date % 7 === 2)) { + let count = 0; + Object.keys(menu).forEach(name => { + if (MENU[name].kind === MENU_KIND.DESSERT) count += menu[name]; + }); + weekDisCount += -INFO.WEEK_DISCOUNT * count; + } + return weekDisCount; + } + + isWeekend(date, menu) { + let weekendDisCount = 0; + if (date % 7 === 1 || date % 7 === 2) { + let count = 0; + Object.keys(menu).forEach(name => { + if (MENU[name].kind === MENU_KIND.MAIN) count += menu[name]; + }); + weekendDisCount += -INFO.WEEK_DISCOUNT * count; + } + return weekendDisCount; + } + + isSpecialDay(date) { + let specialDayDiscount = 0; + if (DATE.SPECIAL_DATE.includes(date)) + specialDayDiscount += -DATE.SPECIAL_DISCOUNT; + return specialDayDiscount; + } +} + +export default Discount;
JavaScript
๋ฏธ์…˜์—์„œ 2023๋…„ 2์›”๋กœ ์ •ํ™•ํ•œ ๊ธฐ๊ฐ„์„ ๋ช…์‹œํ•ด์ฃผ์—ˆ๊ธฐ์— ๊ด€๋ จ๋œ ๊ฑด ์ตœ๋Œ€ํ•œ Date ๊ฐ์ฒด ์ƒ์„ฑ์ด ์•„๋‹Œ ๊ตฌํ˜„์œผ๋กœ ํ•ด๊ฒฐํ•˜๊ณ  ์‹ถ๋‹ค๋Š” ์ ์—์„œ ์‚ฌ์šฉ์„ ํ•˜์ง€ ์•Š์œผ๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค ! ๋˜ ๊ทธ๋Ÿฌ๋‹ค๋ณด๋‹ˆ ์ถฉ๋ถ„ํžˆ ๋‹ค๋ฅธ ํ‘œํ˜„์‹์œผ๋กœ ์ผ์ž๋ฅผ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์—ฌ ๊ทธ๋ ‡๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋˜ ๊ฑฐ ๊ฐ™๋„ค์š” ๐Ÿค”
@@ -0,0 +1,90 @@ +const MENU_KIND = Object.freeze({ + APPETIZER: '์• ํ”ผํƒ€์ด์ €', + MAIN: '๋ฉ”์ธ', + DESSERT: '๋””์ €ํŠธ', + DRINK: '์Œ๋ฃŒ', +}); + +const MENU = Object.freeze({ + ์–‘์†ก์ด์ˆ˜ํ”„: { price: 6000, kind: MENU_KIND.APPETIZER }, + ํƒ€ํŒŒ์Šค: { price: 5500, kind: MENU_KIND.APPETIZER }, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: { price: 8000, kind: MENU_KIND.APPETIZER }, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: { price: 55000, kind: MENU_KIND.MAIN }, + ๋ฐ”๋น„ํ๋ฆฝ: { price: 54000, kind: MENU_KIND.MAIN }, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: { price: 35000, kind: MENU_KIND.MAIN }, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: { price: 25000, kind: MENU_KIND.MAIN }, + ์ดˆ์ฝ”์ผ€์ดํฌ: { price: 15000, kind: MENU_KIND.DESSERT }, + ์•„์ด์Šคํฌ๋ฆผ: { price: 5000, kind: MENU_KIND.DESSERT }, + ์ œ๋กœ์ฝœ๋ผ: { price: 3000, kind: MENU_KIND.DRINK }, + ๋ ˆ๋“œ์™€์ธ: { price: 60000, kind: MENU_KIND.DRINK }, + ์ƒดํŽ˜์ธ: { price: 25000, kind: MENU_KIND.DRINK }, +}); + +const DATE = Object.freeze({ + EVENT_START: 1, + EVENT_END: 25, + PERIOD_DISCOUNT: 1000, + SPECIAL_DISCOUNT: 1000, + SPECIAL_DATE: [3, 10, 17, 24, 25, 31], + PER_DAY_DISCOUNT: 100, +}); + +const INFO = Object.freeze({ + UNIT: '์›', + COUNT: '๊ฐœ', + WEEK_DISCOUNT: 2023, + ORDER_MINIMUM: 10000, + GIFT_CONDITION: 120000, + GIFT_PRICE: 25000, + GIFT: '์ƒดํŽ˜์ธ 1๊ฐœ', + NONE: '์—†์Œ', + BADGE: { + SANTA: { PRICE: 20000, NAME: '์‚ฐํƒ€' }, + TREE: { PRICE: 10000, NAME: 'ํŠธ๋ฆฌ' }, + STAR: { PRICE: 5000, NAME: '๋ณ„' }, + }, +}); + +const DISCOUNT = Object.freeze({ + CHRISTMAS: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', + WEEK: 'ํ‰์ผ ํ• ์ธ', + WEEKEND: '์ฃผ๋ง ํ• ์ธ', + SPECIAL: 'ํŠน๋ณ„ ํ• ์ธ', + GIFTS: '์ฆ์ • ์ด๋ฒคํŠธ', +}); + +const INPUT_MESSAGE = Object.freeze({ + DATE: '12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n', + ORDER: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', +}); + +const OUTPUT_MESSAGE = Object.freeze({ + INTRO: '์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.\n', + TITLE: { + PREVIEW: (date) => `12์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ๋ณด๊ธฐ!\n`, + ORDER_MENU: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + BEFORE_DISCOUNT: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + AFTER_DISCOUNT: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + GIFT: '<์ฆ์ • ๋ฉ”๋‰ด>', + BENEFIT: '<ํ˜œํƒ ๋‚ด์—ญ>', + BENEFIT_AMOUNT: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + BADGE: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + }, +}); + +const ERROR_MESSAGE = Object.freeze({ + INVALID_DATE: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + INVALID_ORDER: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', +}); + +export { + MENU_KIND, + MENU, + DATE, + INFO, + DISCOUNT, + INPUT_MESSAGE, + OUTPUT_MESSAGE, + ERROR_MESSAGE, +};
JavaScript
๋ฏธ์…˜์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๊ฐœ์ธ์ ์œผ๋ก  key๊ฐ’์„ ํ•œ๊ธ€๋กœ ํ•˜๋Š”๊ฒŒ ๋งž๋Š”๊ฐ€? ํ•˜๋Š” ์˜๋ฌธ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค! ์—ฌ๊ฒฝ๋‹˜ ์ƒ๊ฐ์ด ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,28 @@ +import InputView from '../View/InputView'; +import OutputView from '../View/OutputView'; +import Client from './Client'; + +class PromotionController { + async insertInput() { + const client = new Client( + await InputView.readDate(), + await InputView.readOrder(), + ); + const date = client.getDate(); + this.printOutput(client, date); + } + + printOutput(client, date) { + OutputView.printIntro(); + OutputView.printPreview(date); + OutputView.printMenuTitle(client); + OutputView.printBeforeDiscount(client); + OutputView.printGift(client); + OutputView.printBenefit(client); + OutputView.printDiscountAmount(client); + OutputView.printAfterDiscount(client); + OutputView.printEventBadge(client); + } +} + +export default PromotionController;
JavaScript
Controller ๋กœ์ง์ด ๊น”๋”ํ•ด์„œ ๋ณด๊ธฐ ์ข‹๋„ค์š” ๋ฐฐ์›Œ๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,90 @@ +const MENU_KIND = Object.freeze({ + APPETIZER: '์• ํ”ผํƒ€์ด์ €', + MAIN: '๋ฉ”์ธ', + DESSERT: '๋””์ €ํŠธ', + DRINK: '์Œ๋ฃŒ', +}); + +const MENU = Object.freeze({ + ์–‘์†ก์ด์ˆ˜ํ”„: { price: 6000, kind: MENU_KIND.APPETIZER }, + ํƒ€ํŒŒ์Šค: { price: 5500, kind: MENU_KIND.APPETIZER }, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: { price: 8000, kind: MENU_KIND.APPETIZER }, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: { price: 55000, kind: MENU_KIND.MAIN }, + ๋ฐ”๋น„ํ๋ฆฝ: { price: 54000, kind: MENU_KIND.MAIN }, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: { price: 35000, kind: MENU_KIND.MAIN }, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: { price: 25000, kind: MENU_KIND.MAIN }, + ์ดˆ์ฝ”์ผ€์ดํฌ: { price: 15000, kind: MENU_KIND.DESSERT }, + ์•„์ด์Šคํฌ๋ฆผ: { price: 5000, kind: MENU_KIND.DESSERT }, + ์ œ๋กœ์ฝœ๋ผ: { price: 3000, kind: MENU_KIND.DRINK }, + ๋ ˆ๋“œ์™€์ธ: { price: 60000, kind: MENU_KIND.DRINK }, + ์ƒดํŽ˜์ธ: { price: 25000, kind: MENU_KIND.DRINK }, +}); + +const DATE = Object.freeze({ + EVENT_START: 1, + EVENT_END: 25, + PERIOD_DISCOUNT: 1000, + SPECIAL_DISCOUNT: 1000, + SPECIAL_DATE: [3, 10, 17, 24, 25, 31], + PER_DAY_DISCOUNT: 100, +}); + +const INFO = Object.freeze({ + UNIT: '์›', + COUNT: '๊ฐœ', + WEEK_DISCOUNT: 2023, + ORDER_MINIMUM: 10000, + GIFT_CONDITION: 120000, + GIFT_PRICE: 25000, + GIFT: '์ƒดํŽ˜์ธ 1๊ฐœ', + NONE: '์—†์Œ', + BADGE: { + SANTA: { PRICE: 20000, NAME: '์‚ฐํƒ€' }, + TREE: { PRICE: 10000, NAME: 'ํŠธ๋ฆฌ' }, + STAR: { PRICE: 5000, NAME: '๋ณ„' }, + }, +}); + +const DISCOUNT = Object.freeze({ + CHRISTMAS: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', + WEEK: 'ํ‰์ผ ํ• ์ธ', + WEEKEND: '์ฃผ๋ง ํ• ์ธ', + SPECIAL: 'ํŠน๋ณ„ ํ• ์ธ', + GIFTS: '์ฆ์ • ์ด๋ฒคํŠธ', +}); + +const INPUT_MESSAGE = Object.freeze({ + DATE: '12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n', + ORDER: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', +}); + +const OUTPUT_MESSAGE = Object.freeze({ + INTRO: '์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.\n', + TITLE: { + PREVIEW: (date) => `12์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ๋ณด๊ธฐ!\n`, + ORDER_MENU: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + BEFORE_DISCOUNT: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + AFTER_DISCOUNT: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + GIFT: '<์ฆ์ • ๋ฉ”๋‰ด>', + BENEFIT: '<ํ˜œํƒ ๋‚ด์—ญ>', + BENEFIT_AMOUNT: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + BADGE: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + }, +}); + +const ERROR_MESSAGE = Object.freeze({ + INVALID_DATE: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + INVALID_ORDER: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', +}); + +export { + MENU_KIND, + MENU, + DATE, + INFO, + DISCOUNT, + INPUT_MESSAGE, + OUTPUT_MESSAGE, + ERROR_MESSAGE, +};
JavaScript
์ €๋„ ์ด๋ถ€๋ถ„ ๋•Œ๋ฌธ์— ๊ณคํ˜น์„ ์น˜๋ค˜๋Š”๋ฐ ๋‹ค๋“ค ๋น„์Šทํ•œ ์ƒ๊ฐ์ด์…จ๋‚˜๋ด์š” ๐Ÿค” ํŠนํžˆ ์ƒดํŽ˜์ธ ์ฆ์ • ์—ฌ๋ถ€๋ฅผ ํŒ๋ณ„ํ•  ๋•Œ `์ƒดํŽ˜์ธ.price` ์‹์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผํ•˜๋Š” ์  ๋•Œ๋ฌธ์— ์ƒดํŽ˜์ธ์˜ ๊ฐ€๊ฒฉ๊ณผ ๋ช…์นญ์€ ๋”ฐ๋กœ ์ฆ์ •๋ฉ”๋‰ด์— ๋Œ€ํ•œ ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•˜๊ธฐ๋„ ํ–ˆ์Šต๋‹ˆ๋‹ค.. ๋กœ์ง์ƒ์—์„œ๋Š” ํ˜ธ์ถœ๋˜์ง€ ์•Š์ง€๋งŒ, ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์—์„œ๋Š” ๋˜ ํ˜ธ์ถœํ•ด์•ผํ•˜๋Š” ๋ถ€๋ถ„๋“ค์ด ์žˆ์–ด ๊ณ ๋ฏผ์ด ๋งŽ์•˜๋„ค์š” ใ…Žใ…Ž ์‚ฌ์‹ค.. ```js SOUP : { name: ์–‘์†ก์ด ์ˆ˜ํ”„, price : 6000, ... } ``` ์š”๋Ÿฐ ์‹์œผ๋กœ ์„ ์–ธ ํ˜•ํƒœ๋ฅผ ๋ฐ”๊พธ๊ฑฐ๋‚˜ id ๋กœ SOUP๋ฅผ ์„ ์–ธํ•ด์„œ ํ‚ค๊ฐ’์„ ํŒ๋ณ„ํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ํ•œ๊ธ€๋กœ ์ž‘์„ฑํ•˜๋Š” ๋กœ์ง์„ ๋œ์–ด๋‚ผ ์ˆ˜ ์žˆ์—ˆ์„ ๊ฑฐ๋ž€ ์ƒ๊ฐ์„ ํ–ˆ์—ˆ์–ด์š”...ใ…Žใ…Ž ์ž…๋ ฅ์— ๋Œ€ํ•œ ๋ณ€ํ™˜์ด ์‰ฌ์šด ํ˜•ํƒœ๋ฅผ ์ฑ„ํƒํ•˜๊ณ ์ž ํ–ˆ๋˜ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค ๐Ÿคฃ
@@ -0,0 +1,90 @@ +const MENU_KIND = Object.freeze({ + APPETIZER: '์• ํ”ผํƒ€์ด์ €', + MAIN: '๋ฉ”์ธ', + DESSERT: '๋””์ €ํŠธ', + DRINK: '์Œ๋ฃŒ', +}); + +const MENU = Object.freeze({ + ์–‘์†ก์ด์ˆ˜ํ”„: { price: 6000, kind: MENU_KIND.APPETIZER }, + ํƒ€ํŒŒ์Šค: { price: 5500, kind: MENU_KIND.APPETIZER }, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: { price: 8000, kind: MENU_KIND.APPETIZER }, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: { price: 55000, kind: MENU_KIND.MAIN }, + ๋ฐ”๋น„ํ๋ฆฝ: { price: 54000, kind: MENU_KIND.MAIN }, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: { price: 35000, kind: MENU_KIND.MAIN }, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: { price: 25000, kind: MENU_KIND.MAIN }, + ์ดˆ์ฝ”์ผ€์ดํฌ: { price: 15000, kind: MENU_KIND.DESSERT }, + ์•„์ด์Šคํฌ๋ฆผ: { price: 5000, kind: MENU_KIND.DESSERT }, + ์ œ๋กœ์ฝœ๋ผ: { price: 3000, kind: MENU_KIND.DRINK }, + ๋ ˆ๋“œ์™€์ธ: { price: 60000, kind: MENU_KIND.DRINK }, + ์ƒดํŽ˜์ธ: { price: 25000, kind: MENU_KIND.DRINK }, +}); + +const DATE = Object.freeze({ + EVENT_START: 1, + EVENT_END: 25, + PERIOD_DISCOUNT: 1000, + SPECIAL_DISCOUNT: 1000, + SPECIAL_DATE: [3, 10, 17, 24, 25, 31], + PER_DAY_DISCOUNT: 100, +}); + +const INFO = Object.freeze({ + UNIT: '์›', + COUNT: '๊ฐœ', + WEEK_DISCOUNT: 2023, + ORDER_MINIMUM: 10000, + GIFT_CONDITION: 120000, + GIFT_PRICE: 25000, + GIFT: '์ƒดํŽ˜์ธ 1๊ฐœ', + NONE: '์—†์Œ', + BADGE: { + SANTA: { PRICE: 20000, NAME: '์‚ฐํƒ€' }, + TREE: { PRICE: 10000, NAME: 'ํŠธ๋ฆฌ' }, + STAR: { PRICE: 5000, NAME: '๋ณ„' }, + }, +}); + +const DISCOUNT = Object.freeze({ + CHRISTMAS: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', + WEEK: 'ํ‰์ผ ํ• ์ธ', + WEEKEND: '์ฃผ๋ง ํ• ์ธ', + SPECIAL: 'ํŠน๋ณ„ ํ• ์ธ', + GIFTS: '์ฆ์ • ์ด๋ฒคํŠธ', +}); + +const INPUT_MESSAGE = Object.freeze({ + DATE: '12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n', + ORDER: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', +}); + +const OUTPUT_MESSAGE = Object.freeze({ + INTRO: '์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.\n', + TITLE: { + PREVIEW: (date) => `12์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ๋ณด๊ธฐ!\n`, + ORDER_MENU: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + BEFORE_DISCOUNT: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + AFTER_DISCOUNT: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + GIFT: '<์ฆ์ • ๋ฉ”๋‰ด>', + BENEFIT: '<ํ˜œํƒ ๋‚ด์—ญ>', + BENEFIT_AMOUNT: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + BADGE: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + }, +}); + +const ERROR_MESSAGE = Object.freeze({ + INVALID_DATE: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + INVALID_ORDER: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', +}); + +export { + MENU_KIND, + MENU, + DATE, + INFO, + DISCOUNT, + INPUT_MESSAGE, + OUTPUT_MESSAGE, + ERROR_MESSAGE, +};
JavaScript
Object.freeze() ๋Š” ์–•์€ ๋™๊ฒฐ์„ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— deepFreeze์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!! https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze#%EB%B0%B0%EC%97%B4_%EB%8F%99%EA%B2%B0
@@ -0,0 +1,90 @@ +const MENU_KIND = Object.freeze({ + APPETIZER: '์• ํ”ผํƒ€์ด์ €', + MAIN: '๋ฉ”์ธ', + DESSERT: '๋””์ €ํŠธ', + DRINK: '์Œ๋ฃŒ', +}); + +const MENU = Object.freeze({ + ์–‘์†ก์ด์ˆ˜ํ”„: { price: 6000, kind: MENU_KIND.APPETIZER }, + ํƒ€ํŒŒ์Šค: { price: 5500, kind: MENU_KIND.APPETIZER }, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: { price: 8000, kind: MENU_KIND.APPETIZER }, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: { price: 55000, kind: MENU_KIND.MAIN }, + ๋ฐ”๋น„ํ๋ฆฝ: { price: 54000, kind: MENU_KIND.MAIN }, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: { price: 35000, kind: MENU_KIND.MAIN }, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: { price: 25000, kind: MENU_KIND.MAIN }, + ์ดˆ์ฝ”์ผ€์ดํฌ: { price: 15000, kind: MENU_KIND.DESSERT }, + ์•„์ด์Šคํฌ๋ฆผ: { price: 5000, kind: MENU_KIND.DESSERT }, + ์ œ๋กœ์ฝœ๋ผ: { price: 3000, kind: MENU_KIND.DRINK }, + ๋ ˆ๋“œ์™€์ธ: { price: 60000, kind: MENU_KIND.DRINK }, + ์ƒดํŽ˜์ธ: { price: 25000, kind: MENU_KIND.DRINK }, +}); + +const DATE = Object.freeze({ + EVENT_START: 1, + EVENT_END: 25, + PERIOD_DISCOUNT: 1000, + SPECIAL_DISCOUNT: 1000, + SPECIAL_DATE: [3, 10, 17, 24, 25, 31], + PER_DAY_DISCOUNT: 100, +}); + +const INFO = Object.freeze({ + UNIT: '์›', + COUNT: '๊ฐœ', + WEEK_DISCOUNT: 2023, + ORDER_MINIMUM: 10000, + GIFT_CONDITION: 120000, + GIFT_PRICE: 25000, + GIFT: '์ƒดํŽ˜์ธ 1๊ฐœ', + NONE: '์—†์Œ', + BADGE: { + SANTA: { PRICE: 20000, NAME: '์‚ฐํƒ€' }, + TREE: { PRICE: 10000, NAME: 'ํŠธ๋ฆฌ' }, + STAR: { PRICE: 5000, NAME: '๋ณ„' }, + }, +}); + +const DISCOUNT = Object.freeze({ + CHRISTMAS: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', + WEEK: 'ํ‰์ผ ํ• ์ธ', + WEEKEND: '์ฃผ๋ง ํ• ์ธ', + SPECIAL: 'ํŠน๋ณ„ ํ• ์ธ', + GIFTS: '์ฆ์ • ์ด๋ฒคํŠธ', +}); + +const INPUT_MESSAGE = Object.freeze({ + DATE: '12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n', + ORDER: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', +}); + +const OUTPUT_MESSAGE = Object.freeze({ + INTRO: '์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.\n', + TITLE: { + PREVIEW: (date) => `12์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ๋ณด๊ธฐ!\n`, + ORDER_MENU: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + BEFORE_DISCOUNT: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + AFTER_DISCOUNT: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + GIFT: '<์ฆ์ • ๋ฉ”๋‰ด>', + BENEFIT: '<ํ˜œํƒ ๋‚ด์—ญ>', + BENEFIT_AMOUNT: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + BADGE: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + }, +}); + +const ERROR_MESSAGE = Object.freeze({ + INVALID_DATE: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + INVALID_ORDER: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', +}); + +export { + MENU_KIND, + MENU, + DATE, + INFO, + DISCOUNT, + INPUT_MESSAGE, + OUTPUT_MESSAGE, + ERROR_MESSAGE, +};
JavaScript
์ด๋ฒˆ์— ๊ธˆ์•ก์— ๊ด€ํ•œ ์ˆซ์ž๋ฅผ ๋‹ค๋ฃจ๋ฉด์„œ `numeric seperator` ๋ผ๋Š” ๋ฐฉ์‹์„ ์•Œ๊ฒŒ๋˜์—ˆ๋Š”๋ฐ ๊ฐœ์ธ์ ์œผ๋กœ ์ˆซ์ž ๋‹จ์œ„๊ฐ€ ํ•œ๋ˆˆ์— ๋“ค์–ด์™€์„œ ์ข‹๋”๋ผ๊ตฌ์š”! ```suggestion GIFT_CONDITION: 120_000, ```
@@ -0,0 +1,10 @@ +const parseInput = userInput => + userInput + .trim() + .split(',') + .map(item => { + const [name, quantity] = item.trim().split('-'); + return [name, Number(quantity)]; + }); + +export default parseInput;
JavaScript
ํ•จ์ˆ˜ ๋กœ์ง์ด ์—ฌ๋Ÿฌ์ค„์ด๋ผ ์•„๋ž˜์ฒ˜๋Ÿผ ์ˆ˜์ •ํ•˜๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?? ```suggestion const parseInput = (userInput) => { return userInput .trim() .split(",") .map((item) => { const [name, quantity] = item.trim().split("-"); return [name, Number(quantity)]; }); }; ``` https://github.com/airbnb/javascript#whitespace--implicit-arrow-linebreak https://github.com/airbnb/javascript#arrows--paren-wrap
@@ -0,0 +1,60 @@ +import { ERROR_MESSAGE, MENU, MENU_KIND } from './constants'; + +const validateDate = date => { + if (!(date >= 1 && date <= 31)) { + throw new Error(ERROR_MESSAGE.INVALID_DATE); + } + if (typeof date !== 'number' || !Number.isInteger(date)) { + throw new Error(ERROR_MESSAGE.INVALID_DATE); + } +}; + +const validateName = menu => { + const checkedName = new Set(); + menu.forEach(([name]) => { + if (!Object.keys(MENU).includes(name)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } + checkedName.add(name); + }); +}; + +const validateDuplicate = menu => { + const checkedName = new Set(); + menu.forEach(([name]) => { + if (checkedName.has(name)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } + checkedName.add(name); + }); +}; + +const validateOnlyDrink = menu => { + const types = new Set(menu.map(([name]) => MENU[name].type)); + if (types.size === 1 && types.has(MENU_KIND.DRINK)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } +}; + +const validateCount = menu => { + if (!menu.every(([, count]) => count >= 1)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } +}; + +const validateTotalCount = menu => { + const total = menu.reduce((acc, [, count]) => acc + count, 0); + if (total > 20) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } +}; + +const validateOrder = order => { + validateName(order); + validateDuplicate(order); + validateOnlyDrink(order); + validateCount(order); + validateTotalCount(order); +}; + +export { validateDate, validateOrder };
JavaScript
์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์–ธ๋”๋ฐ”๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ด€์Šต์ด ์žˆ๋‹ค๊ณ ํ•ฉ๋‹ˆ๋‹ค! ```suggestion const total = menu.reduce((acc, [_, count]) => acc + count, 0); ```
@@ -0,0 +1,60 @@ +import { ERROR_MESSAGE, MENU, MENU_KIND } from './constants'; + +const validateDate = date => { + if (!(date >= 1 && date <= 31)) { + throw new Error(ERROR_MESSAGE.INVALID_DATE); + } + if (typeof date !== 'number' || !Number.isInteger(date)) { + throw new Error(ERROR_MESSAGE.INVALID_DATE); + } +}; + +const validateName = menu => { + const checkedName = new Set(); + menu.forEach(([name]) => { + if (!Object.keys(MENU).includes(name)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } + checkedName.add(name); + }); +}; + +const validateDuplicate = menu => { + const checkedName = new Set(); + menu.forEach(([name]) => { + if (checkedName.has(name)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } + checkedName.add(name); + }); +}; + +const validateOnlyDrink = menu => { + const types = new Set(menu.map(([name]) => MENU[name].type)); + if (types.size === 1 && types.has(MENU_KIND.DRINK)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } +}; + +const validateCount = menu => { + if (!menu.every(([, count]) => count >= 1)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } +}; + +const validateTotalCount = menu => { + const total = menu.reduce((acc, [, count]) => acc + count, 0); + if (total > 20) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } +}; + +const validateOrder = order => { + validateName(order); + validateDuplicate(order); + validateOnlyDrink(order); + validateCount(order); + validateTotalCount(order); +}; + +export { validateDate, validateOrder };
JavaScript
์˜ค ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋„ค์š”.
@@ -0,0 +1,208 @@ +## ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๋Šฅ ๋ชฉ๋ก ์ •๋ฆฌ ๐ŸŽ„ + +### ํด๋ž˜์Šค, ํ•จ์ˆ˜ ์ •๋ฆฌ + +1. Domain +- 1-1. `PromotionController` + - ์ „์ฒด ํ”„๋กœ๋ชจ์…˜์˜ ํ”„๋กœ์„ธ์Šค ๋ฐ ์ž…์ถœ๋ ฅ์„ ๋‹ด๋‹นํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค +- 1-2. `Discount` + - ๋‚ ์งœ์— ๋Œ€ํ•œ ํ˜œํƒ๋“ค์„ ํ™•์ธํ•˜๊ณ , ํ˜œํƒ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค +- 1-3. `Client` + - ์ฃผ๋ฌธ๊ณผ ๋‚ ์งœ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ณ ๊ฐ์˜ ํ• ์ธ ํ˜œํƒ๊ณผ ์ฃผ๋ฌธ ๊ฐ€๊ฒฉ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค + +2. View +- 2-1. `InputView` + - ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›์•„ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ณ , ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ +- 2-2. `OutputView` + - ํ”„๋กœ์„ธ์Šค ๊ฒฐ๊ณผ ๋ฐ ์˜ค๋ฅ˜ ๋ฉ”์„ธ์ง€๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜ + + +3. Util +- 3-1. `constants` + - ์‚ฌ์šฉ๋˜๋Š” ๋งค์ง๋„˜๋ฒ„, ์—๋Ÿฌ๋ฉ”์„ธ์ง€, ์ž…์ถœ๋ ฅ ๋ฉ”์‹œ์ง€, ์ถœ๋ ฅ ํƒ€์ดํ‹€ ๋“ฑ ์ƒ์ˆ˜ ์ •์˜ +- 3-2. `inputHandler` + - InputView ๋‚ด์—์„œ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ๋กœ์ง ํ•ธ๋“ค๋Ÿฌ ์ •์˜ +- 3-3. `parseInput` + - ๊ณ ๊ฐ์˜ ๋ฉ”๋‰ด ์ž…๋ ฅ์„ ๋ฉ”๋‰ด ์ฃผ๋ฌธ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ ์ •์˜ +- 3-4. `validateInput` + - ๊ณ ๊ฐ์˜ ์ž…๋ ฅ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ํ•จ์ˆ˜ ์ •์˜ +4. `App` + - ํ”„๋กœ๋ชจ์…˜ ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ์ „์ฒด +5. `index` + - ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” App์„ ์‹คํ–‰ํ•˜๋Š” ์ง„์ž…์  + + +### ๊ตฌํ˜„ ๊ธฐ๋Šฅ ์ •๋ฆฌ + +**1. ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ ์ž…๋ ฅ** + +- [x] ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ ๋ฉ˜ํŠธ ์ถœ๋ ฅ +- [x] ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ๋‚ ์งœ(์ผ) ์ž…๋ ฅ ๋ฐ›๊ธฐ +- [x] ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ๋‚ ์งœ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์ฆ + +**2. ์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜ ์ž…๋ ฅ** + +- [x] ์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด์™€ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š” ๋ฌธ๊ตฌ ์ถœ๋ ฅ +- [x] `์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด-๋ฉ”๋‰ด ๊ฐœ์ˆ˜` ํ˜•ํƒœ ์ž…๋ ฅ ๋ฐ›๊ธฐ +- [x] ์ž…๋ ฅ ๋ฐ›์€ ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์ฆ + +**3. ๋ฐฉ๋ฌธ ๋‚ ์งœ์— ๋Œ€ํ•œ ํ˜œํƒ ์‹œ์ž‘ ๋ฉ˜ํŠธ ์ถœ๋ ฅ** + +- [x] 12์›” (1.์—์„œ ์ž…๋ ฅ๋ฐ›์€ ๋‚ ์งœ)์ผ ์— ๋Œ€ํ•œ ํ˜œํƒ ์†Œ๊ฐœ ๋ฌธ๊ตฌ ์ถœ๋ ฅ + +**4. ์ฃผ๋ฌธ ๋ฉ”๋‰ด ์ถœ๋ ฅ** + +- [x] `<์ฃผ๋ฌธ๋ฉ”๋‰ด>` ์ถœ๋ ฅ +- [x] `์ฃผ๋ฌธ ๋ฉ”๋‰ด-๋ฉ”๋‰ด ๊ฐœ์ˆ˜` ํ˜•ํƒœ๋กœ ์ž…๋ ฅ ๋ฐ›์€ ๋‚ด์šฉ ์ถœ๋ ฅ + +**5. ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก ์ถœ๋ ฅ** + +- [x] `<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>`์ถœ๋ ฅ +- [x] ์ฃผ๋ฌธ ๊ธˆ์•ก ์ถœ๋ ฅ + +**6. ์ฆ์ •๋ฉ”๋‰ด ์ฆ์ • ์—ฌ๋ถ€ ์ถœ๋ ฅ** + +- [x] `<์ฆ์ • ๋ฉ”๋‰ด>` ์ถœ๋ ฅ +- [x] `์ƒดํŽ˜์ธ 1๊ฐœ` ๋˜๋Š” `์—†์Œ` ์ถœ๋ ฅ + +**7. ํ˜œํƒ ๋‚ด์—ญ ์ถœ๋ ฅ** + +- [x] `<ํ˜œํƒ ๋‚ด์—ญ>` ์ถœ๋ ฅ +- [x] ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ธ ํ• ์ธ ๊ธˆ์•ก ์ถœ๋ ฅ +- [x] ํ‰์ผ ํ• ์ธ ๊ธˆ์•ก ์ถœ๋ ฅ +- [x] ํŠน๋ณ„ ํ• ์ธ ๊ธˆ์•ก ์ถœ๋ ฅ +- [x] ์ฆ์ • ์ด๋ฒคํŠธ ํ• ์ธ ์ด๋ฒคํŠธ ์ถœ๋ ฅ + +**8. ์ดํ˜œํƒ ๊ธˆ์•ก ์ถœ๋ ฅ** + +- [x] `<์ดํ˜œํƒ ๊ธˆ์•ก>` ์ถœ๋ ฅ +- [x] ์ด ํ˜œํƒ ๊ธˆ์•ก ์ถœ๋ ฅ + +**9. ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก ์ถœ๋ ฅ** + +- [x] `<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>`์ถœ๋ ฅ +- [x] ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก ์ถœ๋ ฅ + +**10. 12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ์ถœ๋ ฅ** + +- [x] `<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>` ์ถœ๋ ฅ +- [x] ์ด๋ฒคํŠธ ๋ฐฐ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ฐฐ์ง€ ์ถœ๋ ฅ + + +### ์ด๋ฒคํŠธ ์ •๋ฆฌ + +1-1. 2023.12.1 ~ 2023.12.25 ํ• ์ธ + +- ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ + - 12.1 ๊ธฐ์ค€ 1,000์› ํ• ์ธ ~ + - 12.25 ๊ธฐ์ค€ 3400์› ํž์ธ + +1-2. 2023.12.1 ~ 2023.12.31 ํ• ์ธ + +- ํ‰์ผ ํ• ์ธ (์ผ, ์›”, ํ™”, ์ˆ˜, ๋ชฉ) + - ๋””์ €ํŠธ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2023์› ํ• ์ธ +- ์ฃผ๋ง ํ• ์ธ (๊ธˆ, ํ† ) + - ๋ฉ”์ธ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2023์› ํ• ์ธ +- ํŠน๋ณ„ ํ• ์ธ (3(์ผ), 10(์ผ), 17(์ผ), 24(์ผ), 25(์›”), 31(์ผ)) + - ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์—์„œ 1000์› ํ• ์ธ +- ์ฆ์ • ์ด๋ฒคํŠธ + - **ํ• ์ธ ์ „ ์ด ์ฃผ๋ฌธ๊ธˆ์•ก**์ด 12๋งŒ์› ์ด์ƒ์ผ ๋•Œ, ์ƒดํŽ˜์ธ 1๊ฐœ ์ฆ์ • + +1-3. ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ๋ถ€์—ฌ + +- ์ด ํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ฅธ ์ด๋ฒคํŠธ ๋ฐฐ์ง€ + - ๋ฏธ์ถฉ์กฑ : ์—†์Œ + - 5์ฒœ์› ์ด์ƒ : ๋ณ„ + - 1๋งŒ์› ์ด์ƒ : ํŠธ๋ฆฌ + - 2๋งŒ์› ์ด์ƒ : ์‚ฐํƒ€ + +1-4. ์ฃผ์˜ ์‚ฌํ•ญ + +- 'ํ• ์ธ'์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€๋งŒ, ๊ธˆ์•ก '์ฐจ๊ฐ'์— ๋” ๊ฐ€๊นŒ์›€. %๋กœ ํ• ์ธ์ด ์•„๋‹Œ - ํ˜•์‹ +- ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก 10,000์› ์ด์ƒ์ธ์ง€ ํ™•์ธํ•˜๊ณ  ์ด๋ฒคํŠธ ์ ์šฉ ํ•„์š” +- ์Œ๋ฃŒ๋งŒ, ์ฃผ๋ฌธ ์‹œ ์ฃผ๋ฌธ ์ž์ฒด๊ฐ€ ๋ถˆ๊ฐ€ +- ๋ฉ”๋‰ด๋Š” ํ•œ๋ฒˆ์— ์ตœ๋Œ€ 20๊ฐœ ๊นŒ์ง€ ์ฃผ๋ฌธ ๊ฐ€๋Šฅ + + +### ํ๋ฆ„ ์ •๋ฆฌ + + +**1. ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ ์ž…๋ ฅ** + +- 1-1. ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ ์†Œ๊ฐœ ๋ฉ˜ํŠธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +- 1-2. ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ(์ผ)๋ฅผ ์ˆซ์ž๋กœ๋งŒ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. + -1-2-1. ์ž…๋ ฅ ๋ฐ›์€ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. + +**2. ์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜ ์ž…๋ ฅ** + +- 2-1. ์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด์™€ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š” ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +- 2-2. `์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด-๋ฉ”๋‰ด ๊ฐœ์ˆ˜` ํ˜•ํƒœ๋กœ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. + - 2-2-1. ์ž…๋ ฅ ๋ฐ›์€ ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. + +**3. ๋ฐฉ๋ฌธ ๋‚ ์งœ์— ๋Œ€ํ•œ ํ˜œํƒ ์‹œ์ž‘ ๋ฉ˜ํŠธ ์ถœ๋ ฅ** + +- 3-1. 12์›” (1.์—์„œ ์ž…๋ ฅ๋ฐ›์€ ๋‚ ์งœ)์ผ ์— ๋Œ€ํ•œ ํ˜œํƒ ์†Œ๊ฐœ ๋ฉ˜ํŠธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. + +**4. ์ฃผ๋ฌธ ๋ฉ”๋‰ด ์ถœ๋ ฅ** + +- 4-1. `<์ฃผ๋ฌธ๋ฉ”๋‰ด>`๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +- 4-2. `์ฃผ๋ฌธ ๋ฉ”๋‰ด-๋ฉ”๋‰ด ๊ฐœ์ˆ˜` ํ˜•ํƒœ๋กœ ์ž…๋ ฅ ๋ฐ›์€ ๋‚ด์šฉ์„ `์ฃผ๋ฌธ ๋ฉ”๋‰ด (๋ฉ”๋‰ด๊ฐœ์ˆ˜)๊ฐœ\n` ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. + +**5. ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก ์ถœ๋ ฅ** + +- 5-1. `<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>`์„ ์ถœ๋ ฅํ•œ๋‹ค. +- 5-2. ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. + - 5-2-1. 1,000 ๋‹จ์œ„๋‹น `,` ์„ ๋ถ™์—ฌ ์ถœ๋ ฅ ํ•œ๋‹ค. + +**6. ์ฆ์ •๋ฉ”๋‰ด ์ฆ์ • ์—ฌ๋ถ€ ์ถœ๋ ฅ** + +- 6-1. `์ƒดํŽ˜์ธ (๊ฐœ์ˆ˜)๊ฐœ` ์ถœ๋ ฅํ•œ๋‹ค. + - 6-2-1. ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์ด 12๋งŒ์› ์ด์ƒ ๊ฒฝ์šฐ ๋‹น ์ƒดํŽ˜์ธ์ด 1๊ฐœ์”ฉ ์ฆ์ •๋œ๋‹ค. + +**7. ํ˜œํƒ ๋‚ด์—ญ ์ถœ๋ ฅ** + +- 7-1. `<ํ˜œํƒ ๋‚ด์—ญ>` ์„ ์ถœ๋ ฅํ•˜๋ฉฐ, ์•„๋ž˜ ๊ธˆ์•ก์€ `-(๊ธˆ์•ก)์›` ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. + - 7-1-1. 1,000 ๋‹จ์œ„๋‹น `,` ์„ ๋ถ™์—ฌ ์ถœ๋ ฅ ํ•œ๋‹ค. +- 7-2. ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ธ ํ• ์ธ ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. +- 7-3. ํ‰์ผ ํ• ์ธ ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. +- 7-4. ํŠน๋ณ„ ํ• ์ธ ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. +- 7-5. ์ฆ์ • ์ด๋ฒคํŠธ ํ• ์ธ ์ด๋ฒคํŠธ ์ถœ๋ ฅํ•œ๋‹ค. + +**8. ์ดํ˜œํƒ ๊ธˆ์•ก ์ถœ๋ ฅ** + +- 8-1. `<์ดํ˜œํƒ ๊ธˆ์•ก>` ์„ ์ถœ๋ ฅํ•˜๋ฉฐ, ์•„๋ž˜ ๊ธˆ์•ก์€ `-(๊ธˆ์•ก)์›` ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. + - 8-1-1. 1,000 ๋‹จ์œ„๋‹น `,` ์„ ๋ถ™์—ฌ ์ถœ๋ ฅ ํ•œ๋‹ค. +- 8-2. *7. ํ˜œํƒ๋‚ด์—ญ ์ถœ๋ ฅ* ์˜ `7-2 ~ 7-5` ์— ๊ธˆ์•ก์„ ๋ชจ๋‘ ๋”ํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค. + + +**9. ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก ์ถœ๋ ฅ** + +- 9-1. `<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>` ์„ ์ถœ๋ ฅํ•˜๋ฉฐ, ์•„๋ž˜ ๊ธˆ์•ก์€ `(๊ธˆ์•ก)์›` ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. + - 9-1-1. 1,000 ๋‹จ์œ„๋‹น `,` ์„ ๋ถ™์—ฌ ์ถœ๋ ฅํ•œ๋‹ค. +- 9-2. **5. ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก ์ถœ๋ ฅ** ์—์„œ **8. ์ด ํ˜œํƒ ๊ธˆ์•ก ์ถœ๋ ฅ** ์„ ๋บ€ ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. + +**10. 12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ์ถœ๋ ฅ** + +- 10-1. `<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>` ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +- 10-2. ์ถœ๋ ฅํ•  ๋ฐฐ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ฐฐ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋ฉฐ, ์—†๋Š” ๊ฒฝ์šฐ `์—†์Œ`์„ ์ถœ๋ ฅํ•œ๋‹ค. + + +### ๐Ÿ—‚ ํŒŒ์ผ ๊ตฌ์กฐ + +``` +โ”— src + โ”ฃ Domain + โ”ƒ โ”ฃ PromotionController.js + โ”ƒ โ”ฃ Client.js + โ”ƒ โ”— Discount.js + โ”ฃ View + โ”ƒ โ”ฃ InputView.js + โ”ƒ โ”— OutputView.js + โ”ฃ Util + โ”ƒ โ”ฃ constants.js + โ”ƒ โ”ฃ parseInput.js + โ”ƒ โ”ฃ inputHandler.js + โ”ƒ โ”— validateInput.js + โ”ฃ App.js + โ”— index.js +``` \ No newline at end of file
Unknown
UserFlow๊ฐ€ ๋ช…ํ™•ํ•˜๊ฒŒ ์ž‘์„ฑ๋œ ๊ฒƒ ๊ฐ™์•„์š” :) userflow๋ฅผ ์ž‘์„ฑํ•˜๋ฉด์„œ ์„ค๊ณ„๋ฅผ ํ•˜๋ฉด, ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋‚˜ flow๋ฅผ ์„ค๊ณ„ํ•จ์— ์žˆ์–ด์„œ ๋งŽ์€ ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™๋„ค์š”!
@@ -0,0 +1,10 @@ +const parseInput = userInput => + userInput + .trim() + .split(',') + .map(item => { + const [name, quantity] = item.trim().split('-'); + return [name, Number(quantity)]; + }); + +export default parseInput;
JavaScript
์ด ํŒจํ„ด ์ž์ฒด๋Š” ์ƒ๋‹นํžˆ ์ข‹๋„ค์š”. ๊ทธ๋Ÿฐ๋ฐ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์ปค๋ฒ„๊ฐ€ ์•ˆ ๋  ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ```javascript const item = 'ํƒ€ํŒŒ์Šค-1-' const [name, menu] = item.trim().split('-') console.log(name, menu) // ํƒ€ํŒŒ์Šค, 1 ```
@@ -0,0 +1,58 @@ +import { DATE, DISCOUNT, INFO, MENU, MENU_KIND } from '../Util/constants'; + +class Discount { + calculateDiscount(date, menu) { + const discount = {}; + const addDisCount = (key, value) => { + if (value !== 0) discount[key] = value; + }; + + addDisCount(DISCOUNT.CHRISTMAS, this.isPeriod(date)); + addDisCount(DISCOUNT.WEEK, this.isWeekday(date, menu)); + addDisCount(DISCOUNT.WEEKEND, this.isWeekend(date, menu)); + addDisCount(DISCOUNT.SPECIAL, this.isSpecialDay(date)); + return discount; + } + + isPeriod(date) { + let periodDiscount = 0; + if (date >= DATE.EVENT_START && date <= DATE.EVENT_END) { + periodDiscount += + -DATE.PERIOD_DISCOUNT + (date - 1) * -DATE.PER_DAY_DISCOUNT; + } + return periodDiscount; + } + + isWeekday(date, menu) { + let weekDisCount = 0; + if (!(date % 7 === 1 || date % 7 === 2)) { + let count = 0; + Object.keys(menu).forEach(name => { + if (MENU[name].kind === MENU_KIND.DESSERT) count += menu[name]; + }); + weekDisCount += -INFO.WEEK_DISCOUNT * count; + } + return weekDisCount; + } + + isWeekend(date, menu) { + let weekendDisCount = 0; + if (date % 7 === 1 || date % 7 === 2) { + let count = 0; + Object.keys(menu).forEach(name => { + if (MENU[name].kind === MENU_KIND.MAIN) count += menu[name]; + }); + weekendDisCount += -INFO.WEEK_DISCOUNT * count; + } + return weekendDisCount; + } + + isSpecialDay(date) { + let specialDayDiscount = 0; + if (DATE.SPECIAL_DATE.includes(date)) + specialDayDiscount += -DATE.SPECIAL_DISCOUNT; + return specialDayDiscount; + } +} + +export default Discount;
JavaScript
๋งค์šฐ ์‚ฌ์†Œํ•œ ๋ถ€๋ถ„์ด์ง€๋งŒ, 7๊ณผ 1,2๋„ ์ƒ์ˆ˜ํ™”๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด ์–ด๋–จ๊นŒ ์‹ถ๋„ค์š”! 1๊ณผ 2๊ฐ€ ๋ฌด์—‡์„ ์˜๋ฏธํ•˜๋Š”์ง€ ํ—ท๊ฐˆ๋ฆด ์ˆ˜๋„ ์žˆ๊ณ , ๋™์ผํ•˜๊ฒŒ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก์š” :)
@@ -0,0 +1,96 @@ +import { DISCOUNT, INFO, MENU } from '../Util/constants'; +import Discount from './Discount'; + +class Client { + #date; + #order; + + constructor(date, order) { + this.#date = date; + this.#order = order; + } + + calculateBenefits() { + const discount = new Discount(); + return discount.calculateDiscount(this.#date, this.#order); + } + + getDate() { + return this.#date; + } + + getOrderList() { + let output = ''; + Object.entries(this.#order).forEach(([name, count]) => { + output += `${name} ${count}${INFO.COUNT}\n`; + }); + return output; + } + + getBeforeDiscount() { + let cost = 0; + Object.entries(this.#order).forEach(([name, count]) => { + cost += MENU[name].price * count; + }); + return cost; + } + + getGift() { + const isGift = this.getBeforeDiscount() >= INFO.GIFT_CONDITION; + return (isGift && INFO.GIFT) || INFO.NONE; + } + + getBenefitList() { + const benefit = this.calculateBenefits(); + if (!benefit || Object.keys(benefit).length === 0) { + return INFO.NONE; + } + let output = ''; + Object.entries(benefit).forEach(([discount, cost]) => { + output += `${discount}: ${cost.toLocaleString()}${INFO.UNIT}\n`; + }); + if (this.getGift() !== INFO.NONE) + output += `${DISCOUNT.GIFTS}: -${INFO.GIFT_PRICE.toLocaleString()}${ + INFO.UNIT + }\n`; + return output; + } + + getDiscountAmount() { + const benefit = this.calculateBenefits(); + if (!benefit || Object.keys(benefit).length === 0) return 0; + + let totalAmount = Object.values(benefit).reduce((acc, cur) => acc + cur); + if (this.getGift() !== INFO.NONE) totalAmount += -INFO.GIFT_PRICE; + return totalAmount; + } + + getAfterDiscount() { + const beforeDiscount = this.getBeforeDiscount(); + const discountAmount = this.getDiscountAmount(); + let gift = 0; + + if (this.getGift() !== INFO.NONE) { + gift = INFO.GIFT_PRICE; + } + + const total = beforeDiscount + discountAmount + gift; + return total; + } + + getEventBadge() { + const benefit = this.getDiscountAmount(); + + switch (true) { + case benefit <= -INFO.BADGE.SANTA.PRICE: + return INFO.BADGE.SANTA.NAME; + case benefit <= -INFO.BADGE.TREE.PRICE: + return INFO.BADGE.TREE.NAME; + case benefit <= -INFO.BADGE.STAR.PRICE: + return INFO.BADGE.STAR.NAME; + default: + return INFO.NONE; + } + } +} +export default Client;
JavaScript
```javascript getOrderList() { Object.entries(this.#order).map(([name, count]) =>`${name} ${count}${INFO.COUNT}`); return output.join('\n)'; } ``` ```javascript Object.entries(this.#order) .map(([name, count]) => MENU[name].price * count) .reduce((acc, cur) => acc+cur, 0) ``` ์‚ฌ์†Œํ•œ ์Šคํƒ€์ผ ์ฐจ์ด์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๊ณ ์น  ์ˆ˜๋„ ์žˆ๊ฒ ๋„ค์š”.
@@ -0,0 +1,96 @@ +import { DISCOUNT, INFO, MENU } from '../Util/constants'; +import Discount from './Discount'; + +class Client { + #date; + #order; + + constructor(date, order) { + this.#date = date; + this.#order = order; + } + + calculateBenefits() { + const discount = new Discount(); + return discount.calculateDiscount(this.#date, this.#order); + } + + getDate() { + return this.#date; + } + + getOrderList() { + let output = ''; + Object.entries(this.#order).forEach(([name, count]) => { + output += `${name} ${count}${INFO.COUNT}\n`; + }); + return output; + } + + getBeforeDiscount() { + let cost = 0; + Object.entries(this.#order).forEach(([name, count]) => { + cost += MENU[name].price * count; + }); + return cost; + } + + getGift() { + const isGift = this.getBeforeDiscount() >= INFO.GIFT_CONDITION; + return (isGift && INFO.GIFT) || INFO.NONE; + } + + getBenefitList() { + const benefit = this.calculateBenefits(); + if (!benefit || Object.keys(benefit).length === 0) { + return INFO.NONE; + } + let output = ''; + Object.entries(benefit).forEach(([discount, cost]) => { + output += `${discount}: ${cost.toLocaleString()}${INFO.UNIT}\n`; + }); + if (this.getGift() !== INFO.NONE) + output += `${DISCOUNT.GIFTS}: -${INFO.GIFT_PRICE.toLocaleString()}${ + INFO.UNIT + }\n`; + return output; + } + + getDiscountAmount() { + const benefit = this.calculateBenefits(); + if (!benefit || Object.keys(benefit).length === 0) return 0; + + let totalAmount = Object.values(benefit).reduce((acc, cur) => acc + cur); + if (this.getGift() !== INFO.NONE) totalAmount += -INFO.GIFT_PRICE; + return totalAmount; + } + + getAfterDiscount() { + const beforeDiscount = this.getBeforeDiscount(); + const discountAmount = this.getDiscountAmount(); + let gift = 0; + + if (this.getGift() !== INFO.NONE) { + gift = INFO.GIFT_PRICE; + } + + const total = beforeDiscount + discountAmount + gift; + return total; + } + + getEventBadge() { + const benefit = this.getDiscountAmount(); + + switch (true) { + case benefit <= -INFO.BADGE.SANTA.PRICE: + return INFO.BADGE.SANTA.NAME; + case benefit <= -INFO.BADGE.TREE.PRICE: + return INFO.BADGE.TREE.NAME; + case benefit <= -INFO.BADGE.STAR.PRICE: + return INFO.BADGE.STAR.NAME; + default: + return INFO.NONE; + } + } +} +export default Client;
JavaScript
๊ฐœ์ธ์ ์ธ ์˜๊ฒฌ์ด์ง€๋งŒ ์ด ๊ฒฝ์šฐ๋Š” `model`์ด `view`์˜ ์ง€์‹์„ ๋„ˆ๋ฌด ๋งŽ์ด ์•Œ๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์–ด๋–ค `view`์—์„œ๋Š” "{ํ• ์ธ}: {๊ธˆ์•ก}์›"ํ˜•์‹์œผ๋กœ ๋ฐ›๊ธธ ์›ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๋‹ค๋ฅธ `view`์—์„œ๋Š” "{๊ธˆ์•ก}๋งŒํผ {ํ• ์ธ}์„ ๋ฐ›์œผ์…จ์–ด์š”"์™€ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๊ธฐ๋ฅผ ์›ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค๊ณ , ๋”ฐ๋ผ์„œ `domain` ์˜์—ญ์ด `view` ์˜์—ญ์—์„œ ์ฒ˜๋ฆฌ๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒŒ ์•„๋‹๊นŒ ์ƒ๊ฐ์ด ๋˜๋„ค์š”.
@@ -0,0 +1,58 @@ +import { DATE, DISCOUNT, INFO, MENU, MENU_KIND } from '../Util/constants'; + +class Discount { + calculateDiscount(date, menu) { + const discount = {}; + const addDisCount = (key, value) => { + if (value !== 0) discount[key] = value; + }; + + addDisCount(DISCOUNT.CHRISTMAS, this.isPeriod(date)); + addDisCount(DISCOUNT.WEEK, this.isWeekday(date, menu)); + addDisCount(DISCOUNT.WEEKEND, this.isWeekend(date, menu)); + addDisCount(DISCOUNT.SPECIAL, this.isSpecialDay(date)); + return discount; + } + + isPeriod(date) { + let periodDiscount = 0; + if (date >= DATE.EVENT_START && date <= DATE.EVENT_END) { + periodDiscount += + -DATE.PERIOD_DISCOUNT + (date - 1) * -DATE.PER_DAY_DISCOUNT; + } + return periodDiscount; + } + + isWeekday(date, menu) { + let weekDisCount = 0; + if (!(date % 7 === 1 || date % 7 === 2)) { + let count = 0; + Object.keys(menu).forEach(name => { + if (MENU[name].kind === MENU_KIND.DESSERT) count += menu[name]; + }); + weekDisCount += -INFO.WEEK_DISCOUNT * count; + } + return weekDisCount; + } + + isWeekend(date, menu) { + let weekendDisCount = 0; + if (date % 7 === 1 || date % 7 === 2) { + let count = 0; + Object.keys(menu).forEach(name => { + if (MENU[name].kind === MENU_KIND.MAIN) count += menu[name]; + }); + weekendDisCount += -INFO.WEEK_DISCOUNT * count; + } + return weekendDisCount; + } + + isSpecialDay(date) { + let specialDayDiscount = 0; + if (DATE.SPECIAL_DATE.includes(date)) + specialDayDiscount += -DATE.SPECIAL_DISCOUNT; + return specialDayDiscount; + } +} + +export default Discount;
JavaScript
๊ฐ case๋งˆ๋‹ค ๋”ฐ๋กœ ๊ตฌ๋ถ„์„ ์ง€์–ด ์ฃผ์‹  ๊ฑฐ๋ผ๋ฉด, ```javascript let specialDayDiscount = 0; specialDayDiscount += -DATE.SPECIAL_DISCOUNT; ``` ์™€ ๊ฐ™์€ ๋ฐฉ์‹๋„ ์ข‹์ง€๋งŒ, ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•œ๋ฒˆ์— ์„ ์–ธํ•ด ๋ฒ„๋ ค๋„ ๊ดœ์ฐฎ์ง€ ์•Š์„๊นŒ ์‹ถ์–ด์š”! ```javascript const specialDayDiscount = -DATE.SPECIAL_DISCOUNT; ```
@@ -0,0 +1,90 @@ +const MENU_KIND = Object.freeze({ + APPETIZER: '์• ํ”ผํƒ€์ด์ €', + MAIN: '๋ฉ”์ธ', + DESSERT: '๋””์ €ํŠธ', + DRINK: '์Œ๋ฃŒ', +}); + +const MENU = Object.freeze({ + ์–‘์†ก์ด์ˆ˜ํ”„: { price: 6000, kind: MENU_KIND.APPETIZER }, + ํƒ€ํŒŒ์Šค: { price: 5500, kind: MENU_KIND.APPETIZER }, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: { price: 8000, kind: MENU_KIND.APPETIZER }, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: { price: 55000, kind: MENU_KIND.MAIN }, + ๋ฐ”๋น„ํ๋ฆฝ: { price: 54000, kind: MENU_KIND.MAIN }, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: { price: 35000, kind: MENU_KIND.MAIN }, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: { price: 25000, kind: MENU_KIND.MAIN }, + ์ดˆ์ฝ”์ผ€์ดํฌ: { price: 15000, kind: MENU_KIND.DESSERT }, + ์•„์ด์Šคํฌ๋ฆผ: { price: 5000, kind: MENU_KIND.DESSERT }, + ์ œ๋กœ์ฝœ๋ผ: { price: 3000, kind: MENU_KIND.DRINK }, + ๋ ˆ๋“œ์™€์ธ: { price: 60000, kind: MENU_KIND.DRINK }, + ์ƒดํŽ˜์ธ: { price: 25000, kind: MENU_KIND.DRINK }, +}); + +const DATE = Object.freeze({ + EVENT_START: 1, + EVENT_END: 25, + PERIOD_DISCOUNT: 1000, + SPECIAL_DISCOUNT: 1000, + SPECIAL_DATE: [3, 10, 17, 24, 25, 31], + PER_DAY_DISCOUNT: 100, +}); + +const INFO = Object.freeze({ + UNIT: '์›', + COUNT: '๊ฐœ', + WEEK_DISCOUNT: 2023, + ORDER_MINIMUM: 10000, + GIFT_CONDITION: 120000, + GIFT_PRICE: 25000, + GIFT: '์ƒดํŽ˜์ธ 1๊ฐœ', + NONE: '์—†์Œ', + BADGE: { + SANTA: { PRICE: 20000, NAME: '์‚ฐํƒ€' }, + TREE: { PRICE: 10000, NAME: 'ํŠธ๋ฆฌ' }, + STAR: { PRICE: 5000, NAME: '๋ณ„' }, + }, +}); + +const DISCOUNT = Object.freeze({ + CHRISTMAS: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', + WEEK: 'ํ‰์ผ ํ• ์ธ', + WEEKEND: '์ฃผ๋ง ํ• ์ธ', + SPECIAL: 'ํŠน๋ณ„ ํ• ์ธ', + GIFTS: '์ฆ์ • ์ด๋ฒคํŠธ', +}); + +const INPUT_MESSAGE = Object.freeze({ + DATE: '12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n', + ORDER: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', +}); + +const OUTPUT_MESSAGE = Object.freeze({ + INTRO: '์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.\n', + TITLE: { + PREVIEW: (date) => `12์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ๋ณด๊ธฐ!\n`, + ORDER_MENU: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + BEFORE_DISCOUNT: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + AFTER_DISCOUNT: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + GIFT: '<์ฆ์ • ๋ฉ”๋‰ด>', + BENEFIT: '<ํ˜œํƒ ๋‚ด์—ญ>', + BENEFIT_AMOUNT: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + BADGE: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + }, +}); + +const ERROR_MESSAGE = Object.freeze({ + INVALID_DATE: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + INVALID_ORDER: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', +}); + +export { + MENU_KIND, + MENU, + DATE, + INFO, + DISCOUNT, + INPUT_MESSAGE, + OUTPUT_MESSAGE, + ERROR_MESSAGE, +};
JavaScript
์ €๋Š” ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์„ ๋ชฐ๋ผ์„œ ๋‘๊ฐœ๋กœ ๋‚˜๋ˆ ์„œ ์ผ์—ˆ๋Š”๋ฐ, ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๊ตฐ์š”! ์ข‹์€ ์ •๋ณด ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,90 @@ +const MENU_KIND = Object.freeze({ + APPETIZER: '์• ํ”ผํƒ€์ด์ €', + MAIN: '๋ฉ”์ธ', + DESSERT: '๋””์ €ํŠธ', + DRINK: '์Œ๋ฃŒ', +}); + +const MENU = Object.freeze({ + ์–‘์†ก์ด์ˆ˜ํ”„: { price: 6000, kind: MENU_KIND.APPETIZER }, + ํƒ€ํŒŒ์Šค: { price: 5500, kind: MENU_KIND.APPETIZER }, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: { price: 8000, kind: MENU_KIND.APPETIZER }, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: { price: 55000, kind: MENU_KIND.MAIN }, + ๋ฐ”๋น„ํ๋ฆฝ: { price: 54000, kind: MENU_KIND.MAIN }, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: { price: 35000, kind: MENU_KIND.MAIN }, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: { price: 25000, kind: MENU_KIND.MAIN }, + ์ดˆ์ฝ”์ผ€์ดํฌ: { price: 15000, kind: MENU_KIND.DESSERT }, + ์•„์ด์Šคํฌ๋ฆผ: { price: 5000, kind: MENU_KIND.DESSERT }, + ์ œ๋กœ์ฝœ๋ผ: { price: 3000, kind: MENU_KIND.DRINK }, + ๋ ˆ๋“œ์™€์ธ: { price: 60000, kind: MENU_KIND.DRINK }, + ์ƒดํŽ˜์ธ: { price: 25000, kind: MENU_KIND.DRINK }, +}); + +const DATE = Object.freeze({ + EVENT_START: 1, + EVENT_END: 25, + PERIOD_DISCOUNT: 1000, + SPECIAL_DISCOUNT: 1000, + SPECIAL_DATE: [3, 10, 17, 24, 25, 31], + PER_DAY_DISCOUNT: 100, +}); + +const INFO = Object.freeze({ + UNIT: '์›', + COUNT: '๊ฐœ', + WEEK_DISCOUNT: 2023, + ORDER_MINIMUM: 10000, + GIFT_CONDITION: 120000, + GIFT_PRICE: 25000, + GIFT: '์ƒดํŽ˜์ธ 1๊ฐœ', + NONE: '์—†์Œ', + BADGE: { + SANTA: { PRICE: 20000, NAME: '์‚ฐํƒ€' }, + TREE: { PRICE: 10000, NAME: 'ํŠธ๋ฆฌ' }, + STAR: { PRICE: 5000, NAME: '๋ณ„' }, + }, +}); + +const DISCOUNT = Object.freeze({ + CHRISTMAS: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', + WEEK: 'ํ‰์ผ ํ• ์ธ', + WEEKEND: '์ฃผ๋ง ํ• ์ธ', + SPECIAL: 'ํŠน๋ณ„ ํ• ์ธ', + GIFTS: '์ฆ์ • ์ด๋ฒคํŠธ', +}); + +const INPUT_MESSAGE = Object.freeze({ + DATE: '12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n', + ORDER: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', +}); + +const OUTPUT_MESSAGE = Object.freeze({ + INTRO: '์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.\n', + TITLE: { + PREVIEW: (date) => `12์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ๋ณด๊ธฐ!\n`, + ORDER_MENU: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + BEFORE_DISCOUNT: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + AFTER_DISCOUNT: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + GIFT: '<์ฆ์ • ๋ฉ”๋‰ด>', + BENEFIT: '<ํ˜œํƒ ๋‚ด์—ญ>', + BENEFIT_AMOUNT: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + BADGE: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + }, +}); + +const ERROR_MESSAGE = Object.freeze({ + INVALID_DATE: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + INVALID_ORDER: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', +}); + +export { + MENU_KIND, + MENU, + DATE, + INFO, + DISCOUNT, + INPUT_MESSAGE, + OUTPUT_MESSAGE, + ERROR_MESSAGE, +};
JavaScript
์ •๋ณด ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž โ˜บ๏ธ
@@ -0,0 +1,90 @@ +const MENU_KIND = Object.freeze({ + APPETIZER: '์• ํ”ผํƒ€์ด์ €', + MAIN: '๋ฉ”์ธ', + DESSERT: '๋””์ €ํŠธ', + DRINK: '์Œ๋ฃŒ', +}); + +const MENU = Object.freeze({ + ์–‘์†ก์ด์ˆ˜ํ”„: { price: 6000, kind: MENU_KIND.APPETIZER }, + ํƒ€ํŒŒ์Šค: { price: 5500, kind: MENU_KIND.APPETIZER }, + ์‹œ์ €์ƒ๋Ÿฌ๋“œ: { price: 8000, kind: MENU_KIND.APPETIZER }, + ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ: { price: 55000, kind: MENU_KIND.MAIN }, + ๋ฐ”๋น„ํ๋ฆฝ: { price: 54000, kind: MENU_KIND.MAIN }, + ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€: { price: 35000, kind: MENU_KIND.MAIN }, + ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€: { price: 25000, kind: MENU_KIND.MAIN }, + ์ดˆ์ฝ”์ผ€์ดํฌ: { price: 15000, kind: MENU_KIND.DESSERT }, + ์•„์ด์Šคํฌ๋ฆผ: { price: 5000, kind: MENU_KIND.DESSERT }, + ์ œ๋กœ์ฝœ๋ผ: { price: 3000, kind: MENU_KIND.DRINK }, + ๋ ˆ๋“œ์™€์ธ: { price: 60000, kind: MENU_KIND.DRINK }, + ์ƒดํŽ˜์ธ: { price: 25000, kind: MENU_KIND.DRINK }, +}); + +const DATE = Object.freeze({ + EVENT_START: 1, + EVENT_END: 25, + PERIOD_DISCOUNT: 1000, + SPECIAL_DISCOUNT: 1000, + SPECIAL_DATE: [3, 10, 17, 24, 25, 31], + PER_DAY_DISCOUNT: 100, +}); + +const INFO = Object.freeze({ + UNIT: '์›', + COUNT: '๊ฐœ', + WEEK_DISCOUNT: 2023, + ORDER_MINIMUM: 10000, + GIFT_CONDITION: 120000, + GIFT_PRICE: 25000, + GIFT: '์ƒดํŽ˜์ธ 1๊ฐœ', + NONE: '์—†์Œ', + BADGE: { + SANTA: { PRICE: 20000, NAME: '์‚ฐํƒ€' }, + TREE: { PRICE: 10000, NAME: 'ํŠธ๋ฆฌ' }, + STAR: { PRICE: 5000, NAME: '๋ณ„' }, + }, +}); + +const DISCOUNT = Object.freeze({ + CHRISTMAS: 'ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ', + WEEK: 'ํ‰์ผ ํ• ์ธ', + WEEKEND: '์ฃผ๋ง ํ• ์ธ', + SPECIAL: 'ํŠน๋ณ„ ํ• ์ธ', + GIFTS: '์ฆ์ • ์ด๋ฒคํŠธ', +}); + +const INPUT_MESSAGE = Object.freeze({ + DATE: '12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)\n', + ORDER: + '์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)\n', +}); + +const OUTPUT_MESSAGE = Object.freeze({ + INTRO: '์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.\n', + TITLE: { + PREVIEW: (date) => `12์›” ${date}์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ๋ณด๊ธฐ!\n`, + ORDER_MENU: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + BEFORE_DISCOUNT: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + AFTER_DISCOUNT: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + GIFT: '<์ฆ์ • ๋ฉ”๋‰ด>', + BENEFIT: '<ํ˜œํƒ ๋‚ด์—ญ>', + BENEFIT_AMOUNT: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + BADGE: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + }, +}); + +const ERROR_MESSAGE = Object.freeze({ + INVALID_DATE: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + INVALID_ORDER: '[ERROR] ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', +}); + +export { + MENU_KIND, + MENU, + DATE, + INFO, + DISCOUNT, + INPUT_MESSAGE, + OUTPUT_MESSAGE, + ERROR_MESSAGE, +};
JavaScript
์˜ค ๊ธˆ์•ก์ด ํฐ ๊ฒฝ์šฐ ๋” ์œ ์šฉํ•˜๊ฒ ๋„ค์š” ์ •๋ณด ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค !
@@ -0,0 +1,60 @@ +import { ERROR_MESSAGE, MENU, MENU_KIND } from './constants'; + +const validateDate = date => { + if (!(date >= 1 && date <= 31)) { + throw new Error(ERROR_MESSAGE.INVALID_DATE); + } + if (typeof date !== 'number' || !Number.isInteger(date)) { + throw new Error(ERROR_MESSAGE.INVALID_DATE); + } +}; + +const validateName = menu => { + const checkedName = new Set(); + menu.forEach(([name]) => { + if (!Object.keys(MENU).includes(name)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } + checkedName.add(name); + }); +}; + +const validateDuplicate = menu => { + const checkedName = new Set(); + menu.forEach(([name]) => { + if (checkedName.has(name)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } + checkedName.add(name); + }); +}; + +const validateOnlyDrink = menu => { + const types = new Set(menu.map(([name]) => MENU[name].type)); + if (types.size === 1 && types.has(MENU_KIND.DRINK)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } +}; + +const validateCount = menu => { + if (!menu.every(([, count]) => count >= 1)) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } +}; + +const validateTotalCount = menu => { + const total = menu.reduce((acc, [, count]) => acc + count, 0); + if (total > 20) { + throw new Error(ERROR_MESSAGE.INVALID_ORDER); + } +}; + +const validateOrder = order => { + validateName(order); + validateDuplicate(order); + validateOnlyDrink(order); + validateCount(order); + validateTotalCount(order); +}; + +export { validateDate, validateOrder };
JavaScript
์—์–ด๋น„์•ค๋น„ ์ปจ๋ฒค์…˜์—์„œ ์ง€์–‘ํ•˜๋Š” ๊ฑฐ ๊ฐ™๋”๋ผ๊ตฌ์š”.. ๊ทธ๋ž˜์„œ ํ˜น์‹œ๋‚˜ ํ•ด์„œ ๋ปˆ๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์ข‹์•˜๋˜ ๊ฑฐ ๊ฐ™๋„ค์š” ๐Ÿค”
@@ -0,0 +1,10 @@ +const parseInput = userInput => + userInput + .trim() + .split(',') + .map(item => { + const [name, quantity] = item.trim().split('-'); + return [name, Number(quantity)]; + }); + +export default parseInput;
JavaScript
์•„๋งˆ ์ €๋Ÿฐ ์ž…๋ ฅ์ด ๋“ค์–ด์˜จ๋‹ค๋ฉด ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์—์„œ ๊ฑธ๋Ÿฌ๋‚ด์ค„ ์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค ! ๊ทธ๋ž˜๋„ ํŒŒ์‹ฑํ•˜๋Š” ๊ณผ์ •์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์—๋Ÿฌ ๊ฒฝ์šฐ๋ฅผ ์•Œ๋ ค์ฃผ์…”์„œ ์ข€ ๋” ๊ณ ๋ฏผํ•ด๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ณ„๊ธฐ๊ฐ€ ๋  ๊ฑฐ ๊ฐ™์•„์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค โ˜บ๏ธ
@@ -0,0 +1,96 @@ +import { DISCOUNT, INFO, MENU } from '../Util/constants'; +import Discount from './Discount'; + +class Client { + #date; + #order; + + constructor(date, order) { + this.#date = date; + this.#order = order; + } + + calculateBenefits() { + const discount = new Discount(); + return discount.calculateDiscount(this.#date, this.#order); + } + + getDate() { + return this.#date; + } + + getOrderList() { + let output = ''; + Object.entries(this.#order).forEach(([name, count]) => { + output += `${name} ${count}${INFO.COUNT}\n`; + }); + return output; + } + + getBeforeDiscount() { + let cost = 0; + Object.entries(this.#order).forEach(([name, count]) => { + cost += MENU[name].price * count; + }); + return cost; + } + + getGift() { + const isGift = this.getBeforeDiscount() >= INFO.GIFT_CONDITION; + return (isGift && INFO.GIFT) || INFO.NONE; + } + + getBenefitList() { + const benefit = this.calculateBenefits(); + if (!benefit || Object.keys(benefit).length === 0) { + return INFO.NONE; + } + let output = ''; + Object.entries(benefit).forEach(([discount, cost]) => { + output += `${discount}: ${cost.toLocaleString()}${INFO.UNIT}\n`; + }); + if (this.getGift() !== INFO.NONE) + output += `${DISCOUNT.GIFTS}: -${INFO.GIFT_PRICE.toLocaleString()}${ + INFO.UNIT + }\n`; + return output; + } + + getDiscountAmount() { + const benefit = this.calculateBenefits(); + if (!benefit || Object.keys(benefit).length === 0) return 0; + + let totalAmount = Object.values(benefit).reduce((acc, cur) => acc + cur); + if (this.getGift() !== INFO.NONE) totalAmount += -INFO.GIFT_PRICE; + return totalAmount; + } + + getAfterDiscount() { + const beforeDiscount = this.getBeforeDiscount(); + const discountAmount = this.getDiscountAmount(); + let gift = 0; + + if (this.getGift() !== INFO.NONE) { + gift = INFO.GIFT_PRICE; + } + + const total = beforeDiscount + discountAmount + gift; + return total; + } + + getEventBadge() { + const benefit = this.getDiscountAmount(); + + switch (true) { + case benefit <= -INFO.BADGE.SANTA.PRICE: + return INFO.BADGE.SANTA.NAME; + case benefit <= -INFO.BADGE.TREE.PRICE: + return INFO.BADGE.TREE.NAME; + case benefit <= -INFO.BADGE.STAR.PRICE: + return INFO.BADGE.STAR.NAME; + default: + return INFO.NONE; + } + } +} +export default Client;
JavaScript
์˜ค ํ›จ์”ฌ ๊น”๋”ํ•œ ๊ฑฐ ๊ฐ™์•„ ์ข‹๋„ค์š” ํ”ผ๋“œ๋ฐฑ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค โ˜บ๏ธ
@@ -0,0 +1,96 @@ +import { DISCOUNT, INFO, MENU } from '../Util/constants'; +import Discount from './Discount'; + +class Client { + #date; + #order; + + constructor(date, order) { + this.#date = date; + this.#order = order; + } + + calculateBenefits() { + const discount = new Discount(); + return discount.calculateDiscount(this.#date, this.#order); + } + + getDate() { + return this.#date; + } + + getOrderList() { + let output = ''; + Object.entries(this.#order).forEach(([name, count]) => { + output += `${name} ${count}${INFO.COUNT}\n`; + }); + return output; + } + + getBeforeDiscount() { + let cost = 0; + Object.entries(this.#order).forEach(([name, count]) => { + cost += MENU[name].price * count; + }); + return cost; + } + + getGift() { + const isGift = this.getBeforeDiscount() >= INFO.GIFT_CONDITION; + return (isGift && INFO.GIFT) || INFO.NONE; + } + + getBenefitList() { + const benefit = this.calculateBenefits(); + if (!benefit || Object.keys(benefit).length === 0) { + return INFO.NONE; + } + let output = ''; + Object.entries(benefit).forEach(([discount, cost]) => { + output += `${discount}: ${cost.toLocaleString()}${INFO.UNIT}\n`; + }); + if (this.getGift() !== INFO.NONE) + output += `${DISCOUNT.GIFTS}: -${INFO.GIFT_PRICE.toLocaleString()}${ + INFO.UNIT + }\n`; + return output; + } + + getDiscountAmount() { + const benefit = this.calculateBenefits(); + if (!benefit || Object.keys(benefit).length === 0) return 0; + + let totalAmount = Object.values(benefit).reduce((acc, cur) => acc + cur); + if (this.getGift() !== INFO.NONE) totalAmount += -INFO.GIFT_PRICE; + return totalAmount; + } + + getAfterDiscount() { + const beforeDiscount = this.getBeforeDiscount(); + const discountAmount = this.getDiscountAmount(); + let gift = 0; + + if (this.getGift() !== INFO.NONE) { + gift = INFO.GIFT_PRICE; + } + + const total = beforeDiscount + discountAmount + gift; + return total; + } + + getEventBadge() { + const benefit = this.getDiscountAmount(); + + switch (true) { + case benefit <= -INFO.BADGE.SANTA.PRICE: + return INFO.BADGE.SANTA.NAME; + case benefit <= -INFO.BADGE.TREE.PRICE: + return INFO.BADGE.TREE.NAME; + case benefit <= -INFO.BADGE.STAR.PRICE: + return INFO.BADGE.STAR.NAME; + default: + return INFO.NONE; + } + } +} +export default Client;
JavaScript
์ข‹์€ ์˜๊ฒฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋ง์”€ํ•˜์‹  ๊ฑฐ์ฒ˜๋Ÿผ ๋ชจ๋ธ์ด ๋ทฐ์˜ ์„ธ๋ถ€์‚ฌํ•ญ์„ ๊ณผ๋„ํ•˜๊ฒŒ ์•Œ๊ณ  ์žˆ๋Š” ํ˜•ํƒœ๋ผ๋ฉด ํ‘œํ˜„๋ฐฉ์‹์ด ํ˜ผํ•ฉ๋œ ๋А๋‚Œ์ด ๋“ค์–ด ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์งˆ ๊ฑฐ ๊ฐ™๋„ค์š” ใ…œใ…œ ์‚ฌ์‹ค mvc ํŒจํ„ด์— ์ง‘์ฐฉํ•˜์ง€ ์•Š๊ณ  ํด๋ž˜์Šค ์ž์ฒด๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์—ญํ• ์„ ์ƒ๊ฐํ•˜๊ณ , ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ์‹์„ ๊ณ ๋ คํ•˜๋‹ค๋ณด๋‹ˆ ํŒจํ„ด์—์„œ ์˜ค๋Š” ์ฒ˜๋ฆฌ๊ธฐ์ค€๊ณผ๋Š” ์–ด๊ธ‹๋‚˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฑฐ ๊ฐ™๋„ค์š” ... ๋ฒ ๋ฆฌ์—์ด์…˜์ด ๋‹ค์–‘ํ•˜๊ฒŒ ์กด์žฌํ•˜๋Š” ์ƒํ™ฉ์—์„œ๋„ ์œ ์—ฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ข€๋” ๊ณ ๋ฏผํ•ด๋ด์•ผ๊ฒ ๋„ค์š” ์กฐ์–ธ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค โ˜บ๏ธ
@@ -0,0 +1,10 @@ +const parseInput = userInput => + userInput + .trim() + .split(',') + .map(item => { + const [name, quantity] = item.trim().split('-'); + return [name, Number(quantity)]; + }); + +export default parseInput;
JavaScript
ํ•ธ๋“ค ์ธํ’‹์—์„œ ๋ฐ›์•„์„œ ๋ฐ”๋กœ ํŒŒ์‹ฑํ•˜์‹œ๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์—์„œ ๋ˆ„๋ฝ๋˜์ง€ ์•Š์„๊นŒ์š”?
@@ -0,0 +1,208 @@ +## ํฌ๋ฆฌ์Šค๋งˆ์Šค ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๋Šฅ ๋ชฉ๋ก ์ •๋ฆฌ ๐ŸŽ„ + +### ํด๋ž˜์Šค, ํ•จ์ˆ˜ ์ •๋ฆฌ + +1. Domain +- 1-1. `PromotionController` + - ์ „์ฒด ํ”„๋กœ๋ชจ์…˜์˜ ํ”„๋กœ์„ธ์Šค ๋ฐ ์ž…์ถœ๋ ฅ์„ ๋‹ด๋‹นํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค +- 1-2. `Discount` + - ๋‚ ์งœ์— ๋Œ€ํ•œ ํ˜œํƒ๋“ค์„ ํ™•์ธํ•˜๊ณ , ํ˜œํƒ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค +- 1-3. `Client` + - ์ฃผ๋ฌธ๊ณผ ๋‚ ์งœ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ณ ๊ฐ์˜ ํ• ์ธ ํ˜œํƒ๊ณผ ์ฃผ๋ฌธ ๊ฐ€๊ฒฉ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค + +2. View +- 2-1. `InputView` + - ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›์•„ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ณ , ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ +- 2-2. `OutputView` + - ํ”„๋กœ์„ธ์Šค ๊ฒฐ๊ณผ ๋ฐ ์˜ค๋ฅ˜ ๋ฉ”์„ธ์ง€๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜ + + +3. Util +- 3-1. `constants` + - ์‚ฌ์šฉ๋˜๋Š” ๋งค์ง๋„˜๋ฒ„, ์—๋Ÿฌ๋ฉ”์„ธ์ง€, ์ž…์ถœ๋ ฅ ๋ฉ”์‹œ์ง€, ์ถœ๋ ฅ ํƒ€์ดํ‹€ ๋“ฑ ์ƒ์ˆ˜ ์ •์˜ +- 3-2. `inputHandler` + - InputView ๋‚ด์—์„œ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ๋กœ์ง ํ•ธ๋“ค๋Ÿฌ ์ •์˜ +- 3-3. `parseInput` + - ๊ณ ๊ฐ์˜ ๋ฉ”๋‰ด ์ž…๋ ฅ์„ ๋ฉ”๋‰ด ์ฃผ๋ฌธ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ ์ •์˜ +- 3-4. `validateInput` + - ๊ณ ๊ฐ์˜ ์ž…๋ ฅ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ํ•จ์ˆ˜ ์ •์˜ +4. `App` + - ํ”„๋กœ๋ชจ์…˜ ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ์ „์ฒด +5. `index` + - ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” App์„ ์‹คํ–‰ํ•˜๋Š” ์ง„์ž…์  + + +### ๊ตฌํ˜„ ๊ธฐ๋Šฅ ์ •๋ฆฌ + +**1. ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ ์ž…๋ ฅ** + +- [x] ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ ๋ฉ˜ํŠธ ์ถœ๋ ฅ +- [x] ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ๋‚ ์งœ(์ผ) ์ž…๋ ฅ ๋ฐ›๊ธฐ +- [x] ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ๋‚ ์งœ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์ฆ + +**2. ์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜ ์ž…๋ ฅ** + +- [x] ์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด์™€ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š” ๋ฌธ๊ตฌ ์ถœ๋ ฅ +- [x] `์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด-๋ฉ”๋‰ด ๊ฐœ์ˆ˜` ํ˜•ํƒœ ์ž…๋ ฅ ๋ฐ›๊ธฐ +- [x] ์ž…๋ ฅ ๋ฐ›์€ ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์ฆ + +**3. ๋ฐฉ๋ฌธ ๋‚ ์งœ์— ๋Œ€ํ•œ ํ˜œํƒ ์‹œ์ž‘ ๋ฉ˜ํŠธ ์ถœ๋ ฅ** + +- [x] 12์›” (1.์—์„œ ์ž…๋ ฅ๋ฐ›์€ ๋‚ ์งœ)์ผ ์— ๋Œ€ํ•œ ํ˜œํƒ ์†Œ๊ฐœ ๋ฌธ๊ตฌ ์ถœ๋ ฅ + +**4. ์ฃผ๋ฌธ ๋ฉ”๋‰ด ์ถœ๋ ฅ** + +- [x] `<์ฃผ๋ฌธ๋ฉ”๋‰ด>` ์ถœ๋ ฅ +- [x] `์ฃผ๋ฌธ ๋ฉ”๋‰ด-๋ฉ”๋‰ด ๊ฐœ์ˆ˜` ํ˜•ํƒœ๋กœ ์ž…๋ ฅ ๋ฐ›์€ ๋‚ด์šฉ ์ถœ๋ ฅ + +**5. ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก ์ถœ๋ ฅ** + +- [x] `<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>`์ถœ๋ ฅ +- [x] ์ฃผ๋ฌธ ๊ธˆ์•ก ์ถœ๋ ฅ + +**6. ์ฆ์ •๋ฉ”๋‰ด ์ฆ์ • ์—ฌ๋ถ€ ์ถœ๋ ฅ** + +- [x] `<์ฆ์ • ๋ฉ”๋‰ด>` ์ถœ๋ ฅ +- [x] `์ƒดํŽ˜์ธ 1๊ฐœ` ๋˜๋Š” `์—†์Œ` ์ถœ๋ ฅ + +**7. ํ˜œํƒ ๋‚ด์—ญ ์ถœ๋ ฅ** + +- [x] `<ํ˜œํƒ ๋‚ด์—ญ>` ์ถœ๋ ฅ +- [x] ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ธ ํ• ์ธ ๊ธˆ์•ก ์ถœ๋ ฅ +- [x] ํ‰์ผ ํ• ์ธ ๊ธˆ์•ก ์ถœ๋ ฅ +- [x] ํŠน๋ณ„ ํ• ์ธ ๊ธˆ์•ก ์ถœ๋ ฅ +- [x] ์ฆ์ • ์ด๋ฒคํŠธ ํ• ์ธ ์ด๋ฒคํŠธ ์ถœ๋ ฅ + +**8. ์ดํ˜œํƒ ๊ธˆ์•ก ์ถœ๋ ฅ** + +- [x] `<์ดํ˜œํƒ ๊ธˆ์•ก>` ์ถœ๋ ฅ +- [x] ์ด ํ˜œํƒ ๊ธˆ์•ก ์ถœ๋ ฅ + +**9. ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก ์ถœ๋ ฅ** + +- [x] `<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>`์ถœ๋ ฅ +- [x] ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก ์ถœ๋ ฅ + +**10. 12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ์ถœ๋ ฅ** + +- [x] `<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>` ์ถœ๋ ฅ +- [x] ์ด๋ฒคํŠธ ๋ฐฐ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ฐฐ์ง€ ์ถœ๋ ฅ + + +### ์ด๋ฒคํŠธ ์ •๋ฆฌ + +1-1. 2023.12.1 ~ 2023.12.25 ํ• ์ธ + +- ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ + - 12.1 ๊ธฐ์ค€ 1,000์› ํ• ์ธ ~ + - 12.25 ๊ธฐ์ค€ 3400์› ํž์ธ + +1-2. 2023.12.1 ~ 2023.12.31 ํ• ์ธ + +- ํ‰์ผ ํ• ์ธ (์ผ, ์›”, ํ™”, ์ˆ˜, ๋ชฉ) + - ๋””์ €ํŠธ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2023์› ํ• ์ธ +- ์ฃผ๋ง ํ• ์ธ (๊ธˆ, ํ† ) + - ๋ฉ”์ธ ๋ฉ”๋‰ด 1๊ฐœ๋‹น 2023์› ํ• ์ธ +- ํŠน๋ณ„ ํ• ์ธ (3(์ผ), 10(์ผ), 17(์ผ), 24(์ผ), 25(์›”), 31(์ผ)) + - ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์—์„œ 1000์› ํ• ์ธ +- ์ฆ์ • ์ด๋ฒคํŠธ + - **ํ• ์ธ ์ „ ์ด ์ฃผ๋ฌธ๊ธˆ์•ก**์ด 12๋งŒ์› ์ด์ƒ์ผ ๋•Œ, ์ƒดํŽ˜์ธ 1๊ฐœ ์ฆ์ • + +1-3. ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ๋ถ€์—ฌ + +- ์ด ํ˜œํƒ ๊ธˆ์•ก์— ๋”ฐ๋ฅธ ์ด๋ฒคํŠธ ๋ฐฐ์ง€ + - ๋ฏธ์ถฉ์กฑ : ์—†์Œ + - 5์ฒœ์› ์ด์ƒ : ๋ณ„ + - 1๋งŒ์› ์ด์ƒ : ํŠธ๋ฆฌ + - 2๋งŒ์› ์ด์ƒ : ์‚ฐํƒ€ + +1-4. ์ฃผ์˜ ์‚ฌํ•ญ + +- 'ํ• ์ธ'์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€๋งŒ, ๊ธˆ์•ก '์ฐจ๊ฐ'์— ๋” ๊ฐ€๊นŒ์›€. %๋กœ ํ• ์ธ์ด ์•„๋‹Œ - ํ˜•์‹ +- ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก 10,000์› ์ด์ƒ์ธ์ง€ ํ™•์ธํ•˜๊ณ  ์ด๋ฒคํŠธ ์ ์šฉ ํ•„์š” +- ์Œ๋ฃŒ๋งŒ, ์ฃผ๋ฌธ ์‹œ ์ฃผ๋ฌธ ์ž์ฒด๊ฐ€ ๋ถˆ๊ฐ€ +- ๋ฉ”๋‰ด๋Š” ํ•œ๋ฒˆ์— ์ตœ๋Œ€ 20๊ฐœ ๊นŒ์ง€ ์ฃผ๋ฌธ ๊ฐ€๋Šฅ + + +### ํ๋ฆ„ ์ •๋ฆฌ + + +**1. ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ ์ž…๋ ฅ** + +- 1-1. ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ ์†Œ๊ฐœ ๋ฉ˜ํŠธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +- 1-2. ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ(์ผ)๋ฅผ ์ˆซ์ž๋กœ๋งŒ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. + -1-2-1. ์ž…๋ ฅ ๋ฐ›์€ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. + +**2. ์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜ ์ž…๋ ฅ** + +- 2-1. ์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด์™€ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š” ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +- 2-2. `์ฃผ๋ฌธํ•  ๋ฉ”๋‰ด-๋ฉ”๋‰ด ๊ฐœ์ˆ˜` ํ˜•ํƒœ๋กœ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. + - 2-2-1. ์ž…๋ ฅ ๋ฐ›์€ ์ฃผ๋ฌธ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. + +**3. ๋ฐฉ๋ฌธ ๋‚ ์งœ์— ๋Œ€ํ•œ ํ˜œํƒ ์‹œ์ž‘ ๋ฉ˜ํŠธ ์ถœ๋ ฅ** + +- 3-1. 12์›” (1.์—์„œ ์ž…๋ ฅ๋ฐ›์€ ๋‚ ์งœ)์ผ ์— ๋Œ€ํ•œ ํ˜œํƒ ์†Œ๊ฐœ ๋ฉ˜ํŠธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. + +**4. ์ฃผ๋ฌธ ๋ฉ”๋‰ด ์ถœ๋ ฅ** + +- 4-1. `<์ฃผ๋ฌธ๋ฉ”๋‰ด>`๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +- 4-2. `์ฃผ๋ฌธ ๋ฉ”๋‰ด-๋ฉ”๋‰ด ๊ฐœ์ˆ˜` ํ˜•ํƒœ๋กœ ์ž…๋ ฅ ๋ฐ›์€ ๋‚ด์šฉ์„ `์ฃผ๋ฌธ ๋ฉ”๋‰ด (๋ฉ”๋‰ด๊ฐœ์ˆ˜)๊ฐœ\n` ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. + +**5. ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก ์ถœ๋ ฅ** + +- 5-1. `<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>`์„ ์ถœ๋ ฅํ•œ๋‹ค. +- 5-2. ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. + - 5-2-1. 1,000 ๋‹จ์œ„๋‹น `,` ์„ ๋ถ™์—ฌ ์ถœ๋ ฅ ํ•œ๋‹ค. + +**6. ์ฆ์ •๋ฉ”๋‰ด ์ฆ์ • ์—ฌ๋ถ€ ์ถœ๋ ฅ** + +- 6-1. `์ƒดํŽ˜์ธ (๊ฐœ์ˆ˜)๊ฐœ` ์ถœ๋ ฅํ•œ๋‹ค. + - 6-2-1. ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์ด 12๋งŒ์› ์ด์ƒ ๊ฒฝ์šฐ ๋‹น ์ƒดํŽ˜์ธ์ด 1๊ฐœ์”ฉ ์ฆ์ •๋œ๋‹ค. + +**7. ํ˜œํƒ ๋‚ด์—ญ ์ถœ๋ ฅ** + +- 7-1. `<ํ˜œํƒ ๋‚ด์—ญ>` ์„ ์ถœ๋ ฅํ•˜๋ฉฐ, ์•„๋ž˜ ๊ธˆ์•ก์€ `-(๊ธˆ์•ก)์›` ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. + - 7-1-1. 1,000 ๋‹จ์œ„๋‹น `,` ์„ ๋ถ™์—ฌ ์ถœ๋ ฅ ํ•œ๋‹ค. +- 7-2. ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ธ ํ• ์ธ ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. +- 7-3. ํ‰์ผ ํ• ์ธ ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. +- 7-4. ํŠน๋ณ„ ํ• ์ธ ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. +- 7-5. ์ฆ์ • ์ด๋ฒคํŠธ ํ• ์ธ ์ด๋ฒคํŠธ ์ถœ๋ ฅํ•œ๋‹ค. + +**8. ์ดํ˜œํƒ ๊ธˆ์•ก ์ถœ๋ ฅ** + +- 8-1. `<์ดํ˜œํƒ ๊ธˆ์•ก>` ์„ ์ถœ๋ ฅํ•˜๋ฉฐ, ์•„๋ž˜ ๊ธˆ์•ก์€ `-(๊ธˆ์•ก)์›` ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. + - 8-1-1. 1,000 ๋‹จ์œ„๋‹น `,` ์„ ๋ถ™์—ฌ ์ถœ๋ ฅ ํ•œ๋‹ค. +- 8-2. *7. ํ˜œํƒ๋‚ด์—ญ ์ถœ๋ ฅ* ์˜ `7-2 ~ 7-5` ์— ๊ธˆ์•ก์„ ๋ชจ๋‘ ๋”ํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค. + + +**9. ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก ์ถœ๋ ฅ** + +- 9-1. `<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>` ์„ ์ถœ๋ ฅํ•˜๋ฉฐ, ์•„๋ž˜ ๊ธˆ์•ก์€ `(๊ธˆ์•ก)์›` ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. + - 9-1-1. 1,000 ๋‹จ์œ„๋‹น `,` ์„ ๋ถ™์—ฌ ์ถœ๋ ฅํ•œ๋‹ค. +- 9-2. **5. ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก ์ถœ๋ ฅ** ์—์„œ **8. ์ด ํ˜œํƒ ๊ธˆ์•ก ์ถœ๋ ฅ** ์„ ๋บ€ ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. + +**10. 12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€ ์ถœ๋ ฅ** + +- 10-1. `<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>` ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +- 10-2. ์ถœ๋ ฅํ•  ๋ฐฐ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ฐฐ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋ฉฐ, ์—†๋Š” ๊ฒฝ์šฐ `์—†์Œ`์„ ์ถœ๋ ฅํ•œ๋‹ค. + + +### ๐Ÿ—‚ ํŒŒ์ผ ๊ตฌ์กฐ + +``` +โ”— src + โ”ฃ Domain + โ”ƒ โ”ฃ PromotionController.js + โ”ƒ โ”ฃ Client.js + โ”ƒ โ”— Discount.js + โ”ฃ View + โ”ƒ โ”ฃ InputView.js + โ”ƒ โ”— OutputView.js + โ”ฃ Util + โ”ƒ โ”ฃ constants.js + โ”ƒ โ”ฃ parseInput.js + โ”ƒ โ”ฃ inputHandler.js + โ”ƒ โ”— validateInput.js + โ”ฃ App.js + โ”— index.js +``` \ No newline at end of file
Unknown
์ฒ˜์Œ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๋ชฉ๋ก ์ž‘์„ฑ์ „์— ํ๋ฆ„์ •๋ฆฌ๋ฅผ ๋จผ์ € ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ 4์ฃผ๊ฐ„ ์ง„ํ–‰ํ–ˆ์–ด์š” !! ์ด๋•Œ ๋ญ˜ ๊ตฌํ˜„ํ•ด์•ผํ• ์ง€๊ฐ€ ์ž˜ ๋ณด์—ฌ์„œ ๋„์›€์ด ๋งŽ์ด ๋˜์—ˆ๋˜ ๊ฑฐ ๊ฐ™์•„์š” ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค ใ…Žใ…Ž
@@ -0,0 +1,58 @@ +import { DATE, DISCOUNT, INFO, MENU, MENU_KIND } from '../Util/constants'; + +class Discount { + calculateDiscount(date, menu) { + const discount = {}; + const addDisCount = (key, value) => { + if (value !== 0) discount[key] = value; + }; + + addDisCount(DISCOUNT.CHRISTMAS, this.isPeriod(date)); + addDisCount(DISCOUNT.WEEK, this.isWeekday(date, menu)); + addDisCount(DISCOUNT.WEEKEND, this.isWeekend(date, menu)); + addDisCount(DISCOUNT.SPECIAL, this.isSpecialDay(date)); + return discount; + } + + isPeriod(date) { + let periodDiscount = 0; + if (date >= DATE.EVENT_START && date <= DATE.EVENT_END) { + periodDiscount += + -DATE.PERIOD_DISCOUNT + (date - 1) * -DATE.PER_DAY_DISCOUNT; + } + return periodDiscount; + } + + isWeekday(date, menu) { + let weekDisCount = 0; + if (!(date % 7 === 1 || date % 7 === 2)) { + let count = 0; + Object.keys(menu).forEach(name => { + if (MENU[name].kind === MENU_KIND.DESSERT) count += menu[name]; + }); + weekDisCount += -INFO.WEEK_DISCOUNT * count; + } + return weekDisCount; + } + + isWeekend(date, menu) { + let weekendDisCount = 0; + if (date % 7 === 1 || date % 7 === 2) { + let count = 0; + Object.keys(menu).forEach(name => { + if (MENU[name].kind === MENU_KIND.MAIN) count += menu[name]; + }); + weekendDisCount += -INFO.WEEK_DISCOUNT * count; + } + return weekendDisCount; + } + + isSpecialDay(date) { + let specialDayDiscount = 0; + if (DATE.SPECIAL_DATE.includes(date)) + specialDayDiscount += -DATE.SPECIAL_DISCOUNT; + return specialDayDiscount; + } +} + +export default Discount;
JavaScript
๋‚ ์งœ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๊ด€๋ จ ์ƒ์ˆ˜๋ฅผ ์ •์˜ํ•ด๋‘์‹  ๋ถ„๋“ค์ด ๋งŽ์ด ๋ณด์ด๋”๋ผ๊ตฌ์š” ! ์ €๋„ ๊ด€๋ จ๋œ ๋ณ€์ˆ˜๋Š” ์ •์˜ํ•˜๋Š”๊ฒŒ ์ข‹์€ ๊ฑฐ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“œ๋„ค์š” ใ…Žใ…Ž ์ถ”์ฒœ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ~ โ˜บ๏ธ
@@ -0,0 +1,58 @@ +import { DATE, DISCOUNT, INFO, MENU, MENU_KIND } from '../Util/constants'; + +class Discount { + calculateDiscount(date, menu) { + const discount = {}; + const addDisCount = (key, value) => { + if (value !== 0) discount[key] = value; + }; + + addDisCount(DISCOUNT.CHRISTMAS, this.isPeriod(date)); + addDisCount(DISCOUNT.WEEK, this.isWeekday(date, menu)); + addDisCount(DISCOUNT.WEEKEND, this.isWeekend(date, menu)); + addDisCount(DISCOUNT.SPECIAL, this.isSpecialDay(date)); + return discount; + } + + isPeriod(date) { + let periodDiscount = 0; + if (date >= DATE.EVENT_START && date <= DATE.EVENT_END) { + periodDiscount += + -DATE.PERIOD_DISCOUNT + (date - 1) * -DATE.PER_DAY_DISCOUNT; + } + return periodDiscount; + } + + isWeekday(date, menu) { + let weekDisCount = 0; + if (!(date % 7 === 1 || date % 7 === 2)) { + let count = 0; + Object.keys(menu).forEach(name => { + if (MENU[name].kind === MENU_KIND.DESSERT) count += menu[name]; + }); + weekDisCount += -INFO.WEEK_DISCOUNT * count; + } + return weekDisCount; + } + + isWeekend(date, menu) { + let weekendDisCount = 0; + if (date % 7 === 1 || date % 7 === 2) { + let count = 0; + Object.keys(menu).forEach(name => { + if (MENU[name].kind === MENU_KIND.MAIN) count += menu[name]; + }); + weekendDisCount += -INFO.WEEK_DISCOUNT * count; + } + return weekendDisCount; + } + + isSpecialDay(date) { + let specialDayDiscount = 0; + if (DATE.SPECIAL_DATE.includes(date)) + specialDayDiscount += -DATE.SPECIAL_DISCOUNT; + return specialDayDiscount; + } +} + +export default Discount;
JavaScript
์˜ค ๋ณ€์ˆ˜ ์„ ์–ธ์„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ๊ฑฐ ๊ฐ™์•„์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž!!
@@ -0,0 +1,26 @@ +package christmas.config; + +import java.util.Arrays; + +public enum EventBadge { + SANTA("์‚ฐํƒ€", 20000), + TREE("ํŠธ๋ฆฌ", 10000), + STAR("๋ณ„", 5000), + NONE("์—†์Œ", 0); + + private final String name; + private final int limitPrice; + + EventBadge(String name, int limitPrice) { + this.name = name; + this.limitPrice = limitPrice; + } + + public static String findBadgeByTotalDiscount(int totalDiscount) { + return Arrays.stream(values()) + .filter(badge -> totalDiscount >= badge.limitPrice) + .findFirst() + .orElse(NONE) + .name; + } +}
Java
์ด ๋ฉ”์„œ๋“œ๋Š” EvenBadge์˜ ๋ฉค๋ฒ„์˜ ์„ ์–ธ ์ˆœ์„œ์— ์˜์กด์ ์ธ ๊ฒƒ ๊ฐ™์•„์š”. ๋” ์ข‹๊ฒŒ ๊ฐœ์„ ํ•  ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ์š”?
@@ -0,0 +1,25 @@ +package christmas.config; + +public class Message { + public static final String INPUT_DATE = "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"; + public static final String INPUT_ORDER = "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)"; + + public static final String OUTPUT_VISIT_DATE = "12์›” %d์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n"; + public static final String OUTPUT_MENU_TITLE = "\n<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"; + public static final String OUTPUT_ITEM = "%s %d๊ฐœ%n"; + public static final String OUTPUT_WON = "%s์›%n"; + public static final String OUTPUT_MINUS_WON = "-" + OUTPUT_WON; + public static final String OUTPUT_TOTAL_PRICE = "%n<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>%n" + OUTPUT_WON; + public static final String OUTPUT_GIFT_TITLE = "\n<์ฆ์ • ๋ฉ”๋‰ด>"; + public static final String OUTPUT_PROMOTION_TITLE = "\n<ํ˜œํƒ ๋‚ด์—ญ>"; + public static final String OUTPUT_PROMOTION_ITEM = "%s: " + OUTPUT_MINUS_WON; + public static final String OUTPUT_TOTAL_DISCOUNT = "%n<์ดํ˜œํƒ ๊ธˆ์•ก>%n" + OUTPUT_WON; + public static final String OUTPUT_PAYMENT_PRICE = "%n<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>%n" + OUTPUT_WON; + public static final String OUTPUT_BADGE = "%n<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>%n%s%n"; + public static final String OUTPUT_NONE = "์—†์Œ"; + + + public static final String ERROR_MESSAGE_PREFIX = "[ERROR]"; + public static final String ERROR_INPUT_DATE = ERROR_MESSAGE_PREFIX + " ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + public static final String ERROR_INPUT_ORDER = ERROR_MESSAGE_PREFIX + " ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; +}
Java
ํ”„๋กœ๊ทธ๋žจ ์ „์—ญ์—์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  Output์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜๋Š” ๋”ฐ๋กœ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ  Output๊ฐ์ฒด ์•ˆ์— ์ƒ์ˆ˜ ํ•„๋“œ๋กœ ์„ ์–ธํ•ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”:smile: ์ถ”๊ฐ€์ ์œผ๋กœ Message ์•ˆ์— ์—๋Ÿฌ๋ฉ”์‹œ์ง€์™€ Output์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ƒ์ˆ˜๊ฐ€ ํ˜ผํ•ฉ๋˜์–ด ์žˆ์–ด์„œ ๋ถ„๋ฆฌํ•˜๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,25 @@ +package christmas.controller; + +import christmas.model.Order; +import christmas.model.VisitDate; +import christmas.view.InputView; + +public class InputController { + public static VisitDate setVisitDate() { + try { + return new VisitDate(InputView.readDate()); + } catch (IllegalArgumentException illegalArgumentException) { + System.out.println(illegalArgumentException.getMessage()); + return setVisitDate(); + } + } + + public static Order setOrder() { + try { + return new Order(InputView.readOrder()); + } catch (IllegalArgumentException illegalArgumentException) { + System.out.println(illegalArgumentException.getMessage()); + return setOrder(); + } + } +}
Java
MVC ํŒจํ„ด์—์„œ Controller๋ผ๋ฆฌ ์„œ๋กœ ์˜์กดํ•˜๋Š” ๊ฒƒ์€ ๋ณ„๋กœ ์ข‹์ง€ ์•Š๋‹ค๊ณ  ์•Œ๊ณ  ์žˆ๋Š”๋ฐ InputController๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค๊ณ  PromotionController์—์„œ ์‚ฌ์šฉํ•œ ์ด์œ ๊ฐ€ ๋”ฐ๋กœ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,25 @@ +package christmas.controller; + +import christmas.model.Order; +import christmas.model.VisitDate; +import christmas.view.InputView; + +public class InputController { + public static VisitDate setVisitDate() { + try { + return new VisitDate(InputView.readDate()); + } catch (IllegalArgumentException illegalArgumentException) { + System.out.println(illegalArgumentException.getMessage()); + return setVisitDate(); + } + } + + public static Order setOrder() { + try { + return new Order(InputView.readOrder()); + } catch (IllegalArgumentException illegalArgumentException) { + System.out.println(illegalArgumentException.getMessage()); + return setOrder(); + } + } +}
Java
setOrder, setVisitDate ๊ฐ๊ฐ VisitDate, Order ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š”. set์ด๋ผ๋Š” ์ ‘๋‘์–ด๋ณด๋‹ค generate๊ฐ€ ์ข€ ๋” ์–ด์šธ๋ฆฐ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ์–ด๋– ์‹ ๊ฐ€์š”?
@@ -0,0 +1,49 @@ +package christmas.controller; + +import christmas.config.Constant; +import christmas.config.EventBadge; +import christmas.config.Gift; +import christmas.model.Order; +import christmas.model.Promotion; +import christmas.model.PromotionGenerator; +import christmas.model.VisitDate; +import christmas.view.OutputView; + +public class PromotionController { + private final VisitDate visitDate; + private final Order order; + private final Promotion promotion; + + public PromotionController() { + visitDate = InputController.setVisitDate(); + order = InputController.setOrder(); + promotion = new PromotionGenerator(visitDate, order).getPromotion(); + } + + public void active() { + print(); + } + + private int totalPrice() { + return order.getTotalPrice(); + } + + private int totalDiscount() { + return promotion.getTotalDiscount(); + } + + private boolean isEmptyPromotion() { + return promotion.getTotalDiscount() == 0; + } + + private void print() { + OutputView.printVisitDate(visitDate.getVisitDate()); + OutputView.printOrder(order.getOrderList()); + OutputView.printTotalPrice(totalPrice()); + OutputView.printGift(Gift.getGift(totalPrice())); + OutputView.printPromotion(isEmptyPromotion(), promotion.getPromotionList()); + OutputView.printTotalDiscount(totalDiscount() * Constant.MINUS); + OutputView.printPaymentPrice(totalPrice() - promotion.getDiscount()); + OutputView.printBadge(EventBadge.findBadgeByTotalDiscount(totalDiscount())); + } +}
Java
๋ฉ”์„œ๋“œ ๋ช…์„ ๋ช…์‚ฌ๋กœ ํ•˜๋Š” ๊ฒƒ์€ ๋ณ„๋กœ ์ข‹์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,45 @@ +package christmas.model; + +import christmas.config.Event; +import christmas.config.Gift; +import christmas.config.Menu; + +import java.util.EnumMap; +import java.util.Map; + +public class GiftEvent implements DiscountEventInterface { + private final int totalPrice; + + public GiftEvent(int totalPrice) { + this.totalPrice = totalPrice; + } + + private Gift getGift() { + return Gift.getGift(totalPrice); + } + + private String getGiftMenu() { + return getGift().getMenu(); + } + + @Override + public boolean isDiscountTarget() { + return !getGift().equals(Gift.NONE); + } + + private int getGiftPrice() { + return Menu.getMenu(getGiftMenu()) + .getPrice(); + } + + @Override + public Map<Event, Integer> getDiscountInfo() { + Map<Event, Integer> discountInfo = new EnumMap<>(Event.class); + + if (isDiscountTarget()) { + discountInfo.put(Event.GIFT, getGiftPrice()); + } + + return discountInfo; + } +}
Java
์ด๋ฒคํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ณตํ†ต๊ธฐ๋Šฅ์„ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ž˜ ๋ฝ‘์€ ๊ฒƒ ๊ฐ™์•„์š”:+1:
@@ -0,0 +1,46 @@ +package christmas.model; + +import christmas.config.Menu; +import christmas.config.MenuType; +import christmas.util.Util; +import christmas.util.validator.OrderGenerateAfterValidator; +import christmas.util.validator.OrderGenerateBeforeValidator; + +import java.util.Map; +import java.util.stream.Collectors; + +public class Order { + private final Map<Menu, Integer> order; + + public Order(String order) { + new OrderGenerateBeforeValidator(order); + this.order = new OrderGenerator(order).generateOrder(); + new OrderGenerateAfterValidator(this); + } + + public int getTotalQuantity() { + return Util.intStreamSum(order.values().stream()); + } + + public int getMenuQuantity(MenuType menuType) { + return Util.intStreamSum(order.keySet() + .stream() + .filter(menu -> menu.getType() == menuType) + .map(order::get)); + } + + public int getTotalPrice() { + return Util.intStreamSum(order.entrySet() + .stream() + .map(entry -> entry.getKey().getPrice() * entry.getValue())); + } + + public Map<String, Integer> getOrderList() { + return order.entrySet() + .stream() + .collect(Collectors.toMap( + entry -> entry.getKey().getName(), + Map.Entry::getValue + )); + } +}
Java
์ €๋Š” String ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„์„œ Order ๊ฐ์ฒด์—์„œ ํŒŒ์‹ฑ์„ ์ง„ํ–‰ํ–ˆ๋Š”๋ฐ ์ด ๋ถ€๋ถ„์„ ๋”ฐ๋กœ OrderGenerator๋ผ๋Š” ๊ฐ์ฒด๋กœ ๋ถ„๋ฆฌํ•˜๋‹ˆ๊นŒ ๋” ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š”:+1:
@@ -0,0 +1,42 @@ +package christmas.model; + +import christmas.config.Event; +import christmas.util.Util; + +import java.util.EnumMap; +import java.util.Map; +import java.util.stream.Collectors; + +public class Promotion { + private final Map<Event, Integer> promotion; + + public Promotion() { + promotion = new EnumMap<>(Event.class); + } + + public Map<String, Integer> getPromotionList() { + return promotion.entrySet() + .stream() + .filter(event -> event.getValue() > 0) + .collect(Collectors.toMap( + event -> event.getKey().getName(), + Map.Entry::getValue) + ); + } + + public int getTotalDiscount() { + return Util.intStreamSum(promotion.values() + .stream()); + } + + public int getDiscount() { + return Util.intStreamSum(promotion.entrySet() + .stream() + .filter(event -> event.getKey() != Event.GIFT) + .map(Map.Entry::getValue)); + } + + public void setDiscountInfo(Map<Event, Integer> discountInfo) { + promotion.putAll(discountInfo); + } +}
Java
Promotion์ด๋ผ๋Š” ํด๋ž˜์Šค๋ช…์€ ํด๋ž˜์Šค๋ช…๋งŒ ๋ณด๊ณ  ์–ด๋–ค ๊ธฐ๋Šฅ์„ ํ•˜๋Š”์ง€ ์ถ”์ธกํ•˜๊ธฐ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,42 @@ +package christmas.model; + +import christmas.config.Event; +import christmas.util.Util; + +import java.util.EnumMap; +import java.util.Map; +import java.util.stream.Collectors; + +public class Promotion { + private final Map<Event, Integer> promotion; + + public Promotion() { + promotion = new EnumMap<>(Event.class); + } + + public Map<String, Integer> getPromotionList() { + return promotion.entrySet() + .stream() + .filter(event -> event.getValue() > 0) + .collect(Collectors.toMap( + event -> event.getKey().getName(), + Map.Entry::getValue) + ); + } + + public int getTotalDiscount() { + return Util.intStreamSum(promotion.values() + .stream()); + } + + public int getDiscount() { + return Util.intStreamSum(promotion.entrySet() + .stream() + .filter(event -> event.getKey() != Event.GIFT) + .map(Map.Entry::getValue)); + } + + public void setDiscountInfo(Map<Event, Integer> discountInfo) { + promotion.putAll(discountInfo); + } +}
Java
์ด ํ˜œํƒ ๊ธˆ์•ก์„ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ์ธ๋ฐ TotalDiscount๋Š” ์ด ํ• ์ธ ๊ธˆ์•ก์„ ๊ตฌํ•˜๋Š” ์˜๋ฏธ๊ฐ€ ๋” ๊ฐ•ํ•œ ๊ฒƒ ๊ฐ™์•„์„œ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ getTotalBenefitPrice๋ผ๋˜๊ฐ€ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ฐ”๊พธ๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,42 @@ +package christmas.model; + +import christmas.config.Event; +import christmas.util.Util; + +import java.util.EnumMap; +import java.util.Map; +import java.util.stream.Collectors; + +public class Promotion { + private final Map<Event, Integer> promotion; + + public Promotion() { + promotion = new EnumMap<>(Event.class); + } + + public Map<String, Integer> getPromotionList() { + return promotion.entrySet() + .stream() + .filter(event -> event.getValue() > 0) + .collect(Collectors.toMap( + event -> event.getKey().getName(), + Map.Entry::getValue) + ); + } + + public int getTotalDiscount() { + return Util.intStreamSum(promotion.values() + .stream()); + } + + public int getDiscount() { + return Util.intStreamSum(promotion.entrySet() + .stream() + .filter(event -> event.getKey() != Event.GIFT) + .map(Map.Entry::getValue)); + } + + public void setDiscountInfo(Map<Event, Integer> discountInfo) { + promotion.putAll(discountInfo); + } +}
Java
setter๋ฅผ ์ด์šฉํ•ด์„œ Promotion ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง„ ์ƒํƒœ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๊ฒƒ์€ ๋ณ„๋กœ ๊ฐ์ฒด ์ง€ํ–ฅ์ ์ธ ์„ค๊ณ„๊ฐ€ ์•„๋‹Œ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,49 @@ +package christmas.model; + +import christmas.config.MenuType; + +public class PromotionGenerator { + public static final int DISCOUNT_LIMIT_PRICE = 10000; + + private final Promotion promotion; + + public PromotionGenerator(final VisitDate visitDate, final Order order) { + this.promotion = new Promotion(); + + if (isDiscountTarget(order.getTotalPrice())) { + christmasDDayEvent(visitDate.getVisitDate()); + weekdayEvent(visitDate.getWeekDay(), order.getMenuQuantity(MenuType.DESSERT)); + weekendEvent(visitDate.getWeekDay(), order.getMenuQuantity(MenuType.MAIN)); + specialDayEvent(visitDate.getVisitDate()); + giftEvent(order.getTotalPrice()); + } + } + + private boolean isDiscountTarget(int totalPrice) { + return totalPrice >= DISCOUNT_LIMIT_PRICE; + } + + private void christmasDDayEvent(int visitDate) { + promotion.setDiscountInfo(new ChristmasDDayEvent(visitDate).getDiscountInfo()); + } + + private void weekdayEvent(int weekday, int dessertQuantity) { + promotion.setDiscountInfo(new WeekdayEvent(weekday, dessertQuantity).getDiscountInfo()); + } + + private void weekendEvent(int weekday, int mainQuantity) { + promotion.setDiscountInfo(new WeekendEvent(weekday, mainQuantity).getDiscountInfo()); + } + + private void specialDayEvent(int visitDate) { + promotion.setDiscountInfo(new SpecialDayEvent(visitDate).getDiscountInfo()); + } + + private void giftEvent(int totalPrice) { + promotion.setDiscountInfo(new GiftEvent(totalPrice).getDiscountInfo()); + } + + public Promotion getPromotion() { + return promotion; + } +}
Java
์ด๋ฒคํŠธ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋กœ Promotionํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ ๊ฐ™๊ตฐ์š”. ์ €๋Š” Promotion์„ ๊ตณ์ด ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์ด ํด๋ž˜์Šค์—์„œ ๊ด€๋ฆฌํ•˜๋Š”๊ฒŒ ๋” ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ์„ค๊ณ„ํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,49 @@ +package christmas.model; + +import christmas.config.MenuType; + +public class PromotionGenerator { + public static final int DISCOUNT_LIMIT_PRICE = 10000; + + private final Promotion promotion; + + public PromotionGenerator(final VisitDate visitDate, final Order order) { + this.promotion = new Promotion(); + + if (isDiscountTarget(order.getTotalPrice())) { + christmasDDayEvent(visitDate.getVisitDate()); + weekdayEvent(visitDate.getWeekDay(), order.getMenuQuantity(MenuType.DESSERT)); + weekendEvent(visitDate.getWeekDay(), order.getMenuQuantity(MenuType.MAIN)); + specialDayEvent(visitDate.getVisitDate()); + giftEvent(order.getTotalPrice()); + } + } + + private boolean isDiscountTarget(int totalPrice) { + return totalPrice >= DISCOUNT_LIMIT_PRICE; + } + + private void christmasDDayEvent(int visitDate) { + promotion.setDiscountInfo(new ChristmasDDayEvent(visitDate).getDiscountInfo()); + } + + private void weekdayEvent(int weekday, int dessertQuantity) { + promotion.setDiscountInfo(new WeekdayEvent(weekday, dessertQuantity).getDiscountInfo()); + } + + private void weekendEvent(int weekday, int mainQuantity) { + promotion.setDiscountInfo(new WeekendEvent(weekday, mainQuantity).getDiscountInfo()); + } + + private void specialDayEvent(int visitDate) { + promotion.setDiscountInfo(new SpecialDayEvent(visitDate).getDiscountInfo()); + } + + private void giftEvent(int totalPrice) { + promotion.setDiscountInfo(new GiftEvent(totalPrice).getDiscountInfo()); + } + + public Promotion getPromotion() { + return promotion; + } +}
Java
26๋ฒˆ ๋ผ์ธ ๋ถ€ํ„ฐ 44๋ฒˆ ๋ผ์ธ์€ ๊ฐ๊ฐ์˜ ์ด๋ฒคํŠธ๋ฅผ ์ ์šฉํ•˜๊ณ  ํ˜œํƒ ๊ธˆ์•ก์„ promotion๊ฐ์ฒด์— set ํ•ด์ฃผ๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ์ด๋ถ€๋ถ„์„ ์กฐ๊ธˆ ๋” ๊ฐ„๋žตํ™” ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”?
@@ -0,0 +1,49 @@ +package christmas.controller; + +import christmas.config.Constant; +import christmas.config.EventBadge; +import christmas.config.Gift; +import christmas.model.Order; +import christmas.model.Promotion; +import christmas.model.PromotionGenerator; +import christmas.model.VisitDate; +import christmas.view.OutputView; + +public class PromotionController { + private final VisitDate visitDate; + private final Order order; + private final Promotion promotion; + + public PromotionController() { + visitDate = InputController.setVisitDate(); + order = InputController.setOrder(); + promotion = new PromotionGenerator(visitDate, order).getPromotion(); + } + + public void active() { + print(); + } + + private int totalPrice() { + return order.getTotalPrice(); + } + + private int totalDiscount() { + return promotion.getTotalDiscount(); + } + + private boolean isEmptyPromotion() { + return promotion.getTotalDiscount() == 0; + } + + private void print() { + OutputView.printVisitDate(visitDate.getVisitDate()); + OutputView.printOrder(order.getOrderList()); + OutputView.printTotalPrice(totalPrice()); + OutputView.printGift(Gift.getGift(totalPrice())); + OutputView.printPromotion(isEmptyPromotion(), promotion.getPromotionList()); + OutputView.printTotalDiscount(totalDiscount() * Constant.MINUS); + OutputView.printPaymentPrice(totalPrice() - promotion.getDiscount()); + OutputView.printBadge(EventBadge.findBadgeByTotalDiscount(totalDiscount())); + } +}
Java
์ผ๋ฐ˜ ์ ์œผ๋กœ Controller์—์„œ๋Š” ๋™์ž‘๋งŒ ์ •์˜ํ•˜์ง€ ์ƒํƒœ๋Š” ์ •์˜ํ•˜์ง€ ์•Š๋Š” ๊ฑธ๋กœ ์•Œ๊ณ  ์žˆ๋Š”๋ฐ, PromotionController๋Š” Controller๋ณด๋‹ค๋Š” ๋„๋ฉ”์ธ์Šค๋Ÿฌ์šด(?)๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,26 @@ +package christmas.config; + +import java.util.Arrays; + +public enum EventBadge { + SANTA("์‚ฐํƒ€", 20000), + TREE("ํŠธ๋ฆฌ", 10000), + STAR("๋ณ„", 5000), + NONE("์—†์Œ", 0); + + private final String name; + private final int limitPrice; + + EventBadge(String name, int limitPrice) { + this.name = name; + this.limitPrice = limitPrice; + } + + public static String findBadgeByTotalDiscount(int totalDiscount) { + return Arrays.stream(values()) + .filter(badge -> totalDiscount >= badge.limitPrice) + .findFirst() + .orElse(NONE) + .name; + } +}
Java
๋ฐ๊ตด์ด๋‹˜ ์ฝ”๋“œ ๋ณด๊ณ  ์•Œ๊ฒŒ๋˜์—ˆ์ง€๋งŒ Predicate ๋ฅผ ์ด์šฉํ•ด์„œ ์กฐ๊ฑด์„ ๊ฑธ์–ด์ฃผ๋ฉด ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•œ ๊ณต๋ถ€๋ฅผ ํ•ด์•ผ๊ฒ ๋„ค์š”!
@@ -0,0 +1,25 @@ +package christmas.config; + +public class Message { + public static final String INPUT_DATE = "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"; + public static final String INPUT_ORDER = "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)"; + + public static final String OUTPUT_VISIT_DATE = "12์›” %d์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n"; + public static final String OUTPUT_MENU_TITLE = "\n<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"; + public static final String OUTPUT_ITEM = "%s %d๊ฐœ%n"; + public static final String OUTPUT_WON = "%s์›%n"; + public static final String OUTPUT_MINUS_WON = "-" + OUTPUT_WON; + public static final String OUTPUT_TOTAL_PRICE = "%n<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>%n" + OUTPUT_WON; + public static final String OUTPUT_GIFT_TITLE = "\n<์ฆ์ • ๋ฉ”๋‰ด>"; + public static final String OUTPUT_PROMOTION_TITLE = "\n<ํ˜œํƒ ๋‚ด์—ญ>"; + public static final String OUTPUT_PROMOTION_ITEM = "%s: " + OUTPUT_MINUS_WON; + public static final String OUTPUT_TOTAL_DISCOUNT = "%n<์ดํ˜œํƒ ๊ธˆ์•ก>%n" + OUTPUT_WON; + public static final String OUTPUT_PAYMENT_PRICE = "%n<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>%n" + OUTPUT_WON; + public static final String OUTPUT_BADGE = "%n<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>%n%s%n"; + public static final String OUTPUT_NONE = "์—†์Œ"; + + + public static final String ERROR_MESSAGE_PREFIX = "[ERROR]"; + public static final String ERROR_INPUT_DATE = ERROR_MESSAGE_PREFIX + " ์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + public static final String ERROR_INPUT_ORDER = ERROR_MESSAGE_PREFIX + " ์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; +}
Java
์ถœ๋ ฅ๋ถ€๋ถ„์˜ ๋ฉ”์‹œ์ง€ ์ƒ์ˆ˜์˜ ๊ฒฝ์šฐ์—๋Š” ๊ฐœ๋ฐœํ•˜๋ฉด์„œ ํ•ญ์ƒ ๊ณ ๋ฏผํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ํ•œ ๊ตฐ๋ฐ ๋ชจ์•„ ๊ฐ€๋…์„ฑ์„ ๋†’์ผ์ง€, ๋ถ„๋ฆฌํ•ด์„œ ๋ฉ”์‹œ์ง€๋ฅผ ์กฐ๊ธˆ๋„ ์œ ์—ฐํ•˜๊ฒŒ ์ˆ˜์ • ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ• ์ง€ ๊ณ ๋ฏผํ•˜์ง€๋งŒ ๋Œ€๋ถ€๋ถ„ ์ถœ๋ ฅ์—์„œ๋Š” ์ •ํ•ด์ง„ ๊ทœ๊ฒฉ์˜ ์ •์ œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด์ค€๋‹ค๊ณ  ์ƒ๊ฐํ•˜์—ฌ ๋ณ€๊ฒฝ๊ฐ€๋Šฅ์„ฑ์ด ํฐ ๋ฉ”์‹œ์ง€ ์ž์ฒด๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ์ชฝ์„ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ €๋„ ๋ถ„๋ฆฌํ•˜๋Š”๋ฐ ๋™์˜ํ•ฉ๋‹ˆ๋‹ค! ์ฒ˜์Œ์—๋Š” ์—๋Ÿฌ๋ฉ”์‹œ์ง€๋ฅผ ๋‹ค๋ฃจ๋Š” ํด๋ž˜์Šค๋ฅผ ๋ณ„๋„๋กœ ๋‘˜๊นŒ ํ–ˆ๋Š”๋ฐ ์ƒ๊ฐ๋ณด๋‹ค ์ ์€ ์—๋Ÿฌ๋ฉ”์‹œ์ง€ ์‚ฌ์šฉ์œผ๋กœ ์ด๋ฒˆ์—๋Š” Message ํด๋ž˜์Šค ๋‚ด์— ํ•ฉ์ณ์„œ ์‚ฌ์šฉ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,25 @@ +package christmas.controller; + +import christmas.model.Order; +import christmas.model.VisitDate; +import christmas.view.InputView; + +public class InputController { + public static VisitDate setVisitDate() { + try { + return new VisitDate(InputView.readDate()); + } catch (IllegalArgumentException illegalArgumentException) { + System.out.println(illegalArgumentException.getMessage()); + return setVisitDate(); + } + } + + public static Order setOrder() { + try { + return new Order(InputView.readOrder()); + } catch (IllegalArgumentException illegalArgumentException) { + System.out.println(illegalArgumentException.getMessage()); + return setOrder(); + } + } +}
Java
ํ•จ์ˆ˜๋ช… ์ง“๊ธฐ์— ๊ด€๋ จํ•ด์„œ๋Š” ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ ๋งŽ์ด ๋ถ€์กฑํ–ˆ๋„ค์š”...
@@ -0,0 +1,49 @@ +package christmas.model; + +import christmas.config.MenuType; + +public class PromotionGenerator { + public static final int DISCOUNT_LIMIT_PRICE = 10000; + + private final Promotion promotion; + + public PromotionGenerator(final VisitDate visitDate, final Order order) { + this.promotion = new Promotion(); + + if (isDiscountTarget(order.getTotalPrice())) { + christmasDDayEvent(visitDate.getVisitDate()); + weekdayEvent(visitDate.getWeekDay(), order.getMenuQuantity(MenuType.DESSERT)); + weekendEvent(visitDate.getWeekDay(), order.getMenuQuantity(MenuType.MAIN)); + specialDayEvent(visitDate.getVisitDate()); + giftEvent(order.getTotalPrice()); + } + } + + private boolean isDiscountTarget(int totalPrice) { + return totalPrice >= DISCOUNT_LIMIT_PRICE; + } + + private void christmasDDayEvent(int visitDate) { + promotion.setDiscountInfo(new ChristmasDDayEvent(visitDate).getDiscountInfo()); + } + + private void weekdayEvent(int weekday, int dessertQuantity) { + promotion.setDiscountInfo(new WeekdayEvent(weekday, dessertQuantity).getDiscountInfo()); + } + + private void weekendEvent(int weekday, int mainQuantity) { + promotion.setDiscountInfo(new WeekendEvent(weekday, mainQuantity).getDiscountInfo()); + } + + private void specialDayEvent(int visitDate) { + promotion.setDiscountInfo(new SpecialDayEvent(visitDate).getDiscountInfo()); + } + + private void giftEvent(int totalPrice) { + promotion.setDiscountInfo(new GiftEvent(totalPrice).getDiscountInfo()); + } + + public Promotion getPromotion() { + return promotion; + } +}
Java
์ฒ˜์Œ ์„ค๊ณ„์—์„œ๋Š” Promotionํด๋ž˜์Šค์—์„œ ์ด๋ฒคํŠธ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•˜๊ณ  ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๋Š” ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ํ•„์š” ์ด์ƒ์œผ๋กœ Promotionํด๋ž˜์Šค๊ฐ€ ๋น„๋Œ€ํ•ด์กŒ๋‹ค๊ณ  ์ƒ๊ฐ์„ ํ•ด์„œ ์ด๋ฅผ ์ด๋ฒคํŠธ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•˜๋Š” PromotionGeneratorํด๋ž˜์Šค์™€ ์ด๋ฒคํŠธ ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” Promotion ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ Promotion์˜ ์ƒํƒœ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” setter๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ ์˜คํžˆ๋ ค ๊ฐ์ฒด ์ง€ํ–ฅ์ ์ด์ง€ ๋ชปํ•œ ์„ค๊ณ„๊ฐ€ ๋˜์—ˆ๋„ค์š”ใ… ใ… 
@@ -0,0 +1,49 @@ +package christmas.controller; + +import christmas.config.Constant; +import christmas.config.EventBadge; +import christmas.config.Gift; +import christmas.model.Order; +import christmas.model.Promotion; +import christmas.model.PromotionGenerator; +import christmas.model.VisitDate; +import christmas.view.OutputView; + +public class PromotionController { + private final VisitDate visitDate; + private final Order order; + private final Promotion promotion; + + public PromotionController() { + visitDate = InputController.setVisitDate(); + order = InputController.setOrder(); + promotion = new PromotionGenerator(visitDate, order).getPromotion(); + } + + public void active() { + print(); + } + + private int totalPrice() { + return order.getTotalPrice(); + } + + private int totalDiscount() { + return promotion.getTotalDiscount(); + } + + private boolean isEmptyPromotion() { + return promotion.getTotalDiscount() == 0; + } + + private void print() { + OutputView.printVisitDate(visitDate.getVisitDate()); + OutputView.printOrder(order.getOrderList()); + OutputView.printTotalPrice(totalPrice()); + OutputView.printGift(Gift.getGift(totalPrice())); + OutputView.printPromotion(isEmptyPromotion(), promotion.getPromotionList()); + OutputView.printTotalDiscount(totalDiscount() * Constant.MINUS); + OutputView.printPaymentPrice(totalPrice() - promotion.getDiscount()); + OutputView.printBadge(EventBadge.findBadgeByTotalDiscount(totalDiscount())); + } +}
Java
MVCํŒจํ„ด์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ํ”„๋ฆฌ์ฝ”์Šค๋ฅผ ์‹œ์ž‘ํ•˜๋ฉด์„œ ์ ‘ํ–ˆ๋Š”๋ฐ ์–„ํŒํ•œ ์ง€์‹์ด ์ž˜๋ชป๋œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์—ˆ๋„ค์š” ใ…Žใ…Ž;; ๋‹ค์‹œ ๊ณต๋ถ€ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -5,13 +5,15 @@ import com.somemore.domains.review.dto.response.ReviewDetailResponseDto; import com.somemore.domains.review.dto.response.ReviewDetailWithNicknameResponseDto; import com.somemore.domains.review.usecase.ReviewQueryUseCase; +import com.somemore.global.auth.annotation.RoleId; import com.somemore.global.common.response.ApiResponse; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; +import org.springframework.security.access.annotation.Secured; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -41,7 +43,7 @@ public ApiResponse<ReviewDetailResponseDto> getById(@PathVariable Long id) { ); } - @Operation(summary = "๊ธฐ๊ด€๋ณ„ ๋ฆฌ๋ทฐ ์กฐํšŒ", description = "๊ธฐ๊ด€ ID๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ๋ทฐ ์กฐํšŒ") + @Operation(summary = "ํŠน์ • ๊ธฐ๊ด€ ๋ฆฌ๋ทฐ ์กฐํšŒ", description = "๊ธฐ๊ด€ ID๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ๋ทฐ ์กฐํšŒ") @GetMapping("/reviews/center/{centerId}") public ApiResponse<Page<ReviewDetailWithNicknameResponseDto>> getReviewsByCenterId( @PathVariable UUID centerId, @@ -53,14 +55,34 @@ public ApiResponse<Page<ReviewDetailWithNicknameResponseDto>> getReviewsByCenter .pageable(pageable) .build(); + return ApiResponse.ok( + 200, + reviewQueryUseCase.getDetailsWithNicknameByCenterId(centerId, condition), + "ํŠน์ • ๊ธฐ๊ด€ ๋ฆฌ๋ทฐ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ ์„ฑ๊ณต" + ); + } + + @Secured("ROLE_CENTER") + @Operation(summary = "๊ธฐ๊ด€ ๋ฆฌ๋ทฐ ์กฐํšŒ", description = "๊ธฐ๊ด€ ์ž์‹ ์˜ ๋ฆฌ๋ทฐ ์กฐํšŒ") + @GetMapping("/reviews/center/me") + public ApiResponse<Page<ReviewDetailWithNicknameResponseDto>> getMyCenterReviews( + @RoleId UUID centerId, + @PageableDefault(sort = "created_at", direction = DESC) Pageable pageable, + @RequestParam(required = false) VolunteerCategory category + ) { + ReviewSearchCondition condition = ReviewSearchCondition.builder() + .category(category) + .pageable(pageable) + .build(); + return ApiResponse.ok( 200, reviewQueryUseCase.getDetailsWithNicknameByCenterId(centerId, condition), "๊ธฐ๊ด€ ๋ฆฌ๋ทฐ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ ์„ฑ๊ณต" ); } - @Operation(summary = "๋ด‰์‚ฌ์ž ๋ฆฌ๋ทฐ ์กฐํšŒ", description = "๋ด‰์‚ฌ์ž ID๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ๋ทฐ ์กฐํšŒ") + @Operation(summary = "ํŠน์ • ๋ด‰์‚ฌ์ž ๋ฆฌ๋ทฐ ์กฐํšŒ", description = "๋ด‰์‚ฌ์ž ID๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ๋ทฐ ์กฐํšŒ") @GetMapping("/reviews/volunteer/{volunteerId}") public ApiResponse<Page<ReviewDetailWithNicknameResponseDto>> getReviewsByVolunteerId( @PathVariable UUID volunteerId, @@ -75,7 +97,27 @@ public ApiResponse<Page<ReviewDetailWithNicknameResponseDto>> getReviewsByVolunt return ApiResponse.ok( 200, reviewQueryUseCase.getDetailsWithNicknameByVolunteerId(volunteerId, condition), - "์œ ์ € ๋ฆฌ๋ทฐ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ ์„ฑ๊ณต" + "ํŠน์ • ๋ด‰์‚ฌ์ž ๋ฆฌ๋ทฐ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ ์„ฑ๊ณต" + ); + } + + @Secured("ROLE_VOLUNTEER") + @Operation(summary = "๋ด‰์‚ฌ์ž ๋ฆฌ๋ทฐ ์กฐํšŒ", description = "๋ด‰์‚ฌ์ž ์ž์‹ ์˜ ๋ฆฌ๋ทฐ ์กฐํšŒ") + @GetMapping("/reviews/volunteer/me") + public ApiResponse<Page<ReviewDetailWithNicknameResponseDto>> getMyVolunteerReviews( + @RoleId UUID volunteerId, + @PageableDefault(sort = "created_at", direction = DESC) Pageable pageable, + @RequestParam(required = false) VolunteerCategory category + ) { + ReviewSearchCondition condition = ReviewSearchCondition.builder() + .category(category) + .pageable(pageable) + .build(); + + return ApiResponse.ok( + 200, + reviewQueryUseCase.getDetailsWithNicknameByVolunteerId(volunteerId, condition), + "๋ด‰์‚ฌ์ž ๋ฆฌ๋ทฐ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ ์„ฑ๊ณต" ); }
Java
๋นŒ๋” ๋กœ์ง์„ ์ปจ๋””์…˜ ํด๋ž˜์Šค์˜ of ์ •์  ๋ฉ”์„œ๋“œ์—์„œ ์ง„ํ–‰ํ•˜๋ฉด ๋” ๊น”๋”ํ•  ๊ฒƒ ๊ฐ™์•„์š”~ ๊ตณ์ด ์ˆ˜์ •ํ•  ํ•„์š”๋Š” ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค!
@@ -1,35 +1,34 @@ package com.somemore.domains.review.service; -import com.somemore.domains.center.usecase.query.CenterQueryUseCase; +import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_REVIEW; + import com.somemore.domains.review.domain.Review; import com.somemore.domains.review.dto.condition.ReviewSearchCondition; import com.somemore.domains.review.dto.response.ReviewDetailResponseDto; import com.somemore.domains.review.dto.response.ReviewDetailWithNicknameResponseDto; import com.somemore.domains.review.repository.ReviewRepository; import com.somemore.domains.review.usecase.ReviewQueryUseCase; -import com.somemore.domains.volunteer.domain.Volunteer; -import com.somemore.domains.volunteer.usecase.VolunteerQueryUseCase; +import com.somemore.domains.volunteerapply.usecase.VolunteerApplyQueryUseCase; import com.somemore.global.exception.NoSuchElementException; +import com.somemore.volunteer.repository.record.VolunteerNicknameAndId; +import com.somemore.volunteer.usecase.NEWVolunteerQueryUseCase; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; - -import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_REVIEW; - @RequiredArgsConstructor @Transactional(readOnly = true) @Service public class ReviewQueryService implements ReviewQueryUseCase { private final ReviewRepository reviewRepository; - private final VolunteerQueryUseCase volunteerQueryUseCase; - private final CenterQueryUseCase centerQueryUseCase; + private final NEWVolunteerQueryUseCase volunteerQueryUseCase; + private final VolunteerApplyQueryUseCase volunteerApplyQueryUseCase; @Override public boolean existsByVolunteerApplyId(Long volunteerApplyId) { @@ -45,7 +44,9 @@ public Review getById(Long id) { @Override public ReviewDetailResponseDto getDetailById(Long id) { Review review = getById(id); - return ReviewDetailResponseDto.from(review); + Long recruitBoardId = volunteerApplyQueryUseCase.getRecruitBoardIdById( + review.getVolunteerApplyId()); + return ReviewDetailResponseDto.of(review, recruitBoardId); } @Override @@ -54,10 +55,11 @@ public Page<ReviewDetailWithNicknameResponseDto> getDetailsWithNicknameByVolunte ReviewSearchCondition condition ) { String nickname = volunteerQueryUseCase.getNicknameById(volunteerId); - Page<Review> reviews = reviewRepository.findAllByVolunteerIdAndSearch(volunteerId, condition); + Page<Review> reviews = reviewRepository.findAllByVolunteerIdAndSearch(volunteerId, + condition); return reviews.map( - review -> ReviewDetailWithNicknameResponseDto.from(review, nickname) + review -> ReviewDetailWithNicknameResponseDto.of(review, nickname) ); } @@ -66,28 +68,20 @@ public Page<ReviewDetailWithNicknameResponseDto> getDetailsWithNicknameByCenterI UUID centerId, ReviewSearchCondition condition ) { - centerQueryUseCase.validateCenterExists(centerId); - Page<Review> reviews = reviewRepository.findAllByCenterIdAndSearch(centerId, condition); List<UUID> volunteerIds = reviews.get().map(Review::getVolunteerId).toList(); - Map<UUID, String> volunteerNicknames = getVolunteerNicknames(volunteerIds); + Map<UUID, String> volunteerNicknames = mapVolunteerIdsToNicknames(volunteerIds); - return reviews.map( - review -> { - String nickname = volunteerNicknames.getOrDefault(review.getVolunteerId(), - "์‚ญ์ œ๋œ ์•„์ด๋””"); - return ReviewDetailWithNicknameResponseDto.from(review, nickname); - }); + return reviews.map(review -> + ReviewDetailWithNicknameResponseDto.of(review, + volunteerNicknames.get(review.getVolunteerId())) + ); } - private Map<UUID, String> getVolunteerNicknames(List<UUID> volunteerIds) { - List<Volunteer> volunteers = volunteerQueryUseCase.getAllByIds(volunteerIds); - - Map<UUID, String> volunteerNicknames = new HashMap<>(); - for (Volunteer volunteer : volunteers) { - volunteerNicknames.put(volunteer.getId(), volunteer.getNickname()); - } - - return volunteerNicknames; + private Map<UUID, String> mapVolunteerIdsToNicknames(List<UUID> volunteerIds) { + return volunteerQueryUseCase.getVolunteerNicknameAndIdsByIds(volunteerIds) + .stream() + .collect(Collectors.toMap(VolunteerNicknameAndId::id, + VolunteerNicknameAndId::nickname)); } }
Java
๋ฉ”์„œ๋“œ๋กœ ์ฝ”๋“œ๋ฅผ ํฌ์žฅํ•˜๋ฉด ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”~
@@ -1,35 +1,34 @@ package com.somemore.domains.review.service; -import com.somemore.domains.center.usecase.query.CenterQueryUseCase; +import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_REVIEW; + import com.somemore.domains.review.domain.Review; import com.somemore.domains.review.dto.condition.ReviewSearchCondition; import com.somemore.domains.review.dto.response.ReviewDetailResponseDto; import com.somemore.domains.review.dto.response.ReviewDetailWithNicknameResponseDto; import com.somemore.domains.review.repository.ReviewRepository; import com.somemore.domains.review.usecase.ReviewQueryUseCase; -import com.somemore.domains.volunteer.domain.Volunteer; -import com.somemore.domains.volunteer.usecase.VolunteerQueryUseCase; +import com.somemore.domains.volunteerapply.usecase.VolunteerApplyQueryUseCase; import com.somemore.global.exception.NoSuchElementException; +import com.somemore.volunteer.repository.record.VolunteerNicknameAndId; +import com.somemore.volunteer.usecase.NEWVolunteerQueryUseCase; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; - -import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_REVIEW; - @RequiredArgsConstructor @Transactional(readOnly = true) @Service public class ReviewQueryService implements ReviewQueryUseCase { private final ReviewRepository reviewRepository; - private final VolunteerQueryUseCase volunteerQueryUseCase; - private final CenterQueryUseCase centerQueryUseCase; + private final NEWVolunteerQueryUseCase volunteerQueryUseCase; + private final VolunteerApplyQueryUseCase volunteerApplyQueryUseCase; @Override public boolean existsByVolunteerApplyId(Long volunteerApplyId) { @@ -45,7 +44,9 @@ public Review getById(Long id) { @Override public ReviewDetailResponseDto getDetailById(Long id) { Review review = getById(id); - return ReviewDetailResponseDto.from(review); + Long recruitBoardId = volunteerApplyQueryUseCase.getRecruitBoardIdById( + review.getVolunteerApplyId()); + return ReviewDetailResponseDto.of(review, recruitBoardId); } @Override @@ -54,10 +55,11 @@ public Page<ReviewDetailWithNicknameResponseDto> getDetailsWithNicknameByVolunte ReviewSearchCondition condition ) { String nickname = volunteerQueryUseCase.getNicknameById(volunteerId); - Page<Review> reviews = reviewRepository.findAllByVolunteerIdAndSearch(volunteerId, condition); + Page<Review> reviews = reviewRepository.findAllByVolunteerIdAndSearch(volunteerId, + condition); return reviews.map( - review -> ReviewDetailWithNicknameResponseDto.from(review, nickname) + review -> ReviewDetailWithNicknameResponseDto.of(review, nickname) ); } @@ -66,28 +68,20 @@ public Page<ReviewDetailWithNicknameResponseDto> getDetailsWithNicknameByCenterI UUID centerId, ReviewSearchCondition condition ) { - centerQueryUseCase.validateCenterExists(centerId); - Page<Review> reviews = reviewRepository.findAllByCenterIdAndSearch(centerId, condition); List<UUID> volunteerIds = reviews.get().map(Review::getVolunteerId).toList(); - Map<UUID, String> volunteerNicknames = getVolunteerNicknames(volunteerIds); + Map<UUID, String> volunteerNicknames = mapVolunteerIdsToNicknames(volunteerIds); - return reviews.map( - review -> { - String nickname = volunteerNicknames.getOrDefault(review.getVolunteerId(), - "์‚ญ์ œ๋œ ์•„์ด๋””"); - return ReviewDetailWithNicknameResponseDto.from(review, nickname); - }); + return reviews.map(review -> + ReviewDetailWithNicknameResponseDto.of(review, + volunteerNicknames.get(review.getVolunteerId())) + ); } - private Map<UUID, String> getVolunteerNicknames(List<UUID> volunteerIds) { - List<Volunteer> volunteers = volunteerQueryUseCase.getAllByIds(volunteerIds); - - Map<UUID, String> volunteerNicknames = new HashMap<>(); - for (Volunteer volunteer : volunteers) { - volunteerNicknames.put(volunteer.getId(), volunteer.getNickname()); - } - - return volunteerNicknames; + private Map<UUID, String> mapVolunteerIdsToNicknames(List<UUID> volunteerIds) { + return volunteerQueryUseCase.getVolunteerNicknameAndIdsByIds(volunteerIds) + .stream() + .collect(Collectors.toMap(VolunteerNicknameAndId::id, + VolunteerNicknameAndId::nickname)); } }
Java
๋ณต์ˆ˜ํ˜• ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ
@@ -0,0 +1,27 @@ +package christmas.domain.policy.discount; + +import christmas.domain.Orders; +import christmas.domain.VisitDate; + +public class DdayDiscountPolicy implements DiscountPolicy { + public static final int CHRISTMAS_DATE = 25; + public static final int DISCOUNT_AMOUNT = 1000; + public static final int ADDITIONAL_DISCOUNT_AMOUNT = 100; + public static final int NOT_DISCOUNT_AMOUNT = 0; + + @Override + public int discount(VisitDate visitDate, Orders orders) { + if (isNotAfter(visitDate)) { + return calculateDiscount(visitDate); + } + return NOT_DISCOUNT_AMOUNT; + } + + private boolean isNotAfter(VisitDate visitDate) { + return visitDate.isNotAfter(CHRISTMAS_DATE); + } + + private int calculateDiscount(VisitDate visitDate) { + return DISCOUNT_AMOUNT + visitDate.calculateDiscount(ADDITIONAL_DISCOUNT_AMOUNT); + } +}
Java
DdayDiscountPolicy์˜ discount ๋ฉ”์†Œ๋“œ๊ฐ€ ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ๋„ ์žˆ๋Š”๋ฐ ๋งŒ์› ์ดํ•˜๋ฉด ์ ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์กฐ๊ฑด์— ์•ˆ์ „ํ•˜์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. DiscountPolicy๋ฅผ ์ถ”์ƒํด๋ž˜์Šค๋กœ ํ•˜์—ฌ ๋งŒ์› ์ดํ•˜ ์ ์šฉ ๋ถˆ๊ฐ€ ์กฐ๊ฑด์„ ๊ฑธ๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,53 @@ +package christmas.domain; + +import christmas.dto.BenefitDto; +import christmas.dto.GiftsDto; +import java.util.function.Supplier; + +public class Bill { + public static final int MINIMUM_ORDERS_PRICE = 10000; + public static final int EVENT_NOT_APPLIED_AMOUNT = 0; + + private final Discounts discounts; + private final Gifts gifts; + + public Bill(Discounts discounts, Gifts gifts) { + this.discounts = discounts; + this.gifts = gifts; + } + + public int getTotalBenefit(Orders orders) { + return getAmount(this::sumBenefit, orders); + } + + public int getDiscountPrice(Orders orders) { + return orders.getTotalPrice() - discounts.sumDiscount(); + } + + public GiftsDto getGiftDto() { + return new GiftsDto(gifts.getResult()); + } + + public BenefitDto getBenefitDto() { + return new BenefitDto(discounts.getResult(), gifts.sumPrice()); + } + + public String getBadgeName() { + return Badge.getNameByBenefit(sumBenefit()); + } + + private int sumBenefit() { + return discounts.sumDiscount() + gifts.sumPrice(); + } + + private boolean isEventApplicable(Orders orders) { + return orders.getTotalPrice() >= MINIMUM_ORDERS_PRICE; + } + + private <T> int getAmount(Supplier<T> supplier, Orders orders) { + if (isEventApplicable(orders)) { + return (int) supplier.get(); + } + return EVENT_NOT_APPLIED_AMOUNT; + } +}
Java
domain์ด ํŠน์ • Dto์— ์ข…์†๋˜๋Š” ๊ฒƒ์— ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”? ์ €๋Š” Dto๋Š” ๋ณ€๋™์„ฑ์ด ํฐ ๊ฐ์ฒด๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ domain์ด ์˜์กดํ•˜๋Š” ํ˜•ํƒœ๋Š” ์ข‹์ง€ ์•Š๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,36 @@ +package christmas.domain; + +import christmas.exception.NotExistsBadgeException; +import java.util.Arrays; + +public enum Badge { + SANTA("์‚ฐํƒ€", 20000), + TREE("ํŠธ๋ฆฌ", 10000), + STAR("๋ณ„", 5000), + NONE("์—†์Œ", 0), + ; + + private final String name; + private final int minimumAmount; + + private Badge(String name, int minimumAmount) { + this.name = name; + this.minimumAmount = minimumAmount; + } + + public static String getNameByBenefit(int amount) { + return from(amount).name; + } + + public static Badge from(int amount) { + return Arrays.stream(Badge.values()) + .filter(badge -> badge.isGreaterThan(amount)) + .findFirst() + .orElseThrow(NotExistsBadgeException::new); + } + + private boolean isGreaterThan(int benefitAmount) { + return this.minimumAmount <= benefitAmount; + } +} +
Java
enum์˜ ์ˆœ์„œ๊ฐ€ ๊ณ ์ •๋˜์–ด์•ผ ์ •์ƒ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋“ฑ๊ธ‰์ด ์ถ”๊ฐ€๋˜๊ฑฐ๋‚˜ ํ–ˆ์„ ๋•Œ ์˜ํ–ฅ์ด ์—†๋„๋ก minimumAmount๋กœ ์ •๋ ฌ์„ ํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,18 @@ +package christmas.domain; + +public enum DiscountType { + CHRISTMAS_DDAY("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"), + WEEKDAY("ํ‰์ผ ํ• ์ธ"), + WEEKEND("์ฃผ๋ง ํ• ์ธ"), + SPECIAL("ํŠน๋ณ„ ํ• ์ธ"); + + private final String name; + + DiscountType(String name) { + this.name = name; + } + + public String getName() { + return name; + } +}
Java
์ด๋ฏธ Policies๋กœ ํด๋ž˜์Šค๋ณ„ ๊ตฌ๋ถ„์ด ๋˜์–ด ์žˆ๋Š”๋ฐ enum์œผ๋กœ ์ œ๋ชฉ๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ์„ ํ•„์š”๊ฐ€ ์žˆ์„๊นŒ์š”? ์ œ๋ชฉ์„ ๊ฐ Policy ๋‚ด๋ถ€๋กœ ๋„ฃ๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์„ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,53 @@ +package christmas.domain; + +import christmas.dto.BenefitDto; +import christmas.dto.GiftsDto; +import java.util.function.Supplier; + +public class Bill { + public static final int MINIMUM_ORDERS_PRICE = 10000; + public static final int EVENT_NOT_APPLIED_AMOUNT = 0; + + private final Discounts discounts; + private final Gifts gifts; + + public Bill(Discounts discounts, Gifts gifts) { + this.discounts = discounts; + this.gifts = gifts; + } + + public int getTotalBenefit(Orders orders) { + return getAmount(this::sumBenefit, orders); + } + + public int getDiscountPrice(Orders orders) { + return orders.getTotalPrice() - discounts.sumDiscount(); + } + + public GiftsDto getGiftDto() { + return new GiftsDto(gifts.getResult()); + } + + public BenefitDto getBenefitDto() { + return new BenefitDto(discounts.getResult(), gifts.sumPrice()); + } + + public String getBadgeName() { + return Badge.getNameByBenefit(sumBenefit()); + } + + private int sumBenefit() { + return discounts.sumDiscount() + gifts.sumPrice(); + } + + private boolean isEventApplicable(Orders orders) { + return orders.getTotalPrice() >= MINIMUM_ORDERS_PRICE; + } + + private <T> int getAmount(Supplier<T> supplier, Orders orders) { + if (isEventApplicable(orders)) { + return (int) supplier.get(); + } + return EVENT_NOT_APPLIED_AMOUNT; + } +}
Java
์ €๋„ ๋™๊ฐํ•ฉ๋‹ˆ๋‹ค ๐Ÿค” ์‚ฌ์‹ค ์ €๋„ ๋„๋ฉ”์ธ์— toDto ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ตฌํ˜„ํ•˜๊ธด ํ–ˆ์–ด์š” DTO์—์„œ๋“  Mapper๋ฅผ ํ†ตํ•ด์„œ๋“  ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ DTO๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด getter๋ฅผ ์—ด์–ด์•ผ ํ•˜๋Š”๋ฐ getter๋ฅผ ์ตœ๋Œ€ํ•œ ์ง€์–‘ํ•ด๋ณผ๋ ค๊ณ  ๊ทธ๋ ‡๊ฒŒ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ”„๋ฆฌ์ฝ”์Šค๋‚˜ ๋‹ค๋ฅธ ๊ธ€๋“ค ๋ณด๋‹ˆ ๋งŽ์€ ๋ถ„๋“ค์ด ์ค‘์š”ํ•œ ๊ฐ์ฒด๊ฐ€(๋„๋ฉ”์ธ) ๋œ ์ค‘์š”ํ•œ ๊ฐ์ฒด(DTO)๋ฅผ ์˜์กดํ•˜๋Š” ๊ฒƒ์ด ์ข‹์ง€ ์•Š๋‹ค๊ณ  ๋ง์”€ํ•˜์‹œ๋”๋ผ๊ณ ์š” ๊ทธ๋ž˜์„œ ์ €๋„ DTO์˜ ์ƒ์„ฑ๋ฐฉ์‹์— ๋Œ€ํ•ด์„œ ์ƒˆ๋กญ๊ฒŒ ๊ณ ๋ฏผํ•˜๊ฒŒ ๋˜์—ˆ๋„ค์š”
@@ -0,0 +1,53 @@ +package christmas.domain; + +import christmas.dto.BenefitDto; +import christmas.dto.GiftsDto; +import java.util.function.Supplier; + +public class Bill { + public static final int MINIMUM_ORDERS_PRICE = 10000; + public static final int EVENT_NOT_APPLIED_AMOUNT = 0; + + private final Discounts discounts; + private final Gifts gifts; + + public Bill(Discounts discounts, Gifts gifts) { + this.discounts = discounts; + this.gifts = gifts; + } + + public int getTotalBenefit(Orders orders) { + return getAmount(this::sumBenefit, orders); + } + + public int getDiscountPrice(Orders orders) { + return orders.getTotalPrice() - discounts.sumDiscount(); + } + + public GiftsDto getGiftDto() { + return new GiftsDto(gifts.getResult()); + } + + public BenefitDto getBenefitDto() { + return new BenefitDto(discounts.getResult(), gifts.sumPrice()); + } + + public String getBadgeName() { + return Badge.getNameByBenefit(sumBenefit()); + } + + private int sumBenefit() { + return discounts.sumDiscount() + gifts.sumPrice(); + } + + private boolean isEventApplicable(Orders orders) { + return orders.getTotalPrice() >= MINIMUM_ORDERS_PRICE; + } + + private <T> int getAmount(Supplier<T> supplier, Orders orders) { + if (isEventApplicable(orders)) { + return (int) supplier.get(); + } + return EVENT_NOT_APPLIED_AMOUNT; + } +}
Java
10000์› ์ด์ƒ์ด์—ฌ์•ผ๋งŒ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋˜๋Š” ๊ณตํ†ต ์กฐ๊ฑด์„ Bill์— ์œ„์น˜์‹œํ‚ค์…จ๋„ค์š”..! ํ•ด๋‹น ์กฐ๊ฑด์ด ์ด๋ฒคํŠธ๋ฅผ ์ ์šฉํ• ์ง€ ๋ง์ง€๋ฅผ ์ •ํ•œ๋‹ค๋Š” ์ธก๋ฉด์—์„œ Bill์— ์œ„์น˜ํ•˜๊ธฐ์—๋Š” ์ฑ…์ž„์ด ๋ฒ—์–ด๋‚œ ๋А๋‚Œ์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ ์ด๋ฒคํŠธ๋ฅผ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•˜์‹  ๊ฒƒ ๊ฐ™์€๋ฐ 10000์› ์ด์ƒ์ธ์ง€ ๊ฒ€์ฆํ•˜๋Š” ์ฝ”๋“œ์˜ ์œ„์น˜๋Š” DiscountPolicy๋“ค์— ์œ„์น˜์‹œํ‚ค์‹œ๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š” ๐Ÿค”
@@ -0,0 +1,53 @@ +package christmas.domain; + +import christmas.dto.BenefitDto; +import christmas.dto.GiftsDto; +import java.util.function.Supplier; + +public class Bill { + public static final int MINIMUM_ORDERS_PRICE = 10000; + public static final int EVENT_NOT_APPLIED_AMOUNT = 0; + + private final Discounts discounts; + private final Gifts gifts; + + public Bill(Discounts discounts, Gifts gifts) { + this.discounts = discounts; + this.gifts = gifts; + } + + public int getTotalBenefit(Orders orders) { + return getAmount(this::sumBenefit, orders); + } + + public int getDiscountPrice(Orders orders) { + return orders.getTotalPrice() - discounts.sumDiscount(); + } + + public GiftsDto getGiftDto() { + return new GiftsDto(gifts.getResult()); + } + + public BenefitDto getBenefitDto() { + return new BenefitDto(discounts.getResult(), gifts.sumPrice()); + } + + public String getBadgeName() { + return Badge.getNameByBenefit(sumBenefit()); + } + + private int sumBenefit() { + return discounts.sumDiscount() + gifts.sumPrice(); + } + + private boolean isEventApplicable(Orders orders) { + return orders.getTotalPrice() >= MINIMUM_ORDERS_PRICE; + } + + private <T> int getAmount(Supplier<T> supplier, Orders orders) { + if (isEventApplicable(orders)) { + return (int) supplier.get(); + } + return EVENT_NOT_APPLIED_AMOUNT; + } +}
Java
ํ• ์ธ๋œ ๊ฐ€๊ฒฉ์„ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ์˜ ์œ„์น˜๋Š” Orders๊ฐ€ ์ ์ ˆํ• ๊นŒ์š” Bill์ด ์ ์ ˆํ• ๊นŒ์š” ๐Ÿค” ์ €๋„ ์ด๊ฒƒ์„ ์ฐธ ๋งŽ์ด ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ์š” ..! ์ €๋Š” Order์— ์œ„์น˜์‹œํ‚ค๊ธด ํ–ˆ๋Š”๋ฐ ๊ด€๋ จํ•ด์„œ ํ•˜๋Š˜๋‹˜ ์ƒ๊ฐ๋„ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! ๋˜ Orders๋ผ๋Š” ๋„๋ฉ”์ธ์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๊ณ  ์žˆ๋Š”๋ฐ int๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋Š”์ง€๋„ ๊ถ๊ธˆํ•ด์š” ๊ด€๋ จํ•œ ๊ธฐ์ค€ ๊ณต์œ ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ™‡๐Ÿป
@@ -0,0 +1,25 @@ +package christmas.domain; + +import java.util.Map; + +public class Discount { + private final DiscountType type; + private final int amount; + + public Discount(DiscountType type, int amount) { + this.type = type; + this.amount = amount; + } + + public boolean isDiscount() { + return amount != 0; + } + + public int getAmount() { + return amount; + } + + public String getName() { + return type.getName(); + } +}
Java
ํ• ์ธ๋‚ด์—ญ์€ ํ• ์ธ ์ข…๋ฅ˜์™€ ํ• ์ธ ๊ธˆ์•ก์˜ ์–‘์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ตฐ์š” ๐Ÿ‘๐Ÿป ์ €๋„ ๋น„์Šทํ•˜๊ฒŒ ์ž‘์„ฑํ–ˆ์ง€๋งŒ ์ €๋Š” String ํƒ€์ž…์˜ description๊ณผ int amount๋กœ ์ž‘์„ฑํ–ˆ๋„ค์š”.. ์ด๋ ‡๊ฒŒ ๋ณด๋‹ˆ enum์œผ๋กœ ๊ด€๋ฆฌํ•˜์‹  ๊ฒƒ ์ •๋ง ์ข‹์•„๋ณด์ด๋„ค์š” ์„ธ์„ธํ•œ ์ฝ”๋“œ ์ž˜ ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค ๐Ÿ‘๐Ÿป ๐Ÿ‘๐Ÿป
@@ -0,0 +1,25 @@ +package christmas.domain; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Discounts { + private final List<Discount> discounts; + + public Discounts(List<Discount> discounts) { + this.discounts = discounts; + } + + public int sumDiscount() { + return discounts.stream() + .mapToInt(Discount::getAmount) + .sum(); + } + + public Map<String, Integer> getResult() { + return discounts.stream() + .filter(Discount::isDiscount) + .collect(Collectors.toUnmodifiableMap(Discount::getName, Discount::getAmount)); + } +}
Java
์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์„ ์ถ”์ถœํ•˜์‹œ๊ณ  ๊ด€๋ จ๋œ ๋กœ์ง๋„ ๊น”๋”ํ•˜๊ฒŒ ์ž˜ ๋ชจ์•„์ฃผ์‹  ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ‘๐Ÿป ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค ๐Ÿ˜„
@@ -0,0 +1,21 @@ +package christmas.domain; + +public enum GiftType { + CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000); + + private final String name; + private final int price; + + private GiftType(String name, int price) { + this.name = name; + this.price = price; + } + + public int calculatePrice(int quantity) { + return this.price * quantity; + } + + public String getName() { + return name; + } +}
Java
๋ฉ”๋‰ด์ด๊ธฐ๋„ ํ•˜๋ฉด์„œ ์ฆ์ •ํ’ˆ์ด๊ธฐ๋„ ํ•œ ์ƒดํŽ˜์ธ์€ GiftType์—๋„ ์œ„์น˜ํ•˜๊ณ  Menu์—๋„ ์œ„์น˜ํ•˜๋„ค์š” ๐Ÿค” ๋ณ€๊ฒฝ ์š”๊ตฌ์‚ฌํ•ญ์œผ๋กœ ์ƒดํŽ˜์ธ์˜ ํŒ๋งค๊ฐ€๊ฒฉ์ด ์ˆ˜์ •๋œ๋‹ค๋ฉด ํ•ด๋‹น ์œ„์น˜๋„ ์ˆ˜์ •๋˜์–ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์•„์„œ ์ˆ˜์ • ์ง€์ ์ด ํผ์ง€๋Š” ๋ถ€๋ถ„์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,57 @@ +package christmas.domain; + +import static christmas.domain.MenuType.APPETIZER; +import static christmas.domain.MenuType.DESSERT; +import static christmas.domain.MenuType.DRINK; +import static christmas.domain.MenuType.MAIN; + +import christmas.exception.InvalidOrderException; +import java.util.Arrays; + +public enum Menu { + MUSHROOM_SOUP("์–‘์†ก์ด์ˆ˜ํ”„", 6_000, APPETIZER), + TAPAS("ํƒ€ํŒŒ์Šค", 5_500, APPETIZER), + CAESAR_SALAD("์‹œ์ €์ƒ๋Ÿฌ๋“œ", 8_000, APPETIZER), + + T_BONE_STEAK("ํ‹ฐ๋ณธ์Šคํ…Œ์ดํฌ", 55_000, MAIN), + BBQ_RIBS("๋ฐ”๋น„ํ๋ฆฝ", 54_000, MAIN), + SEAFOOD_PASTA("ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€", 35_000, MAIN), + CHRISTMAS_PASTA("ํฌ๋ฆฌ์Šค๋งˆ์ŠคํŒŒ์Šคํƒ€", 25_000, MAIN), + + CHOCOLATE_CAKE("์ดˆ์ฝ”์ผ€์ดํฌ", 15_000, DESSERT), + ICE_CREAM("์•„์ด์Šคํฌ๋ฆผ", 5_000, DESSERT), + + ZERO_COLA("์ œ๋กœ์ฝœ๋ผ", 3_000, DRINK), + RED_WINE("๋ ˆ๋“œ์™€์ธ", 60_000, DRINK), + CHAMPAGNE("์ƒดํŽ˜์ธ", 25_000, DRINK); + + private final String name; + private final int price; + private final MenuType type; + + Menu(String menuName, int price, MenuType type) { + this.name = menuName; + this.price = price; + this.type = type; + } + + public static Menu from(String menuName) { + return Arrays.stream(Menu.values()) + .filter(menu -> menuName.equals(menu.name)) + .findFirst() + .orElseThrow(InvalidOrderException::new); + } + + public boolean isEqualsMenuType(MenuType menuType) { + return this.type == menuType; + } + + public int calculatePrice(int quantity) { + return this.price * quantity; + } + + public String getName() { + return name; + } +} +
Java
๊ฒŒํ„ฐ๋ฅผ ์ตœ๋Œ€ํ•œ ์ง€์–‘ํ•˜์…จ๋„ค์š”..! ์ €๋Š” ์ด๋ ‡๊ฒŒ ๊นŒ์ง€ ์ƒ๊ฐ ๋ชปํ•ด๋ณด๊ณ  enum์—๋Š” Getter๋ฅผ ์ „๋ถ€ ์—ด์—ˆ๋„ค์š” ๐Ÿ˜ญ ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค! ๐Ÿ‘๐Ÿป
@@ -0,0 +1,50 @@ +package christmas.domain; + +import static christmas.exception.ErrorMessages.INVALID_ORDER; + +import christmas.utils.IntegerConverter; +import christmas.exception.InvalidOrderException; + +public class Order { + public static final int MIN_ORDER_QUANTITY = 1; + + private final Menu menu; + private final int quantity; + + public Order(String menuName, String quantity) { + this.menu = Menu.from(menuName); + this.quantity = convertType(quantity); + + validateMinQuantity(); + } + + private int convertType(String quantity) { + return IntegerConverter.convert(quantity, INVALID_ORDER); + } + + private void validateMinQuantity() { + if (quantity < MIN_ORDER_QUANTITY) { + throw new InvalidOrderException(); + } + } + + public boolean isEqualsMenuType(MenuType type) { + return menu.isEqualsMenuType(type); + } + + public int calculatePrice() { + return menu.calculatePrice(quantity); + } + + public String getMenuName() { + return menu.getName(); + } + + public int getQuantity() { + return quantity; + } + + public Menu getMenu() { + return menu; + } +}
Java
String์œผ๋กœ ๋“ค์–ด์˜จ quantity๋ฅผ ์ •์ˆ˜๋กœ ์ปจ๋ฒ„ํŒ… ํ•˜๊ณ  ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ํ• ๋‹น์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๋ถ€๋ถ„์ด๋„ค์š”! ๋„๋ฉ”์ธ ๊ด€๋ จ ๋กœ์ง์ด ์•„๋‹˜์—๋„ Order ๋„๋ฉ”์ธ์— ์œ„์น˜ํ•˜์—ฌ Order ๋„๋ฉ”์ธ์˜ ์ฑ…์ž„์„ ํ๋ฆด ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š” ๐Ÿค” ๋ณ€ํ™˜์€ ์™ธ๋ถ€์—์„œ ์ผ์–ด๋‚˜๊ณ  ์ƒ์„ฑ์š”์ฒญ์„ ๋ฐ›์„ ๋•Œ์—๋Š” ์ •์ œ๋œ ์ƒํƒœ๋กœ ๋ฐ›์•„์•ผ ํ•˜์ง€ ์•Š๋‚˜๋ผ๋Š” ๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค ๐Ÿ‘๐Ÿป
@@ -0,0 +1,104 @@ +package christmas.domain; + +import christmas.dto.OrdersDto; +import christmas.exception.InvalidOrderException; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Orders { + public static final String ORDER_DELIMITER = ","; + public static final String MENU_AND_QUANTITY_DELIMITER = "-"; + public static final int MAX_ORDER_QUANTITY = 20; + + private final List<Order> orders; + + public Orders(String orders) { + validateFormat(orders); + this.orders = createOrders(orders); + + validateOverQuantity(); + validateDuplicate(); + validateAllDrink(); + } + + private List<Order> createOrders(String value) { + return getOrdersStream(value) + .map(order -> new Order(order[0], order[1])) + .toList(); + } + + private void validateFormat(String value) { + if (isInvalidFormat(value)) { + throw new InvalidOrderException(); + } + } + + private boolean isInvalidFormat(String value) { + return getOrdersStream(value) + .anyMatch(order -> order.length != 2); + } + + private Stream<String[]> getOrdersStream(String value) { + return Arrays.stream(value.split(ORDER_DELIMITER)) + .map(order -> order.split(MENU_AND_QUANTITY_DELIMITER)); + } + + private void validateOverQuantity() { + if (sumOrderQuantity() > MAX_ORDER_QUANTITY) { + throw new InvalidOrderException(); + } + } + + private int sumOrderQuantity() { + return orders.stream() + .mapToInt(Order::getQuantity) + .sum(); + } + + private void validateDuplicate() { + if (countUniqueOrderMenu() != orders.size()) { + throw new InvalidOrderException(); + } + } + + private int countUniqueOrderMenu() { + return (int) orders.stream() + .map(Order::getMenu) + .distinct() + .count(); + } + + private void validateAllDrink() { + if (isAllDrinkMenu()) { + throw new InvalidOrderException(); + } + } + + private boolean isAllDrinkMenu() { + return orders.stream() + .allMatch(order -> order.isEqualsMenuType(MenuType.DRINK)); + } + + public int getTotalPrice() { + return orders.stream() + .mapToInt(Order::calculatePrice) + .sum(); + } + + public int getQuantityByMenuType(MenuType type) { + return orders.stream() + .filter(order -> order.isEqualsMenuType(type)) + .mapToInt(Order::getQuantity) + .sum(); + } + + public OrdersDto getOrders() { + Map<String, Integer> result = orders.stream() + .collect(Collectors.toUnmodifiableMap(Order::getMenuName, Order::getQuantity)); + + return new OrdersDto(result); + } +}
Java
๋ฐ”๋กœ ์œ„์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ String์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒ์„ฑํ•˜๋ ค๊ณ  ํ•˜๋‹ค๋ณด๋‹ˆ String์„ splitํ•˜๊ณ  ๊ฐ’์„ ํŒŒ์‹ฑํ•˜๋Š” ๋“ฑ์˜ ์ฑ…์ž„์ด Orders ๋„๋ฉ”์ธ์— ์œ„์น˜ํ•˜๋„๋ก ์œ ๋„ํ•˜๊ณ  ์žˆ๋„ค์š”! List<Order>๋ฅผ ๋ฐ›์•„์„œ ๋ฐ”๋กœ ์ƒ์„ฑํ•˜๋„๋ก ์œ ๋„ํ•˜์‹œ๋Š” ๊ฒƒ๋„ ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค ๐Ÿ˜„
@@ -0,0 +1,48 @@ +package christmas.domain; + +import christmas.exception.InvalidDateException; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.List; + +public class VisitDate { + private static final int YEAR = 2023; + public static final int MONTH = 12; + private static final int MIN_DATE = 1; + private static final int MAX_DATE = 31; + + private final int date; + + public VisitDate(int date) { + validateDateRange(date); + this.date = date; + } + + private void validateDateRange(int date) { + if (date < MIN_DATE || date > MAX_DATE) { + throw new InvalidDateException(); + } + } + + public DayOfWeek getDayOfWeek() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, date); + return localDate.getDayOfWeek(); + } + + public boolean isContainDayOfDay(List<DayOfWeek> weekDay) { + DayOfWeek dayOfWeek = getDayOfWeek(); + return weekDay.contains(dayOfWeek); + } + + public boolean isEqualsDate(int date) { + return this.date == date; + } + + public boolean isNotAfter(int date) { + return this.date <= date; + } + + public int calculateDiscount(int discountAmount) { + return (date - 1) * discountAmount; + } +}
Java
์ฃผ๋ง์ธ์ง€ ํ‰์ผ์ธ์ง€ ๋ฌผ์–ด๋ณด๊ธฐ ์œ„ํ•ด์„œ getDayOfWeek๋ฅผ ์‚ฌ์šฉํ•˜์‹  ๊ฒƒ ๊ฐ™์€๋ฐ isWeekend()์™€ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๊ฐ€ TDA ์ธก๋ฉด์—์„œ ์ข‹์•„๋ณด์ธ๋‹ค๋Š” ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค ๐Ÿ˜„
@@ -0,0 +1,27 @@ +package christmas.domain.policy.discount; + +import christmas.domain.Orders; +import christmas.domain.VisitDate; + +public class DdayDiscountPolicy implements DiscountPolicy { + public static final int CHRISTMAS_DATE = 25; + public static final int DISCOUNT_AMOUNT = 1000; + public static final int ADDITIONAL_DISCOUNT_AMOUNT = 100; + public static final int NOT_DISCOUNT_AMOUNT = 0; + + @Override + public int discount(VisitDate visitDate, Orders orders) { + if (isNotAfter(visitDate)) { + return calculateDiscount(visitDate); + } + return NOT_DISCOUNT_AMOUNT; + } + + private boolean isNotAfter(VisitDate visitDate) { + return visitDate.isNotAfter(CHRISTMAS_DATE); + } + + private int calculateDiscount(VisitDate visitDate) { + return DISCOUNT_AMOUNT + visitDate.calculateDiscount(ADDITIONAL_DISCOUNT_AMOUNT); + } +}
Java
์ถ”์ƒ ํด๋ž˜์Šค ์ „ํ™˜ ๋„ˆ๋ฌด ์ข‹์€ ์•„์ด๋””์–ด์ธ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ข€ ๋” ์ฐพ์•„๋ณด๋‹ˆ ์ธํ„ฐํŽ˜์ด์Šค์— default ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋”๋ผ๊ตฌ์š”. ์ธํ„ฐํŽ˜์ด์Šค default ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ธํ„ฐํŽ˜์ด์Šค์˜ ์žฅ์ ๋„ ๊ฐ€์ ธ๊ฐ€๊ณ , ์กฐ๊ฑด ๊ฒ€์‚ฌ์˜ ์ฑ…์ž„๋„ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™๋„ค์š”. ๋ฆฌ๋ทฐ ๋•๋ถ„์— ์ถ”์ƒ ํด๋ž˜์Šค, ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•ด ์ฐพ์•„๋ณด๊ณ  ๊ณต๋ถ€ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜Š
@@ -0,0 +1,53 @@ +package christmas.domain; + +import christmas.dto.BenefitDto; +import christmas.dto.GiftsDto; +import java.util.function.Supplier; + +public class Bill { + public static final int MINIMUM_ORDERS_PRICE = 10000; + public static final int EVENT_NOT_APPLIED_AMOUNT = 0; + + private final Discounts discounts; + private final Gifts gifts; + + public Bill(Discounts discounts, Gifts gifts) { + this.discounts = discounts; + this.gifts = gifts; + } + + public int getTotalBenefit(Orders orders) { + return getAmount(this::sumBenefit, orders); + } + + public int getDiscountPrice(Orders orders) { + return orders.getTotalPrice() - discounts.sumDiscount(); + } + + public GiftsDto getGiftDto() { + return new GiftsDto(gifts.getResult()); + } + + public BenefitDto getBenefitDto() { + return new BenefitDto(discounts.getResult(), gifts.sumPrice()); + } + + public String getBadgeName() { + return Badge.getNameByBenefit(sumBenefit()); + } + + private int sumBenefit() { + return discounts.sumDiscount() + gifts.sumPrice(); + } + + private boolean isEventApplicable(Orders orders) { + return orders.getTotalPrice() >= MINIMUM_ORDERS_PRICE; + } + + private <T> int getAmount(Supplier<T> supplier, Orders orders) { + if (isEventApplicable(orders)) { + return (int) supplier.get(); + } + return EVENT_NOT_APPLIED_AMOUNT; + } +}
Java
> Dto๋Š” ๋ณ€๋™์„ฑ์ด ํฐ ๊ฐ์ฒด๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ domain์ด ์˜์กดํ•˜๋Š” ํ˜•ํƒœ๋Š” ์ข‹์ง€ ์•Š๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. > ์ค‘์š”ํ•œ ๊ฐ์ฒด๊ฐ€(๋„๋ฉ”์ธ) ๋œ ์ค‘์š”ํ•œ ๊ฐ์ฒด(DTO)๋ฅผ ์˜์กดํ•˜๋Š” ๊ฒƒ์ด ์ข‹์ง€ ์•Š๋‹ค ๋„ˆ๋ฌด ๊ณต๊ฐ๋˜๋Š” ๋‚ด์šฉ์ด๋„ค์š”! ํ˜„์žฌ๋Š” DTO์™€ Domain์˜ ๊ฐ•ํ•œ ๊ฒฐํ•ฉ๋„๋กœ ์ธํ•ด DTO ๋ณ€๊ฒฝ ์‹œ Domain ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผํ•œ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”๊ฑฐ ๊ฐ™์•„์š”. Domain์—์„œ๋Š” DTO ์ƒ์„ฑ์— ํ•„์š”ํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , Controller์—์„œ DTO๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊ฑฐ ๊ฐ™๋„ค์š”!!! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,36 @@ +package christmas.domain; + +import christmas.exception.NotExistsBadgeException; +import java.util.Arrays; + +public enum Badge { + SANTA("์‚ฐํƒ€", 20000), + TREE("ํŠธ๋ฆฌ", 10000), + STAR("๋ณ„", 5000), + NONE("์—†์Œ", 0), + ; + + private final String name; + private final int minimumAmount; + + private Badge(String name, int minimumAmount) { + this.name = name; + this.minimumAmount = minimumAmount; + } + + public static String getNameByBenefit(int amount) { + return from(amount).name; + } + + public static Badge from(int amount) { + return Arrays.stream(Badge.values()) + .filter(badge -> badge.isGreaterThan(amount)) + .findFirst() + .orElseThrow(NotExistsBadgeException::new); + } + + private boolean isGreaterThan(int benefitAmount) { + return this.minimumAmount <= benefitAmount; + } +} +
Java
์ด ๋ถ€๋ถ„์€ ๊ณ ๋ ค ํ•ด๋ณด์ง€ ๋ชปํ–ˆ๋„ค์š” ๐Ÿ˜ฎ ํ˜„์žฌ Enum์€ minimumAmount ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ์—†์ง€๋งŒ, ์ถ”ํ›„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ํ•ด๋‹น ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ถ”๊ฐ€์ ์ธ ๊ตฌํ˜„์„ ํ•˜๋Š” ๊ฒฝ์šฐ ์ƒ์ˆ˜ ์„ ์–ธ ๊ทœ์น™์„ ์ธ์ง€ํ•˜์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™๋„ค์š”. @Arachneee ๋ง์”€์ฒ˜๋Ÿผ ์–ด๋– ํ•œ ์ƒํ™ฉ์—์„œ๋„ ์ฝ”๋“œ์˜ ๋™์ž‘์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์ •๋ ฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ข‹์€ ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,18 @@ +package christmas.domain; + +public enum DiscountType { + CHRISTMAS_DDAY("ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋””๋ฐ์ด ํ• ์ธ"), + WEEKDAY("ํ‰์ผ ํ• ์ธ"), + WEEKEND("์ฃผ๋ง ํ• ์ธ"), + SPECIAL("ํŠน๋ณ„ ํ• ์ธ"); + + private final String name; + + DiscountType(String name) { + this.name = name; + } + + public String getName() { + return name; + } +}
Java
์ €๋„ ๊ฐ DiscountPolicy ๊ตฌํ˜„์ฒด์—์„œ ํ• ์ธ ์ œ๋ชฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ตฌ์กฐ๋ฅผ ์ƒ๊ฐํ•ด๋ดค๋Š”๋ฐ, ์ด ๊ตฌ์กฐ๋Š” DiscountPolicy์™€ ํ• ์ธ ์ œ๋ชฉ์ด ์ผ๋Œ€์ผ๋กœ ๋งคํ•‘๋˜๊ธฐ ๋•Œ๋ฌธ์— DiscountPolicy๋ฅผ ์—ฌ๋Ÿฌ ํ• ์ธ์—์„œ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ถ”ํ›„ ์ถ”๊ฐ€๋  ์ˆ˜ ์žˆ๋Š” ์š”๊ตฌ์‚ฌํ•ญ์„ ๊ณ ๋ คํ–ˆ์„ ๋•Œ ํ™•์žฅ์„ฑ ๋ฉด์—์„œ ์ข‹์ง€ ์•Š์„๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•ด DiscountPolicy์™€ ํ• ์ธ ์ œ๋ชฉ์„ ๋”ฐ๋กœ ๊ด€๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์–ด๋–ค ์ฝ”๋“œ๊ฐ€ ๋” ์ข‹์€ ์ฝ”๋“œ์ธ์ง€ ์ •๋‹ต์€ ์—†๋Š”๊ฑฐ ๊ฐ™์•„์š”. ์ด๋ฒˆ ๋ฏธ์…˜์„ ์ง„ํ–‰ํ•˜๋ฉฐ ์š”๊ตฌ์‚ฌํ•ญ ํ™•์žฅ์„ ์–ผ๋งˆ๋‚˜ ๊ณ ๋ คํ•ด์•ผํ• ์ง€์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์ด ๋งŽ์•˜๊ธฐ์— ์ œ ์ƒ๊ฐ์„ ํ•œ ๋ฒˆ ๊ณต์œ ๋“œ๋ ค๋ด…๋‹ˆ๋‹ค! ์ถ”๊ฐ€๋กœ, Enum์œผ๋กœ ํ• ์ธ ์ข…๋ฅ˜๋ฅผ ํ•œ ๊ณณ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋…์„ฑ ๋ฉด์—์„œ ๋” ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์–ด์š”. ๋˜ํ•œ, ํ• ์ธ ์ œ๋ชฉ์„ String์œผ๋กœ ๊ด€๋ฆฌํ•˜๋ฉด ์ž˜๋ชป๋œ ๊ฐ’์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Enum์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜€
@@ -0,0 +1,53 @@ +package christmas.domain; + +import christmas.dto.BenefitDto; +import christmas.dto.GiftsDto; +import java.util.function.Supplier; + +public class Bill { + public static final int MINIMUM_ORDERS_PRICE = 10000; + public static final int EVENT_NOT_APPLIED_AMOUNT = 0; + + private final Discounts discounts; + private final Gifts gifts; + + public Bill(Discounts discounts, Gifts gifts) { + this.discounts = discounts; + this.gifts = gifts; + } + + public int getTotalBenefit(Orders orders) { + return getAmount(this::sumBenefit, orders); + } + + public int getDiscountPrice(Orders orders) { + return orders.getTotalPrice() - discounts.sumDiscount(); + } + + public GiftsDto getGiftDto() { + return new GiftsDto(gifts.getResult()); + } + + public BenefitDto getBenefitDto() { + return new BenefitDto(discounts.getResult(), gifts.sumPrice()); + } + + public String getBadgeName() { + return Badge.getNameByBenefit(sumBenefit()); + } + + private int sumBenefit() { + return discounts.sumDiscount() + gifts.sumPrice(); + } + + private boolean isEventApplicable(Orders orders) { + return orders.getTotalPrice() >= MINIMUM_ORDERS_PRICE; + } + + private <T> int getAmount(Supplier<T> supplier, Orders orders) { + if (isEventApplicable(orders)) { + return (int) supplier.get(); + } + return EVENT_NOT_APPLIED_AMOUNT; + } +}
Java
@Libienz ๋ง์”€์ฒ˜๋Ÿผ Bill๋Š” ์ฃผ๋ฌธ์— ๋Œ€ํ•œ ํ• ์ธ ๋‚ด์—ญ์„ ๊ฐ–๋Š” ๊ฐ์ฒด์ธ๋ฐ ํ•ด๋‹น ๊ฐ์ฒด์—์„œ ํ• ์ธ ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ์€ ๋งŽ์€ ์ฑ…์ž„์„ ๊ฐ–๊ณ  ์žˆ๋Š”๊ฑฐ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“œ๋„ค์š”. DiscountPolicy์˜ default ๋ฉ”์„œ๋“œ๋กœ ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค ์ข‹์€ ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜€
@@ -0,0 +1,53 @@ +package christmas.domain; + +import christmas.dto.BenefitDto; +import christmas.dto.GiftsDto; +import java.util.function.Supplier; + +public class Bill { + public static final int MINIMUM_ORDERS_PRICE = 10000; + public static final int EVENT_NOT_APPLIED_AMOUNT = 0; + + private final Discounts discounts; + private final Gifts gifts; + + public Bill(Discounts discounts, Gifts gifts) { + this.discounts = discounts; + this.gifts = gifts; + } + + public int getTotalBenefit(Orders orders) { + return getAmount(this::sumBenefit, orders); + } + + public int getDiscountPrice(Orders orders) { + return orders.getTotalPrice() - discounts.sumDiscount(); + } + + public GiftsDto getGiftDto() { + return new GiftsDto(gifts.getResult()); + } + + public BenefitDto getBenefitDto() { + return new BenefitDto(discounts.getResult(), gifts.sumPrice()); + } + + public String getBadgeName() { + return Badge.getNameByBenefit(sumBenefit()); + } + + private int sumBenefit() { + return discounts.sumDiscount() + gifts.sumPrice(); + } + + private boolean isEventApplicable(Orders orders) { + return orders.getTotalPrice() >= MINIMUM_ORDERS_PRICE; + } + + private <T> int getAmount(Supplier<T> supplier, Orders orders) { + if (isEventApplicable(orders)) { + return (int) supplier.get(); + } + return EVENT_NOT_APPLIED_AMOUNT; + } +}
Java
Bill ๊ฐ์ฒด๋Š” Order์™€ Discount, Gift ๊ฐ์ฒด ์‚ฌ์ด์˜ ํ˜‘๋ ฅ์„ ์œ„ํ•œ ์ค‘๊ฐ„ ๊ฐ์ฒด๋กœ, ์ฃผ๋ฌธ์— ๋Œ€ํ•œ ํ• ์ธ ํ˜œํƒ์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์„ค๊ณ„ํ–ˆ์–ด์š”. ๋”ฐ๋ผ์„œ, ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก์— ๋Œ€ํ•œ ๊ณ„์‚ฐ์€ Bill ๊ฐ์ฒด์˜ ์ฑ…์ž„์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์‚ดํŽด๋ณด๋‹ˆ ์•„๋ž˜์™€ ๊ฐ™์ด Order ๊ฐ์ฒด์— ํ• ์ธ ๊ธˆ์•ก์„ ๋„˜๊ฒจ์คŒ์œผ๋กœ์จ Order ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ ํ• ์ธ๋œ ๊ฐ€๊ฒฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์„๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๊ฐ€ ๋” ๊ฐ์ฒด๊ฐ„ ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ ๋ฐ›์œผ๋ฉฐ ํ˜‘๋ ฅํ•˜๋Š” ๊ตฌ์กฐ๋ผ๊ณ  ์ƒ๊ฐ๋˜๋„ค์š”. ๋•๋ถ„์— ๋” ๋‚˜์€ ์ฝ”๋“œ๋กœ ๋ฆฌํŒฉํ† ๋ง ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋„ค์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜ถ ```java public int getDiscountPrice(Orders orders) { int discount = discounts.sumDiscount(); return orders.getDiscountPrice(discount); } ``` ๊ทธ๋ฆฌ๊ณ , Bill ๊ฐ์ฒด๋Š” ์ค‘๊ฐ„ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— Orders ๋„๋ฉ”์ธ์„ ์•Œ์•„๋„ ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์–ด์š”. ๋งŒ์•ฝ, Orders๊ฐ€ ์•„๋‹Œ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์„ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค๋ฉด ์™ธ๋ถ€์—์„œ getTotalPrice ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผํ•˜๋Š”๋ฐ, ์™ธ๋ถ€์—์„œ ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์„ ์•Œ๊ณ ์žˆ์–ด์•ผ ํ• ๊นŒ? ๋ผ๋Š” ์˜๋ฌธ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, Restaurant ๊ฐ์ฒด๋Š” ๋ฐฉ๋ฌธ ๋‚ ์งœ์™€ ์ฃผ๋ฌธ์„ ๋ฐ›๊ธฐ๋งŒ ํ•˜๊ณ , ์ด๋ฅผ Bill ๊ฐ์ฒด์—๊ฒŒ ํ• ์ธ๋œ ๊ฐ€๊ฒฉ์„ ์•Œ๋ ค์ค˜ ~ ๋ผ๊ณ  ์ฑ…์ž„์„ ์œ„์ž„ํ•˜๋Š” ๊ฒƒ์ด ๋” ๊ฐ์ฒด์ง€ํ–ฅ์Šค๋Ÿฝ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์–ด์š”. @Libienz ๋ฆฌ๋ทฐ๋ฅผ ํ†ตํ•ด ์ œ ์ฝ”๋“œ์— ๋Œ€ํ•œ ๊ทผ๊ฑฐ๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ƒ๊ฐํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ๋„ค์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค !!!!
@@ -0,0 +1,50 @@ +package christmas.domain; + +import static christmas.exception.ErrorMessages.INVALID_ORDER; + +import christmas.utils.IntegerConverter; +import christmas.exception.InvalidOrderException; + +public class Order { + public static final int MIN_ORDER_QUANTITY = 1; + + private final Menu menu; + private final int quantity; + + public Order(String menuName, String quantity) { + this.menu = Menu.from(menuName); + this.quantity = convertType(quantity); + + validateMinQuantity(); + } + + private int convertType(String quantity) { + return IntegerConverter.convert(quantity, INVALID_ORDER); + } + + private void validateMinQuantity() { + if (quantity < MIN_ORDER_QUANTITY) { + throw new InvalidOrderException(); + } + } + + public boolean isEqualsMenuType(MenuType type) { + return menu.isEqualsMenuType(type); + } + + public int calculatePrice() { + return menu.calculatePrice(quantity); + } + + public String getMenuName() { + return menu.getName(); + } + + public int getQuantity() { + return quantity; + } + + public Menu getMenu() { + return menu; + } +}
Java
@Libienz ๋ฆฌ๋ทฐ์— ์ ๊ทน ๊ณต๊ฐํ•˜๋Š” ๋ฐ”์ž…๋‹ˆ๋‹ค! ์ž…๋ ฅ์— ๋Œ€ํ•œ ํ˜•์‹์€ View ๊ณ„์ธต์—์„œ ๊ฒ€์ฆํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋„ค์š”. ํ˜„์žฌ๋Š” ์ž…๋ ฅ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ƒ์„ฑ์ž ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋„๋ฉ”์ธ ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ ํ˜•์‹์— ๋งž์ถฐ ๊ฐ’์„ ํŒŒ์‹ฑํ•˜๋Š” ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ์–ด์š”. @Libienz ๋ง์”€์ฒ˜๋Ÿผ ์ด๋Ÿฌํ•œ ๊ตฌ์กฐ๋Š” ๋„๋ฉ”์ธ์˜ ์ฑ…์ž„์„ ํ๋ฆด ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์š”. ์ถ”๊ฐ€๋กœ, ํŒŒ์‹ฑ ๋กœ์ง์— ์˜ํ•ด ๋„๋ฉ”์ธ ๋กœ์ง์„ ํ•œ ๋ˆˆ์— ํŒŒ์•…ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์„๊ฑฐ ๊ฐ™์•„์š”. ๊ทผํฌ๋‹˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ์ž…๋ ฅ๊ฐ’์— ๋Œ€ํ•œ ํ˜•์‹ ๊ฒ€์ฆ ๋ฐ ํŒŒ์‹ฑ ๊ฐ์ฒด๊ฐ€ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ๋”๋ผ๊ตฌ์š”. ์ •๋ง ์ธ์ƒ๊นŠ๊ฒŒ ๋ดค์Šต๋‹ˆ๋‹ค. ์ €๋„ ์•ž์œผ๋กœ๋Š” View ๊ณ„์ธต์œผ๋กœ ํ•ด๋‹น ๋กœ์ง์„ ๋ถ„๋ฆฌํ•ด ๋ณผ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค. ์ข‹์€ ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ‘๐Ÿป
@@ -0,0 +1,48 @@ +package christmas.domain; + +import christmas.exception.InvalidDateException; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.List; + +public class VisitDate { + private static final int YEAR = 2023; + public static final int MONTH = 12; + private static final int MIN_DATE = 1; + private static final int MAX_DATE = 31; + + private final int date; + + public VisitDate(int date) { + validateDateRange(date); + this.date = date; + } + + private void validateDateRange(int date) { + if (date < MIN_DATE || date > MAX_DATE) { + throw new InvalidDateException(); + } + } + + public DayOfWeek getDayOfWeek() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, date); + return localDate.getDayOfWeek(); + } + + public boolean isContainDayOfDay(List<DayOfWeek> weekDay) { + DayOfWeek dayOfWeek = getDayOfWeek(); + return weekDay.contains(dayOfWeek); + } + + public boolean isEqualsDate(int date) { + return this.date == date; + } + + public boolean isNotAfter(int date) { + return this.date <= date; + } + + public int calculateDiscount(int discountAmount) { + return (date - 1) * discountAmount; + } +}
Java
์—‡ ์ด ๋ฉ”์†Œ๋“œ๋Š” VisitDate ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ธ๋ฐ ์ ‘๊ทผ ์ œํ•œ์ž๋ฅผ ์ž˜๋ชป ์„ค์ •ํ–ˆ๊ตฐ์š” .. ์„ธ์„ธํ•œ ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,53 @@ +package christmas.domain; + +import christmas.dto.BenefitDto; +import christmas.dto.GiftsDto; +import java.util.function.Supplier; + +public class Bill { + public static final int MINIMUM_ORDERS_PRICE = 10000; + public static final int EVENT_NOT_APPLIED_AMOUNT = 0; + + private final Discounts discounts; + private final Gifts gifts; + + public Bill(Discounts discounts, Gifts gifts) { + this.discounts = discounts; + this.gifts = gifts; + } + + public int getTotalBenefit(Orders orders) { + return getAmount(this::sumBenefit, orders); + } + + public int getDiscountPrice(Orders orders) { + return orders.getTotalPrice() - discounts.sumDiscount(); + } + + public GiftsDto getGiftDto() { + return new GiftsDto(gifts.getResult()); + } + + public BenefitDto getBenefitDto() { + return new BenefitDto(discounts.getResult(), gifts.sumPrice()); + } + + public String getBadgeName() { + return Badge.getNameByBenefit(sumBenefit()); + } + + private int sumBenefit() { + return discounts.sumDiscount() + gifts.sumPrice(); + } + + private boolean isEventApplicable(Orders orders) { + return orders.getTotalPrice() >= MINIMUM_ORDERS_PRICE; + } + + private <T> int getAmount(Supplier<T> supplier, Orders orders) { + if (isEventApplicable(orders)) { + return (int) supplier.get(); + } + return EVENT_NOT_APPLIED_AMOUNT; + } +}
Java
์™€์šฐ ๋„๋ฉ”์ธ๊ณผ ์ฑ…์ž„๊ณผ ์—ญํ• ์— ๋Œ€ํ•ด์„œ ๊นŠ์€ ๊ณ ๋ฏผ์„ ํ•˜์‹  ๊ฒƒ์ด ๋А๊ปด์ง€๋„ค์š”.. ๐Ÿ‘๐Ÿป ๐Ÿ‘๐Ÿป ์ฑ…์ž„๊ณผ ์—ญํ• ์„ ๋ถ„๋ฆฌํ•˜๊ณ  ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์€ ์ €๋„ ์ต์ˆ™์น˜๊ฐ€ ์•Š์€๋ฐ ์ •์„ฑ์Šค๋Ÿฌ์šด ๋‹ต๋ณ€ ์ฃผ์…”์„œ ์ €๋„ ์ €๋งŒ์˜ ๊ธฐ์ค€์„ ํ•œ๋ฒˆ ๊ฒ€ํ† ํ•ด๋ณด์•„์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“œ๋„ค์š” ๐Ÿ˜„
@@ -0,0 +1,55 @@ +package christmas.controller; + +import static christmas.utils.RepeatReader.repeatRead; + +import christmas.domain.Bill; +import christmas.domain.Orders; +import christmas.domain.Restaurant; +import christmas.domain.VisitDate; +import christmas.view.InputView; +import christmas.view.OutputView; + +public class EventController { + private final Restaurant restaurant; + private final InputView inputView; + private final OutputView outputView; + + public EventController(Restaurant restaurant, InputView inputView, OutputView outputView) { + this.restaurant = restaurant; + this.inputView = inputView; + this.outputView = outputView; + } + + public void run() { + VisitDate visitDate = repeatRead(this::readDate); + Orders orders = repeatRead(this::readOrders); + + Bill bill = restaurant.order(visitDate, orders); + + printOrders(orders); + printBill(bill, orders); + } + + private VisitDate readDate() { + int date = inputView.readDate(); + return new VisitDate(date); + } + + private Orders readOrders() { + String orders = inputView.readOrders(); + return new Orders(orders); + } + + private void printOrders(Orders orders) { + outputView.printOrders(orders.getOrders()); + outputView.printPrice(orders.getTotalPrice()); + } + + private void printBill(Bill bill, Orders orders) { + outputView.printGifts(bill.getGiftDto()); + outputView.printBenefit(bill.getBenefitDto()); + outputView.printBenefitAmount(bill.getTotalBenefit(orders)); + outputView.printDiscountPrice(bill.getDiscountPrice(orders)); + outputView.printBadge(bill.getBadgeName()); + } +}
Java
์šฐ์™€.. ์ „๋ฐ˜์ ์œผ๋กœ ์ฝ”๋“œ ํ๋ฆ„์ด ์ž˜ ์ฝํžˆ๋„ค์š”..!! ํ• ์ธ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ํ•จ์ˆ˜์˜ ์ด๋ฆ„์„ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ ๊ณ ๋ฏผ์„ ๋งŽ์ด ํ–ˆ๋Š”๋ฐ, printBill()์ด๋ผ๋Š” ์ด๋ฆ„์„ ๋ณด๋‹ˆ ๋จธ๋ฆฌ์— ๋ง์น˜๋ฅผ ํ•œ ๋Œ€ ๋งž์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.. ๐Ÿ‘
@@ -0,0 +1,84 @@ +import { Console } from '@woowacourse/mission-utils'; +import { PROMOTION_CATEGORIES } from '../constant/index.js'; + +const MESSAGE = { + GREETINGS: '์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค.', + INTRO_PREVIEW: '12์›” 26์ผ์— ์šฐํ…Œ์ฝ” ์‹๋‹น์—์„œ ๋ฐ›์„ ์ด๋ฒคํŠธ ํ˜œํƒ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n', + ORDER_MENU_TITLE: '<์ฃผ๋ฌธ ๋ฉ”๋‰ด>', + TOTAL_PRICE_WITHOUT_DISCOUNT_TITLE: '<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>', + BENEFIT_DETAILS_TITLE: '<ํ˜œํƒ ๋‚ด์—ญ>', + TOTAL_PRICE_WITH_DISCOUNT_TITLE: '<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>', + GIFT_TITLE: '<์ฆ์ • ๋ฉ”๋‰ด>', + BENEFIT_TOTAL_PRICE_TITLE: '<์ดํ˜œํƒ ๊ธˆ์•ก>', + BADGE_TITLE: '<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>', + NONE: '์—†์Œ', + MENU: (name, count) => `${name} ${count}๊ฐœ`, + PRICE: (price) => `${price.toLocaleString()}์›`, + GIFT: (name, count) => `${name} ${count}๊ฐœ`, + DISCOUNT: (name, price) => `${name} ํ• ์ธ: -${price.toLocaleString()}์›`, + TOTAL_BENEFIT_PRICE: (price) => (price > 0 ? `-${price.toLocaleString()}์›` : '0์›'), +}; + +const OutputView = { + printGreetings() { + Console.print(MESSAGE.GREETINGS); + }, + printPreviewMessage() { + Console.print(MESSAGE.INTRO_PREVIEW); + Console.print(''); + }, + printOrderMenus(orders) { + Console.print(MESSAGE.ORDER_MENU_TITLE); + + orders.forEach((order) => { + Console.print(MESSAGE.MENU(order.name, order.count)); + }); + + Console.print(''); + }, + printTotalPriceWithoutDiscount(totalPrice) { + Console.print(MESSAGE.TOTAL_PRICE_WITHOUT_DISCOUNT_TITLE); + Console.print(MESSAGE.PRICE(totalPrice)); + Console.print(''); + }, + printBenefitDetails(promotions) { + Console.print(MESSAGE.BENEFIT_DETAILS_TITLE); + + const printData = []; + + promotions.forEach((promotion) => { + const { NAME, promotionBenefitPrice } = promotion; + const isApplied = promotionBenefitPrice > 0; + if (isApplied) printData.push(MESSAGE.DISCOUNT(NAME, promotionBenefitPrice)); + }); + if (printData.length === 0) printData.push(MESSAGE.NONE); + + Console.print(printData.join('\n')); + Console.print(''); + }, + printTotalPriceWithDiscount(totalPriceWithDiscount) { + Console.print(MESSAGE.TOTAL_PRICE_WITH_DISCOUNT_TITLE); + Console.print(MESSAGE.PRICE(totalPriceWithDiscount)); + Console.print(''); + }, + printGiveWayMenu(promotions) { + const giftMenu = promotions.find((promotion) => promotion.EVENT === PROMOTION_CATEGORIES.GIFT); + const { PRODUCT, promotionBenefitPrice } = giftMenu; + const isApplied = promotionBenefitPrice > 0; + + Console.print(MESSAGE.GIFT_TITLE); + Console.print(isApplied ? MESSAGE.GIFT(PRODUCT.NAME, 1) : MESSAGE.NONE); + Console.print(''); + }, + printTotalBenefitPrice(totalBenefitPrice) { + Console.print(MESSAGE.BENEFIT_TOTAL_PRICE_TITLE); + Console.print(MESSAGE.TOTAL_BENEFIT_PRICE(totalBenefitPrice)); + Console.print(''); + }, + printBadge(badge) { + Console.print(MESSAGE.BADGE_TITLE); + Console.print(badge); + }, +}; + +export default OutputView;
JavaScript
์ €๋„ ์ด ๋ถ€๋ถ„ ์‹ค์ˆ˜ํ–ˆ๋Š”๋ฐ, ์ผ์ž๊ฐ€ ๋ฐ”๋€Œ์–ด์„œ ์ถœ๋ ฅ๋˜๋„๋ก ํ•ด์•ผํ•ด์š”..ใ…œใ…œ
@@ -0,0 +1,126 @@ +import { isDuplicate, isEveryInclude, isMoreThanLimit, isNotMatchRegex, isSomeNotInclude } from '../util/index.js'; +import { LIMIT_ORDER_COUNT, MENU_CATEGORIES, MENUS, ORDER_REGEX } from '../constant/index.js'; + +const ERROR_MESSAGE = Object.freeze({ + INVALID_DATE: '์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + IS_INVALID_ORDER: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', +}); + +const { INVALID_DATE, IS_INVALID_ORDER } = ERROR_MESSAGE; + +class Order { + static validateOrder(orderMenus) { + const validators = [ + () => Order.validateDuplicationOrder(orderMenus), + () => Order.validateOrderMenuFormat(orderMenus, ORDER_REGEX.ORDER_FORMAT), + () => Order.validateLimitOrderCount(orderMenus, LIMIT_ORDER_COUNT), + () => Order.validateIncludeMenu(orderMenus, MENUS), + () => Order.validateOrderOnlyOneCategory(orderMenus, MENUS, MENU_CATEGORIES.BEVERAGE), + ]; + + validators.forEach((validator) => validator()); + } + static validateDate(date) { + const validators = [() => Order.validateDayFormat(date, ORDER_REGEX.DAY_FORMAT)]; + + validators.forEach((validator) => validator()); + } + static validateOrderOnlyOneCategory(orderMenus, menus, category) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + const menuNameListForCategory = Object.values(menus) + .filter((menu) => menu.CATEGORY === category) + .map((menu) => menu.NAME); + + isEveryInclude(IS_INVALID_ORDER, orderMenuNameList, menuNameListForCategory); + } + static validateIncludeMenu(orderMenus, menus) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + const menuNameList = Object.values(menus).map((menu) => menu.NAME); + + isSomeNotInclude(IS_INVALID_ORDER, orderMenuNameList, menuNameList); + } + static validateDuplicationOrder(orderMenus) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + + isDuplicate(IS_INVALID_ORDER, orderMenuNameList); + } + static validateLimitOrderCount(orderMenus, limitOrderCount) { + const { totalOrderCount } = Order.parseOrders(orderMenus); + + isMoreThanLimit(IS_INVALID_ORDER, totalOrderCount, limitOrderCount); + } + static validateOrderMenuFormat(orderMenus, regex) { + const { orderMenuList } = Order.parseOrders(orderMenus); + + orderMenuList.forEach((orderMenu) => isNotMatchRegex(IS_INVALID_ORDER, orderMenu, regex)); + } + static validateDayFormat(date, regex) { + isNotMatchRegex(INVALID_DATE, date, regex); + } + static parseOrders(orderMenu) { + const orderMenuList = orderMenu.split(',').map((menu) => menu.trim()); + const orderMenuNameList = orderMenuList.map((menu) => menu.split('-')[0]); + const orderMenuCountList = orderMenuList.map((menu) => menu.split('-')[1]); + const totalOrderCount = orderMenuCountList.reduce((acc, cur) => acc + Number(cur), 0); + + return { + orderMenuList, + orderMenuNameList, + orderMenuCountList, + totalOrderCount, + }; + } + + #orderDate; + #orderMenus = new Map(); + #menus = new Map(); + + constructor(menus, orderMenus, orderDate) { + this.#menus = new Map(Object.entries(menus)); + this.#orderDate = orderDate; + this.#setOrderMenus(orderMenus); + } + + getOrderMenuList() { + return this.#getOrderMenuCategories() + .map((categoryName) => this.getOrderMenuByCategory(categoryName)) + .flat(); + } + getTotalPrice() { + return Array.from(this.#orderMenus.values()).reduce((acc, orderMenu) => { + const key = this.#getKeyByMenuName(orderMenu.name); + const menu = this.#menus.get(key); + return acc + menu.PRICE * orderMenu.count; + }, 0); + } + getOrderDate() { + return this.#orderDate; + } + getOrderMenuByCategory(categoryName) { + return Array.from(this.#orderMenus.values()).filter((orderMenu) => orderMenu.category === categoryName); + } + #setOrderMenus(orderMenus) { + const { orderMenuNameList, orderMenuCountList } = Order.parseOrders(orderMenus); + + orderMenuNameList.forEach((menuName, index) => { + const category = this.#getCategoryByMenuName(menuName); + const key = this.#getKeyByMenuName(menuName); + + this.#orderMenus.set(key, { + name: menuName, + count: orderMenuCountList[index], + category, + }); + }); + } + #getOrderMenuCategories() { + return Array.from(new Set(Array.from(this.#menus.values()).map((menu) => menu.CATEGORY))); + } + #getCategoryByMenuName(menuName) { + return Array.from(this.#menus.values()).find((menuMap) => menuMap.NAME === menuName).CATEGORY; + } + #getKeyByMenuName(menuName) { + return Array.from(this.#menus.keys()).find((key) => this.#menus.get(key).NAME === menuName); + } +} +export default Order;
JavaScript
Input์— ๋Œ€ํ•œ ๋ชจ๋“  ์œ ํšจ์„ฑ ๊ฒ€์ฆ์„ Order ๋„๋ฉ”์ธ์—์„œ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ณ„์‹ ๋ฐ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”. ๊ณตํ†ต ํ”ผ๋“œ๋ฐฑ์—์„œ ๊ตฌํ˜„ ์ˆœ์„œ์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด ํด๋ž˜์Šค๋Š” ํ•„๋“œ-์ƒ์„ฑ์ž-๋ฉ”์„œ๋“œ ์ˆœ์œผ๋กœ ๊ตฌํ˜„๋˜์–ด์žˆ์ง€ ์•Š์•„์„œ ์ด ๋ถ€๋ถ„์„ ๊ฐœ์„ ํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,126 @@ +import { isDuplicate, isEveryInclude, isMoreThanLimit, isNotMatchRegex, isSomeNotInclude } from '../util/index.js'; +import { LIMIT_ORDER_COUNT, MENU_CATEGORIES, MENUS, ORDER_REGEX } from '../constant/index.js'; + +const ERROR_MESSAGE = Object.freeze({ + INVALID_DATE: '์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + IS_INVALID_ORDER: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', +}); + +const { INVALID_DATE, IS_INVALID_ORDER } = ERROR_MESSAGE; + +class Order { + static validateOrder(orderMenus) { + const validators = [ + () => Order.validateDuplicationOrder(orderMenus), + () => Order.validateOrderMenuFormat(orderMenus, ORDER_REGEX.ORDER_FORMAT), + () => Order.validateLimitOrderCount(orderMenus, LIMIT_ORDER_COUNT), + () => Order.validateIncludeMenu(orderMenus, MENUS), + () => Order.validateOrderOnlyOneCategory(orderMenus, MENUS, MENU_CATEGORIES.BEVERAGE), + ]; + + validators.forEach((validator) => validator()); + } + static validateDate(date) { + const validators = [() => Order.validateDayFormat(date, ORDER_REGEX.DAY_FORMAT)]; + + validators.forEach((validator) => validator()); + } + static validateOrderOnlyOneCategory(orderMenus, menus, category) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + const menuNameListForCategory = Object.values(menus) + .filter((menu) => menu.CATEGORY === category) + .map((menu) => menu.NAME); + + isEveryInclude(IS_INVALID_ORDER, orderMenuNameList, menuNameListForCategory); + } + static validateIncludeMenu(orderMenus, menus) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + const menuNameList = Object.values(menus).map((menu) => menu.NAME); + + isSomeNotInclude(IS_INVALID_ORDER, orderMenuNameList, menuNameList); + } + static validateDuplicationOrder(orderMenus) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + + isDuplicate(IS_INVALID_ORDER, orderMenuNameList); + } + static validateLimitOrderCount(orderMenus, limitOrderCount) { + const { totalOrderCount } = Order.parseOrders(orderMenus); + + isMoreThanLimit(IS_INVALID_ORDER, totalOrderCount, limitOrderCount); + } + static validateOrderMenuFormat(orderMenus, regex) { + const { orderMenuList } = Order.parseOrders(orderMenus); + + orderMenuList.forEach((orderMenu) => isNotMatchRegex(IS_INVALID_ORDER, orderMenu, regex)); + } + static validateDayFormat(date, regex) { + isNotMatchRegex(INVALID_DATE, date, regex); + } + static parseOrders(orderMenu) { + const orderMenuList = orderMenu.split(',').map((menu) => menu.trim()); + const orderMenuNameList = orderMenuList.map((menu) => menu.split('-')[0]); + const orderMenuCountList = orderMenuList.map((menu) => menu.split('-')[1]); + const totalOrderCount = orderMenuCountList.reduce((acc, cur) => acc + Number(cur), 0); + + return { + orderMenuList, + orderMenuNameList, + orderMenuCountList, + totalOrderCount, + }; + } + + #orderDate; + #orderMenus = new Map(); + #menus = new Map(); + + constructor(menus, orderMenus, orderDate) { + this.#menus = new Map(Object.entries(menus)); + this.#orderDate = orderDate; + this.#setOrderMenus(orderMenus); + } + + getOrderMenuList() { + return this.#getOrderMenuCategories() + .map((categoryName) => this.getOrderMenuByCategory(categoryName)) + .flat(); + } + getTotalPrice() { + return Array.from(this.#orderMenus.values()).reduce((acc, orderMenu) => { + const key = this.#getKeyByMenuName(orderMenu.name); + const menu = this.#menus.get(key); + return acc + menu.PRICE * orderMenu.count; + }, 0); + } + getOrderDate() { + return this.#orderDate; + } + getOrderMenuByCategory(categoryName) { + return Array.from(this.#orderMenus.values()).filter((orderMenu) => orderMenu.category === categoryName); + } + #setOrderMenus(orderMenus) { + const { orderMenuNameList, orderMenuCountList } = Order.parseOrders(orderMenus); + + orderMenuNameList.forEach((menuName, index) => { + const category = this.#getCategoryByMenuName(menuName); + const key = this.#getKeyByMenuName(menuName); + + this.#orderMenus.set(key, { + name: menuName, + count: orderMenuCountList[index], + category, + }); + }); + } + #getOrderMenuCategories() { + return Array.from(new Set(Array.from(this.#menus.values()).map((menu) => menu.CATEGORY))); + } + #getCategoryByMenuName(menuName) { + return Array.from(this.#menus.values()).find((menuMap) => menuMap.NAME === menuName).CATEGORY; + } + #getKeyByMenuName(menuName) { + return Array.from(this.#menus.keys()).find((key) => this.#menus.get(key).NAME === menuName); + } +} +export default Order;
JavaScript
์ง€๊ธˆ ์ด menus๋Š” ์ƒ์ˆ˜ ๊ฐ์ฒด MENUS์ธ๋ฐ ๋‹ค์‹œ Map์œผ๋กœ ๋งŒ๋“œ์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”. MENUS์˜ ํ‚ค๊ฐ’์„ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐœ์„ ํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,126 @@ +import { isDuplicate, isEveryInclude, isMoreThanLimit, isNotMatchRegex, isSomeNotInclude } from '../util/index.js'; +import { LIMIT_ORDER_COUNT, MENU_CATEGORIES, MENUS, ORDER_REGEX } from '../constant/index.js'; + +const ERROR_MESSAGE = Object.freeze({ + INVALID_DATE: '์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + IS_INVALID_ORDER: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', +}); + +const { INVALID_DATE, IS_INVALID_ORDER } = ERROR_MESSAGE; + +class Order { + static validateOrder(orderMenus) { + const validators = [ + () => Order.validateDuplicationOrder(orderMenus), + () => Order.validateOrderMenuFormat(orderMenus, ORDER_REGEX.ORDER_FORMAT), + () => Order.validateLimitOrderCount(orderMenus, LIMIT_ORDER_COUNT), + () => Order.validateIncludeMenu(orderMenus, MENUS), + () => Order.validateOrderOnlyOneCategory(orderMenus, MENUS, MENU_CATEGORIES.BEVERAGE), + ]; + + validators.forEach((validator) => validator()); + } + static validateDate(date) { + const validators = [() => Order.validateDayFormat(date, ORDER_REGEX.DAY_FORMAT)]; + + validators.forEach((validator) => validator()); + } + static validateOrderOnlyOneCategory(orderMenus, menus, category) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + const menuNameListForCategory = Object.values(menus) + .filter((menu) => menu.CATEGORY === category) + .map((menu) => menu.NAME); + + isEveryInclude(IS_INVALID_ORDER, orderMenuNameList, menuNameListForCategory); + } + static validateIncludeMenu(orderMenus, menus) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + const menuNameList = Object.values(menus).map((menu) => menu.NAME); + + isSomeNotInclude(IS_INVALID_ORDER, orderMenuNameList, menuNameList); + } + static validateDuplicationOrder(orderMenus) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + + isDuplicate(IS_INVALID_ORDER, orderMenuNameList); + } + static validateLimitOrderCount(orderMenus, limitOrderCount) { + const { totalOrderCount } = Order.parseOrders(orderMenus); + + isMoreThanLimit(IS_INVALID_ORDER, totalOrderCount, limitOrderCount); + } + static validateOrderMenuFormat(orderMenus, regex) { + const { orderMenuList } = Order.parseOrders(orderMenus); + + orderMenuList.forEach((orderMenu) => isNotMatchRegex(IS_INVALID_ORDER, orderMenu, regex)); + } + static validateDayFormat(date, regex) { + isNotMatchRegex(INVALID_DATE, date, regex); + } + static parseOrders(orderMenu) { + const orderMenuList = orderMenu.split(',').map((menu) => menu.trim()); + const orderMenuNameList = orderMenuList.map((menu) => menu.split('-')[0]); + const orderMenuCountList = orderMenuList.map((menu) => menu.split('-')[1]); + const totalOrderCount = orderMenuCountList.reduce((acc, cur) => acc + Number(cur), 0); + + return { + orderMenuList, + orderMenuNameList, + orderMenuCountList, + totalOrderCount, + }; + } + + #orderDate; + #orderMenus = new Map(); + #menus = new Map(); + + constructor(menus, orderMenus, orderDate) { + this.#menus = new Map(Object.entries(menus)); + this.#orderDate = orderDate; + this.#setOrderMenus(orderMenus); + } + + getOrderMenuList() { + return this.#getOrderMenuCategories() + .map((categoryName) => this.getOrderMenuByCategory(categoryName)) + .flat(); + } + getTotalPrice() { + return Array.from(this.#orderMenus.values()).reduce((acc, orderMenu) => { + const key = this.#getKeyByMenuName(orderMenu.name); + const menu = this.#menus.get(key); + return acc + menu.PRICE * orderMenu.count; + }, 0); + } + getOrderDate() { + return this.#orderDate; + } + getOrderMenuByCategory(categoryName) { + return Array.from(this.#orderMenus.values()).filter((orderMenu) => orderMenu.category === categoryName); + } + #setOrderMenus(orderMenus) { + const { orderMenuNameList, orderMenuCountList } = Order.parseOrders(orderMenus); + + orderMenuNameList.forEach((menuName, index) => { + const category = this.#getCategoryByMenuName(menuName); + const key = this.#getKeyByMenuName(menuName); + + this.#orderMenus.set(key, { + name: menuName, + count: orderMenuCountList[index], + category, + }); + }); + } + #getOrderMenuCategories() { + return Array.from(new Set(Array.from(this.#menus.values()).map((menu) => menu.CATEGORY))); + } + #getCategoryByMenuName(menuName) { + return Array.from(this.#menus.values()).find((menuMap) => menuMap.NAME === menuName).CATEGORY; + } + #getKeyByMenuName(menuName) { + return Array.from(this.#menus.keys()).find((key) => this.#menus.get(key).NAME === menuName); + } +} +export default Order;
JavaScript
์ œ๊ฐ€ ์ž˜ ๋ชป ์ฐพ๋Š” ๊ฑด์ง€ ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ, ์ด orderDate๋Š” Order ๋„๋ฉ”์ธ ๋‚ด์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋กœ์ง์ด ์—†๋Š” ๊ฒƒ ๊ฐ™์•„์š”. ์—ญํ• ์„ ๋ถ„๋ฆฌํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ๋А๊ปด์ง€๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋Š”์ง€ ๊ถ๊ธˆํ•ด์š”..!
@@ -0,0 +1,126 @@ +import { isDuplicate, isEveryInclude, isMoreThanLimit, isNotMatchRegex, isSomeNotInclude } from '../util/index.js'; +import { LIMIT_ORDER_COUNT, MENU_CATEGORIES, MENUS, ORDER_REGEX } from '../constant/index.js'; + +const ERROR_MESSAGE = Object.freeze({ + INVALID_DATE: '์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + IS_INVALID_ORDER: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', +}); + +const { INVALID_DATE, IS_INVALID_ORDER } = ERROR_MESSAGE; + +class Order { + static validateOrder(orderMenus) { + const validators = [ + () => Order.validateDuplicationOrder(orderMenus), + () => Order.validateOrderMenuFormat(orderMenus, ORDER_REGEX.ORDER_FORMAT), + () => Order.validateLimitOrderCount(orderMenus, LIMIT_ORDER_COUNT), + () => Order.validateIncludeMenu(orderMenus, MENUS), + () => Order.validateOrderOnlyOneCategory(orderMenus, MENUS, MENU_CATEGORIES.BEVERAGE), + ]; + + validators.forEach((validator) => validator()); + } + static validateDate(date) { + const validators = [() => Order.validateDayFormat(date, ORDER_REGEX.DAY_FORMAT)]; + + validators.forEach((validator) => validator()); + } + static validateOrderOnlyOneCategory(orderMenus, menus, category) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + const menuNameListForCategory = Object.values(menus) + .filter((menu) => menu.CATEGORY === category) + .map((menu) => menu.NAME); + + isEveryInclude(IS_INVALID_ORDER, orderMenuNameList, menuNameListForCategory); + } + static validateIncludeMenu(orderMenus, menus) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + const menuNameList = Object.values(menus).map((menu) => menu.NAME); + + isSomeNotInclude(IS_INVALID_ORDER, orderMenuNameList, menuNameList); + } + static validateDuplicationOrder(orderMenus) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + + isDuplicate(IS_INVALID_ORDER, orderMenuNameList); + } + static validateLimitOrderCount(orderMenus, limitOrderCount) { + const { totalOrderCount } = Order.parseOrders(orderMenus); + + isMoreThanLimit(IS_INVALID_ORDER, totalOrderCount, limitOrderCount); + } + static validateOrderMenuFormat(orderMenus, regex) { + const { orderMenuList } = Order.parseOrders(orderMenus); + + orderMenuList.forEach((orderMenu) => isNotMatchRegex(IS_INVALID_ORDER, orderMenu, regex)); + } + static validateDayFormat(date, regex) { + isNotMatchRegex(INVALID_DATE, date, regex); + } + static parseOrders(orderMenu) { + const orderMenuList = orderMenu.split(',').map((menu) => menu.trim()); + const orderMenuNameList = orderMenuList.map((menu) => menu.split('-')[0]); + const orderMenuCountList = orderMenuList.map((menu) => menu.split('-')[1]); + const totalOrderCount = orderMenuCountList.reduce((acc, cur) => acc + Number(cur), 0); + + return { + orderMenuList, + orderMenuNameList, + orderMenuCountList, + totalOrderCount, + }; + } + + #orderDate; + #orderMenus = new Map(); + #menus = new Map(); + + constructor(menus, orderMenus, orderDate) { + this.#menus = new Map(Object.entries(menus)); + this.#orderDate = orderDate; + this.#setOrderMenus(orderMenus); + } + + getOrderMenuList() { + return this.#getOrderMenuCategories() + .map((categoryName) => this.getOrderMenuByCategory(categoryName)) + .flat(); + } + getTotalPrice() { + return Array.from(this.#orderMenus.values()).reduce((acc, orderMenu) => { + const key = this.#getKeyByMenuName(orderMenu.name); + const menu = this.#menus.get(key); + return acc + menu.PRICE * orderMenu.count; + }, 0); + } + getOrderDate() { + return this.#orderDate; + } + getOrderMenuByCategory(categoryName) { + return Array.from(this.#orderMenus.values()).filter((orderMenu) => orderMenu.category === categoryName); + } + #setOrderMenus(orderMenus) { + const { orderMenuNameList, orderMenuCountList } = Order.parseOrders(orderMenus); + + orderMenuNameList.forEach((menuName, index) => { + const category = this.#getCategoryByMenuName(menuName); + const key = this.#getKeyByMenuName(menuName); + + this.#orderMenus.set(key, { + name: menuName, + count: orderMenuCountList[index], + category, + }); + }); + } + #getOrderMenuCategories() { + return Array.from(new Set(Array.from(this.#menus.values()).map((menu) => menu.CATEGORY))); + } + #getCategoryByMenuName(menuName) { + return Array.from(this.#menus.values()).find((menuMap) => menuMap.NAME === menuName).CATEGORY; + } + #getKeyByMenuName(menuName) { + return Array.from(this.#menus.keys()).find((key) => this.#menus.get(key).NAME === menuName); + } +} +export default Order;
JavaScript
๋ฉ”์„œ๋“œ๋งˆ๋‹ค ๊ฐœํ–‰์„ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๊ฐ€๋…์„ฑ์ด ๋†’์•„์งˆ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -1,5 +1,42 @@ +import { InputView, OutputView } from './view/index.js'; +import { MENUS, PROMOTION_MONTH, PROMOTION_YEAR } from './constant/index.js'; +import { Order, Planner, PromotionCalendar, Promotions } from './model/index.js'; + class App { - async run() {} + async run() { + OutputView.printGreetings(); + const { order, planner } = await this.reserve(); + OutputView.printPreviewMessage(); + await this.preview(order, planner); + } + + async reserve() { + const orderDate = await InputView.readOrderDate((input) => Order.validateDate(input)); + const orderMenu = await InputView.readOrderMenus((input) => Order.validateOrder(input)); + const order = new Order(MENUS, orderMenu, orderDate); + const planner = new Planner(order, new PromotionCalendar(PROMOTION_YEAR, PROMOTION_MONTH, Promotions)); + + return { + order, + planner, + }; + } + + async preview(order, planner) { + const orderMenuList = order.getOrderMenuList(); + const totalPriceWithoutDiscount = order.getTotalPrice(); + const promotions = planner.getPromotionsByOrderDate(); + const totalBenefitPrice = planner.getTotalBenefitPrice(); + const totalPriceWithDiscount = planner.getTotalPriceWithDiscount(); + const badge = planner.getBadge(); + OutputView.printOrderMenus(orderMenuList); + OutputView.printTotalPriceWithoutDiscount(totalPriceWithoutDiscount); + OutputView.printGiveWayMenu(promotions); + OutputView.printBenefitDetails(promotions); + OutputView.printTotalBenefitPrice(totalBenefitPrice); + OutputView.printTotalPriceWithDiscount(totalPriceWithDiscount); + OutputView.printBadge(badge); + } } export default App;
JavaScript
์ง€๊ทนํžˆ ๊ฐœ์ธ์ ์ธ ์˜๊ฒฌ์ธ๋ฐ, ๋ฉ”์„œ๋“œ๋ช…์„ '์˜ˆ์•ฝ'์ด๋ผ๊ณ  ์ง€์œผ์…จ๋Š”๋ฐ oderDate๋Š” ์กฐ๊ธˆ ํ˜ผ๋ž€์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š”. '๋ฐฉ๋ฌธ'์ด๋‚˜ '์˜ˆ์•ฝ'์œผ๋กœ ๋ณ€๊ฒฝํ•˜์‹œ๋Š” ๊ฒŒ ์–ด๋–จ๊นŒ์š”..?
@@ -0,0 +1,126 @@ +import { isDuplicate, isEveryInclude, isMoreThanLimit, isNotMatchRegex, isSomeNotInclude } from '../util/index.js'; +import { LIMIT_ORDER_COUNT, MENU_CATEGORIES, MENUS, ORDER_REGEX } from '../constant/index.js'; + +const ERROR_MESSAGE = Object.freeze({ + INVALID_DATE: '์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', + IS_INVALID_ORDER: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.', +}); + +const { INVALID_DATE, IS_INVALID_ORDER } = ERROR_MESSAGE; + +class Order { + static validateOrder(orderMenus) { + const validators = [ + () => Order.validateDuplicationOrder(orderMenus), + () => Order.validateOrderMenuFormat(orderMenus, ORDER_REGEX.ORDER_FORMAT), + () => Order.validateLimitOrderCount(orderMenus, LIMIT_ORDER_COUNT), + () => Order.validateIncludeMenu(orderMenus, MENUS), + () => Order.validateOrderOnlyOneCategory(orderMenus, MENUS, MENU_CATEGORIES.BEVERAGE), + ]; + + validators.forEach((validator) => validator()); + } + static validateDate(date) { + const validators = [() => Order.validateDayFormat(date, ORDER_REGEX.DAY_FORMAT)]; + + validators.forEach((validator) => validator()); + } + static validateOrderOnlyOneCategory(orderMenus, menus, category) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + const menuNameListForCategory = Object.values(menus) + .filter((menu) => menu.CATEGORY === category) + .map((menu) => menu.NAME); + + isEveryInclude(IS_INVALID_ORDER, orderMenuNameList, menuNameListForCategory); + } + static validateIncludeMenu(orderMenus, menus) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + const menuNameList = Object.values(menus).map((menu) => menu.NAME); + + isSomeNotInclude(IS_INVALID_ORDER, orderMenuNameList, menuNameList); + } + static validateDuplicationOrder(orderMenus) { + const { orderMenuNameList } = Order.parseOrders(orderMenus); + + isDuplicate(IS_INVALID_ORDER, orderMenuNameList); + } + static validateLimitOrderCount(orderMenus, limitOrderCount) { + const { totalOrderCount } = Order.parseOrders(orderMenus); + + isMoreThanLimit(IS_INVALID_ORDER, totalOrderCount, limitOrderCount); + } + static validateOrderMenuFormat(orderMenus, regex) { + const { orderMenuList } = Order.parseOrders(orderMenus); + + orderMenuList.forEach((orderMenu) => isNotMatchRegex(IS_INVALID_ORDER, orderMenu, regex)); + } + static validateDayFormat(date, regex) { + isNotMatchRegex(INVALID_DATE, date, regex); + } + static parseOrders(orderMenu) { + const orderMenuList = orderMenu.split(',').map((menu) => menu.trim()); + const orderMenuNameList = orderMenuList.map((menu) => menu.split('-')[0]); + const orderMenuCountList = orderMenuList.map((menu) => menu.split('-')[1]); + const totalOrderCount = orderMenuCountList.reduce((acc, cur) => acc + Number(cur), 0); + + return { + orderMenuList, + orderMenuNameList, + orderMenuCountList, + totalOrderCount, + }; + } + + #orderDate; + #orderMenus = new Map(); + #menus = new Map(); + + constructor(menus, orderMenus, orderDate) { + this.#menus = new Map(Object.entries(menus)); + this.#orderDate = orderDate; + this.#setOrderMenus(orderMenus); + } + + getOrderMenuList() { + return this.#getOrderMenuCategories() + .map((categoryName) => this.getOrderMenuByCategory(categoryName)) + .flat(); + } + getTotalPrice() { + return Array.from(this.#orderMenus.values()).reduce((acc, orderMenu) => { + const key = this.#getKeyByMenuName(orderMenu.name); + const menu = this.#menus.get(key); + return acc + menu.PRICE * orderMenu.count; + }, 0); + } + getOrderDate() { + return this.#orderDate; + } + getOrderMenuByCategory(categoryName) { + return Array.from(this.#orderMenus.values()).filter((orderMenu) => orderMenu.category === categoryName); + } + #setOrderMenus(orderMenus) { + const { orderMenuNameList, orderMenuCountList } = Order.parseOrders(orderMenus); + + orderMenuNameList.forEach((menuName, index) => { + const category = this.#getCategoryByMenuName(menuName); + const key = this.#getKeyByMenuName(menuName); + + this.#orderMenus.set(key, { + name: menuName, + count: orderMenuCountList[index], + category, + }); + }); + } + #getOrderMenuCategories() { + return Array.from(new Set(Array.from(this.#menus.values()).map((menu) => menu.CATEGORY))); + } + #getCategoryByMenuName(menuName) { + return Array.from(this.#menus.values()).find((menuMap) => menuMap.NAME === menuName).CATEGORY; + } + #getKeyByMenuName(menuName) { + return Array.from(this.#menus.keys()).find((key) => this.#menus.get(key).NAME === menuName); + } +} +export default Order;
JavaScript
์•„ static method๋ผ ์œ„๋กœ ๋บด๋†จ์Šต๋‹ˆ๋‹ค. static์€ ๊ฐ ๊ฐ์ฒด๋งˆ๋‹ค์˜ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ณ  ํ•จ์ˆ˜๋กœ์„œ์˜ ๊ธฐ๋Šฅ๋งŒ ๊ฐ€๋Šฅํ•ด์„œ ์œ„๋กœ ๋บด๋†จ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋กœ Validation ๋นผ๊ธฐ์—๋Š” ๋ชจ๋‘ ์˜ˆ์•ฝ๊ด€์ ์—์„œ ์œ ํšจ์„ฑ ๊ฒ€์ฆ์ด๋ผ Order์— ๋ชจ๋‘ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค. view์—์„œ ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ๋„ ์žˆ์—ˆ๋Š”๋ฐ ์˜ˆ์•ฝ์— ๊ด€๋ จ๋œ ๊ฒ€์ฆ์ด๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค