01: /*
02: @COPYRIGHT@
03: */
04: package demo.townsend.service;
05:
06: import java.util.ArrayList;
07: import java.util.Iterator;
08:
09: /**
10: * DataKeeper keeps track of the current state of the user's list. All
11: * modifications to the user's list are made by calling DataKeeper's methods.
12: */
13: public class DataKeeper {
14:
15: private final int MAX_NUM = 5;
16: private ArrayList userList;
17:
18: public DataKeeper() {
19: userList = new ArrayList();
20: }
21:
22: public void addListItem(Product newProd) {
23: for (Iterator iter = userList.iterator(); iter.hasNext();) {
24: if (((Product) iter.next()).getId().equals(newProd.getId())) {
25: iter.remove();
26: }
27: }
28:
29: userList.add(0, newProd);
30:
31: if (userList.size() > MAX_NUM) {
32: userList.remove(MAX_NUM);
33: }
34: }
35:
36: public int getListSize() {
37: return userList.size();
38: }
39:
40: public ArrayList getList() {
41: return userList;
42: }
43:
44: public Product getCurrent() {
45: if (getListSize() > 0)
46: return (Product) userList.get(0);
47:
48: return null;
49: }
50:
51: }
|