Hello I am working on a Travel Expenses Java Program. I have most of it so far b
ID: 3760129 • Letter: H
Question
Hello I am working on a Travel Expenses Java Program. I have most of it so far but keep getting errors, Please help!!!! After i hit calculate i keep getting a large list of build errors!!!!!
Problem:
Write a TravelExpenses class that prompts the user in a JFrame using grid layout for the following inputs (all doubles, except the first):
Number of days on the trip
Amount of airfare (if any)
Amount of car rental charges (if any)
Number of miles driven in personal car (if any)
Amount of parking fees (if any)
Amount of taxi charges (if any)
Amount of tolls (if any)
Conference or seminar fees (if any)
Meal charges
Lodging charges
The company reimburses all travel expenses subject to the following maximums (a-d are restrictions on the maximum the company will reimburse the employee):
$37 per day for meals
Up to $10 per day parking fees
Up to $20 per day taxi charges
Up to $95 per day for lodging
If a personal vehicle is used, $0.27 per mile driven
If an employee goes over in a category, then the employee is responsible for paying the excess out-of-pocket. Being under in another category does NOT offset the excess.
The application should calculate and display:
Total expenses incurred,
The allowable expenses for the trip,
The excess that must be paid by the traveler, if any.
Program so Far:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TravelWorksheet extends JFrame
{
private JTextField dtripTextField;
private JTextField dlodgeTextField;
private JTextField airfareTextField;
private JTextField tcostTextField;
private JTextField milesTextField;
private JCheckBox airCheckBox;
private JCheckBox taxiCheckBox;
private JCheckBox pvecCheckBox;
private JCheckBox crentCheckBox;
private JButton calcButton;
private final int WINDOW_WIDTH = 500;
private final int WINDOW_HEIGHT = 500;
public TravelWorksheet()
{
setTitle("Travel Worksheet");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 3));
JTextField dtrip = new JTextField(4);
JCheckBox air = new JCheckBox("Air");
JCheckBox crent = new JCheckBox("Car rental");
JCheckBox taxi = new JCheckBox("Taxi");
JCheckBox pvec = new JCheckBox("Personal Vehicle");
JTextField dlodge = new JTextField(4);
JTextField airfare = new JTextField(4);
JTextField tcost = new JTextField(4);
JTextField miles = new JTextField(4);
JTextField sfees = new JTextField(4);
JTextField pfees = new JTextField(4);
JButton calcButton = new JButton("Calculate");
JLabel label1 = new JLabel("Days on Trip:");
JLabel label2 = new JLabel("Mode of Travel:");
JLabel label3 = new JLabel("Daily Lodging Cost:");
JLabel label4 = new JLabel("Air Fare Cost:");
JLabel label5 = new JLabel("Taxi Cost:");
JLabel label6 = new JLabel("Miles Driven:");
JLabel label7 = new JLabel("Seminar or Registration Fees:");
JLabel label8 = new JLabel("Parking Fees:");
JLabel label9 = new JLabel("Calculate");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();
JPanel panel7 = new JPanel();
JPanel panel8 = new JPanel();
JPanel panel9 = new JPanel();
panel1.add(label1);
panel2.add(label2);
panel3.add(label3);
panel4.add(label4);
panel5.add(label5);
panel6.add(label6);
panel7.add(label7);
panel8.add(label8);
panel9.setLayout(new BorderLayout());
panel1.add(dtrip);
panel2.add(air);
panel2.add(crent);
panel2.add(taxi);
panel2.add(pvec);
panel3.add(dlodge);
panel4.add(airfare);
panel5.add(tcost);
panel6.add(miles);
panel7.add(sfees);
panel8.add(pfees);
panel9.add(calcButton, BorderLayout.SOUTH);
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);
add(panel6);
add(panel7);
add(panel8);
add(panel9);
calcButton.addActionListener(new calcButtonListener());
setVisible(true);
}
private class calcButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
String input;
String input1;
String input2;
String input3;
String input4;
String input5;
String input6;
double avalue;
double mvalue;
double tvalue;
double ptax;
input = dtripTextField.getText();
input1 = dlodgeTextField.getText();
input2 = airfareTextField.getText();
input3 = tcostTextField.getText();
input4 = dlodgeTextField.getText();
input5 = dlodgeTextField.getText();
input6 = dlodgeTextField.getText();
mvalue = Double.parseDouble(input) * 37;
avalue = Double.parseDouble(input) * Double.parseDouble(input1);
tvalue = avalue + mvalue;
JOptionPane.showMessageDialog(null, " Total money spent on trip is: $" + tvalue + ".");
}
}
public static void main(String[] args)
{
TravelWorksheet travelWorksheet = new TravelWorksheet();
}
}
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
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: " + " " +
"Allowable expenses: " + " " +
" " + "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)56
{
new TravelExpenses();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.