From 0dcac302ef22bd599f86795a7595c6f9c6428427 Mon Sep 17 00:00:00 2001 From: sreekutty karamil <82576672+sreekuttyk@users.noreply.github.com> Date: Mon, 21 Mar 2022 10:08:04 +0100 Subject: [PATCH] Add files via upload --- constants/tollAmount.js | 20 +++ constants/tollFreeDates.js | 43 +++++ constants/tollFreeDays.js | 2 + constants/tollFreeVehicle.js | 10 ++ dates.js | 22 +++ test/tollAmountForDates.test.js | 210 ++++++++++++++++++++++++ test/tollAmountForMultipleTimes.test.js | 79 +++++++++ test/tollFreeDays.test.js | 163 ++++++++++++++++++ test/tollFreeVehicles.test.js | 72 ++++++++ tollAmount.js | 19 +++ tollCalculator.js | 79 +++++++++ vehicle.js | 11 ++ 12 files changed, 730 insertions(+) create mode 100644 constants/tollAmount.js create mode 100644 constants/tollFreeDates.js create mode 100644 constants/tollFreeDays.js create mode 100644 constants/tollFreeVehicle.js create mode 100644 dates.js create mode 100644 test/tollAmountForDates.test.js create mode 100644 test/tollAmountForMultipleTimes.test.js create mode 100644 test/tollFreeDays.test.js create mode 100644 test/tollFreeVehicles.test.js create mode 100644 tollAmount.js create mode 100644 tollCalculator.js create mode 100644 vehicle.js diff --git a/constants/tollAmount.js b/constants/tollAmount.js new file mode 100644 index 00000000..cf9b8e6f --- /dev/null +++ b/constants/tollAmount.js @@ -0,0 +1,20 @@ +const tollAmount = require('../tollAmount'); + +var tollAmountByHours = { + + 6: Object.assign({}, ...tollAmount.setTollForHalfHour(8), ...tollAmount.setTollForHalfHour(13, 30)), + 7: Object.assign({}, ...tollAmount.setTollForOneHour(18)), + 8: Object.assign({}, ...tollAmount.setTollForHalfHour(13), ...tollAmount.setTollForHalfHour(8, 30)), + 9: Object.assign({}, ...tollAmount.setTollForHalfHour(8, 30)), + 10: Object.assign({}, ...tollAmount.setTollForHalfHour(8, 30)), + 11: Object.assign({}, ...tollAmount.setTollForHalfHour(8, 30)), + 12: Object.assign({}, ...tollAmount.setTollForHalfHour(8, 30)), + 13: Object.assign({}, ...tollAmount.setTollForHalfHour(8, 30)), + 14: Object.assign({}, ...tollAmount.setTollForHalfHour(8, 30)), + 15: Object.assign({}, ...tollAmount.setTollForHalfHour(13), ...tollAmount.setTollForHalfHour(18, 30)), + 16: Object.assign({}, ...tollAmount.setTollForOneHour(18)), + 17: Object.assign({}, ...tollAmount.setTollForOneHour(13)), + 18: Object.assign({}, ...tollAmount.setTollForHalfHour(8), ...tollAmount.setTollForHalfHour(0, 30)), +}; + +module.exports = Object.freeze(tollAmountByHours); \ No newline at end of file diff --git a/constants/tollFreeDates.js b/constants/tollFreeDates.js new file mode 100644 index 00000000..13a1c460 --- /dev/null +++ b/constants/tollFreeDates.js @@ -0,0 +1,43 @@ +const tollFreeDates = { + 2013: { + January: { + 1: true, + }, + March: { + 28: true, + 29: true, + }, + April: { + 1: true, + 30: true, + }, + May: { + 1: true, + 8: true, + 9: true, + }, + June: { + 1: true, + 8: true, + 9: true, + }, + July: { + ...Array.from(Array(31)) + .map((e, i) => i + 1) + .map((x) => ({ [x]: true })), + }, + November: { + 1: true, + }, + December: { + 24: true, + 25: true, + 26: true, + 31: true, + }, + }, + +}; + + +module.exports = Object.freeze(tollFreeDates); \ No newline at end of file diff --git a/constants/tollFreeDays.js b/constants/tollFreeDays.js new file mode 100644 index 00000000..a1cfb89f --- /dev/null +++ b/constants/tollFreeDays.js @@ -0,0 +1,2 @@ +const tollFreeDays = ["Saturday", "Sunday"]; +module.exports = Object.freeze(tollFreeDays); \ No newline at end of file diff --git a/constants/tollFreeVehicle.js b/constants/tollFreeVehicle.js new file mode 100644 index 00000000..c038ba89 --- /dev/null +++ b/constants/tollFreeVehicle.js @@ -0,0 +1,10 @@ +const TollFreeVehicles = [ + "Motorbike", + "Tractor", + "Emergency", + "Diplomat", + "Foreign", + "Military", +]; + +module.exports = Object.freeze(TollFreeVehicles); \ No newline at end of file diff --git a/dates.js b/dates.js new file mode 100644 index 00000000..b8134391 --- /dev/null +++ b/dates.js @@ -0,0 +1,22 @@ +const moment = require('moment'); + + +const sortDatesByAscOrder = (dates) => { + return dates.sort((a, b) => { + return moment(a).diff(b); + }) +} + + +const getMinutesDifferenceBetweenDates = (date1, date2) => { + return moment(date1, "DD/MM/YYYY HH:mm:ss").diff( + moment(date2, "DD/MM/YYYY HH:mm:ss") + ) / 60000; +} + + + +module.exports = { + sortDatesByAscOrder: sortDatesByAscOrder, + getMinutesDifferenceBetweenDates: getMinutesDifferenceBetweenDates +} \ No newline at end of file diff --git a/test/tollAmountForDates.test.js b/test/tollAmountForDates.test.js new file mode 100644 index 00000000..a8912fd3 --- /dev/null +++ b/test/tollAmountForDates.test.js @@ -0,0 +1,210 @@ +const TollCalculator = require("../tollCalculator"); +const Vehicle = require("../vehicle"); +let tollCalculator; + +describe("Getting the toll amount for date and time", () => { + beforeAll(() => { + tollCalculator = new TollCalculator(); + }); + + it("Toll fee between 0:00 - 5:59 = 0", () => { + const carToll = tollCalculator.getTollFee( + new Date("2013-01-11T02:07:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-01-11T04:07:00"), + new Vehicle("Bike") + ); + const scooterToll = tollCalculator.getTollFee( + new Date("2013-01-11T00:00:00"), + new Vehicle("Scooter") + ); + expect(carToll).toBe(0); + expect(bikeToll).toBe(0); + expect(scooterToll).toBe(0); + }); + + it("Toll fee between 6:00 - 6:29 = 8", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T06:07:00"), + new Vehicle("Car") + ); + expect(toll).toBe(8); + }); + + it("Toll fee between 6:30 - 6:59 = 13", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T06:30:00"), + new Vehicle("Car") + ); + expect(toll).toBe(13); + }); + + it("Toll fee between 7:00 - 7:59 = 18", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T07:22:00"), + new Vehicle("Car") + ); + expect(toll).toBe(18); + }); + + it("Toll fee between 8:00 - 7:29 = 13", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T08:11:00"), + new Vehicle("Car") + ); + expect(toll).toBe(13); + }); + + it("Toll fee between 8:30 - 8:59 = 8", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T08:34:00"), + new Vehicle("Car") + ); + expect(toll).toBe(8); + }); + + it("Toll fee between 9:30 - 9:59 = 8", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T09:34:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-01-11T09:05:00"), + new Vehicle("Bike") + ); + expect(toll).toBe(8); + expect(bikeToll).toBe(0); + }); + + it("Toll fee between 10:30 - 10:59 = 8", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T10:34:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-01-11T10:05:00"), + new Vehicle("Bike") + ); + expect(toll).toBe(8); + expect(bikeToll).toBe(0); + }); + + it("Toll fee between 11:30 - 11:59 = 8", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T11:34:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-01-11T11:05:00"), + new Vehicle("Bike") + ); + expect(toll).toBe(8); + expect(bikeToll).toBe(0); + }); + + it("Toll fee between 12:30 - 12:59 = 8", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T12:34:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-01-11T12:05:00"), + new Vehicle("Bike") + ); + expect(toll).toBe(8); + expect(bikeToll).toBe(0); + }); + + it("Toll fee between 13:30 - 13:59 = 8", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T13:34:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-01-11T13:05:00"), + new Vehicle("Bike") + ); + expect(toll).toBe(8); + expect(bikeToll).toBe(0); + }); + + it("Toll fee between 14:30 - 14:59 = 8", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T14:34:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-01-11T14:05:00"), + new Vehicle("Bike") + ); + expect(toll).toBe(8); + expect(bikeToll).toBe(0); + }); + + it("Toll fee between 15:00 - 15:29 = 13", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T15:05:00"), + new Vehicle("Car") + ); + expect(toll).toBe(13); + }); + + it("Toll fee between 15:30 - 16:59 = 18", () => { + const carToll = tollCalculator.getTollFee( + new Date("2013-01-11T15:48:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-01-11T16:22:00"), + new Vehicle("Bike") + ); + expect(carToll).toBe(18); + expect(bikeToll).toBe(18); + }); + + it("Toll fee between 17:00 - 17:59 = 13", () => { + const carToll = tollCalculator.getTollFee( + new Date("2013-01-11T17:48:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-01-11T17:22:00"), + new Vehicle("Bike") + ); + expect(carToll).toBe(13); + expect(bikeToll).toBe(13); + }); + + it("Toll fee between 18:00 - 18:29 = 8", () => { + const carToll = tollCalculator.getTollFee( + new Date("2013-01-11T18:29:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-01-11T18:30:00"), + new Vehicle("Bike") + ); + expect(carToll).toBe(8); + expect(bikeToll).toBe(0); + }); + + it("Toll fee between 19:00 - 23:59 = 0", () => { + const carToll = tollCalculator.getTollFee( + new Date("2013-01-11T19:29:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-01-11T20:55:00"), + new Vehicle("Bike") + ); + const scooterToll = tollCalculator.getTollFee( + new Date("2013-01-11T23:55:00"), + new Vehicle("Bike") + ); + expect(carToll).toBe(0); + expect(bikeToll).toBe(0); + expect(scooterToll).toBe(0); + }); +}); diff --git a/test/tollAmountForMultipleTimes.test.js b/test/tollAmountForMultipleTimes.test.js new file mode 100644 index 00000000..d0991b65 --- /dev/null +++ b/test/tollAmountForMultipleTimes.test.js @@ -0,0 +1,79 @@ +const TollCalculator = require("../tollCalculator"); +const Vehicle = require("../vehicle"); +let tollCalculator; + +describe("Calculate toll amount for multiple dates", () => { + beforeAll(() => { + tollCalculator = new TollCalculator(); + }); + + + it("Gets the toll amount with unsorted dates", () => { + const toll = tollCalculator.getTollFee(new Vehicle("Car"), [ + new Date("2013-01-11T16:45:00"), + new Date("2013-01-11T15:07:00"), + ]); + expect(toll).toBe(31); + }); + + + + it("Gets the toll amount with mixed dates", () => { + const toll = tollCalculator.getTollFee(new Vehicle("Car"), [ + new Date("2013-01-11T16:45:00"), + new Date("2013-01-11T15:45:00"), + new Date("2013-01-11T15:07:00"), + ]); + expect(toll).toBe(36); + }); + + + + it("Gets the toll amount for dates more than an hour", () => { + const toll = tollCalculator.getTollFee(new Vehicle("Car"), [ + new Date("2013-01-11T15:07:00"), + new Date("2013-01-11T16:45:00"), + ]); + expect(toll).toBe(31); + }); + + it("Gets toll amount for dates with multiple fees within an hour which gets the largest one ", () => { + const toll = tollCalculator.getTollFee(new Vehicle("Car"), [ + new Date("2013-01-11T15:07:00"), + new Date("2013-01-11T15:45:00"), + ]); + expect(toll).toBe(18); + }); + + + + it("Gets toll amount for dates with multiple fees of within an hour and more than an hour", () => { + const toll = tollCalculator.getTollFee(new Vehicle("Car"), [ + new Date("2013-01-11T15:07:00"), + new Date("2013-01-11T15:45:00"), + new Date("2013-01-11T16:25:00") + ]); + expect(toll).toBe(36); + }); + + it("Limit toll amount for maximum 60SEK per day", () => { + const toll = tollCalculator.getTollFee(new Vehicle("Car"), [ + new Date("2013-01-11T06:03:00"), + new Date("2013-01-11T07:10:00"), + new Date("2013-01-11T08:07:00"), + new Date("2013-01-11T09:45:00"), + new Date("2013-01-11T15:45:00"), + new Date("2013-01-11T16:25:00") + ]); + expect(toll).toBe(60); + }); + it("get toll amount for different dates", () => { + const toll = tollCalculator.getTollFee(new Vehicle("Car"), [ + new Date("2013-01-11T06:03:00"), + new Date("2013-01-12T07:10:40"), + + ]); + expect(toll).toBe(8); + }); + +}); \ No newline at end of file diff --git a/test/tollFreeDays.test.js b/test/tollFreeDays.test.js new file mode 100644 index 00000000..e943d2c1 --- /dev/null +++ b/test/tollFreeDays.test.js @@ -0,0 +1,163 @@ +const TollCalculator = require("../tollCalculator"); +const Vehicle = require("../vehicle"); +let tollCalculator; + +describe("Toll free dates", () => { + beforeAll(() => { + tollCalculator = new TollCalculator(); + }); + + it("New year day is toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-01T15:48:00"), + new Vehicle("Car") + ); + expect(toll).toBe(0); + }); + + it("February is not toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-02-01T15:48:00"), + new Vehicle("Car") + ); + expect(toll).not.toBe(0); + }); + + it("Mar 28th and 29th is toll free", () => { + const carToll = tollCalculator.getTollFee( + new Date("2013-03-29T00:48:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-03-29T15:48:00"), + new Vehicle("Bike") + ); + expect(carToll).toBe(0); + expect(bikeToll).toBe(0); + }); + + it("Apr 1st and 30th is toll free", () => { + const carToll = tollCalculator.getTollFee( + new Date("2013-04-01T00:48:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-04-30T15:48:00"), + new Vehicle("Bike") + ); + expect(carToll).toBe(0); + expect(bikeToll).toBe(0); + }); + + it("May 1st, 8th and 9th is toll free", () => { + const carToll = tollCalculator.getTollFee( + new Date("2013-05-01T00:48:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-05-08T15:48:00"), + new Vehicle("Bike") + ); + const scooterToll = tollCalculator.getTollFee( + new Date("2013-05-09T18:48:00"), + new Vehicle("Scooter") + ); + expect(carToll).toBe(0); + expect(bikeToll).toBe(0); + expect(scooterToll).toBe(0); + }); + + it("June 1st, 8th and 9th is toll free", () => { + const carToll = tollCalculator.getTollFee( + new Date("2013-06-01T00:48:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-06-08T15:48:00"), + new Vehicle("Bike") + ); + const scooterToll = tollCalculator.getTollFee( + new Date("2013-06-09T18:48:00"), + new Vehicle("Scooter") + ); + expect(carToll).toBe(0); + expect(bikeToll).toBe(0); + expect(scooterToll).toBe(0); + }); + + it("July month is toll free", () => { + const carToll = tollCalculator.getTollFee( + new Date("2013-07-01T00:48:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-07-15T15:48:00"), + new Vehicle("Bike") + ); + const scooterToll = tollCalculator.getTollFee( + new Date("2013-07-29T15:48:00"), + new Vehicle("Scooter") + ); + expect(carToll).toBe(0); + expect(bikeToll).toBe(0); + expect(scooterToll).toBe(0); + }); + + it("August is not toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-08-22T15:48:00"), + new Vehicle("Car") + ); + expect(toll).not.toBe(0); + }); + + it("September is not toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-09-06T15:48:00"), + new Vehicle("Car") + ); + expect(toll).not.toBe(0); + }); + + it("October is not toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-10-10T15:48:00"), + new Vehicle("Car") + ); + expect(toll).not.toBe(0); + }); + + it("November 1st is toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-11-01T15:48:00"), + new Vehicle("Car") + ); + expect(toll).toBe(0); + }); + + it("Christmas holidays are toll free", () => { + const carToll = tollCalculator.getTollFee( + new Date("2013-12-25T15:48:00"), + new Vehicle("Car") + ); + const bikeToll = tollCalculator.getTollFee( + new Date("2013-12-24T15:48:00"), + new Vehicle("Bike") + ); + const scooterToll = tollCalculator.getTollFee( + new Date("2013-12-26T15:48:00"), + new Vehicle("Scooter") + ); + expect(carToll).toBe(0); + expect(bikeToll).toBe(0); + expect(scooterToll).toBe(0); + }); + + it("December 31st is toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-12-31T15:48:00"), + new Vehicle("Car") + ); + expect(toll).toBe(0); + }); +}); \ No newline at end of file diff --git a/test/tollFreeVehicles.test.js b/test/tollFreeVehicles.test.js new file mode 100644 index 00000000..8629b768 --- /dev/null +++ b/test/tollFreeVehicles.test.js @@ -0,0 +1,72 @@ +const TollCalculator = require("../tollCalculator"); +const Vehicle = require("../vehicle"); +let tollCalculator; + +describe("Toll free vehicles", () => { + beforeAll(() => { + tollCalculator = new TollCalculator(); + }); + + it("Military vehicles are toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T15:48:00"), + new Vehicle("Military") + ); + expect(toll).toBe(0); + }); + + it("Tractor vehicles are toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T15:48:00"), + new Vehicle("Tractor") + ); + expect(toll).toBe(0); + }); + + it("Motorbike vehicles are toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T15:48:00"), + new Vehicle("Motorbike") + ); + expect(toll).toBe(0); + }); + + it("Emergency vehicles are toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T15:48:00"), + new Vehicle("Emergency") + ); + expect(toll).toBe(0); + }); + + it("Diplomat vehicles are toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T15:48:00"), + new Vehicle("Diplomat") + ); + expect(toll).toBe(0); + }); + + it("Foreign vehicles are toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T15:48:00"), + new Vehicle("Foreign") + ); + expect(toll).toBe(0); + }); + + it("Van is not toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T15:48:00"), + new Vehicle("Van") + ); + expect(toll).toBe(18); + }); + it("Car is not toll free", () => { + const toll = tollCalculator.getTollFee( + new Date("2013-01-11T15:48:00"), + new Vehicle("car") + ); + expect(toll).toBe(18); + }); +}); \ No newline at end of file diff --git a/tollAmount.js b/tollAmount.js new file mode 100644 index 00000000..6a448474 --- /dev/null +++ b/tollAmount.js @@ -0,0 +1,19 @@ + +const setTollForOneHour = (tollAmount) => { + return Array.from(Array(60)) + .map((_, index) => index) + .map((minute) => ({ [minute]: tollAmount })); +}; + + +const setTollForHalfHour = (tollAmount, offset = 0) => { + return Array.from(Array(30)) + .map((_, index) => index + offset) + .map((minute) => ({ [minute]: tollAmount })); +}; + + +module.exports = { + setTollForOneHour: setTollForOneHour, + setTollForHalfHour: setTollForHalfHour +} \ No newline at end of file diff --git a/tollCalculator.js b/tollCalculator.js new file mode 100644 index 00000000..6dc5d3e5 --- /dev/null +++ b/tollCalculator.js @@ -0,0 +1,79 @@ +const moment = require("moment"); +const Vehicle = require("./vehicle"); +const TollFreeVehicles = require("./constants/tollFreeVehicle"); +const tollFreeDates = require("./constants/tollFreeDates"); +const tollFreeDays = require("./constants/tollFreeDays"); +const tollAmount = require("./constants/tollAmount"); +const datesHelpers = require("./dates") + +class TollCalculator { + getTollFee(dateOrVehicle, datesOrVehicle) { + if (dateOrVehicle instanceof Date && datesOrVehicle instanceof Vehicle) { + return this.getTollFeeForDate(dateOrVehicle, datesOrVehicle); + } else if ( + dateOrVehicle instanceof Vehicle && + Array.isArray(datesOrVehicle) && + datesOrVehicle.every((date) => date instanceof Date) + ) { + return this.getTotalTollFee(dateOrVehicle, datesOrVehicle); + } else { + throw "Cannot calculate toll:Invalid args"; + } + } + + getTotalTollFee(vehicle, dates) { + let totalFee = 0; + const sortedDates = datesHelpers.sortDatesByAscOrder(dates); + let intervalStart = sortedDates[0]; + let tempFee = this.getTollFee(intervalStart, vehicle); + for (const date of sortedDates) { + const nextFee = this.getTollFee(date, vehicle); + let minutes = datesHelpers.getMinutesDifferenceBetweenDates(date, intervalStart); + if (minutes <= 60) { + if (totalFee > 0) totalFee -= tempFee; + if (nextFee >= tempFee) tempFee = nextFee; + totalFee += tempFee; + tempFee = nextFee; + } else { + totalFee += nextFee; + intervalStart = date; + } + } + if (totalFee > 60) totalFee = 60; + return totalFee; + } + + getTollFeeForDate(date, vehicle) { + if (this.isTollFreeDate(date) || this.isTollFreeVehicle(vehicle)) return 0; + const hour = Math.abs(moment(date).format("HH")); + const minute = Math.abs(moment(date).format("mm")); + return tollAmount[hour] ? tollAmount[hour][minute] || 0 : 0; + } + + isTollFreeDate(date) { + const year = moment(date).format("YYYY"); + const month = moment(date).format("MMMM"); + const day = Math.abs(moment(date).format("DD")); + const dayOfWeek = moment(date).format("dddd"); + + if (tollFreeDays.includes(dayOfWeek)) return true; + + if ( + tollFreeDates[year] && + tollFreeDates[year][month] && + tollFreeDates[year][month][day] + ) + return true; + + return false; + } + + isTollFreeVehicle(vehicle) { + if (vehicle == null) return false; + const vehicleType = vehicle.getType(); + return TollFreeVehicles.includes(vehicleType); + } +} + + +module.exports = TollCalculator; \ No newline at end of file diff --git a/vehicle.js b/vehicle.js new file mode 100644 index 00000000..52638e64 --- /dev/null +++ b/vehicle.js @@ -0,0 +1,11 @@ +class Vehicle { + constructor(type) { + this.type = type; + } + + getType() { + return this.type; + } + } + + module.exports = Vehicle; \ No newline at end of file