//******************************************************************
// PunchIn program
// A data file contains time cards for employees who have punched
// in for work.  This program reads in the time cards from the
// input file, then reads employee ID numbers from the standard
// input.  For each ID number, the program looks up the employee's
// punch-in time and displays it to the user
//******************************************************************
#include <iostream>
#include <fstream>      // For file I/O
#include <string>       // For string class
#include "timecard.h"   // For TimeCard class
#include "tclist.h"     // For TimeCardList class

void GetID( long& );
void OpenForInput( ifstream& );

int main()
{
    ifstream     punchInFile;      // Input file of time cards
    TimeCardList punchInList;      // List of time cards
    TimeCard     punchInCard;      // A single time card
    long         idNum;            // Employee ID number
    bool         found;            // True if idNum found in list

    OpenForInput(punchInFile);
    if ( !punchInFile )
        return 1;

    punchInList.ReadAll(punchInFile);
    punchInList.SelSort();

    GetID(idNum);
    while (idNum >= 0)
    {
        punchInList.BinSearch(idNum, found, punchInCard);
        if (found)
        {
            punchInCard.Print();
            cout << endl;
        }
        else
            cout << idNum << " has not punched in yet." << endl;
        GetID(idNum);
    }
    return 0;
}

//******************************************************************

void OpenForInput( /* inout */ ifstream& someFile )    // File to be
                                                       // opened
// Prompts the user for the name of an input file
// and attempts to open the file

// Postcondition:
//     The user has been prompted for a file name
//  && IF the file could not be opened
//         An error message has been printed
// Note:
//     Upon return from this function, the caller must test
//     the stream state to see if the file was successfully opened

{
    string fileName;    // User-specified file name

    cout << "Input file name: ";
    cin >> fileName;

    someFile.open(fileName.c_str());
    if ( !someFile )
        cout << "** Can't open " << fileName << " **" << endl;
}

//******************************************************************

void GetID( /* out */ long& idNum )    // Employee ID number

// Prompts for and reads an employee ID number

// Postcondition:
//     idNum == value read from standard input

{
    cout << endl;
    cout << "Enter an employee ID (negative to quit): ";
    cin >> idNum;
}
