2022 / 02.08~02.18
λ―Έμ - μλμ°¨ κ²½μ£Ό κ²μ
π κΈ°λ₯ μꡬμ¬ν
- μ£Όμ΄μ§ νμ λμ nλμ μλμ°¨λ μ μ§ λλ λ©μΆ μ μλ€.
- κ° μλμ°¨μ μ΄λ¦μ λΆμ¬ν μ μλ€. μ μ§νλ μλμ°¨λ₯Ό μΆλ ₯ν λ μλμ°¨ μ΄λ¦μ κ°μ΄ μΆλ ₯νλ€.
- μλμ°¨ μ΄λ¦μ μΌν(,)λ₯Ό κΈ°μ€μΌλ‘ ꡬλΆνλ©° μ΄λ¦μ 5μ μ΄νλ§ κ°λ₯νλ€.
- μ¬μ©μλ λͺ λ²μ μ΄λμ ν κ²μΈμ§λ₯Ό μ λ ₯ν μ μμ΄μΌ νλ€.
- μ μ§νλ 쑰건μ 0μμ 9 μ¬μ΄μμ random κ°μ ꡬν ν random κ°μ΄ 4 μ΄μμΌ κ²½μ° μ μ§νκ³ , 3 μ΄νμ κ°μ΄λ©΄ λ©μΆλ€.
- μλμ°¨ κ²½μ£Ό κ²μμ μλ£ν ν λκ° μ°μΉνλμ§λ₯Ό μλ €μ€λ€. μ°μΉμλ ν λͺ μ΄μμΌ μ μλ€.
βπ» μ μΆλ ₯ μꡬμ¬ν
β¨οΈ μ λ ₯
- κ²½μ£Ό ν μλμ°¨ μ΄λ¦(μ΄λ¦μ μΌν(,) κΈ°μ€μΌλ‘ ꡬλΆ)
pobi,woni,jun
- μλν νμ
5
π₯ μΆλ ₯
- κ° μ°¨μλ³ μ€ν κ²°κ³Ό
pobi : --
woni : ----
jun : ---
- λ¨λ μ°μΉμ μλ΄ λ¬Έκ΅¬
μ΅μ’
μ°μΉμ: pobi
- 곡λ μ°μΉμ μλ΄ λ¬Έκ΅¬
μ΅μ’
μ°μΉμ: pobi, jun
- μμΈ μν© μ μλ¬ λ¬Έκ΅¬λ₯Ό μΆλ ₯ν΄μΌ νλ€. λ¨, μλ¬ λ¬Έκ΅¬λ [ERROR] λ‘ μμν΄μΌ νλ€.
[ERROR] μλ νμλ μ«μμ¬μΌ νλ€.
###
- static λ³μλ non-static λ³μλ³΄λ€ μμ μ μΈ
- λ©μλλͺ μ λμ¬ or λμ¬+λͺ μ¬
- λ©μλλͺ or return νμ μ£Όμ
λ©μλλͺ μ΄ get~ μ΄λΌλ©΄ return νμ μ‘΄μ¬ν΄μΌν¨
(+ λ©μλλͺ μ΄ is~ λΌλ©΄ return νμ μ boolean)
- indexκ° νμνμ§ μμ κ²½μ° ν₯μλ forλ¬Έ μ¬μ©
### code ###
package racingCar;
public class Application {
public static void main(String[] args) {
RacingCarGame racingCarGame = new RacingCarGame();
racingCarGame.play();
}
}
package racingCar;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
class RacingCarGame {
private static final int NAME_LENGTH_STANDARD = 5;
private static final int NOT_A_NUMBER = -1;
final Scanner scanner = new Scanner(System.in);
public void play() {
String[] names = inputNames();
int tryNumber = inputTryNumber();
List<Car> cars = createCars(names);
System.out.println("μ€ν κ²°κ³Ό");
for (int i = 0; i < tryNumber; i++)
printRaceResult(cars);
System.out.print("μ΅μ’
μ°μΉμ: ");
List<String> winners = findWinner(cars);
printWinner(winners);
}
public int inputTryNumber() {
int tryNumber = NOT_A_NUMBER;
while (!isNumber(tryNumber)) {
try {
System.out.println("μλν νμλ λͺνμΈκ°μ?");
tryNumber = scanner.nextInt();
} catch (InputMismatchException ime) {
System.out.println("[ERROR] μλ νμλ μ«μμ¬μΌ νλ€.");
tryNumber = NOT_A_NUMBER;
scanner.next();
}
}
return tryNumber;
}
public boolean isNumber(int tryNumber) {
return tryNumber != NOT_A_NUMBER;
}
public String[] splitNames(String name) {
return name.split(",");
}
public boolean checkNameLength(String[] names) {
for (String name : names) {
if (name.length() > NAME_LENGTH_STANDARD) {
System.out.println("[ERROR] 5μ μ΄νμ μ΄λ¦μΌλ‘ μ
λ ₯νμΈμ.");
return false;
}
}
return true;
}
private String[] inputNames() {
boolean check;
String[] names;
System.out.println("κ²½μ£Όν μλμ°¨ μ΄λ¦μ μ
λ ₯νμΈμ. (μ΄λ¦μ μΌν(,) κΈ°μ€μΌλ‘ ꡬλΆ)");
do {
String name = scanner.next();
names = splitNames(name);
check = checkNameLength(names);
} while (!check);
return names;
}
public List<Car> createCars(String[] names) {
List<Car> cars = new ArrayList<>();
for (String name : names) {
Car car = new Car(name);
cars.add(car);
}
return cars;
}
private void printRaceResult(List<Car> cars) {
for (int i = 0; i < cars.size(); i++) {
Car car = cars.get(i);
int count = car.getMoveCount();
car.printMove(count);
System.out.println("");
}
System.out.println("");
}
public int getMaxPositionIdx(List<Car> cars, Car car, int maxPosition) {
int maxPositionIdx = 0;
for (int i = 0; i < cars.size(); i++) {
car = cars.get(i);
int position = car.getPosition();
if (maxPosition < position) {
maxPosition = position;
maxPositionIdx = i;
}
}
return maxPositionIdx;
}
public List<Integer> getCoWinnerIdx(List<Car> cars, int maxPositionIdx) {
List<Integer> coWinnerIdx = new ArrayList<Integer>();
Car car = cars.get(maxPositionIdx);
int maxPosition = car.getPosition();
for (int i = maxPositionIdx + 1; i < cars.size(); i++) {
car = cars.get(i);
if (maxPosition == car.getPosition()) {
coWinnerIdx.add(i);
}
}
return coWinnerIdx;
}
private void addWinner(List<Car> cars, List<String> winners, int maxPositionIdx) {
Car car = cars.get(maxPositionIdx);
String winnerName = car.getName();
winners.add(winnerName);
}
private List<String> findWinner(List<Car> cars) {
List<String> winners = new ArrayList<>();
Car initialCar = cars.get(0);
int initialPosition = initialCar.getPosition();
int maxPositionIdx = getMaxPositionIdx(cars, initialCar, initialPosition);
addWinner(cars, winners, maxPositionIdx);
List<Integer> coWinnerIndex = getCoWinnerIdx(cars, maxPositionIdx);
for (int coWinnerIdx : coWinnerIndex)
addWinner(cars, winners, coWinnerIdx);
return winners;
}
public String printWinner(List<String> winners) {
String winner = String.join(", ", winners);
System.out.println(winner);
return winner;
}
}
package racingCar;
import utils.RandomUtils;
public class Car {
private static final int START_INCLUSIVE = 0;
private static final int END_INCLUSIVE = 9;
private static final int MOVING_STANDARD = 4;
private final String name;
private int position = 0;
public Car(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
private int getNumber() {
return RandomUtils.nextInt(START_INCLUSIVE, END_INCLUSIVE);
}
public int getPosition() {
return this.position;
}
public void moveOrStop(int number) {
if (number >= MOVING_STANDARD)
this.position++;
}
public int getMoveCount(){
int number = getNumber();
moveOrStop(number);
int moveCount = this.getPosition();
return moveCount;
}
public String printMove(int count) {
String move = "";
char racePoint = '-';
System.out.print(this.name + ":");
for (int i = 0; i < count; i++) {
move += racePoint;
System.out.print("-");
}
return move;
}
}β
package utils;
import java.util.Random;
public class RandomUtils {
private static final Random RANDOM = new Random();
private RandomUtils() {
}
public static int nextInt(final int startInclusive, final int endInclusive) {
if (startInclusive > endInclusive) {
throw new IllegalArgumentException();
}
if (startInclusive < 0) {
throw new IllegalArgumentException();
}
if (startInclusive == endInclusive) {
return startInclusive;
}
return startInclusive + RANDOM.nextInt(endInclusive - startInclusive + 1);
}
}
'π» > Java CleanCode Study' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[ERROR] - Could not find or load main class worker.org.gradle.process.internal.worker.GradleWorkerMain (0) | 2022.02.26 |
---|---|
[JAVA] test code μμ±λ² (0) | 2022.02.26 |
π¨ λΉμ¦λμ€ λ‘μ§μ΄λ? : Business Logic (0) | 2022.02.21 |
πͺ MVC ν¨ν΄μ΄λ? : Model, View, Controller (2) | 2022.02.21 |
2022 CleanCode Study (0) | 2022.02.19 |