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