设计一个java的图书管理系统并给出具体的代码实现
时间: 2024-04-30 22:18:35 浏览: 99
好的,下面是一个简单的图书管理系统的代码实现。
首先,我们需要定义一个Book类,表示图书。
```java
public class Book {
private String title;
private String author;
private int year;
private String publisher;
public Book(String title, String author, int year, String publisher) {
this.title = title;
this.author = author;
this.year = year;
this.publisher = publisher;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getYear() {
return year;
}
public String getPublisher() {
return publisher;
}
public String toString() {
return title + " by " + author + ", published in " + year + " by " + publisher;
}
}
```
接下来,我们需要设计一个图书管理系统类,用于添加、删除、查找图书等操作。
```java
import java.util.ArrayList;
import java.util.List;
public class Library {
private List<Book> books;
public Library() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(Book book) {
books.remove(book);
}
public List<Book> searchByTitle(String title) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().equals(title)) {
result.add(book);
}
}
return result;
}
public List<Book> searchByAuthor(String author) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getAuthor().equals(author)) {
result.add(book);
}
}
return result;
}
public List<Book> searchByYear(int year) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getYear() == year) {
result.add(book);
}
}
return result;
}
public List<Book> searchByPublisher(String publisher) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getPublisher().equals(publisher)) {
result.add(book);
}
}
return result;
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (Book book : books) {
sb.append(book.toString()).append("\n");
}
return sb.toString();
}
}
```
最后,我们可以在主函数中演示如何使用图书管理系统。
```java
public class Main {
public static void main(String[] args) {
Library library = new Library();
Book book1 = new Book("Java Programming", "John Smith", 2019, "Apress");
Book book2 = new Book("Python Programming", "Jane Doe", 2020, "O'Reilly");
Book book3 = new Book("C++ Programming", "Bob Johnson", 2018, "Pearson");
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
System.out.println("All books:");
System.out.println(library);
System.out.println("Search by title:");
System.out.println(library.searchByTitle("Java Programming"));
System.out.println("Search by author:");
System.out.println(library.searchByAuthor("Jane Doe"));
System.out.println("Search by year:");
System.out.println(library.searchByYear(2019));
System.out.println("Search by publisher:");
System.out.println(library.searchByPublisher("Pearson"));
library.removeBook(book2);
System.out.println("All books after removing book2:");
System.out.println(library);
}
}
```
输出如下:
```
All books:
Java Programming by John Smith, published in 2019 by Apress
Python Programming by Jane Doe, published in 2020 by O'Reilly
C++ Programming by Bob Johnson, published in 2018 by Pearson
Search by title:
[Java Programming by John Smith, published in 2019 by Apress]
Search by author:
[Python Programming by Jane Doe, published in 2020 by O'Reilly]
Search by year:
[Java Programming by John Smith, published in 2019 by Apress]
Search by publisher:
[C++ Programming by Bob Johnson, published in 2018 by Pearson]
All books after removing book2:
Java Programming by John Smith, published in 2019 by Apress
C++ Programming by Bob Johnson, published in 2018 by Pearson
```
阅读全文