TD 4 - Bibliothèque d'Evry

biblio

Introduction

L'université d'Evry a besoin de rénover le système informatique de gestion des livres de ses bibliothèques. L'université possède deux bibliothèques et chacune gère plusieurs livres. Les étudiants peuvent consulter la liste des livres disponibles, emprunter et rendre les livres. Dans ce qui suit, nous allons construire deux classes Book.java et Library.java qui fournissent des fonctionnalités pour la gestion des livres.

La class Book.java

D'abord, nous avons besoin d'une classe Book.java pour modéliser les livres. Considérons le code ci-dessous.
public class Book {
    public String title;
    public boolean borrowed;

    // Question 1
    public Book(String bookTitle) {
    }   
    // Question 2
    public void borrowed() {
     }   
    // Question 3
    public void returned() {
    }   
    // Question 4
    public boolean isBorrowed() {
    }  
    // Question 5
    public String getTitle() {
     }

    public static void main(String[] arguments) {
        Book example = new Book("The Da Vinci Code");
        System.out.println("Title (should be The Da Vinci Code): " 
        						+ example.getTitle());
        System.out.println("Borrowed? (should be false): " + example.isBorrowed());
        example.borrowed();
        System.out.println("Borrowed? (should be true): " + example.isBorrowed());
        example.returned();
        System.out.println("Borrowed? (should be false): " + example.isBorrowed());
    }
} 
  1. Complétez le constructeur de la classe qui prend en argument le titre du livre.
  2. Complétez la méthode borrowed qui marque le livre comme déjà emprunté.
  3. Complétez la méthode returned qui marque le livre comme disponible.
  4. Complétez la méthode isBorrowed qui retourne true si le livre est déjà emprunté et false si non.
  5. Complétez la méthode getTitle qui retourne le titre du livre.
  6. Quels sont les attributs ou méthodes que l'on peut rendre private. Expliquez pourquoi?
Le résultat quand vous exécutez la classe Book.java devra être comme suit:
Title (should be The Da Vinci Code): The Da Vinci Code
Borrowed? (should be false): false
Borrowed? (should be true): true
Borrowed? (should be false): false

La class Library.java

Maintenant, nous allons construire une classe représentant chaque bibliothèque qui gère une collection des livres. Les bibliothèques sont ouvertes de 8h à 18h tous les jours. Les horaires peuvent être ajustés selon les saisons mais doivent être les mêmes pour les bibliothèques. Chaque bibliothèque a sa propre adresse et sa propre collection de livres. Nous avons une squelette de code du fichier Library.java donné ci-dessous.
import java.util.ArrayList;

public class Library {
    // Add the missing implementation to this class
   
    // Question 7, attributs
    
    // Question 8, constructeur
    public Library(String addr){    
    }
    // Question 9
    public boolean addBook(Book b){
    }
    // Question 10
    public static void printOpeningHours(){
    }
    // Question 11
    public void printAddress(){
    }
    // Question 12
    public void printAvailableBooks(){
    }
    // Question 13
    public void borrowBook(String bookTitle){
    }
    // Question 14

    public static void main(String[] args) {
        // Create two libraries
        Library firstLibrary = new Library("23 bld de France");
        Library secondLibrary = new Library("1 Maupertuis");

        // Add four books to the first library
        firstLibrary.addBook(new Book("The Da Vinci Code"));
        firstLibrary.addBook(new Book("Le Petit Prince"));
        firstLibrary.addBook(new Book("A Tale of Two Cities"));
        firstLibrary.addBook(new Book("The Lord of the Rings"));

        // Print opening hours and the addresses
        System.out.println("Library hours:");
        printOpeningHours();
        System.out.println();

        System.out.println("Library addresses:");
        firstLibrary.printAddress();
        secondLibrary.printAddress();
        System.out.println();

        // Try to borrow The Lords of the Rings from both libraries
        System.out.println("Borrowing The Lord of the Rings:");
        firstLibrary.borrowBook("The Lord of the Rings");
        firstLibrary.borrowBook("The Lord of the Rings");
        secondLibrary.borrowBook("The Lord of the Rings");
        System.out.println();

        // Print the titles of all available books from both libraries
        System.out.println("Books available in the first library:");
        firstLibrary.printAvailableBooks();
        System.out.println();
        System.out.println("Books available in the second library:");
        secondLibrary.printAvailableBooks();
        System.out.println();

        // Return The Lords of the Rings to the first library
        System.out.println("Returning The Lord of the Rings:");
        firstLibrary.returnBook("The Lord of the Rings");
        System.out.println();

        // Print the titles of available from the first library
        System.out.println("Books available in the first library:");
        firstLibrary.printAvailableBooks();
    }
} 
  1. Définissez les attributs nécessaires à la construction d'une bibliothèque. La collection des livres sera un tableau des livres construit à l'aide de la classe ArrayList. Le fichier Library.java et la sortie demandée pourraient vous donner des idées sur les attributs nécessaires.
  2. Ecrivez le constructeur de la classe Library.java qui prend en argument l'adresse de la bibliothèque. Le constructeur instancie une bibliothèque avec cette adresse donnée et une collection vide des livres.
  3. Complétez la méthode addBook qui ajoute un livre en argument à la collection de la bibliothèque. Vous pouvez utiliser la méthode add de la classe ArrayList.
  4. Ecrivez une méthode statique printOpeningHours qui affiche à l'écran l'horaire de la bibiliothèque.
  5. Ecrivez une méthode printAddress qui affiche l'adresse de la bibiliothèque.
  6. Ecrivez une méthode printAvailableBooks qui affiche les livres disponibles de la bibiliothèque. S'il n'y a aucun livre disponible, affichez "No available book in catalog".
  7. Ecrivez une méthode borrowBook qui prend en argument le titre d'un livre et qui perment d'emprunter un livre de la bibliothèque. Si le livre est emprunté, affichez "You successful borrowed" avec le titre du livre. Si le livre n'est pas dans le catalogue, affichez "Sorry, this book is not in our catalog". Si le livre est dans le catalogue mais déjà emprunté, affichez "Sorry, this book is already borrowed". Remarque: Pour comparer les deux chaînes de caractères s1 et s2, l'instruction s1.equals(s2) retourne true s'elles sont identiques, et false si non.
  8. Cherchez les méthodes manquées (qui apparaissent dans la méthode main mais ne sont pas encore définies). Dénifissez-les et implémentez-les.
Le résultat quand vous exécutez la classe Library.java devra être comme suit:
Library hours:
Libraries are open daily from 8h to 17h

Library addresses:
23 bld de France
1 Maupertuis

Borrowing The Lord of the Rings:
You successfully borrowed The Lord of the Rings
Sorry, this book is already borrowed
Sorry, this book is not in our catalog

Books available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities

Books available in the second library:
No available book in catalog

Returning The Lord of the Rings:
You successfully returned The Lord of the Rings

Books available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities
The Lord of the Rings



Dernière mise à jour : 27/03/2012

Pour tout commentaire : thang [arobase] ibisc [point] fr