-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a Pricing calculation strategy.
- Loading branch information
Priyak Dey
committed
Sep 23, 2023
1 parent
8b094be
commit d6d726c
Showing
6 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/com/priyakdey/parker/core/model/ParkingCharge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.priyakdey.parker.core.model; | ||
|
||
/** | ||
* Represents the details of a parking charge for a vehicle. | ||
* | ||
* <p>This record abstracts the representation of hours parked and the charge incurred | ||
* to avoid tight coupling with specific numerical or currency types. | ||
* By using {@code String} for all fields, it provides flexibility in terms of | ||
* representing charges (which could be integers today and floating points tomorrow) | ||
* and currency units.</p> | ||
* <p> | ||
* It is a data classes which holds the following information: | ||
* <li> | ||
* <ul>registrationNumber: The registration number of the parked vehicle.</ul> | ||
* <ul>hoursParked: The number of hours the vehicle has been parked. Example formats "2", "2.5", "2 hours 30 minutes", etc.</ul> | ||
* <ul>charge: The charge incurred for the parking duration. Example formats: "10", "10.6", "10 dollars 60 cents", etc.<ul/> | ||
* </li> | ||
* <p/> | ||
* | ||
* @author Priyak Dey | ||
*/ | ||
public record ParkingCharge(String registrationNumber, String hoursParked, String charge) { | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/priyakdey/parker/core/pricing/ParkingFeeCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.priyakdey.parker.core.pricing; | ||
|
||
/** | ||
* Represents a mechanism to compute the parking fee based on the number of hours a vehicle has been parked. | ||
* | ||
* <p> | ||
* Implementations of this interface will define the logic to calculate the parking fee | ||
* for a given number of hours. This allows for flexibility in defining different pricing | ||
* strategies for parking. | ||
* </p> | ||
* <p> | ||
* Example: | ||
* <pre> | ||
* ParkingFeeCalculator calculator = ...; | ||
* Number fee = calculator.computePrice(5); // Calculate fee for 5 hours of parking. | ||
* </pre> | ||
* | ||
* @author Priyak Dey | ||
*/ | ||
public interface ParkingFeeCalculator { | ||
|
||
/** | ||
* Computes the parking fee for the specified number of hours. | ||
* | ||
* @param hoursParked The number of hours for which the parking fee should be computed. | ||
* @return The computed parking fee as a {@link Number}. Implementations might return fee as | ||
* an integer, decimal, or other numerical representations. | ||
*/ | ||
Number computePrice(int hoursParked); | ||
} | ||
|
61 changes: 61 additions & 0 deletions
61
src/main/java/com/priyakdey/parker/core/pricing/impl/PerHourFeeCalculatorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.priyakdey.parker.core.pricing.impl; | ||
|
||
import com.priyakdey.parker.core.pricing.ParkingFeeCalculator; | ||
import com.priyakdey.parker.exception.BadInputException; | ||
|
||
/** | ||
* Implementation of the {@link ParkingFeeCalculator} that calculates parking fees | ||
* based on a per-hour pricing model. | ||
* | ||
* <p> | ||
* This implementation uses a flat rate for the first few hours (as defined by | ||
* {@link #FLAT_RATE_DURATION_HOURS}) and then charges a specified rate for every additional | ||
* hour. | ||
* </p> | ||
* <p> | ||
* Example: | ||
* <pre> | ||
* ParkingFeeCalculator calculator = new PerHourFeeCalculatorImpl(); | ||
* Number fee = calculator.computePrice(3); // Calculate fee for 3 hours of parking. | ||
* </pre> | ||
* | ||
* @author Priyak Dey | ||
*/ | ||
public class PerHourFeeCalculatorImpl implements ParkingFeeCalculator { | ||
|
||
/** | ||
* The fixed charge applicable for parking durations up to FLAT_RATE_DURATION_HOURS. | ||
*/ | ||
private static final int FLAT_RATE_CHARGE = 10; | ||
|
||
/** | ||
* The number of hours for which the flat rate (FLAT_RATE_CHARGE) applies. | ||
*/ | ||
private static final int FLAT_RATE_DURATION_HOURS = 2; | ||
|
||
|
||
/** | ||
* Charge applied for every hour after the INITIAL_HOURS. | ||
*/ | ||
private static final int PER_HOUR_CHARGE = 10; | ||
|
||
|
||
/** | ||
* Computes the parking fee for the specified number of hours. | ||
* | ||
* @param hoursParked The number of hours for which the parking fee should be computed. | ||
* @return The computed parking fee as a {@link Number}. Implementations might return fee as | ||
* an integer, decimal, or other numerical representations. | ||
*/ | ||
@Override | ||
public Number computePrice(int hoursParked) { | ||
if (hoursParked < 0) { | ||
throw new BadInputException("Hours parked cannot be negative"); | ||
} | ||
|
||
int overheadHours = | ||
hoursParked > FLAT_RATE_DURATION_HOURS ? hoursParked - FLAT_RATE_DURATION_HOURS : 0; | ||
|
||
return FLAT_RATE_CHARGE + overheadHours * PER_HOUR_CHARGE; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/priyakdey/parker/core/service/ParkingLot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.priyakdey.parker.core.service;/** | ||
* | ||
* @author Priyak Dey | ||
*/ | ||
public class ParkingLot { | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/priyakdey/parker/core/service/ParkingLotManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.priyakdey.parker.core.service;/** | ||
* | ||
* @author Priyak Dey | ||
*/ | ||
public class ParkingLotManager { | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/priyakdey/parker/core/service/ParkingLotManagerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.priyakdey.parker.core.service;/** | ||
* | ||
* @author Priyak Dey | ||
*/ | ||
public class ParkingLotManagerImpl { | ||
} |