import java.awt.*;
import java.awt.event.*;
import java.io.*;

/*
 * The train set.
 * This initial train set has only two tracks, 
 * each with a train one car in length.
 
 * You will modify the following class to handle multiple tracks
 * and trains.
 */

public class TrainSet {
    
    // Tracks in this trainset.
    private Track trk, trk2;
    
    // Representation invariant:
    
    // - trk and trk2 each store instances of Track.
    
    /*
     * Sets up the initial state of this trainset.
     
     * @param inFile  MyStreamTokenizer containing tokens
     *        describing the trainset.
     * (The complete format of the file is describe in the 
     * TrainSetDriver class file.)
     */
     
    public void setup(MyStreamTokenizer inFile) throws 
        IOException {
       
        trk = new Track();                
        trk.addDetails(inFile);
        inFile.flushLine();
        
        trk2 = new Track();
        trk2.addDetails(inFile);
        inFile.flushLine();
    }
    
    /*
     * Draws this trainset.
     * @param  g  the graphics context in which to draw this train set.
     */
     
    public void draw(Graphics g) {
        trk.draw(g);
        trk2.draw(g);
    }
    
    /*
     * Start, run and stop this trainset simulation.
     */
     
    public void runSimulation(TrainFrame frame) {
    
        trk.startTrain();
        trk2.startTrain();
        
        for(int count=0; count<500; count++) {
            trk.moveTrain();
            trk2.moveTrain();
            frame.repaint();
            try {Thread.sleep(300);} catch (Exception e){};

        }

        trk.stopTrain();
        trk2.stopTrain();
        
    }
}
