📄 bookstore.java
字号:
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[]){
UI ui = new UI();
ui.inital();
}
}
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(toPage);
p.add(review);
p.add(submit);
p.add(ext);
nf1 = NumberFormat.getInstance();
nf2 = NumberFormat.getInstance();
nf1.setMinimumIntegerDigits(3);
nf2.setMinimumFractionDigits(2);
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("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@" + dbServer + ":1521:oradb";
conn = DriverManager.getConnection(url,"scott","tiger");
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("Note" + 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("Note")){
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 e1){}
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -