package scheduler;

import os.OperatingSystem;
import readinstruction.ReadInstruction;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by Karol Pokomeda on 2017-04-21.
 */
public class ScanScheduler implements Scheduler {
    private LinkedList<ReadInstruction> waitingQueue;
    private OperatingSystem parentOs;
    private int headPosition;

    public ScanScheduler(){
        this.waitingQueue = new LinkedList<>();
        this.headPosition = 0;
    }

    @Override
    public void update(int time) {

    }

    @Override
    public void setOs(OperatingSystem parentOs) {
        this.parentOs = parentOs;
    }

    @Override
    public void push(ReadInstruction instruction) {
        if (this.isEmpty()) this.waitingQueue.add(instruction);
        else {
            int i = 0;
            while (instruction.getReadAddress() < this.waitingQueue.get(i).getReadAddress()) i++;
            this.waitingQueue.add(i, instruction);
        }
    }

    @Override
    public String getName() {
        return "SCAN";
    }

    @Override
    public boolean isEmpty() {
        return this.waitingQueue.isEmpty();
    }
}
@@ -358,16 +358,42 @@
 osition;
+%0A    private int discSize;
 %0A%0A    pu
@@ -411,16 +411,28 @@
 heduler(
+int discSize
 )%7B%0A     
@@ -502,17 +502,52 @@
 ition = 
-0
+-1;%0A        this.discSize = discSize
 ;%0A    %7D%0A
@@ -596,16 +596,471 @@
 time) %7B%0A
+        this.headPosition++;%0A        if (this.headPosition %3E= this.discSize) this.headPosition = 0;%0A        int i = 0;%0A        ReadInstruction result;%0A        while (!this.isEmpty() && (result = this.waitingQueue.get(i)).getReadAddress() %3E this.headPosition)%7B%0A            if(result.getReadAddress() == this.headPosition)%7B%0A                result.read(time);%0A                this.parentOs.push(this.waitingQueue.remove(i));%0A            %7D else i++;%0A        %7D
 %0A    %7D%0A%0A

Implement update method in ScanScheduler