1 Customer Write a class for modeling a customer. A customer knows its arrival t
ID: 3852396 • Letter: 1
Question
1 Customer
Write a class for modeling a customer. A customer knows its arrival time, its initial number of items, as well as the number of items remaining to be processed. The maximum number of items per customer is (MAX NUM ITEMS). Make sure you identified instance variables and class variables.
The constructor has a single parameter. It specifies the arrival time. The initial number of items is determined when the object is first created using the following formula:
Since, Math.random() generates a random number greater than or equal to 0.0 and less than 1.0, the expression MAX_NUM_ITEMS * Math.random() generates a number greater than or equals to 0.0 and less than MAX_NUM_ITEMS, adding 1 ensures that the number of items is greater than or equals to 1.0 but lower than MAX_NUM_ITEMS + 1. This real value is then converted to an int, in the range 1 to MAX_NUM_ITEMS (here, I wanted to make sure that no customer would show up empty handed).
The instance methods of a customer include:
• int getArrivalTime() returns the arrival time;
• int getNumberOfItems() returns the number of items remaining to be processed;
• int getNumberOfServedItems() returns the number of items that have been processed; • serve() decrements by one the number of items of this customer.
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 total 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 currentTime ) is a key element of the simulation. The method serveCustomers of each cashier is called once for each step of the simulation. The parameter cur- rentTime 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.
– If the cashier is not presently serving a customer, the next customer in line becomes the current customer, unless the queue is empty. Whenever, a customer is taken out of the queue, the cashier tallies the total amount time this customer spent waiting in line. If the cashier has no customer and the queue is empty, there is nothing to be done for this step;
– The cashier serves one item (a call to the method serve() of its current customer);
– If the current customer has no more items, the cashier adds the number of items of this customer to the tally. The state of this cashier object now indicates that this cashier has no current customer (the customer has been sent away).
There are also 3 instance methods used to report statistics for the total waiting time, total number of items served and total number of customers served by this cashier: int getTotalCustomerWaitTime(), int getTotalItemsServed(), and int getTotalCustomersServed(). Finally, the String toString() method returns a String that summarizes the statistics of this cashier.
Explanation / Answer
Simulation.java
public class Simulation {
// Constants
private static final String nl = System.getProperty( "line.separator" );
private static final int SECONDS_PER_MINUTE = 60;
private static final int MINUTES_PER_HOUR = 60;
private static final int TICK = 5;
private static final double PROBABILITY_NEW_ARRIVAL = 0.125;
private static final int EXPRESS_MAX_NUM_ITEMS = 12;
// Instance variables
private Cashiers express;
private Cashiers regular;
private int lengthOfSimulation;
// Constructor
public Simulation( int duration ) {
lengthOfSimulation = duration;
express = new Cashiers(1);
regular = new Cashiers(2);
}
public void run() {
int currentTime = 0;
while ( currentTime < lengthOfSimulation ) {
if ( Math.random() <= PROBABILITY_NEW_ARRIVAL ) {
Customer customer = new Customer( currentTime );
if ( customer.getNumberOfItems() <= EXPRESS_MAX_NUM_ITEMS ) {
express.addCustomer( customer );
} else {
regular.addCustomer( customer );
}
}
express.serveCustomers( currentTime );
regular.serveCustomers( currentTime );
currentTime += TICK;
}
display();
}
private void display() {
System.out.println( "SIMULATION :: " );
System.out.println( "The duration (in seconds) of the simulation was " + lengthOfSimulation + nl );
System.out.println( "EXPRESS CHECKOUT LINE :: " );
System.out.println( express );
System.out.println( "REGULAR CHECKOUT LINE :: " );
System.out.println( regular );
}
public static void main(String[] args) {
int duration = SECONDS_PER_MINUTE * MINUTES_PER_HOUR * 8;
Simulation sim = new Simulation( duration );
sim.run();
}
}
Cashiers.java
public class Cashier{
//constant
private static final String nl=System.getProperty("line.seperator");
//instance variables
private int customersServed;
private int totalCustomerWaitTime;
private int totalItemsServed;
private Customer currentCustomer;
private Queue<Customer> queue;
public Cashier(){
queue=new ArrayQueue<Customer>();
customersServed=0;
totalCustomerWaitTime=0;
totalItemsServed=0;
}
public void addCustomer(Customer c){
queue.enqueue(c);
}
public int getQueueSize(){
return queue.size();
}
public void serveCustomers(int currentTime){
if(currentCustomer==null && queue.isEmpty()) return;
if(currentCustomer==null){
currentCustomer=queue.dequeue();
totalCustomerWaitTime += currentTime -currentCustomer.getArrivalTime();
customersServed++;
}
currentCustomer.serve();
if(currentCustomer.getNumberOfItems()==0){
totalItemsServed += currentCustomer.getNumberOfServedItems();
currentCustomer=null;
}
}
public int getTotalCustomerWaitTime(){
return totalCustomerWaitTime;
}
public int getTotalCustomersServed(){
return customersServed;
}
public int getTotalItemsServed(){
return totalItemsServed;
}
public String toString(){
StringBuffer results= new StringBuffer();
results.append("The total number of customers served is: ");
results.append(customersServed);
results.append(nl);
results.append("The average number of items per customer is: ");
results.append(totalItemsServed/customersServed);
results.append(nl);
results.append("The average waiting time(seconds) is: ");
results.append(totalCustomerWaitTime/customersServed);
results.append(nl);
return results.toString();
}
}
Customer.java
public class Customer{
private int arrivalTime;
private int initialNumberOfItems;
private int numberOfItems;
private static final int MAX_NUM_ITEMS=50;
public Customer(int arrivalTime){
this.arrivalTime=arrivalTime;
numberOfItems=(int)((MAX_NUM_ITEMS-1) * Math.random() +1);
initialNumberOfItems=numberOfItems;
}
public int getArrivalTime(){
return arrivalTime;
}
public int getNumberOfItems(){
return numberOfItems;
}
public int getNumberOfServedItems(){
return initialNumberOfItems-numberOfItems;
}
public void serve(){
numberOfItems--;
}
}
Queue.java
public interface Queue<E> {
public abstract void enqueue( E obj );
public abstract E dequeue();
public abstract boolean isEmpty();
public abstract int size();
}
ArrayQueue.java
public class ArrayQueue<E> implements Queue<E> {
// Constant
private static final int MAX_QUEUE_SIZE = 10000;
// Instance variables
private E[] elems; // stores the elements of this queue
private int front, rear, size;
@SuppressWarnings( "unchecked" )
public ArrayQueue() {
elems = (E []) new Object[MAX_QUEUE_SIZE]; //casting to the type tht you want
front = 0;
rear = -1;
size = 0;
}
// Instance methods
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == MAX_QUEUE_SIZE;
}
public void enqueue( E elem ) {
// pre-condition: ???
if ( rear == ( MAX_QUEUE_SIZE -1 ) ) {
int j=0;
for ( int i=front; i<=rear; i++ ) {
elems[ j++ ] = elems[ i ];
}
front = 0;
rear = size - 1;
}
elems[ ++rear ] = elem;
size++;
}
public E dequeue() {
// pre-condition: ???
E saved = elems[ front ];
elems[ front ] = null; // scrubbing the memory!
front++;
size--;
return saved;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.