Last active
April 18, 2016 01:45
-
-
Save fxg42/b68486f346b8de9ef91064906ad999f5 to your computer and use it in GitHub Desktop.
INF1256: Exemples de programmation OO vus en classe
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
import datetime | |
import string | |
import math | |
import random | |
class Cours(object): | |
def __init__(self, sigle): | |
self.sigle = sigle | |
self.groupes_cours = [ ] | |
class GroupeCours(object): | |
def __init__(self, cours, max_inscriptions, politique_reservation): | |
self.cours = cours | |
cours.groupes_cours.append(self) | |
self.inscriptions = [ ] | |
self.max_inscriptions = max_inscriptions | |
self.politique_reservation = politique_reservation | |
def nb_inscrits(self): | |
return len(self.inscriptions) | |
def inscrire_etudiante(self, etudiante): | |
if self.politique_reservation.est_respectee(self): | |
Inscription(self, etudiante) | |
return True | |
else: | |
return False | |
class Inscription(object): | |
def __init__(self, groupe_cours, etudiante): | |
self.etudiante = etudiante | |
etudiante.inscriptions.append(self) | |
self.groupe_cours = groupe_cours | |
groupe_cours.inscriptions.append(self) | |
class Etudiante(object): | |
def __init__(self, nom, prenom, date_naissance): | |
self.nom = nom | |
self.prenom = prenom | |
self.date_naissance = date_naissance | |
self.code_permanent = self.generer_code_permanent() | |
self.inscriptions = [ ] | |
def generer_code_permanent(self): | |
code_permanent = "" | |
if len(self.nom) >= 3: | |
code_permanent += self.nom[0:3].upper() | |
else: | |
code_permanent += self.nom.ljust(3, 'X').upper() | |
code_permanent += self.prenom[0].upper() | |
code_permanent += self.date_naissance.strftime("%Y%m%d") | |
code_permanent += random.choice(string.digits) | |
code_permanent += random.choice(string.digits) | |
return code_permanent | |
def __str__(self): | |
return "%s, %s (%s)" % (self.nom, self.prenom, self.code_permanent) | |
class PolitiqueReservation(object): | |
def est_respectee(self, groupe_cours): | |
return groupe_cours.nb_inscrits() < groupe_cours.max_inscriptions | |
class PolitiqueSurreservation(PolitiqueReservation): | |
def __init__(self, taux): | |
self.taux = taux | |
def est_respectee(self, groupe_cours): | |
return groupe_cours.nb_inscrits() < math.floor(groupe_cours.max_inscriptions * self.taux) | |
alice = Etudiante('Carroll', 'Alice', datetime.datetime(1900, 1, 1)) | |
print alice | |
inf1256 = Cours('INF1256') | |
reservationStricte = PolitiqueReservation() | |
surreservation = PolitiqueSurreservation(1.15) | |
groupe_du_lundi = GroupeCours(inf1256, 70, surreservation) | |
groupe_du_mardi = GroupeCours(inf1256, 50, reservationStricte) | |
groupe_du_jeudi = GroupeCours(inf1256, 100, reservationStricte) | |
if groupe_du_lundi.inscrire_etudiante(alice): | |
print "Inscription reussie" | |
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 Porte(object): | |
pass | |
class Fenetre(object): | |
def __init__(larg, haut): | |
self.largeur = larg | |
self.hauteur = haut | |
class Maison(object): | |
def __init__(self, larg, prof): | |
self.largeur = larg | |
self.profondeur = prof | |
self.porte_entree = None | |
self.fenetres = [ ] | |
def calculer_superficie(self): | |
return self.largeur * self.profondeur | |
def calculer_nb_fenetres(self): | |
return len(self.fenetres) | |
class Chalet(Maison): | |
def calculer_superficie(self): | |
return self.largeur * self.profondeur / 2 | |
maison_paille = Maison(100.0, 20.0) | |
maison_paille.porte_entree = Porte() | |
maison_paille.fenetres.append(Fenetre(1, 2)) | |
maison_paille.fenetres.append(Fenetre(3, 4)) | |
maison_paille.fenetres.append(Fenetre(1, 2)) | |
print maison_paille.calculer_superficie() | |
maison_bois = Maison(3, 1000000) | |
print maison_bois.calculer_superficie() | |
maison_brique = Chalet(2, 3) | |
print maison_brique.calculer_superficie() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment