用Java代码编写一个无图形界面的图书管理系统
时间: 2023-10-03 21:09:20 浏览: 99
好的,以下是一个简单的无图形界面的图书管理系统的Java代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BookManagementSystem {
private static ArrayList<Book> bookList = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("欢迎使用图书管理系统!");
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. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
searchBook();
break;
case 4:
displayBooks();
break;
case 5:
System.out.println("感谢使用图书管理系统!");
System.exit(0);
break;
default:
System.out.println("输入有误,请重新选择!");
break;
}
}
}
// 添加图书
private static void addBook() {
System.out.println("请输入图书编号:");
String id = scanner.next();
System.out.println("请输入图书名称:");
String name = scanner.next();
System.out.println("请输入图书作者:");
String author = scanner.next();
System.out.println("请输入图书价格:");
double price = scanner.nextDouble();
Book book = new Book(id, name, author, price);
bookList.add(book);
System.out.println("添加成功!");
}
// 删除图书
private static void deleteBook() {
System.out.println("请输入要删除的图书编号:");
String id = scanner.next();
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getId().equals(id)) {
bookList.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该图书编号!");
}
// 查找图书
private static void searchBook() {
System.out.println("请输入要查找的图书编号:");
String id = scanner.next();
for (Book book : bookList) {
if (book.getId().equals(id)) {
System.out.println(book);
return;
}
}
System.out.println("未找到该图书编号!");
}
// 显示所有图书
private static void displayBooks() {
for (Book book : bookList) {
System.out.println(book);
}
}
}
class Book {
private String id;
private String name;
private String author;
private double price;
public Book(String id, String name, String author, double price) {
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "图书编号:" + id + ",图书名称:" + name + ",图书作者:" + author + ",图书价格:" + price;
}
}
```
这个图书管理系统可以执行以下功能:
1. 添加图书
2. 删除图书
3. 查找图书
4. 显示所有图书
5. 退出系统
您可以根据自己的需求对代码进行修改和扩展。
阅读全文