λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

πŸ’»/Java CleanCode Study

πŸš— μžλ™μ°¨ κ²½μ£Ό κ²Œμž„ #2022.02.08~2022.02.18

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)

ex

- 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);
    }
}