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

Tamil - Toll free Calculator #91

Open
wants to merge 2 commits 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
4 changes: 4 additions & 0 deletions Java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
### IntelliJ IDEA ###
.idea

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.

45 changes: 45 additions & 0 deletions Java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.work</groupId>
<artifactId>toll-calculator-master</artifactId>
<version>1.0-SNAPSHOT</version>

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

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>

<!-- junit 5, unit test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<finalName>maven-unit-test</finalName>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>

</plugins>
</build>

</project>
160 changes: 160 additions & 0 deletions Java/src/main/java/com/work/TollCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package com.work;

import com.work.exceptions.MissingHolidayDataException;
import com.work.model.TollFeeTime;
import com.work.model.vehicles.Vehicle;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class TollCalculator {

private static final int MAX_FEE_PER_DAY = 60;
private List<TollFeeTime> tollFeeTimes = null;

/**
* 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 getTollFee(Vehicle vehicle, Date... dates) throws MissingHolidayDataException, IOException {

if(dates == null || dates.length == 0) return 0;
if(vehicle != null && vehicle.isTollFree()) return 0;
if(isTollFreeDate(dates[0])) return 0;
if(dates.length == 1){
return getTollFee(dates[0]);
}

Date intervalStart = dates[0];
int totalFee = 0;
int tempFee = 0;

Arrays.sort(dates);


for (Date date : dates) {

int nextFee = getTollFee(date);

TimeUnit timeUnit = TimeUnit.MINUTES;
long diffInMillies = date.getTime() - intervalStart.getTime();
long minutes = timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);

if (minutes <= 60) {
if(nextFee >= tempFee) {
totalFee -= tempFee;
totalFee += nextFee;
tempFee = nextFee;
}

} else {
intervalStart = date;
tempFee = nextFee;
totalFee += nextFee;
}
if (totalFee >= MAX_FEE_PER_DAY){
return MAX_FEE_PER_DAY;
}

}
return totalFee;
}

private List<TollFeeTime> getFeeAndTimes() throws IOException {
List<TollFeeTime> tollFeeTimesList = new ArrayList<>();
try{
JSONObject jsonObject = new JSONObject(Files.readString(Paths.get("src/main/resources/TollFees.json")));
JSONArray feeTimes = jsonObject.getJSONArray("feeTimes");
feeTimes.forEach(feeTime -> {
JSONObject jsonFeeTime = (JSONObject) feeTime;
JSONArray timeArray = jsonFeeTime.getJSONArray("time");
timeArray.forEach(time -> {
TollFeeTime tollFeeTime = new TollFeeTime();
JSONObject jsonTime = (JSONObject) time;
tollFeeTime.setStartTime(LocalTime.parse(jsonTime.getString("startTime")));
tollFeeTime.setEndTime(LocalTime.parse(jsonTime.getString("endTime")));
tollFeeTime.setFee(jsonFeeTime.getInt("fee"));
tollFeeTimesList.add(tollFeeTime);
});
});
} catch (Exception e){
throw new RuntimeException("Exception occurred in getting fee from json", e);
}

return tollFeeTimesList;
}

private int getTollFee(final Date date) throws IOException {

if(tollFeeTimes == null){
tollFeeTimes = getFeeAndTimes();
}

LocalTime localTime = LocalTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
Optional<TollFeeTime> optionalTollFeeTime= tollFeeTimes.stream().filter(tollFeeTime ->
tollFeeTime.getStartTime().equals(localTime) || tollFeeTime.getEndTime().equals(localTime) ||
(localTime.isAfter(tollFeeTime.getStartTime()) && localTime.isBefore(tollFeeTime.getEndTime()))).findFirst();
return optionalTollFeeTime.map(TollFeeTime::getFee).orElse(0);
}

private boolean isTollFreeDate(Date date) throws MissingHolidayDataException {
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);

int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) return true;

int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
return isHoliday(year, month, day);
}

private boolean isHoliday(int year, int month, int day) throws MissingHolidayDataException {

try {
JSONObject jsonObject = new JSONObject(Files.readString(Paths.get("src/main/resources/Holidays.json")));
JSONObject holidays = jsonObject.getJSONObject(String.valueOf(year));
if(holidays != null && !holidays.isEmpty()){
String monthlyHolidays = holidays.getString(getMonth(month));
String[] dateArray = monthlyHolidays.split(",");
if(Arrays.asList(dateArray).contains(String.valueOf(day)))
return true;
}

}catch (Exception e){
throw new MissingHolidayDataException(e.getCause());
}
return false;
}

private String getMonth (int monthNum){
switch (monthNum){
case 0: return "January";
case 1: return "February";
case 2: return "March";
case 3: return "April";
case 4: return "May";
case 5: return "June";
case 6: return "July";
case 7: return "August";
case 8: return "September";
case 9: return "October";
case 10: return "November";
case 11: return "December";
default : return "Error";
}
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.work.exceptions;

public class MissingHolidayDataException extends Exception {
public MissingHolidayDataException(Throwable cause) {
super("Unable to get Holiday list for the date", cause);
}
}
35 changes: 35 additions & 0 deletions Java/src/main/java/com/work/model/TollFeeTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.work.model;

import java.time.LocalTime;

public class TollFeeTime {

private LocalTime startTime;
private LocalTime endTime;
private int fee;

public LocalTime getStartTime() {
return startTime;
}

public void setStartTime(LocalTime startTime) {
this.startTime = startTime;
}

public LocalTime getEndTime() {
return endTime;
}

public void setEndTime(LocalTime endTime) {
this.endTime = endTime;
}

public int getFee() {
return fee;
}

public void setFee(int fee) {
this.fee = fee;
}

}
20 changes: 13 additions & 7 deletions Java/Car.java → ...ain/java/com/work/model/vehicles/Car.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@

public class Car implements Vehicle {
@Override
public String getType() {
return "Car";
}
}
package com.work.model.vehicles;

public class Car implements Vehicle {
@Override
public String getType() {
return "Car";
}

@Override
public boolean isTollFree() {
return false;
}
}
Loading