Implementation of charging algorithm based on java

clipboard.png

as shown in the picture, the parking fee is charged per second every day, regardless of holidays.
Note:

  1. the billing period is 24 hours, and the end of the day at 21 o"clock must be next to the night start time at 21:00
  2. the variables are daytime start time, daytime end time, night start time, night end time, daytime billing standard N yuan / half hour, night pay per time N yuan / time
  3. charge for more than 1 millisecond, no free time

for example, if a car is parked from 7: 00 a.m. to 9: 00 the next day, the parking fee: 4 + (21-8) * 8 + 4 + (9-8) * 8

my algorithm is weak. I only think of recursive 8-21-8-21 to see that if the parking end time is greater than the next end point (if the start is 8, the next end point is 21), then the end time takes one end point (21) and calculates the cost within its range. Recursive, until the parking end time is less than a small end point, take the end time, calculate the cost, and stop recursion.

I hope you can give me some comments. I appreciate it.


  1. initial value of total is 0
  2. if the start time is before 8 o'clock, total+4, then set the start time to 8 o'clock sharp
    if the start time is after 21:00, total+4, then set the start time to 8 o'clock sharp the next day
  3. returns if the end time is less than or equal to the start time. Otherwise, execute the later judgment
  4. calculate the integer days of the difference between the end time and the start time, multiply it by the daily cost, add it to total, and then set the start time to start time + integer days
  5. if the end time is before 8 o'clock or after 21:00, total+4+ (21-8) * 8
    otherwise, total+ rounds up [(end time * 2-16)] * 4

all operations that compare start time and end time need to consider the date, not just the hour time. For example, any time of the next day is later than any time of the day before.

The purpose of

step 2 is to normalize the start time to facilitate later calculations.

the charge for a whole day (from 8 o'clock to 8 o'clock on the second day) here should be calculated in terms of 108 instead of 112: (21-8) * 8pm 4pm , because only one parking can be counted from 21:00 at night to 8 o'clock on the second day, so the fee is 4 yuan.

< hr >

Let me give you a few examples, just as testing the algorithm by the way:

  1. start time = 5: 00, end time = 7: 00 (same day):

     (1) total=0
     (2) total=48
     (3) 
     (4) 1total=4+108=11228
     (5) 21total=112+4+(21-8)*8=220
Menu