简体   繁体   中英

toString method seems to be only working once even tho it is called multiple

I am trying to figure out why my toString() function will only work a singular time while adding another pizza. This is the output from the first created pizza which is as expected and functioning well

$ kitchen = new Kitchen()                                              
$ pizza = new Pizza()                                                   
$ pizza.toString()                                                      

-> "no crust pizza with no toppings and no sauce: $0.00"              
$ pizza.ingredients.add(kitchen.ingredients.get(1))                     
-> true                                                                 
$ pizza.toString()                                                     

-> "Thick crust pizza with no toppings and no sauce: $3.50"             
$ pizza.ingredients.add(kitchen.ingredients.get(2))                     
-> true                                                                
$ pizza.toString()                                                 

-> "Thick crust pizza with no toppings and Tomato sauce: $4.50"         
$ pizza.ingredients.add(kitchen.ingredients.get(5))                   
-> true                                                            
$ pizza.toString()                                                     

-> "Thick crust pizza with Olives and Tomato sauce: $6.00"           
$ pizza.ingredients.add(kitchen.ingredients.get(7))                    
-> true                                                              
$ pizza.toString()                                                     

-> "Thick crust pizza with Olives, Beef and Tomato sauce: $8.75"      
$ pizza.ingredients.add(kitchen.ingredients.get(4))                     
-> true                                                                
$ pizza.toString()                                                      

-> "Thick crust pizza with Olives, Beef, Capsicum and Tomato sauce:

but the output of another pizza is as follows

EXPECTED OUTPUT                                                         Current output

Creating new pizza                                                      Creating new pizza
Ingredient(s): Thin                                                     Ingredient(s): Thin
Thin crust pizza with no toppings and no sauce: $3.00                <
Ingredient(s): Tomato                                                   Ingredient(s): Tomato
Thin crust pizza with no toppings and Tomato sauce: $4.00            <
Ingredient(s): Jalapenos                                                Ingredient(s): Jalapenos
Thin crust pizza with Jalapenos and Tomato sauce: $5.00              <
Ingredient(s): Beef                                                     Ingredient(s): Beef
Thin crust pizza with Jalapenos, Beef and Tomato sauce: $7.75        <

=========================================================================edit

After analyzing my code it appears that the linkedlist ingredients in the class Pizza is not being modified so the issue would be with my testing method which is below.

public void testing(Ingredient ingredient1){  
    for (Ingredient ingredient : ingredients){
        if (ingredient1.ingtoString().equals(ingredient)){
            ingredients.add(ingredient1);
        }
    }
}

and that method is called by my Kitchen class, like so.

  public void nameMatch(String match){
        Pizza pizza = new Pizza();
        for (Ingredient ingredient : ingredients){
            if (match.equals(ingredient.ingtoString())){
                pizza.testing(ingredient);
            }
        }
        pizza.toString();
    }

and finally, this is what is being used to create the pizza and call these methods

public void create() {
    System.out.println("Creating new pizza");
    String ingredient1 = "";
    while (!ingredient1.equals(".")){
        System.out.print("Ingredient(s): ");
        ingredient1 = (In.nextLine());
        kitchen.nameMatch(ingredient1);
    }
    System.out.println("ORDER SUMMARY");
    kitchen.nameMatch(ingredient1);
}

quick note all these methods are in 3 separate classes

Every time you call nameMatch you create a new pizza, add the ingredient to that pizza and then throw the pizza away. I assume you want to create a single pizza and all of the ingredients to it.

Plus, nameMatch doesn't actually print anything. pizza.toString() will convert the pizza to a string, but if you want to print that string you need to call System.out.println - eg System.out.println(pizza.toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM