标签:tco rem out item shm double java span contain
1 需求:1、写一个商品类,有商品编号、商品名称、商品分类、商品单价属性。2、写一个商品条目信息类,有商品和数量两个属性,有商品总价格方法。 2 3 3、写一个购物车类,有添加商品方法、查看订单信息,删除商品,修改商品,清空购物车,求购物车中所有商品总金额方法。4、写一个测试类,测试上述方法。 4 5 商品类: 6 [java] view plain copy 7 8 public class Product { 9 private int productId;// 商品编号 10 private String productName;// 商品名称 11 private String category;// 商品分类 12 private double price;// 单价 13 14 public Product() {// 无参构造 15 super(); 16 } 17 18 public Product(int productId, String productName, String category, 19 double price) { 20 super(); 21 this.productId = productId; 22 this.productName = productName; 23 this.category = category; 24 this.price = price; 25 } 26 27 public String toString() { 28 return "Product [productId=" + productId + ", productName=" 29 + productName + ", category=" + category + ", price=" + price 30 + "]"; 31 } 32 33 public int getProductId() { 34 return productId; 35 } 36 37 public void setProductId(int productId) { 38 this.productId = productId; 39 } 40 41 public String getProductName() { 42 return productName; 43 } 44 45 public void setProductName(String productName) { 46 this.productName = productName; 47 } 48 49 public String getCategory() { 50 return category; 51 } 52 53 public void setCategory(String category) { 54 this.category = category; 55 } 56 57 public double getPrice() { 58 return price; 59 } 60 61 public void setPrice(double price) { 62 this.price = price; 63 } 64 65 } 66 67 商品条目信息类: 68 [java] view plain copy 69 70 public class ProductItem { 71 private Product product;//购买的商品 72 private int count;//商品数量 73 public double totalMoney(){//小计 74 double price=product.getPrice();//获取商品单价 75 return price*count; 76 } 77 78 public ProductItem() { 79 super(); 80 } 81 82 public ProductItem(Product product, int count) { 83 super(); 84 this.product = product; 85 this.count = count; 86 } 87 88 public Product getProduct() { 89 return product; 90 } 91 public void setProduct(Product product) { 92 this.product = product; 93 } 94 public int getCount() { 95 return count; 96 } 97 public void setCount(int count) { 98 this.count = count; 99 } 100 101 } 102 103 104 购物车类: 105 [java] view plain copy 106 107 import java.util.Collection; 108 import java.util.Iterator; 109 import java.util.LinkedHashMap; 110 import java.util.Map; 111 public class ShoppingCart {//购物车 112 //key:商品编号 value:商品条目 113 private Map<Integer,ProductItem> map=new LinkedHashMap<Integer,ProductItem>(); 114 115 public void addProduct(Product p){//添加商品 116 int productId=p.getProductId(); 117 if(map.containsKey(productId)){ 118 ProductItem productItem=map.get(productId); 119 productItem.setCount(productItem.getCount()+1); 120 }else{ 121 map.put(productId, new ProductItem(p,1)); 122 } 123 } 124 public void showAll(){//查看订单信息 125 Collection<ProductItem> productItems = map.values(); 126 Iterator<ProductItem> iterator = productItems.iterator(); 127 while(iterator.hasNext()){ 128 ProductItem productItem = iterator.next(); 129 Product product = productItem.getProduct(); 130 System.out.println("商品编号:"+product.getProductId()+",商品名称:" 131 +product.getProductName()+",单价:"+product.getPrice()+",数量:"+productItem.getCount() 132 +",小计:"+productItem.totalMoney()); 133 } 134 } 135 public boolean deleteProduct(int productId){//删除商品 136 if(map.containsKey(productId)){ 137 map.remove(productId); 138 return true; 139 } 140 return false; 141 } 142 public boolean modifyProduct(int productId,int count){//修改 143 if(map.containsKey(productId)){ 144 if(count>=1){ 145 ProductItem productItem = map.get(productId); 146 productItem.setCount(count); 147 return true; 148 }else if(count==0){//删除该商品 149 deleteProduct(productId); 150 return true; 151 } 152 } 153 return false; 154 } 155 156 public void clearCart(){//清空购物车 157 map.clear(); 158 } 159 160 public double totalAllMoney(){//商品总钱数 161 double total=0; 162 Collection<ProductItem> productItems = map.values(); 163 Iterator<ProductItem> iterator = productItems.iterator(); 164 while(iterator.hasNext()){ 165 ProductItem productItem = iterator.next(); 166 double money=productItem.totalMoney(); 167 total+=money; 168 } 169 return total; 170 } 171 } 172 173 174 测试类: 175 [java] view plain copy 176 177 public class ShoppingCartTest { 178 179 public static void main(String[] args) { 180 ShoppingCart cart=new ShoppingCart(); 181 Product p1=new Product(101,"华硕笔记本","笔记本",4599); 182 Product p2=new Product(102,"苹果","水果",5.9); 183 Product p3=new Product(103,"彩电","家电",2799); 184 Product p4=new Product(104,"秋裤","服装",128); 185 Product p5=new Product(105,"华为手机","手机",2998); 186 Product p6=new Product(101,"华硕笔记本","笔记本",4599);//测试买两件商品的情况 187 cart.addProduct(p1); 188 cart.addProduct(p2); 189 cart.addProduct(p3); 190 cart.addProduct(p4); 191 cart.addProduct(p5); 192 cart.addProduct(p6); 193 cart.showAll(); 194 System.out.println("############"); 195 boolean flag=cart.deleteProduct(p2.getProductId()); 196 if(flag){ 197 System.out.println("商品编号为:"+p2.getProductId()+"的商品删除成功!"); 198 }else{ 199 System.out.println("删除失败"); 200 } 201 cart.showAll(); 202 System.out.println("############"); 203 boolean flag2=cart.modifyProduct(p3.getProductId(), 2); 204 if(flag2){ 205 System.out.println("商品编号为:"+p3.getProductId()+"的商品修改成功!"); 206 }else{ 207 System.out.println("修改失败"); 208 } 209 cart.showAll(); 210 211 //cart.clearCart(); 212 //cart.showAll(); 213 System.out.println("商品总价钱为:"+cart.totalAllMoney()); 214 215 } 216 217 }
标签:tco rem out item shm double java span contain
原文地址:http://www.cnblogs.com/stsinghua/p/6418613.html