/*
Program:
4.6
Programmer:
Brad Shedd
Date:
May 22, 2006
File:
Traffic.java
Purpose:
Calculates traffic ticket and court
cost.
*/
import java.io.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public
class
Traffic
{
public
static
void
main(String[]arg)
{
//declare
variables
int
limit, speed, tickets;
double
charge, cost;
//method
calls
displayWelcome();
speed =
getSpeed();
limit =
getLimit();
tickets =
getTickets();
charge =
calccharge(speed, limit);
cost =
calccost(tickets);
displaytot(charge, cost);
finish();
}
// end main
public
static
void
displayWelcome()
{
JOptionPane.showMessageDialog(null,
"Welcome to the Ticket Program","Welcome",JOptionPane.PLAIN_MESSAGE);
}
public
static
int
getSpeed()
{
// declare
method variables
int
speed = 0;
boolean
done =
false;
// loop
while not done
while
(!done)
{
String strSpeed = JOptionPane.showInputDialog(null,"Enter
the violater's speed,\n or click Cancel to exit:");
if
(strSpeed ==
null) finish();
try
{
speed = Integer.parseInt(strSpeed);
if
(speed <= 0)
throw
new
NumberFormatException();
else
done =
true;
}
catch(NumberFormatException
e)
{
JOptionPane.showMessageDialog(null,"Your
entry was not in the proper format.","Error",JOptionPane.INFORMATION_MESSAGE);
}
}
return
speed;
}
// end
getSpeed
public
static
int
getLimit()
{
// declare
method variables
int
limit = 0;
boolean
done =
false;
// loop
while not done
while
(!done)
{
String strlimit = JOptionPane.showInputDialog(null,"Enter
the speed limit, \nor click Cancel to exit:");
if
(strlimit ==
null) finish();
try
{
limit = Integer.parseInt(strlimit);
if
(limit <= 0)
throw
new
NumberFormatException();
else
done =
true;
}
catch(NumberFormatException
e)
{
JOptionPane.showMessageDialog(null,"Your
entry was not in the proper format.","Error",JOptionPane.INFORMATION_MESSAGE);
}
}
return
limit;
}
// end
getLimit
public
static
int
getTickets()
{
// declare
method variables
int
tickets = 0;
boolean
done =
false;
// loop
while not done
while
(!done)
{
String strtickets = JOptionPane.showInputDialog(null,"Enter
the number of previous \ntickets, or click Cancel to exit:");
if
(strtickets ==
null)
finish();
try
{
tickets = Integer.parseInt(strtickets);
if
(tickets < 0)
throw
new
NumberFormatException();
else
done =
true;
}
catch(NumberFormatException
e)
{
JOptionPane.showMessageDialog(null,"Your
entry was not in the proper format.","Error",JOptionPane.INFORMATION_MESSAGE);
}
}
return
tickets;
}
// end
getTickets
public
static
double
calccharge(int speed,int
limit)
{
double
charge;
charge =
(speed - limit) * 10;
return
charge;
}
// end
calccharge
public
static
double calccost(int
tickets)
{
double
cost;
double
base = 53.20;
double
cap = 93.20;
tickets+=1;
switch(tickets)
{
case
1:
cost = base;
break;
case
2:
cost = base + 20;
break;
case
3:
cost = base + 40;
break;
default
:
cost = cap;
break;
}
return
cost;
}
// end
calccost
public
static
void
displaytot(double charge,double
cost)
{
double
total;
total =
charge + cost;
DecimalFormat
twoDigits =
new
DecimalFormat("$###0.00");
JOptionPane.showMessageDialog(null,
"Ticket Charge = "+
twoDigits.format (charge) +
"\nCourt Cost = "+
twoDigits.format (cost) +
"\n Total = "+
twoDigits.format (total),"Tickets",JOptionPane.PLAIN_MESSAGE);
}
// end
displaytot
public
static
void
finish()
{
System.exit(0);
}
}
// end traffic