diff --git a/Java/Car.java b/Java/Car.java index e0ec832c..4a02d27d 100644 --- a/Java/Car.java +++ b/Java/Car.java @@ -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; } } diff --git a/Java/Constants.java b/Java/Constants.java new file mode 100644 index 00000000..b54bbc6e --- /dev/null +++ b/Java/Constants.java @@ -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; + +} diff --git a/Java/Diplomat.java b/Java/Diplomat.java new file mode 100644 index 00000000..b0069798 --- /dev/null +++ b/Java/Diplomat.java @@ -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; + } +} diff --git a/Java/Emergency.java b/Java/Emergency.java new file mode 100644 index 00000000..2b2bb6b9 --- /dev/null +++ b/Java/Emergency.java @@ -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; + } +} diff --git a/Java/Foreign.java b/Java/Foreign.java new file mode 100644 index 00000000..84bcc549 --- /dev/null +++ b/Java/Foreign.java @@ -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; + } +} diff --git a/Java/HolidayList.java b/Java/HolidayList.java new file mode 100644 index 00000000..37e12e61 --- /dev/null +++ b/Java/HolidayList.java @@ -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; + } + +} diff --git a/Java/Military.java b/Java/Military.java new file mode 100644 index 00000000..1756f71b --- /dev/null +++ b/Java/Military.java @@ -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; + } +} diff --git a/Java/Motorbike.java b/Java/Motorbike.java index 14056ff9..a7e83bdd 100644 --- a/Java/Motorbike.java +++ b/Java/Motorbike.java @@ -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; } } diff --git a/Java/TestTollCalculator.java b/Java/TestTollCalculator.java new file mode 100644 index 00000000..f1bcd210 --- /dev/null +++ b/Java/TestTollCalculator.java @@ -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"))); + } + +} diff --git a/Java/TollCalculator.java b/Java/TollCalculator.java index d239f327..5a1ff53c 100644 --- a/Java/TollCalculator.java +++ b/Java/TollCalculator.java @@ -1,6 +1,8 @@ +package com.evolve.tollcalculator; import java.util.*; import java.util.concurrent.*; +import static com.evolve.tollcalculator.Constants.*; public class TollCalculator { @@ -22,7 +24,7 @@ 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; @@ -30,81 +32,58 @@ public int getTollFee(Vehicle vehicle, Date... dates) { 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 holidayList = new ArrayList<>(Arrays.asList(holidays.split(","))); + int day = calendar.get(Calendar.DAY_OF_MONTH); + return holidayList.contains(Integer.toString(day)); } -} - +} \ No newline at end of file diff --git a/Java/TollCalculatorMain.java b/Java/TollCalculatorMain.java new file mode 100644 index 00000000..3afb6fda --- /dev/null +++ b/Java/TollCalculatorMain.java @@ -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); + + + } +} diff --git a/Java/TollFreeVehicles.java b/Java/TollFreeVehicles.java new file mode 100644 index 00000000..880aec51 --- /dev/null +++ b/Java/TollFreeVehicles.java @@ -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)); + } + +} diff --git a/Java/Tractor.java b/Java/Tractor.java new file mode 100644 index 00000000..95fe492a --- /dev/null +++ b/Java/Tractor.java @@ -0,0 +1,8 @@ +package com.evolve.tollcalculator; +import static com.evolve.tollcalculator.Constants.TRACTOR; +public class Tractor implements Vehicle { + @Override + public String getType() { + return TRACTOR; + } +} diff --git a/Java/Vehicle.java b/Java/Vehicle.java index 289ec7df..705e559c 100644 --- a/Java/Vehicle.java +++ b/Java/Vehicle.java @@ -1,5 +1,6 @@ +package com.evolve.tollcalculator; public interface Vehicle { - public String getType(); + String getType(); }