Must be written in java FX! write a java FX GUI program that makes a mileage cal
ID: 3821116 • Letter: M
Question
Must be written in java FX!
write a java FX GUI program that makes a mileage calculator for a trip. The program should calculate overall mileage as well as individua fuel ratings along each trip line ( for 5 legs).
Empty trip legs should not be included in calculations, should be computed when a Button is pressed
Must have a UI Controller ( CheckBox or RadioButoon with a ToggleGroup) to switch between MPG and liters per 100 kilometers. This UI controller should convert all current numbers in the GUI.
MUST:
Accept User enter miles and duel usage for individual legs of a trip.
Calculate both that leg's mileage, and the overall mileage for the entire trip.
Blank entires should not be in calculations
Assume a max of 5 legs per trip
Have a Button to start calculations
UI Controller must auto convert any current numbers in GUI
Thank you!
Explanation / Answer
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: " + " " +
"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)
{
new TravelExpenses();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.