Advertisement
mmouhib

TP3 EX2

Oct 24th, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. public class Produit {
  2.  
  3.     public final int REF;
  4.     public String libelle;
  5.     private int qte;
  6.     private float prix;
  7.  
  8.     public Produit(int ref, String libelle) {
  9.         this.REF = ref;
  10.         this.libelle = libelle;
  11.     }
  12.  
  13.     public Produit(int ref) {
  14.         this.REF = ref;
  15.         this.setLibelle("Inconnu");
  16.     }
  17.  
  18.     public int getQte() {
  19.         return qte;
  20.     }
  21.  
  22.     public void setQte(int qte) {
  23.         this.qte = qte;
  24.     }
  25.  
  26.     public float getPrix() {
  27.         return prix;
  28.     }
  29.  
  30.     public void setPrix(float prix) {
  31.         this.prix = prix;
  32.     }
  33.  
  34.     public void setLibelle(String libelle) {
  35.         this.libelle = libelle;
  36.     }
  37.  
  38.     public Produit fusion (Produit prod){
  39.         Produit res = new Produit(this.REF, this.libelle);
  40.         res.setLibelle(this.libelle + ", " + prod.libelle);
  41.         res.setQte(this.getQte() + prod.getQte());
  42.         float price = (this.getPrix() * this.getQte() + prod.getPrix() * prod.getQte())
  43.                 /(this.getQte()+ prod.getQte());
  44.         res.setPrix(price);
  45.         return res;
  46.     }
  47. }
  48.  
  49.  
  50. // test
  51.  
  52. public class test {
  53.     public static void main(String[] args) {
  54.         Produit prod1 = new Produit(120, "Verre V255");
  55.         Produit prod2 = new Produit(125, "Verre V220");
  56.  
  57.         prod1.setQte(12000);
  58.         prod1.setPrix(0.125f);
  59.  
  60.         prod2.setQte(50000);
  61.         prod2.setPrix(0.100f);
  62.  
  63.         Produit prod3 = prod1.fusion(prod2);
  64.  
  65.         System.out.println(prod3.REF);
  66.         System.out.println(prod3.libelle);
  67.         System.out.println(prod3.getQte());
  68.         System.out.println(prod3.getPrix());
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement