Last active
July 7, 2017 12:27
-
-
Save SebDeclercq/a7b9ecaee133e18453c0e53ee0eb3fdc 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
| <?php | |
| // LES CLASSES | |
| class Bibliotheque | |
| { | |
| protected $livres = array(); | |
| public function getLivres() { | |
| return $this->livres; | |
| } | |
| public function ajouteLivre(Livre $livre) { | |
| $this->livres[] = $livre; | |
| } | |
| public function ajouteSaga(Saga $saga) { | |
| foreach ($saga->livres as $livre) { | |
| $this->ajouteLivre($livre); | |
| } | |
| } | |
| } | |
| class Livre | |
| { | |
| public $titre; | |
| protected $auteur; | |
| public function __construct($titre, Auteur $auteur) { | |
| $this->titre = $titre; | |
| $this->auteur = $auteur; | |
| } | |
| public function getAuteur() { | |
| return $this->auteur; | |
| } | |
| public function __toString() { | |
| return "Le livre \"$this->titre\" est écrit par ".$this->auteur->prenom." ".$this->auteur->nom; | |
| } | |
| } | |
| class Ebook extends Livre | |
| { | |
| public $format; | |
| public function __construct($titre, Auteur $auteur, $format='mobi') { | |
| parent::__construct($titre, $auteur); | |
| $this->format = $format; | |
| } | |
| } | |
| class Saga | |
| { | |
| public $titre, $livres = array(), $auteur; | |
| public function __construct($titre, Auteur $auteur) { | |
| $this->titre = $titre; | |
| $this->auteur = $auteur; | |
| } | |
| public function ajouteLivre(Livre $livre) { | |
| $this->livres[] = $livre; | |
| } | |
| public function __toString() { | |
| return "La saga \"$this->titre\" est écrite par $this->auteur->prenom $this->auteur->nom}. | |
| Elle contient #{@livres.count} livres"; | |
| } | |
| } | |
| class Auteur | |
| { | |
| public $prenom, $nom, $dob; | |
| public function __construct($nom, $prenom, $dob=false) { | |
| $this->nom = $nom; | |
| $this->prenom = $prenom; | |
| if ($dob === false) { | |
| $this->dob = 'inconnue'; | |
| } | |
| else { | |
| $this->dob = $dob; | |
| } | |
| } | |
| } | |
| // LE CODE | |
| $biblio = new Bibliotheque; | |
| $hamilton = new Auteur('Hamilton', 'Peter F.'); | |
| $sagas = array( | |
| "L'aube de la nuit" => array("Rupture dans le réel : Genèse", "Rupture dans le réel : Émergence", "Rupture dans le réel : Expansion", "L'alchimiste du neutronium : Consolidation", "L'alchimiste du neutronium : Conflit", "Le Dieu nu : Résistance", "Le Dieu nu : Révélations"), | |
| "La saga du Commonwealth" => array("Pandore abusée", "Pandore menacée", "Judas déchaîné", "Judas démasqué"), | |
| "La trilogie du vide" => array("Vide qui songe", "Vide temporel", "Vide en évolution"), | |
| "La trilogie des rêveurs" => array("The abyss beyond dreams", "Night without stars"), | |
| ); | |
| foreach ($sagas as $titreSaga => $livres) { | |
| $saga = new Saga($titreSaga, $hamilton); | |
| foreach ($livres as $titre) { | |
| $livre = new Livre($titre, $hamilton); | |
| $saga->ajouteLivre($livre); | |
| } | |
| $biblio->ajouteSaga($saga); | |
| } | |
| $gregMandel = new Saga('Greg Mandel', $hamilton); | |
| foreach (explode(' ', 'Mindstar Quantum Nano') as $tome) { | |
| $livre = new Livre($tome, $hamilton); | |
| $gregMandel->ajouteLivre($livre); | |
| } | |
| $biblio->ajouteSaga($gregMandel); | |
| foreach ($biblio->getLivres() as $livre) { | |
| echo $livre."\n"; | |
| } |
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
| #!/usr/bin/env perl | |
| use warnings; | |
| use strict; | |
| use feature "say"; | |
| use feature "switch"; | |
| use Carp qw/carp croak cluck confess/; | |
| package Bibliotheque { | |
| use Moose; | |
| has "livres", is => "ro", isa => "ArrayRef[Livre]", default => sub {[]}; | |
| sub ajouteLivre { | |
| my ($self, $livre) = @_; | |
| confess("ajouteLivre() prend en entrée une instance de la classe Livre") | |
| unless $livre->isa("Livre"); | |
| push $self->livres, $livre; | |
| } | |
| sub ajouteSaga { | |
| my ($self, $saga) = @_; | |
| confess("ajouteSaga() prend en entrée une instance de la classe Saga") | |
| unless $saga->isa("Saga"); | |
| for (@{$saga->livres}) { | |
| $self->ajouteLivre($_); | |
| } | |
| } | |
| } | |
| package Livre { | |
| use Moose; | |
| has "titre", is => "rw", isa => "Str"; | |
| has "auteur", is => "ro", isa => "Auteur"; | |
| use overload '""' => sub { | |
| my $self = shift; | |
| "Le livre \"".$self->titre."\" est écrit par " | |
| .$self->auteur->prenom." ".uc($self->auteur->nom); | |
| } | |
| } | |
| package Ebook { | |
| use Moose; | |
| extends "Livre"; | |
| has "format", is => 'rw', isa => 'Str', default => 'mobi'; | |
| } | |
| package Saga { | |
| use Moose; | |
| has "titre", is => 'ro', isa => 'Str'; | |
| has "livres", is => 'ro', isa => 'ArrayRef[Livre]', default => sub {[]}; | |
| has "auteur", is => 'ro', isa => 'Auteur'; | |
| sub ajouteLivre { | |
| my ($self, $livre) = @_; | |
| confess("ajouteLivre() prend en entrée une instance de la classe Livre") | |
| unless $livre->isa("Livre"); | |
| push $self->livres, $livre; | |
| } | |
| use overload '""' => sub { | |
| my $self = shift; | |
| "La saga \"".$self->titre."\" est écrite par " | |
| .$self->auteur->prenom." ".uc($self->auteur->nom). | |
| ". Elle contient ".scalar @{$self->livres}." livre(s)"; | |
| } | |
| } | |
| package Auteur { | |
| use Moose; | |
| has ["prenom", "nom"], is => "rw", isa => "Str"; | |
| has "dob", is => "rw", isa => "Str", default => 'inconnue'; | |
| } | |
| my $biblio = Bibliotheque->new(); | |
| my $hamilton = Auteur->new(prenom => 'Peter F.', nom => 'Hamilton'); | |
| my %sagas = ( | |
| "L'aube de la nuit" => ["Rupture dans le réel : Genèse", "Rupture dans le réel : Émergence", "Rupture dans le réel : Expansion", "L'alchimiste du neutronium : Consolidation", "L'alchimiste du neutronium : Conflit", "Le Dieu nu : Résistance", "Le Dieu nu : Révélations"], | |
| "La saga du Commonwealth" => ["Pandore abusée", "Pandore menacée", "Judas déchaîné", "Judas démasqué"], | |
| "La trilogie du vide" => ["Vide qui songe", "Vide temporel", "Vide en évolution"], | |
| "La trilogie des rêveurs" => ["The abyss beyond dreams", "Night without stars"], | |
| ); | |
| while (my ($titreSaga, $livres) = each(%sagas)) { | |
| my $saga = Saga->new(titre => $titreSaga, auteur => $hamilton); | |
| for (@$livres) { | |
| my $livre = Livre->new(titre => $_, auteur => $hamilton); | |
| $saga->ajouteLivre($livre); | |
| } | |
| $biblio->ajouteSaga($saga); | |
| } | |
| my $gregMandel = Saga->new("Greg Mandel", $hamilton); | |
| for (qw/Mindstar Quantum Nano/) { | |
| my $livre = Livre->new(titre => $_, auteur => $hamilton); | |
| $gregMandel->ajouteLivre($livre); | |
| } | |
| $biblio->ajouteSaga($gregMandel); | |
| say for @{$biblio->livres}; |
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
| #!/usr/bin/env python3 | |
| # LES CLASSES | |
| class Auteur: | |
| def __init__(self, nom, prenom, dob=None): | |
| self.prenom, self.nom = prenom, nom | |
| if dob == None: | |
| self.dob = 'inconnue' | |
| else: | |
| self.dob = dob | |
| class Livre: | |
| def __init__(self, titre, auteur: Auteur): | |
| self.titre, self.auteur = titre, auteur | |
| class Ebook(Livre): | |
| def __init__(self, titre, auteur: Auteur, format="mobi"): | |
| Livre.__init__(self, titre, auteur) | |
| self.format = format | |
| class Saga: | |
| def __init__(self, titre, auteur: Auteur): | |
| self.titre, self.auteur, self.livres = titre, auteur, [] | |
| def ajouteLivre(self, livre: Livre): | |
| self.livres.append(livre) | |
| def __str__(self): | |
| return 'La saga "' + self.titre + '" est écrite par ' + self.auteur.prenom + ' ' + self.auteur.prenom + 'Elle contient ' + str(len(self.livres)) + ' livres ' | |
| class Bibliothèque: | |
| def __init__(self): | |
| self.livres = [] | |
| def ajouteLivre(self, livre: Livre): | |
| self.livres.append(livre) | |
| def ajouteSaga(self, saga: Saga): | |
| for livre in saga.livres: | |
| self.ajouteLivre(livre) | |
| # LE CODE | |
| biblio = Bibliothèque() | |
| hamilton = Auteur('Hamilton', 'Peter F.') | |
| sagas = { | |
| "L'aube de la nuit": ["Rupture dans le réel : Genèse", "Rupture dans le réel : Émergence", "Rupture dans le réel : Expansion", "L'alchimiste du neutronium : Consolidation", "L'alchimiste du neutronium : Conflit", "Le Dieu nu : Résistance", "Le Dieu nu : Révélations"], | |
| "La saga du Commonwealth": ["Pandore abusée", "Pandore menacée", "Judas déchaîné", "Judas démasqué"], | |
| "La trilogie du vide": ["Vide qui songe", "Vide temporel", "Vide en évolution"], | |
| "La trilogie des rêveurs": ["The abyss beyond dreams", "Night without stars"], | |
| } | |
| for titreSaga in sagas: | |
| saga = Saga(titreSaga, hamilton) | |
| for livre in sagas[titreSaga]: | |
| saga.ajouteLivre(livre) | |
| biblio.ajouteSaga(saga) | |
| gregMandel = Saga("Greg Mandel", hamilton) | |
| for tome in "Mindstar Quantum Nano".split(' '): | |
| livre = Ebook(tome, hamilton) | |
| gregMandel.ajouteLivre(livre) | |
| biblio.ajouteSaga(gregMandel) | |
| for livre in biblio.livres: | |
| print(livre) | |
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
| #!/usr/bin/env ruby | |
| # LES CLASSES | |
| class Bibliothèque | |
| attr_reader :livres | |
| def initialize | |
| @livres = Array.new | |
| end | |
| def ajouteLivre(livre) | |
| raise TypeError unless livre.is_a? Livre | |
| @livres << livre | |
| end | |
| def ajouteSaga(saga) | |
| raise TypeError unless saga.is_a? Saga | |
| saga.livres.each do |livre| | |
| self.ajouteLivre(livre) | |
| end | |
| end | |
| end | |
| class Livre | |
| attr_accessor :titre | |
| attr_reader :auteur | |
| def initialize(titre, auteur) | |
| raise TypeError unless auteur.is_a? Auteur | |
| @titre, @auteur = titre, auteur | |
| end | |
| def to_s | |
| "Le livre \"#@titre\" est écrit par #{@auteur.prenom} #{@auteur.nom}" | |
| end | |
| end | |
| class Ebook < Livre | |
| attr_accessor :format | |
| def initialize(titre, auteur, format='mobi') | |
| super titre, auteur | |
| @format = format | |
| end | |
| end | |
| class Saga | |
| attr_reader :titre, :livres, :auteur | |
| def initialize(titre, auteur) | |
| raise TypeError unless auteur.is_a? Auteur | |
| @titre, @auteur, @livres = titre, auteur, Array.new | |
| end | |
| def ajouteLivre(livre) | |
| raise TypeError unless livre.is_a? Livre | |
| @livres << livre | |
| end | |
| def to_s | |
| "La saga \"#@titre\" est écrite par #{@auteur.prenom} #{@auteur.nom}. | |
| Elle contient #{@livres.count} livres" | |
| end | |
| end | |
| class Auteur | |
| attr_accessor :nom, :prenom, :dob | |
| def initialize(nom, prenom, dob=nil) | |
| @nom, @prenom = nom, prenom | |
| if dob.nil? | |
| @dob = 'inconnue' | |
| else | |
| @dob = dob | |
| end | |
| end | |
| end | |
| # LE CODE | |
| biblio = Bibliothèque.new | |
| hamilton = Auteur.new('Hamilton', 'Peter F.') | |
| sagas = [ | |
| "L'aube de la nuit": ["Rupture dans le réel : Genèse", "Rupture dans le réel : Émergence", "Rupture dans le réel : Expansion", "L'alchimiste du neutronium : Consolidation", "L'alchimiste du neutronium : Conflit", "Le Dieu nu : Résistance", "Le Dieu nu : Révélations"], | |
| "La saga du Commonwealth": ["Pandore abusée", "Pandore menacée", "Judas déchaîné", "Judas démasqué"], | |
| "La trilogie du vide": ["Vide qui songe", "Vide temporel", "Vide en évolution"], | |
| "La trilogie des rêveurs": ["The abyss beyond dreams", "Night without stars"], | |
| ] | |
| sagas.each do |saga| | |
| saga.each do |titreSaga, livres| | |
| saga = Saga.new(titreSaga, hamilton) | |
| livres.each do |titre| | |
| livre = Livre.new(titre, hamilton) | |
| saga.ajouteLivre(livre) | |
| end | |
| biblio.ajouteSaga(saga) | |
| end | |
| end | |
| gregMandel = Saga.new("Greg Mandel", hamilton) | |
| %w(Mindstar Quantum Nano).each do |tome| | |
| livre = Ebook.new(tome, hamilton) | |
| gregMandel.ajouteLivre(livre) | |
| end | |
| biblio.ajouteSaga(gregMandel) | |
| biblio.livres.each do |livre| | |
| puts livre | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment