// Noor Arinie binti Norhalil
// Lokessh A/L Pathmanatan

#include <iostream>
#include <fstream>
#include <iomanip>

int displayMenu();
void TotalPrice(float);
using namespace std;

int main()
{
    int r = 15 ;			// r is rows
    int c = 30 ;			// c is columns (seats)
    int row , column , answer , ans ;
    float rowprice[r] ;
    float total = 0 ;
    char map[r][c] ;
    int choice , count = 0 , cnt = 0 , ct = 0 ;
    ifstream inFile ;

    inFile.open("seat.txt") ;

    cout << "Enter the seats price for each row:\n";
    for(int i = 0 ; i < 15 ; i++)
    {
        cout << "Row " << (i + 1) << " : ";
        cin >> rowprice[i] ;
    }

    for(int i = 0 ; i < 15 ; i++)
    {
        for(int j = 0 ; j < 30 ; j++)
        {
            inFile >> map[i][j] ;
        }
    }

    cout << "\n\t\tSeats\n" ;
    cout << "\t123456789012345678901234567890\n" ;

    for(int i = 0 ; i < 15 ; i++)
    {
        cout << "Row " << (i + 1) << "\t" ;

        for(int j = 0 ; j < 30 ; j++)
        {
            cout << map[i][j] ;
        }
        cout << endl ;
    }

    do
    {

        choice = displayMenu() ;

        switch(choice)
        {
            case 1 : cout << "\nView Seat Prices\n\n";

                for (int i = 0 ; i < 15 ; i++)
                {
                    cout << "The price for row " << (i + 1) << ": RM ";
                    cout << fixed << showpoint << setprecision(2) << rowprice[i] << endl;
                }
                break ;

            case 2 :
            {

                cout << "\nPurchase a Ticket\n\n" ;

                A :
                cout << "Please select the row you would like to sit in : ";
                cin >> row ;
                cout << "Please select the seat you would like to sit in : ";
                cin >> column ;
                int i = row - 1 ;
                int j = column - 1 ;

                if (map [i][j] == '*')
                {
                    cout << "Sorry that seat is sold-out, Please select a new seat.\n";
                    goto B;
                }
                else
                {

                    total = total + rowprice[i];
                    cout << "That ticket costs: RM " << fixed << showpoint << setprecision(2) << rowprice[i] << endl;
                    cout << "Confirm Purchase? Enter (1 = YES / 2 = NO)" ;
                    cin >> answer ;

                    if (answer == 1)
                    {
                        cout << "Your ticket purchase has been confirmed." << endl;
                        map [i][j] = '*';
                    }
                    else
                    {
                        B:
                        {
                            cout << "Would you like to look at another seat? (1 = YES / 2 = NO)" ;
                            cin >> ans ;
                            if(ans == 1)
                            {
                                goto A;
                            }
                        }
                    }
                }
            }
                break ;

            case 3 : cout << "\n\nView seats available.\n" ;

                cout << "\n\t\tSeats\n" ;
                cout << "\t123456789012345678901234567890\n" ;

                for(int i = 0 ; i < 15 ; i++)
                {
                    cout << "Row " << (i + 1) << "\t" ;

                    for(int j = 0 ; j < 30 ; j++)
                    {
                        cout << map[i][j] ;
                    }
                    cout << endl ;
                }
                break ;

            case 4: cout << "\nView Seat Sales\n\n" ;  // Total sales
                TotalPrice(total) ;

                break ;



            case 5: cout << "\nTotal seats have been sold.\n" ;
                count = 0 ;
                for(int i = 0 ; i < 15 ; i++)
                {
                    for(int j = 0 ; j < 30 ; j++)
                    {
                        if(map[i][j] == '*')
                            count = count + 1 ;
                    }
                }
                cout << count << " have been sold.\n" ;
                break ;

            case 6 : cout << "\nTotal seats available in each row.\n";
                for(int i = 0 ; i < 15 ; i++)
                {
                    cnt = 30 ;
                    for(int j = 0 ; j < 30 ; j++)
                    {
                        if(map[i][j] == '*')
                            cnt = cnt + - 1 ;
                    }
                    cout << "Row " << (i + 1) << " : "<< cnt << endl ;

                }
                break ;

            case 7 : cout << "\nTotal seats available in the entire auditorium.\n" ;
                ct = 450 ;

                for(int i = 0 ; i < 15 ; i++)
                {
                    for(int j = 0 ; j< 30 ; j++)
                    {
                        if(map[i][j] == '*')
                            ct = ct + - 1 ;
                    }


                }
                cout << ct << " seats available.\n" << endl ;
                break ;

            case 8 : cout << "\nThanks for purchasing.\n";
                break ;

            default : cout << "Error input\n";
        }


    }
    while(choice!=8) ;

    inFile.close() ;

    return 0 ;
}
int displayMenu()
{
    int choice;
    cout << "\n\tMenu\n";
    cout << "1. View seat prices.\n" ;
    cout << "2. Purchase a ticket.\n" ;
    cout << "3. View available seats.\n" ;
    cout << "4. View ticket sales.\n" ;
    cout << "5. Total seats have been sold.\n" ;
    cout << "6. Total seats available in each row.\n" ;
    cout << "7. Total seats available in the entire auditorium.\n" ;
    cout << "8. Quit the program.\n\n" ;
    cout << "Enter your choice : " ;
    cin >> choice ;
    return choice ;
}

void TotalPrice(float val1)
{
    cout << "The total price is : RM " << fixed << showpoint << setprecision(2) << val1 << endl ;
}

