-
Notifications
You must be signed in to change notification settings - Fork 99
[그리디] 이고은 자동차 경주 미션 1,2 단계 제출합니다. #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: ke-62
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# 자동차 경주 | ||
|
||
## 설명 | ||
1. 자동차 이름 입력받기 | ||
2. 4이상-> 전진, 4미만->멈춤 | ||
|
||
## 조건 | ||
1. indent(인덴트, 들여쓰기) depth를 2를 넘지 않도록 | ||
2. 3항 연산자, else, switch/case 모두 금지 | ||
3. 함수(메서드) 길이 15라인 넘지 않도록 | ||
4. 함수가 한 가지 일만 할 수 있게 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
public class Car { | ||
private static final int MOVE_THRESHOLD = 4; | ||
|
||
private String name; | ||
private int position=0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코드스타일은 아래 단축키를 통해 한 번에 정렬 가능합니다!
|
||
|
||
public Car(String name){ | ||
this.name=name; | ||
} //자동차 이름 규칙 강제화 | ||
|
||
public String getName(){ | ||
return name; | ||
} | ||
|
||
public int getPosition(){ | ||
return position; | ||
} | ||
|
||
public void move(int randomNumber){ | ||
if(randomNumber>=MOVE_THRESHOLD){ | ||
position++; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import java.util.List; | ||
|
||
public class Cars { | ||
private final List<Car> cars; | ||
|
||
public Cars(List<Car> carList) { | ||
this.cars = carList; | ||
} | ||
|
||
public StringBuilder findWinnerNames() { | ||
int maxPosition = findMaxPosition(); | ||
return buildWinnerNames(maxPosition); | ||
} | ||
|
||
private int findMaxPosition() { | ||
int maxPosition = 0; | ||
for (Car car : cars) { | ||
maxPosition = Math.max(maxPosition, car.getPosition()); | ||
} | ||
return maxPosition; | ||
} | ||
|
||
private StringBuilder buildWinnerNames(int maxPosition) { | ||
StringBuilder winnerNames = new StringBuilder(); | ||
for (Car car : cars) { | ||
appendWinnerName(car, maxPosition, winnerNames); | ||
} | ||
return winnerNames; | ||
} | ||
|
||
private void appendWinnerName(Car car, int maxPosition, StringBuilder winnerNames) { | ||
if (car.getPosition() == maxPosition) { | ||
winnerNames.append(car.getName()); | ||
} | ||
} | ||
|
||
public Car[] getCars() { | ||
return cars.toArray(new Car[0]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import java.util.Random; | ||
|
||
public class MoveCondition { | ||
|
||
public void randomMove(Car car){ | ||
Random random = new Random(); | ||
|
||
int randomNumber = random.nextInt(10); // 0부터 9까지의 랜덤 숫자 생성 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 10도 매직넘버 상수화를 적용시켜보면 어떨까요? |
||
System.out.println("랜덤변수 : " + randomNumber); | ||
car.move(randomNumber); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 리뷰를 반영한 흔적이 보이네요 :) 저는 완전한 분리(car.move 호출을 다른 곳에서)를 제안하고 싶은데요, 이유는 다음과 같습니다. 추가로 이 randomMove 메서드는 각각의 자동차가 움직일 때마다 호출되는 메서드 입니다! 고은님의 생각을 들려주시면 감사하겠습니다! |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
public class RaceController { | ||
public void startRace(Cars car, int rounds) { | ||
MoveCondition moveCondition = new MoveCondition(); | ||
|
||
for (int round = 0; round < rounds; round++) { | ||
runRound(car, moveCondition); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
private void runRound(Cars cars, MoveCondition moveCondition) { | ||
for (Car car : cars.getCars()) { | ||
moveCondition.randomMove(car); | ||
} | ||
|
||
for (Car car : cars.getCars()) { | ||
String dashes = "-".repeat(car.getPosition()); | ||
System.out.println(car.getName() + " : " + dashes); | ||
} | ||
System.out.println(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import java.util.Scanner; | ||
import java.util.Arrays; | ||
|
||
public class RaceStart { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
int rounds = scanner.nextInt(); //라운드 횟수 입력 | ||
int n = scanner.nextInt(); // 차 갯수 입력 | ||
Car[] cars = new Car[n]; | ||
|
||
for (int i = 0; i < n; i++) { | ||
String name = scanner.next(); | ||
cars[i] = new Car(name); | ||
} | ||
|
||
Cars carsList = new Cars(Arrays.asList(cars)); | ||
|
||
RaceController raceController = new RaceController(); | ||
raceController.startRace(carsList, rounds); | ||
|
||
Winner winner = new Winner(); | ||
winner.findWinner(carsList); | ||
|
||
scanner.close(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
public class Winner { | ||
|
||
public void findWinner(Cars cars) { | ||
StringBuilder result = cars.findWinnerNames(); | ||
System.out.println(result + "가 최종 우승했습니다."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class TestRace { | ||
|
||
@Test | ||
@DisplayName("4이상 일때만 전진하는지 확인") | ||
void CarMove(){ | ||
//given | ||
Car f1 = new Car("f1"); | ||
//when | ||
f1.move(4); | ||
//then | ||
assertThat(f1.getPosition()).isEqualTo(1); | ||
} | ||
|
||
void CarStop(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CarStop 메서드에 @test 어노테이션이 빠져있어서 실제로는 테스트가 실행되지 않을 것 같아요. 현재는 자동차의 이동에 대한 부분만 테스트가 작성되어 있는 것 같습니다! 시간이 되신다면 테스트를 더 다양하게 만들어 보시는 것은 어떨까요? 테스트 커버리지를 확인하는 방법 |
||
//given | ||
Car f1 = new Car("f1"); | ||
//when | ||
f1.move(3); | ||
//then | ||
assertThat(f1.getPosition()).isEqualTo(0); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
쓰지 않는 파일은 삭제해주셔도 괜찮습니다!