简体   繁体   中英

Why can't I add an element to a list from another class?

I have two classes, Pizza and PizzaTopping . In Pizza is an ArrayList which should store strings (the pizza topping). I am trying to add an element to the array from the ham method in the PizzaTopping class. However, when I print the list it shows to be empty. Why?

Here is PizzaTopping:

public class PizzaTopping {

    Pizza p = new Pizza();

    public void ham() {
        double cost = 1;
        System.out.println("im in ham");
        p.addToPizza("ham");
        p.setCost(p.getCost() + cost);
    }
}

Here is my Pizza class:

import java.util.ArrayList;
import java.util.Arrays;

public class Pizza {

    private double cost;
    ArrayList<String> pizza = new ArrayList<String>();

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public void addToPizza(String x) {
        System.out.println("im in add");
        pizza.add(x);
    }

    public static void main(String [] args) {
        //scanner to add topping
        PizzaTopping pt = new PizzaTopping();
        Pizza p = new Pizza();
        pt.ham();
        System.out.println(Arrays.toString(p.pizza.toArray()));
    }

}

Simply do this:

PizzaTopping pt = new PizzaTopping();
pt.ham();
System.out.println(Arrays.toString(pt.p.pizza.toArray()));

The problem was that you were creating a new pizza object inside main and were not using the one tied to pt so the ham method was saving values inside pt.p and not in p(that you created in main method)

Your Pizza class should probably have a default or a no-argument constructor to make a cheese pizza (with some sauce). It should provide an .addTopping(PizzaTopping topping) to allow adding topping to it. In theory, there should be an unlimited number of topping that you can add. However, to control the number of toppings, you can probably limit them by adding them to an array (or some .addTopping(PizzaTopping topping) to allow adding topping to it. In theory, there should be an unlimited number of topping that you can add. However, to control the number of toppings, you can probably limit them by adding them to an array (or some .addTopping(PizzaTopping topping) to allow adding topping to it. In theory, there should be an unlimited number of topping that you can add. However, to control the number of toppings, you can probably limit them by adding them to an array (or some Collections` object).

public class Pizza
{
    private List<PizzaTopping> toppings = new ArrayList<PizzaTopping>();

    public Pizza() {} // constructs basic cheese pizza

    public void addTopping(PizzaTopping topping) throws IllegalArgumentException
    {
        if (toppings == null)
            throw new IllegalArgumentException("Topping cannot be null");
        toppings.add(new PizzaTopping(topping.toString());
    }

    @Override
    public String toString()
    {
        return "Pizza with " + toppings.size() + " toppings: " + toppings.toString();
    }
}

The PizzaTopping class should probably have a single-argument constructor (String) to provide the name of the topping.

public PizzaTopping
{
    private String name;

    public PizzaTopping(String name) throws IllegalArgumentException
    {
        if (name == null)
            throw new IllegalArgumentException("Name cannot be null");
        this.name = name;
    }

    @Override
    public String toString()
    {
        return name;
    }
}

Your test class should simply create the toppings, the pizza, and add the toppings to the pizza. Each class has a convenience method ( toString() ) to print out information about the object. The toppings prints out its name and the pizza prints out the number of toppings and what the toppings are. (This is untested code but should work.)

The reason for adding a new instance of PizzaTopping to the pizza, is because toppings should not be shared between different Pizza instances. Also, you should make sure that you check for null objects if they should not be allowed. Throwing an IllegalArgumentException for null objects is a good way that these illegal arguments do not make it (or break) into your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM