I have a program consists of 15 jRadioButton which are phone products, jButton to calculate the total amount, and a purchase button. The program runs smooth and actually calculates the total sum of the selected radio buttons. The only thing that is lacking is that the quantity. I want the program to be able to choose the quantity of a selected product (ex. "Iphone 6" "How many do you wish to buy?"). Then, after you click "Total amount" it will be able to calculate, including the quantity(ex. "Iphone6" "How many: 6" "Samsung S5" "How many:4" "total Amount" "The sum of the products").
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author user
*/
public class New extends javax.swing.JFrame {
/**
* Creates new form New
*/
public New() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
a = new javax.swing.JRadioButton();
b = new javax.swing.JRadioButton();
c = new javax.swing.JRadioButton();
d = new javax.swing.JRadioButton();
e = new javax.swing.JRadioButton();
f = new javax.swing.JRadioButton();
g = new javax.swing.JRadioButton();
h = new javax.swing.JRadioButton();
i = new javax.swing.JRadioButton();
j = new javax.swing.JRadioButton();
k = new javax.swing.JRadioButton();
l = new javax.swing.JRadioButton();
m = new javax.swing.JRadioButton();
n = new javax.swing.JRadioButton();
o = new javax.swing.JRadioButton();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
jLabel1.setText("Choose Items:");
a.setText("Iphone 6: P21,991.35");
a.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aActionPerformed(evt);
}
});
b.setText("Iphone 5s: P14,800.23");
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bActionPerformed(evt);
}
});
c.setText("Iphone 5: P11,000.43");
c.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cActionPerformed(evt);
}
});
d.setText("Xperia M2: P7,500.54");
d.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
dActionPerformed(evt);
}
});
e.setText("Xperia zl2 sol25: P11,200.82");
e.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
eActionPerformed(evt);
}
});
f.setText("Xperia Z: P9,500.34");
f.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fActionPerformed(evt);
}
});
g.setText("Xperia U: P4,500.23");
g.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
gActionPerformed(evt);
}
});
h.setText("Xperia C White: P4,119.12");
h.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
hActionPerformed(evt);
}
});
i.setText("Asus Zenphone 2: P11,500.23");
i.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
iActionPerformed(evt);
}
});
j.setText("Asus Zenphone 5: P15,000.54");
j.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jActionPerformed(evt);
}
});
k.setText("Asus ZenPad C 7.0: P5,000.12");
k.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
kActionPerformed(evt);
}
});
l.setText("Samsung Galaxy Note edge: P13,000.54");
l.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lActionPerformed(evt);
}
});
m.setText("Samsung Galaxy Note4: P15,799.23");
m.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mActionPerformed(evt);
}
});
n.setText("Samsung Galaxy S5: P12,000.65");
n.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nActionPerformed(evt);
}
});
o.setText("Samsung Galaxy S6: P20,500.22");
o.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
oActionPerformed(evt);
}
});
jButton1.setText("Total Amount");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jButton2.setText("Purchase");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(a)
.addComponent(b)
.addComponent(c)
.addComponent(d)
.addComponent(e)
.addComponent(f)
.addComponent(g))
.addGap(59, 59, 59)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(o)
.addComponent(n)
.addComponent(m)
.addComponent(l)
.addComponent(k)
.addComponent(j)
.addComponent(i)))
.addComponent(h)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(25, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(a)
.addComponent(i))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b)
.addComponent(j))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(c)
.addComponent(k))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(d)
.addComponent(l))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(e)
.addComponent(m))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(f)
.addComponent(n))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(g)
.addComponent(o))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(h)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void cActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton c = new JRadioButton(Integer.toString(11,000)); // TODO add your handling code here:
}
private void fActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton f = new JRadioButton(Integer.toString(9,500)); // TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int choice = JOptionPane.showOptionDialog(null,
"Do you wish to add more items?",
"Purchase",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, null, null);
if (choice == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null, "Please choose the items you wish to add");
}
else{
JOptionPane.showMessageDialog(null, "Thank you for Buying! " + " Your Total purchase is:" + " P " + total );
System.exit(0);
}
}
private void aActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton a = new JRadioButton(Integer.toString(21,991));
}
private void bActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton b = new JRadioButton(Integer.toString(14,800)); // TODO add your handling code here:
}
private void dActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton d = new JRadioButton(Integer.toString(7,500)); // TODO add your handling code here:
}
private void eActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton e = new JRadioButton(Integer.toString(11,200)); // TODO add your handling code here:
}
private void gActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton g = new JRadioButton(Integer.toString(4,500)); // TODO add your handling code here:
}
private void hActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton h = new JRadioButton(Integer.toString(4,119)); // TODO add your handling code here:
}
private void iActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton i = new JRadioButton(Integer.toString(11,500)); // TODO add your handling code here:
}
private void jActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton j = new JRadioButton(Integer.toString(15,000)); // TODO add your handling code here:
}
private void kActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton k = new JRadioButton(Integer.toString(5,000)); // TODO add your handling code here:
}
private void lActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton l = new JRadioButton(Integer.toString(13,000)); // TODO add your handling code here:
}
private void mActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton m = new JRadioButton(Integer.toString(15,799)); // TODO add your handling code here:
}
private void nActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton n = new JRadioButton(Integer.toString(12,000)); // TODO add your handling code here:
}
private void oActionPerformed(java.awt.event.ActionEvent evt) {
JRadioButton o = new JRadioButton(); // TODO add your handling code here:
}
private double total = 0;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Added Code
for (JRadioButton button : rbuttons){
if (button.isSelected()){
String[] parts = button.getText().split(":");
total += Double.parseDouble(parts[1]);
}
}
jLabel2.setText("" + total);
jLabel2.validate();
//Added Code
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(New.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(New.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(New.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(New.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new New().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JRadioButton a;
private javax.swing.JRadioButton b;
private javax.swing.JRadioButton c;
private javax.swing.JRadioButton d;
private javax.swing.JRadioButton e;
private javax.swing.JRadioButton f;
private javax.swing.JRadioButton g;
private javax.swing.JRadioButton h;
private javax.swing.JRadioButton i;
private javax.swing.JRadioButton j;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JRadioButton k;
private javax.swing.JRadioButton l;
private javax.swing.JRadioButton m;
private javax.swing.JRadioButton n;
private javax.swing.JRadioButton o;
// End of variables declaration
}
Try it Like that: Put jSpinnerNumberModels on your jFrame and use the Values to calculate the Sum. change your radiobuttons to labels. Then instead of checking if the radiobuttons are checked, you check if the Spinners Value is > 0