Java 图书管理系统代码示例 - 简单易懂的实现
下面是一个简单的 Java 图书管理系统的代码示例:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Book {
private String title;
private String author;
private int year;
public Book(String title, String author, int year) {
this.title = title;
this.author = author;
this.year = year;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getYear() {
return year;
}
}
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().equalsIgnoreCase(title)) {
result.add(book);
}
}
return result;
}
public List<Book> searchByAuthor(String author) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getAuthor().equalsIgnoreCase(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 class Main {
public static void main(String[] args) {
Library library = new Library();
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 year");
System.out.println("6. Quit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter book title: ");
String title = scanner.next();
System.out.print("Enter book author: ");
String author = scanner.next();
System.out.print("Enter book year: ");
int year = scanner.nextInt();
Book book = new Book(title, author, year);
library.addBook(book);
System.out.println("Book added successfully.");
break;
case 2:
System.out.print("Enter book title: ");
title = scanner.next();
System.out.print("Enter book author: ");
author = scanner.next();
System.out.print("Enter book year: ");
year = scanner.nextInt();
book = new Book(title, author, year);
library.removeBook(book);
System.out.println("Book removed successfully.");
break;
case 3:
System.out.print("Enter book title: ");
title = scanner.next();
List<Book> searchResult = library.searchByTitle(title);
if (searchResult.isEmpty()) {
System.out.println("No matching books found.");
} else {
System.out.println("Matching books:");
for (Book b : searchResult) {
System.out.println(b.getTitle() + " by " + b.getAuthor() + " (" + b.getYear() + ")");
}
}
break;
case 4:
System.out.print("Enter book author: ");
author = scanner.next();
searchResult = library.searchByAuthor(author);
if (searchResult.isEmpty()) {
System.out.println("No matching books found.");
} else {
System.out.println("Matching books:");
for (Book b : searchResult) {
System.out.println(b.getTitle() + " by " + b.getAuthor() + " (" + b.getYear() + ")");
}
}
break;
case 5:
System.out.print("Enter book year: ");
year = scanner.nextInt();
searchResult = library.searchByYear(year);
if (searchResult.isEmpty()) {
System.out.println("No matching books found.");
} else {
System.out.println("Matching books:");
for (Book b : searchResult) {
System.out.println(b.getTitle() + " by " + b.getAuthor() + " (" + b.getYear() + ")");
}
}
break;
case 6:
System.out.println("Exiting...");
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
这个代码实现了一个简单的图书管理系统,用户可以选择添加书籍、删除书籍、按标题、作者或年份搜索书籍。用户可以在控制台中输入选择,并根据选择执行相应的操作。
原文地址: https://www.cveoy.top/t/topic/o2kw 著作权归作者所有。请勿转载和采集!