Now with a 22-recipe survey_dataset.xml.
Unit test shows an example of checking that Honey cake contains gluten, eggs and spice but not red meat, shellfish or poultry.
This commit is contained in:
2830
data/survey_dataset.xml
Normal file
2830
data/survey_dataset.xml
Normal file
File diff suppressed because it is too large
Load Diff
@@ -4,9 +4,30 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
|
|||||||
|
|
||||||
@XStreamAlias("ing")
|
@XStreamAlias("ing")
|
||||||
public class Ingredient {
|
public class Ingredient {
|
||||||
|
public enum TYPE { ALCOHOL, DAIRY, EGGS, FISH, GLUTEN, GRAIN, NUTS, PORK, POULTRY, RED_MEAT, SHELLFISH, SPICE, SUGAR}
|
||||||
|
|
||||||
private String item;
|
private String item;
|
||||||
|
|
||||||
public String getItem() {
|
public String getItem() {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isType(TYPE type) {
|
||||||
|
switch (type) {
|
||||||
|
case DAIRY :
|
||||||
|
return item.contains("margarine");
|
||||||
|
case EGGS :
|
||||||
|
return item.equals("egg") || item.equals("eggs");
|
||||||
|
case GLUTEN :
|
||||||
|
return item.contains("flour");
|
||||||
|
case RED_MEAT :
|
||||||
|
return item.contains("beef");
|
||||||
|
case SPICE :
|
||||||
|
return item.endsWith("cinnamon") || item.endsWith("nutmeg") || item.endsWith("cloves");
|
||||||
|
case SUGAR :
|
||||||
|
return item.endsWith("sugar");
|
||||||
|
default : //unknown ingredient, e.g. coffee, bananas, honey
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
33
src/net/woodyfolsom/cs6601/p2/IngredientDivision.java
Normal file
33
src/net/woodyfolsom/cs6601/p2/IngredientDivision.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package net.woodyfolsom.cs6601.p2;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
||||||
|
|
||||||
|
@XStreamAlias("ing-div")
|
||||||
|
public class IngredientDivision {
|
||||||
|
@XStreamImplicit
|
||||||
|
private List<Ingredient> ingredients;
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
public boolean contains(Ingredient.TYPE ingredientType) {
|
||||||
|
for (Ingredient ingredient : ingredients) {
|
||||||
|
if (ingredient.isType(ingredientType)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public Ingredient getIngredient(int index) {
|
||||||
|
return ingredients.get(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIngredientsSize() {
|
||||||
|
return ingredients.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
}
|
||||||
55
src/net/woodyfolsom/cs6601/p2/Ingredients.java
Normal file
55
src/net/woodyfolsom/cs6601/p2/Ingredients.java
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package net.woodyfolsom.cs6601.p2;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
||||||
|
|
||||||
|
@XStreamAlias("ingredients")
|
||||||
|
public class Ingredients {
|
||||||
|
@XStreamImplicit
|
||||||
|
private List<Ingredient> ingredients;
|
||||||
|
@XStreamImplicit
|
||||||
|
private List<IngredientDivision> ingDivs;
|
||||||
|
|
||||||
|
public boolean contains(Ingredient.TYPE ingredientType) {
|
||||||
|
for (Ingredient ingredient : ingredients) {
|
||||||
|
if (ingredient.isType(ingredientType)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (IngredientDivision ingDiv : ingDivs) {
|
||||||
|
if (ingDiv.contains(ingredientType)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Ingredient> getIngredients() {
|
||||||
|
return ingredients;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIngredients(List<Ingredient> ingredients) {
|
||||||
|
this.ingredients = ingredients;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<IngredientDivision> getIngDivs() {
|
||||||
|
return ingDivs;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object readResolve() {
|
||||||
|
if (ingDivs == null) {
|
||||||
|
ingDivs = new ArrayList<IngredientDivision>();
|
||||||
|
}
|
||||||
|
if (ingredients == null) {
|
||||||
|
ingredients = new ArrayList<Ingredient>();
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIngDivs(List<IngredientDivision> ingDivs) {
|
||||||
|
this.ingDivs = ingDivs;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,24 +1,17 @@
|
|||||||
package net.woodyfolsom.cs6601.p2;
|
package net.woodyfolsom.cs6601.p2;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
|
||||||
@XStreamAlias("recipe")
|
@XStreamAlias("recipe")
|
||||||
public class Recipe {
|
public class Recipe {
|
||||||
|
private Ingredients ingredients;
|
||||||
private List<Ingredient> ingredients;
|
|
||||||
private RecipeHead head;
|
private RecipeHead head;
|
||||||
|
|
||||||
public RecipeHead getHead() {
|
public RecipeHead getHead() {
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Ingredient getIngredient(int index) {
|
public Ingredients getIngredients() {
|
||||||
return ingredients.get(index);
|
return ingredients;
|
||||||
}
|
|
||||||
|
|
||||||
public int getIngredientsSize() {
|
|
||||||
return ingredients.size();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
36
test/net/woodyfolsom/cs6601/p2/SurveyDatasetReaderTest.java
Normal file
36
test/net/woodyfolsom/cs6601/p2/SurveyDatasetReaderTest.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package net.woodyfolsom.cs6601.p2;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import net.woodyfolsom.cs6601.p2.Ingredient.TYPE;
|
||||||
|
import net.woodyfolsom.cs6601.p2.Recipe;
|
||||||
|
import net.woodyfolsom.cs6601.p2.RecipeBook;
|
||||||
|
import net.woodyfolsom.cs6601.p2.RecipeBookReader;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class SurveyDatasetReaderTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReadSurveyDataset() {
|
||||||
|
RecipeBook recipeBook = RecipeBookReader.readRecipeBook(new File("data/survey_dataset.xml"));
|
||||||
|
assertNotNull(recipeBook);
|
||||||
|
assertThat(recipeBook.getSize(), is(equalTo(22)));
|
||||||
|
|
||||||
|
Recipe recipe = recipeBook.getRecipe(0);
|
||||||
|
System.out.println(recipe.getHead().getTitle());
|
||||||
|
assertThat(recipe.getHead().getTitle(), is(equalTo("Honey cake")));
|
||||||
|
assertTrue(recipe.getIngredients().contains(TYPE.EGGS));
|
||||||
|
assertTrue(recipe.getIngredients().contains(TYPE.GLUTEN));
|
||||||
|
assertTrue(recipe.getIngredients().contains(TYPE.SPICE));
|
||||||
|
assertFalse(recipe.getIngredients().contains(TYPE.RED_MEAT));
|
||||||
|
assertFalse(recipe.getIngredients().contains(TYPE.POULTRY));
|
||||||
|
assertFalse(recipe.getIngredients().contains(TYPE.SHELLFISH));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user