写一个图书借阅管理系统的代码
时间: 2023-03-16 09:43:41 浏览: 127
这取决于系统的功能和设计,但以下是一个简单的图书借阅管理系统的代码示例:// 定义类:Library
class Library {
// 定义类变量
int totalBooks;
int totalUsers;
// 定义构造函数
Library(){
totalBooks = 0;
totalUsers = 0;
}
// 定义函数:增加图书
void addBook(){
totalBooks++;
}
// 定义函数:增加用户
void addUser(){
totalUsers++;
}
// 定义函数:借阅图书
void lendBook(){
if(totalBooks > 0 && totalUsers > 0){
totalBooks--;
totalUsers--;
}
}
// 定义函数:还书
void returnBook(){
totalBooks++;
}
}
相关问题
写一个图书借阅管理系统
以下是一个简单的图书借阅管理系统的Java代码示例,包括读者和管理员两种账号类型的功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class LibrarySystem {
private ArrayList<Book> books;
private ArrayList<Reader> readers;
private ArrayList<BorrowRecord> records;
private Scanner scanner;
public LibrarySystem() {
books = new ArrayList<>();
readers = new ArrayList<>();
records = new ArrayList<>();
scanner = new Scanner(System.in);
}
public void run() {
while (true) {
System.out.println("欢迎使用图书借阅管理系统!");
System.out.println("请选择登录类型:1.读者 2.管理员");
int type = scanner.nextInt();
if (type == 1) {
Reader reader = loginAsReader();
if (reader != null) {
readerMenu(reader);
}
} else if (type == 2) {
Admin admin = loginAsAdmin();
if (admin != null) {
adminMenu(admin);
}
} else {
System.out.println("输入错误,请重新选择!");
}
}
}
private Reader loginAsReader() {
System.out.println("请输入读者账号:");
String account = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
for (Reader reader : readers) {
if (reader.getAccount().equals(account) && reader.getPassword().equals(password)) {
System.out.println("登录成功!");
return reader;
}
}
System.out.println("账号或密码错误,登录失败!");
return null;
}
private void readerMenu(Reader reader) {
while (true) {
System.out.println("请选择操作:1.查找图书 2.借阅图书 3.还书 4.查看历史借阅记录 5.修改个人资料 6.修改密码 7.退出登录");
int choice = scanner.nextInt();
switch (choice) {
case 1:
searchBooks();
break;
case 2:
borrowBook(reader);
break;
case 3:
returnBook(reader);
break;
case 4:
viewBorrowRecords(reader);
break;
case 5:
modifyReaderInfo(reader);
break;
case 6:
modifyReaderPassword(reader);
break;
case 7:
System.out.println("退出登录成功!");
return;
default:
System.out.println("输入错误,请重新选择!");
}
}
}
private Admin loginAsAdmin() {
System.out.println("请输入管理员账号:");
String account = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
for (Admin admin : admins) {
if (admin.getAccount().equals(account) && admin.getPassword().equals(password)) {
System.out.println("登录成功!");
return admin;
}
}
System.out.println("账号或密码错误,登录失败!");
return null;
}
private void adminMenu(Admin admin) {
while (true) {
System.out.println("请选择操作:1.添加图书 2.修改图书 3.删除图书 4.添加读者 5.修改读者 6.删除读者 7.查看借阅记录 8.退出登录");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addBook();
break;
case 2:
modifyBook();
break;
case 3:
deleteBook();
break;
case 4:
addReader();
break;
case 5:
modifyReader();
break;
case 6:
deleteReader();
break;
case 7:
viewBorrowRecords();
break;
case 8:
System.out.println("退出登录成功!");
return;
default:
System.out.println("输入错误,请重新选择!");
}
}
}
private void searchBooks() {
System.out.println("请输入关键字:");
String keyword = scanner.next();
ArrayList<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
result.add(book);
}
}
if (result.isEmpty()) {
System.out.println("未找到相关图书!");
} else {
System.out.println("共找到" + result.size() + "本图书:");
for (Book book : result) {
System.out.println(book);
}
}
}
private void borrowBook(Reader reader) {
System.out.println("请输入要借阅的图书编号:");
int bookId = scanner.nextInt();
Book book = findBookById(bookId);
if (book == null) {
System.out.println("未找到该图书!");
} else if (book.isBorrowed()) {
System.out.println("该图书已被借出!");
} else {
book.setBorrowed(true);
BorrowRecord record = new BorrowRecord(reader, book);
records.add(record);
System.out.println("借阅成功!");
}
}
private void returnBook(Reader reader) {
System.out.println("请输入要归还的图书编号:");
int bookId = scanner.nextInt();
Book book = findBookById(bookId);
if (book == null) {
System.out.println("未找到该图书!");
} else if (!book.isBorrowed()) {
System.out.println("该图书未被借出!");
} else {
book.setBorrowed(false);
for (BorrowRecord record : records) {
if (record.getReader() == reader && record.getBook() == book) {
record.setReturned(true);
System.out.println("归还成功!");
return;
}
}
System.out.println("归还失败!");
}
}
private void viewBorrowRecords(Reader reader) {
ArrayList<BorrowRecord> result = new ArrayList<>();
for (BorrowRecord record : records) {
if (record.getReader() == reader) {
result.add(record);
}
}
if (result.isEmpty()) {
System.out.println("未找到历史借阅记录!");
} else {
System.out.println("共找到" + result.size() + "条历史借阅记录:");
for (BorrowRecord record : result) {
System.out.println(record);
}
}
}
private void modifyReaderInfo(Reader reader) {
System.out.println("请输入新的姓名:");
String name = scanner.next();
System.out.println("请输入新的性别:");
String gender = scanner.next();
System.out.println("请输入新的年龄:");
int age = scanner.nextInt();
reader.setName(name);
reader.setGender(gender);
reader.setAge(age);
System.out.println("修改成功!");
}
private void modifyReaderPassword(Reader reader) {
System.out.println("请输入旧密码:");
String oldPassword = scanner.next();
System.out.println("请输入新密码:");
String newPassword = scanner.next();
if (reader.getPassword().equals(oldPassword)) {
reader.setPassword(newPassword);
System.out.println("修改成功!");
} else {
System.out.println("密码错误,修改失败!");
}
}
private void addBook() {
System.out.println("请输入图书编号:");
int id = scanner.nextInt();
System.out.println("请输入图书名称:");
String title = scanner.next();
System.out.println("请输入作者名称:");
String author = scanner.next();
Book book = new Book(id, title, author);
books.add(book);
System.out.println("添加成功!");
}
private void modifyBook() {
System.out.println("请输入要修改的图书编号:");
int id = scanner.nextInt();
Book book = findBookById(id);
if (book == null) {
System.out.println("未找到该图书!");
} else {
System.out.println("请输入新的图书名称:");
String title = scanner.next();
System.out.println("请输入新的作者名称:");
String author = scanner.next();
book.setTitle(title);
book.setAuthor(author);
System.out.println("修改成功!");
}
}
private void deleteBook() {
System.out.println("请输入要删除的图书编号:");
int id = scanner.nextInt();
Book book = findBookById(id);
if (book == null) {
System.out.println("未找到该图书!");
} else if (book.isBorrowed()) {
System.out.println("该图书已被借出,无法删除!");
} else {
books.remove(book);
System.out.println("删除成功!");
}
}
private void addReader() {
System.out.println("请输入读者账号:");
String account = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入性别:");
String gender = scanner.next();
System.out.println("请输入年龄:");
int age = scanner.nextInt();
Reader reader = new Reader(account, password, name, gender, age);
readers.add(reader);
System.out.println("添加成功!");
}
private void modifyReader() {
System.out.println("请输入要修改的读者账号:");
String account = scanner.next();
Reader reader = findReaderByAccount(account);
if (reader == null) {
System.out.println("未找到该读者!");
} else {
System.out.println("请输入新的密码:");
String password = scanner.next();
System.out.println("请输入新的姓名:");
String name = scanner.next();
System.out.println("请输入新的性别:");
String gender = scanner.next();
System.out.println("请输入新的年龄:");
int age = scanner.nextInt();
reader.setPassword(password);
reader.setName(name);
reader.setGender(gender);
reader.setAge(age);
System.out.println("修改成功!");
}
}
private void deleteReader() {
System.out.println("请输入要删除的读者账号:");
String account = scanner.next();
Reader reader = findReaderByAccount(account);
if (reader == null) {
System.out.println("未找到该读者!");
} else if (hasBorrowedBooks(reader)) {
System.out.println("该读者还有未归还的图书,无法删除!");
} else {
readers.remove(reader);
System.out.println("删除成功!");
}
}
private void viewBorrowRecords() {
if (records.isEmpty()) {
System.out.println("暂无借阅记录!");
} else {
System.out.println("共有" + records.size() + "条借阅记录:");
for (BorrowRecord record : records) {
System.out.println(record);
}
}
}
private Book findBookById(int id) {
for (Book book : books) {
if (book.getId() == id) {
return book;
}
}
return null;
}
private Reader findReaderByAccount(String account) {
for (Reader reader : readers) {
if (reader.getAccount().equals(account)) {
return reader;
}
}
return null;
}
private boolean hasBorrowedBooks(Reader reader) {
for (BorrowRecord record : records) {
if (record.getReader() == reader && !record.isReturned()) {
return true;
}
}
return false;
}
}
class Book {
private int id;
private String title;
private String author;
private boolean borrowed;
public Book(int id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
borrowed = false;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public boolean isBorrowed() {
return borrowed;
}
public void setBorrowed(boolean borrowed) {
this.borrowed = borrowed;
}
@Override
public String toString() {
return "编号:" + id + ",书名:" + title + ",作者:" + author + ",状态:" + (borrowed ? "已借出" : "未借出");
}
}
class Reader {
private String account;
private String password;
private String name;
private String gender;
private int age;
public Reader(String account, String password, String name, String gender, int age) {
this.account = account;
this.password = password;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getAccount() {
return account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name =
用sql写一个图书借阅管理系统
好的,以下是一个简单的图书借阅管理系统的 SQL:
```
CREATE TABLE books (
id INT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
publish_date DATE NOT NULL,
total INT NOT NULL,
available INT NOT NULL
);
CREATE TABLE borrowers (
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone VARCHAR(20) NOT NULL
);
CREATE TABLE borrowings (
id INT PRIMARY KEY,
book_id INT NOT NULL,
borrower_id INT NOT NULL,
borrow_date DATE NOT NULL,
return_date DATE,
FOREIGN KEY (book_id) REFERENCES books(id),
FOREIGN KEY (borrower_id) REFERENCES borrowers(id)
);
```
这里我们创建了三个表:
- `books` 表存储了所有的图书信息,包括书名、作者、出版日期、总数以及可供借阅的数量。
- `borrowers` 表存储了所有的借阅者信息,包括姓名、邮箱、电话等。
- `borrowings` 表记录了每一次借阅的具体信息,包括所借图书的 ID、借阅者的 ID、借阅日期和归还日期。
接下来,我们可以通过以下 SQL 语句来实现一些基本的操作:
- 向 books 表中添加新的图书记录:
```
INSERT INTO books (id, title, author, publish_date, total, available)
VALUES (1, 'The Great Gatsby', 'F. Scott Fitzgerald', '1925-04-10', 5, 5);
```
- 向 borrowers 表中添加新的借阅者记录:
```
INSERT INTO borrowers (id, name, email, phone)
VALUES (1, 'John Smith', 'john.smith@example.com', '555-1234');
```
- 向 borrowings 表中添加新的借阅记录:
```
INSERT INTO borrowings (id, book_id, borrower_id, borrow_date, return_date)
VALUES (1, 1, 1, '2021-01-01', NULL);
```
- 查询所有在借图书:
```
SELECT books.title, books.author, borrowings.borrow_date, borrowings.return_date
FROM borrowings
INNER JOIN books ON borrowings.book_id = books.id
WHERE borrowings.return_date IS NULL;
```
- 查询某个借阅者借了哪些书:
```
SELECT books.title, books.author, borrowings.borrow_date, borrowings.return_date
FROM borrowings
INNER JOIN books ON borrowings.book_id = books.id
WHERE borrowings.borrower_id = 1;
```
- 更新 borrowings 表中某条借阅记录的归还日期:
```
UPDATE borrowings
SET return_date = '2021-02-01'
WHERE id = 1;
```
以上是一个简单的图书借阅管理系统的 SQL 实现,实际应用中可能会更加复杂,但是这里提供的代码可以作为一个基本的参考。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)