Advertisement
Guest User

Untitled

a guest
Sep 12th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     class Product {
  3.         constructor(productType, name, price) {
  4.             this.productType = productType;
  5.             this.name = name;
  6.             this.price = price;
  7.         }
  8.  
  9.         get productType() {
  10.             return this._productType;
  11.         }
  12.  
  13.         set productType(value) {
  14.             if (typeof(value) !== 'string') {
  15.                 throw new Error('Product type is not a string');
  16.             }
  17.             this._productType = value;
  18.         }
  19.  
  20.         get name() {
  21.             return this._name;
  22.         }
  23.  
  24.         set name(value) {
  25.             if (typeof(value) !== 'string') {
  26.                 throw new Error('Name is not a string');
  27.             }
  28.             this._name = value;
  29.         }
  30.  
  31.         get price() {
  32.             return this._price;
  33.         }
  34.  
  35.         set price(value) {
  36.             if (typeof(value) !== 'number') {
  37.                 throw new Error('Price is not a number');
  38.             }
  39.             this._price = value;
  40.         }
  41.     }
  42.  
  43.     Array.prototype.getUnique = function () {
  44.         var u = {}, a = [];
  45.         for (var i = 0, l = this.length; i < l; ++i) {
  46.             if (u.hasOwnProperty(this[i])) {
  47.                 continue;
  48.             }
  49.             a.push(this[i]);
  50.             u[this[i]] = 1;
  51.         }
  52.         return a;
  53.     }
  54.  
  55.     class ShoppingCart {
  56.         constructor() {
  57.             this.products = [];
  58.         }
  59.  
  60.         add(product) {
  61.             let isValid = false;
  62.  
  63.             if (product instanceof Product) {
  64.                 isValid = true;
  65.             } else if (product.name && product.productType && product.price) {
  66.                 isValid = true;
  67.             }
  68.  
  69.             if (isValid) {
  70.                 this.products.push(product);
  71.                 return this;
  72.             } else {
  73.                 throw new Error('Not a instance of product or product-like object!');
  74.             }
  75.         }
  76.  
  77.         remove(product) {
  78.             let isValid = false;
  79.  
  80.             if (product instanceof Product) {
  81.                 isValid = true;
  82.             } else if (product.name && product.productType && product.price) {
  83.                 isValid = true;
  84.             }
  85.  
  86.             if (isValid) {
  87.                 let contains = this.products.indexOf(product) > -1;
  88.  
  89.                 if (!contains) {
  90.                     throw new Error('The product does not exist!');
  91.                 } else if (this.products.length === 0) {
  92.                     throw new Error('No products in the shopping cart!');
  93.                 } else {
  94.                     let index = this.products.indexOf(product);
  95.                     this.products.splice(index, 1);
  96.                 }
  97.             } else {
  98.                 throw Error();
  99.             }
  100.         }
  101.  
  102.         showCost() {
  103.             if (this.products.length === 0) {
  104.                 return 0;
  105.             } else {
  106.                 let sum = 0;
  107.  
  108.                 this.products.forEach(i => sum += i.price);
  109.                 return sum;
  110.             }
  111.         }
  112.  
  113.         showProductTypes() {
  114.             if (this.products.length === 0) {
  115.                 return [];
  116.             }
  117.  
  118.             let set = new Set();
  119.             let result = this.products.getUnique();
  120.  
  121.             this.products.forEach(p => set.add(p.productType));
  122.  
  123.             return Array.from(set).sort((a, b) => {
  124.                 if (a > b) {
  125.                     return 1;
  126.                 } else if (a < b) {
  127.                     return -1;
  128.                 }
  129.  
  130.                 return 0;
  131.             });
  132.         }
  133.  
  134.         getInfo() {
  135.             if (this.products.length === 0) {
  136.                 return {totalPrice: 0, products: []}
  137.             }
  138.  
  139.             let totalSum = 0;
  140.             this.products.forEach(p => totalSum += p.price);
  141.             let productsSortedByName = this.products.sort((a, b) => {
  142.                 if (a.name > b.name) {
  143.                     return 1;
  144.                 } else if (a.name < b.name) {
  145.                     return -1;
  146.                 }
  147.  
  148.                 return 0;
  149.             });
  150.  
  151.             let groups = [];
  152.             let current = {};
  153.             current.name = this.products[0].name;
  154.             current.price = this.products[0].price;
  155.             current.productType = this.products[0].productType;
  156.  
  157.             for (let i = 1, len = this.products.length; i < len - 1; i += 1) {
  158.                 let product = this.products[i];
  159.  
  160.                 if (product.name === current.name && product.productType === current.productType) {
  161.                     current.name = product.name;
  162.                     current.productType = product.productType;
  163.                     current.price += product.price;
  164.                 } else if (i === len - 1 && product.name === current.name || product.productType === current.productType) {
  165.                     current.price += product.price;
  166.                     current.productType = product.productType;
  167.                     groups.push(current);
  168.                     continue;
  169.                 } else if (i === len - 1 && product.name !== current.name) {
  170.                     groups.push({name: product.name, price: product.price, productType: product.productType});
  171.                     continue;
  172.                 } else if (product.name !== current.name) {
  173.                     groups.push(current);
  174.                     current.name = '';
  175.                     current.price = 0;
  176.                     current.productType = '';
  177.                 }
  178.             }
  179.  
  180.             groups.push({});
  181.             return {products: groups, totalPrice: totalSum};
  182.         }
  183.     }
  184.     return {
  185.         Product, ShoppingCart
  186.     };
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement