Need help writing a universal equation involving time and stepping rates.

robbiemcleod56

New member
Joined
Aug 7, 2018
Messages
14
I need help writing an equation, it's just too complicated for me to trust my end work.

I am writing a program to keep track of when people check in, and how much to charge them for their time stayed. The formula has to calculate how much they need to pay based on the following rules:

A deposit of $8 is taken in advance to cover up to 4 hours during the day between 7AM and 6PM.
1 hour or less is $3. If they leave before this time, they get the rest of their deposit back. 2 hours or less is $5, and 4 hours is the full $8.
Once they exceed this time before 6PM, it turns to a $15 flat rate (current deposit+$7). Past 6PM, another $5 is added to the current deposit, and that covers another 4 hours. If they exceed 4 hours past 6PM, an additional $10 is added to the current deposit, and that lasts until 7AM. The charge will continue at that point as the rules stated above.

It may help if you have some programming experience, or if you could write this in the format of if this, then add that, that would help me a lot, try to use words as where the numbers are stored. I'm more having problems with the time. I don't know how to write the equation to accept the different number of hours throughout the day.
 
… 1 hour or less is $3 … 2 hours or less is $5 … 4 hours is the full $8.
Is that supposed to say, "3 or 4 hours" ?

My initial thought is that I don't see a single equation encompassing all of this. But, an algorithm will surely work.
 
Let's see whether I understand the pricing.

Consider a group of people who all check in at 7am. Each person pays $8.

People leaving at or before 8am are charged $3, so they get $5 back.

People leaving at or before 9am are charged $5, so they get $3 back.

People leaving at or before 11am are charged $8, so they get nothing back.

People leaving at or before noon are charged $15, so they owe $7.

People leaving at or before 6pm are also charged $15, so they owe $7.

People leaving at or before 7pm are charged $20, so they owe $12.

People leaving at or before 11pm are also charged $20, so they owe $12.

People leaving after 11pm but before 7:01am are charged $30, so they owe $22.

After 24 hours, people leaving at or before 8am are charged $33, so they owe $25.

After 24 hours, people leaving at or before 9am are charged $35, so they owe $27.

After 24 hours, people leaving at or before 11am are charged $38, so they owe $30.

After 24 hours, people leaving at or before noon are charged $45, so they owe $37.

After 24 hours, people leaving at or before 6pm are also charged $45, so they owe $37.

After 24 hours, people leaving at or before 7pm are charged $50, so they owe $42.

After 24 hours, people leaving at or before 11pm are charged $55, so they owe $47.

After 24 hours, people leaving after 11pm but before 7:01am are charged $65, so they owe $57.

After 48 hours, people leaving at or before 8am are charged $68, so they owe $63.

Is this correct, so far? :cool:
 
Last edited:
I thought I had seen a rejoinder from robbiemcleod56, confirming the amounts I'd asked about.

I seem to remember asking another question, after that.

Both of those posts are gone (that or I dreamt it all, but I don't think so).

@Robbiemcleod56, please show me how you would calculate the amount owed (if any) for somebody who checks in at 5pm and checks out at 7pm.

Sorry for any confusion, if posts are missing.
 
Yeah, my post is gone and I don’t see yours. Not sure why. But that is correct. Let’s say the hourly rate rolls over at 6PM and 7AM. So anything before those would be charged separately than the hours after them. So if you mean same day, it would be $3 for 5PM-6PM, then the flat rate of $5 to cover the other hour past 6PM.

If you mean from 5PM to 7PM the following day;
1 hour before 6PM is $3
then add another $5 for 4 hours, but since it goes past that, add another $10, getting to 7AM (maximum for either 6PM to 7AM or 7AM to 6PM is $15, whole 24 hours is $30)
Add another $8 deposit for 4 hours, but since it goes past that, add another $7 (the maximum for that period) to get to 6PM
add $5 for the hour after 6PM (the $5 is a flat rate after 6PM for 4 hours, non refundable)
total being 3+(5+10)+(8+7)+5. P


im not sure how else to deal with the extra hours. If you can think of a better way than pricing the extra hours like this, that would be appreciated. This way will charge more around those hours. We can’t just drop them out of the equation though, because the computer program will count them. With the pen and paper this was easy, but this has to cover all the bases. :???:
 
If possible, it might be a better option to make a slight leeway around those hours. Let’s say if they come in at 5PM and leave at 7PM the same day, they only get charged two hours instead of the extra $5 that the 6PM rollover would make. I’m not sure how difficult that would be to write into the equations.
 
… if you mean same day, it would be $3 for 5PM-6PM, then the flat rate of $5 to cover the other hour past 6PM …

If possible, it might be a better option to make a slight leeway around those hours. Let’s say if they come in at 5PM and leave at 7PM the same day, they only get charged two hours instead of the extra $5 that the 6PM rollover would make …
Almost anything is possible (I think) because the task doesn't seem overwhelming; however, these types of issues (example above) ought to be settled, as much as possible, before thinking of coding.

So, I had asked about the 5pm-7pm customer because I didn't know whether all customers are charged based on the same clock or each is charged according to their own clock, or some combination of the two.

What if I were to edit your original post, and advance all of the times by 3.75 hours. Would it change what customers pay?

If your business is a busy airport parking lot (with both short-term and long-term parking), then it makes sense for time to be relative. Each customer is charged based on their own clock, which starts timing at 0 hours, and the algorithm is the same for each customer.

If your business is a flophouse, then it makes sense to have a central clock because there are peak hours and off-peak hours. Or maybe some combination of a central clock and individualized timers for each customer on some part(s).

How would you describe the clock(s) or timer(s) that you're using, to calculate the fees? Is our 24-hour day broken up into sections with different charging rates, and timers are running within each section?

When you go through the steps to calculate customer fees by hand, do you have a clear sense of pattern that remains constant from customer to customer? If that's what you posted in the op, then can you provide a list of exceptions?


… With the pen and paper this was easy, but [the code] has to cover all the bases.
I agree, it's not easy taking a set of decisions that you're accustomed to doing by rote and translating them into step-by-step, written instructions for a machine with no common sense. The only thing we can rely on the machine to do is complete our list of instructions to the letter, consistently.

If you think it would be easier, I could provide some more entry/exit timestamps, and you could describe each calculation and decision you made between doing it one way versus another, as you determined their fee by hand step-by-step. I would then respond with additional questions, to clarify things I have yet to understand.


Mainly, it's exceptions to what's been said thus far that we ought to focus on. The more information people have, the better their suggestions will be. Cheers :cool:
 
***People leaving at or before noon are charged $15, so they owe $7.
People leaving at or before 6pm are also charged $15, so they owe $7.

***People leaving at or before 7pm are charged $20, so they owe $12.
People leaving at or before 11pm are also charged $20, so they owe $12.

***After 24 hours, people leaving at or before noon are charged $45, so they owe $37.
After 24 hours, people leaving at or before 6pm are also charged $45, so they owe $37.
*** : these 3 can be removed, right?
 
*** : these 3 can be removed, right?
Yes, they can be. :) I'm having issues with my brain always wanting to divide the day in half, instead of using 11- and 13-hour intervals.

I'm wondering how strict the cutoff times are. If I plan to check out before 11:00am to avoid the extra $7 after 11am, but I'm late and check out at 11:01am, do I still get dinged?
 
Last edited:
So, those can generally be removed. But the “leaving at or before 6PM”, if they stayed more than 8 hours, it would result in $15 max for that time period. But the first post of yours was very correct. The only thing we need to work out is the leeway between the rollover times. I’m thinking give a 2 hour leeway around the hours of 6PM and 7AM. So if they come one at 4PM, and leave at 6PM, it’s $5. If they stay one more hour, it’s $8. If they come in at 3PM, and leave at 6PM, it would be the original deposit of $8. If they come in at 5PM, and leave at 7PM, it would be $5. If they come in at 6PM and leave at 9PM, it’s the $5 flat rate for 4 hours.
 
I'm using a 24-hour clock.

Times without brackets = time-of-day

Times with brackets = elasped time (ET)

Example: 18:55 means 6:55pm, and [18:55] means elapsed time of 18hrs+55min


Scenario #1

Two people check in and out; they each stay about 4 hrs.

08:44 Man IN [00:00]

08:56 Woman IN [00:00]

12:45 Man OUT [04:01]

12:55 Woman OUT [03:59]

Man: $15

Woman: $8

Is this correct?
 
Yes, that is correct. The sign was just put up, and I think this will help a lot. There’s just the fair leeway around the changeover times.
9B48373D-92BB-4B2F-8151-402057570204.jpg
 
Yes, it’s 4 hours, not 8 hours.
Thanks for clarifying.

Scenario #2

06:59 Man IN [00:00]

11:01 Man OUT [04:02]

Breakdown:

06:59 - 07:00 [00:01] $5

07:00 - 11:00 [04:00] $8

11:00 - 11:01 [00:01] $7

Charge: $20

Is this correct?

In other words, the charge is $20 for 4 hr 2 min. You mentioned "fair leeway" around thresholds. If the charge in scenario #2 is not $20, then please show how you would do the calculation by hand. Be sure to quantify any exceptions to thresholds (i.e., describe the exception in numerical terms). :cool:
 
Last edited:
It would be $8. The extra minute on each end is under the 5 minute threshold. The change at 7PM wouldn’t matter as well, the 2 hour threshold would make it $3 for one hour before the changeover and $5 for two hours. I guess using >5 minute threshold to determine whether it’s an hour before is good.
 
It would be $8. The extra minute on each end is under the 5 minute threshold.
I don't find any mention of this 5-minute grace period.

You indicated no grace period, in scenario #1, where a man parked [04:01] and was charged $7 extra for that last minute.


The change at 7PM wouldn’t matter as well …
Scenario #2 does not involve 7pm. Are you thinking of a different scenario?

It would help to have a list of all the exceptions. :cool:
 
Just got back from lunch; waitress was nice and gave me paper. This flow chart (based on my memory, at a restaurant) shows the sort of yes/no questions the algorithm needs to ask. So, this is not for coding; it's for you to get a sense of how I'm thinking.

I think we've been misunderstanding each other, on some points. I re-read the thread, and let's see whether I've formed a better picture of what you're doing. So, I'll write up a different flow (more linear) and just type it out …

CLICK THUMBNAIL TO ENLARGE IMAGE

oozeChart.JPG
 
Loud, screaming person has walked down the street twice now; keeping an eye out … I'll list flow through a simple example (customer charged only daytime rates) and annotate it with comments. Again, this is just organization; it's not for coding.

Algorithm receives IN 06:00 and OUT 11:30 timestamps

CHRG = -8


Discuss multiple-day parking, later. The CHRG starts with customer credit $8. First question determines whether we start with day or night rates.

IN before 04:00 ?

Threshold is 2 hours, so ask whether IN is earlier than 4am because, if it is, start with night rates instead. At some point, ask whether OUT is later than 6:05pm because, if it is, might end with some night rates. In this example, IN is 6am, and let's assume algorithm determines CHRG will contain only day rates. (i.e., asks if OUT<18:05 somewhere in the program)

IN = IN + [01:05]

CHRG = CHRG + 3


Started timer, by advancing IN. Added [01:05], to introduce grace period. Customer @ 1hr rate; charged $3.

Updated IN = 07:05 and CHRG = -5

IN > OUT ?

If yes, then we're done. ET ≤ [01:05]: customer CHRG = -5 so refund $5 (customer paid $3)

But, in this example, the answer is no; customer parked more than [01:05] so they are @ 2hr rate.

IN = IN + [01:00]

CHRG = CHRG + 2


Updated IN = 08:05 and CHRG = -3

IN > OUT ?

If yes, then we're done because ET > [01:05] and ≤ [02:05]: customer CHRG = -3 so refund $3 (customer paid $5)

In this example, the answer is no; customer parked more than [02:05] so they are @ 4hr rate.

IN = IN + [02:00]

CHRG = CHRG + 3


Updated IN = 10:05 and CHRG = 0

IN > OUT ?

If yes, then we're done because ET > [02:05] and ≤ [04:05]: customer CHRG = 0 so no refund (customer paid $8)

The answer is no; customer parked more than [04:05] so they are @ full day rate.

CHRG = CHRG + 7

Updated CHRG = 7

We're done because ET > [04:05] and OUT < 18:05 (determined at some point); customer CHRG = 7 so collect $7 (customer paid $15)

Again, this is another way to structure the flow. There are more details to work out, and I need to run through more scenarios, to cover all the bases. Hopefully, we're on track. If there's anything unclear with my notation or you have other questions, please let me know. (It's late, and I'm trying to do three things at the same time.) Otherwise, tell me whether this example jives with your interpretation. We'll fix it or expand on it. :cool:
 
Last edited:
Yes, that example is perfect. I think we’re both on the same page now. A couple more examples may help to make sure we have everything down, but everything seems good.
 
Top