C13

Streda

package sk.upjs.paz.calculator;

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

import sk.upjs.paz.EmptyListException;
import sk.upjs.paz.ParseFailedException;

public class Utils {
        /**
         * Metoda average vrati priemer cisel zo vstupneho zoznamu
         * @param zoznam - list cisel zvany zoznam
         * @return hodnota priemeru cisel v liste zoznam
         */

        public static double average(List<Double> zoznam)  {
                double sucet = 0;
                if(zoznam == null || zoznam.size() ==0) {
                        throw new EmptyListException("Zoznam je null alebo prazdny");
                }
                for (Double double1 : zoznam) {
                        sucet += double1;
                }
                return sucet/zoznam.size();
        }


         public static List<Double> parseNumbers(String[] array) throws ParseFailedException{

                 List<Double> zoznam = new ArrayList<>();
                 List<String> neparsovatelne = new ArrayList<>();
                 for (String string : array) {
                        double cislo = 0;
                        try {
                                cislo = Double.parseDouble(string);
                                zoznam.add(cislo);
                        } catch (NumberFormatException e) {
                                neparsovatelne.add(string);
                                //throw new ParseFailedException();
                        }

                }

                 if(!neparsovatelne.isEmpty()) {

                                throw new ParseFailedException(neparsovatelne.toString());
                        }

                 return zoznam;
         }

}
package sk.upjs.paz;

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;

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
        }



}
package sk.upjs.paz.calculator;

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

import sk.upjs.jpaz2.*;
import sk.upjs.paz.ParseFailedException;

public class Launcher {

        public static void main(String[] args)  {

                /*String[] array = {"3.15", "14.2", "pat cela osem", "tri"};
                List<Double> zoznam = new ArrayList<>();
                */

                /*zoznam.add(3.15);
                zoznam.add(14.2);*/


                        try {
                                System.out.println(Utils.parseNumbers(args));
                        } catch (ParseFailedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }

                //System.out.println(Utils.average(zoznam));

        }
}