-
Notifications
You must be signed in to change notification settings - Fork 79
[자동차 경주] 강동안 미션 제출합니다. #80
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
Changes from all commits
6974755
7a629e8
75d7e77
701b663
be61228
6b021a4
f05b78a
981d0ca
e2f9636
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,41 @@ | ||
import domain.Car; | ||
import domain.Competition; | ||
import view.InputView; | ||
import view.ResultView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
public class Game { | ||
Competition game = new Competition(); | ||
|
||
public void game(){ | ||
String[] carNames = InputView.getCarNames(); | ||
int gameTime = InputView.getGameTime(); | ||
|
||
if(isVaildNames(carNames)){ | ||
return; | ||
} | ||
|
||
joinGame(carNames); | ||
|
||
System.out.println("\n실행 결과"); | ||
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. 그러게요! 이제와서 생각이 나네요. |
||
for (int i=0;i < gameTime;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. 파일 전체적으로 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. 감사합니다! |
||
game.runRandomCompetition(); | ||
ResultView.printRound(game.getCars()); | ||
} | ||
|
||
ArrayList<Car> winners = game.getWinners(); | ||
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. 공부할 때 이부분은 빼놓고 선언 해주는 것만 생각하고있었나봐요. 피드백 주셔서 감사합니다! |
||
ResultView.printWinners(winners); | ||
} | ||
|
||
private boolean isVaildNames(String[] cars){ | ||
return Arrays.stream(cars).allMatch(car -> car.length() > 5); | ||
} | ||
|
||
private void joinGame(String[] cars){ | ||
for (String car : cars){ | ||
game.joinCompetition(car); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
public class Main { | ||
public static void main(String[] args){ | ||
Game game = new Game(); | ||
|
||
game.game(); | ||
Comment on lines
+3
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. 이 정도는 개행 안해도 될것 같아요 👍 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,26 @@ | ||
package domain; | ||
|
||
public class Car { | ||
private String name; | ||
private int position; | ||
|
||
public Car(String name, int position){ | ||
this.name = name; | ||
this.position = position; | ||
} | ||
|
||
public int getPosition(){ | ||
return this.position; | ||
} | ||
|
||
public String getName(){ | ||
return this.name; | ||
} | ||
|
||
// 차 이동 | ||
public void moveCar(int value) { | ||
if (value >= 4){ | ||
this.position += 1; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Random; | ||
import java.util.stream.Collectors; | ||
|
||
public class Competition { | ||
ArrayList<Car> cars = new ArrayList<>(); | ||
|
||
public void joinCompetition(String name){ | ||
cars.add(new Car(name, 0)); | ||
} | ||
|
||
public void runRandomCompetition(){ | ||
Random random = new Random(); | ||
|
||
for (Car car : cars){ | ||
car.moveCar(random.nextInt(10)); | ||
} | ||
} | ||
|
||
public void runCompetition(int value){ | ||
for (Car car : cars){ | ||
car.moveCar(value); | ||
} | ||
} | ||
Comment on lines
+23
to
+27
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 int getPositionMax(){ | ||
ArrayList<Integer> positionList = new ArrayList<>(); | ||
|
||
for (Car car : cars){ | ||
positionList.add(car.getPosition()); | ||
} | ||
Comment on lines
+32
to
+34
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. 오 그럼 CarController로 네이밍 후 객체를 다루는 클래스로 분리해주는거도 괜찮은거 같아요! |
||
|
||
return Collections.max(positionList); | ||
} | ||
|
||
public ArrayList<Car> getWinners(){ | ||
int maxPosition = getPositionMax(); | ||
return cars.stream() | ||
.filter(car -> car.getPosition() == maxPosition) | ||
.collect(Collectors.toCollection(ArrayList::new)); | ||
} | ||
|
||
public ArrayList<Car> getCars(){ | ||
return cars; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package view; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
private static final Scanner scanner = new Scanner(System.in); | ||
|
||
public static String[] getCarNames() { | ||
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); | ||
String input = scanner.nextLine(); | ||
return input.split(","); | ||
} | ||
|
||
public static int getGameTime() { | ||
System.out.println("시도할 횟수는 몇 회인가요?"); | ||
return scanner.nextInt(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package view; | ||
|
||
import domain.Car; | ||
|
||
import java.util.ArrayList; | ||
import java.util.StringJoiner; | ||
|
||
public class ResultView { | ||
public static void printRound(ArrayList<Car> cars) { | ||
for (Car car : cars) { | ||
System.out.println(car.getName() + " : " + "-".repeat(car.getPosition())); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public static void printWinners(ArrayList<Car> winners) { | ||
StringJoiner joiner = new StringJoiner(", "); | ||
for (Car car : winners) { | ||
joiner.add(car.getName()); | ||
} | ||
System.out.println(joiner + "가 최종 우승했습니다!"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import domain.Car; | ||
import domain.Competition; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
|
||
public class JUnit5Test { | ||
|
||
@Nested | ||
class moveTest{ | ||
|
||
@Test | ||
void move3(){ | ||
Car car = new Car("TeCar", 0); | ||
car.moveCar(3); | ||
assertEquals(0, car.getPosition()); | ||
} | ||
|
||
@Test | ||
void move4(){ | ||
Car car = new Car("TeCar", 0); | ||
car.moveCar(4); | ||
assertEquals(1, car.getPosition()); | ||
} | ||
} | ||
|
||
@Nested | ||
class competitionTest{ | ||
|
||
@Test | ||
void runFixedValue() { | ||
Competition game = new Competition(); | ||
|
||
game.joinCompetition("Kang"); | ||
game.joinCompetition("dong"); | ||
game.joinCompetition("ahn"); | ||
|
||
game.runCompetition(4); | ||
|
||
for (Car car: game.getCars()){ | ||
assertEquals(1, car.getPosition()); | ||
} | ||
} | ||
} | ||
} |
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.
Game
클래스 내에서Competition
클래스의 객체 이름이game
인건 뭔가 헷갈리는것 같아요!Racing
같은 네이밍은 어떨까요??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.
그런 방향으로 생각도 해봐야겠네요. 비슷한 기능들이 좀 클래스화되어있으니 좀 더 이름 짓기가 어려워지는거 같아요. 다음에는 좀 더 구체적으로 적어보겠습니다!