Last active
April 20, 2016 14:08
-
-
Save crystalclear/bdd0aa419a93b8be06cdd78150cfa9d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Product = (function(){ | |
var name = "", | |
description = "", | |
price = 0, | |
stock = 0, | |
manufacturer = "", | |
category = 0; | |
var _this = this; | |
return { | |
Stock: this.stock, | |
// Getter | |
getProductName: function() { return this.name; }, | |
getDescription: function() { return this.description; }, | |
getProductPrice: function() { return this.price; }, | |
getManufacturer: function() { return this.manufacturer; }, | |
getCategory: function() { return this.category; }, | |
getStock: function() { return this.stock; }, | |
// Setter | |
setProductName: function(name) { this.name = name; return this; }, | |
setDescription: function(descritpion) { this.description = descritpion; return this; }, | |
setProductPrice: function(price) { this.price = price; return this; }, | |
setManufacturer: function(manufacturer) { this.manufacturer = manufacturer; return this; }, | |
setCategory: function(category) { this.category = category; return this; }, | |
setStock: function(stock) { this.stock = stock; return this; }, | |
// Misc Functions | |
reducePrice: function(reduceVal) { | |
var tmpPrice = this.price; | |
if(tmpPrice>0) { | |
this.price = tmpPrice - (tmpPrice * parseInt(reduceVal) / 100); | |
} | |
} | |
} | |
}); | |
var products = []; | |
products.push( new Product().setProductName("Apple Macbook Pro 15") | |
.setDescription("Profesional arbeiten mit dem neuen Macbook von Apple.") | |
.setProductPrice(1500) | |
.setCategory("Notebooks") | |
.setStock(3) | |
.setManufacturer("Apple")); | |
products.push( new Product().setProductName("Dell Latitude E6420") | |
.setDescription("Dell Latitude E6420 Core i7-2620M 2,70 GHz 256 GB SSD BACKLIT-TASTATUR 1600 x 900 WEBCAM.") | |
.setProductPrice(800) | |
.setCategory("Notebooks") | |
.setStock(13) | |
.setManufacturer("Dell")); | |
products.push(new Product().setProductName("Apple iPad Pro") | |
.setDescription("Auf der Couch oder für den Büroalltag.") | |
.setProductPrice(499) | |
.setCategory("Tablets") | |
.setStock(10) | |
.setManufacturer("Apple Inc.")); | |
var getList = function(products) { | |
var list = []; | |
products.forEach(function(product, index, array){ | |
list.push('Artikel (' + (index).toString() + '): ' + product.getProductName() + ' für unschlagbare ' + product.getProductPrice() + '\n' ); | |
}); | |
return list.join(); | |
} | |
var Print = function(out) { | |
console.log(out); | |
}; | |
Print('Products: \n' + getList(products)); | |
// Sortieren: | |
var compareFunc = function(a,b) { | |
console.log('a: ' + a.getProductPrice() ); | |
console.log('b: ' + b.getProductPrice() ); | |
return a.getProductPrice() - b.getProductPrice(); | |
} | |
products.sort(compareFunc); | |
// Alle Artikel über 1000 EUR um 10% reduzieren | |
var priceMarge = 1000; | |
var saleProducts = (products.filter( f => f.getProductPrice() > priceMarge )).map( (p) => { p.reducePrice(10); return p; } ) ; | |
Print('For Sale: \n' + getList(saleProducts)); | |
// Anzahl Notebooks auf Lager: | |
Print('ANZAHL: '+(products.length).toString()) | |
var amount = products.reduce( (prev, current) => { return prev.Stock + current.Stock; }); | |
Print('Menge: \n' + amount); | |
//var elem = document.getElementById("content"); | |
//elem.innerHTML = "Liste aller Artikel"; | |
//elem.innerHTML = getList(products); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment