《jsp网站开发技术》中的源代码(清华大学出版社)
源代码在线查看: cart.java
/*
* Copyright (c) 2000 Javacat. All Rights Reserved.
*
* @(#)Cart.java 12/10/2000
*/
package com.javacat.jsp.beans.ebiz;
import javax.servlet.http.*;
import java.util.Vector;
import java.util.Enumeration;
/**
*The Cart bean is used as a dummy cart for client
*to hold their goods when they're shopping online.
*/
public class Cart{
Vector goods= new Vector();
String submit = null;
int ablumID = -1;
String item=null;
//add new goods
private void add(String id) {
if(!goods.contains(id))
goods.addElement(id);
}
//remove the specified goods
private void remove(String[] ids) {
for(int i=0;i goods.removeElement(ids[i]);
}
}
public void setItem(String id) {
item = id;
}
public void setSubmit(String s) {
if(s==null||s.equals(""))
return;
else submit=s;
}
public String[] getAllGoods() {
String[] sg = new String[goods.size()];
goods.copyInto(sg);
return sg;
}
public void processRequest(HttpServletRequest request) {
// null value for submit - user hit enter instead of clicking on
// "add" or "remove"
if (submit == null&&request.getParameter("remove")==null){
reset();
return;
}
if (submit.equals("add")){
add(item);
this.setAlbumID(item);
}
else if (request.getParameter("remove")!=null){
java.util.Enumeration pns=request.getParameterNames();
String pn;
String p;
Vector vid=new Vector();
while(pns.hasMoreElements()){
pn=(String)pns.nextElement();
p=request.getParameter(pn);
if(pn.startsWith("c")&&p.equals("on")){
vid.add(pn.substring(1));
}
}
if(vid.size()>0){
String[] ids=new String[vid.size()];
vid.copyInto(ids);
remove(ids);
}
}
// reset at the end of the request
reset();
}
// reset
private void reset() {
submit = null;
item = null;
}
public synchronized void setAlbumID(String ite)
{
this.ablumID=Integer.parseInt(ite);
}
public synchronized int getAlbumID()
{
return this.ablumID;
}
public Album[] getOrderedAlbums()
{
String[] items=this.getAllGoods();
Album[] albums=new Album[items.length];
for(int i=0;i {
albums[i]=new Album(Integer.parseInt(items[i]));
}
return albums;
}
}