Skip to content

숫자야구 단위 테스트 #116

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

Open
wants to merge 2 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
19 changes: 18 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'eclipse'

group = 'camp.nextstep'
version = '1.0.0'
sourceCompatibility = '1.8'
sourceCompatibility = "14"

repositories {
mavenCentral()
Expand All @@ -17,3 +17,20 @@ dependencies {
test {
useJUnitPlatform()
}

//// annotation processing 추가
//tasks.withType(JavaCompile) {
// options.annotationProcessorPath = configurations.apt
//}
//
//configurations {
// apt
//}
//
//dependencies {
// apt 'org.projectlombok:lombok:1.18.20'
// compileOnly 'org.projectlombok:lombok:1.18.20'
//}

sourceCompatibility = 11
targetCompatibility = 11
Binary file added src/main/java/Calculator.class
Binary file not shown.
64 changes: 64 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Calculator {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String expression = sc.nextLine();
Calculator cc = new Calculator();
Calculator.create(expression);
System.out.println(cc.calculate(expression));
}
public static Calculator create (String expression){

if (!validatingExpression(expression))
throw new IllegalArgumentException("올바른 연산식이 아닙니다.");

return new Calculator();
}

private static boolean validatingExpression (String expression){

Pattern pattern = Pattern.compile("^[+\\-]?\\d( ?[*\\-+/] ?\\d)*$");
Matcher matcher = pattern.matcher(expression);
return matcher.find();
}


public int calculate (String expression) {

String[] parsingExpression = expression.split(" ");

int calculationResult = Integer.parseInt(parsingExpression[0]);

String operator = "";

int operand = 0;

for (int i = 0; i < parsingExpression.length; i++) {

if (i % 2 != 0) { // 홀수 인덱스에서 연산자 값 추출
operator = parsingExpression[i];
continue;
}

operand = Integer.parseInt(parsingExpression[i]);
switch (operator) {
case "+" : calculationResult += operand; break;
case "-" : calculationResult -= operand; break;
case "*" : calculationResult *= operand; break;
case "/" : {
if (operand != 0) calculationResult /= operand;
else throw new ArithmeticException("0을 나눌 수 없습니다.");
break;
}
}

}
return calculationResult;
}

}
Binary file added src/main/java/NumberBaseballGame.class
Binary file not shown.
50 changes: 50 additions & 0 deletions src/main/java/NumberBaseballGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.util.Scanner;

public class NumberBaseballGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int min = 100;
int max = 999;
int com = (int) (Math.random() * (max - min + 1)) + min;

while (com != -1) {
System.out.println(com);
int strike = 0;
int ball = 0;
System.out.print("숫자를 입력해 주세요 : ");
int userNum = sc.nextInt();
com = NumberBaseballGame.numberCheck(com, userNum, sc);
}

}

public static int numberCheck(int com, int userNum, Scanner sc) {
int strike = 0, ball = 0;
int min = 100;
int max = 999;

if (com / 100 == userNum / 100) strike++;
if (com / 10 % 10 == userNum / 10 % 10) strike++;
if (com % 10 == userNum % 10) strike++;
if (strike == 3) {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
int select = sc.nextInt();
if (select == 1) {
return (int) (Math.random() * (max - min + 1)) + min;

} else return -1;
}
if (strike != 3) {
if (com / 100 != userNum / 100 && (com / 10 % 10 == userNum / 100 || com % 10 == userNum / 100)) ball++;
if (com / 10 % 10 != userNum / 10 % 10 && (com / 100 == userNum / 10 % 10 || com % 10 == userNum / 10 % 10))
ball++;
if (com % 10 != userNum % 10 && (com / 100 == userNum % 10 || com / 10 % 10 == userNum % 10)) ball++;
if (strike != 0 && ball != 0) System.out.printf("%d스트라이크 %d볼 %n", strike, ball);
if (strike == 0 && ball != 0) System.out.printf("%d볼 %n", ball);
if (strike != 0 && ball == 0) System.out.printf("%d스트라이크 %n", strike);
if (strike == 0 && ball == 0) System.out.printf("");
}
return com;
}
}
33 changes: 33 additions & 0 deletions src/main/java/baseball/Ball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package baseball;

public class Ball {
private final int ballNum;
private final int position;

public Ball(int position, int ballNum) {
this.position = position;
this.ballNum = ballNum;
}

public BallStatus play(Ball ball) {
BallStatus ballStatus = null;
if(ball.mathBallNum(ballNum)){
ballStatus = BallStatus.BALL;
if(ball.mathPosition(position)){
ballStatus = BallStatus.STRIKE;
}
}
else ballStatus = BallStatus.NOTING;

return ballStatus;
}

private boolean mathPosition(int position) {
return this.position == position;
}

private boolean mathBallNum(int ballNum) {
return this.ballNum == ballNum;
}

}
5 changes: 5 additions & 0 deletions src/main/java/baseball/BallStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package baseball;

public enum BallStatus {
BALL, STRIKE, NOTING
}
13 changes: 13 additions & 0 deletions src/main/java/baseball/ValidationUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package baseball;

public class ValidationUtils {

public static final int MIN_NUM = -1;
public static final int MAX_NUM = 9;

public static boolean validNumTest(int num) {
// if( -1 < num && num <= 9)return true;
// return false;
return MIN_NUM < num && num <= MAX_NUM;
}
}
26 changes: 26 additions & 0 deletions src/test/java/baseball/BallTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package baseball;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class BallTest {
private Ball com;
@BeforeEach
void sutup(){
com = new Ball(1, 4);
}
@Test
void noting(){
assertThat(com.play(new Ball(2,5))).isEqualTo(BallStatus.NOTING);
}
@Test
void ball(){
assertThat(com.play(new Ball(2,4))).isEqualTo(BallStatus.BALL);
}
@Test
void strike(){
assertThat(com.play(new Ball(1, 4))).isEqualTo(BallStatus.STRIKE);
}
}
19 changes: 19 additions & 0 deletions src/test/java/baseball/ValidationUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package baseball;

import baseball.ValidationUtils;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class ValidationUtilsTest {
@Test
@DisplayName("숫자야 0~9 사이 숫자 테스트")
void baseballNumberTest(){
assertThat(ValidationUtils.validNumTest(9)).isTrue();
assertThat(ValidationUtils.validNumTest(1)).isTrue();
assertThat(ValidationUtils.validNumTest(0)).isTrue();
assertThat(ValidationUtils.validNumTest(-1)).isFalse();
assertThat(ValidationUtils.validNumTest(10)).isFalse();
}
}
Loading