用eclipse做一个图书馆管理系统的源代码
时间: 2024-01-29 08:04:43 浏览: 23
因为图书馆管理系统是一个比较复杂的系统,涉及到多个类和功能,所以这里提供一个简单的示例代码,以供参考:
1. Book类
```java
package model;
public class Book {
private int id;
private String name;
private String author;
private String publisher;
private String isbn;
private boolean isBorrowed;
public Book(int id, String name, String author, String publisher, String isbn, boolean isBorrowed) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.isbn = isbn;
this.isBorrowed = isBorrowed;
}
// getters and setters
}
```
2. User类
```java
package model;
public class User {
private int id;
private String name;
private String studentId;
private String phoneNumber;
private String password;
public User(int id, String name, String studentId, String phoneNumber, String password) {
this.id = id;
this.name = name;
this.studentId = studentId;
this.phoneNumber = phoneNumber;
this.password = password;
}
// getters and setters
}
```
3. BorrowRecord类
```java
package model;
import java.time.LocalDateTime;
public class BorrowRecord {
private int id;
private int bookId;
private int userId;
private LocalDateTime borrowTime;
private LocalDateTime returnTime;
public BorrowRecord(int id, int bookId, int userId, LocalDateTime borrowTime, LocalDateTime returnTime) {
this.id = id;
this.bookId = bookId;
this.userId = userId;
this.borrowTime = borrowTime;
this.returnTime = returnTime;
}
// getters and setters
}
```
4. BookDao类
```java
package dao;
import java.util.ArrayList;
import java.util.List;
import model.Book;
public class BookDao {
private List<Book> books = new ArrayList<>();
public void addBook(Book book) {
books.add(book);
}
public void removeBook(int id) {
for (Book book : books) {
if (book.getId() == id) {
books.remove(book);
return;
}
}
}
public void updateBook(Book book) {
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getId() == book.getId()) {
books.set(i, book);
return;
}
}
}
public Book getBookById(int id) {
for (Book book : books) {
if (book.getId() == id) {
return book;
}
}
return null;
}
public List<Book> getAllBooks() {
return books;
}
}
```
5. UserDao类
```java
package dao;
import java.util.ArrayList;
import java.util.List;
import model.User;
public class UserDao {
private List<User> users = new ArrayList<>();
public void addUser(User user) {
users.add(user);
}
public void removeUser(int id) {
for (User user : users) {
if (user.getId() == id) {
users.remove(user);
return;
}
}
}
public void updateUser(User user) {
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getId() == user.getId()) {
users.set(i, user);
return;
}
}
}
public User getUserById(int id) {
for (User user : users) {
if (user.getId() == id) {
return user;
}
}
return null;
}
public List<User> getAllUsers() {
return users;
}
}
```
6. BorrowRecordDao类
```java
package dao;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import model.BorrowRecord;
public class BorrowRecordDao {
private List<BorrowRecord> records = new ArrayList<>();
public void addRecord(BorrowRecord record) {
records.add(record);
}
public List<BorrowRecord> getRecordsByUserId(int userId) {
List<BorrowRecord> userRecords = new ArrayList<>();
for (BorrowRecord record : records) {
if (record.getUserId() == userId) {
userRecords.add(record);
}
}
return userRecords;
}
public List<BorrowRecord> getRecordsByBookId(int bookId) {
List<BorrowRecord> bookRecords = new ArrayList<>();
for (BorrowRecord record : records) {
if (record.getBookId() == bookId) {
bookRecords.add(record);
}
}
return bookRecords;
}
public List<BorrowRecord> getRecordsByDate(LocalDateTime startDate, LocalDateTime endDate) {
List<BorrowRecord> dateRecords = new ArrayList<>();
for (BorrowRecord record : records) {
if (record.getBorrowTime().isAfter(startDate) && record.getBorrowTime().isBefore(endDate)) {
dateRecords.add(record);
}
}
return dateRecords;
}
}
```
7. BookService类
```java
package service;
import java.time.LocalDateTime;
import java.util.List;
import dao.BookDao;
import dao.BorrowRecordDao;
import dao.UserDao;
import model.Book;
import model.BorrowRecord;
public class BookService {
private BookDao bookDao;
private UserDao userDao;
private BorrowRecordDao recordDao;
public BookService(BookDao bookDao, UserDao userDao, BorrowRecordDao recordDao) {
this.bookDao = bookDao;
this.userDao = userDao;
this.recordDao = recordDao;
}
public void borrowBook(int bookId, int userId) {
Book book = bookDao.getBookById(bookId);
if (book.isBorrowed()) {
System.out.println("该书已借出");
return;
}
book.setBorrowed(true);
bookDao.updateBook(book);
LocalDateTime borrowTime = LocalDateTime.now();
BorrowRecord record = new BorrowRecord(recordDao.getAllRecords().size() + 1, bookId, userId, borrowTime, null);
recordDao.addRecord(record);
}
public void returnBook(int bookId, int userId) {
Book book = bookDao.getBookById(bookId);
if (!book.isBorrowed()) {
System.out.println("该书未被借出");
return;
}
book.setBorrowed(false);
bookDao.updateBook(book);
List<BorrowRecord> records = recordDao.getRecordsByBookId(bookId);
for (BorrowRecord record : records) {
if (record.getUserId() == userId && record.getReturnTime() == null) {
record.setReturnTime(LocalDateTime.now());
recordDao.updateRecord(record);
return;
}
}
System.out.println("没有找到对应的借阅记录");
}
}
```
8. UserService类
```java
package service;
import java.util.List;
import dao.UserDao;
import model.User;
public class UserService {
private UserDao userDao;
public UserService(UserDao userDao) {
this.userDao = userDao;
}
public boolean register(String name, String studentId, String phoneNumber, String password) {
List<User> users = userDao.getAllUsers();
for (User user : users) {
if (user.getStudentId().equals(studentId)) {
System.out.println("该学号已被注册");
return false;
}
}
User user = new User(users.size() + 1, name, studentId, phoneNumber, password);
userDao.addUser(user);
return true;
}
public boolean login(String studentId, String password) {
List<User> users = userDao.getAllUsers();
for (User user : users) {
if (user.getStudentId().equals(studentId) && user.getPassword().equals(password)) {
return true;
}
}
return false;
}
}
```
9. BorrowRecordService类
```java
package service;
import java.time.LocalDateTime;
import java.util.List;
import dao.BorrowRecordDao;
import model.BorrowRecord;
public class BorrowRecordService {
private BorrowRecordDao recordDao;
public BorrowRecordService(BorrowRecordDao recordDao) {
this.recordDao = recordDao;
}
public List<BorrowRecord> getRecordsByUserId(int userId) {
return recordDao.getRecordsByUserId(userId);
}
public List<BorrowRecord> getRecordsByBookId(int bookId) {
return recordDao.getRecordsByBookId(bookId);
}
public List<BorrowRecord> getRecordsByDate(LocalDateTime startDate, LocalDateTime endDate) {
return recordDao.getRecordsByDate(startDate, endDate);
}
public int countRecordsByUserId(int userId) {
List<BorrowRecord> records = recordDao.getRecordsByUserId(userId);
return records.size();
}
public int countRecordsByBookId(int bookId) {
List<BorrowRecord> records = recordDao.getRecordsByBookId(bookId);
return records.size();
}
public int countRecordsByDate(LocalDateTime startDate, LocalDateTime endDate) {
List<BorrowRecord> records = recordDao.getRecordsByDate(startDate, endDate);
return records.size();
}
}
```
10. LoginView类
```java
package view;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import service.UserService;
public class LoginView extends JFrame {
private UserService userService;
public LoginView(UserService userService) {
this.userService = userService;
setTitle("Login");
setSize(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
add(panel);
JLabel idLabel = new JLabel("Student ID:");
panel.add(idLabel);
JTextField idField = new JTextField(15);
panel.add(idField);
JLabel passwordLabel = new JLabel("Password:");
panel.add(passwordLabel);
JTextField passwordField = new JTextField(15);
panel.add(passwordField);
JButton loginButton = new JButton("Login");
loginButton.addActionListener(e -> {
String id = idField.getText();
String password = passwordField.getText();
if (userService.login(id, password)) {
JOptionPane.showMessageDialog(null, "Login successfully");
// TODO: 跳转到书列表界面
} else {
JOptionPane.showMessageDialog(null, "Invalid student ID or password");
}
});
panel.add(loginButton);
}
}
```
11. RegisterView类
```java
package view;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import service.UserService;
public class RegisterView extends JFrame {
private UserService userService;
public RegisterView(UserService userService) {
this.userService = userService;
setTitle("Register");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
add(panel);
JLabel nameLabel = new JLabel("Name:");
panel.add(nameLabel);
JTextField nameField = new JTextField(15);
panel.add(nameField);
JLabel idLabel = new JLabel("Student ID:");
panel.add(idLabel);
JTextField idField = new JTextField(15);
panel.add(idField);
JLabel phoneLabel = new JLabel("Phone Number:");
panel.add(phoneLabel);
JTextField phoneField = new JTextField(15);
panel.add(phoneField);
JLabel passwordLabel = new JLabel("Password:");
panel.add(passwordLabel);
JTextField passwordField = new JTextField(15);
panel.add(passwordField);
JButton registerButton = new JButton("Register");
registerButton.addActionListener(e -> {
String name = nameField.getText();
String id = idField.getText();
String phone = phoneField.getText();
String password = passwordField.getText();
if (userService.register(name, id, phone, password)) {
JOptionPane.showMessageDialog(null, "Register successfully");
// TODO: 跳转到登录界面
} else {
JOptionPane.showMessageDialog(null, "The student ID has been registered");
}
});
panel.add(registerButton);
}
}
```
12. BookListView类
```java
package view;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.Default