-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPricingService.java
More file actions
63 lines (50 loc) · 2.11 KB
/
Copy pathPricingService.java
File metadata and controls
63 lines (50 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.parking.service;
import com.parking.model.ParkingSpot;
/**
* Smart pricing engine with dynamic rates based on spot type and duration.
*/
public class PricingService {
// Base rates per hour (in INR)
private static final double REGULAR_RATE = 30.0;
private static final double COMPACT_RATE = 25.0;
private static final double PREMIUM_RATE = 60.0;
private static final double EV_RATE = 40.0;
private static final double HANDICAPPED_RATE = 15.0;
// Free grace period (15 minutes)
private static final long GRACE_PERIOD_MINUTES = 15;
// Peak hour surcharge (8AM–10AM, 5PM–8PM) — 50% extra
private static final double PEAK_SURCHARGE = 1.5;
public double calculateCharge(ParkingSpot.SpotType spotType, long durationMinutes) {
if (durationMinutes <= GRACE_PERIOD_MINUTES) {
return 0.0; // Free within grace period
}
double baseRate = getBaseRate(spotType);
double hours = Math.ceil((durationMinutes - GRACE_PERIOD_MINUTES) / 60.0);
if (hours < 1) hours = 1;
double charge = baseRate * hours;
// Apply peak hour surcharge
int currentHour = java.time.LocalTime.now().getHour();
boolean isPeakHour = (currentHour >= 8 && currentHour <= 10) || (currentHour >= 17 && currentHour <= 20);
if (isPeakHour) {
charge *= PEAK_SURCHARGE;
}
// Daily cap: max 12 hours billing
double maxDailyCharge = baseRate * 12;
return Math.min(charge, maxDailyCharge);
}
private double getBaseRate(ParkingSpot.SpotType type) {
switch (type) {
case COMPACT: return COMPACT_RATE;
case PREMIUM: return PREMIUM_RATE;
case EV_CHARGING: return EV_RATE;
case HANDICAPPED: return HANDICAPPED_RATE;
default: return REGULAR_RATE;
}
}
public double getHourlyRate(ParkingSpot.SpotType type) {
return getBaseRate(type);
}
public boolean isGracePeriod(long durationMinutes) {
return durationMinutes <= GRACE_PERIOD_MINUTES;
}
}