import java.util.ArrayList;
import java.util.List;
import cn.itcast.bean.book.OrderContactInfo;
import cn.itcast.bean.book.OrderDeliverInfo;
import cn.itcast.bean.book.PaymentWay;
/**
* 购物车
*/
public class BuyCart {
/* 购物项 */
List<BuyItem> items = new ArrayList<BuyItem>();
private OrderDeliverInfo deliverInfo;
private OrderContactInfo contactInfo;
private Boolean buyerIsrecipients;
/* 支付方式 */
private PaymentWay paymentWay;
/* 配送费 */
private float deliveFee = 10f;
/* 用户附言 */
private String note;
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public float getDeliveFee() {
return deliveFee;
}
public void setDeliveFee(float deliveFee) {
this.deliveFee = deliveFee;
}
public PaymentWay getPaymentWay() {
return paymentWay;
}
public void setPaymentWay(PaymentWay paymentWay) {
this.paymentWay = paymentWay;
}
public Boolean getBuyerIsrecipients() {
return buyerIsrecipients;
}
public void setBuyerIsrecipients(Boolean buyerIsrecipients) {
this.buyerIsrecipients = buyerIsrecipients;
}
public OrderDeliverInfo getDeliverInfo() {
return deliverInfo;
}
public void setDeliverInfo(OrderDeliverInfo deliverInfo) {
this.deliverInfo = deliverInfo;
}
public OrderContactInfo getContactInfo() {
return contactInfo;
}
public void setContactInfo(OrderContactInfo contactInfo) {
this.contactInfo = contactInfo;
}
/**
* 添加商品
* @param buyItem 购物项
*/
public void addBuyItem(BuyItem buyItem){
if(this.items.contains(buyItem)){
for(BuyItem item : this.items){
if(item.equals(buyItem)){
item.setAmount(item.getAmount()+1);
break;
}
}
}else{
this.items.add(buyItem);
}
}
/**
* 删除购物项
* @param buyItem 购物项
*/
public void deleteBuyItem(BuyItem buyItem){
if(this.items.contains(buyItem)) this.items.remove(buyItem);
}
/**
* 清空购物车
*/
public void deleteAll(){
this.items.clear();
}
public List<BuyItem> getItems() {
return items;
}
/**
* 计算产品总销售价
* @return
*/
public float getTotalSellPrice(){
float result = 0f;
for(BuyItem item : this.items){
result += item.getProduct().getSellprice() * item.getAmount();
}
return result;
}
/**
* 计算总节省金额
* @return
*/
public float getTotalSavePrice(){
float result = 0f;
for(BuyItem item : this.items){
result += item.getProduct().getMarketprice() * item.getAmount();
}
return result- getTotalSellPrice();
}
/**
* 计算订单的总费用
*/
public float getOrderTotalPrice(){
return getTotalSellPrice()+ this.deliveFee;
}
}
--------------------------------------------------------------------------------
import cn.itcast.bean.product.ProductInfo;
public class BuyItem {
/* 商品购买数量 */
private Integer amount = 1;
/* 购买的商品 */
private ProductInfo product;
public BuyItem(){}
public BuyItem(ProductInfo product) {
this.product = product;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public ProductInfo getProduct() {
return product;
}
public void setProduct(ProductInfo product) {
this.product = product;
}
@Override
public int hashCode() {
String code = this.product.getId()+ "-";
if(!this.product.getStyles().isEmpty()){
Integer styleid = this.product.getStyles().iterator().next().getId();
code += styleid;
}
return code.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final BuyItem other = (BuyItem) obj;
if (product == null) {
if (other.product != null)
return false;
} else if (!product.equals(other.product))
return false;
if(product.getStyles().size()!=other.product.getStyles().size()){
return false;
}
Integer styleid = product.getStyles().iterator().next().getId();
Integer otherstyleid = other.product.getStyles().iterator().next().getId();
if(!styleid.equals(otherstyleid)) return false;
return true;
}
}
-----------------------------------------------------------------------
public class PageIndex {
private long startindex;
private long endindex;
public PageIndex(long startindex, long endindex) {
this.startindex = startindex;
this.endindex = endindex;
}
public long getStartindex() {
return startindex;
}
public void setStartindex(long startindex) {
this.startindex = startindex;
}
public long getEndindex() {
return endindex;
}
public void setEndindex(long endindex) {
this.endindex = endindex;
}
public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){
long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);
long endpage = currentPage+viewpagecount/2;
if(startpage<1){
startpage = 1;
if(totalpage>=viewpagecount) endpage = viewpagecount;
else endpage = totalpage;
}
if(endpage>totalpage){
endpage = totalpage;
if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;
else startpage = 1;
}
return new PageIndex(startpage, endpage);
}
}
-------------------------------------------------------------------
import java.util.List;
public class PageView<T> {
/** 分页数据 **/
private List<T> records;
/** 页码开始索引和结束索引 **/
private PageIndex pageindex;
/** 总页数 **/
private long totalpage = 1;
/** 每页显示记录数 **/
private int maxresult = 12;
/** 当前页 **/
private int currentpage = 1;
/** 总记录数 **/
private long totalrecord;
/** 页码数量 **/
private int pagecode = 10;
/** 要获取记录的开始索引 **/
public int getFirstResult() {
return (this.currentpage-1)*this.maxresult;
}
public int getPagecode() {
return pagecode;
}
public void setPagecode(int pagecode) {
this.pagecode = pagecode;
}
public PageView(int maxresult, int currentpage) {
this.maxresult = maxresult;
this.currentpage = currentpage;
}
public void setQueryResult(QueryResult<T> qr){
setTotalrecord(qr.getTotalrecord());
setRecords(qr.getResultlist());
}
public long getTotalrecord() {
return totalrecord;
}
public void setTotalrecord(long totalrecord) {
this.totalrecord = totalrecord;
setTotalpage(this.totalrecord%this.maxresult==0? this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1);
}
public List<T> getRecords() {
return records;
}
public void setRecords(List<T> records) {
this.records = records;
}
public PageIndex getPageindex() {
return pageindex;
}
public long getTotalpage() {
return totalpage;
}
public void setTotalpage(long totalpage) {
this.totalpage = totalpage;
this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage);
}
public int getMaxresult() {
return maxresult;
}
public int getCurrentpage() {
return currentpage;
}
}
-----------------------------------------------------------------------------
import java.util.List;
public class QueryResult<T> {
private List<T> resultlist;
private long totalrecord;
public List<T> getResultlist() {
return resultlist;
}
public void setResultlist(List<T> resultlist) {
this.resultlist = resultlist;
}
public long getTotalrecord() {
return totalrecord;
}
public void setTotalrecord(long totalrecord) {
this.totalrecord = totalrecord;
}
}