Waiting Queue Simulation

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1451 Views - Last Post: 02 April 2013 - 06:39 PM Rate Topic: -----

#1 SpiderD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.add("A"); 
Customer.add("B");
Customer.add("C"); 
Customer.add("D"); 
Customer.add("F"); 
Customer.add("G"); 
Customer.add("H"); 
Customer.add("I"); 
Customer.add("J"); 
Customer.add("K");

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  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,905
  • 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(); ?
Was This Post Helpful? 0
  • +
  • -

#3 SpiderD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: Waiting Queue Simulation

Posted 31 March 2013 - 03:24 PM

View PostSpiderD, 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

View PostSpiderD, 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 ?
Was This Post Helpful? 0
  • +
  • -

#5 SpiderD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
		line.add(customers[cus]);
		}
		
		//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.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,905
  • 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)
  al.add(str);
...
Random ran = new Random();
...

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



Was This Post Helpful? 0
  • +
  • -

#7 SpiderD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
		line.add(customers[cus]);
		}
		
		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;
		}
		
		
	}

}


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,905
  • 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);
line.add©;

when you remove it, check this Customer getTime() with the actual System.currentTimeMillis();
Was This Post Helpful? 0
  • +
  • -

#9 SpiderD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
		line.add(customers[cus]);
		}


		Customer c = new Customer(cus);
		line.add©;
	}

}


Was This Post Helpful? 0
  • +
  • -

#10 SpiderD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • 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?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,905
  • 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);
		line.add(new Customer(customers[cus]));
		}
		
		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
		}
		
		
	}

}



Was This Post Helpful? 0
  • +
  • -

#13 SpiderD  Icon User is offline

  • New D.I.C Head

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

Re: Waiting Queue Simulation

Posted 01 April 2013 - 07:56 PM

View PostGregBrannon, 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.
Was This Post Helpful? 0
  • +
  • -

#14 SpiderD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 ?
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,905
  • 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 {
			     Thread.sleep(wait * 1000);
			}
			catch(Exception e) {System.out.println("Sleep error: " + e);}

		}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2