Skip to content

Commit

Permalink
feat(Cars): 우승자 계산
Browse files Browse the repository at this point in the history
  • Loading branch information
homebdy committed Sep 8, 2023
1 parent ab75b3f commit afc00cf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/racingcar/domain/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ public void race() {
position.move();
}

public boolean isWinner(int maxPosition) {
return position.isSame(maxPosition);
}

public String getRacingResult() {
return String.format(OutputMessage.RACING_RESULT.toString(), name.getValue(), position.getValue());
}

public Position getPosition() {
return position;
}

public Name getName() {
return name;
}
}
19 changes: 19 additions & 0 deletions src/main/java/racingcar/domain/Cars.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package racingcar.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Cars {

Expand All @@ -23,4 +25,21 @@ public String getRacingResult() {
);
return stringBuilder.toString();
}

public List<Name> getWinner() {
int maxPosition = calculateMaxPosition();
return cars.stream()
.filter(car -> car.isWinner(maxPosition))
.map(Car::getName)
.collect(Collectors.toList());
}

public int calculateMaxPosition() {
List<Integer> positions = cars.stream()
.map(Car::getPosition)
.mapToInt(Position::getPosition)
.boxed()
.collect(Collectors.toList());
return Collections.max(positions);
}
}

0 comments on commit afc00cf

Please sign in to comment.