Skip to content

Commit 7b2e12c

Browse files
committed
feat: 저장소 구현체, 메시지를 담을 클래스 구현
1 parent 490ce1d commit 7b2e12c

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

calculator/src/main/java/com/wonu606/calculator/CalculatorApp.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import com.wonu606.app.App;
44
import com.wonu606.calculator.storage.Persistence;
5+
import com.wonu606.calculator.storage.ResultStore;
56
import com.wonu606.calculator.strategy.Strategy;
67
import com.wonu606.io.Input;
78
import com.wonu606.io.Print;
9+
import com.wonu606.util.Message;
810
import java.io.IOException;
911
import java.util.ArrayList;
1012
import java.util.List;
@@ -13,7 +15,7 @@
1315
public class CalculatorApp implements App {
1416

1517
private final List<Strategy> strategies = new ArrayList<>();
16-
private final Persistence store;
18+
private final Persistence store = new ResultStore();
1719
Input input;
1820
Print printer;
1921

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.wonu606.calculator.storage;
2+
3+
import com.wonu606.calculator.model.CalculationResult;
4+
import com.wonu606.util.Message;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class ResultStore implements Persistence {
10+
11+
private final List<CalculationResult> store = new ArrayList<>();
12+
13+
@Override
14+
public void saveResult(CalculationResult calculationResult) {
15+
store.add(calculationResult);
16+
}
17+
18+
@Override
19+
public CalculationResult findResult(int sequence) {
20+
try {
21+
return store.get(sequence - 1);
22+
} catch (IndexOutOfBoundsException exception) {
23+
throw new IllegalArgumentException(Message.INVALID_ORDER);
24+
}
25+
}
26+
27+
@Override
28+
public List<CalculationResult> findAllResult() {
29+
return new ArrayList<>(store);
30+
}
31+
32+
@Override
33+
public void clear() {
34+
store.clear();
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.wonu606.util;
2+
3+
public abstract class Message {
4+
public static final String INVALID_ORDER = "잘못된 순번입니다.";
5+
public static final String INVALID_NUMBER = "잘못된 번호입니다.";
6+
}

calculator/src/test/java/com/wonu606/storage/ResultStoreTest.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55

66
import com.wonu606.calculator.model.CalculationResult;
77
import com.wonu606.calculator.storage.Persistence;
8+
import com.wonu606.calculator.storage.ResultStore;
9+
import com.wonu606.util.Message;
810
import java.util.ArrayList;
911
import java.util.List;
1012
import org.junit.jupiter.api.BeforeEach;
1113
import org.junit.jupiter.api.DisplayName;
1214
import org.junit.jupiter.api.Test;
1315

1416
public class ResultStoreTest {
15-
1617
@Test
1718
@DisplayName("저장")
1819
void testSave() {
1920
// given
20-
Persistence store;
21+
Persistence store = new ResultStore();
2122

2223
// when
2324
CalculationResult calculationResult = new CalculationResult("2 + 2", 4.0d);
@@ -31,7 +32,7 @@ void testSave() {
3132
@DisplayName("순서로 결과 찾기")
3233
void testFind() {
3334
// given
34-
Persistence store;
35+
Persistence store = new ResultStore();
3536

3637
// when
3738
CalculationResult saveResult = new CalculationResult("5 + 10", 15.0d);
@@ -46,7 +47,7 @@ void testFind() {
4647
@DisplayName("1 미만의 수로 찾은 경우 예외 발생")
4748
void testFindNumberLessThan1() {
4849
// given
49-
Persistence store;
50+
Persistence store = new ResultStore();
5051

5152
// then
5253
CalculationResult saveResult = new CalculationResult("5 + 10", 15.0d);
@@ -55,14 +56,14 @@ void testFindNumberLessThan1() {
5556
// then
5657
assertThatThrownBy(() -> store.findResult(0))
5758
.isInstanceOf(IllegalArgumentException.class)
58-
.hasMessageContaining("잘못된 순번입니다.");
59+
.hasMessageContaining(Message.INVALID_ORDER);
5960
}
6061

6162
@Test
6263
@DisplayName("사이즈 초과의 수로 찾은 경우 예외 발생")
6364
void testFindNumberMoreThanSize() {
6465
// given
65-
Persistence store;
66+
Persistence store = new ResultStore();
6667

6768
// when
6869
CalculationResult saveResult = new CalculationResult("5 + 10", 15.0d);
@@ -71,14 +72,14 @@ void testFindNumberMoreThanSize() {
7172
// then
7273
assertThatThrownBy(() -> store.findResult(store.findAllResult().size() + 1))
7374
.isInstanceOf(IllegalArgumentException.class)
74-
.hasMessageContaining("잘못된 순번입니다.");
75+
.hasMessageContaining(Message.INVALID_ORDER);
7576
}
7677

7778
@Test
7879
@DisplayName("모두 찾기")
7980
void testFindAll() {
8081
// given
81-
Persistence store;
82+
Persistence store = new ResultStore();
8283

8384
// when
8485
List<CalculationResult> results = new ArrayList<>();
@@ -100,7 +101,7 @@ void testFindAll() {
100101
@DisplayName("클리어")
101102
void testClear() {
102103
// given
103-
Persistence store;
104+
Persistence store = new ResultStore();
104105

105106
// when
106107
store.saveResult(new CalculationResult("11 + 6", 17.0d));

0 commit comments

Comments
 (0)