Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My clear button does not clear the previous orders of coffee. It continues to add the total in the next purchases. Please help me fix

My clear button does not clear the previous orders of coffee. It continues to add the total in the next purchases. Please help me fix my clear button so that that it goes back to 0.

import javax.swing.*;

import java.awt.event.*;

import javax.swing.JOptionPane;

public class JDemoFrameThatCloses extends javax.swing.JFrame

{

double subtotalCost = 0;

double taxTotalCost;

double GrandTotal;

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 static void main1(String[] args)

{

JDemoFrameThatCloses myclass = new JDemoFrameThatCloses();

myclass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Set Program to exit on close.

}

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)); // NOI18N

jLabel1.setText("ABC Company");

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+"\t"+quantity+"x"+cost+ "\t" + (cost*quantity) +" ");

subtotalCost += cost*quantity;

taxTotalCost += (subtotalCost*TAX);

GrandTotal = subtotalCost + taxTotalCost;

}

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

{

jTextArea1.setText("");

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 $" + subtotalCost + " " + "Tax Total is $ " + taxTotalCost + " " + "Grand Total Cost is $ " + GrandTotal );}

public static void main(String args[]) {

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(JDemoFrameThatCloses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(JDemoFrameThatCloses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(JDemoFrameThatCloses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(JDemoFrameThatCloses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

}

//

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run()

{

new JDemoFrameThatCloses().setVisible(true);

}

});

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data In Just 7 Chapters

Authors: Prof Marcus Vinicius Pinto

1st Edition

B09NZ7ZX72, 979-8787954036

More Books

Students also viewed these Databases questions

Question

3. Avoid making mistakes when reaching our goals

Answered: 1 week ago