Skip to content
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1613bb7
docs: update manual.md
Jaehooni Mar 14, 2023
494a07d
feat: move function
Jaehooni Mar 14, 2023
71039f0
docs: add new manual
Jaehooni Mar 14, 2023
0c861ab
feat: add game method
Jaehooni Mar 14, 2023
64e0e9f
feat: add make method
Jaehooni Mar 14, 2023
dfe3b19
docs: delete manual item
Jaehooni Mar 14, 2023
79e996e
fix: typo
Jaehooni Mar 14, 2023
e10a9cc
feat: add new func to show output
Jaehooni Mar 15, 2023
9b3f2c8
feat: add new error find features
Jaehooni Mar 15, 2023
4178bf0
docs: simpify fix sentence
Jaehooni Mar 15, 2023
f10bbe7
docs: simplify fix sentence
Jaehooni Mar 15, 2023
7635771
refactor: move input feature to InputCheck.java
Jaehooni Mar 15, 2023
78a914e
feat: make execute code
Jaehooni Mar 15, 2023
7a8542e
refactor: make project convention style same as naver java convention
Jaehooni Mar 15, 2023
7254c16
refactor(replace declaration type):
Jaehooni Mar 21, 2023
f73573a
refactor: Separate print func from Car.java
Jaehooni Mar 21, 2023
6330ace
refactor: Separate func from Car.java
Jaehooni Mar 21, 2023
6e6c23b
refactor: just fix some codes
Jaehooni Mar 21, 2023
1ba30dd
feat: make new instance parameter
Jaehooni Mar 21, 2023
88db2a1
style: change parameter name
Jaehooni Mar 21, 2023
bfa1c4e
style: change parameter name
Jaehooni Mar 24, 2023
d90073c
refactor: separate sort function from findMaxValue(...)
Jaehooni Mar 24, 2023
5a31b44
refactor: add new function to start()
Jaehooni Mar 24, 2023
aa47529
refactor: simple fix
Jaehooni Mar 24, 2023
f5c5154
refactor: move files to utility directory
Jaehooni Mar 25, 2023
b00e2b0
refactor: move files to utility directory
Jaehooni Mar 25, 2023
3de28a2
refactor: move sort func to Sort.java
Jaehooni Mar 25, 2023
4a7544c
feat: overriding toString()
Jaehooni Mar 25, 2023
582d57f
refactor: move sort func to Sort.java
Jaehooni Mar 25, 2023
7909af2
feat: white space
Jaehooni Apr 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package racingcar;

import java.util.ArrayList;

public class Application {
public static void main(String[] args) {
// TODO 구현 진행
}
public static void main(String[] args) {
ArrayList<Car> cars = Car.make();
Car.game(cars);
Car.printResult(cars);
}
}
66 changes: 60 additions & 6 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,66 @@
package racingcar;

import java.util.ArrayList;

import camp.nextstep.edu.missionutils.Randoms;

public class Car {
private final String name;
private int position = 0;
private final String name;
private int position = 0;

public Car(String name) {
this.name = name;
}

public int getPosition() {
return this.position;
}

// 추가 기능 구현
public void move() {
if (Randoms.pickNumberInRange(0, 9) >= 4) {
this.position += 1;
}
}

public static void game(ArrayList<Car> cars) {
int num = InputCheck.inputRound();
for (int i = 0; i < num; i++) {
System.out.println("실행 결과");
for (Car car : cars) {
car.move();
car.printProgress();
}
System.out.println();
}
}

public static ArrayList<Car> make() {
ArrayList<String> names = InputCheck.inputCarNames();
ArrayList<Car> cars = new ArrayList<>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAVA 에서는 List와 같은 Interface 타입을 변수의 타입으로 사용하길 권장하고 있습니다. 한번 찾아보시면 좋을것 같아요!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다! 아직 자바 코드 작성 경험이 적어 생기는 문제점인 것 같습니다 코드 수정 과정에서 수정하겠습니다 :)

for (String name : names) {
cars.add(new Car(name));
}
return cars;
}

public void printProgress() {
String distance = new String(new char[this.position]).replace("\0", "-");
System.out.printf("%s : %s%n", this.name, distance);
}

public static void printResult(ArrayList<Car> cars) {
String winner = "";
int winnerScore = -1;

public Car(String name) {
this.name = name;
}
cars.sort((x, y) -> y.getPosition() - x.getPosition());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

빠른 순서대로 정렬! 이라는 아이디어는 재밌는것 같습니다!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다! :)

for (Car car : cars) {
if (car.getPosition() >= winnerScore) {
winner += car.name + ", ";
winnerScore = car.position;
}
}

// 추가 기능 구현
System.out.printf("최종 우승자 : %s", winner.substring(0, winner.length() - 2));
}
}
53 changes: 53 additions & 0 deletions src/main/java/racingcar/InputCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package racingcar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Pattern;

import camp.nextstep.edu.missionutils.Console;

public class InputCheck {
public InputCheck() {
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 메소드는 불필요해 보입니다. 객체 생성을 방지하고 싶으시면 private으로 선언해 보시는건 어떠신가요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

삭제 완료하였습니다!


public static ArrayList<String> inputCarNames() {
while (true) {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉽표(,) 기준으로 구분)");
ArrayList<String> names = new ArrayList<>(Arrays.asList(Console.readLine().split(",")));
try {
iterateCars(names);
return names;
} catch (IllegalArgumentException e) {
System.out.println("[ERROR] 이름은 5자 이하만 가능하다.");
}
}
}

public static void iterateCars(ArrayList<String> names) {
for (String name : names) {
if (name.length() > 5) {
throw new IllegalArgumentException();
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception을 던지실때 Error Message도 넣어서 던지실 수가 있어요! 에러 메세지를 넣어서 던지는건 어떻게 생각하시나요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영했습니다!


public static int inputRound() {
while (true) {
System.out.println("시도할 회수는 몇회인가요?");
try {
String input = Console.readLine();
match(input);
return Integer.parseInt(input);
} catch (IllegalArgumentException e) {
System.out.println("[ERROR] 시도 횟수는 자연수인 숫자여야 한다.");
}
}
}

public static void match(String input) {
String pattern = "^[0-9]+$";
if (!Pattern.matches(pattern, input)) {
throw new IllegalArgumentException();
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/racingcar/manual.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
자동차 경주 게임 기능 구현
======================

🗒️기능 목록
--------
### 1. 자동차가 주어진 Random 값에 따라 전진 혹은 멈추는 기능
### 2. 입력받은 이동 횟수만큼 1의 함수를 실행시키는 기능
### 3. 입력이 잘못되었을때 에러를 발생시키고 다시 입력받게 하는 기능
### 4. 입력으로 주어진 이름을 기반으로 Car Object를 생성하는 기능
### 5. 결과를 출력하는 기능