-
Notifications
You must be signed in to change notification settings - Fork 93
[김민지] 자동차경주 1~2단계 제출합니다 #122
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: mmm307955
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,23 @@ | ||
public class Car { | ||
private final String carName; | ||
private int position = 0; | ||
|
||
public Car(String name){ | ||
this.carName = name; | ||
} | ||
|
||
public void move(Movable movable){ | ||
if(movable.canMove()){ | ||
position++; | ||
} | ||
} | ||
Comment on lines
+9
to
+13
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 String getCarName(){ | ||
return carName; | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//움직일 수 있나? 만 보는 인터페이스 | ||
public interface Movable { | ||
boolean canMove(); | ||
} | ||
Comment on lines
+1
to
+4
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. 인터페이스 이름 규약을 잘 지켜주셨네요! 👍👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
public class RandomMove implements Movable { | ||
//값의 의미나 목적을 코드에서 명확하게 알리고 가독성을 위한 상수 사용 | ||
//4는 전진 조건, 10은 랜덤 범위를 의미. 추후 변경 시 상수만 수정하면 됨 | ||
private static final int MOVE_THRESHOLD = 4; | ||
private static final int RANDOM_BOUND = 10; | ||
Comment on lines
+1
to
+5
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.
만약 해당 클래스의 메서드에서 반환하는 값이 true이면 전진, false면 정지로 판단할 수 있을 것 같아요. 여기서 미션의 요구사항을 조금 더 분석해보면 중요해 보이는 것은 해당 요구사항은 반드시 모든 자동차(전기자동차라는 새로운 클래스가 생기더라도)가 이를 지켜야해요. 그렇다면 이 요구사항 지킨 로직은 어디에 존재해야 하는 것이 맞을까요? 새로운 |
||
|
||
@Override | ||
public boolean canMove(){ | ||
//nextInt(10)은 0~9까지의 랜덤한 값을 반환. 그 값이 4보다 크거나 같으면 true 반환 | ||
return (int)(Math.random() * RANDOM_BOUND) >= MOVE_THRESHOLD; | ||
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. 랜덤한 값을 구하기 위해
|
||
} | ||
} | ||
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.
해당 아이콘은 파일의 가장 끝에 개행이 없어서 발생하는데, 개행이 없어도 코드 실행에는 문제가 없으나, 다른곳에서는 문제가 발생할 수 있어요.
이를 해결하기 위해 매 파일마다 확인하기는 너무 불편하니, IntelliJ를 사용하면 간단한 방법으로 막을 수 있어요.
그리고 next-step/java-calculator-unit-playground#79 (comment) 해당 리뷰 참고하셔서 설정하면 앞으로 해당 이슈를 방지할 수 있을 것 같네요! 또한 해당 경고는 Github가 아닌, git에서도 발생하는데, 다음과 같이 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Winners { | ||
public static List<String> findWinnersNames(List<Car> cars){ | ||
List<String> winners = new ArrayList<>(); | ||
for(int i = 0; i < cars.size(); i++){ | ||
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. for문을 사용해주셨네요! 그리고 반복문은 for문 말고 적어도 3가지 방법을 더 사용할 수 있어요! |
||
if(cars.get(i).getPosition() >= 5){ | ||
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. 승자는 |
||
winners.add(cars.get(i).getCarName()); | ||
} | ||
} | ||
return winners; | ||
} | ||
} | ||
Comment on lines
+4
to
+14
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 리스트를 받은 뒤, Car 중 position이 5 이상인 Car의 이름을 우승 자동차로 판단하고, 이를 반환하는 클래스네요! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class WinnersTest { | ||
@Test | ||
@DisplayName("우승자(들) 이름을 반환한다") | ||
void returnWinnerNames(){ | ||
Car carA = new Car("Car A"); | ||
Car carB = new Car("Car B"); | ||
Car carC = new Car("Car C"); | ||
|
||
for(int i = 0; i < 5; i++){ | ||
carA.move(()->true); | ||
} | ||
|
||
for(int i = 0; i < 4; i++){ | ||
carB.move(()->true); | ||
} | ||
for(int i = 0; i < 5; i++){ | ||
carC.move(()->true); | ||
} | ||
Comment on lines
+12
to
+25
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. 우승자를 구하기 위해 매번 반복문과 |
||
|
||
List<Car> cars = List.of(carA, carB, carC); | ||
assertThat(Winners.findWinnersNames(cars)).containsExactlyInAnyOrder("Car A","Car C"); | ||
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. AssertJ 사용을 잘 해주셨군요! 👍👍 |
||
} | ||
} |
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.
final
키워드를 사용해주셨네요!왜 final 키워드를 사용하셨나요? final 키워드를 사용하면 어떤 장점이 있나요?