相信大家肯定都在电商网站买过东西,当我们看中一件喜欢又想买的东西时,这时候你又不想这么快结账,这时候你就可以放入购物车;
就像我们平时去超市买东西一样,会推着购物车去买东西;
那么我们接下来看看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());
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://www.cnblogs.com/MrXiaoAndDong/p/Cart.html
相关文章
猜你喜欢
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 107
-
2025-05-25 85
-
2025-05-26 64
-
2025-05-27 56
-
2025-05-27 15
热门评论

