java程序提供图书馆馆藏书籍管理功能:(使用Map实现) A. 书籍信息包括书名、作者(可能多人,使用ArrayList实现)、出版社、及馆藏数量。C. 提供针对书籍书名、作者、出版社的查询功能:书名可进行模糊查询(即部分文字检索功能);作者及出版社查询可能返回多条信息结果(遍历使用迭代器实现) D. 可添加书籍(已存在的数量增加,新的书籍增加条目)或删除已有书籍信息。(删除书籍不能删除存在借阅情况的书籍)
时间: 2024-03-19 16:40:26 浏览: 113
好的,这是一个很好的题目。以下是Java程序的代码实现:
```
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class LibraryManagementSystem {
private Map<String, ArrayList<Book>> books;
public LibraryManagementSystem() {
books = new HashMap<String, ArrayList<Book>>();
}
public void addBook(Book book) {
if (books.containsKey(book.getTitle())) {
ArrayList<Book> list = books.get(book.getTitle());
boolean found = false;
for (Book b : list) {
if (b.getAuthor().equals(book.getAuthor())
&& b.getPublisher().equals(book.getPublisher())) {
found = true;
b.setQuantity(b.getQuantity() + book.getQuantity());
break;
}
}
if (!found) {
list.add(book);
}
} else {
ArrayList<Book> list = new ArrayList<Book>();
list.add(book);
books.put(book.getTitle(), list);
}
}
public void removeBook(String title, String author, String publisher) {
if (books.containsKey(title)) {
ArrayList<Book> list = books.get(title);
Iterator<Book> iter = list.iterator();
while (iter.hasNext()) {
Book book = iter.next();
if (book.getAuthor().equals(author)
&& book.getPublisher().equals(publisher)) {
if (book.getQuantity() > 0) {
book.setQuantity(book.getQuantity() - 1);
} else {
iter.remove();
}
break;
}
}
if (list.isEmpty()) {
books.remove(title);
}
}
}
public ArrayList<Book> searchByTitle(String title) {
ArrayList<Book> result = new ArrayList<Book>();
Set<String> keys = books.keySet();
for (String key : keys) {
if (key.contains(title)) {
result.addAll(books.get(key));
}
}
return result;
}
public ArrayList<Book> searchByAuthor(String author) {
ArrayList<Book> result = new ArrayList<Book>();
Set<String> keys = books.keySet();
for (String key : keys) {
ArrayList<Book> list = books.get(key);
for (Book book : list) {
if (book.getAuthors().contains(author)) {
result.add(book);
}
}
}
return result;
}
public ArrayList<Book> searchByPublisher(String publisher) {
ArrayList<Book> result = new ArrayList<Book>();
Set<String> keys = books.keySet();
for (String key : keys) {
ArrayList<Book> list = books.get(key);
for (Book book : list) {
if (book.getPublisher().equals(publisher)) {
result.add(book);
}
}
}
return result;
}
public void printBooks(ArrayList<Book> list) {
for (Book book : list) {
System.out.println(book);
}
}
public static void main(String[] args) {
LibraryManagementSystem system = new LibraryManagementSystem();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Add book");
System.out.println("2. Remove book");
System.out.println("3. Search by title");
System.out.println("4. Search by author");
System.out.println("5. Search by publisher");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter title: ");
String title = scanner.nextLine();
System.out.print("Enter author(s): ");
String[] authors = scanner.nextLine().split(",");
ArrayList<String> authorList = new ArrayList<String>();
for (String author : authors) {
authorList.add(author.trim());
}
System.out.print("Enter publisher: ");
String publisher = scanner.nextLine();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
Book book = new Book(title, authorList, publisher, quantity);
system.addBook(book);
break;
case 2:
System.out.print("Enter title: ");
title = scanner.nextLine();
System.out.print("Enter author(s): ");
authors = scanner.nextLine().split(",");
authorList = new ArrayList<String>();
for (String author : authors) {
authorList.add(author.trim());
}
System.out.print("Enter publisher: ");
publisher = scanner.nextLine();
system.removeBook(title, authorList.get(0), publisher);
break;
case 3:
System.out.print("Enter title: ");
title = scanner.nextLine();
ArrayList<Book> list = system.searchByTitle(title);
if (list.isEmpty()) {
System.out.println("No books found.");
} else {
system.printBooks(list);
}
break;
case 4:
System.out.print("Enter author: ");
String author = scanner.nextLine();
list = system.searchByAuthor(author);
if (list.isEmpty()) {
System.out.println("No books found.");
} else {
system.printBooks(list);
}
break;
case 5:
System.out.print("Enter publisher: ");
publisher = scanner.nextLine();
list = system.searchByPublisher(publisher);
if (list.isEmpty()) {
System.out.println("No books found.");
} else {
system.printBooks(list);
}
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
break;
}
}
}
}
class Book {
private String title;
private ArrayList<String> authors;
private String publisher;
private int quantity;
public Book(String title, ArrayList<String> authors, String publisher, int quantity) {
this.title = title;
this.authors = authors;
this.publisher = publisher;
this.quantity = quantity;
}
public String getTitle() {
return title;
}
public ArrayList<String> getAuthors() {
return authors;
}
public String getAuthor() {
StringBuilder sb = new StringBuilder();
for (String author : authors) {
sb.append(author);
sb.append(", ");
}
sb.delete(sb.length() - 2, sb.length());
return sb.toString();
}
public String getPublisher() {
return publisher;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Title: ");
sb.append(title);
sb.append("\nAuthor(s): ");
sb.append(getAuthor());
sb.append("\nPublisher: ");
sb.append(publisher);
sb.append("\nQuantity: ");
sb.append(quantity);
sb.append("\n");
return sb.toString();
}
}
```
希望这个代码可以解决你的问题。
阅读全文