// Arup Guha
// 9/4/2026
// Solution to 2026 Fall COP 3330 Program 3: Ticketing System Version 1

import java.util.*;

public class ticketing_v1 {

	final public static int TOTAL_ROWS = 26;
	final public static int TOTAL_COLS = 60;
	final public static int REGION_ROWS = 13;
	final public static int REGION_COLS = 20;
	
	final public static int[] COST = {200, 300, 200, 100, 150, 100};

	public static void main(String[] args) {
	
		// Set up several required variables for main.
		Scanner stdin = new Scanner(System.in);
		char[][] seats = makeEmptyChart();
		int totalRevenue = 0, totalTickets = 0;
		int choice = 0;
		
		// Process choices until quit.
		do {
			
			// Get choice.
			choice = getMenuChoice(stdin);
			
			// Buying tickets.
			if (choice == 1) {
				
				// Get the region.
				int region = getRegion(stdin);
				
				// Get # of tickets.
				System.out.println("How many tickets do you want?");
				int numTickets = stdin.nextInt();
				
				// Update revenue.
				int thisSale = buySeats(seats, region, numTickets);
				totalRevenue += thisSale;
				
				// This is tricky.
				if (thisSale != 0)
					totalTickets += numTickets;
			}
			
			// Prints chart.
			else if (choice == 2) {
				System.out.println();
				System.out.println("Here is the current seating chart:\n");
				printChart(seats);
			}
			
			// Report with tickets sold, total revenue.
			else if (choice == 3) {
				System.out.println();
				System.out.println("Current tickets sold = "+totalTickets);
				System.out.println("Current revenue = $"+totalRevenue);
				System.out.println();
			}
			
		} while (choice != 4);

		// Ending message.
		System.out.println();
		System.out.println("Thank you for using Ticketing System V1");
		System.out.println("Total tickets sold = "+totalTickets);
		System.out.println("Total revenue = $"+totalRevenue);
		System.out.println("Here is the final seating chart:\n");
		printChart(seats);
	}
	
	// Uses the Scanner stdin to prompt the user for a region, continuing to do so until
	// the user enters a valid region and this value is returned.
	public static int getRegion(Scanner stdin) {
		
		// Initial value, will get over-written.
		int region = 0;
		
		do {
			
			// Get user's choice.
			System.out.println("Which region(1-6) do you want your seats in?");
			region = stdin.nextInt();
			
			// Error message.
			if (region < 1 || region > 6)
				System.out.println("Please enter a region in between 1 and 6.");
			
		} while (region < 1 || region > 6);
		
		// Ta da!
		return region;
	}
	
	// Uses the Scanner stdin to prompt the user for a menu choice, continuing to do so
	// until the user enters a valid menu selection. This value is returned.
	public static int getMenuChoice(Scanner stdin) {
		

		int choice = 0;
		
		// Loop until there's a good choice.
		do {
		
			// Print choices and read in user's choice.
			System.out.println("Please select a choice on the menu below:");
			System.out.println("1. Buy Concert Tickets");
			System.out.println("2. See Stadium Ticket Map");
			System.out.println("3. View Total Revenue, Tickets Sold");
			System.out.println("4. Exit Ticketing System V1");
			choice = stdin.nextInt();
			
			// Error case.
			if (choice < 1 || choice > 4)
				System.out.println("\nSorry that choice is not valid. Please try again.\n");
			
		} while (choice < 1 || choice > 4);
		
		// Ta da!
		return choice;
	}
	
	// Returns an empty seating chart.
	public static char[][] makeEmptyChart() {
		
		// Allocate space.
		char[][] seats = new char[TOTAL_ROWS][TOTAL_COLS];
		
		// Fill the whole array so that all seats are not taken.
		for (int i=0; i<TOTAL_ROWS; i++)
			Arrays.fill(seats[i], 'O');
		
		// Return it.
		return seats;
	}
	
	// Prints the chart stored in seats.
	public static void printChart(char[][] seats) {
	
		// Just prints units digit of seat number.
		System.out.print("  ");
		for (int col=0; col<TOTAL_COLS; col++) {
			System.out.print((col+1)%10);
			
			// Gaps between regions (columns)
			if (col%REGION_COLS == REGION_COLS-1)
				System.out.print(" ");
		}
		System.out.println("\n");
		
		// Now go through the rows.
		for (int row=0; row<TOTAL_ROWS; row++) {
			
			// Row label.
			System.out.print((char)('A'+row)+" ");
			
			// Go left to right.
			for (int col=0; col<TOTAL_COLS; col++) {
				System.out.print(seats[row][col]);
			
				// Separates out columns.
				if (col%REGION_COLS == REGION_COLS-1)
					System.out.print(" ");
			}
			System.out.println();
			
			// Separates out regions by row so regions 1,2,3 from 4,5,6.
			if (row%REGION_ROWS == REGION_ROWS - 1)
				System.out.println();
		}
	
	}

	// Attempts to perform buying numTickets tickets from the region region for the
	// concert with the current seating chart seats. Returns total spent on tickets.
	// 0 if no tickets bought due to a request that is too high or too low.
	public static int buySeats(char[][] seats, int region, int numTickets) {
		
		// We need to know this first.
		int totalOpen = numOpenSeats(seats, region);
		
		// Error case, not enough tickets; none sold.
		if (numTickets > totalOpen) {
			System.out.println("\nSorry the transaction couldn't be made.");
			System.out.println("Not enough available seats.\n");
			return 0;
		}
		
		// Another error case.
		else if (numTickets < 0) {
			System.out.println("\nSorry you can't buy a negative number of seats.\n");
			return 0;
		}
		
		// Header for this case.
		System.out.println("\nHere are the tickets you are receiving:");
		
		// Keep track of tickets sold.
		int sold = 0;
		
		// Number of regions in a single row.
		int colGroups = TOTAL_COLS/REGION_COLS;
		
		// Compute starting row and column of this region.
		int startRow = REGION_ROWS*((region-1)/colGroups);
		int startCol = REGION_COLS*((region-1)%colGroups);
		
		// This goes through every seat in the order we want.
		for (int i= startRow; i<startRow+REGION_ROWS; i++) {
			for (int j=startCol; j<startCol+REGION_COLS; j++) {
				
				// An open seat! Buy it!
				if (seats[i][j] == 'O') {
					sold++;
					seats[i][j] = 'X';
					System.out.print((char)('A'+i)+""+j+" ");
				}
				
				// For our spacing.
				if (sold>0 && sold%REGION_COLS == 0)
					System.out.println();
				
				// Need to get out so I don't buy too many!
				if (sold == numTickets) break;
			}
			
			// Same here.
			if (sold == numTickets) break;
		}
		
		// For formatting.
		if (sold%REGION_COLS != 0)
			System.out.println();
		System.out.println();
		
		// This is the cost.
		return numTickets*COST[region-1];
	}

	// Returns the number of open seats in the region region in the seating chart seats.
	public static int numOpenSeats(char[][] seats, int region) {
		
		// Set all of this up so we can loop through the region.
		int res = 0;
		int colGroups = TOTAL_COLS/REGION_COLS;
		int startRow = REGION_ROWS*((region-1)/colGroups);
		int startCol = REGION_COLS*((region-1)%colGroups);
		
		// This goes through each seat in the region.
		for (int i= startRow; i<startRow+REGION_ROWS; i++)
			for (int j=startCol; j<startCol+REGION_COLS; j++)

				// An open one, so count it!
				if (seats[i][j] == 'O')
					res++;
				
		// This is the number we need.
		return res;
	}
	
}