package facture; import article.Article; import java.util.HashMap; import java.util.Map; /** * Une facture. Implémentation avec une Map pour les lignes de facture. */ public class Facture { private static int numeroCourant = 0; private String client, date; // Données des lignes de la facture private Map<String, Integer> lignes = new HashMap<>(); /** * Numéro de la facture */ private int numero = 0; public Facture(String client, String date) { this.client = client; this.date = date; numero = ++numeroCourant; } public void ajouterLigne(String referenceArticle, int quantite) { if (lignes.containsKey(referenceArticle)) { // Déjà une ligne avec cet article // On ajoute la quantité à la quantité déjà dans la facture lignes.put(referenceArticle, lignes.get(referenceArticle) + quantite); } else { lignes.put(referenceArticle, quantite); } } public int getPrixTotal() { int pt = 0; for (Map.Entry<String, Integer> entree : lignes.entrySet()) { Article article = Article.getArticle(entree.getKey()); pt += article.getPU() * entree.getValue(); } return pt; } /** * Affichage de la facture sous une forme plus lisible. Utilisation de printf * "comme dans le langage C" (pas étudié dans le cours). */ public void afficheToi() { System.out.printf("Facture numéro %d ; Client : %s ; Date : %s %n", numero, client, date); System.out.printf("%7s | %6s | %22s | %10s | %10s %n", "Quant.", "Réf.", "Nom", "PU", "PT"); for (Map.Entry<String, Integer> entree : lignes.entrySet()) { afficherLigne(entree); } System.out.printf("%35s Prix total facture : %10d", "", getPrixTotal()); System.out.println(); } private void afficherLigne(Map.Entry<String, Integer> entree) { Article article = Article.getArticle(entree.getKey()); int quantite = entree.getValue(); System.out.printf("%7d | %6s | %22s | %10d | %10d %n", quantite, article.getReference(), article.getNom(), article.getPU(), article.getPU() * quantite); } @Override public String toString() { String descript = "Facture " + numero + ";Client=" + client + ";Date=" + date + "\n"; for (Map.Entry<String, Integer> entree : lignes.entrySet()) { Article article = Article.getArticle(entree.getKey()); descript += article.getNom() + " " + entree.getValue() + "\n"; } descript += "Prix total Facture=" + getPrixTotal(); return descript; } }