You are on page 1of 8
TpLibrairieTest.java Limport java.util.*; 2public class TPLibrairieTest { wovauns 10 11 12 B 14 45 16 7 18 19 28 21 22 23 24 25 26 27 28 29 30 31. 32 33 34 35, 36 37 38 39 40 aL a2 43 44 45 46 47 48 49 50 Sa. 52 53 54 55 56 L'adhérent: private static final Scanner sc = new Scanner(System.in); private static int lireEntier(String message) { System. out.printIn(message) ; return sc.nextInt(); ? private static String lirechaine(String message) { system.out.print1n(message) ; return sc.next(); y private static double lireDouble(String message) { system.out.printin(message); return sc.nextDouble(); } private static int afficheMenu() { system.out.printIn("1 : Ajouter un adherent” System.out.printIn("2 : Ajouter un livre"); System.out.printIn("3 : Ajouter un journal"); System.out.printIn("4 : Emprunter un livre"); System.out.printin("5 : Consulter les livres empruntés par un adhérent") 5 system.out.print1n("6 : Lister la bibliotheque"); System.out.print1n("7 : Rendre un livre"); System.out.println("@ : Quitter 1*application"); int rep = LireEntier("\nFaites un choix"); return rep; } public static void main(string[] args) { Bibliotheque bib = new Bibliotheque(); int choix = dof choix = afficheMenu(); switch(choix) A case 1: String num = LireChaine("Saisir le numéro d'identité de String name = LireChaine("Saisir le nom de 1'adhérent: “); String pr = Lirechaine("Saisir le prénom de l'adhérent: "); adherent ad = new Adherent(num, name, pr); bib.ajouteradherent (ad); break; case 2: String titre '= Lirechaine("saisir le titre du livre: String naut = Lirechaine("Saisir le nom de l'auteur: "); Page 1 Scanné avec CamScanner ‘TPLibrairieTest.java 59 bib. ajouterDocunent (doc); 60 brea 61 case 3: 62 String tit = LireChaine("Saisir le titre du Journal: 63 int an = LireEntier("Saisir l'année: "); 64 int mois = LireEntier("Saisir le mois: "); 65 int jour = Liretntier("Saisir le jour: "); 66 Document d = new Journal(tit, an, mois, jour); 67 bib. ajouterDocument(d); 68 break; 69 case 4: 7 String numid = LireChaine("Saisir le numéro d*identité de L'adhérent: "); 7a String t = LireChaine("Saisir le titre du livre: 72 String aut = Lirechaine("Saisir le nom de 1'auteur: 73 Adherent adh = bib.chercheradherent(numid) ; 74 Livre 1 = (Livreybib.chercherLivre(t, aut); a if(adh t= null 8& 1 1= null) 76 { 7 if(1.getestemprunte()) 78 System.out.printin("\n****** L‘opeération a échouée: Ce livre est déja emprunté *#++#e*\n"); 79 else if(adh.getNbEmprunte() >= 2) 80 System.out.printIn("\n* L'opeération a échouee L'adhérent posséde déja deux livres *****#*\n) ai else 82 adh.empruntertivre( 1: 83 84 85 break; 86 case S$: 87 String nid = Lirechaine("Saisir le numéro d*identité de 88 Adherent adherent = bib.chercheradherent(nid); 89 4if(adherent != null) 90 adherent. 1isterLivretmprunte(); 91 else 92 System.out.println(“Le numéro d'identité ne correspond A aucun adhérent"); 93 break; 94 case 6 : 95 System.out.printIn("\t****** Liste des adhérents *xxs+es 96 bib. 1isterAdnerent(); 97 Systen.out.printn("\n\t***** Liste des livres seeaeenny 98 bib. 1isterLivres(); 99 System.out.printIn("\n\t***** Liste des Journaux s##se0 168 bib. ListerJournauxs(); 201 break; 102 case 7: 103 String nuneroid = Lirechaine("Saisir le nunéro d'identité de Ladnérent: 104 Srjne tite » Lirechaine("saisir Je titre du livre: “); 105 panereatte = Linechaine("Saisir 1e non de ‘auteur: *); 106 Adherent adhe = bib.chercheradherent (nuneroid); 107 pivre liv = (Livre)bib.cherchertivre(titr, aute); 108 if(adhe = null && liv I= nul1) 109 if(adhe.getbEmprunte() > @) ne adhe.rendreLivre(liv); Page 2 Scanné avec CamScanner 117 118 119 128 12a 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 441 142 143 144 145 146 147 148 149 158 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 a7 172 173 i TPLibrairieTest. java default : a System.out.printin("\n\tnuméro érroné") 3 + Jwhile(choix I= @); class Bibliotheque { adhérent") private ArrayList listeadherent; private ArrayList listeDocument; private int nbAd; private int nbDoc; public Bibliotheque() { this. listeAdherent = new ArrayList(); this.listeDocument = new ArrayList(); this.nbad = 0; this .nbDoc } public void ajouterDocument (Document doc) { ListeDocument .add(doc) ; nbboc++; + public void ajouteradherent(Adherent ad) { ‘ListeAdherent .add(ad) ; nbadt+; } public Adherent chercherAdherent(String id) { Adherent ad = null; boolean trouve = false; for(int i= @; icnbAd; i++) sf ad = (Adherent)1isteadherent..get (i); if (id. equalsignoreCase(ad.getId())) trouve = true; breal + By if(trouve) return ad; else { System-out.printin( "le numéro d'indentité ne correspond § aucun return null; } Scanné avec CamScanner TPLibrairietest.java 175 176 public Document 7 Fi fent_chercherLivre(String tit, String aut) 178 Document doc = null; v9 boolean trouve = false; 188 for(int i = @; icnbdoc; i++) 181 { 182 doc = (Document) listeDocument.get(i); 183 if(doc instanceof Livre) 184 if(aut.equalsIgnoreCase(((Livre) doc).getAuteur()) && tit.equalsignoreCase(doc.getTitre())) 185 { 186 trouve = true; 187 break; 188 } 189 } 198 if (trouve) 191 return doc; 192 else 193 { . 194 system.out.printIn("le titre et/ou le nom de 1'auteur ne correspondent 3 aucun livre de la bibliotheque); 195 return null; 196 ) 197 } 198 199 public void listerAdherent() 200 201 for(int i = 5 icnbAd; i++) 202 System. out.printin(listeadherent.get(i)); 203 } 204 205 public void listerLivres() 206 { 207 Document doc = null; 208 for(int i = @; icnbboc; it+) 209 210 doc = (Document) listeDocument..get(i); 2aa if(doc instanceof Livre) 212 Systen.out.printIn(doc); 213 } 214 215 } 216 217 public void listerJournauxs() 218 { 219 Document doc = null; 220 for(int 4 = @; icnbDoc; i++) 22a { 222 doc = (Document)listeDocument.get(i); 223 if(doc instanceof Journal) 224 System.out.println(doc); 225 } 226 27 } 228 229 23e Pare 4 Scanné avec CamScanner 233, 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 258 251 252 253 254 255 256 257 258 259 268 261 262 263 264 265 266 267 268 269 278 271 272 273 274 275 276 277 278 279 289 281 282 283, 284 285 285, 287 288 289 TPLibrairievest.java private String id; private String non; private String prenom; private Livre [] liste; private static final int nbMax = private int nbenprunte = public Adherent(String num, String nom, String pr) { this.id = num; this.nom = nom; this.prenom = prs Liste = new Livre[nbMax]; } public String gettd() { return this.id; } public String getNon() { return this.nom; } public String getPrenom() { return this.prenon; } public void emprunterLivre(Livre 1) { Liste[nbemprunte] = 1; L.setEstEmprunte(true) ; nbEmprunte++; ? public void rendreLivre(Livre 1) { boolean trouve = false; if(1 == liste[e]) { liste[0] = liste[1); liste[1] = null; trouve = true; die Fa ee Hsteta) Liste[1) = null; trouve = true; Iecerouve) nbEmprunte-. L.setestEmprunte(false); y else System.out.printin(******* \n Ce livre n'est pas enprunté par cet Page 5 Scanné avec CamScanner ‘TPLibrairietest. java 201 292 293 public void listerLivreénprunte() 294, { 295 System.out.printIn("\nt*###* Liste des livres enpruntés par: "+ this +" seaneneny s 296 System,out.printin("*##** Nombre de livres empruntés: " + this.nbenprunte +" +##24#4\p) 297 for(int i = 0; icnbEmprunte; i++) 298 System.out.printIn(this.1iste[4]); 299 } 300 3e1 public int getNbemprunte() 302 { 303 return nbémprunt 304 } 305 306 public String toString() 7 ” 4 thi mi 300 peturn “munéro d°identité : * + this.gets4() +" Hom : * + this.getton) +" Prénom : "+ this.getPrenon(); 309 } 31e y Baa Biz 313 class Document Bia { 31s private String titre; 316 public Document(String t) 317 t 318 this.titre = t; 31s. + 328 321 public String getTitre() 322 t 323 return this.titre; 324 } 325 326 public void setTitre(String t) 327 { 328 this.titre = t; 329 ) 330 331 public String toString() 332 { 333 return “Titre : "+ this.titre; 334 y 335} 336 337 class Livre extends Document 338 { 339 private String auteur; 340 private int nbpage; ua private boolean esténprunte; 343 ublic Livee(s' Ke fs (String t, String aut, int nbp) 345 super(t); Page 6 Scanné avec CamScanner 349 358 351 352 353 354 355, 356 357 358 359 360 361 362 363 364 365 366 367 368 369 378 371 372 373, 374 375 376 377 378 373, 380 381 382 383, 384 385, 386 387 388 389 390 391 392 393, 395 396 397 398, 399 400 492 403, 405, TPLibrairieTest.java } puoite String getAuteur() return this.auteur; } public int getNbPage() return this.nbPage; y polite boolean getestemprunte() return this.estEmprunte; } public void setAuteur(string a) ‘this.auteur = a; } public void setNbPage(int n) ‘this.nbpage = n; } public void setestmprunte(boolean b) { this.estemprunte = b; } public String toString() { return super.toString() + "\n" + “Auteur : "+ this.auteur + "\n" + "Nombre de pages : "+ this.nbpage + "\n"; ) } class Journal extends Document { private Date dateParu; public Journal (String t, int an, int mois, int jour) super(t); Gregoriancalendar cal = new GregorianCalendar(an, mois-1, jour); this dateParu = cal.getTime(); 3 } public Date getDateParu() © eeurn this.dateParu; t public void setDateParu(Date 4) this.dateParu = d; Page 7 Scanné avec CamScanner TpLibrairietest. java 407 } 408 409 ublic Str: pt public String tostring() aut "\n"5 az 4130} 414 415 return super.toString() + "\n" + “Date de parution: “ + this.dateParu + Scanné avec CamScanner

You might also like