java表格实现管理员对图书的查询和修改,不用数据库
时间: 2024-03-14 19:48:27 浏览: 50
可以使用Java的集合类来实现管理员对图书的查询和修改,不用数据库。具体来说可以使用ArrayList类来存储图书信息,在程序运行过程中对其进行查询和修改操作。以下是简单的示例代码:
```
import java.util.ArrayList;
import java.util.Scanner;
public class BookManager {
private ArrayList<Book> books;
public BookManager() {
books = new ArrayList<Book>();
}
public void addBook(Book book) {
books.add(book);
}
public ArrayList<Book> searchBook(String keyword) {
ArrayList<Book> results = new ArrayList<Book>();
for (Book book : books) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
results.add(book);
}
}
return results;
}
public void modifyBook(Book book) {
int index = books.indexOf(book);
if (index != -1) {
books.set(index, book);
}
}
public static void main(String[] args) {
BookManager manager = new BookManager();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1.添加图书 2.查询图书 3.修改图书");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.println("请输入图书信息(格式:书名,作者,出版社,价格):");
String[] fields = scanner.next().split(",");
Book book = new Book(fields[0], fields[1], fields[2], Double.parseDouble(fields[3]));
manager.addBook(book);
System.out.println("添加成功!");
} else if (choice == 2) {
System.out.println("请输入关键字:");
String keyword = scanner.next();
ArrayList<Book> results = manager.searchBook(keyword);
if (results.size() == 0) {
System.out.println("未找到相关图书!");
} else {
for (Book book : results) {
System.out.println(book);
}
}
} else if (choice == 3) {
System.out.println("请输入要修改的图书信息(格式:书名,作者,出版社,价格):");
String[] fields = scanner.next().split(",");
Book book = new Book(fields[0], fields[1], fields[2], Double.parseDouble(fields[3]));
manager.modifyBook(book);
System.out.println("修改成功!");
}
}
}
}
class Book {
private String title;
private String author;
private String publisher;
private double price;
public Book(String title, String author, String publisher, double price) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.price = price;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getPublisher() {
return publisher;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "书名:" + title + ",作者:" + author + ",出版社:" + publisher + ",价格:" + price;
}
}
```
这个示例代码实现了一个简单的图书管理系统,其中BookManager类负责管理图书信息,包括添加、查询和修改,Book类代表图书实体,包括书名、作者、出版社和价格等属性。在程序运行时,通过Scanner类从控制台读取用户输入,然后进行相应的操作。由于图书信息存储在内存中的ArrayList中,所以不需要使用数据库。
阅读全文