Question Details
Hello,
I am trying to write a program in Java using JGrasp (from textbook Starting out with Java Early Objects - Chapter 11 Programming Challenge 4).
I am having trouble after adding in some calculations methods into the calcButton Action Listener. I am getting an error stating:
TravelExpenses.java:203: illegal start of expression
private void getData()
^
TravelExpenses.java:233: ';' expected
}
^
TravelExpenses.java:243: ';' expected
and I havent even really added much code to do the calculations yet. I think I am supposed to be defining getData somewhere but I am not sure as to where. I am also not sure about the code I have put in the resetButton Action Listener either.
Please see all the specification information below.
The code I have created so far is listed below the specifications.
THIS IS THE DESCRIPTION FROM THE BOOK:
Travel Expenses
Create a GUI application that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide:
Number of days on the trip
Amount of airfare, if any
Amount of car rental fees, if any
Number of miles driven, if a private vehicle was used
Amount of parking fees, if any
Amount of taxi charges, if any
Conference or seminar registration fees, if any
Lodging charges, per night
The company reimburses travel expenses according to the following policy:
$37 per day for meal (this is given regardless of if used so no text field is needed for input)
Parking fees, up to $10.00 per day
Taxi charges up to $20.00 per day
Lodging charges up to $95.00 per day
If a private vehicle is used, $0.27 per mile driven
This application should calculate and display the following:
Total expenses incurred by the businessperson
The total allowable expenses for the trip
The excess that must be paid by the businessperson, if any
The amount saved by the businessperson if the expenses were under the total allowed
THESE ARE THE SPECIFICATIONS FOR THE PROGRAM:
See pseudocode below to help with programming:
//Import required classes
// Header Comments go here
public class TravelExpenses extends JFrame
{
//Declare components
// A panel to hold labels and text fields
// A panel for the buttons
// Label prompting for number of days
// Label prompting for airfare
// Label prompting for car rental
// Label prompting for miles driven
// Label prompting for parking fees
// Label prompting for taxi charges
// Label prompting for conf. registration
// Label prompting for per-night lodging
// Textfield for Number of days
// Textfield for Airfare
// Textfield for Car rental
// Textfield for Miles driven
// Textfield for Parking fees
// Textfield for Taxi charges
// Textfield for Conference registration
// Textfield for Per-night lodging
//Button to Calculates everything
// Button to Resets everything
// Declare the 5 required Constants
//Constructor
public TravelExpenses()
{
// Set the title.
// Specify what happens when the close button is clicked.
// Build the panel that contains the labels and text fields.
// Build the panel that contains the buttons.
// Add the panels to the content pane.
// Pack and display the window.
}
//buildPanel method
private void buildPanel()
{
// Create the 8 labels prompting for the required data.
// Create the 8 text fields for the required data.
// Create a panel.
// Add a layout manager use GridLayout(10,2).
// Add the 8 labels, 8 text fields to the panel.
// Put an empty border around the panel (10, 10, 1, 10).
}
//buildButtonPanel method
private void buildButtonPanel()
{
// Create a button to calculate the expenses & reimbursement.
// Add an action listener to the button.
// Create a button to reset everything.
// Add an action listener to the button.
// Put the buttons in their own panel.
}
//CalcButtonListener is an action listener class for the calcbutton component.
private class CalcButtonListener implements ActionListener
{
//Declare variables for days, air, car rental, miles, meals parking, etc.
// actionPerformed method
public void actionPerformed(ActionEvent e)
{
// Declare variables for Actual expenses incurred, Reimbursable expenses
Amount saved, Excess amount spent, Output message
// Create a DecimalFormat object to format output.
// Get the data entered.
// Determine the actual expenses.
// Determine the reimbursement amounts.
// Display the results.
}
// getData method-gets the data entered by the user.
private void getData()
{
EXAMPLES : days = Integer.parseInt(numDays.getText());
air = Double.parseDouble(airfare.getText());
// There are 6 more to code
}
//Determine Actual Expenses method
private double determineActualExpenses()
{
// calculate actual expense total here and return as a double
}
//Determine Reimbursement method
private double determineReimbursement()
{
// Calculate meal reimbursement.
// Determine parking fee reimbursement.
// Determine taxi charge reimbursement.
// Determine lodging reimbursement.
// return reimbursable as a double
}
} // end of inner class
// ResetButtonListener is an action listener class for the resetButton component.
private class ResetButtonListener implements ActionListener
{
//actionPerformed method
public void actionPerformed(ActionEvent e)
{
//Set each textfield to zero
}
} // End of inner class
// The main method creates an instance of the TravelExpenses class.
public static void main(String[] args)
{
TravelExpenses te = new TravelExpenses();
}
}
MY CODE SO FAR:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
/**
* The TravelExpense class creates the GUI for the
* Travel Expenses application.
*/
public class TravelExpenses extends JFrame
{
// The following variables will reference the
// custom panel objects
private JPanel travelInfoPanel; // TravelInfo panel
private JPanel buttonPanel; // Buttons panel
// Labels for the Travel Information fields.
private JLabel numDaysOnTripLabel;
private JLabel amountAirfairLabel;
private JLabel amountCarRentalLabel;
private JLabel milesDrivenLabel;
private JLabel parkingFeesLabel;
private JLabel taxiFeesLabel;
private JLabel confRegLabel;
private JLabel lodgingChargesPerNightLabel;
// Text Fields for Travel Information entry
private JTextField numDaysOnTripTextField;
private JTextField amountAirfairTextField;
private JTextField amountCarRentalTextField;
private JTextField milesDrivenTextField;
private JTextField parkingFeesTextField;
private JTextField taxiFeesTextField;
private JTextField confRegTextField;
private JTextField lodgingChargesPerNightTextField;
// Buttons
private JButton resetButton;
private JButton calcButton;
// 5 required constants
private double mealsAmount = 37.00; // Meals amount reimbursed by company per day.
private double parkingFeesReimbursed = 10.00; // Parking Fees amount reimbursed by company per day.
private double taxiChargesReimbursed = 20.00; // Taxi Charges amount reimbursed by company per day.
private double lodgingChargesReimbursed = 95.00; // Lodging Charges amount reimbursed by company per day.
private double prVechiclePerMileReimbursed = 0.27; // Private Vehicle per miles reimbursment rate.
/**
* Constructor
*/
public TravelExpenses()
{
// Call the JFrame constructor & set the title.
super("Travel Expenses");
// Set the main window to open in the center of the screen.
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());
// Build the TravelInfo and Buttons panels
buildTravelInfoPanel();
buildButtonPanel();
// Add the panels to the frame's content pane
add(travelInfoPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
// Pack the contents of the window and display it.
pack();
setVisible(true);
}
// The buildTravelInfoPanel method adds the labels and text fiels to the TravelInfo panel.
private void buildTravelInfoPanel()
{
// Create the labels for TravelInfo fields
numDaysOnTripLabel = new JLabel("Number of days on trip: ");
amountAirfairLabel = new JLabel("Amount of airfair: ");
amountCarRentalLabel = new JLabel("Amount of car rental: ");
milesDrivenLabel = new JLabel("Miles driven: ");
parkingFeesLabel = new JLabel("Parking Fees: ");
taxiFeesLabel = new JLabel("Taxi fees: ");
confRegLabel = new JLabel("Conference registration: ");
lodgingChargesPerNightLabel = new JLabel("Lodging charges per night: ");
// Create the text boxes for TravelInfo user input
numDaysOnTripTextField = new JTextField(3);
amountAirfairTextField = new JTextField(8);
amountCarRentalTextField = new JTextField(8);
milesDrivenTextField = new JTextField(4);
parkingFeesTextField = new JTextField(6);
taxiFeesTextField = new JTextField(6);
confRegTextField = new JTextField(8);
lodgingChargesPerNightTextField = new JTextField(6);
// Create a panel to hold labels and text fields.
travelInfoPanel = new JPanel();
// Create GridLayout manager with 10 rows and 2 columns.
travelInfoPanel.setLayout(new GridLayout(10, 2));
// Add the 8 labels and 8 text fields to this panel.
travelInfoPanel.add(numDaysOnTripLabel);
travelInfoPanel.add(numDaysOnTripTextField);
travelInfoPanel.add(amountAirfairLabel);
travelInfoPanel.add(amountAirfairTextField);
travelInfoPanel.add(amountCarRentalLabel);
travelInfoPanel.add(amountCarRentalTextField);
travelInfoPanel.add(milesDrivenLabel);
travelInfoPanel.add(milesDrivenTextField);
travelInfoPanel.add(parkingFeesLabel);
travelInfoPanel.add(parkingFeesTextField);
travelInfoPanel.add(taxiFeesLabel);
travelInfoPanel.add(taxiFeesTextField);
travelInfoPanel.add(confRegLabel);
travelInfoPanel.add(confRegTextField);
travelInfoPanel.add(lodgingChargesPerNightLabel);
travelInfoPanel.add(lodgingChargesPerNightTextField);
// Add an empty border around the panel for spacing.
travelInfoPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 1, 10));
}
/**
* The buildButtonPanel method creates and adds the Reset and Calculate
* buttons to the TravelExpense panel as its own panel.
*/
private void buildButtonPanel()
{
// Create the calcButton.
calcButton = new JButton("Calculate");
// Register an event listener for the calcButton.
calcButton.addActionListener(new CalcButtonListener());
//Create the resetButton.
resetButton = new JButton("Reset");
// Register an event listener for the resetButton.
//resetButton.addActionListener(new ResetButtonListener());
// Create the Buttons panels.
buttonPanel = new JPanel();
// Create BorderLayout manager.
buttonPanel.setLayout(new BorderLayout(5, 5));
// Add the two buttons to the buttonPanel.
buttonPanel.add(resetButton, BorderLayout.WEST);
buttonPanel.add(calcButton, BorderLayout.CENTER);
// Add an empty border around the panel for spacing.
buttonPanel.setBorder(BorderFactory.createEmptyBorder(1, 10, 10, 10));
}
/**
* Private inner class that handles the event when the user clicks
* the Calculate button .
*/
private class CalcButtonListener implements ActionListener
{
// Declare variables used in calculations.
String input; // To hold the users input
int days; // Number of days on trip entered
double air; // Amount for airfair
double carRental; // Amount of car rental
double miles; // Miles driven
double parking; // Parking fees
double taxi; // Taxi fees
double confReg; // Conference Registration charges
double lodging; // Lodging charges per night
double mealsAmount;
public void actionPerformed(ActionEvent e)
{
//Declare variables for calculated items.
double actualExpenses;
double milesExpenses;
double allowableExpenses;
double excessAir;
double excessCarRental;
double excessParking;
double excessTaxi;
double excessLodging;
double excessAmountTotal;
double amountSaved;
double paidBackAmount;
// Create a DecimalFormat object to format the totals as dollar amounts.
DecimalFormat dollar = new DecimalFormat("$#,##0.00");
// Get Input Data the user entered in the text fields.
private void getData()
{
days = Integer.parseInt(numDaysOnTripTextField.getText());
air = Double.parseDouble(amountAirfairTextField.getText());
carRental = Double.parseDouble(amountCarRentalTextField.getText());
miles = Double.parseDouble(milesDrivenTextField.getText());
parking = Double.parseDouble(parkingFeesTextField.getText());
taxi = Double.parseDouble(taxiFeesTextField.getText());
confReg = Double.parseDouble(confRegTextField.getText());
lodging = Double.parseDouble(lodgingChargesPerNightTextField.getText());
}
// Determine actualExpenses method.
private double determineActualExpenses()
{
actualExpenses = air + carRental + parking + taxi + confReg + lodging + milesExpenses;
// need to calculate milesExpense = miles * prVechiclePerMileReimbursed (??)
// Calculate the allowableExpenses.
// Calculate the paidBackAmount.
// Display the Totals message box.
JOptionPane.showMessageDialog(null, "Total expenses: " + "\n" +
"Allowable expenses: " + "\n" +
"\n" + "Amount to be paid back: ");
}
}
/**
* Private inner class that handles the event when the user clicks
* the Reset button .
*/
private class ResetButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
numDaysOnTripTextField.setText("");
amountAirfairTextField.setText("");
amountCarRentalTextField.setText("");
milesDrivenTextField.setText("");
parkingFeesTextField.setText("");
taxiFeesTextField.setText("");
confRegTextField.setText("");
lodgingChargesPerNightTextField.setText("");
}
// The main method creates an instance of the TravelExpenses class.
public static void main(String[] args)
{
new TravelExpenses();
}
}
With all that being defined I have written the code to create the windows (panels) and they work when I compile and run them with no code listed for the calculate and reset buttons actionListener(s) the panels display correctly but again I am having trouble with the calculations in the ActionListener for the calcButton and the resetButton.
I greatly appreciate any help .

New Topic/Question
Reply




MultiQuote




|