Home > Blockchain >  Don't understand how to utilize ArrayList with a collection
Don't understand how to utilize ArrayList with a collection

Time:11-08

For my current assignment, I have to create a program that can make pizzas, select toppings, calculate order total, and export the order to a txt file. All of this is done in JavaFX with multiple controller classes.

package application;

import java.util.ArrayList;

// You can add additional methods for help.
public abstract class Pizza // Abstract is used to make specific methods that will be used by other classes that are related to pizza.
{
    protected ArrayList<Topping> toppings = new ArrayList<Topping>(); // How the heck do I use this???
    protected Size size; // Enum Class
    public abstract double price();
}

The code above is my pizza class. I have to use the parameters included, but I can add more methods if I need to. The problem I have is using the ArrayList which has to have the "Topping" collection of items. This is the first time we're using ArrayList at all (they never taught us how to use it properly at all which really sucks) so I made an enum class with the list of the toppings.

package application;

public enum Topping 
{
    Pepperoni, Sausage, Chicken, Beef, Ham, GreenPepper, Onion, Mushroom, Pinapple, BlackOlives;
}

But now I don't know how I'm supposed to use ArrayList. For example, I have inheritor classes for my pizza class for particular types of pizza (deluxe, hawaiian, pepperoni). The code below is an example of what it looks like now. I want the deluxe pizza to start with the toppings Chicken, Mushroom, GreenPepper, Sausage, and Onion. Then the user will be able to add or remove toppings in the main program.

package application;

public class PizzaDeluxe extends Pizza
{
    @Override
    public double price() 
    {
        return 0;
    }
}

How do I make the deluxe pizza class start off with certain toppings? After I do that, how would I be able to remove or add certain toppings? Finally, how would I get the info list of what toppings are on the pizza?

CodePudding user response:

For your Pizza class you may want to create addTopping and removeTopping methods. For listing all the toppings, you may want to create a getter:

public abstract class Pizza {
    protected List<Topping> toppings = new ArrayList<>();
    protected Size size; // Enum Class
    public abstract double price();

    public void addTopping(Topping topping) {
        toppings.add(topping);
    }

    public void removeTopping(Topping topping) {
        toppings.remove(topping);
    }

    public List<Topping> getToppings() {
        return toppings;
    }
}

In order to initialize PizzaDeluxe with certain toppings, we can accomplish this in the constructor by using addAll:

public class PizzaDeluxe extends Pizza {
    public PizzaDeluxe() {
        toppings.addAll(Arrays.asList(Topping.Chicken, Topping.Mushroom, Topping.GreenPepper, Topping.Sausage, Topping.Onion));
    }

...
}
  • Related