Skip to content
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

Jooseongho #15

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class Car {
private String name;
private int position;

public Car() {
}

public Car(String name) {
this.name = name;
this.position = 0;
}

public Car(String name, int position) {
this.name = name;
this.position = position;
}

public void progress(int randomGenerate) {
if(randomGenerate >=4) movePosition();
}


public int getPosition() {
return position;
}

public String getName() {
return name;
}

public void movePosition() {
this.position+=1;
}
}
18 changes: 18 additions & 0 deletions src/main/java/GameStart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Arrays;
import java.util.Scanner;

public class GameStart {
public static boolean validate(String input) {
if(input.length() > 5) return false;
return true;
}

public String[] input(String s) {
String[] split = s.split(",");
Arrays.stream(split)
.filter(x->validate(x))
.findAny()
.orElseThrow(()-> new IllegalArgumentException("5 글자 미만이어야합니다."));
return split;
}
}
23 changes: 23 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Racing racing = new Racing();
Scanner scanner = new Scanner(System.in);
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).");
String input = scanner.nextLine();
System.out.println("시도할 회수는 몇회인가요?");
int game = scanner.nextInt();
List<Car> cars = racing.start(input,game);

boolean play = true;
while(play){
play = racing.play();
}

Result.printResult(cars);


}
}
56 changes: 56 additions & 0 deletions src/main/java/Racing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.util.*;


public class Racing {
List<Car> cars;
int game;

public Racing() {
this.cars = new ArrayList<>();
this.game = 0;
}

public Racing(List<Car> cars, int game) {
this.cars = cars;
this.game = game;
}

public List<Car> start(String input, int game){
GameStart gameStart = new GameStart();
String[] split = gameStart.input(input);
for(String s : split){
this.cars.add(new Car(s));
}

this.game = game;
return this.cars;
}

public static int randomGenerate() {
Random random = new Random();
int ran = random.nextInt(9);
return ran;
}

public List<Car> getCars() {
return cars;
}

public int getGame() {
return game;
}

private boolean consumeGame(){
if(getGame() == 0) {
return false;
}
cars.stream().forEach(car-> car.progress(Racing.randomGenerate()));
Result.printProgreeBar(this.cars);
this.game-=1;
return true;
}

public boolean play() {
return consumeGame();
}
}
32 changes: 32 additions & 0 deletions src/main/java/Result.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

public class Result {
public static List<Car> endGame(List<Car> cars) {
cars.sort(Comparator.comparing(Car::getPosition).reversed());
List<Car> carList = new ArrayList<>();
carList.add(cars.get(0));
for(int i=1;i< cars.size();i++){
if(cars.get(i-1).getPosition() != cars.get(i).getPosition()){
break;
}
carList.add(cars.get(i));
}
return carList;
}

public static void printProgreeBar(List<Car> cars){
for(int i=0;i<cars.size();i++){
System.out.println(cars.get(i).getName() + " : " + cars.get(i).getPosition());
}
System.out.println();
}

public static void printResult(List<Car> cars){
List<Car> result = endGame(cars);
System.out.print("우승자는 ");
result.stream().forEach(x-> System.out.print(x.getName() + " "));
}
}
15 changes: 15 additions & 0 deletions src/test/java/CarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CarTest {

@Test
void progress_over_4(){
Racing racing = new Racing();
int position = 3;
Car car = new Car("pobi", position);
car.progress(racing.randomGenerate());
assertEquals(car.getPosition() , position+1);
}
}
28 changes: 28 additions & 0 deletions src/test/java/GameStartTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class GameStartTest {


@Test
void car_name_split(){
GameStart gs = new GameStart();
String[] str = gs.input("pobi,crong,honux");
assertEquals(str[0], "pobi");
assertEquals(str[1], "crong");
assertEquals(str[2], "honux");
}


@Test
void car_name_under_5(){
String input1 = "crong";
String input2 = "crongs";

assertTrue(GameStart.validate(input1));
assertTrue(!GameStart.validate(input2));
}

}
31 changes: 31 additions & 0 deletions src/test/java/RacingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;

public class RacingTest {

@Test
void randomGenerate_random_1_9(){
int num = Racing.randomGenerate();
assertTrue(num <= 9 );
assertTrue(num >= 0 );
}

@Test
@DisplayName("Racing의 this.game은 1이 줄어들고, progress는 진행한다.")
void play(){
Racing racing = new Racing();
List<Car> cars = racing.start("pobi,crong,honux",4);
racing.play();
assertEquals(racing.getGame(),3);
assertEquals(cars.get(0).getName(), "pobi");
assertEquals(cars.get(1).getName(), "crong");
assertEquals(cars.get(2).getName(), "honux");
}



}
24 changes: 24 additions & 0 deletions src/test/java/ResultTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import org.junit.jupiter.api.Test;

import java.util.*;

import static org.junit.jupiter.api.Assertions.*;

class ResultTest {

@Test
void printResult(){

}

@Test
void endGame(){
List<Car> cars = Arrays.asList(new Car("pobi", 4), new Car("crong", 2), new Car("honux", 4));
List<Car> result = Result.endGame(cars);
assertEquals(result.get(0).getName(), "pobi");
assertEquals(result.get(0).getPosition(), 4);
assertEquals(result.get(1).getName(), "honux");
assertEquals(result.get(1).getPosition(), 4);
}

}