當前位置:首頁 » 網購平台 » java購物車創建添加代碼
擴展閱讀
寧波奧德賽優惠價格 2021-03-15 14:26:02
丹尼斯購物卡能掛失么 2021-03-15 14:25:58
淘寶購物指紋驗證失敗 2021-03-15 14:24:44

java購物車創建添加代碼

發布時間: 2021-03-02 21:21:54

1. java中購物車如何實現的求代碼,網上等。謝謝

我知道的有兩種方式:

  1. 面向對象實現: 這個需要兩個表(一個是購物車表,一個是購物項表)

  2. 在一個表也可以實現(存: 用戶id, 商品id, 商品數量),根據用戶id來查詢商品.

2. 你好,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可能顯示不出來你的效果。不滿意得話我在給你編一個。

3. 購物車的Java代碼

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;public class ShoppingCartManager {

HashMap<String, String> hm=new HashMap<String, 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 ArrayList<BookBean> getShoppingCart(){
ArrayList<BookBean> al=new ArrayList<BookBean>();
Iterator<String> 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;j<al.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);
}
}

4. 急求java購物車代碼

package bean;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Administrator
* 購物車類:
* 為了方便將商品信息綁訂到session上面而設計的一個
* 工具,提供了商品的添加,刪除,列表,計價,清空,
* 修改功能。
*/
public class Cart {
//items屬性:用來保存商品
private List<CartItem> items =
new ArrayList<CartItem>();
/**
* 將商品添加到購物車
*/
public boolean add(CartItem item){
for(int i=0;i<items.size();i++){
CartItem curr = items.get(i);
if(curr.getC().getId() == item.getC().getId()){
//該商品已經購買過
return false;
}
}
//沒有購買過,則添加該商品
items.add(item);
return true;
}
/**
* 從購物車當中刪除某件商品
*/
public void delete(int id){
for(int i=0;i<items.size();i++){
CartItem curr = items.get(i);
if(curr.getC().getId() == id){
items.remove(curr);
return;
}
}
}
/**
* 獲得購物車中所有商品信息
*/
public List<CartItem> list(){
return items;
}
/**
* 商品總價
*/
public double cost(){
double total = 0;
for(int i=0;i<items.size();i++){
CartItem curr = items.get(i);
total += curr.getC().getPrice() * curr.getQty();
}
return total;
}
/**
* 清空購物車中的所有商品
*/
public void clear(){
items.clear();
}
/**
* 修改購物車中某種商品的數量
*/
public void modify(int id,int qty){
for(int i=0;i<items.size();i++){
CartItem curr = items.get(i);
if(curr.getC().getId() == id){
curr.setQty(qty);
return;
}
}
}
}

5. Java使用字元串生成器來保存購物車中的商品信息,並能實現商品信息的添加、刪除以及修改等功能

publicclassCart{
publicStringBuilderdata;
publicfloattotal;
publicCart(){
data=newStringBuilder();
}
publicvoidbuy(Goodsg){
g.gtotal=g.gnum*g.gprice;
total=total+g.gtotal;
data.append("[");
data.append(g.gname+"|");
data.append(g.gprice+"|");
data.append(g.gnum+"|");//還是豎線看著方便
data.append(g.gtotal);
data.append("]");
}
publicvoiddelete(Goodsg){
ints=data.indexOf(g.gname);
inte=data.indexOf("]",s);
data.delete(s-1,e+1);
total=total-g.gtotal;//刪除商品,需要修改總額
}
publicvoipdate(Goodsg){
data.replace(3,10,"["+g.gname+"|"+g.gprice+"|"+g.gnum+"|"+g.gtotal);
}
publicvoidshow(){
System.out.print("總計金額:"+total+"");
System.out.println(data);
}
}


//Excute類里有點小錯誤,
//總覺得update方法不對頭,你想怎麼做?

6. 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可能顯示不出來你的效果。不滿意得話我在給你編一個。

7. 達內 java的購物車商品 不能重復添加 的代碼。

在購物車中寫:購抄物車襲一般放到session域中。其中有商品小計,有商品總計的一些求和計算。我記得用Map來弄的。 在購物車中添加map集合。根據放到購物車中的key來判斷在map中是否存在,如果存在那就在原有商品上+1,如果不存在,那麼就可以添加商品到購物車。

8. java 購物車 的代碼創造

用不用都沒關系的
這又不是強制的
只是寫了可以方便人理解而已
沒有說是private的就必須寫this的說

9. java購物車用servlet做,sql資料庫,能實現基本增加、刪除、修改商品,結賬,然後下訂單,求源代碼

加入購物車的代碼:
//把商品保存到session中
HttpSession session=request.getSession();

List<Goods> list=(List) session.getAttribute("list");
int gid=Integer.parseInt(request.getParameter("gid"));
int num=Integer.parseInt(request.getParameter("num"));
Goods goods=null;

if(list==null || list.size()<0){
list=new ArrayList();
}else{
for (Goods g : list) {
if(g.getGid()==gid){
goods=g;
g.setSum(g.getSum()+num);
break;
}
}
}

if(goods==null){
goods=goodsDao.queryGoodsByG_id(gid);
goods.setSum(num);
list.add(goods);
}
session.setAttribute("list", list);
request.getRequestDispatcher("/index.jsp").forward(request, response);

增加、刪除、修改商品差不多,只是sql語句不同

10. java簡單的購物車代碼

packageTest;
importjava.util.LinkedHashMap;
importjava.util.Map;
importjava.util.Map.Entry;
importjava.util.Scanner;

publicclassTest{
publicstaticvoidmain(String[]args){
init();//初始化
Map<String,String>map=newLinkedHashMap<>();
while(true){
Scannerin=newScanner(System.in);
map=buy(in,map);//選擇
System.out.println();
System.out.println("還要繼續購物嗎?(Y/N)");
Stringjx=in.nextLine();
if(jx.equals("N")){
break;
}
}
print(map);
}
publicstaticvoidprint(Map<String,String>m){
System.out.println(" ******************");
System.out.println("購物車清單");
Integerhj=0;
for(Entry<String,String>entry:m.entrySet()){
Stringkey=entry.getKey();
Stringvalue=entry.getValue();
if(key.equals("1")){
hj+=Integer.parseInt(value)*3;
System.out.println("哇哈哈純凈水:"+value+"件,合計:¥"+Integer.parseInt(value)*3);
}elseif(key.equals("2")){
hj+=Integer.parseInt(value)*5;
System.out.println("康師傅方便麵:"+value+"件,合計:¥"+Integer.parseInt(value)*5);
}elseif(key.equals("3")){
hj+=Integer.parseInt(value)*4;
System.out.println("可口可樂:"+value+"件,合計:¥"+Integer.parseInt(value)*4);
}
}
System.out.println("合計金額:¥"+hj);
}
publicstaticvoidinit(){
System.out.println("******************");
System.out.println(" 商品列表 ");
System.out.println("商品名稱價格");
System.out.println("1.哇哈哈純凈水¥3");
System.out.println("2.康師傅方便麵¥5");
System.out.println("3.可口可樂¥4");
System.out.println("******************");
}

publicstaticMap<String,String>buy(Scannerscan,Map<String,String>m){
System.out.print("請輸入編號:");
Stringbh=scan.nextLine();
System.out.print("請輸入購買數量:");
Stringnum=scan.nextLine();
if(m.size()>0&&m.keySet().contains(bh)){
m.put(bh,(Integer.parseInt(bh)+Integer.parseInt(num))+"");
}else{
m.put(bh,num);
}
returnm;
}
}