java小型图书管理系统
时间: 2023-09-05 19:13:32 浏览: 101
java小型图书管理系统含完整代码及工具
5星 · 资源好评率100%
好的,下面是一个简单的Java小型图书管理系统的实现:
首先,需要创建一个Book类来表示图书。这个类应该包括图书的属性,如书名、作者、出版社、出版时间、价格等。
```java
public class Book {
private String title;
private String author;
private String publisher;
private String publishDate;
private double price;
public Book(String title, String author, String publisher, String publishDate, double price) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.publishDate = publishDate;
this.price = price;
}
// Getters and setters for the book properties
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 String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
接下来,我们需要创建一个BookManager类来实现图书管理系统的功能,如添加图书、删除图书、查找图书等。
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BookManager {
private ArrayList<Book> bookList;
public BookManager() {
this.bookList = new ArrayList<Book>();
}
public void addBook(Book book) {
bookList.add(book);
}
public void removeBook(int index) {
bookList.remove(index);
}
public void searchBook(String keyword) {
boolean found = false;
for (Book book : bookList) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
System.out.println(book.getTitle() + " by " + book.getAuthor());
found = true;
}
}
if (!found) {
System.out.println("No books found with keyword " + keyword);
}
}
public void displayBooks() {
for (int i = 0; i < bookList.size(); i++) {
Book book = bookList.get(i);
System.out.println((i + 1) + ". " + book.getTitle() + " by " + book.getAuthor());
}
}
public static void main(String[] args) {
BookManager manager = new BookManager();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter 1 to add a book");
System.out.println("Enter 2 to remove a book");
System.out.println("Enter 3 to search for a book");
System.out.println("Enter 4 to display all books");
System.out.println("Enter 0 to exit");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (choice) {
case 0:
System.exit(0);
case 1:
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 publish date: ");
String publishDate = scanner.nextLine();
System.out.print("Enter book price: ");
double price = scanner.nextDouble();
scanner.nextLine(); // Consume the newline character
Book book = new Book(title, author, publisher, publishDate, price);
manager.addBook(book);
System.out.println("Book added successfully!");
break;
case 2:
System.out.print("Enter book index to remove: ");
int index = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
manager.removeBook(index - 1);
System.out.println("Book removed successfully!");
break;
case 3:
System.out.print("Enter search keyword: ");
String keyword = scanner.nextLine();
manager.searchBook(keyword);
break;
case 4:
manager.displayBooks();
break;
default:
System.out.println("Invalid choice!");
}
System.out.println();
}
}
}
```
这个图书管理系统具有添加图书、删除图书、查找图书和显示所有图书的功能。在程序运行时,用户可以输入数字选择要执行的操作。具体的操作流程如下:
1. 添加图书:用户输入书名、作者、出版社、出版时间和价格等信息,程序将这些信息存储到一个Book对象中,并添加到bookList中。
2. 删除图书:用户输入要删除的图书的序号,程序将这个图书从bookList中删除。
3. 查找图书:用户输入一个关键字,程序将遍历bookList,查找所有包含这个关键字的图书,并将它们的标题和作者打印到屏幕上。
4. 显示所有图书:程序将遍历bookList,打印每本书的标题和作者。
这个小型图书管理系统只是一个粗略的实现,还有很多需要完善和改进的地方。例如,可以添加更多的图书属性、改进搜索功能、实现保存和读取图书列表等。
阅读全文