Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem calling toString from a toString

$
0
0
Howdy everyone! From my title it is pretty clear what I am looking to do...

public class Recipe implements Cloneable{

	static String Name;
	
	final static int INGREDIENT_ARRAY_MAX = 10;
	
	static Ingredient Recipe[] = new Ingredient[INGREDIENT_ARRAY_MAX];
	
	public Recipe(String name){
		
		Name = name;
		
	}
public String toString(){
		
		return getName() + ": " /*Looking to call Ingredients toString here!*/; 
		
	}}


It is Obvious to see what is going on here. This toString method is in a class called Recipe, which is an object that needs to hold up to 10 Ingredient Objects and signify it with a name. With in the Ingredient class is that Objects class' separate toString method as so
public class Ingredient implements Cloneable{
	
	String Name;
	
	double Quantity;
	
	UnitOfMeasurement unitOfMeasurement;
	
	public Ingredient(String name, double quantity, UnitOfMeasurement unit){
		
		Name = name;
		Quantity = quantity;
		unitOfMeasurement = unit;
		
	}
public String toString(){
		
		return Quantity + " " + unitOfMeasurement + " of " + Name;
		
	}}



My problem comes in here because I want to use Ingredients toString within the Recipes in order to get an organized output that looks something along the lines of this: "Pepperoni Pizza: 1.0 Pounds of Dough, 8.0 Ounces of Sauce, 10.0 Ounces of Cheese". Now I know static isn't an option because they are objects, but this drives me to another small possible issue. Elsewhere in my program in my main when I want to call Recipes toString method I have this

Ingredient one = new Ingredient("Dough", 1, UnitOfMeasurement.valueOf("Pounds"));
Ingredient two = new Ingredient("Sauce", 8, UnitOfMeasurement.valueOf("Ounces"));
Ingredient three = new Ingredient("Cheese", 10, UnitOfMeasurement.valueOf("Ounces"));
		
Recipe recipe = new Recipe("Pepperoni Pizza");
		
recipe.addIngredient(one);
recipe.addIngredient(two);
recipe.addIngredient(three);

System.out.println(recipe.toString());



This code was written purely just to test these methods quickly and easily to work out the bugs and get them to work etc. Anyways, when I call toString in that last line, all I am able to get is "Pepperoni Pizza : ". First off, I know there is nothing else there to be printed, read, or done so...yeah lol. Furthermore, is there anyway for me to access the the Ingredient elements within my recipe object when it calls toString? Is it possible to get the Ingredient elements out of recipe with a simple API call or something? On top of that, I need to access all of them as if they were in an array. I know technically "recipe" isn't an array but shouldn't it store the ingredients as such because it is defined as one in the Recipe class? Its not like I can just change the "recipe.toString()" line to "recipe[0].toString"

I am a little confused as to how all that works out, so hopefully someone can clarify and steer me in the right direction. Thanks in advance!

Viewing all articles
Browse latest Browse all 51036

Trending Articles