COMP2240-Assignment2/Code/P2_Printer.java
2021-09-30 20:54:35 +10:00

73 lines
2.1 KiB
Java

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;
}
}
}