交作业(1-9图书查询系统)
package book_retrieval_system;
import java.util.Scanner;
public class RetrievalSystem {
String[] books = {"Head First Java","Java数据结构","Java从入门到精通"};
public static void main(String[] args) {
// TODO Auto-generated method stub
RetrievalSystem find = new RetrievalSystem();
find.books();
}
public void books(){
try {
System.out.println("请选择查找方式:1、以书名查找。2、以图书序号查找。");
Scanner inputWays = new Scanner (System.in);
int way = inputWays.nextInt();
if(way==1||way==2) {
ways(way);
}else {
System.out.println("命令输入错误,请输入1或2");
books();
}
}catch(Exception ex) {
System.out.println("命令输入错误,请输入1或2");
books();
}
}
public void ways(int way) {
if(way==1) {
Scanner input1 = new Scanner(System.in);
System.out.println("请输入书名:");
String bookName = input1.next();
if (contains(books,bookName)==true) {
System.out.println("您要找的图书是:" + bookName);
books();
}else {
System.out.println("图书不存在");
books();
}
}else if(way==2) {
Scanner input2 = new Scanner(System.in);
System.out.println("请输入序号:");
int bookNumber = input2.nextInt();
if(bookNumber<=books.length&&bookNumber>0) {
System.out.println("您要找的图书是:" + books[bookNumber-1]);
books();
}else {
System.out.println("您输入有误或图书不存在");
books();
}
}
}
private boolean contains(String[] books,String bookName) {
// TODO Auto-generated method stub
for(String find:books) {
if(find.equals(bookName))
return true;
}
return false;
}
}