?? bookmanagement.java
字號:
//書籍管理系統,made by liwei...
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Dialog;
public class BookManagement implements ActionListener
{
Frame f;
CardLayout myCard;
Panel p1,p2,p3,p4;
TextField name,num,press,price,store,dnum,sname,snum,zname,zoff,zshow;
Button b1,b2,db1,db2,sb1,sb2,sb3,sb4,zb1,zb2;
Label xinxi,dxinxi;
TextArea sxinxi;
static HashMap bookname= new HashMap();
static HashMap bookpublisher= new HashMap();
static HashMap bookprice= new HashMap();
static HashMap bookstock= new HashMap();
public static void main(String args[])
{
BookManagement app=new BookManagement();
app.init();
}
public void init()
{
f=new Frame("書籍信息管理");
myCard=new CardLayout();
f.setLayout(myCard);
p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();
p1.setBackground(Color.yellow);
p2.setBackground(Color.green);
p3.setBackground(Color.blue);
p4.setBackground(Color.pink);
f.add(p1,"First");
f.add(p2,"Second");
f.add(p3,"Third");
f.add(p4,"Fourth");
myCard.show(f,"First");
MenuBar menu=new MenuBar();
Menu m1=new Menu("操作選項");
MenuItem a1=new MenuItem("輸入");
MenuItem a2=new MenuItem("刪除");
MenuItem a3=new MenuItem("查詢");
MenuItem a4=new MenuItem("折扣");
menu.add(m1);
m1.add(a1);
m1.addSeparator();
m1.add(a2);
m1.addSeparator();
m1.add(a3);
m1.addSeparator();
m1.add(a4);
f.setMenuBar(menu);
a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
a4.addActionListener(this);
//panel頁面設計
Label name1=new Label("請輸入書名");
name=new TextField(10);
Label num1=new Label("請輸入書號");
num=new TextField(10);
Label press1=new Label("請輸入出版社名稱");
press=new TextField(10);
Label price1=new Label("請輸入價格");
price=new TextField(10);
Label store1=new Label("請輸入存儲量");
store=new TextField(10);
b1=new Button("錄入");
b2=new Button("重置");
xinxi=new Label( );
Label blank1=new Label( );
Label blank2=new Label( );
Label blank3=new Label( );
Label blank4=new Label( );
Label blank5=new Label( );
Box box1=Box.createHorizontalBox();
box1.add(name1);
box1.add(name);
Box box2=Box.createHorizontalBox();
box2.add(num1);
box2.add(num);
Box box3=Box.createHorizontalBox();
box3.add(press1);
box3.add(press);
Box box4=Box.createHorizontalBox();
box4.add(price1);
box4.add(price);
Box box5=Box.createHorizontalBox();
box5.add(store1);
box5.add(store);
Box box6=Box.createHorizontalBox();
box6.add(b1);
box6.add(b2);
Box box7=Box.createHorizontalBox();
box7.add(xinxi);
Box box8=Box.createHorizontalBox();
box8.add(blank1);
Box box9=Box.createHorizontalBox();
box9.add(blank2);
Box box10=Box.createHorizontalBox();
box10.add(blank3);
Box box11=Box.createHorizontalBox();
box11.add(blank4);
Box box12=Box.createHorizontalBox();
box12.add(blank5);
Box boxH=Box.createVerticalBox();
boxH.add(box1);
boxH.add(box8);
boxH.add(box2);
boxH.add(box9);
boxH.add(box3);
boxH.add(box10);
boxH.add(box4);
boxH.add(box11);
boxH.add(box5);
boxH.add(box12);
boxH.add(box6);
boxH.add(box7);
b1.addActionListener(this);
b2.addActionListener(this);
p1.add(boxH);
//panel2頁面設計
Label name2=new Label("請輸入書號");
dnum=new TextField(10);
db1=new Button("刪除");
db2=new Button("重置");
dxinxi=new Label( );
Label dblank1=new Label( );
Box boxd1=Box.createHorizontalBox();
boxd1.add(name2);
boxd1.add(dnum);
Box boxd2=Box.createHorizontalBox();
boxd2.add(dblank1);
Box boxd3=Box.createHorizontalBox();
boxd2.add(db1);
boxd2.add(db2);
Box boxd4=Box.createHorizontalBox();
boxd4.add(dxinxi);
Box boxdH=Box.createVerticalBox();
boxdH.add(boxd1);
boxdH.add(boxd2);
boxdH.add(boxd3);
boxdH.add(boxd4);
db1.addActionListener(this);
db2.addActionListener(this);
p2.add(boxdH);
//panel3頁面設計
Label title1=new Label("查詢信息系統");
Label title2=new Label("請輸入要查詢的書籍書名");
sname=new TextField(10);
Label title3=new Label("請輸入要查詢的書籍書號");
snum=new TextField(10);
Label title4=new Label("所查詢的書籍相關信息");
sxinxi=new TextArea(10,1);
Label blanks1=new Label( );
sb1=new Button("查詢");
sb2=new Button("重置");
Box boxs1=Box.createHorizontalBox();
boxs1.add(title1);
Box boxs2=Box.createHorizontalBox();
boxs2.add(title2);
boxs2.add(sname);
Box boxs3=Box.createHorizontalBox();
boxs3.add(title3);
boxs3.add(snum);
Box boxs4=Box.createHorizontalBox();
boxs4.add(title4);
Box boxs5=Box.createHorizontalBox();
boxs5.add(sxinxi);
Box boxs6=Box.createHorizontalBox();
boxs6.add(sb1);
boxs6.add(sb2);
Box boxs8=Box.createHorizontalBox();
boxs8.add(blanks1);
Box boxsH=Box.createVerticalBox();
boxsH.add(boxs1);
boxsH.add(boxs2);
boxsH.add(boxs3);
boxsH.add(boxs4);
boxsH.add(boxs5);
boxsH.add(boxs8);
boxsH.add(boxs6);
p3.add(boxsH);
sb1.addActionListener(this);
sb2.addActionListener(this);
//panel4頁面設計
Label titlez1=new Label("書價計算器");
Label titlez3=new Label("請輸入要計算價錢的書籍名");
zname=new TextField(10);
Label titlez2=new Label("請輸入折扣,例如70,80,90");
zoff=new TextField(20);
zb1=new Button("計算");
zb2=new Button("重置");
zshow=new TextField(10);
Box boxz1=Box.createHorizontalBox();
boxz1.add(titlez1);
Box boxz2=Box.createHorizontalBox();
boxz2.add(titlez2);
boxz2.add(zoff);
Box boxz3=Box.createHorizontalBox();
boxz3.add(zb1);
boxz3.add(zb2);
Box boxz4=Box.createHorizontalBox();
boxz4.add(zshow);
Box boxz5=Box.createHorizontalBox();
boxz5.add(titlez3);
boxz5.add(zname);
Box boxzH=Box.createVerticalBox();
boxzH.add(boxz1);
boxzH.add(boxz5);
boxzH.add(boxz2);
boxzH.add(boxz3);
boxzH.add(boxz4);
zb1.addActionListener(this);
zb2.addActionListener(this);
p4.add(boxzH);
//frame相關參數設置
f.setSize(500,390);
f.show();
//用適配器方法實現窗口關閉
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
//事件處理
public void actionPerformed(ActionEvent e)
{
//實現菜單事件
if(e.getActionCommand()=="輸入")
myCard.show(f,"First");
if(e.getActionCommand()=="刪除")
myCard.show(f,"Second");
if(e.getActionCommand()=="查詢")
myCard.show(f,"Third");
if(e.getActionCommand()=="折扣")
myCard.show(f,"Fourth");
//panel1 面板按鈕事件處理
if(e.getSource()==b1)
{ xinxi.setText("錄入成功");
go(); }
if(e.getSource()==b2)
{ name.setText("");
num.setText("");
press.setText("");
price.setText("");
store.setText(""); }
//panel2 面板按鈕事件處理
if(e.getSource()==db1)
{ dxinxi.setText("刪除成功");
in(); }
if(e.getSource()==db2)
{ dnum.setText(""); }
//panel3 面板按鈕事件處理
if(e.getSource()==sb1)
{ out(); }
if(e.getSource()==sb2)
{ sname.setText("");
snum.setText("");
sxinxi.setText(""); }
//panel4 面板按鈕事件處理
if(e.getSource()==zb1)
{
String s9=zname.getText();
Iterator itname = bookname.keySet().iterator();
while(itname.hasNext())
{
Object key= itname.next();
if(bookname.get(key).equals(s9))
{
int x = Integer.parseInt(bookprice.get(key).toString());
String s10=zoff.getText();
float y=100;
y=Integer.parseInt(s10);
float z=x*y/100;
zshow.setText("原價為: "+x+",折扣后,價錢為: "+z);
}
}
}
if(e.getSource()==zb2)
{ zname.setText("");
zoff.setText("");
zshow.setText(""); }
}
//go方法
public void go()
{
String s1=name.getText();
String s2=num.getText();
String s3=press.getText();
String s4=price.getText();
String s5=store.getText();
bookname.put(s2, s1);
bookpublisher.put(s2, s3);
bookprice.put(s2, s4);
bookstock.put(s2, s5);
}
//in方法
public void in()
{
String s6=dnum.getText();
bookname.remove(s6);
bookpublisher.remove(s6);
bookprice.remove(s6);
bookprice.remove(s6);
}
//out方法
public void out()
{
String s7=sname.getText();
Iterator itname = bookname.keySet().iterator();
while(itname.hasNext())
{
Object key= itname.next();
if(bookname.get(key).equals(s7))
{
int x = Integer.parseInt(bookprice.get(key).toString());
int a = Integer.parseInt(bookstock.get(key).toString());
//int z=x*y/100;
sxinxi.setText("書名:"+bookname.get(key)+'\n'+"---書號:"+key+'\n'+"---出版社:"+bookpublisher.get(key)+'\n'+"---單價:"+x+'\n'+"---庫存:"+a+'\n');
if(a<5)
sxinxi.append("庫存小于5,請趕快進貨");
}
if(!(bookname.get(key).equals(s7)))
sxinxi.setText("沒有你要查找的書籍信息");
}
String s8=snum.getText();
int x = Integer.parseInt(bookprice.get(s8).toString());
int a = Integer.parseInt(bookstock.get(s8).toString());
sxinxi.setText("書名:"+bookname.get(s8)+'\n'+"---書號:"+s8+'\n'+"---出版社:"+bookpublisher.get(s8)+'\n'+"---單價:"+x+'\n'+"---庫存:"+bookstock.get(s8)+'\n');
if(a<5)
sxinxi.append("庫存小于5,請趕快進貨");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -