function DemandIn();

global Demands;
global Num devicenum quant capacity
global NextOutTime NextInTime Devices inputnum refusednum Queue
global curtime queuenum

curtime=NextInTime;

inputnum=inputnum+1;

Time;

if inputnum>=Num
    AddDemand; 
end;
NextInTime = curtime + Demands(2,inputnum+1);

if min(Devices) == 0
    
    for i=1:devicenum
        if Devices(i) == 0
            point = i;
        end;
    end;
    
    Devices(point) = inputnum;
    
    if Demands(4,inputnum) < quant 
        NextOutTime(point) = curtime + Demands(4,inputnum);
        Demands(4,inputnum) = 0;
    else
        NextOutTime(point) = curtime + quant;
        Demands(4,inputnum) = Demands(4,inputnum) - quant;
    end;
       
else
    
    for i=1:length(Queue)
        if Queue(i) == 0
            queuepoint = i;
            break;
        end;
    end;
    
    if queuepoint-1 < capacity
        queuenum=queuenum+1;
                Queue(queuepoint) = inputnum;
                Queue(queuepoint+1) = 0;
    
    else
        refusednum = refusednum+1;
    end;
end;
