用java写一个黑马程序员的图书管理系统
时间: 2023-08-22 17:07:46 浏览: 368
好的,下面是一个简单的黑马程序员图书管理系统的 Java 实现:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BookManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Book> books = new ArrayList<>();
while (true) {
System.out.println("欢迎来到黑马程序员图书管理系统!");
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(books, scanner);
break;
case 2:
removeBook(books, scanner);
break;
case 3:
displayBooks(books);
break;
case 4:
System.out.println("感谢使用黑马程序员图书管理系统!");
System.exit(0);
default:
System.out.println("输入的选项不正确,请重新输入!");
}
}
}
public static void addBook(ArrayList<Book> books, Scanner scanner) {
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("添加图书成功!");
}
public static void removeBook(ArrayList<Book> books, Scanner scanner) {
System.out.println("请输入要删除的图书的名称:");
String name = scanner.next();
boolean found = false;
for (Book book : books) {
if (book.getName().equals(name)) {
books.remove(book);
System.out.println("删除图书成功!");
found = true;
break;
}
}
if (!found) {
System.out.println("没有找到要删除的图书,请重新输入!");
}
}
public static void displayBooks(ArrayList<Book> books) {
if (books.size() == 0) {
System.out.println("当前没有图书!");
} else {
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 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 "书名:" + name + ",作者:" + author + ",价格:" + price;
}
}
```
这个程序实现了一个简单的图书管理系统,可以通过菜单选择添加、删除和查看图书。本程序使用了 ArrayList 来存储图书信息,每个图书都是一个 Book 类的实例。用户可以通过输入图书名称、作者和价格来添加图书,通过输入图书名称来删除图书,通过查看所有图书来查看当前系统中所有的图书信息。
阅读全文