用java写一个基于控制台的图书管理系统
时间: 2023-05-19 09:05:36 浏览: 114
基于Java实现(控制台)图书信息管理系统【100011587】
5星 · 资源好评率100%
好的,以下是一个基于控制台的图书管理系统的 Java 代码示例:
```
import java.util.ArrayList;
import java.util.Scanner;
public class BookManagementSystem {
private static ArrayList<Book> books = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加图书");
System.out.println("2. 删除图书");
System.out.println("3. 查看所有图书");
System.out.println("4. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
showAllBooks();
break;
case 4:
System.out.println("感谢使用图书管理系统!");
System.exit(0);
default:
System.out.println("无效的选择,请重新输入!");
break;
}
}
}
private static void addBook() {
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(name, author, price);
books.add(book);
System.out.println("添加成功!");
}
private static void deleteBook() {
System.out.println("请输入要删除的图书名称:");
String name = scanner.next();
for (Book book : books) {
if (book.getName().equals(name)) {
books.remove(book);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该图书!");
}
private static void showAllBooks() {
System.out.println("所有图书如下:");
for (Book book : books) {
System.out.println(book);
}
}
}
class Book {
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
```
希望这个示例能够帮助你实现一个基于控制台的图书管理系统。
阅读全文