import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

public class Stats {
    /** Stored numbers. */
    private ArrayList<Double> contents;

    /** Statistics fields. */
    private double min;   // minimum number
    private double max;   // maximum number
    private double sum;   // sum of all numbers
    private double mean;  // mean of all numbers

    /**
     * Run the program.
     *
     * @param args  the array of command-line arguments
     * @return      nothing
     */
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("usage: java Stats filename");
            System.exit(1);
        }
        String filename = args[0];
        Stats s = new Stats();
        try {
            s.load(filename);
            s.computeStats();
            s.printStats();
        } catch (FileNotFoundException e) {
            System.err.println("FILE NOT FOUND: " + e.getMessage());
            System.exit(1);
        } catch (NumberFormatException e) {
            System.err.println("BAD NUMBER FORMAT: " + e.getMessage());
            System.exit(1);
        }
    }

    /** Make a new Stats instance. */
    public Stats() {
        contents = new ArrayList<Double>();
    }

    /** Load numbers from a file into the object. */
    public void load(String filename) throws FileNotFoundException, NumberFormatException {
        Scanner sc = new Scanner(new File(filename));
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            double d = Double.parseDouble(line); 
            contents.add(d);
        }
        sc.close();
    }

    /** Dump the `contents` array to the terminal. */
    public void dump() {
        for (int i = 0; i < contents.size(); i++) {
            System.out.println(contents.get(i));
        }
    }

    /** Compute the statistics of the file. */
    public void computeStats() {
        min =  1000000000.0;
        max = -1000000000.0;
        sum = 0.0;
        for (int i = 0; i < contents.size(); i++) {
           double curr = contents.get(i);
           sum += curr;
           if (curr < min) {
               min = curr; 
           }
           if (curr > max) {
               max = curr;
           }
        }
        mean = sum / contents.size();
    }

    /** Print the statistics of the file. */
    public void printStats() {
       System.out.println("Number of entries: " + contents.size());
       System.out.println("Minimum: " + String.format("%.2f", min));
       System.out.println("Maximum: " + String.format("%.2f", max));
       System.out.println("Sum: " + String.format("%.2f", sum));
       System.out.println("Mean: " + String.format("%.2f", mean));
    }

    /** Return a copy of the contents. */
    public ArrayList<Double> getContents() {
        ArrayList<Double> copy = new ArrayList<Double>();
        for (int i = 0; i < contents.size(); i++) {
            copy.add(contents.get(i));
        }
        return copy;
    }

    /** Return the number of entries. */
    public double size() {
        return contents.size();
    }

    /** Return the `min` field. */
    public double getMin() {
        return min;
    }

    /** Return the `max` field. */
    public double getMax() {
        return max;
    }

    /** Return the `sum` field. */
    public double getSum() {
        return sum;
    }

    /** Return the `mean` field. */
    public double getMean() {
        return mean;
    }
}

