package tp1.ex2_1;

public class SolveEquation {
    private double a, b, c;

    public SolveEquation(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    /*
     * Gives the number of solutions of the equation .
     * 
     * @return the number of solution .
     * 
     * @throws ArithmeticException if there
     * is an infinite number of solutions .
     */
    public int getNbSolutions() throws ArithmeticException {
        double delta = b * b - 4 * a * c;
        if (delta < 0)
            throw new ArithmeticException("No real solution");
        else if (delta == 0)
            return 1;
        else
            return 2;
    }

    /*
     * * Gives the equation ’s solutions for x .
     * 
     * @return the 2 solutions of the equation . If there is
     * only 1 solution , the 2 members of the pair are equal .
     * 
     * @throws ArithmeticException if there is no real
     * solution for x , or if there is an infinite number
     * of solutions .
     */
    public Pair<Double> getSolutions() throws ArithmeticException {
        double delta = b * b - 4 * a * c;
        if (delta < 0)
            throw new ArithmeticException("No real solution");
        else if (delta == 0) {
            double x = -b / (2 * a);
            return new Pair<Double>(x, x);
        }
        else
            return new Pair<Double>(
                (-b - Math.sqrt(delta)) / (2 * a), 
                (-b + Math.sqrt(delta)) / (2 * a));
    }
}
