JavaWeb后台购物车类实现代码详解
相信大家肯定都在电商网站买过东西,当我们看中一件喜欢又想买的东西时,这时候你又不想这么快结账,这时候你就可以放入购物车;
就像我们平时去超市买东西一样,会推着购物车去买东西;
那么我们接下来看看java怎么实现购物车的功能,其实原理很简单,java的特点就是面向对象,并且有着封装继承多态三大特性;
java实现这个购物车功能是通过内存来实现的而不是将数据添加到数据库中
首先是Item类,一个Item就代表购物车里面的一行数据
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和数量的一个集合,还有商品的总金额
下面是购物车类的详细代码以及测试方法:
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());
}
}
以上是 JavaWeb后台购物车类实现代码详解 的全部内容, 来源链接: utcz.com/z/359579.html