Back to this grocery line queue program,
I have created in the Customer class a getServiceTime() which creates a random number between 1 and 5, in the main class the service time is decremented until 0, once it is 0, the customer is removed from the queue, but the problem is I am getting this at runtime.
New customer has been added!, New queue length is now 1
Customer serviced and removed from queue. New queue length is now 0
New customer has been added!, New queue length is now 1
Customer serviced and removed from queue. New queue length is now 0
New customer has been added!, New queue length is now 1
Customer serviced and removed from queue. New queue length is now 0
New customer has been added!, New queue length is now 1
Customer serviced and removed from queue. New queue length is now 0
New customer has been added!, New queue length is now 1
Customer serviced and removed from queue. New queue length is now 0
New customer has been added!, New queue length is now 1
code pasted below:
main class
I have created in the Customer class a getServiceTime() which creates a random number between 1 and 5, in the main class the service time is decremented until 0, once it is 0, the customer is removed from the queue, but the problem is I am getting this at runtime.
New customer has been added!, New queue length is now 1
Customer serviced and removed from queue. New queue length is now 0
New customer has been added!, New queue length is now 1
Customer serviced and removed from queue. New queue length is now 0
New customer has been added!, New queue length is now 1
Customer serviced and removed from queue. New queue length is now 0
New customer has been added!, New queue length is now 1
Customer serviced and removed from queue. New queue length is now 0
New customer has been added!, New queue length is now 1
Customer serviced and removed from queue. New queue length is now 0
New customer has been added!, New queue length is now 1
code pasted below:
import java.util.Random;
public class Customer {
private int serviceTime;
public Customer(){
}
public int getServiceTime(){
Random time = new Random();
serviceTime = time.nextInt(5) + 1;
return serviceTime;
}
}
main class
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
public class GroceryStoreQueue {
public static void main(String[] args) {
int newCust;
int serviceTime = 0;
int totalCust = 0;
int maxQ = 0;
Queue<Customer>custQ = new LinkedList<Customer>();
for(int i = 60; i >= 0; i--){//60 minutes of activity
Random r = new Random();
newCust = r.nextInt(4) + 1;//25% random number will be 1, if random number equals 1, add cust to q
Customer cust = new Customer();
cust.getServiceTime();
serviceTime--;
if(newCust == 1){
custQ.add(cust);//if customer has been added to line, add cust to q
custQ.size();
if(maxQ < custQ.size()){
maxQ = custQ.size();
}
System.out.println("\nNew customer has been added!, New queue length is now " + custQ.size());
totalCust += newCust;
if(serviceTime <= 0){
custQ.remove(cust);
System.out.println("\nCustomer serviced and removed from queue. New queue length is now " + custQ.size());
}
}
}
for(int i = 1; i<= 60; i++){
System.out.print("-");//shows the elapsed time
}
System.out.println("\nTotal number of customers serviced: " + totalCust);
System.out.println("Maximum line length during the simulation: " + maxQ);
}
}