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

Upgraded toll fee software #92

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
5 changes: 3 additions & 2 deletions Java/Car.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

package com.evolve.tollcalculator;
import static com.evolve.tollcalculator.Constants.CAR;
public class Car implements Vehicle {
@Override
public String getType() {
return "Car";
return CAR;
}
}
17 changes: 17 additions & 0 deletions Java/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.evolve.tollcalculator;
public class Constants {

public static final String CAR = "Car";
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 JULY = "July";
public static final int MAXTOLLFEE_FOR_A_DAY = 60;
public static final int MINTOLLFEE = 8;
public static final int MAXTOLLFEE=18;
public static final int NORMALTOLLFEE=13;

}
8 changes: 8 additions & 0 deletions Java/Diplomat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.evolve.tollcalculator;
import static com.evolve.tollcalculator.Constants.DIPLOMAT;
public class Diplomat implements Vehicle {
@Override
public String getType() {
return DIPLOMAT;
}
}
8 changes: 8 additions & 0 deletions Java/Emergency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.evolve.tollcalculator;
import static com.evolve.tollcalculator.Constants.EMERGENCY;
public class Emergency implements Vehicle{
@Override
public String getType() {
return EMERGENCY;
}
}
8 changes: 8 additions & 0 deletions Java/Foreign.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.evolve.tollcalculator;
import static com.evolve.tollcalculator.Constants.FOREIGN;
public class Foreign implements Vehicle{
@Override
public String getType() {
return FOREIGN;
}
}
27 changes: 27 additions & 0 deletions Java/HolidayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.evolve.tollcalculator;

public enum HolidayList {
JANUARY("1"),
MARCH("28,29"),
APRIL("1,30"),
MAY("1,8,9"),
JUNE("5,6,21"),
NOVEMBER("1"),
DECEMBER("24,25,26,31");

private final String month;

HolidayList(String month) {
this.month = month;
}

public static String getDays(String month) {
for (HolidayList holidayList : HolidayList.values()) {
if (holidayList.name().equalsIgnoreCase(month)) {
return holidayList.month;
}
}
return null;
}

}
8 changes: 8 additions & 0 deletions Java/Military.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.evolve.tollcalculator;
import static com.evolve.tollcalculator.Constants.MILITARY;
public class Military implements Vehicle {
@Override
public String getType() {
return MILITARY;
}
}
5 changes: 3 additions & 2 deletions Java/Motorbike.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

package com.evolve.tollcalculator;
import static com.evolve.tollcalculator.Constants.MOTORBIKE;
public class Motorbike implements Vehicle {
@Override
public String getType() {
return "Motorbike";
return MOTORBIKE;
}
}
64 changes: 64 additions & 0 deletions Java/TestTollCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.evolve.tollcalculator;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;

@RunWith(JUnit4.class)
public class TestTollCalculator {

TollCalculator tollCalculator = new TollCalculator();
Car car ;

@Test
public void testMinimumTollFee() throws Exception {
Assert.assertEquals(8,tollCalculator.getTollFee(car,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-05-05 06:00:00"),
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-05-05 06:20:00")));
}

@Test
public void testCasualTimeTollFee() throws Exception {
Assert.assertEquals(18,tollCalculator.getTollFee(car,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-05-05 16:00:00"),
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-05-05 18:31:00")));
}

@Test
public void testTollFreeVehicles() throws Exception {
Assert.assertEquals(0,tollCalculator.getTollFee(new Motorbike(), Date.from(Instant.now())));
Assert.assertEquals(0,tollCalculator.getTollFee(new Military(), Date.from(Instant.now())));
Assert.assertEquals(0,tollCalculator.getTollFee(new Tractor(), Date.from(Instant.now())));
Assert.assertEquals(0,tollCalculator.getTollFee(new Emergency(), Date.from(Instant.now())));
Assert.assertEquals(0,tollCalculator.getTollFee(new Diplomat(), Date.from(Instant.now())));
Assert.assertEquals(0,tollCalculator.getTollFee(new Foreign(), Date.from(Instant.now())));

}

@Test
public void testWeekends() throws Exception {
Assert.assertEquals(0,tollCalculator.getTollFee(car, new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/12/04 09:00:00")));
}

@Test
public void testHolidays() throws Exception {
Assert.assertEquals(0,tollCalculator.getTollFee(car, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-01-01 10:00:00")));
}

@Test
public void testMaximumPricePerDay() throws Exception {
Assert.assertEquals(60,tollCalculator.getTollFee(car,
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 09:00:00"),
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 11:00:00"),
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 11:30:00"),
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 13:30:00"),
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 15:30:00"),
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 16:30:00"),
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 16:00:00"),
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 10:05:00"),
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 17:30:00")));
}

}
99 changes: 39 additions & 60 deletions Java/TollCalculator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.evolve.tollcalculator;

import java.util.*;
import java.util.concurrent.*;
import static com.evolve.tollcalculator.Constants.*;

public class TollCalculator {

Expand All @@ -22,89 +24,66 @@ public int getTollFee(Vehicle vehicle, Date... dates) {
long diffInMillies = date.getTime() - intervalStart.getTime();
long minutes = timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);

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

private boolean isTollFreeVehicle(Vehicle vehicle) {
if(vehicle == null) return false;
String vehicleType = vehicle.getType();
return vehicleType.equals(TollFreeVehicles.MOTORBIKE.getType()) ||
vehicleType.equals(TollFreeVehicles.TRACTOR.getType()) ||
vehicleType.equals(TollFreeVehicles.EMERGENCY.getType()) ||
vehicleType.equals(TollFreeVehicles.DIPLOMAT.getType()) ||
vehicleType.equals(TollFreeVehicles.FOREIGN.getType()) ||
vehicleType.equals(TollFreeVehicles.MILITARY.getType());
if(vehicle == null) {
return false;
}
return TollFreeVehicles.contains(vehicle.getType());
}

public int getTollFee(final Date date, Vehicle vehicle) {
if(isTollFreeDate(date) || isTollFreeVehicle(vehicle)) return 0;
Calendar calendar = GregorianCalendar.getInstance();
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
return calculateTollFee(calendar);
}
private static int calculateTollFee(final Calendar calendar) {
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);

if (hour == 6 && minute >= 0 && minute <= 29) return 8;
else if (hour == 6 && minute >= 30 && minute <= 59) return 13;
else if (hour == 7 && minute >= 0 && minute <= 59) return 18;
else if (hour == 8 && minute >= 0 && minute <= 29) return 13;
else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) return 8;
else if (hour == 15 && minute >= 0 && minute <= 29) return 13;
else if (hour == 15 && minute >= 0 || hour == 16 && minute <= 59) return 18;
else if (hour == 17 && minute >= 0 && minute <= 59) return 13;
else if (hour == 18 && minute >= 0 && minute <= 29) return 8;
if (hour == 6 && minute >= 0 && minute <= 29) return MINTOLLFEE;
else if (hour == 6 && minute >= 30 && minute <= 59) return NORMALTOLLFEE;
else if (hour == 7 && minute >= 0 && minute <= 59) return MAXTOLLFEE;
else if (hour == 8 && minute >= 0 && minute <= 29) return NORMALTOLLFEE;
else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) return MINTOLLFEE;
else if (hour == 15 && minute >= 0 && minute <= 29) return NORMALTOLLFEE;
else if (hour == 15 && minute >= 0 || hour == 16 && minute <= 59) return MAXTOLLFEE;
else if (hour == 17 && minute >= 0 && minute <= 59) return NORMALTOLLFEE;
else if (hour == 18 && minute >= 0 && minute <= 29) return MINTOLLFEE;
else return 0;
}

private Boolean isTollFreeDate(Date date) {
Calendar calendar = GregorianCalendar.getInstance();
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);

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

if (year == 2013) {
if (month == Calendar.JANUARY && day == 1 ||
month == Calendar.MARCH && (day == 28 || day == 29) ||
month == Calendar.APRIL && (day == 1 || day == 30) ||
month == Calendar.MAY && (day == 1 || day == 8 || day == 9) ||
month == Calendar.JUNE && (day == 5 || day == 6 || day == 21) ||
month == Calendar.JULY ||
month == Calendar.NOVEMBER && day == 1 ||
month == Calendar.DECEMBER && (day == 24 || day == 25 || day == 26 || day == 31)) {
return true;
}
}
return false;
if(isWeekend(calendar)) return true;
return isHoliday(calendar);
}
private static boolean isWeekend(final Calendar cal)
{
int day = cal.get(Calendar.DAY_OF_WEEK);
return day == Calendar.SATURDAY || day == Calendar.SUNDAY;
}

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;
}
private static boolean isHoliday(final Calendar calendar)
{
String month = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
if (month.equalsIgnoreCase(JULY)) return true;
String holidays = HolidayList.getDays(month);
if (null == holidays) return false;
List<String> holidayList = new ArrayList<>(Arrays.asList(holidays.split(",")));
int day = calendar.get(Calendar.DAY_OF_MONTH);
return holidayList.contains(Integer.toString(day));
}
}

}
70 changes: 70 additions & 0 deletions Java/TollCalculatorMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.evolve.tollcalculator;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class TollCalculatorMain {

public static void main(String[] args) throws ParseException {
System.out.println("Movie name of the GIF is Hackers ;) ");

TollCalculator calculator = new TollCalculator();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Vehicle test = new Car();
String dateString1 = "2013-06-26 07:00:00";
String dateString2 = "2013-06-26 15:31:00";
String dateString3 = "2013-06-26 15:33:00";
String dateString4 = "2013-06-26 16:34:00";
Date dateObject1 = sdf.parse(dateString1);
Date dateObject2 = sdf.parse(dateString2);
Date dateObject3 = sdf.parse(dateString3);
Date dateObject4 = sdf.parse(dateString4);
int value = calculator.getTollFee(test,dateObject1,dateObject2,dateObject3,dateObject4);
System.out.println("Maximum TollFee Test: " +value);

test = new Car();
dateString1 = "2013-06-26 06:00:00";
dateString2 = "2013-06-26 06:20:00";
dateObject1 = sdf.parse(dateString1);
dateObject2 = sdf.parse(dateString2);
value = calculator.getTollFee(test,dateObject1,dateObject2);
System.out.println("Minimum TollFee Test: " +value);

test = new Motorbike();
dateString1 = "2013-06-26 07:00:00";
dateString2 = "2013-06-26 15:31:00";
dateObject1 = sdf.parse(dateString1);
dateObject2 = sdf.parse(dateString2);
value = calculator.getTollFee(test,dateObject1,dateObject2);
System.out.println("TollFree Test: " +value);

test = new Car();
dateString1 = "2013-06-21 07:00:00";
dateString2 = "2013-06-21 15:31:00";
dateObject1 = sdf.parse(dateString1);
dateObject2 = sdf.parse(dateString2);
value = calculator.getTollFee(test,dateObject1,dateObject2);
System.out.println("Holiday Toll Test: " +value);

test = new Car();
dateString1 = "2013-06-15 07:00:00";
dateString2 = "2013-06-15 15:31:00";
dateObject1 = sdf.parse(dateString1);
dateObject2 = sdf.parse(dateString2);
value = calculator.getTollFee(test,dateObject1,dateObject2);
System.out.println("Weekend TollFee Test: " +value);

test = new Car();
dateString1 = "2013-06-26 16:00:00";
dateString2 = "2013-06-26 18:31:00";
dateObject1 = sdf.parse(dateString1);
dateObject2 = sdf.parse(dateString2);
value = calculator.getTollFee(test,dateObject1,dateObject2);
System.out.println("Casual Hours Test: " +value);


}
}
22 changes: 22 additions & 0 deletions Java/TollFreeVehicles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.evolve.tollcalculator;

import java.util.stream.Stream;

public 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 static boolean contains(final String type) {
return Stream.of(TollFreeVehicles.values()).anyMatch(vehicleType -> vehicleType.type.equalsIgnoreCase(type));
}

}
Loading