|
7 | 7 | import java.io.BufferedReader; |
8 | 8 | import java.io.FileReader; |
9 | 9 | import java.io.IOException; |
| 10 | +import java.util.Comparator; |
| 11 | +import java.util.Formatter; |
| 12 | +import java.util.List; |
10 | 13 |
|
11 | 14 | /** |
12 | 15 | * @author Grigoriy Lyashenko (Grog). |
@@ -81,4 +84,33 @@ public static String convertToHumanReadableTable(Cell[][] ingredients) { |
81 | 84 | return output.toString(); |
82 | 85 | } |
83 | 86 |
|
| 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 | + } |
84 | 116 | } |
0 commit comments