Alx09

java lliste pretmax pretmin

Oct 10th, 2021 (edited)
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. package problema3;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class Produs {
  7.     private double pret, cantitate;
  8.     private String nume;
  9.     private static List<Produs> listaPretMax = new ArrayList<Produs>();
  10.     private static List<Produs> listaPretMin = new ArrayList<Produs>();
  11.  
  12.     public Produs(String nume, double pret, double cantitate) {
  13.         this.cantitate = cantitate;
  14.         this.nume = nume;
  15.         this.pret = pret;
  16.  
  17.         if (listaPretMin.isEmpty() || pret < listaPretMin.get(0).pret) {
  18.             listaPretMin.clear();
  19.             listaPretMin.add(this);
  20.         } else if (pret == listaPretMin.get(0).pret)
  21.             listaPretMin.add(this);
  22.  
  23.         if (listaPretMax.isEmpty() || pret > listaPretMax.get(0).pret) {
  24.             listaPretMax.clear();
  25.             listaPretMax.add(this);
  26.         } else if (pret == listaPretMax.get(0).pret)
  27.             listaPretMax.add(this);
  28.     }
  29.  
  30.     public static List<Produs> getMin() {
  31.         return listaPretMin;
  32.     }
  33.  
  34.     public static List<Produs> getMax() {
  35.         return listaPretMax;
  36.     }
  37.  
  38.     public String toString() {
  39.         return String.format("%-10s%-10.2f%-10.2f", nume, pret, cantitate);
  40.     }
  41.  
  42.     public boolean CompareCantitatea(double cantitateData) {
  43.         return cantitate <= cantitateData;
  44.     }
  45.  
  46. }
  47.  
Add Comment
Please, Sign In to add comment