/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package inport; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * * @author topazh_ag */ public class CargoFlow { private Storage storage; private Cargo cargo; Map flow; public Map getFlow() { return flow; } public void setFlow(Map flow) { this.flow = flow; } /** * Get the value of storage * * @return the value of storage */ public Storage getStorage() { return storage; } /** * Set the value of storage * * @param storage new value of storage */ public void setStorage(Storage storage) { this.storage = storage; } public Cargo getCargo() { return cargo; } public void setCargo(Cargo cargo) { this.cargo = cargo; } public CargoFlow() { this.flow = new HashMap<>(); } public CargoFlow(Storage storage, Cargo cargo) { this.storage = storage; this.cargo = cargo; this.flow = new HashMap<>(); } public double getCurrentValue(double forTime) { double res = 0.0; boolean isFound = false; Set keyTimes = flow.keySet(); double prevKey = -1.0; for (Double keyTime : keyTimes) { if (forTime>prevKey && forTimeprevKey) res = flow.get(prevKey); return res; } public double getTotalValue(double forTime) { double res = 0.0; boolean isFound = false; Set keyTimes = flow.keySet(); double prevKey = -1.0; for (Double keyTime : keyTimes) { if (forTime>prevKey && forTimeprevKey) res += flow.get(prevKey)*(forTime-prevKey); return res; } @Override public String toString() { String res = ""; boolean first = true; for(Double s : flow.keySet()) { if (!first) res += "," + s+ ":" + flow.get(s); else res += s + ":" + flow.get(s); first = false; } return storage.getId() + ";[" + res +"]"; } public CargoFlow(String s, Map mp, Map cp) { this.flow = new HashMap<>(); String[] tokens = s.split(";"); int key = Integer.parseInt(tokens[0].trim()); storage = mp.get(key); key = Integer.parseInt(tokens[1].trim()); cargo = cp.get(key); String[] rs = tokens[2].trim().replace("[", "").replace("]", "").split(","); for (String crs : rs) if (crs.length()>0) { String[] kv = crs.split(":"); Double key1 = Double.parseDouble(kv[0].trim()); Double value1 = Double.parseDouble(kv[1].trim()); flow.put(key1, value1); } } }