Dan D'amico

Ranch Hand

Posts: 94

posted 3 years ago

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"

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.

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.

Dan D'amico

Ranch Hand

Posts: 94

Joanne Neal

Rancher

Posts: 3742

16

posted 3 years ago

- 1

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.

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

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.

Joanne

Dan D'amico

Ranch Hand

Posts: 94

posted 3 years ago

i liked the idea.

thanks i will try do this

update.

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.

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.

posted 3 years ago

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

- 1

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

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Dan D'amico

Ranch Hand

Posts: 94

Dan D'amico

Ranch Hand

Posts: 94

posted 3 years ago

The requirements (though not exactly clearly stated) mean 15 minutes
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

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

Joanne Neal

Rancher

Posts: 3742

16

posted 3 years ago

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.

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.

Joanne