// // COMP102 // Example 14: Supermarket Checkout Simulation // Queues with random arrival and service times. // // Paul E. Dunne 24/11/99 // import java.io.*; import Customer; import Queue; public class CheckItAllOut { public static InputStreamReader input = new InputStreamReader(System.in); public static BufferedReader keyboardInput = new BufferedReader(input); //************************************************************ // User specified fields to be input at the simulation start * //************************************************************ static int MAX_CUSTOMERS; // Maximum number of customers in system. static int MAX_TILLS; // The number of checkout tills. static int MAX_ITEMS; // The maximum number of items. static int ROUNDS_TO_SIMULATE; // Number of simulation rounds during which // new customers can arrive. static double ARRIVAL_RATE; // Should be a value between 0.0 and 1.0, // higher value == more frequent arrivals. //*********************************************************** // The following fields allow a user to specify statistical * // information that should be output. * //*********************************************************** static int UPDATE_STATS=1; // Show till status every // UPDATE_STATS iterations static boolean FINAL_ONLY; // Only show final customer records but // do not show statistics every iteration. static boolean AVERAGE_MAX_ONLY; // Only show average and maximum values for // Queue Lengths, Service Times, etc. //************************************************************ // These fields hold various statistics that can be collated * // as a simulation procedes. * //************************************************************ static int customers_left=0; // The customers remaining in queues static int customers_through=0; // The total number of customers. static int longest_queue_length =0; static int longest_queue; //**************************************************** // Fields associated with the Simulation Process * //**************************************************** static Queue[] CheckOutQueues; // The Queue at each Checkout; static int[] TimeLeftAtTill; // Time remaining before a till is free. static Customer[] CustomerStats; // Records Customer data. //*************************************************** // Class Methods * //*************************************************** // // Input user settings for this simulation // public static void SetSimulationParameters() throws IOException { char YesNo; // To indicate statistics required. System.out.print("How many checkout tills are there?:"); MAX_TILLS = new Integer (keyboardInput.readLine()).intValue(); System.out.println(MAX_TILLS); System.out.print("What is the maximum number of customers?"); MAX_CUSTOMERS = new Integer (keyboardInput.readLine()).intValue(); System.out.println(MAX_CUSTOMERS); System.out.print("What is the maximum number of items a single customer might buy?:"); MAX_ITEMS = new Integer (keyboardInput.readLine()).intValue(); System.out.println(MAX_ITEMS); System.out.print("For how many rounds are the Check Out Queues Open?:"); ROUNDS_TO_SIMULATE = new Integer (keyboardInput.readLine()).intValue(); System.out.println(ROUNDS_TO_SIMULATE); System.out.print("How frequently do customers arrive at checkout (0.0 < r < 1.0)?:"); ARRIVAL_RATE = new Double (keyboardInput.readLine()).doubleValue(); System.out.println(ARRIVAL_RATE); System.out.print("How often should Checkout statistics be shown?:"); UPDATE_STATS = new Integer (keyboardInput.readLine()).intValue(); System.out.println(UPDATE_STATS); System.out.print("Show status of each queue until completed? (y/n):"); YesNo = keyboardInput.readLine().charAt(0); if ( (YesNo=='y') || (YesNo=='Y') ) FINAL_ONLY = false; else FINAL_ONLY = true; System.out.println(YesNo); System.out.print("Output Averages and Maxima Only? (y/n):"); YesNo = keyboardInput.readLine().charAt(0); if ( (YesNo=='y') || (YesNo=='Y') ) AVERAGE_MAX_ONLY = true; else AVERAGE_MAX_ONLY = false; System.out.println(YesNo); System.out.println(); } //********************************************* // Initiate Simulation Structures * //********************************************* public static void StartUpQueues() { CheckOutQueues = new Queue[MAX_TILLS]; TimeLeftAtTill = new int[MAX_TILLS]; for (int i=0; i longest_wait) { longest_wait = temp_wait; longest_waiting=i; }; }; average_wait = total_waiting_time/customers_through; System.out.println("Longest wait was "+ longest_wait+ " for customer "+ longest_waiting + " in check out queue "+ CustomerStats[longest_waiting].get_checkout()); System.out.println("Average waiting time was "+average_wait); } //*********************************************************************** // Select a Queue: A simple `greedy' method is used in which any new * // customer is added to the current shortest queue * // To compare different strategies, all that is needed is to replace * // this method with an alternative. * //*********************************************************************** public static int ChooseQueueToJoin ( Queue[] CheckOuts ) { int shortest_so_far =0; for (int i=1; i longest_queue_length) { longest_queue_length = CheckOutQueues[put_on_queue].GetWaiting(); longest_queue = put_on_queue; }; customers_left++; // One more customer in the system. customers_through++; // One more customer seen. }; //***************************************************************** // Output Statistics as required * //***************************************************************** if ( (on_round_number%UPDATE_STATS==0) && (!FINAL_ONLY) && (!AVERAGE_MAX_ONLY) ) OutputCheckOutStats(on_round_number); }; //************************************************************* // Simulation rounds completed, so output statistcal info. * //************************************************************* if (AVERAGE_MAX_ONLY) OutputAverageMaxStats(on_round_number); else { OutputCustomerStats(); OutputAverageMaxStats(on_round_number); }; } }