cartbean.java
来自「这是一个在线订购的例子」· Java 代码 · 共 45 行
JAVA
45 行
package mybeans;
import java.util.ArrayList; // 要求JDK1.2以上
public class CartBean {
private String[] itemsSelected = null;
/*被选的商品*/
private String action = null;
/*进行的操作*/
private ArrayList itemsList = new ArrayList();
/*购物车的全部商品*/
public synchronized void setAction (String newAction) {
action = newAction;
if (action.startsWith("clear")) { //判断是否选择清空操作
itemsList.clear();//清空购物车
}
}
public synchronized void setItemsSelected (String[] newItems) {
if (newItems != null) { // some items selected
for (int i = 0; i < newItems.length; i++) {
if (action.startsWith("add")) { //加入到购物车中
itemsList.add(newItems[i]);
} else if (action.startsWith("move")) { //移出购物车
/*寻找最后一个所选择的商品,并移出购物车*/
int index = itemsList.lastIndexOf(newItems[i]);
if (index != -1) {
itemsList.remove(index);
}
}
}
}
}
public int countItems () {
return itemsList.size();
}
/*用来得到指定索引的商品*/
public Object getItem(int index)
{
return itemsList.get(index);
}
/*用来得到购物车的全部商品*/
public ArrayList getOrder()
{
return itemsList;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?