Skip to content

[자동차 경주] 강동안 미션 제출합니다. #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

Merged
merged 9 commits into from
Feb 18, 2025
41 changes: 41 additions & 0 deletions src/main/java/Game.java
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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Game 클래스 내에서 Competition 클래스의 객체 이름이 game인건 뭔가 헷갈리는것 같아요!
Racing 같은 네이밍은 어떨까요??

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그런 방향으로 생각도 해봐야겠네요. 비슷한 기능들이 좀 클래스화되어있으니 좀 더 이름 짓기가 어려워지는거 같아요. 다음에는 좀 더 구체적으로 적어보겠습니다!


public void game(){
String[] carNames = InputView.getCarNames();
int gameTime = InputView.getGameTime();

if(isVaildNames(carNames)){
return;
}

joinGame(carNames);

System.out.println("\n실행 결과");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 ResultView의 메서드로 뺄 수 있을것 같아요!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그러게요! 이제와서 생각이 나네요.

for (int i=0;i < gameTime;i++){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

파일 전체적으로 ctrl + alt + L 단축키를 사용하면 자동 줄맞춤이 되는데 한번 해보면 좋을 것 같아용

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다!

game.runRandomCompetition();
ResultView.printRound(game.getCars());
}

ArrayList<Car> winners = game.getWinners();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

업캐스팅에 대해서 한번 알아보면 좋을 것 같아요!
다형성과 관련이 있답니다
자료 : https://bibi6666667.tistory.com/236

Copy link
Author

Choose a reason for hiding this comment

The 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);
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/Main.java
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 정도는 개행 안해도 될것 같아요 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵! 😄

}
}
26 changes: 26 additions & 0 deletions src/main/java/domain/Car.java
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;
}
}
}
49 changes: 49 additions & 0 deletions src/main/java/domain/Competition.java
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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cars 를 많이 사용하는 것 같은데 Car 객체들을 다루는 클래스도 하나 만들어보는건 어떨까요?

Copy link
Author

Choose a reason for hiding this comment

The 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;
}
}
18 changes: 18 additions & 0 deletions src/main/java/view/InputView.java
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();
}
}
23 changes: 23 additions & 0 deletions src/main/java/view/ResultView.java
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 + "가 최종 우승했습니다!");
}
}
46 changes: 46 additions & 0 deletions src/test/java/JUnit5Test.java
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());
}
}
}
}