Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. package serie06;
  2.  
  3. import java.util.Map;
  4. import java.util.TreeMap;
  5.  
  6. import util.Contract;
  7.  
  8. public class StdStock<E> implements Stock<E> {
  9.  
  10. // ATTRIBUTS
  11.  
  12. private final Map<E, Integer> stock;
  13.  
  14. // CONSTRUCTEURS
  15.  
  16. /**
  17. * Un stock vide.
  18. * @post <pre>
  19. * e != null </pre>
  20. */
  21. public StdStock() {
  22. stock = new TreeMap<E, Integer>();
  23. }
  24.  
  25. // REQUETES
  26.  
  27. public int getNumber(E e) {
  28. Contract.checkCondition(e != null);
  29. if (stock.get(e) == null) {
  30. return 0;
  31. }
  32. return stock.get(e).intValue();
  33. }
  34.  
  35. public int getTotalNumber() {
  36. int sum = 0;
  37. for (E e : stock.keySet()) {
  38. sum += stock.get(e).intValue();
  39. }
  40. return sum;
  41. }
  42.  
  43. // COMMANDES
  44.  
  45. public void addElement(E e) {
  46. Contract.checkCondition(e != null);
  47.  
  48. if (!stock.containsKey(e)) {
  49. stock.put(e, new Integer(1));
  50. } else {
  51. changeNumber(e, 1);
  52. }
  53. }
  54.  
  55. public void addElement(E e, int qty) {
  56. Contract.checkCondition(e != null);
  57. Contract.checkCondition(qty > 0);
  58.  
  59. if (!stock.containsKey(e)) {
  60. stock.put(e, new Integer(qty));
  61. } else {
  62. changeNumber(e, qty);
  63. }
  64. }
  65.  
  66. public void removeElement(E e) {
  67. Contract.checkCondition(e != null);
  68. Contract.checkCondition(getNumber(e) >= 1);
  69.  
  70. changeNumber(e, -1);
  71. }
  72.  
  73. public void removeElement(E e, int qty) {
  74. Contract.checkCondition(e != null);
  75. Contract.checkCondition(qty > 0);
  76. Contract.checkCondition(getNumber(e) >= qty);
  77.  
  78. changeNumber(e, -qty);
  79. }
  80.  
  81. public void reset() {
  82. stock.clear();
  83. }
  84.  
  85. // OUTILS
  86.  
  87. private void changeNumber(E e, int qty) {
  88. stock.put(e, new Integer(stock.get(e).intValue() + qty));
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement