//Proper Code & Indentation (2.0)//2.0
//Declaration & Function (2.0)//0.5
//Accumulated Task in Coding (24.0)//23.0
//Output (2.0)//2.0

//TOTAL = 29/30

//Saharulnizam Hakimy bin Shobri
//A19EC0205
//Chee Wai Lum
//A19EC0032
#include<iostream>
#include<iomanip>
#include<cstdlib>
using namespace std;

//Function declaration
int Menu();
void SeatPlan();
int ticketSold();
int ticketSales(float []);
void seatRow();

//constant variable
const char SOLD='*';	//seat is occupied
const char EMPTY='#';	//seat is available
const int row=15;
const int col=30;

//declare as global so all function can use
char map[row][col]={{'*','*','*','#','#','#','*','*','*','#','#','#','*','#','#','#','#','#','#','#','#','*','*','*','*','*','#','#','#','#'},
					{'#','#','#','#','*','*','*','*','*','*','*','*','*','*','*','*','*','#','#','#','#','*','*','*','*','*','*','*','#','#'},
                  	{'*','*','#','#','#','*','*','*','*','*','*','*','*','*','*','#','#','#','#','#','#','#','#','*','*','*','*','#','#','#'},
                  	{'*','*','#','#','#','#','#','#','*','*','*','*','*','*','*','*','*','*','*','*','*','*','#','#','*','*','*','*','*','*'},
                  	{'*','*','*','*','*','*','*','*','#','#','#','#','#','*','*','*','*','*','*','*','*','*','#','#','#','#','#','#','#','#'},
                  	{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','*','*','*','*','*','*','*','*','*','*','*','*','#','#','#','#'},
                  	{'#','#','#','#','#','#','#','*','*','*','*','*','*','*','*','*','*','*','*','#','#','#','#','#','#','#','#','#','#','#'},
                  	{'*','*','*','*','*','*','*','*','*','*','*','*','#','#','*','*','*','*','#','#','#','#','#','#','#','#','#','#','#','#'},
                  	{'#','#','#','#','#','#','#','#','#','*','*','*','*','*','#','#','#','#','#','#','#','#','#','#','#','#','*','*','*','*'},
                  	{'#','#','#','#','#','*','*','*','*','*','*','*','*','*','*','*','*','*','#','#','#','#','#','#','#','#','#','#','#','#'},
                  	{'#','*','*','*','*','*','*','*','*','*','*','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','*','*'},
                  	{'#','#','#','#','#','#','#','#','#','#','#','#','#','*','*','*','*','*','*','*','*','#','#','#','#','#','#','#','#','*'},
           	      	{'#','#','#','*','*','*','*','*','*','*','*','*','*','*','#','#','#','#','#','#','#','#','*','*','#','#','#','#','#','#'},
            	  	{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
				  	{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}};
				  	//2.0
int main(){
	float price[row];
	int row2, col2, loop=1, seatSold, choice, totalSeat=450, seatAvailable=0;

	cout<<"\n\t\t\tTheater Ticket System\n\n\n";
	cout<<fixed<<setprecision(2);
	//get price for each row
	for(int k=0; k<row; k++){
		cout<<"\t\tPlease enter the price for row "<<(k+1)<<": ";
		cin>>price[k];
		while(price[k]<=0){
			cout<<"\n\t\tSorry, the price entered is invalid.\n";
			cout<<"\t\tPlease enter the value of price greater than 0.\n\n";
			cout<<"\t\tPlease enter the price for row "<<(k+1)<<": ";
			cin>>price[k];
		}
	}//2.0
	system("cls");	//clear screen
	SeatPlan();		//display the seating plan
		
	do{
		int cost=0, totalCost=0;
		choice=Menu();		//show menu and make choice
		switch(choice){//3.0	
			case 1:
				system("cls");
				cout<<"\t\t\t    View Seat Prices\n\n";
				for (int l=0; l<row; l++){
					cout<<"\t\t\t  The price for row "<<(l+1)<<": RM ";
					cout<<price[l]<<endl;
				}//2.5
				break;
			case 2:
				system("cls");
				cout<<"\t\t\t   Purchase a Ticket\n\n";
				do{
					cout<<"\t\t\tPlease select the row: ";
					cin>>row2;
					while(row2<1||row2>15){
						cout<<"\n\t\tSorry, the row number you entered is invalid.\n";
						cout<<"\t\tPlease enter the row as from row 1 to 15 only.\n\n";
						cout<<"\t\t\tPlease select the seat row: ";
						cin>>row2;
					}//1.5
					
					cout<<"\t\t\tPlease select the seat: ";
					cin>>col2;
					while(col2<1||col2>30){
						cout<<"\n\t\tSorry, the seat number you entered is invalid.\n";
						cout<<"\t\tPlease enter the seat number as from 1 to 30 only.\n\n";
						cout<<"\t\t\tPlease select the seat: ";
						cin>>col2;
					}//1.5
					
					--row2, --col2;		//adjustment to make sure it is align with array counting
					
					if(map[row2][col2]==SOLD){
						cout<<"\n\t\t\tSorry, that seat has been sold.\n";
						cout<<"\t\t\tPlease select a new seat.\n";
						cout<<endl;
					}//1.0
					else{
						cout<<fixed<<setprecision(2);
						cost=price[row2];
						LOOP2:
							cout<<"\t\t\t   That ticket costs: "<<cost<<endl;
							cout<<"\t\t   Confirm Purchase? Enter (1 = YES / 2 = NO): ";
							cin>>seatSold;
						
							if(seatSold==1){ 
								cout<<"\t\t\tTicket purchase has been confirmed.\n";	
								map[row2][col2]=SOLD;
								totalCost+=cost;
							}
							else if(seatSold==2){
								goto LOOP1;
							}
							else{
								cout<<"\n\t\t\tThe value entered is invalid!\n";
								cout<<"\t\t\tPlease enter either 1 or 2 only.\n\n";
								goto LOOP2;
							}
						LOOP1:	
							cout<<"\t\tWould you like to look at another seat? Enter (1 = YES / 2 = NO): ";
							cin>>loop;
							system("cls");
					}//3.0
				}while(loop==1);
				cout<<fixed<<setprecision(2);
				cout<<"\n\n\t   The total prices for the ticket purchased: RM "<<totalCost<<"\n\n";
				break;
			case 3:
				system("cls");
				cout<<"\t\t\t    View Available Seats\n";
				SeatPlan();
				break;
			case 4:
				system("cls");
				cout<<fixed<<setprecision(2);
				cout<<"\t\t\t    View Seat Sales\n\n";
				cout<<"\t\tTotal number of tickets sold: "<<ticketSold()<<" tickets.\n\n";
				cout<<"\t\tTotal sales of tickets sold: RM "<<ticketSales(price)<<"\n\n";
				break;
			case 5:
				system("cls");
				cout<<"\t\t\t   View Seat Status\n\n";
				cout<<"\t\tTotal number of seats sold: "<<ticketSold()<<" seats.\n\n";	//number of tickets sold=number of seats sold
				seatRow();
				seatAvailable=(totalSeat-ticketSold());
				cout<<"\n\tTotal number of seat available in entire theater: "<<seatAvailable<<" seats.\n\n";
				break;
			case 6:
				system("cls");
				cout<<"\n\n\tYou have successfully exit from the system.\n";
				break;
			default :
				system("CLS"); 
				cout<<"\n\n\t\t\t\tError input\n";//0.5
		}
	} while (choice != 6);
return 0;
}

int Menu(){
	int MenuChoice;
		cout<<endl<<endl;
		cout<<"\t\t\t\tMAIN MENU\n";
		cout<<"\t\t\t 1. View Seat Prices.\n";
		cout<<"\t\t\t 2. Purchase a Ticket.\n";
		cout<<"\t\t\t 3. View Available Seats.\n";
		cout<<"\t\t\t 4. View Ticket Sales.\n";
		cout<<"\t\t\t 5. View Seat Status.\n";
		cout<<"\t\t\t 6. Quit the program.\n";
		cout<<"\t\t\t ________________________\n\n";
		cout<<"\t\t\t Please enter your choice: ";
		cin>>MenuChoice;
		cout<<endl<<endl;
	return MenuChoice;
}//2.0

void SeatPlan(){
	cout<<"\n\t\t\t\tSeating Plan\n\n";
	cout<<"\t 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0\n";
		for(int i=0; i<15; i++){
			cout<<endl<<"Row "<<(i+1)<<"\t";
			for (int j=0; j<30; j++){
				cout<<" "<<map[i][j];
			}
		}
			cout<<endl;
}//3.5

int ticketSold(){
	int k=0;	
	for(int i=0; i<row; i++)
		for (int j=0; j<col; j++)
			if(map[i][j]==SOLD){
				++k;	
			}
	return k;
}

int ticketSales(float price[]){
	int k=0;
	for(int i=0; i<row; i++){
		int l=0;
		for (int j=0; j<col; j++){
			if(map[i][j]==SOLD)
				++l;	
		}
		l*=price[i];
		k+=l;
	}//1.0
	return k;
}

void seatRow(){
	cout<<"\t     Total number of seats available in each row: \n";
	for(int i=0; i<row; i++){
		int k=0;
		for(int j=0; j<col; j++){
			if(map[i][j]==EMPTY)
				++k;
		}
		cout<<"\t\t\t    Row "<<(i+1)<<": "<<k<<endl;
	}
}
