I have two JFrame
in my GUI.
One is order
frame and another is payment
frame.
I would like to add onto the quantity
variable every time when the user clicked on the button
. I have added the increment-logic under the button method, but it seems only applicable in that method only. It won't add +1
into my array variable.
Here is my code for this:
class Bun extends javax.swing.JFrame {
String decimal = "0.00";
DecimalFormat df = new DecimalFormat(decimal);
public final String [] bname = new String [] {"Tuna Sandwich","Garlic Bread","Redbean Bun"};
public final double [] bprice = new double [] {1.20, 1.50, 1.50};
public double [] bsub = new double [9];
public int [] bquantity = new int[]{0,0,0};
public double bsubtotal;
private void BtnTunaSandwichActionPerformed(java.awt.event.ActionEvent evt) {
bquantity[0]++;
numTunaSandwich.setText(Integer.toString(bquantity[0]));
}
private void BtnGarlicBreadActionPerformed(java.awt.event.ActionEvent evt) {
bquantity[1]++;
numGarlicBread.setText(Integer.toString(bquantity[1]));
}
private void BtnRedbeanBunActionPerformed(java.awt.event.ActionEvent evt) {
bquantity[2]++;
numRedbeanBun.setText(Integer.toString(bquantity[2]));
}
public void calculatesub()
{
for(int counter=0;counter<bsub.length;counter++)
{
bsub[counter]=bquantity[counter]*bprice[counter];
}
}
public void setsubtotal()
{
for(int counter=0;counter<bsub.length;counter++)
{
bsubtotal += bsub[counter];
}
}
public String getsubtotal()
{
return (df.format(bsubtotal));
}
This is my Payment frame code:
private void subtotalActionPerformed(java.awt.event.ActionEvent evt) {
Bun bun = new Bun();
bun.calculatesub();
bun.setsubtotal();
subtotal.setText(bun.getsubtotal());
}
Whenever I run the program and try to calculate the subtotal, it shows 0
.
My interface: