How to add values to array based on condition? (read two files)

here is my version with two classes

// https://discourse.processing.org/t/how-to-add-values-to-array-based-on-condition-read-two-files/28340



ArrayList<City> cities = new ArrayList(); 
ArrayList<Migration> migrations = new ArrayList();

String[] countryData = 
  {
  "country,population", 
  "countryA,100000", 
  "countryB,200000", 
  "countryC,300000" 
};

String[] migData = 
  {
  "country_origin,country_dest,mig1990,mig1995,mig2000,mig2005", 
  "countryA,countryB,100,100,100,100", 
  "countryA,countryC,200,200,200,200", 
  "countryB,countryA,300,300,300,300", 
  "countryB,countryC,400,400,400,400"
};

void setup() {
  size(1660, 860);
  background(111); 

  // read data 
  for (String stringOneLineCity : countryData) {
    String[] sLineParts=split(stringOneLineCity, ",");

    if (!sLineParts[1].equals("population")) {
      City newCity = new City (sLineParts[0], sLineParts[1]);
      cities.add(newCity);
    }//if
  }//for

  // read data 
  for (String stringOneLineMigration : migData) {
    String[] sLineParts=split(stringOneLineMigration, ",");

    int from = getCity(sLineParts[0]);
    int to =   getCity(sLineParts[1]); 
    if ( ! (from == -1 || to == -1) ) {
      Migration newMigration = new Migration (from, to, 
        sLineParts[2], sLineParts[3], 
        sLineParts[4], sLineParts[5] );
      migrations.add(newMigration);
    }//if
  }//for
}//func

void draw() {
  background(111); 
  // display 1
  for (City c : cities) {
    c.display();
  }

  // display 2
  for (Migration m : migrations) {
    m.display();
  }
}//func

int getCity(String c_) {
  // returns the index of a city in the ArrayList cities
  // based on the name of the city 
  int i=0; 
  for (City c : cities) {
    if (c.name.equals(c_))
      return i;
    i++;
  }
  println("getCity failed with " 
    +c_);
  return -1;
}

// ==================================================================================

class City {

  float posx=random(100, width-100), 
    posy=random(100, height-100);
  String name="";
  int pop=0; 
  color col=color(random(255), random(255), random(255) ); 

  //constr 
  City(String name_, String pop_) {
    name=name_;
    pop=int(pop_);
  }//constr 

  void display() {
    //
    strokeWeight(1); 
    fill(col); 
    ellipse(posx, posy, 
      pop/1000, pop/1000);
  }//
  //
}//class

// ==================================================================================

class Migration {

  int indexFromCity; 
  int indexToCity;

  String[] migData; 

  //constr 
  Migration(int from, int to, 
    String... vals) {
    //
    indexFromCity=from; 
    indexToCity=to;
    migData = vals;
  }//constr 

  void display() {
    City city1 = cities.get(indexFromCity); 
    City city2 = cities.get(indexToCity); 

    stroke(0); 

    strokeWeight(map(int(migData[0]), 
      0, 400, 1, 15) ); 

    line(city1.posx, city1.posy, 
      city2.posx, city2.posy);

    //reset 
    strokeWeight(1);
  }
  //
}//class
//

1 Like