Skip to content

Commit 67ac80b

Browse files
committed
Implement domain entities, parsing input files scratch
1 parent 41b5196 commit 67ac80b

File tree

7 files changed

+230
-3
lines changed

7 files changed

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

3+
import com.google.hashcode.entity.Pizza;
4+
import com.google.hashcode.entity.PizzaCell;
5+
import com.google.hashcode.utils.IoUtils;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
10+
311
public class App {
4-
public static void main(String[] args) {
5-
System.out.println("GoogleHashCode2017");
12+
public static void main(String[] args) throws IOException {
13+
String exampleInputFile = "inputDataSets/example.in";
14+
PizzaCell[][] pizzaCells = IoUtils.parsePizza(exampleInputFile);
15+
Pizza pizza = new Pizza(new File(exampleInputFile), pizzaCells, IoUtils.parseSliceInstructions(exampleInputFile));
16+
17+
System.out.println("GoogleHashCode2017! Pizza task");
18+
System.out.print(pizza);
19+
620
}
21+
722
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.google.hashcode.entity;
2+
3+
import com.google.hashcode.utils.IoUtils;
4+
5+
import java.io.File;
6+
7+
/**
8+
* Represents an immutable pizza
9+
*
10+
* @author Grigoriy Lyashenko (Grog).
11+
*/
12+
public class Pizza {
13+
14+
private final File input;
15+
private final PizzaCell[][] pizzaCells;
16+
private final SliceInstruction sliceInstruction;
17+
18+
public Pizza(File input, PizzaCell[][] pizzaCells, SliceInstruction sliceInstruction) {
19+
this.input = input;
20+
this.pizzaCells = pizzaCells;
21+
this.sliceInstruction = sliceInstruction;
22+
}
23+
24+
public File getInput() {
25+
return input;
26+
}
27+
28+
public PizzaCell[][] getPizzaCells() {
29+
return pizzaCells;
30+
}
31+
32+
33+
@Override
34+
public String toString() {
35+
return input.toString() +
36+
"\n" + IoUtils.convertToHumanReadableTable(pizzaCells) +
37+
"\n" + sliceInstruction.toString();
38+
}
39+
40+
41+
public SliceInstruction getSliceInstruction() {
42+
return sliceInstruction;
43+
}
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.google.hashcode.entity;
2+
3+
/**
4+
* Represents possible pizza cell types
5+
*
6+
* @author Grigoriy Lyashenko (Grog).
7+
*/
8+
public enum PizzaCell {
9+
MUSHROOM("M"), TOMATO("T");
10+
private final String type;
11+
12+
PizzaCell(final String type) {
13+
this.type = type;
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return type;
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.google.hashcode.entity;
2+
3+
/**
4+
* Instructions how to slice a pizza
5+
*
6+
* @author Grigoriy Lyashenko (Grog).
7+
*/
8+
public class SliceInstruction {
9+
private final Integer minNumberOfIngredientPerSlice;
10+
private final Integer maxNumberOfCellsPerSlice;
11+
12+
public SliceInstruction(Integer minNumberOfIngredientPerSlice, Integer maxNumberOfIngredientPerSlice) {
13+
this.minNumberOfIngredientPerSlice = minNumberOfIngredientPerSlice;
14+
this.maxNumberOfCellsPerSlice = maxNumberOfIngredientPerSlice;
15+
}
16+
17+
public Integer getMinNumberOfIngredientPerSlice() {
18+
return minNumberOfIngredientPerSlice;
19+
}
20+
21+
public Integer getMaxNumberOfCellsPerSlice() {
22+
return maxNumberOfCellsPerSlice;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "SliceInstructions: \n" +
28+
"min " + minNumberOfIngredientPerSlice + " ingredient per slice, " +
29+
"max " + maxNumberOfCellsPerSlice + " cells per slice ";
30+
}
31+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.google.hashcode.utils;
2+
3+
import com.google.hashcode.entity.PizzaCell;
4+
import com.google.hashcode.entity.SliceInstruction;
5+
6+
import java.io.BufferedReader;
7+
import java.io.FileReader;
8+
import java.io.IOException;
9+
10+
/**
11+
* @author Grigoriy Lyashenko (Grog).
12+
*/
13+
public class IoUtils {
14+
15+
/**
16+
* Parses given input file to a 2d pizza cells array
17+
*
18+
* @param file input file
19+
* @return 2d array representing a pizza
20+
* @throws IOException parsing fail
21+
*/
22+
public static PizzaCell[][] parsePizza(String file) throws IOException {
23+
try (FileReader fileReader = new FileReader(file)) {
24+
BufferedReader br = new BufferedReader(fileReader);
25+
//parse the first line
26+
String[] headerTokens = br.readLine().split(" ");
27+
int rowsCount = Integer.parseInt(headerTokens[0]);
28+
int columnsCount = Integer.parseInt(headerTokens[1]);
29+
//declare a pizza cells array
30+
PizzaCell[][] pizzaCells = new PizzaCell[rowsCount][columnsCount];
31+
int row = 0;
32+
String fileLine;
33+
while ((fileLine = br.readLine()) != null) {
34+
for (int i = 0; i < fileLine.length(); i++) {
35+
Character literal = fileLine.charAt(i);
36+
if (literal.toString().equals(PizzaCell.TOMATO.toString())) {
37+
pizzaCells[row][i] = PizzaCell.TOMATO;
38+
} else if (literal.toString().equals(PizzaCell.MUSHROOM.toString())) {
39+
pizzaCells[row][i] = PizzaCell.MUSHROOM;
40+
}
41+
}
42+
row++;
43+
}
44+
return pizzaCells;
45+
}
46+
}
47+
48+
/**
49+
* Produces SliceInstructions based on given input data set
50+
*
51+
* @param file input data set
52+
* @return slice instructions
53+
* @throws IOException file reading error
54+
*/
55+
public static SliceInstruction parseSliceInstructions(String file) throws IOException {
56+
try (FileReader fileReader = new FileReader(file)) {
57+
BufferedReader br = new BufferedReader(fileReader);
58+
String[] headerTokens = br.readLine().split(" ");
59+
int minNumberOfIngredientPerSlice = Integer.parseInt(headerTokens[2]);
60+
int maxNumberOfCellsPerSlice = Integer.parseInt(headerTokens[3]);
61+
return new SliceInstruction(minNumberOfIngredientPerSlice, maxNumberOfCellsPerSlice);
62+
}
63+
}
64+
65+
66+
/**
67+
* Converts given pizza cells 2d array to human readable string representation
68+
*
69+
* @param pizzaCells given array
70+
* @return table like String representation
71+
*/
72+
public static String convertToHumanReadableTable(PizzaCell[][] pizzaCells) {
73+
StringBuilder output = new StringBuilder();
74+
for (Enum[] row : pizzaCells) {
75+
for (Enum cell : row) {
76+
output.append(cell).append(" ");
77+
}
78+
output.append("\n");
79+
}
80+
return output.toString();
81+
}
82+
83+
}

src/test/java/com/google/hashcode/AppTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import org.junit.Test;
44

5+
import java.io.IOException;
6+
57
public class AppTest {
68

79
@Test
8-
public void main() {
10+
public void main() throws IOException {
911
App.main(new String[0]);
1012
}
1113

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.google.hashcode.utils;
2+
3+
import com.google.hashcode.entity.PizzaCell;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertFalse;
10+
11+
/**
12+
* @author Grigoriy Lyashenko (Grog).
13+
*/
14+
public class IoUtilsTest {
15+
private final String examplePizzaFile = "inputDataSets/example.in";
16+
17+
@Test
18+
public void parseExampleInput() throws IOException {
19+
PizzaCell[][] pizzaCells = IoUtils.parsePizza(examplePizzaFile);
20+
assertEquals("We expect" + examplePizzaFile + "contains 3 rows", 3, pizzaCells.length);
21+
assertEquals("We expect" + examplePizzaFile + "contains 5 columns", 5, pizzaCells[0].length);
22+
assertFalse("We expect no null value in pizzaCells", IoUtils.convertToHumanReadableTable(pizzaCells).contains("null"));
23+
}
24+
25+
@Test
26+
public void parseExampleSliceInstructions() throws IOException {
27+
assertEquals("We expect min 1 ingredient per slice", 1,
28+
IoUtils.parseSliceInstructions(examplePizzaFile).getMinNumberOfIngredientPerSlice().intValue());
29+
assertEquals("We expect max 6 cells per slice", 6,
30+
IoUtils.parseSliceInstructions(examplePizzaFile).getMaxNumberOfCellsPerSlice().intValue());
31+
}
32+
}

0 commit comments

Comments
 (0)