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.
Code :
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);
}
}
}
}
Code :
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);
}
}
Code :
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;
}
}
Code :
public class ServerList{
public LinkedList Servers;
public boolean checkForFreeServer(){
while(Servers != null){
}
}
}
Code :
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.
Code :
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.
Code :
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
Code :
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.
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?
Code :
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;
}
}
Code :
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
}
}
Code :
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;
}
}
Code :
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);
}
}