用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