Joe\'s Automotive performs the following routine maintenance services: Oil Chang
ID: 3625876 • Letter: J
Question
Joe's Automotive performs the following routine maintenance services:Oil Change- 26.00
Lube Job - 18.00
Radiator Flush - 30.00
Transmission Flush - 80.00
Inspection - 15.00
Muffer Replacement - 100.00
Tire Rotation - 20.00
Joe also performs other nonroutine ervices and charges for parts and labor (20.00 per hour). Create a GUI application that displays the total for a customers visit to Joe's.
Everything works out ok but, one thing that it's not working is when I press the Calculation button I get an error.Thanks
//This is For the services part
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Service extends JPanel {
// The following constants are used to indicate
// the cost of services for automotive.
public final double OIL_CHANGE = 26.00;
public final double LUBE_JOB = 18.00;
public final double RADIATOR_FLUSH = 30.00;
public final double TRANSMISSION_FLUSH = 80.00;
public final double INSPECTION = 15.00;
public final double MUFFLER_REPLACEMENT = 100.00;
public final double TIRE_ROTATION = 20.00;
// for text field.
private JTextField calcTextField;
// Check boxes for the available services.
private JCheckBox Oil_change;
private JCheckBox Lube_job;
private JCheckBox Radiator_flush;
private JCheckBox Transmission_flush;
private JCheckBox Inspection;
private JCheckBox Muffler_replacement;
private JCheckBox Tire_rotation;
/**
* Constructor
*/
public Service() {
// Create a GridLayout manager with
// seven rows and one column.
setLayout(new GridLayout(7, 1));
// Create the check boxes.
Oil_change = new JCheckBox("Oil Change ($26.00)");
Lube_job = new JCheckBox("Lube Job ($18.00)");
Radiator_flush = new JCheckBox("Radiator Flush ($30.00)");
Transmission_flush = new JCheckBox("Transmission Flush ($80.00)");
Inspection = new JCheckBox("Inspection ($15.00)");
Muffler_replacement = new JCheckBox("Muffler Replacement ($100.00)");
Tire_rotation = new JCheckBox("Tire Rotation ($20.00)");
// Add a border around the panel.
setBorder(BorderFactory.createTitledBorder("Routine Services"));
// Add the check boxes to this panel.
add(Oil_change);
add(Lube_job);
add(Radiator_flush);
add(Transmission_flush);
add(Inspection);
add(Muffler_replacement);
add(Tire_rotation);
}
/**
* The actionCalculate method returns the cost of the selected services.
*/
public double actionCalculate() {
double total = 0;
if (Oil_change.isSelected())
total = total + OIL_CHANGE;
if (Lube_job.isSelected())
total = total + LUBE_JOB;
if (Radiator_flush.isSelected())
total = total + RADIATOR_FLUSH;
if (Transmission_flush.isSelected())
total = total + TRANSMISSION_FLUSH;
if (Inspection.isSelected())
total = total + INSPECTION;
if (Muffler_replacement.isSelected())
total = total + MUFFLER_REPLACEMENT;
if (Tire_rotation.isSelected())
total = total + TIRE_ROTATION;
return total;
}
}
____________________________________________________________________________
//This for NonRoutine Services
import java.awt.*;
import javax.swing.*;
public class NonService extends JPanel {
public final double perHourCharge = 20.00; // per hour labor cost
private JLabel partsLabel; // references parts charges label
private JLabel laborLabel; // references hours of labor label
private JTextField partsTextField; // references parts charges text field
private JTextField laborTextField; // references hours of labor text field
private JPanel panel;
public NonService() {
// Create labels
partsLabel = new JLabel("Parts Charges:");
laborLabel = new JLabel("Hours of Labor:");
// Create the text Fields
partsTextField = new JTextField(10);
laborTextField = new JTextField(10);
// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("Nonroutine services"));
add(partsLabel);
add(partsTextField);
add(laborLabel);
add(laborTextField);
panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
setLayout(new GridLayout(2, 2));
}
public double getNonroutineServicesCost()
{
return Double.parseDouble(partsTextField.getText())+(Double.parseDouble(laborTextField.getText())*perHourCharge);
}
}
______________________________________________________________________________
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
/**
* The CalculatorGUI class creates the GUI for Joe's Automotive application.
*/
public class CalculatorGUI extends JFrame {
// The following variables will reference the
// custom panel objects.
private Service sc; // Service panel
private NonService ns; // NonService panel
// The following variables will reference objects
// needed to add the Calculate and Exit buttons.
private JPanel buttonPanel; // To hold the buttons
private JButton calcButton; // To calculate the cost
private JButton exitButton; // To exit the application
/**
* Constructor
*/
public CalculatorGUI() {
// Display title.
super("Joe's Automotive");
setLocationRelativeTo(null);
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a BorderLayout manager for
// the content pane.
setLayout(new BorderLayout());
// Create the custom panels.
sc = new Service();
ns = new NonService();
// Call the buildButtonPanel method to
// create the button panel.
buildButtonPanel();
// Add the components to the content pane.
add(sc, BorderLayout.PAGE_START);
add(ns, BorderLayout.CENTER);
add (buttonPanel,BorderLayout.PAGE_END);
// Pack the contents of the window and display it.
pack();
setVisible(true);
}
/**
* The buildButtonPanel method builds the button panel.
*/
private void buildButtonPanel() {
// Create a panel for the buttons.
buttonPanel = new JPanel();
// Create the buttons.
calcButton = new JButton("Calculate Charges");
exitButton = new JButton("Exit");
// Register the action listeners.
calcButton.addActionListener(new CalcButtonListener());
exitButton.addActionListener(new ExitButtonListener());
// Add the buttons to the button panel.
buttonPanel.add(calcButton);
buttonPanel.add(exitButton);
}
/**
* Private inner class that handles the event when the user clicks the
* Calculate button.
*/
private class CalcButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// The order subtotal
double total; // The order total
// Calculate the subtotal.
total = sc.actionCalculate() + ns.getNonroutineServicesCost();
// Create a DecimalFormat object to format
// the total as a dollar amount.
DecimalFormat dollar = new DecimalFormat("0.00");
// Display the charges.
JOptionPane.showMessageDialog(null, "Total: $"
+ dollar.format(total));
}
}
/**
* Private inner class that handles the event when the user clicks the Exit
* button.
*/
private class ExitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Exit the application.
System.exit(0);
}
}
public static void main(String args[]) {
new CalculatorGUI();
}
}
Explanation / Answer
Great code. I found your mistake in the NonService class at the getNonroutineServiceCost() method. The problem occurs because if the field for the parts or labor is left blank, a call to .getText() returns an empty string, which cannot be parsed by Double.parseDouble(). The solution is to check if the return value of .getText() is equal to an empty string "" before trying to parse. If someone leaves it blank or types something wrong like "asdf" instead of "30" your program should either ignore it or show an error message. Else it should try to parse the call to .getText().
Below is a sample
public double getNonroutineServicesCost()
{
double total = 0;
if (!partsTextField.getText().equals("")){
try{
total += Double.parseDouble(partsTextField.getText());
}
catch(NumberFormatException e1){}//ignore a mistyped value
}
if (!laborTextField.getText().equals("")){
try{
total += Double.parseDouble(laborTextField.getText()) * perHourCharge;
}
catch(NumberFormatException e2){}//ignore a mistyped value
}
return total;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.