Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch03] Calculator with Template/Callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Sep 2, 2017
1 parent 670cc5d commit 2e6c24b
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/main/java/ch03/springbook/calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ch03.springbook.calculator;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Calculator {

public Integer calcSum(String filePath) throws IOException {

return lineReadTemplate(
filePath,
new LineCallBack() {
@Override
public Integer doSomethingWithLine(String line, Integer value) {
return value + Integer.valueOf(line);
}
},
0
);
}

public Integer calcMultiply(String filePath) throws IOException {

return lineReadTemplate(
filePath,
new LineCallBack() {
@Override
public Integer doSomethingWithLine(String line, Integer value) {
return value * Integer.valueOf(line);
}
},
1
);
}

public Integer lineReadTemplate(String filePath, LineCallBack lineCallBack, int initVal) throws IOException {

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader(filePath));

Integer res = initVal;
String line = null;

while ((line = br.readLine()) != null) {
res = lineCallBack.doSomethingWithLine(line, res);
}

return res;

} catch (IOException e) {
throw e;

} finally {

if (br != null) {
try {
br.close();
} catch (IOException e) {
throw e;
}
}
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/ch03/springbook/calculator/LineCallBack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ch03.springbook.calculator;

public interface LineCallBack {
Integer doSomethingWithLine(String line, Integer value);
}
4 changes: 4 additions & 0 deletions src/main/resources/numbers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
2
3
4
32 changes: 32 additions & 0 deletions src/test/java/ch03/springbook/calculator/CalcSumTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ch03.springbook.calculator;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

public class CalcSumTest {

private Calculator calculator;
private String numFilePath;

@Before
public void setUp() {
this.calculator = new Calculator();
this.numFilePath = getClass().getResource("/numbers.txt").getPath();
}

@Test
public void sumOfNumbers() throws IOException {
assertThat(calculator.calcSum(numFilePath), is(10));
}

@Test
public void multiplyOfNumbers() throws IOException {
assertThat(calculator.calcMultiply(numFilePath), is(24));
}
}

0 comments on commit 2e6c24b

Please sign in to comment.