Advertisement
krutzz

Untitled

Mar 2nd, 2017
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     // Your classes
  3.  
  4.     const VALIDATOR = {
  5.         isString: function(x) {
  6.             if (typeof x !== 'string') {
  7.                 throw Error();
  8.             }
  9.         },
  10.         isInRange: function(x, min, max) {
  11.             if (x < min || x > max || Number.isNaN(x)) {
  12.                 throw Error();
  13.             }
  14.         },
  15.         isPositive: function(x) {
  16.             if (typeof x !== 'number' || x <= 0 || Number.isNaN(x)) {
  17.                 throw Error();
  18.             }
  19.         },
  20.         isNumber: function(x) {
  21.             if (typeof x !== 'number' || Number.isNaN(x)) {
  22.                 throw Error();
  23.             }
  24.         },
  25.         isInt: function(x) {
  26.             if ((typeof x !== 'number') || (x <= 0) || ((x | 0) !== x) || Number.isNaN(x)) {
  27.                 throw Error();
  28.             }
  29.         },
  30.                 isValidId: function(x) {
  31.             if ((typeof x !== 'number') || (x < 0) || ((x | 0) !== x) || Number.isNaN(x)) {
  32.                 throw Error();
  33.             }
  34.         },
  35.         isValidQuality: (function() {
  36.             const validAlignments = ['high', 'mid', 'low'];
  37.             return function(x) {
  38.                 if (validAlignments.indexOf(x) < 0) {
  39.                     throw Error();
  40.                 }
  41.             };
  42.         })(),
  43.     }
  44.  
  45.     const getNextId = (function() {
  46.         let counter = 0;
  47.         return function() {
  48.             counter += 1;
  49.             return counter;
  50.         };
  51.     })();
  52.  
  53.     class Product {
  54.  
  55.         constructor(manufacturer, model, price) {
  56.             this._id = getNextId();
  57.             this.manufacturer = manufacturer;
  58.             this.model = model;
  59.             this.price = price;
  60.         }
  61.  
  62.         get id() {
  63.             return this._id;
  64.         }
  65.  
  66.         get manufacturer() {
  67.             return this._manufacturer;
  68.         }
  69.         set manufacturer(x) {
  70.             VALIDATOR.isString(x);
  71.             VALIDATOR.isInRange(x.length, 1, 20);
  72.             this._manufacturer = x;
  73.         }
  74.  
  75.         get model() {
  76.             return this._model;
  77.         }
  78.         set model(x) {
  79.             VALIDATOR.isString(x);
  80.             VALIDATOR.isInRange(x.length, 1, 20);
  81.             this._model = x;
  82.         }
  83.  
  84.         get price() {
  85.             return this._price;
  86.         }
  87.  
  88.         set price(x) {
  89.             VALIDATOR.isPositive(x);
  90.             this._price = x;
  91.         }
  92.  
  93.         getLabel() {
  94.             return this.manufacturer + ' ' + this.model + ' - **' + this.price + '**';
  95.         }
  96.     }
  97.  
  98.     class SmartPhone extends Product {
  99.         constructor(manufacturer, model, price, screenSize, operatingSystem) {
  100.             super(manufacturer, model, price);
  101.  
  102.             this.screenSize = screenSize;
  103.             this.operatingSystem = operatingSystem;
  104.         }
  105.  
  106.         get screenSize() {
  107.             return this._screenSize;
  108.         }
  109.         set screenSize(x) {
  110.             VALIDATOR.isPositive(x);
  111.             this._screenSize = x;
  112.         }
  113.  
  114.         get operatingSystem() {
  115.             return this._operatingSystem;
  116.         }
  117.         set operatingSystem(x) {
  118.             VALIDATOR.isString(x);
  119.             VALIDATOR.isInRange(x.length, 1, 10);
  120.             this._operatingSystem = x;
  121.         }
  122.  
  123.         getLabel() {
  124.             return 'SmartPhone - ' + super.getLabel();
  125.         }
  126.  
  127.     }
  128.  
  129.     class Charger extends Product {
  130.         constructor(manufacturer, model, price, outputVoltage, outputCurrent) {
  131.             super(manufacturer, model, price);
  132.             this.outputVoltage = outputVoltage;
  133.             this.outputCurrent = outputCurrent;
  134.  
  135.         }
  136.  
  137.         get outputVoltage() {
  138.             return this._outputVoltage;
  139.         }
  140.         set outputVoltage(x) {
  141.             VALIDATOR.isNumber(x);
  142.             VALIDATOR.isInRange(x, 5, 20);
  143.             this._outputVoltage = x;
  144.         }
  145.  
  146.         get outputCurrent() {
  147.             return this._outputCurrent;
  148.         }
  149.         set outputCurrent(x) {
  150.             VALIDATOR.isNumber(x);
  151.             VALIDATOR.isInRange(x, 100, 3000);
  152.             this._outputCurrent = x;
  153.         }
  154.  
  155.         getLabel() {
  156.             return 'Charger - ' + super.getLabel();
  157.         }
  158.  
  159.     }
  160.  
  161.     class Router extends Product {
  162.         constructor(manufacturer, model, price, wifiRange, lanPorts) {
  163.             super(manufacturer, model, price);
  164.             this.wifiRange = wifiRange;
  165.             this.lanPorts = lanPorts;
  166.         }
  167.  
  168.         get wifiRange() {
  169.             return this._wifiRange;
  170.         }
  171.         set wifiRange(x) {
  172.             VALIDATOR.isPositive(x);
  173.             this._wifiRange = x;
  174.         }
  175.  
  176.         get lanPorts() {
  177.             return this._lanPorts;
  178.         }
  179.         set lanPorts(x) {
  180.             VALIDATOR.isInt(x);
  181.             this._lanPorts = x;
  182.         }
  183.  
  184.         getLabel() {
  185.             return 'Router - ' + super.getLabel();
  186.         }
  187.  
  188.     }
  189.  
  190.     class Headphones extends Product {
  191.         constructor(manufacturer, model, price, quality, hasMicrophone) {
  192.             super(manufacturer, model, price);
  193.             this.quality = quality;
  194.             this.hasMicrophone = hasMicrophone;
  195.         }
  196.  
  197.         get quality() {
  198.             return this._quality;
  199.         }
  200.         set quality(x) {
  201.             VALIDATOR.isValidQuality(x);
  202.             this._quality = x;
  203.         }
  204.  
  205.         get hasMicrophone() {
  206.             return this._hasMicrophone;
  207.         }
  208.         set hasMicrophone(x) {
  209.             if (x) {
  210.                 this._hasMicrophone = true;
  211.             } else {
  212.                 this._hasMicrophone = false;
  213.             }
  214.         }
  215.  
  216.         getLabel() {
  217.             return 'Headphones - ' + super.getLabel();
  218.         }
  219.     }
  220.  
  221.  
  222.     class HardwareStore {
  223.         constructor(name) {
  224.             this.name = name;
  225.             this._products = [];
  226.                         this._productsH = [];
  227.             this._sold = 0;
  228.         }
  229.  
  230.         get name() {
  231.             return this._name;
  232.         }
  233.         set name(x) {
  234.             VALIDATOR.isString(x);
  235.             VALIDATOR.isInRange(x.length, 1, 20);
  236.             this._name = x;
  237.         }
  238.  
  239.         get products() {
  240.             return this._products;
  241.         }
  242.  
  243.         stock(p, quantity) {
  244.             if (!(p instanceof Product)) {
  245.                 throw Error();
  246.             }
  247.             VALIDATOR.isInt(quantity);
  248.             VALIDATOR.isPositive(quantity);
  249.  
  250.             const productId = p.id;
  251.             const productObj = this._productsH.find(x => x.id === productId);
  252.             if (productObj) {
  253.                 productObj.quantity += quantity;
  254.             } else {
  255.                 this._productsH.push({
  256.                     id: productId,
  257.                     product: p,
  258.                     quantity: quantity
  259.                 });
  260.                                 this.products.push(p);
  261.             }
  262.  
  263.             return this;
  264.         }
  265.  
  266.  
  267.  
  268.         sell(productId, quantity) {
  269.             VALIDATOR.isInt(quantity);
  270.             VALIDATOR.isPositive(quantity);
  271.                         VALIDATOR.isValidId(productId);
  272.  
  273.             const productObj = this._productsH.find(x => x.id === productId);
  274.  
  275.             if (productObj && (productObj.quantity - quantity) >= 0) {
  276.                 this._sold += productObj.product.price * quantity;
  277.                 productObj.quantity -= quantity;
  278.             }else{
  279.                             throw Error();
  280.                         }
  281.                         if(productObj && productObj.quantity === 0){
  282.                             let index = this.products.findIndex(x => x.id === productObj.id);
  283.                             this.products.splice(index,1);
  284.                             index = this._productsH.findIndex(x => x.id === productObj.id);
  285.                             this._productsH.splice(index,1);
  286.                         }
  287.  
  288.             return this;
  289.         }
  290.  
  291.         getSold() {
  292.             return this._sold;
  293.         }
  294.  
  295.         search(obj) {
  296.             if (typeof obj === 'object') {
  297.  
  298.                             /*
  299.                             manufacturerPattern - string, should be contained in manufacterures (case sensitive)
  300.                             modelPattern - string, should be contained in models (case sensitive)
  301.                             type - string - SmartPhone, Charger, Router or Headphones - the product should be of the specified type
  302.                             minPrice - number - the product should not be cheaper than minPrice
  303.                             maxPrice - number - the product should not be more expensive than maxPrice
  304.                             */
  305.                             let resultArr = this._productsH.slice();
  306.                             const {manufacturerPattern,modelPattern,type,minPrice,maxPrice} = obj;
  307.  
  308.                             if(typeof manufacturerPattern !== 'undefined'){
  309.                                 VALIDATOR.isString(manufacturerPattern);
  310.                                 resultArr = resultArr.filter(x => x.product.manufacturer.indexOf(manufacturerPattern)>=0);
  311.                             }
  312.                             if(typeof modelPattern !== 'undefined'){
  313.                                 VALIDATOR.isString(modelPattern);
  314.                                 resultArr = resultArr.filter(x => x.product.model.indexOf(modelPattern) >= 0);
  315.                             }
  316.  
  317.                             if(typeof type !== 'undefined'){
  318.                                 resultArr = resultArr.filter(x => x.product.constructor.name === type);
  319.                             }
  320.  
  321.                             if(typeof minPrice !== 'undefined'){
  322.                                 VALIDATOR.isNumber(minPrice);
  323.                                 resultArr = resultArr.filter(x => x.product.price >= minPrice);
  324.                             }
  325.  
  326.                             if(typeof maxPrice !== 'undefined'){
  327.                                 VALIDATOR.isNumber(maxPrice);
  328.                                 resultArr = resultArr.filter(x => x.product.price <= maxPrice);
  329.                             }
  330.                             return resultArr;
  331.             } else {
  332.                                 VALIDATOR.isString(obj);
  333.                 return this._productsH.filter(x => x.product.model.toLowerCase().indexOf(obj.toLowerCase()) >= 0 || x.product.manufacturer.toLowerCase().indexOf(obj.toLocaleLowerCase())>=0)
  334.             }
  335.         }
  336.  
  337.     }
  338.  
  339.  
  340.     return {
  341.         getSmartPhone(manufacturer, model, price, screenSize, operatingSystem) {
  342.             // returns SmarhPhone instance
  343.             return new SmartPhone(manufacturer, model, price, screenSize, operatingSystem);
  344.         },
  345.         getCharger(manufacturer, model, price, outputVoltage, outputCurrent) {
  346.             // returns Charger instance
  347.             return new Charger(manufacturer, model, price, outputVoltage, outputCurrent);
  348.         },
  349.         getRouter(manufacturer, model, price, wifiRange, lanPorts) {
  350.             // returns Router instance
  351.             return new Router(manufacturer, model, price, wifiRange, lanPorts);
  352.         },
  353.         getHeadphones(manufacturer, model, price, quality, hasMicrophone) {
  354.             // returns Headphones instance
  355.             return new Headphones(manufacturer, model, price, quality, hasMicrophone);
  356.         },
  357.         getHardwareStore(name) {
  358.             // returns HardwareStore instance
  359.             return new HardwareStore(name);
  360.         }
  361.     };
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement