-1

I am making a program in which you can create ingredients and also create menus with these ingredients.

I have a controller class which in turn controls other classes. This one is called CucharitaGUI and it controls both InventoryModuleControllerGUI and MenuModuleControllerGUI. I have an ArrayList of ingredients in the class called InventoryManager(connected to InventoryModuleControllerGUI) and another ArrayList which I want to have the same Ingredient objects in the MenuModuleControllerGUI class and be able to edit them without editing the original ArrayList (the one from InventoryManager). For this reason I am trying to clone it, but I get a warning.

CucharitaGUI class:

 //Attributes
    public UserManager userManager;
    
    private StaffModuleControllerGUI staffModule;
    
    public InventoryModuleControllerGUI inventoryModule;
    
    public MenuModuleControllerGUI menuModule;
    
    public OrderModuleControllerGUI orderModule;
    
    //private InventoryManager inventoryManager; 
    
    private Stage loginStage;
     
    //@FXML Attributes:
    
    @FXML
    private PasswordField pFLogin;

    @FXML
    private Button btnLogIn;

    @FXML
    private TextField txtUserLogin;

    @FXML
    private Pane loginPane;
    
    @FXML
    private Pane taskManagerPane;
    
    
    public CucharitaGUI() 
    {
        staffModule = new StaffModuleControllerGUI(this);  
        inventoryModule = new InventoryModuleControllerGUI(this);
        menuModule = new MenuModuleControllerGUI(this);
        orderModule = new OrderModuleControllerGUI(this);
        userManager = new UserManager();
        //inventoryManager = new InventoryManager();
        loginStage = new Stage();
        
    }
    @FXML
    private void openInventoryModule(ActionEvent event) throws IOException 
    {
    
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("inventoryModule.fxml"));
        fxmlLoader.setController(inventoryModule);
        Parent root = fxmlLoader.load();
        Scene scene = new Scene(root);
        loginStage.setScene(scene);
        loginStage.setTitle("InventoryModule");
        loginStage.show(); 
        
        inventoryModule.initializeTableView();
        inventoryModule.initializeComboBox();
    }

    @FXML
    void openMenuModule(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("menuModule.fxml"));
        fxmlLoader.setController(menuModule);
        Parent root = fxmlLoader.load();
        Scene scene = new Scene(root);
        loginStage.setScene(scene);
        loginStage.setTitle("MenuModule");
        loginStage.show(); 
        
        menuModule.initializeTableView();
        menuModule.initializeComboBox();
    }

InventoryModuleController class:

  //Attributes
  private Stage inventoryModuleStage;
   
  private ObservableList<Ingredient> observableInventoryList;
      
  private ObservableList<String> observableUnitsList;
  
  public InventoryManager inventoryManager;
  
  private CucharitaGUI cucharitaGUI;
  
      
      public InventoryModuleControllerGUI(CucharitaGUI cucharitaGUI){
          
          setInventoryModuleStage(new Stage());
          inventoryModulePane = new Pane();
          inventoryManager = new InventoryManager();
          this.cucharitaGUI = cucharitaGUI;
          
      }

InventoryManager class:

public class InventoryManager {
    
    public ArrayList<Ingredient> ingredients;
    
    public InventoryManager()
    {
        ingredients = new ArrayList<Ingredient>();
    }

    public ArrayList<Ingredient> getIngredients()
    {
        return ingredients;
    }

MenuModuleControllerGUI class:

    ArrayList<Ingredient> newIngredientsToPreview;                     
    public MenuManager menuManager;

    private CucharitaGUI cucharitaGUI;
    
    private boolean dishNameReadyToCreate=false;
    
    private boolean dishIngredientsReadyToCreate=false;

    public MenuModuleControllerGUI(CucharitaGUI cucharitaGUI){

        setMenuModuleStage(new Stage());
        menuModulePane = new Pane();
        menuManager = new MenuManager();
        ArrayList<Ingredient>ingredientClone=cucharitaGUI.
        inventoryModule.inventoryManager.getIngredients();
        newIngredientsToPreview= (ArrayList<Ingredient>)ingredientClone.clone();
        this.cucharitaGUI = cucharitaGUI;       
    }

I get the warning here:

newIngredientsToPreview= (ArrayList<Ingredient>)ingredientClone.clone();

It says
Type safety: Unchecked cast from Object to ArrayList<Ingredient>

Does anyone know how to fix it?

Laurel
  • 5,965
  • 14
  • 31
  • 57
  • You can read [**this**](https://stackoverflow.com/questions/715650/how-to-clone-arraylist-and-also-clone-its-contents?rq=1) for your task. – btomar Sep 26 '21 at 08:49

1 Answers1

1

Don't use clone(). It is horribly broken.

Use the copy constructor:

newIngredientsToPreview = new ArrayList<>(ingredientClone);
Andy Turner
  • 137,514
  • 11
  • 162
  • 243