######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 + "]";
}
}
购物车功能:
①先有购物项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 + "]";
}
}
相关文章推荐
- ###Jsp+Servlet购物商城day04.3:订单模块Order实体设计失误。【Bean属性设计两大原则】-MyBeanUtils使用注意事项。
- Eclipse快速上手EJB -- 2. 设计一个实体Bean
- 将Bean类保存在本地,已JSON的形式提交,便于其他页面读取。常用于购物车
- win2k环境下基于JBOSS的J2EE开发实践----之四:BMP实体Bean的编写与设计
- linux可以借鉴的设计思想(笔记)
- 记录一些比较有趣的网站,来给自己的网站设计积累思想
- 一对一对多关系与购物车、订单设计
- 数据库索引:唯一索引【便捷的无重复插入记录的方法(基于mysql)和索引设计思想小结】
- 以MyBatis+SpringMVC3.0实现的,借鉴了Hibernate设计思想,采用封装、抽象、继承的设计思想,做到了数据与相应的操作的高内聚低耦合的实现
- Eclipse快速上手EJB -- 2. 设计一个实体Bean
- 读-《c++设计新思维-泛型编程与设计模式之应用》经典记录(英文书名:《modern c++ design》)
- GoF 23个经典的设计模式03--创建模式之工厂方法(未完代续)
- [记录]由Virtools的设计想到的实体系统...
- Eclipse快速上手EJB -- 2. 设计一个实体Bean
- 记录设计思想的重要性 推荐
- 购物车设计思想之最佳
- (非常经典)Javascript继承机制的设计思想
- 06-03 Java 面向对象思想概述、开发设计特征,类和对象的定义使用,对象内存图
- jsp+javaBean(模拟购物车)(DAO设计模式的应用)
- jstl 遇到的那些坑。很有名的坑,但容易忘。记录一下,便于复习