Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saravanan Toll Calculator #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions C#/Car.cs

This file was deleted.

674 changes: 0 additions & 674 deletions C#/LICENSE

This file was deleted.

16 changes: 0 additions & 16 deletions C#/Motorbike.cs

This file was deleted.

108 changes: 0 additions & 108 deletions C#/TollCalculator.cs

This file was deleted.

13 changes: 0 additions & 13 deletions C#/Vehicle.cs

This file was deleted.

7 changes: 0 additions & 7 deletions Java/Car.java

This file was deleted.

7 changes: 0 additions & 7 deletions Java/Motorbike.java

This file was deleted.

110 changes: 0 additions & 110 deletions Java/TollCalculator.java

This file was deleted.

5 changes: 0 additions & 5 deletions Java/Vehicle.java

This file was deleted.

30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.presis.code.challenge</groupId>
<artifactId>toll-calculator</artifactId>
<version>2.0.0</version>
<name>Toll</name>
<description>Toll calculator program - Code Challanges</description>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-properties</artifactId>
<version>2.14.2</version>
</dependency>

</dependencies>

</project>
66 changes: 66 additions & 0 deletions src/main/java/com/presis/code/challenge/toll/TollCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.presis.code.challenge.toll;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import com.presis.code.challenge.toll.config.ApplicationProperties;
import com.presis.code.challenge.toll.model.TollMaster;
import com.presis.code.challenge.toll.model.Vehicle;
import com.presis.code.challenge.util.TollUtil;

public final class TollCalculator {

static TollMaster configData;

public TollCalculator() {
configData = new ApplicationProperties().readMaster();
}

/**
* Calculate the total toll fee for one day
*
* @param vehicle - the vehicle
* @param dates - date and time of all passes on one day
* @return - the total toll fee for that day
*/
public int calculateTotalTollFee(Vehicle vehicle, LocalDateTime... dates) {
// checkSameDay - date and time of all passes on one day
if (configData != null && vehicle != null && dates != null && TollUtil.checkSameDay(dates)) {

Arrays.sort(dates); // If in case not in order
LocalDateTime intervalStart = dates[0];

int firstTollFee = calculateOneEntry(vehicle, intervalStart);
int total = firstTollFee;
System.out.println("1st Travel on = " + intervalStart + ", Toll Fee = " + firstTollFee);
List<LocalDateTime> remainingInterval = Arrays.asList(dates).subList(1, dates.length);

for (LocalDateTime date : remainingInterval) {
int currentTollFee = calculateOneEntry(vehicle, date);
long minutesSincePreviousTrip = Duration.between(intervalStart, date).toMinutes();

System.out.println("Travel on = " + date + ", Toll Fee = " + currentTollFee + ", From last Trip min = " + minutesSincePreviousTrip);

if (minutesSincePreviousTrip > configData.getMaxNextTripMinute()) {
intervalStart = date;
firstTollFee = currentTollFee;
total = total + currentTollFee;
} else if (firstTollFee < currentTollFee) {
total = total + currentTollFee - firstTollFee;
}
System.out.println("Total Toll Fee -> " + total);
}
return total > configData.getMaxFee() ? configData.getMaxFee() : total;
}
return 0;
}

private static int calculateOneEntry(Vehicle vehicle, LocalDateTime date) {
if (TollUtil.isTollFreeDate(configData, date.toLocalDate()) || TollUtil.isTollFreeVehicle(vehicle)) {
return 0;
}
return configData.fetchFeeForTimeRange(date.toLocalTime());
}
}
Loading