// DO NOT ADD NEW METHODS OR NEW DATA FIELDS! package PJ3; class Cashier { // ca
ID: 3698178 • Letter: #
Question
// DO NOT ADD NEW METHODS OR NEW DATA FIELDS!
package PJ3;
class Cashier {
// cashier id and current customer which is served by this cashier
private int cashierID;
private Customer serveCustomer;
// start time and end time of current interval
private int startTime;
private int endTime;
// for keeping statistical data
private int totalFreeTime;
private int totalBusyTime;
private int totalCustomers;
// Constructor
Cashier()
{
// add statements
}
// Constructor with cashier id
Cashier(int cashierId)
{
// add statements
}
// accessor methods
int getCashierID()
{
return cashierID;
}
Customer getCurrentCustomer()
{
// add statements
return null;
}
// Transition from free interval to busy interval
void freeToBusy (Customer serveCustomer, int currentTime)
{
// goal : switch from free interval to busy interval
// i.e. end free interval, start busy interval
// to serve a new customer
//
// steps : update totalFreeTime
// set startTime, endTime, serveCustomer,
// update totalCustomers
// add statements
}
// Transition from busy interval to free interval
Customer busyToFree ()
{
// goal : switch from busy interval to free interval
// i.e. end busy interval to return served customer,
// start free interval
//
// steps : update totalBusyTime
// set startTime
// return serveCustomer
// add statements
return null;
}
// Return end busy clock time, use in priority queue
int getEndBusyTime()
{
// add statements
return 0;
}
// For free interval at the end of simulation,
// update totalFreeTime
void setEndFreeTime (int endsimulationtime)
{
// for free interval at the end of simulation:
// set endTime, update totalFreeTime
// add statements
}
// For busy interval at the end of simulation,
// update totalBusyTime
void setEndBusyTime (int endsimulationtime)
{
// for busy interval at the end of simulation:
// set endTime, update totalBusyTime
// add statements
}
// functions for printing statistics :
void printStatistics ()
{
// print cashier statistics, see project statement
System.out.println(" Cashier ID : "+cashierID);
System.out.println(" Total free time : "+totalFreeTime);
System.out.println(" Total busy time : "+totalBusyTime);
System.out.println(" Total # of customers : "+totalCustomers);
if (totalCustomers > 0)
System.out.format(" Average checkout time : %.2f%n ",(totalBusyTime*1.0)/totalCustomers);
}
public String toString()
{
return "CashierID="+cashierID+":startTime="+startTime+
":endTime="+endTime+">>serveCustomer:"+serveCustomer;
}
public static void main(String[] args) {
// quick check
Customer mycustomer = new Customer(1,15,5);
Cashier mycashier = new Cashier(5);
mycashier.freeToBusy (mycustomer, 12);
System.out.println(mycashier);
}
};
-----------------------------------------------------------------------------------------------------------------------------
package PJ3;
import java.util.*;
//--------------------------------------------------------------------------
//
// Define simulation queues in a checkout area. Queues hold references to Customer
// and Cashier objects
//
// Customer (FIFO) queue is used to hold waiting customers. If the queue is too long
// (i.e. > customerQLimit), customer goes away without entering customer queue
//
// There are several cashiers in a checkout area. Use PriorityQueue to
// hold BUSY cashiers and FIFO queue to hold FREE cashiers,
// i.e. a cashier that is FREE for the longest time should start be used first.
//
// To handle cashier in PriorityQueue, we need to define comparator
// for comparing 2 cashier objects. Here is a constructor from Java API:
//
// PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
//
// For priority queue, the default compare function is "natural ordering"
// i.e. for numbers, minimum value is returned first
//
// User can define own comparator class for PriorityQueue.
// For cashier objects, we like to have smallest end busy interval time first.
// i.e. use Cashier's getEndBusyTime()
//
// The following class define compare() for two cashiers :
class CompareCashier implements Comparator<Cashier>{
// overide compare() method
public int compare(Cashier o1, Cashier o2) {
return o1.getEndBusyTime() - o2.getEndBusyTime();
}
}
// DO NOT ADD NEW METHODS OR NEW DATA FIELDS
class CheckoutArea {
// Private data fields:
// define one priority queue
private PriorityQueue <Cashier> busyCashierQ;
// define two FIFO queues
private Queue<Customer> customerQ;
private Queue<Cashier> freeCashierQ;
// define customer queue limit
private int customerQLimit;
// Constructor
public CheckoutArea()
{
// add statements
}
// Constructor
public CheckoutArea(int numCashiers, int customerQlimit)
{
// use ArrayDeque to construct FIFO queue objects
// construct PriorityQueue object
// overide compare() in Comparator to compare Cashier objects
busyCashierQ= new PriorityQueue<Cashier>( numCashiers,
new CompareCashier());
// initialize customerQlimit
// Construct Cashier objects and insert into FreeCashierQ
// add statements
}
// -------------------------------------------------
// freeCashierQ methods: remove, insert, empty, size
// -------------------------------------------------
public Cashier removeFreeCashierQ()
{
// remove and return a free cashier
// Add statetments
return null;
}
public void insertFreeCashierQ(Cashier cashier)
{
// insert a free cashier
// Add statetments
}
public boolean emptyFreeCashierQ()
{
// is freeCashierQ empty?
// Add statetments
return false;
}
public int sizeFreeCashierQ()
{
// get number of free cashiers
// Add statetments
return 0;
}
// -------------------------------------------------------
// busyCashierQ methods: remove, insert, empty, size, peek
// -------------------------------------------------------
public Cashier removeBusyCashierQ()
{
// remove and return a busy cashier
// Add statetments
return null;
}
public void insertBusyCashierQ(Cashier cashier)
{
// insert a busy cashier
// Add statetments
}
public boolean emptyBusyCashierQ()
{
// is busyCashierQ empty?
// Add statetments
return false;
}
public int sizeBusyCashierQ()
{
// get number of busy cashiers
// Add statetments
return 0;
}
public Cashier peekBusyCashierQ()
{
// get highest prioirty cashier
// "retrieve" but not "remove"
// Add statetments
return null;
}
// -------------------------------------------------------
// customerQ methods: remove, insert, empty, size
// and check isCustomerQTooLong()
// -------------------------------------------------------
public Customer removeCustomerQ()
{
// remove and return a customer
// Add statetments
return null;
}
public void insertCustomerQ(Customer customer)
{
// insert a customer
// Add statetments
}
public boolean emptyCustomerQ()
{
// is customerQ empty?
// Add statetments
return false;
}
public int sizeCustomerQ()
{
// get number of customers
// Add statetments
return 0;
}
public boolean isCustomerQTooLong()
{
// is customerQ too long?
// Add statetments
return false;
}
public void printStatistics()
{
System.out.println(" # waiting customers : "+sizeCustomerQ());
System.out.println(" # busy cashiers : "+sizeBusyCashierQ());
System.out.println(" # free cashiers : "+sizeFreeCashierQ());
}
public static void main(String[] args) {
// quick check
// create a CheckoutArea and 4 customers
CheckoutArea sc = new CheckoutArea(4, 5);
Customer c1 = new Customer(1,18,10);
Customer c2 = new Customer(2,33,11);
Customer c3 = new Customer(3,21,12);
Customer c4 = new Customer(4,37,13);
// insert customers into customerQ
sc.insertCustomerQ(c1);
sc.insertCustomerQ(c2);
sc.insertCustomerQ(c3);
sc.insertCustomerQ(c4);
System.out.println("customerQ:"+sc.customerQ);
System.out.println("===============================================");
System.out.println("Remove customer:"+sc.removeCustomerQ());
System.out.println("Remove customer:"+sc.removeCustomerQ());
System.out.println("Remove customer:"+sc.removeCustomerQ());
System.out.println("Remove customer:"+sc.removeCustomerQ());
System.out.println("===============================================");
// remove cashiers from freeCashierQ
System.out.println("freeCashierQ:"+sc.freeCashierQ);
System.out.println("===============================================");
Cashier p1=sc.removeFreeCashierQ();
Cashier p2=sc.removeFreeCashierQ();
Cashier p3=sc.removeFreeCashierQ();
Cashier p4=sc.removeFreeCashierQ();
System.out.println("Remove free cashier:"+p1);
System.out.println("Remove free cashier:"+p2);
System.out.println("Remove free cashier:"+p3);
System.out.println("Remove free cashier:"+p4);
System.out.println("===============================================");
System.out.println("freeCashierQ:"+sc.freeCashierQ);
System.out.println("===============================================");
// insert customers to cashiers
p1.freeToBusy (c1, 13);
p2.freeToBusy (c2, 13);
p3.freeToBusy (c3, 13);
p4.freeToBusy (c4, 13);
// insert cashiers to busyCashierQ
System.out.println("busyCashierQ:"+sc.busyCashierQ);
System.out.println("===============================================");
sc.insertBusyCashierQ(p1);
sc.insertBusyCashierQ(p4);
sc.insertBusyCashierQ(p2);
sc.insertBusyCashierQ(p3);
System.out.println("busyCashierQ:"+sc.busyCashierQ);
System.out.println("===============================================");
// remove cashiers from busyCashierQ
p1=sc.removeBusyCashierQ();
p2=sc.removeBusyCashierQ();
p3=sc.removeBusyCashierQ();
p4=sc.removeBusyCashierQ();
System.out.println("Remove busy cashier:"+p1);
System.out.println("Remove busy cashier:"+p2);
System.out.println("Remove busy cashier:"+p3);
System.out.println("Remove busy cashier:"+p4);
}
};
-----------------------------------------------------------------------------------------------------------------------------
// DO NOT ADD NEW METHODS OR NEW DATA FIELDS!
package PJ3;
class Customer
{
private int customerID;
private int serviceTime;
private int arrivalTime;
// default constructor
Customer()
{
// add statements
}
// constructor to set customerID, serviceTime and arrivalTime
Customer(int customerid, int servicetime, int arrivaltime)
{
// add statements
arrivalTime = arrivaltime;
}
int getServiceTime()
{
// add statements
return 0;
}
int getArrivalTime()
{
// add statements
return 0;
}
int getCustomerID()
{
return customerID;
}
public String toString()
{
return "customerID="+customerID+":serviceTime="+
serviceTime+":arrivalTime="+arrivalTime;
}
public static void main(String[] args) {
// quick check!
Customer mycustomer = new Customer(1,35,5);
System.out.println("Customer Info --> "+mycustomer);
}
}
-----------------------------------------------------------------------------------------------------------------------------
package PJ3;
import java.util.*;
import java.io.*;
// You may add new functions or data fields in this class
// You may modify any functions or data members here
// You must use Customer, Cashier and CheckoutArea classes
// to implement SuperMart simulator
class SuperMart {
// input parameters
private int numCashiers, customerQLimit;
private int chancesOfArrival, maxServiceTime;
private int simulationTime, dataSource;
// statistical data
private int numGoaway, numServed, totalWaitingTime;
// internal data
private int counter; // customer ID counter
private CheckoutArea checkoutarea; // checkout area object
private Scanner dataFile; // get customer data from file
private Random dataRandom; // get customer data using random function
// most recent customer arrival info, see getCustomerData()
private boolean anyNewArrival;
private int serviceTime;
// initialize data fields
private SuperMart()
{
// add statements
}
private void setupParameters()
{
// read input parameters from user
// setup dataFile or dataRandom
// add statements
}
// use by step 1 in doSimulation()
private void getCustomerData()
{
// get next customer data : from file or random number generator
// set anyNewArrival and serviceTime
// add statements
}
private void doSimulation()
{
// add statements
// Initialize CheckoutArea
// Time driver simulation loop
for (int currentTime = 0; currentTime < simulationTime; currentTime++) {
// Step 1: any new customer enters the checkout area?
getCustomerData();
if (anyNewArrival) {
// Step 1.1: setup customer data
// Step 1.2: check customer waiting queue too long?
} else {
System.out.println(" No new customer!");
}
// Step 2: free busy cashiers, add to free cashierQ
// Step 3: get free cashiers to serve waiting customers
} // end simulation loop
}
private void printStatistics()
{
// add statements into this method!
// print out simulation results
// see the given example in project statement
// you need to display all free and busy cashiers
}
// *** main method to run simulation ****
public static void main(String[] args) {
SuperMart runSuperMart=new SuperMart();
runSuperMart.setupParameters();
runSuperMart.doSimulation();
runSuperMart.printStatistics();
}
}
Explanation / Answer
package PJ3;
class Cashier {
// cashier id and current customer which is served by this cashier
private int cashierID;
private Customer serveCustomer;
// start time and end time of current interval
private int startTime;
private int endTime;
// for keeping statistical data
private int totalFreeTime;
private int totalBusyTime;
private int totalCustomers;
// Constructor
Cashier()
{
//initializing variables to zero
cashierID = -1;
startTime = 0;
endTime = 0;
totalFreeTime = 0;
totalBusyTime = 0;
totalCustomers = 0;
}
// Constructor with cashier id
Cashier(int cashierId)
{
cashierID = cashierId;
startTime = 0;
endTime = 0;
totalFreeTime = 0;
totalBusyTime = 0;
totalCustomers = 0;
}
// accessor methods
int getCashierID()
{
return cashierID;
}
Customer getServeCustomer()
{
return serveCustomer;
}
// Transition from free interval to busy interval
void freeToBusy (Customer serveCustomer, int currentTime)
{
totalFreeTime += currentTime - startTime;
BUSY = 1;
this.serveCustomer = serveCustomer;
startTime = currentTime;
endTime = startTime + serveCustomer.getCheckoutTime();
totalCustomers++;
}
// Transition from busy interval to free interval
Customer busyToFree ()
{
totalBusyTime += endTime - startTime;
FREE = 0;
startTime = endTime;
this.currentCustomer = currentCustomer;
return currentCustomer;
}
// Return end busy clock time, use in priority queue
int getEndBusyTime()
{
return endTime;
}
// For free interval at the end of simulation,
// update totalFreeTime
void setEndFreeTime (int endsimulationtime)
{
endTime = endsimulationtime;
if (FREE == 1) {
totalFreeTime += endTime - startTime;
}else{
totalBusyTime += endTime - startTime;
this.currentCustomer = currentCustomer;
}
}
// For busy interval at the end of simulation,
// update totalBusyTime
void setEndBusyTime (int endsimulationtime)
{
// for busy interval at the end of simulation:
// set endTime, update totalBusyTime
// add statements
}
// functions for printing statistics :
void printStatistics ()
{
// print cashier statistics, see project statement
System.out.println(" Cashier ID : "+cashierID);
System.out.println(" Total free time : "+totalFreeTime);
System.out.println(" Total busy time : "+totalBusyTime);
System.out.println(" Total # of customers : "+totalCustomers);
if (totalCustomers > 0)
System.out.format(" Average checkout time : %.2f%n ",(totalBusyTime*1.0)/totalCustomers);
}
public String toString()
{
return "CashierID="+cashierID+":startTime="+startTime+
":endTime="+endTime+">>serveCustomer:"+serveCustomer;
}
public static void main(String[] args) {
// quick check
Customer mycustomer = new Customer(1,15,5);
Cashier mycashier = new Cashier(5);
mycashier.freeToBusy (mycustomer, 12);
System.out.println(mycashier);
}
};
//
class CheckoutArea {
// Private data fields:
// define one priority queue
private PriorityQueue <Cashier> busyCashierQ;
// define two FIFO queues
private Queue<Customer> customerQ;
private Queue<Cashier> freeCashierQ;
// define customer queue limit
private int customerQLimit;
// Constructor
public CheckoutArea()
{
}
// Constructor
public CheckoutArea(int numCashiers, int customerQlimit)
{
customerQ = new ArrayDeque<Customer>();
freeCashierQ = new ArrayDeque<Cashier>();
busyCashierQ= new PriorityQueue<Cashier>( numCashiers,
new CompareCashier());
this.customerQLimit = customerQlimit;
for (int i = startCashierID; i < numCashiers; i++) {
freeCashierQ.add(new Cashier(i));
}
// -------------------------------------------------
// freeCashierQ methods: remove, insert, empty, size
// -------------------------------------------------
public Cashier removeFreeCashierQ()
{
return freeCashierQ.poll();
}
public void insertFreeCashierQ(Cashier cashier)
{
freeCashierQ.add(cashier);
}
public boolean emptyFreeCashierQ()
{
return freeCashierQ.isEmpty();
}
public int sizeFreeCashierQ()
{
return freeCashierQ.isEmpty() ? 0 : freeCashierQ.size();
}
// -------------------------------------------------------
// busyCashierQ methods: remove, insert, empty, size, peek
// -------------------------------------------------------
public Cashier removeBusyCashierQ()
{
return busyCashierQ.isEmpty();
}
public void insertBusyCashierQ(Cashier cashier)
{
return busyCashierQ.poll();
}
public boolean emptyBusyCashierQ()
{
return busyCashierQ.isEmpty();
}
public int sizeBusyCashierQ()
{
return busyCashierQ.isEmpty() ? 0 : busyCashierQ.size();
}
public Cashier peekBusyCashierQ()
{
return busyCashierQ.peek();
return null;
}
// -------------------------------------------------------
// customerQ methods: remove, insert, empty, size
// and check isCustomerQTooLong()
// -------------------------------------------------------
public Customer removeCustomerQ()
{
return customerQ.remove();
}
public void insertCustomerQ(Customer customer)
{
customerQ.add(customer);
}
public boolean emptyCustomerQ()
{
return customerQ.remove();
}
public int sizeCustomerQ()
{
return customerQ.isEmpty() ? 0 : customerQ.size();
}
public boolean isCustomerQTooLong()
{
return !customerQ.isEmpty() && customerQ.size() >= customerQLimit;
}
public void printStatistics()
{
System.out.println(" # waiting customers : "+sizeCustomerQ());
System.out.println(" # busy cashiers : "+sizeBusyCashierQ());
System.out.println(" # free cashiers : "+sizeFreeCashierQ());
}
public static void main(String[] args) {
// quick check
// create a CheckoutArea and 4 customers
CheckoutArea sc = new CheckoutArea(4, 5);
Customer c1 = new Customer(1,18,10);
Customer c2 = new Customer(2,33,11);
Customer c3 = new Customer(3,21,12);
Customer c4 = new Customer(4,37,13);
// insert customers into customerQ
sc.insertCustomerQ(c1);
sc.insertCustomerQ(c2);
sc.insertCustomerQ(c3);
sc.insertCustomerQ(c4);
System.out.println("customerQ:"+sc.customerQ);
System.out.println("===============================================");
System.out.println("Remove customer:"+sc.removeCustomerQ());
System.out.println("Remove customer:"+sc.removeCustomerQ());
System.out.println("Remove customer:"+sc.removeCustomerQ());
System.out.println("Remove customer:"+sc.removeCustomerQ());
System.out.println("===============================================");
// remove cashiers from freeCashierQ
System.out.println("freeCashierQ:"+sc.freeCashierQ);
System.out.println("===============================================");
Cashier p1=sc.removeFreeCashierQ();
Cashier p2=sc.removeFreeCashierQ();
Cashier p3=sc.removeFreeCashierQ();
Cashier p4=sc.removeFreeCashierQ();
System.out.println("Remove free cashier:"+p1);
System.out.println("Remove free cashier:"+p2);
System.out.println("Remove free cashier:"+p3);
System.out.println("Remove free cashier:"+p4);
System.out.println("===============================================");
System.out.println("freeCashierQ:"+sc.freeCashierQ);
System.out.println("===============================================");
// insert customers to cashiers
p1.freeToBusy (c1, 13);
p2.freeToBusy (c2, 13);
p3.freeToBusy (c3, 13);
p4.freeToBusy (c4, 13);
// insert cashiers to busyCashierQ
System.out.println("busyCashierQ:"+sc.busyCashierQ);
System.out.println("===============================================");
sc.insertBusyCashierQ(p1);
sc.insertBusyCashierQ(p4);
sc.insertBusyCashierQ(p2);
sc.insertBusyCashierQ(p3);
System.out.println("busyCashierQ:"+sc.busyCashierQ);
System.out.println("===============================================");
// remove cashiers from busyCashierQ
p1=sc.removeBusyCashierQ();
p2=sc.removeBusyCashierQ();
p3=sc.removeBusyCashierQ();
p4=sc.removeBusyCashierQ();
System.out.println("Remove busy cashier:"+p1);
System.out.println("Remove busy cashier:"+p2);
System.out.println("Remove busy cashier:"+p3);
System.out.println("Remove busy cashier:"+p4);
}
};
package PJ3;
class Customer
{
private int customerID;
private int serviceTime;
private int arrivalTime;
// default constructor
Customer()
{
// add statements
}
// constructor to set customerID, serviceTime and arrivalTime
Customer(int customerid, int servicetime, int arrivaltime)
{
this. customerid= customerid;
this. servicetime= servicetime;
arrivalTime = arrivaltime;
}
int getServiceTime()
{
return servicetime;
}
int getArrivalTime()
{ return arrivaltime;
}
int getCustomerID()
{
return customerID;
}
public String toString()
{
return "customerID="+customerID+":serviceTime="+
serviceTime+":arrivalTime="+arrivalTime;
}
public static void main(String[] args) {
// quick check!
Customer mycustomer = new Customer(1,35,5);
System.out.println("Customer Info --> "+mycustomer);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.