用java代碼寫一個(gè)簡(jiǎn)單的網(wǎng)上購(gòu)物車程序,供大家參考,具體內(nèi)容如下
需求:
1、寫一個(gè)商品類,有商品編號(hào)、商品名稱、商品分類、商品單價(jià)屬性。
2、寫一個(gè)商品條目信息類,有商品和數(shù)量?jī)蓚€(gè)屬性,有商品總價(jià)格方法。
3、寫一個(gè)購(gòu)物車類,有添加商品方法、查看訂單信息,刪除商品,修改商品,清空購(gòu)物車,求購(gòu)物車中所有商品總金額方法。4、寫一個(gè)測(cè)試類,測(cè)試上述方法。
商品類:
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; // 商品編號(hào) private String productName; // 商品名稱 private String category; // 商品分類 private double price; // 單價(jià) public Product() { // 無參構(gòu)造 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; //購(gòu)買的商品 private int count; //商品數(shù)量 public double totalMoney(){ //小計(jì) double price=product.getPrice(); //獲取商品單價(jià) 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; } } |
購(gòu)物車類:
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 { //購(gòu)物車 //key:商品編號(hào) 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( "商品編號(hào):" +product.getProductId()+ ",商品名稱:" +product.getProductName()+ ",單價(jià):" +product.getPrice()+ ",數(shù)量:" +productItem.getCount() + ",小計(jì):" +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(){ //清空購(gòu)物車 map.clear(); } public double totalAllMoney(){ //商品總錢數(shù) 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; } } |
測(cè)試類:
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 , "華為手機(jī)" , "手機(jī)" , 2998 ); Product p6= new Product( 101 , "華碩筆記本" , "筆記本" , 4599 ); //測(cè)試買兩件商品的情況 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( "商品編號(hào)為:" +p2.getProductId()+ "的商品刪除成功!" ); } else { System.out.println( "刪除失敗" ); } cart.showAll(); System.out.println( "############" ); boolean flag2=cart.modifyProduct(p3.getProductId(), 2 ); if (flag2){ System.out.println( "商品編號(hào)為:" +p3.getProductId()+ "的商品修改成功!" ); } else { System.out.println( "修改失敗" ); } cart.showAll(); //cart.clearCart(); //cart.showAll(); System.out.println( "商品總價(jià)錢為:" +cart.totalAllMoney()); } } |
運(yùn)行效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/ylyang12/article/details/52972432