JAVA購物車代碼民工 java購物車和訂單

JAVA語言編寫的網(wǎng)上訂餐系統(tǒng)購物車功能如何實現(xiàn)?

用Vector 或者是HashMap去裝

成都網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、成都網(wǎng)站建設(shè)公司、微信開發(fā)、小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。核心團隊均擁有互聯(lián)網(wǎng)行業(yè)多年經(jīng)驗,服務(wù)眾多知名企業(yè)客戶;涵蓋的客戶類型包括:發(fā)電機租賃等眾多領(lǐng)域,積累了大量豐富的經(jīng)驗,同時也獲得了客戶的一致贊揚!

下面有部分代碼你去看吧

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)進行遍歷

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

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

double?total?=?0;

List?list?=?new?ArrayList();

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

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

//?從Map對象cart中取出第i個餐品,放入cartItem中

CartItemBean?cartItem?=?(CartItemBean)?carts

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

//?從cartItem中取出FoodBean對象

FoodBean?food1?=?cartItem.getFoodBean();

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

int?quantity?=?cartItem.getQuantity();

//?定義double變量price,表示餐品單價

double?price?=?food1.getFoodPrice();

//?定義double變量,subtotal表示單個餐品總價

double?subtotal?=?quantity?*?price;

//?//?計算購物車內(nèi)餐品總價格

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);

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

if?(cart.isEmpty())?{

CartItemBean?cartBean?=?new?CartItemBean(food,?1);

cart.put(foodID,?cartBean);

}?else?{

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

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)前菜放到購物車里面

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

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("一個面紙、");

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"+"總價為:"+"\n"+sum);

}

}

}

public class Shopping {

public static void main(String[] args) {

new ShopFrame("購物車");

}

}

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

Java初學(xué)者,哪位友友能幫我設(shè)計一個簡單的類似超市購物車的程序,參考一下~謝謝!

以前學(xué)習(xí)java又做個實例,挺值得學(xué)習(xí)的。

1.首先我先列出我們所需要的java類結(jié)構(gòu)。

1)Database.java --------- 模擬存儲商品的數(shù)據(jù)庫。

2)McBean.java ------------ 商品實體類,一個普通的javabean,里面有商品的基本屬性。

3)OrderItemBean.java --- 訂單表。

4)ShoppingCar.java ------ 這個就是最主要的購物車,當(dāng)然比較簡單。

5)TestShoppingCar.java --- 這個是測試類。

2.下面貼出具體代碼并帶關(guān)鍵注釋。

---Database.java

public class Database {

/*采用Map存儲商品數(shù)據(jù),為什么呢?我覺得這個問題你自己需要想下。

* 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; // 購物車所有商品數(shù)量

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購買數(shù)量\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("合計: 總數(shù)量: " + df.format(totalCount) + " 總價格: " + df.format(totalPrice));

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

}

}

---OrderItemBean.java

public class OrderItemBean {

private McBean thing;//商品的實體

private int count;//商品的數(shù)量

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.打印輸出結(jié)果

商品編號 商品名稱 單價 購買數(shù)量 總價

1 地瓜 ¥2 3 ¥6

2 土豆 ¥1.2 1 ¥1.2

3 絲瓜 ¥1.5 1 ¥1.5

合計: 總數(shù)量: ¥5 總價格: ¥8.7

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

商品編號 商品名稱 單價 購買數(shù)量 總價

2 土豆 ¥1.2 1 ¥1.2

3 絲瓜 ¥1.5 1 ¥1.5

合計: 總數(shù)量: ¥2 總價格: ¥2.7

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

商品編號 商品名稱 單價 購買數(shù)量 總價

合計: 總數(shù)量: ¥0 總價格: ¥0

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

4.打字太累了,比較匆忙,但是主要靠你自己領(lǐng)會。哪里不清楚再提出來。

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+"件,合計:¥"+Integer.parseInt(value)*3);

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

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

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

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

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

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

}

}

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

}

public?static?void?init(){

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

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

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

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("請輸入編號:");

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

System.out.print("請輸入購買數(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;

}

}

求一個JAVA里用map集合寫一個購物車的代碼,購物車實現(xiàn)商品的添加,刪除,查詢和結(jié)算,寫了半天沒

建一個靜態(tài)的Map集合 做購物車的集合

key值 放商品的ID value 放 商品對象.

對map 增刪改查就好了.. 結(jié)算完了 清空map

分享文章:JAVA購物車代碼民工 java購物車和訂單
文章出自:http://muchs.cn/article46/docdjhg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站網(wǎng)站設(shè)計、服務(wù)器托管網(wǎng)站排名、網(wǎng)站收錄、定制開發(fā)

廣告

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

成都app開發(fā)公司