Skip to content

Instantly share code, notes, and snippets.

@shadeglare
Created December 4, 2013 13:41
Show Gist options
  • Save shadeglare/7787569 to your computer and use it in GitHub Desktop.
Save shadeglare/7787569 to your computer and use it in GitHub Desktop.
var Railway = Railway || {};
(function(Railway) {
var Train = function(settings) {
settings = settings || {};
this.mainCoupeSize = settings.mainCoupeSize || 4;
this.leftCoupeSize = settings.leftCoupeSize || 2;
this.coupeCount = settings.coupeCount || 9;
this.totalMainSeatsOffset = this.coupeCount * this.mainCoupeSize;
this.seatToCoupes = {};
this._calculateSeatMap();
};
Train.prototype._calculateSeatMap = function() {
this.seatToCoupes = {};
for (var currentCoupe = 1; currentCoupe <= this.coupeCount; currentCoupe++)
{
for (var offsetMainSeatNumber = 1; offsetMainSeatNumber <= this.mainCoupeSize; offsetMainSeatNumber++)
{
var currentMainSeatNumber = (currentCoupe - 1) * this.mainCoupeSize + offsetMainSeatNumber;
this.seatToCoupes[currentMainSeatNumber] = currentCoupe;
}
for (var offsetLeftSeatNumber = 1; offsetLeftSeatNumber <= this.leftCoupeSize; offsetLeftSeatNumber++)
{
var currentLeftSeatNumber =
(this.coupeCount - currentCoupe) * this.leftCoupeSize +
offsetLeftSeatNumber +
this.totalMainSeatsOffset;
this.seatToCoupes[currentLeftSeatNumber] = currentCoupe;
}
}
};
Train.prototype.getCoupeBySeat = function(seatNumber) {
if (!this.seatToCoupes.hasOwnProperty(seatNumber)) {
throw new Error("Invalid seat number");
} else {
return this.seatToCoupes[seatNumber];
}
};
if (module) {
module.exports = {
Train: Train
};
} else {
Railway = {
Train: Train
};
}
})(Railway);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment