import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.JComboBox;
public class gascalculatorApplet extends Applet implements ItemListener
{
int auto, miles;
double fuelcost, Cost;
Color darkGreen = new Color(39, 77, 6);
String beginning[] = {"Minneapolis", "Duluth", "Sioux Falls", "International Falls", "Fargo", "Chicago"};
String ending[] = {"Phoenix", "Daytona", "Dallas", "Las Vegas", "Darlington", "Talladega"};
String fuelprices[] = {"2.79", "2.89", "3.19"};
String vehicletypes[] = {"Compact", "Midsize", "Luxury", "Suv"};
Label promptLabel = new Label("Enter the amount of miles you plan to travel or select the city you are leaving from:");
TextField milesField = new TextField(25);
Label autoLabel = new Label("Select the vehicle type you will be using:");
CheckboxGroup codeGroup = new CheckboxGroup();
Checkbox compactBox = new Checkbox("Compact Vehicle",false,codeGroup);
Checkbox midsizeBox = new Checkbox("Mid-size Vehicle",false,codeGroup);
Checkbox luxuryBox = new Checkbox("Luxury Vehicle",false,codeGroup);
Checkbox suvBox = new Checkbox("SUV", false,codeGroup);
Checkbox hiddenBox = new Checkbox("",true,codeGroup);
Label outputLabel = new Label("Input the amount you are paying for fuel.");
public void init()
{
setBackground(darkGreen);
setForeground(Color.white);
add(promptLabel);
add(milesField);
milesField.requestFocus();
milesField.setForeground(Color.black);
add(autoLabel);
add(compactBox);
compactBox.addItemListener(this);
add(midsizeBox);
midsizeBox.addItemListener(this);
add(luxuryBox);
luxuryBox.addItemListener(this);
add(suvBox);
suvBox.addItemListener(this);
add(outputLabel);
}
public void itemStateChanged(ItemEvent choice)
{
try
{
miles = getMiles();
auto = getVehicle();
fuelcost = getFuelcost(miles,auto);
output (fuelcost, miles);
}
catch(NumberFormatException e)
{
outputLabel.setText("Enter the amount of miles you plan to drive.");
hiddenBox.setState(true);
milesField.setText("");
milesField.requestFocus();
}
}
public int getMiles()
{
miles = Integer.parseInt(milesField.getText());
if (miles <=0) throw new NumberFormatException();
return miles;
}
public int getVehicle()
{
int auto = 0;
if (compactBox.getState()) auto = 1;
else
if (midsizeBox.getState()) auto = 2;
else
if(luxuryBox.getState()) auto = 3;
else
if(suvBox.getState()) auto = 4;
return auto;
}
public static double getFuelcost(int miles, int auto)
{
double fuelcost = 0.0;
switch(auto)
{
case 1:
fuelcost = (2.49) * (miles/15) + (miles/3000) * (15);
break;
case 2:
fuelcost = (2.69) * (miles/15) + (miles/3000) * (15);
break;
case 3:
fuelcost = (2.99) * (miles/15) + (miles/3000) * (15);
break;
case 4:
fuelcost = (3.19) * (miles/15) + (miles/3000) * (15);
break;
}
return fuelcost;
}
public void output(double fuelcost, int miles)
{
DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
outputLabel.setText("Your cost to travel is" + twoDigits.format(fuelcost));
}
}