Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

441
Visualizações
Update arraylist method in Java

Im trying to make a code that similar to this removeMenu method but I want to make another method that updates the quantity of the product in the menu i've ordered.

Code for restaurant controller:

Menu coffee = new Menu("Coffee", 30);
Menu icedTea = new Menu("Iced Tea", 50);
Menu hotChoco = new Menu("Hot Chocolate", 30);
/*
   constructor ommittet.
*/
private ArrayList<Menu> menulist;

public void removeMenu(Menu menumodel) {
    for (Menu temp : menulist) {
        if (temp.equals(menumodel)) {
            menulist.remove(temp);
            break;
        }
    }
}

public void updateMenu(int updateQuant) {
            //menulist.set(3, updateQuant);
        }
    }
    
}

Restaurant class

RestaurantController controller1 = new RestaurantController();
    private void DeleteProductActionPerformed(java.awt.event.ActionEvent evt) {                                              
    selectedProduct = controller1.getMenulist().get(JTableOrderReceived.getSelectedRow());
    if (selectedProduct != null) {
        int ans = JOptionPane.showConfirmDialog(this, "The selected product will be removed! ", "DELETE PRODUCT", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
        if (ans == JOptionPane.YES_OPTION) {
            controller1.removeMenu(selectedProduct);
            refTable();
        }
    }
}                                             
private void UpdateProduct(java.awt.event.ActionEvent evt) {                               
selectedProduct controller1.getMenulist().get(JTableOrderReceived.getSelectedRow());
    if (selectedProduct != null) {
        int parseQuant = (int) selectedProduct.getQuantity();
        String uQuant = JOptionPane.showInputDialog(this, "Update quantity " + selectedProduct.getItemName() + "\nPrice: " + selectedProduct.getPrice() + "\nCurrent order: " + parseQuant);
        int nQuant = Integer.parseInt(uQuant);
        controller1.updateMenu(nQuant);
        refTable();
    }
}  

Im having problems on the updateMenu method in restaurant controller class, can anyone help on how to?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

First of all you have to read about the basic usage of an arraylist.

Your remove implementation has a wrong assumption:

public void removeMenu(Menu menumodel) {
    for (Menu temp : menulist) {
        if (temp.equals(menumodel)) {
            menulist.remove(temp);
            break;
        }
    }
}

is equal to:

public void removeMenu(Menu menumodel) {
     menulist.remove(temp);
}

The implementation of Arraylist will use hashCode() and equals() to identify the correct object within the arraylist and remove it for you. Thus you do not have to iteratate over the array.

See also how to remove object from a list, which is iterated: Calling remove in foreach loop in Java

To update an object within the arraylist, you first have to retrieve the object and then update it. This is because the arrayslist stores a reference to the object and not a copy.

Thus your update method has to identify the object to update and then set the quantity accordenly:

In my example you identify the object by domain name: Here "name of the menue" eg. Coffee.

public void updateMenu(int updateQuant, String name) {
    for (Menu temp : menulist) {
        if (temp.getName().equals(name)) {
            temp.setQuant(updateQuant);
            break;
        }
    }
}

Further you already obtain the correct object within your UpdateProduct EventListener:

selectedProduct

This is the SAME object as it would be found by the updateMenu method. Thus you may invoke

selectedProduct.setQuant(...) 

here and it will update the arraylist as well. This is because there is a reference to the object on different variables. Both reference the same object.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda