购物车模块设计

来源:互联网 发布:yum y install wget 编辑:程序博客网 时间:2024/04/29 20:14

第一章          

Ø  购物车设计 =两个类:购物车BuyCart + 购物项BuyItem。。。+ 产品类ProductInfo +商品样式ProductStyle

 

Ø  BuyCart

import java.util.ArrayList;

import java.util.List;

 

publicclass BuyCart {

    private List<BuyItem>items = new ArrayList<BuyItem>();

   

    public List<BuyItem> getItems() {

       returnitems;

    }

 

    /**

     * 添加购物项

     * @param item

     */

    publicvoid addItem(BuyItem item){

       if(!items.contains(item)){

           items.add(item);

       }else{//如果已经存在该购物项,就累加其购买数量

           for(BuyItem bi :items){

              if(bi.equals(item)){

                  bi.setAmount(bi.getAmount()+1);

                  break;

              }

           }

       }

    }

    /**

     * 删除所有购物项

     */

    publicvoid removeAll(){

       items.clear();

    }

    /**

     * 删除购物项

     * @param item

     */

    publicvoid removeBuyItem(BuyItem item){

       if(items.contains(item)){

           items.remove(item);

       }

    }

 

    /**

     * 获取应付总金额

     * @return

     */

    publicfloat getTotalPrice(){

       float total = 0f;

       for(BuyItem bi :this.items){

           total += bi.getProduct().getSellprice() * bi.getAmount();

       }

       return total;

    }

    /**

     * 获取市场价总金额

     * @return

     */

    publicfloat getTotalMarketPrice(){

       float total = 0f;

       for(BuyItem bi :this.items){

           total += bi.getProduct().getMarketprice() * bi.getAmount();

       }

       return total;

    }

    /**

     * 总节省金额

     * @return

     */

    publicfloat getTotalSavedPrice(){

       return getTotalMarketPrice()-getTotalPrice();

    }

}

 

Ø  BuyItem

import com.itcast.bean.product.ProductInfo;

import com.itcast.bean.product.ProductStyle;

 

publicclass BuyItem {

    /**所购买的商品 **/

    private ProductInfoproduct;

    /**购买数量 **/

    privateintamount;

   

    public BuyItem(ProductInfo product) {

       this.product = product;

    }

    public BuyItem(ProductInfo product,int amount) {

       this.product = product;

       this.amount = amount;

    }

    public ProductInfo getProduct() {

       returnproduct;

    }

    publicvoid setProduct(ProductInfo product) {

       this.product = product;

    }

    publicint getAmount() {

       returnamount;

    }

    publicvoid setAmount(int amount) {

       this.amount = amount;

    }

   

    @Override

    publicint hashCode() {//产品id和产品类型Id组成比较的字符串,得到对应的hashCode

       String buyitemid = product.getId()+ "-"    if(product.getStyles().size()>0){

           buyitemid += product.getStyles().iterator().next().getId();

       }

       return buyitemid.hashCode();

    }

   

    @Override

    publicboolean equals(Object obj) {

       if (this == obj)

           returntrue;

       if (obj ==null)

           returnfalse;

       if (getClass() != obj.getClass())

           returnfalse;

       final BuyItem other = (BuyItem) obj;

       //自动生成的产品的比较

       if (product ==null) {

           if (other.product !=null)

              returnfalse;

       } elseif (!product.equals(other.product))

           returnfalse;    

        if(product.getStyles().size()!=other.product.getStyles().size()){

           returnfalse;

       }

       //手写的产品样式的比较

       if(product.getStyles().size()>0){

           ProductStyle style = product.getStyles().iterator().next();

           ProductStyle otherstyle = other.product.getStyles().iterator().next();

           if(!style.equals(otherstyle))returnfalse;     

       }     

       returntrue;

    }

   

}

 

Ø  ProductInfo

 

import java.io.Serializable;

import java.util.Date;

import java.util.HashSet;

import java.util.Set;

 

import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.EnumType;

import javax.persistence.Enumerated;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.Lob;

import javax.persistence.ManyToOne;

import javax.persistence.OneToMany;

import javax.persistence.OrderBy;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;

import javax.persistence.Transient;

 

@Entity

publicclass ProductInfoimplements Serializable{

    privatestaticfinallongserialVersionUID = -8860864584425256200L;

    private Integerid;

    /**货号 **/

    private Stringcode;

    /**产品名称 **/

    private Stringname;

    /**品牌 **/

    private Brandbrand;

    /**型号 **/

    private Stringmodel;

    /**底价(采购进来的价格) **/

    private Floatbaseprice;

    /**市场价 **/

    private Floatmarketprice;

    /**销售价 **/

    private Floatsellprice;

    /**重量单位: **/

    private Integerweight;

    /**产品简介 **/

    private Stringdescription;

    /**购买说明 **/

    private Stringbuyexplain;

    /**是否可见 **/

    private Booleanvisible = true;

    /**产品类型 **/

    private ProductTypetype;

    /**上架日期 **/

    private Datecreatedate = new Date();

    /**人气指数 **/

    private Integerclickcount = 1;

    /**销售量 **/

    private Integersellcount = 0;

    /**是否推荐 **/

    private Booleancommend = false;

    /**性别要求 **/

    private Sexsexrequest = Sex.NONE;

    /**产品样式 **/

    private Set<ProductStyle>styles = new HashSet<ProductStyle>();

   

    public ProductInfo(Integer id) {

       this.id = id;

    }

    public ProductInfo() {}

   

    @Transient

    public Float getSavedPrice(){

       returnmarketprice-sellprice;

    }

   

    @OneToMany(cascade={CascadeType.REMOVE,CascadeType.PERSIST}, mappedBy="product")

    @OrderBy("visible desc, id asc")

    public Set<ProductStyle> getStyles() {

       returnstyles;

    }

    publicvoid setStyles(Set<ProductStyle> styles) {

       this.styles = styles;

    }

    /**

     * 从样式集合中删除指定样式

     * @param style

     */

    publicvoid removeProductStyle(ProductStyle style){

       if(this.styles.contains(style)){

           this.styles.remove(style);

           style.setProduct(null);

       }

    }  

    /**

     * 添加样式到样式集合

     * @param style

     */

    publicvoid addProductStyle(ProductStyle style){

       if(!this.styles.contains(style)){

           this.styles.add(style);

           style.setProduct(this);

       }

    }

   

    @Id@GeneratedValue

    public Integer getId() {

       returnid;

    }

    publicvoid setId(Integer id) {

       this.id = id;

    }

    @Column(length=30)

    public String getCode() {

       returncode;

    }

    publicvoid setCode(String code) {

       this.code = code;

    }

    @Column(length=50,nullable=false)

    public String getName() {

       returnname;

    }

    publicvoid setName(String name) {

       this.name = name;

    }

    @ManyToOne(cascade=CascadeType.REFRESH)

    @JoinColumn(name="brandid")

    public Brand getBrand() {

       returnbrand;

    }

    publicvoid setBrand(Brand brand) {

       this.brand = brand;

    }

    @Column(length=20)

    public String getModel() {

       returnmodel;

    }

    publicvoid setModel(String model) {

       this.model = model;

    }

    @Column(nullable=false)

    public Float getBaseprice() {

       returnbaseprice;

    }

    publicvoid setBaseprice(Float baseprice) {

       this.baseprice = baseprice;

    }

    @Column(nullable=false)

    public Float getMarketprice() {

       returnmarketprice;

    }

    publicvoid setMarketprice(Float marketprice) {

       this.marketprice = marketprice;

    }

    @Column(nullable=false)

    public Float getSellprice() {

       returnsellprice;

    }

    publicvoid setSellprice(Float sellprice) {

       this.sellprice = sellprice;

    }

    public Integer getWeight() {

       returnweight;

    }

    publicvoid setWeight(Integer weight) {

       this.weight = weight;

    }

    @Lob@Column(nullable=false)

    public String getDescription() {

       returndescription;

    }

    publicvoid setDescription(String description) {

       this.description = description;

    }

    @Column(length=30)

    public String getBuyexplain() {

       returnbuyexplain;

    }

    publicvoid setBuyexplain(String buyexplain) {

       this.buyexplain = buyexplain;

    }

    @Column(nullable=false)

    public Boolean getVisible() {

       returnvisible;

    }

    publicvoid setVisible(Boolean visible) {

       this.visible = visible;

    }

    @ManyToOne(cascade=CascadeType.REFRESH,optional=false)

    @JoinColumn(name="typeid")

    public ProductType getType() {

       returntype;

    }

    publicvoid setType(ProductType type) {

       this.type = type;

    }

    @Temporal(TemporalType.DATE)

    public Date getCreatedate() {

       returncreatedate;

    }

    publicvoid setCreatedate(Date createdate) {

       this.createdate = createdate;

    }

    @Column(nullable=false)

    public Integer getClickcount() {

       returnclickcount;

    }

    publicvoid setClickcount(Integer clickcount) {

       this.clickcount = clickcount;

    }

    @Column(nullable=false)

    public Integer getSellcount() {

       returnsellcount;

    }

    publicvoid setSellcount(Integer sellcount) {

       this.sellcount = sellcount;

    }

    @Column(nullable=false)

    public Boolean getCommend() {

       returncommend;

    }

    publicvoid setCommend(Boolean commend) {

       this.commend = commend;

    }

    @Enumerated(EnumType.STRING)@Column(length=5,nullable=false)

    public Sex getSexrequest() {

       returnsexrequest;

    }

    publicvoid setSexrequest(Sex sexrequest) {

       this.sexrequest = sexrequest;

    }

   

    @Override

    publicint hashCode() {

       finalint prime = 31;

       int result = 1;

       result = prime * result + ((id ==null) ? 0 :id.hashCode());

       return result;

    }

    @Override

    publicboolean equals(Object obj) {

       if (this == obj)

           returntrue;

       if (obj ==null)

           returnfalse;

       if (getClass() != obj.getClass())

           returnfalse;

       final ProductInfo other = (ProductInfo) obj;

       if (id ==null) {

           if (other.id !=null)

              returnfalse;

       } elseif (!id.equals(other.id))

           returnfalse;

       returntrue;

    }

}

 

 

Ø  ProductStyle

 

import java.io.Serializable;

 

import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.ManyToOne;

import javax.persistence.Transient;

 

@Entity

publicclass ProductStyleimplements Serializable{

    privatestaticfinallongserialVersionUID = -4926119953511144279L;

    private Integerid;

    /**样式的名称 **/

    private Stringname;

    /**图片 **/

    private Stringimagename;

    /**是否可见 **/

    private Booleanvisible = true;

    private ProductInfoproduct;

   

    public ProductStyle() {}

   

    public ProductStyle(Integer id) {

       this.id = id;

    }

 

    public ProductStyle(String name, String imagename) {

       this.name = name;

       this.imagename = imagename;

    }

    @ManyToOne(cascade=CascadeType.REFRESH,optional=false)

    @JoinColumn(name="productid")

    public ProductInfo getProduct() {

       returnproduct;

    }

    publicvoid setProduct(ProductInfo product) {

       this.product = product;

    }

    @Id@GeneratedValue

    public Integer getId() {

       returnid;

    }

    publicvoid setId(Integer id) {

       this.id = id;

    }

    @Column(length=30,nullable=false)

    public String getName() {

       returnname;

    }

    publicvoid setName(String name) {

       this.name = name;

    }

    @Column(length=40,nullable=false)

    public String getImagename() {

       returnimagename;

    }

    publicvoid setImagename(String imagename) {

       this.imagename = imagename;

    }

    @Column(nullable=false)

    public Boolean getVisible() {

       returnvisible;

    }

    publicvoid setVisible(Boolean visible) {

       this.visible = visible;

    }

    @Transient

    public String getImageFullPath(){

       return"/images/product/"+this.getProduct().getType().getTypeid()+"/"+

       this.getProduct().getId()+"/prototype/"+this.imagename;

    }

    @Transient

    public String getImage140FullPath(){

       return"/images/product/"+this.getProduct().getType().getTypeid()+"/"+

       this.getProduct().getId()+"/140x/"+ this.imagename;

    }

   

    @Override

    publicint hashCode() {

       finalint prime = 31;

       int result = 1;

       result = prime * result + ((id ==null) ? 0 :id.hashCode());

       return result;

    }

    @Override

    publicboolean equals(Object obj) {

       if (this == obj)

           returntrue;

       if (obj ==null)

           returnfalse;

       if (getClass() != obj.getClass())

           returnfalse;

       final ProductStyle other = (ProductStyle) obj;

       if (id ==null) {

           if (other.id !=null)

              returnfalse;

       } elseif (!id.equals(other.id))

           returnfalse;

       returntrue;

    }

}

 

原创粉丝点击