# Fixed cost upto a distance limit, Per unit distance cost after that

Hi ,

I have a problem :

We have many vehicles and they all will be paid a fixed amount if they travel upto X kms but if they travel more than X kms then we will pay them per unit of extra distance that they travelled .

Eg. suppose we will pay vehicle 100\$ if they travel upto 120 kms and 2\$ per km after that . So , if some vehicle travels 140km then , cost will 100(\$) + (140-120)*2 = 100\$ + 40\$ = 140\$ . How can we implement such a cost function . My main problem is because if am unable to retrieve the value of distance travelled till now in the getTransportCost function .

They definition of getTransportCost is like this :>

``````@Override
public double getTransportCost(Location from, Location to, double time, Driver driver, Vehicle vehicle) {
double distance;
try {
distance = calculateDistance(from, to);
} catch (NullPointerException e) {
throw new NullPointerException("cannot calculate euclidean distance. coordinates are missing. either add coordinates or use another transport-cost-calculator.");
}
double costs = distance;
if (vehicle != null) {
if (vehicle.getType() != null) {
costs = distance * vehicle.getType().getVehicleCostParams().perDistanceUnit;
}
}
return costs;
}
``````

Unable to fetch route level data inorder to override the function .