/**
* Queue2.java v1.0 - Single Server Queue Simulation using Lindley's 
* Formula
*
*       Copyright (c) 2009-, Dr Adrian Muscat. All rights reserved.
*
*       This code can be used for academic and non-commerial research
*       and academic activities only.
*
* Purpose:
* 	Outputs the delay averaged over N runs
****/

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

class qq2 {

    private static int d;
    private static Random myRandom = new Random();

    public static double GetUniArrival(double a) {
        double x;
        d=6;
        x=myRandom.nextDouble()*a;
        return x;
    }

    public static double GetUniService(double a) {
        double x;
        x=myRandom.nextDouble()*a;
        return x;
    }

    public static void main(String[] args){

       System.out.println("I'm a Simple Program to Simulate a Single-Server Queue");

       NumberFormat myIFormat = new DecimalFormat("0000");
       NumberFormat myDFormat = new DecimalFormat("00000.00");

       int J=1000,N=20;
       double[][] DelayT;
       DelayT=new double[J][N];

       double ArrTime=0.0;
       double DelayTime=0.0;
       double ServiceTime=GetUniService(1.4);
       double DepartTime=ArrTime+DelayTime+ServiceTime;
       double NextArrTime;
       int JobNo=0;


      
       System.out.println("JobNo   ArrTime    DelayTime   ServiceTime   DepartTime");
       System.out.println(myIFormat.format(JobNo)
                    + " ,  "+myDFormat.format(ArrTime)
                    +" , "+myDFormat.format(DelayTime)
                    +" ,  "+myDFormat.format(ServiceTime)
                    + "  ,   " +myDFormat.format(DepartTime));

       for (int n=0; n<N; n++){
           for (JobNo=0; JobNo<J; JobNo++){
                NextArrTime=ArrTime+GetUniArrival(2);
                if ((NextArrTime)<DepartTime){
                    DelayTime=DepartTime-NextArrTime;
                }
                else{
                    DelayTime=0;
                }
                ServiceTime=GetUniService(1.4);
                ArrTime=NextArrTime;
                DepartTime=ArrTime+DelayTime+ServiceTime;
                System.out.println(myIFormat.format(JobNo)
                        + " ,  "+myDFormat.format(ArrTime)
                        +" , "+myDFormat.format(DelayTime)
                        +" ,  "+myDFormat.format(ServiceTime)
                        + "  ,   " +myDFormat.format(DepartTime));
           DelayT[JobNo][n]=DelayTime;
           }

       }//for n


       try {
           FileOutputStream fout = new FileOutputStream("test.csv");
           PrintStream myOutput = new PrintStream(fout);

           for(int m=0; m<J; m++){
               for(int n=0; n<N; n++){
                    myOutput.print(myDFormat.format(DelayT[m][n])+",");
               }
               myOutput.println();
           }
       }
       catch (IOException e)
       {
                System.out.println("Error: " + e);
                System.exit(1);
       }

    } //method main

    
} //class virtu
