Answer to Question #158580 in Java | JSP | JSF for ajay

Question #158580

Suppose a business person launches new cinema at Islamabad and ask his team to develop a ticket system for box office. He assigns some requirements about system that how should it work. The requirements are such a way that there are only '5' number of box office windows in the theatre. Each window can have at max '20' number of people waiting in line. To start with, only one window is opened. If the number of people waiting in line in that window exceeds 20, then the next window is opened and people can join the line in that window. Likewise, if both the first and second windows have n number of people waiting in each queue, then a third window is opened. This can go on until the maximum number of windows w is reached. Let us assume that once a window is opened it never closes. A new window is only opened if all open windows are full. Each person can buy only one ticket. So, the system should not allot more than one ticket per person. Let us assume that the system issues one ticket each across all open windows. When a ticket is issued, the count of the number of people in each open queue is reduced by 1.

When a new person has to join the queue, the system has to prompt him to join a queue such that they are issued a ticket as fast as possible. The system prompts the person based on these factors: o First it looks for an open window with the least number of people and prompts that window number. If more than one window has the least number of people, then the system can prompt the person to join the first window (smaller window ld) it encounters with the least number of people. If the queues of all open windows are full and a new window can be opened, then the new person is prompted to join the new queue for the new box office window. O If all queues for all windows are full, a corresponding message is displayed. That person need not be considered in the next iteration .After a queue is prompted to a person, the person or system cannot change the queue.

Implement the system based on above scenario in java using suitable data structure.



1
Expert's answer
2021-01-26T17:46:32-0500
import java.util.ArrayList;


class Customer{
	
}
class TicketingSystem{
	ArrayList<ArrayList<Customer>> windows = new ArrayList<ArrayList<Customer>>();
	int openWindows = 1; // One window is open when system starts
	boolean isFull = false; // Becomes true if each window has 20 customers
	
	void createWindows() { // Create empty arrayLists of try Customer
		for (int i = 0; i < 5; i++) {
			windows.add(new ArrayList<Customer>());
		}
	}
	
	void displayQueueLengths() { // Display current lengths of queues for each window
		System.out.print("New queue lengths>>> ");
		for (int i = 0; i < openWindows; i++) {
			System.out.print("window #" + (i + 1) + ":" + windows.get(i).size() + " ");
		}
		System.out.println();
	}
	void addCustomer() { // Add new customer to appropriate window
		int shortestQueue = 0;
		int shortestQueueLength = windows.get(0).size();
		
		for (int windowIndex = 0; windowIndex < openWindows; windowIndex++) { // Get shortest queue
			int queueLength = windows.get(windowIndex).size();
			if (queueLength < shortestQueueLength) {
				shortestQueue = windowIndex;
				shortestQueueLength = queueLength;
			}
		}
		
		if (windows.get(shortestQueue).size() == 20 & openWindows < 5) { // Open a window
			openWindows += 1;
			windows.get(openWindows - 1).add(new Customer());
			System.out.println("Customer added to window #" + openWindows);
		}
		else if (windows.get(shortestQueue).size() < 20){ 
			windows.get(shortestQueue).add(new Customer());
			System.out.println("Customer added to window #" + (shortestQueue + 1));
		}
		else { // All windows are full
			System.out.println("All queues are full.");
			isFull = true;
		}
		
	}
	
	void sellTickets() { // Sell tickets to customers in front of each queue
		for (int windowIndex = 0; windowIndex < openWindows; windowIndex++) {
			if (windows.get(windowIndex).size() > 0) {
				// Remove customer from queue after they buy a ticket
				windows.get(windowIndex).remove(0);
			}
		}
	}
}


public class Q158580 {
	
	public static void main(String[] args) {
		TicketingSystem ts = new TicketingSystem();
		ts.createWindows();
		
		int counter = 0;
		while (true) {
			if (ts.isFull == true) {
				break; // No space for more customers
			}
			
			// Assuming tickets are sold each time 6 customers join the queue 
			if (counter % 6 == 0) {
				ts.sellTickets();
				System.out.println("Tickets sold!");
				ts.displayQueueLengths();
			}
			
			ts.addCustomer();
			ts.displayQueueLengths();
			counter ++;
		}
		
	}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog