[java] OOP Lab

Viewer

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

Editor

You can edit this paste and save as new:


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