Exemple05_BiblioRetourTélécharger Exemple de la gestion d'un livre d'une bibliotheque Exemple05.java // Exemple de la gestion d'un livre d'une bibliotheque import java.util.*; public class Exemple05 { public static void main(String... a_args) { Terminal.ecrireStringln("Gestion d'une bilbliotheque"); String[] auteurs = new String[2]; auteurs[0] = "Philip Jose Farmer"; auteurs[1] = "J. Stephens"; Livre livre1 = new Livre("12112011-0001", "Les Dieux du fleuve", "Le noir dessein", auteurs, 0, GenreLivre.ROMAN); livre1.afficherTitre(); Terminal.ecrireStringln("1 =================================="); int l_tome = 2; livre1.setTome(l_tome); Terminal.ecrireStringln( livre1.toString() ); Terminal.ecrireStringln("2 =================================="); String[] l_tab = new String[2]; l_tab[0] = "Philip Jose Farmer"; l_tab[1] = "K. Cramer"; livre1.setAuteurs(l_tab); Terminal.ecrireStringln( livre1.toString() ); Terminal.ecrireStringln("3 =================================="); String[] t = { "Isaac Asimov" }; Livre livre2 = new Livre("12112011-0002", "Les cavernes d'acier", "", t, 1, GenreLivre.SF ); livre1.afficherTitre(); Terminal.ecrireStringln("4 =================================="); Terminal.ecrireStringln( livre2.toString() ); Terminal.ecrireStringln("5 =================================="); Livre.afficherLivres(livre1,livre2); Terminal.ecrireStringln("6 =================================="); // Pour tester une enumeration for(GenreLivre g:GenreLivre.values() ) Terminal.ecrireStringln(""+g); Terminal.ecrireStringln("7 =================================="); if (livre2.getGenre().equals(GenreLivre.SF)) Terminal.ecrireStringln("EGAL"); } } GenreLivre.java public enum GenreLivre { HISTOIRE, GEOGRAPIE, ROMAN, SF } Livre.java // Classe de definition d'un livre // public class Livre { private String ident; // Identification unique du livre private String titre; // Titre du livre private String sousTitre; // Sous-titre du livre private String[] auteurs; // Les auteurs du livre private int tome; // Tome du livre (0 si pas plusieurs tome) private GenreLivre genre; // Genre du livre // Constructeur // public Livre(String ident, String titre, String sousTitre, String[] auteurs, int tome, GenreLivre genre) { this.ident = ident; this.titre = titre; this.sousTitre = sousTitre; this.auteurs = auteurs; this.tome = tome; this.genre = genre; } // Afficher le titre et/ou sous-titre d'un livre // public void afficherTitre() { if (! sousTitre.equals("")) { Terminal.ecrireStringln(titre + "(" + sousTitre + ")" ); return; } Terminal.ecrireStringln(titre); } // Conversion d'un livre en chain // public String toString() { String str=""; str=str+"Ident : "+ident+"\n"; str=str+"Titre : "+titre+"\n"; str=str+"Sous-titre : "+sousTitre+"\n"; str=str+"Tome : "+tome+"\n"; str=str+"Auteurs : "; for(String s:auteurs) str=str+s+" "; str=str+"\n"; str=str+"genre : "+genre+"\n"; return ( str ); } // Methode static pour afficher plusieurs livres // static public void afficherLivres(Livre... livres) { for(Livre l:livres) Terminal.ecrireStringln( l.toString() ); } // getteurs // public int getTome(){return tome;} public String[] getAuteurs(){return auteurs;} public GenreLivre getGenre(){return genre;} // setteurs // public void setIdent(String a_ident){ident = a_ident;} public void setTome(int a_tome){tome = a_tome;} public void setTitre(String a_titre){titre = a_titre;} public void setTitre(String a_titre,String a_sousTitre) { titre = a_titre; sousTitre = a_sousTitre; } public void setAuteurs(String[] a_auteurs){auteurs = a_auteurs;} } Terminal.java import java.io.*; public class Terminal{ static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static String lireString() // Lire un String { String tmp=""; char C='\0'; try { tmp = in.readLine(); } catch (IOException e) { exceptionHandler(e); } return tmp; } // fin de lireString() public static int lireInt() // Lire un entier { int x=0; try { x=Integer.parseInt(lireString()); } catch (NumberFormatException e) { exceptionHandler(e); } return x ; } public static boolean lireBoolean() // Lire un entier { boolean b = true; try { b = Boolean.valueOf(lireString()).booleanValue(); } catch (NumberFormatException e) { exceptionHandler(e); } return b; } public static double lireDouble() // Lire un double { double x=0.0; try { x=Double.valueOf(lireString()).doubleValue(); } catch (NumberFormatException e) { exceptionHandler(e); } return x ; } public static char lireChar() // Lire un caractere { String tmp=lireString(); if (tmp.length()==0) return '\n'; else { return tmp.charAt(0); } } public static void ecrireString(String s){ // Afficher un String try{ System.out.print(s); } catch (Exception ex){ exceptionHandler(ex); } } public static void ecrireStringln(String s) // Afficher un String { ecrireString(s); sautDeLigne(); } // fin de ecrireStringln() public static void ecrireInt(int i) // Afficher un entier { ecrireString(""+i); } public static void ecrireIntln(int i) // Afficher un entier { ecrireString(""+i); sautDeLigne(); } public static void ecrireBoolean(boolean b){ ecrireString(""+b); } public static void ecrireBooleanln(boolean b){ ecrireString(""+b); sautDeLigne(); } public static void ecrireDouble(double d) // Afficher un double { ecrireString(""+d); } public static void ecrireDoubleln(double d) // Afficher un double { ecrireDouble(d); sautDeLigne(); } public static void ecrireChar(char c) // Afficher un caractere { ecrireString(""+c); } public static void ecrireCharln(char c) // Afficher un caractere { ecrireChar(c); sautDeLigne(); } public static void sautDeLigne(){ try{ System.out.println(); }catch(Exception ex){ exceptionHandler(ex); } } protected static void exceptionHandler(Exception ex){ TerminalException err = new TerminalException(ex); throw err; } public static void ecrireException(Throwable ex){ ecrireString(ex.toString()); ex.printStackTrace(System.err); } } class TerminalException extends RuntimeException{ Exception ex; TerminalException(Exception e){ ex = e; } } compil.bat mkdir bin del /f /s /q bin\*.class javac -d bin *.java pause run.bat cd bin java Exemple05 pause