用java 完整的写一个图书管理系统
时间: 2023-12-09 10:07:04 浏览: 62
好的,我可以给您提供一个Java实现的图书管理系统的基本代码框架。该系统实现了基本的图书管理功能,包括添加图书、查询图书、修改图书、删除图书等。具体实现细节可以根据您的具体要求进行修改。
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BookManagementSystem {
private ArrayList<Book> bookList = new ArrayList<Book>();
private Scanner scanner = new Scanner(System.in);
// 添加图书
public void addBook() {
System.out.println("请输入图书编号:");
String bookId = scanner.nextLine();
System.out.println("请输入图书名称:");
String bookName = scanner.nextLine();
System.out.println("请输入图书作者:");
String author = scanner.nextLine();
System.out.println("请输入图书价格:");
double price = scanner.nextDouble();
scanner.nextLine(); // 读取多余的回车符
Book book = new Book(bookId, bookName, author, price);
bookList.add(book);
System.out.println("添加成功!");
}
// 查询图书
public void queryBook() {
System.out.println("请输入要查询的图书名称:");
String bookName = scanner.nextLine();
for (Book book : bookList) {
if (book.getBookName().equals(bookName)) {
System.out.println(book);
return;
}
}
System.out.println("没有找到该图书!");
}
// 修改图书
public void modifyBook() {
System.out.println("请输入要修改的图书编号:");
String bookId = scanner.nextLine();
for (Book book : bookList) {
if (book.getBookId().equals(bookId)) {
System.out.println("请输入新的图书名称:");
String bookName = scanner.nextLine();
System.out.println("请输入新的图书作者:");
String author = scanner.nextLine();
System.out.println("请输入新的图书价格:");
double price = scanner.nextDouble();
scanner.nextLine(); // 读取多余的回车符
book.setBookName(bookName);
book.setAuthor(author);
book.setPrice(price);
System.out.println("修改成功!");
return;
}
}
System.out.println("没有找到该图书!");
}
// 删除图书
public void deleteBook() {
System.out.println("请输入要删除的图书编号:");
String bookId = scanner.nextLine();
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getBookId().equals(bookId)) {
bookList.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到该图书!");
}
// 显示所有图书
public void displayAllBooks() {
if (bookList.isEmpty()) {
System.out.println("图书馆为空!");
return;
}
for (Book book : bookList) {
System.out.println(book);
}
}
public static void main(String[] args) {
BookManagementSystem bms = new BookManagementSystem();
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("5. 显示所有图书");
System.out.println("0. 退出系统");
System.out.println("请选择操作:");
int choice = scanner.nextInt();
scanner.nextLine(); // 读取多余的回车符
switch (choice) {
case 1:
bms.addBook();
break;
case 2:
bms.queryBook();
break;
case 3:
bms.modifyBook();
break;
case 4:
bms.deleteBook();
break;
case 5:
bms.displayAllBooks();
break;
case 0:
System.out.println("谢谢使用!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
}
}
}
}
class Book {
private String bookId; // 图书编号
private String bookName; // 图书名称
private String author; // 作者
private double price; // 价格
public Book(String bookId, String bookName, String author, double price) {
this.bookId = bookId;
this.bookName = bookName;
this.author = author;
this.price = price;
}
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "图书编号:" + bookId + ",图书名称:" + bookName + ",作者:" + author + ",价格:" + price;
}
}
```
以上为一个简单的图书管理系统的Java实现,可以根据您的需要进行修改和扩展。
阅读全文