/******************************************************************************************
person.cpp
    implementation with class person
******************************************************************************************/
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

#include "person.h"

//------- Find the youngest person in the list ------
int indexOfYoungest(person list[], int size) {
    int youngestAge = list[0].age;
    int youngestPos = 0;

    for (int i = 1; i < size; i++)
        if (list[i].age < youngestAge) {
            youngestAge = list[i].age;
            youngestPos = i;
        }

    return youngestPos;
}

//------- Find the oldest person in the list ------
int indexOfOldest(person list[], int size) {
    int oldestAge = list[0].age;
    int oldestPos = 0;

    for (int i = 1; i < size; i++)
        if (list[i].age > oldestAge) {
            oldestAge = list[i].age;
            oldestPos = i;
        }

    return oldestPos;
}

//------- Find the age of people in the list ------
int AverageAge(person list[], int size) {
    int sum = 0;

    for (int i = 0; i < size; i++)
        sum += list[i].age;

    return sum/size;
}

//------- Print the people in the list older than the supplied age ------
void showOlderThan(person people[], const int size, const int age) {
    cout << "People at or above age " << age << ":\n";
    for (int i=0; i < size; i++) {
        if (people[i].age >= age)
            cout << '\t' << people[i].last << ", " << people[i].first
                 << " (" << people[i].age << ")\n";
    }
}

//------- Print the people in the list younger than the supplied age ------
void showYoungerThan(person people[], const int size, const int age) {
    cout << "People below age " << age << ":\n";
    for (int i=0; i < size; i++) {
        if (people[i].age < age)
            cout << '\t' << people[i].last << ", " << people[i].first
                 << " (" << people[i].age << ")\n";
    }
}

//------- display the whole list ------
void showPeople(person people[], const int size) {
    for (int i=0; i < size; i++) {
        cout << people[i].last << ", " << people[i].first << " (" << people[i].age << ")\n";
    }
}

//------- 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].last >> people[i].first >> people[i].age)
            size++;
    }
}