写一个图书借阅管理系统
时间: 2023-12-24 17:03:31 浏览: 73
图书借阅管理系统,采用SpringBoot+MyBatis+MySQL架构.zip
以下是一个简单的图书借阅管理系统的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 =
阅读全文