購物網站2:基本實體類---購物車----購物項----pageIndex----pageView----QueryResult

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;
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章