Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete Question Text: Java programming task: Simulate a checkout area of a sup

ID: 3758431 • Letter: C

Question

Complete Question Text:

Java programming task:

Simulate a checkout area of a supermarket consisting of one super-express counter, two express counters, and numStandLines standard counters. All customers with numSuper or fewer items proceed to a super-express counter with the fewest customers, unless there is a free express or regular line, and those with between numSuper and numExp proceed to the express counter with the shortest line unless there is a free standard line. Customers with more than numExp go to the standard counter with the shortest standard line.
The number of items bought will be a random number in the range 1 to maxItems.
The time to process a customer is 5 seconds per item.
Calculate the following statistics:

- Average waiting time for each of the lines
- Overall average waiting time
- Maximum length of each line
- Number of customers per hour for each line and overall
- Number of items processed per hour for each line and overall
- Average free time of each counter
- Overall free time

NOTE: The average waiting time for a line is the total of the customer waiting times divided by the number of customers. A customer's waiting time is the time from when he (or she) enters the queue for a given checkout line until the checkout processing begins. If the customer can find a free line, then the wait time is zero.

Your program should read the following data:

numSuper The number of items allowed in the super-express line.
numExp The number of items allowed in the express line.
numStandLines The number of regular lines.
arrivalRate The arrival rate of customers per hour.
maxItems The maximum number of items.
maxSimTime The simulation time.

It may be that some lines do not get any business. In that case you must be sure, in calculating the average, not to divide by zero.

if a super-express or express line has an empty queue for 30 seconds, that line's cashier takes the next customer from the longest regular line.

Add 10 seconds for moving between lines to that customer's service time. This action does not change the random processes described.

Explanation / Answer

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);
   }
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote