用java实现图书管理系统
时间: 2023-06-18 11:02:18 浏览: 70
好的,下面是一个简单的图书管理系统的Java代码实现,其中包括添加图书,删除图书,查找图书和展示所有图书等功能。
```
import java.util.ArrayList;
import java.util.Scanner;
class Book {
private String bookName;
private String author;
private String publishDate;
public Book(String bookName, String author, String publishDate) {
this.bookName = bookName;
this.author = author;
this.publishDate = publishDate;
}
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 String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
}
public class BookManager {
private ArrayList<Book> bookList = new ArrayList<Book>();
public void addBook(Book book) {
bookList.add(book);
System.out.println("添加成功!");
}
public void removeBook(String bookName) {
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getBookName().equals(bookName)) {
bookList.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到该书!");
}
public void findBook(String bookName) {
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getBookName().equals(bookName)) {
System.out.println("书名:" + bookList.get(i).getBookName());
System.out.println("作者:" + bookList.get(i).getAuthor());
System.out.println("出版日期:" + bookList.get(i).getPublishDate());
return;
}
}
System.out.println("没有找到该书!");
}
public void showAllBooks() {
for (int i = 0; i < bookList.size(); i++) {
System.out.println("书名:" + bookList.get(i).getBookName());
System.out.println("作者:" + bookList.get(i).getAuthor());
System.out.println("出版日期:" + bookList.get(i).getPublishDate());
}
}
public static void main(String[] args) {
BookManager bookManager = new BookManager();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1.添加图书");
System.out.println("2.删除图书");
System.out.println("3.查找图书");
System.out.println("4.展示所有图书");
System.out.println("5.退出");
System.out.println("请输入操作序号:");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("请输入书名:");
String bookName = sc.next();
System.out.println("请输入作者:");
String author = sc.next();
System.out.println("请输入出版日期:");
String publishDate = sc.next();
Book book = new Book(bookName, author, publishDate);
bookManager.addBook(book);
break;
case 2:
System.out.println("请输入要删除的书名:");
String deleteBookName = sc.next();
bookManager.removeBook(deleteBookName);
break;
case 3:
System.out.println("请输入要查找的书名:");
String findBookName = sc.next();
bookManager.findBook(findBookName);
break;
case 4:
bookManager.showAllBooks();
break;
case 5:
System.out.println("谢谢使用!");
System.exit(0);
default:
System.out.println("请输入正确的操作序号!");
break;
}
}
}
}
```
这个简单的图书管理系统使用了一个Book类来表示每本书的信息,并使用ArrayList来保存所有书籍。主要功能包括添加图书,删除图书,查找图书和展示所有图书。在主函数中,使用了Scanner类来读取用户输入的操作序号,并根据用户的选择来执行相应的操作。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)