发布网友 发布时间:2022-04-26 15:38
共2个回答
热心网友 时间:2022-04-26 17:07
以前学习java又做个实例,挺值得学习的。1.首先我先列出我们所需要的java类结构。1)Database.java---------模拟存储商品的数据库。2)McBean.java------------商品实体类,一个普通的javabean,里面有商品的基本属性。3)OrderItemBean.java---订单表。4)ShoppingCar.java------这个就是最主要的购物车,当然比较简单。5)TestShoppingCar.java---这个是测试类。2.下面贴出具体代码并带关键注释。---Database.javapublicclassDatabase{/*采用Map存储商品数据,为什么呢?我觉得这个问题你自己需要想下。*Integer为Map的key值,McBean为Map的value值。*/privatestaticMapdata=newHashMap();publicDatabase(){McBeanbean=newMcBean();bean.setId(1);bean.setName("地瓜");bean.setPrice(2.0);bean.setInstuction("新鲜的地瓜");data.put(1,bean);//把商品放入Mapbean=newMcBean();bean.setId(2);bean.setName("土豆");bean.setPrice(1.2);bean.setInstuction("又好又大的土豆");data.put(2,bean);//把商品放入Mapbean=newMcBean();bean.setId(3);bean.setName("丝瓜");bean.setPrice(1.5);bean.setInstuction("本地丝瓜");data.put(3,bean);//把商品放入Map}publicvoidsetMcBean(McBeanmcBean){data.put(mcBean.getId(),mcBean);}publicMcBeangetMcBean(intnid){returndata.get(nid);}}---McBean.javapublicclassMcBean{privateintid;//商品编号privateStringname;//商品名privatedoubleprice;//商品价格privateStringinstuction;//商品说明publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicdoublegetPrice(){returnprice;}publicvoidsetPrice(doubleprice){this.price=price;}publicStringgetInstuction(){returninstuction;}publicvoidsetInstuction(Stringinstuction){this.instuction=instuction;}}---ShoppingCar.javapublicclassShoppingCar{privatedoubletotalPrice;//购物车所有商品总价格privateinttotalCount;//购物车所有商品数量privateMapitemMap;//商品编号与订单项的键值对publicShoppingCar(){itemMap=newHashMap();}publicvoidbuy(intnid){OrderItemBeanorder=itemMap.get(nid);McBeanmb;if(order==null){mb=newDatabase().getMcBean(nid);order=newOrderItemBean(mb,1);itemMap.put(nid,order);update(nid,1);}else{order.setCount(order.getCount()+1);update(nid,1);}}publicvoiddelete(intnid){OrderItemBeandelorder=itemMap.remove(nid);totalCount=totalCount-delorder.getCount();totalPrice=totalPrice-delorder.getThing().getPrice()*delorder.getCount();}publicvoipdate(intnid,intcount){OrderItemBeanupdorder=itemMap.get(nid);totalCount=totalCount+count;totalPrice=totalPrice+updorder.getThing().getPrice()*count;}publicvoidclear(){itemMap.clear();totalCount=0;totalPrice=0.0;}publicvoidshow(){DecimalFormatdf=newDecimalFormat("¤#.##");System.out.println("商品编号\t商品名称\t单价\t购买数量\t总价");Setset=itemMap.keySet();Iteratorit=set.iterator();while(it.hasNext()){OrderItemBeanorder=itemMap.get(it.next());System.out.println(order.getThing().getId()+"\t"+order.getThing().getName()+"\t"+df.format(order.getThing().getPrice())+"\t"+order.getCount()+"\t"+df.format(order.getCount()*order.getThing().getPrice()));}System.out.println("合计:总数量:"+df.format(totalCount)+"总价格:"+df.format(totalPrice));System.out.println("**********************************************");}}---OrderItemBean.javapublicclassOrderItemBean{privateMcBeanthing;//商品的实体privateintcount;//商品的数量publicOrderItemBean(McBeanthing,intcount){super();this.thing=thing;this.count=count;}publicMcBeangetThing(){returnthing;}publicvoidsetThing(McBeanthing){this.thing=thing;}publicintgetCount(){returncount;}publicvoidsetCount(intcount){this.count=count;}}---TestShoppingCar.javapackagecom.shop;publicclassTestShoppingCar{publicstaticvoidmain(String[]args){ShoppingCars=newShoppingCar();s.buy(1);//购买商品编号1的商品s.buy(1);s.buy(2);s.buy(3);s.buy(1);s.show();//显示购物车的信息s.delete(1);//删除商品编号为1的商品s.show();s.clear();s.show();}}3.打印输出结果商品编号商品名称单价购买数量总价1地瓜¥23¥62土豆¥1.21¥1.23丝瓜¥1.51¥1.5合计:总数量:¥5总价格:¥8.7**********************************************商品编号商品名称单价购买数量总价2土豆¥1.21¥1.23丝瓜¥1.51¥1.5合计:总数量:¥2总价格:¥2.7**********************************************商品编号商品名称单价购买数量总价合计:总数量:¥0总价格:¥0**********************************************4.打字太累了,比较匆忙,但是主要靠你自己领会。哪里不清楚再提出来。热心网友 时间:2022-04-26 18:25
一般就是你在页面增加监听事件,每次修改商品数量就重新计算总价