//Barb Sanchez
//Student Records Simulation

#include <iostream>
#include <fstream> //for files
#include <iomanip> //for output formatting


using namespace std;

///////////////////////////////////////////////////////////////
class student
{
    public:
        char name1 [20];
        char name2 [20];
        char SS [12];
        int grad_year;
        double fees_due;


        //function definition
        void showData()
        {
            cout <<  name1 << setw (22) << name2 << setw (15) << SS <<
                setw(6) << grad_year << setw (10) << fees_due <<endl;
//          cout << "Last Name:  " << name2 << endl;
//          cout << "SS:          " << SS << endl;
//          cout << "Graduation Year " << grad_year << endl;
//          cout << "Fees Due " << fees_due << endl;

        }

        //function prototypes
        void set_up();
        void writeData();
        int countData();
        void readData(int);
        void showData(int);
        int menu();

}; //class

///////////////////////////////////////////////////////////////
/**************************************************************
The menu() function will display a menu to the screen and allow 
the user to enter a selection, which will be returned to main.
  *************************************************************/

int menu()
{
    int choice;
    
    cout << "*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^\n";
    cout <<"    \n(1) Set Up New Student Account";
    cout <<"    \n(2) Show Current Account Info";
    cout <<"    \n(3) Show All Accounts and Balances"; 

    cout << "\n*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^\n";

    cin >> choice;

    while (choice !=1 && choice !=2 && choice !=3 && choice !=4 && choice !=5 && choice!=6)
    {
        cout << "Please enter a valid choice from the list above";
        cin  >> choice;
    }

    return choice;
}

///////////////////////////////////////////////////////////////
/**************************************************************
The setup() function will collect information about the student
which can then be writen to a disk file.
  *************************************************************/
void student::set_up()
{
    cout <<"Enter first name ";
    cin  >> name1;

    cout <<"Enter last name ";
    cin  >> name2;

    cout << "Enter SS #:   ";
    cin  >> SS;
    
    cout << "Enter year of expected graduation: ";
    cin  >> grad_year;
    
    cout << "Enter the tuition/fees that are owed  ";
    cin  >> fees_due;


}
//------------------------------------------------------------- 

/**************************************************************
The writeData() function will append data to the file and write
it to the disk.
  *************************************************************/
void student::writeData()
{
    ofstream outfile; //create object
    outfile.open("account.dat", ios::app | ios::binary);  //create/append file
    outfile.write(  (char*) this, sizeof (*this)  ); //write to it

}

//------------------------------------------------------------- 

/**************************************************************
The countData() function will determine the number of records
in the file.
  *************************************************************/
int student::countData()
{
    ifstream infile; //create object
    infile.open("account.dat", ios::in | ios::binary);  //open
    infile.seekg(0, ios::end);  //go to end of file (0 bytes from end)
    int endposition = infile.tellg(); 
    int n = endposition / sizeof (student); 
    return n;

}


//------------------------------------------------------------- 

/**************************************************************
The readData() function will read the data in the file.
  *************************************************************/
void student:: readData(int number)
{

    ifstream infile;  //create object
    infile.open("account.dat", ios::binary); //open file
    infile.seekg( number* sizeof(student) ); //move file pointer to this item
    infile.read(  (char*) this, sizeof (*this)  );  //read one object

}


/////////////////////////////////////////////////////////////////////////////////
int main()
{

    int count=0, acct, choice; 
    int num_accounts = 0;
    char again;

    student s;
    
    do  
    {   
        choice = menu();

        switch (choice)
        {
            case 1:
                    s.set_up();  //set up new account
                    s.writeData();   //append to data file
                    
                //  display current number of accounts
                    count = s.countData();
                    cout << endl << count << " accounts are currently set up.";

                    break;

            case 2: //show one account's info
                    //show changes  
                    
                    
                    cout << "Which account # do you want to display?";
                    cin >> acct;
                    //acct = acct -1; //numbering starts at 0;
                
                    cout << "\nAccount Info: \n";
                    s.readData(acct); //get info from file  //not reading updated info until next time through
                    s.showData(); //display info to console
                    cout << "------------------\n";
        
                
                break;

            case 3: //display current number of accounts
                    
                count = s.countData();
                    cout << endl << count << " accounts are currently set up."; 

                    for (int j=0; j<count; j++)
                    {
                        cout << "\nAccount Info: for account " << j<< endl;
                        s.readData(j); //get info from file
                        s.showData(); //display info to console
                        cout << "------------------\n";
                    } //for
    
                break;
                
            
        } //switch


        //==========================

        cout << "\nKeep working? (Y/y)\n";
        cin >> again;

    } while (again =='y' || again =='Y');



    return 0;

}

/* Program Output (2 runs)

*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^

(1) Set Up New Student Account
(2) Show Current Account Info
(3) Show All Accounts and Balances
*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^
3

2 accounts are currently set up.
Account Info: for account 0
Barb               Sanchez    123-33-3333  1984     12000
------------------

Account Info: for account 1
Smith                 Tanya    234-44-4444  4321         2
------------------

Keep working? (Y/y)
y
*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^

(1) Set Up New Student Account
(2) Show Current Account Info
(3) Show All Accounts and Balances
*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^
1
Enter first name Jones
Enter last name Wee
Enter SS #:   333-44-5555
Enter year of expected graduation: 2007
Enter the tuition/fees that are owed  1000

3 accounts are currently set up.
Keep working? (Y/y)
y
*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^

(1) Set Up New Student Account
(2) Show Current Account Info
(3) Show All Accounts and Balances
*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^
3

3 accounts are currently set up.
Account Info: for account 0
Barb               Sanchez    123-33-3333  1984     12000
------------------

Account Info: for account 1
Smith                 Tanya    234-44-4444  4321         2
------------------

Account Info: for account 2
Jones                   Wee    333-44-5555  2007      1000
------------------

Keep working? (Y/y)


  */