#include <iostream>
#include <fstream>

#define ROW 15					// Size of rows 
#define COL 30					// Size of columns

using namespace std;

void inputFile(double []);
int menu();
void buyTicket(int &,char [ROW][COL],double []);
void totalSales(int);
void unavailableSeat(char [ROW][COL]);
void availableSeat(char [ROW][COL]);

int main()
{
	int choice,ticket=0;
	double price[15];
	char seat[ROW][COL];
	
	inputFile(price);
	
	for(int i=0;i<ROW;i++)								// Display the free seats with '#'
		for(int j=0;j<COL;j++)
			seat[i][j]='#';
				
	while(1)
	{
		choice=menu();
		switch(choice)
		{
			case 1: buyTicket(ticket,seat,price); break;
			case 2: totalSales(ticket); break;
			case 3: unavailableSeat(seat); break;
			case 4: availableSeat(seat); break;
			case 5:
				cout << "\nProgram Closed" << endl;
				cout << "\nFile closed"<< endl;
				return 0;
		}
	}
}

//-----------------------------------------------------------------------------------
// Function : Read file
// Purpose  : To read inputs from a file consisting of the price of seats
//------------------------------------------------------------------------------------

void inputFile(double price[])
{
	int i=0;
	ifstream readFile;
	readFile.open("seatPrice.txt");
	if(!readFile)
	{
		cout << "Fail to read file"<<endl<<endl;
		exit(1);
	}
	else cout << "Read successfully" <<endl<<endl;
	
	while(!readFile.eof())
	{
		readFile >> price[i+1];
		i++;
	}
	
	readFile.close();
}

//-------------------------------------------------------------------
// Function : Display menu
// Purpose : Let user key in the choice
// Return value : Choice
//-------------------------------------------------------------------

int menu()
{
	int choice=0;
	system( "cls" );
	
	cout<<"---------MENU---------"<<endl;
	cout<<"1.BUY TICKET"<<endl;
	cout<<"2.TOTAL TICKET SALES"<<endl;
	cout<<"3.UNAVAILABLE SEATS"<<endl;
	cout<<"4.AVAILABLE SEATS"<<endl;
	cout<<"5.EXIT."<<endl;
	cout<<"Enter a choice 1-5 :";
	cin >> choice;
	
	while(choice>5||choice<1)
	{
		cin.clear();
		cin.ignore();
		cout << "INVALID INPUT..."<<endl<<endl;
		cout<<"\n-----MENU-----"<<endl;
		cout<<"1.BUY TICKET"<<endl;
		cout<<"2.TOTAL TICKET SALES"<<endl;
		cout<<"3.UNAVAILABLE SEATS"<<endl;
		cout<<"4.AVAILABLE SEATS"<<endl;
		cout<<"5.EXIT."<<endl;
		cout<<"Enter a choice 1-5 :";
		cin >> choice;
	}
	
	return choice;
}

//-------------------------------------------------------------
// Function : buyTicket
// Purpose : To select the seats
// Parameters :  
//				ticket  :	the number of ticket 
//				seat    :   the array hold the list of seat
//				price   :   the array hold the list of price
//--------------------------------------------------------------

void buyTicket(int &ticket,char seat[ROW][COL],double price[])
{
	int people,row,seatnumber,k=0;
	double total[50];
	bool cfail;
	while(1)
		{
			cout << "\t\t   Seats"<<endl;
			cout << "\t";
			for(int i=0;i<3;i++)
				cout << "1234567890";						// Display 30 columns
			cout <<endl;
			for(int i=0;i<ROW;i++)
			{
				cout << "ROW "<<i+1<<"\t";
				for(int j=0;j<COL;j++)						// Display 15 rows
				{
					cout << seat[i][j];
				}
				cout << endl;
			}
	
			cout << "\nNumber of ticket (0 to exit):";
			cin >> people;
			cfail = cin.fail();
			cin.clear();
			cin.ignore();
			
			while(people<0||cfail==true)
			{
				cout << "\nINVALID INPUT..."<<endl;
				cout << "\nNumber of ticket (0 to exit):";
				cin >> people;
				cfail = cin.fail();
				cin.clear();
				cin.ignore();
			}
			
			if(people==0) break;
			
			for(int i=0;i<people;i++)
			{
				cout << "\nTicket"<<i+1<<" : "<<endl;
				cout << "\t  Enter row : ";
				cin >> row;
				cout << "\t  Enter seat number : ";
				cin >> seatnumber;
				
				while(!(row>0&&row<16)||!(seatnumber>0&&seatnumber<31))
				{
					cin.clear();
					cin.ignore();
					cout << "Ticket"<<i+1<<" : "<<endl;
					cout << "\t  INVALID INPUT..."<<endl;
					cout << "\t  Enter row : ";
					cin >> row;
					cout << "\t  Enter seat number : ";
					cin >> seatnumber;
				}
				

				
				while(seat[row-1][seatnumber-1]=='*')
				{
					cout << "\nTicket sold. Choose another seat." <<endl<<endl;
					cout << "Ticket"<<i+1<<" :"<<endl;
					cout << "\t  Enter row : ";
					cin >> row;
					cout << "\t  Enter seat number : ";
					cin >> seatnumber;
				}
				
				seat[row-1][seatnumber-1]='*';						// Display sold ticket with '*'
		
				total[k]=total[k]+price[row];						// Total the price of tickets sold from file
			}
			
			ticket=ticket+people;
			
			cout << "\nTotal ticket price = $"<<total[k]<<endl<<endl;
			k++;

		}
		cout << endl;
		system("pause");
}

//-------------------------------------------------------------------------------
// Function : unavailableSeat
// Purpose  : to total up the unavailable and available seats in each rows
// Parameters:
//           Seat  	:  the array hold the list of seat
//          
// ------------------------------------------------------------------------------

void unavailableSeat(char seat[ROW][COL])
{
	int counter[15];
	cout<<"\nUnavailable seat :"<<endl;
		for(int i=0;i<ROW;i++)
		{
			counter[i]=0;
			cout << "\tROW"<<i+1<<" =";
			for(int j=0;j<COL;j++)
			{
				if(seat[i][j]=='*')
				{
					counter[i]=counter[i]+1;
				}
			}
			cout << counter[i] <<endl;
		}
		cout << endl;
		system("pause");
}

//-------------------------------------------------------------------------------
// Function : availableSeat
// Purpose  : to check which seat are available 
//            either by each row or the entire of auditorium
// Parameters:
//           Seat  	:  the array hold the list of seat
//          
// ------------------------------------------------------------------------------

void availableSeat(char seat[ROW][COL])
{
	int option,number,available;
	cout << "\n1.Seat available in each row"<<endl;
	cout << "2.Seat available in entire auditorium"<<endl;
	cout << "Enter choice (1 or 2) :";
	cin >> option;
	
	while(!(option==1||option==2))
	{
		cin.clear();
		cin.ignore();
		cout << "\nINVALID INPUT..."<<endl;
		cout << "\n1.Seat available in each row"<<endl;
		cout << "2.Seat available in entire auditorium"<<endl;
		cout << "Enter choice (1 or 2) :";
		cin >> option;
	}
	
	switch(option)
	{	case 1:														// Check the available seat in row
			cout << "Enter row number :";
			cin >> number;
			while(number>15||number<1)
			{
				cin.clear();
				cin.ignore();
				cout << "\nINVALID INPUT..."<<endl<<endl;
				cout << "Enter row number :";
				cin >> number;	
			}
			available=0;
			for(int i=0;i<COL;i++)
			{
				if(seat[number-1][i]=='#')
				{
					available++;
				}
			}
			cout << "\nAvailable seat in ROW "<<number<<" = "<<available<<endl<<endl;
			break;
			
		case 2:													// Check the available seats in auditorium
			available=0;
			for(int i=0;i<ROW;i++)
			{
				for(int j=0;j<COL;j++)
				{
					if(seat[i][j]=='#')
					{
						available++;
					}
				}
			}
			cout << "\nAvailable seat in auditorium = "<<available<<endl<<endl;
			break;
	}
		system("pause");
}

//---------------------------------------------------------------
// Function : totalSales
// Purpose  : to show the total of the sales         
// Parameters:
//           ticket	:  the number of ticket
//---------------------------------------------------------------

void totalSales(int ticket)
{
	cout << "\nTotal ticket sales = " << ticket<<endl<<endl;
	system("pause");
}
