I am trying to figure out the best way to approach this.....
I am creating a grocery line simulation using a queue, and I know that I will want to create a Customer class that has the information for serviceTime.

I am weak on creating classes, what I want to happen is everytime the program iterates, it will check to see if a new customer has been added to the queue, if so it will output ("New customer added! Queue length is now...: ");

I believe I am good on that part of the program, but how do I implement the time decrementer?

Each time a new customer is at the beginning of the queue, a random number from 1 - 5 is generated and this will be their serviceTime, at the end of their serviceTime they will be removed from the queue wiith the output("customer serviced and removed from queue, queue length is now...");

at the end, I want to calculate the number of customers serviced, and the max length of the queue,

This is what I have so far...

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;

         Queue<Integer>custQ = new LinkedList();

         for(int i = 1; i <= 60; i++){

         System.out.print("-");//shows the elapsed time

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

         Random r = new Random();
         newCust = r.nextInt(4) + 1;//25% chance new customer will be added to q every minute

         if(newCust == 1){
             custQ.add(1);//if customer has been added to line, add cust to q            
           System.out.println("\nCustomer " + custQ.peek() + " your service time is: " + serviceTime + " minutes.  New queue size is: " + custQ.size());


I know this isnt much, but i reaallyy need help

4 Years
Discussion Span
Last Post by bguild

You already have an if statement for adding new customers to the queue, so why not print your message there? You should probably also increment a counter in there so you don't have to give every customer the number 1.

You probably also want an if statement for removing customers from the queue when serviceTime <= 0. You can print your removing message, do the removal, and pick a new random serviceTime in that if statement.

Then all you need is a serviceTime-- somewhere in the loop and you're done.

Less critically, you should be aware that creating a new Random every time you want a random number defeats the sophisticated algorithm in Random designed to give you good random numbers. It is a bit like having a barrel of apples and for each apple you take just one bite, then throw that apple away and take another one. The people who made Random put good effort into giving you as many random numbers as you could want for most purposes, and it is an insult to them when you just take the first one and throw the rest away.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.