/******************************************************************************************
ex6.cpp
    Working our way to dynamic criteria
    Second criteria from additional states
******************************************************************************************/
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

// class definitions
class person {
    public:
        string first;
        string last;
        string state;
};

class state {
    public:
        string full;
        string token;
        int count;
        float percent;
};

// prototypes
void readFile(string filename, person [], const int maximum, int& size);
int TallyByState(string token, person [], const int size);

// MAIN!
void main() {
    int count=0;
    const int maxsize=100;
    person census[maxsize];

    string filename;
    cout << "Enter the file to read: ";
    cin >> filename;
    readFile(filename, census, maxsize, count);

    state NewYork;
    NewYork.full = "New York";
    NewYork.token = "NY";
    NewYork.count = TallyByState(NewYork.token, census, count);
    NewYork.percent = (float)NewYork.count/count;

    state NewJersey;
    NewJersey.full = "New Jersey";
    NewJersey.token = "NJ";
    NewJersey.count = TallyByState(NewJersey.token, census, count);
    NewJersey.percent = (float)NewJersey.count/count;

    cout << NewYork.count << " records from "<< NewYork.full << endl
         << NewJersey.count << " records from "<< NewJersey.full << endl
         << endl
         << NewYork.percent*100 << "% of records from "<< NewYork.full << endl
         << NewJersey.percent*100 << "% of records from "<< NewJersey.full << endl;

}

//------- return the count of records where state matches the token ------
int TallyByState(string token, person list[], const int size)
    {
    int count = 0;
    for (int i = 0; i < size; i++)
        if (list[i].state == token)
            count++;
    return count;
    }

//------- fill each person in the list from a file ------
void readFile(string filename, person people[], const int maximum, int& size) {
    ifstream fin(filename.c_str());

    for (int i=0; i < maximum; i++) {
        if(fin >> people[i].first >> people[i].last >> people[i].state)
            size++;
    }
}