Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

440
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!