编写一个图书管理系统,实现图书上架、下架、图书信息修改、图书信息查询等功能。
时间: 2023-12-04 17:05:53 浏览: 86
好的,为了简化代码,我将使用Java的面向对象特性,使用类来实现图书管理系统。以下是一个简单的图书管理系统的实现:
```
import java.util.ArrayList;
import java.util.Scanner;
public class BookManagementSystem {
private ArrayList<Book> books;
public BookManagementSystem() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
System.out.println("添加图书成功!");
}
public void removeBook(String bookName) {
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getName().equals(bookName)) {
books.remove(i);
System.out.println("删除图书成功!");
}
}
}
public void updateBook(String bookName, Book newBook) {
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getName().equals(bookName)) {
books.set(i, newBook);
System.out.println("更新图书成功!");
}
}
}
public void searchBook(String bookName) {
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getName().equals(bookName)) {
System.out.println("查询结果为:" + books.get(i).toString());
}
}
}
public void showAllBooks() {
System.out.println("所有图书信息如下:");
for (Book book : books) {
System.out.println(book.toString());
}
}
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("6. 退出程序");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.println("请输入要添加的图书信息(书名 作者 出版社 ISBN):");
String[] bookInfo = scanner.nextLine().split(" ");
bms.addBook(new Book(bookInfo[0], bookInfo[1], bookInfo[2], bookInfo[3]));
break;
case 2:
System.out.println("请输入要删除的图书名称:");
String bookName = scanner.nextLine();
bms.removeBook(bookName);
break;
case 3:
System.out.println("请输入要更新的图书名称和新的图书信息(书名 作者 出版社 ISBN):");
String[] updateInfo = scanner.nextLine().split(" ");
bms.updateBook(updateInfo[0], new Book(updateInfo[0], updateInfo[1], updateInfo[2], updateInfo[3]));
break;
case 4:
System.out.println("请输入要查询的图书名称:");
bookName = scanner.nextLine();
bms.searchBook(bookName);
break;
case 5:
bms.showAllBooks();
break;
case 6:
System.out.println("程序已退出!");
System.exit(0);
break;
default:
System.out.println("输入错误,请重新输入!");
}
}
}
}
class Book {
private String name;
private String author;
private String publisher;
private String ISBN;
public Book(String name, String author, String publisher, String ISBN) {
this.name = name;
this.author = author;
this.publisher = publisher;
this.ISBN = ISBN;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getISBN() {
return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
@Override
public String toString() {
return "书名:" + name + " 作者:" + author + " 出版社:" + publisher + " ISBN号:" + ISBN;
}
}
```
以上就是一个简单的图书管理系统的实现,实现了图书的添加、删除、修改、查询和查看所有图书信息的功能。
阅读全文