Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Product {
- constructor(name, price, weight) {
- this.name = name;
- this.price = price;
- this.weight = weight;
- }
- }
- class Basket {
- constructor() {
- this.maxWeight = 10;
- this.products = [];
- this.totalWeight = 0;
- this.totalPrice = 0;
- }
- addProduct(prd) {
- this.products.push(prd);
- }
- copyProduct() {
- var dpProducts = []
- for(var i in this.products) {
- var prd = this.products[i];
- dpProducts.push(new Product(prd.name, prd.price, prd.weight));
- }
- return dpProducts;
- }
- setProducts(products) {
- this.products = products;
- }
- getWeight() {
- this.totalWeight = 0;
- this.totalPrice = 0;
- for(var r in this.products) {
- var prd = this.products[r];
- this.totalWeight = this.totalWeight + prd.weight;
- this.totalPrice = this.totalPrice + prd.price;
- }
- return this.totalWeight ;
- }
- displayProducts() {
- console.log("------- basket start ------ total price: " + this.totalPrice);
- for(var r in this.products) {
- var prd = this.products[r];
- console.log(" prd: " + prd.name + ", price: " + prd.price + ", weight: " + prd.weight);
- }
- console.log("------- basket end ------ ");
- }
- }
- // what is the max price
- var prdsList = [
- new Product("beer", 3, 2),
- new Product("vodka", 8, 12),
- new Product("cheese", 4, 5),
- new Product("nuts", 1, 4),
- new Product("ham", 2, 3),
- new Product("wiskey", 8, 13),
- new Product("pork", 10, 2),
- new Product("duck", 9, 3),
- new Product("chocko", 5, 4),
- new Product("chips", 6, 5),
- ];
- var counterIndex = 0;
- var productsLength = prdsList.length;
- var allResults = [];
- debugger;
- while(counterIndex < productsLength) {
- var currentPrd = prdsList[counterIndex];
- var tempList = [];
- var newB = new Basket();
- newB.addProduct(currentPrd);
- tempList.push(newB);
- for(var u in allResults) {
- var cBasket = allResults[u];
- var nBasket = new Basket();
- nBasket.setProducts(cBasket.copyProduct());
- nBasket.addProduct(currentPrd);
- tempList.push(nBasket);
- }
- allResults = allResults.concat(tempList);
- counterIndex++;
- }
- console.log(allResults.length);
- debugger;
- for(var s in allResults) {
- var basket = allResults[s];
- var cw = (basket.getWeight());
- }
- function compare( a, b ) {
- if ( a.totalPrice < b.totalPrice ){
- return -1;
- }
- if ( a.totalPrice > b.totalPrice ){
- return 1;
- }
- return 0;
- }
- allResults.sort(compare);
- debugger;
- for(var s in allResults) {
- var basket = allResults[s];
- var cw = (basket.getWeight());
- if(cw <= 10) {
- basket.displayProducts();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement