📄 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 + -