// Bank.java
/*
 Creates a bunch of accounts and uses threads
 to post transactions to the accounts concurrently.
*/

import java.io.*;
import java.util.*;

public class Bank {
    private Account[] accounts;
    private Buffer buffer;
    private int numWorkers;
    
    
    public static final int ACCOUNTS = 20;     // number of accounts

    public Bank(int numWorkers) {
        this.numWorkers = numWorkers;
        
        // YOUR CODE HERE
        
    }
    
    
    // Worker: get transaction, apply to accounts
    class Worker extends Thread {
        public void run() {
            // YOUR CODE HERE
        }
    }

    /*
     (provided)
     Reads transactions out of a file and adds
     them to the buffer.
    */
    public void readFile(File file) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            
            // Use stream tokenizer to get successive words from file
            StreamTokenizer tokenizer = new StreamTokenizer(reader);
            int read, from = 0, to = 0, amount = 0;
            
            while (true) {
                read = tokenizer.nextToken();
                if (read == StreamTokenizer.TT_EOF) break;  // detect EOF
                from = (int)tokenizer.nval;
                
                tokenizer.nextToken();
                to = (int)tokenizer.nval;
                
                tokenizer.nextToken();
                amount = (int)tokenizer.nval;
                
                // Add a transaction to the buffer
                buffer.add(new Transaction(from, to, amount));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    /*
     Processes one file:
     -fork off workers
     -read file into the buffer
     -wait for the workers to finish
    */
    public void processFile(File file) {
        // YOUR CODE HERE
    }

    // Provided code to print acounts
    public void printAccounts() {
        for (int i = 0; i<accounts.length; i++) {
            System.out.println(accounts[i]);    // trick: uses toString()
        }
    }
    
    
    
    // Provided main to create Bank object and start the processing
    public static void main(String[] args) {
        if (args.length!=2 && args.length!=3) {
            System.out.println("Args: <filename> <number-of-threads> [limit]");
            System.exit(1);
        }
        
        String fname = args[0];
        int num = Integer.parseInt(args[1]);
        
        // Parse the "limit" arg if present
        int limit = Integer.MAX_VALUE;
        if (args.length == 3) {
            limit = Integer.parseInt(args[2]);
        }
        
        Bank bank = new Bank(num);
        bank.processFile(new File(fname));
        bank.printAccounts();
    }
}

