Venciity

Estates JavaScript 84/100

Nov 8th, 2014
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function processEstatesAgencyCommands(commands) {
  2.  
  3.     'use strict';
  4.  
  5.     Object.prototype.extends = function (parent) {
  6.         this.prototype = Object.create(parent.prototype);
  7.         this.prototype.constructor = this;
  8.     }
  9.  
  10.  
  11.     var Estate = (function () {
  12.         function Estate(name, area, location, isFurnitured) {
  13.  
  14.             if (this.constructor === Estate) {
  15.                 throw new Error("Can't instantiate abstract class Estate!");
  16.             }
  17.  
  18.             this.setName(name);
  19.             this.setArea(area);
  20.             this.setLocation(location);
  21.             this.setIsFurnitured(isFurnitured);
  22.         }
  23.  
  24.         Estate.prototype.getName = function () {
  25.             return this._name;
  26.         }
  27.  
  28.         Estate.prototype.setName = function (name) {
  29.             // TODO ADD VALIDATION
  30.             this._name = name;
  31.         }
  32.  
  33.         Estate.prototype.getArea = function () {
  34.             return this._area;
  35.         }
  36.  
  37.         Estate.prototype.setArea = function (area) {
  38.             // TODO ADD VALIDATION
  39.             this._area = area;
  40.         }
  41.  
  42.         Estate.prototype.getLocation = function () {
  43.             return this._location;
  44.         }
  45.  
  46.         Estate.prototype.setLocation = function (location) {
  47.             // TODO ADD VALIDATION
  48.             this._location = location;
  49.         }
  50.  
  51.         Estate.prototype.getIsFurnitured = function () {
  52.             return this._isFurnitured;
  53.         }
  54.  
  55.         Estate.prototype.setIsFurnitured = function (isFurnitured) {
  56.             // TODO ADD VALIDATION
  57.             this._isFurnitured = isFurnitured;
  58.         }
  59.  
  60.         // House: Name = houseBankya, Area = 206, Location = Bankya, Furnitured = Yes, Floors: 3
  61.         Estate.prototype.toString = function () {
  62.             var furitured = this.getIsFurnitured() ? "Yes" : "No";
  63.             return this.constructor.name + ": Name = " + this.getName() +
  64.                 ", Area = " + this.getArea() +
  65.                 ", Location = " + this.getLocation() +
  66.                 ", Furnitured = " + furitured;
  67.         }
  68.  
  69.         return Estate;
  70.     }());
  71.  
  72.  
  73.     var BuildingEstate = (function () {
  74.         function BuildingEstate(name, area, location, isFurnitured, rooms, hasElevator) {
  75.  
  76.             if (this.constructor === BuildingEstate) {
  77.                 throw new Error("Can't instantiate abstract class BuildingEstate!");
  78.             }
  79.  
  80.             Estate.call(this, name, area, location, isFurnitured);
  81.             this.setRooms(rooms);
  82.             this.setHasElevator(hasElevator);
  83.         }
  84.  
  85.         BuildingEstate.extends(Estate);
  86.  
  87.         BuildingEstate.prototype.getRooms = function () {
  88.             return this._rooms;
  89.         }
  90.  
  91.         BuildingEstate.prototype.setRooms = function (rooms) {
  92.             // TODO ADD VALIDATION
  93.             this._rooms = rooms;
  94.         }
  95.  
  96.         BuildingEstate.prototype.getHasElevator = function () {
  97.             return this._hasElevator;
  98.         }
  99.  
  100.         BuildingEstate.prototype.setHasElevator = function (hasElevator) {
  101.             // TODO ADD VALIDATION
  102.             this._hasElevator = hasElevator;
  103.         }
  104.  
  105.  
  106.         // Apartment: Name = aptLozenec24, Area = 150, Location = Sofia, Furnitured = Yes, Rooms: 4, Elevator: Yes
  107.         BuildingEstate.prototype.toString = function () {
  108.             var elevator = this.getHasElevator() ? "Yes" : "No";
  109.             return Estate.prototype.toString.call(this) +
  110.                 ", Rooms: " + this.getRooms() +
  111.                 ", Elevator: " + elevator;
  112.         }
  113.  
  114.         return BuildingEstate;
  115.     }());
  116.  
  117.  
  118.     var Apartment = (function () {
  119.         function Apartment(name, area, location, isFurnitured, rooms, hasElevator) {
  120.             BuildingEstate.call(this, name, area, location, isFurnitured, rooms, hasElevator);
  121.         }
  122.  
  123.         Apartment.extends(BuildingEstate);
  124.  
  125.         return Apartment;
  126.     }());
  127.  
  128.  
  129.     var Office = (function () {
  130.         function Office(name, area, location, isFurnitured, rooms, hasElevator) {
  131.             BuildingEstate.call(this, name, area, location, isFurnitured, rooms, hasElevator);
  132.         }
  133.  
  134.         Office.extends(BuildingEstate);
  135.  
  136.         return Office;
  137.     }());
  138.  
  139.  
  140.     var House = (function () {
  141.         function House(name, area, location, isFurnitured, floors) {
  142.             Estate.call(this, name, area, location, isFurnitured);
  143.             this.setFloors(floors);
  144.         }
  145.  
  146.         House.extends(Estate);
  147.  
  148.         House.prototype.getFloors = function () {
  149.             return this._floors;
  150.         }
  151.  
  152.         House.prototype.setFloors = function (floors) {
  153.             // TO DO ADD VALIDATION
  154.             this._floors = floors;
  155.         }
  156.  
  157.         // House: Name = houseBankya, Area = 206, Location = Bankya, Furnitured = Yes, Floors: 3
  158.         House.prototype.toString = function () {
  159.             return Estate.prototype.toString.call(this) +
  160.                 ", Floors: " + this.getFloors();
  161.         }
  162.  
  163.         return House;
  164.  
  165.     }());
  166.  
  167.  
  168.     var Garage = (function () {
  169.         function Garage(name, area, location, isFurnitured, width, height) {
  170.             Estate.call(this, name, area, location, isFurnitured);
  171.             this.setWidth(width);
  172.             this.setHeigth(height);
  173.            
  174.         }
  175.  
  176.         Garage.extends(Estate);
  177.  
  178.         Garage.prototype.getWidth = function () {
  179.             return this._width;
  180.         }
  181.  
  182.         Garage.prototype.setWidth = function (width) {
  183.             // TO DO ADD VALIDATION
  184.             this._width = width;
  185.         }
  186.  
  187.         Garage.prototype.getHeigth = function () {
  188.             return this._height;
  189.         }
  190.  
  191.         Garage.prototype.setHeigth = function (height) {
  192.             // TO DO ADD VALIDATION
  193.             this._height = height;
  194.         }
  195.  
  196.         // Garage: Name = garageLozenec, Area = 18, Location = Sofia, Furnitured = No, Width: 3, Height: 6
  197.         Garage.prototype.toString = function () {
  198.             return Estate.prototype.toString.call(this) +
  199.                 ", Width: " + this.getWidth() +
  200.                 ", Height: " + this.getHeigth();
  201.         }
  202.  
  203.         return Garage;
  204.  
  205.     }());
  206.  
  207.  
  208.     var Offer = (function () {
  209.         function Offer(estate, price) {
  210.  
  211.             if (this.constructor === Offer) {
  212.                 throw new Error("Can't instantiate abstract class Offer!");
  213.             }
  214.  
  215.             this.setEstate(estate);
  216.             this.setPrice(price);
  217.         }
  218.  
  219.         Offer.prototype.getEstate = function () {
  220.             return this._estate;
  221.         }
  222.  
  223.         Offer.prototype.setEstate = function (estate) {
  224.             if (!estate) {
  225.                 throw new Error("Estate cannot be empty");
  226.             }
  227.  
  228.             this._estate = estate;
  229.         }
  230.  
  231.         Offer.prototype.getPrice = function () {
  232.             return this._price;
  233.         }
  234.  
  235.         Offer.prototype.setPrice = function (price) {
  236.             // TODO ADD VALIDATION
  237.             this._price = price;
  238.         }
  239.  
  240.         // Rent: Estate = aptLozenec24, Location = Sofia, Price = 750
  241.         Offer.prototype.toString = function () {
  242.             return "Estate = " + this.getEstate().getName() +
  243.                 ", Location = " + this.getEstate().getLocation() +
  244.                 ", Price = " + this.getPrice();
  245.         }
  246.  
  247.         return Offer;
  248.     }());
  249.  
  250.  
  251.     var RentOffer = (function () {
  252.         function RentOffer(estate, price) {
  253.             Offer.call(this, estate, price);
  254.         }
  255.  
  256.         RentOffer.extends(Offer);
  257.  
  258.         RentOffer.prototype.toString = function () {
  259.             return "Rent: " + Offer.prototype.toString.call(this);
  260.         }
  261.  
  262.         return RentOffer;
  263.     }());
  264.  
  265.  
  266.     var SaleOffer = (function () {
  267.         function SaleOffer(estate, price) {
  268.             Offer.call(this, estate, price);
  269.         }
  270.  
  271.         SaleOffer.extends(Offer);
  272.  
  273.         SaleOffer.prototype.toString = function () {
  274.             return "Sale: " + Offer.prototype.toString.call(this);
  275.         }
  276.  
  277.         return SaleOffer;
  278.     }());
  279.  
  280.  
  281.     var EstatesEngine = (function () {
  282.         var _estates;
  283.         var _uniqueEstateNames;
  284.         var _offers;
  285.  
  286.         function initialize() {
  287.             _estates = [];
  288.             _uniqueEstateNames = {};
  289.             _offers = [];
  290.         }
  291.  
  292.         function executeCommand(command) {
  293.             var cmdParts = command.split(' ');
  294.             var cmdName = cmdParts[0];
  295.             var cmdArgs = cmdParts.splice(1);
  296.             switch (cmdName) {
  297.                 case 'create':
  298.                     return executeCreateCommand(cmdArgs);
  299.                 case 'status':
  300.                     return executeStatusCommand();
  301.                 case 'find-sales-by-location':
  302.                     return executeFindSalesByLocationCommand(cmdArgs[0]);
  303.                 case 'find-rents-by-location':
  304.                     return executeFindRentsByLocationCommand(cmdArgs[0]);
  305.                 case 'find-rents-by-price':
  306.                     return executeFindRentsByPriceCommand(Number(cmdArgs[0]), Number(cmdArgs[1]));
  307.                 default:
  308.                     throw new Error('Unknown command: ' + cmdName);
  309.             }
  310.         }
  311.  
  312.         function executeCreateCommand(cmdArgs) {
  313.             var objType = cmdArgs[0];
  314.             switch (objType) {
  315.                 case 'Apartment':
  316.                     var apartment = new Apartment(cmdArgs[1], Number(cmdArgs[2]), cmdArgs[3],
  317.                         parseBoolean(cmdArgs[4]), Number(cmdArgs[5]), parseBoolean(cmdArgs[6]));
  318.                     addEstate(apartment);
  319.                     break;
  320.                 case 'Office':
  321.                     var office = new Office(cmdArgs[1], Number(cmdArgs[2]), cmdArgs[3],
  322.                         parseBoolean(cmdArgs[4]), Number(cmdArgs[5]), parseBoolean(cmdArgs[6]));
  323.                     addEstate(office);
  324.                     break;
  325.                 case 'House':
  326.                     var house = new House(cmdArgs[1], Number(cmdArgs[2]), cmdArgs[3],
  327.                         parseBoolean(cmdArgs[4]), Number(cmdArgs[5]));
  328.                     addEstate(house);
  329.                     break;
  330.                 case 'Garage':
  331.                     var garage = new Garage(cmdArgs[1], Number(cmdArgs[2]), cmdArgs[3],
  332.                         parseBoolean(cmdArgs[4]), Number(cmdArgs[5]), Number(cmdArgs[6]));
  333.                     addEstate(garage);
  334.                     break;
  335.                 case 'RentOffer':
  336.                     var estate = findEstateByName(cmdArgs[1]);
  337.                     var rentOffer = new RentOffer(estate, Number(cmdArgs[2]));
  338.                     addOffer(rentOffer);
  339.                     break;
  340.                 case 'SaleOffer':
  341.                     estate = findEstateByName(cmdArgs[1]);
  342.                     var saleOffer = new SaleOffer(estate, Number(cmdArgs[2]));
  343.                     addOffer(saleOffer);
  344.                     break;
  345.                 default:
  346.                     throw new Error('Unknown object to create: ' + objType);
  347.             }
  348.             return objType + ' created.';
  349.         }
  350.  
  351.         function parseBoolean(value) {
  352.             switch (value) {
  353.                 case "true":
  354.                     return true;
  355.                 case "false":
  356.                     return false;
  357.                 default:
  358.                     throw new Error("Invalid boolean value: " + value);
  359.             }
  360.         }
  361.  
  362.         function findEstateByName(estateName) {
  363.             for (var i = 0; i < _estates.length; i++) {
  364.                 if (_estates[i].getName() == estateName) {
  365.                     return _estates[i];
  366.                 }
  367.             }
  368.             return undefined;
  369.         }
  370.  
  371.         function addEstate(estate) {
  372.             if (_uniqueEstateNames[estate.getName()]) {
  373.                 throw new Error('Duplicated estate name: ' + estate.getName());
  374.             }
  375.             _uniqueEstateNames[estate.getName()] = true;
  376.             _estates.push(estate);
  377.         }
  378.  
  379.         function addOffer(offer) {
  380.             _offers.push(offer);
  381.         }
  382.  
  383.         function executeStatusCommand() {
  384.             var result = '', i;
  385.             if (_estates.length > 0) {
  386.                 result += 'Estates:\n';
  387.                 for (i = 0; i < _estates.length; i++) {
  388.                     result += "  " + _estates[i].toString() + '\n';
  389.                 }
  390.             } else {
  391.                 result += 'No estates\n';
  392.             }
  393.  
  394.             if (_offers.length > 0) {
  395.                 result += 'Offers:\n';
  396.                 for (i = 0; i < _offers.length; i++) {
  397.                     result += "  " + _offers[i].toString() + '\n';
  398.                 }
  399.             } else {
  400.                 result += 'No offers\n';
  401.             }
  402.  
  403.             return result.trim();
  404.         }
  405.  
  406.         function executeFindSalesByLocationCommand(location) {
  407.             return executeFindOffersByLocationCommand(location, SaleOffer);
  408.         }
  409.  
  410.         function executeFindRentsByLocationCommand(location) {
  411.             return executeFindOffersByLocationCommand(location, RentOffer);
  412.         }
  413.  
  414.         function executeFindOffersByLocationCommand(location , offerType) {
  415.             if (!location) {
  416.                 throw new Error("Location cannot be empty.");
  417.             }
  418.  
  419.             var selectedOffers = _offers.filter(function (offer) {
  420.                 return offer.getEstate().getLocation() === location &&
  421.                     offer instanceof offerType;
  422.             });
  423.             selectedOffers.sort(function (a, b) {
  424.                 return a.getEstate().getName().localeCompare(b.getEstate().getName());
  425.             });
  426.  
  427.             return formatQueryResults(selectedOffers);
  428.         }
  429.  
  430.         function executeFindRentsByPriceCommand(minPrice, maxPrice) {
  431.             if (isNaN(minPrice) || isNaN(maxPrice)) {
  432.                 throw new Error("Invalid price range.");
  433.             }
  434.  
  435.             var selectedOffers = _offers.filter(function (offer) {
  436.                 return offer.getPrice() >= minPrice && offer.getPrice() <= maxPrice && offer instanceof RentOffer;
  437.             });
  438.             selectedOffers.sort(function (a, b) {
  439.                 var result = a.getPrice() - b.getPrice();
  440.                 if (result == 0) {
  441.                     result = a.getEstate().getName().localeCompare(b.getEstate().getName());
  442.                 }
  443.  
  444.                 return result;
  445.             });
  446.  
  447.             return formatQueryResults(selectedOffers);
  448.         }
  449.  
  450.         function formatQueryResults(offers) {
  451.             var result = '';
  452.             if (offers.length == 0) {
  453.                 result += 'No Results\n';
  454.             } else {
  455.                 result += 'Query Results:\n';
  456.                 for (var i = 0; i < offers.length; i++) {
  457.                     var offer = offers[i];
  458.                     result += '  [Estate: ' + offer.getEstate().getName() +
  459.                         ', Location: ' + offer.getEstate().getLocation() +
  460.                         ', Price: ' + offer.getPrice() + ']\n';
  461.                 }
  462.             }
  463.             return result.trim();
  464.         }
  465.  
  466.         return {
  467.             initialize: initialize,
  468.             executeCommand: executeCommand
  469.         };
  470.     }());
  471.  
  472.  
  473.     // Process the input commands and return the results
  474.     var results = '';
  475.     EstatesEngine.initialize();
  476.     commands.forEach(function (cmd) {
  477.         if (cmd != '') {
  478.             try {
  479.                 var cmdResult = EstatesEngine.executeCommand(cmd);
  480.                 results += cmdResult + '\n';
  481.             } catch (err) {
  482.                 console.log(err);
  483.                 results += 'Invalid command.\n';
  484.             }
  485.         }
  486.     });
  487.     return results.trim();
  488.  
  489. }
  490.  
  491. // ------------------------------------------------------------
  492. // Read the input from the console as array and process it
  493. // Remove all below code before submitting to the judge system!
  494. // ------------------------------------------------------------
  495.  
  496. (function () {
  497.     var arr = [];
  498.     if (typeof (require) == 'function') {
  499.         // We are in node.js --> read the console input and process it
  500.         require('readline').createInterface({
  501.             input: process.stdin,
  502.             output: process.stdout
  503.         }).on('line', function (line) {
  504.             arr.push(line);
  505.         }).on('close', function () {
  506.             console.log(processEstatesAgencyCommands(arr));
  507.         });
  508.     }
  509. })();
Advertisement
Add Comment
Please, Sign In to add comment