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

Delete unused classes

parent ae8fd7bb
No related branches found
No related tags found
2 merge requests!42Hs frontend recipes,!41Hs frontend recipes
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);
}
}
package no.ntnu.idatt1002.demo.data.recipes;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class IngredientsAtHandDetailedTest {
IngredientsAtHandDetailed ingredientsAtHand = new IngredientsAtHandDetailed();
@BeforeEach
void beforeEach() {
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC));
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MILK, 1.5f, MeasuringUnit.L));
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MINCED_MEAT, 400, MeasuringUnit.GR));
}
@Test
@DisplayName("The getIngredient method returns the correct ingredient.")
void getIngredientReturnsSuccessfully() {
assertEquals(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC),
ingredientsAtHand.getIngredient(FoodItem.LEMON));
}
@Test
@DisplayName("The getIngredient method returns null if the ingredient is not in the collection.")
void getIngredientReturnsNullWhenNotFoundOrNull(){
assertNull(ingredientsAtHand.getIngredient(FoodItem.ONION));
assertNull(ingredientsAtHand.getIngredient(null));
}
@Test
@DisplayName("Altering ingredient successfully and return true.")
void alterIngredientSuccessfully() {
assertNotEquals(new Ingredient(FoodItem.ONION, 500, MeasuringUnit.GR),
ingredientsAtHand.getIngredient(FoodItem.ONION));
assertTrue(ingredientsAtHand.alterIngredient(FoodItem.ONION, 500, MeasuringUnit.GR));
assertEquals(new Ingredient(FoodItem.ONION, 500, MeasuringUnit.GR),
ingredientsAtHand.getIngredient(FoodItem.ONION));
}
@Test
@DisplayName("Altering ingredient that does not yet exist in collection adds it and returns true.")
void alterNewIngredientAddsIt() {
int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size();
assertTrue(ingredientsAtHand.alterIngredient(FoodItem.ORANGE, 8, MeasuringUnit.PC));
assertEquals(ingredientsAtStart + 1, ingredientsAtHand.getIngredientsAtHand().size());
}
@Test
@DisplayName("Attempting to alter ingredient in illegal way does not alter the collection and returns false.")
void alterIngredientUnchangedForInvalidChange() {
assertFalse(ingredientsAtHand.alterIngredient(null, 350, MeasuringUnit.GR));
assertEquals(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC),
ingredientsAtHand.getIngredient(FoodItem.LEMON));
}
@Test
@DisplayName("Altering an ingredients without changing values, leaves no change on the collection and returns true.")
void alterNothingLeavesCollectionIntact() {
int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size();
assertTrue(ingredientsAtHand.alterIngredient(FoodItem.LEMON, 3, MeasuringUnit.PC));
assertEquals(ingredientsAtStart, ingredientsAtHand.getIngredientsAtHand().size());
}
@Test
@DisplayName("Ingredient is removed successfully and true is returned.")
void removeIngredientSuccessfully() {
int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size();
assertTrue(ingredientsAtHand.removeIngredient(FoodItem.LEMON));
assertEquals(ingredientsAtStart-1, ingredientsAtHand.getIngredientsAtHand().size());
}
@Test
@DisplayName("Removing ingredient that is not in the collection, leaves it unchanged and returns false.")
void removeIngredientNotInCollection() {
assertFalse(ingredientsAtHand.removeIngredient(FoodItem.SALSA_SAUCE));
}
}
\ No newline at end of file
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