相信大家肯定都在電商網(wǎng)站買(mǎi)過(guò)東西,當(dāng)我們看中一件喜歡又想買(mǎi)的東西時(shí),這時(shí)候你又不想這么快結(jié)賬,這時(shí)候你就可以放入購(gòu)物車(chē);
就像我們平時(shí)去超市買(mǎi)東西一樣,會(huì)推著購(gòu)物車(chē)去買(mǎi)東西;
那么我們接下來(lái)看看java怎么實(shí)現(xiàn)購(gòu)物車(chē)的功能,其實(shí)原理很簡(jiǎn)單,java的特點(diǎn)就是面向?qū)ο螅⑶矣兄庋b繼承多態(tài)三大特性;
java實(shí)現(xiàn)這個(gè)購(gòu)物車(chē)功能是通過(guò)內(nèi)存來(lái)實(shí)現(xiàn)的而不是將數(shù)據(jù)添加到數(shù)據(jù)庫(kù)中
首先是Item類(lèi),一個(gè)Item就代表購(gòu)物車(chē)?yán)锩娴囊恍袛?shù)據(jù)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package com.wxd.shopping; public class Item { private int id; //商品id private String name; //商品名稱(chēng) private String city; //商品產(chǎn)地 private double price; //商品價(jià)格 private int number; //商品數(shù)量 private String picture; //商品圖片地址 public int getId() { return id; } public void setId( int id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getCity() { return city; } public void setCity(String city) { this .city = city; } public double getPrice() { return price; } public void setPrice( double price) { this .price = price; } public int getNumber() { return number; } public void setNumber( int number) { this .number = number; } public String getPicture() { return picture; } public void setPicture(String picture) { this .picture = picture; } /** * 重寫(xiě)hashCode方法,使得在購(gòu)物車(chē)添加商品的時(shí)候,如果id和名稱(chēng)相同就判定為同一件商品 * @return */ @Override public int hashCode() { return ( this .getId()+ this .getName()).hashCode(); } /** * 重寫(xiě)equals方法,判斷是否為同一個(gè)對(duì)象 * @param obj * @return */ @Override public boolean equals(Object obj) { if ( this ==obj){ return true ; } if (obj instanceof Item){ Item i= (Item) obj; if ( this .getId()==i.getId()&& this .getName().equals(i.getName())){ return true ; } else { return false ; } } else { return false ; } } @Override public String toString() { return "Item{" + "id=" + id + ", name='" + name + '\ '' + ", city='" + city + '\ '' + ", price=" + price + ", number=" + number + ", picture='" + picture + '\ '' + '}' ; } //有參構(gòu)造 public Item( int id, String name, String city, double price, int number, String picture) { this .id = id; this .name = name; this .city = city; this .price = price; this .number = number; this .picture = picture; } //無(wú)參構(gòu)造 public Item() { } } |
購(gòu)物車(chē)類(lèi)分裝了Item和數(shù)量的一個(gè)集合,還有商品的總金額
下面是購(gòu)物車(chē)類(lèi)的詳細(xì)代碼以及測(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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
package com.wxd.shopping; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; //購(gòu)物車(chē) public class Cart { //購(gòu)買(mǎi)商品的集合 private HashMap<Item,Integer> goods; //購(gòu)物車(chē)的總金額 private double totalPrice; //構(gòu)造方法初始化數(shù)據(jù) public Cart(){ goods= new HashMap<Item,Integer>(); totalPrice= 0.0 ; } public HashMap<Item, Integer> getGoods() { return goods; } public void setGoods(HashMap<Item, Integer> goods) { this .goods = goods; } public double getTotalPrice() { return totalPrice; } public void setTotalPrice( double totalPrice) { this .totalPrice = totalPrice; } //添加商品進(jìn)購(gòu)物車(chē)的方法 public boolean addGoodsInCart(Item item, int number){ if (goods.containsKey(item)){ goods.put(item,goods.get(item)+number); } else { goods.put(item,number); } calTotalPrice(); //重新計(jì)算購(gòu)物車(chē)的總金額 return true ; } //刪除商品的方法 public boolean removeGoodsFromCart(Item item){ goods.remove(item); calTotalPrice(); //重新計(jì)算購(gòu)物車(chē)的總金額 return true ; } //統(tǒng)計(jì)購(gòu)物車(chē)的總金額 public double calTotalPrice(){ double sum= 0.0 ; Set<Item> keys=goods.keySet(); Iterator<Item> iterator = keys.iterator(); while (iterator.hasNext()){ Item i = iterator.next(); sum+=i.getPrice()*goods.get(i); } this .setTotalPrice(sum); //設(shè)置購(gòu)物車(chē)的總金額 return this .getTotalPrice(); } //測(cè)試的main方法 public static void main(String[] args) { //先創(chuàng)建兩個(gè)商品對(duì)象 Item i1= new Item( 1 , "耐克" , "溫州" , 300.0 , 500 , "001.jpg" ); Item i2= new Item( 2 , "阿迪" , "廣州" , 500.0 , 500 , "001.jpg" ); Item i3= new Item( 1 , "耐克" , "溫州" , 300.0 , 500 , "001.jpg" ); Cart c= new Cart(); c.addGoodsInCart(i1, 1 ); c.addGoodsInCart(i2, 2 ); //再次購(gòu)買(mǎi)耐克鞋 c.addGoodsInCart(i3, 3 ); //遍歷購(gòu)買(mǎi)商品的集合 HashMap<Item, Integer> goods = c.getGoods(); Set<Map.Entry<Item, Integer>> entries = goods.entrySet(); for (Map.Entry<Item, Integer> itemEntry:entries){ System.out.println(itemEntry.toString()); } System.out.println( "購(gòu)物車(chē)總金額:" +c.getTotalPrice()); } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/MrXiaoAndDong/p/Cart.html