public class RaceStatistics {
    
    private static final int TEAMSIZE = 100;
    private static final int TEAMSINMEET = 3;
    private static final int LASTSCORER = 4;
    private static final int MAXTEAMS = 10;
    
    private RacerInfo [ ] racer = new RacerInfo[TEAMSIZE*TEAMSINMEET];
    private int racerCount;
    
    public RaceStatistics( ) {
        racerCount = 0;
    }
    
    public void addRacer( int bib, String time ) {
        if ( racerCount < racer.length ) {
            racer[ racerCount ] = new RacerInfo( bib, time);
            racerCount++;
        }
    }
    
    public String individualResults() {
        
        String results = "";
        
        for ( int place = 0; place < racerCount; place++ ) {
            results = results +
                (place+1) + ". " +
                "Racer " + racer[place].getBib()  + "  " +
                racer[place].getTime() +
                "\n";
        }
        return results;
    }
    
    public int getPlacement( int bib ) {
        for ( int place = 0; place < racerCount; place++ ) {
            if ( racer[place].getBib() == bib ) {
                return place+1;
            }
        }
        return -1;
    }
    
    public int teamScore( int teamNo ) {
        
        int racersCounted=0;     // Number of finishers found so far
        int score=0;             // Sum of finishing places
        
        // Find the first four racers for the specified team
        for ( int place = 0; 
              place < racerCount && racersCounted < LASTSCORER; 
              place++ ) {
            
            if (racer[place].getTeam() == teamNo )   {
                racersCounted++;
                score = score + (place + 1);
            }
        }
        
        // Make sure that at least four racers were found
        if ( racersCounted < LASTSCORER ) {
            score = -1;
        }
        
        return score;
        
    }
    
    public int [ ] teamStandings() {
        
        // How many racers have been found from each team
        int [] racersCounted = new int[MAXTEAMS];
        
        // Sum of places or each team's racers
        int [] score = new int[MAXTEAMS];
        
        // Look for four finishers from each team 
        for ( int place = 0; place < racerCount; place++ ) {
            
            int teamNo = racer[place].getTeam();
            
            if ( racersCounted[ teamNo ] < LASTSCORER ) {
                racersCounted[ teamNo ]++;
                score[ teamNo ] = score[ teamNo ] + (place+1);
            }
        }
        
        // Identify teams that had less than four finishers
        for ( int teamNo = 0; teamNo < MAXTEAMS; teamNo++ ) {
            if ( racersCounted[ teamNo ] < LASTSCORER ) {
                score[ teamNo ] = -1;
            }
        }
        
        return score;
    }
    
    
    public void addRacerAtPosition( int bib, String time, int insertionPos ) {
        if ( racerCount < racer.length && insertionPos <= racerCount ) {
            for ( int position = racerCount; position > insertionPos; position-- ) {
                racer[position] = racer[position-1];
            }
            racer[insertionPos] = new RacerInfo( bib, time );
            racerCount++;
        }
    }
    
    public void removeRacerAtPosition(int position) {
        
        if ( position < racerCount ) {
            racerCount--;
            
            for ( int place = position; place < racerCount; place++) {
                racer[place] = racer[place + 1];
            }
            
            racer[racerCount] = null;
        }
    }
}
