.java图书馆系统,要求模拟实现对数据库的增、删、改、查操作。 利用集合2.要求至少要实现两个业务需求。
时间: 2023-12-16 19:03:10 浏览: 127
biblioteca-trab3-poo-2015:OOP 工作 3 - 2015. 实现图书馆系统
好的,我可以给您提供一个简单的Java图书馆系统实现。
首先,我们需要定义一个Book类,它包含书籍的基本属性,如书名、作者、出版日期和ISBN号。然后,我们可以创建一个BookDatabase类,该类将管理所有图书的增、删、改、查操作。
下面是一个简单的实现:
Book.java
```java
public class Book {
private String title;
private String author;
private String publicationDate;
private String isbn;
public Book(String title, String author, String publicationDate, String isbn) {
this.title = title;
this.author = author;
this.publicationDate = publicationDate;
this.isbn = isbn;
}
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 String getPublicationDate() {
return publicationDate;
}
public void setPublicationDate(String publicationDate) {
this.publicationDate = publicationDate;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
}
```
BookDatabase.java
```java
import java.util.ArrayList;
import java.util.List;
public class BookDatabase {
private List<Book> books;
public BookDatabase() {
this.books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
System.out.println("Book added: " + book.getTitle());
}
public void removeBook(String isbn) {
Book bookToRemove = null;
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
bookToRemove = book;
break;
}
}
if (bookToRemove != null) {
books.remove(bookToRemove);
System.out.println("Book removed: " + bookToRemove.getTitle());
} else {
System.out.println("Book not found");
}
}
public void updateBook(String isbn, Book newBook) {
Book bookToUpdate = null;
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
bookToUpdate = book;
break;
}
}
if (bookToUpdate != null) {
bookToUpdate.setTitle(newBook.getTitle());
bookToUpdate.setAuthor(newBook.getAuthor());
bookToUpdate.setPublicationDate(newBook.getPublicationDate());
System.out.println("Book updated: " + bookToUpdate.getTitle());
} else {
System.out.println("Book not found");
}
}
public void searchBook(String keyword) {
List<Book> results = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
results.add(book);
}
}
if (results.size() > 0) {
System.out.println("Search results:");
for (Book book : results) {
System.out.println(book.getTitle() + " by " + book.getAuthor());
}
} else {
System.out.println("No results found");
}
}
}
```
现在,我们可以创建一个测试类来演示如何使用BookDatabase类:
LibrarySystemTest.java
```java
public class LibrarySystemTest {
public static void main(String[] args) {
BookDatabase bookDatabase = new BookDatabase();
Book book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald", "1925", "978-0743273565");
Book book2 = new Book("To Kill a Mockingbird", "Harper Lee", "1960", "978-0446310789");
Book book3 = new Book("1984", "George Orwell", "1949", "978-0451524935");
bookDatabase.addBook(book1);
bookDatabase.addBook(book2);
bookDatabase.addBook(book3);
bookDatabase.searchBook("Mockingbird");
Book newBook = new Book("Animal Farm", "George Orwell", "1945", "978-0451526342");
bookDatabase.updateBook("978-0451524935", newBook);
bookDatabase.removeBook("978-0743273565");
}
}
```
这个测试类将输出以下内容:
```
Book added: The Great Gatsby
Book added: To Kill a Mockingbird
Book added: 1984
Search results:
To Kill a Mockingbird by Harper Lee
Book updated: 1984
Book removed: The Great Gatsby
```
在这个简单的示例中,我们演示了如何使用BookDatabase类进行添加、删除、更新和搜索图书的操作。您可以添加更多的功能,以满足您的需求。
阅读全文