Advertisement
stanevplamen

Knapsack bag

Sep 21st, 2022
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.83 KB | Software | 0 0
  1. class Product {
  2.  
  3.     constructor(name, price, weight) {
  4.         this.name = name;
  5.         this.price = price;
  6.         this.weight = weight;
  7.     }
  8. }
  9.  
  10. class Basket {
  11.  
  12.     constructor() {
  13.         this.maxWeight = 10;
  14.         this.products = [];
  15.         this.totalWeight = 0;
  16.         this.totalPrice = 0;
  17.     }
  18.  
  19.     addProduct(prd) {
  20.         this.products.push(prd);
  21.     }
  22.  
  23.     copyProduct() {
  24.         var dpProducts = []
  25.         for(var i in this.products) {
  26.             var prd = this.products[i];
  27.             dpProducts.push(new Product(prd.name, prd.price, prd.weight));
  28.         }
  29.         return dpProducts;
  30.     }
  31.  
  32.     setProducts(products) {
  33.         this.products = products;
  34.     }
  35.  
  36.     getWeight() {
  37.         this.totalWeight = 0;
  38.         this.totalPrice = 0;
  39.         for(var r in this.products) {
  40.             var prd = this.products[r];
  41.             this.totalWeight = this.totalWeight + prd.weight;
  42.             this.totalPrice = this.totalPrice + prd.price;
  43.         }
  44.         return this.totalWeight ;
  45.     }
  46.  
  47.     displayProducts() {
  48.         console.log("------- basket start ------ total price: " + this.totalPrice);
  49.         for(var r in this.products) {
  50.             var prd = this.products[r];
  51.             console.log(" prd: " + prd.name + ", price: " + prd.price + ", weight: " + prd.weight);
  52.         }
  53.         console.log("------- basket end ------ ");
  54.     }
  55. }
  56.  
  57.  
  58. // what is the max price
  59. var prdsList = [
  60.     new Product("beer", 3, 2),
  61.     new Product("vodka", 8, 12),
  62.     new Product("cheese", 4, 5),
  63.     new Product("nuts", 1, 4),
  64.     new Product("ham", 2, 3),
  65.     new Product("wiskey", 8, 13),
  66.     new Product("pork", 10, 2),
  67.     new Product("duck", 9, 3),
  68.     new Product("chocko", 5, 4),
  69.     new Product("chips", 6, 5),
  70. ];
  71.  
  72. var counterIndex = 0;
  73. var productsLength = prdsList.length;
  74. var allResults = [];
  75. debugger;
  76. while(counterIndex < productsLength) {
  77.     var currentPrd = prdsList[counterIndex];
  78.     var tempList = [];
  79.     var newB = new Basket();
  80.     newB.addProduct(currentPrd);
  81.     tempList.push(newB);
  82.     for(var u in allResults) {
  83.         var cBasket = allResults[u];
  84.         var nBasket = new Basket();
  85.         nBasket.setProducts(cBasket.copyProduct());
  86.         nBasket.addProduct(currentPrd);
  87.         tempList.push(nBasket);
  88.     }
  89.     allResults = allResults.concat(tempList);
  90.     counterIndex++;
  91. }
  92.  
  93. console.log(allResults.length);
  94. debugger;
  95.  
  96. for(var s in allResults) {
  97.     var basket = allResults[s];
  98.     var cw = (basket.getWeight());
  99. }
  100.  
  101. function compare( a, b ) {
  102.     if ( a.totalPrice < b.totalPrice ){
  103.       return -1;
  104.     }
  105.     if ( a.totalPrice > b.totalPrice ){
  106.       return 1;
  107.     }
  108.     return 0;
  109.   }
  110.  
  111.   allResults.sort(compare);
  112.   debugger;
  113.  
  114.   for(var s in allResults) {
  115.     var basket = allResults[s];
  116.     var cw = (basket.getWeight());
  117.     if(cw <= 10) {
  118.         basket.displayProducts();
  119.     }
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement