3 Replies - 7459 Views - Last Post: 03 April 2012 - 03:54 AM Rate Topic: -----

#1 seans0007  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 48
  • Joined: 03-October 11

bank simulation using queues

Posted 02 April 2012 - 07:15 PM

I know this is a very common project and I have looked at many tutorials and examples of this same project. I have gotten what i thought was a good running program but I am getting an unsafe operation error or warning in my teller class when I enqueue a new customer. Here is my exact assignment because my assignment differs a little with what I have seen in other examples.


Write two classes and a driver program in addition to the two classes provided to simulate a line at a bank.

The Customerclass

has a one-int-parameter constructor that takes the current clock time as an integer parameter and stores that parameter.
has a public int getWait(int current) method that will return the difference between current and the clock time that was stored from the constructor.
the Tellerclass

contains a queue of Customer objects
has a constructor with 3 int parameters
the number of seconds to run the simulation
the maximum time before a new customer comes, in seconds
the maximum time to handle one customer's transactions, in seconds
has a public void run()method to run the simulation
Generate a random number less than the second constructor parameter for when the next customer arrives.
Loop while a clock variable is less than the number of seconds to run the simulation.
If the time has elapsed for a new customer to arrive, enqueue a new customer and generate a random number for the next clock time when a customer will arrive.
If the teller is not busy, dequeue a customer, accumulate the number of customers served and the total wait time, and generate a random number to calculate the next clock time when the teller will be free.
increment the clock time
has a public void report()method to output, clearly labeled,
the Total time in minutes.
the Average wait time in minutes.
the Number served
If the queue is not empty
the number of customers still in line
the wait time of the customer in front of the line, in minutes.


my customer class
 
public class Customer
{
	int clock, currentTime;
	public Customer()
	{}
	public Customer(int theTime)
	{
		clock=theTime;
	}

	public int getCurrent(int current)
	{
		 currentTime=clock-current;
		 return currentTime;
	}
}


my teller class
import java.util.Random;
import java.util.Scanner;
public class Teller
{

	Customer c=new Customer();

 	int  runTime, customerArrival, transactionTime;
 	Queue q=new Queue();
    int customers=0;



  public Teller(int run, int maxNewCustomer, int maxHandle)
	 {
		runTime= run;
    	customerArrival= maxNewCustomer;
    	transactionTime = maxHandle;
	 }//end constructor

	 public int runTime(){
 	   return runTime;
 	 }//end runTime

 	 public int getTransactionTime(){
 	   return transactionTime;
 	 }//endTransactionTime

 	 public int getCustomerArrival(){
 	   return customerArrival;
 	 }//end getCustomerArrival

     public void Run()
	  {

		  Random rand= new Random();

		  int newArrival = rand.nextInt(customerArrival);
		  customerArrival=newArrival;
		  q.empty();
		  int time;
		  Customer next=new Customer();
		  for(time=0;time<runTime; time++)
		  {
			if(q.empty()==false)
			{
			if(c.currentTime>=customerArrival)
			  {
				q.enqueue(next);
				customers++;

			  }else if(customerArrival>transactionTime)
			  {

				  q.dequeue();
				  customers++;
				  int nextArrival = rand.nextInt(customerArrival);
		  	      customerArrival=newArrival;
		  	      nextArrival=c.clock+nextArrival;

			  }//end if
		  	}//end if
		  }//end for
	  }//end Run
	  public void report()
	  {
		  int totalTime=c.clock;
		  System.out.println(totalTime);
		  int Average=totalTime/customers;
		  		  System.out.println(Average);

		  int numberServed=customers;
		  		  System.out.println(numberServed);

		  if(q.empty()==true)
		  {
			  System.out.println(q.toString());
			  System.out.println(c.clock);
		  }//end if
	  }//end report

	   public static void main(String[]args)
	  	  {



	  		  Scanner key=new Scanner(System.in);
	  		  System.out.println("how many seconds to run: ");
	  		  int newRunTime=key.nextInt();
	  		  System.out.println("max time between customers: ");
	  		  int newCustomerArrival=key.nextInt();
	  		  System.out.println("max processing time: ");
	  		  int newTransTime=key.nextInt();
	  		  Teller t= new Teller(newRunTime,newCustomerArrival,newTransTime);
	  		  t.Run();
	  		  t.report();

	  	  }//end main
}




the queue class that was given
public class Queue<T> {
	private Node<T> first;
	private Node<T> last;
	T newOne;
	private int count;
	public Queue () {
	   last = new Node<T>();
	   first = new Node<T>(null,last);
	   count=0;
	}

	public String toString() {
		String ret = "";
		Node<T> r = first.getNext();
	    for (; r!=last; r=r.getNext())
		    ret += r.getData() + " ";
		return "first " + ret + "last";
	}

	public T dequeue() {
		T ret;
		if (count==0) return null;
		ret = first.getNext().getData();
		first = first.getNext();
		count--;
		return ret;
	}

	public void enqueue(T newData) {
		last.setData(newData);
		last.setNext(new Node<T>());
		last = last.getNext();
		count++;
	}
	public boolean empty() { return count==0; }

	public int length() { return count; }

}


and node class that was given
public class Node<T> {
	private T data;
	private Node<T> next;

	public Node(T d, Node<T> n) { data=d; next=n; }
	public Node(T d) { data = d; next = null; }
	public Node() { data = null; next = null; }

	public T getData() {return data;}
	public Node<T> getNext() {return next;}

	public void setData(T d) { data = d; }
	public void setNext(Node<T> n) { next = n; }

	public String toString() { return "" + data;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: bank simulation using queues

#2 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: bank simulation using queues

Posted 02 April 2012 - 08:24 PM

Queue q=new Queue();

your Queue is a generic class so its generic type should be defined

So it is a queue of your Node class, but your Node class is also generic
I think that is Customer that you want to put in your Node so should be

Queue<Node<Customer>> q = new Queue<Node<Customer>>();
Was This Post Helpful? 0
  • +
  • -

#3 seans0007  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 48
  • Joined: 03-October 11

Re: bank simulation using queues

Posted 02 April 2012 - 10:09 PM

Thanks! its working now but have one problem...When I enqueue another customer each time the transaction time is always 0 so there is never a wait time for the customers.Therefore, I cant calculate the average wait time or total wait time...?
import java.util.Random;
import java.util.Scanner;
public class Teller
{



 	int  runTime, customerArrival, transactionTime;
Queue<Node<Customer>> q = new Queue<Node<Customer>>();
int customers=0;
int totalTime=0;



  public Teller(int run, int maxNewCustomer, int maxHandle)
	 {
		runTime= run;
    	customerArrival= maxNewCustomer;
    	transactionTime = maxHandle;
	 }//end constructor

	 public int runTime(){
 	   return runTime;
 	 }//end runTime

 	 public int getTransactionTime(){
 	   return transactionTime;
 	 }//endTransactionTime

 	 public int getCustomerArrival(){
 	   return customerArrival;
 	 }//end getCustomerArrival
int randTrans=(int)(Math.random() * transactionTime);
Customer c=new Customer(randTrans);


    public void Run()
	  {

		  Random rand= new Random();

		  int newArrival = (int)(Math.random() * customerArrival);


		  q.empty();
		  int time;
		   Node<Customer> next=new Node<Customer>();
		  for(time=0;time<runTime; time++)
		  {

			if(time<newArrival)
			  {
				  System.out.println(newArrival);


				q.enqueue(next);
				customers++;
			
				int newOne=(int)(Math.random()* customerArrival);
				System.out.println(newOne);
				int nextOne=c.currentTime+newOne;

				System.out.println(nextOne);


				if(nextOne>=time)
				{
				     q.dequeue();
				     customers++;
				     System.out.println(newArrival);
				     newOne=(int)(Math.random()* customerArrival);
				     nextOne=time+newOne;





			  }//end if
		  	}//end if
		  }//end for
	  }//end Run
	  public void report()
	  {
		  int totalTime=c.clock;
		  System.out.println("total time: "+totalTime);
		  int Average=totalTime/customers;
		  		  System.out.println("average wait time: "+Average);

		  int numberServed=customers;
		  		  System.out.println("number served: "+numberServed);

		  if(q.empty()==true)
		  {
			  System.out.println(q.toString());
			  System.out.println(c.clock);
		  }//end if
	  }//end report

	   public static void main(String[]args)
	  	  {



	  		  Scanner key=new Scanner(System.in);
	  		  System.out.println("how many seconds to run: ");
	  		  int newRunTime=key.nextInt();
	  		  System.out.println("max time between customers: ");
	  		  int newCustomerArrival=key.nextInt();
	  		  System.out.println("max processing time: ");
	  		  int newTransTime=key.nextInt();
	  		  Teller t= new Teller(newRunTime,newCustomerArrival,newTransTime);
	  		  t.Run();
	  		  t.report();

	  	  }//end main
}




Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: bank simulation using queues

Posted 03 April 2012 - 03:54 AM

Add a timestamp in your Node class

private long timeStamp;
timeStamp = System.nanoTime();
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1