Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- // Your classes
- const VALIDATOR = {
- isString: function(x) {
- if (typeof x !== 'string') {
- throw Error();
- }
- },
- isInRange: function(x, min, max) {
- if (x < min || x > max || Number.isNaN(x)) {
- throw Error();
- }
- },
- isPositive: function(x) {
- if (typeof x !== 'number' || x <= 0 || Number.isNaN(x)) {
- throw Error();
- }
- },
- isNumber: function(x) {
- if (typeof x !== 'number' || Number.isNaN(x)) {
- throw Error();
- }
- },
- isInt: function(x) {
- if ((typeof x !== 'number') || (x <= 0) || ((x | 0) !== x) || Number.isNaN(x)) {
- throw Error();
- }
- },
- isValidId: function(x) {
- if ((typeof x !== 'number') || (x < 0) || ((x | 0) !== x) || Number.isNaN(x)) {
- throw Error();
- }
- },
- isValidQuality: (function() {
- const validAlignments = ['high', 'mid', 'low'];
- return function(x) {
- if (validAlignments.indexOf(x) < 0) {
- throw Error();
- }
- };
- })(),
- }
- const getNextId = (function() {
- let counter = 0;
- return function() {
- counter += 1;
- return counter;
- };
- })();
- class Product {
- constructor(manufacturer, model, price) {
- this._id = getNextId();
- this.manufacturer = manufacturer;
- this.model = model;
- this.price = price;
- }
- get id() {
- return this._id;
- }
- get manufacturer() {
- return this._manufacturer;
- }
- set manufacturer(x) {
- VALIDATOR.isString(x);
- VALIDATOR.isInRange(x.length, 1, 20);
- this._manufacturer = x;
- }
- get model() {
- return this._model;
- }
- set model(x) {
- VALIDATOR.isString(x);
- VALIDATOR.isInRange(x.length, 1, 20);
- this._model = x;
- }
- get price() {
- return this._price;
- }
- set price(x) {
- VALIDATOR.isPositive(x);
- this._price = x;
- }
- getLabel() {
- return this.manufacturer + ' ' + this.model + ' - **' + this.price + '**';
- }
- }
- class SmartPhone extends Product {
- constructor(manufacturer, model, price, screenSize, operatingSystem) {
- super(manufacturer, model, price);
- this.screenSize = screenSize;
- this.operatingSystem = operatingSystem;
- }
- get screenSize() {
- return this._screenSize;
- }
- set screenSize(x) {
- VALIDATOR.isPositive(x);
- this._screenSize = x;
- }
- get operatingSystem() {
- return this._operatingSystem;
- }
- set operatingSystem(x) {
- VALIDATOR.isString(x);
- VALIDATOR.isInRange(x.length, 1, 10);
- this._operatingSystem = x;
- }
- getLabel() {
- return 'SmartPhone - ' + super.getLabel();
- }
- }
- class Charger extends Product {
- constructor(manufacturer, model, price, outputVoltage, outputCurrent) {
- super(manufacturer, model, price);
- this.outputVoltage = outputVoltage;
- this.outputCurrent = outputCurrent;
- }
- get outputVoltage() {
- return this._outputVoltage;
- }
- set outputVoltage(x) {
- VALIDATOR.isNumber(x);
- VALIDATOR.isInRange(x, 5, 20);
- this._outputVoltage = x;
- }
- get outputCurrent() {
- return this._outputCurrent;
- }
- set outputCurrent(x) {
- VALIDATOR.isNumber(x);
- VALIDATOR.isInRange(x, 100, 3000);
- this._outputCurrent = x;
- }
- getLabel() {
- return 'Charger - ' + super.getLabel();
- }
- }
- class Router extends Product {
- constructor(manufacturer, model, price, wifiRange, lanPorts) {
- super(manufacturer, model, price);
- this.wifiRange = wifiRange;
- this.lanPorts = lanPorts;
- }
- get wifiRange() {
- return this._wifiRange;
- }
- set wifiRange(x) {
- VALIDATOR.isPositive(x);
- this._wifiRange = x;
- }
- get lanPorts() {
- return this._lanPorts;
- }
- set lanPorts(x) {
- VALIDATOR.isInt(x);
- this._lanPorts = x;
- }
- getLabel() {
- return 'Router - ' + super.getLabel();
- }
- }
- class Headphones extends Product {
- constructor(manufacturer, model, price, quality, hasMicrophone) {
- super(manufacturer, model, price);
- this.quality = quality;
- this.hasMicrophone = hasMicrophone;
- }
- get quality() {
- return this._quality;
- }
- set quality(x) {
- VALIDATOR.isValidQuality(x);
- this._quality = x;
- }
- get hasMicrophone() {
- return this._hasMicrophone;
- }
- set hasMicrophone(x) {
- if (x) {
- this._hasMicrophone = true;
- } else {
- this._hasMicrophone = false;
- }
- }
- getLabel() {
- return 'Headphones - ' + super.getLabel();
- }
- }
- class HardwareStore {
- constructor(name) {
- this.name = name;
- this._products = [];
- this._productsH = [];
- this._sold = 0;
- }
- get name() {
- return this._name;
- }
- set name(x) {
- VALIDATOR.isString(x);
- VALIDATOR.isInRange(x.length, 1, 20);
- this._name = x;
- }
- get products() {
- return this._products;
- }
- stock(p, quantity) {
- if (!(p instanceof Product)) {
- throw Error();
- }
- VALIDATOR.isInt(quantity);
- VALIDATOR.isPositive(quantity);
- const productId = p.id;
- const productObj = this._productsH.find(x => x.id === productId);
- if (productObj) {
- productObj.quantity += quantity;
- } else {
- this._productsH.push({
- id: productId,
- product: p,
- quantity: quantity
- });
- this.products.push(p);
- }
- return this;
- }
- sell(productId, quantity) {
- VALIDATOR.isInt(quantity);
- VALIDATOR.isPositive(quantity);
- VALIDATOR.isValidId(productId);
- const productObj = this._productsH.find(x => x.id === productId);
- if (productObj && (productObj.quantity - quantity) >= 0) {
- this._sold += productObj.product.price * quantity;
- productObj.quantity -= quantity;
- }else{
- throw Error();
- }
- if(productObj && productObj.quantity === 0){
- let index = this.products.findIndex(x => x.id === productObj.id);
- this.products.splice(index,1);
- index = this._productsH.findIndex(x => x.id === productObj.id);
- this._productsH.splice(index,1);
- }
- return this;
- }
- getSold() {
- return this._sold;
- }
- search(obj) {
- if (typeof obj === 'object') {
- /*
- manufacturerPattern - string, should be contained in manufacterures (case sensitive)
- modelPattern - string, should be contained in models (case sensitive)
- type - string - SmartPhone, Charger, Router or Headphones - the product should be of the specified type
- minPrice - number - the product should not be cheaper than minPrice
- maxPrice - number - the product should not be more expensive than maxPrice
- */
- let resultArr = this._productsH.slice();
- const {manufacturerPattern,modelPattern,type,minPrice,maxPrice} = obj;
- if(typeof manufacturerPattern !== 'undefined'){
- VALIDATOR.isString(manufacturerPattern);
- resultArr = resultArr.filter(x => x.product.manufacturer.indexOf(manufacturerPattern)>=0);
- }
- if(typeof modelPattern !== 'undefined'){
- VALIDATOR.isString(modelPattern);
- resultArr = resultArr.filter(x => x.product.model.indexOf(modelPattern) >= 0);
- }
- if(typeof type !== 'undefined'){
- resultArr = resultArr.filter(x => x.product.constructor.name === type);
- }
- if(typeof minPrice !== 'undefined'){
- VALIDATOR.isNumber(minPrice);
- resultArr = resultArr.filter(x => x.product.price >= minPrice);
- }
- if(typeof maxPrice !== 'undefined'){
- VALIDATOR.isNumber(maxPrice);
- resultArr = resultArr.filter(x => x.product.price <= maxPrice);
- }
- return resultArr;
- } else {
- VALIDATOR.isString(obj);
- return this._productsH.filter(x => x.product.model.toLowerCase().indexOf(obj.toLowerCase()) >= 0 || x.product.manufacturer.toLowerCase().indexOf(obj.toLocaleLowerCase())>=0)
- }
- }
- }
- return {
- getSmartPhone(manufacturer, model, price, screenSize, operatingSystem) {
- // returns SmarhPhone instance
- return new SmartPhone(manufacturer, model, price, screenSize, operatingSystem);
- },
- getCharger(manufacturer, model, price, outputVoltage, outputCurrent) {
- // returns Charger instance
- return new Charger(manufacturer, model, price, outputVoltage, outputCurrent);
- },
- getRouter(manufacturer, model, price, wifiRange, lanPorts) {
- // returns Router instance
- return new Router(manufacturer, model, price, wifiRange, lanPorts);
- },
- getHeadphones(manufacturer, model, price, quality, hasMicrophone) {
- // returns Headphones instance
- return new Headphones(manufacturer, model, price, quality, hasMicrophone);
- },
- getHardwareStore(name) {
- // returns HardwareStore instance
- return new HardwareStore(name);
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement