From 9d721de47d29f7f23580b8947391d4a6fef0e9dc Mon Sep 17 00:00:00 2001 From: Tamilarasan Date: Sat, 20 Nov 2021 23:35:28 +0100 Subject: [PATCH 1/2] Upgraded Toll Calculator --- Java/.gitignore | 4 + Java/TollCalculator.java | 110 ------------- Java/Vehicle.java | 5 - Java/pom.xml | 45 +++++ .../main/java/com/work/TollCalculator.java | 155 ++++++++++++++++++ .../MissingHolidayDataException.java | 7 + .../main/java/com/work/model/TollFeeTime.java | 35 ++++ .../java/com/work/model/vehicles}/Car.java | 20 ++- .../com/work/model/vehicles/Diplomat.java | 15 ++ .../com/work/model/vehicles/Emergency.java | 15 ++ .../java/com/work/model/vehicles/Foreign.java | 15 ++ .../com/work/model/vehicles/Military.java | 15 ++ .../com/work/model/vehicles}/Motorbike.java | 20 ++- .../java/com/work/model/vehicles/Tractor.java | 15 ++ .../java/com/work/model/vehicles/Vehicle.java | 7 + Java/src/main/resources/Holidays.json | 18 ++ Java/src/main/resources/TollFees.json | 55 +++++++ Java/src/package-info.java | 1 + .../java/com/work/TollCalculatorTest.java | 110 +++++++++++++ 19 files changed, 538 insertions(+), 129 deletions(-) create mode 100644 Java/.gitignore delete mode 100644 Java/TollCalculator.java delete mode 100644 Java/Vehicle.java create mode 100644 Java/pom.xml create mode 100644 Java/src/main/java/com/work/TollCalculator.java create mode 100644 Java/src/main/java/com/work/exceptions/MissingHolidayDataException.java create mode 100644 Java/src/main/java/com/work/model/TollFeeTime.java rename Java/{ => src/main/java/com/work/model/vehicles}/Car.java (50%) create mode 100644 Java/src/main/java/com/work/model/vehicles/Diplomat.java create mode 100644 Java/src/main/java/com/work/model/vehicles/Emergency.java create mode 100644 Java/src/main/java/com/work/model/vehicles/Foreign.java create mode 100644 Java/src/main/java/com/work/model/vehicles/Military.java rename Java/{ => src/main/java/com/work/model/vehicles}/Motorbike.java (53%) create mode 100644 Java/src/main/java/com/work/model/vehicles/Tractor.java create mode 100644 Java/src/main/java/com/work/model/vehicles/Vehicle.java create mode 100644 Java/src/main/resources/Holidays.json create mode 100644 Java/src/main/resources/TollFees.json create mode 100644 Java/src/package-info.java create mode 100644 Java/src/test/java/com/work/TollCalculatorTest.java diff --git a/Java/.gitignore b/Java/.gitignore new file mode 100644 index 00000000..d53a3abd --- /dev/null +++ b/Java/.gitignore @@ -0,0 +1,4 @@ +target +### IntelliJ IDEA ### +.idea + diff --git a/Java/TollCalculator.java b/Java/TollCalculator.java deleted file mode 100644 index d239f327..00000000 --- a/Java/TollCalculator.java +++ /dev/null @@ -1,110 +0,0 @@ - -import java.util.*; -import java.util.concurrent.*; - -public class TollCalculator { - - /** - * 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) { - 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 (totalFee > 60) totalFee = 60; - 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()); - } - - public int getTollFee(final Date date, Vehicle vehicle) { - if(isTollFreeDate(date) || isTollFreeVehicle(vehicle)) return 0; - Calendar calendar = GregorianCalendar.getInstance(); - calendar.setTime(date); - 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; - else return 0; - } - - private Boolean isTollFreeDate(Date date) { - Calendar calendar = GregorianCalendar.getInstance(); - 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; - } - - 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; - } - } -} - diff --git a/Java/Vehicle.java b/Java/Vehicle.java deleted file mode 100644 index 289ec7df..00000000 --- a/Java/Vehicle.java +++ /dev/null @@ -1,5 +0,0 @@ - -public interface Vehicle { - - public String getType(); -} diff --git a/Java/pom.xml b/Java/pom.xml new file mode 100644 index 00000000..48ac011b --- /dev/null +++ b/Java/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.work + toll-calculator-master + 1.0-SNAPSHOT + + + 11 + 11 + + + + + org.json + json + 20210307 + + + + + org.junit.jupiter + junit-jupiter-engine + 5.8.1 + test + + + + + maven-unit-test + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.0 + + + + + + \ No newline at end of file diff --git a/Java/src/main/java/com/work/TollCalculator.java b/Java/src/main/java/com/work/TollCalculator.java new file mode 100644 index 00000000..8f4347d0 --- /dev/null +++ b/Java/src/main/java/com/work/TollCalculator.java @@ -0,0 +1,155 @@ +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 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 getFeeAndTimes() throws IOException { + List tollFeeTimesList = new ArrayList<>(); + 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); + }); + }); + return tollFeeTimesList; + } + + private int getTollFee(final Date date) throws IOException { + + if(tollFeeTimes == null){ + tollFeeTimes = getFeeAndTimes(); + } + + LocalTime localTime = LocalTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); + Optional 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"; + } + } + +} + diff --git a/Java/src/main/java/com/work/exceptions/MissingHolidayDataException.java b/Java/src/main/java/com/work/exceptions/MissingHolidayDataException.java new file mode 100644 index 00000000..de2e28a8 --- /dev/null +++ b/Java/src/main/java/com/work/exceptions/MissingHolidayDataException.java @@ -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); + } +} diff --git a/Java/src/main/java/com/work/model/TollFeeTime.java b/Java/src/main/java/com/work/model/TollFeeTime.java new file mode 100644 index 00000000..fb5b024e --- /dev/null +++ b/Java/src/main/java/com/work/model/TollFeeTime.java @@ -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; + } + +} diff --git a/Java/Car.java b/Java/src/main/java/com/work/model/vehicles/Car.java similarity index 50% rename from Java/Car.java rename to Java/src/main/java/com/work/model/vehicles/Car.java index e0ec832c..7c39872c 100644 --- a/Java/Car.java +++ b/Java/src/main/java/com/work/model/vehicles/Car.java @@ -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; + } +} diff --git a/Java/src/main/java/com/work/model/vehicles/Diplomat.java b/Java/src/main/java/com/work/model/vehicles/Diplomat.java new file mode 100644 index 00000000..fc0aa257 --- /dev/null +++ b/Java/src/main/java/com/work/model/vehicles/Diplomat.java @@ -0,0 +1,15 @@ +package com.work.model.vehicles; + +public class Diplomat implements Vehicle { + @Override + public String getType() { + return "Diplomat"; + } + + @Override + public boolean isTollFree() { + return true; + } + + +} diff --git a/Java/src/main/java/com/work/model/vehicles/Emergency.java b/Java/src/main/java/com/work/model/vehicles/Emergency.java new file mode 100644 index 00000000..d92a89a2 --- /dev/null +++ b/Java/src/main/java/com/work/model/vehicles/Emergency.java @@ -0,0 +1,15 @@ +package com.work.model.vehicles; + +public class Emergency implements Vehicle { + @Override + public String getType() { + return "Emergency"; + } + + @Override + public boolean isTollFree() { + return true; + } + + +} diff --git a/Java/src/main/java/com/work/model/vehicles/Foreign.java b/Java/src/main/java/com/work/model/vehicles/Foreign.java new file mode 100644 index 00000000..f0966ac2 --- /dev/null +++ b/Java/src/main/java/com/work/model/vehicles/Foreign.java @@ -0,0 +1,15 @@ +package com.work.model.vehicles; + +public class Foreign implements Vehicle { + @Override + public String getType() { + return "Foreign"; + } + + @Override + public boolean isTollFree() { + return true; + } + + +} diff --git a/Java/src/main/java/com/work/model/vehicles/Military.java b/Java/src/main/java/com/work/model/vehicles/Military.java new file mode 100644 index 00000000..606fb9d3 --- /dev/null +++ b/Java/src/main/java/com/work/model/vehicles/Military.java @@ -0,0 +1,15 @@ +package com.work.model.vehicles; + +public class Military implements Vehicle { + @Override + public String getType() { + return "Military"; + } + + @Override + public boolean isTollFree() { + return true; + } + + +} diff --git a/Java/Motorbike.java b/Java/src/main/java/com/work/model/vehicles/Motorbike.java similarity index 53% rename from Java/Motorbike.java rename to Java/src/main/java/com/work/model/vehicles/Motorbike.java index 14056ff9..ed938eb9 100644 --- a/Java/Motorbike.java +++ b/Java/src/main/java/com/work/model/vehicles/Motorbike.java @@ -1,7 +1,13 @@ - -public class Motorbike implements Vehicle { - @Override - public String getType() { - return "Motorbike"; - } -} +package com.work.model.vehicles; + +public class Motorbike implements Vehicle { + @Override + public String getType() { + return "Motorbike"; + } + + @Override + public boolean isTollFree() { + return true; + } +} diff --git a/Java/src/main/java/com/work/model/vehicles/Tractor.java b/Java/src/main/java/com/work/model/vehicles/Tractor.java new file mode 100644 index 00000000..05b8f66c --- /dev/null +++ b/Java/src/main/java/com/work/model/vehicles/Tractor.java @@ -0,0 +1,15 @@ +package com.work.model.vehicles; + +public class Tractor implements Vehicle { + @Override + public String getType() { + return "Tractor"; + } + + @Override + public boolean isTollFree() { + return true; + } + + +} diff --git a/Java/src/main/java/com/work/model/vehicles/Vehicle.java b/Java/src/main/java/com/work/model/vehicles/Vehicle.java new file mode 100644 index 00000000..380980fd --- /dev/null +++ b/Java/src/main/java/com/work/model/vehicles/Vehicle.java @@ -0,0 +1,7 @@ +package com.work.model.vehicles; + +public interface Vehicle { + + public String getType(); + public boolean isTollFree(); +} diff --git a/Java/src/main/resources/Holidays.json b/Java/src/main/resources/Holidays.json new file mode 100644 index 00000000..35963906 --- /dev/null +++ b/Java/src/main/resources/Holidays.json @@ -0,0 +1,18 @@ +{ + "2021": { + "January": "1,6", + "April": "2,4,5", + "May": "1,13,23", + "June": "6,26", + "November": "6", + "December": "24,25,26,31" + }, + "2022": { + "January": "1,6", + "April": "15,17,18", + "May": "1,26", + "June": "5,6,25", + "November": "5", + "December": "24,25,26,31" + } +} \ No newline at end of file diff --git a/Java/src/main/resources/TollFees.json b/Java/src/main/resources/TollFees.json new file mode 100644 index 00000000..7b510ae5 --- /dev/null +++ b/Java/src/main/resources/TollFees.json @@ -0,0 +1,55 @@ +{ + "feeTimes": [ + { + "fee": 18, + "time": [ + { + "startTime": "07:00:00", + "endTime": "07:59:00" + }, + { + "startTime": "15:30:00", + "endTime": "16:59:00" + } + ] + }, + { + "fee": 13, + "time": [ + { + "startTime": "06:30:00", + "endTime": "06:59:00" + }, + { + "startTime": "08:00:00", + "endTime": "08:29:00" + }, + { + "startTime": "15:00:00", + "endTime": "15:29:00" + }, + { + "startTime": "17:00:00", + "endTime": "17:59:00" + } + ] + }, + { + "fee": 8, + "time": [ + { + "startTime": "06:00:00", + "endTime": "06:29:00" + }, + { + "startTime": "08:30:00", + "endTime": "14:59:00" + }, + { + "startTime": "18:00:00", + "endTime": "18:29:00" + } + ] + } + ] +} \ No newline at end of file diff --git a/Java/src/package-info.java b/Java/src/package-info.java new file mode 100644 index 00000000..d740a19f --- /dev/null +++ b/Java/src/package-info.java @@ -0,0 +1 @@ +package src; \ No newline at end of file diff --git a/Java/src/test/java/com/work/TollCalculatorTest.java b/Java/src/test/java/com/work/TollCalculatorTest.java new file mode 100644 index 00000000..1b02c373 --- /dev/null +++ b/Java/src/test/java/com/work/TollCalculatorTest.java @@ -0,0 +1,110 @@ +package com.work; + +import com.work.exceptions.MissingHolidayDataException; +import com.work.model.vehicles.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.text.SimpleDateFormat; +import java.time.Instant; +import java.util.Date; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class TollCalculatorTest { + + TollCalculator tc; + Car car ; + + public TollCalculatorTest() { + tc = new TollCalculator(); + car = new Car(); + + } + + @Test + public void testWithNoDates() throws Exception { + Assertions.assertEquals(0,tc.getTollFee(car)); + } + + @Test + public void testTollFreeVehicles() throws Exception { + Assertions.assertEquals(0,tc.getTollFee(new Motorbike(), Date.from(Instant.now()))); + Assertions.assertEquals(0,tc.getTollFee(new Emergency(), Date.from(Instant.now()))); + Assertions.assertEquals(0,tc.getTollFee(new Diplomat(), Date.from(Instant.now()))); + Assertions.assertEquals(0,tc.getTollFee(new Tractor(), Date.from(Instant.now()))); + Assertions.assertEquals(0,tc.getTollFee(new Military(), Date.from(Instant.now()))); + Assertions.assertEquals(0,tc.getTollFee(new Foreign(), Date.from(Instant.now()))); + + } + + @Test + public void testWeekends() throws Exception { + Assertions.assertEquals(0,tc.getTollFee(car, Date.from(Instant.now()))); + } + + @Test + public void testHolidays() throws Exception { + Assertions.assertEquals(0,tc.getTollFee(car, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-01-01 10:00:00"))); + } + + @Test + public void testSingleDate() throws Exception { + Assertions.assertEquals(8,tc.getTollFee(car, new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 10:00:00"))); + } + + @Test + public void testMultipleTimesInHour() throws Exception { + Assertions.assertEquals(18,tc.getTollFee(car, + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 15:00:00"), + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 15:10: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 15:45:00"), + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 16:00:00"))); + } + + @Test + public void testMultipleTimesInConsecutiveHours() throws Exception { + Assertions.assertEquals(36,tc.getTollFee(car, + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 15:00:00"), + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 15:15: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 15:45: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 16:15: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:45:00"))); + } + + @Test + public void testFreePaidHourCombination() throws Exception { + Assertions.assertEquals(18,tc.getTollFee(car, + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 15:00:00"), + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 15:10: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 15:45:00"), + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 06:45:00"), + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 18:45:00"), + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2021/11/18 16:00:00"))); + } + + @Test + public void testMaximumPricePerDay() throws Exception { + Assertions.assertEquals(60,tc.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"))); + } + + @Test + public void testMissingHolidayList() { + assertThrows(MissingHolidayDataException.class, () -> + tc.getTollFee(car, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2023-05-08 10:00:00"))); + } +} From 16ccacb50402679eebf0a44ef64f99e9dc2d13cc Mon Sep 17 00:00:00 2001 From: Tamilarasan Date: Sat, 20 Nov 2021 23:48:10 +0100 Subject: [PATCH 2/2] Added exception handling --- .../main/java/com/work/TollCalculator.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/Java/src/main/java/com/work/TollCalculator.java b/Java/src/main/java/com/work/TollCalculator.java index 8f4347d0..f425ac27 100644 --- a/Java/src/main/java/com/work/TollCalculator.java +++ b/Java/src/main/java/com/work/TollCalculator.java @@ -72,20 +72,25 @@ public int getTollFee(Vehicle vehicle, Date... dates) throws MissingHolidayDataE private List getFeeAndTimes() throws IOException { List tollFeeTimesList = new ArrayList<>(); - 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); + 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; }