Categories
JavaScript Телерик

JS OOP изпит – сутрешна група

Решение на задачата на сутрешната група на изпита по JavaScript OOP

// item.js
define(function() {
    'use strict';
    var Item;

    Item = (function() {
        var types = ['accessory', 'smart-phone', 'notebook', 'pc', 'tablet'];

        function Item(type, name, price) {
            if (types.indexOf(type) == -1) {
                throw "invalid item type";
            };

            if (name.length < 6 || name.length > 40) {
                throw "invalid item name";
            };

            if (price < 0.0) {
                throw "invalid item price";
            };
            this.type = type;
            this.name = name;
            this.price = price;
        }
        return Item;
    })();
    return Item;
});

// store.js 

define(function() {
    'use strict';
    var Store;
    Store = (function() {
        function Store(storeName) {
            if (storeName.length < 6 || storeName.length > 40) {
                throw "invalid store name";
            }

            this._name = storeName;
            this._items = [];
        };

        Store.prototype.addItem = function(item) {
            if (item.constructor.name !== "Item") {
                throw "invalid item type";
            }

            this._items.push(item);

            return this;
        };

        Store.prototype.getAll = function() {
            var sortedItems = compareBy(this._items, "name");

            return sortedItems;
        };

        Store.prototype.getSmartPhones = function() {
            var items = [];

            this._items.forEach(function(item) {
                if (item.type === 'smart-phone') {
                    items.push(item);
                }
            });

            var sortedItems = compareBy(items, "name");

            return sortedItems;
        };

        Store.prototype.getMobiles = function() {
            var items = [];

            this._items.forEach(function(item) {
                if (item.type === 'smart-phone' || item.type === 'tablet') {
                    items.push(item);
                }
            });

            var sortedItems = compareBy(items, "name");

            return sortedItems;
        };

        Store.prototype.getComputers = function() {
            var items = [];

            this._items.forEach(function(item) {
                if (item.type === 'pc' || item.type === 'notebook') {
                    items.push(item);
                }
            });

            var sortedItems = compareBy(items, "name");

            return sortedItems;
        };

        Store.prototype.filterItemsByType = function(type) {
            var items = [];

            this._items.forEach(function(item) {
                if (item.type === type) {
                    items.push(item);
                }
            });

            var sortedItems = compareBy(items, "name");

            return sortedItems;
        };

        Store.prototype.filterItemsByPrice = function(options) {
            var minPrice = 0;
            var maxPrice = 999999999.99;
            var items = [];

            if (options) {
                minPrice = options.min || minPrice;
                maxPrice = options.max || maxPrice;
            }

            this._items.forEach(function(item) {
                if (item.price >= minPrice && item.price <= maxPrice) {
                    items.push(item);
                }
            });
            var sortedItems = compareBy(items, "price");
            return sortedItems;
        };
        Store.prototype.countItemsByType = function() {
            var itemTypes = [];
            for (var i = 0; i < this._items.length; i++) {
                if (itemTypes[this._items[i].type]) {
                    itemTypes[this._items[i].type]++;
                } else {
                    itemTypes[this._items[i].type] = 1;
                }
            }
            return itemTypes;
        };
        Store.prototype.filterItemsByName = function(part) {
            var items = [];
            this._items.forEach(function(item) {
                if (item.name.toLowerCase().indexOf(part.toLowerCase()) >= 0) {
                    items.push(item);
                }
            });

            var sortedItems = compareBy(items, "name");

            return sortedItems;
        };

        function compareBy(data, field) {
            data.sort(function(a, b) {
                if (a[field] < b[field]) return -1;
                if (a[field] > b[field])
                    return 1;
                return 0;
            });

            return data;
        };

        return Store;
    })();

    return Store;
});