code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,8 @@
+public class Car {
+ public String name;
+ public int position;
+
+ public int run() {
+ return this.position++;
+ }
+}
\ No newline at end of file | Java | ๋ฉ์๋๋ช
์ ์ง์ ๋ ์กฐ๊ฑด ๋ณ๊ฒฝ์ ๊ฐ๋ฅ์ฑ์ ๊ผญ ๊ณ ๋ คํ๋๋ก ํ๊ฒ ์ต๋๋ค! ๋จ๋ฒ์ ์ดํด๋๋ ์ข์ ์์ ์ ์ํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค. |
@@ -0,0 +1,8 @@
+public class Car {
+ public String name;
+ public int position;
+
+ public int run() {
+ return this.position++;
+ }
+}
\ No newline at end of file | Java | GUI ํ๋ก๊ทธ๋จ์ผ๋ก ํ์ฅ๋ ๊ฒฝ์ฐ๋ ๊ณ ๋ คํด์ผ ํ๋ค๋ ์ ๋ช
์ฌํ๊ฒ ์ต๋๋ค! |
@@ -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 ์ ๋ํด์ ์ถ๋ ฅ๋ง ํด์ฃผ๋ ์ ๋์ ์ญํ ์ ํ๋๋ก ๋ณ๊ฒฝํด๋ณด๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,37 @@
+package christmas.service;
+
+import christmas.domain.Order;
+import christmas.domain.constant.Message;
+import christmas.domain.constant.dish.Appetizer;
+import christmas.domain.constant.dish.Beverage;
+import christmas.domain.constant.dish.Dessert;
+import christmas.domain.constant.dish.MainDish;
+import christmas.domain.constant.dish.Orderable;
+import java.util.Map;
+import java.util.Objects;
+
+public class OrderMaker {
+ public Order make(Map<String, Integer> parsedOrder) {
+ Order order = new Order();
+ parsedOrder.forEach((dishLabel, count) -> order.addMenu(findDish(dishLabel), count));
+ order.validate();
+ return order;
+ }
+
+ private Orderable findDish(String input) {
+ if (Objects.nonNull(Appetizer.valueOfLabel(input))) {
+ return Appetizer.valueOfLabel(input);
+ }
+ if (Objects.nonNull(Beverage.valueOfLabel(input))) {
+ return Beverage.valueOfLabel(input);
+ }
+ if (Objects.nonNull(Dessert.valueOfLabel(input))) {
+ return Dessert.valueOfLabel(input);
+ }
+ if (Objects.nonNull(MainDish.valueOfLabel(input))) {
+ return MainDish.valueOfLabel(input);
+ }
+ throw new IllegalArgumentException(Message.INVALID_ORDER.getContent());
+ }
+
+} | Java | ๋ฉ๋ด enum ํ์
์ด ์์์ผ๋ฉด ํ ๋ฒ์ ์ฐพ์ ์ ์์๊ฑฐ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,38 @@
+package christmas.domain.constant.dish;
+
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public enum MainDish implements Orderable {
+ T_BONE_STEAK("ํฐ๋ณธ์คํ
์ดํฌ", 55000),
+ BARBEQUE_RIBS("๋ฐ๋นํ๋ฆฝ", 54000),
+ SEAFOOD_PASTA("ํด์ฐ๋ฌผํ์คํ", 35000),
+ CHRISTMAS_PASTA("ํฌ๋ฆฌ์ค๋ง์คํ์คํ", 25000);
+
+ private static final Map<String, MainDish> BY_LABEL =
+ Stream.of(MainDish.values()).collect(Collectors.toMap(MainDish::getLabel, e -> e));
+
+ private final String label;
+ private final int price;
+
+ MainDish(String label, int price) {
+ this.label = label;
+ this.price = price;
+ }
+
+ @Override
+ public String getLabel() {
+ return label;
+ }
+
+ @Override
+ public int getPrice() {
+ return price;
+ }
+
+ public static MainDish valueOfLabel(String label) {
+ return BY_LABEL.get(label);
+ }
+
+} | Java | static์ผ๋ก Map์ ์์ฑํด๋ ๊ฑฐ ์ข์ ๊ฑฐ ๊ฐ์ต๋๋ค.
๊ทธ๋ฐ๋ฐ BY_LABEL ์ด๋ฆ์ ๋๋ฌธ์๋ก ํ๋ ๊ฒ์ด ์ปจ๋ฒค์
์ธ์ง ์ ๋ ์ ๋ชจ๋ฅด๊ฒ ๋๋ผ๊ณ ์.
์์๋ ์๋๋ฐ static final์ด๊ธฐ๋ ํ๊ณ ๋ญ๊ฐ ๋ง์๊น์? |
@@ -0,0 +1,31 @@
+package christmas.service;
+
+import christmas.domain.DecemberDate;
+import christmas.domain.constant.Discount;
+import christmas.domain.constant.dish.Orderable;
+import christmas.domain.dto.BenefitDto;
+import christmas.service.discountcalculator.SpecialDiscountCalculator;
+import christmas.service.discountcalculator.WeekDiscountCalculator;
+import christmas.service.discountcalculator.XmasDdayDiscountCalculator;
+import java.util.Map;
+
+public class DiscountManager {
+ public BenefitDto applyDiscount(Map<Orderable, Integer> menus, DecemberDate visitDate) {
+ int totalCost = getTotalCost(menus);
+ BenefitDto benefitDto = new BenefitDto(menus, totalCost);
+ if (totalCost < Discount.DISCOUNT_APPLY_LOWER_BOUND.getValue()) {
+ return benefitDto;
+ }
+ XmasDdayDiscountCalculator.apply(benefitDto, visitDate);
+ WeekDiscountCalculator.apply(menus, benefitDto, visitDate);
+ SpecialDiscountCalculator.apply(benefitDto, visitDate);
+ return benefitDto;
+ }
+
+ private int getTotalCost(Map<Orderable, Integer> menus) {
+ return menus.entrySet().stream()
+ .mapToInt(entry -> entry.getKey().getPrice() * entry.getValue())
+ .sum();
+ }
+
+} | Java | menus๋ณด๋ค Order๋ฅผ ์ธ์๋ก ๋ฐ๋ ๊ฒ์ด ๋ ๊ฐ์ฒด์งํฅ์ ๋ง๋ค๊ณ ์๊ฐํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ Order ๋ด๋ถ์ getTotalCost ๋ฉ์๋๊ฐ ์๋ ๊ฒ์ด ์ด๋จ๊น์? ๊ทธ๋ฌ๋ฉด ์์ ์ ๋ฐ์ดํฐ๋ก ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค. |
@@ -0,0 +1,21 @@
+package christmas.service;
+
+import christmas.domain.constant.Benefit;
+import christmas.domain.dto.BenefitDto;
+
+public class PresentationManager {
+ private static final int PRESENTATION_THRESHOLD = 120000;
+ private static final int PRESENTATION_PRICE = 25000;
+
+ private PresentationManager() {
+
+ }
+
+ public static void present(BenefitDto benefitDto) {
+ if (benefitDto.getTotalCost() >= PRESENTATION_THRESHOLD) {
+ benefitDto.addBenefit(Benefit.PRESENTATION, PRESENTATION_PRICE);
+ benefitDto.presentChampagne();
+ }
+ }
+
+} | Java | 120_000 ์ฒ๋ผ _ ์ ๋ถ์ผ ์ ์์ต๋๋ค! |
@@ -0,0 +1,21 @@
+package christmas.service;
+
+import christmas.domain.constant.Benefit;
+import christmas.domain.dto.BenefitDto;
+
+public class PresentationManager {
+ private static final int PRESENTATION_THRESHOLD = 120000;
+ private static final int PRESENTATION_PRICE = 25000;
+
+ private PresentationManager() {
+
+ }
+
+ public static void present(BenefitDto benefitDto) {
+ if (benefitDto.getTotalCost() >= PRESENTATION_THRESHOLD) {
+ benefitDto.addBenefit(Benefit.PRESENTATION, PRESENTATION_PRICE);
+ benefitDto.presentChampagne();
+ }
+ }
+
+} | Java | private์ผ๋ก ์์ฑ์ ๋ง์์ค๋ ๊ฒ ์ข์ต๋๋ค! ๋ค๋ง ๋ด์ฉ์ด ์์ผ๋ฉด ๋น์ค๋ ์๋ ๊ฒ์ด ์ข์ต๋๋ค. |
@@ -0,0 +1,24 @@
+package christmas.service.discountcalculator;
+
+import static christmas.domain.constant.Discount.DATE_OF_CHRISTMAS;
+import static christmas.domain.constant.Discount.ONE_WEEK;
+import static christmas.domain.constant.Discount.SPECIAL_DISCOUNT_AMOUNT;
+import static christmas.domain.constant.Discount.THREE;
+
+import christmas.domain.DecemberDate;
+import christmas.domain.constant.Benefit;
+import christmas.domain.dto.BenefitDto;
+
+public class SpecialDiscountCalculator {
+ private SpecialDiscountCalculator() {
+
+ }
+
+ public static void apply(BenefitDto benefitDto, DecemberDate visitDate) {
+ if (visitDate.date() == DATE_OF_CHRISTMAS.getValue()
+ || visitDate.date() % ONE_WEEK.getValue() == THREE.getValue()) {
+ benefitDto.addBenefit(Benefit.SPECIAL_DISCOUNT, SPECIAL_DISCOUNT_AMOUNT.getValue());
+ }
+ }
+
+} | Java | ๊ธด ์กฐ๊ฑด์์ ๋ฉ์๋๋ก ๋ฝ๋ ๊ฒ์ด ๊ฐ๋
์ฑ์ ์ข์ต๋๋ค! |
@@ -0,0 +1,70 @@
+package christmas.service.discountcalculator;
+
+import static christmas.domain.constant.Discount.ONE;
+import static christmas.domain.constant.Discount.ONE_WEEK;
+import static christmas.domain.constant.Discount.TWO;
+import static christmas.domain.constant.Discount.WEEK_DISCOUNT_UNIT;
+import static christmas.domain.constant.Discount.ZERO;
+
+import christmas.domain.DecemberDate;
+import christmas.domain.constant.Benefit;
+import christmas.domain.constant.dish.Dessert;
+import christmas.domain.constant.dish.MainDish;
+import christmas.domain.constant.dish.Orderable;
+import christmas.domain.dto.BenefitDto;
+import java.util.Map;
+import java.util.Map.Entry;
+
+public class WeekDiscountCalculator {
+ private WeekDiscountCalculator() {
+
+ }
+
+ public static void apply(Map<Orderable, Integer> menus, BenefitDto benefitDto, DecemberDate visitDate) {
+ if (isWeekend(visitDate)) {
+ applyWeekendDiscount(menus, benefitDto);
+ return;
+ }
+ applyWeekdayDiscount(menus, benefitDto);
+ }
+
+ private static void applyWeekendDiscount(Map<Orderable, Integer> menus, BenefitDto benefitDto) {
+ if (getMainDishCount(menus) == ZERO.getValue()) {
+ return;
+ }
+ benefitDto.addBenefit(Benefit.WEEK_END_DISCOUNT, getMainDishCount(menus) * WEEK_DISCOUNT_UNIT.getValue());
+ }
+
+ private static void applyWeekdayDiscount(Map<Orderable, Integer> menus, BenefitDto benefitDto) {
+ if (getDessertCount(menus) == ZERO.getValue()) {
+ return;
+ }
+ benefitDto.addBenefit(Benefit.WEEK_DAY_DISCOUNT, getDessertCount(menus) * WEEK_DISCOUNT_UNIT.getValue());
+ }
+
+ private static int getDessertCount(Map<Orderable, Integer> menus) {
+ return countDishesOfCertainCategory(menus, Dessert.class);
+ }
+
+ private static int getMainDishCount(Map<Orderable, Integer> menus) {
+ return countDishesOfCertainCategory(menus, MainDish.class);
+ }
+
+ private static int countDishesOfCertainCategory(Map<Orderable, Integer> menus,
+ Class<? extends Orderable> category) {
+ int count = ZERO.getValue();
+ for (Entry<Orderable, Integer> entry : menus.entrySet()) {
+ if (entry.getKey().getClass() == category) {
+ count += entry.getValue();
+ }
+ }
+ return count;
+ }
+
+ private static boolean isWeekend(DecemberDate visitDate) {
+ int dateNumber = visitDate.date();
+ return dateNumber % ONE_WEEK.getValue() == ONE.getValue()
+ || dateNumber % ONE_WEEK.getValue() == TWO.getValue();
+ }
+
+} | Java | dto์ addํ๋ ๊ฒ๋ณด๋ค ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํ๋ฉด Dto์ ์์กด์ฑ์ ๋จ์ด๋จ๋ฆฌ๊ณ ๋ค๋ฅธ Dto์๋ ์ ๋ฌ ํ ์ ์์ต๋๋ค. |
@@ -0,0 +1,42 @@
+import Customer from '../src/models/Customer';
+import Order from '../src/models/Order';
+
+describe('Customer ํด๋์ค', () => {
+ let customer;
+
+ beforeEach(() => {
+ customer = new Customer();
+ });
+
+ test('์ฃผ๋ฌธ์ด ์์ ๋, ๋ฑ์ง ๊ณ์ฐ ๊ฒฐ๊ณผ๋ null์ด ๋๋ค.', () => {
+ customer.calculateBadge();
+ expect(customer.getBadge()).toBeNull();
+ });
+
+ test('์ ์ ํ ํ ์ธ ๊ธ์ก์ ๋ฐ๋ผ ์ฌ๋ฐ๋ฅธ ๋ฑ์ง๊ฐ ํ ๋น๋๋ค.', () => {
+ const order = new Order();
+ order.setDiscountAmount(10000);
+ customer.addOrder(order);
+ customer.calculateBadge();
+ expect(customer.getBadge()).toBe('ํธ๋ฆฌ');
+ });
+
+ test('์ฌ๋ฌ ์ฃผ๋ฌธ์ ์ถ๊ฐํ์ ๋, ์ด ํ ์ธ ๊ธ์ก์ ๋ฐ๋ฅธ ๋ฑ์ง๊ฐ ํ ๋น๋๋ค.', () => {
+ const order1 = new Order();
+ order1.setDiscountAmount(3000);
+ const order2 = new Order();
+ order2.setDiscountAmount(2000);
+ customer.addOrder(order1);
+ customer.addOrder(order2);
+ customer.calculateBadge();
+ expect(customer.getBadge()).toBe('๋ณ');
+ });
+
+ test('ํ ์ธ ๊ธ์ก์ด 5000์ ์ดํ์ผ ๋ ๋ฑ์ง๋ null๋ก ํ์๋๋ค.', () => {
+ const order = new Order();
+ order.setDiscountAmount(4000);
+ customer.addOrder(order);
+ customer.calculateBadge();
+ expect(customer.getBadge()).toBeNull();
+ });
+}); | JavaScript | ์ง๊ธ์ `ํธ๋ฆฌ`์ ๋ํ ํ
์คํธ ์ผ์ด์ค๋ง ์๋๋ฐ, `๋ณ`๊ณผ `์ฐํ` ํ
์คํธ ์ผ์ด์ค๊น์ง ๋ชจ๋ ํฌํจํ๋ฉด ํ
์คํธ ์ ๋ขฐ๋๊ฐ ๋์์ง ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,48 @@
+import Menu from '../src/models/Menu';
+import { MenuConstants } from '../src/constants/MenuConstants';
+
+describe('Menu ํด๋์ค', () => {
+ let menu;
+
+ beforeEach(() => {
+ menu = new Menu();
+ });
+
+ test('๋ฉ๋ด ํญ๋ชฉ์ด ์ฌ๋ฐ๋ฅด๊ฒ ์ด๊ธฐํ๋์ด์ผ ํ๋ค.', () => {
+ expect(menu.getItem('ํฐ๋ณธ์คํ
์ดํฌ')).toEqual({
+ name: 'ํฐ๋ณธ์คํ
์ดํฌ',
+ price: 55000,
+ category: 'MAINS',
+ });
+ expect(menu.getItem('์์ ์๋ฌ๋')).toEqual({
+ name: '์์ ์๋ฌ๋',
+ price: 8000,
+ category: 'APPETIZERS',
+ });
+ });
+
+ test('์กด์ฌํ์ง ์๋ ๋ฉ๋ด ํญ๋ชฉ์ ๋ํด null์ ๋ฐํํด์ผ ํ๋ค.', () => {
+ expect(menu.getItem('์์ก์ด์คํ')).toBeNull();
+ expect(menu.getItem('1234')).toBeNull();
+ expect(menu.getItem('์คํ
์ดํฌ@')).toBeNull();
+ });
+
+ test('๋ชจ๋ ๋ฉ๋ด ์นดํ
๊ณ ๋ฆฌ๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ๋ก๋๋์ด์ผ ํ๋ค.', () => {
+ Object.keys(MenuConstants).forEach((category) => {
+ Object.keys(MenuConstants[category]).forEach((itemName) => {
+ const menuItem = menu.getItem(itemName);
+ expect(menuItem).not.toBeNull();
+ expect(menuItem.category).toBe(category);
+ });
+ });
+ });
+
+ test('๊ฐ ๋ฉ๋ด ํญ๋ชฉ์ ๊ฐ๊ฒฉ์ด ์ ํํด์ผ ํจ', () => {
+ Object.keys(MenuConstants).forEach((category) => {
+ Object.entries(MenuConstants[category]).forEach(([itemName, price]) => {
+ const menuItem = menu.getItem(itemName);
+ expect(menuItem.price).toBe(price);
+ });
+ });
+ });
+}); | JavaScript | ์ฌ๊ธฐ ๋ ํ
์คํธ ๋ชจ๋ ์ฌ๋ฌ ๊ฐ์ `expect()`๋ฅผ ๊ฐ์ง๊ณ ์๋๋ฐ์. `expect()`ํ๋ ๋ถ๋ถ์ ๊ตฌ์กฐ๊ฐ ๋น์ทํ๋๊น "๋ฉ๋ด ํญ๋ชฉ ์ด๊ธฐํ ํ
์คํธ"์ "์กด์ฌํ์ง ์๋ ๋ฉ๋ด ํญ๋ชฉ ํ
์คํธ"๋ฅผ `test.each()`๋ก ๋ณ๊ฒฝํ๋ ๊ฑด ์ด๋จ๊น์?. ํ๋ก๋์
์ฝ๋๋ฅผ ์์ฑํ ๋ ์ค๋ณต๋๋ ๋ถ๋ถ์ ์ ๊ฑฐํ๋ ๊ฒ์ฒ๋ผ, ํ
์คํธ ์ฝ๋๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์ค๋ณต์ ์ ๊ฑฐํ๋ ๋ฆฌํฉํฐ๋ง์ด ํ์ํ๋ค๊ณ ์๊ฐํฉ๋๋ค! |
@@ -0,0 +1,73 @@
+import OrderValidator from '../src/validators/OrderValidator';
+import Menu from '../src/models/Menu';
+import { ValidatorConstants } from '../src/validators/constants/ValidatorConstants';
+
+describe('OrderValidator ํด๋์ค', () => {
+ let mockMenu;
+
+ beforeEach(() => {
+ mockMenu = new Menu();
+ });
+
+ describe('validateOrderItems ๋ฉ์๋', () => {
+ test('์ ํจํ ์ฃผ๋ฌธ ํญ๋ชฉ์ ๋ํด ์์ธ๊ฐ ๋ฐ์ํ์ง ์์์ผ ํ๋ค', () => {
+ const orderItems = [{ name: 'ํฐ๋ณธ์คํ
์ดํฌ', quantity: 1 }];
+ expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).not.toThrow();
+ });
+
+ test('์กด์ฌํ์ง ์๋ ๋ฉ๋ด ํญ๋ชฉ์ ๋ํด ์์ธ๊ฐ ๋ฐ์ํด์ผ ํ๋ค', () => {
+ const orderItems = [{ name: '๋ฏธ์ง์๋ฉ๋ด', quantity: 1 }];
+ expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).toThrow(
+ new Error(ValidatorConstants.INVALID_ORDER_ERROR),
+ );
+ });
+
+ test('์๋ฃ๋ง ํฌํจ๋ ์ฃผ๋ฌธ ์ ์์ธ๊ฐ ๋ฐ์ํด์ผ ํ๋ค', () => {
+ const orderItems = [{ name: '์ ๋ก์ฝ๋ผ', quantity: 2 }];
+ expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).toThrow(
+ new Error(ValidatorConstants.ONLY_BEVERAGE_ERROR),
+ );
+ });
+
+ test('์ฃผ๋ฌธ ํญ๋ชฉ์ ์ด ์๋์ด 20๊ฐ๋ฅผ ์ด๊ณผํ๋ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํด์ผ ํ๋ค', () => {
+ const orderItems = new Array(21).fill({ name: 'ํฐ๋ณธ์คํ
์ดํฌ', quantity: 1 });
+ expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).toThrow(
+ new Error(ValidatorConstants.INVALID_ORDER_ERROR),
+ );
+ });
+ test('์ฃผ๋ฌธ ํญ๋ชฉ์ ์ด ์๋์ด 20๊ฐ ์ดํ์ธ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ์ง ์์์ผ ํ๋ค', () => {
+ const orderItems = new Array(20).fill({ name: 'ํฐ๋ณธ์คํ
์ดํฌ', quantity: 1 });
+ expect(() => OrderValidator.validateTotalQuantity(orderItems)).not.toThrow();
+ });
+
+ test('์ฃผ๋ฌธ ์๋์ด 0 ์ดํ์ธ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํด์ผ ํ๋ค', () => {
+ const orderItems = [{ name: 'ํฐ๋ณธ์คํ
์ดํฌ', quantity: 0 }];
+ expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).toThrow(
+ new Error(ValidatorConstants.INVALID_ORDER_ERROR),
+ );
+ });
+
+ test('์๋์ด 1 ์ด์์ธ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ์ง ์์์ผ ํ๋ค', () => {
+ const item = { name: 'ํฐ๋ณธ์คํ
์ดํฌ', quantity: 1 };
+ expect(() => OrderValidator.validateQuantity(item)).not.toThrow();
+ });
+
+ test('์ค๋ณต๋ ๋ฉ๋ด ํญ๋ชฉ์ด ํฌํจ๋ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํด์ผ ํ๋ค', () => {
+ const orderItems = [
+ { name: 'ํฐ๋ณธ์คํ
์ดํฌ', quantity: 1 },
+ { name: 'ํฐ๋ณธ์คํ
์ดํฌ', quantity: 1 },
+ ];
+ expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).toThrow(
+ new Error(ValidatorConstants.INVALID_ORDER_ERROR),
+ );
+ });
+
+ test('์์๊ณผ ์๋ฃ๊ฐ ํผํฉ๋ ์ ํจํ ์ฃผ๋ฌธ์ ์์ธ๊ฐ ๋ฐ์ํ์ง ์์์ผ ํ๋ค', () => {
+ const orderItems = [
+ { name: 'ํฐ๋ณธ์คํ
์ดํฌ', quantity: 1 },
+ { name: '์ ๋ก์ฝ๋ผ', quantity: 2 },
+ ];
+ expect(() => OrderValidator.validateOrderItems(orderItems, mockMenu)).not.toThrow();
+ });
+ });
+}); | JavaScript | `Validator` ํ
์คํธ ํ์ผ์์๋ ์์ธ๊ฐ ๋ฐ์ํ์ ๋ ์๋ฌ์ ์ ํ๊ณผ ๋ฉ์์ง ํฌํจ ์ฌ๋ถ๋ฅผ ๊ฒ์ฌํ๊ณ ์๋๋ฐ์. ๋ค๋ฅธ ๊ณณ์ ์์ธ ํ
์คํธ์์๋ ๋ฉ์์ง ๊ฒ์ฌ ์์ด ์์ธ๊ฐ ๋ฐ์ํ๋์ง๋ง(`toThrow()`) ๊ฒ์ฌํ๋ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค. |
@@ -0,0 +1,56 @@
+import InputView from '../views/InputView';
+import OutputView from '../views/OutputView';
+import OrderService from '../services/OrderService';
+import DiscountService from '../services/DiscountService';
+import Customer from '../models/Customer';
+import { DateFormatter } from '../utils/DateFormatter';
+
+export default class EventController {
+ #orderService;
+
+ #customer;
+
+ #discountService;
+
+ constructor() {
+ this.#orderService = new OrderService();
+ this.#customer = new Customer();
+ this.#discountService = new DiscountService();
+ }
+
+ async start() {
+ OutputView.printWelcomeMessage();
+ await this.handleOrderProcess();
+ }
+
+ async handleOrderProcess() {
+ try {
+ const inputDate = await this.getDateFromUser();
+ await this.handleMenuOrderProcess(inputDate);
+ } catch (error) {
+ OutputView.printErrorMessage(error.message);
+ await this.handleOrderProcess();
+ }
+ }
+
+ async getDateFromUser() {
+ const day = await InputView.readDate();
+ return DateFormatter.createDateFromDay(day);
+ }
+
+ async handleMenuOrderProcess(inputDate) {
+ try {
+ const inputMenuItems = await InputView.readMenu();
+ const order = this.#orderService.createOrder(inputDate, inputMenuItems);
+ order.applyDiscounts(this.#discountService, inputDate);
+ this.#customer.addOrder(order);
+ this.#customer.calculateBadge();
+ OutputView.printDay(inputDate);
+ OutputView.printOrderDetails(order);
+ OutputView.printBadge(this.#customer);
+ } catch (error) {
+ OutputView.printErrorMessage(error.message);
+ await this.handleMenuOrderProcess(inputDate);
+ }
+ }
+} | JavaScript | ํจ์ ๋ผ์ธ ์๊ฐ ๋ฑ 15๋ผ์ธ์ธ ๊ฒ ๊ฐ๋ค์. ๋ง์ฝ ์ด๋ค ์ถ๋ ฅ์ด ํ๋ ์ถ๊ฐ๋๋ค๋ฉด ์ฌ๊ธฐ์ ์ถ๊ฐํด์ผ ํ ํ
๋ฐ, ๊ทธ๋ฌ๋ฉด ํจ์ ๋ผ์ธ ์๊ฐ 15๋ผ์ธ์ ๋๊ธธ ์๋ ์์ ๊ฒ ๊ฐ์์.
ํจ์ ๋ผ์ธ ์ ์ ํ์ด ์๋ ๊ฒ์ 15๋ผ์ธ์ด๋ผ๋ ๊ฒ ์ค์ํด์๋ผ๊ธฐ๋ณด๋ค ๊ทธ๋งํผ **ํจ์๋ฅผ ์ ๋ถ๋ฆฌํ์** ๋ ์๋ฏธ์ธ ๊ฒ ๊ฐ์ต๋๋ค. ๊ทธ๋์ ํจ์์ ๋ผ์ธ ์๊ฐ ๊ฐ๋น๊ฐ๋นํ๋ค๋ ๊ฒ์, ๋ฆฌํฉํฐ๋ง์ ์ ํธ๊ฐ ์๋๊น ์ถ์ด์๐ค
์ด ๋ฉ์๋๋ ๋์์ ์ฌ๋ฌ ๊ฐ์ง ์ผ์ ์ฒ๋ฆฌํ๊ณ ์๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋๋ฐ์(์
๋ ฅ ๋ฐ๊ธฐ, ์ฃผ๋ฌธ ์์ฑ, ๊ฐ์ข
ํ ์ธ ์ ์ฉ, ์ถ๋ ฅ, ์
๋ ฅ ์์ธ ์ฒ๋ฆฌ ๋ฐ ์ฌ์
๋ ฅ ๋ฐ๊ธฐ ๋ฑ). ๋ฉ์๋๊ฐ ํ๋์ ์ญํ ๋ง ์ํํ ์ ์๋๋ก ๋ถ๋ฆฌํ๋ ๊ฒ์ ์ด๋จ๊น์? |
@@ -0,0 +1,30 @@
+export default class Customer {
+ #orders;
+
+ #badge;
+
+ static BADGE_CRITERIA = [
+ { name: '์ฐํ', amount: 20000 },
+ { name: 'ํธ๋ฆฌ', amount: 10000 },
+ { name: '๋ณ', amount: 5000 },
+ ];
+
+ constructor() {
+ this.#orders = [];
+ this.#badge = null;
+ }
+
+ addOrder(order) {
+ this.#orders.push(order);
+ }
+
+ calculateBadge() {
+ const totalDiscount = this.#orders.reduce((sum, order) => sum + order.getDiscountAmount(), 0);
+ const badge = Customer.BADGE_CRITERIA.find((badgeName) => totalDiscount >= badgeName.amount);
+ this.#badge = badge ? badge.name : null;
+ }
+
+ getBadge() {
+ return this.#badge;
+ }
+} | JavaScript | ์์๋ฅผ ํ์ผ๋ก ๋ง๋ ๋ค๋ฉด ์์ ์ด ํ์ํ ๋ ์ฐพ๊ธฐ๊ฐ ์์ํ๊ณ , ๋ฐฐ์ง ์ ๋ณด๊ฐ ๋์ด๋๊ฑฐ๋ ์ค์ด๋ค์ด๋ ๊ด๋ฆฌํ๊ธฐ ํธ๋ฆฌํ ๊ฒ ๊ฐ์๋ฐ์. ํ์ผ๋ก ๋ง๋ค์ง ์๊ณ ์ฌ๊ธฐ์ ์์ฑํ์ ์ด์ ๊ฐ ์๋์? |
@@ -0,0 +1,81 @@
+import { OrderConstants } from './constants/OrderConstants';
+
+export default class Order {
+ #menuItems;
+
+ #totalAmount;
+
+ #discountAmount;
+
+ #gift;
+
+ #discountDetails;
+
+ constructor() {
+ this.#menuItems = new Map();
+ this.#totalAmount = 0;
+ this.#discountAmount = 0;
+ this.#gift = null;
+ this.#discountDetails = {};
+ }
+
+ applyDiscounts(discountService, date) {
+ const discountResults = discountService.applyDiscounts(this, date);
+ this.setDiscountDetails(discountResults);
+ this.checkForGift();
+ }
+
+ addMenuItem(menuItem, quantity) {
+ this.#menuItems.set(menuItem.name, { ...menuItem, quantity });
+ this.#totalAmount += menuItem.price * quantity;
+ }
+
+ addDiscountDetail(discountName, discountAmount) {
+ this.#discountDetails[discountName] =
+ (this.#discountDetails[discountName] || 0) + discountAmount;
+ this.#discountAmount += discountAmount;
+ }
+
+ setDiscountDetails(discountDetails) {
+ this.#discountDetails = { ...discountDetails };
+ this.#discountAmount = Object.values(this.#discountDetails).reduce(
+ (sum, amount) => sum + amount,
+ 0,
+ );
+ }
+
+ checkForGift() {
+ if (this.#totalAmount >= OrderConstants.GIFT_THRESHOLD_AMOUNT) {
+ this.#gift = `${OrderConstants.GIFT_NAME} ${OrderConstants.GIFT_QUANTITY}๊ฐ`;
+ }
+ }
+
+ getDiscountDetails() {
+ return this.#discountDetails;
+ }
+
+ getTotalAmount() {
+ return this.#totalAmount;
+ }
+
+ getFinalAmount() {
+ const giftDiscount = this.#discountDetails[OrderConstants.GIFT_EVENT_TITLE] || 0;
+ return this.#totalAmount - (this.#discountAmount - giftDiscount);
+ }
+
+ setDiscountAmount(discountAmount) {
+ this.#discountAmount = discountAmount;
+ }
+
+ getDiscountAmount() {
+ return this.#discountAmount;
+ }
+
+ getItems() {
+ return Array.from(this.#menuItems.values());
+ }
+
+ getGift() {
+ return this.#gift;
+ }
+} | JavaScript | ์ ๋ `Model`์ ์ฆ์ ๋ฉ๋ด์ ๋ํ ์ ๋ณด(๋ฐ์ดํฐ)๋ง ์ ์ฅํ๊ณ , ๊ทธ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ '`์ฆ์ ๋ฉ๋ด` `๊ฐ์`๊ฐ'์ ๊ฐ์ด ํฌ๋งทํ
ํ๋ ๊ฒ์ `View`์ ์ญํ ์ด๋ผ๊ณ ์๊ฐํด์.
๋ง์ฝ ์ฆ์ ๋ฉ๋ด ๋ฐ์ดํฐ์ ๋ํด ๋ค๋ฅธ ํ์์ผ๋ก ์ถ๋ ฅํ๋ ๋ทฐ๊ฐ ์ฌ๋ฌ ๊ฐ ์๊ธด๋ค๊ณ ๊ฐ์ ํด ๋ด
์๋ค. ์ง๊ธ ์ฝ๋๋ฐฉ์๋๋ก๋ผ๋ฉด ๊ฐ ๋ทฐ์์ ๋ํ๋ผ ์ถ๋ ฅ ํ์์ `Model`์์ ์ํํด์ผ ํฉ๋๋ค. ํ์ง๋ง ์ด๊ฑด `Model`์ด `View`์ ์์กดํ๋ ๊ฒ๊ณผ ๊ฐ์ต๋๋ค. ์ถ๋ ฅ ํ์์ด ๋ณ๊ฒฝ๋์์ ๋ `Model`์ ๋ฉ์๋๋ฅผ ๋ณ๊ฒฝํ๊ฒ ๋ ํ
๋๊น์. ๋ `Model`์ ์ญํ ์ ๋ฒ์ด๋๋ ๊ฒ์ด๊ธฐ๋ ํฉ๋๋ค.
๊ทธ๋์ ์ ๋ ์ด ๋ฉ์๋๋ฅผ ์ถ๋ ฅ์ ๋ด๋นํ๋ `View` ์ชฝ์ผ๋ก ์ฎ๊ธฐ๋ ๊ฒ ์ข์ ๊ฒ ๊ฐ์๋ฐ, ์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,6 @@
+export const OrderConstants = {
+ GIFT_THRESHOLD_AMOUNT: 120000,
+ GIFT_EVENT_TITLE: '์ฆ์ ์ด๋ฒคํธ',
+ GIFT_NAME: '์ดํ์ธ',
+ GIFT_QUANTITY: 1,
+}; | JavaScript | ์ง๊ธ์ ์ฆ์ ์ด๋ฒคํธ๊ฐ ์ดํ์ธ 1๊ฐ๋ง ์ฃผ๊ณ ์๋๋ฐ์. ๋ง์ฝ ์ด ์ด๋ฒคํธ๊ฐ ํ์ฅ๋๋ค๋ฉด, ๋ฐฐ์ง ์ด๋ฒคํธ์ฒ๋ผ ๊ธ์ก์ ๋ฐ๋ผ ๋ค๋ฅธ ์ ๋ฌผ์ ์ฆ์ ํ๊ฒ ๋ ์๋ ์์ ๊ฒ ๊ฐ์์. Strategy ํจํด์ ์ ํํ์ ์ด์ ๋ ์ ์ง๋ณด์์ ๋ณ๊ฒฝ์ ์ฉ์ดํด์๋ผ๊ณ ์๊ฐ๋๋๋ฐ์. ๊ทธ๋งํผ ์์๋ฅผ ์ ์ธํ ๋์๋ ํ์ฅ์ฑ์ ์ฑ๊ธฐ๋ฉด ๋ ์ข์ง ์์์๊น ์๊ฐํด๋ด
๋๋ค! |
@@ -0,0 +1,46 @@
+import ChristmasDiscountStrategy from './strategies/ChristmasDiscountStrategy';
+import WeekdayDiscountStrategy from './strategies/WeekdayDiscountStrategy';
+import WeekendDiscountStrategy from './strategies/WeekendDiscountStrategy';
+import SpecialDiscountStrategy from './strategies/SpecialDiscountStrategy';
+import GiftChampagneStrategy from './strategies/GiftChampagneStrategy';
+
+export default class DiscountService {
+ #strategies;
+
+ constructor() {
+ this.#strategies = {
+ christmas: new ChristmasDiscountStrategy(),
+ special: new SpecialDiscountStrategy(),
+ weekday: new WeekdayDiscountStrategy(),
+ weekend: new WeekendDiscountStrategy(),
+ giftChampagne: new GiftChampagneStrategy(),
+ };
+ }
+
+ applyDiscounts(order, date) {
+ const MINIMUM_AMOUNT_FOR_DISCOUNTS = 10000;
+ if (order.getTotalAmount() >= MINIMUM_AMOUNT_FOR_DISCOUNTS) {
+ const discountResults = this.calculateTotalDiscounts(order, date);
+ const totalDiscountAmount = this.calculateTotalDiscountAmount(discountResults);
+ order.setDiscountAmount(totalDiscountAmount);
+ return discountResults;
+ }
+ return {};
+ }
+
+ calculateTotalDiscounts(order, date) {
+ const discountResults = {};
+ Object.keys(this.#strategies).forEach((strategyKey) => {
+ const strategy = this.#strategies[strategyKey];
+ const discountResult = strategy.applyDiscount(order, date);
+ if (discountResult.amount > 0) {
+ discountResults[discountResult.name] = discountResult.amount;
+ }
+ });
+ return discountResults;
+ }
+
+ calculateTotalDiscountAmount(discountResults) {
+ return Object.values(discountResults).reduce((sum, amount) => sum + amount, 0);
+ }
+} | JavaScript | ์ด ๋ถ๋ถ์ ์ด๋ ๊ฒ ๋ฐ๊ฟ ์ ์์ ๊ฒ ๊ฐ์์!
```javascript
Object.values(this.#strategies).forEach((strategy) => {
const discountResult = strategy.applyDiscount(order, date);
// ...
});
``` |
@@ -0,0 +1,5 @@
+export default class IDiscountStrategy {
+ applyDiscount(order, date) {
+ throw new Error('[ERROR] ํ์ ํด๋์ค์์ applyDiscount ๋ฉ์๋๋ฅผ ๊ตฌํํด์ผ ํฉ๋๋ค.');
+ }
+} | JavaScript | ํ์ผ๋ช
๊ณผ ํด๋์ค๋ช
๋งจ ์์ `I`๊ฐ ๋ถ์ด ์๋๋ฐ, ํน์ ์๋ํ์ ๊ฑด๊ฐ์? ์๋ํ์ ๊ฑฐ๋ผ๋ฉด ์ด๋ค ์๋ฏธ์ธ์ง ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,16 @@
+import IDiscountStrategy from './IDiscountStrategy';
+import { DiscountStrategyConstants } from './constants/DiscountStrategyConstants';
+
+export default class SpecialDiscountStrategy extends IDiscountStrategy {
+ applyDiscount(order, date) {
+ const dayOfWeek = date.getDay();
+ const isSunday = dayOfWeek === 0;
+ const isChristmas = date.getDate() === 25;
+
+ if (isSunday || isChristmas) {
+ return { name: DiscountStrategyConstants.SPECIAL_DISCOUNT, amount: 1000 };
+ }
+
+ return { name: DiscountStrategyConstants.SPECIAL_DISCOUNT, amount: 0 };
+ }
+} | JavaScript | ํน๋ณ ํ ์ธ์ด ๋ชจ๋ ์ผ์์ผ (+25์ผ)์ด๋ผ๋ ์ ์ ์ด์ฉํ์
จ๋ค์!
์ ๋ ๋ณ ํ์๊ฐ ๋ ๋ ์ง๋ฅผ ํ๋ํ๋ ์์ ๋ฐฐ์ด์ ๋ด์์ ๊ด๋ฆฌํ๋๋ฐ์. ์ฎ๊ฒจ ์ ์ ๋ ์คํ๋ผ๋ ๋๋ค๋ฉด ํฐ์ผ์ด์ง๋ง๐
, ์ด๋ ๊ฒ ํ ์ด์ ๋ ํน๋ณ ํ ์ธ ๋ ์ง๊ฐ ๋ณ๊ฒฝ๋ ์ ์์ ๊ฒ ๊ฐ๋ค๊ณ ์๊ฐํ๊ธฐ ๋๋ฌธ์
๋๋ค. ์๊ตฌ์ฌํญ์์ "ํน๋ณ ํ ์ธ"์ ์ค๋ช
ํ ๋, '๋ฌ๋ ฅ์ ๋ณ์ด ํ์๋ ๋ ์ง'๋ผ๊ณ ์ค๋ช
ํ๋๋ผ๊ตฌ์. ๋ง์ฝ '๋ชจ๋ ์ผ์์ผ๊ณผ ํฌ๋ฆฌ์ค๋ง์ค ๋น์ผ'์ฒ๋ผ ์ค๋ช
ํ๋ค๋ฉด ์ ๋ ์ด๋ฐ ์ฝ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ์ ๊ฒ ๊ฐ๋ค์.
๊ตฌํ ๋ฐฉ์์ ์ ๋ต์ ์์ง๋ง ์ ์๊ฐ์ ํ ๋ฒ ๊ณต์ ๋๋ฆฌ๊ณ ์ถ์์ด์๐ |
@@ -0,0 +1,19 @@
+import IDiscountStrategy from './IDiscountStrategy';
+import { DiscountStrategyConstants } from './constants/DiscountStrategyConstants';
+
+export default class WeekdayDiscountStrategy extends IDiscountStrategy {
+ applyDiscount(order, date) {
+ const dayOfWeek = date.getDay();
+ const isWeekday = dayOfWeek >= 0 && dayOfWeek <= 4;
+
+ if (isWeekday) {
+ const DESSERT_DISCOUNT = 2023;
+ const discountAmount = order
+ .getItems()
+ .filter((item) => item.category === 'DESSERTS')
+ .reduce((total, item) => total + item.quantity * DESSERT_DISCOUNT, 0);
+ return { name: DiscountStrategyConstants.WEEKDAY_DISCOUNT, amount: discountAmount };
+ }
+ return { name: DiscountStrategyConstants.WEEKDAY_DISCOUNT, amount: 0 };
+ }
+} | JavaScript | `0`, `4`์ ๊ฐ์ ์ซ์๋ฅผ `SUNDAY`, `THURSDAY`์ฒ๋ผ ์์ํํด์ ์ฌ์ฉํ๋ฉด ์ฝ๋ ๊ฐ๋
์ฑ์ ๋ ์ฑ๊ธธ ์ ์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,36 @@
+import { ValidatorConstants } from './constants/ValidatorConstants';
+
+export const InputValidator = {
+ Day: {
+ validateDay(input) {
+ const dayPattern = /^\d+$/;
+ if (!dayPattern.test(input)) {
+ throw new Error(ValidatorConstants.INVALID_DATE_ERROR);
+ }
+ const day = parseInt(input, 10);
+ if (day < 1 || day > 31) {
+ throw new Error(ValidatorConstants.INVALID_DATE_ERROR);
+ }
+
+ return day;
+ },
+ },
+
+ Menu: {
+ validateMenu(input) {
+ const menuSelections = input.split(',');
+ const selectionPattern = /^[๊ฐ-ํฃ]+-\d+$/;
+ const validSelections = menuSelections.map((selection) => {
+ const trimmedSelection = selection.trim();
+ if (!selectionPattern.test(trimmedSelection)) {
+ throw new Error(ValidatorConstants.INVALID_ORDER_ERROR);
+ }
+ const [name, quantity] = trimmedSelection.split('-');
+
+ return { name, quantity: parseInt(quantity, 10) };
+ });
+
+ return validSelections;
+ },
+ },
+}; | JavaScript | ๊ฐ๊ฐ์ validation(์ ๊ทํํ์ ํจํด & ๋ฒ์)์ ๋ ๊ฐ์ ํจ์๋ก ๋ถ๋ฆฌํด ์ ์ธํ๊ณ ํธ์ถํด์ ์ฌ์ฉํ๋ ๊ฑด ์ด๋จ๊น์? ์๋ํด์ ํ๊บผ๋ฒ์ ์์ฑํ์ ๊ฒ ๊ฐ์๋ฐ, ๋ง์ฝ ๋ง๋ค๋ฉด ์ด์ ๊ฐ ๊ถ๊ธํด์! |
@@ -0,0 +1,36 @@
+import { ValidatorConstants } from './constants/ValidatorConstants';
+
+export const InputValidator = {
+ Day: {
+ validateDay(input) {
+ const dayPattern = /^\d+$/;
+ if (!dayPattern.test(input)) {
+ throw new Error(ValidatorConstants.INVALID_DATE_ERROR);
+ }
+ const day = parseInt(input, 10);
+ if (day < 1 || day > 31) {
+ throw new Error(ValidatorConstants.INVALID_DATE_ERROR);
+ }
+
+ return day;
+ },
+ },
+
+ Menu: {
+ validateMenu(input) {
+ const menuSelections = input.split(',');
+ const selectionPattern = /^[๊ฐ-ํฃ]+-\d+$/;
+ const validSelections = menuSelections.map((selection) => {
+ const trimmedSelection = selection.trim();
+ if (!selectionPattern.test(trimmedSelection)) {
+ throw new Error(ValidatorConstants.INVALID_ORDER_ERROR);
+ }
+ const [name, quantity] = trimmedSelection.split('-');
+
+ return { name, quantity: parseInt(quantity, 10) };
+ });
+
+ return validSelections;
+ },
+ },
+}; | JavaScript | ์ด ๋ก์ง์ด๋ผ๋ฉด ์ด๋ฐ ์
๋ ฅ์ ๋ฃ์ด๋ ์ ์์ ์ผ๋ก ์๋ํ ๊ฒ ๊ฐ์์. ํน์ ๋ค๋ฅธ ๋ถ๋ถ์์ ๊ฒ์ฌํ๊ณ ์๋๋ฐ ์ ๊ฐ ๋์น ๊ฒ์ผ๊น์?
```
๋ฐ๋นํ๋ฆฝ-1,,,,,,,ํํ์ค-2
๋ฐ๋นํ๋ฆฝ-1,,,ํํ์ค-2,,
``` |
@@ -6,6 +6,7 @@ import kr.kro.dearmoment.image.application.command.SaveImageCommand
import kr.kro.dearmoment.image.application.port.input.DeleteImageUseCase
import kr.kro.dearmoment.image.application.port.input.GetImageUseCase
import kr.kro.dearmoment.image.application.port.input.SaveImageUseCase
+import kr.kro.dearmoment.image.application.port.input.UpdateImagePort
import kr.kro.dearmoment.image.application.port.output.DeleteImageFromDBPort
import kr.kro.dearmoment.image.application.port.output.DeleteImageFromObjectStoragePort
import kr.kro.dearmoment.image.application.port.output.GetImageFromObjectStoragePort
@@ -20,6 +21,7 @@ class ImageService(
private val uploadImagePort: UploadImagePort,
private val saveImagePort: SaveImagePort,
private val getImagePort: GetImagePort,
+ private val updateImagePort: UpdateImagePort,
private val deleteImageFromDBPort: DeleteImageFromDBPort,
private val getImageFromObjectStorage: GetImageFromObjectStoragePort,
private val deleteImageFromObjectStorage: DeleteImageFromObjectStoragePort,
@@ -40,21 +42,25 @@ class ImageService(
override fun getOne(imageId: Long): GetImageResponse {
val image = getImagePort.findOne(imageId)
- if (image.isUrlExpired()) {
- return GetImageResponse.from(getImageFromObjectStorage.getImage(image))
- }
+ val updatedImage =
+ if (image.isUrlExpired()) {
+ val renewedImage = getImageFromObjectStorage.getImageWithUrl(image)
+ updateImagePort.updateUrlInfo(renewedImage)
+ } else {
+ image
+ }
- return GetImageResponse.from(image)
+ return GetImageResponse.from(updatedImage)
}
@Transactional(readOnly = true)
override fun getAll(userId: Long): GetImagesResponse {
- val images = getImagePort.findAll(userId)
+ val images = getImagePort.findUserImages(userId)
val finalResult =
images.map { image ->
if (image.isUrlExpired()) {
- getImageFromObjectStorage.getImage(image)
+ getImageFromObjectStorage.getImageWithUrl(image)
} else {
image
} | Kotlin | ```js
@Transactional(readOnly = true)
override fun getOne(imageId: Long): GetImageResponse {
val image = getImagePort.findOne(imageId)
if (image.isUrlExpired()) {
val renewedImage = getImageFromObjectStorage.getImageWithUrl(image)
check(updateImagePort.update(renewedImage) > 0) { "fail update" }
// ์ฌ๊ธฐ์ GetImageResponse.from() ํธ์ถํ๋ ๋ถ๋ถ์,
// ๋์ค์ else์์๋ ๋๊ฐ์ด ์ฌ์ฉ๋๋๊น ์ค๋ณต
return GetImageResponse.from(renewedImage)
}
// ์ if ๋ธ๋ก๊ณผ ๋์ผํ๊ฒ ์๋ต ๊ฐ์ฒด ๋ง๋๋ ์ฝ๋๊ฐ ์์ผ๋,
// ์ค๋ณต์ ์ค์ผ ์ ์์ง ์์๊น์?
return GetImageResponse.from(image)
}
``` |
@@ -1,5 +1,6 @@
package kr.kro.dearmoment.image.adapter.output.persistence
+import kr.kro.dearmoment.image.application.port.input.UpdateImagePort
import kr.kro.dearmoment.image.application.port.output.DeleteImageFromDBPort
import kr.kro.dearmoment.image.application.port.output.GetImagePort
import kr.kro.dearmoment.image.application.port.output.SaveImagePort
@@ -10,7 +11,7 @@ import org.springframework.stereotype.Component
@Component
class ImagePersistenceAdapter(
private val imageRepository: JpaImageRepository,
-) : SaveImagePort, DeleteImageFromDBPort, GetImagePort {
+) : SaveImagePort, UpdateImagePort, DeleteImageFromDBPort, GetImagePort {
override fun save(image: Image): Long {
val entity = ImageEntity.from(image)
return imageRepository.save(entity).id
@@ -22,15 +23,20 @@ class ImagePersistenceAdapter(
.map { it.id }
}
- override fun findAll(userId: Long): List<Image> {
- val entities = imageRepository.findAllByUserId(userId)
- return entities.map { ImageEntity.toDomain(it) }
- }
+ override fun findUserImages(userId: Long): List<Image> =
+ imageRepository.findAllByUserId(userId)
+ .map { it.toDomain() }
override fun findOne(imageId: Long): Image {
val entity =
imageRepository.findByIdOrNull(imageId) ?: throw IllegalArgumentException("Invalid imageId: $imageId")
- return ImageEntity.toDomain(entity)
+ return entity.toDomain()
+ }
+
+ override fun updateUrlInfo(image: Image): Image {
+ val entity = imageRepository.findByIdOrNull(image.imageId) ?: throw IllegalArgumentException("Invalid imageId: ${image.imageId}")
+ entity.modifyUrlInfo(image.url, image.parId)
+ return entity.toDomain()
}
override fun delete(imageId: Long) { | Kotlin | ์ด๊ฑด ์ฝ๋ ๋ฆฌ๋ทฐ๊ฐ ์๋๋ผ ๊ฐ์ธ์ ์ผ๋ก ๊ถ๊ธํด์ ์ด๊ฑธ ์ข ๋ ๊ฐ๊ฒฐํ๊ฒ ์ฐ๋ ๋ฐฉ๋ฒ์ ์ฐพ์๋ณด์๋๋ฐ์.
์๋์ ์ฝ๋๊ฐ ๋ ๊ฐ๊ฒฐํ๊ณ ์ฝ๊ธฐ ํธํ๊ฐ์? ๊ทธ๋ฅ ์ฝ๋๋ฆฌ๋ทฐ์ฐจ ๊ณต๋ถ๋ฅผ ํด๋ด
๋๋ค.
```js
override fun findAll(userId: Long): List<Image> =
imageRepository.findAllByUserId(userId)
.map { it.toDomain() }
``` |
@@ -6,6 +6,7 @@ import kr.kro.dearmoment.image.application.command.SaveImageCommand
import kr.kro.dearmoment.image.application.port.input.DeleteImageUseCase
import kr.kro.dearmoment.image.application.port.input.GetImageUseCase
import kr.kro.dearmoment.image.application.port.input.SaveImageUseCase
+import kr.kro.dearmoment.image.application.port.input.UpdateImagePort
import kr.kro.dearmoment.image.application.port.output.DeleteImageFromDBPort
import kr.kro.dearmoment.image.application.port.output.DeleteImageFromObjectStoragePort
import kr.kro.dearmoment.image.application.port.output.GetImageFromObjectStoragePort
@@ -20,6 +21,7 @@ class ImageService(
private val uploadImagePort: UploadImagePort,
private val saveImagePort: SaveImagePort,
private val getImagePort: GetImagePort,
+ private val updateImagePort: UpdateImagePort,
private val deleteImageFromDBPort: DeleteImageFromDBPort,
private val getImageFromObjectStorage: GetImageFromObjectStoragePort,
private val deleteImageFromObjectStorage: DeleteImageFromObjectStoragePort,
@@ -40,21 +42,25 @@ class ImageService(
override fun getOne(imageId: Long): GetImageResponse {
val image = getImagePort.findOne(imageId)
- if (image.isUrlExpired()) {
- return GetImageResponse.from(getImageFromObjectStorage.getImage(image))
- }
+ val updatedImage =
+ if (image.isUrlExpired()) {
+ val renewedImage = getImageFromObjectStorage.getImageWithUrl(image)
+ updateImagePort.updateUrlInfo(renewedImage)
+ } else {
+ image
+ }
- return GetImageResponse.from(image)
+ return GetImageResponse.from(updatedImage)
}
@Transactional(readOnly = true)
override fun getAll(userId: Long): GetImagesResponse {
- val images = getImagePort.findAll(userId)
+ val images = getImagePort.findUserImages(userId)
val finalResult =
images.map { image ->
if (image.isUrlExpired()) {
- getImageFromObjectStorage.getImage(image)
+ getImageFromObjectStorage.getImageWithUrl(image)
} else {
image
} | Kotlin | ๋ต ์์ ํด๋ณด๊ฒ ์ต๋๋ค. |
@@ -1,5 +1,6 @@
package kr.kro.dearmoment.image.adapter.output.persistence
+import kr.kro.dearmoment.image.application.port.input.UpdateImagePort
import kr.kro.dearmoment.image.application.port.output.DeleteImageFromDBPort
import kr.kro.dearmoment.image.application.port.output.GetImagePort
import kr.kro.dearmoment.image.application.port.output.SaveImagePort
@@ -10,7 +11,7 @@ import org.springframework.stereotype.Component
@Component
class ImagePersistenceAdapter(
private val imageRepository: JpaImageRepository,
-) : SaveImagePort, DeleteImageFromDBPort, GetImagePort {
+) : SaveImagePort, UpdateImagePort, DeleteImageFromDBPort, GetImagePort {
override fun save(image: Image): Long {
val entity = ImageEntity.from(image)
return imageRepository.save(entity).id
@@ -22,15 +23,20 @@ class ImagePersistenceAdapter(
.map { it.id }
}
- override fun findAll(userId: Long): List<Image> {
- val entities = imageRepository.findAllByUserId(userId)
- return entities.map { ImageEntity.toDomain(it) }
- }
+ override fun findUserImages(userId: Long): List<Image> =
+ imageRepository.findAllByUserId(userId)
+ .map { it.toDomain() }
override fun findOne(imageId: Long): Image {
val entity =
imageRepository.findByIdOrNull(imageId) ?: throw IllegalArgumentException("Invalid imageId: $imageId")
- return ImageEntity.toDomain(entity)
+ return entity.toDomain()
+ }
+
+ override fun updateUrlInfo(image: Image): Image {
+ val entity = imageRepository.findByIdOrNull(image.imageId) ?: throw IllegalArgumentException("Invalid imageId: ${image.imageId}")
+ entity.modifyUrlInfo(image.url, image.parId)
+ return entity.toDomain()
}
override fun delete(imageId: Long) { | Kotlin | ์ ๋ ์ด๊ฒ ํญ์ ๊ณ ๋ฏผ์ธ๋ฐ ๋๋ฒ๊น
์ ์ํด์ ์ด๋์ ๋ ๋์ด์ ์จ์ค์ง ์ฒด์ด๋์ ์จ์ค ์ ์์ผ๋ฉด ์จ์ค์ง ๋๋ ๋ง ์
๋๋ค ๐
๊ทผ๋ฐ ๊ฐ์ธ์ ์ผ๋ก๋ ๊ฐ๊ฒฐํ๊ฑธ ๋ ์ ํธํด์ ๊ณ ์ณ๋ณด๊ฒ ์ต๋๋ค. |
@@ -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,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์ ์์ฑ๋ฐฉ์์ ๋ํด์ ์๋กญ๊ฒ ๊ณ ๋ฏผํ๊ฒ ๋์๋ค์ |
@@ -0,0 +1,53 @@
+package christmas.domain;
+
+import christmas.dto.BenefitDto;
+import christmas.dto.GiftsDto;
+import java.util.function.Supplier;
+
+public class Bill {
+ public static final int MINIMUM_ORDERS_PRICE = 10000;
+ public static final int EVENT_NOT_APPLIED_AMOUNT = 0;
+
+ private final Discounts discounts;
+ private final Gifts gifts;
+
+ public Bill(Discounts discounts, Gifts gifts) {
+ this.discounts = discounts;
+ this.gifts = gifts;
+ }
+
+ public int getTotalBenefit(Orders orders) {
+ return getAmount(this::sumBenefit, orders);
+ }
+
+ public int getDiscountPrice(Orders orders) {
+ return orders.getTotalPrice() - discounts.sumDiscount();
+ }
+
+ public GiftsDto getGiftDto() {
+ return new GiftsDto(gifts.getResult());
+ }
+
+ public BenefitDto getBenefitDto() {
+ return new BenefitDto(discounts.getResult(), gifts.sumPrice());
+ }
+
+ public String getBadgeName() {
+ return Badge.getNameByBenefit(sumBenefit());
+ }
+
+ private int sumBenefit() {
+ return discounts.sumDiscount() + gifts.sumPrice();
+ }
+
+ private boolean isEventApplicable(Orders orders) {
+ return orders.getTotalPrice() >= MINIMUM_ORDERS_PRICE;
+ }
+
+ private <T> int getAmount(Supplier<T> supplier, Orders orders) {
+ if (isEventApplicable(orders)) {
+ return (int) supplier.get();
+ }
+ return EVENT_NOT_APPLIED_AMOUNT;
+ }
+} | Java | 10000์ ์ด์์ด์ฌ์ผ๋ง ์ด๋ฒคํธ๊ฐ ์ ์ฉ๋๋ ๊ณตํต ์กฐ๊ฑด์ Bill์ ์์น์ํค์
จ๋ค์..!
ํด๋น ์กฐ๊ฑด์ด ์ด๋ฒคํธ๋ฅผ ์ ์ฉํ ์ง ๋ง์ง๋ฅผ ์ ํ๋ค๋ ์ธก๋ฉด์์ Bill์ ์์นํ๊ธฐ์๋ ์ฑ
์์ด ๋ฒ์ด๋ ๋๋์ด ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ๊ฐ ์ด๋ฒคํธ๋ฅผ ํด๋์ค๋ก ๊ตฌํํ์ ๊ฒ ๊ฐ์๋ฐ 10000์ ์ด์์ธ์ง ๊ฒ์ฆํ๋ ์ฝ๋์ ์์น๋ DiscountPolicy๋ค์ ์์น์ํค์๋ ๊ฒ์ ์ด๋จ๊น์ ๐ค |
@@ -0,0 +1,53 @@
+package christmas.domain;
+
+import christmas.dto.BenefitDto;
+import christmas.dto.GiftsDto;
+import java.util.function.Supplier;
+
+public class Bill {
+ public static final int MINIMUM_ORDERS_PRICE = 10000;
+ public static final int EVENT_NOT_APPLIED_AMOUNT = 0;
+
+ private final Discounts discounts;
+ private final Gifts gifts;
+
+ public Bill(Discounts discounts, Gifts gifts) {
+ this.discounts = discounts;
+ this.gifts = gifts;
+ }
+
+ public int getTotalBenefit(Orders orders) {
+ return getAmount(this::sumBenefit, orders);
+ }
+
+ public int getDiscountPrice(Orders orders) {
+ return orders.getTotalPrice() - discounts.sumDiscount();
+ }
+
+ public GiftsDto getGiftDto() {
+ return new GiftsDto(gifts.getResult());
+ }
+
+ public BenefitDto getBenefitDto() {
+ return new BenefitDto(discounts.getResult(), gifts.sumPrice());
+ }
+
+ public String getBadgeName() {
+ return Badge.getNameByBenefit(sumBenefit());
+ }
+
+ private int sumBenefit() {
+ return discounts.sumDiscount() + gifts.sumPrice();
+ }
+
+ private boolean isEventApplicable(Orders orders) {
+ return orders.getTotalPrice() >= MINIMUM_ORDERS_PRICE;
+ }
+
+ private <T> int getAmount(Supplier<T> supplier, Orders orders) {
+ if (isEventApplicable(orders)) {
+ return (int) supplier.get();
+ }
+ return EVENT_NOT_APPLIED_AMOUNT;
+ }
+} | Java | ํ ์ธ๋ ๊ฐ๊ฒฉ์ ๊ตฌํ๋ ๋ฉ์๋์ ์์น๋ Orders๊ฐ ์ ์ ํ ๊น์ Bill์ด ์ ์ ํ ๊น์ ๐ค
์ ๋ ์ด๊ฒ์ ์ฐธ ๋ง์ด ๊ณ ๋ฏผํ๋๋ฐ์ ..! ์ ๋ Order์ ์์น์ํค๊ธด ํ๋๋ฐ ๊ด๋ จํด์ ํ๋๋ ์๊ฐ๋ ๊ถ๊ธํฉ๋๋ค!
๋ Orders๋ผ๋ ๋๋ฉ์ธ์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๊ณ ์๋๋ฐ int๋ฅผ ๋ฐ๋ ๊ฒ์ ์ด๋ป๊ฒ ์๊ฐํ์๋์ง๋ ๊ถ๊ธํด์ ๊ด๋ จํ ๊ธฐ์ค ๊ณต์ ํด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค ๐๐ป |
@@ -0,0 +1,25 @@
+package christmas.domain;
+
+import java.util.Map;
+
+public class Discount {
+ private final DiscountType type;
+ private final int amount;
+
+ public Discount(DiscountType type, int amount) {
+ this.type = type;
+ this.amount = amount;
+ }
+
+ public boolean isDiscount() {
+ return amount != 0;
+ }
+
+ public int getAmount() {
+ return amount;
+ }
+
+ public String getName() {
+ return type.getName();
+ }
+} | Java | ํ ์ธ๋ด์ญ์ ํ ์ธ ์ข
๋ฅ์ ํ ์ธ ๊ธ์ก์ ์์ผ๋ก ์ ์ํ ์ ์๊ตฐ์ ๐๐ป
์ ๋ ๋น์ทํ๊ฒ ์์ฑํ์ง๋ง ์ ๋ String ํ์
์ description๊ณผ int amount๋ก ์์ฑํ๋ค์..
์ด๋ ๊ฒ ๋ณด๋ enum์ผ๋ก ๊ด๋ฆฌํ์ ๊ฒ ์ ๋ง ์ข์๋ณด์ด๋ค์ ์ธ์ธํ ์ฝ๋ ์ ๋ฐฐ์ฐ๊ณ ๊ฐ๋๋ค ๐๐ป ๐๐ป |
@@ -0,0 +1,25 @@
+package christmas.domain;
+
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class Discounts {
+ private final List<Discount> discounts;
+
+ public Discounts(List<Discount> discounts) {
+ this.discounts = discounts;
+ }
+
+ public int sumDiscount() {
+ return discounts.stream()
+ .mapToInt(Discount::getAmount)
+ .sum();
+ }
+
+ public Map<String, Integer> getResult() {
+ return discounts.stream()
+ .filter(Discount::isDiscount)
+ .collect(Collectors.toUnmodifiableMap(Discount::getName, Discount::getAmount));
+ }
+} | Java | ์ผ๊ธ ์ปฌ๋ ์
์ ์ถ์ถํ์๊ณ ๊ด๋ จ๋ ๋ก์ง๋ ๊น๋ํ๊ฒ ์ ๋ชจ์์ฃผ์ ๊ฒ ๊ฐ์์ ๐๐ป
์ข์๋ณด์
๋๋ค ๐ |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.