Java编写图形用户界面的图书借阅系统
时间: 2023-10-16 12:10:45 浏览: 87
Java编写图形用户界面的图书借阅系统需要用到Java Swing和AWT等库。以下是一些实现步骤:
1. 设计数据库结构,包括图书信息、借阅记录等。
2. 设计图书借阅系统的界面。可以使用JFrame、JPanel等组件,包括图书查询、借阅、归还等功能。
3. 实现与数据库的交互。使用JDBC连接数据库,并进行增删改查等操作。
4. 实现借阅和归还功能。需要对图书状态进行修改,更新借阅记录等。
5. 实现图书查询功能。可以按照书名、作者、出版社等进行查询,同时需要显示图书的剩余数量等信息。
6. 实现用户登录和注册功能,保证系统安全性。
7. 进行异常处理,确保程序的稳定性。
以上是Java编写图形用户界面的图书借阅系统的一些基本步骤。需要注意的是,在实现过程中需要根据实际需求进行适当的修改和优化。
相关问题
Java编写图形用户界面图书借阅系统
好的,下面是一些基本的实现思路:
1. 数据存储:使用文本文件存储图书信息,每行表示一本书,数据格式为"书名,作者,出版社,数量"。在程序启动时,读取文件中的数据并加载到内存中,可以使用ArrayList等容器类方便后续操作。
2. 图书信息显示:使用Java Swing中的JTable控件展示所有图书信息,可以通过JScrollPane控件实现滚动条。可以在表头上添加按钮,实现按书名、作者、出版社、数量排序,使用JComboBox控件实现下拉菜单选择排序方式。在表格中添加一列复选框或者单选框,表示选择要借阅的图书。
3. 图书信息查找:使用文本框和搜索按钮实现按书名、作者、出版社查找相关图书。可以使用Java的正则表达式实现模糊匹配,在表格中显示匹配的结果。
4. 图书借阅:添加按钮实现借阅操作。需要检查借阅数量是否超过限制(最多借阅3本,同一本书最多借阅2本),并且需要判断图书的数量是否足够。借阅成功后,更新内存中的图书数量和借阅记录,将借阅记录写入文本文件。
5. 借阅记录:使用文本文件存储借阅记录,每行表示一条记录,数据格式为"借阅人,书名,借阅时间,归还时间"。在程序启动时,读取文件中的数据并加载到内存中。可以使用JTable控件展示借阅记录,实现按借阅人、书名、借阅时间、归还时间排序等功能。
以上是Java编写图书借阅系统的一些基本思路,具体实现需要根据实际需求进行设计和编写。
Java编写图形用户界面图书借阅系统代码
以下是一个简单的Java图书借阅系统代码实现,仅供参考:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.table.*;
public class BookBorrowSystem extends JFrame {
private static final long serialVersionUID = 1L;
private static final String BOOK_FILE = "books.txt";
private static final String BORROW_RECORD_FILE = "borrow_records.txt";
private static final int MAX_BORROW_COUNT = 3;
private static final int MAX_BORROW_SAME_BOOK_COUNT = 2;
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private ArrayList<Book> books = new ArrayList<Book>();
private ArrayList<BorrowRecord> borrowRecords = new ArrayList<BorrowRecord>();
private JTable bookTable;
private JTable borrowRecordTable;
public static void main(String[] args) {
new BookBorrowSystem();
}
public BookBorrowSystem() {
loadBooks();
loadBorrowRecords();
setTitle("图书借阅系统");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
add(tabbedPane, BorderLayout.CENTER);
JPanel bookPanel = new JPanel(new BorderLayout());
tabbedPane.add("图书信息", bookPanel);
JPanel bookButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
bookPanel.add(bookButtonPanel, BorderLayout.NORTH);
JButton refreshBookButton = new JButton("刷新");
refreshBookButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
refreshBookTable();
}
});
bookButtonPanel.add(refreshBookButton);
JComboBox<String> sortComboBox = new JComboBox<String>(new String[] { "按书名排序", "按作者排序", "按出版社排序", "按数量排序" });
sortComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sortBooks(sortComboBox.getSelectedIndex());
refreshBookTable();
}
});
bookButtonPanel.add(sortComboBox);
JTextField searchTextField = new JTextField(20);
bookButtonPanel.add(searchTextField);
JButton searchButton = new JButton("搜索");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchBooks(searchTextField.getText());
}
});
bookButtonPanel.add(searchButton);
JButton borrowButton = new JButton("借阅");
borrowButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[] selectedRows = bookTable.getSelectedRows();
if (selectedRows.length == 0) {
JOptionPane.showMessageDialog(BookBorrowSystem.this, "请选择要借阅的图书");
return;
}
int borrowCount = getBorrowCount();
if (borrowCount >= MAX_BORROW_COUNT) {
JOptionPane.showMessageDialog(BookBorrowSystem.this, "您已经借阅了" + borrowCount + "本图书,不能再借阅了");
return;
}
for (int i = 0; i < selectedRows.length; i++) {
int rowIndex = selectedRows[i];
Book book = books.get(rowIndex);
int borrowSameBookCount = getBorrowSameBookCount(book);
if (borrowSameBookCount >= MAX_BORROW_SAME_BOOK_COUNT) {
JOptionPane.showMessageDialog(BookBorrowSystem.this, "您已经借阅了" + borrowSameBookCount + "本《" + book.getName() + "》,不能再借阅了");
return;
}
if (book.getCount() == 0) {
JOptionPane.showMessageDialog(BookBorrowSystem.this, "《" + book.getName() + "》已经借光了");
return;
}
book.setCount(book.getCount() - 1);
borrowRecords.add(new BorrowRecord(System.getProperty("user.name"), book.getName(), new Date()));
}
saveBooks();
saveBorrowRecords();
refreshBookTable();
refreshBorrowRecordTable();
}
private int getBorrowCount() {
int count = 0;
for (BorrowRecord record : borrowRecords) {
if (record.getBorrower().equals(System.getProperty("user.name")) && record.getReturnDate() == null) {
count++;
}
}
return count;
}
private int getBorrowSameBookCount(Book book) {
int count = 0;
for (BorrowRecord record : borrowRecords) {
if (record.getBorrower().equals(System.getProperty("user.name")) && record.getReturnDate() == null && record.getBookName().equals(book.getName())) {
count++;
}
}
return count;
}
});
bookButtonPanel.add(borrowButton);
bookTable = new JTable(new DefaultTableModel(new String[] { "书名", "作者", "出版社", "数量", "选择" }, 0) {
private static final long serialVersionUID = 1L;
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 4) {
return Boolean.class;
}
return super.getColumnClass(columnIndex);
}
public boolean isCellEditable(int row, int column) {
return column == 4;
}
});
bookTable.getTableHeader().setReorderingAllowed(false);
bookTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
bookTable.getColumnModel().getColumn(3).setCellRenderer(new DefaultTableCellRenderer() {
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (Integer.parseInt(value.toString()) == 0) {
component.setForeground(Color.RED);
} else {
component.setForeground(Color.BLACK);
}
return component;
}
});
bookPanel.add(new JScrollPane(bookTable), BorderLayout.CENTER);
JPanel borrowRecordPanel = new JPanel(new BorderLayout());
tabbedPane.add("借阅记录", borrowRecordPanel);
JPanel borrowRecordButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
borrowRecordPanel.add(borrowRecordButtonPanel, BorderLayout.NORTH);
JButton refreshBorrowRecordButton = new JButton("刷新");
refreshBorrowRecordButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
refreshBorrowRecordTable();
}
});
borrowRecordButtonPanel.add(refreshBorrowRecordButton);
JComboBox<String> borrowRecordSortComboBox = new JComboBox<String>(new String[] { "按借阅人排序", "按书名排序", "按借阅时间排序", "按归还时间排序" });
borrowRecordSortComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sortBorrowRecords(borrowRecordSortComboBox.getSelectedIndex());
refreshBorrowRecordTable();
}
});
borrowRecordButtonPanel.add(borrowRecordSortComboBox);
borrowRecordTable = new JTable(new DefaultTableModel(new String[] { "借阅人", "书名", "借阅时间", "归还时间" }, 0));
borrowRecordTable.getTableHeader().setReorderingAllowed(false);
borrowRecordPanel.add(new JScrollPane(borrowRecordTable), BorderLayout.CENTER);
setVisible(true);
}
private void loadBooks() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(BOOK_FILE), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
Book book = new Book();
book.setName(fields[0]);
book.setAuthor(fields[1]);
book.setPublisher(fields[2]);
book.setCount(Integer.parseInt(fields[3]));
books.add(book);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveBooks() {
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(BOOK_FILE), "UTF-8"));
for (Book book : books) {
writer.write(book.getName() + "," + book.getAuthor() + "," + book.getPublisher() + "," + book.getCount());
writer.newLine();
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void loadBorrowRecords() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(BORROW_RECORD_FILE), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
BorrowRecord record = new BorrowRecord();
record.setBorrower(fields[0]);
record.setBookName(fields[1]);
record.setBorrowDate(DATE_FORMAT.parse(fields[2]));
if (fields.length > 3 && fields[3].length() > 0) {
record.setReturnDate(DATE_FORMAT.parse(fields[3]));
}
borrowRecords.add(record);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveBorrowRecords() {
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(BORROW_RECORD_FILE), "UTF-8"));
for (BorrowRecord record : borrowRecords) {
writer.write(record.getBorrower() + "," + record.getBookName() + "," + DATE_FORMAT.format(record.getBorrowDate()));
if (record.getReturnDate() != null) {
writer.write("," + DATE_FORMAT.format(record.getReturnDate()));
}
writer.newLine();
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void refreshBookTable() {
DefaultTableModel model = (DefaultTableModel) bookTable.getModel();
model.setRowCount(0);
for (Book book : books) {
model.addRow(new Object[] { book.getName(), book.getAuthor(), book.getPublisher(), book.getCount() });
}
}
private void sortBooks(int sortType) {
switch (sortType) {
case 0:
books.sort((a, b) -> a.getName().compareTo(b.getName()));
break;
case 1:
books.sort((a, b) -> a.getAuthor().compareTo(b.getAuthor()));
break;
case 2:
books.sort((a, b) -> a.getPublisher().compareTo(b.getPublisher()));
break;
case 3:
books.sort((a, b) -> a.getCount() - b.getCount());
break;
}
}
private void searchBooks(String keyword) {
DefaultTableModel model = (DefaultTableModel) bookTable.getModel();
model.setRowCount(0);
Pattern pattern = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE);
for (Book book : books) {
if (pattern.matcher(book.getName()).find() || pattern.matcher(book.getAuthor()).find() || pattern.matcher(book.getPublisher()).find()) {
model.addRow(new Object[] { book.getName(), book.getAuthor(), book.getPublisher(), book.getCount() });
}
}
}
private void refreshBorrowRecordTable() {
DefaultTableModel model = (DefaultTableModel) borrowRecordTable.getModel();
model.setRowCount(0);
for (BorrowRecord record : borrowRecords) {
model.addRow(new Object[] { record.getBorrower(), record.getBookName(), DATE_FORMAT.format(record.getBorrowDate()), record.getReturnDate() == null ? "未归还" : DATE_FORMAT.format(record.getReturnDate()) });
}
}
private void sortBorrowRecords(int sortType) {
switch (sortType) {
case 0:
borrowRecords.sort((a, b) -> a.getBorrower().compareTo(b.getBorrower()));
break;
case 1:
borrowRecords.sort((a, b) -> a.getBookName().compareTo(b.getBookName()));
break;
case 2:
borrowRecords.sort((a, b) -> a.getBorrowDate().compareTo(b.getBorrowDate()));
break;
case 3:
borrowRecords.sort((a, b) -> {
if (a.getReturnDate() == null && b.getReturnDate() == null) {
return 0;
} else if (a.getReturnDate() == null) {
return 1;
} else if (b.getReturnDate() == null) {
return -1;
} else {
return a.getReturnDate().compareTo(b.getReturnDate());
}
});
break;
}
}
private static class Book {
private String name;
private String author;
private String publisher;
private int count;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
private static class BorrowRecord {
阅读全文