RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
跨境购物流程java代码,跨境购物流程java代码

java中servlet的购物车程序是怎么样的流程?

购买过程就是选择好物品放入购物车然后结账

创新互联-专业网站定制、快速模板网站建设、高性价比殷都网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式殷都网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖殷都地区。费用合理售后完善,十年实体公司更值得信赖。

import java.util.Vector;public class gouwuche { /**

* @param args

*/

private static Vector vec = new Vector();

public static void gw(String name,int price,int sum)

{

gouwuchebean bean;

if(vec.size()0)

{

//说明购物车内有物品 进来比对 看是否有一样的东西 有的话让数量+sum

bean = new gouwuchebean();

int j=0;//用来计数

for(int i=0;ivec.size();i++)

{

gouwuchebean bean1 = (gouwuchebean)vec.get(i);

if(bean1.getName().equals(name))

{

j++;

bean.setName(name);

bean.setPrice(price);

bean.setSum(sum+bean1.getSum());

vec.remove(i);//去掉原来的数据

vec.add(bean);//放入新的数据

}

}

if(j==0)

{

bean.setName(name);

bean.setPrice(price);

bean.setSum(sum);

vec.add(bean);

}

}

else

{

//如果集合是空的说明购物车内没有重复的 直接放入即可

bean = new gouwuchebean();

bean.setName(name);

bean.setPrice(price);

bean.setSum(sum);

vec.add(bean);

}

}

public static void show()

{

System.out.println("=============购物车当前物品==============");

int sum = 0;//用来计一共有几件物品

int pric = 0;//用来计共消费金额

for(int i=0;ivec.size();i++)

{

gouwuchebean bean = (gouwuchebean)vec.get(i);

sum = sum+bean.getSum();

pric = pric+(bean.getPrice()*bean.getSum());

System.out.println("*第"+(i+1)+"种物品-----名称:"+bean.getName()+"---数量是:"+bean.getSum()+"---单价是:"+bean.getPrice()+"元----共计"+(bean.getSum()*bean.getPrice()+"元"));

}

System.out.println("物品共计"+sum+"件 共计金额是:"+pric+"元");

System.out.println("=============欢迎使用购物车==============");

}

public static void main(String[] args) {

// TODO Auto-generated method stub gw("电视机",100,1);//选择购买物品 价格和数量还有名字

gw("可口可乐",100,9);

gw("电视机",100,9);

gw("西瓜",10,5);

gw("电动车",3000,2);

gw("奥迪A6",4000000,2);

show();

}}测试结果

Java初学者,哪位友友能帮我设计一个简单的类似超市购物车的程序,参考一下~谢谢!

以前学习java又做个实例,挺值得学习的。

1.首先我先列出我们所需要的java类结构。

1)Database.java --------- 模拟存储商品的数据库。

2)McBean.java ------------ 商品实体类,一个普通的javabean,里面有商品的基本属性。

3)OrderItemBean.java --- 订单表。

4)ShoppingCar.java ------ 这个就是最主要的购物车,当然比较简单。

5)TestShoppingCar.java --- 这个是测试类。

2.下面贴出具体代码并带关键注释。

---Database.java

public class Database {

/*采用Map存储商品数据,为什么呢?我觉得这个问题你自己需要想下。

* Integer 为Map的key值,McBean为Map的value值。

*/

private static MapInteger, McBean data = new HashMapInteger, McBean();

public Database() {

McBean bean = new McBean();

bean.setId(1);

bean.setName("地瓜");

bean.setPrice(2.0);

bean.setInstuction("新鲜的地瓜");

data.put(1, bean);//把商品放入Map

bean = new McBean();

bean.setId(2);

bean.setName("土豆");

bean.setPrice(1.2);

bean.setInstuction("又好又大的土豆");

data.put(2, bean);//把商品放入Map

bean = new McBean();

bean.setId(3);

bean.setName("丝瓜");

bean.setPrice(1.5);

bean.setInstuction("本地丝瓜");

data.put(3, bean);//把商品放入Map

}

public void setMcBean(McBean mcBean){

data.put(mcBean.getId(),mcBean);

}

public McBean getMcBean(int nid) {

return data.get(nid);

}

}

---McBean.java

public class McBean {

private int id;//商品编号

private String name;//商品名

private double price;//商品价格

private String instuction;//商品说明

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 double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public String getInstuction() {

return instuction;

}

public void setInstuction(String instuction) {

this.instuction = instuction;

}

}

---ShoppingCar.java

public class ShoppingCar {

private double totalPrice; // 购物车所有商品总价格

private int totalCount; // 购物车所有商品数量

private MapInteger, OrderItemBean itemMap; // 商品编号与订单项的键值对

public ShoppingCar() {

itemMap = new HashMapInteger, OrderItemBean();

}

public void buy(int nid) {

OrderItemBean order = itemMap.get(nid);

McBean mb;

if (order == null) {

mb = new Database().getMcBean(nid);

order = new OrderItemBean(mb, 1);

itemMap.put(nid, order);

update(nid, 1);

} else {

order.setCount(order.getCount() + 1);

update(nid, 1);

}

}

public void delete(int nid) {

OrderItemBean delorder = itemMap.remove(nid);

totalCount = totalCount - delorder.getCount();

totalPrice = totalPrice - delorder.getThing().getPrice() * delorder.getCount();

}

public void update(int nid, int count) {

OrderItemBean updorder = itemMap.get(nid);

totalCount = totalCount + count;

totalPrice = totalPrice + updorder.getThing().getPrice() * count;

}

public void clear() {

itemMap.clear();

totalCount = 0;

totalPrice = 0.0;

}

public void show() {

DecimalFormat df = new DecimalFormat("¤#.##");

System.out.println("商品编号\t商品名称\t单价\t购买数量\t总价");

Set set = itemMap.keySet();

Iterator it = set.iterator();

while (it.hasNext()) {

OrderItemBean order = itemMap.get(it.next());

System.out.println(order.getThing().getId() + "\t"

+ order.getThing().getName() + "\t"

+ df.format(order.getThing().getPrice()) + "\t" + order.getCount()

+ "\t" + df.format(order.getCount() * order.getThing().getPrice()));

}

System.out.println("合计: 总数量: " + df.format(totalCount) + " 总价格: " + df.format(totalPrice));

System.out.println("**********************************************");

}

}

---OrderItemBean.java

public class OrderItemBean {

private McBean thing;//商品的实体

private int count;//商品的数量

public OrderItemBean(McBean thing, int count) {

super();

this.thing = thing;

this.count = count;

}

public McBean getThing() {

return thing;

}

public void setThing(McBean thing) {

this.thing = thing;

}

public int getCount() {

return count;

}

public void setCount(int count) {

this.count = count;

}

}

---TestShoppingCar.java

package com.shop;

public class TestShoppingCar {

public static void main(String[] args) {

ShoppingCar s = new ShoppingCar();

s.buy(1);//购买商品编号1的商品

s.buy(1);

s.buy(2);

s.buy(3);

s.buy(1);

s.show();//显示购物车的信息

s.delete(1);//删除商品编号为1的商品

s.show();

s.clear();

s.show();

}

}

3.打印输出结果

商品编号 商品名称 单价 购买数量 总价

1 地瓜 ¥2 3 ¥6

2 土豆 ¥1.2 1 ¥1.2

3 丝瓜 ¥1.5 1 ¥1.5

合计: 总数量: ¥5 总价格: ¥8.7

**********************************************

商品编号 商品名称 单价 购买数量 总价

2 土豆 ¥1.2 1 ¥1.2

3 丝瓜 ¥1.5 1 ¥1.5

合计: 总数量: ¥2 总价格: ¥2.7

**********************************************

商品编号 商品名称 单价 购买数量 总价

合计: 总数量: ¥0 总价格: ¥0

**********************************************

4.打字太累了,比较匆忙,但是主要靠你自己领会。哪里不清楚再提出来。

求java购物车例子。要用框架struts+hibernate+spring

java教程购物车Struts Hibernate实现shopcart

全部代码在

原理:利用session会话保持用户一次购物操作的购买记录,当用户点击“结帐”后将保存在session中的hashmap容器中的信息insert到DB中,完成一次购物操作。

模块所需要配置文件:hibernate.cfg.xml ,TableGoods.hbm.xml ,struts-config.xml

模块对应的jsp有:index.jsp(商品信息一览页面),buy.jsp(购买操作后的商品清单页面)

模块对应的action有:IndexAction (实现对DB中的商品表信息结果集的遍历,并转向对应的index.jsp)

ListAction (将JSP上的商品信息存入hashmap容器,并转向对应的buy.jsp)

UpdateAction (对buy.jsp页面上的商品数量修改的业务逻辑处理)

DeleteAction (对buy.jsp页面上的商品列表信息的业务逻辑处理)

模块所需的相关Java容器选择:存储商品id,sum,price,name,allprices信息用hashmap,主要是考虑到其key重复后可以覆盖上次的value记录。存储点击商品后的商品id用list容器,主要考虑到list是有序并可以重复的特点,用其可以跟踪用户多次点击相同商品的操作,并对商品的数量进行相应的增加。

模块主要Action类如下:

IndexAction:

public class IndexAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

//查找商品表中的所有商品信息

GoodsDAO dao = new GoodsDAO();

List list = dao.find();

request.setAttribute("lister",list);

return mapping.findForward("a");

}

}

ListAction:

public class ListAction extends Action {

// 将hashmap中value转到list中

public static List getList(HashMap hs) {

List list = new ArrayList();

Iterator itr = hs.keySet().iterator();

while (itr.hasNext()) {

list.add(hs.get(itr.next()));

}

return list;

}

//优化后的getList方法

public static List getList(HashMap hs) {

return new ArrayList(hs.values());

}

全部代码在

购物车的Java代码

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;public class ShoppingCartManager {

HashMapString, String hm=new HashMapString, String();

float totlePrice=0;

//添加book到购物车

public void addBook(String bookId,String bookQuantity){

if(hm.containsKey(bookId)){

int value=Integer.parseInt(hm.get(bookId));

value+=Integer.parseInt(bookQuantity);

hm.put(bookId, value+"");

}else{

hm.put(bookId, bookQuantity);

}

}

//修改数量

public void updateQuantity(String bookId,String bookQuantity){

hm.put(bookId, bookQuantity);

}

//获取购物车的所有信息 并计算总价

public ArrayListBookBean getShoppingCart(){

ArrayListBookBean al=new ArrayListBookBean();

IteratorString i=hm.keySet().iterator();

String ids="";

BookTableManager btm=new BookTableManager();

while(i.hasNext()){

ids=ids+","+i.next();

}

al= btm.selectByBookIds(ids);

totlePrice=0; //清空总价,防止无限累计

for(int j=0;jal.size();j++){

BookBean bb=al.get(j);

totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));

}

return al;

}

//获取总价

public float getTotlePrice(){

return totlePrice;

}

//根据ID获取数量

public String getQuantityById(String id){

String quantity=hm.get(id);

return quantity;

}

//清空购物车

public void clear(){

hm.clear();

}

//删除购物车中的一本书

public void deleteById(String id){

hm.remove(id);

}

}

用JAVA编写购物系统的代码是什么?(急)

算是最简单的吧

package cn.job01;

import java.util.Scanner;

public class Lx07 {

public static void choice() {

System.out.println("登陆菜单 ");

System.out.println("1登陆系统");

System.out.println("2退出");

}

static void choice1() {

System.out.println("购物管理系统客户信息");

System.out.println("1显示所有客户信息");

System.out.println("2添加客户信息");

System.out.println("3修改客户信息");

System.out.println("4查询客户信息");

}

static void choice2() {

System.out.println("购物管理系统真情回馈");

System.out.println("1幸运大放送");

System.out.println("2幸运抽奖");

System.out.println("3生日问候");

}

public static void main(String[] args) {

choice();

Scanner input = new Scanner(System.in);

System.out.println("请输入1or2");

int num = input.nextInt();

switch (num) {

case 1:

System.out.println("主菜单");

System.out.println("1客户信息管理");

System.out.println("2购物结算");

System.out.println("3真情回馈");

System.out.println("4注销");

break;

}

System.out.println("选择输入数字");

int num1 = input.nextInt();

switch (num1) {

case 1:

choice1();

break;

case 2:

System.out.println("购物结算");

break;

case 3:

choice2();

break;

case 4:

choice();

break;

}

}

}


分享文章:跨境购物流程java代码,跨境购物流程java代码
当前地址:http://scyingshan.cn/article/dssipie.html