用java代码写一个简单的网上购物车程序,供大家参考,具体内容如下
需求:
1、写一个商品类,有商品编号、商品名称、商品分类、商品单价属性。
2、写一个商品条目信息类,有商品和数量两个属性,有商品总价格方法。
3、写一个购物车类,有添加商品方法、查看订单信息,删除商品,修改商品,清空购物车,求购物车中所有商品总金额方法。4、写一个测试类,测试上述方法。
商品类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
public class Product {
private int productId; // 商品编号
private String productName; // 商品名称
private String category; // 商品分类
private double price; // 单价
public Product() { // 无参构造
super ();
}
public Product( int productId, String productName, String category,
double price) {
super ();
this .productId = productId;
this .productName = productName;
this .category = category;
this .price = price;
}
public String toString() {
return "Product [productId=" + productId + ", productName="
+ productName + ", category=" + category + ", price=" + price
+ "]" ;
}
public int getProductId() {
return productId;
}
public void setProductId( int productId) {
this .productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this .productName = productName;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this .category = category;
}
public double getPrice() {
return price;
}
public void setPrice( double price) {
this .price = price;
}
}
|
商品条目信息类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public class ProductItem {
private Product product; //购买的商品
private int count; //商品数量
public double totalMoney(){ //小计
double price=product.getPrice(); //获取商品单价
return price*count;
}
public ProductItem() {
super ();
}
public ProductItem(Product product, int count) {
super ();
this .product = product;
this .count = count;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this .product = product;
}
public int getCount() {
return count;
}
public void setCount( int count) {
this .count = count;
}
}
|
购物车类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class ShoppingCart { //购物车
//key:商品编号 value:商品条目
private Map<Integer,ProductItem> map= new LinkedHashMap<Integer,ProductItem>();
public void addProduct(Product p){ //添加商品
int productId=p.getProductId();
if (map.containsKey(productId)){
ProductItem productItem=map.get(productId);
productItem.setCount(productItem.getCount()+ 1 );
} else {
map.put(productId, new ProductItem(p, 1 ));
}
}
public void showAll(){ //查看订单信息
Collection<ProductItem> productItems = map.values();
Iterator<ProductItem> iterator = productItems.iterator();
while (iterator.hasNext()){
ProductItem productItem = iterator.next();
Product product = productItem.getProduct();
System.out.println( "商品编号:" +product.getProductId()+ ",商品名称:"
+product.getProductName()+ ",单价:" +product.getPrice()+ ",数量:" +productItem.getCount()
+ ",小计:" +productItem.totalMoney());
}
}
public boolean deleteProduct( int productId){ //删除商品
if (map.containsKey(productId)){
map.remove(productId);
return true ;
}
return false ;
}
public boolean modifyProduct( int productId, int count){ //修改
if (map.containsKey(productId)){
if (count>= 1 ){
ProductItem productItem = map.get(productId);
productItem.setCount(count);
return true ;
} else if (count== 0 ){ //删除该商品
deleteProduct(productId);
return true ;
}
}
return false ;
}
public void clearCart(){ //清空购物车
map.clear();
}
public double totalAllMoney(){ //商品总钱数
double total= 0 ;
Collection<ProductItem> productItems = map.values();
Iterator<ProductItem> iterator = productItems.iterator();
while (iterator.hasNext()){
ProductItem productItem = iterator.next();
double money=productItem.totalMoney();
total+=money;
}
return total;
}
}
|
测试类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
public class ShoppingCartTest {
public static void main(String[] args) {
ShoppingCart cart= new ShoppingCart();
Product p1= new Product( 101 , "华硕笔记本" , "笔记本" , 4599 );
Product p2= new Product( 102 , "苹果" , "水果" , 5.9 );
Product p3= new Product( 103 , "彩电" , "家电" , 2799 );
Product p4= new Product( 104 , "秋裤" , "服装" , 128 );
Product p5= new Product( 105 , "华为手机" , "手机" , 2998 );
Product p6= new Product( 101 , "华硕笔记本" , "笔记本" , 4599 ); //测试买两件商品的情况
cart.addProduct(p1);
cart.addProduct(p2);
cart.addProduct(p3);
cart.addProduct(p4);
cart.addProduct(p5);
cart.addProduct(p6);
cart.showAll();
System.out.println( "############" );
boolean flag=cart.deleteProduct(p2.getProductId());
if (flag){
System.out.println( "商品编号为:" +p2.getProductId()+ "的商品删除成功!" );
} else {
System.out.println( "删除失败" );
}
cart.showAll();
System.out.println( "############" );
boolean flag2=cart.modifyProduct(p3.getProductId(), 2 );
if (flag2){
System.out.println( "商品编号为:" +p3.getProductId()+ "的商品修改成功!" );
} else {
System.out.println( "修改失败" );
}
cart.showAll();
//cart.clearCart();
//cart.showAll();
System.out.println( "商品总价钱为:" +cart.totalAllMoney());
}
}
|
运行效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/ylyang12/article/details/52972432