# Thread: help with program that calculates parking fee

1. Member
Join Date
Oct 2014
Posts
9
Rep Power
0

## help with program that calculates parking fee

i succeed but with limit. the code is below.

I cant use advanced material (really just the basics even without loops)
if statements are allowed, and i must use Final veriables for key value.
i must use one class and one method (the main )
I need to prompt the user for a entrance time and exit time
it must be with a space , like 18 00 and not 18:00
also, if the entrance time is greater or equal to 18 00 the charge is always 20 dollars.
if the user enters wrong data or out of range data an output of "ERROR WRONG DATA"
and then the programs terminates.
same for wrong time . like if the enter hour is bigger than the exit hour
an output of "ERROR entrance time must preccede exit"
for the first hour the output is "no charge",
for the second hour the charge is 10 dollars.
from the 3 hour for every 15 minutes an additional fee of 3 dollars.
for example if the user entered 10 00 and 13 20 , it should be 52 dollars
** it works for me but if the difference between the entered and exit hours is more then 3 it doesnt work.
i could've added another if statements for the bigger hours but it will be very long.

i managed everything. except the last part.
that calculates adiitional 3 dollar for each 15 minutes. what is did is.
converted everything to minutes.
subtract enter time from exit time.
and devide by 15.
thats give me the number of 15 minute periods.
if it last or equal to 4. no charge
else last or equal to 8 . 10 dollars
else...... here is the hard part.
if the totall minutes wont divide even by 15 it wont work.

2. ## Re: help with program that calculates parking fee

3. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
9

## Re: help with program that calculates parking fee

Hello d2idan, welcome.

For completeness, here is the code posted on the other site.

Java Code:
```import java.util.Scanner;

public class Parking
{
public static void main(String[] args)
{
final int MAX_HOUR = 24;   //no more than 24 hours to enter
final int MAX_MIN = 59;   //no more than 59 minutes to enter
final int HOUR_MIN = 01;  //no less then 1 hour to enter
final int QUARTER = 15;  //4 quarters of a clock
final int HALF = 30;
final int QUARTER_THREE = 45;
final int QUARTER_FOUR = 59;
final int SIX_PM = 18;      //for the 20 NIS charge start from 18 00
final int SIX_CHARGE = 20;
final int QUARTER_CHARGE = 3;   //charge for each additional 15 minutes
final int FIRST_CHARGE = 10;    //charge for the second hour
int hourEnter, hourExit, minEnter, minExit; //user input veriables
int amount = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter entrance time: ");
hourEnter = input.nextInt();
minEnter = input.nextInt();
System.out.print("Enter exit time: ");
hourExit = input.nextInt();
minExit = input.nextInt();
if((hourEnter > MAX_HOUR||hourExit > MAX_HOUR)||(minEnter > MAX_MIN||minExit > MAX_MIN)
||(hourEnter< HOUR_MIN ||hourExit < HOUR_MIN)) {
System.out.println("Error! Illegal data.");
}else if(hourEnter>hourExit){
System.out.println("Error! Entrance time must preccede exit time.");
}else if(hourEnter >= SIX_PM){
System.out.println("Totall charge is " + SIX_CHARGE + " NIS.");
}else if(hourExit-hourEnter == 1 && minExit < minEnter){
System.out.println("No charge.");
}else if(hourExit == hourEnter && minExit<=MAX_MIN){
System.out.println("No charge.");
}else if(hourExit-hourEnter==1 && minExit >= minEnter){
System.out.println("Charge is " + FIRST_CHARGE + " NIS.");
}else if(hourExit-hourEnter == 2 && minExit < minEnter){
System.out.println("Charge is " + FIRST_CHARGE + " NIS.");
}else if((hourExit-hourEnter >= 2) && (minExit-minEnter >=0 && minExit-minEnter<=QUARTER)){
System.out.println("Charge is " + (FIRST_CHARGE + QUARTER_CHARGE) +" NIS.");
}else if((hourExit-hourEnter >= 2) && (minExit-minEnter >QUARTER && minExit-minEnter<=HALF)){
System.out.println("Charge is " + (FIRST_CHARGE + (QUARTER_CHARGE * 2)) +" NIS.");
}else if((hourExit-hourEnter >= 2) && (minExit-minEnter > HALF && minExit-minEnter<= QUARTER_THREE)){
System.out.println("Charge is " + (FIRST_CHARGE + (QUARTER_CHARGE * 3)) +" NIS.");
}else if((hourExit-hourEnter >= 2) && (minExit-minEnter > QUARTER_THREE && minExit-minEnter<= QUARTER_FOUR)){
System.out.println("Charge is " + (FIRST_CHARGE + (QUARTER_CHARGE * 4)) +" NIS.");
}

}

}```
I started to read the code but my head started to hurt. There are only so many times I could read the line
Java Code:
`else if((hourExit-hourEnter >= 2)`
Seriously, if the continuous check for this condition is necessary then its much simpler to nest the if statements. On the other hand if all other possible conditions have already been checked (i.e. Difference is equal to or less than 2) then this check become mute and an unnecessary use of processing time.

Splitting up the quarters is also completely unnecessary. This is something which could be quickly calculated by using the modus of the minutes.

Not to disrespect your attempt at coding (We all started somewhere), but I would recommend going back to the drawing board on this. You will appreciate the chance to do this again and hopefully gain an understanding of how to generalize the problem in such a way you do not micromanage every conceivable condition.

We would be happy to provide pointers for any further attempt.

Regards.
Last edited by Ronin; 10-23-2014 at 05:49 PM. Reason: Typo

4. Member
Join Date
Oct 2014
Posts
9
Rep Power
0

## Re: help with program that calculates parking fee

Originally Posted by Ronin
Hello d2idan, welcome.

For completeness, here is the code posted on the other site.

Java Code:
```import java.util.Scanner;

public class Parking
{
public static void main(String[] args)
{
final int MAX_HOUR = 24;   //no more than 24 hours to enter
final int MAX_MIN = 59;   //no more than 59 minutes to enter
final int HOUR_MIN = 01;  //no less then 1 hour to enter
final int QUARTER = 15;  //4 quarters of a clock
final int HALF = 30;
final int QUARTER_THREE = 45;
final int QUARTER_FOUR = 59;
final int SIX_PM = 18;      //for the 20 NIS charge start from 18 00
final int SIX_CHARGE = 20;
final int QUARTER_CHARGE = 3;   //charge for each additional 15 minutes
final int FIRST_CHARGE = 10;    //charge for the second hour
int hourEnter, hourExit, minEnter, minExit; //user input veriables
int amount = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter entrance time: ");
hourEnter = input.nextInt();
minEnter = input.nextInt();
System.out.print("Enter exit time: ");
hourExit = input.nextInt();
minExit = input.nextInt();
if((hourEnter > MAX_HOUR||hourExit > MAX_HOUR)||(minEnter > MAX_MIN||minExit > MAX_MIN)
||(hourEnter< HOUR_MIN ||hourExit < HOUR_MIN)) {
System.out.println("Error! Illegal data.");
}else if(hourEnter>hourExit){
System.out.println("Error! Entrance time must preccede exit time.");
}else if(hourEnter >= SIX_PM){
System.out.println("Totall charge is " + SIX_CHARGE + " NIS.");
}else if(hourExit-hourEnter == 1 && minExit < minEnter){
System.out.println("No charge.");
}else if(hourExit == hourEnter && minExit<=MAX_MIN){
System.out.println("No charge.");
}else if(hourExit-hourEnter==1 && minExit >= minEnter){
System.out.println("Charge is " + FIRST_CHARGE + " NIS.");
}else if(hourExit-hourEnter == 2 && minExit < minEnter){
System.out.println("Charge is " + FIRST_CHARGE + " NIS.");
}else if((hourExit-hourEnter >= 2) && (minExit-minEnter >=0 && minExit-minEnter<=QUARTER)){
System.out.println("Charge is " + (FIRST_CHARGE + QUARTER_CHARGE) +" NIS.");
}else if((hourExit-hourEnter >= 2) && (minExit-minEnter >QUARTER && minExit-minEnter<=HALF)){
System.out.println("Charge is " + (FIRST_CHARGE + (QUARTER_CHARGE * 2)) +" NIS.");
}else if((hourExit-hourEnter >= 2) && (minExit-minEnter > HALF && minExit-minEnter<= QUARTER_THREE)){
System.out.println("Charge is " + (FIRST_CHARGE + (QUARTER_CHARGE * 3)) +" NIS.");
}else if((hourExit-hourEnter >= 2) && (minExit-minEnter > QUARTER_THREE && minExit-minEnter<= QUARTER_FOUR)){
System.out.println("Charge is " + (FIRST_CHARGE + (QUARTER_CHARGE * 4)) +" NIS.");
}

}

}```
I started to read the code but my head started to hurt. There are only so many times I could read the line
Java Code:
`else if((hourExit-hourEnter >= 2)`
Seriously, if the continuous check for this condition is necessary then its much simpler to next the if statements. On the other hand if all other possible conditions have already been checked (i.e. Difference is equal to or less than 2) then this check become mute and an unnecessary use of processing time.

Splitting up the quarters is also completely unnecessary. This is something which could be quickly calculated by using the modus of the minutes.

Not to disrespect your attempt at coding (We all started somewhere), but I would recommend going back to the drawing board on this. You will appreciate the chance to do this again and hopefully gain an understanding of how to generalize the problem in such a way you do not micromanage every conceivable condition.

We would be happy to provide pointers for any further attempt.

Regards.
yep. youre right.
ok.
thanks

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•