/*
Chapter 4:
Programming Assignment 6
Programmer: Brad Shedd
Date:
January 27, 2004
Filename:
TrafficViolations.java
Purpose:
Allows the clerks in the traffic court
office to enter the actual speed limit, the speed at which the offender was
travelling, and the number of previous tickets
the offender has received.
*/
import java.text.*;
import javax.swing.*;
public
class
TrafficViolations
{
final
static
double
FINE_PER_MILE = 10.0;
final
static
double
INITIAL_COURT_COST = 53.2;
final
static
double
PRIOR_OFFENSE_CHARGE = 20.0;
final
static
int
PRIOR_OFFENSE_LIMIT = 3;
static
boolean validResponse;
static
int speedLimit, priorViolations,
actualSpeed;
static
double fine, courtCost, totalFine;
public
static
void
main(String[] args)
{
getInput();
calculate();
output();
}// end
main()
public
static
void
getInput()
{
// get the
speed limit
validResponse =
false;
while
(!validResponse)
{
validResponse =
true;
try
{
speedLimit = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter the speed limit where the offense occured:
"));
if
(speedLimit <= 0)
throw
new
NumberFormatException();
}
catch
(NumberFormatException e)
{
error();
}
}
// get the
violator's speed
validResponse =
false;
while
(!validResponse)
{
validResponse =
true;
try
{
actualSpeed = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter the offender's speed: "));
if
(actualSpeed <= 0)
throw
new
NumberFormatException();
}
catch
(NumberFormatException e)
{
error();
}
}
// get the
number of prior offenses
validResponse =
false;
while
(!validResponse)
{
validResponse =
true;
try
{
priorViolations = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter the number of prior offenses on record:
"));
if
(priorViolations < 0)
throw
new
NumberFormatException();
}
catch
(NumberFormatException e)
{
error();
}
}
}// end
getInput()
public
static
void
calculate()
{
if
(actualSpeed > speedLimit)
{
fine = (actualSpeed - speedLimit) * FINE_PER_MILE;
if
(priorViolations > PRIOR_OFFENSE_LIMIT)
priorViolations = PRIOR_OFFENSE_LIMIT;
courtCost = INITIAL_COURT_COST + (priorViolations *
PRIOR_OFFENSE_CHARGE);
totalFine = fine + courtCost;
}
}// end
calculate()
public
static
void
output()
{
DecimalFormat currency =
new
DecimalFormat("$###,###.00");
String
message =
"The offender's fine for this offense is:
" + currency.format(totalFine);
JOptionPane.showMessageDialog(null,
message,
"Traffic Violations", JOptionPane.PLAIN_MESSAGE);
}// end
output()
public
static
void
error()
{
JOptionPane.showMessageDialog(null,
"Please enter a valid number!",
"Invalid Input", JOptionPane.ERROR_MESSAGE);
validResponse =
false;
}// end
error()
}// end TrafficViolations class