// SECJ1013-01 Assignment 2
// Group Members:
// AHMAD ZULHAKIM BIN ZAINAL A19EC0179
// LEE TONG MING             A19EC0069					

#include <iostream>
#include <iomanip>
using namespace std;

// Function prototypes
int priceForEachRow(double[], int, int);
int getSeat(int&, int&);
void updateAvailableSeat(char seat[][30], int m, int n);
void displaySeats(char[][30]);
void displayMenu();

// Function to display available seats. 
// Accepts array seats as parameter.
void displaySeats(char seats[][30])
{
	cout << "\n\tList of available seats\n\n";
    cout << right << setw(25) << "Seats" << endl;
    cout << setw(9);
	for (int i=0; i<3; i++)
    {
        for (int j=1; j < 10; j++)
			cout << j;
        cout << "0";
    }
    cout << endl;
    
    for (int i=0; i<15; i++)
    {
    	cout << left;
        cout << setw(4) << "ROW" << setw(1) << (i+1);
        cout << '\t';
        
        for (int j=0; j<30; j++)    
            cout << seats[i][j];
        cout << endl;
    }   
}

// Funtion to get the price for each row.
int priceForEachRow(double priceByRow[], int ROW)
{
	int price;
	cout << "\n\t\tEnter the price for each row" << endl; 
	for (int n=0; n<ROW; n++)
	{
		cout << "\tROW " << (n+1) << " : ";
		cin >> price;
		priceByRow[n] = price;
	}
	return price;
}

// Function to prompt the user to enter their seat
// read their inputs.
int getSeat(int& i, int& j)
{
	do
	{
		cout << "\tEnter the row of your seat: ";
		cin >> i;
		cout << "\tEnter the column of your seat: ";
		cin >> j;
		
		if ((i>15)||(j>30))
			cout << "\n\tPlease enter the valid seat no.\n";
	}	while((i>15)||(j>30));
	return i, j;
}

// Function to update the list of available seats.
void updateAvailableSeat(char seats[15][30], int m, int n)
{
	seats[m-1][n-1] = '*'; 
}


int main()
{
    int SIZE_ROW = 15;
    int SIZE_COL = 30;
    char seats[15][30];
    double priceRow[15];
    double totalPrice;
    int numTicket, totalTicket, row, col, choice, totalSeat;
    
    cout << "\n\t\tTheater Ticket System\n";
    priceForEachRow(priceRow, 15); // Get the price of each row of seats
    system("cls"); // clear the screen
	
	for (int i=0; i<SIZE_ROW; i++) // Fill the seats array with # (seat available)
    {
        for (int j=0; j<SIZE_COL; j++)
            seats[i][j] = '#';
    }
	
	system("cls");
	cout << endl;

	do
	{
		displayMenu();
		int n=0;
		cin >> choice;
		
			switch(choice)
			{
				case 1 :	displaySeats(seats);
							break;
			
				case 2 :	cout << "\tEnter the amount of tickets: ";
						    cin >> numTicket;
						    
							for (; n<numTicket; n++)
						    {
								cout << "\n\tTicket " << (n+1) << ": ";
								cout << endl;
								do
								{
									getSeat(row,col);
								
									if (seats[row-1][col-1]=='*')
									{
										cout << "\n\tSeat is not available. Please pick another seat.";
										n--;
										break;
									}
											
										updateAvailableSeat(seats, row, col);
										totalPrice += priceRow[row-1];
										
								} while (seats[row-1][col-1]=='#');
							}
								
								cout << fixed << setprecision(2);
								cout << "\n\tTotal price: RM" << totalPrice;
								totalTicket += numTicket;
								break;	
				
				case 3 : cout 	<< "\n\tThe amount of ticket sold are "
								<< totalTicket
								<< "\n\tTotal price: RM" << totalPrice;
								break;
								
				case 4 : cout	<< "\n\tThe amount of seats sold are "
								<< totalTicket;
								break;
								
				case 5 : cout 	<< "\n\tThe amount of seat available in each row:\n\n";
								for (int s=0; s<15; s++)
								{
									int seatAvailable = 0;
									for(int sc=0; sc<30; sc++)
									{
										if (seats[s][sc]=='#')
											seatAvailable++;
									}
									cout << "\n\tRow " << (s+1) << ": "  << seatAvailable;
									cout << endl;
									totalSeat += seatAvailable;
								}
								break;
						
				case 6 : cout	<< "\n\tThe total seats available are " << totalSeat;
								break;
								
				default 	:	break; 
			
			}	
	}	while (choice!=0);

	return 0;
	}


void displayMenu()
{
	cout 	<< "\n\n\t\tMain Menu"
			<< "\n\n\tEnter your selection\n"
			<< "\n\t1)	View available seats"
			<< "\n\t2) 	Purchase ticket"
		 	<< "\n\t3) 	View amount of tickets purchased"
			<< "\n\t4) 	View amount of seats sold"
			<< "\n\t5)	View amount of seats available in each row"
			<< "\n\t6) 	View total seats available"
			<< "\n\n\tor QUIT by pressing 0\n"
			<< "\n\tEnter your selection: ";
			
}


