Skip to content

Commit 7fa8135

Browse files
authored
Merge pull request #6 from LyashenkoGS/createAFormater
Refactored output formatter, create an end-to-end test
2 parents 3b412ae + f13882c commit 7fa8135

File tree

8 files changed

+110
-149
lines changed

8 files changed

+110
-149
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: java
2+
jdk:
3+
- oraclejdk8
4+
script: mvn clean install

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

src/main/java/com/google/hashcode/App.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.google.hashcode;
22

33
import com.google.hashcode.entity.Cell;
4-
import com.google.hashcode.entity.Ingredient;
54
import com.google.hashcode.entity.Pizza;
65
import com.google.hashcode.utils.IoUtils;
76

src/main/java/com/google/hashcode/utils/Formatter.java

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/main/java/com/google/hashcode/utils/IoUtils.java

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77
import java.io.BufferedReader;
88
import java.io.FileReader;
99
import java.io.IOException;
10+
import java.io.PrintWriter;
11+
import java.nio.file.Files;
12+
import java.nio.file.Paths;
1013
import java.util.Comparator;
1114
import java.util.Formatter;
1215
import java.util.List;
1316

1417
/**
1518
* @author Grigoriy Lyashenko (Grog).
19+
* @author github.com/VadimKlindukhov skype: kv_vadim
1620
*/
1721
public class IoUtils {
22+
private IoUtils() {
23+
}
1824

1925
/**
2026
* Parses given input file to a 2d pizza cells array
@@ -66,7 +72,6 @@ public static SliceInstruction parseSliceInstructions(String file) throws IOExce
6672
}
6773
}
6874

69-
7075
/**
7176
* Converts given pizza cells 2d array to human readable string representation
7277
*
@@ -84,33 +89,45 @@ public static String convertToHumanReadableTable(Cell[][] ingredients) {
8489
return output.toString();
8590
}
8691

87-
8892
/**
89-
* this method formats data for output to file
90-
* @see issue 1 description
91-
* @see task description
92-
*
93-
* @author github.com/VadimKlindukhov skype: kv_vadim
94-
* @param list — inner representation of pizza
95-
* @return long String that contains output data
96-
*/
97-
public static String outputFormat(List<List<Cell>> list){
98-
Comparator<Cell> cellComparator = (Cell c1, Cell c2) ->{
99-
if(c1.x != c2.x){
100-
return Integer.compare(c1.x, c2.x);
101-
} else
102-
return Integer.compare(c1.y, c2.y);
103-
};
104-
StringBuffer sb = new StringBuffer();
105-
Formatter textFormatter = new Formatter(sb);
106-
textFormatter.format("%d%n", list.size());
107-
Cell min, max;
108-
for(List<Cell> innerList : list){
109-
min = innerList.stream().min(cellComparator).get();
110-
max = innerList.stream().max(cellComparator).get();
111-
textFormatter.format("%d %d %d %d%n", min.y, min.x, max.y, max.x);
112-
}
113-
textFormatter.close();
114-
return sb.toString();
93+
* Formats data from list of slices to the required output format
94+
*
95+
* @param list inner representation of pizza
96+
* @return String that contains output data
97+
*/
98+
public static String parseSlices(List<List<Cell>> list) {
99+
Comparator<Cell> cellComparator = (Cell c1, Cell c2) -> {
100+
if (c1.x != c2.x) {
101+
return Integer.compare(c1.x, c2.x);
102+
} else
103+
return Integer.compare(c1.y, c2.y);
104+
};
105+
StringBuilder sb = new StringBuilder();
106+
Formatter textFormatter = new Formatter(sb);
107+
textFormatter.format("%d%n", list.size());
108+
Cell min, max;
109+
for (List<Cell> innerList : list) {
110+
min = innerList.stream().min(cellComparator).get();
111+
max = innerList.stream().max(cellComparator).get();
112+
textFormatter.format("%d %d %d %d%n", min.y, min.x, max.y, max.x);
113+
}
114+
textFormatter.close();
115+
return sb.toString().trim();
116+
}
117+
118+
public static void writeToFile(String fileName, String outputDate) throws IOException {
119+
try (PrintWriter out = new PrintWriter(fileName)) {
120+
out.println(outputDate);
121+
}
122+
}
123+
124+
public static String readFromFile(String fileName) throws IOException {
125+
List<String> lines = Files.readAllLines(Paths.get(fileName));
126+
StringBuilder stringBuilder = new StringBuilder();
127+
lines.forEach(
128+
line -> stringBuilder.append(line).append("\n")
129+
);
130+
return stringBuilder.toString();
115131
}
116132
}
133+

src/test/java/com/google/hashcode/utils/FormatterTest.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/test/java/com/google/hashcode/utils/IoUtilsTest.java

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import org.junit.Test;
66

77
import java.io.IOException;
8+
import java.net.URISyntaxException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Paths;
11+
import java.util.ArrayList;
12+
import java.util.List;
813

914
import static org.junit.Assert.assertEquals;
1015
import static org.junit.Assert.assertFalse;
@@ -13,21 +18,66 @@
1318
* @author Grigoriy Lyashenko (Grog).
1419
*/
1520
public class IoUtilsTest {
16-
private final String examplePizzaFile = "inputDataSets/example.in";
21+
private static final String TEST_OUTPUT_FILE = "testOutput.txt";
22+
private static final String PARAGON_OUTPUT_EXAMPLE_FILE = "src/test/resources/paragonOutputExample.txt";
23+
private static final String EXAMPLE_PIZZA_FILE = "inputDataSets/example.in";
24+
25+
private static List<List<Cell>> createSlicesForParagonOutputExample() {
26+
List<List<Cell>> resultList = new ArrayList<>();
27+
List<Cell> slice0 = new ArrayList<>();
28+
List<Cell> slice1 = new ArrayList<>();
29+
List<Cell> slice2 = new ArrayList<>();
30+
31+
slice0.add(new Cell(0, 0, Ingredient.TOMATO));
32+
slice0.add(new Cell(0, 1, Ingredient.TOMATO));
33+
slice0.add(new Cell(0, 2, Ingredient.TOMATO));
34+
slice0.add(new Cell(1, 0, Ingredient.TOMATO));
35+
slice0.add(new Cell(1, 1, Ingredient.MUSHROOM));
36+
slice0.add(new Cell(1, 2, Ingredient.TOMATO));
37+
38+
slice1.add(new Cell(2, 0, Ingredient.TOMATO));
39+
slice1.add(new Cell(2, 1, Ingredient.MUSHROOM));
40+
slice1.add(new Cell(2, 2, Ingredient.TOMATO));
41+
42+
slice2.add(new Cell(3, 0, Ingredient.TOMATO));
43+
slice2.add(new Cell(3, 1, Ingredient.MUSHROOM));
44+
slice2.add(new Cell(3, 2, Ingredient.TOMATO));
45+
slice2.add(new Cell(4, 0, Ingredient.TOMATO));
46+
slice2.add(new Cell(4, 1, Ingredient.TOMATO));
47+
slice2.add(new Cell(4, 2, Ingredient.TOMATO));
48+
49+
resultList.add(slice0);
50+
resultList.add(slice1);
51+
resultList.add(slice2);
52+
53+
return resultList;
54+
}
1755

1856
@Test
1957
public void parseExampleInput() throws IOException {
20-
Cell[][] ingredients = IoUtils.parsePizza(examplePizzaFile);
21-
assertEquals("We expect" + examplePizzaFile + "contains 3 rows", 3, ingredients.length);
22-
assertEquals("We expect" + examplePizzaFile + "contains 5 columns", 5, ingredients[0].length);
58+
Cell[][] ingredients = IoUtils.parsePizza(EXAMPLE_PIZZA_FILE);
59+
assertEquals("We expect" + EXAMPLE_PIZZA_FILE + "contains 3 rows", 3, ingredients.length);
60+
assertEquals("We expect" + EXAMPLE_PIZZA_FILE + "contains 5 columns", 5, ingredients[0].length);
2361
assertFalse("We expect no null value in ingredients", IoUtils.convertToHumanReadableTable(ingredients).contains("null"));
2462
}
2563

2664
@Test
2765
public void parseExampleSliceInstructions() throws IOException {
2866
assertEquals("We expect min 1 ingredient per slice", 1,
29-
IoUtils.parseSliceInstructions(examplePizzaFile).getMinNumberOfIngredientPerSlice().intValue());
67+
IoUtils.parseSliceInstructions(EXAMPLE_PIZZA_FILE).getMinNumberOfIngredientPerSlice().intValue());
3068
assertEquals("We expect max 6 cells per slice", 6,
31-
IoUtils.parseSliceInstructions(examplePizzaFile).getMaxNumberOfCellsPerSlice().intValue());
69+
IoUtils.parseSliceInstructions(EXAMPLE_PIZZA_FILE).getMaxNumberOfCellsPerSlice().intValue());
70+
}
71+
72+
@Test
73+
public void parseSlicesToOutputFormat() throws IOException, URISyntaxException {
74+
//Given a list of slices
75+
List<List<Cell>> slicesForParagonOutputExample = createSlicesForParagonOutputExample();
76+
//Then parse slices according to the output format
77+
String outputDate = IoUtils.parseSlices(slicesForParagonOutputExample);
78+
IoUtils.writeToFile(TEST_OUTPUT_FILE, outputDate);
79+
assertEquals(IoUtils.readFromFile(PARAGON_OUTPUT_EXAMPLE_FILE), IoUtils.readFromFile(TEST_OUTPUT_FILE));
80+
//clean the file under the test
81+
Files.deleteIfExists(Paths.get(TEST_OUTPUT_FILE));
3282
}
3383
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
3
2+
0 0 2 1
3+
0 2 2 2
4+
0 3 2 4

0 commit comments

Comments
 (0)