A13

Streda

package sk.upjs.paz.average_calculator;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Utils {

        public static double average(List<Double> zoznam) {
                try {
                        if (zoznam.size() == 0) {
                                throw new EmptyListException("Zoznam ma velkost 0");
                        }

                        double sucet = 0;
                        for (Double cislo : zoznam) {
                                sucet = sucet + cislo;
                        }
                        return sucet / zoznam.size();
                } catch (NullPointerException e) {
                        throw new EmptyListException(e);
                }
        }

        /**
         * Popis metody...
         * @param array popis parametra
         * @return co to vracia
         * @throws ParseFailedException aku vynimku a kedy ju to hadze
         */

        public static List<Double> parseNumbers(String[] array) throws ParseFailedException {
                List<Double> zoznam = new LinkedList<>();
                for (int i = 0; i < array.length; i++) {

                        try {
                                zoznam.add(Double.parseDouble(array[i]));
                        } catch (NumberFormatException e) {
                                throw new ParseFailedException("Pokazilo sa to na hodnote: \""+ array[i]+ "\"");
                        }
                }
                return zoznam;
        }

}
 
package sk.upjs.paz.average_calculator;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import sk.upjs.jpaz2.*;

public class Launcher {

        public static void main(String[] args) throws ParseFailedException {
//              List<Double> zoznam = new LinkedList<>();
//              zoznam.add(-7.0);
//              zoznam.add(1.0);
//              zoznam.add(4.0);
//              System.out.println(zoznam.get(100));
//              System.out.println(Utils.average(zoznam));

//              String[] pole = {"1.0", "5.2", "3.14ahoj"};
                System.out.println(Utils.average(Utils.parseNumbers(args)));

        }
}
 
package sk.upjs.paz.average_calculator;

public class EmptyListException extends RuntimeException {

        public EmptyListException() {
                super();
                // TODO Auto-generated constructor stub
        }

        public EmptyListException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
                super(message, cause, enableSuppression, writableStackTrace);
                // TODO Auto-generated constructor stub
        }

        public EmptyListException(String message, Throwable cause) {
                super(message, cause);
                // TODO Auto-generated constructor stub
        }

        public EmptyListException(String message) {
                super(message);
                // TODO Auto-generated constructor stub
        }

        public EmptyListException(Throwable cause) {
                super(cause);
                // TODO Auto-generated constructor stub
        }

}
 
package sk.upjs.paz.average_calculator;

public class ParseFailedException extends Exception {

        public ParseFailedException() {
                super();
                // TODO Auto-generated constructor stub
        }

        public ParseFailedException(String message, Throwable cause, boolean enableSuppression,
                        boolean writableStackTrace) {
                super(message, cause, enableSuppression, writableStackTrace);
                // TODO Auto-generated constructor stub
        }

        public ParseFailedException(String message, Throwable cause) {
                super(message, cause);
                // TODO Auto-generated constructor stub
        }

        public ParseFailedException(String message) {
                super(message);
                // TODO Auto-generated constructor stub
        }

        public ParseFailedException(Throwable cause) {
                super(cause);
                // TODO Auto-generated constructor stub
        }

}