Okay. I am actually having an issue with logic more-so than with the actual coding of the main method. Here is the background:
Your program will allow the manager to run multiple simulations for different numbers of customers to determine the optimal number of servers to hire. The exact service time for a customer is difficult to predict. Using an average time based on past observations is one possible approach. But, a more realistic strategy is to use average minimum and maximum service times and generate each customer’s service time randomly between these two numbers. The program will allow the manager to set the minimum and maximum length of service time. Predicting how often customers will arrive (represented usually by the customer inter-arrival time) is another issue to be addressed. Again, a strategy similar to generating service times is used. The program will allow the manager to enter minimum and maximum inter-arrival times. The exact time between arrivals for each customer will be generated randomly between the two inter-arrival times.
Your program will read in the following data:
- minimum and maximum service times (in minutes)
- minimum and maximum inter-arrival times (in minutes)
- the number of customers
Once the data items are entered, your program will display
- the average wait time for the customers
- the largest number of customers waiting in line during the entire simulation (maximum queue length).
In a situation like this, where the inputs variables are random in nature, running a simulation just once is not adequate for drawing reliable inferences. So, the program should also allow the manager to run the simulation multiple times.
I wrote out pseudocode for the program and it seemed okay until I was working it by hand and realized that some of the times are actual times and some are durations of time.
The problem that I am having is that the only way I can calculate some things is by using info from a previous customer that is no longer available as that customer has been removed from the queue (adapted from a single linked list).
How can I organize the simulation by time instead of customer arrival? (Algorithm help would be preferred to code as it would most help me understand the logic, which is the problem I am having.)
Here is the code that I have so far in the main method:
/**
* Class FastFoodSimulation simulates customer service at a fast food restaurant.
* This simulation serves as a tool for managers to determine how many servers are needed
* for average amount of customers to be serviced in a timely manner.
*
* @author (Ebony Brewer)
* @version (10272013)
*/
import javax.swing.JOptionPane;
import java.util.Random;
public class FastFoodSimulation
{
public static void main(String args[])
{
int time;
boolean reRun = true;
int maxQueueLength;
int customerCounter;
int serviceTime;
int arrivalTime;
int departureTime;
int waitTime;
int interArrivalTime;
int totalTime;
int totalWaitTime;
do
{
int minServiceTime = Integer.parseInt(JOptionPane.showInputDialog("Enter Minimum Service Time (in minutes)."));
int maxServiceTime = Integer.parseInt(JOptionPane.showInputDialog("Enter Maximum Service Time (in minutes)."));
int minInterArrivalTime = Integer.parseInt(JOptionPane.showInputDialog("Enter Minimum Time Between Customers(in minutes)."));
int maxInterArrivalTime = Integer.parseInt(JOptionPane.showInputDialog("Enter Maximum Time Between Customers(in minutes)."));
int numberOfCustomers = Integer.parseInt(JOptionPane.showInputDialog("Enter Number of Customers to Simulate."));
time = 0;
//LinkedQueue queue = new LinkedQueue();
maxQueueLength = 0;
customerCounter = 0;
arrivalTime = 0;
departureTime = 0;
waitTime = 0;
totalTime = 0;
totalWaitTime = 0;
while(customerCounter < numberOfCustomers)
{
serviceTime = randInt(minServiceTime, maxServiceTime);
interArrivalTime = randInt(minInterArrivalTime, maxInterArrivalTime);
arrivalTime = time + interArrivalTime;
SimulationCustomer cust = new SimulationCustomer();
cust.setServiceTime(serviceTime);
cust.setInterArrivalTime(interArrivalTime);
cust.setArrivalTime(arrivalTime);
if(time == cust.getArrivalTime() && maxQueueLength == 0)
{
//queue.offer(cust);
customerCounter++;
departureTime = arrivalTime + serviceTime;
totalTime = departureTime - arrivalTime;
cust.setWaitTime(0);
cust.setDepartureTime(departureTime);
cust.setTotalTime(totalTime);
//int length = queue.size();
//if(length > maxQueueLength)
// maxQueueLength = length;
//queue.remove(cust);
time++;
}
else if(time == cust.getArrivalTime() && maxQueueLength >= 1)
{
//queue.offer(cust);
customerCounter++;
}
}
}
while(reRun);
}
/**
* Returns a psuedo-random number between min and max, inclusive.
* The difference between min and max can be at most
* Integer.MAX_VALUE - 1
* By Greg Case @ http://stackoverflow.com/questions/363681/generating-random-numbers-in-a-range-with-java
*
* @param min Minimim value
* @param max Maximim value. Must be greater than min.
* @return Integer between min and max, inclusive.
* @see java.util.Random#nextInt(int)
*/
public static int randInt(int min, int max)
{
// Usually this can be a field rather than a method variable
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}