Please help me to check my answer. How do i create switch statement for different vehicle?
Question :
Write a structured Java program program to calculate the parking fare for
customers who park their cars in a parking lot when the following information is
given:
a) A character showing the type of vehicle: C for car, B for bus, T for truck
c) An integer between 0 and 60 showing the minute the vehicle entered the lot
d) An integer between 0 and 24 showing the hour the vehicle left the lot
e) An integer between 0 and 60 showing the minute the vehicle left the lot
This is a public lot. To encourage people to park for a short period of time, the
management uses two different rates for each type of vehicle, as shown in the
following table:
Vehicle First Rate Second Rate
CAR RM 0.00/hour first 3 hour RM 1.50/hour after 3 hour
TRUCK RM 1.00/hour first 2 hour RM 2.30/hour after 2 hour
BUS RM 2.00/hour for first hour RM 3.70/hour after first hour
The input data consist of a character and a set of four integers representing the
type of vehicle and the entering and leaving hours and minutes. But these
pieces of data must be input into the computer in a user-friendly way. In other
words, the computer must prompt the user to enter each piece of data as show
below. (Note: Red colour indicates typical data)
Type of vehicle? C
Hour vehicle entered lot (0-24)? 14
Minute vehicle entered lot (0-60)? 23
Hour vehicle left lot (0-24)? 18
Minute vehicle left lot (0-60)? 8
The output format is shown below:
This program must first calculate the actual time spent in the parking lot for
each vehicle. You may use the following algorithm:
a) Compare the minute portion of the leaving and the entering time. If the first
one is smaller than the second,
- Add 60 to the minute portion of the leaving time
- Subtract 1 from the hour portion of the leaving time
c) Subtract the minute portions
d) Since there are no fractional hour charges, the program must also round the
parking time up to the next hour before calculating the charge. The program
should use switch statement to distinguish between the different types of
vehicles
A well-structured program design is required. Run your program six times with
the following data:
Test Type Hour In Minute In Hour Out Minute Out
1 C 12 40 14 22
2 B 8 20 8 40
3 T 2 0 3 59
4 C 12 40 16 22
5 B 8 20 14 20
6 T 2 0 12 0
Note: You CANNNOT use GUI/Applet and/or object-oriented approach in solving
this question.
PARKING LOT CHARGE
Type of vehicle: Car or Bus or Truck
TIME-IN XX : XX
TIME-OUT XX : XX
-------
PARKING TIME XX : XX
ROUNDED TOTAL XX
-------
TOTAL CHARGE RM XX.XX
Answer :
My Code
import java.io.*;
class menuA
{
public static void main(String[] args) throws IOException
{
char menu;
int hourin, minutin, hourout, minutout;
hourin = 0;
minutin = 0;
hourout = 0;
minutout = 0;
Pembaca read = new Pembaca();
System.out.println("Please don't use capital letter");
System.out.println("Type of Vehicle?");
menu = read.bacaChar();
System.out.println("Hour vehicle entered lot (0-23)? ");
hourin = read.bacaInt();
if ( (hourin < 0) && (hourin > 23) ) // you have missed the outer most encompassing brackets here !
{
System.out.println("Please input value from 0 until 23 only");
}
else
{
System.out.println("Minute vehicle entered lot (0-59)?");
minutin = read.bacaInt();
}
if ( ( minutin < 0) && ( minutin > 59) )
{
System.out.println("Please input value from 0 until 59 only");
}
else
{
System.out.println("Hour vehicle left lot (0-23)? ");
hourout = read.bacaInt();
}
if ( ( hourout < 0) && (hourout > 23) )
{
System.out.println("Please input value from 0 until 23 only");
}
else if (hourout < hourin)
{
System.out.println("please input bigger value then hour vehicle enter" );
}
System.out.println("Minute vehicle left lot (0-60)?");
minutout = read.bacaInt();
if ( (minutout < 0) && ( minutout > 60 ) )
{
System.out.println("Please input value from 0 until 59 only");
}
else
{
int timeh = hourin - hourout;
int timem = minutin - minutout;
System.out.println("PARKING LOT CHARGE");
System.out.println("");
System.out.println("Type of vehicle: Car or Bus or Truck"+ menu) ;
System.out.println("TIME-IN "+hourin+":" + minutin);
System.out.println("TIME-OUT "+hourout+":"+ minutout);
System.out.println(" ----------------------------------");
System.out.println("PARKING TIME " + timeh + ":" + timem);
System.out.println("ROUNDED TOTAL : XX");
System.out.println(" ----------------------------------");
System.out.println("TOTAL CHARGE RM XX.XX");
}
} //main
} //class
This post has been edited by JackOfAllTrades: 16 February 2010 - 06:54 PM
Reason for edit:: Added code tags. Please [code]...put your code in here...[/code]

New Topic/Question
Reply




MultiQuote





|