Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please Help: when I display the quantity it is not addind all the coffees purcha

ID: 3589892 • Letter: P

Question

Please Help: when I display the quantity it is not addind all the coffees purchases; however, it is only displaying the last number entered. I need to display the enitre quanitity of purchase.
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class JDemoFrameThatCloses extends javax.swing.JFrame
{
double subtotalCost = 0;
double taxTotalCost;
double GrandTotal;
DecimalFormat df = new DecimalFormat("0.00");
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration

public JDemoFrameThatCloses ()
{
designComponents ();//call on the design class.
}
private void designComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jTextField2 = new javax.swing.JTextField();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Dialog", 1, 18));
jLabel1.setText("Dripping Cups of Coffee");
jLabel2.setText("By Jordan Ramos");
jTextField1.setText("Enter Name");
jButton1.setText("Add to Order");
jButton1.addActionListener(new java.awt.event.ActionListener() {
  
public void actionPerformed(java.awt.event.ActionEvent evt)
{
addtoOrderActionPerformed(evt);
}
});
jTextField2.setText("Quantity");
jRadioButton1.setText("Mocha");
buttonGroup1.add(jRadioButton1);
jRadioButton2.setText("Latte");
buttonGroup1.add(jRadioButton2);
jRadioButton3.setText("Drip");
buttonGroup1.add(jRadioButton3);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setEnabled(false);
jScrollPane1.setViewportView(jTextArea1);
jButton2.setText("Complete Order");
jButton2.addActionListener(new java.awt.event.ActionListener() {
  
public void actionPerformed(java.awt.event.ActionEvent evt)
{
completeOrderActionPerformed(evt);
}
});
jButton3.setText("Clear");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{
clearActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(210, 210, 210))
.addComponent(jTextField2))
.addGroup(layout.createSequentialGroup()
.addComponent(jRadioButton1)
.addGap(18, 18, 18)
.addComponent(jRadioButton2)
.addGap(18, 18, 18)
.addComponent(jRadioButton3)
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(49, Short.MAX_VALUE))  
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(84, 84, 84))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton3)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addGap(65, 65, 65))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2)
.addComponent(jRadioButton3)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton3))
.addContainerGap(16, Short.MAX_VALUE))
);
pack();
} // end of design class   
private void addtoOrderActionPerformed(java.awt.event.ActionEvent evt)
{
String item;
double cost;
double TAX = .0975;
  
if (jRadioButton1.isSelected()) {
item = "Mocha";
cost = 3.75;
}
else if (jRadioButton2.isSelected()) {
item = "Latte";
cost = 3.25;
}
else if (jRadioButton3.isSelected()) {
item = "Drip";
cost = 1.75;
}
else {
System.err.println("Nothing selected");
return;
}
int quantity = Integer.parseInt(jTextField2.getText());
jTextArea1.append(item+" "+quantity+"x"+cost+ " " + (cost*quantity) +" ");
subtotalCost += cost*quantity;
taxTotalCost += (subtotalCost*TAX);
GrandTotal = subtotalCost + taxTotalCost;
}   
private void clearActionPerformed(java.awt.event.ActionEvent evt)
{
jTextArea1.setText("");
jTextField1.setText("");
jTextField2.setText("");
subtotalCost=0;
taxTotalCost=0;

buttonGroup1.clearSelection();
  
}   
private void completeOrderActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(this, "Customer Name is " + jTextField1.getText() + " " + "Quantity is " + jTextField2.getText() + " " + "SubTotal cost is $" + df.format(subtotalCost) + " " + "Tax Total is $ " + df.format(taxTotalCost) + " " + "Grand Total is $ " + df.format(GrandTotal) );}
public static void main(String args[])
{


new JDemoFrameThatCloses().setVisible(true);
}
  
}
  

Explanation / Answer

JDemoFrameThatCloses.java

Did changes in code to display all the orders related data.

Kindly go through the ITEM-01,02,03,04 description and code (which I included) to get better idea.

import java.text.DecimalFormat;

import javax.swing.JOptionPane;

/*****

* @author kishore

*

*

* ITEM-01----Declared a string buffer variable to store all the orders related data on addition of each order

*

* ITEM-02---This line of code actually stores each order related data in string buffer

*

* ITEM-03----Added a line of code to remove all the content from string buffer once user clicks on clear button, to clear all the previous orders data

*

*ITEM-04---This line is changed to display all orders related data (available in string buffer) instead of displaying last order data

*/

public class JDemoFrameThatCloses extends javax.swing.JFrame

{

/**

*

*/

private static final long serialVersionUID = 1L;

double subtotalCost = 0;

double taxTotalCost;

double GrandTotal;

DecimalFormat df = new DecimalFormat("0.00");

private javax.swing.ButtonGroup buttonGroup1;

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2;

private javax.swing.JButton jButton3;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JRadioButton jRadioButton1;

private javax.swing.JRadioButton jRadioButton2;

private javax.swing.JRadioButton jRadioButton3;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTextArea jTextArea1;

private javax.swing.JTextField jTextField1;

private javax.swing.JTextField jTextField2;

//ITEM-01 starts

private StringBuffer sb=new StringBuffer(50);

//ITEM-01 ends

// End of variables declaration

public JDemoFrameThatCloses ()

{

designComponents ();//call on the design class.

}

private void designComponents() {

buttonGroup1 = new javax.swing.ButtonGroup();

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jTextField1 = new javax.swing.JTextField();

jButton1 = new javax.swing.JButton();

jTextField2 = new javax.swing.JTextField();

jRadioButton1 = new javax.swing.JRadioButton();

jRadioButton2 = new javax.swing.JRadioButton();

jRadioButton3 = new javax.swing.JRadioButton();

jScrollPane1 = new javax.swing.JScrollPane();

jTextArea1 = new javax.swing.JTextArea();

jButton2 = new javax.swing.JButton();

jButton3 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Dialog", 1, 18));

jLabel1.setText("Dripping Cups of Coffee");

jLabel2.setText("By Jordan Ramos");

jTextField1.setText("Enter Name");

jButton1.setText("Add to Order");

jButton1.addActionListener(new java.awt.event.ActionListener() {

  

public void actionPerformed(java.awt.event.ActionEvent evt)

{

addtoOrderActionPerformed(evt);

}

});

jTextField2.setText("Quantity");

jRadioButton1.setText("Mocha");

buttonGroup1.add(jRadioButton1);

jRadioButton2.setText("Latte");

buttonGroup1.add(jRadioButton2);

jRadioButton3.setText("Drip");

buttonGroup1.add(jRadioButton3);

jTextArea1.setColumns(20);

jTextArea1.setRows(5);

jTextArea1.setEnabled(false);

jScrollPane1.setViewportView(jTextArea1);

jButton2.setText("Complete Order");

jButton2.addActionListener(new java.awt.event.ActionListener() {

  

public void actionPerformed(java.awt.event.ActionEvent evt)

{

completeOrderActionPerformed(evt);

}

});

jButton3.setText("Clear");

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt)

{

clearActionPerformed(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()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()

.addGap(33, 33, 33)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addComponent(jTextField1)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addComponent(jLabel1)

.addGap(210, 210, 210))

.addComponent(jTextField2))

.addGroup(layout.createSequentialGroup()

.addComponent(jRadioButton1)

.addGap(18, 18, 18)

.addComponent(jRadioButton2)

.addGap(18, 18, 18)

.addComponent(jRadioButton3)

.addGap(18, 18, 18)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))))

.addContainerGap(49, Short.MAX_VALUE))  

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addComponent(jLabel2)

.addGap(84, 84, 84))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addComponent(jButton3)

.addGap(18, 18, 18)

.addComponent(jButton2)

.addGap(65, 65, 65))))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(32, 32, 32)

.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jLabel2)

.addGap(18, 18, 18)

.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(12, 12, 12)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jRadioButton1)

.addComponent(jRadioButton2)

.addComponent(jRadioButton3)

.addComponent(jButton1))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jButton2)

.addComponent(jButton3))

.addContainerGap(16, Short.MAX_VALUE))

);

pack();

} // end of design class   

private void addtoOrderActionPerformed(java.awt.event.ActionEvent evt)

{

String item;

double cost;

double TAX = .0975;

  

if (jRadioButton1.isSelected()) {

item = "Mocha";

cost = 3.75;

}

else if (jRadioButton2.isSelected()) {

item = "Latte";

cost = 3.25;

}

else if (jRadioButton3.isSelected()) {

item = "Drip";

cost = 1.75;

}

else {

System.err.println("Nothing selected");

return;

}

int quantity = Integer.parseInt(jTextField2.getText());

jTextArea1.append(item+" "+quantity+"x"+cost+ " " + (cost*quantity) +" ");

subtotalCost += cost*quantity;

taxTotalCost += (subtotalCost*TAX);

GrandTotal = subtotalCost + taxTotalCost;

  

//ITEM-2 starts

sb.append("Customer Name is " + jTextField1.getText() + " " + "Quantity is " + jTextField2.getText() + " " + "SubTotal cost is $" + df.format(subtotalCost) + " " + "Tax Total is $ " + df.format(taxTotalCost) + " " + "Grand Total is $ " + df.format(GrandTotal));

//ITEM-2 ends

}   

private void clearActionPerformed(java.awt.event.ActionEvent evt)

{

jTextArea1.setText("");

jTextField1.setText("");

jTextField2.setText("");

subtotalCost=0;

taxTotalCost=0;

buttonGroup1.clearSelection();

  

  

//ITEM-3 starts

sb.delete(0, sb.length());

//ITEM-3 ends

  

}   

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

//ITEM-4 stars

JOptionPane.showMessageDialog(this, sb.toString() );}//ITEM-04 ends

public static void main(String args[])

{

new JDemoFrameThatCloses().setVisible(true);

}

  

}