0

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?

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Vearaa
  • 25
  • 5
  • The declaration should really be `List` because you should always [program to the interface, not the implementation](https://stackoverflow.com/a/384067/636009). You say they never taught you how to use it properly, but have you looked at the documentation for [`List`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html) or [`ArrayList`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ArrayList.html)? That would be a good starting point. – David Conrad Nov 07 '21 at 22:14
  • Also, it may not matter much for a school project, but [double is not suitable](https://stackoverflow.com/questions/316727/is-a-double-really-unsuitable-for-money) for representing money. It would be better to store a whole number of cents in an `int` or a `long` or to use `BigDecimal` instead. But you can worry about that later. – David Conrad Nov 07 '21 at 22:17
  • 1
    If you don't know how to use something in your code for the school project, maybe you should ask first your mentor / teacher? – Nowhere Man Nov 07 '21 at 22:37
  • please keep the tags focused - this is basic java without any relation to fx, fxml or css – kleopatra Nov 07 '21 at 23:47

1 Answers1

1

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));
    }

...
}
Ervin Szilagyi
  • 14,274
  • 2
  • 25
  • 40
  • 1
    Just a side note, `List getToppings()` should return a new `List` and not the original `List` stored in `Pizza`, otherwise it would be possible to add or remove toppings without the usage of the proper methods. – João Dias Nov 07 '21 at 22:51
  • 1
    I mean if you want it to be unmodifiable `Collections.unmodifiableList(toppings)` and let the consumer do what she wants with it. But I feel this is somewhat out of topic for this question. – Ervin Szilagyi Nov 07 '21 at 22:55
  • Thank you very much for the information! I've been struggling for too long trying to get something to work and this makes it much easier to understand. – Vearaa Nov 07 '21 at 23:07
  • 2
    @ErvinSzilagyi, yes it is off-topic, it was simply a side note on something that is usually a source of bugs on Java applications ;) – João Dias Nov 07 '21 at 23:23