急求java购物车代码

急求java购物车代码,第1张

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=0i<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=0i<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=0i<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=0i<items.size()i++){

CartItem curr = items.get(i)

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

curr.setQty(qty)

return

}

}

}

}

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可能显示不出来你的效果。不满意得话我在给你编一个。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9942574.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存