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

/******************************************************************
 Perform basis path testing on this module (ignore exceptions).

 This program reads a file of income amounts classified by
 gender and computes the average income for each gender
 Original program in C++ from Nell Dale, converted to Java by J. Dalbey
*******************************************************************/

public class AvgIncome
{
    public static void main (String[] args) throws FileNotFoundException, IOException
    {
        char     gender;           // Coded 'F' = female, 'M' = male
        float    amount;           // Amount of income for a person
        StringTokenizer tokenizer;  // Needed to parse the file input line
        String InputLine;           // The line read from the file
        String filename="Incomes.dat";  // the name of the data file

        Incomes TheIncomes = new Incomes();  //Instance of Incomes class

        // Open the data file and read all the lines of input
        FileReader fr = new FileReader (filename);
        BufferedReader inFile = new BufferedReader (fr);

        InputLine = inFile.readLine();

        // Loop to read until end of file
        while (InputLine != null)
        {
            // Read a string and extract the fields
            tokenizer = new StringTokenizer (InputLine);
            gender = tokenizer.nextToken().charAt(0);
            amount = Float.parseFloat (tokenizer.nextToken());

            // Echo the input
            System.out.println( "Gender: " + gender + " Amount: " + amount);

            // Update the appropriate Sum
            TheIncomes.Update(gender, amount);

            InputLine = inFile.readLine();  //read next line
        }

        inFile.close();  // we're done with the file

        // Output results
        System.out.println("For "+ TheIncomes.FemaleCount() +" females, the average "
                           + "income is " + TheIncomes.FemaleAverage() );
        System.out.println("For "+ TheIncomes.MaleCount() +" males, the average "
                           + "income is " + TheIncomes.MaleAverage() );

    }
}