Last active
December 11, 2015 00:48
-
-
Save titouanc/4518627 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Compte(object): | |
| """Represente un compte en banque""" | |
| EUROS_PAR_DOLLAR = 0.8447 | |
| def __init__(self): | |
| """Renvoie un nouveau compte""" | |
| self.solde = 0.0 | |
| def getSolde(self): | |
| """Renvoie le solde du compte en euros (c'est un getter)""" | |
| return self.solde | |
| def getSoldeDollar(self): | |
| """Renvoie le solde du compte en dollars (c'est aussi un getter)""" | |
| return (1.0/self.EUROS_PAR_DOLLAR)*self.solde | |
| def crediter(self, montant): | |
| """Credite le compte d'un montant en euros""" | |
| if not (isinstance(montant, float) or isinstance(montant, int)): | |
| raise Exception("Le montant d'un versement doit etre un nombre !") | |
| elif montant <= 0: | |
| raise Exception("Le montant d'un versement ne peut etre nul ou negatif") | |
| self.solde += montant | |
| def debiter(self, montant): | |
| """Debite le compte d'un montant en euros""" | |
| if not (isinstance(montant, float) or isinstance(montant, int)): | |
| raise Exception("Le montant d'un debit doit etre un nombre !") | |
| elif montant <= 0: | |
| raise Exception("Le montant d'un debit ne peut etre nul ou negatif") | |
| elif montant > self.solde: | |
| raise Exception("Le solde du compte est insuffisant") | |
| self.solde -= montant | |
| if __name__ == "__main__": | |
| titou = Compte() | |
| titou.crediter(127.52) | |
| print(titou.getSolde()) | |
| titou.debiter(32.47) | |
| print(titou.getSoldeDollar()) | |
| titou.debiter(100) # => Exception("Le solde du compte est insuffisant") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment