5 Replies - 9783 Views - Last Post: 23 February 2010 - 06:40 AM Rate Topic: -----

#1 mapelai76  Icon User is offline

  • New D.I.C Head

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

calculate the parking fare

Posted 15 February 2010 - 09:05 PM

Hi Java Master there,

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
B) 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
B) 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


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]


Is This A Good Question/Topic? 0
  • +

Replies To: calculate the parking fare

#2 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

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

Re: calculate the parking fare

Posted 16 February 2010 - 11:29 PM

View Postmapelai76, on 15 February 2010 - 08:05 PM, said:

How do i create switch statement for different vehicle?

Look into enums
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Phan*


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.
Was This Post Helpful? 0

#4 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

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

Re: calculate the parking fare

Posted 19 February 2010 - 11:13 AM

View PostPhan, 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).
Was This Post Helpful? 0
  • +
  • -

#5 Guest_PUTERI*


Reputation:

Re: calculate the parking fare

Posted 23 February 2010 - 02:47 AM

View Postmapelai76, on 15 February 2010 - 08:05 PM, said:

Hi Java Master there,

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
B) 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
B) 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


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

Was This Post Helpful? 0

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • 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
}  


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1