[java] Lab - zadatak
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- //definicija
- ArrayList<T> idevi = new ArrayList<>();
- ArrayList<U> valujuzi = new ArrayList<>();
- T[] ids;
- U[] values;
- Integer[] amounts;
- //problematicna metoda
- public void addProduct(T otherId, U otherValue, Integer otherAmount) {
- if (doesIdExists(otherId)) {
- throw new IllegalArgumentException(String.format("Id " + otherId + " exists!"));
- } else {
- counter++;
- //Postojao je suppress warning kao u primjeru iz predavanja, ali
- //je i dalje bacalo ClassCastException
- //T[] noviIds = (T[]) new Object[ids.length + 1];
- //U[] noviValues = (U[]) new Object[values.length + 1];
- Integer[] noviAmounts = new Integer[ids.length + 1];
- for (int i = 0; i < ids.length; i++) {
- idevi.add(ids[i]);
- valujuzi.add(values[i]);
- //noviIds[i] = ids[i];
- //noviValues[i] = values[i];
- noviAmounts[i] = amounts[i];
- }
- idevi.add(otherId);
- valujuzi.add(otherValue);
- //noviIds[ids.length] = otherId;
- //noviValues[ids.length] = otherValue;
- noviAmounts[ids.length] = otherAmount;
- //ids = noviIds;
- //values = noviValues;
- amounts = noviAmounts;
- }
- }
- //Fja za racunanje
- public Double totalMarketValue() {
- double sum = 0;
- if(counter == 0) {
- for (int i = 0; i < ids.length + counter; i++) {
- sum += values[i].doubleValue() * amounts[i];
- }
- }
- if(counter > 0) {
- for (int i = 0; i < idevi.size(); i++) {
- sum+= valujuzi.get(i).doubleValue() * amounts[i];
- }
- }
- return sum;
- }
Editor
You can edit this paste and save as new: