code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,57 @@
+package racingcar.view;
+
+import racingcar.model.Car;
+import racingcar.model.CarName;
+import racingcar.model.Cars;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private static final String NEW_LINE = "\n";
+ private static final String SCORE_UNIT = "-";
+ private static final String SCORE_BOARD = "%s : %s";
+ private static final String WINNER_SEPARATOR = ", ";
+ private static final String WINNERS_ARE = "์ต์ข
์ฐ์น์ : ";
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String RESULT_INTRO = "์คํ ๊ฒฐ๊ณผ";
+
+ public void printResult(Cars cars) {
+ System.out.println(getScore(cars));
+ }
+
+ private String getScore(Cars cars) {
+ return cars.get().stream()
+ .map(this::toScoreBoard)
+ .collect(Collectors.joining(NEW_LINE));
+ }
+
+ private String toScoreBoard(Car car) {
+ return String.format(SCORE_BOARD, car.getName(), convertToScore(car.getPosition()));
+ }
+
+ private String convertToScore(int position) {
+ return SCORE_UNIT.repeat(position);
+ }
+
+ public void printWinners(List<CarName> winners) {
+ System.out.print(WINNERS_ARE);
+ System.out.println(getWinnerNames(winners));
+ }
+
+ private String getWinnerNames(List<CarName> winners) {
+ return winners.stream()
+ .map(CarName::get)
+ .collect(Collectors.joining(WINNER_SEPARATOR));
+ }
+
+ public void printError(IllegalArgumentException error) {
+ System.out.print(ERROR_PREFIX);
+ System.out.println(error.getMessage());
+ }
+
+ public void printRaceIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+} | Java | ์ ๋ ๊ฐ์ธ์ ์ผ๋ก MVC ์์ View๋ ์ปจํธ๋กค๋ฌ๋ก๋ถํฐ ๋์ ์ธ ์ ๋ณด๋ง ๋ฐ์์ผ ํ๋ค๊ณ ์๊ฐํ๋๋ฐ์! ํน์ ์ด๋ฐ ์ ๊ทผ ๋ฐฉ๋ฒ์ ์ด๋ ์ค๊น์?
์ ๊ฐ ์ด๋ ๊ฒ ์๊ฐํ๋ ์ด์ ๋, ๊ด์ฌ์ฌ๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์
๋๋ค.
์๋ฌ ์ฒ๋ฆฌ์ ๊ดํ ์ถ๋ ฅ์ ํด๋น try-catch ๋ธ๋ก์ ์ฑ
์์ด ์๋ค๊ณ ์๊ฐํด์.
์ฌ์ค ์ ๋ต์ ์๋ ๊ฒ ๊ฐ์ผ๋, ์ ๊ฐ ๊ณ ์ํ๋ ๋ฐฉ๋ฒ์ ์ด๋ค์ง ์ ์๋๋ ค ๋ด
๋๋ค. |
@@ -0,0 +1,47 @@
+package racingcar.model;
+
+public class Car implements Comparable<Car> {
+ private static final int MOVABLE_LOWER_BOUND = 4;
+
+ private final CarName name;
+ private final Position position = Position.init();
+
+ private Car(String name) {
+ this.name = CarName.from(name);
+ }
+
+ public static Car from(String name) {
+ return new Car(name);
+ }
+
+ public void move(int command) {
+ if (isMovable(command)) {
+ position.increase();
+ }
+ }
+
+ private static boolean isMovable(int command) {
+ return command >= MOVABLE_LOWER_BOUND;
+ }
+
+ public boolean isAt(Position expectedPosition) {
+ return position.equals(expectedPosition);
+ }
+
+ public int getPosition() {
+ return position.getPosition();
+ }
+
+ public CarName getName() {
+ return name;
+ }
+
+ public boolean isSamePosition(Car other) {
+ return this.position.equals(other.position);
+ }
+
+ @Override
+ public int compareTo(Car other) {
+ return this.position.compareTo(other.position);
+ }
+} | Java | ์ ๋ ๋ฐฐ์๊ฐ๋๋ค!! ์ด๋ฐ ์์ผ๋ก ๋น๊ต๋ ๊ฐ๋ฅํ๊ตฐ์.. |
@@ -0,0 +1,52 @@
+package racingcar.model;
+
+import java.util.Objects;
+
+public class CarName {
+
+ private static final String INVALID_NAME_LENGTH = "์๋์ฐจ ์ด๋ฆ์ 5์๋ฅผ ์ด๊ณผํ ์ ์์ต๋๋ค.";
+ private static final int NAME_LENGTH_UPPER_BOUND = 5;
+
+ private final String name;
+
+ public CarName(String name) {
+ validate(name);
+ this.name = name;
+ }
+
+ private void validate(String name) {
+ if (isInvalidLength(name)) {
+ throw new IllegalArgumentException(INVALID_NAME_LENGTH);
+ }
+ }
+
+ private boolean isInvalidLength(String name) {
+ return name.length() > NAME_LENGTH_UPPER_BOUND;
+ }
+
+ public static CarName from(String name) {
+ return new CarName(name);
+ }
+
+ public String get() {
+ return name;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ CarName carName = (CarName) o;
+ return Objects.equals(name, carName.name);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name);
+ }
+
+ @Override
+ public String toString() {
+ return name;
+ }
+} | Java | hashCode๊ฐ ์ฌ์ฉ๋์ง ์๋ ๊ฒ ๊ฐ์, ๋ฐ๋ก ์ค๋ฒ๋ผ์ด๋ฉ ํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,52 @@
+package racingcar.model;
+
+import java.util.Objects;
+
+public class CarName {
+
+ private static final String INVALID_NAME_LENGTH = "์๋์ฐจ ์ด๋ฆ์ 5์๋ฅผ ์ด๊ณผํ ์ ์์ต๋๋ค.";
+ private static final int NAME_LENGTH_UPPER_BOUND = 5;
+
+ private final String name;
+
+ public CarName(String name) {
+ validate(name);
+ this.name = name;
+ }
+
+ private void validate(String name) {
+ if (isInvalidLength(name)) {
+ throw new IllegalArgumentException(INVALID_NAME_LENGTH);
+ }
+ }
+
+ private boolean isInvalidLength(String name) {
+ return name.length() > NAME_LENGTH_UPPER_BOUND;
+ }
+
+ public static CarName from(String name) {
+ return new CarName(name);
+ }
+
+ public String get() {
+ return name;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ CarName carName = (CarName) o;
+ return Objects.equals(name, carName.name);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name);
+ }
+
+ @Override
+ public String toString() {
+ return name;
+ }
+} | Java | get ๋ฉ์๋๊ฐ toString๊ณผ ์ค๋ณต๋๋ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,63 @@
+package racingcar.cotroller;
+
+import racingcar.RandomRacingNumberGenerator;
+import racingcar.model.AttemptCount;
+import racingcar.model.Cars;
+import racingcar.view.InputView;
+import racingcar.view.OutputView;
+
+import java.util.List;
+import java.util.function.Supplier;
+
+public class RacingCarController {
+
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final Cars cars;
+
+ public RacingCarController() {
+ inputView = new InputView();
+ outputView = new OutputView();
+ cars = checkError(this::getCars);
+ }
+
+ public void run() {
+ progressGame();
+ printWinners();
+ }
+
+ private void progressGame() {
+ AttemptCount attemptCount = checkError(inputView::readAttemptCount);
+
+ outputView.printRaceIntro();
+ while (attemptCount.isPlayable()) {
+ attempt();
+ attemptCount.decrease();
+ }
+ }
+
+ private void printWinners() {
+ outputView.printWinners(cars.findWinners());
+ }
+
+ private void attempt() {
+ RandomRacingNumberGenerator numberGenerator = new RandomRacingNumberGenerator();
+ cars.race(numberGenerator);
+
+ outputView.printResult(cars);
+ }
+
+ private Cars getCars() {
+ List<String> names = inputView.readNames();
+ return Cars.from(names);
+ }
+
+ private <T> T checkError(Supplier<T> reader) {
+ try {
+ return reader.get();
+ } catch (IllegalArgumentException error) {
+ outputView.printError(error);
+ return checkError(reader);
+ }
+ }
+} | Java | ๋ฐ๋ก ์ปจํธ๋กค๋ฌ์์ RandomRacingNumberGenerator๋ฅผ ์ฃผ์
ํด์ฃผ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค.
์ ์๊ฐ์๋, RandomRacingNumberGenerator๋ฅผ race ๋ฉ์๋ ๋ด์์ ์ ์ธํ๋ฉด ์ปจํธ๋กค๋ฌ์์ ์์์ผ ํ ๊ด์ฌ์ฌ๊ฐ ํ๋ ์ค์ด๋๋ ์ด์ ์ด ์์ ๊ฒ ๊ฐ์์์! |
@@ -1,29 +1,51 @@
package lotto.domain;
-import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import java.util.stream.Collectors;
public class Lotto {
- private List<Integer> lotto;
+ private List<LottoNumber> lotto;
- public Lotto(List<Integer> list) {
- lotto = new ArrayList<>();
- lotto = list;
+ public Lotto(List<LottoNumber> lotto) {
+ Collections.sort(lotto);
+ this.lotto = lotto;
+ }
+
+ public Lotto() {
}
- public List<Integer> getLotto() {
+ public List<LottoNumber> getLotto() {
return lotto;
}
- public void setLotto(List<Integer> lotto) {
+ public void setLotto(List<LottoNumber> lotto) {
this.lotto = lotto;
}
- public boolean isContain(int number) {
- return lotto.contains(number);
+ public int countMatch(List<LottoNumber> winningNumber) {
+ int count = 0;
+ for (LottoNumber lottoNumber : winningNumber) {
+ count = increaseCount(lottoNumber, count);
+ }
+ return count;
+ }
+
+ public int increaseCount(LottoNumber lottoNumber, int count) {
+ if (this.hasThisNumber(lottoNumber)) {
+ count++;
+ }
+ return count;
+ }
+
+ public boolean hasThisNumber(LottoNumber number) {
+ return lotto.stream()
+ .filter(lottoNo -> lottoNo.equals(number)).count() != 0;
}
public String toString() {
- return lotto.toString();
+ return lotto.stream()
+ .map(LottoNumber::toString)
+ .collect(Collectors.joining(", ","[","]"));
}
-}
+}
\ No newline at end of file | Java | ๋ญ ํฌํจํ๊ณ ์๋ค๋๊ฑฐ์ฃ ? |
@@ -1,29 +1,51 @@
package lotto.domain;
-import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import java.util.stream.Collectors;
public class Lotto {
- private List<Integer> lotto;
+ private List<LottoNumber> lotto;
- public Lotto(List<Integer> list) {
- lotto = new ArrayList<>();
- lotto = list;
+ public Lotto(List<LottoNumber> lotto) {
+ Collections.sort(lotto);
+ this.lotto = lotto;
+ }
+
+ public Lotto() {
}
- public List<Integer> getLotto() {
+ public List<LottoNumber> getLotto() {
return lotto;
}
- public void setLotto(List<Integer> lotto) {
+ public void setLotto(List<LottoNumber> lotto) {
this.lotto = lotto;
}
- public boolean isContain(int number) {
- return lotto.contains(number);
+ public int countMatch(List<LottoNumber> winningNumber) {
+ int count = 0;
+ for (LottoNumber lottoNumber : winningNumber) {
+ count = increaseCount(lottoNumber, count);
+ }
+ return count;
+ }
+
+ public int increaseCount(LottoNumber lottoNumber, int count) {
+ if (this.hasThisNumber(lottoNumber)) {
+ count++;
+ }
+ return count;
+ }
+
+ public boolean hasThisNumber(LottoNumber number) {
+ return lotto.stream()
+ .filter(lottoNo -> lottoNo.equals(number)).count() != 0;
}
public String toString() {
- return lotto.toString();
+ return lotto.stream()
+ .map(LottoNumber::toString)
+ .collect(Collectors.joining(", ","[","]"));
}
-}
+}
\ No newline at end of file | Java | Builder ํจํด๊ณผ ๋ฉ์๋ ์ฒด์ด๋ ๊ณต๋ถํ์๊ณ stringBuilder ๋ค์ ์จ๋ณด์ธ์ |
@@ -1,29 +1,51 @@
package lotto.domain;
-import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import java.util.stream.Collectors;
public class Lotto {
- private List<Integer> lotto;
+ private List<LottoNumber> lotto;
- public Lotto(List<Integer> list) {
- lotto = new ArrayList<>();
- lotto = list;
+ public Lotto(List<LottoNumber> lotto) {
+ Collections.sort(lotto);
+ this.lotto = lotto;
+ }
+
+ public Lotto() {
}
- public List<Integer> getLotto() {
+ public List<LottoNumber> getLotto() {
return lotto;
}
- public void setLotto(List<Integer> lotto) {
+ public void setLotto(List<LottoNumber> lotto) {
this.lotto = lotto;
}
- public boolean isContain(int number) {
- return lotto.contains(number);
+ public int countMatch(List<LottoNumber> winningNumber) {
+ int count = 0;
+ for (LottoNumber lottoNumber : winningNumber) {
+ count = increaseCount(lottoNumber, count);
+ }
+ return count;
+ }
+
+ public int increaseCount(LottoNumber lottoNumber, int count) {
+ if (this.hasThisNumber(lottoNumber)) {
+ count++;
+ }
+ return count;
+ }
+
+ public boolean hasThisNumber(LottoNumber number) {
+ return lotto.stream()
+ .filter(lottoNo -> lottoNo.equals(number)).count() != 0;
}
public String toString() {
- return lotto.toString();
+ return lotto.stream()
+ .map(LottoNumber::toString)
+ .collect(Collectors.joining(", ","[","]"));
}
-}
+}
\ No newline at end of file | Java | ๊ทธ๋ฆฌ๊ณ ์ ๋ง toString์ ์ด๋ ๊ฒ ๊ตฌํํ์๊ฒ ์ต๋๊น? |
@@ -3,77 +3,78 @@
import lotto.utils.Splitter;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class LottoGame {
- private List<Lotto> lottos = new ArrayList<Lotto>();
+ private static final int LOTTO_PRICE = 1000;
+ private List<Lotto> lottos;
+ private List<WinningLotto> winningLottos;
+
+ public LottoGame() {
+ lottos = new ArrayList<Lotto>();
+ winningLottos = new ArrayList<WinningLotto>();
+ }
public List<Lotto> getLottos() {
return lottos;
}
- public void setLottos(List<Lotto> lottos) {
- this.lottos = lottos;
+ public List<WinningLotto> getWinningLottos() {
+ return winningLottos;
}
- public List<Lotto> purchase(int money) {
- for (int numbers = 0; numbers < changeUnit(money); numbers++) {
- List<Integer> randomNumber = generateRandomNumbers();
- Collections.sort(randomNumber);
- Lotto lotto = new Lotto(randomNumber);
- lottos.add(lotto);
+ public void purchaseManual(String[] continuousNumbers) {
+ for (String continuousNumber : continuousNumbers) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.manual(continuousNumber));
}
- return lottos;
}
- public static List<Integer> generateRandomNumbers() {
- List<Integer> randomNumbers = new ArrayList<Integer>();
- for (int number = 1; number <= 45; number++) {
- randomNumbers.add(number);
+ public void setWinningLottos(String continuousWinningNumbers, LottoNumber bonusNumber) {
+ List<LottoNumber> winningNumbers = Splitter.splitNumber(continuousWinningNumbers);
+ winningLottos = new ArrayList<>();
+ for (Lotto lotto : lottos) {
+ setWinningLotto(lotto, lotto.countMatch(winningNumbers), bonusNumber);
}
- Collections.shuffle(randomNumbers);
- return randomNumbers.subList(0, 6);
}
- public int[] saveLottoResult(String text) {
- List<Integer> winnerNumber = Splitter.splitNumber(text);
- int[] result = new int[NumberOfHits.values().length];
- for (int index = 0; index < lottos.size(); index++) {
- result[countMatch(winnerNumber, index)]++;
+ public void setWinningLotto(Lotto lotto, int count, LottoNumber bonusNumber) {
+ if (Rank.lookUpRank(count, lotto.hasThisNumber(bonusNumber)) != Rank.MISS) {
+ winningLottos.add(new WinningLotto(lotto, count, bonusNumber));
}
- return result;
}
- public int countMatch(List<Integer> winnerNumber, int index) {
- int count = 0;
- for (int number : winnerNumber) {
- count = increaseCount(number, index, count);
+ public List<Lotto> purchaseAuto(int money, int numberOfManual) {
+ for (int numbers = 0; numbers < numberOfAutoLotto(money, numberOfManual); numbers++) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.auto());
}
- return count;
+ return lottos;
}
- public int increaseCount(int number, int index, int count) {
- if (lottos.get(index).isContain(number)) {
- count++;
- }
- return count;
+ public int countSameRank(Rank rank) {
+ return (int) winningLottos.stream()
+ .filter(winningLotto -> winningLotto.findRank().equals(rank))
+ .count();
}
- public double calculateRate(int[] result, int outcome) {
- int income = calculateReward(result);
- return Math.round((double)income / outcome * 1000.0) / 10.0 ;
+ public double calculateRate(int outcome) {
+ return (double) calculateIncome() / outcome * 100.0;
}
- public int calculateReward(int[] result) {
- int total = 0;
- for (int index = 0; index < result.length; index++) {
- total += result[index] * NumberOfHits.values()[index].getReward();
+ public int calculateIncome() {
+ int income = 0;
+ for (WinningLotto winningLotto : winningLottos) {
+ income = winningLotto.findRank().plusReward(income);
}
- return total;
+ return income;
}
public int changeUnit(int totalPrice) {
- return totalPrice / 1000;
+ return totalPrice / LOTTO_PRICE;
+ }
+
+ public int numberOfAutoLotto(int money, int numberOfManual) {
+ return changeUnit(money) - numberOfManual;
}
-}
+}
\ No newline at end of file | Java | text ๋ณด๋ค ์ข์ ๋ค์ด๋ฐ์ด ์์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -3,77 +3,78 @@
import lotto.utils.Splitter;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class LottoGame {
- private List<Lotto> lottos = new ArrayList<Lotto>();
+ private static final int LOTTO_PRICE = 1000;
+ private List<Lotto> lottos;
+ private List<WinningLotto> winningLottos;
+
+ public LottoGame() {
+ lottos = new ArrayList<Lotto>();
+ winningLottos = new ArrayList<WinningLotto>();
+ }
public List<Lotto> getLottos() {
return lottos;
}
- public void setLottos(List<Lotto> lottos) {
- this.lottos = lottos;
+ public List<WinningLotto> getWinningLottos() {
+ return winningLottos;
}
- public List<Lotto> purchase(int money) {
- for (int numbers = 0; numbers < changeUnit(money); numbers++) {
- List<Integer> randomNumber = generateRandomNumbers();
- Collections.sort(randomNumber);
- Lotto lotto = new Lotto(randomNumber);
- lottos.add(lotto);
+ public void purchaseManual(String[] continuousNumbers) {
+ for (String continuousNumber : continuousNumbers) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.manual(continuousNumber));
}
- return lottos;
}
- public static List<Integer> generateRandomNumbers() {
- List<Integer> randomNumbers = new ArrayList<Integer>();
- for (int number = 1; number <= 45; number++) {
- randomNumbers.add(number);
+ public void setWinningLottos(String continuousWinningNumbers, LottoNumber bonusNumber) {
+ List<LottoNumber> winningNumbers = Splitter.splitNumber(continuousWinningNumbers);
+ winningLottos = new ArrayList<>();
+ for (Lotto lotto : lottos) {
+ setWinningLotto(lotto, lotto.countMatch(winningNumbers), bonusNumber);
}
- Collections.shuffle(randomNumbers);
- return randomNumbers.subList(0, 6);
}
- public int[] saveLottoResult(String text) {
- List<Integer> winnerNumber = Splitter.splitNumber(text);
- int[] result = new int[NumberOfHits.values().length];
- for (int index = 0; index < lottos.size(); index++) {
- result[countMatch(winnerNumber, index)]++;
+ public void setWinningLotto(Lotto lotto, int count, LottoNumber bonusNumber) {
+ if (Rank.lookUpRank(count, lotto.hasThisNumber(bonusNumber)) != Rank.MISS) {
+ winningLottos.add(new WinningLotto(lotto, count, bonusNumber));
}
- return result;
}
- public int countMatch(List<Integer> winnerNumber, int index) {
- int count = 0;
- for (int number : winnerNumber) {
- count = increaseCount(number, index, count);
+ public List<Lotto> purchaseAuto(int money, int numberOfManual) {
+ for (int numbers = 0; numbers < numberOfAutoLotto(money, numberOfManual); numbers++) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.auto());
}
- return count;
+ return lottos;
}
- public int increaseCount(int number, int index, int count) {
- if (lottos.get(index).isContain(number)) {
- count++;
- }
- return count;
+ public int countSameRank(Rank rank) {
+ return (int) winningLottos.stream()
+ .filter(winningLotto -> winningLotto.findRank().equals(rank))
+ .count();
}
- public double calculateRate(int[] result, int outcome) {
- int income = calculateReward(result);
- return Math.round((double)income / outcome * 1000.0) / 10.0 ;
+ public double calculateRate(int outcome) {
+ return (double) calculateIncome() / outcome * 100.0;
}
- public int calculateReward(int[] result) {
- int total = 0;
- for (int index = 0; index < result.length; index++) {
- total += result[index] * NumberOfHits.values()[index].getReward();
+ public int calculateIncome() {
+ int income = 0;
+ for (WinningLotto winningLotto : winningLottos) {
+ income = winningLotto.findRank().plusReward(income);
}
- return total;
+ return income;
}
public int changeUnit(int totalPrice) {
- return totalPrice / 1000;
+ return totalPrice / LOTTO_PRICE;
+ }
+
+ public int numberOfAutoLotto(int money, int numberOfManual) {
+ return changeUnit(money) - numberOfManual;
}
-}
+}
\ No newline at end of file | Java | contrivedNumber ๋ณด๋ค ์ข์ ๋ค์ด๋ฐ์ด ์์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -3,77 +3,78 @@
import lotto.utils.Splitter;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class LottoGame {
- private List<Lotto> lottos = new ArrayList<Lotto>();
+ private static final int LOTTO_PRICE = 1000;
+ private List<Lotto> lottos;
+ private List<WinningLotto> winningLottos;
+
+ public LottoGame() {
+ lottos = new ArrayList<Lotto>();
+ winningLottos = new ArrayList<WinningLotto>();
+ }
public List<Lotto> getLottos() {
return lottos;
}
- public void setLottos(List<Lotto> lottos) {
- this.lottos = lottos;
+ public List<WinningLotto> getWinningLottos() {
+ return winningLottos;
}
- public List<Lotto> purchase(int money) {
- for (int numbers = 0; numbers < changeUnit(money); numbers++) {
- List<Integer> randomNumber = generateRandomNumbers();
- Collections.sort(randomNumber);
- Lotto lotto = new Lotto(randomNumber);
- lottos.add(lotto);
+ public void purchaseManual(String[] continuousNumbers) {
+ for (String continuousNumber : continuousNumbers) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.manual(continuousNumber));
}
- return lottos;
}
- public static List<Integer> generateRandomNumbers() {
- List<Integer> randomNumbers = new ArrayList<Integer>();
- for (int number = 1; number <= 45; number++) {
- randomNumbers.add(number);
+ public void setWinningLottos(String continuousWinningNumbers, LottoNumber bonusNumber) {
+ List<LottoNumber> winningNumbers = Splitter.splitNumber(continuousWinningNumbers);
+ winningLottos = new ArrayList<>();
+ for (Lotto lotto : lottos) {
+ setWinningLotto(lotto, lotto.countMatch(winningNumbers), bonusNumber);
}
- Collections.shuffle(randomNumbers);
- return randomNumbers.subList(0, 6);
}
- public int[] saveLottoResult(String text) {
- List<Integer> winnerNumber = Splitter.splitNumber(text);
- int[] result = new int[NumberOfHits.values().length];
- for (int index = 0; index < lottos.size(); index++) {
- result[countMatch(winnerNumber, index)]++;
+ public void setWinningLotto(Lotto lotto, int count, LottoNumber bonusNumber) {
+ if (Rank.lookUpRank(count, lotto.hasThisNumber(bonusNumber)) != Rank.MISS) {
+ winningLottos.add(new WinningLotto(lotto, count, bonusNumber));
}
- return result;
}
- public int countMatch(List<Integer> winnerNumber, int index) {
- int count = 0;
- for (int number : winnerNumber) {
- count = increaseCount(number, index, count);
+ public List<Lotto> purchaseAuto(int money, int numberOfManual) {
+ for (int numbers = 0; numbers < numberOfAutoLotto(money, numberOfManual); numbers++) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.auto());
}
- return count;
+ return lottos;
}
- public int increaseCount(int number, int index, int count) {
- if (lottos.get(index).isContain(number)) {
- count++;
- }
- return count;
+ public int countSameRank(Rank rank) {
+ return (int) winningLottos.stream()
+ .filter(winningLotto -> winningLotto.findRank().equals(rank))
+ .count();
}
- public double calculateRate(int[] result, int outcome) {
- int income = calculateReward(result);
- return Math.round((double)income / outcome * 1000.0) / 10.0 ;
+ public double calculateRate(int outcome) {
+ return (double) calculateIncome() / outcome * 100.0;
}
- public int calculateReward(int[] result) {
- int total = 0;
- for (int index = 0; index < result.length; index++) {
- total += result[index] * NumberOfHits.values()[index].getReward();
+ public int calculateIncome() {
+ int income = 0;
+ for (WinningLotto winningLotto : winningLottos) {
+ income = winningLotto.findRank().plusReward(income);
}
- return total;
+ return income;
}
public int changeUnit(int totalPrice) {
- return totalPrice / 1000;
+ return totalPrice / LOTTO_PRICE;
+ }
+
+ public int numberOfAutoLotto(int money, int numberOfManual) {
+ return changeUnit(money) - numberOfManual;
}
-}
+}
\ No newline at end of file | Java | ์ด๋์๋ menual์ด๊ณ ์ฌ๊ธฐ์๋ manual์ด๊ณ ์คํ ๋ง์ด ๋๋์ฒด ๋ญ์ฃ ? |
@@ -0,0 +1,67 @@
+package lotto.domain;
+
+import java.util.Arrays;
+
+public enum Rank {
+ FIRST(6, 2000000000),
+ SECOND(5, 30000000),
+ THIRD(5, 1500000),
+ FOURTH(4, 50000),
+ FIFTH(3, 5000),
+ MISS(0, 0);
+
+ private static final int PRIZE_MIN_NUMBER = 3;
+
+ private int matchCount;
+ private int winningPrice;
+
+ private Rank(int matchCount, int winningPrice) {
+ this.matchCount = matchCount;
+ this.winningPrice = winningPrice;
+ }
+
+ public int getMatchCount() {
+ return matchCount;
+ }
+
+ public int getWinningPrice() {
+ return winningPrice;
+ }
+
+ public static Rank lookUpRank(int matchCount, boolean hasBonusNumber) {
+ if (matchCount < PRIZE_MIN_NUMBER) {
+ return MISS;
+ }
+
+ if (SECOND.isRightCount(matchCount) && hasBonusNumber) {
+ return SECOND;
+ }
+
+ if (THIRD.isRightCount(matchCount)) {
+ return THIRD;
+ }
+
+ return (Rank) Arrays.stream(values()).filter(rank -> rank.isRightCount(matchCount)).toArray()[0];
+ }
+
+ public boolean isRightCount(int matchCount) {
+ return this.matchCount == matchCount;
+ }
+
+ public int plusReward(int income) {
+ return income + winningPrice;
+ }
+
+ public String toString() {
+ StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append(this.matchCount)
+ .append("๊ฐ ์ผ์น");
+ if (this.winningPrice == Rank.SECOND.winningPrice) {
+ stringBuilder.append(", ๋ณด๋์ค ๋ณผ ์ผ์น");
+ }
+ stringBuilder.append("(")
+ .append(this.winningPrice)
+ .append(")-");
+ return stringBuilder.toString();
+ }
+}
\ No newline at end of file | Java | ์ข ๋ ์ข์ ๋ฉ์๋ ๋ค์ด๋ฐ |
@@ -0,0 +1,67 @@
+package lotto.domain;
+
+import java.util.Arrays;
+
+public enum Rank {
+ FIRST(6, 2000000000),
+ SECOND(5, 30000000),
+ THIRD(5, 1500000),
+ FOURTH(4, 50000),
+ FIFTH(3, 5000),
+ MISS(0, 0);
+
+ private static final int PRIZE_MIN_NUMBER = 3;
+
+ private int matchCount;
+ private int winningPrice;
+
+ private Rank(int matchCount, int winningPrice) {
+ this.matchCount = matchCount;
+ this.winningPrice = winningPrice;
+ }
+
+ public int getMatchCount() {
+ return matchCount;
+ }
+
+ public int getWinningPrice() {
+ return winningPrice;
+ }
+
+ public static Rank lookUpRank(int matchCount, boolean hasBonusNumber) {
+ if (matchCount < PRIZE_MIN_NUMBER) {
+ return MISS;
+ }
+
+ if (SECOND.isRightCount(matchCount) && hasBonusNumber) {
+ return SECOND;
+ }
+
+ if (THIRD.isRightCount(matchCount)) {
+ return THIRD;
+ }
+
+ return (Rank) Arrays.stream(values()).filter(rank -> rank.isRightCount(matchCount)).toArray()[0];
+ }
+
+ public boolean isRightCount(int matchCount) {
+ return this.matchCount == matchCount;
+ }
+
+ public int plusReward(int income) {
+ return income + winningPrice;
+ }
+
+ public String toString() {
+ StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append(this.matchCount)
+ .append("๊ฐ ์ผ์น");
+ if (this.winningPrice == Rank.SECOND.winningPrice) {
+ stringBuilder.append(", ๋ณด๋์ค ๋ณผ ์ผ์น");
+ }
+ stringBuilder.append("(")
+ .append(this.winningPrice)
+ .append(")-");
+ return stringBuilder.toString();
+ }
+}
\ No newline at end of file | Java | set์ด๋ผ๋ ๋ช
์นญ์ ํด๋์ค ํ๋๋ค์ ๊ฐ์ ์ธํ
ํ ๋ ์ฌ์ฉํ๋๊น ๋ค๋ฅธ ๋ช
์นญ์ด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -3,77 +3,78 @@
import lotto.utils.Splitter;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class LottoGame {
- private List<Lotto> lottos = new ArrayList<Lotto>();
+ private static final int LOTTO_PRICE = 1000;
+ private List<Lotto> lottos;
+ private List<WinningLotto> winningLottos;
+
+ public LottoGame() {
+ lottos = new ArrayList<Lotto>();
+ winningLottos = new ArrayList<WinningLotto>();
+ }
public List<Lotto> getLottos() {
return lottos;
}
- public void setLottos(List<Lotto> lottos) {
- this.lottos = lottos;
+ public List<WinningLotto> getWinningLottos() {
+ return winningLottos;
}
- public List<Lotto> purchase(int money) {
- for (int numbers = 0; numbers < changeUnit(money); numbers++) {
- List<Integer> randomNumber = generateRandomNumbers();
- Collections.sort(randomNumber);
- Lotto lotto = new Lotto(randomNumber);
- lottos.add(lotto);
+ public void purchaseManual(String[] continuousNumbers) {
+ for (String continuousNumber : continuousNumbers) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.manual(continuousNumber));
}
- return lottos;
}
- public static List<Integer> generateRandomNumbers() {
- List<Integer> randomNumbers = new ArrayList<Integer>();
- for (int number = 1; number <= 45; number++) {
- randomNumbers.add(number);
+ public void setWinningLottos(String continuousWinningNumbers, LottoNumber bonusNumber) {
+ List<LottoNumber> winningNumbers = Splitter.splitNumber(continuousWinningNumbers);
+ winningLottos = new ArrayList<>();
+ for (Lotto lotto : lottos) {
+ setWinningLotto(lotto, lotto.countMatch(winningNumbers), bonusNumber);
}
- Collections.shuffle(randomNumbers);
- return randomNumbers.subList(0, 6);
}
- public int[] saveLottoResult(String text) {
- List<Integer> winnerNumber = Splitter.splitNumber(text);
- int[] result = new int[NumberOfHits.values().length];
- for (int index = 0; index < lottos.size(); index++) {
- result[countMatch(winnerNumber, index)]++;
+ public void setWinningLotto(Lotto lotto, int count, LottoNumber bonusNumber) {
+ if (Rank.lookUpRank(count, lotto.hasThisNumber(bonusNumber)) != Rank.MISS) {
+ winningLottos.add(new WinningLotto(lotto, count, bonusNumber));
}
- return result;
}
- public int countMatch(List<Integer> winnerNumber, int index) {
- int count = 0;
- for (int number : winnerNumber) {
- count = increaseCount(number, index, count);
+ public List<Lotto> purchaseAuto(int money, int numberOfManual) {
+ for (int numbers = 0; numbers < numberOfAutoLotto(money, numberOfManual); numbers++) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.auto());
}
- return count;
+ return lottos;
}
- public int increaseCount(int number, int index, int count) {
- if (lottos.get(index).isContain(number)) {
- count++;
- }
- return count;
+ public int countSameRank(Rank rank) {
+ return (int) winningLottos.stream()
+ .filter(winningLotto -> winningLotto.findRank().equals(rank))
+ .count();
}
- public double calculateRate(int[] result, int outcome) {
- int income = calculateReward(result);
- return Math.round((double)income / outcome * 1000.0) / 10.0 ;
+ public double calculateRate(int outcome) {
+ return (double) calculateIncome() / outcome * 100.0;
}
- public int calculateReward(int[] result) {
- int total = 0;
- for (int index = 0; index < result.length; index++) {
- total += result[index] * NumberOfHits.values()[index].getReward();
+ public int calculateIncome() {
+ int income = 0;
+ for (WinningLotto winningLotto : winningLottos) {
+ income = winningLotto.findRank().plusReward(income);
}
- return total;
+ return income;
}
public int changeUnit(int totalPrice) {
- return totalPrice / 1000;
+ return totalPrice / LOTTO_PRICE;
+ }
+
+ public int numberOfAutoLotto(int money, int numberOfManual) {
+ return changeUnit(money) - numberOfManual;
}
-}
+}
\ No newline at end of file | Java | ์คํ์
๋๋ค |
@@ -3,77 +3,78 @@
import lotto.utils.Splitter;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class LottoGame {
- private List<Lotto> lottos = new ArrayList<Lotto>();
+ private static final int LOTTO_PRICE = 1000;
+ private List<Lotto> lottos;
+ private List<WinningLotto> winningLottos;
+
+ public LottoGame() {
+ lottos = new ArrayList<Lotto>();
+ winningLottos = new ArrayList<WinningLotto>();
+ }
public List<Lotto> getLottos() {
return lottos;
}
- public void setLottos(List<Lotto> lottos) {
- this.lottos = lottos;
+ public List<WinningLotto> getWinningLottos() {
+ return winningLottos;
}
- public List<Lotto> purchase(int money) {
- for (int numbers = 0; numbers < changeUnit(money); numbers++) {
- List<Integer> randomNumber = generateRandomNumbers();
- Collections.sort(randomNumber);
- Lotto lotto = new Lotto(randomNumber);
- lottos.add(lotto);
+ public void purchaseManual(String[] continuousNumbers) {
+ for (String continuousNumber : continuousNumbers) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.manual(continuousNumber));
}
- return lottos;
}
- public static List<Integer> generateRandomNumbers() {
- List<Integer> randomNumbers = new ArrayList<Integer>();
- for (int number = 1; number <= 45; number++) {
- randomNumbers.add(number);
+ public void setWinningLottos(String continuousWinningNumbers, LottoNumber bonusNumber) {
+ List<LottoNumber> winningNumbers = Splitter.splitNumber(continuousWinningNumbers);
+ winningLottos = new ArrayList<>();
+ for (Lotto lotto : lottos) {
+ setWinningLotto(lotto, lotto.countMatch(winningNumbers), bonusNumber);
}
- Collections.shuffle(randomNumbers);
- return randomNumbers.subList(0, 6);
}
- public int[] saveLottoResult(String text) {
- List<Integer> winnerNumber = Splitter.splitNumber(text);
- int[] result = new int[NumberOfHits.values().length];
- for (int index = 0; index < lottos.size(); index++) {
- result[countMatch(winnerNumber, index)]++;
+ public void setWinningLotto(Lotto lotto, int count, LottoNumber bonusNumber) {
+ if (Rank.lookUpRank(count, lotto.hasThisNumber(bonusNumber)) != Rank.MISS) {
+ winningLottos.add(new WinningLotto(lotto, count, bonusNumber));
}
- return result;
}
- public int countMatch(List<Integer> winnerNumber, int index) {
- int count = 0;
- for (int number : winnerNumber) {
- count = increaseCount(number, index, count);
+ public List<Lotto> purchaseAuto(int money, int numberOfManual) {
+ for (int numbers = 0; numbers < numberOfAutoLotto(money, numberOfManual); numbers++) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.auto());
}
- return count;
+ return lottos;
}
- public int increaseCount(int number, int index, int count) {
- if (lottos.get(index).isContain(number)) {
- count++;
- }
- return count;
+ public int countSameRank(Rank rank) {
+ return (int) winningLottos.stream()
+ .filter(winningLotto -> winningLotto.findRank().equals(rank))
+ .count();
}
- public double calculateRate(int[] result, int outcome) {
- int income = calculateReward(result);
- return Math.round((double)income / outcome * 1000.0) / 10.0 ;
+ public double calculateRate(int outcome) {
+ return (double) calculateIncome() / outcome * 100.0;
}
- public int calculateReward(int[] result) {
- int total = 0;
- for (int index = 0; index < result.length; index++) {
- total += result[index] * NumberOfHits.values()[index].getReward();
+ public int calculateIncome() {
+ int income = 0;
+ for (WinningLotto winningLotto : winningLottos) {
+ income = winningLotto.findRank().plusReward(income);
}
- return total;
+ return income;
}
public int changeUnit(int totalPrice) {
- return totalPrice / 1000;
+ return totalPrice / LOTTO_PRICE;
+ }
+
+ public int numberOfAutoLotto(int money, int numberOfManual) {
+ return changeUnit(money) - numberOfManual;
}
-}
+}
\ No newline at end of file | Java | 1000์ด๋ ๊ฐ์ ํ๋์ฝ๋ฉ์ค |
@@ -0,0 +1,17 @@
+package lotto.domain;
+
+public class WinningLotto extends Lotto {
+ private Lotto lotto;
+ private int matchCount;
+ private boolean hasbonusNumber;
+
+ public WinningLotto(Lotto lotto, int matchCount, LottoNumber bonusNumber) {
+ this.hasbonusNumber = lotto.hasThisNumber(bonusNumber);
+ this.lotto = lotto;
+ this.matchCount = matchCount;
+ }
+
+ public Rank findRank() {
+ return Rank.lookUpRank(matchCount, hasbonusNumber);
+ }
+}
\ No newline at end of file | Java | get set ์ง์๋ณด๊ณ ์๊ฐํด๋ณด์ญ์ผ (๊ฐ์ฒด์งํฅ ์ํ์ฒด์กฐ ๊ท์น get set ์ฌ์ฉ์ ์์ ํ๋ค) |
@@ -3,77 +3,78 @@
import lotto.utils.Splitter;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class LottoGame {
- private List<Lotto> lottos = new ArrayList<Lotto>();
+ private static final int LOTTO_PRICE = 1000;
+ private List<Lotto> lottos;
+ private List<WinningLotto> winningLottos;
+
+ public LottoGame() {
+ lottos = new ArrayList<Lotto>();
+ winningLottos = new ArrayList<WinningLotto>();
+ }
public List<Lotto> getLottos() {
return lottos;
}
- public void setLottos(List<Lotto> lottos) {
- this.lottos = lottos;
+ public List<WinningLotto> getWinningLottos() {
+ return winningLottos;
}
- public List<Lotto> purchase(int money) {
- for (int numbers = 0; numbers < changeUnit(money); numbers++) {
- List<Integer> randomNumber = generateRandomNumbers();
- Collections.sort(randomNumber);
- Lotto lotto = new Lotto(randomNumber);
- lottos.add(lotto);
+ public void purchaseManual(String[] continuousNumbers) {
+ for (String continuousNumber : continuousNumbers) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.manual(continuousNumber));
}
- return lottos;
}
- public static List<Integer> generateRandomNumbers() {
- List<Integer> randomNumbers = new ArrayList<Integer>();
- for (int number = 1; number <= 45; number++) {
- randomNumbers.add(number);
+ public void setWinningLottos(String continuousWinningNumbers, LottoNumber bonusNumber) {
+ List<LottoNumber> winningNumbers = Splitter.splitNumber(continuousWinningNumbers);
+ winningLottos = new ArrayList<>();
+ for (Lotto lotto : lottos) {
+ setWinningLotto(lotto, lotto.countMatch(winningNumbers), bonusNumber);
}
- Collections.shuffle(randomNumbers);
- return randomNumbers.subList(0, 6);
}
- public int[] saveLottoResult(String text) {
- List<Integer> winnerNumber = Splitter.splitNumber(text);
- int[] result = new int[NumberOfHits.values().length];
- for (int index = 0; index < lottos.size(); index++) {
- result[countMatch(winnerNumber, index)]++;
+ public void setWinningLotto(Lotto lotto, int count, LottoNumber bonusNumber) {
+ if (Rank.lookUpRank(count, lotto.hasThisNumber(bonusNumber)) != Rank.MISS) {
+ winningLottos.add(new WinningLotto(lotto, count, bonusNumber));
}
- return result;
}
- public int countMatch(List<Integer> winnerNumber, int index) {
- int count = 0;
- for (int number : winnerNumber) {
- count = increaseCount(number, index, count);
+ public List<Lotto> purchaseAuto(int money, int numberOfManual) {
+ for (int numbers = 0; numbers < numberOfAutoLotto(money, numberOfManual); numbers++) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.auto());
}
- return count;
+ return lottos;
}
- public int increaseCount(int number, int index, int count) {
- if (lottos.get(index).isContain(number)) {
- count++;
- }
- return count;
+ public int countSameRank(Rank rank) {
+ return (int) winningLottos.stream()
+ .filter(winningLotto -> winningLotto.findRank().equals(rank))
+ .count();
}
- public double calculateRate(int[] result, int outcome) {
- int income = calculateReward(result);
- return Math.round((double)income / outcome * 1000.0) / 10.0 ;
+ public double calculateRate(int outcome) {
+ return (double) calculateIncome() / outcome * 100.0;
}
- public int calculateReward(int[] result) {
- int total = 0;
- for (int index = 0; index < result.length; index++) {
- total += result[index] * NumberOfHits.values()[index].getReward();
+ public int calculateIncome() {
+ int income = 0;
+ for (WinningLotto winningLotto : winningLottos) {
+ income = winningLotto.findRank().plusReward(income);
}
- return total;
+ return income;
}
public int changeUnit(int totalPrice) {
- return totalPrice / 1000;
+ return totalPrice / LOTTO_PRICE;
+ }
+
+ public int numberOfAutoLotto(int money, int numberOfManual) {
+ return changeUnit(money) - numberOfManual;
}
-}
+}
\ No newline at end of file | Java | ์ด ํด๋์ค๊ฐ ๋๋ฌด ๋ฌด๊ฑฐ์๋ณด์
๋๋ค. ๋๋ฌด ๋ง์ ์ฑ
์์ ๊ฐ๊ณ ์๋๊ฑด ์๋์ง ํ๋ฒ ๋ด๋ณด์ธ์ |
@@ -0,0 +1,32 @@
+package lotto.domain;
+
+import lotto.utils.Splitter;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class LottoGenerator {
+ List<LottoNumber> randomNumbers;
+
+ public LottoGenerator() {
+ randomNumbers = new ArrayList<>();
+ }
+
+ public Lotto manual(String continuousNumber) {
+ randomNumbers = Splitter.splitNumber(continuousNumber);
+ return new Lotto(randomNumbers);
+ }
+
+ public Lotto auto() {
+ return new Lotto(generateRandomNumbers());
+ }
+
+ public List<LottoNumber> generateRandomNumbers() {
+ for (int number = 1; number <= 45; number++) {
+ randomNumbers.add(new LottoNumber(number));
+ }
+ Collections.shuffle(randomNumbers);
+ return randomNumbers.subList(0, 6);
+ }
+}
\ No newline at end of file | Java | generateAuto() ๊ฐ ๋ซ์ง ์์๊น์ |
@@ -0,0 +1,32 @@
+package lotto.domain;
+
+import lotto.utils.Splitter;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class LottoGenerator {
+ List<LottoNumber> randomNumbers;
+
+ public LottoGenerator() {
+ randomNumbers = new ArrayList<>();
+ }
+
+ public Lotto manual(String continuousNumber) {
+ randomNumbers = Splitter.splitNumber(continuousNumber);
+ return new Lotto(randomNumbers);
+ }
+
+ public Lotto auto() {
+ return new Lotto(generateRandomNumbers());
+ }
+
+ public List<LottoNumber> generateRandomNumbers() {
+ for (int number = 1; number <= 45; number++) {
+ randomNumbers.add(new LottoNumber(number));
+ }
+ Collections.shuffle(randomNumbers);
+ return randomNumbers.subList(0, 6);
+ }
+}
\ No newline at end of file | Java | generateMenual ์ด ๋ซ์ง ์์๊น์. menual์ธ์ง manual์ธ์ง ํต์ผ ์์ผ๋ฌ๋ผ๋๊น ๋ฌด์๋นํ๋ค์ |
@@ -15,7 +15,7 @@ const CenterWrapper = styled.div`
overflow: scroll;
height: calc(100dvh - 126px);
align-items: center;
- justigy-content: space-between;
+ justify-content: space-between;
padding-top: 80px;
&::-webkit-scrollbar {
display: none;
@@ -155,47 +155,52 @@ const ReviewPage = () => {
};
return (
- <><AppBar onBack={() => navigate(-1)} title="์ฐ์ฑ
๋ก ๋ฆฌ๋ทฐ" /><CenterWrapper>
- <ContentWrapper>
- <TitleWrapper>
- <RequiredMark>*</RequiredMark>
- <Title>์ด๋ฒ ์ฐ์ฑ
์ ์ด๋ ์
จ๋์?</Title>
- </TitleWrapper>
-
- <StarContainer>
- {[1, 2, 3, 4, 5].map((value) => (
- <StyledStarIcon
- key={value}
- isactive={((hoveredRating || rating) >= value).toString()}
- onMouseEnter={() => setHoveredRating(value)}
- onMouseLeave={() => setHoveredRating(0)}
- onClick={() => setRating(value)} />
- ))}
- </StarContainer>
-
- <Divider margin="0 0 2rem 0" height="3px" />
-
- <TitleWrapper>
- <RequiredMark>*</RequiredMark>
- <Title fontSize="13px">์ฐ์ฑ
๋ก์ ๋ํ ์์ง ๋ฆฌ๋ทฐ๋ฅผ ๋จ๊ฒจ์ฃผ์ธ์!</Title>
- </TitleWrapper>
-
- <TextAreaWrapper>
- <TextArea
- value={review}
- onChange={(e) => setReview(e.target.value.slice(0, 100))}
- placeholder="๋ฆฌ๋ทฐ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์" />
- <CharCount>
- <span>{review.length}</span> / 100
- </CharCount>
- </TextAreaWrapper>
- </ContentWrapper>
-
- <Button isActive={isActive} onClick={handleSubmit}>
- ๋ฆฌ๋ทฐ ๋ฑ๋กํ๊ธฐ
- </Button>
- </CenterWrapper><BottomNavigation /></>
-
+ <>
+ <AppBar onBack={() => navigate(-1)} title="์ฐ์ฑ
๋ก ๋ฆฌ๋ทฐ" />
+ <CenterWrapper>
+ <ContentWrapper>
+ <TitleWrapper>
+ <RequiredMark>*</RequiredMark>
+ <Title>์ด๋ฒ ์ฐ์ฑ
์ ์ด๋ ์
จ๋์?</Title>
+ </TitleWrapper>
+
+ <StarContainer>
+ {[1, 2, 3, 4, 5].map((value) => (
+ <StyledStarIcon
+ key={value}
+ isactive={((hoveredRating || rating) >= value).toString()}
+ onMouseEnter={() => setHoveredRating(value)}
+ onMouseLeave={() => setHoveredRating(0)}
+ onClick={() => setRating(value)}
+ />
+ ))}
+ </StarContainer>
+
+ <Divider margin="0 0 2rem 0" height="3px" />
+
+ <TitleWrapper>
+ <RequiredMark>*</RequiredMark>
+ <Title fontSize="13px">์ฐ์ฑ
๋ก์ ๋ํ ์์ง ๋ฆฌ๋ทฐ๋ฅผ ๋จ๊ฒจ์ฃผ์ธ์!</Title>
+ </TitleWrapper>
+
+ <TextAreaWrapper>
+ <TextArea
+ value={review}
+ onChange={(e) => setReview(e.target.value.slice(0, 100))}
+ placeholder="๋ฆฌ๋ทฐ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"
+ />
+ <CharCount>
+ <span>{review.length}</span> / 100
+ </CharCount>
+ </TextAreaWrapper>
+ </ContentWrapper>
+
+ <Button isActive={isActive} onClick={handleSubmit}>
+ ๋ฆฌ๋ทฐ ๋ฑ๋กํ๊ธฐ
+ </Button>
+ </CenterWrapper>
+ <BottomNavigation />
+ </>
);
};
| Unknown | `handleSubmit`์ ์๋ ๋ฆฌ๋ทฐ ๋ฑ๋ก api ๋ก์ง๋ `review.ts`ํ์ผ์ ๋ถ๋ฆฌํ๋ฉด ๊ฐ๋
์ฑ์ด ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -15,7 +15,7 @@ const CenterWrapper = styled.div`
overflow: scroll;
height: calc(100dvh - 126px);
align-items: center;
- justigy-content: space-between;
+ justify-content: space-between;
padding-top: 80px;
&::-webkit-scrollbar {
display: none;
@@ -155,47 +155,52 @@ const ReviewPage = () => {
};
return (
- <><AppBar onBack={() => navigate(-1)} title="์ฐ์ฑ
๋ก ๋ฆฌ๋ทฐ" /><CenterWrapper>
- <ContentWrapper>
- <TitleWrapper>
- <RequiredMark>*</RequiredMark>
- <Title>์ด๋ฒ ์ฐ์ฑ
์ ์ด๋ ์
จ๋์?</Title>
- </TitleWrapper>
-
- <StarContainer>
- {[1, 2, 3, 4, 5].map((value) => (
- <StyledStarIcon
- key={value}
- isactive={((hoveredRating || rating) >= value).toString()}
- onMouseEnter={() => setHoveredRating(value)}
- onMouseLeave={() => setHoveredRating(0)}
- onClick={() => setRating(value)} />
- ))}
- </StarContainer>
-
- <Divider margin="0 0 2rem 0" height="3px" />
-
- <TitleWrapper>
- <RequiredMark>*</RequiredMark>
- <Title fontSize="13px">์ฐ์ฑ
๋ก์ ๋ํ ์์ง ๋ฆฌ๋ทฐ๋ฅผ ๋จ๊ฒจ์ฃผ์ธ์!</Title>
- </TitleWrapper>
-
- <TextAreaWrapper>
- <TextArea
- value={review}
- onChange={(e) => setReview(e.target.value.slice(0, 100))}
- placeholder="๋ฆฌ๋ทฐ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์" />
- <CharCount>
- <span>{review.length}</span> / 100
- </CharCount>
- </TextAreaWrapper>
- </ContentWrapper>
-
- <Button isActive={isActive} onClick={handleSubmit}>
- ๋ฆฌ๋ทฐ ๋ฑ๋กํ๊ธฐ
- </Button>
- </CenterWrapper><BottomNavigation /></>
-
+ <>
+ <AppBar onBack={() => navigate(-1)} title="์ฐ์ฑ
๋ก ๋ฆฌ๋ทฐ" />
+ <CenterWrapper>
+ <ContentWrapper>
+ <TitleWrapper>
+ <RequiredMark>*</RequiredMark>
+ <Title>์ด๋ฒ ์ฐ์ฑ
์ ์ด๋ ์
จ๋์?</Title>
+ </TitleWrapper>
+
+ <StarContainer>
+ {[1, 2, 3, 4, 5].map((value) => (
+ <StyledStarIcon
+ key={value}
+ isactive={((hoveredRating || rating) >= value).toString()}
+ onMouseEnter={() => setHoveredRating(value)}
+ onMouseLeave={() => setHoveredRating(0)}
+ onClick={() => setRating(value)}
+ />
+ ))}
+ </StarContainer>
+
+ <Divider margin="0 0 2rem 0" height="3px" />
+
+ <TitleWrapper>
+ <RequiredMark>*</RequiredMark>
+ <Title fontSize="13px">์ฐ์ฑ
๋ก์ ๋ํ ์์ง ๋ฆฌ๋ทฐ๋ฅผ ๋จ๊ฒจ์ฃผ์ธ์!</Title>
+ </TitleWrapper>
+
+ <TextAreaWrapper>
+ <TextArea
+ value={review}
+ onChange={(e) => setReview(e.target.value.slice(0, 100))}
+ placeholder="๋ฆฌ๋ทฐ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"
+ />
+ <CharCount>
+ <span>{review.length}</span> / 100
+ </CharCount>
+ </TextAreaWrapper>
+ </ContentWrapper>
+
+ <Button isActive={isActive} onClick={handleSubmit}>
+ ๋ฆฌ๋ทฐ ๋ฑ๋กํ๊ธฐ
+ </Button>
+ </CenterWrapper>
+ <BottomNavigation />
+ </>
);
};
| Unknown | ๊ทธ๋ฆฌ๊ณ ์ฑ๊ณต ์ ํ์ด์ง ์ด๋ ์ฝ๋๊ฐ ๋๋ฝ๋ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -37,14 +37,14 @@ export const showReviewRating = async (
// ์ฐ์ฑ
๋ก ๋ฆฌ๋ทฐ ๋ด์ฉ๋ณด๊ธฐ api
export const showReviewContent = async (
walkwayId: string,
- type: string
+ sort: string
): Promise<{
reviews: ReviewContentType[];
}> => {
try {
const response = await instance.get<
ApiResponseFormat<{ reviews: ReviewContentType[] }>
- >(`/walkways/${walkwayId}/review/content?type=${type}`);
+ >(`/walkways/${walkwayId}/review/content?sort=${sort}`);
return response.data.data;
} catch (error) {
throw error; | TypeScript | `showReviewContent` ํธ์ถ ์ ์๋ต์ผ๋ก ๋ฐ์์ค๋ `period` ํ๋๊ฐ ui ์ปดํฌ๋ํธ์ ๋๋ฝ๋ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,79 @@
+import React, { useState } from "react";
+import { useNavigate } from 'react-router-dom';
+import "../../../style/reset.scss";
+import "../../../style/common.scss";
+import "../../../style/variables.scss"
+import "./Login.scss";
+
+
+const Login = () => {
+
+ const [userInfo, setUserInfo] = useState({
+ userId:"์์ด๋",
+ userPw:"ํจ์ค์๋",
+ });
+ const navigate = useNavigate();
+ const goToMain = () => {
+ fetch("http://10.58.52.144:3000/users/signup", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ email: userInfo.userId,
+ password: userInfo.userPw,
+ }),
+ })
+ .then((response) => response.json())
+ .then((result) => {
+ if(result.accessToken) {
+ localStorage.setItem("token", result.accessToken);
+ navigate("/jinheekim-main");
+ }
+ if(result.message === "invalid password") {
+ alert ("๋น๋ฐ๋ฒํธ ํ๋ ธ์");
+ };
+ if(result.message === "specified user does not exist") {
+ alert ("์์ด๋ ํ๋ ธ์");
+ }
+ })
+
+ }
+
+ const handleInput = (event) => {
+ const {value, id} = event.target;
+ setUserInfo({...userInfo, [id]:value});
+ };
+
+ const isValue = userInfo.userId.includes('@') && userInfo.userPw.length >= 5;
+
+ return (
+ <div className="login">
+ <div className="box">
+ <h1>Westagram</h1>
+ <div className="inputTxt">
+ <input
+ id="userId"
+ onChange={handleInput}
+ type="text"
+ className="id"
+ placeholder="์ ํ๋ฒํธ, ์ฌ์ฉ์ ์ด๋ฆ ๋๋ ์ด๋ฉ์ผ"
+ />
+ <input
+ id="userPw"
+ onChange={handleInput}
+ type="password"
+ className="pw"
+ placeholder="๋น๋ฐ๋ฒํธ"
+ />
+ <button type="button" onClick={goToMain} className={isValue ? "abled-button" : 'disabled-button'} disabled={!isValue}>๋ก๊ทธ์ธ</button>
+ <div className="findPw">
+ <a href="#">๋น๋ฐ๋ฒํธ๋ฅผ ์์ผ์
จ๋์?</a>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+};
+
+export default Login;
\ No newline at end of file | JavaScript | ๐ ์ข์ต๋๋ค! ๊ณ์ํด์ ์ด๋ ๊ฒ ์ธ์
๋ด์ฉ ๋ฐ๋ก๋ฐ๋ก ์ ์ฉํด์ฃผ๋ฉด์ ๋ณธ์ธ์ ์ง์์ผ๋ก ๊ฐ์ ธ๊ฐ์ฃผ์๋ฉด ์ข๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,79 @@
+import React, { useState } from "react";
+import { useNavigate } from 'react-router-dom';
+import "../../../style/reset.scss";
+import "../../../style/common.scss";
+import "../../../style/variables.scss"
+import "./Login.scss";
+
+
+const Login = () => {
+
+ const [userInfo, setUserInfo] = useState({
+ userId:"์์ด๋",
+ userPw:"ํจ์ค์๋",
+ });
+ const navigate = useNavigate();
+ const goToMain = () => {
+ fetch("http://10.58.52.144:3000/users/signup", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ email: userInfo.userId,
+ password: userInfo.userPw,
+ }),
+ })
+ .then((response) => response.json())
+ .then((result) => {
+ if(result.accessToken) {
+ localStorage.setItem("token", result.accessToken);
+ navigate("/jinheekim-main");
+ }
+ if(result.message === "invalid password") {
+ alert ("๋น๋ฐ๋ฒํธ ํ๋ ธ์");
+ };
+ if(result.message === "specified user does not exist") {
+ alert ("์์ด๋ ํ๋ ธ์");
+ }
+ })
+
+ }
+
+ const handleInput = (event) => {
+ const {value, id} = event.target;
+ setUserInfo({...userInfo, [id]:value});
+ };
+
+ const isValue = userInfo.userId.includes('@') && userInfo.userPw.length >= 5;
+
+ return (
+ <div className="login">
+ <div className="box">
+ <h1>Westagram</h1>
+ <div className="inputTxt">
+ <input
+ id="userId"
+ onChange={handleInput}
+ type="text"
+ className="id"
+ placeholder="์ ํ๋ฒํธ, ์ฌ์ฉ์ ์ด๋ฆ ๋๋ ์ด๋ฉ์ผ"
+ />
+ <input
+ id="userPw"
+ onChange={handleInput}
+ type="password"
+ className="pw"
+ placeholder="๋น๋ฐ๋ฒํธ"
+ />
+ <button type="button" onClick={goToMain} className={isValue ? "abled-button" : 'disabled-button'} disabled={!isValue}>๋ก๊ทธ์ธ</button>
+ <div className="findPw">
+ <a href="#">๋น๋ฐ๋ฒํธ๋ฅผ ์์ผ์
จ๋์?</a>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+};
+
+export default Login;
\ No newline at end of file | JavaScript | ์ฝ๋์ ์ฃผ์์ด ๋ง์ต๋๋ค!
์ถํ์ ๋ค์ ํ์ตํ ์ฉ๋๋ก ๋จ๊ฒจ๋์ ์ฃผ์์ด๋ผ๋ฉด, ํด๋น๋ด์ฉ์ ๋ฐ๋ก ๋ธ๋ก๊ทธ๋ ๊ฐ์ธ ๋ฉ๋ชจ์ ์์ฑ์ ํด ์ฃผ์๊ณ , ์ฝ๋๋ฅผ github์ ํตํด ์ฌ๋ ค์ฃผ์ค๋๋ ๋ถํ์ํ ์ฝ๋๋ฅผ ์ต๋ํ ์ค์ฌ์ฃผ์ธ์! |
@@ -0,0 +1,79 @@
+import React, { useState } from "react";
+import { useNavigate } from 'react-router-dom';
+import "../../../style/reset.scss";
+import "../../../style/common.scss";
+import "../../../style/variables.scss"
+import "./Login.scss";
+
+
+const Login = () => {
+
+ const [userInfo, setUserInfo] = useState({
+ userId:"์์ด๋",
+ userPw:"ํจ์ค์๋",
+ });
+ const navigate = useNavigate();
+ const goToMain = () => {
+ fetch("http://10.58.52.144:3000/users/signup", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ email: userInfo.userId,
+ password: userInfo.userPw,
+ }),
+ })
+ .then((response) => response.json())
+ .then((result) => {
+ if(result.accessToken) {
+ localStorage.setItem("token", result.accessToken);
+ navigate("/jinheekim-main");
+ }
+ if(result.message === "invalid password") {
+ alert ("๋น๋ฐ๋ฒํธ ํ๋ ธ์");
+ };
+ if(result.message === "specified user does not exist") {
+ alert ("์์ด๋ ํ๋ ธ์");
+ }
+ })
+
+ }
+
+ const handleInput = (event) => {
+ const {value, id} = event.target;
+ setUserInfo({...userInfo, [id]:value});
+ };
+
+ const isValue = userInfo.userId.includes('@') && userInfo.userPw.length >= 5;
+
+ return (
+ <div className="login">
+ <div className="box">
+ <h1>Westagram</h1>
+ <div className="inputTxt">
+ <input
+ id="userId"
+ onChange={handleInput}
+ type="text"
+ className="id"
+ placeholder="์ ํ๋ฒํธ, ์ฌ์ฉ์ ์ด๋ฆ ๋๋ ์ด๋ฉ์ผ"
+ />
+ <input
+ id="userPw"
+ onChange={handleInput}
+ type="password"
+ className="pw"
+ placeholder="๋น๋ฐ๋ฒํธ"
+ />
+ <button type="button" onClick={goToMain} className={isValue ? "abled-button" : 'disabled-button'} disabled={!isValue}>๋ก๊ทธ์ธ</button>
+ <div className="findPw">
+ <a href="#">๋น๋ฐ๋ฒํธ๋ฅผ ์์ผ์
จ๋์?</a>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+};
+
+export default Login;
\ No newline at end of file | JavaScript | ```suggestion
<div className="inputTxt">
```
์ ์ฝ๋ ์ฒ๋ผ js์ ๊ธฐ๋ณธ ์ปจ๋ฒค์
์ `camelCase`์
๋๋ค! className์ ์์ฑํ ๋์๋ ์ ๊ฒฝ์จ์ฃผ์
์ผํฉ๋๋ค! |
@@ -0,0 +1,79 @@
+import React, { useState } from "react";
+import { useNavigate } from 'react-router-dom';
+import "../../../style/reset.scss";
+import "../../../style/common.scss";
+import "../../../style/variables.scss"
+import "./Login.scss";
+
+
+const Login = () => {
+
+ const [userInfo, setUserInfo] = useState({
+ userId:"์์ด๋",
+ userPw:"ํจ์ค์๋",
+ });
+ const navigate = useNavigate();
+ const goToMain = () => {
+ fetch("http://10.58.52.144:3000/users/signup", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ email: userInfo.userId,
+ password: userInfo.userPw,
+ }),
+ })
+ .then((response) => response.json())
+ .then((result) => {
+ if(result.accessToken) {
+ localStorage.setItem("token", result.accessToken);
+ navigate("/jinheekim-main");
+ }
+ if(result.message === "invalid password") {
+ alert ("๋น๋ฐ๋ฒํธ ํ๋ ธ์");
+ };
+ if(result.message === "specified user does not exist") {
+ alert ("์์ด๋ ํ๋ ธ์");
+ }
+ })
+
+ }
+
+ const handleInput = (event) => {
+ const {value, id} = event.target;
+ setUserInfo({...userInfo, [id]:value});
+ };
+
+ const isValue = userInfo.userId.includes('@') && userInfo.userPw.length >= 5;
+
+ return (
+ <div className="login">
+ <div className="box">
+ <h1>Westagram</h1>
+ <div className="inputTxt">
+ <input
+ id="userId"
+ onChange={handleInput}
+ type="text"
+ className="id"
+ placeholder="์ ํ๋ฒํธ, ์ฌ์ฉ์ ์ด๋ฆ ๋๋ ์ด๋ฉ์ผ"
+ />
+ <input
+ id="userPw"
+ onChange={handleInput}
+ type="password"
+ className="pw"
+ placeholder="๋น๋ฐ๋ฒํธ"
+ />
+ <button type="button" onClick={goToMain} className={isValue ? "abled-button" : 'disabled-button'} disabled={!isValue}>๋ก๊ทธ์ธ</button>
+ <div className="findPw">
+ <a href="#">๋น๋ฐ๋ฒํธ๋ฅผ ์์ผ์
จ๋์?</a>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+};
+
+export default Login;
\ No newline at end of file | JavaScript | ํด๋น scssํ์ผ๋ค์ Index.js์์ import ํด์์ ์ ์ญ์ผ๋ก ์ ์ฉ์ํค๋๊ฒ ์ข์ต๋๋ค. |
@@ -0,0 +1,87 @@
+@import '../../../style/variables.scss';
+@import '../../../style/reset.scss';
+
+
+body {
+ font-size: 14px;
+}
+input {
+ outline: none;
+}
+
+.login {
+ width: 100%;
+ height: 100vh;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ flex-direction: column;
+ .box {
+ width: 400px;
+ padding: 40px 0 20px;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ flex-direction: column;
+ border: 1px solid rgb(219, 219, 219);
+
+ h1 {
+ font-size:40px;
+ font-family: 'Lobster';
+ }
+
+ .inputTxt {
+ width: 300px;
+
+ input {
+ display: block;
+ width: 100%;
+ margin: 5px 0;
+ padding: 10px 0 10px 5px;
+ background-color: $grey-color-bg;
+ border-radius: 3px;
+ border: 1px solid $grey-color-border;
+
+ &::placeholder{
+ font-size: 12px;
+ }
+ }
+ }
+ }
+}
+
+@mixin button-style {
+ margin-top: 20px;
+ display: inline-block;
+ width: 100%;
+ text-align: center;
+ text-decoration-line: none;
+ padding: 8px 0;
+ border-radius: 8px;
+ border: 0px;
+ font-weight: bold;
+ color: #fff;
+ cursor: pointer;
+ }
+
+.abled-button {
+ @include button-style;
+ background-color: $point-color-blue;
+}
+
+.disabled-button {
+ @include button-style;
+ background-color: #666;
+}
+
+
+.findPw {
+ text-align: center;
+ margin: 60px 0;
+
+ a {
+ font-size: 9px;
+ color: rgb(0, 55, 107);
+ text-decoration: none;
+ }
+}
\ No newline at end of file | Unknown | ๋ง์ฐฌ๊ฐ์ง ์
๋๋ค! font๊ฐ์ ์์ฑ๋ค๋ common.scss์ ์์ฑํด์ ์ ์ญ์ผ๋ก ๊ด๋ฆฌํ ์ ์๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,87 @@
+@import '../../../style/variables.scss';
+@import '../../../style/reset.scss';
+
+
+body {
+ font-size: 14px;
+}
+input {
+ outline: none;
+}
+
+.login {
+ width: 100%;
+ height: 100vh;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ flex-direction: column;
+ .box {
+ width: 400px;
+ padding: 40px 0 20px;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ flex-direction: column;
+ border: 1px solid rgb(219, 219, 219);
+
+ h1 {
+ font-size:40px;
+ font-family: 'Lobster';
+ }
+
+ .inputTxt {
+ width: 300px;
+
+ input {
+ display: block;
+ width: 100%;
+ margin: 5px 0;
+ padding: 10px 0 10px 5px;
+ background-color: $grey-color-bg;
+ border-radius: 3px;
+ border: 1px solid $grey-color-border;
+
+ &::placeholder{
+ font-size: 12px;
+ }
+ }
+ }
+ }
+}
+
+@mixin button-style {
+ margin-top: 20px;
+ display: inline-block;
+ width: 100%;
+ text-align: center;
+ text-decoration-line: none;
+ padding: 8px 0;
+ border-radius: 8px;
+ border: 0px;
+ font-weight: bold;
+ color: #fff;
+ cursor: pointer;
+ }
+
+.abled-button {
+ @include button-style;
+ background-color: $point-color-blue;
+}
+
+.disabled-button {
+ @include button-style;
+ background-color: #666;
+}
+
+
+.findPw {
+ text-align: center;
+ margin: 60px 0;
+
+ a {
+ font-size: 9px;
+ color: rgb(0, 55, 107);
+ text-decoration: none;
+ }
+}
\ No newline at end of file | Unknown | variables.scssํ์ผ์ ์์ผ๋ฉด ๋ ๋ณ์๋ค์ด๋ค์! |
@@ -0,0 +1,163 @@
+import React, { useState, useEffect } from "react";
+import MainFeeds from "./MainFeeds";
+import "./Main.scss";
+import compass from "../assets/icon/compass.png";
+import heart from "../assets/icon/heart.png";
+import instagram from "../assets/icon/instagram.png";
+import search from "../assets/icon/search.png";
+import user from "../assets/icon/user.png";
+import userimg from "../assets/images/test.jpg";
+import feedimg from "../assets/images/test.jpg";
+
+const INFO_LIST = [
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+];
+
+const Main = () => {
+ return (
+ <>
+ <nav className="nav">
+ <div className="navLeft">
+ <img className="navIcon iconInstagram" alt="." src={instagram} />
+ <span className="logo">Westagram</span>
+ </div>
+ <div className="searchBar">
+ <img className="iconSearch" alt="." src={search} />
+ <input
+ className="searchBox"
+ type="text"
+ aria-label="๊ฒ์์ฐฝ"
+ placeholder="๊ฒ์"
+ />
+ </div>
+ <div className="navRight">
+ <img className="navIcon" alt="." src={compass} />
+ <img className="navIcon" alt="." src={heart} />
+ <img className="navIcon" alt="." src={user} />
+ </div>
+ </nav>
+ <div className="main">
+ <MainFeeds />
+ <div className="mainRight">
+ <div className="mainRightTop">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="stories">
+ <div className="storiesTop">
+ <p className="story">์คํ ๋ฆฌ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ </div>
+ <div className="recommend">
+ <div className="storiesTop">
+ <p className="story">ํ์๋์ ์ํ ์ถ์ฒ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ </div>
+ <div>
+ <ul>
+ {INFO_LIST.map((info) => {
+ return (
+ <li key={info.id}>
+ <a href="{info.link}">{info.text}</a>
+ </li>
+ );
+ })}
+ </ul>
+ <p className="copyright">โ 2023 WESTAGRAM</p>
+ </div>
+ </div>
+ </div>
+ </>
+ );
+};
+
+export default Main; | JavaScript | index.js์์ ์ ์ญ์ผ๋ก importํ๋ค๋ฉด ์ด๋ ๊ฒ ํ์ผ๋ง๋ค ๋งค๋ฒ import ํ์ง์์๋ ๋ฉ๋๋ค! |
@@ -0,0 +1,163 @@
+import React, { useState, useEffect } from "react";
+import MainFeeds from "./MainFeeds";
+import "./Main.scss";
+import compass from "../assets/icon/compass.png";
+import heart from "../assets/icon/heart.png";
+import instagram from "../assets/icon/instagram.png";
+import search from "../assets/icon/search.png";
+import user from "../assets/icon/user.png";
+import userimg from "../assets/images/test.jpg";
+import feedimg from "../assets/images/test.jpg";
+
+const INFO_LIST = [
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+];
+
+const Main = () => {
+ return (
+ <>
+ <nav className="nav">
+ <div className="navLeft">
+ <img className="navIcon iconInstagram" alt="." src={instagram} />
+ <span className="logo">Westagram</span>
+ </div>
+ <div className="searchBar">
+ <img className="iconSearch" alt="." src={search} />
+ <input
+ className="searchBox"
+ type="text"
+ aria-label="๊ฒ์์ฐฝ"
+ placeholder="๊ฒ์"
+ />
+ </div>
+ <div className="navRight">
+ <img className="navIcon" alt="." src={compass} />
+ <img className="navIcon" alt="." src={heart} />
+ <img className="navIcon" alt="." src={user} />
+ </div>
+ </nav>
+ <div className="main">
+ <MainFeeds />
+ <div className="mainRight">
+ <div className="mainRightTop">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="stories">
+ <div className="storiesTop">
+ <p className="story">์คํ ๋ฆฌ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ </div>
+ <div className="recommend">
+ <div className="storiesTop">
+ <p className="story">ํ์๋์ ์ํ ์ถ์ฒ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ </div>
+ <div>
+ <ul>
+ {INFO_LIST.map((info) => {
+ return (
+ <li key={info.id}>
+ <a href="{info.link}">{info.text}</a>
+ </li>
+ );
+ })}
+ </ul>
+ <p className="copyright">โ 2023 WESTAGRAM</p>
+ </div>
+ </div>
+ </div>
+ </>
+ );
+};
+
+export default Main; | JavaScript | `comment`๋ผ๋ ๋ณ์๋ช
์ด ์์ฑ๋์ด์๋ comment๋ค์ ๋ชจ์์ธ์ง, ๋ด๊ฐ input์ ์
๋ ฅํ ๊ฐ์ ๊ด๋ฆฌํ๋ ๋ณ์์ธ์ง ์ด๋ฆ๋ง ๋ดค์ ๋ ๋ฐ๋ก ์ถ์ธกํ๊ธฐ ์ด๋ ต์ต๋๋ค.
์๋์ commentArray๋ผ๋ ๋ณ์๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์กฐ๊ธ ๋ ๋ณ์๊ฐ ์ด๋ค state๋ฅผ ๊ฐ์ง๊ณ ์๋์ง ๋ช
ํํ ์ด๋ฆ์ผ๋ก ์์ฑํด์ฃผ์๋ฉด ์ข๊ฒ ์ต๋๋ค |
@@ -0,0 +1,163 @@
+import React, { useState, useEffect } from "react";
+import MainFeeds from "./MainFeeds";
+import "./Main.scss";
+import compass from "../assets/icon/compass.png";
+import heart from "../assets/icon/heart.png";
+import instagram from "../assets/icon/instagram.png";
+import search from "../assets/icon/search.png";
+import user from "../assets/icon/user.png";
+import userimg from "../assets/images/test.jpg";
+import feedimg from "../assets/images/test.jpg";
+
+const INFO_LIST = [
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+];
+
+const Main = () => {
+ return (
+ <>
+ <nav className="nav">
+ <div className="navLeft">
+ <img className="navIcon iconInstagram" alt="." src={instagram} />
+ <span className="logo">Westagram</span>
+ </div>
+ <div className="searchBar">
+ <img className="iconSearch" alt="." src={search} />
+ <input
+ className="searchBox"
+ type="text"
+ aria-label="๊ฒ์์ฐฝ"
+ placeholder="๊ฒ์"
+ />
+ </div>
+ <div className="navRight">
+ <img className="navIcon" alt="." src={compass} />
+ <img className="navIcon" alt="." src={heart} />
+ <img className="navIcon" alt="." src={user} />
+ </div>
+ </nav>
+ <div className="main">
+ <MainFeeds />
+ <div className="mainRight">
+ <div className="mainRightTop">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="stories">
+ <div className="storiesTop">
+ <p className="story">์คํ ๋ฆฌ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ </div>
+ <div className="recommend">
+ <div className="storiesTop">
+ <p className="story">ํ์๋์ ์ํ ์ถ์ฒ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ </div>
+ <div>
+ <ul>
+ {INFO_LIST.map((info) => {
+ return (
+ <li key={info.id}>
+ <a href="{info.link}">{info.text}</a>
+ </li>
+ );
+ })}
+ </ul>
+ <p className="copyright">โ 2023 WESTAGRAM</p>
+ </div>
+ </div>
+ </div>
+ </>
+ );
+};
+
+export default Main; | JavaScript | ```suggestion
setCommentArray(commentList => [ ...commentList,comment]);
```
์ด ๋๊ฐ์ ์ฐจ์ด์ ์ ํ์คํ ์๊ณ ๋์ด๊ฐ์ฃผ์
จ์ผ๋ฉด ์ข๊ฒ ์ต๋๋ค!! |
@@ -0,0 +1,163 @@
+import React, { useState, useEffect } from "react";
+import MainFeeds from "./MainFeeds";
+import "./Main.scss";
+import compass from "../assets/icon/compass.png";
+import heart from "../assets/icon/heart.png";
+import instagram from "../assets/icon/instagram.png";
+import search from "../assets/icon/search.png";
+import user from "../assets/icon/user.png";
+import userimg from "../assets/images/test.jpg";
+import feedimg from "../assets/images/test.jpg";
+
+const INFO_LIST = [
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+];
+
+const Main = () => {
+ return (
+ <>
+ <nav className="nav">
+ <div className="navLeft">
+ <img className="navIcon iconInstagram" alt="." src={instagram} />
+ <span className="logo">Westagram</span>
+ </div>
+ <div className="searchBar">
+ <img className="iconSearch" alt="." src={search} />
+ <input
+ className="searchBox"
+ type="text"
+ aria-label="๊ฒ์์ฐฝ"
+ placeholder="๊ฒ์"
+ />
+ </div>
+ <div className="navRight">
+ <img className="navIcon" alt="." src={compass} />
+ <img className="navIcon" alt="." src={heart} />
+ <img className="navIcon" alt="." src={user} />
+ </div>
+ </nav>
+ <div className="main">
+ <MainFeeds />
+ <div className="mainRight">
+ <div className="mainRightTop">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="stories">
+ <div className="storiesTop">
+ <p className="story">์คํ ๋ฆฌ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ </div>
+ <div className="recommend">
+ <div className="storiesTop">
+ <p className="story">ํ์๋์ ์ํ ์ถ์ฒ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ </div>
+ <div>
+ <ul>
+ {INFO_LIST.map((info) => {
+ return (
+ <li key={info.id}>
+ <a href="{info.link}">{info.text}</a>
+ </li>
+ );
+ })}
+ </ul>
+ <p className="copyright">โ 2023 WESTAGRAM</p>
+ </div>
+ </div>
+ </div>
+ </>
+ );
+};
+
+export default Main; | JavaScript | Input์ ๊ฐ์ด ๋น์ด์์๋ ํจ์๊ฐ ๊ทธ๋ฅ ์๋ฌด๋ฐ ๋์์ ํ์ง ์๋๋ค๋ฉด ๊ธฐ๋ฅ์ ์๋ฌ์ธ์ง, ์๋๋ ๋์์ธ์ง ํ์
ํ๊ธฐ ์ด๋ ต์ต๋๋ค.
`comment` ๊ฐ์ด ๋น์ด์๋ค๋ฉด ํจ์๊ฐ ํธ์ถ๋ ๋ `alert`๋ฅผ ํ์ํด์ฃผ๋ ๋ฑ์ ์ถ๊ฐ์ ์ธ ์กฐ์น๊ฐ ์์ผ๋ฉด ์ข์ต๋๋ค |
@@ -0,0 +1,163 @@
+import React, { useState, useEffect } from "react";
+import MainFeeds from "./MainFeeds";
+import "./Main.scss";
+import compass from "../assets/icon/compass.png";
+import heart from "../assets/icon/heart.png";
+import instagram from "../assets/icon/instagram.png";
+import search from "../assets/icon/search.png";
+import user from "../assets/icon/user.png";
+import userimg from "../assets/images/test.jpg";
+import feedimg from "../assets/images/test.jpg";
+
+const INFO_LIST = [
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+];
+
+const Main = () => {
+ return (
+ <>
+ <nav className="nav">
+ <div className="navLeft">
+ <img className="navIcon iconInstagram" alt="." src={instagram} />
+ <span className="logo">Westagram</span>
+ </div>
+ <div className="searchBar">
+ <img className="iconSearch" alt="." src={search} />
+ <input
+ className="searchBox"
+ type="text"
+ aria-label="๊ฒ์์ฐฝ"
+ placeholder="๊ฒ์"
+ />
+ </div>
+ <div className="navRight">
+ <img className="navIcon" alt="." src={compass} />
+ <img className="navIcon" alt="." src={heart} />
+ <img className="navIcon" alt="." src={user} />
+ </div>
+ </nav>
+ <div className="main">
+ <MainFeeds />
+ <div className="mainRight">
+ <div className="mainRightTop">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="stories">
+ <div className="storiesTop">
+ <p className="story">์คํ ๋ฆฌ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ </div>
+ <div className="recommend">
+ <div className="storiesTop">
+ <p className="story">ํ์๋์ ์ํ ์ถ์ฒ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ </div>
+ <div>
+ <ul>
+ {INFO_LIST.map((info) => {
+ return (
+ <li key={info.id}>
+ <a href="{info.link}">{info.text}</a>
+ </li>
+ );
+ })}
+ </ul>
+ <p className="copyright">โ 2023 WESTAGRAM</p>
+ </div>
+ </div>
+ </div>
+ </>
+ );
+};
+
+export default Main; | JavaScript | map ๋ฉ์๋์ ์ฝ๋ฐฑํจ์์ ๋๋ฒ ์งธ ์ธ์, ์ฆ ์งํฌ๋์ ์ฝ๋์์ user๋ผ๊ณ ํ๋ ๋ณ์๋ ๊ธฐ์ค๋ฐฐ์ด(commnetArray)์ value(๋ฐฐ์ด์ ๊ฐ๊ฐ์ ์์)๋ง๋ค์ index(๋ฐฐ์ด๋ด์ ์์)๋ฅผ ๋ํ๋
๋๋ค. ๋ฆฌ์กํธ์์ ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํด์ key ์์ฑ์ ๊ผญ ๋ถ์ฌํ๋ผ๊ณ ํ๋ ์ด์ ๋ ํด๋น์์๋ฅผ ๋ฆฌ์กํธ๊ฐ ๋ช
ํ์ด ์ธ์ํ๊ธฐ ์ํจ์ธ๋ฐ, ๋ฐฐ์ด์ index๋ฅผ key๊ฐ์ผ๋ก ๋ถ์ฌํ๋๊ฒ์ ์กฐ๊ธ ์ง์ํ๋ ๋ฐฉ๋ฒ์
๋๋ค. ์ด๋ค ๊ฐ์ผ๋ก key ์์ฑ์ ๋ถ์ฌํด์ผํ ์ง ์กฐ๊ธ ๋ ๊ณ ๋ฏผํด๋ณด์ธ์! |
@@ -0,0 +1,163 @@
+import React, { useState, useEffect } from "react";
+import MainFeeds from "./MainFeeds";
+import "./Main.scss";
+import compass from "../assets/icon/compass.png";
+import heart from "../assets/icon/heart.png";
+import instagram from "../assets/icon/instagram.png";
+import search from "../assets/icon/search.png";
+import user from "../assets/icon/user.png";
+import userimg from "../assets/images/test.jpg";
+import feedimg from "../assets/images/test.jpg";
+
+const INFO_LIST = [
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+];
+
+const Main = () => {
+ return (
+ <>
+ <nav className="nav">
+ <div className="navLeft">
+ <img className="navIcon iconInstagram" alt="." src={instagram} />
+ <span className="logo">Westagram</span>
+ </div>
+ <div className="searchBar">
+ <img className="iconSearch" alt="." src={search} />
+ <input
+ className="searchBox"
+ type="text"
+ aria-label="๊ฒ์์ฐฝ"
+ placeholder="๊ฒ์"
+ />
+ </div>
+ <div className="navRight">
+ <img className="navIcon" alt="." src={compass} />
+ <img className="navIcon" alt="." src={heart} />
+ <img className="navIcon" alt="." src={user} />
+ </div>
+ </nav>
+ <div className="main">
+ <MainFeeds />
+ <div className="mainRight">
+ <div className="mainRightTop">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="stories">
+ <div className="storiesTop">
+ <p className="story">์คํ ๋ฆฌ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ </div>
+ <div className="recommend">
+ <div className="storiesTop">
+ <p className="story">ํ์๋์ ์ํ ์ถ์ฒ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ </div>
+ <div>
+ <ul>
+ {INFO_LIST.map((info) => {
+ return (
+ <li key={info.id}>
+ <a href="{info.link}">{info.text}</a>
+ </li>
+ );
+ })}
+ </ul>
+ <p className="copyright">โ 2023 WESTAGRAM</p>
+ </div>
+ </div>
+ </div>
+ </>
+ );
+};
+
+export default Main; | JavaScript | ๊ตณ์ด ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ง์๋๋ผ๋ `<textarea></textarea>`ํ๊ทธ๋ฅผ ์ฌ์ฉํ๋ฉด multiline input์ ํ์ฉํ ์ ์์ต๋๋ค. |
@@ -0,0 +1,264 @@
+@import '../../../style/reset.scss';
+@import '../../../style/variables.scss';
+
+body {
+ font-size: 14px;
+}
+.logo {
+ font-family: "Lobster";
+ src: url(../assets/Lobster-Regular.ttf);
+}
+input {
+ outline: none;
+}
+
+.navIcon {
+ width: 24px;
+ margin-left: 20px;
+}
+
+.navLeft{
+ .iconInstagram {
+ margin-left: 0;
+ }
+}
+.nav {
+ display: flex;
+ width: 860px;
+ margin: 0 auto;
+ justify-content: space-between;
+ align-items: center;
+ padding: 40px 0;
+
+ .logo {
+ font-size: 24px;
+ margin-left: 20px;
+ border-left: 1px solid black;
+ padding-left: 20px;
+ }
+}
+.navLeft, .searchBar, .navRight{
+ display: flex;
+ align-items: center;
+}
+
+.searchBar {
+ width: 300px;
+ background-color: $grey-color-bg;
+ border: 1px solid $grey-color-border;
+ border-radius: 5px;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+
+ .searchBox {
+ width: 50px;
+ padding : 8px 0;
+ border: none;
+ background-color: $grey-color-bg;
+ text-align: center;
+ margin-left: 10px;
+ overflow: auto;
+ outline: none;
+ &:focus {
+ outline: none;
+ }
+ }
+
+ .iconSearch {
+ width: 16px;
+ }
+}
+
+.bold {font-weight: bold;}
+.main {
+ width: 100%;
+ height: 100vh;
+ display: flex;
+ justify-content: center;
+ padding: 80px 40px;
+ background-color: $grey-color-bg;
+ border-top: 2px solid black;
+
+ .username {
+ font-weight: bold;
+ }
+
+ .feeds {
+ border: 1px solid $grey-color-border;
+ background-color: white;
+
+ .feedImg img {
+ width: 468px;
+ }
+ }
+ .feedsTop, .mainRightTop {
+ display: flex;
+ align-items: stretch;
+ padding: 15px;
+ }
+
+ .name {
+ display: flex;
+ margin-left: 15px;
+ justify-content: center;
+ flex-direction: column;
+ }
+ .location, .username2 {
+ font-size: 12px;
+ color: gray;
+ }
+
+ .photo img{
+ width: 50px;
+ border-radius: 100%;
+ }
+
+ .stories{
+ .photo {
+ background: radial-gradient(circle at bottom left, #F58529 20%, #C42D91);
+ border-radius: 100%;
+ height: 57px;
+ width: 57px;
+
+ img {
+ box-sizing: content-box;
+ border: 2px solid #fff;
+ margin: 1px;
+ }
+ }
+ }
+
+ .feedsBottom {
+ padding: 10px;
+
+ .photo img {
+ width: 24px;
+ margin-right: 10px;
+ padding: 1px;
+ border:1px solid rgb(188, 188, 188);
+ }
+
+ .like {
+ margin-top:10px;
+ display: inline-flex;
+ }
+
+ .feedsBottomIcons {
+ display: flex;
+ justify-content: space-between;
+
+ .bottomRight > .navIcon {
+ margin-left:0;
+ }
+
+ .bottomLeft .navIcon:first-child {
+ margin-left: 0;
+ }
+ }
+
+ .comment {
+ margin-top: 10px;
+
+ .writeTime {
+ color: grey;
+ margin-top: 10px;
+ }
+ .commentContainer{
+ position: relative;
+ }
+ .commentButton {
+ position: absolute;
+ right: 0;
+ top: 20px;
+ border: none;
+ background-color: inherit;
+ color: grey;
+ }
+ }
+
+ .description {
+ margin-top: 20px;
+ }
+
+ .commentBox {
+ width: 100%;
+ resize: none;
+ margin-top: 20px;
+ border-style: none;
+ border-bottom: 1px solid grey;
+
+ &:focus {
+ outline: none;
+ }
+ }
+ }
+
+ .mainRight {
+ width: 300px;
+ padding: 0 20px;
+
+ .stories, .recommend {
+ border: 1px solid $grey-color-border;
+ background-color: white;
+ padding: 15px;
+ height: 250px;
+ overflow: hidden;
+ }
+
+ .storiesTop {
+ display: flex;
+ justify-content: space-between;
+ font-size: 12px;
+ }
+
+ .recommend {
+ margin-top: 20px;
+ }
+
+ .story {
+ color: grey;
+ }
+
+ .storyPeople {
+ display: flex;
+ position: relative;
+ margin-top: 15px;
+
+ button {
+ position: absolute;
+ right: 0;
+ margin-top: 15px;
+ border: none;
+ background-color: inherit;
+ color: $point-color-blue;
+ font-weight: bold;
+
+ }
+ }
+ }
+
+ ul {
+ padding: 0;
+
+ li {
+ display: inline-block;
+ list-style-type: none;
+ color: $grey-color-193;
+ margin:5px 5px 0;
+ }
+ li:first-child {
+ margin-left: 0;
+ }
+
+ a {
+ text-decoration: none;
+ color: $grey-color-193;
+ }
+ }
+
+ .copyright {
+ color: $grey-color-193;
+ margin-top: 10px;
+ }
+}
\ No newline at end of file | Unknown | ์์ ๋จ๊ฒจ๋๋ฆฐ ๋ด์ฉ์ ๋ฆฌ๋ทฐ์ ๋์ผํฉ๋๋ค |
@@ -0,0 +1,42 @@
+package racingcar.domain;
+
+import racingcar.exception.CarNameLengthException;
+
+public class Car {
+
+ private static final int CAR_NAME_LENGTH = 5;
+ private static final int MOVABLE_MIN_NUMBER = 4;
+ private final String name;
+ private int position = 0;
+
+ public Car(String name) {
+ validate(name);
+ this.name = name;
+ }
+
+ private void validate(String name) {
+ validateLength(name);
+ }
+
+ private void validateLength(String name) {
+ if (name.length() > CAR_NAME_LENGTH) {
+ throw new CarNameLengthException();
+ }
+ }
+
+ public void move(CarMoveNumberGenerator carMoveNumberGenerator) {
+ final int number = carMoveNumberGenerator.generate();
+
+ if (number >= MOVABLE_MIN_NUMBER) {
+ position++;
+ }
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPosition() {
+ return position;
+ }
+} | Java | ์ปจ๋ฒค์
์ ๋ฐ๋ผ์ ์์์ ์ธ์คํด์ค ๋ณ์๋ฅผ ์ ์ค๋ก ๊ตฌ๋ถํด์ฃผ์๋ ๊ฒ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค!! |
@@ -0,0 +1,71 @@
+package racingcar.domain;
+
+import racingcar.exception.CarsDuplicatedNameException;
+import racingcar.exception.CarsMaxScoreBlankException;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+public class Cars {
+
+ private static final String DELIMITER = ",";
+ private static final String DUPLICATED_DELIMITER_REGEX = ",+";
+ private final List<Car> cars;
+
+ private Cars(List<Car> cars) {
+ this.cars = Collections.unmodifiableList(cars);
+ }
+
+ public static Cars createCarNameByWord(String input) {
+ List<Car> cars = new ArrayList<>();
+ String[] words = divideWord(input);
+ validate(words);
+
+ for (String carName : words) {
+ cars.add(new Car(carName));
+ }
+ return new Cars(cars);
+ }
+
+ private static void validate(String[] words) {
+ validateDuplicatedWord(words);
+ }
+
+ private static void validateDuplicatedWord(String[] words) {
+ Set<String> uniqueWord = new HashSet<>();
+ for (String word : words) {
+ if (!uniqueWord.add(word)) {
+ throw new CarsDuplicatedNameException();
+ }
+ }
+ }
+
+ private static String[] divideWord(String word) {
+ return word.replaceAll(DUPLICATED_DELIMITER_REGEX, DELIMITER).split(DELIMITER);
+ }
+
+ public void move(CarMoveNumberGenerator carMoveNumberGenerator) {
+ for (Car car : cars) {
+ car.move(carMoveNumberGenerator);
+ }
+ }
+
+ public Winner findWinner() {
+ int maxScore = findMaxScore();
+ return new Winner(cars.stream()
+ .filter(car -> (car.getPosition() == maxScore))
+ .map(Car::getName)
+ .collect(Collectors.toList()));
+ }
+
+ private int findMaxScore() {
+ return cars.stream()
+ .max(Comparator.comparingInt(Car::getPosition))
+ .map(Car::getPosition)
+ .orElseThrow(CarsMaxScoreBlankException::new);
+ }
+
+ public List<Car> getCars() {
+ return cars;
+ }
+} | Java | ๋ถ๋ณ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ๋ ์์ฑ์ ๋จ๊ณ์์๋ ๋ฐฉ์ด์ ๋ณต์ฌ๋ฅผ ๊ณ ๋ คํด์ฃผ์๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์์!!
- [๋ฐฉ์ด์ ๋ณต์ฌ์ Unmodifiable Collections](https://tecoble.techcourse.co.kr/post/2021-04-26-defensive-copy-vs-unmodifiable/) |
@@ -0,0 +1,16 @@
+package racingcar.domain;
+
+import java.util.List;
+
+public class Winner {
+
+ private final List<String> winner;
+
+ public Winner(List<String> winner) {
+ this.winner = winner;
+ }
+
+ public List<String> getWinner() {
+ return winner;
+ }
+} | Java | ๋ฆฌ์คํธ์ ๋ค์ด๋ฐ์ ๋ณต์ํ์ด ๋ ์ ์ ํ๋ค๊ณ ์๊ฐํฉ๋๋ค!
`Winner` ํด๋์ค ์์ฒด๋ ์น๋ฆฌ์๋ค์ ์ด๋ฆ์ ๋ด๊ณ ์๋ ์ผ๊ธ ์ปฌ๋ ์
์ด๊ธฐ ๋๋ฌธ์ `Winners` ๋ผ๋ ๋ค์ด๋ฐ์ ๋ ์ถ์ฒ๋๋ฆฝ๋๋ค! |
@@ -0,0 +1,54 @@
+package racingcar.view;
+
+import racingcar.domain.Car;
+import racingcar.domain.Cars;
+import racingcar.domain.Winner;
+
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public class OutputView {
+
+ private static final String MESSAGE_INPUT_CAR_NAME = "๊ฒฝ์ฃผํ ์๋์ฐจ ์ด๋ฆ์ ์
๋ ฅํ์ธ์.(์ด๋ฆ์ ์ผํ(,) ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถ)";
+ private static final String MESSAGE_INPUT_TRY = "์๋ํ ํ์๋ ๋ชํ์ธ๊ฐ์?";
+ private static final String MESSAGE_RESULT = "์คํ ๊ฒฐ๊ณผ";
+ private static final String MOVE_MARK = "-";
+ private static final String JOIN_NAME_AND_POSITION = " : ";
+ private static final String WINNER_DELIMITER = ",";
+ private static final String MESSAGE_WINNER_PREFIX = "์ต์ข
์ฐ์น์ : ";
+ private static final String MESSAGE_WINNER_SUFFIX = "";
+
+ public void printInputCarName() {
+ System.out.println(MESSAGE_INPUT_CAR_NAME);
+ }
+
+ public void printExceptionMessage(IllegalArgumentException exceptionMessage) {
+ System.out.println(exceptionMessage.getMessage());
+ }
+
+ public void printInputTry() {
+ System.out.println(MESSAGE_INPUT_TRY);
+ }
+
+ public void printBlank() {
+ System.out.println();
+ }
+
+ public void printResult(Cars cars) {
+ System.out.println(MESSAGE_RESULT);
+ for(Car car : cars.getCars()){
+ System.out.println(car.getName() + JOIN_NAME_AND_POSITION + convertMoveMark(car.getPosition()));
+ }
+ printBlank();
+ }
+
+ private String convertMoveMark(int position) {
+ return Stream.generate(() -> MOVE_MARK).limit(position).collect(Collectors.joining());
+ }
+
+ public void printWinner(Winner winner) {
+ String message = winner.getWinner().stream()
+ .collect(Collectors.joining(WINNER_DELIMITER, MESSAGE_WINNER_PREFIX, MESSAGE_WINNER_SUFFIX));
+ System.out.println(message);
+ }
+} | Java | String์์ ์ ๊ณตํ๋ `repeat()` ๋ฉ์๋๋ฅผ ํ์ฉํด๋ณด์๋ ๊ฒ๋ ์ถ์ฒ๋๋ฆฝ๋๋ค! |
@@ -0,0 +1,71 @@
+package racingcar.domain;
+
+import racingcar.exception.CarsDuplicatedNameException;
+import racingcar.exception.CarsMaxScoreBlankException;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+public class Cars {
+
+ private static final String DELIMITER = ",";
+ private static final String DUPLICATED_DELIMITER_REGEX = ",+";
+ private final List<Car> cars;
+
+ private Cars(List<Car> cars) {
+ this.cars = Collections.unmodifiableList(cars);
+ }
+
+ public static Cars createCarNameByWord(String input) {
+ List<Car> cars = new ArrayList<>();
+ String[] words = divideWord(input);
+ validate(words);
+
+ for (String carName : words) {
+ cars.add(new Car(carName));
+ }
+ return new Cars(cars);
+ }
+
+ private static void validate(String[] words) {
+ validateDuplicatedWord(words);
+ }
+
+ private static void validateDuplicatedWord(String[] words) {
+ Set<String> uniqueWord = new HashSet<>();
+ for (String word : words) {
+ if (!uniqueWord.add(word)) {
+ throw new CarsDuplicatedNameException();
+ }
+ }
+ }
+
+ private static String[] divideWord(String word) {
+ return word.replaceAll(DUPLICATED_DELIMITER_REGEX, DELIMITER).split(DELIMITER);
+ }
+
+ public void move(CarMoveNumberGenerator carMoveNumberGenerator) {
+ for (Car car : cars) {
+ car.move(carMoveNumberGenerator);
+ }
+ }
+
+ public Winner findWinner() {
+ int maxScore = findMaxScore();
+ return new Winner(cars.stream()
+ .filter(car -> (car.getPosition() == maxScore))
+ .map(Car::getName)
+ .collect(Collectors.toList()));
+ }
+
+ private int findMaxScore() {
+ return cars.stream()
+ .max(Comparator.comparingInt(Car::getPosition))
+ .map(Car::getPosition)
+ .orElseThrow(CarsMaxScoreBlankException::new);
+ }
+
+ public List<Car> getCars() {
+ return cars;
+ }
+} | Java | `CarMoveNumberGenerator` ๋ฅผ `Car` ๊ฐ์ฒด๊น์ง ๋๊ฒจ์ฃผ์ง ์์๋ ๋ ๊ฒ ๊ฐ์ต๋๋ค!
๊ทธ๋ฌ๋ฉด `car.move()` ๋ฉ์๋๋ฅผ ํ
์คํธํ๊ธฐ๋ ํจ์ฌ ๊ฐํธํด์ง ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,22 @@
+package racingcar.domain;
+
+public class RacingCarGame {
+
+ private final Cars cars;
+
+ public RacingCarGame(Cars cars) {
+ this.cars = cars;
+ }
+
+ public void move() {
+ cars.move(new CarRandomMoveNumberGenerator());
+ }
+
+ public Winner findWinner() {
+ return cars.findWinner();
+ }
+
+ public Cars getCars() {
+ return cars;
+ }
+} | Java | `RacingCarGame` ํด๋์ค์ ๋ฉ์๋๋ค์ด ๋๋ถ๋ถ `Cars` ํด๋์ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ์ฉ๋๋ก๋ง ์ฌ์ฉ๋๋ ๊ฒ ๊ฐ์์!
`RacingCarGame` ํด๋์ค๊ฐ ์ ๋ง ํ์ํ์ง, ์๋๋ฉด ์ด๋ค ์์ผ๋ก `RacingCarGame` ๋ง์ ์๋ก์ด ์ฑ
์์ ๋ถ์ฌํ ์ ์์ ์ง
์๊ฐํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์์!! |
@@ -0,0 +1,47 @@
+package racingcar.domain;
+
+import racingcar.exception.TryCommandNumberException;
+import racingcar.exception.TryCommandRangeException;
+
+public class TryCommand {
+
+ private static final int MIN_TRY = 1;
+ private static final int MAX_TRY = 100000;
+ private int tryCount;
+
+ private TryCommand(int tryCount) {
+ this.tryCount = tryCount;
+ }
+
+ public static TryCommand createTryCommandByString(String input) {
+ int number = convertInt(input);
+ validate(number);
+ return new TryCommand(number);
+ }
+
+ private static void validate(int number) {
+ validateRange(number);
+ }
+
+ private static void validateRange(int number) {
+ if(number < MIN_TRY || number > MAX_TRY) {
+ throw new TryCommandRangeException(MIN_TRY, MAX_TRY);
+ }
+ }
+
+ private static int convertInt(String input) {
+ try{
+ return Integer.parseInt(input);
+ }catch (NumberFormatException exception) {
+ throw new TryCommandNumberException();
+ }
+ }
+
+ public boolean tryMove() {
+ if(tryCount > 0) {
+ tryCount--;
+ return true;
+ }
+ return false;
+ }
+} | Java | ๊ฐ ๊ฐ์ฒด์ ๋ด๋ถ ์ํ๋ฅผ `final` ๋ก ์ ์ธํ๋ ๋ฐฉ๋ฒ๋ ์๋๋ผ๊ตฌ์! ์ฐธ๊ณ ํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค!
- [Value Object, Reference Obejct](http://aeternum.egloos.com/v/1111257) |
@@ -0,0 +1,71 @@
+package racingcar.domain;
+
+import racingcar.exception.CarsDuplicatedNameException;
+import racingcar.exception.CarsMaxScoreBlankException;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+public class Cars {
+
+ private static final String DELIMITER = ",";
+ private static final String DUPLICATED_DELIMITER_REGEX = ",+";
+ private final List<Car> cars;
+
+ private Cars(List<Car> cars) {
+ this.cars = Collections.unmodifiableList(cars);
+ }
+
+ public static Cars createCarNameByWord(String input) {
+ List<Car> cars = new ArrayList<>();
+ String[] words = divideWord(input);
+ validate(words);
+
+ for (String carName : words) {
+ cars.add(new Car(carName));
+ }
+ return new Cars(cars);
+ }
+
+ private static void validate(String[] words) {
+ validateDuplicatedWord(words);
+ }
+
+ private static void validateDuplicatedWord(String[] words) {
+ Set<String> uniqueWord = new HashSet<>();
+ for (String word : words) {
+ if (!uniqueWord.add(word)) {
+ throw new CarsDuplicatedNameException();
+ }
+ }
+ }
+
+ private static String[] divideWord(String word) {
+ return word.replaceAll(DUPLICATED_DELIMITER_REGEX, DELIMITER).split(DELIMITER);
+ }
+
+ public void move(CarMoveNumberGenerator carMoveNumberGenerator) {
+ for (Car car : cars) {
+ car.move(carMoveNumberGenerator);
+ }
+ }
+
+ public Winner findWinner() {
+ int maxScore = findMaxScore();
+ return new Winner(cars.stream()
+ .filter(car -> (car.getPosition() == maxScore))
+ .map(Car::getName)
+ .collect(Collectors.toList()));
+ }
+
+ private int findMaxScore() {
+ return cars.stream()
+ .max(Comparator.comparingInt(Car::getPosition))
+ .map(Car::getPosition)
+ .orElseThrow(CarsMaxScoreBlankException::new);
+ }
+
+ public List<Car> getCars() {
+ return cars;
+ }
+} | Java | ์คํธ๋ฆผ์ผ๋ก ๋ก์ง์ ๋ฐ๊พธ์๋ฉด ๋น ๋ฆฌ์คํธ๋ฅผ ์ ์ธํ์ง ์๊ณ ๋ ๊น๋ํ๊ฒ ๊ตฌํํ ์ ์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,10 @@
+package racingcar.exception;
+
+public class CarNameLengthException extends IllegalArgumentException{
+
+ private static final String EXCEPTION_MESSAGE_CAR_NAME_LENGTH = "[ERROR] ์๋์ฐจ์ด๋ฆ์ 5๊ธ์ ์ดํ์
๋๋ค";
+
+ public CarNameLengthException() {
+ super(EXCEPTION_MESSAGE_CAR_NAME_LENGTH);
+ }
+} | Java | ํน๋ณํ ์ปค์คํ
์์ธ๋ฅผ ์ฌ์ฉํ์ ์ด์ ๊ฐ ์์๊น์?? ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,42 @@
+package racingcar.domain;
+
+import racingcar.exception.CarNameLengthException;
+
+public class Car {
+
+ private static final int CAR_NAME_LENGTH = 5;
+ private static final int MOVABLE_MIN_NUMBER = 4;
+ private final String name;
+ private int position = 0;
+
+ public Car(String name) {
+ validate(name);
+ this.name = name;
+ }
+
+ private void validate(String name) {
+ validateLength(name);
+ }
+
+ private void validateLength(String name) {
+ if (name.length() > CAR_NAME_LENGTH) {
+ throw new CarNameLengthException();
+ }
+ }
+
+ public void move(CarMoveNumberGenerator carMoveNumberGenerator) {
+ final int number = carMoveNumberGenerator.generate();
+
+ if (number >= MOVABLE_MIN_NUMBER) {
+ position++;
+ }
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPosition() {
+ return position;
+ }
+} | Java | ๊ฐ์ฌํฉ๋๋ค! ๋์น๊ณ ์๋ ๋ถ๋ถ์ด๋ค์.. |
@@ -0,0 +1,71 @@
+package racingcar.domain;
+
+import racingcar.exception.CarsDuplicatedNameException;
+import racingcar.exception.CarsMaxScoreBlankException;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+public class Cars {
+
+ private static final String DELIMITER = ",";
+ private static final String DUPLICATED_DELIMITER_REGEX = ",+";
+ private final List<Car> cars;
+
+ private Cars(List<Car> cars) {
+ this.cars = Collections.unmodifiableList(cars);
+ }
+
+ public static Cars createCarNameByWord(String input) {
+ List<Car> cars = new ArrayList<>();
+ String[] words = divideWord(input);
+ validate(words);
+
+ for (String carName : words) {
+ cars.add(new Car(carName));
+ }
+ return new Cars(cars);
+ }
+
+ private static void validate(String[] words) {
+ validateDuplicatedWord(words);
+ }
+
+ private static void validateDuplicatedWord(String[] words) {
+ Set<String> uniqueWord = new HashSet<>();
+ for (String word : words) {
+ if (!uniqueWord.add(word)) {
+ throw new CarsDuplicatedNameException();
+ }
+ }
+ }
+
+ private static String[] divideWord(String word) {
+ return word.replaceAll(DUPLICATED_DELIMITER_REGEX, DELIMITER).split(DELIMITER);
+ }
+
+ public void move(CarMoveNumberGenerator carMoveNumberGenerator) {
+ for (Car car : cars) {
+ car.move(carMoveNumberGenerator);
+ }
+ }
+
+ public Winner findWinner() {
+ int maxScore = findMaxScore();
+ return new Winner(cars.stream()
+ .filter(car -> (car.getPosition() == maxScore))
+ .map(Car::getName)
+ .collect(Collectors.toList()));
+ }
+
+ private int findMaxScore() {
+ return cars.stream()
+ .max(Comparator.comparingInt(Car::getPosition))
+ .map(Car::getPosition)
+ .orElseThrow(CarsMaxScoreBlankException::new);
+ }
+
+ public List<Car> getCars() {
+ return cars;
+ }
+} | Java | ์ค... ์ข์๊ธ ๊ฐ์ฌํฉ๋๋ค. ๋ฐฉ์ด์ ๋ณต์ฌ๋ ์ฒ์ ์ ํด๋ณด๋ ๊ฐ๋
์ด๋ค์! |
@@ -0,0 +1,54 @@
+package racingcar.view;
+
+import racingcar.domain.Car;
+import racingcar.domain.Cars;
+import racingcar.domain.Winner;
+
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public class OutputView {
+
+ private static final String MESSAGE_INPUT_CAR_NAME = "๊ฒฝ์ฃผํ ์๋์ฐจ ์ด๋ฆ์ ์
๋ ฅํ์ธ์.(์ด๋ฆ์ ์ผํ(,) ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถ)";
+ private static final String MESSAGE_INPUT_TRY = "์๋ํ ํ์๋ ๋ชํ์ธ๊ฐ์?";
+ private static final String MESSAGE_RESULT = "์คํ ๊ฒฐ๊ณผ";
+ private static final String MOVE_MARK = "-";
+ private static final String JOIN_NAME_AND_POSITION = " : ";
+ private static final String WINNER_DELIMITER = ",";
+ private static final String MESSAGE_WINNER_PREFIX = "์ต์ข
์ฐ์น์ : ";
+ private static final String MESSAGE_WINNER_SUFFIX = "";
+
+ public void printInputCarName() {
+ System.out.println(MESSAGE_INPUT_CAR_NAME);
+ }
+
+ public void printExceptionMessage(IllegalArgumentException exceptionMessage) {
+ System.out.println(exceptionMessage.getMessage());
+ }
+
+ public void printInputTry() {
+ System.out.println(MESSAGE_INPUT_TRY);
+ }
+
+ public void printBlank() {
+ System.out.println();
+ }
+
+ public void printResult(Cars cars) {
+ System.out.println(MESSAGE_RESULT);
+ for(Car car : cars.getCars()){
+ System.out.println(car.getName() + JOIN_NAME_AND_POSITION + convertMoveMark(car.getPosition()));
+ }
+ printBlank();
+ }
+
+ private String convertMoveMark(int position) {
+ return Stream.generate(() -> MOVE_MARK).limit(position).collect(Collectors.joining());
+ }
+
+ public void printWinner(Winner winner) {
+ String message = winner.getWinner().stream()
+ .collect(Collectors.joining(WINNER_DELIMITER, MESSAGE_WINNER_PREFIX, MESSAGE_WINNER_SUFFIX));
+ System.out.println(message);
+ }
+} | Java | ์ด๋ฐ,, ์คํธ๋ฆผ์ ๋๋ฌด ๋จ๋ฐํ๋ค์.. `repeat()` ๋ผ๋ ์ข์ ํจ์๊ฐ ์๋๋ฐ ๋ง์ด์ฃ ใ
|
@@ -0,0 +1,22 @@
+package racingcar.domain;
+
+public class RacingCarGame {
+
+ private final Cars cars;
+
+ public RacingCarGame(Cars cars) {
+ this.cars = cars;
+ }
+
+ public void move() {
+ cars.move(new CarRandomMoveNumberGenerator());
+ }
+
+ public Winner findWinner() {
+ return cars.findWinner();
+ }
+
+ public Cars getCars() {
+ return cars;
+ }
+} | Java | ์๋ `RacingCarGame` ์์ ์๋ํ์๋ฅผ ๊ฐ์ง๊ณ ์์ด์ `Cars` ๋ฅผ ์๋ํ์๋งํผ ์์ง์ด๋ ๋ก์ง์ด์์๋๋ฐ controller ์ชฝ์ผ๋ก ํด๋น ๋ก์ง์ ์ฎ๊ธฐ๋๋ฐ๋์ ์ธ๋ชจ์๋ ํด๋์ค๊ฐ ๋์๋ค์.. ์ข์ ์ง์ ๊ฐ์ฌํฉ๋๋ค..! |
@@ -0,0 +1,47 @@
+package racingcar.domain;
+
+import racingcar.exception.TryCommandNumberException;
+import racingcar.exception.TryCommandRangeException;
+
+public class TryCommand {
+
+ private static final int MIN_TRY = 1;
+ private static final int MAX_TRY = 100000;
+ private int tryCount;
+
+ private TryCommand(int tryCount) {
+ this.tryCount = tryCount;
+ }
+
+ public static TryCommand createTryCommandByString(String input) {
+ int number = convertInt(input);
+ validate(number);
+ return new TryCommand(number);
+ }
+
+ private static void validate(int number) {
+ validateRange(number);
+ }
+
+ private static void validateRange(int number) {
+ if(number < MIN_TRY || number > MAX_TRY) {
+ throw new TryCommandRangeException(MIN_TRY, MAX_TRY);
+ }
+ }
+
+ private static int convertInt(String input) {
+ try{
+ return Integer.parseInt(input);
+ }catch (NumberFormatException exception) {
+ throw new TryCommandNumberException();
+ }
+ }
+
+ public boolean tryMove() {
+ if(tryCount > 0) {
+ tryCount--;
+ return true;
+ }
+ return false;
+ }
+} | Java | ๋ถ๋ณ์ผ๋ก ํด๋ ๊ฐ์ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ์ด ์์๊ตฐ์..
DDD ์์ ๋์ค๋ ๊ฐ๋
์ด๋ผ MVC ์๋ ์ด๋ป๊ฒ ์ ์ฉํ ์ง๋ ๊ณ ๋ฏผ์ ํด๋ด์ผ๊ฒ ๋ค์..
์ข์๊ธ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,10 @@
+package racingcar.exception;
+
+public class CarNameLengthException extends IllegalArgumentException{
+
+ private static final String EXCEPTION_MESSAGE_CAR_NAME_LENGTH = "[ERROR] ์๋์ฐจ์ด๋ฆ์ 5๊ธ์ ์ดํ์
๋๋ค";
+
+ public CarNameLengthException() {
+ super(EXCEPTION_MESSAGE_CAR_NAME_LENGTH);
+ }
+} | Java | [์ปค์คํ
์์ธ](https://tecoble.techcourse.co.kr/post/2020-08-17-custom-exception/)
์ปค์คํ
์์ธ๋ค์ ์ฅ๋จ์ ์ด ์์ต๋๋ค! ๊ทธ๋์ ์ ๋ต์ ์์ง๋ง ์ ๊ฐ ์ฌ์ฉํ ์ด์ ๋
์ฒซ์งธ๋กํด๋์ค ์ด๋ฆ์ผ๋ก ์ด๋ค ์์ธ์ธ์ง ๋ฐ๋ก ์ ์ ์๊ณ
๋์งธ๋ก ๋ณดํต ๋๋ฉ์ธ ์ฝ๋๋ฅผ ๋ณผ๋ ์ค์ํ๊ฒ์ ๊ทธ ๋๋ฉ์ธ ์ญํ ๊ณผ ๊ด๋ จ๋ ๋ก์ง์ด์ง, ์์ธ์ฒ๋ฆฌ๋ฅผ ์ด๋ป๊ฒ ํ๋์ง๊ฐ ์ค์ํ๊ฒ ์๋๊ธฐ๋๋ฌธ์ ์์ธ๋ฅผ ๋ฐ๋ก๋นผ๋ด์ด ์ข ๋ ๋ณด๊ธฐ ํธํ๊ฒ ๋ง๋ค๊ธฐ ์ํจ์
๋๋ค!
์
์งธ๋ก ๋๋ถ๋ถ Spring MVC ํ๋ก์ ํธ์์๋ ์ปค์คํ
์์ธ๋ฅผ ์ฌ์ฉํ๊ธฐ์ ์ฐ์ต์ฐจ์์์ ์ปค์คํ
์์ธ๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. |
@@ -0,0 +1,32 @@
+package baseball.util
+
+// Message
+const val PRINT_NOTHING_MESSAGE = "๋ซ์ฑ"
+const val PRINT_STRIKE_MESSAGE = "%d์คํธ๋ผ์ดํฌ"
+const val PRINT_BALL_MESSAGE = "%d๋ณผ"
+const val PRINT_STRIKE_BALL_MESSAGE = "%d๋ณผ %d์คํธ๋ผ์ดํฌ"
+const val START_GAME_MASSAGE = "์ซ์ ์ผ๊ตฌ ๊ฒ์์ ์์ํฉ๋๋ค."
+const val QUIT_GAME_MASSAGE = "3๊ฐ์ ์ซ์๋ฅผ ๋ชจ๋ ๋งํ์
จ์ต๋๋ค! ๊ฒ์ ์ข
๋ฃ"
+const val SELECT_COMMAND_MESSAGE = "๊ฒ์์ ์๋ก ์์ํ๋ ค๋ฉด 1, ์ข
๋ฃํ๋ ค๋ฉด 2๋ฅผ ์
๋ ฅํ์ธ์."
+const val USER_INPUT_NUMBER_MESSAGE = "์ซ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์: "
+// Error
+const val ERROR_INVALID_INPUT_MESSAGE = "์๋ชป๋ ๊ฐ์ ์
๋ ฅํ์ต๋๋ค"
+const val ERROR_INVALID_COMMAND_MESSAGE = "1๊ณผ 2๋ง ์
๋ ฅํด์ฃผ์ธ์."
+
+// Command
+const val RETRY_COMMAND = "1"
+const val QUIT_COMMAND = "2"
+
+// Code
+const val END_CODE = 2
+const val PLAYING_CODE = 1
+
+// Size, Number
+const val MAX_SIZE = 3
+const val MAX_NUMBER = 9
+const val MIN_NUMBER = 1
+
+// String
+const val EMPTY_STRING = ""
+
+ | Kotlin | util ํด๋๋ฅผ ์ฌ์ฉํด EMPTY_STRING๊ณผ ๊ฐ์ด ์ฌ์ํ ๊ฐ๋ค๊น์ง ์์ํ๋ฅผ ํ์ ๋๋ถ์ ๊ฐ๋
์ฑ์ด ์ข๋ค์. ๋ฐฐ์๊ฐ๋๋ค! |
@@ -0,0 +1,59 @@
+package baseball.game
+
+import baseball.game.service.Game
+import baseball.model.AnswerBoard
+import baseball.model.Computer
+import baseball.util.*
+import baseball.view.User
+import baseball.view.validator.InputValidator
+
+class Baseball(
+ private val user: User,
+ private val computer: Computer,
+ private val answerBoard: AnswerBoard,
+) : Game {
+ private var gameState = PLAYING_CODE
+ private var answer = computer.createAnswer()
+
+ override fun play() {
+ println(START_GAME_MASSAGE)
+ process()
+ }
+
+ // ๊ฒ์ ์งํ
+ override fun process() {
+ do {
+ val userNumber = user.createNumber()
+
+ answerBoard.createResult(answer, userNumber)
+ println(answerBoard.printResult())
+
+ finish()
+ } while (isPlaying())
+ }
+
+ private fun isPlaying(): Boolean = gameState != END_CODE
+
+ override fun quit() {
+ gameState = END_CODE
+ }
+
+ override fun retry() {
+ answer = computer.createAnswer()
+ answerBoard.clearState()
+ }
+
+ private fun printFinishMessage() = println(QUIT_GAME_MASSAGE + "\n" + SELECT_COMMAND_MESSAGE)
+
+ private fun finish() {
+ if (answerBoard.isThreeStrike()) {
+ printFinishMessage()
+ when (InputValidator.validateUserCommand(readLine()!!)) {
+ RETRY_COMMAND -> retry()
+ QUIT_COMMAND -> quit()
+ }
+ }
+ }
+
+}
+ | Kotlin | MVC ๋ชจ๋ธ์ ์ฌ์ฉํ์ ๊ฒ ๊ฐ์ต๋๋ค.
Baseball Class๋ Controller์ ์ญํ ์ ํ๋ ๊ฒ ๊ฐ์๋ฐ, Controller์์ ์ง์ ์
๋ ฅ์ ๋ฐ๋ ๊ฒ๋ณด๋ค๋
์ฌ์ฉ์์ ์ง์ ์ ์ผ๋ก ๋ง๋ฟ๋ View์์ ์
๋ ฅ์ ๋ฐ์์ Controller์์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,12 @@
+package baseball.view
+
+import baseball.util.USER_INPUT_NUMBER_MESSAGE
+import baseball.view.validator.InputValidator
+
+class User {
+ fun createNumber(): String {
+ print(USER_INPUT_NUMBER_MESSAGE)
+ val userNumber = readLine()!!
+ return InputValidator.validateUserNumber(userNumber)
+ }
+}
\ No newline at end of file | Kotlin | ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ์ ๋ฐ์์ ๊ฒ์ฌ๋ ๊ฐ์ ๋๋ ค๋ณด๋ด์ฃผ๋ ํจ์์ธ ๊ฒ ๊ฐ์ต๋๋ค.
create๋ผ๋ ํจ์๋ช
์ ๋ณด์์ ๋ ์ ๋ "์ฌ์ฉ์๊ฐ ๋ฒํธ๋ฅผ ๋ง๋๋? ์ฌ์ฉ์๊ฐ ๊ธฐ๊ณ์ ์ญํ ์ ํ๋ ๊ฒ์ธ๊ฐ?"๋ผ๋ ์๊ฐ์ด ๋ค์์ต๋๋ค.
input๊ณผ ์ด์ธ๋ฆฌ๋ ํจ์๋ช
์ด๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
๋ํ, ์ ๋ ์ด ๋ถ๋ถ์ ์กฐ๊ธ ๊ณ ๋ฏผ๋๋ ๋ถ๋ถ์ด์ง๋ง,
MVC ํจํด์ View์์ ๋ค๋ฅธ ํด๋์ค๋ฅผ ๊ฐ์ ธ์ ๋ถ๊ธฐ๊ฐ ๋ฐ์ํ ์ ์๋ ๋ก์ง์ ์ฌ์ฉํ๋ ๊ฒ์ ํจํด์ ๊ท์น์ ๋ง์ง ์๋๋ค๊ณ ์๊ฐํฉ๋๋ค. |
@@ -0,0 +1,17 @@
+package baseball.view.validator
+
+import baseball.util.*
+
+object InputValidator {
+
+ fun validateUserNumber(userNumber: String): String {
+ require(userNumber.length == MAX_SIZE) { ERROR_INVALID_INPUT_MESSAGE }
+ return userNumber
+ }
+
+ fun validateUserCommand(command: String): String {
+ require(command == RETRY_COMMAND || command == QUIT_COMMAND) { ERROR_INVALID_COMMAND_MESSAGE }
+ return command
+ }
+}
+ | Kotlin | ๋ฏผ์ฌ๋๊ป์ ์ ์๊ฒ require์ ๋ํด ์๋ ค์ฃผ์
จ์ฃ ..! ์ ์ฐ๊ณ ์์ต๋๋ค! ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,59 @@
+package baseball.model
+
+import baseball.util.*
+
+
+class AnswerBoard {
+ // ์ํ๋ฅผ ๋ชจ๋ ์์์ผ๋ก ์ด๊ธฐํ
+ private val stateList = MutableList(MAX_SIZE) { BaseballState.OUT }
+
+
+ private fun createCount(): Triple<Int, Int, Int> {
+ var strikeCount = 0
+ var ballCount = 0
+ var outCount = 0
+
+ stateList.forEach { state ->
+ when (state) {
+ BaseballState.STRIKE -> strikeCount++
+ BaseballState.BALL -> ballCount++
+ BaseballState.OUT -> outCount++
+ }
+ }
+ return Triple(strikeCount, ballCount, outCount)
+ }
+
+ fun createResult(answer: String, userNumber: String) {
+ answer.forEachIndexed { answerIndex, answerNumber ->
+ if (answerNumber == userNumber[answerIndex]) {
+ stateList[answerIndex] = BaseballState.STRIKE
+ }
+ if (answerNumber != userNumber[answerIndex] && answer.contains(userNumber[answerIndex])) {
+ stateList[answerIndex] = BaseballState.BALL
+ }
+ if (answerNumber != userNumber[answerIndex] && !answer.contains(userNumber[answerIndex])) {
+ stateList[answerIndex] = BaseballState.OUT
+ }
+ }
+ }
+
+ fun printResult(): String {
+ val (strikeCount, ballCount, outCount) = createCount()
+ var message = EMPTY_STRING
+
+ // ์์์ผ ๋ ์ถ๋ ฅ
+ if (outCount == stateList.size) message = PRINT_NOTHING_MESSAGE
+ // ์คํธ๋ผ์ดํฌ์ผ๋ ์ถ๋ ฅ
+ if (ballCount == 0 && strikeCount != 0) message = PRINT_STRIKE_MESSAGE.format(strikeCount)
+ // ๋ณผ์ผ๋ ์ถ๋ ฅ
+ if (ballCount != 0 && strikeCount == 0) message = PRINT_BALL_MESSAGE.format(ballCount)
+ // ์คํธ๋ผ์ดํฌ ๋ณผ์ผ ๋
+ if (ballCount != 0 && strikeCount != 0) message = PRINT_STRIKE_BALL_MESSAGE.format(ballCount, strikeCount)
+
+ return message
+ }
+
+ fun isThreeStrike(): Boolean = stateList.all { state -> state == BaseballState.STRIKE }
+
+ fun clearState() = stateList.replaceAll { BaseballState.OUT }
+} | Kotlin | createCount๋ผ๋ ํจ์๋ช
์ "๊ฐ์๋ฅผ **_๋ง๋ ๋ค_**"๋ผ๋ ์๋ฏธ์ธ ๊ฒ ๊ฐ์ต๋๋ค.
ํ์ง๋ง ํจ์์ ์ญํ ์ "์คํธ๋ผ์ดํฌ, ๋ณผ, ์์์ ๊ฐ์๋ฅผ **_์ผ๋ค_**"์ธ ๊ฒ ๊ฐ์ต๋๋ค.
ํจ์๋ช
๊ณผ ์ธ๊ฐ์ง์ ๊ธฐ๋ฅ์ ๋๋๋ ๊ฒ์ ์ด๋จ๊น์?
countStrike, countBall, countOut๊ณผ ๊ฐ์ด์! |
@@ -0,0 +1,59 @@
+package baseball.model
+
+import baseball.util.*
+
+
+class AnswerBoard {
+ // ์ํ๋ฅผ ๋ชจ๋ ์์์ผ๋ก ์ด๊ธฐํ
+ private val stateList = MutableList(MAX_SIZE) { BaseballState.OUT }
+
+
+ private fun createCount(): Triple<Int, Int, Int> {
+ var strikeCount = 0
+ var ballCount = 0
+ var outCount = 0
+
+ stateList.forEach { state ->
+ when (state) {
+ BaseballState.STRIKE -> strikeCount++
+ BaseballState.BALL -> ballCount++
+ BaseballState.OUT -> outCount++
+ }
+ }
+ return Triple(strikeCount, ballCount, outCount)
+ }
+
+ fun createResult(answer: String, userNumber: String) {
+ answer.forEachIndexed { answerIndex, answerNumber ->
+ if (answerNumber == userNumber[answerIndex]) {
+ stateList[answerIndex] = BaseballState.STRIKE
+ }
+ if (answerNumber != userNumber[answerIndex] && answer.contains(userNumber[answerIndex])) {
+ stateList[answerIndex] = BaseballState.BALL
+ }
+ if (answerNumber != userNumber[answerIndex] && !answer.contains(userNumber[answerIndex])) {
+ stateList[answerIndex] = BaseballState.OUT
+ }
+ }
+ }
+
+ fun printResult(): String {
+ val (strikeCount, ballCount, outCount) = createCount()
+ var message = EMPTY_STRING
+
+ // ์์์ผ ๋ ์ถ๋ ฅ
+ if (outCount == stateList.size) message = PRINT_NOTHING_MESSAGE
+ // ์คํธ๋ผ์ดํฌ์ผ๋ ์ถ๋ ฅ
+ if (ballCount == 0 && strikeCount != 0) message = PRINT_STRIKE_MESSAGE.format(strikeCount)
+ // ๋ณผ์ผ๋ ์ถ๋ ฅ
+ if (ballCount != 0 && strikeCount == 0) message = PRINT_BALL_MESSAGE.format(ballCount)
+ // ์คํธ๋ผ์ดํฌ ๋ณผ์ผ ๋
+ if (ballCount != 0 && strikeCount != 0) message = PRINT_STRIKE_BALL_MESSAGE.format(ballCount, strikeCount)
+
+ return message
+ }
+
+ fun isThreeStrike(): Boolean = stateList.all { state -> state == BaseballState.STRIKE }
+
+ fun clearState() = stateList.replaceAll { BaseballState.OUT }
+} | Kotlin | ๋ณ์๋ช
์ด ๋์ฌ + ๋ช
์ฌ๋ก ํ๋ค๋ฉด ํจ์๋ช
๊ณผ ๋ถ์ผ์น ํ์ง ์์๊น์ ?!?
์ด๋ฆ ์ง๊ธฐ์์ ์ค์ํ ๋ช
์ฌ๋ ์์ ์ฐ๋ ๊ฒ์ด ์ข๋ซ๊ณ ํฉ๋๋ค !! ex) countStrike -> strikeCount |
@@ -0,0 +1,12 @@
+package baseball.view
+
+import baseball.util.USER_INPUT_NUMBER_MESSAGE
+import baseball.view.validator.InputValidator
+
+class User {
+ fun createNumber(): String {
+ print(USER_INPUT_NUMBER_MESSAGE)
+ val userNumber = readLine()!!
+ return InputValidator.validateUserNumber(userNumber)
+ }
+}
\ No newline at end of file | Kotlin | ์ด ๋ถ๋ถ๋ ์ง๊ธ๊น์ง๋ ๊ณ ๋ฏผ์ด์๋๋ฐ ์์ธ์ฒ๋ฆฌ๋ฅผ ๋ฐ๋ก ํด์ฃผ๋ ๊ฒ์ด ์ข๋ค๊ณ ํฉ๋๋ค !! |
@@ -1,37 +1,20 @@
import java.util.List;
+import java.util.Set;
-import domain.LottoGame;
-import domain.LottoResults;
-import domain.WinningAnalyzer;
-import domain.WinningStatistics;
+import domain.ManualRequest;
import view.LottoInputView;
-import view.LottoOutputView;
public class LottoController {
- private LottoGame lottoGame;
- private WinningAnalyzer winningAnalyzer;
- public void playLottoGames(int money) {
- lottoGame = new LottoGame();
- lottoGame.generateLottoResultsFromMoney(money);
- }
-
- public void getLottoResults() {
- List<List<Integer>> results = lottoGame.getLottoResults().lottoNumbersToInt();
- int gameCount = lottoGame.getCount();
- LottoOutputView.printLottoResults(gameCount, results);
- }
-
- public void getWinningStatistics() {
- LottoResults lottoResults = lottoGame.getLottoResults();
- winningAnalyzer = new WinningAnalyzer(lottoResults, LottoInputView.getWinningNumbers(), LottoInputView.getBonusNumber());
- WinningStatistics winningStatistics = winningAnalyzer.calculateWinningStatistics();
- LottoOutputView.printBeforeWinnings();
- LottoOutputView.printWinnings(winningStatistics);
- }
-
- public void getReturnOnInvestment() {
- int money = lottoGame.getMoney();
- LottoOutputView.printReturnOnInvestment(winningAnalyzer.getReturnOnInvestment(money));
+ public static void main(String[] args) {
+ int money = LottoInputView.getMoney();
+ int manualCount = LottoInputView.getManualCount();
+ List<Set<Integer>> manualNumbers = LottoInputView.getManualLottos(manualCount);
+ LottoService lottoService = new LottoService();
+ ManualRequest manualRequest = new ManualRequest(manualCount, manualNumbers);
+ lottoService.playLottoGames(manualRequest, money);
+ lottoService.lottoResults();
+ lottoService.winningStatistics();
+ lottoService.returnOnInvestment();
}
} | Java | ์ผ๊ธ ์ปฌ๋ ์
์ ์จ๋ณด๋ฉด ์ด๋จ๊น์? |
@@ -1,13 +0,0 @@
-import view.LottoInputView;
-
-public class LottoMain {
-
- public static void main(String[] args) {
- int money = LottoInputView.getMoney();
- LottoController lottoController = new LottoController();
- lottoController.playLottoGames(money);
- lottoController.getLottoResults();
- lottoController.getWinningStatistics();
- lottoController.getReturnOnInvestment();
- }
-} | Java | - ๊ฐ ๋์
, ๋ฉ์๋ ํธ์ถ๊ณผ ๊ฐ์ด ๊ฐ๋
์ ์ ์ฌ์ฑ์ด ๋ค๋ฅธ ์ฝ๋๋ค๊ฐ์๋ ๊ฐํ์ผ๋ก ๊ฐ๋
์ฑ์ ๋ํ์ค ์ ์์ต๋๋ค.
- ์ปจํธ๋กค๋ฌ์๊ฒ ์ธ๋ถ ๊ธฐ๋ฅ๋ค์ ํธ์ถํ๊ณ ์์ต๋๋ค. ์ด๊ฑด ๋น์ฆ๋์ค๋ก์ง ์ค์ผ์คํธ๋ ์ด์
์ผ๋ก ๋ณด์ฌ์. ๊ทธ๋ฅ ์ ์ ํ Request ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๊ณ ์ต์ข
๊ฒฐ๊ณผ๋ง ๋ฐํ๋ฐ์์ ์ถ๋ ฅ๊ณ์ธต์๊ฒ ์ ๋ฌํ๋ฉด ๋ ๊ฒ ๊ฐ์์.
- ํ์ฌ ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด LottoMain์ด ์ปจํธ๋กค๋ฌ์ ์ญํ ์ ํ๊ณ ์๊ณ , LottoController๊ฐ Service Layer์ ์ญํ ์ ํ๊ณ ์์ต๋๋ค. ๊ทธ๋ผ ์ฐจ๋ผ๋ฆฌ Controller์์ View Layer๋ฅผ ๋ถ๋ฆฌํด LottoMain์ผ๋ก ์ฎ๊ธฐ์๊ณ LottoController์ ์ด๋ฆ์ LottoService๋ก ๋ฐ๊พธ๋๊ฑด ์ด๋จ๊น์ |
@@ -0,0 +1,15 @@
+package domain;
+
+public class GameCount {
+ private final int manualCount;
+ private final int automaticCount;
+
+ public GameCount(int totalCount, int manualCount) {
+ this.manualCount = manualCount;
+ this.automaticCount = totalCount - manualCount;
+ }
+
+ public int getAutomaticCount() {
+ return automaticCount;
+ }
+} | Java | ๋ด๋ถ์ ์ผ๋ก ๊ฐ์ด ๋ณ๊ฒฝ๋์ง ์๋๋ค๋ฉด final ํค์๋๋ฅผ ํตํด ๋ถ๋ณ์ฑ ํ๋ณด๋ฅผ ํ ์ ์์ ๊ฒ ๊ฐ๋ค์. |
@@ -1,23 +1,47 @@
package domain;
+import java.util.List;
+import java.util.Set;
+
public class LottoGame {
private LottoResults lottoResults;
-
private Money money;
+ private WinningAnalyzer winningAnalyzer;
public LottoGame() {
this.lottoResults = new LottoResults();
}
- public void generateLottoResultsFromMoney(int money) {
- this.money = new Money(money);
- for (int i = 0; i < getCount(); i++) {
- lottoResults.add(LottoNumGenerator.generateResult());
+ public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) {
+ int manualCount = manualRequest.getManualCount();
+ List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers();
+ this.money = new Money(money, manualCount);
+ if (manualCount != manualNumbers.size()) {
+ throw new IllegalArgumentException("์๋ ๊ตฌ๋งค๊ฐ ์๋ชป๋์์ต๋๋ค.");
+ }
+ // ์๋ ๊ตฌ๋งค
+ generateManualResults(manualCount, manualNumbers);
+ generateAutomaticResults();
+ }
+
+ private void generateAutomaticResults() {
+ for (int i = 0; i < getAutomaticCount(); i++) {
+ lottoResults.add(LottoNumGenerator.generateAutomaticResults());
+ }
+ }
+
+ private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) {
+ for (int i = 0; i < manualCount; i++) {
+ lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i)));
}
}
public int getCount() {
- return money.getCount();
+ return money.getTotalCount();
+ }
+
+ public int getAutomaticCount() {
+ return money.getAutomaticCount();
}
public int getMoney() {
@@ -27,5 +51,19 @@ public int getMoney() {
public LottoResults getLottoResults() {
return lottoResults;
}
+
+ public List<List<Integer>> getLottoResultsToInt() {
+ return lottoResults.lottoResultsToInt();
+ }
+
+ public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) {
+ winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber);
+ return winningAnalyzer.calculateWinningStatistics();
+ }
+
+ public float getReturnOnInvestment() {
+ return winningAnalyzer.getReturnOnInvestment(money.getMoney());
+ }
+
}
| Java | ์ด๋ฐ ์ฐธ์กฐํ์
๋ ์ผ๊ธ ์ปฌ๋ ์
์ ์ด์ฉํด์ ๋ด๋ถ ์ธ๋ฑ์ค ์ ์ด๋ฅผ ์ฑ
์ ๋ถ๋ฆฌํ ์ ์์ ๊ฒ ๊ฐ์์. |
@@ -1,23 +1,47 @@
package domain;
+import java.util.List;
+import java.util.Set;
+
public class LottoGame {
private LottoResults lottoResults;
-
private Money money;
+ private WinningAnalyzer winningAnalyzer;
public LottoGame() {
this.lottoResults = new LottoResults();
}
- public void generateLottoResultsFromMoney(int money) {
- this.money = new Money(money);
- for (int i = 0; i < getCount(); i++) {
- lottoResults.add(LottoNumGenerator.generateResult());
+ public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) {
+ int manualCount = manualRequest.getManualCount();
+ List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers();
+ this.money = new Money(money, manualCount);
+ if (manualCount != manualNumbers.size()) {
+ throw new IllegalArgumentException("์๋ ๊ตฌ๋งค๊ฐ ์๋ชป๋์์ต๋๋ค.");
+ }
+ // ์๋ ๊ตฌ๋งค
+ generateManualResults(manualCount, manualNumbers);
+ generateAutomaticResults();
+ }
+
+ private void generateAutomaticResults() {
+ for (int i = 0; i < getAutomaticCount(); i++) {
+ lottoResults.add(LottoNumGenerator.generateAutomaticResults());
+ }
+ }
+
+ private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) {
+ for (int i = 0; i < manualCount; i++) {
+ lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i)));
}
}
public int getCount() {
- return money.getCount();
+ return money.getTotalCount();
+ }
+
+ public int getAutomaticCount() {
+ return money.getAutomaticCount();
}
public int getMoney() {
@@ -27,5 +51,19 @@ public int getMoney() {
public LottoResults getLottoResults() {
return lottoResults;
}
+
+ public List<List<Integer>> getLottoResultsToInt() {
+ return lottoResults.lottoResultsToInt();
+ }
+
+ public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) {
+ winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber);
+ return winningAnalyzer.calculateWinningStatistics();
+ }
+
+ public float getReturnOnInvestment() {
+ return winningAnalyzer.getReturnOnInvestment(money.getMoney());
+ }
+
}
| Java | Money๊ฐ์ฒด์ธ๋ฐ count์ ๋ณด๊น์ง ๋ด์๋ฒ๋ฆฌ๋ฉด ์ผ๊ธ ๊ฐ์ฒด๋ก์จ์ ํจ์ฉ์ด ๋จ์ด์ง๊ณ ๊ฐ์ฒด ์๊ทธ๋์ฒ๊ฐ ๋ถ๋ถ๋ช
ํด์ง ๊ฒ ๊ฐ๋ค์ ๐ค |
@@ -1,23 +1,47 @@
package domain;
+import java.util.List;
+import java.util.Set;
+
public class LottoGame {
private LottoResults lottoResults;
-
private Money money;
+ private WinningAnalyzer winningAnalyzer;
public LottoGame() {
this.lottoResults = new LottoResults();
}
- public void generateLottoResultsFromMoney(int money) {
- this.money = new Money(money);
- for (int i = 0; i < getCount(); i++) {
- lottoResults.add(LottoNumGenerator.generateResult());
+ public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) {
+ int manualCount = manualRequest.getManualCount();
+ List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers();
+ this.money = new Money(money, manualCount);
+ if (manualCount != manualNumbers.size()) {
+ throw new IllegalArgumentException("์๋ ๊ตฌ๋งค๊ฐ ์๋ชป๋์์ต๋๋ค.");
+ }
+ // ์๋ ๊ตฌ๋งค
+ generateManualResults(manualCount, manualNumbers);
+ generateAutomaticResults();
+ }
+
+ private void generateAutomaticResults() {
+ for (int i = 0; i < getAutomaticCount(); i++) {
+ lottoResults.add(LottoNumGenerator.generateAutomaticResults());
+ }
+ }
+
+ private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) {
+ for (int i = 0; i < manualCount; i++) {
+ lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i)));
}
}
public int getCount() {
- return money.getCount();
+ return money.getTotalCount();
+ }
+
+ public int getAutomaticCount() {
+ return money.getAutomaticCount();
}
public int getMoney() {
@@ -27,5 +51,19 @@ public int getMoney() {
public LottoResults getLottoResults() {
return lottoResults;
}
+
+ public List<List<Integer>> getLottoResultsToInt() {
+ return lottoResults.lottoResultsToInt();
+ }
+
+ public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) {
+ winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber);
+ return winningAnalyzer.calculateWinningStatistics();
+ }
+
+ public float getReturnOnInvestment() {
+ return winningAnalyzer.getReturnOnInvestment(money.getMoney());
+ }
+
}
| Java | ์๋,์๋ ๋ก๋๋ฉด ๊ทธ๋ฅ LottoNumber, Lotto, Lottos ์ ๋๋ก ๋ค์ด๋ฐ์ ์ง์ด๋ ๋ ๊ฒ ๊ฐ๋ค์ Result๋ผ๊ณ ํ๋ ๋ญ๊ฐ ๋น๊ต๊น์ง ๋ค ๋๋ ์ต์ข
๊ฒฐ๊ณผ๋ฅผ ์๋ฏธํ๋ ๊ฑธ๋ก ๋ณด์
๋๋ค. |
@@ -1,23 +1,47 @@
package domain;
+import java.util.List;
+import java.util.Set;
+
public class LottoGame {
private LottoResults lottoResults;
-
private Money money;
+ private WinningAnalyzer winningAnalyzer;
public LottoGame() {
this.lottoResults = new LottoResults();
}
- public void generateLottoResultsFromMoney(int money) {
- this.money = new Money(money);
- for (int i = 0; i < getCount(); i++) {
- lottoResults.add(LottoNumGenerator.generateResult());
+ public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) {
+ int manualCount = manualRequest.getManualCount();
+ List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers();
+ this.money = new Money(money, manualCount);
+ if (manualCount != manualNumbers.size()) {
+ throw new IllegalArgumentException("์๋ ๊ตฌ๋งค๊ฐ ์๋ชป๋์์ต๋๋ค.");
+ }
+ // ์๋ ๊ตฌ๋งค
+ generateManualResults(manualCount, manualNumbers);
+ generateAutomaticResults();
+ }
+
+ private void generateAutomaticResults() {
+ for (int i = 0; i < getAutomaticCount(); i++) {
+ lottoResults.add(LottoNumGenerator.generateAutomaticResults());
+ }
+ }
+
+ private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) {
+ for (int i = 0; i < manualCount; i++) {
+ lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i)));
}
}
public int getCount() {
- return money.getCount();
+ return money.getTotalCount();
+ }
+
+ public int getAutomaticCount() {
+ return money.getAutomaticCount();
}
public int getMoney() {
@@ -27,5 +51,19 @@ public int getMoney() {
public LottoResults getLottoResults() {
return lottoResults;
}
+
+ public List<List<Integer>> getLottoResultsToInt() {
+ return lottoResults.lottoResultsToInt();
+ }
+
+ public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) {
+ winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber);
+ return winningAnalyzer.calculateWinningStatistics();
+ }
+
+ public float getReturnOnInvestment() {
+ return winningAnalyzer.getReturnOnInvestment(money.getMoney());
+ }
+
}
| Java | ์๋์์ฑ๊ณผ ์๋์์ฑ์ ๋ฉ์๋๋ก ๊ตฌ๋ถํ๊ณ ์๋๋ฐ LottoGenerator -> LottoAutoGenerator, LottoManualGenerator ๋ฑ์ผ๋ก ์ ๋ตํจํด์ ์ฌ์ฉํ ์๋ ์์ ๊ฒ ๊ฐ๋ค์ |
@@ -1,23 +1,47 @@
package domain;
+import java.util.List;
+import java.util.Set;
+
public class LottoGame {
private LottoResults lottoResults;
-
private Money money;
+ private WinningAnalyzer winningAnalyzer;
public LottoGame() {
this.lottoResults = new LottoResults();
}
- public void generateLottoResultsFromMoney(int money) {
- this.money = new Money(money);
- for (int i = 0; i < getCount(); i++) {
- lottoResults.add(LottoNumGenerator.generateResult());
+ public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) {
+ int manualCount = manualRequest.getManualCount();
+ List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers();
+ this.money = new Money(money, manualCount);
+ if (manualCount != manualNumbers.size()) {
+ throw new IllegalArgumentException("์๋ ๊ตฌ๋งค๊ฐ ์๋ชป๋์์ต๋๋ค.");
+ }
+ // ์๋ ๊ตฌ๋งค
+ generateManualResults(manualCount, manualNumbers);
+ generateAutomaticResults();
+ }
+
+ private void generateAutomaticResults() {
+ for (int i = 0; i < getAutomaticCount(); i++) {
+ lottoResults.add(LottoNumGenerator.generateAutomaticResults());
+ }
+ }
+
+ private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) {
+ for (int i = 0; i < manualCount; i++) {
+ lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i)));
}
}
public int getCount() {
- return money.getCount();
+ return money.getTotalCount();
+ }
+
+ public int getAutomaticCount() {
+ return money.getAutomaticCount();
}
public int getMoney() {
@@ -27,5 +51,19 @@ public int getMoney() {
public LottoResults getLottoResults() {
return lottoResults;
}
+
+ public List<List<Integer>> getLottoResultsToInt() {
+ return lottoResults.lottoResultsToInt();
+ }
+
+ public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) {
+ winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber);
+ return winningAnalyzer.calculateWinningStatistics();
+ }
+
+ public float getReturnOnInvestment() {
+ return winningAnalyzer.getReturnOnInvestment(money.getMoney());
+ }
+
}
| Java | ์ด ๋ฉ์๋๋ ProfitCalculator -> DefaultProfiltCalculator๋ฑ์ผ๋ก ๋ถ๋ฆฌํด์ ์ด์จ ๊ณ์ฐ๋ ์ถ์ํํ๋ฉด ์ ์ง๋ณด์์ฑ์ด ๋์์ง ๊ฒ ๊ฐ์์ |
@@ -1,7 +1,9 @@
package domain;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
+import java.util.Set;
public class LottoResult {
private List<LottoNumber> lottoResult;
@@ -14,10 +16,11 @@ public LottoResult(List<LottoNumber> lottoNumbers) {
this.lottoResult = lottoNumbers;
}
- public static LottoResult fromIntegers(List<Integer> lottoIntegers) {
+ public static LottoResult fromIntegers(Set<Integer> lottoIntegers) {
LottoResult lottoResult = new LottoResult();
- for (int i = 0; i < lottoIntegers.size(); i++) {
- lottoResult.lottoResult.add(LottoNumber.from(lottoIntegers.get(i)));
+ Iterator<Integer> iterator = lottoIntegers.iterator();
+ while (iterator.hasNext()) {
+ lottoResult.lottoResult.add(LottoNumber.from(iterator.next()));
}
return lottoResult;
}
@@ -38,4 +41,14 @@ public List<Integer> getLottoResult() {
}
return lottoNumbers;
}
+
+ public int calculateCount(WinningNumbers winningNumbers) {
+ int count = 0;
+ return lottoResult.stream().mapToInt(num -> num.addCountIfContain(count, winningNumbers)).sum();
+ }
+
+ public boolean isBonusMatch(BonusNumber bonusNumber) {
+ return lottoResult.stream()
+ .anyMatch(lottoNumber -> bonusNumber.isBonusMatch(lottoNumber));
+ }
} | Java | ๋๋ค์์์ count๋ผ๋ ์ธ๋ถ ๊ฐ์ ์ํฅ์ ์ฃผ๊ณ ์์ด์. ๐ค
๋๋ค์์ ์ธ๋ถ๊ฐ์ ๋ํ ์ํฅ์ ์ฃผ์ง ์์์ผ ํ๊ธฐ์ ์์ ํจ์๋ก ์ค๊ณ๋์ผํฉ๋๋ค. ๋ก์ง๋ด์์ ์ง์ count๊ฐ์ ์ง์ญ๋ณ์๋ฅผ ๋ณ๊ฒฝํ๋ ค๊ณ ํ๋ฉด effective final variable์ ์์ ํ๋ คํ๋ค๊ณ ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์ธ๋ฐ, ๋ฉ์๋๋ฅผ ๋ค์ ํธ์ถํด์ ๊ทธ ๋ฌธ์ ๋ฅผ ํผํ์ง๋ง, ๊ทธ๋ ๋ค๊ณ ๋ฌธ์ ๊ฐ ๋์ง ์๋๊ฑด ์๋๋๋ค.
filter์ count๋ฑ์ ์ด์ฉํด์๊ตฌํํ ์๋ ์์ ๊ฒ ๊ฐ๋ค์ |
@@ -1,68 +1,75 @@
package domain;
+import static domain.WinningStatistics.THRESHOLD;
+
import java.util.HashMap;
import java.util.Map;
+import java.util.function.Function;
public enum WinningPrizes {
- MISS(0, 0) {
-
- },
- FIFTH_PRIZE(5, 5_000),
- FOURTH_PRIZE(4, 50_000),
- THIRD_PRIZE(3, 1_500_000),
- SECOND_PRIZE(2, 3_000_000),
- FIRST_PRIZE(1, 2_000_000_000);
-
- public static final int OFFSET = 3;
+ ZERO(0, false, 0, 0, count -> THRESHOLD),
+ ONE(1, false,0, 0, count -> THRESHOLD),
+ TWO(2, false, 5, 5_000, count -> THRESHOLD),
+ THREE(3, false, 5, 5_000, count -> count),
+ FOUR(4, false, 4, 50_000, count -> count),
+ FIVE(5, false, 3, 1_500_000, count -> count),
+ FIVE_BONUS(5, true, 2, 3_000_000, count -> count),
+ SIX(6, false, 1, 2_000_000_000, count -> count);
- private static final Map<Integer, WinningPrizes> WINNING_PRIZES_MAP = new HashMap<>();
+ private static final int HASH_GENERATION_NUMBER = 31;
+ private static final Map<Integer, WinningPrizes> WINNING_PRIZE_MATCHERS_MAP = new HashMap<>();
- static {
- for (WinningPrizes value : values()) {
- WINNING_PRIZES_MAP.put(value.rank, value);
- }
- }
+ private int numberOfCount;
+ private boolean isBonusMatch;
private int rank;
- private final int prizeMoney;
+ private int prizeMoney;
+ private Function<Integer, Integer> countSupplier;
- WinningPrizes(int rank, int prizeMoney) {
+
+ WinningPrizes(int numberOfCount, boolean isBonusMatch, int rank, int prizeMoney, Function<Integer, Integer> countSupplier) {
+ this.numberOfCount = numberOfCount;
+ this.isBonusMatch = isBonusMatch;
this.rank = rank;
this.prizeMoney = prizeMoney;
+ this.countSupplier = countSupplier;
}
- public int calculatePrizeMoney(int count) {
- return prizeMoney * count;
+ static {
+ for (WinningPrizes value : WinningPrizes.values()) {
+ WINNING_PRIZE_MATCHERS_MAP.put(getHashCode(value.numberOfCount, value.isBonusMatch), value);
+ }
}
- public int getPrizeMoney() {
- return prizeMoney;
+ public static int getHashCode(int numberOfCount, boolean isBonusMatch) {
+ int bonusNum = 0;
+ if (isBonusMatch) {
+ bonusNum = 1;
+ }
+ return numberOfCount * HASH_GENERATION_NUMBER + bonusNum;
}
- public int getRank() {
- return rank;
+ public static WinningPrizes valueOf(int numberOfCount, boolean isBonusMatch) {
+ isBonusMatch = checkBonus(numberOfCount, isBonusMatch);
+ return WINNING_PRIZE_MATCHERS_MAP.get(getHashCode(numberOfCount, isBonusMatch));
}
- public static WinningPrizes valueOf(int countOfMatch, boolean matchBonus) {
- if (countOfMatch < OFFSET) {
- return WinningPrizes.MISS;
- }
-
- if (WinningPrizes.SECOND_PRIZE.rank == countOfMatch) {
- return decideSecondOrThirdPrizes(matchBonus);
+ private static boolean checkBonus(int numberOfCount, boolean isBonusMatch) {
+ if (FIVE_BONUS.getNumberOfCount() != numberOfCount && isBonusMatch) {
+ isBonusMatch = false;
}
+ return isBonusMatch;
+ }
- return WINNING_PRIZES_MAP.get(countOfMatch);
+ public int calculatePrizeMoney(int count) {
+ return prizeMoney * count;
}
- public static WinningPrizes valueOf(int rank) {
- return WINNING_PRIZES_MAP.get(rank);
+ public int getPrizeMoney() {
+ return prizeMoney;
}
- private static WinningPrizes decideSecondOrThirdPrizes(boolean matchBonus) {
- if (matchBonus) {
- return WinningPrizes.SECOND_PRIZE;
- }
- return WinningPrizes.THIRD_PRIZE;
+ public int getNumberOfCount() {
+ return countSupplier.apply(numberOfCount);
}
} | Java | IntFunction์ ํ์ฉํด๋ ์ข์ ๊ฒ ๊ฐ์์. |
@@ -2,25 +2,62 @@
import static org.assertj.core.api.Assertions.assertThat;
+import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import org.junit.jupiter.api.Test;
public class LottoGameTest {
@Test
- public void ๋ก๋_๊ตฌ์
๊ธ์ก์_์
๋ ฅํ๋ฉด_๊ตฌ์
๊ธ์ก์_ํด๋นํ๋_๋ก๋๋ฅผ_๋ฐํํ๋ค() {
+ public void ๋ก๋_๊ตฌ์
๊ธ์ก๊ณผ_๋ฒํธ๋ฅผ_์๋์ผ๋ก_์
๋ ฅํ๋ฉด_๊ตฌ์
๊ธ์ก์_ํด๋นํ๋_๋ก๋๋ฅผ_๋ฐํํ๋ค() {
//given
int money = 14000;
+ Set<Integer> manualLottoNumber = new HashSet<>();
+ manualLottoNumber.add(2);
+ manualLottoNumber.add(4);
+ manualLottoNumber.add(6);
+ manualLottoNumber.add(7);
+ manualLottoNumber.add(10);
+ manualLottoNumber.add(12);
+
+ int manualCount = 1;
LottoGame lottoGenerator = new LottoGame();
+ List<Set<Integer>> manualNumbers = new ArrayList<>();
+ manualNumbers.add(manualLottoNumber);
+ ManualRequest manualRequest = new ManualRequest(manualCount, manualNumbers);
+
//when
- lottoGenerator.generateLottoResultsFromMoney(money);
+ lottoGenerator.generateLottoResultsFromMoney(manualRequest, money);
LottoResults lottoResults = lottoGenerator.getLottoResults();
- List<List<Integer>> lottoResultList = lottoResults.lottoNumbersToInt();
+ List<List<Integer>> lottoResultList = lottoResults.lottoResultsToInt();
//then
assertThat(lottoResultList).hasSize(14);
for (List<Integer> lottoNum : lottoResultList) {
assertThat(lottoNum).hasSize(6);
}
}
+
+// @Test
+// public void ๋ก๋_๋ฒํธ๋ฅผ_์๋์ผ๋ก_์
๋ ฅํ _๋_์ค๋ณต๋_๋ฒํธ๋ฅผ_์
๋ ฅํ๋ฉด_์์ธ๋ฅผ_๋์ง๋ค() {
+// //given
+// int money = 1000;
+// List<Integer> manualLottoNumber = Arrays.asList(2, 2, 6, 7, 10, 12);
+// int manualCount = 1;
+// LottoGame lottoGenerator = new LottoGame();
+// List<List<Integer>> manualNumbers = new ArrayList<>();
+// manualNumbers.add(manualLottoNumber);
+// ManualRequest manualRequest = new ManualRequest(manualCount, manualNumbers);
+//
+// //when
+//
+//
+// //then
+//
+// }
+
+
+
} | Java | ์ฌ์ฉํ์ง ์๋ ์ฃผ์(์ฝ๋)์ ์ ๊ฑฐํด์ฃผ์ธ์! |
@@ -0,0 +1,97 @@
+package com.selab.todo.controller;
+
+import com.selab.todo.common.dto.PageDto;
+import com.selab.todo.common.dto.ResponseDto;
+import com.selab.todo.dto.request.diary.DiaryRegisterRequest;
+import com.selab.todo.dto.request.diary.DiaryUpdateRequest;
+import com.selab.todo.dto.request.feel.FeelingUpdateRequest;
+import com.selab.todo.service.DiaryService;
+import com.selab.todo.service.FeelingService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.web.PageableDefault;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@Api(tags = {"Diary API"})
+@RestController
+@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE)
+@RequiredArgsConstructor
+public class DiaryController {
+ private final DiaryService diaryService;
+ private final FeelingService feelingService;
+
+ @ApiOperation(value = "Diary ๋ฑ๋กํ๊ธฐ")
+ @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register")
+ public ResponseEntity<?> register(@RequestBody DiaryRegisterRequest request) {
+ var response = diaryService.register(request);
+ return ResponseDto.created(response);
+ }
+
+ @ApiOperation(value = "Diary ๋จ๊ฑด ์กฐํํ๊ธฐ")
+ @GetMapping("/{id}")
+ public ResponseEntity<?> get(@PathVariable Long id) {
+ var response = diaryService.get(id);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์ ์ฒด ์กฐํํ๊ธฐ")
+ @GetMapping("/search-all")
+ public ResponseEntity<?> getAll(
+ @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
+ ) {
+ var response = diaryService.getAll(pageable);
+ return PageDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์์ ํ๊ธฐ")
+ @PutMapping("/{id}") // @PatchMapping
+ public ResponseEntity<?> update(
+ @PathVariable Long id,
+ @RequestBody DiaryUpdateRequest request
+ ) {
+ var response = diaryService.update(id, request);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์ญ์ ํ๊ธฐ")
+ @DeleteMapping("/{id}")
+ public ResponseEntity<Void> delete(
+ @PathVariable Long id
+ ) {
+ diaryService.delete(id);
+ return ResponseDto.noContent();
+ }
+
+ @ApiOperation(value = "Diary ์๋ณ ์ญ์ ํ๊ธฐ")
+ @DeleteMapping("/month/{month}")
+ public ResponseEntity<Void> monthDelete(@PathVariable int month) {
+ diaryService.monthDelete(month);
+ return ResponseDto.noContent();
+ }
+
+ @ApiOperation(value = "Feeling ์์ ํ๊ธฐ")
+ @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}")
+ public ResponseEntity<?> updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) {
+ var response = feelingService.updateFeeling(id, request);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Feeling ์ ์ฒด ์กฐํํ๊ธฐ")
+ @GetMapping("/feeling-all")
+ public ResponseEntity<?> getAllFeeling(@PageableDefault Pageable pageable) {
+ var response = feelingService.getAllFeeling(pageable);
+ return PageDto.ok(response);
+ }
+} | Java | ์ด๋ฐ ๊ฒฝ์ฐ์๋ month๋ฅผ Request Param์ผ๋ก |
@@ -0,0 +1,97 @@
+package com.selab.todo.controller;
+
+import com.selab.todo.common.dto.PageDto;
+import com.selab.todo.common.dto.ResponseDto;
+import com.selab.todo.dto.request.diary.DiaryRegisterRequest;
+import com.selab.todo.dto.request.diary.DiaryUpdateRequest;
+import com.selab.todo.dto.request.feel.FeelingUpdateRequest;
+import com.selab.todo.service.DiaryService;
+import com.selab.todo.service.FeelingService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.web.PageableDefault;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@Api(tags = {"Diary API"})
+@RestController
+@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE)
+@RequiredArgsConstructor
+public class DiaryController {
+ private final DiaryService diaryService;
+ private final FeelingService feelingService;
+
+ @ApiOperation(value = "Diary ๋ฑ๋กํ๊ธฐ")
+ @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register")
+ public ResponseEntity<?> register(@RequestBody DiaryRegisterRequest request) {
+ var response = diaryService.register(request);
+ return ResponseDto.created(response);
+ }
+
+ @ApiOperation(value = "Diary ๋จ๊ฑด ์กฐํํ๊ธฐ")
+ @GetMapping("/{id}")
+ public ResponseEntity<?> get(@PathVariable Long id) {
+ var response = diaryService.get(id);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์ ์ฒด ์กฐํํ๊ธฐ")
+ @GetMapping("/search-all")
+ public ResponseEntity<?> getAll(
+ @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
+ ) {
+ var response = diaryService.getAll(pageable);
+ return PageDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์์ ํ๊ธฐ")
+ @PutMapping("/{id}") // @PatchMapping
+ public ResponseEntity<?> update(
+ @PathVariable Long id,
+ @RequestBody DiaryUpdateRequest request
+ ) {
+ var response = diaryService.update(id, request);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์ญ์ ํ๊ธฐ")
+ @DeleteMapping("/{id}")
+ public ResponseEntity<Void> delete(
+ @PathVariable Long id
+ ) {
+ diaryService.delete(id);
+ return ResponseDto.noContent();
+ }
+
+ @ApiOperation(value = "Diary ์๋ณ ์ญ์ ํ๊ธฐ")
+ @DeleteMapping("/month/{month}")
+ public ResponseEntity<Void> monthDelete(@PathVariable int month) {
+ diaryService.monthDelete(month);
+ return ResponseDto.noContent();
+ }
+
+ @ApiOperation(value = "Feeling ์์ ํ๊ธฐ")
+ @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}")
+ public ResponseEntity<?> updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) {
+ var response = feelingService.updateFeeling(id, request);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Feeling ์ ์ฒด ์กฐํํ๊ธฐ")
+ @GetMapping("/feeling-all")
+ public ResponseEntity<?> getAllFeeling(@PageableDefault Pageable pageable) {
+ var response = feelingService.getAllFeeling(pageable);
+ return PageDto.ok(response);
+ }
+} | Java | ์ ์ฌ๋ฌ ์กฐ๊ฑด์ผ๋ก ๊ฒ์ ๊ธฐ๋ฅ์ ๋ง๋์ ๊ฒ ๊ฐ์๋ฐ, API๋ฅผ ๋๋์ง ๋ง๊ณ , ๊ฒ์ ๋ชจ๋์ ๋ง๋๋๊ฒ ์ด๋จ๊น์? |
@@ -0,0 +1,71 @@
+package com.selab.todo.entity;
+
+import com.selab.todo.common.BaseEntity;
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import org.springframework.data.annotation.CreatedDate;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import java.time.DayOfWeek;
+import java.time.LocalDateTime;
+import java.time.Month;
+import java.time.Year;
+
+@Entity(name = "diary")
+@Getter
+@Table(name = "diary")
+@NoArgsConstructor(access = AccessLevel.PROTECTED)
+public class Diary extends BaseEntity {
+ @Id
+ @Column(name = "id")
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private Long id;
+
+ @Column(name = "title")
+ private String title;
+
+ @Column(name = "content")
+ private String content;
+
+ @Column(name = "createdAt")
+ @CreatedDate
+ private LocalDateTime createdAt;
+
+ @Column(name = "year")
+ private Year year;
+
+ @Column(name = "month")
+ private Month month;
+
+ @Column(name = "day")
+ private DayOfWeek day;
+
+ @Column(name = "feel")
+ private String feel;
+
+
+ public Diary(String title, String content, String feel, Year year, Month month, DayOfWeek day) {
+ this.title = title;
+ this.content = content;
+ this.feel = feel;
+ this.year = year;
+ this.month = month;
+ this.day = day;
+ }
+
+ public void update(String title, String content, String feel) {
+ this.title = title;
+ this.content = content;
+ this.feel = feel;
+ }
+
+ public void feelingUpdate(String feel){
+ this.feel = feel;
+ }
+}
\ No newline at end of file | Java | enumrated ์ถ๊ฐํด์ผ ๋ ๊ฒ ๊ฐ์๋ฐ์? |
@@ -0,0 +1,97 @@
+package com.selab.todo.controller;
+
+import com.selab.todo.common.dto.PageDto;
+import com.selab.todo.common.dto.ResponseDto;
+import com.selab.todo.dto.request.diary.DiaryRegisterRequest;
+import com.selab.todo.dto.request.diary.DiaryUpdateRequest;
+import com.selab.todo.dto.request.feel.FeelingUpdateRequest;
+import com.selab.todo.service.DiaryService;
+import com.selab.todo.service.FeelingService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.web.PageableDefault;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@Api(tags = {"Diary API"})
+@RestController
+@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE)
+@RequiredArgsConstructor
+public class DiaryController {
+ private final DiaryService diaryService;
+ private final FeelingService feelingService;
+
+ @ApiOperation(value = "Diary ๋ฑ๋กํ๊ธฐ")
+ @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register")
+ public ResponseEntity<?> register(@RequestBody DiaryRegisterRequest request) {
+ var response = diaryService.register(request);
+ return ResponseDto.created(response);
+ }
+
+ @ApiOperation(value = "Diary ๋จ๊ฑด ์กฐํํ๊ธฐ")
+ @GetMapping("/{id}")
+ public ResponseEntity<?> get(@PathVariable Long id) {
+ var response = diaryService.get(id);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์ ์ฒด ์กฐํํ๊ธฐ")
+ @GetMapping("/search-all")
+ public ResponseEntity<?> getAll(
+ @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
+ ) {
+ var response = diaryService.getAll(pageable);
+ return PageDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์์ ํ๊ธฐ")
+ @PutMapping("/{id}") // @PatchMapping
+ public ResponseEntity<?> update(
+ @PathVariable Long id,
+ @RequestBody DiaryUpdateRequest request
+ ) {
+ var response = diaryService.update(id, request);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์ญ์ ํ๊ธฐ")
+ @DeleteMapping("/{id}")
+ public ResponseEntity<Void> delete(
+ @PathVariable Long id
+ ) {
+ diaryService.delete(id);
+ return ResponseDto.noContent();
+ }
+
+ @ApiOperation(value = "Diary ์๋ณ ์ญ์ ํ๊ธฐ")
+ @DeleteMapping("/month/{month}")
+ public ResponseEntity<Void> monthDelete(@PathVariable int month) {
+ diaryService.monthDelete(month);
+ return ResponseDto.noContent();
+ }
+
+ @ApiOperation(value = "Feeling ์์ ํ๊ธฐ")
+ @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}")
+ public ResponseEntity<?> updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) {
+ var response = feelingService.updateFeeling(id, request);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Feeling ์ ์ฒด ์กฐํํ๊ธฐ")
+ @GetMapping("/feeling-all")
+ public ResponseEntity<?> getAllFeeling(@PageableDefault Pageable pageable) {
+ var response = feelingService.getAllFeeling(pageable);
+ return PageDto.ok(response);
+ }
+} | Java | url path์ ๋ํด ๋ค์ ํ๋ฒ ๋ ์ ๊ฒ ๋ถํ๋๋ฆ
๋๋ค!
rest url ๊ท์น ํ์ธํ๊ธฐ |
@@ -0,0 +1,71 @@
+package com.selab.todo.entity;
+
+import com.selab.todo.common.BaseEntity;
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import org.springframework.data.annotation.CreatedDate;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import java.time.DayOfWeek;
+import java.time.LocalDateTime;
+import java.time.Month;
+import java.time.Year;
+
+@Entity(name = "diary")
+@Getter
+@Table(name = "diary")
+@NoArgsConstructor(access = AccessLevel.PROTECTED)
+public class Diary extends BaseEntity {
+ @Id
+ @Column(name = "id")
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private Long id;
+
+ @Column(name = "title")
+ private String title;
+
+ @Column(name = "content")
+ private String content;
+
+ @Column(name = "createdAt")
+ @CreatedDate
+ private LocalDateTime createdAt;
+
+ @Column(name = "year")
+ private Year year;
+
+ @Column(name = "month")
+ private Month month;
+
+ @Column(name = "day")
+ private DayOfWeek day;
+
+ @Column(name = "feel")
+ private String feel;
+
+
+ public Diary(String title, String content, String feel, Year year, Month month, DayOfWeek day) {
+ this.title = title;
+ this.content = content;
+ this.feel = feel;
+ this.year = year;
+ this.month = month;
+ this.day = day;
+ }
+
+ public void update(String title, String content, String feel) {
+ this.title = title;
+ this.content = content;
+ this.feel = feel;
+ }
+
+ public void feelingUpdate(String feel){
+ this.feel = feel;
+ }
+}
\ No newline at end of file | Java | LocalDateTime ํน์ ZoneDateTime๊ณผ ๊ฐ์ ์๊ฐ์ ๋ค๋ฃจ๋ ๊ฐ์ฒด ์ฌ์ฉ์ ํ์ง ์์ ์ด์ ๊ฐ ๋ฌด์์ผ๊น์? |
@@ -0,0 +1,23 @@
+package com.selab.todo.entity;
+
+import lombok.AccessLevel;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+
+import javax.persistence.*;
+
+@Entity(name = "feeling")
+@Getter
+@Table(name = "feeling")
+@NoArgsConstructor(access = AccessLevel.PROTECTED)
+@AllArgsConstructor
+public class Feeling {
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ @Column(name = "id")
+ private Long id;
+
+ @Column(name = "feel")
+ private String feel;
+} | Java | ํด๋น ํด๋์ค์ ์ญํ ์ ๋ฌด์์ผ๊น์?
id, baseentity ์ ๋๋ ์ถ๊ฐํด๋ ๋ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,97 @@
+package com.selab.todo.controller;
+
+import com.selab.todo.common.dto.PageDto;
+import com.selab.todo.common.dto.ResponseDto;
+import com.selab.todo.dto.request.diary.DiaryRegisterRequest;
+import com.selab.todo.dto.request.diary.DiaryUpdateRequest;
+import com.selab.todo.dto.request.feel.FeelingUpdateRequest;
+import com.selab.todo.service.DiaryService;
+import com.selab.todo.service.FeelingService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.web.PageableDefault;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@Api(tags = {"Diary API"})
+@RestController
+@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE)
+@RequiredArgsConstructor
+public class DiaryController {
+ private final DiaryService diaryService;
+ private final FeelingService feelingService;
+
+ @ApiOperation(value = "Diary ๋ฑ๋กํ๊ธฐ")
+ @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register")
+ public ResponseEntity<?> register(@RequestBody DiaryRegisterRequest request) {
+ var response = diaryService.register(request);
+ return ResponseDto.created(response);
+ }
+
+ @ApiOperation(value = "Diary ๋จ๊ฑด ์กฐํํ๊ธฐ")
+ @GetMapping("/{id}")
+ public ResponseEntity<?> get(@PathVariable Long id) {
+ var response = diaryService.get(id);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์ ์ฒด ์กฐํํ๊ธฐ")
+ @GetMapping("/search-all")
+ public ResponseEntity<?> getAll(
+ @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
+ ) {
+ var response = diaryService.getAll(pageable);
+ return PageDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์์ ํ๊ธฐ")
+ @PutMapping("/{id}") // @PatchMapping
+ public ResponseEntity<?> update(
+ @PathVariable Long id,
+ @RequestBody DiaryUpdateRequest request
+ ) {
+ var response = diaryService.update(id, request);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์ญ์ ํ๊ธฐ")
+ @DeleteMapping("/{id}")
+ public ResponseEntity<Void> delete(
+ @PathVariable Long id
+ ) {
+ diaryService.delete(id);
+ return ResponseDto.noContent();
+ }
+
+ @ApiOperation(value = "Diary ์๋ณ ์ญ์ ํ๊ธฐ")
+ @DeleteMapping("/month/{month}")
+ public ResponseEntity<Void> monthDelete(@PathVariable int month) {
+ diaryService.monthDelete(month);
+ return ResponseDto.noContent();
+ }
+
+ @ApiOperation(value = "Feeling ์์ ํ๊ธฐ")
+ @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}")
+ public ResponseEntity<?> updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) {
+ var response = feelingService.updateFeeling(id, request);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Feeling ์ ์ฒด ์กฐํํ๊ธฐ")
+ @GetMapping("/feeling-all")
+ public ResponseEntity<?> getAllFeeling(@PageableDefault Pageable pageable) {
+ var response = feelingService.getAllFeeling(pageable);
+ return PageDto.ok(response);
+ }
+} | Java | ์ ์ฉํด๋ณด์์ต๋๋ค |
@@ -0,0 +1,97 @@
+package com.selab.todo.controller;
+
+import com.selab.todo.common.dto.PageDto;
+import com.selab.todo.common.dto.ResponseDto;
+import com.selab.todo.dto.request.diary.DiaryRegisterRequest;
+import com.selab.todo.dto.request.diary.DiaryUpdateRequest;
+import com.selab.todo.dto.request.feel.FeelingUpdateRequest;
+import com.selab.todo.service.DiaryService;
+import com.selab.todo.service.FeelingService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.web.PageableDefault;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@Api(tags = {"Diary API"})
+@RestController
+@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE)
+@RequiredArgsConstructor
+public class DiaryController {
+ private final DiaryService diaryService;
+ private final FeelingService feelingService;
+
+ @ApiOperation(value = "Diary ๋ฑ๋กํ๊ธฐ")
+ @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register")
+ public ResponseEntity<?> register(@RequestBody DiaryRegisterRequest request) {
+ var response = diaryService.register(request);
+ return ResponseDto.created(response);
+ }
+
+ @ApiOperation(value = "Diary ๋จ๊ฑด ์กฐํํ๊ธฐ")
+ @GetMapping("/{id}")
+ public ResponseEntity<?> get(@PathVariable Long id) {
+ var response = diaryService.get(id);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์ ์ฒด ์กฐํํ๊ธฐ")
+ @GetMapping("/search-all")
+ public ResponseEntity<?> getAll(
+ @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
+ ) {
+ var response = diaryService.getAll(pageable);
+ return PageDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์์ ํ๊ธฐ")
+ @PutMapping("/{id}") // @PatchMapping
+ public ResponseEntity<?> update(
+ @PathVariable Long id,
+ @RequestBody DiaryUpdateRequest request
+ ) {
+ var response = diaryService.update(id, request);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์ญ์ ํ๊ธฐ")
+ @DeleteMapping("/{id}")
+ public ResponseEntity<Void> delete(
+ @PathVariable Long id
+ ) {
+ diaryService.delete(id);
+ return ResponseDto.noContent();
+ }
+
+ @ApiOperation(value = "Diary ์๋ณ ์ญ์ ํ๊ธฐ")
+ @DeleteMapping("/month/{month}")
+ public ResponseEntity<Void> monthDelete(@PathVariable int month) {
+ diaryService.monthDelete(month);
+ return ResponseDto.noContent();
+ }
+
+ @ApiOperation(value = "Feeling ์์ ํ๊ธฐ")
+ @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}")
+ public ResponseEntity<?> updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) {
+ var response = feelingService.updateFeeling(id, request);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Feeling ์ ์ฒด ์กฐํํ๊ธฐ")
+ @GetMapping("/feeling-all")
+ public ResponseEntity<?> getAllFeeling(@PageableDefault Pageable pageable) {
+ var response = feelingService.getAllFeeling(pageable);
+ return PageDto.ok(response);
+ }
+} | Java | default๋ ์ ํ์ํด์?? |
@@ -0,0 +1,97 @@
+package com.selab.todo.controller;
+
+import com.selab.todo.common.dto.PageDto;
+import com.selab.todo.common.dto.ResponseDto;
+import com.selab.todo.dto.request.diary.DiaryRegisterRequest;
+import com.selab.todo.dto.request.diary.DiaryUpdateRequest;
+import com.selab.todo.dto.request.feel.FeelingUpdateRequest;
+import com.selab.todo.service.DiaryService;
+import com.selab.todo.service.FeelingService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.web.PageableDefault;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@Api(tags = {"Diary API"})
+@RestController
+@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE)
+@RequiredArgsConstructor
+public class DiaryController {
+ private final DiaryService diaryService;
+ private final FeelingService feelingService;
+
+ @ApiOperation(value = "Diary ๋ฑ๋กํ๊ธฐ")
+ @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register")
+ public ResponseEntity<?> register(@RequestBody DiaryRegisterRequest request) {
+ var response = diaryService.register(request);
+ return ResponseDto.created(response);
+ }
+
+ @ApiOperation(value = "Diary ๋จ๊ฑด ์กฐํํ๊ธฐ")
+ @GetMapping("/{id}")
+ public ResponseEntity<?> get(@PathVariable Long id) {
+ var response = diaryService.get(id);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์ ์ฒด ์กฐํํ๊ธฐ")
+ @GetMapping("/search-all")
+ public ResponseEntity<?> getAll(
+ @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
+ ) {
+ var response = diaryService.getAll(pageable);
+ return PageDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์์ ํ๊ธฐ")
+ @PutMapping("/{id}") // @PatchMapping
+ public ResponseEntity<?> update(
+ @PathVariable Long id,
+ @RequestBody DiaryUpdateRequest request
+ ) {
+ var response = diaryService.update(id, request);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Diary ์ญ์ ํ๊ธฐ")
+ @DeleteMapping("/{id}")
+ public ResponseEntity<Void> delete(
+ @PathVariable Long id
+ ) {
+ diaryService.delete(id);
+ return ResponseDto.noContent();
+ }
+
+ @ApiOperation(value = "Diary ์๋ณ ์ญ์ ํ๊ธฐ")
+ @DeleteMapping("/month/{month}")
+ public ResponseEntity<Void> monthDelete(@PathVariable int month) {
+ diaryService.monthDelete(month);
+ return ResponseDto.noContent();
+ }
+
+ @ApiOperation(value = "Feeling ์์ ํ๊ธฐ")
+ @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}")
+ public ResponseEntity<?> updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) {
+ var response = feelingService.updateFeeling(id, request);
+ return ResponseDto.ok(response);
+ }
+
+ @ApiOperation(value = "Feeling ์ ์ฒด ์กฐํํ๊ธฐ")
+ @GetMapping("/feeling-all")
+ public ResponseEntity<?> getAllFeeling(@PageableDefault Pageable pageable) {
+ var response = feelingService.getAllFeeling(pageable);
+ return PageDto.ok(response);
+ }
+} | Java | REST API
- POST : /api/v1/diaries
- GET : /api/v1/diaries
- GET : /api/v1/diaries/{id}
- PUT : /api/v1/diaries/{id}
- DELETE : /api/v1/diaries/{id}
์ด ํํ ์๋๊ฐ์?
๊ทธ๋ฆฌ๊ณ url path์ ์ ๋๋ฌธ์๊ฐ ๋ค์ด๊ฐ๋๊ฑธ๊น์ฉ? |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ์๋ก ์ฑ๊ฒฉ์ด ๋ค๋ฅธ ๋ณ์ ์ ์ธ ๊ฐ์๋ ์ค๋ฐ๊ฟ์ ํ ๋ฒ ๋ฃ์ด์ฃผ๋ ๊ฒ ๋ ๋ณด๊ธฐ ์ข์ง ์์๊น์??๐ค |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ์กฐ๊ฑด๋ฌธ์ ์ซ์์ ๋งค๊ฐ๋ณ์๊ฐ ์ผ์นํ๋ค์!
์ด๋ฐ ์์ผ๋ก ์ฝ๋๋ฅผ ์ค์ผ ์ ์์ง ์์๊น์??๐
```suggestion
answer = randomNumber(numberOfDigit[0]);
``` |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | JS์์ ์ ๊ณตํ๋ ํจ์๋ฅผ ์ ํ์ฉํ์
จ๋ค์! ๐ |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ๋งค์ง ๋๋ฒ ๋ถ๋ฆฌ์ ๋ค์ด๋ฐ ์ปจ๋ฒค์
์ ์ค์ํ์
จ๋ค์! ๐ |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ํ๋์ ํจ์๋ ํ ๊ฐ์ง ์ฑ
์๋ง ๊ฐ๋๋ก ํ๋ ๊ฒ ์ข์์!
randomNumber ํจ์์์๋ ์ค๋ณต๋์ง ์๋ ๋์๋ฅผ ๋ฐํํ๋ ๊ฒ์ด ๋ชฉํ์ธ๋ฐ, randomNumber ๋ณ์์ ๋๋ค๊ฐ์ ๋ฃ๋ ํ์๋ ๊ทธ ์ฑ
์๊ณผ๋ ๋ถ๋ฆฌํ ์ ์์ ๊ฒ ๊ฐ์์.
`let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);` ๋ฅผ ๋ค๋ฅธ ํจ์๋ก ๋ถ๋ฆฌํด๋ณด๋ ๊ฑด ์ด๋จ๊น์?? ๐
[์ฐธ๊ณ ์๋ฃ](https://inpa.tistory.com/entry/OOP-%F0%9F%92%A0-%EC%95%84%EC%A3%BC-%EC%89%BD%EA%B2%8C-%EC%9D%B4%ED%95%B4%ED%95%98%EB%8A%94-SRP-%EB%8B%A8%EC%9D%BC-%EC%B1%85%EC%9E%84-%EC%9B%90%EC%B9%99) |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | while๋ฌธ ํธ์ถ ์ด์ ์ randomNumber์ ์ ์๋ฏธํ ๊ฐ์ ๋ฃ๊ธฐ ์ํด์ ๋๋ค๊ฐ์ ์์ฑํ์ฌ ๋ฏธ๋ฆฌ ๋ฃ์ด์ฃผ๊ณ while์ ํธ์ถํ ๊ฒ ๊ฐ๋ค์. ํ์ง๋ง ์ด๋ ๊ฒ ๋๋ฉด randomNumber์ ์ด๊ธฐํ์์ ๋ํด์ ์ฝ๋ ์ค๋ณต์ด ์ผ์ด๋๊ฒ ๋ฉ๋๋ค!
do .. while ๋ฌธ์ ์ฌ์ฉํด์ ์ฝ๋ ์ค๋ณต์ ์์จ ์ ์์ง ์์๊น์?? ๐ |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | newRow๋ HTML ์์์ธ ๊ฒ ๊ฐ์๋ฐ $๋ฅผ ๋ถ์ฌ์ฃผ๋๊ฒ ๋ง์ง ์์๊น์?? ๐ค
์ ๋ ์ด๋ถ๋ถ์ ํ์คํ์ง ์์ผ๋ ์ง์ ๊ณ ๋ฏผํด๋ณด์๋ ๊ฒ ์ข์ ๊ฒ ๊ฐ์์!
[์ฐธ๊ณ ์๋ฃ](https://choseongho93.tistory.com/213) |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ์ด ๋ถ๋ถ๋ค๋ HTML ์์์ธ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ์ฝ๋์ ๊ฐ๊ฒฐํจ์ ์ํด ์ข์ ๋ฐฉ๋ฒ์ด ๋ ์๋ ์์ง๋ง, ํ ํ์์๋ ํ๋์ ๋ณ๊ฒฝ๋ง์ด ์ผ์ด๋์ผ ํ๋ค๋ ๋ง์ ๋ค์ ์ ์ด ์๋ ๊ฒ ๊ฐ์์!
 |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ๊ฒฝ๊ณ ๋ฌธ์ ๋ด์ฉ์ด ๊ฒน์น๋๋ฐ, ๊ฒฝ๊ณ ๋ฌธ์ ํจ์๋ก ๋ถ๋ฆฌํ๊ฑฐ๋ ๊ฒฝ๊ณ ๋ฌธ ๋ด์ฉ์ ๋ฌธ์์ด ๋ฆฌํฐ๋ด๋ก ์ ์ํ๋ ๊ฑด ์ด๋จ๊น์? ๐ |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ๋ณ๊ฒฝํ innerText ๋ด์ฉ์ ๋ณ์๋ฅผ ๊ฐ๋ ๋ฌธ์์ด ๋ฆฌํฐ๋ด๋ก ํํํ ์ ์์ง ์์๊น์?? ๐
[์ฐธ๊ณ ์๋ฃ](https://www.delftstack.com/ko/howto/javascript/javascript-variable-in-string/) |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ```
score[0] -> ball
score[1] -> strike
```
์ธ ๊ฒ ๊ฐ์๋ฐ, ์ด๋ ๊ฒ ๋งค์ง ๋๋ฒ๋ฅผ ํตํด ์์ฑํ๊ฒ ๋๋ฉด ๋ค๋ฅธ ์ฌ๋๋ค์ด ์ฝ๋๋ฅผ ๋ณด์์ ๋ ์ธ๋ฑ์ค ๋ณ ์ญํ ์ ๋ํด ํด์ํ๊ธฐ ํ๋ค ๊ฒ ๊ฐ์์! ์ธ๋ฑ์ค์ธ 0๊ณผ 1์ ๊ฐ๊ฐ ์์๋ก ๋ถ๋ฆฌํด๋ณด๋ ๊ฑด ์ด๋จ๊น์? ๐ |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | 
๊นํ๋ธ์์ `No newline at end of file`๋ผ๋ ์๋ฌ๊ฐ ๋ํ๋๋ค์! ์ด๋ฐ ์๋ฌ๋ ์ ๋ํ๋๋ ๊ฑธ๊น์??
[์ฐธ๊ณ ์๋ฃ](https://velog.io/@jesop/%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EC%BD%94%EB%93%9C-%EB%A7%88%EC%A7%80%EB%A7%89%EC%97%90-%EB%B9%88-%EC%A4%84-%EC%82%BD%EC%9E%85%ED%95%98%EB%8A%94-%EC%9D%B4%EC%9C%A0) |
@@ -0,0 +1,29 @@
+.container{
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ align-items: center;
+ min-height: 100vh;
+}
+
+.btn-group, .input-content, .table-content{
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ margin: 10px;
+}
+
+table{
+ width: 500px;
+ border: 1px solid black;
+}
+
+th, td{
+ border: 1px solid black;
+ text-align: center;
+}
+
+div input{
+ margin-left: 5px;
+ margin-right: 5px;
+}
\ No newline at end of file | Unknown | ์ค ์ด๋ฐ ๋ฐฉ๋ฒ๋ ์๊ตฐ์...!! ๐ฎ |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ๋ต ๊ฐ์ฌํฉ๋๋ค!
๋ฆ๊ฒ ๋ฆฌ๋ทฐ๋ฌ์์ ์ ๋ง ์ฃ์กํฉ๋๋ค... |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ์ํ ์๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ์ฐธ๊ณ ์๋ฃ ์ ์ฝ์ด๋ดค์ต๋๋ค! SRP์ ๋ํด ์ข ๋ ๊ณต๋ถ๊ฐ ํ์ํ ๊ฒ ๊ฐ๋ค์. ์ฐธ๊ณ ์๋ฃ๋ฅผ ์ฝ์ผ๋ฉด์ ์ข ํท๊ฐ๋ ธ๋ ๋ถ๋ถ์ด ์๋๋ฐ "์์ง๋๋ฅผ ๋๊ฒ, ๊ฒฐํฉ๋๋ ๋ฎ๊ฒ" ๋ผ๋ ๋ฌธ์ฅ์์ ์์ง๋์ ๊ฒฐํฉ๋์ ์ฐจ์ด์ ๋ํด ์กฐ๊ธ ํท๊ฐ๋ฆฝ๋๋ค. ์์ง๋๋ ํจ์ ์์ ๊ตฌ์ฑ์์๋ค์ ์ฐ๊ด๋ ์ ๋, ๊ฒฐํฉ๋๋ ์๋ก ๋ค๋ฅธ ํจ์๊ฐ์ ์ฐ๊ด๋ ์ ๋๋ก ํด์ํ๋๋ฐ ์ด๊ฒ ๋ง๋ ํด์์ธ์ง ์๊ณ ์ถ์ต๋๋ค. |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | function getRandomNumber()๋ก ์๋ก์ด ํจ์๋ฅผ ๋ง๋ค์ด์ ์ค๋ณต์ ํผํ๋๋ก ์์ ํ๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ์ฃผ๋ก ๋ง์ ์ฌ๋๋ค์ด ์ฌ์ฉํ๋ ํจ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋์ง ์๋๋ฉด ์ ๊ฐ ํ์๋ก ํ๋ ํจ์๋ฅผ ์จ์ผ ํ๋๊ฐ ๊ถ๊ธํฉ๋๋ค!
์๋ฅผ ๋ค์ด existingNumber๋ ๋ฐฐ์ด์ด๊ธฐ ๋๋ฌธ์ includesํจ์๋ฅผ ์ฌ์ฉํด์ randomNumber๊ฐ ํฌํจ๋์ด ์๋์ง ์ฐพ์๋๋ฐ, ๋ง์ฝ ์ด ํจ์๋ฅผ ๋ค๋ฅธ ์ฌ๋๋ค์ด ์์ฃผ ์ฌ์ฉํ์ง ์๋๋ค๋ฉด ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ฐพ์์ ํ๋ก๊ทธ๋๋ฐ ํด์ผ ํ๋์ง ๋ฌป๊ณ ์ถ์ต๋๋ค. |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | do .. while ๋ฌธ์ ์ฌ์ฉํด์ ์ค๋ณต ์์ ๋ณด๊ฒ ์ต๋๋ค!! do .. while ๋ฌธ์ ์๊ฐ ๋ชปํ๋ค์.. |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | $๋ฅผ ๋ถ์ด๋ ๊ฒ์ด ๋ง๋ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ๊ทธ๋ฌ๋ฉด index๊ฐ์ turn์ ๋ฃ๋ ๊ฒ, index๊ฐ 1์ฉ ์ฆ๊ฐ ํ๋ ๊ฒ ๋ ๊ฐ๋ฅผ ๋๋๋๋ก ์์ ํ๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,40 @@
+// Controller๋ ์ค์ง Service ๋ ์ด์ด์๋ง ์์กดํฉ๋๋ค.
+const { userService } = require('../services');
+const { errorGenerator } = require('../erros');
+
+const signUp = async (req, res, next) => {
+ try {
+ const {
+ email,
+ hashedPassword,
+ phoneNumber,
+ profileImageUrl,
+ introduction,
+ } = req.body;
+
+ /*
+ middleware์์ email, hashedPassword, phoneNumber์
+ ์กด์ฌ์ ๋ํ ์๋ฌ ์ฒ๋ฆฌ ํ๋๋ฐ,
+ ์ฌ๊ธฐ์๋ ๋ ์๋ฌ ์ฒ๋ฆฌ๋ฅผ ํด์ผํ ๊น์?
+ */
+
+ const createdUser = await userService.createUser({
+ email,
+ password: hashedPassword,
+ phoneNumber,
+ profileImageUrl,
+ introduction,
+ });
+
+ res.status(201).json({
+ message: 'user created',
+ userId: createdUser.id,
+ });
+ } catch (err) {
+ next(err);
+ }
+};
+
+module.exports = {
+ signUp,
+}; | JavaScript | ๋ณ์ ์ ์ธํ ๋๋ `scope` ๊ด๋ฆฌ๋ฅผ ์ํด์ ํญ์ `const`, `let` ๋ฑ์ ํค์๋๋ฅผ ์ฌ์ฉํด์ฃผ๋๊ฒ ์ข์ต๋๋ค! |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.