Vous êtes sur la page 1sur 3

Registre.

java

1package exam2018;
2
3import java.util.Iterator;
4import java.util.LinkedList;
5import java.util.Map;
6import java.util.Set;
7import java.util.TreeMap;
8import java.util.TreeSet;
9import java.util.function.Predicate;
10
11public class Registre extends LinkedList<Produit>{
12
13 Registre css(int qt) {
14 Registre res = new Registre();
15 Predicate<Produit> p = (e -> e.quantity > qt);
16
17 for(Iterator<Produit> it = iterator(); it.hasNext();) {
18 Produit e = it.next();
19 if(p.test(e)) {
20 res.add(e);
21 }
22 }
23
24 return res;
25 }
26
27 Map<String, Integer> scommandes(){
28 Map<String, Integer> m = new TreeMap<>();
29 for(Produit p : this) {
30 if(!m.containsKey(p.code)) {
31 m.put(p.code, p.quantity);
32 }else {
33 m.replace(p.code, m.get(p.code)+p.quantity);
34 }
35 }
36 return m;
37 }
38
39 String plusProduitCommande() {
40 Map<String, Integer> m = scommandes();
41
42 Set<Map.Entry<String, Integer>> set = m.entrySet();
43
44 TreeSet<Integer> qt = new TreeSet<>(m.values());
45 int max = qt.last();
46
47 for(Map.Entry<String, Integer> en : set) {
48 if(en.getValue() == max)
49 return en.getKey();
50 }
51

52 return null;
53 }
54}
Produit.java

1package exam2018;
2
3public class Produit {
4
5 String code;
6 int quantity;
7
8 public Produit() {
9
10 }
11
12 public Produit(String code, int quantity) {
13 this.code = code;
14 this.quantity = quantity;
15 }
16
17 @Override
18 public String toString() {
19 return "Produit [code=" + code + ", quantity=" + quantity + "]";
20 }
21
22 @Override
23 public boolean equals(Object obj) {
24 if (this == obj)
25 return true;
26 if (obj == null)
27 return false;
28 if (getClass() != obj.getClass())
29 return false;
30 Produit other = (Produit) obj;
31 if (code == null) {
32 if (other.code != null)
33 return false;
34 } else if (!code.equals(other.code))
35 return false;
36 return true;
37 }
38
39
40
41
42
43}
Test.java

1package exam2018;
2
3public class Test {
4 public static void main(String[] args) {
5
6 Registre r = new Registre();
7
8 r.add(new Produit("X",15));
9 r.add(new Produit("X",5));
10 r.add(new Produit("Y",12));
11 r.add(new Produit("Z",19));
12 r.add(new Produit("X",3));
13 r.add(new Produit("Z",7));
14 r.add(new Produit("Y",14));
15 r.add(new Produit("Y",1));
16
17 System.out.println(r.plusProduitCommande());
18 }
19}
20

Vous aimerez peut-être aussi