-
Notifications
You must be signed in to change notification settings - Fork 440
[자동자 경주 게임] 이준섭 미션 제출합니다. #533
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: main
Are you sure you want to change the base?
Changes from all commits
4c9b5bd
1dd0320
778bd42
7e2027e
fc32731
8d54909
3d5d1ff
06548c0
5376620
cfa16f2
4a3b5bc
b696c3d
76d0c39
5839d31
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 | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,99 @@ | ||||||
package racingcar; | ||||||
|
||||||
import camp.nextstep.edu.missionutils.Console; | ||||||
|
||||||
import java.util.ArrayList; | ||||||
import java.util.Arrays; | ||||||
import java.util.List; | ||||||
|
||||||
public class Application { | ||||||
public static void main(String[] args) { | ||||||
// TODO 구현 진행 | ||||||
List<Car> cars = createCars(); | ||||||
|
||||||
int tryCount = receiveTryCount(); | ||||||
|
||||||
runRacingGame(cars, tryCount); | ||||||
|
||||||
printWinners(findWinners(cars)); | ||||||
} | ||||||
|
||||||
private static List<Car> createCars() { | ||||||
List<String> carNames = null; | ||||||
List<Car> cars = new ArrayList<>(); | ||||||
while (carNames == null) { | ||||||
System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)"); | ||||||
String input = Console.readLine(); | ||||||
try { | ||||||
carNames = Arrays.asList(input.split(",")); | ||||||
carNames.forEach(inputName -> cars.add(new Car(inputName))); // carNames 리스트의 각 요소를 inputName으로 받아서 객체를 생성하고 cars 리스트에 추가함. | ||||||
break; | ||||||
} catch (IllegalArgumentException e) { | ||||||
System.out.println("[ERROR] 올바른 형식의 자동차 이름을 입력하세요."); | ||||||
} | ||||||
} | ||||||
return cars; | ||||||
} | ||||||
|
||||||
private static int receiveTryCount() { | ||||||
int tryCount = 0; | ||||||
while (tryCount == 0) { | ||||||
System.out.println("시도할 횟수는 몇 회인가요?"); | ||||||
String input = Console.readLine(); | ||||||
try { | ||||||
tryCount = Integer.parseInt(input); // 문자열을 정수로 변환하는 문법 | ||||||
validateTryCount(tryCount); | ||||||
break; | ||||||
} | ||||||
catch (IllegalArgumentException e) { | ||||||
System.out.println("[ERROR] 올바른 형식의 숫자를 입력하세요."); | ||||||
} | ||||||
} | ||||||
return tryCount; | ||||||
} | ||||||
|
||||||
private static void validateTryCount(int tryCount) { | ||||||
if (tryCount <= 0) { | ||||||
throw new IllegalArgumentException(); | ||||||
} | ||||||
} | ||||||
|
||||||
private static void runRacingGame(List<Car> cars, int tryCount) { | ||||||
System.out.println("\n실행 결과"); | ||||||
for (int i = 0; i < tryCount; i++) { | ||||||
for (Car car : cars) { | ||||||
car.moveForward(); | ||||||
} | ||||||
printRoundResult(cars); | ||||||
} | ||||||
} | ||||||
|
||||||
private static void printRoundResult(List<Car> cars) { | ||||||
for (Car car : cars) { | ||||||
System.out.println(car.getName() + " : " + new String(new char[car.getPosition()]).replace('\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.
Suggested change
Car 클래스의 name 필드 접근 제어자를 private에서 public 으로 변경하면 다음과 같이 코드를 수정할 수 있습니다. 왜 필드를 private으로 사용해야할까요? 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. 왜 외부에서 직접 접근하면 안될까요? 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. 외부에서 직접 접근하는 메서드가 많아진다면 추후 클래스의 구성이나 구현을 변경할 때 수정이 어려워지기 때문입니다! |
||||||
} | ||||||
System.out.println(); | ||||||
} | ||||||
|
||||||
private static List<String> findWinners(List<Car> cars) { | ||||||
int maxPosition = getMaxPosition(cars); | ||||||
List<String> winners = new ArrayList<>(); | ||||||
for (Car car : cars) { | ||||||
if (car.getPosition() == maxPosition) { | ||||||
winners.add(car.getName()); | ||||||
} | ||||||
} | ||||||
return winners; | ||||||
} | ||||||
|
||||||
private static int getMaxPosition(List<Car> cars) { | ||||||
int maxPosition = 0; | ||||||
for (Car car : cars) { | ||||||
maxPosition = Math.max(maxPosition, car.getPosition()); | ||||||
} | ||||||
return maxPosition; | ||||||
} | ||||||
|
||||||
private static void printWinners(List<String> winners) { | ||||||
System.out.println("최종 우승자 : " + String.join(", ", winners)); | ||||||
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. String.join은 문자열을 결합할 때 사용합니다. |
||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,12 +1,36 @@ | ||||||||||||||||
package racingcar; | ||||||||||||||||
|
||||||||||||||||
import camp.nextstep.edu.missionutils.Randoms; | ||||||||||||||||
|
||||||||||||||||
public class Car { | ||||||||||||||||
static final int MAX_NAME_LENGTH = 5; | ||||||||||||||||
private final String name; | ||||||||||||||||
private int position = 0; | ||||||||||||||||
private int position; | ||||||||||||||||
|
||||||||||||||||
public Car(String inputName) { | ||||||||||||||||
validateCarNames(inputName); | ||||||||||||||||
this.name = inputName; | ||||||||||||||||
this.position = 0; | ||||||||||||||||
} | ||||||||||||||||
Comment on lines
+10
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. 좋습니다. 이렇게 했을 때 장점이 뭘까요!? 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 void moveForward() { | ||||||||||||||||
int randomValue = Randoms.pickNumberInRange(0,9); | ||||||||||||||||
if (randomValue >= 4) { | ||||||||||||||||
position++; | ||||||||||||||||
} | ||||||||||||||||
Comment on lines
+17
to
+20
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. 이렇게 바꾸기 vs 그냥 쓰기
Suggested change
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. 바꿨을 때의 장단점 장점: randomValue 변수를 사용한다면 코드를 읽기가 더 쉽습니다. 다른 부분에서 동일한 난수를 사용한다면 재사용에 용이합니다. 또한 난수 생성 로직이 변경되더라도 해당 부분만 수정하면 됩니다. 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. 맞습니다! 재사용할 수 있다는 것에 초점을 두고 이런 답변을 드렸네요! 현재 코드에서 성능은 동일합니다! |
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
public String getName() { | ||||||||||||||||
return name; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
public Car(String name) { | ||||||||||||||||
this.name = name; | ||||||||||||||||
public int getPosition() { | ||||||||||||||||
return position; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// 추가 기능 구현 | ||||||||||||||||
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. 필요 없는 주석은 삭제! |
||||||||||||||||
private void validateCarNames(String inputName) { | ||||||||||||||||
if (inputName.isEmpty() || inputName.length() > MAX_NAME_LENGTH) { | ||||||||||||||||
throw new IllegalArgumentException(); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} |
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.
적절한 forEach 활용 좋네요! Collection 인터페이스의 api들은 많이 알면 알수록 도움이 됩니다!