Download presentation
Presentation is loading. Please wait.
Published byLorin Barker Modified over 8 years ago
1
Sit-in Lab 4 Orders Topic: Stack and Queue
2
Overview Simulate an order management system that: Can take multiple orders and processing them according to their time of arrival New order must be assigned to the lowest-numbered available processor If no processor available, the order will wait for the next readily available processor For each order, print its order number, the number of assigned processor, and the time that the order is processed 2
3
Input First line: Two integers K (number of processors) and O (number of orders) O rows follow: T (time of arrival of the order), X (time needed to process that order) 3
4
Main Method Read K (number of processors) and O (number of orders) Initialize the processors For each order: Read order information Process the order accordingly 4
5
Main Method public void run() { Scanner sc = new Scanner(System.in); int K = sc.nextInt(); // number of processors int O = sc.nextInt(); // number of orders initProcessors(K); // initialize the processors for (int i = 1; i <= O; i++){ int arrTime = sc.nextInt(); // arrival time of the order int timeNeeded = sc.nextInt(); // time needed to process processOrder(i, arrTime, timeNeeded); } 5
6
Class Processor 6 Processor -number: int -finishTime: int + getNumber(): int + setFinishTime(): void + getFinishTime(): int Note: Finish time of a processor is the finish time of the last order assigned to it. The default value of finish time is -1 (when the processor is not assigned to any order)
7
Choosing better processor to assigned // return true if processor x is better to be assigned // than processor y for the specific order arrival time private boolean isBetter(Processor x, Processor y, int orderArrTime){ // If the last order in the processor finish before orderArrTime, // the ready time is orderArrTime. Otherwise, the ready time is // the finish time of the last order int xReady = (x.getFinishTime() < orderArrTime) ? orderArrTime : x.getFinishTime(); int yReady = (y.getFinishTime() < orderArrTime) ? orderArrTime : y.getFinishTime(); // return true only when processor x has earlier ready time // or has the same ready time with y but smaller number return (xReady < yReady) || (xReady == yReady && x.getNumber() < y.getNumber()); } 7
8
Processing orders For each order Loop through the processors list to find the best processor to be assigned Update the assigned processor in the list Print the order number, the number of assigned processor, and the time that the order is processed 8
9
Processors list What data structure can we use to store the list? Array, ArrayList, LinkedList? (ok but not recommended) Stack? Queue? 9
10
Process order – Using stack private Stack processors; private void initProcessors(int K){ processors = new Stack (); for (int i = 1; i <= K; i++){ // push new processor with specific number // and default finish time value processors.push(new Processor(i, -1)); } 10
11
Process order – Using stack private void processOrder(int orderNum, int arrTime, int timeNeeded){ Stack temp = new Stack (); // find the best processor to be assigned //... // compute the finish time of the order with the assigned // processor int readyTime = (best.getFinishTime() < arrTime) ? arrTime : best.getFinishTime(); int finishTime = readyTime + timeNeeded; // Push elements back to the original stack and update // the assigned processor //... // print the result System.out.println(orderNum + " " + best.getNumber() + " “ + finishTime); } 11
12
Process order – Using stack processors P1 (-1)P2 (-1) temp 12
13
Order 1: arrTime = 0, timeNeeded = 9 processors P1 (-1)P2 (-1) temp 13 Best = … Loop through all elements in the list to find the best processor to be assigned to the order
14
Order 1: arrTime = 0, timeNeeded = 9 processors P1 (-1) temp P2(-1) 14 Best = P2
15
Order 1: arrTime = 0, timeNeeded = 9 processors temp P2(-1)P1(-1) 15 Best = P1 readyTime = 0, finishTime = 0 + 9 = 9 P1.setFinishTime(9)
16
Order 1: arrTime = 0, timeNeeded = 9 processors P1(9) temp P2(-1) 16 Best = P1 Push elements back to the original stack and update the assigned processor
17
Order 1: arrTime = 0, timeNeeded = 9 processors P1(9)P2(-1) temp 17 Best = P1 Print “1 1 9”
18
Order 2: arrTime = 5, timeNeeded = 5 processors P1(9)P2(-1) temp 18 Best = … Loop through all elements in the list to find the best processor to be assigned to the order
19
Order 2: arrTime = 5, timeNeeded = 5 processors P1(9) temp P2(-1) 19 Best = P2
20
Order 2: arrTime = 5, timeNeeded = 5 processors temp P2(-1)P1(9) 20 Best = P2 readyTime = 5, finishTime = 5 +5 = 10 P2.setFinishTime(10)
21
Order 2: arrTime = 5, timeNeeded = 5 processors P1(9) temp P2(-1) 21 Best = P2 Push elements back to the original stack and update the assigned processor
22
Order 2: arrTime = 5, timeNeeded = 5 processors P1(9)P2(10) temp 22 Best = P2 Print “2 2 10”
23
Order 3: arrTime = 10, timeNeeded = 1 processors P1(9)P2(10) temp 23 Best = … Loop through all elements in the list to find the best processor to be assigned to the order
24
Order 3: arrTime = 10, timeNeeded = 1 processors P1(9) temp P2(10) 24 Best = P1
25
Order 3: arrTime = 10, timeNeeded = 1 processors temp P2(10)P1(9) 25 Best = P1 readyTime = 10, finishTime = 10 + 1 = 11 P1.setFinishTime(11)
26
Order 3: arrTime = 10, timeNeeded = 1 processors P1(11) temp P2(10) 26 Best = P1 Push elements back to the original stack and update the assigned processor
27
Order 3: arrTime = 10, timeNeeded = 1 processors P1(11)P2(10) temp 27 Best = P1 Print “3 1 11”
28
Process order – Using Queue Similar to Stack Using offer() and poll() instead of push() and pop() 28
29
END OF FILE 29
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.