Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

supermarket simulation

807601Dec 19 2007 — edited Dec 20 2007
Alright, so i need to create a supermarket simulation using a queue, nothing fancy like GUI or multiple counters required, just demonstrate queue. I have created a queue by using a reference based implementation along with a class called Node. Now i don't understand how i can do this:
a) Choose a random integer between 1 and 4 to determine the minute at which the first customer arrives.
b)At the first customer?s arrival time, do the following:
Determine customer?s service time (random integer from 1 to 4).
Begin servicing the customer.
Schedule arrival time of next customer (random integer 1 to 4 added to the current time).
c) For each minute of the day, consider the following:
If the next customer arrives, proceed as follows:
Say so.
Enqueue the customer.
Schedule the arrival time of the next customer.
If service was completed for the last customer, do the following:
Say so.
Dequeue next customer to be serviced.
Determine customer?s service completion time (random integer from 1 to 4
added to the current time).

I think something like a for loop should be used, but i fail to see how to run the departure and arrival side-by-side. I am going to try something out, in the main time, please help me with some ideas.

// the queue to represent the line of customers at supermarket
import supermarket.*;
public class marketLineQueue
{
	private Node lastNode;

	public marketLineQueue()  
	{
		lastNode = null;
	}

	public boolean isEmpty()
	{
		return lastNode == null;
	}

	public void dequeueAll()
	{
		lastNode = null;
	}

	public void enqueue( Object newItem )
	{
		Node newNode = new Node(newItem);
		if ( isEmpty() )
		{
			newNode.setNext( newNode );
		}
		else 
		{
			newNode.setNext( lastNode.getNext() );
			lastNode.setNext( newNode );
		}

		lastNode = newNode;
	}

	public Object dequeue() 
	{
		if ( !isEmpty() )
		{
			Node firstNode = lastNode.getNext();
			if ( firstNode == lastNode )
			{
				lastNode = null;
			}
			else
			{
				lastNode.setNext( firstNode.getNext() );
			}
			return firstNode.getItem();
			}
			else
			{
				return ( "Queue is empty." );
			}

	}

}
That's my queue, which uses this class:
package supermarket;
public class Node 
{
	private Object item;
	private Node next;

	public Node( Object newItem )
	{
		item = newItem;
		next = null;
	}

	public Node( Object newItem, Node nextNode )
	{
		item = newItem;
		next = nextNode;
	}

	public void setItem( Object newItem )
	{
		item = newItem;
	}

	public Object getItem()
	{
		return item;
	}

	public void setNext( Node nextNode )
	{
		next = nextNode;
	}

	public Node getNext()
	{
		return next;
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 17 2008
Added on Dec 19 2007
8 comments
394 views