/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package inport; import inport.ConversionUtils.Pair; import java.util.ArrayList; import java.util.Map; /** * * @author topazh_ag */ public class Storage { private int id; private String name; private ArrayList> storageSections = new ArrayList<>(); public ArrayList> getStorageSections() { return storageSections; } public void setStorageSections(ArrayList> storageSections) { this.storageSections = storageSections; } /** * Get the value of name * * @return the value of name */ public String getName() { return name; } /** * Set the value of name * * @param name new value of name */ public void setName(String name) { this.name = name; } /** * Get the value of id * * @return the value of id */ public int getId() { return id; } /** * Set the value of id * * @param id new value of id */ public void setId(int id) { this.id = id; } public Storage(int id, String name, double volume, ArrayList> storageSections) { this.id = id; this.name = name; this.storageSections = storageSections; } public Storage() { } @Override public String toString() { return id + "; " + name + "; " + storageSectionsToString(storageSections); } public Storage(String s, Map cargoes) { String[] tokens = s.split(";"); id = Integer.parseInt(tokens[0].trim()); name = tokens[1].trim(); storageSections = storageSectionsFromString(tokens[2], cargoes); } static String storageSectionsToString(ArrayList> storageSections) { ArrayList sections = new ArrayList<>(); for (Pair p : storageSections) { sections.add("(" + p.getKey().getId() + ", " + p.getValue() + ")"); } return "[" + String.join(", ",sections) + "]"; } static ArrayList> storageSectionsFromString(String str, Map cargoes) { ArrayList> storageSections = new ArrayList<>(); String sections = str.substring(str.indexOf('[') + 1, str.indexOf(']')); for (int pos = 0; pos < sections.length(); ) { int p1 = sections.indexOf('(', pos); int p2 = sections.indexOf(')', pos); if ((p1 >= 0) && (p2 >= 0) && (p1 < p2)) { int type = Integer.parseInt(sections.substring(p1 + 1, sections.indexOf(',', p1)).trim()); double cargoMax = Double.parseDouble(sections.substring(sections.indexOf(',', p1) + 1, p2).trim()); storageSections.add(new Pair<>(cargoes.get(type), cargoMax)); pos = p2 + 1; } else { break; } } return storageSections; } }