• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with a parking fees code  RSS feed

 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 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"
i must use only one class and only the main method
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 entrance hour 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.




 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would convert the inputted hours to minutes and add that to the inputted minutes then do all the calculations and comparisons using those.
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i see.
what about the output.
its works only if the difference is more than 2 hours and less then 3.
for example. if user entered 13 00 and 15 30. its will work.
but if he entered 13 00 and 16 05
it will not show an input.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your smallest charging time is 15 minutes, so why don't you work with 15 minute periods instead of hours and minutes.
Convert your hours and minutes to minutes as Carey suggested and then work out how many 15 minute periods there are in that number of minutes (use the % operator to do this).
You can then say
if there are four or less 15 minute periods parking is free
else if there are eight or less 15 minute periods parking is $10
else subtract 8 from the number of periods, multiply the result by 3, add 10 (for the first two hours) and you have your answer.

You might need to tinker a bit when the number of minutes isn't an exact multiple of 15, but this is just a case of adding an extra three dollars at the end.
That should certainly reduce the number of if statements you have.
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i liked the idea.
thanks i will try do this

update.

Joanne Neal wrote:Your smallest charging time is 15 minutes, so why don't you work with 15 minute periods instead of hours and minutes.
Convert your hours and minutes to minutes as Carey suggested and then work out how many 15 minute periods there are in that number of minutes (use the % operator to do this).
You can then say
if there are four or less 15 minute periods parking is free
else if there are eight or less 15 minute periods parking is $10
else subtract 8 from the number of periods, multiply the result by 3, add 10 (for the first two hours) and you have your answer.

You might need to tinker a bit when the number of minutes isn't an exact multiple of 15, but this is just a case of adding an extra three dollars at the end.
That should certainly reduce the number of if statements you have.


i subtracted the entering minutes from the exit minutes and divide by 15 . thats give me thw periods.
its works until the number is dont divide by 15.
if i enter 13 00 and exit at 18 20
it should be charge of 52.
because the totall didnt divide by 15 it gives me wrong number of 50.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dan dam wrote:i liked the idea.
thanks i will try do this

update.

Joanne Neal wrote:Your smallest charging time is 15 minutes, so why don't you work with 15 minute periods instead of hours and minutes.
Convert your hours and minutes to minutes as Carey suggested and then work out how many 15 minute periods there are in that number of minutes (use the % operator to do this).
You can then say
if there are four or less 15 minute periods parking is free
else if there are eight or less 15 minute periods parking is $10
else subtract 8 from the number of periods, multiply the result by 3, add 10 (for the first two hours) and you have your answer.

You might need to tinker a bit when the number of minutes isn't an exact multiple of 15, but this is just a case of adding an extra three dollars at the end.
That should certainly reduce the number of if statements you have.


i subtracted the entering minutes from the exit minutes and divide by 15 . thats give me thw periods.
its works until the number is dont divide by 15.
if i enter 13 00 and exit at 18 20
it should be charge of 52.
because the totall didnt divide by 15 it gives me wrong number of 50.


= ((minutes + 14) / 15 - 8) * 3 + 10
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Post your current source code.
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:P.S. Post your current source code.

im currently at work. so when i finish i wiil post the code
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:[

= ((minutes + 14) / 15 - 8) * 3 + 10


yes. thats work for all the inputs. why did the " + 14 " fixed it ?
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Dam wrote:
Carey Brown wrote:[

= ((minutes + 14) / 15 - 8) * 3 + 10


yes. thats work for all the inputs. why did the " + 14 " fixed it ?


The requirements (though not exactly clearly stated) mean 15 minutes or fraction thereof. Obviously 30/15 is 2 and 45/15 is 3 but we also want 44/15 to be 3, hence the +14 (almost, but not quite 15).
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Dam wrote:yes. thats work for all the inputs.

Does it ? What about periods of less than an hour ?
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
formula was specifically to handle this part of the requirement:
from the 3 hour for every 15 minutes an additional fee of 3 dollars.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:formula was specifically to handle this part of the requirement:
from the 3 hour for every 15 minutes an additional fee of 3 dollars.

Okay. I assumed that as you were calculating the number of 15 minutes periods here, that you were trying to handle all the cases in one line, but I can see now that you could handle the other cases without knowing the number of periods.
I'd been thinking about it in a slightly different way that would handle the doesn't divide by 15 case in a possibly slightly more obvious way, but both approaches are reasonable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!