我也发一发
package bookborrowingsystem;
public class Book {
private int num;
private String name;
public Book(int num,String name) {
this.num = num;
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
=============================================================================
package bookborrowingsystem;
import java.util.Scanner;
public class BookBorrowingSystem {
private Book[] books = {new Book(1,"Java入门第一季教程"),new Book(2,"Java入门第二季教程"),new Book(3,"Java入门第三季教程")};
private static Scanner input = new Scanner(System.in);
public String findBookNum() {
while(true) {
System.out.println("输入图书序号");
if(!input.hasNextInt()){
System.out.println("输入不匹配异常!请根据提示输入数字命令!");
input.nextLine();
continue;
}
int num = input.nextInt();
for(Book book:books) {
if(book.getNum()==num) {
return book.getName();
}
System.out.println("图书不存在!");
break;
}
}
}
public String findBookName() throws Exception {
while(true) {
System.out.println("输入图书名称");
String name = input.next();
for(Book book:books) {
if(book.getName().equals(name)) {
return book.getName();
}
System.out.println("图书不存在!");
break;
}
}
}
}
=============================================================================
package bookborrowingsystem;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
BookBorrowingSystem bookBorrowingSystem = new BookBorrowingSystem();
Scanner input = new Scanner(System.in);
while(true){
try {
System.out.println("模拟借书系统:1-按照序号查找图书;2-按照名称查找图书");
int choice = input.nextInt();//
String bookName = null;
if(choice==1) {
bookName = bookBorrowingSystem.findBookNum();
}else if(choice==2) {
bookName = bookBorrowingSystem.findBookName();
}else {
System.out.println("命令输入错误!请根据提示输入数字命令!");
continue;
}
if(bookName == null) {
System.out.println("图书不存在!");
continue;
}
System.out.println("找到这本名为《"+bookName+"》的书");
break;
}catch (InputMismatchException e) {
System.out.println("输入不匹配异常!请根据提示输入数字命令!");
input.nextLine();
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
}