Skip to content

Commit 828bcb4

Browse files
authored
Merge pull request #3 from VadimKlindukhov/master
Formatter for output files basics.
2 parents 3b1bd20 + 015e4c7 commit 828bcb4

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/main/java/com/google/hashcode/entity/Cell.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* @author Grigoriy Lyashenko (Grog).
77
*/
88
public class Cell {
9-
final int x;
10-
final int y;
11-
final Ingredient ingredient;
9+
public final int x;
10+
public final int y;
11+
public final Ingredient ingredient;
1212

1313
public Cell(int x, int y, Ingredient ingredient) {
1414
this.x = x;

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import java.io.BufferedReader;
88
import java.io.FileReader;
99
import java.io.IOException;
10+
import java.util.Comparator;
11+
import java.util.Formatter;
12+
import java.util.List;
1013

1114
/**
1215
* @author Grigoriy Lyashenko (Grog).
@@ -81,4 +84,33 @@ public static String convertToHumanReadableTable(Cell[][] ingredients) {
8184
return output.toString();
8285
}
8386

87+
88+
/**
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();
115+
}
84116
}

0 commit comments

Comments
 (0)