How do I add items into ArrayList one by one and not replace each other?

Viewed 185

I want to create a shopping list application. I'm not sure how to add items one by one so the list expands instead of replacing an existing item. This is my code at the moment. I was unsuccessfully trying to use for loop.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    List<ShoppingItem> list = new ArrayList<>();

    String name = ShoppingListUI.jTextField1.getText();    
    double price = Double.parseDouble(jTextField2.getText());
    int quantity = Integer.parseInt(jTextField3.getText());

    ShoppingItem item = new ShoppingItem(name, price, quantity);
    list.add(item);

    for (ShoppingItem temp : list) {
        System.out.println("Item: " + temp.getName() + ", Price: " + temp.getPrice() + ", Quantity: " + temp.getQuantity());
    }

    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");    
}
4 Answers
Related