A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© doevents 中级黑马   /  2013-9-16 22:04  /  1424 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 doevents 于 2013-9-17 00:03 编辑

简单的实现购物车功能,其中有3个文件Product.java,ShoppingCart.java和ShoppingTest.java,代码如下
Product.java
//超市中的商品
public class Product
{
//Field
private int no;
private String name;
private double price;
//Constructor
public Product(){}
public Product(int no,String name,double price){
  this.no = no;
  this.name = name;
  this.price = price;
}
//setter and getter
public void setNo(int no){
  this.no = no;
}
public int getNo(){
  return no;
}
public void setName(String name){
  this.name = name;
}
public String getName(){
  return name;
}
public void setPrice(double price){
  this.price = price;
}
public double getPrice(){
  return price;
}
//toString
public String toString(){
  return "[编号:"+no+",名称:"+name+",单价:"+price+"元/个]";
}
//hashCode
//超市商品编号的格式:[1000-9999]
public int hashCode(){
  return no/1000;
}
//equals
//需求:如果商品编号和商品名字都一样表示同一个商品
public boolean equals(Object o){
  if(this==o) return true;
  if(o instanceof Product){
   Product p = (Product)o;
   if(p.no==this.no && p.name.equals(this.name)){
    return true;
   }
  }
  return false;
}

}

ShoppingCart.java
import java.util.*;
//购物车
public class ShoppingCart
{
//使用Map存储商品条目.
Map<Product,Integer> productMaps;
//总价
double totalPrice;
//Constructor
ShoppingCart(){
  productMaps = new HashMap<Product,Integer>();
}

//添加1个商品
public void add(Product p){
  add(p,1);
}
//添加N个商品
public void add(Product p,int n){
  //判断该购物车中是否含有该商品
  if(!productMaps.containsKey(p)){
   productMaps.put(p,n); //自动装箱
  }else{
   //车中有该商品
   int before = productMaps.get(p).intValue(); //添加商品之前的该商品数量
   int after = before + n;
   productMaps.put(p,after); //key重复value覆盖。
  }
  //修改总价
  totalPrice += p.getPrice()*n;
}
//删除1个商品
public void remove(Product p){
  remove(p,1);
}
//删除N个商品
public void remove(Product p,int n){
  
  int before = productMaps.get(p).intValue();
  //如果购物车中的某商品数量和n是相等的,必须删除整个条目
  if(before==n){
   productMaps.remove(p);
  }else{
   int after = before - n;
   productMaps.put(p,after);
  }
  //修改总价
  totalPrice -= p.getPrice()*n;
}
//清空购物车
public void clear(){
  productMaps.clear();
  totalPrice = 0.0;
}
//输出购物车详单.
/*
  格式:
  购物详单:
   10 苹果 10元
   3 西瓜 30元
   ................
     总价:40...
*/
public void print(){
  StringBuffer sb = new StringBuffer();
  sb.append("购物详单:\n");
  //遍历Map
  Set<Product> keys = productMaps.keySet();
  Iterator<Product> it = keys.iterator();
  while(it.hasNext()){
   Product k = it.next();
   Integer v = productMaps.get(k);
   sb.append("\t" + v.intValue() + "\t" + k + "\t" + (v.intValue()*k.getPrice())+"元\n");
  }
  sb.append("\t\t\t\t\t\t\t\t\t\t\t总价:" + totalPrice+"元");
  System.out.println(sb);
  
}
}

ShoppingTest.java
public class ShoppingTest
{
public static void main(String[] args){
  
  //创建购物车
  ShoppingCart cart = new ShoppingCart();
  
  //货物
  Product p1 = new Product(1000,"iphone4s",10.0);
  Product p2 = new Product(2000,"samsung",3.0);
  Product p3 = new Product(3000,"HTC",6.0);
  Product p4 = new Product(4000,"小米",2.0);
  //开始购物
  cart.add(p1);
  cart.add(p1,3);
  
  cart.add(p2,1);
  cart.add(p3,2);
  cart.add(p4,3);
  cart.remove(p1);
  cart.remove(p1,2);

  //打印详单
  cart.print();
  //清空
  cart.clear();
  cart.print();
}
}
希望大家多多学习,{:soso_e100:}

购物车.zip

2.03 KB, 下载次数: 88

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1 很给力!

查看全部评分

4 个回复

倒序浏览
哥们,你应该做过不少项目吧
回复 使用道具 举报
神之梦 发表于 2013-9-16 23:33
哥们,你应该做过不少项目吧

都是自学的,通过学习知识点,整合一下,做点小程序,顺便联系下学过的东西。
回复 使用道具 举报
不错,学习学习
回复 使用道具 举报
doevents 发表于 2013-9-17 00:02
都是自学的,通过学习知识点,整合一下,做点小程序,顺便联系下学过的东西。
...

很厉害{:soso_e179:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马