Skip to content
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
48 changes: 48 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

# Created by .ignore support plugin (hsz.mobi)
### Java template
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

.DS_Store
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar
/out/
/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
9 changes: 9 additions & 0 deletions .idea/2021-Java-Study-1.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 112 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added image/img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions racingcar실습/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import utils.GameUtils;

public class Application {
public static void main(String[] args) {
GameUtils.run();
}
}
23 changes: 23 additions & 0 deletions racingcar실습/java/inpututils/InputValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package inpututils;

public class InputValidator {
private static final int MAX_NAME = 5;

public static void nameValidator(String name) {
nullCheck(name);
lengthCheck(name);
}

public static void nullCheck(String name) {
if ("".equals(name)) {
throw new IllegalArgumentException("[ERROR]자동차 이름을 입력해주세요.");
}
}

public static void lengthCheck(String name) {
if (name.length() > MAX_NAME) {
throw new IllegalArgumentException("[ERROR]자동차 이름은 5자 이내여야합니다.");
}
}

}
21 changes: 21 additions & 0 deletions racingcar실습/java/inpututils/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package inpututils;

import java.util.Scanner;

public class InputView {
private static final Scanner SCANNER = new Scanner(System.in);

public static String getCarName() {
System.out.print("경주 할 자동차 이름 : ");
return SCANNER.nextLine();
}

public static int getTryNumber() {
System.out.print("시도할 횟수 : ");
try {
return SCANNER.nextInt();
} catch (NumberFormatException e) {
throw new NumberFormatException("[ERROR] 시도 횟수는 숫자여야 한다.");
}
}
}
21 changes: 21 additions & 0 deletions racingcar실습/java/outpututils/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package outpututils;

import java.util.ArrayList;

public class OutputView {
private OutputView() {
}
public static void printCarName(String name) {
System.out.print(name + " : ");
}

public static void printMove() {
System.out.print("-");
}

public static void printWinner(ArrayList<String> winner) {
//TODO:마지막에 , 나오지 않도록 수정 필요
winner.stream()
.forEach(w -> System.out.print(w + ","));
}
}
26 changes: 26 additions & 0 deletions racingcar실습/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package racingcar;

import utils.GameUtils;

public class Car {
private final String name;
private int position = 0;
Copy link
Member

Choose a reason for hiding this comment

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

객체의 생성 부분에 들어가는게 좋지않을까요?


public Car(String name) {
InputValidator.nameValidator(name);
this.name = name;
}

public String getName(){
return name;
}

public int plusPosition(){
return position++;
}

public int getPosition() {
return position;
}

}
67 changes: 67 additions & 0 deletions racingcar실습/java/racingcar/Cars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package racingcar;

import outpututils.OutputView;
import utils.RandomUtils;

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

public class Cars {
private static final int START_NUMBER = 0;
private static final int END_NUMBER = 9;
private static final int BOUNDARY_NUMBER = 4;
private static List<Car> carList;
private static ArrayList<String> winner;

public Cars(List<Car> carList) {
this.carList = carList;
}

public static boolean isMove(int randomNumber) {
if (randomNumber >= BOUNDARY_NUMBER) {
return true;
}
return false;
}

public static void playGame() {
for (int j = 0; j < carList.size(); j++) {
OutputView.printCarName(carList.get(j).getName());

if (isMove(RandomUtils.nextInt(START_NUMBER, END_NUMBER))) {
carList.get(j).plusPosition();
}
for (int k = 0; k < carList.get(j).getPosition(); k++) {
OutputView.printMove();
}
System.out.println();
}
}

public static int getMax() {
int max = carList.stream()
.mapToInt(Car::getPosition)
.max()
.getAsInt();
return max;
}

public static void setWinnerList(int max) {
winner = new ArrayList<String>();
carList.stream()
.filter(s -> s.getPosition() == max)
.filter(s -> winner.add(s.getName()))
.collect(Collectors.toList());
}

public static ArrayList<String> getWinnerList() {
return winner;
}
public static void playGames(int inputCount){
for (int i = 0; i < inputCount; i++) {
playGame();
System.out.println();
}
}
}
32 changes: 32 additions & 0 deletions racingcar실습/java/utils/GameUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils;

import inpututils.InputView;
import outpututils.OutputView;
import racingcar.Car;
import racingcar.Cars;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class GameUtils {

public static List<Car> makeCarList(String[] splitResult) {
return Arrays.stream(splitResult)
.map(Car::new)
.collect(Collectors.toList());
}

public static void run() {
String inputName = InputView.getCarName();
int inputCount = InputView.getTryNumber();
String[] splitResult = SplitString.splitString(inputName);

Cars cars = new Cars(makeCarList(splitResult));

Cars.playGames(inputCount);
Cars.setWinnerList(Cars.getMax());
OutputView.printWinner(Cars.getWinnerList());

}
}
Loading