// Arup Guha
// 9/4/2026
// Solution to 2026 Fall COP 3330 Program 3: Ticketing System Version 1

/*** 
	 11/10/2026
	 Edited my P3 solution to add Exceptions and error checking for P8 
***/

import java.util.*;

public class ticketing_v3 {

	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 = 0;
				
				// Keep going until we get a number.
				while (true) {
				
					try {
						numTickets = stdin.nextInt();
						break;
					}
					catch (InputMismatchException e) {
						System.out.println("Sorry you must enter a positive number to answer this question.");
						stdin.next();
					}
				}
				
				// Update revenue.
				int thisSale = 0;
				
				// Try this out.
				try {
					thisSale = buySeats(seats, region, numTickets);
				}
				catch (NegativeSeatException e) {
					System.out.println("Sorry, you can't be a negative number of seats. No seats will be sold this time.");
				}
				
				// Update the sale. Will work if the exception is caught.
				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.
	/*** Simplified with call to doIORegion ***/
	public static int getRegion(Scanner stdin) {
		
		// Initial value, will get over-written.
		int region = 0;
		
		// Don't do the loop control here.
		while (true) {
			
			// Try this.
			try {
				region = doIORegion(stdin);
				
				// We can get out.
				break;
			}
			
			// If you mess up, we do this.
			catch (InvalidRegionException e) {
				System.out.println("Sorry, a valid region must be in between 1 and 6, inclusive.");
			}
			
			// We'll recover from this also.
			catch (InputMismatchException e) {
				System.out.println("Sorry, you must enter an integer, specifically in between 1 and 6, inclusive.");
				stdin.next();
			}
		}
		
		// Ta da!
		return region;
	}
	
	/*** Newly added method. Just does I/O for getting region from user. ***/
	public static int doIORegion(Scanner stdin) throws InvalidRegionException {
		
		// Get user's choice.
		System.out.println("Which region(1-6) do you want your seats in?");
		int region = stdin.nextInt();

		// Error message.
		if (region < 1 || region > 6)
			throw new InvalidRegionException("Invalid Region: Not in between 1 and 6.");
		
		// If we get here, we're good.
		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.
	/*** Simplified with call to doIOMenu ***/
	public static int getMenuChoice(Scanner stdin) {
		
		int choice = 0;
		
		// Loop until there's a good choice.
		while (true) {
		
			// Try this...
			try {
				choice = doIOMenu(stdin);
				
				// If we get here, we're good and get out.
				break;
			}
			
			// Here's what we do if you didn't follow directions.
			catch (InvalidMenuSelectionException e) {
				System.out.println("Sorry, valid menu choices must be in between 1 and 4, inclusive.");
			}
			
			// We'll recover from this also.
			catch (InputMismatchException e) {
				System.out.println("Sorry, you need to enter a number, specifically in between 1 and 4, inclusive.");
				stdin.next();
			}
		}
		
		// Ta da!
		return choice;
	}
	
	/*** Newly added method. Just does I/O for getting menu choice from user. ***/
	public static int doIOMenu(Scanner stdin) throws InvalidMenuSelectionException {
		
		// Get 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");
		int choice = stdin.nextInt();
			
		// Error message.
		if (choice < 1 || choice > 4)
			throw new InvalidMenuSelectionException("Invalid Menu Choice: Not in between 1 and 4.");
		
		// If we get here, we're good.
		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) throws NegativeSeatException {
		
		// 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) {
			throw new NegativeSeatException("You can't buy a negative number of seats!");
		}
		
		// 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;
	}
	
}