ITI 1221 Introduction to Computing II Lab8 Dewan
ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa
Today’s Objective q Case Study o Simulating a Supermarket Check out
Supermarket Checkout – General Idea (cont. . ) For simplicity, Let’s consider only one cashier, During Program we’ll handle it. Market. Model Creates Generates Serves Cashier Uses Customer Contains Queue
Supermarket Checkout – General Idea Input Range of Values Total minute (length of Simulation) 0 < total < 1000 Average Time Spent per Customer 0 < 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
Supermarket Checkout q Two types of Cashiers q Express ( if number of items are less 13 ) q Regular q Each cashier has an queue for the customers q A new customer joins to a queue having minimum number of customers for both cases (express and regular cashiers).
Customer q Each customer has o Arrival time o Initial number of items o Remaining number of items for serve q Constructor to initialize the instance variables o public Customer(int arrival. Time, int number. Of. Items) q Methods o Access methods Ø arrival time and Ø number of remaining items o Current status of the Served (on going) customer Ø int number. Of. Served. Items() Ø number of items served so far o Update remaining items of a customer Ø void serve()
Customer – (cont. . ) public class Customer { private int arrival. Time; private int number. Of. Items; private int initialnumber. Of. Items; // constructor public Customer(int arrival. Time, int number. Of. Items) { this. arrival. Time = arrival. Time; this. number. Of. Items = number. Of. Items; this. initialnumber. Of. Items = number. Of. Items; } public int number. Of. Served. Items() { return this. initialnumber. Of. Items - this. number. Of. Items; } public void serve() {number. Of. Items--; } // access methods public int arrival. Time() { return arrival. Time; } public int number. Of. Items() { return number. Of. Items; } }
Queue public interface Queue { public void enqueue(Object obj); public Object dequeue(); public boolean is. Empty(); public int size(); }
Queue - Array. Queue public class Array. Queue implements Queue { private static final int MAX_QUEUE_SIZE = 10000; private Object[] q; private int front, rear, size; public Array. Queue() { q = new Object[MAX_QUEUE_SIZE]; front = 0; rear = -1; size = 0; } public int size() { return size; } public boolean is. Empty() { return size == 0; } public boolean is. Full() { return size == MAX_QUEUE_SIZE; }
Queue – Array. Queue (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 saved. Value = q[front]; q[front] = null; front++; size--; return saved. Value; } } // end of the class
Cashier q Each cashier has o o o Total waiting time of all customers Total number of served items Current customer A queue to store waiting customers q Constructor o o Initialize - instance variables You need a Queue Data Structure q Methods & Access methods o o o Get total waiting time Get total served items Get total served customers Add a new customer to the cashier queue Get Number of customers in the queue Serve one customer at a time
Cashier – (cont. . ) public class Cashier { private int total. Customer. Wait. Time, customers. Served, total. Items. Served; private Customer current. Customer; private Queue queue; // constructor public Cashier(){ total. Customer. Wait. Time = 0; customers. Served = 0; current. Customer = null; total. Items. Served = 0; queue = new Array. Queue(); } // access methods public int get. Total. Customer. Wait. Time() { return this. total. Customer. Wait. Time; public int get. Total. Items. Served() { if(this. current. Customer != null) return total. Items. Served + this. current. Customer. number. Of. Served. Items(); else return total. Items. Served; } public int get. Total. Customers. Served() { return customers. Served; } public void add. Customer(Customer c){ queue. enqueue(c); } public int length. Of. Queue() { return queue. size(); } }
Cashier – (cont. . ) public void serve. Customers(int current. Time){ if (current. Customer == null){ // no customers yet!!! if (queue. is. Empty()) return; else{ // dequeue first waiting customer and tally results current. Customer = (Customer) queue. dequeue(); total. Customer. Wait. Time = total. Customer. Wait. Time + current. Time - current. Customer. arrival. Time(); customers. Served++; } } // give a unit of service current. Customer. serve(); // if current customer is finished, send it away if (current. Customer. number. Of. Items() == 0) { total. Items. Served += current. Customer. number. Of. Served. Items(); current. Customer = null; } } } // end of the class
Cashiers q Cashiers has o An array of cashier q Constructor o Initialized the size of the cashiers array q Methods o Adding a new customer Ø Where size of the queue is minimum o Serving customers at a particular instant of time o to. String() Ø Ø Ø Ø 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
Cashiers – (cont. . ) import java. text. Number. Format; public class Cashiers { private Cashier[] cashiers; private static final String nl = System. get. Property("line. separator"); public Cashiers(int n) { if (n < 1) throw new Illegal. Argument. Exception(); cashiers = new Cashier[n]; for (int i=0; i<n; i++) cashiers[i] = new Cashier(); } public void add. Customer(Customer c) { int i. Max, v. Max; i. Max = 0; for (int i=1; i<cashiers. length; i++) if (cashiers[i]. length. Of. Queue() < cashiers[i. Max]. length. Of. Queue()) i. Max = i; cashiers[i. Max]. add. Customer(c); }
Cashiers – (cont. . ) public void serve. Customers(int current. Time) { for (int i=0; i<cashiers. length; i++) cashiers[i]. serve. Customers(current. Time); } public String to. String() { int nb. Customers = 0; int nb. Items = 0; int waiting. Time = 0; int nb. Served. Customers = 0; String out = "Tally: " + nl; for (int i=0; i<cashiers. length; i++) { nb. Customers += cashiers[i]. length. Of. Queue(); nb. Items += cashiers[i]. get. Total. Items. Served(); waiting. Time += cashiers[i]. get. Total. Customer. Wait. Time(); nb. Served. Customers += cashiers[i]. get. Total. Customers. Served(); } double ave. Length = (double) nb. Customers / (double) cashiers. length; double ave. Items. Customer = (double) nb. Items/ (double) nb. Served. Customers; double ave. Items. Cashier = (double) nb. Items/ (double) cashiers. length; double ave. Waiting. Time. Cus = (double) waiting. Time / (double) nb. Served. Customers; double ave. Served. Customers. Cashier = (double) nb. Served. Customers/ (double) cashiers. length;
Cashiers – (cont. . ) Number. Format nf = Number. Format. get. Number. Instance(); out += "Average number of customers currently waiting: " + nf. format(ave. Length) +nl; out += "Average number of items per customer: " + nf. format(ave. Items. Customer)+nl; out += "Average waiting time per customer: " + nf. format(ave. Waiting. Time. Cus)+nl; out += "Average number of customers served per cashier: " + nf. format(ave. Served. Customers. Cashier)+nl; out += "Average number of items Handled per cashier: " + nf. format(ave. Items. Cashier)+nl; out += "Total number of clients served: " + nf. format(nb. Served. Customers)+nl; out += "Total number of items handled: " + nf. format(nb. Items)+nl; return out; } }
Market. Model public class Market. Model { 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. get. Property("line. separator"); private static final double probability. Of. New. Arrival = 0. 5; private Cashiers express; private Cashiers regular; private int length. Of. Simulation, ave. Nb. Items; public Market. Model(int duration) { this. length. Of. Simulation = duration; express = new Cashiers(3); regular = new Cashiers(10); }
Market. Model – (cont. . ) public void run. Simulation() { int current. Time = 0; while (current. Time < length. Of. Simulation) { if (Math. random() <= probability. Of. New. Arrival) { Customer customer = new Customer(current. Time, (int) (50 * Math. random()) + 1); if (customer. number. Of. Items() <= 12) express. add. Customer(customer); else regular. add. Customer(customer); } express. serve. Customers(current. Time); regular. serve. Customers(current. Time); if ((current. Time % (5 * SECONDS_PER_MINUTE)) == 0) System. out. println(this); current. Time += TICK; } }
Market. Model – (cont. . ) public String to. String() { String out = "Market. Model" + nl; out += "express lanes: " + express + nl; out += "regular lanes: " + regular + nl; return out; } public static void main(String[] args) throws java. io. IOException { Market. Model mm = new Market. Model(SECONDS_PER_MINUTE * MINUTES_PER_HOUR); mm. run. Simulation(); } }
The End
- Slides: 21