帮我写个图书管理系统并生成代码
时间: 2023-07-21 15:13:24 浏览: 42
好的,以下是一个基于Java的图书管理系统的代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BookManagementSystem {
private static ArrayList<Book> bookList = new ArrayList<Book>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("1. Add book");
System.out.println("2. Delete book");
System.out.println("3. Modify book information");
System.out.println("4. Query book information");
System.out.println("5. Borrow book");
System.out.println("6. Return book");
System.out.println("7. Exit");
System.out.print("Please enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
modifyBook();
break;
case 4:
queryBook();
break;
case 5:
borrowBook();
break;
case 6:
returnBook();
break;
case 7:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice!");
break;
}
} while (choice != 7);
}
private static void addBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter book name: ");
String name = scanner.nextLine();
System.out.print("Please enter author name: ");
String author = scanner.nextLine();
System.out.print("Please enter publisher name: ");
String publisher = scanner.nextLine();
System.out.print("Please enter ISBN number: ");
String isbn = scanner.nextLine();
Book book = new Book(name, author, publisher, isbn);
bookList.add(book);
System.out.println("Book added successfully!");
}
private static void deleteBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter book name or ISBN number: ");
String input = scanner.nextLine();
boolean found = false;
for (int i = 0; i < bookList.size(); i++) {
Book book = bookList.get(i);
if (book.getName().equals(input) || book.getIsbn().equals(input)) {
bookList.remove(i);
found = true;
break;
}
}
if (found) {
System.out.println("Book deleted successfully!");
} else {
System.out.println("Book not found!");
}
}
private static void modifyBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter book name or ISBN number: ");
String input = scanner.nextLine();
boolean found = false;
for (int i = 0; i < bookList.size(); i++) {
Book book = bookList.get(i);
if (book.getName().equals(input) || book.getIsbn().equals(input)) {
System.out.print("Please enter new author name: ");
String author = scanner.nextLine();
System.out.print("Please enter new publisher name: ");
String publisher = scanner.nextLine();
book.setAuthor(author);
book.setPublisher(publisher);
found = true;
break;
}
}
if (found) {
System.out.println("Book information modified successfully!");
} else {
System.out.println("Book not found!");
}
}
private static void queryBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter book name or ISBN number: ");
String input = scanner.nextLine();
boolean found = false;
for (int i = 0; i < bookList.size(); i++) {
Book book = bookList.get(i);
if (book.getName().equals(input) || book.getIsbn().equals(input)) {
System.out.println(book.toString());
found = true;
break;
}
}
if (!found) {
System.out.println("Book not found!");
}
}
private static void borrowBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter book name or ISBN number: ");
String input = scanner.nextLine();
boolean found = false;
for (int i = 0; i < bookList.size(); i++) {
Book book = bookList.get(i);
if (book.getName().equals(input) || book.getIsbn().equals(input)) {
if (book.getStatus() == Book.AVAILABLE) {
System.out.print("Please enter borrower name: ");
String borrower = scanner.nextLine();
book.setBorrower(borrower);
book.setStatus(Book.BORROWED);
found = true;
System.out.println("Book borrowed successfully!");
} else {
System.out.println("Book is not available for borrowing!");
}
break;
}
}
if (!found) {
System.out.println("Book not found!");
}
}
private static void returnBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter book name or ISBN number: ");
String input = scanner.nextLine();
boolean found = false;
for (int i = 0; i < bookList.size(); i++) {
Book book = bookList.get(i);
if (book.getName().equals(input) || book.getIsbn().equals(input)) {
if (book.getStatus() == Book.BORROWED) {
book.setBorrower("");
book.setStatus(Book.AVAILABLE);
found = true;
System.out.println("Book returned successfully!");
} else {
System.out.println("Book is not borrowed!");
}
break;
}
}
if (!found) {
System.out.println("Book not found!");
}
}
}
class Book {
public static final int AVAILABLE = 1;
public static final int BORROWED = 2;
private String name;
private String author;
private String publisher;
private String isbn;
private int status;
private String borrower;
public Book(String name, String author, String publisher, String isbn) {
this.name = name;
this.author = author;
this.publisher = publisher;
this.isbn = isbn;
this.status = AVAILABLE;
this.borrower = "";
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public String getPublisher() {
return publisher;
}
public String getIsbn() {
return isbn;
}
public int getStatus() {
return status;
}
public String getBorrower() {
return borrower;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public void setStatus(int status) {
this.status = status;
}
public void setBorrower(String borrower) {
this.borrower = borrower;
}
public String toString() {
return "Book name: " + name + "\n" +
"Author: " + author + "\n" +
"Publisher: " + publisher + "\n" +
"ISBN number: " + isbn + "\n" +
"Status: " + (status == AVAILABLE ? "Available" : "Borrowed") + "\n" +
"Borrower: " + borrower + "\n";
}
}
```
这个系统包括了添加图书、删除图书、修改图书信息、查询图书、借阅图书和归还图书等功能。你可以根据需要进行修改和扩展。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)