用Java写一个小图书管理系统
时间: 2025-01-01 22:40:32 浏览: 5
创建一个简单的图书管理系统,我们可以使用Java的基本控制结构、集合框架以及一些基本的数据库操作。以下是一个简化的例子,使用了命令行界面作为用户交互:
```java
import java.util.*;
class Book {
String title;
String author;
int quantity;
// constructor and getters/setters
}
class Library {
private Map<String, Book> books;
public Library() {
this.books = new HashMap<>();
}
public void addBook(Book book) {
books.put(book.getTitle(), book);
}
public void removeBook(String title) {
books.remove(title);
}
public Book findBook(String title) {
return books.get(title);
}
}
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. 添加书籍");
System.out.println("2. 删除书籍");
System.out.println("3. 查找书籍");
System.out.print("请输入选项(1-3): ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("输入书名: ");
String title = scanner.next();
System.out.print("作者: ");
String author = scanner.next();
System.out.print("库存量: ");
int quantity = scanner.nextInt();
Book book = new Book();
book.setTitle(title);
book.setAuthor(author);
book.setQuantity(quantity);
library.addBook(book);
break;
case 2:
System.out.print("输入要删除的书名: ");
title = scanner.next();
library.removeBook(title);
break;
case 3:
System.out.print("输入查找的书名: ");
title = scanner.next();
Book foundBook = library.findBook(title);
if (foundBook != null) {
System.out.println("找到的书籍信息: " + foundBook);
} else {
System.out.println("未找到该书籍");
}
break;
default:
System.out.println("无效的选择");
}
scanner.nextLine(); // 清理输入缓冲区
}
}
}
```
阅读全文