用Java实现1、图书管理 图书类:图书编号、图书名称、作者、单价、类别 使用集合存储多本图书信息,要求实现以下几个功能: 1)、查询图书 1.1)、查询所有 1.2)、根据编号查询 1.3)、根据书名模糊查询 1.4)、根据作者查询 1.5)、根据类型查询 2)、新增图书 要求用户输入图书信息,图书编号必须要判断是否已经存在 3)、修改图书 要求用户输入图书编号,展示该图书所有信息,询问用户是否修改? 要求用户输入修改信息(不能修改编号),实现修改操作 4)、删除图书 要求用户输入图书编号,展示该图书所有信息,询问用户是否删除?
时间: 2023-06-26 20:07:30 浏览: 138
以下是用Java实现图书管理的代码:
图书类Book.java:
```java
public class Book {
private String id;
private String name;
private String author;
private double price;
private String category;
public Book(String id, String name, String author, double price, String category) {
this.id = id;
this.name = name;
this.author = author;
this.price = price;
this.category = category;
}
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;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
@Override
public String toString() {
return "图书编号:" + id + "\n图书名称:" + name + "\n作者:" + author + "\n单价:" + price + "\n类别:" + category;
}
}
```
图书管理类BookManager.java:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookManager {
private List<Book> bookList;
public BookManager() {
bookList = new ArrayList<>();
}
// 查询所有图书
public void queryAll() {
System.out.println("所有图书信息如下:");
for (Book book : bookList) {
System.out.println(book);
System.out.println("--------------------");
}
}
// 根据编号查询图书
public void queryById() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的图书编号:");
String id = scanner.next();
boolean flag = false;
for (Book book : bookList) {
if (book.getId().equals(id)) {
System.out.println("查询结果如下:");
System.out.println(book);
flag = true;
break;
}
}
if (!flag) {
System.out.println("未找到该编号对应的图书!");
}
}
// 根据书名模糊查询图书
public void queryByName() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的图书名称关键字:");
String keyword = scanner.next();
boolean flag = false;
for (Book book : bookList) {
if (book.getName().contains(keyword)) {
System.out.println("查询结果如下:");
System.out.println(book);
System.out.println("--------------------");
flag = true;
}
}
if (!flag) {
System.out.println("未找到包含该关键字的图书!");
}
}
// 根据作者查询图书
public void queryByAuthor() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的作者名称:");
String author = scanner.next();
boolean flag = false;
for (Book book : bookList) {
if (book.getAuthor().equals(author)) {
System.out.println("查询结果如下:");
System.out.println(book);
System.out.println("--------------------");
flag = true;
}
}
if (!flag) {
System.out.println("未找到该作者的图书!");
}
}
// 根据类型查询图书
public void queryByCategory() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的图书类型:");
String category = scanner.next();
boolean flag = false;
for (Book book : bookList) {
if (book.getCategory().equals(category)) {
System.out.println("查询结果如下:");
System.out.println(book);
System.out.println("--------------------");
flag = true;
}
}
if (!flag) {
System.out.println("未找到该类型的图书!");
}
}
// 新增图书
public void addBook() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要新增的图书信息:");
System.out.print("编号:");
String id = scanner.next();
// 判断编号是否已存在
for (Book book : bookList) {
if (book.getId().equals(id)) {
System.out.println("该编号已存在,请重新输入!");
addBook();
return;
}
}
System.out.print("名称:");
String name = scanner.next();
System.out.print("作者:");
String author = scanner.next();
System.out.print("单价:");
double price = scanner.nextDouble();
System.out.print("类别:");
String category = scanner.next();
Book book = new Book(id, name, author, price, category);
bookList.add(book);
System.out.println("新增成功!");
}
// 修改图书
public void modifyBook() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的图书编号:");
String id = scanner.next();
boolean flag = false;
for (Book book : bookList) {
if (book.getId().equals(id)) {
System.out.println("查询结果如下:");
System.out.println(book);
System.out.println("--------------------");
System.out.println("是否要修改该图书信息?(y/n)");
String choice = scanner.next();
if (choice.equals("y")) {
System.out.println("请输入要修改的信息(除了编号以外的其他信息):");
System.out.print("名称:");
String name = scanner.next();
System.out.print("作者:");
String author = scanner.next();
System.out.print("单价:");
double price = scanner.nextDouble();
System.out.print("类别:");
String category = scanner.next();
book.setName(name);
book.setAuthor(author);
book.setPrice(price);
book.setCategory(category);
System.out.println("修改成功!");
}
flag = true;
break;
}
}
if (!flag) {
System.out.println("未找到该编号对应的图书!");
}
}
// 删除图书
public void deleteBook() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的图书编号:");
String id = scanner.next();
boolean flag = false;
for (Book book : bookList) {
if (book.getId().equals(id)) {
System.out.println("查询结果如下:");
System.out.println(book);
System.out.println("--------------------");
System.out.println("是否要删除该图书?(y/n)");
String choice = scanner.next();
if (choice.equals("y")) {
bookList.remove(book);
System.out.println("删除成功!");
}
flag = true;
break;
}
}
if (!flag) {
System.out.println("未找到该编号对应的图书!");
}
}
}
```
主程序Main.java:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BookManager bookManager = new BookManager();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1、查询图书");
System.out.println("2、新增图书");
System.out.println("3、修改图书");
System.out.println("4、删除图书");
System.out.println("0、退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请选择查询方式:");
System.out.println("1、查询所有");
System.out.println("2、根据编号查询");
System.out.println("3、根据书名模糊查询");
System.out.println("4、根据作者查询");
System.out.println("5、根据类型查询");
int queryChoice = scanner.nextInt();
switch (queryChoice) {
case 1:
bookManager.queryAll();
break;
case 2:
bookManager.queryById();
break;
case 3:
bookManager.queryByName();
break;
case 4:
bookManager.queryByAuthor();
break;
case 5:
bookManager.queryByCategory();
break;
default:
System.out.println("输入有误!");
break;
}
break;
case 2:
bookManager.addBook();
break;
case 3:
bookManager.modifyBook();
break;
case 4:
bookManager.deleteBook();
break;
case 0:
System.out.println("谢谢使用!");
System.exit(0);
default:
System.out.println("输入有误!");
break;
}
}
}
}
```
阅读全文