/*
*
*
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.text.*;
import java.util.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
public class TripCalcUpdated extends JFrame implements PropertyChangeListener
{
//Declare output stream
DataOutputStream output;
String[] startStrings = { "Detroit Lakes", "Minneapolis", "Duluth", "Bemidji", "Rochester", "StCloud", "Daytona", "Darlington", "Charlotte", "Talladega", "Richmond", "Bristol" };
double distance, fuelprice, gallons, totalCost, index;
int i, j, k, l, m, n, o, p;
//construct components
JLabel typePrompt = new JLabel("Vehicle Type");
JComboBox typeCombo = new javax.swing.JComboBox();
JTextPane typePane = new JTextPane();
JLabel fuelPrompt = new JLabel("Fuel Type");
JComboBox fuelCombo = new javax.swing.JComboBox();
JTextPane fuelPane = new JTextPane();
JLabel startPrompt = new JLabel("Starting Location");
JComboBox startCombo = new javax.swing.JComboBox();
JTextPane startPane = new JTextPane();
JLabel endPrompt = new JLabel("Ending Location");
JComboBox endCombo = new javax.swing.JComboBox();
JTextPane endPane = new JTextPane();
JFormattedTextField distance;
JFormattedTextField fuelprice;
JFormattedTextField mpg;
JFormattedTextField totalCost;
NumberFormat distanceFormat;
NumberFormat fuelpriceFormat;
NumberFormat mpgFormat;
NumberFormat totalCostFormat;
// Panel Creation
JPanel firstRow = new JPanel();
JPanel secondRow = new JPanel();
// Panel for Fields and Buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
// Construct Labels and TextBoxes
JLabel Miles = new JLabel("Miles planning to travel" );
distance = new JFormattedTextField(distanceFormat);
distance.setValue(new Double(distance));
distance.setColumns(5);
distance.addPropertyChangeListener("value", this);
JLabel Fuelcost = new JLabel("What is the price per gallon?" );
fuelprice = new JFormattedTextField(fuelpriceFormat);
fuelprice.setValue(new Double (fuelprice));
fuelprice.setColumns(5);
fuelprice.addPropertyChangeListener("value", this);
JLabel FuelAmount = new JLabel("What is your vehicle's MPG?" );
mpg = new JFormattedTextField(mpgFormat);
mpg.setValue(new Double(mpg));
mpg.setColumns(5);
mpg.addPropertyChangeListener("value", this);
JLabel Total = new JLabel("Your total cost for the trip will be " );
totalCost = new JFormattedTextField(totalCostFormat);
totalCost.setValue(new Double(totalCost));
JButton submitJButton = new JButton("Submit");
JButton clearJButton = new JButton("Clear Fields");
//main method executes at run time
public static void main(String args[])
{
TripCalc f = new TripCalc();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(f.createContentPane());
f.setTitle("Trip Calculator");
f.setSize(800,300);
f.setVisible(true);
}
//create the content pane
public Container createContentPane()
{
//populate the JCombobox
typeCombo.addItem("Compact");
typeCombo.addItem("Mid-Size");
typeCombo.addItem("Luxury");
typeCombo.addItem("SUV");
typeCombo.addActionListener(this);
fuelCombo.addItem("Leaded");
fuelCombo.addItem("Unleaded");
fuelCombo.addItem("Super Unleaded");
fuelCombo.addItem("Diesel");
fuelCombo.addActionListener(this);
startCombo.addItem("Minneapolis");
startCombo.addItem("Duluth");
startCombo.addItem("Bemidji");
startCombo.addItem("Rochester");
startCombo.addItem("Detroit Lakes");
startCombo.addItem("St.Cloud");
startCombo.addActionListener(this);
endCombo.addItem("Daytona");
endCombo.addItem("Darlington");
endCombo.addItem("Las Vegas");
endCombo.addItem("Talladega");
endCombo.addItem("Richmond");
endCombo.addItem("Bristol");
endCombo.addActionListener(this);
//construct and populate the north panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(typePrompt);
northPanel.add(typeCombo);
northPanel.add(fuelPrompt);
northPanel.add(fuelCombo);
northPanel.add(startPrompt);
northPanel.add(startCombo);
northPanel.add(endPrompt);
northPanel.add(endCombo);
JPanel southPanel = new JPanel();
southPanel.setLayout(new FlowLayout());
southPanel.add(submitJButton);
southPanel.add(clearJButton);
clearJButton.addActionListener(this);
submitJButton.addActionListener(this);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new FlowLayout());
centerPanel.add(Miles);
centerPanel.add(distance);
centerPanel.add(Fuelcost);
centerPanel.add(fuelprice);
centerPanel.add(FuelAmount);
centerPanel.add(gallons);
centerPanel.add(Total);
//createContainer and set attributes
Container f = getContentPane();
setLayout(new BorderLayout(40,40));
fieldPanel.setLayout(new GridLayout(1,1));
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
add(northPanel,BorderLayout.NORTH);
add(southPanel,BorderLayout.SOUTH);
add(centerPanel,BorderLayout.CENTER);
setLocation(200,200);
return f;
}
// event to process user clicks
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
Object target = e.getSource();
if(target == submitJButton)
{
fuelprice = ((Number)fuelprice.getValue()).doubleValue();
mpg = ((Number)gallons.getValue()).doubleValue();
distance = ((Number)distance.getValue()).doubleValue();
index = (distance/mpg)* fuelprice;
totalCost.setText(index);
}
else if(target == clearJButton)
{
distance.setText("");
gallons.setText("");
fuelprice.setText("");
}
}
}