# calculate the parking fare

Page 1 of 1

## 5 Replies - 18010 Views - Last Post: 23 February 2010 - 06:40 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=156265&amp;s=f7a8a870aaf3d6080e155c14474678bd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mapelai76

Reputation: 0
• Posts: 2
• Joined: 25-January 10

# calculate the parking fare

Posted 15 February 2010 - 09:05 PM

Hi Java Master there,

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
An integer between 0 and 24 showing the hour the vehicle entered the lot
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
Subtract the hour portions
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

My Code

```import java.io.*;

{

public static void main(String[] args) throws IOException
{

int hourin, minutin, hourout, minutout;
hourin   = 0;
minutin  = 0;
hourout  = 0;
minutout = 0;

System.out.println("Type of Vehicle?");

System.out.println("Hour vehicle entered lot (0-23)? ");

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)?");
}

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)? ");
}

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)?");

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

Is This A Good Question/Topic? 0

## Replies To: calculate the parking fare

### #2 xor-logic

• HAL9000 was an Apple product

Reputation: 128
• Posts: 764
• Joined: 04-February 10

## Re: calculate the parking fare

Posted 16 February 2010 - 11:29 PM

mapelai76, on 15 February 2010 - 08:05 PM, said:

How do i create switch statement for different vehicle?

Look into enums

Reputation:

## Re: calculate the parking fare

Posted 19 February 2010 - 09:12 AM

I would like to have same question but in C language coding,
did you have any idea for this in C language coding?

Thanks.

### #4 xor-logic

• HAL9000 was an Apple product

Reputation: 128
• Posts: 764
• Joined: 04-February 10

## Re: calculate the parking fare

Posted 19 February 2010 - 11:13 AM

Phan, on 19 February 2010 - 08:12 AM, said:

I would like to have same question but in C language coding,
did you have any idea for this in C language coding?

Thanks.

That is a question that would be better asked in the C forum (this is the Java forum).

Reputation:

## Re: calculate the parking fare

Posted 23 February 2010 - 02:47 AM

mapelai76, on 15 February 2010 - 08:05 PM, said:

Hi Java Master there,

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
An integer between 0 and 24 showing the hour the vehicle entered the lot
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
Subtract the hour portions
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

My Code

```import java.io.*;

{

public static void main(String[] args) throws IOException
{

int hourin, minutin, hourout, minutout;
hourin   = 0;
minutin  = 0;
hourout  = 0;
minutout = 0;

System.out.println("Type of Vehicle?");

System.out.println("Hour vehicle entered lot (0-23)? ");

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)?");
}

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)? ");
}

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)?");

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
```

### #6 japanir

• jaVanir

Reputation: 1014
• Posts: 3,025
• Joined: 20-August 09

## Re: calculate the parking fare

Posted 23 February 2010 - 06:40 AM

you could use enums, as xor-logic said, but you can also use simple chars.
get an input from the use for vehicle type, and use a switch statement:
```char vehicleType;
//get input for vehicleType
switch(vehicleType){
case 'C':
case 'c':
//code for car
break;
case 'B':
case 'b':
//code for bus
break;
case 'T':
case 't':
//code for truck
break;
default: //print error messege
}

```