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

Toll Fee calculation done in Node Js #100

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
7 changes: 7 additions & 0 deletions Node Js/car.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Car {
getType() {
return "Car";
}
}

module.exports = Car;
20 changes: 20 additions & 0 deletions Node Js/constants/toll-amount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const tollAmountHelpers = require('../helpers/toll-amount')

//Setting the toll Amount Dynamically
const tollAmountByHours = {
6: Object.assign({}, ...tollAmountHelpers.setTollForHalfHour(8), ...tollAmountHelpers.setTollForHalfHour(13, 30)),
7: Object.assign({}, ...tollAmountHelpers.setTollForOneHour(18)),
8: Object.assign({}, ...tollAmountHelpers.setTollForHalfHour(13), ...tollAmountHelpers.setTollForHalfHour(8, 30)),
9: Object.assign({}, ...tollAmountHelpers.setTollForHalfHour(8, 30)),
10: Object.assign({}, ...tollAmountHelpers.setTollForHalfHour(8, 30)),
11: Object.assign({}, ...tollAmountHelpers.setTollForHalfHour(8, 30)),
12: Object.assign({}, ...tollAmountHelpers.setTollForHalfHour(8, 30)),
13: Object.assign({}, ...tollAmountHelpers.setTollForHalfHour(8, 30)),
14: Object.assign({}, ...tollAmountHelpers.setTollForHalfHour(8, 30)),
15: Object.assign({}, ...tollAmountHelpers.setTollForHalfHour(13), ...tollAmountHelpers.setTollForHalfHour(18, 30)),
16: Object.assign({}, ...tollAmountHelpers.setTollForOneHour(18)),
17: Object.assign({}, ...tollAmountHelpers.setTollForOneHour(13)),
18: Object.assign({}, ...tollAmountHelpers.setTollForHalfHour(8), ...tollAmountHelpers.setTollForHalfHour(0, 30)),
};

module.exports = Object.freeze(tollAmountByHours);
41 changes: 41 additions & 0 deletions Node Js/constants/toll-free-dates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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);
3 changes: 3 additions & 0 deletions Node Js/constants/toll-free-days.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const tollFreeDays = ["Saturday", "Sunday"];

module.exports = Object.freeze(tollFreeDays);
10 changes: 10 additions & 0 deletions Node Js/constants/toll-free-vehicles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const TollFreeVehicles = [
"Motorbike",
"Tractor",
"Emergency",
"Diplomat",
"Foreign",
"Military",
];

module.exports = Object.freeze(TollFreeVehicles);
29 changes: 29 additions & 0 deletions Node Js/helpers/dates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const moment = require('moment');

/*
* @param {dates} dates of indifferent order
* @returns {number} the sorted dates in ascending order
*/
const sortDatesByAscOrder = (dates) => {
return dates.sort((a, b) => {
return moment(a).diff(b);
})
}

/*
* @param {date1} date
* @param {date2} date
* @returns {number} the difference in minutes between the dates
*/
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
}
26 changes: 26 additions & 0 deletions Node Js/helpers/toll-amount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* @param {tollAmount} tollAmount
* @returns {number} the array of toll amounts within an hour
*/
const setTollForOneHour = (tollAmount) => {
return Array.from(Array(60))
.map((_, index) => index)
.map((minute) => ({ [minute]: tollAmount }));
};

/*
* @param {tollAmount} tollAmount
* @param {offset} offset value
* @returns {number} the array of toll amounts within half an hour
*/
const setTollForHalfHour = (tollAmount, offset = 0) => {
return Array.from(Array(30))
.map((_, index) => index + offset)
.map((minute) => ({ [minute]: tollAmount }));
};


module.exports = {
setTollForOneHour: setTollForOneHour,
setTollForHalfHour: setTollForHalfHour
}
7 changes: 7 additions & 0 deletions Node Js/motor-bike.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class MotorBike {
getType() {
return "Motorbike";
}
}

module.exports = MotorBike;
17 changes: 17 additions & 0 deletions Node Js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "toll-calculator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest --verbose"
},
"author": "",
"license": "ISC",
"dependencies": {
"moment": "^2.29.1"
},
"devDependencies": {
"jest": "^27.5.1"
}
}
213 changes: 213 additions & 0 deletions Node Js/test/toll-amount-for-date.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
const TollCalculator = require("../toll-calculator");
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);
});
});



Loading