使用JAVA包含类的定义、GUI界面设计、文件IO、集合; 图书查询 设计一个图书查询应用,book.txt文件中至少有5条图书记录,每条图书记录都包括图书的ISBN号,作 者,出版社,定价,页数等。用户输入图书的ISBN号,则在界面中显示该图书的详细信息。生成程序
时间: 2024-04-28 07:24:13 浏览: 95
运用JAVA图书查询系统
4星 · 用户满意度95%
好的,我可以帮助你设计一个图书查询应用。首先,我们需要创建一个Book类来表示每一本书的信息。以下是示例代码:
```java
public class Book {
private String isbn;
private String author;
private String publisher;
private double price;
private int pages;
public Book(String isbn, String author, String publisher, double price, int pages) {
this.isbn = isbn;
this.author = author;
this.publisher = publisher;
this.price = price;
this.pages = pages;
}
// Getters and setters
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
}
```
接下来,我们需要创建一个GUI界面来接收用户输入,并显示图书信息。以下是示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class BookQueryGUI extends JFrame implements ActionListener {
private JLabel promptLabel;
private JTextField isbnField;
private JTextArea resultArea;
private Map<String, Book> bookMap;
public BookQueryGUI() {
super("图书查询");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 创建组件
promptLabel = new JLabel("请输入图书的ISBN号:");
isbnField = new JTextField(20);
resultArea = new JTextArea(10, 30);
resultArea.setEditable(false);
JPanel inputPanel = new JPanel();
inputPanel.add(promptLabel);
inputPanel.add(isbnField);
JScrollPane resultScrollPane = new JScrollPane(resultArea);
JButton queryButton = new JButton("查询");
queryButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(queryButton);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(resultScrollPane, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
// 读取图书信息
bookMap = new HashMap<>();
try (Scanner scanner = new Scanner(new File("book.txt"))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] fields = line.split(",");
String isbn = fields[0];
String author = fields[1];
String publisher = fields[2];
double price = Double.parseDouble(fields[3]);
int pages = Integer.parseInt(fields[4]);
Book book = new Book(isbn, author, publisher, price, pages);
bookMap.put(isbn, book);
}
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, "找不到图书信息文件!", "错误", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
System.exit(1);
}
}
@Override
public void actionPerformed(ActionEvent e) {
String isbn = isbnField.getText().trim();
Book book = bookMap.get(isbn);
if (book == null) {
resultArea.setText("找不到该图书!");
} else {
resultArea.setText("ISBN号:" + book.getIsbn() + "\n" +
"作者:" + book.getAuthor() + "\n" +
"出版社:" + book.getPublisher() + "\n" +
"定价:" + book.getPrice() + "\n" +
"页数:" + book.getPages());
}
}
public static void main(String[] args) {
BookQueryGUI gui = new BookQueryGUI();
gui.setVisible(true);
}
}
```
注意,我们使用了一个Map来存储图书信息,这样可以快速根据ISBN号查找到对应的书籍。
最后,我们需要将图书信息存储在book.txt文件中。以下是示例文件内容:
```
978-7-111-59984-1,李刚,机械工业出版社,69.00,480
978-7-115-35317-9,刘汝佳,清华大学出版社,59.00,300
978-7-302-32801-3,郝斌,电子工业出版社,89.00,550
978-7-111-55431-7,韩顺平,人民邮电出版社,49.00,310
978-7-302-37297-2,黄勇,电子工业出版社,59.00,360
```
这个文件中包含了5条图书记录,每条记录都有ISBN号、作者、出版社、定价和页数等信息。
现在你可以编译并运行这个程序,输入图书的ISBN号,就可以查询到该图书的详细信息了。
阅读全文