Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***Need some help with a java program*** You will be given two premade classes(listed below) Create a class called Conversion and add three fields: first

***Need some help with a java program***

You will be given two premade classes(listed below)

Create a class called Conversion and add three fields: first - which stores the name of a unit of measure (cups, pints, oz., etc.). second - which stores the name of another unit of measure. factor - which stores the amount that you must multiply a quantity of the first unit by to get a quantity of the second unit. For example, if the first unit was cups and the second pints, the factor would be 0.5. Or if the first unit was cups and the second ounces, the factor would be 8. Add a constructor with parameters for each field and then add accessors for each of the fields in the Conversion class.

Create another class called Cookbook with three fields, name - for the name of the cookbook. recipes - for a flexibly-sized collection of recipes, in alphabetical order. conversions - stores a flexibly-sized collection of conversions, in no particular order. Add a constructor for the Cookbook class with one parameter for the new cookbook's name and a new recipe has no recipes or conversions. Also add an accessor for the name field of cookbook..... Add a method called addConversion to the Cookbook class. It should one parameter, which is a Conversion object to add to the collection..... Add another method named addConversion to the Cookbook class thas has three parameters: two units of measure and a conversion factor, and should construct a Conversion instance and add it to the collection.....Add a method named removeConversions to the Cookbook class that has two parameters, both of which are units of measure. It should remove from the collection any conversions between those units of measure (there may be more than one).....Add a method named convert to the Cookbook class that has three parameters: two units of measure and a quantity. It should look for a conversion in the collection that is between the two units of measure, apply the conversion factor to the quantity, and return the result. If the first unit of measure in the parameters is also the first in the conversion, you will multiply by the factor. But if the first unit of measure in the parameters is the second in the conversion, you will divide by the factor. If there are no known conversion between the units of measure, just return the original quantity......Add a method named addRecipe to the Cookbook class that has one parameter, which is a Recipe object to add to the collection. This method will need to insert it at the correct location to ensure that the collection of recipes stays in alphabetical order by recipe name....Add a method named getRecipe to the Cookbook class with one parameter, which is the name of the recipe. It should return the recipe with that name, or null if no recipe exists with that name. If there is more than one recipe with that name, just return one of them....Add a method named removeRecipe to the Cookbook class that has one parameter, which is the name of a recipe. It should remove all recipes with that name from the collection.....

Go back to the Recipe class from the previous assignment, and add an accessor for the ingredients field.... Create a method called canMake in the Cookbook class with two parameters: a recipe, and a collection of ingredients that represents all the unique ingredients that represents all the food you have in your home (order does not matter). It should return true if you have enough of every ingredient required by the recipe. Otherwise, it should return false. (You can assume no type of ingredient appears twice.) (This could require some conversions, ex) get a recipe that requires 2 cups of sugar and 3 tablespoons of butter, and we have 5 cups of sugar and 1 stick of butter. We also have a conversion that 1 stick = 8 tablespoons. We can make this recipe, because 5 cups of sugar is at least 2 cups of sugar, and 1 stick of butter is the same as 8 tablespoons of butter, which is at least 3 tablespoons of butter. ) ...... Create a method called getMakeable in the Cookbook class with one parameter, a collection of unique ingredients that represents all the food you have in your home (order does not matter). It should return a collection of all recipes that you can make with those ingredients, in alphabetical order by recipe name...... Add a static method named getGroceryList to the Cookbook class with one parameter, a collection of recipes that you intend to prepare during the week. It should return a collection of all the unique ingredients that will be needed to make those recipes. If two recipes require the same ingredient, with the same unit of measure, they should be combined. (example: make a recipe that require 3 cups of sugar and another that requires 4 cups of sugar, your collection should contain one entry of 7 cups of sugar. However, if one recipe requires 3 cups of sugar and another requires 3 tablespoons of sugar, you can leave that as two di erent entries.

Lastly, write some early exits for your loops in previous methods and write a main method to test all of the methods you created....

Given code(previous two classes) ::

public class Food {

private double amount;

private String foodType;

private String unit;

private int calorieAmount;

public Food(double amount, String foodType, String unit, int calorieAmount) {

this.amount = amount;

this.foodType = foodType;

this.unit = unit;

this.calorieAmount = calorieAmount;

}

public double getAmount() {

return amount;

}

public void setAmount(double amount) {

this.amount = amount;

}

public String getFoodType() {

return foodType;

}

public void setFoodType(String foodType) {

this.foodType = foodType;

}

public String getUnit() {

return unit;

}

public void setUnit(String unit) {

this.unit = unit;

}

public int getCalorieAmount() {

return calorieAmount;

}

public void setCalorieAmount(int calorieAmount) {

this.calorieAmount = calorieAmount;

}

@Override

public String toString() {

return String.format(" %s %s of %s", amount, unit, foodType);

}

}

import java.util.HashSet;

import java.util.LinkedList;

import java.util.Set;

public class Recipe {

private String name;

private String category;

private Set ingredients;

private LinkedList instructions;

public Recipe(String name, String category) {

this.name = name;

this.category = category;

this.ingredients = new HashSet<>();

this.instructions = new LinkedList<>();

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getCategory() {

return category;

}

public void setCategory(String category) {

this.category = category;

}

public Set getIngredients() {

return ingredients;

}

public void setIngredients(Set ingredients) {

this.ingredients = ingredients;

}

public LinkedList getInstructions() {

return instructions;

}

public void setInstructions(LinkedList instructions) {

this.instructions = instructions;

}

public void addIngredient(Food food) {

this.ingredients.add(food);

}

public void addIngredient(double amount, String foodType, String unit, int calorieAmount) {

this.ingredients.add(new Food(amount, foodType, unit, calorieAmount));

}

public void addStep(String step) {

this.instructions.add(step);

}

public void insertStep(String step, int index) {

this.instructions.add(this.instructions.size() - index - 1, step);

}

@Override

public String toString() {

String string = "Name: " + name + " ";

string += "Category: " + category + " ";

string += " Ingredients: ";

for (Food food : this.ingredients) {

string += " " + food.toString();

string += " ";

}

string += " ";

string += " Instructions:" + " ";

for (int i = 0; i < instructions.size(); i++) {

string += String.format(" \"%d:\" ", i) + instructions.get(i);

string += " ";

}

return string;

}

public int getTotalCalories() {

int totalCalories = 0;

for (Food food : ingredients) {

totalCalories += food.getAmount() * food.getCalorieAmount();

}

return totalCalories;

}

public Recipe getDoubled() {

Recipe recipe = new Recipe(this.getName(), this.getCategory());

recipe.getInstructions().addAll(this.getInstructions());

for (Food food : this.getIngredients()) {

recipe.addIngredient(

new Food(food.getAmount() * 2, food.getFoodType(), food.getUnit(), food.getCalorieAmount()));

}

return recipe;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions

Question

3. Discuss the process of behavior modeling training.

Answered: 1 week ago