第1个回答 2017-08-01
根据你上面的提示和程序里的要求,修改了Database,新增了衣服的方法。每次计算的价格可以写成共通方法。希望其他人有好的建议也发上来。
import java.util.HashMap;
import java.util.Map;
public class Database {
private Map<Integer, McBean> data = new HashMap<Integer, McBean>();
public Database(){
McBean bean = new McBean();
bean.setNid(1);
bean.setSname("地瓜");
bean.setNprice(2.0);
bean.setSdescription("新鲜的地瓜");
data.put(1, bean);
bean = new McBean();
bean.setNid(2);
bean.setSname("土豆");
bean.setNprice(1.2);
bean.setSdescription("又好又大的土豆");
data.put(2, bean);
bean = new McBean();
bean.setNid(3);
bean.setSname("丝瓜");
bean.setNprice(1.5);
bean.setSdescription("本地丝瓜");
data.put(3, bean);
bean = new McBean();
bean.setNid(4);
bean.setSname("衣服");
bean.setNprice(30);
bean.setSdescription("衣服");
data.put(4, bean);
}
public McBean getMcBean(int nid){
return data.get(nid);
}
}
public class McBean {
private Integer nid; //商品编号
private String sname; //名称
private double nprice; //价格
private String sdescription; //描述
public Integer getNid() {
return nid;
}
public void setNid(Integer nid) {
this.nid = nid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public double getNprice() {
return nprice;
}
public void setNprice(double nprice) {
this.nprice = nprice;
}
public String getSdescription() {
return sdescription;
}
public void setSdescription(String sdescription) {
this.sdescription = sdescription;
}
}
public class OrderItemBean {
private McBean mcbean; //商品
private int count; //商品数量
public McBean getMcbean() {
return mcbean;
}
public void setMcbean(McBean mcbean) {
this.mcbean = mcbean;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class ShoppingCar {
private double totalPrice; //购物车所有商品总价格
private int totalCount; //购物车所有商品数量
private Map<Integer,OrderItemBean> itemMap; //商品编号与订单项的键值对
public ShoppingCar(){
//初始化购物车
itemMap = new HashMap<Integer, OrderItemBean>();
Database db = new Database();
McBean bean = new McBean();
OrderItemBean orderItem1 = new OrderItemBean();
OrderItemBean orderItem2 = new OrderItemBean();
OrderItemBean orderItem3 = new OrderItemBean();
bean = db.getMcBean(1);
orderItem1.setMcbean(bean);
orderItem1.setCount(2);
bean = db.getMcBean(2);
orderItem2.setMcbean(bean);
orderItem2.setCount(5);
bean = db.getMcBean(4);
orderItem3.setMcbean(bean);
orderItem3.setCount(3);
itemMap.put(1, orderItem1);
itemMap.put(2, orderItem2);
itemMap.put(4, orderItem3);
totalCount = itemMap.size();
totalPrice = 0;
Set set = itemMap.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry<Integer, OrderItemBean> entry1=(Map.Entry<Integer, OrderItemBean>)i.next();
totalPrice += entry1.getValue().getCount() * entry1.getValue().getMcbean().getNprice();
}
}
public void buy(int nid){
//如果该商品是第一次购买,商品的信息从数据库获取,数据库的模拟代
//码见附录。itemMap增加一对键值对。
//如果不是第一次购买,则通过商品编号找到对应的定单项,然后更新定
//单项的商品数量。
//不管是否第一次购买,都得更新购物车的总价格和总数量。
Database db = new Database();
McBean mcBean = new McBean();
OrderItemBean orderItem = new OrderItemBean();
if(itemMap.get(nid) == null) {
mcBean = db.getMcBean(nid);
orderItem.setMcbean(mcBean);
orderItem.setCount(1);
itemMap.put(nid, orderItem);
} else {
orderItem = itemMap.get(nid);
orderItem.setCount(orderItem.getCount() + 1);
}
totalCount = itemMap.size();
totalPrice = 0;
Set set = itemMap.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry<Integer, OrderItemBean> entry1=(Map.Entry<Integer, OrderItemBean>)i.next();
totalPrice += entry1.getValue().getCount() * entry1.getValue().getMcbean().getNprice();
}
}
public void delete(int nid){
//通过商品编号删除的对应的定单项,然后从更新购物车的总价格和总数量。
itemMap.remove(nid);
totalCount = itemMap.size();
totalPrice = 0;
Set set = itemMap.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry<Integer, OrderItemBean> entry1=(Map.Entry<Integer, OrderItemBean>)i.next();
totalPrice += entry1.getValue().getCount() * entry1.getValue().getMcbean().getNprice();
}
}
public void update(int nid, int count){
///通过商品编号找到对应的对应的定单项,修改商品数量。然后从更新购物车的总价格和总数量。
OrderItemBean orderItem = new OrderItemBean();
orderItem = itemMap.get(nid);
orderItem.setCount(count);
totalCount = itemMap.size();
totalPrice = 0;
Set set = itemMap.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry<Integer, OrderItemBean> entry1=(Map.Entry<Integer, OrderItemBean>)i.next();
totalPrice += entry1.getValue().getCount() * entry1.getValue().getMcbean().getNprice();
}
}
public void clear(){
//清空定单项,购物车的总价格和总数量清零。
itemMap.clear();
totalCount = 0;
totalPrice = 0;
}
public void show(){
//显示购物车的商品,格式如下:
//商品编号 商品名称 单价 购买数量 总价
//1 地瓜 2.0 2 4.0
//2 衣服 30 5 150
//…
//合计:总数量:5 总价格:20元
System.out.println("商品编号" + " " + "商品名称" + " " + "单价" + " " + "购买数量" + " " + "总价" );
Set set = itemMap.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry<Integer, OrderItemBean> entry1=(Map.Entry<Integer, OrderItemBean>)i.next();
int nid = entry1.getKey();
String name = entry1.getValue().getMcbean().getSname();
double price = entry1.getValue().getMcbean().getNprice();
int count = entry1.getValue().getCount();
double totalprice = price * count;
System.out.println(nid + " "+ name + " " + price + " " + count + " " + totalprice);
}
System.out.println("合计:总数量:" + totalCount + " 总价格:" + totalPrice+ "元");
}
public static void main(String[] args) {
ShoppingCar shoppingCar = new ShoppingCar();
shoppingCar.show();
System.out.println("--------------------------------");
shoppingCar.buy(3);
shoppingCar.show();
System.out.println("--------------------------------");
shoppingCar.update(4, 2);
shoppingCar.show();
System.out.println("--------------------------------");
shoppingCar.delete(2);
shoppingCar.show();
System.out.println("--------------------------------");
shoppingCar.clear();
shoppingCar.show();
}
}
运行结果:
商品编号 商品名称 单价 购买数量 总价
1 地瓜 2.0 2 4.0
2 土豆 1.2 5 6.0
4 衣服 30.0 3 90.0
合计:总数量:3 总价格:100.0元
--------------------------------
商品编号 商品名称 单价 购买数量 总价
1 地瓜 2.0 2 4.0
2 土豆 1.2 5 6.0
3 丝瓜 1.5 1 1.5
4 衣服 30.0 3 90.0
合计:总数量:4 总价格:101.5元
--------------------------------
商品编号 商品名称 单价 购买数量 总价
1 地瓜 2.0 2 4.0
2 土豆 1.2 5 6.0
3 丝瓜 1.5 1 1.5
4 衣服 30.0 2 60.0
合计:总数量:4 总价格:71.5元
--------------------------------
商品编号 商品名称 单价 购买数量 总价
1 地瓜 2.0 2 4.0
3 丝瓜 1.5 1 1.5
4 衣服 30.0 2 60.0
合计:总数量:3 总价格:65.5元
--------------------------------
商品编号 商品名称 单价 购买数量 总价
合计:总数量:0 总价格:0.0元本回答被提问者采纳