相信大家肯定都在電商網站買過東西,當我們看中一件喜歡又想買的東西時,這時候你又不想這么快結賬,這時候你就可以放入購物車;
就像我們平時去超市買東西一樣,會推著購物車去買東西;
那么我們接下來看看java怎么實現購物車的功能,其實原理很簡單,java的特點就是面向對象,并且有著封裝繼承多態三大特性;
java實現這個購物車功能是通過內存來實現的而不是將數據添加到數據庫中
首先是Item類,一個Item就代表購物車里面的一行數據
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; //商品名稱 private String city; //商品產地 private double price; //商品價格 private int number; //商品數量 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; } /** * 重寫hashCode方法,使得在購物車添加商品的時候,如果id和名稱相同就判定為同一件商品 * @return */ @Override public int hashCode() { return ( this .getId()+ this .getName()).hashCode(); } /** * 重寫equals方法,判斷是否為同一個對象 * @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 + '\ '' + '}' ; } //有參構造 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; } //無參構造 public Item() { } } |
購物車類分裝了Item和數量的一個集合,還有商品的總金額
下面是購物車類的詳細代碼以及測試方法:
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; //購物車 public class Cart { //購買商品的集合 private HashMap<Item,Integer> goods; //購物車的總金額 private double totalPrice; //構造方法初始化數據 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; } //添加商品進購物車的方法 public boolean addGoodsInCart(Item item, int number){ if (goods.containsKey(item)){ goods.put(item,goods.get(item)+number); } else { goods.put(item,number); } calTotalPrice(); //重新計算購物車的總金額 return true ; } //刪除商品的方法 public boolean removeGoodsFromCart(Item item){ goods.remove(item); calTotalPrice(); //重新計算購物車的總金額 return true ; } //統計購物車的總金額 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); //設置購物車的總金額 return this .getTotalPrice(); } //測試的main方法 public static void main(String[] args) { //先創建兩個商品對象 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 ); //再次購買耐克鞋 c.addGoodsInCart(i3, 3 ); //遍歷購買商品的集合 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( "購物車總金額:" +c.getTotalPrice()); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/MrXiaoAndDong/p/Cart.html