shoppingaction.java
来自「有简单的网上书店需求及设计流程」· Java 代码 · 共 221 行
JAVA
221 行
package org.wiely.action;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import org.wiely.model.Cart;
import org.wiely.service.BookService;
import org.wiely.service.OrdersService;
import org.wiely.vo.Book;
import org.wiely.vo.OrderUser;
import org.wiely.vo.Orders;
import org.wiely.vo.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* shoppingAction extends ActionSupport,it has three methods. one is
* addCart(),the other is updateCart(),the last is checkout(). the three method
* is provides the most funcitons of shopping online.
*
* @author wiely
* @version 1.0
* @param bookService
* @param ordersService
* @param isbn
* @param quantity
* @param name
* @param adress
* @param totalprice
* @param code
* @param phone
*/
@SuppressWarnings("serial")
public class ShoppingAction extends ActionSupport {
protected BookService bookService;
protected OrdersService ordersService;
String isbn;
int quantity;
String name;
String adress;
String code;
String phone;
public BookService getBookService() {
return bookService;
}
public void setBookService(BookService bookService) {
this.bookService = bookService;
}
public OrdersService getOrdersService() {
return ordersService;
}
public void setOrdersService(OrdersService ordersService) {
this.ordersService = ordersService;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
/**
* @see addCart() *
* @return String
* @throws Exception
*/
@SuppressWarnings("unchecked")
public String addCart() throws Exception {
Book book = bookService.queryBookByBookisbn(isbn);
Orders orders = new Orders();
orders.setBook(book);
orders.setBookacount(quantity);
Map session = ActionContext.getContext().getSession();
Cart cart;
if (session.get("cart") != null) {
cart = (Cart) session.get("cart");
} else {
cart = new Cart();
}
cart.addBook(isbn, orders);
System.out.println(quantity);
session.put("cart", cart);
return "addsuccess";
}
/**
*
* @return String
* @throws Exception
*/
@SuppressWarnings("unchecked")
public String updateCart() throws Exception {
Map<String, Cart> session = ActionContext.getContext().getSession();
Cart cart;
if (session.get("cart") != null) {
cart = (Cart) session.get("cart");
} else {
cart = new Cart();
}
cart.updateCart(isbn, quantity);
session.put("cart", cart);
return "updatesuccess";
}
@SuppressWarnings("unchecked")
public String removeBook() throws Exception {
Map<String, Cart> session = ActionContext.getContext().getSession();
Cart cart = (Cart) session.get("cart");
cart.removeBook(isbn);
return "removesuccess";
}
/**
*
* @return String
* @throws Exception
* @see checkout is the most method of shoppingAction it process checkout
* user's orders.
* @note before checkout ,the params should be validate. because ,we must
* assure the data is valid.
*/
@SuppressWarnings("unchecked")
public String checkout() throws Exception {
System.out.println("name"+name);
Map session = ActionContext.getContext().getSession();
Map request = (Map) ActionContext.getContext().get("request");
User user = (User) session.get("user");
Cart cart = (Cart) session.get("cart");
OrderUser orderUser = new OrderUser();
orderUser.setAdress(adress);
orderUser.setCode(code);
orderUser.setName(name);
orderUser.setOrderdate(new Date());
String totalprice = String.valueOf(cart.getTotalPrice());
orderUser.setTotalprice(totalprice);
orderUser.setUser(user);
orderUser.setPhone(phone);
for (Iterator it = cart.getItems().values().iterator(); it.hasNext();) {
Orders order = (Orders) it.next();
order.setOrderuser(orderUser);
orderUser.getOrderses().add(order);
}
ordersService.saveOrders(orderUser);
request.put("orderUser", orderUser);
return SUCCESS;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?