From ddd143b525f11c23e1a9be3ab75c88fc248d8382 Mon Sep 17 00:00:00 2001 From: Zach Date: Thu, 30 Sep 2021 20:54:35 +1000 Subject: [PATCH] ShortestTimeRemaining? --- Code/P2.java | 68 +++++++++++++++++++++++++++++++++++++ Code/P2_Jobs.java | 54 +++++++++++++++++++++++++++++ Code/P2_Printer.java | 73 ++++++++++++++++++++++++++++++++++++++++ Code/P2_PrinterHead.java | 62 ++++++++++++++++++++++++++++++++++ 4 files changed, 257 insertions(+) create mode 100644 Code/P2_Jobs.java create mode 100644 Code/P2_Printer.java create mode 100644 Code/P2_PrinterHead.java diff --git a/Code/P2.java b/Code/P2.java index e69de29..b090f28 100644 --- a/Code/P2.java +++ b/Code/P2.java @@ -0,0 +1,68 @@ +import java.util.*; +import java.io.*; +public class P2 { + /** + * COMP2240 Assignment 2 Part 2 + * Date: + * Author: Zach S-B + * Student Number: c3262201 + */ + + private int numOfJobs = 0; + private LinkedList jobList = new LinkedList(); + private P2_Printer printer = new P2_Printer();; + + /** + * Main. Runs the program + */ + public static void main(String[] args){ + P2 system = new P2(); + system.run(); + } + + public void run() + { + //Read file inputs and save to a job list. + readFile("../P2-1in.txt"); + + //Start the jobs going + for(int i = 0; i p2.getTimeRemaining()) + { + shortestTimeRemaining = p2.getTimeRemaining(); + head = 2; + } + + if(shortestTimeRemaining > p3.getTimeRemaining()) + { + head = 3; + } + + return head; + } + + public boolean requestPrint(P2_Jobs jobInput, int headInput) + { + //Check if job can be done currently + if(jobInput.getJobType()==printerMode) //If the job matches the current printer mode + { + //If there is an available head + if(headInput==1) + { + //Send to a head to be printed and count the time + int startTime = p1.print(jobInput, time); + System.out.println("("+startTime+") "+jobInput.getId()+" uses head 1 (time: "+jobInput.getPages()+")"); + return true; + } + else if(headInput==2) + { + int startTime = p2.print(jobInput, time); + System.out.println("("+startTime+") "+jobInput.getId()+" uses head 2 (time: "+jobInput.getPages()+")"); + return true; + } + else if(headInput==3) + { + int startTime = p3.print(jobInput, time); + System.out.println("("+startTime+") "+jobInput.getId()+" uses head 3 (time: "+jobInput.getPages()+")"); + return true; + } + else + { + return false; + } + } + else + { + //Keep waiting and asking + return false; + } + + } + +} \ No newline at end of file diff --git a/Code/P2_PrinterHead.java b/Code/P2_PrinterHead.java new file mode 100644 index 0000000..4c44eed --- /dev/null +++ b/Code/P2_PrinterHead.java @@ -0,0 +1,62 @@ +public class P2_PrinterHead{ + + private char mode = 'M'; + private boolean inUse=false; + private String jobId; + private int timeRemaining = 0; + + public P2_PrinterHead() + { + //Nothing to set up. + } + + public synchronized int print(P2_Jobs jobInput, int[] time) + { + //Set the details of the current job + jobId = jobInput.getId(); + mode = jobInput.getJobType(); + inUse = true; + timeRemaining = jobInput.getPages(); + int startTime = time[0]; + + //Wait (and delay the thread) one second per page of the job + for(int i = 0; i