diff --git a/build.gradle b/build.gradle index 8172fb73f..b83664189 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,10 @@ repositories { dependencies { testImplementation "org.junit.jupiter:junit-jupiter:5.7.2" testImplementation "org.assertj:assertj-core:3.19.0" + // https://mvnrepository.com/artifact/org.hamcrest/hamcrest + testImplementation group: 'org.hamcrest', name: 'hamcrest', version: '2.1' + testImplementation "org.junit.jupiter:junit-jupiter-params:5.8.1" + } test { diff --git a/src/main/java/baseball/BaseBallMain.java b/src/main/java/baseball/BaseBallMain.java new file mode 100644 index 000000000..a4444b26c --- /dev/null +++ b/src/main/java/baseball/BaseBallMain.java @@ -0,0 +1,91 @@ +package baseball; + +import java.util.Arrays; +import java.util.Scanner; +import java.util.stream.Collectors; + +public class BaseBallMain { + + public static String[] computerRandomNumbers = new String[3]; + public static String[] userRandomNumbers = new String[3]; + + public static int ballNum = 0; + public static int strike = 0; + public static boolean isReturnGame = true; + public static int isReturnNumber = 0; + public static Scanner sc = new Scanner(System.in); + + public static String inputValue() { + return sc.next(); + } + + public static void getComputerRandomNumbers() { + for(int i = 0; i < computerRandomNumbers.length;i++) { + computerRandomNumbers[i] = String.valueOf((int)(Math.random() * 9) + 1); + for(int j = 0; j < i; j++) { + if(computerRandomNumbers[i].equals(computerRandomNumbers[j])) { + i--; + } + } + } + getComputerRandomNumbersTrans(computerRandomNumbers); + } + + public static void getComputerRandomNumbersTrans(String[] computerRandomNumbers) { + String resComputerRandomNumbers = Arrays.stream(BaseBallMain.computerRandomNumbers).collect(Collectors.joining()); + print(resComputerRandomNumbers); + } + + public static void characterComparison(String userInputNumbers) { + for(int i = 0; i < computerRandomNumbers.length;i++) { + if(userInputNumbers.contains(computerRandomNumbers[i])) { + indexNumberComparison(userInputNumbers.indexOf(computerRandomNumbers[i]),i); + } + } + } + + public static void indexNumberComparison(int indexNumbers, int i ) { + if( indexNumbers == i ) { + strike++; + } else { + ballNum++; + } + } + + public static void numberBaseBallIsBoolean() { + if( strike == 3 ) { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + isReturnNumber = Integer.parseInt(inputValue()); + gameChoiceNumbers(isReturnNumber); + } + } + + public static void gameChoiceNumbers(int isReturnNumber) { + if(isReturnNumber == 2) { + isReturnGame = false; + return; + } + getComputerRandomNumbers(); + } + + public static void print(String resComputerRandomNumbers) { + System.out.println("컴퓨터 :" + resComputerRandomNumbers); + } + + public static void main(String[] args) { + getComputerRandomNumbers(); + + String userInputNumber = ""; + + while ( isReturnGame ) { + System.out.print("숫자를 입력해 주세요 : "); + String inputNum = inputValue(); + characterComparison(inputNum); + System.out.println(ballNum + "볼 " + strike + "스트라이크"); + numberBaseBallIsBoolean(); + ballNum = 0; + strike = 0; + } + } +} diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java new file mode 100644 index 000000000..061efc35d --- /dev/null +++ b/src/test/java/study/SetTest.java @@ -0,0 +1,78 @@ +package study; + +import org.assertj.core.internal.Strings; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.HashSet; +import java.util.Set; + +/* +지원을 안해준다.hamcrest +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +*/ + +/* +파악 필요 +import static org.hamcrest.MatcherAssert.assertThat;*/ + + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.*; + + +public class SetTest { + private Set numbers; + // Set은 무엇이며 HashSet은 어떤구조로 이루어져있는가? + + @BeforeEach + void setUp() { + numbers = new HashSet<>(); + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + @Test + @DisplayName("현재 Set에 담긴 데이터 값이 4개인지") + void sizeFour() { + assertThat(numbers.size()).isEqualTo(4); + } + + @Test + @DisplayName("현재 Set에 담긴 데이터 값이 3개인지") + void sizeThree() { + assertThat(numbers.size()).isEqualTo(3); + } + + @Test + @DisplayName("값들이 정상적으로 존재하는지 확인") + void contains() { + assertThat(numbers.contains(1)).isTrue(); + assertThat(numbers.contains(2)).isTrue(); + assertThat(numbers.contains(3)).isTrue(); + } + + // 중복되는 코드들은 생각해서 개발 필요 + // 둘다 동일한건지 확인 필요 import static org.hamcrest.MatcherAssert.assertThat; + @ParameterizedTest + @ValueSource(strings = {"", ""}) + void isBlank_ShouldReturnTrueForNullOrBlankStrings(String input) { + assertThat(input).isEqualTo(""); + } + + // 입력값에 따른 결과값이 달라지는 테스트 구현 + @ParameterizedTest + @CsvSource(value = {"test:test", "tEst:test","Java:java","asb:sss"}, delimiter = ':') + void toLowerCase_shouldGenerateTheExpectedLowercaseValue(String input,String expected) { + String actualValue = input.toLowerCase(); + assertThat(expected).isEqualTo(actualValue); + } + +} diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index 43e47d90b..d9fd11b46 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -1,8 +1,9 @@ package study; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.*; public class StringTest { @Test @@ -10,4 +11,61 @@ void replace() { String actual = "abc".replace("b", "d"); assertThat(actual).isEqualTo("adc"); } + + @Test + void test() { + String[] actual = {"2"}; + String s = "123"; + + System.out.println(s.indexOf(actual[0])); + System.out.println(s.contains(actual[0])); + } + + // 요구사항 1 + @Test + void split() { + String[] actual = "1,2".split(","); + assertThat(actual[0]).isEqualTo("1"); + } + + @Test + void contains() { + String[] actual = "1,2".split(","); + assertThat(actual).contains(actual[0],actual[1]); + } + + @Test + void containsExactly() { + String[] actual = "1,2".split(","); + assertThat(actual).containsExactly(actual[0],actual[1]); + } + + // 요구사항 2 + @Test + void substring() { + String str = "(1,2)".substring(1,4); + assertThat(str).isEqualTo("1,2"); + } + + // 요구사항 3 + @Test + void charAt() { + char oneChar = "123".charAt(5); + System.out.println(oneChar); + } + + @Test + @DisplayName("입력값 범위 밖일 경우 StringIndexOutOfBoundsException 확인") + public void StringIndexOutOfBoundsException() { + String str = "abc"; + + assertThatThrownBy(() -> str.charAt(str.length())) + .isInstanceOf(StringIndexOutOfBoundsException.class) + .hasMessageContaining("String 범위를 넘어섰습니다.") + .hasMessageContaining(String.valueOf(str.length())); + } + + + + } diff --git a/src/test/java/study/baseBallTest/BaseBallTest.java b/src/test/java/study/baseBallTest/BaseBallTest.java new file mode 100644 index 000000000..ddeabf52b --- /dev/null +++ b/src/test/java/study/baseBallTest/BaseBallTest.java @@ -0,0 +1,5 @@ +package study.baseBallTest; + +public class BaseBallTest { + +} diff --git a/src/test/java/study/calculator/Calculator.java b/src/test/java/study/calculator/Calculator.java new file mode 100644 index 000000000..5932ebb07 --- /dev/null +++ b/src/test/java/study/calculator/Calculator.java @@ -0,0 +1,21 @@ +package study.calculator; + +public class Calculator { + + public static int add(int a, int b) { + return a + b; + } + + public static int minus(int a, int b) { + return a - b; + } + + public static int division(int a, int b) { + return a / b; + } + + public static int remainder(int a, int b) { + return a % b; + } + +} diff --git a/src/test/java/study/calculator/CalculatorTest.java b/src/test/java/study/calculator/CalculatorTest.java new file mode 100644 index 000000000..9b0414f69 --- /dev/null +++ b/src/test/java/study/calculator/CalculatorTest.java @@ -0,0 +1,44 @@ +package study.calculator; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CalculatorTest { + Calculator calculator; + + // 생성자 초기화 + @BeforeEach + public void setUp() { + calculator = new Calculator(); + } + + @Test + public void 더하기() { + assertEquals(5,calculator.add(1,3)); + } + + @Test + public void 빼기() { + assertEquals(5,calculator.minus(1,3)); + } + + @Test + public void 나누기() { + assertEquals(5,calculator.division(1,3)); + } + + @Test + public void 나머지() { + assertEquals(5,calculator.remainder(1,3)); + } + + // 클래스 null 처리 + // 이미 한번 선언한 클래스는 계속 쓰레기 영역에 들어가있기때문임. 메모리 낭비 + @AfterEach + public void tearDown() { + calculator = null; + } + +} diff --git a/src/test/java/study/calculatorNew/AddStrategy.java b/src/test/java/study/calculatorNew/AddStrategy.java new file mode 100644 index 000000000..18c57d438 --- /dev/null +++ b/src/test/java/study/calculatorNew/AddStrategy.java @@ -0,0 +1,8 @@ +package study.calculatorNew; + +public class AddStrategy implements CalculatorStrategy{ + @Override + public int calculator(int a, int b) { + return a + b; + } +} diff --git a/src/test/java/study/calculatorNew/CalculatorNew.java b/src/test/java/study/calculatorNew/CalculatorNew.java new file mode 100644 index 000000000..e15884fff --- /dev/null +++ b/src/test/java/study/calculatorNew/CalculatorNew.java @@ -0,0 +1,17 @@ +package study.calculatorNew; + +import java.util.Scanner; + +public class CalculatorNew { + + CalculatorStrategy calculatorStrategy; + + CalculatorNew(CalculatorStrategy calculatorStrategy) { + this.calculatorStrategy = calculatorStrategy; + } + + public int returnResult(int a, int b) { + return calculatorStrategy.calculator(a, b); + } + +} diff --git a/src/test/java/study/calculatorNew/CalculatorState.java b/src/test/java/study/calculatorNew/CalculatorState.java new file mode 100644 index 000000000..603005753 --- /dev/null +++ b/src/test/java/study/calculatorNew/CalculatorState.java @@ -0,0 +1,4 @@ +package study.calculatorNew; + +public enum CalculatorState { +} diff --git a/src/test/java/study/calculatorNew/CalculatorStrategy.java b/src/test/java/study/calculatorNew/CalculatorStrategy.java new file mode 100644 index 000000000..448af7eef --- /dev/null +++ b/src/test/java/study/calculatorNew/CalculatorStrategy.java @@ -0,0 +1,6 @@ +package study.calculatorNew; + +// 전략패턴을 이용하여 객체간의 의존성을 낮춰준다. +public interface CalculatorStrategy { + int calculator(int a, int b ); +} diff --git a/src/test/java/study/calculatorNew/CalculatorTest.java b/src/test/java/study/calculatorNew/CalculatorTest.java new file mode 100644 index 000000000..7e731ae5f --- /dev/null +++ b/src/test/java/study/calculatorNew/CalculatorTest.java @@ -0,0 +1,27 @@ +package study.calculatorNew; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +public class CalculatorTest { + + CalculatorNew calculatorNew; + + @Test + @DisplayName("정상적으로 add의 값이 동일한지 확인") + void add() { + calculatorNew = new CalculatorNew(new AddStrategy()); + assertThat(calculatorNew.returnResult(1,3)).isEqualTo(4); + } + + @Test + @DisplayName("정상적으로 곱의 값이 동일한지 확인") + void mut() { + calculatorNew = new CalculatorNew(new MultipleStategy()); + assertThat(calculatorNew.returnResult(1,3)).isEqualTo(4); + } + +} diff --git a/src/test/java/study/calculatorNew/MultipleStategy.java b/src/test/java/study/calculatorNew/MultipleStategy.java new file mode 100644 index 000000000..a750ea8cc --- /dev/null +++ b/src/test/java/study/calculatorNew/MultipleStategy.java @@ -0,0 +1,8 @@ +package study.calculatorNew; + +public class MultipleStategy implements CalculatorStrategy{ + @Override + public int calculator(int a , int b) { + return a * b; + } +}