21
ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Embed Size (px)

Citation preview

Page 1: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

ITI 1221. Introduction to Computing IILab-8

Dewan Tanvir AhmedUniversity of Ottawa

Page 2: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Today’s Objective

Case Studyo Simulating a Supermarket Check out

Page 3: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Supermarket Checkout – General Idea (cont..)

MarketModel

CustomerCashier

Queue

Creates Generates

Uses Contains

Serves

For simplicity, Let’s consider only one cashier,

During Program we’ll handle it.

Page 4: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Supermarket Checkout – General Idea

Input Range of Values

Total minute (length of Simulation) 0 < total < 1000

Average Time Spent per Customer0 < average <=

total

Probability of a new customer arrival in the next minute 0 < probability <= 1

Output

Total Customers processes

Customers left in line

Average waiting time

Page 5: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Supermarket Checkout

Two types of Cashiers

Express ( if number of items are less 13 )

Regular

Each cashier has an queue for the customers

A new customer joins to a queue having minimum number of customers for both cases (express and regular cashiers).

Page 6: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Customer

Each customer haso Arrival timeo Initial number of itemso Remaining number of items for serve

Constructor to initialize the instance variableso public Customer(int arrivalTime, int numberOfItems)

Methodso Access methods

arrival time and number of remaining items

o Current status of the Served (on going) customer int numberOfServedItems() number of items served so far

o Update remaining items of a customer void serve()

Page 7: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Customer – (cont..)

public class Customer { private int arrivalTime; private int numberOfItems; private int initialnumberOfItems;

// constructor public Customer(int arrivalTime, int numberOfItems) { this.arrivalTime = arrivalTime; this.numberOfItems = numberOfItems; this.initialnumberOfItems = numberOfItems; }

public int numberOfServedItems() { return this.initialnumberOfItems - this.numberOfItems; } public void serve() {numberOfItems--; }

// access methods public int arrivalTime() { return arrivalTime; } public int numberOfItems() { return numberOfItems; }

}

Page 8: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Queue

public interface Queue {

public void enqueue(Object obj); public Object dequeue(); public boolean isEmpty(); public int size();

}

public interface Queue {

public void enqueue(Object obj); public Object dequeue(); public boolean isEmpty(); public int size();

}

Page 9: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Queue - ArrayQueue

public class ArrayQueue implements Queue {

private static final int MAX_QUEUE_SIZE = 10000; private Object[] q; private int front, rear, size;

public ArrayQueue() { q = new Object[MAX_QUEUE_SIZE]; front = 0; rear = -1; size = 0; }

public int size() { return size; }

public boolean isEmpty() { return size == 0; }

public boolean isFull() { return size == MAX_QUEUE_SIZE; }

public class ArrayQueue implements Queue {

private static final int MAX_QUEUE_SIZE = 10000; private Object[] q; private int front, rear, size;

public ArrayQueue() { q = new Object[MAX_QUEUE_SIZE]; front = 0; rear = -1; size = 0; }

public int size() { return size; }

public boolean isEmpty() { return size == 0; }

public boolean isFull() { return size == MAX_QUEUE_SIZE; }

Page 10: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Queue – ArrayQueue (cont..)

public void enqueue(Object o) { // pre-condition: ??? if (rear == (MAX_QUEUE_SIZE -1)) { int j=0; for (int i=front; i<=rear; i++) { q[j++] = q[i]; } front = 0; rear = size - 1; } q[++rear] = o; size++; }

public Object dequeue() { // pre-condition: ??? Object savedValue = q[front]; q[front] = null; front++; size--; return savedValue; }} // end of the class

public void enqueue(Object o) { // pre-condition: ??? if (rear == (MAX_QUEUE_SIZE -1)) { int j=0; for (int i=front; i<=rear; i++) { q[j++] = q[i]; } front = 0; rear = size - 1; } q[++rear] = o; size++; }

public Object dequeue() { // pre-condition: ??? Object savedValue = q[front]; q[front] = null; front++; size--; return savedValue; }} // end of the class

Page 11: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Cashier

Each cashier has o Total waiting time of all customerso Total number of served customerso Total number of served itemso Current customero A queue to store waiting customers

Constructoro Initialize - instance variableso You need a Queue Data Structure

Methods & Access methodso Get total waiting timeo Get total served itemso Get total served customerso Add a new customer to the cashier queueo Get Number of customers in the queueo Serve one customer at a time

Page 12: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Cashier – (cont..)public class Cashier { private int totalCustomerWaitTime, customersServed, totalItemsServed; private Customer currentCustomer; private Queue queue;

// constructor public Cashier(){ totalCustomerWaitTime = 0; customersServed = 0; currentCustomer = null; totalItemsServed = 0; queue = new ArrayQueue(); }

// access methods public int getTotalCustomerWaitTime() { return this.totalCustomerWaitTime; }

public int getTotalItemsServed() { if(this.currentCustomer != null) return totalItemsServed + this.currentCustomer.numberOfServedItems(); else return totalItemsServed; }

public int getTotalCustomersServed() { return customersServed; } public void addCustomer(Customer c){ queue.enqueue(c); } public int lengthOfQueue() { return queue.size(); }

public class Cashier { private int totalCustomerWaitTime, customersServed, totalItemsServed; private Customer currentCustomer; private Queue queue;

// constructor public Cashier(){ totalCustomerWaitTime = 0; customersServed = 0; currentCustomer = null; totalItemsServed = 0; queue = new ArrayQueue(); }

// access methods public int getTotalCustomerWaitTime() { return this.totalCustomerWaitTime; }

public int getTotalItemsServed() { if(this.currentCustomer != null) return totalItemsServed + this.currentCustomer.numberOfServedItems(); else return totalItemsServed; }

public int getTotalCustomersServed() { return customersServed; } public void addCustomer(Customer c){ queue.enqueue(c); } public int lengthOfQueue() { return queue.size(); }

Page 13: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Cashier – (cont..) public void serveCustomers(int currentTime){

if (currentCustomer == null){ // no customers yet!!! if (queue.isEmpty()) return; else{ // dequeue first waiting customer and tally results currentCustomer = (Customer) queue.dequeue(); totalCustomerWaitTime = totalCustomerWaitTime + currentTime - currentCustomer.arrivalTime(); customersServed++; } }

// give a unit of service currentCustomer.serve();

// if current customer is finished, send it away if (currentCustomer.numberOfItems() == 0) { totalItemsServed += currentCustomer.numberOfServedItems(); currentCustomer = null; } }

} // end of the class

public void serveCustomers(int currentTime){

if (currentCustomer == null){ // no customers yet!!! if (queue.isEmpty()) return; else{ // dequeue first waiting customer and tally results currentCustomer = (Customer) queue.dequeue(); totalCustomerWaitTime = totalCustomerWaitTime + currentTime - currentCustomer.arrivalTime(); customersServed++; } }

// give a unit of service currentCustomer.serve();

// if current customer is finished, send it away if (currentCustomer.numberOfItems() == 0) { totalItemsServed += currentCustomer.numberOfServedItems(); currentCustomer = null; } }

} // end of the class

Page 14: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Cashiers

Cashiers haso An array of cashier

Constructoro Initialized the size of the cashiers array

Methodso Adding a new customer

Where size of the queue is minimumo Serving customers at a particular instant of timeo toString()

Average number of customers currently waiting Average number of items per customer Average waiting time per customer Average number of customers served per cashier Average number of items Handled per cashier Total number of clients served Total number of items handled

Page 15: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Cashiers – (cont..)

import java.text.NumberFormat;

public class Cashiers { private Cashier[] cashiers; private static final String nl = System.getProperty("line.separator"); public Cashiers(int n) { if (n < 1) throw new IllegalArgumentException();

cashiers = new Cashier[n]; for (int i=0; i<n; i++) cashiers[i] = new Cashier(); }

public void addCustomer(Customer c) { int iMax, vMax;

iMax = 0; for (int i=1; i<cashiers.length; i++) if (cashiers[i].lengthOfQueue() < cashiers[iMax].lengthOfQueue()) iMax = i;

cashiers[iMax].addCustomer(c); }

import java.text.NumberFormat;

public class Cashiers { private Cashier[] cashiers; private static final String nl = System.getProperty("line.separator"); public Cashiers(int n) { if (n < 1) throw new IllegalArgumentException();

cashiers = new Cashier[n]; for (int i=0; i<n; i++) cashiers[i] = new Cashier(); }

public void addCustomer(Customer c) { int iMax, vMax;

iMax = 0; for (int i=1; i<cashiers.length; i++) if (cashiers[i].lengthOfQueue() < cashiers[iMax].lengthOfQueue()) iMax = i;

cashiers[iMax].addCustomer(c); }

Page 16: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Cashiers – (cont..)

public void serveCustomers(int currentTime) { for (int i=0; i<cashiers.length; i++) cashiers[i].serveCustomers(currentTime); }

public String toString() { int nbCustomers = 0; int nbItems = 0; int waitingTime = 0; int nbServedCustomers = 0; String out = "Tally: " + nl;

for (int i=0; i<cashiers.length; i++) { nbCustomers += cashiers[i].lengthOfQueue(); nbItems += cashiers[i].getTotalItemsServed(); waitingTime += cashiers[i].getTotalCustomerWaitTime(); nbServedCustomers += cashiers[i].getTotalCustomersServed();

} double aveLength = (double) nbCustomers / (double) cashiers.length; double aveItemsCustomer = (double) nbItems/ (double) nbServedCustomers; double aveItemsCashier = (double) nbItems/ (double) cashiers.length; double aveWaitingTimeCus = (double) waitingTime / (double) nbServedCustomers; double aveServedCustomersCashier = (double) nbServedCustomers/ (double) cashiers.length;

public void serveCustomers(int currentTime) { for (int i=0; i<cashiers.length; i++) cashiers[i].serveCustomers(currentTime); }

public String toString() { int nbCustomers = 0; int nbItems = 0; int waitingTime = 0; int nbServedCustomers = 0; String out = "Tally: " + nl;

for (int i=0; i<cashiers.length; i++) { nbCustomers += cashiers[i].lengthOfQueue(); nbItems += cashiers[i].getTotalItemsServed(); waitingTime += cashiers[i].getTotalCustomerWaitTime(); nbServedCustomers += cashiers[i].getTotalCustomersServed();

} double aveLength = (double) nbCustomers / (double) cashiers.length; double aveItemsCustomer = (double) nbItems/ (double) nbServedCustomers; double aveItemsCashier = (double) nbItems/ (double) cashiers.length; double aveWaitingTimeCus = (double) waitingTime / (double) nbServedCustomers; double aveServedCustomersCashier = (double) nbServedCustomers/ (double) cashiers.length;

Page 17: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Cashiers – (cont..)

NumberFormat nf = NumberFormat.getNumberInstance(); out += "Average number of customers currently waiting: " + nf.format(aveLength) +nl; out += "Average number of items per customer: " + nf.format(aveItemsCustomer)+nl; out += "Average waiting time per customer: " + nf.format(aveWaitingTimeCus)+nl; out += "Average number of customers served per cashier: " + nf.format(aveServedCustomersCashier)+nl; out += "Average number of items Handled per cashier: " + nf.format(aveItemsCashier)+nl; out += "Total number of clients served: " + nf.format(nbServedCustomers)+nl; out += "Total number of items handled: " + nf.format(nbItems)+nl;

return out;

}}

NumberFormat nf = NumberFormat.getNumberInstance(); out += "Average number of customers currently waiting: " + nf.format(aveLength) +nl; out += "Average number of items per customer: " + nf.format(aveItemsCustomer)+nl; out += "Average waiting time per customer: " + nf.format(aveWaitingTimeCus)+nl; out += "Average number of customers served per cashier: " + nf.format(aveServedCustomersCashier)+nl; out += "Average number of items Handled per cashier: " + nf.format(aveItemsCashier)+nl; out += "Total number of clients served: " + nf.format(nbServedCustomers)+nl; out += "Total number of items handled: " + nf.format(nbItems)+nl;

return out;

}}

Page 18: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

MarketModel

public class MarketModel {

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 String nl = System.getProperty("line.separator");

private static final double probabilityOfNewArrival = 0.5;

private Cashiers express; private Cashiers regular;

private int lengthOfSimulation, aveNbItems;

public MarketModel(int duration) { this.lengthOfSimulation = duration; express = new Cashiers(3); regular = new Cashiers(10); }

Page 19: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

MarketModel – (cont..)

public void runSimulation() {

int currentTime = 0; while (currentTime < lengthOfSimulation) {

if (Math.random() <= probabilityOfNewArrival) {

Customer customer = new Customer(currentTime, (int) (50 * Math.random()) + 1); if (customer.numberOfItems() <= 12) express.addCustomer(customer); else regular.addCustomer(customer); }

express.serveCustomers(currentTime); regular.serveCustomers(currentTime);

if ((currentTime % (5 * SECONDS_PER_MINUTE)) == 0) System.out.println(this);

currentTime += TICK; } }

Page 20: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

MarketModel – (cont..)

public String toString() { String out = "MarketModel" + nl; out += "express lanes: " + express + nl; out += "regular lanes: " + regular + nl; return out; }

public static void main(String[] args) throws java.io.IOException { MarketModel mm = new MarketModel(SECONDS_PER_MINUTE * MINUTES_PER_HOUR); mm.runSimulation(); }}

Page 21: ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

The End