那你是否可以将还书操作也添加进去,并在管理员系统里增加一个查看所有已有书籍的选项
时间: 2024-03-09 07:45:56 浏览: 125
图书管理系统借书还书等操作
好的,我来为你添加还书操作和管理员系统中查看所有已有书籍的选项的代码。
首先,我们需要在Book类中添加一个borrower属性来表示借阅者。代码如下:
```java
public class Book {
private String title;
private String author;
private boolean borrowed;
private User borrower;
public Book(String title, String author) {
this.title = title;
this.author = author;
this.borrowed = false;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean isBorrowed() {
return borrowed;
}
public void setBorrowed(boolean borrowed) {
this.borrowed = borrowed;
}
public User getBorrower() {
return borrower;
}
public void setBorrower(User borrower) {
this.borrower = borrower;
}
}
```
接着,我们需要在Library类中添加一个还书的方法。代码如下:
```java
public void returnBook(Book book, User user) {
if (isAdmin(user)) {
book.setBorrowed(false);
book.setBorrower(null);
} else {
System.out.println("只有管理员才能还书");
}
}
```
然后,我们需要在Main类中添加一个管理员系统,这个系统可以让管理员查看所有已有书籍、添加书籍、删除书籍、还书等操作。代码如下:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
User admin = new User("admin", "123456");
User borrower = new User("borrower", "654321");
Library library = new Library(admin, borrower);
library.addBook(new Book("Java编程思想", "Bruce Eckel"), admin);
library.addBook(new Book("Effective Java", "Joshua Bloch"), admin);
library.addBook(new Book("Head First Java", "Kathy Sierra"), admin);
library.addBook(new Book("Head First Design Patterns", "Eric Freeman"), admin);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择您的身份:1.管理员 2.借阅者 3.退出");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.println("请输入管理员用户名:");
String username = scanner.next();
System.out.println("请输入管理员密码:");
String password = scanner.next();
if (library.isAdmin(new User(username, password))) {
while (true) {
System.out.println("请选择您要进行的操作:1.查看所有已有书籍 2.添加书籍 3.删除书籍 4.还书 5.退出");
int adminChoice = scanner.nextInt();
if (adminChoice == 1) {
System.out.println("所有已有书籍:");
for (Book book : library.getBooks()) {
System.out.println(book.getTitle() + " - " + book.getAuthor() + " - " + (book.isBorrowed() ? "已借出" : "未借出"));
}
} else if (adminChoice == 2) {
System.out.println("请输入书籍名称:");
String title = scanner.next();
System.out.println("请输入书籍作者:");
String author = scanner.next();
library.addBook(new Book(title, author), admin);
System.out.println("添加成功");
} else if (adminChoice == 3) {
System.out.println("请输入要删除的书籍名称:");
String title = scanner.next();
boolean success = library.removeBook(new Book(title, ""), admin);
if (success) {
System.out.println("删除成功");
} else {
System.out.println("删除失败,找不到该书籍");
}
} else if (adminChoice == 4) {
System.out.println("请输入要还的书籍名称:");
String title = scanner.next();
Book bookToReturn = library.borrowBook(title, borrower);
if (bookToReturn != null) {
library.returnBook(bookToReturn, admin);
System.out.println("还书成功");
}
} else if (adminChoice == 5) {
break;
} else {
System.out.println("输入有误,请重新输入");
}
}
} else {
System.out.println("管理员用户名或密码错误");
}
} else if (choice == 2) {
System.out.println("请输入借阅者用户名:");
String username = scanner.next();
System.out.println("请输入借阅者密码:");
String password = scanner.next();
if (library.isBorrower(new User(username, password))) {
System.out.println("可借阅的书籍:");
for (Book book : library.getAvailableBooks()) {
System.out.println(book.getTitle() + " - " + book.getAuthor());
}
System.out.println("请输入要借阅的书籍名称:");
String title = scanner.next();
Book borrowedBook = library.borrowBook(title, borrower);
if (borrowedBook != null) {
borrowedBook.setBorrower(borrower);
System.out.println("借阅成功:" + borrowedBook.getTitle());
}
} else {
System.out.println("借阅者用户名或密码错误");
}
} else if (choice == 3) {
break;
} else {
System.out.println("输入有误,请重新输入");
}
}
}
}
```
这样,一个功能更加完善的图书馆借阅系统就完成了。当然,这个系统还有很多需要优化和改进的地方,比如增加借阅期限、优化输入验证等等。
阅读全文