帮我用java写一份图书管理系统
时间: 2023-07-15 13:15:19 浏览: 205
好的,以下是一个简单的图书管理系统的Java代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class LibrarySystem {
private ArrayList<Book> books;
public LibrarySystem() {
this.books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
System.out.println("添加成功!");
}
public void removeBook(int bookId) {
for (Book book : books) {
if (book.getId() == bookId) {
books.remove(book);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到该书籍!");
}
public void searchBook(String keyword) {
for (Book book : books) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
System.out.println(book);
}
}
}
public void displayAllBooks() {
for (Book book : books) {
System.out.println(book);
}
}
public static void main(String[] args) {
LibrarySystem system = new LibrarySystem();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入操作编号:");
System.out.println("1. 添加书籍");
System.out.println("2. 删除书籍");
System.out.println("3. 搜索书籍");
System.out.println("4. 显示所有书籍");
System.out.println("0. 退出程序");
int option = scanner.nextInt();
scanner.nextLine();
switch (option) {
case 0:
System.out.println("再见!");
System.exit(0);
case 1:
System.out.println("请输入书籍信息(格式:书名,作者,出版社,价格):");
String[] info = scanner.nextLine().split(",");
Book book = new Book(info[0], info[1], info[2], Double.parseDouble(info[3]));
system.addBook(book);
break;
case 2:
System.out.println("请输入要删除的书籍编号:");
int id = scanner.nextInt();
scanner.nextLine();
system.removeBook(id);
break;
case 3:
System.out.println("请输入搜索关键词:");
String keyword = scanner.nextLine();
system.searchBook(keyword);
break;
case 4:
system.displayAllBooks();
break;
default:
System.out.println("无效操作!");
}
}
}
}
class Book {
private static int count = 1;
private int id;
private String title;
private String author;
private String publisher;
private double price;
public Book(String title, String author, String publisher, double price) {
this.id = count++;
this.title = title;
this.author = author;
this.publisher = publisher;
this.price = price;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getPublisher() {
return publisher;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "编号:" + id + ",书名:" + title + ",作者:" + author + ",出版社:" + publisher + ",价格:" + price;
}
}
```
这个图书管理系统包括以下功能:
1. 添加书籍
2. 删除书籍
3. 搜索书籍
4. 显示所有书籍
你可以根据自己的需求进行修改和扩展。
阅读全文