ShortestTimeRemaining?
This commit is contained in:
parent
d3658b16e7
commit
ddd143b525
68
Code/P2.java
68
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<P2_Jobs> jobList = new LinkedList<P2_Jobs>();
|
||||||
|
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<jobList.size(); i++)
|
||||||
|
{
|
||||||
|
Thread job = new Thread(jobList.get(i));
|
||||||
|
job.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* readFile. Reads the file given, creating processes as needed to save details.
|
||||||
|
* @param fileNameInput address of the file to be read in.
|
||||||
|
*/
|
||||||
|
public void readFile(String input)
|
||||||
|
{
|
||||||
|
Scanner fileReader; //Scanner to read the file.
|
||||||
|
File dataInput;// = new File(fileNameInput);
|
||||||
|
|
||||||
|
dataInput = new File(input);
|
||||||
|
|
||||||
|
//Try and open file. If it works, procede.
|
||||||
|
try {
|
||||||
|
fileReader = new Scanner(dataInput);
|
||||||
|
|
||||||
|
String line = fileReader.nextLine();
|
||||||
|
|
||||||
|
numOfJobs = line.charAt(0) - '0';
|
||||||
|
for(int i = 0; i< numOfJobs; i++)
|
||||||
|
{
|
||||||
|
line = fileReader.nextLine();
|
||||||
|
jobList.add(new P2_Jobs(line, printer));
|
||||||
|
}
|
||||||
|
fileReader.close();
|
||||||
|
}
|
||||||
|
//If the file isn't found, throw a hissy fit.
|
||||||
|
catch (FileNotFoundException e) {
|
||||||
|
System.out.println("File not found.");
|
||||||
|
System.exit(0);
|
||||||
|
//e.printStackTrace(); //Optional, shows more data.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
54
Code/P2_Jobs.java
Normal file
54
Code/P2_Jobs.java
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
public class P2_Jobs implements Runnable{
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private char jobType;
|
||||||
|
private int pages = 0;
|
||||||
|
private P2_Printer printer;
|
||||||
|
private boolean done = false;
|
||||||
|
|
||||||
|
public P2_Jobs(String jobDetails, P2_Printer printerToUse)
|
||||||
|
{
|
||||||
|
//Create a new job
|
||||||
|
id = jobDetails.substring(0, 2);
|
||||||
|
jobType = jobDetails.charAt(0);
|
||||||
|
pages = Integer.parseInt(jobDetails.substring(3));
|
||||||
|
|
||||||
|
//Assing to printer
|
||||||
|
printer = printerToUse;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
int head=0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
//While the job hasn't been started, keep nagging
|
||||||
|
head = printer.requestHead();
|
||||||
|
}while(!printer.requestPrint(this,head));
|
||||||
|
|
||||||
|
//Once we're out of that while loop, we're done.
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getIsDone()
|
||||||
|
{
|
||||||
|
return done;
|
||||||
|
}
|
||||||
|
|
||||||
|
public char getJobType()
|
||||||
|
{
|
||||||
|
return jobType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPages()
|
||||||
|
{
|
||||||
|
return pages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId()
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
73
Code/P2_Printer.java
Normal file
73
Code/P2_Printer.java
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
public class P2_Printer{
|
||||||
|
|
||||||
|
private int[] time = {0}; //Really hacky solution, but seems easier than wrapping in a custom object.
|
||||||
|
private char printerMode = 'M';
|
||||||
|
|
||||||
|
private P2_PrinterHead p1 = new P2_PrinterHead();
|
||||||
|
private P2_PrinterHead p2 = new P2_PrinterHead();
|
||||||
|
private P2_PrinterHead p3 = new P2_PrinterHead();
|
||||||
|
|
||||||
|
|
||||||
|
public P2_Printer()
|
||||||
|
{
|
||||||
|
//Nothing to set up.
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized int requestHead()
|
||||||
|
{
|
||||||
|
int head = 1;
|
||||||
|
int shortestTimeRemaining = p1.getTimeRemaining();
|
||||||
|
|
||||||
|
if(shortestTimeRemaining > 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
62
Code/P2_PrinterHead.java
Normal file
62
Code/P2_PrinterHead.java
Normal file
@ -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<jobInput.getPages(); i++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Thread.sleep(1000);
|
||||||
|
timeRemaining--;
|
||||||
|
time[0]++;
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
//Don't actually care
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Head is now available for another activity
|
||||||
|
inUse = false;
|
||||||
|
return startTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Getters
|
||||||
|
public String getJobId()
|
||||||
|
{
|
||||||
|
return jobId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getInUse()
|
||||||
|
{
|
||||||
|
return inUse;
|
||||||
|
}
|
||||||
|
|
||||||
|
public char getMode()
|
||||||
|
{
|
||||||
|
return mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTimeRemaining()
|
||||||
|
{
|
||||||
|
return timeRemaining;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user