code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,29 @@ +import java.util.Random; +import java.util.List; + +public class RacingGame { + private static final int RANDOM_RANGE = 10; + private static final int MIN_MOVE_STANDARD = 4; + + public void raceByTime(List<Car> cars) { + for (Car car : cars) { + int randomValue = giveRandomValue(); + move(car, randomValue); + } + } + + private int giveRandomValue() { + Random random = new Random(); + return random.nextInt(RANDOM_RANGE); + } + + private void move(Car car, int randomValue) { + if (canMove(randomValue)) { + car.run(); + } + } + + private boolean canMove(int number) { + return number >= MIN_MOVE_STANDARD; + } +} \ No newline at end of file
Java
๋ฉ”์„œ๋“œ์˜ ์ ‘๊ทผ์ œ์–ด์ž์— ๋ชฐ๋‘ํ•œ ๋‚˜๋จธ์ง€ ๋ณ€์ˆ˜์˜ ์ ‘๊ทผ์ œ์–ด์ž๋Š” ๋ฏธ์ฒ˜ ๊ณ ๋ คํ•˜์ง€ ๋ชปํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ƒ๋žตํ•  ๊ฒฝ์šฐ default ์ ‘๊ทผ์ œ์–ด์ž๊ฐ€ ๋˜์–ด ํ•ด๋‹น **ํŒจํ‚ค์ง€** ๋‚ด์—์„œ ์ ‘๊ทผ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” ํ•ด๋‹น ํด๋ž˜์Šค ๋‚ด์—์„œ๋งŒ์˜ ์ ‘๊ทผ์ด ํ•„์š”ํ•˜๋ฏ€๋กœ private๋ฅผ ๋ถ™์—ฌ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,22 @@ +import java.util.ArrayList; +import java.util.List; + +public class GameWinner { + public List<String> findWinner(List<Car> cars) { + int max = cars.get(0).position; + for (int i = 1; i < cars.size(); i++) { + max = Math.max(max, cars.get(i).position); + } + List<String> winnerList = new ArrayList<>(); + for (Car car : cars) { + addWinnerList(car, max, winnerList); + } + return winnerList; + } + + private void addWinnerList(Car car, int max, List<String> winnerList) { + if (car.position == max) { + winnerList.add(car.name); + } + } +}
Java
์ž๋ฃŒ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค๐Ÿ‘ List์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,8 @@ +public class Car { + public String name; + public int position; + + public int run() { + return this.position++; + } +} \ No newline at end of file
Java
car ์˜ ์ƒํƒœ๊ฐ€ ์™ธ๋ถ€์—์„œ ์ ‘๊ทผ๊ฐ€๋Šฅํ•œ public ์ธ๋ฐ์š”~ private ์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์…”์•ผํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค !
@@ -0,0 +1,22 @@ +import java.util.ArrayList; +import java.util.List; + +public class GameWinner { + public List<String> findWinner(List<Car> cars) { + int max = cars.get(0).position; + for (int i = 1; i < cars.size(); i++) { + max = Math.max(max, cars.get(i).position); + } + List<String> winnerList = new ArrayList<>(); + for (Car car : cars) { + addWinnerList(car, max, winnerList); + } + return winnerList; + } + + private void addWinnerList(Car car, int max, List<String> winnerList) { + if (car.position == max) { + winnerList.add(car.name); + } + } +}
Java
Winner ๊ฐ์ฒด์—์„œ ๋“ค๊ณ  ์žˆ์œผ๋ฉด ์ข‹์„๋งŒํ•œ ์ƒํƒœ(ํ•„๋“œ ๋ณ€์ˆ˜)๋“ค์„ ์ •์˜ํ•˜๊ณ  ์ฑ…์ž„์„ ๋ถ€์—ฌํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,22 @@ +import java.util.ArrayList; +import java.util.List; + +public class GameWinner { + public List<String> findWinner(List<Car> cars) { + int max = cars.get(0).position; + for (int i = 1; i < cars.size(); i++) { + max = Math.max(max, cars.get(i).position); + } + List<String> winnerList = new ArrayList<>(); + for (Car car : cars) { + addWinnerList(car, max, winnerList); + } + return winnerList; + } + + private void addWinnerList(Car car, int max, List<String> winnerList) { + if (car.position == max) { + winnerList.add(car.name); + } + } +}
Java
๊ฐ์ฒด์ง€ํ–ฅ์—์„œ๋Š” ๊ฐ์ฒด์˜ ์ƒํƒœ์— ์ง์ ‘ ์ ‘๊ทผํ•ด์„œ๋Š” ์•ˆ๋ฉ๋‹ˆ๋‹ค. private ์œผ๋กœ ๋ณ€๊ฒฝํ•˜์‹œ๋ฉด getter ๋กœ ๋ณ€๊ฒฝํ•˜์…”์•ผํ•  ๊ฒƒ ๊ฐ™์•„์š”~
@@ -0,0 +1,22 @@ +import java.util.ArrayList; +import java.util.List; + +public class GameWinner { + public List<String> findWinner(List<Car> cars) { + int max = cars.get(0).position; + for (int i = 1; i < cars.size(); i++) { + max = Math.max(max, cars.get(i).position); + } + List<String> winnerList = new ArrayList<>(); + for (Car car : cars) { + addWinnerList(car, max, winnerList); + } + return winnerList; + } + + private void addWinnerList(Car car, int max, List<String> winnerList) { + if (car.position == max) { + winnerList.add(car.name); + } + } +}
Java
java8 ์˜ stream api ๋ฅผ ์ ์šฉํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”~?
@@ -0,0 +1,50 @@ +import java.util.Scanner; +import java.util.List; +import java.util.ArrayList; + +public class InputView { + private static final int INITIAL_POSITION = 0; + private static final int MIN_NAME_LENGTH = 1; + private static final int MAX_NAME_LENGTH = 5; + + public String inputCarName() { + Scanner scanner = new Scanner(System.in); + System.out.println("๊ฒฝ์ฃผํ•  ์ž๋™์ฐจ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”(์ด๋ฆ„์€ ์‰ผํ‘œ(,) ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„)."); + return scanner.nextLine(); + } + + public List<Car> registerCar(String inputName) { + String[] names = inputName.split(","); + List<Car> cars = new ArrayList<>(); + for (int i = 0; i < names.length; i++) { + checkValidName(names[i]); + cars.add(new Car()); + cars.get(i).name = names[i]; + cars.get(i).position = INITIAL_POSITION; + } + return cars; + } + + public int inputTime() { + Scanner scanner = new Scanner(System.in); + System.out.println("์‹œ๋„ํ•  ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ์ธ๊ฐ€์š”?"); + return scanner.nextInt(); + } + + private void checkValidName(String name) { + try { + checkNameLength(name); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void checkNameLength(String name) throws Exception { + if (name.length() < MIN_NAME_LENGTH || name.length() > MAX_NAME_LENGTH) { + throw new InputNameException(name); + } + if (name.trim().isEmpty()) { + throw new InputNameException(); + } + } +}
Java
InputView ์—๋Š” ์ƒํƒœ๊ฐ’์ด ์—†๋Š”๋ฐ์š”, util ํด๋ž˜์Šค๋กœ ํ™œ์šฉ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์„œ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋“ค์— static ์„ ํ™œ์šฉํ•ด๋ณด์‹œ๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,29 @@ +import java.util.Random; +import java.util.List; + +public class RacingGame { + private static final int RANDOM_RANGE = 10; + private static final int MIN_MOVE_STANDARD = 4; + + public void raceByTime(List<Car> cars) { + for (Car car : cars) { + int randomValue = giveRandomValue(); + move(car, randomValue); + } + } + + private int giveRandomValue() { + Random random = new Random(); + return random.nextInt(RANDOM_RANGE); + } + + private void move(Car car, int randomValue) { + if (canMove(randomValue)) { + car.run(); + } + } + + private boolean canMove(int number) { + return number >= MIN_MOVE_STANDARD; + } +} \ No newline at end of file
Java
random ์œผ๋กœ ์ธํ•ด ๊ฒŒ์ž„์˜ ํ•ต์‹ฌ pulbic ๋ฉ”์„œ๋“œ๊ฐ€ ํ…Œ์ŠคํŠธ ๋ถˆ๊ฐ€๋Šฅํ•ด์กŒ๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ๊ฐœ์„ ํ•ด ๋ณผ ์ˆ˜ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,21 @@ +public class Car { + private String name; + private int position; + + Car(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void goForward() { + this.position++; + } +}
Java
๊ทœ์น™ : setter ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค (getter ์‚ฌ์šฉ์€ ์ง€์–‘ํ•œ๋‹ค) ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ setter ์™€ getter ์‚ฌ์šฉ์„ ๋˜๋„๋ก ์ง€์–‘ํ•˜๋ผ๊ณ  ํ•˜๋Š” ์ด์œ ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ์š”?
@@ -0,0 +1,21 @@ +public class Car { + private String name; + private int position; + + Car(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void goForward() { + this.position++; + } +}
Java
๋žœ๋ค์„ ํ†ตํ•ด ํŠน์ • ์กฐ๊ฑด์—์„œ ์ „์ง„ํ•˜๋Š” ๊ฒŒ์ž„์˜ ๋ฃฐ์„ ์ž๋™์ฐจ๋ผ๋Š” ๋ชจ๋ธ ํด๋ž˜์Šค๊ฐ€ ์•Œ๊ณ  ์žˆ์„ ํ•„์š”๊ฐ€ ์žˆ์„๊นŒ์š”? ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฒŒ์ž„์˜ ๋ชจ๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜์–ด์„œ ๋žœ๋ค ๋ฃฐ์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๊ฒŒ์ž„ ๋ฃฐ์ด ์ƒ๊ธด๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ๋ชจ๋ธ์ด ๊ฒŒ์ž„์˜ ๋ฃฐ์— ๊ด€๊ณ„์—†์ด pure ํ•˜๊ฒŒ ์งœ์—ฌ์ ธ ์žˆ์œผ๋ฉด ๋ชจ๋ธ์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š์•„๋„ ๋˜์ง€ ์•Š์„๊นŒ์š”? moveForward ๊ฐ€ ๋‹จ์ˆœํžˆ car ์˜ position ์„ ++ ํ•˜๋Š” ๋กœ์ง๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด ์–ด๋–ค์ง€ ์˜๊ฒฌ๋“œ๋ ค๋ด…๋‹ˆ๋‹ค ~
@@ -0,0 +1,21 @@ +public class Car { + private String name; + private int position; + + Car(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void goForward() { + this.position++; + } +}
Java
๋‹ค์Œ์—๋Š” code formatting ์„ ํ•œ ๋’ค์— ์ œ์ถœํ•˜๋Š” ์Šต๊ด€์„ ๋“ค์ด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” ~ :)
@@ -0,0 +1,21 @@ +public class Car { + private String name; + private int position; + + Car(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void goForward() { + this.position++; + } +}
Java
10, 4 ๊ฐ€ ์–ด๋–ค ์˜๋ฏธ๋ฅผ ์ง€๋‹ˆ๊ณ  ์žˆ๋Š”์ง€ 2๋…„ ๋’ค์— ์ด ์ฝ”๋“œ๋ฅผ ๋ณด๊ฑฐ๋‚˜ ํƒ€์ธ์ด ๋ณด๋ฉด ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”. ์ด๋Ÿฐ magic number ๋ฅผ ์ƒ์ˆ˜๋กœ ์ถ”์ถœํ•ด๋ณด๋Š”๊ฒŒ ์–ด๋–จ๊นŒ์š”?
@@ -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
primitive type ์ด ์•„๋‹ˆ๋ผ wrapper type ์œผ๋กœ ์„ ์–ธํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -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
์ ‘๊ทผ์ œ์–ด์ž๊ฐ€ ๋น ์ง„ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? ์ ‘๊ทผ์ œ์–ด์ž๋ฅผ ์ƒ๋žตํ•˜๋ฉด ์–ด๋–ค ์ ‘๊ทผ์ œ์–ด์ž๊ฐ€ ์ ์šฉ๋ ๊นŒ์š”?
@@ -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
InputView ๊ฐ์ฒด๋Š” ์ƒํƒœ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๋Š” util ํด๋ž˜์Šค์ฒ˜๋Ÿผ ๋น„์ถฐ์ง€๋Š”๋ฐ static ๋ฉ”์„œ๋“œ๋“ค๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ๊ฐ์ฒด ์ƒ์„ฑ ๋น„์šฉ์„ ์ ˆ๊ฐ์‹œ์ผœ๋ณด๋Š”๊ฑด ์–ด๋–ค๊ฐ€์š”?
@@ -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
์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ฝ”๋“œ๋Š” ๊ณผ๊ฐํžˆ ์‚ญ์ œํ•˜์‹œ๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -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
์ค„๋ฐ”๊ฟˆ, ์ŠคํŽ˜์ด์Šค๋„ ์ปจ๋ฒค์…˜์ž…๋‹ˆ๋‹ค. ๋ฌธ๋งฅ์ด ๋‹ฌ๋ผ์ง„๋‹ค๋˜๊ฐ€, ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ ์‚ฌ์ด ๋“ฑ์„ ํ•œ๋ฒˆ์˜ ์ค„๋ฐ”๊ฟˆ์œผ๋กœ ๊ตฌ๋ถ„ ์ง€์–ด์„œ ๊ฐ€๋…์„ฑ์„ ๋†’์—ฌ๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -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
์ด ๋ถ€๋ถ„์— java 8 ์˜ stream api ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -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
์„ ์–ธ๊ณผ ํ• ๋‹น์ด ๋ถ„๋ฆฌ๋œ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,27 @@ +import java.util.List; + +public class OutputView { + private static final String DRIVE = "-"; + + public void resultMessage() { + System.out.println("\n์‹คํ–‰ ๊ฒฐ๊ณผ"); + } + + public void oneTrialMessage(List<Car> cars) { + for(Car car: cars) { + System.out.println(car.getName() + ": " + raceOneTrial(car)); + } + } + + private StringBuilder raceOneTrial(Car car) { + StringBuilder goSignal = new StringBuilder(); + for(int i = 0; i<car.getPosition(); i++) { + goSignal.append(DRIVE); + } + return goSignal; + } + + public void getWinnerMessage(List<String> winnernames) { + System.out.println(String.join(",", winnernames + "๊ฐ€ ์ตœ์ข… ์šฐ์Šนํ–ˆ์Šต๋‹ˆ๋‹ค.")); + } +}
Java
๊ฒŒ์ž„ ์‹œ์ž‘์„ ์œ„ํ•œ input ๊ณผ ๊ด€๋ จ๋œ print ๋Š” inputview ์— ์žˆ๋Š”๊ฒŒ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,27 @@ +import java.util.List; + +public class OutputView { + private static final String DRIVE = "-"; + + public void resultMessage() { + System.out.println("\n์‹คํ–‰ ๊ฒฐ๊ณผ"); + } + + public void oneTrialMessage(List<Car> cars) { + for(Car car: cars) { + System.out.println(car.getName() + ": " + raceOneTrial(car)); + } + } + + private StringBuilder raceOneTrial(Car car) { + StringBuilder goSignal = new StringBuilder(); + for(int i = 0; i<car.getPosition(); i++) { + goSignal.append(DRIVE); + } + return goSignal; + } + + public void getWinnerMessage(List<String> winnernames) { + System.out.println(String.join(",", winnernames + "๊ฐ€ ์ตœ์ข… ์šฐ์Šนํ–ˆ์Šต๋‹ˆ๋‹ค.")); + } +}
Java
๋ฉ”์„œ๋“œ ์ด๋ฆ„์ด ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๊ณ  ๋ช…์‚ฌ๋„ค์š” ~
@@ -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
๋ฆฌํ„ด์„ ์ด๋ ‡๊ฒŒ ์ ์œผ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -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
trim ๊ณผ checkNameLength ๋‘๊ฐœ์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋„ค์š” ๋ฉ”์„œ๋“œ๋“ค์„ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„ค์ด๋ฐ ๋ณ€๊ฒฝ, ๋‹จ์ผ์ฑ…์ž„์›์น™์„ ์ง€์ผœ์„œ ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -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
return scanner.nextInt(); ๋กœ ์ถฉ๋ถ„ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ~ ๊ทธ๋ฆฌ๊ณ  input() ๋ฉ”์„œ๋“œ๊ฐ€ inputCount() ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•˜๋Š” ๋“ฏํ•œ ๋„ค์ด๋ฐ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ๋‘˜์˜ ๊ตฌ๋ถ„์„ ์œ„ํ•ด ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ์ข€ ๋ณ€๊ฒฝํ•ด๋ณด์‹œ๋ฉด ์–ด๋–จ๊นŒ์š”? (Count ๋ผ๋Š” ๋„ค์ด๋ฐ์€ ๋„ˆ๋ฌด ๊ด‘๋ฒ”์œ„ํ•œ ๋‹จ์–ด์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‹œ๋„ ํšŸ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋” ๊ดœ์ฐฎ์€ ๋„ค์ด๋ฐ์€ ์—†์„๊นŒ์š”?)
@@ -0,0 +1,27 @@ +import java.util.List; + +public class OutputView { + private static final String DRIVE = "-"; + + public void resultMessage() { + System.out.println("\n์‹คํ–‰ ๊ฒฐ๊ณผ"); + } + + public void oneTrialMessage(List<Car> cars) { + for(Car car: cars) { + System.out.println(car.getName() + ": " + raceOneTrial(car)); + } + } + + private StringBuilder raceOneTrial(Car car) { + StringBuilder goSignal = new StringBuilder(); + for(int i = 0; i<car.getPosition(); i++) { + goSignal.append(DRIVE); + } + return goSignal; + } + + public void getWinnerMessage(List<String> winnernames) { + System.out.println(String.join(",", winnernames + "๊ฐ€ ์ตœ์ข… ์šฐ์Šนํ–ˆ์Šต๋‹ˆ๋‹ค.")); + } +}
Java
์—ฌ๊ธฐ์„œ๋Š” StringBuilder ๋กœ ๋ฐ›๋Š” ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,39 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class RacingGame { + private List<String> winnernames = new ArrayList<>(); + + public void race(List<Car> cars) { + for(Car car: cars) { + goOrStay(car); + } + } + + private void goOrStay(Car car) { + if(Rule.isGoForward()) { + car.goForward(); + } + } + + public List<String> getWinner(List<Car> cars) { + int positionOfWinner = findPositionOfWinner(cars); + cars = cars.stream().filter(car -> (car.getPosition() == positionOfWinner)).collect(Collectors.toList()); + for (Car car : cars) { + this.winnernames.add(car.getName()); + } + return winnernames; + } + + private int findPositionOfWinner(List<Car> cars) { + List<Integer> position = new ArrayList<>(); + int positionOfWinner; + for(Car car: cars) { + position.add(car.getPosition()); + } + positionOfWinner = Collections.max(position); + return positionOfWinner; + } +} \ No newline at end of file
Java
์ „์ฒด์ ์œผ๋กœ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๊ฐ€ ์—†์–ด์„œ ์•„์‰ฝ๋„ค์š” ใ… ใ… 
@@ -0,0 +1,21 @@ +public class Car { + private String name; + private int position; + + Car(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void goForward() { + this.position++; + } +}
Java
๋ถˆํ•„์š”ํ•œ setter/getter๋Š” ๊ฐ์ฒด์˜ ์†์„ฑ์„ ์–ธ์ œ๋“  ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ง€์–‘ํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๊ตณ์ด ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋ฉด ๋ฐ˜์˜ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,21 @@ +public class Car { + private String name; + private int position; + + Car(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void goForward() { + this.position++; + } +}
Java
๊ฒŒ์ž„์˜ ๋ฃฐ์„ ์ž๋™์ฐจ ๋ชจ๋ธ ํด๋ž˜์Šค๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ์„ ํ•„์š”๋Š” ์—†๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๋ณด๋‹ค ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์„ ์•„์ง ์ƒ๊ฐํ•ด๋ณด์ง€ ๋ชปํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์›๋ž˜๋Š” Rule ํด๋ž˜์Šค๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ–ˆ์œผ๋‚˜ ์•„์ง ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผ ์ค‘์— ์žˆ์Šต๋‹ˆ๋‹ค! ๊ณ ๋ฏผํ•ด์„œ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋…ธ๋ ฅํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,21 @@ +public class Car { + private String name; + private int position; + + Car(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void goForward() { + this.position++; + } +}
Java
๋„ต code formatting ํ•˜๋Š” ์Šต๊ด€์„ ๋“ค์ด๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,21 @@ +public class Car { + private String name; + private int position; + + Car(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void goForward() { + this.position++; + } +}
Java
magic number ์—ญ์‹œ ์ถ”ํ›„์— ๋ดค์„ ๋•Œ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•˜๋Š” ์Šต๊ด€์„ ๋“ค์ด๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -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 ํ•„๋“œ ์ œ์™ธํ•˜๋Š” ๊ฒƒ ๋ฐ˜์˜ํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -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ํŒจํ„ด์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ํ”„๋ฆฌ์ฝ”์Šค๋ฅผ ์‹œ์ž‘ํ•˜๋ฉด์„œ ์ ‘ํ–ˆ๋Š”๋ฐ ์–„ํŒํ•œ ์ง€์‹์ด ์ž˜๋ชป๋œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์—ˆ๋„ค์š” ใ…Žใ…Ž;; ๋‹ค์‹œ ๊ณต๋ถ€ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -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์˜ ์ƒ์„ฑ๋ฐฉ์‹์— ๋Œ€ํ•ด์„œ ์ƒˆ๋กญ๊ฒŒ ๊ณ ๋ฏผํ•˜๊ฒŒ ๋˜์—ˆ๋„ค์š”