用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

相关推荐

最新推荐

recommend-type

dtp--eclipse的安装数据源管理的一个插件的安装方法

dtp eclipse数据源插件安装 dtp--eclipse的安装数据源管理的一个插件的安装方法
recommend-type

在Eclipse里关联android源代码的三种方式

第一种方式:source attach 第二种方式:复制platform/frameworks/base/core/java到在AndroidSDK的安装目录下的platforms/android-12下新建sources目录 第三种方式:最全面但最复杂的一种,见文件里的方式
recommend-type

基于web的图书馆管理系统的设计与实现毕业设计

系统设计利用了Java语言,SSM框架,MYSQL数据库,Eclipse开发工具实现了一个基于web的图书馆管理系统。
recommend-type

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a
recommend-type

matlab建立计算力学课程的笔记和文件.zip

matlab建立计算力学课程的笔记和文件.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。