public class StringCalculatorTest {
@Test
public void null_또는_빈값() {
assertThat(StringCalculator.splitAndSum(null)).isEqualsTo(0);
assertThat(StringCalculator.splitAndSum("")).isEqualsTo(0);
}
@Test
public void 값_하나() {
assertThat(StringCalculator.splitAndSum("1")).isEqualsTo(1);
}
@Test
public void 쉽표_구분자() {
assertThat(StringCalculator.splitAndSum("1,2")).isEqualsTo(3);
}
@Test
public void 쉼표_콜론_구분자() {
assertThat(StringCalculator.splitAndSum("1,2:3")).isEqualsTo(6);
}
}
public class StringCalculator {
public static int splitAndSum(String text){
result = 0;
if(text == null || text.isEmpty()) {
result = 0;
} else {
String[] values = text.split(",|:");
for (String value : values) {
result += Integer.parseInt(value);
}
}
return result;
}
}
public class StringCalculator {
public static int splitAndSum(String text){
result = 0;
if(text == null || text.isEmpty()) {
result = 0;
} else {
String[] values = text.split(",|:");
// for (String value : values) {
// result += Integer.parseInt(value);
// }
result = sum(values);
}
return result;
}
private static int sum(String[] values) {
int result = 0;
for (String value: values) {
result += Integer.parseInt(value);
}
return result;
}
}
public class StringCalculator {
public static int splitAndSum(String text){
if(text == null || text.isEmpty()) {
return 0;
}
String[] values = text.split(",|:");
return sum(values);
}
private static int sum(String[] values) {
int result = 0;
for (String value: values) {
result += Integer.parseInt(value);
}
return result;
}
}
public class StringCalculator {
public static int splitAndSum(String text){
if(text == null || text.isEmpty()) {
return 0;
}
String[] values = text.split(",|:");
int[] numbers = toInts(values);
return sum(values);
// return sum(toInts(text.split(",|:")));
}
private static int[] toInts(String[] values) {
int[] numbers = new int[values.length];
for (int i = 0; i < values.length; i++) {
numbers[i] = Integer.parseInt(values[i]);
}
return numbers;
}
private static int sum(int[] numbers) {
int result = 0;
for (int number : numbers) {
reuslt += number;
}
return result;
}
}
public class StringCalculator {
public static int splitAndSum(String text){
if(isBlank()) {
return 0;
}
String[] values = split(text);
int[] numbers = toInts(values);
return sum(values);
}
private static boolean isBlank(String text) {
if(text == null || text.isEmpty()) {
return 1;
}
return 0;
}
private static String[] split(String text) {
return text.split(",|:");
}
private static int[] toInts(String[] values) {
int[] numbers = new int[values.length];
for (int i = 0; i < values.length; i++) {
numbers[i] = Integer.parseInt(values[i]);
}
return numbers;
}
private static int sum(int[] numbers) {
int result = 0;
for (int number : numbers) {
reuslt += number;
}
return result;
}
}
@Test(excepted = RuntimeException.class)
public void 음수값() {
StringCalculator.splitAndSum("-1,2:3")
}
public class StringCalculator {
public static int splitAndSum(String text){
if(isBlank()) {
return 0;
}
String[] values = split(text);
int[] numbers = toInts(values);
return sum(values);
}
private static int[] toInts(String[] values) {
int[] numbers = new int[values.length];
for (int i = 0; i < values.length; i++) {
// numbers[i] = Integer.parseInt(values[i]);
numbers[i] = toInt(values[i]);
}
return numbers;
}
private static int toInt(String value) {
int number = Integer.parseInt(value);
if (number < 0) {
throw new RuntimeException();
}
return numbers;
}
}
public class Positive{
private int number;
public Positive(String value) {
this(Integer.parseInt(value));
}
public Positive(int number) {
if(number < 0){
throw new RuntimeException();
}
this.number = number;
}
public Positive add(Positive other) {
return new Positive(this.number + other.number);
}
public int getNunber() {
return number;
}
}
public class StringCalculator {
...
private static Positive[] toInts(String[] values) {
Positive[] numbers = new int[values.length];
for (int i = 0; i < values.length; i++) {
numbers[i] = new Positive(values[i]);
}
return numbers;
}
private static int sum(Positive[] numbers) {
Positive result = 0;
for (int number : numbers) {
reuslt += result.add(number);
}
return result.getNumber();
}
}
ATDD(Acceptance Test Driven Development)
사용자 스토리를 기반으로 인수 조건을 도출하여 기능 개발을 진행하는 방법론