Monday, February 20, 2006

Simple Program in Java

/*This program will calculate the amount charged to a customer at a parking
lot based on vehicle type and length of stay*/

package homework3;

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.io.*;

public class Parking_Lot {
public static void main(String[] args) {
Parking_Lot parking_lot = new Parking_Lot();

char vehicle, C, B, T;//initializes the vehicle types to char
int hourenter = 0, minuteenter = 0, hourleft = 0, minuteleft = 0, totalminutes=0,
totalhours=0, hours=0, minutes=0;//initializes as int
float price=0;//initializes price as a float
double charge=0;//initializes charge as double

String inputString;
inputString = JOptionPane.showInputDialog(null, "Type of Vehicle (C=Car, T=Truck, B=Bus):");
vehicle = inputString.charAt(0);//inputted number assigned to vehicle

inputString = JOptionPane.showInputDialog(null, "Hour vehicle entered lot (0-24):");
hourenter = Integer.parseInt(inputString);//inputted number assigned to hourenter

inputString = JOptionPane.showInputDialog(null, "Minute vehicle entered lot:");
minuteenter = Integer.parseInt(inputString);//inputted number assigned to minuteenter

inputString = JOptionPane.showInputDialog(null, "Hour vehicle left lot (0-24):");
hourleft = Integer.parseInt(inputString);//inputted number assigned to hourleft

inputString = JOptionPane.showInputDialog(null, "Minute vehicle left lot:");
minuteleft = Integer.parseInt(inputString);//inputted number assigned to minuteleft

if(minuteleft//loop makes sure total time is calculated correctly
{
minutes = minuteleft + 60;
hours = hourleft - 1;
}
totalminutes=minuteleft-minuteenter;//calculates the total minutes
totalhours=hourleft-hourenter;//calculates the total hours
if(totalminutes > 0) totalhours++;//rounds up to next hour due to no fractional charges

switch (vehicle)//statement sorts vehicle by letter and calculates charge based on vehicle type
{
case 'C':
{
if(totalhours<=3)//customer not charged if stay is <=3 hours
charge=0;
else
charge=1.50*(totalhours-3);//customer still not charged for first 3 hours,
//but is charged a higher rate for subsequent hours
}
break;
case 'T':
{
if(totalhours<=2)//customer charged special rate if stay is <=2 hours
charge=1.00*totalhours;
else
charge=(1.00*2)+2.30*(totalhours-2);//customer charged special rate for
//first 2 hours, but charged higher rate

//for subsequent hours
}
break;
case 'B':
{
if(totalhours==1)//customer charged special rate if stay is ==1 hour
charge=2.00;
else
charge=2.00+3.70*(totalhours-1);//customer charged special rate for first hour,

//but charged higher rate for subsequent hours
}
break;

default:
JOptionPane.showMessageDialog(null, "Invalid type! Program will terminate.", "Flagrant Error!",
OptionPane.ERROR_MESSAGE);
//error message checks for invalid vehicle types
}
JOptionPane.showMessageDialog(null, "Type of Vehicle: "+vehicle+"\n TIME-IN: "
+hourenter+":"+minuteenter+"\n TIME-OUT: "+hourleft+":"+minuteleft+
"\n Parking Time: "+totalhours+":00\n Total Charge: "+charge+
"","Parking Lot Charges ", JOptionPane.INFORMATION_MESSAGE);
/*above creates window that outputs the vehicle type, hour entered, minute entered,

hour left, minute left, total parking time, and the charge*/
}
}

1 Comments:

Anonymous Anonymous said...

As they say, "It's all Greek to me."

February 23, 2006 3:31 PM  

Post a Comment

<< Home