以下是一個簡單的基于Java的圖書管理系統(tǒng):
Book.java
public class Book {
private int id;
private String title;
private String author;
private String publisher;
private int year;
public Book(int id, String title, String author, String publisher, int year) {
this.id = id;
this.title = title;
this.author = author;
this.publisher = publisher;
this.year = year;
}
public int getId() {
return id;
}
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;
}
}
Library.java
import java.util.ArrayList;
import java.util.List;
public class Library {
private List
public Library() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(int id) {
for (Book book : books) {
if (book.getId() == id) {
books.remove(book);
break;
}
}
}
public Book getBook(int id) {
for (Book book : books) {
if (book.getId() == id) {
return book;
}
}
return null;
}
public List
List
for (Book book : books) {
if (book.getTitle().equals(title)) {
result.add(book);
}
}
return result;
}
public List
List
for (Book book : books) {
if (book.getAuthor().equals(author)) {
result.add(book);
}
}
return result;
}
public List
List
for (Book book : books) {
if (book.getPublisher().equals(publisher)) {
result.add(book);
}
}
return result;
}
public List
List
for (Book book : books) {
if (book.getYear() == year) {
result.add(book);
}
}
return result;
}
}
Main.java
import java.util.List;
import java.util.Scanner;
public class Main {
private static Library library = new Library();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n1. Add book");
System.out.println("2. Remove book");
System.out.println("3. Search book");
System.out.println("4. Exit");
System.out.print("\nEnter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addBook(scanner);
break;
case 2:
removeBook(scanner);
break;
case 3:
searchBook(scanner);
break;
case 4:
System.exit(0);
default:
System.out.println("Invalid choice.");
}
}
}
private static void addBook(Scanner scanner) {
System.out.print("\nEnter book ID: ");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter book author: ");
String author = scanner.nextLine();
System.out.print("Enter book publisher: ");
String publisher = scanner.nextLine();
System.out.print("Enter book year: ");
int year = scanner.nextInt();
Book book = new Book(id, title, author, publisher, year);
library.addBook(book);
System.out.println("\nBook added successfully.");
}
private static void removeBook(Scanner scanner) {
System.out.print("\nEnter book ID: ");
int id = scanner.nextInt();
Book book = library.getBook(id);
if (book == null) {
System.out.println("Book not found.");
} else {
library.removeBook(id);
System.out.println("\nBook removed successfully.");
}
}
private static void searchBook(Scanner scanner) {
System.out.println("\n1. Search by title");
System.out.println("2. Search by author");
System.out.println("3. Search by publisher");
System.out.println("4. Search by year");
System.out.print("\nEnter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
searchByTitle(scanner);
break;
case 2:
searchByAuthor(scanner);
break;
case 3:
searchByPublisher(scanner);
break;
case 4:
searchByYear(scanner);
break;
default:
System.out.println("Invalid choice.");
}
}
private static void searchByTitle(Scanner scanner) {
System.out.print("\nEnter book title: ");
String title = scanner.nextLine();
List
printSearchResult(result);
}
private static void searchByAuthor(Scanner scanner) {
System.out.print("\nEnter book author: ");
String author = scanner.nextLine();
List
printSearchResult(result);
}
private static void searchByPublisher(Scanner scanner) {
System.out.print("\nEnter book publisher: ");
String publisher = scanner.nextLine();
List
printSearchResult(result);
}
private static void searchByYear(Scanner scanner) {
System.out.print("\nEnter book year: ");
int year = scanner.nextInt();
List
printSearchResult(result);
}
private static void printSearchResult(List
if (result.size() == 0) {
System.out.println("\nNo books found.");
} else {
System.out.println("\nSearch result:");
for (Book book : result) {
System.out.println(book.getId() + "\t" + book.getTitle() + "\t" + book.getAuthor() + "\t" + book.getPublisher() + "\t" + book.getYear());
}
}
}
}
這個圖書管理系統(tǒng)可以添加、刪除和搜索圖書。用戶可以根據(jù)書名、作者、出版商和年份搜索圖書。