[java] Lab - zadatak

Viewer

copydownloadembedprintName: Lab - zadatak
  1. //definicija
  2.  
  3.     ArrayList<T> idevi = new ArrayList<>();
  4.     ArrayList<U> valujuzi = new ArrayList<>();
  5.     T[] ids;
  6.     U[] values;
  7.     Integer[] amounts;
  8.  
  9.  
  10.  
  11. //problematicna metoda 
  12.  
  13. public void addProduct(T otherId, U otherValue, Integer otherAmount) {
  14.         if (doesIdExists(otherId)) {
  15.             throw new IllegalArgumentException(String.format("Id " + otherId + " exists!"));
  16.         } else {
  17.          counter++;
  18.  
  19.  
  20.         //Postojao je suppress warning kao u primjeru iz predavanja, ali 
  21.         //je i dalje bacalo ClassCastException
  22.  
  23.         //T[] noviIds = (T[]) new Object[ids.length + 1];
  24.         //U[] noviValues = (U[]) new Object[values.length + 1];
  25.         Integer[] noviAmounts = new Integer[ids.length + 1];
  26.  
  27.  
  28.         for (int i = 0; i < ids.length; i++) {
  29.             idevi.add(ids[i]);
  30.             valujuzi.add(values[i]);
  31.             //noviIds[i] = ids[i];
  32.             //noviValues[i] = values[i];
  33.             noviAmounts[i] = amounts[i];
  34.         }
  35.  
  36.         idevi.add(otherId);
  37.         valujuzi.add(otherValue);
  38.         //noviIds[ids.length] = otherId;
  39.         //noviValues[ids.length] = otherValue;
  40.         noviAmounts[ids.length] = otherAmount;
  41.  
  42.         //ids = noviIds;
  43.         //values = noviValues;
  44.         amounts = noviAmounts;
  45.         }
  46.     }
  47.  
  48.  
  49. //Fja za racunanje
  50.  
  51. public Double totalMarketValue() {
  52.         double sum = 0;
  53.         if(counter == 0) {
  54.             for (int i = 0; i < ids.length + counter; i++) {
  55.                 sum += values[i].doubleValue() * amounts[i];
  56.             }
  57.         }
  58.         if(counter > 0) {
  59.             for (int i = 0; i < idevi.size(); i++) {
  60.                 sum+= valujuzi.get(i).doubleValue() * amounts[i];
  61.             }
  62.         }
  63.         return sum;
  64.     }

Editor

You can edit this paste and save as new:


File Description
  • Lab - zadatak
  • Paste Code
  • 07 May-2024
  • 1.76 Kb
You can Share it: