⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lx8_7.java

📁 java简单购物车的实现Vector创建模拟购物结算界面并实现结算功能。其中使用了内部类和Vector 向量。运行程序后
💻 JAVA
字号:
package com.eekq.structure.composite.security;
import java.util.Iterator;
import java.util.Vector;
/*
 * 对多个物品的管理
 * */
public class MultiResImpl implements IRes {
    /**购物车*/
    private Vector car = new Vector();
    private static float totle = 0.0f;

public void pay() {
        if(!car.isEmpty()){
        System.out.println("名称        价格\n");
        shopping();
        System.out.println("\n总价:" + totle + "元");
        }else{
            System.out.println("您好,你没有购买任何物品,不用买单!");
        }
    }
    public void shopping() {
        if (car != null || !car.isEmpty()) {
            Iterator it = car.iterator();
            SingleResImpl res = null;
            Object temp = null;// 临时对象
            while (it.hasNext()) {
                temp = it.next();import java.util.*;import java.applet.*;import java.awt.*;import java.awt.event.*;

public class LX8_7 extends Applet implements ActionListener{

         Vector vect=new Vector();

         Label label1,label2,label0;

         TextField t0,t1,t2,t3;

         TextArea area;

         Button bnt1,bnt2,bnt3,bnt4,bnt5,bnt6,bnt7,bnt8,bnt9;

         public void init(){

           vect=new Vector(1,1);

           label0=new Label("品名");label1=new Label("数量");label2=new Label("单价");

           t0=new TextField(10); t1=new TextField(10);t2=new TextField(10);t3=new TextField(4);

           area=new TextArea(10,50);

           bnt1=new Button("添加");bnt2=new Button("删除");bnt3=new Button("定位");

           bnt4=new Button("清空");bnt5=new Button("付款");bnt6=new Button("清单");

           bnt7=new Button("清屏");bnt8=new Button("首位");bnt9=new Button("末位");

           add(label0);add(t0);add(label1);add(t1);add(label2);add(t2);add(bnt1);add(bnt2);

           add(bnt3);add(t3); add(bnt4);add(bnt5);add(bnt6);add(bnt7);add(bnt8);add(bnt9);

           add(area);

           bnt1.addActionListener(this);bnt2.addActionListener(this);

           bnt3.addActionListener(this);bnt4.addActionListener(this);

           bnt5.addActionListener(this);bnt6.addActionListener(this);

           bnt7.addActionListener(this);bnt8.addActionListener(this);

           bnt9.addActionListener(this);  }

           public void actionPerformed(ActionEvent e) {

                list v=new list(t0.getText(),t1.getText(),t2.getText());

               String s0=t0.getText()+t1.getText()+"台,单价"+t2.getText()+"元";

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

                       vect.addElement(v);

                       int k1=vect.indexOf(v);

                       area.append("添加"+k1+"记录:"+s0+"\n");

                       t3.setText(String.valueOf(k1));}

              else if(e.getSource()==bnt2){

                       int k1=Integer.parseInt(t3.getText());

                       vect.removeElementAt(k1);

                       area.append("\n"+"删除了记录"+s0+"\n");}

              else if(e.getSource()==bnt3){

                       int k=Integer.parseInt(t3.getText());

                       list l1=(list)vect.elementAt(k);

                       t0.setText(l1.pm);

                       t1.setText(l1.number);

                       t2.setText(l1.dj);}

              else if(e.getSource()==bnt4){

                       vect.clear();

                       area.append("清除所有记录:"+vect.size()+"\n");}

              else if(e.getSource()==bnt5){

                       area.append("你购买了"+vect.size()+"种物品");

                       double s=0;

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

                                list r=(list)vect.elementAt(i);

                                s=s+Integer.parseInt(r.number)*Double.parseDouble(r.dj);}

                       area.append("总金额为:"+s+"元,请付款,谢谢"+"\n");}

              else if(e.getSource()==bnt6){

                       area.append("\n"+"你购买了"+vect.size()+"种物品,如下所示"+"\n");

                       String t="";

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

                                list b=(list)vect.elementAt(i);

                                area.append(t=i+"记录"+b.pm+b.number+"台,单价"+b.dj+"元"+"\n");}}

              else if(e.getSource()==bnt7){

                       area.setText("");}

              else if(e.getSource()==bnt8){

                       list v0=(list)vect.elementAt(0);

                       t0.setText(v0.pm);t1.setText(v0.number);t2.setText(v0.dj);t3.setText("0");}

              else if(e.getSource()==bnt9){

                       list v2=(list)vect.elementAt(vect.size()-1);

                   t0.setText(v2.pm);t1.setText(v2.number);t2.setText(v2.dj);                   t3.setText(String.valueOf(vect.size()-1));}}

     class list{

              String pm,number,dj;

              list(String pm,String number,String dj){

                       this.number=number;

                       this.pm=pm;

                       this.dj=dj;}

              list(){this("","","");}}}

                if (temp instanceof MultiResImpl) {
                    ((MultiResImpl) temp).shopping();
                } else {
                    res = (SingleResImpl) temp;
                    synchronized (this) {
                        totle += res.getMoney();
                    }
                    System.out.println(res.getName() + "            " + res.getMoney()
                            + "元");
                }
            }
        }
    }
    /**加入新的物品*/
    public void addRes(IRes res) {
        car.add(res);
    }

    /**放回物品*/
    public void removeRes(IRes res) {
        car.remove(res);
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -