成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Java查看購物車代碼,java購物車和訂單

java web購物車

首先你應(yīng)該對(duì)java有個(gè)基礎(chǔ)的了解,什么是變量,什么事關(guān)鍵字。我先給你每行都注釋下

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、吉州網(wǎng)絡(luò)推廣、成都微信小程序、吉州網(wǎng)絡(luò)營銷、吉州企業(yè)策劃、吉州品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供吉州建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:jinyejixie.com

//公共的類,類名為ShopingServlet 繼承父類HttpServlet

public class ShopingServlet extends HttpServlet {

實(shí)現(xiàn)父類方法doGet 意識(shí)就是通過get請(qǐng)求的就會(huì)進(jìn)入這個(gè)方法,下面還有一個(gè)doPost方法就是通過post方式請(qǐng)求的會(huì)進(jìn)入doPost,至于這兩個(gè)的區(qū)別:doGet安全性差,參數(shù)是在瀏覽器連接中直接顯示,然而doPost就是不會(huì)顯示的安全性要高,這也是最直觀的區(qū)別

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//通過request獲取session

HttpSession session=request.getSession();

//獲取參數(shù)為id的值

String id=request.getParameter("id");

//判斷id的值是否為null

if(id!=null)

{

//id不為空進(jìn)入這里面,在獲取參數(shù)為book的值,book的值為數(shù)組類型

Book[]book=(Book[])session.getAttribute("book");

在判斷book是否為null

if(book!=null)

{

//部位空進(jìn)入,進(jìn)行循環(huán)

for(int i=0;ibook.length;i++)

{

//判斷book數(shù)組中的第i個(gè)的BookId是否和之前的參數(shù)Id相同

if(book[i].getBookId().equals(id))

{

//相同,就把book數(shù)組中的第i個(gè)的id賦值為空

book[i].setid();

}

}

把當(dāng)前book存入session中,變量名為book

session.setAttribute("book", book);

}

}

//跳轉(zhuǎn)到頁面/test4E/Shopping.jsp

response.sendRedirect("/test4E/Shopping.jsp");

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//調(diào)用上面的doGet方法

doGet(request,response);

}

}

下面的代碼和這個(gè)是重復(fù)的,不知道為什么你要發(fā)布兩遍,你可以對(duì)比一下

購物車的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);

}

}

//修改數(shù)量

public void updateQuantity(String bookId,String bookQuantity){

hm.put(bookId, bookQuantity);

}

//獲取購物車的所有信息 并計(jì)算總價(jià)

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; //清空總價(jià),防止無限累計(jì)

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

BookBean bb=al.get(j);

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

}

return al;

}

//獲取總價(jià)

public float getTotlePrice(){

return totlePrice;

}

//根據(jù)ID獲取數(shù)量

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購物車代碼?

import java.awt.*;

import java.awt.event.*;

class ShopFrame extends Frame implements ActionListener

{ Label label1,label2,label3,label4;

Button button1,button2,button3,button4,button5;

TextArea text;

Panel panel1,panel2;

static float sum=0.0f;

ShopFrame(String s)

{ super(s);

setLayout(new BorderLayout());

label1=new Label("面紙:3元",Label.LEFT);

label2=new Label("鋼筆:5元",Label.LEFT);

label3=new Label("書:10元",Label.LEFT);

label4=new Label("襪子:8元",Label.LEFT);

button1=new Button("加入購物車");

button2=new Button("加入購物車");

button3=new Button("加入購物車");

button4=new Button("加入購物車");

button5=new Button("查看購物車");

text=new TextArea("商品有:"+"\n",5,10);

text.setEditable(false);

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e)

{ System.exit(0);

}

}

);

button1.addActionListener(this);

button2.addActionListener(this);

button3.addActionListener(this);

button4.addActionListener(this);

button5.addActionListener(this);

panel1=new Panel();

panel2=new Panel();

panel1.add(label1);

panel1.add(button1);

panel1.add(label2);

panel1.add(button2);

panel1.add(label3);

panel1.add(button3);

panel1.add(label4);

panel1.add(button4);

panel2.setLayout(new BorderLayout());

panel2.add(button5,BorderLayout.NORTH);

panel2.add(text,BorderLayout.SOUTH);

this.add(panel1,BorderLayout.CENTER);

this.add(panel2,BorderLayout.SOUTH);

setBounds(100,100,350,250);

setVisible(true);

validate();

}

public void actionPerformed(ActionEvent e)

{ if(e.getSource()==button1)

{ text.append("一個(gè)面紙、");

sum=sum+3;

}

else if(e.getSource()==button2)

{ text.append("一只鋼筆、");

sum=sum+5;

}

else if(e.getSource()==button3)

{ text.append("一本書、");

sum=sum+10;

}

else if(e.getSource()==button4)

{ text.append("一雙襪子、");

sum=sum+8;

}

else if(e.getSource()==button5)

{

text.append("\n"+"總價(jià)為:"+"\n"+sum);

}

}

}

public class Shopping {

public static void main(String[] args) {

new ShopFrame("購物車");

}

}

我沒用Swing可能顯示不出來你的效果。不滿意得話我在給你編一個(gè)。

java 如何編寫購物車

用Vector 或者是HashMap去裝

下面有部分代碼你去看吧

package com.aptech.restrant.DAO;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.sql.Connection;

import com.aptech.restrant.bean.CartItemBean;

import com.aptech.restrant.bean.FoodBean;

public class CartModel {

private Connection conn;

public CartModel(Connection conn) {

this.conn=conn;

}

/**

* 得到訂餐列表

*

* @return

*/

public List changeToList(Map carts) {

// 將Set中元素轉(zhuǎn)換成數(shù)組,以便使用循環(huán)進(jìn)行遍歷

Object[] foodItems = carts.keySet().toArray();

// 定義double變量total,用于存放購物車內(nèi)餐品總價(jià)格

double total = 0;

List list = new ArrayList();

// 循環(huán)遍歷購物車內(nèi)餐品,并顯示各個(gè)餐品的餐品名稱,價(jià)格,數(shù)量

for (int i = 0; i foodItems.length; i++) {

// 從Map對(duì)象cart中取出第i個(gè)餐品,放入cartItem中

CartItemBean cartItem = (CartItemBean) carts

.get((String) foodItems[i]);

// 從cartItem中取出FoodBean對(duì)象

FoodBean food1 = cartItem.getFoodBean();

// 定義int類型變量quantity,用于表示購物車中單個(gè)餐品的數(shù)量

int quantity = cartItem.getQuantity();

// 定義double變量price,表示餐品單價(jià)

double price = food1.getFoodPrice();

// 定義double變量,subtotal表示單個(gè)餐品總價(jià)

double subtotal = quantity * price;

// // 計(jì)算購物車內(nèi)餐品總價(jià)格

total += subtotal;

cartItem.setSubtotal(subtotal);

cartItem.setTotal(total);

list.add(cartItem);

}

return list;

}

/**

* 增加訂餐

*/

public Map add(Map cart, String foodID) {

// 購物車為空

if (cart == null) {

cart = new HashMap();

}

FoodModel fd = new FoodModel(conn);

FoodBean food = fd.findFoodById(foodID);

// 判斷購物車是否放東西(第一次點(diǎn)餐)

if (cart.isEmpty()) {

CartItemBean cartBean = new CartItemBean(food, 1);

cart.put(foodID, cartBean);

} else {

// 判斷當(dāng)前菜是否在購物車中,false表示當(dāng)前菜沒有被點(diǎn)過。。

boolean flag = false;

// 得到鍵的集合

Set set = cart.keySet();

// 遍歷集合

Object[] obj = set.toArray();

for (int i = 0; i obj.length; i++) {

Object object = obj[i];

// 如果購物車已經(jīng)存在當(dāng)前菜,數(shù)量+1

if (object.equals(foodID)) {

int quantity = ((CartItemBean) cart.get(object))

.getQuantity();

quantity += 1;

System.out.println(quantity);

((CartItemBean) cart.get(object)).setQuantity(quantity);

flag = true;

break;

}

}

if (flag == false) {

// 把當(dāng)前菜放到購物車?yán)锩?/p>

CartItemBean cartBean = new CartItemBean(food, 1);

cart.put(foodID, cartBean);

}

}

return cart;

}

/**

* 取消訂餐

*/

public Map remove(Map cart, String foodID) {

cart.remove(foodID);

return cart;

}

/**

* 更新購物車信息

*

* @param cart

* @param foodID

* @return

*/

public MapString, CartItemBean update(Map cart, String foodID,

boolean isAddorRemove) {

Map map;

if (isAddorRemove) {

map = add(cart, foodID);

} else {

map = remove(cart, foodID);

}

return map;

}

}

急求java購物車代碼

package bean;

import java.util.ArrayList;

import java.util.List;

/**

*

* @author Administrator

* 購物車類:

* 為了方便將商品信息綁訂到session上面而設(shè)計(jì)的一個(gè)

* 工具,提供了商品的添加,刪除,列表,計(jì)價(jià),清空,

* 修改功能。

*/

public class Cart {

//items屬性:用來保存商品

private ListCartItem items =

new ArrayListCartItem();

/**

* 將商品添加到購物車

*/

public boolean add(CartItem item){

for(int i=0;iitems.size();i++){

CartItem curr = items.get(i);

if(curr.getC().getId() == item.getC().getId()){

//該商品已經(jīng)購買過

return false;

}

}

//沒有購買過,則添加該商品

items.add(item);

return true;

}

/**

* 從購物車當(dāng)中刪除某件商品

*/

public void delete(int id){

for(int i=0;iitems.size();i++){

CartItem curr = items.get(i);

if(curr.getC().getId() == id){

items.remove(curr);

return;

}

}

}

/**

* 獲得購物車中所有商品信息

*/

public ListCartItem list(){

return items;

}

/**

* 商品總價(jià)

*/

public double cost(){

double total = 0;

for(int i=0;iitems.size();i++){

CartItem curr = items.get(i);

total += curr.getC().getPrice() * curr.getQty();

}

return total;

}

/**

* 清空購物車中的所有商品

*/

public void clear(){

items.clear();

}

/**

* 修改購物車中某種商品的數(shù)量

*/

public void modify(int id,int qty){

for(int i=0;iitems.size();i++){

CartItem curr = items.get(i);

if(curr.getC().getId() == id){

curr.setQty(qty);

return;

}

}

}

}

java簡單的購物車代碼

package?Test;

import?java.util.LinkedHashMap;

import?java.util.Map;

import?java.util.Map.Entry;

import?java.util.Scanner;

public?class?Test?{

public?static?void?main(String[]?args)?{

init();//初始化

MapString,String?map?=?new?LinkedHashMap();

while(true){

Scanner?in=?new?Scanner(System.in);

map?=?buy(in,map);//選擇

System.out.println();

System.out.println("還要繼續(xù)購物嗎?(Y/N)");

String?jx?=?in.nextLine();

if(jx.equals("N")){

break;

}

}

print(map);

}

public?static?void?print(MapString,?String?m){

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

System.out.println("???????購物車清單");

Integer?hj?=?0;

for?(EntryString,?String?entry?:?m.entrySet())?{

String?key?=?entry.getKey();

String?value?=?entry.getValue();

if(key.equals("1")){

hj?+=?Integer.parseInt(value)*3;

System.out.println("哇哈哈純凈水:?"+value+"件,合計(jì):¥"+Integer.parseInt(value)*3);

}else?if(key.equals("2")){

hj?+=?Integer.parseInt(value)*5;

System.out.println("康師傅方便面:?"+value+"件,合計(jì):¥"+Integer.parseInt(value)*5);

}else?if(key.equals("3")){

hj?+=?Integer.parseInt(value)*4;

System.out.println("可口可樂:?"+value+"件,合計(jì):¥"+Integer.parseInt(value)*4);

}

}

System.out.println("合計(jì)金額:¥"+hj);

}

public?static?void?init(){

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

System.out.println("\t商品列表\n");

System.out.println("??????????????商品名稱????????????????價(jià)格");

System.out.println("1.???哇哈哈純凈水????????¥3");

System.out.println("2.???康師傅方便面????????¥5");

System.out.println("3.???可口可樂????????????????¥4");

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

}

public?static?MapString,String?buy(Scanner?scan,MapString,String?m){

System.out.print("請(qǐng)輸入編號(hào):");

String?bh?=?scan.nextLine();

System.out.print("請(qǐng)輸入購買數(shù)量:");

String?num?=?scan.nextLine();

if(m.size()0??m.keySet().contains(bh)){

m.put(bh,(Integer.parseInt(bh)+Integer.parseInt(num))+"");

}else{

m.put(bh,?num);

}

return?m;

}

}

網(wǎng)站題目:Java查看購物車代碼,java購物車和訂單
轉(zhuǎn)載來源:http://jinyejixie.com/article10/dssscdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作網(wǎng)站內(nèi)鏈、App設(shè)計(jì)營銷型網(wǎng)站建設(shè)定制開發(fā)、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)
榕江县| 嘉鱼县| 和平区| 永平县| 兴仁县| 郁南县| 南平市| 青冈县| 绥滨县| 丘北县| 武冈市| 新晃| 万州区| 清苑县| 陆良县| 和顺县| 惠州市| 延长县| 襄垣县| 鹿邑县| 彭阳县| 普陀区| 保德县| 滨海县| 嘉定区| 朝阳市| 梨树县| 阜城县| 香港| 和林格尔县| 金塔县| 留坝县| 射洪县| 平顶山市| 剑阁县| 平远县| 乐清市| 高阳县| 清远市| 杭锦旗| 桂阳县|