Handling 8 toggle buttons in a point-of-sale application I am self-taught and trying to improve my programming skills.  My project is a point-of-sale application, with lots of buttons. I am curious how improve this section of code, as I seem to be going against the DRY principle.

I have a Customer object named cust, and each customer has an ID that is set to one of 8 different toggle buttons. 

    //update the sales to the customers
    //if cust1 toggle button is selected:
    if (cust1.isSelected()){
        //get the id number from the button
        cust = custDAO.getCustById(Integer.getInteger(cust1.getText()));
        //add the sales to the current value
        int sales = cust.getTotalSales();
        //set the value (note: cust.setTotalSales(cust.getTotalSales() + subtotal) doesn't work
        cust.setTotalSales(sales + subtotal);
        //update the Customer
        custDAO.updateCustomer(cust);
    }else if (cust2.isSelected()){
        cust = custDAO.getCustById(Integer.getInteger(cust2.getText()));
        int sales = cust.getTotalSales();
        cust.setTotalSales(sales + subtotal);
        custDAO.updateCustomer(cust);
    }else if (cust3.isSelected()){
        cust = custDAO.getCustById(Integer.getInteger(cust3.getText()));
        int sales = cust.getTotalSales();
        cust.setTotalSales(sales + subtotal);
        custDAO.updateCustomer(cust);
    }else if(cust4.isSelected()){
        cust = custDAO.getCustById(Integer.getInteger(cust4.getText()));
        int sales = cust.getTotalSales();
        cust.setTotalSales(sales + subtotal);
        custDAO.updateCustomer(cust);
    }else if (cust5.isSelected()){
        cust = custDAO.getCustById(Integer.getInteger(cust5.getText()));
        int sales = cust.getTotalSales();
        cust.setTotalSales(sales + subtotal);
        custDAO.updateCustomer(cust);
    }else if(cust6.isSelected()){
        cust = custDAO.getCustById(Integer.getInteger(cust6.getText()));
        int sales = cust.getTotalSales();
        cust.setTotalSales(sales + subtotal);
        custDAO.updateCustomer(cust);
    }else if(cust7.isSelected()){
        cust = custDAO.getCustById(Integer.getInteger(cust7.getText()));
        int sales = cust.getTotalSales();
        cust.setTotalSales(sales + subtotal);
        custDAO.updateCustomer(cust);
    }else if(cust8.isSelected()){
        cust = custDAO.getCustById(Integer.getInteger(cust8.getText()));
        int sales = cust.getTotalSales();
        cust.setTotalSales(sales + subtotal);
        custDAO.updateCustomer(cust);
    }else{
        //No button selected
        System.out.println("No customer selected");
    }
Short answer: You need the Java Collections framework.

Instead of eight standalone variables cust1...cust8, you should put them into a Collection so that you can easily iterate through them:

// assuming the buttons are of JButton type
Collection&lt;JButton&gt; buttons = Arrays.asList(cust1, cust2, /* ... */, cust8);
for (JButton button : buttons) {
    if (button.isSelected()) {
        cust = custDAO.getCustById(Integer.getInteger(button.getText()));
        int sales = cust.getTotalSales();
        cust.setTotalSales(sales + subtotal);
        custDAO.updateCustomer(cust);
    }
}


Longer answer...

It is not known how these lines are being run, but you can also consider writing event listeners, which is the typical implementation for checking if buttons have selected. For example, to use an ActionListener:

JButton button = new JButton();
// lambda expression below requires Java 8
button.addActionListener(actionEvent -&gt; {
    JButton source = (JButton) actionEvent.getSource();
    // using valueOf() as getInteger() looks at System.getProperty(),
    // unless that is really the intended usage
    cust = custDAO.getCustById(Integer.valueOf(source.getText()));
    int sales = cust.getTotalSales();
    cust.setTotalSales(sales + subtotal);
    custDAO.updateCustomer(cust);
});


Instead of having cust as a method, or even class, variable, you should scope it appropriately to eliminate any possible mis-references. Also, you can consider adding a more expressive method updateTotalSales(int incoming) that returns the object itself, so that you do not have to perform that 'manually' here:

public Customer {

    // ...

    public Customer updateTotalSales(int incoming) {
        setTotalSales(getTotalSales() + incoming);
        return this;
    }
}




JButton button = new JButton();
button.addActionListener(actionEvent -&gt; {
    JButton source = (JButton) actionEvent.getSource();
    Customer cust = custDAO.getCustById(Integer.valueOf(source.getText()));
    custDAO.updateCustomer(cust.updateTotalSales(subtotal));
});


In the unlikely (?) event that you still prefer/require the original iteration method by checking on isSelected(), the Collection-based implementation, coupled with stream-based processing, can also achieve the same outcome with the following, for simplicity:

// buttons from the short answer
buttons.stream().filter(JButton::isSelected).findFirst()
        .map(i -&gt; custDAO.getCustById(Integer.valueOf(i.getText())))
        .ifPresent(cust -&gt; custDAO.updateCustomer(cust.updateTotalSales(subtotal)));



By using a method reference JButton::isSelected, filter for the JButton where that is true.
Use findFirst() to 'get' it in the form of an Optional wrapper.
From the JButton instance, map() it to the desired Customer object, and finally call custDAO to 'consume' it via the ifPresent(Consumer) method.