File tree Expand file tree Collapse file tree 7 files changed +210
-0
lines changed Expand file tree Collapse file tree 7 files changed +210
-0
lines changed Original file line number Diff line number Diff line change 1+ import  domain .Car ;
2+ import  domain .Competition ;
3+ import  view .InputView ;
4+ import  view .ResultView ;
5+ 
6+ import  java .util .ArrayList ;
7+ import  java .util .Arrays ;
8+ 
9+ public  class  Game  {
10+     Competition  game  = new  Competition ();
11+ 
12+     public  void  game (){
13+         String [] carNames  = InputView .getCarNames ();
14+         int  gameTime  = InputView .getGameTime ();
15+ 
16+         if (isVaildNames (carNames )){
17+             return ;
18+         }
19+ 
20+         joinGame (carNames );
21+ 
22+         System .out .println ("\n 실행 결과" );
23+         for  (int  i =0 ;i  < gameTime ;i ++){
24+             game .runRandomCompetition ();
25+             ResultView .printRound (game .getCars ());
26+         }
27+ 
28+         ArrayList <Car > winners  = game .getWinners ();
29+         ResultView .printWinners (winners );
30+     }
31+ 
32+     private  boolean  isVaildNames (String [] cars ){
33+         return  Arrays .stream (cars ).allMatch (car  -> car .length () > 5 );
34+     }
35+ 
36+     private  void  joinGame (String [] cars ){
37+         for  (String  car  : cars ){
38+             game .joinCompetition (car );
39+         }
40+     }
41+ }
Original file line number Diff line number Diff line change 1+ public  class  Main  {
2+     public  static  void  main (String [] args ){
3+         Game  game  = new  Game ();
4+ 
5+         game .game ();
6+     }
7+ }
Original file line number Diff line number Diff line change 1+ package  domain ;
2+ 
3+ public  class  Car  {
4+     private  String  name ;
5+     private  int  position ;
6+ 
7+     public  Car (String  name , int  position ){
8+         this .name  = name ;
9+         this .position  = position ;
10+     }
11+ 
12+     public  int  getPosition (){
13+         return  this .position ;
14+     }
15+ 
16+     public  String  getName (){
17+         return  this .name ;
18+     }
19+ 
20+     // 차 이동 
21+     public  void  moveCar (int  value ) {
22+         if  (value  >= 4 ){
23+             this .position  += 1 ;
24+         }
25+     }
26+ }
Original file line number Diff line number Diff line change 1+ package  domain ;
2+ 
3+ import  java .util .ArrayList ;
4+ import  java .util .Collections ;
5+ import  java .util .Random ;
6+ import  java .util .stream .Collectors ;
7+ 
8+ public  class  Competition  {
9+     ArrayList <Car > cars  = new  ArrayList <>();
10+ 
11+     public  void  joinCompetition (String  name ){
12+         cars .add (new  Car (name , 0 ));
13+     }
14+ 
15+     public  void  runRandomCompetition (){
16+         Random  random  = new  Random ();
17+ 
18+         for  (Car  car  : cars ){
19+             car .moveCar (random .nextInt (10 ));
20+         }
21+     }
22+ 
23+     public  void  runCompetition (int  value ){
24+         for  (Car  car  : cars ){
25+             car .moveCar (value );
26+         }
27+     }
28+ 
29+     private  int  getPositionMax (){
30+         ArrayList <Integer > positionList  = new  ArrayList <>();
31+ 
32+         for  (Car  car  : cars ){
33+             positionList .add (car .getPosition ());
34+         }
35+ 
36+         return  Collections .max (positionList );
37+     }
38+ 
39+     public  ArrayList <Car > getWinners (){
40+         int  maxPosition  = getPositionMax ();
41+         return  cars .stream ()
42+                 .filter (car  ->  car .getPosition () == maxPosition )
43+                 .collect (Collectors .toCollection (ArrayList ::new ));
44+     }
45+ 
46+     public  ArrayList <Car > getCars (){
47+         return  cars ;
48+     }
49+ }
Original file line number Diff line number Diff line change 1+ package  view ;
2+ 
3+ import  java .util .Scanner ;
4+ 
5+ public  class  InputView  {
6+     private  static  final  Scanner  scanner  = new  Scanner (System .in );
7+ 
8+     public  static  String [] getCarNames () {
9+         System .out .println ("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)." );
10+         String  input  = scanner .nextLine ();
11+         return  input .split ("," );
12+     }
13+ 
14+     public  static  int  getGameTime () {
15+         System .out .println ("시도할 횟수는 몇 회인가요?" );
16+         return  scanner .nextInt ();
17+     }
18+ }
Original file line number Diff line number Diff line change 1+ package  view ;
2+ 
3+ import  domain .Car ;
4+ 
5+ import  java .util .ArrayList ;
6+ import  java .util .StringJoiner ;
7+ 
8+ public  class  ResultView  {
9+     public  static  void  printRound (ArrayList <Car > cars ) {
10+         for  (Car  car  : cars ) {
11+             System .out .println (car .getName () + " : "  + "-" .repeat (car .getPosition ()));
12+         }
13+         System .out .println ();
14+     }
15+ 
16+     public  static  void  printWinners (ArrayList <Car > winners ) {
17+         StringJoiner  joiner  = new  StringJoiner (", " );
18+         for  (Car  car  : winners ) {
19+             joiner .add (car .getName ());
20+         }
21+         System .out .println (joiner  + "가 최종 우승했습니다!" );
22+     }
23+ }
Original file line number Diff line number Diff line change 1+ import  domain .Car ;
2+ import  domain .Competition ;
3+ import  org .junit .jupiter .api .Nested ;
4+ import  org .junit .jupiter .api .Test ;
5+ import  static  org .junit .jupiter .api .Assertions .*;
6+ 
7+ 
8+ public  class  JUnit5Test  {
9+ 
10+     @ Nested 
11+     class  moveTest {
12+ 
13+         @ Test 
14+         void  move3 (){
15+             Car  car  = new  Car ("TeCar" , 0 );
16+             car .moveCar (3 );
17+             assertEquals (0 , car .getPosition ());
18+         }
19+ 
20+         @ Test 
21+         void  move4 (){
22+             Car  car  = new  Car ("TeCar" , 0 );
23+             car .moveCar (4 );
24+             assertEquals (1 , car .getPosition ());
25+         }
26+     }
27+ 
28+     @ Nested 
29+     class  competitionTest {
30+ 
31+         @ Test 
32+         void  runFixedValue () {
33+             Competition  game  = new  Competition ();
34+ 
35+             game .joinCompetition ("Kang" );
36+             game .joinCompetition ("dong" );
37+             game .joinCompetition ("ahn" );
38+ 
39+             game .runCompetition (4 );
40+ 
41+             for  (Car  car : game .getCars ()){
42+                 assertEquals (1 , car .getPosition ());
43+             }
44+         }
45+     }
46+ }
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments