Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function processVehicleParkCommands(commands) {
  2.     'use strict';
  3.  
  4.     Object.prototype.extends = function (parent) {
  5.         this.prototype = Object.create(parent.prototype);
  6.         this.prototype.constructor = this;
  7.     };
  8.  
  9.     var Models = (function() {
  10.         var Employee = (function() {
  11.             function Employee(name, position, grade) {
  12.                 this.setName(name);
  13.                 this.setPosition(position);
  14.                 this.setGrade(grade);
  15.             }
  16.  
  17.             Employee.prototype.getName = function() {
  18.                 return this._name;
  19.             }
  20.  
  21.             Employee.prototype.setName = function(name) {
  22.                 if (name === undefined || name === "") {
  23.                     throw new Error("Name cannot be empty or undefined.");
  24.                 }
  25.                 this._name = name;
  26.             }
  27.  
  28.             Employee.prototype.getPosition = function() {
  29.                 return this._position;
  30.             }
  31.  
  32.             Employee.prototype.setPosition = function(position) {
  33.                 if (position === undefined || position === "") {
  34.                     throw new Error("Position cannot be empty or undefined.");
  35.                 }
  36.                 this._position = position;
  37.             }
  38.  
  39.             Employee.prototype.getGrade = function() {
  40.                 return this._grade;
  41.             }
  42.  
  43.             Employee.prototype.setGrade = function(grade) {
  44.                 if (grade === undefined || isNaN(grade) || grade < 0) {
  45.                     throw new Error("Grade cannot be negative.");
  46.                 }
  47.                 this._grade = grade;
  48.             }
  49.  
  50.             Employee.prototype.toString = function() {
  51.                 return " ---> " + this.getName() +
  52.                     ",position=" + this.getPosition();
  53.             }
  54.  
  55.             return Employee;
  56.         }());
  57.  
  58.  
  59.         var Vehicle = (function () {
  60.             function Vehicle(brand, age, terrainCoverage, numberOfWheels) {
  61.                 if(this.constructor === Vehicle) { //todo check if correct
  62.                     throw {
  63.                         name: 'InvalidOperationError',
  64.                         message: 'Vehicle can not be instantiated.'
  65.                     }
  66.                 }
  67.                 this.setBrand(brand);
  68.                 this.setAge(age);
  69.                 this.setTerrainCoverage(terrainCoverage);
  70.                 this.setNumberOfWheels(numberOfWheels);
  71.             }
  72.  
  73.             Vehicle.prototype.setBrand = function (value) {
  74.                 if(!value) {
  75.                     throw {
  76.                         name: 'InvalidVehicleBrandError',
  77.                         message: 'Vehicle brand should be a non-empty string.'
  78.                     }
  79.                 }
  80.  
  81.                 this._brand = value;
  82.             };
  83.  
  84.             Vehicle.prototype.getBrand = function () {
  85.                 return this._brand;
  86.             };
  87.  
  88.             Vehicle.prototype.setAge = function (value) {
  89.                 if(value < 0) {
  90.                     throw {
  91.                         name: 'InvalidVehicleAgeError',
  92.                         message: 'The age of the vehicle can not be a negative number'
  93.                     }
  94.                 }
  95.  
  96.                 this._age = value;
  97.             };
  98.  
  99.             Vehicle.prototype.getAge = function () {
  100.                 return this._age;
  101.             };
  102.  
  103.             Vehicle.prototype.setTerrainCoverage = function (value) {
  104.                 if(value.toString().toLowerCase() !== 'road' || value.toString().toLowerCase() !== 'all') {
  105.                     throw {
  106.                         name: 'InvalidVehicleTerrainCoverageError',
  107.                         message: 'The vehicle terrain coverage should be either road or all.'
  108.                     }
  109.                 }
  110.  
  111.                 this._vehicleTerrainCoverage = value;
  112.             };
  113.  
  114.             Vehicle.prototype.getTerrainCoverage = function () {
  115.                 return this._vehicleTerrainCoverage;
  116.             };
  117.  
  118.             Vehicle.prototype.setNumberOfWheels = function (value) {
  119.                 if(value < 0) {
  120.                     throw {
  121.                         name: 'InvalidVehicleNumberOfWheelsError',
  122.                         message: 'Vehicle number of wheels should always be a non-negative number'
  123.                     }
  124.                 }
  125.  
  126.                 this._vehicleNumberOfWheels = value;
  127.             };
  128.  
  129.             Vehicle.prototype.getNumberOfWheels = function () {
  130.                 return this._vehicleNumberOfWheels;
  131.             };
  132.  
  133.             Vehicle.prototype.getVehicleInfo = function() {
  134.                 return " -> " + this.constructor.name + ": " +
  135.                     "brand=" + this.getBrand() +
  136.                     ",age=" + this.getAge().toFixed(1) +
  137.                     ",terrainCoverage=" + this.getTerrainCoverage() +
  138.                     ",numberOfWheels=" + this.getNumberOfWheels();
  139.             };
  140.  
  141.             Vehicle.prototype.toString = function() {
  142.                 return this.getVehicleInfo();
  143.             };
  144.  
  145.             return Vehicle;
  146.         })();
  147.  
  148.         var Bike = (function () {
  149.  
  150.             function Bike(brand, age, terrainCoverage, numberOfWheels, frameSize, numberOfShifts) {
  151.                 Vehicle.call(this, brand, age, terrainCoverage, 2);
  152.                 this.setFrameSize(frameSize);
  153.                 if(numberOfShifts) {
  154.                     this.setNumberOfShifts(numberOfShifts);
  155.                 }
  156.             }
  157.  
  158.             Bike.extends(Vehicle);
  159.  
  160.             Bike.prototype.setFrameSize = function (value) {
  161.                 if(value < 0) {
  162.                     throw {
  163.                         name: 'InvalidBikeFrameSizeError',
  164.                         message: 'Bike frame size should always be a non-negative number.'
  165.                     }
  166.                 }
  167.  
  168.                 this._frameSize = value;
  169.             };
  170.  
  171.             Bike.prototype.getFrameSize = function () {
  172.                 return this._frameSize;
  173.             };
  174.  
  175.             Bike.prototype.setNumberOfShifts = function (value) {
  176.                 if(!value) {
  177.                     throw {
  178.                         name: 'InvalidNumberOfShiftsError',
  179.                         message: 'Bike numbers of shifts should be a non-empty string when it exists.'
  180.                     }
  181.                 }
  182.  
  183.                 this._bikeNumberOfShifts = value;
  184.             };
  185.  
  186.             Bike.prototype.getNumberOfShifts = function () {
  187.                 return this._bikeNumberOfShifts;
  188.             };
  189.  
  190.             Bike.prototype.toString = function() {
  191.                 return Vehicle.prototype.getVehicleInfo.call(this) +
  192.                     ",frameSize=" + this.getFrameSize() +
  193.                     (this.getNumberOfShifts() ? ",numberOfShifts=" + this.getNumberOfShifts() : "");
  194.             };
  195.  
  196.             return Bike;
  197.         })();
  198.  
  199.         var Automobile = (function () {
  200.  
  201.             function Automobile(brand, age, terrainCoverage, numberOfWheels, consumption, typeOfFuel) {
  202.                 if(this.constructor === Automobile) {
  203.                     throw {
  204.                         name: 'InvalidOperationError',
  205.                         message: 'Automobile can not be instantiated.'
  206.                     }
  207.                 }
  208.                 Vehicle.call(this, brand, age, terrainCoverage, numberOfWheels);
  209.                 this.setConsumption(consumption);
  210.                 this.setTypeOfFuel(typeOfFuel);
  211.             }
  212.  
  213.             Automobile.extends(Vehicle);
  214.  
  215.             Automobile.prototype.setConsumption = function (value) {
  216.                 if(value < 0) {
  217.                     throw {
  218.                         name: 'InvalidAutomobileConsumptionError',
  219.                         message: 'Automobile consumption should be a non-negative number.'
  220.                     }
  221.                 }
  222.  
  223.                 this._consumption = value;
  224.             };
  225.  
  226.             Automobile.prototype.getConsumption = function () {
  227.                 return this._consumption;
  228.             };
  229.  
  230.             Automobile.prototype.setTypeOfFuel = function (value) {
  231.                 if(!value) {
  232.                     throw {
  233.                         name: 'InvalidTypeOfFuelError',
  234.                         message: 'Automobile type of fuel should be a non-empty string.'
  235.                     }
  236.                 }
  237.  
  238.                 this._typeOfFuel = value;
  239.             };
  240.  
  241.             Automobile.prototype.getTypeOfFuel = function () {
  242.                 return this._typeOfFuel;
  243.             };
  244.  
  245.             Automobile.prototype.toString = function() {
  246.                 return Vehicle.prototype.toString.call(this) +
  247.                     ",consumption=[" + this.getConsumption() + "l/100km " + this.getTypeOfFuel() + "]";
  248.             };
  249.  
  250.             return Automobile;
  251.         })();
  252.  
  253.  
  254.         var Truck = (function () {
  255.  
  256.             function Truck(brand, age, terrainCoverage, numberOfWheels, consumption, typeOfFuel, numberOfDoors) {
  257.                 Automobile.call(this, brand, age, 'all', 4, consumption, typeOfFuel);
  258.                 this.setNumberOfDoors(numberOfDoors);
  259.             }
  260.  
  261.             Truck.extends(Automobile);
  262.  
  263.             Truck.prototype.setNumberOfDoors = function (value) {
  264.                 if(value < 0) {
  265.                     throw {
  266.                         name: 'InvalidTruckNumberOfDoorsError',
  267.                         message: 'Truck number of doors should be a non-negative number'
  268.                     }
  269.                 }
  270.  
  271.                 this._numberOfDoors = value;
  272.             };
  273.  
  274.             Truck.prototype.getNumberOfDoors = function () {
  275.                 return this._numberOfDoors;
  276.             };
  277.  
  278.             Truck.prototype.toString = function() {
  279.                 return Automobile.prototype.toString.call(this) +
  280.                     ",numberOfDoors=" + this.getNumberOfDoors();
  281.             };
  282.  
  283.             return Truck;
  284.         })();
  285.  
  286.         var Limo = (function () {
  287.  
  288.             function Limo (brand, age, terrainCoverage, numberOfWheels, consumption, typeOfFuel) {
  289.                 Automobile.call(this, brand, age, 'road', numberOfWheels, consumption, typeOfFuel);
  290.                 this._employees = [];
  291.             }
  292.  
  293.             Limo.extends(Automobile);
  294.  
  295.             Limo.prototype.appendEmployee = function (employee) {
  296.                 var index = this.getEmployees().indexOf(employee);
  297.                 if (index == -1) {
  298.                     this.getEmployees().push(employee);
  299.                 }
  300.             };
  301.  
  302.             Limo.prototype.detachEmployee = function (employee) {
  303.                 var index = this.getEmployees().indexOf(employee);
  304.                 if (index !== -1) {
  305.                     this.getEmployees().splice(index, 1);
  306.                 } else {
  307.                     throw new Error("Limo does not contain such employee.");
  308.                 }
  309.             };
  310.  
  311.             Limo.prototype.getEmployees = function () {
  312.                 return this._employees;
  313.             };
  314.  
  315.             Limo.prototype.getEmployeesInfo = function () {
  316.                 var employeesInfo = " --> Employees, allowed to drive:" +
  317.                     (this.getEmployees().length > 0 ? "\n" + this.getEmployees().join("\n") : " ---");
  318.  
  319.                 return employeesInfo;
  320.             };
  321.  
  322.             Limo.prototype.toString = function() {
  323.                 return Automobile.prototype.toString.call(this) +
  324.                     "\n" + this.getEmployeesInfo();
  325.             };
  326.  
  327.             return Limo;
  328.         })();
  329.  
  330.         return {
  331.             Employee: Employee,
  332.             Vehicle: Vehicle,
  333.             Bike: Bike,
  334.             Automobile: Automobile,
  335.             Truck: Truck,
  336.             Limo: Limo
  337.         }
  338.     }());
  339.  
  340.     var ParkManager = (function(){
  341.         var _vehicles;
  342.         var _employees;
  343.  
  344.         function init() {
  345.             _vehicles = [];
  346.             _employees = [];
  347.         }
  348.  
  349.         var CommandProcessor = (function() {
  350.  
  351.             function processInsertCommand(command) {
  352.                 var object;
  353.  
  354.                 switch (command["type"]) {
  355.                     case "bike":
  356.                         object = new Models.Bike(
  357.                             command["brand"],
  358.                             parseFloat(command["age"]),
  359.                             command["terrain-coverage"],
  360.                             parseFloat(command["frame-size"]),
  361.                             command["number-of-shifts"]);
  362.                         _vehicles.push(object);
  363.                         break;
  364.                     case "truck":
  365.                         object = new Models.Truck(
  366.                             command["brand"],
  367.                             parseFloat(command["age"]),
  368.                             command["terrain-coverage"],
  369.                             parseFloat(command["consumption"]),
  370.                             command["type-of-fuel"],
  371.                             parseFloat(command["number-of-doors"]));
  372.                         _vehicles.push(object);
  373.                         break;
  374.                     case "limo":
  375.                         object = new Models.Limo(
  376.                             command["brand"],
  377.                             parseFloat(command["age"]),
  378.                             parseFloat(command["number-of-wheels"]),
  379.                             parseFloat(command["consumption"]),
  380.                             command["type-of-fuel"]);
  381.                         _vehicles.push(object);
  382.                         break;
  383.                     case "employee":
  384.                         object = new Models.Employee(command["name"], command["position"], parseFloat(command["grade"]));
  385.                         _employees.push(object);
  386.                         break;
  387.                     default:
  388.                         throw new Error("Invalid type.");
  389.                 }
  390.  
  391.                 return object.constructor.name + " created.";
  392.             }
  393.  
  394.             function processDeleteCommand(command) {
  395.                 var object,
  396.                     index;
  397.  
  398.                 switch (command["type"]) {
  399.                     case "employee":
  400.                         object = getEmployeeByName(command["name"]);
  401.                         _vehicles.forEach(function(t) {
  402.                             if (t instanceof Models.Limo && t.getEmployees().indexOf(object) !== -1) {
  403.                                 t.detachEmployee(object);
  404.                             }
  405.                         });
  406.                         index = _employees.indexOf(object);
  407.                         _employees.splice(index, 1);
  408.                         break;
  409.                     case "bike":
  410.                     case "truck":
  411.                     case "limo":
  412.                         object = getVehicleByBrandAndType(command["brand"],command["type"]);
  413.                         index = _vehicles.indexOf(object);
  414.                         _vehicles.splice(index, 1);
  415.                         break;
  416.                     default:
  417.                         throw new Error("Unknown type.");
  418.                 }
  419.  
  420.                 return object.constructor.name + " deleted.";
  421.             }
  422.  
  423.             function processListCommand(command) {
  424.                 return formatOutputList(_vehicles);
  425.             }
  426.  
  427.             function processAppendEmployeeCommand(command) {
  428.                 var employee = getEmployeeByName(command["name"]);
  429.                 var limos = getLimosByBrand(command["brand"]);
  430.  
  431.                 for (var i=0; i < limos.length; i++) {
  432.                     limos[i].appendEmployee(employee);
  433.                 }
  434.                 return "Added employee to possible Limos.";
  435.             }
  436.  
  437.             function processDetachEmployeeCommand(command) {
  438.                 var employee = getEmployeeByName(command["name"]);
  439.                 var limos = getLimosByBrand(command["brand"]);
  440.  
  441.                 for (var i=0; i < limos.length; i++) {
  442.                     limos[i].detachEmployee(employee);
  443.                 }
  444.  
  445.                 return "Removed employee from possible Limos.";
  446.             }
  447.  
  448.             // Functions below are not revealed
  449.  
  450.             function getVehicleByBrandAndType(brand, type) {
  451.                 for (var i=0; i < _vehicles.length; i++) {
  452.                     if (_vehicles[i].constructor.name.toString().toLowerCase() === type &&
  453.                         _vehicles[i].getBrand() === brand) {
  454.                         return _vehicles[i];
  455.                     }
  456.                 }
  457.                 throw new Error("No Limo with such brand exists.");
  458.             }
  459.  
  460.             function getLimosByBrand(brand) {
  461.                 var currentVehicles = [];
  462.                 for (var i=0; i < _vehicles.length; i++) {
  463.                     if (_vehicles[i] instanceof Models.Limo &&
  464.                         _vehicles[i].getBrand() === brand) {
  465.                         currentVehicles.push(_vehicles[i]);
  466.                     }
  467.                 }
  468.                 if (currentVehicles.length > 0) {
  469.                     return currentVehicles;
  470.                 }
  471.                 throw new Error("No Limo with such brand exists.");
  472.             }
  473.  
  474.             function getEmployeeByName(name) {
  475.  
  476.                 for (var i = 0; i < _employees.length; i++) {
  477.                     if (_employees[i].getName() === name) {
  478.                         return _employees[i];
  479.                     }
  480.                 }
  481.                 throw new Error("No Employee with such name exists.");
  482.             }
  483.  
  484.             function formatOutputList(output) {
  485.                 var queryString = "List Output:\n";
  486.  
  487.                 if (output.length > 0) {
  488.                     queryString += output.join("\n");
  489.                 } else {
  490.                     queryString = "No results.";
  491.                 }
  492.  
  493.                 return queryString;
  494.             }
  495.  
  496.             function processListEmployees(commandArgs){
  497.                 var grade = commandArgs["grade"];
  498.                 _employees.sort(function (a, b) {
  499.                     return a.getName() > b.getName();
  500.                 });
  501.                 var result = "List Output:" + "\n";
  502.                 for (var i = 0; i < _employees.length; i++) {
  503.                     if (_employees[i].getGrade() >= grade || grade === "all") {
  504.                         result += _employees[i].toString() + "\n";
  505.                     }
  506.                 }
  507.                 return result;
  508.             };
  509.  
  510.             return {
  511.                 processInsertCommand: processInsertCommand,
  512.                 processDeleteCommand: processDeleteCommand,
  513.                 processListCommand: processListCommand,
  514.                 processAppendEmployeeCommand: processAppendEmployeeCommand,
  515.                 processDetachEmployeeCommand: processDetachEmployeeCommand,
  516.                 processListEmployees: processListEmployees
  517.             }
  518.         }());
  519.  
  520.         var Command = (function() {
  521.             function Command(cmdLine) {
  522.                 this._cmdArgs = processCommand(cmdLine);
  523.             }
  524.  
  525.             function processCommand(cmdLine) {
  526.                 var parameters = [],
  527.                     matches = [],
  528.                     pattern = /(.+?)=(.+?)[;)]/g,
  529.                     key,
  530.                     value,
  531.                     split;
  532.  
  533.                 split = cmdLine.split("(");
  534.                 parameters["command"] = split[0];
  535.                 while ((matches = pattern.exec(split[1])) !== null) {
  536.                     key = matches[1];
  537.                     value = matches[2];
  538.                     parameters[key] = value;
  539.                 }
  540.  
  541.                 return parameters;
  542.             }
  543.  
  544.             return Command;
  545.         }());
  546.  
  547.         function executeCommands(cmds) {
  548.             var commandArgs = new Command(cmds)._cmdArgs,
  549.                 action = commandArgs["command"],
  550.                 output;
  551.  
  552.             switch (action) {
  553.                 case "insert":
  554.                     output = CommandProcessor.processInsertCommand(commandArgs);
  555.                     break;
  556.                 case "delete":
  557.                     output = CommandProcessor.processDeleteCommand(commandArgs);
  558.                     break;
  559.                 case "append-employee":
  560.                     output = CommandProcessor.processAppendEmployeeCommand(commandArgs);
  561.                     break;
  562.                 case "detach-employee":
  563.                     output = CommandProcessor.processDetachEmployeeCommand(commandArgs);
  564.                     break;
  565.                 case "list":
  566.                     output = CommandProcessor.processListCommand(commandArgs);
  567.                     break;
  568.                 case "list-employees":
  569.                     output = CommandProcessor.processListEmployees(commandArgs);
  570.                     break;
  571.                 default:
  572.                     throw new Error("Unsupported command.");
  573.             }
  574.  
  575.             return output;
  576.         }
  577.  
  578.         return {
  579.             init: init,
  580.             executeCommands: executeCommands
  581.         }
  582.     }());
  583.  
  584.     var output = "";
  585.     ParkManager.init();
  586.  
  587.     commands.forEach(function(cmd) {
  588.         var result;
  589.         if (cmd != "") {
  590.             try {
  591.                 result = ParkManager.executeCommands(cmd) + "\n";
  592.             } catch (e) {
  593.                 result = "Invalid command." + "\n";
  594.                 //result = e.message + "\n";
  595.             }
  596.             output += result;
  597.         }
  598.     });
  599.  
  600.     return output;
  601. }
  602.  
  603. // ------------------------------------------------------------
  604. // Read the input from the console as array and process it
  605. // Remove all below code before submitting to the judge system!
  606. // ------------------------------------------------------------
  607.  
  608. (function() {
  609.     var arr = [];
  610.     if (typeof (require) == 'function') {
  611.         // We are in node.js --> read the console input and process it
  612.         require('readline').createInterface({
  613.             input: process.stdin,
  614.             output: process.stdout
  615.         }).on('line', function(line) {
  616.             arr.push(line);
  617.         }).on('close', function() {
  618.             console.log(processVehicleParkCommands(arr));
  619.         });
  620.     }
  621. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement