| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 844 人关注过本帖
标题:网上书店源程序,千里冰封见收!
取消只看楼主 加入收藏
都市猎人
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2006-3-26
收藏
 问题点数:0 回复次数:0 
网上书店源程序,千里冰封见收!

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
public class BookStore {

public static void main(String[] args) {
// Create application frame.
UI ui =new UI();
ui.inital();
//调用ININTAL方法
}
}

class UI implements ActionListener
{
private Frame fm;
private Panel pm;
private Vector books;//图书信息
private int[] pq;//购买数量
private int idx;//当前页码
private Checkbox[] cb;//购买复选框
private TextField[] tfpq;//购买数量文本框

private Button[] bnote;//内容介绍按纽组
private Button clear;
private Button prev;
private Button next;
private Button jump;
private Button review;
private Button submit;
private Button ext;

private Dialog rew;//统计对话框
private Dialog dnote;//图书介绍内容
private TextArea revTa;//已选清单
private TextField toPage=new TextField(4);

private String dbServer="127.0.0.1";
private Connection conn;
private Statement stmt;
private NumberFormat nf1,nf2;

public void inital()
{
this.connectDB();
this.readDB();

pm=new Panel();
pm.setLayout(new GridLayout(16,1,1,1));
pm.setBackground(new Color (95,145,145));
this.connectDB();
this.readDB();


Panel p=new Panel();
p.setBackground(new Color(95,145,145));
p.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));

clear=new Button("全部解除");
clear.setActionCommand("Clear");
clear.addActionListener(this);

prev=new Button("上一页");
prev.setActionCommand("Prev");
prev.addActionListener(this);

next=new Button("下一页");
next.setActionCommand("Next");
next.addActionListener(this);

jump=new Button("转到指定页");
jump.setActionCommand("Goto");
jump.addActionListener(this);

review=new Button("已选书清单");
review.setActionCommand("Review");
review.addActionListener(this);

submit=new Button("提交");
submit.setActionCommand("Submit");
submit.addActionListener(this);

ext=new Button("退出");
ext.setActionCommand("exit");
ext.addActionListener(this);

p.add(clear);
p.add(prev);
p.add(next);
p.add(jump);
p.add(review);
p.add(submit);
p.add(ext);

nf1=NumberFormat.getInstance();
nf1.setMinimumIntegerDigits(3);
nf2=NumberFormat.getInstance();
nf2.setMinimumFractionDigits(3);

fm=new Frame("英雄书店管理系统");
fm.setLayout(new BorderLayout(1,1));
fm.add(pm,"Center");
fm.add(p,"South");
fm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
fm.setSize(655,390);
fm.setVisible(true);
idx=0;
this.setButtonState();
this.showPage(idx);
}
public void connectDB()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

conn=DriverManager.getConnection("jdbc:odbc:xixi","hehe","123");
stmt=conn.createStatement();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void closeDB()
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}

public void exit()
{
this.closeDB();
System.exit(1);
}

public void readDB()
{
books=new Vector();
Book b;
try
{
ResultSet rs=stmt.executeQuery("select*from bstore");
while(rs.next())
{
b=new Book(
rs.getString(1).trim(),
rs.getString(2).trim(),
rs.getDouble(3),
rs.getString(4).trim(),
rs.getString(5).trim(),
rs.getString(6).trim(),
rs.getInt(7)
);
books.add(b);
}
pq=new int [books.size()];
cb=new Checkbox[books.size()];
bnote=new Button[books.size()];
tfpq=new TextField[books.size()];
for(int i=0;i<books.size();i++)
{
cb[i]=new Checkbox();
bnote[i]=new Button("内容介绍");
bnote[i].setActionCommand("Nate"+i);
bnote[i].addActionListener(this);
tfpq[i]=new TextField("",3);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

public void updateDB()
{
Book b;
String id;
String sql;
try
{
for(int i=0;i<books.size();i++)
{
if(cb[i].getState()&&pq[i]>0)
{
b=(Book)books.elementAt(i);
id=b.getId();
int q=b.getQuantity()-pq[i];
sql="update bstore set quantity="+q+"where id="+id+"";
stmt.executeUpdate(sql);
}

}
}catch(Exception e)
{
e.printStackTrace();
}
}

public void showPage(int page)
{
pm.removeAll();
Panel p;
TextField t;

p=new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
p.add(new Label("选购"));
p.add(new Label("编号\t\t"));
p.add(new Label("\t\t\t\t\t\t书名\t\t\t\t\t\t"));
p.add(new Label("\t\t\t\t\t出版社\t\t\t\t"));
p.add(new Label("单价(元)"));
p.add(new Label("购买数"));
pm.add(p);

for(int i=page*15;i<(page+1)*15;i++)
{
if(i>=books.size()) break;
p=new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT,1,0));
p.add(cb[i]);

t=new TextField(nf1.format(i+1)+"",4);
t.setEditable(false);
p.add(t);

Book bk=(Book)books.elementAt(i);
t=new TextField(bk.getName(),30);
t.setEditable(false);
p.add(t);

t=new TextField(bk.getPublisher(),20);
t.setEditable(false);
p.add(t);

t=new TextField(nf2.format(bk.getPrice())+"",4);
t.setEditable(false);
p.add(t);
if(! cb[i].getState())
{
tfpq[i].setText("");
}
p.add(tfpq[i]);
p.add(bnote[i]);
pm.add(p);
}
fm.validate();
}

public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("Exit"))
{
this.exit();
}else if(s.equals("Review"))
{
this.showSelectedBook();
return;
}else if(s.equals("Submit"))
{
this.purchase();
return;
}else if(s.startsWith("Nate"))
{
s=s.substring(4);
int i=Integer.parseInt(s);
this.showNote(i);
return;
}
if(s.equals("Next"))
{
idx++;
}else if(s.equals("Prev"))
{
idx--;
}else if(s.equals("Clear"))
{
this.reset();
}else if(s.equals("Goto"))
{
try
{
int i=Integer.parseInt(toPage.getText())-1;
if(i>=0&&i<=books.size()/15)
idx=i;
}catch(Exception el){}
toPage.setText("");
}
this.showPage(idx);
this.setButtonState();
}

public void reset()
{
for(int i=0;i<books.size();i++)
{
pq[i]=0;
cb[i].setState(false);
}
}

public void createRevInfo()
{
Book b;
revTa=new TextArea("",20,20);
revTa.setEditable(false);
revTa.setText("编号/书名/单价(元)/数量\n");
revTa.append("-------------------------------------------\n");
double total=0.0;
for(int i=0;i<books.size();i++)
{
try{pq[i]=Integer.parseInt(tfpq[i].getText());}
catch(Exception e){pq[i]=0;}
if(cb[i].getState()&&pq[i]>0)
{
b=(Book)books.elementAt(i);
revTa.append(nf1.format(i+1)+"\t"+b.getName()+
"\t"+nf2.format(b.getPrice())+"\t"+pq[i]+"\n");
total+=pq[i]*b.getPrice();
}
}
revTa.append("-------------------------------------------\n");
revTa.append("应付总金额:"+nf2.format(total)+"元");
}

public void showSelectedBook()
{
if(rew!=null)
{
rew.dispose();
}
Label title=new Label("您已经购买了下列书籍:");
title.setFont(new Font("Impact",Font.PLAIN,20));
this.createRevInfo();
rew=new Dialog(fm);
rew.add(title,"North");
rew.add(revTa);

Button ok=new Button("ok");
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
rew.dispose();
}
});
Panel p=new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));
p.add(ok);
rew.add(p,"South");
rew.setSize(350,200);
rew.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
rew.dispose();
}
});
rew.setVisible(true);
}

public void purchase()
{
if(rew!=null)
{
rew.dispose();
}
Label title=new Label("您的购书清单:");
title.setFont(new Font("Impact",Font.PLAIN,20));
this.createRevInfo();
rew=new Dialog(fm,true);
rew.add(title,"North");
rew.add(revTa);

Button ok=new Button("确定");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
rew.dispose();
updateDB();
reset();
showPage(idx);
}
});

Button cancel=new Button("取消");
cancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
rew.dispose();
}
});
Panel p=new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));
p.add(ok);
p.add(cancel);
rew.add(p,"South");
rew.setSize(350,200);
rew.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
rew.dispose();
}
});
rew.setVisible(true);
}


public void showNote(int bIndex)
{
if(dnote!=null)
{
dnote.dispose();
}
dnote=new Dialog(fm);

Book b =(Book)books.elementAt(bIndex);
Label title=new Label("图书内容介绍");
title.setFont(new Font("Impact",Font.PLAIN,20));
dnote.add(title,"North");

TextArea noteTa=new TextArea("",20,20,1);
noteTa.setEditable(false);
noteTa.append("编号:"+nf1.format(bIndex+1)+"\n书名:"+b.getName()+
"\n标准书号:"+b.getId()+"\n作者:"+b.getAuthor()+"\n单价:"+
nf2.format(b.getPrice())+"\n介绍:\n"+b.getNote()+"\n");
dnote.add(noteTa);

Button ok=new Button("ok");
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dnote.dispose();
}
});
Panel p=new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));
p.add(ok);
dnote.add(p,"South");
dnote.setSize(300,200);
dnote.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dnote.dispose();
}
});

dnote.setVisible(true);
}
public void setButtonState()
{
if(idx==0)
{
prev.setEnabled(false);
}else {
prev.setEnabled(true);
}
if(idx==books.size()/15)
{
next.setEnabled(false);
}else{
next.setEnabled(true);
}
}
}

class Book
{
private String id;
private String name;
private double price;
private String author;
private String publisher;
private String note;
private int quantity;

public Book(String id,String name,double price,String author,String publisher,String note,int quantity)
{
this.id=id;
this.name=name;
this.price=price;
this.author=author;
this.publisher=publisher;
this.note=note;
this.quantity=quantity;
}

public void setId(String id)
{
this.id=id;
}
public void setName(String name)
{
this.name=name;
}
public void setPrice(double price)
{
this.price=price;
}
public void setAuthor(String author)
{
this.author=author;
}
public void setPublisher(String publisher)
{
this.publisher=publisher;
}
public void setNote(String note)
{
this.note=note;
}
public void setQuantity(int quantity)
{
this.quantity=quantity;
}
public String getId()
{
return id;
}
public String getName()
{
return name;
}
public double getPrice()
{
return price;
}
public String getAuthor()
{
return author;
}
public String getPublisher()
{
return publisher;
}
public String getNote()
{
return note;
}
public int getQuantity()
{
return quantity;
}

}



但它不能调用数据库HEHE中的表BSTORE进行交易?为什么?

搜索更多相关主题的帖子: import 千里冰封 书店 java private 
2006-04-05 11:30
快速回复:网上书店源程序,千里冰封见收!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027583 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved