Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Having trouble understanding how to simulate a coffee shop service...

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having trouble understanding how to simulate a coffee shop service...

    The problem I'm having is setting up the classes with their methods to be really solid for the main simulation. This is for a capstone project which wants you to simulate a coffee shop service. There are 5 classes minimum we need(Customer, Server, Waiting Queue(place customers waiting), server list, and the main simulation).

    We also need to record statistics for various customers and servers. This is what I have so far.

    import java.util.Scanner;
     
    public class SimulationDriver{
     
    	public static void main(String[] args){
     
    		Scanner sc = new Scanner(System.in); 
     
    			//gathering all the information from the user
     
    		System.out.print("Please enter simulation time: "); 
    		int simTime = sc.nextInt();
    		System.out.print("Please enter number of servers: ");
    		int numOfServers = sc.nextInt();
    		System.out.print("Please enter transaction time: ");
    		int transTime = sc.nextInt();
    		System.out.print("Please enter arrival probability: ");
    		double arrivalProb = sc.nextDouble();
     
    		//setting up customer waiting queue
    	WaitingQueue queue = new WaitingQueue();
     
    	//setting up number of servers in the server list
    	ServerList servers = new ServerList(numOfServers);
     
    	for(int i = 1; i<= simTime; i++){
    		System.out.println("Clock Unit " + i);
     
    		if(Customer.customerArrived(arrivalProb) == true){ //if a customer arrives, place them in a queue
    			Customer newCust = new Customer();
    			queue.getsInLine(newCust);
    		}
    	}
    }
    }

     
    public class Customer {
     
    	protected int customerNumber;
    	protected int arrivalTime; //time customer showed up
    	protected int waitingTime; //in line waiting to be served
    	protected int transactionTime; //at the counter while being served
    	protected static int nextNumber = 0;
     
     
     
       	public static int nextNumber(){
    		nextNumber++;
    		return nextNumber;
    	}
     
    	public Customer(){ //default constructor
    		this.customerNumber = nextNumber();
    		this.arrivalTime = 0;
    		this.waitingTime = 0;
    		this.transactionTime = 0;
     
    	}
     
     
        public static boolean customerArrived(double arrivalProbability)
        	 //checks to see if a customer has arrived during the passing of one clock unit
    			{
            		return (Math.random() < arrivalProbability);
    			}
     
     
    }

    public class Server{
     
    	protected Customer currentCustomer;
    	protected boolean status; //free or busy serving the customer
    	protected int transactionTime; //remaining to complete the service
     
    	public Server(int transactionTime){
    		status = false;  //false(does not have a customer) if server is free; true if busy
    		this.transactionTime = transactionTime;
    	}
     
     
    	public boolean status(){
    		return this.status;
    	}
     
    	public void setCurrentCustomer(Customer obj){
     
    		currentCustomer = obj;
     
    	}
    }

    public class ServerList{
     
    	public LinkedList Servers;
     
     
    	public boolean checkForFreeServer(){
     
    		while(Servers != null){
     
     
    		}
     
    	}
    }

     
    public class WaitingQueue{
     
    	protected LinkedQueue custServed;
     
     
    	public WaitingQueue(){
    		custServed = new LinkedQueue(); //intializing waiting queue
    	}
     
    	public void getsInLine(Customer obj){  //when a customer enters, they get put in the waiting queue
    		custServed.enqueue(obj);
    	}
     
    	public Customer goesToCounter(){ //when a server is no longer busy, first customer is dequeued
    		Customer temp;
    		temp = custServed.dequeue();
    		return temp;
    	}
     
     
    }

    We had to use a queue data structure that I've written from previous weeks, and another choice of data structure which I chose unordered linked lists. Our teacher gave us a power point to which he describes the behaviors but I'm still having trouble understanding what methods should be in them.

    CUSTOMER CLASS
     
    Attributes
    customerNumber
    arrivalTime
    waitingTime (in line to be served)
    transactionTime (at the counter while being served)
     
    Behaviors
    waiting for your coffee and paying.
    you arrive, wait, place order, pay, and depart.
    significant parameter:  passing of time.

    Server class
    Attributes
    currentCustomer
    status ( free or busy serving the customer )
    transactionTime (remaining to complete the service)
     
    Behaviors
    passing of time while dispensing the service

    SIM LOOP 
     
    	foreach( clock time unit )   end of previous period, begin next period
    		update servers (passing of time, service completed, ...)
    		update customer wait queue(passing of time, ...)
     
    		if( customer arrived )
    		              create customer record
    			            ( cust#, clock, waitTime=0, transactTime )
    			add customer to wait queue
     
    		if( free server )
    			remove customer from wait queue
    			add customer to server
     
    		generate statistics report

    I needed help with the customer and server classes but I'm not sure what methods I should write. Any advice would be great how to go about this as I'm stuck.


  2. #2
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble understanding how to simulate a coffee shop service...

    I managed to update my customer class...but I dont understand how I can increment the waitingTime to every customer that's in the queue, rather than doing it in the customer class? Do I need to write a method in the Queue class so that I can increment it in the waitingQueue class?

    public class Customer {
     
    	protected int customerNumber;
    	protected int arrivalTime; //time customer showed up
    	protected int waitingTime; //in line waiting to be served
    	protected int transactionTime; //at the counter while being served
    	protected static int nextNumber = 0;
     
     
     
       	public static int nextNumber(){
    		nextNumber++;
    		return nextNumber;
    	}
     
    	public Customer(){ //default constructor
    		this.customerNumber = nextNumber();
    		this.arrivalTime = 0;
    		this.waitingTime = 0;
    		this.transactionTime = 0;
     
    	}
     
     
        public static boolean arrived(double arrivalProbability)
        	 //checks to see if a customer has arrived during the passing of one clock unit
    			{
            		return (Math.random() < arrivalProbability);
    			}
     
        public void customerWaiting(){
        	waitingTime++;
        }
     
        public void customerTransaction(int transactionTime){
        	this.transactionTime = transactionTime;
        }
     
        public String placeOrder(){
        	return "Customer " + customerNumber + " has placed their order.";
        }
        public String pay(){
        	return "Customer " + customerNumber + " has paid for their order.";
        }
     
        public String depart(){
        	return "Customer " + customerNumber + " has left.";
        }
     
        public String timeArrival(int time){
     
        	this.arrivalTime = time;
        	return "Customer " + customerNumber + " arrived at time unit " + arrivalTime;
     
        }
    }

    public class WaitingQueue{
     
    	protected Queue waitingLine;
     
     
    	public WaitingQueue(){
    		waitingLine = new Queue(); //intializing waiting queue
    	}
     
    	public void getsInLine(Customer cust){  //when a customer enters, they get put in the waiting line
    		waitingLine.enqueue(cust);
    	}
     
    	public Customer goesToCounter(){ //when a server is no longer busy, first customer is sent to server
     
    		return waitingLine.dequeue();
     
    	}
     
    	public void passingTime(){ //while all the customers in line are waiting, increment their waitingTime
     
    	}
     
     
    }

    public class Queue{
     
    	protected class Node{
     
     
    		protected Customer data;
    		protected Node link;
    	}
     
    	protected int count;
    	protected Node first;
    	protected Node last;
     
    	public Queue(){
    		count = 0;
    		first = null;
    		last = null;
    	}
     
    	public void enqueue(Customer newCust){
     
    		Node oldLast = last;
    		last = new Node();
    		last.data = newCust;
    		last.link = null;
     
    		if(count == 0 ){
    			first = last;
     
    		}
     
    		else{
    			oldLast.link = last;
    		}
     
    		count++;
     
     
    	}
     
    	public Customer returnData(){
    		Node temp = new Node();
     
    	}
     
     
     
     
    	public Customer dequeue(){
    		Customer temp = first.data;
    		first = first.link;
     
    		count--;
     
    		if(count == 0){
    			last = null;
    		}
     
    		return temp;
    	}
     
     
    }

    import java.util.Scanner;
     
    public class SimulationDriver{
     
    	public static void main(String[] args){
     
    		Scanner sc = new Scanner(System.in); 
     
    			//gathering all the information from the user
     
    		System.out.print("Please enter simulation time: "); 
    		int simTime = sc.nextInt();
    		System.out.print("Please enter number of servers: ");
    		int numOfServers = sc.nextInt();
    		System.out.print("Please enter transaction time: ");
    		int transTime = sc.nextInt();
    		System.out.print("Please enter arrival probability: ");
    		double arrivalProb = sc.nextDouble();
     
    		//setting up customer waiting queue
    	WaitingQueue waitingLine = new WaitingQueue();
     
    	//setting up number of servers in the server list
    	ServerList servers = new ServerList();
     
    	for(int i = 1; i<= simTime; i++){
    		System.out.println("Clock Unit " + i);
    	if(Customer.arrived(arrivalProb) == true)){
    		waitingLine.getsInLine(new Customer());
     
    	}
     
    	if(){
     
    	}
     
     
    }
     
    System.out.println("Simulation ran for " + simTime + " time units.");
    System.out.println("Number of servers: + " numOfServers);
    System.out.println("Arrival probability for Customer: " + arrivalProb);
    }
    }

Similar Threads

  1. Having trouble understanding recursive methods??
    By orbin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 17th, 2012, 01:08 AM
  2. Having trouble understanding Int Logs.
    By orbin in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 10th, 2012, 12:30 PM
  3. Inheritance trouble understanding how to use it?
    By orbin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 21st, 2012, 11:24 AM
  4. Replies: 3
    Last Post: July 12th, 2012, 07:11 AM
  5. Having trouble understanding what teacher said for build Tree.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 16th, 2010, 08:22 PM