从键盘依次输入10本图书信息(以顺序表存放),分别编写按书号、书名及价格等信息的查找函数,实现图书信息的查找。 说明:图书信息顺序表数据结构定义。 class Book{ public String isbn; public String bname; public String publish; public float price; } class BookManage{ private final int BOOK_MAX = 100; private Book []books; private int size; public BookManage(){ books = new Book[BOOK_MAX]; } public void add(Book book){ books[size] = book; size ++; } public int findBookByIsbn(int index, String isbn){ // 补充代码 ...... } public int findBookByBname(int index, String bname){ // 补充代码 ...... } public int findBookByPrice(int index, float price){ // 补充代码 ...... } } // findBookByIsbn()方法的代码 // findBookByBname()方法的代码 // findBookByPrice()方法的代码 // main()代码,要求输入多本书信息,再依次执行3种查找方法
时间: 2024-03-02 14:49:33 浏览: 391
以下是完整的代码实现:
```
import java.util.Scanner;
class Book {
public String isbn;
public String bname;
public String publish;
public float price;
}
class BookManage {
private final int BOOK_MAX = 100;
private Book[] books;
private int size;
public BookManage() {
books = new Book[BOOK_MAX];
}
public void add(Book book) {
books[size] = book;
size++;
}
public int findBookByIsbn(int index, String isbn) {
if (index < 0 || index >= size) {
return -1;
}
if (books[index].isbn.equals(isbn)) {
return index;
} else {
return findBookByIsbn(index + 1, isbn);
}
}
public int findBookByBname(int index, String bname) {
if (index < 0 || index >= size) {
return -1;
}
if (books[index].bname.equals(bname)) {
return index;
} else {
return findBookByBname(index + 1, bname);
}
}
public int findBookByPrice(int index, float price) {
if (index < 0 || index >= size) {
return -1;
}
if (books[index].price == price) {
return index;
} else {
return findBookByPrice(index + 1, price);
}
}
}
public class Main {
public static void main(String[] args) {
BookManage bookManage = new BookManage();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入10本书的信息:");
for (int i = 0; i < 10; i++) {
Book book = new Book();
System.out.print("第" + (i + 1) + "本书的书号:");
book.isbn = scanner.next();
System.out.print("第" + (i + 1) + "本书的书名:");
book.bname = scanner.next();
System.out.print("第" + (i + 1) + "本书的出版社:");
book.publish = scanner.next();
System.out.print("第" + (i + 1) + "本书的价格:");
book.price = scanner.nextFloat();
bookManage.add(book);
}
System.out.print("请输入要查找的书的书号:");
String isbn = scanner.next();
int index = bookManage.findBookByIsbn(0, isbn);
if (index == -1) {
System.out.println("未找到该书!");
} else {
System.out.println("该书的信息如下:");
System.out.println("书号:" + bookManage.books[index].isbn);
System.out.println("书名:" + bookManage.books[index].bname);
System.out.println("出版社:" + bookManage.books[index].publish);
System.out.println("价格:" + bookManage.books[index].price);
}
System.out.print("请输入要查找的书的书名:");
String bname = scanner.next();
index = bookManage.findBookByBname(0, bname);
if (index == -1) {
System.out.println("未找到该书!");
} else {
System.out.println("该书的信息如下:");
System.out.println("书号:" + bookManage.books[index].isbn);
System.out.println("书名:" + bookManage.books[index].bname);
System.out.println("出版社:" + bookManage.books[index].publish);
System.out.println("价格:" + bookManage.books[index].price);
}
System.out.print("请输入要查找的书的价格:");
float price = scanner.nextFloat();
index = bookManage.findBookByPrice(0, price);
if (index == -1) {
System.out.println("未找到该书!");
} else {
System.out.println("该书的信息如下:");
System.out.println("书号:" + bookManage.books[index].isbn);
System.out.println("书名:" + bookManage.books[index].bname);
System.out.println("出版社:" + bookManage.books[index].publish);
System.out.println("价格:" + bookManage.books[index].price);
}
}
}
```
运行程序后,按照提示输入10本书的信息,然后分别输入要查找的书的书号、书名和价格,程序会输出相应的书籍信息。
阅读全文