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

Implemented toll calculator using Node Js #105

Open
wants to merge 1 commit 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
20 changes: 20 additions & 0 deletions constants/tollAmount.js
Original file line number Diff line number Diff line change
@@ -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);
43 changes: 43 additions & 0 deletions constants/tollFreeDates.js
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 2 additions & 0 deletions constants/tollFreeDays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const tollFreeDays = ["Saturday", "Sunday"];
module.exports = Object.freeze(tollFreeDays);
10 changes: 10 additions & 0 deletions constants/tollFreeVehicle.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);
22 changes: 22 additions & 0 deletions dates.js
Original file line number Diff line number Diff line change
@@ -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
}
210 changes: 210 additions & 0 deletions test/tollAmountForDates.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
79 changes: 79 additions & 0 deletions test/tollAmountForMultipleTimes.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

});
Loading