Compte.java 2.56 KB


public class Compte {

    int MAX_OPERATIONS = 10;
    
    private double credits[] = new double[MAX_OPERATIONS];
    private double debits[] = new double[MAX_OPERATIONS];
    private int dernierCredit=0;
    private int dernierDebit=0;
    
    public Compte(){
    
    }
    
    public Compte(double x){
        crediter(x);
    }
    
    
    public int getDernierCredit(){
        return dernierCredit;
    }
    
    public int getDernierDebit(){
        return dernierDebit;
    }
    
    public void setDernierCredit(int i){
       this.dernierCredit=i;
    }
    
    public void setDernierDebit(int i){
        this.dernierDebit=i;
    }
    
    public double getCredit(int indice){
        return credits[indice];
    }
    
    public double getDebit(int indice){
        return debits[indice];
    }
    
    public void setCredit(int indice, double d){
       credits[indice]=d;
    }
    
    public void setDebit(int indice, double d){
        debits[indice]=d;
    }
    
    public void crediter(double x){
        if(getDernierCredit()+1>MAX_OPERATIONS){
            double sd = soldeCredit();
            this.credits = new double[MAX_OPERATIONS];
            setCredit(0, sd);
        } else {
            setCredit(getDernierCredit(),x);
            setDernierCredit(getDernierCredit()+1);
        }
    }

   public void debiter(double x){
       
        
        if(getDernierDebit()+1>MAX_OPERATIONS){
            double sd = soldeDebit();
            this.debits = new double[MAX_OPERATIONS];
            setDebit(0, sd);
        } else {
            setDebit(getDernierDebit(),x);
            setDernierDebit(getDernierDebit()+1);
        }
    }
    
    public double soldeCredit(){
        double res = 0;
        if(getDernierCredit()>0){
            for(int i = 0; i<=getDernierCredit(); i++){
                res+=getCredit(i);
            }
        }  
        return res;
    }
    
    public double soldeDebit(){
        double res = 0;
        if(getDernierDebit()>0){
            for(int i = 0; i<=getDernierDebit(); i++){
                res+=getDebit(i);
            }
        }  
        return res;
    }
    
    public double solde(){
        return soldeCredit()-soldeDebit();
    }
    
    public String toString(){
        String res="";
        for(int i = 0; i<=getDernierCredit(); i++){
            res+="Credit"+i+"= "+getCredit(i)+"\n";
        }
        res+="\n";
        for(int i = 0; i<=getDernierDebit(); i++){
            res+="Debit"+i+"= "+getDebit(i)+"\n";
        }
        res+="\nSolde final:"+solde()+".\n";
        return res;
    }
    
}