/**
* Queue1.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 time of arrival, delay, service time
* 	and departure time for each job
*/

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

class qq1 {

    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");

       double ArrTime=0.0;
       double DelayTime=0.0;
       double ServiceTime=GetUniService(3);
       double DepartTime=ArrTime+DelayTime+ServiceTime;
       double NextArrTime;
       int JobNo=0;


       try {
       FileOutputStream fout = new FileOutputStream("test.out");
       PrintStream myOutput = new PrintStream(fout);

       
       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));

       while (ArrTime<100){
            JobNo++;
            NextArrTime=ArrTime+GetUniArrival(3.5);
            if ((NextArrTime)<DepartTime){
                DelayTime=DepartTime-NextArrTime;
            }
            else{
                DelayTime=0;
            }
            ServiceTime=GetUniService(3);
            ArrTime=NextArrTime;
            DepartTime=ArrTime+DelayTime+ServiceTime;
            System.out.println(myIFormat.format(JobNo)
                    + " ,  "+myDFormat.format(ArrTime)
                    +" , "+myDFormat.format(DelayTime)
                    +" ,  "+myDFormat.format(ServiceTime)
                    + "  ,   " +myDFormat.format(DepartTime));


            myOutput.println(myIFormat.format(JobNo)
                    + " ,  "+myDFormat.format(ArrTime)
                    +" , "+myDFormat.format(DelayTime)
                    +" ,  "+myDFormat.format(ServiceTime)
                    + "  ,   " +myDFormat.format(DepartTime));
            }
       }
            catch (IOException e)
            {
                System.out.println("Error: " + e);
                System.exit(1);
            }
       } //method main

    
} //class virtu
