Skip to content

Commit

Permalink
Create a Pricing calculation strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
Priyak Dey committed Sep 23, 2023
1 parent 8b094be commit d6d726c
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/priyakdey/parker/core/model/ParkingCharge.java
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) {
}
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);
}

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;
}
}
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 {
}
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 {
}
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 {
}

0 comments on commit d6d726c

Please sign in to comment.