Created
March 21, 2020 14:10
-
-
Save GravenilvecTV/acdbb596a193e4a92e13c6ebbdc5b9cb to your computer and use it in GitHub Desktop.
JOUR 2 - JAVA - Magasin de jouets - Niveau 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.gravendev.ex; | |
public class Jouet implements Comparable<Jouet> { | |
// attributs de notre classe | |
private String nom; // nom du jouet | |
private int prix; // prix du jouet | |
private String description; // description du jouet | |
private int promo; // promotion du jouet en cours | |
// constructeur | |
public Jouet(String nom, int prix, String description, int promo) { | |
this.nom = nom; | |
this.prix = prix; | |
this.description = description; | |
this.promo = promo; | |
} | |
// methodes | |
public String getNom() { return nom; } | |
public int getPrix() { return prix; } | |
public String getDesc() { return description; } | |
public int getPromo() { return promo; } | |
@Override | |
public int compareTo(Jouet jouet2) { | |
return this.getNom().compareTo(jouet2.getNom()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.gravendev.ex; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
public class Main { | |
public static void main(String[] args) { | |
// créer une liste qui va stocker le nom de plusieurs jouets | |
List<Jouet> jouets = new ArrayList<>(); | |
jouets.add(new Jouet("Scrubble Deluxe", 20, "Petit jeu de société", 5)); | |
jouets.add(new Jouet("Lu Garu", 15, "Jeu avec pleins de roles", 10)); | |
jouets.add(new Jouet("Zebre Figurine", 6, "Super figurine de zebre", 5)); | |
jouets.add(new Jouet("Osobot Evo", 70, "robot pour apprendre à dev", 15)); | |
jouets.add(new Jouet("Araignée peluche", 30, "peluche toute douce", 5)); | |
jouets.add(new Jouet("Ligo Start wars", 120, "super ligo box de start wars", 20)); | |
jouets.add(new Jouet("Bakogan Battle Pack", 20, "jeu de bataille de figurine", 0)); | |
// Afficher la liste à nouveau | |
for(Jouet jouet : jouets) { | |
System.out.println("Jouet: " + jouet.getNom() + " ("+ jouet.getPrix() +"€) " + | |
jouet.getDesc() + " (promo: " + jouet.getPromo() + "% de reduc)"); | |
} | |
// Tri | |
Collections.sort(jouets, new PrixComparator()); | |
System.out.println("Tri en cours...................."); | |
// Afficher la liste à nouveau | |
for(Jouet jouet : jouets) { | |
System.out.println("Jouet: " + jouet.getNom() + " ("+ jouet.getPrix() +"€) " + | |
jouet.getDesc() + " (promo: " + jouet.getPromo() + "% de reduc)"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.gravendev.ex; | |
import java.util.Comparator; | |
public class PrixComparator implements Comparator<Jouet> { | |
@Override | |
public int compare(Jouet jouet1, Jouet jouet2) { | |
// je compare le prix | |
if(jouet1.getPrix() == jouet2.getPrix()) | |
{ | |
return jouet1.compareTo(jouet2); // comparaison du nom | |
} | |
return jouet2.getPrix() - jouet1.getPrix(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment