Link to home
Create AccountLog in
Avatar of amhinkel
amhinkelFlag for United States of America

asked on

Java Queue Supermarket Simulation

I am running a simulation for a supermarket checkout with multiple lines.  One super express line, two express lines, and variable number of standard lines.  I need to calculate the total wait time of all customers in each line.  The wait time of a customer is the time from when he enters the queue for a given line, until the checkout processing begins.  

I am having problems with the wait time for customers in line.  For the standard lines, I am getting that all the wait times are equal.  


public class Customer {

private int numItems;
private int simTime; 
private int timeEntered;
private int timeExit;
private int timeStart;
private int startingItems;
	
	public Customer(int items, int entered) {
	
		numItems = items;
		timeEntered = entered;
		startingItems = items;
	}
	
	public int getStartingItems(){
		return startingItems;
	}
	public int getTimeEntered(){
		return timeEntered;
	}
	
	public void timeStart(int time){
		timeStart = time;
	}
	
	public void process(){
		numItems--;
	}
	
	public int getItems(){
		return numItems;
	}
	
	public int exit(int time){
		timeExit = time;
		int waitTime = timeExit - timeEntered - 5*startingItems;
		return waitTime;
	}
	
	
	
}

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;


-------------------------------------------------

public class Simulation {



	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int supExpWaitTime = 0;
		   int numSuper = 10;
		   int numExp = 20;
		   int numStandLines = 3;
		   double arrivalRate = 3600;
		  int maxItems = 50;
		  double maxSimTime = 3600; //seconds (1hour)
		  int currTime = 0;
		  int exp1Items = 0;
		  int exp2Items = 0;
		  int exp1Cust = 0;
		  int exp2Cust = 0;
		 int exp1Free = 0;
		 int exp2Free = 0;
		  boolean interval = true;
		  int time = 0;
		  boolean added = false;
		  int supExpItems = 0;

		  int supExpFreeTime=0;
		  int supExpCust=0;
		  int supExpMax = 0;
		  int exp1Max=0;
		  int exp2Max=0;
		  int exp1WaitTime=0;
		  int exp1FreeTime=0;
		  int exp2WaitTime=0;
		  int exp2FreeTime=0;
		  double arrival = maxSimTime/arrivalRate;
		
		Queue<Customer> supExp = new LinkedList<Customer>();
		Queue<Customer> exp1 = new LinkedList<Customer>();
		Queue<Customer> exp2 = new LinkedList<Customer>();
		
		//Array of standard lines
		
		ArrayList<Integer> custAmount = new ArrayList<Integer>();
		ArrayList<Integer> itemAmount = new ArrayList<Integer>();
		ArrayList<Integer> standWaitTime = new ArrayList<Integer>();
		ArrayList<Integer> standFreeTime = new ArrayList<Integer>();
		
		
		ArrayList<Queue<Customer>> lines = new ArrayList<Queue<Customer>>();
		ArrayList<Integer> sizes = new ArrayList<Integer>();
		for(int i = 0; i<numStandLines; i++ ){
			lines.add(new LinkedList<Customer>());
			sizes.add(0);
			custAmount.add(0);
			itemAmount.add(0);
			standWaitTime.add(0);
			standFreeTime.add(0);
		}
		
		
		
		
		Random rand = new Random();
		
		while(currTime < maxSimTime){
			int randomItemNum = rand.nextInt(maxItems) + 1;
			
			if(interval){
				added = false;
				if(randomItemNum <= numSuper){
				if(supExp.peek()==null){
					supExp.add(new Customer(randomItemNum , currTime));
					if(supExp.size() > supExpMax){
						supExpMax = supExp.size();
					}
					added = true;
				}
				else if(added == false){
					if(exp1.peek()==null){
					exp1.add(new Customer(randomItemNum , currTime));
					if(exp1.size() > exp1Max){
						exp1Max = exp1.size();
					}
					added = true;
				}
				}
				else if(added == false){
					if(exp2.peek()==null){
					exp2.add(new Customer(randomItemNum , currTime));
					if(exp2.size() > exp2Max){
						exp2Max = exp2.size();
					}
					added = true;
				}
				}
				else if(added == false) for(int i = 0; i< lines.size() ; i++){
					if(lines.get(i).isEmpty()){
						lines.get(i).add(new Customer(randomItemNum , currTime));
						if(lines.get(i).size() > sizes.get(i)){
							sizes.set(i, lines.get(i).size());
						}
						added = true;
					}
				}
				else if(added == false) {
					supExp.add(new Customer(randomItemNum , currTime));
					if(supExp.size() > supExpMax){
						supExpMax = supExp.size();
					}
					added = true;
				}
				}
				
				
			if(randomItemNum > numSuper && randomItemNum <= numExp){
				if(added == false){
					if(exp1.peek()==null){
					exp1.add(new Customer(randomItemNum , currTime));
					if(exp1.size() > exp1Max){
						exp1Max = exp1.size();
					}
					added = true;
				}
				}
				else if(added == false){
					if(exp2.peek()==null){
					exp2.add(new Customer(randomItemNum , currTime));
					if(exp2.size() > exp2Max){
						exp2Max = exp2.size();
					}
					added = true;
				}
				}
				
				else if(added == false) {
					for(int i = 0; i< lines.size() ; i++){
					if(lines.get(i).peek()==null){
						lines.get(i).add(new Customer(randomItemNum , currTime));
						if(lines.get(i).size() > sizes.get(i)){
							sizes.set(i, lines.get(i).size());
						}
						added = true;
					}
				}
			}
				
				else if(added == false){
					if(exp1.size() > exp2.size() ){
					exp2.add(new Customer(randomItemNum , currTime));
					if(exp2.size() > exp2Max){
						exp2Max = exp2.size();
					}
					added = true;
				}
				else {
					exp1.add(new Customer(randomItemNum , currTime));
					if(exp1.size() > exp1Max){
						exp1Max = exp1.size();
					}
					added = true;
				}
			}
			}
				
			if(randomItemNum > numExp){
				//Check the rest of the Queues for emptiness
				if(added == false){
					for(int i = 0; i< lines.size() ; i++){
					if(lines.get(i).isEmpty()){
						lines.get(i).add(new Customer(randomItemNum , currTime));
						if(lines.get(i).size() > sizes.get(i)){
							sizes.set(i, lines.get(i).size());
						}
						added = true;
					}
				}
				}
			
			else if(added == false){
			int leastSize = lines.get(0).size();
			for(int i = 1; i< lines.size(); i++){
				int x = lines.get(i).size();
				if(x<leastSize){
					leastSize = x;
				}
			}
				for(int k = 0; k < lines.size(); k++){
					if(lines.get(k).size() == leastSize){
						lines.get(k).add(new Customer(randomItemNum, currTime));
						if(lines.get(k).size() > sizes.get(k)){
							sizes.set(k, lines.get(k).size());
						}
					}
			}
			added = true;
			}
			}
			}
			//Do all processing and get all data for statistics
			if(supExp.peek() != null){
				if(supExp.peek().getItems() > 0){
				supExp.peek().process();
				supExpItems++;
				}
				else if(supExp.peek().getItems() == 0){
					supExpWaitTime = supExpWaitTime + supExp.peek().exit(currTime);
					supExp.poll();
					supExpCust++;
				}
			}
				else{
					supExpFreeTime = supExpFreeTime +5;
				}
			
			
			if(exp1.peek() != null){
				if(exp1.peek().getItems() > 0){
				exp1.peek().process();
				exp1Items++;
				}
				else if(exp1.peek().getItems() == 0){
					int r = exp1.peek().exit(currTime);
					exp1WaitTime = exp1WaitTime + r;
					exp1.poll();
					exp1Cust++;
				}
			}
				else{
					exp1FreeTime = exp1FreeTime +5;
				}
			
			
			if(exp2.peek() != null){
				if(exp2.peek().getItems() != 0){
				exp2.peek().process();
				exp2Items++;
				}
				else if(exp2.peek().getItems() == 0){
					exp2WaitTime = exp2WaitTime + exp2.peek().exit(currTime);
					exp2.poll();
					exp2Cust++;
				}
			}
				else{
					exp2FreeTime = exp2FreeTime +5;
				}
			
				
			for(int i = 0; i<lines.size(); i++){
					if(lines.get(i).peek() != null){
					if(lines.get(i).peek().getItems()!=0){
						lines.get(i).peek().process();
						itemAmount.set(i, itemAmount.get(i)+1);
					}
					
					else if(lines.get(i).peek().getItems() == 0){
						standWaitTime.set(i, standWaitTime.get(i) + lines.get(i).peek().exit(currTime));
						lines.get(i).poll();
						custAmount.set(i, custAmount.get(i)+1);
					}
					}
					else{
						standFreeTime.set(i, standFreeTime.get(i) + 5);
					}
				}
			
			currTime = currTime +5;
			time = time + 5; 
			if(time >= arrival){
				interval = true;
				time = 0; 
			}
			else interval = false;
			
			}
		System.out.println("The total waiting time for the Super Express Lane is:" + supExpWaitTime);
		System.out.println("The total free time for the Super Express Lane is:" + supExpFreeTime);
		System.out.println("The maximum length of the Super Express Lane is:" + supExpMax);
		System.out.println();
		System.out.println("The total waiting time for the first Express Lane is:" + exp1WaitTime);
		System.out.println("The total free time for the first Express Lane is:" + exp1FreeTime);
		System.out.println("The maximum length of the first Express Lane is:" + exp1Max);
		System.out.println();
		System.out.println("The total waiting time for the second Express Lane is:" + exp2WaitTime);
		System.out.println("The total free time for the second Express Lane is:" + exp2FreeTime);
		System.out.println("The maximum length of the second Express Lane is:" + exp2Max);
		System.out.println();
		System.out.println("The total waiting time for the first standard line is:" + standWaitTime.get(0));
		System.out.println("The total waiting time for the second standard line is:" + standWaitTime.get(1));
		System.out.println("The total waiting time for the third standard line is:" + standWaitTime.get(2));
		System.out.println();
		System.out.println("The total free time for the first standard line is:" + standFreeTime.get(0));
		System.out.println("The total free time for the second standard line is:" + standFreeTime.get(1));
		System.out.println("The total free time for the third standard line is:" + standFreeTime.get(2));
		System.out.println();
		System.out.println("The maximum length of the first STANDARD Lane is:" + sizes.get(0));
		System.out.println("The maximum length of the second STANDARD Lane is:" + sizes.get(1));
		System.out.println("The maximum length of the third STANDARD Lane is:" + sizes.get(2));
		
		int count = 0;
		for(int i =0 ; i<custAmount.size(); i++){
			count = count + custAmount.get(i);
		}
		
		
		int total = supExpCust + exp1Cust + exp2Cust +count;
		System.out.println("The total number of customers who completed checkout:" + total);
	}
	}

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

COuld you explain a little bit more - so how are you supposed to calculate total time and which times are equal.
It compiles and works but the code is too long to read it without any particular  understanding what to look for.

so what should be different:
The total waiting time for the Super Express Lane is:0
The total free time for the Super Express Lane is:1585
The maximum length of the Super Express Lane is:1

The total waiting time for the first Express Lane is:0
The total free time for the first Express Lane is:580
The maximum length of the first Express Lane is:1

The total waiting time for the second Express Lane is:0
The total free time for the second Express Lane is:3600
The maximum length of the second Express Lane is:0

The total waiting time for the first standard line is:0
The total waiting time for the second standard line is:0
The total waiting time for the third standard line is:0

The total free time for the first standard line is:65
The total free time for the second standard line is:65
The total free time for the third standard line is:65

The maximum length of the first STANDARD Lane is:1
The maximum length of the second STANDARD Lane is:1
The maximum length of the third STANDARD Lane is:1
The total number of customers who completed checkout:170

Open in new window

Avatar of amhinkel

ASKER

COuld you explain a little bit more - so how are you supposed to calculate total time and which times are equal.
It compiles and works but the code is too long to read it without any particular  understanding what to look for.


I'm supposed to calculate the time each person waited in line.  This time is equal to the time they entered (which is the second parameter in the customer constructor) until the time they started to be processed. I  take the time they entered and subtract that from the current time (which is when they are exiting).  This would give the whole time the person was in the line.  However, I must then subtract the amount of time it took to get processed.  I forgot to mention that it takes 5 seconds for each item a person has to be processed.  So, I subtract 5 times the persons starting items.

The .exit() method computes the waiting time as I have explained.  

I am thinking that it would be impossible for the wait times for the standard lines to be the same.


So are these same values that cause your concern:

The total free time for the first standard line is:80
The total free time for the second standard line is:80
The total free time for the third standard line is:80
Actually, those would be the other values that cause my concern.  These values for free time would be related to the values for wait time.  The free time is time that a line had no customers to process.

I am concerned about:

The total waiting time for the first standard line is:0
The total waiting time for the second standard line is:0
The total waiting time for the third standard line is:0

I don't see how the waiting time for one of these lines could be 0.  Given the time it would take to process a person in these lines,  it would be enough time for another person to enter it before the first person was done.  This would give us a waiting time.
can you liimit it for the begining with just one standrad line and then understand
why it comes up as zero; it is kind of difficult  to follow with so many things together

Just make another small class wityh only one line and copy everything only corresponding to this one line.
Then it would be easier to track the error - well at lest so it seems to me.
To have one standard line, all you need to do is change the variable numStandLines.  However, you will need to remove some print statements at the end for it to compile since those were just me testing.  
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account