Program: 173698
Project: 173698
Buggy source:
private void formCGR(){
  Player player;
  Portfolio portfolio;
  int count, cgrSharesUsed, oldShares, newShares;
  PublicCertificateI cgrCert, poolCert;
  List<PublicCertificateI> certs=new ArrayList<PublicCertificateI>();
  Player temporaryPresident=null;
  Player newPresident=null;
  Player firstCGRowner=null;
  int maxShares=0;
  setCurrentPlayer(startingPlayer);
  cgrSharesUsed=0;
  ReportBuffer.add("String_Node_Str");
  do {
    player=getCurrentPlayer();
    portfolio=player.getPortfolio();
    oldShares=newShares=0;
    certs.clear();
    poolCert=null;
    for (    PublicCertificateI cert : player.getPortfolio().getCertificates()) {
      if (mergingCompanies.contains(cert.getCompany())) {
        certs.add((cert));
        oldShares++;
        if (cert.isPresidentShare()) {
          oldShares++;
        }
      }
    }
    if (oldShares > 0) {
      count=oldShares;
      if (count >= 4 && temporaryPresident == null && cgrSharesUsed <= 18) {
        cgrCert=cgr.getPresidentsShare();
        cgrCert.moveTo(portfolio);
        count-=4;
        cgrSharesUsed+=2;
        newShares+=2;
        temporaryPresident=player;
      }
      while (count >= 2 && cgrSharesUsed <= 19) {
        cgrCert=unavailable.findCertificate(cgr,false);
        cgrCert.moveTo(portfolio);
        count-=2;
        cgrSharesUsed++;
        newShares++;
      }
      String message=LocalText.getText("String_Node_Str",player.getName(),oldShares,newShares,PublicCompany_CGR.NAME);
      DisplayBuffer.add(message,false);
      ReportBuffer.add(message);
      if (count == 1) {
        poolCert=certs.get(certs.size() - 1);
        poolCert.moveTo(pool);
        certs.remove(poolCert);
        message=LocalText.getText("String_Node_Str",player.getName());
        DisplayBuffer.add(message,false);
        ReportBuffer.add(message);
      }
      if (firstCGRowner == null)       firstCGRowner=player;
      if (newShares > maxShares) {
        maxShares=newShares;
        newPresident=player;
      }
    }
    gameManager.setNextPlayer();
  }
 while (getCurrentPlayer() != startingPlayer);
  certs.clear();
  oldShares=newShares=0;
  for (  PublicCertificateI cert : pool.getCertificates()) {
    if (mergingCompanies.contains(cert.getCompany())) {
      certs.add((cert));
      oldShares++;
    }
  }
  count=oldShares;
  while (count >= 2 && cgrSharesUsed <= 19) {
    cgrCert=unavailable.findCertificate(cgr,false);
    cgrCert.moveTo(pool);
    count-=2;
    cgrSharesUsed++;
    newShares++;
  }
  String message=LocalText.getText("String_Node_Str",LocalText.getText("String_Node_Str"),oldShares,newShares,PublicCompany_CGR.NAME);
  DisplayBuffer.add(message);
  ReportBuffer.add(message);
  for (  PublicCertificateI discardCert : certs) {
    discardCert.moveTo(scrapHeap);
  }
  log.info(cgrSharesUsed + "String_Node_Str");
  if (cgrSharesUsed <= 10) {
    cgr.setShareUnit(10);
  }
  message=LocalText.getText("String_Node_Str",cgr.getName(),100 / cgr.getShareUnit(),cgr.getShareUnit());
  DisplayBuffer.add(message);
  ReportBuffer.add(message);
  certs=new ArrayList<PublicCertificateI>(unavailable.getCertificatesPerCompany(PublicCompany_CGR.NAME));
  for (  PublicCertificateI cert : certs) {
    cert.moveTo(ipo);
  }
  if (newPresident.getPortfolio().getShare(cgr) == cgr.getShareUnit()) {
    log.debug("String_Node_Str" + firstCGRowner.getName());
    cgr.setTemporaryPresident(firstCGRowner);
    newPresident=firstCGRowner;
  }
 else   if (temporaryPresident != null && temporaryPresident != newPresident) {
    log.debug("String_Node_Str" + temporaryPresident.getName() + "String_Node_Str"+ newPresident.getName());
    temporaryPresident.getPortfolio().swapPresidentCertificate(cgr,newPresident.getPortfolio());
  }
  newPresident.getPortfolio().getShareModel(cgr).setShare();
  message=LocalText.getText("String_Node_Str",newPresident.getName(),cgrName);
  ReportBuffer.add(message);
  DisplayBuffer.add(message);
  int lowestPrice=999;
  int totalPrice=0;
  int price;
  int numberMerged=mergingCompanies.size();
  for (  PublicCompanyI comp : mergingCompanies) {
    price=comp.getMarketPrice();
    totalPrice+=price;
    if (price < lowestPrice)     lowestPrice=price;
  }
  if (numberMerged >= 3) {
    totalPrice-=lowestPrice;
    numberMerged--;
  }
  int cgrPrice=Math.max(100,(((totalPrice / numberMerged) / 5)) * 5);
  if (cgrPrice == 100) {
    cgr.start(100);
  }
 else {
    int prevColPrice=100;
    int colPrice;
    StockSpaceI startSpace;
    for (int col=6; col <= stockMarket.getNumberOfColumns(); col++) {
      colPrice=stockMarket.getStockSpace(1,col).getPrice();
      if (cgrPrice > colPrice)       continue;
      if (cgrPrice - prevColPrice < colPrice - cgrPrice) {
        startSpace=stockMarket.getStockSpace(1,col - 1);
      }
 else {
        startSpace=stockMarket.getStockSpace(1,col);
      }
      cgr.start(startSpace);
      message=LocalText.getText("String_Node_Str",PublicCompany_CGR.NAME,Bank.format(startSpace.getPrice()),startSpace.getName());
      DisplayBuffer.add(message);
      ReportBuffer.add(message);
      break;
    }
  }
  cgr.setFloated();
  ReportBuffer.add(LocalText.getText("String_Node_Str",PublicCompany_CGR.NAME));
  int numCompanies=Math.min(11,12 - mergingCompanies.size());
  int numPlayers=gameManager.getNumberOfPlayers();
  int newCertLimit=certLimitsTable[numPlayers - 3][numCompanies - 4];
  gameManager.setPlayerCertificateLimit(newCertLimit);
  message=LocalText.getText("String_Node_Str",newCertLimit,numPlayers,numCompanies);
  DisplayBuffer.add(message);
  ReportBuffer.add(message);
  List<BaseToken> homeTokens=new ArrayList<BaseToken>();
  nonHomeTokens=new ArrayList<BaseToken>();
  BaseToken bt;
  MapHex hex;
  City city;
  for (  PublicCompanyI comp : mergingCompanies) {
    for (    TokenI token : comp.getTokens()) {
      if (token instanceof BaseToken) {
        bt=(BaseToken)token;
        if (!bt.isPlaced())         continue;
        city=(City)bt.getHolder();
        hex=city.getHolder();
        if (hex == comp.getHomeHex()) {
          homeTokens.add(bt);
        }
 else {
          nonHomeTokens.add(bt);
        }
      }
    }
    if (comp.getCash() > 0) {
      new CashMove(comp,cgr,comp.getCash());
    }
    List<TrainI> trains=new ArrayList<TrainI>(comp.getPortfolio().getTrainList());
    for (    TrainI train : trains) {
      train.moveTo(cgr.getPortfolio());
      if (train.getType().isPermanent())       cgr.setHadPermanentTrain(true);
    }
    if (comp.getBonuses() != null) {
      List<Bonus> bonuses=new ArrayList<Bonus>(comp.getBonuses());
      bonuses:       for (      Bonus bonus : bonuses) {
        comp.removeBonus(bonus);
        if (cgr.getBonuses() != null) {
          for (          Bonus b : cgr.getBonuses()) {
            if (b.equals(bonus)) {
              List<SellBonusToken> commonSP=gameManager.getSpecialProperties(SellBonusToken.class,true);
              if (commonSP != null) {
                for (                SellBonusToken sp : commonSP) {
                  if (sp.getName().equalsIgnoreCase(b.getName())) {
                    sp.makeResellable();
                    log.debug("String_Node_Str" + b.getName() + "String_Node_Str");
                    break;
                  }
                }
              }
              log.debug("String_Node_Str" + b.getName() + "String_Node_Str"+ cgrName);
              continue bonuses;
            }
          }
        }
        cgr.addBonus(new Bonus(cgr,bonus.getName(),bonus.getValue(),bonus.getLocations()));
      }
    }
  }
  ReportBuffer.add("String_Node_Str");
  for (  BaseToken token : homeTokens) {
    city=(City)token.getHolder();
    hex=city.getHolder();
    token.moveTo(token.getCompany());
    if (hex.layBaseToken(cgr,city.getNumber())) {
      ReportBuffer.add(LocalText.getText("String_Node_Str",cgrName,token.getCompany().getName(),city.getName()));
      cgr.layBaseToken(hex,0);
    }
  }
  for (  BaseToken token : new ArrayList<BaseToken>(nonHomeTokens)) {
    city=(City)token.getHolder();
    hex=city.getHolder();
    List<BaseToken> otherTokens=hex.getBaseTokens();
    if (otherTokens != null) {
      for (      BaseToken token2 : otherTokens) {
        if (token2.getCompany() == cgr || nonHomeTokens.contains(token2) && token2 != token) {
          ReportBuffer.add(LocalText.getText("String_Node_Str",cgrName,token.getCompany().getName(),city.getName()));
          token.moveTo(token.getCompany());
          nonHomeTokens.remove(token);
          break;
        }
      }
    }
  }
  if (homeTokens.size() + nonHomeTokens.size() > cgr.getNumberOfBaseTokens()) {
    Map<String,String> oldTokens=new HashMap<String,String>();
    String cityName;
    for (    BaseToken token : nonHomeTokens) {
      if (token.getHolder() instanceof City) {
        cityName=token.getHolder().getName();
        if (oldTokens.containsKey(cityName)) {
          oldTokens.put(cityName,oldTokens.get(cityName) + "String_Node_Str" + token.getCompany().getName());
        }
 else {
          oldTokens.put(cityName,token.getCompany().getName());
        }
      }
    }
    tokensToExchangeFrom=new ArrayList<ExchangeableToken>();
    for (    String key : new TreeSet<String>(oldTokens.keySet())) {
      tokensToExchangeFrom.add(new ExchangeableToken(key,oldTokens.get(key)));
    }
  }
 else {
    executeExchangeTokens(nonHomeTokens);
  }
  for (  PublicCompanyI comp : mergingCompanies) {
    comp.setClosed();
  }
  int trainLimit=cgr.getTrainLimit(gameManager.getCurrentPlayerIndex());
  List<TrainI> trains=cgr.getPortfolio().getTrainList();
  if (cgr.getNumberOfTrains() > trainLimit) {
    ReportBuffer.add("String_Node_Str");
    int numberToDiscard=cgr.getNumberOfTrains() - trainLimit;
    List<TrainI> trainsToDiscard=new ArrayList<TrainI>(4);
    for (    TrainI train : trains) {
      if (!train.getType().isPermanent()) {
        trainsToDiscard.add(train);
        if (--numberToDiscard == 0)         break;
      }
    }
    for (    TrainI train : trainsToDiscard) {
      train.moveTo(pool);
      ReportBuffer.add(LocalText.getText("String_Node_Str",cgrName,train.getName()));
    }
  }
}

Fixed version (ground truth):
private void formCGR(){
  Player player;
  Portfolio portfolio;
  int count, cgrSharesUsed, oldShares, newShares;
  PublicCertificateI cgrCert, poolCert;
  List<PublicCertificateI> certs=new ArrayList<PublicCertificateI>();
  Player temporaryPresident=null;
  Player newPresident=null;
  Player firstCGRowner=null;
  int maxShares=0;
  setCurrentPlayer(startingPlayer);
  cgrSharesUsed=0;
  ReportBuffer.add("String_Node_Str");
  do {
    player=getCurrentPlayer();
    portfolio=player.getPortfolio();
    oldShares=newShares=0;
    certs.clear();
    poolCert=null;
    for (    PublicCertificateI cert : player.getPortfolio().getCertificates()) {
      if (mergingCompanies.contains(cert.getCompany())) {
        certs.add((cert));
        oldShares++;
        if (cert.isPresidentShare()) {
          oldShares++;
        }
      }
    }
    if (oldShares > 0) {
      count=oldShares;
      if (count >= 4 && temporaryPresident == null && cgrSharesUsed <= 18) {
        cgrCert=cgr.getPresidentsShare();
        cgrCert.moveTo(portfolio);
        count-=4;
        cgrSharesUsed+=2;
        newShares+=2;
        temporaryPresident=player;
      }
      while (count >= 2 && cgrSharesUsed <= 19) {
        cgrCert=unavailable.findCertificate(cgr,false);
        cgrCert.moveTo(portfolio);
        count-=2;
        cgrSharesUsed++;
        newShares++;
      }
      String message=LocalText.getText("String_Node_Str",player.getName(),oldShares,newShares,PublicCompany_CGR.NAME);
      DisplayBuffer.add(message,false);
      ReportBuffer.add(message);
      if (count == 1) {
        poolCert=certs.get(certs.size() - 1);
        poolCert.moveTo(pool);
        certs.remove(poolCert);
        message=LocalText.getText("String_Node_Str",player.getName());
        DisplayBuffer.add(message,false);
        ReportBuffer.add(message);
      }
      if (firstCGRowner == null)       firstCGRowner=player;
      if (newShares > maxShares) {
        maxShares=newShares;
        newPresident=player;
      }
    }
    gameManager.setNextPlayer();
  }
 while (getCurrentPlayer() != startingPlayer);
  certs.clear();
  oldShares=newShares=0;
  for (  PublicCertificateI cert : pool.getCertificates()) {
    if (mergingCompanies.contains(cert.getCompany())) {
      certs.add((cert));
      oldShares++;
    }
  }
  count=oldShares;
  while (count >= 2 && cgrSharesUsed <= 19) {
    cgrCert=unavailable.findCertificate(cgr,false);
    cgrCert.moveTo(pool);
    count-=2;
    cgrSharesUsed++;
    newShares++;
  }
  String message=LocalText.getText("String_Node_Str",LocalText.getText("String_Node_Str"),oldShares,newShares,PublicCompany_CGR.NAME);
  DisplayBuffer.add(message);
  ReportBuffer.add(message);
  for (  PublicCertificateI discardCert : certs) {
    discardCert.moveTo(scrapHeap);
  }
  log.info(cgrSharesUsed + "String_Node_Str");
  if (cgrSharesUsed <= 10) {
    cgr.setShareUnit(10);
  }
  message=LocalText.getText("String_Node_Str",cgr.getName(),100 / cgr.getShareUnit(),cgr.getShareUnit());
  DisplayBuffer.add(message);
  ReportBuffer.add(message);
  certs=new ArrayList<PublicCertificateI>(unavailable.getCertificatesPerCompany(PublicCompany_CGR.NAME));
  for (  PublicCertificateI cert : certs) {
    cert.moveTo(ipo);
  }
  if (newPresident.getPortfolio().getShare(cgr) == cgr.getShareUnit()) {
    log.debug("String_Node_Str" + firstCGRowner.getName());
    cgr.setTemporaryPresident(firstCGRowner);
    newPresident=firstCGRowner;
  }
 else   if (temporaryPresident != null && temporaryPresident != newPresident) {
    log.debug("String_Node_Str" + temporaryPresident.getName() + "String_Node_Str"+ newPresident.getName());
    temporaryPresident.getPortfolio().swapPresidentCertificate(cgr,newPresident.getPortfolio());
  }
  newPresident.getPortfolio().getShareModel(cgr).setShare();
  message=LocalText.getText("String_Node_Str",newPresident.getName(),cgrName);
  ReportBuffer.add(message);
  DisplayBuffer.add(message);
  int lowestPrice=999;
  int totalPrice=0;
  int price;
  int numberMerged=mergingCompanies.size();
  for (  PublicCompanyI comp : mergingCompanies) {
    price=comp.getMarketPrice();
    totalPrice+=price;
    if (price < lowestPrice)     lowestPrice=price;
  }
  if (numberMerged >= 3) {
    totalPrice-=lowestPrice;
    numberMerged--;
  }
  int cgrPrice=Math.max(100,(((totalPrice / numberMerged) / 5)) * 5);
  if (cgrPrice == 100) {
    cgr.start(100);
  }
 else {
    int prevColPrice=100;
    int colPrice;
    StockSpaceI startSpace;
    for (int col=6; col <= stockMarket.getNumberOfColumns(); col++) {
      colPrice=stockMarket.getStockSpace(1,col).getPrice();
      if (cgrPrice > colPrice)       continue;
      if (cgrPrice - prevColPrice < colPrice - cgrPrice) {
        startSpace=stockMarket.getStockSpace(1,col - 1);
      }
 else {
        startSpace=stockMarket.getStockSpace(1,col);
      }
      cgr.start(startSpace);
      message=LocalText.getText("String_Node_Str",PublicCompany_CGR.NAME,Bank.format(startSpace.getPrice()),startSpace.getName());
      DisplayBuffer.add(message);
      ReportBuffer.add(message);
      break;
    }
  }
  cgr.setFloated();
  ReportBuffer.add(LocalText.getText("String_Node_Str",PublicCompany_CGR.NAME));
  int numCompanies=Math.min(11,12 - mergingCompanies.size());
  int numPlayers=gameManager.getNumberOfPlayers();
  int newCertLimit=certLimitsTable[numPlayers - 3][numCompanies - 4];
  gameManager.setPlayerCertificateLimit(newCertLimit);
  message=LocalText.getText("String_Node_Str",newCertLimit,numPlayers,numCompanies);
  DisplayBuffer.add(message);
  ReportBuffer.add(message);
  List<BaseToken> homeTokens=new ArrayList<BaseToken>();
  nonHomeTokens=new ArrayList<BaseToken>();
  BaseToken bt;
  MapHex hex;
  City city;
  for (  PublicCompanyI comp : mergingCompanies) {
    for (    TokenI token : comp.getTokens()) {
      if (token instanceof BaseToken) {
        bt=(BaseToken)token;
        if (!bt.isPlaced())         continue;
        city=(City)bt.getHolder();
        hex=city.getHolder();
        if (hex == comp.getHomeHex()) {
          homeTokens.add(bt);
        }
 else {
          nonHomeTokens.add(bt);
        }
      }
    }
    if (comp.getCash() > 0) {
      new CashMove(comp,cgr,comp.getCash());
    }
    List<TrainI> trains=new ArrayList<TrainI>(comp.getPortfolio().getTrainList());
    for (    TrainI train : trains) {
      train.moveTo(cgr.getPortfolio());
      if (train.getType().isPermanent())       cgr.setHadPermanentTrain(true);
    }
    if (comp.getBonuses() != null) {
      List<Bonus> bonuses=new ArrayList<Bonus>(comp.getBonuses());
      bonuses:       for (      Bonus bonus : bonuses) {
        comp.removeBonus(bonus);
        if (cgr.getBonuses() != null) {
          for (          Bonus b : cgr.getBonuses()) {
            if (b.equals(bonus)) {
              List<SellBonusToken> commonSP=gameManager.getSpecialProperties(SellBonusToken.class,true);
              if (commonSP != null) {
                for (                SellBonusToken sp : commonSP) {
                  if (sp.getName().equalsIgnoreCase(b.getName())) {
                    sp.makeResellable();
                    log.debug("String_Node_Str" + b.getName() + "String_Node_Str");
                    break;
                  }
                }
              }
              log.debug("String_Node_Str" + b.getName() + "String_Node_Str"+ cgrName);
              continue bonuses;
            }
          }
        }
        cgr.addBonus(new Bonus(cgr,bonus.getName(),bonus.getValue(),bonus.getLocations()));
      }
    }
  }
  ReportBuffer.add("String_Node_Str");
  for (  BaseToken token : homeTokens) {
    city=(City)token.getHolder();
    hex=city.getHolder();
    token.moveTo(token.getCompany());
    if (hex.layBaseToken(cgr,city.getNumber())) {
      ReportBuffer.add(LocalText.getText("String_Node_Str",cgrName,token.getCompany().getName(),city.getName()));
      cgr.layBaseToken(hex,0);
    }
  }
  for (  BaseToken token : new ArrayList<BaseToken>(nonHomeTokens)) {
    city=(City)token.getHolder();
    hex=city.getHolder();
    List<BaseToken> otherTokens=hex.getBaseTokens();
    if (otherTokens != null) {
      for (      BaseToken token2 : otherTokens) {
        if (token2.getCompany() == cgr || nonHomeTokens.contains(token2) && token2 != token) {
          ReportBuffer.add(LocalText.getText("String_Node_Str",cgrName,token.getCompany().getName(),city.getName()));
          token.moveTo(token.getCompany());
          nonHomeTokens.remove(token);
          break;
        }
      }
    }
  }
  if (homeTokens.size() + nonHomeTokens.size() > cgr.getNumberOfBaseTokens()) {
    Map<String,String> oldTokens=new HashMap<String,String>();
    String cityName;
    for (    BaseToken token : nonHomeTokens) {
      if (token.getHolder() instanceof City) {
        cityName=token.getHolder().getName();
        if (oldTokens.containsKey(cityName)) {
          oldTokens.put(cityName,oldTokens.get(cityName) + "String_Node_Str" + token.getCompany().getName());
        }
 else {
          oldTokens.put(cityName,token.getCompany().getName());
        }
      }
    }
    tokensToExchangeFrom=new ArrayList<ExchangeableToken>();
    for (    String key : new TreeSet<String>(oldTokens.keySet())) {
      tokensToExchangeFrom.add(new ExchangeableToken(key,oldTokens.get(key)));
    }
  }
 else {
    executeExchangeTokens(nonHomeTokens);
  }
  for (  PublicCompanyI comp : mergingCompanies) {
    comp.setClosed();
  }
  int trainLimit=cgr.getCurrentTrainLimit();
  List<TrainI> trains=cgr.getPortfolio().getTrainList();
  if (cgr.getNumberOfTrains() > trainLimit) {
    ReportBuffer.add("String_Node_Str");
    int numberToDiscard=cgr.getNumberOfTrains() - trainLimit;
    List<TrainI> trainsToDiscard=new ArrayList<TrainI>(4);
    for (    TrainI train : trains) {
      if (!train.getType().isPermanent()) {
        trainsToDiscard.add(train);
        if (--numberToDiscard == 0)         break;
      }
    }
    for (    TrainI train : trainsToDiscard) {
      train.moveTo(pool);
      ReportBuffer.add(LocalText.getText("String_Node_Str",cgrName,train.getName()));
    }
  }
}