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

Problem Creating New Objects

$
0
0
Okay, I have come across an issue that has completely bewildered me, I know the title is generic but I am going to do my best to explain it and helpfully one of you more intelligent chaps can steer me to clear waters!

I am going to show my code first and explain after each chunk, I'll also point out what I've tried and what has still kind of perplexed me! First off is main

Recipe recipe = new Recipe("Pepperoni Pizza");
		
		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.addIngredient(one);
		recipe.addIngredient(two);
		recipe.addIngredient(three);
		
		RecipeBook.addRecipe(recipe);
		
		Recipe recipeTwo = new Recipe("Chicken Soup");
		
		Ingredient four = new Ingredient("Chicken", 2, UnitOfMeasurement.valueOf("Pounds"));
		Ingredient five = new Ingredient("Chicken Broth", 20, UnitOfMeasurement.valueOf("Ounces"));
		Ingredient six = new Ingredient("Carrots", 10, UnitOfMeasurement.valueOf("Ounces"));
		Ingredient seven = new Ingredient("Celery", 5, UnitOfMeasurement.valueOf("Ounces"));
		
		recipeTwo.addIngredient(four);
		recipeTwo.addIngredient(five);
		recipeTwo.addIngredient(six);
		recipeTwo.addIngredient(seven);
		
		RecipeBook.addRecipe(recipeTwo);
		
		RecipeBook.printMenu();



Now it is pretty simple whats going on here, creating some Objects and giving properties, using methods to add them and print them out etc., nothing special.

I want to go to the addIngredient code simply for reference, I am not 100% positive there is no problem here, but so far not really seeing an issue, again adding only for reference later on...

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 void addIngredient(Ingredient i){
		
		for(int j = 0 ; j < Recipe.length ; j++){
			
			if(Recipe[j] != null){
				if(Recipe[j].getName().equals(i.getName())){
					System.out.println("Sorry, that ingredient already exists!");
					break;}}
					
			else{
				
				Recipe[j] = new Ingredient(i.getName(), i.getQuantity(), i.getUnit());
				break;
			}

			}
		}}


So yeah nothing crazy going on in here, just doing some stuff, again not so sure there is an issue here but whatever...

This is where it gets hairy, the line
RecipeBook.addRecipe(recipe);
in my main calls this portion here and does pretty much what I ask it to, no complaints...

public class RecipeBook{
	
	final static int MENU_ARRAY_MAX = 50;
	
	static Recipe Menu[] = new Recipe[MENU_ARRAY_MAX];
	
	public static void addRecipe(Recipe r){
		
		try{
			
			if(Menu[0] == null){
				
				System.out.println("blah");
				Menu[0] = new Recipe(r.getName());
				System.out.println(Menu[0]);
				
			}
			
			else if(Menu[1] == null){
				
				System.out.println("bloh");
				Menu[1] = new Recipe(r.getName());
				System.out.println(Menu[1]);
				
			}
			
			else{
				
				throw new RecipeBookOverflowException();
				
			}
			
	}catch(RecipeBookOverflowException e){
		
		System.out.println("The Menu is full!");
		
	}
		
}
public static void printMenu(){
		
		System.out.println(Menu[0]);
		System.out.println(Menu[1]);
		//Prints the restaurant's menu, with all of the currently available
		//recipes. This method should utilize Recipe's toString()
		
	}

}


Just want to state, this is NOT THE FINAL OR "REAL" VERSION OF THIS FUNCTION! I HAVE COMPLETELY GUTTED IT just to make it quick and easy and nicely fit the confines of what I am attempting to test from main just to see if it works at all and that I have the right idea. It's output is simple...

"blah
Pepperoni Pizza: 1.0 Pounds of Dough, 8.0 Ounces of Sauce, 10.0 Ounces of Cheese"

The "blah" is for me to know this is happening and its coming from there, it obviously adds the object and simple prints it back out to me for further reassurance.

Okay so now this is where it all goes completely down hill, so get ready lol. If you look back at my main you will see a new Recipe object "recipeTwo". I give it some objects to be added and then I add them and to my knowledge it is all no big deal, then I get back to this line
RecipeBook.addRecipe(recipeTwo);


Now again to reiterate, addRecipe() is only coded that way in order for this second case of adding to work and its outline is very clear, first spot is clear, go to the next, check it, if its clear, add it and print it, but the output is a little troubling...

"Chicken Soup: 1.0 Pounds of Dough, 8.0 Ounces of Sauce, 10.0 Ounces of Cheese, 2.0 Pounds of Chicken, 20.0 Ounces of Chicken Broth, 10.0 Ounces of Carrots, 5.0 Ounces of Celery"

WOW! It made the Chicken Soup object, and added my Ingredients, but after the ingredients it somehow got from my first object recipe("Pepperoni Pizza"), now the only reason I have added the addIngredient code and am suspicious of that code is this output and this one alone, but again, I don't see an issue? I am calling a whole new object and whole new ingredients in, what does that have to do with my other Recipe object and its ingredients at all? Am I passing some sort of reference and not a new object? Are the methods holding onto the first set of ingredients and just tossing them in with the rest in a new object? I honestly have no clue, I have tried tossing printlns all over the place and signifying where they go, what they get, and then print out what I am looking for and it all seems to check out?

Finally this is my last issue, and if ANYONE reads this I just want to say thank you so much for taking the time to go through this and possibly help me out, I really do appreciate it very, very much, again, Thank You!

So last problem comes from that last line in main
RecipeBook.printMenu();
which very obviously calls the printMenu() in my RecipeBook class shown above, here is its output...

"Chicken Soup: 1.0 Pounds of Dough, 8.0 Ounces of Sauce, 10.0 Ounces of Cheese, 2.0 Pounds of Chicken, 20.0 Ounces of Chicken Broth, 10.0 Ounces of Carrots, 5.0 Ounces of Celery
Chicken Soup: 1.0 Pounds of Dough, 8.0 Ounces of Sauce, 10.0 Ounces of Cheese, 2.0 Pounds of Chicken, 20.0 Ounces of Chicken Broth, 10.0 Ounces of Carrots, 5.0 Ounces of Celery"

Again I am completely dumbfounded, we've already covered the whole added ingredients issue and thats a beast in its own right, but how has the location Menu[0] and Menu[1] become the same thing?! I didn't assign anything, I didnt code for this to even happen, it is clear that in addRecipe it is suppose to check the first Menu location so see if it is null or not and it should NOT be null on the second call due to recipe("Pepperoni Pizza") being in that location. Actually as I write this right now I have just changed that else if in addRecipe to this

else if(Menu[1] == null){
				
				System.out.println(Menu[0]);
				System.out.println("bloh");
				Menu[1] = new Recipe(r.getName());
				System.out.println(Menu[0]);
				System.out.println(Menu[1]);
				
			}



With the output now being...

"Chicken Soup: 1.0 Pounds of Dough, 8.0 Ounces of Sauce, 10.0 Ounces of Cheese, 2.0 Pounds of Chicken, 20.0 Ounces of Chicken Broth, 10.0 Ounces of Carrots, 5.0 Ounces of Celery
bloh
Chicken Soup: 1.0 Pounds of Dough, 8.0 Ounces of Sauce, 10.0 Ounces of Cheese, 2.0 Pounds of Chicken, 20.0 Ounces of Chicken Broth, 10.0 Ounces of Carrots, 5.0 Ounces of Celery
Chicken Soup: 1.0 Pounds of Dough, 8.0 Ounces of Sauce, 10.0 Ounces of Cheese, 2.0 Pounds of Chicken, 20.0 Ounces of Chicken Broth, 10.0 Ounces of Carrots, 5.0 Ounces of Celery"

I have no clue how those locations got reassigned?! So yeah, not sure what else can be said other than thank you in advance for those who can help or provide insight, and thanks to all who read this very long thread of mine!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>