2 Cashier A cashier is responsible for helping a queue of customers. It serves o
ID: 3852387 • Letter: 2
Question
2 Cashier A cashier is responsible for helping a queue of customers. It serves one customer at a time. Since the simulation is used to produce statistics, a cashier also memorizes the tota number of customers served, the total amount of time the customers have been waiting, as well as the total number of items served (processed). Make sure you identified instance variables and class variables . The class has a single constructor. It has no parameters. It initializes the instance variables of the cashier The method addCustomer( Customer c ) adds a customer to the rear of its queue. The method int getQueueSize) returns the number of customers currently waiting in line. . The method serveCustomers( int current Time is a key element of the simulation. The method serveCustomers of each cashiers called once for each step of the simulation. The parameter cur- rent Time is used to compute the total amount of time this customer has spent waiting in line. Here is the behaviour of the cashier when serving customers.Explanation / Answer
public class Cashier { private Queue queue; private Customer currentCustomer; private int customerWaitTime,itemsServed,customersServed; public Cashier(){ queue = new ArrayQueue(); customerWaitTime = 0; itemsServed = 0; customersServed = 0; } public void addCustomer(Customer c) { queue.enqueue(c); } public void serveCustomers(int currentTime){ if(currentCustomer == null && queue.isEmpty()){//no customers in line return; } if(currentCustomer == null){ currentCustomer = queue.dequeue(); customerWaitTime += currentTime - currentCustomer.getArrivalTime(); customersServed++; } currentCustomer.serve(); //current customer has no more items if(currentCustomer.getNumberOfItems() == 0){ itemsServed += currentCustomer.getNumberOfServedItems(); currentCustomer = null; } } public int getQueueSize(){ return queue.size(); } public int getTotalCustomerWaitTime(){ return customerWaitTime; } public int getTotalItemsServed(){ return itemsServed; } public int getTotalCustomersServed(){ return customersServed; } public String toString(){ StringBuffer results = new StringBuffer(); results.append("The number of customers served: "); results.append(customersServed); results.append(" "); results.append("The average number of items served: "); results.append(itemsServed/customersServed); results.append(" "); results.append("Total customer wait time (in seconds) = " ); results.append(customerWaitTime/customersServed); results.append(" "); return results.toString(); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.