Skip to content
Snippets Groups Projects
Commit eb177b1b authored by HSoreide's avatar HSoreide
Browse files

Refactor simplified class for ingredients at hand

parent 3ae78945
No related branches found
No related tags found
3 merge requests!42Hs frontend recipes,!41Hs frontend recipes,!36Hs rewrite file handling for recipes and ingredients
...@@ -10,7 +10,6 @@ import no.ntnu.idatt1002.demo.data.recipes.*; ...@@ -10,7 +10,6 @@ import no.ntnu.idatt1002.demo.data.recipes.*;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
...@@ -32,10 +31,10 @@ public class RecipeController implements Initializable { ...@@ -32,10 +31,10 @@ public class RecipeController implements Initializable {
List<Recipe> suggestedRecipes = null; List<Recipe> suggestedRecipes = null;
IngredientsAtHand ingredientsAtHand = new IngredientsAtHand(); IngredientsAtHand ingredientsAtHand = new IngredientsAtHand();
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.LEMON, 6, MeasuringUnit.PC)); ingredientsAtHand.addIngredient(FoodItem.LEMON);
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MINCED_MEAT, 400, MeasuringUnit.GR)); ingredientsAtHand.addIngredient(FoodItem.MINCED_MEAT);
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.POTATO, 4, MeasuringUnit.PC)); ingredientsAtHand.addIngredient(FoodItem.POTATO);
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MILK, 8, MeasuringUnit.DL)); ingredientsAtHand.addIngredient(FoodItem.MILK);
......
...@@ -3,22 +3,16 @@ package no.ntnu.idatt1002.demo.data.recipes; ...@@ -3,22 +3,16 @@ package no.ntnu.idatt1002.demo.data.recipes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* The IngredientsAtHand class contains a collection of Ingredient objects that are currently available to the user.
* Only one instance of each ingredient type may exist in the collection. If an ingredient is already present in the
* collection, the old will be replaced by the new.
*
* @author hannesofie
*/
public class IngredientsAtHand { public class IngredientsAtHand {
//TODO: go through JavaDoc after refactoring.
private final List<Ingredient> ingredientsAtHand = new ArrayList<>(); private final ArrayList<FoodItem> ingredientsAtHand = new ArrayList<>();
/** /**
* The method returns the collection of ingredients at hand as an arraylist of ingredient objects. * The method returns the collection of ingredients at hand as an arraylist of ingredient objects.
* @return The collection of ingredients at hand to the user. * @return The collection of ingredients at hand to the user.
*/ */
public List<Ingredient> getIngredientsAtHand() { public List<FoodItem> getIngredientsAtHand() {
return ingredientsAtHand; return ingredientsAtHand;
} }
...@@ -26,21 +20,26 @@ public class IngredientsAtHand { ...@@ -26,21 +20,26 @@ public class IngredientsAtHand {
* The method takes in an ingredient object and adds it to the collection of ingredients at hand. * The method takes in an ingredient object and adds it to the collection of ingredients at hand.
* @param ingredient The ingredient object to add to the collection of ingredients at hand. * @param ingredient The ingredient object to add to the collection of ingredients at hand.
*/ */
public void addIngredient(Ingredient ingredient) { public void addIngredient(FoodItem ingredient) {
this.ingredientsAtHand.add(ingredient); this.ingredientsAtHand.add(ingredient);
} }
/** //TODO: Unit tests and javadoc
public boolean atHand(FoodItem foodItem) {
return ingredientsAtHand.stream().anyMatch( (in) -> in.equals(foodItem));
}
/* *//**
* Returns null if no ingredient of the requested type is found in the collection. * Returns null if no ingredient of the requested type is found in the collection.
* @param ingredientType What type of food the ingredient is. * @param ingredientType What type of food the ingredient is.
* @return The ingredient of the specified type found among the ingredients at hand, null otherwise. * @return The ingredient of the specified type found among the ingredients at hand, null otherwise.
*/ *//*
public Ingredient getIngredient(FoodItem ingredientType) { public Ingredient getIngredient(FoodItem ingredientType) {
if(ingredientType == null) return null; if(ingredientType == null) return null;
return this.getIngredientsAtHand().stream() return this.getIngredientsAtHand().stream()
.filter((ingredient) -> ingredient.getFoodType() == ingredientType) .filter((ingredient) -> ingredient.getFoodType() == ingredientType)
.findFirst().orElse(null); .findFirst().orElse(null);
} }*/
/** /**
* The method takes in three parameters. The method first checks if the Ingredient is at hand in the first place. * The method takes in three parameters. The method first checks if the Ingredient is at hand in the first place.
...@@ -50,7 +49,7 @@ public class IngredientsAtHand { ...@@ -50,7 +49,7 @@ public class IngredientsAtHand {
* @param amount The amount of the ingredient. * @param amount The amount of the ingredient.
* @return True if Ingredient is successfully altered or added, false if not. * @return True if Ingredient is successfully altered or added, false if not.
*/ */
public boolean alterIngredient(FoodItem ingredientType, double amount, MeasuringUnit unit) { /* public boolean alterIngredient(FoodItem ingredientType, double amount, MeasuringUnit unit) {
//TODO: Consider handling exceptions differently. //TODO: Consider handling exceptions differently.
if(ingredientsAtHand.stream().anyMatch((ingredient) -> ingredient.getFoodType() == ingredientType)) { if(ingredientsAtHand.stream().anyMatch((ingredient) -> ingredient.getFoodType() == ingredientType)) {
try { try {
...@@ -68,7 +67,7 @@ public class IngredientsAtHand { ...@@ -68,7 +67,7 @@ public class IngredientsAtHand {
} }
} }
return true; return true;
} }*/
/** /**
* The method takes in a value of the FoodItem enum as a parameter and removes it from the collection of * The method takes in a value of the FoodItem enum as a parameter and removes it from the collection of
...@@ -78,7 +77,8 @@ public class IngredientsAtHand { ...@@ -78,7 +77,8 @@ public class IngredientsAtHand {
* @return True if the ingredient was found among the ingredients at hand and removed, false otherwise. * @return True if the ingredient was found among the ingredients at hand and removed, false otherwise.
*/ */
public boolean removeIngredient(FoodItem ingredientType) { public boolean removeIngredient(FoodItem ingredientType) {
return ingredientsAtHand.removeIf((ingredient) -> ingredient.getFoodType() == ingredientType); return ingredientsAtHand.remove(ingredientType);
} }
} }
package no.ntnu.idatt1002.demo.data.recipes;
import java.util.ArrayList;
import java.util.List;
/**
* The IngredientsAtHandDetailed class contains a collection of Ingredient objects that are currently available to the user.
* Only one instance of each ingredient type may exist in the collection. If an ingredient is already present in the
* collection, the old will be replaced by the new.
*
* @author hannesofie
*/
public class IngredientsAtHandDetailed {
private final List<Ingredient> ingredientsAtHand = new ArrayList<>();
/**
* The method returns the collection of ingredients at hand as an arraylist of ingredient objects.
* @return The collection of ingredients at hand to the user.
*/
public List<Ingredient> getIngredientsAtHand() {
return ingredientsAtHand;
}
/**
* The method takes in an ingredient object and adds it to the collection of ingredients at hand.
* @param ingredient The ingredient object to add to the collection of ingredients at hand.
*/
public void addIngredient(Ingredient ingredient) {
this.ingredientsAtHand.add(ingredient);
}
/**
* Returns null if no ingredient of the requested type is found in the collection.
* @param ingredientType What type of food the ingredient is.
* @return The ingredient of the specified type found among the ingredients at hand, null otherwise.
*/
public Ingredient getIngredient(FoodItem ingredientType) {
if(ingredientType == null) return null;
return this.getIngredientsAtHand().stream()
.filter((ingredient) -> ingredient.getFoodType() == ingredientType)
.findFirst().orElse(null);
}
/**
* The method takes in three parameters. The method first checks if the Ingredient is at hand in the first place.
* If it is, the old amount and unit of this ingredient are replaced by the provided amount and unit if they
* differ. If not, the ingredient is left as is. If the ingredient is not in the collection,
* @param ingredientType What type of food the ingredient is.
* @param amount The amount of the ingredient.
* @return True if Ingredient is successfully altered or added, false if not.
*/
public boolean alterIngredient(FoodItem ingredientType, double amount, MeasuringUnit unit) {
//TODO: Consider handling exceptions differently.
if(ingredientsAtHand.stream().anyMatch((ingredient) -> ingredient.getFoodType() == ingredientType)) {
try {
getIngredient(ingredientType).setAmount(amount);
getIngredient(ingredientType).setUnit(unit);
} catch (IllegalArgumentException e) {
return false;
}
} else {
try {
addIngredient(new Ingredient(ingredientType, amount, unit));
} catch (IllegalArgumentException e) {
return false;
}
}
return true;
}
/**
* The method takes in a value of the FoodItem enum as a parameter and removes it from the collection of
* ingredients at hand if it exists and returns true. If no ingredient of the given type was found in the
* collection, false is returned.
* @param ingredientType What type of food the ingredient is.
* @return True if the ingredient was found among the ingredients at hand and removed, false otherwise.
*/
public boolean removeIngredient(FoodItem ingredientType) {
return ingredientsAtHand.removeIf((ingredient) -> ingredient.getFoodType() == ingredientType);
}
}
...@@ -96,19 +96,35 @@ public class Recipe { ...@@ -96,19 +96,35 @@ public class Recipe {
throw new IllegalArgumentException("The collection of ingredients at hand is empty."); throw new IllegalArgumentException("The collection of ingredients at hand is empty.");
} else { } else {
missingIngredients = 0; missingIngredients = 0;
int notMissing = (int) ingredientList.stream().filter((inRecipe) -> ingredientsAtHand.atHand(inRecipe.getFoodType())).count();
ingredientList.forEach((inRecipe) -> { ingredientList.forEach((inRecipe) -> {
inRecipe.setAtHand(ingredientsAtHand.atHand(inRecipe.getFoodType()));
});
};
/* ingredientList.forEach((inRecipe) -> {
ingredientsAtHand.getIngredientsAtHand().forEach((atHand) -> { ingredientsAtHand.getIngredientsAtHand().forEach((atHand) -> {
if(inRecipe.getFoodType() == atHand.getFoodType()) {
System.out.println("----");
System.out.println(inRecipe.getFoodType());
System.out.println(atHand);
System.out.println("----");
if(inRecipe.getFoodType() == atHand) {
inRecipe.setAtHand(true); inRecipe.setAtHand(true);
} else { } else {
inRecipe.setAtHand(false); inRecipe.setAtHand(false);
missingIngredients += 1; missingIngredients += 1;
} }
}); });
}); });*/
}
} }
public int getMissingIngredients() { public int getMissingIngredients() {
return missingIngredients; return missingIngredients;
} }
......
...@@ -4,7 +4,7 @@ package no.ntnu.idatt1002.demo.data.recipes; ...@@ -4,7 +4,7 @@ package no.ntnu.idatt1002.demo.data.recipes;
* The RecipeIngredient class is an extension of the Ingredient class used as part of recipes * The RecipeIngredient class is an extension of the Ingredient class used as part of recipes
* that also stored the boolean value 'atHand'. This value can be set to true whenever the given ingredient, * that also stored the boolean value 'atHand'. This value can be set to true whenever the given ingredient,
* being part of a recipe, is also at hand to the user. Ingredients that the user has available are stored in * being part of a recipe, is also at hand to the user. Ingredients that the user has available are stored in
* the IngredientsAtHand class. * the IngredientsAtHandDetailed class.
* *
* @author hannesofie * @author hannesofie
*/ */
......
...@@ -28,4 +28,5 @@ public class RecipeRegister { ...@@ -28,4 +28,5 @@ public class RecipeRegister {
filter((recipe) -> recipe.getName().equals(name)) filter((recipe) -> recipe.getName().equals(name))
.findFirst().orElse(null); .findFirst().orElse(null);
} }
} }
...@@ -12,19 +12,25 @@ class IngredientsAtHandTest { ...@@ -12,19 +12,25 @@ class IngredientsAtHandTest {
@BeforeEach @BeforeEach
void beforeEach() { void beforeEach() {
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC)); ingredientsAtHand.addIngredient(FoodItem.LEMON);
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MILK, 1.5f, MeasuringUnit.L)); ingredientsAtHand.addIngredient(FoodItem.MILK);
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MINCED_MEAT, 400, MeasuringUnit.GR)); ingredientsAtHand.addIngredient(FoodItem.MINCED_MEAT);
} }
@Test /* @Test
@DisplayName("The getIngredient method returns the correct ingredient.") @DisplayName("The getIngredient method returns the correct ingredient.")
void getIngredientReturnsSuccessfully() { void getIngredientReturnsSuccessfully() {
assertEquals(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC), assertEquals(FoodItem.LEMON),
ingredientsAtHand.getIngredient(FoodItem.LEMON)); ingredientsAtHand.getIngredient(FoodItem.LEMON));
}*/
@Test
@DisplayName("The atHand method returns true/false correctly.")
void atHandReturnsSuccessfully() {
assertTrue(ingredientsAtHand.atHand(FoodItem.LEMON));
assertFalse(ingredientsAtHand.atHand(FoodItem.YELLOW_CHEESE));
} }
/*
@Test @Test
@DisplayName("The getIngredient method returns null if the ingredient is not in the collection.") @DisplayName("The getIngredient method returns null if the ingredient is not in the collection.")
...@@ -73,6 +79,7 @@ class IngredientsAtHandTest { ...@@ -73,6 +79,7 @@ class IngredientsAtHandTest {
assertEquals(ingredientsAtStart, ingredientsAtHand.getIngredientsAtHand().size()); assertEquals(ingredientsAtStart, ingredientsAtHand.getIngredientsAtHand().size());
} }
*/
@Test @Test
@DisplayName("Ingredient is removed successfully and true is returned.") @DisplayName("Ingredient is removed successfully and true is returned.")
......
...@@ -93,8 +93,8 @@ class RecipeTest { ...@@ -93,8 +93,8 @@ class RecipeTest {
@DisplayName("Updating ingredient status works as expected.") @DisplayName("Updating ingredient status works as expected.")
void updateIngredientStatus() { void updateIngredientStatus() {
IngredientsAtHand inFridge = new IngredientsAtHand(); IngredientsAtHand inFridge = new IngredientsAtHand();
inFridge.addIngredient(new Ingredient(FoodItem.MINCED_MEAT, 400, MeasuringUnit.GR)); inFridge.addIngredient(FoodItem.MINCED_MEAT);
inFridge.addIngredient(new Ingredient(FoodItem.YELLOW_CHEESE, 500, MeasuringUnit.GR)); inFridge.addIngredient(FoodItem.YELLOW_CHEESE);
recipe.getIngredientList().forEach((ingredient) -> assertFalse(ingredient.isAtHand())); recipe.getIngredientList().forEach((ingredient) -> assertFalse(ingredient.isAtHand()));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment