Going back to the grocery line queue. At the end of the program, it will output how many total customers I serviced, and it is supposed to output my maximum line length during the simulation.

I think its obvious that I will use an "if" statement, but how do you determine your max queue length?

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


public class GroceryStoreCopy {

    public static void main(String[] args) {

        int newCust;
        int serviceTime = 0;
        int totalCust = 0;
        int maxQ = 0;

         Queue<Integer>custQ = new LinkedList();


         for(int i = 60; i >= 0; i--){//60  minutes of activity             


         Random r = new Random();
         newCust = r.nextInt(4) + 1;//25% random number will be 1, if random number equals 1, add cust to q

          Random time = new Random();
          serviceTime = time.nextInt(5) + 1;
          serviceTime--;

         if(newCust == 1){
              custQ.add(1);//if customer has been added to line, add cust to q  
              System.out.println("\nNew customer has been added!, New queue length is now " + custQ.size());
              totalCust += newCust;


             if(serviceTime == 0){             

             custQ.remove();
                  System.out.println("\nCustomer serviced and removed from queue.  New queue length is  now " + custQ.size());
        }    
      }
    }  
          for(int i = 1; i<= 60; i++){
          System.out.print("-");//shows the elapsed time 
         }
        System.out.println("\nTotal number of customers serviced: " + totalCust);
         System.out.println("Maximum line length during the simulation: " + maxQ); 
    }   
  }

Any suggestions on creating a Customer class with serviceTime? and just call it from the loop

Hi,

It's actually just an extension to your new queue length print. If the current line length is bigger than the current max it becomes the new max. Once all the customers are done you print it where you already are printing it; outside the loop.

for your second question.

A simple customer class that saves the current time would be:

public class Customer{  
    private int time;

    public Customer(){  //your constructor to create object instances
        time = System.currentTimeMillis();  //save the current system time  if you want a date and hour look up the date class.
    }

    public int getTime(){  //returns the creation time of the object.
        return time;
    }
}

then in your program all you have to do is create a new instance every time you get a new customer.

Customer cust = new Customer();
custQ.add(cust);

you will also have to change the quene type to Customer for this to work.

So, it would be in the output?
custQ.size() is max?

Thank you, the information you gave me was really helpful.

One more question....
How do I call the return time; in the main class?

If you mean the time the customer was created then load your customer out of the queue into a variable then call cust.getTime() to get the creation time. Like this:

Customer cust = custQ.peek();
cust.getTime();

If you mean how to get the time when the main method ends or returns. simply place System.currentTimeMillis(); at the end of the main method. You can even return the value to the caller if you like.

return System.currentTimeMillis();

Im sorry I had to guess at what you ment if neither of those is what you ment describe what you want in more detail and ill try to answer your question.

In response to your earlier post.

Are you assigning each customer a number and you want to find the max of those numbers or do you want to find the number of customers in the queue?

The first is realy easy simply add a variable to your customer class and make it static every time you create a new customer compair the value stored in the variable to the customer' number if the customer's number is greator set the value to the customer's number.
So your customer class would look like:

public class Customer{ 
    private static int max = 0;  //im assuming you only assign positive integers to your customers
    //making the variable static means there will only be one of it no matter how many instances of this class you create
    private int time;  //where as there will be one of this variable for every instance of customer
    public Customer(int number){  //your constructor to create object instances
        if(number > max)
            max = number;// set max to largest number

        time = System.currentTimeMillis();  //save the current system time  if you want a date and hour look up the date class.
    }
    public int getTime(){  //returns the creation time of the object.
        return time;
    }
}

the second is simply custQ.size()

Here is my Customer class, but when I call cust.getServiceTime(); in the main class it messes up my output. I dont want it to output the time, just remove the customer from the queue when the serviceTime reaches 0.

public class Customer{ 

    private int time;

    public Customer()
    { 
      Random time = new Random();
      serviceTime = time.nextInt(5) + 1; // generates a random serviceTime amt 1-5 min.        
    }

    public int getTime()
    {  
       return serviceTime--;    
}
}





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


public class GroceryStoreCopy {

    public static void main(String[] args) {

        int newCust;
        int serviceTime = 0;
        int totalCust = 0;
        int maxQ = 0;

         Queue<Integer>custQ = new LinkedList();


         for(int i = 60; i >= 0; i--){//60  minutes of activity             

         Customer cust = new Customer();
        Random r = new Random();
         newCust = r.nextInt(4) + 1;//25% random number will be 1, if random number equals 1, add cust to q

          cust.getServiceTime();

         if(newCust == 1){
              custQ.add(cust);//if customer has been added to line, add cust to q  
              System.out.println("\nNew customer has been added!, New queue length is now " + custQ.size());
              totalCust += newCust;


             if(serviceTime == 0){             

             custQ.remove();
                  System.out.println("\nCustomer serviced and removed from queue.  New queue length is  now " + custQ.size());

      }
    }  
          for(int i = 1; i<= 60; i++){
          System.out.print("-");//shows the elapsed time 
         }
        System.out.println("\nTotal number of customers serviced: " + totalCust);
         System.out.println("Maximum line length during the simulation: " + maxQ); 
    }   
  }

The output looks like new customer added 1 in line
cust remvoed 0 in line
it outputs this several times

well youve got a discrepency between your customer class and your main method where you call it.

the method in your customer class is getTime()

the method you call on line 44 is getServiceTime()

also you never update the value of service time from 0 to something else so line 52 is always true.

typo, I did have it changed in my code to getServiceTime(), I just did not fix this on the part I posted.
But I still get the same thing with this code

import java.util.Random;

public class Customer {
     private int serviceTime;

    public Customer(){  

    }
    public int getServiceTime(){  
        Random time = new Random();
         serviceTime = time.nextInt(5) + 1;
        return serviceTime;
    }
}

Main class

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

public class GroceryStoreQueue {

    public static void main(String[] args) {
      int newCust;
        int serviceTime = 0;
        int totalCust = 0;
        int maxQ = 0;


         Queue<Customer>custQ = new LinkedList<Customer>();


         for(int i = 60; i >= 0; i--){//60  minutes of activity  

         Random r = new Random();
         newCust = r.nextInt(4) + 1;//25% random number will be 1, if random number equals 1, add cust to q

        Customer cust = new Customer(); 
        cust.getServiceTime();
        serviceTime--;
        //  Random time = new Random();
         // serviceTime = time.nextInt(5) + 1;
         // serviceTime--;


         if(newCust == 1){             
              custQ.add(cust);//if customer has been added to line, add cust to q  
              custQ.size();

              if(maxQ < custQ.size()){
                  maxQ = custQ.size();
              }
              System.out.println("\nNew customer has been added!, New queue length is now " + custQ.size());
              totalCust += newCust;


             if(serviceTime <= 0){             

             custQ.remove(cust);
                  System.out.println("\nCustomer serviced and removed from queue.  New queue length is  now " + custQ.size());
        }    
      }
    }  
          for(int i = 1; i<= 60; i++){
          System.out.print("-");//shows the elapsed time 
         }
        System.out.println("\nTotal number of customers serviced: " + totalCust);
         System.out.println("Maximum line length during the simulation: " + maxQ); 
    }   
  }

Again i say line 41 is always true because service time is always less than or equal to zero. The only place that service time is updated is line 24 and it is decreased.

Also your only updateing the time till service of the customer on the top of the queue. For the implementation you are using a linked list would be better than a queue. That is if im understanding what you are trying to do.

I changed the code on line 41, I only want to update the time for the customer at the top of the queue, the one being serviced, as their time is decremented to 0, it will remove them from the queue and move on the the next customer who will then have a serviceTime

if(serviceTime == 0){             
             custQ.remove(cust);
                  System.out.println("\nCustomer serviced and removed from queue.  New queue length is  now " + custQ.size());
        }    

I agree the problem lies with the getServiceTime field, because if I dont call this from the Customer class and I simply have in the main class the

Random time = new Random();
serviceTime = time.nextInt(5) + 1;
serviceTime--;

The code works just fine

Setting serviceTime to a new random number every time through the loop does not work just fine. It means that service might take any amount of time, even much more than 5 times through the loop. And to prove that you don't understand what you are doing, you are adding 1 to your random number then immediately subtracting 1 again.

if statement in your previous post place the service time update and the loading of the next customer.

if(serviceTime == 0){             
    custQ.remove(cust);
    System.out.println("\nCustomer serviced and removed from queue.  New queue length is  now " + custQ.size());

    cust = custq.peek();
    custq.pop();
    serviceTime = cust.getServiceTime()
} 

also i would move the service time generation into the constructor in you customer class and just have the getServiceTime() return the already computed value.

import java.util.Random;


public class Customer {

    private int serviceTime;

    public Customer(){    
        Random time = new Random();
        serviceTime = time.nextInt(5) + 1;
    }

    public int getServiceTime(){
        return serviceTime;
    }
}

also you only want to create a new customer when the random number = 1 right so place it inside of a if statement.

serviceTime--;

Random r = new Random();

if(r.nextInt(4) + 1 == 1){
    Customer cust = new Customer(); //if the random = 1 then create a new cust add to q and get q size
    custQ.add(cust);//if customer has been added to line, add cust to q  
    custQ.size();

    if(maxQ < custQ.size()){
        maxQ = custQ.size();
    }

    System.out.println("\nNew customer has been added!, New queue length is now " + custQ.size());
    totalCust += newCust;

    if(serviceTime <= 0){             
        custQ.remove(cust);
        System.out.println("\nCustomer serviced and removed from queue.  New queue length is  now " + custQ.size());
    }
}

hope this helps.

Thank you so much for the explanation it was very helpful. I am definitely no novice at Java, but I'm trying to get there

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.