图书查询系统
图书查询系统要求完成:
(1) 图书信息的添加、删除、查询、修改功能
(2) 使用界面操作
(3) 使用文件完成
下面是写的程序,删除和修改不会写,请高手指导。
急用,在线等。
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import
import
import
import
import
import
import
import
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
class Lab extends JFrame{
private String shuming;
private String shuhao;
private String chubanshe;
JLabel b1=new JLabel("信息管理");
JTextField 书名,书号,出版社;
JButton 添加,查找,删除,修改,重置;
JPanel p1,p2,p3,p4,p5,p6;
public Lab()
{
书名=new JTextField(10);
书号=new JTextField(10);
出版社=new JTextField(10);
添加=new JButton("添加");
查找=new JButton("查找");
删除=new JButton("删除");
修改=new JButton("修改");
重置=new JButton("重置");
p1=new JPanel();
p1.add(b1);
p2=new JPanel();
p2.add(new JLabel("书名:",JLabel.CENTER));
p2.add(书名);
p3=new JPanel();
p3.add(new JLabel("书号:",JLabel.CENTER));
p3.add(书号);
p4=new JPanel();
p4.add(new JLabel("出版社:",JLabel.CENTER));
p4.add(出版社);
p5=new JPanel();
p5.add(添加);
添加.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
PrintWriter t=null;
try {
String shuming="";
shuming=书名.getText();
String shuhao="";
shuhao=书号.getText();
String chubanshe="";
chubanshe=出版社.getText();
t= new PrintWriter(new FileWriter("labria.txt",true));
t.println(shuming+"|"+shuhao+"|"+chubanshe);
} catch (FileNotFoundException e1) {
} catch (IOException e2) {
}
t.close();
}
});
p5.add(查找);
查找.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
BufferedReader bf=null;
String shuhao=书号.getText();
try {
bf=new BufferedReader(new FileReader("labria.txt"));
String read;
int bl = 0;
while((read=bf.readLine())!=null){
StringTokenizer st=new StringTokenizer(read,"|");
String shumi=st.nextToken();
String shuh=st.nextToken();
String chuban=st.nextToken();
if(shuh.equals(shuhao)){
JOptionPane.showMessageDialog(null, "书名:" + shumi+ "\n书号:" + shuh+"\n出版社:"+chuban);
System.out.println(read);
bl=0;
}
else{
bl=1;
}
}
if(bl==1){
JOptionPane.showMessageDialog(null, "找不到信息");
}
bf.close();
} catch (FileNotFoundException e1) {
} catch (IOException e1) {
}
}
});
p5.add(删除);
删除.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
BufferedReader bf=null;
BufferedWriter ra=null;
String shuming=书名.getText();
//String shuhao=书号.getText();
//String chubanshe=出版社.getText();
//String temp=shuming+"|"+shuhao+"|"+chubanshe;
try {
ra=new BufferedWriter(new FileWriter("labria.txt"));
bf=new BufferedReader(new FileReader("labria.txt"));
String read,rea;
int bl = 0;
StringBuffer re=new StringBuffer();
while((read=bf.readLine())!=null){
StringTokenizer st=new StringTokenizer(read,"|");
String shumi=st.nextToken();
//String shuh=st.nextToken();
//String chuban=st.nextToken();
if(shumi.equals(shuming)){
re.append(read+"\n");
rea=re.toString();
bf.close();
rea=rea.replaceAll(read, "");
//JOptionPane.showMessageDialog(null, "书名:" + shumi+ "\n书号:" + shuh+"\n出版社:"+chuban);
//read="";
//bf.reset();
//String rea=bf.readLine();
//rea="bbb";
//ra.write(read);
//bl=0;
}
else{
bl=1;
}
}
if(bl==1){
JOptionPane.showMessageDialog(null, "找不到信息");
}
bf.close();
} catch (FileNotFoundException e1) {
} catch (IOException e1) {
}
}
});
p5.add(修改);
修改.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
书名.setText("");
书号.setText("");
出版社.setText("");
}
});
p5.add(重置);
重置.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
书名.setText("");
书号.setText("");
出版社.setText("");
}
});
p6=new JPanel();
p6.setLayout(new GridLayout(5,1));
p6.add(p1);
p6.add(p2);
p6.add(p3);
p6.add(p4);
p6.add(p5);
JFrame jf=new JFrame("图书管理系统");
jf.setBounds(200,200,400,500);
jf.setVisible(true);
jf.add(p6);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public String getChubanshe() {
return chubanshe;
}
public void setChubanshe(String chubanshe) {
this.chubanshe = chubanshe;
}
public String getShuhao() {
return shuhao;
}
public void setShuhao(String shuhao) {
this.shuhao = shuhao;
}
public String getShuming() {
return shuming;
}
public void setShuming(String shuming) {
this.shuming = shuming;
}
}
public class Labri{
public static void main(String[] args){
Lab l=new Lab();
}
}