Search code examples
javauser-interfacearraylistnetbeansjbutton

how to make ArrayList accessible by all JFrames and how to update it?


So I'm making a mock Starbucks app and I want that everytime a customer clicks the "Order" button, the product is added to an ArrayList and for this ArrayList to be accessed by all. I'm kind of confused where to insert the global ArrayList code...

This is code for my btnOrder:

private void btnOrderActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String name = lblName.getText();
    String size = cmbSize.getSelectedItem().toString();
    int quantity = (int) spnrQuantity.getValue();
    int price=0;

    if (size.equals("Tall 12oz")) {
        price = 170;
    } else if (size.equals("Grande 16oz")) {
        price = 180;
    } else if (size.equals("Venti 20oz")) {
        price = 190;
    }

    Global.list.add(new Object());
    new Receipt(name, size, quantity, price).setVisible(true);
}

This is code for my Receipt frame which contains the JTable so I can display orders:

public class Receipt extends javax.swing.JFrame {

/**
 * Creates new form Receipt
 */
public Receipt() {
    initComponents();
}

String size, name;
int quantity, price;

public Receipt(String name, String size, int quantity, int price) {
    initComponents();

    this.name = name;
    this.size = size;
    this.quantity = quantity;
    this.price = price;

    addToTable();
}

void addToTable() {
    DefaultTableModel table = (DefaultTableModel) tblCart.getModel();

    Vector v = new Vector();

    v.add(name);
    v.add(size);
    v.add(price);
    v.add(quantity);

    table.addRow(v);
}

And this is the code for the accessible ArrayList:

public class Global {
    public static ArrayList<Object> list = new ArrayList<>();

    private Global(){

    }
}

Solution

  • Managing global state can be a nightmare, while you can use a singleton to solve the issue, it violates the Single responsibility principle. It also removes access control, allowing anyone to modify the list in whatever way they see fit without control.

    Another solution is to use some kind of model, which can passed between the various components, if you make clever use of interfaces, you can control who can do what and when.

    This is a core concept of Model-View-Controller and program to interface not implementation principle.

    The basic idea is you would create a "model" which maintains the data you want to share, in this, primarily the items in the customer's order (and maybe the customer's name)

    You would create an appropriate order and pass a reference of it to the "order" view, where it would be able to add/remove/update items to the model. When complete, the "controller" would then pass the same instance of the model to the "check-out" view, which would use this information to generate a bill (and possibly a payment information) and finally store the transaction

    You would then be able to take the information from the model at the end and tell what has happened.

    Because there are complex states you might need to control, you might need more than one model, for example, you could pass the "order" model to the "check-out" view, but it could create a "transaction" model, which wraps the "order" model