2

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Xen
  • 105
  • 9

1 Answers1

2

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Marcinek
  • 2,144
  • 1
  • 19
  • 25