1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| class Compte{
int solde;
String titulaire;
int numero;
int decouvertAutorise;
void afficher(){
Terminal.ecrireString("solde:"+ this.solde);
Terminal.sautDeLigne();
}
void deposer(int montant)throws MontantNeg{
if(montant<0)
throw new MontantNeg();
solde=solde+montant;
}
void retirer(int montant)throws MontantNeg,Decouvert{
if(montant<0)
throw new MontantNeg();
if(montant>solde+decouvertAutorise)
throw new Decouvert();
solde+=montant;
}
}
class MontantNeg extends Exception{}
class Decouvert extends Exception{}
class Essai_1_Compte{
public static void main(String[]args){
Compte c1= new Compte();
Compte c2 =new Compte();
c1.solde=100;
c1.decouvertAutorise=100;
try{
Terminal.ecrireString("saisir le montant à deposer :");
c1.deposer(Terminal.lireInt());
}
try{
Terminal.ecrireString("saisir le montant à retirer :");
c1.retirer(Terminal.lireInt());
}
catch(MontantNeg e){
Terminal.ecrireStringln("le montant est neg");
}
catch(Decouvert e){
Terminal.ecrireStringln("decouvert interdit, retrait impossible");
}
c1.afficher();
}
} |