1

This is my first time using enum in a class. I want to create a Pizza class so the user can create a Pizza object and then set the size, get the size, set the number of cheese etc.. Pizza() is the default constructor to initialize a Pizza object with no arguments. Thanks!!

package PizzaPackage;

public class Pizza {
private enum PizzaSize {
    small, medium, large }
protected int numcheese;
protected int numpep;
protected int numham;

Pizza(){
     PizzaSize newpizza= PizzaSize.medium; //Is this correct?
     numcheese = 1;
     numpep =0;
     numham=0;
}
public int getnumcheese() {
    return this.numcheese;

}

public int getnumpep() {
    return this.numpep;
}

public int getnumham() {
    return this.numham;
}

public void setSize(PizzaSize newpizza){
    //???

}
public PizzaSize getSize(){
    //???
}
}
1

3 Answers 3

2

Not quite.

PizzaSize is an enum, and you have declared that properly.

However, you are mistakening that PizzaSize enum for an instance member that holds this value for any particular instance of a Pizza.

You should create an additional private member variable, called private pizzaSize, and your constructor should be doing

this.pizzaSize = PizzaSize.medium;

Then, in your getSize() method, you should be returning this.pizzaSize;

Additionally, your setSize(PizzaSize newpizza) should contain this.pizzaSize = newpizza

Sign up to request clarification or add additional context in comments.

Comments

1

Although you have created Enum, you forgot to have it as a instance member just like your other instance members numcheese, numpep etc.

PizzaSize newpizza;

Declare that as a member and use it.

package PizzaPackage;

public class Pizza {
private enum PizzaSize {
    small, medium, large }
protected int numcheese;
protected int numpep;
protected int numham;
PizzaSize newpizza;

Pizza(){
     newpizza= PizzaSize.medium; //Is this correct?
     numcheese = 1;
     numpep =0;
     numham=0;
}
public int getnumcheese() {
    return this.numcheese;

}

public int getnumpep() {
    return this.numpep;
}

public int getnumham() {
    return this.numham;
}

public void setSize(PizzaSize newpizza){
    this.PizzaSize newpizza = newpizza;

}
public PizzaSize getSize(){
    return newpizza;
}
}

//Is this correct?

Pizza(){
     PizzaSize newpizza= PizzaSize.medium; //Is this correct?
     numcheese = 1;
     numpep =0;
     numham=0;
}

Not really. Because you are restricting the scope of your PizzaSize to this constructor only. That variable of type PizzaSize no more accessible outside of constructor.

Comments

0

There are 2 possible solutions:

  1. change type of your pizzasize field to PizzaSize
  2. use ordinal method of enum values: pizzasize = newpizza.ordinal(); - it returns index of enum value in original enum.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.