// "Student Database", by Randall C. Waldrep
// This program utilizes a linked list to allow the user to 
// add or delete students from the database.  The user can also
// search for a student by his or her student ID number, which 
// is assigned by the user.

// CHANGES MADE FROM THE ROUGH DRAFT:
// exception handlers implemented
// linked list bug remedied
// grade function implemented

// known bugs:
// doesn't display the office number of the grad students yet.

#include <iostream.h>
#include <stdlib.h>
#include "List.h"

//This checks to see if the student first/last name is non-numeric.
//Exception returned if it is numeric.
int checkname(char name[20])
{
	int counter=0;
	int correct=1;
	while(counter<20 && name[counter]!='\0')
	{	
		try
		{
			if( int(name[counter]) < 65 || name[counter] > 122)
			{
				throw "Names cannot contain numeric characters.";
			}
		}
		catch(char* pError)
		{
			cout<<"Error occurred: "<<pError<<endl;
			counter=20;
			correct=0;
		}
		counter++;
	}
	return correct;
}

//This checks to see if the student number entered is 8 digits long.
//Exception returned if it is the wrong size.
int checknum(int num)
{
	int correct=1;
	if ((num / num) != 1) //this keeps an infinite loop from occurring
	{					  //should user enter a char string
		exit(1);
	}
	int temp=(num / 10000000); //dividing an 8-digit number by 10000000
	try						   //should yield a num between 1 and 9.
	{
		if(temp < 1 || temp > 9)
		{
			throw "number must be 8 digits.";
		}
	}
	catch(char* pError)
	{
		cout<<"Error occurred: "<<pError<<endl;
		correct=0;
	}
	return correct;
}

//This checks to see if the office number for the grad student is
//4 digits long.
//Exception returned if it is the wrong size.
int checkoffice(int num)
{
	if((num / num) != 1)  //this keeps an infinite loop from occuring
	{					  //should user enter a char string
		exit(1);
	}
	int correct=1;
	int temp=(num / 1000); //dividing a 4-digit number by 1000
	try					   //should yield a num between 1 and 9.
	{
		if(temp < 1 || temp > 9)
		{
			throw "number must be 4 digits.";
		}
	}
	catch(char* pError)
	{
		cout<<"Error occurred: "<<pError<<endl;
		correct=0;
	}
	return correct;
}


//The main menu selector
char menu()
{
	cout<<"**********************************"<<endl;
	cout<<"* UCI Student Information Center *"<<endl;
	cout<<"**********************************"<<endl;
	cout<<"* a = add student                *"<<endl;
	cout<<"* b = add grad student           *"<<endl;
	cout<<"* d = drop student               *"<<endl;
	cout<<"* s = search for student         *"<<endl;
	cout<<"* g = assign grade               *"<<endl;
	cout<<"* v = view student list          *"<<endl;
	cout<<"* q = quit this program          *"<<endl;
	cout<<"**********************************"<<endl;
	cout<<"Your choice: "<<endl;
	char choice;
	cin>>choice;
	return choice;
}

//Main program begins here.
int main()
{
	
	List mechanicsStudents;  //defines the list for the database
	
	while(1)
	{
		char choice=menu();  //let user choose from menu
		switch (choice)
		{
		case 'a': //function to add a student
			{
				int correct=0;
				char last[20];
				char first[20];
				int idTempHolder;
				while(correct==0) //makes sure they enter a valid name
				{
					cout << "enter last name: ";
					cin >> last;
					correct=checkname(last);
				}
				correct=0;
				while(correct==0) //makes sure they enter a valid name
				{
					cout << "enter first name: ";
					cin	>> first;
					correct=checkname(first);
				}
				correct=0;
				while(correct==0) //makes sure they enter a valid #
				{
					cout << "enter id number: ";
					cin >> idTempHolder;
					correct=checknum(idTempHolder);
				}
				Student s(idTempHolder, last, first);
				mechanicsStudents.insert(s); 
				break;
			}
		case 'b':  //function to add a grad student
			{
				char last[20];
				char first[20];
				int idTempHolder;
				int officenumber;
				int correct=0;
				while(correct==0) //makes sure they enter a valid name
				{
					cout << "enter last name: ";
					cin >> last;
					correct=checkname(last);
				}
				correct=0;
				while(correct==0) //makes sure they enter a valid name
				{
					cout << "enter first name: ";
					cin	>> first;
					correct=checkname(first);
				}
				correct=0;
				while(correct==0) //makes sure they enter a valid #
				{
					cout << "enter id number: ";
					cin >> idTempHolder;
					correct=checknum(idTempHolder);
				}
				correct=0;
				while(correct==0) //makes sure they enter a valid office #
				{
					cout<<"enter office number in Fred Reines Hall: ";
					cin>>officenumber;
					correct=checkoffice(officenumber);
				}
				GraduateStudent gs(idTempHolder, last, first, officenumber);
				mechanicsStudents.insert(gs);
				break;
			}
		case 'd': //function to remove a student 
			{
				int idTempHolder;
				int correct=0;
				while(correct==0) //makes sure a valid # is entered
				{
					cout<<"enter the id of a student to be removed: ";
					cin>>idTempHolder;
					correct=checknum(idTempHolder); //check: 8 digits?
				}
				if (mechanicsStudents.isInClass(idTempHolder))
				{
					mechanicsStudents.remove(idTempHolder);
					cout<<"Student has been successfully removed."<<endl;
				}
				else cout << "Student is not in class." << endl;
				break;
			}
		case 's': //function to search for a student
			{
				// test isInClass; is a student in the list already
				int correct=0;
				int idTempHolder;
				while(correct==0)
				{
					cout<<"enter the id of a student: ";
					cin>>idTempHolder;
					correct=checknum(idTempHolder); //check: 8 digits?
				}
				if (mechanicsStudents.isInClass(idTempHolder))
				{
					cout<<"Student found."<<endl;
					mechanicsStudents.showName(idTempHolder);
				}
				else
					cout<<"Student not in list."<<endl;
				break;
			}
		case 'g': //function to assign a grade
			{

				int idTempHolder;
				int correct=0;
				while(correct==0) //check ID#
				{
					cout<<"enter the id of a student to be graded: ";
					cin>>idTempHolder;
					correct=checknum(idTempHolder); //check: 8 digits?
				}
				if (mechanicsStudents.isInClass(idTempHolder))
				{
					cout<<"Student found."<<endl;
					mechanicsStudents.showName(idTempHolder);
					char grade;
					cout<<"Enter the grade for this student: ";
					cin>>grade;
					Student &ref = mechanicsStudents.retrieve(idTempHolder);
					ref.grade = grade;
					cout<<"Student has been successfully graded."<<endl;
				}
				else
				{
					cout<<"Student not in list."<<endl;
				}
				break;
			}
		case 'v':  //function to view the database
			{
				if (mechanicsStudents.isEmpty())
					cout<<"The list is empty."<<endl;
				else if (!mechanicsStudents.isEmpty())
					cout<<mechanicsStudents<<endl;
				break;
			}
		case 'q':  //function to quit the program
			{
				exit(1);
			}
		default:  //if garbage is entered, have them retry.
			{
				cout<<endl<<"Sorry, that item does not exist in this menu."<<endl;
			}
		}
	}
	return 0;
}
