Exercice 2 TP2
package tp2ex2; HAJTAIB Ahmed
public abstract class Document {
protected String titre; &
FEKI Omar
public Document(String titre) {
[Link] = titre;
}
public abstract String getNumero();
public String toString() {
return "Titre: " + titre;
}
}
package tp2ex2;
public class Livre extends Document {
private String auteur;
private int nbPages;
private String numero;
public Livre(String numero, String titre, String auteur, int nbPages) {
super(titre);
[Link] = numero;
[Link] = auteur;
[Link] = nbPages;
}
public String getNumero() {
return numero;
}
public String getAuteur() {
return auteur;
}
public String toString() {
return "Livre [" + numero + "] - " + [Link]() +
", Auteur: " + auteur + ", Pages: " + nbPages;
}
}
package tp2ex2;
public class Dictionnaire extends Document {
private String langue;
private int volume;
private String numero;
public Dictionnaire(String numero, String titre, String langue, int volume) {
super(titre);
[Link] = numero;
[Link] = langue;
[Link] = volume;
}
public String getNumero() {
return numero;
}
public String toString() {
return "Dictionnaire [" + numero + "] - " + [Link]() +
", Langue: " + langue + ", Volume: " + volume;
}
}
package tp2ex2;
public class TestDocuments {
public static void main(String[] args) {
Livre livre1 = new Livre("Liv2354", "Le Petit Prince", "Antoine de Saint-
Exupéry", 96);
Dictionnaire dict1 = new Dictionnaire("Dic1298", "Dictionnaire Français",
"Français",
3);
[Link](livre1);
[Link](dict1);
}
}
package tp2ex2;
public class Bibliotheque {
private Document[] documents;
private int compteur;
public Bibliotheque(int capacite) {
documents = new Document[capacite];
compteur = 0;
}
public boolean ajouterDocument(Document doc) {
if (compteur < [Link]) {
documents[compteur] = doc;
compteur++;
return true;
}
return false;
}
public void afficherDocuments() {
for (int i = 0; i < compteur; i++) {
[Link](documents[i]);
}
}
public void afficherAuteurs() {
for (int i = 0; i < compteur; i++) {
if (documents[i] instanceof Livre) {
Livre l = (Livre) documents[i];
[Link]("Auteur: " + [Link]());
}
}
}
public Document document(int i) {
if (i >= 0 && i < compteur) {
return documents[i];
}
return null;
}
public boolean supprimer(Document doc) {
for (int i = 0; i < compteur; i++) {
if (documents[i].equals(doc)) {
for (int j = i; j < compteur - 1; j++) {
documents[j] = documents[j + 1];
}
documents[compteur - 1] = null;
compteur--;
return true;
}
}
return false;
}}
package tp2ex2;
public class TestBiblio {
public static void main(String[] args) {
Bibliotheque biblio = new Bibliotheque(5);
Livre livre1 = new Livre("Liv2354", "Le Petit Prince", "Antoine de Saint-
Exupéry", 96);
Livre livre2 = new Livre("Liv1234", "1984", "George Orwell", 328);
Dictionnaire dict1 = new Dictionnaire("Dic1298", "Dictionnaire Français",
"Français", 3);
[Link](livre1);
[Link](livre2);
[Link](dict1);
[Link]("Documents dans la bibliothèque:");
[Link]();
[Link]("\nAuteurs présents:");
[Link]();
[Link]("\nSuppression d’un document:");
if ([Link](livre2)) {
[Link]("Suppression réussie.");
} else {
[Link]("Document non trouvé.");
}
[Link]("\nDocuments après suppression:");
[Link]();
}
}