java代码实现图书管理系统
时间: 2023-01-11 11:55:36 浏览: 227
Java实现图书管理系统
好的,我可以给出一些 Java 代码来实现图书管理系统。首先,我们可以定义一个图书类来表示图书,其中包含图书的标题、作者、出版社、ISBN 等信息。
```
public class Book {
private String title;
private String author;
private String publisher;
private String isbn;
public Book(String title, String author, String publisher, String isbn) {
this.title = title;
this.author = author;
this.publisher = publisher;
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 getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
}
```
然后,我们可以创建一个图书库类来管理图书。这个类可以包含一个 ArrayList 来存储图书,以及一些方法来添加、删除、查询和打印图书信息。
```
import java.util.ArrayList;
import java.util.List;
public class Library {
private List<Book> books;
public Library() {
this.books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void deleteBook(Book book) {
books.remove(book);
}
public List<Book> searchBookByTitle(String title) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().equals(title)) {
result.add(book);
}
}
return result;
}
public List<Book> searchBookByAuthor(String author) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getAuthor().equals(author)) {
result.add(book);
}
}
return result;
阅读全文