function DemandOut();

global Demands;
global devicenum Num quant
global NextOutTime NextInTime Devices finishednum refusednum Queue
global curtime

curtime = min(NextOutTime);

%finishednum = finishednum + 1;

for i =1:devicenum
    if NextOutTime(i) == min(NextOutTime)
        point = i;
    end;
end;

Time;

if Demands(4,Devices(point)) ~= 0 
        for i=1:length(Queue)
        if Queue(i) == 0
            Queue(i) = Devices(point);
            Queue(i+1) = 0;
            break;
        end;
        end;
else
    finishednum = finishednum + 1;
end;
    
    Devices(point) = 0;
    NextOutTime(point) = inf;
    
if Queue(1) ~= 0 
    
    Devices(point) = Queue(1);
    
    if Demands(4,Devices(point)) < quant 
        NextOutTime(point) = curtime + Demands(4,Devices(point));
        Demands(4,Devices(point)) = 0;
    else
        NextOutTime(point) = curtime + quant;
        Demands(4,Devices(point)) = Demands(4,Devices(point)) - quant;
    end;
        
    for i = 1:length(Queue)-1
        Queue(i) = Queue (i+1);
    end;
    
    Queue(length(Queue)) = 0;

end;
