class livre{ private String titre; private String auteur; private int nbp; private float prix; livre(String titre,String auteur,int nbp,float prix){ this.titre=titre; this.auteur=auteur; this.nbp=nbp; this.prix=prix; } String getTitre(){ return this.titre; } String getAuteur(){ return this.auteur; } int getNbp(){ return this.nbp; } float getPrix(){ return this.prix; } void setTitre(String titre){ this.titre=titre; } void setAuteur(String auteur){ this.auteur=auteur; } void setNbp(int nbp){ this.nbp=nbp; } void setPrix(float prix){ this.prix=prix; } public String toString() { return "Livre{" + "titre='" + titre + '\'' + ", auteur='" + auteur + '\'' + ", nombrePages=" + nbp + ", prix=" + prix + '}'; } public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } livre autre = (livre) obj; return this.titre.equals(autre.getTitre()) && this.auteur.equals(autre.getAuteur()); }} class etagere{ private livre [] tab; private int nblivre; etagere(int n){ this.tab=new livre[n]; this.nblivre=0; } int getNbmax(){ return tab.length; } int getNblivre(){ return this.nblivre; } boolean ajouter(livre l){ if(nblivre<tab.length){ tab[nblivre]=l; nblivre++; return true; } return false; } livre getLivre(int p){ if(p>0 && p<=nblivre){ return tab[p-1]; } return null; } int cher(String ti,String au){ int nb=0; for(int i=0;i<nblivre;i++){ if(tab[i].getTitre().equals(ti) && tab[i].getAuteur().equals(au)){ return i+1; } } return 0; } int [] cherch(String ti,String au){ int x=0,x1=0; for(int i=0;i<nblivre;i++){ if(tab[i].getTitre().equals(ti) && tab[i].getAuteur().equals(au)){ x++; }} int []t=new int[x]; for(int i=0;i<nblivre;i++){ if(tab[i].getTitre().equals(ti) && tab[i].getAuteur().equals(au)){ t[x1]=i+1; x1++;}} return t; } void affiche() { System.out.println("Livres sur l'etagere :"); for (int i = 0; i < nblivre; i++) { System.out.println((i + 1) + ". " + tab[i]); } } } public class Main{ public static void main(String [] args){ livre l1=new livre("koukou","ahmed",250,3500); livre l2=new livre("boubou","iyed",630,7800); livre l3=new livre("kouk","wael",852,6988); livre l4=new livre("boubou","iyed",630,7800); System.out.println(l1.toString()); System.out.println(l2.toString()); etagere e1=new etagere(5); e1.ajouter(l1); e1.ajouter(l2); e1.ajouter(l3); e1.ajouter(l4); e1.affiche(); int[] position = e1.cherch("boubou", "iyed"); if (position.length > 0) { System.out.println("Positions des livres trouves"); for (int i = 0; i < position.length; i++) { System.out.println(position[i]); } } else { System.out.println("Aucun livre trouvé avec ce titre et cet auteur."); } }}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter