package simulator;

import java.io.*;
import java.util.Random;

public class Utils {
    public static Random random = new Random();

    public static int random(int min, int max) {
        return min + random.nextInt(max - min + 1);
    }

    public static String readFile(String fileName) {
        String str = "";
        try {
            File file = new File(fileName);
            FileInputStream fis = new FileInputStream(file);
            byte[] data = new byte[(int) file.length()];
            if (fis.read(data) == -1) {
                System.out.printf("Failed to read %s\n", file);
                System.exit(-1);
            }
            str = new String(data, "UTF-8");
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        return str;
    }

    public static void writeFile(String fileName, String s) {

        try {
            // Assume default encoding.
            FileWriter fileWriter = new FileWriter(fileName);

            // Always wrap FileWriter in BufferedWriter.
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

            bufferedWriter.write(s);
            bufferedWriter.close();

            System.out.printf("Saved %s (%d characters)\n", fileName, s.length());
        } catch (IOException ex) {
            System.out.println("Error writing to file '" + fileName + "'");
        }

    }

}
