用java写出一个图书管理系统
时间: 2023-10-03 07:05:45 浏览: 65
好的,下面是一个简单的图书管理系统的Java代码实现,包括添加、删除、查询和展示图书信息的功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BookManager {
// 定义图书类
static class Book {
String name;
String author;
String ISBN;
double price;
public Book(String name, String author, String ISBN, double price) {
this.name = name;
this.author = author;
this.ISBN = ISBN;
this.price = price;
}
}
// 图书集合
static ArrayList<Book> books = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("----------------------------------------");
System.out.println("1.添加图书 2.删除图书 3.查询图书 4.展示所有图书 5.退出系统");
System.out.println("----------------------------------------");
System.out.print("请选择功能:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addBook();
break;
case 2:
removeBook();
break;
case 3:
searchBook();
break;
case 4:
showBooks();
break;
case 5:
System.out.println("感谢使用图书管理系统!");
System.exit(0);
default:
System.out.println("输入有误,请重新选择!");
}
}
}
// 添加图书
private static void addBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入书名:");
String name = scanner.nextLine();
System.out.print("请输入作者:");
String author = scanner.nextLine();
System.out.print("请输入ISBN号:");
String ISBN = scanner.nextLine();
System.out.print("请输入价格:");
double price = scanner.nextDouble();
Book book = new Book(name, author, ISBN, price);
books.add(book);
System.out.println("添加成功!");
}
// 删除图书
private static void removeBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的图书的ISBN号:");
String ISBN = scanner.nextLine();
for (int i = 0; i < books.size(); i++) {
if (books.get(i).ISBN.equals(ISBN)) {
books.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到该图书!");
}
// 查询图书
private static void searchBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查询的图书的ISBN号:");
String ISBN = scanner.nextLine();
for (int i = 0; i < books.size(); i++) {
if (books.get(i).ISBN.equals(ISBN)) {
System.out.println("书名:" + books.get(i).name);
System.out.println("作者:" + books.get(i).author);
System.out.println("价格:" + books.get(i).price);
return;
}
}
System.out.println("没有找到该图书!");
}
// 展示所有图书
private static void showBooks() {
if (books.size() == 0) {
System.out.println("当前没有图书!");
return;
}
System.out.println("当前共有" + books.size() + "本图书:");
for (int i = 0; i < books.size(); i++) {
System.out.println("书名:" + books.get(i).name);
System.out.println("作者:" + books.get(i).author);
System.out.println("ISBN:" + books.get(i).ISBN);
System.out.println("价格:" + books.get(i).price);
System.out.println();
}
}
}
```
这是一个简单的图书管理系统,包含基本的增、删、查和展示功能。你可以根据自己的需求进行修改和扩展。
阅读全文