Skip to content

Commit 0970138

Browse files
committed
pizzaHumanReadableOutput. Pizza toString, without indexes and catering empty elements
1 parent fb01518 commit 0970138

File tree

5 files changed

+33
-28
lines changed

5 files changed

+33
-28
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ public int hashCode() {
4141
public int getX() {
4242
return x;
4343
}
44+
45+
public int getY() {
46+
return y;
47+
}
4448
}

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

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

33
import java.io.File;
4+
import java.util.Comparator;
45
import java.util.List;
56
import java.util.Optional;
67

@@ -51,9 +52,22 @@ public SliceInstruction getSliceInstruction() {
5152

5253
@Override
5354
public String toString() {
54-
return input.toString() +
55-
//TODO fix human readable output "\n" + IoUtils.convertToHumanReadableTable(cells) +
56-
"\n" + sliceInstruction.toString();
55+
return input.toString()
56+
+ "\n" + sliceInstruction.toString()
57+
+ "\n" + outputCellsArray();
58+
}
59+
60+
private String outputCellsArray() {
61+
StringBuilder stringBuilder = new StringBuilder();
62+
int columnsCount = cells.stream().max(Comparator.comparingInt(Cell::getX)).get().getX();
63+
int rowsCount = cells.stream().max(Comparator.comparingInt(Cell::getY)).get().getY();
64+
for (int i = 0; i < rowsCount + 1; i++) {
65+
for (int j = 0; j < columnsCount + 1; j++) {
66+
stringBuilder.append(this.getCell(i, j).toString()).append(" ");
67+
}
68+
stringBuilder.append("\n");
69+
}
70+
return stringBuilder.toString();
5771
}
5872

5973
}

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,6 @@ public static SliceInstruction parseSliceInstructions(String file) throws IOExce
7272
}
7373
}
7474

75-
/**
76-
* Converts given pizza cells 2d array to human readable string representation
77-
*
78-
* @param ingredients given array
79-
* @return table like String representation
80-
*/
81-
public static String convertToHumanReadableTable(Cell[][] ingredients) {
82-
StringBuilder output = new StringBuilder();
83-
for (Cell[] row : ingredients) {
84-
for (Cell cell : row) {
85-
output.append(cell).append(" ");
86-
}
87-
output.append("\n");
88-
}
89-
return output.toString();
90-
//TODO reimplement for the pizza
91-
}
92-
9375
/**
9476
* Formats data from list of slices to the required output format
9577
*

src/test/java/com/google/hashcode/entity/PizzaTest.java

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

77
import java.io.File;
8+
import java.io.IOException;
89

910
import static com.google.hashcode.utils.InputFiles.*;
1011
import static org.junit.Assert.assertEquals;
@@ -26,4 +27,10 @@ public void getCellException() throws Exception {
2627
pizza.getCell(100500, 0);
2728
}
2829

30+
@Test
31+
public void testToString() throws IOException {
32+
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
33+
System.out.println(pizza);
34+
}
35+
2936
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Arrays;
1313
import java.util.List;
1414

15+
import static junit.framework.TestCase.assertFalse;
1516
import static org.junit.Assert.assertEquals;
1617

1718
/**
@@ -33,11 +34,11 @@ private static List<Slice> createSlicesForParagonOutputExample() {
3334
slice0.cells.add(new Cell(0, 1, Ingredient.TOMATO));
3435
slice0.cells.add(new Cell(1, 1, Ingredient.MUSHROOM));
3536
slice0.cells.add(new Cell(2, 1, Ingredient.TOMATO));
36-
37+
3738
slice1.cells.add(new Cell(0, 2, Ingredient.TOMATO));
3839
slice1.cells.add(new Cell(1, 2, Ingredient.MUSHROOM));
3940
slice1.cells.add(new Cell(2, 2, Ingredient.TOMATO));
40-
41+
4142
slice2.cells.add(new Cell(0, 3, Ingredient.TOMATO));
4243
slice2.cells.add(new Cell(1, 3, Ingredient.MUSHROOM));
4344
slice2.cells.add(new Cell(2, 3, Ingredient.TOMATO));
@@ -50,11 +51,8 @@ private static List<Slice> createSlicesForParagonOutputExample() {
5051

5152
@Test
5253
public void parseExampleInput() throws IOException {
53-
/* List<Cell> ing = IoUtils.parsePizza(EXAMPLE_PIZZA_FILE);
54-
assertEquals("We expect" + EXAMPLE_PIZZA_FILE + "contains 3 rows", 3, ingredients.size);
55-
assertEquals("We expect" + EXAMPLE_PIZZA_FILE + "contains 5 columns", 5, ingredients[0].length);
56-
assertFalse("We expect no null value in ingredients", IoUtils.convertToHumanReadableTable(ingredients).contains("null"));
57-
*/
54+
List<Cell> ing = IoUtils.parsePizza(EXAMPLE_PIZZA_FILE);
55+
5856
}
5957

6058
@Test

0 commit comments

Comments
 (0)