import java.util.List; import java.util.Iterator; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application that directs a discrete event simulation in which ** customers arrive, wait in a queue, are given a service, and then ** depart. Reported are the average and maximum wait times in the ** queue. ** ** Author: R. McCloskey ** Date: March 2026 */ public class SimulationApp { public static void main(String[] args) throws FileNotFoundException { int randSeed; int minIAT, maxIAT, minST, maxST; int closingTime, numClerks; // Get the inputs if (args.length == 1) { // If there is one command-line argument, it is taken to be // the name of a file containing the seven inputs. Scanner input = new Scanner(new File(args[0])); randSeed = input.nextInt(); minIAT = input.nextInt(); // minimum inter-arrival gap maxIAT = input.nextInt(); // maximum inter-arrival gap minST = input.nextInt(); // minimum service duration maxST = input.nextInt(); // maximum service duration closingTime = input.nextInt(); // time after which no new customers arrive numClerks = input.nextInt(); // # of tellers to provide service } else { // Otherwise, it is assumed that there are seven command-line // arguments that provide the input values. randSeed = Integer.parseInt(args[0]); minIAT = Integer.parseInt(args[1]); maxIAT = Integer.parseInt(args[2]); minST = Integer.parseInt(args[3]); maxST = Integer.parseInt(args[4]); closingTime = Integer.parseInt(args[5]); numClerks = Integer.parseInt(args[6]); } // If the inputs don't make sense, abort. if (minIAT > maxIAT || minST > maxST || closingTime <= 0 || numClerks <= 0) { System.out.println("Invalid input; program aborting"); System.exit(0); } // Echo the inputs System.out.println("Inputs:"); System.out.println(" Seed for Random:" + randSeed); System.out.println(" Minimum interarrival time/gap:" + minIAT); System.out.println(" Maximum interarrival time/gap:" + maxIAT); System.out.println(" Minimum service duration:" + minST); System.out.println(" Maximum service duration:" + maxST); System.out.println(" Closing time:" + closingTime); System.out.println(" # of clerks:" + numClerks); System.out.println(); // Do the simulation. CustomerGenerator cg; cg = new CustomerGenerator(randSeed, minIAT, maxIAT, minST, maxST); Simulation s = new Simulation(cg); s.simulate(closingTime, numClerks); // Display the customers who got service. List customerList = s.customers(); System.out.println("\n\nList of serviced customers:"); System.out.println(customerList); // Display average and maximum wait times. System.out.printf("\nAverage wait time: %.2f", averageWaitTime(customerList)); System.out.printf("\nMaximum wait time: %d\n", maxWaitTime(customerList)); } /* Returns the average wait time among the customers in the given list. ** pre: customerList.size() != 0 */ public static double averageWaitTime(List customerList) { int sumWaitTimes = 0; Iterator iter = customerList.iterator(); while (iter.hasNext()) { sumWaitTimes = sumWaitTimes + iter.next().waitTime(); } double avgWaitTime = sumWaitTimes / customerList.size(); return avgWaitTime; } /* Returns the maximum wait time among the customers in the given list. ** pre: customerList.size() != 0 */ public static int maxWaitTime(List customerList) { int maxSoFar = -1; Iterator iter = customerList.iterator(); while (iter.hasNext()) { int waitTime = iter.next().waitTime(); if (waitTime > maxSoFar) { maxSoFar = waitTime; } } return maxSoFar; } }