import java.util.ArrayList;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.apple.struts.Book;
import com.apple.struts.ShoppingCart;
import com.apple.struts.form.ShoppingForm;
public class ShoppingAction extends Action {
@SuppressWarnings("unchecked")
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ShoppingForm shoppingForm = (ShoppingForm) form;
HttpSession newSession = request.getSession(false);
ArrayList tempBook = new ArrayList();
ShoppingCart myShoppingCart = (ShoppingCart) newSession.getAttribute("myShoppingCart");
// ShoppingCart totlebooktemp = new MyBean().Getbook();
Book[] totlebook = (Book[]) this.servlet.getServletContext().getAttribute("totlebooks");
int totlebooklength = totlebook.length;
request.setAttribute("totlebook", totlebook);
request.setAttribute("totlebooklength", totlebooklength);
boolean couldRemove = false;
boolean couldShop = false;
boolean couldCheckOut = false;
Enumeration temp = null;
temp = request.getParameterNames();
Enumeration temp1 = null;
temp1 = request.getParameterNames();
if (!temp.hasMoreElements()) {
request.setAttribute("books", myShoppingCart.Booklist());
request.setAttribute("bookslength",
myShoppingCart.Booklist().length);
return (mapping.findForward("continue"));
}
while (temp.hasMoreElements()) {
String name = (String) temp.nextElement();
if (name.equals("remove")) {
couldRemove = true;
}
if (name.equals("shop")) {
couldShop = true;
}
if (name.equals("checkout")) {
couldCheckOut = true;
}
}
if (couldRemove) {
Book yy = new Book();
tempBook = myShoppingCart.booklist;
while (temp1.hasMoreElements()) {
String name = (String) temp1.nextElement();
if (!name.equals("remove")) {
for (int i = 0; i < tempBook.size(); i++) {
yy = (Book) tempBook.get(i);
String name1 = yy.getName();
if (name1.equals(name)) {
tempBook.remove(yy);
}
}
}
myShoppingCart.booklist = tempBook;
}
request.setAttribute("books", myShoppingCart.Booklist());
request.setAttribute("bookslength",
myShoppingCart.Booklist().length);
return (mapping.findForward("continue"));
} else {
/** ****************** 加书 **************** */
while (temp1.hasMoreElements()) {
String name = (String) temp1.nextElement();
if (!name.equals("shop") && !name.equals("checkout")
&& !name.equals("remove")) {
String prize = (String) request.getParameter(name);
String isbn = "0";
Book newBook = new Book();
newBook.setName(name);
newBook.setPrize(prize);
newBook.setIsbn(isbn);
myShoppingCart.addBook(newBook);
}
}
}
if (couldShop) {
request.setAttribute("books", myShoppingCart.Booklist());
request.setAttribute("bookslength",
myShoppingCart.Booklist().length);
return (mapping.findForward("continue"));
}
if (couldCheckOut) {
return (mapping.findForward("checkout"));
}
return null;
}
}
发表于 @ 2007年03月29日 19:59:00|编辑