package SolveEquation;

public class Eq2D {

	private double a;
	private double b;
	private double c;
	private double x1;
	private double x2;
	private int nbRacines = -1;
	
	public Eq2D(double a, double b, double c) throws DegenerateExceptionEq2D {
		if(a==0){
			throw new DegenerateExceptionEq2D();
		}
		this.a = a;
		this.b = b;
		this.c = c;
	}

	public double getX1() {
		return x1;
	}

	public void setX1(double x1) {
		this.x1 = x1;
	}

	public double getX2() {
		return x2;
	}

	public void setX2(double x2) {
		this.x2 = x2;
	}

	public int getNbRacines() {
		return nbRacines;
	}

	public void setNbRacines(int nbRacines) {
		this.nbRacines = nbRacines;
	}

	public double getA() {
		return a;
	}

	public double getB() {
		return b;
	}

	public double getC() {
		return c;
	}
	
	public void Resoudre()
	{
		double Delta;
		
		
			Delta = Math.pow(this.getB(), 2) - 4 * this.getA() * this.getC() ;
			
			
			if(Delta<0){
				this.setNbRacines(0);
			}
			else if (Delta==0)
			{
				this.setNbRacines(1);
				this.setX1( - this.getB() /  (2*this.getC())  );
				this.setX2( - this.getB() /  (2*this.getC())  );
			}
			else 
			{
				this.setNbRacines(2);
				this.setX1( (- this.getB() + Math.sqrt(Delta)) /  (2*this.getC())  );
				this.setX2( (- this.getB() - Math.sqrt(Delta)) /  (2*this.getC())  );	
			}

		
		
		
	}

	public void Afficher() throws SolveExceptionEq2D {
		
		if(this.getNbRacines()==-1){
			throw new SolveExceptionEq2D();
		}
		
		System.out.println("Voici l'équation que vous avez entré : ");
		System.out.println("("+this.getA()+")X² + ("+this.getB()+")X + "+this.getC());

		if(this.getNbRacines()==0){
			System.out.println("Il n'y a pas de solution réelle.");
		}
		else if(this.getNbRacines()==1){
			System.out.println("Il y a une solution double : ");
			System.out.println("x1 = "+this.getX1());
		}
		else{
			System.out.println("Il y a deux solutions simples : ");
			System.out.println("x1 = "+this.getX1());
			System.out.println("x2 = "+this.getX2());
		}
	}
	
	
	
}
