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

[디디] 온보딩 리뷰요청 #8

Merged
merged 10 commits into from
Feb 9, 2020
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ repositories {
dependencies {
testCompile('org.junit.jupiter:junit-jupiter:5.4.2')
testCompile('org.assertj:assertj-core:3.11.1')
}

test {
useJUnitPlatform()
}
24 changes: 24 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Calculator {
private static final int FIRST = 0;
private static final int TWO = 2;
private double result = 0;

void calculate(InputValues inputValues) {
Input input = inputValues.getInput();
int length = input.getValuesLength();
double nextNumber;

result = Double.parseDouble(input.getValueByIndex(FIRST));
Operator nowOperator;
for (int i = 1; i < length; i += TWO) {
nowOperator =
Operator.getOperatorByString(input.getValueByIndex(i));
nextNumber = Double.parseDouble(input.getValueByIndex(i + 1));
result = nowOperator.operate(result, nextNumber);
}
}

double getResult(){
return result;
}
}
67 changes: 67 additions & 0 deletions src/main/java/InputValues.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.util.Scanner;
class Input{
String value;
String[] values;

public Input(){
Scanner scanner = new Scanner(System.in);
System.out.println("문자열 수식을 입력해주세요 : ");
value = scanner.nextLine();
values = value.split(" ");
}

public Input(String input){
this.value = input;
this.values = value.split(" ");
}

public int getValuesLength(){
return values.length;
}

public String getValueByIndex(int index){
return values[index];
}
}
public class InputValues {
private static final int ZERO = 0;

Input input;

public InputValues(Input input) throws IllegalArgumentException {
this.input = input;
int valueLength = input.getValuesLength();

validateEndWithOperator(valueLength);
for (int i = 0; i < valueLength; i++) {
validateValues(i);
}

}

public InputValues(String value) {
Input input = new Input(value);
}

void validateValues(int index) throws IllegalArgumentException{
if (index % 2 == ZERO) {
try {
Double.parseDouble(input.getValueByIndex(index));
} catch (NumberFormatException ne) {
throw new IllegalArgumentException("피연산자가 잘못되었습니다.");
}
} else{
Operator.getOperatorByString(input.getValueByIndex(index));
}
}

void validateEndWithOperator(int valueLength) throws IllegalArgumentException{
if (valueLength % 2 == ZERO) {
throw new IllegalArgumentException("연산자와 숫자가 맞지 않습니다.");
}
}

Input getInput(){
return input;
}
}
10 changes: 10 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Main {
public static void main(String[] args) throws IllegalArgumentException{
Input input = new Input();
InputValues inputValues = new InputValues(input);
Calculator calculator = new Calculator();

calculator.calculate(inputValues);
OutputView.printResult(calculator);
}
}
54 changes: 54 additions & 0 deletions src/main/java/Operator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.function.BinaryOperator;

public enum Operator {
PLUS("+") {
double operate(double num1, double num2) {
return operate2((s, t) -> s + t, num1, num2);
}
},
MINUS("-") {
double operate(double num1, double num2) {
return operate2((s, t) -> s - t, num1, num2);
}
},
MUL("*") {
double operate(double num1, double num2) {
return operate2((s, t) -> s * t, num1, num2);
}
},
DIV("/") {
double operate(double num1, double num2) {
double result = num1 / num2;

if (Double.isInfinite(result) || Double.isNaN(result)) {
throw new IllegalArgumentException("0으로 나눌 수 없습니다.");
}

return operate2((s, t) -> s / t, num1, num2);
}

Choose a reason for hiding this comment

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

훨씬 깔끔하네요 👍

};

private String stringOperator;

Operator(String stringOperator) {
this.stringOperator = stringOperator;
}

abstract double operate(double num1, double num2);

double operate2(BinaryOperator<Double> binaryOperator, double num1,
double num2) {
return binaryOperator.apply(num1, num2);
}

static Operator getOperatorByString(String stringOperator) throws IllegalArgumentException {
Operator[] operators = values();

for (Operator o : operators) {
if (o.stringOperator.equals(stringOperator)) {
return o;
}
}
throw new IllegalArgumentException("연산자가 잘못되었습니다.");
}
}
5 changes: 5 additions & 0 deletions src/main/java/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class OutputView {
public static void printResult(Calculator calculator){
System.out.println("결과 : " + calculator.getResult());
}
}
Empty file removed src/main/java/empty.txt
Empty file.
127 changes: 127 additions & 0 deletions src/test/java/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

public class CalculatorTest {
@ParameterizedTest
@DisplayName("입력이 올바른지 검증")
@CsvSource(value = {"1.9 * 6.2:true", "5 * 6 * 7 * 9:true", "0 / 0:true"}
, delimiter = ':')
void validateValuesTest(String value, boolean expected) {
boolean actualResult = true;
try {
Input input = new Input(value);
InputValues inputValues = new InputValues(input);
int length = input.getValuesLength();

for (int i = 0; i < length; i++) {
inputValues.validateValues(i);
}
} catch (NumberFormatException ne) {
actualResult = false;
} catch (IllegalArgumentException ie) {
actualResult = false;
}
Assertions.assertThat(actualResult).isEqualTo(expected);
}

@ParameterizedTest
@DisplayName("올바르지 않은 연산자가 오는 경우 에러메세지 검증")
@ValueSource(strings = {"3 ! 3 * 3", "3 3 3", "5 ) 9"})
void validateValuesTest2(String value) {
Assertions.assertThatThrownBy(() -> {
Input input = new Input(value);
InputValues inputValues = new InputValues(input);
int length = input.getValuesLength();

for (int i = 0; i < length; i++) {
inputValues.validateValues(i);
}
}).isInstanceOf(IllegalArgumentException.class).hasMessageMatching(
"연산자가 잘못되었습니다.");
}

@ParameterizedTest
@DisplayName("올바르지 않은 피연산자가 오는 경우 에러메세지 검증")
@ValueSource(strings = {"! * 3 * 3", "a / 3", "5 * * + 6"})
void validateValuesTest3(String value) {
Assertions.assertThatThrownBy(() -> {
Input input = new Input(value);
InputValues inputValues = new InputValues(input);
int length = input.getValuesLength();

for (int i = 0; i < length; i++) {
inputValues.validateValues(i);
}
}).isInstanceOf(IllegalArgumentException.class).hasMessageMatching(
"피연산자가 잘못되었습니다.");
}

@ParameterizedTest
@DisplayName("연산자로 수식이 끝나는 경우 검증")
@CsvSource(value = {"2 + 3 * 5 / 2:true", "2 +:false", "2 + 3 *:false"}, delimiter = ':')
void validateEndWithOperatorTest(String value, boolean expected) {
boolean actualResult = true;
try {
Input input = new Input(value);
InputValues inputValues = new InputValues(input);
int length = input.getValuesLength();

inputValues.validateEndWithOperator(length);
} catch (IllegalArgumentException ie) {
actualResult = false;
}
Assertions.assertThat(actualResult).isEqualTo(expected);
}

@ParameterizedTest
@DisplayName("연산자로 수식이 끝나는 경우 에러메세지 검증")
@ValueSource(strings = {"2 + 3 *", "2 +", "2 + 3 -"})
void validateEndWithOperatorTest2(String value) {
Assertions.assertThatThrownBy(() -> {
Input input = new Input(value);
InputValues inputValues = new InputValues(input);
int length = input.getValuesLength();

inputValues.validateEndWithOperator(length);
}).isInstanceOf(IllegalArgumentException.class).hasMessageMatching(
"연산자와 숫자가 맞지 않습니다.");
}

@ParameterizedTest
@DisplayName("연산자 enum 이 올바른지 검증")
@CsvSource(value = {"3:false", "+:true", "/:true",
"문자입력:false", "):false"}, delimiter = ':')
void getOperatorByStringTest(String value, boolean expected) {
boolean actualResult = true;
try {
Operator.getOperatorByString(value);
} catch (IllegalArgumentException e) {
actualResult = false;
}
Assertions.assertThat(actualResult).isEqualTo(expected);
}

@ParameterizedTest
@DisplayName("연산 결과가 올바른지 검증")
@CsvSource(value = {"+:1:2:3", "-:2:1:1", "*:7:4:28", "*:2:3:6"}, delimiter = ':')
void operateTest(String stringOperator, double num1, double num2, double expected) {
Operator operator = Operator.getOperatorByString(stringOperator);
double actualResult = operator.operate(num1, num2);

Assertions.assertThat(actualResult).isEqualTo(expected);
}

@ParameterizedTest
@DisplayName("0으로 나눈 경우 에러메세지 검증")
@CsvSource(value = {"0:0", "1:0", "2:0"}, delimiter = ':')
void divideByZeroTest(double num1, double num2) {
Assertions.assertThatThrownBy(() -> {
Operator operator = Operator.getOperatorByString("/");
operator.operate(num1, num2);
}).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("0으로 나눌 수 없습니다.");
}
}
Empty file removed src/test/java/empty.txt
Empty file.
45 changes: 45 additions & 0 deletions src/test/java/study/SetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package study;


import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.HashSet;
import java.util.Set;

public class SetTest {
private Set numbers;

@BeforeEach
void setUp() {
numbers = new HashSet<>();
numbers.add(1);
numbers.add(1);
numbers.add(2);
numbers.add(3);
}

// Test Case 구현
@Test
void size(){
int size = numbers.size();
Assertions.assertThat(size).isEqualTo(3);
}

@ParameterizedTest
@ValueSource(ints = {6,2,3})
void contains(int number) {
Assertions.assertThat(numbers.contains(number)).isTrue();
}

@ParameterizedTest
@CsvSource({"9,false", "2,false", "3,true"})
void contains2(int input, boolean expected) {
boolean result = numbers.contains(input);
Assertions.assertThat(result).isEqualTo(expected);
}
}
44 changes: 44 additions & 0 deletions src/test/java/study/StringTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package study;

import org.assertj.core.api.Assertions;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class StringTest {
@Test
void split() {
String value1 = "1,2";
String value2 = "1";

String[] result1 = value1.split(",");
Assertions.assertThat(result1).contains("1");
Assertions.assertThat(result1).contains("2");

String[] result2 = value2.split(",");
Assertions.assertThat(result2).containsExactly("1");
}

@Test
void substring() {
String value = "(1,2)";
String result = value.substring(1, 4);

Assertions.assertThat(result).isEqualTo("1,2");
}

@Test
@DisplayName("String 범위 확인")
void charAt() {
Assertions.assertThatThrownBy(()-> {
String value = "abc";
char value2 = value.charAt(4);
}).isInstanceOf(IndexOutOfBoundsException.class).hasMessageContaining("index out of range: 4");

Assertions.assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> {
String value = "abc";
char value2 = value.charAt(2);
}).withMessageMatching("String index out of range: \\d+");
}
}