-1

It's several years since I actually took a programming class. I know I can do what I'm thinking of, but I cannot remember the terms, so I cannot look it up.

I'm writing a mock application for a HMI class. It orders pizzas. Right now, I use a separate class to pass the values of the pizza between activities. (Values like toppings, pizza size, etc). The class is named pizzaApplication.class, and an example value would be pizzaApplication.chkPepperoni = true;

The way it's set up now, I can only have one pizza. I'd like to be able to have the pizzaApplication class setup to have more than one pizza. The code (from memory) would then end up looking similar to
pizzaApplication.pizzaOne(chkPepperoni = true);

So, two questions:
What is the exact terminology for this
What is the best way to increment the different objects I'd make? Ie
pizzaApplication.pizzaOne(chkPepperoni = true);
or
pizzaApplication.pizza.1(chkPepperoni = true);

Edit:
For further clarification, I'm writing an Android app. I first ask them to make their selections for their pizza, and save them to a a different java file, named PizzaApplication.java Here are the first lines of code.

public class PizzaApplication extends Application { private Order order = new Order();

//private final String[] toppings = new String[] { "Pepperoni", "Onion",
    //  "Sausage", "etc." };

public PizzaApplication() {

}
//Booleans for checking whether the individual pages have been completed. 
Boolean vfTop = false;
Boolean vfAdd = false;
Boolean vfBill = false;
//Booleans for whether to add toppings
Boolean cheese = false;
Boolean italian = false;
Boolean pineapple = false;
Boolean ham = false;
Boolean pepperoni = false;
Boolean chicken = false;
//Globally accessible storage strings for various things
public String creditCard = "none";
public String crustType = "";
public String addressLine1 = "";
public String addressLine2 = "";
public String phoneNumber = "";
public String Toppings = "";
public Integer pizzaCount;
public String pizzaSize = "";

Now, here is some sample code from the Toppings class.

public class Toppings_Page extends Activity {
PizzaApplication pizzaApplication;
CheckBox Cheese, Ham, Italian, Pepperoni, Chicken, Pineapple;
Integer intpizzaCount;
TextView t;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_toppings__page);
    pizzaApplication = (PizzaApplication) getApplication();
    Cheese = (CheckBox)findViewById(R.id.chkCheese);
    Italian = (CheckBox)findViewById(R.id.chkItalian);
    Ham = (CheckBox)findViewById(R.id.chkHam);
    Pepperoni = (CheckBox)findViewById(R.id.chkPepperoni);
    Chicken = (CheckBox)findViewById(R.id.chkChicken);
    Pineapple = (CheckBox)findViewById(R.id.chkPinapple);

and a little further down that class

 public void onChecked(View view){
    if (Cheese.isChecked()){pizzaApplication.cheese=true;}
    else {pizzaApplication.cheese = false;}

    if (Italian.isChecked()){pizzaApplication.italian=true;}
    else {pizzaApplication.italian = false;}

    if (Ham.isChecked()){pizzaApplication.ham=true;}
    else {pizzaApplication.ham = false; }

    if (Pepperoni.isChecked()){pizzaApplication.pepperoni=true;}
    else {pizzaApplication.pepperoni = false;}

    if (Chicken.isChecked()){pizzaApplication.chicken=true;}
    else {pizzaApplication.chicken = false; }
Lion
  • 18,729
  • 22
  • 80
  • 110
GeoffM
  • 13
  • 5
  • Every line of code you posted except the first one is invalid Java code. And your class doesn't respect standard naming conventions. I don't understand what you want to do, especially with so few code posted. My advice would be to buy a good, recent, introductory Java book or to read the Java tutorial. You're probably looking for "what is an object" or "how to construct an object" or "what is a constructor". This will be described by all the introductory Java books or tutorials. – JB Nizet Nov 24 '13 at 16:56
  • Creating an object from a class in know as **instantiation**. You are creating a new object which is an _instance_ of the pizza class. I would read up on how to do this in Java, as what you've posted makes no sense. – James Barnett Nov 24 '13 at 17:11

1 Answers1

0

You should have a seperate class named Pizza that represents a single pie. It should have no direct connection to your UI/Android code. You might use the builder pattern as shown in this example, which would allow you to create a pizza using something like this:

Pizza pizza = new Pizza.Builder(12).cheese().pepperoni().build();

At that point, working with multiple pizzas simply means using a List<Pizza> (or some other type of Collection):

List<Pizza> order = Arrays.asList(
        new Pizza.Builder(9).cheese().ham().pineapple().build(),
        new Pizza.Builder(12).cheese().sausage().build()
    );
Community
  • 1
  • 1
quietmint
  • 13,885
  • 6
  • 48
  • 73