以下是一个简单的图书信息管理系统的Java实现代码,包含基本的图书信息的添加、删除、修改、查询功能。
import java.util.ArrayList;
import java.util.Scanner;
class Book {
private String title;
private String author;
private String publisher;
private int year;
public Book(String title, String author, String publisher, int year) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.year = year;
}
// getter and setters
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 int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
class BookManager {
ArrayList<Book> books = new ArrayList<>();
public void addBook(Book book) {
books.add(book);
}
public void removeBook(int index) {
books.remove(index);
}
public void updateBook(int index, Book book) {
books.set(index, book);
}
public ArrayList<Book> getBooks() {
return books;
}
public ArrayList<Book> searchBooks(String title) {
ArrayList<Book> results = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().contains(title)) {
results.add(book);
}
}
return results;
}
}
public class BookManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BookManager manager = new BookManager();
int choice = 0;
do {
System.out.println("\n1. Add Book");
System.out.println("2. Remove Book");
System.out.println("3. Update Book");
System.out.println("4. Search Books");
System.out.println("5. List Books");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1: // Add Book
System.out.print("\nEnter title: ");
String title = scanner.next();
System.out.print("Enter author: ");
String author = scanner.next();
System.out.print("Enter publisher: ");
String publisher = scanner.next();
System.out.print("Enter year: ");
int year = scanner.nextInt();
Book book = new Book(title, author, publisher, year);
manager.addBook(book);
System.out.println("Book added successfully.");
break;
case 2: // Remove Book
System.out.print("\nEnter index of book to remove (starting from 0): ");
int index = scanner.nextInt();
manager.removeBook(index);
System.out.println("Book removed successfully.");
break;
case 3: // Update Book
System.out.print("\nEnter index of book to update (starting from 0): ");
index = scanner.nextInt();
System.out.print("Enter title: ");
title = scanner.next();
System.out.print("Enter author: ");
author = scanner.next();
System.out.print("Enter publisher: ");
publisher = scanner.next();
System.out.print("Enter year: ");
year = scanner.nextInt();
book = new Book(title, author, publisher, year);
manager.updateBook(index, book);
System.out.println("Book updated successfully.");
break;
case 4: // Search Books
System.out.print("\nEnter title to search: ");
title = scanner.next();
ArrayList<Book> results = manager.searchBooks(title);
System.out.println("Found " + results.size() + " books:");
for (Book result : results) {
System.out.println(result.getTitle() + "\t" + result.getAuthor() + "\t" + result.getPublisher()
+ "\t" + result.getYear());
}
break;
case 5: // List Books
System.out.println("\nList of books:");
ArrayList<Book> books = manager.getBooks();
for (Book b : books) {
System.out.println(b.getTitle() + "\t" + b.getAuthor() + "\t" + b.getPublisher() + "\t"
+ b.getYear());
}
break;
case 6: // Exit
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 6);
}
}
在这个程序中,Book类表示一个图书对象,包含书名、作者、出版社和出版年份等基本信息。BookManager类负责对图书信息的管理,包括图书的添加、删除、修改和查询。主程序BookManagementSystem实现了用户接口,提供了添加图书、删除图书、修改图书、搜索图书和列出所有图书的选项。用户可以通过输入数字选择相应的操作,并按照提示依次输入图书信息。程序使用ArrayList来存储图书信息,并使用switch语句来处理用户操作。