?? lx8_7.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 + -