École Nationale d’Ingénieurs de Gabès
Département de GEA
Programmation Orientée Objets
Fatma ELLOUZE
[email protected] Correction TP 2
Auditoire : GEA2
AU : 2023 - 2024 1
Exercice 1
Objectif
Le développement d’une application de gestion de bibliothèque, en
mettant en pratique les différentes notions de l’héritage, surcharge des
constructeurs, polymorphisme et exception.
2
La classe
Document
3
public class Document {
int num;
String titre;
protected static int nbreDoc=0;
public Document (String titre)
{
this.titre=titre;
Document. nbreDoc++;
this.num= nbreDoc;
}
public String toString() {
return "Document [num=" + num + ", titre=" + titre + "]";
}
}
4
La classe
Livre
5
public class Livre extends Document {
String auteur;
int nbrePages;
public Livre (String t, String a, int n)
{
super(t);
auteur=a;
nbrePages=n;
}
@Override
public String toString() {
return super.toString() +" [auteur=" + auteur + ", nbrePages=" +
nbrePages + "]";
}
6
La classe
Roman
7
public class Roman extends Livre{
String prix;
public Roman (String t, String a, int n, String p) {
super(t,a,n);
prix= p;
}
@Override
public String toString() {
return super.toString() + " [prix=" + prix + "]";
}
8
La classe
Bibliothèque
9
import java.io.FileWriter;
import java.io.IOException;
public class Bibliothèque {
Document [] T;
public Bibliothèque (int c)
{
T= new Document [c];
}
public void afficherDocuments()
{
for (int i=0; i<Document.nbreDoc; i++)
System.out.println(T[i].toString());
}
public Document document(int i)
{
return T[i]; 10
}
public boolean ajouter(Document doc)
{
if (Document.nbreDoc <= T.length)
{T[Document.nbreDoc-1]= doc;
return true;}
else
return false;
}
// on n’a pas tester l’unicité du document à ajouter vu que le
numéro est géré d’une façon automatique. D’où, on est sûr qu’il
n’y a pas de redondance
public void ajouterDsFichier(Document doc) throws IOException
{
FileWriter w = new
FileWriter("C:\\Users\\DELL\\Desktop\\fileAII");
w.write(doc.toString()+ "\n");
w.close();
11
}
public static void main(String[] args) throws IOException {
Bibliothèque b= new Bibliothèque(500);
Document d1= new Livre("Serais tu là", "Guillame musso", 300);
b.ajouter(d1);
b.ajouterDsFichier(d1);
Document d2= new Livre("L'alchimiste", "Paulo Coelho",300);
b.ajouter(d2);
b.ajouterDsFichier(d2);
Document d3 = new Roman("Les misérables", "Victor Hugo", 300,
"Cesar");
b.ajouter(d3);
b.ajouterDsFichier(d3);
System.out.println("**********Doccccc**********"+b.document(0));
System.out.println("**********Tous les documents**********");
b.afficherDocuments();
}
12
}
Exemple avec Interface
• Soit l’interface Biblio permettant de fixer la
capacité (nombre maximum de documents) à
1000.
public interface Biblio
{
int Max =1000;
}
• Implémenter l’interface Biblio dans la
bibliothèque et faire les modifications
nécessaires 13
public class Bibliothèque implements Biblio {
Document[] T;
// Constructeur par défaut qui utilise l'interface afin
de fixer la taille du tableau
public Bibliothèque() {
T = new Document[MAX];
}
14