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后台购物车类的具体实现的更多相关文章
- javaweb常用工具类及配置文件备份
Javaweb常用工具类及配置文件备份 做一个代码备份,以后常用到的. hibernate工具类备份 package com.dly.service; /* * hibernate获取sessi ...
- PHP购物车类
<?php /** * 购物车类 */ session_start(); class Cart{ private static $ins = null; private $items = arr ...
- php之购物车类思路及代码
<?php /* 购物车类 1.整站范围内,购物车--全局有效 解决:把购物车的信息,放在session里 2.既然全局有效,购物车的实例只有一个 解决:单例模式 技术选型:session+单例 ...
- PHP商城购物车类
<?php /* 购物车类 */ // session_start(); class Cart { //定义一个数组来保存购物车商品 private $iteams; private stati ...
- Abp后台工作者类使用hangfire
一.Abp中的后台工作及后台工作者类 请阅读这篇文章 二 .Abp官方实现的缺点 Abp官方实现方式很简单,也很容易上手,但缺点是工作者类依赖了具体的基类(PeriodicBackgroundWork ...
- JavaWeb后台从input表单获取文本值的两种方式
JavaWeb后台从input表单获取文本值的两种方式 #### index.html <!DOCTYPE html> <html lang="en"> & ...
- ECshop网点程序优化-后台添加类目自动选择上次父类目并计算Sort Order
如果在ECshop后台批量添加过大量类目的人都能体会到是多么的不方便(这点还是要说一下ECshop的产品经理,细节上还是要多注意),每次添加都需要在几百个类目里面找到要添加的父类目也是一个麻烦事,比如 ...
- JavaWeb学习——获取类路径下的资源
对于JavaWeb而言,获取类路径下的资源,就是获取classes目录下的资源. 获取资源的方式有两种,利用Class或ClassLoader. Class类的getResourceAsStream( ...
- JavaWeb开发购物车设计总结
一. 实体类设计 图书实体类 public class Book { private String id; private String name; private String author; pr ...
随机推荐
- ADSL 动态IP拨号VPS 软件配置
http://yun.baidu.com/share/link?uk=2520566727&shareid=330788421&third=0&adapt=pc&fr= ...
- dart之旅(二)- 内建类型
目录 number 类型 字符串 布尔类型 像大多数语言一样,dart 也提供了 number,string,boolean 等类型,包括以下几种: numbers strings booleans ...
- TCP/IP 笔记 - 域名解析和域名系统
由于IP地址的烦琐导致的记忆和使用困难,互联网支持使用主机名称来识别包括客户机和服务器在内的主机.同时为了使用一系列协议,主机名称通过称为"名称解析"的过程转换成对应IP地址. 互 ...
- Java 容器源码分析之ArrayBlockingQueue和LinkedBlockingQueue
Java中的阻塞队列接口BlockingQueue继承自Queue接口. BlockingQueue接口提供了3个添加元素方法. add:添加元素到队列里,添加成功返回true,由于容量满了添加失败会 ...
- Vxlan学习笔记——原理
1. 为什么需要Vxlan 普通的VLAN数量只有4096个,无法满足大规模云计算IDC的需求,而IDC为何需求那么多VLAN呢,因为目前大部分IDC内部结构主要分为两种L2,L3.L2结构里面,所有 ...
- tensorflow实现循环神经网络
包括卷积神经网络(CNN)在内的各种前馈神经网络模型, 其一次前馈过程的输出只与当前输入有关与历史输入无关. 递归神经网络(Recurrent Neural Network, RNN)充分挖掘了序列数 ...
- T-SQL:谓词和运算符(六)
谓词一般有 where和having,check 谓词只计算 TRUE ,FALSE或者UNKNOWN 逻辑表达式 如 AND 和OR 1.IN 谓词的用法 SELECT orderid, em ...
- c# 对象集合转Json
/// <summary> /// 普通集合转换Json /// </summary> /// <param name="array">集合对象 ...
- Hive 表类型简述
Hive 表类型简述 表类型一.管理表或内部表Table Type: MANAGED_TABLE example: create table Inner(id int,name string, ...
- mysql游标中使用临时表
有时候需我们要组合几张表的数据,在存储过程中,经过比较复杂的运算获取结果直接输出给调用方,比如符合条件的几张表的某些字段的组合计算,mysql临时表可以解决这个问题. 所谓临时表:只有在当前连接情况下 ...