您的位置:首页 > 其它

######03.经典实体Bean设计:购物项-购物车;订单项-订单。---记录。便于复习。借鉴设计思想

2017-09-12 11:41 453 查看
Bean设计 总思路:(有轻逻辑的Bean)

购物车功能:

①先有购物项CartItem,再有购物车Cart 对其封装。-----和“分页”的PageBean封装类似。都是为了便于页面取值显示数据。

===【当然,这里Cart对CartItem的封装。除了页面取值方便,更重要的是:购物项和购物车 有个各自的轻逻辑(业务需要):

CartItem:除了表字段映射的属性,还有“小计总价”这个轻逻辑。

Cart:除了表字段映射的属性,还有“总价”、“购物积分”这个轻逻辑。

此外,###【最容易忽视,和表字段属性弄混的业务属性】,就是 封装CartItem的Map<String,CartItem> map属性。

 】

订单功能:(和购物车思想类似)

②先有OrderItem,再有Orders(名也可以叫Order)。

===============Bean代码如下:

购物车功能:

package cn.itcast.domain;

import java.io.Serializable;

public class CartItem implements Serializable{
private Product pro;
private int count;
private double total;//价格小计
public CartItem() {
super();
}
public CartItem(Product pro, int count, double total) {
super();
this.pro = pro;
this.count = count;
this.total = total;
}
public Product getPro() {
return pro;
}
public void setPro(Product pro) {
this.pro = pro;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getTotal() {
return total = pro.getShop_price()*count;
}
@Override
public String toString() {
return "CartItem [pro=" + pro.getPname() + ", count=" + count + ", total=" + getTotal() + "]";
}

}

------

package cn.itcast.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/*=====【思想和PageBean一样的,都是要封装。】
========【把CartItem封装到Cart而不是 集合。因为整个还要计算所有CartItem的总价,总积分。
====正因为存在这些轻逻辑。所以再次封装一个实体。便于操作。面向对象。
==###==【一个Cart,CartItem通常不止一个】,所以要存到集合里。

===涉及选哪种集合。===哪种集合最终都可以实现。只是实现语法不同。
如果选用
List,可重复,需要判断重复。Cart实体还要重写equals方法;
Set,不可重复。无序。
Map,不可重复。无序。?

* */

public class Cart implements Serializable {
// private Map<String,CartItem> items;
private Map<String,CartItem> items = new LinkedHashMap<String,CartItem>();
private int score;//购物积分。
private double totalMoney;

public Cart() {
super();
}

public Cart(Map<String,CartItem> items, int score, double totalMoney) {
super();
this.items = items;
this.score = score;
this.totalMoney = totalMoney;
}

/*
public Map<String,CartItem> getItems() {
return items;
}
//========######====【这个写错,导致页面显示不了。】
//======property Pro is not found.====?页面取得是ProductList不是Map
*/
public Collection<CartItem> getItems() {
return items.values();
}

public Map<String, CartItem> getItemsMap() {
return items;
}

public List<CartItem> getItems2() {
Collection<CartItem> collection = items.values();
//collection.remove(o)//ok

return (List<CartItem>) items.values();
}
//
public void delByPid(String pid){
items.remove(pid);
}
public void setItems(Map<String,CartItem> items) {
this.items = items;
}

/*
public void setCartItems(Map<String,CartItem> items) {
this.items = items;
}
==========购物车添加CartItem,不需要每次都添加一个Map集合(不需要set)。
改进:新建一个Map。每次Map里添加一个。
*/
public void addCartItem(CartItem cartItem ) {
/*//===v1【每种商品只能添加一个,pid 重复,添加覆盖】
this.items.put(cartItem.getPro().getPid(), cartItem);*/

//===v2
CartItem im = items.get(cartItem.getPro().getPid());
if (im==null) {
items.put(cartItem.getPro().getPid(), cartItem);
} else {
cartItem.setCount(cartItem.getCount()+im.getCount());
items.put(cartItem.getPro().getPid(), cartItem);
}
}

public int getScore() {

return (int) (getTotalMoney()/10);
}
public void setScore(int score) {
this.score = score;
}
public double getTotalMoney() {

/*//===v1
for (int i = 0; i < items.size(); i++) {
totalMoney+=items.get(i).getTotal();
}*/

/*//===v2
Collection values = items.values();
System.out.println(values);
for (Iterator iterator = values.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
System.out.println();

}*/

int total=0;
//遍历所有购物项,将小计相加。
Collection<CartItem> item = items.values();
System.out.println(item);
for (CartItem value: item) {
total+=value.getTotal();
}

return total;
}
/*public void setTotalMoney(double totalMoney) {
this.totalMoney = totalMoney;
}*/
@Override
public String toString() {
return "Cart [items=" + items + ", score=" + getScore() + ", totalMoney=" + getTotalMoney() + "]";
}

}


订单功能:(和购物车思想类似)

package cn.itcast.domain;

import java.util.List;

public class OrderItem {

private String itemid;
private int count;
private double subtotal;
/*private List product ;
private List order ;*/
private Product product ;
private Orders order ;
public OrderItem() {
super();
// TODO Auto-generated constructor stub
}
public OrderItem(String itemid, int count, double subtotal, Product product, Orders order) {
super();
this.itemid = itemid;
this.count = count;
this.subtotal = subtotal;
this.product = product;
this.order = order;
}
public String getItemid() {
return itemid;
}
public void setItemid(String itemid) {
this.itemid = itemid;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getSubtotal() {
// return subtotal;
//====bean 加入业务轻逻辑。
subtotal=product.getShop_price()*count;
return subtotal;
}
/*
//===subtotal是计算出来的,不用set赋值
public void setSubtotal(double subtotal) {
this.subtotal = subtotal;
}*/
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Orders getOrder() {
return order;
}
public void setOrder(Orders order) {
this.order = order;
}
@Override
public String toString() {
return "OrderItem [itemid=" + itemid + ", count=" + count + ", subtotal=" + subtotal + ", product=" + product
+ ", order=" + order + "]";
}

}


-------
package cn.itcast.domain;

import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;

public class Orders {
private String oid;
private Date date;
private double total;
private int state;
//对着关系图写。sql语句找起来还是费点劲的。
private String address;
private String name;
private String telephone;
// private String uid ;
private User user;

//================#######===========【加深理解:Bean属性有 两种。业务需要,表字段映射】
//订单和订单项。1:n关系。所以Orders表里不需要添加 itemid属性。
//但是业务逻辑上: Orders 包含 多个OrderItem,Orders本就是用来封装OderItem的。
//(和分页 PageBean类似,封装数据,作用只不过是:方便页面取值)
//所以:和【Cart封装CartItem一样,这里也要添加一个Map<Stirng pid,OderItem item> 的map 封装多个订单项。 】
//===所以说:这个map属性是 业务需要。其他属性是表字段映射。
private LinkedHashMap<String,OrderItem> orderItems = new LinkedHashMap<String,OrderItem>();

public Collection<OrderItem> getOrderItems() {
return orderItems.values();
}
public void setOrderItems(LinkedHashMap<String, OrderItem> map) {
this.orderItems = map;
}
public Orders() {
super();
}
public Orders(String oid, Date date, double total, int state, String address, String name, String telephone,
User user) {
super();
this.oid = oid;
this.date = date;
this.total = total;
this.state = state;
this.address = address;
this.name = name;
this.telephone = telephone;
this.user = user;
}
public String getOid() {
return oid;
}
public void setOid(String oid) {
this.oid = oid;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public double getTotal() {
Collection<OrderItem> items = getOrderItems();
int total=0;
for (OrderItem item : items) {
total += item.getSubtotal();
}
return total;
}
public void setTotal(double total) {
this.total = total;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "Orders [oid=" + oid + ", date=" + date + ", total=" + getTotal() + ", state=" + state + ", address="
+ address + ", name=" + name + ", telephone=" + telephone + ", user=" + user + "]";
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐