Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package serie06;
- import java.util.Map;
- import java.util.TreeMap;
- import util.Contract;
- public class StdStock<E> implements Stock<E> {
- // ATTRIBUTS
- private final Map<E, Integer> stock;
- // CONSTRUCTEURS
- /**
- * Un stock vide.
- * @post <pre>
- * e != null </pre>
- */
- public StdStock() {
- stock = new TreeMap<E, Integer>();
- }
- // REQUETES
- public int getNumber(E e) {
- Contract.checkCondition(e != null);
- if (stock.get(e) == null) {
- return 0;
- }
- return stock.get(e).intValue();
- }
- public int getTotalNumber() {
- int sum = 0;
- for (E e : stock.keySet()) {
- sum += stock.get(e).intValue();
- }
- return sum;
- }
- // COMMANDES
- public void addElement(E e) {
- Contract.checkCondition(e != null);
- if (!stock.containsKey(e)) {
- stock.put(e, new Integer(1));
- } else {
- changeNumber(e, 1);
- }
- }
- public void addElement(E e, int qty) {
- Contract.checkCondition(e != null);
- Contract.checkCondition(qty > 0);
- if (!stock.containsKey(e)) {
- stock.put(e, new Integer(qty));
- } else {
- changeNumber(e, qty);
- }
- }
- public void removeElement(E e) {
- Contract.checkCondition(e != null);
- Contract.checkCondition(getNumber(e) >= 1);
- changeNumber(e, -1);
- }
- public void removeElement(E e, int qty) {
- Contract.checkCondition(e != null);
- Contract.checkCondition(qty > 0);
- Contract.checkCondition(getNumber(e) >= qty);
- changeNumber(e, -qty);
- }
- public void reset() {
- stock.clear();
- }
- // OUTILS
- private void changeNumber(E e, int qty) {
- stock.put(e, new Integer(stock.get(e).intValue() + qty));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement