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

Solution includes below changes : #109

Open
wants to merge 3 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/out
/.idea
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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.tollcalculator.constants;

public class TollCalculatorConstants {
public static final int MAX_FEE=60;
public static final String MOTORBIKE="Motorbike";
public static final String DIPLOMAT="Diplomat";
public static final String EMERGENCY="Emergency";
public static final String MILITARY="Military";
public static final String TRACTOR="Tractor";
public static final String FOREIGN="Foreign";
public static final String CAR="Car";
}
21 changes: 21 additions & 0 deletions Java/src/main/java/com/tollcalculator/enums/TollFreeVehicles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.tollcalculator.enums;

import com.tollcalculator.constants.TollCalculatorConstants;

public enum TollFreeVehicles {
MOTORBIKE(TollCalculatorConstants.MOTORBIKE),
TRACTOR(TollCalculatorConstants.TRACTOR),
EMERGENCY(TollCalculatorConstants.EMERGENCY),
DIPLOMAT(TollCalculatorConstants.DIPLOMAT),
FOREIGN(TollCalculatorConstants.FOREIGN),
MILITARY(TollCalculatorConstants.MILITARY);
private final String type;

TollFreeVehicles(String type) {
this.type = type;
}

public String getType() {
return type;
}
}
10 changes: 10 additions & 0 deletions Java/src/main/java/com/tollcalculator/pojo/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tollcalculator.pojo;

import com.tollcalculator.constants.TollCalculatorConstants;

public class Car implements Vehicle {
@Override
public String getType() {
return TollCalculatorConstants.CAR;
}
}
10 changes: 10 additions & 0 deletions Java/src/main/java/com/tollcalculator/pojo/Diplomat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tollcalculator.pojo;

import com.tollcalculator.constants.TollCalculatorConstants;

public class Diplomat implements Vehicle{
@Override
public String getType() {
return TollCalculatorConstants.DIPLOMAT;
}
}
10 changes: 10 additions & 0 deletions Java/src/main/java/com/tollcalculator/pojo/Emergency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tollcalculator.pojo;

import com.tollcalculator.constants.TollCalculatorConstants;

public class Emergency implements Vehicle{
@Override
public String getType() {
return TollCalculatorConstants.EMERGENCY;
}
}
10 changes: 10 additions & 0 deletions Java/src/main/java/com/tollcalculator/pojo/Foreign.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tollcalculator.pojo;

import com.tollcalculator.constants.TollCalculatorConstants;

public class Foreign implements Vehicle{
@Override
public String getType() {
return TollCalculatorConstants.FOREIGN;
}
}
10 changes: 10 additions & 0 deletions Java/src/main/java/com/tollcalculator/pojo/Military.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tollcalculator.pojo;

import com.tollcalculator.constants.TollCalculatorConstants;

public class Military implements Vehicle{
@Override
public String getType() {
return TollCalculatorConstants.MILITARY;
}
}
10 changes: 10 additions & 0 deletions Java/src/main/java/com/tollcalculator/pojo/Motorbike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tollcalculator.pojo;

import com.tollcalculator.constants.TollCalculatorConstants;

public class Motorbike implements Vehicle {
@Override
public String getType() {
return TollCalculatorConstants.MOTORBIKE;
}
}
11 changes: 11 additions & 0 deletions Java/src/main/java/com/tollcalculator/pojo/Tractor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.tollcalculator.pojo;

import com.tollcalculator.constants.TollCalculatorConstants;

public class Tractor implements Vehicle{

@Override
public String getType() {
return TollCalculatorConstants.TRACTOR;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package com.tollcalculator.pojo;

public interface Vehicle {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.tollcalculator.service;

import com.tollcalculator.constants.TollCalculatorConstants;
import com.tollcalculator.enums.TollFreeVehicles;
import com.tollcalculator.pojo.Vehicle;

import java.util.*;
import java.util.concurrent.*;

import static com.tollcalculator.constants.TollCalculatorConstants.MAX_FEE;

public class TollCalculator {

/**
Expand All @@ -12,26 +19,39 @@ public class TollCalculator {
* @return - the total toll fee for that day
*/
public int getTollFee(Vehicle vehicle, Date... dates) {
Date intervalStart = dates[0];
int totalFee = 0;
for (Date date : dates) {
int nextFee = getTollFee(date, vehicle);
int tempFee = getTollFee(intervalStart, vehicle);

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

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

if(null!=dates && null!=vehicle){
Arrays.sort(dates);
Date intervalStart = dates[0];
int totalFee = 0;
for (Date date : dates) {
/**
* Maximum total fee is 60 .So if the total fee is greater than 60 then no need to calculate further
*/
if (totalFee > MAX_FEE) {
totalFee = MAX_FEE;
return totalFee;
}
int nextFee = getTollFee(date, vehicle);
int tempFee = getTollFee(intervalStart, vehicle);

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

if (minutes <= 60) {
if (totalFee > 0) totalFee -= tempFee;
if (nextFee >= tempFee) tempFee = nextFee;
totalFee += tempFee;
} else {
totalFee += nextFee;
intervalStart = date;
}
}
return totalFee;
}else{
throw new RuntimeException("Vehicle or Dates should not be null");
}
if (totalFee > 60) totalFee = 60;
return totalFee;
}

private boolean isTollFreeVehicle(Vehicle vehicle) {
Expand Down Expand Up @@ -89,22 +109,5 @@ private Boolean isTollFreeDate(Date date) {
return false;
}

private enum TollFreeVehicles {
MOTORBIKE("Motorbike"),
TRACTOR("Tractor"),
EMERGENCY("Emergency"),
DIPLOMAT("Diplomat"),
FOREIGN("Foreign"),
MILITARY("Military");
private final String type;

TollFreeVehicles(String type) {
this.type = type;
}

public String getType() {
return type;
}
}
}

Loading