算是最簡(jiǎn)單的吧
創(chuàng)新互聯(lián)公司專注于網(wǎng)站建設(shè)|成都網(wǎng)站改版|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計(jì)與制作經(jīng)驗(yàn),為許多企業(yè)提供了網(wǎng)站定制設(shè)計(jì)服務(wù),案例作品覆蓋成都酒樓設(shè)計(jì)等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身建設(shè)品質(zhì)網(wǎng)站。
package cn.job01;
import java.util.Scanner;
public class Lx07 {
public static void choice() {
System.out.println("登陸菜單 ");
System.out.println("1登陸系統(tǒng)");
System.out.println("2退出");
}
static void choice1() {
System.out.println("購(gòu)物管理系統(tǒng)客戶信息");
System.out.println("1顯示所有客戶信息");
System.out.println("2添加客戶信息");
System.out.println("3修改客戶信息");
System.out.println("4查詢客戶信息");
}
static void choice2() {
System.out.println("購(gòu)物管理系統(tǒng)真情回饋");
System.out.println("1幸運(yùn)大放送");
System.out.println("2幸運(yùn)抽獎(jiǎng)");
System.out.println("3生日問候");
}
public static void main(String[] args) {
choice();
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)輸入1or2");
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("主菜單");
System.out.println("1客戶信息管理");
System.out.println("2購(gòu)物結(jié)算");
System.out.println("3真情回饋");
System.out.println("4注銷");
break;
}
System.out.println("選擇輸入數(shù)字");
int num1 = input.nextInt();
switch (num1) {
case 1:
choice1();
break;
case 2:
System.out.println("購(gòu)物結(jié)算");
break;
case 3:
choice2();
break;
case 4:
choice();
break;
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;public class ShoppingCartManager {
HashMapString, String hm=new HashMapString, String();
float totlePrice=0;
//添加book到購(gòu)物車
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);
}
}
//修改數(shù)量
public void updateQuantity(String bookId,String bookQuantity){
hm.put(bookId, bookQuantity);
}
//獲取購(gòu)物車的所有信息 并計(jì)算總價(jià)
public ArrayListBookBean getShoppingCart(){
ArrayListBookBean al=new ArrayListBookBean();
IteratorString i=hm.keySet().iterator();
String ids="";
BookTableManager btm=new BookTableManager();
while(i.hasNext()){
ids=ids+","+i.next();
}
al= btm.selectByBookIds(ids);
totlePrice=0; //清空總價(jià),防止無(wú)限累計(jì)
for(int j=0;jal.size();j++){
BookBean bb=al.get(j);
totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));
}
return al;
}
//獲取總價(jià)
public float getTotlePrice(){
return totlePrice;
}
//根據(jù)ID獲取數(shù)量
public String getQuantityById(String id){
String quantity=hm.get(id);
return quantity;
}
//清空購(gòu)物車
public void clear(){
hm.clear();
}
//刪除購(gòu)物車中的一本書
public void deleteById(String id){
hm.remove(id);
}
}
綁定一個(gè)事件到被點(diǎn)擊的窗體或控件,點(diǎn)擊后獲取控件信息,然后傳參給一個(gè)自定義的窗體類的構(gòu)造函數(shù),如A = new oneDialog(參數(shù)1,參數(shù)2....);然后A.setVisible(true)。大概就是這個(gè)樣子,有不懂得可以繼續(xù)問
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("加入購(gòu)物車");
button2=new Button("加入購(gòu)物車");
button3=new Button("加入購(gòu)物車");
button4=new Button("加入購(gòu)物車");
button5=new Button("查看購(gòu)物車");
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("一個(gè)面紙、");
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"+"總價(jià)為:"+"\n"+sum);
}
}
}
public class Shopping {
public static void main(String[] args) {
new ShopFrame("購(gòu)物車");
}
}
我沒用Swing可能顯示不出來(lái)你的效果。不滿意得話我在給你編一個(gè)。