Ryan H
Ryan H

Reputation: 85

Printing values within an object

I've created a margherita pizza. I want it to store some values, such as a few ingredients and its cost. I'm trying to print off the values but I'm getting null values back.

However I have noticed if I remove the marg.setCost and just use setCost, it works as I wanted it to. But I thought if I created a marg object I should keep the values in the marg object. I've had a lot of coffee and I'm confused :(

Upvotes: 0

Views: 183

Answers (5)

Pankaj Goyal
Pankaj Goyal

Reputation: 980

still now you are using

public void margherita(){
PizzaMenu marg = new PizzaMenu();

marg.setIngredient1( "tomato" );
marg.setIngredient2( "basil" );
marg.setIngredient3( "mozzerella" );
marg.setCost( 5.7 );
marg.setBase( "thin" );
marg.setPizza("margherita");

}

but you need to use

public void margherita(){

setIngredient1( "tomato" );
setIngredient2( "basil" );
setIngredient3( "mozzerella" );
setCost( 5.7 );
setBase( "thin" );
setPizza("margherita");

}

the reason behind this is you are creating new object ans assign value to that object. since those data member are not static so that they will not change your obejct (u) value.

Upvotes: 1

BlackPOP
BlackPOP

Reputation: 5737

I think it's scope issue. In margherita() you are creating new obj. But in print() you're calling current obj values. The values set to new obj marg. So try to print

public void print(){
System.out.println( String.format( "Good choice! You picked the " + "%s", getPizza() ));
System.out.println( String.format( "The " + "%s" + " is made with " + "%s" + ", " + "%s" + " and " + "%s", marg.getPizza(), marg.getIngredient1(),marg.getIngredient2(), marg.getIngredient3() ) );
System.out.println( String.format( "And the price is a very reasonable £" + "%.2f", marg.getCost() ));   

}

Maybe marg obj not visible to print(). Try to send than obj as an argument!

Upvotes: 0

james goooseling
james goooseling

Reputation: 791

public void margherita(){
    PizzaMenu marg = new PizzaMenu();

    marg.setIngredient1( "tomato" );
    marg.setIngredient2( "basil" );
    marg.setIngredient3( "mozzerella" );
    marg.setCost( 5.7 );
    marg.setBase( "thin" );

}

public static void main(String[] args) {

   PizzaMenu u = new PizzaMenu();
   u.makePizza();
   u.print();
} 

Maybe because your instantiating the PizzaMenu object class twice..."new PizzaMenu()" One is in the Main method and one is in the margherita() method...

If that is the way you want to process your code..You can Either create a overloaded contructor for your object class PizzaMenu that will set the values again..

For ex.

PizzaMenu(String ingredient, String ingredient2, ..etc..){
/*Do the setters here..*/
}

but actually this is not a good programming constructs...Hope it helps

Upvotes: 0

Musfiqur rahman
Musfiqur rahman

Reputation: 739

In the function margharita you are creating another pizzamenu and setting the properties of that object.Your have not set the properties of original object. So you are getting null.But you need to set the properties of the "this" object. Change the below function:

public void margherita(){

        setIngredient1( "tomato" );
        setIngredient2( "basil" );
        setIngredient3( "mozzerella" );
        setCost( 5.7 );
        etBase( "thin" );

    }

Upvotes: 1

csvan
csvan

Reputation: 9454

Your problem is here:

    public void makePizza(){
        String pizza = margherita;

        if( pizza.equalsIgnoreCase("margherita")){
            setPizza("margherita");
            margherita();
        }
    }

notice that lone call to margerita() - this means that you are creating a separate Pizza object, which you then simply discard. You can still access the pizza attribute since you set it, but the other ones are never set. You need to explicitly invoke the various set methods in order to store the ingredients for the pizza you are currently working with.

Upvotes: 0

Related Questions