UG.java 8.15 KB
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class UG {
    String external;
    String internal;
    int[] adj;
    float area;
    int[] presc;
    int[][] periods;
    int[] wood;
    int[] cork;
    float mediumX;
    float mediumY;
    int time;
    boolean valid;

    String region;
    String[] externalPresc;


    static public int[] returnInsertable(Scanner myReader){
        String data = myReader.nextLine();
        String[] str_split = data.split(",", 0);
        int size = str_split.length;
        int[] toInsert = new int [size];
        for(int i=0; i<size; i++) {
            toInsert[i] = Integer.parseInt(str_split[i]);
        }
        return toInsert;
    }

    static public int[] returnInsertable2(Scanner myReader){
        String data = myReader.nextLine();
        String[] str_split = data.split(",", 0);
        int size = str_split.length;
        int[] toInsert = new int[size];
        for(int i=0; i<size; i++) {
            toInsert[i] = (int)Float.parseFloat(str_split[i]);
        }
        return toInsert;
    }

    static public int[] returnPeriods(String data){
        String[] str_split = data.split(",", 0);
        int size = str_split.length;
        int[] toInsert = new int [size];
        for(int i=0; i<size; i++) {
            toInsert[i] = Integer.parseInt(str_split[i]);
        }
        return toInsert;
    }




    static public void fillArray(UG[] array, String dir, float X, float Y, int maxDistance) {
        int index = 0;

        try {                       // idea: pass a directory, which should contain files called
            // graph_init, ugs_init, ...
            File myObj = new File(dir + "/graph_init");
            Scanner myReaderAdj = new Scanner(myObj);

            File myObj2 = new File(dir + "/ugs_init");
            Scanner myReaderPresc = new Scanner(myObj2);

            File myObj3 = new File(dir + "/area_init");
            Scanner myReaderArea = new Scanner(myObj3);

            File myObj4 = new File(dir + "/period_init");
            Scanner myReaderPeriod = new Scanner(myObj4);

            File myObj5 = new File(dir + "/external_init");
            Scanner myReaderExternal = new Scanner(myObj5);

            File myObj6 = new File(dir + "/xvertex_init");
            Scanner myReaderXVertex = new Scanner(myObj6);

            File myObj7 = new File(dir + "/yvertex_init");
            Scanner myReaderYVertex = new Scanner(myObj7);

            File myObj8 = new File(dir + "/wood_init");
            Scanner myReaderWood = new Scanner(myObj8);

            File myObj9 = new File(dir + "/cork_init");
            Scanner myReaderCork = new Scanner(myObj9);

            File myObj10 = new File(dir + "/internal_init");
            Scanner myReaderInternal = new Scanner(myObj10);

            File myObj11 = new File(dir + "/region_init");
            Scanner myReaderRegion = new Scanner(myObj11);


            while (myReaderAdj.hasNextLine()) {
                UG toInsert = new UG();


                toInsert.mediumX = Float.parseFloat(myReaderXVertex.nextLine());
                toInsert.mediumY = Float.parseFloat(myReaderYVertex.nextLine());
                double d = Math.sqrt(Math.pow (toInsert.mediumX - X, 2) + Math.pow (toInsert.mediumY - Y, 2));
                toInsert.region = myReaderRegion.nextLine();
                if(d <= maxDistance){
                    toInsert.valid = true;
                }
                else{
                    toInsert.valid = false;
                }


                toInsert.adj = returnInsertable(myReaderAdj);
                toInsert.presc = returnInsertable(myReaderPresc);
                toInsert.external = myReaderExternal.nextLine();
                toInsert.internal = myReaderInternal.nextLine();
                toInsert.area = Float.parseFloat(myReaderArea.nextLine());

                String data = myReaderPeriod.nextLine();
                String[] str_split = data.split("/", 0);
                int size = str_split.length;
                toInsert.periods = new int[size][];


                for(int i = 0; i < size; i++) {
                    toInsert.periods[i] = returnPeriods(str_split[i]);
                }

                toInsert.wood = returnInsertable2(myReaderWood);
                toInsert.cork = returnInsertable2(myReaderCork);

                array[index] = toInsert;
                index++;
            }

            myReaderAdj.close();
            myReaderPresc.close();
            myReaderArea.close();
            myReaderExternal.close();
            myReaderXVertex.close();
            myReaderYVertex.close();
            myReaderWood.close();
            myReaderCork.close();
            myReaderRegion.close();

        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }



    static public void fillArray(UG[] array, String dir, String region) {
        int index = 0;

        try {                       // idea: pass a directory, which should contain files called
            // graph_init, ugs_init, ...
            File myObj = new File(dir + "/graph_init");
            Scanner myReaderAdj = new Scanner(myObj);

            File myObj2 = new File(dir + "/ugs_init");
            Scanner myReaderPresc = new Scanner(myObj2);

            File myObj3 = new File(dir + "/area_init");
            Scanner myReaderArea = new Scanner(myObj3);

            File myObj4 = new File(dir + "/period_init");
            Scanner myReaderPeriod = new Scanner(myObj4);

            File myObj5 = new File(dir + "/external_init");
            Scanner myReaderExternal = new Scanner(myObj5);

            File myObj6 = new File(dir + "/xvertex_init");
            Scanner myReaderXVertex = new Scanner(myObj6);

            File myObj7 = new File(dir + "/yvertex_init");
            Scanner myReaderYVertex = new Scanner(myObj7);

            File myObj8 = new File(dir + "/wood_init");
            Scanner myReaderWood = new Scanner(myObj8);

            File myObj9 = new File(dir + "/cork_init");
            Scanner myReaderCork = new Scanner(myObj9);

            File myObj10 = new File(dir + "/internal_init");
            Scanner myReaderInternal = new Scanner(myObj10);

            File myObj11 = new File(dir + "/region_init");
            Scanner myReaderRegion = new Scanner(myObj11);


            while (myReaderAdj.hasNextLine()) {
                UG toInsert = new UG();


                toInsert.mediumX = Float.parseFloat(myReaderXVertex.nextLine());
                toInsert.mediumY = Float.parseFloat(myReaderYVertex.nextLine());

                toInsert.region = myReaderRegion.nextLine();
                if(!region.equalsIgnoreCase(toInsert.region)){
                    toInsert.valid = true;
                }
                else{
                    toInsert.valid = false;
                }


                toInsert.adj = returnInsertable(myReaderAdj);
                toInsert.presc = returnInsertable(myReaderPresc);
                toInsert.external = myReaderExternal.nextLine();
                toInsert.internal = myReaderInternal.nextLine();
                toInsert.area = Float.parseFloat(myReaderArea.nextLine());

                String data = myReaderPeriod.nextLine();
                String[] str_split = data.split("/", 0);
                int size = str_split.length;
                toInsert.periods = new int[size][];


                for(int i = 0; i < size; i++) {
                    toInsert.periods[i] = returnPeriods(str_split[i]);
                }

                toInsert.wood = returnInsertable2(myReaderWood);
                toInsert.cork = returnInsertable2(myReaderCork);

                array[index] = toInsert;
                index++;
            }

            myReaderAdj.close();
            myReaderPresc.close();
            myReaderArea.close();
            myReaderExternal.close();
            myReaderXVertex.close();
            myReaderYVertex.close();
            myReaderWood.close();
            myReaderCork.close();
            myReaderRegion.close();

        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }



}