Conversation
| Car[] car = new Car[carCount]; | ||
| for (int i = 0; i < carCount; i++) { | ||
| car[i] = new Car(); | ||
| car[i].name = cars[i]; |
There was a problem hiding this comment.
객체의 정보(필드)에 접근할 때에는 직접 접근하지 않고, Getter(접근자), Setter(수정자) 메소드를 클래스 안에 별도로 만들어서 접근하는 것이 일반적인 원칙입니다!
이는 보고서에서 작성해주신 정보은닉과 관련이 있습니다
public class Car {
String name;
public getName() { // 접근자
return name;
}
public setName(String name) { // 수정자
this.name = name;
}
}
| String name; | ||
| int count = 0; | ||
| public void move() { | ||
| if (Randoms.pickNumberInRange(0, 9) >= 4) { |
There was a problem hiding this comment.
4 와 같이 무언가 의미가 있는 숫자들은 상수로 처리하는 것을 권장합니다!
private static final int CAR_MOVE_STANDARD = 4;
public void move() {
if(Randoms.pickNumberInRange(0, 9) >= CAR_MOVE_STANDARD) { ... }
}
| throw new IllegalArgumentException("시도 횟수는 숫자여야 합니다."); | ||
| } | ||
|
|
||
| if (count <= 0) { |
There was a problem hiding this comment.
자바 메소드(Integer.signum())를 이용해서 양수/음수 를 판별할 수도 있습니다!
| } | ||
|
|
||
| for (int k = 0; k < carCount; k++) { | ||
| String dashes = "-".repeat(car[k].count); |
There was a problem hiding this comment.
이 부분도 위에서 언급했던 것과 마찬가지로, 객체에 접근자를 만들어 사용하는 것을 권장합니다
String dashes = "-".repeat(car[k].getCount()):
|
|
||
| // 빈 값 검증 | ||
| if (name.isEmpty()) { | ||
| throw new IllegalArgumentException("이름은 빈 값일 수 없습니다."); |
| throw new IllegalArgumentException("이름은 빈 값일 수 없습니다."); | ||
| } | ||
| // 5자 초과 검증 | ||
| if (name.length() > 5) { |
There was a problem hiding this comment.
이 부분도 상술한 것과 마찬가지로 상수 처리 하면 좋을 것 같습니다..!
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| class Car { | ||
| String name; |
There was a problem hiding this comment.
Car 의 이름은 한 번 정해지면 바뀔 필요가 없으므로, final 로 지정해주셔도 됩니다!
그리고 Car 의 이름은 생성자에서 초기화 시키는 방향으로 하면 좋을 것 같습니다
|
|
||
|
|
||
| int carCount = cars.length; | ||
| Car[] car = new Car[carCount]; |
There was a problem hiding this comment.
자바에서 기본적으로 제공하는 List<> 를 이용해보시는 것도 추천드립니다!
사용할 수 있는 유용한 메소드들이 많아서 정말 많이 사용되는 라이브러리입니다
No description provided.