Skip to content

Commit 3b412ae

Browse files
authored
Merge pull request #5 from jarc0der/master
Class Formatter issue `create a Formatter for output file`
2 parents d03a831 + 3cd9e0f commit 3b412ae

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.google.hashcode.utils;
2+
3+
4+
import java.io.BufferedWriter;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
import java.util.ArrayList;
8+
import java.util.Comparator;
9+
import java.util.List;
10+
11+
import com.google.hashcode.entity.Cell;
12+
import com.google.hashcode.entity.Ingredient;
13+
14+
/**
15+
*
16+
* @author Tuzhanskyi (jarc0der)
17+
*
18+
*/
19+
20+
public class Formatter {
21+
private List<List<Cell>> cellList;
22+
private final Comparator<Cell> comp = ((thisCell, thatCell) -> {
23+
if(thisCell.x != thatCell.x){
24+
return Integer.compare(thisCell.x, thatCell.x);
25+
}else{
26+
return Integer.compare(thisCell.y, thatCell.y);
27+
}
28+
});
29+
30+
public Formatter(List<List<Cell>> list) {
31+
this.cellList = list;
32+
}
33+
34+
public Formatter() {
35+
this(hardInit());
36+
// this.cellList = hardInit();
37+
}
38+
39+
public void writeResultsToFile(String fileName) throws IOException {
40+
41+
try (BufferedWriter out = new BufferedWriter(new FileWriter(fileName))) {
42+
out.write("" + cellList.size());
43+
out.newLine();
44+
for (List<Cell> slice : cellList) {
45+
Cell maxCell = slice.stream().max(comp).get();
46+
Cell minCell = slice.stream().min(comp).get();
47+
48+
String coords = getCoordsLineForSlice(minCell, maxCell);
49+
out.write(coords);
50+
out.newLine();
51+
}
52+
53+
//check: autoflush?
54+
out.flush();
55+
}
56+
57+
}
58+
59+
private String getCoordsLineForSlice(Cell min, Cell max) {
60+
StringBuilder sb = new StringBuilder();
61+
sb.append(min.y).append(" " + min.x).append(" " + max.y).append(" " + max.x);
62+
return sb.toString();
63+
}
64+
65+
private static List<List<Cell>> hardInit() {
66+
List<List<Cell>> resultList = new ArrayList<>();
67+
List<Cell> slice0 = new ArrayList<>();
68+
List<Cell> slice1 = new ArrayList<>();
69+
List<Cell> slice2 = new ArrayList<>();
70+
71+
slice0.add(new Cell(0, 0, Ingredient.TOMATO));
72+
slice0.add(new Cell(0, 1, Ingredient.TOMATO));
73+
slice0.add(new Cell(0, 2, Ingredient.TOMATO));
74+
slice0.add(new Cell(1, 0, Ingredient.TOMATO));
75+
slice0.add(new Cell(1, 1, Ingredient.MUSHROOM));
76+
slice0.add(new Cell(1, 2, Ingredient.TOMATO));
77+
78+
slice1.add(new Cell(2, 0, Ingredient.TOMATO));
79+
slice1.add(new Cell(2, 1, Ingredient.MUSHROOM));
80+
slice1.add(new Cell(2, 2, Ingredient.TOMATO));
81+
82+
slice2.add(new Cell(3, 0, Ingredient.TOMATO));
83+
slice2.add(new Cell(3, 1, Ingredient.MUSHROOM));
84+
slice2.add(new Cell(3, 2, Ingredient.TOMATO));
85+
slice2.add(new Cell(4, 0, Ingredient.TOMATO));
86+
slice2.add(new Cell(4, 1, Ingredient.TOMATO));
87+
slice2.add(new Cell(4, 2, Ingredient.TOMATO));
88+
89+
resultList.add(slice0);
90+
resultList.add(slice1);
91+
resultList.add(slice2);
92+
93+
return resultList;
94+
}
95+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.google.hashcode.utils;
2+
3+
import com.google.hashcode.entity.Cell;
4+
import com.google.hashcode.entity.Ingredient;
5+
import org.junit.*;
6+
import static org.junit.Assert.*;
7+
8+
9+
public class FormatterTest {
10+
private Formatter outFormatter = new Formatter();
11+
12+
@Test
13+
public void testOutputForOneSlice(){
14+
String out = outFormatter.getCoordsLineForSlice(new Cell(0, 0, Ingredient.MUSHROOM), new Cell(1,1, Ingredient.MUSHROOM));
15+
assertEquals("0 0 1 1", out);
16+
}
17+
18+
}

0 commit comments

Comments
 (0)