Anonymous Anonymous - 1 month ago
275 0

Algdat oblig 1 hiof

Java

Algdat oblig 1 hiof

package no.hiof.dcnilsen.flyplassen;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    public static void main(String[] args)  throws IOException {
        //Konstanter:
        final int MAXQUEUE = 10; //Oppgtekst: f.eks. maks 10 fly i kø
        boolean debug = false; //Printer en masse ekstra greier.

        int timeUnits;
        double meanArrivals;
        double meanDepartures;

        //Variabler relatert til kjøringen
        if(!debug) {
            timeUnits = getTimeunits();
            meanArrivals = getMeanArrivals();
            meanDepartures = getMeanDepartures();
        }else{
            timeUnits = 20;
            meanArrivals = 0.5;
            meanDepartures = 0.5;
        }



        //Stats-variable:
        int statLanded = 0, statDeparted = 0, statRejected = 0, statLandRequest = 0, statTakeoffRequest = 0, statTotalRequest = 0, statTotalPlanes = 0, statEmpty = 0;
        double statPercentageEmpty, statAvgWaitTime, statQueueTime;

        //Initierer køer.
        Queue<Plane> arrQueue = new LinkedList<Plane>();
        Queue<Plane> depQueue = new LinkedList<Plane>();

        //Kontrollerer og tømmer evt innhold fra køene.
        if(!arrQueue.isEmpty()){arrQueue.clear();}
        if(!depQueue.isEmpty()){depQueue.clear();}

        //Simuleringen
        for(int i=0; i <= timeUnits; i++){
            if(debug){
            System.out.println("--------------------------------------------");
            System.out.println("timeUnit: " + i);}
            System.out.println("--------------------------------------------");
            int arrivals = getPoissonRandom(meanArrivals);
            int departures = getPoissonRandom(meanDepartures);

            //Oppdatere stats:
            statLandRequest += arrivals;
            statTakeoffRequest += departures;
            statTotalRequest += (departures+arrivals);

            if(debug){
            System.out.println("Poisson arr: " + arrivals);
            System.out.println("Poisson dep: " + departures);}

            //Sjekker om det er fly i lufta
            while (arrivals!=0) {
                if (arrQueue.size() <= MAXQUEUE) {
                    Plane plane = generatePlane();
                    arrQueue.add(plane);
                    arrivals--;
                    System.out.println("ID: " + plane.number + " - " + plane.name + " lagt til i landingskøen");
                } else{
                    statRejected++;
                    System.out.println("Inkommende fly avvist.");
                    arrivals--;
                }
            }

            //Sjekker om det er fly på bakken
            while(departures!=0) {
                if (depQueue.size() <= MAXQUEUE) {
                    Plane plane = generatePlane();
                    depQueue.add(plane);
                    departures--;
                    System.out.println("ID: " + plane.number + " - " + plane.name + " lagt til i avgangskøen");
                } else {
                    statRejected++;
                    System.out.println("Avreise avvist.");
                    departures--;
                }
            }

            //Dersom det er fly i landingskø, land og gå til neste tidsenhet.
            if(!arrQueue.isEmpty()){
                Plane plane = arrQueue.poll();
                System.out.println("ID: " + plane.number + " - " + plane.name + " har landet");
                statLanded++;
                continue;
            }

            //Dersom det er fly i kø på bakken så ta av og gå til neste tidsenhet.
            if(!depQueue.isEmpty()){
                Plane plane = depQueue.poll();
                System.out.println("ID: " + plane.number + " - " + plane.name + " har lettet");
                statDeparted++;
                continue;
            }

            if(depQueue.isEmpty() && arrQueue.isEmpty()){
                System.out.println("Alle køer tomme.");
                statEmpty++;
            }
        }

        System.out.println("--------------------------------------------");
        System.out.println("Stats:");
        System.out.println("Tidsenheter: \t\t\t" + timeUnits);
        System.out.println("Antall forespørsler: \t" + statTotalRequest);
        System.out.println("Antall fly avvist: \t\t" + statRejected);
        System.out.println("Antall fly landet: \t\t" + statLanded);
        System.out.println("Antall fly reist: \t\t" + statDeparted);
        System.out.println("Rullebane ledig: \t\t" + statEmpty);
        System.out.println();
        System.out.println("Antall fly i landngskø:\t" + arrQueue.size());
        System.out.println("Antall fly på bakken \t" + depQueue.size());
        System.out.println("");
        System.out.println("--------------------------------------------");

    }

    public static class Plane{
        String name;
        int number;
        static int planeIndex = 0;

        public Plane(String name) {
            this.name = name;
            this.number = planeIndex;
            planeIndex++;
        }

        public String getName() {
            return name;
        }

        public int getNumber() {
            return number;
        }

        @Override
        public String toString() {
            return name + " with ID: " + number;
        }
    }

    private static int getPoissonRandom(double mean){
        Random r = new Random();
        double L = Math.exp(-mean);
        int k = 0;
        double p = 1.0;
        do
        {
            p = p * r.nextDouble();
            k++;
        } while (p > L);
        return k - 1;
    }

    private static int getTimeunits(){
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int timeUnits=0;
        System.out.print("Hvor mange tidsenheter skal simuleringen kjøre?: ");
        try{
            timeUnits = Integer.parseInt(bufferedReader.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return timeUnits;
    }

    private static double getMeanArrivals(){
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        double meanArrivals = 0;
        System.out.print("Forventet antall ankomster pr. tidsenhet?: ");
        try{
            meanArrivals = Double.parseDouble(bufferedReader.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return meanArrivals;
    }

    private static double getMeanDepartures(){
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        double meanDepartures = 0;
        System.out.print("Forventet antall avganger pr. tidsenhet?: ");
        try{
            meanDepartures = Double.parseDouble(bufferedReader.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return meanDepartures;
    }

    private static String[] getPlaneNames() {
        String[] planeNames = {"Sea Duck", "Red Baron", "Air Force One", "Dusty Cropduster", "Ranger Plane", "Thunderquack", "Starscream", "Turbokat", "X Jet", "Batwing", "VF-1S Strike Valkyrie", "Enola Gay", "X-wing", "Starfighter x-15", "B52", "B17", "Spitfire", "Messerchmitt Bf-119", "Spirit of St.Louis", "Wright Flyer", "Sopwith Camel", "SR-71 Blackbird", "Blériot XI", "Cessna 172", "Bell X-1", "Rutan VariEze", "MQ-1 Predator", "Voyager", "Piper J-3 Cub", "Gossamer Albatross", "V-22 Osprey", "Ekranoplan", "Hindenburg", "P-791", "XF-85 Goblin", "Mosquito"};
        return planeNames;
    }

    private static Plane generatePlane(){
        String[] planeNames = getPlaneNames();
        Plane plane = new Plane(planeNames[new Random().nextInt(getPlaneNames().length)]);
        return plane;
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download