基于Java实现图书管理代码
时间: 2024-04-04 18:35:10 浏览: 5
以下是一个简单的基于Java实现图书管理的代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
class Book {
private String title;
private String author;
private String isbn;
private int quantity;
public Book(String title, String author, String isbn, int quantity) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.quantity = quantity;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getIsbn() {
return isbn;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
return "Title: " + title + "\nAuthor: " + author + "\nISBN: " + isbn + "\nQuantity: " + quantity;
}
}
class Library {
private ArrayList<Book> books;
public Library() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(String isbn) {
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getIsbn().equals(isbn)) {
books.remove(i);
return;
}
}
System.out.println("Book not found.");
}
public void displayBooks() {
for (Book book : books) {
System.out.println(book);
}
}
public void searchBook(String keyword) {
for (Book book : books) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
System.out.println(book);
}
}
}
public void borrowBook(String isbn) {
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
if (book.getQuantity() > 0) {
book.setQuantity(book.getQuantity() - 1);
System.out.println("Book borrowed successfully.");
return;
} else {
System.out.println("Book not available.");
return;
}
}
}
System.out.println("Book not found.");
}
public void returnBook(String isbn) {
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
book.setQuantity(book.getQuantity() + 1);
System.out.println("Book returned successfully.");
return;
}
}
System.out.println("Book not found.");
}
}
public class Main {
public static void main(String[] args) {
Library library = new Library();
// Add some books
library.addBook(new Book("The Great Gatsby", "F. Scott Fitzgerald", "9780743273565", 5));
library.addBook(new Book("To Kill a Mockingbird", "Harper Lee", "9780446310789", 3));
library.addBook(new Book("1984", "George Orwell", "9780451524935", 2));
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\n1. Add book");
System.out.println("2. Remove book");
System.out.println("3. Display books");
System.out.println("4. Search book");
System.out.println("5. Borrow book");
System.out.println("6. Return book");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter title: ");
String title = scanner.next();
System.out.print("Enter author: ");
String author = scanner.next();
System.out.print("Enter ISBN: ");
String isbn = scanner.next();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
library.addBook(new Book(title, author, isbn, quantity));
System.out.println("Book added successfully.");
break;
case 2:
System.out.print("Enter ISBN: ");
isbn = scanner.next();
library.removeBook(isbn);
break;
case 3:
library.displayBooks();
break;
case 4:
System.out.print("Enter keyword: ");
String keyword = scanner.next();
library.searchBook(keyword);
break;
case 5:
System.out.print("Enter ISBN: ");
isbn = scanner.next();
library.borrowBook(isbn);
break;
case 6:
System.out.print("Enter ISBN: ");
isbn = scanner.next();
library.returnBook(isbn);
break;
case 0:
break;
default:
System.out.println("Invalid choice.");
break;
}
} while (choice != 0);
}
}
```
这个代码实现了一个简单的图书管理系统,包括添加书籍、删除书籍、显示所有书籍、搜索书籍、借出书籍和归还书籍等功能。你可以根据需要进行修改和扩展。