Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
구현해야할 목록

자동차 이름과 시도횟수 입력

레이싱 진행 출력

우승자 출력
117 changes: 117 additions & 0 deletions src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,124 @@
package racingcar;
import java.util.*;
import camp.nextstep.edu.missionutils.Console;
import camp.nextstep.edu.missionutils.Randoms;

public class Application {

public static void main(String[] args) {
// TODO: 프로그램 구현
ArrayList<Car> Cars = inputCars();
int round = inputRound();
Cars = rounding(round, Cars);
Cars = findWinner(Cars);
award(Cars);
}

private static boolean nameAgain(ArrayList<Car> Cars){
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  1. private static 메소드는 무슨 특징을 지닐까요?
  2. 이렇게 지정한 이유가 있으신가요?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

해당 클래스 안에서만 호출이 가능하고 객체 없이 호출이 가능한 특징이 있습니다. 그리고 여기 클래스 안에서만 사용하고 따로 객체 없이 기능을 하는 메소드라는 특징이 있습니다. 따로 이유는 없이 유틸 메소드를 만들려고 static으로 지정했습니다.

Set<String> nameSet = new HashSet<>();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Set 자료구조의 특징은 무엇일까요?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

순서가 없어 중복을 허용하지 않습니다.

for(int i = 0; i < Cars.size() ; i++) {
if (!nameSet.add(Cars.get(i).getName())) {
return true;
}
}
return false;
}

private static ArrayList<Car> inputCars(){
ArrayList<Car> Cars = new ArrayList<>();
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
String input = Console.readLine();
String[] names = input.split(",");

for(int i = 0; i < names.length; i++){
if(names[i].length() > 5 || names[i].contains(" ") || names[i].isEmpty()){
throw new IllegalArgumentException();
} else {
Cars.add(new Car(names[i]));
}
Comment on lines +36 to +38
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

별 차이는 없지만, if 문에서 조건이 걸린다면 어차피 예외를 발생해서 프로그램을 종료하므로 else 문은 명시하지 않아도 될 것 같아요!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

더욱 깔끔하게 되네요. 참고하겠습니다.

}
if(nameAgain(Cars)){
throw new IllegalArgumentException();
}
return Cars;
}

private static int inputRound(){
System.out.println("시도할 회수는 몇회인가요?");
int round = 0;
try {
round = Integer.parseInt(Console.readLine().trim());
} catch(NumberFormatException e){
throw new IllegalArgumentException();
}
if(round <= 0){
throw new IllegalArgumentException();
}
System.out.println(" ");
return round;
}

private static ArrayList<Car> racing(ArrayList<Car> Cars){
int random = 0;
for (int i = 0; i < Cars.size(); i++){
random = Randoms.pickNumberInRange(0, 9);
if(random >= 4){
Cars.get(i).move();
}
}
return Cars;
}

private static ArrayList<Car> rounding(int round, ArrayList<Car> Cars){
System.out.println("실행 결과");
for (int i = 0; i < round; i++){
racing(Cars);
chart(Cars);
System.out.println(" ");
}
return Cars;
}

private static void chart(ArrayList<Car> Cars){
for(int i = 0; i < Cars.size(); i++){
System.out.print(Cars.get(i).getName() + " : ");
for(int j = 0; j < Cars.get(i).getScore(); j++){
System.out.print("-");
}
System.out.println(" ");
}
}

private static ArrayList<Car> findWinner(ArrayList<Car> Cars){
int maxScore = 0;

for(int i = 0; i < Cars.size(); i++){
if(Cars.get(i).getScore() >= maxScore){
maxScore = Cars.get(i).getScore();
}
}

for(int i = 0; i < Cars.size(); i++){
if(Cars.get(i).getScore() == maxScore){
Cars.get(i).win();
}
}

return Cars;
}

private static void award(ArrayList<Car> Cars) {
boolean flag = true;
System.out.print("최종 우승자 : ");
for (int i = 0; i < Cars.size(); i++){
if(Cars.get(i).isWinner() && !flag){
System.out.print(", ");
System.out.print(Cars.get(i).getName());
}
if(Cars.get(i).isWinner() && flag) {
System.out.print(Cars.get(i).getName());
flag = false;
}
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package racingcar;

public class Car {
private String name;
private int score = 0;
private boolean winner = false;

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

public String getName(){
return this.name;
}

public int getScore(){
return this.score;
}

public boolean isWinner(){
if(this.winner) {
return true;
}
return false;
}

public void move() {
this.score++;
}

public void win(){
this.winner = true;
}
}