# Waiting Queue Simulation

• (2 Pages)
• 1
• 2

## 23 Replies - 2219 Views - Last Post: 02 April 2013 - 06:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=317233&amp;s=f268fce978dd70b9511bb1ac1ab5a0f5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 SpiderD

Reputation: 0
• Posts: 48
• Joined: 30-March 13

# Waiting Queue Simulation

Posted 30 March 2013 - 04:17 PM

Hi everyone Ive got this homework to complete basically I need to make a waiting queue simulation with Java by using the Queue, LinkedList methods. The points are:

There is only 1 queue.
Consisting of 10 customers arriving at random intervals which u can label them.
Customers time in queue will be measured.
The user should input how many cashiers is.(More cashiers means faster process of customers).
I need to output the different stages of the queue and each customer time in the queue.

So far this is what Im able to get by the way im using Eclipse:
```import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Station
{
public static Queue<String> Customer = new LinkedList<String> ();

public static void main(String[] args)
{

Customer.size();
Customer.peek();

System.out.println("Enter the number of Cashiers here: ");
Scanner userinput = new Scanner(System.in);
String cashier = userinput.nextLine(); System.out.println(Customer.size());
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Waiting Queue Simulation

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Waiting Queue Simulation

Posted 30 March 2013 - 09:39 PM

And the question is ?

Respect Java convention, variable name start with a lower case letter so should be customer not Customer. We think you referenece the class Customer.

What is the use of calling customer.size(); if you do not do anything with the returned result ?
Same question for customer.peek(); ?

### #3 SpiderD

Reputation: 0
• Posts: 48
• Joined: 30-March 13

## Re: Waiting Queue Simulation

Posted 31 March 2013 - 10:45 AM

oki so I changed to lower case letter the peek and size i put in because I may use it but its best I take it out.

I have a random 10 letter generator to but how do i set my generator to send 1 or 2 letters into the empty queue in
in intervals of 20 seconds.

The second question is lets say customer inputs 2 cashiers so cashiers are available to process customers but how do i implement the cashier station based on the user input. Also at cashier station how do I record the time the customer spent waiting in the queue.

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Waiting Queue Simulation

Posted 31 March 2013 - 03:24 PM

SpiderD, on 31 March 2013 - 01:45 PM, said:

oki so I changed to lower case letter the peek and size i put in because I may use it but its best I take it out.

Good

SpiderD, on 31 March 2013 - 01:45 PM, said:

I have a random 10 letter generator to but how do i set my generator to send 1 or 2 letters into the empty queue in intervals of 20 seconds.

So where is it ?

### #5 SpiderD

Reputation: 0
• Posts: 48
• Joined: 30-March 13

## Re: Waiting Queue Simulation

Posted 31 March 2013 - 04:16 PM

Here is what I have done so far

```public class Station
{
public static Queue<String> line = new  LinkedList<String> ();

public static void main(String[] args)
{
String[] customers = {"Naqi", "Monty", "Mohin", "Paal", "Yasmin", "Maighjoo", "Ashish", "Kevin", "Ruhail", "Tony", };
int customer = customers.length;

for (int x = 0; x < customer; x++ )
{
int cus = (int) (Math.random() * customer);
}

//System.out.println("Enter the number of Cashiers here: ");

//Scanner userinput = new Scanner(System.in);
//String cashier = userinput.nextLine();

System.out.println(line);

}

}

```

However what problem I have is i getting duplicates of the same customers how do I make sure it doesn't pick the same customer again ?

Also I haven't implemented the intervals.

How would i add the intervals and send off the customers ?

Right now im just sending of customers 1 by 1 all happening very fast.

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Waiting Queue Simulation

Posted 31 March 2013 - 05:40 PM

For that tyoe of thing use an ArrayList and randomly remove from it

```ArrayList<String> al = new ArrayList<String>(customers.length);
for(String str : customers)
...
Random ran = new Random();
...

for(int i = 0; i < customers.length; ++i) {
int index = ran.nextInt(al.size());

```

### #7 SpiderD

Reputation: 0
• Posts: 48
• Joined: 30-March 13

## Re: Waiting Queue Simulation

Posted 31 March 2013 - 07:55 PM

Oki I didnt use the array list however I manged to pick a random customer and used a while loop to remove the customers 1 by 1.

Im on the last part which is recording the time.
time in queue is from when a customer arrives in the queue (not all customers arrive at the same time) to when there is a cashier available to process him.
The customer's arrival time needs to be recorded to be able to compute time in queue.

The processing time at the cashier is what keeps a customer waiting in the queue.
When a customer arrives at a cashier there needs to be a random time from min to max that the cashier is blocked processing the customer.
```import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;

public class Station
{
public static Queue<String> line = new  LinkedList<String> ();

public static void main(String[] args)
{
String[] customers = {"Naqi", "Monty", "Mohin", "Paal", "Yasmin", "Maighjoo", "Ashish", "Kevin", "Ruhail", "Tony", };
int customer = customers.length;

for (int x = 0; x < customer; x++ )
{
int cus = (int) (Math.random() * customer);
}

boolean empty = line.isEmpty();
while (!empty == true)
{
System.out.println(line  + "\n");
System.out.println("The queue has " + line.size() + " customers left");
System.out.println(line.remove() + " <=== SERVED" + "\n");
System.out.println("The queue has " + line.size() + " customers left");
System.out.println(line.remove() + " <=== SERVED" + "\n");
System.out.println("The queue has " + line.size() + " customers left");
System.out.println(line.remove() + " <=== SERVED" + "\n");
System.out.println("The queue has " + line.size() + " customers left");
System.out.println(line.remove() + " <=== SERVED" + "\n");
System.out.println("The queue has " + line.size() + " customers left");
System.out.println(line.remove() + " <=== SERVED" + "\n");
System.out.println("The queue has " + line.size() + " customers left");
System.out.println(line.remove() + " <=== SERVED" + "\n");
System.out.println("The queue has " + line.size() + " customers left");
System.out.println(line.remove() + " <=== SERVED" + "\n");
System.out.println("The queue has " + line.size() + " customers left");
System.out.println(line.remove() + " <=== SERVED" + "\n");
System.out.println("The queue has " + line.size() + " customers left");
System.out.println(line.remove() + " <=== SERVED" + "\n");
System.out.println("The queue has " + line.size() + " customer left");
System.out.println(line.remove() + " <=== SERVED" + "\n");
break;
}

}

}

```

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Waiting Queue Simulation

Posted 01 April 2013 - 01:01 PM

So you will have to create a class that contains a customer name and a the time

```    class Customer {
String name;
long time;

// constructor
Customer(String name) {
this.name = name;
time = System.currentTimeMillis();
}

long getTime() {
return time;
}

}

```

Now make a Queue<Customer>
when you retreive a name from the arrayList crea a Customer object
Customer c = new Customer(cus);

when you remove it, check this Customer getTime() with the actual System.currentTimeMillis();

### #9 SpiderD

Reputation: 0
• Posts: 48
• Joined: 30-March 13

## Re: Waiting Queue Simulation

Posted 01 April 2013 - 01:24 PM

I dont why its not working for me can you have look at the code please:

```public class Customer
{
String name;
long time;
public static Queue<String> line = new  LinkedList<String> ();
//constructor
Customer(String name)
{
this.name = name;
time = System.currentTimeMillis();
}

long getTime()
{
return time;
}

public static void main(String[] args)
{
String[] customers = {"Naqi", "Monty", "Mohin", "Paal", "Yasmin", "Maighjoo", "Ashish", "Kevin", "Ruhail", "Tony", };
int customer = customers.length;

for (int i = 0; i < customer; i++ )
{
int cus = (int) (Math.random() * customer);
}

Customer c = new Customer(cus);
}

}

```

### #10 SpiderD

Reputation: 0
• Posts: 48
• Joined: 30-March 13

## Re: Waiting Queue Simulation

Posted 01 April 2013 - 01:35 PM

I'm sorry I am a beginner to Java can you explain a bit how the whole thing will work if I make a separate class which will contain the customer name and time.

### #11 GregBrannon

• D.I.C Lover

Reputation: 2245
• Posts: 5,329
• Joined: 10-September 10

## Re: Waiting Queue Simulation

Posted 01 April 2013 - 02:01 PM

This is a pretty advanced topic for someone new to Java. Do you have experience in another language?

### #12 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Waiting Queue Simulation

Posted 01 April 2013 - 03:43 PM

The Customer class that I wrote is a completly new class that should go together with you already have

And no need to repeat the code when you retreive... you retreive while there are customer on the queue

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

public class Station
{
public static Queue<Customer> line = new  LinkedList<Customer> ();

public static void main(String[] args)
{
String[] customers = {"Naqi", "Monty", "Mohin", "Paal", "Yasmin", "Maighjoo", "Ashish", "Kevin", "Ruhail", "Tony", };
int customer = customers.length;

for (int x = 0; x < customer; x++ )
{
int cus = (int) (Math.random() * customer);
}

while (!line.isEmpty())
{
System.out.println(line  + "\n");
System.out.println("The queue has " + line.size() + " customers left");
Customer cus = line.remove();
System.out.println(cus.name + "queued at " + cus.getTime() + " <=== SERVED" + "\n");
// you will have to sleep a random number of seconds here
}

}

}

```

### #13 SpiderD

Reputation: 0
• Posts: 48
• Joined: 30-March 13

## Re: Waiting Queue Simulation

Posted 01 April 2013 - 07:56 PM

GregBrannon, on 01 April 2013 - 02:01 PM, said:

This is a pretty advanced topic for someone new to Java. Do you have experience in another language?

Im not complete beginner its just need more practice and the syntax are new to me.

### #14 SpiderD

Reputation: 0
• Posts: 48
• Joined: 30-March 13

## Re: Waiting Queue Simulation

Posted 01 April 2013 - 08:02 PM

oki i understand pbl we are basically calling the customer name and class from a new class just like how we call methods

where u commented about the random sleep number would i need to make another random number generator vbut this time with min and max values of lets say 1- 2 seconds whats the syntax for the sleep methods ?

### #15 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Waiting Queue Simulation

Posted 02 April 2013 - 03:54 AM

```
Random ran = new Random();
while (!line.isEmpty())
{
System.out.println(line  + "\n");
System.out.println("The queue has " + line.size() + " customers left");
Customer cus = line.remove();
System.out.println(cus.name + "queued at " + cus.getTime() + " <=== SERVED" + "\n");
int wait = ran.nextInt(2) + 1;  // will generate 1 or 2
try {
}
catch(Exception e) {System.out.println("Sleep error: " + e);}

}

```